some logic adjustments, but not entirely fixed yet

This commit is contained in:
Nate 2024-11-23 23:29:08 -08:00
parent 4916cdda2e
commit 942fd9fa4e
7 changed files with 34 additions and 36 deletions

View file

@ -27,7 +27,7 @@ import resolveEditorContent, {
} from "../components/mainInput/resolveInput";
import { IIdeMessenger } from "../context/IdeMessenger";
import { defaultModelSelector } from "../redux/selectors/modelSelectors";
import { selectLastToolCall } from "../redux/selectors/selectLastToolCall";
import { selectCurrentToolCall } from "../redux/selectors/selectCurrentToolCall";
import {
abortStream,
acceptToolCall,
@ -70,7 +70,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) {
const toolSettings = useSelector(
(store: RootState) => store.uiState.toolSettings,
);
const toolCallState = useSelector(selectLastToolCall);
const toolCallState = useSelector(selectCurrentToolCall);
const activeRef = useRef(active);
@ -121,14 +121,18 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) {
}
// If it's a tool call that is automatically accepted, we should call it
await new Promise((resolve) => setTimeout(resolve, 0));
if (
toolSettings[toolCallState?.toolCall.function.name] ===
"allowedWithoutPermission"
toolCallState &&
toolCallState.status === "generated" &&
toolSettings[toolCallState.toolCall.function.name] ===
"allowedWithoutPermission"
) {
await callTool();
}
} catch (e) {
debugger;
// If there's an error, we should clear the response so there aren't two input boxes
dispatch(clearLastEmptyResponse());
}
@ -419,6 +423,9 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) {
await handleErrors(async () => {
resetStateForNewMessage();
dispatch(acceptToolCall());
await new Promise((resolve) => setTimeout(resolve, 0));
const newMessage: ChatMessage = {
role: "tool",
content: renderContextItems(toolOutput),
@ -438,10 +445,8 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) {
})),
}),
);
dispatch(acceptToolCall());
activeRef.current = true;
dispatch(setActive());
await new Promise((resolve) => setTimeout(resolve, 0));
const messages = constructMessages(
@ -460,6 +465,10 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) {
async function callTool() {
console.log("calling tool", toolCallState.toolCall);
if (!toolCallState) {
return;
}
// If it goes "generated" -> "calling" -> "done" really quickly
// we don't want an abrupt flash so just skip "calling"
let setCallingState = true;

View file

@ -35,7 +35,7 @@ import useHistory from "../../hooks/useHistory";
import { useTutorialCard } from "../../hooks/useTutorialCard";
import { useWebviewListener } from "../../hooks/useWebviewListener";
import { defaultModelSelector } from "../../redux/selectors/modelSelectors";
import { selectLastToolCall } from "../../redux/selectors/selectLastToolCall";
import { selectCurrentToolCall } from "../../redux/selectors/selectCurrentToolCall";
import {
clearLastEmptyResponse,
newSession,
@ -134,7 +134,7 @@ export function Chat() {
useHistory(dispatch);
const toolCallState = useSelector<RootState, ToolCallState | undefined>(
selectLastToolCall,
selectCurrentToolCall,
);
const snapToBottom = useCallback(() => {

View file

@ -10,7 +10,7 @@ import {
import Spinner from "../../../components/markdown/StepContainerPreToolbar/Spinner";
import { IdeMessengerContext } from "../../../context/IdeMessenger";
import useChatHandler from "../../../hooks/useChatHandler";
import { selectLastToolCall } from "../../../redux/selectors/selectLastToolCall";
import { selectCurrentToolCall } from "../../../redux/selectors/selectCurrentToolCall";
import { cancelToolCall } from "../../../redux/slices/stateSlice";
const ButtonContainer = styled.div`
@ -52,7 +52,7 @@ interface ToolCallButtonsProps {}
export function ToolCallButtons(props: ToolCallButtonsProps) {
const dispatch = useDispatch();
const toolCallState = useSelector(selectLastToolCall);
const toolCallState = useSelector(selectCurrentToolCall);
const ideMessenger = useContext(IdeMessengerContext);
@ -73,6 +73,10 @@ export function ToolCallButtons(props: ToolCallButtonsProps) {
]);
}
if (!toolCallState) {
return null;
}
return (
<>
<ButtonContainer>

View file

@ -31,8 +31,6 @@ export function ToolCallDiv(props: ToolCallDivProps) {
}, [props.toolCall.function.arguments]);
function getIcon(state: ToolStatus) {
console.log("State: ", state);
switch (state) {
case "generating":
return <Spinner />;
@ -49,7 +47,6 @@ export function ToolCallDiv(props: ToolCallDivProps) {
return (
<ThreadDiv icon={getIcon(props.status)} toolCall={props.toolCall}>
{props.status}
<FunctionSpecificToolCallDiv toolCall={props.toolCall} />
</ThreadDiv>
);

View file

@ -1,10 +1,10 @@
import { createSelector } from "@reduxjs/toolkit";
import { RootState } from "../store";
import { findLastToolCall } from "../util";
import { findCurrentToolCall } from "../util";
export const selectLastToolCall = createSelector(
export const selectCurrentToolCall = createSelector(
(store: RootState) => store.state.history,
(history) => {
return findLastToolCall(history);
return findCurrentToolCall(history);
},
);

View file

@ -16,7 +16,7 @@ import { BrowserSerializedContinueConfig } from "core/config/load";
import { ConfigValidationError } from "core/config/validation";
import { renderChatMessage } from "core/util/messageContent";
import { v4 as uuidv4, v4 } from "uuid";
import { findLastToolCall } from "../util";
import { findCurrentToolCall } from "../util";
// We need this to handle reorderings (e.g. a mid-array deletion) of the messages array.
// The proper fix is adding a UUID to all chat messages, but this is the temp workaround.
@ -457,26 +457,26 @@ export const stateSlice = createSlice({
// Related to currentToolCallState
cancelToolCall: (state) => {
const toolCallState = findLastToolCall(state.history);
const toolCallState = findCurrentToolCall(state.history);
if (!toolCallState) return;
toolCallState.status = "canceled";
},
acceptToolCall: (state) => {
const toolCallState = findLastToolCall(state.history);
const toolCallState = findCurrentToolCall(state.history);
if (!toolCallState) return;
toolCallState.status = "done";
},
setGeneratedOutput: (state, { payload }: PayloadAction<ToolCall>) => {
const toolCallState = findLastToolCall(state.history);
const toolCallState = findCurrentToolCall(state.history);
if (!toolCallState) return;
toolCallState.toolCall = payload;
toolCallState.status = "generated";
},
setCalling: (state) => {
const toolCallState = findLastToolCall(state.history);
const toolCallState = findCurrentToolCall(state.history);
if (!toolCallState) return;
toolCallState.status = "calling";

View file

@ -1,20 +1,8 @@
import { ToolCallState } from "core";
import { RootState } from "../store";
function findLast<T>(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
): T | undefined {
for (let i = array.length - 1; i >= 0; i--) {
if (predicate(array[i], i, array)) {
return array[i];
}
}
return undefined;
}
export function findLastToolCall(
export function findCurrentToolCall(
state: RootState["state"]["history"],
): ToolCallState | undefined {
return findLast(state, (item) => !!item.toolCallState)?.toolCallState;
return state[state.length - 1]?.toolCallState;
}