{"version":3,"file":"contextDetailApi.mjs","names":[],"sources":["../../../src/web/lib/contextDetailApi.ts"],"sourcesContent":["import { type ContextItemDetail, type ContextItemKind, itemDetailUrl } from '@agimon-ai/po-core/contextApi';\nimport { sessionApiPath } from '@agimon-ai/po-core/web';\nimport { browserTransport } from '@agimon-ai/po-web-security/browser';\n\n/**\n * The page's half of the runtime's context API.\n *\n * The panel prices every tool and skill from a projection small enough to\n * republish on every composition change. The prose and the schema behind a\n * figure are neither small nor wanted in bulk, so they are asked for one row at\n * a time, when a reader clicks the row.\n *\n * A session that has gone away, or one whose runtime predates this API, answers\n * 404. That is reported as text in the dialog rather than swallowed: a reader\n * who clicked deserves to know the session cannot say, instead of watching a\n * spinner forever.\n */\n\nconst UNREACHABLE = 'The session did not answer.';\n\nexport type FetchItemDetailResult = { ok: true; detail: ContextItemDetail } | { ok: false; error: string };\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n  return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\nasync function readBody(response: Response): Promise<unknown> {\n  const text = await response.text();\n  if (text === '') return undefined;\n  try {\n    return JSON.parse(text) as unknown;\n  } catch {\n    return undefined;\n  }\n}\n\nfunction errorOf(body: unknown, fallback: string): string {\n  return isRecord(body) && typeof body.error === 'string' && body.error !== '' ? body.error : fallback;\n}\n\nexport async function fetchContextItemDetail(\n  sessionId: string,\n  itemKind: ContextItemKind,\n  name: string,\n): Promise<FetchItemDetailResult> {\n  let response: Response;\n  try {\n    response = await browserTransport.fetch(itemDetailUrl(sessionApiPath(sessionId), itemKind, name));\n  } catch {\n    return { ok: false, error: UNREACHABLE };\n  }\n  const body = await readBody(response);\n  if (!response.ok) return { ok: false, error: errorOf(body, `The session answered ${String(response.status)}.`) };\n  if (!isRecord(body) || !isRecord(body.item)) return { ok: false, error: 'The session answered with no detail.' };\n  return { ok: true, detail: body.item as unknown as ContextItemDetail };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,MAAM,cAAc;AAIpB,SAAS,SAAS,OAAkD;CAClE,OAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC5E;AAEA,eAAe,SAAS,UAAsC;CAC5D,MAAM,OAAO,MAAM,SAAS,KAAK;CACjC,IAAI,SAAS,IAAI,OAAO,KAAA;CACxB,IAAI;EACF,OAAO,KAAK,MAAM,IAAI;CACxB,QAAQ;EACN;CACF;AACF;AAEA,SAAS,QAAQ,MAAe,UAA0B;CACxD,OAAO,SAAS,IAAI,KAAK,OAAO,KAAK,UAAU,YAAY,KAAK,UAAU,KAAK,KAAK,QAAQ;AAC9F;AAEA,eAAsB,uBACpB,WACA,UACA,MACgC;CAChC,IAAI;CACJ,IAAI;EACF,WAAW,MAAM,iBAAiB,MAAM,cAAc,eAAe,SAAS,GAAG,UAAU,IAAI,CAAC;CAClG,QAAQ;EACN,OAAO;GAAE,IAAI;GAAO,OAAO;EAAY;CACzC;CACA,MAAM,OAAO,MAAM,SAAS,QAAQ;CACpC,IAAI,CAAC,SAAS,IAAI,OAAO;EAAE,IAAI;EAAO,OAAO,QAAQ,MAAM,wBAAwB,OAAO,SAAS,MAAM,EAAE,EAAE;CAAE;CAC/G,IAAI,CAAC,SAAS,IAAI,KAAK,CAAC,SAAS,KAAK,IAAI,GAAG,OAAO;EAAE,IAAI;EAAO,OAAO;CAAuC;CAC/G,OAAO;EAAE,IAAI;EAAM,QAAQ,KAAK;CAAqC;AACvE"}