|
| 1 | +import { |
| 2 | + LlmRunner, |
| 3 | + LocalLlmConstrainedOutputGenerateRequestOptions, |
| 4 | + LocalLlmConstrainedOutputGenerateResponse, |
| 5 | + LocalLlmGenerateFilesRequestOptions, |
| 6 | + LocalLlmGenerateFilesResponse, |
| 7 | + LocalLlmGenerateTextRequestOptions, |
| 8 | + LocalLlmGenerateTextResponse, |
| 9 | + PromptDataMessage, |
| 10 | +} from './llm-runner.js'; |
| 11 | +import { |
| 12 | + FilePart, |
| 13 | + generateObject, |
| 14 | + generateText, |
| 15 | + LanguageModel, |
| 16 | + ModelMessage, |
| 17 | + SystemModelMessage, |
| 18 | + TextPart, |
| 19 | +} from 'ai'; |
| 20 | +import {anthropic, AnthropicProviderOptions} from '@ai-sdk/anthropic'; |
| 21 | +import z from 'zod'; |
| 22 | +import {callWithTimeout} from '../utils/timeout.js'; |
| 23 | +import {combineAbortSignals} from '../utils/abort-signal.js'; |
| 24 | + |
| 25 | +const SUPPORTED_MODELS = [ |
| 26 | + 'claude-opus-4.1-no-thinking', |
| 27 | + 'claude-opus-4.1-with-thinking', |
| 28 | + 'claude-sonnet-4.5-no-thinking', |
| 29 | + 'claude-sonnet-4.5-with-thinking', |
| 30 | +] as const; |
| 31 | + |
| 32 | +// Increased to a very high value as we rely on an actual timeout |
| 33 | +// that aborts stuck LLM requests. WCS is targeting stability here; |
| 34 | +// even if it involves many exponential backoff-waiting. |
| 35 | +const DEFAULT_MAX_RETRIES = 100000; |
| 36 | + |
| 37 | +export class AiSDKRunner implements LlmRunner { |
| 38 | + displayName = 'AI SDK'; |
| 39 | + id = 'ai-sdk'; |
| 40 | + hasBuiltInRepairLoop = true; |
| 41 | + |
| 42 | + async generateText( |
| 43 | + options: LocalLlmGenerateTextRequestOptions, |
| 44 | + ): Promise<LocalLlmGenerateTextResponse> { |
| 45 | + const response = await this._wrapRequestWithTimeoutAndRateLimiting(options, async abortSignal => |
| 46 | + generateText({ |
| 47 | + ...(await this._getAiSdkModelOptions(options)), |
| 48 | + abortSignal: abortSignal, |
| 49 | + messages: this._convertRequestToMessagesList(options), |
| 50 | + maxRetries: DEFAULT_MAX_RETRIES, |
| 51 | + }), |
| 52 | + ); |
| 53 | + |
| 54 | + return { |
| 55 | + reasoning: response.reasoningText ?? '', |
| 56 | + text: response.text, |
| 57 | + usage: response.usage, |
| 58 | + // TODO: Consider supporting `toolLogs` and MCP here. |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + async generateConstrained<T extends z.ZodTypeAny = z.ZodTypeAny>( |
| 63 | + options: LocalLlmConstrainedOutputGenerateRequestOptions<T>, |
| 64 | + ): Promise<LocalLlmConstrainedOutputGenerateResponse<T>> { |
| 65 | + const response = await this._wrapRequestWithTimeoutAndRateLimiting(options, async abortSignal => |
| 66 | + generateObject({ |
| 67 | + ...(await this._getAiSdkModelOptions(options)), |
| 68 | + messages: this._convertRequestToMessagesList(options), |
| 69 | + schema: options.schema, |
| 70 | + abortSignal: abortSignal, |
| 71 | + maxRetries: DEFAULT_MAX_RETRIES, |
| 72 | + }), |
| 73 | + ); |
| 74 | + |
| 75 | + return { |
| 76 | + reasoning: response.reasoning ?? '', |
| 77 | + output: response.object, |
| 78 | + usage: response.usage, |
| 79 | + // TODO: Consider supporting `toolLogs` and MCP here. |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + async generateFiles( |
| 84 | + options: LocalLlmGenerateFilesRequestOptions, |
| 85 | + ): Promise<LocalLlmGenerateFilesResponse> { |
| 86 | + const response = await this.generateConstrained({ |
| 87 | + ...options, |
| 88 | + prompt: options.context.executablePrompt, |
| 89 | + systemPrompt: options.context.systemInstructions, |
| 90 | + schema: z.object({ |
| 91 | + outputFiles: z.array( |
| 92 | + z.object({ |
| 93 | + filePath: z.string().describe('Name of the file that is being changed'), |
| 94 | + code: z.string().describe('New code of the file'), |
| 95 | + }), |
| 96 | + ), |
| 97 | + }), |
| 98 | + }); |
| 99 | + |
| 100 | + return { |
| 101 | + files: response.output?.outputFiles ?? [], |
| 102 | + reasoning: response.reasoning, |
| 103 | + usage: response.usage, |
| 104 | + // TODO: Consider supporting `toolLogs` and MCP here. |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + getSupportedModels(): string[] { |
| 109 | + return [...SUPPORTED_MODELS]; |
| 110 | + } |
| 111 | + |
| 112 | + async dispose(): Promise<void> {} |
| 113 | + |
| 114 | + private async _wrapRequestWithTimeoutAndRateLimiting<T>( |
| 115 | + request: LocalLlmGenerateTextRequestOptions | LocalLlmConstrainedOutputGenerateRequestOptions, |
| 116 | + fn: (abortSignal: AbortSignal) => Promise<T>, |
| 117 | + ): Promise<T> { |
| 118 | + // TODO: Check if rate-limiting is actually necessary here. AI SDK |
| 119 | + // seems to do retrying on its own. |
| 120 | + |
| 121 | + if (request.timeout === undefined) { |
| 122 | + return await fn(request.abortSignal); |
| 123 | + } |
| 124 | + return callWithTimeout( |
| 125 | + request.timeout.description, |
| 126 | + abortSignal => fn(combineAbortSignals(abortSignal, request.abortSignal)), |
| 127 | + request.timeout.durationInMins, |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + private async _getAiSdkModelOptions( |
| 132 | + request: LocalLlmGenerateTextRequestOptions, |
| 133 | + ): Promise<{model: LanguageModel; providerOptions: {}}> { |
| 134 | + switch (request.model) { |
| 135 | + case 'claude-opus-4.1-no-thinking': |
| 136 | + case 'claude-opus-4.1-with-thinking': { |
| 137 | + const thinkingEnabled = request.model.endsWith('with-thinking'); |
| 138 | + return { |
| 139 | + model: anthropic('claude-opus-4-1'), |
| 140 | + providerOptions: { |
| 141 | + sendReasoning: thinkingEnabled, |
| 142 | + thinking: {type: thinkingEnabled ? 'enabled' : 'disabled'}, |
| 143 | + } satisfies AnthropicProviderOptions, |
| 144 | + }; |
| 145 | + } |
| 146 | + case 'claude-sonnet-4.5-no-thinking': |
| 147 | + case 'claude-sonnet-4.5-with-thinking': { |
| 148 | + const thinkingEnabled = request.model.endsWith('with-thinking'); |
| 149 | + return { |
| 150 | + model: anthropic('claude-sonnet-4-5'), |
| 151 | + providerOptions: { |
| 152 | + sendReasoning: true, |
| 153 | + thinking: {type: 'enabled'}, |
| 154 | + } satisfies AnthropicProviderOptions, |
| 155 | + }; |
| 156 | + } |
| 157 | + default: |
| 158 | + throw new Error(`Unexpected model in AI SDK runner: ${request.model}.`); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private _convertRequestToMessagesList( |
| 163 | + request: LocalLlmConstrainedOutputGenerateRequestOptions | LocalLlmGenerateTextRequestOptions, |
| 164 | + ): ModelMessage[] { |
| 165 | + return [ |
| 166 | + // System prompt message. |
| 167 | + ...(request.systemPrompt !== undefined |
| 168 | + ? [ |
| 169 | + { |
| 170 | + role: 'system', |
| 171 | + content: request.systemPrompt, |
| 172 | + } satisfies SystemModelMessage, |
| 173 | + ] |
| 174 | + : []), |
| 175 | + // Optional additional messages |
| 176 | + ...this._toAiSDKMessage(request.messages ?? []), |
| 177 | + // The main message. |
| 178 | + {role: 'user', content: [{type: 'text', text: request.prompt}]}, |
| 179 | + ]; |
| 180 | + } |
| 181 | + |
| 182 | + private _toAiSDKMessage(messages: PromptDataMessage[]): ModelMessage[] { |
| 183 | + const result: ModelMessage[] = []; |
| 184 | + |
| 185 | + for (const message of messages) { |
| 186 | + if (message.role === 'model') { |
| 187 | + result.push({ |
| 188 | + role: 'assistant', |
| 189 | + content: message.content.map(c => |
| 190 | + 'media' in c |
| 191 | + ? ({type: 'file', data: c.media.url, mediaType: 'image/png'} satisfies FilePart) |
| 192 | + : ({type: 'text', text: c.text} satisfies TextPart), |
| 193 | + ), |
| 194 | + }); |
| 195 | + } else if (message.role === 'user') { |
| 196 | + result.push({ |
| 197 | + role: 'user', |
| 198 | + content: message.content.map(c => |
| 199 | + 'media' in c |
| 200 | + ? ({type: 'file', data: c.media.url, mediaType: 'image/png'} satisfies FilePart) |
| 201 | + : ({type: 'text', text: c.text} satisfies TextPart), |
| 202 | + ), |
| 203 | + }); |
| 204 | + } |
| 205 | + } |
| 206 | + return result; |
| 207 | + } |
| 208 | +} |
0 commit comments