Skip to content

Commit 654b62d

Browse files
pearaiAgentModels-null-fix
1 parent ca9e0a1 commit 654b62d

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/api/providers/pearai/pearai.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { OpenAiHandler } from "../openai"
1111
import { PearAIGenericHandler } from "./pearaiGeneric"
1212
import { PEARAI_URL } from "../../../shared/pearaiApi"
1313

14-
interface PearAiModelsResponse {
14+
export interface PearAIAgentModelsConfig {
1515
models: {
1616
[key: string]: {
1717
underlyingModel?: { [key: string]: any }
@@ -23,7 +23,7 @@ interface PearAiModelsResponse {
2323

2424
export class PearAiHandler extends BaseProvider implements SingleCompletionHandler {
2525
private handler!: AnthropicHandler | PearAIGenericHandler
26-
private pearAiModelsResponse: PearAiModelsResponse | null = null
26+
private pearAIAgentModels: PearAIAgentModelsConfig | null = null
2727
private options: ApiHandlerOptions
2828

2929
constructor(options: ApiHandlerOptions) {
@@ -64,15 +64,17 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl
6464

6565
if (modelId.startsWith("pearai")) {
6666
try {
67-
const response = await fetch(`${PEARAI_URL}/getPearAIAgentModels`)
68-
if (!response.ok) {
69-
throw new Error(`Failed to fetch models: ${response.statusText}`)
67+
if (!options.pearaiAgentModels) {
68+
console.log("NOT AMAZING")
69+
throw new Error("PearAI models not found")
70+
} else {
71+
console.log("AMAZING")
7072
}
71-
const data = (await response.json()) as PearAiModelsResponse
72-
this.pearAiModelsResponse = data
73+
const pearaiAgentModels = options.pearaiAgentModels
74+
this.pearAIAgentModels = pearaiAgentModels
7375
const underlyingModel =
74-
data.models[modelId]?.underlyingModelUpdated?.underlyingModel ||
75-
data.models[modelId]?.underlyingModel ||
76+
pearaiAgentModels.models[modelId]?.underlyingModelUpdated?.underlyingModel ||
77+
pearaiAgentModels.models[modelId]?.underlyingModel ||
7678
"claude-3-5-sonnet-20241022"
7779
if (underlyingModel.startsWith("claude") || modelId.startsWith("anthropic/")) {
7880
// Default to Claude
@@ -120,9 +122,9 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl
120122
if (this.options.apiModelId) {
121123
let modelInfo = null
122124
if (this.options.apiModelId.startsWith("pearai")) {
123-
modelInfo = this.pearAiModelsResponse?.models[this.options.apiModelId].underlyingModelUpdated
124-
} else if (this.pearAiModelsResponse) {
125-
modelInfo = this.pearAiModelsResponse.models[this.options.apiModelId || "pearai-model"]
125+
modelInfo = this.pearAIAgentModels?.models[this.options.apiModelId].underlyingModelUpdated
126+
} else if (this.pearAIAgentModels) {
127+
modelInfo = this.pearAIAgentModels.models[this.options.apiModelId || "pearai-model"]
126128
}
127129
if (modelInfo) {
128130
return {

src/core/webview/ClineProvider.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ import { getUri } from "./getUri"
7878
import { telemetryService } from "../../services/telemetry/TelemetryService"
7979
import { TelemetrySetting } from "../../shared/TelemetrySetting"
8080
import { getWorkspacePath } from "../../utils/path"
81+
import { PEARAI_URL } from "../../shared/pearaiApi"
82+
import { PearAIAgentModelsConfig } from "../../api/providers/pearai/pearai"
8183

8284
/**
8385
* https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
@@ -459,6 +461,15 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
459461
this.outputChannel.appendLine("Webview view resolved")
460462
}
461463

464+
public async getPearAIAgentModels() {
465+
const response = await fetch(`${PEARAI_URL}/getPearAIAgentModels`)
466+
if (!response.ok) {
467+
throw new Error(`Failed to fetch models: ${response.statusText}`)
468+
}
469+
const data = (await response.json()) as PearAIAgentModelsConfig
470+
return data
471+
}
472+
462473
public async initClineWithSubTask(parent: Cline, task?: string, images?: string[]) {
463474
return this.initClineWithTask(task, images, parent)
464475
}
@@ -482,9 +493,11 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
482493
const modePrompt = customModePrompts?.[mode] as PromptComponent
483494
const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")
484495

496+
const pearaiAgentModels = await this.getPearAIAgentModels()
497+
485498
const cline = new Cline({
486499
provider: this,
487-
apiConfiguration,
500+
apiConfiguration: { ...apiConfiguration, pearaiAgentModels },
488501
customInstructions: effectiveInstructions,
489502
enableDiff,
490503
enableCheckpoints,
@@ -549,9 +562,11 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
549562
}
550563
}
551564

565+
const pearaiAgentModels = await this.getPearAIAgentModels()
566+
552567
const cline = new Cline({
553568
provider: this,
554-
apiConfiguration,
569+
apiConfiguration: { ...apiConfiguration, pearaiAgentModels },
555570
customInstructions: effectiveInstructions,
556571
enableDiff,
557572
...checkpoints,

src/shared/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from "vscode"
2+
import { PearAIAgentModelsConfig } from "../api/providers/pearai/pearai"
23

34
export type ApiProvider =
45
| "anthropic"
@@ -81,6 +82,7 @@ export interface ApiHandlerOptions {
8182
pearaiBaseUrl?: string
8283
pearaiModelId?: string
8384
pearaiModelInfo?: ModelInfo
85+
pearaiAgentModels?: PearAIAgentModelsConfig
8486
modelMaxThinkingTokens?: number
8587
fakeAi?: unknown
8688
}

0 commit comments

Comments
 (0)