@arbidocs/react 0.4.166 → 0.4.168

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.
Files changed (97) hide show
  1. package/dist/artifacts/index.cjs +15 -12
  2. package/dist/artifacts/index.cjs.map +1 -1
  3. package/dist/artifacts/index.js +6 -3
  4. package/dist/artifacts/index.js.map +1 -1
  5. package/dist/calls/livekit/index.cjs +977 -0
  6. package/dist/calls/livekit/index.cjs.map +1 -0
  7. package/dist/calls/livekit/index.d.cts +280 -0
  8. package/dist/calls/livekit/index.d.ts +280 -0
  9. package/dist/calls/livekit/index.js +961 -0
  10. package/dist/calls/livekit/index.js.map +1 -0
  11. package/dist/{chunk-ON4PL6DQ.cjs → chunk-2M4B4FFA.cjs} +48 -4
  12. package/dist/chunk-2M4B4FFA.cjs.map +1 -0
  13. package/dist/chunk-3J7MEBOW.cjs +2551 -0
  14. package/dist/chunk-3J7MEBOW.cjs.map +1 -0
  15. package/dist/chunk-3NVSUYUV.js +528 -0
  16. package/dist/chunk-3NVSUYUV.js.map +1 -0
  17. package/dist/{chunk-QJHCVQ6C.js → chunk-3WIBD44F.js} +4734 -4673
  18. package/dist/chunk-3WIBD44F.js.map +1 -0
  19. package/dist/chunk-5B4PPJNW.cjs +1035 -0
  20. package/dist/chunk-5B4PPJNW.cjs.map +1 -0
  21. package/dist/chunk-5HDC43MF.js +145 -0
  22. package/dist/chunk-5HDC43MF.js.map +1 -0
  23. package/dist/{chunk-D66JCVBB.cjs → chunk-7NHOKV7N.cjs} +4093 -4040
  24. package/dist/chunk-7NHOKV7N.cjs.map +1 -0
  25. package/dist/chunk-APZZQZ6P.js +147 -0
  26. package/dist/chunk-APZZQZ6P.js.map +1 -0
  27. package/dist/{chunk-I5ZBQ63Y.js → chunk-DGMK3EUN.js} +188 -181
  28. package/dist/chunk-DGMK3EUN.js.map +1 -0
  29. package/dist/chunk-DPYBZDJB.js +18 -0
  30. package/dist/chunk-DPYBZDJB.js.map +1 -0
  31. package/dist/chunk-EEIYXVM4.cjs +181 -0
  32. package/dist/chunk-EEIYXVM4.cjs.map +1 -0
  33. package/dist/chunk-H3LMKO3T.js +2362 -0
  34. package/dist/chunk-H3LMKO3T.js.map +1 -0
  35. package/dist/{chunk-JSSKEXRR.js → chunk-KGCGA452.js} +2 -2
  36. package/dist/chunk-KGCGA452.js.map +1 -0
  37. package/dist/chunk-MJNZLWJD.js +986 -0
  38. package/dist/chunk-MJNZLWJD.js.map +1 -0
  39. package/dist/chunk-N73IMVFF.cjs +21 -0
  40. package/dist/chunk-N73IMVFF.cjs.map +1 -0
  41. package/dist/chunk-NVAIVJID.js +298 -0
  42. package/dist/chunk-NVAIVJID.js.map +1 -0
  43. package/dist/{chunk-GTJ667CP.cjs → chunk-QQ7SCJRY.cjs} +2 -2
  44. package/dist/chunk-QQ7SCJRY.cjs.map +1 -0
  45. package/dist/chunk-S4PCA3ES.cjs +572 -0
  46. package/dist/chunk-S4PCA3ES.cjs.map +1 -0
  47. package/dist/chunk-VAMPYLQ4.cjs +157 -0
  48. package/dist/chunk-VAMPYLQ4.cjs.map +1 -0
  49. package/dist/chunk-VLK52VXC.cjs +307 -0
  50. package/dist/chunk-VLK52VXC.cjs.map +1 -0
  51. package/dist/{chunk-SVHGCX25.js → chunk-YWYCEEBF.js} +43 -4
  52. package/dist/chunk-YWYCEEBF.js.map +1 -0
  53. package/dist/{chunk-EUC2BTNS.cjs → chunk-ZI2VJOID.cjs} +206 -199
  54. package/dist/chunk-ZI2VJOID.cjs.map +1 -0
  55. package/dist/document-tools/index.cjs +85 -109
  56. package/dist/document-tools/index.cjs.map +1 -1
  57. package/dist/document-tools/index.d.cts +19 -5
  58. package/dist/document-tools/index.d.ts +19 -5
  59. package/dist/document-tools/index.js +28 -70
  60. package/dist/document-tools/index.js.map +1 -1
  61. package/dist/index.cjs +12692 -9494
  62. package/dist/index.cjs.map +1 -1
  63. package/dist/{index.d-DfCFKora.d.cts → index.d-Bvp6rKNo.d.cts} +139 -1
  64. package/dist/{index.d-DfCFKora.d.ts → index.d-Bvp6rKNo.d.ts} +139 -1
  65. package/dist/{index.d-DCjYj7YK.d.ts → index.d-Dx9sGkW2.d.cts} +20 -8
  66. package/dist/{index.d-DCjYj7YK.d.cts → index.d-Dx9sGkW2.d.ts} +20 -8
  67. package/dist/index.d.cts +5465 -2291
  68. package/dist/index.d.ts +5465 -2291
  69. package/dist/index.js +11047 -8858
  70. package/dist/index.js.map +1 -1
  71. package/dist/query-keys.d-Kw0JMoff.d.cts +308 -0
  72. package/dist/query-keys.d-Kw0JMoff.d.ts +308 -0
  73. package/dist/shell/index.cjs +78 -21
  74. package/dist/shell/index.d.cts +1 -1
  75. package/dist/shell/index.d.ts +1 -1
  76. package/dist/shell/index.js +6 -1
  77. package/dist/ui/index.cjs +200 -195
  78. package/dist/ui/index.d.cts +1 -1
  79. package/dist/ui/index.d.ts +1 -1
  80. package/dist/ui/index.js +4 -3
  81. package/dist/useAudioRecorder.d-CWFDsb_S.d.cts +778 -0
  82. package/dist/useAudioRecorder.d-CWFDsb_S.d.ts +778 -0
  83. package/dist/viewers/index.cjs +2 -2
  84. package/dist/viewers/index.js +1 -1
  85. package/package.json +13 -3
  86. package/dist/chunk-6MTKKT2O.cjs +0 -999
  87. package/dist/chunk-6MTKKT2O.cjs.map +0 -1
  88. package/dist/chunk-CEZCFQ47.js +0 -932
  89. package/dist/chunk-CEZCFQ47.js.map +0 -1
  90. package/dist/chunk-D66JCVBB.cjs.map +0 -1
  91. package/dist/chunk-EUC2BTNS.cjs.map +0 -1
  92. package/dist/chunk-GTJ667CP.cjs.map +0 -1
  93. package/dist/chunk-I5ZBQ63Y.js.map +0 -1
  94. package/dist/chunk-JSSKEXRR.js.map +0 -1
  95. package/dist/chunk-ON4PL6DQ.cjs.map +0 -1
  96. package/dist/chunk-QJHCVQ6C.js.map +0 -1
  97. package/dist/chunk-SVHGCX25.js.map +0 -1
