| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import z from "zod"
- import * as path from "path"
- import { Tool } from "./tool"
- import { LSP } from "../lsp"
- import { Permission } from "../permission"
- import DESCRIPTION from "./write.txt"
- import { Bus } from "../bus"
- import { File } from "../file"
- import { FileTime } from "../file/time"
- import { Filesystem } from "../util/filesystem"
- import { Instance } from "../project/instance"
- import { Agent } from "../agent/agent"
- const MAX_DIAGNOSTICS_PER_FILE = 20
- const MAX_PROJECT_DIAGNOSTICS_FILES = 5
- export const WriteTool = Tool.define("write", {
- description: DESCRIPTION,
- parameters: z.object({
- content: z.string().describe("The content to write to the file"),
- filePath: z.string().describe("The absolute path to the file to write (must be absolute, not relative)"),
- }),
- async execute(params, ctx) {
- const agent = await Agent.get(ctx.agent)
- const filepath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath)
- if (!Filesystem.contains(Instance.directory, filepath)) {
- const parentDir = path.dirname(filepath)
- if (agent.permission.external_directory === "ask") {
- await Permission.ask({
- type: "external_directory",
- pattern: [parentDir, path.join(parentDir, "*")],
- sessionID: ctx.sessionID,
- messageID: ctx.messageID,
- callID: ctx.callID,
- title: `Write file outside working directory: ${filepath}`,
- metadata: {
- filepath,
- parentDir,
- },
- })
- } else if (agent.permission.external_directory === "deny") {
- throw new Permission.RejectedError(
- ctx.sessionID,
- "external_directory",
- ctx.callID,
- {
- filepath: filepath,
- parentDir,
- },
- `File ${filepath} is not in the current working directory`,
- )
- }
- }
- const file = Bun.file(filepath)
- const exists = await file.exists()
- if (exists) await FileTime.assert(ctx.sessionID, filepath)
- if (agent.permission.edit === "ask")
- await Permission.ask({
- type: "write",
- sessionID: ctx.sessionID,
- messageID: ctx.messageID,
- callID: ctx.callID,
- title: exists ? "Overwrite this file: " + filepath : "Create new file: " + filepath,
- metadata: {
- filePath: filepath,
- content: params.content,
- exists,
- },
- })
- await Bun.write(filepath, params.content)
- await Bus.publish(File.Event.Edited, {
- file: filepath,
- })
- FileTime.read(ctx.sessionID, filepath)
- let output = ""
- await LSP.touchFile(filepath, true)
- const diagnostics = await LSP.diagnostics()
- const normalizedFilepath = Filesystem.normalizePath(filepath)
- let projectDiagnosticsCount = 0
- for (const [file, issues] of Object.entries(diagnostics)) {
- const errors = issues.filter((item) => item.severity === 1)
- if (errors.length === 0) continue
- const limited = errors.slice(0, MAX_DIAGNOSTICS_PER_FILE)
- const suffix =
- errors.length > MAX_DIAGNOSTICS_PER_FILE ? `\n... and ${errors.length - MAX_DIAGNOSTICS_PER_FILE} more` : ""
- if (file === normalizedFilepath) {
- output += `\nThis file has errors, please fix\n<file_diagnostics>\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</file_diagnostics>\n`
- continue
- }
- if (projectDiagnosticsCount >= MAX_PROJECT_DIAGNOSTICS_FILES) continue
- projectDiagnosticsCount++
- output += `\n<project_diagnostics>\n${file}\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</project_diagnostics>\n`
- }
- return {
- title: path.relative(Instance.worktree, filepath),
- metadata: {
- diagnostics,
- filepath,
- exists: exists,
- },
- output,
- }
- },
- })
|