forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpearai.ts
More file actions
152 lines (140 loc) · 5.03 KB
/
pearai.ts
File metadata and controls
152 lines (140 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import * as vscode from "vscode"
import { ApiHandlerOptions, ModelInfo } from "../../../shared/api"
import { AnthropicHandler } from "../anthropic"
import { DeepSeekHandler } from "../deepseek"
import Anthropic from "@anthropic-ai/sdk"
import { BaseProvider } from "../base-provider"
import { SingleCompletionHandler } from "../.."
import { OpenRouterHandler } from "../openrouter"
import { GeminiHandler } from "../gemini"
import { OpenAiHandler } from "../openai"
import { PearAIGenericHandler } from "./pearaiGeneric"
import { PEARAI_URL } from "../../../shared/pearaiApi"
export interface PearAIAgentModelsConfig {
models: Record<string, ModelInfo>
defaultModelId?: string
}
export class PearAIHandler extends BaseProvider implements SingleCompletionHandler {
private handler!: AnthropicHandler | PearAIGenericHandler
private pearaiAgentModels: PearAIAgentModelsConfig | null = null
private options: ApiHandlerOptions
constructor(options: ApiHandlerOptions) {
super()
if (!options.pearaiApiKey) {
vscode.commands.executeCommand("pearai-roo-cline.PearAIKeysNotFound", undefined)
vscode.window.showErrorMessage("PearAI API key not found.", "Login to PearAI").then(async (selection) => {
if (selection === "Login to PearAI") {
const extensionUrl = `${vscode.env.uriScheme}://pearai.pearai/auth`
const callbackUri = await vscode.env.asExternalUri(vscode.Uri.parse(extensionUrl))
vscode.env.openExternal(
await vscode.env.asExternalUri(
vscode.Uri.parse(`https://trypear.ai/signin?callback=${callbackUri.toString()}`),
),
)
}
})
throw new Error("PearAI API key not found. Please login to PearAI.")
} else {
vscode.commands.executeCommand("pearai.checkPearAITokens", undefined)
}
this.options = options
this.handler = new PearAIGenericHandler({
...options,
openAiBaseUrl: PEARAI_URL,
openAiApiKey: options.pearaiApiKey,
openAiModelId: "deepseek/deepseek-chat",
})
// Then try to initialize the correct handler asynchronously
this.initializeHandler(options).catch((error) => {
console.error("Failed to initialize PearAI handler:", error)
})
}
private async initializeHandler(options: ApiHandlerOptions): Promise<void> {
const modelId = options.apiModelId || "pearai-model"
if (modelId.startsWith("pearai")) {
try {
if (!options.pearaiAgentModels) {
throw new Error("PearAI models not found")
}
const pearaiAgentModels = options.pearaiAgentModels
const underlyingModel =
pearaiAgentModels.models[modelId]?.underlyingModel || "claude-3-5-sonnet-20241022"
if (underlyingModel.startsWith("claude") || modelId.startsWith("anthropic/")) {
// Default to Claude
this.handler = new AnthropicHandler({
...options,
apiKey: options.pearaiApiKey,
anthropicBaseUrl: PEARAI_URL,
apiModelId: underlyingModel,
})
} else {
// Use OpenAI fields here as we are using the same handler structure as OpenAI Hander lin PearAIGenericHandler
this.handler = new PearAIGenericHandler({
...options,
openAiBaseUrl: PEARAI_URL,
openAiApiKey: options.pearaiApiKey,
openAiModelId: underlyingModel,
})
}
} catch (error) {
console.error("Error fetching PearAI models:", error)
// Default to Claude if there's an error
this.handler = new AnthropicHandler({
...options,
apiKey: options.pearaiApiKey,
anthropicBaseUrl: PEARAI_URL,
apiModelId: "claude-3-5-sonnet-20241022",
})
}
} else if (modelId.startsWith("claude") || modelId.startsWith("anthropic/")) {
this.handler = new AnthropicHandler({
...options,
apiKey: options.pearaiApiKey,
anthropicBaseUrl: PEARAI_URL,
})
} else {
this.handler = new PearAIGenericHandler({
...options,
openAiBaseUrl: PEARAI_URL,
openAiApiKey: options.pearaiApiKey,
openAiModelId: modelId,
})
}
}
public getModel(): { id: string; info: ModelInfo } {
// Fallback to using what's available on client side
const baseModel = this.handler.getModel()
return baseModel
}
async *createMessage(systemPrompt: string, messages: any[]): AsyncGenerator<any> {
try {
const generator = this.handler.createMessage(systemPrompt, messages)
let warningMsg = ""
for await (const chunk of generator) {
console.dir(chunk)
if (chunk.type === "text" && chunk.metadata?.ui_only) {
warningMsg += chunk.metadata?.content ?? ""
continue
}
yield chunk
}
if (warningMsg) {
if (warningMsg.includes("pay-as-you-go")) {
vscode.window.showInformationMessage(warningMsg, "View Pay-As-You-Go").then((selection) => {
if (selection === "View Pay-As-You-Go") {
vscode.env.openExternal(vscode.Uri.parse("https://trypear.ai/pay-as-you-go"))
}
})
} else {
vscode.window.showInformationMessage(warningMsg)
}
}
} catch (e) {
const errorMessage = e instanceof Error ? e.message : String(e)
vscode.window.showWarningMessage(`Notice: ${errorMessage}`)
}
}
async completePrompt(prompt: string): Promise<string> {
return this.handler.completePrompt(prompt)
}
}