run tools

This commit is contained in:
Nate 2024-11-17 18:54:23 -08:00
parent 47dd44da55
commit 445716c1cc
4 changed files with 44 additions and 11 deletions

View file

@ -744,7 +744,7 @@ export class Core {
on("tools/call", async ({ data: { toolCall } }) => {
const tool = instantiateTool(toolCall.function.name, { ide: this.ide });
const result = await tool.action(toolCall.function.arguments);
const result = await tool.action(JSON.parse(toolCall.function.arguments));
return { result };
});
}

View file

@ -1,4 +1,5 @@
import { IDE, Tool } from "..";
import { getPathModuleForIde } from "../util/pathModule";
export interface ToolParams {
ide: IDE;
@ -7,7 +8,16 @@ export interface ToolParams {
export const makeCreateNewFileTool = ({ ide }: ToolParams): Tool => ({
type: "function",
action: async (args: any) => {
await ide.writeFile(args.filepath, args.contents);
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",

View file

@ -11,10 +11,10 @@ export function CreateFile(props: CreateFileToolCallProps) {
<>
<p>Continue wants to create a file:</p>
<StepContainerPreToolbar
codeBlockContent={props.fileContents ?? ""}
codeBlockContent={props.fileContents ?? "New File"}
codeBlockIndex={0}
language={"javascript"}
filepath={props.filepath ?? ""}
filepath={props.filepath ?? "Loading"}
isGeneratingCodeBlock={false}
expanded={false}
hideApply={true}

View file

@ -1,5 +1,5 @@
import { ToolCall } from "core";
import { useContext } from "react";
import { useContext, useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import styled from "styled-components";
import {
@ -8,6 +8,7 @@ import {
vscButtonBackground,
vscButtonForeground,
} from "../../../components";
import Spinner from "../../../components/markdown/StepContainerPreToolbar/Spinner";
import { IdeMessengerContext } from "../../../context/IdeMessenger";
import { streamUpdate } from "../../../redux/slices/stateSlice";
import { CreateFile } from "./CreateFile";
@ -55,16 +56,16 @@ interface ToolCallDivProps {
toolCall: ToolCall;
}
function incrementalParseJson(raw: string): any {
function incrementalParseJson(raw: string): [boolean, any] {
try {
return JSON.parse(raw);
return [true, JSON.parse(raw)];
} catch (e) {
return {};
return [false, {}];
}
}
function FunctionSpecificToolCallDiv(toolCall: ToolCall) {
const args = incrementalParseJson(toolCall.function.arguments);
const [_, args] = incrementalParseJson(toolCall.function.arguments);
switch (toolCall.function.name) {
case "create_new_file":
@ -91,6 +92,19 @@ export function ToolCallDiv(props: ToolCallDivProps) {
const ideMessenger = useContext(IdeMessengerContext);
const dispatch = useDispatch();
const [loading, setLoading] = useState<boolean>(true);
useEffect(() => {
if (props.toolCall.function.arguments.length === 0) {
return;
}
const [done, _] = incrementalParseJson(props.toolCall.function.arguments);
if (done) {
setLoading(false);
}
}, [props.toolCall.function.arguments]);
async function callTool() {
const result = await ideMessenger.request("tools/call", {
toolCall: props.toolCall,
@ -109,8 +123,17 @@ export function ToolCallDiv(props: ToolCallDivProps) {
<Container>
<FunctionSpecificToolCallDiv {...props.toolCall} />
<ButtonContainer>
<RejectButton onClick={() => {}}>Cancel</RejectButton>
<AcceptButton onClick={callTool}>Continue</AcceptButton>
{loading ? (
<div className="ml-auto flex items-center gap-4">
Loading...
<Spinner />
</div>
) : (
<>
<RejectButton onClick={() => {}}>Cancel</RejectButton>
<AcceptButton onClick={callTool}>Continue</AcceptButton>
</>
)}
</ButtonContainer>
</Container>
);