@devries-cameron20 / t3code Mirror · read-only / commits / cfbfbca6e0
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 00:30 UTC

fix(provider-opencode): render native reads without XML envelopes (#21507)

Julius Marminge committed Oct 19, 2026 at 16:18 UTC cfbfbca6e0582fb48423949b10f0663f8fed2be0
2 files changed +59 -9
packages/provider-opencode/src/server/adapter.test.ts
+43 -8
@@ -902,8 +902,16 @@ describe("OpenCodeAdapterV2", () => {
902 ),
903 );
904
905 - it.effect("titles OpenCode reads and searches from their input", () =>
905 + it.effect("projects OpenCode read output and search titles from native tool calls", () =>
906 Effect.gen(function* () {
907 + const directoryOutput =
908 + "<path>/workspace/src</path>\n<type>directory</type>\n<entries>\nenv.ts\nlib/\n\n(2 entries)\n</entries>";
909 + const fileOutput =
910 + "<path>/workspace/src/env.ts</path>\n<type>file</type>\n<content>\n1: hello\n2: </content> literal\n3: world\n</content>";
911 + const fileWithInstructions =
912 + "<path>/workspace/src/env.ts</path>\n<type>file</type>\n<content>1: hello\n\n(End of file - total 1 lines)\n</content>\n\n<system-reminder>\nProject instructions\n</system-reminder>";
913 + const missingPath = directoryOutput.slice(directoryOutput.indexOf("<type>"));
914 + const missingClose = fileOutput.slice(0, fileOutput.lastIndexOf("\n</content>"));
915 const nativeEvents = asyncEventStream();
916 const nativeSessionId = "native-opencode-search";
917 const harness = yield* makeOpenCodeRuntimeHarness("search-projection", nativeSessionId, {
@@ -913,6 +921,7 @@ describe("OpenCodeAdapterV2", () => {
921 return { stream: nativeEvents.stream };
922 },
923 },
924 + mcp: { status: async () => ({ data: {} }) },
925 session: {
926 create: async () => ({
927 data: { id: nativeSessionId, time: { created: 1, updated: 1 } },
@@ -928,24 +937,31 @@ describe("OpenCodeAdapterV2", () => {
937 Stream.runCollect,
938 Effect.forkScoped,
939 );
931 - for (const [tool, input, output] of [
940 + const toolCalls = [
941 ["read", { filePath: "src/env.ts" }, "---\nfile body"],
942 + ["read", { filePath: "/workspace/src" }, directoryOutput],
943 + ["read", { filePath: "/workspace/src/env.ts" }, fileOutput],
944 + ["read", { filePath: "/workspace/src/env.ts" }, fileWithInstructions],
945 + ["read", { filePath: "/workspace/src" }, missingPath],
946 + ["read", { filePath: "/workspace/src/env.ts" }, missingClose],
947 + ["custom_read", { filePath: "/workspace/src" }, directoryOutput],
948 ["grep", { pattern: "TODO", path: "apps/web" }, "---\nfile body"],
949 ["websearch", { query: "OpenCode documentation" }, "---\nfile body"],
950 ["glob", { pattern: "missing", path: "apps/web" }, ""],
951 ["codesearch", {}, " \n\t"],
937 - ] as const) {
952 + ] as const;
953 + for (const [index, [tool, input, output]] of toolCalls.entries()) {
954 yield* Effect.promise(() =>
955 nativeEvents.push({
956 type: "message.part.updated",
957 properties: {
958 sessionID: nativeSessionId,
959 part: {
944 - id: `part-${tool}`,
960 + id: `part-${index}`,
961 sessionID: nativeSessionId,
962 messageID: "assistant-search",
963 type: "tool",
948 - callID: `call-${tool}`,
964 + callID: `call-${index}`,
965 tool,
966 state: {
967 status: "completed",
@@ -969,8 +985,24 @@ describe("OpenCodeAdapterV2", () => {
985 const items = (yield* Fiber.join(received)).flatMap((event) =>
986 event.type === "turn_item.updated" ? [event.turnItem] : [],
987 );
972 - const read = items.find((item) => item.type === "dynamic_tool");
973 - assert.equal(read?.title, "Read src/env.ts");
988 + const reads = items.filter((item) => item.type === "dynamic_tool");
989 + assert.equal(reads[0]?.title, "Read src/env.ts");
990 + assert.deepEqual(
991 + reads.map((item) => ({ toolName: item.toolName, output: item.output })),
992 + [
993 + { toolName: "read", output: "---\nfile body" },
994 + { toolName: "read", output: "env.ts\nlib/\n\n(2 entries)" },
995 + { toolName: "read", output: "1: hello\n2: </content> literal\n3: world" },
996 + {
997 + toolName: "read",
998 + output:
999 + "1: hello\n\n(End of file - total 1 lines)\n\n<system-reminder>\nProject instructions\n</system-reminder>",
1000 + },
1001 + { toolName: "read", output: missingPath },
1002 + { toolName: "read", output: missingClose },
1003 + { toolName: "custom_read", output: directoryOutput },
1004 + ],
1005 + );
1006 const grep = items.find((item) => item.type === "file_search");
1007 assert.equal(grep?.title, "Searched TODO in web");
1008 assert.equal(grep?.type === "file_search" ? grep.pattern : null, "TODO");
@@ -1002,6 +1034,8 @@ describe("OpenCodeAdapterV2", () => {
1034
1035 it.effect("presents OpenCode MCP calls without treating remote tools as local edits", () =>
1036 Effect.gen(function* () {
1037 + const output =
1038 + "<path>/workspace/src</path>\n<type>directory</type>\n<entries>\nenv.ts\nlib/\n\n(2 entries)\n</entries>";
1039 const nativeEvents = asyncEventStream();
1040 const nativeSessionId = "native-opencode-mcp";
1041 let statusReads = 0;
@@ -1057,7 +1091,7 @@ describe("OpenCodeAdapterV2", () => {
1091 input: { document: "remote-doc" },
1092 title: "Edit remote document",
1093 metadata: {},
1060 - output: "saved",
1094 + output,
1095 error: "failed",
1096 time: { start: 1, end: 2 },
1097 },
@@ -1150,6 +1184,7 @@ describe("OpenCodeAdapterV2", () => {
1184 });
1185 assert.deepEqual(item.input, { document: "remote-doc" });
1186 }
1187 + assert.equal(items[1]?.output, output);
1188 assert.equal(items[3]?.toolName, "ambiguous_server_edit_document");
1189 assert.isUndefined(items[3]?.toolSource);
1190 assert.equal(statusReads, 1);
packages/provider-opencode/src/server/toolItems.ts
+16 -1
@@ -66,6 +66,19 @@ function stableJson(value: unknown): string {
66 }
67 }
68
69 +function nativeReadOutput(output: string): string {
70 + const header = /^<path>[^\r\n]+<\/path>\r?\n<type>(directory|file)<\/type>\r?\n/.exec(output);
71 + if (header === null) return output;
72 + const body = output.slice(header[0].length);
73 + // File lines are numbered; only a closing tag on its own line ends the envelope.
74 + const envelope =
75 + header[1] === "directory"
76 + ? /^<entries>\r?\n([\s\S]*?)\r?\n<\/entries>(?=\r?\n|$)/.exec(body)
77 + : /^<content>(?:\r?\n)?([\s\S]*?)\r?\n<\/content>(?=\r?\n|$)/.exec(body);
78 + // OpenCode can append instructions after the envelope; keep that suffix verbatim.
79 + return envelope === null ? output : envelope[1] + body.slice(envelope[0].length);
80 +}
81 +
82 export function openCodeToolTurnItem(
83 base: ToolItemBase,
84 tool: {
@@ -142,6 +155,8 @@ export function openCodeToolTurnItem(
155 }
156 case "dynamic_tool": {
157 const readPath = recordString(input, "filePath", "path", "file");
158 + const cleanedOutput =
159 + tool.name === "read" && output !== undefined ? nativeReadOutput(output) : output;
160 return {
161 ...base,
162 title:
@@ -151,7 +166,7 @@ export function openCodeToolTurnItem(
166 type: "dynamic_tool",
167 toolName: tool.name,
168 input,
154 - ...(output === undefined ? {} : { output }),
169 + ...(cleanedOutput === undefined ? {} : { output: cleanedOutput }),
170 };
171 }
172 }