korely-memory 0.1.9 → 0.1.11
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 +25 -4
- package/dist/ai-sdk.cjs +53 -3
- package/dist/ai-sdk.js +37 -3
- package/dist/chunk-6SD5RRK3.js +31 -0
- package/dist/index.cjs +410 -100
- package/dist/index.d.cts +265 -15
- package/dist/index.d.ts +265 -15
- package/dist/index.js +395 -100
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__require,
|
|
3
|
+
keysOf,
|
|
4
|
+
unknownOption
|
|
5
|
+
} from "./chunk-6SD5RRK3.js";
|
|
6
|
+
|
|
1
7
|
// src/errors.ts
|
|
2
8
|
var KorelyError = class extends Error {
|
|
3
9
|
constructor(message = "", opts = {}) {
|
|
@@ -12,6 +18,7 @@ var APIError = class extends KorelyError {
|
|
|
12
18
|
constructor(message = "", opts = {}) {
|
|
13
19
|
super(message, opts);
|
|
14
20
|
this.name = "APIError";
|
|
21
|
+
this.retryAfter = opts.retryAfter;
|
|
15
22
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
16
23
|
}
|
|
17
24
|
};
|
|
@@ -36,25 +43,78 @@ var NotFoundError = class _NotFoundError extends APIError {
|
|
|
36
43
|
Object.setPrototypeOf(this, _NotFoundError.prototype);
|
|
37
44
|
}
|
|
38
45
|
};
|
|
39
|
-
var
|
|
46
|
+
var ConflictError = class extends APIError {
|
|
47
|
+
constructor(message = "", opts = {}) {
|
|
48
|
+
super(message, opts);
|
|
49
|
+
this.name = "ConflictError";
|
|
50
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var StaleWriteError = class _StaleWriteError extends ConflictError {
|
|
40
54
|
constructor(message = "", opts = {}) {
|
|
41
55
|
super(message, opts);
|
|
42
56
|
this.name = "StaleWriteError";
|
|
43
57
|
Object.setPrototypeOf(this, _StaleWriteError.prototype);
|
|
44
58
|
}
|
|
45
59
|
};
|
|
46
|
-
var QuotaExceededError = class
|
|
60
|
+
var QuotaExceededError = class extends APIError {
|
|
47
61
|
constructor(message = "", opts = {}) {
|
|
48
62
|
super(message, opts);
|
|
49
63
|
this.name = "QuotaExceededError";
|
|
50
|
-
this
|
|
51
|
-
|
|
64
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var TooManyBatchesError = class _TooManyBatchesError extends QuotaExceededError {
|
|
68
|
+
constructor(message = "", opts = {}) {
|
|
69
|
+
super(message, opts);
|
|
70
|
+
this.name = "TooManyBatchesError";
|
|
71
|
+
Object.setPrototypeOf(this, _TooManyBatchesError.prototype);
|
|
52
72
|
}
|
|
53
73
|
};
|
|
54
74
|
|
|
55
75
|
// src/client.ts
|
|
56
|
-
var VERSION = "0.1.
|
|
76
|
+
var VERSION = "0.1.11";
|
|
57
77
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
78
|
+
var CLIENT_KEYS = keysOf()("apiKey", "region", "baseUrl", "timeoutMs", "fetch");
|
|
79
|
+
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
80
|
+
var ADD_KEYS = keysOf()("agent_id", "user_id", "run_id", "metadata", "timestamp");
|
|
81
|
+
var SEARCH_KEYS = keysOf()("user_id", "agent_id", "run_id", "metadata", "limit");
|
|
82
|
+
var LIST_KEYS = keysOf()("user_id", "agent_id", "run_id", "limit", "offset");
|
|
83
|
+
var UPDATE_KEYS = keysOf()("content", "expected_updated_at");
|
|
84
|
+
var USERS_KEYS = keysOf()("agent_id", "limit", "offset");
|
|
85
|
+
var AGENTS_KEYS = keysOf()("limit", "offset");
|
|
86
|
+
var EVENTS_KEYS = keysOf()("user_id", "status", "limit");
|
|
87
|
+
var AUDIT_KEYS = keysOf()("user_id", "action", "since", "until", "limit", "offset");
|
|
88
|
+
var ITER_AUDIT_KEYS = keysOf()("user_id", "action", "since", "until", "offset", "page_size");
|
|
89
|
+
var FACTS_KEYS = keysOf()(
|
|
90
|
+
"subject",
|
|
91
|
+
"entity",
|
|
92
|
+
"predicate",
|
|
93
|
+
"predicate_family",
|
|
94
|
+
"include_invalidated",
|
|
95
|
+
"as_of",
|
|
96
|
+
"user_id",
|
|
97
|
+
"agent_id",
|
|
98
|
+
"limit",
|
|
99
|
+
"offset"
|
|
100
|
+
);
|
|
101
|
+
var TRIPLE_KEYS = keysOf()(
|
|
102
|
+
"user_id",
|
|
103
|
+
"agent_id",
|
|
104
|
+
"run_id",
|
|
105
|
+
"subject_type",
|
|
106
|
+
"object_is_literal",
|
|
107
|
+
"confidence",
|
|
108
|
+
"valid_from",
|
|
109
|
+
"tense"
|
|
110
|
+
);
|
|
111
|
+
var PROFILE_KEYS = keysOf()("user_id", "agent_id", "as_of");
|
|
112
|
+
var CONTEXT_KEYS = keysOf()("query", "user_id", "agent_id", "token_budget");
|
|
113
|
+
var CONTEXT_EXTRA_KEYS = keysOf()("user_id", "agent_id", "token_budget");
|
|
114
|
+
function checkOptions(method, opts, known) {
|
|
115
|
+
const problem = unknownOption(method, opts, known);
|
|
116
|
+
if (problem) throw new KorelyError(problem);
|
|
117
|
+
}
|
|
58
118
|
function coerceContent(content) {
|
|
59
119
|
if (typeof content === "string") return content;
|
|
60
120
|
if (Array.isArray(content)) {
|
|
@@ -83,6 +143,30 @@ function seg(value, what) {
|
|
|
83
143
|
if (!s) throw new KorelyError(`${what} is empty.`);
|
|
84
144
|
return encodeURIComponent(s);
|
|
85
145
|
}
|
|
146
|
+
function moment(value, what) {
|
|
147
|
+
if (value == null) return void 0;
|
|
148
|
+
if (typeof value === "string") return value;
|
|
149
|
+
if (value instanceof Date) {
|
|
150
|
+
if (Number.isNaN(value.getTime())) throw new KorelyError(`${what} is an invalid Date.`);
|
|
151
|
+
return value.toISOString();
|
|
152
|
+
}
|
|
153
|
+
throw new KorelyError(`${what} must be an ISO 8601 string or a Date, not a ${typeof value}.`);
|
|
154
|
+
}
|
|
155
|
+
function auditFilters(opts) {
|
|
156
|
+
for (const name of ["user_id", "action"]) {
|
|
157
|
+
if (opts[name] === "") {
|
|
158
|
+
throw new KorelyError(
|
|
159
|
+
`${name} is empty: the server reads an empty filter as no filter. Leave it out to mean every ${name.split("_")[0]}.`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
user_id: opts.user_id,
|
|
165
|
+
action: opts.action,
|
|
166
|
+
since: moment(opts.since, "since"),
|
|
167
|
+
until: moment(opts.until, "until")
|
|
168
|
+
};
|
|
169
|
+
}
|
|
86
170
|
function detailLine(detail) {
|
|
87
171
|
if (typeof detail === "string") return detail.trim() ? detail : void 0;
|
|
88
172
|
if (Array.isArray(detail) && detail.length) {
|
|
@@ -99,13 +183,18 @@ function detailLine(detail) {
|
|
|
99
183
|
return void 0;
|
|
100
184
|
}
|
|
101
185
|
function errorFields(status, body) {
|
|
102
|
-
const
|
|
186
|
+
const text2 = (v) => typeof v === "string" && v.trim() ? v : void 0;
|
|
103
187
|
const b = body && typeof body === "object" ? body : {};
|
|
104
188
|
const pair = b.detail && typeof b.detail === "object" && !Array.isArray(b.detail) ? b.detail : {};
|
|
105
|
-
const code =
|
|
106
|
-
const message =
|
|
189
|
+
const code = text2(b.code) ?? text2(pair.code);
|
|
190
|
+
const message = text2(b.message) ?? text2(pair.message) ?? detailLine(b.detail) ?? code ?? `HTTP ${status}`;
|
|
107
191
|
return { code, message };
|
|
108
192
|
}
|
|
193
|
+
function retryAfterSeconds(raw) {
|
|
194
|
+
if (raw == null) return void 0;
|
|
195
|
+
const n = Math.ceil(Number(raw));
|
|
196
|
+
return Number.isFinite(n) && n >= 0 ? n : void 0;
|
|
197
|
+
}
|
|
109
198
|
function fillPair(obj, current, deprecated) {
|
|
110
199
|
if (obj[current] == null && obj[deprecated] != null) obj[current] = obj[deprecated];
|
|
111
200
|
if (obj[deprecated] == null && obj[current] != null) obj[deprecated] = obj[current];
|
|
@@ -141,23 +230,128 @@ function refuseAMismatchedPair(apiKey, baseUrl) {
|
|
|
141
230
|
);
|
|
142
231
|
}
|
|
143
232
|
}
|
|
233
|
+
async function exchange(t, method, path, opts = {}) {
|
|
234
|
+
let url = t.baseUrl + path;
|
|
235
|
+
if (opts.params) {
|
|
236
|
+
const qs = new URLSearchParams();
|
|
237
|
+
for (const [k, v] of Object.entries(opts.params)) {
|
|
238
|
+
if (v !== void 0 && v !== null) qs.append(k, String(v));
|
|
239
|
+
}
|
|
240
|
+
const s = qs.toString();
|
|
241
|
+
if (s) url += "?" + s;
|
|
242
|
+
}
|
|
243
|
+
const headers = {
|
|
244
|
+
Accept: "application/json",
|
|
245
|
+
"X-Korely-Client": `korely-js/${VERSION}`
|
|
246
|
+
};
|
|
247
|
+
if (t.apiKey) headers.Authorization = `Bearer ${t.apiKey}`;
|
|
248
|
+
let body;
|
|
249
|
+
if (opts.body !== void 0) {
|
|
250
|
+
body = JSON.stringify(opts.body);
|
|
251
|
+
headers["Content-Type"] = "application/json";
|
|
252
|
+
}
|
|
253
|
+
const controller = new AbortController();
|
|
254
|
+
let timedOut = false;
|
|
255
|
+
const timer = setTimeout(() => {
|
|
256
|
+
timedOut = true;
|
|
257
|
+
controller.abort();
|
|
258
|
+
}, t.timeoutMs);
|
|
259
|
+
const failed = (e) => new KorelyError(
|
|
260
|
+
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`
|
|
261
|
+
);
|
|
262
|
+
let resp;
|
|
263
|
+
let text2;
|
|
264
|
+
try {
|
|
265
|
+
try {
|
|
266
|
+
resp = await t.fetchImpl(url, {
|
|
267
|
+
method,
|
|
268
|
+
headers,
|
|
269
|
+
body,
|
|
270
|
+
signal: controller.signal
|
|
271
|
+
});
|
|
272
|
+
} catch (e) {
|
|
273
|
+
throw failed(e);
|
|
274
|
+
}
|
|
275
|
+
try {
|
|
276
|
+
text2 = await resp.text();
|
|
277
|
+
} catch (e) {
|
|
278
|
+
throw failed(e);
|
|
279
|
+
}
|
|
280
|
+
} finally {
|
|
281
|
+
clearTimeout(timer);
|
|
282
|
+
}
|
|
283
|
+
let parsed = {};
|
|
284
|
+
if (text2) {
|
|
285
|
+
try {
|
|
286
|
+
parsed = JSON.parse(text2);
|
|
287
|
+
} catch {
|
|
288
|
+
if (resp.ok) {
|
|
289
|
+
throw new KorelyError(
|
|
290
|
+
`The server answered ${resp.status} to ${method} ${path} with a body that is not JSON: ${JSON.stringify(text2.slice(0, 200))}`,
|
|
291
|
+
{ status: resp.status }
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
parsed = { message: text2 };
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (!resp.ok) {
|
|
298
|
+
raiseFor(resp.status, parsed, resp.headers.get("retry-after"));
|
|
299
|
+
}
|
|
300
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
301
|
+
}
|
|
302
|
+
function raiseFor(status, body, retryAfter) {
|
|
303
|
+
const { code, message: msg } = errorFields(status, body);
|
|
304
|
+
const opts = {
|
|
305
|
+
status,
|
|
306
|
+
code,
|
|
307
|
+
retryAfter: retryAfterSeconds(retryAfter ?? body?.retry_after ?? body?._retry_after)
|
|
308
|
+
};
|
|
309
|
+
if (status === 401) throw new AuthenticationError(msg, opts);
|
|
310
|
+
if (status === 403) throw new NamespaceForbiddenError(msg, opts);
|
|
311
|
+
if (status === 404) throw new NotFoundError(msg, opts);
|
|
312
|
+
if (status === 409) {
|
|
313
|
+
if (code === "stale_write") throw new StaleWriteError(msg, opts);
|
|
314
|
+
throw new ConflictError(msg, opts);
|
|
315
|
+
}
|
|
316
|
+
if (status === 429) {
|
|
317
|
+
if (code === "too_many_batches") throw new TooManyBatchesError(msg, opts);
|
|
318
|
+
throw new QuotaExceededError(msg, opts);
|
|
319
|
+
}
|
|
320
|
+
throw new APIError(msg, opts);
|
|
321
|
+
}
|
|
322
|
+
function savedConfig() {
|
|
323
|
+
try {
|
|
324
|
+
const proc = typeof process !== "undefined" ? process : void 0;
|
|
325
|
+
if (!proc?.versions?.node) return {};
|
|
326
|
+
const builtin = (id) => typeof proc.getBuiltinModule === "function" ? proc.getBuiltinModule(id) : typeof __require === "function" ? __require(id) : void 0;
|
|
327
|
+
const fs = builtin("node:fs");
|
|
328
|
+
const os = builtin("node:os");
|
|
329
|
+
const path = builtin("node:path");
|
|
330
|
+
if (!fs || !os || !path) return {};
|
|
331
|
+
const home = proc.env?.KORELY_CONFIG_HOME || path.join(os.homedir(), ".korely");
|
|
332
|
+
const data = JSON.parse(fs.readFileSync(path.join(home, "config.json"), "utf8"));
|
|
333
|
+
return data && typeof data === "object" ? data : {};
|
|
334
|
+
} catch {
|
|
335
|
+
return {};
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function text(v) {
|
|
339
|
+
return typeof v === "string" && v ? v : void 0;
|
|
340
|
+
}
|
|
144
341
|
var Korely = class {
|
|
145
342
|
constructor(opts = {}) {
|
|
146
|
-
|
|
147
|
-
throw new KorelyError(
|
|
148
|
-
"Unknown option base_url: the JavaScript SDK takes baseUrl (new Korely({ baseUrl: 'https://...' })) or the KORELY_BASE_URL env var."
|
|
149
|
-
);
|
|
150
|
-
}
|
|
343
|
+
checkOptions("Korely", opts, CLIENT_KEYS);
|
|
151
344
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
152
|
-
const
|
|
345
|
+
const envBase = typeof process !== "undefined" ? process.env?.KORELY_BASE_URL : void 0;
|
|
346
|
+
const saved = opts.apiKey && (opts.baseUrl || envBase) ? {} : savedConfig();
|
|
347
|
+
const key = opts.apiKey ?? envKey ?? text(saved.api_key);
|
|
153
348
|
if (!key) {
|
|
154
349
|
throw new KorelyError(
|
|
155
|
-
"No API key. Pass { apiKey: 'kor_live_...' } or
|
|
350
|
+
"No API key. Pass { apiKey: 'kor_live_...' }, set KORELY_API_KEY, or run `korely init`, which saves one to ~/.korely/config.json."
|
|
156
351
|
);
|
|
157
352
|
}
|
|
158
353
|
this.apiKey = key;
|
|
159
|
-
|
|
160
|
-
this.baseUrl = (opts.baseUrl ?? envBase ?? REGIONS[opts.region ?? "eu"] ?? REGIONS.eu).replace(/\/+$/, "");
|
|
354
|
+
this.baseUrl = (opts.baseUrl ?? envBase ?? text(saved.base_url) ?? REGIONS[opts.region ?? "eu"] ?? REGIONS.eu).replace(/\/+$/, "");
|
|
161
355
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
162
356
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
163
357
|
const f = opts.fetch ?? globalThis.fetch;
|
|
@@ -168,92 +362,166 @@ var Korely = class {
|
|
|
168
362
|
}
|
|
169
363
|
this.fetchImpl = f;
|
|
170
364
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
365
|
+
/**
|
|
366
|
+
* POST /v1/agents/init, with no key: sign up for a free hobby key.
|
|
367
|
+
*
|
|
368
|
+
* The one call that runs without a key, because it is how one is obtained,
|
|
369
|
+
* so it is static: `await Korely.initAgent("my-app")`, then
|
|
370
|
+
* `new Korely({ apiKey: result.api_key })`. `agentCaller` is a free-form
|
|
371
|
+
* label of who signed up, kept for your reference. The answer carries the
|
|
372
|
+
* key, shown once (save it), its `tier`, `region`, `scopes` and `quotas`.
|
|
373
|
+
* The account it creates has no login: the key is the account, and
|
|
374
|
+
* `deleteAccount({ confirm: true })` closes it.
|
|
375
|
+
*
|
|
376
|
+
* Cloud only. The server is `baseUrl`, else KORELY_BASE_URL, else the
|
|
377
|
+
* region's. Refusals: 403 `signup_disabled` (NamespaceForbiddenError) when
|
|
378
|
+
* self-signup is closed; 429 `signup_rate_limited` (QuotaExceededError, with
|
|
379
|
+
* `retryAfter`) past the new accounts a network may open in a day. The
|
|
380
|
+
* Self-hosted has no such route and answers 404, or 405 where it serves its
|
|
381
|
+
* dashboard: its keys come from its own dashboard.
|
|
382
|
+
*/
|
|
383
|
+
static async initAgent(agentCaller, opts = {}) {
|
|
384
|
+
checkOptions("initAgent", opts, INIT_KEYS);
|
|
385
|
+
const where = opts.processingRegion;
|
|
386
|
+
if (where !== void 0 && where !== "eu" && where !== "global") {
|
|
387
|
+
throw new KorelyError(`processingRegion is "eu" or "global", not ${JSON.stringify(where)}.`);
|
|
181
388
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
};
|
|
187
|
-
let body;
|
|
188
|
-
if (opts.body !== void 0) {
|
|
189
|
-
body = JSON.stringify(opts.body);
|
|
190
|
-
headers["Content-Type"] = "application/json";
|
|
389
|
+
if (agentCaller != null && typeof agentCaller !== "string") {
|
|
390
|
+
throw new KorelyError(
|
|
391
|
+
`agentCaller is a label, a string, not ${Array.isArray(agentCaller) ? "an array" : `a ${typeof agentCaller}`}: Korely.initAgent('my-app', { baseUrl }).`
|
|
392
|
+
);
|
|
191
393
|
}
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
timedOut ? `Request timed out after ${this.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`
|
|
200
|
-
);
|
|
201
|
-
let resp;
|
|
202
|
-
let text;
|
|
203
|
-
try {
|
|
204
|
-
try {
|
|
205
|
-
resp = await this.fetchImpl(url, {
|
|
206
|
-
method,
|
|
207
|
-
headers,
|
|
208
|
-
body,
|
|
209
|
-
signal: controller.signal
|
|
210
|
-
});
|
|
211
|
-
} catch (e) {
|
|
212
|
-
throw failed(e);
|
|
213
|
-
}
|
|
214
|
-
try {
|
|
215
|
-
text = await resp.text();
|
|
216
|
-
} catch (e) {
|
|
217
|
-
throw failed(e);
|
|
218
|
-
}
|
|
219
|
-
} finally {
|
|
220
|
-
clearTimeout(timer);
|
|
394
|
+
const envBase = typeof process !== "undefined" ? process.env?.KORELY_BASE_URL : void 0;
|
|
395
|
+
const baseUrl = (opts.baseUrl ?? envBase ?? REGIONS[opts.region ?? "eu"] ?? REGIONS.eu).replace(/\/+$/, "");
|
|
396
|
+
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
397
|
+
if (!fetchImpl) {
|
|
398
|
+
throw new KorelyError(
|
|
399
|
+
"No fetch available. On Node < 18 pass a fetch implementation via { fetch }."
|
|
400
|
+
);
|
|
221
401
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
throw new KorelyError(
|
|
229
|
-
`The server answered ${resp.status} to ${method} ${path} with a body that is not JSON: ${JSON.stringify(text.slice(0, 200))}`,
|
|
230
|
-
{ status: resp.status }
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
parsed = { message: text };
|
|
402
|
+
return exchange(
|
|
403
|
+
{ baseUrl, timeoutMs: opts.timeoutMs ?? 3e4, fetchImpl },
|
|
404
|
+
"POST",
|
|
405
|
+
"/v1/agents/init",
|
|
406
|
+
{
|
|
407
|
+
body: { agent_caller: agentCaller ?? void 0, processing_region: opts.processingRegion }
|
|
234
408
|
}
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
// ── transport ─────────────────────────────────────────────────────────────
|
|
412
|
+
request(method, path, opts = {}) {
|
|
413
|
+
return exchange(
|
|
414
|
+
{ baseUrl: this.baseUrl, timeoutMs: this.timeoutMs, fetchImpl: this.fetchImpl, apiKey: this.apiKey },
|
|
415
|
+
method,
|
|
416
|
+
path,
|
|
417
|
+
opts
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
// ── the key and its account ───────────────────────────────────────────────
|
|
421
|
+
/**
|
|
422
|
+
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
423
|
+
* it, with the same shape.
|
|
424
|
+
*
|
|
425
|
+
* The cheapest authenticated call there is: no scope, no rate limit, no
|
|
426
|
+
* quota, so it is how to check a key that was just minted or rotated without
|
|
427
|
+
* spending anything. `users()` was the usual stand-in, and it needs
|
|
428
|
+
* `memories:read` and counts as a query.
|
|
429
|
+
*/
|
|
430
|
+
async ping() {
|
|
431
|
+
return this.request("GET", "/v1/ping");
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* DELETE /v1/account?confirm=true: delete the account of this key for good,
|
|
435
|
+
* with every key, project, memory, fact and webhook of it; the key stops
|
|
436
|
+
* working. Cloud only, for an account made by `Korely.initAgent()` or
|
|
437
|
+
* `korely init --agent`, which nobody signs in to: the key is the account,
|
|
438
|
+
* and this is how it is closed (GDPR Art. 17).
|
|
439
|
+
*
|
|
440
|
+
* `{ confirm: true }` is required, and checked here, before anything is
|
|
441
|
+
* sent: without it, a KorelyError whose `code` is `confirmation_required`,
|
|
442
|
+
* the code the server gives the same refusal.
|
|
443
|
+
*
|
|
444
|
+
* An account with a Korely login rejects with 409 `account_has_login`
|
|
445
|
+
* (ConflictError): a key that ended up in a log must not be able to delete
|
|
446
|
+
* it, so it is closed from the app (Settings, Account). The Self-hosted has
|
|
447
|
+
* no such route and answers 404 (NotFoundError), or 405 where it serves its
|
|
448
|
+
* dashboard. No audit event survives: the trail is part of what goes.
|
|
449
|
+
*/
|
|
450
|
+
async deleteAccount(opts = {}) {
|
|
451
|
+
checkOptions("deleteAccount", opts, ["confirm"]);
|
|
452
|
+
if (opts?.confirm !== true) {
|
|
453
|
+
throw new KorelyError(
|
|
454
|
+
"deleteAccount() deletes this key's account, every key, memory and fact of it, for good. Pass { confirm: true } to mean it.",
|
|
455
|
+
{ code: "confirmation_required" }
|
|
456
|
+
);
|
|
235
457
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
458
|
+
return this.request("DELETE", "/v1/account", { params: { confirm: "true" } });
|
|
459
|
+
}
|
|
460
|
+
// ── audit ─────────────────────────────────────────────────────────────────
|
|
461
|
+
/**
|
|
462
|
+
* GET /v1/audit: what this key's project did, and what its agents read,
|
|
463
|
+
* newest first (`ts` descending, then the event's id, so paging with
|
|
464
|
+
* `offset` is stable). Both products; the key needs `memories:read`.
|
|
465
|
+
*
|
|
466
|
+
* `user_id` keeps the events that touched one end user, the shape of an
|
|
467
|
+
* access or erasure request; `action` one kind ("read", "write", "erase"...;
|
|
468
|
+
* an unknown one answers an empty page); `since` and `until` bound `ts`,
|
|
469
|
+
* both inclusive, as ISO 8601 text or a Date. `limit` is 1 to 1000. To read
|
|
470
|
+
* everything, use `iterAudit()`. The page is iterable.
|
|
471
|
+
*
|
|
472
|
+
* Reading the trail counts against no quota and is not itself written to
|
|
473
|
+
* it; it does count against the rate limit. Scoped to the key's account and
|
|
474
|
+
* project, like every read.
|
|
475
|
+
*/
|
|
476
|
+
async audit(opts = {}) {
|
|
477
|
+
checkOptions("audit", opts, AUDIT_KEYS);
|
|
478
|
+
const page = await this.request(
|
|
479
|
+
"GET",
|
|
480
|
+
"/v1/audit",
|
|
481
|
+
{
|
|
482
|
+
params: { ...auditFilters(opts), limit: opts.limit ?? 100, offset: opts.offset ?? 0 }
|
|
483
|
+
}
|
|
484
|
+
);
|
|
485
|
+
return iterableOver(page, "events");
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Every event `audit()` would page through, for an export:
|
|
489
|
+
* `for await (const e of korely.iterAudit({ user_id }))`. The same filters,
|
|
490
|
+
* `page_size` events per request (1000, the most the API gives),
|
|
491
|
+
* `offset += page.length` until `total`.
|
|
492
|
+
*
|
|
493
|
+
* Without `until` the walk pins it to the newest event of its first page.
|
|
494
|
+
* The trail grows while it is read, newest first, and every new event pushed
|
|
495
|
+
* the rest one place down: the next page began with events already returned.
|
|
496
|
+
* The pin makes the export the trail as it stood when it started.
|
|
497
|
+
*
|
|
498
|
+
* A rate limit rejects mid-way, as any call does, and the SDK does not
|
|
499
|
+
* retry. To resume, call again with the same `until` (pin it yourself:
|
|
500
|
+
* `until: new Date()`) and `offset` the number of events already read.
|
|
501
|
+
*/
|
|
502
|
+
async *iterAudit(opts = {}) {
|
|
503
|
+
checkOptions("iterAudit", opts, ITER_AUDIT_KEYS);
|
|
504
|
+
const filters = auditFilters(opts);
|
|
505
|
+
let offset = opts.offset ?? 0;
|
|
506
|
+
let pin = filters.until === void 0 && offset === 0;
|
|
507
|
+
for (; ; ) {
|
|
508
|
+
const page = await this.audit({
|
|
509
|
+
user_id: opts.user_id,
|
|
510
|
+
action: opts.action,
|
|
511
|
+
since: filters.since,
|
|
512
|
+
until: filters.until,
|
|
513
|
+
limit: opts.page_size ?? 1e3,
|
|
514
|
+
offset
|
|
515
|
+
});
|
|
516
|
+
if (!page.events.length) return;
|
|
517
|
+
yield* page.events;
|
|
518
|
+
if (pin && page.events[0].ts) {
|
|
519
|
+
filters.until = page.events[0].ts;
|
|
520
|
+
pin = false;
|
|
253
521
|
}
|
|
254
|
-
|
|
522
|
+
offset += page.events.length;
|
|
523
|
+
if (offset >= page.total) return;
|
|
255
524
|
}
|
|
256
|
-
throw new APIError(msg, { status, code });
|
|
257
525
|
}
|
|
258
526
|
// ── memories ────────────────────────────────────────────────────────────
|
|
259
527
|
/**
|
|
@@ -263,15 +531,16 @@ var Korely = class {
|
|
|
263
531
|
* backfill: facts extracted inherit it as `valid_from`.
|
|
264
532
|
*/
|
|
265
533
|
async add(content, opts = {}) {
|
|
266
|
-
|
|
267
|
-
|
|
534
|
+
checkOptions("add", opts, ADD_KEYS);
|
|
535
|
+
const text2 = coerceContent(content);
|
|
536
|
+
if (!text2.trim()) {
|
|
268
537
|
throw new KorelyError(
|
|
269
538
|
"content is empty: pass a non-blank string or messages with content."
|
|
270
539
|
);
|
|
271
540
|
}
|
|
272
541
|
return this.request("POST", "/v1/memories", {
|
|
273
542
|
body: {
|
|
274
|
-
content:
|
|
543
|
+
content: text2,
|
|
275
544
|
agent_id: opts.agent_id,
|
|
276
545
|
user_id: opts.user_id,
|
|
277
546
|
run_id: opts.run_id,
|
|
@@ -287,6 +556,7 @@ var Korely = class {
|
|
|
287
556
|
* the server default (15), max 50.
|
|
288
557
|
*/
|
|
289
558
|
async search(query, opts = {}) {
|
|
559
|
+
checkOptions("search", opts, SEARCH_KEYS);
|
|
290
560
|
const body = await this.request("POST", "/v1/memories/search", {
|
|
291
561
|
body: {
|
|
292
562
|
query,
|
|
@@ -305,6 +575,7 @@ var Korely = class {
|
|
|
305
575
|
* `total` and `offset` walk the rest.
|
|
306
576
|
*/
|
|
307
577
|
async getAll(opts = {}) {
|
|
578
|
+
checkOptions("getAll", opts, LIST_KEYS);
|
|
308
579
|
const page = await this.request(
|
|
309
580
|
"GET",
|
|
310
581
|
"/v1/memories",
|
|
@@ -329,6 +600,7 @@ var Korely = class {
|
|
|
329
600
|
* for optimistic concurrency (throws StaleWriteError instead of clobbering).
|
|
330
601
|
*/
|
|
331
602
|
async update(memoryId, opts) {
|
|
603
|
+
checkOptions("update", opts, UPDATE_KEYS);
|
|
332
604
|
return this.request("PATCH", `/v1/memories/${seg(memoryId, "memoryId")}`, {
|
|
333
605
|
body: {
|
|
334
606
|
content: opts.content,
|
|
@@ -354,6 +626,7 @@ var Korely = class {
|
|
|
354
626
|
* the same numbers; whichever pair the server sends fills both.
|
|
355
627
|
*/
|
|
356
628
|
async deleteAll(opts) {
|
|
629
|
+
checkOptions("deleteAll", opts, ["user_id"]);
|
|
357
630
|
const r = await this.request(
|
|
358
631
|
"DELETE",
|
|
359
632
|
`/v1/users/${seg(opts.user_id, "user_id")}/memories`
|
|
@@ -375,6 +648,7 @@ var Korely = class {
|
|
|
375
648
|
* namespaces), each with active memory + fact counts and last-active time.
|
|
376
649
|
*/
|
|
377
650
|
async users(opts = {}) {
|
|
651
|
+
checkOptions("users", opts, USERS_KEYS);
|
|
378
652
|
const page = await this.request(
|
|
379
653
|
"GET",
|
|
380
654
|
"/v1/users",
|
|
@@ -403,6 +677,7 @@ var Korely = class {
|
|
|
403
677
|
* sets no cap and answers `cap: 0`.
|
|
404
678
|
*/
|
|
405
679
|
async listAgents(opts = {}) {
|
|
680
|
+
checkOptions("listAgents", opts, AGENTS_KEYS);
|
|
406
681
|
const page = await this.request("GET", "/v1/agents", {
|
|
407
682
|
params: {
|
|
408
683
|
limit: opts.limit ?? 50,
|
|
@@ -434,6 +709,7 @@ var Korely = class {
|
|
|
434
709
|
* array that also carries `total`, the matches across all pages.
|
|
435
710
|
*/
|
|
436
711
|
async getFacts(opts = {}) {
|
|
712
|
+
checkOptions("getFacts", opts, FACTS_KEYS);
|
|
437
713
|
const params = {
|
|
438
714
|
subject: opts.subject,
|
|
439
715
|
entity: opts.entity,
|
|
@@ -462,7 +738,8 @@ var Korely = class {
|
|
|
462
738
|
* listing any fact ids it superseded.
|
|
463
739
|
*/
|
|
464
740
|
async addFactTriple(subject, predicate, object, opts = {}) {
|
|
465
|
-
|
|
741
|
+
checkOptions("addFactTriple", opts, TRIPLE_KEYS);
|
|
742
|
+
const fact = await this.request("POST", "/v1/facts", {
|
|
466
743
|
body: {
|
|
467
744
|
subject,
|
|
468
745
|
predicate,
|
|
@@ -477,6 +754,11 @@ var Korely = class {
|
|
|
477
754
|
tense: opts.tense
|
|
478
755
|
}
|
|
479
756
|
});
|
|
757
|
+
return {
|
|
758
|
+
...fact,
|
|
759
|
+
user_id: fact.user_id !== void 0 ? fact.user_id : opts.user_id,
|
|
760
|
+
agent_id: fact.agent_id !== void 0 ? fact.agent_id : opts.agent_id
|
|
761
|
+
};
|
|
480
762
|
}
|
|
481
763
|
/**
|
|
482
764
|
* POST /v1/facts/:id/forget: close a fact; it stops being current and stays
|
|
@@ -493,6 +775,7 @@ var Korely = class {
|
|
|
493
775
|
* knows a fact is finished says so, and nothing has to infer it.
|
|
494
776
|
*/
|
|
495
777
|
async forgetFact(factId, opts = {}) {
|
|
778
|
+
checkOptions("forgetFact", opts, ["at"]);
|
|
496
779
|
return this.request("POST", `/v1/facts/${seg(factId, "factId")}/forget`, {
|
|
497
780
|
body: { at: opts.at }
|
|
498
781
|
});
|
|
@@ -522,6 +805,7 @@ var Korely = class {
|
|
|
522
805
|
* Pass `as_of` (ISO date) for the point-in-time profile.
|
|
523
806
|
*/
|
|
524
807
|
async getProfile(opts) {
|
|
808
|
+
checkOptions("getProfile", opts, PROFILE_KEYS);
|
|
525
809
|
return this.request("GET", "/v1/profile", {
|
|
526
810
|
params: {
|
|
527
811
|
user_id: opts.user_id,
|
|
@@ -536,6 +820,8 @@ var Korely = class {
|
|
|
536
820
|
* (profile + relevant facts + memories) within a token budget.
|
|
537
821
|
*/
|
|
538
822
|
async getContext(opts, extra = {}) {
|
|
823
|
+
if (typeof opts === "string") checkOptions("getContext", extra, CONTEXT_EXTRA_KEYS);
|
|
824
|
+
else checkOptions("getContext", opts, CONTEXT_KEYS);
|
|
539
825
|
const o = typeof opts === "string" ? { ...extra, query: opts } : opts;
|
|
540
826
|
if (!o || typeof o.query !== "string" || !o.query.trim()) {
|
|
541
827
|
throw new KorelyError("getContext needs a query.");
|
|
@@ -563,6 +849,7 @@ var Korely = class {
|
|
|
563
849
|
* this is how to know.
|
|
564
850
|
*/
|
|
565
851
|
async events(opts = {}) {
|
|
852
|
+
checkOptions("events", opts, EVENTS_KEYS);
|
|
566
853
|
return this.request("GET", "/v1/events", {
|
|
567
854
|
params: {
|
|
568
855
|
user_id: opts.user_id,
|
|
@@ -586,6 +873,12 @@ var Korely = class {
|
|
|
586
873
|
* `code === "invalid_request"`) whose message names the item, e.g.
|
|
587
874
|
* `memories[3].timestamp`. Any key not listed above is refused the same
|
|
588
875
|
* way. Servers older than 2026-09-28 refuse `timestamp` itself.
|
|
876
|
+
*
|
|
877
|
+
* On the Cloud a batch is also refused, before anything is stored, with a
|
|
878
|
+
* 429: `quota_exceeded` (QuotaExceededError) when its memories do not fit in
|
|
879
|
+
* what is left of the month, batches already queued included, and
|
|
880
|
+
* `too_many_batches` (TooManyBatchesError) while three batches are still
|
|
881
|
+
* being imported. The Self-hosted meters nothing and refuses neither.
|
|
589
882
|
*/
|
|
590
883
|
async batch(memories) {
|
|
591
884
|
return this.request("POST", "/v1/batch", { body: { memories } });
|
|
@@ -598,11 +891,13 @@ var Korely = class {
|
|
|
598
891
|
export {
|
|
599
892
|
APIError,
|
|
600
893
|
AuthenticationError,
|
|
894
|
+
ConflictError,
|
|
601
895
|
Korely,
|
|
602
896
|
KorelyError,
|
|
603
897
|
NamespaceForbiddenError,
|
|
604
898
|
NotFoundError,
|
|
605
899
|
QuotaExceededError,
|
|
606
900
|
StaleWriteError,
|
|
901
|
+
TooManyBatchesError,
|
|
607
902
|
VERSION
|
|
608
903
|
};
|