- Replace all response.data ?? [] with (response.data ?? response) pattern across 14 files to handle SDK array-shaped responses - Normalize SDK parts in parts-reader.ts by injecting sessionID/ messageID before validation (P1: SDK parts lack these fields) - Treat unknown part types as having content in recover-empty-content-message-sdk.ts to prevent false placeholder injection on image/file parts - Replace local isRecord with shared import in parts-reader.ts
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import { existsSync, readdirSync } from "node:fs"
|
|
import type { PluginInput } from "@opencode-ai/plugin"
|
|
import { getMessageDir } from "../../shared/opencode-message-dir"
|
|
|
|
export { getMessageDir }
|
|
|
|
type OpencodeClient = PluginInput["client"]
|
|
|
|
interface SDKMessage {
|
|
info: { id: string }
|
|
parts: unknown[]
|
|
}
|
|
|
|
export async function getMessageIdsFromSDK(
|
|
client: OpencodeClient,
|
|
sessionID: string
|
|
): Promise<string[]> {
|
|
try {
|
|
const response = await client.session.messages({ path: { id: sessionID } })
|
|
const messages = ((response.data ?? response) as unknown as SDKMessage[]) ?? []
|
|
return messages.map(msg => msg.info.id)
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
export function getMessageIds(sessionID: string): string[] {
|
|
const messageDir = getMessageDir(sessionID)
|
|
if (!messageDir || !existsSync(messageDir)) return []
|
|
|
|
const messageIds: string[] = []
|
|
for (const file of readdirSync(messageDir)) {
|
|
if (!file.endsWith(".json")) continue
|
|
const messageId = file.replace(".json", "")
|
|
messageIds.push(messageId)
|
|
}
|
|
|
|
return messageIds
|
|
}
|