Skip to content
TypeScript

fix(opencode): edits that delete text keep their empty replacement (#30643)

Johan Vaz28aed0dcb986
diff --git a/packages/provider-opencode/src/server/toolItems.test.ts b/packages/provider-opencode/src/server/toolItems.test.ts
index 8e043879b08..28bcf889291 100644
--- a/packages/provider-opencode/src/server/toolItems.test.ts
+++ b/packages/provider-opencode/src/server/toolItems.test.ts
@@ -1,3 +1,5 @@
+import { ThreadId, TurnItemId } from "@t3tools/contracts";
+import * as DateTime from "effect/DateTime";
import { describe, expect, it } from "vite-plus/test";
import {
MAX_TOOL_OUTPUT_IMAGES,
@@ -5,7 +7,7 @@ import {
toolOutputImages,
} from "@t3tools/shared/toolOutput";
-import { openCodeToolOutput } from "./toolItems.ts";
+import { openCodeToolOutput, openCodeToolTurnItem } from "./toolItems.ts";
describe("openCodeToolOutput", () => {
it("preserves inline raster file content and leaves other files unserved", () => {
@@ -41,3 +43,52 @@ describe("openCodeToolOutput", () => {
expect(images[1]?.data).toBe("AQ==");
});
});
+
+describe("openCodeToolTurnItem", () => {
+ it.each([
+ {
+ name: "deletion",
+ toolName: "edit",
+ input: { oldString: "old\n", newString: "", newText: "fallback" },
+ expected: { oldStr: "old\n", newStr: "" },
+ },
+ {
+ name: "whitespace replacement",
+ toolName: "edit",
+ input: { oldString: " \t", newString: "\n\t" },
+ expected: { oldStr: " \t", newStr: "\n\t" },
+ },
+ {
+ name: "empty write",
+ toolName: "write",
+ input: { content: "" },
+ expected: { newStr: "" },
+ },
+ ])("preserves exact file content for $name", ({ toolName, input, expected }) => {
+ const item = openCodeToolTurnItem(
+ {
+ id: TurnItemId.make("tool-item"),
+ threadId: ThreadId.make("thread"),
+ runId: null,
+ nodeId: null,
+ providerThreadId: null,
+ providerTurnId: null,
+ nativeItemRef: null,
+ parentItemId: null,
+ ordinal: 0,
+ status: "completed",
+ title: toolName,
+ startedAt: null,
+ completedAt: null,
+ updatedAt: DateTime.makeUnsafe("2026-11-04T00:00:00Z"),
+ },
+ {
+ name: toolName,
+ input: { filePath: "src/index.ts", ...input },
+ output: undefined,
+ completedMetadata: undefined,
+ },
+ );
+ expect(item).toMatchObject({ type: "file_change", fileName: "src/index.ts", ...expected });
+ });
+});
diff --git a/packages/provider-opencode/src/server/toolItems.ts b/packages/provider-opencode/src/server/toolItems.ts
index f651d6465b7..606788f0885 100644
--- a/packages/provider-opencode/src/server/toolItems.ts
+++ b/packages/provider-opencode/src/server/toolItems.ts
@@ -73,6 +73,14 @@ function recordString(input: unknown, ...keys: ReadonlyArray<string>): string |
return undefined;
}
+function recordText(input: Record<string, unknown>, ...keys: ReadonlyArray<string>) {
+ for (const key of keys) {
+ const value = input[key];
+ if (typeof value === "string") return value;
+ }
+ return undefined;
+}
+
function recordNumber(input: unknown, ...keys: ReadonlyArray<string>): number | undefined {
if (typeof input !== "object" || input === null) return undefined;
for (const key of keys) {
@@ -137,8 +145,8 @@ export function openCodeToolTurnItem(
};
}
case "file_change": {
- const oldStr = recordString(input, "oldString", "oldText");
- const newStr = recordString(input, "newString", "content", "newText");
+ const oldStr = recordText(input, "oldString", "oldText");
+ const newStr = recordText(input, "newString", "content", "newText");
const diffStr =
recordString(tool.completedMetadata, "diff", "patch") ??
(base.status === "failed" && output?.trim() ? output : undefined);