Skip to content

Variable: server

@providerprotocol/ai


@providerprotocol/ai / proxy / server

const server: object

Defined in: src/providers/proxy/server/index.ts:96

Server adapters namespace.

Contains framework-specific adapters for Web API, Express, Fastify, and H3.

express: object

Express/Connect adapter

sendEmbeddingJSON: (result, res) => void

Send an EmbeddingResult as JSON response.

EmbeddingResult

The embedding result

ExpressResponse

Express response object

void

sendError: (message, status, res) => void

Send an error response.

string

Error message

number

HTTP status code

ExpressResponse

Express response object

void

sendImageJSON: (result, res) => void

Send an ImageResult as JSON response.

ImageResult

The image result

ExpressResponse

Express response object

void

sendJSON: (turn, res) => void

Send a Turn as JSON response.

Turn

The completed inference turn

ExpressResponse

Express response object

void

const turn = await instance.generate(messages);
expressAdapter.sendJSON(turn, res);

streamImageSSE: (stream, res) => void

Stream an ImageStreamResult as Server-Sent Events.

ImageStreamLike

The ImageStreamResult or ImageProviderStreamResult from image().stream()

ExpressResponse

Express response object

void

streamSSE: (stream, res) => void

Stream a StreamResult as Server-Sent Events.

StreamResult

The StreamResult from instance.stream()

ExpressResponse

Express response object

void

const stream = instance.stream(messages);
expressAdapter.streamSSE(stream, res);

fastify: object

Fastify adapter

sendEmbeddingJSON: (result, reply) => FastifyReply

Send an EmbeddingResult as JSON response.

EmbeddingResult

The embedding result

FastifyReply

Fastify reply object

FastifyReply

sendError: (message, status, reply) => FastifyReply

Send an error response.

string

Error message

number

HTTP status code

FastifyReply

Fastify reply object

FastifyReply

sendImageJSON: (result, reply) => FastifyReply

Send an ImageResult as JSON response.

ImageResult

The image result

FastifyReply

Fastify reply object

FastifyReply

sendJSON: (turn, reply) => FastifyReply

Send a Turn as JSON response.

Turn

The completed inference turn

FastifyReply

Fastify reply object

FastifyReply

const turn = await instance.generate(messages);
return fastifyAdapter.sendJSON(turn, reply);

streamImageSSE: (stream, reply) => FastifyReply

Stream an ImageStreamResult as Server-Sent Events.

ImageStreamLike

The ImageStreamResult or ImageProviderStreamResult from image().stream()

FastifyReply

Fastify reply object

FastifyReply

streamSSE: (stream, reply) => FastifyReply

Stream a StreamResult as Server-Sent Events.

StreamResult

The StreamResult from instance.stream()

FastifyReply

Fastify reply object

FastifyReply

const stream = instance.stream(messages);
return fastifyAdapter.streamSSE(stream, reply);

h3: object

H3/Nitro/Nuxt adapter

createImageSSEStream: (stream) => ReadableStream<Uint8Array<ArrayBufferLike>>

Create a ReadableStream for image SSE data.

ImageStreamLike

The ImageStreamResult or ImageProviderStreamResult from image().stream()

ReadableStream<Uint8Array<ArrayBufferLike>>

A ReadableStream of SSE data

createSSEStream: (stream) => ReadableStream<Uint8Array<ArrayBufferLike>>

Create a ReadableStream for H3’s sendStream utility.

Use this with H3’s sendStream for better integration:

import { sendStream } from 'h3';
return sendStream(event, h3Adapter.createSSEStream(stream));

StreamResult

The StreamResult from instance.stream()

ReadableStream<Uint8Array<ArrayBufferLike>>

A ReadableStream of SSE data

sendEmbeddingJSON: (result, event) => unknown

Send an EmbeddingResult as JSON response.

EmbeddingResult

The embedding result

H3Event

H3 event object

unknown

Serialized result data

sendError: (message, status, event) => object

Send an error response with proper HTTP status.

string

Error message

number

HTTP status code

H3Event

H3 event object

object

Error object for H3 to serialize

error: string

statusCode: number

sendImageJSON: (result, event) => unknown

Send an ImageResult as JSON response.

ImageResult

The image result

H3Event

H3 event object

unknown

Serialized image result data

sendJSON: (turn, event) => unknown

Send a Turn as JSON response.

Turn

The completed inference turn

