From e6b47799a40642dc428f9c031133a329d6fd3fa2 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Tue, 11 Nov 2025 23:28:39 +0100 Subject: [PATCH] Add abort signal handling to read, write, and edit tools All tools now check the abort signal before executing and throw "Operation aborted" error if the signal is already aborted. This ensures consistent abort behavior across all tools. --- packages/coding-agent/example.json | 66 ++++++++++++++++++++++++ packages/coding-agent/src/tools/edit.ts | 6 +++ packages/coding-agent/src/tools/read.ts | 7 ++- packages/coding-agent/src/tools/write.ts | 7 ++- 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 packages/coding-agent/example.json diff --git a/packages/coding-agent/example.json b/packages/coding-agent/example.json new file mode 100644 index 00000000..32691ff9 --- /dev/null +++ b/packages/coding-agent/example.json @@ -0,0 +1,66 @@ +{ + "name": "example", + "version": "1.0.0", + "description": "A JSON file formatted with tabs", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node index.js", + "dev": "nodemon index.js", + "build": "tsc", + "lint": "eslint .", + "format": "prettier --write .", + "clean": "rm -rf dist node_modules" + }, + "keywords": [ + "example", + "json", + "tabs", + "nodejs", + "typescript", + "api" + ], + "author": "Assistant", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/example/example-repo.git" + }, + "bugs": { + "url": "https://github.com/example/example-repo/issues" + }, + "homepage": "https://github.com/example/example-repo#readme", + "engines": { + "node": ">=18.0.0", + "npm": ">=9.0.0" + }, + "dependencies": { + "express": "^4.18.0", + "dotenv": "^16.0.3", + "axios": "^1.6.0", + "lodash": "^4.17.21", + "mongoose": "^8.0.0", + "redis": "^4.6.0", + "jsonwebtoken": "^9.0.2", + "bcrypt": "^5.1.1", + "winston": "^3.11.0" + }, + "devDependencies": { + "@types/node": "^20.10.0", + "@types/express": "^4.17.21", + "@types/bcrypt": "^5.0.2", + "@types/jsonwebtoken": "^9.0.5", + "typescript": "^5.3.3", + "nodemon": "^3.0.2", + "eslint": "^8.55.0", + "prettier": "^3.1.1", + "vitest": "^1.0.4", + "supertest": "^6.3.3" + }, + "config": { + "port": 3000, + "env": "development" + }, + "private": false +} diff --git a/packages/coding-agent/src/tools/edit.ts b/packages/coding-agent/src/tools/edit.ts index e1ee2005..af2e3781 100644 --- a/packages/coding-agent/src/tools/edit.ts +++ b/packages/coding-agent/src/tools/edit.ts @@ -32,7 +32,13 @@ export const editTool: AgentTool = { execute: async ( _toolCallId: string, { path, oldText, newText }: { path: string; oldText: string; newText: string }, + signal?: AbortSignal, ) => { + // Check if already aborted + if (signal?.aborted) { + throw new Error("Operation aborted"); + } + const absolutePath = resolve(expandPath(path)); if (!existsSync(absolutePath)) { diff --git a/packages/coding-agent/src/tools/read.ts b/packages/coding-agent/src/tools/read.ts index 9e549cda..bb1c0a47 100644 --- a/packages/coding-agent/src/tools/read.ts +++ b/packages/coding-agent/src/tools/read.ts @@ -26,7 +26,12 @@ export const readTool: AgentTool = { label: "read", description: "Read the contents of a file. Returns the full file content as text.", parameters: readSchema, - execute: async (_toolCallId: string, { path }: { path: string }) => { + execute: async (_toolCallId: string, { path }: { path: string }, signal?: AbortSignal) => { + // Check if already aborted + if (signal?.aborted) { + throw new Error("Operation aborted"); + } + const absolutePath = resolve(expandPath(path)); if (!existsSync(absolutePath)) { diff --git a/packages/coding-agent/src/tools/write.ts b/packages/coding-agent/src/tools/write.ts index 4eb18e25..1b1c8397 100644 --- a/packages/coding-agent/src/tools/write.ts +++ b/packages/coding-agent/src/tools/write.ts @@ -28,7 +28,12 @@ export const writeTool: AgentTool = { description: "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.", parameters: writeSchema, - execute: async (_toolCallId: string, { path, content }: { path: string; content: string }) => { + execute: async (_toolCallId: string, { path, content }: { path: string; content: string }, signal?: AbortSignal) => { + // Check if already aborted + if (signal?.aborted) { + throw new Error("Operation aborted"); + } + const absolutePath = resolve(expandPath(path)); const dir = dirname(absolutePath);