import type { ActionChatUIConfig } from "../action-ui.js"; import { type CodeAgentTranscriptEvent as CoreCodeAgentTranscriptEvent } from "../code-agents/transcript-normalizer.js"; import type { AgentMcpAppPayload } from "../mcp-client/app-result.js"; import type { EngineMessage } from "./engine/types.js"; import type { AgentChatAttachment, RunEvent } from "./types.js"; interface ContentPart { type: string; text?: string; toolCallId?: string; toolName?: string; argsText?: string; args?: Record; result?: string; isError?: boolean; completedSideEffect?: boolean; mcpApp?: AgentMcpAppPayload; chatUI?: ActionChatUIConfig; } interface BuildAssistantMessageOptions { suppressInternalContinuation?: boolean; /** * Logical-turn identity. When set it is stamped onto the message metadata so * continuation runs of the same turn can be folded onto a single durable * assistant message (see foldAssistantTurn) instead of each run dropping or * overwriting the others. */ turnId?: string; runDurationMs?: number; } type AssistantMessage = NonNullable>; type UserMessage = ReturnType; export declare const ASSISTANT_RUN_DURATION_METADATA_KEY = "agentNativeRunDurationMs"; /** * Reconstruct an assistant-ui message from raw agent run events. * Mirrors the client-side processEvent logic so the server can persist * the assistant's response even if the frontend is disconnected. */ export declare function buildAssistantMessage(events: RunEvent[], runId?: string, options?: BuildAssistantMessageOptions): { id: string; createdAt: Date; role: "assistant"; content: ContentPart[]; status: { type: "complete"; reason: "stop"; } | { type: "incomplete"; reason: "error"; }; metadata: Record; } | null; /** * Convert legacy/partially merged thread data into assistant-ui's exported * repository shape and repair parent links so `threadRuntime.import()` cannot * fail with "Parent message not found". */ export declare function normalizeThreadRepository(repo: any): any; /** * Rebuild a flat `EngineMessage[]` from persisted thread_data (the * assistant-ui ExportedMessageRepository shape). Text-only — tool calls/results * are flattened to their text so a continuation run gets the conversation * prefix as plain context (Anthropic's prompt cache makes the resume cheap). * * Used to resume a background sub-agent in a fresh function invocation (the * server-side analog of the browser re-POSTing history for the main chat). * Originally inlined in `integrations/webhook-handler.ts`. */ export declare function threadDataToEngineMessages(threadData: string | Record | null | undefined): EngineMessage[]; /** * Select the participant-visible delivery for integration turns while keeping * a compact, trusted resource ledger available to the agent. Raw tool results * remain in thread_data for UI/audit use but are not replayed into the prompt. */ export declare function threadMessageTextForEngine(message: any): string; export interface CodeAgentThreadTranscriptEvent { id: string; runId: string; kind?: CoreCodeAgentTranscriptEvent["kind"]; type?: CoreCodeAgentTranscriptEvent["kind"] | "note"; message?: string; text?: string; createdAt: string; metadata?: Record; artifactPath?: string; artifactUrl?: string; signal?: CoreCodeAgentTranscriptEvent["signal"]; } export interface BuildRepositoryFromCodeAgentTranscriptOptions { hideCredentialMessages?: boolean; } export declare function buildRepositoryFromCodeAgentTranscript(events: readonly CodeAgentThreadTranscriptEvent[], options?: BuildRepositoryFromCodeAgentTranscriptOptions): any; /** * Merge an incoming client-side full-thread save over the current SQL copy. * * The browser exports and PUTs the whole assistant-ui repository. If a server * completion save lands first, an older browser export can otherwise replace * `thread_data` wholesale and delete the assistant message the server just * reconstructed from run events. Preserve server-only messages while still * accepting client-only messages and metadata. */ export interface MergeThreadDataOptions { preserveExistingQueuedMessages?: boolean; preserveExistingTopLevelKeys?: boolean; } export declare function mergeThreadDataForClientSave(existingRepo: any, incomingRepo: any, options?: MergeThreadDataOptions): any; export declare function buildUserMessage(opts: { text: string; attachments?: AgentChatAttachment[]; runId?: string; createdAt?: Date; }): { id: string; createdAt: Date; role: "user"; content: ContentPart[]; attachments?: any[]; metadata: Record; }; export declare function upsertUserMessage(repo: any, userMsg: UserMessage): any; /** * Merge the server-reconstructed assistant message into persisted * assistant-ui thread data. * * The browser periodically saves thread data while a run is still streaming. * That can leave the last assistant message non-empty but partial/pending. * Completion must replace that same-run partial message instead of treating * any assistant content as proof that the frontend already saved the final * turn. */ export declare function upsertAssistantMessage(repo: any, assistantMsg: AssistantMessage): any; /** * Fold a continuation run's assistant message onto the single durable message * for its logical turn (identified by `turnId`), so a turn that spans several * continuation runs accumulates into ONE message that only ever grows. This is * the server-side analog of an append-only rollout: the durable transcript is * a monotonic fold over every run in the turn, never a per-run snapshot that * drops the earlier chunks. * * Idempotent and never-shrinking, so it is safe to run alongside the client's * full-thread export (which may write the same turn from the other side): * - First chunk of a turn → appended as a fresh message. * - A run whose content is already represented (already folded, or the client * saved it) → kept as-is, choosing whichever copy has more content. * - A new chunk → appended onto the accumulated turn. * Falls back to per-run upsert when no `turnId` is available (turn == run). */ export declare function foldAssistantTurn(repo: any, assistantMsg: AssistantMessage, options: { turnId?: string; runId?: string; }): any; export declare function normalizeThreadTitle(value: unknown): string; /** * Extract title and preview from a thread runtime export. * Isomorphic — works on both server and client. */ export declare function extractThreadMeta(repo: any): { title: string; preview: string; }; export {}; //# sourceMappingURL=thread-data-builder.d.ts.map