H3Event

H3 event object

unknown

Serialized turn data

const turn = await instance.generate(messages);
return h3Adapter.sendJSON(turn, event);

streamImageSSE: (stream, event) => void

Stream an ImageStreamResult as Server-Sent Events.

ImageStreamLike

The ImageStreamResult or ImageProviderStreamResult from image().stream()

H3Event

H3 event object

void

streamSSE: (stream, event) => void

Stream a StreamResult as Server-Sent Events.

Note: For better H3/Nuxt integration, prefer using createSSEStream with sendStream:

import { sendStream } from 'h3';
return sendStream(event, h3Adapter.createSSEStream(stream));

StreamResult

The StreamResult from instance.stream()

H3Event

H3 event object

void

webapi: object

Web API adapter (Bun, Deno, Next.js, Workers)

bindTools: (schemas, implementations) => Tool<unknown, unknown>[]

Bind tool schemas to implementation functions.

Takes tool schemas from the request and binds them to your server-side implementations.

Tool schemas from the request

object[] | undefined

Record<string, (params) => unknown | Promise<unknown>>

Map of tool name to implementation

Tool<unknown, unknown>[]

Array of complete Tool objects

const { tools: schemas } = parseBody(body);
const tools = bindTools(schemas, {
get_weather: async ({ location }) => fetchWeather(location),
search: async ({ query }) => searchDB(query),
});
const instance = llm({ model, tools });

parseBody: (body) => ParsedRequest

Parse an HTTP request body into PP types.

unknown

The JSON-parsed request body

ParsedRequest

Deserialized PP data

const body = await req.json();
const { messages, system, params } = parseBody(body);
const instance = llm({ model: anthropic('...'), system, params });
const turn = await instance.generate(messages);

parseEmbeddingBody: (body) => ParsedEmbeddingRequest

Parse an HTTP request body into embedding inputs.

unknown

The JSON-parsed request body

ParsedEmbeddingRequest

Parsed embedding request data

parseImageBody: (body) => ParsedImageRequest

Parse an HTTP request body into image request data.

unknown

The JSON-parsed request body

ParsedImageRequest

Parsed image request data

toEmbeddingJSON: (result) => Response

Create a JSON Response from an embedding result.

EmbeddingResult

The embedding result

Response

HTTP Response with JSON body

toError: (message, status) => Response

Create an error Response.

string

Error message

number = 500

HTTP status code (default: 500)

Response

HTTP Response with error body

toImageJSON: (result) => Response

Create a JSON Response from an image result.

ImageResult

The image result

Response

HTTP Response with JSON body

toImageSSE: (stream) => Response

Create an SSE Response from an ImageStreamResult.

Streams image events as SSE, then sends the final image result.

ImageStreamLike

The ImageStreamResult or ImageProviderStreamResult from image().stream()

Response

HTTP Response with SSE body

toJSON: (turn) => Response

Create a JSON Response from a Turn.

Turn

The completed inference turn

Response

HTTP Response with JSON body

const turn = await instance.generate(messages);
return toJSON(turn);

toSSE: (stream) => Response

Create an SSE Response from a StreamResult.

Streams PP StreamEvents as SSE, then sends the final Turn data.

StreamResult

The StreamResult from instance.stream()

Response

HTTP Response with SSE body

const stream = instance.stream(messages);
return toSSE(stream);
import { express } from '@providerprotocol/ai/proxy/server';
app.post('/api/ai', async (req, res) => {
const { messages } = parseBody(req.body);
if (req.headers.accept?.includes('text/event-stream')) {
express.streamSSE(instance.stream(messages), res);
} else {
express.sendJSON(await instance.generate(messages), res);
}
});
import { fastify } from '@providerprotocol/ai/proxy/server';
app.post('/api/ai', async (request, reply) => {
const { messages } = parseBody(request.body);
if (request.headers.accept?.includes('text/event-stream')) {
return fastify.streamSSE(instance.stream(messages), reply);
}
return fastify.sendJSON(await instance.generate(messages), reply);
});
import { h3 } from '@providerprotocol/ai/proxy/server';
export default defineEventHandler(async (event) => {
const { messages } = parseBody(await readBody(event));
if (getHeader(event, 'accept')?.includes('text/event-stream')) {
return h3.streamSSE(instance.stream(messages), event);
}
return h3.sendJSON(await instance.generate(messages), event);
});