cherry-studio/src/main/services/agents
Vaayne f90bda861f feat(agents): add automatic database schema synchronization
- Add schemaSyncer.ts with Drizzle Kit push integration
- Integrate auto schema sync into BaseService.initialize()
- Database schema now automatically updates on agent service startup
- Users no longer need manual migration commands
- Ensures schema consistency across app updates

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2025-09-14 23:07:54 +08:00
..
database 🏗️ refactor: migrate agents service from custom migrations to Drizzle ORM 2025-09-13 19:51:16 +08:00
services Refactor: Remove sessions route and related validation logic 2025-09-14 16:31:15 +08:00
BaseService.ts feat(agents): add automatic database schema synchronization 2025-09-14 23:07:54 +08:00
drizzle.config.ts 🏗️ refactor: migrate agents service from custom migrations to Drizzle ORM 2025-09-13 19:51:16 +08:00
index.ts 🏗️ refactor: migrate agents service from custom migrations to Drizzle ORM 2025-09-13 19:51:16 +08:00
README.md 🏗️ refactor: migrate agents service from custom migrations to Drizzle ORM 2025-09-13 19:51:16 +08:00
schemaSyncer.ts feat(agents): add automatic database schema synchronization 2025-09-14 23:07:54 +08:00

Agents Service - Drizzle ORM Implementation

This service now uses a clean, modern Drizzle ORM implementation for all database operations.

Database Schema

The database schema is defined in /database/schema/ using Drizzle ORM:

  • agents.schema.ts - Agent table and indexes
  • sessions.schema.ts - Sessions and session logs tables
  • migrations.schema.ts - Migration tracking (if needed)

Working with the Database

Development Setup

For new development, you can:

  1. Use Drizzle Kit to generate migrations from schema:

    npx drizzle-kit generate:sqlite --config src/main/services/agents/drizzle.config.ts
    
  2. Push schema directly to database (for development):

    npx drizzle-kit push:sqlite --config src/main/services/agents/drizzle.config.ts
    
  3. Create tables programmatically (if needed): The schema exports can be used with CREATE TABLE statements.

Usage

All database operations are now fully type-safe:

import { agentService } from './services'

// Create an agent - fully typed
const agent = await agentService.createAgent({
  type: 'custom',
  name: 'My Agent',
  model: 'claude-3-5-sonnet-20241022'
})

// TypeScript knows the exact shape of the returned data
console.log(agent.id) // ✅ Type-safe

Architecture

  • Pure Drizzle ORM: No legacy migration system
  • Type Safety: Full TypeScript integration
  • Modern Patterns: Schema-first development
  • Simplicity: Clean, maintainable codebase

Services

  • AgentService - CRUD operations for agents
  • SessionService - Session management
  • SessionMessageService - Message logging
  • BaseService - Shared database utilities