// ============================================================ // Agent Tool - Subagent orchestration tool // ============================================================ // 支持基于配置的子代理和异步启动。 import { z } from "zod"; import type { ToolCallId, TraceId } from "../interfaces/shared.js"; import type { ModelUsage } from "../model/index.js"; import { toToolJsonSchema } from "./json-schema.js"; export const AgentType = { GeneralPurpose: "general-purpose", Explore: "Explore", } as const; export type AgentType = string; export const AgentInputSchema = z.object({ description: z.string().describe("A short (3-5 word) description of the task"), prompt: z.string().describe("The task for the agent to perform"), subagent_type: z .string() .optional() .describe("The type of specialized agent to use for this task"), // subagent 模型由 Settings / Markdown profile 统一决定;若把调用级 // model 暴露给父模型,历史 tool call 会持续生成旧 override 并覆盖当前配置。 run_in_background: z .boolean() .optional() .describe( "Set to true to run this agent in the background. You will be notified when it completes.", ), }); export type AgentInput = z.infer; export const AgentInputJsonSchema = toToolJsonSchema(AgentInputSchema); export interface AgentTextContentBlock { type: "text"; text: string; } export interface AgentCompletedOutput { status: "completed"; agentId: string; agentType: AgentType; description: string; prompt: string; content: AgentTextContentBlock[]; totalToolUseCount: number; totalDurationMs: number; totalTokens?: number; usage?: ModelUsage; } export interface AgentBackgroundedOutput { status: "async_launched"; isAsync: true; agentId: string; agentType: AgentType; description: string; prompt: string; childSessionId: string; backgroundTaskId: string; outputFile: string; canReadOutputFile: boolean; } export type AgentOutput = AgentCompletedOutput | AgentBackgroundedOutput; export const AgentTextContentBlockSchema = z .object({ type: z.literal("text"), text: z.string(), }) .strict(); export const AgentCompletedOutputSchema = z .object({ status: z.literal("completed"), agentId: z.string(), agentType: z.string(), description: z.string(), prompt: z.string(), content: z.array(AgentTextContentBlockSchema), totalToolUseCount: z.number().int().nonnegative(), totalDurationMs: z.number().int().nonnegative(), totalTokens: z.number().int().nonnegative().optional(), usage: z.record(z.unknown()).optional(), }) .strict(); export const AgentBackgroundedOutputSchema = z .object({ status: z.literal("async_launched"), isAsync: z.literal(true), agentId: z.string(), agentType: z.string(), description: z.string(), prompt: z.string(), childSessionId: z.string(), backgroundTaskId: z.string(), outputFile: z.string(), canReadOutputFile: z.boolean(), }) .strict(); export const AgentOutputSchema = z.union([ AgentCompletedOutputSchema, AgentBackgroundedOutputSchema, ]); export const AgentOutputJsonSchema = toToolJsonSchema(AgentOutputSchema); export interface AgentToolCall { id: ToolCallId; name: "Agent"; input: AgentInput; traceId: TraceId; startedAt: Date; } export interface AgentToolResult { toolCallId: ToolCallId; output: AgentOutput; traceId: TraceId; durationMs: number; } export const AgentErrorCode = { SUBAGENT_UNAVAILABLE: "agent_subagent_unavailable", BACKGROUND_UNAVAILABLE: "agent_background_unavailable", UNKNOWN_AGENT_TYPE: "agent_unknown_type", CHILD_RUNTIME_FAILED: "agent_child_runtime_failed", } as const; export type AgentErrorCode = (typeof AgentErrorCode)[keyof typeof AgentErrorCode];