@arbidocs/react 0.4.143 → 0.4.145
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/artifacts/index.cjs +11 -14
- package/dist/artifacts/index.cjs.map +1 -1
- package/dist/artifacts/index.d.cts +1 -3
- package/dist/artifacts/index.d.ts +1 -3
- package/dist/artifacts/index.js +7 -10
- package/dist/artifacts/index.js.map +1 -1
- package/dist/{chunk-5RYBYTCF.cjs → chunk-CG6USBJX.cjs} +481 -11
- package/dist/chunk-CG6USBJX.cjs.map +1 -0
- package/dist/chunk-MUYBFDYJ.js +859 -0
- package/dist/chunk-MUYBFDYJ.js.map +1 -0
- package/dist/index.cjs +562 -284
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +554 -158
- package/dist/index.d.ts +554 -158
- package/dist/index.js +291 -145
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/dist/chunk-5RYBYTCF.cjs.map +0 -1
- package/dist/chunk-GQ5TA4BM.js +0 -421
- package/dist/chunk-GQ5TA4BM.js.map +0 -1
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
var chunkR2MF7YCW_cjs = require('./chunk-R2MF7YCW.cjs');
|
|
4
4
|
var react = require('react');
|
|
5
5
|
var reactQuery = require('@tanstack/react-query');
|
|
6
|
+
var browser = require('@arbidocs/sdk/browser');
|
|
6
7
|
|
|
7
8
|
// src/usage-metered.ts
|
|
8
9
|
function isUsageMetered(usage, subscription) {
|
|
@@ -12,7 +13,15 @@ function isUsageMetered(usage, subscription) {
|
|
|
12
13
|
var arbiQueryKeys = {
|
|
13
14
|
workspaces: () => ["arbi", "workspaces"],
|
|
14
15
|
workspaceUsers: (workspaceId) => ["arbi", "workspaceUsers", workspaceId],
|
|
16
|
+
/** Every workspace's documents list: the prefix a write patches when it does not know the workspace. */
|
|
17
|
+
allDocuments: () => ["arbi", "documents"],
|
|
15
18
|
documents: (workspaceId) => ["arbi", "documents", workspaceId],
|
|
19
|
+
/** One document (a viewer's); writes keep it current next to the list. */
|
|
20
|
+
document: (docId) => ["arbi", "document", docId],
|
|
21
|
+
documentsById: (docIds) => ["arbi", "documentsById", [...docIds].sort().join(",")],
|
|
22
|
+
/** Every workspace's near-duplicate pairs. */
|
|
23
|
+
allSimilarDocuments: () => ["arbi", "similarDocuments"],
|
|
24
|
+
similarDocuments: (workspaceId, threshold) => ["arbi", "similarDocuments", workspaceId, threshold],
|
|
16
25
|
/** Every version list: the prefix a new version invalidates. */
|
|
17
26
|
allDocumentVersions: () => ["arbi", "documentVersions"],
|
|
18
27
|
documentVersions: (docId) => ["arbi", "documentVersions", docId],
|
|
@@ -36,6 +45,8 @@ var arbiQueryKeys = {
|
|
|
36
45
|
/** Under the conversations prefix: every step action or conversation write refreshes it. */
|
|
37
46
|
conversationUsage: (conversationId) => ["arbi", "conversations", conversationId, "usage"],
|
|
38
47
|
thumbnails: (docIds) => ["arbi", "thumbnails", [...docIds].sort().join(",")],
|
|
48
|
+
/** Every stage of a document's parsed content. */
|
|
49
|
+
parsedDocument: (docId) => ["arbi", "parsedDoc", docId],
|
|
39
50
|
parsedDoc: (docId, stage) => ["arbi", "parsedDoc", docId, stage],
|
|
40
51
|
agents: () => ["arbi", "agents"],
|
|
41
52
|
sessions: () => ["arbi", "sessions"],
|
|
@@ -93,7 +104,15 @@ var arbiMutationKeys = {
|
|
|
93
104
|
createTag: () => ["arbi", "tags", "create"],
|
|
94
105
|
updateTag: () => ["arbi", "tags", "update"],
|
|
95
106
|
deleteTag: () => ["arbi", "tags", "delete"],
|
|
96
|
-
extractEntities: () => ["arbi", "tags", "entities", "extract"]
|
|
107
|
+
extractEntities: () => ["arbi", "tags", "entities", "extract"],
|
|
108
|
+
updateDocuments: () => ["arbi", "documents", "update"],
|
|
109
|
+
deleteDocuments: () => ["arbi", "documents", "delete"],
|
|
110
|
+
uploadDocuments: () => ["arbi", "documents", "upload"],
|
|
111
|
+
uploadUrls: () => ["arbi", "documents", "uploadUrls"],
|
|
112
|
+
assignDocTags: () => ["arbi", "doctags", "assign"],
|
|
113
|
+
removeDocTags: () => ["arbi", "doctags", "remove"],
|
|
114
|
+
updateDocTag: () => ["arbi", "doctags", "update"],
|
|
115
|
+
generateDocTags: () => ["arbi", "doctags", "generate"]
|
|
97
116
|
};
|
|
98
117
|
function useWorkspaceUsers(workspaceId, options) {
|
|
99
118
|
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
@@ -116,14 +135,6 @@ function useWorkspaces(options) {
|
|
|
116
135
|
enabled: options?.enabled ?? arbi.isLoggedIn
|
|
117
136
|
});
|
|
118
137
|
}
|
|
119
|
-
function useDocuments(workspaceId, options) {
|
|
120
|
-
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
121
|
-
return reactQuery.useQuery({
|
|
122
|
-
queryKey: arbiQueryKeys.documents(workspaceId ?? ""),
|
|
123
|
-
queryFn: () => arbi.documents.list(),
|
|
124
|
-
enabled: (options?.enabled ?? true) && !!workspaceId && arbi.hasWorkspace
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
138
|
function useDocumentVersionList(docId, options) {
|
|
128
139
|
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
129
140
|
return reactQuery.useQuery({
|
|
@@ -417,11 +428,458 @@ function useUserUsageToday(options) {
|
|
|
417
428
|
staleTime: 15e3
|
|
418
429
|
});
|
|
419
430
|
}
|
|
431
|
+
function openWorkspaceId(arbi) {
|
|
432
|
+
return arbi.hasWorkspace ? arbi.getWorkspaceId() : null;
|
|
433
|
+
}
|
|
434
|
+
function patchWorkspaceTags(qc, workspaceId, reducer) {
|
|
435
|
+
const key = arbiQueryKeys.tags(workspaceId);
|
|
436
|
+
const current = qc.getQueryData(key);
|
|
437
|
+
if (!current) return;
|
|
438
|
+
const next = reducer(current);
|
|
439
|
+
if (next !== current) qc.setQueryData(key, next);
|
|
440
|
+
}
|
|
441
|
+
function invalidateTagTables(qc, workspaceId) {
|
|
442
|
+
return qc.invalidateQueries({ queryKey: arbiQueryKeys.tagLists(workspaceId) });
|
|
443
|
+
}
|
|
444
|
+
function invalidateTagData(qc, workspaceId) {
|
|
445
|
+
return Promise.all([
|
|
446
|
+
qc.invalidateQueries({ queryKey: arbiQueryKeys.tags(workspaceId) }),
|
|
447
|
+
qc.invalidateQueries({ queryKey: arbiQueryKeys.facts(workspaceId) })
|
|
448
|
+
]);
|
|
449
|
+
}
|
|
450
|
+
function useInvalidateTagData() {
|
|
451
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
452
|
+
const qc = reactQuery.useQueryClient();
|
|
453
|
+
return react.useCallback(() => {
|
|
454
|
+
const workspaceId = openWorkspaceId(arbi);
|
|
455
|
+
return workspaceId ? invalidateTagData(qc, workspaceId) : Promise.resolve();
|
|
456
|
+
}, [arbi, qc]);
|
|
457
|
+
}
|
|
458
|
+
function settleCreatedTag(qc, created) {
|
|
459
|
+
const workspaceId = created.workspace_ext_id;
|
|
460
|
+
patchWorkspaceTags(
|
|
461
|
+
qc,
|
|
462
|
+
workspaceId,
|
|
463
|
+
(old) => old.some((t) => t.external_id === created.external_id) ? old : [...old, created]
|
|
464
|
+
);
|
|
465
|
+
return invalidateTagTables(qc, workspaceId);
|
|
466
|
+
}
|
|
467
|
+
function settleChangedTag(qc, tag) {
|
|
468
|
+
const workspaceId = tag.workspace_ext_id;
|
|
469
|
+
patchWorkspaceTags(
|
|
470
|
+
qc,
|
|
471
|
+
workspaceId,
|
|
472
|
+
(old) => old.map((t) => t.external_id === tag.external_id ? { ...t, ...tag } : t)
|
|
473
|
+
);
|
|
474
|
+
return invalidateTagTables(qc, workspaceId);
|
|
475
|
+
}
|
|
476
|
+
var replayableTagMutations = [
|
|
477
|
+
{
|
|
478
|
+
mutationKey: arbiMutationKeys.createTag(),
|
|
479
|
+
run: (client, input) => browser.tags.createTag(client, input),
|
|
480
|
+
settle: (qc, created) => settleCreatedTag(qc, created)
|
|
481
|
+
}
|
|
482
|
+
];
|
|
483
|
+
function useCreateTag() {
|
|
484
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
485
|
+
const qc = reactQuery.useQueryClient();
|
|
486
|
+
return reactQuery.useMutation({
|
|
487
|
+
mutationKey: arbiMutationKeys.createTag(),
|
|
488
|
+
mutationFn: (input) => arbi.tags.create(input),
|
|
489
|
+
onSuccess: (created) => settleCreatedTag(qc, created)
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
function useUpdateTag() {
|
|
493
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
494
|
+
const qc = reactQuery.useQueryClient();
|
|
495
|
+
return reactQuery.useMutation({
|
|
496
|
+
mutationKey: arbiMutationKeys.updateTag(),
|
|
497
|
+
mutationFn: ({ tagId, change }) => arbi.tags.update(tagId, change),
|
|
498
|
+
onSuccess: (tag) => settleChangedTag(qc, tag)
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
function useSetTagValues() {
|
|
502
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
503
|
+
const qc = reactQuery.useQueryClient();
|
|
504
|
+
return reactQuery.useMutation({
|
|
505
|
+
mutationKey: arbiMutationKeys.updateTag(),
|
|
506
|
+
mutationFn: ({
|
|
507
|
+
tagId,
|
|
508
|
+
values
|
|
509
|
+
}) => arbi.tags.setValues(tagId, values),
|
|
510
|
+
onSuccess: (tag) => settleChangedTag(qc, tag)
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
function useDeleteTag() {
|
|
514
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
515
|
+
const qc = reactQuery.useQueryClient();
|
|
516
|
+
return reactQuery.useMutation({
|
|
517
|
+
mutationKey: arbiMutationKeys.deleteTag(),
|
|
518
|
+
mutationFn: (tagId) => arbi.tags.delete(tagId),
|
|
519
|
+
onSuccess: (_data, tagId) => {
|
|
520
|
+
const workspaceId = openWorkspaceId(arbi);
|
|
521
|
+
if (!workspaceId) return;
|
|
522
|
+
patchWorkspaceTags(
|
|
523
|
+
qc,
|
|
524
|
+
workspaceId,
|
|
525
|
+
(old) => old.filter((t) => t.external_id !== tagId).map((t) => t.parent_ext_id === tagId ? { ...t, parent_ext_id: null } : t)
|
|
526
|
+
);
|
|
527
|
+
return Promise.all([
|
|
528
|
+
invalidateTagTables(qc, workspaceId),
|
|
529
|
+
qc.invalidateQueries({ queryKey: arbiQueryKeys.facts(workspaceId) })
|
|
530
|
+
]);
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
function useExtractEntities() {
|
|
535
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
536
|
+
return reactQuery.useMutation({
|
|
537
|
+
mutationKey: arbiMutationKeys.extractEntities(),
|
|
538
|
+
mutationFn: (docIds) => arbi.tags.extractEntities(docIds)
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
function useTagKindSchema(kind, options) {
|
|
542
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
543
|
+
const workspaceId = openWorkspaceId(arbi) ?? "";
|
|
544
|
+
return reactQuery.useQuery({
|
|
545
|
+
queryKey: arbiQueryKeys.tagKind(workspaceId, kind ?? ""),
|
|
546
|
+
queryFn: () => arbi.tagKinds.get(kind),
|
|
547
|
+
enabled: (options?.enabled ?? true) && !!kind && !!workspaceId
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
function useUpdateTagKindSchema() {
|
|
551
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
552
|
+
const qc = reactQuery.useQueryClient();
|
|
553
|
+
return reactQuery.useMutation({
|
|
554
|
+
mutationKey: arbiMutationKeys.updateTagKindSchema(),
|
|
555
|
+
mutationFn: ({
|
|
556
|
+
kind,
|
|
557
|
+
fields
|
|
558
|
+
}) => arbi.tagKinds.update(kind, fields),
|
|
559
|
+
onSuccess: (schema) => {
|
|
560
|
+
const workspaceId = openWorkspaceId(arbi);
|
|
561
|
+
if (!workspaceId) return;
|
|
562
|
+
qc.setQueryData(arbiQueryKeys.tagKind(workspaceId, schema.kind), schema);
|
|
563
|
+
return invalidateTagData(qc, workspaceId);
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// src/documents/documents-cache.ts
|
|
569
|
+
var rowListeners = /* @__PURE__ */ new Set();
|
|
570
|
+
function subscribeDocumentRows(listener) {
|
|
571
|
+
rowListeners.add(listener);
|
|
572
|
+
return () => {
|
|
573
|
+
rowListeners.delete(listener);
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
function getCachedDocuments(qc, workspaceId) {
|
|
577
|
+
if (!workspaceId) return void 0;
|
|
578
|
+
return qc.getQueryData(arbiQueryKeys.documents(workspaceId));
|
|
579
|
+
}
|
|
580
|
+
function cachedLists(qc) {
|
|
581
|
+
return qc.getQueriesData({ queryKey: arbiQueryKeys.allDocuments() }).flatMap(
|
|
582
|
+
([key, data]) => typeof key[2] === "string" && Array.isArray(data) ? [[key[2], data]] : []
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
function diffRows(workspaceId, before, after) {
|
|
586
|
+
const beforeById = new Map(before.map((doc) => [doc.external_id, doc]));
|
|
587
|
+
const add = [];
|
|
588
|
+
const update = [];
|
|
589
|
+
const seen = /* @__PURE__ */ new Set();
|
|
590
|
+
for (const doc of after) {
|
|
591
|
+
seen.add(doc.external_id);
|
|
592
|
+
const previous = beforeById.get(doc.external_id);
|
|
593
|
+
if (!previous) add.push(doc);
|
|
594
|
+
else if (previous !== doc) update.push(doc);
|
|
595
|
+
}
|
|
596
|
+
const remove = [...beforeById.keys()].filter((id) => !seen.has(id));
|
|
597
|
+
return { workspaceId, add, update, remove };
|
|
598
|
+
}
|
|
599
|
+
function patchDocuments(qc, reducer, workspaceId) {
|
|
600
|
+
const lists = workspaceId ? (() => {
|
|
601
|
+
const current = getCachedDocuments(qc, workspaceId);
|
|
602
|
+
return current ? [[workspaceId, current]] : [];
|
|
603
|
+
})() : cachedLists(qc);
|
|
604
|
+
for (const [id, before] of lists) {
|
|
605
|
+
const after = reducer(before);
|
|
606
|
+
if (after === before) continue;
|
|
607
|
+
qc.setQueryData(arbiQueryKeys.documents(id), after);
|
|
608
|
+
const change = diffRows(id, before, after);
|
|
609
|
+
const beforeById = new Map(before.map((doc) => [doc.external_id, doc]));
|
|
610
|
+
if (change.update.some(
|
|
611
|
+
(doc) => doc.current_version !== beforeById.get(doc.external_id)?.current_version
|
|
612
|
+
))
|
|
613
|
+
void qc.invalidateQueries({ queryKey: arbiQueryKeys.allDocumentVersions() });
|
|
614
|
+
if (change.add.length || change.update.length || change.remove.length)
|
|
615
|
+
for (const listener of rowListeners) listener(change);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function upsertDocuments(qc, documents, workspaceId) {
|
|
619
|
+
if (!documents.length) return;
|
|
620
|
+
const byId = new Map(documents.map((doc) => [doc.external_id, doc]));
|
|
621
|
+
const byWorkspace = /* @__PURE__ */ new Map();
|
|
622
|
+
for (const doc of documents) {
|
|
623
|
+
const ws = workspaceId ?? doc.workspace_ext_id;
|
|
624
|
+
byWorkspace.set(ws, [...byWorkspace.get(ws) ?? [], doc]);
|
|
625
|
+
}
|
|
626
|
+
for (const [ws, incoming] of byWorkspace) {
|
|
627
|
+
patchDocuments(
|
|
628
|
+
qc,
|
|
629
|
+
(old) => {
|
|
630
|
+
const known = new Set(old.map((doc) => doc.external_id));
|
|
631
|
+
return [
|
|
632
|
+
...old.map((doc) => byId.get(doc.external_id) ?? doc),
|
|
633
|
+
...incoming.filter((doc) => !known.has(doc.external_id))
|
|
634
|
+
];
|
|
635
|
+
},
|
|
636
|
+
ws
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
function setCachedDocument(qc, doc) {
|
|
641
|
+
qc.setQueryData(arbiQueryKeys.document(doc.external_id), doc);
|
|
642
|
+
}
|
|
643
|
+
function patchCachedDocument(qc, docId, reducer) {
|
|
644
|
+
const current = qc.getQueryData(arbiQueryKeys.document(docId));
|
|
645
|
+
if (current) qc.setQueryData(arbiQueryKeys.document(docId), reducer(current));
|
|
646
|
+
}
|
|
647
|
+
function forgetDocuments(qc, docIds) {
|
|
648
|
+
for (const id of docIds) {
|
|
649
|
+
qc.removeQueries({ queryKey: arbiQueryKeys.document(id) });
|
|
650
|
+
qc.removeQueries({ queryKey: arbiQueryKeys.documentVersions(id) });
|
|
651
|
+
qc.removeQueries({ queryKey: arbiQueryKeys.parsedDocument(id) });
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
var DOCUMENTS_FIRST_PAGE_SIZE = 500;
|
|
655
|
+
var DOCUMENTS_PAGE_SIZE = 2e3;
|
|
656
|
+
var DOCUMENTS_FIRST_PAGE_MEASURE = "arbi:document-list:page1";
|
|
657
|
+
var FIRST_PAGE_MEASURE = DOCUMENTS_FIRST_PAGE_MEASURE;
|
|
658
|
+
var FIRST_PAGE_START = "arbi:document-list:start";
|
|
659
|
+
function mark(name) {
|
|
660
|
+
try {
|
|
661
|
+
performance.mark(name);
|
|
662
|
+
} catch {
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
function measure(name, start) {
|
|
666
|
+
try {
|
|
667
|
+
performance.measure(name, start);
|
|
668
|
+
} catch {
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
var backgroundReads = /* @__PURE__ */ new Map();
|
|
672
|
+
var loadingListeners = /* @__PURE__ */ new Set();
|
|
673
|
+
function emitLoading() {
|
|
674
|
+
for (const listener of loadingListeners) listener();
|
|
675
|
+
}
|
|
676
|
+
function subscribeLoading(listener) {
|
|
677
|
+
loadingListeners.add(listener);
|
|
678
|
+
return () => {
|
|
679
|
+
loadingListeners.delete(listener);
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
function isDocumentsLoading() {
|
|
683
|
+
return backgroundReads.size > 0;
|
|
684
|
+
}
|
|
685
|
+
function useDocumentsLoading() {
|
|
686
|
+
return react.useSyncExternalStore(subscribeLoading, isDocumentsLoading, () => false);
|
|
687
|
+
}
|
|
688
|
+
function stopDocumentsLoading(workspaceId) {
|
|
689
|
+
const controller = backgroundReads.get(workspaceId);
|
|
690
|
+
if (!controller) return;
|
|
691
|
+
controller.abort();
|
|
692
|
+
backgroundReads.delete(workspaceId);
|
|
693
|
+
emitLoading();
|
|
694
|
+
}
|
|
695
|
+
async function loadDocuments(qc, arbi, workspaceId, signal) {
|
|
696
|
+
let stopped = false;
|
|
697
|
+
for (const [id, controller2] of backgroundReads) {
|
|
698
|
+
if (id === workspaceId) continue;
|
|
699
|
+
controller2.abort();
|
|
700
|
+
backgroundReads.delete(id);
|
|
701
|
+
stopped = true;
|
|
702
|
+
}
|
|
703
|
+
if (stopped) emitLoading();
|
|
704
|
+
const controller = new AbortController();
|
|
705
|
+
signal?.addEventListener("abort", () => controller.abort(), { once: true });
|
|
706
|
+
const pages = arbi.documents.listPaginated({
|
|
707
|
+
pageSize: DOCUMENTS_PAGE_SIZE,
|
|
708
|
+
firstPageSize: DOCUMENTS_FIRST_PAGE_SIZE,
|
|
709
|
+
signal: controller.signal
|
|
710
|
+
});
|
|
711
|
+
mark(FIRST_PAGE_START);
|
|
712
|
+
const first = await pages.next();
|
|
713
|
+
const firstPage = first.done ? [] : first.value;
|
|
714
|
+
measure(FIRST_PAGE_MEASURE, FIRST_PAGE_START);
|
|
715
|
+
if (first.done || firstPage.length < DOCUMENTS_FIRST_PAGE_SIZE) return firstPage;
|
|
716
|
+
if (backgroundReads.has(workspaceId)) {
|
|
717
|
+
controller.abort();
|
|
718
|
+
return firstPage;
|
|
719
|
+
}
|
|
720
|
+
backgroundReads.set(workspaceId, controller);
|
|
721
|
+
emitLoading();
|
|
722
|
+
void appendRemainingPages(qc, workspaceId, pages, controller.signal).finally(() => {
|
|
723
|
+
if (backgroundReads.get(workspaceId) !== controller) return;
|
|
724
|
+
backgroundReads.delete(workspaceId);
|
|
725
|
+
emitLoading();
|
|
726
|
+
});
|
|
727
|
+
return firstPage;
|
|
728
|
+
}
|
|
729
|
+
async function appendRemainingPages(qc, workspaceId, pages, signal) {
|
|
730
|
+
await Promise.resolve();
|
|
731
|
+
try {
|
|
732
|
+
for await (const page of pages) {
|
|
733
|
+
if (signal.aborted) return;
|
|
734
|
+
patchDocuments(
|
|
735
|
+
qc,
|
|
736
|
+
(old) => {
|
|
737
|
+
const known = new Set(old.map((doc) => doc.external_id));
|
|
738
|
+
const fresh = page.filter((doc) => !known.has(doc.external_id));
|
|
739
|
+
return fresh.length ? [...old, ...fresh] : old;
|
|
740
|
+
},
|
|
741
|
+
workspaceId
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
} catch (error) {
|
|
745
|
+
if (!signal.aborted) console.error(`Reading the documents of ${workspaceId} failed:`, error);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
var FINISHED_DOCUMENT_STATUSES = /* @__PURE__ */ new Set([
|
|
749
|
+
"completed",
|
|
750
|
+
"low-content",
|
|
751
|
+
"failed",
|
|
752
|
+
"skipped",
|
|
753
|
+
"empty"
|
|
754
|
+
]);
|
|
755
|
+
function openWorkspaceId2(arbi) {
|
|
756
|
+
return arbi.hasWorkspace ? arbi.getWorkspaceId() : null;
|
|
757
|
+
}
|
|
758
|
+
function useDocuments(workspaceId, options) {
|
|
759
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
760
|
+
const qc = reactQuery.useQueryClient();
|
|
761
|
+
const workspace = workspaceId ?? openWorkspaceId2(arbi) ?? "";
|
|
762
|
+
return reactQuery.useQuery({
|
|
763
|
+
queryKey: arbiQueryKeys.documents(workspace),
|
|
764
|
+
queryFn: ({ signal }) => {
|
|
765
|
+
if (openWorkspaceId2(arbi) !== workspace)
|
|
766
|
+
return Promise.reject(new Error("The selected workspace changed."));
|
|
767
|
+
return loadDocuments(qc, arbi, workspace, signal);
|
|
768
|
+
},
|
|
769
|
+
enabled: (options?.enabled ?? true) && !!workspace && arbi.hasWorkspace,
|
|
770
|
+
staleTime: Infinity,
|
|
771
|
+
refetchOnMount: false,
|
|
772
|
+
refetchOnReconnect: false,
|
|
773
|
+
refetchOnWindowFocus: false
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
var DOCUMENT_STATUS_POLL_MS = 4e3;
|
|
777
|
+
function processingInterval(doc) {
|
|
778
|
+
return doc?.status && !FINISHED_DOCUMENT_STATUSES.has(doc.status) ? DOCUMENT_STATUS_POLL_MS : false;
|
|
779
|
+
}
|
|
780
|
+
function useDocument(docId, options) {
|
|
781
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
782
|
+
return reactQuery.useQuery({
|
|
783
|
+
queryKey: arbiQueryKeys.document(docId ?? ""),
|
|
784
|
+
queryFn: async () => (await arbi.documents.get([docId]))[0] ?? null,
|
|
785
|
+
enabled: (options?.enabled ?? true) && !!docId && arbi.isLoggedIn,
|
|
786
|
+
refetchInterval: options?.pollWhileProcessing ? (query) => processingInterval(query.state.data) : false
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
function useDocumentsById(docIds, options) {
|
|
790
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
791
|
+
return reactQuery.useQuery({
|
|
792
|
+
queryKey: arbiQueryKeys.documentsById(docIds),
|
|
793
|
+
queryFn: () => arbi.documents.get(docIds),
|
|
794
|
+
enabled: (options?.enabled ?? true) && docIds.length > 0 && arbi.isLoggedIn
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
var SIMILARITY_THRESHOLD = 0.98;
|
|
798
|
+
function useSimilarDocuments(options) {
|
|
799
|
+
const arbi = chunkR2MF7YCW_cjs.useArbi();
|
|
800
|
+
const workspace = openWorkspaceId2(arbi) ?? "";
|
|
801
|
+
return reactQuery.useQuery({
|
|
802
|
+
queryKey: arbiQueryKeys.similarDocuments(workspace, SIMILARITY_THRESHOLD),
|
|
803
|
+
queryFn: () => arbi.documents.similar({ threshold: SIMILARITY_THRESHOLD }),
|
|
804
|
+
enabled: (options?.enabled ?? true) && !!workspace
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
var readSeq = 0;
|
|
808
|
+
var latestReadByDoc = /* @__PURE__ */ new Map();
|
|
809
|
+
async function refreshDocuments(qc, arbi, docIds, options) {
|
|
810
|
+
if (!docIds.length) return [];
|
|
811
|
+
const seq = ++readSeq;
|
|
812
|
+
for (const id of docIds) latestReadByDoc.set(id, seq);
|
|
813
|
+
try {
|
|
814
|
+
const read = await arbi.documents.get(docIds);
|
|
815
|
+
const fresh = read.filter((doc) => latestReadByDoc.get(doc.external_id) === seq).map((doc) => {
|
|
816
|
+
const live = options?.processingStatus?.(doc.external_id);
|
|
817
|
+
return live && FINISHED_DOCUMENT_STATUSES.has(live) && doc.status !== live ? { ...doc, status: live } : doc;
|
|
818
|
+
});
|
|
819
|
+
if (!fresh.length) return [];
|
|
820
|
+
reconcileTagCounts(qc, fresh);
|
|
821
|
+
upsertDocuments(qc, fresh);
|
|
822
|
+
for (const doc of fresh) setCachedDocument(qc, doc);
|
|
823
|
+
return fresh;
|
|
824
|
+
} catch (error) {
|
|
825
|
+
console.error("Re-reading documents failed; the cached ones are kept:", error);
|
|
826
|
+
return [];
|
|
827
|
+
} finally {
|
|
828
|
+
for (const id of docIds) if (latestReadByDoc.get(id) === seq) latestReadByDoc.delete(id);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
function reconcileTagCounts(qc, fresh) {
|
|
832
|
+
const byWorkspace = /* @__PURE__ */ new Map();
|
|
833
|
+
for (const doc of fresh) {
|
|
834
|
+
const cached = getCachedDocuments(qc, doc.workspace_ext_id)?.find(
|
|
835
|
+
(d) => d.external_id === doc.external_id
|
|
836
|
+
);
|
|
837
|
+
const before = new Set((cached?.doctags ?? []).map((dt) => dt.tag_ext_id));
|
|
838
|
+
const after = new Set((doc.doctags ?? []).map((dt) => dt.tag_ext_id));
|
|
839
|
+
const delta = byWorkspace.get(doc.workspace_ext_id) ?? /* @__PURE__ */ new Map();
|
|
840
|
+
for (const id of after) if (!before.has(id)) delta.set(id, (delta.get(id) ?? 0) + 1);
|
|
841
|
+
for (const id of before) if (!after.has(id)) delta.set(id, (delta.get(id) ?? 0) - 1);
|
|
842
|
+
byWorkspace.set(doc.workspace_ext_id, delta);
|
|
843
|
+
}
|
|
844
|
+
for (const [workspaceId, delta] of byWorkspace) adjustTagCounts(qc, workspaceId, delta);
|
|
845
|
+
}
|
|
846
|
+
function adjustTagCounts(qc, workspaceId, delta) {
|
|
847
|
+
if (![...delta.values()].some(Boolean)) return;
|
|
848
|
+
patchWorkspaceTags(
|
|
849
|
+
qc,
|
|
850
|
+
workspaceId,
|
|
851
|
+
(tags) => tags.map((tag) => {
|
|
852
|
+
const change = delta.get(tag.external_id);
|
|
853
|
+
return change ? { ...tag, doctag_count: Math.max(0, tag.doctag_count + change) } : tag;
|
|
854
|
+
})
|
|
855
|
+
);
|
|
856
|
+
void invalidateTagTables(qc, workspaceId);
|
|
857
|
+
}
|
|
420
858
|
|
|
859
|
+
exports.DOCUMENTS_FIRST_PAGE_MEASURE = DOCUMENTS_FIRST_PAGE_MEASURE;
|
|
860
|
+
exports.DOCUMENTS_FIRST_PAGE_SIZE = DOCUMENTS_FIRST_PAGE_SIZE;
|
|
861
|
+
exports.DOCUMENTS_PAGE_SIZE = DOCUMENTS_PAGE_SIZE;
|
|
862
|
+
exports.FINISHED_DOCUMENT_STATUSES = FINISHED_DOCUMENT_STATUSES;
|
|
421
863
|
exports.PROJECT_CREATION_DENIED_REASON = PROJECT_CREATION_DENIED_REASON;
|
|
864
|
+
exports.SIMILARITY_THRESHOLD = SIMILARITY_THRESHOLD;
|
|
865
|
+
exports.adjustTagCounts = adjustTagCounts;
|
|
422
866
|
exports.arbiMutationKeys = arbiMutationKeys;
|
|
423
867
|
exports.arbiQueryKeys = arbiQueryKeys;
|
|
868
|
+
exports.forgetDocuments = forgetDocuments;
|
|
869
|
+
exports.getCachedDocuments = getCachedDocuments;
|
|
870
|
+
exports.invalidateTagData = invalidateTagData;
|
|
871
|
+
exports.invalidateTagTables = invalidateTagTables;
|
|
872
|
+
exports.isDocumentsLoading = isDocumentsLoading;
|
|
424
873
|
exports.isUsageMetered = isUsageMetered;
|
|
874
|
+
exports.patchCachedDocument = patchCachedDocument;
|
|
875
|
+
exports.patchDocuments = patchDocuments;
|
|
876
|
+
exports.patchWorkspaceTags = patchWorkspaceTags;
|
|
877
|
+
exports.refreshDocuments = refreshDocuments;
|
|
878
|
+
exports.replayableTagMutations = replayableTagMutations;
|
|
879
|
+
exports.setCachedDocument = setCachedDocument;
|
|
880
|
+
exports.stopDocumentsLoading = stopDocumentsLoading;
|
|
881
|
+
exports.subscribeDocumentRows = subscribeDocumentRows;
|
|
882
|
+
exports.upsertDocuments = upsertDocuments;
|
|
425
883
|
exports.useActivity = useActivity;
|
|
426
884
|
exports.useAgents = useAgents;
|
|
427
885
|
exports.useAssistantQuery = useAssistantQuery;
|
|
@@ -430,23 +888,35 @@ exports.useCanCreateProjects = useCanCreateProjects;
|
|
|
430
888
|
exports.useConfigs = useConfigs;
|
|
431
889
|
exports.useConversationThreads = useConversationThreads;
|
|
432
890
|
exports.useConversations = useConversations;
|
|
891
|
+
exports.useCreateTag = useCreateTag;
|
|
433
892
|
exports.useCurrentUser = useCurrentUser;
|
|
434
893
|
exports.useDeleteAgents = useDeleteAgents;
|
|
894
|
+
exports.useDeleteTag = useDeleteTag;
|
|
895
|
+
exports.useDocument = useDocument;
|
|
435
896
|
exports.useDocumentVersionList = useDocumentVersionList;
|
|
436
897
|
exports.useDocuments = useDocuments;
|
|
898
|
+
exports.useDocumentsById = useDocumentsById;
|
|
899
|
+
exports.useDocumentsLoading = useDocumentsLoading;
|
|
900
|
+
exports.useExtractEntities = useExtractEntities;
|
|
437
901
|
exports.useFirmPool = useFirmPool;
|
|
438
902
|
exports.useHealth = useHealth;
|
|
903
|
+
exports.useInvalidateTagData = useInvalidateTagData;
|
|
439
904
|
exports.useParsedDocument = useParsedDocument;
|
|
440
905
|
exports.useProjectInvoices = useProjectInvoices;
|
|
441
906
|
exports.useProjectUsage = useProjectUsage;
|
|
442
907
|
exports.useProjects = useProjects;
|
|
443
908
|
exports.useProjectsUsage = useProjectsUsage;
|
|
909
|
+
exports.useSetTagValues = useSetTagValues;
|
|
910
|
+
exports.useSimilarDocuments = useSimilarDocuments;
|
|
911
|
+
exports.useTagKindSchema = useTagKindSchema;
|
|
444
912
|
exports.useTags = useTags;
|
|
445
913
|
exports.useThumbnails = useThumbnails;
|
|
446
914
|
exports.useTopUpFirmPool = useTopUpFirmPool;
|
|
915
|
+
exports.useUpdateTag = useUpdateTag;
|
|
916
|
+
exports.useUpdateTagKindSchema = useUpdateTagKindSchema;
|
|
447
917
|
exports.useUserUsageToday = useUserUsageToday;
|
|
448
918
|
exports.useWorkspaceActivity = useWorkspaceActivity;
|
|
449
919
|
exports.useWorkspaceUsers = useWorkspaceUsers;
|
|
450
920
|
exports.useWorkspaces = useWorkspaces;
|
|
451
|
-
//# sourceMappingURL=chunk-
|
|
452
|
-
//# sourceMappingURL=chunk-
|
|
921
|
+
//# sourceMappingURL=chunk-CG6USBJX.cjs.map
|
|
922
|
+
//# sourceMappingURL=chunk-CG6USBJX.cjs.map
|