also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,72 @@ | ||
| 1 | import type { AIProvider, ChatOptions, ChatResponse, EmbeddingResponse, Message, StreamChunk } from '../types' | |
| 2 | ||
| 3 | export class MockProvider implements AIProvider { | |
| 4 | name = 'mock' | |
| 5 | responses: string[] | |
| 6 | calls: Array<{ messages: Message[], options?: ChatOptions }> = [] | |
| 7 | private index = 0 | |
| 8 | ||
| 9 | constructor(responses?: string[]) { | |
| 10 | this.responses = responses ?? ['Mock response'] | |
| 11 | } | |
| 12 | ||
| 13 | async chat(messages: Message[], options?: ChatOptions): Promise<ChatResponse> { | |
| 14 | this.calls.push({ messages, options }) | |
| 15 | const content = this.responses[this.index % this.responses.length] | |
| 16 | this.index++ | |
| 17 | ||
| 18 | return { | |
| 19 | content, | |
| 20 | model: options?.model ?? 'mock-model', | |
| 21 | usage: { | |
| 22 | inputTokens: messages.reduce((sum, m) => sum + m.content.length, 0), | |
| 23 | outputTokens: content.length, | |
| 24 | totalTokens: messages.reduce((sum, m) => sum + m.content.length, 0) + content.length, | |
| 25 | }, | |
| 26 | finishReason: 'stop', | |
| 27 | } | |
| 28 | } | |
| 29 | ||
| 30 | async *stream(messages: Message[], options?: ChatOptions): AsyncIterable<StreamChunk> { | |
| 31 | this.calls.push({ messages, options }) | |
| 32 | const content = this.responses[this.index % this.responses.length] | |
| 33 | this.index++ | |
| 34 | ||
| 35 | for (let i = 0; i < content.length; i++) { | |
| 36 | const isLast = i === content.length - 1 | |
| 37 | yield { | |
| 38 | content: content[i], | |
| 39 | done: isLast, | |
| 40 | finishReason: isLast ? 'stop' : undefined, | |
| 41 | } | |
| 42 | } | |
| 43 | } | |
| 44 | ||
| 45 | async embed(text: string, _model?: string): Promise<EmbeddingResponse> { | |
| 46 | const embedding: number[] = [] | |
| 47 | for (let i = 0; i < 128; i++) { | |
| 48 | // Deterministic pseudo-random based on text and index | |
| 49 | embedding.push(Math.sin(i * 0.1 + text.length * 0.3) * 0.5) | |
| 50 | } | |
| 51 | ||
| 52 | return { | |
| 53 | embedding, | |
| 54 | model: _model ?? 'mock-embedding', | |
| 55 | usage: { totalTokens: text.split(/\s+/).length }, | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 59 | addResponse(content: string): void { | |
| 60 | this.responses.push(content) | |
| 61 | } | |
| 62 | ||
| 63 | getLastCall(): { messages: Message[], options?: ChatOptions } | undefined { | |
| 64 | return this.calls[this.calls.length - 1] | |
| 65 | } | |
| 66 | ||
| 67 | reset(): void { | |
| 68 | this.calls = [] | |
| 69 | this.index = 0 | |
| 70 | this.responses = ['Mock response'] | |
| 71 | } | |
| 72 | } | |