Skip to content

FunctionBackendAdapter

Defined in: backend-browser/src/function.ts:168

Generic function-based backend adapter.

Accepts async functions for execute and stream operations, making it easy to:

  • Create custom backends from simple functions
  • Write integration tests with controlled behavior
  • Wrap external services or APIs
  • Create mock backends with specific responses
// Simple echo backend
const echoBackend = new FunctionBackendAdapter({
execute: async (request) => ({
message: { role: 'assistant', content: `Echo: ${request.messages[0]?.content}` },
finishReason: 'stop',
metadata: { requestId: request.metadata.requestId, provenance: {} },
}),
});
// Backend with streaming
const streamingBackend = new FunctionBackendAdapter({
execute: async (request) => ({ ... }),
executeStream: async function* (request) {
yield { type: 'start', sequence: 0 };
yield { type: 'content', sequence: 1, delta: 'Hello', role: 'assistant' };
yield { type: 'done', sequence: 2, finishReason: 'stop', message: { ... } };
},
});

TRequest = IRChatRequest

TResponse = IRChatResponse

new FunctionBackendAdapter<TRequest, TResponse>(config): FunctionBackendAdapter<TRequest, TResponse>

Defined in: backend-browser/src/function.ts:187

Create a new function-based backend adapter.

FunctionBackendConfig<TRequest, TResponse>

Configuration with execute function and optional customizations

FunctionBackendAdapter<TRequest, TResponse>

readonly metadata: AdapterMetadata

Defined in: backend-browser/src/function.ts:172

Adapter metadata for identification and capabilities.

BackendAdapter.metadata

estimateCost(request): Promise<number | null>

Defined in: backend-browser/src/function.ts:322

Estimate cost for a request.

IRChatRequest

Promise<number | null>

BackendAdapter.estimateCost


execute(request, signal?): Promise<IRChatResponse>

Defined in: backend-browser/src/function.ts:270

Execute non-streaming chat completion request.

IRChatRequest

AbortSignal

Promise<IRChatResponse>

BackendAdapter.execute


executeStream(request, signal?): IRChatStream

Defined in: backend-browser/src/function.ts:295

Execute streaming chat completion request.

IRChatRequest

AbortSignal

IRChatStream

BackendAdapter.executeStream


fromIR(request): TRequest

Defined in: backend-browser/src/function.ts:243

Convert IR request to provider-specific format. Default implementation returns the request unchanged.

IRChatRequest

TRequest

BackendAdapter.fromIR


healthCheck(): Promise<boolean>

Defined in: backend-browser/src/function.ts:315

Health check to verify backend is available.

Promise<boolean>

BackendAdapter.healthCheck


listModels(options?): Promise<ListModelsResult>

Defined in: backend-browser/src/function.ts:329

List available models.

ListModelsOptions

Promise<ListModelsResult>

BackendAdapter.listModels


toIR(response, originalRequest, latencyMs): IRChatResponse

Defined in: backend-browser/src/function.ts:251

Convert provider response to IR format. Default implementation returns the response with updated metadata.

TResponse

IRChatRequest

number

IRChatResponse

BackendAdapter.toIR