Skip to content

Commit 382da05

Browse files
committed
Added working
1 parent ed4fefd commit 382da05

3 files changed

Lines changed: 86 additions & 56 deletions

File tree

src/core/webview/ClineProvider.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,9 +1691,6 @@ export class ClineProvider implements vscode.WebviewViewProvider {
16911691
requestyModelInfo,
16921692
modelTemperature,
16931693
modelMaxTokens,
1694-
pearaiBaseUrl,
1695-
pearaiModelId,
1696-
pearaiModelInfo,
16971694
} = apiConfiguration
16981695
await Promise.all([
16991696
this.updateGlobalState("apiProvider", apiProvider),
@@ -1743,9 +1740,6 @@ export class ClineProvider implements vscode.WebviewViewProvider {
17431740
this.updateGlobalState("requestyModelInfo", requestyModelInfo),
17441741
this.updateGlobalState("modelTemperature", modelTemperature),
17451742
this.updateGlobalState("modelMaxTokens", modelMaxTokens),
1746-
await this.updateGlobalState("pearaiBaseUrl", PEARAI_URL),
1747-
await this.updateGlobalState("pearaiModelId", pearaiModelId),
1748-
await this.updateGlobalState("pearaiModelInfo", pearaiModelInfo),
17491743
])
17501744
if (this.cline) {
17511745
this.cline.api = buildApiHandler(apiConfiguration)
@@ -2190,8 +2184,6 @@ export class ClineProvider implements vscode.WebviewViewProvider {
21902184
pearaiApiKey,
21912185
pearaiRefreshKey,
21922186
pearaiBaseUrl,
2193-
pearaiModelId,
2194-
pearaiModelInfo,
21952187
mistralCodestralUrl,
21962188
azureApiVersion,
21972189
openAiStreamingEnabled,
@@ -2279,8 +2271,6 @@ export class ClineProvider implements vscode.WebviewViewProvider {
22792271
this.getSecret("pearai-token") as Promise<string | undefined>,
22802272
this.getSecret("pearai-refresh") as Promise<string | undefined>,
22812273
this.getGlobalState("pearaiBaseUrl") as Promise<string | undefined>,
2282-
this.getGlobalState("pearaiModelId") as Promise<string | undefined>,
2283-
this.getGlobalState("pearaiModelInfo") as Promise<ModelInfo | undefined>,
22842274
this.getGlobalState("mistralCodestralUrl") as Promise<string | undefined>,
22852275
this.getGlobalState("azureApiVersion") as Promise<string | undefined>,
22862276
this.getGlobalState("openAiStreamingEnabled") as Promise<boolean | undefined>,
@@ -2384,8 +2374,6 @@ export class ClineProvider implements vscode.WebviewViewProvider {
23842374
mistralApiKey,
23852375
pearaiApiKey,
23862376
pearaiBaseUrl,
2387-
pearaiModelId,
2388-
pearaiModelInfo,
23892377
mistralCodestralUrl,
23902378
azureApiVersion,
23912379
openAiStreamingEnabled,

src/shared/api.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ export interface ApiHandlerOptions {
7272
modelMaxTokens?: number
7373
pearaiApiKey?: string
7474
pearaiBaseUrl?: string
75-
pearaiModelId?: string
76-
pearaiModelInfo?: ModelInfo
7775
}
7876

7977
export type ApiConfiguration = ApiHandlerOptions & {
@@ -807,20 +805,29 @@ export let pearAiModels: Record<string, ModelInfo> = defaultPearAiModels
807805
// DEV:
808806
export const PEARAI_URL = "http://localhost:8000/integrations/cline"
809807

810-
// Dynamically fetch models from PearAI server
811-
export const getPearAiModels = async () => {
808+
// Promise to track initialization status
809+
export let modelsInitialized: Promise<Record<string, ModelInfo>>
810+
811+
// Immediately invoked function to initialize models
812+
modelsInitialized = (async () => {
812813
try {
813814
const res = await fetch(`${PEARAI_URL}/getPearAIAgentModels`)
814815
if (!res.ok) throw new Error("Failed to fetch models")
815816
const config = await res.json()
816817
if (config.models && Object.keys(config.models).length > 0) {
817818
pearAiModels = config.models
818819
pearAiDefaultModelId = config.defaultModelId || "pearai-model"
819-
console.dir("IM HER1111")
820+
console.log("Models successfully loaded from server")
820821
console.dir(pearAiModels)
822+
823+
window.dispatchEvent(
824+
new CustomEvent("pearAiModelsUpdated", {
825+
detail: { models: pearAiModels, defaultModelId: pearAiDefaultModelId },
826+
}),
827+
)
821828
return pearAiModels
822829
} else {
823-
console.dir("IM HER2222")
830+
console.log("Using default models (no models returned from server)")
824831
pearAiModels = defaultPearAiModels
825832
return defaultPearAiModels
826833
}
@@ -829,7 +836,15 @@ export const getPearAiModels = async () => {
829836
pearAiModels = defaultPearAiModels
830837
return defaultPearAiModels
831838
}
839+
})()
840+
841+
// Helper function to ensure models are loaded before operations that depend on them
842+
export const ensureModelsLoaded = async () => {
843+
return await modelsInitialized
832844
}
833845

834-
// Initialize models when module loads
835-
getPearAiModels()
846+
// This will log after models are initialized
847+
modelsInitialized.then(() => {
848+
console.log("Models initialization complete")
849+
console.dir(pearAiModels)
850+
})

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

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,6 @@ import { validateApiConfiguration, validateModelId } from "@/utils/validate"
5353
import { ApiErrorMessage } from "./ApiErrorMessage"
5454
import { ThinkingBudget } from "./ThinkingBudget"
5555

56-
const modelsByProvider: Record<string, Record<string, ModelInfo>> = {
57-
anthropic: anthropicModels,
58-
bedrock: bedrockModels,
59-
vertex: vertexModels,
60-
gemini: geminiModels,
61-
"openai-native": openAiNativeModels,
62-
deepseek: deepSeekModels,
63-
mistral: mistralModels,
64-
pearai: pearAiModels,
65-
}
66-
67-
console.dir("IM HERE888")
68-
console.dir(pearAiModels)
69-
console.dir("IM HERE9999")
70-
console.dir(anthropicModels)
71-
7256
interface ApiOptionsProps {
7357
uriScheme: string | undefined
7458
apiConfiguration: ApiConfiguration
@@ -106,6 +90,8 @@ const ApiOptions = ({
10690
[requestyDefaultModelId]: requestyDefaultModelInfo,
10791
})
10892

93+
const [pearAiModelsState, setPearAiModelsState] = useState<Record<string, ModelInfo>>(pearAiModels)
94+
10995
const [openAiModels, setOpenAiModels] = useState<Record<string, ModelInfo> | null>(null)
11096

11197
const [anthropicBaseUrlSelected, setAnthropicBaseUrlSelected] = useState(!!apiConfiguration?.anthropicBaseUrl)
@@ -130,8 +116,8 @@ const ApiOptions = ({
130116
)
131117

132118
const { selectedProvider, selectedModelId, selectedModelInfo } = useMemo(
133-
() => normalizeApiConfiguration(apiConfiguration),
134-
[apiConfiguration],
119+
() => normalizeApiConfiguration(apiConfiguration, pearAiModelsState),
120+
[apiConfiguration, pearAiModelsState],
135121
)
136122

137123
// Debounced refresh model updates, only executed 250ms after the user
@@ -228,11 +214,63 @@ const ApiOptions = ({
228214
setVsCodeLmModels(newModels)
229215
}
230216
break
217+
case "state":
218+
if (message.state?.apiConfiguration?.pearaiApiKey) {
219+
setPearAiModelsState(pearAiModels)
220+
}
221+
break
231222
}
232223
}, [])
233224

225+
// Update PearAI models when API key changes
226+
useEffect(() => {
227+
if (apiConfiguration?.pearaiApiKey) {
228+
setPearAiModelsState(pearAiModels)
229+
}
230+
}, [apiConfiguration?.pearaiApiKey])
231+
234232
useEvent("message", onMessage)
235233

234+
type ApiProvider =
235+
| "anthropic"
236+
| "bedrock"
237+
| "vertex"
238+
| "gemini"
239+
| "openai-native"
240+
| "deepseek"
241+
| "mistral"
242+
| "pearai"
243+
| "openrouter"
244+
| "glama"
245+
| "unbound"
246+
| "requesty"
247+
| "openai"
248+
| "ollama"
249+
| "lmstudio"
250+
| "vscode-lm"
251+
252+
const modelsByProvider = useMemo<Record<ApiProvider, Record<string, ModelInfo>>>(
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: pearAiModelsState,
262+
openrouter: openRouterModels,
263+
glama: glamaModels,
264+
unbound: unboundModels,
265+
requesty: requestyModels,
266+
openai: openAiModels || {},
267+
ollama: {},
268+
lmstudio: {},
269+
"vscode-lm": {},
270+
}),
271+
[pearAiModelsState, openRouterModels, glamaModels, unboundModels, requestyModels, openAiModels],
272+
)
273+
236274
const selectedProviderModelOptions: DropdownOption[] = useMemo(
237275
() =>
238276
modelsByProvider[selectedProvider]
@@ -244,7 +282,7 @@ const ApiOptions = ({
244282
})),
245283
]
246284
: [],
247-
[selectedProvider],
285+
[selectedProvider, modelsByProvider],
248286
)
249287

250288
return (
@@ -1465,7 +1503,10 @@ export function getOpenRouterAuthUrl(uriScheme?: string) {
14651503
return `https://openrouter.ai/auth?callback_url=${uriScheme || "vscode"}://rooveterinaryinc.roo-cline/openrouter`
14661504
}
14671505

1468-
export function normalizeApiConfiguration(apiConfiguration?: ApiConfiguration) {
1506+
export function normalizeApiConfiguration(
1507+
apiConfiguration?: ApiConfiguration,
1508+
pearAiModelsState?: Record<string, ModelInfo>,
1509+
) {
14691510
const provider = apiConfiguration?.apiProvider || "anthropic"
14701511
const modelId = apiConfiguration?.apiModelId
14711512

@@ -1552,22 +1593,8 @@ export function normalizeApiConfiguration(apiConfiguration?: ApiConfiguration) {
15521593
supportsImages: false, // VSCode LM API currently doesn't support images.
15531594
},
15541595
}
1555-
case "pearai": {
1556-
// Get the base Anthropic model info
1557-
const baseModelInfo = anthropicModels[anthropicDefaultModelId]
1558-
const pearaiModelInfo: ModelInfo = {
1559-
...baseModelInfo,
1560-
inputPrice: baseModelInfo.inputPrice,
1561-
outputPrice: baseModelInfo.outputPrice,
1562-
cacheWritesPrice: baseModelInfo.cacheWritesPrice ? baseModelInfo.cacheWritesPrice : undefined,
1563-
cacheReadsPrice: baseModelInfo.cacheWritesPrice ? baseModelInfo.cacheReadsPrice : undefined,
1564-
}
1565-
return {
1566-
selectedProvider: provider,
1567-
selectedModelId: apiConfiguration?.pearaiModelId || "pearai_model",
1568-
selectedModelInfo: pearaiModelInfo,
1569-
}
1570-
}
1596+
case "pearai":
1597+
return getProviderData(pearAiModelsState || pearAiModels, pearAiDefaultModelId)
15711598
default:
15721599
return getProviderData(anthropicModels, anthropicDefaultModelId)
15731600
}

0 commit comments

Comments
 (0)