|
| 1 | +import type { Part, SessionMessagesResponses } from "@opencode-ai/sdk"; |
| 2 | +import type { GraphitiEpisode } from "../types/index.ts"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Narrow type for a single SDK message entry as returned by |
| 6 | + * `session.messages()`. |
| 7 | + */ |
| 8 | +export type SdkMessage = { |
| 9 | + info: { role?: string; id?: string }; |
| 10 | + parts: Part[]; |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * Normalize an SDK response that may be wrapped in `{ data: … }` or returned |
| 15 | + * directly. Returns the inner value cast to `T`, or `undefined` when the |
| 16 | + * response is absent. |
| 17 | + * |
| 18 | + * This replaces the repeated `"data" in response ? (response as |
| 19 | + * { data?: … }).data : response` pattern found in session.ts and |
| 20 | + * context-limit.ts. |
| 21 | + */ |
| 22 | +export function unwrapSdkResponse<T>(response: unknown): T | undefined { |
| 23 | + if (response == null) return undefined; |
| 24 | + if (typeof response === "object" && "data" in (response as object)) { |
| 25 | + return (response as { data?: T }).data; |
| 26 | + } |
| 27 | + return response as T; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Extract the messages array from a raw `session.messages()` response. |
| 32 | + * Returns an empty array when the response is missing or malformed. |
| 33 | + */ |
| 34 | +export function extractSdkMessages( |
| 35 | + response: unknown, |
| 36 | +): SdkMessage[] { |
| 37 | + const payload = unwrapSdkResponse<SessionMessagesResponses[200]>(response); |
| 38 | + return Array.isArray(payload) ? (payload as SdkMessage[]) : []; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Extract the provider list from a raw `provider.list()` response. |
| 43 | + * Returns an empty array when the response is missing or malformed. |
| 44 | + */ |
| 45 | +export type SdkProvider = { |
| 46 | + id?: string; |
| 47 | + models?: SdkModel[]; |
| 48 | +}; |
| 49 | + |
| 50 | +export type SdkModel = { |
| 51 | + id?: string; |
| 52 | + limit?: { context?: number }; |
| 53 | +}; |
| 54 | + |
| 55 | +export function extractSdkProviders(response: unknown): SdkProvider[] { |
| 56 | + // provider.list() may return `{ providers: [...] }` directly (no data wrap). |
| 57 | + if (response != null && typeof response === "object") { |
| 58 | + const obj = response as Record<string, unknown>; |
| 59 | + if (Array.isArray(obj["providers"])) { |
| 60 | + return obj["providers"] as SdkProvider[]; |
| 61 | + } |
| 62 | + if ("data" in obj) { |
| 63 | + const data = obj["data"]; |
| 64 | + if (data != null && typeof data === "object") { |
| 65 | + const dataObj = data as Record<string, unknown>; |
| 66 | + if (Array.isArray(dataObj["providers"])) { |
| 67 | + return dataObj["providers"] as SdkProvider[]; |
| 68 | + } |
| 69 | + } |
| 70 | + if (Array.isArray(data)) return data as SdkProvider[]; |
| 71 | + } |
| 72 | + } |
| 73 | + return []; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Normalize a raw Graphiti episode object so that `sourceDescription` is |
| 78 | + * always the canonical field regardless of whether the payload used |
| 79 | + * camelCase (`sourceDescription`) or snake_case (`source_description`). |
| 80 | + * |
| 81 | + * Call this at the API boundary (e.g. inside `GraphitiClient.getEpisodes`) |
| 82 | + * so all downstream consumers only need to read `episode.sourceDescription`. |
| 83 | + */ |
| 84 | +export function normalizeEpisode( |
| 85 | + raw: GraphitiEpisode & { |
| 86 | + source_description?: string; |
| 87 | + }, |
| 88 | +): GraphitiEpisode { |
| 89 | + const { source_description, ...rest } = raw; |
| 90 | + return { |
| 91 | + ...rest, |
| 92 | + sourceDescription: rest.sourceDescription ?? source_description, |
| 93 | + }; |
| 94 | +} |
0 commit comments