From 9889ac0dd952baba4db811998e9d955e11b047a9 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Mon, 16 Feb 2026 15:54:29 +0900 Subject: [PATCH] fix: handle array-shaped SDK responses in getSdkMessages & dedup getMessageDir - getSdkMessages now handles both response.data and direct array responses from SDK - Consolidated getMessageDir: storage.ts now re-exports from shared opencode-message-dir.ts (with path traversal guards) --- .../empty-content-recovery-sdk.ts | 4 ++- src/tools/session-manager/storage.ts | 25 +++---------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/hooks/anthropic-context-window-limit-recovery/empty-content-recovery-sdk.ts b/src/hooks/anthropic-context-window-limit-recovery/empty-content-recovery-sdk.ts index 4d2b21af1..f95a0b51b 100644 --- a/src/hooks/anthropic-context-window-limit-recovery/empty-content-recovery-sdk.ts +++ b/src/hooks/anthropic-context-window-limit-recovery/empty-content-recovery-sdk.ts @@ -47,9 +47,11 @@ function messageHasContentFromSDK(message: SDKMessage): boolean { function getSdkMessages(response: unknown): SDKMessage[] { if (typeof response !== "object" || response === null) return [] + if (Array.isArray(response)) return response as SDKMessage[] const record = response as Record const data = record["data"] - return Array.isArray(data) ? (data as SDKMessage[]) : [] + if (Array.isArray(data)) return data as SDKMessage[] + return Array.isArray(record) ? (record as SDKMessage[]) : [] } async function findEmptyMessagesFromSDK(client: Client, sessionID: string): Promise { diff --git a/src/tools/session-manager/storage.ts b/src/tools/session-manager/storage.ts index de0226f1c..fff123938 100644 --- a/src/tools/session-manager/storage.ts +++ b/src/tools/session-manager/storage.ts @@ -1,9 +1,10 @@ -import { existsSync, readdirSync } from "node:fs" +import { existsSync } from "node:fs" import { readdir, readFile } from "node:fs/promises" import { join } from "node:path" import type { PluginInput } from "@opencode-ai/plugin" import { MESSAGE_STORAGE, PART_STORAGE, SESSION_STORAGE, TODO_DIR, TRANSCRIPT_DIR } from "./constants" import { isSqliteBackend } from "../../shared/opencode-storage-detection" +import { getMessageDir } from "../../shared/opencode-message-dir" import type { SessionMessage, SessionInfo, TodoItem, SessionMetadata } from "./types" export interface GetMainSessionsOptions { @@ -116,27 +117,7 @@ export async function getAllSessions(): Promise { return [...new Set(sessions)] } -export function getMessageDir(sessionID: string): string | null { - if (!existsSync(MESSAGE_STORAGE)) return null - - const directPath = join(MESSAGE_STORAGE, sessionID) - if (existsSync(directPath)) { - return directPath - } - - try { - for (const dir of readdirSync(MESSAGE_STORAGE)) { - const sessionPath = join(MESSAGE_STORAGE, dir, sessionID) - if (existsSync(sessionPath)) { - return sessionPath - } - } - } catch { - return null - } - - return null -} +export { getMessageDir } from "../../shared/opencode-message-dir" export async function sessionExists(sessionID: string): Promise { if (isSqliteBackend() && sdkClient) {