Class ConversationManager

ConversationManager

Description

Manages ConversationContext instances for AgentOS, handling their creation, retrieval, in-memory caching, and persistent storage via sql-storage-adapter. This class is vital for maintaining conversational state across user sessions and GMI interactions.

Constructors

Methods

  • Initializes the ConversationManager with its configuration and dependencies. This method sets up persistence if enabled and prepares the manager for operation.

    Parameters

    • config: ConversationManagerConfig

      Configuration for the manager.

    • Optional utilityAIService: IUtilityAI

      Optional IUtilityAI instance, primarily used by ConversationContext instances for features like summarization.

    • Optional storageAdapter: StorageAdapter

      Optional storage adapter for database persistence. Required if config.persistenceEnabled is true.

    Returns Promise<void>

    A promise that resolves when initialization is complete.

    Async

    Throws

    If configuration is invalid or dependencies are missing when required.

  • Creates a new conversation context or retrieves an existing one. If conversationId is provided:

    • Tries to find it in the active (in-memory) cache.
    • If not in cache and persistence is enabled, tries to load from the database.
    • If not found in DB or persistence disabled, creates a new context with this ID. If no conversationId is provided, a new one is generated. Manages in-memory cache size by evicting the oldest conversation if capacity is reached.

    Parameters

    • Optional conversationId: string

      Optional ID of an existing conversation. This ID will also be used as the ConversationContext.sessionId.

    • Optional userId: string

      ID of the user associated with the conversation.

    • Optional gmiInstanceId: string

      ID of the GMI instance this conversation is for.

    • Optional activePersonaId: string

      ID of the active persona for the conversation.

    • Optional initialMetadata: Record<string, any> = {}

      Initial metadata for a new conversation.

    • Optional overrideConfig: Partial<ConversationContextConfig>

      Config overrides for a new context.

    Returns Promise<ConversationContext>

    The created or retrieved ConversationContext.

    Async

    Throws

    If essential parameters for creating a new context are missing or if an error occurs.

  • Retrieves a ConversationContext if present in memory or persistent storage. Returns null when not found.

    Parameters

    • conversationId: string

    Returns Promise<null | ConversationContext>

  • Lists minimal context info for a given session. Currently returns a single entry matching the provided sessionId if found in memory or storage.

    Parameters

    • sessionId: string

    Returns Promise<{
        sessionId: string;
        createdAt: number;
    }[]>

  • Saves a ConversationContext to persistent storage if persistence is enabled. This is called automatically when a context is evicted from memory or during shutdown.

    Parameters

    • context: ConversationContext

      The ConversationContext to save.

    Returns Promise<void>

    Async

    Throws

    If the save operation fails.

  • Deletes a conversation from both memory and persistent storage.

    Parameters

    • conversationId: string

      The ID of the conversation to delete.

    Returns Promise<void>

    Async

    Throws

    If the deletion fails.

  • Gets basic info about a conversation (ID and creation timestamp). Checks in-memory cache first, then persistent storage if enabled.

    Parameters

    • sessionId: string

      The ID of the conversation.

    Returns Promise<{
        sessionId: string;
        createdAt: number;
    }[]>

    Array with conversation info, or empty if not found.

    Async

  • Gets the last active time for a conversation, typically the timestamp of the last message or update. Checks in-memory cache first, then persistent storage if enabled.

    Parameters

    • conversationId: string

      The ID of the conversation.

    Returns Promise<undefined | number>

    Timestamp of last activity (Unix epoch ms), or undefined if not found.

    Async

  • Shuts down the ConversationManager. If persistence is enabled, ensures all active conversations are saved to the database. Clears the in-memory cache of conversations.

    Returns Promise<void>

    Async

Properties

managerId: string

Unique identifier for this ConversationManager instance.