mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-19 14:01:15 +00:00
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:
parent
e6b47799a4
commit
594edec31b
3 changed files with 230 additions and 55 deletions
|
|
@ -1,8 +1,9 @@
|
||||||
import * as os from "node:os";
|
import * as os from "node:os";
|
||||||
import type { AgentTool } from "@mariozechner/pi-ai";
|
import type { AgentTool } from "@mariozechner/pi-ai";
|
||||||
import { Type } from "@sinclair/typebox";
|
import { Type } from "@sinclair/typebox";
|
||||||
import { existsSync, readFileSync, writeFileSync } from "fs";
|
import { constants } from "fs";
|
||||||
import { resolve } from "path";
|
import { access, readFile, writeFile } from "fs/promises";
|
||||||
|
import { resolve as resolvePath } from "path";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand ~ to home directory
|
* Expand ~ to home directory
|
||||||
|
|
@ -34,42 +35,116 @@ export const editTool: AgentTool<typeof editSchema> = {
|
||||||
{ path, oldText, newText }: { path: string; oldText: string; newText: string },
|
{ path, oldText, newText }: { path: string; oldText: string; newText: string },
|
||||||
signal?: AbortSignal,
|
signal?: AbortSignal,
|
||||||
) => {
|
) => {
|
||||||
// Check if already aborted
|
const absolutePath = resolvePath(expandPath(path));
|
||||||
if (signal?.aborted) {
|
|
||||||
throw new Error("Operation aborted");
|
|
||||||
}
|
|
||||||
|
|
||||||
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)) {
|
let aborted = false;
|
||||||
throw new Error(`File not found: ${path}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (signal) {
|
||||||
if (!content.includes(oldText)) {
|
signal.addEventListener("abort", onAbort, { once: true });
|
||||||
throw new Error(
|
}
|
||||||
`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Count occurrences
|
// Perform the edit operation
|
||||||
const occurrences = content.split(oldText).length - 1;
|
(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) {
|
// Check if aborted before reading
|
||||||
throw new Error(
|
if (aborted) {
|
||||||
`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`,
|
return;
|
||||||
);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Perform replacement
|
// Read the file
|
||||||
const newContent = content.replace(oldText, newText);
|
const content = await readFile(absolutePath, "utf-8");
|
||||||
writeFileSync(absolutePath, newContent, "utf-8");
|
|
||||||
|
|
||||||
return {
|
// Check if aborted after reading
|
||||||
output: `Successfully replaced text in ${path}. Changed ${oldText.length} characters to ${newText.length} characters.`,
|
if (aborted) {
|
||||||
details: undefined,
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
import * as os from "node:os";
|
import * as os from "node:os";
|
||||||
import type { AgentTool } from "@mariozechner/pi-ai";
|
import type { AgentTool } from "@mariozechner/pi-ai";
|
||||||
import { Type } from "@sinclair/typebox";
|
import { Type } from "@sinclair/typebox";
|
||||||
import { existsSync, readFileSync } from "fs";
|
import { constants } from "fs";
|
||||||
import { resolve } from "path";
|
import { access, readFile } from "fs/promises";
|
||||||
|
import { resolve as resolvePath } from "path";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand ~ to home directory
|
* Expand ~ to home directory
|
||||||
|
|
@ -27,18 +28,71 @@ export const readTool: AgentTool<typeof readSchema> = {
|
||||||
description: "Read the contents of a file. Returns the full file content as text.",
|
description: "Read the contents of a file. Returns the full file content as text.",
|
||||||
parameters: readSchema,
|
parameters: readSchema,
|
||||||
execute: async (_toolCallId: string, { path }: { path: string }, signal?: AbortSignal) => {
|
execute: async (_toolCallId: string, { path }: { path: string }, signal?: AbortSignal) => {
|
||||||
// Check if already aborted
|
const absolutePath = resolvePath(expandPath(path));
|
||||||
if (signal?.aborted) {
|
|
||||||
throw new Error("Operation aborted");
|
|
||||||
}
|
|
||||||
|
|
||||||
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)) {
|
let aborted = false;
|
||||||
throw new Error(`File not found: ${path}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const content = readFileSync(absolutePath, "utf-8");
|
// Set up abort handler
|
||||||
return { output: content, details: undefined };
|
const onAbort = () => {
|
||||||
|
aborted = true;
|
||||||
|
reject(new Error("Operation aborted"));
|
||||||
|
};
|
||||||
|
|
||||||
|
if (signal) {
|
||||||
|
signal.addEventListener("abort", onAbort, { once: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform the read operation
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
// Check if file exists
|
||||||
|
try {
|
||||||
|
await access(absolutePath, constants.R_OK);
|
||||||
|
} catch {
|
||||||
|
if (signal) {
|
||||||
|
signal.removeEventListener("abort", onAbort);
|
||||||
|
}
|
||||||
|
reject(new Error(`File not found: ${path}`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if aborted before reading
|
||||||
|
if (aborted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the file
|
||||||
|
const content = await readFile(absolutePath, "utf-8");
|
||||||
|
|
||||||
|
// Check if aborted after reading
|
||||||
|
if (aborted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up abort handler
|
||||||
|
if (signal) {
|
||||||
|
signal.removeEventListener("abort", onAbort);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve({ output: content, details: undefined });
|
||||||
|
} catch (error: any) {
|
||||||
|
// Clean up abort handler
|
||||||
|
if (signal) {
|
||||||
|
signal.removeEventListener("abort", onAbort);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!aborted) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import * as os from "node:os";
|
import * as os from "node:os";
|
||||||
import type { AgentTool } from "@mariozechner/pi-ai";
|
import type { AgentTool } from "@mariozechner/pi-ai";
|
||||||
import { Type } from "@sinclair/typebox";
|
import { Type } from "@sinclair/typebox";
|
||||||
import { mkdirSync, writeFileSync } from "fs";
|
import { mkdir, writeFile } from "fs/promises";
|
||||||
import { dirname, resolve } from "path";
|
import { dirname, resolve as resolvePath } from "path";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand ~ to home directory
|
* Expand ~ to home directory
|
||||||
|
|
@ -29,18 +29,64 @@ export const writeTool: AgentTool<typeof writeSchema> = {
|
||||||
"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
|
"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
|
||||||
parameters: writeSchema,
|
parameters: writeSchema,
|
||||||
execute: async (_toolCallId: string, { path, content }: { path: string; content: string }, signal?: AbortSignal) => {
|
execute: async (_toolCallId: string, { path, content }: { path: string; content: string }, signal?: AbortSignal) => {
|
||||||
// Check if already aborted
|
const absolutePath = resolvePath(expandPath(path));
|
||||||
if (signal?.aborted) {
|
|
||||||
throw new Error("Operation aborted");
|
|
||||||
}
|
|
||||||
|
|
||||||
const absolutePath = resolve(expandPath(path));
|
|
||||||
const dir = dirname(absolutePath);
|
const dir = dirname(absolutePath);
|
||||||
|
|
||||||
// Create parent directories if needed
|
return new Promise<{ output: string; details: undefined }>((resolve, reject) => {
|
||||||
mkdirSync(dir, { recursive: true });
|
// Check if already aborted
|
||||||
|
if (signal?.aborted) {
|
||||||
|
reject(new Error("Operation aborted"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
writeFileSync(absolutePath, content, "utf-8");
|
let aborted = false;
|
||||||
return { output: `Successfully wrote ${content.length} bytes to ${path}`, details: undefined };
|
|
||||||
|
// Set up abort handler
|
||||||
|
const onAbort = () => {
|
||||||
|
aborted = true;
|
||||||
|
reject(new Error("Operation aborted"));
|
||||||
|
};
|
||||||
|
|
||||||
|
if (signal) {
|
||||||
|
signal.addEventListener("abort", onAbort, { once: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform the write operation
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
// Create parent directories if needed
|
||||||
|
await mkdir(dir, { recursive: true });
|
||||||
|
|
||||||
|
// Check if aborted before writing
|
||||||
|
if (aborted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the file
|
||||||
|
await writeFile(absolutePath, content, "utf-8");
|
||||||
|
|
||||||
|
// Check if aborted after writing
|
||||||
|
if (aborted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up abort handler
|
||||||
|
if (signal) {
|
||||||
|
signal.removeEventListener("abort", onAbort);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve({ output: `Successfully wrote ${content.length} bytes to ${path}`, details: undefined });
|
||||||
|
} catch (error: any) {
|
||||||
|
// Clean up abort handler
|
||||||
|
if (signal) {
|
||||||
|
signal.removeEventListener("abort", onAbort);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!aborted) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue