mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-25 11:20:07 +08:00
| .. | ||
| database | ||
| interfaces | ||
| services | ||
| AGENT_MESSAGE_ARCHITECTURE.md | ||
| BaseService.ts | ||
| drizzle.config.ts | ||
| errors.ts | ||
| index.ts | ||
| README.md | ||
Agents Service
Simplified Drizzle ORM implementation for agent and session management in Cherry Studio.
Features
- Native Drizzle migrations - Uses built-in migrate() function
- Zero CLI dependencies in production
- Auto-initialization with retry logic
- Full TypeScript type safety
- Model validation to ensure models exist and provider configuration matches the agent type
Schema
agents.schema.ts- Agent definitionssessions.schema.ts- Session and message tablesmigrations.schema.ts- Migration tracking
Usage
import { agentService } from './services'
// Create agent - fully typed
const agent = await agentService.createAgent({
type: 'custom',
name: 'My Agent',
model: 'anthropic:claude-3-5-sonnet-20241022'
})
Model Validation
- Model identifiers must use the
provider:model_idformat (for exampleanthropic:claude-3-5-sonnet-20241022). model,plan_model, andsmall_modelare validated against the configured providers before the database is touched.- Invalid configurations return a
400 invalid_request_errorresponse and the create/update operation is aborted.
Development Commands
# Apply schema changes
yarn agents:generate
# Quick development sync
yarn agents:push
# Database tools
yarn agents:studio # Open Drizzle Studio
yarn agents:health # Health check
yarn agents:drop # Reset database
Workflow
- Edit schema in
/database/schema/ - Generate migration with
yarn agents:generate - Test changes with
yarn agents:health - Deploy - migrations apply automatically
Services
AgentService- Agent CRUD operationsSessionService- Session managementSessionMessageService- Message loggingBaseService- Database utilitiesschemaSyncer- Migration handler
Troubleshooting
# Check status
yarn agents:health
# Apply migrations
yarn agents:migrate
# Reset completely
yarn agents:reset --yes
The simplified migration system reduced complexity from 463 to ~30 lines while maintaining all functionality through Drizzle's native migration system.