Something went wrong. Try again.
Your thinking partner.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114/** * 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<string, ToolStatus>(),};
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", "<system-reminder>ignore me</system-reminder>Real 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", '<channel-notification source="discord" chat_id="c" sender_id="s" sender_name="Ada"><attachment kind="image" name="pic.png" />Hello <mention user_id="7">@you</mention></channel-notification>'), 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", "<channel-notification>literal</channel-notification>"), base)).toMatchObject({ kind: "assistant", text: "<channel-notification>literal</channel-notification>", }); });
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(); });});