Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/api/providers/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,28 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
id = "claude-3-7-sonnet-20250219"
}

// Prioritize serverside model info
if (this.options.apiModelId && this.options.pearaiAgentModels) {
let modelInfo = null
if (this.options.apiModelId.startsWith("pearai")) {
modelInfo = this.options.pearaiAgentModels.models[this.options.apiModelId].underlyingModelUpdated
} else {
modelInfo = this.options.pearaiAgentModels.models[this.options.apiModelId || "pearai-model"]
}
if (modelInfo) {
return {
id: this.options.apiModelId,
info: modelInfo,
virtualId,
...getModelParams({
options: this.options,
model: info,
defaultMaxTokens: ANTHROPIC_DEFAULT_MAX_TOKENS,
}),
}
}
}

return {
id,
info,
Expand Down
37 changes: 8 additions & 29 deletions src/api/providers/pearai/pearai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { OpenAiHandler } from "../openai"
import { PearAIGenericHandler } from "./pearaiGeneric"
import { PEARAI_URL } from "../../../shared/pearaiApi"

interface PearAiModelsResponse {
export interface PearAIAgentModelsConfig {
models: {
[key: string]: {
underlyingModel?: { [key: string]: any }
Expand All @@ -23,7 +23,7 @@ interface PearAiModelsResponse {

export class PearAiHandler extends BaseProvider implements SingleCompletionHandler {
private handler!: AnthropicHandler | PearAIGenericHandler
private pearAiModelsResponse: PearAiModelsResponse | null = null
private pearAIAgentModels: PearAIAgentModelsConfig | null = null
private options: ApiHandlerOptions

constructor(options: ApiHandlerOptions) {
Expand Down Expand Up @@ -64,15 +64,13 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl

if (modelId.startsWith("pearai")) {
try {
const response = await fetch(`${PEARAI_URL}/getPearAIAgentModels`)
if (!response.ok) {
throw new Error(`Failed to fetch models: ${response.statusText}`)
if (!options.pearaiAgentModels) {
throw new Error("PearAI models not found")
}
const data = (await response.json()) as PearAiModelsResponse
this.pearAiModelsResponse = data
const pearaiAgentModels = options.pearaiAgentModels
const underlyingModel =
data.models[modelId]?.underlyingModelUpdated?.underlyingModel ||
data.models[modelId]?.underlyingModel ||
pearaiAgentModels.models[modelId]?.underlyingModelUpdated?.underlyingModel ||
pearaiAgentModels.models[modelId]?.underlyingModel ||
"claude-3-5-sonnet-20241022"
if (underlyingModel.startsWith("claude") || modelId.startsWith("anthropic/")) {
// Default to Claude
Expand Down Expand Up @@ -116,26 +114,7 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl
}
}

getModel(): { id: string; info: ModelInfo } {
if (this.options.apiModelId) {
let modelInfo = null
if (this.options.apiModelId.startsWith("pearai")) {
modelInfo = this.pearAiModelsResponse?.models[this.options.apiModelId].underlyingModelUpdated
} else if (this.pearAiModelsResponse) {
modelInfo = this.pearAiModelsResponse.models[this.options.apiModelId || "pearai-model"]
}
if (modelInfo) {
return {
id: this.options.apiModelId,
info: {
contextWindow: modelInfo.contextWindow || 4096, // provide default or actual value
supportsPromptCache: modelInfo.supportsPromptCaching || false, // provide default or actual value
...modelInfo,
},
}
}
}

public getModel(): { id: string; info: ModelInfo } {
// Fallback to using what's available on client side
const baseModel = this.handler.getModel()
return baseModel
Expand Down
15 changes: 15 additions & 0 deletions src/api/providers/pearai/pearaiGeneric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@ export class PearAIGenericHandler extends BaseProvider implements SingleCompleti

override getModel(): { id: string; info: ModelInfo } {
const modelId = this.options.openAiModelId ?? "none"
// Prioritize serverside model info
if (this.options.apiModelId && this.options.pearaiAgentModels) {
let modelInfo = null
if (this.options.apiModelId.startsWith("pearai")) {
modelInfo = this.options.pearaiAgentModels.models[this.options.apiModelId].underlyingModelUpdated
} else {
modelInfo = this.options.pearaiAgentModels.models[this.options.apiModelId || "pearai-model"]
}
if (modelInfo) {
return {
id: this.options.apiModelId,
info: modelInfo,
}
}
}
return {
id: modelId,
info: allModels[modelId],
Expand Down
19 changes: 17 additions & 2 deletions src/core/webview/ClineProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ import { getUri } from "./getUri"
import { telemetryService } from "../../services/telemetry/TelemetryService"
import { TelemetrySetting } from "../../shared/TelemetrySetting"
import { getWorkspacePath } from "../../utils/path"
import { PEARAI_URL } from "../../shared/pearaiApi"
import { PearAIAgentModelsConfig } from "../../api/providers/pearai/pearai"

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

public async getPearAIAgentModels() {
const response = await fetch(`${PEARAI_URL}/getPearAIAgentModels`)
if (!response.ok) {
throw new Error(`Failed to fetch models: ${response.statusText}`)
}
const data = (await response.json()) as PearAIAgentModelsConfig
return data
}

public async initClineWithSubTask(parent: Cline, task?: string, images?: string[]) {
return this.initClineWithTask(task, images, parent)
}
Expand All @@ -482,9 +493,11 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
const modePrompt = customModePrompts?.[mode] as PromptComponent
const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")

const pearaiAgentModels = await this.getPearAIAgentModels()

const cline = new Cline({
provider: this,
apiConfiguration,
apiConfiguration: { ...apiConfiguration, pearaiAgentModels },
customInstructions: effectiveInstructions,
enableDiff,
enableCheckpoints,
Expand Down Expand Up @@ -549,9 +562,11 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
}
}

const pearaiAgentModels = await this.getPearAIAgentModels()

const cline = new Cline({
provider: this,
apiConfiguration,
apiConfiguration: { ...apiConfiguration, pearaiAgentModels },
customInstructions: effectiveInstructions,
enableDiff,
...checkpoints,
Expand Down
2 changes: 2 additions & 0 deletions src/shared/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from "vscode"
import { PearAIAgentModelsConfig } from "../api/providers/pearai/pearai"

export type ApiProvider =
| "anthropic"
Expand Down Expand Up @@ -81,6 +82,7 @@ export interface ApiHandlerOptions {
pearaiBaseUrl?: string
pearaiModelId?: string
pearaiModelInfo?: ModelInfo
pearaiAgentModels?: PearAIAgentModelsConfig
modelMaxThinkingTokens?: number
fakeAi?: unknown
}
Expand Down
Loading