2025-06-12 21:04:34 +00:00
|
|
|
import { Tool } from "./tool"
|
|
|
|
|
import DESCRIPTION from "./task.txt"
|
2025-10-26 19:50:41 +00:00
|
|
|
import z from "zod"
|
2025-06-12 21:04:34 +00:00
|
|
|
import { Session } from "../session"
|
2025-06-18 17:40:21 +00:00
|
|
|
import { Bus } from "../bus"
|
2025-07-07 19:53:43 +00:00
|
|
|
import { MessageV2 } from "../session/message-v2"
|
2025-07-13 21:22:11 +00:00
|
|
|
import { Identifier } from "../id/id"
|
2025-07-25 01:20:43 +00:00
|
|
|
import { Agent } from "../agent/agent"
|
2025-10-15 06:12:51 +00:00
|
|
|
import { SessionLock } from "../session/lock"
|
2025-09-13 09:46:14 +00:00
|
|
|
import { SessionPrompt } from "../session/prompt"
|
2025-06-12 21:04:34 +00:00
|
|
|
|
2025-07-25 17:29:29 +00:00
|
|
|
export const TaskTool = Tool.define("task", async () => {
|
2025-08-07 20:32:12 +00:00
|
|
|
const agents = await Agent.list().then((x) => x.filter((a) => a.mode !== "primary"))
|
|
|
|
|
const description = DESCRIPTION.replace(
|
|
|
|
|
"{agents}",
|
|
|
|
|
agents
|
2025-11-08 01:59:02 +00:00
|
|
|
.map((a) => `- ${a.name}: ${a.description ?? "This subagent should only be called manually by the user."}`)
|
2025-08-07 20:32:12 +00:00
|
|
|
.join("\n"),
|
|
|
|
|
)
|
2025-07-25 01:20:43 +00:00
|
|
|
return {
|
|
|
|
|
description,
|
|
|
|
|
parameters: z.object({
|
|
|
|
|
description: z.string().describe("A short (3-5 words) description of the task"),
|
|
|
|
|
prompt: z.string().describe("The task for the agent to perform"),
|
|
|
|
|
subagent_type: z.string().describe("The type of specialized agent to use for this task"),
|
|
|
|
|
}),
|
|
|
|
|
async execute(params, ctx) {
|
|
|
|
|
const agent = await Agent.get(params.subagent_type)
|
2025-11-08 01:59:02 +00:00
|
|
|
if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`)
|
2025-10-06 23:37:30 +00:00
|
|
|
const session = await Session.create({
|
|
|
|
|
parentID: ctx.sessionID,
|
|
|
|
|
title: params.description + ` (@${agent.name} subagent)`,
|
|
|
|
|
})
|
2025-11-07 19:46:58 +00:00
|
|
|
const msg = await MessageV2.get({ sessionID: ctx.sessionID, messageID: ctx.messageID })
|
2025-08-15 15:16:08 +00:00
|
|
|
if (msg.info.role !== "assistant") throw new Error("Not an assistant message")
|
2025-10-30 17:15:21 +00:00
|
|
|
|
|
|
|
|
ctx.metadata({
|
|
|
|
|
title: params.description,
|
|
|
|
|
metadata: {
|
|
|
|
|
sessionId: session.id,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2025-07-25 01:20:43 +00:00
|
|
|
const messageID = Identifier.ascending("message")
|
|
|
|
|
const parts: Record<string, MessageV2.ToolPart> = {}
|
|
|
|
|
const unsub = Bus.subscribe(MessageV2.Event.PartUpdated, async (evt) => {
|
|
|
|
|
if (evt.properties.part.sessionID !== session.id) return
|
|
|
|
|
if (evt.properties.part.messageID === messageID) return
|
|
|
|
|
if (evt.properties.part.type !== "tool") return
|
|
|
|
|
parts[evt.properties.part.id] = evt.properties.part
|
|
|
|
|
ctx.metadata({
|
|
|
|
|
title: params.description,
|
|
|
|
|
metadata: {
|
|
|
|
|
summary: Object.values(parts).sort((a, b) => a.id?.localeCompare(b.id)),
|
2025-10-30 17:15:21 +00:00
|
|
|
sessionId: session.id,
|
2025-07-25 01:20:43 +00:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const model = agent.model ?? {
|
2025-07-31 14:34:43 +00:00
|
|
|
modelID: msg.info.modelID,
|
|
|
|
|
providerID: msg.info.providerID,
|
2025-07-25 01:20:43 +00:00
|
|
|
}
|
2025-06-18 17:40:21 +00:00
|
|
|
|
2025-07-25 01:20:43 +00:00
|
|
|
ctx.abort.addEventListener("abort", () => {
|
2025-10-15 06:12:51 +00:00
|
|
|
SessionLock.abort(session.id)
|
2025-07-25 01:20:43 +00:00
|
|
|
})
|
2025-11-12 01:38:50 +00:00
|
|
|
const promptParts = await SessionPrompt.resolvePromptParts(params.prompt)
|
2025-09-13 09:46:14 +00:00
|
|
|
const result = await SessionPrompt.prompt({
|
2025-07-25 01:20:43 +00:00
|
|
|
messageID,
|
|
|
|
|
sessionID: session.id,
|
2025-09-01 21:15:49 +00:00
|
|
|
model: {
|
|
|
|
|
modelID: model.modelID,
|
|
|
|
|
providerID: model.providerID,
|
|
|
|
|
},
|
2025-08-07 20:32:12 +00:00
|
|
|
agent: agent.name,
|
2025-07-25 17:29:29 +00:00
|
|
|
tools: {
|
2025-08-07 20:32:12 +00:00
|
|
|
todowrite: false,
|
|
|
|
|
todoread: false,
|
2025-07-25 17:29:29 +00:00
|
|
|
task: false,
|
2025-08-07 20:32:12 +00:00
|
|
|
...agent.tools,
|
2025-07-25 17:29:29 +00:00
|
|
|
},
|
2025-11-12 01:38:50 +00:00
|
|
|
parts: promptParts,
|
2025-07-25 01:20:43 +00:00
|
|
|
})
|
|
|
|
|
unsub()
|
2025-09-19 05:09:52 +00:00
|
|
|
let all
|
2025-11-06 18:20:13 +00:00
|
|
|
all = await Session.messages({ sessionID: session.id })
|
2025-09-19 05:09:52 +00:00
|
|
|
all = all.filter((x) => x.info.role === "assistant")
|
2025-11-08 01:59:02 +00:00
|
|
|
all = all.flatMap((msg) => msg.parts.filter((x: any) => x.type === "tool") as MessageV2.ToolPart[])
|
2025-07-25 01:20:43 +00:00
|
|
|
return {
|
2025-06-18 17:40:21 +00:00
|
|
|
title: params.description,
|
2025-07-07 19:53:43 +00:00
|
|
|
metadata: {
|
2025-09-19 05:09:52 +00:00
|
|
|
summary: all,
|
2025-10-30 17:15:21 +00:00
|
|
|
sessionId: session.id,
|
2025-06-12 21:04:34 +00:00
|
|
|
},
|
2025-09-01 21:15:49 +00:00
|
|
|
output: (result.parts.findLast((x: any) => x.type === "text") as any)?.text ?? "",
|
2025-07-25 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
2025-06-12 21:04:34 +00:00
|
|
|
})
|