korely-memory 0.1.8 → 0.1.10
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 +14 -3
- package/dist/index.cjs +302 -89
- package/dist/index.d.cts +242 -14
- package/dist/index.d.ts +242 -14
- package/dist/index.js +300 -89
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -114,6 +114,11 @@ Every method maps to one REST endpoint.
|
|
|
114
114
|
| `events(opts?)` | `GET /v1/events` | Which writes are still being extracted; `limit` up to 200. |
|
|
115
115
|
| `batch(memories)` | `POST /v1/batch` | Bulk import, for migrations. Each item takes `timestamp`, as `add()` does. |
|
|
116
116
|
| `batchStatus(jobId)` | `GET /v1/batch/:id` | Poll an import job. |
|
|
117
|
+
| `ping()` | `GET /v1/ping` | Check a key without spending anything (no scope, rate limit or quota): its `tier`, `region` and `scopes`. Both products. |
|
|
118
|
+
| `Korely.initAgent(agentCaller?, opts?)` | `POST /v1/agents/init` | Cloud only. Static, no key needed: sign up for a free hobby key (shown once, save it), then `new Korely({ apiKey: result.api_key })`. `opts` takes `baseUrl`, `region`, `timeoutMs`, `fetch`. |
|
|
119
|
+
| `deleteAccount({ confirm: true })` | `DELETE /v1/account` | Cloud only. Delete the key's account for good, every key, memory and fact; for an account `korely init --agent` made, which nobody signs in to. One with a login rejects with `ConflictError` (`account_has_login`). Without `confirm: true` nothing is sent. The Self-hosted answers 404 (405 where it serves its dashboard). |
|
|
120
|
+
| `audit(opts?)` | `GET /v1/audit` | The trail of the key's project, newest first: `user_id`, `action`, `since` / `until` (ISO text or `Date`), `limit` up to 1000. Ids, never content. Both products; needs `memories:read`, costs no quota. |
|
|
121
|
+
| `iterAudit(opts?)` | `GET /v1/audit`, every page | `for await (const e of korely.iterAudit({ user_id }))`: an export, `page_size` events per request, `until` pinned when it starts. |
|
|
117
122
|
|
|
118
123
|
## Bi-temporal facts (the moat)
|
|
119
124
|
|
|
@@ -157,10 +162,16 @@ try {
|
|
|
157
162
|
```
|
|
158
163
|
|
|
159
164
|
`AuthenticationError` (401) · `NamespaceForbiddenError` (403, e.g.
|
|
160
|
-
`agent_cap_exceeded`) · `NotFoundError` (404) · `
|
|
165
|
+
`agent_cap_exceeded`) · `NotFoundError` (404) · `ConflictError` (409, e.g.
|
|
166
|
+
`account_has_login`; `stale_write` is its subclass `StaleWriteError`) ·
|
|
161
167
|
`QuotaExceededError` (429: `retryAfter` is set for `rate_limit_exceeded`,
|
|
162
|
-
undefined for the monthly `quota_exceeded`) · `
|
|
163
|
-
|
|
168
|
+
undefined for the monthly `quota_exceeded`) · `TooManyBatchesError` (a
|
|
169
|
+
`QuotaExceededError`: `too_many_batches` from `batch()`, Cloud only, three
|
|
170
|
+
imports still running; send again when one finishes) · `APIError` (the base
|
|
171
|
+
of all of these, and everything else: 422, 503). Every `APIError` has
|
|
172
|
+
`retryAfter` when the server sent `Retry-After`: a rate limit, and the Cloud's
|
|
173
|
+
503 `writes_paused` (writes that need a model are paused until 00:00 UTC). The
|
|
174
|
+
SDK does not retry on its own.
|
|
164
175
|
|
|
165
176
|
## Configuration
|
|
166
177
|
|
package/dist/index.cjs
CHANGED
|
@@ -22,12 +22,14 @@ var src_exports = {};
|
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
APIError: () => APIError,
|
|
24
24
|
AuthenticationError: () => AuthenticationError,
|
|
25
|
+
ConflictError: () => ConflictError,
|
|
25
26
|
Korely: () => Korely,
|
|
26
27
|
KorelyError: () => KorelyError,
|
|
27
28
|
NamespaceForbiddenError: () => NamespaceForbiddenError,
|
|
28
29
|
NotFoundError: () => NotFoundError,
|
|
29
30
|
QuotaExceededError: () => QuotaExceededError,
|
|
30
31
|
StaleWriteError: () => StaleWriteError,
|
|
32
|
+
TooManyBatchesError: () => TooManyBatchesError,
|
|
31
33
|
VERSION: () => VERSION
|
|
32
34
|
});
|
|
33
35
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -46,6 +48,7 @@ var APIError = class extends KorelyError {
|
|
|
46
48
|
constructor(message = "", opts = {}) {
|
|
47
49
|
super(message, opts);
|
|
48
50
|
this.name = "APIError";
|
|
51
|
+
this.retryAfter = opts.retryAfter;
|
|
49
52
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
50
53
|
}
|
|
51
54
|
};
|
|
@@ -70,24 +73,37 @@ var NotFoundError = class _NotFoundError extends APIError {
|
|
|
70
73
|
Object.setPrototypeOf(this, _NotFoundError.prototype);
|
|
71
74
|
}
|
|
72
75
|
};
|
|
73
|
-
var
|
|
76
|
+
var ConflictError = class extends APIError {
|
|
77
|
+
constructor(message = "", opts = {}) {
|
|
78
|
+
super(message, opts);
|
|
79
|
+
this.name = "ConflictError";
|
|
80
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var StaleWriteError = class _StaleWriteError extends ConflictError {
|
|
74
84
|
constructor(message = "", opts = {}) {
|
|
75
85
|
super(message, opts);
|
|
76
86
|
this.name = "StaleWriteError";
|
|
77
87
|
Object.setPrototypeOf(this, _StaleWriteError.prototype);
|
|
78
88
|
}
|
|
79
89
|
};
|
|
80
|
-
var QuotaExceededError = class
|
|
90
|
+
var QuotaExceededError = class extends APIError {
|
|
81
91
|
constructor(message = "", opts = {}) {
|
|
82
92
|
super(message, opts);
|
|
83
93
|
this.name = "QuotaExceededError";
|
|
84
|
-
this
|
|
85
|
-
|
|
94
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var TooManyBatchesError = class _TooManyBatchesError extends QuotaExceededError {
|
|
98
|
+
constructor(message = "", opts = {}) {
|
|
99
|
+
super(message, opts);
|
|
100
|
+
this.name = "TooManyBatchesError";
|
|
101
|
+
Object.setPrototypeOf(this, _TooManyBatchesError.prototype);
|
|
86
102
|
}
|
|
87
103
|
};
|
|
88
104
|
|
|
89
105
|
// src/client.ts
|
|
90
|
-
var VERSION = "0.1.
|
|
106
|
+
var VERSION = "0.1.10";
|
|
91
107
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
92
108
|
function coerceContent(content) {
|
|
93
109
|
if (typeof content === "string") return content;
|
|
@@ -110,10 +126,37 @@ function coerceContent(content) {
|
|
|
110
126
|
return String(content);
|
|
111
127
|
}
|
|
112
128
|
function seg(value, what) {
|
|
129
|
+
if (value != null && typeof value !== "string" && !(typeof value === "number" && Number.isInteger(value))) {
|
|
130
|
+
throw new KorelyError(`${what} must be a string id, not ${Array.isArray(value) ? "an array" : `a ${typeof value}`}.`);
|
|
131
|
+
}
|
|
113
132
|
const s = value == null ? "" : String(value);
|
|
114
133
|
if (!s) throw new KorelyError(`${what} is empty.`);
|
|
115
134
|
return encodeURIComponent(s);
|
|
116
135
|
}
|
|
136
|
+
function moment(value, what) {
|
|
137
|
+
if (value == null) return void 0;
|
|
138
|
+
if (typeof value === "string") return value;
|
|
139
|
+
if (value instanceof Date) {
|
|
140
|
+
if (Number.isNaN(value.getTime())) throw new KorelyError(`${what} is an invalid Date.`);
|
|
141
|
+
return value.toISOString();
|
|
142
|
+
}
|
|
143
|
+
throw new KorelyError(`${what} must be an ISO 8601 string or a Date, not a ${typeof value}.`);
|
|
144
|
+
}
|
|
145
|
+
function auditFilters(opts) {
|
|
146
|
+
for (const name of ["user_id", "action"]) {
|
|
147
|
+
if (opts[name] === "") {
|
|
148
|
+
throw new KorelyError(
|
|
149
|
+
`${name} is empty: the server reads an empty filter as no filter. Leave it out to mean every ${name.split("_")[0]}.`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
user_id: opts.user_id,
|
|
155
|
+
action: opts.action,
|
|
156
|
+
since: moment(opts.since, "since"),
|
|
157
|
+
until: moment(opts.until, "until")
|
|
158
|
+
};
|
|
159
|
+
}
|
|
117
160
|
function detailLine(detail) {
|
|
118
161
|
if (typeof detail === "string") return detail.trim() ? detail : void 0;
|
|
119
162
|
if (Array.isArray(detail) && detail.length) {
|
|
@@ -137,6 +180,11 @@ function errorFields(status, body) {
|
|
|
137
180
|
const message = text(b.message) ?? text(pair.message) ?? detailLine(b.detail) ?? code ?? `HTTP ${status}`;
|
|
138
181
|
return { code, message };
|
|
139
182
|
}
|
|
183
|
+
function retryAfterSeconds(raw) {
|
|
184
|
+
if (raw == null) return void 0;
|
|
185
|
+
const n = Math.ceil(Number(raw));
|
|
186
|
+
return Number.isFinite(n) && n >= 0 ? n : void 0;
|
|
187
|
+
}
|
|
140
188
|
function fillPair(obj, current, deprecated) {
|
|
141
189
|
if (obj[current] == null && obj[deprecated] != null) obj[current] = obj[deprecated];
|
|
142
190
|
if (obj[deprecated] == null && obj[current] != null) obj[deprecated] = obj[current];
|
|
@@ -172,13 +220,105 @@ function refuseAMismatchedPair(apiKey, baseUrl) {
|
|
|
172
220
|
);
|
|
173
221
|
}
|
|
174
222
|
}
|
|
223
|
+
async function exchange(t, method, path, opts = {}) {
|
|
224
|
+
let url = t.baseUrl + path;
|
|
225
|
+
if (opts.params) {
|
|
226
|
+
const qs = new URLSearchParams();
|
|
227
|
+
for (const [k, v] of Object.entries(opts.params)) {
|
|
228
|
+
if (v !== void 0 && v !== null) qs.append(k, String(v));
|
|
229
|
+
}
|
|
230
|
+
const s = qs.toString();
|
|
231
|
+
if (s) url += "?" + s;
|
|
232
|
+
}
|
|
233
|
+
const headers = {
|
|
234
|
+
Accept: "application/json",
|
|
235
|
+
"X-Korely-Client": `korely-js/${VERSION}`
|
|
236
|
+
};
|
|
237
|
+
if (t.apiKey) headers.Authorization = `Bearer ${t.apiKey}`;
|
|
238
|
+
let body;
|
|
239
|
+
if (opts.body !== void 0) {
|
|
240
|
+
body = JSON.stringify(opts.body);
|
|
241
|
+
headers["Content-Type"] = "application/json";
|
|
242
|
+
}
|
|
243
|
+
const controller = new AbortController();
|
|
244
|
+
let timedOut = false;
|
|
245
|
+
const timer = setTimeout(() => {
|
|
246
|
+
timedOut = true;
|
|
247
|
+
controller.abort();
|
|
248
|
+
}, t.timeoutMs);
|
|
249
|
+
const failed = (e) => new KorelyError(
|
|
250
|
+
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`
|
|
251
|
+
);
|
|
252
|
+
let resp;
|
|
253
|
+
let text;
|
|
254
|
+
try {
|
|
255
|
+
try {
|
|
256
|
+
resp = await t.fetchImpl(url, {
|
|
257
|
+
method,
|
|
258
|
+
headers,
|
|
259
|
+
body,
|
|
260
|
+
signal: controller.signal
|
|
261
|
+
});
|
|
262
|
+
} catch (e) {
|
|
263
|
+
throw failed(e);
|
|
264
|
+
}
|
|
265
|
+
try {
|
|
266
|
+
text = await resp.text();
|
|
267
|
+
} catch (e) {
|
|
268
|
+
throw failed(e);
|
|
269
|
+
}
|
|
270
|
+
} finally {
|
|
271
|
+
clearTimeout(timer);
|
|
272
|
+
}
|
|
273
|
+
let parsed = {};
|
|
274
|
+
if (text) {
|
|
275
|
+
try {
|
|
276
|
+
parsed = JSON.parse(text);
|
|
277
|
+
} catch {
|
|
278
|
+
if (resp.ok) {
|
|
279
|
+
throw new KorelyError(
|
|
280
|
+
`The server answered ${resp.status} to ${method} ${path} with a body that is not JSON: ${JSON.stringify(text.slice(0, 200))}`,
|
|
281
|
+
{ status: resp.status }
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
parsed = { message: text };
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (!resp.ok) {
|
|
288
|
+
raiseFor(resp.status, parsed, resp.headers.get("retry-after"));
|
|
289
|
+
}
|
|
290
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
291
|
+
}
|
|
292
|
+
function raiseFor(status, body, retryAfter) {
|
|
293
|
+
const { code, message: msg } = errorFields(status, body);
|
|
294
|
+
const opts = {
|
|
295
|
+
status,
|
|
296
|
+
code,
|
|
297
|
+
retryAfter: retryAfterSeconds(retryAfter ?? body?.retry_after ?? body?._retry_after)
|
|
298
|
+
};
|
|
299
|
+
if (status === 401) throw new AuthenticationError(msg, opts);
|
|
300
|
+
if (status === 403) throw new NamespaceForbiddenError(msg, opts);
|
|
301
|
+
if (status === 404) throw new NotFoundError(msg, opts);
|
|
302
|
+
if (status === 409) {
|
|
303
|
+
if (code === "stale_write") throw new StaleWriteError(msg, opts);
|
|
304
|
+
throw new ConflictError(msg, opts);
|
|
305
|
+
}
|
|
306
|
+
if (status === 429) {
|
|
307
|
+
if (code === "too_many_batches") throw new TooManyBatchesError(msg, opts);
|
|
308
|
+
throw new QuotaExceededError(msg, opts);
|
|
309
|
+
}
|
|
310
|
+
throw new APIError(msg, opts);
|
|
311
|
+
}
|
|
312
|
+
function refusePythonSpelling(opts) {
|
|
313
|
+
if (opts && Object.prototype.hasOwnProperty.call(opts, "base_url")) {
|
|
314
|
+
throw new KorelyError(
|
|
315
|
+
"Unknown option base_url: the JavaScript SDK takes baseUrl (new Korely({ baseUrl: 'https://...' })) or the KORELY_BASE_URL env var."
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
175
319
|
var Korely = class {
|
|
176
320
|
constructor(opts = {}) {
|
|
177
|
-
|
|
178
|
-
throw new KorelyError(
|
|
179
|
-
"Unknown option base_url: the JavaScript SDK takes baseUrl (new Korely({ baseUrl: 'https://...' })) or the KORELY_BASE_URL env var."
|
|
180
|
-
);
|
|
181
|
-
}
|
|
321
|
+
refusePythonSpelling(opts);
|
|
182
322
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
183
323
|
const key = opts.apiKey ?? envKey;
|
|
184
324
|
if (!key) {
|
|
@@ -199,92 +339,157 @@ var Korely = class {
|
|
|
199
339
|
}
|
|
200
340
|
this.fetchImpl = f;
|
|
201
341
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
342
|
+
/**
|
|
343
|
+
* POST /v1/agents/init, with no key: sign up for a free hobby key.
|
|
344
|
+
*
|
|
345
|
+
* The one call that runs without a key, because it is how one is obtained,
|
|
346
|
+
* so it is static: `await Korely.initAgent("my-app")`, then
|
|
347
|
+
* `new Korely({ apiKey: result.api_key })`. `agentCaller` is a free-form
|
|
348
|
+
* label of who signed up, kept for your reference. The answer carries the
|
|
349
|
+
* key, shown once (save it), its `tier`, `region`, `scopes` and `quotas`.
|
|
350
|
+
* The account it creates has no login: the key is the account, and
|
|
351
|
+
* `deleteAccount({ confirm: true })` closes it.
|
|
352
|
+
*
|
|
353
|
+
* Cloud only. The server is `baseUrl`, else KORELY_BASE_URL, else the
|
|
354
|
+
* region's. Refusals: 403 `signup_disabled` (NamespaceForbiddenError) when
|
|
355
|
+
* self-signup is closed; 429 `signup_rate_limited` (QuotaExceededError, with
|
|
356
|
+
* `retryAfter`) past the new accounts a network may open in a day. The
|
|
357
|
+
* Self-hosted has no such route and answers 404, or 405 where it serves its
|
|
358
|
+
* dashboard: its keys come from its own dashboard.
|
|
359
|
+
*/
|
|
360
|
+
static async initAgent(agentCaller, opts = {}) {
|
|
361
|
+
refusePythonSpelling(opts);
|
|
362
|
+
if (agentCaller != null && typeof agentCaller !== "string") {
|
|
363
|
+
throw new KorelyError(
|
|
364
|
+
`agentCaller is a label, a string, not ${Array.isArray(agentCaller) ? "an array" : `a ${typeof agentCaller}`}: Korely.initAgent('my-app', { baseUrl }).`
|
|
365
|
+
);
|
|
212
366
|
}
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
body = JSON.stringify(opts.body);
|
|
221
|
-
headers["Content-Type"] = "application/json";
|
|
367
|
+
const envBase = typeof process !== "undefined" ? process.env?.KORELY_BASE_URL : void 0;
|
|
368
|
+
const baseUrl = (opts.baseUrl ?? envBase ?? REGIONS[opts.region ?? "eu"] ?? REGIONS.eu).replace(/\/+$/, "");
|
|
369
|
+
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
370
|
+
if (!fetchImpl) {
|
|
371
|
+
throw new KorelyError(
|
|
372
|
+
"No fetch available. On Node < 18 pass a fetch implementation via { fetch }."
|
|
373
|
+
);
|
|
222
374
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}, this.timeoutMs);
|
|
229
|
-
const failed = (e) => new KorelyError(
|
|
230
|
-
timedOut ? `Request timed out after ${this.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`
|
|
375
|
+
return exchange(
|
|
376
|
+
{ baseUrl, timeoutMs: opts.timeoutMs ?? 3e4, fetchImpl },
|
|
377
|
+
"POST",
|
|
378
|
+
"/v1/agents/init",
|
|
379
|
+
{ body: { agent_caller: agentCaller ?? void 0 } }
|
|
231
380
|
);
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
381
|
+
}
|
|
382
|
+
// ── transport ─────────────────────────────────────────────────────────────
|
|
383
|
+
request(method, path, opts = {}) {
|
|
384
|
+
return exchange(
|
|
385
|
+
{ baseUrl: this.baseUrl, timeoutMs: this.timeoutMs, fetchImpl: this.fetchImpl, apiKey: this.apiKey },
|
|
386
|
+
method,
|
|
387
|
+
path,
|
|
388
|
+
opts
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
// ── the key and its account ───────────────────────────────────────────────
|
|
392
|
+
/**
|
|
393
|
+
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
394
|
+
* it, with the same shape.
|
|
395
|
+
*
|
|
396
|
+
* The cheapest authenticated call there is: no scope, no rate limit, no
|
|
397
|
+
* quota, so it is how to check a key that was just minted or rotated without
|
|
398
|
+
* spending anything. `users()` was the usual stand-in, and it needs
|
|
399
|
+
* `memories:read` and counts as a query.
|
|
400
|
+
*/
|
|
401
|
+
async ping() {
|
|
402
|
+
return this.request("GET", "/v1/ping");
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* DELETE /v1/account?confirm=true: delete the account of this key for good,
|
|
406
|
+
* with every key, project, memory, fact and webhook of it; the key stops
|
|
407
|
+
* working. Cloud only, for an account made by `Korely.initAgent()` or
|
|
408
|
+
* `korely init --agent`, which nobody signs in to: the key is the account,
|
|
409
|
+
* and this is how it is closed (GDPR Art. 17).
|
|
410
|
+
*
|
|
411
|
+
* `{ confirm: true }` is required, and checked here, before anything is
|
|
412
|
+
* sent: without it, a KorelyError whose `code` is `confirmation_required`,
|
|
413
|
+
* the code the server gives the same refusal.
|
|
414
|
+
*
|
|
415
|
+
* An account with a Korely login rejects with 409 `account_has_login`
|
|
416
|
+
* (ConflictError): a key that ended up in a log must not be able to delete
|
|
417
|
+
* it, so it is closed from the app (Settings, Account). The Self-hosted has
|
|
418
|
+
* no such route and answers 404 (NotFoundError), or 405 where it serves its
|
|
419
|
+
* dashboard. No audit event survives: the trail is part of what goes.
|
|
420
|
+
*/
|
|
421
|
+
async deleteAccount(opts = {}) {
|
|
422
|
+
if (opts?.confirm !== true) {
|
|
423
|
+
throw new KorelyError(
|
|
424
|
+
"deleteAccount() deletes this key's account, every key, memory and fact of it, for good. Pass { confirm: true } to mean it.",
|
|
425
|
+
{ code: "confirmation_required" }
|
|
426
|
+
);
|
|
252
427
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
428
|
+
return this.request("DELETE", "/v1/account", { params: { confirm: "true" } });
|
|
429
|
+
}
|
|
430
|
+
// ── audit ─────────────────────────────────────────────────────────────────
|
|
431
|
+
/**
|
|
432
|
+
* GET /v1/audit: what this key's project did, and what its agents read,
|
|
433
|
+
* newest first (`ts` descending, then the event's id, so paging with
|
|
434
|
+
* `offset` is stable). Both products; the key needs `memories:read`.
|
|
435
|
+
*
|
|
436
|
+
* `user_id` keeps the events that touched one end user, the shape of an
|
|
437
|
+
* access or erasure request; `action` one kind ("read", "write", "erase"...;
|
|
438
|
+
* an unknown one answers an empty page); `since` and `until` bound `ts`,
|
|
439
|
+
* both inclusive, as ISO 8601 text or a Date. `limit` is 1 to 1000. To read
|
|
440
|
+
* everything, use `iterAudit()`. The page is iterable.
|
|
441
|
+
*
|
|
442
|
+
* Reading the trail counts against no quota and is not itself written to
|
|
443
|
+
* it; it does count against the rate limit. Scoped to the key's account and
|
|
444
|
+
* project, like every read.
|
|
445
|
+
*/
|
|
446
|
+
async audit(opts = {}) {
|
|
447
|
+
const page = await this.request(
|
|
448
|
+
"GET",
|
|
449
|
+
"/v1/audit",
|
|
450
|
+
{
|
|
451
|
+
params: { ...auditFilters(opts), limit: opts.limit ?? 100, offset: opts.offset ?? 0 }
|
|
265
452
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
453
|
+
);
|
|
454
|
+
return iterableOver(page, "events");
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Every event `audit()` would page through, for an export:
|
|
458
|
+
* `for await (const e of korely.iterAudit({ user_id }))`. The same filters,
|
|
459
|
+
* `page_size` events per request (1000, the most the API gives),
|
|
460
|
+
* `offset += page.length` until `total`.
|
|
461
|
+
*
|
|
462
|
+
* Without `until` the walk pins it to the newest event of its first page.
|
|
463
|
+
* The trail grows while it is read, newest first, and every new event pushed
|
|
464
|
+
* the rest one place down: the next page began with events already returned.
|
|
465
|
+
* The pin makes the export the trail as it stood when it started.
|
|
466
|
+
*
|
|
467
|
+
* A rate limit rejects mid-way, as any call does, and the SDK does not
|
|
468
|
+
* retry. To resume, call again with the same `until` (pin it yourself:
|
|
469
|
+
* `until: new Date()`) and `offset` the number of events already read.
|
|
470
|
+
*/
|
|
471
|
+
async *iterAudit(opts = {}) {
|
|
472
|
+
const filters = auditFilters(opts);
|
|
473
|
+
let offset = opts.offset ?? 0;
|
|
474
|
+
let pin = filters.until === void 0 && offset === 0;
|
|
475
|
+
for (; ; ) {
|
|
476
|
+
const page = await this.audit({
|
|
477
|
+
user_id: opts.user_id,
|
|
478
|
+
action: opts.action,
|
|
479
|
+
since: filters.since,
|
|
480
|
+
until: filters.until,
|
|
481
|
+
limit: opts.page_size ?? 1e3,
|
|
482
|
+
offset
|
|
483
|
+
});
|
|
484
|
+
if (!page.events.length) return;
|
|
485
|
+
yield* page.events;
|
|
486
|
+
if (pin && page.events[0].ts) {
|
|
487
|
+
filters.until = page.events[0].ts;
|
|
488
|
+
pin = false;
|
|
284
489
|
}
|
|
285
|
-
|
|
490
|
+
offset += page.events.length;
|
|
491
|
+
if (offset >= page.total) return;
|
|
286
492
|
}
|
|
287
|
-
throw new APIError(msg, { status, code });
|
|
288
493
|
}
|
|
289
494
|
// ── memories ────────────────────────────────────────────────────────────
|
|
290
495
|
/**
|
|
@@ -617,6 +822,12 @@ var Korely = class {
|
|
|
617
822
|
* `code === "invalid_request"`) whose message names the item, e.g.
|
|
618
823
|
* `memories[3].timestamp`. Any key not listed above is refused the same
|
|
619
824
|
* way. Servers older than 2026-09-28 refuse `timestamp` itself.
|
|
825
|
+
*
|
|
826
|
+
* On the Cloud a batch is also refused, before anything is stored, with a
|
|
827
|
+
* 429: `quota_exceeded` (QuotaExceededError) when its memories do not fit in
|
|
828
|
+
* what is left of the month, batches already queued included, and
|
|
829
|
+
* `too_many_batches` (TooManyBatchesError) while three batches are still
|
|
830
|
+
* being imported. The Self-hosted meters nothing and refuses neither.
|
|
620
831
|
*/
|
|
621
832
|
async batch(memories) {
|
|
622
833
|
return this.request("POST", "/v1/batch", { body: { memories } });
|
|
@@ -630,11 +841,13 @@ var Korely = class {
|
|
|
630
841
|
0 && (module.exports = {
|
|
631
842
|
APIError,
|
|
632
843
|
AuthenticationError,
|
|
844
|
+
ConflictError,
|
|
633
845
|
Korely,
|
|
634
846
|
KorelyError,
|
|
635
847
|
NamespaceForbiddenError,
|
|
636
848
|
NotFoundError,
|
|
637
849
|
QuotaExceededError,
|
|
638
850
|
StaleWriteError,
|
|
851
|
+
TooManyBatchesError,
|
|
639
852
|
VERSION
|
|
640
853
|
});
|