Skip to content

Commit 8b405c4

Browse files
authored
Merge pull request #63 from trypear/pearaiAgentModels-null-fix
pearaiAgentModels-null-fix
2 parents ca9e0a1 + 6352282 commit 8b405c4

5 files changed

Lines changed: 64 additions & 31 deletions

File tree

src/api/providers/anthropic.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,28 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
227227
id = "claude-3-7-sonnet-20250219"
228228
}
229229

230+
// Prioritize serverside model info
231+
if (this.options.apiModelId && this.options.pearaiAgentModels) {
232+
let modelInfo = null
233+
if (this.options.apiModelId.startsWith("pearai")) {
234+
modelInfo = this.options.pearaiAgentModels.models[this.options.apiModelId].underlyingModelUpdated
235+
} else {
236+
modelInfo = this.options.pearaiAgentModels.models[this.options.apiModelId || "pearai-model"]
237+
}
238+
if (modelInfo) {
239+
return {
240+
id: this.options.apiModelId,
241+
info: modelInfo,
242+
virtualId,
243+
...getModelParams({
244+
options: this.options,
245+
model: info,
246+
defaultMaxTokens: ANTHROPIC_DEFAULT_MAX_TOKENS,
247+
}),
248+
}
249+
}
250+
}
251+
230252
return {
231253
id,
232254
info,

src/api/providers/pearai/pearai.ts

Lines changed: 8 additions & 29 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,13 @@ 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+
throw new Error("PearAI models not found")
7069
}
71-
const data = (await response.json()) as PearAiModelsResponse
72-
this.pearAiModelsResponse = data
70+
const pearaiAgentModels = options.pearaiAgentModels
7371
const underlyingModel =
74-
data.models[modelId]?.underlyingModelUpdated?.underlyingModel ||
75-
data.models[modelId]?.underlyingModel ||
72+
pearaiAgentModels.models[modelId]?.underlyingModelUpdated?.underlyingModel ||
73+
pearaiAgentModels.models[modelId]?.underlyingModel ||
7674
"claude-3-5-sonnet-20241022"
7775
if (underlyingModel.startsWith("claude") || modelId.startsWith("anthropic/")) {
7876
// Default to Claude
@@ -116,26 +114,7 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl
116114
}
117115
}
118116

119-
getModel(): { id: string; info: ModelInfo } {
120-
if (this.options.apiModelId) {
121-
let modelInfo = null
122-
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"]
126-
}
127-
if (modelInfo) {
128-
return {
129-
id: this.options.apiModelId,
130-
info: {
131-
contextWindow: modelInfo.contextWindow || 4096, // provide default or actual value
132-
supportsPromptCache: modelInfo.supportsPromptCaching || false, // provide default or actual value
133-
...modelInfo,
134-
},
135-
}
136-
}
137-
}
138-
117+
public getModel(): { id: string; info: ModelInfo } {
139118
// Fallback to using what's available on client side
140119
const baseModel = this.handler.getModel()
141120
return baseModel

src/api/providers/pearai/pearaiGeneric.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ export class PearAIGenericHandler extends BaseProvider implements SingleCompleti
222222

223223
override getModel(): { id: string; info: ModelInfo } {
224224
const modelId = this.options.openAiModelId ?? "none"
225+
// Prioritize serverside model info
226+
if (this.options.apiModelId && this.options.pearaiAgentModels) {
227+
let modelInfo = null
228+
if (this.options.apiModelId.startsWith("pearai")) {
229+
modelInfo = this.options.pearaiAgentModels.models[this.options.apiModelId].underlyingModelUpdated
230+
} else {
231+
modelInfo = this.options.pearaiAgentModels.models[this.options.apiModelId || "pearai-model"]
232+
}
233+
if (modelInfo) {
234+
return {
235+
id: this.options.apiModelId,
236+
info: modelInfo,
237+
}
238+
}
239+
}
225240
return {
226241
id: modelId,
227242
info: allModels[modelId],

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)