@opengeni/documents 0.5.41 → 0.7.0-canary.0

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/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { Settings } from "@opengeni/config";
2
- import { type AddDocumentRequest, CreateDocumentBaseRequest, Document, DocumentAuthorityKind, DocumentBase, DocumentCurationStatus, DocumentSearchMode, DocumentSearchResult, DocumentStatus, DocumentVisibility, FileAsset, KnowledgeBrowseResponse, KnowledgeRecord, KnowledgeSearchResponse, KnowledgeSourceKind, ListIndexedDocumentsResponse } from "@opengeni/contracts";
2
+ import { type AddDocumentRequest, CreateDocumentBaseRequest, Document, DocumentAuthorityKind, DocumentBase, DocumentCurationStatus, DocumentSearchMode, DocumentSearchResult, DocumentStatus, DocumentVisibility, FileAsset, KnowledgeBrowseResponse, KnowledgeRecord, KnowledgeSearchResponse, KnowledgeSourceKind, ListIndexedDocumentsResponse, type ReclassifyDocumentAuthorityRequest, type RunDocumentDefaultCollectionBackfillRequest } from "@opengeni/contracts";
3
3
  import { type Database } from "@opengeni/db";
4
- import type { ObjectStorage } from "@opengeni/storage";
4
+ import { type ObjectStorage } from "@opengeni/storage";
5
5
  export { projectKnowledgeRecord } from "./knowledge-projection.js";
6
6
  export declare const DEFAULT_DOCUMENT_PARSER = "liteparse";
7
7
  export declare const DEFAULT_DOCUMENT_EMBEDDING_MODEL = "text-embedding-3-large";
@@ -84,12 +84,51 @@ export type DocumentAccessFilter = {
84
84
  * only when the agent carries the creating subject as its viewer subject.
85
85
  */
86
86
  agentOnly?: boolean | undefined;
87
+ /** Exact attempt whose grant snapshot must be revalidated in the content query. */
88
+ authorizedPersonalAttempt?: {
89
+ accountId: string;
90
+ workspaceId: string;
91
+ sessionId: string;
92
+ attemptId: string;
93
+ } | undefined;
87
94
  };
88
95
  export type DocumentAuthority = {
89
96
  kind: DocumentAuthorityKind;
90
97
  workspaceId: string | null;
91
98
  subjectId: string | null;
92
99
  };
100
+ /** JSON-safe projection of the opaque capability minted by the API access layer. */
101
+ export type DocumentAccountAdminAuthorization = Readonly<{
102
+ authorizationId: string;
103
+ accountId: string;
104
+ actorSubjectId: string;
105
+ permission: "account:admin";
106
+ }>;
107
+ export type ReclassifyDocumentAuthorityInput = ReclassifyDocumentAuthorityRequest & {
108
+ accountId: string;
109
+ workspaceId: string;
110
+ documentId: string;
111
+ actorSubjectId: string;
112
+ accountAdminAuthorization: DocumentAccountAdminAuthorization | null;
113
+ };
114
+ export type RunDocumentDefaultCollectionBackfillInput = RunDocumentDefaultCollectionBackfillRequest & {
115
+ accountId: string;
116
+ workspaceId: string;
117
+ actorSubjectId: string;
118
+ accountAdminAuthorization: DocumentAccountAdminAuthorization;
119
+ };
120
+ export type DocumentMigrationAuditInput = {
121
+ accountId: string;
122
+ workspaceId: string;
123
+ actorSubjectId: string;
124
+ accountAdminAuthorization: DocumentAccountAdminAuthorization;
125
+ limit?: number | undefined;
126
+ cursor?: string | undefined;
127
+ };
128
+ export type AgentDocumentAuthorityContext = {
129
+ sessionId: string;
130
+ attemptId: string;
131
+ };
93
132
  export type DocumentInventoryStatusCounts = Record<DocumentStatus, number>;
94
133
  export type DocumentInventorySourceKindCounts = Record<KnowledgeSourceKind, number>;
95
134
  export type DocumentInventoryAuthorityKindCounts = Record<DocumentAuthorityKind, number>;
@@ -135,6 +174,7 @@ export type EffectiveDocumentSearchInput = Omit<DocumentSearchInput, "access"> &
135
174
  initiatingSubjectId: string;
136
175
  /** Agent retrieval additionally enforces documents.agent_access. */
137
176
  surface: "human" | "agent";
177
+ agentAuthority?: AgentDocumentAuthorityContext | undefined;
138
178
  };
