Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions next/src/lib/agents/__tests__/argv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,39 @@ describe("parseLine opencode", () => {
});
});

describe("parseLine copilot", () => {
it("emits a single delta when response and text repeat one payload", () => {
const line = JSON.stringify({
response: "<html><body>Hello</body></html>",
text: "<html><body>Hello</body></html>",
});

expect(parseLine("copilot", line)).toEqual([
{
kind: "delta",
text: "<html><body>Hello</body></html>",
},
]);
});
});

describe("parseLine qwen", () => {
it("emits a single delta when text, content, and message repeat one payload", () => {
const line = JSON.stringify({
text: "<html><body>Hello</body></html>",
content: "<html><body>Hello</body></html>",
message: "<html><body>Hello</body></html>",
});

expect(parseLine("qwen", line)).toEqual([
{
kind: "delta",
text: "<html><body>Hello</body></html>",
},
]);
});
});

describe("parseLine bob", () => {
it("extracts text from stream-json output", () => {
const line = JSON.stringify({
Expand Down Expand Up @@ -203,6 +236,34 @@ describe("parseLine bob", () => {
]);
});

it("emits a single delta when text, content, and message repeat one payload", () => {
// Some CLIs echo the turn payload under several keys at once. Emitting
// one delta per key appends the same HTML two or three times.
const line = JSON.stringify({
text: "<html><body>Hello</body></html>",
content: "<html><body>Hello</body></html>",
message: "<html><body>Hello</body></html>",
});

expect(parseLine("bob", line)).toEqual([
{
kind: "delta",
text: "<html><body>Hello</body></html>",
},
]);
});

it("falls back to the next populated field when the first is empty", () => {
const line = JSON.stringify({ text: "", content: "<html><body>Hi</body></html>" });

expect(parseLine("bob", line)).toEqual([
{
kind: "delta",
text: "<html><body>Hi</body></html>",
},
]);
});

it("handles final answer after thinking when --hide-intermediary-output is used", () => {
// When --hide-intermediary-output is enabled, Bob only emits the final answer.
// This test verifies that the parser correctly handles the final completion.
Expand Down
29 changes: 21 additions & 8 deletions next/src/lib/agents/argv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,13 @@ function parseLineWithState(agent: string, line: string, state: ParseState): Age
}

if (agent === "copilot") {
if (typeof obj.response === "string") out.push({ kind: "delta", text: obj.response });
if (typeof obj.text === "string") out.push({ kind: "delta", text: obj.text });
// Copilot can echo the turn payload under both `response` and `text`.
// Emit a single delta (first populated field wins), like the opencode
// single-pick below — otherwise the same HTML is appended twice.
const text = [obj.response, obj.text].find(
(value): value is string => typeof value === "string" && value.length > 0,
);
if (text) out.push({ kind: "delta", text });
}

if (agent === "opencode") {
Expand Down Expand Up @@ -417,15 +422,23 @@ function parseLineWithState(agent: string, line: string, state: ParseState): Age
}

if (agent === "qwen") {
if (typeof obj.text === "string") out.push({ kind: "delta", text: obj.text });
if (typeof obj.content === "string") out.push({ kind: "delta", text: obj.content });
if (typeof obj.message === "string") out.push({ kind: "delta", text: obj.message });
// Qwen can echo the turn payload under `text`, `content`, and `message`
// at once. Emit a single delta (first populated field wins), like the
// opencode single-pick above — otherwise the same HTML is appended up to
// three times.
const text = [obj.text, obj.content, obj.message].find(
(value): value is string => typeof value === "string" && value.length > 0,
);
if (text) out.push({ kind: "delta", text });
}

if (agent === "bob") {
if (typeof obj.text === "string") out.push({ kind: "delta", text: obj.text });
if (typeof obj.content === "string") out.push({ kind: "delta", text: obj.content });
if (typeof obj.message === "string") out.push({ kind: "delta", text: obj.message });
// Same single-pick as qwen: Bob repeats the completion across `text`,
// `content`, and `message` on some stream-json lines.
const text = [obj.text, obj.content, obj.message].find(
(value): value is string => typeof value === "string" && value.length > 0,
);
if (text) out.push({ kind: "delta", text });
}

if (agent === "qoder") {
Expand Down