( systemPrompt: string, messages: Anthropic.Messages.MessageParam[], metadata?: ApiHandlerCreateMessageMetadata, )
| 363 | } |
| 364 | |
| 365 | override async *createMessage( |
| 366 | systemPrompt: string, |
| 367 | messages: Anthropic.Messages.MessageParam[], |
| 368 | metadata?: ApiHandlerCreateMessageMetadata, |
| 369 | ): ApiStream { |
| 370 | // Ensure clean state before starting a new request |
| 371 | this.ensureCleanState() |
| 372 | const client: vscode.LanguageModelChat = await this.getClient() |
| 373 | |
| 374 | // Process messages |
| 375 | const cleanedMessages = messages.map((msg) => ({ |
| 376 | ...msg, |
| 377 | content: this.cleanMessageContent(msg.content), |
| 378 | })) |
| 379 | |
| 380 | // Convert Anthropic messages to VS Code LM messages |
| 381 | const vsCodeLmMessages: vscode.LanguageModelChatMessage[] = [ |
| 382 | vscode.LanguageModelChatMessage.Assistant(systemPrompt), |
| 383 | ...convertToVsCodeLmMessages(cleanedMessages), |
| 384 | ] |
| 385 | |
| 386 | // Initialize cancellation token for the request |
| 387 | this.currentRequestCancellation = new vscode.CancellationTokenSource() |
| 388 | |
| 389 | // Calculate input tokens before starting the stream |
| 390 | const totalInputTokens: number = await this.calculateTotalInputTokens(vsCodeLmMessages) |
| 391 | |
| 392 | // Accumulate the text and count at the end of the stream to reduce token counting overhead. |
| 393 | let accumulatedText: string = "" |
| 394 | |
| 395 | try { |
| 396 | // Create the response stream with required options |
| 397 | const requestOptions: vscode.LanguageModelChatRequestOptions = { |
| 398 | justification: `Roo Code would like to use '${client.name}' from '${client.vendor}', Click 'Allow' to proceed.`, |
| 399 | tools: convertToVsCodeLmTools(metadata?.tools ?? []), |
| 400 | } |
| 401 | |
| 402 | const response: vscode.LanguageModelChatResponse = await client.sendRequest( |
| 403 | vsCodeLmMessages, |
| 404 | requestOptions, |
| 405 | this.currentRequestCancellation.token, |
| 406 | ) |
| 407 | |
| 408 | // Consume the stream and handle both text and tool call chunks |
| 409 | for await (const chunk of response.stream) { |
| 410 | if (chunk instanceof vscode.LanguageModelTextPart) { |
| 411 | // Validate text part value |
| 412 | if (typeof chunk.value !== "string") { |
| 413 | console.warn("Roo Code <Language Model API>: Invalid text part value received:", chunk.value) |
| 414 | continue |
| 415 | } |
| 416 | |
| 417 | accumulatedText += chunk.value |
| 418 | yield { |
| 419 | type: "text", |
| 420 | text: chunk.value, |
| 421 | } |
| 422 | } else if (chunk instanceof vscode.LanguageModelToolCallPart) { |
nothing calls this directly
no test coverage detected