@babav/knowledge-core-client 0.3.0 → 0.5.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 +29 -5
- package/dist/index.js +46 -3
- package/package.json +1 -1
- package/src/index.ts +55 -7
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,14 @@
|
|
|
11
11
|
* Auth: every call carries an X-API-Key. Use KnowledgeCoreClient with a TENANT
|
|
12
12
|
* key for all data ops; use AdminClient with the ADMIN key for tenant / API-key /
|
|
13
13
|
* agent management. The key is server-side only — never ship it to a browser.
|
|
14
|
+
*
|
|
15
|
+
* Configuration — environment variables (Node `process.env` or Deno `Deno.env`):
|
|
16
|
+
* `new KnowledgeCoreClient()` reads BABAV_KC_BASE_URL + BABAV_KC_TENANT_KEY
|
|
17
|
+
* `new AdminClient()` reads BABAV_KC_BASE_URL + BABAV_KC_ADMIN_KEY
|
|
18
|
+
* Passing { baseUrl } / { apiKey } explicitly overrides the env var for that field
|
|
19
|
+
* (the app is then responsible for supplying it). A missing value (no param AND no
|
|
20
|
+
* env var) throws at construction with a clear message. In Deno, env access needs
|
|
21
|
+
* `--allow-env` (without it, pass the values explicitly).
|
|
14
22
|
*/
|
|
15
23
|
export type UUID = string;
|
|
16
24
|
export interface Page<T> {
|
|
@@ -196,10 +204,12 @@ export declare class KnowledgeCoreError extends Error {
|
|
|
196
204
|
get code(): string | undefined;
|
|
197
205
|
}
|
|
198
206
|
export interface ClientOptions {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
*
|
|
207
|
+
/** API base URL. Defaults to env BABAV_KC_BASE_URL when omitted. */
|
|
208
|
+
baseUrl?: string;
|
|
209
|
+
/** Tenant key (KnowledgeCoreClient) or admin key (AdminClient). Defaults to env
|
|
210
|
+
* BABAV_KC_TENANT_KEY / BABAV_KC_ADMIN_KEY respectively when omitted. */
|
|
211
|
+
apiKey?: string;
|
|
212
|
+
/** Optional default fetch timeout (ms). Streaming ignores this. */
|
|
203
213
|
timeoutMs?: number;
|
|
204
214
|
fetch?: typeof fetch;
|
|
205
215
|
}
|
|
@@ -215,7 +225,7 @@ declare class HttpBase {
|
|
|
215
225
|
protected readonly apiKey: string;
|
|
216
226
|
protected readonly timeoutMs?: number;
|
|
217
227
|
protected readonly _fetch: typeof fetch;
|
|
218
|
-
constructor(opts
|
|
228
|
+
constructor(opts?: ClientOptions, keyEnvVar?: string);
|
|
219
229
|
protected url(path: string, query?: RequestOpts["query"]): string;
|
|
220
230
|
protected raw(method: string, path: string, opts?: RequestOpts): Promise<Response>;
|
|
221
231
|
protected request<T>(method: string, path: string, opts?: RequestOpts): Promise<T>;
|
|
@@ -240,6 +250,8 @@ export interface StreamHandlers {
|
|
|
240
250
|
onEvent?: (event: string, data: unknown) => void;
|
|
241
251
|
}
|
|
242
252
|
export declare class KnowledgeCoreClient extends HttpBase {
|
|
253
|
+
/** Defaults to env BABAV_KC_BASE_URL + BABAV_KC_TENANT_KEY when not passed. */
|
|
254
|
+
constructor(opts?: ClientOptions);
|
|
243
255
|
query(agentId: UUID, body: QueryRequest): Promise<QueryResponse>;
|
|
244
256
|
/** Streaming query (SSE). Resolves when the stream ends. */
|
|
245
257
|
queryStream(agentId: UUID, body: QueryRequest, handlers: StreamHandlers, signal?: AbortSignal): Promise<void>;
|
|
@@ -276,6 +288,11 @@ export declare class KnowledgeCoreClient extends HttpBase {
|
|
|
276
288
|
}) => Promise<Page<Document>>;
|
|
277
289
|
/** Count documents in the corpus: { total, indexed, in_flight, failed }. */
|
|
278
290
|
countDocuments: (id: UUID) => Promise<DocumentCount>;
|
|
291
|
+
/** Documents in the corpus whose filename contains `q` (case-insensitive), paginated. */
|
|
292
|
+
searchDocuments: (id: UUID, q: string, opts?: {
|
|
293
|
+
limit?: number;
|
|
294
|
+
cursor?: string;
|
|
295
|
+
}) => Promise<Page<Document>>;
|
|
279
296
|
/** Upload a file and ingest it ASYNCHRONOUSLY — resolves with the created
|
|
280
297
|
* document at status `pending` (HTTP 202); track via documents.get() polling
|
|
281
298
|
* the status to `indexed`/`failed`, or the tenant result webhook. */
|
|
@@ -305,6 +322,11 @@ export declare class KnowledgeCoreClient extends HttpBase {
|
|
|
305
322
|
}) => Promise<Page<Document>>;
|
|
306
323
|
/** Count documents in the folder: { total, indexed, in_flight, failed }. */
|
|
307
324
|
countDocuments: (folderId: UUID) => Promise<DocumentCount>;
|
|
325
|
+
/** Documents in the folder whose filename contains `q` (case-insensitive), paginated. */
|
|
326
|
+
searchDocuments: (folderId: UUID, q: string, opts?: {
|
|
327
|
+
limit?: number;
|
|
328
|
+
cursor?: string;
|
|
329
|
+
}) => Promise<Page<Document>>;
|
|
308
330
|
};
|
|
309
331
|
documents: {
|
|
310
332
|
get: (id: UUID) => Promise<Document>;
|
|
@@ -386,6 +408,8 @@ export declare class KnowledgeCoreClient extends HttpBase {
|
|
|
386
408
|
};
|
|
387
409
|
}
|
|
388
410
|
export declare class AdminClient extends HttpBase {
|
|
411
|
+
/** Defaults to env BABAV_KC_BASE_URL + BABAV_KC_ADMIN_KEY when not passed. */
|
|
412
|
+
constructor(opts?: ClientOptions);
|
|
389
413
|
tenants: {
|
|
390
414
|
create: (b: Partial<Tenant> & {
|
|
391
415
|
name: string;
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,14 @@
|
|
|
11
11
|
* Auth: every call carries an X-API-Key. Use KnowledgeCoreClient with a TENANT
|
|
12
12
|
* key for all data ops; use AdminClient with the ADMIN key for tenant / API-key /
|
|
13
13
|
* agent management. The key is server-side only — never ship it to a browser.
|
|
14
|
+
*
|
|
15
|
+
* Configuration — environment variables (Node `process.env` or Deno `Deno.env`):
|
|
16
|
+
* `new KnowledgeCoreClient()` reads BABAV_KC_BASE_URL + BABAV_KC_TENANT_KEY
|
|
17
|
+
* `new AdminClient()` reads BABAV_KC_BASE_URL + BABAV_KC_ADMIN_KEY
|
|
18
|
+
* Passing { baseUrl } / { apiKey } explicitly overrides the env var for that field
|
|
19
|
+
* (the app is then responsible for supplying it). A missing value (no param AND no
|
|
20
|
+
* env var) throws at construction with a clear message. In Deno, env access needs
|
|
21
|
+
* `--allow-env` (without it, pass the values explicitly).
|
|
14
22
|
*/
|
|
15
23
|
// ---------------------------------------------------------------------------
|
|
16
24
|
// Errors
|
|
@@ -35,14 +43,37 @@ export class KnowledgeCoreError extends Error {
|
|
|
35
43
|
return d?.detail?.error ?? d?.error;
|
|
36
44
|
}
|
|
37
45
|
}
|
|
46
|
+
/** Read an env var under Node (process.env) or Deno (Deno.env); undefined if unset
|
|
47
|
+
* or inaccessible (e.g. Deno without --allow-env). */
|
|
48
|
+
function readEnv(name) {
|
|
49
|
+
const g = globalThis;
|
|
50
|
+
const fromNode = g.process?.env?.[name];
|
|
51
|
+
if (fromNode)
|
|
52
|
+
return fromNode;
|
|
53
|
+
try {
|
|
54
|
+
const fromDeno = g.Deno?.env?.get?.(name);
|
|
55
|
+
if (fromDeno)
|
|
56
|
+
return fromDeno;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
/* Deno env access denied (no --allow-env) — treat as unset */
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
38
63
|
class HttpBase {
|
|
39
64
|
baseUrl;
|
|
40
65
|
apiKey;
|
|
41
66
|
timeoutMs;
|
|
42
67
|
_fetch;
|
|
43
|
-
constructor(opts) {
|
|
44
|
-
|
|
45
|
-
|
|
68
|
+
constructor(opts = {}, keyEnvVar = "BABAV_KC_TENANT_KEY") {
|
|
69
|
+
const baseUrl = opts.baseUrl ?? readEnv("BABAV_KC_BASE_URL");
|
|
70
|
+
const apiKey = opts.apiKey ?? readEnv(keyEnvVar);
|
|
71
|
+
if (!baseUrl)
|
|
72
|
+
throw new Error("KnowledgeCore: no base URL — pass { baseUrl } or set BABAV_KC_BASE_URL");
|
|
73
|
+
if (!apiKey)
|
|
74
|
+
throw new Error(`KnowledgeCore: no API key — pass { apiKey } or set ${keyEnvVar}`);
|
|
75
|
+
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
76
|
+
this.apiKey = apiKey;
|
|
46
77
|
this.timeoutMs = opts.timeoutMs;
|
|
47
78
|
this._fetch = opts.fetch ?? fetch;
|
|
48
79
|
}
|
|
@@ -121,6 +152,10 @@ function toBlob(data, contentType) {
|
|
|
121
152
|
// Tenant client (data ops) — use a TENANT key
|
|
122
153
|
// ---------------------------------------------------------------------------
|
|
123
154
|
export class KnowledgeCoreClient extends HttpBase {
|
|
155
|
+
/** Defaults to env BABAV_KC_BASE_URL + BABAV_KC_TENANT_KEY when not passed. */
|
|
156
|
+
constructor(opts = {}) {
|
|
157
|
+
super(opts, "BABAV_KC_TENANT_KEY");
|
|
158
|
+
}
|
|
124
159
|
// --- query (agent-anchored) ---
|
|
125
160
|
query(agentId, body) {
|
|
126
161
|
return this.request("POST", `/v1/agents/${agentId}/query`, { json: body });
|
|
@@ -166,6 +201,8 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
166
201
|
listDocuments: (id, q) => this.request("GET", `/v1/corpora/${id}/documents`, { query: q }),
|
|
167
202
|
/** Count documents in the corpus: { total, indexed, in_flight, failed }. */
|
|
168
203
|
countDocuments: (id) => this.request("GET", `/v1/corpora/${id}/documents/count`),
|
|
204
|
+
/** Documents in the corpus whose filename contains `q` (case-insensitive), paginated. */
|
|
205
|
+
searchDocuments: (id, q, opts) => this.request("GET", `/v1/corpora/${id}/documents/search`, { query: { q, ...opts } }),
|
|
169
206
|
/** Upload a file and ingest it ASYNCHRONOUSLY — resolves with the created
|
|
170
207
|
* document at status `pending` (HTTP 202); track via documents.get() polling
|
|
171
208
|
* the status to `indexed`/`failed`, or the tenant result webhook. */
|
|
@@ -195,6 +232,8 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
195
232
|
listDocuments: (folderId, q) => this.request("GET", `/v1/folders/${folderId}/documents`, { query: q }),
|
|
196
233
|
/** Count documents in the folder: { total, indexed, in_flight, failed }. */
|
|
197
234
|
countDocuments: (folderId) => this.request("GET", `/v1/folders/${folderId}/documents/count`),
|
|
235
|
+
/** Documents in the folder whose filename contains `q` (case-insensitive), paginated. */
|
|
236
|
+
searchDocuments: (folderId, q, opts) => this.request("GET", `/v1/folders/${folderId}/documents/search`, { query: { q, ...opts } }),
|
|
198
237
|
};
|
|
199
238
|
// --- documents ---
|
|
200
239
|
documents = {
|
|
@@ -253,6 +292,10 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
253
292
|
// Admin client (tenant + key + agent management) — use the ADMIN key
|
|
254
293
|
// ---------------------------------------------------------------------------
|
|
255
294
|
export class AdminClient extends HttpBase {
|
|
295
|
+
/** Defaults to env BABAV_KC_BASE_URL + BABAV_KC_ADMIN_KEY when not passed. */
|
|
296
|
+
constructor(opts = {}) {
|
|
297
|
+
super(opts, "BABAV_KC_ADMIN_KEY");
|
|
298
|
+
}
|
|
256
299
|
tenants = {
|
|
257
300
|
create: (b) => this.request("POST", "/v1/tenants", { json: b }),
|
|
258
301
|
list: (q) => this.request("GET", "/v1/tenants", { query: q }),
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -11,6 +11,14 @@
|
|
|
11
11
|
* Auth: every call carries an X-API-Key. Use KnowledgeCoreClient with a TENANT
|
|
12
12
|
* key for all data ops; use AdminClient with the ADMIN key for tenant / API-key /
|
|
13
13
|
* agent management. The key is server-side only — never ship it to a browser.
|
|
14
|
+
*
|
|
15
|
+
* Configuration — environment variables (Node `process.env` or Deno `Deno.env`):
|
|
16
|
+
* `new KnowledgeCoreClient()` reads BABAV_KC_BASE_URL + BABAV_KC_TENANT_KEY
|
|
17
|
+
* `new AdminClient()` reads BABAV_KC_BASE_URL + BABAV_KC_ADMIN_KEY
|
|
18
|
+
* Passing { baseUrl } / { apiKey } explicitly overrides the env var for that field
|
|
19
|
+
* (the app is then responsible for supplying it). A missing value (no param AND no
|
|
20
|
+
* env var) throws at construction with a clear message. In Deno, env access needs
|
|
21
|
+
* `--allow-env` (without it, pass the values explicitly).
|
|
14
22
|
*/
|
|
15
23
|
|
|
16
24
|
// ---------------------------------------------------------------------------
|
|
@@ -225,14 +233,34 @@ export class KnowledgeCoreError extends Error {
|
|
|
225
233
|
// Base HTTP
|
|
226
234
|
// ---------------------------------------------------------------------------
|
|
227
235
|
export interface ClientOptions {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
*
|
|
236
|
+
/** API base URL. Defaults to env BABAV_KC_BASE_URL when omitted. */
|
|
237
|
+
baseUrl?: string;
|
|
238
|
+
/** Tenant key (KnowledgeCoreClient) or admin key (AdminClient). Defaults to env
|
|
239
|
+
* BABAV_KC_TENANT_KEY / BABAV_KC_ADMIN_KEY respectively when omitted. */
|
|
240
|
+
apiKey?: string;
|
|
241
|
+
/** Optional default fetch timeout (ms). Streaming ignores this. */
|
|
232
242
|
timeoutMs?: number;
|
|
233
243
|
fetch?: typeof fetch; // override for tests
|
|
234
244
|
}
|
|
235
245
|
|
|
246
|
+
/** Read an env var under Node (process.env) or Deno (Deno.env); undefined if unset
|
|
247
|
+
* or inaccessible (e.g. Deno without --allow-env). */
|
|
248
|
+
function readEnv(name: string): string | undefined {
|
|
249
|
+
const g = globalThis as {
|
|
250
|
+
process?: { env?: Record<string, string | undefined> };
|
|
251
|
+
Deno?: { env?: { get?: (n: string) => string | undefined } };
|
|
252
|
+
};
|
|
253
|
+
const fromNode = g.process?.env?.[name];
|
|
254
|
+
if (fromNode) return fromNode;
|
|
255
|
+
try {
|
|
256
|
+
const fromDeno = g.Deno?.env?.get?.(name);
|
|
257
|
+
if (fromDeno) return fromDeno;
|
|
258
|
+
} catch {
|
|
259
|
+
/* Deno env access denied (no --allow-env) — treat as unset */
|
|
260
|
+
}
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
236
264
|
interface RequestOpts {
|
|
237
265
|
query?: Record<string, string | number | boolean | undefined>;
|
|
238
266
|
json?: unknown;
|
|
@@ -247,9 +275,13 @@ class HttpBase {
|
|
|
247
275
|
protected readonly timeoutMs?: number;
|
|
248
276
|
protected readonly _fetch: typeof fetch;
|
|
249
277
|
|
|
250
|
-
constructor(opts: ClientOptions) {
|
|
251
|
-
|
|
252
|
-
|
|
278
|
+
constructor(opts: ClientOptions = {}, keyEnvVar = "BABAV_KC_TENANT_KEY") {
|
|
279
|
+
const baseUrl = opts.baseUrl ?? readEnv("BABAV_KC_BASE_URL");
|
|
280
|
+
const apiKey = opts.apiKey ?? readEnv(keyEnvVar);
|
|
281
|
+
if (!baseUrl) throw new Error("KnowledgeCore: no base URL — pass { baseUrl } or set BABAV_KC_BASE_URL");
|
|
282
|
+
if (!apiKey) throw new Error(`KnowledgeCore: no API key — pass { apiKey } or set ${keyEnvVar}`);
|
|
283
|
+
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
284
|
+
this.apiKey = apiKey;
|
|
253
285
|
this.timeoutMs = opts.timeoutMs;
|
|
254
286
|
this._fetch = opts.fetch ?? fetch;
|
|
255
287
|
}
|
|
@@ -342,6 +374,11 @@ export interface StreamHandlers {
|
|
|
342
374
|
// Tenant client (data ops) — use a TENANT key
|
|
343
375
|
// ---------------------------------------------------------------------------
|
|
344
376
|
export class KnowledgeCoreClient extends HttpBase {
|
|
377
|
+
/** Defaults to env BABAV_KC_BASE_URL + BABAV_KC_TENANT_KEY when not passed. */
|
|
378
|
+
constructor(opts: ClientOptions = {}) {
|
|
379
|
+
super(opts, "BABAV_KC_TENANT_KEY");
|
|
380
|
+
}
|
|
381
|
+
|
|
345
382
|
// --- query (agent-anchored) ---
|
|
346
383
|
query(agentId: UUID, body: QueryRequest): Promise<QueryResponse> {
|
|
347
384
|
return this.request("POST", `/v1/agents/${agentId}/query`, { json: body });
|
|
@@ -392,6 +429,9 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
392
429
|
/** Count documents in the corpus: { total, indexed, in_flight, failed }. */
|
|
393
430
|
countDocuments: (id: UUID) =>
|
|
394
431
|
this.request<DocumentCount>("GET", `/v1/corpora/${id}/documents/count`),
|
|
432
|
+
/** Documents in the corpus whose filename contains `q` (case-insensitive), paginated. */
|
|
433
|
+
searchDocuments: (id: UUID, q: string, opts?: { limit?: number; cursor?: string }) =>
|
|
434
|
+
this.request<Page<Document>>("GET", `/v1/corpora/${id}/documents/search`, { query: { q, ...opts } }),
|
|
395
435
|
/** Upload a file and ingest it ASYNCHRONOUSLY — resolves with the created
|
|
396
436
|
* document at status `pending` (HTTP 202); track via documents.get() polling
|
|
397
437
|
* the status to `indexed`/`failed`, or the tenant result webhook. */
|
|
@@ -422,6 +462,9 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
422
462
|
/** Count documents in the folder: { total, indexed, in_flight, failed }. */
|
|
423
463
|
countDocuments: (folderId: UUID) =>
|
|
424
464
|
this.request<DocumentCount>("GET", `/v1/folders/${folderId}/documents/count`),
|
|
465
|
+
/** Documents in the folder whose filename contains `q` (case-insensitive), paginated. */
|
|
466
|
+
searchDocuments: (folderId: UUID, q: string, opts?: { limit?: number; cursor?: string }) =>
|
|
467
|
+
this.request<Page<Document>>("GET", `/v1/folders/${folderId}/documents/search`, { query: { q, ...opts } }),
|
|
425
468
|
};
|
|
426
469
|
|
|
427
470
|
// --- documents ---
|
|
@@ -495,6 +538,11 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
495
538
|
// Admin client (tenant + key + agent management) — use the ADMIN key
|
|
496
539
|
// ---------------------------------------------------------------------------
|
|
497
540
|
export class AdminClient extends HttpBase {
|
|
541
|
+
/** Defaults to env BABAV_KC_BASE_URL + BABAV_KC_ADMIN_KEY when not passed. */
|
|
542
|
+
constructor(opts: ClientOptions = {}) {
|
|
543
|
+
super(opts, "BABAV_KC_ADMIN_KEY");
|
|
544
|
+
}
|
|
545
|
+
|
|
498
546
|
tenants = {
|
|
499
547
|
create: (b: Partial<Tenant> & { name: string }) => this.request<Tenant>("POST", "/v1/tenants", { json: b }),
|
|
500
548
|
list: (q?: { limit?: number; cursor?: string }) => this.request<Page<Tenant>>("GET", "/v1/tenants", { query: q }),
|