@ontos-ai/knowhere-sdk 0.8.0 → 0.10.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/README.md CHANGED
@@ -252,16 +252,22 @@ const updateJob = await client.jobs.create({
252
252
  documentId,
253
253
  });
254
254
 
255
- const documents = await client.documents.list({ namespace: 'support-center' });
255
+ const documents = await client.documents.list({
256
+ namespace: 'support-center',
257
+ page: 1,
258
+ pageSize: 50,
259
+ });
256
260
  const document = await client.documents.get(documentId);
257
261
  const chunks = await client.documents.listChunks(documentId, {
258
262
  page: 1,
259
263
  pageSize: 50,
260
- chunkType: 'text',
264
+ chunkType: 'image',
265
+ includeAssetUrls: true,
261
266
  });
262
267
  const archived = await client.documents.archive(documentId);
263
268
 
264
269
  console.log(documents.documents.length);
270
+ console.log(documents.pagination.totalPages);
265
271
  console.log(document.status);
266
272
  console.log(chunks.pagination.total);
267
273
  if (chunks.chunks[0]) {
@@ -269,6 +275,7 @@ if (chunks.chunks[0]) {
269
275
  includeAssetUrls: true,
270
276
  });
271
277
  console.log(chunk.chunk.content);
278
+ console.log(chunk.chunk.assetUrl);
272
279
  }
273
280
  console.log(archived.status);
