diff --git a/hosts/vscode/host-grpc-service-config.ts b/hosts/vscode/host-grpc-service-config.ts deleted file mode 100644 index 0da4ac0a..00000000 --- a/hosts/vscode/host-grpc-service-config.ts +++ /dev/null @@ -1,33 +0,0 @@ -// AUTO-GENERATED FILE - DO NOT MODIFY DIRECTLY -// Generated by proto/build-proto.js - -import { StreamingResponseHandler } from "./host-grpc-handler" -import { handleUriServiceRequest, handleUriServiceStreamingRequest } from "./uri/index" -import { handleWatchServiceRequest, handleWatchServiceStreamingRequest } from "./watch/index" - -/** - * Configuration for a host service handler - */ -export interface HostServiceHandlerConfig { - requestHandler: (method: string, message: any) => Promise - streamingHandler: ( - method: string, - message: any, - responseStream: StreamingResponseHandler, - requestId?: string, - ) => Promise -} - -/** - * Map of host service names to their handler configurations - */ -export const hostServiceHandlers: Record = { - "host.UriService": { - requestHandler: handleUriServiceRequest, - streamingHandler: handleUriServiceStreamingRequest, - }, - "host.WatchService": { - requestHandler: handleWatchServiceRequest, - streamingHandler: handleWatchServiceStreamingRequest, - }, -} diff --git a/hosts/vscode/uri/index.ts b/hosts/vscode/uri/index.ts deleted file mode 100644 index 6bc08c5d..00000000 --- a/hosts/vscode/uri/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -// AUTO-GENERATED FILE - DO NOT MODIFY DIRECTLY -// Generated by proto/build-proto.js - -import { createServiceRegistry, ServiceMethodHandler, StreamingMethodHandler } from "../host-grpc-service" -import { StreamingResponseHandler } from "../host-grpc-handler" -import { registerAllMethods } from "./methods" - -// Create uri service registry -const uriService = createServiceRegistry("uri") - -// Export the method handler types and registration function -export type UriMethodHandler = ServiceMethodHandler -export type UriStreamingMethodHandler = StreamingMethodHandler -export const registerMethod = uriService.registerMethod - -// Export the request handlers -export const handleUriServiceRequest = uriService.handleRequest -export const handleUriServiceStreamingRequest = uriService.handleStreamingRequest -export const isStreamingMethod = uriService.isStreamingMethod - -// Register all uri methods -registerAllMethods() diff --git a/hosts/vscode/uri/methods.ts b/hosts/vscode/uri/methods.ts deleted file mode 100644 index 8dec5cde..00000000 --- a/hosts/vscode/uri/methods.ts +++ /dev/null @@ -1,16 +0,0 @@ -// AUTO-GENERATED FILE - DO NOT MODIFY DIRECTLY -// Generated by proto/build-proto.js - -// Import all method implementations -import { registerMethod } from "./index" -import { file } from "./file" -import { joinPath } from "./joinPath" -import { parse } from "./parse" - -// Register all uri service methods -export function registerAllMethods(): void { - // Register each method with the registry - registerMethod("file", file) - registerMethod("joinPath", joinPath) - registerMethod("parse", parse) -} diff --git a/hosts/vscode/watch/index.ts b/hosts/vscode/watch/index.ts deleted file mode 100644 index 2167ba5c..00000000 --- a/hosts/vscode/watch/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -// AUTO-GENERATED FILE - DO NOT MODIFY DIRECTLY -// Generated by proto/build-proto.js - -import { createServiceRegistry, ServiceMethodHandler, StreamingMethodHandler } from "../host-grpc-service" -import { StreamingResponseHandler } from "../host-grpc-handler" -import { registerAllMethods } from "./methods" - -// Create watch service registry -const watchService = createServiceRegistry("watch") - -// Export the method handler types and registration function -export type WatchMethodHandler = ServiceMethodHandler -export type WatchStreamingMethodHandler = StreamingMethodHandler -export const registerMethod = watchService.registerMethod - -// Export the request handlers -export const handleWatchServiceRequest = watchService.handleRequest -export const handleWatchServiceStreamingRequest = watchService.handleStreamingRequest -export const isStreamingMethod = watchService.isStreamingMethod - -// Register all watch methods -registerAllMethods() diff --git a/hosts/vscode/watch/methods.ts b/hosts/vscode/watch/methods.ts deleted file mode 100644 index c7ed68ea..00000000 --- a/hosts/vscode/watch/methods.ts +++ /dev/null @@ -1,15 +0,0 @@ -// AUTO-GENERATED FILE - DO NOT MODIFY DIRECTLY -// Generated by proto/build-proto.js - -// Import all method implementations -import { registerMethod } from "./index" -import { subscribeToFile } from "./subscribeToFile" - -// Streaming methods for this service -export const streamingMethods = ["subscribeToFile"] - -// Register all watch service methods -export function registerAllMethods(): void { - // Register each method with the registry - registerMethod("subscribeToFile", subscribeToFile, { isStreaming: true }) -} diff --git a/proto/models.proto b/proto/models.proto index b10c4404..706eb047 100644 --- a/proto/models.proto +++ b/proto/models.proto @@ -20,6 +20,8 @@ service ModelsService { rpc refreshOpenAiModels(OpenAiModelsRequest) returns (StringArray); // Refreshes and returns Requesty models rpc refreshRequestyModels(EmptyRequest) returns (OpenRouterCompatibleModelInfo); + // Subscribe to OpenRouter models updates + rpc subscribeToOpenRouterModels(EmptyRequest) returns (stream OpenRouterCompatibleModelInfo); } // List of VS Code LM models @@ -35,17 +37,42 @@ message VsCodeLmModel { string id = 4; } +// Price tier for tiered pricing models +message PriceTier { + int32 token_limit = 1; // Upper limit (inclusive) of input tokens for this price + double price = 2; // Price per million tokens for this tier +} + +// Thinking configuration for models that support thinking/reasoning +message ThinkingConfig { + optional int32 max_budget = 1; // Max allowed thinking budget tokens + optional double output_price = 2; // Output price per million tokens when budget > 0 + repeated PriceTier output_price_tiers = 3; // Optional: Tiered output price when budget > 0 +} + +// Model tier for tiered pricing structures +message ModelTier { + int32 context_window = 1; + optional double input_price = 2; + optional double output_price = 3; + optional double cache_writes_price = 4; + optional double cache_reads_price = 5; +} + // For OpenRouterCompatibleModelInfo structure in OpenRouterModels message OpenRouterModelInfo { - int32 max_tokens = 1; - int32 context_window = 2; - bool supports_images = 3; + optional int32 max_tokens = 1; + optional int32 context_window = 2; + optional bool supports_images = 3; bool supports_prompt_cache = 4; - double input_price = 5; - double output_price = 6; - double cache_writes_price = 7; - double cache_reads_price = 8; - string description = 9; + optional double input_price = 5; + optional double output_price = 6; + optional double cache_writes_price = 7; + optional double cache_reads_price = 8; + optional string description = 9; + optional ThinkingConfig thinking_config = 10; + optional bool supports_global_endpoint = 11; + repeated ModelTier tiers = 12; } // Shared response message for model information diff --git a/src/core/controller/index.ts b/src/core/controller/index.ts index 498acc83..50a8ec22 100644 --- a/src/core/controller/index.ts +++ b/src/core/controller/index.ts @@ -1,7 +1,6 @@ import { Anthropic } from "@anthropic-ai/sdk" import axios from "axios" import { v4 as uuidv4 } from "uuid" - import fs from "fs/promises" import { setTimeout as setTimeoutPromise } from "node:timers/promises" import pWaitFor from "p-wait-for" @@ -13,12 +12,8 @@ import { EmptyRequest } from "@shared/proto/common" import { buildApiHandler } from "@api/index" import { cleanupLegacyCheckpoints } from "@integrations/checkpoints/CheckpointMigration" import { downloadTask } from "@integrations/misc/export-markdown" -import { fetchOpenGraphData } from "@integrations/misc/link-preview" -import { handleFileServiceRequest } from "./file" -import { getTheme } from "@integrations/theme/getTheme" import WorkspaceTracker from "@integrations/workspace/WorkspaceTracker" import { ClineAccountService } from "@services/account/ClineAccountService" -import { BrowserSession } from "@services/browser/BrowserSession" import { McpHub } from "@services/mcp/McpHub" import { telemetryService } from "@/services/posthog/telemetry/TelemetryService" import { ApiProvider, ModelInfo } from "@shared/api" @@ -26,40 +21,31 @@ import { ChatContent } from "@shared/ChatContent" import { ChatSettings } from "@shared/ChatSettings" import { ExtensionMessage, ExtensionState, Platform } from "@shared/ExtensionMessage" import { HistoryItem } from "@shared/HistoryItem" -import { McpDownloadResponse, McpMarketplaceCatalog, McpServer } from "@shared/mcp" +import { McpMarketplaceCatalog } from "@shared/mcp" import { TelemetrySetting } from "@shared/TelemetrySetting" import { WebviewMessage } from "@shared/WebviewMessage" import { fileExistsAtPath } from "@utils/fs" import { getWorkingState } from "@utils/git" import { extractCommitMessage } from "@integrations/git/commit-message-generator" -import { getTotalTasksSize } from "@utils/storage" -import { - ensureMcpServersDirectoryExists, - ensureSettingsDirectoryExists, - GlobalFileNames, - ensureWorkflowsDirectoryExists, -} from "../storage/disk" +import { ensureMcpServersDirectoryExists, ensureSettingsDirectoryExists, GlobalFileNames } from "../storage/disk" import { getAllExtensionState, getGlobalState, getSecret, getWorkspaceState, - resetExtensionState, storeSecret, updateApiConfiguration, updateGlobalState, updateWorkspaceState, } from "../storage/state" -import { Task, cwd } from "../task" +import { Task } from "../task" import { ClineRulesToggles } from "@shared/cline-rules" import { sendStateUpdate } from "./state/subscribeToState" import { sendAddToInputEvent } from "./ui/subscribeToAddToInput" import { sendAuthCallbackEvent } from "./account/subscribeToAuthCallback" -import { sendChatButtonClickedEvent } from "./ui/subscribeToChatButtonClicked" import { sendMcpMarketplaceCatalogEvent } from "./mcp/subscribeToMcpMarketplaceCatalog" -import { refreshClineRulesToggles } from "@core/context/instructions/user-instructions/cline-rules" -import { refreshExternalRulesToggles } from "@core/context/instructions/user-instructions/external-rules" -import { refreshWorkflowToggles } from "@core/context/instructions/user-instructions/workflows" +import { sendOpenRouterModelsEvent } from "./models/subscribeToOpenRouterModels" +import { OpenRouterCompatibleModelInfo } from "@/shared/proto/models" /* https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts @@ -227,10 +213,7 @@ export class Controller { // post last cached models in case the call to endpoint fails this.readOpenRouterModels().then((openRouterModels) => { if (openRouterModels) { - this.postMessageToWebview({ - type: "openRouterModels", - openRouterModels, - }) + sendOpenRouterModelsEvent(OpenRouterCompatibleModelInfo.create({ models: openRouterModels })) } }) // gui relies on model info to be up-to-date to provide the most accurate pricing, so we need to fetch the latest details on launch. diff --git a/src/core/controller/models/refreshOpenRouterModels.ts b/src/core/controller/models/refreshOpenRouterModels.ts index 7d5480e9..62d72102 100644 --- a/src/core/controller/models/refreshOpenRouterModels.ts +++ b/src/core/controller/models/refreshOpenRouterModels.ts @@ -19,7 +19,7 @@ export async function refreshOpenRouterModels( ): Promise { const openRouterModelsFilePath = path.join(await ensureCacheDirectoryExists(controller), GlobalFileNames.openRouterModels) - let models: Record> = {} + let models: Record = {} try { const response = await axios.get("https://openrouter.ai/api/v1/models") @@ -32,15 +32,20 @@ export async function refreshOpenRouterModels( return undefined } for (const rawModel of rawModels) { - const modelInfo: Partial = { - maxTokens: rawModel.top_provider?.max_completion_tokens, - contextWindow: rawModel.context_length, - supportsImages: rawModel.architecture?.modality?.includes("image"), + const modelInfo = OpenRouterModelInfo.create({ + maxTokens: rawModel.top_provider?.max_completion_tokens ?? 0, + contextWindow: rawModel.context_length ?? 0, + supportsImages: rawModel.architecture?.modality?.includes("image") ?? false, supportsPromptCache: false, - inputPrice: parsePrice(rawModel.pricing?.prompt), - outputPrice: parsePrice(rawModel.pricing?.completion), - description: rawModel.description, - } + inputPrice: parsePrice(rawModel.pricing?.prompt) ?? 0, + outputPrice: parsePrice(rawModel.pricing?.completion) ?? 0, + cacheWritesPrice: 0, + cacheReadsPrice: 0, + description: rawModel.description ?? "", + thinkingConfig: rawModel.thinking_config ?? undefined, + supportsGlobalEndpoint: rawModel.supports_global_endpoint ?? undefined, + tiers: rawModel.tiers ?? [], + }) switch (rawModel.id) { case "anthropic/claude-sonnet-4": @@ -129,30 +134,13 @@ export async function refreshOpenRouterModels( } } - // Convert the Record> to Record - // by filling in any missing required fields with defaults - const typedModels: Record = {} - for (const [key, model] of Object.entries(models)) { - typedModels[key] = { - maxTokens: model.maxTokens ?? 0, - contextWindow: model.contextWindow ?? 0, - supportsImages: model.supportsImages ?? false, - supportsPromptCache: model.supportsPromptCache ?? false, - inputPrice: model.inputPrice ?? 0, - outputPrice: model.outputPrice ?? 0, - cacheWritesPrice: model.cacheWritesPrice ?? 0, - cacheReadsPrice: model.cacheReadsPrice ?? 0, - description: model.description ?? "", - } - } - - return OpenRouterCompatibleModelInfo.create({ models: typedModels }) + return OpenRouterCompatibleModelInfo.create({ models }) } /** * Reads cached OpenRouter models from disk */ -async function readOpenRouterModels(controller: Controller): Promise> | undefined> { +async function readOpenRouterModels(controller: Controller): Promise | undefined> { const openRouterModelsFilePath = path.join(await ensureCacheDirectoryExists(controller), GlobalFileNames.openRouterModels) const fileExists = await fileExistsAtPath(openRouterModelsFilePath) if (fileExists) { diff --git a/src/core/controller/models/subscribeToOpenRouterModels.ts b/src/core/controller/models/subscribeToOpenRouterModels.ts new file mode 100644 index 00000000..1f6a5ebd --- /dev/null +++ b/src/core/controller/models/subscribeToOpenRouterModels.ts @@ -0,0 +1,60 @@ +import { Controller } from "../index" +import { EmptyRequest } from "@shared/proto/common" +import { OpenRouterCompatibleModelInfo } from "@shared/proto/models" +import { StreamingResponseHandler, getRequestRegistry } from "../grpc-handler" + +// Keep track of active OpenRouter models subscriptions +const activeOpenRouterModelsSubscriptions = new Set() + +/** + * Subscribe to OpenRouter models events + * @param controller The controller instance + * @param request The empty request + * @param responseStream The streaming response handler + * @param requestId The ID of the request (passed by the gRPC handler) + */ +export async function subscribeToOpenRouterModels( + controller: Controller, + request: EmptyRequest, + responseStream: StreamingResponseHandler, + requestId?: string, +): Promise { + console.log("[DEBUG] set up OpenRouter models subscription") + + // Add this subscription to the active subscriptions + activeOpenRouterModelsSubscriptions.add(responseStream) + + // Register cleanup when the connection is closed + const cleanup = () => { + activeOpenRouterModelsSubscriptions.delete(responseStream) + console.log("[DEBUG] Cleaned up OpenRouter models subscription") + } + + // Register the cleanup function with the request registry if we have a requestId + if (requestId) { + getRequestRegistry().registerRequest(requestId, cleanup, { type: "openRouterModels_subscription" }, responseStream) + } +} + +/** + * Send an OpenRouter models event to all active subscribers + * @param models The OpenRouter models to send + */ +export async function sendOpenRouterModelsEvent(models: OpenRouterCompatibleModelInfo): Promise { + // Send the event to all active subscribers + const promises = Array.from(activeOpenRouterModelsSubscriptions).map(async (responseStream) => { + try { + await responseStream( + models, + false, // Not the last message + ) + console.log("[DEBUG] sending OpenRouter models event") + } catch (error) { + console.error("Error sending OpenRouter models event:", error) + // Remove the subscription if there was an error + activeOpenRouterModelsSubscriptions.delete(responseStream) + } + }) + + await Promise.all(promises) +} diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index 15dbdbb0..23abfa4f 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -20,7 +20,6 @@ export interface ExtensionMessage { | "ollamaModels" | "lmStudioModels" | "workspaceUpdated" - | "openRouterModels" | "openAiModels" | "requestyModels" | "mcpServers" @@ -43,7 +42,6 @@ export interface ExtensionMessage { lmStudioModels?: string[] vsCodeLmModels?: { vendor?: string; family?: string; version?: string; id?: string }[] filePaths?: string[] - openRouterModels?: Record openAiModels?: string[] requestyModels?: Record mcpServers?: McpServer[] diff --git a/src/standalone/services/host-grpc-client.ts b/src/standalone/services/host-grpc-client.ts deleted file mode 100644 index 354cdc22..00000000 --- a/src/standalone/services/host-grpc-client.ts +++ /dev/null @@ -1,11 +0,0 @@ -// AUTO-GENERATED FILE - DO NOT MODIFY DIRECTLY -// Generated by proto/build-proto.js - -import { createGrpcClient } from "./host-grpc-client-base" -import { UriServiceDefinition } from "@shared/proto/host/uri" -import { WatchServiceDefinition } from "@shared/proto/host/watch" - -const UriServiceClient = createGrpcClient(UriServiceDefinition) -const WatchServiceClient = createGrpcClient(WatchServiceDefinition) - -export { UriServiceClient, WatchServiceClient } diff --git a/webview-ui/src/context/ExtensionStateContext.tsx b/webview-ui/src/context/ExtensionStateContext.tsx index 1b90e255..f3a264b9 100644 --- a/webview-ui/src/context/ExtensionStateContext.tsx +++ b/webview-ui/src/context/ExtensionStateContext.tsx @@ -20,7 +20,9 @@ import { import { McpMarketplaceCatalog, McpServer, McpViewTab } from "../../../src/shared/mcp" import { ModelsServiceClient, StateServiceClient, UiServiceClient, McpServiceClient } from "../services/grpc-client" import { convertTextMateToHljs } from "../utils/textMateToHljs" +import { convertOpenRouterCompatibleModelInfoToModelInfoRecord } from "../../../src/shared/proto-conversions/models/openrouter-models-conversion" import { vscode } from "../utils/vscode" +import { OpenRouterCompatibleModelInfo } from "@shared/proto/models" interface ExtensionStateContextType extends ExtensionState { didHydrateState: boolean @@ -201,14 +203,6 @@ export const ExtensionStateContextProvider: React.FC<{ setFilePaths(message.filePaths ?? []) break } - case "openRouterModels": { - const updatedModels = message.openRouterModels ?? {} - setOpenRouterModels({ - [openRouterDefaultModelId]: openRouterDefaultModelInfo, // in case the extension sent a model list without the default model - ...updatedModels, - }) - break - } case "openAiModels": { const updatedModels = message.openAiModels ?? [] setOpenAiModels(updatedModels) @@ -241,6 +235,7 @@ export const ExtensionStateContextProvider: React.FC<{ const partialMessageUnsubscribeRef = useRef<(() => void) | null>(null) const mcpMarketplaceUnsubscribeRef = useRef<(() => void) | null>(null) const themeSubscriptionRef = useRef<(() => void) | null>(null) + const openRouterModelsUnsubscribeRef = useRef<(() => void) | null>(null) // Subscribe to state updates and UI events using the gRPC streaming API useEffect(() => { @@ -461,6 +456,23 @@ export const ExtensionStateContextProvider: React.FC<{ }, }) + // Subscribe to OpenRouter models updates + openRouterModelsUnsubscribeRef.current = ModelsServiceClient.subscribeToOpenRouterModels(EmptyRequest.create({}), { + onResponse: (response: OpenRouterCompatibleModelInfo) => { + const models = response.models + setOpenRouterModels({ + [openRouterDefaultModelId]: openRouterDefaultModelInfo, // in case the extension sent a model list without the default model + ...models, + }) + }, + onError: (error) => { + console.error("Error in OpenRouter models subscription:", error) + }, + onComplete: () => { + console.log("OpenRouter models subscription completed") + }, + }) + // Still send the webviewDidLaunch message for other initialization vscode.postMessage({ type: "webviewDidLaunch" }) @@ -517,15 +529,20 @@ export const ExtensionStateContextProvider: React.FC<{ themeSubscriptionRef.current() themeSubscriptionRef.current = null } + if (openRouterModelsUnsubscribeRef.current) { + openRouterModelsUnsubscribeRef.current() + openRouterModelsUnsubscribeRef.current = null + } } }, []) const refreshOpenRouterModels = useCallback(() => { ModelsServiceClient.refreshOpenRouterModels(EmptyRequest.create({})) - .then((res) => { + .then((response: OpenRouterCompatibleModelInfo) => { + const models = response.models setOpenRouterModels({ [openRouterDefaultModelId]: openRouterDefaultModelInfo, // in case the extension sent a model list without the default model - ...res.models, + ...models, }) }) .catch((error: Error) => console.error("Failed to refresh OpenRouter models:", error))