Something went wrong. Try again.
Your thinking partner.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586/** * Projection from the SDK's transcript rows into this app's render vocabulary. * * Row identity, delta accumulation, replay suppression and backfill merging are * the accumulator's job (letta-agent-sdk#274). What is left — and what lives * here — is presentation: which row carries the caret, how a think's duration * reads, and the two tool states only the approval flow knows about. Pure, so * the mapping is testable without a session. */import type { TranscriptRow } from "@letta-ai/letta-agent-sdk/client";
import type { ToolStatus, TranscriptItem } from "./model";import { cleanUserText, formatToolInput, summarizeToolInput, toolInputDescription } from "./toolText";import { parseChannelNotification } from "./channelNotification";
export interface ProjectionState { /** Row currently being written by an in-flight turn, if any. */ liveKey: string | null; /** Row a turn abandoned — rendered "Stopped". */ interruptedKey: string | null; /** awaiting_approval / denied, which no wire status expresses. */ toolStatusOverride: ReadonlyMap<string, ToolStatus>;}
export function projectRow(row: TranscriptRow, state: ProjectionState): TranscriptItem { const live = row.key === state.liveKey;
if (row.kind === "tool_call") { const override = state.toolStatusOverride.get(row.toolCallId); const settled = row.status === "complete"; return { kind: "tool", id: row.key, toolCallId: row.toolCallId, name: row.toolName, description: toolInputDescription(row.toolInput), summary: summarizeToolInput(row.toolInput), input: formatToolInput(row.toolInput), // A denial is the user-meaningful terminal state and outranks the error // tool_result the server sends after it. status: override ?? (settled ? (row.result?.isError ? "error" : "success") : "running"), ...(row.result ? { result: row.result.content } : {}), }; }
if (row.kind === "reasoning") { return { kind: "reasoning", id: row.key, text: row.text, ...(live ? { streaming: true } : {}), }; }
if (row.kind === "assistant") { return { kind: "assistant", id: row.key, text: row.text, ...(live ? { streaming: true } : {}), ...(state.interruptedKey === row.key ? { interrupted: true } : {}), }; }
// User rows arrive from history and from the stream's own echo; strip the // system-reminder wrappers the wire carries either way. const text = cleanUserText(row.text); return { kind: "user", id: row.key, ...(parseChannelNotification(text) ?? { text }) };}
export function projectRows( rows: readonly TranscriptRow[], state: ProjectionState,): TranscriptItem[] { return rows.map((row) => projectRow(row, state));}
/** Newest text row — the one an abrupt end leaves unfinished. */export function newestTextKey(rows: readonly TranscriptRow[]): string | null { for (let i = rows.length - 1; i >= 0; i--) { const row = rows[i]!; if (row.kind === "assistant" || row.kind === "reasoning") return row.key; } return null;}