import { z } from 'zod'; import type { Tool, ToolCallContext, ToolResult } from '../../types.js'; import { toToolParameters } from '../../utils/zodToJson.js'; import { assertNoDependencyCycle, effectiveTaskStatus, listManagedTasks, mutateManagedTasks, newTaskId, type ManagedTask, type ManagedTaskStatus, } from './taskStore.js'; const id = z.string().regex(/^task_[0-9a-f-]{36}$/i); const createSchema = z.object({ title: z.string().min(1).max(200), description: z.string().max(5000).optional(), activeForm: z.string().max(200).optional(), owner: z.string().max(120).optional().describe('责任人标签;不会自动启动或派遣 Agent'), blockedBy: z.array(id).default([]).optional(), metadata: z.record(z.unknown()).optional(), }); const getSchema = z.object({ task_id: id }); const updateSchema = z.object({ task_id: id, title: z.string().min(1).max(200).optional(), description: z.string().max(5000).optional(), activeForm: z.string().max(200).optional(), owner: z.string().max(120).optional(), blockedBy: z.array(id).optional(), status: z.enum(['pending', 'in_progress', 'completed', 'deleted']).optional(), metadata: z.record(z.unknown()).optional(), }); const listSchema = z.object({ status: z.enum(['pending', 'in_progress', 'completed', 'blocked']).optional(), include_completed: z.boolean().default(false).optional(), }); function render(task: ManagedTask, all: ManagedTask[]): string { return `${task.id} [${effectiveTaskStatus(task, all)}] ${task.title}${task.blockedBy.length ? ` (blockedBy: ${task.blockedBy.join(', ')})` : ''}`; } export const taskCreateTool: Tool> = { name: 'TaskCreate', description: 'Creates a durable task record in the current workspace/session; does not execute work or start an Agent. Use for multi-step work that must survive compression or another turn.', inputSchema: createSchema, parameters: toToolParameters(createSchema), isReadOnly: false, isConcurrencySafe: false, call: async () => ({ ok: false, error: 'TaskCreate requires session context', }), callWithContext: async (input, _s, ctx) => { const now = Date.now(); const task = { id: newTaskId(), title: input.title, description: input.description, activeForm: input.activeForm, owner: input.owner, status: 'pending' as ManagedTaskStatus, blockedBy: input.blockedBy ?? [], metadata: input.metadata, createdAt: now, updatedAt: now, }; try { const tasks = await mutateManagedTasks(ctx, (items) => { const next = [...items, task]; assertNoDependencyCycle(next); return next; }); return { ok: true, content: `已创建 ${render(task, tasks)}` }; } catch (e) { return { ok: false, error: (e as Error).message }; } }, }; export const taskGetTool: Tool> = { name: 'TaskGet', description: 'Reads one durable task and its dependency state.', inputSchema: getSchema, parameters: toToolParameters(getSchema), isReadOnly: true, isConcurrencySafe: true, call: async () => ({ ok: false, error: 'TaskGet requires session context' }), callWithContext: async (input, _s, ctx) => { const tasks = await listManagedTasks(ctx); const task = tasks.find((t) => t.id === input.task_id); return task ? { ok: true, content: JSON.stringify( { ...task, effectiveStatus: effectiveTaskStatus(task, tasks) }, null, 2, ), } : { ok: false, error: `任务不存在:${input.task_id}` }; }, }; export const taskUpdateTool: Tool> = { name: 'TaskUpdate', description: 'Updates task status, dependencies or details; metadata and blockedBy replace their previous values. Mark completed only after actual verification. status=deleted removes only the record, never project files.', inputSchema: updateSchema, parameters: toToolParameters(updateSchema), isReadOnly: false, isConcurrencySafe: false, call: async () => ({ ok: false, error: 'TaskUpdate requires session context', }), callWithContext: async (input, _s, ctx) => { try { let found = false; const tasks = await mutateManagedTasks(ctx, (items) => { const target = items.find((t) => t.id === input.task_id); if (!target) return items; found = true; if (input.status === 'deleted') { if (items.some((t) => t.blockedBy.includes(target.id))) throw new Error('仍有任务依赖它,不能删除'); return items.filter((t) => t.id !== target.id); } Object.assign(target, { ...(input.title !== undefined && { title: input.title }), ...(input.description !== undefined && { description: input.description, }), ...(input.activeForm !== undefined && { activeForm: input.activeForm, }), ...(input.owner !== undefined && { owner: input.owner }), ...(input.blockedBy !== undefined && { blockedBy: input.blockedBy }), ...(input.status !== undefined && { status: input.status }), ...(input.metadata !== undefined && { metadata: input.metadata }), updatedAt: Date.now(), }); assertNoDependencyCycle(items); return items; }); if (!found) return { ok: false, error: `任务不存在:${input.task_id}` }; const task = tasks.find((t) => t.id === input.task_id); return { ok: true, content: task ? `已更新 ${render(task, tasks)}` : `已删除任务记录 ${input.task_id}`, }; } catch (e) { return { ok: false, error: (e as Error).message }; } }, }; export const taskListTool: Tool> = { name: 'TaskList', description: 'Lists active durable tasks and dependency blockers. Completed tasks are hidden by default.', inputSchema: listSchema, parameters: toToolParameters(listSchema), isReadOnly: true, isConcurrencySafe: true, call: async () => ({ ok: false, error: 'TaskList requires session context' }), callWithContext: async (input, _s, ctx) => { const tasks = await listManagedTasks(ctx); const selected = tasks.filter( (t) => (input.status === 'completed' || input.include_completed || t.status !== 'completed') && (!input.status || effectiveTaskStatus(t, tasks) === input.status), ); return { ok: true, content: selected.length ? selected.map((t) => render(t, tasks)).join('\n') : '(no matching tasks)', }; }, };