Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/core/webview/webviewMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import pWaitFor from "p-wait-for"
import * as vscode from "vscode"
// kilocode_change start
import axios from "axios"
import { getKiloBaseUriFromToken } from "@roo-code/types"
import { getKiloBaseUriFromToken, isGlobalStateKey } from "@roo-code/types"
import {
MaybeTypedWebviewMessage,
ProfileData,
SeeNewChangesPayload,
TaskHistoryRequestPayload,
TasksByIdRequestPayload,
UpdateGlobalStateMessage,
} from "../../shared/WebviewMessage"
// kilocode_change end

Expand Down Expand Up @@ -85,7 +87,7 @@ import { fetchAndRefreshOrganizationModesOnStartup, refreshOrganizationModes } f

export const webviewMessageHandler = async (
provider: ClineProvider,
message: WebviewMessage,
message: MaybeTypedWebviewMessage, // kilocode_change switch to MaybeTypedWebviewMessage for better type-safety
marketplaceManager?: MarketplaceManager,
) => {
// Utility functions provided for concise get/update of global state via contextProxy API.
Expand Down Expand Up @@ -3499,6 +3501,16 @@ export const webviewMessageHandler = async (
break
}
// kilocode_change end
// kilocode_change start: Type-safe global state handler
case "updateGlobalState": {
const { stateKey, stateValue } = message as UpdateGlobalStateMessage
if (stateKey !== undefined && stateValue !== undefined && isGlobalStateKey(stateKey)) {
await updateGlobalState(stateKey, stateValue)
await provider.postStateToWebview()
}
break
}
// kilocode_change end: Type-safe global state handler
case "insertTextToChatArea":
provider.postMessageToWebview({ type: "insertTextToChatArea", text: message.text })
break
Expand Down
14 changes: 14 additions & 0 deletions src/shared/WebviewMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
// kilocode_change start
CommitRange,
HistoryItem,
GlobalState,
// kilocode_change end
} from "@roo-code/types"

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

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

// kilocode_change start: Type-safe global state update message
export type GlobalStateValue<K extends keyof GlobalState> = GlobalState[K]
export type UpdateGlobalStateMessage<K extends keyof GlobalState = keyof GlobalState> = {
type: "updateGlobalState"
stateKey: K
stateValue: GlobalStateValue<K>
}
// kilocode_change end: Type-safe global state update message

export interface WebviewMessage {
type:
| "updateTodoList"
Expand Down Expand Up @@ -267,6 +277,7 @@ export interface WebviewMessage {
| "dismissNotificationId" // kilocode_change
| "tasksByIdRequest" // kilocode_change
| "taskHistoryRequest" // kilocode_change
| "updateGlobalState" // kilocode_change
| "shareTaskSuccess"
| "exportMode"
| "exportModeResult"
Expand Down Expand Up @@ -386,6 +397,9 @@ export interface WebviewMessage {
}
}

// kilocode_change: Create discriminated union for type-safe messages
export type MaybeTypedWebviewMessage = WebviewMessage | UpdateGlobalStateMessage

// kilocode_change begin
export type OrganizationRole = "owner" | "admin" | "member"

Expand Down
10 changes: 10 additions & 0 deletions webview-ui/src/utils/globalStateHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { vscode } from "@src/utils/vscode"
import type { GlobalState } from "@roo-code/types"
import { GlobalStateValue } from "@roo/WebviewMessage"

/**
* Type-safe helper for sending global state updates from the WebView
*/
export function updateHostGlobalState<K extends keyof GlobalState>(stateKey: K, stateValue: GlobalStateValue<K>): void {
vscode.postMessage({ type: "updateGlobalState", stateKey, stateValue })
}
2 changes: 1 addition & 1 deletion webview-ui/src/utils/vscode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WebviewApi } from "vscode-webview"

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

/**
* A utility wrapper around the acquireVsCodeApi() function, which enables
Expand Down
Loading