2026-02-08 15:01:42 +09:00
|
|
|
import { tool, type ToolDefinition } from "@opencode-ai/plugin"
|
2026-02-08 21:24:08 +09:00
|
|
|
import type { BackgroundTask } from "../../features/background-agent"
|
|
|
|
|
import { storeToolMetadata } from "../../features/tool-metadata-store"
|
2026-02-08 15:01:42 +09:00
|
|
|
import type { BackgroundOutputArgs } from "./types"
|
|
|
|
|
import type { BackgroundOutputClient, BackgroundOutputManager } from "./clients"
|
|
|
|
|
import { BACKGROUND_OUTPUT_DESCRIPTION } from "./constants"
|
|
|
|
|
import { delay } from "./delay"
|
|
|
|
|
import { formatFullSession } from "./full-session-format"
|
|
|
|
|
import { formatTaskResult } from "./task-result-format"
|
|
|
|
|
import { formatTaskStatus } from "./task-status-format"
|
|
|
|
|
|
2026-02-16 21:32:33 +09:00
|
|
|
import { getAgentDisplayName } from "../../shared/agent-display-names"
|
|
|
|
|
|
|
|
|
|
const SISYPHUS_JUNIOR_AGENT = getAgentDisplayName("sisyphus-junior")
|
2026-02-08 21:24:08 +09:00
|
|
|
|
|
|
|
|
type ToolContextWithMetadata = {
|
|
|
|
|
sessionID: string
|
|
|
|
|
metadata?: (input: { title?: string; metadata?: Record<string, unknown> }) => void
|
|
|
|
|
callID?: string
|
|
|
|
|
callId?: string
|
|
|
|
|
call_id?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveToolCallID(ctx: ToolContextWithMetadata): string | undefined {
|
|
|
|
|
if (typeof ctx.callID === "string" && ctx.callID.trim() !== "") return ctx.callID
|
|
|
|
|
if (typeof ctx.callId === "string" && ctx.callId.trim() !== "") return ctx.callId
|
|
|
|
|
if (typeof ctx.call_id === "string" && ctx.call_id.trim() !== "") return ctx.call_id
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatResolvedTitle(task: BackgroundTask): string {
|
|
|
|
|
const label = task.agent === SISYPHUS_JUNIOR_AGENT && task.category ? task.category : task.agent
|
|
|
|
|
return `${label} - ${task.description}`
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:55:11 +09:00
|
|
|
function isTaskActiveStatus(status: BackgroundTask["status"]): boolean {
|
|
|
|
|
return status === "pending" || status === "running"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appendTimeoutNote(output: string, timeoutMs: number): string {
|
|
|
|
|
return `${output}\n\n> **Timed out waiting** after ${timeoutMs}ms. Task is still running; showing latest available output.`
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 15:01:42 +09:00
|
|
|
export function createBackgroundOutput(manager: BackgroundOutputManager, client: BackgroundOutputClient): ToolDefinition {
|
|
|
|
|
return tool({
|
|
|
|
|
description: BACKGROUND_OUTPUT_DESCRIPTION,
|
|
|
|
|
args: {
|
|
|
|
|
task_id: tool.schema.string().describe("Task ID to get output from"),
|
|
|
|
|
block: tool.schema
|
|
|
|
|
.boolean()
|
|
|
|
|
.optional()
|
|
|
|
|
.describe(
|
|
|
|
|
"Wait for completion (default: false). System notifies when done, so blocking is rarely needed."
|
|
|
|
|
),
|
|
|
|
|
timeout: tool.schema.number().optional().describe("Max wait time in ms (default: 60000, max: 600000)"),
|
2026-02-21 16:40:15 +09:00
|
|
|
full_session: tool.schema.boolean().optional().describe("Return full session messages with filters (default: true)"),
|
2026-02-08 15:01:42 +09:00
|
|
|
include_thinking: tool.schema.boolean().optional().describe("Include thinking/reasoning parts in full_session output (default: false)"),
|
|
|
|
|
message_limit: tool.schema.number().optional().describe("Max messages to return (capped at 100)"),
|
|
|
|
|
since_message_id: tool.schema.string().optional().describe("Return messages after this message ID (exclusive)"),
|
|
|
|
|
include_tool_results: tool.schema.boolean().optional().describe("Include tool results in full_session output (default: false)"),
|
|
|
|
|
thinking_max_chars: tool.schema.number().optional().describe("Max characters for thinking content (default: 2000)"),
|
|
|
|
|
},
|
2026-02-08 21:24:08 +09:00
|
|
|
async execute(args: BackgroundOutputArgs, toolContext) {
|
2026-02-08 15:01:42 +09:00
|
|
|
try {
|
2026-02-08 21:24:08 +09:00
|
|
|
const ctx = toolContext as ToolContextWithMetadata
|
2026-02-08 15:01:42 +09:00
|
|
|
const task = manager.getTask(args.task_id)
|
|
|
|
|
if (!task) {
|
|
|
|
|
return `Task not found: ${args.task_id}`
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 21:24:08 +09:00
|
|
|
const meta = {
|
|
|
|
|
title: formatResolvedTitle(task),
|
|
|
|
|
metadata: {
|
|
|
|
|
task_id: task.id,
|
|
|
|
|
agent: task.agent,
|
|
|
|
|
category: task.category,
|
|
|
|
|
description: task.description,
|
|
|
|
|
sessionId: task.sessionID ?? "pending",
|
|
|
|
|
} as Record<string, unknown>,
|
|
|
|
|
}
|
|
|
|
|
ctx.metadata?.(meta)
|
|
|
|
|
|
|
|
|
|
const callID = resolveToolCallID(ctx)
|
|
|
|
|
if (callID) {
|
|
|
|
|
storeToolMetadata(ctx.sessionID, callID, meta)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 03:52:20 +09:00
|
|
|
const shouldBlock = args.block === true
|
|
|
|
|
const timeoutMs = Math.min(args.timeout ?? 60000, 600000)
|
2026-02-21 16:24:18 +09:00
|
|
|
const fullSession = args.full_session ?? true
|
2026-02-24 03:52:20 +09:00
|
|
|
|
|
|
|
|
let resolvedTask = task
|
|
|
|
|
|
2026-02-26 20:55:11 +09:00
|
|
|
let didTimeoutWhileActive = false
|
|
|
|
|
|
|
|
|
|
if (shouldBlock && isTaskActiveStatus(task.status)) {
|
2026-02-24 03:52:20 +09:00
|
|
|
const startTime = Date.now()
|
|
|
|
|
while (Date.now() - startTime < timeoutMs) {
|
|
|
|
|
await delay(1000)
|
|
|
|
|
|
|
|
|
|
const currentTask = manager.getTask(args.task_id)
|
|
|
|
|
if (!currentTask) {
|
|
|
|
|
return `Task was deleted: ${args.task_id}`
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:55:11 +09:00
|
|
|
resolvedTask = currentTask
|
|
|
|
|
|
|
|
|
|
if (!isTaskActiveStatus(currentTask.status)) {
|
2026-02-24 03:52:20 +09:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:55:11 +09:00
|
|
|
if (isTaskActiveStatus(resolvedTask.status)) {
|
|
|
|
|
const finalCheck = manager.getTask(args.task_id)
|
|
|
|
|
if (finalCheck) {
|
|
|
|
|
resolvedTask = finalCheck
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isTaskActiveStatus(resolvedTask.status)) {
|
|
|
|
|
didTimeoutWhileActive = true
|
2026-02-24 03:52:20 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:55:11 +09:00
|
|
|
const isActive = isTaskActiveStatus(resolvedTask.status)
|
2026-02-16 22:47:51 +09:00
|
|
|
const includeThinking = isActive || (args.include_thinking ?? false)
|
|
|
|
|
const includeToolResults = isActive || (args.include_tool_results ?? false)
|
2026-02-16 22:37:27 +09:00
|
|
|
|
|
|
|
|
if (fullSession) {
|
2026-02-26 20:55:11 +09:00
|
|
|
const output = await formatFullSession(resolvedTask, client, {
|
2026-02-16 22:37:27 +09:00
|
|
|
includeThinking,
|
2026-02-08 15:01:42 +09:00
|
|
|
messageLimit: args.message_limit,
|
|
|
|
|
sinceMessageId: args.since_message_id,
|
2026-02-16 22:37:27 +09:00
|
|
|
includeToolResults,
|
2026-02-08 15:01:42 +09:00
|
|
|
thinkingMaxChars: args.thinking_max_chars,
|
|
|
|
|
})
|
2026-02-26 20:55:11 +09:00
|
|
|
|
|
|
|
|
return didTimeoutWhileActive ? appendTimeoutNote(output, timeoutMs) : output
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 03:52:20 +09:00
|
|
|
if (resolvedTask.status === "completed") {
|
|
|
|
|
return await formatTaskResult(resolvedTask, client)
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 03:52:20 +09:00
|
|
|
if (resolvedTask.status === "error" || resolvedTask.status === "cancelled" || resolvedTask.status === "interrupt") {
|
|
|
|
|
return formatTaskStatus(resolvedTask)
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|
|
|
|
|
|
2026-02-26 20:55:11 +09:00
|
|
|
const statusOutput = formatTaskStatus(resolvedTask)
|
|
|
|
|
return didTimeoutWhileActive ? appendTimeoutNote(statusOutput, timeoutMs) : statusOutput
|
2026-02-08 15:01:42 +09:00
|
|
|
} catch (error) {
|
|
|
|
|
return `Error getting output: ${error instanceof Error ? error.message : String(error)}`
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|