This release has a bunch of minor breaking changes if you are using opencode plugins or sdk 1. storage events have been removed (we might bring this back but had some issues) 2. concept of `app` is gone - there is a new concept called `project` and endpoints to list projects and get the current project 3. plugin receives `directory` which is cwd and `worktree` which is where the root of the project is if it's a git repo 4. the session.chat function has been renamed to session.prompt in sdk. it no longer requires model to be passed in (model is now an object) 5. every endpoint takes an optional `directory` parameter to operate as though opencode is running in that directory
87 lines
3.0 KiB
TypeScript
87 lines
3.0 KiB
TypeScript
import { Tool } from "./tool"
|
|
import DESCRIPTION from "./task.txt"
|
|
import { z } from "zod"
|
|
import { Session } from "../session"
|
|
import { Bus } from "../bus"
|
|
import { MessageV2 } from "../session/message-v2"
|
|
import { Identifier } from "../id/id"
|
|
import { Agent } from "../agent/agent"
|
|
|
|
export const TaskTool = Tool.define("task", async () => {
|
|
const agents = await Agent.list().then((x) => x.filter((a) => a.mode !== "primary"))
|
|
const description = DESCRIPTION.replace(
|
|
"{agents}",
|
|
agents
|
|
.map((a) => `- ${a.name}: ${a.description ?? "This subagent should only be called manually by the user."}`)
|
|
.join("\n"),
|
|
)
|
|
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)
|
|
if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`)
|
|
const session = await Session.create(ctx.sessionID, params.description + ` (@${agent.name} subagent)`)
|
|
const msg = await Session.getMessage(ctx.sessionID, ctx.messageID)
|
|
if (msg.info.role !== "assistant") throw new Error("Not an assistant message")
|
|
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)),
|
|
},
|
|
})
|
|
})
|
|
|
|
const model = agent.model ?? {
|
|
modelID: msg.info.modelID,
|
|
providerID: msg.info.providerID,
|
|
}
|
|
|
|
ctx.abort.addEventListener("abort", () => {
|
|
Session.abort(session.id)
|
|
})
|
|
const result = await Session.prompt({
|
|
messageID,
|
|
sessionID: session.id,
|
|
model: {
|
|
modelID: model.modelID,
|
|
providerID: model.providerID,
|
|
},
|
|
agent: agent.name,
|
|
tools: {
|
|
todowrite: false,
|
|
todoread: false,
|
|
task: false,
|
|
...agent.tools,
|
|
},
|
|
parts: [
|
|
{
|
|
id: Identifier.ascending("part"),
|
|
type: "text",
|
|
text: params.prompt,
|
|
},
|
|
],
|
|
})
|
|
unsub()
|
|
return {
|
|
title: params.description,
|
|
metadata: {
|
|
summary: result.parts.filter((x: any) => x.type === "tool"),
|
|
},
|
|
output: (result.parts.findLast((x: any) => x.type === "text") as any)?.text ?? "",
|
|
}
|
|
},
|
|
}
|
|
})
|