/** * The SDK's accumulator owns row identity, delta accumulation and replay * suppression, and is tested upstream. What this app still owns is the * projection into its render vocabulary — the caret, think durations, and the * two tool states that have no wire equivalent. Those are what these pin. */ import { describe, expect, it } from "bun:test"; import type { TranscriptRow } from "@letta-ai/letta-agent-sdk/client"; import { newestTextKey, projectRow, projectRows, type ProjectionState } from "./transcriptProjection"; import type { ToolItem, ToolStatus } from "./model"; const base: ProjectionState = { liveKey: null, interruptedKey: null, toolStatusOverride: new Map(), }; function text(kind: "user" | "assistant" | "reasoning", key: string, value: string): TranscriptRow { return { kind, key, text: value } as TranscriptRow; } function tool(key: string, status: "streaming" | "ready" | "complete", result?: { content: string; isError: boolean }): TranscriptRow { return { kind: "tool_call", key, toolCallId: `call-${key}`, toolName: "shell", toolInput: { command: "npm test" }, argumentsComplete: true, status, ...(result ? { result } : {}), } as TranscriptRow; } describe("transcript projection", () => { it("preserves agent descriptions from object and JSON arguments, including completed history", () => { const args = { command: "bun test src", description: "Run the\nproject’s tests" }; for (const status of ["streaming", "complete"] as const) { for (const input of [args, JSON.stringify(args)]) { const row = { ...tool("described", status), toolInput: input } as TranscriptRow; expect(projectRow(row, base)).toMatchObject({ description: "Run the project’s tests", input: JSON.stringify(args, null, 2), }); } } }); it("does not turn commands or incomplete arguments into descriptions", () => { for (const input of [null, "bun test", '{"description":"Run', { description: 42 }, { description: " " }, { command: "bun test" }]) { const row = { ...tool("fallback", "streaming"), toolInput: input } as TranscriptRow; expect((projectRow(row, base) as ToolItem).description).toBeUndefined(); } }); it("marks only the live row as streaming", () => { const rows = [text("assistant", "a1", "older"), text("assistant", "a2", "newer")]; const items = projectRows(rows, { ...base, liveKey: "a2" }); expect(items[0]).toMatchObject({ kind: "assistant", text: "older" }); expect((items[0] as { streaming?: boolean }).streaming).toBeUndefined(); expect(items[1]).toMatchObject({ streaming: true }); }); it("renders an abandoned row as interrupted", () => { const item = projectRow(text("assistant", "a1", "half a thought"), { ...base, interruptedKey: "a1" }); expect(item).toMatchObject({ interrupted: true }); }); it("keeps a denial over the error tool_result that follows it", () => { const row = tool("t1", "complete", { content: "denied by user", isError: true }); const state = { ...base, toolStatusOverride: new Map([["call-t1", "denied" as ToolStatus]]) }; expect((projectRow(row, state) as ToolItem).status).toBe("denied"); }); it("maps settled tool outcomes to success and error", () => { expect((projectRow(tool("t1", "complete", { content: "ok", isError: false }), base) as ToolItem).status).toBe("success"); expect((projectRow(tool("t2", "complete", { content: "boom", isError: true }), base) as ToolItem).status).toBe("error"); expect((projectRow(tool("t3", "streaming"), base) as ToolItem).status).toBe("running"); }); it("strips system-reminder wrappers from user rows", () => { const item = projectRow( text("user", "u1", "ignore meReal question?"), base, ); expect(item).toMatchObject({ kind: "user", text: "Real question?" }); }); it("projects channel notifications into a user bubble with provenance", () => { const item = projectRow( text("user", "u-channel", 'Hello @you'), base, ); expect(item).toMatchObject({ kind: "user", text: "Hello @you", originLabel: "Discord · Ada", channelAttachments: "Image · pic.png", }); }); it("leaves ordinary assistant text untouched", () => { expect(projectRow(text("assistant", "a-plain", "literal"), base)).toMatchObject({ kind: "assistant", text: "literal", }); }); it("finds the newest text row, ignoring tool rows after it", () => { expect(newestTextKey([text("assistant", "a1", "x"), tool("t1", "complete")])).toBe("a1"); expect(newestTextKey([tool("t1", "complete")])).toBeNull(); }); });