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.
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 (app.py) serves as the web interface and coordination layer for Cognisphere. It:
- Initializes services (database, embedding, identity store)
- Creates and configures the agents
- Sets up API endpoints for frontend communication
- Manages session state and persistence
- 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'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:
- Agents: The reasoning entities that process information
- Tools: Functions that agents can use to perform actions
- State: Memory that persists across conversation turns
- 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
Cognisphere's memory system has multiple layers:
- Session State: Information specific to the current conversation thread
- User State: Information persisted across all sessions for one user
- 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
The Narrative system is responsible for organizing experiences into coherent storylines. It uses:
create_narrative_thread: Creates storylines with titles and themesadd_thread_event: Adds significant events to existing threadsget_active_threads: Retrieves ongoing narrativesgenerate_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.
The Identity system allows the agent to adopt different personas:
create_identity: Forms new identities with detailed attributesswitch_to_identity: Changes the active identity contextupdate_identity: Modifies existing identitieslink_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.
Cognisphere includes three major integration systems:
MCP allows Cognisphere to connect to external language model servers and tools:
MCPToolset: Manages connections to MCP serversMCPClient: Handles communication with MCP endpointsMCPServerManager: 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
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
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
To change how agents behave:
-
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 inmemory_agent.py. -
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
toolslist increate_memory_agent
- Create the function in
-
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.
-
Database Configuration: To change how data is stored, modify the
DatabaseServiceindatabase.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
- Changing the
-
Session Storage: To modify session persistence, change the
SessionServiceimplementation inapp.py. Options include:InMemorySessionService: For testing (data lost on restart)DatabaseSessionService: For persistent storage in a relational databaseVertexAiSessionService: For using Google Cloud storage
-
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()
- Register a new server using
-
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
- Add new functions to
-
AIRA Network Expansion: To enhance AIRA integration:
- Update the
register_all_cognisphere_tools_with_aira()function inaira/tools.py - Add adapters for new Cognisphere tools in the corresponding sections
- Modify hub connection parameters in
aira/client.py
- Update the
The frontend UI is built with HTML, CSS, and JavaScript in index.html. To modify the interface:
- Sections: The UI is divided into panels - chat panel and info panel. Edit the HTML structure to add or modify sections.
- API Communication: Frontend JavaScript makes fetch requests to the Flask API endpoints. Modify these function calls to change data flow.
- Styling: Update the CSS at the top of the file to change appearance.
- Interaction: Event listeners at the bottom handle user interactions.
To effectively modify Cognisphere, these are the most important files to understand:
-
app.py - Main application entry point and Flask server setup
-
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
-
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
-
services/ - Core service implementations
- database.py - Vector database operations
- embedding.py - Text embedding functionality
-
mcpIntegration/ - MCP protocol implementation
- toolset.py - Integration between ADK and MCP
- server_installer.py - MCP server management
-
aira/ - AIRA network implementation
- client.py - AIRA hub connectivity
- tools.py - Tool registration and discovery
-
data_models/ - Data structures
- memory.py - Memory data model
- narrative.py - Narrative thread model
- identity.py - Identity profile model
-
index.html - Frontend UI implementation
When a user sends a message, this is what happens in Cognisphere:
- The UI sends a POST request to
/api/chatwith the message and session ID - 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
- 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
- After processing, the system:
- Updates the session state
- Processes any new memories or narrative elements
- Updates the UI with the response
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.