import type { CallOmoAgentArgs } from "./types" import type { PluginInput } from "@opencode-ai/plugin" import { log } from "../../shared" import { getAgentToolRestrictions } from "../../shared" import { createOrGetSession } from "./session-creator" import { waitForCompletion } from "./completion-poller" import { processMessages } from "./message-processor" type SessionWithPromptAsync = { promptAsync: (opts: { path: { id: string }; body: Record }) => Promise } type ExecuteSyncDeps = { createOrGetSession: typeof createOrGetSession waitForCompletion: typeof waitForCompletion processMessages: typeof processMessages } const defaultDeps: ExecuteSyncDeps = { createOrGetSession, waitForCompletion, processMessages, } export async function executeSync( args: CallOmoAgentArgs, toolContext: { sessionID: string messageID: string agent: string abort: AbortSignal metadata?: (input: { title?: string; metadata?: Record }) => void }, ctx: PluginInput, deps: ExecuteSyncDeps = defaultDeps ): Promise { const { sessionID } = await deps.createOrGetSession(args, toolContext, ctx) await toolContext.metadata?.({ title: args.description, metadata: { sessionId: sessionID }, }) log(`[call_omo_agent] Sending prompt to session ${sessionID}`) log(`[call_omo_agent] Prompt text:`, args.prompt.substring(0, 100)) try { await (ctx.client.session as unknown as SessionWithPromptAsync).promptAsync({ path: { id: sessionID }, body: { agent: args.subagent_type, tools: { ...getAgentToolRestrictions(args.subagent_type), task: false, question: false, }, parts: [{ type: "text", text: args.prompt }], }, }) } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error) log(`[call_omo_agent] Prompt error:`, errorMessage) if (errorMessage.includes("agent.name") || errorMessage.includes("undefined")) { return `Error: Agent "${args.subagent_type}" not found. Make sure the agent is registered in your opencode.json or provided by a plugin.\n\n\nsession_id: ${sessionID}\n` } return `Error: Failed to send prompt: ${errorMessage}\n\n\nsession_id: ${sessionID}\n` } await deps.waitForCompletion(sessionID, toolContext, ctx) const responseText = await deps.processMessages(sessionID, ctx) const output = responseText + "\n\n" + ["", `session_id: ${sessionID}`, ""].join("\n") return output }