|
| 1 | +import type { Controller } from "../index" |
| 2 | +import { EmptyRequest, Empty } from "@shared/proto/common" |
| 3 | +import { handleModelsServiceRequest } from "../models" |
| 4 | +import { getAllExtensionState, getGlobalState, updateGlobalState } from "../../storage/state" |
| 5 | +import { sendOpenRouterModelsEvent } from "../models/subscribeToOpenRouterModels" |
| 6 | +import { sendMcpMarketplaceCatalogEvent } from "../mcp/subscribeToMcpMarketplaceCatalog" |
| 7 | +import { telemetryService } from "@/services/posthog/telemetry/TelemetryService" |
| 8 | +import { OpenRouterCompatibleModelInfo } from "@/shared/proto/models" |
| 9 | +import { McpMarketplaceCatalog } from "@shared/mcp" |
| 10 | + |
| 11 | +/** |
| 12 | + * Initialize webview when it launches |
| 13 | + * @param controller The controller instance |
| 14 | + * @param request The empty request |
| 15 | + * @returns Empty response |
| 16 | + */ |
| 17 | +export async function initializeWebview(controller: Controller, request: EmptyRequest): Promise<Empty> { |
| 18 | + try { |
| 19 | + // Populate file paths for workspace tracker (don't await) |
| 20 | + controller.workspaceTracker?.populateFilePaths() |
| 21 | + |
| 22 | + // Post last cached models in case the call to endpoint fails |
| 23 | + controller.readOpenRouterModels().then((openRouterModels) => { |
| 24 | + if (openRouterModels) { |
| 25 | + sendOpenRouterModelsEvent(OpenRouterCompatibleModelInfo.create({ models: openRouterModels })) |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + // Refresh OpenRouter models from API |
| 30 | + handleModelsServiceRequest(controller, "refreshOpenRouterModels", EmptyRequest.create()).then(async (response) => { |
| 31 | + if (response && response.models) { |
| 32 | + // Update model info in state (this needs to be done here since we don't want to update state while settings is open, and we may refresh models there) |
| 33 | + const { apiConfiguration } = await getAllExtensionState(controller.context) |
| 34 | + if (apiConfiguration.openRouterModelId && response.models[apiConfiguration.openRouterModelId]) { |
| 35 | + await updateGlobalState( |
| 36 | + controller.context, |
| 37 | + "openRouterModelInfo", |
| 38 | + response.models[apiConfiguration.openRouterModelId], |
| 39 | + ) |
| 40 | + await controller.postStateToWebview() |
| 41 | + } |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + // 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. |
| 46 | + // We do this for all users since many users switch between api providers and if they were to switch back to openrouter it would be showing outdated model info if we hadn't retrieved the latest at this point |
| 47 | + // (see normalizeApiConfiguration > openrouter) |
| 48 | + // Prefetch marketplace and OpenRouter models |
| 49 | + |
| 50 | + // Send cached MCP marketplace catalog if available |
| 51 | + getGlobalState(controller.context, "mcpMarketplaceCatalog").then((mcpMarketplaceCatalog) => { |
| 52 | + if (mcpMarketplaceCatalog) { |
| 53 | + sendMcpMarketplaceCatalogEvent(mcpMarketplaceCatalog as McpMarketplaceCatalog) |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + // Silently refresh MCP marketplace catalog |
| 58 | + controller.silentlyRefreshMcpMarketplace() |
| 59 | + |
| 60 | + // Initialize telemetry service with user's current setting |
| 61 | + controller.getStateToPostToWebview().then((state) => { |
| 62 | + const { telemetrySetting } = state |
| 63 | + const isOptedIn = telemetrySetting !== "disabled" |
| 64 | + telemetryService.updateTelemetryState(isOptedIn) |
| 65 | + }) |
| 66 | + |
| 67 | + return Empty.create({}) |
| 68 | + } catch (error) { |
| 69 | + console.error("Failed to initialize webview:", error) |
| 70 | + // Return empty response even on error to not break the frontend |
| 71 | + return Empty.create({}) |
| 72 | + } |
| 73 | +} |
0 commit comments