-
+
+ Pending tool call
+
store.session.activeToolStreamId,
+ const currentToolCallApplyState = useAppSelector(
+ selectCurrentToolCallApplyState,
);
useWebviewListener(
"updateApplyState",
async (state) => {
dispatch(updateApplyState(state));
- const lastHistoryMsg = history.at(-1);
- const [streamId, toolCallId] = activeToolStreamId ?? [];
+
+ // Handle apply status updates that are associated with current tool call
if (
- toolCallId &&
state.status === "closed" &&
- lastHistoryMsg?.toolCallState?.toolCallId === toolCallId
+ currentToolCallApplyState &&
+ currentToolCallApplyState.streamId === state.streamId
) {
- if (state.streamId === streamId) {
- // const output: ContextItem = {
- // name: "Edit tool output",
- // content: "Completed edit",
- // description: "",
- // };
- dispatch(
- acceptToolCall({
- toolCallId,
- }),
- );
- // dispatch(setToolCallOutput([]));
- dispatch(
- streamResponseAfterToolCall({
- toolCallId,
- }),
- );
- }
+ // const output: ContextItem = {
+ // name: "Edit tool output",
+ // content: "Completed edit",
+ // description: "",
+ // };
+ dispatch(
+ acceptToolCall({
+ toolCallId: currentToolCallApplyState.toolCallId!,
+ }),
+ );
+ // dispatch(setToolCallOutput([]));
+ dispatch(
+ streamResponseAfterToolCall({
+ toolCallId: currentToolCallApplyState.toolCallId!,
+ }),
+ );
}
},
- [activeToolStreamId, history],
+ [currentToolCallApplyState, history],
);
}
diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx
index f8f3eee18..162cc0752 100644
--- a/gui/src/pages/gui/Chat.tsx
+++ b/gui/src/pages/gui/Chat.tsx
@@ -30,7 +30,10 @@ import { IdeMessengerContext } from "../../context/IdeMessenger";
import { useWebviewListener } from "../../hooks/useWebviewListener";
import { useAppDispatch, useAppSelector } from "../../redux/hooks";
import { selectUseHub } from "../../redux/selectors";
-import { selectCurrentToolCall } from "../../redux/selectors/selectCurrentToolCall";
+import {
+ selectCurrentToolCall,
+ selectCurrentToolCallApplyState,
+} from "../../redux/selectors/selectCurrentToolCall";
import { selectSelectedChatModel } from "../../redux/slices/configSlice";
import { submitEdit } from "../../redux/slices/editModeState";
import {
@@ -152,6 +155,10 @@ export function Chat() {
const { widget, highlights } = useFindWidget(stepsDivRef);
+ const currentToolCallApplyState = useAppSelector(
+ selectCurrentToolCallApplyState,
+ );
+
const sendInput = useCallback(
(
editorState: JSONContent,
@@ -164,6 +171,14 @@ export function Chat() {
"Cannot submit message while awaiting tool confirmation",
);
}
+ if (
+ currentToolCallApplyState &&
+ currentToolCallApplyState.status !== "closed"
+ ) {
+ return console.error(
+ "Cannot submit message while awaiting tool call apply",
+ );
+ }
if (selectedChatModel?.provider === "free-trial") {
const newCount = incrementFreeTrialCount();
@@ -352,6 +367,7 @@ export function Chat() {
toolCallState={item.toolCallState!}
toolCall={toolCall}
output={history[index + 1]?.contextItems}
+ historyIndex={index}
/>
);
diff --git a/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx b/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx
index 7e50b7305..a3e81355b 100644
--- a/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx
+++ b/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx
@@ -4,6 +4,7 @@ import StyledMarkdownPreview from "../../../components/StyledMarkdownPreview";
interface CreateFileToolCallProps {
relativeFilepath: string;
fileContents: string;
+ historyIndex: number;
}
export function CreateFile(props: CreateFileToolCallProps) {
@@ -14,6 +15,7 @@ export function CreateFile(props: CreateFileToolCallProps) {
isRenderingInStepContainer
disableManualApply
source={src}
+ itemIndex={props.historyIndex}
/>
) : null;
}
diff --git a/gui/src/pages/gui/ToolCallDiv/EditFile.tsx b/gui/src/pages/gui/ToolCallDiv/EditFile.tsx
index a298325b5..35b9d1a5f 100644
--- a/gui/src/pages/gui/ToolCallDiv/EditFile.tsx
+++ b/gui/src/pages/gui/ToolCallDiv/EditFile.tsx
@@ -1,26 +1,40 @@
import { getMarkdownLanguageTagForFile } from "core/util";
-import { useMemo } from "react";
+import { useEffect } from "react";
+import { v4 as uuidv4 } from "uuid";
import AcceptRejectAllButtons from "../../../components/AcceptRejectAllButtons";
import StyledMarkdownPreview from "../../../components/StyledMarkdownPreview";
-import { useAppSelector } from "../../../redux/hooks";
+import { useAppDispatch, useAppSelector } from "../../../redux/hooks";
+import {
+ selectApplyStateByToolCallId,
+ updateApplyState,
+} from "../../../redux/slices/sessionSlice";
type EditToolCallProps = {
relativeFilePath: string;
changes: string;
toolCallId?: string;
+ historyIndex: number;
};
export function EditFile(props: EditToolCallProps) {
const src = `\`\`\`${getMarkdownLanguageTagForFile(props.relativeFilePath ?? "test.txt")} ${props.relativeFilePath}\n${props.changes ?? ""}\n\`\`\``;
+ const dispatch = useAppDispatch();
const isStreaming = useAppSelector((state) => state.session.isStreaming);
- const states = useAppSelector((store) => store.session.codeBlockApplyStates);
-
- const applyState = useMemo(() => {
- return states.states.find(
- (state) => state.toolCallId && state.toolCallId === props.toolCallId,
- );
- }, [states, props.toolCallId]);
+ const applyState = useAppSelector((state) =>
+ selectApplyStateByToolCallId(state, props.toolCallId),
+ );
+ useEffect(() => {
+ if (!applyState) {
+ dispatch(
+ updateApplyState({
+ streamId: uuidv4(),
+ toolCallId: props.toolCallId,
+ status: "not-started",
+ }),
+ );
+ }
+ }, [applyState, props.toolCallId]);
if (!props.relativeFilePath) {
return null;
@@ -32,8 +46,9 @@ export function EditFile(props: EditToolCallProps) {
isRenderingInStepContainer
disableManualApply
source={src}
- singleCodeblockStreamId={applyState?.streamId}
+ forceStreamId={applyState?.streamId}
expandCodeblocks={false}
+ itemIndex={props.historyIndex}
/>
{/* TODO better indicator of generation at bottom */}
{/* {isStreaming && applyState?.status === "streaming" && (
diff --git a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx
index 02759dd18..516282d74 100644
--- a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx
+++ b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx
@@ -7,9 +7,11 @@ import { RunTerminalCommand } from "./RunTerminalCommand";
function FunctionSpecificToolCallDiv({
toolCall,
toolCallState,
+ historyIndex,
}: {
toolCall: ToolCallDelta;
toolCallState: ToolCallState;
+ historyIndex: number;
}) {
const args = toolCallState.parsedArgs;
@@ -19,6 +21,7 @@ function FunctionSpecificToolCallDiv({
);
case BuiltInToolNames.EditExistingFile:
@@ -27,6 +30,7 @@ function FunctionSpecificToolCallDiv({
relativeFilePath={args.filepath}
changes={args.changes}
toolCallId={toolCall.id}
+ historyIndex={historyIndex}
/>
);
case BuiltInToolNames.RunTerminalCommand:
diff --git a/gui/src/pages/gui/ToolCallDiv/index.tsx b/gui/src/pages/gui/ToolCallDiv/index.tsx
index 19bb13c02..5fd016d45 100644
--- a/gui/src/pages/gui/ToolCallDiv/index.tsx
+++ b/gui/src/pages/gui/ToolCallDiv/index.tsx
@@ -31,6 +31,7 @@ interface ToolCallDivProps {
toolCall: ToolCallDelta;
toolCallState: ToolCallState;
output?: ContextItemWithId[];
+ historyIndex: number;
}
const toolCallIcons: Record = {
@@ -100,6 +101,7 @@ export function ToolCallDiv(props: ToolCallDivProps) {
);
diff --git a/gui/src/redux/selectors/selectCurrentToolCall.ts b/gui/src/redux/selectors/selectCurrentToolCall.ts
index eb0bf669b..56c3a6cc1 100644
--- a/gui/src/redux/selectors/selectCurrentToolCall.ts
+++ b/gui/src/redux/selectors/selectCurrentToolCall.ts
@@ -8,3 +8,20 @@ export const selectCurrentToolCall = createSelector(
return findCurrentToolCall(history);
},
);
+
+export const selectCurrentToolCallApplyState = createSelector(
+ [
+ (store: RootState) => store.session.history,
+ (store: RootState) => store.session.codeBlockApplyStates,
+ ],
+ (history, applyStates) => {
+ const currentToolCall = findCurrentToolCall(history);
+ if (!currentToolCall) {
+ return undefined;
+ }
+ return applyStates.states.find(
+ (state) =>
+ state.toolCallId && state.toolCallId === currentToolCall.toolCallId,
+ );
+ },
+);
diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts
index 6adbabcf7..5f74e0c50 100644
--- a/gui/src/redux/slices/sessionSlice.ts
+++ b/gui/src/redux/slices/sessionSlice.ts
@@ -21,7 +21,6 @@ import {
ToolCallDelta,
ToolCallState,
} from "core";
-import { BuiltInToolNames } from "core/tools/builtIn";
import { NEW_SESSION_TITLE } from "core/util/constants";
import { incrementalParseJson } from "core/util/incrementalParseJson";
import { renderChatMessage } from "core/util/messageContent";
@@ -54,7 +53,6 @@ type SessionState = {
states: ApplyState[];
curIndex: number;
};
- activeToolStreamId?: [string, string];
newestToolbarPreviewForInput: Record;
};
@@ -357,7 +355,7 @@ export const sessionSlice = createSlice({
if (
!(
toolCallDelta.id &&
- toolCallDelta.function?.arguments &&
+ toolCallDelta.function?.arguments !== undefined &&
toolCallDelta.function?.name &&
toolCallDelta.type
)
@@ -368,19 +366,6 @@ export const sessionSlice = createSlice({
);
}
- if (
- toolCallDelta.id &&
- toolCallDelta.function?.name ===
- BuiltInToolNames.EditExistingFile
- ) {
- const streamId = uuidv4();
- state.codeBlockApplyStates.states.push({
- streamId,
- toolCallId: toolCallDelta.id,
- status: "streaming",
- });
- state.activeToolStreamId = [streamId, toolCallDelta.id];
- }
historyItem.toolCallState = toolCallDeltaToState(toolCallDelta);
}
state.history.push(historyItem);
@@ -660,9 +645,6 @@ export const sessionSlice = createSlice({
toolCallId: string;
}>,
) => {
- if (action.payload.toolCallId === state.activeToolStreamId?.[1]) {
- state.activeToolStreamId = undefined;
- }
const toolCallState = findToolCall(
state.history,
action.payload.toolCallId,
@@ -677,9 +659,6 @@ export const sessionSlice = createSlice({
toolCallId: string;
}>,
) => {
- if (action.payload.toolCallId === state.activeToolStreamId?.[1]) {
- state.activeToolStreamId = undefined;
- }
const toolCallState = findToolCall(
state.history,
action.payload.toolCallId,
@@ -694,9 +673,6 @@ export const sessionSlice = createSlice({
toolCallId: string;
}>,
) => {
- if (action.payload.toolCallId === state.activeToolStreamId?.[1]) {
- state.activeToolStreamId = undefined;
- }
const toolCallState = findToolCall(
state.history,
action.payload.toolCallId,
@@ -803,6 +779,16 @@ export const selectApplyStateByStreamId = createSelector(
},
);
+export const selectApplyStateByToolCallId = createSelector(
+ [
+ (state: RootState) => state.session.codeBlockApplyStates.states,
+ (state: RootState, toolCallId?: string) => toolCallId,
+ ],
+ (states, toolCallId) => {
+ return states.find((state) => state.toolCallId === toolCallId);
+ },
+);
+
export const {
updateFileSymbols,
setContextItemsAtIndex,
diff --git a/gui/src/redux/thunks/callCurrentTool.ts b/gui/src/redux/thunks/callCurrentTool.ts
index 61795de96..f6f408238 100644
--- a/gui/src/redux/thunks/callCurrentTool.ts
+++ b/gui/src/redux/thunks/callCurrentTool.ts
@@ -63,7 +63,11 @@ export const callCurrentTool = createAsyncThunk(
} = await callClientTool(toolCallState.toolCall, {
dispatch,
ideMessenger: extra.ideMessenger,
- activeToolStreamId: state.session.activeToolStreamId?.[0],
+ streamId: state.session.codeBlockApplyStates.states.find(
+ (state) =>
+ state.toolCallId && state.toolCallId === toolCallState.toolCallId,
+ )?.streamId,
+ getState,
});
output = clientToolOuput;
errorMessage = clientToolError;
diff --git a/gui/src/util/clientTools/callClientTool.ts b/gui/src/util/clientTools/callClientTool.ts
index 6d451fe7e..65f11fbf6 100644
--- a/gui/src/util/clientTools/callClientTool.ts
+++ b/gui/src/util/clientTools/callClientTool.ts
@@ -1,13 +1,14 @@
import { ContextItem, ToolCall } from "core";
import { BuiltInToolNames } from "core/tools/builtIn";
import { IIdeMessenger } from "../../context/IdeMessenger";
-import { AppThunkDispatch } from "../../redux/store";
+import { AppThunkDispatch, RootState } from "../../redux/store";
import { editToolImpl } from "./editImpl";
export interface ClientToolExtras {
+ getState: () => RootState;
dispatch: AppThunkDispatch;
ideMessenger: IIdeMessenger;
- activeToolStreamId?: string;
+ streamId?: string;
}
export interface ClientToolOutput {
diff --git a/gui/src/util/clientTools/editImpl.ts b/gui/src/util/clientTools/editImpl.ts
index 3e5271228..d1f80f25a 100644
--- a/gui/src/util/clientTools/editImpl.ts
+++ b/gui/src/util/clientTools/editImpl.ts
@@ -1,5 +1,4 @@
import { resolveRelativePathInDir } from "core/util/ideUtils";
-import { updateApplyState } from "../../redux/slices/sessionSlice";
import { ClientToolImpl } from "./callClientTool";
export const editToolImpl: ClientToolImpl = async (
@@ -7,7 +6,7 @@ export const editToolImpl: ClientToolImpl = async (
toolCallId,
extras,
) => {
- if (!extras.activeToolStreamId) {
+ if (!extras.streamId) {
throw new Error("Invalid apply state");
}
const firstUriMatch = await resolveRelativePathInDir(
@@ -18,7 +17,7 @@ export const editToolImpl: ClientToolImpl = async (
throw new Error(`${args.filepath} does not exist`);
}
const apply = await extras.ideMessenger.request("applyToFile", {
- streamId: extras.activeToolStreamId,
+ streamId: extras.streamId,
text: args.changes,
toolCallId,
filepath: firstUriMatch,
@@ -26,17 +25,7 @@ export const editToolImpl: ClientToolImpl = async (
if (apply.status === "error") {
throw new Error(apply.error);
}
- if (extras.activeToolStreamId) {
- extras.dispatch(
- updateApplyState({
- streamId: extras.activeToolStreamId,
- status: "closed",
- toolCallId,
- numDiffs: 0,
- filepath: args.filepath,
- }),
- );
- }
+
return {
respondImmediately: false,
output: undefined, // No immediate output.