File size: 9,371 Bytes
bc575bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | import type { AssistantMessage } from "@earendil-works/pi-ai";
import type { TuiMouseEvent } from "@earendil-works/pi-tui";
import { describe, expect, test } from "vitest";
import { AssistantMessageComponent } from "../src/modes/interactive/components/assistant-message.ts";
import { UserMessageComponent } from "../src/modes/interactive/components/user-message.ts";
import { initTheme } from "../src/modes/interactive/theme/theme.ts";
import { stripAnsi } from "../src/utils/ansi.ts";
const OSC133_ZONE_START = "\x1b]133;A\x07";
const OSC133_ZONE_END = "\x1b]133;B\x07";
const OSC133_ZONE_FINAL = "\x1b]133;C\x07";
function createAssistantMessage(
content: AssistantMessage["content"],
overrides: Partial<Pick<AssistantMessage, "stopReason">> = {},
): AssistantMessage {
return {
role: "assistant",
content,
api: "openai-responses",
provider: "openai",
model: "gpt-4o-mini",
usage: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
stopReason: overrides.stopReason ?? "stop",
timestamp: Date.now(),
};
}
describe("AssistantMessageComponent", () => {
test("adds OSC 133 zone markers to assistant messages without tool calls", () => {
initTheme("dark");
const component = new AssistantMessageComponent(createAssistantMessage([{ type: "text", text: "hello" }]));
const lines = component.render(40);
expect(lines).not.toHaveLength(0);
expect(lines[0]).toContain(OSC133_ZONE_START);
expect(lines[lines.length - 1].startsWith(OSC133_ZONE_END + OSC133_ZONE_FINAL)).toBe(true);
});
test("does not add OSC 133 zone markers when assistant message contains tool calls", () => {
initTheme("dark");
const component = new AssistantMessageComponent(
createAssistantMessage([
{ type: "text", text: "calling tool" },
{ type: "toolCall", id: "tool-1", name: "read", arguments: { path: "file.txt" } },
]),
);
const rendered = component.render(60).join("\n");
expect(rendered.includes(OSC133_ZONE_START)).toBe(false);
expect(rendered.includes(OSC133_ZONE_END)).toBe(false);
expect(rendered.includes(OSC133_ZONE_FINAL)).toBe(false);
});
test("renders length stops with neutral truncation wording", () => {
initTheme("dark");
const component = new AssistantMessageComponent(
createAssistantMessage([{ type: "thinking", thinking: "private reasoning" }], { stopReason: "length" }),
true,
);
const rendered = component.render(80).join("\n");
expect(rendered).toContain("Thinking...");
expect(rendered).toContain("Response was truncated before completion.");
});
test("coalesces adjacent thinking blocks into one hidden thinking label", () => {
initTheme("dark");
const component = new AssistantMessageComponent(
createAssistantMessage([
{ type: "thinking", thinking: "first thought" },
{ type: "thinking", thinking: "" },
{ type: "thinking", thinking: "second thought" },
{ type: "text", text: "answer" },
]),
true,
);
const rendered = stripAnsi(component.render(80).join("\n"));
expect(rendered.match(/Thinking\.\.\./g)).toHaveLength(1);
expect(rendered).toContain("answer");
});
test("collapses individual thinking runs when clicked", () => {
initTheme("dark");
const component = new AssistantMessageComponent(
createAssistantMessage([
{ type: "thinking", thinking: "first reasoning" },
{ type: "text", text: "answer" },
{ type: "thinking", thinking: "second reasoning" },
]),
);
const width = 80;
const lines = component.render(width);
const firstThinkingRow = lines.findIndex((line) => stripAnsi(line).includes("first reasoning"));
expect(firstThinkingRow).toBeGreaterThanOrEqual(0);
const event: TuiMouseEvent = {
type: "click",
button: "left",
x: 1,
y: firstThinkingRow,
screenX: 1,
screenY: firstThinkingRow,
width,
height: lines.length,
shift: false,
alt: false,
ctrl: false,
clickCount: 1,
};
expect(component.handleMouse(event)?.handled).toBe(true);
const collapsed = stripAnsi(component.render(width).join("\n"));
expect(collapsed).not.toContain("first reasoning");
expect(collapsed).toContain("Thinking...");
expect(collapsed).toContain("second reasoning");
});
test("uses configured output padding for text and thinking", () => {
initTheme("dark");
const component = new AssistantMessageComponent(
createAssistantMessage([
{ type: "text", text: "hello" },
{ type: "thinking", thinking: "reasoning" },
]),
false,
undefined,
"Thinking...",
1,
);
const lines = component.render(80).map((line) => stripAnsi(line));
expect(lines.some((line) => line.includes(" hello"))).toBe(true);
expect(lines.some((line) => line.includes(" reasoning"))).toBe(true);
component.setOutputPad(0);
const updatedLines = component.render(80).map((line) => stripAnsi(line));
expect(updatedLines.some((line) => line.startsWith("hello"))).toBe(true);
expect(updatedLines.some((line) => line.startsWith("reasoning"))).toBe(true);
});
test("chains Markdown transformers in registration order", () => {
initTheme("dark");
const calls: string[] = [];
const message = createAssistantMessage([{ type: "text", text: "The result is $x^2$." }]);
const component = new AssistantMessageComponent(message, false, undefined, "Thinking...", 1, [
(markdown, context) => {
calls.push("formula");
expect(context).toEqual({ messageType: "assistant", isStreaming: false, availableWidth: 78 });
return markdown.replace("$x^2$", "x²");
},
(markdown) => {
calls.push("suffix");
return `${markdown} Done.`;
},
]);
expect(stripAnsi(component.render(80).join("\n"))).toContain("The result is x². Done.");
expect(calls).toEqual(["formula", "suffix"]);
});
test("identifies partial assistant Markdown as streaming", () => {
initTheme("dark");
const streamingStates: boolean[] = [];
const message = createAssistantMessage([{ type: "text", text: "partial" }]);
const component = new AssistantMessageComponent(undefined, false, undefined, "Thinking...", 1, [
(markdown, context) => {
streamingStates.push(context.isStreaming);
return context.isStreaming ? markdown : `${markdown} transformed`;
},
]);
component.updateContent(message, true);
expect(stripAnsi(component.render(80).join("\n"))).not.toContain("transformed");
component.updateContent(message, false);
expect(stripAnsi(component.render(80).join("\n"))).toContain("partial transformed");
expect(streamingStates).toEqual([true, false]);
});
test("reapplies Markdown transformers when available width changes", () => {
initTheme("dark");
const availableWidths: number[] = [];
const component = new AssistantMessageComponent(
createAssistantMessage([{ type: "text", text: "answer" }]),
false,
undefined,
"Thinking...",
1,
[
(markdown, context) => {
availableWidths.push(context.availableWidth);
return `${markdown} (${context.availableWidth})`;
},
],
);
expect(stripAnsi(component.render(80).join("\n"))).toContain("answer (78)");
component.render(80);
expect(stripAnsi(component.render(60).join("\n"))).toContain("answer (58)");
expect(availableWidths).toEqual([78, 58]);
});
test("continues the Markdown transformer chain when a transformer throws", () => {
initTheme("dark");
const calls: string[] = [];
const component = new AssistantMessageComponent(
createAssistantMessage([{ type: "text", text: "still visible" }]),
false,
undefined,
"Thinking...",
1,
[
(markdown) => {
calls.push("first");
return markdown.replace("still", "remains");
},
() => {
calls.push("throw");
throw new Error("broken transformer");
},
(markdown) => {
calls.push("last");
return `${markdown} after error`;
},
],
);
expect(stripAnsi(component.render(80).join("\n"))).toContain("remains visible after error");
expect(calls).toEqual(["first", "throw", "last"]);
});
test("transforms text and thinking Markdown without mutating the original message", () => {
initTheme("dark");
const message = createAssistantMessage([
{ type: "text", text: "answer" },
{ type: "thinking", thinking: "reasoning" },
]);
const component = new AssistantMessageComponent(message, false, undefined, "Thinking...", 1, [
(markdown, { messageType }) => {
return `${messageType}:${markdown}`;
},
]);
const rendered = stripAnsi(component.render(80).join("\n"));
expect(rendered).toContain("assistant:answer");
expect(rendered).toContain("assistant-thinking:reasoning");
expect(message.content).toEqual([
{ type: "text", text: "answer" },
{ type: "thinking", thinking: "reasoning" },
]);
});
test("uses configured output padding for user messages", () => {
initTheme("dark");
const paddedComponent = new UserMessageComponent("hello", undefined, 1);
const paddedLines = paddedComponent.render(40).map((line) => stripAnsi(line));
expect(paddedLines.some((line) => line.startsWith(" hello"))).toBe(true);
const unpaddedComponent = new UserMessageComponent("hello", undefined, 0);
const unpaddedLines = unpaddedComponent.render(40).map((line) => stripAnsi(line));
expect(unpaddedLines.some((line) => line.startsWith("hello"))).toBe(true);
});
});
|