feat: continue tool calls after rejection
This commit is contained in:
parent
bdb393abcc
commit
9e28c7a4eb
5 changed files with 74 additions and 3 deletions
|
|
@ -32,6 +32,7 @@ export const sharedConfigSchema = z
|
|||
displayRawMarkdown: z.boolean(),
|
||||
showChatScrollbar: z.boolean(),
|
||||
autoAcceptEditToolDiffs: z.boolean(),
|
||||
continueAfterToolRejection: z.boolean(),
|
||||
|
||||
// `tabAutocompleteOptions` in `ContinueConfig`
|
||||
useAutocompleteCache: z.boolean(),
|
||||
|
|
@ -158,6 +159,11 @@ export function modifyAnyConfigWithSharedConfig<
|
|||
configCopy.ui.showSessionTabs = sharedConfig.showSessionTabs;
|
||||
}
|
||||
|
||||
if (sharedConfig.continueAfterToolRejection !== undefined) {
|
||||
configCopy.ui.continueAfterToolRejection =
|
||||
sharedConfig.continueAfterToolRejection;
|
||||
}
|
||||
|
||||
configCopy.experimental = {
|
||||
...configCopy.experimental,
|
||||
};
|
||||
|
|
|
|||
1
core/index.d.ts
vendored
1
core/index.d.ts
vendored
|
|
@ -1343,6 +1343,7 @@ export interface ContinueUIConfig {
|
|||
codeWrap?: boolean;
|
||||
showSessionTabs?: boolean;
|
||||
autoAcceptEditToolDiffs?: boolean;
|
||||
continueAfterToolRejection?: boolean;
|
||||
}
|
||||
|
||||
export interface ContextMenuConfig {
|
||||
|
|
|
|||
|
|
@ -82,6 +82,8 @@ export function UserSettingsForm() {
|
|||
|
||||
// TODO defaults are in multiple places, should be consolidated and probably not explicit here
|
||||
const showSessionTabs = config.ui?.showSessionTabs ?? false;
|
||||
const continueAfterToolRejection =
|
||||
config.ui?.continueAfterToolRejection ?? false;
|
||||
const codeWrap = config.ui?.codeWrap ?? false;
|
||||
const showChatScrollbar = config.ui?.showChatScrollbar ?? false;
|
||||
const readResponseTTS = config.experimental?.readResponseTTS ?? false;
|
||||
|
|
@ -449,6 +451,16 @@ export function UserSettingsForm() {
|
|||
text="@Codebase: use tool calling only"
|
||||
/>
|
||||
|
||||
<ToggleSwitch
|
||||
isToggled={continueAfterToolRejection}
|
||||
onToggle={() =>
|
||||
handleUpdate({
|
||||
continueAfterToolRejection: !continueAfterToolRejection,
|
||||
})
|
||||
}
|
||||
text="Stream after tool rejection"
|
||||
/>
|
||||
|
||||
{hasContinueEmail && (
|
||||
<ContinueFeaturesMenu
|
||||
enableStaticContextualization={
|
||||
|
|
|
|||
|
|
@ -1,8 +1,19 @@
|
|||
import { createAsyncThunk } from "@reduxjs/toolkit";
|
||||
import { ChatMessage } from "core";
|
||||
import posthog from "posthog-js";
|
||||
import { cancelToolCall as cancelToolCallAction } from "../slices/sessionSlice";
|
||||
import {
|
||||
cancelToolCall as cancelToolCallAction,
|
||||
streamUpdate,
|
||||
updateToolCallOutput,
|
||||
} from "../slices/sessionSlice";
|
||||
import { ThunkApiType } from "../store";
|
||||
import { findToolCallById } from "../util";
|
||||
import { streamNormalInput } from "./streamNormalInput";
|
||||
|
||||
const DEFAULT_USER_REJECTION_MESSAGE = `The user skipped the tool call.
|
||||
If the tool call is optional or non-critical to the main goal, skip it and continue with the next step.
|
||||
If the tool call is essential, try an alternative approach.
|
||||
If no alternatives exist, offer to pause here.`;
|
||||
|
||||
export const cancelToolCallThunk = createAsyncThunk<
|
||||
void,
|
||||
|
|
@ -10,6 +21,8 @@ export const cancelToolCallThunk = createAsyncThunk<
|
|||
ThunkApiType
|
||||
>("chat/cancelToolCall", async ({ toolCallId }, { dispatch, getState }) => {
|
||||
const state = getState();
|
||||
const continueAfterToolRejection =
|
||||
state.config.config.ui?.continueAfterToolRejection ?? false;
|
||||
const toolCallState = findToolCallById(state.session.history, toolCallId);
|
||||
|
||||
if (toolCallState) {
|
||||
|
|
@ -21,6 +34,36 @@ export const cancelToolCallThunk = createAsyncThunk<
|
|||
});
|
||||
}
|
||||
|
||||
if (continueAfterToolRejection) {
|
||||
// Update tool call output with rejection message
|
||||
dispatch(
|
||||
updateToolCallOutput({
|
||||
toolCallId,
|
||||
contextItems: [
|
||||
{
|
||||
icon: "problems",
|
||||
name: "Tool Call Rejected",
|
||||
description: "User skipped the tool call",
|
||||
content: DEFAULT_USER_REJECTION_MESSAGE,
|
||||
hidden: false,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Dispatch the actual cancel action
|
||||
dispatch(cancelToolCallAction({ toolCallId }));
|
||||
|
||||
if (continueAfterToolRejection) {
|
||||
const rejectionMessage: ChatMessage = {
|
||||
role: "tool",
|
||||
content: "User skipped the tool call",
|
||||
toolCallId,
|
||||
};
|
||||
dispatch(streamUpdate([rejectionMessage]));
|
||||
|
||||
// Continue the conversation by triggering a new LLM response
|
||||
void dispatch(streamNormalInput({}));
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import { streamThunkWrapper } from "./streamThunkWrapper";
|
|||
*/
|
||||
function areAllToolsDoneStreaming(
|
||||
assistantMessage: ChatHistoryItemWithMessageId | undefined,
|
||||
continueAfterToolRejection: boolean,
|
||||
): boolean {
|
||||
// This might occur because of race conditions, if so, the tools are completed
|
||||
if (!assistantMessage?.toolCallStates) {
|
||||
|
|
@ -24,7 +25,10 @@ function areAllToolsDoneStreaming(
|
|||
|
||||
// Only continue if all tool calls are complete
|
||||
const completedToolCalls = assistantMessage.toolCallStates.filter(
|
||||
(tc) => tc.status === "done" || tc.status === "errored",
|
||||
(tc) =>
|
||||
tc.status === "done" ||
|
||||
tc.status === "errored" ||
|
||||
(continueAfterToolRejection && tc.status === "canceled"),
|
||||
);
|
||||
|
||||
return completedToolCalls.length === assistantMessage.toolCallStates.length;
|
||||
|
|
@ -71,7 +75,12 @@ export const streamResponseAfterToolCall = createAsyncThunk<
|
|||
item.toolCallStates?.some((tc) => tc.toolCallId === toolCallId),
|
||||
);
|
||||
|
||||
if (areAllToolsDoneStreaming(assistantMessage)) {
|
||||
if (
|
||||
areAllToolsDoneStreaming(
|
||||
assistantMessage,
|
||||
state.config.config.ui?.continueAfterToolRejection ?? false,
|
||||
)
|
||||
) {
|
||||
unwrapResult(await dispatch(streamNormalInput({})));
|
||||
}
|
||||
}),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue