From 8e984f2d98bfa7646d12ebbd39064150fad340fc Mon Sep 17 00:00:00 2001 From: celestial-vault <58194240+celestial-vault@users.noreply.github.com> Date: Thu, 31 Jul 2025 11:47:06 -0700 Subject: [PATCH] clean up getStateToPostToWebview in preparation for migration to StateManager service (#5266) --- src/core/controller/index.ts | 50 ++++++++++--------- .../controller/ui/onDidShowAnnouncement.ts | 4 +- src/core/storage/state.ts | 12 ++++- src/extension.ts | 4 ++ 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/core/controller/index.ts b/src/core/controller/index.ts index 82fa3551b5..c3a17decdf 100644 --- a/src/core/controller/index.ts +++ b/src/core/controller/index.ts @@ -35,6 +35,7 @@ import { handleGrpcRequest, handleGrpcRequestCancel } from "./grpc-handler" import { sendMcpMarketplaceCatalogEvent } from "./mcp/subscribeToMcpMarketplaceCatalog" import { sendStateUpdate } from "./state/subscribeToState" import { sendAddToInputEvent } from "./ui/subscribeToAddToInput" +import { getLatestAnnouncementId } from "@/extension" /* https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts @@ -53,9 +54,6 @@ export class Controller { mcpHub: McpHub accountService: ClineAccountService authService: AuthService - get latestAnnouncementId(): string { - return this.context.extension?.packageJSON?.version?.split(".").slice(0, 2).join(".") ?? "" - } constructor( readonly context: vscode.ExtensionContext, @@ -715,32 +713,38 @@ export class Controller { welcomeViewCompleted, mcpResponsesCollapsed, terminalOutputLineLimit, + localClineRulesToggles, + localWindsurfRulesToggles, + localCursorRulesToggles, + localWorkflowToggles, } = await getAllExtensionState(this.context) - const localClineRulesToggles = - ((await getWorkspaceState(this.context, "localClineRulesToggles")) as ClineRulesToggles) || {} + const currentTaskItem = this.task?.taskId ? (taskHistory || []).find((item) => item.id === this.task?.taskId) : undefined + const checkpointTrackerErrorMessage = this.task?.taskState.checkpointTrackerErrorMessage + const clineMessages = this.task?.messageStateHandler.getClineMessages() || [] - const localWindsurfRulesToggles = - ((await getWorkspaceState(this.context, "localWindsurfRulesToggles")) as ClineRulesToggles) || {} + const processedTaskHistory = (taskHistory || []) + .filter((item) => item.ts && item.task) + .sort((a, b) => b.ts - a.ts) + .slice(0, 100) // for now we're only getting the latest 100 tasks, but a better solution here is to only pass in 3 for recent task history, and then get the full task history on demand when going to the task history view (maybe with pagination?) - const localCursorRulesToggles = - ((await getWorkspaceState(this.context, "localCursorRulesToggles")) as ClineRulesToggles) || {} - - const localWorkflowToggles = ((await getWorkspaceState(this.context, "workflowToggles")) as ClineRulesToggles) || {} + const latestAnnouncementId = getLatestAnnouncementId(this.context) + const shouldShowAnnouncement = lastShownAnnouncementId !== latestAnnouncementId + const platform = process.platform as Platform + const distinctId = telemetryService.distinctId + const version = this.context.extension?.packageJSON?.version ?? "" + const uriScheme = vscode.env.uriScheme return { - version: this.context.extension?.packageJSON?.version ?? "", + version, apiConfiguration, - uriScheme: vscode.env.uriScheme, - currentTaskItem: this.task?.taskId ? (taskHistory || []).find((item) => item.id === this.task?.taskId) : undefined, - checkpointTrackerErrorMessage: this.task?.taskState.checkpointTrackerErrorMessage, - clineMessages: this.task?.messageStateHandler.getClineMessages() || [], - taskHistory: (taskHistory || []) - .filter((item) => item.ts && item.task) - .sort((a, b) => b.ts - a.ts) - .slice(0, 100), // for now we're only getting the latest 100 tasks, but a better solution here is to only pass in 3 for recent task history, and then get the full task history on demand when going to the task history view (maybe with pagination?) - shouldShowAnnouncement: lastShownAnnouncementId !== this.latestAnnouncementId, - platform: process.platform as Platform, + uriScheme, + currentTaskItem, + checkpointTrackerErrorMessage, + clineMessages, + taskHistory: processedTaskHistory, + shouldShowAnnouncement, + platform, autoApprovalSettings, browserSettings, preferredLanguage, @@ -752,7 +756,7 @@ export class Controller { telemetrySetting, planActSeparateModelsSetting, enableCheckpointsSetting: enableCheckpointsSetting ?? true, - distinctId: telemetryService.distinctId, + distinctId, globalClineRulesToggles: globalClineRulesToggles || {}, localClineRulesToggles: localClineRulesToggles || {}, localWindsurfRulesToggles: localWindsurfRulesToggles || {}, diff --git a/src/core/controller/ui/onDidShowAnnouncement.ts b/src/core/controller/ui/onDidShowAnnouncement.ts index 271de4943e..c9cec4a001 100644 --- a/src/core/controller/ui/onDidShowAnnouncement.ts +++ b/src/core/controller/ui/onDidShowAnnouncement.ts @@ -2,6 +2,7 @@ import type { EmptyRequest } from "@shared/proto/cline/common" import { Boolean } from "@shared/proto/cline/common" import type { Controller } from "../index" import { updateGlobalState } from "../../storage/state" +import { getLatestAnnouncementId } from "@/extension" /** * Marks the current announcement as shown @@ -12,8 +13,9 @@ import { updateGlobalState } from "../../storage/state" */ export async function onDidShowAnnouncement(controller: Controller, _request: EmptyRequest): Promise { try { + const latestAnnouncementId = getLatestAnnouncementId(controller.context) // Update the lastShownAnnouncementId to the current latestAnnouncementId - await updateGlobalState(controller.context, "lastShownAnnouncementId", controller.latestAnnouncementId) + await updateGlobalState(controller.context, "lastShownAnnouncementId", latestAnnouncementId) return Boolean.create({ value: false }) } catch (error) { console.error("Failed to acknowledge announcement:", error) diff --git a/src/core/storage/state.ts b/src/core/storage/state.ts index 90d70120d3..3098c3b547 100644 --- a/src/core/storage/state.ts +++ b/src/core/storage/state.ts @@ -271,7 +271,12 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { getSecret(context, "huaweiCloudMaasApiKey") as Promise, ]) - const localClineRulesToggles = (await getWorkspaceState(context, "localClineRulesToggles")) as ClineRulesToggles + const [localClineRulesToggles, localWindsurfRulesToggles, localCursorRulesToggles, localWorkflowToggles] = await Promise.all([ + getWorkspaceState(context, "localClineRulesToggles") as Promise, + getWorkspaceState(context, "localWindsurfRulesToggles") as Promise, + getWorkspaceState(context, "localCursorRulesToggles") as Promise, + getWorkspaceState(context, "workflowToggles") as Promise, + ]) const [ preferredLanguage, @@ -550,7 +555,6 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { taskHistory, autoApprovalSettings: autoApprovalSettings || DEFAULT_AUTO_APPROVAL_SETTINGS, // default value can be 0 or empty string globalClineRulesToggles: globalClineRulesToggles || {}, - localClineRulesToggles: localClineRulesToggles || {}, browserSettings: { ...DEFAULT_BROWSER_SETTINGS, ...browserSettings }, // this will ensure that older versions of browserSettings (e.g. before remoteBrowserEnabled was added) are merged with the default values (false for remoteBrowserEnabled) preferredLanguage: preferredLanguage || "English", openaiReasoningEffort: (openaiReasoningEffort as OpenaiReasoningEffort) || "medium", @@ -567,6 +571,10 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { terminalOutputLineLimit: terminalOutputLineLimit ?? 500, defaultTerminalProfile: defaultTerminalProfile ?? "default", globalWorkflowToggles: globalWorkflowToggles || {}, + localClineRulesToggles: localClineRulesToggles || {}, + localWindsurfRulesToggles: localWindsurfRulesToggles || {}, + localCursorRulesToggles: localCursorRulesToggles || {}, + localWorkflowToggles: localWorkflowToggles || {}, } } diff --git a/src/extension.ts b/src/extension.ts index b92cdee182..2ffe7b6f0a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -679,6 +679,10 @@ export async function activate(context: vscode.ExtensionContext) { return createClineAPI(sidebarWebview.controller) } +export function getLatestAnnouncementId(context: vscode.ExtensionContext) { + return context.extension?.packageJSON?.version?.split(".").slice(0, 2).join(".") ?? "" +} + function maybeSetupHostProviders(context: ExtensionContext) { if (!HostProvider.isInitialized()) { console.log("Setting up vscode host providers...")