The Context Management System uses a “pull” strategy where the AI model naturally requests context through tool calls rather than having context pushed to it. This ensures seamless integration with the Gemini Live API.
Recent Updates
Fixes
Fixed getContext tool registration and added automatic system prompt augmentation for context tool usage. The AI now proactively calls the tool when needing user information, page context, or memories.
Architecture Overview
1
Context Sources
Multiple sources feed into the context system:
- Page Metadata (URL, title, domain)
- User Information (preferences, settings)
- Memory Search (semantic search)
- Custom Context (application-specific)
2
Context State Manager
Centralized state storage with:
- Context formatting for LLM
- Update tracking and management
- Type-safe state access
3
Context Memory Manager
Automatic memory handling:
- Transcription-based searches
- Debouncing and thresholds
- Memory backend integration
4
GetContext Tool
Pull-based retrieval:
- Synchronous tool execution
- INTERRUPT scheduling for immediate use
- No API calls - just formatting
Key Components
SystemPromptAugmentor
The
SystemPromptAugmentor
is a singleton that manages all system prompt modifications, ensuring the AI knows about available tools and context.ContextStateManager
The central state management class for all contextual information:- Page Metadata
- User Information
- Custom Context
- Format Output
ContextMemoryManager
Handles automatic memory searches based on transcriptions:Automatic Context Tracking
SAMMY Three provides hooks for automatic context updates without manual intervention:useContextUpdater Hook
The
useContextUpdater
hook automatically tracks page changes and updates context in real-time.Manual Context Updates
For more granular control, update context manually:Context Lifecycle
Initialization
Context managers are created when the agent starts
Automatic Updates
Page changes and user actions trigger context updates
Memory Search
Transcriptions automatically trigger memory searches
AI Retrieval
AI calls getContext tool when needed
Cleanup
Context is cleared when agent stops
Integration Guide
Basic Setup
Context management is automatically initialized when creating a new agent. No manual setup required!
Manual Context Updates
- Page Context
- User Context
- Custom Context
React Hook Usage
For React applications, use theuseContextUpdater
hook:
Context Flow Example
1
Page Navigation
User navigates to a new page
- URL change detected by context updater
- Page metadata updated in ContextStateManager
2
User Speech
User speaks: “What is my name?”
- Transcription processed by ContextMemoryManager
- Memory search triggered after 20+ characters
- Relevant memories stored in context state
3
AI Response
AI recognizes it needs context
- System prompt instructs AI to use getContext tool
- AI calls getContext with query “user information”
- Tool retrieves formatted context from ContextStateManager
- Context returned with INTERRUPT scheduling
- AI uses context: “Your name is Joseph Marinio”
System Prompt Augmentations
System Prompt Augmentation Details
- Context Tool Instructions (Priority: 10)
- Language Instructions (Priority: 5)
- Memory Management (Priority: 20)
- Tool Usage Guidelines (Priority: 30)
Context Formatting
Tool Response Format
Context Retrieved
The system formats context with clear sections for easy AI parsing:
Custom Formatting Options
Configuration Reference
Memory Search Configuration
Context State Structure
Best Practices
Update Proactively
- Use context updater hook for automatic page tracking
- Update user context after authentication
- Set custom context for important state changes
Optimize Memory Search
- Adjust thresholds based on your use case
- Use debouncing to prevent excessive searches
- Consider feature-scoped vs global searches
Use Custom Context
- Store workflow states
- Track user actions
- Add temporary contextual data
Performance Tips
- Context is only formatted when requested
- Memory searches are debounced
- State updates are lightweight
Troubleshooting
GetContext Tool Not Being Called
If the AI isn’t calling the getContext tool, follow these steps:
1
Check Registration
Run
agent.debugContextSystem()
to verify the tool is registered2
Verify System Prompt
Check console logs to ensure system prompt includes context instructions
3
Test Manually
Try asking explicit questions like “What is my name?” or “What page am I on?”
4
Check Logs
Look for these log messages:
🔧 [SAMMY-AGENT-CORE] Registered getContext tool
✅ [SAMMY-AGENT-CORE] getContext tool is present in declarations
Memory Search Not Working
1
Check Thresholds
Ensure transcription meets minimum character thresholds
2
Verify API Connection
Check memory service is properly initialized
3
Monitor Logs
Look for
💭 [CONTEXT-MEMORY-MANAGER]
messagesDebug Logging
Enable debug logging to see context updates:Migration from Legacy System
The old MemoryManager has been completely removed. All memory management now goes through the centralized context system.
Future Enhancements
When Google fixes the
sendClientContent
bug, the system can easily switch from tool-based to text-based injection: