|
1 | | -import { Anthropic } from "@anthropic-ai/sdk" |
2 | | -import OpenAI from "openai" |
3 | | - |
4 | | -import { ApiHandlerOptions, ModelInfo, requestyModelInfoSaneDefaults } from "../../shared/api" |
5 | | -import { ApiHandler, SingleCompletionHandler } from "../index" |
6 | | -import { convertToOpenAiMessages } from "../transform/openai-format" |
7 | | -import { convertToR1Format } from "../transform/r1-format" |
8 | | -import { ApiStream } from "../transform/stream" |
9 | | - |
10 | | -export class RequestyHandler implements ApiHandler, SingleCompletionHandler { |
11 | | - protected options: ApiHandlerOptions |
12 | | - private client: OpenAI |
13 | | - |
14 | | - constructor(options: ApiHandlerOptions) { |
15 | | - this.options = options |
16 | | - this.client = new OpenAI({ |
17 | | - baseURL: "https://router.requesty.ai/v1", |
18 | | - apiKey: this.options.requestyApiKey, |
| 1 | +import { OpenAiHandler, OpenAiHandlerOptions } from "./openai" |
| 2 | +import { ModelInfo, requestyModelInfoSaneDefaults, requestyDefaultModelId } from "../../shared/api" |
| 3 | +import { ApiStream, ApiStreamUsageChunk } from "../transform/stream" |
| 4 | + |
| 5 | +export class RequestyHandler extends OpenAiHandler { |
| 6 | + constructor(options: OpenAiHandlerOptions) { |
| 7 | + if (!options.requestyApiKey) { |
| 8 | + throw new Error("Requesty API key is required. Please provide it in the settings.") |
| 9 | + } |
| 10 | + super({ |
| 11 | + ...options, |
| 12 | + openAiApiKey: options.requestyApiKey, |
| 13 | + openAiModelId: options.requestyModelId ?? requestyDefaultModelId, |
| 14 | + openAiBaseUrl: "https://router.requesty.ai/v1", |
| 15 | + openAiCustomModelInfo: options.requestyModelInfo ?? requestyModelInfoSaneDefaults, |
19 | 16 | defaultHeaders: { |
20 | 17 | "HTTP-Referer": "https://github.com/RooVetGit/Roo-Cline", |
21 | 18 | "X-Title": "Roo Code", |
22 | 19 | }, |
23 | 20 | }) |
24 | 21 | } |
25 | 22 |
|
26 | | - async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream { |
27 | | - const modelInfo = this.getModel().info |
28 | | - const modelId = this.options.requestyModelId ?? "" |
29 | | - |
30 | | - const deepseekReasoner = modelId.includes("deepseek-reasoner") |
31 | | - |
32 | | - if (this.options.openAiStreamingEnabled ?? true) { |
33 | | - const systemMessage: OpenAI.Chat.ChatCompletionSystemMessageParam = { |
34 | | - role: "system", |
35 | | - content: systemPrompt, |
36 | | - } |
37 | | - const requestOptions: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = { |
38 | | - model: modelId, |
39 | | - temperature: 0, |
40 | | - messages: deepseekReasoner |
41 | | - ? convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) |
42 | | - : [systemMessage, ...convertToOpenAiMessages(messages)], |
43 | | - stream: true as const, |
44 | | - stream_options: { include_usage: true }, |
45 | | - } |
46 | | - if (this.options.includeMaxTokens) { |
47 | | - requestOptions.max_tokens = modelInfo.maxTokens |
48 | | - } |
49 | | - |
50 | | - const stream = await this.client.chat.completions.create(requestOptions) |
51 | | - |
52 | | - for await (const chunk of stream) { |
53 | | - const delta = chunk.choices[0]?.delta ?? {} |
54 | | - |
55 | | - if (delta.content) { |
56 | | - yield { |
57 | | - type: "text", |
58 | | - text: delta.content, |
59 | | - } |
60 | | - } |
61 | | - |
62 | | - if ("reasoning_content" in delta && delta.reasoning_content) { |
63 | | - yield { |
64 | | - type: "reasoning", |
65 | | - text: (delta.reasoning_content as string | undefined) || "", |
66 | | - } |
67 | | - } |
68 | | - if (chunk.usage) { |
69 | | - yield { |
70 | | - type: "usage", |
71 | | - inputTokens: chunk.usage.prompt_tokens || 0, |
72 | | - outputTokens: chunk.usage.completion_tokens || 0, |
73 | | - cacheWriteTokens: (chunk.usage as any).cache_creation_input_tokens || undefined, |
74 | | - cacheReadTokens: (chunk.usage as any).cache_read_input_tokens || undefined, |
75 | | - } |
76 | | - } |
77 | | - } |
78 | | - } else { |
79 | | - // o1 for instance doesnt support streaming, non-1 temp, or system prompt |
80 | | - const systemMessage: OpenAI.Chat.ChatCompletionUserMessageParam = { |
81 | | - role: "user", |
82 | | - content: systemPrompt, |
83 | | - } |
84 | | - |
85 | | - const requestOptions: OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming = { |
86 | | - model: modelId, |
87 | | - messages: deepseekReasoner |
88 | | - ? convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) |
89 | | - : [systemMessage, ...convertToOpenAiMessages(messages)], |
90 | | - } |
91 | | - |
92 | | - const response = await this.client.chat.completions.create(requestOptions) |
93 | | - |
94 | | - yield { |
95 | | - type: "text", |
96 | | - text: response.choices[0]?.message.content || "", |
97 | | - } |
98 | | - yield { |
99 | | - type: "usage", |
100 | | - inputTokens: response.usage?.prompt_tokens || 0, |
101 | | - outputTokens: response.usage?.completion_tokens || 0, |
102 | | - } |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - getModel(): { id: string; info: ModelInfo } { |
| 23 | + override getModel(): { id: string; info: ModelInfo } { |
| 24 | + const modelId = this.options.requestyModelId ?? requestyDefaultModelId |
107 | 25 | return { |
108 | | - id: this.options.requestyModelId ?? "", |
| 26 | + id: modelId, |
109 | 27 | info: this.options.requestyModelInfo ?? requestyModelInfoSaneDefaults, |
110 | 28 | } |
111 | 29 | } |
112 | 30 |
|
113 | | - async completePrompt(prompt: string): Promise<string> { |
114 | | - try { |
115 | | - const requestOptions: OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming = { |
116 | | - model: this.getModel().id, |
117 | | - messages: [{ role: "user", content: prompt }], |
118 | | - } |
119 | | - |
120 | | - const response = await this.client.chat.completions.create(requestOptions) |
121 | | - return response.choices[0]?.message.content || "" |
122 | | - } catch (error) { |
123 | | - if (error instanceof Error) { |
124 | | - throw new Error(`OpenAI completion error: ${error.message}`) |
125 | | - } |
126 | | - throw error |
| 31 | + protected override processUsageMetrics(usage: any): ApiStreamUsageChunk { |
| 32 | + return { |
| 33 | + type: "usage", |
| 34 | + inputTokens: usage?.prompt_tokens || 0, |
| 35 | + outputTokens: usage?.completion_tokens || 0, |
| 36 | + cacheWriteTokens: usage?.cache_creation_input_tokens, |
| 37 | + cacheReadTokens: usage?.cache_read_input_tokens, |
127 | 38 | } |
128 | 39 | } |
129 | 40 | } |
0 commit comments