Compare commits

...
Author SHA1 Message Date
lukeandRoundRedDot b78fae9b9b fix(server): map OpenCode websearch as a web search with results
OpenCode names its web search tool `websearch`, but the parser only knew
`web_search`, so every OpenCode web search fell through to an unknown
tool: no query on the row, no results, and the app summed the turn up as
"used 1 other tool" instead of a search.

Map `websearch` (and keep `web_search`) to a web search detail with its
query and the printed text as `content`. OpenCode v2 prints each hit as a
`## [title](url)` heading at the start of a block; read those headings as
`webResults`, deduplicated by URL, with the URL for a missing title.

Co-authored-by: RoundRedDot <roundreddot@gmail.com>
2026-12-06 10:30:06 +00:00
3 changed files with 144 additions and 2 deletions
@@ -154,6 +154,47 @@ const OpencodeGrepOutputSchema = z
])
.nullable();
// OpenCode v2's `websearch` tool prints every hit as a `## [title](url)` heading, followed by an
// optional `Published:` line and the page text, and separates hits with a blank line. OpenCode
// substitutes the URL when a hit has no title. Only this text reaches clients; the structured hits
// stay inside OpenCode. OpenCode v1 passes the search service's own text through instead.
const OPENCODE_WEB_SEARCH_HIT = /^## \[(.*)\]\((https?:\/\/\S+)\)$/;
function parseOpencodeWebSearchHits(text: string): Array<{ title: string; url: string }> {
const webResults: Array<{ title: string; url: string }> = [];
const seen = new Set<string>();
const lines = text.split("\n");
lines.forEach((line, index) => {
if (index > 0 && lines[index - 1] !== "") {
return;
}
const match = OPENCODE_WEB_SEARCH_HIT.exec(line);
const url = match?.[2];
if (!url || seen.has(url)) {
return;
}
seen.add(url);
webResults.push({ title: match[1]?.trim() || url, url });
});
return webResults;
}
function toOpencodeWebSearchDetail(
input: z.infer<typeof ToolSearchInputSchema> | null,
output: unknown,
): ToolCallDetail | undefined {
const detail = toSearchToolDetail({ input, toolName: "web_search" });
if (detail?.type !== "search" || !detail.query || typeof output !== "string" || !output) {
return detail;
}
const webResults = parseOpencodeWebSearchHits(output);
return {
...detail,
content: output,
...(webResults.length > 0 ? { webResults } : {}),
};
}
function extractOpenCodeTaskSessionId(value: unknown): string | undefined {
const text = readOutputText(value);
if (text) {
@@ -324,8 +365,18 @@ const OpencodeKnownToolDetailSchema = z.union([
toolName: "glob",
});
}),
toolDetailBranchByToolName("web_search", ToolSearchInputSchema, z.unknown(), (input) =>
toSearchToolDetail({ input, toolName: "web_search" }),
// OpenCode names its web search tool `websearch`.
toolDetailBranchByToolName(
"websearch",
ToolSearchInputSchema,
z.unknown(),
toOpencodeWebSearchDetail,
),
toolDetailBranchByToolName(
"web_search",
ToolSearchInputSchema,
z.unknown(),
toOpencodeWebSearchDetail,
),
toolDetailBranchByToolName(
"skill",
@@ -303,6 +303,55 @@ describe("opencode tool-call mapper", () => {
});
});
it("maps OpenCode websearch hits to web results and keeps the result text", () => {
// OpenCode 2.x websearch text (packages/core/src/tool/plugin/websearch.ts): one
// `## [title](url)` heading per hit, an optional `Published:` line, then the page text.
const output = [
"## [Effect Documentation](https://effect.website/docs)\nPublished: 2026-07-25T00:00:00.000Z\n\nEffect is a TypeScript library.\n\n## [Not a hit](https://example.com/inline) inside a paragraph",
"## [Effect Duplicate](https://effect.website/docs)",
"## [https://example.com/untitled](https://example.com/untitled)",
].join("\n\n");
const item = expectMapped(
mapOpencodeToolCall({
toolName: "websearch",
callId: "opencode-websearch-1",
status: "completed",
input: { query: "effect typescript" },
output,
}),
);
expect(item.detail).toEqual({
type: "search",
query: "effect typescript",
toolName: "web_search",
content: output,
webResults: [
{ title: "Effect Documentation", url: "https://effect.website/docs" },
{ title: "https://example.com/untitled", url: "https://example.com/untitled" },
],
});
});
it("keeps OpenCode websearch text without headings as content only", () => {
const item = expectMapped(
mapOpencodeToolCall({
toolName: "websearch",
callId: "opencode-websearch-empty",
status: "completed",
input: { query: "nothing" },
output: "No search results found. Please try a different query.",
}),
);
expect(item.detail).toEqual({
type: "search",
query: "nothing",
toolName: "web_search",
content: "No search results found. Please try a different query.",
});
});
it("maps completed write calls with OpenCode success text into canonical detail", () => {
const item = expectMapped(
mapOpencodeToolCall({
@@ -104,6 +104,48 @@ test("a tool's image output is shown as an image, not as base64 in the tool outp
expect(await collectTimelineSnapshot(timeline, [screenshot])).toEqual([]);
});
test("a completed websearch lists its hits as web results", async () => {
// OpenCode 2.x websearch (packages/core/src/tool/plugin/websearch.ts) sends clients only the
// formatted text; its structured hits never reach the session message.
const text =
"## [Effect](https://effect.website)\nPublished: 2026-07-25T00:00:00.000Z\n\nparallel results\n\n## [Effect Docs](https://effect.website/docs/introduction)";
const search = assistant([
{
type: "tool",
id: "call-websearch",
name: "websearch",
executed: false,
state: {
status: "completed",
input: { query: "effect layers" },
content: [{ type: "text", text }],
metadata: { provider: "parallel" },
},
time: { created: 2, ran: 2, completed: 2 },
},
]);
expect(await collectTimelineSnapshot(new V2Timeline(), [search])).toMatchObject([
{
item: {
type: "tool_call",
name: "websearch",
status: "completed",
detail: {
type: "search",
query: "effect layers",
toolName: "web_search",
content: text,
webResults: [
{ title: "Effect", url: "https://effect.website" },
{ title: "Effect Docs", url: "https://effect.website/docs/introduction" },
],
},
},
},
]);
});
test("a surrogate pair split across deltas streams back intact", () => {
const timeline = new V2Timeline();
const part = { assistantMessageID: "answer", type: "text" as const, ordinal: 0 };