274
281
  ```
@@ -313,6 +320,30 @@ console.log(serverSearch.references);
313
320
  Local grep and reads use the cached parse result, not server-side chunk scans.
314
321
  Search uses the Knowhere API retrieval query; local document IDs only help map
315
322
  returned server document IDs back to local cache IDs when available.
323
+ If a search result only has a published `documentId`, or an async parse flow only
324
+ has a completed `jobId`, read-oriented helpers can accept that remote identifier
325
+ directly and will sync the result into the local cache before reading. For a
326
+ `documentId`, the SDK resolves the document's current published `jobId` and then
327
+ downloads the parser result ZIP through the same `cacheJobResult(...)` path:
328
+
329
+ ```typescript
330
+ const remoteRead = await client.knowledge.readChunks({
331
+ documentId: 'doc_123',
332
+ sectionPath: 'Overview',
333
+ limit: 5,
334
+ });
335
+
336
+ const remoteOutline = await client.knowledge.getDocumentOutline({
337
+ documentId: 'doc_123',
338
+ });
339
+
340
+ const jobRead = await client.knowledge.readChunks({
341
+ jobId: jobResult.jobId,
342
+ localDocumentId: 'manual-v1',
343
+ limit: 5,
344
+ });
345
+ ```
346
+
316
347
  The MCP package is a wrapper over this SDK interface; install it only when an
317
348
  agent host needs an MCP server. See the MCP package README for Codex, Claude
318
349
  Code, Claude Desktop, and generic stdio MCP host configuration examples.
package/dist/index.d.mts CHANGED
@@ -799,6 +799,30 @@ interface Document {
799
799
  /** Archive timestamp, when archived */
800
800
  archivedAt?: Date;
801
801
  }
802
+ /**
803
+ * Pagination metadata returned by document list endpoints.
804
+ */
805
+ interface DocumentListPagination {
806
+ /** Current page number */
807
+ page: number;
808
+ /** Number of items requested per page */
809
+ pageSize: number;
810
+ /** Total matching documents */
811
+ total: number;
812
+ /** Total number of pages */
813
+ totalPages: number;
814
+ }
815
+ /**
816
+ * Query parameters for GET /v1/documents.
817
+ */
818
+ interface DocumentListParams {
819
+ /** Retrieval namespace */
820
+ namespace?: string;
821
+ /** Page number (default: 1) */
822
+ page?: number;
823
+ /** Items per page (default: 50, maximum: 200) */
824
+ pageSize?: number;
825
+ }
802
826
  /**
803
827
  * Response from GET /v1/documents.
804
828
  */
@@ -807,6 +831,8 @@ interface DocumentListResponse {
807
831
  namespace: string;
808
832
  /** Documents visible in the namespace */
809
833
  documents: Document[];
834
+ /** Pagination metadata */
835
+ pagination: DocumentListPagination;
810
836
  }
811
837
  /**
812
838
  * Document chunk types supported by document chunk endpoints.
@@ -835,14 +861,14 @@ interface DocumentChunkListParams {
835
861
  pageSize?: number;
836
862
  /** Optional chunk type filter */
837
863
  chunkType?: DocumentChunkType;
838
- /** Generate asset URLs for media chunks (default: false) */
864
+ /** Set true to include 7-day asset URLs for media chunks */
839
865
  includeAssetUrls?: boolean;
840
866
  }
841
867
  /**
842
868
  * Query parameters for GET /v1/documents/{document_id}/chunks/{document_chunk_id}.
843
869
  */
844
870
  interface DocumentChunkGetParams {
845
- /** Generate asset URLs for media chunks (default: false) */
871
+ /** Set true to include 7-day asset URLs for media chunks */
846
872
  includeAssetUrls?: boolean;
847
873
  }
848
874
  /**
@@ -869,7 +895,7 @@ interface DocumentChunk {
869
895
  sortOrder: number;
870
896
  /** Chunk metadata returned by the API */
871
897
  metadata: Record<string, unknown>;
872
- /** Generated asset URL when requested and available */
898
+ /** 7-day asset URL for media chunks when available */
873
899
  assetUrl?: string | null;
874
900
  /** Chunk creation timestamp */
875
901
  createdAt?: Date;
@@ -914,9 +940,7 @@ declare class Documents extends BaseResource {
914
940
  /**
915
941
  * List canonical documents in a namespace.
916
942
  */
917
- list(params?: {
918
- namespace?: string;
919
- }): Promise<DocumentListResponse>;
943
+ list(params?: DocumentListParams): Promise<DocumentListResponse>;
920
944
  /**
921
945
  * Get one canonical document by ID.
922
946
  */
@@ -933,6 +957,8 @@ declare class Documents extends BaseResource {
933
957
  * Archive one canonical document by ID.
934
958
  */
935
959
  archive(documentId: string): Promise<Document>;
960
+ private createDocumentListRequestConfig;
961
+ private normalizeDocumentListResponse;
936
962
  private createChunkListRequestConfig;
937
963
  private createChunkGetRequestConfig;
938
964
  }
@@ -996,6 +1022,20 @@ interface KnowledgeCacheJobResultParams {
996
1022
  localDocumentId?: string;
997
1023
  verifyChecksum?: boolean;
998
1024
  }
1025
+ interface KnowledgeCacheDocumentParams {
1026
+ /** Canonical Knowhere document identifier from published retrieval results. */
1027
+ documentId: string;
1028
+ /** Optional local cache identifier to use for the localized document copy. */
1029
+ localDocumentId?: string;
1030
+ }
1031
+ interface KnowledgeDocumentReference {
1032
+ /** Stable local identifier used by cached outline/read/grep methods. */
1033
+ localDocumentId?: string;
1034
+ /** Canonical Knowhere document identifier from published retrieval results. */
1035
+ documentId?: string;
1036
+ /** Server parse job identifier whose completed result should be cached before reading. */
1037
+ jobId?: string;
1038
+ }
999
1039
  interface KnowledgeSection {
1000
1040
  sectionPath: string;
1001
1041
  sectionTitle: string;
@@ -1014,8 +1054,7 @@ interface KnowledgeOutline {
1014
1054
  sections: KnowledgeSection[];
1015
1055
  sectionTree: KnowledgeSection[];
1016
1056
  }
1017
- interface KnowledgeReadParams {
1018
- localDocumentId: string;
1057
+ interface KnowledgeReadParams extends KnowledgeDocumentReference {
1019
1058
  sectionPath?: string;
1020
1059
  startChunk?: number;
1021
1060
  endChunk?: number;
@@ -1038,8 +1077,7 @@ interface KnowledgeReadResponse {
1038
1077
  chunks: KnowledgeReadChunk[];
1039
1078
  nextChunk?: number;
1040
1079
  }
1041
- interface KnowledgeGrepParams {
1042
- localDocumentId: string;
1080
+ interface KnowledgeGrepParams extends KnowledgeDocumentReference {
1043
1081
  pattern: string;
1044
1082
  isRegex?: boolean;
1045
1083
  isCaseSensitive?: boolean;
@@ -1111,13 +1149,17 @@ declare class Knowledge {
1111
1149
  getJobStatus(jobId: string): Promise<KnowledgeAsyncJobStatusResponse>;
1112
1150
  recoverPendingAsyncParseJobs(): Promise<KnowledgeStartupRecoveryResponse>;
1113
1151
  cacheJobResult(params: KnowledgeCacheJobResultParams): Promise<LocalKnowledgeParseResponse>;
1152
+ cacheDocument(params: KnowledgeCacheDocumentParams): Promise<LocalKnowledgeParseResponse>;
1114
1153
  private resolveAsyncCache;
1115
1154
  listDocuments(): Promise<LocalKnowledgeDocument[]>;
1116
- getDocumentOutline(localDocumentId: string): Promise<KnowledgeOutline>;
1155
+ getDocumentOutline(reference: string | KnowledgeDocumentReference): Promise<KnowledgeOutline>;
1117
1156
  readChunks(params: KnowledgeReadParams): Promise<KnowledgeReadResponse>;
1118
1157
  grepChunks(params: KnowledgeGrepParams): Promise<KnowledgeGrepResponse>;
1119
1158
  search(params: KnowledgeSearchParams): Promise<KnowledgeSearchResponse>;
1120
1159
  private resolveSearchDocuments;
1160
+ private loadReadableResult;
1161
+ private findLocalDocumentByRemoteId;
1162
+ private getPublishedDocumentJobId;
1121
1163
  }
1122
1164
 
1123
1165
  interface StoredAsyncParseJob {
@@ -1342,4 +1384,4 @@ declare class JobFailedError extends KnowhereError {
1342
1384
  constructor(message: string, code: string, jobResult: JobResult);
1343
1385
  }
1344
1386
 
1345
- export { APIError, type AuthTokenProvider, AuthenticationError, BadRequestError, type BaseChunk, ChecksumError, type Chunk, ConflictError, type CreateJobParams, type DocType, type Document, type DocumentChunk, type DocumentChunkGetParams, type DocumentChunkListParams, type DocumentChunkListResponse, type DocumentChunkPagination, type DocumentChunkResponse, type DocumentChunkType, type DocumentListResponse, type DocumentMetadata, Documents, type FileIndex, GatewayTimeoutError, type ImageChunk, InternalServerError, InvalidStateError, type Job, type JobError, JobFailedError, type JobResult, type JobStatus, Jobs, Knowhere, KnowhereError, type KnowhereOptions, Knowledge, type KnowledgeAsyncJobStatusResponse, type KnowledgeAsyncParseParams, type KnowledgeAsyncParseResponse, type KnowledgeCacheJobResultParams, type KnowledgeChunkType, type KnowledgeGrepMatch, type KnowledgeGrepParams, type KnowledgeGrepResponse, type KnowledgeOutline, type KnowledgeParseParams, type KnowledgeReadChunk, type KnowledgeReadParams, type KnowledgeReadResponse, type KnowledgeSearchParams, type KnowledgeSearchReference, type KnowledgeSearchResponse, type KnowledgeSearchResult, type KnowledgeSection, type KnowledgeStartupRecoveryResponse, type LoadOptions, type LocalKnowledgeDocument, type LocalKnowledgeParseResponse, LocalKnowledgeStore, type Manifest, NetworkError, NotFoundError, type ParseParams, type ParseResult, type ParsingModel, type ParsingParams, PaymentRequiredError, PermissionDeniedError, type PollProgress, PollingTimeoutError, RateLimitError, Retrieval, type RetrievalChannel, type RetrievalFilterMode, type RetrievalQueryParams, type RetrievalQueryResponse, type RetrievalReferencedChunk, type RetrievalResult, type RetrievalSectionExclusion, type RetrievalSource, ServiceUnavailableError, type Statistics, type TableChunk, type TextChunk, TimeoutError, type UploadParams, type UploadProgress, VERSION, ValidationError, type WaitOptions, type WebhookConfig, Knowhere as default };
1387
+ export { APIError, type AuthTokenProvider, AuthenticationError, BadRequestError, type BaseChunk, ChecksumError, type Chunk, ConflictError, type CreateJobParams, type DocType, type Document, type DocumentChunk, type DocumentChunkGetParams, type DocumentChunkListParams, type DocumentChunkListResponse, type DocumentChunkPagination, type DocumentChunkResponse, type DocumentChunkType, type DocumentListPagination, type DocumentListParams, type DocumentListResponse, type DocumentMetadata, Documents, type FileIndex, GatewayTimeoutError, type ImageChunk, InternalServerError, InvalidStateError, type Job, type JobError, JobFailedError, type JobResult, type JobStatus, Jobs, Knowhere, KnowhereError, type KnowhereOptions, Knowledge, type KnowledgeAsyncJobStatusResponse, type KnowledgeAsyncParseParams, type KnowledgeAsyncParseResponse, type KnowledgeCacheDocumentParams, type KnowledgeCacheJobResultParams, type KnowledgeChunkType, type KnowledgeDocumentReference, type KnowledgeGrepMatch, type KnowledgeGrepParams, type KnowledgeGrepResponse, type KnowledgeOutline, type KnowledgeParseParams, type KnowledgeReadChunk, type KnowledgeReadParams, type KnowledgeReadResponse, type KnowledgeSearchParams, type KnowledgeSearchReference, type KnowledgeSearchResponse, type KnowledgeSearchResult, type KnowledgeSection, type KnowledgeStartupRecoveryResponse, type LoadOptions, type LocalKnowledgeDocument, type LocalKnowledgeParseResponse, LocalKnowledgeStore, type Manifest, NetworkError, NotFoundError, type ParseParams, type ParseResult, type ParsingModel, type ParsingParams, PaymentRequiredError, PermissionDeniedError, type PollProgress, PollingTimeoutError, RateLimitError, Retrieval, type RetrievalChannel, type RetrievalFilterMode, type RetrievalQueryParams, type RetrievalQueryResponse, type RetrievalReferencedChunk, type RetrievalResult, type RetrievalSectionExclusion, type RetrievalSource, ServiceUnavailableError, type Statistics, type TableChunk, type TextChunk, TimeoutError, type UploadParams, type UploadProgress, VERSION, ValidationError, type WaitOptions, type WebhookConfig, Knowhere as default };
package/dist/index.d.ts CHANGED
@@ -799,6 +799,30 @@ interface Document {
799
799
  /** Archive timestamp, when archived */
800
800
  archivedAt?: Date;
801
801
  }
802
+ /**
803
+ * Pagination metadata returned by document list endpoints.
804
+ */
805
+ interface DocumentListPagination {
806
+ /** Current page number */
807
+ page: number;
808
+ /** Number of items requested per page */
809
+ pageSize: number;
810
+ /** Total matching documents */
811
+ total: number;
812
+ /** Total number of pages */
813
+ totalPages: number;
814
+ }
815
+ /**
816
+ * Query parameters for GET /v1/documents.
817
+ */
818
+ interface DocumentListParams {
819
+ /** Retrieval namespace */
820
+ namespace?: string;
821
+ /** Page number (default: 1) */
822
+ page?: number;
823
+ /** Items per page (default: 50, maximum: 200) */
824
+ pageSize?: number;
825
+ }
802
826
  /**
803
827
  * Response from GET /v1/documents.
804
828
  */
@@ -807,6 +831,8 @@ interface DocumentListResponse {
807
831
  namespace: string;
808
832
  /** Documents visible in the namespace */
809
833
  documents: Document[];
834
+ /** Pagination metadata */
835
+ pagination: DocumentListPagination;
810
836
  }
811
837
  /**
812
838
  * Document chunk types supported by document chunk endpoints.
@@ -835,14 +861,14 @@ interface DocumentChunkListParams {
835
861
  pageSize?: number;
836
862
  /** Optional chunk type filter */
837
863
  chunkType?: DocumentChunkType;
838
- /** Generate asset URLs for media chunks (default: false) */
864
+ /** Set true to include 7-day asset URLs for media chunks */
839
865
  includeAssetUrls?: boolean;
840
866
  }
841
867
  /**
842
868
  * Query parameters for GET /v1/documents/{document_id}/chunks/{document_chunk_id}.
843
869
  */
844
870
  interface DocumentChunkGetParams {
845
- /** Generate asset URLs for media chunks (default: false) */
871
+ /** Set true to include 7-day asset URLs for media chunks */
846
872
  includeAssetUrls?: boolean;
847
873
  }
848
874
  /**
@@ -869,7 +895,7 @@ interface DocumentChunk {
869
895
  sortOrder: number;
870
896
  /** Chunk metadata returned by the API */
871
897
  metadata: Record<string, unknown>;
872
- /** Generated asset URL when requested and available */
898
+ /** 7-day asset URL for media chunks when available */
873
899
  assetUrl?: string | null;
874
900
  /** Chunk creation timestamp */
875
901
  createdAt?: Date;
@@ -914,9 +940,7 @@ declare class Documents extends BaseResource {
914
940
  /**
915
941
  * List canonical documents in a namespace.
916
942
  */
917
- list(params?: {
918
- namespace?: string;
919
- }): Promise<DocumentListResponse>;
943
+ list(params?: DocumentListParams): Promise<DocumentListResponse>;
920
944
  /**
921
945
  * Get one canonical document by ID.
922
946
  */
@@ -933,6 +957,8 @@ declare class Documents extends BaseResource {
933
957
  * Archive one canonical document by ID.
934
958
  */
935
959
  archive(documentId: string): Promise<Document>;
960
+ private createDocumentListRequestConfig;
961
+ private normalizeDocumentListResponse;
936
962
  private createChunkListRequestConfig;
937
963
  private createChunkGetRequestConfig;
938
964
  }
@@ -996,6 +1022,20 @@ interface KnowledgeCacheJobResultParams {
996
1022
  localDocumentId?: string;
997
1023
  verifyChecksum?: boolean;
998
1024
  }
1025
+ interface KnowledgeCacheDocumentParams {
1026
+ /** Canonical Knowhere document identifier from published retrieval results. */
1027
+ documentId: string;
1028
+ /** Optional local cache identifier to use for the localized document copy. */
1029
+ localDocumentId?: string;
1030
+ }
1031
+ interface KnowledgeDocumentReference {
1032
+ /** Stable local identifier used by cached outline/read/grep methods. */
1033
+ localDocumentId?: string;
1034
+ /** Canonical Knowhere document identifier from published retrieval results. */
1035
+ documentId?: string;
1036
+ /** Server parse job identifier whose completed result should be cached before reading. */
1037
+ jobId?: string;
1038
+ }
999
1039
  interface KnowledgeSection {
1000
1040
  sectionPath: string;
1001
1041
  sectionTitle: string;
@@ -1014,8 +1054,7 @@ interface KnowledgeOutline {
1014
1054
  sections: KnowledgeSection[];
1015
1055
  sectionTree: KnowledgeSection[];
1016
1056
  }
1017
- interface KnowledgeReadParams {
1018
- localDocumentId: string;
1057
+ interface KnowledgeReadParams extends KnowledgeDocumentReference {
1019
1058
  sectionPath?: string;
1020
1059
  startChunk?: number;
1021
1060
  endChunk?: number;
@@ -1038,8 +1077,7 @@ interface KnowledgeReadResponse {
1038
1077
  chunks: KnowledgeReadChunk[];
1039
1078
  nextChunk?: number;
1040
1079
  }
1041
- interface KnowledgeGrepParams {
1042
- localDocumentId: string;
1080
+ interface KnowledgeGrepParams extends KnowledgeDocumentReference {
1043
1081
  pattern: string;
1044
1082
  isRegex?: boolean;
1045
1083
  isCaseSensitive?: boolean;
@@ -1111,13 +1149,17 @@ declare class Knowledge {
1111
1149
  getJobStatus(jobId: string): Promise<KnowledgeAsyncJobStatusResponse>;
1112
1150
  recoverPendingAsyncParseJobs(): Promise<KnowledgeStartupRecoveryResponse>;
1113
1151
  cacheJobResult(params: KnowledgeCacheJobResultParams): Promise<LocalKnowledgeParseResponse>;
1152
+ cacheDocument(params: KnowledgeCacheDocumentParams): Promise<LocalKnowledgeParseResponse>;
1114
1153
  private resolveAsyncCache;
1115
1154
  listDocuments(): Promise<LocalKnowledgeDocument[]>;
1116
- getDocumentOutline(localDocumentId: string): Promise<KnowledgeOutline>;
1155
+ getDocumentOutline(reference: string | KnowledgeDocumentReference): Promise<KnowledgeOutline>;
1117
1156
  readChunks(params: KnowledgeReadParams): Promise<KnowledgeReadResponse>;
1118
1157
  grepChunks(params: KnowledgeGrepParams): Promise<KnowledgeGrepResponse>;
1119
1158
  search(params: KnowledgeSearchParams): Promise<KnowledgeSearchResponse>;
1120
1159
  private resolveSearchDocuments;
1160
+ private loadReadableResult;
1161
+ private findLocalDocumentByRemoteId;
1162
+ private getPublishedDocumentJobId;
1121
1163
  }
1122
1164
 
1123
1165
  interface StoredAsyncParseJob {
@@ -1342,4 +1384,4 @@ declare class JobFailedError extends KnowhereError {
1342
1384
  constructor(message: string, code: string, jobResult: JobResult);
1343
1385
  }
1344
1386
 
1345
- export { APIError, type AuthTokenProvider, AuthenticationError, BadRequestError, type BaseChunk, ChecksumError, type Chunk, ConflictError, type CreateJobParams, type DocType, type Document, type DocumentChunk, type DocumentChunkGetParams, type DocumentChunkListParams, type DocumentChunkListResponse, type DocumentChunkPagination, type DocumentChunkResponse, type DocumentChunkType, type DocumentListResponse, type DocumentMetadata, Documents, type FileIndex, GatewayTimeoutError, type ImageChunk, InternalServerError, InvalidStateError, type Job, type JobError, JobFailedError, type JobResult, type JobStatus, Jobs, Knowhere, KnowhereError, type KnowhereOptions, Knowledge, type KnowledgeAsyncJobStatusResponse, type KnowledgeAsyncParseParams, type KnowledgeAsyncParseResponse, type KnowledgeCacheJobResultParams, type KnowledgeChunkType, type KnowledgeGrepMatch, type KnowledgeGrepParams, type KnowledgeGrepResponse, type KnowledgeOutline, type KnowledgeParseParams, type KnowledgeReadChunk, type KnowledgeReadParams, type KnowledgeReadResponse, type KnowledgeSearchParams, type KnowledgeSearchReference, type KnowledgeSearchResponse, type KnowledgeSearchResult, type KnowledgeSection, type KnowledgeStartupRecoveryResponse, type LoadOptions, type LocalKnowledgeDocument, type LocalKnowledgeParseResponse, LocalKnowledgeStore, type Manifest, NetworkError, NotFoundError, type ParseParams, type ParseResult, type ParsingModel, type ParsingParams, PaymentRequiredError, PermissionDeniedError, type PollProgress, PollingTimeoutError, RateLimitError, Retrieval, type RetrievalChannel, type RetrievalFilterMode, type RetrievalQueryParams, type RetrievalQueryResponse, type RetrievalReferencedChunk, type RetrievalResult, type RetrievalSectionExclusion, type RetrievalSource, ServiceUnavailableError, type Statistics, type TableChunk, type TextChunk, TimeoutError, type UploadParams, type UploadProgress, VERSION, ValidationError, type WaitOptions, type WebhookConfig, Knowhere as default };
1387
+ export { APIError, type AuthTokenProvider, AuthenticationError, BadRequestError, type BaseChunk, ChecksumError, type Chunk, ConflictError, type CreateJobParams, type DocType, type Document, type DocumentChunk, type DocumentChunkGetParams, type DocumentChunkListParams, type DocumentChunkListResponse, type DocumentChunkPagination, type DocumentChunkResponse, type DocumentChunkType, type DocumentListPagination, type DocumentListParams, type DocumentListResponse, type DocumentMetadata, Documents, type FileIndex, GatewayTimeoutError, type ImageChunk, InternalServerError, InvalidStateError, type Job, type JobError, JobFailedError, type JobResult, type JobStatus, Jobs, Knowhere, KnowhereError, type KnowhereOptions, Knowledge, type KnowledgeAsyncJobStatusResponse, type KnowledgeAsyncParseParams, type KnowledgeAsyncParseResponse, type KnowledgeCacheDocumentParams, type KnowledgeCacheJobResultParams, type KnowledgeChunkType, type KnowledgeDocumentReference, type KnowledgeGrepMatch, type KnowledgeGrepParams, type KnowledgeGrepResponse, type KnowledgeOutline, type KnowledgeParseParams, type KnowledgeReadChunk, type KnowledgeReadParams, type KnowledgeReadResponse, type KnowledgeSearchParams, type KnowledgeSearchReference, type KnowledgeSearchResponse, type KnowledgeSearchResult, type KnowledgeSection, type KnowledgeStartupRecoveryResponse, type LoadOptions, type LocalKnowledgeDocument, type LocalKnowledgeParseResponse, LocalKnowledgeStore, type Manifest, NetworkError, NotFoundError, type ParseParams, type ParseResult, type ParsingModel, type ParsingParams, PaymentRequiredError, PermissionDeniedError, type PollProgress, PollingTimeoutError, RateLimitError, Retrieval, type RetrievalChannel, type RetrievalFilterMode, type RetrievalQueryParams, type RetrievalQueryResponse, type RetrievalReferencedChunk, type RetrievalResult, type RetrievalSectionExclusion, type RetrievalSource, ServiceUnavailableError, type Statistics, type TableChunk, type TextChunk, TimeoutError, type UploadParams, type UploadProgress, VERSION, ValidationError, type WaitOptions, type WebhookConfig, Knowhere as default };
package/dist/index.js CHANGED
@@ -1438,12 +1438,11 @@ var Documents = class extends BaseResource {
1438
1438
  * List canonical documents in a namespace.
1439
1439
  */
1440
1440
  async list(params) {
1441
- const requestConfig = params?.namespace ? {
1442
- params: {
1443
- namespace: params.namespace
1444
- }
1445
- } : void 0;
1446
- return this.httpClient.get("/v1/documents", requestConfig);
1441
+ const response = await this.httpClient.get(
1442
+ "/v1/documents",
1443
+ this.createDocumentListRequestConfig(params)
1444
+ );
1445
+ return this.normalizeDocumentListResponse(response);
1447
1446
  }
1448
1447
  /**
1449
1448
  * Get one canonical document by ID.
@@ -1475,6 +1474,38 @@ var Documents = class extends BaseResource {
1475
1474
  async archive(documentId) {
1476
1475
  return this.httpClient.post(`/v1/documents/${documentId}/archive`);
1477
1476
  }
1477
+ createDocumentListRequestConfig(params) {
1478
+ if (!params) {
1479
+ return void 0;
1480
+ }
1481
+ const queryParams = {};
1482
+ if (params.namespace !== void 0) {
1483
+ queryParams.namespace = params.namespace;
1484
+ }
1485
+ if (params.page !== void 0) {
1486
+ queryParams.page = params.page;
1487
+ }
1488
+ if (params.pageSize !== void 0) {
1489
+ queryParams.page_size = params.pageSize;
1490
+ }
1491
+ return Object.keys(queryParams).length > 0 ? { params: queryParams } : void 0;
1492
+ }
1493
+ normalizeDocumentListResponse(response) {
1494
+ const maybePaginatedResponse = response;
1495
+ if (maybePaginatedResponse.pagination) {
1496
+ return response;
1497
+ }
1498
+ const total = response.documents.length;
1499
+ return {
1500
+ ...response,
1501
+ pagination: {
1502
+ page: 1,
1503
+ pageSize: total,
1504
+ total,
1505
+ totalPages: total > 0 ? 1 : 0
1506
+ }
1507
+ };
1508
+ }
1478
1509
  createChunkListRequestConfig(params) {
1479
1510
  if (!params) {
1480
1511
  return void 0;
@@ -1489,23 +1520,26 @@ var Documents = class extends BaseResource {
1489
1520
  if (params.chunkType !== void 0) {
1490
1521
  queryParams.chunk_type = params.chunkType;
1491
1522
  }
1492
- if (params.includeAssetUrls === true) {
1493
- queryParams.include_asset_urls = true;
1523
+ if (params.includeAssetUrls !== void 0) {
1524
+ queryParams.include_asset_urls = params.includeAssetUrls;
1494
1525
  }
1495
1526
  return Object.keys(queryParams).length > 0 ? { params: queryParams } : void 0;
1496
1527
  }
1497
1528
  createChunkGetRequestConfig(params) {
1498
- if (params?.includeAssetUrls !== true) {
1529
+ if (params?.includeAssetUrls === void 0) {
1499
1530
  return void 0;
1500
1531
  }
1501
1532
  return {
1502
1533
  params: {
1503
- include_asset_urls: true
1534
+ include_asset_urls: params.includeAssetUrls
1504
1535
  }
1505
1536
  };
1506
1537
  }
1507
1538
  };
1508
1539
 
1540
+ // src/knowledge/knowledge.ts
1541
+ var import_crypto2 = require("crypto");
1542
+
1509
1543
  // src/knowledge/local-store.ts
1510
1544
  var import_crypto = require("crypto");
1511
1545
  var import_os = __toESM(require("os"));
@@ -1734,6 +1768,8 @@ var MAX_READ_LIMIT = 40;
1734
1768
  var DEFAULT_GREP_LIMIT = 20;
1735
1769
  var MAX_GREP_LIMIT = 50;
1736
1770
  var DEFAULT_CONTEXT_CHARS = 80;
1771
+ var SAFE_LOCAL_DOCUMENT_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
1772
+ var REMOTE_DOCUMENT_ID_PATTERN = /^doc[_-]/;
1737
1773
  var Knowledge = class _Knowledge {
1738
1774
  client;
1739
1775
  store;
@@ -1789,6 +1825,19 @@ var Knowledge = class _Knowledge {
1789
1825
  });
1790
1826
  return { document, result };
1791
1827
  }
1828
+ async cacheDocument(params) {
1829
+ if (!params.documentId) {
1830
+ throw new ValidationError("documentId is required");
1831
+ }
1832
+ const existing = await this.findLocalDocumentByRemoteId(
1833
+ params.documentId
1834
+ );
1835
+ const jobId = await this.getPublishedDocumentJobId(params.documentId);
1836
+ return this.cacheJobResult({
1837
+ jobId,
1838
+ localDocumentId: params.localDocumentId ?? existing?.localDocumentId ?? createLocalDocumentIdForRemote(params.documentId)
1839
+ });
1840
+ }
1792
1841
  async resolveAsyncCache(jobId, isDone, isFailed) {
1793
1842
  const trackedJob = await this.store.getAsyncParseJob(jobId);
1794
1843
  if (!trackedJob) {
@@ -1845,8 +1894,8 @@ var Knowledge = class _Knowledge {
1845
1894
  async listDocuments() {
1846
1895
  return this.store.listDocuments();
1847
1896
  }
1848
- async getDocumentOutline(localDocumentId) {
1849
- const { document, result } = await this.store.loadResult(localDocumentId);
1897
+ async getDocumentOutline(reference) {
1898
+ const { document, result } = await this.loadReadableResult(reference);
1850
1899
  const chunks = indexChunks(result);
1851
1900
  const sections = buildFlatSections(result, chunks);
1852
1901
  const sectionTree = result.docNav?.sections && result.docNav.sections.length > 0 ? result.docNav.sections.map(
@@ -1861,7 +1910,7 @@ var Knowledge = class _Knowledge {
1861
1910
  };
1862
1911
  }
1863
1912
  async readChunks(params) {
1864
- const { document, result } = await this.store.loadResult(params.localDocumentId);
1913
+ const { document, result } = await this.loadReadableResult(params);
1865
1914
  const limit = clampLimit(params.limit, DEFAULT_READ_LIMIT, MAX_READ_LIMIT);
1866
1915
  const chunks = indexChunks(result).filter((chunk) => matchesReadScope(chunk, params));
1867
1916
  const selected = selectReadWindow(chunks, params, limit);
@@ -1877,7 +1926,7 @@ var Knowledge = class _Knowledge {
1877
1926
  if (!params.pattern) {
1878
1927
  throw new ValidationError("pattern is required");
1879
1928
  }
1880
- const { document, result } = await this.store.loadResult(params.localDocumentId);
1929
+ const { document, result } = await this.loadReadableResult(params);
1881
1930
  const maxResults = clampLimit(params.maxResults, DEFAULT_GREP_LIMIT, MAX_GREP_LIMIT);
1882
1931
  const contextChars = params.contextChars ?? DEFAULT_CONTEXT_CHARS;
1883
1932
  const matcher = createMatcher(params);
@@ -1947,7 +1996,68 @@ var Knowledge = class _Knowledge {
1947
1996
  const requested = new Set(localDocumentIds);
1948
1997
  return documents.filter((document) => requested.has(document.localDocumentId));
1949
1998
  }
1999
+ async loadReadableResult(reference) {
2000
+ const normalized = normalizeDocumentReference(reference);
2001
+ if (normalized.documentId) {
2002
+ return this.cacheDocument({
2003
+ documentId: normalized.documentId,
2004
+ localDocumentId: normalized.localDocumentId
2005
+ });
2006
+ }
2007
+ if (normalized.jobId) {
2008
+ return this.cacheJobResult({
2009
+ jobId: normalized.jobId,
2010
+ localDocumentId: normalized.localDocumentId
2011
+ });
2012
+ }
2013
+ if (!normalized.localDocumentId) {
2014
+ throw new ValidationError("localDocumentId, documentId, or jobId is required");
2015
+ }
2016
+ const document = await this.store.getDocument(normalized.localDocumentId);
2017
+ if (document) {
2018
+ return this.store.loadResult(normalized.localDocumentId);
2019
+ }
2020
+ if (looksLikeRemoteDocumentId(normalized.localDocumentId)) {
2021
+ return this.cacheDocument({
2022
+ documentId: normalized.localDocumentId,
2023
+ localDocumentId: createLocalDocumentIdForRemote(normalized.localDocumentId)
2024
+ });
2025
+ }
2026
+ throw new Error(`Local Knowhere document not found: ${normalized.localDocumentId}`);
2027
+ }
2028
+ async findLocalDocumentByRemoteId(documentId) {
2029
+ const documents = await this.store.listDocuments();
2030
+ return documents.find((document) => document.documentId === documentId);
2031
+ }
2032
+ async getPublishedDocumentJobId(documentId) {
2033
+ const response = await this.client.documents.listChunks(documentId, {
2034
+ page: 1,
2035
+ pageSize: 1
2036
+ });
2037
+ if (!response.jobId) {
2038
+ throw new Error(
2039
+ `Cannot sync server document ${documentId}: current published job id was not returned.`
2040
+ );
2041
+ }
2042
+ return response.jobId;
2043
+ }
1950
2044
  };
2045
+ function normalizeDocumentReference(reference) {
2046
+ if (typeof reference === "string") {
2047
+ return { localDocumentId: reference };
2048
+ }
2049
+ return reference;
2050
+ }
2051
+ function looksLikeRemoteDocumentId(value) {
2052
+ return REMOTE_DOCUMENT_ID_PATTERN.test(value);
2053
+ }
2054
+ function createLocalDocumentIdForRemote(documentId) {
2055
+ if (SAFE_LOCAL_DOCUMENT_ID_PATTERN.test(documentId) && !documentId.includes("..") && !documentId.includes("/") && !documentId.includes("\\")) {
2056
+ return documentId;
2057
+ }
2058
+ const hash = (0, import_crypto2.createHash)("sha256").update(documentId).digest("hex").slice(0, 16);
2059
+ return `remote_${hash}`;
2060
+ }
1951
2061
  function indexChunks(result) {
1952
2062
  return result.chunks.map((chunk, index) => {
1953
2063
  const filePath = getChunkFilePath2(chunk);
package/dist/index.mjs CHANGED
@@ -1378,12 +1378,11 @@ var Documents = class extends BaseResource {
1378
1378
  * List canonical documents in a namespace.
1379
1379
  */
1380
1380
  async list(params) {
1381
- const requestConfig = params?.namespace ? {
1382
- params: {
1383
- namespace: params.namespace
1384
- }
1385
- } : void 0;
1386
- return this.httpClient.get("/v1/documents", requestConfig);
1381
+ const response = await this.httpClient.get(
1382
+ "/v1/documents",
1383
+ this.createDocumentListRequestConfig(params)
1384
+ );
1385
+ return this.normalizeDocumentListResponse(response);
1387
1386
  }
1388
1387
  /**
1389
1388
  * Get one canonical document by ID.
@@ -1415,6 +1414,38 @@ var Documents = class extends BaseResource {
1415
1414
  async archive(documentId) {
1416
1415
  return this.httpClient.post(`/v1/documents/${documentId}/archive`);
1417
1416
  }
1417
+ createDocumentListRequestConfig(params) {
1418
+ if (!params) {
1419
+ return void 0;
1420
+ }
1421
+ const queryParams = {};
1422
+ if (params.namespace !== void 0) {
1423
+ queryParams.namespace = params.namespace;
1424
+ }
1425
+ if (params.page !== void 0) {
1426
+ queryParams.page = params.page;
1427
+ }
1428
+ if (params.pageSize !== void 0) {
1429
+ queryParams.page_size = params.pageSize;
1430
+ }
1431
+ return Object.keys(queryParams).length > 0 ? { params: queryParams } : void 0;
1432
+ }
1433
+ normalizeDocumentListResponse(response) {
1434
+ const maybePaginatedResponse = response;
1435
+ if (maybePaginatedResponse.pagination) {
1436
+ return response;
1437
+ }
1438
+ const total = response.documents.length;
1439
+ return {
1440
+ ...response,
1441
+ pagination: {
1442
+ page: 1,
1443
+ pageSize: total,
1444
+ total,
1445
+ totalPages: total > 0 ? 1 : 0
1446
+ }
1447
+ };
1448
+ }
1418
1449
  createChunkListRequestConfig(params) {
1419
1450
  if (!params) {
1420
1451
  return void 0;
@@ -1429,23 +1460,26 @@ var Documents = class extends BaseResource {
1429
1460
  if (params.chunkType !== void 0) {
1430
1461
  queryParams.chunk_type = params.chunkType;
1431
1462
  }
1432
- if (params.includeAssetUrls === true) {
1433
- queryParams.include_asset_urls = true;
1463
+ if (params.includeAssetUrls !== void 0) {
1464
+ queryParams.include_asset_urls = params.includeAssetUrls;
1434
1465
  }
1435
1466
  return Object.keys(queryParams).length > 0 ? { params: queryParams } : void 0;
1436
1467
  }
1437
1468
  createChunkGetRequestConfig(params) {
1438
- if (params?.includeAssetUrls !== true) {
1469
+ if (params?.includeAssetUrls === void 0) {
1439
1470
  return void 0;
1440
1471
  }
1441
1472
  return {
1442
1473
  params: {
1443
- include_asset_urls: true
1474
+ include_asset_urls: params.includeAssetUrls
1444
1475
  }
1445
1476
  };
1446
1477
  }
1447
1478
  };
1448
1479
 
1480
+ // src/knowledge/knowledge.ts
1481
+ import { createHash as createHash2 } from "crypto";
1482
+
1449
1483
  // src/knowledge/local-store.ts
1450
1484
  import { createHash } from "crypto";
1451
1485
  import os from "os";
@@ -1674,6 +1708,8 @@ var MAX_READ_LIMIT = 40;
1674
1708
  var DEFAULT_GREP_LIMIT = 20;
1675
1709
  var MAX_GREP_LIMIT = 50;
1676
1710
  var DEFAULT_CONTEXT_CHARS = 80;
1711
+ var SAFE_LOCAL_DOCUMENT_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
1712
+ var REMOTE_DOCUMENT_ID_PATTERN = /^doc[_-]/;
1677
1713
  var Knowledge = class _Knowledge {
1678
1714
  client;
1679
1715
  store;
@@ -1729,6 +1765,19 @@ var Knowledge = class _Knowledge {
1729
1765
  });
1730
1766
  return { document, result };
1731
1767
  }
1768
+ async cacheDocument(params) {
1769
+ if (!params.documentId) {
1770
+ throw new ValidationError("documentId is required");
1771
+ }
1772
+ const existing = await this.findLocalDocumentByRemoteId(
1773
+ params.documentId
1774
+ );
1775
+ const jobId = await this.getPublishedDocumentJobId(params.documentId);
1776
+ return this.cacheJobResult({
1777
+ jobId,
1778
+ localDocumentId: params.localDocumentId ?? existing?.localDocumentId ?? createLocalDocumentIdForRemote(params.documentId)
1779
+ });
1780
+ }
1732
1781
  async resolveAsyncCache(jobId, isDone, isFailed) {
1733
1782
  const trackedJob = await this.store.getAsyncParseJob(jobId);
1734
1783
  if (!trackedJob) {
@@ -1785,8 +1834,8 @@ var Knowledge = class _Knowledge {
1785
1834
  async listDocuments() {
1786
1835
  return this.store.listDocuments();
1787
1836
  }
1788
- async getDocumentOutline(localDocumentId) {
1789
- const { document, result } = await this.store.loadResult(localDocumentId);
1837
+ async getDocumentOutline(reference) {
1838
+ const { document, result } = await this.loadReadableResult(reference);
1790
1839
  const chunks = indexChunks(result);
1791
1840
  const sections = buildFlatSections(result, chunks);
1792
1841
  const sectionTree = result.docNav?.sections && result.docNav.sections.length > 0 ? result.docNav.sections.map(
@@ -1801,7 +1850,7 @@ var Knowledge = class _Knowledge {
1801
1850
  };
1802
1851
  }
1803
1852
  async readChunks(params) {
1804
- const { document, result } = await this.store.loadResult(params.localDocumentId);
1853
+ const { document, result } = await this.loadReadableResult(params);
1805
1854
  const limit = clampLimit(params.limit, DEFAULT_READ_LIMIT, MAX_READ_LIMIT);
1806
1855
  const chunks = indexChunks(result).filter((chunk) => matchesReadScope(chunk, params));
1807
1856
  const selected = selectReadWindow(chunks, params, limit);
@@ -1817,7 +1866,7 @@ var Knowledge = class _Knowledge {
1817
1866
  if (!params.pattern) {
1818
1867
  throw new ValidationError("pattern is required");
1819
1868
  }
1820
- const { document, result } = await this.store.loadResult(params.localDocumentId);
1869
+ const { document, result } = await this.loadReadableResult(params);
1821
1870
  const maxResults = clampLimit(params.maxResults, DEFAULT_GREP_LIMIT, MAX_GREP_LIMIT);
1822
1871
  const contextChars = params.contextChars ?? DEFAULT_CONTEXT_CHARS;
1823
1872
  const matcher = createMatcher(params);
@@ -1887,7 +1936,68 @@ var Knowledge = class _Knowledge {
1887
1936
  const requested = new Set(localDocumentIds);
1888
1937
  return documents.filter((document) => requested.has(document.localDocumentId));
1889
1938
  }
1939
+ async loadReadableResult(reference) {
1940
+ const normalized = normalizeDocumentReference(reference);
1941
+ if (normalized.documentId) {
1942
+ return this.cacheDocument({
1943
+ documentId: normalized.documentId,
1944
+ localDocumentId: normalized.localDocumentId
1945
+ });
1946
+ }
1947
+ if (normalized.jobId) {
1948
+ return this.cacheJobResult({
1949
+ jobId: normalized.jobId,
1950
+ localDocumentId: normalized.localDocumentId
1951
+ });
1952
+ }
1953
+ if (!normalized.localDocumentId) {
1954
+ throw new ValidationError("localDocumentId, documentId, or jobId is required");
1955
+ }
1956
+ const document = await this.store.getDocument(normalized.localDocumentId);
1957
+ if (document) {
1958
+ return this.store.loadResult(normalized.localDocumentId);
1959
+ }
1960
+ if (looksLikeRemoteDocumentId(normalized.localDocumentId)) {
1961
+ return this.cacheDocument({
1962
+ documentId: normalized.localDocumentId,
1963
+ localDocumentId: createLocalDocumentIdForRemote(normalized.localDocumentId)
1964
+ });
1965
+ }
1966
+ throw new Error(`Local Knowhere document not found: ${normalized.localDocumentId}`);
1967
+ }
1968
+ async findLocalDocumentByRemoteId(documentId) {
1969
+ const documents = await this.store.listDocuments();
1970
+ return documents.find((document) => document.documentId === documentId);
1971
+ }
1972
+ async getPublishedDocumentJobId(documentId) {
1973
+ const response = await this.client.documents.listChunks(documentId, {
1974
+ page: 1,
1975
+ pageSize: 1
1976
+ });
1977
+ if (!response.jobId) {
1978
+ throw new Error(
1979
+ `Cannot sync server document ${documentId}: current published job id was not returned.`
1980
+ );
1981
+ }
1982
+ return response.jobId;
1983
+ }
1890
1984
  };
1985
+ function normalizeDocumentReference(reference) {
1986
+ if (typeof reference === "string") {
1987
+ return { localDocumentId: reference };
1988
+ }
1989
+ return reference;
1990
+ }
1991
+ function looksLikeRemoteDocumentId(value) {
1992
+ return REMOTE_DOCUMENT_ID_PATTERN.test(value);
1993
+ }
1994
+ function createLocalDocumentIdForRemote(documentId) {
1995
+ if (SAFE_LOCAL_DOCUMENT_ID_PATTERN.test(documentId) && !documentId.includes("..") && !documentId.includes("/") && !documentId.includes("\\")) {
1996
+ return documentId;
1997
+ }
1998
+ const hash = createHash2("sha256").update(documentId).digest("hex").slice(0, 16);
1999
+ return `remote_${hash}`;
2000
+ }
1891
2001
  function indexChunks(result) {
1892
2002
  return result.chunks.map((chunk, index) => {
1893
2003
  const filePath = getChunkFilePath2(chunk);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ontos-ai/knowhere-sdk",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Official Node.js SDK for Knowhere document parsing API",
5
5
  "keywords": [
6
6
  "knowhere",