|
| 1 | +import { OpenAiHandler } from "./openai" |
| 2 | +import type { ApiHandlerOptions } from "../../shared/api" |
| 3 | +import { DOUBAO_API_BASE_URL, doubaoDefaultModelId, doubaoModels } from "@roo-code/types" |
| 4 | +import { getModelParams } from "../transform/model-params" |
| 5 | +import { ApiStreamUsageChunk } from "../transform/stream" |
| 6 | + |
| 7 | +// Core types for Doubao API |
| 8 | +interface ChatCompletionMessageParam { |
| 9 | + role: "system" | "user" | "assistant" | "developer" |
| 10 | + content: |
| 11 | + | string |
| 12 | + | Array<{ |
| 13 | + type: "text" | "image_url" |
| 14 | + text?: string |
| 15 | + image_url?: { url: string } |
| 16 | + }> |
| 17 | +} |
| 18 | + |
| 19 | +interface ChatCompletionParams { |
| 20 | + model: string |
| 21 | + messages: ChatCompletionMessageParam[] |
| 22 | + temperature?: number |
| 23 | + stream?: boolean |
| 24 | + stream_options?: { include_usage: boolean } |
| 25 | + max_completion_tokens?: number |
| 26 | +} |
| 27 | + |
| 28 | +interface ChatCompletion { |
| 29 | + choices: Array<{ |
| 30 | + message: { |
| 31 | + content: string |
| 32 | + } |
| 33 | + }> |
| 34 | + usage?: { |
| 35 | + prompt_tokens: number |
| 36 | + completion_tokens: number |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +interface ChatCompletionChunk { |
| 41 | + choices: Array<{ |
| 42 | + delta: { |
| 43 | + content?: string |
| 44 | + } |
| 45 | + }> |
| 46 | + usage?: { |
| 47 | + prompt_tokens: number |
| 48 | + completion_tokens: number |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export class DoubaoHandler extends OpenAiHandler { |
| 53 | + constructor(options: ApiHandlerOptions) { |
| 54 | + super({ |
| 55 | + ...options, |
| 56 | + openAiApiKey: options.doubaoApiKey ?? "not-provided", |
| 57 | + openAiModelId: options.apiModelId ?? doubaoDefaultModelId, |
| 58 | + openAiBaseUrl: options.doubaoBaseUrl ?? DOUBAO_API_BASE_URL, |
| 59 | + openAiStreamingEnabled: true, |
| 60 | + includeMaxTokens: true, |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + override getModel() { |
| 65 | + const id = this.options.apiModelId ?? doubaoDefaultModelId |
| 66 | + const info = doubaoModels[id as keyof typeof doubaoModels] || doubaoModels[doubaoDefaultModelId] |
| 67 | + const params = getModelParams({ format: "openai", modelId: id, model: info, settings: this.options }) |
| 68 | + return { id, info, ...params } |
| 69 | + } |
| 70 | + |
| 71 | + // Override to handle Doubao's usage metrics, including caching. |
| 72 | + protected override processUsageMetrics(usage: any): ApiStreamUsageChunk { |
| 73 | + return { |
| 74 | + type: "usage", |
| 75 | + inputTokens: usage?.prompt_tokens || 0, |
| 76 | + outputTokens: usage?.completion_tokens || 0, |
| 77 | + cacheWriteTokens: usage?.prompt_tokens_details?.cache_miss_tokens, |
| 78 | + cacheReadTokens: usage?.prompt_tokens_details?.cached_tokens, |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments