import type { EmbeddingModel } from 'ai'; import type { AgentExecutionCounter, ModelConfig, SerializableAgentState } from './agent'; import type { AgentDbMessage } from './message'; import type { BuiltObservationLogStore, ObservationLogEntry, ObservationLogObserveFn, ObservationLogReflectFn, ObservationLogScope } from './observation-log'; import type { JSONObject } from '../utils/json'; export interface MemoryDescriptor { name: string; constructorName: string; connectionParams: TParams | null; } export interface Thread { id: string; resourceId: string; title?: string; createdAt: Date; updatedAt: Date; metadata?: Record; } export interface BuiltMemory { getThread(threadId: string): Promise; saveThread(thread: Omit): Promise; deleteThread(threadId: string): Promise; getMessages(threadId: string, opts?: { limit?: number; before?: Date; resourceId?: string; }): Promise; saveMessages(args: { threadId: string; resourceId: string; messages: AgentDbMessage[]; }): Promise; deleteMessages(messageIds: string[]): Promise; episodic?: EpisodicMemoryMethods; close?(): Promise; describe(): MemoryDescriptor; } export type EpisodicMemoryStatus = 'active' | 'superseded' | 'dropped'; export interface EpisodicMemoryScope { resourceId: string; } export interface EpisodicMemoryEntry { id: string; resourceId: string; content: string; contentHash: string; status: EpisodicMemoryStatus; supersededBy: string | null; embedding?: number[]; embeddingModel?: string; metadata?: JSONObject | null; createdAt: Date; updatedAt: Date; lastSeenAt: Date; } export type NewEpisodicMemoryEntry = Omit & { contentHash?: string; createdAt?: Date; lastSeenAt?: Date; }; export interface EpisodicMemoryEntrySource { id: string; memoryEntryId: string; observationId: string; threadId: string; evidenceText: string; createdAt: Date; } export type NewEpisodicMemoryEntrySource = Omit & { createdAt?: Date; }; export type NewEpisodicMemoryEntrySourceForEntry = Omit; export interface EpisodicMemoryCursor extends ObservationLogScope { lastIndexedObservationId: string; lastIndexedObservationCreatedAt: Date; updatedAt: Date; } export type NewEpisodicMemoryCursor = Omit & { updatedAt?: Date; }; export interface RetrievedEpisodicMemoryEntry extends EpisodicMemoryEntry { lexicalScore: number; vectorScore: number; rrfScore: number; finalScore: number; } export interface EpisodicMemorySearchOptions { topK?: number; queryEmbedding?: number[]; includeStatuses?: EpisodicMemoryStatus[]; } export interface EpisodicMemoryTaskLockHandle { resourceId: string; holderId: string; heldUntil: Date; } export interface EpisodicMemoryTaskLockMethods { acquire(resourceId: string, opts: { ttlMs: number; holderId: string; }): Promise; release(handle: EpisodicMemoryTaskLockHandle): Promise; } export interface EpisodicMemoryMethods { saveEntryWithSources(entry: NewEpisodicMemoryEntry, sources: NewEpisodicMemoryEntrySourceForEntry[]): Promise; searchEntries(scope: EpisodicMemoryScope, query: string, opts?: EpisodicMemorySearchOptions): Promise; getEntrySources(entryIds: string[]): Promise; applyReflection(scope: EpisodicMemoryScope, reflection: EpisodicMemoryReflectionApply): Promise; getCursor(scope: ObservationLogScope): Promise; setCursor(cursor: NewEpisodicMemoryCursor): Promise; taskLock?: EpisodicMemoryTaskLockMethods; } export interface BuiltEpisodicMemoryStore { episodic: EpisodicMemoryMethods; } export interface EpisodicMemoryExtractionCandidate { content: string; sources: Array<{ observationId: string; evidence: string; }>; } export interface EpisodicMemoryExtractorInput { scope: EpisodicMemoryScope; observationScope: ObservationLogScope; now: Date; observations: ObservationLogEntry[]; renderedObservations: string; existingEntries: RetrievedEpisodicMemoryEntry[]; executionCounter?: AgentExecutionCounter; } export interface EpisodicMemoryExtraction { entries: EpisodicMemoryExtractionCandidate[]; } export type EpisodicMemoryExtractFn = (input: EpisodicMemoryExtractorInput) => Promise; export interface EpisodicMemoryReflectionMerge { supersedes: string[]; content: string; } export interface EpisodicMemoryReflection { drop: string[]; merge: EpisodicMemoryReflectionMerge[]; } export interface EpisodicMemoryReflectorInput { scope: EpisodicMemoryScope; now: Date; seedEntryIds: string[]; entries: RetrievedEpisodicMemoryEntry[]; sources: EpisodicMemoryEntrySource[]; executionCounter?: AgentExecutionCounter; } export type EpisodicMemoryReflectFn = (input: EpisodicMemoryReflectorInput) => Promise; export interface EpisodicMemoryReflectionApplyMerge { supersedes: string[]; entry: NewEpisodicMemoryEntry; } export interface EpisodicMemoryReflectionApply { drop: string[]; merge: EpisodicMemoryReflectionApplyMerge[]; } export interface EpisodicMemoryReflectionResult { droppedIds: string[]; supersededIds: string[]; inserted: EpisodicMemoryEntry[]; } export interface EpisodicMemoryPrompts { extraction?: string; reflection?: string; recallToolInstruction?: string; } export interface EpisodicMemoryEmbeddingProviderOptions { apiKey?: string; baseURL?: string; fetch?: typeof globalThis.fetch; } export interface EpisodicMemoryConfig { enabled?: boolean; topK?: number; maxEntriesPerRun?: number; embedder?: EmbeddingModel; embeddingModel?: string; embeddingProviderOptions?: string | EpisodicMemoryEmbeddingProviderOptions; extract?: EpisodicMemoryExtractFn; reflect?: EpisodicMemoryReflectFn; prompts?: EpisodicMemoryPrompts; } export interface TitleGenerationConfig { model?: ModelConfig; instructions?: string; sync?: boolean; } export type ObservationCapableMemory = BuiltMemory & BuiltObservationLogStore; export interface ObservationLogMemoryConfig { renderTokenBudget?: number; } export interface ObservationalMemoryConfig { observerThresholdTokens?: number; reflectorThresholdTokens?: number; renderTokenBudget?: number; observationLogTailLimit?: number; lockTtlMs?: number; observe?: ObservationLogObserveFn; reflect?: ObservationLogReflectFn; } interface MemoryConfigBase { observationLog?: ObservationLogMemoryConfig; episodicMemory?: EpisodicMemoryConfig; titleGeneration?: TitleGenerationConfig; } export type MemoryConfig = (MemoryConfigBase & { memory: BuiltMemory; observationalMemory?: undefined; }) | (MemoryConfigBase & { memory: ObservationCapableMemory; observationalMemory: ObservationalMemoryConfig; }); export interface CheckpointStore { save(key: string, state: SerializableAgentState): Promise; load(key: string): Promise; claimForResume?(key: string, state: SerializableAgentState): Promise; delete(key: string): Promise; } export {};