2026-02-12 16:03:20 +01:00
|
|
|
import { tool, type ToolDefinition } from "@opencode-ai/plugin"
|
|
|
|
|
import { executeCouncil } from "../../agents/athena/council-orchestrator"
|
2026-02-18 23:31:55 +01:00
|
|
|
import type { CouncilConfig } from "../../agents/athena/types"
|
2026-02-13 11:19:25 +01:00
|
|
|
import type { BackgroundManager } from "../../features/background-agent"
|
2026-02-12 16:03:20 +01:00
|
|
|
import { createCouncilLauncher } from "./council-launcher"
|
2026-02-17 14:38:56 +01:00
|
|
|
import { waitForCouncilSessions } from "./session-waiter"
|
2026-02-18 23:31:55 +01:00
|
|
|
import type { AthenaCouncilToolArgs, AthenaCouncilToolContext } from "./types"
|
2026-02-18 19:26:20 +01:00
|
|
|
import { storeToolMetadata } from "../../features/tool-metadata-store"
|
2026-02-18 23:31:55 +01:00
|
|
|
import { log } from "../../shared/logger"
|
|
|
|
|
import {
|
|
|
|
|
isCouncilConfigured,
|
|
|
|
|
filterCouncilMembers,
|
|
|
|
|
buildSingleMemberSelectionError,
|
|
|
|
|
buildToolDescription,
|
|
|
|
|
formatCouncilLaunchFailure,
|
|
|
|
|
} from "./tool-helpers"
|
2026-02-13 10:52:57 +01:00
|
|
|
|
2026-02-12 16:03:20 +01:00
|
|
|
export function createAthenaCouncilTool(args: {
|
|
|
|
|
backgroundManager: BackgroundManager
|
|
|
|
|
councilConfig: CouncilConfig | undefined
|
|
|
|
|
}): ToolDefinition {
|
2026-02-18 19:26:20 +01:00
|
|
|
const { backgroundManager, councilConfig } = args
|
2026-02-13 10:52:57 +01:00
|
|
|
const description = buildToolDescription(councilConfig)
|
2026-02-12 16:03:20 +01:00
|
|
|
|
|
|
|
|
return tool({
|
2026-02-13 10:52:57 +01:00
|
|
|
description,
|
2026-02-12 16:03:20 +01:00
|
|
|
args: {
|
|
|
|
|
question: tool.schema.string().describe("The question to send to all council members"),
|
2026-02-12 18:21:33 +01:00
|
|
|
members: tool.schema
|
|
|
|
|
.array(tool.schema.string())
|
|
|
|
|
.optional()
|
2026-02-18 19:26:20 +01:00
|
|
|
.describe("Single-item list containing exactly one council member name or model ID."),
|
2026-02-12 16:03:20 +01:00
|
|
|
},
|
2026-02-18 23:31:55 +01:00
|
|
|
async execute(toolArgs: AthenaCouncilToolArgs, toolContext: AthenaCouncilToolContext) {
|
2026-02-12 16:03:20 +01:00
|
|
|
if (!isCouncilConfigured(councilConfig)) {
|
2026-02-18 20:56:07 +01:00
|
|
|
return "Athena council is not configured. Add council members to agents.athena.council.members in .opencode/oh-my-opencode.jsonc."
|
2026-02-12 16:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-12 18:21:33 +01:00
|
|
|
const filteredMembers = filterCouncilMembers(councilConfig.members, toolArgs.members)
|
|
|
|
|
if (filteredMembers.error) {
|
|
|
|
|
return filteredMembers.error
|
|
|
|
|
}
|
2026-02-18 19:26:20 +01:00
|
|
|
if (filteredMembers.members.length !== 1) {
|
|
|
|
|
return buildSingleMemberSelectionError(councilConfig.members)
|
|
|
|
|
}
|
2026-02-12 18:21:33 +01:00
|
|
|
|
2026-02-18 19:26:20 +01:00
|
|
|
const execution = await executeCouncil({
|
|
|
|
|
question: toolArgs.question,
|
|
|
|
|
council: { members: filteredMembers.members },
|
|
|
|
|
launcher: createCouncilLauncher(backgroundManager),
|
|
|
|
|
parentSessionID: toolContext.sessionID,
|
|
|
|
|
parentMessageID: toolContext.messageID,
|
|
|
|
|
parentAgent: toolContext.agent,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (execution.launched.length === 0) {
|
|
|
|
|
return formatCouncilLaunchFailure(execution.failures)
|
2026-02-12 16:46:23 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-18 19:26:20 +01:00
|
|
|
const launched = execution.launched[0]
|
|
|
|
|
const launchedMemberName = launched?.member.name ?? launched?.member.model
|
|
|
|
|
const launchedMemberModel = launched?.member.model ?? "unknown"
|
|
|
|
|
const launchedTaskId = launched?.taskId ?? "unknown"
|
|
|
|
|
|
2026-02-18 23:31:55 +01:00
|
|
|
log("[athena-council] Launching council member", { member: launchedMemberName, model: launchedMemberModel, taskId: launchedTaskId })
|
|
|
|
|
|
2026-02-18 20:56:07 +01:00
|
|
|
const waitResult = await waitForCouncilSessions(execution.launched, backgroundManager, toolContext.abort)
|
|
|
|
|
const launchedSession = waitResult.sessions.find((session) => session.taskId === launchedTaskId)
|
2026-02-18 19:26:20 +01:00
|
|
|
const sessionId = launchedSession?.sessionId ?? "pending"
|
|
|
|
|
|
2026-02-18 23:31:55 +01:00
|
|
|
let statusNote = ""
|
|
|
|
|
if (waitResult.timedOut) {
|
|
|
|
|
statusNote = "\nNote: Session creation timed out. The task is still running — use background_output to check status."
|
|
|
|
|
} else if (waitResult.aborted) {
|
|
|
|
|
statusNote = "\nNote: Session wait was aborted. The task may still be running."
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log("[athena-council] Session resolved", { taskId: launchedTaskId, sessionId })
|
|
|
|
|
|
|
|
|
|
if (toolContext.metadata) {
|
2026-02-18 19:26:20 +01:00
|
|
|
const memberMetadata = {
|
|
|
|
|
title: `Council: ${launchedMemberName}`,
|
|
|
|
|
metadata: {
|
|
|
|
|
sessionId,
|
|
|
|
|
agent: "council-member",
|
|
|
|
|
model: launchedMemberModel,
|
|
|
|
|
description: `Council member: ${launchedMemberName}`,
|
|
|
|
|
},
|
2026-02-17 14:38:56 +01:00
|
|
|
}
|
2026-02-18 20:56:07 +01:00
|
|
|
try {
|
2026-02-18 23:31:55 +01:00
|
|
|
await toolContext.metadata(memberMetadata)
|
2026-02-18 20:56:07 +01:00
|
|
|
|
2026-02-18 23:31:55 +01:00
|
|
|
if (toolContext.callID) {
|
|
|
|
|
storeToolMetadata(toolContext.sessionID, toolContext.callID, memberMetadata)
|
2026-02-18 20:56:07 +01:00
|
|
|
}
|
2026-02-18 23:31:55 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
log("[athena-council] Metadata storage failed (best-effort)", { error: error instanceof Error ? error.message : String(error) })
|
2026-02-18 19:26:20 +01:00
|
|
|
}
|
2026-02-12 16:46:23 +01:00
|
|
|
}
|
2026-02-18 19:26:20 +01:00
|
|
|
|
|
|
|
|
return `Council member launched in background.
|
|
|
|
|
|
|
|
|
|
Task ID: ${launchedTaskId}
|
|
|
|
|
Session ID: ${sessionId}
|
|
|
|
|
Member: ${launchedMemberName}
|
|
|
|
|
Model: ${launchedMemberModel}
|
2026-02-18 23:31:55 +01:00
|
|
|
Status: running${statusNote}
|
2026-02-18 19:26:20 +01:00
|
|
|
|
|
|
|
|
Use \`background_output\` with task_id="${launchedTaskId}" to collect this member's result.
|
|
|
|
|
- block=true: Wait for completion and return the result
|
|
|
|
|
- full_session=true: Include full session messages when needed
|
|
|
|
|
|
|
|
|
|
<task_metadata>
|
|
|
|
|
session_id: ${sessionId}
|
|
|
|
|
</task_metadata>`
|
2026-02-12 16:03:20 +01:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|