Skip to content

Open-source, local-first AI agent for real work.

3.6 KB
Raw Blame
import { type Static, Type } from "@sinclair/typebox";
import type { RuntimeToolDefinition } from "@vetta/runtime-core/kernel";
import { ToolCallDescriptionSchema } from "@vetta/runtime-tools/coding";
import type { CodingAgentWorkflowDispatcherPort } from "../../../../runtime-contracts/index.js";
import { resolveSubagentTaskMessage, SubagentTaskContractSchema } from "../../task-contract.js";
import { DISPATCH_WORKFLOWS_TOOL_DESCRIPTION } from "./description.js";

export const DISPATCH_WORKFLOWS_MAX_BATCH = 8;

const WorkflowItemSchema = Type.Object({
	task_name: Type.String({
		description:
			"Unique snake_case id for this workflow (e.g. refactor_api). Lowercase letters, digits, underscore; start with a letter.",
	}),
	title: Type.String({
		description:
			"Human-readable one-line summary of this workflow, shown in the UI (e.g. 重构 API 鉴权层). Write it in the user's language.",
	}),
	task: Type.Optional(SubagentTaskContractSchema),
	message: Type.Optional(
		Type.String({
			description: "Legacy task text. New calls must use the structured task contract.",
		}),
	),
	todos: Type.Array(Type.String(), {
		minItems: 1,
		description: "Ordered todo items pre-filled into the workflow's own todo list.",
	}),
});

export const DispatchWorkflowsToolInputSchema = Type.Object({
	description: ToolCallDescriptionSchema,
	workflows: Type.Array(WorkflowItemSchema, {
		minItems: 1,
		maxItems: DISPATCH_WORKFLOWS_MAX_BATCH,
		description: `Workflows to dispatch in one batch (max ${DISPATCH_WORKFLOWS_MAX_BATCH}).`,
	}),
});

export type DispatchWorkflowsToolInput = Static<typeof DispatchWorkflowsToolInputSchema>;

export interface DispatchWorkflowsToolOptions {
	readonly getWorkflowDispatcher: () => CodingAgentWorkflowDispatcherPort | undefined;
	readonly workflowTypeId: string;
}

export function createDispatchWorkflowsTool(
	options: DispatchWorkflowsToolOptions,
): RuntimeToolDefinition<DispatchWorkflowsToolInput> {
	let batchSequence = 0;
	return {
		name: "dispatch_workflows",
		label: "dispatch_workflows",
		description: DISPATCH_WORKFLOWS_TOOL_DESCRIPTION,
		inputSchema: DispatchWorkflowsToolInputSchema,
		async execute({ input, toolCallId }) {
			const dispatcher = requireWorkflowDispatcher(options);
			batchSequence += 1;
			const batchId = `workflow-batch-${batchSequence}`;
			const snapshots = dispatcher.dispatchWorkflows(
				input.workflows.map((workflow) => ({
					taskName: workflow.task_name,
					title: workflow.title,
					message: resolveSubagentTaskMessage(workflow),
					agentType: options.workflowTypeId,
					todos: workflow.todos,
					deliveryMode: "batch",
					batchId,
					originToolCallId: toolCallId,
				})),
			);
			const lines = [`Dispatched ${snapshots.length} workflow(s):`];
			for (const snapshot of snapshots) {
				const todo = snapshot.todoProgress
					? ` todos: ${snapshot.todoProgress.done}/${snapshot.todoProgress.total}`
					: "";
				lines.push(`- ${snapshot.taskName} [${snapshot.status}] id: ${snapshot.id}${todo}`);
			}
			lines.push(
				"You will receive one <subagent_notification> after every workflow in this batch reaches a terminal state. Do NOT call wait_agent — end your turn (or continue other work) and handle the batch result passively.",
			);
			return {
				content: [{ type: "text", text: lines.join("\n") }],
				details: { workflows: snapshots },
			};
		},
	};
}

function requireWorkflowDispatcher(options: DispatchWorkflowsToolOptions): CodingAgentWorkflowDispatcherPort {
	const dispatcher = options.getWorkflowDispatcher();
	if (!dispatcher) throw new Error("Subagents are not enabled for this session.");
	return dispatcher;
}