| 29 | session_id: z.string().describe("Existing Task session to continue").optional(), |
| 30 | }), |
| 31 | async execute(params, ctx) { |
| 32 | const agent = await Agent.get(params.subagent_type) |
| 33 | if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`) |
| 34 | |
| 35 | Telemetry.agentSpawned(agent.name) |
| 36 | |
| 37 | const session = await iife(async () => { |
| 38 | if (params.session_id) { |
| 39 | const found = await Session.get(params.session_id).catch(() => {}) |
| 40 | if (found) return found |
| 41 | } |
| 42 | |
| 43 | return await Session.create({ |
| 44 | parentID: ctx.sessionID, |
| 45 | title: params.description + ` (@${agent.name} subagent)`, |
| 46 | }) |
| 47 | }) |
| 48 | const msg = await MessageV2.get({ sessionID: ctx.sessionID, messageID: ctx.messageID }) |
| 49 | if (msg.info.role !== "assistant") throw new Error("Not an assistant message") |
| 50 | |
| 51 | ctx.metadata({ |
| 52 | title: params.description, |
| 53 | metadata: { |
| 54 | sessionId: session.id, |
| 55 | }, |
| 56 | }) |
| 57 | |
| 58 | const messageID = Identifier.ascending("message") |
| 59 | const parts: Record<string, { id: string; tool: string; state: { status: string; title?: string } }> = {} |
| 60 | const unsub = Bus.subscribe(MessageV2.Event.PartUpdated, async (evt) => { |
| 61 | if (evt.properties.part.sessionID !== session.id) return |
| 62 | if (evt.properties.part.messageID === messageID) return |
| 63 | if (evt.properties.part.type !== "tool") return |
| 64 | const part = evt.properties.part |
| 65 | parts[part.id] = { |
| 66 | id: part.id, |
| 67 | tool: part.tool, |
| 68 | state: { |
| 69 | status: part.state.status, |
| 70 | title: part.state.status === "completed" ? part.state.title : undefined, |
| 71 | }, |
| 72 | } |
| 73 | ctx.metadata({ |
| 74 | title: params.description, |
| 75 | metadata: { |
| 76 | summary: Object.values(parts).sort((a, b) => a.id.localeCompare(b.id)), |
| 77 | sessionId: session.id, |
| 78 | }, |
| 79 | }) |
| 80 | }) |
| 81 | |
| 82 | const model = agent.model ?? { |
| 83 | modelID: msg.info.modelID, |
| 84 | providerID: msg.info.providerID, |
| 85 | } |
| 86 | |
| 87 | function cancel() { |
| 88 | SessionPrompt.cancel(session.id) |