feat: dynamic policies

This commit is contained in:
Tomasz Stefaniak 2025-08-26 12:58:47 -07:00
parent 9258d3de64
commit 940031917c
2 changed files with 108 additions and 24 deletions

View file

@ -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) => {

View file

@ -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<ToolPolicy> {
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<string, unknown> = {};
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<void> {
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,
);
},
);