From 1530cfe034b63a0dc489f39ac4b52e7c87245b60 Mon Sep 17 00:00:00 2001 From: Bee <68532117+abeatrix@users.noreply.github.com> Date: Thu, 11 Sep 2025 20:35:16 -0700 Subject: [PATCH] Register commands and view to use centralized ExtensionRegistryInfo (#6153) * Register commands and view to use centralized ExtensionRegistryInfo Replace hardcoded package.json imports and string literals with a centralized ExtensionRegistryInfo object that provides extension name and command definitions. This improves maintainability and ensures consistency across the codebase. * update view id * Fix environment variable serialization in vite config Wrap all process.env values with JSON.stringify to ensure proper string serialization and prevent undefined values from breaking the build configuration. --- src/core/controller/ui/openWalkthrough.ts | 7 +- src/extension.ts | 12 ++-- src/hosts/vscode/VscodeWebviewProvider.ts | 6 +- src/hosts/vscode/commandUtils.ts | 3 +- .../workspace/openClineSidebarPanel.ts | 4 +- src/registry.ts | 69 +++++++++++++------ src/services/test/TestServer.ts | 6 +- webview-ui/vite.config.ts | 14 ++-- 8 files changed, 75 insertions(+), 46 deletions(-) diff --git a/src/core/controller/ui/openWalkthrough.ts b/src/core/controller/ui/openWalkthrough.ts index 44c00948..26405ddb 100644 --- a/src/core/controller/ui/openWalkthrough.ts +++ b/src/core/controller/ui/openWalkthrough.ts @@ -1,8 +1,8 @@ import type { EmptyRequest } from "@shared/proto/cline/common" import { Empty } from "@shared/proto/cline/common" import * as vscode from "vscode" +import { ExtensionRegistryInfo } from "@/registry" import { telemetryService } from "@/services/telemetry" -import { name as pkgName } from "../../../../package.json" import type { Controller } from "../index" /** @@ -13,7 +13,10 @@ import type { Controller } from "../index" */ export async function openWalkthrough(_controller: Controller, _request: EmptyRequest): Promise { try { - await vscode.commands.executeCommand("workbench.action.openWalkthrough", `saoudrizwan.${pkgName}#ClineWalkthrough`) + await vscode.commands.executeCommand( + "workbench.action.openWalkthrough", + `saoudrizwan.${ExtensionRegistryInfo.name}#ClineWalkthrough`, + ) telemetryService.captureButtonClick("webview_openWalkthrough") return Empty.create({}) } catch (error) { diff --git a/src/extension.ts b/src/extension.ts index 1977f62d..530092d4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -35,7 +35,7 @@ import { focusChatInput, getContextForCommand } from "./hosts/vscode/commandUtil import { VscodeDiffViewProvider } from "./hosts/vscode/VscodeDiffViewProvider" import { VscodeWebviewProvider } from "./hosts/vscode/VscodeWebviewProvider" import { GitCommitGenerator } from "./integrations/git/commit-message-generator" -import { getClineCommands } from "./registry" +import { ExtensionRegistryInfo } from "./registry" import { AuthService } from "./services/auth/AuthService" import { telemetryService } from "./services/telemetry" import { SharedUriHandler } from "./services/uri/SharedUriHandler" @@ -71,7 +71,7 @@ export async function activate(context: vscode.ExtensionContext) { }), ) - const commands = getClineCommands(context.extension.packageJSON.name) + const { commands } = ExtensionRegistryInfo context.subscriptions.push( vscode.commands.registerCommand(commands.PlusButton, async (webview: any) => { @@ -339,7 +339,7 @@ export async function activate(context: vscode.ExtensionContext) { // Add to Cline (Always available) const addAction = new vscode.CodeAction("Add to Cline", vscode.CodeActionKind.QuickFix) addAction.command = { - command: "cline.addToChat", + command: commands.AddToChat, title: "Add to Cline", arguments: [expandedRange, context.diagnostics], } @@ -348,7 +348,7 @@ export async function activate(context: vscode.ExtensionContext) { // Explain with Cline (Always available) const explainAction = new vscode.CodeAction("Explain with Cline", vscode.CodeActionKind.RefactorExtract) // Using a refactor kind explainAction.command = { - command: "cline.explainCode", + command: commands.ExplainCode, title: "Explain with Cline", arguments: [expandedRange], } @@ -357,7 +357,7 @@ export async function activate(context: vscode.ExtensionContext) { // Improve with Cline (Always available) const improveAction = new vscode.CodeAction("Improve with Cline", vscode.CodeActionKind.RefactorRewrite) // Using a refactor kind improveAction.command = { - command: "cline.improveCode", + command: commands.ImproveCode, title: "Improve with Cline", arguments: [expandedRange], } @@ -368,7 +368,7 @@ export async function activate(context: vscode.ExtensionContext) { const fixAction = new vscode.CodeAction("Fix with Cline", vscode.CodeActionKind.QuickFix) fixAction.isPreferred = true fixAction.command = { - command: "cline.fixWithCline", + command: commands.FixWithCline, title: "Fix with Cline", arguments: [expandedRange, context.diagnostics], } diff --git a/src/hosts/vscode/VscodeWebviewProvider.ts b/src/hosts/vscode/VscodeWebviewProvider.ts index 89fcfe52..59f43ba1 100644 --- a/src/hosts/vscode/VscodeWebviewProvider.ts +++ b/src/hosts/vscode/VscodeWebviewProvider.ts @@ -4,10 +4,10 @@ import type { Uri } from "vscode" import * as vscode from "vscode" import { handleGrpcRequest, handleGrpcRequestCancel } from "@/core/controller/grpc-handler" import { HostProvider } from "@/hosts/host-provider" +import { ExtensionRegistryInfo } from "@/registry" import type { ExtensionMessage } from "@/shared/ExtensionMessage" import { WebviewMessage } from "@/shared/WebviewMessage" import type { WebviewProviderType } from "@/shared/webview/types" -import { name as pkgName } from "../../../package.json" /* https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts @@ -17,8 +17,8 @@ https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/c export class VscodeWebviewProvider extends WebviewProvider implements vscode.WebviewViewProvider { // Used in package.json as the view's id. This value cannot be changed due to how vscode caches // views based on their id, and updating the id would break existing instances of the extension. - public static readonly SIDEBAR_ID = `${pkgName}.SidebarProvider` - public static readonly TAB_PANEL_ID = `${pkgName}.TabPanelProvider` + public static readonly SIDEBAR_ID = ExtensionRegistryInfo.views.Sidebar + public static readonly TAB_PANEL_ID = ExtensionRegistryInfo.views.TabPanel private webview?: vscode.WebviewView | vscode.WebviewPanel private disposables: vscode.Disposable[] = [] diff --git a/src/hosts/vscode/commandUtils.ts b/src/hosts/vscode/commandUtils.ts index 60cb13e9..0c74e831 100644 --- a/src/hosts/vscode/commandUtils.ts +++ b/src/hosts/vscode/commandUtils.ts @@ -1,5 +1,6 @@ import pWaitFor from "p-wait-for" import * as vscode from "vscode" +import { ExtensionRegistryInfo } from "@/registry" import { CommandContext } from "@/shared/proto/index.cline" import { Controller } from "../../core/controller" import { WebviewProvider } from "../../core/webview" @@ -50,7 +51,7 @@ export async function getContextForCommand( } export async function focusChatInput(): Promise { - await vscode.commands.executeCommand("cline.focusChatInput") + await vscode.commands.executeCommand(ExtensionRegistryInfo.commands.FocusChatInput) // Wait for a webview instance to become available after focusing await pWaitFor(() => !!WebviewProvider.getLastActiveInstance()) diff --git a/src/hosts/vscode/hostbridge/workspace/openClineSidebarPanel.ts b/src/hosts/vscode/hostbridge/workspace/openClineSidebarPanel.ts index a36cb2ca..cc804c72 100644 --- a/src/hosts/vscode/hostbridge/workspace/openClineSidebarPanel.ts +++ b/src/hosts/vscode/hostbridge/workspace/openClineSidebarPanel.ts @@ -1,8 +1,8 @@ import * as vscode from "vscode" +import { ExtensionRegistryInfo } from "@/registry" import { OpenClineSidebarPanelRequest, OpenClineSidebarPanelResponse } from "@/shared/proto/index.host" -import { name as pkgName } from "../../../../../package.json" export async function openClineSidebarPanel(_: OpenClineSidebarPanelRequest): Promise { - await vscode.commands.executeCommand(`${pkgName}.SidebarProvider.focus`) + await vscode.commands.executeCommand(`${ExtensionRegistryInfo.views.Sidebar}.focus`) return {} } diff --git a/src/registry.ts b/src/registry.ts index d31f1fc4..51a311f5 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -1,25 +1,50 @@ +import { name, publisher, version } from "../package.json" + +const prefix = name === "claude-dev" ? "cline" : name + /** - * Generate and export command identifiers based on the extension name. - * This allows for flexibility if the extension is published with a different name. + * List of commands with the name of the extension they are registered under. + * These should match the command IDs defined in package.json. + * For Nightly build, the publish script has updated all the commands to use the extension name as prefix. + * In production, all commands are registered under "cline" for consistency. */ -export function getClineCommands(extensionName: string) { - const prefix = extensionName === "claude-dev" ? "cline" : extensionName - return { - PlusButton: prefix + ".plusButtonClicked", - McpButton: prefix + ".mcpButtonClicked", - PopoutButton: prefix + ".popoutButtonClicked", - OpenInNewTab: prefix + ".openInNewTab", - SettingsButton: prefix + ".settingsButtonClicked", - HistoryButton: prefix + ".historyButtonClicked", - AccountButton: prefix + ".accountButtonClicked", - TerminalOutput: prefix + ".addTerminalOutputToChat", - AddToChat: prefix + ".addToChat", - FixWithCline: prefix + ".fixWithCline", - ExplainCode: prefix + ".explainCode", - ImproveCode: prefix + ".improveCode", - FocusChatInput: prefix + ".focusChatInput", - Walkthrough: prefix + ".openWalkthrough", - GenerateCommit: prefix + ".generateGitCommitMessage", - AbortCommit: prefix + ".abortGitCommitMessage", - } +const ClineCommands = { + PlusButton: prefix + ".plusButtonClicked", + McpButton: prefix + ".mcpButtonClicked", + PopoutButton: prefix + ".popoutButtonClicked", + OpenInNewTab: prefix + ".openInNewTab", + SettingsButton: prefix + ".settingsButtonClicked", + HistoryButton: prefix + ".historyButtonClicked", + AccountButton: prefix + ".accountButtonClicked", + TerminalOutput: prefix + ".addTerminalOutputToChat", + AddToChat: prefix + ".addToChat", + FixWithCline: prefix + ".fixWithCline", + ExplainCode: prefix + ".explainCode", + ImproveCode: prefix + ".improveCode", + FocusChatInput: prefix + ".focusChatInput", + Walkthrough: prefix + ".openWalkthrough", + GenerateCommit: prefix + ".generateGitCommitMessage", + AbortCommit: prefix + ".abortGitCommitMessage", +} + +/** + * IDs for the views registered by the extension. + * These should match the name + view IDs defined in package.json. + */ +const ClineViewIds = { + Sidebar: name + ".SidebarProvider", + TabPanel: name + ".TabPanelProvider", +} + +/** + * The registry info for the extension, including its ID, name, version, commands, and views + * registered for the current host. + */ +export const ExtensionRegistryInfo = { + id: publisher + "." + name, + name, + version, + publisher, + commands: ClineCommands, + views: ClineViewIds, } diff --git a/src/services/test/TestServer.ts b/src/services/test/TestServer.ts index ecb12d87..561ca9b9 100644 --- a/src/services/test/TestServer.ts +++ b/src/services/test/TestServer.ts @@ -9,8 +9,8 @@ import * as http from "http" import * as path from "path" import * as vscode from "vscode" import { Controller } from "@/core/controller" +import { ExtensionRegistryInfo } from "@/registry" import { getCwd } from "@/utils/path" -import { name as pkgName } from "../../../package.json" import { calculateToolSuccessRate, getFileChanges, initializeGitRepository, validateWorkspacePath } from "./GitHelper" /** @@ -88,10 +88,10 @@ async function updateAutoApprovalSettings(_context: vscode.ExtensionContext, con export function createTestServer(controller: Controller): http.Server { // Try to show the Cline sidebar Logger.log("[createTestServer] Opening Cline in sidebar...") - vscode.commands.executeCommand(`workbench.view.${pkgName}-ActivityBar`) + vscode.commands.executeCommand(`workbench.view.${ExtensionRegistryInfo.name}-ActivityBar`) // Then ensure the webview is focused/loaded - vscode.commands.executeCommand(`${pkgName}.SidebarProvider.focus`) + vscode.commands.executeCommand(`${ExtensionRegistryInfo.views.Sidebar}.focus`) // Update auto approval settings is available updateAutoApprovalSettings(controller.context, controller) diff --git a/webview-ui/vite.config.ts b/webview-ui/vite.config.ts index bfb9f34b..6cee31d7 100644 --- a/webview-ui/vite.config.ts +++ b/webview-ui/vite.config.ts @@ -91,14 +91,14 @@ export default defineConfig({ __PLATFORM__: JSON.stringify(platform), process: JSON.stringify({ env: { - NODE_ENV: process?.env?.IS_DEV ? "development" : "production", - CLINE_ENVIRONMENT: process?.env?.CLINE_ENVIRONMENT ?? "production", - IS_DEV: process?.env?.IS_DEV === "true", - IS_TEST: process?.env?.IS_TEST === "true", - CI: process?.env?.CI === "true", + NODE_ENV: JSON.stringify(process?.env?.IS_DEV ? "development" : "production"), + CLINE_ENVIRONMENT: JSON.stringify(process?.env?.CLINE_ENVIRONMENT ?? "production"), + IS_DEV: JSON.stringify(process?.env?.IS_DEV), + IS_TEST: JSON.stringify(process?.env?.IS_TEST), + CI: JSON.stringify(process?.env?.CI), // PostHog environment variables - TELEMETRY_SERVICE_API_KEY: process?.env?.TELEMETRY_SERVICE_API_KEY, - ERROR_SERVICE_API_KEY: process?.env?.ERROR_SERVICE_API_KEY, + TELEMETRY_SERVICE_API_KEY: JSON.stringify(process?.env?.TELEMETRY_SERVICE_API_KEY), + ERROR_SERVICE_API_KEY: JSON.stringify(process?.env?.ERROR_SERVICE_API_KEY), }, }), },