Architecture¶
Bounded Contexts¶
The AI service is organized into 7 bounded contexts, each with its own aggregate roots, child entities, repositories, and domain services.
1. Conversation¶
Manages multi-turn chat sessions between users and AI models.
- Aggregate:
Conversation— ownsMessagechildren (cascade delete) - Entities:
Message(role, content, token_count, attachments) - Repository:
ConversationRepository,MessageRepository - Service:
ConversationService— create, list, get, append message, fork
2. Run¶
Orchestrates model↔tool execution loops with safety limits and state machine transitions.
- Aggregate:
Run— ownsRunStepchildren (cascade delete) - Entities:
RunStep(model_call or tool_call, with input/output/error tracking) - Repository:
RunRepository - Service:
RunService— create, start, complete, fail, cancel - State Machine:
PENDING → RUNNING → STREAMING → COMPLETED | FAILED | CANCELLED - Safety:
SafetyLimits(max model calls, tool calls, wall time),LoopBreaker(repeated tool call detection)
3. Tool Registry¶
Discovers and versions external tools from registered sources.
- Aggregate:
ToolSource— ownsToolchildren (cascade delete) - Entities:
Tool(tool_id, name, description, parameters_schema),ToolCatalogRevision(immutable snapshot) - Repository:
ToolRepository,ToolSourceRepository,ToolCatalogRevisionRepository - Service:
ToolRegistryService— list tools, refresh sources, manage catalog revisions
4. Policy¶
Per-space configuration for feature gates and model bindings.
- Aggregate:
SpacePolicy— ownsModelBindingchildren (cascade delete) - Entities:
ModelBinding(provider, modality, model_id, endpoint, API key reference) - Repository:
SpacePolicyRepository - Service:
PolicyService— get/create default policy, update limits and bindings
5. Ingestion¶
Fetches web content, extracts text, and stores in MinIO.
- Aggregate:
SourceDocument(url, document_type, content_hash, blob keys) - Repository:
SourceDocumentRepository - Service:
IngestionService— fetch URL, extract content, store blobs (pending implementation)
6. Embedding¶
Generates and searches conversation vector embeddings using pgvector.
- Entity:
ConversationVector(message_id, embedding, content_hash) - Repository:
ConversationVectorRepository— cosine similarity search - Service:
EmbeddingService— generate embeddings, search conversations
7. Provider¶
Vendor-agnostic abstractions for LLM and embedding APIs.
- ABCs:
ModelProvider(complete, stream),EmbeddingProvider(embed) - Stubs:
StubModelProvider(canned responses),StubEmbeddingProvider(zero vectors, 1536d) - Future: OpenAI and Anthropic adapters selected via
ModelBinding.provider
Entity-Relationship Diagram¶
ai_space_policy
│ PK: id
│ UK: space_id
│
└──< ai_model_binding
FK: space_policy_id → ai_space_policy.id (CASCADE)
ai_conversation
│ PK: id
│ FK: model_binding_id → ai_model_binding.id (SET NULL)
│ FK: forked_from_id → ai_conversation.id (SET NULL)
│
├──< ai_message
│ FK: conversation_id → ai_conversation.id (CASCADE)
│ │
│ └──< ai_conversation_vector
│ FK: message_id → ai_message.id (CASCADE)
│
└──< ai_run
FK: conversation_id → ai_conversation.id (CASCADE)
FK: model_binding_id → ai_model_binding.id (SET NULL)
│
└──< ai_run_step
FK: run_id → ai_run.id (CASCADE)
ai_tool_source
│ PK: id
│
└──< ai_tool
FK: tool_source_id → ai_tool_source.id (CASCADE)
ai_tool_catalog_revision
(standalone — immutable snapshots, referenced by ai_run.pinned_catalog_revision)
ai_source_document
(standalone — web-ingested content with MinIO blob references)
Layer Diagram¶
┌─────────────────────────────────────────────────┐
│ API Layer │
│ FastAPI routers + Pydantic schema models │
│ (conversation, run, tool, admin, ingestion, │
│ embedding) │
├─────────────────────────────────────────────────┤
│ Domain Layer │
│ @transactional services returning DTOs │
│ Aggregate state machines (Run) │
│ Safety limits (LoopBreaker, SafetyLimits) │
├─────────────────────────────────────────────────┤
│ Infrastructure Layer │
│ BaseRepository (SQLAlchemy 2.0) │
│ DbContext (scoped sessions) │
│ Provider ABCs (ModelProvider, EmbeddingProvider) │
├─────────────────────────────────────────────────┤
│ External Systems │
│ PostgreSQL + pgvector MinIO (S3) │
│ RabbitMQ LLM APIs (future) │
└─────────────────────────────────────────────────┘
Cross-Domain References¶
| Source | Target | Relationship |
|---|---|---|
Run.conversation_id | Conversation.id | Run belongs to a conversation |
Run.model_binding_id | ModelBinding.id | Run uses a specific model binding |
Run.pinned_catalog_revision | ToolCatalogRevision.revision | Run pins tool catalog at creation |
ConversationVector.message_id | Message.id | Vector is derived from a message |
Conversation.model_binding_id | ModelBinding.id | Default model for conversation |
Conversation.forked_from_id | Conversation.id | Self-referential fork chain |