mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-07 13:59:28 +08:00
BREAKING CHANGE: Major refactoring of agents service structure - Split monolithic db.ts into focused query modules (agent, session, sessionLog) - Implement comprehensive migration system with transaction support - Reorganize services into dedicated services/ subdirectory - Add production-ready schema versioning with rollback capability ### New Architecture: - database/migrations/: Version-controlled schema evolution - database/queries/: Entity-specific CRUD operations - database/schema/: Table and index definitions - services/: Business logic layer (AgentService, SessionService, SessionLogService) ### Key Features: - ✅ Migration system with atomic transactions and checksums - ✅ Modular query organization by entity type - ✅ Backward compatibility maintained for existing code - ✅ Production-ready rollback support - ✅ Comprehensive validation and testing ### Benefits: - Single responsibility: Each file handles one specific concern - Better maintainability: Easy to locate and modify entity-specific code - Team-friendly: Reduced merge conflicts with smaller focused files - Scalable: Simple to add new entities without cluttering existing code - Production-ready: Safe schema evolution with migration tracking All existing functionality preserved. Comprehensive testing completed (1420 tests pass).
88 lines
2.7 KiB
TypeScript
88 lines
2.7 KiB
TypeScript
/**
|
|
* SQL queries for Session operations
|
|
*/
|
|
|
|
export const SessionQueries = {
|
|
// Session operations
|
|
insert: `
|
|
INSERT INTO sessions (id, name, main_agent_id, sub_agent_ids, user_goal, status, external_session_id, model, plan_model, small_model, built_in_tools, mcps, knowledges, configuration, accessible_paths, permission_mode, max_steps, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
`,
|
|
|
|
update: `
|
|
UPDATE sessions
|
|
SET name = ?, main_agent_id = ?, sub_agent_ids = ?, user_goal = ?, status = ?, external_session_id = ?, model = ?, plan_model = ?, small_model = ?, built_in_tools = ?, mcps = ?, knowledges = ?, configuration = ?, accessible_paths = ?, permission_mode = ?, max_steps = ?, updated_at = ?
|
|
WHERE id = ?
|
|
`,
|
|
|
|
updateStatus: `
|
|
UPDATE sessions
|
|
SET status = ?, updated_at = ?
|
|
WHERE id = ?
|
|
`,
|
|
|
|
getById: `
|
|
SELECT * FROM sessions
|
|
WHERE id = ?
|
|
`,
|
|
|
|
list: `
|
|
SELECT * FROM sessions
|
|
ORDER BY created_at DESC
|
|
`,
|
|
|
|
listWithLimit: `
|
|
SELECT * FROM sessions
|
|
ORDER BY created_at DESC
|
|
LIMIT ? OFFSET ?
|
|
`,
|
|
|
|
count: 'SELECT COUNT(*) as total FROM sessions',
|
|
|
|
delete: 'DELETE FROM sessions WHERE id = ?',
|
|
|
|
checkExists: 'SELECT id FROM sessions WHERE id = ?',
|
|
|
|
getByStatus: `
|
|
SELECT * FROM sessions
|
|
WHERE status = ?
|
|
ORDER BY created_at DESC
|
|
`,
|
|
|
|
updateExternalSessionId: `
|
|
UPDATE sessions
|
|
SET external_session_id = ?, updated_at = ?
|
|
WHERE id = ?
|
|
`,
|
|
|
|
getSessionWithAgent: `
|
|
SELECT
|
|
s.*,
|
|
a.name as agent_name,
|
|
a.description as agent_description,
|
|
a.avatar as agent_avatar,
|
|
a.instructions as agent_instructions,
|
|
-- Use session configuration if provided, otherwise fall back to agent defaults
|
|
COALESCE(s.model, a.model) as effective_model,
|
|
COALESCE(s.plan_model, a.plan_model) as effective_plan_model,
|
|
COALESCE(s.small_model, a.small_model) as effective_small_model,
|
|
COALESCE(s.built_in_tools, a.built_in_tools) as effective_built_in_tools,
|
|
COALESCE(s.mcps, a.mcps) as effective_mcps,
|
|
COALESCE(s.knowledges, a.knowledges) as effective_knowledges,
|
|
COALESCE(s.configuration, a.configuration) as effective_configuration,
|
|
COALESCE(s.accessible_paths, a.accessible_paths) as effective_accessible_paths,
|
|
COALESCE(s.permission_mode, a.permission_mode) as effective_permission_mode,
|
|
COALESCE(s.max_steps, a.max_steps) as effective_max_steps,
|
|
a.created_at as agent_created_at,
|
|
a.updated_at as agent_updated_at
|
|
FROM sessions s
|
|
LEFT JOIN agents a ON s.main_agent_id = a.id
|
|
WHERE s.id = ?
|
|
`,
|
|
|
|
getByExternalSessionId: `
|
|
SELECT * FROM sessions
|
|
WHERE external_session_id = ?
|
|
`
|
|
} as const
|