more tools

This commit is contained in:
Nate 2024-11-19 22:19:43 -08:00
parent 3021c1e27c
commit 492f53bf46
16 changed files with 163 additions and 125 deletions

1
core/index.d.ts vendored
View file

@ -336,6 +336,7 @@ export interface ChatHistoryItem {
modifiers?: InputModifiers;
contextItems: ContextItemWithId[];
promptLogs?: PromptLog[];
acceptedToolCall?: boolean;
}
export type ChatHistory = ChatHistoryItem[];

View file

@ -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",

View file

@ -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",

View file

@ -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",
},
},
},
},
});

View file

@ -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) {

View file

@ -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.",
},
},
},
},
});

5
core/tools/types.ts Normal file
View file

@ -0,0 +1,5 @@
import { IDE } from "..";
export interface ToolParams {
ide: IDE;
}

View file

@ -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, {}];
}
}
}

View file

@ -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",

1
gui/package-lock.json generated
View file

@ -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",

View file

@ -111,6 +111,7 @@ function fallbackRender({ error, resetErrorBoundary }: any) {
>
<p>Something went wrong:</p>
<pre style={{ color: "red" }}>{error.message}</pre>
<pre style={{ color: lightGray }}>{error.stack}</pre>
<div className="text-center">
<Button onClick={resetErrorBoundary}>Restart</Button>
@ -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 ? (
<div>
{item.message.toolCalls?.map((toolCall) => {
return <ToolCallDiv toolCall={toolCall as any} />;
return (
<ToolCallDiv
toolCall={toolCall as any}
acceptedToolCall={item.acceptedToolCall}
/>
);
})}
</div>
) : (
@ -423,7 +433,10 @@ export function Chat() {
</StopButton>
)}
</div>
<ToolCallButtons />
{toolCallState.currentToolCallState === "generated" && (
<ToolCallButtons />
)}
<ContinueInputBox
isMainInput
isLastUserInput={false}

View file

@ -9,12 +9,12 @@ import {
} from "../../../components";
import Spinner from "../../../components/markdown/StepContainerPreToolbar/Spinner";
import { IdeMessengerContext } from "../../../context/IdeMessenger";
import { streamUpdate } from "../../../redux/slices/stateSlice";
import {
acceptToolCall,
cancelToolCall,
setCalling,
} from "../../../redux/slices/toolCallSlice";
streamUpdate,
} from "../../../redux/slices/stateSlice";
import { RootState } from "../../../redux/store";
const ButtonContainer = styled.div`
@ -56,7 +56,9 @@ interface ToolCallButtonsProps {}
export function ToolCallButtons(props: ToolCallButtonsProps) {
const dispatch = useDispatch();
const toolCallState = useSelector((store: RootState) => store.toolCallState);
const toolCallState = useSelector(
(store: RootState) => store.state.currentToolCallState,
);
const ideMessenger = useContext(IdeMessengerContext);
async function callTool() {

View file

@ -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 <CheckIcon className="text-green-500" color={lightGray} />;
} else if (props.acceptedToolCall === false) {
return <XMarkIcon className="text-red-500" />;
}
switch (state) {
case "generating":
return <Spinner />;
case "done":
return <CheckIcon className="text-green-500" color={lightGray} />;
return <CheckIcon className="text-green-500" />;
case "calling":
return <Spinner />;
case "canceled":

View file

@ -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<string>) => {
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<ToolCall>) => {
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;

View file

@ -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<string>) => {
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<ToolCall>) => {
state.currentToolCallState = "generated";
state.toolCall = payload;
},
setCalling: (state) => {
state.currentToolCallState = "calling";
},
},
});
export const {
registerCurrentToolCall,
setGeneratedOutput,
cancelToolCall,
acceptToolCall,
setCalling,
} = toolCallStateSlice.actions;
export default toolCallStateSlice.reducer;

View file

@ -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<typeof rootReducer>;