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
Example
Section titled “Example”// Simple echo backendconst 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 streamingconst 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: { ... } }; },});Type Parameters
Section titled “Type Parameters”TRequest
Section titled “TRequest”TRequest = IRChatRequest
TResponse
Section titled “TResponse”TResponse = IRChatResponse
Implements
Section titled “Implements”BackendAdapter<TRequest,TResponse>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new FunctionBackendAdapter<
TRequest,TResponse>(config):FunctionBackendAdapter<TRequest,TResponse>
Defined in: backend-browser/src/function.ts:187
Create a new function-based backend adapter.
Parameters
Section titled “Parameters”config
Section titled “config”FunctionBackendConfig<TRequest, TResponse>
Configuration with execute function and optional customizations
Returns
Section titled “Returns”FunctionBackendAdapter<TRequest, TResponse>
Properties
Section titled “Properties”metadata
Section titled “metadata”
readonlymetadata:AdapterMetadata
Defined in: backend-browser/src/function.ts:172
Adapter metadata for identification and capabilities.
Implementation of
Section titled “Implementation of”Methods
Section titled “Methods”estimateCost()
Section titled “estimateCost()”estimateCost(
request):Promise<number|null>
Defined in: backend-browser/src/function.ts:322
Estimate cost for a request.
Parameters
Section titled “Parameters”request
Section titled “request”Returns
Section titled “Returns”Promise<number | null>
Implementation of
Section titled “Implementation of”execute()
Section titled “execute()”execute(
request,signal?):Promise<IRChatResponse>
Defined in: backend-browser/src/function.ts:270
Execute non-streaming chat completion request.
Parameters
Section titled “Parameters”request
Section titled “request”signal?
Section titled “signal?”AbortSignal
Returns
Section titled “Returns”Promise<IRChatResponse>
Implementation of
Section titled “Implementation of”executeStream()
Section titled “executeStream()”executeStream(
request,signal?):IRChatStream
Defined in: backend-browser/src/function.ts:295
Execute streaming chat completion request.
Parameters
Section titled “Parameters”request
Section titled “request”signal?
Section titled “signal?”AbortSignal
Returns
Section titled “Returns”Implementation of
Section titled “Implementation of”fromIR()
Section titled “fromIR()”fromIR(
request):TRequest
Defined in: backend-browser/src/function.ts:243
Convert IR request to provider-specific format. Default implementation returns the request unchanged.
Parameters
Section titled “Parameters”request
Section titled “request”Returns
Section titled “Returns”TRequest
Implementation of
Section titled “Implementation of”healthCheck()
Section titled “healthCheck()”healthCheck():
Promise<boolean>
Defined in: backend-browser/src/function.ts:315
Health check to verify backend is available.
Returns
Section titled “Returns”Promise<boolean>
Implementation of
Section titled “Implementation of”listModels()
Section titled “listModels()”listModels(
options?):Promise<ListModelsResult>
Defined in: backend-browser/src/function.ts:329
List available models.
Parameters
Section titled “Parameters”options?
Section titled “options?”Returns
Section titled “Returns”Promise<ListModelsResult>
Implementation of
Section titled “Implementation of”toIR()
Section titled “toIR()”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.
Parameters
Section titled “Parameters”response
Section titled “response”TResponse
originalRequest
Section titled “originalRequest”latencyMs
Section titled “latencyMs”number