
    i.                        d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	m
Z
mZ  ej        e          Z G d de          ZdS )u  Abstract base class for pluggable memory providers.

Memory providers give the agent persistent recall across sessions. One
external provider is active at a time alongside the always-on built-in
memory (MEMORY.md / USER.md). The MemoryManager enforces this limit.

Built-in memory is always active as the first provider and cannot be removed.
External providers (Honcho, Hindsight, Mem0, etc.) are additive — they never
disable the built-in store. Only one external provider runs at a time to
prevent tool schema bloat and conflicting memory backends.

Registration:
  1. Built-in: BuiltinMemoryProvider — always present, not removable.
  2. Plugins: Ship in plugins/memory/<name>/, activated by memory.provider config.

Lifecycle (called by MemoryManager, wired in run_agent.py):
  initialize()          — connect, create resources, warm up
  system_prompt_block()  — static text for the system prompt
  prefetch(query)        — background recall before each turn
  sync_turn(user, asst)  — async write after each turn
  get_tool_schemas()     — tool schemas to expose to the model
  handle_tool_call()     — dispatch a tool call
  shutdown()             — clean exit

Optional hooks (override to opt in):
  on_turn_start(turn, message, **kwargs) — per-turn tick with runtime context
  on_session_end(messages)               — end-of-session extraction
  on_session_switch(new_session_id, **kwargs) — mid-process session_id rotation
  on_pre_compress(messages) -> str       — extract before context compression
  on_memory_write(action, target, content, metadata=None) — mirror built-in memory writes
  on_delegation(task, result, **kwargs)  — parent-side observation of subagent work
    )annotationsN)ABCabstractmethod)AnyDictListOptionalc                     e Zd ZdZeed7d                        Zed8d            Zed9d	            Zd7d
Z	ddd:dZ
ddd;dZddd<dZed=d            Zd>dZd?dZd@dZdAdZdd d!dBd%ZdCd&Zdd'dDd+Zd=d,ZdEd/Z	 dFdGd6Zd0S )HMemoryProviderz)Abstract base class for memory providers.returnstrc                    dS )zKShort identifier for this provider (e.g. 'builtin', 'honcho', 'hindsight').N selfs    :/home/ubuntu/.hermes/hermes-agent/agent/memory_provider.pynamezMemoryProvider.name.             boolc                    dS )u   Return True if this provider is configured, has credentials, and is ready.

        Called during agent init to decide whether to activate the provider.
        Should not make network calls — just check config and installed deps.
        Nr   r   s    r   is_availablezMemoryProvider.is_available5   r   r   
session_idNonec                    dS )a  Initialize for a session.

        Called once at agent startup. May create resources (banks, tables),
        establish connections, start background threads, etc.

        kwargs always include:
          - hermes_home (str): The active HERMES_HOME directory path. Use this
            for profile-scoped storage instead of hardcoding ``~/.hermes``.
          - platform (str): "cli", "telegram", "discord", "cron", etc.

        kwargs may also include:
          - agent_context (str): "primary", "subagent", "cron", or "flush".
            Providers should skip writes for non-primary contexts (cron system
            prompts would corrupt user representations).
          - agent_identity (str): Profile name (e.g. "coder"). Use for
            per-profile provider identity scoping.
          - agent_workspace (str): Shared workspace name (e.g. "hermes").
          - parent_session_id (str): For subagents, the parent's session_id.
          - user_id (str): Platform user identifier (gateway sessions).
        Nr   )r   r   kwargss      r   
