Appearance
AI Domain
The AI module centers on a configurable Agent aggregate and the execution data produced around it.
Agent Aggregate
An Agent owns the high-level product behavior exposed to end users:
- Identity:
name,role,avatar,description,internalNameId,visibility, andisEnabled. - Brain config: primary
model, optionalfallbackModel, and supportedlanguages. - Instruction config:
systemPrompt, optionalgreetingMessage, fixed rules, variable rules, prohibitions,personality, andwritingStyle. - Knowledge config:
isEnabled,chunkSize,chunkOverlap,embeddingModel,similarityThreshold,searchResults, and an internalqdrantCollectionName. - Limits config: request media limits,
messagesPerMinute, token budgets, and theexecutionLimitActionfor capped or blocked executions. - Stats: chat count, user count, and processed message count.
Important Defaults
- New agents always get a knowledge config, but it starts disabled.
- The knowledge collection name defaults to
agent-<agentId>. - Knowledge defaults are
chunkSize=700,chunkOverlap=150,embeddingModel=openai/text-embedding-3-small,similarityThreshold=0.35, andsearchResults=5. - Limits default to
messagesPerMinute=60, one document per request, and zero-valued token budgets, which the module treats as unlimited. - The default execution over-budget policy is
block_next.
Conversation Model
Chatstores the conversational container for oneagentIdand oneuserId.Messagestores raw Vercel AI SDKparts, optional metadata, the role (userorassistant), and optional message stats.- The read-side message query only exposes
userandassistantmessages in paginated history reads.
Knowledge Model
AgentKnowledgeDocumentrepresents one uploaded knowledge asset for an agent.- Documents move through
pending -> processing -> completed | failed. - Successful processing records
chunkCountandprocessedAt; failures keep a truncatederrorMessage. - Qdrant payloads store the fragment text plus
documentId,fileName,description,language, andagentIdso retrieval and cleanup can stay document-aware.
Model Catalog
Modelmirrors metadata discovered from the Vercel AI Gateway catalog.- The entity stores descriptive metadata, base pricing, provider-specific endpoint data, architecture capabilities, sync status, and last sync timestamps/errors.
- Sync status moves through
PENDING,IN_PROGRESS,SYNCED,FAILED, andREMOVED.
Token Accounting
TokenUsageLogstores every tracked AI cost event with the action type, model id, token counters, cost, and optional execution context (agentId,chatId,userId).OrgTokenUsageSummaryaggregates monthly totals by organization and agency, and keeps breakdowns by model and action.- The summary update path uses a database transaction plus pessimistic locking to avoid lost updates under concurrent writes.
Persistence Ownership
The module owns a dedicated PostgreSQL schema named ai and a dedicated TypeORM connection named ai. Persistence entities exist for:
- agents and their nested configs
- chats and messages
- knowledge documents
- models
- token usage logs and monthly org summaries
AI-specific migration files are not present yet, so the schema contract is represented in code but not fully operationalized for deployment.