@babav/knowledge-core-client 0.5.0 → 0.7.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 +34 -7
- package/dist/index.js +23 -3
- package/package.json +1 -1
- package/src/index.ts +33 -3
package/dist/index.d.ts
CHANGED
|
@@ -73,7 +73,6 @@ export interface QueryResponse {
|
|
|
73
73
|
groundedness: Groundedness | null;
|
|
74
74
|
usage: Record<string, number | null>;
|
|
75
75
|
standalone_query: string | null;
|
|
76
|
-
hops: string[];
|
|
77
76
|
}
|
|
78
77
|
export interface Citation {
|
|
79
78
|
quote: string;
|
|
@@ -124,6 +123,14 @@ export interface DocumentCount {
|
|
|
124
123
|
in_flight: number;
|
|
125
124
|
failed: number;
|
|
126
125
|
}
|
|
126
|
+
/** A signed PUT URL for uploading a large file straight to GCS + the pre-allocated
|
|
127
|
+
* document id to reference on from-upload. */
|
|
128
|
+
export interface UploadUrl {
|
|
129
|
+
upload_url: string;
|
|
130
|
+
document_id: UUID;
|
|
131
|
+
gcs_uri: string;
|
|
132
|
+
expires_in: number;
|
|
133
|
+
}
|
|
127
134
|
export interface Conversation {
|
|
128
135
|
id: UUID;
|
|
129
136
|
title: string | null;
|
|
@@ -233,12 +240,6 @@ declare class HttpBase {
|
|
|
233
240
|
protected pageAll<T>(path: string, query?: RequestOpts["query"]): Promise<T[]>;
|
|
234
241
|
}
|
|
235
242
|
export interface StreamHandlers {
|
|
236
|
-
onHop?: (d: {
|
|
237
|
-
n: number;
|
|
238
|
-
subquery: string;
|
|
239
|
-
status: string;
|
|
240
|
-
retrieval_contents?: number;
|
|
241
|
-
}) => void;
|
|
242
243
|
onSources?: (d: {
|
|
243
244
|
retrieval_contents: RetrievalContent[];
|
|
244
245
|
}) => void;
|
|
@@ -304,6 +305,32 @@ export declare class KnowledgeCoreClient extends HttpBase {
|
|
|
304
305
|
folder_id?: UUID;
|
|
305
306
|
custom_metadata?: Record<string, unknown>;
|
|
306
307
|
}) => Promise<Document>;
|
|
308
|
+
/** Mint a signed PUT URL to upload a large file straight to GCS (bypasses the
|
|
309
|
+
* ~32 MB request limit). PUT the bytes to upload_url, then ingestFromUpload. */
|
|
310
|
+
uploadUrl: (id: UUID, a: {
|
|
311
|
+
filename: string;
|
|
312
|
+
content_type?: string;
|
|
313
|
+
}) => Promise<UploadUrl>;
|
|
314
|
+
/** Ingest a file already PUT to GCS via uploadUrl. Resolves with the `pending`
|
|
315
|
+
* document (202); track via documents.get() / webhook. */
|
|
316
|
+
ingestFromUpload: (id: UUID, a: {
|
|
317
|
+
document_id: UUID;
|
|
318
|
+
filename: string;
|
|
319
|
+
content_type?: string;
|
|
320
|
+
visibility?: Visibility;
|
|
321
|
+
folder_id?: UUID;
|
|
322
|
+
custom_metadata?: Record<string, unknown>;
|
|
323
|
+
}) => Promise<Document>;
|
|
324
|
+
/** Convenience for LARGE files: uploadUrl → PUT the bytes to GCS → ingestFromUpload.
|
|
325
|
+
* Use this instead of ingestDocument when the file may exceed ~32 MB. */
|
|
326
|
+
uploadDocument: (id: UUID, a: {
|
|
327
|
+
file: FileData;
|
|
328
|
+
filename: string;
|
|
329
|
+
content_type?: string;
|
|
330
|
+
visibility?: Visibility;
|
|
331
|
+
folder_id?: UUID;
|
|
332
|
+
custom_metadata?: Record<string, unknown>;
|
|
333
|
+
}) => Promise<Document>;
|
|
307
334
|
};
|
|
308
335
|
parse(a: {
|
|
309
336
|
file: FileData;
|
package/dist/index.js
CHANGED
|
@@ -217,6 +217,29 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
217
217
|
fd.append("folder_id", a.folder_id);
|
|
218
218
|
return this.request("POST", `/v1/corpora/${id}/documents`, { body: fd });
|
|
219
219
|
},
|
|
220
|
+
/** Mint a signed PUT URL to upload a large file straight to GCS (bypasses the
|
|
221
|
+
* ~32 MB request limit). PUT the bytes to upload_url, then ingestFromUpload. */
|
|
222
|
+
uploadUrl: (id, a) => this.request("POST", `/v1/corpora/${id}/documents/upload-url`, { json: a }),
|
|
223
|
+
/** Ingest a file already PUT to GCS via uploadUrl. Resolves with the `pending`
|
|
224
|
+
* document (202); track via documents.get() / webhook. */
|
|
225
|
+
ingestFromUpload: (id, a) => this.request("POST", `/v1/corpora/${id}/documents/from-upload`, { json: a }),
|
|
226
|
+
/** Convenience for LARGE files: uploadUrl → PUT the bytes to GCS → ingestFromUpload.
|
|
227
|
+
* Use this instead of ingestDocument when the file may exceed ~32 MB. */
|
|
228
|
+
uploadDocument: async (id, a) => {
|
|
229
|
+
const u = await this.corpora.uploadUrl(id, { filename: a.filename, content_type: a.content_type });
|
|
230
|
+
const put = await this._fetch(u.upload_url, {
|
|
231
|
+
method: "PUT",
|
|
232
|
+
body: toBlob(a.file, a.content_type),
|
|
233
|
+
headers: a.content_type ? { "content-type": a.content_type } : {},
|
|
234
|
+
});
|
|
235
|
+
if (!put.ok) {
|
|
236
|
+
throw new KnowledgeCoreError(put.status, safeJson(await put.text()), u.upload_url);
|
|
237
|
+
}
|
|
238
|
+
return this.corpora.ingestFromUpload(id, {
|
|
239
|
+
document_id: u.document_id, filename: a.filename, content_type: a.content_type,
|
|
240
|
+
visibility: a.visibility, folder_id: a.folder_id, custom_metadata: a.custom_metadata,
|
|
241
|
+
});
|
|
242
|
+
},
|
|
220
243
|
};
|
|
221
244
|
// --- parsing (utility: file -> text, stores nothing) ---
|
|
222
245
|
parse(a) {
|
|
@@ -331,9 +354,6 @@ function dispatchSse(frame, h) {
|
|
|
331
354
|
const data = safeJson(dataLines.join("\n"));
|
|
332
355
|
h.onEvent?.(event, data);
|
|
333
356
|
switch (event) {
|
|
334
|
-
case "hop":
|
|
335
|
-
h.onHop?.(data);
|
|
336
|
-
break;
|
|
337
357
|
case "retrieval_contents":
|
|
338
358
|
h.onSources?.(data);
|
|
339
359
|
break;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -85,7 +85,6 @@ export interface QueryResponse {
|
|
|
85
85
|
groundedness: Groundedness | null;
|
|
86
86
|
usage: Record<string, number | null>;
|
|
87
87
|
standalone_query: string | null;
|
|
88
|
-
hops: string[];
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
export interface Citation {
|
|
@@ -138,6 +137,14 @@ export interface DocumentCount {
|
|
|
138
137
|
in_flight: number;
|
|
139
138
|
failed: number;
|
|
140
139
|
}
|
|
140
|
+
/** A signed PUT URL for uploading a large file straight to GCS + the pre-allocated
|
|
141
|
+
* document id to reference on from-upload. */
|
|
142
|
+
export interface UploadUrl {
|
|
143
|
+
upload_url: string;
|
|
144
|
+
document_id: UUID;
|
|
145
|
+
gcs_uri: string;
|
|
146
|
+
expires_in: number;
|
|
147
|
+
}
|
|
141
148
|
export interface Conversation {
|
|
142
149
|
id: UUID;
|
|
143
150
|
title: string | null;
|
|
@@ -360,7 +367,6 @@ function toBlob(data: FileData, contentType?: string): Blob {
|
|
|
360
367
|
// SSE handlers
|
|
361
368
|
// ---------------------------------------------------------------------------
|
|
362
369
|
export interface StreamHandlers {
|
|
363
|
-
onHop?: (d: { n: number; subquery: string; status: string; retrieval_contents?: number }) => void;
|
|
364
370
|
onSources?: (d: { retrieval_contents: RetrievalContent[] }) => void;
|
|
365
371
|
onToken?: (text: string) => void;
|
|
366
372
|
onFinal?: (d: QueryResponse) => void;
|
|
@@ -443,6 +449,31 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
443
449
|
if (a.folder_id) fd.append("folder_id", a.folder_id);
|
|
444
450
|
return this.request<Document>("POST", `/v1/corpora/${id}/documents`, { body: fd });
|
|
445
451
|
},
|
|
452
|
+
/** Mint a signed PUT URL to upload a large file straight to GCS (bypasses the
|
|
453
|
+
* ~32 MB request limit). PUT the bytes to upload_url, then ingestFromUpload. */
|
|
454
|
+
uploadUrl: (id: UUID, a: { filename: string; content_type?: string }) =>
|
|
455
|
+
this.request<UploadUrl>("POST", `/v1/corpora/${id}/documents/upload-url`, { json: a }),
|
|
456
|
+
/** Ingest a file already PUT to GCS via uploadUrl. Resolves with the `pending`
|
|
457
|
+
* document (202); track via documents.get() / webhook. */
|
|
458
|
+
ingestFromUpload: (id: UUID, a: { document_id: UUID; filename: string; content_type?: string; visibility?: Visibility; folder_id?: UUID; custom_metadata?: Record<string, unknown> }) =>
|
|
459
|
+
this.request<Document>("POST", `/v1/corpora/${id}/documents/from-upload`, { json: a }),
|
|
460
|
+
/** Convenience for LARGE files: uploadUrl → PUT the bytes to GCS → ingestFromUpload.
|
|
461
|
+
* Use this instead of ingestDocument when the file may exceed ~32 MB. */
|
|
462
|
+
uploadDocument: async (id: UUID, a: { file: FileData; filename: string; content_type?: string; visibility?: Visibility; folder_id?: UUID; custom_metadata?: Record<string, unknown> }): Promise<Document> => {
|
|
463
|
+
const u = await this.corpora.uploadUrl(id, { filename: a.filename, content_type: a.content_type });
|
|
464
|
+
const put = await this._fetch(u.upload_url, {
|
|
465
|
+
method: "PUT",
|
|
466
|
+
body: toBlob(a.file, a.content_type),
|
|
467
|
+
headers: a.content_type ? { "content-type": a.content_type } : {},
|
|
468
|
+
});
|
|
469
|
+
if (!put.ok) {
|
|
470
|
+
throw new KnowledgeCoreError(put.status, safeJson(await put.text()), u.upload_url);
|
|
471
|
+
}
|
|
472
|
+
return this.corpora.ingestFromUpload(id, {
|
|
473
|
+
document_id: u.document_id, filename: a.filename, content_type: a.content_type,
|
|
474
|
+
visibility: a.visibility, folder_id: a.folder_id, custom_metadata: a.custom_metadata,
|
|
475
|
+
});
|
|
476
|
+
},
|
|
446
477
|
};
|
|
447
478
|
|
|
448
479
|
// --- parsing (utility: file -> text, stores nothing) ---
|
|
@@ -579,7 +610,6 @@ function dispatchSse(frame: string, h: StreamHandlers): void {
|
|
|
579
610
|
const data = safeJson(dataLines.join("\n"));
|
|
580
611
|
h.onEvent?.(event, data);
|
|
581
612
|
switch (event) {
|
|
582
|
-
case "hop": h.onHop?.(data as never); break;
|
|
583
613
|
case "retrieval_contents": h.onSources?.(data as never); break;
|
|
584
614
|
case "token": h.onToken?.((data as { text: string }).text); break;
|
|
585
615
|
case "final": h.onFinal?.(data as QueryResponse); break;
|