// A scripted ModelClient for tests: no network, no API key, no cost. // // You give it the responses it should return, in order. It records every request it receives, // so a test can check exactly what the model would have seen. import type { BetaContentBlock, BetaMessage, BetaStopReason, BetaToolUseBlock, } from '@anthropic-ai/sdk/resources/beta/messages/messages'; import type { ModelCallOptions, ModelClient, ModelRequest } from '../../src/core/model.ts'; export type ScriptStep = | { content: BetaContentBlock[]; stop_reason: BetaStopReason; /** Replaces fields of the default usage, for example `iterations` (fallbacks, lab 12). */ usage?: Partial; } | ((request: ModelRequest) => { content: BetaContentBlock[]; stop_reason: BetaStopReason }) | Error; export class FakeModel implements ModelClient { readonly model = 'fake-model'; readonly requests: ModelRequest[] = []; private readonly script: ScriptStep[]; constructor(script: ScriptStep[]) { this.script = [...script]; } async complete(request: ModelRequest, options: ModelCallOptions = {}): Promise { // Deep copy: the caller may keep appending to its arrays after the call. const snapshot = structuredClone(request); this.requests.push(snapshot); const step = this.script.shift(); if (step === undefined) throw new Error('FakeModel: no scripted response left'); if (step instanceof Error) throw step; const { content, stop_reason } = typeof step === 'function' ? step(snapshot) : step; for (const block of content) if (block.type === 'text') options.onText?.(block.text); const response = message(content, stop_reason); if (typeof step !== 'function' && step.usage) { response.usage = { ...response.usage, ...step.usage }; } return response; } get remaining(): number { return this.script.length; } } export function text(value: string): BetaContentBlock { return { type: 'text', text: value, citations: null } as BetaContentBlock; } export function toolUse(id: string, name: string, input: unknown): BetaToolUseBlock { return { type: 'tool_use', id, name, input } as BetaToolUseBlock; } export function thinking(value: string, signature = 'sig-test'): BetaContentBlock { return { type: 'thinking', thinking: value, signature } as BetaContentBlock; } export function message(content: BetaContentBlock[], stop_reason: BetaStopReason): BetaMessage { return { id: `msg_fake_${Math.random().toString(16).slice(2, 10)}`, type: 'message', role: 'assistant', model: 'fake-model', content, stop_reason, stop_sequence: null, stop_details: null, container: null, context_management: null, diagnostics: null, usage: { input_tokens: 100, output_tokens: 20, cache_read_input_tokens: 80, cache_creation_input_tokens: 0, cache_creation: null, fallback_credit: null, inference_geo: null, iterations: null, output_tokens_details: null, server_tool_use: null, service_tier: 'standard', speed: 'standard', }, } as BetaMessage; } /** Wraps a model and waits before answering; the wait ends early (with an error) on abort. */ export class SlowModel implements ModelClient { readonly model: string; readonly started: number[] = []; readonly finished: number[] = []; private readonly inner: ModelClient; private readonly delayMs: number; constructor(inner: ModelClient, delayMs: number) { this.inner = inner; this.delayMs = delayMs; this.model = inner.model; } async complete(request: ModelRequest, options: ModelCallOptions = {}): Promise { this.started.push(Date.now()); await new Promise((resolve, reject) => { const timer = setTimeout(resolve, this.delayMs); options.signal?.addEventListener('abort', () => { clearTimeout(timer); reject(new Error('aborted')); }); }); const response = await this.inner.complete(request, options); this.finished.push(Date.now()); return response; } }