diff --git a/src/hooks/ralph-loop/continuation-prompt-injector.ts b/src/hooks/ralph-loop/continuation-prompt-injector.ts index 759c75bd..46574be8 100644 --- a/src/hooks/ralph-loop/continuation-prompt-injector.ts +++ b/src/hooks/ralph-loop/continuation-prompt-injector.ts @@ -30,9 +30,9 @@ export async function injectContinuationPrompt( let agent: string | undefined let model: { providerID: string; modelID: string } | undefined let tools: Record | undefined + const sourceSessionID = options.inheritFromSessionID ?? options.sessionID try { - const sourceSessionID = options.inheritFromSessionID ?? options.sessionID const messagesResp = await withTimeout( ctx.client.session.messages({ path: { id: sourceSessionID }, @@ -54,7 +54,7 @@ export async function injectContinuationPrompt( } } } catch { - const messageDir = getMessageDir(options.sessionID) + const messageDir = getMessageDir(sourceSessionID) const currentMessage = messageDir ? findNearestMessageWithFields(messageDir) : null agent = currentMessage?.agent model = diff --git a/src/hooks/ralph-loop/session-reset-strategy.ts b/src/hooks/ralph-loop/session-reset-strategy.ts index b581100b..b4d80859 100644 --- a/src/hooks/ralph-loop/session-reset-strategy.ts +++ b/src/hooks/ralph-loop/session-reset-strategy.ts @@ -1,6 +1,5 @@ import type { PluginInput } from "@opencode-ai/plugin" -import { getServerBasicAuthHeader } from "../../shared/opencode-server-auth" -import { getServerBaseUrl, log } from "../../shared" +import { log } from "../../shared" export async function createIterationSession( ctx: PluginInput, @@ -30,36 +29,44 @@ export async function selectSessionInTui( client: PluginInput["client"], sessionID: string, ): Promise { - const baseUrl = getServerBaseUrl(client) - const authorization = getServerBasicAuthHeader() - - if (!baseUrl || !authorization) { + const selectSession = getSelectSessionApi(client) + if (!selectSession) { return false } - const response = await fetch(`${baseUrl}/tui/select-session`, { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: authorization, - }, - body: JSON.stringify({ sessionID }), - signal: AbortSignal.timeout(5000), - }).catch((error: unknown) => { + try { + await selectSession({ body: { sessionID } }) + return true + } catch (error: unknown) { log("[ralph-loop] Failed to select session in TUI", { sessionID, error: String(error), }) - return null - }) - - if (!response?.ok) { - log("[ralph-loop] TUI session select request failed", { - sessionID, - status: response?.status, - }) return false } - - return true +} + +type SelectSessionApi = (args: { body: { sessionID: string } }) => Promise + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null +} + +function getSelectSessionApi(client: unknown): SelectSessionApi | null { + if (!isRecord(client)) { + return null + } + + const clientRecord = client + const tuiValue = clientRecord.tui + if (!isRecord(tuiValue)) { + return null + } + + const selectSessionValue = tuiValue.selectSession + if (typeof selectSessionValue !== "function") { + return null + } + + return selectSessionValue as SelectSessionApi }