initializezMemoryProvider.initialize=   r   r   c                    dS )a  Return text to include in the system prompt.

        Called during system prompt assembly. Return empty string to skip.
        This is for STATIC provider info (instructions, status). Prefetched
        recall context is injected separately via prefetch().
         r   r   s    r   system_prompt_blockz"MemoryProvider.system_prompt_blockT   s	     rr   r   )r   queryc                   dS )u  Recall relevant context for the upcoming turn.

        Called before each API call. Return formatted text to inject as
        context, or empty string if nothing relevant. Implementations
        should be fast — use background threads for the actual recall
        and return cached results here.

        session_id is provided for providers serving concurrent sessions
        (gateway group chats, cached agents). Providers that don't need
        per-session scoping can ignore it.
        r   r   r   r!   r   s      r   prefetchzMemoryProvider.prefetch]   s	     rr   c                   dS )u   Queue a background recall for the NEXT turn.

        Called after each turn completes. The result will be consumed
        by prefetch() on the next turn. Default is no-op — providers
        that do background prefetching should override this.
        Nr   r#   s      r   queue_prefetchzMemoryProvider.queue_prefetchk   r   r   user_contentassistant_contentc                   dS )u   Persist a completed turn to the backend.

        Called after each turn. Should be non-blocking — queue for
        background processing if the backend has latency.
        Nr   )r   r'   r(   r   s       r   	sync_turnzMemoryProvider.sync_turns   r   r   List[Dict[str, Any]]c                    dS )a   Return tool schemas this provider exposes.

        Each schema follows the OpenAI function calling format:
        {"name": "...", "description": "...", "parameters": {...}}

        Return empty list if this provider has no tools (context-only).
        Nr   r   s    r   get_tool_schemaszMemoryProvider.get_tool_schemasz   r   r   	tool_nameargsDict[str, Any]c                6    t          d| j         d|           )zHandle a tool call for one of this provider's tools.

        Must return a JSON string (the tool result).
        Only called for tool names returned by get_tool_schemas().
        z	Provider z does not handle tool )NotImplementedErrorr   )r   r.   r/   r   s       r   handle_tool_callzMemoryProvider.handle_tool_call   s%     ""Zdi"Z"Zy"Z"Z[[[r   c                    dS )u3   Clean shutdown — flush queues, close connections.Nr   r   s    r   shutdownzMemoryProvider.shutdown   r   r   turn_numberintmessagec                    dS )a  Called at the start of each turn with the user message.

        Use for turn-counting, scope management, periodic maintenance.

        kwargs may include: remaining_tokens, model, platform, tool_count.
        Providers use what they need; extras are ignored.
        Nr   )r   r6   r8   r   s       r   on_turn_startzMemoryProvider.on_turn_start   r   r   messagesc                    dS )u6  Called when a session ends (explicit exit or timeout).

        Use for end-of-session fact extraction, summarization, etc.
        messages is the full conversation history.

        NOT called after every turn — only at actual session boundaries
        (CLI exit, /reset, gateway session expiry).
        Nr   r   r;   s     r   on_session_endzMemoryProvider.on_session_end   r   r   F)parent_session_idresetnew_session_idr?   r@   c                   dS )u  Called when the agent switches session_id mid-process.

        Fires on ``/resume``, ``/branch``, ``/reset``, ``/new`` (CLI), the
        gateway equivalents, and context compression — any path that
        reassigns ``AIAgent.session_id`` without tearing the provider down.

        Providers that cache per-session state in ``initialize()``
        (``_session_id``, ``_document_id``, accumulated turn buffers,
        counters) should update or reset that state here so subsequent
        writes land in the correct session's record.

        Parameters
        ----------
        new_session_id:
            The session_id the agent just switched to.
        parent_session_id:
            The previous session_id, if meaningful — set for ``/branch``
            (fork lineage), context compression (continuation lineage),
            and ``/resume`` (the session we're leaving). Empty string
            when no lineage applies.
        reset:
            ``True`` when this is a genuinely new conversation, not a
            resumption of an existing one. Fired by ``/reset`` / ``/new``.
            Providers should flush accumulated per-session buffers
            (``_session_turns``, ``_turn_counter``, etc.) when this is
            set. ``False`` for ``/resume`` / ``/branch`` / compression
            where the logical conversation continues under the new id.

        Default is no-op for backward compatibility.
        Nr   )r   rA   r?   r@   r   s        r   on_session_switchz MemoryProvider.on_session_switch   r   r   c                    dS )a  Called before context compression discards old messages.

        Use to extract insights from messages about to be compressed.
        messages is the list that will be summarized/discarded.

        Return text to include in the compression summary prompt so the
        compressor preserves provider-extracted insights. Return empty
        string for no contribution (backwards-compatible default).
        r   r   r=   s     r   on_pre_compresszMemoryProvider.on_pre_compress   s	     rr   )child_session_idtaskresultrF   c                   dS )a  Called on the PARENT agent when a subagent completes.

        The parent's memory provider gets the task+result pair as an
        observation of what was delegated and what came back. The subagent
        itself has no provider session (skip_memory=True).

        task: the delegation prompt
        result: the subagent's final response
        child_session_id: the subagent's session_id
        Nr   )r   rG   rH   rF   r   s        r   on_delegationzMemoryProvider.on_delegation   r   r   c                    g S )a  Return config fields this provider needs for setup.

        Used by 'hermes memory setup' to walk the user through configuration.
        Each field is a dict with:
          key:         config key name (e.g. 'api_key', 'mode')
          description: human-readable description
          secret:      True if this should go to .env (default: False)
          required:    True if required (default: False)
          default:     default value (optional)
          choices:     list of valid values (optional)
          url:         URL where user can get this credential (optional)
          env_var:     explicit env var name for secrets (default: auto-generated)

        Return empty list if no config needed (e.g. local-only providers).
        r   r   s    r   get_config_schemaz MemoryProvider.get_config_schema   s	      	r   valueshermes_homec                    dS )a  Write non-secret config to the provider's native location.

        Called by 'hermes memory setup' after collecting user inputs.
        ``values`` contains only non-secret fields (secrets go to .env).
        ``hermes_home`` is the active HERMES_HOME directory path.

        Providers with native config files (JSON, YAML) should override
        this to write to their expected location. Providers that use only
        env vars can leave the default (no-op).

        All new memory provider plugins MUST implement either:
        - save_config() for native config file formats, OR
        - use only env vars (in which case get_config_schema() fields
          should all have ``env_var`` set and this method stays no-op).
        Nr   )r   rM   rN   s      r   save_configzMemoryProvider.save_config   r   r   NactiontargetcontentmetadataOptional[Dict[str, Any]]c                    dS )a  Called when the built-in memory tool writes an entry.

        action: 'add', 'replace', or 'remove'
        target: 'memory' or 'user'
        content: the entry content
        metadata: structured provenance for the write, when available. Common
          keys include ``write_origin``, ``execution_context``, ``session_id``,
          ``parent_session_id``, ``platform``, and ``tool_name``.

        Use to mirror built-in memory writes to your backend.
        Nr   )r   rQ   rR   rS   rT   s        r   on_memory_writezMemoryProvider.on_memory_write  r   r   )r   r   )r   r   )r   r   r   r   )r!   r   r   r   r   r   )r!   r   r   r   r   r   )r'   r   r(   r   r   r   r   r   )r   r+   )r.   r   r/   r0   r   r   )r   r   )r6   r7   r8   r   r   r   )r;   r+   r   r   )rA   r   r?   r   r@   r   r   r   )r;   r+   r   r   )rG   r   rH   r   rF   r   r   r   )rM   r0   rN   r   r   r   )N)
rQ   r   rR   r   rS   r   rT   rU   r   r   )__name__
__module____qualname____doc__propertyr   r   r   r   r    r$   r&   r*   r-   r3   r5   r:   r>   rC   rE   rJ   rL   rP   rW   r   r   r   r   r   +   s"       33Z Z Z ^ XZ
    ^    ^,    9;       ?A       Y[          ^\ \ \ \B B B B
       "$% % % % % %N
 
 
 
 /1        $   , .2      r   r   )r[   
__future__r   loggingabcr   r   typingr   r   r   r	   	getLoggerrX   loggerr   r   r   r   <module>rc      s    B # " " " " "  # # # # # # # # , , , , , , , , , , , ,		8	$	$m m m m mS m m m m mr   