Skip to content

Commit 463bf7a

Browse files
spoons-and-mirrorsrekram1-node
authored andcommitted
plugin(hook): add task tool execution hooks and command context tracking (#5642)
1 parent e4d7bb4 commit 463bf7a

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

packages/opencode/src/session/message-v2.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export namespace MessageV2 {
160160
prompt: z.string(),
161161
description: z.string(),
162162
agent: z.string(),
163+
command: z.string().optional(),
163164
})
164165
export type SubtaskPart = z.infer<typeof SubtaskPart>
165166

packages/opencode/src/session/prompt.ts

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -325,42 +325,60 @@ export namespace SessionPrompt {
325325
prompt: task.prompt,
326326
description: task.description,
327327
subagent_type: task.agent,
328+
command: task.command,
328329
},
329330
time: {
330331
start: Date.now(),
331332
},
332333
},
333334
})) as MessageV2.ToolPart
335+
const taskArgs = {
336+
prompt: task.prompt,
337+
description: task.description,
338+
subagent_type: task.agent,
339+
command: task.command,
340+
}
341+
await Plugin.trigger(
342+
"tool.execute.before",
343+
{
344+
tool: "task",
345+
sessionID,
346+
callID: part.id,
347+
},
348+
{ args: taskArgs },
349+
)
334350
let executionError: Error | undefined
335351
const result = await taskTool
336-
.execute(
337-
{
338-
prompt: task.prompt,
339-
description: task.description,
340-
subagent_type: task.agent,
341-
},
342-
{
343-
agent: task.agent,
344-
messageID: assistantMessage.id,
345-
sessionID: sessionID,
346-
abort,
347-
async metadata(input) {
348-
await Session.updatePart({
349-
...part,
350-
type: "tool",
351-
state: {
352-
...part.state,
353-
...input,
354-
},
355-
} satisfies MessageV2.ToolPart)
356-
},
352+
.execute(taskArgs, {
353+
agent: task.agent,
354+
messageID: assistantMessage.id,
355+
sessionID: sessionID,
356+
abort,
357+
async metadata(input) {
358+
await Session.updatePart({
359+
...part,
360+
type: "tool",
361+
state: {
362+
...part.state,
363+
...input,
364+
},
365+
} satisfies MessageV2.ToolPart)
357366
},
358-
)
367+
})
359368
.catch((error) => {
360369
executionError = error
361370
log.error("subtask execution failed", { error, agent: task.agent, description: task.description })
362371
return undefined
363372
})
373+
await Plugin.trigger(
374+
"tool.execute.after",
375+
{
376+
tool: "task",
377+
sessionID,
378+
callID: part.id,
379+
},
380+
result,
381+
)
364382
assistantMessage.finish = "tool-calls"
365383
assistantMessage.time.completed = Date.now()
366384
await Session.updateMessage(assistantMessage)
@@ -1323,6 +1341,7 @@ export namespace SessionPrompt {
13231341
type: "subtask" as const,
13241342
agent: agent.name,
13251343
description: command.description ?? "",
1344+
command: input.command,
13261345
// TODO: how can we make task tool accept a more complex input?
13271346
prompt: await resolvePromptParts(template).then((x) => x.find((y) => y.type === "text")?.text ?? ""),
13281347
},

packages/opencode/src/tool/task.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const TaskTool = Tool.define("task", async () => {
2626
prompt: z.string().describe("The task for the agent to perform"),
2727
subagent_type: z.string().describe("The type of specialized agent to use for this task"),
2828
session_id: z.string().describe("Existing Task session to continue").optional(),
29+
command: z.string().describe("The command that triggered this task").optional(),
2930
}),
3031
async execute(params, ctx) {
3132
const agent = await Agent.get(params.subagent_type)

packages/sdk/js/src/v2/gen/types.gen.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ export type Part =
420420
prompt: string
421421
description: string
422422
agent: string
423+
command?: string
423424
}
424425
| ReasoningPart
425426
| FilePart
@@ -1603,6 +1604,7 @@ export type SubtaskPartInput = {
16031604
prompt: string
16041605
description: string
16051606
agent: string
1607+
command?: string
16061608
}
16071609

16081610
export type Command = {

0 commit comments

Comments
 (0)