chore(vendor): import pi 1.3.0
Imported as a linear vendor commit so forge rebases do not replay the upstream root tree. git-subtree-dir: third_party/pi git-subtree-split: 71023aec1546d6d677a65669c5665c0927ce0dd0
This commit is contained in:
39
third_party/pi/packages/durable/src/tools/write.ts
vendored
Normal file
39
third_party/pi/packages/durable/src/tools/write.ts
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
import { type Static, Type } from "typebox";
|
||||
import { getOrThrow } from "../env/index.ts";
|
||||
import { defineTool } from "../harness/define.ts";
|
||||
import type { ToolRegistration } from "../harness/types.ts";
|
||||
import { requireEnv } from "./env.ts";
|
||||
import { withFileMutationQueue } from "./file-mutation-queue.ts";
|
||||
import { resolveToolPath } from "./path-utils.ts";
|
||||
|
||||
const writeSchema = Type.Object({
|
||||
path: Type.String({ description: "Path to the file to write (relative or absolute)" }),
|
||||
content: Type.String({ description: "Content to write to the file" }),
|
||||
});
|
||||
|
||||
export type WriteToolInput = Static<typeof writeSchema>;
|
||||
|
||||
export function createWriteTool(): ToolRegistration<typeof writeSchema> {
|
||||
return defineTool({
|
||||
name: "write",
|
||||
description:
|
||||
"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
|
||||
parameters: writeSchema,
|
||||
async execute(args, api, context) {
|
||||
const { path, content } = args;
|
||||
const env = requireEnv(api);
|
||||
const absolutePath = await resolveToolPath(env, path, context);
|
||||
return withFileMutationQueue(
|
||||
env,
|
||||
absolutePath,
|
||||
async () => {
|
||||
if (context.abortSignal?.aborted) throw new Error("Operation aborted");
|
||||
getOrThrow(await env.writeFile(absolutePath, content, context));
|
||||
if (context.abortSignal?.aborted) throw new Error("Operation aborted");
|
||||
return { output: [{ type: "text", text: `Successfully wrote to ${path}` }] };
|
||||
},
|
||||
context,
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user