{"version":3,"file":"athena-urls.cjs","sources":["../../src/lib/athena-urls.ts"],"sourcesContent":["export type AthenaEnvironment = 'production' | 'staging';\n\nexport interface AthenaEnvironmentUrls {\n  apiUrl: string;\n  backendUrl: string;\n  appUrl: string;\n}\n\n/**\n * Bare origins: `apiUrl` is the Iris host fronting the deployment (its origin\n * derives the `/v2/threads` statewire mount) and `backendUrl` is the Agora\n * REST base. Neither carries a path — `deriveStatewireSyncUrl` and the REST\n * helpers append their own.\n */\nconst ATHENA_ENVIRONMENT_URLS: Record<AthenaEnvironment, AthenaEnvironmentUrls> = {\n  production: {\n    apiUrl: 'https://iris.prd.athenaintel.com',\n    backendUrl: 'https://api.athenaintel.com',\n    appUrl: 'https://app.athenaintel.com',\n  },\n  staging: {\n    apiUrl: 'https://iris.stg.athenaintel.com',\n    backendUrl: 'https://staging-api.athenaintel.com',\n    appUrl: 'https://staging-app.athenaintel.com',\n  },\n};\n\nexport const DEFAULT_ATHENA_ENVIRONMENT: AthenaEnvironment = 'production';\nexport const DEFAULT_APP_URL = ATHENA_ENVIRONMENT_URLS.production.appUrl;\n/** Production statewire sync endpoint — the same host the mobile app and\n *  Chrome extension ship as their production statewire base. Exported as a\n *  constant for explicit configuration; it is deliberately NOT used as an\n *  implicit fallback (see {@link resolveStatewireSyncUrl}). */\nexport const DEFAULT_STATEWIRE_SYNC_URL = 'https://iris.prd.athenaintel.com/v2/threads';\n\nconst STATEWIRE_SYNC_PATHNAME = '/v2/threads';\n\n/**\n * Derive the statewire sync endpoint (the `…/v2/threads` mount) from the Iris\n * host's origin: staging `https://iris.stg.athenaintel.com` maps to\n * `https://iris.stg.athenaintel.com/v2/threads`, and likewise for production\n * and workspace Iris hosts (a URL still carrying the retired `/api/chat` path\n * resolves the same way). Unparseable inputs return `null` so callers can\n * fall back explicitly.\n */\nexport function deriveStatewireSyncUrl({\n  apiUrl,\n}: {\n  apiUrl?: string | null;\n}): string | null {\n  if (!apiUrl) return null;\n\n  try {\n    const parsedUrl = new URL(apiUrl);\n    return `${parsedUrl.origin}${STATEWIRE_SYNC_PATHNAME}`;\n  } catch {\n    return null;\n  }\n}\n\n/**\n * Resolve the statewire sync endpoint for a provider configuration: an\n * explicit `statewireSyncUrl` wins, otherwise it is derived from the\n * effective `apiUrl` host.\n *\n * When neither yields a URL this throws instead of falling back to an Athena\n * default — mirroring `deriveAthenaApiUrl`'s null-for-unknown-hosts rule, so\n * a misconfigured custom deployment can never silently send its bearer token\n * or API key to Athena production.\n */\nexport function resolveStatewireSyncUrl({\n  statewireSyncUrl,\n  apiUrl,\n}: {\n  statewireSyncUrl?: string;\n  apiUrl?: string | null;\n}): string {\n  if (statewireSyncUrl) return statewireSyncUrl;\n\n  const derived = deriveStatewireSyncUrl({ apiUrl });\n  if (derived) return derived;\n\n  throw new Error(\n    `[AthenaSDK] transport 'statewire' could not derive a sync URL from apiUrl (${JSON.stringify(\n      apiUrl ?? null,\n    )}). Pass an explicit statewireSyncUrl (the …/v2/threads mount). Credentials are never sent to a default host.`,\n  );\n}\n\n/**\n * Refuse to attach a host-authenticated app to a guessed host. A host that\n * answered the auth bridge (a `/_athena/auth` mint, an `athena-config`\n * message) but minted no statewire mount predates the statewire-native\n * mint; without an explicit `statewireSyncUrl` or `apiUrl` the provider\n * would otherwise fall back to the environment preset and send that host's\n * credential to managed-cloud Athena. No host answering at all is not this\n * case: the preset is the documented default for a standalone app.\n */\nexport function assertHostMintedStatewireMount({\n  hostAnswered,\n  hostStatewireSyncUrl,\n  statewireSyncUrl,\n  apiUrl,\n}: {\n  hostAnswered: boolean;\n  hostStatewireSyncUrl: string | null;\n  statewireSyncUrl?: string;\n  apiUrl?: string;\n}): void {\n  if (!hostAnswered || hostStatewireSyncUrl || statewireSyncUrl || apiUrl) return;\n  throw new Error(\n    '[AthenaSDK] The host answered the auth bridge without a statewire sync mount (statewireSyncUrl): it predates the statewire-native mint. Update the host (Marathon >= 2026-08-29), or configure statewireSyncUrl / apiUrl explicitly. Credentials are never sent to a default host.',\n  );\n}\n\nconst MARATHON_APP_PORT = '8082';\nconst SPACES_PATHNAME = 'dashboard/spaces/';\n\nconst normalizeBaseUrl = (url: string): string => url.replace(/\\/+$/, '');\n\nconst createUrlWithFallback = (url: string, fallbackUrl: string): URL => {\n  try {\n    return new URL(`${normalizeBaseUrl(url)}/`);\n  } catch {\n    return new URL(`${normalizeBaseUrl(fallbackUrl)}/`);\n  }\n};\n\nconst getHostname = (value?: string | null): string | null => {\n  if (!value) return null;\n\n  try {\n    return new URL(value).hostname;\n  } catch {\n    return null;\n  }\n};\n\nconst deriveWorkspaceAppUrl = ({\n  sourceUrl,\n  sourcePort,\n}: {\n  sourceUrl: string;\n  sourcePort: '8000' | '8787';\n}): string | null => {\n  try {\n    const parsedUrl = new URL(sourceUrl);\n    const match = parsedUrl.hostname.match(\n      new RegExp(`^(?<prefix>.+)--${sourcePort}\\\\.(?<domain>app\\\\.athenaintel\\\\.com|staging-app\\\\.athenaintel\\\\.com)$`),\n    );\n\n    if (!match?.groups?.prefix || !match.groups.domain) {\n      return null;\n    }\n\n    return `${parsedUrl.protocol}//${match.groups.prefix}--${MARATHON_APP_PORT}.${match.groups.domain}`;\n  } catch {\n    return null;\n  }\n};\n\nconst deriveWorkspaceApiUrl = (backendUrl: string): string | null => {\n  try {\n    const parsedUrl = new URL(backendUrl);\n    const match = parsedUrl.hostname.match(\n      /^(?<prefix>.+)--8000\\.(?<domain>app\\.athenaintel\\.com|staging-app\\.athenaintel\\.com)$/,\n    );\n\n    if (!match?.groups?.prefix || !match.groups.domain) {\n      return null;\n    }\n\n    return `${parsedUrl.protocol}//${match.groups.prefix}--8787.${match.groups.domain}`;\n  } catch {\n    return null;\n  }\n};\n\nexport function getAthenaEnvironmentUrls({\n  environment = DEFAULT_ATHENA_ENVIRONMENT,\n}: {\n  environment?: AthenaEnvironment;\n} = {}): AthenaEnvironmentUrls {\n  return ATHENA_ENVIRONMENT_URLS[environment];\n}\n\n/**\n * Resolve the Iris origin that fronts a known Agora backend.\n *\n * Unknown custom backends deliberately return `null`: callers can preserve a\n * direct-Agora fallback instead of accidentally sending a custom environment's\n * bearer token and traffic through production Iris.\n */\nexport function deriveAthenaApiUrl({\n  backendUrl,\n}: {\n  backendUrl?: string | null;\n}): string | null {\n  if (!backendUrl) return null;\n\n  const workspaceApiUrl = deriveWorkspaceApiUrl(backendUrl);\n  if (workspaceApiUrl) return workspaceApiUrl;\n\n  try {\n    const parsedUrl = new URL(backendUrl);\n    if (parsedUrl.hostname === 'api.athenaintel.com') {\n      return ATHENA_ENVIRONMENT_URLS.production.apiUrl;\n    }\n    if (parsedUrl.hostname === 'staging-api.athenaintel.com') {\n      return ATHENA_ENVIRONMENT_URLS.staging.apiUrl;\n    }\n    if (parsedUrl.hostname === 'localhost' || parsedUrl.hostname === '127.0.0.1') {\n      parsedUrl.port = '8787';\n      return parsedUrl.origin;\n    }\n  } catch {\n    return null;\n  }\n\n  return null;\n}\n\nexport function deriveAthenaAppUrl({\n  apiUrl,\n  backendUrl,\n}: {\n  apiUrl?: string | null;\n  backendUrl?: string | null;\n}): string | null {\n  if (backendUrl) {\n    const workspaceAppUrl = deriveWorkspaceAppUrl({\n      sourceUrl: backendUrl,\n      sourcePort: '8000',\n    });\n    if (workspaceAppUrl) return workspaceAppUrl;\n\n    const backendHostname = getHostname(backendUrl);\n    if (backendHostname === 'api.athenaintel.com') {\n      return ATHENA_ENVIRONMENT_URLS.production.appUrl;\n    }\n    if (backendHostname === 'staging-api.athenaintel.com') {\n      return ATHENA_ENVIRONMENT_URLS.staging.appUrl;\n    }\n  }\n\n  if (apiUrl) {\n    const workspaceAppUrl = deriveWorkspaceAppUrl({\n      sourceUrl: apiUrl,\n      sourcePort: '8787',\n    });\n    if (workspaceAppUrl) return workspaceAppUrl;\n\n    const apiHostname = getHostname(apiUrl);\n    if (apiHostname === 'iris.prd.athenaintel.com') {\n      return ATHENA_ENVIRONMENT_URLS.production.appUrl;\n    }\n    if (apiHostname === 'iris.stg.athenaintel.com') {\n      return ATHENA_ENVIRONMENT_URLS.staging.appUrl;\n    }\n  }\n\n  return null;\n}\n\nexport function createAthenaSpacesUrl({\n  appUrl = DEFAULT_APP_URL,\n  assetIds,\n  sessionId,\n}: {\n  appUrl?: string;\n  assetIds?: string | string[];\n  sessionId?: string;\n}): string {\n  const url = new URL(SPACES_PATHNAME, createUrlWithFallback(appUrl, DEFAULT_APP_URL));\n\n  if (assetIds) {\n    const normalizedAssetIds = (Array.isArray(assetIds) ? assetIds : [assetIds])\n      .map((assetId) => assetId.trim())\n      .filter((assetId) => assetId.length > 0);\n\n    if (normalizedAssetIds.length > 0) {\n      url.searchParams.set('asset_ids', normalizedAssetIds.join(','));\n    }\n  }\n\n  if (sessionId) {\n    url.searchParams.set('session_id', sessionId);\n  }\n\n  return url.toString();\n}\n"],"names":[],"mappings":";;AAcA,MAAM,0BAA4E;AAAA,EAChF,YAAY;AAAA,IACV,QAAQ;AAAA,IACR,YAAY;AAAA,IACZ,QAAQ;AAAA,EAAA;AAAA,EAEV,SAAS;AAAA,IACP,QAAQ;AAAA,IACR,YAAY;AAAA,IACZ,QAAQ;AAAA,EAAA;AAEZ;AAEO,MAAM,6BAAgD;AACtD,MAAM,kBAAkB,wBAAwB,WAAW;AAK3D,MAAM,6BAA6B;AAE1C,MAAM,0BAA0B;AAUzB,SAAS,uBAAuB;AAAA,EACrC;AACF,GAEkB;AAChB,MAAI,CAAC,OAAQ,QAAO;AAEpB,MAAI;AACF,UAAM,YAAY,IAAI,IAAI,MAAM;AAChC,WAAO,GAAG,UAAU,MAAM,GAAG,uBAAuB;AAAA,EACtD,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAYO,SAAS,wBAAwB;AAAA,EACtC;AAAA,EACA;AACF,GAGW;AACT,MAAI,iBAAkB,QAAO;AAE7B,QAAM,UAAU,uBAAuB,EAAE,QAAQ;AACjD,MAAI,QAAS,QAAO;AAEpB,QAAM,IAAI;AAAA,IACR,8EAA8E,KAAK;AAAA,MACjF,UAAU;AAAA,I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