|
| 1 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 2 | +import OpenAI from "openai" |
| 3 | + |
| 4 | +import { type ModelInfo, openAiModelInfoSaneDefaults, LMSTUDIO_DEFAULT_TEMPERATURE } from "@roo-code/types" |
| 5 | + |
| 6 | +import type { ApiHandlerOptions } from "../../shared/api" |
| 7 | + |
| 8 | +import { NativeToolCallParser } from "../../core/assistant-message/NativeToolCallParser" |
| 9 | +import { TagMatcher } from "../../utils/tag-matcher" |
| 10 | + |
| 11 | +import { convertToOpenAiMessages } from "../transform/openai-format" |
| 12 | +import { ApiStream } from "../transform/stream" |
| 13 | + |
| 14 | +import { BaseProvider } from "./base-provider" |
| 15 | +import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index" |
| 16 | +import { getModelsFromCache } from "./fetchers/modelCache" |
| 17 | +import { getApiRequestTimeout } from "./utils/timeout-config" |
| 18 | +import { handleOpenAIError } from "./utils/openai-error-handler" |
| 19 | +import { DEFAULT_HEADERS } from "./constants" |
| 20 | + |
| 21 | +/** |
| 22 | + * Atomic Chat — local OpenAI-compatible API (default http://127.0.0.1:1337/v1). |
| 23 | + * @see https://github.com/AtomicBot-ai/Atomic-Chat |
| 24 | + */ |
| 25 | +export class AtomicChatHandler extends BaseProvider implements SingleCompletionHandler { |
| 26 | + protected options: ApiHandlerOptions |
| 27 | + private client: OpenAI |
| 28 | + private readonly providerName = "Atomic Chat" |
| 29 | + |
| 30 | + constructor(options: ApiHandlerOptions) { |
| 31 | + super() |
| 32 | + this.options = options |
| 33 | + |
| 34 | + const baseRoot = (this.options.atomicChatBaseUrl || "http://127.0.0.1:1337").replace(/\/+$/, "") |
| 35 | + const apiKey = this.options.atomicChatApiKey?.trim() || "noop" |
| 36 | + |
| 37 | + this.client = new OpenAI({ |
| 38 | + baseURL: `${baseRoot}/v1`, |
| 39 | + apiKey, |
| 40 | + timeout: getApiRequestTimeout(), |
| 41 | + defaultHeaders: { |
| 42 | + ...DEFAULT_HEADERS, |
| 43 | + }, |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + override async *createMessage( |
| 48 | + systemPrompt: string, |
| 49 | + messages: Anthropic.Messages.MessageParam[], |
| 50 | + metadata?: ApiHandlerCreateMessageMetadata, |
| 51 | + ): ApiStream { |
| 52 | + const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
| 53 | + { role: "system", content: systemPrompt }, |
| 54 | + ...convertToOpenAiMessages(messages), |
| 55 | + ] |
| 56 | + |
| 57 | + const toContentBlocks = ( |
| 58 | + blocks: Anthropic.Messages.MessageParam[] | string, |
| 59 | + ): Anthropic.Messages.ContentBlockParam[] => { |
| 60 | + if (typeof blocks === "string") { |
| 61 | + return [{ type: "text", text: blocks }] |
| 62 | + } |
| 63 | + |
| 64 | + const result: Anthropic.Messages.ContentBlockParam[] = [] |
| 65 | + for (const msg of blocks) { |
| 66 | + if (typeof msg.content === "string") { |
| 67 | + result.push({ type: "text", text: msg.content }) |
| 68 | + } else if (Array.isArray(msg.content)) { |
| 69 | + for (const part of msg.content) { |
| 70 | + if (part.type === "text") { |
| 71 | + result.push({ type: "text", text: part.text }) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return result |
| 77 | + } |
| 78 | + |
| 79 | + let inputTokens = 0 |
| 80 | + try { |
| 81 | + inputTokens = await this.countTokens([{ type: "text", text: systemPrompt }, ...toContentBlocks(messages)]) |
| 82 | + } catch (err) { |
| 83 | + console.error("[AtomicChat] Failed to count input tokens:", err) |
| 84 | + inputTokens = 0 |
| 85 | + } |
| 86 | + |
| 87 | + let assistantText = "" |
| 88 | + |
| 89 | + try { |
| 90 | + const params: OpenAI.Chat.ChatCompletionCreateParamsStreaming = { |
| 91 | + model: this.getModel().id, |
| 92 | + messages: openAiMessages, |
| 93 | + temperature: this.options.modelTemperature ?? LMSTUDIO_DEFAULT_TEMPERATURE, |
| 94 | + stream: true, |
| 95 | + tools: this.convertToolsForOpenAI(metadata?.tools), |
| 96 | + tool_choice: metadata?.tool_choice, |
| 97 | + parallel_tool_calls: metadata?.parallelToolCalls ?? true, |
| 98 | + } |
| 99 | + |
| 100 | + let results |
| 101 | + try { |
| 102 | + results = await this.client.chat.completions.create(params) |
| 103 | + } catch (error) { |
| 104 | + throw handleOpenAIError(error, this.providerName) |
| 105 | + } |
| 106 | + |
| 107 | + const matcher = new TagMatcher( |
| 108 | + "think", |
| 109 | + (chunk) => |
| 110 | + ({ |
| 111 | + type: chunk.matched ? "reasoning" : "text", |
| 112 | + text: chunk.data, |
| 113 | + }) as const, |
| 114 | + ) |
| 115 | + |
| 116 | + for await (const chunk of results) { |
| 117 | + const delta = chunk.choices[0]?.delta |
| 118 | + const finishReason = chunk.choices[0]?.finish_reason |
| 119 | + |
| 120 | + if (delta?.content) { |
| 121 | + assistantText += delta.content |
| 122 | + for (const processedChunk of matcher.update(delta.content)) { |
| 123 | + yield processedChunk |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (delta?.tool_calls) { |
| 128 | + for (const toolCall of delta.tool_calls) { |
| 129 | + yield { |
| 130 | + type: "tool_call_partial", |
| 131 | + index: toolCall.index, |
| 132 | + id: toolCall.id, |
| 133 | + name: toolCall.function?.name, |
| 134 | + arguments: toolCall.function?.arguments, |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (finishReason) { |
| 140 | + const endEvents = NativeToolCallParser.processFinishReason(finishReason) |
| 141 | + for (const event of endEvents) { |
| 142 | + yield event |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + for (const processedChunk of matcher.final()) { |
| 148 | + yield processedChunk |
| 149 | + } |
| 150 | + |
| 151 | + let outputTokens = 0 |
| 152 | + try { |
| 153 | + outputTokens = await this.countTokens([{ type: "text", text: assistantText }]) |
| 154 | + } catch (err) { |
| 155 | + console.error("[AtomicChat] Failed to count output tokens:", err) |
| 156 | + outputTokens = 0 |
| 157 | + } |
| 158 | + |
| 159 | + yield { |
| 160 | + type: "usage", |
| 161 | + inputTokens, |
| 162 | + outputTokens, |
| 163 | + } as const |
| 164 | + } catch { |
| 165 | + throw new Error( |
| 166 | + "Atomic Chat request failed. Ensure the app is running, the local API server is enabled, and the model is loaded with enough context for Roo Code.", |
| 167 | + ) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + override getModel(): { id: string; info: ModelInfo } { |
| 172 | + const models = getModelsFromCache("atomic-chat") |
| 173 | + if (models && this.options.atomicChatModelId && models[this.options.atomicChatModelId]) { |
| 174 | + return { |
| 175 | + id: this.options.atomicChatModelId, |
| 176 | + info: models[this.options.atomicChatModelId], |
| 177 | + } |
| 178 | + } |
| 179 | + return { |
| 180 | + id: this.options.atomicChatModelId || "", |
| 181 | + info: openAiModelInfoSaneDefaults, |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + async completePrompt(prompt: string): Promise<string> { |
| 186 | + try { |
| 187 | + const params: OpenAI.Chat.ChatCompletionCreateParamsNonStreaming = { |
| 188 | + model: this.getModel().id, |
| 189 | + messages: [{ role: "user", content: prompt }], |
| 190 | + temperature: this.options.modelTemperature ?? LMSTUDIO_DEFAULT_TEMPERATURE, |
| 191 | + stream: false, |
| 192 | + } |
| 193 | + |
| 194 | + let response |
| 195 | + try { |
| 196 | + response = await this.client.chat.completions.create(params) |
| 197 | + } catch (error) { |
| 198 | + throw handleOpenAIError(error, this.providerName) |
| 199 | + } |
| 200 | + return response.choices[0]?.message.content || "" |
| 201 | + } catch { |
| 202 | + throw new Error( |
| 203 | + "Atomic Chat request failed. Ensure the app is running and the local API server is reachable.", |
| 204 | + ) |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments