/** * Thread Status Types Module * * Type definitions for the Thread Status Queue system that tracks * the lifecycle of every inbound message thread across platforms. * On orchestrator restart, non-terminal threads are recovered and * re-enqueued for processing. * * @module types/thread-status */ import type { MessageSource } from '../constants.js'; /** * All possible states in the thread status lifecycle. * * - `enqueued` — Message received, queued for orchestrator * - `delivered` — Message written to orchestrator PTY * - `replied_completed` — Orchestrator replied, no further action needed * - `replied_waiting_actions` — Orchestrator replied but delegated work; needs follow-up * - `replied_to_follow_up` — Orchestrator replied asking for clarification * - `expired` — Thread timed out without reply * - `error` — Delivery or processing failed (retryable) */ export type ThreadStatus = 'enqueued' | 'delivered' | 'replied_completed' | 'replied_waiting_actions' | 'replied_to_follow_up' | 'expired' | 'error'; /** * All valid thread status values as a tuple for runtime validation. */ export declare const THREAD_STATUS_VALUES: readonly ["enqueued", "delivered", "replied_completed", "replied_waiting_actions", "replied_to_follow_up", "expired", "error"]; /** * Terminal statuses that require no further orchestrator action. * Used by getPendingThreads() to filter out completed threads. * * Note: replied_to_follow_up is NOT terminal — it is waiting for user reply, * and on restart the follow-up tracking (scheduled check + event subscription) * must be re-established. */ export declare const TERMINAL_STATUSES: readonly ThreadStatus[]; /** * Valid reply statuses for the markReplied() method. */ export type ReplyStatus = 'replied_completed' | 'replied_waiting_actions' | 'replied_to_follow_up'; /** * Platform-specific metadata stored with each thread entry. * Used to re-enqueue messages on orchestrator restart. */ export interface ThreadSourceMetadata { /** Slack/GChat/Telegram channel identifier */ channelId?: string; /** Slack thread timestamp */ threadTs?: string; /** User who sent the message */ userId?: string; /** Telegram/WhatsApp chat identifier */ chatId?: string; } /** * A single thread status entry tracking one inbound message thread * from reception through delivery and reply. */ export interface ThreadStatusEntry { /** Unique ID (matches QueuedMessage.id or generated) */ id: string; /** Source platform that originated the message */ source: MessageSource; /** Platform-specific thread identifier (e.g. "C123:1707430000.001234" for Slack) */ threadKey: string; /** The conversationId used in MessageQueueService */ conversationId: string; /** Current status in the thread lifecycle */ status: ThreadStatus; /** ISO timestamp when message was first received */ receivedAt: string; /** ISO timestamp when message was delivered to orchestrator PTY */ deliveredAt?: string; /** ISO timestamp when orchestrator replied */ repliedAt?: string; /** ISO timestamp of last status update */ updatedAt: string; /** Agent sessions that were delegated work from this thread */ delegatedAgents?: string[]; /** Brief summary of the user's message (first 200 chars) */ messagePreview: string; /** Number of delivery retry attempts */ retryCount: number; /** The QueuedMessage.id if still in message queue */ queueMessageId?: string; /** Source metadata for re-enqueue on restart */ sourceMetadata?: ThreadSourceMetadata; } /** * Version number for the persisted thread status queue format. * Increment when making breaking changes to the storage schema. */ export declare const PERSISTED_THREAD_STATUS_VERSION = 1; /** * Compact record of a terminal entry that cleanup removed. Lets readers tell * "this thread was closed, its entry aged out" apart from "never tracked" * (#757). */ export interface ThreadStatusTombstone { /** Platform-specific thread identifier of the removed entry */ threadKey: string; /** Conversation id of the removed entry, when it had one */ conversationId?: string; /** Terminal status the entry ended in */ status: ThreadStatus; /** The entry's last update (ISO) */ updatedAt: string; /** When cleanup removed the entry (ISO) */ removedAt: string; } /** * Shape of the persisted JSON file at ~/.crewly/thread-status-queue.json */ export interface PersistedThreadStatusState { /** Schema version for forward compatibility */ version: number; /** All tracked thread entries */ entries: ThreadStatusEntry[]; /** ISO timestamp of last cleanup run */ lastCleanupAt: string; /** Tombstones of cleaned-up terminal entries (absent in older files) */ tombstones?: ThreadStatusTombstone[]; } /** * Input for tracking a new inbound thread message. */ export interface TrackInboundInput { /** Source platform */ source: MessageSource; /** Platform-specific thread identifier */ threadKey: string; /** The conversationId used in MessageQueueService */ conversationId: string; /** Brief summary of the user's message (first 200 chars) */ messagePreview: string; /** The QueuedMessage.id if still in message queue */ queueMessageId?: string; /** Source metadata for re-enqueue on restart */ sourceMetadata?: ThreadSourceMetadata; } /** * Statistics snapshot for monitoring the thread status queue. */ export interface ThreadStatusStats { /** Total number of entries in the queue */ total: number; /** Count of entries per status */ byStatus: Record; /** ISO timestamp of the oldest non-terminal entry, or null if none */ oldestPending: string | null; } /** * Checks whether a value is a valid ThreadStatus string. * * @param value - Value to check * @returns True if value is a valid ThreadStatus */ export declare function isThreadStatus(value: unknown): value is ThreadStatus; /** * Checks whether a given ThreadStatus is terminal (no further action needed). * * @param status - The status to check * @returns True if the status is terminal */ export declare function isTerminalStatus(status: ThreadStatus): boolean; /** * Checks whether a value is a valid ThreadStatusEntry. * Validates required fields and their types. * * @param value - Value to check * @returns True if value conforms to ThreadStatusEntry */ export declare function isThreadStatusEntry(value: unknown): value is ThreadStatusEntry; /** * Checks whether a value is a valid PersistedThreadStatusState. * * @param value - Value to check * @returns True if value conforms to PersistedThreadStatusState */ export declare function isPersistedThreadStatusState(value: unknown): value is PersistedThreadStatusState; /** * Checks whether a value is a well-formed ThreadStatusTombstone. * * @param value - Value to check * @returns True if value conforms to ThreadStatusTombstone */ export declare function isThreadStatusTombstone(value: unknown): value is ThreadStatusTombstone; /** * Checks whether a value is a valid ReplyStatus. * * @param value - Value to check * @returns True if value is a valid reply status */ export declare function isReplyStatus(value: unknown): value is ReplyStatus; //# sourceMappingURL=thread-status.types.d.ts.map