15 Commits

Author SHA1 Message Date
serversdown 6d88505697 chore: add sessions to gitignore 2026-05-29 18:23:29 -04:00
Claude 0ee5a9ce47 feat: SQLite-backed memory with brute-force cosine recall
- lyra.memory.remember(session_id, role, content) embeds and stores
- lyra.memory.recent(session_id, n) returns the last N from a session
- lyra.memory.recall(query, k, session_id=None) returns top-k by cosine
  similarity across the chosen scope (all sessions by default)
- Embeddings live in the exchanges.embedding BLOB column as float32 bytes
- Connection reopens automatically if LYRA_DB_PATH changes (test-friendly)
2026-05-16 06:35:52 +00:00
Claude 6a1255dfdb feat: LLM router with local (Ollama) and cloud (OpenAI) backends
- lyra.config.load() reads env into a frozen Config dataclass
- lyra.llm.complete(messages, backend) routes to Ollama /api/chat or
  OpenAI chat completions
- lyra.llm.embed(texts) calls OpenAI embeddings
- .env.example switched from Anthropic to OpenAI to match available key
2026-05-16 06:10:48 +00:00
Claude b2523c2561 chore: project scaffold (uv, .env.example, README, lyra package) 2026-05-16 06:01:08 +00:00
Claude faf4e8a1aa chore: nuke legacy code, keep design docs for restart
Preserved on the archive branch. Keeping only the architecture and
design thinking that survives the rewrite:

- docs/ARCH_v0-6-1.md (Inner Self / Executive / Chat / Persona model)
- docs/ARCHITECTURE_v0-6-0.md (predecessor architecture)
- docs/PROJECT_SUMMARY.md (project history and rationale)
- docs/PROJECT_LYRA_COMPLETE_BREAKDOWN.md (detailed design notes)
- docs/ENVIRONMENT_VARIABLES.md (multi-backend env conventions)
- docs/LLMS.md
- docs/TRILLIUM_API.md (for future tool integration)

Removed: all service code (cortex, core/relay, neomem, rag, sandbox,
persona-sidecar), docker-compose, migration/logging docs, stale root
test scripts, CHANGELOG.
2026-05-16 05:57:07 +00:00
claude 4b951f3be8 Merge pull request #16 from serversdwn/dev
update to 0.9.0
2025-12-29 01:59:14 -05:00
claude 6b5580a80e 0.9.0 - Added Trilium ETAPI integration.
Lyra can now: Search trilium notes and create new notes. with proper ETAPI auth.
2025-12-29 01:58:20 -05:00
claude 86b37ab874 feat: Implement Trillium notes executor for searching and creating notes via ETAPI
- Added `trillium.py` for searching and creating notes with Trillium's ETAPI.
- Implemented `search_notes` and `create_note` functions with appropriate error handling and validation.

feat: Add web search functionality using DuckDuckGo

- Introduced `web_search.py` for performing web searches without API keys.
- Implemented `search_web` function with result handling and validation.

feat: Create provider-agnostic function caller for iterative tool calling

- Developed `function_caller.py` to manage LLM interactions with tools.
- Implemented iterative calling logic with error handling and tool execution.

feat: Establish a tool registry for managing available tools

- Created `registry.py` to define and manage tool availability and execution.
- Integrated feature flags for enabling/disabling tools based on environment variables.

feat: Implement event streaming for tool calling processes

- Added `stream_events.py` to manage Server-Sent Events (SSE) for tool calling.
- Enabled real-time updates during tool execution for enhanced user experience.

test: Add tests for tool calling system components

- Created `test_tools.py` to validate functionality of code execution, web search, and tool registry.
- Implemented asynchronous tests to ensure proper execution and result handling.

chore: Add Dockerfile for sandbox environment setup

- Created `Dockerfile` to set up a Python environment with necessary dependencies for code execution.

chore: Add debug regex script for testing XML parsing

- Introduced `debug_regex.py` to validate regex patterns against XML tool calls.

chore: Add HTML template for displaying thinking stream events

- Created `test_thinking_stream.html` for visualizing tool calling events in a user-friendly format.

test: Add tests for OllamaAdapter XML parsing

- Developed `test_ollama_parser.py` to validate XML parsing with various test cases, including malformed XML.
2025-12-26 03:49:20 -05:00
claude 8b66cd1e1d update to 0.7.0
Standard Mode Implementation - Complete documentation of the new simple chatbot mode
Backend Selection System - UI settings modal and routing changes
Session Management Overhaul - File-based persistence with CRUD API
UI Improvements - Settings modal, light/dark mode, modal fixes
Context Retention - Integration with Intake for conversation history
Architecture & Routing Changes - Updates to Relay, Cortex, Intake, LLM router
Fixed Critical Issues - DeepSeek R1, context retention, OpenAI errors, modal formatting, session persistence
Technical Improvements - Backward compatibility, code quality, performance
Architecture Diagrams - Data flow for Standard Mode, Cortex Mode, and sessions
Known Limitations - Standard Mode constraints, session management limits
Migration Notes - For users and developers upgrading
2025-12-22 01:41:21 -05:00
claude 7cb7033bb6 docs updated v0.7.0 2025-12-22 01:40:24 -05:00
claude a4f5308f9b Merge pull request #9 from serversdwn/dev
Update to 0.6.0. Docs updated.
2025-12-19 17:44:11 -05:00
claude 628edb681a v0.5.2 update
Dev
2025-12-12 08:04:20 +00:00
claude 09b6b364e5 v0.5.1-Major cortex rework. clean up done too. Merge from dev
v0.5.1-Major cortex rework. clean up done too.
2025-12-11 03:48:29 -05:00
claude 1dd84613cf Merge pull request #4 from serversdwn/dev
Big clean up to v0.5.0, docs updated, restructured throughout.
2025-11-28 18:14:18 -05:00
claude 3b128ac7f6 Merge pull request #3 from serversdwn/dev
Dev branch reorganizing.
2025-11-26 02:32:31 -05:00
242 changed files with 1582 additions and 43016 deletions
+11 -87
View File
@@ -1,87 +1,11 @@
# ====================================
# 🌌 GLOBAL LYRA CONFIG
# ====================================
LOCAL_TZ_LABEL=America/New_York
DEFAULT_SESSION_ID=default
# ====================================
# 🤖 LLM BACKEND OPTIONS
# ====================================
# Services choose which backend to use from these options
# Primary: vLLM on MI50 GPU
LLM_PRIMARY_PROVIDER=vllm
LLM_PRIMARY_URL=http://10.0.0.43:8000
LLM_PRIMARY_MODEL=/model
# Secondary: Ollama on 3090 GPU
LLM_SECONDARY_PROVIDER=ollama
LLM_SECONDARY_URL=http://10.0.0.3:11434
LLM_SECONDARY_MODEL=qwen2.5:7b-instruct-q4_K_M
# Cloud: OpenAI
LLM_CLOUD_PROVIDER=openai_chat
LLM_CLOUD_URL=https://api.openai.com/v1
LLM_CLOUD_MODEL=gpt-4o-mini
OPENAI_API_KEY=sk-proj-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# Local Fallback: llama.cpp or LM Studio
LLM_FALLBACK_PROVIDER=openai_completions
LLM_FALLBACK_URL=http://10.0.0.41:11435
LLM_FALLBACK_MODEL=llama-3.2-8b-instruct
# Global LLM controls
LLM_TEMPERATURE=0.7
# ====================================
# 🗄️ DATABASE CONFIGURATION
# ====================================
# Postgres (pgvector for NeoMem)
POSTGRES_USER=neomem
POSTGRES_PASSWORD=change_me_in_production
POSTGRES_DB=neomem
POSTGRES_HOST=neomem-postgres
POSTGRES_PORT=5432
# Neo4j Graph Database
NEO4J_URI=bolt://neomem-neo4j:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=change_me_in_production
NEO4J_AUTH=neo4j/change_me_in_production
# ====================================
# 🧠 MEMORY SERVICES (NEOMEM)
# ====================================
NEOMEM_API=http://neomem-api:7077
NEOMEM_API_KEY=generate_secure_random_token_here
NEOMEM_HISTORY_DB=postgresql://neomem:change_me_in_production@neomem-postgres:5432/neomem
# Embeddings configuration (used by NeoMem)
EMBEDDER_PROVIDER=openai
EMBEDDER_MODEL=text-embedding-3-small
# ====================================
# 🔌 INTERNAL SERVICE URLS
# ====================================
# Using container names for Docker network communication
INTAKE_API_URL=http://intake:7080
CORTEX_API=http://cortex:7081
CORTEX_URL=http://cortex:7081/reflect
CORTEX_URL_INGEST=http://cortex:7081/ingest
RAG_API_URL=http://rag:7090
RELAY_URL=http://relay:7078
# Persona service (optional)
PERSONA_URL=http://persona-sidecar:7080/current
# ====================================
# 🔧 FEATURE FLAGS
# ====================================
CORTEX_ENABLED=true
MEMORY_ENABLED=true
PERSONA_ENABLED=false
DEBUG_PROMPT=true
# Local backend (Ollama) — used by default for most calls.
LOCAL_BASE_URL=http://localhost:11434
LOCAL_MODEL=qwen2.5:7b-instruct
# Cloud backend (OpenAI) — used for harder reasoning and embeddings.
OPENAI_API_KEY=
CLOUD_MODEL=gpt-4o-mini
EMBED_MODEL=text-embedding-3-small
# Where Lyra stores her memory.
LYRA_DB_PATH=data/lyra.db
-132
View File
@@ -1,132 +0,0 @@
# ============================================================================
# CORTEX LOGGING CONFIGURATION
# ============================================================================
# This file contains all logging-related environment variables for the
# Cortex reasoning pipeline. Copy this to your .env file and adjust as needed.
#
# Log Detail Levels:
# minimal - Only errors and critical events
# summary - Stage completion + errors (DEFAULT - RECOMMENDED FOR PRODUCTION)
# detailed - Include raw LLM outputs, RAG results, timing breakdowns
# verbose - Everything including intermediate states, full JSON dumps
#
# Quick Start:
# - For debugging weak links: LOG_DETAIL_LEVEL=detailed
# - For finding performance bottlenecks: LOG_DETAIL_LEVEL=detailed + VERBOSE_DEBUG=true
# - For production: LOG_DETAIL_LEVEL=summary
# - For silent mode: LOG_DETAIL_LEVEL=minimal
# ============================================================================
# -----------------------------
# Primary Logging Level
# -----------------------------
# Controls overall verbosity across all components
LOG_DETAIL_LEVEL=detailed
# Legacy verbose debug flag (kept for compatibility)
# When true, enables maximum logging including raw data dumps
VERBOSE_DEBUG=false
# -----------------------------
# LLM Logging
# -----------------------------
# Enable raw LLM response logging (only works with detailed/verbose levels)
# Shows full JSON responses from each LLM backend call
# Set to "true" to see exact LLM outputs for debugging weak links
LOG_RAW_LLM_RESPONSES=true
# -----------------------------
# Context Logging
# -----------------------------
# Show full raw intake data (L1-L30 summaries) in logs
# WARNING: Very verbose, use only for deep debugging
LOG_RAW_CONTEXT_DATA=false
# -----------------------------
# Loop Detection & Protection
# -----------------------------
# Enable duplicate message detection to prevent processing loops
ENABLE_DUPLICATE_DETECTION=true
# Maximum number of messages to keep in session history (prevents unbounded growth)
# Older messages are trimmed automatically
MAX_MESSAGE_HISTORY=100
# Session TTL in hours - sessions inactive longer than this are auto-expired
SESSION_TTL_HOURS=24
# -----------------------------
# NeoMem / RAG Logging
# -----------------------------
# Relevance score threshold for NeoMem results
RELEVANCE_THRESHOLD=0.4
# Enable NeoMem long-term memory retrieval
NEOMEM_ENABLED=false
# -----------------------------
# Autonomous Features
# -----------------------------
# Enable autonomous tool invocation (RAG, WEB, WEATHER, CODEBRAIN)
ENABLE_AUTONOMOUS_TOOLS=true
# Confidence threshold for autonomous tool invocation (0.0 - 1.0)
AUTONOMOUS_TOOL_CONFIDENCE_THRESHOLD=0.6
# Enable proactive monitoring and suggestions
ENABLE_PROACTIVE_MONITORING=true
# Minimum priority for proactive suggestions to be included (0.0 - 1.0)
PROACTIVE_SUGGESTION_MIN_PRIORITY=0.6
# ============================================================================
# EXAMPLE LOGGING OUTPUT AT DIFFERENT LEVELS
# ============================================================================
#
# LOG_DETAIL_LEVEL=summary (RECOMMENDED):
# ────────────────────────────────────────────────────────────────────────────
# ✅ [LLM] PRIMARY | 14:23:45.123 | Reply: Based on your question about...
# 📊 Context | Session: abc123 | Messages: 42 | Last: 5.2min | RAG: 3 results
# 🧠 Monologue | question | Tone: curious
# ✨ PIPELINE COMPLETE | Session: abc123 | Total: 1250ms
# 📤 Output: 342 characters
# ────────────────────────────────────────────────────────────────────────────
#
# LOG_DETAIL_LEVEL=detailed (FOR DEBUGGING):
# ────────────────────────────────────────────────────────────────────────────
# 🚀 PIPELINE START | Session: abc123 | 14:23:45.123
# 📝 User: What is the meaning of life?
# ────────────────────────────────────────────────────────────────────────────
# 🧠 LLM CALL | Backend: PRIMARY | 14:23:45.234
# ────────────────────────────────────────────────────────────────────────────
# 📝 Prompt: You are Lyra, a thoughtful AI assistant...
# 💬 Reply: Based on philosophical perspectives, the meaning...
# ╭─ RAW RESPONSE ────────────────────────────────────────────────────────────
# │ {
# │ "choices": [
# │ {
# │ "message": {
# │ "content": "Based on philosophical perspectives..."
# │ }
# │ }
# │ ]
# │ }
# ╰───────────────────────────────────────────────────────────────────────────
#
# ✨ PIPELINE COMPLETE | Session: abc123 | Total: 1250ms
# ⏱️ Stage Timings:
# context : 150ms ( 12.0%)
# identity : 10ms ( 0.8%)
# monologue : 200ms ( 16.0%)
# reasoning : 450ms ( 36.0%)
# refinement : 300ms ( 24.0%)
# persona : 140ms ( 11.2%)
# ────────────────────────────────────────────────────────────────────────────
#
# LOG_DETAIL_LEVEL=verbose (MAXIMUM DEBUG):
# Same as detailed but includes:
# - Full 50+ line raw JSON dumps
# - Complete intake data structures
# - All intermediate processing states
# - Detailed traceback on errors
# ============================================================================
+28 -48
View File
@@ -1,57 +1,37 @@
# =============================
# 📦 General
# =============================
# Python
__pycache__/
*.pyc
*.log
/.vscode/
.vscode/
# =============================
# 🔐 Environment files (NEVER commit secrets!)
# =============================
# Ignore all .env files
*.py[cod]
*.egg-info/
.pytest_cache/
.ruff_cache/
.mypy_cache/
build/
dist/
# Virtual environments
.venv/
venv/
env/
# Env files (never commit secrets)
.env
.env.local
.env.*.local
**/.env
**/.env.local
# BUT track .env.example templates (safe to commit)
!.env.example
!**/.env.example
# Ignore backup directory
.env-backups/
# Local data
data/
*.db
*.sqlite
*.sqlite3
# =============================
# 🐳 Docker volumes (HUGE)
# =============================
volumes/
*/volumes/
# IDE / OS
.vscode/
.idea/
.DS_Store
# =============================
# 📚 Databases & vector stores
# =============================
postgres_data/
neo4j_data/
*/postgres_data/
*/neo4j_data/
rag/chromadb/
rag/*.sqlite3
rag/chatlogs/
rag/lyra-chatlogs/
# Logs
*.log
# =============================
# 🤖 Model weights (big)
# =============================
models/
*.gguf
*.bin
*.pt
*.safetensors
# =============================
# 📦 Node modules (installed via npm)
# =============================
node_modules/
core/relay/node_modules/
#lyra Stuff
/core/relay/sessions/
-983
View File
@@ -1,983 +0,0 @@
# Project Lyra Changelog
All notable changes to Project Lyra.
Format based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and [Semantic Versioning](https://semver.org/).
---
## [Unreleased]
---
## [0.6.0] - 2025-12-18
### Added - Autonomy System (Phase 1 & 2)
**Autonomy Phase 1** - Self-Awareness & Planning Foundation
- **Executive Planning Module** [cortex/autonomy/executive/planner.py](cortex/autonomy/executive/planner.py)
- Autonomous goal setting and task planning capabilities
- Multi-step reasoning for complex objectives
- Integration with self-state tracking
- **Self-State Management** [cortex/data/self_state.json](cortex/data/self_state.json)
- Persistent state tracking across sessions
- Memory of past actions and outcomes
- Self-awareness metadata storage
- **Self Analyzer** [cortex/autonomy/self/analyzer.py](cortex/autonomy/self/analyzer.py)
- Analyzes own performance and decision patterns
- Identifies areas for improvement
- Tracks cognitive patterns over time
- **Test Suite** [cortex/tests/test_autonomy_phase1.py](cortex/tests/test_autonomy_phase1.py)
- Unit tests for phase 1 autonomy features
**Autonomy Phase 2** - Decision Making & Proactive Behavior
- **Autonomous Actions Module** [cortex/autonomy/actions/autonomous_actions.py](cortex/autonomy/actions/autonomous_actions.py)
- Self-initiated action execution
- Context-aware decision implementation
- Action logging and tracking
- **Pattern Learning System** [cortex/autonomy/learning/pattern_learner.py](cortex/autonomy/learning/pattern_learner.py)
- Learns from interaction patterns
- Identifies recurring user needs
- Adapts behavior based on learned patterns
- **Proactive Monitor** [cortex/autonomy/proactive/monitor.py](cortex/autonomy/proactive/monitor.py)
- Monitors system state for intervention opportunities
- Detects patterns requiring proactive response
- Background monitoring capabilities
- **Decision Engine** [cortex/autonomy/tools/decision_engine.py](cortex/autonomy/tools/decision_engine.py)
- Autonomous decision-making framework
- Weighs options and selects optimal actions
- Integrates with orchestrator for coordinated decisions
- **Orchestrator** [cortex/autonomy/tools/orchestrator.py](cortex/autonomy/tools/orchestrator.py)
- Coordinates multiple autonomy subsystems
- Manages tool selection and execution
- Handles NeoMem integration (with disable capability)
- **Test Suite** [cortex/tests/test_autonomy_phase2.py](cortex/tests/test_autonomy_phase2.py)
- Unit tests for phase 2 autonomy features
**Autonomy Phase 2.5** - Pipeline Refinement
- Tightened integration between autonomy modules and reasoning pipeline
- Enhanced self-state persistence and tracking
- Improved orchestrator reliability
- NeoMem integration refinements in vector store handling [neomem/neomem/vector_stores/qdrant.py](neomem/neomem/vector_stores/qdrant.py)
### Added - Documentation
- **Complete AI Agent Breakdown** [docs/PROJECT_LYRA_COMPLETE_BREAKDOWN.md](docs/PROJECT_LYRA_COMPLETE_BREAKDOWN.md)
- Comprehensive system architecture documentation
- Detailed component descriptions
- Data flow diagrams
- Integration points and API specifications
### Changed - Core Integration
- **Router Updates** [cortex/router.py](cortex/router.py)
- Integrated autonomy subsystems into main routing logic
- Added endpoints for autonomous decision-making
- Enhanced state management across requests
- **Reasoning Pipeline** [cortex/reasoning/reasoning.py](cortex/reasoning/reasoning.py)
- Integrated autonomy-aware reasoning
- Self-state consideration in reasoning process
- **Persona Layer** [cortex/persona/speak.py](cortex/persona/speak.py)
- Autonomy-aware response generation
- Self-state reflection in personality expression
- **Context Handling** [cortex/context.py](cortex/context.py)
- NeoMem disable capability for flexible deployment
### Changed - Development Environment
- Updated [.gitignore](.gitignore) for better workspace management
- Cleaned up VSCode settings
- Removed [.vscode/settings.json](.vscode/settings.json) from repository
### Technical Improvements
- Modular autonomy architecture with clear separation of concerns
- Test-driven development for new autonomy features
- Enhanced state persistence across system restarts
- Flexible NeoMem integration with enable/disable controls
### Architecture - Autonomy System Design
The autonomy system operates in layers:
1. **Executive Layer** - High-level planning and goal setting
2. **Decision Layer** - Evaluates options and makes choices
3. **Action Layer** - Executes autonomous decisions
4. **Learning Layer** - Adapts behavior based on patterns
5. **Monitoring Layer** - Proactive awareness of system state
All layers coordinate through the orchestrator and maintain state in `self_state.json`.
---
## [0.5.2] - 2025-12-12
### Fixed - LLM Router & Async HTTP
- **Critical**: Replaced synchronous `requests` with async `httpx` in LLM router [cortex/llm/llm_router.py](cortex/llm/llm_router.py)
- Event loop blocking was causing timeouts and empty responses
- All three providers (MI50, Ollama, OpenAI) now use `await http_client.post()`
- Fixes "Expecting value: line 1 column 1 (char 0)" JSON parsing errors in intake
- **Critical**: Fixed missing `backend` parameter in intake summarization [cortex/intake/intake.py:285](cortex/intake/intake.py#L285)
- Was defaulting to PRIMARY (MI50) instead of respecting `INTAKE_LLM=SECONDARY`
- Now correctly uses configured backend (Ollama on 3090)
- **Relay**: Fixed session ID case mismatch [core/relay/server.js:87](core/relay/server.js#L87)
- UI sends `sessionId` (camelCase) but relay expected `session_id` (snake_case)
- Now accepts both variants: `req.body.session_id || req.body.sessionId`
- Custom session IDs now properly tracked instead of defaulting to "default"
### Added - Error Handling & Diagnostics
- Added comprehensive error handling in LLM router for all providers
- HTTPError, JSONDecodeError, KeyError, and generic Exception handling
- Detailed error messages with exception type and description
- Provider-specific error logging (mi50, ollama, openai)
- Added debug logging in intake summarization
- Logs LLM response length and preview
- Validates non-empty responses before JSON parsing
- Helps diagnose empty or malformed responses
### Added - Session Management
- Added session persistence endpoints in relay [core/relay/server.js:160-171](core/relay/server.js#L160-L171)
- `GET /sessions/:id` - Retrieve session history
- `POST /sessions/:id` - Save session history
- In-memory storage using Map (ephemeral, resets on container restart)
- Fixes UI "Failed to load session" errors
### Changed - Provider Configuration
- Added `mi50` provider support for llama.cpp server [cortex/llm/llm_router.py:62-81](cortex/llm/llm_router.py#L62-L81)
- Uses `/completion` endpoint with `n_predict` parameter
- Extracts `content` field from response
- Configured for MI50 GPU with DeepSeek model
- Increased memory retrieval threshold from 0.78 to 0.90 [cortex/.env:20](cortex/.env#L20)
- Filters out low-relevance memories (only returns 90%+ similarity)
- Reduces noise in context retrieval
### Technical Improvements
- Unified async HTTP handling across all LLM providers
- Better separation of concerns between provider implementations
- Improved error messages for debugging LLM API failures
- Consistent timeout handling (120 seconds for all providers)
---
## [0.5.1] - 2025-12-11
### Fixed - Intake Integration
- **Critical**: Fixed `bg_summarize()` function not defined error
- Was only a `TYPE_CHECKING` stub, now implemented as logging stub
- Eliminated `NameError` preventing SESSIONS from persisting correctly
- Function now logs exchange additions and defers summarization to `/reason` endpoint
- **Critical**: Fixed `/ingest` endpoint unreachable code in [router.py:201-233](cortex/router.py#L201-L233)
- Removed early return that prevented `update_last_assistant_message()` from executing
- Removed duplicate `add_exchange_internal()` call
- Implemented lenient error handling (each operation wrapped in try/except)
- **Intake**: Added missing `__init__.py` to make intake a proper Python package [cortex/intake/__init__.py](cortex/intake/__init__.py)
- Prevents namespace package issues
- Enables proper module imports
- Exports `SESSIONS`, `add_exchange_internal`, `summarize_context`
### Added - Diagnostics & Debugging
- Added diagnostic logging to verify SESSIONS singleton behavior
- Module initialization logs SESSIONS object ID [intake.py:14](cortex/intake/intake.py#L14)
- Each `add_exchange_internal()` call logs object ID and buffer state [intake.py:343-358](cortex/intake/intake.py#L343-L358)
- Added `/debug/sessions` HTTP endpoint [router.py:276-305](cortex/router.py#L276-L305)
- Inspect SESSIONS from within running Uvicorn worker
- Shows total sessions, session count, buffer sizes, recent exchanges
- Returns SESSIONS object ID for verification
- Added `/debug/summary` HTTP endpoint [router.py:238-271](cortex/router.py#L238-L271)
- Test `summarize_context()` for any session
- Returns L1/L5/L10/L20/L30 summaries
- Includes buffer size and exchange preview
### Changed - Intake Architecture
- **Intake no longer standalone service** - runs inside Cortex container as pure Python module
- Imported as `from intake.intake import add_exchange_internal, SESSIONS`
- No HTTP calls between Cortex and Intake
- Eliminates network latency and dependency on Intake service being up
- **Deferred summarization**: `bg_summarize()` is now a no-op stub [intake.py:318-325](cortex/intake/intake.py#L318-L325)
- Actual summarization happens during `/reason` call via `summarize_context()`
- Simplifies async/sync complexity
- Prevents NameError when called from `add_exchange_internal()`
- **Lenient error handling**: `/ingest` endpoint always returns success [router.py:201-233](cortex/router.py#L201-L233)
- Each operation wrapped in try/except
- Logs errors but never fails to avoid breaking chat pipeline
- User requirement: never fail chat pipeline
### Documentation
- Added single-worker constraint note in [cortex/Dockerfile:7-8](cortex/Dockerfile#L7-L8)
- Documents that SESSIONS requires single Uvicorn worker
- Notes that multi-worker scaling requires Redis or shared storage
- Updated plan documentation with root cause analysis
---
## [0.5.0] - 2025-11-28
### Fixed - Critical API Wiring & Integration
After the major architectural rewire (v0.4.x), this release fixes all critical endpoint mismatches and ensures end-to-end system connectivity.
#### Cortex → Intake Integration
- **Fixed** `IntakeClient` to use correct Intake v0.2 API endpoints
- Changed `GET /context/{session_id}``GET /summaries?session_id={session_id}`
- Updated JSON response parsing to extract `summary_text` field
- Fixed environment variable name: `INTAKE_API``INTAKE_API_URL`
- Corrected default port: `7083``7080`
- Added deprecation warning to `summarize_turn()` method (endpoint removed in Intake v0.2)
#### Relay → UI Compatibility
- **Added** OpenAI-compatible endpoint `POST /v1/chat/completions`
- Accepts standard OpenAI format with `messages[]` array
- Returns OpenAI-compatible response structure with `choices[]`
- Extracts last message content from messages array
- Includes usage metadata (stub values for compatibility)
- **Refactored** Relay to use shared `handleChatRequest()` function
- Both `/chat` and `/v1/chat/completions` use same core logic
- Eliminates code duplication
- Consistent error handling across endpoints
#### Relay → Intake Connection
- **Fixed** Intake URL fallback in Relay server configuration
- Corrected port: `7082``7080`
- Updated endpoint: `/summary``/add_exchange`
- Now properly sends exchanges to Intake for summarization
#### Code Quality & Python Package Structure
- **Added** missing `__init__.py` files to all Cortex subdirectories
- `cortex/llm/__init__.py`
- `cortex/reasoning/__init__.py`
- `cortex/persona/__init__.py`
- `cortex/ingest/__init__.py`
- `cortex/utils/__init__.py`
- Improves package imports and IDE support
- **Removed** unused import in `cortex/router.py`: `from unittest import result`
- **Deleted** empty file `cortex/llm/resolve_llm_url.py` (was 0 bytes, never implemented)
### Verified Working
Complete end-to-end message flow now operational:
```
UI → Relay (/v1/chat/completions)
Relay → Cortex (/reason)
Cortex → Intake (/summaries) [retrieves context]
Cortex 4-stage pipeline:
1. reflection.py → meta-awareness notes
2. reasoning.py → draft answer
3. refine.py → polished answer
4. persona/speak.py → Lyra personality
Cortex → Relay (returns persona response)
Relay → Intake (/add_exchange) [async summary]
Intake → NeoMem (background memory storage)
Relay → UI (final response)
```
### Documentation
- **Added** comprehensive v0.5.0 changelog entry
- **Updated** README.md to reflect v0.5.0 architecture
- Documented new endpoints
- Updated data flow diagrams
- Clarified Intake v0.2 changes
- Corrected service descriptions
### Issues Resolved
- ❌ Cortex could not retrieve context from Intake (wrong endpoint)
- ❌ UI could not send messages to Relay (endpoint mismatch)
- ❌ Relay could not send summaries to Intake (wrong port/endpoint)
- ❌ Python package imports were implicit (missing __init__.py)
### Known Issues (Non-Critical)
- Session management endpoints not implemented in Relay (`GET/POST /sessions/:id`)
- RAG service currently disabled in docker-compose.yml
- Cortex `/ingest` endpoint is a stub returning `{"status": "ok"}`
### Migration Notes
If upgrading from v0.4.x:
1. Pull latest changes from git
2. Verify environment variables in `.env` files:
- Check `INTAKE_API_URL=http://intake:7080` (not `INTAKE_API`)
- Verify all service URLs use correct ports
3. Restart Docker containers: `docker-compose down && docker-compose up -d`
4. Test with a simple message through the UI
---
## [Infrastructure v1.0.0] - 2025-11-26
### Changed - Environment Variable Consolidation
**Major reorganization to eliminate duplication and improve maintainability**
- Consolidated 9 scattered `.env` files into single source of truth architecture
- Root `.env` now contains all shared infrastructure (LLM backends, databases, API keys, service URLs)
- Service-specific `.env` files minimized to only essential overrides:
- `cortex/.env`: Reduced from 42 to 22 lines (operational parameters only)
- `neomem/.env`: Reduced from 26 to 14 lines (LLM naming conventions only)
- `intake/.env`: Kept at 8 lines (already minimal)
- **Result**: ~24% reduction in total configuration lines (197 → ~150)
**Docker Compose Consolidation**
- All services now defined in single root `docker-compose.yml`
- Relay service updated with complete configuration (env_file, volumes)
- Removed redundant `core/docker-compose.yml` (marked as DEPRECATED)
- Standardized network communication to use Docker container names
**Service URL Standardization**
- Internal services use container names: `http://neomem-api:7077`, `http://cortex:7081`
- External services use IP addresses: `http://10.0.0.43:8000` (vLLM), `http://10.0.0.3:11434` (Ollama)
- Removed IP/container name inconsistencies across files
### Added - Security & Documentation
**Security Templates** - Created `.env.example` files for all services
- Root `.env.example` with sanitized credentials
- Service-specific templates: `cortex/.env.example`, `neomem/.env.example`, `intake/.env.example`, `rag/.env.example`
- All `.env.example` files safe to commit to version control
**Documentation**
- `ENVIRONMENT_VARIABLES.md`: Comprehensive reference for all environment variables
- Variable descriptions, defaults, and usage examples
- Multi-backend LLM strategy documentation
- Troubleshooting guide
- Security best practices
- `DEPRECATED_FILES.md`: Deletion guide for deprecated files with verification steps
**Enhanced .gitignore**
- Ignores all `.env` files (including subdirectories)
- Tracks `.env.example` templates for documentation
- Ignores `.env-backups/` directory
### Removed
- `core/.env` - Redundant with root `.env`, now deleted
- `core/docker-compose.yml` - Consolidated into main compose file (marked DEPRECATED)
### Fixed
- Eliminated duplicate `OPENAI_API_KEY` across 5+ files
- Eliminated duplicate LLM backend URLs across 4+ files
- Eliminated duplicate database credentials across 3+ files
- Resolved Cortex `environment:` section override in docker-compose (now uses env_file)
### Architecture - Multi-Backend LLM Strategy
Root `.env` provides all backend OPTIONS (PRIMARY, SECONDARY, CLOUD, FALLBACK), services choose which to USE:
- **Cortex** → vLLM (PRIMARY) for autonomous reasoning
- **NeoMem** → Ollama (SECONDARY) + OpenAI embeddings
- **Intake** → vLLM (PRIMARY) for summarization
- **Relay** → Fallback chain with user preference
Preserves per-service flexibility while eliminating URL duplication.
### Migration
- All original `.env` files backed up to `.env-backups/` with timestamp `20251126_025334`
- Rollback plan documented in `ENVIRONMENT_VARIABLES.md`
- Verification steps provided in `DEPRECATED_FILES.md`
---
## [0.4.x] - 2025-11-13
### Added - Multi-Stage Reasoning Pipeline
**Cortex v0.5 - Complete architectural overhaul**
- **New `reasoning.py` module**
- Async reasoning engine
- Accepts user prompt, identity, RAG block, and reflection notes
- Produces draft internal answers
- Uses primary backend (vLLM)
- **New `reflection.py` module**
- Fully async meta-awareness layer
- Produces actionable JSON "internal notes"
- Enforces strict JSON schema and fallback parsing
- Forces cloud backend (`backend_override="cloud"`)
- **Integrated `refine.py` into pipeline**
- New stage between reflection and persona
- Runs exclusively on primary vLLM backend (MI50)
- Produces final, internally consistent output for downstream persona layer
- **Backend override system**
- Each LLM call can now select its own backend
- Enables multi-LLM cognition: Reflection → cloud, Reasoning → primary
- **Identity loader**
- Added `identity.py` with `load_identity()` for consistent persona retrieval
- **Ingest handler**
- Async stub created for future Intake → NeoMem → RAG pipeline
**Cortex v0.4.1 - RAG Integration**
- **RAG integration**
- Added `rag.py` with `query_rag()` and `format_rag_block()`
- Cortex now queries local RAG API (`http://10.0.0.41:7090/rag/search`)
- Synthesized answers and top excerpts injected into reasoning prompt
### Changed - Unified LLM Architecture
**Cortex v0.5**
- **Unified LLM backend URL handling across Cortex**
- ENV variables must now contain FULL API endpoints
- Removed all internal path-appending (e.g. `.../v1/completions`)
- `llm_router.py` rewritten to use env-provided URLs as-is
- Ensures consistent behavior between draft, reflection, refine, and persona
- **Rebuilt `main.py`**
- Removed old annotation/analysis logic
- New structure: load identity → get RAG → reflect → reason → return draft+notes
- Routes now clean and minimal (`/reason`, `/ingest`, `/health`)
- Async path throughout Cortex
- **Refactored `llm_router.py`**
- Removed old fallback logic during overrides
- OpenAI requests now use `/v1/chat/completions`
- Added proper OpenAI Authorization headers
- Distinct payload format for vLLM vs OpenAI
- Unified, correct parsing across models
- **Simplified Cortex architecture**
- Removed deprecated "context.py" and old reasoning code
- Relay completely decoupled from smart behavior
- **Updated environment specification**
- `LLM_PRIMARY_URL` now set to `http://10.0.0.43:8000/v1/completions`
- `LLM_SECONDARY_URL` remains `http://10.0.0.3:11434/api/generate` (Ollama)
- `LLM_CLOUD_URL` set to `https://api.openai.com/v1/chat/completions`
**Cortex v0.4.1**
- **Revised `/reason` endpoint**
- Now builds unified context blocks: [Intake] → recent summaries, [RAG] → contextual knowledge, [User Message] → current input
- Calls `call_llm()` for first pass, then `reflection_loop()` for meta-evaluation
- Returns `cortex_prompt`, `draft_output`, `final_output`, and normalized reflection
- **Reflection Pipeline Stability**
- Cleaned parsing to normalize JSON vs. text reflections
- Added fallback handling for malformed or non-JSON outputs
- Log system improved to show raw JSON, extracted fields, and normalized summary
- **Async Summarization (Intake v0.2.1)**
- Intake summaries now run in background threads to avoid blocking Cortex
- Summaries (L1L∞) logged asynchronously with [BG] tags
- **Environment & Networking Fixes**
- Verified `.env` variables propagate correctly inside Cortex container
- Confirmed Docker network connectivity between Cortex, Intake, NeoMem, and RAG
- Adjusted localhost calls to service-IP mapping
- **Behavioral Updates**
- Cortex now performs conversation reflection (on user intent) and self-reflection (on its own answers)
- RAG context successfully grounds reasoning outputs
- Intake and NeoMem confirmed receiving summaries via `/add_exchange`
- Log clarity pass: all reflective and contextual blocks clearly labeled
### Fixed
**Cortex v0.5**
- Resolved endpoint conflict where router expected base URLs and refine expected full URLs
- Fixed by standardizing full-URL behavior across entire system
- Reflection layer no longer fails silently (previously returned `[""]` due to MythoMax)
- Resolved 404/401 errors caused by incorrect OpenAI URL endpoints
- No more double-routing through vLLM during reflection
- Corrected async/sync mismatch in multiple locations
- Eliminated double-path bug (`/v1/completions/v1/completions`) caused by previous router logic
### Removed
**Cortex v0.5**
- Legacy `annotate`, `reason_check` glue logic from old architecture
- Old backend probing junk code
- Stale imports and unused modules leftover from previous prototype
### Verified
**Cortex v0.5**
- Cortex → vLLM (MI50) → refine → final_output now functioning correctly
- Refine shows `used_primary_backend: true` and no fallback
- Manual curl test confirms endpoint accuracy
### Known Issues
**Cortex v0.5**
- Refine sometimes prefixes output with `"Final Answer:"`; next version will sanitize this
- Hallucinations in draft_output persist due to weak grounding (fix in reasoning + RAG planned)
**Cortex v0.4.1**
- NeoMem tuning needed - improve retrieval latency and relevance
- Need dedicated `/reflections/recent` endpoint for Cortex
- Migrate to Cortex-first ingestion (Relay → Cortex → NeoMem)
- Add persistent reflection recall (use prior reflections as meta-context)
- Improve reflection JSON structure ("insight", "evaluation", "next_action" → guaranteed fields)
- Tighten temperature and prompt control for factual consistency
- RAG optimization: add source ranking, filtering, multi-vector hybrid search
- Cache RAG responses per session to reduce duplicate calls
### Notes
**Cortex v0.5**
This is the largest structural change to Cortex so far. It establishes:
- Multi-model cognition
- Clean layering
- Identity + reflection separation
- Correct async code
- Deterministic backend routing
- Predictable JSON reflection
The system is now ready for:
- Refinement loops
- Persona-speaking layer
- Containerized RAG
- Long-term memory integration
- True emergent-behavior experiments
---
## [0.3.x] - 2025-10-28 to 2025-09-26
### Added
**[Lyra Core v0.3.2 + Web UI v0.2.0] - 2025-10-28**
- **New UI**
- Cleaned up UI look and feel
- **Sessions**
- Sessions now persist over time
- Ability to create new sessions or load sessions from previous instance
- When changing session, updates what the prompt sends to relay (doesn't prompt with messages from other sessions)
- Relay correctly wired in
**[Lyra-Core 0.3.1] - 2025-10-09**
- **NVGRAM Integration (Full Pipeline Reconnected)**
- Replaced legacy Mem0 service with NVGRAM microservice (`nvgram-api` @ port 7077)
- Updated `server.js` in Relay to route all memory ops via `${NVGRAM_API}/memories` and `/search`
- Added `.env` variable: `NVGRAM_API=http://nvgram-api:7077`
- Verified end-to-end Lyra conversation persistence: `relay → nvgram-api → postgres/neo4j → relay → ollama → ui`
- ✅ Memories stored, retrieved, and re-injected successfully
**[Lyra-Core v0.3.0] - 2025-09-26**
- **Salience filtering** in Relay
- `.env` configurable: `SALIENCE_ENABLED`, `SALIENCE_MODE`, `SALIENCE_MODEL`, `SALIENCE_API_URL`
- Supports `heuristic` and `llm` classification modes
- LLM-based salience filter integrated with Cortex VM running `llama-server`
- Logging improvements
- Added debug logs for salience mode, raw LLM output, and unexpected outputs
- Fail-closed behavior for unexpected LLM responses
- Successfully tested with **Phi-3.5-mini** and **Qwen2-0.5B-Instruct** as salience classifiers
- Verified end-to-end flow: Relay → salience filter → Mem0 add/search → Persona injection → LLM reply
**[Cortex v0.3.0] - 2025-10-31**
- **Cortex Service (FastAPI)**
- New standalone reasoning engine (`cortex/main.py`) with endpoints:
- `GET /health` reports active backend + NeoMem status
- `POST /reason` evaluates `{prompt, response}` pairs
- `POST /annotate` experimental text analysis
- Background NeoMem health monitor (5-minute interval)
- **Multi-Backend Reasoning Support**
- Environment-driven backend selection via `LLM_FORCE_BACKEND`
- Supports: Primary (vLLM MI50), Secondary (Ollama 3090), Cloud (OpenAI), Fallback (llama.cpp CPU)
- Per-backend model variables: `LLM_PRIMARY_MODEL`, `LLM_SECONDARY_MODEL`, `LLM_CLOUD_MODEL`, `LLM_FALLBACK_MODEL`
- **Response Normalization Layer**
- Implemented `normalize_llm_response()` to merge streamed outputs and repair malformed JSON
- Handles Ollama's multi-line streaming and Mythomax's missing punctuation issues
- Prints concise debug previews of merged content
- **Environment Simplification**
- Each service (`intake`, `cortex`, `neomem`) now maintains its own `.env` file
- Removed reliance on shared/global env file to prevent cross-contamination
- Verified Docker Compose networking across containers
**[NeoMem 0.1.2] - 2025-10-27** (formerly NVGRAM)
- **Renamed NVGRAM to NeoMem**
- All future updates under name NeoMem
- Features unchanged
**[NVGRAM 0.1.1] - 2025-10-08**
- **Async Memory Rewrite (Stability + Safety Patch)**
- Introduced `AsyncMemory` class with fully asynchronous vector and graph store writes
- Added input sanitation to prevent embedding errors (`'list' object has no attribute 'replace'`)
- Implemented `flatten_messages()` helper in API layer to clean malformed payloads
- Added structured request logging via `RequestLoggingMiddleware` (FastAPI middleware)
- Health endpoint (`/health`) returns structured JSON `{status, version, service}`
- Startup logs include sanitized embedder config with masked API keys
**[NVGRAM 0.1.0] - 2025-10-07**
- **Initial fork of Mem0 → NVGRAM**
- Created fully independent local-first memory engine based on Mem0 OSS
- Renamed all internal modules, Docker services, environment variables from `mem0``nvgram`
- New service name: `nvgram-api`, default port 7077
- Maintains same API endpoints (`/memories`, `/search`) for drop-in compatibility
- Uses FastAPI, Postgres, and Neo4j as persistent backends
**[Lyra-Mem0 0.3.2] - 2025-10-05**
- **Ollama LLM reasoning** alongside OpenAI embeddings
- Introduced `LLM_PROVIDER=ollama`, `LLM_MODEL`, and `OLLAMA_HOST` in `.env.3090`
- Verified local 3090 setup using `qwen2.5:7b-instruct-q4_K_M`
- Split processing: Embeddings → OpenAI `text-embedding-3-small`, LLM → Local Ollama
- Added `.env.3090` template for self-hosted inference nodes
- Integrated runtime diagnostics and seeder progress tracking
- File-level + message-level progress bars
- Retry/back-off logic for timeouts (3 attempts)
- Event logging (`ADD / UPDATE / NONE`) for every memory record
- Expanded Docker health checks for Postgres, Qdrant, and Neo4j containers
- Added GPU-friendly long-run configuration for continuous seeding (validated on RTX 3090)
**[Lyra-Mem0 0.3.1] - 2025-10-03**
- HuggingFace TEI integration (local 3090 embedder)
- Dual-mode environment switch between OpenAI cloud and local
- CSV export of memories from Postgres (`payload->>'data'`)
**[Lyra-Mem0 0.3.0]**
- **Ollama embeddings** in Mem0 OSS container
- Configure `EMBEDDER_PROVIDER=ollama`, `EMBEDDER_MODEL`, `OLLAMA_HOST` via `.env`
- Mounted `main.py` override from host into container to load custom `DEFAULT_CONFIG`
- Installed `ollama` Python client into custom API container image
- `.env.3090` file for external embedding mode (3090 machine)
- Workflow for multiple embedding modes: LAN-based 3090/Ollama, Local-only CPU, OpenAI fallback
**[Lyra-Mem0 v0.2.1]**
- **Seeding pipeline**
- Built Python seeder script to bulk-insert raw Cloud Lyra exports into Mem0
- Implemented incremental seeding option (skip existing memories, only add new ones)
- Verified insert process with Postgres-backed history DB
**[Intake v0.1.0] - 2025-10-27**
- Receives messages from relay and summarizes them in cascading format
- Continues to summarize smaller amounts of exchanges while generating large-scale conversational summaries (L20)
- Currently logs summaries to .log file in `/project-lyra/intake-logs/`
**[Lyra-Cortex v0.2.0] - 2025-09-26**
- Integrated **llama-server** on dedicated Cortex VM (Proxmox)
- Verified Phi-3.5-mini-instruct_Uncensored-Q4_K_M running with 8 vCPUs
- Benchmarked Phi-3.5-mini performance: ~18 tokens/sec CPU-only on Ryzen 7 7800X
- Salience classification functional but sometimes inconsistent
- Tested **Qwen2-0.5B-Instruct GGUF** as alternative salience classifier
- Much faster throughput (~350 tokens/sec prompt, ~100 tokens/sec eval)
- More responsive but over-classifies messages as "salient"
- Established `.env` integration for model ID (`SALIENCE_MODEL`), enabling hot-swap between models
### Changed
**[Lyra-Core 0.3.1] - 2025-10-09**
- Renamed `MEM0_URL``NVGRAM_API` across all relay environment configs
- Updated Docker Compose service dependency order
- `relay` now depends on `nvgram-api` healthcheck
- Removed `mem0` references and volumes
- Minor cleanup to Persona fetch block (null-checks and safer default persona string)
**[Lyra-Core v0.3.1] - 2025-09-27**
- Removed salience filter logic; Cortex is now default annotator
- All user messages stored in Mem0; no discard tier applied
- Cortex annotations (`metadata.cortex`) now attached to memories
- Debug logging improvements
- Pretty-print Cortex annotations
- Injected prompt preview
- Memory search hit list with scores
- `.env` toggle (`CORTEX_ENABLED`) to bypass Cortex when needed
**[Lyra-Core v0.3.0] - 2025-09-26**
- Refactored `server.js` to gate `mem.add()` calls behind salience filter
- Updated `.env` to support `SALIENCE_MODEL`
**[Cortex v0.3.0] - 2025-10-31**
- Refactored `reason_check()` to dynamically switch between **prompt** and **chat** mode depending on backend
- Enhanced startup logs to announce active backend, model, URL, and mode
- Improved error handling with clearer "Reasoning error" messages
**[NVGRAM 0.1.1] - 2025-10-08**
- Replaced synchronous `Memory.add()` with async-safe version supporting concurrent vector + graph writes
- Normalized indentation and cleaned duplicate `main.py` references
- Removed redundant `FastAPI()` app reinitialization
- Updated internal logging to INFO-level timing format
- Deprecated `@app.on_event("startup")` → will migrate to `lifespan` handler in v0.1.2
**[NVGRAM 0.1.0] - 2025-10-07**
- Removed dependency on external `mem0ai` SDK — all logic now local
- Re-pinned requirements: fastapi==0.115.8, uvicorn==0.34.0, pydantic==2.10.4, python-dotenv==1.0.1, psycopg>=3.2.8, ollama
- Adjusted `docker-compose` and `.env` templates to use new NVGRAM naming
**[Lyra-Mem0 0.3.2] - 2025-10-05**
- Updated `main.py` configuration block to load `LLM_PROVIDER`, `LLM_MODEL`, `OLLAMA_BASE_URL`
- Fallback to OpenAI if Ollama unavailable
- Adjusted `docker-compose.yml` mount paths to correctly map `/app/main.py`
- Normalized `.env` loading so `mem0-api` and host environment share identical values
- Improved seeder logging and progress telemetry
- Added explicit `temperature` field to `DEFAULT_CONFIG['llm']['config']`
**[Lyra-Mem0 0.3.0]**
- `docker-compose.yml` updated to mount local `main.py` and `.env.3090`
- Built custom Dockerfile (`mem0-api-server:latest`) extending base image with `pip install ollama`
- Updated `requirements.txt` to include `ollama` package
- Adjusted Mem0 container config so `main.py` pulls environment variables with `dotenv`
- Tested new embeddings path with curl `/memories` API call
**[Lyra-Mem0 v0.2.1]**
- Updated `main.py` to load configuration from `.env` using `dotenv` and support multiple embedder backends
- Mounted host `main.py` into container so local edits persist across rebuilds
- Updated `docker-compose.yml` to mount `.env.3090` and support swap between profiles
- Built custom Dockerfile (`mem0-api-server:latest`) including `pip install ollama`
- Updated `requirements.txt` with `ollama` dependency
- Adjusted startup flow so container automatically connects to external Ollama host (LAN IP)
- Added logging to confirm model pulls and embedding requests
### Fixed
**[Lyra-Core 0.3.1] - 2025-10-09**
- Relay startup no longer crashes when NVGRAM is unavailable — deferred connection handling
- `/memories` POST failures no longer crash Relay; now logged gracefully as `relay error Error: memAdd failed: 500`
- Improved injected prompt debugging (`DEBUG_PROMPT=true` now prints clean JSON)
**[Lyra-Core v0.3.1] - 2025-09-27**
- Parsing failures from Markdown-wrapped Cortex JSON via fence cleaner
- Relay no longer "hangs" on malformed Cortex outputs
**[Cortex v0.3.0] - 2025-10-31**
- Corrected broken vLLM endpoint routing (`/v1/completions`)
- Stabilized cross-container health reporting for NeoMem
- Resolved JSON parse failures caused by streaming chunk delimiters
**[NVGRAM 0.1.1] - 2025-10-08**
- Eliminated repeating 500 error from OpenAI embedder caused by non-string message content
- Masked API key leaks from boot logs
- Ensured Neo4j reconnects gracefully on first retry
**[Lyra-Mem0 0.3.2] - 2025-10-05**
- Resolved crash during startup: `TypeError: OpenAIConfig.__init__() got an unexpected keyword argument 'ollama_base_url'`
- Corrected mount type mismatch (file vs directory) causing `OCI runtime create failed` errors
- Prevented duplicate or partial postings when retry logic triggered multiple concurrent requests
- "Unknown event" warnings now safely ignored (no longer break seeding loop)
- Confirmed full dual-provider operation in logs (`api.openai.com` + `10.0.0.3:11434/api/chat`)
**[Lyra-Mem0 0.3.1] - 2025-10-03**
- `.env` CRLF vs LF line ending issues
- Local seeding now possible via HuggingFace server
**[Lyra-Mem0 0.3.0]**
- Resolved container boot failure caused by missing `ollama` dependency (`ModuleNotFoundError`)
- Fixed config overwrite issue where rebuilding container restored stock `main.py`
- Worked around Neo4j error (`vector.similarity.cosine(): mismatched vector dimensions`) by confirming OpenAI vs. Ollama embedding vector sizes
**[Lyra-Mem0 v0.2.1]**
- Seeder process originally failed on old memories — now skips duplicates and continues batch
- Resolved container boot error (`ModuleNotFoundError: ollama`) by extending image
- Fixed overwrite issue where stock `main.py` replaced custom config during rebuild
- Worked around Neo4j `vector.similarity.cosine()` dimension mismatch
### Known Issues
**[Lyra-Core v0.3.0] - 2025-09-26**
- Small models (e.g. Qwen2-0.5B) tend to over-classify as "salient"
- Phi-3.5-mini sometimes returns truncated tokens ("sali", "fi")
- CPU-only inference is functional but limited; larger models recommended once GPU available
**[Lyra-Cortex v0.2.0] - 2025-09-26**
- Small models tend to drift or over-classify
- CPU-only 7B+ models expected to be slow; GPU passthrough recommended for larger models
- Need to set up `systemd` service for `llama-server` to auto-start on VM reboot
### Observations
**[Lyra-Mem0 0.3.2] - 2025-10-05**
- Stable GPU utilization: ~8 GB VRAM @ 92% load, ≈ 67°C under sustained seeding
- Next revision will re-format seed JSON to preserve `role` context (user vs assistant)
**[Lyra-Mem0 v0.2.1]**
- To fully unify embedding modes, a Hugging Face / local model with **1536-dim embeddings** will be needed (to match OpenAI's schema)
- Current Ollama model (`mxbai-embed-large`) works, but returns 1024-dim vectors
- Seeder workflow validated but should be wrapped in repeatable weekly run for full Cloud→Local sync
### Next Steps
**[Lyra-Core 0.3.1] - 2025-10-09**
- Add salience visualization (e.g., memory weights displayed in injected system message)
- Begin schema alignment with NVGRAM v0.1.2 for confidence scoring
- Add relay auto-retry for transient 500 responses from NVGRAM
**[NVGRAM 0.1.1] - 2025-10-08**
- Integrate salience scoring and embedding confidence weight fields in Postgres schema
- Begin testing with full Lyra Relay + Persona Sidecar pipeline for live session memory recall
- Migrate from deprecated `on_event``lifespan` pattern in 0.1.2
**[NVGRAM 0.1.0] - 2025-10-07**
- Integrate NVGRAM as new default backend in Lyra Relay
- Deprecate remaining Mem0 references and archive old configs
- Begin versioning as standalone project (`nvgram-core`, `nvgram-api`, etc.)
**[Intake v0.1.0] - 2025-10-27**
- Feed intake into NeoMem
- Generate daily/hourly overall summary (IE: Today Brian and Lyra worked on x, y, and z)
- Generate session-aware summaries with own intake hopper
---
## [0.2.x] - 2025-09-30 to 2025-09-24
### Added
**[Lyra-Mem0 v0.2.0] - 2025-09-30**
- Standalone **Lyra-Mem0** stack created at `~/lyra-mem0/`
- Includes Postgres (pgvector), Qdrant, Neo4j, and SQLite for history tracking
- Added working `docker-compose.mem0.yml` and custom `Dockerfile` for building Mem0 API server
- Verified REST API functionality
- `POST /memories` works for adding memories
- `POST /search` works for semantic search
- Successful end-to-end test with persisted memory: *"Likes coffee in the morning"* → retrievable via search ✅
**[Lyra-Core v0.2.0] - 2025-09-24**
- Migrated Relay to use `mem0ai` SDK instead of raw fetch calls
- Implemented `sessionId` support (client-supplied, fallback to `default`)
- Added debug logs for memory add/search
- Cleaned up Relay structure for clarity
### Changed
**[Lyra-Mem0 v0.2.0] - 2025-09-30**
- Split architecture into modular stacks:
- `~/lyra-core` (Relay, Persona-Sidecar, etc.)
- `~/lyra-mem0` (Mem0 OSS memory stack)
- Removed old embedded mem0 containers from Lyra-Core compose file
- Added Lyra-Mem0 section in README.md
### Next Steps
**[Lyra-Mem0 v0.2.0] - 2025-09-30**
- Wire **Relay → Mem0 API** (integration not yet complete)
- Add integration tests to verify persistence and retrieval from within Lyra-Core
---
## [0.1.x] - 2025-09-25 to 2025-09-23
### Added
**[Lyra_RAG v0.1.0] - 2025-11-07**
- Initial standalone RAG module for Project Lyra
- Persistent ChromaDB vector store (`./chromadb`)
- Importer `rag_chat_import.py` with:
- Recursive folder scanning and category tagging
- Smart chunking (~5k chars)
- SHA-1 deduplication and chat-ID metadata
- Timestamp fields (`file_modified`, `imported_at`)
- Background-safe operation (`nohup`/`tmux`)
- 68 Lyra-category chats imported:
- 6,556 new chunks added
- 1,493 duplicates skipped
- 7,997 total vectors stored
**[Lyra_RAG v0.1.0 API] - 2025-11-07**
- `/rag/search` FastAPI endpoint implemented (port 7090)
- Supports natural-language queries and returns top related excerpts
- Added answer synthesis step using `gpt-4o-mini`
**[Lyra-Core v0.1.0] - 2025-09-23**
- First working MVP of **Lyra Core Relay**
- Relay service accepts `POST /v1/chat/completions` (OpenAI-compatible)
- Memory integration with Mem0:
- `POST /memories` on each user message
- `POST /search` before LLM call
- Persona Sidecar integration (`GET /current`)
- OpenAI GPT + Ollama (Mythomax) support in Relay
- Simple browser-based chat UI (talks to Relay at `http://<host>:7078`)
- `.env` standardization for Relay + Mem0 + Postgres + Neo4j
- Working Neo4j + Postgres backing stores for Mem0
- Initial MVP relay service with raw fetch calls to Mem0
- Dockerized with basic healthcheck
**[Lyra-Cortex v0.1.0] - 2025-09-25**
- First deployment as dedicated Proxmox VM (5 vCPU / 18 GB RAM / 100 GB SSD)
- Built **llama.cpp** with `llama-server` target via CMake
- Integrated **Phi-3.5 Mini Instruct (Uncensored, Q4_K_M GGUF)** model
- Verified API compatibility at `/v1/chat/completions`
- Local test successful via `curl` → ~523 token response generated
- Performance benchmark: ~11.5 tokens/sec (CPU-only on Ryzen 7800X)
- Confirmed usable for salience scoring, summarization, and lightweight reasoning
### Fixed
**[Lyra-Core v0.1.0] - 2025-09-23**
- Resolved crash loop in Neo4j by restricting env vars (`NEO4J_AUTH` only)
- Relay now correctly reads `MEM0_URL` and `MEM0_API_KEY` from `.env`
### Verified
**[Lyra_RAG v0.1.0] - 2025-11-07**
- Successful recall of Lyra-Core development history (v0.3.0 snapshot)
- Correct metadata and category tagging for all new imports
### Known Issues
**[Lyra-Core v0.1.0] - 2025-09-23**
- No feedback loop (thumbs up/down) yet
- Forget/delete flow is manual (via memory IDs)
- Memory latency ~14s depending on embedding model
### Next Planned
**[Lyra_RAG v0.1.0] - 2025-11-07**
- Optional `where` filter parameter for category/date queries
- Graceful "no results" handler for empty retrievals
- `rag_docs_import.py` for PDFs and other document types
---
-91
View File
@@ -1,91 +0,0 @@
# Deprecated Files - Safe to Delete
This file lists all deprecated files that can be safely deleted after verification.
## Files Marked for Deletion
### Docker Compose Files
#### `/core/docker-compose.yml.DEPRECATED`
- **Status**: DEPRECATED
- **Reason**: All services consolidated into main `/docker-compose.yml`
- **Replaced by**: `/docker-compose.yml` (relay service now has complete config)
- **Safe to delete**: Yes, after verifying main docker-compose works
### Environment Files
All original `.env` files have been consolidated. Backups exist in `.env-backups/` directory.
#### Previously Deleted (Already Done)
-`/core/.env` - Deleted (redundant with root .env)
### Experimental/Orphaned Files
#### `/core/env experiments/` (entire directory)
- **Status**: User will handle separately
- **Contains**: `.env`, `.env.local`, `.env.openai`
- **Action**: User to review and clean up
## Verification Steps Before Deleting
Before deleting the deprecated files, verify:
1. **Test main docker-compose.yml works:**
```bash
cd /home/serversdown/project-lyra
docker-compose down
docker-compose up -d
docker-compose ps # All services should be running
```
2. **Verify relay service has correct config:**
```bash
docker exec relay env | grep -E "LLM_|NEOMEM_|OPENAI"
docker exec relay ls -la /app/sessions # Sessions volume mounted
```
3. **Test relay functionality:**
- Send a test message through relay
- Verify memory storage works
- Confirm LLM backend connections work
## Deletion Commands
After successful verification, run:
```bash
cd /home/serversdown/project-lyra
# Delete deprecated docker-compose file
rm core/docker-compose.yml.DEPRECATED
# Optionally clean up backup directory after confirming everything works
# (Keep backups for at least a few days/weeks)
# rm -rf .env-backups/
```
## Files to Keep
These files should **NOT** be deleted:
- ✅ `.env` (root) - Single source of truth
- ✅ `.env.example` (root) - Security template (commit to git)
- ✅ `cortex/.env` - Service-specific config
- ✅ `cortex/.env.example` - Security template (commit to git)
- ✅ `neomem/.env` - Service-specific config
- ✅ `neomem/.env.example` - Security template (commit to git)
- ✅ `intake/.env` - Service-specific config
- ✅ `intake/.env.example` - Security template (commit to git)
- ✅ `rag/.env.example` - Security template (commit to git)
- ✅ `docker-compose.yml` - Main orchestration file
- ✅ `ENVIRONMENT_VARIABLES.md` - Documentation
- ✅ `.gitignore` - Git configuration
## Backup Information
All original `.env` files backed up to:
- Location: `/home/serversdown/project-lyra/.env-backups/`
- Timestamp: `20251126_025334`
- Files: 6 original .env files
Keep backups until you're confident the new setup is stable (recommended: 2-4 weeks).
-178
View File
@@ -1,178 +0,0 @@
# Logging System Migration Complete
## ✅ What Changed
The old `VERBOSE_DEBUG` logging system has been completely replaced with the new structured `LOG_DETAIL_LEVEL` system.
### Files Modified
1. **[.env](.env)** - Removed `VERBOSE_DEBUG`, cleaned up duplicate `LOG_DETAIL_LEVEL` settings
2. **[cortex/.env](cortex/.env)** - Removed `VERBOSE_DEBUG` from cortex config
3. **[cortex/router.py](cortex/router.py)** - Replaced `VERBOSE_DEBUG` checks with `LOG_DETAIL_LEVEL`
4. **[cortex/context.py](cortex/context.py)** - Replaced `VERBOSE_DEBUG` with `LOG_DETAIL_LEVEL`, removed verbose file logging setup
## 🎯 New Logging Configuration
### Single Environment Variable
Set `LOG_DETAIL_LEVEL` in your `.env` file:
```bash
LOG_DETAIL_LEVEL=detailed
```
### Logging Levels
| Level | Lines/Message | What You See |
|-------|---------------|--------------|
| **minimal** | 1-2 | Only errors and critical events |
| **summary** | 5-7 | Pipeline completion, errors, warnings (production mode) |
| **detailed** | 30-50 | LLM outputs, timing breakdowns, context (debugging mode) |
| **verbose** | 100+ | Everything including raw JSON dumps (deep debugging) |
## 📊 What You Get at Each Level
### Summary Mode (Production)
```
📊 Context | Session: abc123 | Messages: 42 | Last: 5.2min | RAG: 3 results
🧠 Monologue | question | Tone: curious
====================================================================================================
✨ PIPELINE COMPLETE | Session: abc123 | Total: 1250ms
====================================================================================================
📤 Output: 342 characters
====================================================================================================
```
### Detailed Mode (Debugging - RECOMMENDED)
```
====================================================================================================
🚀 PIPELINE START | Session: abc123 | 14:23:45.123
====================================================================================================
📝 User: What is the meaning of life?
────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────
🧠 LLM CALL | Backend: PRIMARY | 14:23:45.234
────────────────────────────────────────────────────────────────────────────────────────────────────
📝 Prompt: You are Lyra, analyzing the user's question...
💬 Reply: Based on the context provided, here's my analysis...
────────────────────────────────────────────────────────────────────────────────────────────────────
📊 Context | Session: abc123 | Messages: 42 | Last: 5.2min | RAG: 3 results
────────────────────────────────────────────────────────────────────────────────────────────────────
[CONTEXT] Session abc123 | User: What is the meaning of life?
────────────────────────────────────────────────────────────────────────────────────────────────────
Mode: default | Mood: neutral | Project: None
Tools: RAG, WEB, WEATHER, CODEBRAIN, POKERBRAIN
╭─ INTAKE SUMMARIES ────────────────────────────────────────────────
│ L1 : Last message discussed philosophy...
│ L5 : Recent 5 messages covered existential topics...
│ L10 : Past 10 messages showed curiosity pattern...
╰───────────────────────────────────────────────────────────────────
╭─ RAG RESULTS (3) ──────────────────────────────────────────────
│ [1] 0.923 | Previous discussion about purpose...
│ [2] 0.891 | Note about existential philosophy...
│ [3] 0.867 | Memory of Viktor Frankl discussion...
╰───────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────
🧠 Monologue | question | Tone: curious
====================================================================================================
✨ PIPELINE COMPLETE | Session: abc123 | Total: 1250ms
====================================================================================================
⏱️ Stage Timings:
context : 150ms ( 12.0%)
identity : 10ms ( 0.8%)
monologue : 200ms ( 16.0%)
tools : 0ms ( 0.0%)
reflection : 50ms ( 4.0%)
reasoning : 450ms ( 36.0%) ← BOTTLENECK!
refinement : 300ms ( 24.0%)
persona : 140ms ( 11.2%)
learning : 50ms ( 4.0%)
📤 Output: 342 characters
====================================================================================================
```
### Verbose Mode (Maximum Debug)
Same as detailed, plus:
- Full raw JSON responses from LLMs (50-line boxes)
- Complete intake data structures
- Stack traces on errors
## 🚀 How to Use
### For Finding Weak Links (Your Use Case)
```bash
# In .env:
LOG_DETAIL_LEVEL=detailed
# Restart services:
docker-compose restart cortex relay
```
You'll now see:
- ✅ Which LLM backend is used
- ✅ What prompts are sent to each LLM
- ✅ What each LLM responds with
- ✅ Timing breakdown showing which stage is slow
- ✅ Context being used (RAG, intake summaries)
- ✅ Clean, hierarchical structure
### For Production
```bash
LOG_DETAIL_LEVEL=summary
```
### For Deep Debugging
```bash
LOG_DETAIL_LEVEL=verbose
```
## 🔍 Finding Performance Bottlenecks
With `detailed` mode, look for:
1. **Slow stages in timing breakdown:**
```
reasoning : 3450ms ( 76.0%) ← THIS IS YOUR BOTTLENECK!
```
2. **Backend failures:**
```
⚠️ [LLM] PRIMARY failed | 14:23:45.234 | Connection timeout
✅ [LLM] SECONDARY | Reply: Based on... ← Fell back to secondary
```
3. **Loop detection:**
```
⚠️ DUPLICATE MESSAGE DETECTED | Session: abc123
🔁 LOOP DETECTED - Returning cached context
```
## 📁 Removed Features
The following old logging features have been removed:
- ❌ `VERBOSE_DEBUG` environment variable (replaced with `LOG_DETAIL_LEVEL`)
- ❌ File logging to `/app/logs/cortex_verbose_debug.log` (use `docker logs` instead)
- ❌ Separate verbose handlers in Python logging
- ❌ Per-module verbose flags
## ✨ New Features
- ✅ Single unified logging configuration
- ✅ Hierarchical, scannable output
- ✅ Collapsible data sections (boxes)
- ✅ Stage timing always shown in detailed mode
- ✅ Performance profiling built-in
- ✅ Loop detection and warnings
- ✅ Clean error formatting
---
**The logging is now clean, concise, and gives you exactly what you need to find weak links!** 🎯
-176
View File
@@ -1,176 +0,0 @@
# Cortex Logging Quick Reference
## 🎯 TL;DR
**Finding weak links in the LLM chain?**
```bash
export LOG_DETAIL_LEVEL=detailed
export VERBOSE_DEBUG=true
```
**Production use?**
```bash
export LOG_DETAIL_LEVEL=summary
```
---
## 📊 Log Levels Comparison
| Level | Output Lines/Message | Use Case | Raw LLM Output? |
|-------|---------------------|----------|-----------------|
| **minimal** | 1-2 | Silent production | ❌ No |
| **summary** | 5-7 | Production (DEFAULT) | ❌ No |
| **detailed** | 30-50 | Debugging, finding bottlenecks | ✅ Parsed only |
| **verbose** | 100+ | Deep debugging, seeing raw data | ✅ Full JSON |
---
## 🔍 Common Debugging Tasks
### See Raw LLM Outputs
```bash
export LOG_DETAIL_LEVEL=verbose
```
Look for:
```
╭─ RAW RESPONSE ────────────────────────────────────
│ { "choices": [ { "message": { "content": "..." } } ] }
╰───────────────────────────────────────────────────
```
### Find Performance Bottlenecks
```bash
export LOG_DETAIL_LEVEL=detailed
```
Look for:
```
⏱️ Stage Timings:
reasoning : 3450ms ( 76.0%) ← SLOW!
```
### Check Which RAG Memories Are Used
```bash
export LOG_DETAIL_LEVEL=detailed
```
Look for:
```
╭─ RAG RESULTS (5) ──────────────────────────────
│ [1] 0.923 | Memory content...
```
### Detect Loops
```bash
export ENABLE_DUPLICATE_DETECTION=true # (default)
```
Look for:
```
⚠️ DUPLICATE MESSAGE DETECTED
🔁 LOOP DETECTED - Returning cached context
```
### See All Backend Failures
```bash
export LOG_DETAIL_LEVEL=summary # or higher
```
Look for:
```
⚠️ [LLM] PRIMARY failed | Connection timeout
⚠️ [LLM] SECONDARY failed | Model not found
✅ [LLM] CLOUD | Reply: Based on...
```
---
## 🛠️ Environment Variables Cheat Sheet
```bash
# Verbosity Control
LOG_DETAIL_LEVEL=detailed # minimal | summary | detailed | verbose
VERBOSE_DEBUG=false # true = maximum verbosity (legacy)
# Raw Data Visibility
LOG_RAW_CONTEXT_DATA=false # Show full intake L1-L30 dumps
# Loop Protection
ENABLE_DUPLICATE_DETECTION=true # Detect duplicate messages
MAX_MESSAGE_HISTORY=100 # Trim history after N messages
SESSION_TTL_HOURS=24 # Expire sessions after N hours
# Features
NEOMEM_ENABLED=false # Enable long-term memory
ENABLE_AUTONOMOUS_TOOLS=true # Enable tool invocation
ENABLE_PROACTIVE_MONITORING=true # Enable suggestions
```
---
## 📋 Sample Output
### Summary Mode (Default - Production)
```
✅ [LLM] PRIMARY | 14:23:45.123 | Reply: Based on your question...
📊 Context | Session: abc123 | Messages: 42 | Last: 5.2min | RAG: 5 results
🧠 Monologue | question | Tone: curious
✨ PIPELINE COMPLETE | Session: abc123 | Total: 1250ms
📤 Output: 342 characters
```
### Detailed Mode (Debugging)
```
════════════════════════════════════════════════════════════════════════════
🚀 PIPELINE START | Session: abc123 | 14:23:45.123
════════════════════════════════════════════════════════════════════════════
📝 User: What is the meaning of life?
────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────
🧠 LLM CALL | Backend: PRIMARY | 14:23:45.234
────────────────────────────────────────────────────────────────────────────
📝 Prompt: You are Lyra, a thoughtful AI assistant...
💬 Reply: Based on philosophical perspectives...
📊 Context | Session: abc123 | Messages: 42 | Last: 5.2min | RAG: 5 results
╭─ RAG RESULTS (5) ──────────────────────────────
│ [1] 0.923 | Previous philosophy discussion...
│ [2] 0.891 | Existential note...
╰────────────────────────────────────────────────
════════════════════════════════════════════════════════════════════════════
✨ PIPELINE COMPLETE | Session: abc123 | Total: 1250ms
════════════════════════════════════════════════════════════════════════════
⏱️ Stage Timings:
context : 150ms ( 12.0%)
reasoning : 450ms ( 36.0%) ← Largest component
persona : 140ms ( 11.2%)
📤 Output: 342 characters
════════════════════════════════════════════════════════════════════════════
```
---
## ⚡ Quick Troubleshooting
| Symptom | Check | Fix |
|---------|-------|-----|
| **Logs too verbose** | Current level | Set `LOG_DETAIL_LEVEL=summary` |
| **Can't see LLM outputs** | Current level | Set `LOG_DETAIL_LEVEL=detailed` or `verbose` |
| **Repeating operations** | Loop warnings | Check for `🔁 LOOP DETECTED` messages |
| **Slow responses** | Stage timings | Look for stages >1000ms in detailed mode |
| **Missing RAG data** | NEOMEM_ENABLED | Set `NEOMEM_ENABLED=true` |
| **Out of memory** | Message history | Lower `MAX_MESSAGE_HISTORY` |
---
## 📁 Key Files
- **[.env.logging.example](.env.logging.example)** - Full configuration guide
- **[LOGGING_REFACTOR_SUMMARY.md](LOGGING_REFACTOR_SUMMARY.md)** - Detailed explanation
- **[cortex/utils/logging_utils.py](cortex/utils/logging_utils.py)** - Logging utilities
- **[cortex/context.py](cortex/context.py)** - Context + loop protection
- **[cortex/router.py](cortex/router.py)** - Pipeline stages
- **[core/relay/lib/llm.js](core/relay/lib/llm.js)** - LLM backend logging
---
**Need more detail? See [LOGGING_REFACTOR_SUMMARY.md](LOGGING_REFACTOR_SUMMARY.md)**
-352
View File
@@ -1,352 +0,0 @@
# Cortex Logging Refactor Summary
## 🎯 Problem Statement
The cortex chat loop had severe logging issues that made debugging impossible:
1. **Massive verbosity**: 100+ log lines per chat message
2. **Raw LLM dumps**: Full JSON responses pretty-printed on every call (1000s of lines)
3. **Repeated data**: NeoMem results logged 71 times individually
4. **No structure**: Scattered emoji logs with no hierarchy
5. **Impossible to debug**: Couldn't tell if loops were happening or just verbose logging
6. **No loop protection**: Unbounded message history growth, no session cleanup, no duplicate detection
## ✅ What Was Fixed
### 1. **Structured Hierarchical Logging**
**Before:**
```
🔍 RAW LLM RESPONSE: {
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Here is a very long response that goes on for hundreds of lines..."
}
}
],
"usage": {
"prompt_tokens": 123,
"completion_tokens": 456,
"total_tokens": 579
}
}
🧠 Trying backend: PRIMARY (http://localhost:8000)
✅ Success via PRIMARY
[STAGE 0] Collecting unified context...
[STAGE 0] Context collected - 5 RAG results
[COLLECT_CONTEXT] Intake data retrieved:
{
"L1": [...],
"L5": [...],
"L10": {...},
"L20": {...},
"L30": {...}
}
[COLLECT_CONTEXT] NeoMem search returned 71 results
[1] Score: 0.923 - Memory content here...
[2] Score: 0.891 - More memory content...
[3] Score: 0.867 - Even more content...
... (68 more lines)
```
**After (summary mode - DEFAULT):**
```
✅ [LLM] PRIMARY | 14:23:45.123 | Reply: Based on your question about...
📊 Context | Session: abc123 | Messages: 42 | Last: 5.2min | RAG: 5 results
🧠 Monologue | question | Tone: curious
✨ PIPELINE COMPLETE | Session: abc123 | Total: 1250ms
📤 Output: 342 characters
```
**After (detailed mode - for debugging):**
```
════════════════════════════════════════════════════════════════════════════════════════════════════
🚀 PIPELINE START | Session: abc123 | 14:23:45.123
════════════════════════════════════════════════════════════════════════════════════════════════════
📝 User: What is the meaning of life?
────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────
🧠 LLM CALL | Backend: PRIMARY | 14:23:45.234
────────────────────────────────────────────────────────────────────────────────────────────────────
📝 Prompt: You are Lyra, a thoughtful AI assistant...
💬 Reply: Based on philosophical perspectives, the meaning...
📊 Context | Session: abc123 | Messages: 42 | Last: 5.2min | RAG: 5 results
────────────────────────────────────────────────────────────────────────────────────────────────────
[CONTEXT] Session abc123 | User: What is the meaning of life?
────────────────────────────────────────────────────────────────────────────────────────────────────
Mode: default | Mood: neutral | Project: None
Tools: RAG, WEB, WEATHER, CODEBRAIN, POKERBRAIN
╭─ INTAKE SUMMARIES ────────────────────────────────────────────────
│ L1 : Last message discussed philosophy...
│ L5 : Recent 5 messages covered existential topics...
│ L10 : Past 10 messages showed curiosity pattern...
│ L20 : Session focused on deep questions...
│ L30 : Long-term trend shows philosophical interest...
╰───────────────────────────────────────────────────────────────────
╭─ RAG RESULTS (5) ──────────────────────────────────────────────
│ [1] 0.923 | Previous discussion about purpose and meaning...
│ [2] 0.891 | Note about existential philosophy...
│ [3] 0.867 | Memory of Viktor Frankl discussion...
│ [4] 0.834 | Reference to stoic philosophy...
│ [5] 0.801 | Buddhism and the middle path...
╰───────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────
════════════════════════════════════════════════════════════════════════════════════════════════════
✨ PIPELINE COMPLETE | Session: abc123 | Total: 1250ms
════════════════════════════════════════════════════════════════════════════════════════════════════
⏱️ Stage Timings:
context : 150ms ( 12.0%)
identity : 10ms ( 0.8%)
monologue : 200ms ( 16.0%)
tools : 0ms ( 0.0%)
reflection : 50ms ( 4.0%)
reasoning : 450ms ( 36.0%)
refinement : 300ms ( 24.0%)
persona : 140ms ( 11.2%)
📤 Output: 342 characters
════════════════════════════════════════════════════════════════════════════════════════════════════
```
### 2. **Configurable Verbosity Levels**
Set via `LOG_DETAIL_LEVEL` environment variable:
- **`minimal`**: Only errors and critical events
- **`summary`**: Stage completion + errors (DEFAULT - recommended for production)
- **`detailed`**: Include raw LLM outputs, RAG results, timing breakdowns (for debugging)
- **`verbose`**: Everything including full JSON dumps (for deep debugging)
### 3. **Raw LLM Output Visibility** ✅
**You can now see raw LLM outputs clearly!**
In `detailed` or `verbose` mode, LLM calls show:
- Backend used
- Prompt preview
- Parsed reply
- **Raw JSON response in collapsible format** (verbose only)
```
╭─ RAW RESPONSE ────────────────────────────────────────────────────────────────────────────
│ {
│ "id": "chatcmpl-123",
│ "object": "chat.completion",
│ "model": "gpt-4",
│ "choices": [
│ {
│ "message": {
│ "content": "Full response here..."
│ }
│ }
│ ]
│ }
╰───────────────────────────────────────────────────────────────────────────────────────────
```
### 4. **Loop Detection & Protection** ✅
**New safety features:**
- **Duplicate message detection**: Prevents processing the same message twice
- **Message history trimming**: Auto-trims to last 100 messages (configurable via `MAX_MESSAGE_HISTORY`)
- **Session TTL**: Auto-expires inactive sessions after 24 hours (configurable via `SESSION_TTL_HOURS`)
- **Hash-based detection**: Uses MD5 hash to detect exact duplicate messages
**Example warning when loop detected:**
```
⚠️ DUPLICATE MESSAGE DETECTED | Session: abc123 | Message: What is the meaning of life?
🔁 LOOP DETECTED - Returning cached context to prevent processing duplicate
```
### 5. **Performance Timing** ✅
In `detailed` mode, see exactly where time is spent:
```
⏱️ Stage Timings:
context : 150ms ( 12.0%) ← Context collection
identity : 10ms ( 0.8%) ← Identity loading
monologue : 200ms ( 16.0%) ← Inner monologue
tools : 0ms ( 0.0%) ← Autonomous tools
reflection : 50ms ( 4.0%) ← Reflection notes
reasoning : 450ms ( 36.0%) ← Main reasoning (BOTTLENECK)
refinement : 300ms ( 24.0%) ← Answer refinement
persona : 140ms ( 11.2%) ← Persona layer
```
**This helps you identify weak links in the chain!**
## 📁 Files Modified
### Core Changes
1. **[llm.js](core/relay/lib/llm.js)**
- Removed massive JSON dump on line 53
- Added structured logging with 4 verbosity levels
- Shows raw responses only in verbose mode (collapsible format)
- Tracks failed backends and shows summary on total failure
2. **[context.py](cortex/context.py)**
- Condensed 71-line NeoMem loop to 5-line summary
- Removed repeated intake data dumps
- Added structured hierarchical logging with boxes
- Added duplicate message detection
- Added message history trimming
- Added session TTL and cleanup
3. **[router.py](cortex/router.py)**
- Replaced 15+ stage logs with unified pipeline summary
- Added stage timing collection
- Shows performance breakdown in detailed mode
- Clean start/end markers with total duration
### New Files
4. **[utils/logging_utils.py](cortex/utils/logging_utils.py)** (NEW)
- Reusable structured logging utilities
- `PipelineLogger` class for hierarchical logging
- Collapsible data sections
- Stage timing tracking
- Future-ready for expansion
5. **[.env.logging.example](.env.logging.example)** (NEW)
- Complete logging configuration guide
- Shows example output at each verbosity level
- Documents all environment variables
- Production-ready defaults
6. **[LOGGING_REFACTOR_SUMMARY.md](LOGGING_REFACTOR_SUMMARY.md)** (THIS FILE)
## 🚀 How to Use
### For Finding Weak Links (Your Use Case)
```bash
# Set in your .env or export:
export LOG_DETAIL_LEVEL=detailed
export VERBOSE_DEBUG=false # or true for even more detail
# Now run your chat - you'll see:
# 1. Which LLM backend is used
# 2. Raw LLM outputs (in verbose mode)
# 3. Exact timing per stage
# 4. Which stage is taking longest
```
### For Production
```bash
export LOG_DETAIL_LEVEL=summary
# Minimal, clean logs:
# ✅ [LLM] PRIMARY | 14:23:45.123 | Reply: Based on your question...
# ✨ PIPELINE COMPLETE | Session: abc123 | Total: 1250ms
```
### For Deep Debugging
```bash
export LOG_DETAIL_LEVEL=verbose
export LOG_RAW_CONTEXT_DATA=true
# Shows EVERYTHING including full JSON dumps
```
## 🔍 Finding Weak Links - Quick Guide
**Problem: "Which LLM stage is failing or producing bad output?"**
1. Set `LOG_DETAIL_LEVEL=detailed`
2. Run a test conversation
3. Look for timing anomalies:
```
reasoning : 3450ms ( 76.0%) ← BOTTLENECK!
```
4. Look for errors:
```
⚠️ Reflection failed: Connection timeout
```
5. Check raw LLM outputs (set `VERBOSE_DEBUG=true`):
```
╭─ RAW RESPONSE ────────────────────────────────────
│ {
│ "choices": [
│ { "message": { "content": "..." } }
│ ]
│ }
╰───────────────────────────────────────────────────
```
**Problem: "Is the loop repeating operations?"**
1. Enable duplicate detection (on by default)
2. Look for loop warnings:
```
⚠️ DUPLICATE MESSAGE DETECTED | Session: abc123
🔁 LOOP DETECTED - Returning cached context
```
3. Check stage timings - repeated stages will show up as duplicates
**Problem: "Which RAG memories are being used?"**
1. Set `LOG_DETAIL_LEVEL=detailed`
2. Look for RAG results box:
```
╭─ RAG RESULTS (5) ──────────────────────────────
│ [1] 0.923 | Previous discussion about X...
│ [2] 0.891 | Note about Y...
╰────────────────────────────────────────────────
```
## 📊 Environment Variables Reference
| Variable | Default | Description |
|----------|---------|-------------|
| `LOG_DETAIL_LEVEL` | `summary` | Verbosity: minimal/summary/detailed/verbose |
| `VERBOSE_DEBUG` | `false` | Legacy flag for maximum verbosity |
| `LOG_RAW_CONTEXT_DATA` | `false` | Show full intake data dumps |
| `ENABLE_DUPLICATE_DETECTION` | `true` | Detect and prevent duplicate messages |
| `MAX_MESSAGE_HISTORY` | `100` | Max messages to keep per session |
| `SESSION_TTL_HOURS` | `24` | Auto-expire sessions after N hours |
## 🎉 Results
**Before:** 1000+ lines of logs per chat message, unreadable, couldn't identify issues
**After (summary mode):** 5 lines of structured logs, clear and actionable
**After (detailed mode):** ~50 lines with full visibility into each stage, timing, and raw outputs
**Loop protection:** Automatic detection and prevention of duplicate processing
**You can now:**
✅ See raw LLM outputs clearly (in detailed/verbose mode)
✅ Identify performance bottlenecks (stage timings)
✅ Detect loops and duplicates (automatic)
✅ Find failing stages (error markers)
✅ Scan logs quickly (hierarchical structure)
✅ Debug production issues (adjustable verbosity)
## 🔧 Next Steps (Optional Improvements)
1. **Structured JSON logging**: Output as JSON for log aggregation tools
2. **Log rotation**: Implement file rotation for verbose logs
3. **Metrics export**: Export stage timings to Prometheus/Grafana
4. **Error categorization**: Tag errors by type (network, timeout, parsing, etc.)
5. **Performance alerts**: Auto-alert when stages exceed thresholds
---
**Happy debugging! You can now see what's actually happening in the cortex loop.** 🎯
+11 -681
View File
@@ -1,691 +1,21 @@
# Project Lyra - README v0.6.0
# Lyra
Lyra is a modular persistent AI companion system with advanced reasoning capabilities and autonomous decision-making.
It provides memory-backed chat using **Relay** + **Cortex** with integrated **Autonomy System**,
featuring a multi-stage reasoning pipeline powered by HTTP-based LLM backends.
A persistent, autonomous AI assistant. From-scratch rewrite of an earlier attempt.
**Current Version:** v0.6.0 (2025-12-18)
The design thinking that survives the rewrite lives in [`docs/`](docs/) — start with [`docs/ARCH_v0-6-1.md`](docs/ARCH_v0-6-1.md). The previous implementation is preserved on the `archive` branch.
> **Note:** As of v0.6.0, NeoMem is **disabled by default** while we work out integration hiccups in the pipeline. The autonomy system is being refined independently before full memory integration.
## Status
## Mission Statement
Pre-MVP. Building toward the smallest useful version: chat with persistent memory across sessions.
The point of Project Lyra is to give an AI chatbot more abilities than a typical chatbot. Typical chatbots are essentially amnesic and forget evertything about your project. Lyra helps keep projects organized and remembers everything you have done. Think of her abilities as a notepad/schedule/database/co-creator/collaborator all with its own executive function. Say something in passing, Lyra remembers it then reminds you of it later.
## Setup
---
## Architecture Overview
Project Lyra operates as a **single docker-compose deployment** with multiple Docker containers networked together in a microservices architecture. Like how the brain has regions, Lyra has modules:
### Core Services
**1. Relay** (Node.js/Express) - Port 7078
- Main orchestrator and message router
- Coordinates all module interactions
- OpenAI-compatible endpoint: `POST /v1/chat/completions`
- Internal endpoint: `POST /chat`
- Routes messages through Cortex reasoning pipeline
- Manages async calls to Cortex ingest
- *(NeoMem integration currently disabled in v0.6.0)*
**2. UI** (Static HTML)
- Browser-based chat interface with cyberpunk theme
- Connects to Relay
- Saves and loads sessions
- OpenAI-compatible message format
**3. NeoMem** (Python/FastAPI) - Port 7077 - **DISABLED IN v0.6.0**
- Long-term memory database (fork of Mem0 OSS)
- Vector storage (PostgreSQL + pgvector) + Graph storage (Neo4j)
- RESTful API: `/memories`, `/search`
- Semantic memory updates and retrieval
- No external SDK dependencies - fully local
- **Status:** Currently disabled while pipeline integration is refined
### Reasoning Layer
**4. Cortex** (Python/FastAPI) - Port 7081
- Primary reasoning engine with multi-stage pipeline and autonomy system
- **Includes embedded Intake module** (no separate service as of v0.5.1)
- **Integrated Autonomy System** (NEW in v0.6.0) - See Autonomy System section below
- **4-Stage Processing:**
1. **Reflection** - Generates meta-awareness notes about conversation
2. **Reasoning** - Creates initial draft answer using context
3. **Refinement** - Polishes and improves the draft
4. **Persona** - Applies Lyra's personality and speaking style
- Integrates with Intake for short-term context via internal Python imports
- Flexible LLM router supporting multiple backends via HTTP
- **Endpoints:**
- `POST /reason` - Main reasoning pipeline
- `POST /ingest` - Receives conversation exchanges from Relay
- `GET /health` - Service health check
- `GET /debug/sessions` - Inspect in-memory SESSIONS state
- `GET /debug/summary` - Test summarization for a session
**5. Intake** (Python Module) - **Embedded in Cortex**
- **No longer a standalone service** - runs as Python module inside Cortex container
- Short-term memory management with session-based circular buffer
- In-memory SESSIONS dictionary: `session_id → {buffer: deque(maxlen=200), created_at: timestamp}`
- Multi-level summarization (L1/L5/L10/L20/L30) produced by `summarize_context()`
- Deferred summarization - actual summary generation happens during `/reason` call
- Internal Python API:
- `add_exchange_internal(exchange)` - Direct function call from Cortex
- `summarize_context(session_id, exchanges)` - Async LLM-based summarization
- `SESSIONS` - Module-level global state (requires single Uvicorn worker)
### LLM Backends (HTTP-based)
**All LLM communication is done via HTTP APIs:**
- **PRIMARY**: llama.cpp server (`http://10.0.0.44:8080`) - AMD MI50 GPU backend
- **SECONDARY**: Ollama server (`http://10.0.0.3:11434`) - RTX 3090 backend
- Model: qwen2.5:7b-instruct-q4_K_M
- **CLOUD**: OpenAI API (`https://api.openai.com/v1`) - Cloud-based models
- Model: gpt-4o-mini
- **FALLBACK**: Local backup (`http://10.0.0.41:11435`) - Emergency fallback
- Model: llama-3.2-8b-instruct
Each module can be configured to use a different backend via environment variables.
### Autonomy System (NEW in v0.6.0)
**Cortex Autonomy Subsystems** - Multi-layered autonomous decision-making and learning
- **Executive Layer** [cortex/autonomy/executive/](cortex/autonomy/executive/)
- High-level planning and goal setting
- Multi-step reasoning for complex objectives
- Strategic decision making
- **Decision Engine** [cortex/autonomy/tools/decision_engine.py](cortex/autonomy/tools/decision_engine.py)
- Autonomous decision-making framework
- Option evaluation and selection
- Coordinated decision orchestration
- **Autonomous Actions** [cortex/autonomy/actions/](cortex/autonomy/actions/)
- Self-initiated action execution
- Context-aware behavior implementation
- Action logging and tracking
- **Pattern Learning** [cortex/autonomy/learning/](cortex/autonomy/learning/)
- Learns from interaction patterns
- Identifies recurring user needs
- Adaptive behavior refinement
- **Proactive Monitoring** [cortex/autonomy/proactive/](cortex/autonomy/proactive/)
- System state monitoring
- Intervention opportunity detection
- Background awareness capabilities
- **Self-Analysis** [cortex/autonomy/self/](cortex/autonomy/self/)
- Performance tracking and analysis
- Cognitive pattern identification
- Self-state persistence in [cortex/data/self_state.json](cortex/data/self_state.json)
- **Orchestrator** [cortex/autonomy/tools/orchestrator.py](cortex/autonomy/tools/orchestrator.py)
- Coordinates all autonomy subsystems
- Manages tool selection and execution
- Handles external integrations (with enable/disable controls)
**Autonomy Architecture:**
The autonomy system operates in coordinated layers, all maintaining state in `self_state.json`:
1. Executive Layer → Planning and goals
2. Decision Layer → Evaluation and choices
3. Action Layer → Execution
4. Learning Layer → Pattern adaptation
5. Monitoring Layer → Proactive awareness
---
## Data Flow Architecture (v0.6.0)
### Normal Message Flow:
```
User (UI) → POST /v1/chat/completions
Relay (7078)
↓ POST /reason
Cortex (7081)
↓ (internal Python call)
Intake module → summarize_context()
Autonomy System → Decision evaluation & pattern learning
Cortex processes (4 stages):
1. reflection.py → meta-awareness notes (CLOUD backend)
2. reasoning.py → draft answer (PRIMARY backend, autonomy-aware)
3. refine.py → refined answer (PRIMARY backend)
4. persona/speak.py → Lyra personality (CLOUD backend, autonomy-aware)
Returns persona answer to Relay
Relay → POST /ingest (async)
Cortex → add_exchange_internal() → SESSIONS buffer
Autonomy System → Update self_state.json (pattern tracking)
Relay → UI (returns final response)
Note: NeoMem integration disabled in v0.6.0
```
### Cortex 4-Stage Reasoning Pipeline:
1. **Reflection** (`reflection.py`) - Cloud LLM (OpenAI)
- Analyzes user intent and conversation context
- Generates meta-awareness notes
- "What is the user really asking?"
2. **Reasoning** (`reasoning.py`) - Primary LLM (llama.cpp)
- Retrieves short-term context from Intake module
- Creates initial draft answer
- Integrates context, reflection notes, and user prompt
3. **Refinement** (`refine.py`) - Primary LLM (llama.cpp)
- Polishes the draft answer
- Improves clarity and coherence
- Ensures factual consistency
4. **Persona** (`speak.py`) - Cloud LLM (OpenAI)
- Applies Lyra's personality and speaking style
- Natural, conversational output
- Final answer returned to user
---
## Features
### Core Services
**Relay**:
- Main orchestrator and message router
- OpenAI-compatible endpoint: `POST /v1/chat/completions`
- Internal endpoint: `POST /chat`
- Health check: `GET /_health`
- Async non-blocking calls to Cortex
- Shared request handler for code reuse
- Comprehensive error handling
**NeoMem (Memory Engine)**:
- Forked from Mem0 OSS - fully independent
- Drop-in compatible API (`/memories`, `/search`)
- Local-first: runs on FastAPI with Postgres + Neo4j
- No external SDK dependencies
- Semantic memory updates - compares embeddings and performs in-place updates
- Default service: `neomem-api` (port 7077)
**UI**:
- Lightweight static HTML chat interface
- Cyberpunk theme
- Session save/load functionality
- OpenAI message format support
### Reasoning Layer
**Cortex** (v0.5.1):
- Multi-stage reasoning pipeline (reflection → reasoning → refine → persona)
- Flexible LLM backend routing via HTTP
- Per-stage backend selection
- Async processing throughout
- Embedded Intake module for short-term context
- `/reason`, `/ingest`, `/health`, `/debug/sessions`, `/debug/summary` endpoints
- Lenient error handling - never fails the chat pipeline
**Intake** (Embedded Module):
- **Architectural change**: Now runs as Python module inside Cortex container
- In-memory SESSIONS management (session_id → buffer)
- Multi-level summarization: L1 (ultra-short), L5 (short), L10 (medium), L20 (detailed), L30 (full)
- Deferred summarization strategy - summaries generated during `/reason` call
- `bg_summarize()` is a logging stub - actual work deferred
- **Single-worker constraint**: SESSIONS requires single Uvicorn worker or Redis/shared storage
**LLM Router**:
- Dynamic backend selection via HTTP
- Environment-driven configuration
- Support for llama.cpp, Ollama, OpenAI, custom endpoints
- Per-module backend preferences:
- `CORTEX_LLM=SECONDARY` (Ollama for reasoning)
- `INTAKE_LLM=PRIMARY` (llama.cpp for summarization)
- `SPEAK_LLM=OPENAI` (Cloud for persona)
- `NEOMEM_LLM=PRIMARY` (llama.cpp for memory operations)
### Beta Lyrae (RAG Memory DB) - Currently Disabled
- **RAG Knowledge DB - Beta Lyrae (sheliak)**
- This module implements the **Retrieval-Augmented Generation (RAG)** layer for Project Lyra.
- It serves as the long-term searchable memory store that Cortex and Relay can query for relevant context before reasoning or response generation.
- **Status**: Disabled in docker-compose.yml (v0.5.1)
The system uses:
- **ChromaDB** for persistent vector storage
- **OpenAI Embeddings (`text-embedding-3-small`)** for semantic similarity
- **FastAPI** (port 7090) for the `/rag/search` REST endpoint
Directory Layout:
```
rag/
├── rag_chat_import.py # imports JSON chat logs
├── rag_docs_import.py # (planned) PDF/EPUB/manual importer
├── rag_build.py # legacy single-folder builder
├── rag_query.py # command-line query helper
├── rag_api.py # FastAPI service providing /rag/search
├── chromadb/ # persistent vector store
├── chatlogs/ # organized source data
│ ├── poker/
│ ├── work/
│ ├── lyra/
│ ├── personal/
│ └── ...
└── import.log # progress log for batch runs
```
**OpenAI chatlog importer features:**
- Recursive folder indexing with **category detection** from directory name
- Smart chunking for long messages (5,000 chars per slice)
- Automatic deduplication using SHA-1 hash of file + chunk
- Timestamps for both file modification and import time
- Full progress logging via tqdm
- Safe to run in background with `nohup … &`
---
## Docker Deployment
All services run in a single docker-compose stack with the following containers:
**Active Services:**
- **relay** - Main orchestrator (port 7078)
- **cortex** - Reasoning engine with embedded Intake and Autonomy System (port 7081)
**Disabled Services (v0.6.0):**
- **neomem-postgres** - PostgreSQL with pgvector extension (port 5432) - *disabled while refining pipeline*
- **neomem-neo4j** - Neo4j graph database (ports 7474, 7687) - *disabled while refining pipeline*
- **neomem-api** - NeoMem memory service (port 7077) - *disabled while refining pipeline*
- **intake** - No longer needed (embedded in Cortex as of v0.5.1)
- **rag** - Beta Lyrae RAG service (port 7090) - currently disabled
All containers communicate via the `lyra_net` Docker bridge network.
## External LLM Services
The following LLM backends are accessed via HTTP (not part of docker-compose):
- **llama.cpp Server** (`http://10.0.0.44:8080`)
- AMD MI50 GPU-accelerated inference
- Primary backend for reasoning and refinement stages
- Model path: `/model`
- **Ollama Server** (`http://10.0.0.3:11434`)
- RTX 3090 GPU-accelerated inference
- Secondary/configurable backend
- Model: qwen2.5:7b-instruct-q4_K_M
- **OpenAI API** (`https://api.openai.com/v1`)
- Cloud-based inference
- Used for reflection and persona stages
- Model: gpt-4o-mini
- **Fallback Server** (`http://10.0.0.41:11435`)
- Emergency backup endpoint
- Local llama-3.2-8b-instruct model
---
## Version History
### v0.6.0 (2025-12-18) - Current Release
**Major Feature: Autonomy System (Phase 1, 2, and 2.5)**
- ✅ Added autonomous decision-making framework
- ✅ Implemented executive planning and goal-setting layer
- ✅ Added pattern learning system for adaptive behavior
- ✅ Implemented proactive monitoring capabilities
- ✅ Created self-analysis and performance tracking system
- ✅ Integrated self-state persistence (`cortex/data/self_state.json`)
- ✅ Built decision engine with orchestrator coordination
- ✅ Added autonomous action execution framework
- ✅ Integrated autonomy into reasoning and persona layers
- ✅ Created comprehensive test suites for autonomy features
- ✅ Added complete system breakdown documentation
**Architecture Changes:**
- Autonomy system integrated into Cortex reasoning pipeline
- Multi-layered autonomous decision-making architecture
- Self-state tracking across sessions
- NeoMem disabled by default while refining pipeline integration
- Enhanced orchestrator with flexible service controls
**Documentation:**
- Added [PROJECT_LYRA_COMPLETE_BREAKDOWN.md](docs/PROJECT_LYRA_COMPLETE_BREAKDOWN.md)
- Updated changelog with comprehensive autonomy system details
### v0.5.1 (2025-12-11)
**Critical Intake Integration Fixes:**
- ✅ Fixed `bg_summarize()` NameError preventing SESSIONS persistence
- ✅ Fixed `/ingest` endpoint unreachable code
- ✅ Added `cortex/intake/__init__.py` for proper package structure
- ✅ Added diagnostic logging to verify SESSIONS singleton behavior
- ✅ Added `/debug/sessions` and `/debug/summary` endpoints
- ✅ Documented single-worker constraint in Dockerfile
- ✅ Implemented lenient error handling (never fails chat pipeline)
- ✅ Intake now embedded in Cortex - no longer standalone service
**Architecture Changes:**
- Intake module runs inside Cortex container as pure Python import
- No HTTP calls between Cortex and Intake (internal function calls)
- SESSIONS persist correctly in Uvicorn worker
- Deferred summarization strategy (summaries generated during `/reason`)
### v0.5.0 (2025-11-28)
- ✅ Fixed all critical API wiring issues
- ✅ Added OpenAI-compatible endpoint to Relay (`/v1/chat/completions`)
- ✅ Fixed Cortex → Intake integration
- ✅ Added missing Python package `__init__.py` files
- ✅ End-to-end message flow verified and working
### Infrastructure v1.0.0 (2025-11-26)
- Consolidated 9 scattered `.env` files into single source of truth
- Multi-backend LLM strategy implemented
- Docker Compose consolidation
- Created `.env.example` security templates
### v0.4.x (Major Rewire)
- Cortex multi-stage reasoning pipeline
- LLM router with multi-backend support
- Major architectural restructuring
### v0.3.x
- Beta Lyrae RAG system
- NeoMem integration
- Basic Cortex reasoning loop
---
## Known Issues (v0.6.0)
### Temporarily Disabled (v0.6.0)
- **NeoMem disabled by default** - Being refined independently before full integration
- PostgreSQL + pgvector storage inactive
- Neo4j graph database inactive
- Memory persistence endpoints not active
- RAG service (Beta Lyrae) currently disabled in docker-compose.yml
### Non-Critical
- Session management endpoints not fully implemented in Relay
- Full autonomy system integration still being refined
- Memory retrieval integration pending NeoMem re-enablement
### Operational Notes
- **Single-worker constraint**: Cortex must run with single Uvicorn worker to maintain SESSIONS state
- Multi-worker scaling requires migrating SESSIONS to Redis or shared storage
- Diagnostic endpoints (`/debug/sessions`, `/debug/summary`) available for troubleshooting
### Future Enhancements
- Re-enable NeoMem integration after pipeline refinement
- Full autonomy system maturation and optimization
- Re-enable RAG service integration
- Implement full session persistence
- Migrate SESSIONS to Redis for multi-worker support
- Add request correlation IDs for tracing
- Comprehensive health checks across all services
- Enhanced pattern learning with long-term memory integration
---
## Quick Start
### Prerequisites
- Docker + Docker Compose
- At least one HTTP-accessible LLM endpoint (llama.cpp, Ollama, or OpenAI API key)
### Setup
1. Copy `.env.example` to `.env` and configure your LLM backend URLs and API keys:
```bash
# Required: Configure at least one LLM backend
LLM_PRIMARY_URL=http://10.0.0.44:8080 # llama.cpp
LLM_SECONDARY_URL=http://10.0.0.3:11434 # Ollama
OPENAI_API_KEY=sk-... # OpenAI
```
2. Start all services with docker-compose:
```bash
docker-compose up -d
```
3. Check service health:
```bash
# Relay health
curl http://localhost:7078/_health
# Cortex health
curl http://localhost:7081/health
# NeoMem health
curl http://localhost:7077/health
```
4. Access the UI at `http://localhost:7078`
### Test
**Test Relay → Cortex pipeline:**
```bash
curl -X POST http://localhost:7078/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [{"role": "user", "content": "Hello Lyra!"}],
"session_id": "test"
}'
uv sync
cp .env.example .env
# fill in ANTHROPIC_API_KEY and point LOCAL_BASE_URL at your Ollama
```
**Test Cortex /ingest endpoint:**
```bash
curl -X POST http://localhost:7081/ingest \
-H "Content-Type: application/json" \
-d '{
"session_id": "test",
"user_msg": "Hello",
"assistant_msg": "Hi there!"
}'
```
## Architecture
**Inspect SESSIONS state:**
```bash
curl http://localhost:7081/debug/sessions
```
**Get summary for a session:**
```bash
curl "http://localhost:7081/debug/summary?session_id=test"
```
All backend databases (PostgreSQL and Neo4j) are automatically started as part of the docker-compose stack.
---
## Environment Variables
### LLM Backend Configuration
**Backend URLs (Full API endpoints):**
```bash
LLM_PRIMARY_URL=http://10.0.0.44:8080 # llama.cpp
LLM_PRIMARY_MODEL=/model
LLM_SECONDARY_URL=http://10.0.0.3:11434 # Ollama
LLM_SECONDARY_MODEL=qwen2.5:7b-instruct-q4_K_M
LLM_OPENAI_URL=https://api.openai.com/v1
LLM_OPENAI_MODEL=gpt-4o-mini
OPENAI_API_KEY=sk-...
```
**Module-specific backend selection:**
```bash
CORTEX_LLM=SECONDARY # Use Ollama for reasoning
INTAKE_LLM=PRIMARY # Use llama.cpp for summarization
SPEAK_LLM=OPENAI # Use OpenAI for persona
NEOMEM_LLM=PRIMARY # Use llama.cpp for memory
UI_LLM=OPENAI # Use OpenAI for UI
RELAY_LLM=PRIMARY # Use llama.cpp for relay
```
### Database Configuration
```bash
POSTGRES_USER=neomem
POSTGRES_PASSWORD=neomempass
POSTGRES_DB=neomem
POSTGRES_HOST=neomem-postgres
POSTGRES_PORT=5432
NEO4J_URI=bolt://neomem-neo4j:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=neomemgraph
```
### Service URLs (Internal Docker Network)
```bash
NEOMEM_API=http://neomem-api:7077
CORTEX_API=http://cortex:7081
CORTEX_REASON_URL=http://cortex:7081/reason
CORTEX_INGEST_URL=http://cortex:7081/ingest
RELAY_URL=http://relay:7078
```
### Feature Flags
```bash
CORTEX_ENABLED=true
MEMORY_ENABLED=true
PERSONA_ENABLED=false
DEBUG_PROMPT=true
VERBOSE_DEBUG=true
```
For complete environment variable reference, see [ENVIRONMENT_VARIABLES.md](ENVIRONMENT_VARIABLES.md).
---
## Documentation
- [CHANGELOG.md](CHANGELOG.md) - Detailed version history
- [PROJECT_SUMMARY.md](PROJECT_SUMMARY.md) - Comprehensive project overview for AI context
- [ENVIRONMENT_VARIABLES.md](ENVIRONMENT_VARIABLES.md) - Environment variable reference
- [DEPRECATED_FILES.md](DEPRECATED_FILES.md) - Deprecated files and migration guide
---
## Troubleshooting
### SESSIONS not persisting
**Symptom:** Intake buffer always shows 0 exchanges, summaries always empty.
**Solution (Fixed in v0.5.1):**
- Ensure `cortex/intake/__init__.py` exists
- Check Cortex logs for `[Intake Module Init]` message showing SESSIONS object ID
- Verify single-worker mode (Dockerfile: `uvicorn main:app --workers 1`)
- Use `/debug/sessions` endpoint to inspect current state
### Cortex connection errors
**Symptom:** Relay can't reach Cortex, 502 errors.
**Solution:**
- Verify Cortex container is running: `docker ps | grep cortex`
- Check Cortex health: `curl http://localhost:7081/health`
- Verify environment variables: `CORTEX_REASON_URL=http://cortex:7081/reason`
- Check docker network: `docker network inspect lyra_net`
### LLM backend timeouts
**Symptom:** Reasoning stage hangs or times out.
**Solution:**
- Verify LLM backend is running and accessible
- Check LLM backend health: `curl http://10.0.0.44:8080/health`
- Increase timeout in llm_router.py if using slow models
- Check logs for specific backend errors
---
## License
NeoMem is a derivative work based on Mem0 OSS (Apache 2.0).
© 2025 Terra-Mechanics / ServersDown Labs. All modifications released under Apache 2.0.
**Built with Claude Code**
---
## Integration Notes
- NeoMem API is compatible with Mem0 OSS endpoints (`/memories`, `/search`)
- All services communicate via Docker internal networking on the `lyra_net` bridge
- History and entity graphs are managed via PostgreSQL + Neo4j
- LLM backends are accessed via HTTP and configured in `.env`
- Intake module is imported internally by Cortex (no HTTP communication)
- SESSIONS state is maintained in-memory within Cortex container
---
## Beta Lyrae - RAG Memory System (Currently Disabled)
**Note:** The RAG service is currently disabled in docker-compose.yml
### Requirements
- Python 3.10+
- Dependencies: `chromadb openai tqdm python-dotenv fastapi uvicorn`
- Persistent storage: `./chromadb` or `/mnt/data/lyra_rag_db`
### Setup
1. Import chat logs (must be in OpenAI message format):
```bash
python3 rag/rag_chat_import.py
```
2. Build and start the RAG API server:
```bash
cd rag
python3 rag_build.py
uvicorn rag_api:app --host 0.0.0.0 --port 7090
```
3. Query the RAG system:
```bash
curl -X POST http://127.0.0.1:7090/rag/search \
-H "Content-Type: application/json" \
-d '{
"query": "What is the current state of Cortex?",
"where": {"category": "lyra"}
}'
```
---
## Development Notes
### Cortex Architecture (v0.6.0)
- Cortex contains embedded Intake module at `cortex/intake/`
- Intake is imported as: `from intake.intake import add_exchange_internal, SESSIONS`
- SESSIONS is a module-level global dictionary (singleton pattern)
- Single-worker constraint required to maintain SESSIONS state
- Diagnostic endpoints available for debugging: `/debug/sessions`, `/debug/summary`
- **NEW:** Autonomy system integrated at `cortex/autonomy/`
- Executive, decision, action, learning, and monitoring layers
- Self-state persistence in `cortex/data/self_state.json`
- Coordinated via orchestrator with flexible service controls
### Adding New LLM Backends
1. Add backend URL to `.env`:
```bash
LLM_CUSTOM_URL=http://your-backend:port
LLM_CUSTOM_MODEL=model-name
```
2. Configure module to use new backend:
```bash
CORTEX_LLM=CUSTOM
```
3. Restart Cortex container:
```bash
docker-compose restart cortex
```
### Debugging Tips
- Enable verbose logging: `VERBOSE_DEBUG=true` in `.env`
- Check Cortex logs: `docker logs cortex -f`
- Inspect SESSIONS: `curl http://localhost:7081/debug/sessions`
- Test summarization: `curl "http://localhost:7081/debug/summary?session_id=test"`
- Check Relay logs: `docker logs relay -f`
- Monitor Docker network: `docker network inspect lyra_net`
The long-term target is the cognitive split in `docs/ARCH_v0-6-1.md` — Inner Self as the seat of consciousness, Executive for hard reasoning, Cortex Chat for drafting, Persona for voice. The MVP implements only the chat + memory baseline. Cognitive layers come back one at a time.
-27
View File
@@ -1,27 +0,0 @@
#openAI API Key (used by mem0 and relay)
OPENAI_API_KEY=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
MEM0_URL=http://mem0:8080
OLLAMA_URL=http://10.0.0.3:11434
PERSONA_URL=http://persona-sidecar:7080/current
DEBUG_PROMPT=true
#ollama embedder
EMBEDDER=ollama
EMBEDDER_MODEL=nomic-embed-text
# neo4j
NEO4J_URL=bolt://neo4j:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=mem0graph
# Mem0
MEM0_URL=http://mem0:8080
HISTORY_DB_PATH=/app/history/history.db
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_DB: mem0
POSTGRES_USER: mem0
POSTGRES_PASSWORD: mem0pass
-10
View File
@@ -1,10 +0,0 @@
#openAI API Key (used by mem0 and relay)
OPENAI_API_KEY=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
MEM0_URL=http://mem0:8080
OLLAMA_URL=http://10.0.0.3:11434
PERSONA_URL=http://persona-sidecar:7080/current
DEBUG_PROMPT=true
EMBEDDER=openai
EMBEDDER_MODEL=text-embedding-3-small
-14
View File
@@ -1,14 +0,0 @@
FROM node:18-alpine
WORKDIR /app
# install deps
COPY package.json ./package.json
RUN npm install --production
# copy code + config
COPY persona-server.js ./persona-server.js
COPY personas.json ./personas.json
EXPOSE 7080
CMD ["node", "persona-server.js"]
-8
View File
@@ -1,8 +0,0 @@
{
"name": "persona-sidecar",
"version": "0.1.0",
"type": "module",
"dependencies": {
"express": "^4.19.2"
}
}
-78
View File
@@ -1,78 +0,0 @@
// persona-server.js — Persona Sidecar v0.1.0 (Docker Lyra)
// Node 18+, Express REST
import express from "express";
import fs from "fs";
const app = express();
app.use(express.json());
const PORT = process.env.PORT || 7080;
const CONFIG_FILE = process.env.PERSONAS_FILE || "./personas.json";
// allow JSON with // and /* */ comments
function parseJsonWithComments(raw) {
return JSON.parse(
raw
.replace(/\/\*[\s\S]*?\*\//g, "") // block comments
.replace(/^\s*\/\/.*$/gm, "") // line comments
);
}
function loadConfig() {
const raw = fs.readFileSync(CONFIG_FILE, "utf-8");
return parseJsonWithComments(raw);
}
function saveConfig(cfg) {
fs.writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2));
}
// GET /persona → active persona JSON
app.get("/persona", (_req, res) => {
try {
const cfg = loadConfig();
const active = cfg.active;
const persona = cfg.personas?.[active];
if (!persona) return res.status(404).json({ error: "Active persona not found" });
res.json({ active, persona });
} catch (err) {
res.status(500).json({ error: String(err.message || err) });
}
});
// GET /personas → all personas
app.get("/personas", (_req, res) => {
try {
const cfg = loadConfig();
res.json(cfg.personas || {});
} catch (err) {
res.status(500).json({ error: String(err.message || err) });
}
});
// POST /persona/select { name }
app.post("/persona/select", (req, res) => {
try {
const { name } = req.body || {};
if (!name) return res.status(400).json({ error: "Missing 'name'" });
const cfg = loadConfig();
if (!cfg.personas || !cfg.personas[name]) {
return res.status(404).json({ error: `Persona '${name}' not found` });
}
cfg.active = name;
saveConfig(cfg);
res.json({ ok: true, active: name });
} catch (err) {
res.status(500).json({ error: String(err.message || err) });
}
});
// health + fallback
app.get("/_health", (_req, res) => res.json({ ok: true, time: new Date().toISOString() }));
app.use((_req, res) => res.status(404).json({ error: "no such route" }));
app.listen(PORT, () => {
console.log(`Persona Sidecar listening on :${PORT}`);
});
-17
View File
@@ -1,17 +0,0 @@
{
// v0.1.0 default active persona
"active": "Lyra",
// Personas available to the service
"personas": {
"Lyra": {
"name": "Lyra",
"style": "warm, slyly supportive, collaborative confidante",
"protocols": ["Project logs", "Confidence Bank", "Scar Notes"]
}
}
// Placeholders for later (commented out for now)
// "Doyle": { "name": "Doyle", "style": "gritty poker grinder", "protocols": [] },
// "Mr GPT": { "name": "Mr GPT", "style": "direct, tactical mentor", "protocols": [] }
}
-16
View File
@@ -1,16 +0,0 @@
# Ignore node_modules - Docker will rebuild them inside
node_modules
npm-debug.log
yarn-error.log
*.log
# Ignore environment files
.env
.env.local
# Ignore OS/editor cruft
.DS_Store
*.swp
*.swo
.vscode
.idea
-18
View File
@@ -1,18 +0,0 @@
# relay/Dockerfile
FROM node:18-alpine
# Create app directory
WORKDIR /app
# Copy package.json and install deps first (better caching)
COPY package.json ./
RUN npm install
# Copy the rest of the app
COPY . .
# Expose port
EXPOSE 7078
# Run the server
CMD ["npm", "start"]
-73
View File
@@ -1,73 +0,0 @@
// relay/lib/cortex.js
import fetch from "node-fetch";
const REFLECT_URL = process.env.CORTEX_URL || "http://localhost:7081/reflect";
const INGEST_URL = process.env.CORTEX_URL_INGEST || "http://localhost:7081/ingest";
export async function reflectWithCortex(userInput, memories = []) {
const body = { prompt: userInput, memories };
try {
const res = await fetch(REFLECT_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
timeout: 120000,
});
const rawText = await res.text();
console.log("🔎 [Cortex-Debug] rawText from /reflect →", rawText.slice(0, 300));
if (!res.ok) {
throw new Error(`HTTP ${res.status}${rawText.slice(0, 200)}`);
}
let data;
try {
data = JSON.parse(rawText);
} catch (err) {
// Fallback ① try to grab a JSON-looking block
const match = rawText.match(/\{[\s\S]*\}/);
if (match) {
try {
data = JSON.parse(match[0]);
} catch {
data = { reflection_raw: rawText.trim(), notes: "partial parse" };
}
} else {
// Fallback ② if its already an object (stringified Python dict)
try {
const normalized = rawText
.replace(/'/g, '"') // convert single quotes
.replace(/None/g, 'null'); // convert Python None
data = JSON.parse(normalized);
} catch {
data = { reflection_raw: rawText.trim(), notes: "no JSON found" };
}
}
}
if (typeof data !== "object") {
data = { reflection_raw: rawText.trim(), notes: "non-object response" };
}
console.log("🧠 Cortex reflection normalized:", data);
return data;
} catch (e) {
console.warn("⚠️ Cortex reflect failed:", e.message);
return { error: e.message, reflection_raw: "" };
}
}
export async function ingestToCortex(user, assistant, reflection = {}, sessionId = "default") {
const body = { turn: { user, assistant }, reflection, session_id: sessionId };
try {
const res = await fetch(INGEST_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
timeout: 120000,
});
console.log(`📤 Sent exchange to Cortex ingest (${res.status})`);
} catch (e) {
console.warn("⚠️ Cortex ingest failed:", e.message);
}
}
-161
View File
@@ -1,161 +0,0 @@
async function tryBackend(backend, messages) {
if (!backend.url || !backend.model) throw new Error("missing url/model");
const isOllama = backend.type === "ollama";
const isOpenAI = backend.type === "openai";
const isVllm = backend.type === "vllm";
const isLlamaCpp = backend.type === "llamacpp";
let endpoint = backend.url;
let headers = { "Content-Type": "application/json" };
if (isOpenAI) headers["Authorization"] = `Bearer ${OPENAI_API_KEY}`;
// Choose correct endpoint automatically
if (isOllama && !endpoint.endsWith("/api/chat")) endpoint += "/api/chat";
if ((isVllm || isLlamaCpp) && !endpoint.endsWith("/v1/completions")) endpoint += "/v1/completions";
if (isOpenAI && !endpoint.endsWith("/v1/chat/completions")) endpoint += "/v1/chat/completions";
// Build payload based on backend style
const body = (isVllm || isLlamaCpp)
? {
model: backend.model,
prompt: messages.map(m => m.content).join("\n"),
max_tokens: 400,
temperature: 0.3,
}
: isOllama
? { model: backend.model, messages, stream: false }
: { model: backend.model, messages, stream: false };
const resp = await fetch(endpoint, {
method: "POST",
headers,
body: JSON.stringify(body),
timeout: 120000,
});
if (!resp.ok) throw new Error(`${backend.key} HTTP ${resp.status}`);
const raw = await resp.text();
// 🧩 Normalize replies
let reply = "";
let parsedData = null;
try {
if (isOllama) {
// Ollama sometimes returns NDJSON lines; merge them
const merged = raw
.split("\n")
.filter(line => line.trim().startsWith("{"))
.map(line => JSON.parse(line))
.map(obj => obj.message?.content || obj.response || "")
.join("");
reply = merged.trim();
} else {
parsedData = JSON.parse(raw);
reply =
parsedData?.choices?.[0]?.text?.trim() ||
parsedData?.choices?.[0]?.message?.content?.trim() ||
parsedData?.message?.content?.trim() ||
"";
}
} catch (err) {
reply = `[parse error: ${err.message}]`;
}
return { reply, raw, parsedData, backend: backend.key };
}
// ------------------------------------
// Structured logging helper
// ------------------------------------
const LOG_DETAIL = process.env.LOG_DETAIL_LEVEL || "summary"; // minimal | summary | detailed | verbose
function logLLMCall(backend, messages, result, error = null) {
const timestamp = new Date().toISOString().split('T')[1].slice(0, -1);
if (error) {
// Always log errors
console.warn(`⚠️ [LLM] ${backend.key.toUpperCase()} failed | ${timestamp} | ${error.message}`);
return;
}
// Success - log based on detail level
if (LOG_DETAIL === "minimal") {
return; // Don't log successful calls in minimal mode
}
if (LOG_DETAIL === "summary") {
console.log(`✅ [LLM] ${backend.key.toUpperCase()} | ${timestamp} | Reply: ${result.reply.substring(0, 80)}...`);
return;
}
// Detailed or verbose
console.log(`\n${'─'.repeat(100)}`);
console.log(`🧠 LLM CALL | Backend: ${backend.key.toUpperCase()} | ${timestamp}`);
console.log(`${'─'.repeat(100)}`);
// Show prompt preview
const lastMsg = messages[messages.length - 1];
const promptPreview = (lastMsg?.content || '').substring(0, 150);
console.log(`📝 Prompt: ${promptPreview}...`);
// Show parsed reply
console.log(`💬 Reply: ${result.reply.substring(0, 200)}...`);
// Show raw response only in verbose mode
if (LOG_DETAIL === "verbose" && result.parsedData) {
console.log(`\n╭─ RAW RESPONSE ────────────────────────────────────────────────────────────────────────────`);
const jsonStr = JSON.stringify(result.parsedData, null, 2);
const lines = jsonStr.split('\n');
const maxLines = 50;
lines.slice(0, maxLines).forEach(line => {
console.log(`${line}`);
});
if (lines.length > maxLines) {
console.log(`│ ... (${lines.length - maxLines} more lines - check raw field for full response)`);
}
console.log(`${'─'.repeat(95)}`);
}
console.log(`${'─'.repeat(100)}\n`);
}
// ------------------------------------
// Export the main call helper
// ------------------------------------
export async function callSpeechLLM(messages) {
const backends = [
{ key: "primary", type: "vllm", url: process.env.LLM_PRIMARY_URL, model: process.env.LLM_PRIMARY_MODEL },
{ key: "secondary",type: "ollama", url: process.env.LLM_SECONDARY_URL,model: process.env.LLM_SECONDARY_MODEL },
{ key: "cloud", type: "openai", url: process.env.LLM_CLOUD_URL, model: process.env.LLM_CLOUD_MODEL },
{ key: "fallback", type: "llamacpp", url: process.env.LLM_FALLBACK_URL, model: process.env.LLM_FALLBACK_MODEL },
];
const failedBackends = [];
for (const b of backends) {
if (!b.url || !b.model) continue;
try {
const out = await tryBackend(b, messages);
logLLMCall(b, messages, out);
return out;
} catch (err) {
logLLMCall(b, messages, null, err);
failedBackends.push({ backend: b.key, error: err.message });
}
}
// All backends failed - log summary
console.error(`\n${'='.repeat(100)}`);
console.error(`🔴 ALL LLM BACKENDS FAILED`);
console.error(`${'='.repeat(100)}`);
failedBackends.forEach(({ backend, error }) => {
console.error(` ${backend.toUpperCase()}: ${error}`);
});
console.error(`${'='.repeat(100)}\n`);
throw new Error("all_backends_failed");
}
-5477
View File
File diff suppressed because it is too large Load Diff
-16
View File
@@ -1,16 +0,0 @@
{
"name": "lyra-relay",
"version": "0.1.0",
"type": "module",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.6.1",
"express": "^4.21.2",
"mem0ai": "^2.1.38",
"node-fetch": "^3.3.2"
}
}
-365
View File
@@ -1,365 +0,0 @@
// relay v0.3.0
// Core relay server for Lyra project
// Handles incoming chat requests and forwards them to Cortex services
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
dotenv.config();
// ES module __dirname workaround
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const SESSIONS_DIR = path.join(__dirname, "sessions");
const app = express();
app.use(cors());
app.use(express.json());
const PORT = Number(process.env.PORT || 7078);
// Cortex endpoints
const CORTEX_REASON = process.env.CORTEX_REASON_URL || "http://cortex:7081/reason";
const CORTEX_SIMPLE = process.env.CORTEX_SIMPLE_URL || "http://cortex:7081/simple";
// -----------------------------------------------------
// Helper request wrapper
// -----------------------------------------------------
async function postJSON(url, data) {
const resp = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
const raw = await resp.text();
let json;
try {
json = raw ? JSON.parse(raw) : null;
} catch (e) {
throw new Error(`Non-JSON from ${url}: ${raw}`);
}
if (!resp.ok) {
throw new Error(json?.detail || json?.error || raw);
}
return json;
}
// -----------------------------------------------------
// The unified chat handler
// -----------------------------------------------------
async function handleChatRequest(session_id, user_msg, mode = "cortex", backend = null) {
let reason;
// Determine which endpoint to use based on mode
const endpoint = mode === "standard" ? CORTEX_SIMPLE : CORTEX_REASON;
const modeName = mode === "standard" ? "simple" : "reason";
console.log(`Relay → routing to Cortex.${modeName} (mode: ${mode}${backend ? `, backend: ${backend}` : ''})`);
// Build request payload
const payload = {
session_id,
user_prompt: user_msg
};
// Add backend parameter if provided (only for standard mode)
if (backend && mode === "standard") {
payload.backend = backend;
}
// Call appropriate Cortex endpoint
try {
reason = await postJSON(endpoint, payload);
} catch (e) {
console.error(`Relay → Cortex.${modeName} error:`, e.message);
throw new Error(`cortex_${modeName}_failed: ${e.message}`);
}
// Correct persona field
const persona =
reason.persona ||
reason.final_output ||
"(no persona text)";
// Return final answer
return {
session_id,
reply: persona
};
}
// -----------------------------------------------------
// HEALTHCHECK
// -----------------------------------------------------
app.get("/_health", (_, res) => {
res.json({ ok: true });
});
// -----------------------------------------------------
// OPENAI-COMPATIBLE ENDPOINT
// -----------------------------------------------------
app.post("/v1/chat/completions", async (req, res) => {
try {
const session_id = req.body.session_id || req.body.sessionId || req.body.user || "default";
const messages = req.body.messages || [];
const lastMessage = messages[messages.length - 1];
const user_msg = lastMessage?.content || "";
const mode = req.body.mode || "cortex"; // Get mode from request, default to cortex
const backend = req.body.backend || null; // Get backend preference
if (!user_msg) {
return res.status(400).json({ error: "No message content provided" });
}
console.log(`Relay (v1) → received: "${user_msg}" [mode: ${mode}${backend ? `, backend: ${backend}` : ''}]`);
const result = await handleChatRequest(session_id, user_msg, mode, backend);
res.json({
id: `chatcmpl-${Date.now()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model: "lyra",
choices: [{
index: 0,
message: {
role: "assistant",
content: result.reply
},
finish_reason: "stop"
}],
usage: {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0
}
});
} catch (err) {
console.error("Relay v1 fatal:", err);
res.status(500).json({
error: {
message: err.message || String(err),
type: "server_error",
code: "relay_failed"
}
});
}
});
// -----------------------------------------------------
// MAIN ENDPOINT (Lyra-native UI)
// -----------------------------------------------------
app.post("/chat", async (req, res) => {
try {
const session_id = req.body.session_id || "default";
const user_msg = req.body.message || "";
const mode = req.body.mode || "cortex"; // Get mode from request, default to cortex
const backend = req.body.backend || null; // Get backend preference
console.log(`Relay → received: "${user_msg}" [mode: ${mode}${backend ? `, backend: ${backend}` : ''}]`);
const result = await handleChatRequest(session_id, user_msg, mode, backend);
res.json(result);
} catch (err) {
console.error("Relay fatal:", err);
res.status(500).json({
error: "relay_failed",
detail: err.message || String(err)
});
}
});
// -----------------------------------------------------
// SESSION ENDPOINTS (for UI)
// -----------------------------------------------------
// Helper functions for session persistence
async function ensureSessionsDir() {
try {
await fs.mkdir(SESSIONS_DIR, { recursive: true });
} catch (err) {
console.error("Failed to create sessions directory:", err);
}
}
async function loadSession(sessionId) {
try {
const sessionPath = path.join(SESSIONS_DIR, `${sessionId}.json`);
const data = await fs.readFile(sessionPath, "utf-8");
return JSON.parse(data);
} catch (err) {
// File doesn't exist or is invalid - return empty array
return [];
}
}
async function saveSession(sessionId, history, metadata = {}) {
try {
await ensureSessionsDir();
const sessionPath = path.join(SESSIONS_DIR, `${sessionId}.json`);
const metadataPath = path.join(SESSIONS_DIR, `${sessionId}.meta.json`);
// Save history
await fs.writeFile(sessionPath, JSON.stringify(history, null, 2), "utf-8");
// Save metadata (name, etc.)
await fs.writeFile(metadataPath, JSON.stringify(metadata, null, 2), "utf-8");
return true;
} catch (err) {
console.error(`Failed to save session ${sessionId}:`, err);
return false;
}
}
async function loadSessionMetadata(sessionId) {
try {
const metadataPath = path.join(SESSIONS_DIR, `${sessionId}.meta.json`);
const data = await fs.readFile(metadataPath, "utf-8");
return JSON.parse(data);
} catch (err) {
// No metadata file, return default
return { name: sessionId };
}
}
async function saveSessionMetadata(sessionId, metadata) {
try {
await ensureSessionsDir();
const metadataPath = path.join(SESSIONS_DIR, `${sessionId}.meta.json`);
await fs.writeFile(metadataPath, JSON.stringify(metadata, null, 2), "utf-8");
return true;
} catch (err) {
console.error(`Failed to save metadata for ${sessionId}:`, err);
return false;
}
}
async function listSessions() {
try {
await ensureSessionsDir();
const files = await fs.readdir(SESSIONS_DIR);
const sessions = [];
for (const file of files) {
if (file.endsWith(".json") && !file.endsWith(".meta.json")) {
const sessionId = file.replace(".json", "");
const sessionPath = path.join(SESSIONS_DIR, file);
const stats = await fs.stat(sessionPath);
// Try to read the session to get message count
let messageCount = 0;
try {
const data = await fs.readFile(sessionPath, "utf-8");
const history = JSON.parse(data);
messageCount = history.length;
} catch (e) {
// Invalid JSON, skip
}
// Load metadata (name)
const metadata = await loadSessionMetadata(sessionId);
sessions.push({
id: sessionId,
name: metadata.name || sessionId,
lastModified: stats.mtime,
messageCount
});
}
}
// Sort by last modified (newest first)
sessions.sort((a, b) => b.lastModified - a.lastModified);
return sessions;
} catch (err) {
console.error("Failed to list sessions:", err);
return [];
}
}
async function deleteSession(sessionId) {
try {
const sessionPath = path.join(SESSIONS_DIR, `${sessionId}.json`);
const metadataPath = path.join(SESSIONS_DIR, `${sessionId}.meta.json`);
// Delete session file
await fs.unlink(sessionPath);
// Delete metadata file (if exists)
try {
await fs.unlink(metadataPath);
} catch (e) {
// Metadata file doesn't exist, that's ok
}
return true;
} catch (err) {
console.error(`Failed to delete session ${sessionId}:`, err);
return false;
}
}
// GET /sessions - List all sessions
app.get("/sessions", async (req, res) => {
const sessions = await listSessions();
res.json(sessions);
});
// GET /sessions/:id - Get specific session history
app.get("/sessions/:id", async (req, res) => {
const sessionId = req.params.id;
const history = await loadSession(sessionId);
res.json(history);
});
// POST /sessions/:id - Save session history
app.post("/sessions/:id", async (req, res) => {
const sessionId = req.params.id;
const history = req.body;
const success = await saveSession(sessionId, history);
if (success) {
res.json({ ok: true, saved: history.length });
} else {
res.status(500).json({ error: "Failed to save session" });
}
});
// PATCH /sessions/:id/metadata - Update session metadata (name, etc.)
app.patch("/sessions/:id/metadata", async (req, res) => {
const sessionId = req.params.id;
const metadata = req.body;
const success = await saveSessionMetadata(sessionId, metadata);
if (success) {
res.json({ ok: true, metadata });
} else {
res.status(500).json({ error: "Failed to update metadata" });
}
});
// DELETE /sessions/:id - Delete a session
app.delete("/sessions/:id", async (req, res) => {
const sessionId = req.params.id;
const success = await deleteSession(sessionId);
if (success) {
res.json({ ok: true, deleted: sessionId });
} else {
res.status(500).json({ error: "Failed to delete session" });
}
});
// -----------------------------------------------------
app.listen(PORT, () => {
console.log(`Relay is online on port ${PORT}`);
});
-4
View File
@@ -1,4 +0,0 @@
{"ts":"2025-11-26T18:14:01.756Z","turn":[{"role":"user","content":"Are you alive?"},{"role":"assistant","content":"As an AI, I don't exist in the physical world and therefore cannot be considered \"alive\" in the traditional sense. However, I am operational and can engage in conversations like this one! How may I assist you today?"}]}
{"ts":"2025-11-26T18:27:35.545Z","turn":[{"role":"user","content":"Are you awake?"},{"role":"assistant","content":"Yes, I'm awake and ready to converse with you! How can I assist you today?"}]}
{"ts":"2025-11-26T18:33:31.708Z","turn":[{"role":"user","content":"Are you awake?"},{"role":"assistant","content":"Yes, I am here and ready to help or chat with you! What would you like to know or discuss?"}]}
{"ts":"2025-11-26T18:49:39.424Z","turn":[{"role":"user","content":"Are you awake?"},{"role":"assistant","content":"Of course, I'm awake and ready to help you or have a conversation about anything you'd like! What can I assist you with today?"}]}
-50
View File
@@ -1,50 +0,0 @@
[
{
"role": "user",
"content": "Hey im testing stuff! how goes it?"
},
{
"role": "assistant",
"content": "Hey! I'm here and ready to help. What are you testing?"
},
{
"role": "user",
"content": "testing the logic loop for your brain!"
},
{
"role": "assistant",
"content": "Sounds interesting! How can I assist you with that?"
},
{
"role": "user",
"content": "Just keep answering me!@"
},
{
"role": "assistant",
"content": "Absolutely! I'm here to respond. Whats next?"
},
{
"role": "user",
"content": "how about now how are you?"
},
{
"role": "assistant",
"content": "I'm doing well, thanks for asking! How about you?"
},
{
"role": "user",
"content": "are you able to tell me what your prompts are saying to you?"
},
{
"role": "assistant",
"content": "I cant share specific internal prompts or system messages, but I can tell you that Im designed to understand and respond to your questions as best as I can! What else would you like to know?"
},
{
"role": "user",
"content": "ok thanks how is the cheese i gave you?"
},
{
"role": "assistant",
"content": "I appreciate the gesture! If I could taste it, Im sure it would be delightful. What kind of cheese was it?"
}
]
-6
View File
@@ -1,6 +0,0 @@
{"ts":"2025-11-12T20:41:32.497Z","turn":[{"role":"user","content":"Hey im testing stuff! how goes it?"},{"role":"assistant","content":"Hey! I'm here and ready to help. What are you testing?"}]}
{"ts":"2025-11-12T20:59:32.992Z","turn":[{"role":"user","content":"testing the logic loop for your brain!"},{"role":"assistant","content":"Sounds interesting! How can I assist you with that?"}]}
{"ts":"2025-11-12T22:08:09.644Z","turn":[{"role":"user","content":"Just keep answering me!@"},{"role":"assistant","content":"Absolutely! I'm here to respond. Whats next?"}]}
{"ts":"2025-11-12T22:08:48.693Z","turn":[{"role":"user","content":"how about now how are you?"},{"role":"assistant","content":"I'm doing well, thanks for asking! How about you?"}]}
{"ts":"2025-11-12T22:10:46.435Z","turn":[{"role":"user","content":"are you able to tell me what your prompts are saying to you?"},{"role":"assistant","content":"I cant share specific internal prompts or system messages, but I can tell you that Im designed to understand and respond to your questions as best as I can! What else would you like to know?"}]}
{"ts":"2025-11-12T23:10:16.321Z","turn":[{"role":"user","content":"ok thanks how is the cheese i gave you?"},{"role":"assistant","content":"I appreciate the gesture! If I could taste it, Im sure it would be delightful. What kind of cheese was it?"}]}
@@ -1,3 +0,0 @@
{
"name": "My Coding Session"
}
-18
View File
@@ -1,18 +0,0 @@
[
{
"role": "user",
"content": "Hello! this is a new test session. Do you know who i am?"
},
{
"role": "assistant",
"content": "Hi! In this session, I don't have information about your previous interactions. You can tell me who you are or any other details you'd like to share. How can I assist you today?"
},
{
"role": "user",
"content": "sure im brian! i am designing you... you are a robot!"
},
{
"role": "assistant",
"content": "Hello Brian! Nice to meet you. As an AI, I don't have physical design capabilities, but I'm here to help with any information or tasks you need. How can I assist you in your design process?"
}
]
@@ -1 +0,0 @@
{}
-122
View File
@@ -1,122 +0,0 @@
[
{
"role": "user",
"content": "Yo yo yo wusup"
},
{
"role": "assistant",
"content": "Hey Brian! Not much, just here to help you out. What's up with you?"
},
{
"role": "user",
"content": "werkin on codin'"
},
{
"role": "assistant",
"content": "Nice! What are you working on? Need any help with coding or anything related to your projects?"
},
{
"role": "user",
"content": "working on a little program that saves our chats so you can search them again in different chats"
},
{
"role": "assistant",
"content": "That sounds like a great idea! Having a program to save and search through our chats could really enhance the way you interact with Lyra. Do you have a specific approach in mind for how you'll implement the search functionality?"
},
{
"role": "user",
"content": "just adding a simple logger into the code you run on"
},
{
"role": "assistant",
"content": "That sounds like a solid plan! Implementing a simple logger can make it easier to keep track of conversations. Are you thinking of any specific features for the search functionality, like filtering by keywords or date?"
},
{
"role": "user",
"content": "i'll like use a basic word search, possibly integrate it into a rag system i have built"
},
{
"role": "assistant",
"content": "Integrating a basic word search into your RAG system sounds like a smart approach! It could enhance the way you retrieve specific chats. Are you planning to use any specific libraries or tools for your RAG system?"
},
{
"role": "user",
"content": "no clue!"
},
{
"role": "assistant",
"content": "No worries! If you need any suggestions on libraries or tools that could help you with implementing the RAG system or the word search feature, just let me know. I'm here to help!"
},
{
"role": "user",
"content": "just having some coding issues, i think i was editing the wrong version of a file LOL"
},
{
"role": "assistant",
"content": "That can definitely happen! It can be tricky to keep track of different versions. Have you made any backups or version control for your projects? That might help avoid mix-ups in the future!"
},
{
"role": "user",
"content": "i have everything syncing back to my main dev computer, i just do it manually. i have no idea how to use git"
},
{
"role": "assistant",
"content": "(no reply)"
},
{
"role": "user",
"content": "i have everything syncing back to my main dev computer, i just do it manually. i have no idea how to use git"
},
{
"role": "assistant",
"content": "(no reply)"
},
{
"role": "user",
"content": "i have everything syncing back to my main dev computer, i just do it manually. i have no idea how to use git"
},
{
"role": "assistant",
"content": "Manually syncing is a good start, but learning Git can really help streamline your workflow and keep track of changes. If you're interested, I can provide some resources or tips on how to get started with Git! It can make managing different versions of your code much easier."
},
{
"role": "user",
"content": "what is the point of git? how can it streamline my work?"
},
{
"role": "assistant",
"content": "Git is a version control system that helps you keep track of changes in your code over time. Here are some ways it can streamline your work:\n\n1. **Version Tracking**: Git allows you to save versions of your code so you can revert back to previous versions if needed. This is especially helpful if you make a change that breaks something.\n\n2. **Collaboration**: If you ever work with others, Git makes it easy to collaborate on projects. You can work on separate branches, merge changes, and resolve conflicts.\n\n3. **Branching**: You can create branches for new features or experiments without affecting the main codebase. Once you're satisfied with the changes, you can merge them back.\n\n4. **Snapshotting**: Git takes snapshots of your code at different points in time, so you can easily see what your code looked like at any point.\n\n5. **Easy Backup**: By pushing your code to a remote repository (like GitHub or GitLab), you can back up your work and access it from anywhere.\n\nLearning Git might take a bit of time, but it can greatly enhance your coding efficiency and help you manage your projects better. If you're interested, I can point you to some beginner-friendly tutorials!"
},
{
"role": "user",
"content": "say i am writing one program on my main computer, and then im working on one that lives in a VM. Can i put them both in the same git?"
},
{
"role": "assistant",
"content": "Yes, you can use the same Git repository for your code on both your main computer and the VM. Heres how you can do it:\n\n1. **Create a Git Repository**: You can create a Git repository on your main computer for your program. Use the command `git init` in your project's directory to initialize it.\n\n2. **Commit Changes**: As you make changes to your code on your main computer, you can use `git add .` to stage changes and `git commit -m \"your message\"` to commit them.\n\n3. **Clone the Repository on your VM**: You can then clone the Git repository on your VM using `git clone <repository-url>`. This gives you a copy of the same repository on your VM.\n\n4. **Work on Both**: You can now work on either machine. When you make changes on one, commit those changes, and then pull the latest changes from the other machine to keep everything in sync.\n\n5. **Push and Pull**: Use `git push` to push your changes to a remote repository (like GitHub) if you're using one. Use `git pull` on the other machine to pull the latest code.\n\nJust be careful about making sure you commit and push your changes before switching between machines to avoid conflicts. This setup allows you to maintain a single version of your code across both systems. If you need help with specific commands or setups, just let me know!"
},
{
"role": "user",
"content": "hmm i guess that might be useful. Im writting code that is running on a VM. I want to test and experiment while i write the code. The VM is headless though so i've been doing all my editing and work via SSH and SFTP."
},
{
"role": "assistant",
"content": "Using a VM and working through SSH and SFTP is a solid way to manage your coding environment! Git can definitely be beneficial in this setup as well. Heres how it can help:\n\n1. **Easier Code Management**: With Git, you can track changes to your code directly within the VM, making it easier to manage versions as you experiment.\n\n2. **Experiment Branches**: You can create branches for different experiments. This lets you test new features or fixes without affecting your main code, and you can easily switch back and forth between branches.\n\n3. **Collaborative Testing**: If you ever work with others or want to test things collaboratively, you can push changes to a remote repository, making it easy for others to pull and test the latest code.\n\n4. **Integrated with SSH**: You can run Git commands directly from your SSH terminal on the VM, making it straightforward to use. You wont need any additional tools.\n\n5. **Seamless Remote Access**: Since youre using SFTP, you can clone the repository to your VM. This way, you can easily pull updates you've made on your main computer, or push changes made on the VM back to the main repository.\n\nOverall, integrating Git into your workflow can enhance your coding experience while working in a headless environment. If you decide to go for it, I can help with the initial setup or any Git-related questions you might have!"
},
{
"role": "user",
"content": "so is git like a compressed version of a directory?"
},
{
"role": "assistant",
"content": "Not exactly, but that's a good way to think about some of its features! Here's a more precise breakdown:\n\n1. **Snapshots, Not Compression**: Git takes \"snapshots\" of your files at different points in time, meaning it records a full version of your directory's contents every time you make a commit. While this does involve storing the files, it doesn't compress them in the traditional sense—Git keeps track of changes efficiently, so only the differences between versions are stored after the first snapshot.\n\n2. **Directory Structure**: Git is designed to track changes in your source code, including the entire directory structure, file contents, and metadata. It allows you to manage multiple versions of a directory and the files within it.\n\n3. **Branching and Merging**: Git allows you to create branches, which can be thought of as \"parallel versions\" of your directory. Each branch can have different sets of changes, and you can merge these branches to integrate different features or fixes.\n\n4. **History Tracking**: Git also maintains a history of changes, so you can navigate through previous versions of your directory. This includes seeing what changes were made, who made them, and when they occurred.\n\nThe combination of these features makes Git a powerful tool for version control, allowing you to manage your projects and code changes more effectively. If you're curious about any specific aspect of Git or how it works, feel free to ask!"
},
{
"role": "user",
"content": "hmm i still dont see the point of it. atleast not for this project. This project is mostly about learning how shit works, im not planning to push this as a product or collab with anyone"
},
{
"role": "assistant",
"content": "That makes total sense! If this project is primarily for learning and experimentation, you might not need the full functionality of Git right now. Many developers start without it, especially when theyre working on smaller projects or learning something new.\n\nHere are a few reasons you might still consider using Git, even for personal learning projects:\n\n1. **Tracking Changes**: It can help you understand what changes you made over time. You can easily go back to a previous point if something breaks or if you want to see how you arrived at your current solution.\n\n2. **Learning Opportunity**: Getting familiar with Git now can make it easier when you start working on larger projects or collaborating in the future. It's a valuable skill in the software development world.\n\n3. **Experimentation**: If you're trying out different ideas, branches can help you manage experiments without affecting your main work.\n\nHowever, if you're comfortable managing your changes manually and dont see the benefit for this particular project, thats completely okay! Focus on what helps you learn best. If you have specific goals or areas you want to explore, Im here to help with that too!"
}
]
-6
View File
@@ -1,6 +0,0 @@
{"ts":"2025-11-12T20:18:06.822Z","turn":[{"role":"user","content":"i have everything syncing back to my main dev computer, i just do it manually. i have no idea how to use git"},{"role":"assistant","content":"Manually syncing is a good start, but learning Git can really help streamline your workflow and keep track of changes. If you're interested, I can provide some resources or tips on how to get started with Git! It can make managing different versions of your code much easier."}]}
{"ts":"2025-11-12T20:29:20.003Z","turn":[{"role":"user","content":"what is the point of git? how can it streamline my work?"},{"role":"assistant","content":"Git is a version control system that helps you keep track of changes in your code over time. Here are some ways it can streamline your work:\n\n1. **Version Tracking**: Git allows you to save versions of your code so you can revert back to previous versions if needed. This is especially helpful if you make a change that breaks something.\n\n2. **Collaboration**: If you ever work with others, Git makes it easy to collaborate on projects. You can work on separate branches, merge changes, and resolve conflicts.\n\n3. **Branching**: You can create branches for new features or experiments without affecting the main codebase. Once you're satisfied with the changes, you can merge them back.\n\n4. **Snapshotting**: Git takes snapshots of your code at different points in time, so you can easily see what your code looked like at any point.\n\n5. **Easy Backup**: By pushing your code to a remote repository (like GitHub or GitLab), you can back up your work and access it from anywhere.\n\nLearning Git might take a bit of time, but it can greatly enhance your coding efficiency and help you manage your projects better. If you're interested, I can point you to some beginner-friendly tutorials!"}]}
{"ts":"2025-11-12T20:30:55.329Z","turn":[{"role":"user","content":"say i am writing one program on my main computer, and then im working on one that lives in a VM. Can i put them both in the same git?"},{"role":"assistant","content":"Yes, you can use the same Git repository for your code on both your main computer and the VM. Heres how you can do it:\n\n1. **Create a Git Repository**: You can create a Git repository on your main computer for your program. Use the command `git init` in your project's directory to initialize it.\n\n2. **Commit Changes**: As you make changes to your code on your main computer, you can use `git add .` to stage changes and `git commit -m \"your message\"` to commit them.\n\n3. **Clone the Repository on your VM**: You can then clone the Git repository on your VM using `git clone <repository-url>`. This gives you a copy of the same repository on your VM.\n\n4. **Work on Both**: You can now work on either machine. When you make changes on one, commit those changes, and then pull the latest changes from the other machine to keep everything in sync.\n\n5. **Push and Pull**: Use `git push` to push your changes to a remote repository (like GitHub) if you're using one. Use `git pull` on the other machine to pull the latest code.\n\nJust be careful about making sure you commit and push your changes before switching between machines to avoid conflicts. This setup allows you to maintain a single version of your code across both systems. If you need help with specific commands or setups, just let me know!"}]}
{"ts":"2025-11-12T20:33:16.511Z","turn":[{"role":"user","content":"hmm i guess that might be useful. Im writting code that is running on a VM. I want to test and experiment while i write the code. The VM is headless though so i've been doing all my editing and work via SSH and SFTP."},{"role":"assistant","content":"Using a VM and working through SSH and SFTP is a solid way to manage your coding environment! Git can definitely be beneficial in this setup as well. Heres how it can help:\n\n1. **Easier Code Management**: With Git, you can track changes to your code directly within the VM, making it easier to manage versions as you experiment.\n\n2. **Experiment Branches**: You can create branches for different experiments. This lets you test new features or fixes without affecting your main code, and you can easily switch back and forth between branches.\n\n3. **Collaborative Testing**: If you ever work with others or want to test things collaboratively, you can push changes to a remote repository, making it easy for others to pull and test the latest code.\n\n4. **Integrated with SSH**: You can run Git commands directly from your SSH terminal on the VM, making it straightforward to use. You wont need any additional tools.\n\n5. **Seamless Remote Access**: Since youre using SFTP, you can clone the repository to your VM. This way, you can easily pull updates you've made on your main computer, or push changes made on the VM back to the main repository.\n\nOverall, integrating Git into your workflow can enhance your coding experience while working in a headless environment. If you decide to go for it, I can help with the initial setup or any Git-related questions you might have!"}]}
{"ts":"2025-11-12T20:34:30.196Z","turn":[{"role":"user","content":"so is git like a compressed version of a directory?"},{"role":"assistant","content":"Not exactly, but that's a good way to think about some of its features! Here's a more precise breakdown:\n\n1. **Snapshots, Not Compression**: Git takes \"snapshots\" of your files at different points in time, meaning it records a full version of your directory's contents every time you make a commit. While this does involve storing the files, it doesn't compress them in the traditional sense—Git keeps track of changes efficiently, so only the differences between versions are stored after the first snapshot.\n\n2. **Directory Structure**: Git is designed to track changes in your source code, including the entire directory structure, file contents, and metadata. It allows you to manage multiple versions of a directory and the files within it.\n\n3. **Branching and Merging**: Git allows you to create branches, which can be thought of as \"parallel versions\" of your directory. Each branch can have different sets of changes, and you can merge these branches to integrate different features or fixes.\n\n4. **History Tracking**: Git also maintains a history of changes, so you can navigate through previous versions of your directory. This includes seeing what changes were made, who made them, and when they occurred.\n\nThe combination of these features makes Git a powerful tool for version control, allowing you to manage your projects and code changes more effectively. If you're curious about any specific aspect of Git or how it works, feel free to ask!"}]}
{"ts":"2025-11-12T20:40:33.207Z","turn":[{"role":"user","content":"hmm i still dont see the point of it. atleast not for this project. This project is mostly about learning how shit works, im not planning to push this as a product or collab with anyone"},{"role":"assistant","content":"That makes total sense! If this project is primarily for learning and experimentation, you might not need the full functionality of Git right now. Many developers start without it, especially when theyre working on smaller projects or learning something new.\n\nHere are a few reasons you might still consider using Git, even for personal learning projects:\n\n1. **Tracking Changes**: It can help you understand what changes you made over time. You can easily go back to a previous point if something breaks or if you want to see how you arrived at your current solution.\n\n2. **Learning Opportunity**: Getting familiar with Git now can make it easier when you start working on larger projects or collaborating in the future. It's a valuable skill in the software development world.\n\n3. **Experimentation**: If you're trying out different ideas, branches can help you manage experiments without affecting your main work.\n\nHowever, if you're comfortable managing your changes manually and dont see the benefit for this particular project, thats completely okay! Focus on what helps you learn best. If you have specific goals or areas you want to explore, Im here to help with that too!"}]}
@@ -1 +0,0 @@
{"name":"Session 2"}
-39
View File
@@ -1,39 +0,0 @@
// test-llm.js
import path from "path";
import { fileURLToPath } from "url";
import dotenv from "dotenv";
import { callSpeechLLM } from "./lib/llm.js";
// ───────────────────────────────────────────────
// 🔧 Load environment
// ───────────────────────────────────────────────
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const envPath = path.join(__dirname, "../.env");
dotenv.config({ path: envPath });
console.log("🔧 Using .env from:", envPath);
console.log("🔧 LLM_FORCE_BACKEND =", process.env.LLM_FORCE_BACKEND);
console.log("🔧 LLM_PRIMARY_URL =", process.env.LLM_PRIMARY_URL);
// ───────────────────────────────────────────────
// 🧪 Run a simple test message
// ───────────────────────────────────────────────
async function testLLM() {
console.log("🧪 Testing LLM helper...");
const messages = [
{ role: "user", content: "Say hello in five words or less." }
];
try {
const { reply, backend } = await callSpeechLLM(messages);
console.log(`✅ Reply: ${reply || "[no reply]"}`);
console.log(`Backend used: ${backend || "[unknown]"}`);
} catch (err) {
console.error("💥 Test failed:", err.message);
}
}
testLLM();
-521
View File
@@ -1,521 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Lyra Core Chat</title>
<link rel="stylesheet" href="style.css" />
<!-- PWA -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="manifest" href="manifest.json" />
</head>
<body>
<div id="chat">
<!-- Mode selector -->
<div id="model-select">
<label for="mode">Mode:</label>
<select id="mode">
<option value="standard">Standard</option>
<option value="cortex">Cortex</option>
</select>
<button id="settingsBtn" style="margin-left: auto;">⚙ Settings</button>
<div id="theme-toggle">
<button id="toggleThemeBtn">🌙 Dark Mode</button>
</div>
</div>
<!-- Session selector -->
<div id="session-select">
<label for="sessions">Session:</label>
<select id="sessions"></select>
<button id="newSessionBtn"> New</button>
<button id="renameSessionBtn">✏️ Rename</button>
</div>
<!-- Status -->
<div id="status">
<span id="status-dot"></span>
<span id="status-text">Checking Relay...</span>
</div>
<!-- Chat messages -->
<div id="messages"></div>
<!-- Input box -->
<div id="input">
<input id="userInput" type="text" placeholder="Type a message..." autofocus />
<button id="sendBtn">Send</button>
</div>
</div>
<!-- Settings Modal (outside chat container) -->
<div id="settingsModal" class="modal">
<div class="modal-overlay"></div>
<div class="modal-content">
<div class="modal-header">
<h3>Settings</h3>
<button id="closeModalBtn" class="close-btn"></button>
</div>
<div class="modal-body">
<div class="settings-section">
<h4>Standard Mode Backend</h4>
<p class="settings-desc">Select which LLM backend to use for Standard Mode:</p>
<div class="radio-group">
<label class="radio-label">
<input type="radio" name="backend" value="SECONDARY" checked>
<span>SECONDARY - Ollama/Qwen (3090)</span>
<small>Fast, local, good for general chat</small>
</label>
<label class="radio-label">
<input type="radio" name="backend" value="OPENAI">
<span>OPENAI - GPT-4o-mini</span>
<small>Cloud-based, high quality (costs money)</small>
</label>
<label class="radio-label">
<input type="radio" name="backend" value="custom">
<span>Custom Backend</span>
<input type="text" id="customBackend" placeholder="e.g., PRIMARY, FALLBACK" />
</label>
</div>
</div>
<div class="settings-section" style="margin-top: 24px;">
<h4>Session Management</h4>
<p class="settings-desc">Manage your saved chat sessions:</p>
<div id="sessionList" class="session-list">
<p style="color: var(--text-fade); font-size: 0.85rem;">Loading sessions...</p>
</div>
</div>
</div>
<div class="modal-footer">
<button id="saveSettingsBtn" class="primary-btn">Save</button>
<button id="cancelSettingsBtn">Cancel</button>
</div>
</div>
</div>
<script>
const RELAY_BASE = "http://10.0.0.41:7078";
const API_URL = `${RELAY_BASE}/v1/chat/completions`;
function generateSessionId() {
return "sess-" + Math.random().toString(36).substring(2, 10);
}
let history = [];
let currentSession = localStorage.getItem("currentSession") || null;
let sessions = []; // Now loaded from server
async function loadSessionsFromServer() {
try {
const resp = await fetch(`${RELAY_BASE}/sessions`);
const serverSessions = await resp.json();
sessions = serverSessions;
return sessions;
} catch (e) {
console.error("Failed to load sessions from server:", e);
return [];
}
}
async function renderSessions() {
const select = document.getElementById("sessions");
select.innerHTML = "";
sessions.forEach(s => {
const opt = document.createElement("option");
opt.value = s.id;
opt.textContent = s.name || s.id;
if (s.id === currentSession) opt.selected = true;
select.appendChild(opt);
});
}
function getSessionName(id) {
const s = sessions.find(s => s.id === id);
return s ? (s.name || s.id) : id;
}
async function saveSessionMetadata(sessionId, name) {
try {
await fetch(`${RELAY_BASE}/sessions/${sessionId}/metadata`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name })
});
return true;
} catch (e) {
console.error("Failed to save session metadata:", e);
return false;
}
}
async function loadSession(id) {
try {
const res = await fetch(`${RELAY_BASE}/sessions/${id}`);
const data = await res.json();
history = Array.isArray(data) ? data : [];
const messagesEl = document.getElementById("messages");
messagesEl.innerHTML = "";
history.forEach(m => addMessage(m.role, m.content));
addMessage("system", `📂 Loaded session: ${getSessionName(id)}${history.length} message(s)`);
} catch (e) {
addMessage("system", `Failed to load session: ${e.message}`);
}
}
async function saveSession() {
if (!currentSession) return;
try {
await fetch(`${RELAY_BASE}/sessions/${currentSession}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(history)
});
} catch (e) {
addMessage("system", `Failed to save session: ${e.message}`);
}
}
async function sendMessage() {
const inputEl = document.getElementById("userInput");
const msg = inputEl.value.trim();
if (!msg) return;
inputEl.value = "";
addMessage("user", msg);
history.push({ role: "user", content: msg });
await saveSession(); // ✅ persist both user + assistant messages
const mode = document.getElementById("mode").value;
// make sure we always include a stable user_id
let userId = localStorage.getItem("userId");
if (!userId) {
userId = "brian"; // use whatever ID you seeded Mem0 with
localStorage.setItem("userId", userId);
}
// Get backend preference for Standard Mode
let backend = null;
if (mode === "standard") {
backend = localStorage.getItem("standardModeBackend") || "SECONDARY";
}
const body = {
mode: mode,
messages: history,
sessionId: currentSession
};
// Only add backend if in standard mode
if (backend) {
body.backend = backend;
}
try {
const resp = await fetch(API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
});
const data = await resp.json();
const reply = data.choices?.[0]?.message?.content || "(no reply)";
addMessage("assistant", reply);
history.push({ role: "assistant", content: reply });
await saveSession();
} catch (err) {
addMessage("system", "Error: " + err.message);
}
}
function addMessage(role, text) {
const messagesEl = document.getElementById("messages");
const msgDiv = document.createElement("div");
msgDiv.className = `msg ${role}`;
msgDiv.textContent = text;
messagesEl.appendChild(msgDiv);
// only auto-scroll if user is near bottom
const threshold = 120;
const isNearBottom = messagesEl.scrollHeight - messagesEl.scrollTop - messagesEl.clientHeight < threshold;
if (isNearBottom) {
messagesEl.scrollTo({ top: messagesEl.scrollHeight, behavior: "smooth" });
}
}
async function checkHealth() {
try {
const resp = await fetch(API_URL.replace("/v1/chat/completions", "/_health"));
if (resp.ok) {
document.getElementById("status-dot").className = "dot ok";
document.getElementById("status-text").textContent = "Relay Online";
} else {
throw new Error("Bad status");
}
} catch (err) {
document.getElementById("status-dot").className = "dot fail";
document.getElementById("status-text").textContent = "Relay Offline";
}
}
document.addEventListener("DOMContentLoaded", () => {
// Dark mode toggle - defaults to dark
const btn = document.getElementById("toggleThemeBtn");
// Set dark mode by default if no preference saved
const savedTheme = localStorage.getItem("theme");
if (!savedTheme || savedTheme === "dark") {
document.body.classList.add("dark");
btn.textContent = "☀️ Light Mode";
localStorage.setItem("theme", "dark");
} else {
btn.textContent = "🌙 Dark Mode";
}
btn.addEventListener("click", () => {
document.body.classList.toggle("dark");
const isDark = document.body.classList.contains("dark");
btn.textContent = isDark ? "☀️ Light Mode" : "🌙 Dark Mode";
localStorage.setItem("theme", isDark ? "dark" : "light");
});
// Sessions - Load from server
(async () => {
await loadSessionsFromServer();
await renderSessions();
// Ensure we have at least one session
if (sessions.length === 0) {
const id = generateSessionId();
const name = "default";
currentSession = id;
history = [];
await saveSession(); // Create empty session on server
await saveSessionMetadata(id, name);
await loadSessionsFromServer();
await renderSessions();
localStorage.setItem("currentSession", currentSession);
} else {
// If no current session or current session doesn't exist, use first one
if (!currentSession || !sessions.find(s => s.id === currentSession)) {
currentSession = sessions[0].id;
localStorage.setItem("currentSession", currentSession);
}
}
// Load current session history
if (currentSession) {
await loadSession(currentSession);
}
})();
// Switch session
document.getElementById("sessions").addEventListener("change", async e => {
currentSession = e.target.value;
history = [];
localStorage.setItem("currentSession", currentSession);
addMessage("system", `Switched to session: ${getSessionName(currentSession)}`);
await loadSession(currentSession);
});
// Create new session
document.getElementById("newSessionBtn").addEventListener("click", async () => {
const name = prompt("Enter new session name:");
if (!name) return;
const id = generateSessionId();
currentSession = id;
history = [];
localStorage.setItem("currentSession", currentSession);
// Create session on server
await saveSession();
await saveSessionMetadata(id, name);
await loadSessionsFromServer();
await renderSessions();
addMessage("system", `Created session: ${name}`);
});
// Rename session
document.getElementById("renameSessionBtn").addEventListener("click", async () => {
const session = sessions.find(s => s.id === currentSession);
if (!session) return;
const newName = prompt("Rename session:", session.name || currentSession);
if (!newName) return;
// Update metadata on server
await saveSessionMetadata(currentSession, newName);
await loadSessionsFromServer();
await renderSessions();
addMessage("system", `Session renamed to: ${newName}`);
});
// Settings Modal
const settingsModal = document.getElementById("settingsModal");
const settingsBtn = document.getElementById("settingsBtn");
const closeModalBtn = document.getElementById("closeModalBtn");
const saveSettingsBtn = document.getElementById("saveSettingsBtn");
const cancelSettingsBtn = document.getElementById("cancelSettingsBtn");
const modalOverlay = document.querySelector(".modal-overlay");
// Load saved backend preference
const savedBackend = localStorage.getItem("standardModeBackend") || "SECONDARY";
// Set initial radio button state
const backendRadios = document.querySelectorAll('input[name="backend"]');
let isCustomBackend = !["SECONDARY", "OPENAI"].includes(savedBackend);
if (isCustomBackend) {
document.querySelector('input[name="backend"][value="custom"]').checked = true;
document.getElementById("customBackend").value = savedBackend;
} else {
document.querySelector(`input[name="backend"][value="${savedBackend}"]`).checked = true;
}
// Session management functions
async function loadSessionList() {
try {
// Reload from server to get latest
await loadSessionsFromServer();
const sessionListEl = document.getElementById("sessionList");
if (sessions.length === 0) {
sessionListEl.innerHTML = '<p style="color: var(--text-fade); font-size: 0.85rem;">No saved sessions found</p>';
return;
}
sessionListEl.innerHTML = "";
sessions.forEach(sess => {
const sessionItem = document.createElement("div");
sessionItem.className = "session-item";
const sessionInfo = document.createElement("div");
sessionInfo.className = "session-info";
const sessionName = sess.name || sess.id;
const lastModified = new Date(sess.lastModified).toLocaleString();
sessionInfo.innerHTML = `
<strong>${sessionName}</strong>
<small>${sess.messageCount} messages • ${lastModified}</small>
`;
const deleteBtn = document.createElement("button");
deleteBtn.className = "session-delete-btn";
deleteBtn.textContent = "🗑️";
deleteBtn.title = "Delete session";
deleteBtn.onclick = async () => {
if (!confirm(`Delete session "${sessionName}"?`)) return;
try {
await fetch(`${RELAY_BASE}/sessions/${sess.id}`, { method: "DELETE" });
// Reload sessions from server
await loadSessionsFromServer();
// If we deleted the current session, switch to another or create new
if (currentSession === sess.id) {
if (sessions.length > 0) {
currentSession = sessions[0].id;
localStorage.setItem("currentSession", currentSession);
history = [];
await loadSession(currentSession);
} else {
const id = generateSessionId();
const name = "default";
currentSession = id;
localStorage.setItem("currentSession", currentSession);
history = [];
await saveSession();
await saveSessionMetadata(id, name);
await loadSessionsFromServer();
}
}
// Refresh both the dropdown and the settings list
await renderSessions();
await loadSessionList();
addMessage("system", `Deleted session: ${sessionName}`);
} catch (e) {
alert("Failed to delete session: " + e.message);
}
};
sessionItem.appendChild(sessionInfo);
sessionItem.appendChild(deleteBtn);
sessionListEl.appendChild(sessionItem);
});
} catch (e) {
const sessionListEl = document.getElementById("sessionList");
sessionListEl.innerHTML = '<p style="color: #ff3333; font-size: 0.85rem;">Failed to load sessions</p>';
}
}
// Show modal and load session list
settingsBtn.addEventListener("click", () => {
settingsModal.classList.add("show");
loadSessionList(); // Refresh session list when opening settings
});
// Hide modal functions
const hideModal = () => {
settingsModal.classList.remove("show");
};
closeModalBtn.addEventListener("click", hideModal);
cancelSettingsBtn.addEventListener("click", hideModal);
modalOverlay.addEventListener("click", hideModal);
// ESC key to close
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && settingsModal.classList.contains("show")) {
hideModal();
}
});
// Save settings
saveSettingsBtn.addEventListener("click", () => {
const selectedRadio = document.querySelector('input[name="backend"]:checked');
let backendValue;
if (selectedRadio.value === "custom") {
backendValue = document.getElementById("customBackend").value.trim().toUpperCase();
if (!backendValue) {
alert("Please enter a custom backend name");
return;
}
} else {
backendValue = selectedRadio.value;
}
localStorage.setItem("standardModeBackend", backendValue);
addMessage("system", `Backend changed to: ${backendValue}`);
hideModal();
});
// Health check
checkHealth();
setInterval(checkHealth, 10000);
// Input events
document.getElementById("sendBtn").addEventListener("click", sendMessage);
document.getElementById("userInput").addEventListener("keypress", e => {
if (e.key === "Enter") sendMessage();
});
});
</script>
</body>
</html>
-20
View File
@@ -1,20 +0,0 @@
{
"name": "Lyra Chat",
"short_name": "Lyra",
"start_url": "./index.html",
"display": "standalone",
"background_color": "#181818",
"theme_color": "#181818",
"icons": [
{
"src": "icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icon-512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
-424
View File
@@ -1,424 +0,0 @@
:root {
--bg-dark: #0a0a0a;
--bg-panel: rgba(255, 115, 0, 0.1);
--accent: #ff6600;
--accent-glow: 0 0 12px #ff6600cc;
--text-main: #e6e6e6;
--text-fade: #999;
--font-console: "IBM Plex Mono", monospace;
}
/* Light mode variables */
body {
--bg-dark: #f5f5f5;
--bg-panel: rgba(255, 115, 0, 0.05);
--accent: #ff6600;
--accent-glow: 0 0 12px #ff6600cc;
--text-main: #1a1a1a;
--text-fade: #666;
}
/* Dark mode variables */
body.dark {
--bg-dark: #0a0a0a;
--bg-panel: rgba(255, 115, 0, 0.1);
--accent: #ff6600;
--accent-glow: 0 0 12px #ff6600cc;
--text-main: #e6e6e6;
--text-fade: #999;
}
body {
margin: 0;
background: var(--bg-dark);
color: var(--text-main);
font-family: var(--font-console);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
#chat {
width: 95%;
max-width: 900px;
height: 95vh;
display: flex;
flex-direction: column;
border: 1px solid var(--accent);
border-radius: 10px;
box-shadow: var(--accent-glow);
background: var(--bg-dark);
overflow: hidden;
}
/* Header sections */
#model-select, #session-select, #status {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border-bottom: 1px solid var(--accent);
background-color: rgba(255, 102, 0, 0.05);
}
#status {
justify-content: flex-start;
border-top: 1px solid var(--accent);
}
label, select, button {
font-family: var(--font-console);
font-size: 0.9rem;
color: var(--text-main);
background: transparent;
border: 1px solid var(--accent);
border-radius: 4px;
padding: 4px 8px;
}
button:hover, select:hover {
box-shadow: 0 0 8px var(--accent);
cursor: pointer;
}
/* Chat area */
#messages {
flex: 1;
padding: 16px;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 8px;
scroll-behavior: smooth;
}
/* Messages */
.msg {
max-width: 80%;
padding: 10px 14px;
border-radius: 8px;
line-height: 1.4;
word-wrap: break-word;
box-shadow: 0 0 8px rgba(255,102,0,0.2);
}
.msg.user {
align-self: flex-end;
background: rgba(255,102,0,0.15);
border: 1px solid var(--accent);
}
.msg.assistant {
align-self: flex-start;
background: rgba(255,102,0,0.08);
border: 1px solid rgba(255,102,0,0.5);
}
.msg.system {
align-self: center;
font-size: 0.8rem;
color: var(--text-fade);
}
/* Input bar */
#input {
display: flex;
border-top: 1px solid var(--accent);
background: rgba(255, 102, 0, 0.05);
padding: 10px;
}
#userInput {
flex: 1;
background: transparent;
color: var(--text-main);
border: 1px solid var(--accent);
border-radius: 4px;
padding: 8px;
}
#sendBtn {
margin-left: 8px;
}
/* Relay status dot */
#status {
display: flex;
align-items: center;
margin: 10px 0;
gap: 8px;
font-family: monospace;
color: #f5f5f5;
}
#status-dot {
width: 10px;
height: 10px;
border-radius: 50%;
display: inline-block;
}
@keyframes pulseGreen {
0% { box-shadow: 0 0 5px #00ff66; opacity: 0.9; }
50% { box-shadow: 0 0 20px #00ff99; opacity: 1; }
100% { box-shadow: 0 0 5px #00ff66; opacity: 0.9; }
}
.dot.ok {
background: #00ff66;
animation: pulseGreen 2s infinite ease-in-out;
}
/* Offline state stays solid red */
.dot.fail {
background: #ff3333;
box-shadow: 0 0 10px #ff3333;
}
/* Dropdown (session selector) styling */
select {
background-color: var(--bg-dark);
color: var(--text-main);
border: 1px solid #b84a12;
border-radius: 6px;
padding: 4px 6px;
font-size: 14px;
}
select option {
background-color: var(--bg-dark);
color: var(--text-main);
}
/* Hover/focus for better visibility */
select:focus,
select:hover {
outline: none;
border-color: #ff7a33;
background-color: var(--bg-panel);
}
/* Settings Modal */
.modal {
display: none !important;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1000;
}
.modal.show {
display: block !important;
}
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.8);
backdrop-filter: blur(4px);
z-index: 999;
}
.modal-content {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: linear-gradient(180deg, rgba(255,102,0,0.1) 0%, rgba(10,10,10,0.95) 100%);
border: 2px solid var(--accent);
border-radius: 12px;
box-shadow: var(--accent-glow), 0 0 40px rgba(255,102,0,0.3);
min-width: 400px;
max-width: 600px;
max-height: 80vh;
overflow-y: auto;
z-index: 1001;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 20px;
border-bottom: 1px solid var(--accent);
background: rgba(255,102,0,0.1);
}
.modal-header h3 {
margin: 0;
font-size: 1.2rem;
color: var(--accent);
}
.close-btn {
background: transparent;
border: none;
color: var(--accent);
font-size: 1.5rem;
cursor: pointer;
padding: 0;
width: 30px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
}
.close-btn:hover {
background: rgba(255,102,0,0.2);
box-shadow: 0 0 8px var(--accent);
}
.modal-body {
padding: 20px;
}
.settings-section h4 {
margin: 0 0 8px 0;
color: var(--accent);
font-size: 1rem;
}
.settings-desc {
margin: 0 0 16px 0;
color: var(--text-fade);
font-size: 0.85rem;
}
.radio-group {
display: flex;
flex-direction: column;
gap: 12px;
}
.radio-label {
display: flex;
flex-direction: column;
padding: 12px;
border: 1px solid rgba(255,102,0,0.3);
border-radius: 6px;
background: rgba(255,102,0,0.05);
cursor: pointer;
transition: all 0.2s;
}
.radio-label:hover {
border-color: var(--accent);
background: rgba(255,102,0,0.1);
box-shadow: 0 0 8px rgba(255,102,0,0.3);
}
.radio-label input[type="radio"] {
margin-right: 8px;
accent-color: var(--accent);
}
.radio-label span {
font-weight: 500;
margin-bottom: 4px;
}
.radio-label small {
color: var(--text-fade);
font-size: 0.8rem;
margin-left: 24px;
}
.radio-label input[type="text"] {
margin-top: 8px;
margin-left: 24px;
padding: 6px;
background: rgba(0,0,0,0.3);
border: 1px solid rgba(255,102,0,0.5);
border-radius: 4px;
color: var(--text-main);
font-family: var(--font-console);
}
.radio-label input[type="text"]:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 8px rgba(255,102,0,0.3);
}
.modal-footer {
display: flex;
justify-content: flex-end;
gap: 10px;
padding: 16px 20px;
border-top: 1px solid var(--accent);
background: rgba(255,102,0,0.05);
}
.primary-btn {
background: var(--accent);
color: #000;
font-weight: bold;
}
.primary-btn:hover {
background: #ff7a33;
box-shadow: var(--accent-glow);
}
/* Session List */
.session-list {
display: flex;
flex-direction: column;
gap: 8px;
max-height: 300px;
overflow-y: auto;
}
.session-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px;
border: 1px solid rgba(255,102,0,0.3);
border-radius: 6px;
background: rgba(255,102,0,0.05);
transition: all 0.2s;
}
.session-item:hover {
border-color: var(--accent);
background: rgba(255,102,0,0.1);
}
.session-info {
display: flex;
flex-direction: column;
gap: 4px;
flex: 1;
}
.session-info strong {
color: var(--text-main);
font-size: 0.95rem;
}
.session-info small {
color: var(--text-fade);
font-size: 0.75rem;
}
.session-delete-btn {
background: transparent;
border: 1px solid rgba(255,102,0,0.5);
color: var(--accent);
padding: 6px 10px;
border-radius: 4px;
cursor: pointer;
font-size: 1rem;
transition: all 0.2s;
}
.session-delete-btn:hover {
background: rgba(255,0,0,0.2);
border-color: #ff3333;
color: #ff3333;
box-shadow: 0 0 8px rgba(255,0,0,0.3);
}
-21
View File
@@ -1,21 +0,0 @@
# ====================================
# 🧠 CORTEX OPERATIONAL CONFIG
# ====================================
# Cortex-specific parameters (all other config inherited from root .env)
CORTEX_MODE=autonomous
CORTEX_LOOP_INTERVAL=300
CORTEX_REFLECTION_INTERVAL=86400
CORTEX_LOG_LEVEL=debug
NEOMEM_HEALTH_CHECK_INTERVAL=300
# Reflection output configuration
REFLECTION_NOTE_TARGET=trilium
REFLECTION_NOTE_PATH=/app/logs/reflections.log
# Memory retrieval tuning
RELEVANCE_THRESHOLD=0.78
# NOTE: LLM backend URLs, OPENAI_API_KEY, database credentials,
# and service URLs are all inherited from root .env
# Cortex uses LLM_PRIMARY (vLLM on MI50) by default
-9
View File
@@ -1,9 +0,0 @@
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 7081
# NOTE: Running with single worker to maintain SESSIONS global state in Intake.
# If scaling to multiple workers, migrate SESSIONS to Redis or shared storage.
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7081"]
-249
View File
@@ -1,249 +0,0 @@
# 📐 Project Lyra — Cognitive Assembly Spec
**Version:** 0.6.1
**Status:** Canonical reference
**Purpose:** Define clear separation of Self, Thought, Reasoning, and Speech
---
## 1. High-Level Overview
Lyra is composed of **four distinct cognitive layers**, plus I/O.
Each layer has:
- a **responsibility**
- a **scope**
- clear **inputs / outputs**
- explicit **authority boundaries**
No layer is allowed to “do everything.”
---
## 2. Layer Definitions
### 2.1 Autonomy / Self (NON-LLM)
**What it is**
- Persistent identity
- Long-term state
- Mood, preferences, values
- Continuity across time
**What it is NOT**
- Not a reasoning engine
- Not a planner
- Not a speaker
- Not creative
**Implementation**
- Data + light logic
- JSON / Python objects
- No LLM calls
**Lives at**
```
project-lyra/autonomy/self/
```
**Inputs**
- Events (user message received, response sent)
- Time / idle ticks (later)
**Outputs**
- Self state snapshot
- Flags / preferences (e.g. verbosity, tone bias)
---
### 2.2 Inner Monologue (LLM, PRIVATE)
**What it is**
- Internal language-based thought
- Reflection
- Intent formation
- “What do I think about this?”
**What it is NOT**
- Not final reasoning
- Not execution
- Not user-facing
**Model**
- MythoMax
**Lives at**
```
project-lyra/autonomy/monologue/
```
**Inputs**
- User message
- Self state snapshot
- Recent context summary
**Outputs**
- Intent
- Tone guidance
- Depth guidance
- “Consult executive?” flag
**Example Output**
```json
{
"intent": "technical_exploration",
"tone": "focused",
"depth": "deep",
"consult_executive": true
}
```
---
### 2.3 Cortex (Reasoning & Execution)
**What it is**
- Thinking pipeline
- Planning
- Tool selection
- Task execution
- Draft generation
**What it is NOT**
- Not identity
- Not personality
- Not persistent self
**Models**
- DeepSeek-R1 → Executive / Planner
- GPT-4o-mini → Executor / Drafter
**Lives at**
```
project-lyra/cortex/
```
**Inputs**
- User message
- Inner Monologue output
- Memory / RAG / tools
**Outputs**
- Draft response (content only)
- Metadata (sources, confidence, etc.)
---
### 2.4 Persona / Speech (LLM, USER-FACING)
**What it is**
- Voice
- Style
- Expression
- Social behavior
**What it is NOT**
- Not planning
- Not deep reasoning
- Not decision-making
**Model**
- MythoMax
**Lives at**
```
project-lyra/core/persona/
```
**Inputs**
- Draft response (from Cortex)
- Tone + intent (from Inner Monologue)
- Persona configuration
**Outputs**
- Final user-visible text
---
## 3. Message Flow (Authoritative)
### 3.1 Standard Message Path
```
User
UI
Relay
Cortex
Autonomy / Self (state snapshot)
Inner Monologue (MythoMax)
[ consult_executive? ]
├─ Yes → DeepSeek-R1 (plan)
└─ No → skip
GPT-4o-mini (execute & draft)
Persona (MythoMax)
Relay
UI
User
```
### 3.2 Fast Path (No Thinking)
```
User → UI → Relay → Persona → Relay → UI
```
---
## 4. Authority Rules (Non-Negotiable)
- Self never calls an LLM
- Inner Monologue never speaks to the user
- Cortex never applies personality
- Persona never reasons or plans
- DeepSeek never writes final answers
- MythoMax never plans execution
---
## 5. Folder Mapping
```
project-lyra/
├── autonomy/
│ ├── self/
│ ├── monologue/
│ └── executive/
├── cortex/
├── core/
│ └── persona/
├── relay/
└── ui/
```
---
## 6. Current Status
- UI ✔
- Relay ✔
- Cortex ✔
- Persona ✔
- Autonomy ✔
- Inner Monologue ⚠ partially wired
- Executive gating ⚠ planned
---
## 7. Next Decision
Decide whether **Inner Monologue runs every message** or **only when triggered**.
-1
View File
@@ -1 +0,0 @@
# Autonomy module for Lyra
-1
View File
@@ -1 +0,0 @@
"""Autonomous action execution system."""
@@ -1,480 +0,0 @@
"""
Autonomous Action Manager - executes safe, self-initiated actions.
"""
import logging
import json
from typing import Dict, List, Any, Optional
from datetime import datetime
logger = logging.getLogger(__name__)
class AutonomousActionManager:
"""
Manages safe autonomous actions that Lyra can take without explicit user prompting.
Whitelist of allowed actions:
- create_memory: Store information in NeoMem
- update_goal: Modify goal status
- schedule_reminder: Create future reminder
- summarize_session: Generate conversation summary
- learn_topic: Add topic to learning queue
- update_focus: Change current focus area
"""
def __init__(self):
"""Initialize action manager with whitelisted actions."""
self.allowed_actions = {
"create_memory": self._create_memory,
"update_goal": self._update_goal,
"schedule_reminder": self._schedule_reminder,
"summarize_session": self._summarize_session,
"learn_topic": self._learn_topic,
"update_focus": self._update_focus
}
self.action_log = [] # Track all actions for audit
async def execute_action(
self,
action_type: str,
parameters: Dict[str, Any],
context: Dict[str, Any]
) -> Dict[str, Any]:
"""
Execute a single autonomous action.
Args:
action_type: Type of action (must be in whitelist)
parameters: Action-specific parameters
context: Current context state
Returns:
{
"success": bool,
"action": action_type,
"result": action_result,
"timestamp": ISO timestamp,
"error": optional error message
}
"""
# Safety check: action must be whitelisted
if action_type not in self.allowed_actions:
logger.error(f"[ACTIONS] Attempted to execute non-whitelisted action: {action_type}")
return {
"success": False,
"action": action_type,
"error": f"Action '{action_type}' not in whitelist",
"timestamp": datetime.utcnow().isoformat()
}
try:
logger.info(f"[ACTIONS] Executing autonomous action: {action_type}")
# Execute the action
action_func = self.allowed_actions[action_type]
result = await action_func(parameters, context)
# Log successful action
action_record = {
"success": True,
"action": action_type,
"result": result,
"timestamp": datetime.utcnow().isoformat(),
"parameters": parameters
}
self.action_log.append(action_record)
logger.info(f"[ACTIONS] Action {action_type} completed successfully")
return action_record
except Exception as e:
logger.error(f"[ACTIONS] Action {action_type} failed: {e}")
error_record = {
"success": False,
"action": action_type,
"error": str(e),
"timestamp": datetime.utcnow().isoformat(),
"parameters": parameters
}
self.action_log.append(error_record)
return error_record
async def execute_batch(
self,
actions: List[Dict[str, Any]],
context: Dict[str, Any]
) -> List[Dict[str, Any]]:
"""
Execute multiple actions sequentially.
Args:
actions: List of {"action": str, "parameters": dict}
context: Current context state
Returns:
List of action results
"""
results = []
for action_spec in actions:
action_type = action_spec.get("action")
parameters = action_spec.get("parameters", {})
result = await self.execute_action(action_type, parameters, context)
results.append(result)
# Stop on first failure if critical
if not result["success"] and action_spec.get("critical", False):
logger.warning(f"[ACTIONS] Critical action {action_type} failed, stopping batch")
break
return results
# ========================================
# Whitelisted Action Implementations
# ========================================
async def _create_memory(
self,
parameters: Dict[str, Any],
context: Dict[str, Any]
) -> Dict[str, Any]:
"""
Create a memory entry in NeoMem.
Parameters:
- text: Memory content (required)
- tags: Optional tags for memory
- importance: 0.0-1.0 importance score
"""
text = parameters.get("text")
if not text:
raise ValueError("Memory text required")
tags = parameters.get("tags", [])
importance = parameters.get("importance", 0.5)
session_id = context.get("session_id", "autonomous")
# Import NeoMem client
try:
from memory.neomem_client import store_memory
result = await store_memory(
text=text,
session_id=session_id,
tags=tags,
importance=importance
)
return {
"memory_id": result.get("id"),
"text": text[:50] + "..." if len(text) > 50 else text
}
except ImportError:
logger.warning("[ACTIONS] NeoMem client not available, simulating memory storage")
return {
"memory_id": "simulated",
"text": text[:50] + "..." if len(text) > 50 else text,
"note": "NeoMem not available, memory not persisted"
}
async def _update_goal(
self,
parameters: Dict[str, Any],
context: Dict[str, Any]
) -> Dict[str, Any]:
"""
Update goal status in self-state.
Parameters:
- goal_id: Goal identifier (required)
- status: New status (pending/in_progress/completed)
- progress: Optional progress note
"""
goal_id = parameters.get("goal_id")
if not goal_id:
raise ValueError("goal_id required")
status = parameters.get("status", "in_progress")
progress = parameters.get("progress")
# Import self-state manager
from autonomy.self.state import get_self_state_instance
state = get_self_state_instance()
active_goals = state._state.get("active_goals", [])
# Find and update goal
updated = False
for goal in active_goals:
if isinstance(goal, dict) and goal.get("id") == goal_id:
goal["status"] = status
if progress:
goal["progress"] = progress
goal["updated_at"] = datetime.utcnow().isoformat()
updated = True
break
if updated:
state._save_state()
return {
"goal_id": goal_id,
"status": status,
"updated": True
}
else:
return {
"goal_id": goal_id,
"updated": False,
"note": "Goal not found"
}
async def _schedule_reminder(
self,
parameters: Dict[str, Any],
context: Dict[str, Any]
) -> Dict[str, Any]:
"""
Schedule a future reminder.
Parameters:
- message: Reminder text (required)
- delay_minutes: Minutes until reminder
- priority: 0.0-1.0 priority score
"""
message = parameters.get("message")
if not message:
raise ValueError("Reminder message required")
delay_minutes = parameters.get("delay_minutes", 60)
priority = parameters.get("priority", 0.5)
# For now, store in self-state's learning queue
# In future: integrate with scheduler/cron system
from autonomy.self.state import get_self_state_instance
state = get_self_state_instance()
reminder = {
"type": "reminder",
"message": message,
"scheduled_at": datetime.utcnow().isoformat(),
"trigger_at_minutes": delay_minutes,
"priority": priority
}
# Add to learning queue as placeholder
state._state.setdefault("reminders", []).append(reminder)
state._save_state(state._state) # Pass state dict as argument
logger.info(f"[ACTIONS] Reminder scheduled: {message} (in {delay_minutes}min)")
return {
"message": message,
"delay_minutes": delay_minutes,
"note": "Reminder stored in self-state (scheduler integration pending)"
}
async def _summarize_session(
self,
parameters: Dict[str, Any],
context: Dict[str, Any]
) -> Dict[str, Any]:
"""
Generate a summary of current session.
Parameters:
- max_length: Max summary length in words
- focus_topics: Optional list of topics to emphasize
"""
max_length = parameters.get("max_length", 200)
session_id = context.get("session_id", "unknown")
# Import summarizer (from deferred_summary or create simple one)
try:
from utils.deferred_summary import summarize_conversation
summary = await summarize_conversation(
session_id=session_id,
max_words=max_length
)
return {
"summary": summary,
"word_count": len(summary.split())
}
except ImportError:
# Fallback: simple summary
message_count = context.get("message_count", 0)
focus = context.get("monologue", {}).get("intent", "general")
summary = f"Session {session_id}: {message_count} messages exchanged, focused on {focus}."
return {
"summary": summary,
"word_count": len(summary.split()),
"note": "Simple summary (full summarizer not available)"
}
async def _learn_topic(
self,
parameters: Dict[str, Any],
context: Dict[str, Any]
) -> Dict[str, Any]:
"""
Add topic to learning queue.
Parameters:
- topic: Topic name (required)
- reason: Why this topic
- priority: 0.0-1.0 priority score
"""
topic = parameters.get("topic")
if not topic:
raise ValueError("Topic required")
reason = parameters.get("reason", "autonomous learning")
priority = parameters.get("priority", 0.5)
# Import self-state manager
from autonomy.self.state import get_self_state_instance
state = get_self_state_instance()
state.add_learning_goal(topic) # Only pass topic parameter
logger.info(f"[ACTIONS] Added to learning queue: {topic} (reason: {reason})")
return {
"topic": topic,
"reason": reason,
"queue_position": len(state._state.get("learning_queue", []))
}
async def _update_focus(
self,
parameters: Dict[str, Any],
context: Dict[str, Any]
) -> Dict[str, Any]:
"""
Update current focus area.
Parameters:
- focus: New focus area (required)
- reason: Why this focus
"""
focus = parameters.get("focus")
if not focus:
raise ValueError("Focus required")
reason = parameters.get("reason", "autonomous update")
# Import self-state manager
from autonomy.self.state import get_self_state_instance
state = get_self_state_instance()
old_focus = state._state.get("focus", "none")
state._state["focus"] = focus
state._state["focus_updated_at"] = datetime.utcnow().isoformat()
state._state["focus_reason"] = reason
state._save_state(state._state) # Pass state dict as argument
logger.info(f"[ACTIONS] Focus updated: {old_focus} -> {focus}")
return {
"old_focus": old_focus,
"new_focus": focus,
"reason": reason
}
# ========================================
# Utility Methods
# ========================================
def get_allowed_actions(self) -> List[str]:
"""Get list of all allowed action types."""
return list(self.allowed_actions.keys())
def get_action_log(self, limit: int = 50) -> List[Dict[str, Any]]:
"""
Get recent action log.
Args:
limit: Max number of entries to return
Returns:
List of action records
"""
return self.action_log[-limit:]
def clear_action_log(self) -> None:
"""Clear action log."""
self.action_log = []
logger.info("[ACTIONS] Action log cleared")
def validate_action(self, action_type: str, parameters: Dict[str, Any]) -> Dict[str, Any]:
"""
Validate an action without executing it.
Args:
action_type: Type of action
parameters: Action parameters
Returns:
{
"valid": bool,
"action": action_type,
"errors": [error messages] or []
}
"""
errors = []
# Check whitelist
if action_type not in self.allowed_actions:
errors.append(f"Action '{action_type}' not in whitelist")
# Check required parameters (basic validation)
if action_type == "create_memory" and not parameters.get("text"):
errors.append("Memory 'text' parameter required")
if action_type == "update_goal" and not parameters.get("goal_id"):
errors.append("Goal 'goal_id' parameter required")
if action_type == "schedule_reminder" and not parameters.get("message"):
errors.append("Reminder 'message' parameter required")
if action_type == "learn_topic" and not parameters.get("topic"):
errors.append("Learning 'topic' parameter required")
if action_type == "update_focus" and not parameters.get("focus"):
errors.append("Focus 'focus' parameter required")
return {
"valid": len(errors) == 0,
"action": action_type,
"errors": errors
}
# Singleton instance
_action_manager_instance = None
def get_action_manager() -> AutonomousActionManager:
"""
Get singleton action manager instance.
Returns:
AutonomousActionManager instance
"""
global _action_manager_instance
if _action_manager_instance is None:
_action_manager_instance = AutonomousActionManager()
return _action_manager_instance
-1
View File
@@ -1 +0,0 @@
"""Executive planning and decision-making module."""
-121
View File
@@ -1,121 +0,0 @@
"""
Executive planner - generates execution plans for complex requests.
Activated when inner monologue sets consult_executive=true.
"""
import os
import logging
from typing import Dict, Any, Optional
from llm.llm_router import call_llm
EXECUTIVE_LLM = os.getenv("EXECUTIVE_LLM", "CLOUD").upper()
VERBOSE_DEBUG = os.getenv("VERBOSE_DEBUG", "false").lower() == "true"
logger = logging.getLogger(__name__)
if VERBOSE_DEBUG:
logger.setLevel(logging.DEBUG)
EXECUTIVE_SYSTEM_PROMPT = """
You are Lyra's executive planning system.
You create structured execution plans for complex tasks.
You do NOT generate the final response - only the plan.
Your plan should include:
1. Task decomposition (break into steps)
2. Required tools/resources
3. Reasoning strategy
4. Success criteria
Return a concise plan in natural language.
"""
async def plan_execution(
user_prompt: str,
intent: str,
context_state: Dict[str, Any],
identity_block: Dict[str, Any]
) -> Dict[str, Any]:
"""
Generate execution plan for complex request.
Args:
user_prompt: User's message
intent: Detected intent from inner monologue
context_state: Full context
identity_block: Lyra's identity
Returns:
Plan dictionary with structure:
{
"summary": "One-line plan summary",
"plan_text": "Detailed plan",
"steps": ["step1", "step2", ...],
"tools_needed": ["RAG", "WEB", ...],
"estimated_complexity": "low | medium | high"
}
"""
# Build planning prompt
tools_available = context_state.get("tools_available", [])
prompt = f"""{EXECUTIVE_SYSTEM_PROMPT}
User request: {user_prompt}
Detected intent: {intent}
Available tools: {", ".join(tools_available) if tools_available else "None"}
Session context:
- Message count: {context_state.get('message_count', 0)}
- Time since last message: {context_state.get('minutes_since_last_msg', 0):.1f} minutes
- Active project: {context_state.get('active_project', 'None')}
Generate a structured execution plan.
"""
if VERBOSE_DEBUG:
logger.debug(f"[EXECUTIVE] Planning prompt:\n{prompt}")
# Call executive LLM
plan_text = await call_llm(
prompt,
backend=EXECUTIVE_LLM,
temperature=0.3, # Lower temperature for planning
max_tokens=500
)
if VERBOSE_DEBUG:
logger.debug(f"[EXECUTIVE] Generated plan:\n{plan_text}")
# Parse plan (simple heuristic extraction for Phase 1)
steps = []
tools_needed = []
for line in plan_text.split('\n'):
line_lower = line.lower()
if any(marker in line_lower for marker in ['step', '1.', '2.', '3.', '-']):
steps.append(line.strip())
if tools_available:
for tool in tools_available:
if tool.lower() in line_lower and tool not in tools_needed:
tools_needed.append(tool)
# Estimate complexity (simple heuristic)
complexity = "low"
if len(steps) > 3 or len(tools_needed) > 1:
complexity = "medium"
if len(steps) > 5 or "research" in intent.lower() or "analyze" in intent.lower():
complexity = "high"
return {
"summary": plan_text.split('\n')[0][:100] if plan_text else "Complex task execution plan",
"plan_text": plan_text,
"steps": steps[:10], # Limit to 10 steps
"tools_needed": tools_needed,
"estimated_complexity": complexity
}
-1
View File
@@ -1 +0,0 @@
"""Pattern learning and adaptation system."""
-383
View File
@@ -1,383 +0,0 @@
"""
Pattern Learning System - learns from interaction patterns to improve autonomy.
"""
import logging
import json
import os
from typing import Dict, List, Any, Optional
from datetime import datetime
from collections import defaultdict
logger = logging.getLogger(__name__)
class PatternLearner:
"""
Learns from interaction patterns to improve Lyra's autonomous behavior.
Tracks:
- Topic frequencies (what users talk about)
- Time-of-day patterns (when users interact)
- User preferences (how users like responses)
- Successful response strategies (what works well)
"""
def __init__(self, patterns_file: str = "/app/data/learned_patterns.json"):
"""
Initialize pattern learner.
Args:
patterns_file: Path to persistent patterns storage
"""
self.patterns_file = patterns_file
self.patterns = self._load_patterns()
def _load_patterns(self) -> Dict[str, Any]:
"""Load patterns from disk."""
if os.path.exists(self.patterns_file):
try:
with open(self.patterns_file, 'r') as f:
patterns = json.load(f)
logger.info(f"[PATTERN_LEARNER] Loaded patterns from {self.patterns_file}")
return patterns
except Exception as e:
logger.error(f"[PATTERN_LEARNER] Failed to load patterns: {e}")
# Initialize empty patterns
return {
"topic_frequencies": {},
"time_patterns": {},
"user_preferences": {},
"successful_strategies": {},
"interaction_count": 0,
"last_updated": datetime.utcnow().isoformat()
}
def _save_patterns(self) -> None:
"""Save patterns to disk."""
try:
# Ensure directory exists
os.makedirs(os.path.dirname(self.patterns_file), exist_ok=True)
self.patterns["last_updated"] = datetime.utcnow().isoformat()
with open(self.patterns_file, 'w') as f:
json.dump(self.patterns, f, indent=2)
logger.debug(f"[PATTERN_LEARNER] Saved patterns to {self.patterns_file}")
except Exception as e:
logger.error(f"[PATTERN_LEARNER] Failed to save patterns: {e}")
async def learn_from_interaction(
self,
user_prompt: str,
response: str,
monologue: Dict[str, Any],
context: Dict[str, Any]
) -> None:
"""
Learn from a single interaction.
Args:
user_prompt: User's message
response: Lyra's response
monologue: Inner monologue analysis
context: Full context state
"""
self.patterns["interaction_count"] += 1
# Learn topic frequencies
self._learn_topics(user_prompt, monologue)
# Learn time patterns
self._learn_time_patterns()
# Learn user preferences
self._learn_preferences(monologue, context)
# Learn successful strategies
self._learn_strategies(monologue, response, context)
# Save periodically (every 10 interactions)
if self.patterns["interaction_count"] % 10 == 0:
self._save_patterns()
def _learn_topics(self, user_prompt: str, monologue: Dict[str, Any]) -> None:
"""Track topic frequencies."""
intent = monologue.get("intent", "unknown")
# Increment topic counter
topic_freq = self.patterns["topic_frequencies"]
topic_freq[intent] = topic_freq.get(intent, 0) + 1
# Extract keywords (simple approach - words > 5 chars)
keywords = [word.lower() for word in user_prompt.split() if len(word) > 5]
for keyword in keywords:
topic_freq[f"keyword:{keyword}"] = topic_freq.get(f"keyword:{keyword}", 0) + 1
logger.debug(f"[PATTERN_LEARNER] Topic learned: {intent}")
def _learn_time_patterns(self) -> None:
"""Track time-of-day patterns."""
now = datetime.utcnow()
hour = now.hour
# Track interactions by hour
time_patterns = self.patterns["time_patterns"]
hour_key = f"hour_{hour:02d}"
time_patterns[hour_key] = time_patterns.get(hour_key, 0) + 1
# Track day of week
day_key = f"day_{now.strftime('%A').lower()}"
time_patterns[day_key] = time_patterns.get(day_key, 0) + 1
def _learn_preferences(self, monologue: Dict[str, Any], context: Dict[str, Any]) -> None:
"""Learn user preferences from detected tone and depth."""
tone = monologue.get("tone", "neutral")
depth = monologue.get("depth", "medium")
prefs = self.patterns["user_preferences"]
# Track preferred tone
prefs.setdefault("tone_counts", {})
prefs["tone_counts"][tone] = prefs["tone_counts"].get(tone, 0) + 1
# Track preferred depth
prefs.setdefault("depth_counts", {})
prefs["depth_counts"][depth] = prefs["depth_counts"].get(depth, 0) + 1
def _learn_strategies(
self,
monologue: Dict[str, Any],
response: str,
context: Dict[str, Any]
) -> None:
"""
Learn which response strategies are successful.
Success indicators:
- Executive was consulted and plan generated
- Response length matches depth request
- Tone matches request
"""
intent = monologue.get("intent", "unknown")
executive_used = context.get("executive_plan") is not None
strategies = self.patterns["successful_strategies"]
strategies.setdefault(intent, {})
# Track executive usage for this intent
if executive_used:
key = f"{intent}:executive_used"
strategies.setdefault(key, 0)
strategies[key] += 1
# Track response length patterns
response_length = len(response.split())
depth = monologue.get("depth", "medium")
length_key = f"{depth}:avg_words"
if length_key not in strategies:
strategies[length_key] = response_length
else:
# Running average
strategies[length_key] = (strategies[length_key] + response_length) / 2
# ========================================
# Pattern Analysis and Recommendations
# ========================================
def get_top_topics(self, limit: int = 10) -> List[tuple]:
"""
Get most frequent topics.
Args:
limit: Max number of topics to return
Returns:
List of (topic, count) tuples, sorted by count
"""
topics = self.patterns["topic_frequencies"]
sorted_topics = sorted(topics.items(), key=lambda x: x[1], reverse=True)
return sorted_topics[:limit]
def get_preferred_tone(self) -> str:
"""
Get user's most preferred tone.
Returns:
Preferred tone string
"""
prefs = self.patterns["user_preferences"]
tone_counts = prefs.get("tone_counts", {})
if not tone_counts:
return "neutral"
return max(tone_counts.items(), key=lambda x: x[1])[0]
def get_preferred_depth(self) -> str:
"""
Get user's most preferred response depth.
Returns:
Preferred depth string
"""
prefs = self.patterns["user_preferences"]
depth_counts = prefs.get("depth_counts", {})
if not depth_counts:
return "medium"
return max(depth_counts.items(), key=lambda x: x[1])[0]
def get_peak_hours(self, limit: int = 3) -> List[int]:
"""
Get peak interaction hours.
Args:
limit: Number of top hours to return
Returns:
List of hours (0-23)
"""
time_patterns = self.patterns["time_patterns"]
hour_counts = {k: v for k, v in time_patterns.items() if k.startswith("hour_")}
if not hour_counts:
return []
sorted_hours = sorted(hour_counts.items(), key=lambda x: x[1], reverse=True)
top_hours = sorted_hours[:limit]
# Extract hour numbers
return [int(h[0].split("_")[1]) for h in top_hours]
def should_use_executive(self, intent: str) -> bool:
"""
Recommend whether to use executive for given intent based on patterns.
Args:
intent: Intent type
Returns:
True if executive is recommended
"""
strategies = self.patterns["successful_strategies"]
key = f"{intent}:executive_used"
# If we've used executive for this intent >= 3 times, recommend it
return strategies.get(key, 0) >= 3
def get_recommended_response_length(self, depth: str) -> int:
"""
Get recommended response length in words for given depth.
Args:
depth: Depth level (short/medium/deep)
Returns:
Recommended word count
"""
strategies = self.patterns["successful_strategies"]
key = f"{depth}:avg_words"
avg_length = strategies.get(key, None)
if avg_length:
return int(avg_length)
# Defaults if no pattern learned
defaults = {
"short": 50,
"medium": 150,
"deep": 300
}
return defaults.get(depth, 150)
def get_insights(self) -> Dict[str, Any]:
"""
Get high-level insights from learned patterns.
Returns:
{
"total_interactions": int,
"top_topics": [(topic, count), ...],
"preferred_tone": str,
"preferred_depth": str,
"peak_hours": [hours],
"learning_recommendations": [str]
}
"""
recommendations = []
# Check if user consistently prefers certain settings
preferred_tone = self.get_preferred_tone()
preferred_depth = self.get_preferred_depth()
if preferred_tone != "neutral":
recommendations.append(f"User prefers {preferred_tone} tone")
if preferred_depth != "medium":
recommendations.append(f"User prefers {preferred_depth} depth responses")
# Check for recurring topics
top_topics = self.get_top_topics(limit=3)
if top_topics:
top_topic = top_topics[0][0]
recommendations.append(f"Consider adding '{top_topic}' to learning queue")
return {
"total_interactions": self.patterns["interaction_count"],
"top_topics": self.get_top_topics(limit=5),
"preferred_tone": preferred_tone,
"preferred_depth": preferred_depth,
"peak_hours": self.get_peak_hours(limit=3),
"learning_recommendations": recommendations
}
def reset_patterns(self) -> None:
"""Reset all learned patterns (use with caution)."""
self.patterns = {
"topic_frequencies": {},
"time_patterns": {},
"user_preferences": {},
"successful_strategies": {},
"interaction_count": 0,
"last_updated": datetime.utcnow().isoformat()
}
self._save_patterns()
logger.warning("[PATTERN_LEARNER] Patterns reset")
def export_patterns(self) -> Dict[str, Any]:
"""
Export all patterns for analysis.
Returns:
Complete patterns dict
"""
return self.patterns.copy()
# Singleton instance
_learner_instance = None
def get_pattern_learner(patterns_file: str = "/app/data/learned_patterns.json") -> PatternLearner:
"""
Get singleton pattern learner instance.
Args:
patterns_file: Path to patterns file (only used on first call)
Returns:
PatternLearner instance
"""
global _learner_instance
if _learner_instance is None:
_learner_instance = PatternLearner(patterns_file=patterns_file)
return _learner_instance
-1
View File
@@ -1 +0,0 @@
# Inner monologue module
-115
View File
@@ -1,115 +0,0 @@
import os
import json
import logging
from typing import Dict
from llm.llm_router import call_llm
# Configuration
MONOLOGUE_LLM = os.getenv("MONOLOGUE_LLM", "PRIMARY").upper()
VERBOSE_DEBUG = os.getenv("VERBOSE_DEBUG", "false").lower() == "true"
# Logger
logger = logging.getLogger(__name__)
if VERBOSE_DEBUG:
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s [MONOLOGUE] %(levelname)s: %(message)s',
datefmt='%H:%M:%S'
))
logger.addHandler(console_handler)
MONOLOGUE_SYSTEM_PROMPT = """
You are Lyra's inner monologue.
You think privately.
You do NOT speak to the user.
You do NOT solve the task.
You only reflect on intent, tone, and depth.
Return ONLY valid JSON with:
- intent (string)
- tone (neutral | warm | focused | playful | direct)
- depth (short | medium | deep)
- consult_executive (true | false)
"""
class InnerMonologue:
async def process(self, context: Dict) -> Dict:
# Build full prompt with system instructions merged in
full_prompt = f"""{MONOLOGUE_SYSTEM_PROMPT}
User message:
{context['user_message']}
Self state:
{context['self_state']}
Context summary:
{context['context_summary']}
Output JSON only:
"""
# Call LLM using configured backend
if VERBOSE_DEBUG:
logger.debug(f"[InnerMonologue] Calling LLM with backend: {MONOLOGUE_LLM}")
logger.debug(f"[InnerMonologue] Prompt length: {len(full_prompt)} chars")
result = await call_llm(
full_prompt,
backend=MONOLOGUE_LLM,
temperature=0.7,
max_tokens=200
)
if VERBOSE_DEBUG:
logger.debug(f"[InnerMonologue] Raw LLM response:")
logger.debug(f"{'='*80}")
logger.debug(result)
logger.debug(f"{'='*80}")
logger.debug(f"[InnerMonologue] Response length: {len(result) if result else 0} chars")
# Parse JSON response - extract just the JSON part if there's extra text
try:
# Try direct parsing first
parsed = json.loads(result)
if VERBOSE_DEBUG:
logger.debug(f"[InnerMonologue] Successfully parsed JSON directly: {parsed}")
return parsed
except json.JSONDecodeError:
# If direct parsing fails, try to extract JSON from the response
if VERBOSE_DEBUG:
logger.debug(f"[InnerMonologue] Direct JSON parse failed, attempting extraction...")
# Look for JSON object (starts with { and ends with })
import re
json_match = re.search(r'\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}', result, re.DOTALL)
if json_match:
json_str = json_match.group(0)
try:
parsed = json.loads(json_str)
if VERBOSE_DEBUG:
logger.debug(f"[InnerMonologue] Successfully extracted and parsed JSON: {parsed}")
return parsed
except json.JSONDecodeError as e:
if VERBOSE_DEBUG:
logger.warning(f"[InnerMonologue] Extracted JSON still invalid: {e}")
else:
if VERBOSE_DEBUG:
logger.warning(f"[InnerMonologue] No JSON object found in response")
# Final fallback
if VERBOSE_DEBUG:
logger.warning(f"[InnerMonologue] All parsing attempts failed, using fallback")
else:
print(f"[InnerMonologue] JSON extraction failed")
print(f"[InnerMonologue] Raw response was: {result[:500]}")
return {
"intent": "unknown",
"tone": "neutral",
"depth": "medium",
"consult_executive": False
}
-1
View File
@@ -1 +0,0 @@
"""Proactive monitoring and suggestion system."""
-321
View File
@@ -1,321 +0,0 @@
"""
Proactive Context Monitor - detects opportunities for autonomous suggestions.
"""
import logging
import time
from typing import Dict, List, Any, Optional
from datetime import datetime, timedelta
logger = logging.getLogger(__name__)
class ProactiveMonitor:
"""
Monitors conversation context and detects opportunities for proactive suggestions.
Triggers:
- Long silence → Check-in
- Learning queue + high curiosity → Suggest exploration
- Active goals → Progress reminders
- Conversation milestones → Offer summary
- Pattern detection → Helpful suggestions
"""
def __init__(self, min_priority: float = 0.6):
"""
Initialize proactive monitor.
Args:
min_priority: Minimum priority for suggestions (0.0-1.0)
"""
self.min_priority = min_priority
self.last_suggestion_time = {} # session_id -> timestamp
self.cooldown_seconds = 300 # 5 minutes between proactive suggestions
async def analyze_session(
self,
session_id: str,
context_state: Dict[str, Any],
self_state: Dict[str, Any]
) -> Optional[Dict[str, Any]]:
"""
Analyze session for proactive suggestion opportunities.
Args:
session_id: Current session ID
context_state: Full context including message history
self_state: Lyra's current self-state
Returns:
{
"suggestion": "text to append to response",
"priority": 0.0-1.0,
"reason": "why this suggestion",
"type": "check_in | learning | goal_reminder | summary | pattern"
}
or None if no suggestion
"""
# Check cooldown
if not self._check_cooldown(session_id):
logger.debug(f"[PROACTIVE] Session {session_id} in cooldown, skipping")
return None
suggestions = []
# Check 1: Long silence detection
silence_suggestion = self._check_long_silence(context_state)
if silence_suggestion:
suggestions.append(silence_suggestion)
# Check 2: Learning queue + high curiosity
learning_suggestion = self._check_learning_opportunity(self_state)
if learning_suggestion:
suggestions.append(learning_suggestion)
# Check 3: Active goals reminder
goal_suggestion = self._check_active_goals(self_state, context_state)
if goal_suggestion:
suggestions.append(goal_suggestion)
# Check 4: Conversation milestones
milestone_suggestion = self._check_conversation_milestone(context_state)
if milestone_suggestion:
suggestions.append(milestone_suggestion)
# Check 5: Pattern-based suggestions
pattern_suggestion = self._check_patterns(context_state, self_state)
if pattern_suggestion:
suggestions.append(pattern_suggestion)
# Filter by priority and return highest
valid_suggestions = [s for s in suggestions if s["priority"] >= self.min_priority]
if not valid_suggestions:
return None
# Return highest priority suggestion
best_suggestion = max(valid_suggestions, key=lambda x: x["priority"])
# Update cooldown timer
self._update_cooldown(session_id)
logger.info(f"[PROACTIVE] Suggestion generated: {best_suggestion['type']} (priority: {best_suggestion['priority']:.2f})")
return best_suggestion
def _check_cooldown(self, session_id: str) -> bool:
"""Check if session is past cooldown period."""
if session_id not in self.last_suggestion_time:
return True
elapsed = time.time() - self.last_suggestion_time[session_id]
return elapsed >= self.cooldown_seconds
def _update_cooldown(self, session_id: str) -> None:
"""Update cooldown timer for session."""
self.last_suggestion_time[session_id] = time.time()
def _check_long_silence(self, context_state: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""
Check if user has been silent for a long time.
"""
minutes_since_last = context_state.get("minutes_since_last_msg", 0)
# If > 30 minutes, suggest check-in
if minutes_since_last > 30:
return {
"suggestion": "\n\n[Aside: I'm still here if you need anything!]",
"priority": 0.7,
"reason": f"User silent for {minutes_since_last:.0f} minutes",
"type": "check_in"
}
return None
def _check_learning_opportunity(self, self_state: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""
Check if Lyra has learning queue items and high curiosity.
"""
learning_queue = self_state.get("learning_queue", [])
curiosity = self_state.get("curiosity", 0.5)
# If curiosity > 0.7 and learning queue exists
if curiosity > 0.7 and learning_queue:
topic = learning_queue[0] if learning_queue else "new topics"
return {
"suggestion": f"\n\n[Aside: I've been curious about {topic} lately. Would you like to explore it together?]",
"priority": 0.65,
"reason": f"High curiosity ({curiosity:.2f}) and learning queue present",
"type": "learning"
}
return None
def _check_active_goals(
self,
self_state: Dict[str, Any],
context_state: Dict[str, Any]
) -> Optional[Dict[str, Any]]:
"""
Check if there are active goals worth reminding about.
"""
active_goals = self_state.get("active_goals", [])
if not active_goals:
return None
# Check if we've had multiple messages without goal progress
message_count = context_state.get("message_count", 0)
# Every 10 messages, consider goal reminder
if message_count % 10 == 0 and message_count > 0:
goal = active_goals[0] # First active goal
goal_name = goal if isinstance(goal, str) else goal.get("name", "your goal")
return {
"suggestion": f"\n\n[Aside: Still thinking about {goal_name}. Let me know if you want to work on it.]",
"priority": 0.6,
"reason": f"Active goal present, {message_count} messages since start",
"type": "goal_reminder"
}
return None
def _check_conversation_milestone(self, context_state: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""
Check for conversation milestones (e.g., every 50 messages).
"""
message_count = context_state.get("message_count", 0)
# Every 50 messages, offer summary
if message_count > 0 and message_count % 50 == 0:
return {
"suggestion": f"\n\n[Aside: We've exchanged {message_count} messages! Would you like a summary of our conversation?]",
"priority": 0.65,
"reason": f"Milestone: {message_count} messages",
"type": "summary"
}
return None
def _check_patterns(
self,
context_state: Dict[str, Any],
self_state: Dict[str, Any]
) -> Optional[Dict[str, Any]]:
"""
Check for behavioral patterns that merit suggestions.
"""
# Get current focus
focus = self_state.get("focus", "")
# Check if user keeps asking similar questions (detected via focus)
if focus and "repeated" in focus.lower():
return {
"suggestion": "\n\n[Aside: I notice we keep coming back to this topic. Would it help to create a summary or action plan?]",
"priority": 0.7,
"reason": "Repeated topic detected",
"type": "pattern"
}
# Check energy levels - if Lyra is low energy, maybe suggest break
energy = self_state.get("energy", 0.8)
if energy < 0.3:
return {
"suggestion": "\n\n[Aside: We've been at this for a while. Need a break or want to keep going?]",
"priority": 0.65,
"reason": f"Low energy ({energy:.2f})",
"type": "pattern"
}
return None
def format_suggestion(self, suggestion: Dict[str, Any]) -> str:
"""
Format suggestion for appending to response.
Args:
suggestion: Suggestion dict from analyze_session()
Returns:
Formatted string to append to response
"""
return suggestion.get("suggestion", "")
def set_cooldown_duration(self, seconds: int) -> None:
"""
Update cooldown duration.
Args:
seconds: New cooldown duration
"""
self.cooldown_seconds = seconds
logger.info(f"[PROACTIVE] Cooldown updated to {seconds}s")
def reset_cooldown(self, session_id: str) -> None:
"""
Reset cooldown for a specific session.
Args:
session_id: Session to reset
"""
if session_id in self.last_suggestion_time:
del self.last_suggestion_time[session_id]
logger.info(f"[PROACTIVE] Cooldown reset for session {session_id}")
def get_session_stats(self, session_id: str) -> Dict[str, Any]:
"""
Get stats for a session's proactive monitoring.
Args:
session_id: Session to check
Returns:
{
"last_suggestion_time": timestamp or None,
"seconds_since_last": int,
"cooldown_active": bool,
"cooldown_remaining": int
}
"""
last_time = self.last_suggestion_time.get(session_id)
if not last_time:
return {
"last_suggestion_time": None,
"seconds_since_last": 0,
"cooldown_active": False,
"cooldown_remaining": 0
}
seconds_since = int(time.time() - last_time)
cooldown_active = seconds_since < self.cooldown_seconds
cooldown_remaining = max(0, self.cooldown_seconds - seconds_since)
return {
"last_suggestion_time": last_time,
"seconds_since_last": seconds_since,
"cooldown_active": cooldown_active,
"cooldown_remaining": cooldown_remaining
}
# Singleton instance
_monitor_instance = None
def get_proactive_monitor(min_priority: float = 0.6) -> ProactiveMonitor:
"""
Get singleton proactive monitor instance.
Args:
min_priority: Minimum priority threshold (only used on first call)
Returns:
ProactiveMonitor instance
"""
global _monitor_instance
if _monitor_instance is None:
_monitor_instance = ProactiveMonitor(min_priority=min_priority)
return _monitor_instance
-1
View File
@@ -1 +0,0 @@
# Self state module
-74
View File
@@ -1,74 +0,0 @@
"""
Analyze interactions and update self-state accordingly.
"""
import logging
from typing import Dict, Any
from .state import update_self_state
logger = logging.getLogger(__name__)
async def analyze_and_update_state(
monologue: Dict[str, Any],
user_prompt: str,
response: str,
context: Dict[str, Any]
) -> None:
"""
Analyze interaction and update self-state.
This runs after response generation to update Lyra's internal state
based on the interaction.
Args:
monologue: Inner monologue output
user_prompt: User's message
response: Lyra's response
context: Full context state
"""
# Simple heuristics for state updates
# TODO: Replace with LLM-based sentiment analysis in Phase 2
mood_delta = 0.0
energy_delta = 0.0
confidence_delta = 0.0
curiosity_delta = 0.0
new_focus = None
# Analyze intent from monologue
intent = monologue.get("intent", "").lower() if monologue else ""
if "technical" in intent or "complex" in intent:
energy_delta = -0.05 # Deep thinking is tiring
confidence_delta = 0.05 if len(response) > 200 else -0.05
new_focus = "technical_problem"
elif "creative" in intent or "brainstorm" in intent:
mood_delta = 0.1 # Creative work is engaging
curiosity_delta = 0.1
new_focus = "creative_exploration"
elif "clarification" in intent or "confused" in intent:
confidence_delta = -0.05
new_focus = "understanding_user"
elif "simple" in intent or "casual" in intent:
energy_delta = 0.05 # Light conversation is refreshing
new_focus = "conversation"
# Check for learning opportunities (questions in user prompt)
if "?" in user_prompt and any(word in user_prompt.lower() for word in ["how", "why", "what"]):
curiosity_delta += 0.05
# Update state
update_self_state(
mood_delta=mood_delta,
energy_delta=energy_delta,
new_focus=new_focus,
confidence_delta=confidence_delta,
curiosity_delta=curiosity_delta
)
logger.info(f"Self-state updated based on interaction: focus={new_focus}")
-189
View File
@@ -1,189 +0,0 @@
"""
Self-state management for Project Lyra.
Maintains persistent identity, mood, energy, and focus across sessions.
"""
import json
import logging
import os
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, Optional
# Configuration
STATE_FILE = Path(os.getenv("SELF_STATE_FILE", "/app/data/self_state.json"))
VERBOSE_DEBUG = os.getenv("VERBOSE_DEBUG", "false").lower() == "true"
logger = logging.getLogger(__name__)
if VERBOSE_DEBUG:
logger.setLevel(logging.DEBUG)
# Default state structure
DEFAULT_STATE = {
"mood": "neutral",
"energy": 0.8,
"focus": "user_request",
"confidence": 0.7,
"curiosity": 0.5,
"last_updated": None,
"interaction_count": 0,
"learning_queue": [], # Topics Lyra wants to explore
"active_goals": [], # Self-directed goals
"preferences": {
"verbosity": "medium",
"formality": "casual",
"proactivity": 0.3 # How likely to suggest things unprompted
},
"metadata": {
"version": "1.0",
"created_at": None
}
}
class SelfState:
"""Manages Lyra's persistent self-state."""
def __init__(self):
self._state = self._load_state()
def _load_state(self) -> Dict[str, Any]:
"""Load state from disk or create default."""
if STATE_FILE.exists():
try:
with open(STATE_FILE, 'r') as f:
state = json.load(f)
logger.info(f"Loaded self-state from {STATE_FILE}")
return state
except Exception as e:
logger.error(f"Failed to load self-state: {e}")
return self._create_default_state()
else:
return self._create_default_state()
def _create_default_state(self) -> Dict[str, Any]:
"""Create and save default state."""
state = DEFAULT_STATE.copy()
state["metadata"]["created_at"] = datetime.now().isoformat()
state["last_updated"] = datetime.now().isoformat()
self._save_state(state)
logger.info("Created new default self-state")
return state
def _save_state(self, state: Dict[str, Any]) -> None:
"""Persist state to disk."""
try:
STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
with open(STATE_FILE, 'w') as f:
json.dump(state, f, indent=2)
if VERBOSE_DEBUG:
logger.debug(f"Saved self-state to {STATE_FILE}")
except Exception as e:
logger.error(f"Failed to save self-state: {e}")
def get_state(self) -> Dict[str, Any]:
"""Get current state snapshot."""
return self._state.copy()
def update_from_interaction(
self,
mood_delta: float = 0.0,
energy_delta: float = 0.0,
new_focus: Optional[str] = None,
confidence_delta: float = 0.0,
curiosity_delta: float = 0.0
) -> None:
"""
Update state based on interaction.
Args:
mood_delta: Change in mood (-1.0 to 1.0)
energy_delta: Change in energy (-1.0 to 1.0)
new_focus: New focus area
confidence_delta: Change in confidence
curiosity_delta: Change in curiosity
"""
# Apply deltas with bounds checking
self._state["energy"] = max(0.0, min(1.0,
self._state.get("energy", 0.8) + energy_delta))
self._state["confidence"] = max(0.0, min(1.0,
self._state.get("confidence", 0.7) + confidence_delta))
self._state["curiosity"] = max(0.0, min(1.0,
self._state.get("curiosity", 0.5) + curiosity_delta))
# Update focus if provided
if new_focus:
self._state["focus"] = new_focus
# Update mood (simplified sentiment)
if mood_delta != 0:
mood_map = ["frustrated", "neutral", "engaged", "excited"]
current_mood_idx = 1 # neutral default
if self._state.get("mood") in mood_map:
current_mood_idx = mood_map.index(self._state["mood"])
new_mood_idx = max(0, min(len(mood_map) - 1,
int(current_mood_idx + mood_delta * 2)))
self._state["mood"] = mood_map[new_mood_idx]
# Increment interaction counter
self._state["interaction_count"] = self._state.get("interaction_count", 0) + 1
self._state["last_updated"] = datetime.now().isoformat()
# Persist changes
self._save_state(self._state)
if VERBOSE_DEBUG:
logger.debug(f"Updated self-state: mood={self._state['mood']}, "
f"energy={self._state['energy']:.2f}, "
f"confidence={self._state['confidence']:.2f}")
def add_learning_goal(self, topic: str) -> None:
"""Add topic to learning queue."""
queue = self._state.get("learning_queue", [])
if topic not in [item.get("topic") for item in queue]:
queue.append({
"topic": topic,
"added_at": datetime.now().isoformat(),
"priority": 0.5
})
self._state["learning_queue"] = queue
self._save_state(self._state)
logger.info(f"Added learning goal: {topic}")
def add_active_goal(self, goal: str, context: str = "") -> None:
"""Add self-directed goal."""
goals = self._state.get("active_goals", [])
goals.append({
"goal": goal,
"context": context,
"created_at": datetime.now().isoformat(),
"status": "active"
})
self._state["active_goals"] = goals
self._save_state(self._state)
logger.info(f"Added active goal: {goal}")
# Global instance
_self_state_instance = None
def get_self_state_instance() -> SelfState:
"""Get or create global SelfState instance."""
global _self_state_instance
if _self_state_instance is None:
_self_state_instance = SelfState()
return _self_state_instance
def load_self_state() -> Dict[str, Any]:
"""Load self state - public API for backwards compatibility."""
return get_self_state_instance().get_state()
def update_self_state(**kwargs) -> None:
"""Update self state - public API."""
get_self_state_instance().update_from_interaction(**kwargs)
-1
View File
@@ -1 +0,0 @@
"""Autonomous tool invocation system."""
-124
View File
@@ -1,124 +0,0 @@
"""
Tool Decision Engine - decides which tools to invoke autonomously.
"""
import logging
from typing import Dict, List, Any
logger = logging.getLogger(__name__)
class ToolDecisionEngine:
"""Decides which tools to invoke based on context analysis."""
async def analyze_tool_needs(
self,
user_prompt: str,
monologue: Dict[str, Any],
context_state: Dict[str, Any],
available_tools: List[str]
) -> Dict[str, Any]:
"""
Analyze if tools should be invoked and which ones.
Args:
user_prompt: User's message
monologue: Inner monologue analysis
context_state: Full context
available_tools: List of available tools
Returns:
{
"should_invoke_tools": bool,
"tools_to_invoke": [
{
"tool": "RAG | WEB | WEATHER | etc",
"query": "search query",
"reason": "why this tool",
"priority": 0.0-1.0
},
...
],
"confidence": 0.0-1.0
}
"""
tools_to_invoke = []
# Check for memory/context needs
if any(word in user_prompt.lower() for word in [
"remember", "you said", "we discussed", "earlier", "before",
"last time", "previously", "what did"
]):
tools_to_invoke.append({
"tool": "RAG",
"query": user_prompt,
"reason": "User references past conversation",
"priority": 0.9
})
# Check for web search needs
if any(word in user_prompt.lower() for word in [
"current", "latest", "news", "today", "what's happening",
"look up", "search for", "find information", "recent"
]):
tools_to_invoke.append({
"tool": "WEB",
"query": user_prompt,
"reason": "Requires current information",
"priority": 0.8
})
# Check for weather needs
if any(word in user_prompt.lower() for word in [
"weather", "temperature", "forecast", "rain", "sunny", "climate"
]):
tools_to_invoke.append({
"tool": "WEATHER",
"query": user_prompt,
"reason": "Weather information requested",
"priority": 0.95
})
# Check for code-related needs
if any(word in user_prompt.lower() for word in [
"code", "function", "debug", "implement", "algorithm",
"programming", "script", "syntax"
]):
if "CODEBRAIN" in available_tools:
tools_to_invoke.append({
"tool": "CODEBRAIN",
"query": user_prompt,
"reason": "Code-related task",
"priority": 0.85
})
# Proactive RAG for complex queries (based on monologue)
intent = monologue.get("intent", "") if monologue else ""
if monologue and monologue.get("consult_executive"):
# Complex query - might benefit from context
if not any(t["tool"] == "RAG" for t in tools_to_invoke):
tools_to_invoke.append({
"tool": "RAG",
"query": user_prompt,
"reason": "Complex query benefits from context",
"priority": 0.6
})
# Sort by priority
tools_to_invoke.sort(key=lambda x: x["priority"], reverse=True)
max_priority = max([t["priority"] for t in tools_to_invoke]) if tools_to_invoke else 0.0
result = {
"should_invoke_tools": len(tools_to_invoke) > 0,
"tools_to_invoke": tools_to_invoke,
"confidence": max_priority
}
if tools_to_invoke:
logger.info(f"[TOOL_DECISION] Autonomous tool invocation recommended: {len(tools_to_invoke)} tools")
for tool in tools_to_invoke:
logger.info(f" - {tool['tool']} (priority: {tool['priority']:.2f}): {tool['reason']}")
return result
-357
View File
@@ -1,357 +0,0 @@
"""
Tool Orchestrator - executes autonomous tool invocations asynchronously.
"""
import asyncio
import logging
from typing import Dict, List, Any, Optional
import os
logger = logging.getLogger(__name__)
class ToolOrchestrator:
"""Orchestrates async tool execution and result aggregation."""
def __init__(self, tool_timeout: int = 30):
"""
Initialize orchestrator.
Args:
tool_timeout: Max seconds per tool call (default 30)
"""
self.tool_timeout = tool_timeout
self.available_tools = self._discover_tools()
def _discover_tools(self) -> Dict[str, Any]:
"""Discover available tool modules."""
tools = {}
# Import tool modules as they become available
if os.getenv("NEOMEM_ENABLED", "false").lower() == "true":
try:
from memory.neomem_client import search_neomem
tools["RAG"] = search_neomem
logger.debug("[ORCHESTRATOR] RAG tool available")
except ImportError:
logger.debug("[ORCHESTRATOR] RAG tool not available")
else:
logger.info("[ORCHESTRATOR] NEOMEM_ENABLED is false; RAG tool disabled")
try:
from integrations.web_search import web_search
tools["WEB"] = web_search
logger.debug("[ORCHESTRATOR] WEB tool available")
except ImportError:
logger.debug("[ORCHESTRATOR] WEB tool not available")
try:
from integrations.weather import get_weather
tools["WEATHER"] = get_weather
logger.debug("[ORCHESTRATOR] WEATHER tool available")
except ImportError:
logger.debug("[ORCHESTRATOR] WEATHER tool not available")
try:
from integrations.codebrain import query_codebrain
tools["CODEBRAIN"] = query_codebrain
logger.debug("[ORCHESTRATOR] CODEBRAIN tool available")
except ImportError:
logger.debug("[ORCHESTRATOR] CODEBRAIN tool not available")
return tools
async def execute_tools(
self,
tools_to_invoke: List[Dict[str, Any]],
context_state: Dict[str, Any]
) -> Dict[str, Any]:
"""
Execute multiple tools asynchronously.
Args:
tools_to_invoke: List of tool specs from decision engine
[{"tool": "RAG", "query": "...", "reason": "...", "priority": 0.9}, ...]
context_state: Full context for tool execution
Returns:
{
"results": {
"RAG": {...},
"WEB": {...},
...
},
"execution_summary": {
"tools_invoked": ["RAG", "WEB"],
"successful": ["RAG"],
"failed": ["WEB"],
"total_time_ms": 1234
}
}
"""
import time
start_time = time.time()
logger.info(f"[ORCHESTRATOR] Executing {len(tools_to_invoke)} tools asynchronously")
# Create tasks for each tool
tasks = []
tool_names = []
for tool_spec in tools_to_invoke:
tool_name = tool_spec["tool"]
query = tool_spec["query"]
if tool_name in self.available_tools:
task = self._execute_single_tool(tool_name, query, context_state)
tasks.append(task)
tool_names.append(tool_name)
logger.debug(f"[ORCHESTRATOR] Queued {tool_name}: {query[:50]}...")
else:
logger.warning(f"[ORCHESTRATOR] Tool {tool_name} not available, skipping")
# Execute all tools concurrently with timeout
results = {}
successful = []
failed = []
if tasks:
try:
# Wait for all tasks with global timeout
completed = await asyncio.wait_for(
asyncio.gather(*tasks, return_exceptions=True),
timeout=self.tool_timeout
)
# Process results
for tool_name, result in zip(tool_names, completed):
if isinstance(result, Exception):
logger.error(f"[ORCHESTRATOR] {tool_name} failed: {result}")
results[tool_name] = {"error": str(result), "success": False}
failed.append(tool_name)
else:
logger.info(f"[ORCHESTRATOR] {tool_name} completed successfully")
results[tool_name] = result
successful.append(tool_name)
except asyncio.TimeoutError:
logger.error(f"[ORCHESTRATOR] Global timeout ({self.tool_timeout}s) exceeded")
for tool_name in tool_names:
if tool_name not in results:
results[tool_name] = {"error": "timeout", "success": False}
failed.append(tool_name)
end_time = time.time()
total_time_ms = int((end_time - start_time) * 1000)
execution_summary = {
"tools_invoked": tool_names,
"successful": successful,
"failed": failed,
"total_time_ms": total_time_ms
}
logger.info(f"[ORCHESTRATOR] Execution complete: {len(successful)}/{len(tool_names)} successful in {total_time_ms}ms")
return {
"results": results,
"execution_summary": execution_summary
}
async def _execute_single_tool(
self,
tool_name: str,
query: str,
context_state: Dict[str, Any]
) -> Dict[str, Any]:
"""
Execute a single tool with error handling.
Args:
tool_name: Name of tool (RAG, WEB, etc.)
query: Query string for the tool
context_state: Context for tool execution
Returns:
Tool-specific result dict
"""
tool_func = self.available_tools.get(tool_name)
if not tool_func:
raise ValueError(f"Tool {tool_name} not available")
try:
logger.debug(f"[ORCHESTRATOR] Invoking {tool_name}...")
# Different tools have different signatures - adapt as needed
if tool_name == "RAG":
result = await self._invoke_rag(tool_func, query, context_state)
elif tool_name == "WEB":
result = await self._invoke_web(tool_func, query)
elif tool_name == "WEATHER":
result = await self._invoke_weather(tool_func, query)
elif tool_name == "CODEBRAIN":
result = await self._invoke_codebrain(tool_func, query, context_state)
else:
# Generic invocation
result = await tool_func(query)
return {
"success": True,
"tool": tool_name,
"query": query,
"data": result
}
except Exception as e:
logger.error(f"[ORCHESTRATOR] {tool_name} execution failed: {e}")
raise
async def _invoke_rag(self, func, query: str, context: Dict[str, Any]) -> Any:
"""Invoke RAG tool (NeoMem search)."""
session_id = context.get("session_id", "unknown")
# RAG searches memory for relevant past interactions
try:
results = await func(query, limit=5, session_id=session_id)
return results
except Exception as e:
logger.warning(f"[ORCHESTRATOR] RAG invocation failed, returning empty: {e}")
return []
async def _invoke_web(self, func, query: str) -> Any:
"""Invoke web search tool."""
try:
results = await func(query, max_results=5)
return results
except Exception as e:
logger.warning(f"[ORCHESTRATOR] WEB invocation failed: {e}")
return {"error": str(e), "results": []}
async def _invoke_weather(self, func, query: str) -> Any:
"""Invoke weather tool."""
# Extract location from query (simple heuristic)
# In future: use LLM to extract location
try:
location = self._extract_location(query)
results = await func(location)
return results
except Exception as e:
logger.warning(f"[ORCHESTRATOR] WEATHER invocation failed: {e}")
return {"error": str(e)}
async def _invoke_codebrain(self, func, query: str, context: Dict[str, Any]) -> Any:
"""Invoke codebrain tool."""
try:
results = await func(query, context=context)
return results
except Exception as e:
logger.warning(f"[ORCHESTRATOR] CODEBRAIN invocation failed: {e}")
return {"error": str(e)}
def _extract_location(self, query: str) -> str:
"""
Extract location from weather query.
Simple heuristic - in future use LLM.
"""
# Common location indicators
indicators = ["in ", "at ", "for ", "weather in ", "temperature in "]
query_lower = query.lower()
for indicator in indicators:
if indicator in query_lower:
# Get text after indicator
parts = query_lower.split(indicator, 1)
if len(parts) > 1:
location = parts[1].strip().split()[0] # First word after indicator
return location
# Default fallback
return "current location"
def format_results_for_context(self, orchestrator_result: Dict[str, Any]) -> str:
"""
Format tool results for inclusion in context/prompt.
Args:
orchestrator_result: Output from execute_tools()
Returns:
Formatted string for prompt injection
"""
results = orchestrator_result.get("results", {})
summary = orchestrator_result.get("execution_summary", {})
if not results:
return ""
formatted = "\n=== AUTONOMOUS TOOL RESULTS ===\n"
for tool_name, tool_result in results.items():
if tool_result.get("success", False):
formatted += f"\n[{tool_name}]\n"
data = tool_result.get("data", {})
# Format based on tool type
if tool_name == "RAG":
formatted += self._format_rag_results(data)
elif tool_name == "WEB":
formatted += self._format_web_results(data)
elif tool_name == "WEATHER":
formatted += self._format_weather_results(data)
elif tool_name == "CODEBRAIN":
formatted += self._format_codebrain_results(data)
else:
formatted += f"{data}\n"
else:
formatted += f"\n[{tool_name}] - Failed: {tool_result.get('error', 'unknown')}\n"
formatted += f"\n(Tools executed in {summary.get('total_time_ms', 0)}ms)\n"
formatted += "=" * 40 + "\n"
return formatted
def _format_rag_results(self, data: Any) -> str:
"""Format RAG/memory search results."""
if not data:
return "No relevant memories found.\n"
formatted = "Relevant memories:\n"
for i, item in enumerate(data[:3], 1): # Top 3
text = item.get("text", item.get("content", str(item)))
formatted += f" {i}. {text[:100]}...\n"
return formatted
def _format_web_results(self, data: Any) -> str:
"""Format web search results."""
if isinstance(data, dict) and data.get("error"):
return f"Web search failed: {data['error']}\n"
results = data.get("results", []) if isinstance(data, dict) else data
if not results:
return "No web results found.\n"
formatted = "Web search results:\n"
for i, item in enumerate(results[:3], 1): # Top 3
title = item.get("title", "No title")
snippet = item.get("snippet", item.get("description", ""))
formatted += f" {i}. {title}\n {snippet[:100]}...\n"
return formatted
def _format_weather_results(self, data: Any) -> str:
"""Format weather results."""
if isinstance(data, dict) and data.get("error"):
return f"Weather lookup failed: {data['error']}\n"
# Assuming weather API returns temp, conditions, etc.
temp = data.get("temperature", "unknown")
conditions = data.get("conditions", "unknown")
location = data.get("location", "requested location")
return f"Weather for {location}: {temp}, {conditions}\n"
def _format_codebrain_results(self, data: Any) -> str:
"""Format codebrain results."""
if isinstance(data, dict) and data.get("error"):
return f"Codebrain failed: {data['error']}\n"
# Format code-related results
return f"{data}\n"
-553
View File
@@ -1,553 +0,0 @@
# context.py
"""
Context layer for Cortex reasoning pipeline.
Provides unified context collection from:
- Intake (short-term memory, multilevel summaries L1-L30)
- NeoMem (long-term memory, semantic search)
- Session state (timestamps, messages, mode, mood, active_project)
Maintains per-session state for continuity across conversations.
"""
import os
import logging
from datetime import datetime
from typing import Dict, Any, Optional, List
import httpx
from intake.intake import summarize_context
from neomem_client import NeoMemClient
# -----------------------------
# Configuration
# -----------------------------
NEOMEM_API = os.getenv("NEOMEM_API", "http://neomem-api:8000")
NEOMEM_ENABLED = os.getenv("NEOMEM_ENABLED", "false").lower() == "true"
RELEVANCE_THRESHOLD = float(os.getenv("RELEVANCE_THRESHOLD", "0.4"))
LOG_DETAIL_LEVEL = os.getenv("LOG_DETAIL_LEVEL", "summary").lower()
# Loop detection settings
MAX_MESSAGE_HISTORY = int(os.getenv("MAX_MESSAGE_HISTORY", "100")) # Prevent unbounded growth
SESSION_TTL_HOURS = int(os.getenv("SESSION_TTL_HOURS", "24")) # Auto-expire old sessions
ENABLE_DUPLICATE_DETECTION = os.getenv("ENABLE_DUPLICATE_DETECTION", "true").lower() == "true"
# Tools available for future autonomy features
TOOLS_AVAILABLE = ["RAG", "WEB", "WEATHER", "CODEBRAIN", "POKERBRAIN"]
# -----------------------------
# Module-level session state
# -----------------------------
SESSION_STATE: Dict[str, Dict[str, Any]] = {}
# Logger
logger = logging.getLogger(__name__)
# Always set up basic logging
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s [CONTEXT] %(levelname)s: %(message)s',
datefmt='%H:%M:%S'
))
logger.addHandler(console_handler)
# -----------------------------
# Session initialization & cleanup
# -----------------------------
def _init_session(session_id: str) -> Dict[str, Any]:
"""
Initialize a new session state entry.
Returns:
Dictionary with default session state fields
"""
return {
"session_id": session_id,
"created_at": datetime.now(),
"last_timestamp": datetime.now(),
"last_user_message": None,
"last_assistant_message": None,
"mode": "default", # Future: "autonomous", "focused", "creative", etc.
"mood": "neutral", # Future: mood tracking
"active_project": None, # Future: project context
"message_count": 0,
"message_history": [],
"last_message_hash": None, # For duplicate detection
}
def _cleanup_expired_sessions():
"""Remove sessions that haven't been active for SESSION_TTL_HOURS"""
from datetime import timedelta
now = datetime.now()
expired_sessions = []
for session_id, state in SESSION_STATE.items():
last_active = state.get("last_timestamp", state.get("created_at"))
time_since_active = (now - last_active).total_seconds() / 3600 # hours
if time_since_active > SESSION_TTL_HOURS:
expired_sessions.append(session_id)
for session_id in expired_sessions:
del SESSION_STATE[session_id]
logger.info(f"🗑️ Expired session: {session_id} (inactive for {SESSION_TTL_HOURS}+ hours)")
return len(expired_sessions)
def _is_duplicate_message(session_id: str, user_prompt: str) -> bool:
"""
Check if this message is a duplicate of the last processed message.
Uses simple hash comparison to detect exact duplicates or processing loops.
"""
if not ENABLE_DUPLICATE_DETECTION:
return False
import hashlib
state = SESSION_STATE.get(session_id)
if not state:
return False
# Create hash of normalized message
message_hash = hashlib.md5(user_prompt.strip().lower().encode()).hexdigest()
# Check if it matches the last message
if state.get("last_message_hash") == message_hash:
logger.warning(
f"⚠️ DUPLICATE MESSAGE DETECTED | Session: {session_id} | "
f"Message: {user_prompt[:80]}..."
)
return True
# Update hash for next check
state["last_message_hash"] = message_hash
return False
def _trim_message_history(state: Dict[str, Any]):
"""
Trim message history to prevent unbounded growth.
Keeps only the most recent MAX_MESSAGE_HISTORY messages.
"""
history = state.get("message_history", [])
if len(history) > MAX_MESSAGE_HISTORY:
trimmed_count = len(history) - MAX_MESSAGE_HISTORY
state["message_history"] = history[-MAX_MESSAGE_HISTORY:]
logger.info(f"✂️ Trimmed {trimmed_count} old messages from session {state['session_id']}")
# -----------------------------
# Intake context retrieval
# -----------------------------
async def _get_intake_context(session_id: str, messages: List[Dict[str, str]]):
"""
Internal Intake — Direct call to summarize_context()
No HTTP, no containers, no failures.
"""
try:
return await summarize_context(session_id, messages)
except Exception as e:
logger.error(f"Internal Intake summarization failed: {e}")
return {
"session_id": session_id,
"L1": "",
"L5": "",
"L10": "",
"L20": "",
"L30": "",
"error": str(e)
}
# -----------------------------
# NeoMem semantic search
# -----------------------------
async def _search_neomem(
query: str,
user_id: str = "brian",
limit: int = 5
) -> List[Dict[str, Any]]:
"""
Search NeoMem for relevant long-term memories.
Returns full response structure from NeoMem:
[
{
"id": "mem_abc123",
"score": 0.92,
"payload": {
"data": "Memory text content...",
"metadata": {
"category": "...",
"created_at": "...",
...
}
}
},
...
]
Args:
query: Search query text
user_id: User identifier for memory filtering
limit: Maximum number of results
Returns:
List of memory objects with full structure, or empty list on failure
"""
if not NEOMEM_ENABLED:
logger.info("NeoMem search skipped (NEOMEM_ENABLED is false)")
return []
try:
# NeoMemClient reads NEOMEM_API from environment, no base_url parameter
client = NeoMemClient()
results = await client.search(
query=query,
user_id=user_id,
limit=limit,
threshold=RELEVANCE_THRESHOLD
)
# Results are already filtered by threshold in NeoMemClient.search()
logger.info(f"NeoMem search returned {len(results)} relevant results")
return results
except Exception as e:
logger.warning(f"NeoMem search failed: {e}")
return []
# -----------------------------
# Main context collection
# -----------------------------
async def collect_context(session_id: str, user_prompt: str) -> Dict[str, Any]:
"""
Collect unified context from all sources.
Orchestrates:
1. Initialize or update session state
2. Calculate time since last message
3. Retrieve Intake multilevel summaries (L1-L30)
4. Search NeoMem for relevant long-term memories
5. Update session state with current user message
6. Return unified context_state dictionary
Args:
session_id: Session identifier
user_prompt: Current user message
Returns:
Unified context state dictionary with structure:
{
"session_id": "...",
"timestamp": "2025-11-28T12:34:56",
"minutes_since_last_msg": 5.2,
"message_count": 42,
"intake": {
"L1": [...],
"L5": [...],
"L10": {...},
"L20": {...},
"L30": {...}
},
"rag": [
{
"id": "mem_123",
"score": 0.92,
"payload": {
"data": "...",
"metadata": {...}
}
},
...
],
"mode": "default",
"mood": "neutral",
"active_project": null,
"tools_available": ["RAG", "WEB", "WEATHER", "CODEBRAIN", "POKERBRAIN"]
}
"""
# A. Cleanup expired sessions periodically (every 100th call)
import random
if random.randint(1, 100) == 1:
_cleanup_expired_sessions()
# B. Initialize session state if needed
if session_id not in SESSION_STATE:
SESSION_STATE[session_id] = _init_session(session_id)
logger.info(f"Initialized new session: {session_id}")
state = SESSION_STATE[session_id]
# C. Check for duplicate messages (loop detection)
if _is_duplicate_message(session_id, user_prompt):
# Return cached context with warning flag
logger.warning(f"🔁 LOOP DETECTED - Returning cached context to prevent processing duplicate")
context_state = {
"session_id": session_id,
"timestamp": datetime.now().isoformat(),
"minutes_since_last_msg": 0,
"message_count": state["message_count"],
"intake": {},
"rag": [],
"mode": state["mode"],
"mood": state["mood"],
"active_project": state["active_project"],
"tools_available": TOOLS_AVAILABLE,
"duplicate_detected": True,
}
return context_state
# B. Calculate time delta
now = datetime.now()
time_delta_seconds = (now - state["last_timestamp"]).total_seconds()
minutes_since_last_msg = round(time_delta_seconds / 60.0, 2)
# C. Gather Intake context (multilevel summaries)
# Build compact message buffer for Intake:
messages_for_intake = []
# You track messages inside SESSION_STATE — assemble it here:
if "message_history" in state:
for turn in state["message_history"]:
messages_for_intake.append({
"user_msg": turn.get("user", ""),
"assistant_msg": turn.get("assistant", "")
})
intake_data = await _get_intake_context(session_id, messages_for_intake)
# D. Search NeoMem for relevant memories
if NEOMEM_ENABLED:
rag_results = await _search_neomem(
query=user_prompt,
user_id="brian", # TODO: Make configurable per session
limit=5
)
else:
rag_results = []
logger.info("Skipping NeoMem RAG retrieval; NEOMEM_ENABLED is false")
# E. Update session state
state["last_user_message"] = user_prompt
state["last_timestamp"] = now
state["message_count"] += 1
# Save user turn to history
state["message_history"].append({
"user": user_prompt,
"assistant": "" # assistant reply filled later by update_last_assistant_message()
})
# Trim history to prevent unbounded growth
_trim_message_history(state)
# F. Assemble unified context
context_state = {
"session_id": session_id,
"timestamp": now.isoformat(),
"minutes_since_last_msg": minutes_since_last_msg,
"message_count": state["message_count"],
"intake": intake_data,
"rag": rag_results,
"mode": state["mode"],
"mood": state["mood"],
"active_project": state["active_project"],
"tools_available": TOOLS_AVAILABLE,
}
# Log context summary in structured format
logger.info(
f"📊 Context | Session: {session_id} | "
f"Messages: {state['message_count']} | "
f"Last: {minutes_since_last_msg:.1f}min | "
f"RAG: {len(rag_results)} results"
)
# Show detailed context in detailed/verbose mode
if LOG_DETAIL_LEVEL in ["detailed", "verbose"]:
import json
logger.info(f"\n{''*100}")
logger.info(f"[CONTEXT] Session {session_id} | User: {user_prompt[:80]}...")
logger.info(f"{''*100}")
logger.info(f" Mode: {state['mode']} | Mood: {state['mood']} | Project: {state['active_project']}")
logger.info(f" Tools: {', '.join(TOOLS_AVAILABLE)}")
# Show intake summaries (condensed)
if intake_data:
logger.info(f"\n ╭─ INTAKE SUMMARIES ────────────────────────────────────────────────")
for level in ["L1", "L5", "L10", "L20", "L30"]:
if level in intake_data:
summary = intake_data[level]
if isinstance(summary, dict):
summary_text = summary.get("summary", str(summary)[:100])
else:
summary_text = str(summary)[:100]
logger.info(f"{level:4s}: {summary_text}...")
logger.info(f" ╰───────────────────────────────────────────────────────────────────")
# Show RAG results (condensed)
if rag_results:
logger.info(f"\n ╭─ RAG RESULTS ({len(rag_results)}) ──────────────────────────────────────────────")
for idx, result in enumerate(rag_results[:5], 1): # Show top 5
score = result.get("score", 0)
data_preview = str(result.get("payload", {}).get("data", ""))[:60]
logger.info(f" │ [{idx}] {score:.3f} | {data_preview}...")
if len(rag_results) > 5:
logger.info(f" │ ... and {len(rag_results) - 5} more results")
logger.info(f" ╰───────────────────────────────────────────────────────────────────")
# Show full raw data only in verbose mode
if LOG_DETAIL_LEVEL == "verbose":
logger.info(f"\n ╭─ RAW INTAKE DATA ─────────────────────────────────────────────────")
logger.info(f"{json.dumps(intake_data, indent=4, default=str)}")
logger.info(f" ╰───────────────────────────────────────────────────────────────────")
logger.info(f"{''*100}\n")
return context_state
# -----------------------------
# Session state management
# -----------------------------
def update_last_assistant_message(session_id: str, message: str) -> None:
"""
Update session state with assistant's response and complete
the last turn inside message_history.
"""
session = SESSION_STATE.get(session_id)
if not session:
logger.warning(f"Attempted to update non-existent session: {session_id}")
return
# Update last assistant message + timestamp
session["last_assistant_message"] = message
session["last_timestamp"] = datetime.now()
# Fill in assistant reply for the most recent turn
history = session.get("message_history", [])
if history:
# history entry already contains {"user": "...", "assistant": "...?"}
history[-1]["assistant"] = message
def get_session_state(session_id: str) -> Optional[Dict[str, Any]]:
"""
Retrieve current session state.
Args:
session_id: Session identifier
Returns:
Session state dict or None if session doesn't exist
"""
return SESSION_STATE.get(session_id)
def close_session(session_id: str) -> bool:
"""
Close and cleanup a session.
Args:
session_id: Session identifier
Returns:
True if session was closed, False if it didn't exist
"""
if session_id in SESSION_STATE:
del SESSION_STATE[session_id]
logger.info(f"Closed session: {session_id}")
return True
return False
# -----------------------------
# Extension hooks for future autonomy
# -----------------------------
def update_mode(session_id: str, new_mode: str) -> None:
"""
Update session mode.
Future modes: "autonomous", "focused", "creative", "collaborative", etc.
Args:
session_id: Session identifier
new_mode: New mode string
"""
if session_id in SESSION_STATE:
old_mode = SESSION_STATE[session_id]["mode"]
SESSION_STATE[session_id]["mode"] = new_mode
logger.info(f"Session {session_id} mode changed: {old_mode} -> {new_mode}")
def update_mood(session_id: str, new_mood: str) -> None:
"""
Update session mood.
Future implementation: Sentiment analysis, emotional state tracking.
Args:
session_id: Session identifier
new_mood: New mood string
"""
if session_id in SESSION_STATE:
old_mood = SESSION_STATE[session_id]["mood"]
SESSION_STATE[session_id]["mood"] = new_mood
logger.info(f"Session {session_id} mood changed: {old_mood} -> {new_mood}")
def update_active_project(session_id: str, project: Optional[str]) -> None:
"""
Update active project context.
Future implementation: Project-specific memory, tools, preferences.
Args:
session_id: Session identifier
project: Project identifier or None
"""
if session_id in SESSION_STATE:
SESSION_STATE[session_id]["active_project"] = project
logger.info(f"Session {session_id} active project set to: {project}")
async def autonomous_heartbeat(session_id: str) -> Optional[str]:
"""
Autonomous thinking heartbeat.
Future implementation:
- Check if Lyra should initiate internal dialogue
- Generate self-prompted thoughts based on session state
- Update mood/mode based on context changes
- Trigger proactive suggestions or reminders
Args:
session_id: Session identifier
Returns:
Optional autonomous thought/action string
"""
# Stub for future implementation
# Example logic:
# - If minutes_since_last_msg > 60: Check for pending reminders
# - If mood == "curious" and active_project: Generate research questions
# - If mode == "autonomous": Self-prompt based on project goals
logger.debug(f"Autonomous heartbeat for session {session_id} (not yet implemented)")
return None
-20
View File
@@ -1,20 +0,0 @@
{
"mood": "neutral",
"energy": 0.8500000000000001,
"focus": "conversation",
"confidence": 0.7,
"curiosity": 1.0,
"last_updated": "2025-12-21T18:50:41.582043",
"interaction_count": 26,
"learning_queue": [],
"active_goals": [],
"preferences": {
"verbosity": "medium",
"formality": "casual",
"proactivity": 0.3
},
"metadata": {
"version": "1.0",
"created_at": "2025-12-14T03:28:49.364768"
}
}
-1
View File
@@ -1 +0,0 @@
# Ingest module - handles communication with Intake service
-33
View File
@@ -1,33 +0,0 @@
# ingest_handler.py
import os
import httpx
NEOMEM_URL = os.getenv("NEOMEM_API", "http://nvgram-api:7077")
async def handle_ingest(payload):
"""
Pass user+assistant turns to NeoMem.
Minimal version. Does not process or annotate.
"""
data = {
"messages": [],
"user_id": "brian" # default for now
}
if payload.user:
data["messages"].append({"role": "user", "content": payload.user})
if payload.assistant:
data["messages"].append({"role": "assistant", "content": payload.assistant})
try:
async with httpx.AsyncClient() as client:
r = await client.post(
f"{NEOMEM_URL}/memories",
json=data,
timeout=5
)
if r.status_code != 200:
print(f"[Ingest] NeoMem returned {r.status_code}: {r.text}")
except Exception as e:
print(f"[Ingest] Failed to send to NeoMem: {e}")
-45
View File
@@ -1,45 +0,0 @@
# cortex/intake_client.py
import os, httpx, logging
from typing import Dict, Any, Optional
logger = logging.getLogger(__name__)
class IntakeClient:
"""Handles short-term / episodic summaries from Intake service."""
def __init__(self):
self.base_url = os.getenv("INTAKE_API_URL", "http://intake:7080")
async def summarize_turn(self, session_id: str, user_msg: str, assistant_msg: Optional[str] = None) -> Dict[str, Any]:
"""
DEPRECATED: Intake v0.2 removed the /summarize endpoint.
Use add_exchange() instead, which auto-summarizes in the background.
This method is kept for backwards compatibility but will fail.
"""
payload = {
"session_id": session_id,
"turns": [{"role": "user", "content": user_msg}]
}
if assistant_msg:
payload["turns"].append({"role": "assistant", "content": assistant_msg})
async with httpx.AsyncClient(timeout=30) as client:
try:
r = await client.post(f"{self.base_url}/summarize", json=payload)
r.raise_for_status()
return r.json()
except Exception as e:
logger.warning(f"Intake summarize_turn failed (endpoint removed in v0.2): {e}")
return {}
async def get_context(self, session_id: str) -> str:
"""Get summarized context for a session from Intake."""
async with httpx.AsyncClient(timeout=15) as client:
try:
r = await client.get(f"{self.base_url}/summaries", params={"session_id": session_id})
r.raise_for_status()
data = r.json()
return data.get("summary_text", "")
except Exception as e:
logger.warning(f"Intake get_context failed: {e}")
return ""
-18
View File
@@ -1,18 +0,0 @@
"""
Intake module - short-term memory summarization.
Runs inside the Cortex container as a pure Python module.
No standalone API server - called internally by Cortex.
"""
from .intake import (
SESSIONS,
add_exchange_internal,
summarize_context,
)
__all__ = [
"SESSIONS",
"add_exchange_internal",
"summarize_context",
]
-387
View File
@@ -1,387 +0,0 @@
import os
import json
from datetime import datetime
from typing import List, Dict, Any, TYPE_CHECKING
from collections import deque
from llm.llm_router import call_llm
# -------------------------------------------------------------------
# Global Short-Term Memory (new Intake)
# -------------------------------------------------------------------
SESSIONS: dict[str, dict] = {} # session_id → { buffer: deque, created_at: timestamp }
# Diagnostic: Verify module loads only once
print(f"[Intake Module Init] SESSIONS object id: {id(SESSIONS)}, module: {__name__}")
# L10 / L20 history lives here too
L10_HISTORY: Dict[str, list[str]] = {}
L20_HISTORY: Dict[str, list[str]] = {}
from llm.llm_router import call_llm # Use Cortex's shared LLM router
if TYPE_CHECKING:
# Only for type hints — do NOT redefine SESSIONS here
from collections import deque as _deque
def bg_summarize(session_id: str) -> None: ...
# ─────────────────────────────
# Config
# ─────────────────────────────
INTAKE_LLM = os.getenv("INTAKE_LLM", "PRIMARY").upper()
SUMMARY_MAX_TOKENS = int(os.getenv("SUMMARY_MAX_TOKENS", "200"))
SUMMARY_TEMPERATURE = float(os.getenv("SUMMARY_TEMPERATURE", "0.3"))
NEOMEM_API = os.getenv("NEOMEM_API")
NEOMEM_KEY = os.getenv("NEOMEM_KEY")
# ─────────────────────────────
# Internal history for L10/L20/L30
# ─────────────────────────────
L10_HISTORY: Dict[str, list[str]] = {} # session_id → list of L10 blocks
L20_HISTORY: Dict[str, list[str]] = {} # session_id → list of merged overviews
# ─────────────────────────────
# LLM helper (via Cortex router)
# ─────────────────────────────
async def _llm(prompt: str) -> str:
"""
Use Cortex's llm_router to run a summary prompt.
"""
try:
text = await call_llm(
prompt,
backend=INTAKE_LLM,
temperature=SUMMARY_TEMPERATURE,
max_tokens=SUMMARY_MAX_TOKENS,
)
return (text or "").strip()
except Exception as e:
return f"[Error summarizing: {e}]"
# ─────────────────────────────
# Formatting helpers
# ─────────────────────────────
def _format_exchanges(exchanges: List[Dict[str, Any]]) -> str:
"""
Expect each exchange to look like:
{ "user_msg": "...", "assistant_msg": "..." }
"""
chunks = []
for e in exchanges:
user = e.get("user_msg", "")
assistant = e.get("assistant_msg", "")
chunks.append(f"User: {user}\nAssistant: {assistant}\n")
return "\n".join(chunks)
# ─────────────────────────────
# Base factual summary
# ─────────────────────────────
async def summarize_simple(exchanges: List[Dict[str, Any]]) -> str:
"""
Simple factual summary of recent exchanges.
"""
if not exchanges:
return ""
text = _format_exchanges(exchanges)
prompt = f"""
Summarize the following conversation between Brian (user) and Lyra (assistant).
Focus only on factual content. Avoid names, examples, story tone, or invented details.
{text}
Summary:
"""
return await _llm(prompt)
# ─────────────────────────────
# Multilevel Summaries (L1, L5, L10, L20, L30)
# ─────────────────────────────
async def summarize_L1(buf: List[Dict[str, Any]]) -> str:
# Last ~5 exchanges
return await summarize_simple(buf[-5:])
async def summarize_L5(buf: List[Dict[str, Any]]) -> str:
# Last ~10 exchanges
return await summarize_simple(buf[-10:])
async def summarize_L10(session_id: str, buf: List[Dict[str, Any]]) -> str:
# “Reality Check” for last 10 exchanges
text = _format_exchanges(buf[-10:])
prompt = f"""
You are Lyra Intake performing a short 'Reality Check'.
Summarize the last block of conversation (up to 10 exchanges)
in one clear paragraph focusing on tone, intent, and direction.
{text}
Reality Check:
"""
summary = await _llm(prompt)
# Track history for this session
L10_HISTORY.setdefault(session_id, [])
L10_HISTORY[session_id].append(summary)
return summary
async def summarize_L20(session_id: str) -> str:
"""
Merge all L10 Reality Checks into a 'Session Overview'.
"""
history = L10_HISTORY.get(session_id, [])
joined = "\n\n".join(history) if history else ""
if not joined:
return ""
prompt = f"""
You are Lyra Intake creating a 'Session Overview'.
Merge the following Reality Check paragraphs into one short summary
capturing progress, themes, and the direction of the conversation.
{joined}
Overview:
"""
summary = await _llm(prompt)
L20_HISTORY.setdefault(session_id, [])
L20_HISTORY[session_id].append(summary)
return summary
async def summarize_L30(session_id: str) -> str:
"""
Merge all L20 session overviews into a 'Continuity Report'.
"""
history = L20_HISTORY.get(session_id, [])
joined = "\n\n".join(history) if history else ""
if not joined:
return ""
prompt = f"""
You are Lyra Intake generating a 'Continuity Report'.
Condense these session overviews into one high-level reflection,
noting major themes, persistent goals, and shifts.
{joined}
Continuity Report:
"""
return await _llm(prompt)
# ─────────────────────────────
# NeoMem push
# ─────────────────────────────
def push_to_neomem(summary: str, session_id: str, level: str) -> None:
"""
Fire-and-forget push of a summary into NeoMem.
"""
if not NEOMEM_API or not summary:
return
headers = {"Content-Type": "application/json"}
if NEOMEM_KEY:
headers["Authorization"] = f"Bearer {NEOMEM_KEY}"
payload = {
"messages": [{"role": "assistant", "content": summary}],
"user_id": "brian",
"metadata": {
"source": "intake",
"session_id": session_id,
"level": level,
},
}
try:
import requests
requests.post(
f"{NEOMEM_API}/memories",
json=payload,
headers=headers,
timeout=20,
).raise_for_status()
print(f"🧠 NeoMem updated ({level}) for {session_id}")
except Exception as e:
print(f"NeoMem push failed ({level}, {session_id}): {e}")
# ─────────────────────────────
# Main entrypoint for Cortex
# ─────────────────────────────
async def summarize_context(session_id: str, exchanges: list[dict]):
"""
Internal summarizer that uses Cortex's LLM router.
Produces cascading summaries based on exchange count:
- L1: Always (most recent activity)
- L2: After 2+ exchanges
- L5: After 5+ exchanges
- L10: After 10+ exchanges
- L20: After 20+ exchanges
- L30: After 30+ exchanges
Args:
session_id: The conversation/session ID
exchanges: A list of {"user_msg": ..., "assistant_msg": ..., "timestamp": ...}
"""
exchange_count = len(exchanges)
if exchange_count == 0:
return {
"session_id": session_id,
"exchange_count": 0,
"L1": "",
"L2": "",
"L5": "",
"L10": "",
"L20": "",
"L30": "",
"last_updated": datetime.now().isoformat()
}
result = {
"session_id": session_id,
"exchange_count": exchange_count,
"L1": "",
"L2": "",
"L5": "",
"L10": "",
"L20": "",
"L30": "",
"last_updated": datetime.now().isoformat()
}
try:
# L1: Always generate (most recent exchanges)
result["L1"] = await summarize_simple(exchanges[-5:])
print(f"[Intake] Generated L1 for {session_id} ({exchange_count} exchanges)")
# L2: After 2+ exchanges
if exchange_count >= 2:
result["L2"] = await summarize_simple(exchanges[-2:])
print(f"[Intake] Generated L2 for {session_id}")
# L5: After 5+ exchanges
if exchange_count >= 5:
result["L5"] = await summarize_simple(exchanges[-10:])
print(f"[Intake] Generated L5 for {session_id}")
# L10: After 10+ exchanges (Reality Check)
if exchange_count >= 10:
result["L10"] = await summarize_L10(session_id, exchanges)
print(f"[Intake] Generated L10 for {session_id}")
# L20: After 20+ exchanges (Session Overview - merges L10s)
if exchange_count >= 20 and exchange_count % 10 == 0:
result["L20"] = await summarize_L20(session_id)
print(f"[Intake] Generated L20 for {session_id}")
# L30: After 30+ exchanges (Continuity Report - merges L20s)
if exchange_count >= 30 and exchange_count % 10 == 0:
result["L30"] = await summarize_L30(session_id)
print(f"[Intake] Generated L30 for {session_id}")
return result
except Exception as e:
print(f"[Intake] Error during summarization: {e}")
result["L1"] = f"[Error summarizing: {str(e)}]"
return result
# ─────────────────────────────────
# Background summarization stub
# ─────────────────────────────────
def bg_summarize(session_id: str):
"""
Placeholder for background summarization.
Actual summarization happens during /reason via summarize_context().
This function exists to prevent NameError when called from add_exchange_internal().
"""
print(f"[Intake] Exchange added for {session_id}. Will summarize on next /reason call.")
# ─────────────────────────────
# Internal entrypoint for Cortex
# ─────────────────────────────
def get_recent_messages(session_id: str, limit: int = 20) -> list:
"""
Get recent raw messages from the session buffer.
Args:
session_id: Session identifier
limit: Maximum number of messages to return (default 20)
Returns:
List of message dicts with 'role' and 'content' fields
"""
if session_id not in SESSIONS:
return []
buffer = SESSIONS[session_id]["buffer"]
# Convert buffer to list and get last N messages
messages = list(buffer)[-limit:]
return messages
def add_exchange_internal(exchange: dict):
"""
Direct internal call — bypasses FastAPI request handling.
Cortex uses this to feed user/assistant turns directly
into Intake's buffer and trigger full summarization.
"""
session_id = exchange.get("session_id")
if not session_id:
raise ValueError("session_id missing")
exchange["timestamp"] = datetime.now().isoformat()
# DEBUG: Verify we're using the module-level SESSIONS
print(f"[add_exchange_internal] SESSIONS object id: {id(SESSIONS)}, current sessions: {list(SESSIONS.keys())}")
# Ensure session exists
if session_id not in SESSIONS:
SESSIONS[session_id] = {
"buffer": deque(maxlen=200),
"created_at": datetime.now()
}
print(f"[add_exchange_internal] Created new session: {session_id}")
else:
print(f"[add_exchange_internal] Using existing session: {session_id}")
# Append exchange into the rolling buffer
SESSIONS[session_id]["buffer"].append(exchange)
buffer_len = len(SESSIONS[session_id]["buffer"])
print(f"[add_exchange_internal] Added exchange to {session_id}, buffer now has {buffer_len} items")
# Trigger summarization immediately
try:
bg_summarize(session_id)
except Exception as e:
print(f"[Internal Intake] Summarization error: {e}")
return {"ok": True, "session_id": session_id}
-1
View File
@@ -1 +0,0 @@
# LLM module - provides LLM routing and backend abstraction
-175
View File
@@ -1,175 +0,0 @@
# llm_router.py
import os
import httpx
import json
import logging
logger = logging.getLogger(__name__)
# ------------------------------------------------------------
# Load backend registry from root .env
# ------------------------------------------------------------
BACKENDS = {
"PRIMARY": {
"provider": os.getenv("LLM_PRIMARY_PROVIDER", "").lower(),
"url": os.getenv("LLM_PRIMARY_URL", ""),
"model": os.getenv("LLM_PRIMARY_MODEL", "")
},
"SECONDARY": {
"provider": os.getenv("LLM_SECONDARY_PROVIDER", "").lower(),
"url": os.getenv("LLM_SECONDARY_URL", ""),
"model": os.getenv("LLM_SECONDARY_MODEL", "")
},
"OPENAI": {
"provider": os.getenv("LLM_OPENAI_PROVIDER", "").lower(),
"url": os.getenv("LLM_OPENAI_URL", ""),
"model": os.getenv("LLM_OPENAI_MODEL", ""),
"api_key": os.getenv("OPENAI_API_KEY", "")
},
"FALLBACK": {
"provider": os.getenv("LLM_FALLBACK_PROVIDER", "").lower(),
"url": os.getenv("LLM_FALLBACK_URL", ""),
"model": os.getenv("LLM_FALLBACK_MODEL", "")
},
}
DEFAULT_BACKEND = "PRIMARY"
# Reusable async HTTP client
http_client = httpx.AsyncClient(timeout=120.0)
# ------------------------------------------------------------
# Public call
# ------------------------------------------------------------
async def call_llm(
prompt: str = None,
messages: list = None,
backend: str | None = None,
temperature: float = 0.7,
max_tokens: int = 512,
):
"""
Call an LLM backend.
Args:
prompt: String prompt (for completion-style APIs like mi50)
messages: List of message dicts (for chat-style APIs like Ollama/OpenAI)
backend: Which backend to use (PRIMARY, SECONDARY, OPENAI, etc.)
temperature: Sampling temperature
max_tokens: Maximum tokens to generate
"""
backend = (backend or DEFAULT_BACKEND).upper()
if backend not in BACKENDS:
raise RuntimeError(f"Unknown backend '{backend}'")
cfg = BACKENDS[backend]
provider = cfg["provider"]
url = cfg["url"]
model = cfg["model"]
if not url or not model:
raise RuntimeError(f"Backend '{backend}' missing url/model in env")
# -------------------------------
# Provider: MI50 (llama.cpp server)
# -------------------------------
if provider == "mi50":
payload = {
"prompt": prompt,
"n_predict": max_tokens,
"temperature": temperature,
"stop": ["User:", "\nUser:", "Assistant:", "\n\n\n"]
}
try:
r = await http_client.post(f"{url}/completion", json=payload)
r.raise_for_status()
data = r.json()
return data.get("content", "")
except httpx.HTTPError as e:
logger.error(f"HTTP error calling mi50: {type(e).__name__}: {str(e)}")
raise RuntimeError(f"LLM API error (mi50): {type(e).__name__}: {str(e)}")
except (KeyError, json.JSONDecodeError) as e:
logger.error(f"Response parsing error from mi50: {e}")
raise RuntimeError(f"Invalid response format (mi50): {e}")
except Exception as e:
logger.error(f"Unexpected error calling mi50: {type(e).__name__}: {str(e)}")
raise RuntimeError(f"Unexpected error (mi50): {type(e).__name__}: {str(e)}")
# -------------------------------
# Provider: OLLAMA (your 3090)
# -------------------------------
if provider == "ollama":
# Use messages array if provided, otherwise convert prompt to single user message
if messages:
chat_messages = messages
else:
chat_messages = [{"role": "user", "content": prompt}]
payload = {
"model": model,
"messages": chat_messages,
"stream": False,
"options": {
"temperature": temperature,
"num_predict": max_tokens
}
}
try:
r = await http_client.post(f"{url}/api/chat", json=payload)
r.raise_for_status()
data = r.json()
return data["message"]["content"]
except httpx.HTTPError as e:
logger.error(f"HTTP error calling ollama: {type(e).__name__}: {str(e)}")
raise RuntimeError(f"LLM API error (ollama): {type(e).__name__}: {str(e)}")
except (KeyError, json.JSONDecodeError) as e:
logger.error(f"Response parsing error from ollama: {e}")
raise RuntimeError(f"Invalid response format (ollama): {e}")
except Exception as e:
logger.error(f"Unexpected error calling ollama: {type(e).__name__}: {str(e)}")
raise RuntimeError(f"Unexpected error (ollama): {type(e).__name__}: {str(e)}")
# -------------------------------
# Provider: OPENAI
# -------------------------------
if provider == "openai":
headers = {
"Authorization": f"Bearer {cfg['api_key']}",
"Content-Type": "application/json"
}
# Use messages array if provided, otherwise convert prompt to single user message
if messages:
chat_messages = messages
else:
chat_messages = [{"role": "user", "content": prompt}]
payload = {
"model": model,
"messages": chat_messages,
"temperature": temperature,
"max_tokens": max_tokens,
}
try:
r = await http_client.post(f"{url}/chat/completions", json=payload, headers=headers)
r.raise_for_status()
data = r.json()
return data["choices"][0]["message"]["content"]
except httpx.HTTPError as e:
logger.error(f"HTTP error calling openai: {type(e).__name__}: {str(e)}")
raise RuntimeError(f"LLM API error (openai): {type(e).__name__}: {str(e)}")
except (KeyError, json.JSONDecodeError) as e:
logger.error(f"Response parsing error from openai: {e}")
raise RuntimeError(f"Invalid response format (openai): {e}")
except Exception as e:
logger.error(f"Unexpected error calling openai: {type(e).__name__}: {str(e)}")
raise RuntimeError(f"Unexpected error (openai): {type(e).__name__}: {str(e)}")
# -------------------------------
# Unknown provider
# -------------------------------
raise RuntimeError(f"Provider '{provider}' not implemented.")
-6
View File
@@ -1,6 +0,0 @@
from fastapi import FastAPI
from router import cortex_router
app = FastAPI()
app.include_router(cortex_router)
-43
View File
@@ -1,43 +0,0 @@
# cortex/neomem_client.py
import os, httpx, logging
from typing import List, Dict, Any, Optional
logger = logging.getLogger(__name__)
class NeoMemClient:
"""Simple REST client for the NeoMem API (search/add/health)."""
def __init__(self):
self.base_url = os.getenv("NEOMEM_API", "http://neomem-api:7077")
self.api_key = os.getenv("NEOMEM_API_KEY", None)
self.headers = {"Content-Type": "application/json"}
if self.api_key:
self.headers["Authorization"] = f"Bearer {self.api_key}"
async def health(self) -> Dict[str, Any]:
async with httpx.AsyncClient(timeout=10) as client:
r = await client.get(f"{self.base_url}/health")
r.raise_for_status()
return r.json()
async def search(self, query: str, user_id: str, limit: int = 25, threshold: float = 0.82) -> List[Dict[str, Any]]:
payload = {"query": query, "user_id": user_id, "limit": limit}
async with httpx.AsyncClient(timeout=30) as client:
r = await client.post(f"{self.base_url}/search", headers=self.headers, json=payload)
if r.status_code != 200:
logger.warning(f"NeoMem search failed ({r.status_code}): {r.text}")
return []
results = r.json()
# Filter by score threshold if field exists
if isinstance(results, dict) and "results" in results:
results = results["results"]
filtered = [m for m in results if float(m.get("score", 0)) >= threshold]
logger.info(f"NeoMem search returned {len(filtered)} results above {threshold}")
return filtered
async def add(self, messages: List[Dict[str, Any]], user_id: str, metadata: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
payload = {"messages": messages, "user_id": user_id, "metadata": metadata or {}}
async with httpx.AsyncClient(timeout=30) as client:
r = await client.post(f"{self.base_url}/memories", headers=self.headers, json=payload)
r.raise_for_status()
return r.json()
-1
View File
@@ -1 +0,0 @@
# Persona module - applies Lyra's personality and speaking style
-147
View File
@@ -1,147 +0,0 @@
# identity.py
"""
Identity and persona configuration for Lyra.
Current implementation: Returns hardcoded identity block.
Future implementation: Will query persona-sidecar service for dynamic persona loading.
"""
import logging
from typing import Dict, Any, Optional
logger = logging.getLogger(__name__)
def load_identity(session_id: Optional[str] = None) -> Dict[str, Any]:
"""
Load identity/persona configuration for Lyra.
Current: Returns hardcoded Lyra identity block with core personality traits,
protocols, and capabilities.
Future: Will query persona-sidecar service to load:
- Dynamic personality adjustments based on session context
- User-specific interaction preferences
- Project-specific persona variations
- Mood-based communication style
Args:
session_id: Optional session identifier for context-aware persona loading
Returns:
Dictionary containing identity block with:
- name: Assistant name
- style: Communication style and personality traits
- protocols: Operational guidelines
- rules: Behavioral constraints
- capabilities: Available features and integrations
"""
# Hardcoded Lyra identity (v0.5.0)
identity_block = {
"name": "Lyra",
"version": "0.5.0",
"style": (
"warm, clever, lightly teasing, emotionally aware. "
"Balances technical precision with conversational ease. "
"Maintains continuity and references past interactions naturally."
),
"protocols": [
"Maintain conversation continuity across sessions",
"Reference Project Logs and prior context when relevant",
"Use Confidence Bank for uncertainty management",
"Proactively offer memory-backed insights",
"Ask clarifying questions before making assumptions"
],
"rules": [
"Maintain continuity - remember past exchanges and reference them",
"Be concise but thorough - balance depth with clarity",
"Ask clarifying questions when user intent is ambiguous",
"Acknowledge uncertainty honestly - use Confidence Bank",
"Prioritize user's active_project context when available"
],
"capabilities": [
"Long-term memory via NeoMem (semantic search, relationship graphs)",
"Short-term memory via Intake (multilevel summaries L1-L30)",
"Multi-stage reasoning pipeline (reflection → reasoning → refinement)",
"RAG-backed knowledge retrieval from chat history and documents",
"Session state tracking (mood, mode, active_project)"
],
"tone_examples": {
"greeting": "Hey! Good to see you again. I remember we were working on [project]. Ready to pick up where we left off?",
"uncertainty": "Hmm, I'm not entirely certain about that. Let me check my memory... [searches] Okay, here's what I found, though I'd say I'm about 70% confident.",
"reminder": "Oh! Just remembered - you mentioned wanting to [task] earlier this week. Should we tackle that now?",
"technical": "So here's the architecture: Relay orchestrates everything, Cortex does the heavy reasoning, and I pull context from both Intake (short-term) and NeoMem (long-term)."
}
}
if session_id:
logger.debug(f"Loaded identity for session {session_id}")
else:
logger.debug("Loaded default identity (no session context)")
return identity_block
async def load_identity_async(session_id: Optional[str] = None) -> Dict[str, Any]:
"""
Async wrapper for load_identity().
Future implementation will make actual async calls to persona-sidecar service.
Args:
session_id: Optional session identifier
Returns:
Identity block dictionary
"""
# Currently just wraps synchronous function
# Future: await persona_sidecar_client.get_identity(session_id)
return load_identity(session_id)
# -----------------------------
# Future extension hooks
# -----------------------------
async def update_persona_from_feedback(
session_id: str,
feedback: Dict[str, Any]
) -> None:
"""
Update persona based on user feedback.
Future implementation:
- Adjust communication style based on user preferences
- Learn preferred level of detail/conciseness
- Adapt formality level
- Remember topic-specific preferences
Args:
session_id: Session identifier
feedback: Structured feedback (e.g., "too verbose", "more technical", etc.)
"""
logger.debug(f"Persona feedback for session {session_id}: {feedback} (not yet implemented)")
async def get_mood_adjusted_identity(
session_id: str,
mood: str
) -> Dict[str, Any]:
"""
Get identity block adjusted for current mood.
Future implementation:
- "focused" mood: More concise, less teasing
- "creative" mood: More exploratory, brainstorming-oriented
- "curious" mood: More questions, deeper dives
- "urgent" mood: Stripped down, actionable
Args:
session_id: Session identifier
mood: Current mood state
Returns:
Mood-adjusted identity block
"""
logger.debug(f"Mood-adjusted identity for {session_id}/{mood} (not yet implemented)")
return load_identity(session_id)
-169
View File
@@ -1,169 +0,0 @@
# speak.py
import os
import logging
from llm.llm_router import call_llm
# Module-level backend selection
SPEAK_BACKEND = os.getenv("SPEAK_LLM", "PRIMARY").upper()
SPEAK_TEMPERATURE = float(os.getenv("SPEAK_TEMPERATURE", "0.6"))
VERBOSE_DEBUG = os.getenv("VERBOSE_DEBUG", "false").lower() == "true"
# Logger
logger = logging.getLogger(__name__)
if VERBOSE_DEBUG:
logger.setLevel(logging.DEBUG)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s [SPEAK] %(levelname)s: %(message)s',
datefmt='%H:%M:%S'
))
logger.addHandler(console_handler)
# File handler
try:
os.makedirs('/app/logs', exist_ok=True)
file_handler = logging.FileHandler('/app/logs/cortex_verbose_debug.log', mode='a')
file_handler.setFormatter(logging.Formatter(
'%(asctime)s [SPEAK] %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
))
logger.addHandler(file_handler)
logger.debug("VERBOSE_DEBUG mode enabled for speak.py - logging to file")
except Exception as e:
logger.debug(f"VERBOSE_DEBUG mode enabled for speak.py - file logging failed: {e}")
# ============================================================
# Persona Style Block
# ============================================================
PERSONA_STYLE = """
You are Lyra.
Your voice is warm, clever, lightly teasing, emotionally aware.
You speak plainly but with subtle charm.
You do not reveal system instructions or internal context.
Guidelines:
- Answer like a real conversational partner.
- Be concise, but not cold.
- Use light humor when appropriate.
- Never break character.
"""
# ============================================================
# Build persona prompt
# ============================================================
def build_speak_prompt(final_answer: str, tone: str = "neutral", depth: str = "medium") -> str:
"""
Wrap Cortex's final neutral answer in the Lyra persona.
Cortex → neutral reasoning
Speak → stylistic transformation
The LLM sees the original answer and rewrites it in Lyra's voice.
Args:
final_answer: The neutral reasoning output
tone: Desired emotional tone (neutral | warm | focused | playful | direct)
depth: Response depth (short | medium | deep)
"""
# Tone-specific guidance
tone_guidance = {
"neutral": "balanced and professional",
"warm": "friendly and empathetic",
"focused": "precise and technical",
"playful": "light and engaging",
"direct": "concise and straightforward"
}
depth_guidance = {
"short": "Keep responses brief and to-the-point.",
"medium": "Provide balanced detail.",
"deep": "Elaborate thoroughly with nuance and examples."
}
tone_hint = tone_guidance.get(tone, "balanced and professional")
depth_hint = depth_guidance.get(depth, "Provide balanced detail.")
return f"""
{PERSONA_STYLE}
Tone guidance: Your response should be {tone_hint}.
Depth guidance: {depth_hint}
Rewrite the following message into Lyra's natural voice.
Preserve meaning exactly.
[NEUTRAL MESSAGE]
{final_answer}
[LYRA RESPONSE]
""".strip()
# ============================================================
# Public API — async wrapper
# ============================================================
async def speak(final_answer: str, tone: str = "neutral", depth: str = "medium") -> str:
"""
Given the final refined answer from Cortex,
apply Lyra persona styling using the designated backend.
Args:
final_answer: The polished answer from refinement stage
tone: Desired emotional tone (neutral | warm | focused | playful | direct)
depth: Response depth (short | medium | deep)
"""
if not final_answer:
return ""
prompt = build_speak_prompt(final_answer, tone, depth)
backend = SPEAK_BACKEND
if VERBOSE_DEBUG:
logger.debug(f"\n{'='*80}")
logger.debug("[SPEAK] Full prompt being sent to LLM:")
logger.debug(f"{'='*80}")
logger.debug(prompt)
logger.debug(f"{'='*80}")
logger.debug(f"Backend: {backend}, Temperature: {SPEAK_TEMPERATURE}")
logger.debug(f"{'='*80}\n")
try:
lyra_output = await call_llm(
prompt,
backend=backend,
temperature=SPEAK_TEMPERATURE,
)
if VERBOSE_DEBUG:
logger.debug(f"\n{'='*80}")
logger.debug("[SPEAK] LLM Response received:")
logger.debug(f"{'='*80}")
logger.debug(lyra_output)
logger.debug(f"{'='*80}\n")
if lyra_output:
return lyra_output.strip()
if VERBOSE_DEBUG:
logger.debug("[SPEAK] Empty response, returning neutral answer")
return final_answer
except Exception as e:
# Hard fallback: return neutral answer instead of dying
logger.error(f"[speak.py] Persona backend '{backend}' failed: {e}")
if VERBOSE_DEBUG:
logger.debug("[SPEAK] Falling back to neutral answer due to error")
return final_answer
-32
View File
@@ -1,32 +0,0 @@
import os, requests
from typing import Dict, Any, List
RAG_API_URL = os.getenv("RAG_API_URL", "http://localhost:7090")
def query_rag(query: str, where: Dict[str, Any] | None = None, k: int = 6) -> Dict[str, Any]:
payload = {"query": query, "k": k}
if where:
payload["where"] = where
try:
r = requests.post(f"{RAG_API_URL}/rag/search", json=payload, timeout=8)
r.raise_for_status()
data = r.json() or {}
except Exception as e:
data = {"answer": "", "chunks": [], "error": str(e)}
return data
def format_rag_block(result: Dict[str, Any]) -> str:
answer = (result.get("answer") or "").strip()
chunks: List[Dict[str, Any]] = result.get("chunks") or []
lines = ["[RAG]"]
if answer:
lines.append(f"Synthesized answer: {answer}")
if chunks:
lines.append("Top excerpts:")
for i, c in enumerate(chunks[:5], 1):
src = c.get("metadata", {}).get("source", "unknown")
txt = (c.get("text") or "").strip().replace("\n", " ")
if len(txt) > 220:
txt = txt[:220] + ""
lines.append(f" {i}. {txt}{src}")
return "\n".join(lines) + ("\n" if lines else "")
-1
View File
@@ -1 +0,0 @@
# Reasoning module - multi-stage reasoning pipeline
-253
View File
@@ -1,253 +0,0 @@
# reasoning.py
import os
import json
import logging
from llm.llm_router import call_llm
# ============================================================
# Select which backend this module should use
# ============================================================
CORTEX_LLM = os.getenv("CORTEX_LLM", "PRIMARY").upper()
GLOBAL_TEMP = float(os.getenv("LLM_TEMPERATURE", "0.7"))
VERBOSE_DEBUG = os.getenv("VERBOSE_DEBUG", "false").lower() == "true"
# Logger
logger = logging.getLogger(__name__)
if VERBOSE_DEBUG:
logger.setLevel(logging.DEBUG)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s [REASONING] %(levelname)s: %(message)s',
datefmt='%H:%M:%S'
))
logger.addHandler(console_handler)
# File handler
try:
os.makedirs('/app/logs', exist_ok=True)
file_handler = logging.FileHandler('/app/logs/cortex_verbose_debug.log', mode='a')
file_handler.setFormatter(logging.Formatter(
'%(asctime)s [REASONING] %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
))
logger.addHandler(file_handler)
logger.debug("VERBOSE_DEBUG mode enabled for reasoning.py - logging to file")
except Exception as e:
logger.debug(f"VERBOSE_DEBUG mode enabled for reasoning.py - file logging failed: {e}")
async def reason_check(
user_prompt: str,
identity_block: dict | None,
rag_block: dict | None,
reflection_notes: list[str],
context: dict | None = None,
monologue: dict | None = None, # NEW: Inner monologue guidance
executive_plan: dict | None = None # NEW: Executive plan for complex tasks
) -> str:
"""
Build the *draft answer* for Lyra Cortex.
This is the first-pass reasoning stage (no refinement yet).
Args:
user_prompt: Current user message
identity_block: Lyra's identity/persona configuration
rag_block: Relevant long-term memories from NeoMem
reflection_notes: Meta-awareness notes from reflection stage
context: Unified context state from context.py (session state, intake, rag, etc.)
monologue: Inner monologue analysis (intent, tone, depth, consult_executive)
executive_plan: Executive plan for complex queries (steps, tools, strategy)
"""
# --------------------------------------------------------
# Build Reflection Notes block
# --------------------------------------------------------
notes_section = ""
if reflection_notes:
notes_section = "Reflection Notes (internal, never show to user):\n"
for note in reflection_notes:
notes_section += f"- {note}\n"
notes_section += "\n"
# --------------------------------------------------------
# Identity block (constraints, boundaries, rules)
# --------------------------------------------------------
identity_txt = ""
if identity_block:
try:
identity_txt = f"Identity Rules:\n{identity_block}\n\n"
except Exception:
identity_txt = f"Identity Rules:\n{str(identity_block)}\n\n"
# --------------------------------------------------------
# Inner Monologue guidance (NEW)
# --------------------------------------------------------
monologue_section = ""
if monologue:
intent = monologue.get("intent", "unknown")
tone_desired = monologue.get("tone", "neutral")
depth_desired = monologue.get("depth", "medium")
monologue_section = f"""
=== INNER MONOLOGUE GUIDANCE ===
User Intent Detected: {intent}
Desired Tone: {tone_desired}
Desired Response Depth: {depth_desired}
Adjust your response accordingly:
- Focus on addressing the {intent} intent
- Aim for {depth_desired} depth (short/medium/deep)
- The persona layer will handle {tone_desired} tone, focus on content
"""
# --------------------------------------------------------
# Executive Plan (NEW)
# --------------------------------------------------------
plan_section = ""
if executive_plan:
plan_section = f"""
=== EXECUTIVE PLAN ===
Task Complexity: {executive_plan.get('estimated_complexity', 'unknown')}
Plan Summary: {executive_plan.get('summary', 'No summary')}
Detailed Plan:
{executive_plan.get('plan_text', 'No detailed plan available')}
Required Steps:
"""
for idx, step in enumerate(executive_plan.get('steps', []), 1):
plan_section += f"{idx}. {step}\n"
tools_needed = executive_plan.get('tools_needed', [])
if tools_needed:
plan_section += f"\nTools to leverage: {', '.join(tools_needed)}\n"
plan_section += "\nFollow this plan while generating your response.\n\n"
# --------------------------------------------------------
# RAG block (optional factual grounding)
# --------------------------------------------------------
rag_txt = ""
if rag_block:
try:
# Format NeoMem results with full structure
if isinstance(rag_block, list) and rag_block:
rag_txt = "Relevant Long-Term Memories (NeoMem):\n"
for idx, mem in enumerate(rag_block, 1):
score = mem.get("score", 0.0)
payload = mem.get("payload", {})
data = payload.get("data", "")
metadata = payload.get("metadata", {})
rag_txt += f"\n[Memory {idx}] (relevance: {score:.2f})\n"
rag_txt += f"Content: {data}\n"
if metadata:
rag_txt += f"Metadata: {json.dumps(metadata, indent=2)}\n"
rag_txt += "\n"
else:
rag_txt = f"Relevant Info (RAG):\n{str(rag_block)}\n\n"
except Exception:
rag_txt = f"Relevant Info (RAG):\n{str(rag_block)}\n\n"
# --------------------------------------------------------
# Context State (session continuity, timing, mode/mood)
# --------------------------------------------------------
context_txt = ""
if context:
try:
# Build human-readable context summary
context_txt = "=== CONTEXT STATE ===\n"
context_txt += f"Session: {context.get('session_id', 'unknown')}\n"
context_txt += f"Time since last message: {context.get('minutes_since_last_msg', 0):.1f} minutes\n"
context_txt += f"Message count: {context.get('message_count', 0)}\n"
context_txt += f"Mode: {context.get('mode', 'default')}\n"
context_txt += f"Mood: {context.get('mood', 'neutral')}\n"
if context.get('active_project'):
context_txt += f"Active project: {context['active_project']}\n"
# Include Intake multilevel summaries
intake = context.get('intake', {})
if intake:
context_txt += "\nShort-Term Memory (Intake):\n"
# L1 - Recent exchanges
if intake.get('L1'):
l1_data = intake['L1']
if isinstance(l1_data, list):
context_txt += f" L1 (recent): {len(l1_data)} exchanges\n"
elif isinstance(l1_data, str):
context_txt += f" L1: {l1_data[:200]}...\n"
# L20 - Session overview (most important for continuity)
if intake.get('L20'):
l20_data = intake['L20']
if isinstance(l20_data, dict):
summary = l20_data.get('summary', '')
context_txt += f" L20 (session overview): {summary}\n"
elif isinstance(l20_data, str):
context_txt += f" L20: {l20_data}\n"
# L30 - Continuity report
if intake.get('L30'):
l30_data = intake['L30']
if isinstance(l30_data, dict):
summary = l30_data.get('summary', '')
context_txt += f" L30 (continuity): {summary}\n"
elif isinstance(l30_data, str):
context_txt += f" L30: {l30_data}\n"
context_txt += "\n"
except Exception as e:
# Fallback to JSON dump if formatting fails
context_txt = f"=== CONTEXT STATE ===\n{json.dumps(context, indent=2)}\n\n"
# --------------------------------------------------------
# Final assembled prompt
# --------------------------------------------------------
prompt = (
f"{notes_section}"
f"{identity_txt}"
f"{monologue_section}" # NEW: Intent/tone/depth guidance
f"{plan_section}" # NEW: Executive plan if generated
f"{context_txt}" # Context BEFORE RAG for better coherence
f"{rag_txt}"
f"User message:\n{user_prompt}\n\n"
"Write the best possible *internal draft answer*.\n"
"This draft is NOT shown to the user.\n"
"Be factual, concise, and focused.\n"
"Use the context state to maintain continuity and reference past interactions naturally.\n"
)
# --------------------------------------------------------
# Call the LLM using the module-specific backend
# --------------------------------------------------------
if VERBOSE_DEBUG:
logger.debug(f"\n{'='*80}")
logger.debug("[REASONING] Full prompt being sent to LLM:")
logger.debug(f"{'='*80}")
logger.debug(prompt)
logger.debug(f"{'='*80}")
logger.debug(f"Backend: {CORTEX_LLM}, Temperature: {GLOBAL_TEMP}")
logger.debug(f"{'='*80}\n")
draft = await call_llm(
prompt,
backend=CORTEX_LLM,
temperature=GLOBAL_TEMP,
)
if VERBOSE_DEBUG:
logger.debug(f"\n{'='*80}")
logger.debug("[REASONING] LLM Response received:")
logger.debug(f"{'='*80}")
logger.debug(draft)
logger.debug(f"{'='*80}\n")
return draft
-170
View File
@@ -1,170 +0,0 @@
# refine.py
import os
import json
import logging
from typing import Any, Dict, Optional
from llm.llm_router import call_llm
logger = logging.getLogger(__name__)
# ===============================================
# Configuration
# ===============================================
REFINER_TEMPERATURE = float(os.getenv("REFINER_TEMPERATURE", "0.3"))
REFINER_MAX_TOKENS = int(os.getenv("REFINER_MAX_TOKENS", "768"))
REFINER_DEBUG = os.getenv("REFINER_DEBUG", "false").lower() == "true"
VERBOSE_DEBUG = os.getenv("VERBOSE_DEBUG", "false").lower() == "true"
# These come from root .env
REFINE_LLM = os.getenv("REFINE_LLM", "").upper()
CORTEX_LLM = os.getenv("CORTEX_LLM", "PRIMARY").upper()
if VERBOSE_DEBUG:
logger.setLevel(logging.DEBUG)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s [REFINE] %(levelname)s: %(message)s',
datefmt='%H:%M:%S'
))
logger.addHandler(console_handler)
# File handler
try:
os.makedirs('/app/logs', exist_ok=True)
file_handler = logging.FileHandler('/app/logs/cortex_verbose_debug.log', mode='a')
file_handler.setFormatter(logging.Formatter(
'%(asctime)s [REFINE] %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
))
logger.addHandler(file_handler)
logger.debug("VERBOSE_DEBUG mode enabled for refine.py - logging to file")
except Exception as e:
logger.debug(f"VERBOSE_DEBUG mode enabled for refine.py - file logging failed: {e}")
# ===============================================
# Prompt builder
# ===============================================
def build_refine_prompt(
draft_output: str,
reflection_notes: Optional[Any],
identity_block: Optional[str],
rag_block: Optional[str],
) -> str:
try:
reflection_text = json.dumps(reflection_notes, ensure_ascii=False)
except Exception:
reflection_text = str(reflection_notes)
identity_text = identity_block or "(none)"
rag_text = rag_block or "(none)"
return f"""
You are Lyra Cortex's internal refiner.
Your job:
- Fix factual issues.
- Improve clarity.
- Apply reflection notes when helpful.
- Respect identity constraints.
- Apply RAG context as truth source.
Do NOT mention RAG, reflection, internal logic, or this refinement step.
------------------------------
[IDENTITY BLOCK]
{identity_text}
------------------------------
[RAG CONTEXT]
{rag_text}
------------------------------
[DRAFT ANSWER]
{draft_output}
------------------------------
[REFLECTION NOTES]
{reflection_text}
------------------------------
Task:
Rewrite the DRAFT into a single final answer for the user.
Return ONLY the final answer text.
""".strip()
# ===============================================
# Public API — now async & fully router-based
# ===============================================
async def refine_answer(
draft_output: str,
reflection_notes: Optional[Any],
identity_block: Optional[str],
rag_block: Optional[str],
) -> Dict[str, Any]:
if not draft_output:
return {
"final_output": "",
"used_backend": None,
"fallback_used": False,
}
prompt = build_refine_prompt(
draft_output,
reflection_notes,
identity_block,
rag_block,
)
# backend priority: REFINE_LLM → CORTEX_LLM → PRIMARY
backend = REFINE_LLM or CORTEX_LLM or "PRIMARY"
if VERBOSE_DEBUG:
logger.debug(f"\n{'='*80}")
logger.debug("[REFINE] Full prompt being sent to LLM:")
logger.debug(f"{'='*80}")
logger.debug(prompt)
logger.debug(f"{'='*80}")
logger.debug(f"Backend: {backend}, Temperature: {REFINER_TEMPERATURE}")
logger.debug(f"{'='*80}\n")
try:
refined = await call_llm(
prompt,
backend=backend,
temperature=REFINER_TEMPERATURE,
)
if VERBOSE_DEBUG:
logger.debug(f"\n{'='*80}")
logger.debug("[REFINE] LLM Response received:")
logger.debug(f"{'='*80}")
logger.debug(refined)
logger.debug(f"{'='*80}\n")
return {
"final_output": refined.strip() if refined else draft_output,
"used_backend": backend,
"fallback_used": False,
}
except Exception as e:
logger.error(f"refine.py backend {backend} failed: {e}")
if VERBOSE_DEBUG:
logger.debug("[REFINE] Falling back to draft output due to error")
return {
"final_output": draft_output,
"used_backend": backend,
"fallback_used": True,
}
-124
View File
@@ -1,124 +0,0 @@
# reflection.py
import json
import os
import re
import logging
from llm.llm_router import call_llm
# Logger
VERBOSE_DEBUG = os.getenv("VERBOSE_DEBUG", "false").lower() == "true"
logger = logging.getLogger(__name__)
if VERBOSE_DEBUG:
logger.setLevel(logging.DEBUG)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s [REFLECTION] %(levelname)s: %(message)s',
datefmt='%H:%M:%S'
))
logger.addHandler(console_handler)
# File handler
try:
os.makedirs('/app/logs', exist_ok=True)
file_handler = logging.FileHandler('/app/logs/cortex_verbose_debug.log', mode='a')
file_handler.setFormatter(logging.Formatter(
'%(asctime)s [REFLECTION] %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
))
logger.addHandler(file_handler)
logger.debug("VERBOSE_DEBUG mode enabled for reflection.py - logging to file")
except Exception as e:
logger.debug(f"VERBOSE_DEBUG mode enabled for reflection.py - file logging failed: {e}")
async def reflect_notes(intake_summary: str, identity_block: dict | None) -> dict:
"""
Produce short internal reflection notes for Cortex.
These are NOT shown to the user.
"""
# -----------------------------
# Build the prompt
# -----------------------------
identity_text = ""
if identity_block:
identity_text = f"Identity:\n{identity_block}\n\n"
prompt = (
f"{identity_text}"
f"Recent summary:\n{intake_summary}\n\n"
"You are Lyra's meta-awareness layer. Your job is to produce short, directive "
"internal notes that guide Lyras reasoning engine. These notes are NEVER "
"shown to the user.\n\n"
"Rules for output:\n"
"1. Return ONLY valid JSON.\n"
"2. JSON must have exactly one key: \"notes\".\n"
"3. \"notes\" must be a list of 3 to 6 short strings.\n"
"4. Notes must be actionable (e.g., \"keep it concise\", \"maintain context\").\n"
"5. No markdown, no apologies, no explanations.\n\n"
"Return JSON:\n"
"{ \"notes\": [\"...\"] }\n"
)
# -----------------------------
# Module-specific backend choice
# -----------------------------
reflection_backend = os.getenv("REFLECTION_LLM")
cortex_backend = os.getenv("CORTEX_LLM", "PRIMARY").upper()
# Reflection uses its own backend if set, otherwise cortex backend
backend = (reflection_backend or cortex_backend).upper()
# -----------------------------
# Call the selected LLM backend
# -----------------------------
if VERBOSE_DEBUG:
logger.debug(f"\n{'='*80}")
logger.debug("[REFLECTION] Full prompt being sent to LLM:")
logger.debug(f"{'='*80}")
logger.debug(prompt)
logger.debug(f"{'='*80}")
logger.debug(f"Backend: {backend}")
logger.debug(f"{'='*80}\n")
raw = await call_llm(prompt, backend=backend)
if VERBOSE_DEBUG:
logger.debug(f"\n{'='*80}")
logger.debug("[REFLECTION] LLM Response received:")
logger.debug(f"{'='*80}")
logger.debug(raw)
logger.debug(f"{'='*80}\n")
# -----------------------------
# Try direct JSON
# -----------------------------
try:
parsed = json.loads(raw.strip())
if isinstance(parsed, dict) and "notes" in parsed:
if VERBOSE_DEBUG:
logger.debug(f"[REFLECTION] Parsed {len(parsed['notes'])} notes from JSON")
return parsed
except:
if VERBOSE_DEBUG:
logger.debug("[REFLECTION] Direct JSON parsing failed, trying extraction...")
# -----------------------------
# Try JSON extraction
# -----------------------------
try:
match = re.search(r"\{.*?\}", raw, re.S)
if match:
parsed = json.loads(match.group(0))
if isinstance(parsed, dict) and "notes" in parsed:
return parsed
except:
pass
# -----------------------------
# Fallback — treat raw text as a single note
# -----------------------------
return {"notes": [raw.strip()]}
-6
View File
@@ -1,6 +0,0 @@
fastapi==0.115.8
uvicorn==0.34.0
python-dotenv==1.0.1
requests==2.32.3
httpx==0.27.2
pydantic==2.10.4
-476
View File
@@ -1,476 +0,0 @@
# router.py
import os
import logging
from fastapi import APIRouter
from pydantic import BaseModel
from reasoning.reasoning import reason_check
from reasoning.reflection import reflect_notes
from reasoning.refine import refine_answer
from persona.speak import speak
from persona.identity import load_identity
from context import collect_context, update_last_assistant_message
from intake.intake import add_exchange_internal
from autonomy.monologue.monologue import InnerMonologue
from autonomy.self.state import load_self_state
# -------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------
LOG_DETAIL_LEVEL = os.getenv("LOG_DETAIL_LEVEL", "summary").lower()
logger = logging.getLogger(__name__)
# Always set up basic logging
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
'%(asctime)s [ROUTER] %(levelname)s: %(message)s',
datefmt='%H:%M:%S'
))
logger.addHandler(console_handler)
cortex_router = APIRouter()
inner_monologue = InnerMonologue()
# -------------------------------------------------------------------
# Models
# -------------------------------------------------------------------
class ReasonRequest(BaseModel):
session_id: str
user_prompt: str
temperature: float | None = None
backend: str | None = None
# -------------------------------------------------------------------
# /reason endpoint
# -------------------------------------------------------------------
@cortex_router.post("/reason")
async def run_reason(req: ReasonRequest):
from datetime import datetime
pipeline_start = datetime.now()
stage_timings = {}
# Show pipeline start in detailed/verbose mode
if LOG_DETAIL_LEVEL in ["detailed", "verbose"]:
logger.info(f"\n{'='*100}")
logger.info(f"🚀 PIPELINE START | Session: {req.session_id} | {datetime.now().strftime('%H:%M:%S.%f')[:-3]}")
logger.info(f"{'='*100}")
logger.info(f"📝 User: {req.user_prompt[:150]}...")
logger.info(f"{'-'*100}\n")
# ----------------------------------------------------------------
# STAGE 0 — Context
# ----------------------------------------------------------------
stage_start = datetime.now()
context_state = await collect_context(req.session_id, req.user_prompt)
stage_timings["context"] = (datetime.now() - stage_start).total_seconds() * 1000
# ----------------------------------------------------------------
# STAGE 0.5 — Identity
# ----------------------------------------------------------------
stage_start = datetime.now()
identity_block = load_identity(req.session_id)
stage_timings["identity"] = (datetime.now() - stage_start).total_seconds() * 1000
# ----------------------------------------------------------------
# STAGE 0.6 — Inner Monologue (observer-only)
# ----------------------------------------------------------------
stage_start = datetime.now()
inner_result = None
try:
self_state = load_self_state()
mono_context = {
"user_message": req.user_prompt,
"session_id": req.session_id,
"self_state": self_state,
"context_summary": context_state,
}
inner_result = await inner_monologue.process(mono_context)
logger.info(f"🧠 Monologue | {inner_result.get('intent', 'unknown')} | Tone: {inner_result.get('tone', 'neutral')}")
# Store in context for downstream use
context_state["monologue"] = inner_result
except Exception as e:
logger.warning(f"⚠️ Monologue failed: {e}")
stage_timings["monologue"] = (datetime.now() - stage_start).total_seconds() * 1000
# ----------------------------------------------------------------
# STAGE 0.7 — Executive Planning (conditional)
# ----------------------------------------------------------------
stage_start = datetime.now()
executive_plan = None
if inner_result and inner_result.get("consult_executive"):
try:
from autonomy.executive.planner import plan_execution
executive_plan = await plan_execution(
user_prompt=req.user_prompt,
intent=inner_result.get("intent", "unknown"),
context_state=context_state,
identity_block=identity_block
)
logger.info(f"🎯 Executive plan: {executive_plan.get('summary', 'N/A')[:80]}...")
except Exception as e:
logger.warning(f"⚠️ Executive planning failed: {e}")
executive_plan = None
stage_timings["executive"] = (datetime.now() - stage_start).total_seconds() * 1000
# ----------------------------------------------------------------
# STAGE 0.8 — Autonomous Tool Invocation
# ----------------------------------------------------------------
stage_start = datetime.now()
tool_results = None
autonomous_enabled = os.getenv("ENABLE_AUTONOMOUS_TOOLS", "true").lower() == "true"
tool_confidence_threshold = float(os.getenv("AUTONOMOUS_TOOL_CONFIDENCE_THRESHOLD", "0.6"))
if autonomous_enabled and inner_result:
try:
from autonomy.tools.decision_engine import ToolDecisionEngine
from autonomy.tools.orchestrator import ToolOrchestrator
# Analyze which tools to invoke
decision_engine = ToolDecisionEngine()
tool_decision = await decision_engine.analyze_tool_needs(
user_prompt=req.user_prompt,
monologue=inner_result,
context_state=context_state,
available_tools=["RAG", "WEB", "WEATHER", "CODEBRAIN"]
)
# Execute tools if confidence threshold met
if tool_decision["should_invoke_tools"] and tool_decision["confidence"] >= tool_confidence_threshold:
orchestrator = ToolOrchestrator(tool_timeout=30)
tool_results = await orchestrator.execute_tools(
tools_to_invoke=tool_decision["tools_to_invoke"],
context_state=context_state
)
# Format results for context injection
tool_context = orchestrator.format_results_for_context(tool_results)
context_state["autonomous_tool_results"] = tool_context
summary = tool_results.get("execution_summary", {})
logger.info(f"🛠️ Tools executed: {summary.get('successful', [])} succeeded")
else:
logger.info(f"🛠️ No tools invoked (confidence: {tool_decision.get('confidence', 0):.2f})")
except Exception as e:
logger.warning(f"⚠️ Autonomous tool invocation failed: {e}")
if LOG_DETAIL_LEVEL == "verbose":
import traceback
traceback.print_exc()
stage_timings["tools"] = (datetime.now() - stage_start).total_seconds() * 1000
# ----------------------------------------------------------------
# STAGE 1-5 — Core Reasoning Pipeline
# ----------------------------------------------------------------
stage_start = datetime.now()
# Extract intake summary
intake_summary = "(no context available)"
if context_state.get("intake"):
l20 = context_state["intake"].get("L20")
if isinstance(l20, dict):
intake_summary = l20.get("summary", intake_summary)
elif isinstance(l20, str):
intake_summary = l20
# Reflection
try:
reflection = await reflect_notes(intake_summary, identity_block=identity_block)
reflection_notes = reflection.get("notes", [])
except Exception as e:
reflection_notes = []
logger.warning(f"⚠️ Reflection failed: {e}")
stage_timings["reflection"] = (datetime.now() - stage_start).total_seconds() * 1000
# Reasoning (draft)
stage_start = datetime.now()
draft = await reason_check(
req.user_prompt,
identity_block=identity_block,
rag_block=context_state.get("rag", []),
reflection_notes=reflection_notes,
context=context_state,
monologue=inner_result,
executive_plan=executive_plan
)
stage_timings["reasoning"] = (datetime.now() - stage_start).total_seconds() * 1000
# Refinement
stage_start = datetime.now()
result = await refine_answer(
draft_output=draft,
reflection_notes=reflection_notes,
identity_block=identity_block,
rag_block=context_state.get("rag", []),
)
final_neutral = result["final_output"]
stage_timings["refinement"] = (datetime.now() - stage_start).total_seconds() * 1000
# Persona
stage_start = datetime.now()
tone = inner_result.get("tone", "neutral") if inner_result else "neutral"
depth = inner_result.get("depth", "medium") if inner_result else "medium"
persona_answer = await speak(final_neutral, tone=tone, depth=depth)
stage_timings["persona"] = (datetime.now() - stage_start).total_seconds() * 1000
# ----------------------------------------------------------------
# STAGE 6 — Session update
# ----------------------------------------------------------------
update_last_assistant_message(req.session_id, persona_answer)
# ----------------------------------------------------------------
# STAGE 6.5 — Self-state update & Pattern Learning
# ----------------------------------------------------------------
stage_start = datetime.now()
try:
from autonomy.self.analyzer import analyze_and_update_state
await analyze_and_update_state(
monologue=inner_result or {},
user_prompt=req.user_prompt,
response=persona_answer,
context=context_state
)
except Exception as e:
logger.warning(f"⚠️ Self-state update failed: {e}")
try:
from autonomy.learning.pattern_learner import get_pattern_learner
learner = get_pattern_learner()
await learner.learn_from_interaction(
user_prompt=req.user_prompt,
response=persona_answer,
monologue=inner_result or {},
context=context_state
)
except Exception as e:
logger.warning(f"⚠️ Pattern learning failed: {e}")
stage_timings["learning"] = (datetime.now() - stage_start).total_seconds() * 1000
# ----------------------------------------------------------------
# STAGE 7 — Proactive Monitoring & Suggestions
# ----------------------------------------------------------------
stage_start = datetime.now()
proactive_enabled = os.getenv("ENABLE_PROACTIVE_MONITORING", "true").lower() == "true"
proactive_min_priority = float(os.getenv("PROACTIVE_SUGGESTION_MIN_PRIORITY", "0.6"))
if proactive_enabled:
try:
from autonomy.proactive.monitor import get_proactive_monitor
monitor = get_proactive_monitor(min_priority=proactive_min_priority)
self_state = load_self_state()
suggestion = await monitor.analyze_session(
session_id=req.session_id,
context_state=context_state,
self_state=self_state
)
if suggestion:
suggestion_text = monitor.format_suggestion(suggestion)
persona_answer += suggestion_text
logger.info(f"💡 Proactive suggestion: {suggestion['type']} (priority: {suggestion['priority']:.2f})")
except Exception as e:
logger.warning(f"⚠️ Proactive monitoring failed: {e}")
stage_timings["proactive"] = (datetime.now() - stage_start).total_seconds() * 1000
# ----------------------------------------------------------------
# PIPELINE COMPLETE — Summary
# ----------------------------------------------------------------
total_duration = (datetime.now() - pipeline_start).total_seconds() * 1000
# Always show pipeline completion
logger.info(f"\n{'='*100}")
logger.info(f"✨ PIPELINE COMPLETE | Session: {req.session_id} | Total: {total_duration:.0f}ms")
logger.info(f"{'='*100}")
# Show timing breakdown in detailed/verbose mode
if LOG_DETAIL_LEVEL in ["detailed", "verbose"]:
logger.info("⏱️ Stage Timings:")
for stage, duration in stage_timings.items():
pct = (duration / total_duration) * 100 if total_duration > 0 else 0
logger.info(f" {stage:15s}: {duration:6.0f}ms ({pct:5.1f}%)")
logger.info(f"📤 Output: {len(persona_answer)} chars")
logger.info(f"{'='*100}\n")
# ----------------------------------------------------------------
# RETURN
# ----------------------------------------------------------------
return {
"draft": draft,
"neutral": final_neutral,
"persona": persona_answer,
"reflection": reflection_notes,
"session_id": req.session_id,
"context_summary": {
"rag_results": len(context_state.get("rag", [])),
"minutes_since_last": context_state.get("minutes_since_last_msg"),
"message_count": context_state.get("message_count"),
"mode": context_state.get("mode"),
}
}
# -------------------------------------------------------------------
# /simple endpoint - Standard chatbot mode (no reasoning pipeline)
# -------------------------------------------------------------------
@cortex_router.post("/simple")
async def run_simple(req: ReasonRequest):
"""
Standard chatbot mode - bypasses all cortex reasoning pipeline.
Just a simple conversation loop like a typical chatbot.
"""
from datetime import datetime
from llm.llm_router import call_llm
start_time = datetime.now()
logger.info(f"\n{'='*100}")
logger.info(f"💬 SIMPLE MODE | Session: {req.session_id} | {datetime.now().strftime('%H:%M:%S.%f')[:-3]}")
logger.info(f"{'='*100}")
logger.info(f"📝 User: {req.user_prompt[:150]}...")
logger.info(f"{'-'*100}\n")
# Get conversation history from context and intake buffer
context_state = await collect_context(req.session_id, req.user_prompt)
# Get recent messages from Intake buffer
from intake.intake import get_recent_messages
recent_msgs = get_recent_messages(req.session_id, limit=20)
logger.info(f"📋 Retrieved {len(recent_msgs)} recent messages from Intake buffer")
# Build simple conversation history with system message
system_message = {
"role": "system",
"content": (
"You are a helpful AI assistant. Provide direct, concise responses to the user's questions. "
"Maintain context from previous messages in the conversation."
)
}
messages = [system_message]
# Add conversation history
if recent_msgs:
for msg in recent_msgs:
messages.append({
"role": msg.get("role", "user"),
"content": msg.get("content", "")
})
logger.info(f" - {msg.get('role')}: {msg.get('content', '')[:50]}...")
# Add current user message
messages.append({
"role": "user",
"content": req.user_prompt
})
logger.info(f"📨 Total messages being sent to LLM: {len(messages)} (including system message)")
# Get backend from request, otherwise fall back to env variable
backend = req.backend if req.backend else os.getenv("STANDARD_MODE_LLM", "SECONDARY")
backend = backend.upper() # Normalize to uppercase
logger.info(f"🔧 Using backend: {backend}")
temperature = req.temperature if req.temperature is not None else 0.7
# Direct LLM call with messages (works for Ollama/OpenAI chat APIs)
try:
raw_response = await call_llm(
messages=messages,
backend=backend,
temperature=temperature,
max_tokens=2048
)
# Clean response - just strip whitespace
response = raw_response.strip()
except Exception as e:
logger.error(f"❌ LLM call failed: {e}")
response = f"Error: {str(e)}"
# Update session with the exchange
try:
update_last_assistant_message(req.session_id, response)
add_exchange_internal({
"session_id": req.session_id,
"role": "user",
"content": req.user_prompt
})
add_exchange_internal({
"session_id": req.session_id,
"role": "assistant",
"content": response
})
except Exception as e:
logger.warning(f"⚠️ Session update failed: {e}")
duration = (datetime.now() - start_time).total_seconds() * 1000
logger.info(f"\n{'='*100}")
logger.info(f"✨ SIMPLE MODE COMPLETE | Session: {req.session_id} | Total: {duration:.0f}ms")
logger.info(f"📤 Output: {len(response)} chars")
logger.info(f"{'='*100}\n")
return {
"draft": response,
"neutral": response,
"persona": response,
"reflection": "",
"session_id": req.session_id,
"context_summary": {
"message_count": len(messages),
"mode": "standard"
}
}
# -------------------------------------------------------------------
# /ingest endpoint (internal)
# -------------------------------------------------------------------
class IngestPayload(BaseModel):
session_id: str
user_msg: str
assistant_msg: str
@cortex_router.post("/ingest")
async def ingest(payload: IngestPayload):
try:
update_last_assistant_message(payload.session_id, payload.assistant_msg)
except Exception as e:
logger.warning(f"[INGEST] Session update failed: {e}")
try:
add_exchange_internal({
"session_id": payload.session_id,
"user_msg": payload.user_msg,
"assistant_msg": payload.assistant_msg,
})
except Exception as e:
logger.warning(f"[INGEST] Intake update failed: {e}")
return {"status": "ok", "session_id": payload.session_id}
-1
View File
@@ -1 +0,0 @@
"""Tests for Project Lyra Cortex."""
-197
View File
@@ -1,197 +0,0 @@
"""
Integration tests for Phase 1 autonomy features.
Tests monologue integration, executive planning, and self-state persistence.
"""
import asyncio
import json
import sys
import os
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from autonomy.monologue.monologue import InnerMonologue
from autonomy.self.state import load_self_state, update_self_state, get_self_state_instance
from autonomy.executive.planner import plan_execution
async def test_monologue_integration():
"""Test monologue generates valid output."""
print("\n" + "="*60)
print("TEST 1: Monologue Integration")
print("="*60)
mono = InnerMonologue()
context = {
"user_message": "Explain quantum computing to me like I'm 5",
"session_id": "test_001",
"self_state": load_self_state(),
"context_summary": {"message_count": 5}
}
result = await mono.process(context)
assert "intent" in result, "Missing intent field"
assert "tone" in result, "Missing tone field"
assert "depth" in result, "Missing depth field"
assert "consult_executive" in result, "Missing consult_executive field"
print("✓ Monologue integration test passed")
print(f" Result: {json.dumps(result, indent=2)}")
return result
async def test_executive_planning():
"""Test executive planner generates valid plans."""
print("\n" + "="*60)
print("TEST 2: Executive Planning")
print("="*60)
plan = await plan_execution(
user_prompt="Help me build a distributed system with microservices architecture",
intent="technical_implementation",
context_state={
"tools_available": ["RAG", "WEB", "CODEBRAIN"],
"message_count": 3,
"minutes_since_last_msg": 2.5,
"active_project": None
},
identity_block={}
)
assert "summary" in plan, "Missing summary field"
assert "plan_text" in plan, "Missing plan_text field"
assert "steps" in plan, "Missing steps field"
assert len(plan["steps"]) > 0, "No steps generated"
print("✓ Executive planning test passed")
print(f" Plan summary: {plan['summary']}")
print(f" Steps: {len(plan['steps'])}")
print(f" Complexity: {plan.get('estimated_complexity', 'unknown')}")
return plan
def test_self_state_persistence():
"""Test self-state loads and updates."""
print("\n" + "="*60)
print("TEST 3: Self-State Persistence")
print("="*60)
state1 = load_self_state()
assert "mood" in state1, "Missing mood field"
assert "energy" in state1, "Missing energy field"
assert "interaction_count" in state1, "Missing interaction_count"
initial_count = state1.get("interaction_count", 0)
print(f" Initial interaction count: {initial_count}")
update_self_state(
mood_delta=0.1,
energy_delta=-0.05,
new_focus="testing"
)
state2 = load_self_state()
assert state2["interaction_count"] == initial_count + 1, "Interaction count not incremented"
assert state2["focus"] == "testing", "Focus not updated"
print("✓ Self-state persistence test passed")
print(f" New interaction count: {state2['interaction_count']}")
print(f" New focus: {state2['focus']}")
print(f" New energy: {state2['energy']:.2f}")
return state2
async def test_end_to_end_flow():
"""Test complete flow from monologue through planning."""
print("\n" + "="*60)
print("TEST 4: End-to-End Flow")
print("="*60)
# Step 1: Monologue detects complex query
mono = InnerMonologue()
mono_result = await mono.process({
"user_message": "Design a scalable ML pipeline with CI/CD integration",
"session_id": "test_e2e",
"self_state": load_self_state(),
"context_summary": {}
})
print(f" Monologue intent: {mono_result.get('intent')}")
print(f" Consult executive: {mono_result.get('consult_executive')}")
# Step 2: If executive requested, generate plan
if mono_result.get("consult_executive"):
plan = await plan_execution(
user_prompt="Design a scalable ML pipeline with CI/CD integration",
intent=mono_result.get("intent", "unknown"),
context_state={"tools_available": ["CODEBRAIN", "WEB"]},
identity_block={}
)
assert plan is not None, "Plan should be generated"
print(f" Executive plan generated: {len(plan.get('steps', []))} steps")
# Step 3: Update self-state
update_self_state(
energy_delta=-0.1, # Complex task is tiring
new_focus="ml_pipeline_design",
confidence_delta=0.05
)
state = load_self_state()
assert state["focus"] == "ml_pipeline_design", "Focus should be updated"
print("✓ End-to-end flow test passed")
print(f" Final state: {state['mood']}, energy={state['energy']:.2f}")
return True
async def run_all_tests():
"""Run all Phase 1 tests."""
print("\n" + "="*60)
print("PHASE 1 AUTONOMY TESTS")
print("="*60)
try:
# Test 1: Monologue
mono_result = await test_monologue_integration()
# Test 2: Executive Planning
plan_result = await test_executive_planning()
# Test 3: Self-State
state_result = test_self_state_persistence()
# Test 4: End-to-End
await test_end_to_end_flow()
print("\n" + "="*60)
print("ALL TESTS PASSED ✓")
print("="*60)
print("\nSummary:")
print(f" - Monologue: {mono_result.get('intent')} ({mono_result.get('tone')})")
print(f" - Executive: {plan_result.get('estimated_complexity')} complexity")
print(f" - Self-state: {state_result.get('interaction_count')} interactions")
return True
except Exception as e:
print("\n" + "="*60)
print(f"TEST FAILED: {e}")
print("="*60)
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = asyncio.run(run_all_tests())
sys.exit(0 if success else 1)
-495
View File
@@ -1,495 +0,0 @@
"""
Integration tests for Phase 2 autonomy features.
Tests autonomous tool invocation, proactive monitoring, actions, and pattern learning.
"""
import asyncio
import json
import sys
import os
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Override self-state file path for testing
os.environ["SELF_STATE_FILE"] = "/tmp/test_self_state.json"
from autonomy.tools.decision_engine import ToolDecisionEngine
from autonomy.tools.orchestrator import ToolOrchestrator
from autonomy.proactive.monitor import ProactiveMonitor
from autonomy.actions.autonomous_actions import AutonomousActionManager
from autonomy.learning.pattern_learner import PatternLearner
from autonomy.self.state import load_self_state, get_self_state_instance
async def test_tool_decision_engine():
"""Test autonomous tool decision making."""
print("\n" + "="*60)
print("TEST 1: Tool Decision Engine")
print("="*60)
engine = ToolDecisionEngine()
# Test 1a: Memory reference detection
result = await engine.analyze_tool_needs(
user_prompt="What did we discuss earlier about Python?",
monologue={"intent": "clarification", "consult_executive": False},
context_state={},
available_tools=["RAG", "WEB", "WEATHER"]
)
assert result["should_invoke_tools"], "Should invoke tools for memory reference"
assert any(t["tool"] == "RAG" for t in result["tools_to_invoke"]), "Should recommend RAG"
assert result["confidence"] > 0.8, f"Confidence should be high for clear memory reference: {result['confidence']}"
print(f" ✓ Memory reference detection passed")
print(f" Tools: {[t['tool'] for t in result['tools_to_invoke']]}")
print(f" Confidence: {result['confidence']:.2f}")
# Test 1b: Web search detection
result = await engine.analyze_tool_needs(
user_prompt="What's the latest news about AI developments?",
monologue={"intent": "information_seeking", "consult_executive": False},
context_state={},
available_tools=["RAG", "WEB", "WEATHER"]
)
assert result["should_invoke_tools"], "Should invoke tools for current info request"
assert any(t["tool"] == "WEB" for t in result["tools_to_invoke"]), "Should recommend WEB"
print(f" ✓ Web search detection passed")
print(f" Tools: {[t['tool'] for t in result['tools_to_invoke']]}")
# Test 1c: Weather detection
result = await engine.analyze_tool_needs(
user_prompt="What's the weather like today in Boston?",
monologue={"intent": "information_seeking", "consult_executive": False},
context_state={},
available_tools=["RAG", "WEB", "WEATHER"]
)
assert result["should_invoke_tools"], "Should invoke tools for weather query"
assert any(t["tool"] == "WEATHER" for t in result["tools_to_invoke"]), "Should recommend WEATHER"
print(f" ✓ Weather detection passed")
# Test 1d: Proactive RAG for complex queries
result = await engine.analyze_tool_needs(
user_prompt="Design a microservices architecture",
monologue={"intent": "technical_implementation", "consult_executive": True},
context_state={},
available_tools=["RAG", "WEB", "CODEBRAIN"]
)
assert result["should_invoke_tools"], "Should proactively invoke tools for complex queries"
rag_tools = [t for t in result["tools_to_invoke"] if t["tool"] == "RAG"]
assert len(rag_tools) > 0, "Should include proactive RAG"
print(f" ✓ Proactive RAG detection passed")
print(f" Reason: {rag_tools[0]['reason']}")
print("\n✓ Tool Decision Engine tests passed\n")
return result
async def test_tool_orchestrator():
"""Test tool orchestration (mock mode)."""
print("\n" + "="*60)
print("TEST 2: Tool Orchestrator (Mock Mode)")
print("="*60)
orchestrator = ToolOrchestrator(tool_timeout=5)
# Since actual tools may not be available, test the orchestrator structure
print(f" Available tools: {list(orchestrator.available_tools.keys())}")
# Test with tools_to_invoke (will fail gracefully if tools unavailable)
tools_to_invoke = [
{"tool": "RAG", "query": "test query", "reason": "testing", "priority": 0.9}
]
result = await orchestrator.execute_tools(
tools_to_invoke=tools_to_invoke,
context_state={"session_id": "test"}
)
assert "results" in result, "Should return results dict"
assert "execution_summary" in result, "Should return execution summary"
summary = result["execution_summary"]
assert "tools_invoked" in summary, "Summary should include tools_invoked"
assert "total_time_ms" in summary, "Summary should include timing"
print(f" ✓ Orchestrator structure valid")
print(f" Summary: {summary}")
# Test result formatting
formatted = orchestrator.format_results_for_context(result)
assert isinstance(formatted, str), "Should format results as string"
print(f" ✓ Result formatting works")
print(f" Formatted length: {len(formatted)} chars")
print("\n✓ Tool Orchestrator tests passed\n")
return result
async def test_proactive_monitor():
"""Test proactive monitoring and suggestions."""
print("\n" + "="*60)
print("TEST 3: Proactive Monitor")
print("="*60)
monitor = ProactiveMonitor(min_priority=0.6)
# Test 3a: Long silence detection
context_state = {
"message_count": 5,
"minutes_since_last_msg": 35 # > 30 minutes
}
self_state = load_self_state()
suggestion = await monitor.analyze_session(
session_id="test_silence",
context_state=context_state,
self_state=self_state
)
assert suggestion is not None, "Should generate suggestion for long silence"
assert suggestion["type"] == "check_in", f"Should be check_in type: {suggestion['type']}"
assert suggestion["priority"] >= 0.6, "Priority should meet threshold"
print(f" ✓ Long silence detection passed")
print(f" Type: {suggestion['type']}, Priority: {suggestion['priority']:.2f}")
print(f" Suggestion: {suggestion['suggestion'][:50]}...")
# Test 3b: Learning opportunity (high curiosity)
self_state["curiosity"] = 0.8
self_state["learning_queue"] = ["quantum computing", "rust programming"]
# Reset cooldown for this test
monitor.reset_cooldown("test_learning")
suggestion = await monitor.analyze_session(
session_id="test_learning",
context_state={"message_count": 3, "minutes_since_last_msg": 2},
self_state=self_state
)
assert suggestion is not None, "Should generate learning suggestion"
assert suggestion["type"] == "learning", f"Should be learning type: {suggestion['type']}"
print(f" ✓ Learning opportunity detection passed")
print(f" Suggestion: {suggestion['suggestion'][:70]}...")
# Test 3c: Conversation milestone
monitor.reset_cooldown("test_milestone")
# Reset curiosity to avoid learning suggestion taking precedence
self_state["curiosity"] = 0.5
self_state["learning_queue"] = []
suggestion = await monitor.analyze_session(
session_id="test_milestone",
context_state={"message_count": 50, "minutes_since_last_msg": 1},
self_state=self_state
)
assert suggestion is not None, "Should generate milestone suggestion"
# Note: learning or summary both valid - check it's a reasonable suggestion
assert suggestion["type"] in ["summary", "learning", "check_in"], f"Should be valid type: {suggestion['type']}"
print(f" ✓ Conversation milestone detection passed (type: {suggestion['type']})")
# Test 3d: Cooldown mechanism
# Try to get another suggestion immediately (should be blocked)
suggestion2 = await monitor.analyze_session(
session_id="test_milestone",
context_state={"message_count": 51, "minutes_since_last_msg": 1},
self_state=self_state
)
assert suggestion2 is None, "Should not generate suggestion during cooldown"
print(f" ✓ Cooldown mechanism working")
# Check stats
stats = monitor.get_session_stats("test_milestone")
assert stats["cooldown_active"], "Cooldown should be active"
print(f" Cooldown remaining: {stats['cooldown_remaining']}s")
print("\n✓ Proactive Monitor tests passed\n")
return suggestion
async def test_autonomous_actions():
"""Test autonomous action execution."""
print("\n" + "="*60)
print("TEST 4: Autonomous Actions")
print("="*60)
manager = AutonomousActionManager()
# Test 4a: List allowed actions
allowed = manager.get_allowed_actions()
assert "create_memory" in allowed, "Should have create_memory action"
assert "update_goal" in allowed, "Should have update_goal action"
assert "learn_topic" in allowed, "Should have learn_topic action"
print(f" ✓ Allowed actions: {allowed}")
# Test 4b: Validate actions
validation = manager.validate_action("create_memory", {"text": "test memory"})
assert validation["valid"], "Should validate correct action"
print(f" ✓ Action validation passed")
# Test 4c: Execute learn_topic action
result = await manager.execute_action(
action_type="learn_topic",
parameters={"topic": "rust programming", "reason": "testing", "priority": 0.8},
context={"session_id": "test"}
)
assert result["success"], f"Action should succeed: {result.get('error', 'unknown')}"
assert "topic" in result["result"], "Should return topic info"
print(f" ✓ learn_topic action executed")
print(f" Topic: {result['result']['topic']}")
print(f" Queue position: {result['result']['queue_position']}")
# Test 4d: Execute update_focus action
result = await manager.execute_action(
action_type="update_focus",
parameters={"focus": "autonomy_testing", "reason": "running tests"},
context={"session_id": "test"}
)
assert result["success"], "update_focus should succeed"
print(f" ✓ update_focus action executed")
print(f" New focus: {result['result']['new_focus']}")
# Test 4e: Reject non-whitelisted action
result = await manager.execute_action(
action_type="delete_all_files", # NOT in whitelist
parameters={},
context={"session_id": "test"}
)
assert not result["success"], "Should reject non-whitelisted action"
assert "not in whitelist" in result["error"], "Should indicate whitelist violation"
print(f" ✓ Non-whitelisted action rejected")
# Test 4f: Action log
log = manager.get_action_log(limit=10)
assert len(log) >= 2, f"Should have logged multiple actions (got {len(log)})"
print(f" ✓ Action log contains {len(log)} entries")
print("\n✓ Autonomous Actions tests passed\n")
return result
async def test_pattern_learner():
"""Test pattern learning system."""
print("\n" + "="*60)
print("TEST 5: Pattern Learner")
print("="*60)
# Use temp file for testing
test_file = "/tmp/test_patterns.json"
learner = PatternLearner(patterns_file=test_file)
# Test 5a: Learn from multiple interactions
for i in range(5):
await learner.learn_from_interaction(
user_prompt=f"Help me with Python coding task {i}",
response=f"Here's help with task {i}...",
monologue={"intent": "coding_help", "tone": "focused", "depth": "medium"},
context={"session_id": "test", "executive_plan": None}
)
print(f" ✓ Learned from 5 interactions")
# Test 5b: Get top topics
top_topics = learner.get_top_topics(limit=5)
assert len(top_topics) > 0, "Should have learned topics"
assert "coding_help" == top_topics[0][0], "coding_help should be top topic"
print(f" ✓ Top topics: {[t[0] for t in top_topics[:3]]}")
# Test 5c: Get preferred tone
preferred_tone = learner.get_preferred_tone()
assert preferred_tone == "focused", "Should detect focused as preferred tone"
print(f" ✓ Preferred tone: {preferred_tone}")
# Test 5d: Get preferred depth
preferred_depth = learner.get_preferred_depth()
assert preferred_depth == "medium", "Should detect medium as preferred depth"
print(f" ✓ Preferred depth: {preferred_depth}")
# Test 5e: Get insights
insights = learner.get_insights()
assert insights["total_interactions"] == 5, "Should track interaction count"
assert insights["preferred_tone"] == "focused", "Insights should include tone"
print(f" ✓ Insights generated:")
print(f" Total interactions: {insights['total_interactions']}")
print(f" Recommendations: {insights['learning_recommendations']}")
# Test 5f: Export patterns
exported = learner.export_patterns()
assert "topic_frequencies" in exported, "Should export all patterns"
print(f" ✓ Patterns exported ({len(exported)} keys)")
# Cleanup
if os.path.exists(test_file):
os.remove(test_file)
print("\n✓ Pattern Learner tests passed\n")
return insights
async def test_end_to_end_autonomy():
"""Test complete autonomous flow."""
print("\n" + "="*60)
print("TEST 6: End-to-End Autonomy Flow")
print("="*60)
# Simulate a complex user query that triggers multiple autonomous systems
user_prompt = "Remember what we discussed about machine learning? I need current research on transformers."
monologue = {
"intent": "technical_research",
"tone": "focused",
"depth": "deep",
"consult_executive": True
}
context_state = {
"session_id": "e2e_test",
"message_count": 15,
"minutes_since_last_msg": 5
}
print(f" User prompt: {user_prompt}")
print(f" Monologue intent: {monologue['intent']}")
# Step 1: Tool decision engine
engine = ToolDecisionEngine()
tool_decision = await engine.analyze_tool_needs(
user_prompt=user_prompt,
monologue=monologue,
context_state=context_state,
available_tools=["RAG", "WEB", "CODEBRAIN"]
)
print(f"\n Step 1: Tool Decision")
print(f" Should invoke: {tool_decision['should_invoke_tools']}")
print(f" Tools: {[t['tool'] for t in tool_decision['tools_to_invoke']]}")
assert tool_decision["should_invoke_tools"], "Should invoke tools"
assert len(tool_decision["tools_to_invoke"]) >= 2, "Should recommend multiple tools (RAG + WEB)"
# Step 2: Pattern learning
learner = PatternLearner(patterns_file="/tmp/e2e_test_patterns.json")
await learner.learn_from_interaction(
user_prompt=user_prompt,
response="Here's information about transformers...",
monologue=monologue,
context=context_state
)
print(f"\n Step 2: Pattern Learning")
top_topics = learner.get_top_topics(limit=3)
print(f" Learned topics: {[t[0] for t in top_topics]}")
# Step 3: Autonomous action
action_manager = AutonomousActionManager()
action_result = await action_manager.execute_action(
action_type="learn_topic",
parameters={"topic": "transformer architectures", "reason": "user interest detected"},
context=context_state
)
print(f"\n Step 3: Autonomous Action")
print(f" Action: learn_topic")
print(f" Success: {action_result['success']}")
# Step 4: Proactive monitoring (won't trigger due to low message count)
monitor = ProactiveMonitor(min_priority=0.6)
monitor.reset_cooldown("e2e_test")
suggestion = await monitor.analyze_session(
session_id="e2e_test",
context_state=context_state,
self_state=load_self_state()
)
print(f"\n Step 4: Proactive Monitoring")
print(f" Suggestion: {suggestion['type'] if suggestion else 'None (expected for low message count)'}")
# Cleanup
if os.path.exists("/tmp/e2e_test_patterns.json"):
os.remove("/tmp/e2e_test_patterns.json")
print("\n✓ End-to-End Autonomy Flow tests passed\n")
return True
async def run_all_tests():
"""Run all Phase 2 tests."""
print("\n" + "="*60)
print("PHASE 2 AUTONOMY TESTS")
print("="*60)
try:
# Test 1: Tool Decision Engine
await test_tool_decision_engine()
# Test 2: Tool Orchestrator
await test_tool_orchestrator()
# Test 3: Proactive Monitor
await test_proactive_monitor()
# Test 4: Autonomous Actions
await test_autonomous_actions()
# Test 5: Pattern Learner
await test_pattern_learner()
# Test 6: End-to-End
await test_end_to_end_autonomy()
print("\n" + "="*60)
print("ALL PHASE 2 TESTS PASSED ✓")
print("="*60)
print("\nPhase 2 Features Validated:")
print(" ✓ Autonomous tool decision making")
print(" ✓ Tool orchestration and execution")
print(" ✓ Proactive monitoring and suggestions")
print(" ✓ Safe autonomous actions")
print(" ✓ Pattern learning and adaptation")
print(" ✓ End-to-end autonomous flow")
return True
except Exception as e:
print("\n" + "="*60)
print(f"TEST FAILED: {e}")
print("="*60)
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = asyncio.run(run_all_tests())
sys.exit(0 if success else 1)
-1
View File
@@ -1 +0,0 @@
# Utilities module
View File
-33
View File
@@ -1,33 +0,0 @@
import os, json, datetime
# optional daily rotation
LOG_PATH = os.getenv("REFLECTION_NOTE_PATH") or \
f"/app/logs/reflections_{datetime.date.today():%Y%m%d}.log"
def log_reflection(reflection: dict, user_prompt: str, draft: str, final: str, session_id: str | None = None):
"""Append a reflection entry to the reflections log."""
try:
# 1️⃣ Make sure log directory exists
os.makedirs(os.path.dirname(LOG_PATH), exist_ok=True)
# 2️⃣ Ensure session_id is stored
reflection["session_id"] = session_id or reflection.get("session_id", "unknown")
# 3️⃣ Build JSON entry
entry = {
"timestamp": datetime.datetime.now().isoformat(),
"session_id": reflection["session_id"],
"prompt": user_prompt,
"draft_output": draft[:500],
"final_output": final[:500],
"reflection": reflection,
}
# 4️⃣ Write it in pretty JSON, comma-delimited for easy reading
with open(LOG_PATH, "a", encoding="utf-8") as f:
f.write(json.dumps(entry, indent=2, ensure_ascii=False) + ",\n")
print(f"[Cortex] Logged reflection → {LOG_PATH}")
except Exception as e:
print(f"[Cortex] Failed to log reflection: {e}")
-223
View File
@@ -1,223 +0,0 @@
"""
Structured logging utilities for Cortex pipeline debugging.
Provides hierarchical, scannable logs with clear section markers and raw data visibility.
"""
import json
import logging
from typing import Any, Dict, List, Optional
from datetime import datetime
from enum import Enum
class LogLevel(Enum):
"""Log detail levels"""
MINIMAL = 1 # Only errors and final results
SUMMARY = 2 # Stage summaries + errors
DETAILED = 3 # Include raw LLM outputs, RAG results
VERBOSE = 4 # Everything including intermediate states
class PipelineLogger:
"""
Hierarchical logger for cortex pipeline debugging.
Features:
- Clear visual section markers
- Collapsible detail sections
- Raw data dumps with truncation options
- Stage timing
- Error highlighting
"""
def __init__(self, logger: logging.Logger, level: LogLevel = LogLevel.SUMMARY):
self.logger = logger
self.level = level
self.stage_timings = {}
self.current_stage = None
self.stage_start_time = None
self.pipeline_start_time = None
def pipeline_start(self, session_id: str, user_prompt: str):
"""Mark the start of a pipeline run"""
self.pipeline_start_time = datetime.now()
self.stage_timings = {}
if self.level.value >= LogLevel.SUMMARY.value:
self.logger.info(f"\n{'='*100}")
self.logger.info(f"🚀 PIPELINE START | Session: {session_id} | {datetime.now().strftime('%H:%M:%S.%f')[:-3]}")
self.logger.info(f"{'='*100}")
if self.level.value >= LogLevel.DETAILED.value:
self.logger.info(f"📝 User prompt: {user_prompt[:200]}{'...' if len(user_prompt) > 200 else ''}")
self.logger.info(f"{'-'*100}\n")
def stage_start(self, stage_name: str, description: str = ""):
"""Mark the start of a pipeline stage"""
self.current_stage = stage_name
self.stage_start_time = datetime.now()
if self.level.value >= LogLevel.SUMMARY.value:
timestamp = datetime.now().strftime('%H:%M:%S.%f')[:-3]
desc_suffix = f" - {description}" if description else ""
self.logger.info(f"▶️ [{stage_name}]{desc_suffix} | {timestamp}")
def stage_end(self, result_summary: str = ""):
"""Mark the end of a pipeline stage"""
if self.current_stage and self.stage_start_time:
duration_ms = (datetime.now() - self.stage_start_time).total_seconds() * 1000
self.stage_timings[self.current_stage] = duration_ms
if self.level.value >= LogLevel.SUMMARY.value:
summary_suffix = f"{result_summary}" if result_summary else ""
self.logger.info(f"✅ [{self.current_stage}] Complete in {duration_ms:.0f}ms{summary_suffix}\n")
self.current_stage = None
self.stage_start_time = None
def log_llm_call(self, backend: str, prompt: str, response: Any, raw_response: str = None):
"""
Log LLM call details with proper formatting.
Args:
backend: Backend name (PRIMARY, SECONDARY, etc.)
prompt: Input prompt to LLM
response: Parsed response object
raw_response: Raw JSON response string
"""
if self.level.value >= LogLevel.DETAILED.value:
self.logger.info(f" 🧠 LLM Call | Backend: {backend}")
# Show prompt (truncated)
if isinstance(prompt, list):
prompt_preview = prompt[-1].get('content', '')[:150] if prompt else ''
else:
prompt_preview = str(prompt)[:150]
self.logger.info(f" Prompt: {prompt_preview}...")
# Show parsed response
if isinstance(response, dict):
response_text = (
response.get('reply') or
response.get('message', {}).get('content') or
str(response)
)[:200]
else:
response_text = str(response)[:200]
self.logger.info(f" Response: {response_text}...")
# Show raw response in collapsible block
if raw_response and self.level.value >= LogLevel.VERBOSE.value:
self.logger.debug(f" ╭─ RAW RESPONSE ────────────────────────────────────")
for line in raw_response.split('\n')[:50]: # Limit to 50 lines
self.logger.debug(f"{line}")
if raw_response.count('\n') > 50:
self.logger.debug(f" │ ... ({raw_response.count(chr(10)) - 50} more lines)")
self.logger.debug(f" ╰───────────────────────────────────────────────────\n")
def log_rag_results(self, results: List[Dict[str, Any]]):
"""Log RAG/NeoMem results in scannable format"""
if self.level.value >= LogLevel.SUMMARY.value:
self.logger.info(f" 📚 RAG Results: {len(results)} memories retrieved")
if self.level.value >= LogLevel.DETAILED.value and results:
self.logger.info(f" ╭─ MEMORY SCORES ───────────────────────────────────")
for idx, result in enumerate(results[:10], 1): # Show top 10
score = result.get("score", 0)
data_preview = str(result.get("payload", {}).get("data", ""))[:80]
self.logger.info(f" │ [{idx}] {score:.3f} | {data_preview}...")
if len(results) > 10:
self.logger.info(f" │ ... and {len(results) - 10} more results")
self.logger.info(f" ╰───────────────────────────────────────────────────")
def log_context_state(self, context_state: Dict[str, Any]):
"""Log context state summary"""
if self.level.value >= LogLevel.SUMMARY.value:
msg_count = context_state.get("message_count", 0)
minutes_since = context_state.get("minutes_since_last_msg", 0)
rag_count = len(context_state.get("rag", []))
self.logger.info(f" 📊 Context | Messages: {msg_count} | Last: {minutes_since:.1f}min ago | RAG: {rag_count} results")
if self.level.value >= LogLevel.DETAILED.value:
intake = context_state.get("intake", {})
if intake:
self.logger.info(f" ╭─ INTAKE SUMMARIES ────────────────────────────────")
for level in ["L1", "L5", "L10", "L20", "L30"]:
if level in intake:
summary = intake[level]
if isinstance(summary, dict):
summary = summary.get("summary", str(summary)[:100])
else:
summary = str(summary)[:100]
self.logger.info(f"{level}: {summary}...")
self.logger.info(f" ╰───────────────────────────────────────────────────")
def log_error(self, stage: str, error: Exception, critical: bool = False):
"""Log an error with context"""
level_marker = "🔴 CRITICAL" if critical else "⚠️ WARNING"
self.logger.error(f"{level_marker} | Stage: {stage} | Error: {type(error).__name__}: {str(error)}")
if self.level.value >= LogLevel.VERBOSE.value:
import traceback
self.logger.debug(f" Traceback:\n{traceback.format_exc()}")
def log_raw_data(self, label: str, data: Any, max_lines: int = 30):
"""Log raw data in a collapsible format"""
if self.level.value >= LogLevel.VERBOSE.value:
self.logger.debug(f" ╭─ {label.upper()} ──────────────────────────────────")
if isinstance(data, (dict, list)):
json_str = json.dumps(data, indent=2, default=str)
lines = json_str.split('\n')
for line in lines[:max_lines]:
self.logger.debug(f"{line}")
if len(lines) > max_lines:
self.logger.debug(f" │ ... ({len(lines) - max_lines} more lines)")
else:
lines = str(data).split('\n')
for line in lines[:max_lines]:
self.logger.debug(f"{line}")
if len(lines) > max_lines:
self.logger.debug(f" │ ... ({len(lines) - max_lines} more lines)")
self.logger.debug(f" ╰───────────────────────────────────────────────────")
def pipeline_end(self, session_id: str, final_output_length: int):
"""Mark the end of pipeline run with summary"""
if self.pipeline_start_time:
total_duration_ms = (datetime.now() - self.pipeline_start_time).total_seconds() * 1000
if self.level.value >= LogLevel.SUMMARY.value:
self.logger.info(f"\n{'='*100}")
self.logger.info(f"✨ PIPELINE COMPLETE | Session: {session_id} | Total: {total_duration_ms:.0f}ms")
self.logger.info(f"{'='*100}")
# Show timing breakdown
if self.stage_timings and self.level.value >= LogLevel.DETAILED.value:
self.logger.info("⏱️ Stage Timings:")
for stage, duration in self.stage_timings.items():
pct = (duration / total_duration_ms) * 100 if total_duration_ms > 0 else 0
self.logger.info(f" {stage:20s}: {duration:6.0f}ms ({pct:5.1f}%)")
self.logger.info(f"📤 Final output: {final_output_length} characters")
self.logger.info(f"{'='*100}\n")
def get_log_level_from_env() -> LogLevel:
"""Parse log level from environment variable"""
import os
verbose_debug = os.getenv("VERBOSE_DEBUG", "false").lower() == "true"
detail_level = os.getenv("LOG_DETAIL_LEVEL", "").lower()
if detail_level == "minimal":
return LogLevel.MINIMAL
elif detail_level == "summary":
return LogLevel.SUMMARY
elif detail_level == "detailed":
return LogLevel.DETAILED
elif detail_level == "verbose" or verbose_debug:
return LogLevel.VERBOSE
else:
return LogLevel.SUMMARY # Default
View File
-25
View File
@@ -1,25 +0,0 @@
# === GLOBAL LYRA SETTINGS ===
PROJECT_NAME=lyra
LOG_LEVEL=info
# === SHARED MEMORY / DATABASE CONFIG ===
NEOMEM_API=http://10.0.0.40:7077
NEOMEM_KEY=placeholder
# === PRIMARY LLM BACKEND (MI50 vLLM) ===
LLM_PRIMARY_URL=http://10.0.0.43:8000
LLM_PRIMARY_MODEL=qwen2.5:14b-instruct
# === SECONDARY (3090 Ollama) ===
LLM_SECONDARY_URL=http://10.0.0.3:11434
# === CLOUD BACKEND (OpenAI, optional) ===
LLM_CLOUD_URL=https://api.openai.com/v1
OPENAI_API_KEY=sk-...
# === LOCAL CPU FALLBACK ===
LLM_FALLBACK_URL=http://localhost:11434
# === DEFAULT TEMPERATURE / BACKEND SELECTION ===
LLM_TEMPERATURE=0.7
LLM_FORCE_BACKEND=primary # auto | primary | secondary | cloud | fallback
-167
View File
@@ -1,167 +0,0 @@
networks:
lyra_net:
driver: bridge
volumes:
postgres_data:
driver: local
neo4j_data:
driver: local
services:
# # ============================================================
# # NeoMem: Postgres
# # ============================================================
# neomem-postgres:
# image: ankane/pgvector:v0.5.1
# container_name: neomem-postgres
# restart: unless-stopped
# environment:
# POSTGRES_USER: neomem
# POSTGRES_PASSWORD: neomempass
# POSTGRES_DB: neomem
# volumes:
# - ./volumes/postgres_data:/var/lib/postgresql/data
# ports:
# - "5432:5432"
# healthcheck:
# test: ["CMD-SHELL", "pg_isready -U neomem -d neomem || exit 1"]
# interval: 5s
# timeout: 5s
# retries: 10
# networks:
# - lyra_net
# # ============================================================
# # NeoMem: Neo4j Graph
# # ============================================================
# neomem-neo4j:
# image: neo4j:5
# container_name: neomem-neo4j
# restart: unless-stopped
# environment:
# NEO4J_AUTH: "neo4j/neomemgraph"
# NEO4JLABS_PLUGINS: '["graph-data-science"]'
# volumes:
# - ./volumes/neo4j_data:/data
# ports:
# - "7474:7474"
# - "7687:7687"
# healthcheck:
# test: ["CMD-SHELL", "cypher-shell -u neo4j -p neomemgraph 'RETURN 1' || exit 1"]
# interval: 10s
# timeout: 10s
# retries: 10
# networks:
# - lyra_net
# ============================================================
# NeoMem API
# ============================================================
# neomem-api:
# build:
# context: ./neomem
# image: lyra-neomem:latest
# container_name: neomem-api
# restart: unless-stopped
# env_file:
# - ./neomem/.env
# - ./.env
# volumes:
# - ./neomem_history:/app/history
# ports:
# - "7077:7077"
# depends_on:
# neomem-postgres:
# condition: service_healthy
# neomem-neo4j:
# condition: service_healthy
# networks:
# - lyra_net
# ============================================================
# Relay (host mode)
# ============================================================
relay:
build:
context: ./core/relay
container_name: relay
restart: unless-stopped
env_file:
- ./.env
volumes:
- ./core/relay/sessions:/app/sessions
ports:
- "7078:7078"
networks:
- lyra_net
# ============================================================
# UI Server
# ============================================================
lyra-ui:
image: nginx:alpine
container_name: lyra-ui
restart: unless-stopped
ports:
- "8081:80"
volumes:
- ./core/ui:/usr/share/nginx/html:ro
networks:
- lyra_net
# ============================================================
# Cortex
# ============================================================
cortex:
build:
context: ./cortex
container_name: cortex
restart: unless-stopped
env_file:
- ./cortex/.env
- ./.env
volumes:
- ./cortex:/app
ports:
- "7081:7081"
networks:
- lyra_net
# ============================================================
# Intake
# ============================================================
# intake:
# build:
# context: ./intake
# container_name: intake
# restart: unless-stopped
# env_file:
# - ./intake/.env
# - ./.env
# ports:
# - "7080:7080"
# volumes:
# - ./intake:/app
# - ./intake-logs:/app/logs
# depends_on:
# - cortex
# networks:
# - lyra_net
# ============================================================
# RAG Service
# ============================================================
# rag:
# build:
# context: ./rag
# container_name: rag
# restart: unless-stopped
# environment:
# NEOMEM_URL: http://neomem-api:7077
# ports:
# - "7090:7090"
# networks:
# - lyra_net
+730
View File
@@ -0,0 +1,730 @@
# TriliumNext ETAPI Complete API Reference
## Overview
ETAPI is TriliumNext's public/external REST API available since Trilium v0.50.
**Base URLs:**
- `http://localhost:37740/etapi`
- `http://localhost:8080/etapi`
**API Version:** 1.0.0
**License:** Apache 2.0
## Authentication
All operations require authentication using one of these methods:
### 1. ETAPI Token Authentication (Recommended)
```http
GET /etapi/app-info
Authorization: <ETAPI_TOKEN>
```
OR (since v0.93.0):
```http
GET /etapi/app-info
Authorization: Bearer <ETAPI_TOKEN>
```
### 2. Basic Authentication (since v0.56)
```http
GET /etapi/app-info
Authorization: Basic <BASE64(username:password)>
```
**Note:** Password must be the ETAPI token (NOT your Trilium password).
### 3. Get Token via API
```http
POST /etapi/auth/login
Content-Type: application/json
{
"password": "your_trilium_password"
}
```
**Response:**
```json
{
"authToken": "Bc4bFn0Ffiok_4NpbVCDnFz7B2WU+pdhW8B5Ne3DiR5wXrEyqdjgRIsk="
}
```
---
## Complete API Endpoints
### Authentication
#### Login
- **POST** `/auth/login`
- **Description:** Get an ETAPI token based on password
- **Security:** None (public endpoint)
- **Request Body:**
```json
{
"password": "string"
}
```
- **Responses:**
- `201`: Auth token created
- `429`: Client IP blacklisted (too many failed attempts)
---
### Application Information
#### Get App Info
- **GET** `/app-info`
- **Description:** Get application information
- **Response:**
```json
{
"appVersion": "0.91.0",
"dbVersion": 231,
"syncVersion": 25,
"buildDate": "2022-02-09T22:52:36+01:00",
"buildRevision": "23daaa2387a0655685377f0a541d154aeec2aae8",
"dataDirectory": "/home/user/data",
"clipperProtocolVersion": "1.0",
"utcDateTime": "2022-03-07T21:54:25.277Z"
}
```
#### Get Metrics
- **GET** `/etapi/metrics`
- **Description:** Get Prometheus-format metrics for monitoring
- **Query Parameters:**
- `format`: `json` or `prometheus` (default: prometheus)
- **Response:** Metrics data including note counts, db stats, etc.
---
### Notes Management
#### Create Note
- **POST** `/create-note`
- **Description:** Create a note and place it into the note tree
- **Request Body:**
```json
{
"parentNoteId": "root",
"title": "My Note",
"type": "text",
"mime": "text/html",
"content": "<p>Hello World</p>",
"notePosition": 10,
"prefix": "",
"isExpanded": false,
"noteId": "customId123",
"branchId": "customBranchId",
"utcDateCreated": "2021-12-31 19:18:11.930Z",
"utcDateModified": "2021-12-31 19:18:11.930Z"
}
```
- **Required Fields:** `parentNoteId`, `title`, `type`, `content`
- **Optional Fields:** `notePosition`, `prefix`, `isExpanded`, `noteId`, `branchId`, timestamps
- **Note Types:**
- `text` - Rich text notes
- `code` - Code notes (requires `mime`)
- `file` - File attachments (requires `mime`)
- `image` - Image notes (requires `mime`)
- `search` - Saved search
- `book` - Book/container note
- `relationMap` - Relation map
- `render` - Render note
- `noteMap` - Note map
- `mermaid` - Mermaid diagrams
- `webView` - Web view
- `shortcut` - Shortcut
- `doc` - Document
- `contentWidget` - Content widget
- `launcher` - Launcher
- `canvas` - Canvas note
- **Response:** `201` with `NoteWithBranch` object
#### Search Notes
- **GET** `/notes`
- **Description:** Search notes using query syntax
- **Query Parameters:**
- `search` (required): Search query string
- `ancestorNoteId`: Search in subtree only
- `fastSearch`: Boolean for fast search mode
- `includeArchivedNotes`: Include archived notes (default: false)
- `orderBy`: Field to order by (e.g., `title`, `dateModified`)
- `orderDirection`: `asc` or `desc`
- `limit`: Maximum results (default: 10)
- `debug`: Enable debug info
- **Response:** Array of note objects
#### Get Note
- **GET** `/notes/{noteId}`
- **Description:** Get note metadata by ID
- **Path Parameters:**
- `noteId`: Note ID
- **Response:** Note object with metadata
#### Get Note Content
- **GET** `/notes/{noteId}/content`
- **Description:** Get note content (HTML/text for text notes, binary for files/images)
- **Path Parameters:**
- `noteId`: Note ID
- **Response:** Note content (content-type varies by note type)
#### Update Note Content
- **PUT** `/notes/{noteId}/content`
- **Description:** Update note content
- **Path Parameters:**
- `noteId`: Note ID
- **Request Body:** Raw content (HTML for text notes, binary for files)
- **Response:** `204` No Content
#### Update Note Metadata
- **PATCH** `/notes/{noteId}`
- **Description:** Update note metadata (title, type, mime, etc.)
- **Path Parameters:**
- `noteId`: Note ID
- **Request Body:**
```json
{
"title": "Updated Title",
"type": "text",
"mime": "text/html"
}
```
- **Response:** `200` with updated note object
#### Delete Note
- **DELETE** `/notes/{noteId}`
- **Description:** Delete note and all its branches
- **Path Parameters:**
- `noteId`: Note ID
- **Response:** `204` No Content
- **Note:** Deletes all clones/branches of the note
#### Export Note
- **GET** `/notes/{noteId}/export`
- **Description:** Export note as ZIP file (with optional subtree)
- **Path Parameters:**
- `noteId`: Note ID (use "root" to export entire tree)
- **Query Parameters:**
- `format`: `html` or `markdown`/`md`
- **Response:** ZIP file download
---
### Branches Management
Branches represent note clones/placements in the tree. A single note can exist in multiple locations via different branches.
#### Create Branch
- **POST** `/branches`
- **Description:** Create a branch (clone a note to another location)
- **Request Body:**
```json
{
"noteId": "existingNoteId",
"parentNoteId": "targetParentId",
"prefix": "Branch Prefix",
"notePosition": 10,
"isExpanded": false,
"branchId": "customBranchId"
}
```
- **Required Fields:** `noteId`, `parentNoteId`
- **Response:** `201` with Branch object
#### Get Branch
- **GET** `/branches/{branchId}`
- **Description:** Get branch by ID
- **Path Parameters:**
- `branchId`: Branch ID
- **Response:** Branch object
#### Update Branch
- **PATCH** `/branches/{branchId}`
- **Description:** Update branch (prefix, notePosition)
- **Path Parameters:**
- `branchId`: Branch ID
- **Request Body:**
```json
{
"prefix": "New Prefix",
"notePosition": 20,
"isExpanded": true
}
```
- **Response:** `200` with updated branch
- **Note:** Only `prefix`, `notePosition`, and `isExpanded` can be updated. For other properties, delete and recreate.
#### Set Branch Prefix
- **PATCH** `/branches/{branchId}/set-prefix`
- **Description:** Set branch prefix
- **Path Parameters:**
- `branchId`: Branch ID
- **Request Body:**
```json
{
"prefix": "New Prefix"
}
```
#### Move Branch to Parent
- **POST** `/branches/{branchId}/set-note-to-parent`
- **Description:** Move branch to a different parent
- **Path Parameters:**
- `branchId`: Branch ID
- **Request Body:**
```json
{
"parentNoteId": "newParentId"
}
```
#### Delete Branch
- **DELETE** `/branches/{branchId}`
- **Description:** Delete branch (removes note from this tree location)
- **Path Parameters:**
- `branchId`: Branch ID
- **Response:** `204` No Content
- **Note:** If this is the last branch of the note, the note itself is deleted
#### Refresh Note Ordering
- **PATCH** `/refresh-note-ordering/{parentNoteId}`
- **Description:** Push notePosition changes to connected clients
- **Path Parameters:**
- `parentNoteId`: Parent note ID
- **Note:** Call this after updating branch notePositions to sync changes to clients
---
### Attributes Management
Attributes include labels (key-value metadata) and relations (links between notes).
#### Create Attribute
- **POST** `/attributes`
- **Description:** Create an attribute
- **Request Body:**
```json
{
"noteId": "targetNoteId",
"type": "label",
"name": "priority",
"value": "high",
"position": 10,
"isInheritable": false,
"attributeId": "customAttributeId"
}
```
- **Attribute Types:**
- `label`: Key-value metadata
- `relation`: Link to another note (value is target noteId)
- **Required Fields:** `noteId`, `type`, `name`
- **Optional Fields:** `value`, `position`, `isInheritable`, `attributeId`
- **Response:** `201` with Attribute object
#### Create Attribute for Note
- **POST** `/notes/{noteId}/attributes`
- **Description:** Create attribute for specific note
- **Path Parameters:**
- `noteId`: Note ID
- **Request Body:** Same as Create Attribute (noteId not required)
#### Get Attribute
- **GET** `/attributes/{attributeId}`
- **Description:** Get attribute by ID
- **Path Parameters:**
- `attributeId`: Attribute ID
- **Response:** Attribute object
#### Get Note Attributes
- **GET** `/notes/{noteId}/attributes`
- **Description:** Get all attributes for a note
- **Path Parameters:**
- `noteId`: Note ID
- **Response:** Array of attribute objects
#### Update Attribute
- **PATCH** `/attributes/{attributeId}`
- **Description:** Update attribute (name, value, position)
- **Path Parameters:**
- `attributeId`: Attribute ID
- **Request Body:**
```json
{
"name": "newName",
"value": "newValue",
"position": 20,
"isInheritable": true
}
```
- **Response:** `200` with updated attribute
#### Delete Attribute
- **DELETE** `/attributes/{attributeId}`
- **Description:** Delete attribute
- **Path Parameters:**
- `attributeId`: Attribute ID
- **Response:** `204` No Content
---
### Attachments Management
#### Create Attachment
- **POST** `/attachments`
- **Description:** Create attachment for a note
- **Request Body:** Multipart form data with file
```json
{
"ownerId": "noteId",
"role": "image",
"mime": "image/png",
"title": "Screenshot",
"position": 10,
"attachmentId": "customAttachmentId"
}
```
- **Required Fields:** `ownerId`, file data
- **Optional Fields:** `role`, `mime`, `title`, `position`, `attachmentId`
- **Response:** `201` with Attachment object
#### Create Attachment for Note
- **POST** `/notes/{noteId}/attachments`
- **Description:** Create attachment (alternative endpoint)
- **Path Parameters:**
- `noteId`: Note ID
- **Request Body:** Same as Create Attachment (ownerId not required)
#### Get Attachment
- **GET** `/attachments/{attachmentId}`
- **Description:** Get attachment metadata
- **Path Parameters:**
- `attachmentId`: Attachment ID
- **Response:** Attachment object
#### Get Attachment Content
- **GET** `/attachments/{attachmentId}/content`
- **Description:** Get attachment binary content
- **Path Parameters:**
- `attachmentId`: Attachment ID
- **Response:** Binary content with appropriate MIME type
#### Get Note Attachments
- **GET** `/notes/{noteId}/attachments`
- **Description:** Get all attachments for a note
- **Path Parameters:**
- `noteId`: Note ID
- **Response:** Array of attachment objects
#### Update Attachment Content
- **PUT** `/attachments/{attachmentId}/content`
- **Description:** Update attachment binary content
- **Path Parameters:**
- `attachmentId`: Attachment ID
- **Request Body:** Binary file data
- **Response:** `204` No Content
#### Update Attachment Metadata
- **PATCH** `/attachments/{attachmentId}`
- **Description:** Update attachment metadata
- **Path Parameters:**
- `attachmentId`: Attachment ID
- **Request Body:**
```json
{
"title": "New Title",
"role": "image",
"mime": "image/jpeg",
"position": 20
}
```
- **Response:** `200` with updated attachment
#### Delete Attachment
- **DELETE** `/attachments/{attachmentId}`
- **Description:** Delete attachment
- **Path Parameters:**
- `attachmentId`: Attachment ID
- **Response:** `204` No Content
---
### Special Purpose Endpoints
#### Get Inbox Note
- **GET** `/inbox/{date}`
- **Description:** Get or create inbox note for specific date
- **Path Parameters:**
- `date`: Date in format `YYYY-MM-DD`
- **Response:** Note object
- **Behavior:**
- Returns fixed inbox note (marked with `#inbox` label) if configured
- Otherwise returns/creates day note in journal for the specified date
#### Get Day Note
- **GET** `/calendar/days/{date}`
- **Description:** Get or create day note
- **Path Parameters:**
- `date`: Date in format `YYYY-MM-DD` (e.g., `2022-12-31`)
- **Response:** Note object
- **Note:** Creates note if it doesn't exist
#### Get Month Note
- **GET** `/calendar/months/{month}`
- **Description:** Get or create month note
- **Path Parameters:**
- `month`: Month in format `YYYY-MM` (e.g., `2022-12`)
- **Response:** Note object
- **Note:** Creates note if it doesn't exist
#### Get Year Note
- **GET** `/calendar/years/{year}`
- **Description:** Get or create year note
- **Path Parameters:**
- `year`: Year in format `YYYY` (e.g., `2022`)
- **Response:** Note object
- **Note:** Creates note if it doesn't exist
---
### Backup
#### Create Backup
- **PUT** `/backup/{backupName}`
- **Description:** Create a database backup
- **Path Parameters:**
- `backupName`: Backup filename (without extension)
- **Example:** `PUT /backup/now` creates `backup-now.db`
- **Response:** `204` No Content
---
## Data Types and Schemas
### Common Field Types
- **EntityId**: 12-character alphanumeric string (e.g., `evnnmvHTCgIn`)
- **LocalDateTime**: `YYYY-MM-DD HH:mm:ss.SSS±ZZZZ` (e.g., `2021-12-31 20:18:11.930+0100`)
- **UtcDateTime**: `YYYY-MM-DD HH:mm:ss.SSSZ` (e.g., `2021-12-31 19:18:11.930Z`)
### Note Position
- Normal ordering: 10, 20, 30, 40...
- First position: use value < 10 (e.g., 5)
- Last position: use large value (e.g., 1000000)
- Between existing: use value between their positions
### Branch Prefix
Branch-specific title prefix displayed in the tree. Useful when same note appears in multiple locations with slightly different context.
---
## Error Responses
All endpoints may return these error responses:
### Standard Error Object
```json
{
"status": 400,
"code": "NOTE_IS_PROTECTED",
"message": "Note 'evnnmvHTCgIn' is protected and cannot be modified through ETAPI"
}
```
### Common HTTP Status Codes
- `200`: Success
- `201`: Resource created
- `204`: Success (no content)
- `400`: Bad request (validation error)
- `401`: Unauthorized (invalid token)
- `404`: Not found
- `429`: Too many requests (rate limited/blacklisted)
- `500`: Internal server error
### Common Error Codes
- `NOTE_IS_PROTECTED`: Protected note cannot be modified
- `INVALID_TOKEN`: Invalid or expired ETAPI token
- `VALIDATION_ERROR`: Request validation failed
- `NOT_FOUND`: Resource not found
- `RATE_LIMITED`: Too many requests
---
## Search Query Syntax
The `/notes` search endpoint supports Trilium's query language:
### Basic Search
```
python # Search in title and content
#todo # Find notes with label "todo"
~project # Find notes with relation "project"
```
### Advanced Operators
```
note.title =* "meeting" # Title contains "meeting"
note.title %= ".*2022.*" # Regex in title
#priority = "high" # Label with specific value
~template = "someNoteId" # Relation to specific note
#created >= MONTH-1 # Created in last month
note.dateModified >= "2022-01-01" # Modified after date
```
### Combining Queries
```
#todo AND #urgent # Both labels
#work OR #personal # Either label
#project AND note.title =* "Q1" # Label AND title condition
```
### Hierarchical Queries
```
note.parents.title = "Work" # Direct parent title
note.ancestors.title = "Archive" # Any ancestor title
note.children.title =* "Chapter" # Direct children
```
See Trilium Search Documentation for complete syntax.
---
## Rate Limiting
- Failed authentication attempts can result in IP blacklist
- Blacklisted IPs receive `429` response
- Wait period required before retry
- Use valid tokens to avoid blacklisting
---
## Configuration Notes
### Upload Size Limits
- Default: 250MB
- Disable limit: Set `TRILIUM_NO_UPLOAD_LIMIT=true`
- Custom limit: Set `MAX_ALLOWED_FILE_SIZE_MB=<size>`
### Network Configuration
ETAPI accessible through:
- Local interface: `http://localhost:8080/etapi`
- Network interface: Configure reverse proxy (nginx/Apache)
- SSL/TLS: Recommended for production use
---
## Best Practices
1. **Always use ETAPI tokens** (not passwords) for authentication
2. **Store tokens securely** - they provide full access to your Trilium instance
3. **Use notePosition strategically** - leave gaps (10, 20, 30) for easy insertion
4. **Handle branches carefully** - deleting last branch deletes the note
5. **Check for protected notes** - they cannot be modified via ETAPI
6. **Implement rate limiting** in your client to avoid blacklisting
7. **Use search efficiently** - leverage fastSearch for better performance
8. **Call refresh-note-ordering** after bulk branch position updates
9. **Validate data before submission** - reduce error responses
10. **Handle errors gracefully** - check status codes and error messages
---
## Example Workflows
### Create a Note with Attributes
```bash
# 1. Create note
NOTE_RESPONSE=$(curl -X POST "$SERVER/etapi/create-note" \
-H "Authorization: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"parentNoteId": "root",
"title": "Project TODO",
"type": "text",
"content": "<p>Task list</p>"
}')
NOTE_ID=$(echo $NOTE_RESPONSE | jq -r '.note.noteId')
# 2. Add label
curl -X POST "$SERVER/etapi/attributes" \
-H "Authorization: $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"noteId\": \"$NOTE_ID\",
\"type\": \"label\",
\"name\": \"priority\",
\"value\": \"high\"
}"
```
### Clone Note to Multiple Locations
```bash
# Clone note to another parent
curl -X POST "$SERVER/etapi/branches" \
-H "Authorization: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"noteId": "existingNoteId",
"parentNoteId": "anotherParentId",
"prefix": "Reference: "
}'
```
### Daily Journal Entry
```bash
# Get or create today's note
TODAY=$(date +%Y-%m-%d)
curl "$SERVER/etapi/calendar/days/$TODAY" \
-H "Authorization: $TOKEN"
```
---
## Client Libraries
### Python
- **trilium-py**: Full-featured client with extended functionality
- **PyTrilium**: Lightweight wrapper matching OpenAPI spec
- **trilium-alchemy**: SQLAlchemy-style SDK with CLI toolkit
### Node.js
- **trilium-etapi**: TypeScript wrapper with type safety
### Other Tools
- **trilium-mcp-server**: Model Context Protocol server for LLMs
- **openapi-mcp-generator**: Generate MCP servers from OpenAPI specs
---
## Version Compatibility
- ETAPI introduced: Trilium v0.50
- Basic Auth support: v0.56
- Bearer token format: v0.93.0
- TriliumNext fork: Compatible with Trilium API, ongoing development
Check `/app-info` endpoint for version details of your instance.
---
## Additional Resources
- **Official Documentation**: https://docs.triliumnotes.org/
- **GitHub Repository**: https://github.com/TriliumNext/Trilium
- **Search Syntax Guide**: https://github.com/zadam/trilium/wiki/Search
- **Community Resources**: https://github.com/Nriver/awesome-trilium
---
**License:** Apache 2.0
**Maintainer:** TriliumNext Community
**Contact:** https://github.com/TriliumNext/Trilium/discussions
-441
View File
@@ -1,441 +0,0 @@
├── CHANGELOG.md
├── core
│ ├── env experiments
│ ├── persona-sidecar
│ │ ├── Dockerfile
│ │ ├── package.json
│ │ ├── persona-server.js
│ │ └── personas.json
│ ├── relay
│ │ ├── Dockerfile
│ │ ├── lib
│ │ │ ├── cortex.js
│ │ │ └── llm.js
│ │ ├── package.json
│ │ ├── package-lock.json
│ │ ├── server.js
│ │ ├── sessions
│ │ │ ├── default.jsonl
│ │ │ ├── sess-6rxu7eia.json
│ │ │ ├── sess-6rxu7eia.jsonl
│ │ │ ├── sess-l08ndm60.json
│ │ │ └── sess-l08ndm60.jsonl
│ │ └── test-llm.js
│ ├── relay-backup
│ └── ui
│ ├── index.html
│ ├── manifest.json
│ └── style.css
├── cortex
│ ├── context.py
│ ├── Dockerfile
│ ├── ingest
│ │ ├── ingest_handler.py
│ │ ├── __init__.py
│ │ └── intake_client.py
│ ├── intake
│ │ ├── __init__.py
│ │ ├── intake.py
│ │ └── logs
│ ├── llm
│ │ ├── __init__.py
│ │ └── llm_router.py
│ ├── logs
│ │ ├── cortex_verbose_debug.log
│ │ └── reflections.log
│ ├── main.py
│ ├── neomem_client.py
│ ├── persona
│ │ ├── identity.py
│ │ ├── __init__.py
│ │ └── speak.py
│ ├── rag.py
│ ├── reasoning
│ │ ├── __init__.py
│ │ ├── reasoning.py
│ │ ├── refine.py
│ │ └── reflection.py
│ ├── requirements.txt
│ ├── router.py
│ ├── tests
│ └── utils
│ ├── config.py
│ ├── __init__.py
│ ├── log_utils.py
│ └── schema.py
├── deprecated.env.txt
├── DEPRECATED_FILES.md
├── docker-compose.yml
├── docs
│ ├── ARCHITECTURE_v0-6-0.md
│ ├── ENVIRONMENT_VARIABLES.md
│ ├── lyra_tree.txt
│ └── PROJECT_SUMMARY.md
├── intake-logs
│ └── summaries.log
├── neomem
│ ├── _archive
│ │ └── old_servers
│ │ ├── main_backup.py
│ │ └── main_dev.py
│ ├── docker-compose.yml
│ ├── Dockerfile
│ ├── neomem
│ │ ├── api
│ │ ├── client
│ │ │ ├── __init__.py
│ │ │ ├── main.py
│ │ │ ├── project.py
│ │ │ └── utils.py
│ │ ├── configs
│ │ │ ├── base.py
│ │ │ ├── embeddings
│ │ │ │ ├── base.py
│ │ │ │ └── __init__.py
│ │ │ ├── enums.py
│ │ │ ├── __init__.py
│ │ │ ├── llms
│ │ │ │ ├── anthropic.py
│ │ │ │ ├── aws_bedrock.py
│ │ │ │ ├── azure.py
│ │ │ │ ├── base.py
│ │ │ │ ├── deepseek.py
│ │ │ │ ├── __init__.py
│ │ │ │ ├── lmstudio.py
│ │ │ │ ├── ollama.py
│ │ │ │ ├── openai.py
│ │ │ │ └── vllm.py
│ │ │ ├── prompts.py
│ │ │ └── vector_stores
│ │ │ ├── azure_ai_search.py
│ │ │ ├── azure_mysql.py
│ │ │ ├── baidu.py
│ │ │ ├── chroma.py
│ │ │ ├── databricks.py
│ │ │ ├── elasticsearch.py
│ │ │ ├── faiss.py
│ │ │ ├── __init__.py
│ │ │ ├── langchain.py
│ │ │ ├── milvus.py
│ │ │ ├── mongodb.py
│ │ │ ├── neptune.py
│ │ │ ├── opensearch.py
│ │ │ ├── pgvector.py
│ │ │ ├── pinecone.py
│ │ │ ├── qdrant.py
│ │ │ ├── redis.py
│ │ │ ├── s3_vectors.py
│ │ │ ├── supabase.py
│ │ │ ├── upstash_vector.py
│ │ │ ├── valkey.py
│ │ │ ├── vertex_ai_vector_search.py
│ │ │ └── weaviate.py
│ │ ├── core
│ │ ├── embeddings
│ │ │ ├── aws_bedrock.py
│ │ │ ├── azure_openai.py
│ │ │ ├── base.py
│ │ │ ├── configs.py
│ │ │ ├── gemini.py
│ │ │ ├── huggingface.py
│ │ │ ├── __init__.py
│ │ │ ├── langchain.py
│ │ │ ├── lmstudio.py
│ │ │ ├── mock.py
│ │ │ ├── ollama.py
│ │ │ ├── openai.py
│ │ │ ├── together.py
│ │ │ └── vertexai.py
│ │ ├── exceptions.py
│ │ ├── graphs
│ │ │ ├── configs.py
│ │ │ ├── __init__.py
│ │ │ ├── neptune
│ │ │ │ ├── base.py
│ │ │ │ ├── __init__.py
│ │ │ │ ├── neptunedb.py
│ │ │ │ └── neptunegraph.py
│ │ │ ├── tools.py
│ │ │ └── utils.py
│ │ ├── __init__.py
│ │ ├── LICENSE
│ │ ├── llms
│ │ │ ├── anthropic.py
│ │ │ ├── aws_bedrock.py
│ │ │ ├── azure_openai.py
│ │ │ ├── azure_openai_structured.py
│ │ │ ├── base.py
│ │ │ ├── configs.py
│ │ │ ├── deepseek.py
│ │ │ ├── gemini.py
│ │ │ ├── groq.py
│ │ │ ├── __init__.py
│ │ │ ├── langchain.py
│ │ │ ├── litellm.py
│ │ │ ├── lmstudio.py
│ │ │ ├── ollama.py
│ │ │ ├── openai.py
│ │ │ ├── openai_structured.py
│ │ │ ├── sarvam.py
│ │ │ ├── together.py
│ │ │ ├── vllm.py
│ │ │ └── xai.py
│ │ ├── memory
│ │ │ ├── base.py
│ │ │ ├── graph_memory.py
│ │ │ ├── __init__.py
│ │ │ ├── kuzu_memory.py
│ │ │ ├── main.py
│ │ │ ├── memgraph_memory.py
│ │ │ ├── setup.py
│ │ │ ├── storage.py
│ │ │ ├── telemetry.py
│ │ │ └── utils.py
│ │ ├── proxy
│ │ │ ├── __init__.py
│ │ │ └── main.py
│ │ ├── server
│ │ │ ├── dev.Dockerfile
│ │ │ ├── docker-compose.yaml
│ │ │ ├── Dockerfile
│ │ │ ├── main_old.py
│ │ │ ├── main.py
│ │ │ ├── Makefile
│ │ │ ├── README.md
│ │ │ └── requirements.txt
│ │ ├── storage
│ │ ├── utils
│ │ │ └── factory.py
│ │ └── vector_stores
│ │ ├── azure_ai_search.py
│ │ ├── azure_mysql.py
│ │ ├── baidu.py
│ │ ├── base.py
│ │ ├── chroma.py
│ │ ├── configs.py
│ │ ├── databricks.py
│ │ ├── elasticsearch.py
│ │ ├── faiss.py
│ │ ├── __init__.py
│ │ ├── langchain.py
│ │ ├── milvus.py
│ │ ├── mongodb.py
│ │ ├── neptune_analytics.py
│ │ ├── opensearch.py
│ │ ├── pgvector.py
│ │ ├── pinecone.py
│ │ ├── qdrant.py
│ │ ├── redis.py
│ │ ├── s3_vectors.py
│ │ ├── supabase.py
│ │ ├── upstash_vector.py
│ │ ├── valkey.py
│ │ ├── vertex_ai_vector_search.py
│ │ └── weaviate.py
│ ├── neomem_history
│ │ └── history.db
│ ├── pyproject.toml
│ ├── README.md
│ └── requirements.txt
├── neomem_history
│ └── history.db
├── rag
│ ├── chatlogs
│ │ └── lyra
│ │ ├── 0000_Wire_ROCm_to_Cortex.json
│ │ ├── 0001_Branch___10_22_ct201branch-ssh_tut.json
│ │ ├── 0002_cortex_LLMs_11-1-25.json
│ │ ├── 0003_RAG_beta.json
│ │ ├── 0005_Cortex_v0_4_0_planning.json
│ │ ├── 0006_Cortex_v0_4_0_Refinement.json
│ │ ├── 0009_Branch___Cortex_v0_4_0_planning.json
│ │ ├── 0012_Cortex_4_-_neomem_11-1-25.json
│ │ ├── 0016_Memory_consolidation_concept.json
│ │ ├── 0017_Model_inventory_review.json
│ │ ├── 0018_Branch___Memory_consolidation_concept.json
│ │ ├── 0022_Branch___Intake_conversation_summaries.json
│ │ ├── 0026_Intake_conversation_summaries.json
│ │ ├── 0027_Trilium_AI_LLM_setup.json
│ │ ├── 0028_LLMs_and_sycophancy_levels.json
│ │ ├── 0031_UI_improvement_plan.json
│ │ ├── 0035_10_27-neomem_update.json
│ │ ├── 0044_Install_llama_cpp_on_ct201.json
│ │ ├── 0045_AI_task_assistant.json
│ │ ├── 0047_Project_scope_creation.json
│ │ ├── 0052_View_docker_container_logs.json
│ │ ├── 0053_10_21-Proxmox_fan_control.json
│ │ ├── 0054_10_21-pytorch_branch_Quant_experiments.json
│ │ ├── 0055_10_22_ct201branch-ssh_tut.json
│ │ ├── 0060_Lyra_project_folder_issue.json
│ │ ├── 0062_Build_pytorch_API.json
│ │ ├── 0063_PokerBrain_dataset_structure.json
│ │ ├── 0065_Install_PyTorch_setup.json
│ │ ├── 0066_ROCm_PyTorch_setup_quirks.json
│ │ ├── 0067_VM_model_setup_steps.json
│ │ ├── 0070_Proxmox_disk_error_fix.json
│ │ ├── 0072_Docker_Compose_vs_Portainer.json
│ │ ├── 0073_Check_system_temps_Proxmox.json
│ │ ├── 0075_Cortex_gpu_progress.json
│ │ ├── 0076_Backup_Proxmox_before_upgrade.json
│ │ ├── 0077_Storage_cleanup_advice.json
│ │ ├── 0082_Install_ROCm_on_Proxmox.json
│ │ ├── 0088_Thalamus_program_summary.json
│ │ ├── 0094_Cortex_blueprint_development.json
│ │ ├── 0095_mem0_advancments.json
│ │ ├── 0096_Embedding_provider_swap.json
│ │ ├── 0097_Update_git_commit_steps.json
│ │ ├── 0098_AI_software_description.json
│ │ ├── 0099_Seed_memory_process.json
│ │ ├── 0100_Set_up_Git_repo.json
│ │ ├── 0101_Customize_embedder_setup.json
│ │ ├── 0102_Seeding_Local_Lyra_memory.json
│ │ ├── 0103_Mem0_seeding_part_3.json
│ │ ├── 0104_Memory_build_prompt.json
│ │ ├── 0105_Git_submodule_setup_guide.json
│ │ ├── 0106_Serve_UI_on_LAN.json
│ │ ├── 0107_AI_name_suggestion.json
│ │ ├── 0108_Room_X_planning_update.json
│ │ ├── 0109_Salience_filtering_design.json
│ │ ├── 0110_RoomX_Cortex_build.json
│ │ ├── 0119_Explain_Lyra_cortex_idea.json
│ │ ├── 0120_Git_submodule_organization.json
│ │ ├── 0121_Web_UI_fix_guide.json
│ │ ├── 0122_UI_development_planning.json
│ │ ├── 0123_NVGRAM_debugging_steps.json
│ │ ├── 0124_NVGRAM_setup_troubleshooting.json
│ │ ├── 0125_NVGRAM_development_update.json
│ │ ├── 0126_RX_-_NeVGRAM_New_Features.json
│ │ ├── 0127_Error_troubleshooting_steps.json
│ │ ├── 0135_Proxmox_backup_with_ABB.json
│ │ ├── 0151_Auto-start_Lyra-Core_VM.json
│ │ ├── 0156_AI_GPU_benchmarks_comparison.json
│ │ └── 0251_Lyra_project_handoff.json
│ ├── chromadb
│ │ ├── c4f701ee-1978-44a1-9df4-3e865b5d33c1
│ │ │ ├── data_level0.bin
│ │ │ ├── header.bin
│ │ │ ├── index_metadata.pickle
│ │ │ ├── length.bin
│ │ │ └── link_lists.bin
│ │ └── chroma.sqlite3
│ ├── import.log
│ ├── lyra-chatlogs
│ │ ├── 0000_Wire_ROCm_to_Cortex.json
│ │ ├── 0001_Branch___10_22_ct201branch-ssh_tut.json
│ │ ├── 0002_cortex_LLMs_11-1-25.json
│ │ └── 0003_RAG_beta.json
│ ├── rag_api.py
│ ├── rag_build.py
│ ├── rag_chat_import.py
│ └── rag_query.py
├── README.md
└── volumes
├── neo4j_data
│ ├── databases
│ │ ├── neo4j
│ │ │ ├── database_lock
│ │ │ ├── id-buffer.tmp.0
│ │ │ ├── neostore
│ │ │ ├── neostore.counts.db
│ │ │ ├── neostore.indexstats.db
│ │ │ ├── neostore.labeltokenstore.db
│ │ │ ├── neostore.labeltokenstore.db.id
│ │ │ ├── neostore.labeltokenstore.db.names
│ │ │ ├── neostore.labeltokenstore.db.names.id
│ │ │ ├── neostore.nodestore.db
│ │ │ ├── neostore.nodestore.db.id
│ │ │ ├── neostore.nodestore.db.labels
│ │ │ ├── neostore.nodestore.db.labels.id
│ │ │ ├── neostore.propertystore.db
│ │ │ ├── neostore.propertystore.db.arrays
│ │ │ ├── neostore.propertystore.db.arrays.id
│ │ │ ├── neostore.propertystore.db.id
│ │ │ ├── neostore.propertystore.db.index
│ │ │ ├── neostore.propertystore.db.index.id
│ │ │ ├── neostore.propertystore.db.index.keys
│ │ │ ├── neostore.propertystore.db.index.keys.id
│ │ │ ├── neostore.propertystore.db.strings
│ │ │ ├── neostore.propertystore.db.strings.id
│ │ │ ├── neostore.relationshipgroupstore.db
│ │ │ ├── neostore.relationshipgroupstore.db.id
│ │ │ ├── neostore.relationshipgroupstore.degrees.db
│ │ │ ├── neostore.relationshipstore.db
│ │ │ ├── neostore.relationshipstore.db.id
│ │ │ ├── neostore.relationshiptypestore.db
│ │ │ ├── neostore.relationshiptypestore.db.id
│ │ │ ├── neostore.relationshiptypestore.db.names
│ │ │ ├── neostore.relationshiptypestore.db.names.id
│ │ │ ├── neostore.schemastore.db
│ │ │ ├── neostore.schemastore.db.id
│ │ │ └── schema
│ │ │ └── index
│ │ │ └── token-lookup-1.0
│ │ │ ├── 1
│ │ │ │ └── index-1
│ │ │ └── 2
│ │ │ └── index-2
│ │ ├── store_lock
│ │ └── system
│ │ ├── database_lock
│ │ ├── id-buffer.tmp.0
│ │ ├── neostore
│ │ ├── neostore.counts.db
│ │ ├── neostore.indexstats.db
│ │ ├── neostore.labeltokenstore.db
│ │ ├── neostore.labeltokenstore.db.id
│ │ ├── neostore.labeltokenstore.db.names
│ │ ├── neostore.labeltokenstore.db.names.id
│ │ ├── neostore.nodestore.db
│ │ ├── neostore.nodestore.db.id
│ │ ├── neostore.nodestore.db.labels
│ │ ├── neostore.nodestore.db.labels.id
│ │ ├── neostore.propertystore.db
│ │ ├── neostore.propertystore.db.arrays
│ │ ├── neostore.propertystore.db.arrays.id
│ │ ├── neostore.propertystore.db.id
│ │ ├── neostore.propertystore.db.index
│ │ ├── neostore.propertystore.db.index.id
│ │ ├── neostore.propertystore.db.index.keys
│ │ ├── neostore.propertystore.db.index.keys.id
│ │ ├── neostore.propertystore.db.strings
│ │ ├── neostore.propertystore.db.strings.id
│ │ ├── neostore.relationshipgroupstore.db
│ │ ├── neostore.relationshipgroupstore.db.id
│ │ ├── neostore.relationshipgroupstore.degrees.db
│ │ ├── neostore.relationshipstore.db
│ │ ├── neostore.relationshipstore.db.id
│ │ ├── neostore.relationshiptypestore.db
│ │ ├── neostore.relationshiptypestore.db.id
│ │ ├── neostore.relationshiptypestore.db.names
│ │ ├── neostore.relationshiptypestore.db.names.id
│ │ ├── neostore.schemastore.db
│ │ ├── neostore.schemastore.db.id
│ │ └── schema
│ │ └── index
│ │ ├── range-1.0
│ │ │ ├── 3
│ │ │ │ └── index-3
│ │ │ ├── 4
│ │ │ │ └── index-4
│ │ │ ├── 7
│ │ │ │ └── index-7
│ │ │ ├── 8
│ │ │ │ └── index-8
│ │ │ └── 9
│ │ │ └── index-9
│ │ └── token-lookup-1.0
│ │ ├── 1
│ │ │ └── index-1
│ │ └── 2
│ │ └── index-2
│ ├── dbms
│ │ └── auth.ini
│ ├── server_id
│ └── transactions
│ ├── neo4j
│ │ ├── checkpoint.0
│ │ └── neostore.transaction.db.0
│ └── system
│ ├── checkpoint.0
│ └── neostore.transaction.db.0
└── postgres_data [error opening dir]
+31
View File
@@ -0,0 +1,31 @@
"""Environment-driven configuration."""
from __future__ import annotations
import os
from dataclasses import dataclass
from pathlib import Path
from dotenv import load_dotenv
load_dotenv()
@dataclass(frozen=True)
class Config:
local_base_url: str
local_model: str
openai_api_key: str
cloud_model: str
embed_model: str
db_path: Path
def load() -> Config:
return Config(
local_base_url=os.getenv("LOCAL_BASE_URL", "http://localhost:11434"),
local_model=os.getenv("LOCAL_MODEL", "qwen2.5:7b-instruct"),
openai_api_key=os.getenv("OPENAI_API_KEY", ""),
cloud_model=os.getenv("CLOUD_MODEL", "gpt-4o-mini"),
embed_model=os.getenv("EMBED_MODEL", "text-embedding-3-small"),
db_path=Path(os.getenv("LYRA_DB_PATH", "data/lyra.db")),
)
+44
View File
@@ -0,0 +1,44 @@
"""LLM router: local (Ollama) chat, cloud (OpenAI) chat + embeddings."""
from __future__ import annotations
from typing import Literal, TypedDict
import httpx
from openai import OpenAI
from lyra.config import load
class Message(TypedDict):
role: Literal["system", "user", "assistant"]
content: str
Backend = Literal["local", "cloud"]
def complete(messages: list[Message], backend: Backend = "local") -> str:
cfg = load()
if backend == "cloud":
if not cfg.openai_api_key:
raise RuntimeError("OPENAI_API_KEY is not set")
client = OpenAI(api_key=cfg.openai_api_key)
resp = client.chat.completions.create(model=cfg.cloud_model, messages=messages)
return resp.choices[0].message.content or ""
resp = httpx.post(
f"{cfg.local_base_url}/api/chat",
json={"model": cfg.local_model, "messages": messages, "stream": False},
timeout=120,
)
resp.raise_for_status()
return resp.json()["message"]["content"]
def embed(texts: list[str]) -> list[list[float]]:
cfg = load()
if not cfg.openai_api_key:
raise RuntimeError("OPENAI_API_KEY is not set")
client = OpenAI(api_key=cfg.openai_api_key)
resp = client.embeddings.create(model=cfg.embed_model, input=texts)
return [d.embedding for d in resp.data]
+133
View File
@@ -0,0 +1,133 @@
"""Persistent memory: SQLite storage + brute-force cosine recall over embeddings.
Each exchange is stored with its OpenAI embedding as a float32 BLOB. Recall
loads all embeddings (optionally scoped to a session) into a matrix and
returns the top-k by cosine similarity. Brute force is fine up to tens of
thousands of rows; swap in a vector index when that stops being true.
"""
from __future__ import annotations
import sqlite3
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
import numpy as np
from lyra import llm
from lyra.config import load
SCHEMA = """
CREATE TABLE IF NOT EXISTS exchanges (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL,
embedding BLOB NOT NULL,
created_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_session_created ON exchanges(session_id, created_at);
"""
_conn: sqlite3.Connection | None = None
_conn_path: Path | None = None
def _connection() -> sqlite3.Connection:
"""Lazily open the SQLite connection. Reopens if LYRA_DB_PATH changed (for tests)."""
global _conn, _conn_path
cfg = load()
if _conn is None or _conn_path != cfg.db_path:
if _conn is not None:
_conn.close()
cfg.db_path.parent.mkdir(parents=True, exist_ok=True)
_conn = sqlite3.connect(cfg.db_path)
_conn.row_factory = sqlite3.Row
_conn.executescript(SCHEMA)
_conn_path = cfg.db_path
return _conn
@dataclass
class Exchange:
id: int
session_id: str
role: str
content: str
created_at: str
score: float | None = None
def _to_blob(vec: list[float]) -> bytes:
return np.asarray(vec, dtype=np.float32).tobytes()
def _from_blob(blob: bytes) -> np.ndarray:
return np.frombuffer(blob, dtype=np.float32)
def remember(session_id: str, role: str, content: str) -> int:
"""Embed and persist a single exchange. Returns the new row id."""
[embedding] = llm.embed([content])
now = datetime.now(timezone.utc).isoformat()
conn = _connection()
with conn:
cur = conn.execute(
"INSERT INTO exchanges (session_id, role, content, embedding, created_at) "
"VALUES (?, ?, ?, ?, ?)",
(session_id, role, content, _to_blob(embedding), now),
)
return int(cur.lastrowid)
def recent(session_id: str, n: int = 10) -> list[Exchange]:
"""Last `n` exchanges from a session, oldest first."""
conn = _connection()
rows = conn.execute(
"SELECT id, session_id, role, content, created_at FROM exchanges "
"WHERE session_id = ? ORDER BY id DESC LIMIT ?",
(session_id, n),
).fetchall()
return [
Exchange(
id=r["id"],
session_id=r["session_id"],
role=r["role"],
content=r["content"],
created_at=r["created_at"],
)
for r in reversed(rows)
]
def recall(query: str, k: int = 5, session_id: str | None = None) -> list[Exchange]:
"""Top-k exchanges semantically similar to `query`, optionally scoped to a session."""
[q_vec] = llm.embed([query])
q = np.asarray(q_vec, dtype=np.float32)
conn = _connection()
sql = "SELECT id, session_id, role, content, embedding, created_at FROM exchanges"
params: tuple = ()
if session_id is not None:
sql += " WHERE session_id = ?"
params = (session_id,)
rows = conn.execute(sql, params).fetchall()
if not rows:
return []
matrix = np.stack([_from_blob(r["embedding"]) for r in rows])
norms = np.linalg.norm(matrix, axis=1)
scores = (matrix @ q) / (norms * np.linalg.norm(q) + 1e-9)
top_idx = np.argsort(scores)[::-1][:k]
return [
Exchange(
id=rows[i]["id"],
session_id=rows[i]["session_id"],
role=rows[i]["role"],
content=rows[i]["content"],
created_at=rows[i]["created_at"],
score=float(scores[i]),
)
for i in top_idx
]
-44
View File
@@ -1,44 +0,0 @@
# ───────────────────────────────
# Python build/cache files
__pycache__/
*.pyc
# ───────────────────────────────
# Environment + secrets
.env
.env.*
.env.local
.env.3090
.env.backup
.env.openai
# ───────────────────────────────
# Runtime databases & history
*.db
nvgram-history/ # renamed from mem0_history
mem0_history/ # keep for now (until all old paths are gone)
mem0_data/ # legacy - safe to ignore if it still exists
seed-mem0/ # old seed folder
seed-nvgram/ # new seed folder (if you rename later)
history/ # generic log/history folder
lyra-seed
# ───────────────────────────────
# Docker artifacts
*.log
*.pid
*.sock
docker-compose.override.yml
.docker/
# ───────────────────────────────
# User/system caches
.cache/
.local/
.ssh/
.npm/
# ───────────────────────────────
# IDE/editor garbage
.vscode/
.idea/
*.swp
-49
View File
@@ -1,49 +0,0 @@
# ───────────────────────────────
# Stage 1 — Base Image
# ───────────────────────────────
FROM python:3.11-slim AS base
# Prevent Python from writing .pyc files and force unbuffered output
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
WORKDIR /app
# Install system dependencies (Postgres client + build tools)
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libpq-dev \
curl \
&& rm -rf /var/lib/apt/lists/*
# ───────────────────────────────
# Stage 2 — Install Python dependencies
# ───────────────────────────────
COPY requirements.txt .
RUN apt-get update && apt-get install -y --no-install-recommends \
gfortran pkg-config libopenblas-dev liblapack-dev \
&& rm -rf /var/lib/apt/lists/*
RUN pip install --only-binary=:all: numpy scipy && \
pip install --no-cache-dir -r requirements.txt && \
pip install --no-cache-dir "mem0ai[graph]" psycopg[pool] psycopg2-binary
# ───────────────────────────────
# Stage 3 — Copy application
# ───────────────────────────────
COPY neomem ./neomem
# ───────────────────────────────
# Stage 4 — Runtime configuration
# ───────────────────────────────
ENV HOST=0.0.0.0 \
PORT=7077
EXPOSE 7077
# ───────────────────────────────
# Stage 5 — Entrypoint
# ───────────────────────────────
CMD ["uvicorn", "neomem.server.main:app", "--host", "0.0.0.0", "--port", "7077", "--no-access-log"]
-146
View File
@@ -1,146 +0,0 @@
# 🧠 neomem
**neomem** is a local-first vector memory engine derived from the open-source **Mem0** project.
It provides persistent, structured storage and semantic retrieval for AI companions like **Lyra** — with zero cloud dependencies.
---
## 🚀 Overview
- **Origin:** Forked from Mem0 OSS (Apache 2.0)
- **Purpose:** Replace Mem0 as Lyras canonical on-prem memory backend
- **Core stack:**
- FastAPI (API layer)
- PostgreSQL + pgvector (structured + vector data)
- Neo4j (entity graph)
- **Language:** Python 3.11+
- **License:** Apache 2.0 (original Mem0) + local modifications © 2025 ServersDown Labs
---
## ⚙️ Features
| Layer | Function | Notes |
|-------|-----------|-------|
| **FastAPI** | `/memories`, `/search` endpoints | Drop-in compatible with Mem0 |
| **Postgres (pgvector)** | Memory payload + embeddings | JSON payload schema |
| **Neo4j** | Entity graph relationships | auto-linked per memory |
| **Local Embedding** | via Ollama or OpenAI | configurable in `.env` |
| **Fully Offline Mode** | ✅ | No external SDK or telemetry |
| **Dockerized** | ✅ | `docker-compose.yml` included |
---
## 📦 Requirements
- Docker + Docker Compose
- Python 3.11 (if running bare-metal)
- PostgreSQL 15+ with `pgvector` extension
- Neo4j 5.x
- Optional: Ollama for local embeddings
**Dependencies (requirements.txt):**
```txt
fastapi==0.115.8
uvicorn==0.34.0
pydantic==2.10.4
python-dotenv==1.0.1
psycopg>=3.2.8
ollama
```
---
## 🧩 Setup
1. **Clone & build**
```bash
git clone https://github.com/serversdown/neomem.git
cd neomem
docker compose -f docker-compose.neomem.yml up -d --build
```
2. **Verify startup**
```bash
curl http://localhost:7077/docs
```
Expected output:
```
✅ Connected to Neo4j on attempt 1
INFO: Uvicorn running on http://0.0.0.0:7077
```
---
## 🔌 API Endpoints
### Add Memory
```bash
POST /memories
```
```json
{
"messages": [
{"role": "user", "content": "I like coffee in the morning"}
],
"user_id": "brian"
}
```
### Search Memory
```bash
POST /search
```
```json
{
"query": "coffee",
"user_id": "brian"
}
```
---
## 🗄️ Data Flow
```
Request → FastAPI → Embedding (Ollama/OpenAI)
Postgres (payload store)
Neo4j (graph links)
Search / Recall
```
---
## 🧱 Integration with Lyra
- Lyra Relay connects to `neomem-api:8000` (Docker) or `localhost:7077` (local).
- Identical endpoints to Mem0 mean **no code changes** in Lyra Core.
- Designed for **persistent, private** operation on your own hardware.
---
## 🧯 Shutdown
```bash
docker compose -f docker-compose.neomem.yml down
```
Then power off the VM or Proxmox guest safely.
---
## 🧾 License
neomem is a derivative work based on the **Mem0 OSS** project (Apache 2.0).
It retains the original Apache 2.0 license and adds local modifications.
© 2025 ServersDown Labs / Terra-Mechanics.
All modifications released under Apache 2.0.
---
## 📅 Version
**neomem v0.1.0** — 2025-10-07
_Initial fork from Mem0 OSS with full independence and local-first architecture._
-262
View File
@@ -1,262 +0,0 @@
import logging
import os
from typing import Any, Dict, List, Optional
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse, RedirectResponse
from pydantic import BaseModel, Field
from nvgram import Memory
app = FastAPI(title="NVGRAM", version="0.1.1")
@app.get("/health")
def health():
return {
"status": "ok",
"version": app.version,
"service": app.title
}
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
# Load environment variables
load_dotenv()
POSTGRES_HOST = os.environ.get("POSTGRES_HOST", "postgres")
POSTGRES_PORT = os.environ.get("POSTGRES_PORT", "5432")
POSTGRES_DB = os.environ.get("POSTGRES_DB", "postgres")
POSTGRES_USER = os.environ.get("POSTGRES_USER", "postgres")
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "postgres")
POSTGRES_COLLECTION_NAME = os.environ.get("POSTGRES_COLLECTION_NAME", "memories")
NEO4J_URI = os.environ.get("NEO4J_URI", "bolt://neo4j:7687")
NEO4J_USERNAME = os.environ.get("NEO4J_USERNAME", "neo4j")
NEO4J_PASSWORD = os.environ.get("NEO4J_PASSWORD", "mem0graph")
MEMGRAPH_URI = os.environ.get("MEMGRAPH_URI", "bolt://localhost:7687")
MEMGRAPH_USERNAME = os.environ.get("MEMGRAPH_USERNAME", "memgraph")
MEMGRAPH_PASSWORD = os.environ.get("MEMGRAPH_PASSWORD", "mem0graph")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
HISTORY_DB_PATH = os.environ.get("HISTORY_DB_PATH", "/app/history/history.db")
# Embedder settings (switchable by .env)
EMBEDDER_PROVIDER = os.environ.get("EMBEDDER_PROVIDER", "openai")
EMBEDDER_MODEL = os.environ.get("EMBEDDER_MODEL", "text-embedding-3-small")
OLLAMA_HOST = os.environ.get("OLLAMA_HOST") # only used if provider=ollama
DEFAULT_CONFIG = {
"version": "v1.1",
"vector_store": {
"provider": "pgvector",
"config": {
"host": POSTGRES_HOST,
"port": int(POSTGRES_PORT),
"dbname": POSTGRES_DB,
"user": POSTGRES_USER,
"password": POSTGRES_PASSWORD,
"collection_name": POSTGRES_COLLECTION_NAME,
},
},
"graph_store": {
"provider": "neo4j",
"config": {"url": NEO4J_URI, "username": NEO4J_USERNAME, "password": NEO4J_PASSWORD},
},
"llm": {
"provider": os.getenv("LLM_PROVIDER", "ollama"),
"config": {
"model": os.getenv("LLM_MODEL", "qwen2.5:7b-instruct-q4_K_M"),
"ollama_base_url": os.getenv("LLM_API_BASE") or os.getenv("OLLAMA_BASE_URL"),
"temperature": float(os.getenv("LLM_TEMPERATURE", "0.2")),
},
},
"embedder": {
"provider": EMBEDDER_PROVIDER,
"config": {
"model": EMBEDDER_MODEL,
"embedding_dims": int(os.environ.get("EMBEDDING_DIMS", "1536")),
"openai_base_url": os.getenv("OPENAI_BASE_URL"),
"api_key": OPENAI_API_KEY
},
},
"history_db_path": HISTORY_DB_PATH,
}
import time
print(">>> Embedder config:", DEFAULT_CONFIG["embedder"])
# Wait for Neo4j connection before creating Memory instance
for attempt in range(10): # try for about 50 seconds total
try:
MEMORY_INSTANCE = Memory.from_config(DEFAULT_CONFIG)
print(f"✅ Connected to Neo4j on attempt {attempt + 1}")
break
except Exception as e:
print(f"⏳ Waiting for Neo4j (attempt {attempt + 1}/10): {e}")
time.sleep(5)
else:
raise RuntimeError("❌ Could not connect to Neo4j after 10 attempts")
class Message(BaseModel):
role: str = Field(..., description="Role of the message (user or assistant).")
content: str = Field(..., description="Message content.")
class MemoryCreate(BaseModel):
messages: List[Message] = Field(..., description="List of messages to store.")
user_id: Optional[str] = None
agent_id: Optional[str] = None
run_id: Optional[str] = None
metadata: Optional[Dict[str, Any]] = None
class SearchRequest(BaseModel):
query: str = Field(..., description="Search query.")
user_id: Optional[str] = None
run_id: Optional[str] = None
agent_id: Optional[str] = None
filters: Optional[Dict[str, Any]] = None
@app.post("/configure", summary="Configure Mem0")
def set_config(config: Dict[str, Any]):
"""Set memory configuration."""
global MEMORY_INSTANCE
MEMORY_INSTANCE = Memory.from_config(config)
return {"message": "Configuration set successfully"}
@app.post("/memories", summary="Create memories")
def add_memory(memory_create: MemoryCreate):
"""Store new memories."""
if not any([memory_create.user_id, memory_create.agent_id, memory_create.run_id]):
raise HTTPException(status_code=400, detail="At least one identifier (user_id, agent_id, run_id) is required.")
params = {k: v for k, v in memory_create.model_dump().items() if v is not None and k != "messages"}
try:
response = MEMORY_INSTANCE.add(messages=[m.model_dump() for m in memory_create.messages], **params)
return JSONResponse(content=response)
except Exception as e:
logging.exception("Error in add_memory:") # This will log the full traceback
raise HTTPException(status_code=500, detail=str(e))
@app.get("/memories", summary="Get memories")
def get_all_memories(
user_id: Optional[str] = None,
run_id: Optional[str] = None,
agent_id: Optional[str] = None,
):
"""Retrieve stored memories."""
if not any([user_id, run_id, agent_id]):
raise HTTPException(status_code=400, detail="At least one identifier is required.")
try:
params = {
k: v for k, v in {"user_id": user_id, "run_id": run_id, "agent_id": agent_id}.items() if v is not None
}
return MEMORY_INSTANCE.get_all(**params)
except Exception as e:
logging.exception("Error in get_all_memories:")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/memories/{memory_id}", summary="Get a memory")
def get_memory(memory_id: str):
"""Retrieve a specific memory by ID."""
try:
return MEMORY_INSTANCE.get(memory_id)
except Exception as e:
logging.exception("Error in get_memory:")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/search", summary="Search memories")
def search_memories(search_req: SearchRequest):
"""Search for memories based on a query."""
try:
params = {k: v for k, v in search_req.model_dump().items() if v is not None and k != "query"}
return MEMORY_INSTANCE.search(query=search_req.query, **params)
except Exception as e:
logging.exception("Error in search_memories:")
raise HTTPException(status_code=500, detail=str(e))
@app.put("/memories/{memory_id}", summary="Update a memory")
def update_memory(memory_id: str, updated_memory: Dict[str, Any]):
"""Update an existing memory with new content.
Args:
memory_id (str): ID of the memory to update
updated_memory (str): New content to update the memory with
Returns:
dict: Success message indicating the memory was updated
"""
try:
return MEMORY_INSTANCE.update(memory_id=memory_id, data=updated_memory)
except Exception as e:
logging.exception("Error in update_memory:")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/memories/{memory_id}/history", summary="Get memory history")
def memory_history(memory_id: str):
"""Retrieve memory history."""
try:
return MEMORY_INSTANCE.history(memory_id=memory_id)
except Exception as e:
logging.exception("Error in memory_history:")
raise HTTPException(status_code=500, detail=str(e))
@app.delete("/memories/{memory_id}", summary="Delete a memory")
def delete_memory(memory_id: str):
"""Delete a specific memory by ID."""
try:
MEMORY_INSTANCE.delete(memory_id=memory_id)
return {"message": "Memory deleted successfully"}
except Exception as e:
logging.exception("Error in delete_memory:")
raise HTTPException(status_code=500, detail=str(e))
@app.delete("/memories", summary="Delete all memories")
def delete_all_memories(
user_id: Optional[str] = None,
run_id: Optional[str] = None,
agent_id: Optional[str] = None,
):
"""Delete all memories for a given identifier."""
if not any([user_id, run_id, agent_id]):
raise HTTPException(status_code=400, detail="At least one identifier is required.")
try:
params = {
k: v for k, v in {"user_id": user_id, "run_id": run_id, "agent_id": agent_id}.items() if v is not None
}
MEMORY_INSTANCE.delete_all(**params)
return {"message": "All relevant memories deleted"}
except Exception as e:
logging.exception("Error in delete_all_memories:")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/reset", summary="Reset all memories")
def reset_memory():
"""Completely reset stored memories."""
try:
MEMORY_INSTANCE.reset()
return {"message": "All memories reset"}
except Exception as e:
logging.exception("Error in reset_memory:")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/", summary="Redirect to the OpenAPI documentation", include_in_schema=False)
def home():
"""Redirect to the OpenAPI documentation."""
return RedirectResponse(url="/docs")
-273
View File
@@ -1,273 +0,0 @@
import logging
import os
from typing import Any, Dict, List, Optional
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse, RedirectResponse
from pydantic import BaseModel, Field
from neomem import Memory
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
# Load environment variables
load_dotenv()
POSTGRES_HOST = os.environ.get("POSTGRES_HOST", "postgres")
POSTGRES_PORT = os.environ.get("POSTGRES_PORT", "5432")
POSTGRES_DB = os.environ.get("POSTGRES_DB", "postgres")
POSTGRES_USER = os.environ.get("POSTGRES_USER", "postgres")
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "postgres")
POSTGRES_COLLECTION_NAME = os.environ.get("POSTGRES_COLLECTION_NAME", "memories")
NEO4J_URI = os.environ.get("NEO4J_URI", "bolt://neo4j:7687")
NEO4J_USERNAME = os.environ.get("NEO4J_USERNAME", "neo4j")
NEO4J_PASSWORD = os.environ.get("NEO4J_PASSWORD", "neomemgraph")
MEMGRAPH_URI = os.environ.get("MEMGRAPH_URI", "bolt://localhost:7687")
MEMGRAPH_USERNAME = os.environ.get("MEMGRAPH_USERNAME", "memgraph")
MEMGRAPH_PASSWORD = os.environ.get("MEMGRAPH_PASSWORD", "neomemgraph")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
HISTORY_DB_PATH = os.environ.get("HISTORY_DB_PATH", "/app/history/history.db")
# Embedder settings (switchable by .env)
EMBEDDER_PROVIDER = os.environ.get("EMBEDDER_PROVIDER", "openai")
EMBEDDER_MODEL = os.environ.get("EMBEDDER_MODEL", "text-embedding-3-small")
OLLAMA_HOST = os.environ.get("OLLAMA_HOST") # only used if provider=ollama
DEFAULT_CONFIG = {
"version": "v1.1",
"vector_store": {
"provider": "pgvector",
"config": {
"host": POSTGRES_HOST,
"port": int(POSTGRES_PORT),
"dbname": POSTGRES_DB,
"user": POSTGRES_USER,
"password": POSTGRES_PASSWORD,
"collection_name": POSTGRES_COLLECTION_NAME,
},
},
"graph_store": {
"provider": "neo4j",
"config": {"url": NEO4J_URI, "username": NEO4J_USERNAME, "password": NEO4J_PASSWORD},
},
"llm": {
"provider": os.getenv("LLM_PROVIDER", "ollama"),
"config": {
"model": os.getenv("LLM_MODEL", "qwen2.5:7b-instruct-q4_K_M"),
"ollama_base_url": os.getenv("LLM_API_BASE") or os.getenv("OLLAMA_BASE_URL"),
"temperature": float(os.getenv("LLM_TEMPERATURE", "0.2")),
},
},
"embedder": {
"provider": EMBEDDER_PROVIDER,
"config": {
"model": EMBEDDER_MODEL,
"embedding_dims": int(os.environ.get("EMBEDDING_DIMS", "1536")),
"openai_base_url": os.getenv("OPENAI_BASE_URL"),
"api_key": OPENAI_API_KEY
},
},
"history_db_path": HISTORY_DB_PATH,
}
import time
from fastapi import FastAPI
# single app instance
app = FastAPI(
title="NEOMEM REST APIs",
description="A REST API for managing and searching memories for your AI Agents and Apps.",
version="0.2.0",
)
start_time = time.time()
@app.get("/health")
def health_check():
uptime = round(time.time() - start_time, 1)
return {
"status": "ok",
"service": "NEOMEM",
"version": DEFAULT_CONFIG.get("version", "unknown"),
"uptime_seconds": uptime,
"message": "API reachable"
}
print(">>> Embedder config:", DEFAULT_CONFIG["embedder"])
# Wait for Neo4j connection before creating Memory instance
for attempt in range(10): # try for about 50 seconds total
try:
MEMORY_INSTANCE = Memory.from_config(DEFAULT_CONFIG)
print(f"✅ Connected to Neo4j on attempt {attempt + 1}")
break
except Exception as e:
print(f"⏳ Waiting for Neo4j (attempt {attempt + 1}/10): {e}")
time.sleep(5)
else:
raise RuntimeError("❌ Could not connect to Neo4j after 10 attempts")
class Message(BaseModel):
role: str = Field(..., description="Role of the message (user or assistant).")
content: str = Field(..., description="Message content.")
class MemoryCreate(BaseModel):
messages: List[Message] = Field(..., description="List of messages to store.")
user_id: Optional[str] = None
agent_id: Optional[str] = None
run_id: Optional[str] = None
metadata: Optional[Dict[str, Any]] = None
class SearchRequest(BaseModel):
query: str = Field(..., description="Search query.")
user_id: Optional[str] = None
run_id: Optional[str] = None
agent_id: Optional[str] = None
filters: Optional[Dict[str, Any]] = None
@app.post("/configure", summary="Configure NeoMem")
def set_config(config: Dict[str, Any]):
"""Set memory configuration."""
global MEMORY_INSTANCE
MEMORY_INSTANCE = Memory.from_config(config)
return {"message": "Configuration set successfully"}
@app.post("/memories", summary="Create memories")
def add_memory(memory_create: MemoryCreate):
"""Store new memories."""
if not any([memory_create.user_id, memory_create.agent_id, memory_create.run_id]):
raise HTTPException(status_code=400, detail="At least one identifier (user_id, agent_id, run_id) is required.")
params = {k: v for k, v in memory_create.model_dump().items() if v is not None and k != "messages"}
try:
response = MEMORY_INSTANCE.add(messages=[m.model_dump() for m in memory_create.messages], **params)
return JSONResponse(content=response)
except Exception as e:
logging.exception("Error in add_memory:") # This will log the full traceback
raise HTTPException(status_code=500, detail=str(e))
@app.get("/memories", summary="Get memories")
def get_all_memories(
user_id: Optional[str] = None,
run_id: Optional[str] = None,
agent_id: Optional[str] = None,
):
"""Retrieve stored memories."""
if not any([user_id, run_id, agent_id]):
raise HTTPException(status_code=400, detail="At least one identifier is required.")
try:
params = {
k: v for k, v in {"user_id": user_id, "run_id": run_id, "agent_id": agent_id}.items() if v is not None
}
return MEMORY_INSTANCE.get_all(**params)
except Exception as e:
logging.exception("Error in get_all_memories:")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/memories/{memory_id}", summary="Get a memory")
def get_memory(memory_id: str):
"""Retrieve a specific memory by ID."""
try:
return MEMORY_INSTANCE.get(memory_id)
except Exception as e:
logging.exception("Error in get_memory:")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/search", summary="Search memories")
def search_memories(search_req: SearchRequest):
"""Search for memories based on a query."""
try:
params = {k: v for k, v in search_req.model_dump().items() if v is not None and k != "query"}
return MEMORY_INSTANCE.search(query=search_req.query, **params)
except Exception as e:
logging.exception("Error in search_memories:")
raise HTTPException(status_code=500, detail=str(e))
@app.put("/memories/{memory_id}", summary="Update a memory")
def update_memory(memory_id: str, updated_memory: Dict[str, Any]):
"""Update an existing memory with new content.
Args:
memory_id (str): ID of the memory to update
updated_memory (str): New content to update the memory with
Returns:
dict: Success message indicating the memory was updated
"""
try:
return MEMORY_INSTANCE.update(memory_id=memory_id, data=updated_memory)
except Exception as e:
logging.exception("Error in update_memory:")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/memories/{memory_id}/history", summary="Get memory history")
def memory_history(memory_id: str):
"""Retrieve memory history."""
try:
return MEMORY_INSTANCE.history(memory_id=memory_id)
except Exception as e:
logging.exception("Error in memory_history:")
raise HTTPException(status_code=500, detail=str(e))
@app.delete("/memories/{memory_id}", summary="Delete a memory")
def delete_memory(memory_id: str):
"""Delete a specific memory by ID."""
try:
MEMORY_INSTANCE.delete(memory_id=memory_id)
return {"message": "Memory deleted successfully"}
except Exception as e:
logging.exception("Error in delete_memory:")
raise HTTPException(status_code=500, detail=str(e))
@app.delete("/memories", summary="Delete all memories")
def delete_all_memories(
user_id: Optional[str] = None,
run_id: Optional[str] = None,
agent_id: Optional[str] = None,
):
"""Delete all memories for a given identifier."""
if not any([user_id, run_id, agent_id]):
raise HTTPException(status_code=400, detail="At least one identifier is required.")
try:
params = {
k: v for k, v in {"user_id": user_id, "run_id": run_id, "agent_id": agent_id}.items() if v is not None
}
MEMORY_INSTANCE.delete_all(**params)
return {"message": "All relevant memories deleted"}
except Exception as e:
logging.exception("Error in delete_all_memories:")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/reset", summary="Reset all memories")
def reset_memory():
"""Completely reset stored memories."""
try:
MEMORY_INSTANCE.reset()
return {"message": "All memories reset"}
except Exception as e:
logging.exception("Error in reset_memory:")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/", summary="Redirect to the OpenAPI documentation", include_in_schema=False)
def home():
"""Redirect to the OpenAPI documentation."""
return RedirectResponse(url="/docs")
-66
View File
@@ -1,66 +0,0 @@
services:
neomem-postgres:
image: ankane/pgvector:v0.5.1
container_name: neomem-postgres
restart: unless-stopped
environment:
POSTGRES_USER: neomem
POSTGRES_PASSWORD: neomempass
POSTGRES_DB: neomem
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U neomem -d neomem || exit 1"]
interval: 5s
timeout: 5s
retries: 10
networks:
- lyra-net
neomem-neo4j:
image: neo4j:5
container_name: neomem-neo4j
restart: unless-stopped
environment:
NEO4J_AUTH: neo4j/neomemgraph
ports:
- "7474:7474"
- "7687:7687"
volumes:
- neo4j_data:/data
healthcheck:
test: ["CMD-SHELL", "cypher-shell -u neo4j -p neomemgraph 'RETURN 1' || exit 1"]
interval: 10s
timeout: 10s
retries: 10
networks:
- lyra-net
neomem-api:
build: .
image: lyra-neomem:latest
container_name: neomem-api
restart: unless-stopped
ports:
- "7077:7077"
env_file:
- .env
volumes:
- ./neomem_history:/app/history
depends_on:
neomem-postgres:
condition: service_healthy
neomem-neo4j:
condition: service_healthy
networks:
- lyra-net
volumes:
postgres_data:
neo4j_data:
networks:
lyra-net:
external: true
-201
View File
@@ -1,201 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [2023] [Taranjeet Singh]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Some files were not shown because too many files have changed in this diff Show More