This commit is contained in:
shengsuan-cloud committed 2025-11-12 17:51:03 +08:00
commit 2a5c74009c
1045 files changed
+107194 -22024

No files matched your search

+62 -86
View File
@@ -1,14 +1,14 @@
import { McpMarketplaceCatalog } from "@shared/mcp"
import { Empty, EmptyRequest } from "@shared/proto/cline/common"
import { OpenRouterCompatibleModelInfo } from "@shared/proto/cline/models"
// import { featureFlagsService } from "@/services/feature-flags"
import { readMcpMarketplaceCatalogFromCache } from "@/core/storage/disk"
// import { telemetryService } from "@/services/telemetry"
import { GlobalStateAndSettings } from "@/shared/storage/state-keys"
import type { Controller } from "../index"
import { sendMcpMarketplaceCatalogEvent } from "../mcp/subscribeToMcpMarketplaceCatalog"
import { refreshBasetenModels } from "../models/refreshBasetenModels"
import { refreshGroqModels } from "../models/refreshGroqModels"
import { refreshHicapModels } from "../models/refreshHicapModels"
import { refreshOpenRouterModels } from "../models/refreshOpenRouterModels"
import { refreshVercelAiGatewayModels } from "../models/refreshVercelAiGatewayModels"
import { sendOpenRouterModelsEvent } from "../models/subscribeToOpenRouterModels"
/**
@@ -19,20 +19,19 @@ import { sendOpenRouterModelsEvent } from "../models/subscribeToOpenRouterModels
*/
export async function initializeWebview(controller: Controller, _request: EmptyRequest): Promise<Empty> {
try {
// Post last cached models in case the call to endpoint fails
controller.readOpenRouterModels().then((openRouterModels) => {
if (openRouterModels) {
sendOpenRouterModelsEvent(OpenRouterCompatibleModelInfo.create({ models: openRouterModels }))
}
})
// Post last cached models as soon as possible for immediate availability in the UI
const lastCachedModels = await controller.readOpenRouterModels()
if (lastCachedModels) {
sendOpenRouterModelsEvent(OpenRouterCompatibleModelInfo.create({ models: lastCachedModels }))
}
// Refresh OpenRouter models from API
refreshOpenRouterModels(controller, EmptyRequest.create()).then(async (response) => {
if (response && response.models) {
refreshOpenRouterModels(controller).then(async (models) => {
if (models && Object.keys(models).length > 0) {
// 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)
const apiConfiguration = controller.stateManager.getApiConfiguration()
const planActSeparateModelsSetting = controller.stateManager.getGlobalStateKey("planActSeparateModelsSetting")
const currentMode = await controller.getCurrentMode()
const planActSeparateModelsSetting = controller.stateManager.getGlobalSettingsKey("planActSeparateModelsSetting")
const currentMode = controller.stateManager.getGlobalSettingsKey("mode")
if (planActSeparateModelsSetting) {
// Separate models: update only current mode
@@ -40,45 +39,41 @@ export async function initializeWebview(controller: Controller, _request: EmptyR
const modelInfoField = currentMode === "plan" ? "planModeOpenRouterModelInfo" : "actModeOpenRouterModelInfo"
const modelId = apiConfiguration[modelIdField]
if (modelId && response.models[modelId]) {
const updatedConfig = {
...apiConfiguration,
[modelInfoField]: response.models[modelId],
}
controller.stateManager.setApiConfiguration(updatedConfig)
if (modelId && models[modelId]) {
controller.stateManager.setGlobalState(modelInfoField, models[modelId])
await controller.postStateToWebview()
}
} else {
// Shared models: update both plan and act modes
const planModelId = apiConfiguration.planModeOpenRouterModelId
const actModelId = apiConfiguration.actModeOpenRouterModelId
const updatedConfig = { ...apiConfiguration }
const updates: Partial<GlobalStateAndSettings> = {}
// Update plan mode model info if we have a model ID
if (planModelId && response.models[planModelId]) {
updatedConfig.planModeOpenRouterModelInfo = response.models[planModelId]
if (planModelId && models[planModelId]) {
updates.planModeOpenRouterModelInfo = models[planModelId]
}
// Update act mode model info if we have a model ID
if (actModelId && response.models[actModelId]) {
updatedConfig.actModeOpenRouterModelInfo = response.models[actModelId]
if (actModelId && models[actModelId]) {
updates.actModeOpenRouterModelInfo = models[actModelId]
}
// Post state update if we updated any model info
if ((planModelId && response.models[planModelId]) || (actModelId && response.models[actModelId])) {
controller.stateManager.setApiConfiguration(updatedConfig)
if (Object.keys(updates).length > 0) {
controller.stateManager.setGlobalStateBatch(updates)
await controller.postStateToWebview()
}
}
}
})
refreshGroqModels(controller, EmptyRequest.create()).then(async (response) => {
if (response && response.models) {
refreshGroqModels(controller).then(async (models) => {
if (models && Object.keys(models).length > 0) {
// Update model info in state for Groq (this needs to be done here since we don't want to update state while settings is open, and we may refresh models there)
const apiConfiguration = controller.stateManager.getApiConfiguration()
const planActSeparateModelsSetting = controller.stateManager.getGlobalStateKey("planActSeparateModelsSetting")
const currentMode = await controller.getCurrentMode()
const planActSeparateModelsSetting = controller.stateManager.getGlobalSettingsKey("planActSeparateModelsSetting")
const currentMode = controller.stateManager.getGlobalSettingsKey("mode")
if (planActSeparateModelsSetting) {
// Separate models: update only current mode
@@ -86,46 +81,42 @@ export async function initializeWebview(controller: Controller, _request: EmptyR
const modelInfoField = currentMode === "plan" ? "planModeGroqModelInfo" : "actModeGroqModelInfo"
const modelId = apiConfiguration[modelIdField]
if (modelId && response.models[modelId]) {
const updatedConfig = {
...apiConfiguration,
[modelInfoField]: response.models[modelId],
}
controller.stateManager.setApiConfiguration(updatedConfig)
if (modelId && models[modelId]) {
controller.stateManager.setGlobalState(modelInfoField, models[modelId])
await controller.postStateToWebview()
}
} else {
// Shared models: update both plan and act modes
const planModelId = apiConfiguration.planModeGroqModelId
const actModelId = apiConfiguration.actModeGroqModelId
const updatedConfig = { ...apiConfiguration }
const updates: Partial<GlobalStateAndSettings> = {}
// Update plan mode model info if we have a model ID
if (planModelId && response.models[planModelId]) {
updatedConfig.planModeGroqModelInfo = response.models[planModelId]
if (planModelId && models[planModelId]) {
updates.planModeGroqModelInfo = models[planModelId]
}
// Update act mode model info if we have a model ID
if (actModelId && response.models[actModelId]) {
updatedConfig.actModeGroqModelInfo = response.models[actModelId]
if (actModelId && models[actModelId]) {
updates.actModeGroqModelInfo = models[actModelId]
}
// Post state update if we updated any model info
if ((planModelId && response.models[planModelId]) || (actModelId && response.models[actModelId])) {
controller.stateManager.setApiConfiguration(updatedConfig)
if (Object.keys(updates).length > 0) {
controller.stateManager.setGlobalStateBatch(updates)
await controller.postStateToWebview()
}
}
}
})
refreshBasetenModels(controller, EmptyRequest.create()).then(async (response) => {
if (response && response.models) {
refreshBasetenModels(controller).then(async (models) => {
if (models && Object.keys(models).length > 0) {
// Update model info in state for Baseten (this needs to be done here since we don't want to update state while settings is open, and we may refresh models there)
const apiConfiguration = controller.stateManager.getApiConfiguration()
const planActSeparateModelsSetting = controller.stateManager.getGlobalStateKey("planActSeparateModelsSetting")
const planActSeparateModelsSetting = controller.stateManager.getGlobalSettingsKey("planActSeparateModelsSetting")
const currentMode = await controller.getCurrentMode()
const currentMode = controller.stateManager.getGlobalSettingsKey("mode")
if (planActSeparateModelsSetting) {
// Separate models: update only current mode
@@ -133,8 +124,8 @@ export async function initializeWebview(controller: Controller, _request: EmptyR
const modelInfoField = currentMode === "plan" ? "planModeBasetenModelInfo" : "actModeBasetenModelInfo"
const modelId = apiConfiguration[modelIdField]
if (modelId && response.models[modelId]) {
controller.stateManager.setGlobalState(modelInfoField, response.models[modelId])
if (modelId && models[modelId]) {
controller.stateManager.setGlobalState(modelInfoField, models[modelId])
await controller.postStateToWebview()
}
} else {
@@ -143,66 +134,60 @@ export async function initializeWebview(controller: Controller, _request: EmptyR
const actModelId = apiConfiguration.actModeBasetenModelId
// Update plan mode model info if we have a model ID
if (planModelId && response.models[planModelId]) {
controller.stateManager.setGlobalState("planModeBasetenModelInfo", response.models[planModelId])
if (planModelId && models[planModelId]) {
controller.stateManager.setGlobalState("planModeBasetenModelInfo", models[planModelId])
}
// Update act mode model info if we have a model ID
if (actModelId && response.models[actModelId]) {
controller.stateManager.setGlobalState("actModeBasetenModelInfo", response.models[actModelId])
if (actModelId && models[actModelId]) {
controller.stateManager.setGlobalState("actModeBasetenModelInfo", models[actModelId])
}
// Post state update if we updated any model info
if ((planModelId && response.models[planModelId]) || (actModelId && response.models[actModelId])) {
if ((planModelId && models[planModelId]) || (actModelId && models[actModelId])) {
await controller.postStateToWebview()
}
}
}
})
// Refresh Vercel AI Gateway models from API
refreshVercelAiGatewayModels(controller, EmptyRequest.create()).then(async (response) => {
// Refresh Hicap models from API
refreshHicapModels(controller, EmptyRequest.create()).then(async (response) => {
if (response && response.models) {
// Update model info in state for Vercel AI Gateway (this needs to be done here since we don't want to update state while settings is open, and we may refresh models there)
// 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)
const apiConfiguration = controller.stateManager.getApiConfiguration()
const planActSeparateModelsSetting = controller.stateManager.getGlobalStateKey("planActSeparateModelsSetting")
const currentMode = await controller.getCurrentMode()
const planActSeparateModelsSetting = controller.stateManager.getGlobalSettingsKey("planActSeparateModelsSetting")
const currentMode = controller.stateManager.getGlobalSettingsKey("mode")
if (planActSeparateModelsSetting) {
// Separate models: update only current mode
const modelIdField =
currentMode === "plan" ? "planModeVercelAiGatewayModelId" : "actModeVercelAiGatewayModelId"
const modelInfoField =
currentMode === "plan" ? "planModeVercelAiGatewayModelInfo" : "actModeVercelAiGatewayModelInfo"
const modelIdField = currentMode === "plan" ? "planModeHicapModelId" : "actModeHicapModelId"
const modelInfoField = currentMode === "plan" ? "planModeHicapModelInfo" : "actModeHicapModelInfo"
const modelId = apiConfiguration[modelIdField]
if (modelId && response.models[modelId]) {
const updatedConfig = {
...apiConfiguration,
[modelInfoField]: response.models[modelId],
}
controller.stateManager.setApiConfiguration(updatedConfig)
controller.stateManager.setGlobalState(modelInfoField, response.models[modelId])
await controller.postStateToWebview()
}
} else {
// Shared models: update both plan and act modes
const planModelId = apiConfiguration.planModeVercelAiGatewayModelId
const actModelId = apiConfiguration.actModeVercelAiGatewayModelId
const updatedConfig = { ...apiConfiguration }
const planModelId = apiConfiguration.planModeHicapModelId
const actModelId = apiConfiguration.actModeHicapModelId
const updates: Partial<GlobalStateAndSettings> = {}
// Update plan mode model info if we have a model ID
if (planModelId && response.models[planModelId]) {
updatedConfig.planModeVercelAiGatewayModelInfo = response.models[planModelId]
updates.planModeHicapModelInfo = response.models[planModelId]
}
// Update act mode model info if we have a model ID
if (actModelId && response.models[actModelId]) {
updatedConfig.actModeVercelAiGatewayModelInfo = response.models[actModelId]
updates.actModeHicapModelInfo = response.models[actModelId]
}
// Post state update if we updated any model info
if ((planModelId && response.models[planModelId]) || (actModelId && response.models[actModelId])) {
controller.stateManager.setApiConfiguration(updatedConfig)
controller.stateManager.setGlobalStateBatch(updates)
await controller.postStateToWebview()
}
}
@@ -215,14 +200,14 @@ export async function initializeWebview(controller: Controller, _request: EmptyR
// Prefetch marketplace and OpenRouter models
// Send stored MCP marketplace catalog if available
const mcpMarketplaceCatalog = controller.stateManager.getGlobalStateKey("mcpMarketplaceCatalog")
const mcpMarketplaceCatalog = await readMcpMarketplaceCatalogFromCache()
if (mcpMarketplaceCatalog) {
sendMcpMarketplaceCatalogEvent(mcpMarketplaceCatalog as McpMarketplaceCatalog)
sendMcpMarketplaceCatalogEvent(mcpMarketplaceCatalog)
}
// Silently refresh MCP marketplace catalog
controller.silentlyRefreshMcpMarketplace()
controller.refreshMcpMarketplace(true /* sendCatalogEvent */)
// Initialize telemetry service with user's current setting
controller.getStateToPostToWebview().then((state) => {
@@ -231,15 +216,6 @@ export async function initializeWebview(controller: Controller, _request: EmptyR
// telemetryService.updateTelemetryState(isOptedIn)
})
// Refresh focus chain remote flag on webview init
// featureFlagsService
// .getFocusChainEnabled()
// .then(async (enabled: boolean) => {
// controller.stateManager.setGlobalState("focusChainFeatureFlagEnabled", enabled)
// await controller.postStateToWebview()
// })
// .catch((err: any) => console.error("Failed to refresh focus chain remote flag on webview init", err))
return Empty.create({})
} catch (error) {
console.error("Failed to initialize webview:", error)