(config: MockAdapterConfig)
| 52 | * Create a mock text adapter for testing |
| 53 | */ |
| 54 | export function createMockTextAdapter(config: MockAdapterConfig) { |
| 55 | let responseIndex = 0 |
| 56 | |
| 57 | const adapter = { |
| 58 | kind: 'text' as const, |
| 59 | name: 'mock', |
| 60 | model: 'mock-model', |
| 61 | |
| 62 | // Type-only property for compatibility |
| 63 | '~types': {} as { |
| 64 | providerOptions: Record<string, unknown> |
| 65 | inputModalities: readonly ['text'] |
| 66 | messageMetadataByModality: DefaultMessageMetadataByModality |
| 67 | }, |
| 68 | |
| 69 | async *chatStream(): AsyncIterable<StreamChunk> { |
| 70 | const response = config.responses[responseIndex % config.responses.length] |
| 71 | if (!response) { |
| 72 | throw new Error('No responses configured for mock adapter') |
| 73 | } |
| 74 | |
| 75 | config.onResponse?.(responseIndex, response) |
| 76 | responseIndex++ |
| 77 | |
| 78 | const timestamp = Date.now() |
| 79 | const id = `mock-${timestamp}` |
| 80 | |
| 81 | // Yield tool calls first if present |
| 82 | if (response.toolCalls && response.toolCalls.length > 0) { |
| 83 | for (let i = 0; i < response.toolCalls.length; i++) { |
| 84 | const toolCall = response.toolCalls[i]! |
| 85 | const toolCallChunk: ToolCallStreamChunk = { |
| 86 | type: 'tool_call', |
| 87 | id, |
| 88 | model: 'mock-model', |
| 89 | timestamp, |
| 90 | index: i, |
| 91 | toolCall: { |
| 92 | id: toolCall.id, |
| 93 | type: 'function', |
| 94 | function: { |
| 95 | name: toolCall.name, |
| 96 | arguments: JSON.stringify(toolCall.arguments), |
| 97 | }, |
| 98 | }, |
| 99 | } |
| 100 | yield toolCallChunk |
| 101 | } |
| 102 | |
| 103 | // Done with tool_calls finish reason |
| 104 | const doneChunk: DoneStreamChunk = { |
| 105 | type: 'done', |
| 106 | id, |
| 107 | model: 'mock-model', |
| 108 | timestamp, |
| 109 | finishReason: 'tool_calls', |
| 110 | usage: { |
| 111 | promptTokens: 10, |
no outgoing calls
no test coverage detected