@athenaintel/react 0.12.3 → 0.12.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chat/AthenaChatErrorBoundary.d.ts +27 -0
- package/dist/chat/StatewireApprovalCard.d.ts +19 -1
- package/dist/chat/StatewireClientToolBridge.d.ts +4 -0
- package/dist/chat/statewire-approval.d.ts +5 -33
- package/dist/collab/client.d.ts +12 -0
- package/dist/collab/react.d.ts +6 -0
- package/dist/collab.cjs +60 -3
- package/dist/collab.cjs.map +1 -1
- package/dist/collab.js +60 -3
- package/dist/collab.js.map +1 -1
- package/dist/diagnostics/errors.d.ts +2 -0
- package/dist/index.cjs +825 -293
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +849 -317
- package/dist/index.js.map +1 -1
- package/dist/lib/posthog/before-send.d.ts +9 -0
- package/dist/runtime/useAthenaStatewireRuntime.d.ts +5 -5
- package/dist/styles.css +1 -1
- package/package.json +1 -1
package/dist/collab.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collab.js","sources":["../src/collab/close-policy.ts","../src/collab/mint.ts","../src/collab/client.ts","../src/collab/react.ts","../src/collab/shape.ts"],"sourcesContent":["/**\n * Keryx WebSocket close-code policy.\n *\n * Keryx (@y/hub) encodes retry semantics in close codes: 4400–4499 are\n * permanent application errors — the client must stop reconnecting until the\n * app acts — while everything else is transient and left to the provider's\n * exponential backoff. Two permanent codes carry specific meaning:\n *\n * - 4401 \"permission revoked\": the token was rejected mid-session (access\n * revoked, permissions changed, or the token aged out server-side). The\n * right response is one fresh mint — which re-runs the server-side\n * permission check — and a reconnect only if the mint succeeds.\n * - 4404 \"document deleted\": permanent; the document is not coming back.\n */\n\nexport const WS_CLOSE_AUTH_REVOKED = 4401;\nexport const WS_CLOSE_DOC_DELETED = 4404;\n\nexport type CloseAction = 'remint' | 'stop' | 'retry';\n\nexport function classifyClose(code: number | undefined): CloseAction {\n if (code === undefined) return 'retry';\n if (code === WS_CLOSE_AUTH_REVOKED) return 'remint';\n if (code >= 4400 && code < 4500) return 'stop';\n return 'retry';\n}\n","/**\n * Collab-token mint client for Generic Doc assets.\n *\n * Calls `POST /api/v0/assets/{assetId}/collab-token` — the only public surface\n * that issues Keryx capability tokens, allowlisted server-side to the\n * `generic_doc` asset type and admin-only during the initial rollout. Tokens\n * are room-bound and short-lived; `connectGenericDoc` re-mints automatically\n * before expiry.\n */\n\nexport interface CollabAuth {\n /** Athena API base or any Athena backend URL (e.g. the AthenaProvider `backendUrl`). */\n backendUrl: string;\n /** Personal or sandbox API key. Used when no bearer token is provided. */\n apiKey?: string;\n /** Per-viewer bearer token. Takes precedence over the API key. */\n token?: string | null;\n}\n\nexport interface CollabTokenResponse {\n token: string;\n access_type: 'r' | 'rw';\n expires_at_ms: number;\n ws_url: string;\n rest_url: string;\n org: string;\n doc_id: string;\n branch: string;\n}\n\nexport class CollabTokenError extends Error {\n readonly status: number;\n\n constructor(status: number, message: string) {\n super(message);\n this.name = 'CollabTokenError';\n this.status = status;\n }\n}\n\nfunction getAuthHeaders(auth: CollabAuth): Record<string, string> {\n if (auth.token) {\n return { Authorization: `Bearer ${auth.token}` };\n }\n if (auth.apiKey) {\n return { 'X-API-KEY': auth.apiKey };\n }\n return {};\n}\n\n/**\n * Trim a nested Athena endpoint back to the API origin. Accepts the\n * AthenaProvider `backendUrl` (`…/api/assistant-ui`), the sandbox\n * `ATHENA_API_URL` convention (`…/api/v0`), or a bare origin.\n */\nexport function getAthenaApiBaseUrl(backendUrl: string): string {\n return backendUrl\n .replace(/\\/api\\/assistant-ui\\/?$/, '')\n .replace(/\\/api\\/v0\\/?$/, '')\n .replace(/\\/$/, '');\n}\n\nexport async function mintCollabToken(args: {\n auth: CollabAuth;\n assetId: string;\n access: 'view' | 'edit';\n signal?: AbortSignal;\n}): Promise<CollabTokenResponse> {\n const base = getAthenaApiBaseUrl(args.auth.backendUrl);\n const response = await fetch(\n `${base}/api/v0/assets/${encodeURIComponent(args.assetId)}/collab-token`,\n {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeaders(args.auth),\n },\n body: JSON.stringify({ access: args.access }),\n signal: args.signal,\n }\n );\n if (!response.ok) {\n let detail = '';\n try {\n const body: unknown = await response.json();\n if (body && typeof body === 'object' && 'detail' in body) {\n detail = String((body as { detail: unknown }).detail);\n }\n } catch {\n // non-JSON error body; status alone tells the story\n }\n throw new CollabTokenError(\n response.status,\n detail || `Collab token mint failed with status ${response.status}`\n );\n }\n return (await response.json()) as CollabTokenResponse;\n}\n","/**\n * Vanilla (framework-agnostic) live client for Generic Doc assets.\n *\n * `connectGenericDoc` owns the full connection lifecycle: mint a room-bound\n * token via the public API, open the Keryx WebSocket with the v14 provider\n * (`@y/websocket` + `@y/y`), re-mint before expiry, apply the close-code\n * policy (4401 → one fresh mint, other 44xx → permanent stop, everything\n * else → provider backoff), and expose presence from awareness. React apps\n * use `useGenericDoc` from `@athenaintel/react/collab` instead of calling\n * this directly.\n */\n\nimport { WebsocketProvider } from '@y/websocket';\nimport * as Y from '@y/y';\nimport { classifyClose } from './close-policy';\nimport {\n type CollabAuth,\n type CollabTokenResponse,\n CollabTokenError,\n mintCollabToken,\n} from './mint';\n\nexport type GenericDocStatus =\n | 'connecting'\n | 'connected'\n | 'disconnected'\n | 'closed'\n | 'error';\n\nexport interface PresenceEntry {\n clientId: number;\n state: Record<string, unknown>;\n}\n\nexport interface GenericDocHandle {\n /** The live shared document. Owned by the handle; destroyed with it. */\n readonly doc: Y.Doc;\n readonly status: GenericDocStatus;\n /** Granted access from the last successful mint. */\n readonly accessType: 'r' | 'rw' | null;\n /** Terminal error, when status is 'error' or 'closed'. */\n readonly error: Error | null;\n /** Monotonic change counter — bumps on every status/presence notification. */\n readonly version: number;\n getPresence: () => PresenceEntry[];\n /** Broadcast this client's presence state (requires a live connection). */\n setPresence: (state: Record<string, unknown> | null) => void;\n /**\n * Force an early token refresh. Session-preserving: the live connection\n * stays up until the fresh token is minted; a failed mint retries on a\n * short timer instead of dropping the session.\n */\n refreshNow: () => Promise<void>;\n onChange: (listener: () => void) => () => void;\n destroy: () => void;\n}\n\n/** Re-mint this long before token expiry (clamped to a floor for short TTLs). */\nconst REFRESH_HEADROOM_MS = 5 * 60 * 1000;\nconst MIN_REFRESH_DELAY_MS = 30 * 1000;\n\n/** Consecutive 4401→mint cycles allowed without a successful connect between. */\nconst MAX_CONSECUTIVE_REMINTS = 2;\n\nexport function connectGenericDoc(args: {\n assetId: string;\n auth: CollabAuth;\n access?: 'view' | 'edit';\n}): GenericDocHandle {\n const access = args.access ?? 'view';\n const doc = new Y.Doc();\n const listeners = new Set<() => void>();\n\n let provider: WebsocketProvider | null = null;\n let refreshTimer: ReturnType<typeof setTimeout> | null = null;\n let destroyed = false;\n let status: GenericDocStatus = 'connecting';\n let accessType: 'r' | 'rw' | null = null;\n let error: Error | null = null;\n let lastPresence: Record<string, unknown> | null = null;\n let version = 0;\n let consecutiveRemints = 0;\n\n const notify = () => {\n version += 1;\n for (const listener of listeners) listener();\n };\n\n const setStatus = (next: GenericDocStatus, err: Error | null = null) => {\n if (destroyed && next !== 'closed') return;\n status = next;\n error = err;\n notify();\n };\n\n const clearRefreshTimer = () => {\n if (refreshTimer !== null) {\n clearTimeout(refreshTimer);\n refreshTimer = null;\n }\n };\n\n const teardownProvider = () => {\n clearRefreshTimer();\n if (provider) {\n provider.destroy();\n provider = null;\n }\n };\n\n const scheduleRefresh = (grant: CollabTokenResponse) => {\n clearRefreshTimer();\n const delay = Math.max(\n grant.expires_at_ms - Date.now() - REFRESH_HEADROOM_MS,\n MIN_REFRESH_DELAY_MS\n );\n refreshTimer = setTimeout(() => {\n void connect({ scheduledRefresh: true });\n }, delay);\n };\n\n const connect = async (opts?: { scheduledRefresh?: boolean }): Promise<void> => {\n if (destroyed) return;\n const scheduledRefresh = opts?.scheduledRefresh === true;\n // A scheduled refresh mints FIRST and keeps the live session up: the old\n // token has ~5 minutes of headroom, so a transient mint failure retries\n // on a short timer instead of dropping a healthy connection.\n if (!scheduledRefresh) {\n teardownProvider();\n } else {\n clearRefreshTimer();\n }\n let grant: CollabTokenResponse;\n try {\n grant = await mintCollabToken({ auth: args.auth, assetId: args.assetId, access });\n } catch (err) {\n if (scheduledRefresh && !destroyed) {\n refreshTimer = setTimeout(() => {\n void connect({ scheduledRefresh: true });\n }, MIN_REFRESH_DELAY_MS);\n return;\n }\n // A denied mint is authoritative (revoked / not shared / not eligible);\n // anything else (network) is worth telling the caller about too — the\n // handle stays usable via a later explicit reconnect-by-recreate.\n setStatus('error', err instanceof Error ? err : new Error(String(err)));\n return;\n }\n if (destroyed) return;\n if (scheduledRefresh) {\n teardownProvider();\n }\n accessType = grant.access_type;\n\n const nextProvider = new WebsocketProvider(grant.ws_url, `${grant.org}/${grant.doc_id}`, doc, {\n params: {\n yauth: grant.token,\n branch: grant.branch,\n gc: 'true',\n },\n // BroadcastChannel would sync same-origin clients locally, bypassing the\n // server's permission enforcement (a read-only tab could write into an\n // editor tab's doc). Permissioned docs must round-trip the server.\n disableBc: true,\n });\n provider = nextProvider;\n\n nextProvider.on('status', (event: { status: 'connecting' | 'connected' | 'disconnected' }) => {\n if (provider !== nextProvider) return;\n if (event.status === 'connected') {\n consecutiveRemints = 0;\n }\n setStatus(event.status);\n if (event.status === 'connected' && lastPresence !== null) {\n nextProvider.awareness.setLocalState(lastPresence);\n }\n });\n\n nextProvider.on('connection-close', (event: CloseEvent | null) => {\n if (provider !== nextProvider) return;\n const action = classifyClose(event?.code);\n if (action === 'remint') {\n // Revoked mid-session: a fresh mint re-runs the server-side permission\n // check. If access is truly gone the mint 403s and we land in 'error'.\n // Bounded: repeated 4401s without an intervening successful connect\n // mean the server keeps rejecting freshly minted tokens — stop rather\n // than loop mint→connect→kick.\n consecutiveRemints += 1;\n teardownProvider();\n if (consecutiveRemints > MAX_CONSECUTIVE_REMINTS) {\n setStatus(\n 'closed',\n new Error('Access repeatedly revoked mid-session (4401); giving up.')\n );\n return;\n }\n void connect();\n } else if (action === 'stop') {\n teardownProvider();\n setStatus(\n 'closed',\n new Error(`Connection closed permanently (code ${event?.code ?? 'unknown'})`)\n );\n }\n // 'retry': the provider's exponential backoff handles it.\n });\n\n nextProvider.awareness.on('change', () => {\n if (provider !== nextProvider) return;\n notify();\n });\n\n scheduleRefresh(grant);\n };\n\n void connect();\n\n return {\n doc,\n get status() {\n return status;\n },\n get accessType() {\n return accessType;\n },\n get error() {\n return error;\n },\n get version() {\n return version;\n },\n getPresence: () => {\n if (!provider) return [];\n const entries: PresenceEntry[] = [];\n provider.awareness.getStates().forEach((state, clientId) => {\n entries.push({ clientId, state: state as Record<string, unknown> });\n });\n return entries;\n },\n setPresence: (state) => {\n lastPresence = state;\n provider?.awareness.setLocalState(state);\n },\n refreshNow: () => connect({ scheduledRefresh: true }),\n onChange: (listener) => {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n destroy: () => {\n if (destroyed) return;\n destroyed = true;\n teardownProvider();\n listeners.clear();\n doc.destroy();\n status = 'closed';\n },\n };\n}\n\nexport { CollabTokenError };\nexport type { CollabAuth, CollabTokenResponse };\n","/**\n * React bindings for Generic Doc live collaboration.\n *\n * `useGenericDoc(assetId)` opens (and owns) a `connectGenericDoc` handle,\n * resolving auth from the surrounding `AthenaProvider` (per-viewer bearer when\n * present, API key otherwise) and re-rendering on status/presence changes via\n * `useSyncExternalStore`.\n */\n\nimport { useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';\nimport { useAthenaConfig } from '../provider/AthenaContext';\nimport {\n type GenericDocHandle,\n type GenericDocStatus,\n type PresenceEntry,\n connectGenericDoc,\n} from './client';\nimport type * as Y from '@y/y';\n\nexport interface UseGenericDocResult {\n /** Null until the handle is created (first render effect). */\n doc: Y.Doc | null;\n status: GenericDocStatus;\n accessType: 'r' | 'rw' | null;\n error: Error | null;\n presence: PresenceEntry[];\n setPresence: (state: Record<string, unknown> | null) => void;\n}\n\nexport function useGenericDoc(\n assetId: string,\n options?: { access?: 'view' | 'edit' }\n): UseGenericDocResult {\n const config = useAthenaConfig();\n const access = options?.access ?? 'view';\n const [handle, setHandle] = useState<GenericDocHandle | null>(null);\n const handleRef = useRef<GenericDocHandle | null>(null);\n\n useEffect(() => {\n const next = connectGenericDoc({\n assetId,\n access,\n auth: {\n backendUrl: config.backendUrl,\n apiKey: config.apiKey,\n token: config.token,\n },\n });\n handleRef.current = next;\n setHandle(next);\n return () => {\n handleRef.current = null;\n next.destroy();\n };\n // Recreate when the target or credentials change; the handle re-mints on\n // its own schedule otherwise.\n }, [assetId, access, config.backendUrl, config.apiKey, config.token]);\n\n const subscribe = useMemo(() => {\n return (onStoreChange: () => void) => {\n if (!handle) return () => {};\n return handle.onChange(onStoreChange);\n };\n }, [handle]);\n\n const snapshot = useSyncExternalStore(\n subscribe,\n () => {\n if (!handle) return EMPTY_SNAPSHOT;\n return `${handle.status}|${handle.accessType ?? ''}|${handle.version}`;\n },\n () => EMPTY_SNAPSHOT\n );\n void snapshot;\n\n return {\n doc: handle?.doc ?? null,\n status: handle?.status ?? 'connecting',\n accessType: handle?.accessType ?? null,\n error: handle?.error ?? null,\n presence: handle?.getPresence() ?? [],\n setPresence: (state) => handleRef.current?.setPresence(state),\n };\n}\n\nconst EMPTY_SNAPSHOT = 'init||0';\n","/**\n * `defineDocShape` — a typed lens over a Generic Doc's root types.\n *\n * A shape is documentation + ergonomics, not a migration system: it names the\n * root keys an app expects and gives JSON-level reads (`toJSON`, `subscribe`)\n * that hide CRDT mechanics for the common case. The raw `Y.Doc` (yjs v14 /\n * `@y/y`) stays available on the handle for full delta-level power.\n */\n\nimport type * as Y from '@y/y';\n\nexport type DocShapeKind = 'map' | 'array' | 'text' | 'xml';\n\nexport interface BoundDocShape<S extends Record<string, DocShapeKind>> {\n /** The live root type for a declared key (v14 unified `YType`). */\n get: <K extends keyof S & string>(key: K) => ReturnType<Y.Doc['get']>;\n /** JSON snapshot of every declared root (v14 node shape: attributes + `children`). */\n toJSON: () => Record<keyof S & string, unknown>;\n /**\n * Subscribe to JSON snapshots. Fires once immediately, then after every\n * change to any declared root (batched per transaction flush).\n */\n subscribe: (listener: (json: Record<keyof S & string, unknown>) => void) => () => void;\n}\n\nexport function defineDocShape<S extends Record<string, DocShapeKind>>(shape: S) {\n const keys = Object.keys(shape) as Array<keyof S & string>;\n return {\n shape,\n bind(doc: Y.Doc): BoundDocShape<S> {\n const toJSON = () => {\n const out: Record<string, unknown> = {};\n for (const key of keys) {\n out[key] = doc.get(key).toJSON();\n }\n return out as Record<keyof S & string, unknown>;\n };\n return {\n get: (key) => doc.get(key),\n toJSON,\n subscribe: (listener) => {\n let scheduled = false;\n const emit = () => {\n scheduled = false;\n listener(toJSON());\n };\n const onDeepChange = () => {\n if (scheduled) return;\n scheduled = true;\n queueMicrotask(emit);\n };\n const roots = keys.map((key) => doc.get(key));\n for (const root of roots) {\n root.observeDeep(onDeepChange);\n }\n listener(toJSON());\n return () => {\n for (const root of roots) {\n root.unobserveDeep(onDeepChange);\n }\n };\n },\n };\n },\n };\n}\n"],"names":[],"mappings":";;;;;;;AAeO,MAAM,wBAAwB;AAC9B,MAAM,uBAAuB;AAI7B,SAAS,cAAc,MAAuC;AACnE,MAAI,SAAS,OAAW,QAAO;AAC/B,MAAI,SAAS,sBAAuB,QAAO;AAC3C,MAAI,QAAQ,QAAQ,OAAO,KAAM,QAAO;AACxC,SAAO;AACT;ACKO,MAAM,yBAAyB,MAAM;AAAA,EAG1C,YAAY,QAAgB,SAAiB;AAC3C,UAAM,OAAO;AAHN;AAIP,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AACF;AAEA,SAAS,eAAe,MAA0C;AAChE,MAAI,KAAK,OAAO;AACd,WAAO,EAAE,eAAe,UAAU,KAAK,KAAK,GAAA;AAAA,EAC9C;AACA,MAAI,KAAK,QAAQ;AACf,WAAO,EAAE,aAAa,KAAK,OAAA;AAAA,EAC7B;AACA,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ACrB;AACA,gBAAM,QAAQ,KAAK,IAAI,CAAC,QAAQ,IAAI,IAAI,GAAG,CAAC;AAC5C,qBAAW,QAAQ,OAAO;AACxB,iBAAK,YAAY,YAAY;AAAA,UAC/B;AACA,mBAAS,QAAQ;AACjB,iBAAO,MAAM;AACX,uBAAW,QAAQ,OAAO;AACxB,mBAAK,cAAc,YAAY;AAAA,YACjC;AAAA,UACF;AAAA,QACF;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"collab.js","sources":["../src/collab/close-policy.ts","../src/collab/mint.ts","../src/collab/client.ts","../src/collab/react.ts","../src/collab/shape.ts"],"sourcesContent":["/**\n * Keryx WebSocket close-code policy.\n *\n * Keryx (@y/hub) encodes retry semantics in close codes: 4400–4499 are\n * permanent application errors — the client must stop reconnecting until the\n * app acts — while everything else is transient and left to the provider's\n * exponential backoff. Two permanent codes carry specific meaning:\n *\n * - 4401 \"permission revoked\": the token was rejected mid-session (access\n * revoked, permissions changed, or the token aged out server-side). The\n * right response is one fresh mint — which re-runs the server-side\n * permission check — and a reconnect only if the mint succeeds.\n * - 4404 \"document deleted\": permanent; the document is not coming back.\n */\n\nexport const WS_CLOSE_AUTH_REVOKED = 4401;\nexport const WS_CLOSE_DOC_DELETED = 4404;\n\nexport type CloseAction = 'remint' | 'stop' | 'retry';\n\nexport function classifyClose(code: number | undefined): CloseAction {\n if (code === undefined) return 'retry';\n if (code === WS_CLOSE_AUTH_REVOKED) return 'remint';\n if (code >= 4400 && code < 4500) return 'stop';\n return 'retry';\n}\n","/**\n * Collab-token mint client for Generic Doc assets.\n *\n * Calls `POST /api/v0/assets/{assetId}/collab-token` — the only public surface\n * that issues Keryx capability tokens, allowlisted server-side to the\n * `generic_doc` asset type and admin-only during the initial rollout. Tokens\n * are room-bound and short-lived; `connectGenericDoc` re-mints automatically\n * before expiry.\n */\n\nexport interface CollabAuth {\n /** Athena API base or any Athena backend URL (e.g. the AthenaProvider `backendUrl`). */\n backendUrl: string;\n /** Personal or sandbox API key. Used when no bearer token is provided. */\n apiKey?: string;\n /** Per-viewer bearer token. Takes precedence over the API key. */\n token?: string | null;\n}\n\nexport interface CollabTokenResponse {\n token: string;\n access_type: 'r' | 'rw';\n expires_at_ms: number;\n ws_url: string;\n rest_url: string;\n org: string;\n doc_id: string;\n branch: string;\n}\n\nexport class CollabTokenError extends Error {\n readonly status: number;\n\n constructor(status: number, message: string) {\n super(message);\n this.name = 'CollabTokenError';\n this.status = status;\n }\n}\n\nfunction getAuthHeaders(auth: CollabAuth): Record<string, string> {\n if (auth.token) {\n return { Authorization: `Bearer ${auth.token}` };\n }\n if (auth.apiKey) {\n return { 'X-API-KEY': auth.apiKey };\n }\n return {};\n}\n\n/**\n * Trim a nested Athena endpoint back to the API origin. Accepts the\n * AthenaProvider `backendUrl` (`…/api/assistant-ui`), the sandbox\n * `ATHENA_API_URL` convention (`…/api/v0`), or a bare origin.\n */\nexport function getAthenaApiBaseUrl(backendUrl: string): string {\n return backendUrl\n .replace(/\\/api\\/assistant-ui\\/?$/, '')\n .replace(/\\/api\\/v0\\/?$/, '')\n .replace(/\\/$/, '');\n}\n\nexport async function mintCollabToken(args: {\n auth: CollabAuth;\n assetId: string;\n access: 'view' | 'edit';\n signal?: AbortSignal;\n}): Promise<CollabTokenResponse> {\n const base = getAthenaApiBaseUrl(args.auth.backendUrl);\n const response = await fetch(\n `${base}/api/v0/assets/${encodeURIComponent(args.assetId)}/collab-token`,\n {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeaders(args.auth),\n },\n body: JSON.stringify({ access: args.access }),\n signal: args.signal,\n }\n );\n if (!response.ok) {\n let detail = '';\n try {\n const body: unknown = await response.json();\n if (body && typeof body === 'object' && 'detail' in body) {\n detail = String((body as { detail: unknown }).detail);\n }\n } catch {\n // non-JSON error body; status alone tells the story\n }\n throw new CollabTokenError(\n response.status,\n detail || `Collab token mint failed with status ${response.status}`\n );\n }\n return (await response.json()) as CollabTokenResponse;\n}\n","/**\n * Vanilla (framework-agnostic) live client for Generic Doc assets.\n *\n * `connectGenericDoc` owns the full connection lifecycle: mint a room-bound\n * token via the public API, open the Keryx WebSocket with the v14 provider\n * (`@y/websocket` + `@y/y`), re-mint before expiry, apply the close-code\n * policy (4401 → one fresh mint, other 44xx → permanent stop, everything\n * else → provider backoff), and expose presence from awareness. React apps\n * use `useGenericDoc` from `@athenaintel/react/collab` instead of calling\n * this directly.\n */\n\nimport { WebsocketProvider } from '@y/websocket';\nimport * as Y from '@y/y';\nimport { classifyClose } from './close-policy';\nimport {\n type CollabAuth,\n type CollabTokenResponse,\n CollabTokenError,\n mintCollabToken,\n} from './mint';\n\nexport type GenericDocStatus =\n | 'connecting'\n | 'connected'\n | 'disconnected'\n | 'closed'\n | 'error';\n\nexport interface PresenceEntry {\n clientId: number;\n state: Record<string, unknown>;\n}\n\nexport interface GenericDocHandle {\n /** The live shared document. Owned by the handle; destroyed with it. */\n readonly doc: Y.Doc;\n readonly status: GenericDocStatus;\n /**\n * Whether the initial server sync has completed for the current connection.\n * `status === 'connected'` fires before the server state arrives, so a doc\n * read immediately after connect can still be empty — gate first reads on\n * this (or `waitForSync`).\n */\n readonly synced: boolean;\n /** Granted access from the last successful mint. */\n readonly accessType: 'r' | 'rw' | null;\n /** Terminal error, when status is 'error' or 'closed'. */\n readonly error: Error | null;\n /** Monotonic change counter — bumps on every status/presence notification. */\n readonly version: number;\n /**\n * Resolve once the initial server sync completes; reject on timeout, on a\n * terminal connection state, or if the handle is destroyed first.\n */\n waitForSync: (timeoutMs?: number) => Promise<void>;\n getPresence: () => PresenceEntry[];\n /** Broadcast this client's presence state (requires a live connection). */\n setPresence: (state: Record<string, unknown> | null) => void;\n /**\n * Force an early token refresh. Session-preserving: the live connection\n * stays up until the fresh token is minted; a failed mint retries on a\n * short timer instead of dropping the session.\n */\n refreshNow: () => Promise<void>;\n onChange: (listener: () => void) => () => void;\n destroy: () => void;\n}\n\n/** Re-mint this long before token expiry (clamped to a floor for short TTLs). */\nconst REFRESH_HEADROOM_MS = 5 * 60 * 1000;\nconst MIN_REFRESH_DELAY_MS = 30 * 1000;\n\n/** Consecutive 4401→mint cycles allowed without a successful connect between. */\nconst MAX_CONSECUTIVE_REMINTS = 2;\n\nexport function connectGenericDoc(args: {\n assetId: string;\n auth: CollabAuth;\n access?: 'view' | 'edit';\n}): GenericDocHandle {\n const access = args.access ?? 'view';\n const doc = new Y.Doc();\n const listeners = new Set<() => void>();\n\n let provider: WebsocketProvider | null = null;\n let refreshTimer: ReturnType<typeof setTimeout> | null = null;\n let destroyed = false;\n let status: GenericDocStatus = 'connecting';\n let synced = false;\n let accessType: 'r' | 'rw' | null = null;\n let error: Error | null = null;\n let lastPresence: Record<string, unknown> | null = null;\n let version = 0;\n let consecutiveRemints = 0;\n\n const notify = () => {\n version += 1;\n for (const listener of listeners) listener();\n };\n\n const setStatus = (next: GenericDocStatus, err: Error | null = null) => {\n if (destroyed && next !== 'closed') return;\n status = next;\n error = err;\n notify();\n };\n\n const clearRefreshTimer = () => {\n if (refreshTimer !== null) {\n clearTimeout(refreshTimer);\n refreshTimer = null;\n }\n };\n\n const teardownProvider = () => {\n clearRefreshTimer();\n // A future provider must complete its own initial sync; the doc keeps its\n // content either way. Subscribers must observe the flip immediately —\n // waiting for the next provider event would let them act on synced=true\n // across the replacement gap.\n if (synced) {\n synced = false;\n notify();\n }\n if (provider) {\n provider.destroy();\n provider = null;\n }\n };\n\n const scheduleRefresh = (grant: CollabTokenResponse) => {\n clearRefreshTimer();\n const delay = Math.max(\n grant.expires_at_ms - Date.now() - REFRESH_HEADROOM_MS,\n MIN_REFRESH_DELAY_MS\n );\n refreshTimer = setTimeout(() => {\n void connect({ scheduledRefresh: true });\n }, delay);\n };\n\n const connect = async (opts?: { scheduledRefresh?: boolean }): Promise<void> => {\n if (destroyed) return;\n const scheduledRefresh = opts?.scheduledRefresh === true;\n // A scheduled refresh mints FIRST and keeps the live session up: the old\n // token has ~5 minutes of headroom, so a transient mint failure retries\n // on a short timer instead of dropping a healthy connection.\n if (!scheduledRefresh) {\n teardownProvider();\n } else {\n clearRefreshTimer();\n }\n let grant: CollabTokenResponse;\n try {\n grant = await mintCollabToken({ auth: args.auth, assetId: args.assetId, access });\n } catch (err) {\n if (scheduledRefresh && !destroyed) {\n refreshTimer = setTimeout(() => {\n void connect({ scheduledRefresh: true });\n }, MIN_REFRESH_DELAY_MS);\n return;\n }\n // A denied mint is authoritative (revoked / not shared / not eligible);\n // anything else (network) is worth telling the caller about too — the\n // handle stays usable via a later explicit reconnect-by-recreate.\n setStatus('error', err instanceof Error ? err : new Error(String(err)));\n return;\n }\n if (destroyed) return;\n if (scheduledRefresh) {\n teardownProvider();\n }\n accessType = grant.access_type;\n\n const nextProvider = new WebsocketProvider(grant.ws_url, `${grant.org}/${grant.doc_id}`, doc, {\n params: {\n yauth: grant.token,\n branch: grant.branch,\n gc: 'true',\n },\n // BroadcastChannel would sync same-origin clients locally, bypassing the\n // server's permission enforcement (a read-only tab could write into an\n // editor tab's doc). Permissioned docs must round-trip the server.\n disableBc: true,\n });\n provider = nextProvider;\n\n nextProvider.on('status', (event: { status: 'connecting' | 'connected' | 'disconnected' }) => {\n if (provider !== nextProvider) return;\n if (event.status === 'connected') {\n consecutiveRemints = 0;\n }\n setStatus(event.status);\n if (event.status === 'connected' && lastPresence !== null) {\n nextProvider.awareness.setLocalState(lastPresence);\n }\n });\n\n nextProvider.on('sync', (state: boolean) => {\n if (provider !== nextProvider) return;\n if (synced !== state) {\n synced = state;\n notify();\n }\n });\n\n nextProvider.on('connection-close', (event: CloseEvent | null) => {\n if (provider !== nextProvider) return;\n const action = classifyClose(event?.code);\n if (action === 'remint') {\n // Revoked mid-session: a fresh mint re-runs the server-side permission\n // check. If access is truly gone the mint 403s and we land in 'error'.\n // Bounded: repeated 4401s without an intervening successful connect\n // mean the server keeps rejecting freshly minted tokens — stop rather\n // than loop mint→connect→kick.\n consecutiveRemints += 1;\n teardownProvider();\n if (consecutiveRemints > MAX_CONSECUTIVE_REMINTS) {\n setStatus(\n 'closed',\n new Error('Access repeatedly revoked mid-session (4401); giving up.')\n );\n return;\n }\n void connect();\n } else if (action === 'stop') {\n teardownProvider();\n setStatus(\n 'closed',\n new Error(`Connection closed permanently (code ${event?.code ?? 'unknown'})`)\n );\n }\n // 'retry': the provider's exponential backoff handles it.\n });\n\n nextProvider.awareness.on('change', () => {\n if (provider !== nextProvider) return;\n notify();\n });\n\n scheduleRefresh(grant);\n };\n\n void connect();\n\n const waitForSync = (timeoutMs = 15_000): Promise<void> =>\n new Promise<void>((resolve, reject) => {\n const terminal = (): Error | null => {\n if (destroyed) return new Error('Handle destroyed before initial sync');\n if (status === 'closed' || status === 'error') {\n return error ?? new Error(`Connection ${status} before initial sync`);\n }\n return null;\n };\n if (synced) {\n resolve();\n return;\n }\n const immediate = terminal();\n if (immediate) {\n reject(immediate);\n return;\n }\n const cleanup = () => {\n clearTimeout(timer);\n listeners.delete(listener);\n };\n const timer = setTimeout(() => {\n cleanup();\n reject(new Error(`Timed out after ${timeoutMs}ms waiting for initial sync`));\n }, timeoutMs);\n const listener = () => {\n if (synced) {\n cleanup();\n resolve();\n return;\n }\n const err = terminal();\n if (err) {\n cleanup();\n reject(err);\n }\n };\n listeners.add(listener);\n });\n\n return {\n doc,\n get status() {\n return status;\n },\n get synced() {\n return synced;\n },\n get accessType() {\n return accessType;\n },\n get error() {\n return error;\n },\n get version() {\n return version;\n },\n waitForSync,\n getPresence: () => {\n if (!provider) return [];\n const entries: PresenceEntry[] = [];\n provider.awareness.getStates().forEach((state, clientId) => {\n entries.push({ clientId, state: state as Record<string, unknown> });\n });\n return entries;\n },\n setPresence: (state) => {\n lastPresence = state;\n provider?.awareness.setLocalState(state);\n },\n refreshNow: () => connect({ scheduledRefresh: true }),\n onChange: (listener) => {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n destroy: () => {\n if (destroyed) return;\n destroyed = true;\n teardownProvider();\n status = 'closed';\n // Final notification so pending waitForSync callers reject instead of\n // hanging on listeners that are about to be dropped.\n for (const listener of [...listeners]) listener();\n listeners.clear();\n doc.destroy();\n },\n };\n}\n\nexport { CollabTokenError };\nexport type { CollabAuth, CollabTokenResponse };\n","/**\n * React bindings for Generic Doc live collaboration.\n *\n * `useGenericDoc(assetId)` opens (and owns) a `connectGenericDoc` handle,\n * resolving auth from the surrounding `AthenaProvider` (per-viewer bearer when\n * present, API key otherwise) and re-rendering on status/presence changes via\n * `useSyncExternalStore`.\n */\n\nimport { useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';\nimport { useAthenaConfig } from '../provider/AthenaContext';\nimport {\n type GenericDocHandle,\n type GenericDocStatus,\n type PresenceEntry,\n connectGenericDoc,\n} from './client';\nimport type * as Y from '@y/y';\n\nexport interface UseGenericDocResult {\n /** Null until the handle is created (first render effect). */\n doc: Y.Doc | null;\n status: GenericDocStatus;\n /**\n * Whether the initial server sync completed for the current connection —\n * `status === 'connected'` fires before the server state arrives, so gate\n * \"is this doc really empty?\" UI on this.\n */\n synced: boolean;\n accessType: 'r' | 'rw' | null;\n error: Error | null;\n presence: PresenceEntry[];\n setPresence: (state: Record<string, unknown> | null) => void;\n}\n\nexport function useGenericDoc(\n assetId: string,\n options?: { access?: 'view' | 'edit' }\n): UseGenericDocResult {\n const config = useAthenaConfig();\n const access = options?.access ?? 'view';\n const [handle, setHandle] = useState<GenericDocHandle | null>(null);\n const handleRef = useRef<GenericDocHandle | null>(null);\n\n useEffect(() => {\n const next = connectGenericDoc({\n assetId,\n access,\n auth: {\n backendUrl: config.backendUrl,\n apiKey: config.apiKey,\n token: config.token,\n },\n });\n handleRef.current = next;\n setHandle(next);\n return () => {\n handleRef.current = null;\n next.destroy();\n };\n // Recreate when the target or credentials change; the handle re-mints on\n // its own schedule otherwise.\n }, [assetId, access, config.backendUrl, config.apiKey, config.token]);\n\n const subscribe = useMemo(() => {\n return (onStoreChange: () => void) => {\n if (!handle) return () => {};\n return handle.onChange(onStoreChange);\n };\n }, [handle]);\n\n const snapshot = useSyncExternalStore(\n subscribe,\n () => {\n if (!handle) return EMPTY_SNAPSHOT;\n return `${handle.status}|${handle.synced}|${handle.accessType ?? ''}|${handle.version}`;\n },\n () => EMPTY_SNAPSHOT\n );\n void snapshot;\n\n return {\n doc: handle?.doc ?? null,\n status: handle?.status ?? 'connecting',\n synced: handle?.synced ?? false,\n accessType: handle?.accessType ?? null,\n error: handle?.error ?? null,\n presence: handle?.getPresence() ?? [],\n setPresence: (state) => handleRef.current?.setPresence(state),\n };\n}\n\nconst EMPTY_SNAPSHOT = 'init|false||0';\n","/**\n * `defineDocShape` — a typed lens over a Generic Doc's root types.\n *\n * A shape is documentation + ergonomics, not a migration system: it names the\n * root keys an app expects and gives JSON-level reads (`toJSON`, `subscribe`)\n * that hide CRDT mechanics for the common case. The raw `Y.Doc` (yjs v14 /\n * `@y/y`) stays available on the handle for full delta-level power.\n */\n\nimport type * as Y from '@y/y';\n\nexport type DocShapeKind = 'map' | 'array' | 'text' | 'xml';\n\nexport interface BoundDocShape<S extends Record<string, DocShapeKind>> {\n /** The live root type for a declared key (v14 unified `YType`). */\n get: <K extends keyof S & string>(key: K) => ReturnType<Y.Doc['get']>;\n /** JSON snapshot of every declared root (v14 node shape: attributes + `children`). */\n toJSON: () => Record<keyof S & string, unknown>;\n /**\n * Subscribe to JSON snapshots. Fires once immediately, then after every\n * change to any declared root (batched per transaction flush).\n */\n subscribe: (listener: (json: Record<keyof S & string, unknown>) => void) => () => void;\n}\n\nexport function defineDocShape<S extends Record<string, DocShapeKind>>(shape: S) {\n const keys = Object.keys(shape) as Array<keyof S & string>;\n return {\n shape,\n bind(doc: Y.Doc): BoundDocShape<S> {\n const toJSON = () => {\n const out: Record<string, unknown> = {};\n for (const key of keys) {\n out[key] = doc.get(key).toJSON();\n }\n return out as Record<keyof S & string, unknown>;\n };\n return {\n get: (key) => doc.get(key),\n toJSON,\n subscribe: (listener) => {\n let scheduled = false;\n const emit = () => {\n scheduled = false;\n listener(toJSON());\n };\n const onDeepChange = () => {\n if (scheduled) return;\n scheduled = true;\n queueMicrotask(emit);\n };\n const roots = keys.map((key) => doc.get(key));\n for (const root of roots) {\n root.observeDeep(onDeepChange);\n }\n listener(toJSON());\n return () => {\n for (const root of roots) {\n root.unobserveDeep(onDeepChange);\n }\n };\n },\n };\n },\n };\n}\n"],"names":[],"mappings":";;;;;;;AAeO,MAAM,wBAAwB;AAC9B,MAAM,uBAAuB;AAI7B,SAAS,cAAc,MAAuC;AACnE,MAAI,SAAS,OAAW,QAAO;AAC/B,MAAI,SAAS,sBAAuB,QAAO;AAC3C,MAAI,QAAQ,QAAQ,OAAO,KAAM,QAAO;AACxC,SAAO;AACT;ACKO,MAAM,yBAAyB,MAAM;AAAA,EAG1C,YAAY,QAAgB,SAAiB;AAC3C,UAAM,OAAO;AAHN;AAIP,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AACF;AAEA,SAAS,eAAe,MAA0C;AAChE,MAAI,KAAK,OAAO;AACd,WAAO,EAAE,eAAe,UAAU,KAAK,KAAK,GAAA;AAAA,EAC9C;AACA,MAAI,KAAK,QAAQ;AACf,WAAO,EAAE,aAAa,KAAK,OAAA;AAAA,EAC7B;AACA,SAAO,CAAA;AACT;AAOO,SAAS,oBAAoB,YAA4B;AAC9D,SAAO,WACJ,QAAQ,2BAA2B,EAAE,EACrC,QAAQ,iBAAiB,EAAE,EAC3B,QAAQ,OAAO,EAAE;AACtB;AAEA,eAAsB,gBAAgB,MAKL;AAC/B,QAAM,OAAO,oBAAoB,KAAK,KAAK,UAAU;AACrD,QAAM,WAAW,MAAM;AAAA,IACrB,GAAG,IAAI,kBAAkB,mBAAmB,KAAK,OAAO,CAAC;AAAA,IACzD;AAAA,MACE,QAAQ;AAAA,MACR,SAAS;AAAA,QACP,gBAAgB;AAAA,QAChB,GAAG,eAAe,KAAK,IAAI;AAAA,MAAA;AAAA,MAE7B,MAAM,KAAK,UAAU,EA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,iCAAQ,UAAS;AAAA,IACxB,WAAU,iCAAQ,kBAAiB,CAAA;AAAA,IACnC,aAAa,CAAC,UAAA;;AAAU,6BAAU,YAAV,mBAAmB,YAAY;AAAA;AAAA,EAAK;AAEhE;AAEA,MAAM,iBAAiB;ACnEhB,SAAS,eAAuD,OAAU;AAC/E,QAAM,OAAO,OAAO,KAAK,KAAK;AAC9B,SAAO;AAAA,IACL;AAAA,IACA,KAAK,KAA8B;AACjC,YAAM,SAAS,MAAM;AACnB,cAAM,MAA+B,CAAA;AACrC,mBAAW,OAAO,MAAM;AACtB,cAAI,GAAG,IAAI,IAAI,IAAI,GAAG,EAAE,OAAA;AAAA,QAC1B;AACA,eAAO;AAAA,MACT;AACA,aAAO;AAAA,QACL,KAAK,CAAC,QAAQ,IAAI,IAAI,GAAG;AAAA,QACzB;AAAA,QACA,WAAW,CAAC,aAAa;AACvB,cAAI,YAAY;AAChB,gBAAM,OAAO,MAAM;AACjB,wBAAY;AACZ,qBAAS,QAAQ;AAAA,UACnB;AACA,gBAAM,eAAe,MAAM;AACzB,gBAAI,UAAW;AACf,wBAAY;AACZ,2BAAe,IAAI;AAAA,UACrB;AACA,gBAAM,QAAQ,KAAK,IAAI,CAAC,QAAQ,IAAI,IAAI,GAAG,CAAC;AAC5C,qBAAW,QAAQ,OAAO;AACxB,iBAAK,YAAY,YAAY;AAAA,UAC/B;AACA,mBAAS,QAAQ;AACjB,iBAAO,MAAM;AACX,uBAAW,QAAQ,OAAO;AACxB,mBAAK,cAAc,YAAY;AAAA,YACjC;AAAA,UACF;AAAA,QACF;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAEJ;"}
|
|
@@ -29,6 +29,8 @@ export declare const ATHENA_SDK_ERROR_CODES: {
|
|
|
29
29
|
readonly stream_failed: 'stream_failed';
|
|
30
30
|
/** The selected collab agent / channel was refused by the backend. */
|
|
31
31
|
readonly collab_agent_rejected: 'collab_agent_rejected';
|
|
32
|
+
/** A React render error crashed the chat surface (caught by the SDK's error boundary). */
|
|
33
|
+
readonly chat_render_crash: 'chat_render_crash';
|
|
32
34
|
/** Anything else. */
|
|
33
35
|
readonly unknown: 'unknown';
|
|
34
36
|
};
|