| 1 | export function createMockModel(config = {}) { |
| 2 | const { responses = [{ text: 'Mock AI response' }], simulateError = false, errorType = 'api', delay = 0 } = config |
| 3 | |
| 4 | let callCount = 0 |
| 5 | |
| 6 | const mockModel = { |
| 7 | specificationVersion: 'v3', |
| 8 | modelId: 'mock-model', |
| 9 | provider: 'mock', |
| 10 | defaultObjectGenerationMode: 'json', |
| 11 | |
| 12 | async doGenerate(options) { |
| 13 | if (delay > 0) { |
| 14 | await new Promise(resolve => setTimeout(resolve, delay)) |
| 15 | } |
| 16 | |
| 17 | if (simulateError) { |
| 18 | throw new Error(`Mock ${errorType} error`) |
| 19 | } |
| 20 | |
| 21 | const response = responses[callCount] || responses[responses.length - 1] |
| 22 | callCount++ |
| 23 | |
| 24 | const textContent = response.text || 'Mock response' |
| 25 | |
| 26 | // Calculate token values |
| 27 | const promptTokens = response.promptTokens || 50 |
| 28 | const totalTokens = response.totalTokens || 100 |
| 29 | const completionTokens = response.completionTokens || (totalTokens - promptTokens) |
| 30 | |
| 31 | return { |
| 32 | text: textContent, |
| 33 | content: [ |
| 34 | { |
| 35 | type: 'text', |
| 36 | text: textContent, |
| 37 | }, |
| 38 | ], |
| 39 | usage: { |
| 40 | promptTokens, |
| 41 | completionTokens, |
| 42 | totalTokens, |
| 43 | inputTokens: { |
| 44 | total: promptTokens, |
| 45 | }, |
| 46 | outputTokens: { |
| 47 | total: completionTokens, |
| 48 | }, |
| 49 | }, |
| 50 | finishReason: response.finishReason || 'stop', |
| 51 | rawResponse: { |
| 52 | headers: {}, |
| 53 | }, |
| 54 | warnings: [], |
| 55 | logprobs: undefined, |
| 56 | response: { |
| 57 | id: `mock-${Date.now()}`, |
| 58 | timestamp: new Date(), |
| 59 | modelId: 'mock-model', |
| 60 | }, |