@babav/knowledge-core-client 0.4.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 +19 -5
- package/dist/index.js +42 -3
- package/package.json +1 -1
- package/src/index.ts +49 -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>;
|
|
@@ -396,6 +408,8 @@ export declare class KnowledgeCoreClient extends HttpBase {
|
|
|
396
408
|
};
|
|
397
409
|
}
|
|
398
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);
|
|
399
413
|
tenants: {
|
|
400
414
|
create: (b: Partial<Tenant> & {
|
|
401
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 });
|
|
@@ -257,6 +292,10 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
257
292
|
// Admin client (tenant + key + agent management) — use the ADMIN key
|
|
258
293
|
// ---------------------------------------------------------------------------
|
|
259
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
|
+
}
|
|
260
299
|
tenants = {
|
|
261
300
|
create: (b) => this.request("POST", "/v1/tenants", { json: b }),
|
|
262
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 });
|
|
@@ -501,6 +538,11 @@ export class KnowledgeCoreClient extends HttpBase {
|
|
|
501
538
|
// Admin client (tenant + key + agent management) — use the ADMIN key
|
|
502
539
|
// ---------------------------------------------------------------------------
|
|
503
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
|
+
|
|
504
546
|
tenants = {
|
|
505
547
|
create: (b: Partial<Tenant> & { name: string }) => this.request<Tenant>("POST", "/v1/tenants", { json: b }),
|
|
506
548
|
list: (q?: { limit?: number; cursor?: string }) => this.request<Page<Tenant>>("GET", "/v1/tenants", { query: q }),
|