{"version":3,"file":"sessionSelectionApi.cjs","names":["browserTransport","pluginApiUrl","sessionApiAddress"],"sources":["../../../src/web/lib/sessionSelectionApi.ts"],"sourcesContent":["import { type ApiTransport, pluginApiUrl, sessionApiAddress } from '@agimon-ai/po-core/web';\nimport { browserTransport } from '@agimon-ai/po-web-security/browser';\n\nimport type { ContextProjection, ModeProjection } from '../../types/hub';\n\nexport interface SessionSelectionAnswer {\n  preset?: { preset: string; activeLayers: string[] };\n  domains?: { domains: string[] };\n  profile?: { profile: string | null };\n  modes?: ModeProjection;\n  context?: ContextProjection;\n}\n\nfunction record(value: unknown): value is Record<string, unknown> {\n  return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\nfunction strings(value: unknown): value is string[] {\n  return Array.isArray(value) && value.every((item) => typeof item === 'string');\n}\nfunction number(value: unknown): boolean {\n  return typeof value === 'number' && Number.isFinite(value) && value >= 0;\n}\nfunction optionalString(value: unknown): boolean {\n  return value === undefined || typeof value === 'string';\n}\nfunction selection(value: unknown): boolean {\n  return record(value) && typeof value.preset === 'string' && strings(value.domains) && optionalString(value.profile);\n}\nfunction isModeProjection(value: unknown): value is ModeProjection {\n  return (\n    record(value) &&\n    value.version === 1 &&\n    number(value.revision) &&\n    Array.isArray(value.modes) &&\n    value.modes.every(\n      (mode: unknown) =>\n        record(mode) &&\n        typeof mode.id === 'string' &&\n        typeof mode.label === 'string' &&\n        typeof mode.description === 'string' &&\n        typeof mode.order === 'number' &&\n        Number.isFinite(mode.order) &&\n        ['inactive', 'activating', 'active', 'deactivating'].includes(String(mode.activation)) &&\n        typeof mode.condition === 'string' &&\n        optionalString(mode.detail) &&\n        Array.isArray(mode.actions) &&\n        mode.actions.every(\n          (action: unknown) =>\n            record(action) &&\n            typeof action.id === 'string' &&\n            typeof action.label === 'string' &&\n            typeof action.description === 'string' &&\n            typeof action.enabled === 'boolean' &&\n            typeof action.needsInput === 'boolean' &&\n            optionalString(action.disabledReason),\n        ),\n    )\n  );\n}\nfunction context(value: unknown): value is ContextProjection {\n  return (\n    record(value) &&\n    value.version === 1 &&\n    number(value.revision) &&\n    number(value.totalTokens) &&\n    number(value.inactiveTokens) &&\n    typeof value.estimator === 'string' &&\n    (value.selection === undefined || selection(value.selection)) &&\n    (value.systemPrompt === undefined ||\n      (record(value.systemPrompt) &&\n        number(value.systemPrompt.tokens) &&\n        ['base', 'effective'].includes(String(value.systemPrompt.stage)))) &&\n    Array.isArray(value.groups) &&\n    value.groups.every(\n      (group: unknown) =>\n        record(group) &&\n        typeof group.id === 'string' &&\n        typeof group.label === 'string' &&\n        ['preset', 'mode', 'domain', 'shared', 'core'].includes(String(group.kind)) &&\n        number(group.tokens) &&\n        number(group.inactiveTokens) &&\n        Array.isArray(group.items) &&\n        group.items.every(\n          (item: unknown) =>\n            record(item) &&\n            typeof item.name === 'string' &&\n            ['tool', 'skill'].includes(String(item.itemKind)) &&\n            ['extension', 'mcp', 'plugin', 'core'].includes(String(item.source)) &&\n            typeof item.owner === 'string' &&\n            number(item.tokens) &&\n            typeof item.active === 'boolean',\n        ),\n    )\n  );\n}\n\n/** Unavailable or invalid parts are omitted so callers retain their last known values. */\nexport async function fetchSessionSelection(sessionId: string): Promise<SessionSelectionAnswer> {\n  const answer: SessionSelectionAnswer = {};\n  await Promise.all(\n    ['preset', 'domains', 'profile', 'modes', 'context'].map(async (base) => {\n      try {\n        const response = await browserTransport.fetch(pluginApiUrl(sessionApiAddress(sessionId), base, '/current'), {\n          cache: 'no-store',\n        });\n        if (!response.ok) return;\n        const body: unknown = await response.json();\n        if (!record(body)) return;\n        if (base === 'preset' && typeof body.preset === 'string' && strings(body.activeLayers))\n          answer.preset = { preset: body.preset, activeLayers: body.activeLayers };\n        if (base === 'domains' && strings(body.domains)) answer.domains = { domains: body.domains };\n        if (base === 'profile' && (body.profile === null || typeof body.profile === 'string'))\n          answer.profile = { profile: body.profile };\n        if (base === 'modes' && isModeProjection(body)) answer.modes = body;\n        if (base === 'context' && context(body)) answer.context = body;\n      } catch {\n        // A failed optional read leaves this part absent, preserving the last value.\n        return;\n      }\n    }),\n  );\n  return answer;\n}\n\n/**\n * Runs one mode action through the modes API and returns the projection it left; throws with the server's reason.\n * Native passes its own host transport; the browser default carries client metadata so Voice can wake this device.\n */\nexport async function postModeAction(\n  sessionId: string,\n  mode: string,\n  action: string,\n  request: ApiTransport = (input, init) => browserTransport.fetch(input, init),\n): Promise<ModeProjection> {\n  const response = await request(pluginApiUrl(sessionApiAddress(sessionId), 'modes', '/action'), {\n    method: 'POST',\n    headers: { 'content-type': 'application/json' },\n    body: JSON.stringify({ mode, action }),\n  });\n  const body: unknown = await response.json().catch(() => undefined);\n  if (!response.ok) {\n    const reason = record(body) && typeof body.error === 'string' ? body.error : `HTTP ${String(response.status)}`;\n    throw new Error(reason);\n  }\n  if (!isModeProjection(body)) throw new Error('The modes API answered with an unexpected shape.');\n  return body;\n}\n"],"mappings":";;;AAaA,SAAS,OAAO,OAAkD;CAChE,OAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC5E;AACA,SAAS,QAAQ,OAAmC;CAClD,OAAO,MAAM,QAAQ,KAAK,KAAK,MAAM,OAAO,SAAS,OAAO,SAAS,QAAQ;AAC/E;AACA,SAAS,OAAO,OAAyB;CACvC,OAAO,OAAO,UAAU,YAAY,OAAO,SAAS,KAAK,KAAK,SAAS;AACzE;AACA,SAAS,eAAe,OAAyB;CAC/C,OAAO,UAAU,KAAA,KAAa,OAAO,UAAU;AACjD;AACA,SAAS,UAAU,OAAyB;CAC1C,OAAO,OAAO,KAAK,KAAK,OAAO,MAAM,WAAW,YAAY,QAAQ,MAAM,OAAO,KAAK,eAAe,MAAM,OAAO;AACpH;AACA,SAAS,iBAAiB,OAAyC;CACjE,OACE,OAAO,KAAK,KACZ,MAAM,YAAY,KAClB,OAAO,MAAM,QAAQ,KACrB,MAAM,QAAQ,MAAM,KAAK,KACzB,MAAM,MAAM,OACT,SACC,OAAO,IAAI,KACX,OAAO,KA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