fix(core): preserve patch receipt metadata
This commit is contained in:
parent
74993a4bcf
commit
c781ef420c
4 changed files with 13 additions and 16 deletions
|
|
@ -9,8 +9,8 @@ export * as EditTool from "./edit"
|
||||||
import type { Context as PluginContext } from "@opencode-ai/plugin/v2/effect/plugin"
|
import type { Context as PluginContext } from "@opencode-ai/plugin/v2/effect/plugin"
|
||||||
import { ToolFailure } from "@opencode-ai/ai"
|
import { ToolFailure } from "@opencode-ai/ai"
|
||||||
import { FileDiff } from "@opencode-ai/schema/file-diff"
|
import { FileDiff } from "@opencode-ai/schema/file-diff"
|
||||||
import { diffLines } from "diff"
|
import { createTwoFilesPatch, diffLines } from "diff"
|
||||||
import { Effect, Schema, Struct } from "effect"
|
import { Effect, Schema } from "effect"
|
||||||
import { FileMutation } from "../file-mutation"
|
import { FileMutation } from "../file-mutation"
|
||||||
import { FSUtil } from "../fs-util"
|
import { FSUtil } from "../fs-util"
|
||||||
import { LocationMutation } from "../location-mutation"
|
import { LocationMutation } from "../location-mutation"
|
||||||
|
|
@ -31,10 +31,8 @@ export const Input = Schema.Struct({
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
const FileInfo = FileDiff.Info.mapFields(Struct.omit(["patch"]))
|
|
||||||
|
|
||||||
export const Output = Schema.Struct({
|
export const Output = Schema.Struct({
|
||||||
files: Schema.Array(FileInfo),
|
files: Schema.Array(FileDiff.Info),
|
||||||
replacements: Schema.Number,
|
replacements: Schema.Number,
|
||||||
})
|
})
|
||||||
export type Output = typeof Output.Type
|
export type Output = typeof Output.Type
|
||||||
|
|
@ -202,6 +200,7 @@ export const Plugin = {
|
||||||
files: [
|
files: [
|
||||||
{
|
{
|
||||||
file: result.resource,
|
file: result.resource,
|
||||||
|
patch: createTwoFilesPatch(result.resource, result.resource, source.text, replaced),
|
||||||
status: "modified" as const,
|
status: "modified" as const,
|
||||||
...counts,
|
...counts,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@ export * as PatchTool from "./patch"
|
||||||
import type { Context as PluginContext } from "@opencode-ai/plugin/v2/effect/plugin"
|
import type { Context as PluginContext } from "@opencode-ai/plugin/v2/effect/plugin"
|
||||||
import { ToolFailure } from "@opencode-ai/ai"
|
import { ToolFailure } from "@opencode-ai/ai"
|
||||||
import { FileDiff } from "@opencode-ai/schema/file-diff"
|
import { FileDiff } from "@opencode-ai/schema/file-diff"
|
||||||
import { diffLines } from "diff"
|
import { createTwoFilesPatch, diffLines } from "diff"
|
||||||
import { Effect, Schema, Struct } from "effect"
|
import { Effect, Schema } from "effect"
|
||||||
import { FileMutation } from "../file-mutation"
|
import { FileMutation } from "../file-mutation"
|
||||||
import { FSUtil } from "../fs-util"
|
import { FSUtil } from "../fs-util"
|
||||||
import { LocationMutation } from "../location-mutation"
|
import { LocationMutation } from "../location-mutation"
|
||||||
|
|
@ -26,11 +26,9 @@ export const Applied = Schema.Struct({
|
||||||
target: Schema.String,
|
target: Schema.String,
|
||||||
})
|
})
|
||||||
|
|
||||||
const FileInfo = FileDiff.Info.mapFields(Struct.omit(["patch"]))
|
|
||||||
|
|
||||||
export const Output = Schema.Struct({
|
export const Output = Schema.Struct({
|
||||||
applied: Schema.Array(Applied),
|
applied: Schema.Array(Applied),
|
||||||
files: Schema.Array(FileInfo),
|
files: Schema.Array(FileDiff.Info),
|
||||||
})
|
})
|
||||||
export type Output = typeof Output.Type
|
export type Output = typeof Output.Type
|
||||||
|
|
||||||
|
|
@ -213,7 +211,7 @@ export const Plugin = {
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
function patchFile(change: Prepared): typeof FileInfo.Type {
|
function patchFile(change: Prepared): typeof FileDiff.Info.Type {
|
||||||
const counts = diffLines(change.before, change.after).reduce(
|
const counts = diffLines(change.before, change.after).reduce(
|
||||||
(result, item) => ({
|
(result, item) => ({
|
||||||
additions: result.additions + (item.added ? (item.count ?? 0) : 0),
|
additions: result.additions + (item.added ? (item.count ?? 0) : 0),
|
||||||
|
|
@ -223,6 +221,7 @@ function patchFile(change: Prepared): typeof FileInfo.Type {
|
||||||
)
|
)
|
||||||
return {
|
return {
|
||||||
file: change.target.resource,
|
file: change.target.resource,
|
||||||
|
patch: createTwoFilesPatch(change.target.resource, change.target.resource, change.before, change.after),
|
||||||
status: change.type === "add" ? "added" : change.type === "delete" ? "deleted" : "modified",
|
status: change.type === "add" ? "added" : change.type === "delete" ? "deleted" : "modified",
|
||||||
...counts,
|
...counts,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -158,10 +158,10 @@ describe("EditTool", () => {
|
||||||
status: "modified",
|
status: "modified",
|
||||||
additions: 1,
|
additions: 1,
|
||||||
deletions: 1,
|
deletions: 1,
|
||||||
|
patch: expect.stringContaining("-before\n+after"),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
expect(settled.output?.structured).not.toHaveProperty("files.0.patch")
|
|
||||||
expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("after\nrest\n")
|
expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("after\nrest\n")
|
||||||
expect(assertions).toMatchObject([{ sessionID, action: "edit", resources: ["hello.txt"], save: ["*"] }])
|
expect(assertions).toMatchObject([{ sessionID, action: "edit", resources: ["hello.txt"], save: ["*"] }])
|
||||||
expect(writes).toEqual([yield* Effect.promise(() => fs.realpath(target))])
|
expect(writes).toEqual([yield* Effect.promise(() => fs.realpath(target))])
|
||||||
|
|
@ -366,7 +366,6 @@ describe("EditTool", () => {
|
||||||
Effect.andThen((settled) =>
|
Effect.andThen((settled) =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
expect(settled.output?.structured).toMatchObject({ replacements: 3 })
|
expect(settled.output?.structured).toMatchObject({ replacements: 3 })
|
||||||
expect(settled.output?.structured).not.toHaveProperty("files.0.patch")
|
|
||||||
expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("after after after")
|
expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("after after after")
|
||||||
expect(writes).toHaveLength(1)
|
expect(writes).toHaveLength(1)
|
||||||
}),
|
}),
|
||||||
|
|
|
||||||
|
|
@ -181,24 +181,24 @@ describe("PatchTool", () => {
|
||||||
status: "added",
|
status: "added",
|
||||||
additions: 1,
|
additions: 1,
|
||||||
deletions: 0,
|
deletions: 0,
|
||||||
|
patch: expect.stringContaining("+created"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
file: "update.txt",
|
file: "update.txt",
|
||||||
status: "modified",
|
status: "modified",
|
||||||
additions: 1,
|
additions: 1,
|
||||||
deletions: 1,
|
deletions: 1,
|
||||||
|
patch: expect.stringContaining("-before\n+after"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
file: "remove.txt",
|
file: "remove.txt",
|
||||||
status: "deleted",
|
status: "deleted",
|
||||||
additions: 0,
|
additions: 0,
|
||||||
deletions: 1,
|
deletions: 1,
|
||||||
|
patch: expect.stringContaining("-remove"),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
expect(settled.output?.structured).not.toHaveProperty("files.0.patch")
|
|
||||||
expect(settled.output?.structured).not.toHaveProperty("files.1.patch")
|
|
||||||
expect(settled.output?.structured).not.toHaveProperty("files.2.patch")
|
|
||||||
expect(assertions).toMatchObject([
|
expect(assertions).toMatchObject([
|
||||||
{ sessionID, action: "edit", resources: ["nested/new.txt", "update.txt", "remove.txt"], save: ["*"] },
|
{ sessionID, action: "edit", resources: ["nested/new.txt", "update.txt", "remove.txt"], save: ["*"] },
|
||||||
])
|
])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue