f993541e0b
This release has a bunch of minor breaking changes if you are using opencode plugins or sdk 1. storage events have been removed (we might bring this back but had some issues) 2. concept of `app` is gone - there is a new concept called `project` and endpoints to list projects and get the current project 3. plugin receives `directory` which is cwd and `worktree` which is where the root of the project is if it's a git repo 4. the session.chat function has been renamed to session.prompt in sdk. it no longer requires model to be passed in (model is now an object) 5. every endpoint takes an optional `directory` parameter to operate as though opencode is running in that directory
74 lines
2.4 KiB
TypeScript
74 lines
2.4 KiB
TypeScript
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"
|
|
|
|
export const WriteTool = Tool.define("write", {
|
|
description: DESCRIPTION,
|
|
parameters: z.object({
|
|
filePath: z.string().describe("The absolute path to the file to write (must be absolute, not relative)"),
|
|
content: z.string().describe("The content to write to the file"),
|
|
}),
|
|
async execute(params, ctx) {
|
|
const filepath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath)
|
|
if (!Filesystem.contains(Instance.directory, filepath)) {
|
|
throw new Error(`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)
|
|
|
|
const agent = await Agent.get(ctx.agent)
|
|
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()
|
|
for (const [file, issues] of Object.entries(diagnostics)) {
|
|
if (issues.length === 0) continue
|
|
if (file === filepath) {
|
|
output += `\nThis file has errors, please fix\n<file_diagnostics>\n${issues.map(LSP.Diagnostic.pretty).join("\n")}\n</file_diagnostics>\n`
|
|
continue
|
|
}
|
|
output += `\n<project_diagnostics>\n${file}\n${issues.map(LSP.Diagnostic.pretty).join("\n")}\n</project_diagnostics>\n`
|
|
}
|
|
|
|
return {
|
|
title: path.relative(Instance.worktree, filepath),
|
|
metadata: {
|
|
diagnostics,
|
|
filepath,
|
|
exists: exists,
|
|
},
|
|
output,
|
|
}
|
|
},
|
|
})
|