|
|
@@ -4,9 +4,9 @@ import pWaitFor from "p-wait-for"
|
|
|
import * as vscode from "vscode"
|
|
|
|
|
|
import { ClineProvider } from "./ClineProvider"
|
|
|
-import { Language, ApiConfigMeta } from "../../schemas"
|
|
|
+import { Language, ApiConfigMeta, ProviderSettings } from "../../schemas"
|
|
|
import { changeLanguage, t } from "../../i18n"
|
|
|
-import { ApiConfiguration, RouterName, toRouterName } from "../../shared/api"
|
|
|
+import { RouterName, toRouterName } from "../../shared/api"
|
|
|
import { supportPrompt } from "../../shared/support-prompt"
|
|
|
|
|
|
import { checkoutDiffPayloadSchema, checkoutRestorePayloadSchema, WebviewMessage } from "../../shared/WebviewMessage"
|
|
|
@@ -89,19 +89,12 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
|
|
|
|
|
|
if (currentConfigName) {
|
|
|
if (!(await provider.providerSettingsManager.hasConfig(currentConfigName))) {
|
|
|
- // current config name not valid, get first config in list
|
|
|
- await updateGlobalState("currentApiConfigName", listApiConfig?.[0]?.name)
|
|
|
- if (listApiConfig?.[0]?.name) {
|
|
|
- const apiConfig = await provider.providerSettingsManager.loadConfig(
|
|
|
- listApiConfig?.[0]?.name,
|
|
|
- )
|
|
|
+ // Current config name not valid, get first config in list.
|
|
|
+ const name = listApiConfig[0]?.name
|
|
|
+ await updateGlobalState("currentApiConfigName", name)
|
|
|
|
|
|
- await Promise.all([
|
|
|
- updateGlobalState("listApiConfigMeta", listApiConfig),
|
|
|
- provider.postMessageToWebview({ type: "listApiConfig", listApiConfig }),
|
|
|
- provider.updateApiConfiguration(apiConfig),
|
|
|
- ])
|
|
|
- await provider.postStateToWebview()
|
|
|
+ if (name) {
|
|
|
+ await provider.activateProviderProfile({ name })
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
@@ -939,45 +932,39 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
|
|
|
const { apiConfiguration, customSupportPrompts, listApiConfigMeta, enhancementApiConfigId } =
|
|
|
await provider.getState()
|
|
|
|
|
|
- // Try to get enhancement config first, fall back to current config
|
|
|
- let configToUse: ApiConfiguration = apiConfiguration
|
|
|
- if (enhancementApiConfigId) {
|
|
|
- const config = listApiConfigMeta?.find((c: ApiConfigMeta) => c.id === enhancementApiConfigId)
|
|
|
- if (config?.name) {
|
|
|
- const loadedConfig = await provider.providerSettingsManager.loadConfig(config.name)
|
|
|
- if (loadedConfig.apiProvider) {
|
|
|
- configToUse = loadedConfig
|
|
|
- }
|
|
|
+ // Try to get enhancement config first, fall back to current config.
|
|
|
+ let configToUse: ProviderSettings = apiConfiguration
|
|
|
+
|
|
|
+ if (
|
|
|
+ enhancementApiConfigId &&
|
|
|
+ !!listApiConfigMeta.find((c: ApiConfigMeta) => c.id === enhancementApiConfigId)
|
|
|
+ ) {
|
|
|
+ const { name: _, ...providerSettings } = await provider.providerSettingsManager.getProfile({
|
|
|
+ id: enhancementApiConfigId,
|
|
|
+ })
|
|
|
+
|
|
|
+ if (providerSettings.apiProvider) {
|
|
|
+ configToUse = providerSettings
|
|
|
}
|
|
|
}
|
|
|
|
|
|
const enhancedPrompt = await singleCompletionHandler(
|
|
|
configToUse,
|
|
|
- supportPrompt.create(
|
|
|
- "ENHANCE",
|
|
|
- {
|
|
|
- userInput: message.text,
|
|
|
- },
|
|
|
- customSupportPrompts,
|
|
|
- ),
|
|
|
+ supportPrompt.create("ENHANCE", { userInput: message.text }, customSupportPrompts),
|
|
|
)
|
|
|
|
|
|
- // Capture telemetry for prompt enhancement
|
|
|
+ // Capture telemetry for prompt enhancement.
|
|
|
const currentCline = provider.getCurrentCline()
|
|
|
telemetryService.capturePromptEnhanced(currentCline?.taskId)
|
|
|
|
|
|
- await provider.postMessageToWebview({
|
|
|
- type: "enhancedPrompt",
|
|
|
- text: enhancedPrompt,
|
|
|
- })
|
|
|
+ await provider.postMessageToWebview({ type: "enhancedPrompt", text: enhancedPrompt })
|
|
|
} catch (error) {
|
|
|
provider.log(
|
|
|
`Error enhancing prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
|
|
|
)
|
|
|
+
|
|
|
vscode.window.showErrorMessage(t("common:errors.enhance_prompt"))
|
|
|
- await provider.postMessageToWebview({
|
|
|
- type: "enhancedPrompt",
|
|
|
- })
|
|
|
+ await provider.postMessageToWebview({ type: "enhancedPrompt" })
|
|
|
}
|
|
|
}
|
|
|
break
|
|
|
@@ -1096,30 +1083,23 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
|
|
|
break
|
|
|
}
|
|
|
|
|
|
- // Load the old configuration to get its ID
|
|
|
- const oldConfig = await provider.providerSettingsManager.loadConfig(oldName)
|
|
|
+ // Load the old configuration to get its ID.
|
|
|
+ const { id } = await provider.providerSettingsManager.getProfile({ name: oldName })
|
|
|
|
|
|
- // Create a new configuration with the same ID
|
|
|
- const newConfig = {
|
|
|
- ...message.apiConfiguration,
|
|
|
- id: oldConfig.id, // Preserve the ID
|
|
|
- }
|
|
|
+ // Create a new configuration with the new name and old ID.
|
|
|
+ await provider.providerSettingsManager.saveConfig(newName, { ...message.apiConfiguration, id })
|
|
|
|
|
|
- // Save with the new name but same ID
|
|
|
- await provider.providerSettingsManager.saveConfig(newName, newConfig)
|
|
|
+ // Delete the old configuration.
|
|
|
await provider.providerSettingsManager.deleteConfig(oldName)
|
|
|
|
|
|
- const listApiConfig = await provider.providerSettingsManager.listConfig()
|
|
|
-
|
|
|
- // Update listApiConfigMeta first to ensure UI has latest data
|
|
|
- await updateGlobalState("listApiConfigMeta", listApiConfig)
|
|
|
- await updateGlobalState("currentApiConfigName", newName)
|
|
|
-
|
|
|
- await provider.postStateToWebview()
|
|
|
+ // Re-activate to update the global settings related to the
|
|
|
+ // currently activated provider profile.
|
|
|
+ await provider.activateProviderProfile({ name: newName })
|
|
|
} catch (error) {
|
|
|
provider.log(
|
|
|
`Error rename api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
|
|
|
)
|
|
|
+
|
|
|
vscode.window.showErrorMessage(t("common:errors.rename_api_config"))
|
|
|
}
|
|
|
}
|
|
|
@@ -1127,16 +1107,7 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
|
|
|
case "loadApiConfiguration":
|
|
|
if (message.text) {
|
|
|
try {
|
|
|
- const apiConfig = await provider.providerSettingsManager.loadConfig(message.text)
|
|
|
- const listApiConfig = await provider.providerSettingsManager.listConfig()
|
|
|
-
|
|
|
- await Promise.all([
|
|
|
- updateGlobalState("listApiConfigMeta", listApiConfig),
|
|
|
- updateGlobalState("currentApiConfigName", message.text),
|
|
|
- provider.updateApiConfiguration(apiConfig),
|
|
|
- ])
|
|
|
-
|
|
|
- await provider.postStateToWebview()
|
|
|
+ await provider.activateProviderProfile({ name: message.text })
|
|
|
} catch (error) {
|
|
|
provider.log(
|
|
|
`Error load api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
|
|
|
@@ -1148,18 +1119,7 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
|
|
|
case "loadApiConfigurationById":
|
|
|
if (message.text) {
|
|
|
try {
|
|
|
- const { config: apiConfig, name } = await provider.providerSettingsManager.loadConfigById(
|
|
|
- message.text,
|
|
|
- )
|
|
|
- const listApiConfig = await provider.providerSettingsManager.listConfig()
|
|
|
-
|
|
|
- await Promise.all([
|
|
|
- updateGlobalState("listApiConfigMeta", listApiConfig),
|
|
|
- updateGlobalState("currentApiConfigName", name),
|
|
|
- provider.updateApiConfiguration(apiConfig),
|
|
|
- ])
|
|
|
-
|
|
|
- await provider.postStateToWebview()
|
|
|
+ await provider.activateProviderProfile({ id: message.text })
|
|
|
} catch (error) {
|
|
|
provider.log(
|
|
|
`Error load api configuration by ID: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
|
|
|
@@ -1180,29 +1140,25 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
|
|
|
break
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- await provider.providerSettingsManager.deleteConfig(message.text)
|
|
|
- const listApiConfig = await provider.providerSettingsManager.listConfig()
|
|
|
+ const oldName = message.text
|
|
|
|
|
|
- // Update listApiConfigMeta first to ensure UI has latest data
|
|
|
- await updateGlobalState("listApiConfigMeta", listApiConfig)
|
|
|
-
|
|
|
- // If this was the current config, switch to first available
|
|
|
- const currentApiConfigName = getGlobalState("currentApiConfigName")
|
|
|
+ const newName = (await provider.providerSettingsManager.listConfig()).filter(
|
|
|
+ (c) => c.name !== oldName,
|
|
|
+ )[0]?.name
|
|
|
|
|
|
- if (message.text === currentApiConfigName && listApiConfig?.[0]?.name) {
|
|
|
- const apiConfig = await provider.providerSettingsManager.loadConfig(listApiConfig[0].name)
|
|
|
- await Promise.all([
|
|
|
- updateGlobalState("currentApiConfigName", listApiConfig[0].name),
|
|
|
- provider.updateApiConfiguration(apiConfig),
|
|
|
- ])
|
|
|
- }
|
|
|
+ if (!newName) {
|
|
|
+ vscode.window.showErrorMessage(t("common:errors.delete_api_config"))
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- await provider.postStateToWebview()
|
|
|
+ try {
|
|
|
+ await provider.providerSettingsManager.deleteConfig(oldName)
|
|
|
+ await provider.activateProviderProfile({ name: newName })
|
|
|
} catch (error) {
|
|
|
provider.log(
|
|
|
`Error delete api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
|
|
|
)
|
|
|
+
|
|
|
vscode.window.showErrorMessage(t("common:errors.delete_api_config"))
|
|
|
}
|
|
|
}
|