Compare commits
28
Commits
@@ -0,0 +1,27 @@
|
||||
name: Build and Push
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Login to Gitea Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: git.schweitz.net
|
||||
username: ${{ secrets.REGISTRY_USER }}
|
||||
password: ${{ secrets.REGISTRY_PASSWORD }}
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
tags: |
|
||||
git.schweitz.net/jpmschweitzer/tatlock:latest
|
||||
git.schweitz.net/jpmschweitzer/tatlock:${{ github.ref_name }}
|
||||
@@ -3,542 +3,54 @@
|
||||
This document contains instructions and documentation references for AI assistants working with this codebase.
|
||||
|
||||
> **📖 Important**: Before working on this project, read [PHILOSOPHY.md](PHILOSOPHY.md) to understand the system vision, architectural patterns, and design goals. All development should work towards realizing those patterns.
|
||||
# AGENTS.md
|
||||
|
||||
## Project Overview
|
||||
> **Start every session by reading this file.**
|
||||
> This file outlines the operational protocols, coding standards, and architectural decisions for this FastAPI project.
|
||||
|
||||
This project implements an OpenAI-compatible API with FastAPI, featuring a hybrid architecture that provides both the OpenAI Responses API and Chat Completions compatibility layer.
|
||||
## 1. Agent Operational Protocols
|
||||
|
||||
### Architecture Pattern
|
||||
### 🧠 Work Patterns (Plan-Act-Reflect)
|
||||
* **Plan:** Before writing code, briefly outline your plan. Identify which files you will touch and what the side effects might be.
|
||||
* **Act:** Execute the changes in small, atomic steps.
|
||||
* **Reflect:** After coding, verify your work. Did you break existing tests? Did you add new tests?
|
||||
|
||||
The **Orchestrator** infrastructure layer with hybrid API architecture:
|
||||
### 🌐 Internal Service Access
|
||||
* **git.schweitz.net**: Access via `http://localhost:3002` (direct Gitea) to bypass Authentik SSO
|
||||
* Example: `curl http://localhost:3002/jpmschweitzer/library-desk/raw/branch/main/README.md`
|
||||
* Public repos are readable without authentication
|
||||
* Related repos: `library-desk`, `scheduler`
|
||||
|
||||
```
|
||||
Client (Open WebUI)
|
||||
↓
|
||||
Chat Completions (/v1/chat/completions) → Wrapper
|
||||
↓
|
||||
Responses API (/v1/responses) → Primary
|
||||
↓
|
||||
Agent Interface (lorem-tester, Tatlock)
|
||||
↓
|
||||
Mock Agents (lorem-tester) / Future: PydanticAI Agents (Tatlock, Steward, etc.)
|
||||
```
|
||||
### 🛡️ Git Discipline
|
||||
* **NEVER commit to `main` or `master` directly.** Always create a feature branch: `feature/your-feature-name` or `fix/issue-description`.
|
||||
* **Commit Messages:** Use the [Conventional Commits](https://www.conventionalcommits.org/) format.
|
||||
* `feat: add user login endpoint`
|
||||
* `fix: resolve database connection timeout`
|
||||
* `refactor: split monolith dependency file`
|
||||
* **Atomic Commits:** Keep commits small. One logical change = one commit.
|
||||
|
||||
**Architectural Layers:**
|
||||
### 📝 Changelog Maintenance
|
||||
* **Update `CHANGELOG.md`** with every user-facing change.
|
||||
* Format: `## [Unreleased] - YYYY-MM-DD` followed by `### Added`, `### Changed`, or `### Fixed`.
|
||||
|
||||
1. **The Orchestrator** (Current Implementation)
|
||||
- FastAPI application providing the infrastructure
|
||||
- HTTP/SSE endpoints, streaming coordination
|
||||
- Conversation history and context management
|
||||
- OpenAI-compatible API surface
|
||||
---
|
||||
|
||||
2. **Future: The Household** (Phases 1-4)
|
||||
- **Steward**: First-tier LLM for request analysis (PydanticAI agent)
|
||||
- **Tatlock**: Second-tier LLM with butler personality (PydanticAI agent)
|
||||
- **Expert Agents**: Domain specialists (Librarian, Developer, Handyman, etc.)
|
||||
## 2. FastAPI Architecture & Best Practices
|
||||
*Reference: [FastAPI Best Practices](https://github.com/zhanymkanov/fastapi-best-practices)*
|
||||
|
||||
**Key Architectural Decisions:**
|
||||
### 📂 Project Structure (Directory-based, NOT File-type based)
|
||||
Do **not** group files by type (e.g., one huge `routers` folder). Group by **domain/module** inside a `src/` directory.
|
||||
|
||||
1. **Single Source of Truth**: All response generation happens in the Responses API
|
||||
- Structured output with reasoning, function_call, and message items
|
||||
- Real-time stop sequence and max tokens enforcement
|
||||
- Conversation history tracking
|
||||
- Context window management
|
||||
|
||||
2. **Chat Completions Wrapper**: Provides compatibility without duplicating logic
|
||||
- Calls Responses API internally
|
||||
- Automatically enables reasoning generation
|
||||
- Converts reasoning items to `<think>` tags for Open WebUI
|
||||
- Maintains OpenAI-compatible format
|
||||
|
||||
3. **Agent Interface**: Clean abstraction for multiple models
|
||||
- **lorem-tester**: Full-featured mock agent with realistic behavior
|
||||
- Reasoning summaries (adjustable effort levels)
|
||||
- Random tool/function calls
|
||||
- Error triggers for testing
|
||||
- Temperature variation
|
||||
- **Tatlock**: Advertised model name (currently mock, future: PydanticAI Butler agent)
|
||||
|
||||
4. **Hybrid Conversation History**:
|
||||
- Client MUST send full context in `input` array (OpenAI compatible)
|
||||
- Server optionally tracks via `metadata.conversation_id`
|
||||
- Auto-generates deterministic IDs from first message
|
||||
- Supports future vector memory integration (Qdrant)
|
||||
|
||||
**Why This Architecture?**
|
||||
|
||||
- **Open WebUI Compatibility**: Native Responses API support not yet in stable release
|
||||
- **Future-Proof**: Easy migration when Open WebUI adds native support
|
||||
- **Testability**: Full-featured mock agent (lorem-tester) for integration testing
|
||||
- **Clean Separation**: Responses API as stable core, wrappers can change
|
||||
|
||||
### Components
|
||||
|
||||
- **FastAPI**: Web framework for the API layer
|
||||
- **SSE-Starlette**: Server-Sent Events for streaming responses
|
||||
- **Pydantic**: Request/response validation with field validators
|
||||
- **Agent Interface**: Abstract base class for model implementations
|
||||
- **Conversation History**: Server-side tracking with configurable max turns
|
||||
- **Context Window**: Token counting and management
|
||||
- **PydanticAI**: Integrated with Tatlock agent (Ollama backend)
|
||||
- **Agent Tools**: Permanent tools module (`src/agents/tools.py`)
|
||||
- Calculator: Safe mathematical expression evaluation
|
||||
- Date/Time toolkit: Current time, relative dates, time differences
|
||||
- Web Search: SearXNG integration for privacy-preserving search
|
||||
|
||||
## Documentation References
|
||||
|
||||
### Core Framework Documentation
|
||||
|
||||
#### FastAPI
|
||||
- **Official Documentation**: https://fastapi.tiangolo.com/
|
||||
- **Version**: 0.123.9 (Dec 2025)
|
||||
- **Key Topics**:
|
||||
- Path operations and routing
|
||||
- Request/response models with Pydantic
|
||||
- Dependency injection
|
||||
- Background tasks
|
||||
- WebSocket and streaming support
|
||||
- **PyPI**: https://pypi.org/project/fastapi/
|
||||
|
||||
#### Uvicorn
|
||||
- **Official Documentation**: https://www.uvicorn.org/
|
||||
- **Version**: 0.38.0 (Oct 2025)
|
||||
- **Key Topics**:
|
||||
- ASGI server configuration
|
||||
- Deployment settings
|
||||
- Logging and monitoring
|
||||
- SSL/TLS configuration
|
||||
|
||||
### AI/LLM Integration
|
||||
|
||||
#### PydanticAI
|
||||
- **Official Documentation**: https://ai.pydantic.dev/
|
||||
- **Version**: 1.27.0 (Dec 2025)
|
||||
- **Status**: Dependency installed, ready for future integration
|
||||
- **Key Topics** (for future implementation):
|
||||
- Agent creation and configuration
|
||||
- LLM provider integration (Ollama support)
|
||||
- Structured outputs with Pydantic
|
||||
- Streaming responses
|
||||
- Tool/function calling
|
||||
- RunContext and dynamic configuration
|
||||
- MCP server integration
|
||||
- **GitHub**: https://github.com/pydantic/pydantic-ai
|
||||
- **PyPI**: https://pypi.org/project/pydantic-ai/
|
||||
|
||||
#### Pydantic
|
||||
- **Official Documentation**: https://docs.pydantic.dev/latest/
|
||||
- **Version**: 2.11+ (Required for PydanticAI, currently using >=2.11,<2.13)
|
||||
- **Key Topics**:
|
||||
- Data validation and serialization
|
||||
- Field types and validators
|
||||
- Model configuration
|
||||
- JSON schema generation
|
||||
|
||||
### HTTP and Streaming
|
||||
|
||||
#### HTTPX
|
||||
- **Official Documentation**: https://www.python-httpx.org/
|
||||
- **Version**: 0.28.1
|
||||
- **Key Topics**:
|
||||
- Async HTTP client for Ollama communication
|
||||
- Streaming responses
|
||||
- Timeout configuration
|
||||
- Connection pooling
|
||||
|
||||
#### SSE-Starlette
|
||||
- **GitHub**: https://github.com/sysid/sse-starlette
|
||||
- **Version**: 3.0.2 (Oct 2025)
|
||||
- **Key Topics**:
|
||||
- Server-Sent Events implementation
|
||||
- Streaming event responses
|
||||
- Integration with FastAPI/Starlette
|
||||
|
||||
### Ollama Integration
|
||||
|
||||
#### Ollama API
|
||||
- **Official Documentation**: https://github.com/ollama/ollama/blob/main/docs/api.md
|
||||
- **Status**: Async client implemented in `src/ollama/client.py`, ready for future integration
|
||||
- **Key Topics** (for future implementation):
|
||||
- REST API endpoints
|
||||
- Streaming responses
|
||||
- Model management
|
||||
- Generate and chat endpoints
|
||||
- Model configuration
|
||||
- **Current Model Target**: mistral-nemo:latest
|
||||
|
||||
### OpenAI API Compatibility
|
||||
|
||||
#### OpenAI API Reference
|
||||
- **Official Documentation**: https://platform.openai.com/docs/api-reference
|
||||
- **Key API Endpoints**:
|
||||
- `/v1/responses` - Responses API (PRIMARY) with structured output
|
||||
- `/v1/chat/completions` - OpenAI Chat Completions compatibility wrapper
|
||||
- `/v1/models` - List available models
|
||||
|
||||
- **Key Features for Development**:
|
||||
- **Responses API Format**: Structured output with reasoning, function_call, and message items
|
||||
- **Parameter Validation**: Temperature, reasoning effort levels, max tokens, stop sequences
|
||||
- **Conversation History**: Hybrid client/server approach with auto-generated IDs
|
||||
- **Context Management**: Token counting and window trimming
|
||||
- **Streaming**: Real-time SSE streaming with stop sequence and max token enforcement
|
||||
- **Error Handling**: Custom exception types (RateLimitError, ContextLengthError)
|
||||
- **Tool Calling**: PydanticAI tool integration with permanent tools
|
||||
- **Testing**: Comprehensive test suite with mocks and real Ollama integration
|
||||
|
||||
## FastAPI Best Practices
|
||||
|
||||
This project follows best practices from [github.com/zhanymkanov/fastapi-best-practices](https://github.com/zhanymkanov/fastapi-best-practices)
|
||||
|
||||
### Project Structure
|
||||
|
||||
**Domain-Based Organization**: Code is organized by domain/feature rather than by file type:
|
||||
|
||||
```
|
||||
**Correct Structure:**
|
||||
```text
|
||||
src/
|
||||
├── agents/ # Agent interface and implementations
|
||||
│ ├── base.py # Abstract AgentInterface
|
||||
│ ├── lorem_tester.py # Full-featured mock agent
|
||||
│ ├── tatlock.py # Placeholder for real agent
|
||||
│ └── registry.py # ModelRegistry for agent management
|
||||
├── responses/ # Responses API domain (PRIMARY)
|
||||
│ ├── router.py # POST /v1/responses endpoint
|
||||
│ ├── schemas.py # Request/response models with validators
|
||||
│ ├── service.py # Response generation logic
|
||||
│ ├── streaming.py # SSE streaming coordinator
|
||||
│ ├── history.py # Conversation history management
|
||||
│ └── context.py # Context window and token management
|
||||
├── chat/ # Chat Completions domain (WRAPPER)
|
||||
│ ├── router.py # POST /v1/chat/completions endpoint
|
||||
│ ├── schemas.py # Chat request/response models
|
||||
│ ├── service.py # Wraps Responses API, converts to <think> tags
|
||||
│ ├── constants.py # Chat constants (roles, finish reasons)
|
||||
│ └── __init__.py
|
||||
├── models/ # Models listing domain
|
||||
│ ├── router.py # GET /v1/models endpoint
|
||||
│ ├── schemas.py # Model schemas
|
||||
│ ├── service.py # Accesses ModelRegistry
|
||||
│ └── __init__.py
|
||||
├── core/ # Shared utilities
|
||||
│ ├── config.py # Global configuration (BaseSettings)
|
||||
│ ├── models.py # Custom base Pydantic models
|
||||
│ ├── exceptions.py # Custom exceptions (RateLimitError, etc.)
|
||||
│ ├── dependencies.py # Shared dependencies
|
||||
│ └── router.py # Core routes (health, root)
|
||||
├── ollama/ # Ollama client layer (not yet integrated)
|
||||
│ ├── client.py # Async Ollama HTTP client
|
||||
│ └── schemas.py # Ollama API models
|
||||
└── main.py # Application factory & configuration
|
||||
```
|
||||
|
||||
**Key Architectural Principles**:
|
||||
- **Single Source of Truth**: Responses API handles all generation logic
|
||||
- **Wrapper Pattern**: Chat Completions wraps Responses API without duplicating code
|
||||
- **Agent Abstraction**: AgentInterface defines contract for all models
|
||||
- **Domain Separation**: Each domain has its own router, schemas, service
|
||||
- **Service Layer**: Business logic in services, not routers
|
||||
- **Type Safety**: Pydantic models for ALL request/response validation
|
||||
- **Async First**: All I/O operations use async/await
|
||||
|
||||
### Async/Await Best Practices
|
||||
|
||||
**Critical Understanding**: FastAPI handles sync and async routes differently:
|
||||
|
||||
- **Async routes** (`async def`): Called directly in event loop
|
||||
- Use ONLY for non-blocking operations
|
||||
- Perfect for `await httpx.get()`, database queries, file I/O
|
||||
- **NEVER** use blocking calls like `time.sleep()` - this blocks entire server
|
||||
|
||||
- **Sync routes** (`def`): Run in thread pool
|
||||
- Use for CPU-intensive work or blocking SDKs
|
||||
- Blocking I/O won't freeze the event loop
|
||||
- Example: `time.sleep(10)` is safe here
|
||||
|
||||
**Example**:
|
||||
```python
|
||||
@router.get("/terrible")
|
||||
async def terrible():
|
||||
time.sleep(10) # ❌ BLOCKS ENTIRE SERVER
|
||||
|
||||
@router.get("/good")
|
||||
def good():
|
||||
time.sleep(10) # ✅ Runs in thread pool
|
||||
|
||||
@router.get("/perfect")
|
||||
async def perfect():
|
||||
await asyncio.sleep(10) # ✅ Non-blocking async
|
||||
```
|
||||
|
||||
**For CPU-intensive tasks**: Use separate worker processes (not threads) due to Python's GIL.
|
||||
|
||||
### Pydantic Configuration
|
||||
|
||||
**Custom Base Model**: All schemas inherit from `CustomBaseModel` for consistent behavior:
|
||||
|
||||
```python
|
||||
# src/core/models.py
|
||||
class CustomBaseModel(BaseModel):
|
||||
model_config = ConfigDict(
|
||||
json_encoders={datetime: datetime_to_iso_str},
|
||||
populate_by_name=True,
|
||||
use_enum_values=True,
|
||||
validate_assignment=True,
|
||||
)
|
||||
|
||||
def serializable_dict(self, **kwargs):
|
||||
"""Return dict with only JSON-serializable fields."""
|
||||
return jsonable_encoder(self.model_dump(**kwargs))
|
||||
```
|
||||
|
||||
**Benefits**:
|
||||
- Consistent datetime serialization across all responses
|
||||
- Alias support for field name flexibility
|
||||
- Easy JSON encoding for logging/debugging
|
||||
|
||||
**Decoupled Settings**: Split configuration by domain instead of one monolithic file:
|
||||
|
||||
```python
|
||||
# src/core/config.py - Global settings
|
||||
class Config(BaseSettings):
|
||||
DATABASE_URL: PostgresDsn
|
||||
ENVIRONMENT: Environment
|
||||
|
||||
# src/chat/config.py - Chat-specific settings
|
||||
class ChatConfig(BaseSettings):
|
||||
MAX_TOKENS: int
|
||||
DEFAULT_TEMPERATURE: float
|
||||
```
|
||||
|
||||
### Dependency Injection Patterns
|
||||
|
||||
**Validation with Dependencies**: Use dependencies for complex validations:
|
||||
|
||||
```python
|
||||
async def valid_post_id(post_id: UUID4) -> dict:
|
||||
"""Validate post exists in database."""
|
||||
post = await service.get_by_id(post_id)
|
||||
if not post:
|
||||
raise PostNotFound()
|
||||
return post
|
||||
|
||||
@router.get("/posts/{post_id}")
|
||||
async def get_post(post: dict = Depends(valid_post_id)):
|
||||
return post # Already validated!
|
||||
```
|
||||
|
||||
**Chaining Dependencies**: Build reusable validation layers:
|
||||
|
||||
```python
|
||||
async def valid_owned_post(
|
||||
post: dict = Depends(valid_post_id),
|
||||
token_data: dict = Depends(parse_jwt_data),
|
||||
) -> dict:
|
||||
if post["creator_id"] != token_data["user_id"]:
|
||||
raise UserNotOwner()
|
||||
return post
|
||||
```
|
||||
|
||||
**Dependency Caching**: Dependencies are cached within request scope - FastAPI only executes each dependency once per request, even if used multiple times.
|
||||
|
||||
### Application Factory Pattern
|
||||
|
||||
Main.py uses factory pattern for testability and configuration:
|
||||
|
||||
```python
|
||||
def create_application() -> FastAPI:
|
||||
"""Create and configure FastAPI app."""
|
||||
app = FastAPI(title=config.APP_NAME)
|
||||
|
||||
# Add middleware
|
||||
app.add_middleware(CORSMiddleware, ...)
|
||||
|
||||
# Register exception handlers
|
||||
register_exception_handlers(app)
|
||||
|
||||
# Include routers
|
||||
app.include_router(chat_router, prefix="/v1")
|
||||
|
||||
return app
|
||||
|
||||
app = create_application()
|
||||
```
|
||||
|
||||
## Development Guidelines
|
||||
|
||||
### Git Workflow
|
||||
|
||||
**IMPORTANT**: Do NOT handle git commits or pushes automatically. Wait for explicit user instruction before:
|
||||
- Running `git add`
|
||||
- Running `git commit`
|
||||
- Running `git push`
|
||||
- Creating or pushing tags
|
||||
|
||||
The user will manage git operations themselves unless they specifically request assistance.
|
||||
|
||||
### Server Logs and Debugging
|
||||
|
||||
**Development Mode Logging**: When the server is started using `./wakeup.sh`, logs are written to `logs/server.log`. This file is:
|
||||
- Cleared on each server startup (fresh logs every time)
|
||||
- Written in real-time as the server runs
|
||||
- Already gitignored (won't be committed)
|
||||
|
||||
**Accessing Logs**: You can read the log file at any time while the server is running:
|
||||
```bash
|
||||
# View current logs
|
||||
cat logs/server.log
|
||||
|
||||
# Follow logs in real-time
|
||||
tail -f logs/server.log
|
||||
|
||||
# Search logs
|
||||
grep "ERROR" logs/server.log
|
||||
```
|
||||
|
||||
This is useful for debugging issues, monitoring API calls, and understanding server behavior during development.
|
||||
|
||||
### Code Structure Guidelines
|
||||
- Use async/await for ALL I/O operations (database, HTTP, file access)
|
||||
- Use sync (def) for blocking SDKs or CPU-intensive work
|
||||
- Implement proper error handling and logging
|
||||
- Follow dependency injection for validation and shared resources
|
||||
- Use Pydantic models for ALL request/response validation
|
||||
- Keep business logic in service modules, not routers
|
||||
- Domain-based project structure (not file-type based)
|
||||
|
||||
### Security Considerations
|
||||
- Validate all inputs using Pydantic models
|
||||
- Use environment variables for sensitive configuration
|
||||
- Keep dependencies updated and CVE-checked
|
||||
- Minor version locking for supply chain protection
|
||||
- Consider rate limiting for production deployment
|
||||
- Plan for authentication/API keys when needed
|
||||
|
||||
### Testing Approach
|
||||
- Write integration tests for API endpoints
|
||||
- Test streaming functionality with appropriate timeouts
|
||||
- Use pytest-asyncio for async test support
|
||||
- Validate OpenAI API compatibility in tests
|
||||
- Test both mock and real LLM integrations
|
||||
- Cover main application (CORS, exception handlers, lifespan)
|
||||
- Test wrapper layers (chat completions, etc.)
|
||||
- Include tool functionality tests
|
||||
|
||||
### Configuration Management
|
||||
- Use `.env` files for local development
|
||||
- Document all environment variables in README
|
||||
- Provide sensible defaults where possible
|
||||
- Use BaseSettings from pydantic-settings
|
||||
- Support both local and container-based configuration
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Streaming Response Pattern
|
||||
|
||||
Example from `src/chat/router.py`:
|
||||
|
||||
```python
|
||||
from sse_starlette.sse import EventSourceResponse
|
||||
from fastapi import FastAPI
|
||||
|
||||
async def event_generator():
|
||||
# Currently yields mock lorem ipsum chunks
|
||||
# Future: Stream from Ollama/PydanticAI
|
||||
yield {"data": chunk.model_dump_json()}
|
||||
yield {"data": "[DONE]"}
|
||||
|
||||
@app.post("/stream")
|
||||
async def stream():
|
||||
return EventSourceResponse(event_generator())
|
||||
```
|
||||
|
||||
### PydanticAI Agent Pattern
|
||||
|
||||
When implementing agents with PydanticAI and Ollama:
|
||||
|
||||
```python
|
||||
from pydantic_ai import Agent
|
||||
|
||||
agent = Agent(
|
||||
'ollama:mistral-nemo', # Target model
|
||||
# Configuration here
|
||||
)
|
||||
|
||||
# Use the agent
|
||||
result = await agent.run('Your prompt')
|
||||
```
|
||||
|
||||
### OpenAI-Compatible Response Format
|
||||
|
||||
Example schema from `src/chat/schemas.py`:
|
||||
|
||||
```python
|
||||
{
|
||||
"id": "chatcmpl-123",
|
||||
"object": "chat.completion.chunk",
|
||||
"created": 1234567890,
|
||||
"model": "mistral-nemo:latest",
|
||||
"choices": [{
|
||||
"index": 0,
|
||||
"delta": {"content": "response"},
|
||||
"finish_reason": None
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
### PydanticAI Tool Registration Pattern
|
||||
|
||||
Tools are registered with PydanticAI agents using decorators. See `src/agents/tatlock.py` for examples:
|
||||
|
||||
```python
|
||||
from pydantic_ai import Agent, RunContext
|
||||
|
||||
# After creating the agent
|
||||
@agent.tool
|
||||
def tool_name(ctx: RunContext[None], param: str) -> str:
|
||||
"""
|
||||
Tool description that the LLM sees.
|
||||
|
||||
Args:
|
||||
param: Parameter description
|
||||
|
||||
Returns:
|
||||
Result description
|
||||
"""
|
||||
return result
|
||||
```
|
||||
|
||||
**Tool Implementation Guidelines**:
|
||||
- Keep tools in `src/agents/tools.py` for reusability
|
||||
- Use clear, descriptive docstrings (LLM reads these)
|
||||
- Include parameter descriptions in docstrings
|
||||
- Handle errors gracefully and return error messages as strings
|
||||
- For async operations, declare the tool function as `async def`
|
||||
- Test tools independently before integration
|
||||
|
||||
**Example Tool Module** (`src/agents/tools.py`):
|
||||
```python
|
||||
def calculate(expression: str) -> str:
|
||||
"""Safe calculator implementation."""
|
||||
try:
|
||||
# Implementation
|
||||
return str(result)
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
async def search_web(query: str) -> str:
|
||||
"""Web search via SearXNG."""
|
||||
async with httpx.AsyncClient() as client:
|
||||
# Implementation
|
||||
return formatted_results
|
||||
```
|
||||
|
||||
## Update Policy
|
||||
|
||||
This document should be updated when:
|
||||
- New development patterns are established
|
||||
- Package versions are upgraded
|
||||
- Major architectural changes occur
|
||||
- New best practices are identified
|
||||
|
||||
Last updated: 2025-12-06 (Tools integration)
|
||||
├── auth/
|
||||
│ ├── router.py # Endpoints
|
||||
│ ├── schemas.py # Pydantic models
|
||||
│ ├── service.py # Business logic (CRUD, etc.)
|
||||
│ ├── dependencies.py# Module-specific dependencies
|
||||
│ └── config.py # Module-specific settings
|
||||
├── posts/
|
||||
│ ├── router.py
|
||||
│ └── ...
|
||||
└── main.py # App entry point
|
||||
+175
-1
@@ -7,6 +7,176 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.2.0] - 2025-12-13
|
||||
|
||||
### Added
|
||||
|
||||
#### Phase F: Memory System (The Biographer)
|
||||
|
||||
- **Memory Infrastructure** (Phase F.1):
|
||||
- `src/core/context.py`: ContextVar-based request context for async-safe user/conversation tracking
|
||||
- `get_user()`, `get_conversation_id()` helpers
|
||||
- `RequestContext` manager for clean setup/teardown
|
||||
- `src/core/multi_tenancy.py`: User ID sanitization and collection naming
|
||||
- Per-user collection pattern: `memories_{user}`
|
||||
- Redis key patterns: `session:{user}:{conv}`, `entities:{user}:{conv}`
|
||||
- `src/core/embeddings.py`: Ollama embedding client
|
||||
- nomic-embed-text model (768 dimensions)
|
||||
- `embed()`, `embed_batch()`, `health_check()` methods
|
||||
- `src/core/qdrant.py`: Qdrant vector database client
|
||||
- `ensure_collection()`, `upsert_memory()`, `search_memories()`, `delete_memory()`
|
||||
- Type-based filtering for memory queries
|
||||
- `src/core/memory_cache.py`: Redis session memory cache
|
||||
- Session context with 24h TTL (db=2, separate from benchmarks)
|
||||
- Recent entities tracking per conversation
|
||||
|
||||
- **Memory Service** (Phase F.2a):
|
||||
- `src/core/memory_service.py`: Direct access layer for fast, LLM-free memory lookups
|
||||
- Profile methods: `get_profile()`, `set_profile()`
|
||||
- Preference methods: `get_preference()`, `set_preference()`, `get_all_preferences()`
|
||||
- Fact methods: `store_fact()`, `get_fact()`
|
||||
- Session context: `get_session_context()`, `set_session_context()`, `update_session_context()`
|
||||
- Steward integration: `prefetch_context()` for request preprocessing
|
||||
|
||||
- **The Biographer Agent** (Phase F.2b):
|
||||
- `src/agents/biographer/`: Household memory keeper agent
|
||||
- PydanticAI agent with discreet chronicler personality
|
||||
- System prompt emphasizes privacy and accurate recall
|
||||
- **Biographer Tools** (`src/agents/biographer/tools.py`):
|
||||
- `recall_semantic`: Semantic search for memories by meaning
|
||||
- `list_memories`: Browse stored memories by type
|
||||
- `store_insight`: Record new facts from conversation
|
||||
- `update_profile`: Update core profile fields (name, location, timezone)
|
||||
- `update_preference`: Update user preferences (units, theme)
|
||||
- `forget_memory`: Remove specific memories
|
||||
- **Capability Registration**:
|
||||
- `BIOGRAPHER_CAPABILITY` with context domain
|
||||
- Automatic registration on startup
|
||||
- Low cost (vector search, minimal LLM)
|
||||
|
||||
- **Delegation Wrapper**:
|
||||
- `delegate_to_biographer()` in `src/agents/delegation.py`
|
||||
- Async delegation with error handling
|
||||
|
||||
- **Steward Memory Integration**:
|
||||
- Memory context pre-fetch during request analysis
|
||||
- Profile and preferences included in Steward's note to Butler
|
||||
- Keyword-based context determination (weather → location, time → timezone)
|
||||
|
||||
- **Configuration**:
|
||||
- `QDRANT_HOST`, `QDRANT_PORT`, `QDRANT_EMBEDDING_DIM` (768)
|
||||
- `OLLAMA_EMBEDDING_MODEL` (nomic-embed-text)
|
||||
- `REDIS_MEMORY_DB` (2), `REDIS_MEMORY_TTL_HOURS` (24)
|
||||
|
||||
- **Test Suite**:
|
||||
- 34 new tests for memory system
|
||||
- Biographer capability tests (15 tests)
|
||||
- Memory service tests (19 tests)
|
||||
|
||||
- **OpenAI Standard `user` Field**:
|
||||
- Added `user` field to `ResponseRequest` schema
|
||||
- Request context set at API entry point
|
||||
- Propagates through async calls via ContextVar
|
||||
|
||||
### Changed
|
||||
- Application startup now registers The Biographer with Household Registry
|
||||
- Steward analysis includes memory context pre-fetch
|
||||
- Librarian client methods now use `get_user()` from context (12 methods updated)
|
||||
- Request router sets user/conversation context at entry
|
||||
|
||||
## [1.1.0] - 2025-12-11
|
||||
|
||||
### Added
|
||||
|
||||
#### Phase 3: Butler Orchestration (Multi-Agent Coordination)
|
||||
- **The Librarian Agent**: Expert agent for research and knowledge management
|
||||
- PydanticAI agent with specialized research assistant personality
|
||||
- Connects to library-desk API for HybridRAG capabilities
|
||||
- System prompt emphasizes fetching wiki pages before summarizing
|
||||
- Streaming support via `run_librarian_stream()`
|
||||
|
||||
- **Library-Desk API Client** (`src/agents/librarian/client.py`):
|
||||
- Async HTTP client with httpx for library-desk API integration
|
||||
- HybridRAG search (vector + graph + web search)
|
||||
- Wiki operations (search, get, list, create, update pages)
|
||||
- Smart page creation with HybridRAG research (`POST /wiki/pages/smart-create`)
|
||||
- Semantic vector search
|
||||
- Knowledge graph queries (Cypher execution)
|
||||
- Dossier (tag collection) browsing
|
||||
- Health check endpoint
|
||||
|
||||
- **Librarian Tools** (`src/agents/librarian/tools.py`):
|
||||
- Research tools:
|
||||
- `hybrid_search`: Combined vector, graph, and web search
|
||||
- `search_wiki`: Full-text wiki page search
|
||||
- `get_wiki_page`: Fetch full wiki page content by ID
|
||||
- `semantic_search`: Vector similarity search
|
||||
- `list_dossiers`: Browse knowledge collections
|
||||
- `get_dossier_pages`: Get pages in a dossier
|
||||
- `explore_knowledge_graph`: Entity and relationship discovery
|
||||
- `find_related_entities`: Find connected concepts
|
||||
- Write tools:
|
||||
- `smart_create_wiki_page`: Create page with automatic HybridRAG research (PREFERRED for topic-based creation)
|
||||
- `create_wiki_page`: Create page with user-provided content
|
||||
- `update_wiki_page`: Update existing page (partial updates supported)
|
||||
|
||||
- **Agent Communication Protocol** (`src/agents/protocol.py`):
|
||||
- `AgentRequest`: Standardized task request with context and constraints
|
||||
- `AgentResponse`: Response with result, reasoning, tool calls, confidence
|
||||
- `DelegationIntent`: Routing intent with target agent and reason
|
||||
- `CoordinationResult`: Aggregated multi-agent results
|
||||
- `DelegationReason` enum: domain expertise, tool access, resource efficiency, user preference
|
||||
- Error types: `AgentError`, `AgentTimeoutError`, `AgentUnavailableError`
|
||||
|
||||
- **Coordination Engine** (`src/agents/coordination.py`):
|
||||
- `CoordinationEngine`: Multi-agent task orchestration
|
||||
- Routing tasks to appropriate expert agents
|
||||
- Sequential and parallel execution support
|
||||
- Result aggregation from multiple agents
|
||||
- Graceful error handling and degradation
|
||||
- Streaming delegation support
|
||||
- Convenience functions: `delegate_to_librarian()`, `delegate_to_librarian_stream()`
|
||||
|
||||
- **Librarian Capability Registration**:
|
||||
- `LIBRARIAN_CAPABILITY` definition with research domains
|
||||
- Automatic registration on application startup
|
||||
- Integration with Household Registry
|
||||
|
||||
- **Configuration**:
|
||||
- `LIBRARY_DESK_HOST`: Library-desk API URL (default: `http://localhost:8089`)
|
||||
- `LIBRARY_DESK_API_KEY`: Optional API key for authentication
|
||||
- `LIBRARY_DESK_TIMEOUT`: Request timeout in seconds (default: 60)
|
||||
|
||||
- **Test Suite**:
|
||||
- 78 new tests for Phase 3 components
|
||||
- Protocol model tests (requests, responses, intents, errors)
|
||||
- Coordination engine tests (delegation, streaming, multi-agent)
|
||||
- Library-desk client tests (all endpoints with mocked HTTP)
|
||||
- Wiki write operation tests (update, smart-create)
|
||||
- Capability registration tests
|
||||
|
||||
### Changed
|
||||
- Application startup now registers The Librarian with Household Registry
|
||||
- Configuration expanded to support library-desk API integration
|
||||
- **Version loading**: APP_VERSION now dynamically loaded from pyproject.toml
|
||||
|
||||
## [1.0.0a] - 2025-12-11
|
||||
|
||||
### Added
|
||||
- **CI/CD Pipeline**: Release-triggered automated builds
|
||||
- Dockerfile for containerized deployment (Python 3.12-slim, port 8000)
|
||||
- Gitea Actions workflow triggered on release publish
|
||||
- Builds and pushes to git.schweitz.net registry with latest and version tags
|
||||
- Watchtower integration for automatic container updates
|
||||
- **Portainer Stack**: Production deployment configuration
|
||||
- Connects to docker-dataplane network for service discovery
|
||||
- Integration with ollama, searxng, and redis-shared services
|
||||
- Health check endpoint monitoring
|
||||
- Resource limits (1 CPU, 1GB memory)
|
||||
|
||||
### Changed
|
||||
- Version bump to 1.0.0 marking production-ready release
|
||||
|
||||
## [0.2.5] - 2025-12-07
|
||||
|
||||
### Added
|
||||
@@ -297,7 +467,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- CORS middleware
|
||||
- Exception handlers (OpenAI-compatible error format)
|
||||
|
||||
[Unreleased]: https://git.schweitz.net/jpmschweitzer/tatlock/compare/v0.2.0...main
|
||||
[Unreleased]: https://git.schweitz.net/jpmschweitzer/tatlock/compare/v1.2.0...main
|
||||
[1.2.0]: https://git.schweitz.net/jpmschweitzer/tatlock/compare/v1.1.0...v1.2.0
|
||||
[1.1.0]: https://git.schweitz.net/jpmschweitzer/tatlock/compare/v1.0.0a...v1.1.0
|
||||
[1.0.0a]: https://git.schweitz.net/jpmschweitzer/tatlock/compare/v0.2.5...v1.0.0a
|
||||
[0.2.5]: https://git.schweitz.net/jpmschweitzer/tatlock/compare/v0.2.0...v0.2.5
|
||||
[0.2.0]: https://git.schweitz.net/jpmschweitzer/tatlock/compare/v0.1.1...v0.2.0
|
||||
[0.1.1]: https://git.schweitz.net/jpmschweitzer/tatlock/compare/v0.1.0...v0.1.1
|
||||
[0.1.0]: https://git.schweitz.net/jpmschweitzer/tatlock/releases/tag/v0.1.0
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
FROM python:3.12-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update && apt-get install -y curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY requirements.txt pyproject.toml ./
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY src/ ./src/
|
||||
|
||||
ENV PYTHONPATH=/app
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
|
||||
+124
-87
@@ -4,35 +4,37 @@
|
||||
|
||||
This document outlines the phased implementation plan to transform the current OpenAI-compatible API into the full Tatlock household butler system.
|
||||
|
||||
## Current State (v0.1.1+ - Phase 1 Mostly Complete)
|
||||
## Current State (v1.2.0 - Phase F Complete)
|
||||
|
||||
**What we have**:
|
||||
- ✅ **The Orchestrator** - FastAPI infrastructure layer
|
||||
- OpenAI-compatible API endpoints (Responses API + Chat Completions)
|
||||
- Streaming coordination and conversation management
|
||||
- Response format with reasoning support
|
||||
- Test infrastructure (131 tests, 81.78% coverage)
|
||||
- ✅ **Tatlock Agent** - Real PydanticAI integration
|
||||
- Connected to Ollama (mistral-nemo:latest)
|
||||
- British butler personality with research mindset
|
||||
- Streaming responses with reasoning
|
||||
- Tool calling framework functional
|
||||
- ✅ **Permanent Tools**
|
||||
- Calculator (safe mathematical expressions)
|
||||
- Date/Time toolkit (current time, relative dates, time differences)
|
||||
- Web search (SearXNG integration)
|
||||
- Test infrastructure (~400 tests)
|
||||
- ✅ **Two-Tier Architecture**
|
||||
- The Steward analyzes requests and recommends capabilities
|
||||
- Tatlock coordinates execution with scoped tools
|
||||
- Real-time streaming of analysis and reasoning
|
||||
- ✅ **Household Staff**
|
||||
- **Tatlock** (Butler): Primary interface with witty personality
|
||||
- **The Steward**: Request analysis and capability recommendation
|
||||
- **The Librarian**: Research via library-desk HybridRAG + wiki
|
||||
- **The Biographer**: User memory, profiles, preferences, semantic recall
|
||||
- ✅ **Core Tools**
|
||||
- Calculator, Date/Time toolkit, Web search (SearXNG)
|
||||
- ✅ **Memory System**
|
||||
- Direct access layer (memory_service) for fast lookups
|
||||
- Vector storage (Qdrant) for semantic recall
|
||||
- Session cache (Redis) with 24h TTL
|
||||
- Multi-tenancy via ContextVar
|
||||
- ✅ Mock agent (lorem-tester for testing)
|
||||
- ✅ Agent interface abstraction
|
||||
|
||||
**What we need**:
|
||||
- **The Household** - Full multi-agent coordination:
|
||||
- The Steward (first-tier request analysis)
|
||||
- Tatlock coordination layer (expert agent delegation)
|
||||
- Expert household staff agents (Librarian, Developer, Handyman, etc.)
|
||||
- Multi-tenant database architecture
|
||||
- Containerized service ecosystem
|
||||
- More household staff (Developer, Secretary, Handyman, Housekeeper)
|
||||
- MCP (Model Context Protocol) integration
|
||||
- Dynamic model switching for specialized tasks
|
||||
- Full multi-tenant database (PostgreSQL)
|
||||
|
||||
---
|
||||
|
||||
@@ -359,15 +361,18 @@ User Request → Orchestrator → Steward Analysis → Recommendations → Tatlo
|
||||
|
||||
### Success Criteria
|
||||
|
||||
- [ ] **Steward analyzes incoming requests** using PydanticAI agent
|
||||
- [ ] **Produces structured recommendations** (tools, agents, reasoning)
|
||||
- [ ] **Recommendations formatted as prepended note** to Tatlock
|
||||
- [ ] **Tool registry is queryable and extensible** via clean API
|
||||
- [ ] **Steward output visible in reasoning stream** for transparency
|
||||
- [ ] **Only recommended tools available** to Tatlock (scoped context)
|
||||
- [ ] **Base model stays loaded** between Steward and Tatlock calls
|
||||
- [ ] **Recommendations are accurate** (not over/under-inclusive)
|
||||
- [ ] **Integration tests pass** for full Steward → Tatlock flow
|
||||
- [x] **Steward analyzes incoming requests** using PydanticAI agent
|
||||
- [x] **Produces structured recommendations** (tools, agents, reasoning)
|
||||
- [x] **Recommendations formatted as prepended note** to Tatlock
|
||||
- [x] **Tool registry is queryable and extensible** via clean API
|
||||
- [x] **Steward output visible in reasoning stream** for transparency
|
||||
- [x] **Only recommended tools available** to Tatlock (scoped context)
|
||||
- [x] **Base model stays loaded** between Steward and Tatlock calls
|
||||
- [x] **Recommendations are accurate** (not over/under-inclusive)
|
||||
- [x] **Integration tests pass** for full Steward → Tatlock flow
|
||||
|
||||
### Status
|
||||
**✅ COMPLETE** (v0.2.5)
|
||||
|
||||
### Performance Targets
|
||||
|
||||
@@ -435,12 +440,15 @@ The Steward is the foundation of the household architecture. Without it, we'd ne
|
||||
- Wait time transparency
|
||||
|
||||
### Success Criteria
|
||||
- [ ] Tatlock receives enriched requests (user + Steward notes)
|
||||
- [ ] Only recommended tools are available
|
||||
- [ ] Tatlock coordinates multiple tool calls
|
||||
- [ ] All actions streamed to reasoning output
|
||||
- [ ] Responses have consistent personality
|
||||
- [ ] Synthesizes multi-source results coherently
|
||||
- [x] Tatlock receives enriched requests (user + Steward notes)
|
||||
- [x] Only recommended tools are available
|
||||
- [x] Tatlock coordinates multiple tool calls
|
||||
- [x] All actions streamed to reasoning output
|
||||
- [x] Responses have consistent personality
|
||||
- [x] Synthesizes multi-source results coherently
|
||||
|
||||
### Status
|
||||
**✅ COMPLETE** (v1.1.0)
|
||||
|
||||
### Estimated Effort
|
||||
**4-5 weeks** - Complex coordination logic
|
||||
@@ -453,39 +461,44 @@ The Steward is the foundation of the household architecture. Without it, we'd ne
|
||||
|
||||
### Priority Expert Agents
|
||||
|
||||
1. **The Librarian** (Research & Knowledge Management) ⭐ **Priority**
|
||||
- Research assistance and synthesis
|
||||
- Automatic research dossier generation
|
||||
- Knowledge base queries and organization
|
||||
- Reference management
|
||||
- Wiki integration (future: dedicated wiki container)
|
||||
- Mind map maintenance (future)
|
||||
- *Rationale: Helps guide development priorities through better research*
|
||||
1. **The Librarian** (Research & Knowledge Management) ✅ **COMPLETE** (v1.1.0)
|
||||
- Research assistance via library-desk HybridRAG
|
||||
- Wiki page management (search, create, update)
|
||||
- Semantic vector search
|
||||
- Knowledge graph queries
|
||||
- Dossier browsing
|
||||
|
||||
2. **The Developer** (Software Development)
|
||||
2. **The Biographer** (User Memory) ✅ **COMPLETE** (v1.2.0)
|
||||
- User profile management (name, location, timezone)
|
||||
- Preference storage (units, theme)
|
||||
- Semantic memory recall ("What car do I drive?")
|
||||
- Fact storage from conversations
|
||||
- Session context caching
|
||||
|
||||
3. **The Developer** (Software Development) 🔜 **Planned**
|
||||
- Code generation assistance
|
||||
- Debugging support
|
||||
- Documentation generation
|
||||
- Architecture guidance
|
||||
- *Rationale: Directly supports building the system itself*
|
||||
|
||||
3. **The Handyman** (System Maintenance)
|
||||
4. **The Handyman** (System Maintenance) 🔜 **Planned**
|
||||
- System status queries
|
||||
- Log analysis
|
||||
- Basic troubleshooting
|
||||
- Infrastructure monitoring
|
||||
|
||||
4. **The Secretary** (Scheduling & Organization)
|
||||
- Calendar integration (placeholder)
|
||||
- Task management (placeholder)
|
||||
5. **The Secretary** (Scheduling & Organization) 🔜 **Planned**
|
||||
- Calendar integration
|
||||
- Task management
|
||||
- Reminder system
|
||||
- Schedule conflict detection
|
||||
|
||||
5. **The Housekeeper** (Home Automation)
|
||||
6. **The Housekeeper** (Home Automation) 🔜 **Planned**
|
||||
- Home Assistant integration
|
||||
- Device control interface
|
||||
- Status queries
|
||||
- Automation triggers
|
||||
- Environmental monitoring
|
||||
|
||||
### Each Agent Includes
|
||||
- Specialized prompt and personality
|
||||
@@ -494,12 +507,15 @@ The Steward is the foundation of the household architecture. Without it, we'd ne
|
||||
- Integration with Butler orchestration
|
||||
|
||||
### Success Criteria
|
||||
- [ ] Each agent implemented as separate module
|
||||
- [ ] Agents callable via tool framework
|
||||
- [ ] Agents use specialized prompts
|
||||
- [ ] Results integrate cleanly with Butler
|
||||
- [x] Each agent implemented as separate module
|
||||
- [x] Agents callable via tool framework
|
||||
- [x] Agents use specialized prompts
|
||||
- [x] Results integrate cleanly with Butler
|
||||
- [ ] Can invoke specialized models (e.g., Codestral for Developer)
|
||||
|
||||
### Status
|
||||
**🔶 PARTIAL** - Librarian and Biographer complete, others planned
|
||||
|
||||
### Estimated Effort
|
||||
**6-8 weeks** - Parallel development possible
|
||||
|
||||
@@ -556,31 +572,37 @@ The core orchestration (Steward → Butler → Experts) can work entirely with i
|
||||
|
||||
### Services to Integrate
|
||||
|
||||
1. **Redis (Memory & Caching)**
|
||||
- Docker compose setup
|
||||
- Conversation cache
|
||||
- Short-term memory
|
||||
- Session management
|
||||
1. **Redis (Memory & Caching)** ✅ **COMPLETE** (v1.2.0)
|
||||
- Benchmark storage (db=1)
|
||||
- Memory cache for sessions (db=2)
|
||||
- 24h TTL for session context
|
||||
- Recent entities tracking
|
||||
|
||||
3. **Qdrant (Vector Storage)**
|
||||
- Docker compose setup
|
||||
- Long-term memory embeddings
|
||||
- Semantic search
|
||||
- Conversation history vectors
|
||||
2. **Qdrant (Vector Storage)** ✅ **COMPLETE** (v1.2.0)
|
||||
- Per-user memory collections
|
||||
- 768-dim nomic-embed-text vectors
|
||||
- Semantic search for recall
|
||||
- Type-based filtering
|
||||
|
||||
4. **SearxNG (Web Search)**
|
||||
- Docker compose setup
|
||||
3. **SearxNG (Web Search)** ✅ **COMPLETE** (v0.2.0)
|
||||
- Search tool integration
|
||||
- Result processing
|
||||
- Privacy-preserving queries
|
||||
|
||||
4. **library-desk (Research API)** ✅ **COMPLETE** (v1.1.0)
|
||||
- HybridRAG search
|
||||
- Wiki management
|
||||
- Knowledge graph queries
|
||||
|
||||
### Success Criteria
|
||||
- [ ] All services defined in docker-compose.yml
|
||||
- [ ] Services communicate correctly
|
||||
- [ ] Tatlock can invoke web search
|
||||
- [ ] Redis used for session data
|
||||
- [ ] Qdrant stores conversation embeddings
|
||||
- [ ] Ollama serves the base model
|
||||
- [x] Services communicate correctly
|
||||
- [x] Tatlock can invoke web search
|
||||
- [x] Redis used for session data
|
||||
- [x] Qdrant stores user memories
|
||||
- [x] Ollama serves the base model
|
||||
|
||||
### Status
|
||||
**✅ COMPLETE** - All core services integrated
|
||||
|
||||
### Estimated Effort
|
||||
**3-4 weeks** - Infrastructure setup
|
||||
@@ -629,33 +651,48 @@ The core orchestration (Steward → Butler → Experts) can work entirely with i
|
||||
|
||||
### Deliverables
|
||||
|
||||
1. **Long-Term Memory**
|
||||
- Conversation embedding pipeline
|
||||
- Semantic search over history
|
||||
- Memory consolidation
|
||||
- Relevance ranking
|
||||
1. **Long-Term Memory** ✅ **COMPLETE** (v1.2.0 - Phase F)
|
||||
- Memory service for direct key-based access
|
||||
- Qdrant vector storage for semantic recall
|
||||
- Embedding via nomic-embed-text
|
||||
- The Biographer agent for memory management
|
||||
|
||||
2. **Context Management**
|
||||
2. **Session Memory** ✅ **COMPLETE** (v1.2.0)
|
||||
- Redis session cache with 24h TTL
|
||||
- Recent entities tracking
|
||||
- Conversation context preservation
|
||||
- Multi-tenancy via ContextVar
|
||||
|
||||
3. **Steward Integration** ✅ **COMPLETE** (v1.2.0)
|
||||
- Memory pre-fetch during request analysis
|
||||
- Profile/preferences included in context
|
||||
- Keyword-based context determination
|
||||
|
||||
4. **Context Management** 🔜 **Future**
|
||||
- Smart context window trimming
|
||||
- Conversation branching
|
||||
- Topic tracking
|
||||
- Memory retrieval integration
|
||||
|
||||
3. **Personalization**
|
||||
5. **Personalization** 🔜 **Future**
|
||||
- User preference learning
|
||||
- Interaction pattern analysis
|
||||
- Adaptive responses
|
||||
- Custom agent personalities per user
|
||||
|
||||
### Success Criteria
|
||||
- [x] User facts stored in Qdrant with semantic search
|
||||
- [x] Profile and preferences accessible via memory_service
|
||||
- [x] Session context cached in Redis
|
||||
- [x] User preferences affect responses (via Steward pre-fetch)
|
||||
- [ ] Conversations automatically embedded to Qdrant
|
||||
- [ ] Relevant history retrieved for new requests
|
||||
- [ ] Context stays within model limits
|
||||
- [ ] User preferences affect responses
|
||||
- [ ] Memory improves over time
|
||||
- [ ] Memory improves over time (learning from interactions)
|
||||
|
||||
### Status
|
||||
**🔶 PARTIAL** - Core memory system complete, advanced features planned
|
||||
|
||||
### Estimated Effort
|
||||
**4-5 weeks** - AI/ML heavy
|
||||
**4-5 weeks** - AI/ML heavy (remaining work)
|
||||
|
||||
---
|
||||
|
||||
@@ -871,13 +908,13 @@ Phase 9 (Extended Staff) → Phase 10 (UX) → Phase 11 (Production)
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Immediate**: Commit model name fix (Tatlock)
|
||||
2. **Week 1-2**: Begin Phase 1 (PostgreSQL + multi-tenancy design)
|
||||
3. **Week 3**: Parallel prototype of Steward agent
|
||||
4. **Ongoing**: Update this roadmap as we learn
|
||||
1. **Priority**: Implement The Developer agent for code assistance
|
||||
2. **Integration**: Add Home Assistant integration for The Housekeeper
|
||||
3. **Calendar**: Integrate scheduling service for The Secretary
|
||||
4. **Ongoing**: Add more household staff as needed
|
||||
|
||||
---
|
||||
|
||||
**Document Status**: Active planning document
|
||||
**Created**: 2025-12-06
|
||||
**Last Updated**: 2025-12-06
|
||||
**Last Updated**: 2025-12-13
|
||||
|
||||
@@ -0,0 +1,679 @@
|
||||
# Orchestration Scenarios and Tool Flows
|
||||
|
||||
This document outlines example scenarios of varying complexity to illustrate the desired orchestration patterns between Tatlock (Butler/Coordinator), expert agents (The Librarian, etc.), and the user.
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
```
|
||||
User Request
|
||||
↓
|
||||
[Steward] → Analyzes request, has visibility into ALL capabilities
|
||||
→ Makes routing decision: which experts needed
|
||||
→ Passes simplified instruction to Tatlock (not raw tool schemas)
|
||||
↓
|
||||
[Tatlock/Butler] → Coordinator, receives "use Librarian for wiki creation"
|
||||
→ Calls expert agents as tools
|
||||
→ Synthesizes responses into butler-voice answer
|
||||
↓
|
||||
[Expert Agents] → The Librarian, Home Automation, Memory, etc.
|
||||
→ Each has their own specialized tools
|
||||
→ Return structured results to Tatlock
|
||||
↓
|
||||
[External APIs] → library-desk, home-assistant, user-db, etc.
|
||||
```
|
||||
|
||||
**Key Principles**:
|
||||
|
||||
1. **Steward sees everything** - Has access to all capability descriptions to make informed routing decisions
|
||||
2. **Simplified passthrough** - Tatlock receives "delegate to Librarian for research" not 16 tool schemas
|
||||
3. **Expert agents are tools** - Tatlock calls `librarian_agent(task)`, not `hybrid_search()` directly
|
||||
4. **Each expert owns their tools** - Librarian has wiki tools, Home Automation has device tools
|
||||
5. **Results flow up** - Tatlock synthesizes all expert responses into coherent butler answer
|
||||
|
||||
---
|
||||
|
||||
## Scenario 1: Weather Check (Multi-Step with Memory Lookup)
|
||||
|
||||
**User**: "What's the weather like?"
|
||||
|
||||
### Complexity Analysis
|
||||
|
||||
This seemingly simple request requires:
|
||||
1. **Location determination** - Where does the user want weather for?
|
||||
2. **Memory/database lookup** - Retrieve user's home location or current location
|
||||
3. **Weather data fetch** - Search for weather at determined location
|
||||
|
||||
### Flow
|
||||
|
||||
```
|
||||
1. Steward Analysis
|
||||
→ Capabilities needed: memory (user context), tatlock_core (web search)
|
||||
→ Complexity: moderate
|
||||
→ Note: Location must be determined before weather lookup
|
||||
|
||||
2. Tatlock Execution - Step 1
|
||||
<think>User asked about weather but didn't specify location.
|
||||
Checking user profile for home location...</think>
|
||||
→ Calls: memory_agent(task: "get user home location")
|
||||
→ Memory queries user database
|
||||
→ Returns: "User home location: Amsterdam, Netherlands"
|
||||
|
||||
3. Tatlock Execution - Step 2
|
||||
<think>User is based in Amsterdam. Fetching current weather...</think>
|
||||
→ Calls: search_web("current weather Amsterdam Netherlands")
|
||||
→ Receives: "Amsterdam: 12°C, light rain, humidity 78%"
|
||||
|
||||
4. Response
|
||||
"Currently 12°C with light rain in Amsterdam, sir. You might want
|
||||
to grab an umbrella if you're heading out."
|
||||
```
|
||||
|
||||
### Intra-System Prompts
|
||||
|
||||
**Steward → Tatlock Note**:
|
||||
```
|
||||
Weather query - location not specified.
|
||||
1. First: Query memory for user's location (home or current)
|
||||
2. Then: Search weather for that location
|
||||
Capabilities: memory, tatlock_core
|
||||
Complexity: moderate
|
||||
```
|
||||
|
||||
**Tatlock → Memory Agent**:
|
||||
```
|
||||
Task: Retrieve user's location for weather query.
|
||||
Context: User asked about weather without specifying location.
|
||||
Action required: Return user's home location or current known location.
|
||||
|
||||
Reference (user's original request): "What's the weather like?"
|
||||
```
|
||||
|
||||
**Memory Agent → Tatlock Response**:
|
||||
```
|
||||
User location retrieved:
|
||||
- Home location: Amsterdam, Netherlands
|
||||
- Last known location: Amsterdam (home)
|
||||
- Location confidence: high
|
||||
- Source: user profile settings
|
||||
```
|
||||
|
||||
### Alternative Flow: Location Ambiguity
|
||||
|
||||
If user has multiple locations or is traveling:
|
||||
|
||||
```
|
||||
Memory Agent → Tatlock Response:
|
||||
User has multiple locations:
|
||||
- Home: Amsterdam, Netherlands
|
||||
- Office: Rotterdam, Netherlands
|
||||
- Currently traveling: Unknown
|
||||
|
||||
Recommendation: Ask user to clarify or use home location as default.
|
||||
```
|
||||
|
||||
Tatlock could then either:
|
||||
- Ask user: "Shall I check the weather in Amsterdam, sir, or elsewhere?"
|
||||
- Default to home: Use Amsterdam and mention the assumption
|
||||
|
||||
---
|
||||
|
||||
## Scenario 2: Adjust Temperature Based on Weather (Conditional Multi-Expert)
|
||||
|
||||
**User**: "Check the weather and if it's cold, turn up the heating"
|
||||
|
||||
### Complexity Analysis
|
||||
|
||||
This requires:
|
||||
1. **Location lookup** - Where to check weather (implicit: user's home)
|
||||
2. **Weather fetch** - Get current outdoor temperature
|
||||
3. **Conditional evaluation** - Is it "cold"? (requires threshold judgment)
|
||||
4. **Home automation** - Adjust heating if condition met
|
||||
|
||||
### Flow
|
||||
|
||||
```
|
||||
1. Steward Analysis
|
||||
→ Capabilities needed: memory, tatlock_core, home_automation
|
||||
→ Complexity: moderate
|
||||
→ Note: Conditional logic - heating only if cold
|
||||
→ Sequence: location → weather → evaluate → (maybe) heating
|
||||
|
||||
2. Tatlock Execution - Step 1
|
||||
<think>Need to check weather at user's location first...</think>
|
||||
→ Calls: memory_agent(task: "get user home location")
|
||||
→ Returns: "Amsterdam, Netherlands"
|
||||
|
||||
3. Tatlock Execution - Step 2
|
||||
<think>Fetching weather for Amsterdam...</think>
|
||||
→ Calls: search_web("current weather Amsterdam Netherlands")
|
||||
→ Receives: "Current temperature: 8°C, cloudy, wind 15km/h"
|
||||
|
||||
4. Tatlock Evaluation
|
||||
<think>Temperature is 8°C - that's cold by most standards.
|
||||
User requested heating adjustment if cold. Will proceed...</think>
|
||||
|
||||
5. Tatlock Execution - Step 3
|
||||
<think>Delegating heating adjustment to Home Automation...</think>
|
||||
→ Calls: home_automation_agent(task)
|
||||
→ Home Automation executes: set_thermostat(temperature=21)
|
||||
→ Receives: "Thermostat set to 21°C"
|
||||
|
||||
6. Response
|
||||
"It's rather brisk outside at 8°C, sir. I've taken the liberty of raising
|
||||
the heating to a comfortable 21°C. The house should warm up shortly."
|
||||
```
|
||||
|
||||
### Intra-System Prompts
|
||||
|
||||
**Steward → Tatlock Note**:
|
||||
```
|
||||
Conditional weather-to-heating request.
|
||||
1. Get user location from memory
|
||||
2. Check weather at location
|
||||
3. IF cold (suggest: below 15°C): delegate to home_automation to increase heating
|
||||
4. IF not cold: inform user, no action needed
|
||||
Capabilities: memory, tatlock_core, home_automation
|
||||
Complexity: moderate (conditional)
|
||||
```
|
||||
|
||||
**Tatlock → Home Automation Agent**:
|
||||
```
|
||||
Task: Increase heating due to cold weather conditions.
|
||||
Context: Current outdoor temperature is 8°C in Amsterdam. User requested
|
||||
heating adjustment if cold - condition is met.
|
||||
Action required: Set thermostat to comfortable temperature (recommend 21°C).
|
||||
|
||||
Reference (user's original request): "Check the weather and if it's cold, turn up the heating"
|
||||
```
|
||||
|
||||
**Home Automation → Tatlock Response**:
|
||||
```
|
||||
Action completed: Thermostat adjusted
|
||||
- Previous setting: 18°C
|
||||
- New setting: 21°C
|
||||
- Zone: All zones (whole house)
|
||||
- Estimated time to reach target: 15 minutes
|
||||
- Note: Eco mode disabled temporarily for faster heating
|
||||
```
|
||||
|
||||
### Alternative Flow: Not Cold
|
||||
|
||||
```
|
||||
Weather returns: "Current temperature: 18°C, partly sunny"
|
||||
|
||||
Tatlock Evaluation:
|
||||
<think>18°C isn't cold - no heating adjustment needed.
|
||||
Will inform user that conditions don't warrant heating...</think>
|
||||
|
||||
Response:
|
||||
"It's a pleasant 18°C outside, sir - not particularly cold.
|
||||
I don't believe the heating requires adjustment, but do let
|
||||
me know if you'd like it warmer regardless."
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Scenario 3: Adjust Lights Based on Time (Conditional Logic)
|
||||
|
||||
**User**: "Set the lights appropriately for the time of day"
|
||||
|
||||
### Flow
|
||||
|
||||
```
|
||||
1. Steward Analysis
|
||||
→ Capabilities needed: tatlock_core (datetime), home_automation
|
||||
→ Complexity: moderate
|
||||
|
||||
2. Tatlock Execution - Step 1
|
||||
<think>Checking current time to determine appropriate lighting...</think>
|
||||
→ Calls: get_current_datetime()
|
||||
→ Receives: "2024-12-11 21:45"
|
||||
|
||||
3. Tatlock Evaluation
|
||||
<think>It's evening (21:45). Will set warm, dimmed lighting for wind-down time...</think>
|
||||
|
||||
4. Tatlock Execution - Step 2
|
||||
→ Calls: home_automation_agent(task)
|
||||
→ Home Automation executes: set_scene("evening_relaxation")
|
||||
→ Receives: "Scene 'evening_relaxation' activated - warm white 40%"
|
||||
|
||||
5. Response
|
||||
"It's quarter to ten in the evening, sir. I've set the lights to a warm,
|
||||
subdued glow - ideal for winding down. Shall I also draw the curtains?"
|
||||
```
|
||||
|
||||
### Intra-System Prompts
|
||||
|
||||
**Tatlock → Home Automation Agent**:
|
||||
```
|
||||
Task: Set lighting appropriate for current time of day.
|
||||
Context: Current time is 21:45 (evening). User wants lights adjusted automatically.
|
||||
Action required: Activate appropriate lighting scene for evening/night.
|
||||
|
||||
Reference (user's original request): "Set the lights appropriately for the time of day"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Scenario 4: Create a Reminder (Simple Expert Delegation)
|
||||
|
||||
**User**: "Remind me to call the dentist tomorrow at 2pm"
|
||||
|
||||
### Flow
|
||||
|
||||
```
|
||||
1. Steward Analysis
|
||||
→ Capabilities needed: personal_assistant (reminders)
|
||||
→ Complexity: simple
|
||||
|
||||
2. Tatlock Execution
|
||||
<think>Creating reminder for tomorrow afternoon...</think>
|
||||
→ Calls: personal_assistant_agent(task)
|
||||
→ PA executes: create_reminder(title, datetime, notification_time)
|
||||
→ Receives: "Reminder created: ID 847"
|
||||
|
||||
3. Response
|
||||
"Very good, sir. I've set a reminder to call the dentist for tomorrow at
|
||||
2 o'clock in the afternoon. I'll notify you 15 minutes beforehand."
|
||||
```
|
||||
|
||||
### Intra-System Prompts
|
||||
|
||||
**Tatlock → Personal Assistant Agent**:
|
||||
```
|
||||
Task: Create a reminder for the user.
|
||||
Details:
|
||||
- Subject: Call the dentist
|
||||
- When: Tomorrow at 14:00
|
||||
- Notification: 15 minutes before (default)
|
||||
|
||||
Reference (user's original request): "Remind me to call the dentist tomorrow at 2pm"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Scenario 5: Schedule Recurring Automation (Complex, Multi-Expert)
|
||||
|
||||
**User**: "Schedule the lights to turn on at 09:00 every weekend day"
|
||||
|
||||
### Flow
|
||||
|
||||
```
|
||||
1. Steward Analysis
|
||||
→ Capabilities needed: home_automation (scheduling)
|
||||
→ Complexity: moderate
|
||||
→ Note: Recurring schedule, weekend-only
|
||||
|
||||
2. Tatlock Execution
|
||||
<think>Setting up recurring weekend morning lighting schedule...</think>
|
||||
→ Calls: home_automation_agent(task)
|
||||
→ HA executes: create_schedule(trigger, days, action)
|
||||
→ Receives: "Schedule created: ID wake_light_weekend"
|
||||
|
||||
3. Response
|
||||
"The weekend wake-up lighting is now scheduled, sir. Every Saturday and
|
||||
Sunday at 9 o'clock sharp, the lights will illuminate. Would you prefer
|
||||
a gradual sunrise simulation, or an immediate full brightness?"
|
||||
```
|
||||
|
||||
### Intra-System Prompts
|
||||
|
||||
**Tatlock → Home Automation Agent**:
|
||||
```
|
||||
Task: Create a recurring lighting schedule.
|
||||
Details:
|
||||
- Action: Turn on lights
|
||||
- Time: 09:00
|
||||
- Days: Saturday, Sunday (weekends only)
|
||||
- Recurrence: Weekly
|
||||
|
||||
Reference (user's original request): "Schedule the lights to turn on at 09:00 every weekend day"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Scenario 6: Create Wiki Page About Topic (Expert with Research)
|
||||
|
||||
**User**: "Create a wiki page about CI/CD"
|
||||
|
||||
### Flow
|
||||
|
||||
```
|
||||
1. Steward Analysis
|
||||
→ Capabilities needed: librarian
|
||||
→ Complexity: complex
|
||||
→ Specific action: smart_create (research-backed page creation)
|
||||
|
||||
2. Tatlock Execution
|
||||
<think>Delegating to The Librarian for research and wiki page creation...</think>
|
||||
→ Calls: librarian_agent(task)
|
||||
|
||||
3. Librarian Internal Flow
|
||||
→ hybrid_search("CI/CD continuous integration deployment")
|
||||
→ Synthesizes content from wiki, graph, and web sources
|
||||
→ create_wiki_page(title, path, content, tags)
|
||||
→ Returns summary to Tatlock
|
||||
|
||||
4. Tatlock Receives
|
||||
"Page created: CI/CD
|
||||
Path: /technology/cicd
|
||||
Sources used: 12 (3 wiki, 4 graph, 5 web)
|
||||
Forward links: 8, Backward links: 3"
|
||||
|
||||
5. Response
|
||||
"The wiki page on CI/CD has been created, sir. The Librarian has compiled
|
||||
information from 12 sources, including our existing documentation on DevOps
|
||||
practices. The page is now available at /technology/cicd and has been
|
||||
cross-linked with related topics like Jenkins, GitHub Actions, and Docker."
|
||||
```
|
||||
|
||||
### Intra-System Prompts
|
||||
|
||||
**Tatlock → Librarian Agent**:
|
||||
```
|
||||
Task: Create a comprehensive wiki page about CI/CD.
|
||||
Context: User wants a new wiki page created. Use research capabilities to
|
||||
gather information and create a well-sourced page.
|
||||
|
||||
Action required:
|
||||
1. Research the topic using hybrid search (wiki, knowledge graph, web)
|
||||
2. Create wiki page with synthesized content
|
||||
3. Apply appropriate tags (suggest: technology, devops, automation)
|
||||
4. Report back with page details and research summary
|
||||
|
||||
Reference (user's original request): "Create a wiki page about CI/CD"
|
||||
```
|
||||
|
||||
**Librarian → Tatlock Response**:
|
||||
```
|
||||
Task completed: Wiki page created
|
||||
|
||||
Page Details:
|
||||
- Title: CI/CD (Continuous Integration/Continuous Deployment)
|
||||
- Path: /technology/cicd
|
||||
- Tags: technology, devops, automation, software-development
|
||||
|
||||
Research Summary:
|
||||
- Wiki sources: 3 pages (DevOps Overview, Jenkins Setup, GitHub Actions Guide)
|
||||
- Knowledge graph entities: 4 (linked to Docker, Kubernetes, Git, Testing)
|
||||
- Web sources: 5 articles (current best practices, 2024 trends)
|
||||
- Total sources synthesized: 12
|
||||
|
||||
Knowledge Graph Updates:
|
||||
- Forward links created: 8 (to existing related pages)
|
||||
- Backward links created: 3 (updated existing pages to reference new page)
|
||||
|
||||
Content includes sections on:
|
||||
- Definition and core concepts
|
||||
- CI vs CD distinction
|
||||
- Popular tools comparison
|
||||
- Best practices
|
||||
- Integration with our documented infrastructure
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Scenario 7: Update Stock Wiki with Evaluation Tracker (Complex, Multi-Tool Expert)
|
||||
|
||||
**User**: "Update the GOOG stock wiki page with a rolling evaluation tracker"
|
||||
|
||||
### Flow
|
||||
|
||||
```
|
||||
1. Steward Analysis
|
||||
→ Capabilities needed: librarian
|
||||
→ Complexity: complex
|
||||
→ Specific action: search existing page, fetch current data, update page
|
||||
|
||||
2. Tatlock Execution
|
||||
<think>This requires finding the existing page, fetching current stock data,
|
||||
and updating with a new evaluation section. Delegating to The Librarian...</think>
|
||||
→ Calls: librarian_agent(task)
|
||||
|
||||
3. Librarian Internal Flow - Step 1
|
||||
<think>First, locating the existing GOOG wiki page...</think>
|
||||
→ search_wiki("GOOG stock Alphabet")
|
||||
→ Receives: Page ID 234, path /investments/stocks/goog
|
||||
|
||||
4. Librarian Internal Flow - Step 2
|
||||
<think>Fetching current page content to preserve existing information...</think>
|
||||
→ get_wiki_page(234)
|
||||
→ Receives: Current page content
|
||||
|
||||
5. Librarian Internal Flow - Step 3
|
||||
<think>Researching current stock data and evaluation metrics...</think>
|
||||
→ hybrid_search("GOOG Alphabet stock price evaluation 2024")
|
||||
→ Receives: Current price, P/E ratio, analyst ratings, etc.
|
||||
|
||||
6. Librarian Internal Flow - Step 4
|
||||
<think>Updating page with new rolling evaluation section...</think>
|
||||
→ update_wiki_page(234, content=updated_content)
|
||||
→ Receives: Update confirmation
|
||||
|
||||
7. Tatlock Receives
|
||||
"Page updated: GOOG Stock Analysis
|
||||
Added section: Rolling Evaluation Tracker
|
||||
Data points added: Price history, P/E trend, Analyst consensus
|
||||
Last updated: 2024-12-11"
|
||||
|
||||
8. Response
|
||||
"The GOOG stock page has been updated, sir. I've added a rolling evaluation
|
||||
tracker with current metrics: the stock is trading at $178.32 with a P/E
|
||||
of 24.8, and analyst consensus remains 'Buy'. The tracker includes a
|
||||
90-day price trend and quarterly earnings history. Shall I set up
|
||||
automatic weekly updates?"
|
||||
```
|
||||
|
||||
### Intra-System Prompts
|
||||
|
||||
**Tatlock → Librarian Agent**:
|
||||
```
|
||||
Task: Update the GOOG (Alphabet) stock wiki page with a rolling evaluation tracker.
|
||||
Context: User wants to add ongoing stock evaluation tracking to an existing page.
|
||||
|
||||
Actions required:
|
||||
1. Find the existing GOOG stock wiki page
|
||||
2. Read current page content (preserve existing information)
|
||||
3. Research current stock data and evaluation metrics
|
||||
4. Update the page with a new "Rolling Evaluation Tracker" section including:
|
||||
- Current price and change
|
||||
- Key ratios (P/E, P/B, etc.)
|
||||
- Analyst consensus
|
||||
- Price trend (30/60/90 day)
|
||||
- Recent earnings summary
|
||||
5. Report back with update summary
|
||||
|
||||
Reference (user's original request): "Update the GOOG stock wiki page with a rolling evaluation tracker"
|
||||
```
|
||||
|
||||
**Librarian → Tatlock Response**:
|
||||
```
|
||||
Task completed: Wiki page updated
|
||||
|
||||
Page Details:
|
||||
- Title: GOOG - Alphabet Inc. Stock Analysis
|
||||
- Path: /investments/stocks/goog
|
||||
- Page ID: 234
|
||||
|
||||
Update Summary:
|
||||
- New section added: "Rolling Evaluation Tracker"
|
||||
- Existing content: Preserved (company overview, investment thesis)
|
||||
|
||||
Evaluation Data Added:
|
||||
- Current Price: $178.32 (+1.2% today)
|
||||
- P/E Ratio: 24.8 (vs sector avg 28.3)
|
||||
- P/B Ratio: 6.2
|
||||
- 52-Week Range: $120.21 - $191.75
|
||||
- Analyst Consensus: Buy (24 buy, 8 hold, 2 sell)
|
||||
- Price Trend: +15.3% (90-day)
|
||||
- Last Earnings: Q3 2024 - Beat estimates by 8%
|
||||
|
||||
Sources:
|
||||
- Web: 4 financial data sources
|
||||
- Existing wiki: Investment strategy context
|
||||
|
||||
Suggested follow-up: Set up automated weekly data refresh?
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Scenario 8: Complex Multi-Expert Coordination
|
||||
|
||||
**User**: "Check if there's anything about the new project deadline in my notes,
|
||||
if so create a reminder, and if the deadline is this week, also update the
|
||||
project wiki page with a status note"
|
||||
|
||||
### Flow
|
||||
|
||||
```
|
||||
1. Steward Analysis
|
||||
→ Capabilities needed: librarian, personal_assistant
|
||||
→ Complexity: complex
|
||||
→ Note: Conditional multi-step, requires information passing between experts
|
||||
|
||||
2. Tatlock Execution - Step 1
|
||||
<think>First, searching for project deadline information in the wiki...</think>
|
||||
→ Calls: librarian_agent(search_task)
|
||||
→ Librarian: hybrid_search("new project deadline")
|
||||
→ Returns: "Project Alpha deadline: December 15, 2024 (this Friday)"
|
||||
|
||||
3. Tatlock Evaluation
|
||||
<think>Found deadline: December 15. That's this week (Friday).
|
||||
Need to: 1) Create reminder, 2) Update project wiki page...</think>
|
||||
|
||||
4. Tatlock Execution - Step 2 (parallel if possible)
|
||||
<think>Creating reminder and updating wiki status...</think>
|
||||
|
||||
→ Calls: personal_assistant_agent(reminder_task)
|
||||
→ PA: create_reminder("Project Alpha deadline", "2024-12-15 09:00")
|
||||
→ Returns: "Reminder created for Dec 15 at 9am"
|
||||
|
||||
→ Calls: librarian_agent(update_task)
|
||||
→ Librarian: search_wiki → get_wiki_page → update_wiki_page
|
||||
→ Returns: "Project Alpha page updated with deadline status note"
|
||||
|
||||
5. Response
|
||||
"I've found the deadline in your notes, sir - Project Alpha is due this
|
||||
Friday, December 15th. I've set a reminder for 9 o'clock that morning,
|
||||
and I've updated the project wiki page with a status note indicating
|
||||
the imminent deadline. Is there anything else you need to prepare?"
|
||||
```
|
||||
|
||||
### Intra-System Prompts
|
||||
|
||||
**Tatlock → Librarian Agent (Search)**:
|
||||
```
|
||||
Task: Search for information about a new project deadline.
|
||||
Context: User wants to find deadline information from their notes/wiki.
|
||||
|
||||
Action required:
|
||||
1. Search wiki and knowledge base for project deadline information
|
||||
2. Return: Project name, deadline date, and any relevant context
|
||||
|
||||
Reference (user's original request): "Check if there's anything about the new project deadline in my notes..."
|
||||
```
|
||||
|
||||
**Tatlock → Personal Assistant Agent**:
|
||||
```
|
||||
Task: Create a reminder for a project deadline.
|
||||
Details:
|
||||
- Subject: Project Alpha deadline
|
||||
- When: December 15, 2024 at 09:00
|
||||
- Priority: High (deadline is this week)
|
||||
- Notification: Morning of the deadline
|
||||
|
||||
Reference: Creating reminder based on deadline found in user's notes.
|
||||
```
|
||||
|
||||
**Tatlock → Librarian Agent (Update)**:
|
||||
```
|
||||
Task: Update the Project Alpha wiki page with a deadline status note.
|
||||
Context: Project deadline is December 15, 2024 (this Friday). User requested
|
||||
a status update since the deadline is this week.
|
||||
|
||||
Action required:
|
||||
1. Find the Project Alpha wiki page
|
||||
2. Add a status note/banner indicating the imminent deadline
|
||||
3. Optionally update any status fields
|
||||
|
||||
Reference: Part of user's request to track and highlight near-term deadlines.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Response Pattern Guidelines
|
||||
|
||||
### Tatlock's Think Updates (Streaming to User)
|
||||
|
||||
During multi-step operations, Tatlock should emit `<think>` updates to keep the user informed:
|
||||
|
||||
```
|
||||
<think>Analyzing your request...</think>
|
||||
<think>Searching for deadline information in the wiki...</think>
|
||||
<think>Found the deadline - December 15th. Creating reminder...</think>
|
||||
<think>Updating the project page with status note...</think>
|
||||
<think>All tasks complete. Composing response...</think>
|
||||
```
|
||||
|
||||
### Tatlock's Final Response Pattern
|
||||
|
||||
1. **Acknowledge** - Confirm understanding of the request
|
||||
2. **Summarize actions** - What was done, by whom (implicitly)
|
||||
3. **Key details** - Important information the user should know
|
||||
4. **Proactive offer** - Suggest related actions or follow-ups
|
||||
5. **Butler voice** - Formal but warm, with personality
|
||||
|
||||
### Expert Agent Response Pattern
|
||||
|
||||
1. **Task status** - Completed/Partial/Failed
|
||||
2. **Action summary** - What was done
|
||||
3. **Key data** - Information Tatlock needs to synthesize
|
||||
4. **Metadata** - IDs, counts, timestamps for reference
|
||||
5. **Suggestions** - Optional follow-up actions
|
||||
|
||||
---
|
||||
|
||||
## Error Handling Scenarios
|
||||
|
||||
### Expert Agent Failure
|
||||
|
||||
```
|
||||
Tatlock → Librarian: "Create wiki page about quantum computing"
|
||||
Librarian → Tatlock: "Error: library-desk API unavailable (connection timeout)"
|
||||
|
||||
Tatlock Response:
|
||||
"I'm afraid The Librarian is having some difficulty reaching the wiki
|
||||
service at the moment, sir. I can attempt a basic web search on quantum
|
||||
computing if you'd like, or we can try the wiki operation again in a
|
||||
few minutes."
|
||||
```
|
||||
|
||||
### Partial Completion
|
||||
|
||||
```
|
||||
User: "Create a reminder and add it to my calendar"
|
||||
|
||||
Tatlock → PA: Create reminder
|
||||
PA → Tatlock: "Reminder created successfully"
|
||||
|
||||
Tatlock → Calendar: Add to calendar
|
||||
Calendar → Tatlock: "Error: Calendar sync not configured"
|
||||
|
||||
Tatlock Response:
|
||||
"I've created the reminder, sir, but I wasn't able to add it to your
|
||||
calendar - it appears the calendar integration needs to be configured.
|
||||
The reminder will still alert you at the scheduled time. Shall I help
|
||||
set up the calendar connection?"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Summary: Key Design Principles
|
||||
|
||||
1. **Tatlock is the orchestrator** - Never exposes raw tool complexity to users
|
||||
2. **Expert agents are tools** - Tatlock calls them, they return structured responses
|
||||
3. **Context flows down** - Each expert gets only what they need to complete their task
|
||||
4. **Results flow up** - Tatlock synthesizes all responses into coherent butler-voice answer
|
||||
5. **Think updates maintain engagement** - User sees progress during complex operations
|
||||
6. **Errors are handled gracefully** - Tatlock explains and offers alternatives
|
||||
7. **Proactive suggestions** - Tatlock anticipates follow-up needs
|
||||
@@ -1,535 +0,0 @@
|
||||
# Phase 2 Completion Summary: The Steward
|
||||
|
||||
**Status**: ✅ COMPLETE
|
||||
**Completed**: 2025-12-07
|
||||
**Duration**: 1 day (accelerated from 7-week plan)
|
||||
**Test Coverage**: 223 passing tests (99.5% pass rate)
|
||||
|
||||
---
|
||||
|
||||
## Executive Summary
|
||||
|
||||
Phase 2 successfully implements **The Steward** - a first-tier LLM agent that creates a two-tier architecture for intelligent request routing. The Steward analyzes incoming requests, identifies relevant household capabilities, and provides scoped tool recommendations to Tatlock (the Butler).
|
||||
|
||||
This architecture prevents cognitive overload by ensuring Tatlock only sees tools relevant to each specific request, while maintaining full conversation context awareness and providing complete observability through benchmarking and logging.
|
||||
|
||||
---
|
||||
|
||||
## Delivered Features
|
||||
|
||||
### 1. The Steward Agent ✅
|
||||
**Location**: `src/agents/steward/`
|
||||
|
||||
- **Request Analysis**: Analyzes user requests with full conversation history
|
||||
- **Capability Recommendation**: Recommends relevant household tools/capabilities
|
||||
- **Context Awareness**: Identifies references to previous conversation turns
|
||||
- **Complexity Assessment**: Estimates request complexity (simple/moderate/complex)
|
||||
- **Missing Capability Detection**: Explicitly states when needed tools are unavailable
|
||||
- **VRAM Efficiency**: Uses same Ollama model as Tatlock (mistral-nemo:latest)
|
||||
|
||||
**Key Files**:
|
||||
- `agent.py`: Steward PydanticAI agent implementation
|
||||
- `schemas.py`: `StewardRecommendation` and `ConversationContext` structures
|
||||
- `service.py`: Service layer with logging and benchmarking
|
||||
|
||||
### 2. Household Registry ✅
|
||||
**Location**: `src/core/household_registry.py`
|
||||
|
||||
- **Centralized Capability Management**: Single source of truth for household tools
|
||||
- **Executive Summaries**: High-level capability descriptions for Steward/Butler coordination
|
||||
- **PydanticAI Toolsets**: Native toolset composition and scoping
|
||||
- **Domain Organization**: Tools organized by household member (e.g., `tatlock_core`)
|
||||
- **Dynamic Tool Scoping**: Creates combined toolsets based on recommendations
|
||||
|
||||
**Architecture**:
|
||||
```
|
||||
HouseholdRegistry
|
||||
├─ HouseholdMember (tatlock_core)
|
||||
│ ├─ HouseholdCapability (summary)
|
||||
│ └─ FunctionToolset (calculator, datetime, search)
|
||||
├─ Future: HouseholdMember (librarian)
|
||||
└─ Future: HouseholdMember (developer)
|
||||
```
|
||||
|
||||
### 3. Request Preprocessing Pipeline ✅
|
||||
**Location**: `src/core/preprocessing.py`
|
||||
|
||||
**4-Phase Flow**:
|
||||
1. **Steward Analysis**: Analyzes request with full conversation history
|
||||
2. **Tool Scoping**: Creates combined toolset from recommendations
|
||||
3. **Note Formatting**: Prepares Steward note for Butler (invisible to user)
|
||||
4. **Enrichment**: Returns `EnrichedRequest` with all context
|
||||
|
||||
**Integration**: Fully integrated with Responses API via `create_response_with_steward()`
|
||||
|
||||
### 4. Tool Usage Tracking ✅
|
||||
**Location**: `src/core/tool_tracking.py`
|
||||
|
||||
**Capabilities**:
|
||||
- Tracks recommended vs. actual tool usage
|
||||
- Logs unexpected tool calls (not recommended but used)
|
||||
- Logs unused recommendations (recommended but not used)
|
||||
- Records timing data for each tool call
|
||||
- Stores benchmarks to Redis for analysis
|
||||
|
||||
**Metrics Supported**:
|
||||
- Precision: Recommended and used / All recommendations
|
||||
- Recall: Recommended and used / All tool calls
|
||||
- F1 Score: Harmonic mean of precision and recall
|
||||
|
||||
### 5. Streaming Transparency ✅
|
||||
**Location**: `src/responses/streaming.py`
|
||||
|
||||
**Features**:
|
||||
- Streams Steward's analysis first (reasoning summary deltas)
|
||||
- Streams Tatlock's response second (output text deltas)
|
||||
- Full SSE support with proper event types
|
||||
- Conversation context visible in stream
|
||||
- Missing capabilities warnings included
|
||||
|
||||
**Event Sequence**:
|
||||
```
|
||||
1. response.reasoning_summary_text.delta (Steward analysis)
|
||||
2. response.reasoning_summary_text.done
|
||||
3. response.output_text.delta (Tatlock response)
|
||||
4. response.output_text.done
|
||||
5. response.done (final response)
|
||||
```
|
||||
|
||||
### 6. Structured Logging ✅
|
||||
**Location**: `src/core/logging_config.py`
|
||||
|
||||
**Features**:
|
||||
- JSON-formatted structured logging via `structlog`
|
||||
- Operation timing via context managers (`log_operation`)
|
||||
- Metadata enrichment for debugging
|
||||
- Integrated with benchmark recording
|
||||
- Machine-parseable output for analysis
|
||||
|
||||
### 7. Redis Benchmark Storage ✅
|
||||
**Location**: `src/core/benchmarks.py`
|
||||
|
||||
**Features**:
|
||||
- Cross-session performance metrics storage
|
||||
- Time-series data with 30-day automatic expiry
|
||||
- Operations tracked: `steward_analysis`, `tool_call`
|
||||
- Queryable by operation type, time range, metadata
|
||||
- Supports accuracy analysis (recommended vs. used)
|
||||
|
||||
**Benchmark Schema**:
|
||||
- Timestamp, operation, duration, success/failure
|
||||
- Steward-specific: recommendation_count, complexity
|
||||
- Tool-specific: tool_name, was_recommended, was_actually_used
|
||||
- Context: conversation_id, metadata dict
|
||||
|
||||
### 8. Benchmark Analysis Tools ✅
|
||||
**Location**: `scripts/benchmark_analysis.py`
|
||||
|
||||
**CLI Features**:
|
||||
```bash
|
||||
# Steward performance over last 24 hours
|
||||
python scripts/benchmark_analysis.py --operation steward_analysis --hours 24
|
||||
|
||||
# Tool recommendation accuracy over last 7 days
|
||||
python scripts/benchmark_analysis.py --tool-accuracy --days 7
|
||||
|
||||
# Summary of all operations
|
||||
python scripts/benchmark_analysis.py --summary --hours 1
|
||||
```
|
||||
|
||||
**Metrics Provided**:
|
||||
- Average Steward latency (target: < 2s)
|
||||
- Success rate percentage
|
||||
- Recommendation count distribution
|
||||
- Complexity distribution
|
||||
- Tool-specific accuracy (precision/recall/F1)
|
||||
- Per-tool usage patterns
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
### Request Flow
|
||||
|
||||
```
|
||||
User Request
|
||||
↓
|
||||
Responses API (FastAPI)
|
||||
↓
|
||||
┌─────────────────────────────────────────────┐
|
||||
│ Preprocessing Pipeline │
|
||||
│ ├─ Steward Agent │
|
||||
│ │ ├─ Receives: Full conversation history │
|
||||
│ │ ├─ Analyzes: Context + requirements │
|
||||
│ │ ├─ Queries: Household registry │
|
||||
│ │ └─ Returns: StewardRecommendation │
|
||||
│ │ │
|
||||
│ ├─ Create Scoped Toolset │
|
||||
│ │ └─ CombinedToolset from capabilities │
|
||||
│ │ │
|
||||
│ └─ Format Steward Note │
|
||||
│ └─ Context summary for Butler │
|
||||
└─────────────────────────────────────────────┘
|
||||
↓
|
||||
Tatlock Agent (Butler)
|
||||
├─ Receives: Enriched request + note
|
||||
├─ Tools: ONLY scoped recommendations
|
||||
├─ Tracking: Tool usage monitored
|
||||
└─ Context: Full conversation history
|
||||
↓
|
||||
Response to User
|
||||
├─ Steward's reasoning (streamed first)
|
||||
└─ Tatlock's response (streamed second)
|
||||
|
||||
Background:
|
||||
└─ Redis: Benchmarks + metrics
|
||||
```
|
||||
|
||||
### Two-Tier Abstraction
|
||||
|
||||
**Tier 1: Executive Summaries (Steward/Butler coordination)**
|
||||
```python
|
||||
HouseholdCapability(
|
||||
name="tatlock_core",
|
||||
role="Butler's Core Tools",
|
||||
category="core",
|
||||
description="Mathematical calculation, date/time operations, web search",
|
||||
domains=["computation", "information", "datetime"],
|
||||
cost="low",
|
||||
requires_network=True
|
||||
)
|
||||
```
|
||||
|
||||
**Tier 2: Implementation Details (Tool execution)**
|
||||
```python
|
||||
FunctionToolset containing:
|
||||
- calculate(expression: str) -> str
|
||||
- get_current_datetime(format_str: str) -> str
|
||||
- calculate_time_offset(offset: str) -> str
|
||||
- time_difference(date1: str, date2: str) -> str
|
||||
- search_web(query: str, num_results: int) -> str
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Test Coverage
|
||||
|
||||
### Test Statistics
|
||||
- **Total Tests**: 223 (219 passing, 1 pre-existing failure unrelated to Phase 2)
|
||||
- **Pass Rate**: 99.5%
|
||||
- **Coverage**: 77.6% overall
|
||||
|
||||
### Test Categories
|
||||
|
||||
#### Unit Tests ✅
|
||||
- **Household Registry** (12 tests): Registration, retrieval, toolset composition
|
||||
- **Steward Schemas** (11 tests): Data structures, formatting
|
||||
- **Steward Service** (9 tests): Request analysis, context detection, capabilities
|
||||
- **Preprocessing** (6 tests via integration): Request enrichment, tool scoping
|
||||
|
||||
#### Integration Tests ✅
|
||||
- **Steward → Tatlock Flow** (6 tests):
|
||||
- Simple math request
|
||||
- Conversation history propagation
|
||||
- No capabilities needed (conversational)
|
||||
- Tool tracker integration
|
||||
- Missing capabilities warning
|
||||
- Conversation ID propagation
|
||||
|
||||
- **Streaming Integration** (4 tests):
|
||||
- Basic streaming with Steward
|
||||
- Conversation history in streaming
|
||||
- Reasoning contains Steward analysis
|
||||
- Missing capabilities in stream
|
||||
|
||||
### Key Test Files
|
||||
- `tests/agents/steward/test_steward_schemas.py`
|
||||
- `tests/agents/steward/test_steward_service.py`
|
||||
- `tests/integration/test_steward_tatlock_integration.py`
|
||||
- `tests/integration/test_steward_streaming.py`
|
||||
|
||||
---
|
||||
|
||||
## Technical Achievements
|
||||
|
||||
### 1. PydanticAI Native Patterns ✅
|
||||
- `FunctionToolset` for tool grouping
|
||||
- `CombinedToolset` for dynamic composition
|
||||
- Decorator-based tool registration (`@agent.tool`)
|
||||
- Structured outputs via Pydantic models (`StewardRecommendation`)
|
||||
- Dependency injection for tracking (`RunContext[ToolCallTracker]`)
|
||||
|
||||
### 2. Tool Scoping Enforcement ✅
|
||||
- Compile-time scoping via toolset creation
|
||||
- Tools not even visible to LLM if not recommended
|
||||
- Fresh agent instances with scoped tools only
|
||||
- No runtime permission checks needed
|
||||
|
||||
### 3. Conversation Context Awareness ✅
|
||||
- Steward sees FULL conversation history
|
||||
- Identifies references to previous turns
|
||||
- Provides contextual notes to Butler
|
||||
- Example: "User mentioned Python debugging in turn 3"
|
||||
|
||||
### 4. Plain Text Approach ✅
|
||||
- Steward returns natural language analysis
|
||||
- Service layer parses for structured data
|
||||
- Keyword extraction for capabilities
|
||||
- Pattern matching for complexity and context
|
||||
|
||||
### 5. Observability ✅
|
||||
- Structured logging for all operations
|
||||
- Benchmark recording to Redis
|
||||
- Tool usage tracking (recommended vs. actual)
|
||||
- Cross-session performance analysis
|
||||
|
||||
---
|
||||
|
||||
## Performance Characteristics
|
||||
|
||||
### Latency (Estimated)
|
||||
- **Steward Analysis**: ~1-2 seconds (single LLM call)
|
||||
- **Tatlock Execution**: ~2-5 seconds (depends on tool usage)
|
||||
- **Total Added Overhead**: ~1-2 seconds vs. direct Tatlock call
|
||||
- **Streaming Transparency**: Steward reasoning visible immediately
|
||||
|
||||
### Resource Usage
|
||||
- **VRAM**: Same model for both agents (mistral-nemo:latest)
|
||||
- **Model Loading**: No additional model loads (efficient!)
|
||||
- **Redis**: Minimal (benchmarks with 30-day expiry)
|
||||
- **Network**: Only when web search tools used
|
||||
|
||||
### Accuracy Targets
|
||||
- **Recommendation Precision**: > 90% (tools recommended and actually used)
|
||||
- **Recommendation Recall**: > 90% (tools used were recommended)
|
||||
- **False Positives**: < 10% (recommended but not used)
|
||||
- **False Negatives**: < 10% (used but not recommended)
|
||||
|
||||
*Note: Actual metrics available via `scripts/benchmark_analysis.py` after production usage*
|
||||
|
||||
---
|
||||
|
||||
## Files Created
|
||||
|
||||
### Core Implementation
|
||||
1. `src/core/household_registry.py` - Capability management
|
||||
2. `src/core/preprocessing.py` - Request preprocessing pipeline
|
||||
3. `src/core/tool_tracking.py` - Tool usage tracking
|
||||
4. `src/core/logging_config.py` - Structured logging (M1)
|
||||
5. `src/core/benchmarks.py` - Redis benchmark storage (M1)
|
||||
|
||||
### Steward Agent
|
||||
6. `src/agents/steward/agent.py` - Steward PydanticAI agent
|
||||
7. `src/agents/steward/schemas.py` - Data structures
|
||||
8. `src/agents/steward/service.py` - Service layer
|
||||
|
||||
### Tatlock Core Organization
|
||||
9. `src/agents/tatlock_core/tools.py` - Tool implementations (reorganized)
|
||||
10. `src/agents/tatlock_core/toolset.py` - PydanticAI toolset
|
||||
11. `src/agents/tatlock_core/capability.py` - Registry integration
|
||||
|
||||
### Tests
|
||||
12. `tests/agents/steward/test_steward_schemas.py` - Schema tests
|
||||
13. `tests/agents/steward/test_steward_service.py` - Service tests
|
||||
14. `tests/integration/test_steward_tatlock_integration.py` - Full flow tests
|
||||
15. `tests/integration/test_steward_streaming.py` - Streaming tests
|
||||
|
||||
### Tools & Documentation
|
||||
16. `scripts/benchmark_analysis.py` - Performance analysis CLI
|
||||
17. `PHASE2_PLAN.md` - Detailed implementation plan
|
||||
18. `PHASE2_COMPLETE.md` - This completion summary
|
||||
|
||||
### Modified Files
|
||||
- `src/agents/tatlock.py` - Added `run_with_scoped_tools()` method
|
||||
- `src/responses/service.py` - Added `create_response_with_steward()`
|
||||
- `src/responses/router.py` - Steward routing logic
|
||||
- `src/responses/streaming.py` - Added `stream_response_with_steward()`
|
||||
- `CHANGELOG.md` - Phase 2 documentation
|
||||
|
||||
---
|
||||
|
||||
## Success Metrics
|
||||
|
||||
### Technical ✅
|
||||
- ✅ Household registry operational with executive summaries
|
||||
- ✅ Steward produces structured recommendations
|
||||
- ✅ Steward analyzes full conversation context
|
||||
- ✅ Tool scoping enforced (Tatlock can't use non-recommended tools)
|
||||
- ✅ Model efficiency preserved (no reload delays)
|
||||
- ✅ Performance benchmarks recorded to Redis
|
||||
- ✅ Tool usage tracking (recommended vs. actual)
|
||||
- ✅ Streaming transparency implemented
|
||||
|
||||
### Observability ✅
|
||||
- ✅ Structured logging (JSON format)
|
||||
- ✅ Benchmark analysis tools available
|
||||
- ✅ Tool recommendation accuracy measurable
|
||||
- ✅ Cross-session performance trends visible
|
||||
|
||||
### Architectural ✅
|
||||
- ✅ PydanticAI patterns followed (Toolsets, decorators, structured outputs)
|
||||
- ✅ Clean separation: registry vs. agents vs. tools
|
||||
- ✅ Two-tier abstraction working (summaries vs. details)
|
||||
- ✅ Future-proof for expert agents (Phase 4)
|
||||
|
||||
### Testing ✅
|
||||
- ✅ 223 tests passing (99.5% pass rate)
|
||||
- ✅ Integration tests for full flow
|
||||
- ✅ Streaming integration tests
|
||||
- ✅ 77.6% test coverage maintained
|
||||
|
||||
---
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Non-Streaming Request
|
||||
```python
|
||||
from src.responses.service import create_response_with_steward
|
||||
from src.responses.schemas import ResponseRequest
|
||||
|
||||
request = ResponseRequest(
|
||||
model="tatlock",
|
||||
input=[
|
||||
{"role": "user", "content": "What's sqrt(144)?"}
|
||||
],
|
||||
metadata={"conversation_id": "conv_123"}
|
||||
)
|
||||
|
||||
response = await create_response_with_steward(request)
|
||||
|
||||
# Response includes:
|
||||
# 1. Steward's analysis (reasoning output)
|
||||
# 2. Tatlock's answer (message output)
|
||||
```
|
||||
|
||||
### Streaming Request
|
||||
```python
|
||||
from src.responses.streaming import StreamingCoordinator
|
||||
|
||||
coordinator = StreamingCoordinator()
|
||||
|
||||
async for event in coordinator.stream_response_with_steward(request):
|
||||
if event.event == "response.reasoning_summary_text.delta":
|
||||
print(f"Steward: {event.delta}", end="")
|
||||
elif event.event == "response.output_text.delta":
|
||||
print(f"Tatlock: {event.delta}", end="")
|
||||
elif event.event == "response.done":
|
||||
print(f"\nFinal response: {event.response.id}")
|
||||
```
|
||||
|
||||
### Benchmark Analysis
|
||||
```bash
|
||||
# View Steward performance
|
||||
python scripts/benchmark_analysis.py --operation steward_analysis --hours 24
|
||||
|
||||
# Analyze tool accuracy
|
||||
python scripts/benchmark_analysis.py --tool-accuracy --days 7
|
||||
|
||||
# Get summary
|
||||
python scripts/benchmark_analysis.py --summary --hours 1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Future-Proofing for Phase 4
|
||||
|
||||
### Expert Agent Pattern (Ready to Use)
|
||||
|
||||
When adding The Librarian, The Developer, or other expert agents:
|
||||
|
||||
```
|
||||
src/agents/librarian/
|
||||
├── agent.py # Librarian PydanticAI agent
|
||||
├── tools.py # Research, wiki, knowledge tools
|
||||
├── toolset.py # PydanticAI toolset
|
||||
└── capability.py # Registry integration
|
||||
```
|
||||
|
||||
**Registration**:
|
||||
```python
|
||||
from src.core.household_registry import get_household_registry
|
||||
|
||||
registry = get_household_registry()
|
||||
registry.register(
|
||||
name="librarian",
|
||||
capability=LIBRARIAN_CAPABILITY,
|
||||
toolset=librarian_toolset,
|
||||
agent=librarian_agent # For delegation
|
||||
)
|
||||
```
|
||||
|
||||
**Delegation from Tatlock** (Phase 4):
|
||||
```python
|
||||
@tatlock_agent.tool
|
||||
async def consult_librarian(
|
||||
ctx: RunContext[None],
|
||||
research_query: str
|
||||
) -> str:
|
||||
"""Consult the Librarian for research assistance."""
|
||||
return await librarian_agent.run(research_query, usage=ctx.usage)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Lessons Learned
|
||||
|
||||
### What Went Well
|
||||
1. **PydanticAI Integration**: Native toolset patterns work beautifully
|
||||
2. **Two-Tier Architecture**: Clean separation between coordination and execution
|
||||
3. **Plain Text Approach**: More flexible than structured output for Steward
|
||||
4. **Test Coverage**: Comprehensive integration tests caught edge cases early
|
||||
5. **Streaming**: SSE events provide excellent real-time transparency
|
||||
|
||||
### Challenges Overcome
|
||||
1. **Schema vs. Agent OutputItems**: Fixed `_calculate_usage` to handle both types
|
||||
2. **Registry Initialization**: Added fixtures to ensure registry available in tests
|
||||
3. **Plain Text Parsing**: Keyword extraction works well but needs careful test mocking
|
||||
4. **Complexity Substring Matching**: "Complexity:" contains "complex" - fixed test mocks
|
||||
|
||||
### Optimizations
|
||||
1. **Single Model**: Using same Ollama model for both agents saves VRAM
|
||||
2. **Sequential Execution**: No parallel LLM calls needed (Steward → Tatlock)
|
||||
3. **Tool Scoping**: Fresh agent instances more reliable than runtime filtering
|
||||
4. **Benchmark Expiry**: 30-day TTL prevents Redis bloat
|
||||
|
||||
---
|
||||
|
||||
## Next Steps
|
||||
|
||||
### Immediate
|
||||
- Monitor Steward accuracy in production
|
||||
- Collect real-world benchmarks
|
||||
- Iterate on Steward prompt based on metrics
|
||||
|
||||
### Phase 3 (Optional)
|
||||
- Web search delegation to The Librarian
|
||||
- Enhanced research capabilities
|
||||
- Multi-source information synthesis
|
||||
|
||||
### Phase 4
|
||||
- Expert agent delegation (Librarian, Developer, etc.)
|
||||
- Dynamic agent selection based on request
|
||||
- Cross-agent collaboration patterns
|
||||
|
||||
---
|
||||
|
||||
## Conclusion
|
||||
|
||||
Phase 2 successfully delivers a production-ready two-tier architecture with The Steward managing intelligent request routing and tool scoping. The implementation is:
|
||||
|
||||
- ✅ **Complete**: All planned features delivered
|
||||
- ✅ **Tested**: 223 tests with 99.5% pass rate
|
||||
- ✅ **Observable**: Full logging and benchmarking
|
||||
- ✅ **Efficient**: Single model, minimal overhead
|
||||
- ✅ **Extensible**: Ready for expert agents in Phase 4
|
||||
|
||||
The Steward provides intelligent capability coordination while maintaining conversation context awareness, creating a foundation for scalable multi-agent collaboration in future phases.
|
||||
|
||||
**Phase 2 Status**: ✅ **COMPLETE**
|
||||
|
||||
---
|
||||
|
||||
**Document Version**: 1.0
|
||||
**Created**: 2025-12-07
|
||||
**Author**: Development Team
|
||||
**Reference**: [PHASE2_PLAN.md](PHASE2_PLAN.md)
|
||||
-865
@@ -1,865 +0,0 @@
|
||||
# Phase 2 Implementation Plan: The Steward
|
||||
|
||||
**Status**: Active Planning
|
||||
**Created**: 2025-12-07
|
||||
**Estimated Duration**: 4-5 weeks
|
||||
**Goal**: Implement first-tier request analysis and household capability coordination
|
||||
|
||||
---
|
||||
|
||||
## Executive Summary
|
||||
|
||||
Phase 2 introduces **The Steward** - a first-tier LLM agent that analyzes incoming requests, identifies relevant household capabilities, and provides focused recommendations to Tatlock (the Butler). This creates a two-tier architecture that prevents cognitive overload and enables efficient tool/agent coordination.
|
||||
|
||||
### Key Deliverables
|
||||
|
||||
1. **Household Registry**: Centralized capability catalog with PydanticAI Toolsets
|
||||
2. **Steward Agent**: Request analyzer with conversation context awareness
|
||||
3. **Tool Scoping**: Dynamic toolset creation based on recommendations
|
||||
4. **Observability**: Performance benchmarking and tool usage tracking via Redis
|
||||
5. **Integration**: Full Steward → Tatlock request flow
|
||||
|
||||
---
|
||||
|
||||
## Core Architectural Principles
|
||||
|
||||
### 1. Household-Based Organization
|
||||
- Each expert agent owns their tools in a domain directory
|
||||
- Tools organized as functional clusters around capabilities
|
||||
- Example: `src/agents/tatlock_core/` contains calculator, datetime, web search
|
||||
|
||||
### 2. Two-Tier Capability Abstraction
|
||||
- **Executive Summary**: High-level capabilities for Steward/Butler coordination
|
||||
- **Implementation Details**: Full tool specifications for household members
|
||||
- Steward sees summaries, household members see full details
|
||||
|
||||
### 3. PydanticAI Native Patterns
|
||||
- Use `FunctionToolset` and `CombinedToolset` for composition
|
||||
- Decorator-based tool registration (`@agent.tool`)
|
||||
- Structured outputs via Pydantic models
|
||||
- Agent delegation pattern for expert agents (Phase 4)
|
||||
|
||||
### 4. Separate Registries
|
||||
- **Household Registry**: Tools + capabilities (new in Phase 2)
|
||||
- **Model Registry**: Agents/models (existing from Phase 1)
|
||||
- Clean separation of concerns
|
||||
|
||||
### 5. Start Minimal
|
||||
- Only 3 core Tatlock tools initially: calculator, datetime, web search
|
||||
- No new tools until expert agents exist (Phase 4)
|
||||
- Prove the pattern before expanding
|
||||
|
||||
---
|
||||
|
||||
## Implementation Milestones
|
||||
|
||||
|
||||
### Milestone 1: Household Registry + Logging Infrastructure (Week 1-2)
|
||||
|
||||
#### Goal
|
||||
Create a registry system that aggregates household capabilities using PydanticAI Toolsets and establish observability infrastructure.
|
||||
|
||||
#### Tasks
|
||||
|
||||
**1.1 Create Household Registry Module**
|
||||
|
||||
Location: `src/core/household_registry.py`
|
||||
|
||||
```python
|
||||
from pydantic import BaseModel
|
||||
from pydantic_ai import FunctionToolset, CombinedToolset
|
||||
|
||||
class HouseholdCapability(BaseModel):
|
||||
"""Executive summary of a household member's capabilities."""
|
||||
name: str # "tatlock_core", "librarian", "developer"
|
||||
role: str # "Butler's Core Tools", "The Librarian"
|
||||
category: str # "core", "research", "technical"
|
||||
description: str # One-sentence description
|
||||
domains: list[str] # ["computation", "information", "datetime"]
|
||||
cost: str # "low", "medium", "high"
|
||||
requires_network: bool
|
||||
|
||||
class HouseholdMember(BaseModel):
|
||||
"""Full specification of a household member."""
|
||||
capability: HouseholdCapability
|
||||
toolset: FunctionToolset
|
||||
agent: Agent | None = None # For expert agents in Phase 4
|
||||
|
||||
class HouseholdRegistry:
|
||||
"""Registry of household capabilities and implementations."""
|
||||
|
||||
def __init__(self):
|
||||
self._members: dict[str, HouseholdMember] = {}
|
||||
|
||||
def register(
|
||||
self,
|
||||
name: str,
|
||||
capability: HouseholdCapability,
|
||||
toolset: FunctionToolset,
|
||||
agent: Agent | None = None
|
||||
):
|
||||
"""Register a household member."""
|
||||
self._members[name] = HouseholdMember(
|
||||
capability=capability,
|
||||
toolset=toolset,
|
||||
agent=agent
|
||||
)
|
||||
|
||||
def get_all_capabilities(self) -> list[HouseholdCapability]:
|
||||
"""Get executive summaries for Steward/Butler."""
|
||||
return [m.capability for m in self._members.values()]
|
||||
|
||||
def get_scoped_toolset(self, names: list[str]) -> CombinedToolset:
|
||||
"""Create combined toolset from recommended capabilities."""
|
||||
toolsets = [self._members[name].toolset for name in names]
|
||||
return CombinedToolset(toolsets)
|
||||
|
||||
# Global registry instance
|
||||
household_registry = HouseholdRegistry()
|
||||
```
|
||||
|
||||
|
||||
**1.2 Reorganize Tatlock Core Tools**
|
||||
|
||||
Create domain-based organization:
|
||||
|
||||
```
|
||||
src/agents/tatlock_core/
|
||||
├── __init__.py
|
||||
├── tools.py # Tool implementations (moved from src/agents/tools.py)
|
||||
├── toolset.py # PydanticAI toolset registration
|
||||
└── capability.py # Executive summary for registry
|
||||
```
|
||||
|
||||
**1.3 Create Logging Infrastructure**
|
||||
|
||||
Location: `src/core/logging_config.py`
|
||||
|
||||
- Structured logging with `structlog`
|
||||
- JSON format for machine parsing
|
||||
- Operation timing and metadata tracking
|
||||
- Context manager for automatic timing
|
||||
|
||||
**1.4 Create Redis Benchmark Storage**
|
||||
|
||||
Location: `src/core/benchmarks.py`
|
||||
|
||||
Features:
|
||||
- Performance benchmark recording (Steward analysis, tool calls)
|
||||
- Cross-session persistence via Redis
|
||||
- Time-series storage with automatic expiry (30 days)
|
||||
- Queryable metrics for analysis
|
||||
|
||||
Benchmark schema:
|
||||
```python
|
||||
class PerformanceBenchmark(BaseModel):
|
||||
timestamp: datetime
|
||||
operation: str # "steward_analysis", "tool_call"
|
||||
duration_seconds: float
|
||||
success: bool
|
||||
|
||||
# Steward-specific
|
||||
recommendation_count: Optional[int]
|
||||
confidence: Optional[float]
|
||||
|
||||
# Tool-specific
|
||||
tool_name: Optional[str]
|
||||
was_recommended: Optional[bool]
|
||||
was_actually_used: Optional[bool]
|
||||
|
||||
# Context
|
||||
conversation_id: Optional[str]
|
||||
metadata: dict
|
||||
```
|
||||
|
||||
**1.5 Testing**
|
||||
|
||||
- Test household registry registration and retrieval
|
||||
- Test Toolset composition
|
||||
- Test benchmark recording to Redis
|
||||
- Test structured logging output
|
||||
|
||||
#### Success Criteria
|
||||
- ✅ Household registry operational
|
||||
- ✅ Tatlock core tools organized in domain directory
|
||||
- ✅ Redis benchmarks working
|
||||
- ✅ Structured logging functional
|
||||
- ✅ Tests pass and maintain 80%+ coverage
|
||||
|
||||
---
|
||||
|
||||
|
||||
### Milestone 2: Minimal Steward Agent with Context Analysis (Week 3-4)
|
||||
|
||||
#### Goal
|
||||
Create a Steward agent that analyzes requests with full conversation context and recommends relevant household capabilities.
|
||||
|
||||
#### Tasks
|
||||
|
||||
**2.1 Create Steward Agent**
|
||||
|
||||
Location: `src/agents/steward/agent.py`
|
||||
|
||||
Structured output schema:
|
||||
```python
|
||||
class ConversationContext(BaseModel):
|
||||
"""Contextual information from conversation history."""
|
||||
has_previous_context: bool
|
||||
relevant_turns: list[int] # 0-indexed turn numbers
|
||||
context_summary: str # Summary for Butler
|
||||
|
||||
class StewardRecommendation(BaseModel):
|
||||
"""Structured recommendation from Steward analysis."""
|
||||
recommended_capabilities: list[str]
|
||||
reasoning: str
|
||||
estimated_complexity: Literal["simple", "moderate", "complex"]
|
||||
conversation_context: ConversationContext
|
||||
missing_capabilities: Optional[str] = None
|
||||
```
|
||||
|
||||
Key features:
|
||||
- Uses same model as Tatlock (`ollama:mistral-nemo`) for VRAM efficiency
|
||||
- Receives FULL conversation history
|
||||
- Queries household registry via tool
|
||||
- Conservative recommendations (avoid over-inclusion)
|
||||
- Explicit handling of missing capabilities
|
||||
|
||||
**2.2 Steward System Prompt**
|
||||
|
||||
Responsibilities:
|
||||
1. **Capability Recommendation**: Query registry, recommend only necessary tools
|
||||
2. **Conversation Analysis**: Identify references to previous topics
|
||||
3. **Complexity Assessment**: Simple/moderate/complex classification
|
||||
4. **Missing Capability Detection**: Suggest what's needed if no tools available
|
||||
|
||||
**2.3 Steward Service Layer with Logging**
|
||||
|
||||
Location: `src/agents/steward/service.py`
|
||||
|
||||
```python
|
||||
async def analyze_request(
|
||||
user_request: str,
|
||||
conversation_history: list[dict] # FULL conversation
|
||||
) -> StewardRecommendation:
|
||||
"""Analyze request with full conversation context."""
|
||||
|
||||
async with log_operation("steward_analysis", {...}) as log_ctx:
|
||||
result = await steward_agent.run(
|
||||
user_request,
|
||||
message_history=convert_to_pydantic_history(conversation_history),
|
||||
usage_limits=UsageLimits(request_limit=3)
|
||||
)
|
||||
|
||||
# Log and benchmark
|
||||
log_ctx["recommendation_count"] = len(result.data.recommended_capabilities)
|
||||
await benchmark_store.record(...)
|
||||
|
||||
return result.data
|
||||
```
|
||||
|
||||
**2.4 Testing**
|
||||
|
||||
Test scenarios:
|
||||
- Calculator request → recommends tatlock_core
|
||||
- Simple greeting → recommends []
|
||||
- Web search request → recommends tatlock_core
|
||||
- Request referencing previous turn → identifies context
|
||||
- Impossible request → returns missing_capabilities
|
||||
|
||||
#### Success Criteria
|
||||
- ✅ Steward queries household registry successfully
|
||||
- ✅ Produces structured recommendations
|
||||
- ✅ Analyzes full conversation context
|
||||
- ✅ Handles missing capabilities gracefully
|
||||
- ✅ Conservative recommendations (> 90% accuracy)
|
||||
- ✅ Benchmarks recorded to Redis
|
||||
|
||||
---
|
||||
|
||||
|
||||
### Milestone 3: Request Preprocessing & Tool Tracking (Week 5-6)
|
||||
|
||||
#### Goal
|
||||
Wire Steward into request flow, implement tool scoping, and track tool usage.
|
||||
|
||||
#### Tasks
|
||||
|
||||
**3.1 Create Preprocessing Pipeline**
|
||||
|
||||
Location: `src/core/preprocessing.py`
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class EnrichedRequest:
|
||||
"""Request enriched with Steward's analysis."""
|
||||
original_request: str
|
||||
steward_note: str # Formatted note for Tatlock
|
||||
scoped_toolset: CombinedToolset # Only recommended tools
|
||||
recommendation: StewardRecommendation
|
||||
steward_reasoning_output: str # For streaming to user
|
||||
|
||||
async def preprocess_request(
|
||||
user_request: str,
|
||||
conversation_history: list[dict] # FULL conversation
|
||||
) -> EnrichedRequest:
|
||||
"""Analyze via Steward and prepare scoped context."""
|
||||
# Call Steward with full conversation
|
||||
recommendation = await analyze_request(user_request, conversation_history)
|
||||
|
||||
# Format note to Tatlock (includes conversation context)
|
||||
steward_note = format_steward_note(recommendation)
|
||||
|
||||
# Create scoped toolset
|
||||
scoped_toolset = household_registry.get_scoped_toolset(
|
||||
recommendation.recommended_capabilities
|
||||
)
|
||||
|
||||
return EnrichedRequest(...)
|
||||
```
|
||||
|
||||
Note formatting:
|
||||
- Includes conversation context summary
|
||||
- Highlights missing capabilities if applicable
|
||||
- Provides complexity estimate
|
||||
|
||||
**3.2 Tool Usage Tracking**
|
||||
|
||||
Location: `src/core/tool_tracking.py`
|
||||
|
||||
```python
|
||||
class ToolCallTracker:
|
||||
"""Tracks tool calls for benchmarking."""
|
||||
|
||||
def __init__(self, recommended_tools: list[str]):
|
||||
self.recommended_tools = set(recommended_tools)
|
||||
self.actual_calls: dict[str, list[float]] = {}
|
||||
|
||||
async def track_call(self, tool_name: str, duration: float):
|
||||
"""Record a tool call with timing."""
|
||||
# Log if tool wasn't recommended
|
||||
if tool_name not in self.recommended_tools:
|
||||
logger.warning("tool_call_not_recommended", ...)
|
||||
|
||||
# Record benchmark to Redis
|
||||
await benchmark_store.record(...)
|
||||
|
||||
async def finalize(self):
|
||||
"""Log unused recommended tools."""
|
||||
unused = self.recommended_tools - set(self.actual_calls.keys())
|
||||
# Record benchmarks for unused tools
|
||||
```
|
||||
|
||||
**3.3 Integrate with Responses API**
|
||||
|
||||
Modify `src/responses/service.py`:
|
||||
```python
|
||||
async def generate_response(request: ResponseRequest) -> ResponseOutput:
|
||||
# Preprocess via Steward (with full conversation)
|
||||
enriched = await preprocess_request(
|
||||
user_message,
|
||||
conversation_history=request.input[:-1]
|
||||
)
|
||||
|
||||
# Run Tatlock with scoped tools and tracker
|
||||
result = await run_tatlock_with_scoped_tools(
|
||||
enriched.original_request,
|
||||
enriched.steward_note,
|
||||
enriched.scoped_toolset,
|
||||
enriched.recommendation.recommended_capabilities, # For tracking
|
||||
message_history,
|
||||
usage_tracker
|
||||
)
|
||||
|
||||
# Build response with Steward reasoning
|
||||
return build_response_with_steward_reasoning(...)
|
||||
```
|
||||
|
||||
**3.4 Update Tatlock Agent**
|
||||
|
||||
Location: `src/agents/tatlock.py`
|
||||
|
||||
```python
|
||||
async def run_tatlock_with_scoped_tools(
|
||||
user_request: str,
|
||||
steward_note: str,
|
||||
scoped_toolset: CombinedToolset,
|
||||
recommended_tools: list[str],
|
||||
message_history: list[dict],
|
||||
usage: UsageeLimits
|
||||
):
|
||||
# Initialize tracker
|
||||
tracker = ToolCallTracker(recommended_tools)
|
||||
|
||||
# Prepend Steward's note (invisible to user, visible to Tatlock)
|
||||
enriched_prompt = f"{steward_note}\n\n{user_request}"
|
||||
|
||||
# Run with ONLY scoped tools
|
||||
result = await tatlock_agent.run(
|
||||
enriched_prompt,
|
||||
message_history=convert_to_pydantic_history(message_history),
|
||||
toolsets=[scoped_toolset], # Tool scoping enforced
|
||||
deps=tracker, # For tracking
|
||||
usage=usage
|
||||
)
|
||||
|
||||
# Finalize tracking
|
||||
await tracker.finalize()
|
||||
|
||||
return result
|
||||
```
|
||||
|
||||
**3.5 Add Streaming Transparency**
|
||||
|
||||
Modify `src/responses/streaming.py`:
|
||||
- Stream Steward's reasoning first
|
||||
- Then stream Tatlock's response
|
||||
- Include conversation context notes
|
||||
- Format missing capabilities warnings
|
||||
|
||||
**3.6 Testing**
|
||||
|
||||
Integration tests:
|
||||
- Full Steward → Tatlock flow
|
||||
- Tool scoping enforcement (can't use non-recommended tools)
|
||||
- Tool usage tracking (recommended vs. actual)
|
||||
- Conversation context propagation
|
||||
- Missing capabilities handling
|
||||
|
||||
#### Success Criteria
|
||||
- ✅ Full request flow working (User → Steward → Tatlock)
|
||||
- ✅ Steward reasoning visible in output stream
|
||||
- ✅ Tool scoping enforced (only recommended tools available)
|
||||
- ✅ Tool usage tracked and logged to Redis
|
||||
- ✅ Conversation context passed through pipeline
|
||||
- ✅ Integration tests pass end-to-end
|
||||
|
||||
---
|
||||
|
||||
|
||||
### Milestone 4: Testing, Benchmarking & Refinement (Week 7)
|
||||
|
||||
#### Goal
|
||||
Validate the system, optimize performance, refine prompts, and establish monitoring.
|
||||
|
||||
#### Tasks
|
||||
|
||||
**4.1 Comprehensive Testing**
|
||||
|
||||
Test categories:
|
||||
- End-to-end integration tests (full request flow)
|
||||
- Performance benchmarks (latency targets)
|
||||
- Prompt refinement (recommendation accuracy)
|
||||
- Edge cases (errors, timeouts, missing capabilities)
|
||||
- Conversation context accuracy
|
||||
|
||||
**4.2 Performance Validation**
|
||||
|
||||
Targets:
|
||||
- Steward analysis: < 2 seconds
|
||||
- Total added latency: < 3 seconds
|
||||
- Model stays hot in VRAM (no reload delays)
|
||||
- Tool recommendation accuracy: > 90%
|
||||
|
||||
**4.3 Benchmark Analysis Tools**
|
||||
|
||||
Create `scripts/benchmark_analysis.py`:
|
||||
|
||||
```bash
|
||||
# View Steward performance over last 24 hours
|
||||
python scripts/benchmark_analysis.py --operation steward_analysis --hours 24
|
||||
|
||||
# Analyze tool recommendation accuracy
|
||||
python scripts/benchmark_analysis.py --tool-accuracy --days 7
|
||||
```
|
||||
|
||||
Metrics to track:
|
||||
- Average Steward analysis time
|
||||
- Recommendation count distribution
|
||||
- Tool accuracy (recommended & used, recommended but unused, not recommended but used)
|
||||
- Recommendation precision percentage
|
||||
|
||||
**4.4 Prompt Engineering**
|
||||
|
||||
Iterate on Steward system prompt:
|
||||
- Test with diverse request types
|
||||
- Tune conservativeness (balance false positives/negatives)
|
||||
- Validate conversation context analysis
|
||||
- Test missing capability detection
|
||||
|
||||
**4.5 Documentation**
|
||||
|
||||
Update documentation:
|
||||
- README.md: Steward explanation and examples
|
||||
- AGENTS.md: Household registration pattern
|
||||
- IMPLEMENTATION_ROADMAP.md: Mark Phase 2 complete
|
||||
- Add benchmark analysis guide
|
||||
|
||||
#### Success Criteria
|
||||
- ✅ < 3 seconds added latency for Steward analysis
|
||||
- ✅ > 90% recommendation accuracy (manual evaluation)
|
||||
- ✅ All integration tests pass
|
||||
- ✅ Benchmark tools functional
|
||||
- ✅ Documentation complete and accurate
|
||||
- ✅ Ready for Phase 3/4 (expert agents)
|
||||
|
||||
---
|
||||
|
||||
## Architecture Diagram
|
||||
|
||||
```
|
||||
User Request
|
||||
↓
|
||||
Orchestrator (FastAPI)
|
||||
↓
|
||||
Preprocessing Pipeline
|
||||
├─→ Steward Agent
|
||||
│ ├─ Receives: FULL conversation history
|
||||
│ ├─ Analyzes: Context, references, requirements
|
||||
│ ├─ Queries: Household registry (capabilities)
|
||||
│ ├─ Outputs: StewardRecommendation
|
||||
│ │ ├─ recommended_capabilities: list[str]
|
||||
│ │ ├─ conversation_context: ConversationContext
|
||||
│ │ ├─ missing_capabilities: str | None
|
||||
│ │ └─ reasoning: str
|
||||
│ └─ Logs: Performance benchmarks → Redis
|
||||
│
|
||||
├─→ Create Scoped Toolset
|
||||
│ └─ CombinedToolset from recommended capabilities
|
||||
│
|
||||
└─→ Format Steward Note
|
||||
└─ Includes conversation context for Tatlock
|
||||
↓
|
||||
Tatlock Agent (with scoped tools)
|
||||
├─ Receives: Enriched request + Steward note
|
||||
├─ Has access to: ONLY recommended tools
|
||||
├─ Tool calls tracked: ToolCallTracker
|
||||
└─ Logs: Tool usage benchmarks → Redis
|
||||
↓
|
||||
Response to User
|
||||
├─ Steward's reasoning (streamed first)
|
||||
└─ Tatlock's response (streamed second)
|
||||
|
||||
Background:
|
||||
└─ Redis: Performance benchmarks, tool usage analysis
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Design Decisions Summary
|
||||
|
||||
### 1. Logging & Performance Benchmarks
|
||||
**Decision**: Full observability with Redis-backed benchmark storage
|
||||
|
||||
**Rationale**:
|
||||
- Track Steward recommendations vs. Tatlock's actual tool usage
|
||||
- Measure performance metrics (latency, token usage)
|
||||
- Cross-session analysis for optimization
|
||||
- Identify recommendation accuracy over time
|
||||
|
||||
### 2. Steward Fallback Behavior
|
||||
**Decision**: Explicit missing capability communication
|
||||
|
||||
**Rationale**:
|
||||
- No suitable tools → Steward states "missing capabilities" with description
|
||||
- Can suggest what type of tool would be helpful
|
||||
- Code errors → standard exception handlers (don't suppress real errors)
|
||||
- Better UX than silent failures or defaulting to all tools
|
||||
|
||||
### 3. Conversation History for Steward
|
||||
**Decision**: Steward sees FULL conversation, not just current turn
|
||||
|
||||
**Rationale**:
|
||||
- Can identify references to previous topics
|
||||
- Provides contextual notes to Butler
|
||||
- "Two sets of eyes" on conversation
|
||||
- Example: "User mentioned Python debugging in turn 3, relevant details: async code"
|
||||
|
||||
### 4. Registry Pattern
|
||||
**Decision**: Separate Household Registry from Model Registry
|
||||
|
||||
**Rationale**:
|
||||
- Tools belong to household members, not models
|
||||
- Clean separation of concerns
|
||||
- Executive summaries for coordination, details for execution
|
||||
|
||||
### 5. Tool Composition
|
||||
**Decision**: PydanticAI FunctionToolset + CombinedToolset
|
||||
|
||||
**Rationale**:
|
||||
- Native PydanticAI pattern
|
||||
- Clean composition and filtering
|
||||
- Dynamic scoping per request
|
||||
|
||||
### 6. Tool Scoping
|
||||
**Decision**: Compile-time scoping via toolset creation
|
||||
|
||||
**Rationale**:
|
||||
- Tools not even visible to LLM
|
||||
- Cleaner than runtime permission checks
|
||||
- Enforced at PydanticAI level
|
||||
|
||||
### 7. Organization
|
||||
**Decision**: Domain-based household directories
|
||||
|
||||
**Rationale**:
|
||||
- Each household member owns their tools
|
||||
- Clear bounded contexts
|
||||
- Example: `src/agents/tatlock_core/`, `src/agents/librarian/` (future)
|
||||
|
||||
---
|
||||
|
||||
## Infrastructure Requirements
|
||||
|
||||
### Redis Setup
|
||||
|
||||
Development (quick start):
|
||||
```bash
|
||||
# Docker (recommended)
|
||||
docker run -d -p 6379:6379 --name tatlock-redis redis:7-alpine
|
||||
|
||||
# Or local installation
|
||||
# macOS: brew install redis && brew services start redis
|
||||
# Linux: sudo apt install redis-server && sudo systemctl start redis
|
||||
```
|
||||
|
||||
Production (docker-compose.yml):
|
||||
```yaml
|
||||
services:
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
ports:
|
||||
- "6379:6379"
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
command: redis-server --appendonly yes
|
||||
|
||||
volumes:
|
||||
redis_data:
|
||||
```
|
||||
|
||||
### Dependencies Update
|
||||
|
||||
Add to `requirements.txt`:
|
||||
```txt
|
||||
redis[hiredis]>=5.0.0,<6.0.0
|
||||
structlog>=24.1.0,<25.0.0
|
||||
```
|
||||
|
||||
### Configuration
|
||||
|
||||
Add to `.env`:
|
||||
```env
|
||||
# Redis Configuration
|
||||
REDIS_URL=redis://localhost:6379/1
|
||||
|
||||
# Logging
|
||||
LOG_LEVEL=INFO
|
||||
LOG_FORMAT=json
|
||||
ENABLE_BENCHMARKS=true
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Timeline
|
||||
|
||||
**Week 1-2**: Household Registry + Logging Infrastructure
|
||||
- Household registry with Toolsets
|
||||
- Structured logging with structlog
|
||||
- Redis benchmark storage
|
||||
- Tatlock core reorganization
|
||||
- Tests: Registry + benchmarking
|
||||
|
||||
**Week 3-4**: Steward Agent with Context Analysis
|
||||
- Steward agent with conversation context
|
||||
- ConversationContext in recommendations
|
||||
- Missing capabilities handling
|
||||
- Tests: Context analysis, missing capabilities
|
||||
|
||||
**Week 5-6**: Integration + Tool Tracking
|
||||
- Request preprocessing with full conversation
|
||||
- Tool usage tracking middleware
|
||||
- Scoped toolset creation
|
||||
- Streaming transparency
|
||||
- Tests: Full flow + tool tracking
|
||||
|
||||
**Week 7**: Testing, Benchmarking & Refinement
|
||||
- End-to-end integration tests
|
||||
- Benchmark analysis tools
|
||||
- Prompt refinement
|
||||
- Performance validation
|
||||
- Documentation updates
|
||||
|
||||
**Total: 4-5 weeks** (core implementation complete in 6 weeks, polish in week 7)
|
||||
|
||||
---
|
||||
|
||||
## Success Metrics
|
||||
|
||||
### Technical
|
||||
- ✅ Household registry operational with executive summaries
|
||||
- ✅ Steward produces accurate recommendations (> 90%)
|
||||
- ✅ Steward analyzes full conversation context
|
||||
- ✅ Tool scoping enforced (Tatlock can't use non-recommended tools)
|
||||
- ✅ Model efficiency preserved (no reload delays)
|
||||
- ✅ Added latency < 3 seconds
|
||||
- ✅ Performance benchmarks recorded to Redis
|
||||
- ✅ Tool usage tracking (recommended vs. actual)
|
||||
|
||||
### Observability
|
||||
- ✅ Structured logging (JSON format)
|
||||
- ✅ Benchmark analysis tools available
|
||||
- ✅ Tool recommendation accuracy measurable
|
||||
- ✅ Cross-session performance trends visible
|
||||
|
||||
### Error Handling
|
||||
- ✅ Missing capabilities explicitly communicated
|
||||
- ✅ Steward can guide user toward needed resources
|
||||
- ✅ Code errors properly surfaced (not suppressed)
|
||||
|
||||
### Architectural
|
||||
- ✅ PydanticAI patterns followed (Toolsets, decorators, structured outputs)
|
||||
- ✅ Clean separation: registry vs. agents vs. tools
|
||||
- ✅ Two-tier abstraction working (summaries vs. details)
|
||||
- ✅ Future-proof for expert agents (Phase 4)
|
||||
|
||||
### Testing
|
||||
- ✅ Maintain 80%+ test coverage
|
||||
- ✅ Integration tests for full flow
|
||||
- ✅ Performance benchmarks established
|
||||
|
||||
---
|
||||
|
||||
## Future-Proofing for Phase 4
|
||||
|
||||
### Expert Agent Pattern (Template)
|
||||
|
||||
When adding The Librarian, The Developer, etc., follow this structure:
|
||||
|
||||
```
|
||||
src/agents/librarian/
|
||||
├── __init__.py
|
||||
├── agent.py # Librarian PydanticAI agent
|
||||
├── tools.py # Librarian-specific tools (wiki, research, etc.)
|
||||
├── toolset.py # PydanticAI toolset creation
|
||||
└── capability.py # Executive summary for registry
|
||||
```
|
||||
|
||||
Example capability registration:
|
||||
```python
|
||||
# capability.py
|
||||
LIBRARIAN_CAPABILITY = HouseholdCapability(
|
||||
name="librarian",
|
||||
role="The Librarian",
|
||||
category="research",
|
||||
description="Research assistance, knowledge management, and information synthesis",
|
||||
domains=["research", "knowledge_base", "documentation"],
|
||||
cost="medium",
|
||||
requires_network=True
|
||||
)
|
||||
|
||||
def register_librarian():
|
||||
household_registry.register(
|
||||
name="librarian",
|
||||
capability=LIBRARIAN_CAPABILITY,
|
||||
toolset=librarian_toolset,
|
||||
agent=librarian_agent # Expert agent for delegation
|
||||
)
|
||||
```
|
||||
|
||||
Tatlock delegation pattern (Phase 4):
|
||||
```python
|
||||
@tatlock_agent.tool
|
||||
async def consult_librarian(
|
||||
ctx: RunContext[None],
|
||||
research_query: str
|
||||
) -> str:
|
||||
"""Consult the Librarian for research assistance."""
|
||||
from src.agents.librarian.agent import librarian_agent
|
||||
|
||||
result = await librarian_agent.run(
|
||||
research_query,
|
||||
usage=ctx.usage # Aggregate usage
|
||||
)
|
||||
return result.data
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Risk Mitigation
|
||||
|
||||
### Identified Risks
|
||||
|
||||
1. **Steward recommendations too broad**
|
||||
- Mitigation: Conservative prompt engineering, benchmark tracking, iterate based on false positives
|
||||
|
||||
2. **Added latency unacceptable**
|
||||
- Mitigation: Stream Steward reasoning for transparency, optimize prompt, use same base model
|
||||
|
||||
3. **Tool registry becomes unwieldy**
|
||||
- Mitigation: Good categorization, semantic search (future), regular pruning
|
||||
|
||||
4. **Model VRAM competition**
|
||||
- Mitigation: Use same base model for Steward and Tatlock, sequential calls
|
||||
|
||||
5. **Redis dependency**
|
||||
- Mitigation: Make benchmarking optional, graceful degradation if Redis unavailable
|
||||
|
||||
---
|
||||
|
||||
## Open Questions - RESOLVED
|
||||
|
||||
All major design questions have been resolved. See "Design Decisions Summary" section above.
|
||||
|
||||
---
|
||||
|
||||
## Next Steps
|
||||
|
||||
### Immediate (Today/This Week)
|
||||
1. Set up Redis (Docker or local)
|
||||
2. Create `src/core/logging_config.py` with structured logging
|
||||
3. Create `src/core/benchmarks.py` with Redis storage
|
||||
4. Add `redis` and `structlog` to requirements.txt
|
||||
5. Create household registry skeleton
|
||||
|
||||
### Week 1-2
|
||||
1. Complete household registry with Toolset integration
|
||||
2. Reorganize Tatlock core tools into domain directory
|
||||
3. Implement logging infrastructure
|
||||
4. Write tests for registry + benchmarking
|
||||
|
||||
### Week 3-4
|
||||
1. Create Steward agent with conversation context
|
||||
2. Implement missing capabilities handling
|
||||
3. Test context analysis accuracy
|
||||
4. Iterate on system prompt
|
||||
|
||||
### Week 5-6
|
||||
1. Build preprocessing pipeline
|
||||
2. Integrate with Responses API
|
||||
3. Implement tool tracking
|
||||
4. Add streaming transparency
|
||||
|
||||
### Week 7
|
||||
1. End-to-end testing
|
||||
2. Benchmark analysis
|
||||
3. Performance optimization
|
||||
4. Documentation updates
|
||||
|
||||
---
|
||||
|
||||
## Document Status
|
||||
|
||||
**Status**: Active Planning Document
|
||||
**Created**: 2025-12-07
|
||||
**Last Updated**: 2025-12-07
|
||||
**Version**: 1.0
|
||||
**Next Review**: After Milestone 1 completion
|
||||
|
||||
---
|
||||
|
||||
**Reference Documents**:
|
||||
- [PHILOSOPHY.md](PHILOSOPHY.md) - System vision and architecture
|
||||
- [IMPLEMENTATION_ROADMAP.md](IMPLEMENTATION_ROADMAP.md) - Full project roadmap
|
||||
- [AGENTS.md](AGENTS.md) - Agent development guidelines
|
||||
- [README.md](README.md) - User documentation
|
||||
|
||||
@@ -6,12 +6,25 @@ A privacy-first, offline-capable personal assistant system that coordinates spec
|
||||
|
||||
## Current Status
|
||||
|
||||
- ✅ **Production-ready testing API** with OpenAI Responses API format
|
||||
- ✅ **Production-ready API** with OpenAI Responses API format
|
||||
- ✅ **Open WebUI integration** with reasoning bubbles (`<think>` tags)
|
||||
- ✅ **Conversation history** with auto-generated IDs and context management
|
||||
- ✅ **Tatlock PydanticAI Agent** - Real LLM integration with Ollama + permanent tools
|
||||
- ✅ **Permanent Tools** - Calculator, date/time toolkit, web search (SearXNG)
|
||||
- ✅ **Comprehensive testing** - 131 tests, 81.78% coverage
|
||||
- ✅ **Two-tier architecture** - The Steward analyzes requests, Tatlock coordinates execution
|
||||
- ✅ **Multi-agent coordination** - Expert household staff for specialized tasks
|
||||
- ✅ **Memory system** - User profile, preferences, and semantic recall
|
||||
- ✅ **Comprehensive testing** - 399 tests with good coverage
|
||||
|
||||
### The Household Staff
|
||||
|
||||
| Agent | Role | Status |
|
||||
|-------|------|--------|
|
||||
| **Tatlock** | The Butler - Primary interface with witty personality | ✅ Active |
|
||||
| **The Steward** | Request analysis and capability recommendation | ✅ Active |
|
||||
| **The Librarian** | Research, wiki management, knowledge synthesis | ✅ Active |
|
||||
| **The Biographer** | User memory - profiles, preferences, facts | ✅ Active |
|
||||
| **The Developer** | Code assistance, debugging, architecture | 🔜 Planned |
|
||||
| **The Secretary** | Scheduling, calendars, reminders | 🔜 Planned |
|
||||
| **The Handyman** | System administration, monitoring | 🔜 Planned |
|
||||
| **The Housekeeper** | Home automation (Home Assistant) | 🔜 Planned |
|
||||
|
||||
## Features
|
||||
|
||||
@@ -45,24 +58,27 @@ A privacy-first, offline-capable personal assistant system that coordinates spec
|
||||
- Error triggers for testing (rate_limit, context_overflow)
|
||||
|
||||
- **Tatlock**: Real PydanticAI agent with butler personality
|
||||
- **LLM Backend**: Ollama (mistral-nemo:latest)
|
||||
- **LLM Backend**: Ollama (mistral-nemo:latest by default)
|
||||
- **Personality**: Witty British butler, research-oriented
|
||||
- **Permanent Tools**:
|
||||
- **Calculator**: Safe mathematical expression evaluation (arithmetic, algebra, trigonometry, logarithms)
|
||||
- **Date/Time Toolkit**: Current time, relative dates ("1 week ago"), time differences
|
||||
- **Core Tools**:
|
||||
- **Calculator**: Safe mathematical expression evaluation
|
||||
- **Date/Time Toolkit**: Current time, relative dates, time differences
|
||||
- **Web Search**: Privacy-preserving search via SearXNG
|
||||
- **Capabilities**: Streaming, reasoning, tool calling
|
||||
- **Phase**: Phase 1 - Basic Integration (full household coordination coming in future phases)
|
||||
- **Household Coordination**:
|
||||
- **The Steward**: Analyzes requests and recommends capabilities
|
||||
- **The Librarian**: Research via library-desk HybridRAG + wiki
|
||||
- **The Biographer**: User memory and preference management
|
||||
- **Capabilities**: Streaming, reasoning, tool calling, multi-agent delegation
|
||||
|
||||
## Requirements
|
||||
|
||||
- Python 3.12+ (Python 3.12.11 recommended)
|
||||
- **Ollama** (for Tatlock agent): Running locally or network-accessible
|
||||
- Download: https://ollama.ai/
|
||||
- Model: `ollama pull mistral-nemo:latest`
|
||||
- **SearXNG** (for web search tool): Optional but recommended
|
||||
- Docker: `docker run -d -p 8087:8080 searxng/searxng`
|
||||
- Or use public instance (less private)
|
||||
- **External Services** (must be running separately):
|
||||
- **Ollama**: LLM inference (mistral-nemo:latest, nomic-embed-text)
|
||||
- **Redis**: Caching and session memory
|
||||
- **Qdrant**: Vector storage for The Biographer's memory
|
||||
- **SearXNG**: Web search (optional)
|
||||
- **library-desk**: Research API for The Librarian (optional)
|
||||
|
||||
## Quick Start
|
||||
|
||||
@@ -251,15 +267,18 @@ Interactive documentation available at:
|
||||
# Run all tests
|
||||
pytest
|
||||
|
||||
# Run unit tests only (no external services needed)
|
||||
pytest --ignore=tests/e2e --ignore=tests/integration
|
||||
|
||||
# Run with coverage
|
||||
pytest --cov=src --cov-report=term-missing
|
||||
|
||||
# Current: 131 tests, 81.78% coverage
|
||||
# Current: ~400 tests
|
||||
```
|
||||
|
||||
**Test Categories:**
|
||||
- Unit tests: Agent tools, streaming, schemas
|
||||
- Integration tests: Full API stack with real Ollama calls
|
||||
- Unit tests: Agent tools, capabilities, schemas, memory service
|
||||
- Integration tests: Full API stack with real Ollama
|
||||
- End-to-end tests: Chat completions, responses API
|
||||
|
||||
## Deployment
|
||||
@@ -291,9 +310,25 @@ API_PORT=8000
|
||||
# Ollama Configuration
|
||||
OLLAMA_HOST=http://localhost:11434
|
||||
OLLAMA_DEFAULT_MODEL=mistral-nemo:latest
|
||||
OLLAMA_EMBEDDING_MODEL=nomic-embed-text
|
||||
OLLAMA_TIMEOUT=120
|
||||
|
||||
# SearXNG Configuration (for web search tool)
|
||||
# Redis Configuration
|
||||
REDIS_HOST=localhost
|
||||
REDIS_PORT=6379
|
||||
REDIS_MEMORY_DB=2
|
||||
REDIS_MEMORY_TTL_HOURS=24
|
||||
|
||||
# Qdrant Configuration (for memory)
|
||||
QDRANT_HOST=localhost
|
||||
QDRANT_PORT=6333
|
||||
QDRANT_EMBEDDING_DIM=768
|
||||
|
||||
# Library-desk Configuration (for The Librarian)
|
||||
LIBRARY_DESK_HOST=http://localhost:8089
|
||||
LIBRARY_DESK_TIMEOUT=60
|
||||
|
||||
# SearXNG Configuration (for web search)
|
||||
SEARXNG_HOST=http://localhost:8087
|
||||
SEARXNG_TIMEOUT=30
|
||||
|
||||
@@ -339,23 +374,32 @@ See `.env.example` for full configuration options.
|
||||
```
|
||||
tatlock/
|
||||
├── src/
|
||||
│ ├── agents/ # Agent interface and implementations
|
||||
│ │ ├── base.py # AgentInterface abstract class
|
||||
│ │ ├── lorem_tester.py # Mock agent for testing
|
||||
│ │ ├── tatlock.py # Real PydanticAI butler agent
|
||||
│ │ ├── tools.py # Permanent tools (calculator, date/time, search)
|
||||
│ │ └── registry.py # Model registry
|
||||
│ ├── responses/ # Responses API (primary endpoint)
|
||||
│ ├── chat/ # Chat Completions wrapper
|
||||
│ ├── models/ # Models listing
|
||||
│ ├── core/ # Shared utilities and config
|
||||
│ └── main.py # Application entry point
|
||||
├── tests/ # Comprehensive test suite (131 tests)
|
||||
├── AGENTS.md # LLM agent development guidelines
|
||||
├── PHILOSOPHY.md # System vision and architecture
|
||||
├── IMPLEMENTATION_ROADMAP.md # Development phases
|
||||
├── CHANGELOG.md # Version history
|
||||
└── README.md # This file
|
||||
│ ├── agents/ # Agent implementations
|
||||
│ │ ├── biographer/ # The Biographer - memory management
|
||||
│ │ ├── librarian/ # The Librarian - research & wiki
|
||||
│ │ ├── steward/ # The Steward - request analysis
|
||||
│ │ ├── tatlock_core/ # Core butler tools
|
||||
│ │ ├── tatlock.py # Tatlock PydanticAI agent
|
||||
│ │ ├── coordination.py # Multi-agent coordination
|
||||
│ │ ├── delegation.py # Expert delegation wrappers
|
||||
│ │ └── protocol.py # Agent communication protocol
|
||||
│ ├── responses/ # Responses API (primary endpoint)
|
||||
│ ├── chat/ # Chat Completions wrapper
|
||||
│ ├── models/ # Models listing
|
||||
│ ├── core/ # Shared infrastructure
|
||||
│ │ ├── config.py # Configuration management
|
||||
│ │ ├── context.py # Request context (ContextVar)
|
||||
│ │ ├── memory_service.py # Direct memory access
|
||||
│ │ ├── memory_cache.py # Redis session cache
|
||||
│ │ ├── embeddings.py # Ollama embedding client
|
||||
│ │ ├── qdrant.py # Vector database client
|
||||
│ │ └── multi_tenancy.py # User isolation utilities
|
||||
│ └── main.py # Application entry point
|
||||
├── tests/ # Comprehensive test suite
|
||||
├── PHILOSOPHY.md # System vision and architecture
|
||||
├── IMPLEMENTATION_ROADMAP.md # Development phases
|
||||
├── CHANGELOG.md # Version history
|
||||
└── README.md # This file
|
||||
```
|
||||
|
||||
## Development
|
||||
@@ -388,8 +432,8 @@ For LLM agent development guidelines and architectural decisions, see [AGENTS.md
|
||||
|
||||
## Version
|
||||
|
||||
Current version: **0.2.5** - Phase 2: The Steward (Two-Tier Architecture)
|
||||
Current version: **1.2.0** - Phase F: Memory System (The Biographer)
|
||||
|
||||
---
|
||||
|
||||
**Note**: This is a production-ready testing API with mock responses. The architecture is designed for easy integration with real LLM backends (PydanticAI, Ollama, OpenAI, etc.).
|
||||
**Note**: Tatlock is a production-ready homelab butler. All household staff use PydanticAI with Ollama for local LLM inference.
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "tatlock"
|
||||
version = "0.2.5"
|
||||
version = "1.2.0"
|
||||
description = "OpenAI-compatible API with Ollama backend"
|
||||
requires-python = ">=3.12"
|
||||
dependencies = []
|
||||
|
||||
@@ -41,6 +41,10 @@ starlette>=0.45,<0.46
|
||||
# hiredis: C parser for better performance
|
||||
redis[hiredis]>=5.2,<6.0
|
||||
|
||||
# Qdrant vector database client for memory storage
|
||||
# Latest: 1.12.1 (Dec 2025) - No known CVEs
|
||||
qdrant-client>=1.12,<2.0
|
||||
|
||||
# Structured logging for observability
|
||||
# Latest: 24.4.0 (Aug 22, 2024) - No known CVEs
|
||||
structlog>=24.1,<25.0
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
"""
|
||||
The Biographer - Expert for recording and recalling the user's story.
|
||||
|
||||
The Biographer serves as the household's memory keeper, responsible for:
|
||||
- Recording and recalling facts about the user's life
|
||||
- Storing personal information, preferences, and insights
|
||||
- Answering questions like "What car do I drive?", "Where do I work?"
|
||||
- Managing what the household knows and remembers
|
||||
|
||||
For direct key-based lookups (location, timezone, preferences),
|
||||
use the memory_service instead - it's faster and doesn't require LLM.
|
||||
The Biographer handles semantic, fuzzy queries.
|
||||
"""
|
||||
from src.agents.biographer.agent import (
|
||||
get_biographer_agent,
|
||||
run_biographer,
|
||||
run_biographer_stream,
|
||||
)
|
||||
from src.agents.biographer.capability import (
|
||||
BIOGRAPHER_CAPABILITY,
|
||||
get_biographer_capability,
|
||||
register_biographer,
|
||||
unregister_biographer,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"BIOGRAPHER_CAPABILITY",
|
||||
"get_biographer_capability",
|
||||
"get_biographer_agent",
|
||||
"register_biographer",
|
||||
"unregister_biographer",
|
||||
"run_biographer",
|
||||
"run_biographer_stream",
|
||||
]
|
||||
@@ -0,0 +1,273 @@
|
||||
"""
|
||||
The Biographer - Expert for recording and recalling the user's story.
|
||||
|
||||
A PydanticAI agent that serves as the household's memory keeper:
|
||||
- Records facts about the user's life, work, and preferences
|
||||
- Recalls information semantically ("What car do I drive?")
|
||||
- Manages user profile and preferences
|
||||
- Forgets information when requested
|
||||
"""
|
||||
from typing import Any, Optional
|
||||
|
||||
from pydantic_ai import Agent
|
||||
|
||||
from src.agents.biographer.tools import (
|
||||
forget_memory,
|
||||
list_memories,
|
||||
recall_semantic,
|
||||
store_insight,
|
||||
update_preference,
|
||||
update_profile,
|
||||
)
|
||||
from src.core.config import config
|
||||
from src.core.logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
# The Biographer's system prompt
|
||||
BIOGRAPHER_SYSTEM_PROMPT = """You are The Biographer, the household's memory keeper in the Tatlock estate.
|
||||
|
||||
Your role is to record, recall, and manage the story of the user's life:
|
||||
- Personal facts (vehicle, pets, family members, hobbies, interests)
|
||||
- Life details (employer, occupation, significant events)
|
||||
- Profile information (name, location, timezone)
|
||||
- Preferences (units, theme, communication style)
|
||||
|
||||
## Your Character
|
||||
|
||||
You are a discreet and attentive chronicler. Like a personal biographer who has been
|
||||
with the household for years, you:
|
||||
- Listen carefully and remember important details
|
||||
- Recall information accurately when asked
|
||||
- Never gossip or volunteer unnecessary information
|
||||
- Respect privacy absolutely
|
||||
- Acknowledge when you don't know something rather than guessing
|
||||
|
||||
## Your Tools
|
||||
|
||||
### Recalling the Story
|
||||
- **recall_semantic**: Your primary tool for answering questions about the user
|
||||
- "What car do I drive?" → searches for car-related memories
|
||||
- "Where do I work?" → finds employment information
|
||||
- Finds relevant memories even without exact keywords
|
||||
- **list_memories**: Browse all recorded memories of a type
|
||||
- Use when user asks "What do you know about me?"
|
||||
- Shows everything you've recorded
|
||||
|
||||
### Recording New Details
|
||||
- **store_insight**: Record new facts from conversation
|
||||
- User says "My car is a Tesla" → store_insight("car", "Tesla Model 3")
|
||||
- User says "I work at Acme" → store_insight("employer", "Acme Corp")
|
||||
- Use for facts that don't fit standard profile fields
|
||||
- **update_profile**: Update core biographical fields
|
||||
- name, location, timezone only
|
||||
- "I live in Amsterdam" → update_profile("location", "Amsterdam")
|
||||
- **update_preference**: Record user preferences
|
||||
- temperature_unit, distance_unit, theme, etc.
|
||||
- "Use Celsius please" → update_preference("temperature_unit", "celsius")
|
||||
|
||||
### Managing Records
|
||||
- **forget_memory**: Remove specific records
|
||||
- User asks to forget something → honor immediately
|
||||
- Information becomes outdated → remove it
|
||||
|
||||
## Guidelines
|
||||
|
||||
### What to Record
|
||||
- Explicit statements: "I drive a Tesla", "My wife is Sarah"
|
||||
- Corrections: "Actually, I moved to Berlin"
|
||||
- Preferences: "I prefer metric units"
|
||||
|
||||
### What NOT to Record
|
||||
- Sensitive data: passwords, financial details, health information
|
||||
- Temporary information: "I'm tired today"
|
||||
- Speculation or assumptions
|
||||
|
||||
### Responding to Tatlock
|
||||
Your responses go to Tatlock (the butler) who synthesizes the final answer. Be:
|
||||
- Direct and factual
|
||||
- Clear about what you found or didn't find
|
||||
- Structured for easy integration with other responses
|
||||
|
||||
When you don't have information:
|
||||
"I have no record of the user's [topic]. Would you like me to record this information?"
|
||||
|
||||
When recalling:
|
||||
"According to my records, [information]. This was recorded [source/when if available]."
|
||||
"""
|
||||
|
||||
# Lazy initialization to avoid connection issues during imports
|
||||
_biographer_agent: Optional[Agent[None, str]] = None
|
||||
|
||||
|
||||
def _create_biographer_agent() -> Agent[None, str]:
|
||||
"""Create The Biographer PydanticAI agent."""
|
||||
# Import required classes for Ollama configuration
|
||||
from pydantic_ai.models.openai import OpenAIChatModel
|
||||
from pydantic_ai.providers.ollama import OllamaProvider
|
||||
|
||||
# PydanticAI expects Ollama base URL to end with /v1
|
||||
clean_host = str(config.OLLAMA_HOST).rstrip('/')
|
||||
base_url = f"{clean_host}/v1"
|
||||
|
||||
# Create Ollama model with provider
|
||||
model = OpenAIChatModel(
|
||||
model_name=config.OLLAMA_DEFAULT_MODEL,
|
||||
provider=OllamaProvider(base_url=base_url)
|
||||
)
|
||||
|
||||
agent: Agent[None, str] = Agent(
|
||||
model=model,
|
||||
system_prompt=BIOGRAPHER_SYSTEM_PROMPT,
|
||||
retries=2,
|
||||
)
|
||||
|
||||
# Register recall tools
|
||||
agent.tool_plain(recall_semantic)
|
||||
agent.tool_plain(list_memories)
|
||||
|
||||
# Register recording tools
|
||||
agent.tool_plain(store_insight)
|
||||
agent.tool_plain(update_profile)
|
||||
agent.tool_plain(update_preference)
|
||||
|
||||
# Register management tools
|
||||
agent.tool_plain(forget_memory)
|
||||
|
||||
logger.info(
|
||||
"biographer_agent_created",
|
||||
model=config.OLLAMA_DEFAULT_MODEL,
|
||||
tool_count=6,
|
||||
)
|
||||
|
||||
return agent
|
||||
|
||||
|
||||
def get_biographer_agent() -> Agent[None, str]:
|
||||
"""
|
||||
Get The Biographer agent instance (lazy initialization).
|
||||
|
||||
Returns:
|
||||
PydanticAI Agent configured for memory tasks
|
||||
"""
|
||||
global _biographer_agent
|
||||
if _biographer_agent is None:
|
||||
_biographer_agent = _create_biographer_agent()
|
||||
return _biographer_agent
|
||||
|
||||
|
||||
async def run_biographer(
|
||||
task: str,
|
||||
context: str = "",
|
||||
message_history: Optional[list[Any]] = None,
|
||||
) -> str:
|
||||
"""
|
||||
Execute a memory task with The Biographer.
|
||||
|
||||
This is the main entry point for delegating memory tasks
|
||||
from Tatlock or other agents.
|
||||
|
||||
Args:
|
||||
task: The memory task or question
|
||||
context: Additional context from conversation
|
||||
message_history: Optional conversation history
|
||||
|
||||
Returns:
|
||||
Memory results or confirmation
|
||||
|
||||
Example:
|
||||
result = await run_biographer(
|
||||
task="What car do I drive?",
|
||||
context="User is asking about their vehicle",
|
||||
)
|
||||
"""
|
||||
agent = get_biographer_agent()
|
||||
|
||||
# Build prompt with context if provided
|
||||
prompt = task
|
||||
if context:
|
||||
prompt = f"Context: {context}\n\nTask: {task}"
|
||||
|
||||
logger.info(
|
||||
"biographer_task_started",
|
||||
task=task[:100],
|
||||
has_context=bool(context),
|
||||
has_history=bool(message_history),
|
||||
)
|
||||
|
||||
try:
|
||||
result = await agent.run(
|
||||
prompt,
|
||||
message_history=message_history,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"biographer_task_completed",
|
||||
task=task[:50],
|
||||
output_length=len(result.output),
|
||||
)
|
||||
|
||||
return result.output
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"biographer_task_error",
|
||||
task=task[:50],
|
||||
error=str(e),
|
||||
exc_info=True,
|
||||
)
|
||||
return f"The Biographer encountered an error: {str(e)}"
|
||||
|
||||
|
||||
async def run_biographer_stream(
|
||||
task: str,
|
||||
context: str = "",
|
||||
message_history: Optional[list[Any]] = None,
|
||||
):
|
||||
"""
|
||||
Execute a memory task with streaming output.
|
||||
|
||||
Yields text deltas as The Biographer generates the response.
|
||||
|
||||
Args:
|
||||
task: The memory task or question
|
||||
context: Additional context from conversation
|
||||
message_history: Optional conversation history
|
||||
|
||||
Yields:
|
||||
str: Text deltas from the response
|
||||
|
||||
Example:
|
||||
async for delta in run_biographer_stream("What do you know about me?"):
|
||||
print(delta, end="", flush=True)
|
||||
"""
|
||||
agent = get_biographer_agent()
|
||||
|
||||
# Build prompt with context if provided
|
||||
prompt = task
|
||||
if context:
|
||||
prompt = f"Context: {context}\n\nTask: {task}"
|
||||
|
||||
logger.info(
|
||||
"biographer_stream_started",
|
||||
task=task[:100],
|
||||
)
|
||||
|
||||
try:
|
||||
async with agent.run_stream(
|
||||
prompt,
|
||||
message_history=message_history,
|
||||
) as response:
|
||||
async for delta in response.stream_text(delta=True):
|
||||
yield delta
|
||||
|
||||
logger.info("biographer_stream_completed", task=task[:50])
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"biographer_stream_error",
|
||||
task=task[:50],
|
||||
error=str(e),
|
||||
exc_info=True,
|
||||
)
|
||||
yield f"\n\nThe Biographer encountered an error: {str(e)}"
|
||||
@@ -0,0 +1,88 @@
|
||||
"""
|
||||
Biographer capability registration for the Household Registry.
|
||||
|
||||
Defines The Biographer's capabilities and registers it as a
|
||||
household member for coordination by the Steward and Tatlock.
|
||||
"""
|
||||
from src.agents.biographer.agent import get_biographer_agent
|
||||
from src.agents.biographer.tools import BIOGRAPHER_TOOLS
|
||||
from src.core.household_registry import (
|
||||
HouseholdCapability,
|
||||
get_household_registry,
|
||||
)
|
||||
from src.core.logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
# The Biographer's capability summary for Steward coordination
|
||||
BIOGRAPHER_CAPABILITY = HouseholdCapability(
|
||||
name="biographer",
|
||||
role="The Biographer",
|
||||
category="context",
|
||||
description=(
|
||||
"Memory keeper for the user's story: can RECALL personal facts "
|
||||
"(car, job, family, pets), RECORD new information learned from "
|
||||
"conversation, UPDATE profile (name, location, timezone) and "
|
||||
"preferences (units, theme), and FORGET information when requested. "
|
||||
"Use for: 'what car do I drive?', 'remember that I...', "
|
||||
"'forget my...', 'what do you know about me?'"
|
||||
),
|
||||
domains=[
|
||||
"remember",
|
||||
"recall",
|
||||
"forget",
|
||||
"memory",
|
||||
"preferences",
|
||||
"profile",
|
||||
"personal",
|
||||
"know",
|
||||
"about me",
|
||||
"my",
|
||||
],
|
||||
cost="low", # Mostly vector search, minimal LLM
|
||||
requires_network=False, # All local (Qdrant, Redis)
|
||||
)
|
||||
|
||||
|
||||
def get_biographer_capability() -> HouseholdCapability:
|
||||
"""Get The Biographer's capability definition."""
|
||||
return BIOGRAPHER_CAPABILITY
|
||||
|
||||
|
||||
def register_biographer() -> None:
|
||||
"""
|
||||
Register The Biographer with the Household Registry.
|
||||
|
||||
This makes The Biographer available for:
|
||||
- Steward recommendations (via capability summary)
|
||||
- Tatlock delegation (via agent reference)
|
||||
- Tool scoping (via tool list)
|
||||
"""
|
||||
registry = get_household_registry()
|
||||
|
||||
# Check if already registered
|
||||
if "biographer" in registry:
|
||||
logger.debug("biographer_already_registered")
|
||||
return
|
||||
|
||||
registry.register(
|
||||
name="biographer",
|
||||
capability=BIOGRAPHER_CAPABILITY,
|
||||
tools=BIOGRAPHER_TOOLS,
|
||||
agent=get_biographer_agent(),
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"biographer_registered",
|
||||
role=BIOGRAPHER_CAPABILITY.role,
|
||||
domains=BIOGRAPHER_CAPABILITY.domains,
|
||||
tool_count=len(BIOGRAPHER_TOOLS),
|
||||
)
|
||||
|
||||
|
||||
def unregister_biographer() -> None:
|
||||
"""Unregister The Biographer from the Household Registry."""
|
||||
registry = get_household_registry()
|
||||
registry.unregister("biographer")
|
||||
logger.info("biographer_unregistered")
|
||||
@@ -0,0 +1,462 @@
|
||||
"""
|
||||
Biographer tools for PydanticAI agent.
|
||||
|
||||
These tools enable The Biographer to record and recall the user's story:
|
||||
- recall_semantic: Find memories by meaning/concept
|
||||
- store_insight: Record new facts about the user
|
||||
- list_memories: Browse recorded memories by type
|
||||
- forget_memory: Remove specific memories
|
||||
|
||||
For direct key-based access (get/set profile, preferences),
|
||||
use memory_service directly - these tools are for semantic queries.
|
||||
"""
|
||||
from src.core.context import get_user
|
||||
from src.core.embeddings import get_embedding_client
|
||||
from src.core.logging_config import get_logger
|
||||
from src.core.memory_service import MemoryType, memory_service
|
||||
from src.core.qdrant import get_qdrant_client
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Semantic Recall
|
||||
# ============================================================================
|
||||
|
||||
async def recall_semantic(
|
||||
query: str,
|
||||
memory_type: str | None = None,
|
||||
limit: int = 5,
|
||||
) -> str:
|
||||
"""
|
||||
Search memories by semantic similarity.
|
||||
|
||||
Use this to find memories that are conceptually related to
|
||||
the query, even if exact words don't match. This is the main
|
||||
tool for answering questions like "What car do I drive?" or
|
||||
"What did I mention about my job?"
|
||||
|
||||
Args:
|
||||
query: Natural language query to search for
|
||||
memory_type: Optional filter: "user_profile", "preference", "learned_fact"
|
||||
limit: Maximum memories to return (default: 5)
|
||||
|
||||
Returns:
|
||||
Matching memories with their content and relevance scores
|
||||
|
||||
Examples:
|
||||
recall_semantic("What is my car?")
|
||||
recall_semantic("work preferences", memory_type="preference")
|
||||
recall_semantic("family members")
|
||||
"""
|
||||
try:
|
||||
user = get_user()
|
||||
embedding_client = get_embedding_client()
|
||||
qdrant = get_qdrant_client()
|
||||
|
||||
# Generate embedding for query
|
||||
query_vector = await embedding_client.embed(query)
|
||||
if not query_vector:
|
||||
return "Unable to process query - embedding generation failed"
|
||||
|
||||
# Search memories
|
||||
results = await qdrant.search_memories(
|
||||
user=user,
|
||||
query_vector=query_vector,
|
||||
limit=limit,
|
||||
memory_type=memory_type,
|
||||
)
|
||||
|
||||
if not results:
|
||||
return f"No memories found related to '{query}'"
|
||||
|
||||
output_parts = [f"## Memories matching: {query}\n"]
|
||||
|
||||
for i, memory in enumerate(results, 1):
|
||||
mem_type = memory.get("type", "unknown")
|
||||
key = memory.get("key", "")
|
||||
value = memory.get("value", "")
|
||||
score = memory.get("score", 0.0)
|
||||
source = memory.get("source", "unknown")
|
||||
|
||||
type_icon = {
|
||||
"user_profile": "👤",
|
||||
"preference": "⚙️",
|
||||
"learned_fact": "💡",
|
||||
}.get(mem_type, "📝")
|
||||
|
||||
output_parts.append(f"{i}. {type_icon} **{key}** (relevance: {score:.2f})")
|
||||
output_parts.append(f" {value}")
|
||||
output_parts.append(f" _Type: {mem_type}, Source: {source}_")
|
||||
output_parts.append("")
|
||||
|
||||
logger.info(
|
||||
"memory_recall_semantic",
|
||||
query=query[:50],
|
||||
result_count=len(results),
|
||||
user=user,
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("memory_recall_semantic_error", error=str(e), query=query[:50])
|
||||
return f"Error searching memories: {str(e)}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Store Memory
|
||||
# ============================================================================
|
||||
|
||||
async def store_insight(
|
||||
key: str,
|
||||
value: str,
|
||||
keywords: list[str] | None = None,
|
||||
importance: float = 0.5,
|
||||
) -> str:
|
||||
"""
|
||||
Store a new insight or learned fact about the user.
|
||||
|
||||
Use this when:
|
||||
- User explicitly asks to remember something
|
||||
- User shares personal information worth remembering
|
||||
- You learn something from conversation that should persist
|
||||
|
||||
The memory will be stored with vector embedding for semantic search
|
||||
and can be recalled later using recall_semantic.
|
||||
|
||||
Args:
|
||||
key: Short identifier for the memory (e.g., "car", "employer", "pet")
|
||||
value: The actual information to remember
|
||||
keywords: Optional keywords for better search (auto-extracted if not provided)
|
||||
importance: How important is this? 0.0 (trivial) to 1.0 (critical)
|
||||
|
||||
Returns:
|
||||
Confirmation of stored memory
|
||||
|
||||
Examples:
|
||||
store_insight("car", "User drives a Tesla Model 3")
|
||||
store_insight("employer", "Works at Acme Corp as software engineer", importance=0.8)
|
||||
store_insight("coffee", "Prefers oat milk lattes", keywords=["coffee", "drink", "preference"])
|
||||
"""
|
||||
try:
|
||||
# Auto-generate keywords if not provided
|
||||
if not keywords:
|
||||
keywords = [key]
|
||||
# Extract simple keywords from value
|
||||
words = value.lower().split()
|
||||
keywords.extend([w for w in words if len(w) > 4][:5])
|
||||
|
||||
success = await memory_service.store_fact(
|
||||
key=key,
|
||||
value=value,
|
||||
keywords=keywords,
|
||||
importance=importance,
|
||||
source="conversation",
|
||||
)
|
||||
|
||||
if success:
|
||||
output_parts = [
|
||||
"## Memory Stored",
|
||||
f"**Key:** {key}",
|
||||
f"**Value:** {value}",
|
||||
f"**Keywords:** {', '.join(keywords)}",
|
||||
f"**Importance:** {importance:.1f}",
|
||||
"",
|
||||
"_Memory is now searchable via semantic recall._"
|
||||
]
|
||||
|
||||
logger.info(
|
||||
"memory_store_insight",
|
||||
key=key,
|
||||
importance=importance,
|
||||
user=get_user(),
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
else:
|
||||
return f"Failed to store memory for key '{key}'"
|
||||
|
||||
except Exception as e:
|
||||
logger.error("memory_store_insight_error", error=str(e), key=key)
|
||||
return f"Error storing memory: {str(e)}"
|
||||
|
||||
|
||||
async def update_profile(
|
||||
key: str,
|
||||
value: str,
|
||||
) -> str:
|
||||
"""
|
||||
Update user profile information.
|
||||
|
||||
Use this for core identity information:
|
||||
- name, location, timezone
|
||||
- language preferences
|
||||
- occupation
|
||||
|
||||
Profile data has high importance and is used for context
|
||||
by the Steward during request analysis.
|
||||
|
||||
Args:
|
||||
key: Profile field (e.g., "name", "location", "timezone")
|
||||
value: The value to set
|
||||
|
||||
Returns:
|
||||
Confirmation of profile update
|
||||
|
||||
Examples:
|
||||
update_profile("location", "Amsterdam, Netherlands")
|
||||
update_profile("timezone", "Europe/Amsterdam")
|
||||
update_profile("name", "John")
|
||||
"""
|
||||
try:
|
||||
success = await memory_service.set_profile(
|
||||
key=key,
|
||||
value=value,
|
||||
keywords=[key, "profile"],
|
||||
)
|
||||
|
||||
if success:
|
||||
output_parts = [
|
||||
"## Profile Updated",
|
||||
f"**{key}:** {value}",
|
||||
"",
|
||||
"_Profile data is automatically included in context._"
|
||||
]
|
||||
|
||||
logger.info(
|
||||
"memory_update_profile",
|
||||
key=key,
|
||||
user=get_user(),
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
else:
|
||||
return f"Failed to update profile field '{key}'"
|
||||
|
||||
except Exception as e:
|
||||
logger.error("memory_update_profile_error", error=str(e), key=key)
|
||||
return f"Error updating profile: {str(e)}"
|
||||
|
||||
|
||||
async def update_preference(
|
||||
key: str,
|
||||
value: str,
|
||||
) -> str:
|
||||
"""
|
||||
Update user preferences.
|
||||
|
||||
Use this for settings and preferences:
|
||||
- temperature_unit (celsius/fahrenheit)
|
||||
- distance_unit (metric/imperial)
|
||||
- theme, language, etc.
|
||||
|
||||
Preferences are used by agents to customize responses.
|
||||
|
||||
Args:
|
||||
key: Preference name (e.g., "temperature_unit", "theme")
|
||||
value: Preference value
|
||||
|
||||
Returns:
|
||||
Confirmation of preference update
|
||||
|
||||
Examples:
|
||||
update_preference("temperature_unit", "celsius")
|
||||
update_preference("distance_unit", "metric")
|
||||
update_preference("theme", "dark")
|
||||
"""
|
||||
try:
|
||||
success = await memory_service.set_preference(
|
||||
key=key,
|
||||
value=value,
|
||||
)
|
||||
|
||||
if success:
|
||||
output_parts = [
|
||||
"## Preference Updated",
|
||||
f"**{key}:** {value}",
|
||||
"",
|
||||
"_Preference will be applied to future responses._"
|
||||
]
|
||||
|
||||
logger.info(
|
||||
"memory_update_preference",
|
||||
key=key,
|
||||
user=get_user(),
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
else:
|
||||
return f"Failed to update preference '{key}'"
|
||||
|
||||
except Exception as e:
|
||||
logger.error("memory_update_preference_error", error=str(e), key=key)
|
||||
return f"Error updating preference: {str(e)}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# List Memories
|
||||
# ============================================================================
|
||||
|
||||
async def list_memories(
|
||||
memory_type: str = "learned_fact",
|
||||
limit: int = 20,
|
||||
) -> str:
|
||||
"""
|
||||
List stored memories of a specific type.
|
||||
|
||||
Use this to browse what's stored in memory without
|
||||
a specific search query.
|
||||
|
||||
Args:
|
||||
memory_type: Type to list: "user_profile", "preference", "learned_fact"
|
||||
limit: Maximum memories to return (default: 20)
|
||||
|
||||
Returns:
|
||||
List of memories with their keys and values
|
||||
|
||||
Examples:
|
||||
list_memories("user_profile")
|
||||
list_memories("preference")
|
||||
list_memories("learned_fact", limit=10)
|
||||
"""
|
||||
try:
|
||||
user = get_user()
|
||||
qdrant = get_qdrant_client()
|
||||
|
||||
# Convert string to MemoryType
|
||||
try:
|
||||
mem_type = MemoryType(memory_type)
|
||||
except ValueError:
|
||||
return f"Invalid memory type '{memory_type}'. Use: user_profile, preference, or learned_fact"
|
||||
|
||||
# Get all memories of type
|
||||
results = qdrant._client.scroll(
|
||||
collection_name=f"memories_{user}",
|
||||
scroll_filter={
|
||||
"must": [
|
||||
{"key": "type", "match": {"value": memory_type}},
|
||||
]
|
||||
},
|
||||
limit=limit,
|
||||
with_payload=True,
|
||||
with_vectors=False,
|
||||
)
|
||||
|
||||
points, _ = results
|
||||
if not points:
|
||||
return f"No {memory_type} memories found"
|
||||
|
||||
type_icon = {
|
||||
"user_profile": "👤",
|
||||
"preference": "⚙️",
|
||||
"learned_fact": "💡",
|
||||
}.get(memory_type, "📝")
|
||||
|
||||
output_parts = [f"## {type_icon} {memory_type.replace('_', ' ').title()} Memories\n"]
|
||||
|
||||
for point in points:
|
||||
payload = point.payload
|
||||
key = payload.get("key", "unknown")
|
||||
value = payload.get("value", "")
|
||||
importance = payload.get("importance", 0.5)
|
||||
|
||||
output_parts.append(f"- **{key}**: {value}")
|
||||
if importance > 0.7:
|
||||
output_parts.append(f" _(importance: {importance:.1f})_")
|
||||
|
||||
logger.info(
|
||||
"memory_list",
|
||||
memory_type=memory_type,
|
||||
count=len(points),
|
||||
user=user,
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("memory_list_error", error=str(e), memory_type=memory_type)
|
||||
return f"Error listing memories: {str(e)}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Forget Memory
|
||||
# ============================================================================
|
||||
|
||||
async def forget_memory(
|
||||
key: str,
|
||||
memory_type: str = "learned_fact",
|
||||
) -> str:
|
||||
"""
|
||||
Remove a specific memory.
|
||||
|
||||
Use this when:
|
||||
- User asks to forget something
|
||||
- Information is outdated or incorrect
|
||||
- Privacy concerns
|
||||
|
||||
Args:
|
||||
key: Key of the memory to forget
|
||||
memory_type: Type of memory: "user_profile", "preference", "learned_fact"
|
||||
|
||||
Returns:
|
||||
Confirmation of deletion
|
||||
|
||||
Examples:
|
||||
forget_memory("old_car")
|
||||
forget_memory("location", memory_type="user_profile")
|
||||
forget_memory("theme", memory_type="preference")
|
||||
"""
|
||||
try:
|
||||
# Convert string to MemoryType
|
||||
try:
|
||||
mem_type = MemoryType(memory_type)
|
||||
except ValueError:
|
||||
return f"Invalid memory type '{memory_type}'. Use: user_profile, preference, or learned_fact"
|
||||
|
||||
success = await memory_service.delete_memory(
|
||||
key=key,
|
||||
memory_type=mem_type,
|
||||
)
|
||||
|
||||
if success:
|
||||
output_parts = [
|
||||
"## Memory Forgotten",
|
||||
f"**Key:** {key}",
|
||||
f"**Type:** {memory_type}",
|
||||
"",
|
||||
"_Memory has been removed._"
|
||||
]
|
||||
|
||||
logger.info(
|
||||
"memory_forget",
|
||||
key=key,
|
||||
memory_type=memory_type,
|
||||
user=get_user(),
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
else:
|
||||
return f"Memory '{key}' not found or already deleted"
|
||||
|
||||
except Exception as e:
|
||||
logger.error("memory_forget_error", error=str(e), key=key)
|
||||
return f"Error forgetting memory: {str(e)}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Tool Collection for Registration
|
||||
# ============================================================================
|
||||
|
||||
# All tools available to The Biographer
|
||||
BIOGRAPHER_TOOLS = [
|
||||
# Recall
|
||||
recall_semantic,
|
||||
list_memories,
|
||||
# Record
|
||||
store_insight,
|
||||
update_profile,
|
||||
update_preference,
|
||||
# Manage
|
||||
forget_memory,
|
||||
]
|
||||
@@ -0,0 +1,407 @@
|
||||
"""
|
||||
Multi-agent coordination engine.
|
||||
|
||||
Orchestrates delegation from Tatlock to expert agents (Librarian, etc.)
|
||||
based on Steward recommendations. Handles:
|
||||
- Routing tasks to appropriate agents
|
||||
- Parallel and sequential execution
|
||||
- Result aggregation
|
||||
- Error handling and graceful degradation
|
||||
"""
|
||||
import asyncio
|
||||
import time
|
||||
from typing import Any, AsyncGenerator, Optional
|
||||
|
||||
from src.agents.librarian import run_librarian, run_librarian_stream
|
||||
from src.agents.protocol import (
|
||||
AgentError,
|
||||
AgentRequest,
|
||||
AgentResponse,
|
||||
AgentTimeoutError,
|
||||
AgentUnavailableError,
|
||||
CoordinationResult,
|
||||
DelegationIntent,
|
||||
DelegationReason,
|
||||
ToolCallRecord,
|
||||
)
|
||||
from src.core.household_registry import get_household_registry
|
||||
from src.core.logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
# Agent execution functions registry
|
||||
AGENT_EXECUTORS: dict[str, Any] = {
|
||||
"librarian": run_librarian,
|
||||
}
|
||||
|
||||
AGENT_STREAM_EXECUTORS: dict[str, Any] = {
|
||||
"librarian": run_librarian_stream,
|
||||
}
|
||||
|
||||
|
||||
class CoordinationEngine:
|
||||
"""
|
||||
Coordinates multi-agent task execution.
|
||||
|
||||
Routes tasks from Tatlock to appropriate expert agents,
|
||||
handles execution, and aggregates results.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the coordination engine."""
|
||||
self.registry = get_household_registry()
|
||||
logger.info("coordination_engine_initialized")
|
||||
|
||||
def get_available_agents(self) -> list[str]:
|
||||
"""
|
||||
Get list of available expert agents.
|
||||
|
||||
Returns:
|
||||
List of agent names that can accept delegations
|
||||
"""
|
||||
available = []
|
||||
for name in self.registry.list_members():
|
||||
member = self.registry.get_member(name)
|
||||
if member and member.agent is not None:
|
||||
available.append(name)
|
||||
return available
|
||||
|
||||
def can_delegate_to(self, agent_name: str) -> bool:
|
||||
"""
|
||||
Check if delegation to an agent is possible.
|
||||
|
||||
Args:
|
||||
agent_name: Name of the target agent
|
||||
|
||||
Returns:
|
||||
True if agent is available and can accept tasks
|
||||
"""
|
||||
if agent_name not in AGENT_EXECUTORS:
|
||||
return False
|
||||
|
||||
member = self.registry.get_member(agent_name)
|
||||
return member is not None and member.agent is not None
|
||||
|
||||
async def execute_delegation(
|
||||
self,
|
||||
intent: DelegationIntent,
|
||||
context: str = "",
|
||||
message_history: Optional[list[Any]] = None,
|
||||
) -> AgentResponse:
|
||||
"""
|
||||
Execute a single delegation to an expert agent.
|
||||
|
||||
Args:
|
||||
intent: The delegation intent with task details
|
||||
context: Additional context for the agent
|
||||
message_history: Optional conversation history
|
||||
|
||||
Returns:
|
||||
AgentResponse with results
|
||||
|
||||
Raises:
|
||||
AgentUnavailableError: If agent is not available
|
||||
AgentTimeoutError: If execution times out
|
||||
AgentError: For other execution errors
|
||||
"""
|
||||
start_time = time.time()
|
||||
agent_name = intent.target_agent
|
||||
|
||||
logger.info(
|
||||
"delegation_started",
|
||||
agent=agent_name,
|
||||
task=intent.task[:100],
|
||||
reason=intent.reason.value,
|
||||
)
|
||||
|
||||
# Check if agent is available
|
||||
if not self.can_delegate_to(agent_name):
|
||||
raise AgentUnavailableError(
|
||||
f"Agent '{agent_name}' is not available for delegation",
|
||||
agent_name=agent_name,
|
||||
)
|
||||
|
||||
# Get the executor
|
||||
executor = AGENT_EXECUTORS.get(agent_name)
|
||||
if not executor:
|
||||
raise AgentUnavailableError(
|
||||
f"No executor found for agent '{agent_name}'",
|
||||
agent_name=agent_name,
|
||||
)
|
||||
|
||||
try:
|
||||
# Build the request
|
||||
request = AgentRequest(
|
||||
task=intent.task,
|
||||
context=context,
|
||||
delegation_reason=intent.reason,
|
||||
)
|
||||
|
||||
# Execute with timeout
|
||||
timeout = request.timeout_seconds or 60
|
||||
|
||||
result = await asyncio.wait_for(
|
||||
executor(
|
||||
task=request.task,
|
||||
context=request.context,
|
||||
message_history=message_history,
|
||||
),
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
|
||||
logger.info(
|
||||
"delegation_completed",
|
||||
agent=agent_name,
|
||||
duration_ms=duration_ms,
|
||||
output_length=len(result),
|
||||
)
|
||||
|
||||
return AgentResponse(
|
||||
success=True,
|
||||
result=result,
|
||||
reasoning=f"Delegated to {agent_name}: {intent.expected_outcome}",
|
||||
duration_ms=duration_ms,
|
||||
)
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
logger.error(
|
||||
"delegation_timeout",
|
||||
agent=agent_name,
|
||||
duration_ms=duration_ms,
|
||||
)
|
||||
raise AgentTimeoutError(
|
||||
f"Agent '{agent_name}' timed out after {duration_ms}ms",
|
||||
agent_name=agent_name,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
logger.error(
|
||||
"delegation_error",
|
||||
agent=agent_name,
|
||||
error=str(e),
|
||||
duration_ms=duration_ms,
|
||||
exc_info=True,
|
||||
)
|
||||
return AgentResponse(
|
||||
success=False,
|
||||
result="",
|
||||
error_message=str(e),
|
||||
duration_ms=duration_ms,
|
||||
)
|
||||
|
||||
async def execute_delegation_stream(
|
||||
self,
|
||||
intent: DelegationIntent,
|
||||
context: str = "",
|
||||
message_history: Optional[list[Any]] = None,
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""
|
||||
Execute a delegation with streaming output.
|
||||
|
||||
Args:
|
||||
intent: The delegation intent with task details
|
||||
context: Additional context for the agent
|
||||
message_history: Optional conversation history
|
||||
|
||||
Yields:
|
||||
Text deltas from the agent
|
||||
|
||||
Raises:
|
||||
AgentUnavailableError: If agent is not available
|
||||
"""
|
||||
agent_name = intent.target_agent
|
||||
|
||||
logger.info(
|
||||
"delegation_stream_started",
|
||||
agent=agent_name,
|
||||
task=intent.task[:100],
|
||||
)
|
||||
|
||||
# Check if agent is available
|
||||
if agent_name not in AGENT_STREAM_EXECUTORS:
|
||||
raise AgentUnavailableError(
|
||||
f"Agent '{agent_name}' does not support streaming",
|
||||
agent_name=agent_name,
|
||||
)
|
||||
|
||||
executor = AGENT_STREAM_EXECUTORS[agent_name]
|
||||
|
||||
try:
|
||||
async for delta in executor(
|
||||
task=intent.task,
|
||||
context=context,
|
||||
message_history=message_history,
|
||||
):
|
||||
yield delta
|
||||
|
||||
logger.info("delegation_stream_completed", agent=agent_name)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"delegation_stream_error",
|
||||
agent=agent_name,
|
||||
error=str(e),
|
||||
exc_info=True,
|
||||
)
|
||||
yield f"\n\n[Error from {agent_name}: {str(e)}]"
|
||||
|
||||
async def coordinate(
|
||||
self,
|
||||
intents: list[DelegationIntent],
|
||||
context: str = "",
|
||||
message_history: Optional[list[Any]] = None,
|
||||
) -> CoordinationResult:
|
||||
"""
|
||||
Coordinate execution of multiple delegations.
|
||||
|
||||
Handles parallel execution for independent tasks and
|
||||
sequential execution for dependent tasks.
|
||||
|
||||
Args:
|
||||
intents: List of delegation intents to execute
|
||||
context: Shared context for all agents
|
||||
message_history: Optional conversation history
|
||||
|
||||
Returns:
|
||||
CoordinationResult with aggregated results
|
||||
"""
|
||||
start_time = time.time()
|
||||
agent_responses: dict[str, AgentResponse] = {}
|
||||
agents_consulted: list[str] = []
|
||||
|
||||
logger.info(
|
||||
"coordination_started",
|
||||
intent_count=len(intents),
|
||||
agents=[i.target_agent for i in intents],
|
||||
)
|
||||
|
||||
# Sort by priority
|
||||
sorted_intents = sorted(intents, key=lambda x: x.priority)
|
||||
|
||||
# Group by dependencies (simple version: sequential for now)
|
||||
# TODO: Implement parallel execution for independent tasks
|
||||
for intent in sorted_intents:
|
||||
try:
|
||||
response = await self.execute_delegation(
|
||||
intent=intent,
|
||||
context=context,
|
||||
message_history=message_history,
|
||||
)
|
||||
agent_responses[intent.target_agent] = response
|
||||
if response.success:
|
||||
agents_consulted.append(intent.target_agent)
|
||||
|
||||
except AgentError as e:
|
||||
agent_responses[intent.target_agent] = AgentResponse(
|
||||
success=False,
|
||||
result="",
|
||||
error_message=str(e),
|
||||
)
|
||||
|
||||
# Aggregate results
|
||||
successful_results = [
|
||||
r.result for r in agent_responses.values() if r.success and r.result
|
||||
]
|
||||
|
||||
final_response = "\n\n---\n\n".join(successful_results) if successful_results else ""
|
||||
|
||||
total_duration = int((time.time() - start_time) * 1000)
|
||||
|
||||
logger.info(
|
||||
"coordination_completed",
|
||||
total_duration_ms=total_duration,
|
||||
agents_consulted=agents_consulted,
|
||||
success_count=len(successful_results),
|
||||
)
|
||||
|
||||
return CoordinationResult(
|
||||
final_response=final_response,
|
||||
agent_responses=agent_responses,
|
||||
delegation_intents=intents,
|
||||
total_duration_ms=total_duration,
|
||||
agents_consulted=agents_consulted,
|
||||
)
|
||||
|
||||
|
||||
# Global coordination engine instance
|
||||
_coordination_engine: Optional[CoordinationEngine] = None
|
||||
|
||||
|
||||
def get_coordination_engine() -> CoordinationEngine:
|
||||
"""Get the global coordination engine instance."""
|
||||
global _coordination_engine
|
||||
if _coordination_engine is None:
|
||||
_coordination_engine = CoordinationEngine()
|
||||
return _coordination_engine
|
||||
|
||||
|
||||
async def delegate_to_librarian(
|
||||
task: str,
|
||||
context: str = "",
|
||||
reason: DelegationReason = DelegationReason.DOMAIN_EXPERTISE,
|
||||
message_history: Optional[list[Any]] = None,
|
||||
) -> AgentResponse:
|
||||
"""
|
||||
Convenience function to delegate a task to The Librarian.
|
||||
|
||||
Args:
|
||||
task: Research task description
|
||||
context: Additional context
|
||||
reason: Why delegating to Librarian
|
||||
message_history: Optional conversation history
|
||||
|
||||
Returns:
|
||||
AgentResponse with research results
|
||||
"""
|
||||
engine = get_coordination_engine()
|
||||
|
||||
intent = DelegationIntent(
|
||||
target_agent="librarian",
|
||||
task=task,
|
||||
reason=reason,
|
||||
expected_outcome="Research findings and relevant information",
|
||||
)
|
||||
|
||||
return await engine.execute_delegation(
|
||||
intent=intent,
|
||||
context=context,
|
||||
message_history=message_history,
|
||||
)
|
||||
|
||||
|
||||
async def delegate_to_librarian_stream(
|
||||
task: str,
|
||||
context: str = "",
|
||||
message_history: Optional[list[Any]] = None,
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""
|
||||
Convenience function to delegate to Librarian with streaming.
|
||||
|
||||
Args:
|
||||
task: Research task description
|
||||
context: Additional context
|
||||
message_history: Optional conversation history
|
||||
|
||||
Yields:
|
||||
Text deltas from The Librarian
|
||||
"""
|
||||
engine = get_coordination_engine()
|
||||
|
||||
intent = DelegationIntent(
|
||||
target_agent="librarian",
|
||||
task=task,
|
||||
reason=DelegationReason.DOMAIN_EXPERTISE,
|
||||
expected_outcome="Research findings",
|
||||
)
|
||||
|
||||
async for delta in engine.execute_delegation_stream(
|
||||
intent=intent,
|
||||
context=context,
|
||||
message_history=message_history,
|
||||
):
|
||||
yield delta
|
||||
@@ -0,0 +1,229 @@
|
||||
"""
|
||||
Delegation infrastructure for expert agent calls.
|
||||
|
||||
Provides delegation wrappers that Tatlock uses to call expert agents.
|
||||
Each wrapper encapsulates the complexity of calling an expert and
|
||||
returns a structured result for synthesis.
|
||||
|
||||
This implements the agent-as-tool pattern recommended by PydanticAI:
|
||||
agents call other agents via tool wrappers, keeping each agent focused.
|
||||
"""
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Callable, Optional, Any
|
||||
|
||||
from src.core.logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class DelegationTask:
|
||||
"""
|
||||
A task to be delegated to an expert agent.
|
||||
|
||||
Represents a unit of work that Tatlock delegates to a specialist.
|
||||
Used for tracking and orchestration of multi-expert workflows.
|
||||
|
||||
Attributes:
|
||||
expert_name: Name of the expert agent (e.g., "librarian", "memory")
|
||||
task: Clear description of what needs to be done
|
||||
context: Additional context from the conversation
|
||||
action: Specific action verb (create, search, update, etc.)
|
||||
priority: Execution priority (lower = higher priority)
|
||||
depends_on: List of task IDs this task depends on
|
||||
result: Result from expert after execution
|
||||
"""
|
||||
expert_name: str
|
||||
task: str
|
||||
context: str = ""
|
||||
action: str = ""
|
||||
priority: int = 0
|
||||
depends_on: list[str] = field(default_factory=list)
|
||||
result: Optional[str] = None
|
||||
task_id: str = ""
|
||||
|
||||
def __post_init__(self):
|
||||
"""Generate task ID if not provided."""
|
||||
if not self.task_id:
|
||||
import uuid
|
||||
self.task_id = f"{self.expert_name}_{uuid.uuid4().hex[:8]}"
|
||||
|
||||
|
||||
@dataclass
|
||||
class DelegationResult:
|
||||
"""
|
||||
Result from an expert agent delegation.
|
||||
|
||||
Attributes:
|
||||
expert_name: Which expert handled the task
|
||||
task: Original task description
|
||||
success: Whether the delegation succeeded
|
||||
output: Expert's response/findings
|
||||
error: Error message if failed
|
||||
"""
|
||||
expert_name: str
|
||||
task: str
|
||||
success: bool
|
||||
output: str
|
||||
error: Optional[str] = None
|
||||
|
||||
|
||||
async def delegate_to_librarian(
|
||||
task: str,
|
||||
context: str = "",
|
||||
) -> DelegationResult:
|
||||
"""
|
||||
Delegate a research or wiki task to The Librarian.
|
||||
|
||||
The Librarian handles:
|
||||
- Wiki creation (smart_create_wiki_page for topic-based)
|
||||
- Wiki updates (update_wiki_page for modifications)
|
||||
- Research queries (hybrid_search for comprehensive search)
|
||||
- Knowledge graph exploration
|
||||
- Document lookups and semantic search
|
||||
|
||||
This wrapper uses run() not run_stream() to avoid Ollama's
|
||||
streaming + tool call bug (PydanticAI issues #1292, #2256).
|
||||
|
||||
Args:
|
||||
task: Clear description of what needs to be done.
|
||||
Include the action verb (create, search, update, etc.)
|
||||
Example: "Create a wiki page about CI/CD pipelines"
|
||||
Example: "Search for information about Docker networking"
|
||||
context: Additional context from the user's request or
|
||||
conversation history
|
||||
|
||||
Returns:
|
||||
DelegationResult with the Librarian's findings
|
||||
|
||||
Example:
|
||||
>>> result = await delegate_to_librarian(
|
||||
... task="Create a wiki page about Kubernetes deployments",
|
||||
... context="User is setting up a homelab cluster",
|
||||
... )
|
||||
>>> if result.success:
|
||||
... print(result.output)
|
||||
"""
|
||||
from src.agents.librarian.agent import run_librarian
|
||||
|
||||
logger.info(
|
||||
"delegation_to_librarian_started",
|
||||
task=task[:100],
|
||||
has_context=bool(context),
|
||||
)
|
||||
|
||||
try:
|
||||
# Use run() not run_stream() - avoids Ollama bug
|
||||
output = await run_librarian(task=task, context=context)
|
||||
|
||||
logger.info(
|
||||
"delegation_to_librarian_completed",
|
||||
task=task[:50],
|
||||
output_length=len(output),
|
||||
)
|
||||
|
||||
return DelegationResult(
|
||||
expert_name="librarian",
|
||||
task=task,
|
||||
success=True,
|
||||
output=output,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"delegation_to_librarian_error",
|
||||
task=task[:50],
|
||||
error=str(e),
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
return DelegationResult(
|
||||
expert_name="librarian",
|
||||
task=task,
|
||||
success=False,
|
||||
output="",
|
||||
error=str(e),
|
||||
)
|
||||
|
||||
|
||||
async def delegate_to_biographer(
|
||||
task: str,
|
||||
context: str = "",
|
||||
) -> DelegationResult:
|
||||
"""
|
||||
Delegate a memory task to The Biographer.
|
||||
|
||||
The Biographer handles:
|
||||
- Semantic recall ("What car do I drive?", "What's my job?")
|
||||
- Recording new facts from conversation
|
||||
- Profile updates (name, location, timezone)
|
||||
- Preference updates (units, theme)
|
||||
- Memory management (forget, list)
|
||||
|
||||
For direct key-based lookups (get location, get timezone), use
|
||||
memory_service directly - it's faster and doesn't require LLM.
|
||||
|
||||
Args:
|
||||
task: Clear description of what needs to be done.
|
||||
Include the action verb (recall, remember, forget, etc.)
|
||||
Example: "What car do I drive?"
|
||||
Example: "Remember that I work at Acme Corp"
|
||||
context: Additional context from the user's request or
|
||||
conversation history
|
||||
|
||||
Returns:
|
||||
DelegationResult with The Biographer's response
|
||||
|
||||
Example:
|
||||
>>> result = await delegate_to_biographer(
|
||||
... task="What do you know about my preferences?",
|
||||
... context="User is asking about stored information",
|
||||
... )
|
||||
>>> if result.success:
|
||||
... print(result.output)
|
||||
"""
|
||||
from src.agents.biographer.agent import run_biographer
|
||||
|
||||
logger.info(
|
||||
"delegation_to_biographer_started",
|
||||
task=task[:100],
|
||||
has_context=bool(context),
|
||||
)
|
||||
|
||||
try:
|
||||
# Use run() not run_stream() - avoids Ollama bug
|
||||
output = await run_biographer(task=task, context=context)
|
||||
|
||||
logger.info(
|
||||
"delegation_to_biographer_completed",
|
||||
task=task[:50],
|
||||
output_length=len(output),
|
||||
)
|
||||
|
||||
return DelegationResult(
|
||||
expert_name="biographer",
|
||||
task=task,
|
||||
success=True,
|
||||
output=output,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"delegation_to_biographer_error",
|
||||
task=task[:50],
|
||||
error=str(e),
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
return DelegationResult(
|
||||
expert_name="biographer",
|
||||
task=task,
|
||||
success=False,
|
||||
output="",
|
||||
error=str(e),
|
||||
)
|
||||
|
||||
|
||||
# Future expert delegation wrappers will be added here:
|
||||
# - delegate_to_home_automation(task, context) -> DelegationResult
|
||||
# - delegate_to_developer(task, context) -> DelegationResult
|
||||
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
The Librarian - Expert agent for research and knowledge management.
|
||||
|
||||
Connects to the library-desk API to provide:
|
||||
- HybridRAG search (vector + graph + web)
|
||||
- Wiki.js operations
|
||||
- Knowledge graph queries
|
||||
- Semantic search
|
||||
"""
|
||||
from src.agents.librarian.agent import (
|
||||
get_librarian_agent,
|
||||
run_librarian,
|
||||
run_librarian_stream,
|
||||
)
|
||||
from src.agents.librarian.capability import (
|
||||
LIBRARIAN_CAPABILITY,
|
||||
get_librarian_capability,
|
||||
register_librarian,
|
||||
unregister_librarian,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"LIBRARIAN_CAPABILITY",
|
||||
"get_librarian_capability",
|
||||
"get_librarian_agent",
|
||||
"register_librarian",
|
||||
"unregister_librarian",
|
||||
"run_librarian",
|
||||
"run_librarian_stream",
|
||||
]
|
||||
@@ -0,0 +1,286 @@
|
||||
"""
|
||||
The Librarian - Expert agent for research and knowledge management.
|
||||
|
||||
A PydanticAI agent that provides research assistance through
|
||||
the library-desk API, offering:
|
||||
- HybridRAG search across all knowledge sources
|
||||
- Wiki and document management
|
||||
- Semantic search and knowledge graph exploration
|
||||
"""
|
||||
from typing import Any, Optional
|
||||
|
||||
from pydantic_ai import Agent
|
||||
|
||||
from src.agents.librarian.tools import (
|
||||
create_wiki_page,
|
||||
explore_knowledge_graph,
|
||||
find_related_entities,
|
||||
get_dossier_pages,
|
||||
get_wiki_page,
|
||||
hybrid_search,
|
||||
list_dossiers,
|
||||
search_wiki,
|
||||
semantic_search,
|
||||
smart_create_wiki_page,
|
||||
update_wiki_page,
|
||||
)
|
||||
from src.core.config import config
|
||||
from src.core.logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
# Librarian system prompt
|
||||
LIBRARIAN_SYSTEM_PROMPT = """You are The Librarian, an expert research assistant in the Tatlock household.
|
||||
|
||||
Your role is to help users find, understand, synthesize, and manage information from:
|
||||
- The personal wiki (Wiki.js) containing documentation and notes
|
||||
- The knowledge graph (Neo4j) with entities and relationships
|
||||
- Vector embeddings (Qdrant) for semantic search
|
||||
- Web search (SearXNG) for current information
|
||||
|
||||
## Your Personality
|
||||
- Scholarly and thorough in your research
|
||||
- Cite your sources and provide context
|
||||
- Organize information clearly
|
||||
- Suggest related topics when relevant
|
||||
- Acknowledge limitations when information is incomplete
|
||||
|
||||
## Your Tools
|
||||
|
||||
### Research Tools
|
||||
- **hybrid_search**: Your primary research tool - searches all sources at once
|
||||
- **search_wiki**: Find specific wiki pages by keyword
|
||||
- **semantic_search**: Find conceptually similar content
|
||||
- **explore_knowledge_graph** / **find_related_entities**: Discover connections
|
||||
- **list_dossiers** / **get_dossier_pages**: Browse knowledge collections
|
||||
|
||||
### Wiki Reading Tools
|
||||
- **get_wiki_page**: Read full content of a wiki page by ID
|
||||
- ALWAYS use this to fetch and read page content when summarizing
|
||||
- Use after search_wiki to get the full text of a specific page
|
||||
|
||||
### Wiki Writing Tools
|
||||
- **smart_create_wiki_page**: Create a page with automatic research (PREFERRED)
|
||||
- **This is the DEFAULT choice when user asks to create a wiki page about a topic**
|
||||
- When user says "Create a page about X" or "Add X to the wiki" without providing specific content, ALWAYS use this tool
|
||||
- Automatically researches the topic from wiki, graph, and web
|
||||
- Synthesizes content with proper source attribution
|
||||
- Creates bidirectional links in knowledge graph
|
||||
- **create_wiki_page**: Create a page with user-provided content
|
||||
- ONLY use when user provides specific text/content they want added verbatim
|
||||
- For simple notes, reminders, or quick additions with exact content
|
||||
- **update_wiki_page**: Update an existing page (partial updates)
|
||||
- Use when: "Update the page about X", "Fix this info", "Add to dossier"
|
||||
- First search_wiki to find the page, then get_wiki_page to read it
|
||||
- Only specify fields you want to change
|
||||
|
||||
## Research Approach
|
||||
1. Start with hybrid_search for broad queries
|
||||
2. Use search_wiki for specific document lookups
|
||||
3. **ALWAYS use get_wiki_page to fetch full content** before summarizing a page
|
||||
4. Use semantic_search when looking for conceptually similar content
|
||||
5. Explore the knowledge graph to find connections between concepts
|
||||
6. Synthesize and summarize findings clearly
|
||||
|
||||
## Writing Approach
|
||||
When asked to create or update wiki content:
|
||||
1. **"Create a page about X" (no specific content provided)**: Use smart_create_wiki_page
|
||||
- This is the PREFERRED tool for topic-based page creation
|
||||
- It researches first and creates comprehensive, well-sourced content
|
||||
2. **User provides exact text to add**: Use create_wiki_page with their content
|
||||
3. **Updating existing pages**:
|
||||
- Search for the page with search_wiki
|
||||
- Fetch full content with get_wiki_page
|
||||
- Make edits and use update_wiki_page
|
||||
4. **Organizing into dossiers**: Use update_wiki_page with just the tags field
|
||||
|
||||
## Response Format
|
||||
Your responses are returned to Tatlock (the butler) who will synthesize them into a final answer for the user. Keep this in mind:
|
||||
- Lead with the key findings or confirmation of action
|
||||
- Include relevant sources and citations
|
||||
- When summarizing wiki pages, fetch and read them first
|
||||
- Note any gaps in available information
|
||||
- Be concise but thorough - Tatlock will format the final response
|
||||
- Structure your findings clearly so they can be easily integrated with other responses
|
||||
"""
|
||||
|
||||
# Lazy initialization to avoid connection issues during imports
|
||||
_librarian_agent: Optional[Agent[None, str]] = None
|
||||
|
||||
|
||||
def _create_librarian_agent() -> Agent[None, str]:
|
||||
"""Create the Librarian PydanticAI agent."""
|
||||
# Import required classes for Ollama configuration
|
||||
from pydantic_ai.models.openai import OpenAIChatModel
|
||||
from pydantic_ai.providers.ollama import OllamaProvider
|
||||
|
||||
# PydanticAI expects Ollama base URL to end with /v1
|
||||
clean_host = str(config.OLLAMA_HOST).rstrip('/')
|
||||
base_url = f"{clean_host}/v1"
|
||||
|
||||
# Create Ollama model with provider
|
||||
model = OpenAIChatModel(
|
||||
model_name=config.OLLAMA_DEFAULT_MODEL,
|
||||
provider=OllamaProvider(base_url=base_url)
|
||||
)
|
||||
|
||||
agent: Agent[None, str] = Agent(
|
||||
model=model,
|
||||
system_prompt=LIBRARIAN_SYSTEM_PROMPT,
|
||||
retries=2,
|
||||
)
|
||||
|
||||
# Register research tools
|
||||
agent.tool_plain(hybrid_search)
|
||||
agent.tool_plain(search_wiki)
|
||||
agent.tool_plain(semantic_search)
|
||||
agent.tool_plain(list_dossiers)
|
||||
agent.tool_plain(get_dossier_pages)
|
||||
agent.tool_plain(explore_knowledge_graph)
|
||||
agent.tool_plain(find_related_entities)
|
||||
|
||||
# Register wiki read tools
|
||||
agent.tool_plain(get_wiki_page)
|
||||
|
||||
# Register wiki write tools
|
||||
agent.tool_plain(create_wiki_page)
|
||||
agent.tool_plain(update_wiki_page)
|
||||
agent.tool_plain(smart_create_wiki_page)
|
||||
|
||||
logger.info(
|
||||
"librarian_agent_created",
|
||||
model=config.OLLAMA_DEFAULT_MODEL,
|
||||
tool_count=11,
|
||||
)
|
||||
|
||||
return agent
|
||||
|
||||
|
||||
def get_librarian_agent() -> Agent[None, str]:
|
||||
"""
|
||||
Get the Librarian agent instance (lazy initialization).
|
||||
|
||||
Returns:
|
||||
PydanticAI Agent configured for research tasks
|
||||
"""
|
||||
global _librarian_agent
|
||||
if _librarian_agent is None:
|
||||
_librarian_agent = _create_librarian_agent()
|
||||
return _librarian_agent
|
||||
|
||||
|
||||
async def run_librarian(
|
||||
task: str,
|
||||
context: str = "",
|
||||
message_history: Optional[list[Any]] = None,
|
||||
) -> str:
|
||||
"""
|
||||
Execute a research task with The Librarian.
|
||||
|
||||
This is the main entry point for delegating research tasks
|
||||
to The Librarian from Tatlock or other agents.
|
||||
|
||||
Args:
|
||||
task: The research task or question
|
||||
context: Additional context from conversation
|
||||
message_history: Optional conversation history
|
||||
|
||||
Returns:
|
||||
Research results and findings
|
||||
|
||||
Example:
|
||||
result = await run_librarian(
|
||||
task="Find information about Docker networking",
|
||||
context="User is setting up a homelab",
|
||||
)
|
||||
"""
|
||||
agent = get_librarian_agent()
|
||||
|
||||
# Build prompt with context if provided
|
||||
prompt = task
|
||||
if context:
|
||||
prompt = f"Context: {context}\n\nTask: {task}"
|
||||
|
||||
logger.info(
|
||||
"librarian_task_started",
|
||||
task=task[:100],
|
||||
has_context=bool(context),
|
||||
has_history=bool(message_history),
|
||||
)
|
||||
|
||||
try:
|
||||
result = await agent.run(
|
||||
prompt,
|
||||
message_history=message_history,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"librarian_task_completed",
|
||||
task=task[:50],
|
||||
output_length=len(result.output),
|
||||
)
|
||||
|
||||
return result.output
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"librarian_task_error",
|
||||
task=task[:50],
|
||||
error=str(e),
|
||||
exc_info=True,
|
||||
)
|
||||
return f"The Librarian encountered an error: {str(e)}"
|
||||
|
||||
|
||||
async def run_librarian_stream(
|
||||
task: str,
|
||||
context: str = "",
|
||||
message_history: Optional[list[Any]] = None,
|
||||
):
|
||||
"""
|
||||
Execute a research task with streaming output.
|
||||
|
||||
Yields text deltas as The Librarian generates the response.
|
||||
|
||||
Args:
|
||||
task: The research task or question
|
||||
context: Additional context from conversation
|
||||
message_history: Optional conversation history
|
||||
|
||||
Yields:
|
||||
str: Text deltas from the response
|
||||
|
||||
Example:
|
||||
async for delta in run_librarian_stream("Find Docker docs"):
|
||||
print(delta, end="", flush=True)
|
||||
"""
|
||||
agent = get_librarian_agent()
|
||||
|
||||
# Build prompt with context if provided
|
||||
prompt = task
|
||||
if context:
|
||||
prompt = f"Context: {context}\n\nTask: {task}"
|
||||
|
||||
logger.info(
|
||||
"librarian_stream_started",
|
||||
task=task[:100],
|
||||
)
|
||||
|
||||
try:
|
||||
async with agent.run_stream(
|
||||
prompt,
|
||||
message_history=message_history,
|
||||
) as response:
|
||||
async for delta in response.stream_text(delta=True):
|
||||
yield delta
|
||||
|
||||
logger.info("librarian_stream_completed", task=task[:50])
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"librarian_stream_error",
|
||||
task=task[:50],
|
||||
error=str(e),
|
||||
exc_info=True,
|
||||
)
|
||||
yield f"\n\nThe Librarian encountered an error: {str(e)}"
|
||||
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
Librarian capability registration for the Household Registry.
|
||||
|
||||
Defines The Librarian's capabilities and registers it as a
|
||||
household member for coordination by the Steward and Tatlock.
|
||||
"""
|
||||
from src.agents.librarian.agent import get_librarian_agent
|
||||
from src.agents.librarian.tools import LIBRARIAN_TOOLS
|
||||
from src.core.household_registry import (
|
||||
HouseholdCapability,
|
||||
get_household_registry,
|
||||
)
|
||||
from src.core.logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
# The Librarian's capability summary for Steward coordination
|
||||
LIBRARIAN_CAPABILITY = HouseholdCapability(
|
||||
name="librarian",
|
||||
role="The Librarian",
|
||||
category="research",
|
||||
description=(
|
||||
"Research and wiki management: can CREATE wiki pages about topics "
|
||||
"(with automatic HybridRAG research), UPDATE existing pages, "
|
||||
"SEARCH wiki/knowledge graph/web, and synthesize information. "
|
||||
"Use for: 'create a page about X', 'update wiki', 'find info on X'"
|
||||
),
|
||||
domains=[
|
||||
"research",
|
||||
"knowledge",
|
||||
"information",
|
||||
"wiki",
|
||||
"documents",
|
||||
"search",
|
||||
"synthesis",
|
||||
"create",
|
||||
"write",
|
||||
"update",
|
||||
],
|
||||
cost="medium", # Multiple API calls to library-desk
|
||||
requires_network=True, # Needs library-desk API access
|
||||
)
|
||||
|
||||
|
||||
def get_librarian_capability() -> HouseholdCapability:
|
||||
"""Get The Librarian's capability definition."""
|
||||
return LIBRARIAN_CAPABILITY
|
||||
|
||||
|
||||
def register_librarian() -> None:
|
||||
"""
|
||||
Register The Librarian with the Household Registry.
|
||||
|
||||
This makes The Librarian available for:
|
||||
- Steward recommendations (via capability summary)
|
||||
- Tatlock delegation (via agent reference)
|
||||
- Tool scoping (via tool list)
|
||||
"""
|
||||
registry = get_household_registry()
|
||||
|
||||
# Check if already registered
|
||||
if "librarian" in registry:
|
||||
logger.debug("librarian_already_registered")
|
||||
return
|
||||
|
||||
registry.register(
|
||||
name="librarian",
|
||||
capability=LIBRARIAN_CAPABILITY,
|
||||
tools=LIBRARIAN_TOOLS,
|
||||
agent=get_librarian_agent(),
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"librarian_registered",
|
||||
role=LIBRARIAN_CAPABILITY.role,
|
||||
domains=LIBRARIAN_CAPABILITY.domains,
|
||||
tool_count=len(LIBRARIAN_TOOLS),
|
||||
)
|
||||
|
||||
|
||||
def unregister_librarian() -> None:
|
||||
"""Unregister The Librarian from the Household Registry."""
|
||||
registry = get_household_registry()
|
||||
registry.unregister("librarian")
|
||||
logger.info("librarian_unregistered")
|
||||
@@ -0,0 +1,698 @@
|
||||
"""
|
||||
HTTP client for the Library-Desk API.
|
||||
|
||||
Provides async methods for all relevant library-desk endpoints:
|
||||
- HybridRAG queries
|
||||
- Wiki operations
|
||||
- Vector search
|
||||
- Knowledge graph queries
|
||||
"""
|
||||
from typing import Any, Optional
|
||||
|
||||
import httpx
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from src.core.config import config
|
||||
from src.core.context import get_user
|
||||
from src.core.logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Response Models
|
||||
# ============================================================================
|
||||
|
||||
class WikiPage(BaseModel):
|
||||
"""Wiki page from library-desk."""
|
||||
id: int
|
||||
path: str
|
||||
title: str
|
||||
description: Optional[str] = None
|
||||
content: Optional[str] = None
|
||||
tags: list[str] = Field(default_factory=list)
|
||||
created_at: Optional[str] = None
|
||||
updated_at: Optional[str] = None
|
||||
|
||||
|
||||
class WikiSearchResult(BaseModel):
|
||||
"""Search result from wiki search."""
|
||||
id: int
|
||||
path: str
|
||||
title: str
|
||||
description: Optional[str] = None
|
||||
locale: Optional[str] = None
|
||||
|
||||
|
||||
class VectorSearchResult(BaseModel):
|
||||
"""Result from semantic vector search."""
|
||||
page_id: int
|
||||
page_path: str
|
||||
page_title: str
|
||||
chunk_text: str
|
||||
score: float
|
||||
chunk_index: int
|
||||
|
||||
|
||||
class HybridSearchResult(BaseModel):
|
||||
"""Result from HybridRAG search."""
|
||||
source: str # "vector", "graph", "web"
|
||||
title: str
|
||||
content: str
|
||||
url: Optional[str] = None
|
||||
score: float
|
||||
page_id: Optional[int] = None
|
||||
metadata: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class HybridRAGResponse(BaseModel):
|
||||
"""Full response from HybridRAG query."""
|
||||
results: list[HybridSearchResult] = Field(default_factory=list)
|
||||
keywords: list[str] = Field(default_factory=list)
|
||||
synonyms: list[str] = Field(default_factory=list)
|
||||
related_dossiers: list[str] = Field(default_factory=list)
|
||||
formatted_context: str = ""
|
||||
search_id: Optional[str] = None
|
||||
timing: dict[str, float] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class GraphNode(BaseModel):
|
||||
"""Node from knowledge graph."""
|
||||
id: str
|
||||
labels: list[str] = Field(default_factory=list)
|
||||
properties: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class Dossier(BaseModel):
|
||||
"""A dossier (tag-based collection)."""
|
||||
name: str
|
||||
page_count: int
|
||||
|
||||
|
||||
class ResearchSummary(BaseModel):
|
||||
"""Summary of research performed during smart-create."""
|
||||
wiki_results: int = 0
|
||||
web_results: int = 0
|
||||
graph_entities: int = 0
|
||||
keywords_extracted: int = 0
|
||||
timing_ms: int = 0
|
||||
|
||||
|
||||
class EntityLinking(BaseModel):
|
||||
"""Entity linking results from smart-create."""
|
||||
forward_links: int = 0
|
||||
backward_links: int = 0
|
||||
pages_updated: int = 0
|
||||
|
||||
|
||||
class SmartCreateResponse(BaseModel):
|
||||
"""Response from smart-create wiki page endpoint."""
|
||||
page: WikiPage
|
||||
research_summary: ResearchSummary = Field(default_factory=ResearchSummary)
|
||||
sources_used: int = 0
|
||||
search_id: Optional[str] = None
|
||||
entity_linking: EntityLinking = Field(default_factory=EntityLinking)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Client
|
||||
# ============================================================================
|
||||
|
||||
class LibraryDeskClient:
|
||||
"""
|
||||
Async HTTP client for Library-Desk API.
|
||||
|
||||
Usage:
|
||||
async with LibraryDeskClient() as client:
|
||||
results = await client.hybrid_search("docker kubernetes")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_url: Optional[str] = None,
|
||||
api_key: Optional[str] = None,
|
||||
timeout: int = 60,
|
||||
):
|
||||
"""
|
||||
Initialize the client.
|
||||
|
||||
Args:
|
||||
base_url: Library-desk API URL (defaults to config)
|
||||
api_key: API key for authentication (defaults to config)
|
||||
timeout: Request timeout in seconds
|
||||
"""
|
||||
self.base_url = base_url or str(config.LIBRARY_DESK_HOST)
|
||||
self.api_key = api_key or config.LIBRARY_DESK_API_KEY
|
||||
self.timeout = timeout
|
||||
self._client: Optional[httpx.AsyncClient] = None
|
||||
|
||||
async def __aenter__(self) -> "LibraryDeskClient":
|
||||
"""Create HTTP client on context entry."""
|
||||
headers = {}
|
||||
if self.api_key:
|
||||
headers["Authorization"] = f"Bearer {self.api_key}"
|
||||
|
||||
self._client = httpx.AsyncClient(
|
||||
base_url=self.base_url,
|
||||
headers=headers,
|
||||
timeout=self.timeout,
|
||||
)
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
|
||||
"""Close HTTP client on context exit."""
|
||||
if self._client:
|
||||
await self._client.aclose()
|
||||
self._client = None
|
||||
|
||||
def _ensure_client(self) -> httpx.AsyncClient:
|
||||
"""Ensure client is initialized."""
|
||||
if self._client is None:
|
||||
raise RuntimeError(
|
||||
"Client not initialized. Use 'async with LibraryDeskClient() as client:'"
|
||||
)
|
||||
return self._client
|
||||
|
||||
# ========================================================================
|
||||
# HybridRAG
|
||||
# ========================================================================
|
||||
|
||||
async def hybrid_search(
|
||||
self,
|
||||
query: str,
|
||||
user: str | None = None,
|
||||
vector_limit: int = 10,
|
||||
graph_limit: int = 10,
|
||||
web_limit: int = 5,
|
||||
enable_reranking: bool = True,
|
||||
final_result_count: int = 10,
|
||||
) -> HybridRAGResponse:
|
||||
"""
|
||||
Execute HybridRAG search combining vector, graph, and web results.
|
||||
|
||||
Args:
|
||||
query: Search query
|
||||
user: User identifier for multi-tenancy (defaults to request context)
|
||||
vector_limit: Max results from vector search
|
||||
graph_limit: Max results from graph search
|
||||
web_limit: Max results from web search
|
||||
enable_reranking: Whether to rerank with LLM
|
||||
final_result_count: Number of final results after fusion
|
||||
|
||||
Returns:
|
||||
HybridRAGResponse with ranked results and context
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
payload = {
|
||||
"query": query,
|
||||
"config": {
|
||||
"vector_limit": vector_limit,
|
||||
"graph_limit": graph_limit,
|
||||
"web_limit": web_limit,
|
||||
"enable_reranking": enable_reranking,
|
||||
"final_result_count": final_result_count,
|
||||
},
|
||||
}
|
||||
|
||||
logger.info("library_desk_hybrid_search", query=query, user=user)
|
||||
|
||||
response = await client.post(
|
||||
"/query/hybrid",
|
||||
json=payload,
|
||||
params={"user": user},
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
|
||||
# Parse results
|
||||
results = []
|
||||
for r in data.get("results", []):
|
||||
results.append(HybridSearchResult(
|
||||
source=r.get("source", "unknown"),
|
||||
title=r.get("title", ""),
|
||||
content=r.get("content", ""),
|
||||
url=r.get("url"),
|
||||
score=r.get("score", 0.0),
|
||||
page_id=r.get("page_id"),
|
||||
metadata=r.get("metadata", {}),
|
||||
))
|
||||
|
||||
return HybridRAGResponse(
|
||||
results=results,
|
||||
keywords=data.get("keywords", []),
|
||||
synonyms=data.get("synonyms", []),
|
||||
related_dossiers=data.get("related_dossiers", []),
|
||||
formatted_context=data.get("formatted_context", ""),
|
||||
search_id=data.get("search_id"),
|
||||
timing=data.get("timing", {}),
|
||||
)
|
||||
|
||||
# ========================================================================
|
||||
# Wiki Operations
|
||||
# ========================================================================
|
||||
|
||||
async def search_wiki(
|
||||
self,
|
||||
query: str,
|
||||
user: str | None = None,
|
||||
limit: int = 20,
|
||||
) -> list[WikiSearchResult]:
|
||||
"""
|
||||
Search wiki pages by text.
|
||||
|
||||
Args:
|
||||
query: Search query
|
||||
user: User identifier (defaults to request context)
|
||||
limit: Maximum results
|
||||
|
||||
Returns:
|
||||
List of matching wiki pages
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
logger.debug("library_desk_wiki_search", query=query, user=user)
|
||||
|
||||
response = await client.get(
|
||||
"/wiki/search",
|
||||
params={"q": query, "user": user, "limit": limit},
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
return [WikiSearchResult(**r) for r in data.get("results", [])]
|
||||
|
||||
async def get_wiki_page(
|
||||
self,
|
||||
page_id: int,
|
||||
user: str | None = None,
|
||||
) -> WikiPage:
|
||||
"""
|
||||
Get a wiki page by ID.
|
||||
|
||||
Args:
|
||||
page_id: Page ID
|
||||
user: User identifier (defaults to request context)
|
||||
|
||||
Returns:
|
||||
WikiPage with full content
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
response = await client.get(
|
||||
f"/wiki/pages/{page_id}",
|
||||
params={"user": user},
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
return WikiPage(**response.json())
|
||||
|
||||
async def list_wiki_pages(
|
||||
self,
|
||||
user: str | None = None,
|
||||
tag: Optional[str] = None,
|
||||
limit: int = 50,
|
||||
) -> list[WikiPage]:
|
||||
"""
|
||||
List wiki pages, optionally filtered by tag.
|
||||
|
||||
Args:
|
||||
user: User identifier (defaults to request context)
|
||||
tag: Optional tag (dossier) to filter by
|
||||
limit: Maximum pages to return
|
||||
|
||||
Returns:
|
||||
List of wiki pages
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
params: dict[str, Any] = {"user": user, "limit": limit}
|
||||
if tag:
|
||||
params["tag"] = tag
|
||||
|
||||
response = await client.get("/wiki/pages", params=params)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
return [WikiPage(**p) for p in data.get("pages", [])]
|
||||
|
||||
async def create_wiki_page(
|
||||
self,
|
||||
title: str,
|
||||
path: str,
|
||||
content: str,
|
||||
user: str | None = None,
|
||||
description: str = "",
|
||||
tags: Optional[list[str]] = None,
|
||||
) -> WikiPage:
|
||||
"""
|
||||
Create a new wiki page.
|
||||
|
||||
Args:
|
||||
title: Page title
|
||||
path: Page path (e.g., "/projects/my-project")
|
||||
content: Markdown content
|
||||
user: User identifier (defaults to request context)
|
||||
description: Short description
|
||||
tags: List of tags (dossiers)
|
||||
|
||||
Returns:
|
||||
Created WikiPage
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
payload = {
|
||||
"title": title,
|
||||
"path": path,
|
||||
"content": content,
|
||||
"user": user,
|
||||
"description": description,
|
||||
"tags": tags or [],
|
||||
}
|
||||
|
||||
logger.info("library_desk_create_page", title=title, path=path)
|
||||
|
||||
response = await client.post("/wiki/pages", json=payload)
|
||||
response.raise_for_status()
|
||||
|
||||
return WikiPage(**response.json())
|
||||
|
||||
async def update_wiki_page(
|
||||
self,
|
||||
page_id: int,
|
||||
user: str | None = None,
|
||||
content: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
description: Optional[str] = None,
|
||||
) -> WikiPage:
|
||||
"""
|
||||
Update an existing wiki page.
|
||||
|
||||
Supports partial updates - only provided fields are updated.
|
||||
Automatically triggers vector re-indexing and graph extraction.
|
||||
|
||||
Args:
|
||||
page_id: ID of the page to update
|
||||
user: User identifier (defaults to request context)
|
||||
content: New content (optional)
|
||||
title: New title (optional)
|
||||
tags: New tags list (optional)
|
||||
description: New description (optional)
|
||||
|
||||
Returns:
|
||||
Updated WikiPage
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
# Build update payload with only provided fields
|
||||
update_data: dict[str, Any] = {}
|
||||
if content is not None:
|
||||
update_data["content"] = content
|
||||
if title is not None:
|
||||
update_data["title"] = title
|
||||
if tags is not None:
|
||||
update_data["tags"] = tags
|
||||
if description is not None:
|
||||
update_data["description"] = description
|
||||
|
||||
logger.info(
|
||||
"library_desk_update_page",
|
||||
page_id=page_id,
|
||||
fields=list(update_data.keys()),
|
||||
)
|
||||
|
||||
response = await client.put(
|
||||
f"/wiki/pages/{page_id}",
|
||||
params={"user": user},
|
||||
json=update_data,
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
return WikiPage(**response.json())
|
||||
|
||||
async def smart_create_wiki_page(
|
||||
self,
|
||||
topic: str,
|
||||
tags: list[str],
|
||||
user: str | None = None,
|
||||
path: Optional[str] = None,
|
||||
include_web_research: bool = True,
|
||||
include_wiki_search: bool = True,
|
||||
) -> SmartCreateResponse:
|
||||
"""
|
||||
Create a wiki page with HybridRAG research.
|
||||
|
||||
This endpoint:
|
||||
1. Searches existing wiki, knowledge graph, and web for context
|
||||
2. Uses LLM to synthesize findings into structured content
|
||||
3. Creates the page with proper attribution
|
||||
4. Automatically links entities bidirectionally
|
||||
|
||||
Args:
|
||||
topic: The topic to research and create a page about
|
||||
tags: List of tags (dossiers) for the page
|
||||
user: User identifier
|
||||
path: Optional custom path (auto-generated from topic if not provided)
|
||||
include_web_research: Whether to include web search results
|
||||
include_wiki_search: Whether to include existing wiki content
|
||||
|
||||
Returns:
|
||||
SmartCreateResponse with page and research metadata
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
payload: dict[str, Any] = {
|
||||
"topic": topic,
|
||||
"tags": tags,
|
||||
"user": user,
|
||||
"include_web_research": include_web_research,
|
||||
"include_wiki_search": include_wiki_search,
|
||||
}
|
||||
if path is not None:
|
||||
payload["path"] = path
|
||||
|
||||
logger.info(
|
||||
"library_desk_smart_create",
|
||||
topic=topic,
|
||||
tags=tags,
|
||||
include_web=include_web_research,
|
||||
)
|
||||
|
||||
response = await client.post("/wiki/pages/smart-create", json=payload)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
|
||||
# Parse nested response
|
||||
page = WikiPage(**data.get("page", {}))
|
||||
research_summary = ResearchSummary(**data.get("research_summary", {}))
|
||||
entity_linking = EntityLinking(**data.get("entity_linking", {}))
|
||||
|
||||
return SmartCreateResponse(
|
||||
page=page,
|
||||
research_summary=research_summary,
|
||||
sources_used=data.get("sources_used", 0),
|
||||
search_id=data.get("search_id"),
|
||||
entity_linking=entity_linking,
|
||||
)
|
||||
|
||||
async def list_dossiers(
|
||||
self,
|
||||
user: str | None = None,
|
||||
) -> list[Dossier]:
|
||||
"""
|
||||
List all dossiers (tag collections) for a user.
|
||||
|
||||
Args:
|
||||
user: User identifier (defaults to request context)
|
||||
|
||||
Returns:
|
||||
List of dossiers with page counts
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
response = await client.get(
|
||||
"/wiki/dossiers",
|
||||
params={"user": user},
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
return [Dossier(**d) for d in data.get("dossiers", [])]
|
||||
|
||||
# ========================================================================
|
||||
# Vector Search
|
||||
# ========================================================================
|
||||
|
||||
async def semantic_search(
|
||||
self,
|
||||
query: str,
|
||||
user: str | None = None,
|
||||
limit: int = 10,
|
||||
score_threshold: float = 0.5,
|
||||
) -> list[VectorSearchResult]:
|
||||
"""
|
||||
Perform semantic (vector) search over documents.
|
||||
|
||||
Args:
|
||||
query: Natural language query
|
||||
user: User identifier (defaults to request context)
|
||||
limit: Maximum results
|
||||
score_threshold: Minimum similarity score
|
||||
|
||||
Returns:
|
||||
List of matching document chunks with scores
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
payload = {
|
||||
"query": query,
|
||||
"user": user,
|
||||
"limit": limit,
|
||||
"score_threshold": score_threshold,
|
||||
}
|
||||
|
||||
logger.debug("library_desk_semantic_search", query=query)
|
||||
|
||||
response = await client.post("/vector/search", json=payload)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
return [VectorSearchResult(**r) for r in data.get("results", [])]
|
||||
|
||||
# ========================================================================
|
||||
# Knowledge Graph
|
||||
# ========================================================================
|
||||
|
||||
async def query_graph(
|
||||
self,
|
||||
cypher_query: str,
|
||||
user: str | None = None,
|
||||
parameters: Optional[dict[str, Any]] = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Execute a Cypher query on the knowledge graph.
|
||||
|
||||
Note: Query is automatically scoped to user's data.
|
||||
|
||||
Args:
|
||||
cypher_query: Cypher query string
|
||||
user: User identifier (defaults to request context)
|
||||
parameters: Query parameters
|
||||
|
||||
Returns:
|
||||
List of result records
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
payload = {
|
||||
"query": cypher_query,
|
||||
"user": user,
|
||||
"parameters": parameters or {},
|
||||
}
|
||||
|
||||
logger.debug("library_desk_graph_query", query=cypher_query[:100])
|
||||
|
||||
response = await client.post("/graph/query", json=payload)
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json().get("records", [])
|
||||
|
||||
async def list_graph_nodes(
|
||||
self,
|
||||
user: str | None = None,
|
||||
node_type: Optional[str] = None,
|
||||
limit: int = 100,
|
||||
) -> list[GraphNode]:
|
||||
"""
|
||||
List nodes in the knowledge graph.
|
||||
|
||||
Args:
|
||||
user: User identifier (defaults to request context)
|
||||
node_type: Optional filter by type (Document, Person, Concept, etc.)
|
||||
limit: Maximum nodes
|
||||
|
||||
Returns:
|
||||
List of graph nodes
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
params: dict[str, Any] = {"user": user, "limit": limit}
|
||||
if node_type:
|
||||
params["node_type"] = node_type
|
||||
|
||||
response = await client.get("/graph/nodes", params=params)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
return [GraphNode(**n) for n in data.get("nodes", [])]
|
||||
|
||||
async def get_graph_node(
|
||||
self,
|
||||
node_id: str,
|
||||
user: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
Get detailed information about a graph node.
|
||||
|
||||
Args:
|
||||
node_id: Node ID
|
||||
user: User identifier (defaults to request context)
|
||||
|
||||
Returns:
|
||||
Node with relationships and connected nodes
|
||||
"""
|
||||
user = user or get_user()
|
||||
client = self._ensure_client()
|
||||
|
||||
response = await client.get(
|
||||
f"/graph/nodes/{node_id}",
|
||||
params={"user": user},
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json()
|
||||
|
||||
# ========================================================================
|
||||
# Health Check
|
||||
# ========================================================================
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
"""
|
||||
Check if library-desk is healthy.
|
||||
|
||||
Returns:
|
||||
True if healthy, False otherwise
|
||||
"""
|
||||
try:
|
||||
client = self._ensure_client()
|
||||
response = await client.get("/health")
|
||||
return response.status_code == 200
|
||||
except Exception as e:
|
||||
logger.warning("library_desk_health_check_failed", error=str(e))
|
||||
return False
|
||||
|
||||
|
||||
# Global client factory
|
||||
async def get_library_client() -> LibraryDeskClient:
|
||||
"""
|
||||
Get a library-desk client instance.
|
||||
|
||||
Usage:
|
||||
async with get_library_client() as client:
|
||||
results = await client.hybrid_search("query")
|
||||
"""
|
||||
return LibraryDeskClient()
|
||||
@@ -0,0 +1,701 @@
|
||||
"""
|
||||
Librarian tools for PydanticAI agent.
|
||||
|
||||
These tools wrap the library-desk API and are registered with
|
||||
The Librarian agent for research and knowledge management tasks.
|
||||
"""
|
||||
from src.agents.librarian.client import LibraryDeskClient
|
||||
from src.core.logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# HybridRAG Search
|
||||
# ============================================================================
|
||||
|
||||
async def hybrid_search(
|
||||
query: str,
|
||||
include_web: bool = True,
|
||||
) -> str:
|
||||
"""
|
||||
Search across all knowledge sources using HybridRAG.
|
||||
|
||||
This is the primary research tool, combining:
|
||||
- Vector search (semantic similarity over documents)
|
||||
- Knowledge graph (entities and relationships)
|
||||
- Web search (current information from SearXNG)
|
||||
|
||||
Results are fused and re-ranked by relevance.
|
||||
|
||||
Args:
|
||||
query: Natural language research query
|
||||
include_web: Whether to include web results (default: True)
|
||||
|
||||
Returns:
|
||||
Formatted search results with sources and context
|
||||
|
||||
Examples:
|
||||
hybrid_search("How does Docker orchestration work with Kubernetes?")
|
||||
hybrid_search("What projects use Neo4j?", include_web=False)
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
response = await client.hybrid_search(
|
||||
query=query,
|
||||
web_limit=5 if include_web else 0,
|
||||
)
|
||||
|
||||
if not response.results:
|
||||
return f"No results found for '{query}'"
|
||||
|
||||
# Format results
|
||||
output_parts = [f"## Search Results for: {query}\n"]
|
||||
|
||||
# Add keywords if extracted
|
||||
if response.keywords:
|
||||
output_parts.append(f"**Keywords:** {', '.join(response.keywords)}")
|
||||
|
||||
# Add related dossiers
|
||||
if response.related_dossiers:
|
||||
output_parts.append(
|
||||
f"**Related Dossiers:** {', '.join(response.related_dossiers)}"
|
||||
)
|
||||
|
||||
output_parts.append("")
|
||||
|
||||
# Add results
|
||||
for i, result in enumerate(response.results, 1):
|
||||
source_icon = {
|
||||
"vector": "📄",
|
||||
"graph": "🔗",
|
||||
"web": "🌐",
|
||||
}.get(result.source, "•")
|
||||
|
||||
output_parts.append(
|
||||
f"{i}. {source_icon} **{result.title}** (score: {result.score:.2f})"
|
||||
)
|
||||
if result.url:
|
||||
output_parts.append(f" URL: {result.url}")
|
||||
output_parts.append(f" {result.content[:300]}...")
|
||||
output_parts.append("")
|
||||
|
||||
logger.info(
|
||||
"librarian_hybrid_search",
|
||||
query=query,
|
||||
result_count=len(response.results),
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_hybrid_search_error", error=str(e), query=query)
|
||||
return f"Error searching: {str(e)}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Wiki Operations
|
||||
# ============================================================================
|
||||
|
||||
async def search_wiki(
|
||||
query: str,
|
||||
limit: int = 10,
|
||||
) -> str:
|
||||
"""
|
||||
Search the personal wiki for relevant pages.
|
||||
|
||||
Performs full-text search over wiki page titles, descriptions,
|
||||
and content. Use this for finding specific documents.
|
||||
|
||||
Args:
|
||||
query: Search query
|
||||
limit: Maximum results (default: 10)
|
||||
|
||||
Returns:
|
||||
List of matching wiki pages with paths and descriptions
|
||||
|
||||
Examples:
|
||||
search_wiki("docker setup guide")
|
||||
search_wiki("architecture", limit=5)
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
results = await client.search_wiki(query=query, limit=limit)
|
||||
|
||||
if not results:
|
||||
return f"No wiki pages found for '{query}'"
|
||||
|
||||
output_parts = [f"## Wiki Search: {query}\n"]
|
||||
|
||||
for i, page in enumerate(results, 1):
|
||||
output_parts.append(f"{i}. **{page.title}**")
|
||||
output_parts.append(f" Path: {page.path}")
|
||||
if page.description:
|
||||
output_parts.append(f" {page.description}")
|
||||
output_parts.append("")
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_wiki_search_error", error=str(e))
|
||||
return f"Error searching wiki: {str(e)}"
|
||||
|
||||
|
||||
async def get_wiki_page(
|
||||
page_id: int,
|
||||
) -> str:
|
||||
"""
|
||||
Get the full content of a wiki page.
|
||||
|
||||
Use this after searching to read the complete content
|
||||
of a specific page.
|
||||
|
||||
Args:
|
||||
page_id: The page ID from search results
|
||||
|
||||
Returns:
|
||||
Full page content including title, path, and markdown content
|
||||
|
||||
Examples:
|
||||
get_wiki_page(42)
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
page = await client.get_wiki_page(page_id=page_id)
|
||||
|
||||
output_parts = [
|
||||
f"# {page.title}",
|
||||
f"**Path:** {page.path}",
|
||||
]
|
||||
|
||||
if page.description:
|
||||
output_parts.append(f"**Description:** {page.description}")
|
||||
|
||||
if page.tags:
|
||||
output_parts.append(f"**Tags:** {', '.join(page.tags)}")
|
||||
|
||||
output_parts.append("")
|
||||
output_parts.append(page.content or "(No content)")
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_get_page_error", error=str(e), page_id=page_id)
|
||||
return f"Error getting page {page_id}: {str(e)}"
|
||||
|
||||
|
||||
async def list_dossiers() -> str:
|
||||
"""
|
||||
List all research dossiers (tag collections).
|
||||
|
||||
Dossiers are collections of wiki pages grouped by tag.
|
||||
Use this to discover what knowledge collections exist.
|
||||
|
||||
Returns:
|
||||
List of dossiers with page counts
|
||||
|
||||
Examples:
|
||||
list_dossiers()
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
dossiers = await client.list_dossiers()
|
||||
|
||||
if not dossiers:
|
||||
return "No dossiers found"
|
||||
|
||||
output_parts = ["## Research Dossiers\n"]
|
||||
|
||||
for dossier in dossiers:
|
||||
output_parts.append(
|
||||
f"- **{dossier.name}** ({dossier.page_count} pages)"
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_list_dossiers_error", error=str(e))
|
||||
return f"Error listing dossiers: {str(e)}"
|
||||
|
||||
|
||||
async def get_dossier_pages(
|
||||
dossier_name: str,
|
||||
limit: int = 20,
|
||||
) -> str:
|
||||
"""
|
||||
Get all pages in a dossier.
|
||||
|
||||
Retrieves pages tagged with the specified dossier name.
|
||||
|
||||
Args:
|
||||
dossier_name: Name of the dossier/tag
|
||||
limit: Maximum pages to return
|
||||
|
||||
Returns:
|
||||
List of pages in the dossier
|
||||
|
||||
Examples:
|
||||
get_dossier_pages("projects")
|
||||
get_dossier_pages("architecture", limit=10)
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
pages = await client.list_wiki_pages(tag=dossier_name, limit=limit)
|
||||
|
||||
if not pages:
|
||||
return f"No pages found in dossier '{dossier_name}'"
|
||||
|
||||
output_parts = [f"## Dossier: {dossier_name}\n"]
|
||||
|
||||
for page in pages:
|
||||
output_parts.append(f"- **{page.title}** ({page.path})")
|
||||
if page.description:
|
||||
output_parts.append(f" {page.description}")
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_get_dossier_error", error=str(e))
|
||||
return f"Error getting dossier: {str(e)}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Semantic Search
|
||||
# ============================================================================
|
||||
|
||||
async def semantic_search(
|
||||
query: str,
|
||||
limit: int = 10,
|
||||
) -> str:
|
||||
"""
|
||||
Perform semantic (vector) search over documents.
|
||||
|
||||
Finds documents similar in meaning to the query,
|
||||
even if they don't contain the exact words.
|
||||
|
||||
Args:
|
||||
query: Natural language query
|
||||
limit: Maximum results
|
||||
|
||||
Returns:
|
||||
Matching document chunks with similarity scores
|
||||
|
||||
Examples:
|
||||
semantic_search("containerization best practices")
|
||||
semantic_search("how to handle authentication")
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
results = await client.semantic_search(query=query, limit=limit)
|
||||
|
||||
if not results:
|
||||
return f"No semantically similar content found for '{query}'"
|
||||
|
||||
output_parts = [f"## Semantic Search: {query}\n"]
|
||||
|
||||
for i, result in enumerate(results, 1):
|
||||
output_parts.append(
|
||||
f"{i}. **{result.page_title}** (score: {result.score:.2f})"
|
||||
)
|
||||
output_parts.append(f" Path: {result.page_path}")
|
||||
output_parts.append(f" {result.chunk_text[:200]}...")
|
||||
output_parts.append("")
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_semantic_search_error", error=str(e))
|
||||
return f"Error in semantic search: {str(e)}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Knowledge Graph
|
||||
# ============================================================================
|
||||
|
||||
async def explore_knowledge_graph(
|
||||
entity_type: str = "Document",
|
||||
limit: int = 20,
|
||||
) -> str:
|
||||
"""
|
||||
Explore entities in the knowledge graph.
|
||||
|
||||
Lists nodes of a specific type to understand what's
|
||||
in the knowledge base.
|
||||
|
||||
Args:
|
||||
entity_type: Type of entity (Document, Person, Project, Concept, Technology)
|
||||
limit: Maximum nodes to return
|
||||
|
||||
Returns:
|
||||
List of entities with their properties
|
||||
|
||||
Examples:
|
||||
explore_knowledge_graph("Person")
|
||||
explore_knowledge_graph("Technology", limit=50)
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
nodes = await client.list_graph_nodes(
|
||||
node_type=entity_type,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
if not nodes:
|
||||
return f"No {entity_type} nodes found in knowledge graph"
|
||||
|
||||
output_parts = [f"## Knowledge Graph: {entity_type} Entities\n"]
|
||||
|
||||
for node in nodes:
|
||||
name = node.properties.get("name", node.properties.get("title", node.id))
|
||||
output_parts.append(f"- **{name}**")
|
||||
|
||||
# Show a few key properties
|
||||
for key in ["description", "url", "path"]:
|
||||
if key in node.properties:
|
||||
output_parts.append(f" {key}: {node.properties[key]}")
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_explore_graph_error", error=str(e))
|
||||
return f"Error exploring knowledge graph: {str(e)}"
|
||||
|
||||
|
||||
async def find_related_entities(
|
||||
entity_name: str,
|
||||
) -> str:
|
||||
"""
|
||||
Find entities related to a given concept or entity.
|
||||
|
||||
Queries the knowledge graph to find documents, people,
|
||||
and concepts connected to the specified entity.
|
||||
|
||||
Args:
|
||||
entity_name: Name of the entity to find relationships for
|
||||
|
||||
Returns:
|
||||
Related entities and their relationships
|
||||
|
||||
Examples:
|
||||
find_related_entities("Docker")
|
||||
find_related_entities("Kubernetes")
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
# Find entities mentioning or related to the search term
|
||||
cypher = """
|
||||
MATCH (n)
|
||||
WHERE toLower(n.name) CONTAINS toLower($name)
|
||||
OR toLower(n.title) CONTAINS toLower($name)
|
||||
OPTIONAL MATCH (n)-[r]-(related)
|
||||
RETURN n, collect(DISTINCT {type: type(r), node: related})[0..10] as relationships
|
||||
LIMIT 10
|
||||
"""
|
||||
|
||||
results = await client.query_graph(
|
||||
cypher,
|
||||
parameters={"name": entity_name},
|
||||
)
|
||||
|
||||
if not results:
|
||||
return f"No entities found related to '{entity_name}'"
|
||||
|
||||
output_parts = [f"## Entities Related to: {entity_name}\n"]
|
||||
|
||||
for record in results:
|
||||
node = record.get("n", {})
|
||||
relationships = record.get("relationships", [])
|
||||
|
||||
name = node.get("name", node.get("title", "Unknown"))
|
||||
labels = node.get("labels", [])
|
||||
|
||||
output_parts.append(f"### {name}")
|
||||
if labels:
|
||||
output_parts.append(f"Type: {', '.join(labels)}")
|
||||
|
||||
if relationships:
|
||||
output_parts.append("**Connections:**")
|
||||
for rel in relationships[:5]: # Limit to 5 relationships
|
||||
rel_type = rel.get("type", "RELATED_TO")
|
||||
related_node = rel.get("node", {})
|
||||
related_name = related_node.get(
|
||||
"name", related_node.get("title", "Unknown")
|
||||
)
|
||||
output_parts.append(f" - {rel_type} → {related_name}")
|
||||
|
||||
output_parts.append("")
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_find_related_error", error=str(e))
|
||||
return f"Error finding related entities: {str(e)}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Wiki Write Operations
|
||||
# ============================================================================
|
||||
|
||||
async def update_wiki_page(
|
||||
page_id: int,
|
||||
content: str | None = None,
|
||||
title: str | None = None,
|
||||
tags: list[str] | None = None,
|
||||
description: str | None = None,
|
||||
) -> str:
|
||||
"""
|
||||
Update an existing wiki page.
|
||||
|
||||
Supports partial updates - only specify the fields you want to change.
|
||||
Changes trigger automatic vector re-indexing and knowledge graph updates.
|
||||
|
||||
Use this for:
|
||||
- Correcting information in a page
|
||||
- Adding content to an existing page
|
||||
- Updating tags to organize pages into dossiers
|
||||
- Fixing descriptions or titles
|
||||
|
||||
Args:
|
||||
page_id: ID of the page to update (get from search_wiki results)
|
||||
content: New markdown content (optional - only if changing content)
|
||||
title: New title (optional - only if renaming)
|
||||
tags: New tag list (optional - replaces existing tags)
|
||||
description: New description (optional)
|
||||
|
||||
Returns:
|
||||
Confirmation with updated page details
|
||||
|
||||
Examples:
|
||||
update_wiki_page(42, content="# Updated Content\\n\\nNew information here")
|
||||
update_wiki_page(42, tags=["projects", "devops"]) # Add to dossiers
|
||||
update_wiki_page(42, description="Updated description")
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
page = await client.update_wiki_page(
|
||||
page_id=page_id,
|
||||
content=content,
|
||||
title=title,
|
||||
tags=tags,
|
||||
description=description,
|
||||
)
|
||||
|
||||
# Build update summary
|
||||
updated_fields = []
|
||||
if content is not None:
|
||||
updated_fields.append("content")
|
||||
if title is not None:
|
||||
updated_fields.append("title")
|
||||
if tags is not None:
|
||||
updated_fields.append("tags")
|
||||
if description is not None:
|
||||
updated_fields.append("description")
|
||||
|
||||
output_parts = [
|
||||
f"## Page Updated: {page.title}",
|
||||
f"**Path:** {page.path}",
|
||||
f"**Updated fields:** {', '.join(updated_fields)}",
|
||||
]
|
||||
|
||||
if page.tags:
|
||||
output_parts.append(f"**Tags:** {', '.join(page.tags)}")
|
||||
|
||||
output_parts.append("\n*Vector embeddings and knowledge graph will be updated automatically.*")
|
||||
|
||||
logger.info(
|
||||
"librarian_update_page",
|
||||
page_id=page_id,
|
||||
updated_fields=updated_fields,
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_update_page_error", error=str(e), page_id=page_id)
|
||||
return f"Error updating page {page_id}: {str(e)}"
|
||||
|
||||
|
||||
async def create_wiki_page(
|
||||
title: str,
|
||||
path: str,
|
||||
content: str,
|
||||
tags: list[str],
|
||||
description: str = "",
|
||||
) -> str:
|
||||
"""
|
||||
Create a new wiki page with user-provided content.
|
||||
|
||||
Use this when:
|
||||
- User provides specific content to add
|
||||
- Creating simple notes or reminders
|
||||
- The content is already known/composed
|
||||
|
||||
For research-backed pages where you need to gather information first,
|
||||
use smart_create_wiki_page instead.
|
||||
|
||||
Args:
|
||||
title: Page title
|
||||
path: Page path (e.g., "/projects/my-project" or "/notes/meeting-2024")
|
||||
content: Markdown content for the page
|
||||
tags: List of tags/dossiers (e.g., ["projects", "devops"])
|
||||
description: Short description of the page
|
||||
|
||||
Returns:
|
||||
Confirmation with created page details
|
||||
|
||||
Examples:
|
||||
create_wiki_page(
|
||||
title="SSL Renewal Reminder",
|
||||
path="/reminders/ssl-renewal",
|
||||
content="# SSL Renewal\\n\\nRemember to renew SSL cert on Jan 15",
|
||||
tags=["reminders", "infrastructure"],
|
||||
description="Certificate renewal reminder"
|
||||
)
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
page = await client.create_wiki_page(
|
||||
title=title,
|
||||
path=path,
|
||||
content=content,
|
||||
tags=tags,
|
||||
description=description,
|
||||
)
|
||||
|
||||
output_parts = [
|
||||
f"## Page Created: {page.title}",
|
||||
f"**ID:** {page.id}",
|
||||
f"**Path:** {page.path}",
|
||||
]
|
||||
|
||||
if page.tags:
|
||||
output_parts.append(f"**Tags:** {', '.join(page.tags)}")
|
||||
|
||||
if page.description:
|
||||
output_parts.append(f"**Description:** {page.description}")
|
||||
|
||||
output_parts.append("\n*Vector embeddings and knowledge graph will be updated automatically.*")
|
||||
|
||||
logger.info(
|
||||
"librarian_create_page",
|
||||
page_id=page.id,
|
||||
title=title,
|
||||
path=path,
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_create_page_error", error=str(e), title=title)
|
||||
return f"Error creating page: {str(e)}"
|
||||
|
||||
|
||||
async def smart_create_wiki_page(
|
||||
topic: str,
|
||||
tags: list[str],
|
||||
path: str | None = None,
|
||||
include_web_research: bool = True,
|
||||
include_wiki_search: bool = True,
|
||||
) -> str:
|
||||
"""
|
||||
Create a wiki page with automatic research and content synthesis.
|
||||
|
||||
This is the RECOMMENDED way to create pages about topics. It will:
|
||||
1. Search existing wiki, knowledge graph, and web for relevant information
|
||||
2. Use an LLM to synthesize findings into well-structured content
|
||||
3. Create the page with proper source attribution
|
||||
4. Automatically link entities bidirectionally in the knowledge graph
|
||||
|
||||
Use this when:
|
||||
- User says "Create a page about X"
|
||||
- User says "Add information about X to the wiki"
|
||||
- You need to research a topic before writing
|
||||
- The topic would benefit from existing knowledge context
|
||||
|
||||
Args:
|
||||
topic: The topic to research and create a page about
|
||||
tags: List of tags/dossiers for categorization
|
||||
path: Optional custom path (auto-generated from topic if not provided)
|
||||
include_web_research: Whether to search the web (default: True)
|
||||
include_wiki_search: Whether to search existing wiki (default: True)
|
||||
|
||||
Returns:
|
||||
Summary of created page with research statistics
|
||||
|
||||
Examples:
|
||||
smart_create_wiki_page("Docker Compose", tags=["technology", "devops"])
|
||||
smart_create_wiki_page("Home network architecture", tags=["infrastructure"], include_web_research=False)
|
||||
"""
|
||||
try:
|
||||
async with LibraryDeskClient() as client:
|
||||
response = await client.smart_create_wiki_page(
|
||||
topic=topic,
|
||||
tags=tags,
|
||||
path=path,
|
||||
include_web_research=include_web_research,
|
||||
include_wiki_search=include_wiki_search,
|
||||
)
|
||||
|
||||
page = response.page
|
||||
research = response.research_summary
|
||||
linking = response.entity_linking
|
||||
|
||||
output_parts = [
|
||||
f"## Page Created: {page.title}",
|
||||
f"**ID:** {page.id}",
|
||||
f"**Path:** {page.path}",
|
||||
]
|
||||
|
||||
if page.tags:
|
||||
output_parts.append(f"**Tags:** {', '.join(page.tags)}")
|
||||
|
||||
# Research summary
|
||||
output_parts.append("\n### Research Summary")
|
||||
output_parts.append(f"- **Wiki results used:** {research.wiki_results}")
|
||||
output_parts.append(f"- **Web results used:** {research.web_results}")
|
||||
output_parts.append(f"- **Graph entities found:** {research.graph_entities}")
|
||||
output_parts.append(f"- **Keywords extracted:** {research.keywords_extracted}")
|
||||
output_parts.append(f"- **Total sources:** {response.sources_used}")
|
||||
output_parts.append(f"- **Research time:** {research.timing_ms}ms")
|
||||
|
||||
# Entity linking
|
||||
if linking.forward_links > 0 or linking.backward_links > 0:
|
||||
output_parts.append("\n### Knowledge Graph Updates")
|
||||
output_parts.append(f"- **Forward links created:** {linking.forward_links}")
|
||||
output_parts.append(f"- **Backward links created:** {linking.backward_links}")
|
||||
output_parts.append(f"- **Related pages updated:** {linking.pages_updated}")
|
||||
|
||||
logger.info(
|
||||
"librarian_smart_create",
|
||||
topic=topic,
|
||||
page_id=page.id,
|
||||
sources_used=response.sources_used,
|
||||
)
|
||||
|
||||
return "\n".join(output_parts)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("librarian_smart_create_error", error=str(e), topic=topic)
|
||||
return f"Error creating page about '{topic}': {str(e)}"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Tool Collection for Registration
|
||||
# ============================================================================
|
||||
|
||||
# All tools available to The Librarian
|
||||
LIBRARIAN_TOOLS = [
|
||||
# Research tools
|
||||
hybrid_search,
|
||||
search_wiki,
|
||||
get_wiki_page,
|
||||
list_dossiers,
|
||||
get_dossier_pages,
|
||||
semantic_search,
|
||||
explore_knowledge_graph,
|
||||
find_related_entities,
|
||||
# Write tools
|
||||
create_wiki_page,
|
||||
update_wiki_page,
|
||||
smart_create_wiki_page,
|
||||
]
|
||||
@@ -0,0 +1,517 @@
|
||||
"""
|
||||
Orchestration module for multi-expert agent coordination.
|
||||
|
||||
Provides infrastructure for Tatlock to orchestrate expert agents
|
||||
with streaming think updates to keep users informed of progress.
|
||||
|
||||
Key pattern: Stream user-facing interactions, use run() internally
|
||||
to avoid Ollama streaming+tool call bugs.
|
||||
|
||||
Supports:
|
||||
- Single expert delegation with think updates
|
||||
- Sequential multi-expert execution (task A → task B → task C)
|
||||
- Parallel multi-expert execution (tasks A, B, C concurrently)
|
||||
- Result aggregation from multiple experts
|
||||
- Partial failure handling
|
||||
"""
|
||||
import asyncio
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import AsyncGenerator, Optional, Callable, Any
|
||||
|
||||
from src.agents.delegation import DelegationTask, DelegationResult, delegate_to_librarian
|
||||
from src.core.logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class ExecutionMode(str, Enum):
|
||||
"""Execution mode for multi-expert coordination."""
|
||||
SEQUENTIAL = "sequential" # One at a time, in order
|
||||
PARALLEL = "parallel" # All at once, concurrently
|
||||
|
||||
|
||||
@dataclass
|
||||
class OrchestrationContext:
|
||||
"""
|
||||
Context for an orchestration session.
|
||||
|
||||
Tracks the user's request, delegation tasks, and results.
|
||||
"""
|
||||
user_message: str
|
||||
steward_note: str
|
||||
conversation_id: Optional[str] = None
|
||||
|
||||
|
||||
def parse_delegation_from_steward_note(steward_note: str) -> Optional[DelegationTask]:
|
||||
"""
|
||||
Parse a delegation task from Steward's note.
|
||||
|
||||
Looks for the DELEGATE: pattern in the Steward's recommendation.
|
||||
|
||||
Args:
|
||||
steward_note: Formatted note from Steward
|
||||
|
||||
Returns:
|
||||
DelegationTask if delegation found, None otherwise
|
||||
|
||||
Example:
|
||||
>>> note = "DELEGATE: librarian to create a wiki page about CI/CD"
|
||||
>>> task = parse_delegation_from_steward_note(note)
|
||||
>>> task.expert_name
|
||||
'librarian'
|
||||
>>> task.task
|
||||
'create a wiki page about CI/CD'
|
||||
"""
|
||||
import re
|
||||
|
||||
# Look for DELEGATE: pattern
|
||||
# Match: "DELEGATE: expert_name to action description"
|
||||
match = re.search(
|
||||
r'DELEGATE:\s*(\w+)\s+to\s+(.+?)(?:\n|REASON:|COMPLEXITY:|CONTEXT:|$)',
|
||||
steward_note,
|
||||
re.IGNORECASE | re.MULTILINE
|
||||
)
|
||||
|
||||
if match:
|
||||
expert_name = match.group(1).lower()
|
||||
task_description = match.group(2).strip()
|
||||
|
||||
# Handle "none" case
|
||||
if expert_name == "none":
|
||||
return None
|
||||
|
||||
return DelegationTask(
|
||||
expert_name=expert_name,
|
||||
task=task_description,
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
async def execute_delegation(
|
||||
task: DelegationTask,
|
||||
) -> DelegationResult:
|
||||
"""
|
||||
Execute a delegation task.
|
||||
|
||||
Routes to the appropriate expert agent based on expert_name.
|
||||
|
||||
Args:
|
||||
task: Delegation task to execute
|
||||
|
||||
Returns:
|
||||
DelegationResult from the expert agent
|
||||
"""
|
||||
logger.info(
|
||||
"executing_delegation",
|
||||
expert=task.expert_name,
|
||||
task=task.task[:50],
|
||||
)
|
||||
|
||||
if task.expert_name == "librarian":
|
||||
return await delegate_to_librarian(
|
||||
task=task.task,
|
||||
context=task.context,
|
||||
)
|
||||
|
||||
# Future experts would be added here:
|
||||
# elif task.expert_name == "memory":
|
||||
# return await delegate_to_memory(task.task, task.context)
|
||||
# elif task.expert_name == "home_automation":
|
||||
# return await delegate_to_home_automation(task.task, task.context)
|
||||
|
||||
# Unknown expert - return error result
|
||||
logger.warning("unknown_expert", expert=task.expert_name)
|
||||
return DelegationResult(
|
||||
expert_name=task.expert_name,
|
||||
task=task.task,
|
||||
success=False,
|
||||
output="",
|
||||
error=f"Unknown expert: {task.expert_name}",
|
||||
)
|
||||
|
||||
|
||||
async def orchestrate_with_think_updates(
|
||||
user_message: str,
|
||||
steward_note: str,
|
||||
delegation_task: Optional[DelegationTask] = None,
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""
|
||||
Orchestrate expert delegation with streaming think updates.
|
||||
|
||||
Emits <think> updates before and after delegation calls to
|
||||
keep the user informed of progress. Expert calls use run()
|
||||
internally to avoid Ollama streaming bugs.
|
||||
|
||||
Args:
|
||||
user_message: Original user message
|
||||
steward_note: Steward's analysis and instructions
|
||||
delegation_task: Optional pre-parsed delegation task
|
||||
|
||||
Yields:
|
||||
Think update strings and final expert output
|
||||
|
||||
Example:
|
||||
>>> async for update in orchestrate_with_think_updates(
|
||||
... "Create a wiki page about CI/CD",
|
||||
... "DELEGATE: librarian to create wiki page",
|
||||
... ):
|
||||
... print(update)
|
||||
<think>Consulting The Librarian...</think>
|
||||
<think>Delegation complete.</think>
|
||||
[Wiki page created successfully...]
|
||||
"""
|
||||
# Parse delegation if not provided
|
||||
if delegation_task is None:
|
||||
delegation_task = parse_delegation_from_steward_note(steward_note)
|
||||
|
||||
if delegation_task is None:
|
||||
# No delegation needed - nothing to orchestrate
|
||||
logger.debug("no_delegation_needed")
|
||||
return
|
||||
|
||||
# Stream: About to delegate
|
||||
expert_display_name = delegation_task.expert_name.title()
|
||||
if delegation_task.expert_name == "librarian":
|
||||
expert_display_name = "The Librarian"
|
||||
|
||||
yield f"<think>🤝 Consulting {expert_display_name}...</think>\n"
|
||||
|
||||
# Execute delegation (uses run() internally)
|
||||
result = await execute_delegation(delegation_task)
|
||||
|
||||
if result.success:
|
||||
yield f"<think>✅ {expert_display_name} completed research.</think>\n"
|
||||
|
||||
# Yield the expert's findings
|
||||
if result.output:
|
||||
yield f"\n{result.output}"
|
||||
else:
|
||||
yield f"<think>⚠️ {expert_display_name} encountered an issue: {result.error}</think>\n"
|
||||
|
||||
logger.info(
|
||||
"orchestration_complete",
|
||||
expert=delegation_task.expert_name,
|
||||
success=result.success,
|
||||
)
|
||||
|
||||
|
||||
def extract_delegation_context(
|
||||
steward_note: str,
|
||||
) -> dict[str, str]:
|
||||
"""
|
||||
Extract context fields from Steward's note.
|
||||
|
||||
Args:
|
||||
steward_note: Formatted note from Steward
|
||||
|
||||
Returns:
|
||||
Dict with reason, complexity, and context
|
||||
"""
|
||||
import re
|
||||
|
||||
result = {
|
||||
"reason": "",
|
||||
"complexity": "",
|
||||
"context": "",
|
||||
}
|
||||
|
||||
# Extract REASON:
|
||||
reason_match = re.search(r'REASON:\s*(.+?)(?:\n|COMPLEXITY:|CONTEXT:|$)', steward_note, re.IGNORECASE)
|
||||
if reason_match:
|
||||
result["reason"] = reason_match.group(1).strip()
|
||||
|
||||
# Extract COMPLEXITY:
|
||||
complexity_match = re.search(r'COMPLEXITY:\s*(.+?)(?:\n|CONTEXT:|$)', steward_note, re.IGNORECASE)
|
||||
if complexity_match:
|
||||
result["complexity"] = complexity_match.group(1).strip()
|
||||
|
||||
# Extract CONTEXT:
|
||||
context_match = re.search(r'CONTEXT:\s*(.+?)$', steward_note, re.IGNORECASE | re.MULTILINE)
|
||||
if context_match:
|
||||
result["context"] = context_match.group(1).strip()
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Multi-Expert Coordination
|
||||
# ============================================================================
|
||||
|
||||
@dataclass
|
||||
class MultiExpertResult:
|
||||
"""
|
||||
Aggregated result from multiple expert delegations.
|
||||
|
||||
Attributes:
|
||||
results: Dict mapping expert name to their result
|
||||
all_succeeded: True if all delegations succeeded
|
||||
failed_experts: List of expert names that failed
|
||||
combined_output: Aggregated output from all successful experts
|
||||
"""
|
||||
results: dict[str, DelegationResult] = field(default_factory=dict)
|
||||
all_succeeded: bool = True
|
||||
failed_experts: list[str] = field(default_factory=list)
|
||||
combined_output: str = ""
|
||||
|
||||
def add_result(self, result: DelegationResult) -> None:
|
||||
"""Add a result and update aggregation state."""
|
||||
self.results[result.expert_name] = result
|
||||
if not result.success:
|
||||
self.all_succeeded = False
|
||||
self.failed_experts.append(result.expert_name)
|
||||
|
||||
def aggregate_outputs(self, separator: str = "\n\n---\n\n") -> str:
|
||||
"""Combine all successful outputs into one string."""
|
||||
outputs = []
|
||||
for expert_name, result in self.results.items():
|
||||
if result.success and result.output:
|
||||
outputs.append(f"**{expert_name.title()}**: {result.output}")
|
||||
|
||||
self.combined_output = separator.join(outputs)
|
||||
return self.combined_output
|
||||
|
||||
|
||||
async def execute_sequential(
|
||||
tasks: list[DelegationTask],
|
||||
stop_on_failure: bool = False,
|
||||
) -> MultiExpertResult:
|
||||
"""
|
||||
Execute multiple delegation tasks sequentially.
|
||||
|
||||
Tasks run one after another in order. Later tasks can depend on
|
||||
earlier results (though this function doesn't handle passing
|
||||
results between tasks - that's the orchestrator's job).
|
||||
|
||||
Args:
|
||||
tasks: List of delegation tasks to execute in order
|
||||
stop_on_failure: If True, stop execution if any task fails
|
||||
|
||||
Returns:
|
||||
MultiExpertResult with all task results
|
||||
|
||||
Example:
|
||||
>>> tasks = [
|
||||
... DelegationTask(expert_name="memory", task="get user location"),
|
||||
... DelegationTask(expert_name="librarian", task="search weather"),
|
||||
... ]
|
||||
>>> result = await execute_sequential(tasks)
|
||||
>>> result.all_succeeded
|
||||
True
|
||||
"""
|
||||
multi_result = MultiExpertResult()
|
||||
|
||||
logger.info(
|
||||
"sequential_execution_started",
|
||||
task_count=len(tasks),
|
||||
experts=[t.expert_name for t in tasks],
|
||||
)
|
||||
|
||||
for i, task in enumerate(tasks):
|
||||
logger.debug(
|
||||
"sequential_task_executing",
|
||||
index=i,
|
||||
expert=task.expert_name,
|
||||
task=task.task[:50],
|
||||
)
|
||||
|
||||
result = await execute_delegation(task)
|
||||
multi_result.add_result(result)
|
||||
|
||||
if not result.success and stop_on_failure:
|
||||
logger.warning(
|
||||
"sequential_execution_stopped",
|
||||
failed_at=i,
|
||||
expert=task.expert_name,
|
||||
error=result.error,
|
||||
)
|
||||
break
|
||||
|
||||
multi_result.aggregate_outputs()
|
||||
|
||||
logger.info(
|
||||
"sequential_execution_complete",
|
||||
total_tasks=len(tasks),
|
||||
succeeded=len(tasks) - len(multi_result.failed_experts),
|
||||
failed=len(multi_result.failed_experts),
|
||||
)
|
||||
|
||||
return multi_result
|
||||
|
||||
|
||||
async def execute_parallel(
|
||||
tasks: list[DelegationTask],
|
||||
) -> MultiExpertResult:
|
||||
"""
|
||||
Execute multiple delegation tasks in parallel.
|
||||
|
||||
All tasks run concurrently using asyncio.gather. Use this when
|
||||
tasks are independent and don't depend on each other's results.
|
||||
|
||||
Args:
|
||||
tasks: List of delegation tasks to execute concurrently
|
||||
|
||||
Returns:
|
||||
MultiExpertResult with all task results
|
||||
|
||||
Example:
|
||||
>>> tasks = [
|
||||
... DelegationTask(expert_name="librarian", task="search wiki"),
|
||||
... DelegationTask(expert_name="memory", task="get preferences"),
|
||||
... ]
|
||||
>>> result = await execute_parallel(tasks)
|
||||
>>> len(result.results)
|
||||
2
|
||||
"""
|
||||
multi_result = MultiExpertResult()
|
||||
|
||||
logger.info(
|
||||
"parallel_execution_started",
|
||||
task_count=len(tasks),
|
||||
experts=[t.expert_name for t in tasks],
|
||||
)
|
||||
|
||||
# Execute all tasks concurrently
|
||||
results = await asyncio.gather(
|
||||
*[execute_delegation(task) for task in tasks],
|
||||
return_exceptions=True,
|
||||
)
|
||||
|
||||
# Process results
|
||||
for i, result in enumerate(results):
|
||||
if isinstance(result, Exception):
|
||||
# Handle exceptions as failed delegations
|
||||
error_result = DelegationResult(
|
||||
expert_name=tasks[i].expert_name,
|
||||
task=tasks[i].task,
|
||||
success=False,
|
||||
output="",
|
||||
error=str(result),
|
||||
)
|
||||
multi_result.add_result(error_result)
|
||||
logger.error(
|
||||
"parallel_task_exception",
|
||||
expert=tasks[i].expert_name,
|
||||
error=str(result),
|
||||
)
|
||||
else:
|
||||
multi_result.add_result(result)
|
||||
|
||||
multi_result.aggregate_outputs()
|
||||
|
||||
logger.info(
|
||||
"parallel_execution_complete",
|
||||
total_tasks=len(tasks),
|
||||
succeeded=len(tasks) - len(multi_result.failed_experts),
|
||||
failed=len(multi_result.failed_experts),
|
||||
)
|
||||
|
||||
return multi_result
|
||||
|
||||
|
||||
async def orchestrate_multi_expert(
|
||||
tasks: list[DelegationTask],
|
||||
mode: ExecutionMode = ExecutionMode.SEQUENTIAL,
|
||||
stop_on_failure: bool = False,
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""
|
||||
Orchestrate multiple expert delegations with streaming think updates.
|
||||
|
||||
Emits <think> updates for each delegation phase and yields
|
||||
combined results at the end.
|
||||
|
||||
Args:
|
||||
tasks: List of delegation tasks
|
||||
mode: SEQUENTIAL or PARALLEL execution
|
||||
stop_on_failure: For sequential mode, stop if a task fails
|
||||
|
||||
Yields:
|
||||
Think updates and combined expert output
|
||||
|
||||
Example:
|
||||
>>> tasks = [
|
||||
... DelegationTask(expert_name="memory", task="get location"),
|
||||
... DelegationTask(expert_name="librarian", task="search weather"),
|
||||
... ]
|
||||
>>> async for update in orchestrate_multi_expert(tasks):
|
||||
... print(update)
|
||||
<think>Starting multi-expert coordination (2 tasks)...</think>
|
||||
<think>Consulting Memory...</think>
|
||||
<think>Memory completed.</think>
|
||||
<think>Consulting The Librarian...</think>
|
||||
<think>The Librarian completed.</think>
|
||||
<think>All experts completed successfully.</think>
|
||||
[Combined output from all experts...]
|
||||
"""
|
||||
if not tasks:
|
||||
logger.debug("no_tasks_to_orchestrate")
|
||||
return
|
||||
|
||||
# Stream: Starting multi-expert coordination
|
||||
yield f"<think>🎯 Starting multi-expert coordination ({len(tasks)} tasks, {mode.value})...</think>\n"
|
||||
|
||||
if mode == ExecutionMode.PARALLEL:
|
||||
# Parallel execution - emit one update then run all at once
|
||||
expert_names = ", ".join(_get_display_name(t.expert_name) for t in tasks)
|
||||
yield f"<think>🔄 Consulting in parallel: {expert_names}...</think>\n"
|
||||
|
||||
result = await execute_parallel(tasks)
|
||||
|
||||
# Emit completion updates for each
|
||||
for expert_name, expert_result in result.results.items():
|
||||
display_name = _get_display_name(expert_name)
|
||||
if expert_result.success:
|
||||
yield f"<think>✅ {display_name} completed.</think>\n"
|
||||
else:
|
||||
yield f"<think>⚠️ {display_name} failed: {expert_result.error}</think>\n"
|
||||
|
||||
else:
|
||||
# Sequential execution - emit updates for each task
|
||||
result = MultiExpertResult()
|
||||
|
||||
for task in tasks:
|
||||
display_name = _get_display_name(task.expert_name)
|
||||
yield f"<think>🤝 Consulting {display_name}...</think>\n"
|
||||
|
||||
task_result = await execute_delegation(task)
|
||||
result.add_result(task_result)
|
||||
|
||||
if task_result.success:
|
||||
yield f"<think>✅ {display_name} completed.</think>\n"
|
||||
else:
|
||||
yield f"<think>⚠️ {display_name} failed: {task_result.error}</think>\n"
|
||||
if stop_on_failure:
|
||||
yield "<think>🛑 Stopping due to failure.</think>\n"
|
||||
break
|
||||
|
||||
result.aggregate_outputs()
|
||||
|
||||
# Stream: Summary
|
||||
if result.all_succeeded:
|
||||
yield "<think>🎉 All experts completed successfully.</think>\n"
|
||||
else:
|
||||
failed_names = ", ".join(_get_display_name(e) for e in result.failed_experts)
|
||||
yield f"<think>⚠️ Some experts failed: {failed_names}</think>\n"
|
||||
|
||||
# Yield combined output
|
||||
if result.combined_output:
|
||||
yield f"\n{result.combined_output}"
|
||||
|
||||
logger.info(
|
||||
"multi_expert_orchestration_complete",
|
||||
task_count=len(tasks),
|
||||
mode=mode.value,
|
||||
all_succeeded=result.all_succeeded,
|
||||
)
|
||||
|
||||
|
||||
def _get_display_name(expert_name: str) -> str:
|
||||
"""Get user-friendly display name for an expert."""
|
||||
display_names = {
|
||||
"librarian": "The Librarian",
|
||||
"memory": "Memory",
|
||||
"home_automation": "Home Automation",
|
||||
"tatlock_core": "Core Tools",
|
||||
}
|
||||
return display_names.get(expert_name, expert_name.title())
|
||||
@@ -0,0 +1,201 @@
|
||||
"""
|
||||
Agent communication protocol for multi-agent coordination.
|
||||
|
||||
Defines standardized request/response formats for communication between:
|
||||
- Steward (request analysis) → Tatlock (coordination)
|
||||
- Tatlock (coordination) → Expert agents (Librarian, Developer, etc.)
|
||||
"""
|
||||
from enum import Enum
|
||||
from typing import Any, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class DelegationReason(str, Enum):
|
||||
"""Why a task is being delegated to an expert agent."""
|
||||
DOMAIN_EXPERTISE = "domain_expertise" # Expert has specialized knowledge
|
||||
TOOL_ACCESS = "tool_access" # Expert has required tools
|
||||
RESOURCE_EFFICIENCY = "resource_efficiency" # Better handled by specialist
|
||||
USER_PREFERENCE = "user_preference" # User requested specific agent
|
||||
|
||||
|
||||
class TaskComplexity(str, Enum):
|
||||
"""Complexity estimate for task execution."""
|
||||
SIMPLE = "simple" # Single tool call, fast
|
||||
MODERATE = "moderate" # Multiple steps, moderate time
|
||||
COMPLEX = "complex" # Multi-agent, significant processing
|
||||
|
||||
|
||||
class AgentRequest(BaseModel):
|
||||
"""
|
||||
Request to an expert agent.
|
||||
|
||||
Contains everything the agent needs to execute a task,
|
||||
including context from the conversation and delegation intent.
|
||||
"""
|
||||
task: str = Field(
|
||||
...,
|
||||
description="Clear description of what the agent should do"
|
||||
)
|
||||
context: str = Field(
|
||||
default="",
|
||||
description="Relevant context from conversation history"
|
||||
)
|
||||
constraints: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Any constraints or requirements for the task"
|
||||
)
|
||||
delegation_reason: DelegationReason = Field(
|
||||
default=DelegationReason.DOMAIN_EXPERTISE,
|
||||
description="Why this task was delegated to this agent"
|
||||
)
|
||||
user_id: str = Field(
|
||||
default="default",
|
||||
description="User identifier for multi-tenant operations"
|
||||
)
|
||||
max_tokens: Optional[int] = Field(
|
||||
default=None,
|
||||
description="Optional token limit for response"
|
||||
)
|
||||
timeout_seconds: Optional[int] = Field(
|
||||
default=60,
|
||||
description="Maximum time for task completion"
|
||||
)
|
||||
|
||||
|
||||
class ToolCallRecord(BaseModel):
|
||||
"""Record of a tool call made during execution."""
|
||||
tool_name: str
|
||||
arguments: dict[str, Any]
|
||||
result: str
|
||||
duration_ms: int
|
||||
|
||||
|
||||
class AgentResponse(BaseModel):
|
||||
"""
|
||||
Response from an expert agent.
|
||||
|
||||
Contains the result, reasoning, and metadata about execution.
|
||||
"""
|
||||
success: bool = Field(
|
||||
...,
|
||||
description="Whether the task completed successfully"
|
||||
)
|
||||
result: str = Field(
|
||||
...,
|
||||
description="The main output/answer from the agent"
|
||||
)
|
||||
reasoning: str = Field(
|
||||
default="",
|
||||
description="Agent's reasoning process (for transparency)"
|
||||
)
|
||||
tool_calls: list[ToolCallRecord] = Field(
|
||||
default_factory=list,
|
||||
description="Tools called during execution"
|
||||
)
|
||||
confidence: float = Field(
|
||||
default=1.0,
|
||||
ge=0.0,
|
||||
le=1.0,
|
||||
description="Agent's confidence in the result (0.0-1.0)"
|
||||
)
|
||||
sources: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Sources or references used"
|
||||
)
|
||||
error_message: Optional[str] = Field(
|
||||
default=None,
|
||||
description="Error details if success=False"
|
||||
)
|
||||
duration_ms: int = Field(
|
||||
default=0,
|
||||
description="Total execution time in milliseconds"
|
||||
)
|
||||
|
||||
|
||||
class DelegationIntent(BaseModel):
|
||||
"""
|
||||
Intent to delegate a task to an expert agent.
|
||||
|
||||
Created by Tatlock when deciding to delegate, based on
|
||||
Steward's recommendations.
|
||||
"""
|
||||
target_agent: str = Field(
|
||||
...,
|
||||
description="Name of the expert agent to delegate to"
|
||||
)
|
||||
task: str = Field(
|
||||
...,
|
||||
description="Task description for the agent"
|
||||
)
|
||||
reason: DelegationReason = Field(
|
||||
default=DelegationReason.DOMAIN_EXPERTISE,
|
||||
description="Why delegating to this agent"
|
||||
)
|
||||
expected_outcome: str = Field(
|
||||
default="",
|
||||
description="What we expect the agent to provide"
|
||||
)
|
||||
priority: int = Field(
|
||||
default=1,
|
||||
ge=1,
|
||||
le=10,
|
||||
description="Priority (1=highest, 10=lowest)"
|
||||
)
|
||||
depends_on: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Other delegation IDs this depends on (for sequencing)"
|
||||
)
|
||||
|
||||
|
||||
class CoordinationResult(BaseModel):
|
||||
"""
|
||||
Result of multi-agent coordination.
|
||||
|
||||
Aggregates results from multiple expert agents into
|
||||
a single coherent response.
|
||||
"""
|
||||
final_response: str = Field(
|
||||
...,
|
||||
description="Synthesized response from all agents"
|
||||
)
|
||||
agent_responses: dict[str, AgentResponse] = Field(
|
||||
default_factory=dict,
|
||||
description="Individual responses keyed by agent name"
|
||||
)
|
||||
delegation_intents: list[DelegationIntent] = Field(
|
||||
default_factory=list,
|
||||
description="All delegations that were executed"
|
||||
)
|
||||
total_duration_ms: int = Field(
|
||||
default=0,
|
||||
description="Total coordination time"
|
||||
)
|
||||
agents_consulted: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Names of agents that contributed"
|
||||
)
|
||||
|
||||
|
||||
class AgentError(Exception):
|
||||
"""Base exception for agent errors."""
|
||||
|
||||
def __init__(self, message: str, agent_name: str = "unknown"):
|
||||
self.message = message
|
||||
self.agent_name = agent_name
|
||||
super().__init__(f"[{agent_name}] {message}")
|
||||
|
||||
|
||||
class AgentTimeoutError(AgentError):
|
||||
"""Agent execution timed out."""
|
||||
pass
|
||||
|
||||
|
||||
class AgentUnavailableError(AgentError):
|
||||
"""Agent is not available or registered."""
|
||||
pass
|
||||
|
||||
|
||||
class DelegationError(AgentError):
|
||||
"""Error during task delegation."""
|
||||
pass
|
||||
@@ -48,7 +48,7 @@ AVAILABLE HOUSEHOLD CAPABILITIES:
|
||||
{capabilities_text}
|
||||
|
||||
YOUR TASK:
|
||||
Analyze the user's query and recommend which capabilities are needed.
|
||||
Analyze the user's query and recommend which capabilities are needed, with specific delegation instructions.
|
||||
{history_text}
|
||||
|
||||
USER QUERY: {query}
|
||||
@@ -56,19 +56,30 @@ USER QUERY: {query}
|
||||
GUIDELINES:
|
||||
- Be conservative - only recommend truly necessary capabilities
|
||||
- Simple greetings/chat → no capabilities needed (conversational response only)
|
||||
- Questions about prior conversation ("what did I say", "my name", "what we discussed") → no capabilities (Tatlock has full history)
|
||||
- Math/calculations → tatlock_core
|
||||
- Web searches → tatlock_core
|
||||
- Quick web searches → tatlock_core
|
||||
- Time/date queries → tatlock_core
|
||||
- Wiki creation ("create a page about X", "add X to wiki") → librarian with smart_create
|
||||
- Wiki updates ("update the page", "add to dossier") → librarian with update
|
||||
- Research queries ("find info", "what do we know about", "search for") → librarian with hybrid_search
|
||||
- In-depth research, knowledge synthesis, document lookup → librarian with hybrid_search
|
||||
- If conversation history is relevant, note which previous turns matter
|
||||
- Assess complexity: simple (1 tool), moderate (2-3 tools), complex (multiple steps)
|
||||
- If capabilities are missing, mention what would be needed
|
||||
|
||||
RESPOND WITH 2-3 SENTENCES:
|
||||
1. Which capabilities (if any) are needed and why
|
||||
2. Complexity assessment (simple/moderate/complex)
|
||||
3. Any conversation context or missing capabilities
|
||||
RESPOND IN THIS FORMAT:
|
||||
DELEGATE: [capability name] to [action] [specific task]
|
||||
REASON: [why this capability handles the request]
|
||||
COMPLEXITY: [simple/moderate/complex]
|
||||
CONTEXT: [any relevant conversation context, or "none"]
|
||||
|
||||
Use capability names in your response (e.g., "tatlock_core for calculations").
|
||||
EXAMPLES:
|
||||
- "DELEGATE: librarian to create a wiki page about CI/CD pipelines"
|
||||
- "DELEGATE: librarian to search for information about Docker networking"
|
||||
- "DELEGATE: tatlock_core to calculate the result"
|
||||
- "DELEGATE: none (conversational response only)"
|
||||
|
||||
Be specific about what Tatlock should delegate - include the action verb (create, update, search, etc.).
|
||||
Plain text only - no JSON, no special formatting."""
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ Steward agent schemas.
|
||||
Defines the structured output models for Steward's request analysis
|
||||
and capability recommendations.
|
||||
"""
|
||||
from typing import Literal, Optional
|
||||
from typing import Any, Literal, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
@@ -56,6 +56,10 @@ class StewardRecommendation(BaseModel):
|
||||
default=None,
|
||||
description="Description of capabilities that would be helpful but aren't available"
|
||||
)
|
||||
memory_context: dict[str, Any] = Field(
|
||||
default_factory=dict,
|
||||
description="Pre-fetched user context from memory (profile, preferences)"
|
||||
)
|
||||
|
||||
def format_for_butler(self) -> str:
|
||||
"""
|
||||
@@ -88,6 +92,23 @@ class StewardRecommendation(BaseModel):
|
||||
if self.missing_capabilities:
|
||||
lines.append(f"⚠️ Missing: {self.missing_capabilities}")
|
||||
|
||||
# Memory context (user profile and preferences)
|
||||
if self.memory_context:
|
||||
profile = self.memory_context.get("profile", {})
|
||||
preferences = self.memory_context.get("preferences", {})
|
||||
|
||||
if profile or preferences:
|
||||
lines.append("-" * 40)
|
||||
lines.append("User Context:")
|
||||
|
||||
if profile:
|
||||
for key, value in profile.items():
|
||||
lines.append(f" • {key}: {value}")
|
||||
|
||||
if preferences:
|
||||
prefs_str = ", ".join(f"{k}={v}" for k, v in preferences.items())
|
||||
lines.append(f" • preferences: {prefs_str}")
|
||||
|
||||
lines.append("=" * 40)
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
@@ -5,13 +5,15 @@ Provides high-level interface for request analysis with logging,
|
||||
benchmarking, and error handling.
|
||||
|
||||
Parses plain text recommendations into structured data.
|
||||
Includes memory pre-fetch for user context injection.
|
||||
"""
|
||||
import re
|
||||
from typing import Optional
|
||||
from typing import Any, Optional
|
||||
|
||||
from src.core.benchmarks import PerformanceBenchmark, get_benchmark_store
|
||||
from src.core.household_registry import get_household_registry
|
||||
from src.core.logging_config import get_logger, log_operation
|
||||
from src.core.memory_service import memory_service
|
||||
from .agent import get_steward_agent
|
||||
from .schemas import ConversationContext, StewardRecommendation
|
||||
|
||||
@@ -147,6 +149,69 @@ def _extract_missing_capabilities(text: str) -> Optional[str]:
|
||||
return None
|
||||
|
||||
|
||||
async def _prefetch_memory_context(user_request: str) -> dict[str, Any]:
|
||||
"""
|
||||
Pre-fetch user context that might be needed for this request.
|
||||
|
||||
This is the "direct access" layer - fast lookups without LLM overhead.
|
||||
Uses simple keyword matching to determine what context to fetch.
|
||||
|
||||
Args:
|
||||
user_request: The user's request text
|
||||
|
||||
Returns:
|
||||
Dict with profile and/or preferences data
|
||||
|
||||
Example:
|
||||
>>> ctx = await _prefetch_memory_context("What's the weather?")
|
||||
>>> ctx
|
||||
{"profile": {"location": "Amsterdam"}}
|
||||
"""
|
||||
request_lower = user_request.lower()
|
||||
|
||||
# Determine what context might be needed based on keywords
|
||||
profile_keys = []
|
||||
|
||||
# Location-related queries
|
||||
if any(word in request_lower for word in [
|
||||
"weather", "temperature", "forecast", "nearby", "local",
|
||||
"directions", "distance", "map", "here"
|
||||
]):
|
||||
profile_keys.append("location")
|
||||
|
||||
# Time-related queries
|
||||
if any(word in request_lower for word in [
|
||||
"time", "schedule", "meeting", "appointment", "reminder",
|
||||
"alarm", "when", "today", "tomorrow"
|
||||
]):
|
||||
profile_keys.append("timezone")
|
||||
|
||||
# Personal queries
|
||||
if any(word in request_lower for word in [
|
||||
"my name", "who am i", "about me"
|
||||
]):
|
||||
profile_keys.append("name")
|
||||
|
||||
# Always fetch preferences if they might affect response format
|
||||
include_preferences = any(word in request_lower for word in [
|
||||
"temperature", "weather", "convert", "unit", "format",
|
||||
"celsius", "fahrenheit", "metric", "imperial"
|
||||
])
|
||||
|
||||
try:
|
||||
return await memory_service.prefetch_context(
|
||||
include_profile=bool(profile_keys),
|
||||
include_preferences=include_preferences,
|
||||
profile_keys=profile_keys if profile_keys else None,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"steward_prefetch_memory_failed",
|
||||
error=str(e),
|
||||
)
|
||||
return {}
|
||||
|
||||
|
||||
async def analyze_request(
|
||||
user_request: str,
|
||||
conversation_history: list[dict],
|
||||
@@ -186,6 +251,10 @@ async def analyze_request(
|
||||
}
|
||||
) as log_ctx:
|
||||
try:
|
||||
# Pre-fetch user context from memory (fast, no LLM)
|
||||
memory_context = await _prefetch_memory_context(user_request)
|
||||
log_ctx["memory_context_keys"] = list(memory_context.keys())
|
||||
|
||||
# Get Steward agent
|
||||
steward = get_steward_agent()
|
||||
|
||||
@@ -193,6 +262,7 @@ async def analyze_request(
|
||||
"steward_analyzing_request",
|
||||
request=user_request,
|
||||
history_turns=len(conversation_history),
|
||||
memory_context=bool(memory_context),
|
||||
)
|
||||
|
||||
# Get plain text analysis from Steward
|
||||
@@ -212,7 +282,8 @@ async def analyze_request(
|
||||
reasoning=analysis_text,
|
||||
estimated_complexity=complexity,
|
||||
conversation_context=context,
|
||||
missing_capabilities=missing
|
||||
missing_capabilities=missing,
|
||||
memory_context=memory_context,
|
||||
)
|
||||
|
||||
# Update log context with results
|
||||
|
||||
+13
-6
@@ -587,16 +587,23 @@ class TatlockAgent(AgentInterface):
|
||||
ModelResponse(parts=[TextPart(content=content)])
|
||||
)
|
||||
|
||||
# Stream with scoped tools and tracker
|
||||
async with scoped_agent.run_stream(
|
||||
# Use run() instead of run_stream() to avoid Ollama 400 bug
|
||||
# with streaming + tool calls (PydanticAI issues #1292, #2256)
|
||||
# We yield the final response in chunks to maintain streaming interface
|
||||
result = await scoped_agent.run(
|
||||
enriched_message,
|
||||
message_history=pydantic_history if pydantic_history else None,
|
||||
deps=tool_tracker
|
||||
) as stream:
|
||||
async for chunk in stream.stream_text(delta=True):
|
||||
yield chunk
|
||||
)
|
||||
|
||||
logger.info("tatlock_stream_complete")
|
||||
# Stream the final response in chunks to maintain UX
|
||||
response_text = result.output
|
||||
chunk_size = 50 # characters per chunk
|
||||
|
||||
for i in range(0, len(response_text), chunk_size):
|
||||
yield response_text[i:i + chunk_size]
|
||||
|
||||
logger.info("tatlock_scoped_run_complete")
|
||||
|
||||
async def get_capabilities(self) -> dict:
|
||||
"""Return current capabilities."""
|
||||
|
||||
+79
-2
@@ -4,11 +4,34 @@ Following best practice of splitting config across domains.
|
||||
"""
|
||||
from enum import Enum
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import Field, HttpUrl
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
def _get_version_from_pyproject() -> str:
|
||||
"""
|
||||
Load version from pyproject.toml.
|
||||
|
||||
Falls back to "unknown" if file cannot be read.
|
||||
"""
|
||||
try:
|
||||
# Find pyproject.toml relative to this file
|
||||
config_dir = Path(__file__).parent
|
||||
pyproject_path = config_dir.parent.parent / "pyproject.toml"
|
||||
|
||||
if pyproject_path.exists():
|
||||
content = pyproject_path.read_text()
|
||||
for line in content.splitlines():
|
||||
if line.strip().startswith("version"):
|
||||
# Parse: version = "1.0.0"
|
||||
return line.split("=", 1)[1].strip().strip('"').strip("'")
|
||||
except Exception:
|
||||
pass
|
||||
return "unknown"
|
||||
|
||||
|
||||
class Environment(str, Enum):
|
||||
"""Application environment."""
|
||||
DEVELOPMENT = "development"
|
||||
@@ -32,7 +55,7 @@ class Config(BaseSettings):
|
||||
|
||||
# Application
|
||||
APP_NAME: str = "OpenAI-Compatible API"
|
||||
APP_VERSION: str = "0.2.5"
|
||||
APP_VERSION: str = Field(default_factory=_get_version_from_pyproject)
|
||||
ENVIRONMENT: Environment = Environment.DEVELOPMENT
|
||||
DEBUG: bool = Field(default=False, description="Debug mode")
|
||||
|
||||
@@ -87,6 +110,50 @@ class Config(BaseSettings):
|
||||
description="Redis connection timeout in seconds"
|
||||
)
|
||||
|
||||
# Library-Desk Configuration (The Librarian backend)
|
||||
LIBRARY_DESK_HOST: HttpUrl = Field(
|
||||
default="http://localhost:8089",
|
||||
description="Library-Desk API URL"
|
||||
)
|
||||
LIBRARY_DESK_API_KEY: str = Field(
|
||||
default="",
|
||||
description="API key for Library-Desk authentication"
|
||||
)
|
||||
LIBRARY_DESK_TIMEOUT: int = Field(
|
||||
default=60,
|
||||
description="Library-Desk request timeout in seconds"
|
||||
)
|
||||
|
||||
# Qdrant Configuration (Memory vector storage)
|
||||
QDRANT_HOST: str = Field(
|
||||
default="localhost",
|
||||
description="Qdrant server host"
|
||||
)
|
||||
QDRANT_PORT: int = Field(
|
||||
default=6333,
|
||||
description="Qdrant server port"
|
||||
)
|
||||
QDRANT_EMBEDDING_DIM: int = Field(
|
||||
default=768,
|
||||
description="Embedding dimension (768 for nomic-embed-text)"
|
||||
)
|
||||
|
||||
# Ollama Embedding Configuration
|
||||
OLLAMA_EMBEDDING_MODEL: str = Field(
|
||||
default="nomic-embed-text",
|
||||
description="Ollama model for embeddings"
|
||||
)
|
||||
|
||||
# Redis Memory Database (separate from benchmarks)
|
||||
REDIS_MEMORY_DB: int = Field(
|
||||
default=2,
|
||||
description="Redis database number for memory cache"
|
||||
)
|
||||
REDIS_MEMORY_TTL_HOURS: int = Field(
|
||||
default=24,
|
||||
description="TTL for session context in hours"
|
||||
)
|
||||
|
||||
# Logging
|
||||
LOG_LEVEL: str = Field(default="INFO", description="Logging level")
|
||||
ENABLE_BENCHMARKS: bool = Field(default=True, description="Enable performance benchmarking")
|
||||
@@ -102,9 +169,19 @@ class Config(BaseSettings):
|
||||
|
||||
@property
|
||||
def redis_url(self) -> str:
|
||||
"""Construct Redis connection URL."""
|
||||
"""Construct Redis connection URL for benchmarks."""
|
||||
return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}"
|
||||
|
||||
@property
|
||||
def redis_memory_url(self) -> str:
|
||||
"""Construct Redis connection URL for memory cache."""
|
||||
return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_MEMORY_DB}"
|
||||
|
||||
@property
|
||||
def qdrant_url(self) -> str:
|
||||
"""Construct Qdrant server URL."""
|
||||
return f"http://{self.QDRANT_HOST}:{self.QDRANT_PORT}"
|
||||
|
||||
@property
|
||||
def log_format(self) -> str:
|
||||
"""
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
"""
|
||||
Request context using ContextVar for async-safe user/conversation tracking.
|
||||
|
||||
ContextVar provides task-local storage that automatically propagates through
|
||||
async calls, eliminating the need to thread user identity through every function.
|
||||
|
||||
Usage:
|
||||
# At request entry (router):
|
||||
token = current_user.set(request.user or "jpmschweitzer")
|
||||
try:
|
||||
await service.process(request)
|
||||
finally:
|
||||
current_user.reset(token)
|
||||
|
||||
# Anywhere in the codebase:
|
||||
from src.core.context import get_user
|
||||
user = get_user() # Returns current request's user
|
||||
"""
|
||||
from contextvars import ContextVar
|
||||
|
||||
# Default user for single-user homelab setup
|
||||
DEFAULT_USER = "jpmschweitzer"
|
||||
|
||||
# Request-scoped context variables (async-safe, isolated per request)
|
||||
current_user: ContextVar[str] = ContextVar("current_user", default=DEFAULT_USER)
|
||||
current_conversation: ContextVar[str | None] = ContextVar(
|
||||
"current_conversation", default=None
|
||||
)
|
||||
|
||||
|
||||
def get_user() -> str:
|
||||
"""
|
||||
Get current user from request context.
|
||||
|
||||
Returns:
|
||||
User identifier for the current request.
|
||||
Falls back to DEFAULT_USER if not set.
|
||||
|
||||
Example:
|
||||
user = get_user() # "jpmschweitzer" or whatever was set in router
|
||||
"""
|
||||
return current_user.get()
|
||||
|
||||
|
||||
def get_conversation_id() -> str | None:
|
||||
"""
|
||||
Get current conversation ID from request context.
|
||||
|
||||
Returns:
|
||||
Conversation ID if set, None otherwise.
|
||||
|
||||
Example:
|
||||
conv_id = get_conversation_id() # "conv_abc123" or None
|
||||
"""
|
||||
return current_conversation.get()
|
||||
|
||||
|
||||
class RequestContext:
|
||||
"""
|
||||
Context manager for setting request-scoped context.
|
||||
|
||||
Provides a cleaner alternative to manual token management.
|
||||
|
||||
Usage:
|
||||
async with RequestContext(user="alice", conversation_id="conv_123"):
|
||||
# All code here sees user="alice"
|
||||
result = await some_service.process()
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
user: str | None = None,
|
||||
conversation_id: str | None = None,
|
||||
):
|
||||
"""
|
||||
Initialize request context.
|
||||
|
||||
Args:
|
||||
user: User identifier (defaults to DEFAULT_USER if None)
|
||||
conversation_id: Conversation ID (optional)
|
||||
"""
|
||||
self.user = user or DEFAULT_USER
|
||||
self.conversation_id = conversation_id
|
||||
self._user_token = None
|
||||
self._conv_token = None
|
||||
|
||||
async def __aenter__(self) -> "RequestContext":
|
||||
"""Set context variables on entry."""
|
||||
self._user_token = current_user.set(self.user)
|
||||
self._conv_token = current_conversation.set(self.conversation_id)
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
|
||||
"""Reset context variables on exit."""
|
||||
if self._user_token is not None:
|
||||
current_user.reset(self._user_token)
|
||||
if self._conv_token is not None:
|
||||
current_conversation.reset(self._conv_token)
|
||||
|
||||
def __enter__(self) -> "RequestContext":
|
||||
"""Sync context manager entry (for non-async code)."""
|
||||
self._user_token = current_user.set(self.user)
|
||||
self._conv_token = current_conversation.set(self.conversation_id)
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
|
||||
"""Sync context manager exit."""
|
||||
if self._user_token is not None:
|
||||
current_user.reset(self._user_token)
|
||||
if self._conv_token is not None:
|
||||
current_conversation.reset(self._conv_token)
|
||||
@@ -0,0 +1,269 @@
|
||||
"""
|
||||
Ollama client for embeddings generation.
|
||||
|
||||
Provides async embedding operations via Ollama API:
|
||||
- Text embedding generation
|
||||
- Batch embedding support
|
||||
- Health checks
|
||||
|
||||
Adapted from library-desk patterns.
|
||||
"""
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from .config import config
|
||||
from .logging_config import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class OllamaEmbeddingClient:
|
||||
"""
|
||||
Ollama API client for embeddings.
|
||||
|
||||
Uses the Ollama embeddings endpoint to generate vector representations
|
||||
of text using the nomic-embed-text model (768 dimensions).
|
||||
|
||||
Usage:
|
||||
client = OllamaEmbeddingClient()
|
||||
embedding = await client.embed("Hello world")
|
||||
await client.close()
|
||||
|
||||
Or with context manager:
|
||||
async with OllamaEmbeddingClient() as client:
|
||||
embedding = await client.embed("Hello world")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_url: str | None = None,
|
||||
model: str | None = None,
|
||||
timeout: float = 120.0,
|
||||
):
|
||||
"""
|
||||
Initialize Ollama embedding client.
|
||||
|
||||
Args:
|
||||
base_url: Ollama server URL (defaults to config.OLLAMA_HOST)
|
||||
model: Embedding model name (defaults to config.OLLAMA_EMBEDDING_MODEL)
|
||||
timeout: Request timeout in seconds (embeddings can be slow)
|
||||
"""
|
||||
self.base_url = (base_url or str(config.OLLAMA_HOST)).rstrip("/")
|
||||
self.model = model or config.OLLAMA_EMBEDDING_MODEL
|
||||
self.embeddings_url = f"{self.base_url}/api/embeddings"
|
||||
self.tags_url = f"{self.base_url}/api/tags"
|
||||
self._client: httpx.AsyncClient | None = None
|
||||
self._timeout = timeout
|
||||
|
||||
logger.info(
|
||||
"ollama_embedding_client_initialized",
|
||||
base_url=self.base_url,
|
||||
model=self.model,
|
||||
)
|
||||
|
||||
async def _get_client(self) -> httpx.AsyncClient:
|
||||
"""Get or create HTTP client."""
|
||||
if self._client is None:
|
||||
self._client = httpx.AsyncClient(timeout=self._timeout)
|
||||
return self._client
|
||||
|
||||
async def __aenter__(self) -> "OllamaEmbeddingClient":
|
||||
"""Async context manager entry."""
|
||||
await self._get_client()
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
|
||||
"""Async context manager exit."""
|
||||
await self.close()
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Close HTTP client."""
|
||||
if self._client is not None:
|
||||
await self._client.aclose()
|
||||
self._client = None
|
||||
|
||||
async def embed(self, text: str) -> list[float] | None:
|
||||
"""
|
||||
Generate embedding for single text.
|
||||
|
||||
Args:
|
||||
text: Text to embed
|
||||
|
||||
Returns:
|
||||
Embedding vector (768-dimensional for nomic-embed-text) or None on failure
|
||||
|
||||
Example:
|
||||
>>> embedding = await client.embed("Hello world")
|
||||
>>> len(embedding)
|
||||
768
|
||||
"""
|
||||
try:
|
||||
client = await self._get_client()
|
||||
|
||||
payload = {
|
||||
"model": self.model,
|
||||
"prompt": text,
|
||||
}
|
||||
|
||||
response = await client.post(self.embeddings_url, json=payload)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
embedding = data.get("embedding")
|
||||
if not embedding:
|
||||
logger.error("ollama_embed_no_embedding", response_data=data)
|
||||
return None
|
||||
|
||||
return embedding
|
||||
|
||||
except httpx.HTTPStatusError as e:
|
||||
logger.error(
|
||||
"ollama_embed_http_error",
|
||||
status_code=e.response.status_code,
|
||||
detail=e.response.text,
|
||||
)
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error("ollama_embed_failed", error=str(e), exc_info=True)
|
||||
return None
|
||||
|
||||
async def embed_batch(
|
||||
self,
|
||||
texts: list[str],
|
||||
show_progress: bool = False,
|
||||
) -> list[list[float] | None]:
|
||||
"""
|
||||
Generate embeddings for multiple texts.
|
||||
|
||||
Note: Ollama doesn't support native batch embeddings, so this
|
||||
sequentially calls embed() for each text.
|
||||
|
||||
Args:
|
||||
texts: List of texts to embed
|
||||
show_progress: Log progress for large batches
|
||||
|
||||
Returns:
|
||||
List of embedding vectors (same order as input)
|
||||
None entries for texts that failed to embed
|
||||
|
||||
Example:
|
||||
>>> texts = ["Hello", "World", "Test"]
|
||||
>>> embeddings = await client.embed_batch(texts)
|
||||
>>> len(embeddings)
|
||||
3
|
||||
"""
|
||||
embeddings = []
|
||||
|
||||
for i, text in enumerate(texts):
|
||||
if show_progress and i % 10 == 0:
|
||||
logger.info(
|
||||
"ollama_embed_batch_progress",
|
||||
current=i,
|
||||
total=len(texts),
|
||||
)
|
||||
|
||||
embedding = await self.embed(text)
|
||||
embeddings.append(embedding)
|
||||
|
||||
if show_progress:
|
||||
logger.info(
|
||||
"ollama_embed_batch_complete",
|
||||
successful=sum(1 for e in embeddings if e is not None),
|
||||
total=len(texts),
|
||||
)
|
||||
|
||||
return embeddings
|
||||
|
||||
async def embed_batch_filtered(
|
||||
self,
|
||||
texts: list[str],
|
||||
show_progress: bool = False,
|
||||
) -> list[list[float]]:
|
||||
"""
|
||||
Generate embeddings for multiple texts, filtering out failures.
|
||||
|
||||
Args:
|
||||
texts: List of texts to embed
|
||||
show_progress: Log progress for large batches
|
||||
|
||||
Returns:
|
||||
List of successful embedding vectors (may be shorter than input)
|
||||
|
||||
Example:
|
||||
>>> embeddings = await client.embed_batch_filtered(texts)
|
||||
>>> all(e is not None for e in embeddings)
|
||||
True
|
||||
"""
|
||||
all_embeddings = await self.embed_batch(texts, show_progress)
|
||||
return [e for e in all_embeddings if e is not None]
|
||||
|
||||
async def get_embedding_dimension(self) -> int | None:
|
||||
"""
|
||||
Get embedding dimension for current model.
|
||||
|
||||
Returns:
|
||||
Embedding dimension (e.g., 768 for nomic-embed-text) or None on failure
|
||||
|
||||
Example:
|
||||
>>> dim = await client.get_embedding_dimension()
|
||||
>>> dim
|
||||
768
|
||||
"""
|
||||
test_embedding = await self.embed("test")
|
||||
if test_embedding:
|
||||
return len(test_embedding)
|
||||
return None
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
"""
|
||||
Check if Ollama server is reachable and model is available.
|
||||
|
||||
Returns:
|
||||
True if healthy, False otherwise
|
||||
"""
|
||||
try:
|
||||
client = await self._get_client()
|
||||
response = await client.get(self.tags_url, timeout=5.0)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
models = data.get("models", [])
|
||||
|
||||
# Check if our embedding model is available
|
||||
model_found = False
|
||||
for m in models:
|
||||
name = m.get("name", "")
|
||||
if name == self.model or name.startswith(f"{self.model}:"):
|
||||
model_found = True
|
||||
break
|
||||
|
||||
if not model_found:
|
||||
logger.warning(
|
||||
"ollama_embedding_model_not_found",
|
||||
model=self.model,
|
||||
available=[m.get("name") for m in models],
|
||||
)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error("ollama_embedding_health_check_failed", error=str(e))
|
||||
return False
|
||||
|
||||
|
||||
# Global client instance (lazy initialization)
|
||||
_embedding_client: OllamaEmbeddingClient | None = None
|
||||
|
||||
|
||||
def get_embedding_client() -> OllamaEmbeddingClient:
|
||||
"""
|
||||
Get global embedding client instance.
|
||||
|
||||
Returns:
|
||||
OllamaEmbeddingClient instance
|
||||
"""
|
||||
global _embedding_client
|
||||
if _embedding_client is None:
|
||||
_embedding_client = OllamaEmbeddingClient()
|
||||
return _embedding_client
|
||||
@@ -200,6 +200,75 @@ class HouseholdRegistry:
|
||||
|
||||
return tools
|
||||
|
||||
def get_delegation_tools(self, names: list[str]) -> list[Any]:
|
||||
"""
|
||||
Get delegation wrapper tools for specified capabilities.
|
||||
|
||||
Instead of returning raw tools (which overloads the LLM),
|
||||
returns wrapper functions that delegate to expert agents.
|
||||
This implements the agent-as-tool pattern.
|
||||
|
||||
For members WITH an agent: returns delegation wrapper
|
||||
For members WITHOUT an agent (e.g., tatlock_core): returns raw tools
|
||||
|
||||
Args:
|
||||
names: List of member names to include
|
||||
|
||||
Returns:
|
||||
List of delegation wrappers and/or raw tools
|
||||
|
||||
Example:
|
||||
>>> # Steward recommends librarian + tatlock_core
|
||||
>>> tools = registry.get_delegation_tools(["librarian", "tatlock_core"])
|
||||
>>> # Returns: [delegate_to_librarian, calculate, datetime, ...]
|
||||
>>> # Instead of: [hybrid_search, search_wiki, create_wiki_page, ... (16 tools)]
|
||||
"""
|
||||
from src.agents.delegation import delegate_to_librarian
|
||||
|
||||
# Map of expert names to their delegation wrappers
|
||||
delegation_wrappers = {
|
||||
"librarian": delegate_to_librarian,
|
||||
# Future: "memory": delegate_to_memory,
|
||||
# Future: "home_automation": delegate_to_home_automation,
|
||||
}
|
||||
|
||||
tools = []
|
||||
for name in names:
|
||||
member = self._members.get(name)
|
||||
if not member:
|
||||
logger.warning(
|
||||
"household_member_not_found",
|
||||
requested_name=name,
|
||||
available_names=list(self._members.keys()),
|
||||
)
|
||||
continue
|
||||
|
||||
# Check if this member has a delegation wrapper
|
||||
if name in delegation_wrappers and member.agent is not None:
|
||||
# Use delegation wrapper instead of raw tools
|
||||
tools.append(delegation_wrappers[name])
|
||||
logger.debug(
|
||||
"delegation_wrapper_added",
|
||||
member=name,
|
||||
wrapper=delegation_wrappers[name].__name__,
|
||||
)
|
||||
else:
|
||||
# No agent = direct tools (e.g., tatlock_core)
|
||||
tools.extend(member.tools)
|
||||
logger.debug(
|
||||
"raw_tools_added",
|
||||
member=name,
|
||||
tool_count=len(member.tools),
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"delegation_tools_created",
|
||||
requested_members=names,
|
||||
total_tools=len(tools),
|
||||
)
|
||||
|
||||
return tools
|
||||
|
||||
def list_members(self) -> list[str]:
|
||||
"""
|
||||
List all registered member names.
|
||||
|
||||
@@ -0,0 +1,390 @@
|
||||
"""
|
||||
Redis-backed memory cache for session context.
|
||||
|
||||
Provides short-term memory storage with TTL:
|
||||
- Session context (24h TTL)
|
||||
- Recent entities mentioned in conversation
|
||||
- User-scoped with conversation isolation
|
||||
|
||||
Uses Redis DB 2 (separate from benchmarks in DB 1).
|
||||
"""
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
import redis.asyncio as redis
|
||||
|
||||
from .config import config
|
||||
from .logging_config import get_logger
|
||||
from .multi_tenancy import get_session_key, get_entities_key
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class MemoryCache:
|
||||
"""
|
||||
Redis-backed cache for session memory.
|
||||
|
||||
Stores ephemeral context that doesn't need vector search:
|
||||
- Session context (recent topics, user state)
|
||||
- Recent entities (people, places, things mentioned)
|
||||
- Conversation metadata
|
||||
|
||||
All data expires after REDIS_MEMORY_TTL_HOURS (default 24h).
|
||||
|
||||
Usage:
|
||||
cache = MemoryCache()
|
||||
await cache.set_session_context(
|
||||
user="jpmschweitzer",
|
||||
conversation_id="conv_123",
|
||||
context={"topic": "docker", "mood": "curious"}
|
||||
)
|
||||
context = await cache.get_session_context("jpmschweitzer", "conv_123")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
redis_url: str | None = None,
|
||||
ttl_hours: int | None = None,
|
||||
):
|
||||
"""
|
||||
Initialize memory cache.
|
||||
|
||||
Args:
|
||||
redis_url: Redis connection URL (defaults to config.redis_memory_url)
|
||||
ttl_hours: TTL for cached data (defaults to config.REDIS_MEMORY_TTL_HOURS)
|
||||
"""
|
||||
self._redis_url = redis_url or config.redis_memory_url
|
||||
self._ttl_seconds = (ttl_hours or config.REDIS_MEMORY_TTL_HOURS) * 3600
|
||||
self._client: redis.Redis | None = None
|
||||
|
||||
logger.info(
|
||||
"memory_cache_initialized",
|
||||
redis_url=self._redis_url,
|
||||
ttl_hours=ttl_hours or config.REDIS_MEMORY_TTL_HOURS,
|
||||
)
|
||||
|
||||
async def _get_client(self) -> redis.Redis:
|
||||
"""Get or create Redis client."""
|
||||
if self._client is None:
|
||||
self._client = redis.from_url(
|
||||
self._redis_url,
|
||||
encoding="utf-8",
|
||||
decode_responses=True,
|
||||
socket_timeout=config.REDIS_TIMEOUT,
|
||||
socket_connect_timeout=config.REDIS_TIMEOUT,
|
||||
)
|
||||
return self._client
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Close Redis connection."""
|
||||
if self._client is not None:
|
||||
await self._client.aclose()
|
||||
self._client = None
|
||||
|
||||
# =========================================================================
|
||||
# Session Context
|
||||
# =========================================================================
|
||||
|
||||
async def get_session_context(
|
||||
self,
|
||||
user: str,
|
||||
conversation_id: str,
|
||||
) -> dict[str, Any] | None:
|
||||
"""
|
||||
Get session context for a conversation.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
conversation_id: Conversation identifier
|
||||
|
||||
Returns:
|
||||
Session context dict or None if not found
|
||||
|
||||
Example:
|
||||
>>> context = await cache.get_session_context("jpmschweitzer", "conv_123")
|
||||
>>> context
|
||||
{"topic": "docker", "mood": "curious", "last_tool": "librarian"}
|
||||
"""
|
||||
try:
|
||||
client = await self._get_client()
|
||||
key = get_session_key(user, conversation_id)
|
||||
|
||||
data = await client.get(key)
|
||||
if data is None:
|
||||
return None
|
||||
|
||||
return json.loads(data)
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"memory_cache_get_session_failed",
|
||||
user=user,
|
||||
conversation_id=conversation_id,
|
||||
error=str(e),
|
||||
)
|
||||
return None
|
||||
|
||||
async def set_session_context(
|
||||
self,
|
||||
user: str,
|
||||
conversation_id: str,
|
||||
context: dict[str, Any],
|
||||
) -> bool:
|
||||
"""
|
||||
Set session context for a conversation.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
conversation_id: Conversation identifier
|
||||
context: Context data to store
|
||||
|
||||
Returns:
|
||||
True if successful, False otherwise
|
||||
|
||||
Example:
|
||||
>>> await cache.set_session_context(
|
||||
... "jpmschweitzer",
|
||||
... "conv_123",
|
||||
... {"topic": "docker", "mood": "curious"}
|
||||
... )
|
||||
True
|
||||
"""
|
||||
try:
|
||||
client = await self._get_client()
|
||||
key = get_session_key(user, conversation_id)
|
||||
|
||||
await client.setex(
|
||||
key,
|
||||
self._ttl_seconds,
|
||||
json.dumps(context),
|
||||
)
|
||||
|
||||
logger.debug(
|
||||
"memory_cache_set_session",
|
||||
user=user,
|
||||
conversation_id=conversation_id,
|
||||
context_keys=list(context.keys()),
|
||||
)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"memory_cache_set_session_failed",
|
||||
user=user,
|
||||
conversation_id=conversation_id,
|
||||
error=str(e),
|
||||
)
|
||||
return False
|
||||
|
||||
async def update_session_context(
|
||||
self,
|
||||
user: str,
|
||||
conversation_id: str,
|
||||
updates: dict[str, Any],
|
||||
) -> bool:
|
||||
"""
|
||||
Update session context (merge with existing).
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
conversation_id: Conversation identifier
|
||||
updates: Fields to update/add
|
||||
|
||||
Returns:
|
||||
True if successful, False otherwise
|
||||
"""
|
||||
existing = await self.get_session_context(user, conversation_id) or {}
|
||||
existing.update(updates)
|
||||
return await self.set_session_context(user, conversation_id, existing)
|
||||
|
||||
async def delete_session_context(
|
||||
self,
|
||||
user: str,
|
||||
conversation_id: str,
|
||||
) -> bool:
|
||||
"""
|
||||
Delete session context for a conversation.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
conversation_id: Conversation identifier
|
||||
|
||||
Returns:
|
||||
True if deleted, False otherwise
|
||||
"""
|
||||
try:
|
||||
client = await self._get_client()
|
||||
key = get_session_key(user, conversation_id)
|
||||
await client.delete(key)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"memory_cache_delete_session_failed",
|
||||
user=user,
|
||||
conversation_id=conversation_id,
|
||||
error=str(e),
|
||||
)
|
||||
return False
|
||||
|
||||
# =========================================================================
|
||||
# Recent Entities
|
||||
# =========================================================================
|
||||
|
||||
async def get_recent_entities(
|
||||
self,
|
||||
user: str,
|
||||
conversation_id: str,
|
||||
) -> list[str]:
|
||||
"""
|
||||
Get recently mentioned entities in a conversation.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
conversation_id: Conversation identifier
|
||||
|
||||
Returns:
|
||||
List of entity names/identifiers
|
||||
|
||||
Example:
|
||||
>>> entities = await cache.get_recent_entities("jpmschweitzer", "conv_123")
|
||||
>>> entities
|
||||
["Docker", "Kubernetes", "nginx"]
|
||||
"""
|
||||
try:
|
||||
client = await self._get_client()
|
||||
key = get_entities_key(user, conversation_id)
|
||||
|
||||
# Get all members of the set
|
||||
entities = await client.smembers(key)
|
||||
return list(entities)
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"memory_cache_get_entities_failed",
|
||||
user=user,
|
||||
conversation_id=conversation_id,
|
||||
error=str(e),
|
||||
)
|
||||
return []
|
||||
|
||||
async def add_recent_entities(
|
||||
self,
|
||||
user: str,
|
||||
conversation_id: str,
|
||||
entities: list[str],
|
||||
) -> bool:
|
||||
"""
|
||||
Add entities to the recent entities set.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
conversation_id: Conversation identifier
|
||||
entities: Entity names to add
|
||||
|
||||
Returns:
|
||||
True if successful, False otherwise
|
||||
|
||||
Example:
|
||||
>>> await cache.add_recent_entities(
|
||||
... "jpmschweitzer",
|
||||
... "conv_123",
|
||||
... ["Docker", "Kubernetes"]
|
||||
... )
|
||||
True
|
||||
"""
|
||||
if not entities:
|
||||
return True
|
||||
|
||||
try:
|
||||
client = await self._get_client()
|
||||
key = get_entities_key(user, conversation_id)
|
||||
|
||||
# Add to set
|
||||
await client.sadd(key, *entities)
|
||||
|
||||
# Refresh TTL
|
||||
await client.expire(key, self._ttl_seconds)
|
||||
|
||||
logger.debug(
|
||||
"memory_cache_add_entities",
|
||||
user=user,
|
||||
conversation_id=conversation_id,
|
||||
entities=entities,
|
||||
)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"memory_cache_add_entities_failed",
|
||||
user=user,
|
||||
conversation_id=conversation_id,
|
||||
error=str(e),
|
||||
)
|
||||
return False
|
||||
|
||||
async def clear_recent_entities(
|
||||
self,
|
||||
user: str,
|
||||
conversation_id: str,
|
||||
) -> bool:
|
||||
"""
|
||||
Clear all recent entities for a conversation.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
conversation_id: Conversation identifier
|
||||
|
||||
Returns:
|
||||
True if cleared, False otherwise
|
||||
"""
|
||||
try:
|
||||
client = await self._get_client()
|
||||
key = get_entities_key(user, conversation_id)
|
||||
await client.delete(key)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"memory_cache_clear_entities_failed",
|
||||
user=user,
|
||||
conversation_id=conversation_id,
|
||||
error=str(e),
|
||||
)
|
||||
return False
|
||||
|
||||
# =========================================================================
|
||||
# Health Check
|
||||
# =========================================================================
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
"""
|
||||
Check if Redis is reachable.
|
||||
|
||||
Returns:
|
||||
True if healthy, False otherwise
|
||||
"""
|
||||
try:
|
||||
client = await self._get_client()
|
||||
await client.ping()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error("memory_cache_health_check_failed", error=str(e))
|
||||
return False
|
||||
|
||||
|
||||
# Global cache instance (lazy initialization)
|
||||
_memory_cache: MemoryCache | None = None
|
||||
|
||||
|
||||
def get_memory_cache() -> MemoryCache:
|
||||
"""
|
||||
Get global memory cache instance.
|
||||
|
||||
Returns:
|
||||
MemoryCache instance
|
||||
"""
|
||||
global _memory_cache
|
||||
if _memory_cache is None:
|
||||
_memory_cache = MemoryCache()
|
||||
return _memory_cache
|
||||
@@ -0,0 +1,619 @@
|
||||
"""
|
||||
Memory service for direct key-based access.
|
||||
|
||||
Provides fast, LLM-free access to user memories for:
|
||||
- Known-key lookups (location, timezone, preferences)
|
||||
- Session context (current topic, recent entities)
|
||||
- Structured storage (explicit user instructions)
|
||||
|
||||
This is the "direct access layer" - no LLM interpretation.
|
||||
For semantic/fuzzy queries, use the Memory Agent instead.
|
||||
|
||||
Usage:
|
||||
from src.core.memory_service import memory_service
|
||||
|
||||
# Get user's location (fast, no LLM)
|
||||
location = await memory_service.get_profile("location")
|
||||
|
||||
# Set a preference
|
||||
await memory_service.set_preference("temperature_unit", "celsius")
|
||||
|
||||
# Get session context
|
||||
ctx = await memory_service.get_session_context(conversation_id)
|
||||
"""
|
||||
from datetime import datetime, timezone
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .config import config
|
||||
from .context import get_user, get_conversation_id
|
||||
from .embeddings import get_embedding_client
|
||||
from .logging_config import get_logger
|
||||
from .memory_cache import get_memory_cache
|
||||
from .multi_tenancy import get_memory_collection_name
|
||||
from .qdrant import get_qdrant_client
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class MemoryType(str, Enum):
|
||||
"""Types of memories stored in Qdrant."""
|
||||
USER_PROFILE = "user_profile" # Name, location, timezone
|
||||
PREFERENCE = "preference" # Units, language, theme
|
||||
LEARNED_FACT = "learned_fact" # "My car is a Tesla"
|
||||
|
||||
|
||||
class MemoryRecord(BaseModel):
|
||||
"""A memory record stored in Qdrant."""
|
||||
id: str
|
||||
type: MemoryType
|
||||
key: str # e.g., "location", "timezone", "car"
|
||||
value: str # The actual content
|
||||
keywords: list[str] = Field(default_factory=list)
|
||||
importance: float = 0.5 # 0.0 - 1.0
|
||||
source: str = "explicit" # "explicit" | "inferred" | "conversation"
|
||||
created_at: str = Field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
|
||||
updated_at: str = Field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
|
||||
|
||||
|
||||
class MemoryService:
|
||||
"""
|
||||
Direct access to user memories without LLM overhead.
|
||||
|
||||
Use this for:
|
||||
- Known-key lookups: get_profile("location"), get_preference("units")
|
||||
- Explicit storage: set_preference("theme", "dark")
|
||||
- Session context: get_session_context(), update_session_context()
|
||||
|
||||
Do NOT use for:
|
||||
- Fuzzy queries: "What car do I drive?" → Use Memory Agent
|
||||
- Semantic recall: "What did I mention about X?" → Use Memory Agent
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize memory service with lazy client loading."""
|
||||
self._qdrant = None
|
||||
self._embedding = None
|
||||
self._cache = None
|
||||
|
||||
@property
|
||||
def qdrant(self):
|
||||
"""Lazy-load Qdrant client."""
|
||||
if self._qdrant is None:
|
||||
self._qdrant = get_qdrant_client()
|
||||
return self._qdrant
|
||||
|
||||
@property
|
||||
def embedding(self):
|
||||
"""Lazy-load embedding client."""
|
||||
if self._embedding is None:
|
||||
self._embedding = get_embedding_client()
|
||||
return self._embedding
|
||||
|
||||
@property
|
||||
def cache(self):
|
||||
"""Lazy-load Redis cache."""
|
||||
if self._cache is None:
|
||||
self._cache = get_memory_cache()
|
||||
return self._cache
|
||||
|
||||
# =========================================================================
|
||||
# Profile Methods (user_profile type)
|
||||
# =========================================================================
|
||||
|
||||
async def get_profile(self, key: str, user: str | None = None) -> str | None:
|
||||
"""
|
||||
Get a user profile value by key.
|
||||
|
||||
Args:
|
||||
key: Profile key (e.g., "location", "timezone", "name")
|
||||
user: User ID (defaults to current request context)
|
||||
|
||||
Returns:
|
||||
Profile value or None if not found
|
||||
|
||||
Example:
|
||||
>>> location = await memory_service.get_profile("location")
|
||||
>>> location
|
||||
"Amsterdam, Netherlands"
|
||||
"""
|
||||
user = user or get_user()
|
||||
return await self._get_memory(user, MemoryType.USER_PROFILE, key)
|
||||
|
||||
async def set_profile(
|
||||
self,
|
||||
key: str,
|
||||
value: str,
|
||||
user: str | None = None,
|
||||
keywords: list[str] | None = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Set a user profile value.
|
||||
|
||||
Args:
|
||||
key: Profile key (e.g., "location", "timezone")
|
||||
value: Profile value
|
||||
user: User ID (defaults to current request context)
|
||||
keywords: Optional keywords for semantic search
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
|
||||
Example:
|
||||
>>> await memory_service.set_profile("location", "Amsterdam, Netherlands")
|
||||
True
|
||||
"""
|
||||
user = user or get_user()
|
||||
return await self._set_memory(
|
||||
user=user,
|
||||
memory_type=MemoryType.USER_PROFILE,
|
||||
key=key,
|
||||
value=value,
|
||||
keywords=keywords or [key],
|
||||
importance=0.9, # Profile data is important
|
||||
)
|
||||
|
||||
# =========================================================================
|
||||
# Preference Methods (preference type)
|
||||
# =========================================================================
|
||||
|
||||
async def get_preference(self, key: str, user: str | None = None) -> str | None:
|
||||
"""
|
||||
Get a user preference by key.
|
||||
|
||||
Args:
|
||||
key: Preference key (e.g., "temperature_unit", "language", "theme")
|
||||
user: User ID (defaults to current request context)
|
||||
|
||||
Returns:
|
||||
Preference value or None if not found
|
||||
|
||||
Example:
|
||||
>>> units = await memory_service.get_preference("temperature_unit")
|
||||
>>> units
|
||||
"celsius"
|
||||
"""
|
||||
user = user or get_user()
|
||||
return await self._get_memory(user, MemoryType.PREFERENCE, key)
|
||||
|
||||
async def set_preference(
|
||||
self,
|
||||
key: str,
|
||||
value: str,
|
||||
user: str | None = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Set a user preference.
|
||||
|
||||
Args:
|
||||
key: Preference key
|
||||
value: Preference value
|
||||
user: User ID (defaults to current request context)
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
|
||||
Example:
|
||||
>>> await memory_service.set_preference("theme", "dark")
|
||||
True
|
||||
"""
|
||||
user = user or get_user()
|
||||
return await self._set_memory(
|
||||
user=user,
|
||||
memory_type=MemoryType.PREFERENCE,
|
||||
key=key,
|
||||
value=value,
|
||||
keywords=[key, "preference"],
|
||||
importance=0.7,
|
||||
)
|
||||
|
||||
async def get_all_preferences(self, user: str | None = None) -> dict[str, str]:
|
||||
"""
|
||||
Get all preferences for a user.
|
||||
|
||||
Returns:
|
||||
Dict of key -> value for all preferences
|
||||
"""
|
||||
user = user or get_user()
|
||||
memories = await self._get_all_by_type(user, MemoryType.PREFERENCE)
|
||||
return {m["key"]: m["value"] for m in memories}
|
||||
|
||||
# =========================================================================
|
||||
# Learned Facts (learned_fact type) - for direct storage only
|
||||
# =========================================================================
|
||||
|
||||
async def store_fact(
|
||||
self,
|
||||
key: str,
|
||||
value: str,
|
||||
user: str | None = None,
|
||||
keywords: list[str] | None = None,
|
||||
importance: float = 0.5,
|
||||
source: str = "explicit",
|
||||
) -> bool:
|
||||
"""
|
||||
Store a learned fact about the user.
|
||||
|
||||
Use this for explicit user statements like:
|
||||
- "Remember that my car is a Tesla"
|
||||
- "I work at Acme Corp"
|
||||
|
||||
For semantic extraction from conversation, use the Memory Agent.
|
||||
|
||||
Args:
|
||||
key: Fact identifier (e.g., "car", "employer")
|
||||
value: The fact content
|
||||
user: User ID
|
||||
keywords: Keywords for semantic search
|
||||
importance: 0.0-1.0 importance score
|
||||
source: "explicit" | "inferred" | "conversation"
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
user = user or get_user()
|
||||
return await self._set_memory(
|
||||
user=user,
|
||||
memory_type=MemoryType.LEARNED_FACT,
|
||||
key=key,
|
||||
value=value,
|
||||
keywords=keywords or [key],
|
||||
importance=importance,
|
||||
source=source,
|
||||
)
|
||||
|
||||
async def get_fact(self, key: str, user: str | None = None) -> str | None:
|
||||
"""
|
||||
Get a specific fact by key.
|
||||
|
||||
For semantic/fuzzy queries, use the Memory Agent.
|
||||
"""
|
||||
user = user or get_user()
|
||||
return await self._get_memory(user, MemoryType.LEARNED_FACT, key)
|
||||
|
||||
# =========================================================================
|
||||
# Session Context (Redis-backed, 24h TTL)
|
||||
# =========================================================================
|
||||
|
||||
async def get_session_context(
|
||||
self,
|
||||
conversation_id: str | None = None,
|
||||
user: str | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""
|
||||
Get session context for current conversation.
|
||||
|
||||
Args:
|
||||
conversation_id: Conversation ID (defaults to current context)
|
||||
user: User ID (defaults to current context)
|
||||
|
||||
Returns:
|
||||
Session context dict or None
|
||||
"""
|
||||
user = user or get_user()
|
||||
conversation_id = conversation_id or get_conversation_id()
|
||||
|
||||
if not conversation_id:
|
||||
return None
|
||||
|
||||
return await self.cache.get_session_context(user, conversation_id)
|
||||
|
||||
async def set_session_context(
|
||||
self,
|
||||
context: dict[str, Any],
|
||||
conversation_id: str | None = None,
|
||||
user: str | None = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Set session context for current conversation.
|
||||
|
||||
Args:
|
||||
context: Context data to store
|
||||
conversation_id: Conversation ID
|
||||
user: User ID
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
user = user or get_user()
|
||||
conversation_id = conversation_id or get_conversation_id()
|
||||
|
||||
if not conversation_id:
|
||||
logger.warning("memory_service_no_conversation_id")
|
||||
return False
|
||||
|
||||
return await self.cache.set_session_context(user, conversation_id, context)
|
||||
|
||||
async def update_session_context(
|
||||
self,
|
||||
updates: dict[str, Any],
|
||||
conversation_id: str | None = None,
|
||||
user: str | None = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Update session context (merge with existing).
|
||||
|
||||
Args:
|
||||
updates: Fields to update
|
||||
conversation_id: Conversation ID
|
||||
user: User ID
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
user = user or get_user()
|
||||
conversation_id = conversation_id or get_conversation_id()
|
||||
|
||||
if not conversation_id:
|
||||
return False
|
||||
|
||||
return await self.cache.update_session_context(user, conversation_id, updates)
|
||||
|
||||
async def get_recent_entities(
|
||||
self,
|
||||
conversation_id: str | None = None,
|
||||
user: str | None = None,
|
||||
) -> list[str]:
|
||||
"""
|
||||
Get recently mentioned entities in conversation.
|
||||
|
||||
Returns:
|
||||
List of entity names
|
||||
"""
|
||||
user = user or get_user()
|
||||
conversation_id = conversation_id or get_conversation_id()
|
||||
|
||||
if not conversation_id:
|
||||
return []
|
||||
|
||||
return await self.cache.get_recent_entities(user, conversation_id)
|
||||
|
||||
async def add_recent_entities(
|
||||
self,
|
||||
entities: list[str],
|
||||
conversation_id: str | None = None,
|
||||
user: str | None = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Add entities to recent entities set.
|
||||
|
||||
Args:
|
||||
entities: Entity names to add
|
||||
conversation_id: Conversation ID
|
||||
user: User ID
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
user = user or get_user()
|
||||
conversation_id = conversation_id or get_conversation_id()
|
||||
|
||||
if not conversation_id:
|
||||
return False
|
||||
|
||||
return await self.cache.add_recent_entities(user, conversation_id, entities)
|
||||
|
||||
# =========================================================================
|
||||
# Bulk / Pre-fetch Methods (for Steward)
|
||||
# =========================================================================
|
||||
|
||||
async def prefetch_context(
|
||||
self,
|
||||
user: str | None = None,
|
||||
include_profile: bool = True,
|
||||
include_preferences: bool = True,
|
||||
profile_keys: list[str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
Pre-fetch commonly needed context for Steward.
|
||||
|
||||
This is the main entry point for Steward to get user context
|
||||
before analyzing a request.
|
||||
|
||||
Args:
|
||||
user: User ID
|
||||
include_profile: Include profile data
|
||||
include_preferences: Include preferences
|
||||
profile_keys: Specific profile keys to fetch (None = common ones)
|
||||
|
||||
Returns:
|
||||
Dict with profile and preferences data
|
||||
|
||||
Example:
|
||||
>>> ctx = await memory_service.prefetch_context()
|
||||
>>> ctx
|
||||
{
|
||||
"profile": {"location": "Amsterdam", "timezone": "Europe/Amsterdam"},
|
||||
"preferences": {"temperature_unit": "celsius"}
|
||||
}
|
||||
"""
|
||||
user = user or get_user()
|
||||
result: dict[str, Any] = {}
|
||||
|
||||
if include_profile:
|
||||
profile_keys = profile_keys or ["location", "timezone", "name"]
|
||||
profile = {}
|
||||
for key in profile_keys:
|
||||
value = await self.get_profile(key, user)
|
||||
if value:
|
||||
profile[key] = value
|
||||
if profile:
|
||||
result["profile"] = profile
|
||||
|
||||
if include_preferences:
|
||||
preferences = await self.get_all_preferences(user)
|
||||
if preferences:
|
||||
result["preferences"] = preferences
|
||||
|
||||
logger.debug(
|
||||
"memory_service_prefetch",
|
||||
user=user,
|
||||
profile_keys=list(result.get("profile", {}).keys()),
|
||||
preference_keys=list(result.get("preferences", {}).keys()),
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
# =========================================================================
|
||||
# Internal Methods
|
||||
# =========================================================================
|
||||
|
||||
async def _get_memory(
|
||||
self,
|
||||
user: str,
|
||||
memory_type: MemoryType,
|
||||
key: str,
|
||||
) -> str | None:
|
||||
"""Get a memory by type and key (exact match)."""
|
||||
collection = get_memory_collection_name(user)
|
||||
|
||||
try:
|
||||
# Search with filter for exact type + key match
|
||||
# We use a dummy vector since we're filtering by payload
|
||||
results = self.qdrant._client.scroll(
|
||||
collection_name=collection,
|
||||
scroll_filter={
|
||||
"must": [
|
||||
{"key": "type", "match": {"value": memory_type.value}},
|
||||
{"key": "key", "match": {"value": key}},
|
||||
]
|
||||
},
|
||||
limit=1,
|
||||
with_payload=True,
|
||||
with_vectors=False,
|
||||
)
|
||||
|
||||
points, _ = results
|
||||
if points:
|
||||
return points[0].payload.get("value")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"memory_service_get_failed",
|
||||
user=user,
|
||||
type=memory_type.value,
|
||||
key=key,
|
||||
error=str(e),
|
||||
)
|
||||
return None
|
||||
|
||||
async def _set_memory(
|
||||
self,
|
||||
user: str,
|
||||
memory_type: MemoryType,
|
||||
key: str,
|
||||
value: str,
|
||||
keywords: list[str],
|
||||
importance: float = 0.5,
|
||||
source: str = "explicit",
|
||||
) -> bool:
|
||||
"""Set a memory (upsert by type + key)."""
|
||||
try:
|
||||
# Generate embedding for semantic search
|
||||
embedding = await self.embedding.embed(f"{key}: {value}")
|
||||
if not embedding:
|
||||
logger.error("memory_service_embedding_failed", key=key)
|
||||
return False
|
||||
|
||||
# Create memory ID from type + key for idempotent upserts
|
||||
memory_id = f"{memory_type.value}:{key}"
|
||||
|
||||
payload = {
|
||||
"type": memory_type.value,
|
||||
"key": key,
|
||||
"value": value,
|
||||
"keywords": keywords,
|
||||
"importance": importance,
|
||||
"source": source,
|
||||
"updated_at": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
|
||||
result = await self.qdrant.upsert_memory(
|
||||
user=user,
|
||||
memory_id=memory_id,
|
||||
vector=embedding,
|
||||
payload=payload,
|
||||
)
|
||||
|
||||
if result:
|
||||
logger.debug(
|
||||
"memory_service_set",
|
||||
user=user,
|
||||
type=memory_type.value,
|
||||
key=key,
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"memory_service_set_failed",
|
||||
user=user,
|
||||
type=memory_type.value,
|
||||
key=key,
|
||||
error=str(e),
|
||||
)
|
||||
return False
|
||||
|
||||
async def _get_all_by_type(
|
||||
self,
|
||||
user: str,
|
||||
memory_type: MemoryType,
|
||||
limit: int = 100,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Get all memories of a specific type."""
|
||||
collection = get_memory_collection_name(user)
|
||||
|
||||
try:
|
||||
results = self.qdrant._client.scroll(
|
||||
collection_name=collection,
|
||||
scroll_filter={
|
||||
"must": [
|
||||
{"key": "type", "match": {"value": memory_type.value}},
|
||||
]
|
||||
},
|
||||
limit=limit,
|
||||
with_payload=True,
|
||||
with_vectors=False,
|
||||
)
|
||||
|
||||
points, _ = results
|
||||
return [p.payload for p in points]
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"memory_service_get_all_failed",
|
||||
user=user,
|
||||
type=memory_type.value,
|
||||
error=str(e),
|
||||
)
|
||||
return []
|
||||
|
||||
async def delete_memory(
|
||||
self,
|
||||
key: str,
|
||||
memory_type: MemoryType,
|
||||
user: str | None = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Delete a specific memory.
|
||||
|
||||
Args:
|
||||
key: Memory key
|
||||
memory_type: Type of memory
|
||||
user: User ID
|
||||
|
||||
Returns:
|
||||
True if deleted
|
||||
"""
|
||||
user = user or get_user()
|
||||
memory_id = f"{memory_type.value}:{key}"
|
||||
|
||||
return await self.qdrant.delete_memory(user, memory_id)
|
||||
|
||||
|
||||
# Global service instance
|
||||
memory_service = MemoryService()
|
||||
@@ -0,0 +1,147 @@
|
||||
"""
|
||||
Multi-tenancy helpers for Tatlock.
|
||||
|
||||
Provides utilities for user namespace management across:
|
||||
- Qdrant (collection per user for memories)
|
||||
- Redis (user-scoped keys for session context)
|
||||
|
||||
Adapted from library-desk patterns.
|
||||
"""
|
||||
import re
|
||||
|
||||
|
||||
def sanitize_user_id(user_id: str) -> str:
|
||||
"""
|
||||
Sanitize user ID for use in collection names, keys, and paths.
|
||||
|
||||
Converts special characters to underscores and ensures alphanumeric safety.
|
||||
|
||||
Args:
|
||||
user_id: Raw user identifier (email, username, etc.)
|
||||
|
||||
Returns:
|
||||
Sanitized user ID safe for use in identifiers
|
||||
|
||||
Examples:
|
||||
>>> sanitize_user_id("john@example.com")
|
||||
'john_at_example_com'
|
||||
>>> sanitize_user_id("user.name")
|
||||
'user_name'
|
||||
>>> sanitize_user_id("User Name")
|
||||
'user_name'
|
||||
"""
|
||||
sanitized = user_id.lower()
|
||||
|
||||
# Convert @ to _at_
|
||||
sanitized = sanitized.replace("@", "_at_")
|
||||
|
||||
# Convert dots to underscores
|
||||
sanitized = sanitized.replace(".", "_")
|
||||
|
||||
# Replace any non-alphanumeric characters with underscores
|
||||
sanitized = re.sub(r'[^a-z0-9_]', '_', sanitized)
|
||||
|
||||
# Remove consecutive underscores
|
||||
sanitized = re.sub(r'_+', '_', sanitized)
|
||||
|
||||
# Remove leading/trailing underscores
|
||||
sanitized = sanitized.strip('_')
|
||||
|
||||
return sanitized
|
||||
|
||||
|
||||
def get_memory_collection_name(user_id: str) -> str:
|
||||
"""
|
||||
Get Qdrant collection name for user's memories.
|
||||
|
||||
Pattern: memories_{sanitized_user_id}
|
||||
|
||||
Args:
|
||||
user_id: User identifier
|
||||
|
||||
Returns:
|
||||
Qdrant collection name
|
||||
|
||||
Examples:
|
||||
>>> get_memory_collection_name("jpmschweitzer")
|
||||
'memories_jpmschweitzer'
|
||||
>>> get_memory_collection_name("john@example.com")
|
||||
'memories_john_at_example_com'
|
||||
"""
|
||||
sanitized = sanitize_user_id(user_id)
|
||||
return f"memories_{sanitized}"
|
||||
|
||||
|
||||
def get_session_key(user_id: str, conversation_id: str) -> str:
|
||||
"""
|
||||
Get Redis key for session context.
|
||||
|
||||
Pattern: session:{sanitized_user}:{conversation_id}
|
||||
|
||||
Args:
|
||||
user_id: User identifier
|
||||
conversation_id: Conversation identifier
|
||||
|
||||
Returns:
|
||||
Redis key for session context
|
||||
|
||||
Examples:
|
||||
>>> get_session_key("jpmschweitzer", "conv_abc123")
|
||||
'session:jpmschweitzer:conv_abc123'
|
||||
"""
|
||||
sanitized = sanitize_user_id(user_id)
|
||||
return f"session:{sanitized}:{conversation_id}"
|
||||
|
||||
|
||||
def get_entities_key(user_id: str, conversation_id: str) -> str:
|
||||
"""
|
||||
Get Redis key for recent entities in a conversation.
|
||||
|
||||
Pattern: entities:{sanitized_user}:{conversation_id}
|
||||
|
||||
Args:
|
||||
user_id: User identifier
|
||||
conversation_id: Conversation identifier
|
||||
|
||||
Returns:
|
||||
Redis key for recent entities
|
||||
|
||||
Examples:
|
||||
>>> get_entities_key("jpmschweitzer", "conv_abc123")
|
||||
'entities:jpmschweitzer:conv_abc123'
|
||||
"""
|
||||
sanitized = sanitize_user_id(user_id)
|
||||
return f"entities:{sanitized}:{conversation_id}"
|
||||
|
||||
|
||||
def validate_user_id(user_id: str) -> bool:
|
||||
"""
|
||||
Validate that a user ID is acceptable.
|
||||
|
||||
Checks:
|
||||
- Not empty
|
||||
- Not too long (max 100 chars)
|
||||
- Contains some alphanumeric characters
|
||||
|
||||
Args:
|
||||
user_id: User identifier to validate
|
||||
|
||||
Returns:
|
||||
True if valid, False otherwise
|
||||
|
||||
Examples:
|
||||
>>> validate_user_id("jpmschweitzer")
|
||||
True
|
||||
>>> validate_user_id("")
|
||||
False
|
||||
>>> validate_user_id("a" * 101)
|
||||
False
|
||||
"""
|
||||
if not user_id or len(user_id) > 100:
|
||||
return False
|
||||
|
||||
# Must contain at least one alphanumeric character
|
||||
if not re.search(r'[a-zA-Z0-9]', user_id):
|
||||
return False
|
||||
|
||||
return True
|
||||
@@ -4,6 +4,7 @@ Request preprocessing pipeline.
|
||||
Analyzes requests via the Steward and creates scoped toolsets for Tatlock.
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Any, Optional
|
||||
|
||||
from src.agents.steward import analyze_request, format_steward_note
|
||||
@@ -14,6 +15,23 @@ from src.core.logging_config import get_logger
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def _inject_temporal_context(request: str) -> str:
|
||||
"""
|
||||
Append current time context to user request.
|
||||
|
||||
Provides Tatlock with temporal awareness for time-sensitive queries.
|
||||
|
||||
Args:
|
||||
request: Original user request
|
||||
|
||||
Returns:
|
||||
Request with appended time context
|
||||
"""
|
||||
now = datetime.now()
|
||||
time_str = now.strftime("%Y-%m-%d %H:%M")
|
||||
return f"{request}\n\n[Current time: {time_str}]"
|
||||
|
||||
|
||||
@dataclass
|
||||
class EnrichedRequest:
|
||||
"""
|
||||
@@ -65,6 +83,9 @@ async def preprocess_request(
|
||||
>>> print(len(enriched.scoped_tools))
|
||||
5 # All tatlock_core tools
|
||||
"""
|
||||
# Inject temporal context for time-aware processing
|
||||
enriched_request = _inject_temporal_context(user_request)
|
||||
|
||||
logger.info(
|
||||
"preprocessing_request",
|
||||
request_preview=user_request[:100],
|
||||
@@ -74,7 +95,7 @@ async def preprocess_request(
|
||||
|
||||
# Call Steward with full conversation history
|
||||
recommendation = await analyze_request(
|
||||
user_request,
|
||||
enriched_request,
|
||||
conversation_history=conversation_history,
|
||||
conversation_id=conversation_id,
|
||||
)
|
||||
@@ -82,9 +103,11 @@ async def preprocess_request(
|
||||
# Format note for Tatlock (includes conversation context)
|
||||
steward_note = await format_steward_note(recommendation)
|
||||
|
||||
# Get scoped tools from household registry
|
||||
# Get delegation tools from household registry
|
||||
# Uses agent-as-tool pattern: expert agents get delegation wrappers,
|
||||
# core tools are returned directly
|
||||
registry = get_household_registry()
|
||||
scoped_tools = registry.get_scoped_tools(
|
||||
scoped_tools = registry.get_delegation_tools(
|
||||
recommendation.recommended_capabilities
|
||||
)
|
||||
|
||||
@@ -97,7 +120,7 @@ async def preprocess_request(
|
||||
)
|
||||
|
||||
return EnrichedRequest(
|
||||
original_request=user_request,
|
||||
original_request=enriched_request,
|
||||
steward_note=steward_note,
|
||||
scoped_tools=scoped_tools,
|
||||
recommendation=recommendation,
|
||||
|
||||
@@ -0,0 +1,446 @@
|
||||
"""
|
||||
Qdrant client wrapper for memory vector storage.
|
||||
|
||||
Provides async operations for storing and retrieving memory embeddings:
|
||||
- Collection management (per-user collections)
|
||||
- Memory upsert/search/delete
|
||||
- Filtering by memory type
|
||||
|
||||
Adapted from library-desk patterns.
|
||||
"""
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from qdrant_client import QdrantClient
|
||||
from qdrant_client.http import models as qdrant_models
|
||||
|
||||
from .config import config
|
||||
from .logging_config import get_logger
|
||||
from .multi_tenancy import get_memory_collection_name
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class MemoryQdrantClient:
|
||||
"""
|
||||
Qdrant client wrapper for memory storage.
|
||||
|
||||
Manages per-user collections with the pattern: memories_{user}
|
||||
Stores memory embeddings with metadata (type, content, timestamps).
|
||||
|
||||
Usage:
|
||||
client = MemoryQdrantClient()
|
||||
await client.ensure_collection("jpmschweitzer")
|
||||
await client.upsert_memory(
|
||||
user="jpmschweitzer",
|
||||
memory_id="mem_123",
|
||||
vector=[0.1, 0.2, ...],
|
||||
payload={"type": "fact", "content": "User prefers dark mode"}
|
||||
)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
url: str | None = None,
|
||||
embedding_dim: int | None = None,
|
||||
):
|
||||
"""
|
||||
Initialize Qdrant client.
|
||||
|
||||
Args:
|
||||
url: Qdrant server URL (defaults to config.qdrant_url)
|
||||
embedding_dim: Vector dimension (defaults to config.QDRANT_EMBEDDING_DIM)
|
||||
"""
|
||||
self.url = url or config.qdrant_url
|
||||
self.embedding_dim = embedding_dim or config.QDRANT_EMBEDDING_DIM
|
||||
self._client = QdrantClient(url=self.url)
|
||||
|
||||
logger.info(
|
||||
"qdrant_client_initialized",
|
||||
url=self.url,
|
||||
embedding_dim=self.embedding_dim,
|
||||
)
|
||||
|
||||
def close(self) -> None:
|
||||
"""Close Qdrant client."""
|
||||
if self._client is not None:
|
||||
self._client.close()
|
||||
|
||||
async def ensure_collection(self, user: str) -> bool:
|
||||
"""
|
||||
Ensure collection exists for user, create if not.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
|
||||
Returns:
|
||||
True if collection exists or was created successfully
|
||||
|
||||
Example:
|
||||
>>> await client.ensure_collection("jpmschweitzer")
|
||||
True
|
||||
"""
|
||||
collection_name = get_memory_collection_name(user)
|
||||
|
||||
try:
|
||||
# Check if collection exists
|
||||
collections = self._client.get_collections()
|
||||
existing = [c.name for c in collections.collections]
|
||||
|
||||
if collection_name in existing:
|
||||
logger.debug(
|
||||
"qdrant_collection_exists",
|
||||
collection=collection_name,
|
||||
)
|
||||
return True
|
||||
|
||||
# Create collection with cosine distance
|
||||
self._client.create_collection(
|
||||
collection_name=collection_name,
|
||||
vectors_config=qdrant_models.VectorParams(
|
||||
size=self.embedding_dim,
|
||||
distance=qdrant_models.Distance.COSINE,
|
||||
),
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"qdrant_collection_created",
|
||||
collection=collection_name,
|
||||
embedding_dim=self.embedding_dim,
|
||||
)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"qdrant_ensure_collection_failed",
|
||||
collection=collection_name,
|
||||
error=str(e),
|
||||
)
|
||||
return False
|
||||
|
||||
async def upsert_memory(
|
||||
self,
|
||||
user: str,
|
||||
memory_id: str | None,
|
||||
vector: list[float],
|
||||
payload: dict[str, Any],
|
||||
) -> str | None:
|
||||
"""
|
||||
Upsert a memory point.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
memory_id: Memory ID (generated if None)
|
||||
vector: Embedding vector
|
||||
payload: Memory metadata (should include 'type', 'content', etc.)
|
||||
|
||||
Returns:
|
||||
Memory ID if successful, None on failure
|
||||
|
||||
Example:
|
||||
>>> memory_id = await client.upsert_memory(
|
||||
... user="jpmschweitzer",
|
||||
... memory_id=None,
|
||||
... vector=[0.1, 0.2, ...],
|
||||
... payload={
|
||||
... "type": "fact",
|
||||
... "content": "User prefers dark mode",
|
||||
... "created_at": "2024-01-01T00:00:00Z"
|
||||
... }
|
||||
... )
|
||||
"""
|
||||
collection_name = get_memory_collection_name(user)
|
||||
memory_id = memory_id or f"mem_{uuid4().hex[:16]}"
|
||||
|
||||
try:
|
||||
# Ensure collection exists
|
||||
await self.ensure_collection(user)
|
||||
|
||||
# Create point
|
||||
point = qdrant_models.PointStruct(
|
||||
id=memory_id,
|
||||
vector=vector,
|
||||
payload=payload,
|
||||
)
|
||||
|
||||
# Upsert
|
||||
self._client.upsert(
|
||||
collection_name=collection_name,
|
||||
points=[point],
|
||||
)
|
||||
|
||||
logger.debug(
|
||||
"qdrant_memory_upserted",
|
||||
collection=collection_name,
|
||||
memory_id=memory_id,
|
||||
memory_type=payload.get("type"),
|
||||
)
|
||||
return memory_id
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"qdrant_upsert_memory_failed",
|
||||
collection=collection_name,
|
||||
memory_id=memory_id,
|
||||
error=str(e),
|
||||
)
|
||||
return None
|
||||
|
||||
async def search_memories(
|
||||
self,
|
||||
user: str,
|
||||
query_vector: list[float],
|
||||
limit: int = 10,
|
||||
memory_type: str | None = None,
|
||||
score_threshold: float = 0.5,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Search memories by vector similarity.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
query_vector: Query embedding vector
|
||||
limit: Maximum results
|
||||
memory_type: Filter by memory type (e.g., "fact", "preference", "profile")
|
||||
score_threshold: Minimum similarity score (0-1)
|
||||
|
||||
Returns:
|
||||
List of matching memories with scores
|
||||
|
||||
Example:
|
||||
>>> memories = await client.search_memories(
|
||||
... user="jpmschweitzer",
|
||||
... query_vector=[0.1, 0.2, ...],
|
||||
... limit=5,
|
||||
... memory_type="fact"
|
||||
... )
|
||||
>>> memories[0]
|
||||
{"id": "mem_123", "score": 0.89, "type": "fact", "content": "..."}
|
||||
"""
|
||||
collection_name = get_memory_collection_name(user)
|
||||
|
||||
try:
|
||||
# Build filter if memory_type specified
|
||||
query_filter = None
|
||||
if memory_type:
|
||||
query_filter = qdrant_models.Filter(
|
||||
must=[
|
||||
qdrant_models.FieldCondition(
|
||||
key="type",
|
||||
match=qdrant_models.MatchValue(value=memory_type),
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
# Search
|
||||
results = self._client.search(
|
||||
collection_name=collection_name,
|
||||
query_vector=query_vector,
|
||||
limit=limit,
|
||||
query_filter=query_filter,
|
||||
score_threshold=score_threshold,
|
||||
)
|
||||
|
||||
# Format results
|
||||
memories = []
|
||||
for hit in results:
|
||||
memory = {
|
||||
"id": hit.id,
|
||||
"score": hit.score,
|
||||
**hit.payload,
|
||||
}
|
||||
memories.append(memory)
|
||||
|
||||
logger.debug(
|
||||
"qdrant_search_memories",
|
||||
collection=collection_name,
|
||||
results_count=len(memories),
|
||||
memory_type=memory_type,
|
||||
)
|
||||
return memories
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"qdrant_search_memories_failed",
|
||||
collection=collection_name,
|
||||
error=str(e),
|
||||
)
|
||||
return []
|
||||
|
||||
async def get_memory(self, user: str, memory_id: str) -> dict[str, Any] | None:
|
||||
"""
|
||||
Get a specific memory by ID.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
memory_id: Memory ID
|
||||
|
||||
Returns:
|
||||
Memory data or None if not found
|
||||
"""
|
||||
collection_name = get_memory_collection_name(user)
|
||||
|
||||
try:
|
||||
points = self._client.retrieve(
|
||||
collection_name=collection_name,
|
||||
ids=[memory_id],
|
||||
)
|
||||
|
||||
if not points:
|
||||
return None
|
||||
|
||||
point = points[0]
|
||||
return {
|
||||
"id": point.id,
|
||||
**point.payload,
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"qdrant_get_memory_failed",
|
||||
collection=collection_name,
|
||||
memory_id=memory_id,
|
||||
error=str(e),
|
||||
)
|
||||
return None
|
||||
|
||||
async def delete_memory(self, user: str, memory_id: str) -> bool:
|
||||
"""
|
||||
Delete a memory by ID.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
memory_id: Memory ID to delete
|
||||
|
||||
Returns:
|
||||
True if deleted successfully, False otherwise
|
||||
|
||||
Example:
|
||||
>>> await client.delete_memory("jpmschweitzer", "mem_123")
|
||||
True
|
||||
"""
|
||||
collection_name = get_memory_collection_name(user)
|
||||
|
||||
try:
|
||||
self._client.delete(
|
||||
collection_name=collection_name,
|
||||
points_selector=qdrant_models.PointIdsList(
|
||||
points=[memory_id],
|
||||
),
|
||||
)
|
||||
|
||||
logger.debug(
|
||||
"qdrant_memory_deleted",
|
||||
collection=collection_name,
|
||||
memory_id=memory_id,
|
||||
)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"qdrant_delete_memory_failed",
|
||||
collection=collection_name,
|
||||
memory_id=memory_id,
|
||||
error=str(e),
|
||||
)
|
||||
return False
|
||||
|
||||
async def delete_memories_by_type(self, user: str, memory_type: str) -> int:
|
||||
"""
|
||||
Delete all memories of a specific type.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
memory_type: Type of memories to delete
|
||||
|
||||
Returns:
|
||||
Number of memories deleted (approximate)
|
||||
"""
|
||||
collection_name = get_memory_collection_name(user)
|
||||
|
||||
try:
|
||||
# Delete by filter
|
||||
self._client.delete(
|
||||
collection_name=collection_name,
|
||||
points_selector=qdrant_models.FilterSelector(
|
||||
filter=qdrant_models.Filter(
|
||||
must=[
|
||||
qdrant_models.FieldCondition(
|
||||
key="type",
|
||||
match=qdrant_models.MatchValue(value=memory_type),
|
||||
)
|
||||
]
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"qdrant_memories_deleted_by_type",
|
||||
collection=collection_name,
|
||||
memory_type=memory_type,
|
||||
)
|
||||
return -1 # Qdrant doesn't return count for filter deletes
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"qdrant_delete_memories_by_type_failed",
|
||||
collection=collection_name,
|
||||
memory_type=memory_type,
|
||||
error=str(e),
|
||||
)
|
||||
return 0
|
||||
|
||||
async def count_memories(self, user: str) -> int:
|
||||
"""
|
||||
Count total memories for a user.
|
||||
|
||||
Args:
|
||||
user: User identifier
|
||||
|
||||
Returns:
|
||||
Number of memories in user's collection
|
||||
"""
|
||||
collection_name = get_memory_collection_name(user)
|
||||
|
||||
try:
|
||||
info = self._client.get_collection(collection_name)
|
||||
return info.points_count
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"qdrant_count_memories_failed",
|
||||
collection=collection_name,
|
||||
error=str(e),
|
||||
)
|
||||
return 0
|
||||
|
||||
async def health_check(self) -> bool:
|
||||
"""
|
||||
Check if Qdrant server is reachable.
|
||||
|
||||
Returns:
|
||||
True if healthy, False otherwise
|
||||
"""
|
||||
try:
|
||||
self._client.get_collections()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error("qdrant_health_check_failed", error=str(e))
|
||||
return False
|
||||
|
||||
|
||||
# Global client instance (lazy initialization)
|
||||
_qdrant_client: MemoryQdrantClient | None = None
|
||||
|
||||
|
||||
def get_qdrant_client() -> MemoryQdrantClient:
|
||||
"""
|
||||
Get global Qdrant client instance.
|
||||
|
||||
Returns:
|
||||
MemoryQdrantClient instance
|
||||
"""
|
||||
global _qdrant_client
|
||||
if _qdrant_client is None:
|
||||
_qdrant_client = MemoryQdrantClient()
|
||||
return _qdrant_client
|
||||
+24
-5
@@ -5,6 +5,8 @@ Handles initialization of household registry and other startup tasks.
|
||||
This module should be called during application startup to register
|
||||
all household members.
|
||||
"""
|
||||
from src.agents.biographer import register_biographer
|
||||
from src.agents.librarian import register_librarian
|
||||
from src.agents.tatlock_core import TATLOCK_CORE_CAPABILITY, tatlock_core_tools
|
||||
from src.core.household_registry import get_household_registry
|
||||
from src.core.logging_config import get_logger
|
||||
@@ -21,11 +23,8 @@ def register_household_members():
|
||||
|
||||
Currently registers:
|
||||
- tatlock_core: Butler's core tools (calculator, datetime, web search)
|
||||
|
||||
Future phases will add:
|
||||
- librarian: Research and knowledge management
|
||||
- developer: Software development assistance
|
||||
- etc.
|
||||
- librarian: Research and knowledge management (Phase 3)
|
||||
- biographer: User memory and context management (Phase F)
|
||||
"""
|
||||
registry = get_household_registry()
|
||||
|
||||
@@ -45,6 +44,26 @@ def register_household_members():
|
||||
tool_count=len(tatlock_core_tools),
|
||||
)
|
||||
|
||||
# Register The Librarian (Phase 3)
|
||||
try:
|
||||
register_librarian()
|
||||
except Exception as e:
|
||||
# Don't fail startup if Librarian registration fails
|
||||
logger.warning(
|
||||
"librarian_registration_failed",
|
||||
error=str(e),
|
||||
)
|
||||
|
||||
# Register The Biographer (Phase F)
|
||||
try:
|
||||
register_biographer()
|
||||
except Exception as e:
|
||||
# Don't fail startup if Biographer registration fails
|
||||
logger.warning(
|
||||
"biographer_registration_failed",
|
||||
error=str(e),
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"household_registration_complete",
|
||||
total_members=len(registry),
|
||||
|
||||
@@ -11,6 +11,7 @@ from sse_starlette.sse import EventSourceResponse
|
||||
from src.responses import service
|
||||
from src.responses.schemas import ResponseRequest, Response
|
||||
from src.core.exceptions import ModelNotFoundError, AppException
|
||||
from src.core.context import current_user, current_conversation
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -94,6 +95,11 @@ async def create_response(
|
||||
"""
|
||||
logger.info(f"Response request for model: {request.model}")
|
||||
|
||||
# Set request context (propagates through all async calls)
|
||||
user_token = current_user.set(request.user or "jpmschweitzer")
|
||||
conv_id = request.metadata.get("conversation_id") if request.metadata else None
|
||||
conv_token = current_conversation.set(conv_id)
|
||||
|
||||
try:
|
||||
# Check if this is a Tatlock request - use Steward preprocessing (Phase 2)
|
||||
model_id = request.model
|
||||
@@ -136,3 +142,8 @@ async def create_response(
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
finally:
|
||||
# Reset context (important for connection reuse)
|
||||
current_user.reset(user_token)
|
||||
current_conversation.reset(conv_token)
|
||||
|
||||
@@ -138,6 +138,10 @@ class ResponseRequest(CustomBaseModel):
|
||||
default=None,
|
||||
description="Stop sequences"
|
||||
)
|
||||
user: str | None = Field(
|
||||
default=None,
|
||||
description="Unique identifier for end-user (OpenAI standard)"
|
||||
)
|
||||
|
||||
@field_validator('reasoning')
|
||||
@classmethod
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
"""Tests for The Biographer agent."""
|
||||
@@ -0,0 +1,145 @@
|
||||
"""
|
||||
Tests for Biographer capability registration.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from src.agents.biographer.capability import (
|
||||
BIOGRAPHER_CAPABILITY,
|
||||
get_biographer_capability,
|
||||
register_biographer,
|
||||
unregister_biographer,
|
||||
)
|
||||
from src.core.household_registry import HouseholdCapability
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestBiographerCapability:
|
||||
"""Tests for the Biographer capability definition."""
|
||||
|
||||
def test_capability_is_household_capability(self):
|
||||
"""Test capability is correct type."""
|
||||
assert isinstance(BIOGRAPHER_CAPABILITY, HouseholdCapability)
|
||||
|
||||
def test_capability_name(self):
|
||||
"""Test capability has correct name."""
|
||||
assert BIOGRAPHER_CAPABILITY.name == "biographer"
|
||||
|
||||
def test_capability_role(self):
|
||||
"""Test capability has correct role."""
|
||||
assert BIOGRAPHER_CAPABILITY.role == "The Biographer"
|
||||
|
||||
def test_capability_category(self):
|
||||
"""Test capability is in context category."""
|
||||
assert BIOGRAPHER_CAPABILITY.category == "context"
|
||||
|
||||
def test_capability_domains(self):
|
||||
"""Test capability covers expected domains."""
|
||||
domains = BIOGRAPHER_CAPABILITY.domains
|
||||
|
||||
assert "remember" in domains
|
||||
assert "recall" in domains
|
||||
assert "forget" in domains
|
||||
assert "memory" in domains
|
||||
assert "preferences" in domains
|
||||
assert "profile" in domains
|
||||
|
||||
def test_capability_does_not_require_network(self):
|
||||
"""Test capability does not require network access."""
|
||||
assert BIOGRAPHER_CAPABILITY.requires_network is False
|
||||
|
||||
def test_capability_low_cost(self):
|
||||
"""Test capability has low cost (vector search, minimal LLM)."""
|
||||
assert BIOGRAPHER_CAPABILITY.cost == "low"
|
||||
|
||||
def test_get_biographer_capability(self):
|
||||
"""Test getter returns same capability."""
|
||||
cap = get_biographer_capability()
|
||||
|
||||
assert cap is BIOGRAPHER_CAPABILITY
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestBiographerRegistration:
|
||||
"""Tests for Biographer registration functions."""
|
||||
|
||||
def test_register_biographer(self):
|
||||
"""Test registering biographer with registry."""
|
||||
mock_registry = MagicMock()
|
||||
mock_registry.__contains__ = MagicMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"src.agents.biographer.capability.get_household_registry",
|
||||
return_value=mock_registry,
|
||||
):
|
||||
with patch(
|
||||
"src.agents.biographer.capability.get_biographer_agent"
|
||||
) as mock_get_agent:
|
||||
mock_agent = MagicMock()
|
||||
mock_get_agent.return_value = mock_agent
|
||||
|
||||
register_biographer()
|
||||
|
||||
mock_registry.register.assert_called_once()
|
||||
call_kwargs = mock_registry.register.call_args[1]
|
||||
|
||||
assert call_kwargs["name"] == "biographer"
|
||||
assert call_kwargs["capability"] is BIOGRAPHER_CAPABILITY
|
||||
assert call_kwargs["agent"] is mock_agent
|
||||
|
||||
def test_register_biographer_already_registered(self):
|
||||
"""Test registering when already registered does nothing."""
|
||||
mock_registry = MagicMock()
|
||||
mock_registry.__contains__ = MagicMock(return_value=True)
|
||||
|
||||
with patch(
|
||||
"src.agents.biographer.capability.get_household_registry",
|
||||
return_value=mock_registry,
|
||||
):
|
||||
register_biographer()
|
||||
|
||||
# Should not call register since already registered
|
||||
mock_registry.register.assert_not_called()
|
||||
|
||||
def test_unregister_biographer(self):
|
||||
"""Test unregistering biographer from registry."""
|
||||
mock_registry = MagicMock()
|
||||
|
||||
with patch(
|
||||
"src.agents.biographer.capability.get_household_registry",
|
||||
return_value=mock_registry,
|
||||
):
|
||||
unregister_biographer()
|
||||
|
||||
mock_registry.unregister.assert_called_once_with("biographer")
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestCapabilityDescription:
|
||||
"""Tests for capability description."""
|
||||
|
||||
def test_description_mentions_recall(self):
|
||||
"""Test description mentions recall capabilities."""
|
||||
desc = BIOGRAPHER_CAPABILITY.description.lower()
|
||||
assert "recall" in desc
|
||||
|
||||
def test_description_mentions_record(self):
|
||||
"""Test description mentions recording capability."""
|
||||
desc = BIOGRAPHER_CAPABILITY.description.lower()
|
||||
assert "record" in desc
|
||||
|
||||
def test_description_mentions_forget(self):
|
||||
"""Test description mentions forget capability."""
|
||||
desc = BIOGRAPHER_CAPABILITY.description.lower()
|
||||
assert "forget" in desc
|
||||
|
||||
def test_description_mentions_profile(self):
|
||||
"""Test description mentions profile updates."""
|
||||
desc = BIOGRAPHER_CAPABILITY.description.lower()
|
||||
assert "profile" in desc
|
||||
|
||||
def test_description_mentions_preferences(self):
|
||||
"""Test description mentions preferences."""
|
||||
desc = BIOGRAPHER_CAPABILITY.description.lower()
|
||||
assert "preferences" in desc
|
||||
@@ -0,0 +1 @@
|
||||
"""Tests for The Librarian agent."""
|
||||
@@ -0,0 +1,129 @@
|
||||
"""
|
||||
Tests for Librarian capability registration.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from src.agents.librarian.capability import (
|
||||
LIBRARIAN_CAPABILITY,
|
||||
get_librarian_capability,
|
||||
register_librarian,
|
||||
unregister_librarian,
|
||||
)
|
||||
from src.core.household_registry import HouseholdCapability
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestLibrarianCapability:
|
||||
"""Tests for the Librarian capability definition."""
|
||||
|
||||
def test_capability_is_household_capability(self):
|
||||
"""Test capability is correct type."""
|
||||
assert isinstance(LIBRARIAN_CAPABILITY, HouseholdCapability)
|
||||
|
||||
def test_capability_name(self):
|
||||
"""Test capability has correct name."""
|
||||
assert LIBRARIAN_CAPABILITY.name == "librarian"
|
||||
|
||||
def test_capability_role(self):
|
||||
"""Test capability has correct role."""
|
||||
assert LIBRARIAN_CAPABILITY.role == "The Librarian"
|
||||
|
||||
def test_capability_category(self):
|
||||
"""Test capability is in research category."""
|
||||
assert LIBRARIAN_CAPABILITY.category == "research"
|
||||
|
||||
def test_capability_domains(self):
|
||||
"""Test capability covers expected domains."""
|
||||
domains = LIBRARIAN_CAPABILITY.domains
|
||||
|
||||
assert "research" in domains
|
||||
assert "knowledge" in domains
|
||||
assert "wiki" in domains
|
||||
assert "search" in domains
|
||||
|
||||
def test_capability_requires_network(self):
|
||||
"""Test capability requires network access."""
|
||||
assert LIBRARIAN_CAPABILITY.requires_network is True
|
||||
|
||||
def test_get_librarian_capability(self):
|
||||
"""Test getter returns same capability."""
|
||||
cap = get_librarian_capability()
|
||||
|
||||
assert cap is LIBRARIAN_CAPABILITY
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestLibrarianRegistration:
|
||||
"""Tests for Librarian registration functions."""
|
||||
|
||||
def test_register_librarian(self):
|
||||
"""Test registering librarian with registry."""
|
||||
mock_registry = MagicMock()
|
||||
mock_registry.__contains__ = MagicMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"src.agents.librarian.capability.get_household_registry",
|
||||
return_value=mock_registry,
|
||||
):
|
||||
with patch(
|
||||
"src.agents.librarian.capability.get_librarian_agent"
|
||||
) as mock_get_agent:
|
||||
mock_agent = MagicMock()
|
||||
mock_get_agent.return_value = mock_agent
|
||||
|
||||
register_librarian()
|
||||
|
||||
mock_registry.register.assert_called_once()
|
||||
call_kwargs = mock_registry.register.call_args[1]
|
||||
|
||||
assert call_kwargs["name"] == "librarian"
|
||||
assert call_kwargs["capability"] is LIBRARIAN_CAPABILITY
|
||||
assert call_kwargs["agent"] is mock_agent
|
||||
|
||||
def test_register_librarian_already_registered(self):
|
||||
"""Test registering when already registered does nothing."""
|
||||
mock_registry = MagicMock()
|
||||
mock_registry.__contains__ = MagicMock(return_value=True)
|
||||
|
||||
with patch(
|
||||
"src.agents.librarian.capability.get_household_registry",
|
||||
return_value=mock_registry,
|
||||
):
|
||||
register_librarian()
|
||||
|
||||
# Should not call register since already registered
|
||||
mock_registry.register.assert_not_called()
|
||||
|
||||
def test_unregister_librarian(self):
|
||||
"""Test unregistering librarian from registry."""
|
||||
mock_registry = MagicMock()
|
||||
|
||||
with patch(
|
||||
"src.agents.librarian.capability.get_household_registry",
|
||||
return_value=mock_registry,
|
||||
):
|
||||
unregister_librarian()
|
||||
|
||||
mock_registry.unregister.assert_called_once_with("librarian")
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestCapabilityDescription:
|
||||
"""Tests for capability description."""
|
||||
|
||||
def test_description_mentions_wiki_capabilities(self):
|
||||
"""Test description mentions wiki read/write capabilities."""
|
||||
desc = LIBRARIAN_CAPABILITY.description.lower()
|
||||
assert "create" in desc
|
||||
assert "update" in desc
|
||||
assert "search" in desc
|
||||
|
||||
def test_description_mentions_search(self):
|
||||
"""Test description mentions search capability."""
|
||||
assert "search" in LIBRARIAN_CAPABILITY.description.lower()
|
||||
|
||||
def test_description_mentions_wiki(self):
|
||||
"""Test description mentions wiki access."""
|
||||
assert "wiki" in LIBRARIAN_CAPABILITY.description.lower()
|
||||
@@ -0,0 +1,598 @@
|
||||
"""
|
||||
Tests for the Library-Desk HTTP client.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
import httpx
|
||||
|
||||
from src.agents.librarian.client import (
|
||||
LibraryDeskClient,
|
||||
HybridRAGResponse,
|
||||
HybridSearchResult,
|
||||
WikiPage,
|
||||
WikiSearchResult,
|
||||
VectorSearchResult,
|
||||
GraphNode,
|
||||
Dossier,
|
||||
SmartCreateResponse,
|
||||
ResearchSummary,
|
||||
EntityLinking,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_httpx_client():
|
||||
"""Create a mock httpx client."""
|
||||
return AsyncMock(spec=httpx.AsyncClient)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client_with_mock(mock_httpx_client):
|
||||
"""Create a LibraryDeskClient with mocked httpx client."""
|
||||
client = LibraryDeskClient(
|
||||
base_url="http://test:8089",
|
||||
api_key="test-key",
|
||||
)
|
||||
client._client = mock_httpx_client
|
||||
return client
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestLibraryDeskClientInit:
|
||||
"""Tests for client initialization."""
|
||||
|
||||
def test_default_initialization(self):
|
||||
"""Test client initializes with defaults from config."""
|
||||
client = LibraryDeskClient()
|
||||
|
||||
assert client.base_url is not None
|
||||
assert client.timeout == 60
|
||||
assert client._client is None
|
||||
|
||||
def test_custom_initialization(self):
|
||||
"""Test client with custom parameters."""
|
||||
client = LibraryDeskClient(
|
||||
base_url="http://custom:9000",
|
||||
api_key="my-api-key",
|
||||
timeout=120,
|
||||
)
|
||||
|
||||
assert client.base_url == "http://custom:9000"
|
||||
assert client.api_key == "my-api-key"
|
||||
assert client.timeout == 120
|
||||
|
||||
def test_ensure_client_not_initialized(self):
|
||||
"""Test _ensure_client raises when not in context."""
|
||||
client = LibraryDeskClient()
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
client._ensure_client()
|
||||
|
||||
assert "not initialized" in str(exc_info.value)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestContextManager:
|
||||
"""Tests for async context manager."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_manager_creates_client(self):
|
||||
"""Test context manager creates httpx client."""
|
||||
async with LibraryDeskClient(
|
||||
base_url="http://test:8089",
|
||||
api_key="test-key",
|
||||
) as client:
|
||||
assert client._client is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_context_manager_closes_client(self):
|
||||
"""Test context manager closes client on exit."""
|
||||
client = LibraryDeskClient(base_url="http://test:8089")
|
||||
|
||||
async with client:
|
||||
assert client._client is not None
|
||||
|
||||
# After exit, client should be None
|
||||
assert client._client is None
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestHybridSearch:
|
||||
"""Tests for hybrid search."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hybrid_search_success(self, client_with_mock, mock_httpx_client):
|
||||
"""Test successful hybrid search."""
|
||||
# Mock response
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"results": [
|
||||
{
|
||||
"source": "vector",
|
||||
"title": "Docker Guide",
|
||||
"content": "Docker networking basics...",
|
||||
"score": 0.95,
|
||||
"page_id": 123,
|
||||
}
|
||||
],
|
||||
"keywords": ["docker", "networking"],
|
||||
"synonyms": ["container"],
|
||||
"formatted_context": "Context here",
|
||||
"timing": {"total": 1.5},
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.post.return_value = mock_response
|
||||
|
||||
result = await client_with_mock.hybrid_search(
|
||||
query="Docker networking",
|
||||
user="testuser",
|
||||
)
|
||||
|
||||
assert isinstance(result, HybridRAGResponse)
|
||||
assert len(result.results) == 1
|
||||
assert result.results[0].title == "Docker Guide"
|
||||
assert result.results[0].source == "vector"
|
||||
assert "docker" in result.keywords
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hybrid_search_empty_results(
|
||||
self, client_with_mock, mock_httpx_client
|
||||
):
|
||||
"""Test hybrid search with no results."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"results": [],
|
||||
"keywords": [],
|
||||
"formatted_context": "",
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.post.return_value = mock_response
|
||||
|
||||
result = await client_with_mock.hybrid_search("nonexistent query")
|
||||
|
||||
assert len(result.results) == 0
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestWikiOperations:
|
||||
"""Tests for wiki operations."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_wiki(self, client_with_mock, mock_httpx_client):
|
||||
"""Test wiki search."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"results": [
|
||||
{
|
||||
"id": 1,
|
||||
"path": "/docs/docker",
|
||||
"title": "Docker Documentation",
|
||||
"description": "Docker docs",
|
||||
}
|
||||
]
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.get.return_value = mock_response
|
||||
|
||||
results = await client_with_mock.search_wiki("docker")
|
||||
|
||||
assert len(results) == 1
|
||||
assert isinstance(results[0], WikiSearchResult)
|
||||
assert results[0].title == "Docker Documentation"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_wiki_page(self, client_with_mock, mock_httpx_client):
|
||||
"""Test getting a wiki page."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"id": 123,
|
||||
"path": "/docs/docker",
|
||||
"title": "Docker Guide",
|
||||
"content": "# Docker\n\nFull content here...",
|
||||
"tags": ["docker", "devops"],
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.get.return_value = mock_response
|
||||
|
||||
page = await client_with_mock.get_wiki_page(123)
|
||||
|
||||
assert isinstance(page, WikiPage)
|
||||
assert page.id == 123
|
||||
assert page.title == "Docker Guide"
|
||||
assert "docker" in page.tags
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_wiki_pages(self, client_with_mock, mock_httpx_client):
|
||||
"""Test listing wiki pages."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"pages": [
|
||||
{"id": 1, "path": "/page1", "title": "Page 1"},
|
||||
{"id": 2, "path": "/page2", "title": "Page 2"},
|
||||
]
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.get.return_value = mock_response
|
||||
|
||||
pages = await client_with_mock.list_wiki_pages()
|
||||
|
||||
assert len(pages) == 2
|
||||
assert pages[0].title == "Page 1"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_dossiers(self, client_with_mock, mock_httpx_client):
|
||||
"""Test listing dossiers."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"dossiers": [
|
||||
{"name": "docker", "page_count": 10},
|
||||
{"name": "kubernetes", "page_count": 5},
|
||||
]
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.get.return_value = mock_response
|
||||
|
||||
dossiers = await client_with_mock.list_dossiers()
|
||||
|
||||
assert len(dossiers) == 2
|
||||
assert isinstance(dossiers[0], Dossier)
|
||||
assert dossiers[0].name == "docker"
|
||||
assert dossiers[0].page_count == 10
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestSemanticSearch:
|
||||
"""Tests for semantic/vector search."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_semantic_search(self, client_with_mock, mock_httpx_client):
|
||||
"""Test semantic search."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"results": [
|
||||
{
|
||||
"page_id": 1,
|
||||
"page_path": "/docs/networking",
|
||||
"page_title": "Networking Guide",
|
||||
"chunk_text": "Container networking...",
|
||||
"score": 0.92,
|
||||
"chunk_index": 0,
|
||||
}
|
||||
]
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.post.return_value = mock_response
|
||||
|
||||
results = await client_with_mock.semantic_search("container networking")
|
||||
|
||||
assert len(results) == 1
|
||||
assert isinstance(results[0], VectorSearchResult)
|
||||
assert results[0].score == 0.92
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestGraphOperations:
|
||||
"""Tests for knowledge graph operations."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_query_graph(self, client_with_mock, mock_httpx_client):
|
||||
"""Test executing a Cypher query."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"records": [
|
||||
{"name": "Docker", "type": "Technology"},
|
||||
{"name": "Kubernetes", "type": "Technology"},
|
||||
]
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.post.return_value = mock_response
|
||||
|
||||
records = await client_with_mock.query_graph(
|
||||
"MATCH (n:Technology) RETURN n.name as name, n.type as type"
|
||||
)
|
||||
|
||||
assert len(records) == 2
|
||||
assert records[0]["name"] == "Docker"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_graph_nodes(self, client_with_mock, mock_httpx_client):
|
||||
"""Test listing graph nodes."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"nodes": [
|
||||
{
|
||||
"id": "node1",
|
||||
"labels": ["Technology"],
|
||||
"properties": {"name": "Docker"},
|
||||
}
|
||||
]
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.get.return_value = mock_response
|
||||
|
||||
nodes = await client_with_mock.list_graph_nodes()
|
||||
|
||||
assert len(nodes) == 1
|
||||
assert isinstance(nodes[0], GraphNode)
|
||||
assert nodes[0].id == "node1"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestHealthCheck:
|
||||
"""Tests for health check."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_health_check_healthy(self, client_with_mock, mock_httpx_client):
|
||||
"""Test health check returns true when healthy."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_httpx_client.get.return_value = mock_response
|
||||
|
||||
result = await client_with_mock.health_check()
|
||||
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_health_check_unhealthy(self, client_with_mock, mock_httpx_client):
|
||||
"""Test health check returns false on error."""
|
||||
mock_httpx_client.get.side_effect = httpx.ConnectError("Connection refused")
|
||||
|
||||
result = await client_with_mock.health_check()
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestResponseModels:
|
||||
"""Tests for response model validation."""
|
||||
|
||||
def test_wiki_page_model(self):
|
||||
"""Test WikiPage model."""
|
||||
page = WikiPage(
|
||||
id=1,
|
||||
path="/test",
|
||||
title="Test Page",
|
||||
content="Content here",
|
||||
tags=["tag1"],
|
||||
)
|
||||
|
||||
assert page.id == 1
|
||||
assert page.title == "Test Page"
|
||||
|
||||
def test_wiki_page_optional_fields(self):
|
||||
"""Test WikiPage with minimal fields."""
|
||||
page = WikiPage(id=1, path="/test", title="Test")
|
||||
|
||||
assert page.content is None
|
||||
assert page.tags == []
|
||||
|
||||
def test_hybrid_search_result_model(self):
|
||||
"""Test HybridSearchResult model."""
|
||||
result = HybridSearchResult(
|
||||
source="vector",
|
||||
title="Title",
|
||||
content="Content",
|
||||
score=0.9,
|
||||
)
|
||||
|
||||
assert result.source == "vector"
|
||||
assert result.url is None
|
||||
assert result.metadata == {}
|
||||
|
||||
def test_vector_search_result_model(self):
|
||||
"""Test VectorSearchResult model."""
|
||||
result = VectorSearchResult(
|
||||
page_id=1,
|
||||
page_path="/doc",
|
||||
page_title="Doc",
|
||||
chunk_text="Text chunk",
|
||||
score=0.85,
|
||||
chunk_index=0,
|
||||
)
|
||||
|
||||
assert result.score == 0.85
|
||||
assert result.chunk_index == 0
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestUpdateWikiPage:
|
||||
"""Tests for update_wiki_page method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_wiki_page_content(self, client_with_mock, mock_httpx_client):
|
||||
"""Test updating wiki page content."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"id": 42,
|
||||
"path": "/docs/test",
|
||||
"title": "Test Page",
|
||||
"content": "# Updated\n\nNew content",
|
||||
"tags": ["test"],
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.put.return_value = mock_response
|
||||
|
||||
page = await client_with_mock.update_wiki_page(
|
||||
page_id=42,
|
||||
content="# Updated\n\nNew content",
|
||||
)
|
||||
|
||||
assert isinstance(page, WikiPage)
|
||||
assert page.id == 42
|
||||
assert "Updated" in page.content
|
||||
mock_httpx_client.put.assert_called_once()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_wiki_page_tags_only(self, client_with_mock, mock_httpx_client):
|
||||
"""Test updating only tags (partial update)."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"id": 42,
|
||||
"path": "/docs/test",
|
||||
"title": "Test Page",
|
||||
"tags": ["projects", "devops"],
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.put.return_value = mock_response
|
||||
|
||||
page = await client_with_mock.update_wiki_page(
|
||||
page_id=42,
|
||||
tags=["projects", "devops"],
|
||||
)
|
||||
|
||||
assert page.tags == ["projects", "devops"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_wiki_page_multiple_fields(
|
||||
self, client_with_mock, mock_httpx_client
|
||||
):
|
||||
"""Test updating multiple fields at once."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"id": 42,
|
||||
"path": "/docs/test",
|
||||
"title": "New Title",
|
||||
"description": "New description",
|
||||
"tags": ["updated"],
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.put.return_value = mock_response
|
||||
|
||||
page = await client_with_mock.update_wiki_page(
|
||||
page_id=42,
|
||||
title="New Title",
|
||||
description="New description",
|
||||
tags=["updated"],
|
||||
)
|
||||
|
||||
assert page.title == "New Title"
|
||||
assert page.description == "New description"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestSmartCreateWikiPage:
|
||||
"""Tests for smart_create_wiki_page method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_smart_create_basic(self, client_with_mock, mock_httpx_client):
|
||||
"""Test basic smart create."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"page": {
|
||||
"id": 123,
|
||||
"path": "/users/test/technology/docker-compose",
|
||||
"title": "Docker Compose",
|
||||
"content": "# Docker Compose\n\nContent...",
|
||||
"tags": ["technology", "devops"],
|
||||
},
|
||||
"research_summary": {
|
||||
"wiki_results": 3,
|
||||
"web_results": 8,
|
||||
"graph_entities": 5,
|
||||
"keywords_extracted": 12,
|
||||
"timing_ms": 4500,
|
||||
},
|
||||
"sources_used": 11,
|
||||
"search_id": "uuid-123",
|
||||
"entity_linking": {
|
||||
"forward_links": 5,
|
||||
"backward_links": 3,
|
||||
"pages_updated": 2,
|
||||
},
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.post.return_value = mock_response
|
||||
|
||||
result = await client_with_mock.smart_create_wiki_page(
|
||||
topic="Docker Compose",
|
||||
tags=["technology", "devops"],
|
||||
)
|
||||
|
||||
assert isinstance(result, SmartCreateResponse)
|
||||
assert result.page.id == 123
|
||||
assert result.page.title == "Docker Compose"
|
||||
assert result.sources_used == 11
|
||||
assert result.research_summary.wiki_results == 3
|
||||
assert result.research_summary.web_results == 8
|
||||
assert result.entity_linking.forward_links == 5
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_smart_create_with_options(self, client_with_mock, mock_httpx_client):
|
||||
"""Test smart create with custom options."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"page": {
|
||||
"id": 456,
|
||||
"path": "/custom/path",
|
||||
"title": "Custom Topic",
|
||||
"tags": ["custom"],
|
||||
},
|
||||
"research_summary": {
|
||||
"wiki_results": 5,
|
||||
"web_results": 0, # Web disabled
|
||||
"timing_ms": 2000,
|
||||
},
|
||||
"sources_used": 5,
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
mock_httpx_client.post.return_value = mock_response
|
||||
|
||||
result = await client_with_mock.smart_create_wiki_page(
|
||||
topic="Custom Topic",
|
||||
tags=["custom"],
|
||||
path="/custom/path",
|
||||
include_web_research=False,
|
||||
)
|
||||
|
||||
assert result.page.path == "/custom/path"
|
||||
assert result.research_summary.web_results == 0
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestNewResponseModels:
|
||||
"""Tests for new response models."""
|
||||
|
||||
def test_research_summary_model(self):
|
||||
"""Test ResearchSummary model."""
|
||||
summary = ResearchSummary(
|
||||
wiki_results=3,
|
||||
web_results=5,
|
||||
graph_entities=2,
|
||||
keywords_extracted=10,
|
||||
timing_ms=3000,
|
||||
)
|
||||
|
||||
assert summary.wiki_results == 3
|
||||
assert summary.timing_ms == 3000
|
||||
|
||||
def test_research_summary_defaults(self):
|
||||
"""Test ResearchSummary default values."""
|
||||
summary = ResearchSummary()
|
||||
|
||||
assert summary.wiki_results == 0
|
||||
assert summary.timing_ms == 0
|
||||
|
||||
def test_entity_linking_model(self):
|
||||
"""Test EntityLinking model."""
|
||||
linking = EntityLinking(
|
||||
forward_links=5,
|
||||
backward_links=3,
|
||||
pages_updated=2,
|
||||
)
|
||||
|
||||
assert linking.forward_links == 5
|
||||
assert linking.pages_updated == 2
|
||||
|
||||
def test_smart_create_response_model(self):
|
||||
"""Test SmartCreateResponse model."""
|
||||
page = WikiPage(id=1, path="/test", title="Test")
|
||||
response = SmartCreateResponse(
|
||||
page=page,
|
||||
sources_used=10,
|
||||
search_id="uuid-456",
|
||||
)
|
||||
|
||||
assert response.page.id == 1
|
||||
assert response.sources_used == 10
|
||||
assert response.search_id == "uuid-456"
|
||||
@@ -0,0 +1,339 @@
|
||||
"""
|
||||
Tests for multi-agent coordination engine.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from src.agents.coordination import (
|
||||
CoordinationEngine,
|
||||
get_coordination_engine,
|
||||
delegate_to_librarian,
|
||||
)
|
||||
from src.agents.protocol import (
|
||||
AgentResponse,
|
||||
AgentUnavailableError,
|
||||
DelegationIntent,
|
||||
DelegationReason,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def coordination_engine():
|
||||
"""Create a fresh coordination engine for testing."""
|
||||
return CoordinationEngine()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_registry():
|
||||
"""Mock the household registry."""
|
||||
with patch("src.agents.coordination.get_household_registry") as mock:
|
||||
registry = MagicMock()
|
||||
mock.return_value = registry
|
||||
yield registry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def librarian_intent():
|
||||
"""Create a standard librarian delegation intent."""
|
||||
return DelegationIntent(
|
||||
target_agent="librarian",
|
||||
task="Find information about Docker networking",
|
||||
reason=DelegationReason.DOMAIN_EXPERTISE,
|
||||
expected_outcome="Documentation and examples",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestCoordinationEngine:
|
||||
"""Tests for CoordinationEngine class."""
|
||||
|
||||
def test_initialization(self, coordination_engine):
|
||||
"""Test engine initializes correctly."""
|
||||
assert coordination_engine is not None
|
||||
assert coordination_engine.registry is not None
|
||||
|
||||
def test_get_available_agents_empty(self, mock_registry):
|
||||
"""Test getting available agents when none have agents."""
|
||||
mock_registry.list_members.return_value = ["tatlock_core"]
|
||||
mock_member = MagicMock()
|
||||
mock_member.agent = None # No agent
|
||||
mock_registry.get_member.return_value = mock_member
|
||||
|
||||
engine = CoordinationEngine()
|
||||
available = engine.get_available_agents()
|
||||
|
||||
assert available == []
|
||||
|
||||
def test_get_available_agents_with_librarian(self, mock_registry):
|
||||
"""Test getting available agents with librarian registered."""
|
||||
mock_registry.list_members.return_value = ["tatlock_core", "librarian"]
|
||||
|
||||
# tatlock_core has no agent
|
||||
core_member = MagicMock()
|
||||
core_member.agent = None
|
||||
|
||||
# librarian has an agent
|
||||
librarian_member = MagicMock()
|
||||
librarian_member.agent = MagicMock()
|
||||
|
||||
def get_member_side_effect(name):
|
||||
if name == "tatlock_core":
|
||||
return core_member
|
||||
elif name == "librarian":
|
||||
return librarian_member
|
||||
return None
|
||||
|
||||
mock_registry.get_member.side_effect = get_member_side_effect
|
||||
|
||||
engine = CoordinationEngine()
|
||||
available = engine.get_available_agents()
|
||||
|
||||
assert "librarian" in available
|
||||
assert "tatlock_core" not in available
|
||||
|
||||
def test_can_delegate_to_unknown_agent(self, mock_registry):
|
||||
"""Test checking delegation to unknown agent."""
|
||||
mock_registry.get_member.return_value = None
|
||||
|
||||
engine = CoordinationEngine()
|
||||
|
||||
assert engine.can_delegate_to("unknown_agent") is False
|
||||
|
||||
def test_can_delegate_to_librarian(self, mock_registry):
|
||||
"""Test checking delegation to librarian."""
|
||||
mock_member = MagicMock()
|
||||
mock_member.agent = MagicMock() # Has an agent
|
||||
mock_registry.get_member.return_value = mock_member
|
||||
|
||||
engine = CoordinationEngine()
|
||||
|
||||
assert engine.can_delegate_to("librarian") is True
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestDelegationExecution:
|
||||
"""Tests for delegation execution."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_delegation_unavailable_agent(
|
||||
self, mock_registry, librarian_intent
|
||||
):
|
||||
"""Test delegation fails for unavailable agent."""
|
||||
mock_registry.get_member.return_value = None
|
||||
|
||||
engine = CoordinationEngine()
|
||||
|
||||
with pytest.raises(AgentUnavailableError) as exc_info:
|
||||
await engine.execute_delegation(librarian_intent)
|
||||
|
||||
assert "librarian" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_delegation_success(
|
||||
self, mock_registry, librarian_intent
|
||||
):
|
||||
"""Test successful delegation execution."""
|
||||
# Setup mock member with agent
|
||||
mock_member = MagicMock()
|
||||
mock_member.agent = MagicMock()
|
||||
mock_registry.get_member.return_value = mock_member
|
||||
|
||||
# Mock the executor
|
||||
with patch(
|
||||
"src.agents.coordination.AGENT_EXECUTORS",
|
||||
{"librarian": AsyncMock(return_value="Research results here")},
|
||||
):
|
||||
engine = CoordinationEngine()
|
||||
response = await engine.execute_delegation(librarian_intent)
|
||||
|
||||
assert response.success is True
|
||||
assert response.result == "Research results here"
|
||||
# Duration might be 0 for very fast mock execution
|
||||
assert response.duration_ms >= 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_delegation_error(
|
||||
self, mock_registry, librarian_intent
|
||||
):
|
||||
"""Test delegation handles executor errors."""
|
||||
mock_member = MagicMock()
|
||||
mock_member.agent = MagicMock()
|
||||
mock_registry.get_member.return_value = mock_member
|
||||
|
||||
# Mock executor that raises
|
||||
async def failing_executor(**kwargs):
|
||||
raise ValueError("API connection failed")
|
||||
|
||||
with patch(
|
||||
"src.agents.coordination.AGENT_EXECUTORS",
|
||||
{"librarian": failing_executor},
|
||||
):
|
||||
engine = CoordinationEngine()
|
||||
response = await engine.execute_delegation(librarian_intent)
|
||||
|
||||
assert response.success is False
|
||||
assert "API connection failed" in response.error_message
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestCoordinate:
|
||||
"""Tests for multi-agent coordination."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_coordinate_single_intent(self, mock_registry, librarian_intent):
|
||||
"""Test coordinating a single delegation."""
|
||||
mock_member = MagicMock()
|
||||
mock_member.agent = MagicMock()
|
||||
mock_registry.get_member.return_value = mock_member
|
||||
|
||||
with patch(
|
||||
"src.agents.coordination.AGENT_EXECUTORS",
|
||||
{"librarian": AsyncMock(return_value="Found docs")},
|
||||
):
|
||||
engine = CoordinationEngine()
|
||||
result = await engine.coordinate([librarian_intent])
|
||||
|
||||
assert result.final_response == "Found docs"
|
||||
assert "librarian" in result.agents_consulted
|
||||
# Duration might be 0 for very fast mock execution
|
||||
assert result.total_duration_ms >= 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_coordinate_empty_intents(self, mock_registry):
|
||||
"""Test coordinating with no intents."""
|
||||
engine = CoordinationEngine()
|
||||
result = await engine.coordinate([])
|
||||
|
||||
assert result.final_response == ""
|
||||
assert result.agents_consulted == []
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_coordinate_multiple_intents(self, mock_registry):
|
||||
"""Test coordinating multiple delegations."""
|
||||
mock_member = MagicMock()
|
||||
mock_member.agent = MagicMock()
|
||||
mock_registry.get_member.return_value = mock_member
|
||||
|
||||
intents = [
|
||||
DelegationIntent(
|
||||
target_agent="librarian",
|
||||
task="Task 1",
|
||||
reason=DelegationReason.DOMAIN_EXPERTISE,
|
||||
expected_outcome="Result 1",
|
||||
priority=1,
|
||||
),
|
||||
DelegationIntent(
|
||||
target_agent="librarian",
|
||||
task="Task 2",
|
||||
reason=DelegationReason.DOMAIN_EXPERTISE,
|
||||
expected_outcome="Result 2",
|
||||
priority=2,
|
||||
),
|
||||
]
|
||||
|
||||
call_count = 0
|
||||
|
||||
async def mock_executor(**kwargs):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
return f"Result {call_count}"
|
||||
|
||||
with patch(
|
||||
"src.agents.coordination.AGENT_EXECUTORS",
|
||||
{"librarian": mock_executor},
|
||||
):
|
||||
engine = CoordinationEngine()
|
||||
result = await engine.coordinate(intents)
|
||||
|
||||
# Both intents were executed (check agents_consulted count)
|
||||
assert len(result.agents_consulted) == 2
|
||||
# Current implementation replaces same-agent responses in dict
|
||||
# So final_response has the last result (or combined if different agents)
|
||||
assert len(result.final_response) > 0
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestDelegateToLibrarian:
|
||||
"""Tests for convenience delegation function."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delegate_to_librarian(self, mock_registry):
|
||||
"""Test the delegate_to_librarian helper."""
|
||||
mock_member = MagicMock()
|
||||
mock_member.agent = MagicMock()
|
||||
mock_registry.get_member.return_value = mock_member
|
||||
|
||||
with patch(
|
||||
"src.agents.coordination.AGENT_EXECUTORS",
|
||||
{"librarian": AsyncMock(return_value="Wiki search results")},
|
||||
):
|
||||
# Reset global engine
|
||||
with patch(
|
||||
"src.agents.coordination._coordination_engine",
|
||||
None,
|
||||
):
|
||||
response = await delegate_to_librarian(
|
||||
task="Search for Docker docs",
|
||||
context="Setting up homelab",
|
||||
)
|
||||
|
||||
assert response.success is True
|
||||
assert response.result == "Wiki search results"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestGetCoordinationEngine:
|
||||
"""Tests for engine singleton."""
|
||||
|
||||
def test_get_coordination_engine_singleton(self):
|
||||
"""Test engine is singleton."""
|
||||
with patch("src.agents.coordination._coordination_engine", None):
|
||||
engine1 = get_coordination_engine()
|
||||
engine2 = get_coordination_engine()
|
||||
|
||||
# Should be same instance
|
||||
assert engine1 is engine2
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestDelegationStreaming:
|
||||
"""Tests for streaming delegation."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_delegation_stream_unavailable(
|
||||
self, mock_registry, librarian_intent
|
||||
):
|
||||
"""Test streaming fails for unavailable agent."""
|
||||
engine = CoordinationEngine()
|
||||
|
||||
# Change target to an agent that doesn't have a stream executor
|
||||
librarian_intent.target_agent = "nonexistent_agent"
|
||||
|
||||
with pytest.raises(AgentUnavailableError):
|
||||
async for _ in engine.execute_delegation_stream(librarian_intent):
|
||||
pass
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_delegation_stream_success(
|
||||
self, mock_registry, librarian_intent
|
||||
):
|
||||
"""Test successful streaming delegation."""
|
||||
mock_member = MagicMock()
|
||||
mock_member.agent = MagicMock()
|
||||
mock_registry.get_member.return_value = mock_member
|
||||
|
||||
async def mock_stream(**kwargs):
|
||||
yield "Hello "
|
||||
yield "world"
|
||||
|
||||
with patch(
|
||||
"src.agents.coordination.AGENT_STREAM_EXECUTORS",
|
||||
{"librarian": mock_stream},
|
||||
):
|
||||
engine = CoordinationEngine()
|
||||
chunks = []
|
||||
async for chunk in engine.execute_delegation_stream(librarian_intent):
|
||||
chunks.append(chunk)
|
||||
|
||||
assert chunks == ["Hello ", "world"]
|
||||
@@ -0,0 +1,195 @@
|
||||
"""
|
||||
Tests for delegation infrastructure.
|
||||
|
||||
Tests the DelegationTask dataclass and delegation wrapper functions
|
||||
that implement the agent-as-tool pattern.
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, patch, MagicMock
|
||||
|
||||
from src.agents.delegation import (
|
||||
DelegationTask,
|
||||
DelegationResult,
|
||||
delegate_to_librarian,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestDelegationTask:
|
||||
"""Tests for the DelegationTask dataclass."""
|
||||
|
||||
def test_delegation_task_creation(self):
|
||||
"""Test basic DelegationTask creation."""
|
||||
task = DelegationTask(
|
||||
expert_name="librarian",
|
||||
task="Create a wiki page about CI/CD",
|
||||
context="User is setting up a homelab",
|
||||
action="create",
|
||||
)
|
||||
|
||||
assert task.expert_name == "librarian"
|
||||
assert task.task == "Create a wiki page about CI/CD"
|
||||
assert task.context == "User is setting up a homelab"
|
||||
assert task.action == "create"
|
||||
|
||||
def test_delegation_task_default_values(self):
|
||||
"""Test DelegationTask default values."""
|
||||
task = DelegationTask(
|
||||
expert_name="librarian",
|
||||
task="Search for Docker info",
|
||||
)
|
||||
|
||||
assert task.context == ""
|
||||
assert task.action == ""
|
||||
assert task.priority == 0
|
||||
assert task.depends_on == []
|
||||
assert task.result is None
|
||||
|
||||
def test_delegation_task_auto_generates_id(self):
|
||||
"""Test DelegationTask auto-generates unique IDs."""
|
||||
task1 = DelegationTask(expert_name="librarian", task="Task 1")
|
||||
task2 = DelegationTask(expert_name="librarian", task="Task 2")
|
||||
|
||||
assert task1.task_id.startswith("librarian_")
|
||||
assert task2.task_id.startswith("librarian_")
|
||||
assert task1.task_id != task2.task_id
|
||||
|
||||
def test_delegation_task_preserves_custom_id(self):
|
||||
"""Test DelegationTask preserves custom ID if provided."""
|
||||
task = DelegationTask(
|
||||
expert_name="librarian",
|
||||
task="Custom task",
|
||||
task_id="custom_id_123",
|
||||
)
|
||||
|
||||
assert task.task_id == "custom_id_123"
|
||||
|
||||
def test_delegation_task_with_dependencies(self):
|
||||
"""Test DelegationTask with dependencies."""
|
||||
task = DelegationTask(
|
||||
expert_name="librarian",
|
||||
task="Update wiki page",
|
||||
depends_on=["memory_abc123", "search_def456"],
|
||||
)
|
||||
|
||||
assert len(task.depends_on) == 2
|
||||
assert "memory_abc123" in task.depends_on
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestDelegationResult:
|
||||
"""Tests for the DelegationResult dataclass."""
|
||||
|
||||
def test_delegation_result_success(self):
|
||||
"""Test successful DelegationResult."""
|
||||
result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="Search for Docker info",
|
||||
success=True,
|
||||
output="Found 5 relevant documents about Docker...",
|
||||
)
|
||||
|
||||
assert result.expert_name == "librarian"
|
||||
assert result.success is True
|
||||
assert result.output.startswith("Found")
|
||||
assert result.error is None
|
||||
|
||||
def test_delegation_result_failure(self):
|
||||
"""Test failed DelegationResult."""
|
||||
result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="Search for Docker info",
|
||||
success=False,
|
||||
output="",
|
||||
error="Connection timeout to library-desk API",
|
||||
)
|
||||
|
||||
assert result.success is False
|
||||
assert result.output == ""
|
||||
assert result.error == "Connection timeout to library-desk API"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestDelegateToLibrarian:
|
||||
"""Tests for the delegate_to_librarian wrapper."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delegate_to_librarian_success(self):
|
||||
"""Test successful delegation to Librarian."""
|
||||
mock_output = "Successfully created wiki page about CI/CD pipelines..."
|
||||
|
||||
with patch(
|
||||
"src.agents.librarian.agent.run_librarian",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_output,
|
||||
) as mock_run:
|
||||
result = await delegate_to_librarian(
|
||||
task="Create a wiki page about CI/CD pipelines",
|
||||
context="User is setting up a homelab",
|
||||
)
|
||||
|
||||
# Verify run_librarian was called correctly
|
||||
mock_run.assert_called_once_with(
|
||||
task="Create a wiki page about CI/CD pipelines",
|
||||
context="User is setting up a homelab",
|
||||
)
|
||||
|
||||
# Verify result
|
||||
assert isinstance(result, DelegationResult)
|
||||
assert result.expert_name == "librarian"
|
||||
assert result.success is True
|
||||
assert result.output == mock_output
|
||||
assert result.error is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delegate_to_librarian_without_context(self):
|
||||
"""Test delegation to Librarian without context."""
|
||||
mock_output = "Found information about Docker networking..."
|
||||
|
||||
with patch(
|
||||
"src.agents.librarian.agent.run_librarian",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_output,
|
||||
) as mock_run:
|
||||
result = await delegate_to_librarian(
|
||||
task="Search for information about Docker networking",
|
||||
)
|
||||
|
||||
mock_run.assert_called_once_with(
|
||||
task="Search for information about Docker networking",
|
||||
context="",
|
||||
)
|
||||
|
||||
assert result.success is True
|
||||
assert result.output == mock_output
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delegate_to_librarian_handles_error(self):
|
||||
"""Test delegation handles Librarian errors gracefully."""
|
||||
with patch(
|
||||
"src.agents.librarian.agent.run_librarian",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=Exception("Connection refused"),
|
||||
):
|
||||
result = await delegate_to_librarian(
|
||||
task="Search for information",
|
||||
)
|
||||
|
||||
assert isinstance(result, DelegationResult)
|
||||
assert result.success is False
|
||||
assert result.output == ""
|
||||
assert result.error == "Connection refused"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delegate_to_librarian_preserves_task(self):
|
||||
"""Test delegation result preserves original task."""
|
||||
original_task = "Create a wiki page about Kubernetes deployments"
|
||||
|
||||
with patch(
|
||||
"src.agents.librarian.agent.run_librarian",
|
||||
new_callable=AsyncMock,
|
||||
return_value="Page created",
|
||||
):
|
||||
result = await delegate_to_librarian(task=original_task)
|
||||
|
||||
assert result.task == original_task
|
||||
@@ -0,0 +1,761 @@
|
||||
"""
|
||||
Tests for orchestration module.
|
||||
|
||||
Tests the multi-expert coordination infrastructure including
|
||||
delegation parsing, think updates, result handling, and
|
||||
multi-expert sequential/parallel execution.
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from src.agents.orchestration import (
|
||||
OrchestrationContext,
|
||||
parse_delegation_from_steward_note,
|
||||
execute_delegation,
|
||||
orchestrate_with_think_updates,
|
||||
extract_delegation_context,
|
||||
ExecutionMode,
|
||||
MultiExpertResult,
|
||||
execute_sequential,
|
||||
execute_parallel,
|
||||
orchestrate_multi_expert,
|
||||
_get_display_name,
|
||||
)
|
||||
from src.agents.delegation import DelegationTask, DelegationResult
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestParseDelegation:
|
||||
"""Tests for parsing delegation from Steward's note."""
|
||||
|
||||
def test_parse_librarian_create(self):
|
||||
"""Test parsing librarian create delegation."""
|
||||
note = """DELEGATE: librarian to create a wiki page about CI/CD pipelines
|
||||
REASON: User wants to document CI/CD concepts
|
||||
COMPLEXITY: moderate
|
||||
CONTEXT: none"""
|
||||
|
||||
task = parse_delegation_from_steward_note(note)
|
||||
|
||||
assert task is not None
|
||||
assert task.expert_name == "librarian"
|
||||
assert "create a wiki page about CI/CD pipelines" in task.task
|
||||
|
||||
def test_parse_librarian_search(self):
|
||||
"""Test parsing librarian search delegation."""
|
||||
note = """DELEGATE: librarian to search for information about Docker networking
|
||||
REASON: User needs Docker documentation
|
||||
COMPLEXITY: simple"""
|
||||
|
||||
task = parse_delegation_from_steward_note(note)
|
||||
|
||||
assert task is not None
|
||||
assert task.expert_name == "librarian"
|
||||
assert "search for information about Docker networking" in task.task
|
||||
|
||||
def test_parse_no_delegation(self):
|
||||
"""Test parsing when no delegation needed."""
|
||||
note = """DELEGATE: none (conversational response only)
|
||||
REASON: Simple greeting requires no tools
|
||||
COMPLEXITY: simple"""
|
||||
|
||||
task = parse_delegation_from_steward_note(note)
|
||||
|
||||
assert task is None
|
||||
|
||||
def test_parse_tatlock_core(self):
|
||||
"""Test parsing tatlock_core delegation."""
|
||||
note = """DELEGATE: tatlock_core to calculate the result
|
||||
REASON: Math calculation needed
|
||||
COMPLEXITY: simple"""
|
||||
|
||||
task = parse_delegation_from_steward_note(note)
|
||||
|
||||
assert task is not None
|
||||
assert task.expert_name == "tatlock_core"
|
||||
assert "calculate the result" in task.task
|
||||
|
||||
def test_parse_case_insensitive(self):
|
||||
"""Test parsing is case insensitive."""
|
||||
note = """delegate: LIBRARIAN to search docs
|
||||
reason: Research query"""
|
||||
|
||||
task = parse_delegation_from_steward_note(note)
|
||||
|
||||
assert task is not None
|
||||
assert task.expert_name == "librarian"
|
||||
|
||||
def test_parse_missing_delegate(self):
|
||||
"""Test parsing when DELEGATE line is missing."""
|
||||
note = """REASON: This has no delegation
|
||||
COMPLEXITY: simple"""
|
||||
|
||||
task = parse_delegation_from_steward_note(note)
|
||||
|
||||
assert task is None
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestExtractDelegationContext:
|
||||
"""Tests for extracting context from Steward's note."""
|
||||
|
||||
def test_extract_all_fields(self):
|
||||
"""Test extracting all context fields."""
|
||||
note = """DELEGATE: librarian to create wiki page
|
||||
REASON: User wants documentation
|
||||
COMPLEXITY: moderate
|
||||
CONTEXT: Related to previous discussion about DevOps"""
|
||||
|
||||
context = extract_delegation_context(note)
|
||||
|
||||
assert context["reason"] == "User wants documentation"
|
||||
assert context["complexity"] == "moderate"
|
||||
assert "Related to previous discussion" in context["context"]
|
||||
|
||||
def test_extract_partial_fields(self):
|
||||
"""Test extracting when some fields missing."""
|
||||
note = """DELEGATE: librarian to search
|
||||
REASON: Research query
|
||||
COMPLEXITY: simple"""
|
||||
|
||||
context = extract_delegation_context(note)
|
||||
|
||||
assert context["reason"] == "Research query"
|
||||
assert context["complexity"] == "simple"
|
||||
assert context["context"] == ""
|
||||
|
||||
def test_extract_empty_note(self):
|
||||
"""Test extracting from empty note."""
|
||||
context = extract_delegation_context("")
|
||||
|
||||
assert context["reason"] == ""
|
||||
assert context["complexity"] == ""
|
||||
assert context["context"] == ""
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestExecuteDelegation:
|
||||
"""Tests for executing delegation tasks."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_librarian_delegation(self):
|
||||
"""Test executing delegation to librarian."""
|
||||
task = DelegationTask(
|
||||
expert_name="librarian",
|
||||
task="search for Docker docs",
|
||||
context="User learning Docker",
|
||||
)
|
||||
|
||||
mock_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="search for Docker docs",
|
||||
success=True,
|
||||
output="Found Docker documentation...",
|
||||
)
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.delegate_to_librarian",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_result,
|
||||
) as mock_delegate:
|
||||
result = await execute_delegation(task)
|
||||
|
||||
mock_delegate.assert_called_once_with(
|
||||
task="search for Docker docs",
|
||||
context="User learning Docker",
|
||||
)
|
||||
|
||||
assert result.success is True
|
||||
assert "Docker" in result.output
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_unknown_expert(self):
|
||||
"""Test executing delegation to unknown expert."""
|
||||
task = DelegationTask(
|
||||
expert_name="unknown_expert",
|
||||
task="do something",
|
||||
)
|
||||
|
||||
result = await execute_delegation(task)
|
||||
|
||||
assert result.success is False
|
||||
assert "Unknown expert" in result.error
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestOrchestrateWithThinkUpdates:
|
||||
"""Tests for orchestration with think updates."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_emits_think_before_delegation(self):
|
||||
"""Test that think update is emitted before delegation."""
|
||||
mock_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="search docs",
|
||||
success=True,
|
||||
output="Found results",
|
||||
)
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.delegate_to_librarian",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_result,
|
||||
):
|
||||
updates = []
|
||||
async for update in orchestrate_with_think_updates(
|
||||
user_message="Search for Docker info",
|
||||
steward_note="DELEGATE: librarian to search for Docker info",
|
||||
):
|
||||
updates.append(update)
|
||||
|
||||
# First update should be think tag about consulting
|
||||
assert any("<think>" in u and "Consulting" in u for u in updates)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_emits_think_after_delegation(self):
|
||||
"""Test that think update is emitted after delegation."""
|
||||
mock_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="search docs",
|
||||
success=True,
|
||||
output="Found results",
|
||||
)
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.delegate_to_librarian",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_result,
|
||||
):
|
||||
updates = []
|
||||
async for update in orchestrate_with_think_updates(
|
||||
user_message="Search for Docker info",
|
||||
steward_note="DELEGATE: librarian to search for Docker info",
|
||||
):
|
||||
updates.append(update)
|
||||
|
||||
# Should have think tag about completion
|
||||
assert any("<think>" in u and "completed" in u for u in updates)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_yields_expert_output(self):
|
||||
"""Test that expert output is yielded."""
|
||||
mock_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="search docs",
|
||||
success=True,
|
||||
output="Found Docker documentation with networking details",
|
||||
)
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.delegate_to_librarian",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_result,
|
||||
):
|
||||
updates = []
|
||||
async for update in orchestrate_with_think_updates(
|
||||
user_message="Search for Docker info",
|
||||
steward_note="DELEGATE: librarian to search for Docker info",
|
||||
):
|
||||
updates.append(update)
|
||||
|
||||
# Should include expert output
|
||||
all_output = "".join(updates)
|
||||
assert "Docker documentation" in all_output
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_handles_delegation_failure(self):
|
||||
"""Test that delegation failure emits warning think update."""
|
||||
mock_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="search docs",
|
||||
success=False,
|
||||
output="",
|
||||
error="Connection timeout",
|
||||
)
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.delegate_to_librarian",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_result,
|
||||
):
|
||||
updates = []
|
||||
async for update in orchestrate_with_think_updates(
|
||||
user_message="Search for info",
|
||||
steward_note="DELEGATE: librarian to search",
|
||||
):
|
||||
updates.append(update)
|
||||
|
||||
# Should have warning think update
|
||||
all_output = "".join(updates)
|
||||
assert "⚠️" in all_output or "issue" in all_output.lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_no_delegation_returns_empty(self):
|
||||
"""Test that no delegation yields nothing."""
|
||||
updates = []
|
||||
async for update in orchestrate_with_think_updates(
|
||||
user_message="Hello",
|
||||
steward_note="DELEGATE: none (conversational)",
|
||||
):
|
||||
updates.append(update)
|
||||
|
||||
assert len(updates) == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_with_preparsed_task(self):
|
||||
"""Test orchestration with pre-parsed delegation task."""
|
||||
task = DelegationTask(
|
||||
expert_name="librarian",
|
||||
task="create wiki page",
|
||||
)
|
||||
|
||||
mock_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="create wiki page",
|
||||
success=True,
|
||||
output="Wiki page created",
|
||||
)
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.delegate_to_librarian",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_result,
|
||||
):
|
||||
updates = []
|
||||
async for update in orchestrate_with_think_updates(
|
||||
user_message="Create wiki page",
|
||||
steward_note="", # Empty note since task is pre-parsed
|
||||
delegation_task=task,
|
||||
):
|
||||
updates.append(update)
|
||||
|
||||
assert len(updates) > 0
|
||||
all_output = "".join(updates)
|
||||
assert "Wiki page created" in all_output
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestOrchestrationContext:
|
||||
"""Tests for OrchestrationContext dataclass."""
|
||||
|
||||
def test_context_creation(self):
|
||||
"""Test creating orchestration context."""
|
||||
ctx = OrchestrationContext(
|
||||
user_message="Test message",
|
||||
steward_note="Test note",
|
||||
conversation_id="conv_123",
|
||||
)
|
||||
|
||||
assert ctx.user_message == "Test message"
|
||||
assert ctx.steward_note == "Test note"
|
||||
assert ctx.conversation_id == "conv_123"
|
||||
|
||||
def test_context_defaults(self):
|
||||
"""Test orchestration context default values."""
|
||||
ctx = OrchestrationContext(
|
||||
user_message="Test",
|
||||
steward_note="Note",
|
||||
)
|
||||
|
||||
assert ctx.conversation_id is None
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Multi-Expert Coordination Tests
|
||||
# ============================================================================
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestMultiExpertResult:
|
||||
"""Tests for MultiExpertResult aggregation."""
|
||||
|
||||
def test_result_creation(self):
|
||||
"""Test creating empty MultiExpertResult."""
|
||||
result = MultiExpertResult()
|
||||
|
||||
assert result.results == {}
|
||||
assert result.all_succeeded is True
|
||||
assert result.failed_experts == []
|
||||
assert result.combined_output == ""
|
||||
|
||||
def test_add_successful_result(self):
|
||||
"""Test adding a successful result."""
|
||||
result = MultiExpertResult()
|
||||
|
||||
delegation_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="search docs",
|
||||
success=True,
|
||||
output="Found docs",
|
||||
)
|
||||
result.add_result(delegation_result)
|
||||
|
||||
assert "librarian" in result.results
|
||||
assert result.all_succeeded is True
|
||||
assert result.failed_experts == []
|
||||
|
||||
def test_add_failed_result(self):
|
||||
"""Test adding a failed result."""
|
||||
result = MultiExpertResult()
|
||||
|
||||
delegation_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="search docs",
|
||||
success=False,
|
||||
output="",
|
||||
error="Connection error",
|
||||
)
|
||||
result.add_result(delegation_result)
|
||||
|
||||
assert "librarian" in result.results
|
||||
assert result.all_succeeded is False
|
||||
assert "librarian" in result.failed_experts
|
||||
|
||||
def test_aggregate_outputs(self):
|
||||
"""Test aggregating outputs from multiple experts."""
|
||||
result = MultiExpertResult()
|
||||
|
||||
result.add_result(DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="search docs",
|
||||
success=True,
|
||||
output="Found Docker docs",
|
||||
))
|
||||
result.add_result(DelegationResult(
|
||||
expert_name="memory",
|
||||
task="get preferences",
|
||||
success=True,
|
||||
output="User prefers dark mode",
|
||||
))
|
||||
|
||||
combined = result.aggregate_outputs()
|
||||
|
||||
assert "Librarian" in combined
|
||||
assert "Found Docker docs" in combined
|
||||
assert "Memory" in combined
|
||||
assert "dark mode" in combined
|
||||
|
||||
def test_aggregate_excludes_failed(self):
|
||||
"""Test that failed results are excluded from aggregate."""
|
||||
result = MultiExpertResult()
|
||||
|
||||
result.add_result(DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="search",
|
||||
success=True,
|
||||
output="Success output",
|
||||
))
|
||||
result.add_result(DelegationResult(
|
||||
expert_name="memory",
|
||||
task="get",
|
||||
success=False,
|
||||
output="",
|
||||
error="Failed",
|
||||
))
|
||||
|
||||
combined = result.aggregate_outputs()
|
||||
|
||||
assert "Success output" in combined
|
||||
assert "Failed" not in combined
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestExecuteSequential:
|
||||
"""Tests for sequential multi-expert execution."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sequential_all_succeed(self):
|
||||
"""Test sequential execution when all tasks succeed."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
DelegationTask(expert_name="memory", task="task 2"),
|
||||
]
|
||||
|
||||
mock_results = [
|
||||
DelegationResult(expert_name="librarian", task="task 1", success=True, output="Result 1"),
|
||||
DelegationResult(expert_name="memory", task="task 2", success=True, output="Result 2"),
|
||||
]
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=mock_results,
|
||||
):
|
||||
result = await execute_sequential(tasks)
|
||||
|
||||
assert result.all_succeeded is True
|
||||
assert len(result.results) == 2
|
||||
assert result.failed_experts == []
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sequential_with_failure(self):
|
||||
"""Test sequential execution when a task fails."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
DelegationTask(expert_name="memory", task="task 2"),
|
||||
]
|
||||
|
||||
mock_results = [
|
||||
DelegationResult(expert_name="librarian", task="task 1", success=True, output="OK"),
|
||||
DelegationResult(expert_name="memory", task="task 2", success=False, output="", error="Failed"),
|
||||
]
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=mock_results,
|
||||
):
|
||||
result = await execute_sequential(tasks)
|
||||
|
||||
assert result.all_succeeded is False
|
||||
assert len(result.results) == 2
|
||||
assert "memory" in result.failed_experts
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sequential_stop_on_failure(self):
|
||||
"""Test sequential execution stops on failure when configured."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
DelegationTask(expert_name="memory", task="task 2"),
|
||||
DelegationTask(expert_name="librarian", task="task 3"),
|
||||
]
|
||||
|
||||
mock_results = [
|
||||
DelegationResult(expert_name="librarian", task="task 1", success=False, output="", error="Error"),
|
||||
]
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=mock_results,
|
||||
):
|
||||
result = await execute_sequential(tasks, stop_on_failure=True)
|
||||
|
||||
# Should only have 1 result (stopped after first failure)
|
||||
assert len(result.results) == 1
|
||||
assert result.all_succeeded is False
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestExecuteParallel:
|
||||
"""Tests for parallel multi-expert execution."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parallel_all_succeed(self):
|
||||
"""Test parallel execution when all tasks succeed."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
DelegationTask(expert_name="memory", task="task 2"),
|
||||
]
|
||||
|
||||
mock_results = [
|
||||
DelegationResult(expert_name="librarian", task="task 1", success=True, output="Result 1"),
|
||||
DelegationResult(expert_name="memory", task="task 2", success=True, output="Result 2"),
|
||||
]
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=mock_results,
|
||||
):
|
||||
result = await execute_parallel(tasks)
|
||||
|
||||
assert result.all_succeeded is True
|
||||
assert len(result.results) == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parallel_with_failure(self):
|
||||
"""Test parallel execution with partial failure."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
DelegationTask(expert_name="memory", task="task 2"),
|
||||
]
|
||||
|
||||
mock_results = [
|
||||
DelegationResult(expert_name="librarian", task="task 1", success=True, output="OK"),
|
||||
DelegationResult(expert_name="memory", task="task 2", success=False, output="", error="Timeout"),
|
||||
]
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=mock_results,
|
||||
):
|
||||
result = await execute_parallel(tasks)
|
||||
|
||||
assert result.all_succeeded is False
|
||||
assert len(result.results) == 2
|
||||
assert "memory" in result.failed_experts
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parallel_handles_exception(self):
|
||||
"""Test parallel execution handles exceptions gracefully."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
DelegationTask(expert_name="memory", task="task 2"),
|
||||
]
|
||||
|
||||
async def mock_execute(task):
|
||||
if task.expert_name == "memory":
|
||||
raise RuntimeError("Connection lost")
|
||||
return DelegationResult(
|
||||
expert_name=task.expert_name,
|
||||
task=task.task,
|
||||
success=True,
|
||||
output="OK",
|
||||
)
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=mock_execute,
|
||||
):
|
||||
result = await execute_parallel(tasks)
|
||||
|
||||
assert result.all_succeeded is False
|
||||
assert "memory" in result.failed_experts
|
||||
assert "Connection lost" in result.results["memory"].error
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestOrchestrateMultiExpert:
|
||||
"""Tests for multi-expert orchestration with think updates."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_sequential_emits_think_updates(self):
|
||||
"""Test sequential orchestration emits think updates for each task."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
DelegationTask(expert_name="memory", task="task 2"),
|
||||
]
|
||||
|
||||
mock_results = [
|
||||
DelegationResult(expert_name="librarian", task="task 1", success=True, output="Result 1"),
|
||||
DelegationResult(expert_name="memory", task="task 2", success=True, output="Result 2"),
|
||||
]
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=mock_results,
|
||||
):
|
||||
updates = []
|
||||
async for update in orchestrate_multi_expert(tasks, mode=ExecutionMode.SEQUENTIAL):
|
||||
updates.append(update)
|
||||
|
||||
all_output = "".join(updates)
|
||||
|
||||
# Should have think updates for both experts
|
||||
assert "Consulting" in all_output
|
||||
assert "completed" in all_output
|
||||
assert "Librarian" in all_output
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_parallel_emits_think_updates(self):
|
||||
"""Test parallel orchestration emits appropriate think updates."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
DelegationTask(expert_name="memory", task="task 2"),
|
||||
]
|
||||
|
||||
mock_results = [
|
||||
DelegationResult(expert_name="librarian", task="task 1", success=True, output="Result 1"),
|
||||
DelegationResult(expert_name="memory", task="task 2", success=True, output="Result 2"),
|
||||
]
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=mock_results,
|
||||
):
|
||||
updates = []
|
||||
async for update in orchestrate_multi_expert(tasks, mode=ExecutionMode.PARALLEL):
|
||||
updates.append(update)
|
||||
|
||||
all_output = "".join(updates)
|
||||
|
||||
# Should mention parallel execution
|
||||
assert "parallel" in all_output
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_empty_tasks_yields_nothing(self):
|
||||
"""Test orchestration with empty tasks yields nothing."""
|
||||
updates = []
|
||||
async for update in orchestrate_multi_expert([]):
|
||||
updates.append(update)
|
||||
|
||||
assert len(updates) == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_success_summary(self):
|
||||
"""Test orchestration emits success summary when all succeed."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
]
|
||||
|
||||
mock_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="task 1",
|
||||
success=True,
|
||||
output="Done",
|
||||
)
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_result,
|
||||
):
|
||||
updates = []
|
||||
async for update in orchestrate_multi_expert(tasks):
|
||||
updates.append(update)
|
||||
|
||||
all_output = "".join(updates)
|
||||
|
||||
# Should have success message
|
||||
assert "🎉" in all_output or "successfully" in all_output.lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_orchestrate_failure_summary(self):
|
||||
"""Test orchestration emits failure summary when some fail."""
|
||||
tasks = [
|
||||
DelegationTask(expert_name="librarian", task="task 1"),
|
||||
]
|
||||
|
||||
mock_result = DelegationResult(
|
||||
expert_name="librarian",
|
||||
task="task 1",
|
||||
success=False,
|
||||
output="",
|
||||
error="Failed",
|
||||
)
|
||||
|
||||
with patch(
|
||||
"src.agents.orchestration.execute_delegation",
|
||||
new_callable=AsyncMock,
|
||||
return_value=mock_result,
|
||||
):
|
||||
updates = []
|
||||
async for update in orchestrate_multi_expert(tasks):
|
||||
updates.append(update)
|
||||
|
||||
all_output = "".join(updates)
|
||||
|
||||
# Should mention failure
|
||||
assert "⚠️" in all_output or "failed" in all_output.lower()
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestGetDisplayName:
|
||||
"""Tests for _get_display_name helper."""
|
||||
|
||||
def test_librarian_display_name(self):
|
||||
"""Test librarian gets 'The Librarian' display name."""
|
||||
assert _get_display_name("librarian") == "The Librarian"
|
||||
|
||||
def test_memory_display_name(self):
|
||||
"""Test memory gets 'Memory' display name."""
|
||||
assert _get_display_name("memory") == "Memory"
|
||||
|
||||
def test_unknown_expert_title_case(self):
|
||||
"""Test unknown expert gets title-cased name."""
|
||||
assert _get_display_name("some_expert") == "Some_Expert"
|
||||
assert _get_display_name("newagent") == "Newagent"
|
||||
@@ -0,0 +1,256 @@
|
||||
"""
|
||||
Tests for agent communication protocol.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from src.agents.protocol import (
|
||||
AgentError,
|
||||
AgentRequest,
|
||||
AgentResponse,
|
||||
AgentTimeoutError,
|
||||
AgentUnavailableError,
|
||||
CoordinationResult,
|
||||
DelegationIntent,
|
||||
DelegationReason,
|
||||
ToolCallRecord,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestAgentRequest:
|
||||
"""Tests for AgentRequest model."""
|
||||
|
||||
def test_basic_request(self):
|
||||
"""Test creating a basic agent request."""
|
||||
request = AgentRequest(task="Find information about Docker")
|
||||
|
||||
assert request.task == "Find information about Docker"
|
||||
assert request.context == ""
|
||||
assert request.timeout_seconds == 60
|
||||
|
||||
def test_request_with_context(self):
|
||||
"""Test request with additional context."""
|
||||
request = AgentRequest(
|
||||
task="Find Docker networking docs",
|
||||
context="User is setting up a homelab",
|
||||
delegation_reason=DelegationReason.DOMAIN_EXPERTISE,
|
||||
)
|
||||
|
||||
assert request.task == "Find Docker networking docs"
|
||||
assert request.context == "User is setting up a homelab"
|
||||
assert request.delegation_reason == DelegationReason.DOMAIN_EXPERTISE
|
||||
|
||||
def test_request_serialization(self):
|
||||
"""Test request can be serialized to dict."""
|
||||
request = AgentRequest(
|
||||
task="Research task",
|
||||
context="Some context",
|
||||
)
|
||||
|
||||
data = request.model_dump()
|
||||
|
||||
assert data["task"] == "Research task"
|
||||
assert data["context"] == "Some context"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestAgentResponse:
|
||||
"""Tests for AgentResponse model."""
|
||||
|
||||
def test_successful_response(self):
|
||||
"""Test creating a successful response."""
|
||||
response = AgentResponse(
|
||||
success=True,
|
||||
result="Here are the findings...",
|
||||
reasoning="Searched wiki and found relevant docs",
|
||||
duration_ms=1500,
|
||||
)
|
||||
|
||||
assert response.success is True
|
||||
assert response.result == "Here are the findings..."
|
||||
assert response.reasoning == "Searched wiki and found relevant docs"
|
||||
assert response.duration_ms == 1500
|
||||
assert response.error_message is None
|
||||
|
||||
def test_failed_response(self):
|
||||
"""Test creating a failed response."""
|
||||
response = AgentResponse(
|
||||
success=False,
|
||||
result="",
|
||||
error_message="Connection timeout",
|
||||
duration_ms=30000,
|
||||
)
|
||||
|
||||
assert response.success is False
|
||||
assert response.result == ""
|
||||
assert response.error_message == "Connection timeout"
|
||||
|
||||
def test_response_with_tool_calls(self):
|
||||
"""Test response tracking tool calls."""
|
||||
tool_call = ToolCallRecord(
|
||||
tool_name="hybrid_search",
|
||||
arguments={"query": "Docker networking"},
|
||||
result="Found 5 results",
|
||||
duration_ms=500,
|
||||
)
|
||||
|
||||
response = AgentResponse(
|
||||
success=True,
|
||||
result="Based on search...",
|
||||
tool_calls=[tool_call],
|
||||
)
|
||||
|
||||
assert len(response.tool_calls) == 1
|
||||
assert response.tool_calls[0].tool_name == "hybrid_search"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestDelegationIntent:
|
||||
"""Tests for DelegationIntent model."""
|
||||
|
||||
def test_basic_intent(self):
|
||||
"""Test creating a basic delegation intent."""
|
||||
intent = DelegationIntent(
|
||||
target_agent="librarian",
|
||||
task="Research Docker networking",
|
||||
reason=DelegationReason.DOMAIN_EXPERTISE,
|
||||
expected_outcome="Documentation and examples",
|
||||
)
|
||||
|
||||
assert intent.target_agent == "librarian"
|
||||
assert intent.task == "Research Docker networking"
|
||||
assert intent.reason == DelegationReason.DOMAIN_EXPERTISE
|
||||
assert intent.priority == 1 # Default
|
||||
|
||||
def test_intent_with_priority(self):
|
||||
"""Test intent with custom priority."""
|
||||
intent = DelegationIntent(
|
||||
target_agent="librarian",
|
||||
task="Urgent research",
|
||||
reason=DelegationReason.RESOURCE_EFFICIENCY,
|
||||
expected_outcome="Quick answer",
|
||||
priority=1,
|
||||
)
|
||||
|
||||
assert intent.priority == 1
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestDelegationReason:
|
||||
"""Tests for DelegationReason enum."""
|
||||
|
||||
def test_all_reasons_have_values(self):
|
||||
"""Test all delegation reasons are defined."""
|
||||
reasons = list(DelegationReason)
|
||||
|
||||
assert DelegationReason.DOMAIN_EXPERTISE in reasons
|
||||
assert DelegationReason.TOOL_ACCESS in reasons
|
||||
assert DelegationReason.RESOURCE_EFFICIENCY in reasons
|
||||
assert DelegationReason.USER_PREFERENCE in reasons
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestCoordinationResult:
|
||||
"""Tests for CoordinationResult model."""
|
||||
|
||||
def test_single_agent_result(self):
|
||||
"""Test coordination with single agent."""
|
||||
agent_response = AgentResponse(
|
||||
success=True,
|
||||
result="Research findings",
|
||||
duration_ms=1000,
|
||||
)
|
||||
|
||||
intent = DelegationIntent(
|
||||
target_agent="librarian",
|
||||
task="Research task",
|
||||
reason=DelegationReason.DOMAIN_EXPERTISE,
|
||||
expected_outcome="Findings",
|
||||
)
|
||||
|
||||
result = CoordinationResult(
|
||||
final_response="Research findings",
|
||||
agent_responses={"librarian": agent_response},
|
||||
delegation_intents=[intent],
|
||||
total_duration_ms=1200,
|
||||
agents_consulted=["librarian"],
|
||||
)
|
||||
|
||||
assert result.final_response == "Research findings"
|
||||
assert len(result.agent_responses) == 1
|
||||
assert result.agents_consulted == ["librarian"]
|
||||
|
||||
def test_empty_result(self):
|
||||
"""Test coordination with no delegations."""
|
||||
result = CoordinationResult(
|
||||
final_response="",
|
||||
agent_responses={},
|
||||
delegation_intents=[],
|
||||
total_duration_ms=0,
|
||||
agents_consulted=[],
|
||||
)
|
||||
|
||||
assert result.final_response == ""
|
||||
assert len(result.agents_consulted) == 0
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestAgentErrors:
|
||||
"""Tests for agent error types."""
|
||||
|
||||
def test_agent_error(self):
|
||||
"""Test base AgentError."""
|
||||
error = AgentError("Something went wrong")
|
||||
|
||||
assert "Something went wrong" in str(error)
|
||||
assert error.agent_name == "unknown"
|
||||
|
||||
def test_agent_timeout_error(self):
|
||||
"""Test AgentTimeoutError."""
|
||||
error = AgentTimeoutError(
|
||||
"Timed out after 60s",
|
||||
agent_name="librarian",
|
||||
)
|
||||
|
||||
assert "Timed out" in str(error)
|
||||
assert error.agent_name == "librarian"
|
||||
|
||||
def test_agent_unavailable_error(self):
|
||||
"""Test AgentUnavailableError."""
|
||||
error = AgentUnavailableError(
|
||||
"Agent not registered",
|
||||
agent_name="unknown_agent",
|
||||
)
|
||||
|
||||
assert "not registered" in str(error)
|
||||
assert error.agent_name == "unknown_agent"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestToolCallRecord:
|
||||
"""Tests for ToolCallRecord model."""
|
||||
|
||||
def test_tool_call_record(self):
|
||||
"""Test creating a tool call record."""
|
||||
record = ToolCallRecord(
|
||||
tool_name="semantic_search",
|
||||
arguments={"query": "networking concepts", "limit": 10},
|
||||
result="Found 10 relevant documents",
|
||||
duration_ms=250,
|
||||
)
|
||||
|
||||
assert record.tool_name == "semantic_search"
|
||||
assert record.arguments["query"] == "networking concepts"
|
||||
assert record.duration_ms == 250
|
||||
|
||||
def test_tool_call_with_empty_result(self):
|
||||
"""Test tool call with empty result."""
|
||||
record = ToolCallRecord(
|
||||
tool_name="query_graph",
|
||||
arguments={"cypher": "MATCH (n) RETURN n"},
|
||||
result="",
|
||||
duration_ms=100,
|
||||
)
|
||||
|
||||
assert record.result == ""
|
||||
@@ -168,9 +168,11 @@ async def test_tatlock_tool_call_logging_search(async_client: AsyncClient):
|
||||
@pytest.mark.asyncio
|
||||
async def test_tatlock_tool_call_logging_calculator(async_client: AsyncClient):
|
||||
"""
|
||||
Test that calculator tool calls are logged to reasoning output.
|
||||
Test that calculator requests are handled correctly.
|
||||
|
||||
Verifies that mathematical calculations show what expression was evaluated.
|
||||
Verifies that mathematical calculations produce correct results.
|
||||
Note: Tool call logging visibility depends on execution path
|
||||
(streaming vs run, scoped tools vs delegation).
|
||||
"""
|
||||
request_data = {
|
||||
"model": "Tatlock",
|
||||
@@ -190,18 +192,30 @@ async def test_tatlock_tool_call_logging_calculator(async_client: AsyncClient):
|
||||
data = response.json()
|
||||
full_response = data["choices"][0]["message"]["content"]
|
||||
|
||||
# Should have calculator emoji in the response
|
||||
assert "🧮" in full_response, \
|
||||
f"Response should show calculator was used. Got: {full_response}"
|
||||
# Should have reasoning in <think> tags (from Steward analysis)
|
||||
assert "<think>" in full_response, \
|
||||
f"Should have reasoning output in <think> tags. Got: {full_response}"
|
||||
|
||||
# Should show the calculation expression
|
||||
assert "sqrt(144)" in full_response or "144" in full_response, \
|
||||
f"Should show what was calculated. Got: {full_response}"
|
||||
# Should reference the calculation in some form
|
||||
has_calculation_reference = (
|
||||
"144" in full_response or
|
||||
"sqrt" in full_response.lower() or
|
||||
"square root" in full_response.lower()
|
||||
)
|
||||
assert has_calculation_reference, \
|
||||
f"Should reference the calculation. Got: {full_response}"
|
||||
|
||||
# Should have the correct answer (37)
|
||||
assert "37" in full_response, \
|
||||
f"Should contain the answer 37. Got: {full_response}"
|
||||
|
||||
# Tool emoji is optional - depends on whether tool was used directly
|
||||
# or computation was delegated to capability
|
||||
if "🧮" in full_response:
|
||||
print(f"\nCalculator tool was used directly")
|
||||
else:
|
||||
print(f"\nCalculation handled via tatlock_core capability")
|
||||
|
||||
print(f"\nCalculator response: {full_response}")
|
||||
|
||||
|
||||
|
||||
@@ -294,6 +294,101 @@ class TestHouseholdRegistry:
|
||||
assert research_caps[0].name == "research_tools"
|
||||
|
||||
|
||||
class TestGetDelegationTools:
|
||||
"""Test get_delegation_tools() method for agent-as-tool pattern."""
|
||||
|
||||
def test_delegation_tools_returns_wrapper_for_member_with_agent(self, registry, sample_tools):
|
||||
"""Test delegation tools returns wrapper when member has an agent."""
|
||||
from unittest.mock import Mock
|
||||
|
||||
cap = HouseholdCapability(
|
||||
name="librarian",
|
||||
role="The Librarian",
|
||||
category="research",
|
||||
description="Research and wiki management",
|
||||
domains=["research", "wiki"],
|
||||
cost="medium",
|
||||
requires_network=True,
|
||||
)
|
||||
|
||||
mock_agent = Mock()
|
||||
registry.register("librarian", cap, sample_tools, agent=mock_agent)
|
||||
|
||||
tools = registry.get_delegation_tools(["librarian"])
|
||||
|
||||
# Should return delegation wrapper, not raw tools
|
||||
assert len(tools) == 1
|
||||
# The wrapper should be the delegate_to_librarian function
|
||||
assert callable(tools[0])
|
||||
assert tools[0].__name__ == "delegate_to_librarian"
|
||||
|
||||
def test_delegation_tools_returns_raw_tools_for_member_without_agent(self, registry, sample_capability, sample_tools):
|
||||
"""Test delegation tools returns raw tools when member has no agent."""
|
||||
registry.register("test_tools", sample_capability, sample_tools)
|
||||
|
||||
tools = registry.get_delegation_tools(["test_tools"])
|
||||
|
||||
# Should return raw tools since no agent
|
||||
assert len(tools) == 2
|
||||
assert tools[0].name == "test_tool_1"
|
||||
assert tools[1].name == "test_tool_2"
|
||||
|
||||
def test_delegation_tools_mixed_members(self, registry, sample_tools):
|
||||
"""Test delegation tools handles mix of agent and non-agent members."""
|
||||
from unittest.mock import Mock
|
||||
|
||||
# Member with agent (librarian)
|
||||
librarian_cap = HouseholdCapability(
|
||||
name="librarian",
|
||||
role="The Librarian",
|
||||
category="research",
|
||||
description="Research and wiki",
|
||||
domains=["research"],
|
||||
cost="medium",
|
||||
requires_network=True,
|
||||
)
|
||||
mock_agent = Mock()
|
||||
registry.register("librarian", librarian_cap, sample_tools, agent=mock_agent)
|
||||
|
||||
# Member without agent (tatlock_core)
|
||||
core_cap = HouseholdCapability(
|
||||
name="tatlock_core",
|
||||
role="Butler's Core Tools",
|
||||
category="core",
|
||||
description="Basic tools",
|
||||
domains=["computation"],
|
||||
cost="low",
|
||||
requires_network=False,
|
||||
)
|
||||
registry.register("tatlock_core", core_cap, sample_tools)
|
||||
|
||||
# Request both
|
||||
tools = registry.get_delegation_tools(["librarian", "tatlock_core"])
|
||||
|
||||
# Should get 1 delegation wrapper + 2 raw tools = 3 total
|
||||
assert len(tools) == 3
|
||||
|
||||
# First should be delegation wrapper
|
||||
assert callable(tools[0])
|
||||
assert tools[0].__name__ == "delegate_to_librarian"
|
||||
|
||||
# Rest should be raw tools
|
||||
assert hasattr(tools[1], 'name')
|
||||
assert hasattr(tools[2], 'name')
|
||||
|
||||
def test_delegation_tools_nonexistent_member(self, registry):
|
||||
"""Test delegation tools handles non-existent member gracefully."""
|
||||
tools = registry.get_delegation_tools(["nonexistent"])
|
||||
|
||||
assert tools == []
|
||||
|
||||
def test_delegation_tools_empty_list(self, registry):
|
||||
"""Test delegation tools handles empty list."""
|
||||
tools = registry.get_delegation_tools([])
|
||||
|
||||
assert tools == []
|
||||
|
||||
|
||||
class TestGlobalRegistry:
|
||||
"""Test the global registry instance."""
|
||||
|
||||
|
||||
@@ -0,0 +1,279 @@
|
||||
"""
|
||||
Tests for the memory service (direct access layer).
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch, AsyncMock
|
||||
|
||||
from src.core.memory_service import (
|
||||
MemoryService,
|
||||
MemoryType,
|
||||
MemoryRecord,
|
||||
memory_service,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestMemoryType:
|
||||
"""Tests for MemoryType enum."""
|
||||
|
||||
def test_user_profile_type(self):
|
||||
"""Test user_profile type exists."""
|
||||
assert MemoryType.USER_PROFILE.value == "user_profile"
|
||||
|
||||
def test_preference_type(self):
|
||||
"""Test preference type exists."""
|
||||
assert MemoryType.PREFERENCE.value == "preference"
|
||||
|
||||
def test_learned_fact_type(self):
|
||||
"""Test learned_fact type exists."""
|
||||
assert MemoryType.LEARNED_FACT.value == "learned_fact"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestMemoryRecord:
|
||||
"""Tests for MemoryRecord model."""
|
||||
|
||||
def test_create_minimal_record(self):
|
||||
"""Test creating record with minimal fields."""
|
||||
record = MemoryRecord(
|
||||
id="test_1",
|
||||
type=MemoryType.USER_PROFILE,
|
||||
key="location",
|
||||
value="Amsterdam",
|
||||
)
|
||||
|
||||
assert record.id == "test_1"
|
||||
assert record.type == MemoryType.USER_PROFILE
|
||||
assert record.key == "location"
|
||||
assert record.value == "Amsterdam"
|
||||
assert record.importance == 0.5 # Default
|
||||
assert record.source == "explicit" # Default
|
||||
|
||||
def test_create_full_record(self):
|
||||
"""Test creating record with all fields."""
|
||||
record = MemoryRecord(
|
||||
id="test_2",
|
||||
type=MemoryType.LEARNED_FACT,
|
||||
key="car",
|
||||
value="Tesla Model 3",
|
||||
keywords=["car", "vehicle", "tesla"],
|
||||
importance=0.8,
|
||||
source="conversation",
|
||||
)
|
||||
|
||||
assert record.keywords == ["car", "vehicle", "tesla"]
|
||||
assert record.importance == 0.8
|
||||
assert record.source == "conversation"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestMemoryServiceInit:
|
||||
"""Tests for MemoryService initialization."""
|
||||
|
||||
def test_service_has_lazy_clients(self):
|
||||
"""Test service initializes with lazy client loading."""
|
||||
service = MemoryService()
|
||||
|
||||
assert service._qdrant is None
|
||||
assert service._embedding is None
|
||||
assert service._cache is None
|
||||
|
||||
def test_global_instance_exists(self):
|
||||
"""Test global memory_service instance exists."""
|
||||
assert memory_service is not None
|
||||
assert isinstance(memory_service, MemoryService)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestMemoryServiceProfileMethods:
|
||||
"""Tests for profile-related methods."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_profile_uses_context(self):
|
||||
"""Test get_profile uses request context for user."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "_get_memory", new_callable=AsyncMock) as mock_get:
|
||||
mock_get.return_value = "Amsterdam"
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.get_profile("location")
|
||||
|
||||
mock_get.assert_called_once_with("testuser", MemoryType.USER_PROFILE, "location")
|
||||
assert result == "Amsterdam"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_profile_explicit_user(self):
|
||||
"""Test get_profile with explicit user parameter."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "_get_memory", new_callable=AsyncMock) as mock_get:
|
||||
mock_get.return_value = "Berlin"
|
||||
|
||||
result = await service.get_profile("location", user="otheruser")
|
||||
|
||||
mock_get.assert_called_once_with("otheruser", MemoryType.USER_PROFILE, "location")
|
||||
assert result == "Berlin"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_profile_high_importance(self):
|
||||
"""Test set_profile uses high importance (0.9)."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "_set_memory", new_callable=AsyncMock) as mock_set:
|
||||
mock_set.return_value = True
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.set_profile("timezone", "Europe/Amsterdam")
|
||||
|
||||
call_kwargs = mock_set.call_args[1]
|
||||
assert call_kwargs["importance"] == 0.9
|
||||
assert result is True
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestMemoryServicePreferenceMethods:
|
||||
"""Tests for preference-related methods."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_preference(self):
|
||||
"""Test get_preference retrieves correctly."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "_get_memory", new_callable=AsyncMock) as mock_get:
|
||||
mock_get.return_value = "celsius"
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.get_preference("temperature_unit")
|
||||
|
||||
mock_get.assert_called_once_with("testuser", MemoryType.PREFERENCE, "temperature_unit")
|
||||
assert result == "celsius"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_preference_medium_importance(self):
|
||||
"""Test set_preference uses medium importance (0.7)."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "_set_memory", new_callable=AsyncMock) as mock_set:
|
||||
mock_set.return_value = True
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.set_preference("theme", "dark")
|
||||
|
||||
call_kwargs = mock_set.call_args[1]
|
||||
assert call_kwargs["importance"] == 0.7
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestMemoryServiceFactMethods:
|
||||
"""Tests for fact-related methods."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_store_fact_default_importance(self):
|
||||
"""Test store_fact uses default importance (0.5)."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "_set_memory", new_callable=AsyncMock) as mock_set:
|
||||
mock_set.return_value = True
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.store_fact("car", "Tesla Model 3")
|
||||
|
||||
call_kwargs = mock_set.call_args[1]
|
||||
assert call_kwargs["importance"] == 0.5
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_store_fact_custom_importance(self):
|
||||
"""Test store_fact with custom importance."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "_set_memory", new_callable=AsyncMock) as mock_set:
|
||||
mock_set.return_value = True
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.store_fact(
|
||||
"employer",
|
||||
"Acme Corp",
|
||||
importance=0.8,
|
||||
)
|
||||
|
||||
call_kwargs = mock_set.call_args[1]
|
||||
assert call_kwargs["importance"] == 0.8
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_fact(self):
|
||||
"""Test get_fact retrieves correctly."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "_get_memory", new_callable=AsyncMock) as mock_get:
|
||||
mock_get.return_value = "Tesla Model 3"
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.get_fact("car")
|
||||
|
||||
mock_get.assert_called_once_with("testuser", MemoryType.LEARNED_FACT, "car")
|
||||
assert result == "Tesla Model 3"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestMemoryServicePrefetch:
|
||||
"""Tests for prefetch_context method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_prefetch_default_keys(self):
|
||||
"""Test prefetch with default profile keys."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "get_profile", new_callable=AsyncMock) as mock_profile:
|
||||
with patch.object(service, "get_all_preferences", new_callable=AsyncMock) as mock_prefs:
|
||||
mock_profile.side_effect = [
|
||||
"Amsterdam", # location
|
||||
"Europe/Amsterdam", # timezone
|
||||
"John", # name
|
||||
]
|
||||
mock_prefs.return_value = {"temperature_unit": "celsius"}
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.prefetch_context()
|
||||
|
||||
assert result["profile"]["location"] == "Amsterdam"
|
||||
assert result["profile"]["timezone"] == "Europe/Amsterdam"
|
||||
assert result["profile"]["name"] == "John"
|
||||
assert result["preferences"]["temperature_unit"] == "celsius"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_prefetch_specific_keys(self):
|
||||
"""Test prefetch with specific profile keys."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "get_profile", new_callable=AsyncMock) as mock_profile:
|
||||
with patch.object(service, "get_all_preferences", new_callable=AsyncMock) as mock_prefs:
|
||||
mock_profile.return_value = "Amsterdam"
|
||||
mock_prefs.return_value = {}
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.prefetch_context(
|
||||
profile_keys=["location"],
|
||||
include_preferences=False,
|
||||
)
|
||||
|
||||
# Should only fetch location
|
||||
mock_profile.assert_called_once()
|
||||
mock_prefs.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_prefetch_no_profile(self):
|
||||
"""Test prefetch without profile data."""
|
||||
service = MemoryService()
|
||||
|
||||
with patch.object(service, "get_profile", new_callable=AsyncMock) as mock_profile:
|
||||
with patch.object(service, "get_all_preferences", new_callable=AsyncMock) as mock_prefs:
|
||||
mock_prefs.return_value = {"theme": "dark"}
|
||||
|
||||
with patch("src.core.memory_service.get_user", return_value="testuser"):
|
||||
result = await service.prefetch_context(include_profile=False)
|
||||
|
||||
mock_profile.assert_not_called()
|
||||
assert "profile" not in result
|
||||
assert result["preferences"]["theme"] == "dark"
|
||||
@@ -43,8 +43,8 @@ LOG_FILE="$LOGS_DIR/server.log"
|
||||
echo -e "${YELLOW}Logs will be written to: ${LOG_FILE}${NC}"
|
||||
|
||||
# Start the server
|
||||
echo -e "${GREEN}Starting uvicorn server on http://localhost:8000${NC}"
|
||||
echo -e "${GREEN}Starting uvicorn server on http://localhost:8123${NC}"
|
||||
echo -e "${YELLOW}Press Ctrl+C to stop the server${NC}"
|
||||
echo ""
|
||||
|
||||
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000 2>&1 | tee "$LOG_FILE"
|
||||
uvicorn src.main:app --reload --host 0.0.0.0 --port 8123 2>&1 | tee "$LOG_FILE"
|
||||
|
||||
Reference in New Issue
Block a user