import { J as JsonRecord } from '../stream-normalizer-DnuqkZvw.js';
export { M as MISSING_TOOL_TERMINAL_ERROR, b as MISSING_TOOL_TERMINAL_REASON, S as StreamEvent, c as asRecord, d as asString, a as attachmentPartKey, e as collapseRedundantTextParts, f as draftAssistantParts, g as encodeEvent, h as finalizeAssistantParts, i as finalizePendingInteractionParts, j as getPartKey, m as mergePersistedPart, n as normalizePersistedPart, k as normalizeTime, l as normalizeToolEvent, r as resolveToolId, o as resolveToolName, t as terminalizeDanglingAssistantToolUpdates, p as terminalizeDanglingToolPart, q as terminalizeDanglingToolParts } from '../stream-normalizer-DnuqkZvw.js';
export { B as BufferedTurnEvent, a as BufferedTurnOptions, b as BufferedTurnTap, D as D1LikeForTurns, P as PumpBufferedTurnOptions, R as ReplayTurnEventsOptions, c as TURN_EVENTS_MIGRATION_SQL, d as TURN_STATUS_SCOPE_MIGRATION_SQL, T as TurnEventStore, e as TurnStatus, f as coalesceChatStreamEvents, g as coalesceDeltas, h as createBufferedTurnTap, i as createD1TurnEventStore, j as createMemoryTurnEventStore, p as pumpBufferedTurn, r as replayTurnEvents, s as stampReplaySeq } from '../turn-buffer-BcPfhr1_.js';
import '../contract-CEewO6DI.js';
import '@tangle-network/agent-interface';

/** Define the structure of a chat message stored for a specific conversation turn */
interface PersistedChatMessageForTurn {
    id: string;
    role: 'user' | 'assistant' | 'system' | 'tool';
    content: string;
    parts: Array<Record<string, unknown>> | null;
}
/** Represent a chat turn with resolved user message insertion and prior message context */
interface ResolvedChatTurn {
    turnIndex: number;
    shouldInsertUserMessage: boolean;
    priorMessages: PersistedChatMessageForTurn[];
    userParts: JsonRecord[];
    /** The id of the user row this turn REUSES (retry dedup), when one was
     *  found. Absent on the insert path, where no row exists yet.
     *
     *  The reused row is deliberately EXCLUDED from `priorMessages` (it is this
     *  turn's own user message, not prior context), so this field is the only
     *  way a caller can name it. */
    reusedUserMessageId?: string;
}
/** Normalize and validate a client turn ID string ensuring it meets format and length requirements */
declare function normalizeClientTurnId(value: unknown): string | undefined;
/** Build an array of text parts with optional turn ID for user input */
declare function buildUserTextParts(text: string, turnId: string | undefined): JsonRecord[];
/** Resolve whether a message contains any part with the specified turn ID */
declare function messageHasTurnId(message: PersistedChatMessageForTurn, turnId: string): boolean;
/** Resolve a chat turn by determining message reuse and constructing user message parts */
declare function resolveChatTurn(input: {
    existingMessages: PersistedChatMessageForTurn[];
    userContent: string;
    turnId?: string;
    /** True when the thread has a turn still RUNNING in the turn-event buffer
     *  (`turnStore.listRunning(threadId)`).
     *
     *  Without incremental persistence the trailing row of a thread mid-turn is
     *  always the user row, so the content fallback below could assume it. With
     *  incremental persistence the assistant row lands seconds into the turn, so
     *  a retry of that same turn finds an ASSISTANT row trailing and would
     *  insert a duplicate user row.
     *
     *  This flag is the discriminator that keeps both cases right, and it needs
     *  no new state: an assistant row trailing a turn that is still running is
     *  that turn's in-flight draft (walk past it — this is a retry), whereas an
     *  assistant row trailing a SETTLED turn is a completed answer (stop — the
     *  user genuinely repeated a message and deserves a new turn). */
    hasRunningTurn?: boolean;
}): ResolvedChatTurn;

export { JsonRecord, type PersistedChatMessageForTurn, type ResolvedChatTurn, buildUserTextParts, messageHasTurnId, normalizeClientTurnId, resolveChatTurn };
