Skip to content

Commit 6a7be66

Browse files
committed
tweaks
1 parent f6b995c commit 6a7be66

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

packages/opencode/src/tool/task.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Identifier } from "../id/id"
88
import { Agent } from "../agent/agent"
99
import { SessionLock } from "../session/lock"
1010
import { SessionPrompt } from "../session/prompt"
11+
import { iife } from "@/util/iife"
1112

1213
export const TaskTool = Tool.define("task", async () => {
1314
const agents = await Agent.list().then((x) => x.filter((a) => a.mode !== "primary"))
@@ -28,12 +29,17 @@ export const TaskTool = Tool.define("task", async () => {
2829
async execute(params, ctx) {
2930
const agent = await Agent.get(params.subagent_type)
3031
if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`)
31-
const session = params.session_id
32-
? await Session.get(params.session_id)
33-
: await Session.create({
34-
parentID: ctx.sessionID,
35-
title: params.description + ` (@${agent.name} subagent)`,
36-
})
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+
})
42+
})
3743
const msg = await MessageV2.get({ sessionID: ctx.sessionID, messageID: ctx.messageID })
3844
if (msg.info.role !== "assistant") throw new Error("Not an assistant message")
3945

@@ -95,12 +101,11 @@ export const TaskTool = Tool.define("task", async () => {
95101
all = await Session.messages({ sessionID: session.id })
96102
all = all.filter((x) => x.info.role === "assistant")
97103
all = all.flatMap((msg) => msg.parts.filter((x: any) => x.type === "tool") as MessageV2.ToolPart[])
98-
const text = (result.parts.findLast((x: any) => x.type === "text") as any)?.text ?? ""
99-
const output = text
100-
? `${text}
104+
const text = result.parts.findLast((x) => x.type === "text")?.text ?? ""
105+
106+
const output =
107+
text + "\n\n" + ["<session_metadata>", `Session ID: ${session.id}`, "</session_metadata>"].join("\n")
101108

102-
[task-session:${session.id}]`
103-
: `[task-session:${session.id}]`
104109
return {
105110
title: params.description,
106111
metadata: {

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. Provide the returned [task-session:<id>] marker as session_id to continue; otherwise include every instruction the agent needs upfront.
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)