139
179
  export type ListEffectiveIndexedDocumentsInput = {
140
180
  accountId: string;
@@ -143,18 +183,22 @@ export type ListEffectiveIndexedDocumentsInput = {
143
183
  initiatingSubjectId: string;
144
184
  checkpoint?: string | undefined;
145
185
  limit?: number | undefined;
186
+ agentAuthority?: AgentDocumentAuthorityContext | undefined;
146
187
  };
147
188
  export type EffectiveKnowledgeBrowseInput = {
148
189
  accountId: string;
149
190
  workspaceId: string;
150
191
  /** Immutable human subject accepted for the logical request/turn. */
151
192
  initiatingSubjectId: string;
193
+ /** Human inspection bypasses agent_access but still uses exact subject/RLS authority. */
194
+ surface?: "human" | "agent" | undefined;
152
195
  /** Omit to browse top-level documents; pass a document record id for chunks. */
153
196
  parentId?: string | undefined;
154
197
  topic?: string | undefined;
155
198
  sourceKinds?: KnowledgeSourceKind[] | undefined;
156
199
  cursor?: string | undefined;
157
200
  limit?: number | undefined;
201
+ agentAuthority?: AgentDocumentAuthorityContext | undefined;
158
202
  };
159
203
  export type DocumentIndexHooks = {
160
204
  beforeEmbed?: (input: {
@@ -275,6 +319,142 @@ export declare function listDocumentBasesEnsuringDefault(db: Database, input: {
275
319
  accountId: string;
276
320
  workspaceId: string;
277
321
  }): Promise<DocumentBase[]>;
322
+ export declare function runDocumentDefaultCollectionBackfill(db: Database, input: RunDocumentDefaultCollectionBackfillInput): Promise<{
323
+ runId: string;
324
+ operationId: string;
325
+ status: "completed" | "running";
326
+ lastWorkspaceId: string | null;
327
+ processedCount: number;
328
+ createdCount: number;
329
+ adoptedCount: number;
330
+ completedAt: string | null;
331
+ }>;
332
+ export declare function listDocumentDefaultCollectionBackfillRuns(db: Database, input: DocumentMigrationAuditInput): Promise<{
333
+ runs: {
334
+ runId: string;
335
+ status: "completed" | "running";
336
+ lastWorkspaceId: string | null;
337
+ processedCount: number;
338
+ createdCount: number;
339
+ adoptedCount: number;
340
+ completedAt: string | null;
341
+ actorSubjectId: string;
342
+ startedAt: string;
343
+ updatedAt: string;
344
+ }[];
345
+ hasMore: boolean;
346
+ nextCursor: string | null;
347
+ }>;
348
+ export declare function getDocumentDefaultCollectionBackfillAudit(db: Database, input: DocumentMigrationAuditInput & {
349
+ runId: string;
350
+ operationCursor?: string | undefined;
351
+ receiptCursor?: string | undefined;
352
+ }): Promise<{
353
+ run: {
354
+ runId: string;
355
+ status: "completed" | "running";
356
+ lastWorkspaceId: string | null;
357
+ processedCount: number;
358
+ createdCount: number;
359
+ adoptedCount: number;
360
+ completedAt: string | null;
361
+ actorSubjectId: string;
362
+ startedAt: string;
363
+ updatedAt: string;
364
+ };
365
+ operations: {
366
+ operationId: string;
367
+ result: {
368
+ runId: string;
369
+ operationId: string;
370
+ status: "completed" | "running";
371
+ lastWorkspaceId: string | null;
372
+ processedCount: number;
373
+ createdCount: number;
374
+ adoptedCount: number;
375
+ completedAt: string | null;
376
+ };
377
+ createdAt: string;
378
+ }[];
379
+ receipts: {
380
+ workspaceId: string;
381
+ baseId: string;
382
+ outcome: "adopted" | "created";
383
+ createdAt: string;
384
+ }[];
385
+ operationsHasMore: boolean;
386
+ operationsNextCursor: string | null;
387
+ receiptsHasMore: boolean;
388
+ receiptsNextCursor: string | null;
389
+ }>;
390
+ export declare function listOrganizationDocumentAuthorityReclassifications(db: Database, input: DocumentMigrationAuditInput): Promise<{
391
+ receipts: {
392
+ operationId: string;
393
+ documentId: string;
394
+ previousAuthority: {
395
+ kind: "organization" | "personal" | "workspace";
396
+ workspaceId: string | null;
397
+ subjectId: string | null;
398
+ authorityId: string | null;
399
+ };
400
+ authority: {
401
+ kind: "organization" | "personal" | "workspace";
402
+ workspaceId: string | null;
403
+ subjectId: string | null;
404
+ authorityId: string | null;
405
+ };
406
+ createdAt: string;
407
+ actorSubjectId: string;
408
+ requestWorkspaceId: string;
409
+ }[];
410
+ hasMore: boolean;
411
+ nextCursor: string | null;
412
+ }>;
413
+ export declare function reclassifyDocumentAuthority(db: Database, input: ReclassifyDocumentAuthorityInput): Promise<{
414
+ operationId: string;
415
+ documentId: string;
416
+ previousAuthority: {
417
+ kind: "organization" | "personal" | "workspace";
418
+ workspaceId: string | null;
419
+ subjectId: string | null;
420
+ authorityId: string | null;
421
+ };
422
+ authority: {
423
+ kind: "organization" | "personal" | "workspace";
424
+ workspaceId: string | null;
425
+ subjectId: string | null;
426
+ authorityId: string | null;
427
+ };
428
+ createdAt: string;
429
+ }>;
430
+ export declare function listDocumentAuthorityReclassifications(db: Database, input: {
431
+ accountId: string;
432
+ workspaceId: string;
433
+ documentId: string;
434
+ actorSubjectId: string;
435
+ limit?: number | undefined;
436
+ cursor?: string | undefined;
437
+ }): Promise<{
438
+ receipts: {
439
+ operationId: string;
440
+ documentId: string;
441
+ previousAuthority: {
442
+ kind: "organization" | "personal" | "workspace";
443
+ workspaceId: string | null;
444
+ subjectId: string | null;
445
+ authorityId: string | null;
446
+ };
447
+ authority: {
448
+ kind: "organization" | "personal" | "workspace";
449
+ workspaceId: string | null;
450
+ subjectId: string | null;
451
+ authorityId: string | null;
452
+ };
453
+ createdAt: string;
454
+ }[];
455
+ hasMore: boolean;
456
+ nextCursor: string | null;
457
+ }>;
278
458
  export declare function addDocumentToBase(db: Database, input: AddDocumentRequest & {
279
459
  accountId: string;
280
460
  workspaceId: string;
@@ -308,6 +488,14 @@ export declare function deleteDocumentFromBase(db: Database, input: {
308
488
  access?: DocumentAccessFilter | undefined;
309
489
  }): Promise<void>;
310
490
  export declare function listDocuments(db: Database, workspaceId: string, baseId: string, access?: DocumentAccessFilter): Promise<Document[]>;
491
+ /**
492
+ * List Documents the human can discover and manage from the requested
493
+ * workspace. Organization Documents are account-wide, workspace Documents are
494
+ * local, activated organization-user Documents are portable across the owner's
495
+ * same-organization workspaces, and legacy personal rows remain anchored to
496
+ * their ingestion workspace through documentAccessConditions.
497
+ */
498
+ export declare function listAccessibleDocuments(db: Database, workspaceId: string, access: DocumentAccessFilter): Promise<Document[]>;
311
499
  /**
312
500
  * List newly ready documents in the same effective scope used by agent
313
501
  * retrieval. The opaque checkpoint is bound to the account, requesting
@@ -327,6 +515,17 @@ export declare function decodeDocumentIndexCheckpoint(value: string, scope: {
327
515
  initiatingSubjectId: string;
328
516
  }): bigint;
329
517
  export declare function getDocument(db: Database, workspaceId: string, documentId: string, access?: DocumentAccessFilter): Promise<Document | null>;
518
+ /**
519
+ * Resolve the immutable source file through Document authority in the requested
520
+ * workspace. The file remains physically owned by its ingestion workspace;
521
+ * callers never gain generic access to that workspace's file inventory.
522
+ */
523
+ export declare function getDocumentOriginalFile(db: Database, input: {
524
+ accountId: string;
525
+ workspaceId: string;
526
+ documentId: string;
527
+ access: DocumentAccessFilter;
528
+ }): Promise<FileAsset | null>;
330
529
  /**
331
530
  * Internal ingestion-only read used after the worker has independently resolved
332
531
  * and fenced the immutable document authority tuple. It deliberately does not
@@ -338,6 +537,14 @@ export declare function getDocumentForIndexing(db: Database, workspaceId: string
338
537
  export declare function queueDocumentForReindex(db: Database, workspaceId: string, documentId: string, access?: DocumentAccessFilter, organizationAuthorityGranted?: boolean): Promise<Document>;
339
538
  export declare function indexDocumentNow(db: Database, objectStorage: ObjectStorage, workspaceId: string, documentId: string, services?: DocumentServices, hooks?: DocumentIndexHooks, access?: DocumentAccessFilter): Promise<Document>;
340
539
  export declare function searchDocuments(db: Database, input: DocumentSearchInput, services?: Pick<DocumentServices, "embedder">): Promise<DocumentSearchResult[]>;
540
+ export type DocumentCandidateRelevanceFloor = {
541
+ vectorScore: number;
542
+ keywordScore: number;
543
+ };
544
+ export declare function selectDocumentSearchCandidateWindow(merged: DocumentSearchResult[], limit: number, relevanceFloor?: DocumentCandidateRelevanceFloor): {
545
+ results: DocumentSearchResult[];
546
+ belowRelevanceFloor: number;
547
+ };
341
548
  /**
342
549
  * Canonical effective retrieval composition for API, SDK, and MCP surfaces.
343
550
  * Callers supply the already-authorized immutable initiating subject; no
@@ -351,12 +558,48 @@ export declare function searchEffectiveDocuments(db: Database, input: EffectiveD
351
558
  * ranking and response construction disappears rather than leaking stale data.
352
559
  */
353
560
  export declare function searchEffectiveKnowledge(db: Database, input: EffectiveDocumentSearchInput, services?: Pick<DocumentServices, "embedder">): Promise<KnowledgeSearchResponse>;
561
+ type KnowledgeSearchCandidate = {
562
+ record: KnowledgeRecord;
563
+ semanticScore: number;
564
+ matchType: DocumentSearchMode;
565
+ vectorScore: number | null;
566
+ keywordScore: number | null;
567
+ };
568
+ type KnowledgeSearchSelectionInput = {
569
+ candidates: KnowledgeSearchCandidate[];
570
+ rankedCandidateCount: number;
571
+ requestedLimit: number;
572
+ alreadyBelowRelevanceFloor?: number | undefined;
573
+ now?: Date | undefined;
574
+ };
575
+ /**
576
+ * Deterministic, content-safe final selection over already-authorized and
577
+ * freshly rechecked Knowledge candidates. Exported for boundary tests; callers
578
+ * must never use it as a substitute for the database authorization pass above.
579
+ */
580
+ export declare function selectKnowledgeSearchResults(input: KnowledgeSearchSelectionInput): KnowledgeSearchResponse;
581
+ type KnowledgeBrowseEntry = {
582
+ record: KnowledgeRecord;
583
+ cursorAfter: string;
584
+ };
585
+ /**
586
+ * Bound a browse page without skipping records. Tail records omitted for the
587
+ * response budget remain behind the returned cursor. If one complete record is
588
+ * itself too large, return a deterministic discovery projection; knowledge_get
589
+ * remains the freshly authorized full-record path.
590
+ */
591
+ export declare function selectKnowledgeBrowseRecords(input: {
592
+ entries: KnowledgeBrowseEntry[];
593
+ hasMoreAfterEntries: boolean;
594
+ }): KnowledgeBrowseResponse;
354
595
  /** Fetch one stable Knowledge record with a fresh authorization check. */
355
596
  export declare function getEffectiveKnowledgeRecord(db: Database, input: {
356
597
  accountId: string;
357
598
  workspaceId: string;
358
599
  initiatingSubjectId: string;
600
+ surface?: "human" | "agent" | undefined;
359
601
  id: string;
602
+ agentAuthority?: AgentDocumentAuthorityContext | undefined;
360
603
  }): Promise<KnowledgeRecord | null>;
361
604
  /**
362
605
  * Browse top-level authorized documents or the chunks of one authorized
@@ -369,6 +612,7 @@ type KnowledgeBrowseCursorScope = {
369
612
  workspaceId: string;
370
613
  initiatingSubjectId: string;
371
614
  parentId: string | null;
615
+ parentRevision?: string | null | undefined;
372
616
  topic: string | null;
373
617
  sourceKinds: readonly string[];
374
618
  };
@@ -381,17 +625,26 @@ export declare function resolveDocumentAuthority(input: {
381
625
  workspaceId: string;
382
626
  initiatingSubjectId?: string | null | undefined;
383
627
  }): DocumentAuthority;
628
+ export declare function resolveEffectiveDocumentAccess(_db: Database, input: {
629
+ accountId: string;
630
+ workspaceId: string;
631
+ initiatingSubjectId: string;
632
+ surface: "human" | "agent";
633
+ agentAuthority?: AgentDocumentAuthorityContext | undefined;
634
+ }): Promise<DocumentAccessFilter>;
384
635
  /**
385
636
  * Whether a single already-fetched document is readable in this workspace.
386
637
  *
387
- * Keep this compatibility predicate as strict as SQL/RLS: personal authority
388
- * remains anchored to its originating workspace, and unknown or incomplete
389
- * authority tuples deny instead of falling through as workspace-visible.
638
+ * Keep this compatibility predicate as strict as SQL/RLS: legacy personal
639
+ * authority remains origin-workspace anchored while activated personal
640
+ * authority has a null workspace and follows the exact owner within the org.
390
641
  */
391
642
  export declare function canViewDocument(document: Pick<DocumentAccessRecord, "authorityKind" | "authoritySubjectId"> & {
392
643
  authorityWorkspaceId?: string | null | undefined;
393
644
  }, viewerSubjectId: string | null | undefined, workspaceId?: string | null | undefined): boolean;
394
645
  type DocumentAccessRecord = {
646
+ id: string;
647
+ authorityId: string | null;
395
648
  authorityKind: string;
396
649
  authorityWorkspaceId: string | null;
397
650
  authoritySubjectId: string | null;