feat(services): add delegate service and refactor CLI

This commit is contained in:
chahinebrini 2026-06-25 12:24:14 +02:00
parent 6708dde532
commit 908a89d772
2 changed files with 47 additions and 26 deletions

View File

@ -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<void> {
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}`);
}
}

View File

@ -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.`,
});
}