Skip to content

Commit 513097f

Browse files
johan vazcodex
authored andcommitted
fix(opencode): preserve empty file edit text
OpenCode drops empty and whitespace-only file content or selects a later alias, losing deletion replacements and empty writes when no usable patch is available. Read exact strings only for oldStr/newStr while keeping other string validation. Co-Authored-By: GPT-6.1 Sol (Codex) <[email protected]>
1 parent 567a105 commit 513097f

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎packages/provider-opencode/src/server/toolItems.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { ThreadId, TurnItemId } from "@t3tools/contracts";
2+
import * as DateTime from "effect/DateTime";
13
import { describe, expect, it } from "vite-plus/test";
24
import {
35
MAX_TOOL_OUTPUT_IMAGES,
46
MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH,
57
toolOutputImages,
68
} from "@t3tools/shared/toolOutput";
79

8-
import { openCodeToolOutput } from "./toolItems.ts";
10+
import { openCodeToolOutput, openCodeToolTurnItem } from "./toolItems.ts";
911

1012
describe("openCodeToolOutput", () => {
1113
it("preserves inline raster file content and leaves other files unserved", () => {
@@ -41,3 +43,52 @@ describe("openCodeToolOutput", () => {
4143
expect(images[1]?.data).toBe("AQ==");
4244
});
4345
});
46+
47+
describe("openCodeToolTurnItem", () => {
48+
it.each([
49+
{
50+
name: "deletion",
51+
toolName: "edit",
52+
input: { oldString: "old\n", newString: "", newText: "fallback" },
53+
expected: { oldStr: "old\n", newStr: "" },
54+
},
55+
{
56+
name: "whitespace replacement",
57+
toolName: "edit",
58+
input: { oldString: " \t", newString: "\n\t" },
59+
expected: { oldStr: " \t", newStr: "\n\t" },
60+
},
61+
{
62+
name: "empty write",
63+
toolName: "write",
64+
input: { content: "" },
65+
expected: { newStr: "" },
66+
},
67+
])("preserves exact file content for $name", ({ toolName, input, expected }) => {
68+
const item = openCodeToolTurnItem(
69+
{
70+
id: TurnItemId.make("tool-item"),
71+
threadId: ThreadId.make("thread"),
72+
runId: null,
73+
nodeId: null,
74+
providerThreadId: null,
75+
providerTurnId: null,
76+
nativeItemRef: null,
77+
parentItemId: null,
78+
ordinal: 0,
79+
status: "completed",
80+
title: toolName,
81+
startedAt: null,
82+
completedAt: null,
83+
updatedAt: DateTime.makeUnsafe("2026-11-04T00:00:00Z"),
84+
},
85+
{
86+
name: toolName,
87+
input: { filePath: "src/index.ts", ...input },
88+
output: undefined,
89+
completedMetadata: undefined,
90+
},
91+
);
92+
expect(item).toMatchObject({ type: "file_change", fileName: "src/index.ts", ...expected });
93+
});
94+
});

‎packages/provider-opencode/src/server/toolItems.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ function recordString(input: unknown, ...keys: ReadonlyArray<string>): string |
7373
return undefined;
7474
}
7575

76+
function recordText(input: Record<string, unknown>, ...keys: ReadonlyArray<string>) {
77+
for (const key of keys) {
78+
const value = input[key];
79+
if (typeof value === "string") return value;
80+
}
81+
return undefined;
82+
}
83+
7684
function recordNumber(input: unknown, ...keys: ReadonlyArray<string>): number | undefined {
7785
if (typeof input !== "object" || input === null) return undefined;
7886
for (const key of keys) {
@@ -137,8 +145,8 @@ export function openCodeToolTurnItem(
137145
};
138146
}
139147
case "file_change": {
140-
const oldStr = recordString(input, "oldString", "oldText");
141-
const newStr = recordString(input, "newString", "content", "newText");
148+
const oldStr = recordText(input, "oldString", "oldText");
149+
const newStr = recordText(input, "newString", "content", "newText");
142150
const diffStr =
143151
recordString(tool.completedMetadata, "diff", "patch") ??
144152
(base.status === "failed" && output?.trim() ? output : undefined);

0 commit comments

Comments
 (0)