Skip to content

Commit 03b6cd1

Browse files
authored
Introduce type-safe message type for global state updates (#3115)
* feat(shared): add type-safe global state update handling Introduce type-safe utilities for global state updates across the extension. This includes new types in GlobalStateTypes.ts, updated message handling in webviewMessageHandler.ts and WebviewMessage.ts, and helper functions in globalStateHelpers.ts with comprehensive tests. Ensures compile-time safety for state key-value pairs and prevents runtime errors from invalid state updates. * Remove lastViewedReleaseVersion for now * Remove lastViewedReleaseVersion for now
1 parent 81bd30f commit 03b6cd1

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/core/webview/webviewMessageHandler.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import pWaitFor from "p-wait-for"
66
import * as vscode from "vscode"
77
// kilocode_change start
88
import axios from "axios"
9-
import { getKiloBaseUriFromToken } from "@roo-code/types"
9+
import { getKiloBaseUriFromToken, isGlobalStateKey } from "@roo-code/types"
1010
import {
11+
MaybeTypedWebviewMessage,
1112
ProfileData,
1213
SeeNewChangesPayload,
1314
TaskHistoryRequestPayload,
1415
TasksByIdRequestPayload,
16+
UpdateGlobalStateMessage,
1517
} from "../../shared/WebviewMessage"
1618
// kilocode_change end
1719

@@ -85,7 +87,7 @@ import { fetchAndRefreshOrganizationModesOnStartup, refreshOrganizationModes } f
8587

8688
export const webviewMessageHandler = async (
8789
provider: ClineProvider,
88-
message: WebviewMessage,
90+
message: MaybeTypedWebviewMessage, // kilocode_change switch to MaybeTypedWebviewMessage for better type-safety
8991
marketplaceManager?: MarketplaceManager,
9092
) => {
9193
// Utility functions provided for concise get/update of global state via contextProxy API.
@@ -3505,6 +3507,16 @@ export const webviewMessageHandler = async (
35053507
break
35063508
}
35073509
// kilocode_change end
3510+
// kilocode_change start: Type-safe global state handler
3511+
case "updateGlobalState": {
3512+
const { stateKey, stateValue } = message as UpdateGlobalStateMessage
3513+
if (stateKey !== undefined && stateValue !== undefined && isGlobalStateKey(stateKey)) {
3514+
await updateGlobalState(stateKey, stateValue)
3515+
await provider.postStateToWebview()
3516+
}
3517+
break
3518+
}
3519+
// kilocode_change end: Type-safe global state handler
35083520
case "insertTextToChatArea":
35093521
provider.postMessageToWebview({ type: "insertTextToChatArea", text: message.text })
35103522
break

src/shared/WebviewMessage.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
// kilocode_change start
1313
CommitRange,
1414
HistoryItem,
15+
GlobalState,
1516
// kilocode_change end
1617
} from "@roo-code/types"
1718

@@ -34,6 +35,15 @@ export interface UpdateTodoListPayload {
3435

3536
export type EditQueuedMessagePayload = Pick<QueuedMessage, "id" | "text" | "images">
3637

38+
// kilocode_change start: Type-safe global state update message
39+
export type GlobalStateValue<K extends keyof GlobalState> = GlobalState[K]
40+
export type UpdateGlobalStateMessage<K extends keyof GlobalState = keyof GlobalState> = {
41+
type: "updateGlobalState"
42+
stateKey: K
43+
stateValue: GlobalStateValue<K>
44+
}
45+
// kilocode_change end: Type-safe global state update message
46+
3747
export interface WebviewMessage {
3848
type:
3949
| "updateTodoList"
@@ -268,6 +278,7 @@ export interface WebviewMessage {
268278
| "dismissNotificationId" // kilocode_change
269279
| "tasksByIdRequest" // kilocode_change
270280
| "taskHistoryRequest" // kilocode_change
281+
| "updateGlobalState" // kilocode_change
271282
| "shareTaskSuccess"
272283
| "exportMode"
273284
| "exportModeResult"
@@ -387,6 +398,9 @@ export interface WebviewMessage {
387398
}
388399
}
389400

401+
// kilocode_change: Create discriminated union for type-safe messages
402+
export type MaybeTypedWebviewMessage = WebviewMessage | UpdateGlobalStateMessage
403+
390404
// kilocode_change begin
391405
export type OrganizationRole = "owner" | "admin" | "member"
392406

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { vscode } from "@src/utils/vscode"
2+
import type { GlobalState } from "@roo-code/types"
3+
import { GlobalStateValue } from "@roo/WebviewMessage"
4+
5+
/**
6+
* Type-safe helper for sending global state updates from the WebView
7+
*/
8+
export function updateHostGlobalState<K extends keyof GlobalState>(stateKey: K, stateValue: GlobalStateValue<K>): void {
9+
vscode.postMessage({ type: "updateGlobalState", stateKey, stateValue })
10+
}

webview-ui/src/utils/vscode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { WebviewApi } from "vscode-webview"
22

3-
import { WebviewMessage } from "@roo/WebviewMessage"
3+
import { MaybeTypedWebviewMessage as WebviewMessage } from "@roo/WebviewMessage" // kilocode_change - using MaybeTypedWebviewMessage
44

55
/**
66
* A utility wrapper around the acquireVsCodeApi() function, which enables

0 commit comments

Comments
 (0)