Add ctx.metadata() calls for session navigation UI in background/subagent tasks
Add metadata() calls to background_task and call_omo_agent tools so that OpenCode UI displays session navigation hints (ctrl+x + arrow keys) like the original Task tool does. This enhances UX by providing consistent session navigation UI for background and subagent tasks. 🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
@@ -37,6 +37,14 @@ function formatDuration(start: Date, end?: Date): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ToolContextWithMetadata = {
|
||||||
|
sessionID: string
|
||||||
|
messageID: string
|
||||||
|
agent: string
|
||||||
|
abort: AbortSignal
|
||||||
|
metadata?: (input: { title?: string; metadata?: Record<string, unknown> }) => void
|
||||||
|
}
|
||||||
|
|
||||||
export function createBackgroundTask(manager: BackgroundManager): ToolDefinition {
|
export function createBackgroundTask(manager: BackgroundManager): ToolDefinition {
|
||||||
return tool({
|
return tool({
|
||||||
description: BACKGROUND_TASK_DESCRIPTION,
|
description: BACKGROUND_TASK_DESCRIPTION,
|
||||||
@@ -46,12 +54,14 @@ export function createBackgroundTask(manager: BackgroundManager): ToolDefinition
|
|||||||
agent: tool.schema.string().describe("Agent type to use (any registered agent)"),
|
agent: tool.schema.string().describe("Agent type to use (any registered agent)"),
|
||||||
},
|
},
|
||||||
async execute(args: BackgroundTaskArgs, toolContext) {
|
async execute(args: BackgroundTaskArgs, toolContext) {
|
||||||
|
const ctx = toolContext as ToolContextWithMetadata
|
||||||
|
|
||||||
if (!args.agent || args.agent.trim() === "") {
|
if (!args.agent || args.agent.trim() === "") {
|
||||||
return `❌ Agent parameter is required. Please specify which agent to use (e.g., "explore", "librarian", "build", etc.)`
|
return `❌ Agent parameter is required. Please specify which agent to use (e.g., "explore", "librarian", "build", etc.)`
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const messageDir = getMessageDir(toolContext.sessionID)
|
const messageDir = getMessageDir(ctx.sessionID)
|
||||||
const prevMessage = messageDir ? findNearestMessageWithFields(messageDir) : null
|
const prevMessage = messageDir ? findNearestMessageWithFields(messageDir) : null
|
||||||
const parentModel = prevMessage?.model?.providerID && prevMessage?.model?.modelID
|
const parentModel = prevMessage?.model?.providerID && prevMessage?.model?.modelID
|
||||||
? { providerID: prevMessage.model.providerID, modelID: prevMessage.model.modelID }
|
? { providerID: prevMessage.model.providerID, modelID: prevMessage.model.modelID }
|
||||||
@@ -61,11 +71,16 @@ export function createBackgroundTask(manager: BackgroundManager): ToolDefinition
|
|||||||
description: args.description,
|
description: args.description,
|
||||||
prompt: args.prompt,
|
prompt: args.prompt,
|
||||||
agent: args.agent.trim(),
|
agent: args.agent.trim(),
|
||||||
parentSessionID: toolContext.sessionID,
|
parentSessionID: ctx.sessionID,
|
||||||
parentMessageID: toolContext.messageID,
|
parentMessageID: ctx.messageID,
|
||||||
parentModel,
|
parentModel,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ctx.metadata?.({
|
||||||
|
title: args.description,
|
||||||
|
metadata: { sessionId: task.sessionID },
|
||||||
|
})
|
||||||
|
|
||||||
return `Background task launched successfully.
|
return `Background task launched successfully.
|
||||||
|
|
||||||
Task ID: ${task.id}
|
Task ID: ${task.id}
|
||||||
|
|||||||
@@ -4,6 +4,14 @@ import type { CallOmoAgentArgs } from "./types"
|
|||||||
import type { BackgroundManager } from "../../features/background-agent"
|
import type { BackgroundManager } from "../../features/background-agent"
|
||||||
import { log } from "../../shared/logger"
|
import { log } from "../../shared/logger"
|
||||||
|
|
||||||
|
type ToolContextWithMetadata = {
|
||||||
|
sessionID: string
|
||||||
|
messageID: string
|
||||||
|
agent: string
|
||||||
|
abort: AbortSignal
|
||||||
|
metadata?: (input: { title?: string; metadata?: Record<string, unknown> }) => void
|
||||||
|
}
|
||||||
|
|
||||||
export function createCallOmoAgent(
|
export function createCallOmoAgent(
|
||||||
ctx: PluginInput,
|
ctx: PluginInput,
|
||||||
backgroundManager: BackgroundManager
|
backgroundManager: BackgroundManager
|
||||||
@@ -27,6 +35,7 @@ export function createCallOmoAgent(
|
|||||||
session_id: tool.schema.string().describe("Existing Task session to continue").optional(),
|
session_id: tool.schema.string().describe("Existing Task session to continue").optional(),
|
||||||
},
|
},
|
||||||
async execute(args: CallOmoAgentArgs, toolContext) {
|
async execute(args: CallOmoAgentArgs, toolContext) {
|
||||||
|
const toolCtx = toolContext as ToolContextWithMetadata
|
||||||
log(`[call_omo_agent] Starting with agent: ${args.subagent_type}, background: ${args.run_in_background}`)
|
log(`[call_omo_agent] Starting with agent: ${args.subagent_type}, background: ${args.run_in_background}`)
|
||||||
|
|
||||||
if (!ALLOWED_AGENTS.includes(args.subagent_type as typeof ALLOWED_AGENTS[number])) {
|
if (!ALLOWED_AGENTS.includes(args.subagent_type as typeof ALLOWED_AGENTS[number])) {
|
||||||
@@ -37,17 +46,17 @@ export function createCallOmoAgent(
|
|||||||
if (args.session_id) {
|
if (args.session_id) {
|
||||||
return `Error: session_id is not supported in background mode. Use run_in_background=false to continue an existing session.`
|
return `Error: session_id is not supported in background mode. Use run_in_background=false to continue an existing session.`
|
||||||
}
|
}
|
||||||
return await executeBackground(args, toolContext, backgroundManager)
|
return await executeBackground(args, toolCtx, backgroundManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
return await executeSync(args, toolContext, ctx)
|
return await executeSync(args, toolCtx, ctx)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeBackground(
|
async function executeBackground(
|
||||||
args: CallOmoAgentArgs,
|
args: CallOmoAgentArgs,
|
||||||
toolContext: { sessionID: string; messageID: string },
|
toolContext: ToolContextWithMetadata,
|
||||||
manager: BackgroundManager
|
manager: BackgroundManager
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
try {
|
try {
|
||||||
@@ -59,6 +68,11 @@ async function executeBackground(
|
|||||||
parentMessageID: toolContext.messageID,
|
parentMessageID: toolContext.messageID,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
toolContext.metadata?.({
|
||||||
|
title: args.description,
|
||||||
|
metadata: { sessionId: task.sessionID },
|
||||||
|
})
|
||||||
|
|
||||||
return `Background agent task launched successfully.
|
return `Background agent task launched successfully.
|
||||||
|
|
||||||
Task ID: ${task.id}
|
Task ID: ${task.id}
|
||||||
@@ -79,7 +93,7 @@ Use \`background_output\` tool with task_id="${task.id}" to check progress:
|
|||||||
|
|
||||||
async function executeSync(
|
async function executeSync(
|
||||||
args: CallOmoAgentArgs,
|
args: CallOmoAgentArgs,
|
||||||
toolContext: { sessionID: string },
|
toolContext: ToolContextWithMetadata,
|
||||||
ctx: PluginInput
|
ctx: PluginInput
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
let sessionID: string
|
let sessionID: string
|
||||||
@@ -112,6 +126,11 @@ async function executeSync(
|
|||||||
log(`[call_omo_agent] Created session: ${sessionID}`)
|
log(`[call_omo_agent] Created session: ${sessionID}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toolContext.metadata?.({
|
||||||
|
title: args.description,
|
||||||
|
metadata: { sessionId: sessionID },
|
||||||
|
})
|
||||||
|
|
||||||
log(`[call_omo_agent] Sending prompt to session ${sessionID}`)
|
log(`[call_omo_agent] Sending prompt to session ${sessionID}`)
|
||||||
log(`[call_omo_agent] Prompt text:`, args.prompt.substring(0, 100))
|
log(`[call_omo_agent] Prompt text:`, args.prompt.substring(0, 100))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user