refactor: create normalizeSDKResponse helper and replace scattered patterns across 37 files
This commit is contained in:
@@ -2,6 +2,7 @@ import type { createOpencodeClient } from "@opencode-ai/sdk"
|
||||
import type { MessageData } from "./types"
|
||||
import { extractMessageIndex } from "./detect-error-type"
|
||||
import { META_TYPES, THINKING_TYPES } from "./constants"
|
||||
import { normalizeSDKResponse } from "../../shared"
|
||||
|
||||
type Client = ReturnType<typeof createOpencodeClient>
|
||||
|
||||
@@ -136,7 +137,7 @@ function sdkMessageHasContent(message: MessageData): boolean {
|
||||
async function readMessagesFromSDK(client: Client, sessionID: string): Promise<MessageData[]> {
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
return ((response.data ?? response) as unknown as MessageData[]) ?? []
|
||||
return normalizeSDKResponse(response, [] as MessageData[], { preferResponseOnMissingData: true })
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { findMessageByIndexNeedingThinking, findMessagesWithOrphanThinking, prep
|
||||
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||
import { prependThinkingPartAsync } from "./storage/thinking-prepend"
|
||||
import { THINKING_TYPES } from "./constants"
|
||||
import { normalizeSDKResponse } from "../../shared"
|
||||
|
||||
type Client = ReturnType<typeof createOpencodeClient>
|
||||
|
||||
@@ -77,7 +78,7 @@ async function findMessagesWithOrphanThinkingFromSDK(
|
||||
let messages: MessageData[]
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
messages = ((response.data ?? response) as unknown as MessageData[]) ?? []
|
||||
messages = normalizeSDKResponse(response, [] as MessageData[], { preferResponseOnMissingData: true })
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
@@ -111,7 +112,7 @@ async function findMessageByIndexNeedingThinkingFromSDK(
|
||||
let messages: MessageData[]
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
messages = ((response.data ?? response) as unknown as MessageData[]) ?? []
|
||||
messages = normalizeSDKResponse(response, [] as MessageData[], { preferResponseOnMissingData: true })
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||
import { stripThinkingPartsAsync } from "./storage/thinking-strip"
|
||||
import { THINKING_TYPES } from "./constants"
|
||||
import { log } from "../../shared/logger"
|
||||
import { normalizeSDKResponse } from "../../shared"
|
||||
|
||||
type Client = ReturnType<typeof createOpencodeClient>
|
||||
|
||||
@@ -38,7 +39,7 @@ async function recoverThinkingDisabledViolationFromSDK(
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
const messages = ((response.data ?? response) as unknown as MessageData[]) ?? []
|
||||
const messages = normalizeSDKResponse(response, [] as MessageData[], { preferResponseOnMissingData: true })
|
||||
|
||||
const messageIDsWithThinking: string[] = []
|
||||
for (const msg of messages) {
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { createOpencodeClient } from "@opencode-ai/sdk"
|
||||
import type { MessageData } from "./types"
|
||||
import { readParts } from "./storage"
|
||||
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||
import { normalizeSDKResponse } from "../../shared"
|
||||
|
||||
type Client = ReturnType<typeof createOpencodeClient>
|
||||
|
||||
@@ -28,7 +29,7 @@ async function readPartsFromSDKFallback(
|
||||
): Promise<MessagePart[]> {
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
const messages = ((response.data ?? response) as unknown as MessageData[]) ?? []
|
||||
const messages = normalizeSDKResponse(response, [] as MessageData[], { preferResponseOnMissingData: true })
|
||||
const target = messages.find((m) => m.info?.id === messageID)
|
||||
if (!target?.parts) return []
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { StoredPart, StoredTextPart, MessageData } from "../types"
|
||||
import { readMessages } from "./messages-reader"
|
||||
import { readParts } from "./parts-reader"
|
||||
import { log, isSqliteBackend, patchPart } from "../../../shared"
|
||||
import { normalizeSDKResponse } from "../../../shared"
|
||||
|
||||
type OpencodeClient = PluginInput["client"]
|
||||
|
||||
@@ -51,7 +52,7 @@ export async function replaceEmptyTextPartsAsync(
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
const messages = ((response.data ?? response) as unknown as MessageData[]) ?? []
|
||||
const messages = normalizeSDKResponse(response, [] as MessageData[], { preferResponseOnMissingData: true })
|
||||
|
||||
const targetMsg = messages.find((m) => m.info?.id === messageID)
|
||||
if (!targetMsg?.parts) return false
|
||||
@@ -101,7 +102,7 @@ export async function findMessagesWithEmptyTextPartsFromSDK(
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
const messages = ((response.data ?? response) as unknown as MessageData[]) ?? []
|
||||
const messages = normalizeSDKResponse(response, [] as MessageData[], { preferResponseOnMissingData: true })
|
||||
const result: string[] = []
|
||||
|
||||
for (const msg of messages) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { join } from "node:path"
|
||||
import type { PluginInput } from "@opencode-ai/plugin"
|
||||
import type { StoredMessageMeta } from "../types"
|
||||
import { getMessageDir } from "./message-dir"
|
||||
import { isSqliteBackend } from "../../../shared"
|
||||
import { isSqliteBackend, normalizeSDKResponse } from "../../../shared"
|
||||
import { isRecord } from "../../../shared/record-type-guard"
|
||||
|
||||
type OpencodeClient = PluginInput["client"]
|
||||
@@ -62,7 +62,9 @@ export async function readMessagesFromSDK(
|
||||
): Promise<StoredMessageMeta[]> {
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
const data: unknown = response.data ?? response
|
||||
const data = normalizeSDKResponse(response, [] as unknown[], {
|
||||
preferResponseOnMissingData: true,
|
||||
})
|
||||
if (!Array.isArray(data)) return []
|
||||
|
||||
const messages = data
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { MessageData } from "../types"
|
||||
import { readMessages } from "./messages-reader"
|
||||
import { readParts } from "./parts-reader"
|
||||
import { log, isSqliteBackend, patchPart } from "../../../shared"
|
||||
import { normalizeSDKResponse } from "../../../shared"
|
||||
|
||||
type OpencodeClient = PluginInput["client"]
|
||||
|
||||
@@ -74,7 +75,7 @@ async function findLastThinkingContentFromSDK(
|
||||
): Promise<string> {
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
const messages = ((response.data ?? response) as unknown as MessageData[]) ?? []
|
||||
const messages = normalizeSDKResponse(response, [] as MessageData[], { preferResponseOnMissingData: true })
|
||||
|
||||
const currentIndex = messages.findIndex((m) => m.info?.id === beforeMessageID)
|
||||
if (currentIndex === -1) return ""
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { PluginInput } from "@opencode-ai/plugin"
|
||||
import { PART_STORAGE, THINKING_TYPES } from "../constants"
|
||||
import type { StoredPart } from "../types"
|
||||
import { log, isSqliteBackend, deletePart } from "../../../shared"
|
||||
import { normalizeSDKResponse } from "../../../shared"
|
||||
|
||||
type OpencodeClient = PluginInput["client"]
|
||||
|
||||
@@ -42,7 +43,7 @@ export async function stripThinkingPartsAsync(
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response = await client.session.messages({ path: { id: sessionID } })
|
||||
const messages = ((response.data ?? response) as unknown as Array<{ parts?: Array<{ type: string; id: string }> }>) ?? []
|
||||
const messages = normalizeSDKResponse(response, [] as Array<{ parts?: Array<{ type: string; id: string }> }>, { preferResponseOnMissingData: true })
|
||||
|
||||
const targetMsg = messages.find((m) => {
|
||||
const info = (m as Record<string, unknown>)["info"] as Record<string, unknown> | undefined
|
||||
|
||||
Reference in New Issue
Block a user