feat: wire session-recovery callers to async SDK/HTTP variants on SQLite
- recover-thinking-disabled-violation: isSqliteBackend() branch using stripThinkingPartsAsync() with SDK message enumeration - recover-thinking-block-order: isSqliteBackend() branch using prependThinkingPartAsync() with SDK orphan thinking detection - recover-empty-content-message: isSqliteBackend() branch delegating to extracted recover-empty-content-message-sdk.ts (200 LOC limit) - storage.ts barrel: add async variant exports for all SDK functions
This commit is contained in:
@@ -2,16 +2,23 @@ import type { createOpencodeClient } from "@opencode-ai/sdk"
|
||||
import type { MessageData } from "./types"
|
||||
import { extractMessageIndex } from "./detect-error-type"
|
||||
import { findMessageByIndexNeedingThinking, findMessagesWithOrphanThinking, prependThinkingPart } from "./storage"
|
||||
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||
import { prependThinkingPartAsync } from "./storage/thinking-prepend"
|
||||
import { THINKING_TYPES } from "./constants"
|
||||
|
||||
type Client = ReturnType<typeof createOpencodeClient>
|
||||
|
||||
export async function recoverThinkingBlockOrder(
|
||||
_client: Client,
|
||||
client: Client,
|
||||
sessionID: string,
|
||||
_failedAssistantMsg: MessageData,
|
||||
_directory: string,
|
||||
error: unknown
|
||||
): Promise<boolean> {
|
||||
if (isSqliteBackend()) {
|
||||
return recoverThinkingBlockOrderFromSDK(client, sessionID, error)
|
||||
}
|
||||
|
||||
const targetIndex = extractMessageIndex(error)
|
||||
if (targetIndex !== null) {
|
||||
const targetMessageID = findMessageByIndexNeedingThinking(sessionID, targetIndex)
|
||||
@@ -34,3 +41,86 @@ export async function recoverThinkingBlockOrder(
|
||||
|
||||
return anySuccess
|
||||
}
|
||||
|
||||
async function recoverThinkingBlockOrderFromSDK(
|
||||
client: Client,
|
||||
sessionID: string,
|
||||
error: unknown
|
||||
): Promise<boolean> {
|
||||
const targetIndex = extractMessageIndex(error)
|
||||
if (targetIndex !== null) {
|
||||
const targetMessageID = await findMessageByIndexNeedingThinkingFromSDK(client, sessionID, targetIndex)
|
||||
if (targetMessageID) {
|
||||
return prependThinkingPartAsync(client, sessionID, targetMessageID)
|
||||
}
|
||||
}
|
||||
|
||||
const orphanMessages = await findMessagesWithOrphanThinkingFromSDK(client, sessionID)
|
||||
if (orphanMessages.length === 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
let anySuccess = false
|
||||
for (const messageID of orphanMessages) {
|
||||
if (await prependThinkingPartAsync(client, sessionID, messageID)) {
|
||||
anySuccess = true
|
||||
}
|
||||
}
|
||||
|
||||
return anySuccess
|
||||
}
|
||||
|
||||
async function findMessagesWithOrphanThinkingFromSDK(
|
||||
client: Client,
|
||||
sessionID: string
|
||||
): Promise<string[]> {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
const messages = (response.data ?? []) as MessageData[]
|
||||
|
||||
const result: string[] = []
|
||||
for (const msg of messages) {
|
||||
if (msg.info?.role !== "assistant") continue
|
||||
if (!msg.info?.id) continue
|
||||
if (!msg.parts || msg.parts.length === 0) continue
|
||||
|
||||
const partsWithIds = msg.parts.filter(
|
||||
(part): part is { id: string; type: string } => typeof part.id === "string"
|
||||
)
|
||||
if (partsWithIds.length === 0) continue
|
||||
|
||||
const sortedParts = [...partsWithIds].sort((a, b) => a.id.localeCompare(b.id))
|
||||
const firstPart = sortedParts[0]
|
||||
if (!THINKING_TYPES.has(firstPart.type)) {
|
||||
result.push(msg.info.id)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
async function findMessageByIndexNeedingThinkingFromSDK(
|
||||
client: Client,
|
||||
sessionID: string,
|
||||
targetIndex: number
|
||||
): Promise<string | null> {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
const messages = (response.data ?? []) as MessageData[]
|
||||
|
||||
if (targetIndex < 0 || targetIndex >= messages.length) return null
|
||||
|
||||
const targetMessage = messages[targetIndex]
|
||||
if (targetMessage.info?.role !== "assistant") return null
|
||||
if (!targetMessage.info?.id) return null
|
||||
if (!targetMessage.parts || targetMessage.parts.length === 0) return null
|
||||
|
||||
const partsWithIds = targetMessage.parts.filter(
|
||||
(part): part is { id: string; type: string } => typeof part.id === "string"
|
||||
)
|
||||
if (partsWithIds.length === 0) return null
|
||||
|
||||
const sortedParts = [...partsWithIds].sort((a, b) => a.id.localeCompare(b.id))
|
||||
const firstPart = sortedParts[0]
|
||||
const firstIsThinking = THINKING_TYPES.has(firstPart.type)
|
||||
|
||||
return firstIsThinking ? null : targetMessage.info.id
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user