@ontos-ai/knowhere-sdk 0.7.0 → 0.9.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
@@ -147,6 +147,10 @@ For granular control over the job lifecycle:
147
147
  const job = await client.jobs.create({
148
148
  sourceType: 'file',
149
149
  fileName: 'document.pdf',
150
+ documentMetadata: {
151
+ createdByClient: 'cli',
152
+ sourceFileName: 'document.pdf',
153
+ },
150
154
  parsingParams: { model: 'advanced', ocrEnabled: true },
151
155
  });
152
156
 
@@ -168,19 +172,23 @@ const result = await client.jobs.load(jobResult);
168
172
  ### Retrieval and Document Lifecycle
169
173
 
170
174
  Published documents are queryable through the retrieval API after a job
171
- finishes. `client.jobs.create(...)` does not return a usable `documentId`;
172
- persist `jobResult.documentId` after publication if you need to update or
173
- archive the same document later.
175
+ finishes. `client.jobs.create(...)` may return a planned `documentId`; persist
176
+ `jobResult.documentId` after publication as the canonical value if you need to
177
+ update or archive the same document later.
174
178
 
175
179
  ```typescript
176
180
  const job = await client.jobs.create({
177
181
  sourceType: 'url',
178
182
  sourceUrl: 'https://example.com/manual.pdf',
179
183
  namespace: 'support-center',
184
+ documentMetadata: {
185
+ createdByClient: 'notebook',
186
+ title: 'Support manual',
187
+ },
180
188
  });
181
189
 
182
190
  const jobResult = await client.jobs.wait(job.jobId);
183
- const documentId = jobResult.documentId;
191
+ const documentId = jobResult.documentId ?? job.documentId;
184
192
 
185
193
  if (!documentId) {
186
194
  throw new Error('Expected documentId after successful publication.');
@@ -305,6 +313,30 @@ console.log(serverSearch.references);
305
313
  Local grep and reads use the cached parse result, not server-side chunk scans.
306
314
  Search uses the Knowhere API retrieval query; local document IDs only help map
307
315
  returned server document IDs back to local cache IDs when available.
316
+ If a search result only has a published `documentId`, or an async parse flow only
317
+ has a completed `jobId`, read-oriented helpers can accept that remote identifier
318
+ directly and will sync the result into the local cache before reading. For a
319
+ `documentId`, the SDK resolves the document's current published `jobId` and then
320
+ downloads the parser result ZIP through the same `cacheJobResult(...)` path:
321
+
322
+ ```typescript
323
+ const remoteRead = await client.knowledge.readChunks({
324
+ documentId: 'doc_123',
325
+ sectionPath: 'Overview',
326
+ limit: 5,
327
+ });
328
+
329
+ const remoteOutline = await client.knowledge.getDocumentOutline({
330
+ documentId: 'doc_123',
331
+ });
332
+
333
+ const jobRead = await client.knowledge.readChunks({
334
+ jobId: jobResult.jobId,
335
+ localDocumentId: 'manual-v1',
336
+ limit: 5,
337
+ });
338
+ ```
339
+
308
340
  The MCP package is a wrapper over this SDK interface; install it only when an
309
341
  agent host needs an MCP server. See the MCP package README for Codex, Claude
310
342
  Code, Claude Desktop, and generic stdio MCP host configuration examples.
package/dist/index.d.mts CHANGED
@@ -46,6 +46,8 @@ interface Job {
46
46
  dataId?: string;
47
47
  /** Retrieval namespace for the canonical document */
48
48
  namespace?: string;
49
+ /** Planned stable document identifier for this job, when returned by the API */
50
+ documentId?: string;
49
51
  /** Job creation timestamp */
50
52
  createdAt: Date;
51
53
  /** Presigned URL for file upload (if sourceType is 'file') */
@@ -154,6 +156,10 @@ interface WebhookConfig {
154
156
  /** Webhook URL to notify on job completion */
155
157
  url: string;
156
158
  }
159
+ /**
160
+ * Client-provided display metadata copied onto the published document.
161
+ */
162
+ type DocumentMetadata = Record<string, unknown>;
157
163
  /**
158
164
  * Job creation parameters
159
165
  */
@@ -170,6 +176,8 @@ interface CreateJobParams {
170
176
  namespace?: string;
171
177
  /** Existing document identifier when updating a published document */
172
178
  documentId?: string;
179
+ /** Display metadata to copy onto the published document */
180
+ documentMetadata?: DocumentMetadata;
173
181
  /** Parsing configuration */
174
182
  parsingParams?: ParsingParams;
175
183
  /** Webhook configuration */
@@ -236,6 +244,8 @@ interface ParseParams {
236
244
  namespace?: string;
237
245
  /** Existing document identifier when updating a published document */
238
246
  documentId?: string;
247
+ /** Display metadata to copy onto the published document */
248
+ documentMetadata?: DocumentMetadata;
239
249
  /** Additional fragment description */
240
250
  addFragDesc?: string;
241
251
  /** Knowledge base directory */
@@ -780,6 +790,8 @@ interface Document {
780
790
  currentJobResultId?: string;
781
791
  /** Original source file name */
782
792
  sourceFileName?: string;
793
+ /** Client-provided display metadata copied from the publishing job */
794
+ documentMetadata?: Record<string, unknown>;
783
795
  /** Document creation timestamp */
784
796
  createdAt?: Date;
785
797
  /** Last update timestamp */
@@ -984,6 +996,20 @@ interface KnowledgeCacheJobResultParams {
984
996
  localDocumentId?: string;
985
997
  verifyChecksum?: boolean;
986
998
  }
999
+ interface KnowledgeCacheDocumentParams {
1000
+ /** Canonical Knowhere document identifier from published retrieval results. */
1001
+ documentId: string;
1002
+ /** Optional local cache identifier to use for the localized document copy. */
1003
+ localDocumentId?: string;
1004
+ }
1005
+ interface KnowledgeDocumentReference {
1006
+ /** Stable local identifier used by cached outline/read/grep methods. */
1007
+ localDocumentId?: string;
1008
+ /** Canonical Knowhere document identifier from published retrieval results. */
1009
+ documentId?: string;
1010
+ /** Server parse job identifier whose completed result should be cached before reading. */
1011
+ jobId?: string;
1012
+ }
987
1013
  interface KnowledgeSection {
988
1014
  sectionPath: string;
989
1015
  sectionTitle: string;
@@ -1002,8 +1028,7 @@ interface KnowledgeOutline {
1002
1028
  sections: KnowledgeSection[];
1003
1029
  sectionTree: KnowledgeSection[];
1004
1030
  }
1005
- interface KnowledgeReadParams {
1006
- localDocumentId: string;
1031
+ interface KnowledgeReadParams extends KnowledgeDocumentReference {
1007
1032
  sectionPath?: string;
1008
1033
  startChunk?: number;
1009
1034
  endChunk?: number;
@@ -1026,8 +1051,7 @@ interface KnowledgeReadResponse {
1026
1051
  chunks: KnowledgeReadChunk[];
1027
1052
  nextChunk?: number;
1028
1053
  }
1029
- interface KnowledgeGrepParams {
1030
- localDocumentId: string;
1054
+ interface KnowledgeGrepParams extends KnowledgeDocumentReference {
1031
1055
  pattern: string;
1032
1056
  isRegex?: boolean;
1033
1057
  isCaseSensitive?: boolean;
@@ -1099,13 +1123,17 @@ declare class Knowledge {
1099
1123
  getJobStatus(jobId: string): Promise<KnowledgeAsyncJobStatusResponse>;
1100
1124
  recoverPendingAsyncParseJobs(): Promise<KnowledgeStartupRecoveryResponse>;
1101
1125
  cacheJobResult(params: KnowledgeCacheJobResultParams): Promise<LocalKnowledgeParseResponse>;
1126
+ cacheDocument(params: KnowledgeCacheDocumentParams): Promise<LocalKnowledgeParseResponse>;
1102
1127
  private resolveAsyncCache;
1103
1128
  listDocuments(): Promise<LocalKnowledgeDocument[]>;
1104
- getDocumentOutline(localDocumentId: string): Promise<KnowledgeOutline>;
1129
+ getDocumentOutline(reference: string | KnowledgeDocumentReference): Promise<KnowledgeOutline>;
1105
1130
  readChunks(params: KnowledgeReadParams): Promise<KnowledgeReadResponse>;
1106
1131
  grepChunks(params: KnowledgeGrepParams): Promise<KnowledgeGrepResponse>;
1107
1132
  search(params: KnowledgeSearchParams): Promise<KnowledgeSearchResponse>;
1108
1133
  private resolveSearchDocuments;
1134
+ private loadReadableResult;
1135
+ private findLocalDocumentByRemoteId;
1136
+ private getPublishedDocumentJobId;
1109
1137
  }
1110
1138
 
1111
1139
  interface StoredAsyncParseJob {
@@ -1330,4 +1358,4 @@ declare class JobFailedError extends KnowhereError {
1330
1358
  constructor(message: string, code: string, jobResult: JobResult);
1331
1359
  }
1332
1360
 
1333
- 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, 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 };
1361
+ 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 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
@@ -46,6 +46,8 @@ interface Job {
46
46
  dataId?: string;
47
47
  /** Retrieval namespace for the canonical document */
48
48
  namespace?: string;
49
+ /** Planned stable document identifier for this job, when returned by the API */
50
+ documentId?: string;
49
51
  /** Job creation timestamp */
50
52
  createdAt: Date;
51
53
  /** Presigned URL for file upload (if sourceType is 'file') */
@@ -154,6 +156,10 @@ interface WebhookConfig {
154
156
  /** Webhook URL to notify on job completion */
155
157
  url: string;
156
158
  }
159
+ /**
160
+ * Client-provided display metadata copied onto the published document.
161
+ */
162
+ type DocumentMetadata = Record<string, unknown>;
157
163
  /**
158
164
  * Job creation parameters
159
165
  */
@@ -170,6 +176,8 @@ interface CreateJobParams {
170
176
  namespace?: string;
171
177
  /** Existing document identifier when updating a published document */
172
178
  documentId?: string;
179
+ /** Display metadata to copy onto the published document */
180
+ documentMetadata?: DocumentMetadata;
173
181
  /** Parsing configuration */
174
182
  parsingParams?: ParsingParams;
175
183
  /** Webhook configuration */
@@ -236,6 +244,8 @@ interface ParseParams {
236
244
  namespace?: string;
237
245
  /** Existing document identifier when updating a published document */
238
246
  documentId?: string;
247
+ /** Display metadata to copy onto the published document */
248
+ documentMetadata?: DocumentMetadata;
239
249
  /** Additional fragment description */
240
250
  addFragDesc?: string;
241
251
  /** Knowledge base directory */
@@ -780,6 +790,8 @@ interface Document {
780
790
  currentJobResultId?: string;
781
791
  /** Original source file name */
782
792
  sourceFileName?: string;
793
+ /** Client-provided display metadata copied from the publishing job */
794
+ documentMetadata?: Record<string, unknown>;
783
795
  /** Document creation timestamp */
784
796
  createdAt?: Date;
785
797
  /** Last update timestamp */
@@ -984,6 +996,20 @@ interface KnowledgeCacheJobResultParams {
984
996
  localDocumentId?: string;
985
997
  verifyChecksum?: boolean;
986
998
  }
999
+ interface KnowledgeCacheDocumentParams {
1000
+ /** Canonical Knowhere document identifier from published retrieval results. */
1001
+ documentId: string;
1002
+ /** Optional local cache identifier to use for the localized document copy. */
1003
+ localDocumentId?: string;
1004
+ }
1005
+ interface KnowledgeDocumentReference {
1006
+ /** Stable local identifier used by cached outline/read/grep methods. */
1007
+ localDocumentId?: string;
1008
+ /** Canonical Knowhere document identifier from published retrieval results. */
1009
+ documentId?: string;
1010
+ /** Server parse job identifier whose completed result should be cached before reading. */
1011
+ jobId?: string;
1012
+ }
987
1013
  interface KnowledgeSection {
988
1014
  sectionPath: string;
989
1015
  sectionTitle: string;
@@ -1002,8 +1028,7 @@ interface KnowledgeOutline {
1002
1028
  sections: KnowledgeSection[];
1003
1029
  sectionTree: KnowledgeSection[];
1004
1030
  }
1005
- interface KnowledgeReadParams {
1006
- localDocumentId: string;
1031
+ interface KnowledgeReadParams extends KnowledgeDocumentReference {
1007
1032
  sectionPath?: string;
1008
1033
  startChunk?: number;
1009
1034
  endChunk?: number;
@@ -1026,8 +1051,7 @@ interface KnowledgeReadResponse {
1026
1051
  chunks: KnowledgeReadChunk[];
1027
1052
  nextChunk?: number;
1028
1053
  }
1029
- interface KnowledgeGrepParams {
1030
- localDocumentId: string;
1054
+ interface KnowledgeGrepParams extends KnowledgeDocumentReference {
1031
1055
  pattern: string;
1032
1056
  isRegex?: boolean;
1033
1057
  isCaseSensitive?: boolean;
@@ -1099,13 +1123,17 @@ declare class Knowledge {
1099
1123
  getJobStatus(jobId: string): Promise<KnowledgeAsyncJobStatusResponse>;
1100
1124
  recoverPendingAsyncParseJobs(): Promise<KnowledgeStartupRecoveryResponse>;
1101
1125
  cacheJobResult(params: KnowledgeCacheJobResultParams): Promise<LocalKnowledgeParseResponse>;
1126
+ cacheDocument(params: KnowledgeCacheDocumentParams): Promise<LocalKnowledgeParseResponse>;
1102
1127
  private resolveAsyncCache;
1103
1128
  listDocuments(): Promise<LocalKnowledgeDocument[]>;
1104
- getDocumentOutline(localDocumentId: string): Promise<KnowledgeOutline>;
1129
+ getDocumentOutline(reference: string | KnowledgeDocumentReference): Promise<KnowledgeOutline>;
1105
1130
  readChunks(params: KnowledgeReadParams): Promise<KnowledgeReadResponse>;
1106
1131
  grepChunks(params: KnowledgeGrepParams): Promise<KnowledgeGrepResponse>;
1107
1132
  search(params: KnowledgeSearchParams): Promise<KnowledgeSearchResponse>;
1108
1133
  private resolveSearchDocuments;
1134
+ private loadReadableResult;
1135
+ private findLocalDocumentByRemoteId;
1136
+ private getPublishedDocumentJobId;
1109
1137
  }
1110
1138
 
1111
1139
  interface StoredAsyncParseJob {
@@ -1330,4 +1358,4 @@ declare class JobFailedError extends KnowhereError {
1330
1358
  constructor(message: string, code: string, jobResult: JobResult);
1331
1359
  }
1332
1360
 
1333
- 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, 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 };
1361
+ 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 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
@@ -1322,11 +1322,7 @@ var Jobs = class extends BaseResource {
1322
1322
  * Create a new parsing job
1323
1323
  */
1324
1324
  async create(params) {
1325
- const job = await this.httpClient.post(
1326
- "/v1/jobs",
1327
- params
1328
- );
1329
- delete job.documentId;
1325
+ const job = await this.httpClient.post("/v1/jobs", params);
1330
1326
  if (job.uploadUrl) {
1331
1327
  this.pendingUploadJobs.set(job.jobId, job);
1332
1328
  }
@@ -1510,6 +1506,9 @@ var Documents = class extends BaseResource {
1510
1506
  }
1511
1507
  };
1512
1508
 
1509
+ // src/knowledge/knowledge.ts
1510
+ var import_crypto2 = require("crypto");
1511
+
1513
1512
  // src/knowledge/local-store.ts
1514
1513
  var import_crypto = require("crypto");
1515
1514
  var import_os = __toESM(require("os"));
@@ -1738,6 +1737,8 @@ var MAX_READ_LIMIT = 40;
1738
1737
  var DEFAULT_GREP_LIMIT = 20;
1739
1738
  var MAX_GREP_LIMIT = 50;
1740
1739
  var DEFAULT_CONTEXT_CHARS = 80;
1740
+ var SAFE_LOCAL_DOCUMENT_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
1741
+ var REMOTE_DOCUMENT_ID_PATTERN = /^doc[_-]/;
1741
1742
  var Knowledge = class _Knowledge {
1742
1743
  client;
1743
1744
  store;
@@ -1793,6 +1794,19 @@ var Knowledge = class _Knowledge {
1793
1794
  });
1794
1795
  return { document, result };
1795
1796
  }
1797
+ async cacheDocument(params) {
1798
+ if (!params.documentId) {
1799
+ throw new ValidationError("documentId is required");
1800
+ }
1801
+ const existing = await this.findLocalDocumentByRemoteId(
1802
+ params.documentId
1803
+ );
1804
+ const jobId = await this.getPublishedDocumentJobId(params.documentId);
1805
+ return this.cacheJobResult({
1806
+ jobId,
1807
+ localDocumentId: params.localDocumentId ?? existing?.localDocumentId ?? createLocalDocumentIdForRemote(params.documentId)
1808
+ });
1809
+ }
1796
1810
  async resolveAsyncCache(jobId, isDone, isFailed) {
1797
1811
  const trackedJob = await this.store.getAsyncParseJob(jobId);
1798
1812
  if (!trackedJob) {
@@ -1849,8 +1863,8 @@ var Knowledge = class _Knowledge {
1849
1863
  async listDocuments() {
1850
1864
  return this.store.listDocuments();
1851
1865
  }
1852
- async getDocumentOutline(localDocumentId) {
1853
- const { document, result } = await this.store.loadResult(localDocumentId);
1866
+ async getDocumentOutline(reference) {
1867
+ const { document, result } = await this.loadReadableResult(reference);
1854
1868
  const chunks = indexChunks(result);
1855
1869
  const sections = buildFlatSections(result, chunks);
1856
1870
  const sectionTree = result.docNav?.sections && result.docNav.sections.length > 0 ? result.docNav.sections.map(
@@ -1865,7 +1879,7 @@ var Knowledge = class _Knowledge {
1865
1879
  };
1866
1880
  }
1867
1881
  async readChunks(params) {
1868
- const { document, result } = await this.store.loadResult(params.localDocumentId);
1882
+ const { document, result } = await this.loadReadableResult(params);
1869
1883
  const limit = clampLimit(params.limit, DEFAULT_READ_LIMIT, MAX_READ_LIMIT);
1870
1884
  const chunks = indexChunks(result).filter((chunk) => matchesReadScope(chunk, params));
1871
1885
  const selected = selectReadWindow(chunks, params, limit);
@@ -1881,7 +1895,7 @@ var Knowledge = class _Knowledge {
1881
1895
  if (!params.pattern) {
1882
1896
  throw new ValidationError("pattern is required");
1883
1897
  }
1884
- const { document, result } = await this.store.loadResult(params.localDocumentId);
1898
+ const { document, result } = await this.loadReadableResult(params);
1885
1899
  const maxResults = clampLimit(params.maxResults, DEFAULT_GREP_LIMIT, MAX_GREP_LIMIT);
1886
1900
  const contextChars = params.contextChars ?? DEFAULT_CONTEXT_CHARS;
1887
1901
  const matcher = createMatcher(params);
@@ -1951,7 +1965,68 @@ var Knowledge = class _Knowledge {
1951
1965
  const requested = new Set(localDocumentIds);
1952
1966
  return documents.filter((document) => requested.has(document.localDocumentId));
1953
1967
  }
1968
+ async loadReadableResult(reference) {
1969
+ const normalized = normalizeDocumentReference(reference);
1970
+ if (normalized.documentId) {
1971
+ return this.cacheDocument({
1972
+ documentId: normalized.documentId,
1973
+ localDocumentId: normalized.localDocumentId
1974
+ });
1975
+ }
1976
+ if (normalized.jobId) {
1977
+ return this.cacheJobResult({
1978
+ jobId: normalized.jobId,
1979
+ localDocumentId: normalized.localDocumentId
1980
+ });
1981
+ }
1982
+ if (!normalized.localDocumentId) {
1983
+ throw new ValidationError("localDocumentId, documentId, or jobId is required");
1984
+ }
1985
+ const document = await this.store.getDocument(normalized.localDocumentId);
1986
+ if (document) {
1987
+ return this.store.loadResult(normalized.localDocumentId);
1988
+ }
1989
+ if (looksLikeRemoteDocumentId(normalized.localDocumentId)) {
1990
+ return this.cacheDocument({
1991
+ documentId: normalized.localDocumentId,
1992
+ localDocumentId: createLocalDocumentIdForRemote(normalized.localDocumentId)
1993
+ });
1994
+ }
1995
+ throw new Error(`Local Knowhere document not found: ${normalized.localDocumentId}`);
1996
+ }
1997
+ async findLocalDocumentByRemoteId(documentId) {
1998
+ const documents = await this.store.listDocuments();
1999
+ return documents.find((document) => document.documentId === documentId);
2000
+ }
2001
+ async getPublishedDocumentJobId(documentId) {
2002
+ const response = await this.client.documents.listChunks(documentId, {
2003
+ page: 1,
2004
+ pageSize: 1
2005
+ });
2006
+ if (!response.jobId) {
2007
+ throw new Error(
2008
+ `Cannot sync server document ${documentId}: current published job id was not returned.`
2009
+ );
2010
+ }
2011
+ return response.jobId;
2012
+ }
1954
2013
  };
2014
+ function normalizeDocumentReference(reference) {
2015
+ if (typeof reference === "string") {
2016
+ return { localDocumentId: reference };
2017
+ }
2018
+ return reference;
2019
+ }
2020
+ function looksLikeRemoteDocumentId(value) {
2021
+ return REMOTE_DOCUMENT_ID_PATTERN.test(value);
2022
+ }
2023
+ function createLocalDocumentIdForRemote(documentId) {
2024
+ if (SAFE_LOCAL_DOCUMENT_ID_PATTERN.test(documentId) && !documentId.includes("..") && !documentId.includes("/") && !documentId.includes("\\")) {
2025
+ return documentId;
2026
+ }
2027
+ const hash = (0, import_crypto2.createHash)("sha256").update(documentId).digest("hex").slice(0, 16);
2028
+ return `remote_${hash}`;
2029
+ }
1955
2030
  function indexChunks(result) {
1956
2031
  return result.chunks.map((chunk, index) => {
1957
2032
  const filePath = getChunkFilePath2(chunk);
@@ -2343,6 +2418,7 @@ var Knowhere = class {
2343
2418
  dataId: params.dataId,
2344
2419
  namespace: params.namespace,
2345
2420
  documentId: params.documentId,
2421
+ documentMetadata: params.documentMetadata,
2346
2422
  parsingParams: buildParsingParams(params),
2347
2423
  webhook
2348
2424
  });
package/dist/index.mjs CHANGED
@@ -1262,11 +1262,7 @@ var Jobs = class extends BaseResource {
1262
1262
  * Create a new parsing job
1263
1263
  */
1264
1264
  async create(params) {
1265
- const job = await this.httpClient.post(
1266
- "/v1/jobs",
1267
- params
1268
- );
1269
- delete job.documentId;
1265
+ const job = await this.httpClient.post("/v1/jobs", params);
1270
1266
  if (job.uploadUrl) {
1271
1267
  this.pendingUploadJobs.set(job.jobId, job);
1272
1268
  }
@@ -1450,6 +1446,9 @@ var Documents = class extends BaseResource {
1450
1446
  }
1451
1447
  };
1452
1448
 
1449
+ // src/knowledge/knowledge.ts
1450
+ import { createHash as createHash2 } from "crypto";
1451
+
1453
1452
  // src/knowledge/local-store.ts
1454
1453
  import { createHash } from "crypto";
1455
1454
  import os from "os";
@@ -1678,6 +1677,8 @@ var MAX_READ_LIMIT = 40;
1678
1677
  var DEFAULT_GREP_LIMIT = 20;
1679
1678
  var MAX_GREP_LIMIT = 50;
1680
1679
  var DEFAULT_CONTEXT_CHARS = 80;
1680
+ var SAFE_LOCAL_DOCUMENT_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
1681
+ var REMOTE_DOCUMENT_ID_PATTERN = /^doc[_-]/;
1681
1682
  var Knowledge = class _Knowledge {
1682
1683
  client;
1683
1684
  store;
@@ -1733,6 +1734,19 @@ var Knowledge = class _Knowledge {
1733
1734
  });
1734
1735
  return { document, result };
1735
1736
  }
1737
+ async cacheDocument(params) {
1738
+ if (!params.documentId) {
1739
+ throw new ValidationError("documentId is required");
1740
+ }
1741
+ const existing = await this.findLocalDocumentByRemoteId(
1742
+ params.documentId
1743
+ );
1744
+ const jobId = await this.getPublishedDocumentJobId(params.documentId);
1745
+ return this.cacheJobResult({
1746
+ jobId,
1747
+ localDocumentId: params.localDocumentId ?? existing?.localDocumentId ?? createLocalDocumentIdForRemote(params.documentId)
1748
+ });
1749
+ }
1736
1750
  async resolveAsyncCache(jobId, isDone, isFailed) {
1737
1751
  const trackedJob = await this.store.getAsyncParseJob(jobId);
1738
1752
  if (!trackedJob) {
@@ -1789,8 +1803,8 @@ var Knowledge = class _Knowledge {
1789
1803
  async listDocuments() {
1790
1804
  return this.store.listDocuments();
1791
1805
  }
1792
- async getDocumentOutline(localDocumentId) {
1793
- const { document, result } = await this.store.loadResult(localDocumentId);
1806
+ async getDocumentOutline(reference) {
1807
+ const { document, result } = await this.loadReadableResult(reference);
1794
1808
  const chunks = indexChunks(result);
1795
1809
  const sections = buildFlatSections(result, chunks);
1796
1810
  const sectionTree = result.docNav?.sections && result.docNav.sections.length > 0 ? result.docNav.sections.map(
@@ -1805,7 +1819,7 @@ var Knowledge = class _Knowledge {
1805
1819
  };
1806
1820
  }
1807
1821
  async readChunks(params) {
1808
- const { document, result } = await this.store.loadResult(params.localDocumentId);
1822
+ const { document, result } = await this.loadReadableResult(params);
1809
1823
  const limit = clampLimit(params.limit, DEFAULT_READ_LIMIT, MAX_READ_LIMIT);
1810
1824
  const chunks = indexChunks(result).filter((chunk) => matchesReadScope(chunk, params));
1811
1825
  const selected = selectReadWindow(chunks, params, limit);
@@ -1821,7 +1835,7 @@ var Knowledge = class _Knowledge {
1821
1835
  if (!params.pattern) {
1822
1836
  throw new ValidationError("pattern is required");
1823
1837
  }
1824
- const { document, result } = await this.store.loadResult(params.localDocumentId);
1838
+ const { document, result } = await this.loadReadableResult(params);
1825
1839
  const maxResults = clampLimit(params.maxResults, DEFAULT_GREP_LIMIT, MAX_GREP_LIMIT);
1826
1840
  const contextChars = params.contextChars ?? DEFAULT_CONTEXT_CHARS;
1827
1841
  const matcher = createMatcher(params);
@@ -1891,7 +1905,68 @@ var Knowledge = class _Knowledge {
1891
1905
  const requested = new Set(localDocumentIds);
1892
1906
  return documents.filter((document) => requested.has(document.localDocumentId));
1893
1907
  }
1908
+ async loadReadableResult(reference) {
1909
+ const normalized = normalizeDocumentReference(reference);
1910
+ if (normalized.documentId) {
1911
+ return this.cacheDocument({
1912
+ documentId: normalized.documentId,
1913
+ localDocumentId: normalized.localDocumentId
1914
+ });
1915
+ }
1916
+ if (normalized.jobId) {
1917
+ return this.cacheJobResult({
1918
+ jobId: normalized.jobId,
1919
+ localDocumentId: normalized.localDocumentId
1920
+ });
1921
+ }
1922
+ if (!normalized.localDocumentId) {
1923
+ throw new ValidationError("localDocumentId, documentId, or jobId is required");
1924
+ }
1925
+ const document = await this.store.getDocument(normalized.localDocumentId);
1926
+ if (document) {
1927
+ return this.store.loadResult(normalized.localDocumentId);
1928
+ }
1929
+ if (looksLikeRemoteDocumentId(normalized.localDocumentId)) {
1930
+ return this.cacheDocument({
1931
+ documentId: normalized.localDocumentId,
1932
+ localDocumentId: createLocalDocumentIdForRemote(normalized.localDocumentId)
1933
+ });
1934
+ }
1935
+ throw new Error(`Local Knowhere document not found: ${normalized.localDocumentId}`);
1936
+ }
1937
+ async findLocalDocumentByRemoteId(documentId) {
1938
+ const documents = await this.store.listDocuments();
1939
+ return documents.find((document) => document.documentId === documentId);
1940
+ }
1941
+ async getPublishedDocumentJobId(documentId) {
1942
+ const response = await this.client.documents.listChunks(documentId, {
1943
+ page: 1,
1944
+ pageSize: 1
1945
+ });
1946
+ if (!response.jobId) {
1947
+ throw new Error(
1948
+ `Cannot sync server document ${documentId}: current published job id was not returned.`
1949
+ );
1950
+ }
1951
+ return response.jobId;
1952
+ }
1894
1953
  };
1954
+ function normalizeDocumentReference(reference) {
1955
+ if (typeof reference === "string") {
1956
+ return { localDocumentId: reference };
1957
+ }
1958
+ return reference;
1959
+ }
1960
+ function looksLikeRemoteDocumentId(value) {
1961
+ return REMOTE_DOCUMENT_ID_PATTERN.test(value);
1962
+ }
1963
+ function createLocalDocumentIdForRemote(documentId) {
1964
+ if (SAFE_LOCAL_DOCUMENT_ID_PATTERN.test(documentId) && !documentId.includes("..") && !documentId.includes("/") && !documentId.includes("\\")) {
1965
+ return documentId;
1966
+ }
1967
+ const hash = createHash2("sha256").update(documentId).digest("hex").slice(0, 16);
1968
+ return `remote_${hash}`;
1969
+ }
1895
1970
  function indexChunks(result) {
1896
1971
  return result.chunks.map((chunk, index) => {
1897
1972
  const filePath = getChunkFilePath2(chunk);
@@ -2283,6 +2358,7 @@ var Knowhere = class {
2283
2358
  dataId: params.dataId,
2284
2359
  namespace: params.namespace,
2285
2360
  documentId: params.documentId,
2361
+ documentMetadata: params.documentMetadata,
2286
2362
  parsingParams: buildParsingParams(params),
2287
2363
  webhook
2288
2364
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ontos-ai/knowhere-sdk",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "Official Node.js SDK for Knowhere document parsing API",
5
5
  "keywords": [
6
6
  "knowhere",