Make file operations properly abortable with async operations

Replace synchronous file operations with async Promise-based operations
that listen to abort signals during execution:
- read, write, edit now use fs/promises async APIs
- Add abort event listeners that reject immediately on abort
- Check abort status before and after each async operation
- Clean up event listeners properly

This ensures pressing Esc during file operations shows red error state.
This commit is contained in:
Mario Zechner 2025-11-11 23:33:16 +01:00
parent e6b47799a4
commit 594edec31b
3 changed files with 230 additions and 55 deletions

View file

@ -1,8 +1,9 @@
import * as os from "node:os";
import type { AgentTool } from "@mariozechner/pi-ai";
import { Type } from "@sinclair/typebox";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
import { constants } from "fs";
import { access, readFile, writeFile } from "fs/promises";
import { resolve as resolvePath } from "path";
/**
* Expand ~ to home directory
@ -34,42 +35,116 @@ export const editTool: AgentTool<typeof editSchema> = {
{ 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 = resolvePath(expandPath(path));
const absolutePath = resolve(expandPath(path));
return new Promise<{ output: string; details: undefined }>((resolve, reject) => {
// Check if already aborted
if (signal?.aborted) {
reject(new Error("Operation aborted"));
return;
}
if (!existsSync(absolutePath)) {
throw new Error(`File not found: ${path}`);
}
let aborted = false;
const content = readFileSync(absolutePath, "utf-8");
// Set up abort handler
const onAbort = () => {
aborted = true;
reject(new Error("Operation aborted"));
};
// Check if old text exists
if (!content.includes(oldText)) {
throw new Error(
`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`,
);
}
if (signal) {
signal.addEventListener("abort", onAbort, { once: true });
}
// Count occurrences
const occurrences = content.split(oldText).length - 1;
// Perform the edit operation
(async () => {
try {
// Check if file exists
try {
await access(absolutePath, constants.R_OK | constants.W_OK);
} catch {
if (signal) {
signal.removeEventListener("abort", onAbort);
}
reject(new Error(`File not found: ${path}`));
return;
}
if (occurrences > 1) {
throw new Error(
`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`,
);
}
// Check if aborted before reading
if (aborted) {
return;
}
// Perform replacement
const newContent = content.replace(oldText, newText);
writeFileSync(absolutePath, newContent, "utf-8");
// Read the file
const content = await readFile(absolutePath, "utf-8");
return {
output: `Successfully replaced text in ${path}. Changed ${oldText.length} characters to ${newText.length} characters.`,
details: undefined,
};
// Check if aborted after reading
if (aborted) {
return;
}
// Check if old text exists
if (!content.includes(oldText)) {
if (signal) {
signal.removeEventListener("abort", onAbort);
}
reject(
new Error(
`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`,
),
);
return;
}
// Count occurrences
const occurrences = content.split(oldText).length - 1;
if (occurrences > 1) {
if (signal) {
signal.removeEventListener("abort", onAbort);
}
reject(
new Error(
`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`,
),
);
return;
}
// Check if aborted before writing
if (aborted) {
return;
}
// Perform replacement
const newContent = content.replace(oldText, newText);
await writeFile(absolutePath, newContent, "utf-8");
// Check if aborted after writing
if (aborted) {
return;
}
// Clean up abort handler
if (signal) {
signal.removeEventListener("abort", onAbort);
}
resolve({
output: `Successfully replaced text in ${path}. Changed ${oldText.length} characters to ${newText.length} characters.`,
details: undefined,
});
} catch (error: any) {
// Clean up abort handler
if (signal) {
signal.removeEventListener("abort", onAbort);
}
if (!aborted) {
reject(error);
}
}
})();
});
},
};