68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import type { AssistantMessage, Message } from "@earendil-works/pi-ai";
|
|
import { MAX_TASK_ACTIVITY, MAX_TASK_ACTIVITY_BYTES, summarizeActivity } from "../src/activity.ts";
|
|
|
|
function assistantMessage(content: AssistantMessage["content"], timestamp: number): AssistantMessage {
|
|
return {
|
|
role: "assistant",
|
|
content,
|
|
api: "fake" as AssistantMessage["api"],
|
|
provider: "fake" as AssistantMessage["provider"],
|
|
model: "fake-model",
|
|
usage: {
|
|
input: 0,
|
|
output: 0,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
totalTokens: 0,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
},
|
|
stopReason: "stop",
|
|
timestamp,
|
|
};
|
|
}
|
|
|
|
test("summarizes assistant text, tool calls, and tool results in order", () => {
|
|
const messages: Message[] = [
|
|
assistantMessage([
|
|
{ type: "text", text: " I'll inspect\nthe code. " },
|
|
{ type: "toolCall", id: "call-1", name: "read", arguments: { path: "src/index.ts" } },
|
|
], 100),
|
|
{
|
|
role: "toolResult",
|
|
toolCallId: "call-1",
|
|
toolName: "read",
|
|
content: [{ type: "text", text: "file contents" }],
|
|
isError: false,
|
|
timestamp: 101,
|
|
},
|
|
];
|
|
|
|
assert.deepEqual(summarizeActivity(messages, 999), [
|
|
{ kind: "text", summary: "I'll inspect the code.", timestamp: 100 },
|
|
{ kind: "toolCall", summary: "read src/index.ts", timestamp: 100 },
|
|
{ kind: "toolResult", summary: "read: file contents", timestamp: 101, isError: false },
|
|
]);
|
|
});
|
|
|
|
test("activity keeps only a bounded, display-safe UTF-8 tail", () => {
|
|
const messages = Array.from({ length: MAX_TASK_ACTIVITY + 5 }, (_, index) =>
|
|
assistantMessage([{ type: "text", text: `message ${index}` }], index),
|
|
);
|
|
const long = summarizeActivity([
|
|
assistantMessage([{
|
|
type: "text",
|
|
text: `\u001b[31m${"é".repeat(MAX_TASK_ACTIVITY_BYTES)}\u001b[0m`,
|
|
}], 0),
|
|
], 999);
|
|
|
|
const activity = summarizeActivity(messages, 999);
|
|
assert.equal(activity.length, MAX_TASK_ACTIVITY);
|
|
assert.equal(activity[0].summary, "message 5");
|
|
assert.equal(activity.at(-1)?.summary, `message ${MAX_TASK_ACTIVITY + 4}`);
|
|
|
|
assert.ok(Buffer.byteLength(long[0].summary, "utf8") <= MAX_TASK_ACTIVITY_BYTES);
|
|
assert.equal(long[0].summary.includes("\u001b"), false);
|
|
assert.equal(long[0].summary.includes("\uFFFD"), false);
|
|
}); |