Skip to content

Latest commit

 

History

History
249 lines (178 loc) · 11.4 KB

File metadata and controls

249 lines (178 loc) · 11.4 KB

Understanding the Cognisphere Framework: A Comprehensive Guide

Cognisphere is a sophisticated cognitive architecture built on the Google Agent Development Kit (ADK) that combines memory management, narrative creation, identity handling, and external tool integration to create a powerful AI system. In this guide, I'll explain how Cognisphere works, its key components, and provide practical advice on customization.

Core Architecture Overview

Cognisphere is organized around multiple specialized agents working together through an orchestrator:

The main coordinator is the Orchestrator Agent, which delegates tasks to specialized sub-agents:

  • Memory Agent: Handles storing and retrieving memories
  • Narrative Agent: Manages narrative threads and storytelling
  • Identity Agent: Manages different identity profiles and switching between them

This multi-agent design allows Cognisphere to handle complex cognitive tasks by breaking them down into specialized functions.

The Flask Application Structure

The Flask application (app.py) serves as the web interface and coordination layer for Cognisphere. It:

  1. Initializes services (database, embedding, identity store)
  2. Creates and configures the agents
  3. Sets up API endpoints for frontend communication
  4. Manages session state and persistence
  5. Handles routing between components

Flask creates several REST API endpoints that the frontend JavaScript calls to interact with the system, including:

  • /api/chat - For sending messages to the orchestrator
  • /api/sessions - For managing conversation sessions
  • /api/memories - For retrieving memory information
  • /api/identities - For identity management
  • /api/narratives - For accessing narrative threads

Google ADK Implementation

Google's Agent Development Kit (ADK) provides the foundation for Cognisphere's agent architecture. ADK is a code-first Python framework that enables structured agent development with:

  1. Agents: The reasoning entities that process information
  2. Tools: Functions that agents can use to perform actions
  3. State: Memory that persists across conversation turns
  4. Events: Records of interactions that form the conversation history

Cognisphere leverages ADK's event-driven architecture where:

  • The Runner orchestrates interactions between agents
  • Agents yield events (like messages or function calls)
  • The system maintains session state across interactions
  • Callbacks allow customization of agent behavior

Memory Management in Cognisphere

Cognisphere's memory system has multiple layers:

  1. Session State: Information specific to the current conversation thread
  2. User State: Information persisted across all sessions for one user
  3. Long-Term Memory: A searchable knowledge store across all conversations

Memory is stored and retrieved through the DatabaseService and EmbeddingService, which work together to create vector embeddings of text and enable semantic search.

The Memory Agent uses tools like create_memory and recall_memories to:

  • Store new memories of different types (explicit, emotional, flashbulb, etc.)
  • Retrieve relevant memories based on semantic searches
  • Associate memories with specific identities

Narrative System

The Narrative system is responsible for organizing experiences into coherent storylines. It uses:

  1. create_narrative_thread: Creates storylines with titles and themes
  2. add_thread_event: Adds significant events to existing threads
  3. get_active_threads: Retrieves ongoing narratives
  4. generate_narrative_summary: Creates summaries of narrative developments

Narratives can be linked to specific identities, creating a personalized story framework for each identity in the system.

Identity Management

The Identity system allows the agent to adopt different personas:

  1. create_identity: Forms new identities with detailed attributes
  2. switch_to_identity: Changes the active identity context
  3. update_identity: Modifies existing identities
  4. link_identity_to_narrative: Connects identities with narrative threads

When an identity is active, the agent's behavior is modified by injecting identity-specific instructions into the LLM's prompt, affecting its tone, personality, and how it responds to queries. This is handled by special callbacks that modify the LLM request before it's sent.

External Integration Systems

Cognisphere includes three major integration systems:

1. MCP (Model Context Protocol)

MCP allows Cognisphere to connect to external language model servers and tools:

  • MCPToolset: Manages connections to MCP servers
  • MCPClient: Handles communication with MCP endpoints
  • MCPServerManager: Manages configurations for MCP servers

Through MCP, Cognisphere can:

  • Discover available tools from external servers
  • Call these tools and integrate their responses
  • Share resources and information between different systems

2. A2A (Agent-to-Agent) Protocol

The A2A protocol enables direct communication between different agent systems:

  • Standardized messaging format between agents
  • Task-based communication model
  • Discovery of other agents' capabilities

Cognisphere implements A2A through:

  • An endpoint that exposes capabilities as an agent card
  • Functions to handle incoming task requests
  • A client for communicating with other A2A-compatible agents

3. AIRA (Agent Interoperability and Resource Access)

AIRA provides a network layer for agent communication:

  • Registration with a central hub
  • Discovery of other agents on the network
  • Invocation of tools across different agents

The AIRA implementation includes:

  • CognisphereAiraClient: Client for AIRA hub interactions
  • Hub registration and heartbeat mechanisms
  • Agent discovery and capabilities sharing
  • Tool invocation across the network

