33 lines
957 B
TypeScript
33 lines
957 B
TypeScript
/**
|
|||
|
|
* Read a file from the workspace. Mirrors `tools/fs/read.rs`.
|
||
|
|
*/
|
||
|
|
import * as fs from "node:fs";
|
||
|
|
import type { JsonValue } from "@zesdex/domain";
|
||
|
|
import { type Tool, type ToolCtx } from "../mod.ts";
|
||
|
|
import { argStr, resolvePath } from "../util.ts";
|
||
|
|
|
||
|
|
/** Read the contents of a file. */
|
||
|
|
export class Read implements Tool {
|
||
|
|
name = "read";
|
||
|
|
description = "Read the contents of a file";
|
||
|
|
|
||
|
|
parameters = {
|
||
|
|
type: "object",
|
||
|
|
properties: {
|
||
|
|
path: { type: "string", description: "Path to the file to read (relative to workspace root)" },
|
||
|
|
},
|
||
|
|
required: ["path"],
|
||
|
|
};
|
||
|
|
|
||
|
|
run(ctx: ToolCtx, args: JsonValue): string {
|
||
|
|
const rel = argStr(args, "path");
|
||
|
|
const file = resolvePath(ctx.workspaces, rel);
|
||
|
|
if (!fs.existsSync(file)) {
|
||
|
|
throw new Error(`file '${rel}' does not exist`);
|
||
|
|
}
|
||
|
|
if (!fs.statSync(file).isFile()) {
|
||
|
|
throw new Error(`'${rel}' is not a file`);
|
||
|
|
}
|
||
|
|
return fs.readFileSync(file, "utf8");
|
||
|
|
}
|
||
|
|
}
|