/** * Shared transcript-history reader for the base-platform chat client. * * Reads a thread's persisted transcript from the public read plane * (`GET {baseUrl}/threads/{id}/messages`, paginated by `nextToken`) so a * reload can repaint history before any SSE traffic starts. Host-agnostic: * the caller supplies the base URL and a `requestInit` carrying whatever * identity that host can present (a `Authorization: Bearer` header on web / * RN when signed in, an `x-guuey-guest` header for RN guests, cookies via * `credentials: "include"`). Consumed by {@link createWebAdapters}; Portal * has its own copy today and can migrate onto this later. */ import type { AgMessage, JsonValue } from "@silverprotocol/core"; import type { AgentMessage, HistoryCard, HistoryLoadResult } from "./types.js"; /** One row of `GET /v1/threads/:id/messages`. */ export interface ThreadHistoryRow { seq: number; at: string; kind: string; authorRole: string; text: string | null; /** * The verbatim persisted `AgArtifact` on `kind === "card"` rows; `null` or * absent on text/event rows (the read plane omits it there). Forwarded * opaquely — never re-parsed into AgEvents. */ cardSnapshot?: JsonValue | null; /** guuey#402: the producing tool's wire name, when the row carries it. */ toolName?: string | null; } /** * Thrown when the read plane returns 401 on a transcript fetch — distinct * from the generic non-OK throw below so a caller holding a token can * `instanceof`-match it and retry once with a freshly-refreshed one * (`createWebAdapters`'s history adapter is the concrete retry). A cached * bearer that has expired since the caller last checked it is exactly the * shape a refresh can fix; a 500 or a malformed request is not, and gets no * such retry. */ export declare class HistoryUnauthorizedError extends Error { constructor(message?: string); } /** Project raw rows to chat turns: text rows only, author → role. */ export declare function threadHistoryRowsToMessages(rows: ThreadHistoryRow[]): AgentMessage[]; /** * Project raw rows to persisted generative-UI cards: `kind === "card"` rows * that actually carry a snapshot, tagged with their transcript position. The * additive counterpart to {@link threadHistoryRowsToMessages} — a * block-preserving consumer merges both by `seq`. */ export declare function threadHistoryRowsToCards(rows: ThreadHistoryRow[]): HistoryCard[]; export interface ThreadHistoryFetchOptions { /** Public read-plane base, already ending in `/v1`. */ baseUrl: string; threadId: string; /** Per-request init merged into each page fetch (headers, credentials). */ requestInit?: RequestInit; /** * Opt-in: ALSO project `kind === "card"` rows into `result.cards` (see * {@link HistoryLoadResult}). Off by default so text-only consumers get the * byte-identical `{ messages }` shape. On, a block-preserving renderer gets * the persisted cards to interleave by `seq`. */ includeCards?: boolean; /** Injection seam for tests; defaults to the global `fetch`. */ fetchImpl?: typeof fetch; } /** * Fetch a thread's transcript across all pages. Returns `{ gone: true }` on * 403/404 (a stale local threadId the caller no longer owns / that no longer * exists) so the hook can drop the persisted id; throws on any other non-OK * status so `useAgentInvoke`'s best-effort caller can swallow it and leave * the chat empty. */ export declare function fetchThreadHistory({ baseUrl, threadId, requestInit, includeCards, fetchImpl, }: ThreadHistoryFetchOptions): Promise; /** * The tool name for a `tool-result` block, read off its paired `tool-call` * block in the same message (the reducer keeps both in one message's content). * Falls back to `"tool"` when the pair is missing. */ export declare function toolNameFor(message: AgMessage, toolCallId: string): string; /** * Persisted cards, ascending by transcript `seq` (stable; input untouched). * These are PRIOR-turn cards — they always precede the live fold, so a * block-preserving renderer surfaces them first (e.g. under an "Earlier in * this conversation" divider). */ export declare function sortHistoryCards(cards: readonly HistoryCard[]): HistoryCard[]; //# sourceMappingURL=history.d.ts.map