Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 963a926

Browse files
shanturactions-userrekram1-node
authored
allow task tool to have resume capabilities (anomalyco#4204)
Co-authored-by: GitHub Action <action@github.com> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com>
1 parent 0d3d48b commit 963a926

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

nix/hashes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"nodeModules": "sha256-lAU5G92UJ998pKOd0EAt9gUa/HUXXvu6xAUdH2STmDo="
2+
"nodeModules": "sha256-Z3GTCHOVaBW79tx2rSkKOCHlZRPiTD6h9pdIDD12kxU="
33
}

packages/opencode/src/tool/task.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { MessageV2 } from "../session/message-v2"
77
import { Identifier } from "../id/id"
88
import { Agent } from "../agent/agent"
99
import { SessionPrompt } from "../session/prompt"
10+
import { iife } from "@/util/iife"
1011
import { defer } from "@/util/defer"
1112

1213
export const TaskTool = Tool.define("task", async () => {
@@ -23,13 +24,21 @@ export const TaskTool = Tool.define("task", async () => {
2324
description: z.string().describe("A short (3-5 words) description of the task"),
2425
prompt: z.string().describe("The task for the agent to perform"),
2526
subagent_type: z.string().describe("The type of specialized agent to use for this task"),
27+
session_id: z.string().describe("Existing Task session to continue").optional(),
2628
}),
2729
async execute(params, ctx) {
2830
const agent = await Agent.get(params.subagent_type)
2931
if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`)
30-
const session = await Session.create({
31-
parentID: ctx.sessionID,
32-
title: params.description + ` (@${agent.name} subagent)`,
32+
const session = await iife(async () => {
33+
if (params.session_id) {
34+
const found = await Session.get(params.session_id).catch(() => {})
35+
if (found) return found
36+
}
37+
38+
return await Session.create({
39+
parentID: ctx.sessionID,
40+
title: params.description + ` (@${agent.name} subagent)`,
41+
})
3342
})
3443
const msg = await MessageV2.get({ sessionID: ctx.sessionID, messageID: ctx.messageID })
3544
if (msg.info.role !== "assistant") throw new Error("Not an assistant message")
@@ -89,13 +98,17 @@ export const TaskTool = Tool.define("task", async () => {
8998
all = await Session.messages({ sessionID: session.id })
9099
all = all.filter((x) => x.info.role === "assistant")
91100
all = all.flatMap((msg) => msg.parts.filter((x: any) => x.type === "tool") as MessageV2.ToolPart[])
101+
const text = result.parts.findLast((x) => x.type === "text")?.text ?? ""
102+
103+
const output = text + "\n\n" + ["<task_metadata>", `session_id: ${session.id}`, "</task_metadata>"].join("\n")
104+
92105
return {
93106
title: params.description,
94107
metadata: {
95108
summary: all,
96109
sessionId: session.id,
97110
},
98-
output: (result.parts.findLast((x: any) => x.type === "text") as any)?.text ?? "",
111+
output,
99112
}
100113
},
101114
}

packages/opencode/src/tool/task.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ When NOT to use the Task tool:
1818
Usage notes:
1919
1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses
2020
2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.
21-
3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.
21+
3. Each agent invocation is stateless unless you provide a session_id. Your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.
2222
4. The agent's outputs should generally be trusted
2323
5. Clearly tell the agent whether you expect it to write code or just to do research (search, file reads, web fetches, etc.), since it is not aware of the user's intent
2424
6. If the agent description mentions that it should be used proactively, then you should try your best to use it without the user having to ask for it first. Use your judgement.

0 commit comments

Comments
 (0)