/** * Server-only Athena chat bridge. * * @module chat/server * * This module wires `@xylex-group/chat-adapter-athena` — a **server-only** * package whose constructors throw when a browser `document` exists. It must * never be imported from a `"use client"` module, a shared component, or * anything reachable from the browser bundle. The browser chat UI lives at * `@xylex-group/athena-auth-ui/chat` and needs nothing from here. * * The adapter is an **optional** peer dependency: it is loaded through a * dynamic `import()` at call time, so consumers who only render the chat UI * never have to install it. Calling any helper here without it installed throws * an actionable error rather than failing at module resolution. * * ## Required environment * * | Variable | Required | Purpose | * | --- | --- | --- | * | `ATHENA_CHAT_URL` | yes | Base URL of the Athena chat gateway. | * | `ATHENA_CHAT_BOT_USER_ID` | yes | User id the bot posts as. | * | `ATHENA_CHAT_BOT_TOKEN` | yes | Bearer token for the bot identity. **Server only.** | * | `ATHENA_CHAT_WEBHOOK_SECRET` | only for webhooks | Shared secret verifying signed outbox deliveries. | * | `ATHENA_CHAT_WEBHOOK_KEY_ID` | no | Signing key id when rotating secrets. | * * None of these may be exposed with a `NEXT_PUBLIC_` prefix or passed into a * client component. */ /** Options accepted by {@link createAthenaChatBot}. */ export interface AthenaChatBotOptions { /** Bearer token for the bot identity. Never send this to the browser. */ botToken: string; /** User id the bot posts as. */ botUserId: string; /** Optional display name for the bot. */ botUserName?: string; /** Base URL of the Athena chat gateway. */ url: string; /** Enables inbound webhook verification. */ webhook?: AthenaChatWebhookOptions; } /** Signed-webhook verification options. */ export interface AthenaChatWebhookOptions { /** Maximum accepted signature age. Defaults to 300 seconds. */ maxAgeSeconds?: number; /** Key id when several signing secrets are in rotation. */ signingKeyId?: string; /** Shared secret used to verify inbound signatures. */ signingSecret: string | Uint8Array; } /** * Minimal structural view of the adapter. * * Declared locally rather than imported so this module type-checks with the * optional peer absent. */ export interface AthenaChatAdapter { readonly capabilities?: Record; [key: string]: unknown; } /** Handles a signed Athena outbox delivery and returns the gateway's response. */ export type AthenaChatWebhookHandler = (request: Request) => Promise; /** * Creates an Athena chat adapter for a bot identity. * * @example * ```ts * const adapter = await createAthenaChatBot({ * botToken: process.env.ATHENA_CHAT_BOT_TOKEN!, * botUserId: process.env.ATHENA_CHAT_BOT_USER_ID!, * url: process.env.ATHENA_CHAT_URL!, * }); * ``` */ export declare function createAthenaChatBot(options: AthenaChatBotOptions): Promise; /** * Reads bot configuration from the environment. * * @throws when a required variable is missing, so a misconfigured deployment * fails at boot instead of silently dropping messages. */ export declare function readAthenaChatBotEnv(env?: Record): AthenaChatBotOptions; /** * Builds a route handler for Athena's signed outbox webhook. * * Mount it at a single POST route, for example `app/api/chat/webhook/route.ts`: * * @example * ```ts * import { createAthenaChatWebhookHandler } from "@xylex-group/athena-auth-ui/chat/server"; * * export const POST = createAthenaChatWebhookHandler(); * ``` * * Athena POSTs a signed JSON body; the handler verifies the signature, rejects * replays outside `maxAgeSeconds`, and returns `202` on success, `401` on a bad * signature and `400` on a malformed body. */ export declare function createAthenaChatWebhookHandler(options?: AthenaChatWebhookOptions): AthenaChatWebhookHandler; /** Encodes a room id as an adapter thread id. */ export declare function encodeAthenaChatThreadId(roomId: string): Promise; /** Decodes an adapter thread id back to a room id. */ export declare function decodeAthenaChatThreadId(threadId: string): Promise<{ roomId: string; }>;