Skip to content

Commit 209d355

Browse files
authored
Merge pull request #24 from trypear/dynamic
Dynamic pearai models
2 parents aec2c72 + 92e3de4 commit 209d355

3 files changed

Lines changed: 76 additions & 63 deletions

File tree

src/core/webview/ClineProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
423423
`style-src ${webview.cspSource} 'unsafe-inline' https://* http://${localServerUrl} http://0.0.0.0:${localPort}`,
424424
`img-src ${webview.cspSource} data:`,
425425
`script-src 'unsafe-eval' https://* http://${localServerUrl} http://0.0.0.0:${localPort} 'nonce-${nonce}'`,
426-
`connect-src https://* ws://${localServerUrl} ws://0.0.0.0:${localPort} http://${localServerUrl} http://0.0.0.0:${localPort}`,
426+
`connect-src https://* ws://${localServerUrl} ws://0.0.0.0:${localPort} http://${localServerUrl} http://0.0.0.0:${localPort} http://localhost:8000 http://0.0.0.0:8000 https://stingray-app-gb2an.ondigitalocean.app`,
427427
]
428428

429429
return /*html*/ `
@@ -507,7 +507,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
507507
<meta charset="utf-8">
508508
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
509509
<meta name="theme-color" content="#000000">
510-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${webview.cspSource}; style-src ${webview.cspSource} 'unsafe-inline'; img-src ${webview.cspSource} data:; script-src 'nonce-${nonce}';">
510+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${webview.cspSource}; style-src ${webview.cspSource} 'unsafe-inline'; img-src ${webview.cspSource} data:; script-src 'nonce-${nonce}'; connect-src ${webview.cspSource} https://stingray-app-gb2an.ondigitalocean.app;">
511511
<link rel="stylesheet" type="text/css" href="${stylesUri}">
512512
<link href="${codiconsUri}" rel="stylesheet" />
513513
<title>Roo Code</title>

src/shared/api.ts

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,19 @@ export const PEARAI_URL = "https://stingray-app-gb2an.ondigitalocean.app/pearai-
799799
// PearAI
800800
export type PearAiModelId = keyof typeof pearAiModels
801801
export const pearAiDefaultModelId: PearAiModelId = "pearai-model"
802+
export const pearAiDefaultModelInfo: ModelInfo = {
803+
maxTokens: 8192,
804+
contextWindow: 64000,
805+
supportsImages: false,
806+
supportsPromptCache: true,
807+
inputPrice: 0.014,
808+
outputPrice: 0.28,
809+
cacheWritesPrice: 0.27,
810+
cacheReadsPrice: 0.07,
811+
description:
812+
"DeepSeek-V3 achieves a significant breakthrough in inference speed over previous models. It tops the leaderboard among open-source models and rivals the most advanced closed-source models globally.",
813+
}
814+
802815
export const pearAiModels = {
803816
"pearai-model": {
804817
maxTokens: 8192,
@@ -812,48 +825,4 @@ export const pearAiModels = {
812825
description:
813826
"DeepSeek-V3 achieves a significant breakthrough in inference speed over previous models. It tops the leaderboard among open-source models and rivals the most advanced closed-source models globally.",
814827
},
815-
"claude-3-5-sonnet-20241022": {
816-
maxTokens: 8192,
817-
contextWindow: 200000,
818-
supportsImages: true,
819-
supportsComputerUse: true,
820-
supportsPromptCache: true,
821-
inputPrice: 3.0,
822-
outputPrice: 15.0,
823-
cacheWritesPrice: 3.75,
824-
cacheReadsPrice: 0.3,
825-
},
826-
"claude-3-5-haiku-20241022": {
827-
maxTokens: 8192,
828-
contextWindow: 200000,
829-
supportsImages: false,
830-
supportsPromptCache: true,
831-
inputPrice: 1.0,
832-
outputPrice: 5.0,
833-
cacheWritesPrice: 1.25,
834-
cacheReadsPrice: 0.1,
835-
},
836-
"deepseek-chat": {
837-
maxTokens: 8192,
838-
contextWindow: 64000,
839-
supportsImages: false,
840-
supportsPromptCache: true,
841-
inputPrice: 0.014,
842-
outputPrice: 0.28,
843-
cacheWritesPrice: 0.27,
844-
cacheReadsPrice: 0.07,
845-
description:
846-
"DeepSeek-V3 achieves a significant breakthrough in inference speed over previous models. It tops the leaderboard among open-source models and rivals the most advanced closed-source models globally.",
847-
},
848-
"deepseek-reasoner": {
849-
maxTokens: 8192,
850-
contextWindow: 64000,
851-
supportsImages: false,
852-
supportsPromptCache: true,
853-
inputPrice: 0.55,
854-
outputPrice: 2.19,
855-
cacheWritesPrice: 0.55,
856-
cacheReadsPrice: 0.14,
857-
description: "DeepSeek-R1 achieves performance comparable to OpenAI-o1 across math, code, and reasoning tasks.",
858-
},
859828
} as const satisfies Record<string, ModelInfo>

webview-ui/src/components/settings/ApiOptions.tsx

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment, memo, useCallback, useEffect, useMemo, useState } from "react"
1+
import { Fragment, memo, useCallback, useEffect, useMemo, useState, useRef } from "react"
22
import { useEvent, useDebounce, useInterval } from "react-use"
33
import { Checkbox, Dropdown, Pane, type DropdownOption } from "vscrui"
44
import {
@@ -40,6 +40,8 @@ import {
4040
requestyDefaultModelInfo,
4141
pearAiModels,
4242
pearAiDefaultModelId,
43+
pearAiDefaultModelInfo,
44+
PEARAI_URL,
4345
} from "../../../../src/shared/api"
4446
import { ExtensionMessage } from "../../../../src/shared/ExtensionMessage"
4547

@@ -52,17 +54,6 @@ import { validateApiConfiguration, validateModelId } from "@/utils/validate"
5254
import { ApiErrorMessage } from "./ApiErrorMessage"
5355
import { ThinkingBudget } from "./ThinkingBudget"
5456

55-
const modelsByProvider: Record<string, Record<string, ModelInfo>> = {
56-
anthropic: anthropicModels,
57-
bedrock: bedrockModels,
58-
vertex: vertexModels,
59-
gemini: geminiModels,
60-
"openai-native": openAiNativeModels,
61-
deepseek: deepSeekModels,
62-
mistral: mistralModels,
63-
pearai: pearAiModels,
64-
}
65-
6657
interface ApiOptionsProps {
6758
uriScheme: string | undefined
6859
apiConfiguration: ApiConfiguration
@@ -101,6 +92,9 @@ const ApiOptions = ({
10192
})
10293

10394
const [openAiModels, setOpenAiModels] = useState<Record<string, ModelInfo> | null>(null)
95+
const [pearAiModels, setPearAiModels] = useState<Record<string, ModelInfo>>({
96+
[pearAiDefaultModelId]: pearAiDefaultModelInfo,
97+
})
10498

10599
const [anthropicBaseUrlSelected, setAnthropicBaseUrlSelected] = useState(!!apiConfiguration?.anthropicBaseUrl)
106100
const [azureApiVersionSelected, setAzureApiVersionSelected] = useState(!!apiConfiguration?.azureApiVersion)
@@ -123,10 +117,16 @@ const ApiOptions = ({
123117
[setApiConfigurationField],
124118
)
125119

126-
const { selectedProvider, selectedModelId, selectedModelInfo } = useMemo(
127-
() => normalizeApiConfiguration(apiConfiguration),
128-
[apiConfiguration],
129-
)
120+
const { selectedProvider, selectedModelId, selectedModelInfo } = useMemo(() => {
121+
const result = normalizeApiConfiguration(apiConfiguration)
122+
if (result.selectedProvider === "pearai") {
123+
return {
124+
...result,
125+
selectedModelInfo: pearAiModels[result.selectedModelId] || pearAiModels[pearAiDefaultModelId],
126+
}
127+
}
128+
return result
129+
}, [apiConfiguration, pearAiModels])
130130

131131
// Debounced refresh model updates, only executed 250ms after the user
132132
// stops typing.
@@ -167,6 +167,28 @@ const ApiOptions = ({
167167
],
168168
)
169169

170+
// Fetch PearAI models when provider is selected
171+
useEffect(() => {
172+
if (selectedProvider === "pearai") {
173+
const fetchPearAiModels = async () => {
174+
try {
175+
const res = await fetch(`${PEARAI_URL}/getPearAIAgentModels`)
176+
if (!res.ok) throw new Error("Failed to fetch models")
177+
const config = await res.json()
178+
179+
if (config.models && Object.keys(config.models).length > 0) {
180+
console.log("Models successfully loaded from server")
181+
setPearAiModels(config.models)
182+
}
183+
} catch (error) {
184+
console.error("Error fetching PearAI models:", error)
185+
}
186+
}
187+
188+
fetchPearAiModels()
189+
}
190+
}, [selectedProvider, setPearAiModels])
191+
170192
useEffect(() => {
171193
const apiValidationResult =
172194
validateApiConfiguration(apiConfiguration) ||
@@ -227,6 +249,28 @@ const ApiOptions = ({
227249

228250
useEvent("message", onMessage)
229251

252+
const modelsByProvider = useMemo(
253+
() => ({
254+
anthropic: anthropicModels,
255+
bedrock: bedrockModels,
256+
vertex: vertexModels,
257+
gemini: geminiModels,
258+
"openai-native": openAiNativeModels,
259+
deepseek: deepSeekModels,
260+
mistral: mistralModels,
261+
pearai: pearAiModels,
262+
glama: glamaModels,
263+
openrouter: openRouterModels,
264+
unbound: unboundModels,
265+
requesty: requestyModels,
266+
openai: openAiModels || {},
267+
ollama: {},
268+
lmstudio: {},
269+
"vscode-lm": {},
270+
}),
271+
[pearAiModels, glamaModels, openRouterModels, unboundModels, requestyModels, openAiModels],
272+
)
273+
230274
const selectedProviderModelOptions: DropdownOption[] = useMemo(
231275
() =>
232276
modelsByProvider[selectedProvider]
@@ -238,7 +282,7 @@ const ApiOptions = ({
238282
})),
239283
]
240284
: [],
241-
[selectedProvider],
285+
[selectedProvider, modelsByProvider],
242286
)
243287

244288
return (

0 commit comments

Comments
 (0)