@@ -0,0 +1,572 @@
1
+ 'use strict';
2
+
3
+ var chunkVLK52VXC_cjs = require('./chunk-VLK52VXC.cjs');
4
+ var chunkQQ7SCJRY_cjs = require('./chunk-QQ7SCJRY.cjs');
5
+ var zustand = require('zustand');
6
+ var react = require('react');
7
+ var reactQuery = require('@tanstack/react-query');
8
+ var browser = require('@arbidocs/sdk/browser');
9
+
10
+ // src/documents/documents-cache.ts
11
+ var rowListeners = /* @__PURE__ */ new Set();
12
+ function subscribeDocumentRows(listener) {
13
+ rowListeners.add(listener);
14
+ return () => {
15
+ rowListeners.delete(listener);
16
+ };
17
+ }
18
+ function getCachedDocuments(qc, workspaceId) {
19
+ if (!workspaceId) return void 0;
20
+ return qc.getQueryData(chunkVLK52VXC_cjs.arbiQueryKeys.documents(workspaceId));
21
+ }
22
+ function cachedLists(qc) {
23
+ return qc.getQueriesData({ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.allDocuments() }).flatMap(
24
+ ([key, data]) => typeof key[2] === "string" && Array.isArray(data) ? [[key[2], data]] : []
25
+ );
26
+ }
27
+ function diffRows(workspaceId, before, after) {
28
+ const beforeById = new Map(before.map((doc) => [doc.external_id, doc]));
29
+ const add = [];
30
+ const update2 = [];
31
+ const seen = /* @__PURE__ */ new Set();
32
+ for (const doc of after) {
33
+ seen.add(doc.external_id);
34
+ const previous = beforeById.get(doc.external_id);
35
+ if (!previous) add.push(doc);
36
+ else if (previous !== doc) update2.push(doc);
37
+ }
38
+ const remove = [...beforeById.keys()].filter((id) => !seen.has(id));
39
+ return { workspaceId, add, update: update2, remove };
40
+ }
41
+ function patchDocuments(qc, reducer, workspaceId) {
42
+ const lists = workspaceId ? (() => {
43
+ const current = getCachedDocuments(qc, workspaceId);
44
+ return current ? [[workspaceId, current]] : [];
45
+ })() : cachedLists(qc);
46
+ for (const [id, before] of lists) {
47
+ const after = reducer(before);
48
+ if (after === before) continue;
49
+ qc.setQueryData(chunkVLK52VXC_cjs.arbiQueryKeys.documents(id), after);
50
+ const change = diffRows(id, before, after);
51
+ const beforeById = new Map(before.map((doc) => [doc.external_id, doc]));
52
+ if (change.update.some(
53
+ (doc) => doc.current_version !== beforeById.get(doc.external_id)?.current_version
54
+ ))
55
+ void qc.invalidateQueries({ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.allDocumentVersions() });
56
+ if (change.add.length || change.update.length || change.remove.length)
57
+ for (const listener of rowListeners) listener(change);
58
+ }
59
+ }
60
+ function upsertDocuments(qc, documents, workspaceId) {
61
+ if (!documents.length) return;
62
+ const byId = new Map(documents.map((doc) => [doc.external_id, doc]));
63
+ const byWorkspace = /* @__PURE__ */ new Map();
64
+ for (const doc of documents) {
65
+ const ws = workspaceId ?? doc.workspace_ext_id;
66
+ byWorkspace.set(ws, [...byWorkspace.get(ws) ?? [], doc]);
67
+ }
68
+ for (const [ws, incoming] of byWorkspace) {
69
+ patchDocuments(
70
+ qc,
71
+ (old) => {
72
+ const known = new Set(old.map((doc) => doc.external_id));
73
+ return [
74
+ ...old.map((doc) => byId.get(doc.external_id) ?? doc),
75
+ ...incoming.filter((doc) => !known.has(doc.external_id))
76
+ ];
77
+ },
78
+ ws
79
+ );
80
+ }
81
+ }
82
+ function setCachedDocument(qc, doc) {
83
+ qc.setQueryData(chunkVLK52VXC_cjs.arbiQueryKeys.document(doc.external_id), doc);
84
+ }
85
+ function patchCachedDocument(qc, docId, reducer) {
86
+ const current = qc.getQueryData(chunkVLK52VXC_cjs.arbiQueryKeys.document(docId));
87
+ if (current) qc.setQueryData(chunkVLK52VXC_cjs.arbiQueryKeys.document(docId), reducer(current));
88
+ }
89
+ function dropTagFromCachedDocuments(qc, tagId) {
90
+ const strip = (doc) => {
91
+ const doctags = doc.doctags?.filter((dt) => dt.tag_ext_id !== tagId);
92
+ return doctags && doctags.length !== doc.doctags?.length ? { ...doc, doctags } : doc;
93
+ };
94
+ const carriers = [];
95
+ patchDocuments(qc, (docs) => {
96
+ const next = docs.map(strip);
97
+ next.forEach((doc, i) => doc !== docs[i] && carriers.push(doc.external_id));
98
+ return carriers.length ? next : docs;
99
+ });
100
+ for (const id of carriers) patchCachedDocument(qc, id, strip);
101
+ }
102
+ function forgetDocuments(qc, docIds) {
103
+ for (const id of docIds) {
104
+ qc.removeQueries({ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.document(id) });
105
+ qc.removeQueries({ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.documentVersions(id) });
106
+ qc.removeQueries({ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.parsedDocument(id) });
107
+ }
108
+ }
109
+ var useEntityExtractionStore = zustand.create(() => ({ docIds: [] }));
110
+ function update(docIds, running) {
111
+ if (!docIds.length) return;
112
+ useEntityExtractionStore.setState((state) => {
113
+ const ids = new Set(state.docIds);
114
+ docIds.forEach((id) => running ? ids.add(id) : ids.delete(id));
115
+ return { docIds: [...ids] };
116
+ });
117
+ }
118
+ function startEntityExtraction(docIds) {
119
+ update(docIds, true);
120
+ }
121
+ function finishEntityExtraction(docIds) {
122
+ update(docIds, false);
123
+ }
124
+ function useEntityExtractionDocIds() {
125
+ return useEntityExtractionStore((state) => state.docIds);
126
+ }
127
+ function openWorkspaceId(arbi) {
128
+ return arbi.hasWorkspace ? arbi.getWorkspaceId() : null;
129
+ }
130
+ function patchWorkspaceTags(qc, workspaceId, reducer) {
131
+ const key = chunkVLK52VXC_cjs.arbiQueryKeys.tags(workspaceId);
132
+ const current = qc.getQueryData(key);
133
+ if (!current) return;
134
+ const next = reducer(current);
135
+ if (next !== current) qc.setQueryData(key, next);
136
+ }
137
+ function invalidateTagTables(qc, workspaceId) {
138
+ return qc.invalidateQueries({ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.tagLists(workspaceId) });
139
+ }
140
+ function invalidateTagData(qc, workspaceId) {
141
+ return Promise.all([
142
+ qc.invalidateQueries({ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.tags(workspaceId) }),
143
+ qc.invalidateQueries({ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.facts(workspaceId) })
144
+ ]);
145
+ }
146
+ function useInvalidateTagData() {
147
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
148
+ const qc = reactQuery.useQueryClient();
149
+ return react.useCallback(() => {
150
+ const workspaceId = openWorkspaceId(arbi);
151
+ return workspaceId ? invalidateTagData(qc, workspaceId) : Promise.resolve();
152
+ }, [arbi, qc]);
153
+ }
154
+ function settleCreatedTag(qc, created) {
155
+ const workspaceId = created.workspace_ext_id;
156
+ patchWorkspaceTags(
157
+ qc,
158
+ workspaceId,
159
+ (old) => old.some((t) => t.external_id === created.external_id) ? old : [...old, created]
160
+ );
161
+ return invalidateTagTables(qc, workspaceId);
162
+ }
163
+ function settleChangedTag(qc, tag) {
164
+ const workspaceId = tag.workspace_ext_id;
165
+ patchWorkspaceTags(
166
+ qc,
167
+ workspaceId,
168
+ (old) => old.map((t) => t.external_id === tag.external_id ? { ...t, ...tag } : t)
169
+ );
170
+ return invalidateTagTables(qc, workspaceId);
171
+ }
172
+ var replayableTagMutations = [
173
+ {
174
+ mutationKey: chunkVLK52VXC_cjs.arbiMutationKeys.createTag(),
175
+ run: (client, input) => browser.tags.createTag(client, input),
176
+ settle: (qc, created) => settleCreatedTag(qc, created)
177
+ }
178
+ ];
179
+ function useCreateTag() {
180
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
181
+ const qc = reactQuery.useQueryClient();
182
+ return reactQuery.useMutation({
183
+ mutationKey: chunkVLK52VXC_cjs.arbiMutationKeys.createTag(),
184
+ mutationFn: (input) => arbi.tags.create(input),
185
+ onSuccess: (created) => settleCreatedTag(qc, created)
186
+ });
187
+ }
188
+ function useIsCreatingTag() {
189
+ const qc = reactQuery.useQueryClient();
190
+ return react.useCallback(
191
+ (name) => {
192
+ const wanted = name.trim().toLowerCase();
193
+ return qc.getMutationCache().findAll({
194
+ mutationKey: chunkVLK52VXC_cjs.arbiMutationKeys.createTag(),
195
+ predicate: (m) => m.state.status === "pending" && m.state.variables?.name.trim().toLowerCase() === wanted
196
+ }).length > 0;
197
+ },
198
+ [qc]
199
+ );
200
+ }
201
+ function useUpdateTag() {
202
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
203
+ const qc = reactQuery.useQueryClient();
204
+ return reactQuery.useMutation({
205
+ mutationKey: chunkVLK52VXC_cjs.arbiMutationKeys.updateTag(),
206
+ mutationFn: ({ tagId, change }) => arbi.tags.update(tagId, change),
207
+ onSuccess: (tag) => settleChangedTag(qc, tag)
208
+ });
209
+ }
210
+ function useSetTagValues() {
211
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
212
+ const qc = reactQuery.useQueryClient();
213
+ return reactQuery.useMutation({
214
+ mutationKey: chunkVLK52VXC_cjs.arbiMutationKeys.updateTag(),
215
+ mutationFn: ({
216
+ tagId,
217
+ values
218
+ }) => arbi.tags.setValues(tagId, values),
219
+ onSuccess: (tag) => settleChangedTag(qc, tag)
220
+ });
221
+ }
222
+ function useDeleteTag() {
223
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
224
+ const qc = reactQuery.useQueryClient();
225
+ return reactQuery.useMutation({
226
+ mutationKey: chunkVLK52VXC_cjs.arbiMutationKeys.deleteTag(),
227
+ mutationFn: (tagId) => arbi.tags.delete(tagId),
228
+ onSuccess: (_data, tagId) => {
229
+ dropTagFromCachedDocuments(qc, tagId);
230
+ const workspaceId = openWorkspaceId(arbi);
231
+ if (!workspaceId) return;
232
+ patchWorkspaceTags(
233
+ qc,
234
+ workspaceId,
235
+ (old) => old.filter((t) => t.external_id !== tagId).map((t) => t.parent_ext_id === tagId ? { ...t, parent_ext_id: null } : t)
236
+ );
237
+ return Promise.all([
238
+ invalidateTagTables(qc, workspaceId),
239
+ qc.invalidateQueries({ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.facts(workspaceId) })
240
+ ]);
241
+ }
242
+ });
243
+ }
244
+ function useExtractEntities() {
245
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
246
+ return reactQuery.useMutation({
247
+ mutationKey: chunkVLK52VXC_cjs.arbiMutationKeys.extractEntities(),
248
+ mutationFn: (docIds) => arbi.tags.extractEntities(docIds),
249
+ onSuccess: (data) => startEntityExtraction(data.doc_ext_ids)
250
+ });
251
+ }
252
+ function useTagKindSchema(kind, options) {
253
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
254
+ const workspaceId = openWorkspaceId(arbi) ?? "";
255
+ return reactQuery.useQuery({
256
+ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.tagKind(workspaceId, kind ?? ""),
257
+ queryFn: () => arbi.tagKinds.get(kind),
258
+ enabled: (options?.enabled ?? true) && !!kind && !!workspaceId
259
+ });
260
+ }
261
+ function useUpdateTagKindSchema() {
262
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
263
+ const qc = reactQuery.useQueryClient();
264
+ return reactQuery.useMutation({
265
+ mutationKey: chunkVLK52VXC_cjs.arbiMutationKeys.updateTagKindSchema(),
266
+ mutationFn: ({
267
+ kind,
268
+ fields
269
+ }) => arbi.tagKinds.update(kind, fields),
270
+ onSuccess: (schema) => {
271
+ const workspaceId = openWorkspaceId(arbi);
272
+ if (!workspaceId) return;
273
+ qc.setQueryData(chunkVLK52VXC_cjs.arbiQueryKeys.tagKind(workspaceId, schema.kind), schema);
274
+ return invalidateTagData(qc, workspaceId);
275
+ }
276
+ });
277
+ }
278
+ var DOCUMENTS_FIRST_PAGE_SIZE = 500;
279
+ var DOCUMENTS_PAGE_SIZE = 2e3;
280
+ var DOCUMENTS_FIRST_PAGE_MEASURE = "arbi:document-list:page1";
281
+ var FIRST_PAGE_MEASURE = DOCUMENTS_FIRST_PAGE_MEASURE;
282
+ var FIRST_PAGE_START = "arbi:document-list:start";
283
+ function mark(name) {
284
+ try {
285
+ performance.mark(name);
286
+ } catch {
287
+ }
288
+ }
289
+ function measure(name, start) {
290
+ try {
291
+ performance.measure(name, start);
292
+ } catch {
293
+ }
294
+ }
295
+ var backgroundReads = /* @__PURE__ */ new Map();
296
+ var loadingListeners = /* @__PURE__ */ new Set();
297
+ function emitLoading() {
298
+ for (const listener of loadingListeners) listener();
299
+ }
300
+ function subscribeLoading(listener) {
301
+ loadingListeners.add(listener);
302
+ return () => {
303
+ loadingListeners.delete(listener);
304
+ };
305
+ }
306
+ function isDocumentsLoading() {
307
+ return backgroundReads.size > 0;
308
+ }
309
+ function useDocumentsLoading() {
310
+ return react.useSyncExternalStore(subscribeLoading, isDocumentsLoading, () => false);
311
+ }
312
+ function stopDocumentsLoading(workspaceId) {
313
+ const controller = backgroundReads.get(workspaceId);
314
+ if (!controller) return;
315
+ controller.abort();
316
+ backgroundReads.delete(workspaceId);
317
+ emitLoading();
318
+ }
319
+ async function loadDocuments(qc, arbi, workspaceId, signal) {
320
+ let stopped = false;
321
+ for (const [id, controller2] of backgroundReads) {
322
+ if (id === workspaceId) continue;
323
+ controller2.abort();
324
+ backgroundReads.delete(id);
325
+ stopped = true;
326
+ }
327
+ if (stopped) emitLoading();
328
+ const controller = new AbortController();
329
+ signal?.addEventListener("abort", () => controller.abort(), { once: true });
330
+ const pages = arbi.documents.listPaginated({
331
+ pageSize: DOCUMENTS_PAGE_SIZE,
332
+ firstPageSize: DOCUMENTS_FIRST_PAGE_SIZE,
333
+ signal: controller.signal
334
+ });
335
+ mark(FIRST_PAGE_START);
336
+ const first = await pages.next();
337
+ const firstPage = first.done ? [] : first.value;
338
+ measure(FIRST_PAGE_MEASURE, FIRST_PAGE_START);
339
+ if (first.done || firstPage.length < DOCUMENTS_FIRST_PAGE_SIZE) return firstPage;
340
+ if (backgroundReads.has(workspaceId)) {
341
+ controller.abort();
342
+ return firstPage;
343
+ }
344
+ backgroundReads.set(workspaceId, controller);
345
+ emitLoading();
346
+ void appendRemainingPages(qc, workspaceId, pages, controller.signal).finally(() => {
347
+ if (backgroundReads.get(workspaceId) !== controller) return;
348
+ backgroundReads.delete(workspaceId);
349
+ emitLoading();
350
+ });
351
+ return firstPage;
352
+ }
353
+ async function appendRemainingPages(qc, workspaceId, pages, signal) {
354
+ await Promise.resolve();
355
+ try {
356
+ for await (const page of pages) {
357
+ if (signal.aborted) return;
358
+ patchDocuments(
359
+ qc,
360
+ (old) => {
361
+ const known = new Set(old.map((doc) => doc.external_id));
362
+ const fresh = page.filter((doc) => !known.has(doc.external_id));
363
+ return fresh.length ? [...old, ...fresh] : old;
364
+ },
365
+ workspaceId
366
+ );
367
+ }
368
+ } catch (error) {
369
+ if (!signal.aborted) console.error(`Reading the documents of ${workspaceId} failed:`, error);
370
+ }
371
+ }
372
+
373
+ // src/documents/processing-order.ts
374
+ var FINISHED_DOCUMENT_STATUSES = /* @__PURE__ */ new Set([
375
+ "completed",
376
+ "low-content",
377
+ "failed",
378
+ "skipped",
379
+ "empty"
380
+ ]);
381
+ var IN_FLIGHT_DOCUMENT_STATUSES = /* @__PURE__ */ new Set([
382
+ "uploading",
383
+ "queued",
384
+ "parsing",
385
+ "encrypting",
386
+ "indexing",
387
+ "analysing"
388
+ ]);
389
+ function ordered(value) {
390
+ return typeof value === "number";
391
+ }
392
+ function isStaleTaskUpdate(held, next) {
393
+ return !!held && ordered(held.seq) && ordered(next.seq) && next.seq <= held.seq;
394
+ }
395
+ function isRowNewer(row, task) {
396
+ if (!ordered(row.status_seq) || !ordered(task.seq)) return void 0;
397
+ return row.status_seq > task.seq;
398
+ }
399
+ function resolveDocumentStatus(row, task) {
400
+ const rowStatus = (row.status ?? "").toLowerCase();
401
+ const live = task?.status;
402
+ if (!task || !live) return rowStatus;
403
+ const rowNewer = isRowNewer(row, task);
404
+ if (rowNewer !== void 0) return rowNewer ? rowStatus || live : live;
405
+ if (IN_FLIGHT_DOCUMENT_STATUSES.has(live)) return live;
406
+ if (FINISHED_DOCUMENT_STATUSES.has(rowStatus)) return rowStatus;
407
+ return live;
408
+ }
409
+ function settledByRows(tasks, rows) {
410
+ const inFlight = /* @__PURE__ */ new Map();
411
+ for (const task of Object.values(tasks))
412
+ if (IN_FLIGHT_DOCUMENT_STATUSES.has(task.status)) inFlight.set(task.doc_ext_id, task);
413
+ if (!inFlight.size) return [];
414
+ const settled = [];
415
+ for (const row of rows) {
416
+ const task = inFlight.get(row.external_id);
417
+ if (!task) continue;
418
+ const rowStatus = (row.status ?? "").toLowerCase();
419
+ if (!FINISHED_DOCUMENT_STATUSES.has(rowStatus) || isRowNewer(row, task) === false) continue;
420
+ settled.push({ ...task, status: rowStatus, seq: row.status_seq ?? task.seq });
421
+ }
422
+ return settled;
423
+ }
424
+ function openWorkspaceId2(arbi) {
425
+ return arbi.hasWorkspace ? arbi.getWorkspaceId() : null;
426
+ }
427
+ function useDocuments(workspaceId, options) {
428
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
429
+ const qc = reactQuery.useQueryClient();
430
+ const workspace = workspaceId ?? openWorkspaceId2(arbi) ?? "";
431
+ return reactQuery.useQuery({
432
+ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.documents(workspace),
433
+ queryFn: ({ signal }) => {
434
+ if (openWorkspaceId2(arbi) !== workspace)
435
+ return Promise.reject(new Error("The selected workspace changed."));
436
+ return loadDocuments(qc, arbi, workspace, signal);
437
+ },
438
+ enabled: (options?.enabled ?? true) && !!workspace && arbi.hasWorkspace,
439
+ staleTime: Infinity,
440
+ refetchOnMount: false,
441
+ refetchOnReconnect: false,
442
+ refetchOnWindowFocus: false
443
+ });
444
+ }
445
+ var DOCUMENT_STATUS_POLL_MS = 4e3;
446
+ function processingInterval(doc) {
447
+ return doc?.status && !FINISHED_DOCUMENT_STATUSES.has(doc.status) ? DOCUMENT_STATUS_POLL_MS : false;
448
+ }
449
+ function useDocument(docId, options) {
450
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
451
+ return reactQuery.useQuery({
452
+ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.document(docId ?? ""),
453
+ queryFn: async () => (await arbi.documents.get([docId]))[0] ?? null,
454
+ enabled: (options?.enabled ?? true) && !!docId && arbi.isLoggedIn,
455
+ refetchInterval: options?.pollWhileProcessing ? (query) => processingInterval(query.state.data) : false
456
+ });
457
+ }
458
+ function useDocumentsById(docIds, options) {
459
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
460
+ return reactQuery.useQuery({
461
+ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.documentsById(docIds),
462
+ queryFn: () => arbi.documents.get(docIds),
463
+ enabled: (options?.enabled ?? true) && docIds.length > 0 && arbi.isLoggedIn
464
+ });
465
+ }
466
+ var SIMILARITY_THRESHOLD = 0.98;
467
+ function useSimilarDocuments(options) {
468
+ const arbi = chunkQQ7SCJRY_cjs.useArbi();
469
+ const workspace = openWorkspaceId2(arbi) ?? "";
470
+ return reactQuery.useQuery({
471
+ queryKey: chunkVLK52VXC_cjs.arbiQueryKeys.similarDocuments(workspace, SIMILARITY_THRESHOLD),
472
+ queryFn: () => arbi.documents.similar({ threshold: SIMILARITY_THRESHOLD }),
473
+ enabled: (options?.enabled ?? true) && !!workspace
474
+ });
475
+ }
476
+ var readSeq = 0;
477
+ var latestReadByDoc = /* @__PURE__ */ new Map();
478
+ async function refreshDocuments(qc, arbi, docIds, options) {
479
+ if (!docIds.length) return [];
480
+ const seq = ++readSeq;
481
+ for (const id of docIds) latestReadByDoc.set(id, seq);
482
+ try {
483
+ const read = await arbi.documents.get(docIds);
484
+ const fresh = read.filter((doc) => latestReadByDoc.get(doc.external_id) === seq).map((doc) => {
485
+ const live = options?.processingTask?.(doc.external_id);
486
+ return live && FINISHED_DOCUMENT_STATUSES.has(live.status) && doc.status !== live.status && isRowNewer(doc, live) !== true ? { ...doc, status: live.status } : doc;
487
+ });
488
+ if (!fresh.length) return [];
489
+ reconcileTagCounts(qc, fresh);
490
+ upsertDocuments(qc, fresh);
491
+ for (const doc of fresh) setCachedDocument(qc, doc);
492
+ return fresh;
493
+ } catch (error) {
494
+ console.error("Re-reading documents failed; the cached ones are kept:", error);
495
+ return [];
496
+ } finally {
497
+ for (const id of docIds) if (latestReadByDoc.get(id) === seq) latestReadByDoc.delete(id);
498
+ }
499
+ }
500
+ function reconcileTagCounts(qc, fresh) {
501
+ const byWorkspace = /* @__PURE__ */ new Map();
502
+ for (const doc of fresh) {
503
+ const cached = getCachedDocuments(qc, doc.workspace_ext_id)?.find(
504
+ (d) => d.external_id === doc.external_id
505
+ );
506
+ const before = new Set((cached?.doctags ?? []).map((dt) => dt.tag_ext_id));
507
+ const after = new Set((doc.doctags ?? []).map((dt) => dt.tag_ext_id));
508
+ const delta = byWorkspace.get(doc.workspace_ext_id) ?? /* @__PURE__ */ new Map();
509
+ for (const id of after) if (!before.has(id)) delta.set(id, (delta.get(id) ?? 0) + 1);
510
+ for (const id of before) if (!after.has(id)) delta.set(id, (delta.get(id) ?? 0) - 1);
511
+ byWorkspace.set(doc.workspace_ext_id, delta);
512
+ }
513
+ for (const [workspaceId, delta] of byWorkspace) adjustTagCounts(qc, workspaceId, delta);
514
+ }
515
+ function adjustTagCounts(qc, workspaceId, delta) {
516
+ if (![...delta.values()].some(Boolean)) return;
517
+ patchWorkspaceTags(
518
+ qc,
519
+ workspaceId,
520
+ (tags) => tags.map((tag) => {
521
+ const change = delta.get(tag.external_id);
522
+ return change ? { ...tag, doctag_count: Math.max(0, tag.doctag_count + change) } : tag;
523
+ })
524
+ );
525
+ void invalidateTagTables(qc, workspaceId);
526
+ }
527
+
528
+ exports.DOCUMENTS_FIRST_PAGE_MEASURE = DOCUMENTS_FIRST_PAGE_MEASURE;
529
+ exports.DOCUMENTS_FIRST_PAGE_SIZE = DOCUMENTS_FIRST_PAGE_SIZE;
530
+ exports.DOCUMENTS_PAGE_SIZE = DOCUMENTS_PAGE_SIZE;
531
+ exports.FINISHED_DOCUMENT_STATUSES = FINISHED_DOCUMENT_STATUSES;
532
+ exports.IN_FLIGHT_DOCUMENT_STATUSES = IN_FLIGHT_DOCUMENT_STATUSES;
533
+ exports.SIMILARITY_THRESHOLD = SIMILARITY_THRESHOLD;
534
+ exports.adjustTagCounts = adjustTagCounts;
535
+ exports.dropTagFromCachedDocuments = dropTagFromCachedDocuments;
536
+ exports.finishEntityExtraction = finishEntityExtraction;
537
+ exports.forgetDocuments = forgetDocuments;
538
+ exports.getCachedDocuments = getCachedDocuments;
539
+ exports.invalidateTagData = invalidateTagData;
540
+ exports.invalidateTagTables = invalidateTagTables;
541
+ exports.isDocumentsLoading = isDocumentsLoading;
542
+ exports.isRowNewer = isRowNewer;
543
+ exports.isStaleTaskUpdate = isStaleTaskUpdate;
544
+ exports.patchCachedDocument = patchCachedDocument;
545
+ exports.patchDocuments = patchDocuments;
546
+ exports.patchWorkspaceTags = patchWorkspaceTags;
547
+ exports.refreshDocuments = refreshDocuments;
548
+ exports.replayableTagMutations = replayableTagMutations;
549
+ exports.resolveDocumentStatus = resolveDocumentStatus;
550
+ exports.setCachedDocument = setCachedDocument;
551
+ exports.settledByRows = settledByRows;
552
+ exports.startEntityExtraction = startEntityExtraction;
553
+ exports.stopDocumentsLoading = stopDocumentsLoading;
554
+ exports.subscribeDocumentRows = subscribeDocumentRows;
555
+ exports.upsertDocuments = upsertDocuments;
556
+ exports.useCreateTag = useCreateTag;
557
+ exports.useDeleteTag = useDeleteTag;
558
+ exports.useDocument = useDocument;
559
+ exports.useDocuments = useDocuments;
560
+ exports.useDocumentsById = useDocumentsById;
561
+ exports.useDocumentsLoading = useDocumentsLoading;
562
+ exports.useEntityExtractionDocIds = useEntityExtractionDocIds;
563
+ exports.useExtractEntities = useExtractEntities;
564
+ exports.useInvalidateTagData = useInvalidateTagData;
565
+ exports.useIsCreatingTag = useIsCreatingTag;
566
+ exports.useSetTagValues = useSetTagValues;
567
+ exports.useSimilarDocuments = useSimilarDocuments;
568
+ exports.useTagKindSchema = useTagKindSchema;
569
+ exports.useUpdateTag = useUpdateTag;
570
+ exports.useUpdateTagKindSchema = useUpdateTagKindSchema;
571
+ //# sourceMappingURL=chunk-S4PCA3ES.cjs.map
572
+ //# sourceMappingURL=chunk-S4PCA3ES.cjs.map