From 492f53bf46c7a556ce131cf2b1c4fe075a205bda Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 19 Nov 2024 22:19:43 -0800 Subject: [PATCH] more tools --- core/index.d.ts | 1 + core/package-lock.json | 6 ++ core/package.json | 1 + core/tools/createNewFile.ts | 37 +++++++++++ core/tools/index.ts | 66 ++----------------- core/tools/runTerminalCommand.ts | 24 +++++++ core/tools/types.ts | 5 ++ core/util/incrementalParseJson.ts | 8 ++- extensions/vscode/package-lock.json | 1 + gui/package-lock.json | 1 + gui/src/pages/gui/Chat.tsx | 17 ++++- .../gui/ToolCallDiv/ToolCallButtonsDiv.tsx | 8 ++- gui/src/pages/gui/ToolCallDiv/index.tsx | 15 ++++- gui/src/redux/slices/stateSlice.ts | 51 ++++++++++++-- gui/src/redux/slices/toolCallSlice.ts | 45 ------------- gui/src/redux/store.ts | 2 - 16 files changed, 163 insertions(+), 125 deletions(-) create mode 100644 core/tools/createNewFile.ts create mode 100644 core/tools/runTerminalCommand.ts create mode 100644 core/tools/types.ts delete mode 100644 gui/src/redux/slices/toolCallSlice.ts diff --git a/core/index.d.ts b/core/index.d.ts index 780ded914..65fed30a0 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -336,6 +336,7 @@ export interface ChatHistoryItem { modifiers?: InputModifiers; contextItems: ContextItemWithId[]; promptLogs?: PromptLog[]; + acceptedToolCall?: boolean; } export type ChatHistory = ChatHistoryItem[]; diff --git a/core/package-lock.json b/core/package-lock.json index 09e22ce96..2c1361364 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -47,6 +47,7 @@ "onnxruntime-node": "1.14.0", "openai": "^4.20.1", "p-limit": "^6.1.0", + "partial-json": "^0.1.7", "pg": "^8.11.3", "posthog-node": "^3.6.3", "puppeteer": "^22.4.0", @@ -12077,6 +12078,11 @@ "url": "https://github.com/inikulin/parse5?sponsor=1" } }, + "node_modules/partial-json": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/partial-json/-/partial-json-0.1.7.tgz", + "integrity": "sha512-Njv/59hHaokb/hRUjce3Hdv12wd60MtM9Z5Olmn+nehe0QDAsRtRbJPvJ0Z91TusF0SuZRIvnM+S4l6EIP8leA==" + }, "node_modules/path-exists": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", diff --git a/core/package.json b/core/package.json index e95c66509..38d2aea0d 100644 --- a/core/package.json +++ b/core/package.json @@ -78,6 +78,7 @@ "onnxruntime-node": "1.14.0", "openai": "^4.20.1", "p-limit": "^6.1.0", + "partial-json": "^0.1.7", "pg": "^8.11.3", "posthog-node": "^3.6.3", "puppeteer": "^22.4.0", diff --git a/core/tools/createNewFile.ts b/core/tools/createNewFile.ts new file mode 100644 index 000000000..caadfc152 --- /dev/null +++ b/core/tools/createNewFile.ts @@ -0,0 +1,37 @@ +import { Tool } from ".."; +import { getPathModuleForIde } from "../util/pathModule"; +import { ToolParams } from "./types"; + +export const makeCreateNewFileTool = ({ ide }: ToolParams): Tool => ({ + type: "function", + action: async (args: any) => { + const pathSep = await ide.pathSep(); + let filepath = args.filepath; + if (!args.filepath.startsWith(pathSep)) { + const pathModule = await getPathModuleForIde(ide); + const workspaceDirs = await ide.getWorkspaceDirs(); + const cwd = workspaceDirs[0]; + filepath = pathModule.join(cwd, filepath); + } + await ide.writeFile(filepath, args.contents); + await ide.openFile(filepath); + }, + function: { + name: "create_new_file", + description: "Create a new file", + parameters: { + type: "object", + required: ["filepath", "contents"], + properties: { + filepath: { + type: "string", + description: "The path where the new file should be created", + }, + contents: { + type: "string", + description: "The contents to write to the new file", + }, + }, + }, + }, +}); diff --git a/core/tools/index.ts b/core/tools/index.ts index 862f7dd6b..5073562d5 100644 --- a/core/tools/index.ts +++ b/core/tools/index.ts @@ -1,65 +1,7 @@ -import { IDE, Tool } from ".."; -import { getPathModuleForIde } from "../util/pathModule"; - -export interface ToolParams { - ide: IDE; -} - -export const makeCreateNewFileTool = ({ ide }: ToolParams): Tool => ({ - type: "function", - action: async (args: any) => { - const pathSep = await ide.pathSep(); - let filepath = args.filepath; - if (!args.filepath.startsWith(pathSep)) { - const pathModule = await getPathModuleForIde(ide); - const workspaceDirs = await ide.getWorkspaceDirs(); - const cwd = workspaceDirs[0]; - filepath = pathModule.join(cwd, filepath); - } - await ide.writeFile(filepath, args.contents); - await ide.openFile(filepath); - }, - function: { - name: "create_new_file", - description: "Create a new file", - parameters: { - type: "object", - required: ["filepath", "contents"], - properties: { - filepath: { - type: "string", - description: "The path where the new file should be created", - }, - contents: { - type: "string", - description: "The contents to write to the new file", - }, - }, - }, - }, -}); - -export const runTerminalCommandTool = ({ ide }: ToolParams): Tool => ({ - type: "function", - action: async (args) => { - await ide.runCommand(args.command); - }, - function: { - name: "run_terminal_command", - description: "Run a terminal command in the current directory", - parameters: { - type: "object", - required: ["command"], - properties: { - command: { - type: "string", - description: - "The command to run. This will be passed directly into the shell.", - }, - }, - }, - }, -}); +import { Tool } from ".."; +import { makeCreateNewFileTool } from "./createNewFile"; +import { runTerminalCommandTool } from "./runTerminalCommand"; +import { ToolParams } from "./types"; export function instantiateTool(name: string, { ide }: ToolParams): Tool { switch (name) { diff --git a/core/tools/runTerminalCommand.ts b/core/tools/runTerminalCommand.ts new file mode 100644 index 000000000..15ce4c84b --- /dev/null +++ b/core/tools/runTerminalCommand.ts @@ -0,0 +1,24 @@ +import { Tool } from ".."; +import { ToolParams } from "./types"; + +export const runTerminalCommandTool = ({ ide }: ToolParams): Tool => ({ + type: "function", + action: async (args) => { + await ide.runCommand(args.command); + }, + function: { + name: "run_terminal_command", + description: "Run a terminal command in the current directory", + parameters: { + type: "object", + required: ["command"], + properties: { + command: { + type: "string", + description: + "The command to run. This will be passed directly into the shell.", + }, + }, + }, + }, +}); diff --git a/core/tools/types.ts b/core/tools/types.ts new file mode 100644 index 000000000..616f8ea5d --- /dev/null +++ b/core/tools/types.ts @@ -0,0 +1,5 @@ +import { IDE } from ".."; + +export interface ToolParams { + ide: IDE; +} diff --git a/core/util/incrementalParseJson.ts b/core/util/incrementalParseJson.ts index c72809035..21385ad6a 100644 --- a/core/util/incrementalParseJson.ts +++ b/core/util/incrementalParseJson.ts @@ -1,7 +1,13 @@ +import { parse } from "partial-json"; + export function incrementalParseJson(raw: string): [boolean, any] { try { return [true, JSON.parse(raw)]; } catch (e) { - return [false, {}]; + try { + return [false, parse(raw)]; + } catch (e2) { + return [false, {}]; + } } } diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 3dc6757e7..fb1219880 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -132,6 +132,7 @@ "onnxruntime-node": "1.14.0", "openai": "^4.20.1", "p-limit": "^6.1.0", + "partial-json": "^0.1.7", "pg": "^8.11.3", "posthog-node": "^3.6.3", "puppeteer": "^22.4.0", diff --git a/gui/package-lock.json b/gui/package-lock.json index 7d5bf06c9..1f857451c 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -134,6 +134,7 @@ "onnxruntime-node": "1.14.0", "openai": "^4.20.1", "p-limit": "^6.1.0", + "partial-json": "^0.1.7", "pg": "^8.11.3", "posthog-node": "^3.6.3", "puppeteer": "^22.4.0", diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 759c1e834..ef64de6dc 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -111,6 +111,7 @@ function fallbackRender({ error, resetErrorBoundary }: any) { >

Something went wrong:

{error.message}
+
{error.stack}
@@ -137,6 +138,10 @@ export function Chat() { const { saveSession, getLastSessionId, loadLastSession } = useHistory(dispatch); + const toolCallState = useSelector( + (store: RootState) => store.state.currentToolCallState, + ); + const snapToBottom = useCallback(() => { if (!stepsDivRef.current) return; const elem = stepsDivRef.current; @@ -348,7 +353,12 @@ export function Chat() { item.message.toolCalls ? (
{item.message.toolCalls?.map((toolCall) => { - return ; + return ( + + ); })}
) : ( @@ -423,7 +433,10 @@ export function Chat() { )}
- + + {toolCallState.currentToolCallState === "generated" && ( + + )} store.toolCallState); + const toolCallState = useSelector( + (store: RootState) => store.state.currentToolCallState, + ); const ideMessenger = useContext(IdeMessengerContext); async function callTool() { diff --git a/gui/src/pages/gui/ToolCallDiv/index.tsx b/gui/src/pages/gui/ToolCallDiv/index.tsx index 6580c205e..1aa8c4a05 100644 --- a/gui/src/pages/gui/ToolCallDiv/index.tsx +++ b/gui/src/pages/gui/ToolCallDiv/index.tsx @@ -8,7 +8,7 @@ import Spinner from "../../../components/markdown/StepContainerPreToolbar/Spinne import { registerCurrentToolCall, setGeneratedOutput, -} from "../../../redux/slices/toolCallSlice"; +} from "../../../redux/slices/stateSlice"; import { RootState } from "../../../redux/store"; import FunctionSpecificToolCallDiv from "./FunctionSpecificToolCallDiv"; import { ThreadDiv } from "./ThreadDiv"; @@ -16,11 +16,14 @@ import { ToolState } from "./types"; interface ToolCallDivProps { toolCall: ToolCall; + acceptedToolCall?: boolean; } export function ToolCallDiv(props: ToolCallDivProps) { const dispatch = useDispatch(); - const toolCallState = useSelector((store: RootState) => store.toolCallState); + const toolCallState = useSelector( + (store: RootState) => store.state.currentToolCallState, + ); useEffect(() => { dispatch(registerCurrentToolCall()); @@ -39,11 +42,17 @@ export function ToolCallDiv(props: ToolCallDivProps) { }, [props.toolCall.function.arguments]); function getIcon(state: ToolState) { + if (props.acceptedToolCall === true) { + return ; + } else if (props.acceptedToolCall === false) { + return ; + } + switch (state) { case "generating": return ; case "done": - return ; + return ; case "calling": return ; case "canceled": diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index 5855d0e54..aea7cbd7a 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -4,18 +4,23 @@ import { ChatHistoryItem, ChatMessage, Checkpoint, - ContextItemId, ContextItemWithId, PersistedSessionInfo, PromptLog, + ToolCall, } from "core"; import { BrowserSerializedContinueConfig } from "core/config/load"; import { ConfigValidationError } from "core/config/validation"; import { stripImages } from "core/llm/images"; -import { createSelector } from "reselect"; -import { v4 as uuidv4, v4 } from "uuid"; -import { RootState } from "../store"; import { ApplyState } from "core/protocol/ideWebview"; +import { v4 as uuidv4, v4 } from "uuid"; +import { ToolState } from "../../pages/gui/ToolCallDiv/types"; + +interface CurrentToolCallState { + currentToolCallId?: string; + currentToolCallState?: ToolState; + toolCall?: ToolCall; +} // 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. @@ -23,6 +28,7 @@ type ChatHistoryItemWithMessageId = ChatHistoryItem & { message: ChatMessage & { id: string }; }; type State = { + currentToolCallState: CurrentToolCallState; history: ChatHistoryItemWithMessageId[]; ttsActive: boolean; active: boolean; @@ -42,6 +48,10 @@ type State = { }; const initialState: State = { + currentToolCallState: { + currentToolCallId: undefined, + currentToolCallState: undefined, + }, history: [], ttsActive: false, active: false, @@ -181,7 +191,6 @@ export const stateSlice = createSlice({ editorState: JSONContent; }>, ) => { - console.log("OAEJFOIJOJOJAJ"); state.history.push({ message: { role: "user", content: "", id: uuidv4() }, contextItems: [], @@ -209,7 +218,6 @@ export const stateSlice = createSlice({ contextItems?: ContextItemWithId[]; }>, ) => { - console.log("aoiujnnnnnnn"); if (payload.index >= state.history.length) { state.history.push({ message: { ...payload.message, id: uuidv4() }, @@ -387,13 +395,36 @@ export const stateSlice = createSlice({ curApplyState.numDiffs = payload.numDiffs ?? curApplyState.numDiffs; curApplyState.filepath = payload.filepath ?? curApplyState.filepath; } - if(payload.status === "done"){ + if (payload.status === "done") { state.nextCodeBlockToApplyIndex++; } }, resetNextCodeBlockToApplyIndex: (state) => { state.nextCodeBlockToApplyIndex = 0; }, + + // Related to currentToolCallState + registerCurrentToolCall: (state, { payload }: PayloadAction) => { + state.currentToolCallState.currentToolCallId = payload; + state.currentToolCallState.currentToolCallState = "generating"; + }, + cancelToolCall: (state) => { + state.currentToolCallState.currentToolCallId = undefined; + state.currentToolCallState.currentToolCallState = undefined; + state.history[state.history.length - 1].acceptedToolCall = false; + }, + acceptToolCall: (state) => { + state.currentToolCallState.currentToolCallId = undefined; + state.currentToolCallState.currentToolCallState = undefined; + state.history[state.history.length - 1].acceptedToolCall = true; + }, + setGeneratedOutput: (state, { payload }: PayloadAction) => { + state.currentToolCallState.currentToolCallState = "generated"; + state.currentToolCallState.toolCall = payload; + }, + setCalling: (state) => { + state.currentToolCallState.currentToolCallState = "calling"; + }, }, }); @@ -423,6 +454,12 @@ export const { setCurCheckpointIndex, resetNextCodeBlockToApplyIndex, updateApplyState, + // toolCallState + registerCurrentToolCall, + setGeneratedOutput, + cancelToolCall, + acceptToolCall, + setCalling, } = stateSlice.actions; export default stateSlice.reducer; diff --git a/gui/src/redux/slices/toolCallSlice.ts b/gui/src/redux/slices/toolCallSlice.ts deleted file mode 100644 index 2b18e898a..000000000 --- a/gui/src/redux/slices/toolCallSlice.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { createSlice, PayloadAction } from "@reduxjs/toolkit"; -import { ToolCall } from "core"; -import { ToolState } from "../../pages/gui/ToolCallDiv/types"; - -interface CurrentToolCallState { - currentToolCallId?: string; - currentToolCallState?: ToolState; - toolCall?: ToolCall; -} - -const initialState: CurrentToolCallState = {}; -export const toolCallStateSlice = createSlice({ - name: "editModeState", - initialState, - reducers: { - registerCurrentToolCall: (state, { payload }: PayloadAction) => { - state.currentToolCallId = payload; - state.currentToolCallState = "generating"; - }, - cancelToolCall: (state) => { - state.currentToolCallId = undefined; - state.currentToolCallState = undefined; - }, - acceptToolCall: (state) => { - state.currentToolCallId = undefined; - state.currentToolCallState = undefined; - }, - setGeneratedOutput: (state, { payload }: PayloadAction) => { - state.currentToolCallState = "generated"; - state.toolCall = payload; - }, - setCalling: (state) => { - state.currentToolCallState = "calling"; - }, - }, -}); - -export const { - registerCurrentToolCall, - setGeneratedOutput, - cancelToolCall, - acceptToolCall, - setCalling, -} = toolCallStateSlice.actions; -export default toolCallStateSlice.reducer; diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts index e1accc497..725d88c01 100644 --- a/gui/src/redux/store.ts +++ b/gui/src/redux/store.ts @@ -10,7 +10,6 @@ import { createFilter } from "redux-persist-transform-filter"; import autoMergeLevel2 from "redux-persist/lib/stateReconciler/autoMergeLevel2"; import storage from "redux-persist/lib/storage"; import editModeStateReducer from "./slices/editModeState"; -import toolCallStateSliceReducer from "./slices/toolCallSlice"; export interface ChatMessage { role: "system" | "user" | "assistant"; content: string; @@ -23,7 +22,6 @@ const rootReducer = combineReducers({ uiState: uiStateReducer, serverState: serverStateReducer, editModeState: editModeStateReducer, - toolCallState: toolCallStateSliceReducer, }); export type RootState = ReturnType;