@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
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Component, type ErrorInfo, type ReactNode } from 'react';
|
|
2
|
+
interface AthenaChatErrorBoundaryProps {
|
|
3
|
+
children: ReactNode;
|
|
4
|
+
}
|
|
5
|
+
interface AthenaChatErrorBoundaryState {
|
|
6
|
+
error: Error | null;
|
|
7
|
+
/** Bumped on retry so the children remount with fresh render state. */
|
|
8
|
+
retrySeq: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Error boundary around the SDK chat surface. A render crash anywhere in the
|
|
12
|
+
* thread tree (a malformed message part, a tool UI throwing, a pre-stream
|
|
13
|
+
* state read) otherwise unmounts the integrator's whole app subtree; this
|
|
14
|
+
* contains it to the chat pane, reports it through the SDK diagnostics bus
|
|
15
|
+
* (`sdk.error`, code `chat_render_crash` — forwarded to PostHog / `onError`
|
|
16
|
+
* when configured), and offers a retry that remounts the chat view. The
|
|
17
|
+
* statewire runtime lives above this boundary in `AthenaProvider`, so a retry
|
|
18
|
+
* re-renders against the still-connected transport.
|
|
19
|
+
*/
|
|
20
|
+
export declare class AthenaChatErrorBoundary extends Component<AthenaChatErrorBoundaryProps, AthenaChatErrorBoundaryState> {
|
|
21
|
+
constructor(props: AthenaChatErrorBoundaryProps);
|
|
22
|
+
static getDerivedStateFromError(error: Error): Partial<AthenaChatErrorBoundaryState>;
|
|
23
|
+
componentDidCatch(error: Error, errorInfo: ErrorInfo): void;
|
|
24
|
+
private handleRetry;
|
|
25
|
+
render(): ReactNode;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -1,2 +1,20 @@
|
|
|
1
|
+
import type { StatewireThread } from '@assistant-ui/react-statewire';
|
|
1
2
|
import type { FC } from 'react';
|
|
2
|
-
export
|
|
3
|
+
export interface StatewireApprovalCardProps {
|
|
4
|
+
/** Surface-styling overrides merged onto the default classes. */
|
|
5
|
+
classNames?: {
|
|
6
|
+
/** The approval card container. */
|
|
7
|
+
root?: string;
|
|
8
|
+
/** The "approval was cancelled" notice container. */
|
|
9
|
+
abandoned?: string;
|
|
10
|
+
};
|
|
11
|
+
/** Copy shown when an interrupt payload carries no message. */
|
|
12
|
+
fallbackMessage?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Which pending interrupt this card answers. Defaults to
|
|
15
|
+
* `isApprovalCardInterrupt` (client-tool parks excluded — keep it that way
|
|
16
|
+
* unless the surface handles those itself).
|
|
17
|
+
*/
|
|
18
|
+
interruptPredicate?: (request: StatewireThread.InputRequestState) => request is StatewireThread.CustomInputRequest;
|
|
19
|
+
}
|
|
20
|
+
export declare const StatewireApprovalCard: FC<StatewireApprovalCardProps>;
|
|
@@ -7,6 +7,10 @@ import type { StatewireClientTool } from './statewire-client-tools';
|
|
|
7
7
|
* input request. The bridge runs the matching local handler and resumes the
|
|
8
8
|
* run with a per-call result envelope, so a missing tool or a thrown handler
|
|
9
9
|
* still frees the run with an error the model can act on instead of hanging.
|
|
10
|
+
*
|
|
11
|
+
* The claim lifecycle, liveness guards, and wire-safety live in
|
|
12
|
+
* `@athenaintel/agent-runtime`'s `useStatewireClientToolBridge`; this wrapper
|
|
13
|
+
* only adapts the SDK's toolkit-derived tool shape and transport.
|
|
10
14
|
*/
|
|
11
15
|
export declare function StatewireClientToolBridge({ tools, threadId, }: {
|
|
12
16
|
tools: readonly StatewireClientTool[];
|
|
@@ -1,35 +1,7 @@
|
|
|
1
|
-
import type { StatewireThread } from '@assistant-ui/react-statewire';
|
|
2
|
-
/** Human-in-the-loop approval payload the agora deep-agent runtime parks a run
|
|
3
|
-
* on (shaped by `athena_deep_agent/hitl_bridge.py`). */
|
|
4
|
-
export interface HitlApprovalPayload {
|
|
5
|
-
source: string;
|
|
6
|
-
type: string;
|
|
7
|
-
message: string;
|
|
8
|
-
context?: {
|
|
9
|
-
tool_name?: string;
|
|
10
|
-
tool_id?: string;
|
|
11
|
-
tool_args?: Record<string, unknown>;
|
|
12
|
-
pending_action_count?: number;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
1
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
2
|
+
* Approval-interrupt primitives, re-exported from `@athenaintel/agent-runtime`
|
|
3
|
+
* — the shared headless home for every statewire chat surface. Kept as a
|
|
4
|
+
* module so existing `@athenaintel/react` imports (and the package's public
|
|
5
|
+
* exports) stay stable.
|
|
20
6
|
*/
|
|
21
|
-
export
|
|
22
|
-
/** A parked interrupt that has not been answered yet. */
|
|
23
|
-
export declare function isPendingInterrupt(request: StatewireThread.InputRequestState): request is StatewireThread.CustomInputRequest;
|
|
24
|
-
/**
|
|
25
|
-
* A pending interrupt the approval card may answer. Client-tool parks are
|
|
26
|
-
* excluded — the client-tool bridge owns those, and a generic Continue resume
|
|
27
|
-
* would corrupt the parked tool call. The source check is deliberately loose
|
|
28
|
-
* (any payload stamped with the client-tool source) so a skewed deploy can
|
|
29
|
-
* never route one here.
|
|
30
|
-
*/
|
|
31
|
-
export declare function isApprovalCardInterrupt(request: StatewireThread.InputRequestState): request is StatewireThread.CustomInputRequest;
|
|
32
|
-
/** Narrow an interrupt payload to the Athena HITL approval shape. */
|
|
33
|
-
export declare function asHitlApproval(value: unknown): HitlApprovalPayload | null;
|
|
34
|
-
/** Best-effort human-readable message from any interrupt payload. */
|
|
35
|
-
export declare function readInterruptMessage(value: unknown): string;
|
|
7
|
+
export { asHitlApproval, hasStatewireThreadExtras, type HitlApprovalPayload, isApprovalCardInterrupt, isPendingInterrupt, readInterruptMessage, } from '@athenaintel/agent-runtime';
|
package/dist/collab/client.d.ts
CHANGED
|
@@ -20,12 +20,24 @@ export interface GenericDocHandle {
|
|
|
20
20
|
/** The live shared document. Owned by the handle; destroyed with it. */
|
|
21
21
|
readonly doc: Y.Doc;
|
|
22
22
|
readonly status: GenericDocStatus;
|
|
23
|
+
/**
|
|
24
|
+
* Whether the initial server sync has completed for the current connection.
|
|
25
|
+
* `status === 'connected'` fires before the server state arrives, so a doc
|
|
26
|
+
* read immediately after connect can still be empty — gate first reads on
|
|
27
|
+
* this (or `waitForSync`).
|
|
28
|
+
*/
|
|
29
|
+
readonly synced: boolean;
|
|
23
30
|
/** Granted access from the last successful mint. */
|
|
24
31
|
readonly accessType: 'r' | 'rw' | null;
|
|
25
32
|
/** Terminal error, when status is 'error' or 'closed'. */
|
|
26
33
|
readonly error: Error | null;
|
|
27
34
|
/** Monotonic change counter — bumps on every status/presence notification. */
|
|
28
35
|
readonly version: number;
|
|
36
|
+
/**
|
|
37
|
+
* Resolve once the initial server sync completes; reject on timeout, on a
|
|
38
|
+
* terminal connection state, or if the handle is destroyed first.
|
|
39
|
+
*/
|
|
40
|
+
waitForSync: (timeoutMs?: number) => Promise<void>;
|
|
29
41
|
getPresence: () => PresenceEntry[];
|
|
30
42
|
/** Broadcast this client's presence state (requires a live connection). */
|
|
31
43
|
setPresence: (state: Record<string, unknown> | null) => void;
|
package/dist/collab/react.d.ts
CHANGED
|
@@ -12,6 +12,12 @@ export interface UseGenericDocResult {
|
|
|
12
12
|
/** Null until the handle is created (first render effect). */
|
|
13
13
|
doc: Y.Doc | null;
|
|
14
14
|
status: GenericDocStatus;
|
|
15
|
+
/**
|
|
16
|
+
* Whether the initial server sync completed for the current connection —
|
|
17
|
+
* `status === 'connected'` fires before the server state arrives, so gate
|
|
18
|
+
* "is this doc really empty?" UI on this.
|
|
19
|
+
*/
|
|
20
|
+
synced: boolean;
|
|
15
21
|
accessType: 'r' | 'rw' | null;
|
|
16
22
|
error: Error | null;
|
|
17
23
|
presence: PresenceEntry[];
|
package/dist/collab.cjs
CHANGED
|
@@ -93,6 +93,7 @@ function connectGenericDoc(args) {
|
|
|
93
93
|
let refreshTimer = null;
|
|
94
94
|
let destroyed = false;
|
|
95
95
|
let status = "connecting";
|
|
96
|
+
let synced = false;
|
|
96
97
|
let accessType = null;
|
|
97
98
|
let error = null;
|
|
98
99
|
let lastPresence = null;
|
|
@@ -116,6 +117,10 @@ function connectGenericDoc(args) {
|
|
|
116
117
|
};
|
|
117
118
|
const teardownProvider = () => {
|
|
118
119
|
clearRefreshTimer();
|
|
120
|
+
if (synced) {
|
|
121
|
+
synced = false;
|
|
122
|
+
notify();
|
|
123
|
+
}
|
|
119
124
|
if (provider) {
|
|
120
125
|
provider.destroy();
|
|
121
126
|
provider = null;
|
|
@@ -179,6 +184,13 @@ function connectGenericDoc(args) {
|
|
|
179
184
|
nextProvider.awareness.setLocalState(lastPresence);
|
|
180
185
|
}
|
|
181
186
|
});
|
|
187
|
+
nextProvider.on("sync", (state) => {
|
|
188
|
+
if (provider !== nextProvider) return;
|
|
189
|
+
if (synced !== state) {
|
|
190
|
+
synced = state;
|
|
191
|
+
notify();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
182
194
|
nextProvider.on("connection-close", (event) => {
|
|
183
195
|
if (provider !== nextProvider) return;
|
|
184
196
|
const action = classifyClose(event == null ? void 0 : event.code);
|
|
@@ -208,11 +220,53 @@ function connectGenericDoc(args) {
|
|
|
208
220
|
scheduleRefresh(grant);
|
|
209
221
|
};
|
|
210
222
|
void connect();
|
|
223
|
+
const waitForSync = (timeoutMs = 15e3) => new Promise((resolve, reject) => {
|
|
224
|
+
const terminal = () => {
|
|
225
|
+
if (destroyed) return new Error("Handle destroyed before initial sync");
|
|
226
|
+
if (status === "closed" || status === "error") {
|
|
227
|
+
return error ?? new Error(`Connection ${status} before initial sync`);
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
};
|
|
231
|
+
if (synced) {
|
|
232
|
+
resolve();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const immediate = terminal();
|
|
236
|
+
if (immediate) {
|
|
237
|
+
reject(immediate);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const cleanup = () => {
|
|
241
|
+
clearTimeout(timer);
|
|
242
|
+
listeners.delete(listener);
|
|
243
|
+
};
|
|
244
|
+
const timer = setTimeout(() => {
|
|
245
|
+
cleanup();
|
|
246
|
+
reject(new Error(`Timed out after ${timeoutMs}ms waiting for initial sync`));
|
|
247
|
+
}, timeoutMs);
|
|
248
|
+
const listener = () => {
|
|
249
|
+
if (synced) {
|
|
250
|
+
cleanup();
|
|
251
|
+
resolve();
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const err = terminal();
|
|
255
|
+
if (err) {
|
|
256
|
+
cleanup();
|
|
257
|
+
reject(err);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
listeners.add(listener);
|
|
261
|
+
});
|
|
211
262
|
return {
|
|
212
263
|
doc,
|
|
213
264
|
get status() {
|
|
214
265
|
return status;
|
|
215
266
|
},
|
|
267
|
+
get synced() {
|
|
268
|
+
return synced;
|
|
269
|
+
},
|
|
216
270
|
get accessType() {
|
|
217
271
|
return accessType;
|
|
218
272
|
},
|
|
@@ -222,6 +276,7 @@ function connectGenericDoc(args) {
|
|
|
222
276
|
get version() {
|
|
223
277
|
return version;
|
|
224
278
|
},
|
|
279
|
+
waitForSync,
|
|
225
280
|
getPresence: () => {
|
|
226
281
|
if (!provider) return [];
|
|
227
282
|
const entries = [];
|
|
@@ -245,9 +300,10 @@ function connectGenericDoc(args) {
|
|
|
245
300
|
if (destroyed) return;
|
|
246
301
|
destroyed = true;
|
|
247
302
|
teardownProvider();
|
|
303
|
+
status = "closed";
|
|
304
|
+
for (const listener of [...listeners]) listener();
|
|
248
305
|
listeners.clear();
|
|
249
306
|
doc.destroy();
|
|
250
|
-
status = "closed";
|
|
251
307
|
}
|
|
252
308
|
};
|
|
253
309
|
}
|
|
@@ -284,13 +340,14 @@ function useGenericDoc(assetId, options) {
|
|
|
284
340
|
subscribe,
|
|
285
341
|
() => {
|
|
286
342
|
if (!handle) return EMPTY_SNAPSHOT;
|
|
287
|
-
return `${handle.status}|${handle.accessType ?? ""}|${handle.version}`;
|
|
343
|
+
return `${handle.status}|${handle.synced}|${handle.accessType ?? ""}|${handle.version}`;
|
|
288
344
|
},
|
|
289
345
|
() => EMPTY_SNAPSHOT
|
|
290
346
|
);
|
|
291
347
|
return {
|
|
292
348
|
doc: (handle == null ? void 0 : handle.doc) ?? null,
|
|
293
349
|
status: (handle == null ? void 0 : handle.status) ?? "connecting",
|
|
350
|
+
synced: (handle == null ? void 0 : handle.synced) ?? false,
|
|
294
351
|
accessType: (handle == null ? void 0 : handle.accessType) ?? null,
|
|
295
352
|
error: (handle == null ? void 0 : handle.error) ?? null,
|
|
296
353
|
presence: (handle == null ? void 0 : handle.getPresence()) ?? [],
|
|
@@ -300,7 +357,7 @@ function useGenericDoc(assetId, options) {
|
|
|
300
357
|
}
|
|
301
358
|
};
|
|
302
359
|
}
|
|
303
|
-
const EMPTY_SNAPSHOT = "init||0";
|
|
360
|
+
const EMPTY_SNAPSHOT = "init|false||0";
|
|
304
361
|
function defineDocShape(shape) {
|
|
305
362
|
const keys = Object.keys(shape);
|
|
306
363
|
return {
|
package/dist/collab.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collab.cjs","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"],"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CAAQ,kBAAiB,CAAA;AAAA,IACnC,aAAa,CAAC,UAAA;;AAAU,6BAAU,YAAV,mBAAmB,YAAY;AAAA;AAAA,EAAK;AAEhE;AAEA,MAAM,iBAAiB;AC5DhB,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;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"collab.cjs","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":["Y","WebsocketProvider","useAthenaConfig","useState","useRef","useEffect","useMemo","useSyncExternalStore"],"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;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AAE,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;;;;;;;;;;"}
|
package/dist/collab.js
CHANGED
|
@@ -74,6 +74,7 @@ function connectGenericDoc(args) {
|
|
|
74
74
|
let refreshTimer = null;
|
|
75
75
|
let destroyed = false;
|
|
76
76
|
let status = "connecting";
|
|
77
|
+
let synced = false;
|
|
77
78
|
let accessType = null;
|
|
78
79
|
let error = null;
|
|
79
80
|
let lastPresence = null;
|
|
@@ -97,6 +98,10 @@ function connectGenericDoc(args) {
|
|
|
97
98
|
};
|
|
98
99
|
const teardownProvider = () => {
|
|
99
100
|
clearRefreshTimer();
|
|
101
|
+
if (synced) {
|
|
102
|
+
synced = false;
|
|
103
|
+
notify();
|
|
104
|
+
}
|
|
100
105
|
if (provider) {
|
|
101
106
|
provider.destroy();
|
|
102
107
|
provider = null;
|
|
@@ -160,6 +165,13 @@ function connectGenericDoc(args) {
|
|
|
160
165
|
nextProvider.awareness.setLocalState(lastPresence);
|
|
161
166
|
}
|
|
162
167
|
});
|
|
168
|
+
nextProvider.on("sync", (state) => {
|
|
169
|
+
if (provider !== nextProvider) return;
|
|
170
|
+
if (synced !== state) {
|
|
171
|
+
synced = state;
|
|
172
|
+
notify();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
163
175
|
nextProvider.on("connection-close", (event) => {
|
|
164
176
|
if (provider !== nextProvider) return;
|
|
165
177
|
const action = classifyClose(event == null ? void 0 : event.code);
|
|
@@ -189,11 +201,53 @@ function connectGenericDoc(args) {
|
|
|
189
201
|
scheduleRefresh(grant);
|
|
190
202
|
};
|
|
191
203
|
void connect();
|
|
204
|
+
const waitForSync = (timeoutMs = 15e3) => new Promise((resolve, reject) => {
|
|
205
|
+
const terminal = () => {
|
|
206
|
+
if (destroyed) return new Error("Handle destroyed before initial sync");
|
|
207
|
+
if (status === "closed" || status === "error") {
|
|
208
|
+
return error ?? new Error(`Connection ${status} before initial sync`);
|
|
209
|
+
}
|
|
210
|
+
return null;
|
|
211
|
+
};
|
|
212
|
+
if (synced) {
|
|
213
|
+
resolve();
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const immediate = terminal();
|
|
217
|
+
if (immediate) {
|
|
218
|
+
reject(immediate);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const cleanup = () => {
|
|
222
|
+
clearTimeout(timer);
|
|
223
|
+
listeners.delete(listener);
|
|
224
|
+
};
|
|
225
|
+
const timer = setTimeout(() => {
|
|
226
|
+
cleanup();
|
|
227
|
+
reject(new Error(`Timed out after ${timeoutMs}ms waiting for initial sync`));
|
|
228
|
+
}, timeoutMs);
|
|
229
|
+
const listener = () => {
|
|
230
|
+
if (synced) {
|
|
231
|
+
cleanup();
|
|
232
|
+
resolve();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const err = terminal();
|
|
236
|
+
if (err) {
|
|
237
|
+
cleanup();
|
|
238
|
+
reject(err);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
listeners.add(listener);
|
|
242
|
+
});
|
|
192
243
|
return {
|
|
193
244
|
doc,
|
|
194
245
|
get status() {
|
|
195
246
|
return status;
|
|
196
247
|
},
|
|
248
|
+
get synced() {
|
|
249
|
+
return synced;
|
|
250
|
+
},
|
|
197
251
|
get accessType() {
|
|
198
252
|
return accessType;
|
|
199
253
|
},
|
|
@@ -203,6 +257,7 @@ function connectGenericDoc(args) {
|
|
|
203
257
|
get version() {
|
|
204
258
|
return version;
|
|
205
259
|
},
|
|
260
|
+
waitForSync,
|
|
206
261
|
getPresence: () => {
|
|
207
262
|
if (!provider) return [];
|
|
208
263
|
const entries = [];
|
|
@@ -226,9 +281,10 @@ function connectGenericDoc(args) {
|
|
|
226
281
|
if (destroyed) return;
|
|
227
282
|
destroyed = true;
|
|
228
283
|
teardownProvider();
|
|
284
|
+
status = "closed";
|
|
285
|
+
for (const listener of [...listeners]) listener();
|
|
229
286
|
listeners.clear();
|
|
230
287
|
doc.destroy();
|
|
231
|
-
status = "closed";
|
|
232
288
|
}
|
|
233
289
|
};
|
|
234
290
|
}
|
|
@@ -265,13 +321,14 @@ function useGenericDoc(assetId, options) {
|
|
|
265
321
|
subscribe,
|
|
266
322
|
() => {
|
|
267
323
|
if (!handle) return EMPTY_SNAPSHOT;
|
|
268
|
-
return `${handle.status}|${handle.accessType ?? ""}|${handle.version}`;
|
|
324
|
+
return `${handle.status}|${handle.synced}|${handle.accessType ?? ""}|${handle.version}`;
|
|
269
325
|
},
|
|
270
326
|
() => EMPTY_SNAPSHOT
|
|
271
327
|
);
|
|
272
328
|
return {
|
|
273
329
|
doc: (handle == null ? void 0 : handle.doc) ?? null,
|
|
274
330
|
status: (handle == null ? void 0 : handle.status) ?? "connecting",
|
|
331
|
+
synced: (handle == null ? void 0 : handle.synced) ?? false,
|
|
275
332
|
accessType: (handle == null ? void 0 : handle.accessType) ?? null,
|
|
276
333
|
error: (handle == null ? void 0 : handle.error) ?? null,
|
|
277
334
|
presence: (handle == null ? void 0 : handle.getPresence()) ?? [],
|
|
@@ -281,7 +338,7 @@ function useGenericDoc(assetId, options) {
|
|
|
281
338
|
}
|
|
282
339
|
};
|
|
283
340
|
}
|
|
284
|
-
const EMPTY_SNAPSHOT = "init||0";
|
|
341
|
+
const EMPTY_SNAPSHOT = "init|false||0";
|
|
285
342
|
function defineDocShape(shape) {
|
|
286
343
|
const keys = Object.keys(shape);
|
|
287
344
|
return {
|