From 940031917cc5e0c45f9c2e547e35fb8ff007364e Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Tue, 26 Aug 2025 12:58:47 -0700 Subject: [PATCH] feat: dynamic policies --- .../vscode/src/extension/VsCodeMessenger.ts | 4 + gui/src/redux/thunks/streamNormalInput.ts | 128 ++++++++++++++---- 2 files changed, 108 insertions(+), 24 deletions(-) diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 516e47983..b872b1746 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -254,6 +254,10 @@ export class VsCodeMessenger { editDecorationManager.clear(); }); + this.onWebview("tools/evaluatePolicy", async (msg) => { + return this.inProcessMessenger.invoke("tools/evaluatePolicy", msg.data); + }); + /** PASS THROUGH FROM WEBVIEW TO CORE AND BACK **/ WEBVIEW_TO_CORE_PASS_THROUGH.forEach((messageType) => { this.onWebview(messageType, async (msg) => { diff --git a/gui/src/redux/thunks/streamNormalInput.ts b/gui/src/redux/thunks/streamNormalInput.ts index 218883cb9..3ac768974 100644 --- a/gui/src/redux/thunks/streamNormalInput.ts +++ b/gui/src/redux/thunks/streamNormalInput.ts @@ -1,5 +1,10 @@ import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; -import { LLMFullCompletionOptions, ModelDescription, Tool } from "core"; +import { + LLMFullCompletionOptions, + ModelDescription, + Tool, + ToolPolicy, +} from "core"; import { getRuleId } from "core/llm/rules/getSystemMessageWithRules"; import { ToCoreProtocol } from "core/protocol"; import { BuiltInToolNames } from "core/tools/builtIn"; @@ -29,6 +34,58 @@ import { selectCurrentToolCalls } from "../selectors/selectToolCalls"; import { DEFAULT_TOOL_SETTING } from "../slices/uiSlice"; import { getBaseSystemMessage } from "../util/getBaseSystemMessage"; import { callToolById } from "./callToolById"; + +/** + * Evaluates the tool policy for a tool call, including dynamic policy evaluation + */ +async function evaluateToolPolicy( + toolCallState: any, + toolSettings: any, + activeTools: Tool[], + ideMessenger: any, +): Promise { + const basePolicy = + toolSettings[toolCallState.toolCall.function.name] ?? + activeTools.find( + (tool) => tool.function.name === toolCallState.toolCall.function.name, + )?.defaultToolPolicy ?? + DEFAULT_TOOL_SETTING; + + // Parse arguments for dynamic policy evaluation + let parsedArgs: Record = {}; + try { + parsedArgs = JSON.parse(toolCallState.toolCall.function.arguments); + } catch { + // If parsing fails, use empty object + } + + console.log("HERE HELLO WORLD"); + + let result; + try { + result = await ideMessenger.request("tools/evaluatePolicy", { + toolName: toolCallState.toolCall.function.name, + basePolicy, + args: parsedArgs, + }); + } catch (error) { + console.log("ERROR ZZZ", error); + // If request fails, return disabled + return "disabled"; + } + + // Evaluate the policy dynamically + + console.log("RESULT ZZZ", result); + + if (!result || result.status === "error") { + // If evaluation fails, treat as disabled + return "disabled"; + } + + return result.content.policy; +} + /** * Handles the execution of tool calls that may be automatically accepted. * Sets all tools as generated first, then executes auto-approved tool calls. @@ -37,7 +94,9 @@ async function handleToolCallExecution( dispatch: AppThunkDispatch, getState: () => RootState, activeTools: Tool[], + ideMessenger: any, ): Promise { + console.log("HANDLE TOOL CALL EXECUTION"); const newState = getState(); const toolSettings = newState.ui.toolSettings; const allToolCallStates = selectCurrentToolCalls(newState); @@ -52,16 +111,14 @@ async function handleToolCallExecution( return; } - // Check if ALL tool calls are auto-approved - if not, wait for user approval - const allAutoApproved = toolCallStates.every((toolCallState) => { - const toolPolicy = - toolSettings[toolCallState.toolCall.function.name] ?? - activeTools.find( - (tool) => tool.function.name === toolCallState.toolCall.function.name, - )?.defaultToolPolicy ?? - DEFAULT_TOOL_SETTING; - return toolPolicy == "allowedWithoutPermission"; - }); + // Check if ALL tool calls are auto-approved using dynamic evaluation + const policyPromises = toolCallStates.map((toolCallState) => + evaluateToolPolicy(toolCallState, toolSettings, activeTools, ideMessenger), + ); + const policies = await Promise.all(policyPromises); + const allAutoApproved = policies.every( + (policy) => policy === "allowedWithoutPermission", + ); // Set all tools as generated first toolCallStates.forEach((toolCallState) => { @@ -138,6 +195,7 @@ export const streamNormalInput = createAsyncThunk< >( "chat/streamNormalInput", async ({ legacySlashCommandData }, { dispatch, extra, getState }) => { + console.log("STREAMING ZZZZZZ ZZ"); const state = getState(); const selectedChatModel = selectSelectedChatModel(state); @@ -301,20 +359,37 @@ export const streamNormalInput = createAsyncThunk< (toolCallState) => toolCallState.status === "generating", ); - // Check if ALL generating tool calls are auto-approved - const allAutoApproved = - generatingToolCalls.length > 0 && - generatingToolCalls.every((toolCallState) => { - const toolPolicy = - toolSettings[toolCallState.toolCall.function.name] ?? - activeTools.find( - (tool) => - tool.function.name === toolCallState.toolCall.function.name, - )?.defaultToolPolicy ?? - DEFAULT_TOOL_SETTING; - return toolPolicy == "allowedWithoutPermission"; + // Check if ALL generating tool calls are auto-approved using dynamic evaluation + let allAutoApproved = false; + if (generatingToolCalls.length > 0) { + const policyPromises = generatingToolCalls.map((toolCallState) => + evaluateToolPolicy( + toolCallState, + toolSettings, + activeTools, + extra.ideMessenger, + ), + ); + const policies = await Promise.all(policyPromises); + + // Check if any are disabled and handle them + policies.forEach((policy, index) => { + if (policy === "disabled") { + // Mark disabled tools as generated but they won't be executed + dispatch( + setToolGenerated({ + toolCallId: generatingToolCalls[index].toolCallId, + tools: newState.config.config.tools, + }), + ); + } }); + allAutoApproved = policies.every( + (policy) => policy === "allowedWithoutPermission", + ); + } + // Only set inactive if: // 1. There are no tool calls, OR // 2. There are tool calls but they require manual approval @@ -323,6 +398,11 @@ export const streamNormalInput = createAsyncThunk< dispatch(setInactive()); } - await handleToolCallExecution(dispatch, getState, activeTools); + await handleToolCallExecution( + dispatch, + getState, + activeTools, + extra.ideMessenger, + ); }, );