mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
refactor(core): share the authorized file write between write and edit (#61150)
This commit is contained in:
committed by
GitHub
Unverified
parent
0d73f08f28
commit
f1b0c27081
@@ -0,0 +1,113 @@
|
||||
export * as FileWrite from "./file-write.js"
|
||||
|
||||
import { makeLocationNode } from "@opencode/util/effect/app-node"
|
||||
import type { FileDiff } from "@opencode/schema/file-diff"
|
||||
import { Bom } from "@opencode/util/bom"
|
||||
import { Context, Effect, Layer } from "effect"
|
||||
import { Environment } from "./environment/index.js"
|
||||
import { FileAccess } from "./file-access.js"
|
||||
import { FileMutation } from "./file-mutation.js"
|
||||
import { Formatter } from "./formatter.js"
|
||||
import { Permission } from "./permission.js"
|
||||
import type { SessionErrors } from "./session/error.js"
|
||||
import { fileDiff } from "./tool/plugin/file-diff.js"
|
||||
|
||||
export interface WriteInput {
|
||||
readonly path: string
|
||||
readonly content: string
|
||||
}
|
||||
|
||||
export interface Written extends FileMutation.TextWriteResult {
|
||||
/** The file text after a formatter ran on it. */
|
||||
readonly formatted?: string
|
||||
}
|
||||
|
||||
export interface Interface {
|
||||
/** Resolve a file to write and obtain external-directory approval. */
|
||||
readonly target: (
|
||||
file: string,
|
||||
context: FileAccess.Invocation,
|
||||
) => Effect.Effect<FileAccess.Target, Environment.Failed | Permission.Error | SessionErrors.NotFoundError>
|
||||
/** Request edit approval for a resolved target, showing the change when it is known. */
|
||||
readonly authorize: (
|
||||
target: FileAccess.Target,
|
||||
context: FileAccess.Invocation,
|
||||
preview?: FileDiff.Info,
|
||||
) => Effect.Effect<void, Permission.Error | SessionErrors.NotFoundError>
|
||||
/** Write approved text, keeping an existing UTF-8 BOM, then format the file. */
|
||||
readonly commit: (
|
||||
target: FileAccess.Target,
|
||||
content: string,
|
||||
) => Effect.Effect<Written, Environment.NotFound | Environment.WrongKind | Environment.Failed>
|
||||
/** Resolve, approve, write and format a whole file. */
|
||||
readonly write: (
|
||||
input: WriteInput,
|
||||
context: FileAccess.Invocation,
|
||||
) => Effect.Effect<
|
||||
Written,
|
||||
Environment.NotFound | Environment.WrongKind | Environment.Failed | Permission.Error | SessionErrors.NotFoundError
|
||||
>
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Interface>()("@opencode/FileWrite") {}
|
||||
|
||||
const layer = Layer.effect(
|
||||
Service,
|
||||
Effect.gen(function* () {
|
||||
const access = yield* FileAccess.Service
|
||||
const environment = yield* Environment.Service
|
||||
const fileMutation = yield* FileMutation.Service
|
||||
const formatter = yield* Formatter.Service
|
||||
const permission = yield* Permission.Service
|
||||
|
||||
const target = Effect.fn("FileWrite.target")(function* (file: string, context: FileAccess.Invocation) {
|
||||
const target = yield* access.resolve({ path: file, kind: "file" })
|
||||
yield* access.authorizeExternal([target], context)
|
||||
return target
|
||||
})
|
||||
|
||||
const authorize = Effect.fn("FileWrite.authorize")(function* (
|
||||
target: FileAccess.Target,
|
||||
context: FileAccess.Invocation,
|
||||
preview?: FileDiff.Info,
|
||||
) {
|
||||
yield* permission.assert({
|
||||
action: "edit",
|
||||
resources: [target.resource],
|
||||
save: ["*"],
|
||||
metadata: preview ? { files: [preview] } : undefined,
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: { type: "tool", messageID: context.messageID, id: context.id },
|
||||
})
|
||||
})
|
||||
|
||||
const commit = Effect.fn("FileWrite.commit")(function* (target: FileAccess.Target, content: string) {
|
||||
const written = yield* fileMutation.writeTextPreservingBom({ target, content })
|
||||
if (!(yield* formatter.file(target.absolute))) return written
|
||||
return {
|
||||
...written,
|
||||
formatted: yield* FileMutation.syncTextBom(environment.files, target.absolute, written.bom),
|
||||
} satisfies Written
|
||||
})
|
||||
|
||||
const write = Effect.fn("FileWrite.write")(function* (input: WriteInput, context: FileAccess.Invocation) {
|
||||
const resolved = yield* target(input.path, context)
|
||||
const current = yield* FileMutation.readOptionalText(environment.files, resolved.absolute)
|
||||
yield* authorize(
|
||||
resolved,
|
||||
context,
|
||||
fileDiff(resolved.resource, current?.text ?? "", Bom.split(input.content).text, current ? "modified" : "added"),
|
||||
)
|
||||
return yield* commit(resolved, input.content)
|
||||
})
|
||||
|
||||
return Service.of({ target, authorize, commit, write })
|
||||
}),
|
||||
)
|
||||
|
||||
export const node = makeLocationNode({
|
||||
service: Service,
|
||||
layer,
|
||||
deps: [Environment.node, FileAccess.node, FileMutation.node, Formatter.node, Permission.node],
|
||||
})
|
||||
@@ -9,6 +9,7 @@ import { Config } from "./config.js"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
import { Node } from "@opencode/util/effect/app-node"
|
||||
import { FileMutation } from "./file-mutation.js"
|
||||
import { FileWrite } from "./file-write.js"
|
||||
import { Environment } from "./environment/index.js"
|
||||
import { Formatter } from "./formatter.js"
|
||||
import { FileSystem } from "./filesystem.js"
|
||||
@@ -89,6 +90,7 @@ const nodes = [
|
||||
InstructionDiscovery.node,
|
||||
FileAccess.node,
|
||||
FileMutation.node,
|
||||
FileWrite.node,
|
||||
Formatter.node,
|
||||
Mcp.node,
|
||||
Permission.node,
|
||||
|
||||
@@ -39,6 +39,7 @@ import { Bus } from "../bus.js"
|
||||
import { Environment } from "../environment/index.js"
|
||||
import { FileAccess } from "../file-access.js"
|
||||
import { FileMutation } from "../file-mutation.js"
|
||||
import { FileWrite } from "../file-write.js"
|
||||
import { Formatter } from "../formatter.js"
|
||||
import { Generate } from "../generate.js"
|
||||
import { Form } from "../form.js"
|
||||
@@ -127,6 +128,7 @@ const services = [
|
||||
Environment.Service,
|
||||
FileAccess.Service,
|
||||
FileMutation.Service,
|
||||
FileWrite.Service,
|
||||
Formatter.Service,
|
||||
Generate.Service,
|
||||
LocationWatcherPolicy.Service,
|
||||
@@ -184,6 +186,7 @@ export const requirements = LayerNode.group([
|
||||
Environment.node,
|
||||
FileAccess.node,
|
||||
FileMutation.node,
|
||||
FileWrite.node,
|
||||
Formatter.node,
|
||||
Generate.node,
|
||||
LocationWatcherPolicy.node,
|
||||
|
||||
@@ -9,14 +9,13 @@ export * as EditTool from "./edit.js"
|
||||
import type { Context } from "@opencode/plugin/effect/plugin"
|
||||
import { ToolFailure } from "@opencode/ai/effect"
|
||||
import { FileDiff } from "@opencode/schema/file-diff"
|
||||
import { Bom } from "@opencode/util/bom"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { toolFailure } from "../runtime.js"
|
||||
import { Environment } from "../../environment/index.js"
|
||||
import { FileMutation } from "../../file-mutation.js"
|
||||
import { Formatter } from "../../formatter.js"
|
||||
import { Location } from "../../location.js"
|
||||
import { FileAccess } from "../../file-access.js"
|
||||
import { FileWrite } from "../../file-write.js"
|
||||
import { Permission } from "../../permission.js"
|
||||
import { fileDiff } from "./file-diff.js"
|
||||
|
||||
@@ -261,10 +260,9 @@ const countBefore = (positions: number[], offset: number) => {
|
||||
export const Plugin = {
|
||||
id: "opencode.tool.edit",
|
||||
effect: Effect.fn("EditTool.Plugin")(function* (ctx: Context) {
|
||||
const access = yield* FileAccess.Service
|
||||
const fileMutation = yield* FileMutation.Service
|
||||
const fileWrite = yield* FileWrite.Service
|
||||
const environment = yield* Environment.Service
|
||||
const formatter = yield* Formatter.Service
|
||||
const location = yield* Location.Service
|
||||
const permission = yield* Permission.Service
|
||||
|
||||
@@ -295,8 +293,7 @@ export const Plugin = {
|
||||
})
|
||||
}
|
||||
|
||||
const target = yield* access.resolve({ path: input.path, kind: "file" })
|
||||
yield* access.authorizeExternal([target], context)
|
||||
const target = yield* fileWrite.target(input.path, context)
|
||||
|
||||
const original = yield* FileMutation.readText(environment.files, target.absolute).pipe(
|
||||
Effect.catchTag("Environment.NotFound", () =>
|
||||
@@ -326,15 +323,7 @@ export const Plugin = {
|
||||
replacements > 0 && (replacements === 1 || input.replaceAll === true)
|
||||
? fileDiff(target.resource, source, replaced)
|
||||
: undefined
|
||||
yield* permission.assert({
|
||||
action: "edit",
|
||||
resources: [target.resource],
|
||||
save: ["*"],
|
||||
metadata: preview ? { files: [preview] } : undefined,
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source: permissionSource,
|
||||
})
|
||||
yield* fileWrite.authorize(target, context, preview)
|
||||
if (replacements === 0) {
|
||||
// The hint shows file contents, so only when reading this file is already allowed.
|
||||
const readable = yield* permission.allows({
|
||||
@@ -362,15 +351,9 @@ export const Plugin = {
|
||||
: `No changes to apply: the edit leaves ${input.path} unchanged. Line endings follow the file; to change them, write \\r explicitly in oldString or newString.`,
|
||||
})
|
||||
}
|
||||
const replacementBom = replaced.startsWith("\uFEFF")
|
||||
const result = yield* fileMutation.write({
|
||||
target,
|
||||
content: Bom.join(replaced, original.bom || replacementBom),
|
||||
})
|
||||
const bom = original.bom || replacementBom
|
||||
const formatted = (yield* formatter.file(target.absolute))
|
||||
? yield* FileMutation.syncTextBom(environment.files, target.absolute, bom)
|
||||
: (yield* FileMutation.readText(environment.files, target.absolute)).text
|
||||
const result = yield* fileWrite.commit(target, replaced)
|
||||
const formatted =
|
||||
result.formatted ?? (yield* FileMutation.readText(environment.files, target.absolute)).text
|
||||
return {
|
||||
files: [fileDiff(result.resource, source, formatted)],
|
||||
replacements,
|
||||
|
||||
@@ -9,13 +9,7 @@ export * as WriteTool from "./write.js"
|
||||
import type { Context } from "@opencode/plugin/effect/plugin"
|
||||
import { ToolFailure } from "@opencode/ai/effect"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { Bom } from "@opencode/util/bom"
|
||||
import { Environment } from "../../environment/index.js"
|
||||
import { FileMutation } from "../../file-mutation.js"
|
||||
import { Formatter } from "../../formatter.js"
|
||||
import { FileAccess } from "../../file-access.js"
|
||||
import { Permission } from "../../permission.js"
|
||||
import { fileDiff } from "./file-diff.js"
|
||||
import { FileWrite } from "../../file-write.js"
|
||||
|
||||
export const name = "write"
|
||||
|
||||
@@ -46,11 +40,7 @@ export const toModelContent = (output: Output) =>
|
||||
export const Plugin = {
|
||||
id: "opencode.tool.write",
|
||||
effect: Effect.fn("WriteTool.Plugin")(function* (ctx: Context) {
|
||||
const access = yield* FileAccess.Service
|
||||
const fileMutation = yield* FileMutation.Service
|
||||
const environment = yield* Environment.Service
|
||||
const formatter = yield* Formatter.Service
|
||||
const permission = yield* Permission.Service
|
||||
const fileWrite = yield* FileWrite.Service
|
||||
|
||||
yield* ctx.tool
|
||||
.transform((editor) =>
|
||||
@@ -63,29 +53,7 @@ export const Plugin = {
|
||||
output: Output,
|
||||
execute: (input, context) =>
|
||||
Effect.gen(function* () {
|
||||
const source = {
|
||||
type: "tool" as const,
|
||||
messageID: context.messageID,
|
||||
id: context.id,
|
||||
}
|
||||
const target = yield* access.resolve({ path: input.path, kind: "file" })
|
||||
yield* access.authorizeExternal([target], context)
|
||||
const current = yield* FileMutation.readOptionalText(environment.files, target.absolute)
|
||||
const next = Bom.split(input.content)
|
||||
const preview = fileDiff(target.resource, current?.text ?? "", next.text, current ? "modified" : "added")
|
||||
yield* permission.assert({
|
||||
action: "edit",
|
||||
resources: [target.resource],
|
||||
save: ["*"],
|
||||
metadata: { files: [preview] },
|
||||
sessionID: context.sessionID,
|
||||
agent: context.agent,
|
||||
source,
|
||||
})
|
||||
const written = yield* fileMutation.writeTextPreservingBom({ target, content: input.content })
|
||||
if (yield* formatter.file(target.absolute)) {
|
||||
yield* FileMutation.syncTextBom(environment.files, target.absolute, written.bom)
|
||||
}
|
||||
const written = yield* fileWrite.write(input, context)
|
||||
return {
|
||||
operation: written.operation,
|
||||
target: written.target,
|
||||
|
||||
@@ -6,6 +6,7 @@ import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
import { Environment } from "@opencode/core/environment/index"
|
||||
import { FileMutation } from "@opencode/core/file-mutation"
|
||||
import { FileWrite } from "@opencode/core/file-write"
|
||||
import { Formatter } from "@opencode/core/formatter"
|
||||
import { Location } from "@opencode/core/location"
|
||||
import { FileAccess } from "@opencode/core/file-access"
|
||||
@@ -25,15 +26,7 @@ import { toolIdentity, executeTool, registerToolPlugin, toolDefinitions } from "
|
||||
const editToolNode = makeLocationNode({
|
||||
name: "test/edit-tool-plugin",
|
||||
layer: Layer.effectDiscard(registerToolPlugin(EditTool.Plugin)),
|
||||
deps: [
|
||||
Tool.node,
|
||||
FileAccess.node,
|
||||
FileMutation.node,
|
||||
Environment.node,
|
||||
Formatter.node,
|
||||
Location.node,
|
||||
Permission.node,
|
||||
],
|
||||
deps: [Tool.node, FileWrite.node, FileMutation.node, Environment.node, Location.node, Permission.node],
|
||||
})
|
||||
|
||||
const sessionID = Session.ID.make("ses_edit_tool_test")
|
||||
|
||||
@@ -3,6 +3,7 @@ import path from "path"
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Effect, Exit, Layer, Tracer } from "effect"
|
||||
import { FileMutation } from "@opencode/core/file-mutation"
|
||||
import { FileWrite } from "@opencode/core/file-write"
|
||||
import { Formatter } from "@opencode/core/formatter"
|
||||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
@@ -25,7 +26,7 @@ import { toolIdentity, executeTool, registerToolPlugin, toolDefinitions } from "
|
||||
const writeToolNode = makeLocationNode({
|
||||
name: "test/write-tool-plugin",
|
||||
layer: Layer.effectDiscard(registerToolPlugin(WriteTool.Plugin)),
|
||||
deps: [Tool.node, FileAccess.node, FileMutation.node, Environment.node, Formatter.node, Permission.node],
|
||||
deps: [Tool.node, FileWrite.node],
|
||||
})
|
||||
|
||||
const sessionID = Session.ID.make("ses_write_tool_test")
|
||||
|
||||
Reference in New Issue
Block a user