2026-02-08 13:57:26 +09:00
|
|
|
import type { DelegateTaskArgs, ToolContextWithMetadata } from "./types"
|
|
|
|
|
import type { ExecutorContext, ParentContext } from "./executor-types"
|
2026-02-19 04:41:00 +02:00
|
|
|
import type { FallbackEntry } from "../../shared/model-requirements"
|
2026-02-08 13:57:26 +09:00
|
|
|
import { getTimingConfig } from "./timing"
|
|
|
|
|
import { storeToolMetadata } from "../../features/tool-metadata-store"
|
|
|
|
|
import { formatDetailedError } from "./error-formatting"
|
2026-02-14 14:30:30 +09:00
|
|
|
import { getSessionTools } from "../../shared/session-tools-store"
|
2026-02-03 12:18:52 +09:00
|
|
|
import { SessionCategoryRegistry } from "../../shared/session-category-registry"
|
2026-02-08 13:57:26 +09:00
|
|
|
|
|
|
|
|
export async function executeBackgroundTask(
|
|
|
|
|
args: DelegateTaskArgs,
|
|
|
|
|
ctx: ToolContextWithMetadata,
|
|
|
|
|
executorCtx: ExecutorContext,
|
|
|
|
|
parentContext: ParentContext,
|
|
|
|
|
agentToUse: string,
|
|
|
|
|
categoryModel: { providerID: string; modelID: string; variant?: string } | undefined,
|
2026-02-19 04:41:00 +02:00
|
|
|
systemContent: string | undefined,
|
|
|
|
|
fallbackChain?: FallbackEntry[],
|
2026-02-08 13:57:26 +09:00
|
|
|
): Promise<string> {
|
|
|
|
|
const { manager } = executorCtx
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const task = await manager.launch({
|
|
|
|
|
description: args.description,
|
|
|
|
|
prompt: args.prompt,
|
|
|
|
|
agent: agentToUse,
|
|
|
|
|
parentSessionID: parentContext.sessionID,
|
|
|
|
|
parentMessageID: parentContext.messageID,
|
|
|
|
|
parentModel: parentContext.model,
|
|
|
|
|
parentAgent: parentContext.agent,
|
2026-02-14 14:30:30 +09:00
|
|
|
parentTools: getSessionTools(parentContext.sessionID),
|
2026-02-08 13:57:26 +09:00
|
|
|
model: categoryModel,
|
2026-02-19 04:41:00 +02:00
|
|
|
fallbackChain,
|
2026-02-08 13:57:26 +09:00
|
|
|
skills: args.load_skills.length > 0 ? args.load_skills : undefined,
|
|
|
|
|
skillContent: systemContent,
|
|
|
|
|
category: args.category,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// OpenCode TUI's `Task` tool UI calculates toolcalls by looking up
|
|
|
|
|
// `props.metadata.sessionId` and then counting tool parts in that session.
|
|
|
|
|
// BackgroundManager.launch() returns immediately (pending) before the session exists,
|
|
|
|
|
// so we must wait briefly for the session to be created to set metadata correctly.
|
|
|
|
|
const timing = getTimingConfig()
|
|
|
|
|
const waitStart = Date.now()
|
|
|
|
|
let sessionId = task.sessionID
|
|
|
|
|
while (!sessionId && Date.now() - waitStart < timing.WAIT_FOR_SESSION_TIMEOUT_MS) {
|
|
|
|
|
if (ctx.abort?.aborted) {
|
|
|
|
|
return `Task aborted while waiting for session to start.\n\nTask ID: ${task.id}`
|
|
|
|
|
}
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, timing.WAIT_FOR_SESSION_INTERVAL_MS))
|
|
|
|
|
const updated = manager.getTask(task.id)
|
|
|
|
|
sessionId = updated?.sessionID
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 12:18:52 +09:00
|
|
|
if (args.category && sessionId) {
|
|
|
|
|
SessionCategoryRegistry.register(sessionId, args.category)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 10:36:20 +09:00
|
|
|
const metadata = {
|
|
|
|
|
prompt: args.prompt,
|
|
|
|
|
agent: task.agent,
|
|
|
|
|
category: args.category,
|
|
|
|
|
load_skills: args.load_skills,
|
|
|
|
|
description: args.description,
|
|
|
|
|
run_in_background: args.run_in_background,
|
|
|
|
|
command: args.command,
|
|
|
|
|
...(sessionId ? { sessionId } : {}),
|
|
|
|
|
...(categoryModel ? { model: { providerID: categoryModel.providerID, modelID: categoryModel.modelID } } : {}),
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 13:57:26 +09:00
|
|
|
const unstableMeta = {
|
|
|
|
|
title: args.description,
|
2026-03-05 10:36:20 +09:00
|
|
|
metadata,
|
2026-02-08 13:57:26 +09:00
|
|
|
}
|
|
|
|
|
await ctx.metadata?.(unstableMeta)
|
|
|
|
|
if (ctx.callID) {
|
|
|
|
|
storeToolMetadata(ctx.sessionID, ctx.callID, unstableMeta)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 10:36:20 +09:00
|
|
|
const taskMetadataBlock = sessionId
|
2026-03-05 10:43:45 +09:00
|
|
|
? `\n\n<task_metadata>\nsession_id: ${sessionId}\ntask_id: ${sessionId}\n</task_metadata>`
|
2026-03-05 10:36:20 +09:00
|
|
|
: ""
|
|
|
|
|
|
2026-02-08 13:57:26 +09:00
|
|
|
return `Background task launched.
|
|
|
|
|
|
2026-03-05 10:43:45 +09:00
|
|
|
Background Task ID: ${task.id}
|
2026-02-08 13:57:26 +09:00
|
|
|
Description: ${task.description}
|
|
|
|
|
Agent: ${task.agent}${args.category ? ` (category: ${args.category})` : ""}
|
|
|
|
|
Status: ${task.status}
|
|
|
|
|
|
2026-03-05 10:36:20 +09:00
|
|
|
System notifies on completion. Use \`background_output\` with task_id="${task.id}" to check.${taskMetadataBlock}`
|
2026-02-08 13:57:26 +09:00
|
|
|
} catch (error) {
|
|
|
|
|
return formatDetailedError(error, {
|
|
|
|
|
operation: "Launch background task",
|
|
|
|
|
args,
|
|
|
|
|
agent: agentToUse,
|
|
|
|
|
category: args.category,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|