|
@@ -7,6 +7,7 @@ import { MessageV2 } from "../session/message-v2"
|
|
|
import { Identifier } from "../id/id"
|
|
import { Identifier } from "../id/id"
|
|
|
import { Agent } from "../agent/agent"
|
|
import { Agent } from "../agent/agent"
|
|
|
import { SessionPrompt } from "../session/prompt"
|
|
import { SessionPrompt } from "../session/prompt"
|
|
|
|
|
+import { iife } from "@/util/iife"
|
|
|
import { defer } from "@/util/defer"
|
|
import { defer } from "@/util/defer"
|
|
|
|
|
|
|
|
export const TaskTool = Tool.define("task", async () => {
|
|
export const TaskTool = Tool.define("task", async () => {
|
|
@@ -23,13 +24,21 @@ export const TaskTool = Tool.define("task", async () => {
|
|
|
description: z.string().describe("A short (3-5 words) description of the task"),
|
|
description: z.string().describe("A short (3-5 words) description of the task"),
|
|
|
prompt: z.string().describe("The task for the agent to perform"),
|
|
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"),
|
|
subagent_type: z.string().describe("The type of specialized agent to use for this task"),
|
|
|
|
|
+ session_id: z.string().describe("Existing Task session to continue").optional(),
|
|
|
}),
|
|
}),
|
|
|
async execute(params, ctx) {
|
|
async execute(params, ctx) {
|
|
|
const agent = await Agent.get(params.subagent_type)
|
|
const agent = await Agent.get(params.subagent_type)
|
|
|
if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`)
|
|
if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`)
|
|
|
- const session = await Session.create({
|
|
|
|
|
- parentID: ctx.sessionID,
|
|
|
|
|
- title: params.description + ` (@${agent.name} subagent)`,
|
|
|
|
|
|
|
+ const session = await iife(async () => {
|
|
|
|
|
+ if (params.session_id) {
|
|
|
|
|
+ const found = await Session.get(params.session_id).catch(() => {})
|
|
|
|
|
+ if (found) return found
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return await Session.create({
|
|
|
|
|
+ parentID: ctx.sessionID,
|
|
|
|
|
+ title: params.description + ` (@${agent.name} subagent)`,
|
|
|
|
|
+ })
|
|
|
})
|
|
})
|
|
|
const msg = await MessageV2.get({ sessionID: ctx.sessionID, messageID: ctx.messageID })
|
|
const msg = await MessageV2.get({ sessionID: ctx.sessionID, messageID: ctx.messageID })
|
|
|
if (msg.info.role !== "assistant") throw new Error("Not an assistant message")
|
|
if (msg.info.role !== "assistant") throw new Error("Not an assistant message")
|
|
@@ -89,13 +98,17 @@ export const TaskTool = Tool.define("task", async () => {
|
|
|
all = await Session.messages({ sessionID: session.id })
|
|
all = await Session.messages({ sessionID: session.id })
|
|
|
all = all.filter((x) => x.info.role === "assistant")
|
|
all = all.filter((x) => x.info.role === "assistant")
|
|
|
all = all.flatMap((msg) => msg.parts.filter((x: any) => x.type === "tool") as MessageV2.ToolPart[])
|
|
all = all.flatMap((msg) => msg.parts.filter((x: any) => x.type === "tool") as MessageV2.ToolPart[])
|
|
|
|
|
+ const text = result.parts.findLast((x) => x.type === "text")?.text ?? ""
|
|
|
|
|
+
|
|
|
|
|
+ const output = text + "\n\n" + ["<task_metadata>", `session_id: ${session.id}`, "</task_metadata>"].join("\n")
|
|
|
|
|
+
|
|
|
return {
|
|
return {
|
|
|
title: params.description,
|
|
title: params.description,
|
|
|
metadata: {
|
|
metadata: {
|
|
|
summary: all,
|
|
summary: all,
|
|
|
sessionId: session.id,
|
|
sessionId: session.id,
|
|
|
},
|
|
},
|
|
|
- output: (result.parts.findLast((x: any) => x.type === "text") as any)?.text ?? "",
|
|
|
|
|
|
|
+ output,
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|