import { Document, DocumentSlot, DocumentFilter, DocumentFilterSet, DocumentListOptions } from '../../types/document.js'; import { ListOptions } from '../../types/list.js'; import { ThreadObject, ThreadType, MessageObject, ThreadFilter, ThreadMetadata, Intent, WorkflowTemplate, UserWorkflow } from '../../types/memory.js'; import { ScheduleRun, ScheduleRunFilter } from '../../types/schedule.js'; /** * Memory connection interface - manages the underlying connection */ interface IMemory { connect(): Promise; disconnect(): Promise; isConnected(): boolean; getThreadMemory(): IThreadMemory; getIntentMemory(): IIntentMemory; getAgentMemory(): IAgentMemory; getWorkflowTemplateMemory(): IWorkflowTemplateMemory; getUserWorkflowMemory(): IUserWorkflowMemory; /** * Document storage. Optional for backward compatibility — memory * implementations that predate documents may omit it. */ getDocumentMemory?(): IDocumentMemory; /** * Schedule run history. Optional for backward compatibility. */ getScheduleRunMemory?(): IScheduleRunMemory; } /** * Thread memory interface - handles thread operations */ interface IThreadMemory { getThread(userId: string, threadId: string): Promise; createThread(type: ThreadType, userId: string, threadId: string, title: string, workflowId?: string): Promise; addMessagesToThread(userId: string, threadId: string, messages: MessageObject[]): Promise; deleteThread(userId: string, threadId: string): Promise; listThreads(userId: string, filter?: ThreadFilter): Promise; updateThreadPin(userId: string, threadId: string, isPinned: boolean): Promise; } /** * Intent memory interface - handles intent operations */ interface IIntentMemory { getIntent(intentId: string): Promise; getIntentByName(intentName: string): Promise; saveIntent(intent: Intent): Promise; updateIntent(intentId: string, intent: Intent): Promise; deleteIntent(intentId: string): Promise; listIntents(): Promise; } /** * Agent memory interface for storing agent configuration */ interface IAgentMemory { getAgentPrompt(): Promise; updateAgentPrompt?(prompt: string): Promise; getAggregatePrompt?(): Promise; getGenerateTitlePrompt?(): Promise; getDocumentAdvicePrompt?(): Promise; getSingleTriggerPrompt?(): Promise; getMultiTriggerPrompt?(): Promise; getToolSelectPrompt?(): Promise; getPIIFilterPrompt?(): Promise; getPIIDetectPrompt?(): Promise; } /** * Workflow template memory interface - handles template operations */ interface IWorkflowTemplateMemory { getTemplate(templateId: string): Promise; createTemplate(template: WorkflowTemplate): Promise; updateTemplate(templateId: string, template: Partial): Promise; deleteTemplate(templateId: string): Promise; listTemplates(): Promise; } /** * User workflow memory interface - handles user workflow and scheduling operations */ interface IUserWorkflowMemory { getUserWorkflow(workflowId: string): Promise; createUserWorkflow(workflow: UserWorkflow): Promise; updateUserWorkflow(workflowId: string, workflow: Partial): Promise; deleteUserWorkflow(workflowId: string, userId: string): Promise; /** * When `options` is given, implementations sort by `updatedAt` desc and * apply offset/limit at the store. Implementations that also provide * countUserWorkflows are trusted to honor `options`; without it the * controller re-sorts/slices in memory (legacy providers ignore options). */ listUserWorkflows(userId?: string, options?: ListOptions): Promise; /** Total workflows for the user. Ships with ListOptions support above. */ countUserWorkflows?(userId?: string): Promise; /** List all active scheduled workflows across all users (used by scheduler) */ listActiveScheduledWorkflows(): Promise; } /** * Document memory interface - handles document persistence. * * Documents are first-class, mutable entities referenced from threads. */ interface IDocumentMemory { getDocument(documentId: string): Promise; createDocument(document: Document): Promise; updateDocument(documentId: string, document: Partial): Promise; /** * Atomically patch a single slot of a document. Concurrent fills of * different slots must not clobber each other, so implementations MUST * target only the matched slot (e.g. Mongo's positional `$` operator) * rather than rewriting the whole `slots` array from a caller snapshot, * and MUST bump `version`/`updatedAt` in the same write. Keys whose value * is `undefined` are removed from the slot. */ updateDocumentSlot(documentId: string, slotId: string, patch: Partial): Promise; deleteDocument(documentId: string): Promise; listDocuments(userId?: string, filter?: DocumentFilter): Promise; /** * Union (OR) of filter sets in a single query, sorted `updatedAt` desc. * Enables correct DB-level skip/limit/count across RBAC scopes. Optional: * when absent the controller falls back to per-set listDocuments plus * in-memory merge/sort/slice. */ listDocumentsAny?(filters: DocumentFilterSet[], options?: DocumentListOptions): Promise; /** Total count for the union of filter sets. Ships with listDocumentsAny. */ countDocumentsAny?(filters: DocumentFilterSet[]): Promise; /** Documents with an active, incomplete autoRefresh (used by the scheduler). */ listAutoRefreshPendingDocuments?(): Promise; /** Atomically append a slot to autoRefresh.doneSlotIds ($addToSet semantics). */ markAutoRefreshSlotDone?(documentId: string, slotId: string): Promise; /** Stamp autoRefresh.completedAt (job finished, never re-runs). */ completeAutoRefresh?(documentId: string, completedAt: number): Promise; } /** * Schedule run memory interface - execution history of scheduled jobs. */ interface IScheduleRunMemory { createScheduleRun(run: ScheduleRun): Promise; updateScheduleRun(runId: string, patch: Partial): Promise; /** Newest first (startedAt desc). */ listScheduleRuns(filter?: ScheduleRunFilter, limit?: number): Promise; /** * Mark runs stuck in "running" (process died mid-run) as failed with * error "interrupted". Returns the number of runs updated. */ failInterruptedRuns(): Promise; } export type { IAgentMemory, IDocumentMemory, IIntentMemory, IMemory, IScheduleRunMemory, IThreadMemory, IUserWorkflowMemory, IWorkflowTemplateMemory };