MockBackendAdapter
Defined in: backend-browser/src/mock.ts:216
Mock Backend Adapter for testing
Returns predefined responses without making real API calls.
Examples
Section titled “Examples”import { MockBackendAdapter } from 'ai.matey';
const backend = new MockBackendAdapter({ defaultResponse: 'This is a test response',});const backend = new MockBackendAdapter({ modelResponses: { 'gpt-4': 'GPT-4 response', 'claude-3': 'Claude response', },});const backend = new MockBackendAdapter({ responseGenerator: (request) => { const lastMessage = request.messages[request.messages.length - 1]; return `You said: ${lastMessage.content[0].text}`; },});const backend = new MockBackendAdapter({ defaultResponse: { content: '', error: new Error('Simulated API error'), },});Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MockBackendAdapter(
config):MockBackendAdapter
Defined in: backend-browser/src/mock.ts:249
Parameters
Section titled “Parameters”config
Section titled “config”MockBackendConfig = {}
Returns
Section titled “Returns”MockBackendAdapter
Properties
Section titled “Properties”allRequests
Section titled “allRequests”allRequests:
IRChatRequest[] =[]
Defined in: backend-browser/src/mock.ts:247
All requests received by this adapter (for testing).
lastRequest?
Section titled “lastRequest?”
optionallastRequest:IRChatRequest
Defined in: backend-browser/src/mock.ts:242
Last request received by this adapter (for testing).
metadata
Section titled “metadata”
readonlymetadata:AdapterMetadata
Defined in: backend-browser/src/mock.ts:217
Adapter metadata for identification and capabilities.
Implementation of
Section titled “Implementation of”Methods
Section titled “Methods”clearHistory()
Section titled “clearHistory()”clearHistory():
void
Defined in: backend-browser/src/mock.ts:645
Clear request history (for testing). Resets both lastRequest and allRequests.
Returns
Section titled “Returns”void
execute()
Section titled “execute()”execute(
irRequest):Promise<IRChatResponse>
Defined in: backend-browser/src/mock.ts:306
Execute a chat request with predefined mock response.
This method processes an IR chat request and returns a mock response without making any real API calls. It simulates delays, errors, and usage statistics based on configuration. Useful for testing, development, and demonstrations where you need predictable responses without external dependencies.
Parameters
Section titled “Parameters”irRequest
Section titled “irRequest”Universal IR chat request
Returns
Section titled “Returns”Promise<IRChatResponse>
Promise resolving to mock IR chat response
Throws
Section titled “Throws”If mock response is configured to throw an error
Example
Section titled “Example”const adapter = new MockBackendAdapter({ responses: [{ content: 'This is a mock response!', delay: 100 // Simulate 100ms network delay }]});const response = await adapter.execute(request);console.log(response.message.content);Implementation of
Section titled “Implementation of”executeStream()
Section titled “executeStream()”executeStream(
irRequest):IRChatStream
Defined in: backend-browser/src/mock.ts:390
Execute a streaming chat request with predefined mock response.
This async generator processes an IR chat request and yields mock stream chunks without making any real API calls. It simulates realistic streaming behavior by splitting responses into chunks with configurable delays between them. Supports error simulation and usage statistics. Perfect for testing streaming UI components without external dependencies.
Parameters
Section titled “Parameters”irRequest
Section titled “irRequest”Universal IR chat request
Returns
Section titled “Returns”Yields
Section titled “Yields”IR stream chunks including start, content, done, and error events
Example
Section titled “Example”const adapter = new MockBackendAdapter({ responses: [{ content: 'Streaming mock response!', streamDelayMs: 50 // 50ms between chunks }]});for await (const chunk of adapter.executeStream(request)) { if (chunk.type === 'content') { console.log('Chunk:', chunk.delta); }}Implementation of
Section titled “Implementation of”fromIR()
Section titled “fromIR()”fromIR(
request):IRChatRequest
Defined in: backend-browser/src/mock.ts:265
Convert IR request to mock format (passthrough since mock uses IR).
Parameters
Section titled “Parameters”request
Section titled “request”Returns
Section titled “Returns”Implementation of
Section titled “Implementation of”listModels()
Section titled “listModels()”listModels(
options?):Promise<ListModelsResult>
Defined in: backend-browser/src/mock.ts:488
List available mock models.
Returns configured models or default mock 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/mock.ts:273
Convert mock response to IR format (passthrough since mock uses IR).
Parameters
Section titled “Parameters”response
Section titled “response”_originalRequest
Section titled “_originalRequest”_latencyMs
Section titled “_latencyMs”number