Customizing Cognisphere

Modifying Agent Behavior

To change how agents behave:

  1. Edit Agent Instructions: To modify what a specific agent does, edit its instruction parameter in the corresponding create_*_agent function (e.g., create_memory_agent, create_narrative_agent, etc.) in the respective agent.py files. For example, to change the Memory Agent's behavior, edit the instruction in memory_agent.py.

  2. Add New Tools: To give agents new capabilities, create new tool functions in the tools directory, then add them to the appropriate agent's tool list in its creation function. For example, to add a new memory tool:

    • Create the function in memory_tools.py
    • Add it to the tools list in create_memory_agent
  3. Customize Orchestrator Logic: To change the overall system behavior, modify the orchestrator's instruction in orchestrator_agent.py. This controls how it delegates tasks to sub-agents and integrates their responses.

Changing Storage and Persistence

  1. Database Configuration: To change how data is stored, modify the DatabaseService in database.py. The default setup uses ChromaDB for vector storage, but you can implement other databases by:

    • Changing the __init__ method to connect to your preferred database
    • Updating the query methods to work with your storage system
  2. Session Storage: To modify session persistence, change the SessionService implementation in app.py. Options include:

    • InMemorySessionService: For testing (data lost on restart)
    • DatabaseSessionService: For persistent storage in a relational database
    • VertexAiSessionService: For using Google Cloud storage

Adding Integration Capabilities

  1. New MCP Servers: To add new MCP server support:

    • Register a new server using server_manager.add_server()
    • Configure connection parameters (command, args, environment variables)
    • Connect to the server using toolset.register_server()
  2. Enhance A2A Capabilities: To expand A2A functionality:

    • Add new functions to a2a_tools.py
    • Create wrapper functions that convert between ADK and A2A formats
    • Register these as tools with the orchestrator
  3. AIRA Network Expansion: To enhance AIRA integration:

    • Update the register_all_cognisphere_tools_with_aira() function in aira/tools.py
    • Add adapters for new Cognisphere tools in the corresponding sections
    • Modify hub connection parameters in aira/client.py

UI Customization

The frontend UI is built with HTML, CSS, and JavaScript in index.html. To modify the interface:

  1. Sections: The UI is divided into panels - chat panel and info panel. Edit the HTML structure to add or modify sections.
  2. API Communication: Frontend JavaScript makes fetch requests to the Flask API endpoints. Modify these function calls to change data flow.
  3. Styling: Update the CSS at the top of the file to change appearance.
  4. Interaction: Event listeners at the bottom handle user interactions.

Key Files and Their Functions

To effectively modify Cognisphere, these are the most important files to understand:

  1. app.py - Main application entry point and Flask server setup

  2. agents/ - Contains all agent definitions

    • orchestrator_agent.py - The main coordinator agent
    • memory_agent.py - Memory management agent
    • narrative_agent.py - Narrative creation agent
    • identity_agent.py - Identity handling agent
    • mcp_agent.py - MCP integration agent
  3. tools/ - Contains all tool implementations

    • memory_tools.py - Memory creation/retrieval tools
    • narrative_tools.py - Narrative management tools
    • identity_tools.py - Identity management tools
    • a2a_tools.py - A2A protocol tools
    • enhanced_a2a_tools.py - Advanced A2A capabilities
  4. services/ - Core service implementations

    • database.py - Vector database operations
    • embedding.py - Text embedding functionality
  5. mcpIntegration/ - MCP protocol implementation

    • toolset.py - Integration between ADK and MCP
    • server_installer.py - MCP server management
  6. aira/ - AIRA network implementation

    • client.py - AIRA hub connectivity
    • tools.py - Tool registration and discovery
  7. data_models/ - Data structures

    • memory.py - Memory data model
    • narrative.py - Narrative thread model
    • identity.py - Identity profile model
  8. index.html - Frontend UI implementation

Workflow Example: Message Processing

When a user sends a message, this is what happens in Cognisphere:

  1. The UI sends a POST request to /api/chat with the message and session ID
  2. Flask calls process_message() which:
    • Ensures the session exists or creates a new one
    • Formats the message as an ADK Content object
    • Calls the Runner to process the message
  3. The Runner:
    • Passes the message to the Orchestrator Agent
    • The Orchestrator decides which sub-agent should handle it
    • The appropriate sub-agent processes it and returns a response
    • Results are sent back to the frontend
  4. After processing, the system:
    • Updates the session state
    • Processes any new memories or narrative elements
    • Updates the UI with the response

Conclusion

Cognisphere represents a sophisticated implementation of Google's ADK that demonstrates the power of multi-agent architectures combined with memory, narrative, and identity systems. By understanding its components and how they interact, you can customize and extend it to suit your specific needs.

Whether you want to modify agent behavior, add new capabilities, or integrate with external systems, this guide should provide the foundation you need to start making meaningful changes to the Cognisphere architecture.