Files
cherry-studio/src/main/services/agents
SuYao 36f86ff2b9 Refactor/agent align (#10276)
* Refactor agent streaming from EventEmitter to ReadableStream

Replaced EventEmitter-based agent streaming with ReadableStream for
better compatibility with AI SDK patterns. Modified
SessionMessageService to return stream/completion pair instead of event
emitter, updated HTTP handlers to use stream pumping, and added IPC
contract for renderer-side message persistence.

* Add accessible paths management to agent configuration

Move accessible paths functionality from session modal to agent modal,
add validation requiring at least one path, and update form handling to
inherit agent paths in sessions.

* Add provider_name field to model objects and improve display

- Add provider_name field to ApiModel schema and transformation logic
- Update model options to include providerName for better display
- Improve provider label fallback chain in model transformation
- Fix agent hook to use proper SWR key and conditional fetching
- Enhance option rendering with better truncation and provider display

* fix(i18n): Auto update translations for PR #10276

* Optimize chat components with memoization and shared layout

- Wrap `SessionMessages` and `SessionInputBar` in `useMemo` to prevent unnecessary re-renders
- Refactor `AgentSessionMessages` to use shared layout components and message grouping
- Extract common styled components to `shared.tsx` for reuse across message components

* Add smooth animations to SessionsTab and Sessions components

- Replace static conditional rendering with Framer Motion animations for no-agent and session states
- Animate session list items with staggered entrance and exit transitions
- Add loading spinner animation with fade effect
- Apply motion to session creation button with delayed entrance

* Add loading state with spinner and i18n support to SessionsTab

- Replace static "No active agent" message with a spinner and loading text
- Integrate react-i18next for translation of loading message
- Adjust animation timing and styling for smoother loading state transition

* Support API models with provider_name field in getModelName

- Add ApiModel type import and update function signature to accept ApiModel
- Return formatted name using provider_name field for API models
- Maintain backward compatibility for legacy models by looking up provider in store

* Simplify provider display name logic and add debug logging

- Replace complex fallback chain for provider display name with direct provider name access
- Add console.log for model debugging in getModelName function

* Extract model name from session model string

- Use split and pop to isolate the model name after the colon
- Fall back to the full model string if no colon is present
- Maintain provider and group identifiers for model object consistency

* Improve model name resolution for agent sessions

- Extract actual model ID from session model string and resolve model details
- Use resolved model name, provider, and group when available instead of defaults
- Remove redundant API model handling in getModelName function

* Set default active agent and session on load

- Automatically select first agent if none active after loading
- Automatically select first session per agent if none active after loading
- Prevent empty selection states in UI components

---------

Co-authored-by: GitHub Action <action@github.com>
2025-09-20 16:56:53 +08:00
..
2025-09-20 16:56:53 +08:00
2025-09-20 16:56:53 +08:00
2025-09-20 16:56:53 +08:00
2025-09-20 16:56:53 +08:00

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 definitions
  • sessions.schema.ts - Session and message tables
  • migrations.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_id format (for example anthropic:claude-3-5-sonnet-20241022).
  • model, plan_model, and small_model are validated against the configured providers before the database is touched.
  • Invalid configurations return a 400 invalid_request_error response 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

  1. Edit schema in /database/schema/
  2. Generate migration with yarn agents:generate
  3. Test changes with yarn agents:health
  4. Deploy - migrations apply automatically

Services

  • AgentService - Agent CRUD operations
  • SessionService - Session management
  • SessionMessageService - Message logging
  • BaseService - Database utilities
  • schemaSyncer - 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.