Skip to content

Commit

Permalink
Use node:path in tools (#34)
Browse files Browse the repository at this point in the history
* Refactor to use node path

* Use local prettier formatting file
  • Loading branch information
djfarrelly authored Dec 20, 2024
1 parent 00d2380 commit 0316cf2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
62 changes: 46 additions & 16 deletions examples/swebench/tools/tools.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { createTool } from "@inngest/agent-kit";
import fs from "fs";
import fs from "node:fs";
import path from "node:path";
import Parser from "tree-sitter";
import Py from "tree-sitter-python";
import { z } from "zod";

// NOTE: In this repo, all files are stored in "./opt/" as the prefix.
const WORKING_DIR = "./opt";

// PyClass represents a class parsed from a python file.
interface PyClass {
name: string;
Expand All @@ -25,14 +29,14 @@ interface PyFn {
export const listFilesTool = createTool({
name: "list_files",
description:
"Lists all files within the project, returned as a JSON string containign the path to each file",
"Lists all files within the project, returned as a JSON string containing the path to each file",
handler: async (_input, opts) => {
// NOTE: In this repo, all files are stored in "./opt/" as the prefix.
const path = "./opt/" + opts.network?.state.kv.get("repo");
const repo = opts.network?.state.kv.get("repo") || "";
const repoDir = path.join(WORKING_DIR, repo);

const files = await opts.step.run("list files", () => {
return fs
.readdirSync(path, { recursive: true })
.readdirSync(repoDir, { recursive: true })
.filter((name) => name.indexOf(".git") !== 0);
});

Expand Down Expand Up @@ -61,6 +65,29 @@ export const readFileTool = createTool({
},
});

export const writeFileTool = createTool({
name: "write_file",
description: "Writes a single file to disk with its content",
parameters: z.object({
filename: z.string(),
content: z.string(),
}),
handler: async ({ filename, content }, opts) => {
await opts.step.run(`read file: ${filename}`, () => {
return writeFile(
opts.network?.state.kv.get("repo") || "",
filename,
content,
);
});

// Set state for the filename. Note that this happens outside of steps
// so that this is not memoized.
opts.network?.state.kv.set("file:" + filename, content);
return content;
},
});

/**
* extractFnTool extracts all top level functions and classes from a Python file. It also
* parses all method definitions of a class.
Expand All @@ -77,7 +104,7 @@ export const extractClassAndFnsTool = createTool({
return await opts.step.run("parse file", () => {
const contents = readFile(
opts.network?.state.kv.get("repo") || "",
input.filename
input.filename,
);
return parseClassAndFns(contents);
});
Expand All @@ -95,15 +122,15 @@ export const replaceClassMethodTool = createTool({
}),
handler: async (
{ filename, class_name, function_name, new_contents },
opts
opts,
) => {
const updated = await opts?.step.run(
`update class method in '${filename}': ${class_name}.${function_name}`,
`update class method in "${filename}": ${class_name}.${function_name}`,
() => {
// Re-parse the contents to find the correct start and end offsets.
const contents = readFile(
opts.network?.state.kv.get("repo") || "",
filename
filename,
);
const parsed = parseClassAndFns(contents);

Expand All @@ -128,12 +155,10 @@ export const replaceClassMethodTool = createTool({
return isRange ? [...updated, new_contents] : updated;
}, [] as string[])
.join("\n");
}
},
);

const path = "./opt/" + opts.network?.state.kv.get("repo");
fs.writeFileSync(path + "/" + filename, updated);

writeFile(opts.network?.state.kv.get("repo") || "", filename, updated);
return new_contents;
},
});
Expand All @@ -143,9 +168,14 @@ export const replaceClassMethodTool = createTool({
//

export const readFile = (repo: string, filename: string) => {
// NOTE: In this repo, all files are stored in "./opt/" as the prefix.
const path = "./opt/" + repo;
return fs.readFileSync(path + "/" + filename).toString();
return fs.readFileSync(path.join(WORKING_DIR, repo, filename)).toString();
};
export const writeFile = (repo: string, filename: string, content: string) => {
return fs.writeFileSync(
path.join(WORKING_DIR, repo, filename),
content,
"utf-8",
);
};

export const parseClassAndFns = (contents: string) => {
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@
},
"lint-staged": {
"*.{j,t}s": "eslint --cache --fix"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"semi": true,
"trailingComma": "es5"
}
}

0 comments on commit 0316cf2

Please sign in to comment.