This commit is contained in:
Nate 2024-11-24 17:59:31 -08:00
parent 79d9148322
commit 6ff5279b02
14 changed files with 96 additions and 102 deletions

1
core/index.d.ts vendored
View file

@ -403,6 +403,7 @@ interface ToolCallState {
toolCallId: string;
toolCall: ToolCall;
status: ToolStatus;
parsedArgs: any;
}
export interface ChatHistoryItem {

View file

@ -74,6 +74,9 @@ function constructSystemPrompt(model: string): string | null {
return null;
}
const CANCELED_TOOL_CALL_MESSAGE =
"This tool call was cancelled by the user. You should clarify next steps, as they don't wish for you to use this tool.";
export function constructMessages(
history: ChatHistory,
model: string,
@ -104,6 +107,12 @@ export function constructMessages(
...historyItem.message,
content,
});
} else if (historyItem.toolCallState?.status === "canceled") {
// Canceled tool call
msgs.push({
...historyItem.message,
content: CANCELED_TOOL_CALL_MESSAGE,
});
} else {
msgs.push(historyItem.message);
}

View file

@ -37,7 +37,7 @@ export default function ResponseActions({
}
return (
<div className="mx-2 flex h-7 cursor-default items-center justify-end space-x-1 bg-transparent pb-0 text-xs text-gray-400">
<div className="mx-2 flex cursor-default items-center justify-end space-x-1 bg-transparent pb-0 text-xs text-gray-400">
{shouldHideActions || (
<>
{isTruncated && (

View file

@ -35,8 +35,16 @@ export default function StepContainer(props: StepContainerProps) {
const isInEditMode = useSelector(
(store: RootState) => store.editModeState.isInEditMode,
);
const historyItemAfterThis = useSelector(
(store: RootState) => store.state.history[props.index + 1],
);
const uiConfig = useUIConfig();
const shouldHideActions = active && props.isLast;
const shouldHideActions =
(active && props.isLast) ||
historyItemAfterThis?.message.role === "assistant";
// const isStepAheadOfCurCheckpoint =
// isInEditMode && Math.floor(props.index / 2) > curCheckpointIndex;
@ -96,15 +104,17 @@ export default function StepContainer(props: StepContainerProps) {
)}
</ContentDiv>
{/* We want to occupy space in the DOM regardless of whether the actions are visible to avoid jank on */}
<div className={`transition-opacity duration-300 ease-in-out`}>
<ResponseActions
isTruncated={isTruncated}
onDelete={onDelete}
onContinueGeneration={onContinueGeneration}
index={props.index}
item={props.item}
shouldHideActions={shouldHideActions}
/>
<div className={`h-7 transition-opacity duration-300 ease-in-out`}>
{shouldHideActions ? null : (
<ResponseActions
isTruncated={isTruncated}
onDelete={onDelete}
onContinueGeneration={onContinueGeneration}
index={props.index}
item={props.item}
shouldHideActions={shouldHideActions}
/>
)}
</div>
</div>
);

View file

@ -1,4 +1,7 @@
import { ContextItemWithId, SymbolWithRange } from "core";
import { ctxItemToRifWithContents } from "core/commands/util";
import { memo, useEffect, useRef } from "react";
import { useSelector } from "react-redux";
import { useRemark } from "react-remark";
import rehypeHighlight, { Options } from "rehype-highlight";
import rehypeKatex from "rehype-katex";
@ -11,19 +14,16 @@ import {
vscEditorBackground,
vscForeground,
} from "..";
import { RootState } from "../../redux/store";
import { getFontSize, isJetBrains } from "../../util";
import FilenameLink from "./FilenameLink";
import "./katex.css";
import "./markdown.css";
import StepContainerPreActionButtons from "./StepContainerPreActionButtons";
import StepContainerPreToolbar from "./StepContainerPreToolbar";
import SymbolLink from "./SymbolLink";
import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre";
import { patchNestedMarkdown } from "./utils/patchNestedMarkdown";
import { RootState } from "../../redux/store";
import { ContextItemWithId, SymbolWithRange } from "core";
import SymbolLink from "./SymbolLink";
import { useSelector } from "react-redux";
import { ctxItemToRifWithContents } from "core/commands/util";
const StyledMarkdown = styled.div<{
fontSize?: number;

View file

@ -360,8 +360,8 @@ export function Chat() {
{item.message.toolCalls?.map((toolCall) => {
return (
<ToolCallDiv
toolCallState={item.toolCallState}
toolCall={toolCall as any}
status={item.toolCallState!.status}
/>
);
})}

View file

@ -1,16 +1,17 @@
import { getMarkdownLanguageTagForFile } from "core/util";
import StyledMarkdownPreview from "../../../components/markdown/StyledMarkdownPreview";
interface CreateFileToolCallProps {
filepath: string;
fileContents: string;
}
export function CreateFile(props: CreateFileToolCallProps) {
const src = `\`\`\`${getMarkdownLanguageTagForFile(props.filepath ?? "javascript")} ${props.filepath}\n${props.fileContents ?? ""}\n\`\`\``;
return (
<>
{props.fileContents}
{/* <StyledMarkdownPreview
isRenderingInStepContainer={true}
source={`\`\`\`${getMarkdownLanguageTagForFile(props.filepath ?? ".txt")} ${props.filepath}\n${props.fileContents ?? ""}\n\`\`\``}
/> */}
<StyledMarkdownPreview isRenderingInStepContainer={true} source={src} />
</>
);
}

View file

@ -1,10 +1,15 @@
import { ToolCall } from "core";
import { incrementalParseJson } from "core/util/incrementalParseJson";
import { ToolCall, ToolCallState } from "core";
import { CreateFile } from "./CreateFile";
import { RunTerminalCommand } from "./RunTerminalCommand";
function FunctionSpecificToolCallDiv({ toolCall }: { toolCall: ToolCall }) {
const [_, args] = incrementalParseJson(toolCall.function.arguments);
function FunctionSpecificToolCallDiv({
toolCall,
toolCallState,
}: {
toolCall: ToolCall;
toolCallState: ToolCallState;
}) {
const args = toolCallState.parsedArgs;
switch (toolCall.function.name) {
case "create_new_file":

View file

@ -1,4 +1,3 @@
import { useContext } from "react";
import { useSelector } from "react-redux";
import styled from "styled-components";
import {
@ -8,7 +7,6 @@ import {
vscButtonForeground,
} from "../../../components";
import Spinner from "../../../components/markdown/StepContainerPreToolbar/Spinner";
import { IdeMessengerContext } from "../../../context/IdeMessenger";
import { selectCurrentToolCall } from "../../../redux/selectors/selectCurrentToolCall";
import { cancelToolCall } from "../../../redux/slices/stateSlice";
import { useAppDispatch } from "../../../redux/store";
@ -56,21 +54,12 @@ export function ToolCallButtons(props: ToolCallButtonsProps) {
const dispatch = useAppDispatch();
const toolCallState = useSelector(selectCurrentToolCall);
const ideMessenger = useContext(IdeMessengerContext);
async function cancelTool() {
dispatch(cancelToolCall());
dispatch(
streamResponseAfterToolCall({
toolCallId: toolCallState.toolCallId,
toolOutput: [
{
name: "Cancelled",
description: "Cancelled",
content:
"This tool call was cancelled by the user. You should try something else, likely just chatting instead of using another tool.",
},
],
toolOutput: [],
}),
);
}

View file

@ -1,53 +1,37 @@
import { CheckIcon, PlayIcon, XMarkIcon } from "@heroicons/react/24/outline";
import { ToolCall, ToolStatus } from "core";
import { incrementalParseJson } from "core/util/incrementalParseJson";
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { lightGray } from "../../../components";
import { CheckIcon, XMarkIcon } from "@heroicons/react/24/outline";
import { ToolCall, ToolCallState, ToolStatus } from "core";
import Spinner from "../../../components/markdown/StepContainerPreToolbar/Spinner";
import { setGeneratedOutput } from "../../../redux/slices/stateSlice";
import FunctionSpecificToolCallDiv from "./FunctionSpecificToolCallDiv";
import { ThreadDiv } from "./ThreadDiv";
interface ToolCallDivProps {
toolCall: ToolCall;
status: ToolStatus;
toolCallState: ToolCallState;
}
export function ToolCallDiv(props: ToolCallDivProps) {
const dispatch = useDispatch();
useEffect(() => {
// Once the JSON can successfully parse, then set state to "generated"
if (props.toolCall.function.arguments.length === 0) {
dispatch(setGeneratedOutput(props.toolCall));
return;
}
const [done, j] = incrementalParseJson(props.toolCall.function.arguments);
if (done) {
dispatch(setGeneratedOutput(props.toolCall));
}
}, [props.toolCall.function.arguments]);
function getIcon(state: ToolStatus) {
switch (state) {
case "generating":
case "calling":
case "generated":
return <Spinner />;
case "done":
return <CheckIcon className="text-green-500" />;
case "calling":
return <Spinner />;
case "canceled":
return <XMarkIcon className="text-red-500" />;
case "generated":
return <PlayIcon color={lightGray} />;
}
}
return (
<ThreadDiv icon={getIcon(props.status)} toolCall={props.toolCall}>
<FunctionSpecificToolCallDiv toolCall={props.toolCall} />
<ThreadDiv
icon={getIcon(props.toolCallState.status)}
toolCall={props.toolCall}
>
<FunctionSpecificToolCallDiv
toolCall={props.toolCall}
toolCallState={props.toolCallState}
/>
</ThreadDiv>
);
}

View file

@ -19,6 +19,7 @@ import {
} from "core";
import { BrowserSerializedContinueConfig } from "core/config/load";
import { ConfigValidationError } from "core/config/validation";
import { incrementalParseJson } from "core/util/incrementalParseJson";
import { renderChatMessage } from "core/util/messageContent";
import { v4 as uuidv4, v4 } from "uuid";
import { streamResponseThunk } from "../thunks/streamResponse";
@ -319,10 +320,14 @@ export const stateSlice = createSlice({
};
if (action.payload.role === "assistant" && action.payload.toolCalls) {
const [_, parsedArgs] = incrementalParseJson(
action.payload.toolCalls[0].function.arguments,
);
historyItem.toolCallState = {
status: "generating",
toolCall: action.payload.toolCalls[0] as ToolCall,
toolCallId: action.payload.toolCalls[0].id,
parsedArgs,
};
}
@ -346,6 +351,14 @@ export const stateSlice = createSlice({
} else {
msg.toolCalls[i].function.arguments +=
toolCall.function.arguments;
const [_, parsedArgs] = incrementalParseJson(
msg.toolCalls[i].function.arguments,
);
state.history[
state.history.length - 1
].toolCallState.parsedArgs = parsedArgs;
}
});
}
@ -462,6 +475,12 @@ export const stateSlice = createSlice({
},
// Related to currentToolCallState
setToolGenerated: (state) => {
const toolCallState = findCurrentToolCall(state.history);
if (!toolCallState) return;
toolCallState.status = "generated";
},
cancelToolCall: (state) => {
const toolCallState = findCurrentToolCall(state.history);
if (!toolCallState) return;
@ -474,13 +493,6 @@ export const stateSlice = createSlice({
toolCallState.status = "done";
},
setGeneratedOutput: (state, { payload }: PayloadAction<ToolCall>) => {
const toolCallState = findCurrentToolCall(state.history);
if (!toolCallState) return;
toolCallState.toolCall = payload;
toolCallState.status = "generated";
},
setCalling: (state) => {
const toolCallState = findCurrentToolCall(state.history);
if (!toolCallState) return;
@ -572,8 +584,8 @@ export const {
setIndexingChatPeekHidden,
setCalling,
cancelToolCall,
setGeneratedOutput,
acceptToolCall,
setToolGenerated,
} = stateSlice.actions;
export default stateSlice.reducer;

View file

@ -1,6 +1,5 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall";
import { setCalling } from "../slices/stateSlice";
import { ThunkApiType } from "../store";
import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall";
@ -15,16 +14,6 @@ export const callTool = createAsyncThunk<void, undefined, ThunkApiType>(
return;
}
// If it goes "generated" -> "calling" -> "done" really quickly
// we don't want an abrupt flash so just skip "calling"
let setCallingState = true;
const timer = setTimeout(() => {
if (setCallingState) {
dispatch(setCalling());
}
}, 800);
if (toolCallState.status !== "generated") {
return;
}
@ -33,9 +22,6 @@ export const callTool = createAsyncThunk<void, undefined, ThunkApiType>(
toolCall: toolCallState.toolCall,
});
setCallingState = false;
clearTimeout(timer);
if (result.status === "success") {
const contextItems = result.content.contextItems;
// Send to the LLM to continue the conversation

View file

@ -7,6 +7,7 @@ import {
abortStream,
addPromptCompletionPair,
clearLastEmptyResponse,
setToolGenerated,
streamUpdate,
} from "../slices/stateSlice";
import { ThunkApiType } from "../store";
@ -64,13 +65,15 @@ export const streamNormalInput = createAsyncThunk<
// If it's a tool call that is automatically accepted, we should call it
const toolCallState = selectCurrentToolCall(getState());
if (
toolCallState &&
toolCallState.status === "generated" &&
toolSettings[toolCallState.toolCall.function.name] ===
if (toolCallState) {
dispatch(setToolGenerated());
if (
toolSettings[toolCallState.toolCall.function.name] ===
"allowedWithoutPermission"
) {
await dispatch(callTool());
) {
await dispatch(callTool());
}
}
} catch (e) {
// If there's an error, we should clear the response so there aren't two input boxes

View file

@ -59,13 +59,7 @@ export const streamResponseAfterToolCall = createAsyncThunk<
const updatedHistory = getState().state.history;
const messages = constructMessages(
[
...updatedHistory,
{
message: newMessage,
contextItems: [],
},
],
[...updatedHistory],
defaultModel.model,
);
await dispatch(streamNormalInput(messages));