fix(ralph-loop): use inherited fallback context and SDK TUI session selection

This commit is contained in:
YeonGyu-Kim 2026-02-21 02:42:59 +09:00
parent 6ad615958f
commit 7bb427078a
2 changed files with 34 additions and 27 deletions

View File

@ -30,9 +30,9 @@ export async function injectContinuationPrompt(
let agent: string | undefined let agent: string | undefined
let model: { providerID: string; modelID: string } | undefined let model: { providerID: string; modelID: string } | undefined
let tools: Record<string, boolean | "allow" | "deny" | "ask"> | undefined let tools: Record<string, boolean | "allow" | "deny" | "ask"> | undefined
const sourceSessionID = options.inheritFromSessionID ?? options.sessionID
try { try {
const sourceSessionID = options.inheritFromSessionID ?? options.sessionID
const messagesResp = await withTimeout( const messagesResp = await withTimeout(
ctx.client.session.messages({ ctx.client.session.messages({
path: { id: sourceSessionID }, path: { id: sourceSessionID },
@ -54,7 +54,7 @@ export async function injectContinuationPrompt(
} }
} }
} catch { } catch {
const messageDir = getMessageDir(options.sessionID) const messageDir = getMessageDir(sourceSessionID)
const currentMessage = messageDir ? findNearestMessageWithFields(messageDir) : null const currentMessage = messageDir ? findNearestMessageWithFields(messageDir) : null
agent = currentMessage?.agent agent = currentMessage?.agent
model = model =

View File

@ -1,6 +1,5 @@
import type { PluginInput } from "@opencode-ai/plugin" import type { PluginInput } from "@opencode-ai/plugin"
import { getServerBasicAuthHeader } from "../../shared/opencode-server-auth" import { log } from "../../shared"
import { getServerBaseUrl, log } from "../../shared"
export async function createIterationSession( export async function createIterationSession(
ctx: PluginInput, ctx: PluginInput,
@ -30,36 +29,44 @@ export async function selectSessionInTui(
client: PluginInput["client"], client: PluginInput["client"],
sessionID: string, sessionID: string,
): Promise<boolean> { ): Promise<boolean> {
const baseUrl = getServerBaseUrl(client) const selectSession = getSelectSessionApi(client)
const authorization = getServerBasicAuthHeader() if (!selectSession) {
if (!baseUrl || !authorization) {
return false return false
} }
const response = await fetch(`${baseUrl}/tui/select-session`, { try {
method: "POST", await selectSession({ body: { sessionID } })
headers: { return true
"Content-Type": "application/json", } catch (error: unknown) {
Authorization: authorization,
},
body: JSON.stringify({ sessionID }),
signal: AbortSignal.timeout(5000),
}).catch((error: unknown) => {
log("[ralph-loop] Failed to select session in TUI", { log("[ralph-loop] Failed to select session in TUI", {
sessionID, sessionID,
error: String(error), error: String(error),
}) })
return null
})
if (!response?.ok) {
log("[ralph-loop] TUI session select request failed", {
sessionID,
status: response?.status,
})
return false return false
} }
}
return true
type SelectSessionApi = (args: { body: { sessionID: string } }) => Promise<unknown>
function isRecord(value: unknown): value is Record<string, unknown> {
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
} }