import type { PluginInput } from "@opencode-ai/plugin" import { promptWithModelSuggestionRetry } from "../../shared" import { dispatchInternalPrompt } from "../../shared/prompt-async-gate" type OpencodeClient = PluginInput["client"] type PromptAsyncArgs = Parameters[0] type PromptRetryClient = Parameters[0] type PromptRetryArgs = Parameters[1] type SessionMessagesArgs = Parameters[0] export function routeSessionPrompt(args: PromptAsyncArgs, directory: string): PromptAsyncArgs { return { ...args, query: { directory }, } } function routePromptRetry(args: PromptRetryArgs, directory: string): PromptRetryArgs { return { ...args, query: { directory }, } } export function promptAsyncInDirectory( client: OpencodeClient, args: PromptAsyncArgs, directory: string, ): Promise { const routedArgs = routeSessionPrompt(args, directory) const sessionID = routedArgs.path?.id if (!sessionID) { return Promise.reject(new Error("session id is required for routed promptAsync")) } return dispatchInternalPrompt({ mode: "async", client, sessionID, input: routedArgs, source: "background-agent-session-route", settleMs: 0, }).then((result) => { if (result.status === "failed") { throw result.error } if (result.status !== "dispatched") { throw new Error(`promptAsync skipped by gate: ${result.status}`) } return result.response }) } export function promptWithRetryInDirectory( client: PromptRetryClient, args: PromptRetryArgs, directory: string, ): Promise { return promptWithModelSuggestionRetry(client, routePromptRetry(args, directory)) } export function messagesInDirectory( client: OpencodeClient, args: SessionMessagesArgs, directory: string, ): Promise { return client.session.messages({ ...args, query: { directory }, }) }