From 908a89d772872eb80edef61fb57b8fb3425d6839 Mon Sep 17 00:00:00 2001 From: chahinebrini Date: Thu, 25 Jun 2026 12:24:14 +0200 Subject: [PATCH] feat(services): add delegate service and refactor CLI --- src/cli/commands/delegate.ts | 35 +++++++------------------ src/core/services/delegateService.ts | 38 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 src/core/services/delegateService.ts diff --git a/src/cli/commands/delegate.ts b/src/cli/commands/delegate.ts index e3e0e98..a9bcdce 100644 --- a/src/cli/commands/delegate.ts +++ b/src/cli/commands/delegate.ts @@ -1,39 +1,22 @@ -import { loadConfig } from '../../core/config.js'; -import { Index } from '../../core/index.js'; -import { handoffCreate } from './handoff.js'; +import { suggestDelegation, autoDelegate } from '../../core/services/delegateService.js'; export async function delegate(cwd: string, options: { auto?: boolean } = {}): Promise { - const config = loadConfig(cwd); - const index = new Index(cwd); - const openTasks = index.list('task', { status: 'open' }); - index.close(); - - if (openTasks.length === 0) { + const suggestion = suggestDelegation(cwd); + if (!suggestion) { console.log('No open tasks to delegate.'); return; } - const task = openTasks[0]; - const role = task.role ?? 'implementer'; - const preferredAgent = config.roles[role]?.preferredAgent ?? 'codex'; - console.log('Suggested delegation:'); - console.log(` Task: ${task.id} — ${task.title}`); - console.log(` Role: ${role}`); - console.log(` Preferred agent: ${preferredAgent}`); + console.log(` Task: ${suggestion.task.id} — ${suggestion.task.title}`); + console.log(` Role: ${suggestion.role}`); + console.log(` Preferred agent: ${suggestion.preferredAgent}`); - if (config.delegationMode === 'auto' || options.auto) { - await handoffCreate(cwd, { - fromRole: 'user', - toRole: role, - toAgent: preferredAgent, - taskId: task.id, - summary: `Delegate ${task.id} to ${role}`, - context: `Task "${task.title}" should be handled by ${preferredAgent} in ${role} role.`, - }); + if (options.auto) { + autoDelegate(cwd); console.log('Handoff created automatically.'); } else { console.log('Run with --auto to create the handoff, or run:'); - console.log(` agenthub handoff create --taskId ${task.id}`); + console.log(` agenthub handoff create --taskId ${suggestion.task.id}`); } } diff --git a/src/core/services/delegateService.ts b/src/core/services/delegateService.ts new file mode 100644 index 0000000..b896218 --- /dev/null +++ b/src/core/services/delegateService.ts @@ -0,0 +1,38 @@ +import { loadConfig } from '../config.js'; +import { Index } from '../index.js'; +import { createHandoff } from './handoffService.js'; +import type { Handoff } from '../schema.js'; +import type { IndexEntry } from '../index.js'; + +export interface DelegationSuggestion { + task: IndexEntry; + role: string; + preferredAgent: string; +} + +export function suggestDelegation(cwd: string): DelegationSuggestion | null { + const config = loadConfig(cwd); + const index = new Index(cwd); + const openTasks = index.list('task', { status: 'open' }); + index.close(); + + if (openTasks.length === 0) return null; + + const task = openTasks[0]; + const role = task.role ?? 'implementer'; + const preferredAgent = config.roles[role]?.preferredAgent ?? 'codex'; + return { task, role, preferredAgent }; +} + +export function autoDelegate(cwd: string): Handoff | null { + const suggestion = suggestDelegation(cwd); + if (!suggestion) return null; + return createHandoff(cwd, { + fromRole: 'user', + toRole: suggestion.role, + toAgent: suggestion.preferredAgent, + taskId: suggestion.task.id, + summary: `Delegate ${suggestion.task.id} to ${suggestion.role}`, + context: `Task "${suggestion.task.title}" should be handled by ${suggestion.preferredAgent} in ${suggestion.role} role.`, + }); +}