Skip to content

proxy

@providerprotocol/ai


@providerprotocol/ai / proxy

Proxy provider for UPP (Unified Provider Protocol)

This module exports the proxy provider for transporting PP requests over HTTP to a backend server, plus server-side utilities for implementing proxy endpoints.

import { proxy } from '@providerprotocol/ai/proxy';
import { llm } from '@providerprotocol/ai';
const backend = proxy({ endpoint: '/api/ai' });
const instance = llm({ model: backend('default') });
const turn = await instance.generate('Hello!');
import { llm, anthropic } from '@providerprotocol/ai';
import { parseBody, toJSON, toSSE, toError } from '@providerprotocol/ai/proxy';
Bun.serve({
async fetch(req) {
if (req.method === 'POST' && new URL(req.url).pathname === '/api/ai') {
try {
const { messages, system, params } = parseBody(await req.json());
const instance = llm({
model: anthropic('claude-sonnet-4-20250514'),
system,
params: { max_tokens: 4096, ...params },
});
const wantsStream = req.headers.get('accept')?.includes('text/event-stream');
if (wantsStream) {
return toSSE(instance.stream(messages));
}
return toJSON(await instance.generate(messages));
} catch (e) {
return toError(e.message, 400);
}
}
return new Response('Not found', { status: 404 });
}
});

Re-exports TurnJSON