/** * Filesystem-based data store for Supen. * * Replaces SQLite (db.ts) with plain files: * - agents/{id}/agent.json → AgentRecord * - threads/{sid}/ → thread.json + messages.jsonl for * non-Codex external chat history * - router-state.json → key-value pairs * - registered groups → agent.json `sources` field (future) * - automations/{id}/runs/ → automation run ledger and Codex thread * mapping/context * * Codex-backed automation runs must not persist their trigger or result * transcript in thread-level messages JSONL files. Codex already owns the * complete thread transcript; Supen only stores automation configuration, * run state, context, mapping, and UI/event caches. * * Design philosophy: the filesystem IS the database. * See docs/data-management-design.md */ export declare const AUTOMATION_TASK_AGENT_ID = "automation-task"; import type { AgentRecord, AgentChannelConfig, AutomationEventRecord, AutomationRecord, AutomationRunRecord, AutomationStateRecord, CreateEventAutomationInput, CreateAutomationInput, CreateAutomationRunInput, CreateScheduleInput, FinalizeAutomationRunInput, FinalizeRunningAutomationRunForThreadInput, InteractionEvent, ListAutomationRunsInput, ListScheduleRunsInput, NewMessage, RegisteredProject, ScheduleRecord, ScheduleRunRecord, ThreadUiEvent, ThreadRecord, UpdateAutomationInput, UpdateAutomationStateInput } from './types.js'; /** Read last N lines from a JSONL file (efficient tail). */ declare function readJsonlTail(filePath: string, limit: number): T[]; export declare function getAgentStorage(agentId: string): { agent: AgentRecord; baseDir: string; configPath: string; } | undefined; export declare function allocateAutomationRunThreadId(automation: Pick, runId: string): string; export declare function initStore(): void; export declare function ensureAgent(input: { agent_id: string; space_id?: string; name?: string; tags?: string[]; channels?: Record; skills?: AgentRecord['skills']; }): AgentRecord; export declare function createAgent(input: { agent_id: string; space_id?: string; name?: string; tags?: string[]; channels?: Record; skills?: AgentRecord['skills']; }): AgentRecord; export declare function updateAgent(agentId: string, updates: Record): AgentRecord | undefined; export declare function deleteAgent(agentId: string): boolean; export declare function getAgent(agentId: string): AgentRecord | undefined; export declare function getAllAgents(): AgentRecord[]; export declare function ensureThread(input: { agent_id: string; thread_id: string; channel: string; channel_thread?: string; backend_driver_id?: string; source_ref?: string; knowledge_id?: string; knowledge_name?: string; environment_id?: string; environment_snapshot?: ThreadRecord['environment_snapshot']; task_workspace_folder?: string | null; agent_name?: string; space_id?: string; title?: string; status?: ThreadRecord['status']; created_at?: string; updated_at?: string; }): ThreadRecord; export declare function getThread(threadId: string): ThreadRecord | undefined; export declare function getThreadForAgent(agentId: string, threadId: string): ThreadRecord | undefined; export declare function getThreadsForAgent(agentId: string): ThreadRecord[]; export declare function getArchivedThreadsForAgent(agentId: string): ThreadRecord[]; export declare function getAllThreads(): ThreadRecord[]; export declare function ensureAutomationTask(automation: AutomationRecord): ThreadRecord; export declare function updateAutomationTaskThreadStatus(automationId: string, status: ThreadRecord['status']): ThreadRecord | undefined; export declare function automationExecutionIdentity(automationId: string): string; type AutomationRunContextInput = { automation: AutomationRecord; runId: string; kind: 'scheduled' | 'event'; nowIso: string; previousTriggeredAt?: string | null; eventEnvelope?: Record | null; }; export declare function writeAutomationRunContext(input: AutomationRunContextInput): string; export declare function renderAutomationActionCommand(command: string, eventEnvelope?: Record | null): string; export declare function buildAutomationRunChatMessage(input: { automation: AutomationRecord; runId: string; contextPath: string; eventEnvelope?: Record | null; }): string; export declare function buildAutomationTriggerMessage(input: { automation: AutomationRecord; nowIso: string; previousTriggeredAt?: string | null; runId?: string; threadId?: string; }): NewMessage; export declare function updateThreadStatus(agentId: string, threadId: string, status: ThreadRecord['status']): void; export declare function updateThreadBackendDriverId(agentId: string, threadId: string, backendDriverId: string): void; export declare function updateThreadUsage(agentId: string, threadId: string, tokensIn: number, tokensOut: number): void; export declare function appendGlobalUsage(agentId: string, threadId: string, tokensIn: number, tokensOut: number): void; export declare function getUsageStats(record: { tokens_in?: number; tokens_out?: number; agent_id?: string; }): { tokens_in: number; tokens_out: number; tokens_total: number; estimated_cost_usd: number; }; /** Get global usage across all agents and threads. */ export declare function getGlobalUsage(): { tokens_in: any; tokens_out: any; tokens_total: any; estimated_cost_usd: any; }; /** Get the full detailed daily usage ledger. */ export declare function getDailyUsage(): any; /** * Reset threads stuck as "running" — called at startup to recover from crashes. * A "running" thread cannot survive a process restart, so these are stale. */ export declare function cleanupStaleThreads(): number; export declare function updateThreadMetadata(agentId: string, threadId: string, updates: Partial): boolean; export declare function archiveThreadForAgent(agentId: string, threadId: string): boolean; export declare function restoreThreadForAgent(agentId: string, threadId: string): boolean; export declare function deleteThread(threadId: string): void; export declare function deleteThreadForAgent(agentId: string, threadId: string, fromArchived?: boolean): boolean; /** Resolve agent+thread from chat_jid (format: "http:agent_id:thread_id" or similar). */ export declare function resolveFromChatJid(chatJid: string): { agentId: string; threadId: string; } | null; export declare function storeMessage(msg: NewMessage): void; export declare function getRecentMessages(chatJid: string, limit?: number): NewMessage[]; export declare function getMessagesSince(chatJid: string, sinceTimestamp: string, _botPrefix: string): NewMessage[]; export declare function getNewMessages(jids: string[], lastTimestamp: string, botPrefix: string): { messages: NewMessage[]; newTimestamp: string; }; export declare class AutomationInputError extends Error { code: string; constructor(code: string, message: string); } export declare class ScheduleInputError extends AutomationInputError { constructor(code: string, message: string); } export declare function countRunningAutomationRuns(automationId: string): number; export declare function getAutomationRunById(runId: string): AutomationRunRecord | undefined; export declare function createAutomation(input: CreateAutomationInput): AutomationRecord; export declare function createEventAutomation(input: CreateEventAutomationInput): AutomationRecord; export declare function getAutomationById(id: string): AutomationRecord | undefined; export declare function getAutomationState(id: string): AutomationStateRecord | undefined; export declare function listAutomations(agentId?: string): AutomationRecord[]; export declare function updateAutomation(id: string, updates: UpdateAutomationInput): AutomationRecord | undefined; export declare function updateAutomationState(id: string, updates: UpdateAutomationStateInput): AutomationStateRecord | undefined; export declare function markAutomationTriggered(input: { automationId: string; messageId: string; triggeredAt: string; keepExecutionStatus?: boolean; }): AutomationRecord | undefined; export declare function deleteAutomation(id: string): void; export declare function createAutomationRun(automationId: string, input?: CreateAutomationRunInput): AutomationRunRecord; export declare function touchAutomationRun(automationRunId: string, now?: string): boolean; export declare function enqueueAutomationEvent(input: { automationId: string; payload: Record; receivedAt?: string; }): AutomationEventRecord; export declare function listAutomationEvents(input: { automation_id: string; status?: AutomationEventRecord['status']; limit?: number; }): AutomationEventRecord[]; export declare function claimAutomationEvent(input: { automationId: string; eventId: string; runId: string; claimedAt?: string; }): AutomationEventRecord | undefined; export declare function skipAutomationEvent(input: { automationId: string; eventId: string; reason?: string; now?: string; }): AutomationEventRecord | undefined; export declare function finalizeAutomationEventForRun(runId: string, input: { status: 'succeeded' | 'failed'; finishedAt?: string; error?: string | null; }): AutomationEventRecord | undefined; export declare function requeueAutomationEventForRun(runId: string, now?: string): AutomationEventRecord | undefined; export declare function finalizeAutomationRun(runId: string, input: FinalizeAutomationRunInput): AutomationRunRecord | undefined; export declare function listAutomationRuns(input?: ListAutomationRunsInput): AutomationRunRecord[]; export declare function finalizeRunningAutomationRunForThread(agentId: string, threadId: string, input?: FinalizeRunningAutomationRunForThreadInput): AutomationRunRecord | undefined; export declare function isAutomationThreadRuntimeActive(agentId: string, threadId: string): boolean; export declare function createScheduleRun(scheduleId: string, input: { started_at: string; thread_id?: string; task_id?: string; chat_jid?: string; notify_target_jid?: string; }): ScheduleRunRecord; export declare function getScheduleRunsForSchedule(scheduleId: string): ScheduleRunRecord[]; export declare function listScheduleRuns(input?: ListScheduleRunsInput): ScheduleRunRecord[]; export declare function finalizeScheduleRunRecord(runId: string, input: { finished_at: string; outcome: 'succeeded' | 'failed'; result_text?: string; error?: string; }): ScheduleRunRecord | undefined; export declare function createSchedule(input: CreateScheduleInput): ScheduleRecord; export declare function getScheduleById(id: string): ScheduleRecord | undefined; export declare function getAllSchedules(): ScheduleRecord[]; export declare function getSchedulesForAgent(agentId: string): ScheduleRecord[]; export declare function getDueSchedules(forceAll?: boolean): ScheduleRecord[]; export declare function claimDueSchedules(nowIso?: string, forceAll?: boolean): ScheduleRecord[]; export declare function protectAutomationRunFromStaleRecovery(runId: string): () => void; export declare function isAutomationRunProtectedFromStaleRecovery(runId: string): boolean; export declare function recoverStaleAutomationExecutions(nowIso?: string): number; /** * Reset automation executions that were in-flight when the daemon process * restarted. The stream owner lived in the old process, so these runs cannot * complete normally even when they have not reached the normal stale timeout. */ export declare function cleanupStaleAutomationExecutions(nowIso?: string): number; export declare function updateSchedule(id: string, updates: Partial>): ScheduleRecord | undefined; export declare function updateScheduleAfterRun(id: string, nextRun: string | null): void; export declare function finalizeScheduleRun(id: string, input: { run_id?: string; finished_at: string; outcome: 'succeeded' | 'failed'; result_text?: string; error?: string; }): ScheduleRecord | undefined; export declare function deleteSchedule(id: string): void; export declare function getRouterState(key: string): string | undefined; export declare function setRouterState(key: string, value: string): void; export declare function getAllRouterState(): Record; export declare function getRegisteredProject(jid: string): (RegisteredProject & { jid: string; }) | undefined; export declare function setRegisteredProject(jid: string, group: RegisteredProject): void; export declare function getAllRegisteredProjects(): Record; export declare function storeChatMetadata(chatJid: string, timestamp: string, name?: string, channel?: string, _isGroup?: boolean): void; export declare function updateChatName(chatJid: string, name: string): void; export interface ChatInfo { jid: string; name: string; last_message_time: string; channel: string; is_group: number; } export declare function getAllChats(): ChatInfo[]; export declare function getLastGroupSync(): string | null; export declare function setLastGroupSync(): void; export declare function storeInteractionEvent(event: InteractionEvent): void; export declare function getRecentInteractionEvents(chatJid: string, limit?: number): InteractionEvent[]; export declare function getThreadInteractionEvents(agentId: string, threadId: string, limit?: number): InteractionEvent[]; export declare function storeThreadUiEvent(agentId: string, threadId: string, event: ThreadUiEvent): void; export declare function getThreadUiEvents(agentId: string, threadId: string, limit?: number): ThreadUiEvent[]; export declare function getThreadUiEventsForTaskIds(agentId: string, threadId: string, taskIds: Iterable): ThreadUiEvent[]; export type CodexCompletedTurnItem = { id?: string; turnId: string; timestamp: string; item: Record; }; export declare function storeCodexCompletedTurnItem(threadId: string, record: CodexCompletedTurnItem): void; export declare function getCodexCompletedTurnItems(threadId: string, limit?: number): CodexCompletedTurnItem[]; export declare const __testOnly: { readJsonlTail: typeof readJsonlTail; }; export {}; //# sourceMappingURL=store.d.ts.map