| 37 | * key (env var or settings), never the MatterAI token. |
| 38 | */ |
| 39 | export class AiSdkClient implements LLMClient { |
| 40 | constructor(private readonly options: AiSdkClientOptions) {} |
| 41 | |
| 42 | async *createMessage( |
| 43 | systemPrompt: string, |
| 44 | messages: OpenAI.Chat.ChatCompletionMessageParam[], |
| 45 | tools: OpenAI.Chat.ChatCompletionTool[], |
| 46 | abortSignal?: AbortSignal, |
| 47 | ): AsyncGenerator<ApiStreamChunk> { |
| 48 | const model = this.options.model |
| 49 | const isAnthropic = model.provider === "anthropic" |
| 50 | |
| 51 | // Replay stored thinking blocks only on Anthropic, where they round-trip |
| 52 | // with their signatures; other providers ignore the side-channel. |
| 53 | const aiMessages = toModelMessages(messages, isAnthropic) |
| 54 | if (isAnthropic) applyCacheBreakpoint(aiMessages) |
| 55 | |
| 56 | const aiTools = toAiTools(tools) |
| 57 | const hasTools = Object.keys(aiTools).length > 0 |
| 58 | |
| 59 | const result = streamText({ |
| 60 | model: this.resolveModel(), |
| 61 | system: this.buildSystem(systemPrompt, isAnthropic), |
| 62 | messages: aiMessages, |
| 63 | ...(hasTools ? { tools: aiTools, toolChoice: "auto" as const } : {}), |
| 64 | maxOutputTokens: model.maxOutputTokens, |
| 65 | // No `temperature`: the current Claude models reject sampling params. |
| 66 | abortSignal, |
| 67 | providerOptions: this.providerOptions(isAnthropic), |
| 68 | }) |
| 69 | |
| 70 | for await (const part of result.fullStream) { |
| 71 | switch (part.type) { |
| 72 | case "text-delta": |
| 73 | if (part.text) yield { type: "text", text: part.text } |
| 74 | break |
| 75 | case "reasoning-delta": |
| 76 | if (part.text) yield { type: "reasoning", text: part.text } |
| 77 | break |
| 78 | case "tool-call": |
| 79 | yield { |
| 80 | type: "native_tool_calls", |
| 81 | toolCalls: [ |
| 82 | { |
| 83 | id: part.toolCallId, |
| 84 | type: "function", |
| 85 | function: { |
| 86 | name: part.toolName, |
| 87 | arguments: JSON.stringify(part.input ?? {}), |
| 88 | }, |
| 89 | }, |
| 90 | ], |
| 91 | } |
| 92 | break |
| 93 | case "finish": |
| 94 | yield this.usageChunk(part.totalUsage) |
| 95 | break |
| 96 | case "error": |
nothing calls this directly
no outgoing calls
no test coverage detected