korely-memory 0.3.0 → 0.4.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/README.md +23 -10
- package/dist/{chunk-E6ER37KI.js → chunk-TX5ICJAX.js} +74 -17
- package/dist/index.cjs +74 -17
- package/dist/index.d.cts +99 -15
- package/dist/index.d.ts +99 -15
- package/dist/index.js +1 -1
- package/dist/index.node.cjs +74 -17
- package/dist/index.node.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -99,22 +99,22 @@ Every method maps to one REST endpoint.
|
|
|
99
99
|
| `getAll(opts?)` | `GET /v1/memories` | List a scope, newest first. The page is iterable; `limit` up to 200. |
|
|
100
100
|
| `get(id)` | `GET /v1/memories/:id` | One memory, with its facts. |
|
|
101
101
|
| `update(id, { content })` | `PATCH /v1/memories/:id` | Re-runs extraction. |
|
|
102
|
-
| `delete(id)` | `DELETE /v1/memories/:id` | Forget one (audited). `facts_reopened` counts the facts it made true again. |
|
|
102
|
+
| `delete(id, { erase? })` | `DELETE /v1/memories/:id` | Forget one (audited). `facts_reopened` counts the facts it made true again. `erase: true` erases it for good: the receipt says `erasure` and `facts_deleted`. |
|
|
103
103
|
| `deleteAll({ user_id })` | `DELETE /v1/users/:user_id/memories` | Erase everything about one end user in one call. The receipt counts `memories_deleted` and `facts_deleted`. |
|
|
104
104
|
| `history(id)` | `GET /v1/memories/:id/history` | A memory's timeline + the facts it produced. |
|
|
105
105
|
| `users(opts?)` | `GET /v1/users` | Your end users, with counts. |
|
|
106
106
|
| `listAgents(opts?)` | `GET /v1/agents` | This project's agent namespaces, with counts. `total` counts this project's names, `used` the cap slots taken across the account. Call after `agent_cap_exceeded` to reuse an id. |
|
|
107
107
|
| `deleteAgent(agentId)` | `DELETE /v1/agents/:agent_id` | Purge an agent namespace of this project. `NotFoundError` for a name the project does not use; the receipt's `slot_freed` is always undefined, because no Korely server sends it: read `cap` and `used` from `listAgents()` to see whether a slot is free. |
|
|
108
|
-
| `getFacts(opts?)` | `GET /v1/facts` | Typed facts; `as_of` for what was true on a date, `known_as_of` for what the store knew on it
|
|
108
|
+
| `getFacts(opts?)` | `GET /v1/facts` | Typed facts; `as_of` for what was true on a date, `known_as_of` for what the store knew on it; `reader` keeps the facts handed over for that reader's next `getContext`. The array also carries `total`. |
|
|
109
109
|
| `addFactTriple(s, p, o, opts?)` | `POST /v1/facts` | Write a fact directly (bi-temporal; `tense: "past"` for one that is over). |
|
|
110
110
|
| `correctFact(id, changes)` | `PATCH /v1/facts/:id` | Supersede a fact with a corrected one; `invalidated` lists what it closed. Restating the fact as it stands reconfirms it (`invalidated: []`). |
|
|
111
111
|
| `forgetFact(id, { at? })` | `POST /v1/facts/:id/forget` | Close a fact; it stays in history. |
|
|
112
112
|
| `getProfile({ user_id, as_of?, known_as_of? })` | `GET /v1/profile` | The assembled profile of one end user. |
|
|
113
|
-
| `getContext({ query, user_id?, known_as_of? })` | `GET /v1/context` | One call, a prompt-ready context block; `known_as_of` rebuilds it as the store could have built it then. |
|
|
113
|
+
| `getContext({ query, user_id?, known_as_of?, reader?, changes? })` | `GET /v1/context` | One call, a prompt-ready context block (at most 10 facts, the nearest the question: a larger `token_budget` adds memories; a memory too long for its room is cut); `known_as_of` rebuilds it as the store could have built it then. `reader` (your agent's own name, Korely 1.0.15+) opens the block with what changed since in the facts that reader was handed: `changed` (at most 3) and `changed_more`; `changes: "all"` adds the closures read from memories in an agent's memory with no end user. |
|
|
114
114
|
| `events(opts?)` | `GET /v1/events` | Which writes are still being extracted; `limit` up to 200. |
|
|
115
115
|
| `batch(memories, opts?)` | `POST /v1/batch` | Bulk import, for migrations. Each item takes `user_id` or `agent_id` and `timestamp`, as `add()` does. `opts.idempotency_key` as in `add()`. |
|
|
116
116
|
| `batchStatus(jobId)` | `GET /v1/batch/:id` | Poll an import job. |
|
|
117
|
-
| `ping()` | `GET /v1/ping` | Check a key without spending
|
|
117
|
+
| `ping()` | `GET /v1/ping` | Check a key without spending any quota (no scope needed; the rate limit counts it, as every call): its `tier`, `region` and `scopes`. Both products. |
|
|
118
118
|
| `Korely.initAgent(agentCaller?, opts?)` | `POST /v1/agents/init` | Cloud only. Static, no key needed: sign up for a key that costs nothing (shown once, save it), then `new Korely({ apiKey: result.api_key })`. The answer says when the key stops working (`expires_at`) and, in `note`, the trial's limits until the account is adopted and how to adopt it; `quotas` are the monthly numbers once adopted. `opts` takes `baseUrl`, `region`, `timeoutMs`, `fetch`. |
|
|
119
119
|
| `deleteAccount({ confirm: "<account id>" })` | `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`). `confirm` is the account's id, typed (`tenant_id` of `GET /admin/whoami`): the DELETE is sent only when it is a UUID equal, spaces trimmed, to the id `GET /admin/whoami` gives the key. Missing, `true`, `"true"` or another id: nothing is sent but that GET, and it rejects with `KorelyError` `confirmation_required`, whose message names the id to type. Only the answer `deleted: true` is a deletion; any other rejects. A server older than 1.0.23 takes only `?confirm=true`: the client does not fall back to it, deletes nothing and rejects with `APIError` `server_too_old` (upgrade the server, or use korely-memory 0.1.18). A key that may not read `GET /admin/whoami` (403, a key without the `admin` scope) deletes nothing: with no id typed it rejects with that `confirmation_required`, whose message says whoami could not be read; with the id typed, with the whoami's refusal, `NamespaceForbiddenError` (an `APIError`, `status` 403, `code` `forbidden`). The Self-hosted, which has no such route, stops at that 403 for a key without the `admin` scope, else answers 404 (405 where it serves its dashboard). |
|
|
120
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. Each event's `api_key_id` names the key that made the call (Korely 1.0.23); `action` is `forget` for a memory forgotten with its text kept, `erase` for one gone for good, `fact_invalidate` for a fact closed (a forget was filed as `erase` before 1.0.23). Both products; needs `memories:read`, costs no quota. |
|
|
@@ -157,6 +157,7 @@ try {
|
|
|
157
157
|
await korely.add("...", { user_id: "u" });
|
|
158
158
|
} catch (e) {
|
|
159
159
|
if (e instanceof QuotaExceededError && e.retryAfter !== undefined) {
|
|
160
|
+
// after the client's own waits: still limited
|
|
160
161
|
console.log(`Rate limited, retry after ${e.retryAfter}s`);
|
|
161
162
|
} else if (e instanceof APIError && e.code === "invalid_key") {
|
|
162
163
|
console.log("Bad or missing API key");
|
|
@@ -166,21 +167,31 @@ try {
|
|
|
166
167
|
}
|
|
167
168
|
```
|
|
168
169
|
|
|
169
|
-
`AuthenticationError` (401) · `NamespaceForbiddenError` (403, e.g.
|
|
170
|
-
|
|
170
|
+
`AuthenticationError` (401) · `NamespaceForbiddenError` (403, e.g. a
|
|
171
|
+
missing scope) · `NotFoundError` (404) · `ConflictError` (409, e.g.
|
|
171
172
|
`account_has_login`; `fact_not_current` from `correctFact()` of a fact that is
|
|
172
173
|
history, with `currentFactId` the fact to correct instead; `stale_write` is its
|
|
173
174
|
subclass `StaleWriteError`) ·
|
|
174
175
|
`QuotaExceededError` (429: `retryAfter` is set for `rate_limit_exceeded`,
|
|
175
|
-
undefined for the monthly `quota_exceeded`
|
|
176
|
+
undefined for the monthly `quota_exceeded` and for `agent_cap_exceeded`, a
|
|
177
|
+
new agent name past the plan's, Cloud only) · `TooManyBatchesError` (a
|
|
176
178
|
`QuotaExceededError`: `too_many_batches` from `batch()`, Cloud only, three
|
|
177
179
|
imports still running; send again when one finishes) · `APIError` (the base
|
|
178
180
|
of all of these, and everything else: 422, 503; a write that names neither
|
|
179
181
|
`user_id` nor `agent_id` is a 422 `identity_required` on Korely 1.0.19 and
|
|
180
182
|
later, and this client sends only the ids each call names). Every `APIError` has
|
|
181
|
-
`retryAfter` when the server sent `Retry-After`: a rate limit, and the
|
|
182
|
-
|
|
183
|
-
|
|
183
|
+
`retryAfter` when the server sent `Retry-After`: a rate limit, and the 503
|
|
184
|
+
`service_paused` (the service does not call the models now, a spending stop or
|
|
185
|
+
a provider's brake; it asks for an hour).
|
|
186
|
+
|
|
187
|
+
A 429 that says how long to wait (Retry-After) is waited out by the client,
|
|
188
|
+
which sends the same call again: at most 3 more times and 60 seconds of
|
|
189
|
+
waiting in all for each request (`maxRetries`, `maxRetryWait`), a write with the
|
|
190
|
+
same Idempotency-Key, so it is made once. A wait that would pass those bounds
|
|
191
|
+
is not waited: `QuotaExceededError` rejects at once, with `retryAfter`, and so
|
|
192
|
+
does a 429 that names no wait (the month's quota, the agent cap).
|
|
193
|
+
`maxRetries: 0` sends every call once, as up to 0.3.0. The client never
|
|
194
|
+
retries a 503 by itself; its other retry is of a write that got no answer at
|
|
184
195
|
all (see `add()`).
|
|
185
196
|
|
|
186
197
|
A call that is not a `GET` (`add`, `update`, `delete`...) is never redirected:
|
|
@@ -197,6 +208,8 @@ new Korely({
|
|
|
197
208
|
region: "eu", // the API, api.korely.ai: data stored in the EU
|
|
198
209
|
timeoutMs: 30000, // covers the whole request, response body included
|
|
199
210
|
retryWrites: true, // a write with no answer is sent once more, same key
|
|
211
|
+
maxRetries: 3, // a 429 with a wait is sent again, at most 3 more times
|
|
212
|
+
maxRetryWait: 60, // and after at most 60 s of waiting in all; 0 retries: maxRetries: 0
|
|
200
213
|
});
|
|
201
214
|
```
|
|
202
215
|
|
|
@@ -78,7 +78,7 @@ var TooManyBatchesError = class _TooManyBatchesError extends QuotaExceededError
|
|
|
78
78
|
};
|
|
79
79
|
|
|
80
80
|
// src/client.ts
|
|
81
|
-
var VERSION = "0.
|
|
81
|
+
var VERSION = "0.4.0";
|
|
82
82
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
83
83
|
function regionUrl(region = "eu") {
|
|
84
84
|
const url = typeof region === "string" && Object.prototype.hasOwnProperty.call(REGIONS, region) ? REGIONS[region] : void 0;
|
|
@@ -96,7 +96,9 @@ var CLIENT_KEYS = keysOf()(
|
|
|
96
96
|
"timeoutMs",
|
|
97
97
|
"fetch",
|
|
98
98
|
"client",
|
|
99
|
-
"retryWrites"
|
|
99
|
+
"retryWrites",
|
|
100
|
+
"maxRetries",
|
|
101
|
+
"maxRetryWait"
|
|
100
102
|
);
|
|
101
103
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
102
104
|
var ADD_KEYS = keysOf()(
|
|
@@ -109,6 +111,21 @@ var ADD_KEYS = keysOf()(
|
|
|
109
111
|
"idempotency_key"
|
|
110
112
|
);
|
|
111
113
|
var WRITE_RETRY_DELAY_MS = 500;
|
|
114
|
+
var RATE_LIMIT_RETRIES = 3;
|
|
115
|
+
var RATE_LIMIT_WAIT = 60;
|
|
116
|
+
async function exchangeWaitingOut(t, b, method, path, opts = {}) {
|
|
117
|
+
let waited = 0;
|
|
118
|
+
for (let tries = 0; ; tries++) {
|
|
119
|
+
try {
|
|
120
|
+
return await exchange(t, method, path, opts);
|
|
121
|
+
} catch (e) {
|
|
122
|
+
const wait = e instanceof QuotaExceededError && e.status === 429 ? e.retryAfter : void 0;
|
|
123
|
+
if (wait === void 0 || tries >= b.maxRetries || waited + wait > b.maxRetryWait) throw e;
|
|
124
|
+
await Korely._sleep(wait * 1e3);
|
|
125
|
+
waited += wait;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
112
129
|
function newIdempotencyKey() {
|
|
113
130
|
const c = globalThis.crypto;
|
|
114
131
|
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
@@ -157,7 +174,8 @@ var FACTS_KEYS = keysOf()(
|
|
|
157
174
|
"agent_id",
|
|
158
175
|
"limit",
|
|
159
176
|
"offset",
|
|
160
|
-
"exclude_sessions"
|
|
177
|
+
"exclude_sessions",
|
|
178
|
+
"reader"
|
|
161
179
|
);
|
|
162
180
|
var TRIPLE_KEYS = keysOf()(
|
|
163
181
|
"user_id",
|
|
@@ -182,14 +200,18 @@ var CONTEXT_KEYS = keysOf()(
|
|
|
182
200
|
"agent_id",
|
|
183
201
|
"token_budget",
|
|
184
202
|
"known_as_of",
|
|
185
|
-
"exclude_sessions"
|
|
203
|
+
"exclude_sessions",
|
|
204
|
+
"reader",
|
|
205
|
+
"changes"
|
|
186
206
|
);
|
|
187
207
|
var CONTEXT_EXTRA_KEYS = keysOf()(
|
|
188
208
|
"user_id",
|
|
189
209
|
"agent_id",
|
|
190
210
|
"token_budget",
|
|
191
211
|
"known_as_of",
|
|
192
|
-
"exclude_sessions"
|
|
212
|
+
"exclude_sessions",
|
|
213
|
+
"reader",
|
|
214
|
+
"changes"
|
|
193
215
|
);
|
|
194
216
|
function checkOptions(method, opts, known) {
|
|
195
217
|
const problem = unknownOption(method, opts, known);
|
|
@@ -499,6 +521,13 @@ function text(v) {
|
|
|
499
521
|
return typeof v === "string" && v ? v : void 0;
|
|
500
522
|
}
|
|
501
523
|
var Korely = class {
|
|
524
|
+
static {
|
|
525
|
+
/**
|
|
526
|
+
* How a wait is waited: the one seam the tests replace.
|
|
527
|
+
* @internal
|
|
528
|
+
*/
|
|
529
|
+
this._sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
530
|
+
}
|
|
502
531
|
constructor(opts = {}) {
|
|
503
532
|
checkOptions("Korely", opts, CLIENT_KEYS);
|
|
504
533
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
@@ -516,6 +545,16 @@ var Korely = class {
|
|
|
516
545
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
517
546
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
518
547
|
this.retryWrites = opts.retryWrites ?? true;
|
|
548
|
+
const retries = opts.maxRetries ?? RATE_LIMIT_RETRIES;
|
|
549
|
+
if (!Number.isInteger(retries) || retries < 0) {
|
|
550
|
+
throw new KorelyError(`maxRetries is a whole number, 0 or more, not ${String(opts.maxRetries)}.`);
|
|
551
|
+
}
|
|
552
|
+
const wait = opts.maxRetryWait ?? RATE_LIMIT_WAIT;
|
|
553
|
+
if (typeof wait !== "number" || !(wait >= 0)) {
|
|
554
|
+
throw new KorelyError(`maxRetryWait is seconds, 0 or more, not ${String(opts.maxRetryWait)}.`);
|
|
555
|
+
}
|
|
556
|
+
this.maxRetries = retries;
|
|
557
|
+
this.maxRetryWait = wait;
|
|
519
558
|
const f = opts.fetch ?? globalThis.fetch;
|
|
520
559
|
if (!f) {
|
|
521
560
|
throw new KorelyError(
|
|
@@ -565,8 +604,9 @@ var Korely = class {
|
|
|
565
604
|
"No fetch available. On Node < 18 pass a fetch implementation via { fetch }."
|
|
566
605
|
);
|
|
567
606
|
}
|
|
568
|
-
return
|
|
607
|
+
return exchangeWaitingOut(
|
|
569
608
|
{ baseUrl, timeoutMs: opts.timeoutMs ?? 3e4, fetchImpl },
|
|
609
|
+
{ maxRetries: RATE_LIMIT_RETRIES, maxRetryWait: RATE_LIMIT_WAIT },
|
|
570
610
|
"POST",
|
|
571
611
|
"/v1/agents/init",
|
|
572
612
|
{
|
|
@@ -576,7 +616,7 @@ var Korely = class {
|
|
|
576
616
|
}
|
|
577
617
|
// ── transport ─────────────────────────────────────────────────────────────
|
|
578
618
|
request(method, path, opts = {}) {
|
|
579
|
-
return
|
|
619
|
+
return exchangeWaitingOut(
|
|
580
620
|
{
|
|
581
621
|
baseUrl: this.baseUrl,
|
|
582
622
|
timeoutMs: this.timeoutMs,
|
|
@@ -584,6 +624,7 @@ var Korely = class {
|
|
|
584
624
|
apiKey: this.apiKey,
|
|
585
625
|
client: this.client
|
|
586
626
|
},
|
|
627
|
+
{ maxRetries: this.maxRetries, maxRetryWait: this.maxRetryWait },
|
|
587
628
|
method,
|
|
588
629
|
path,
|
|
589
630
|
opts
|
|
@@ -628,9 +669,10 @@ var Korely = class {
|
|
|
628
669
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
629
670
|
* it, with the same shape.
|
|
630
671
|
*
|
|
631
|
-
* The cheapest authenticated call there is: no scope
|
|
632
|
-
*
|
|
633
|
-
*
|
|
672
|
+
* The cheapest authenticated call there is: no scope and no quota, so it is
|
|
673
|
+
* how to check a key that was just minted or rotated without spending any.
|
|
674
|
+
* It does count against the rate limit, as every call does: past it, a 429
|
|
675
|
+
* with `retryAfter`. `users()` was the usual stand-in, and it needs
|
|
634
676
|
* `memories:read` and counts as a query.
|
|
635
677
|
*/
|
|
636
678
|
async ping() {
|
|
@@ -777,8 +819,8 @@ var Korely = class {
|
|
|
777
819
|
* the rest one place down: the next page began with events already returned.
|
|
778
820
|
* The pin makes the export the trail as it stood when it started.
|
|
779
821
|
*
|
|
780
|
-
* A rate limit
|
|
781
|
-
*
|
|
822
|
+
* A rate limit the client could not wait out (`maxRetries`) rejects mid-way,
|
|
823
|
+
* as any call does. To resume, call again with the same `until` (pin it yourself:
|
|
782
824
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
783
825
|
*/
|
|
784
826
|
async *iterAudit(opts = {}) {
|
|
@@ -926,10 +968,22 @@ var Korely = class {
|
|
|
926
968
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
927
969
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
928
970
|
* audited). The facts its facts had closed are true again: the receipt
|
|
929
|
-
* counts them in `facts_reopened`.
|
|
971
|
+
* counts them in `facts_reopened`.
|
|
972
|
+
*
|
|
973
|
+
* `{ erase: true }` erases it for good instead: the row, its text and the
|
|
974
|
+
* facts it alone stated go from the database; the receipt says `status`
|
|
975
|
+
* "erased", `erasure` "permanent" and counts `facts_deleted`. For every
|
|
976
|
+
* memory of one end user, `deleteAll`. `erase` is `true` or `false` and
|
|
977
|
+
* nothing else.
|
|
930
978
|
*/
|
|
931
|
-
async delete(memoryId) {
|
|
932
|
-
|
|
979
|
+
async delete(memoryId, opts = {}) {
|
|
980
|
+
checkOptions("delete", opts, ["erase"]);
|
|
981
|
+
if (opts.erase !== void 0 && typeof opts.erase !== "boolean") {
|
|
982
|
+
throw new KorelyError(`delete({ erase }) takes true or false, not ${typeof opts.erase}.`);
|
|
983
|
+
}
|
|
984
|
+
return this.request("DELETE", `/v1/memories/${seg(memoryId, "memoryId")}`, {
|
|
985
|
+
params: opts.erase ? { erase: "true" } : void 0
|
|
986
|
+
});
|
|
933
987
|
}
|
|
934
988
|
/**
|
|
935
989
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
@@ -1039,7 +1093,8 @@ var Korely = class {
|
|
|
1039
1093
|
user_id: opts.user_id,
|
|
1040
1094
|
agent_id: opts.agent_id,
|
|
1041
1095
|
limit: opts.limit ?? 50,
|
|
1042
|
-
offset: opts.offset ?? 0
|
|
1096
|
+
offset: opts.offset ?? 0,
|
|
1097
|
+
reader: opts.reader
|
|
1043
1098
|
};
|
|
1044
1099
|
if (opts.include_invalidated) params.include_invalidated = "true";
|
|
1045
1100
|
if (opts.exclude_sessions) params.exclude_sessions = "true";
|
|
@@ -1159,7 +1214,9 @@ var Korely = class {
|
|
|
1159
1214
|
agent_id: o.agent_id,
|
|
1160
1215
|
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1161
1216
|
known_as_of: o.known_as_of,
|
|
1162
|
-
exclude_sessions: o.exclude_sessions ? "true" : void 0
|
|
1217
|
+
exclude_sessions: o.exclude_sessions ? "true" : void 0,
|
|
1218
|
+
reader: o.reader,
|
|
1219
|
+
changes: o.changes
|
|
1163
1220
|
}
|
|
1164
1221
|
});
|
|
1165
1222
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -127,7 +127,7 @@ function unknownOption(method, opts, known) {
|
|
|
127
127
|
var DEFAULT_TOKEN_BUDGET = 1500;
|
|
128
128
|
|
|
129
129
|
// src/client.ts
|
|
130
|
-
var VERSION = "0.
|
|
130
|
+
var VERSION = "0.4.0";
|
|
131
131
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
132
132
|
function regionUrl(region = "eu") {
|
|
133
133
|
const url = typeof region === "string" && Object.prototype.hasOwnProperty.call(REGIONS, region) ? REGIONS[region] : void 0;
|
|
@@ -145,7 +145,9 @@ var CLIENT_KEYS = keysOf()(
|
|
|
145
145
|
"timeoutMs",
|
|
146
146
|
"fetch",
|
|
147
147
|
"client",
|
|
148
|
-
"retryWrites"
|
|
148
|
+
"retryWrites",
|
|
149
|
+
"maxRetries",
|
|
150
|
+
"maxRetryWait"
|
|
149
151
|
);
|
|
150
152
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
151
153
|
var ADD_KEYS = keysOf()(
|
|
@@ -158,6 +160,21 @@ var ADD_KEYS = keysOf()(
|
|
|
158
160
|
"idempotency_key"
|
|
159
161
|
);
|
|
160
162
|
var WRITE_RETRY_DELAY_MS = 500;
|
|
163
|
+
var RATE_LIMIT_RETRIES = 3;
|
|
164
|
+
var RATE_LIMIT_WAIT = 60;
|
|
165
|
+
async function exchangeWaitingOut(t, b, method, path, opts = {}) {
|
|
166
|
+
let waited = 0;
|
|
167
|
+
for (let tries = 0; ; tries++) {
|
|
168
|
+
try {
|
|
169
|
+
return await exchange(t, method, path, opts);
|
|
170
|
+
} catch (e) {
|
|
171
|
+
const wait = e instanceof QuotaExceededError && e.status === 429 ? e.retryAfter : void 0;
|
|
172
|
+
if (wait === void 0 || tries >= b.maxRetries || waited + wait > b.maxRetryWait) throw e;
|
|
173
|
+
await Korely._sleep(wait * 1e3);
|
|
174
|
+
waited += wait;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
161
178
|
function newIdempotencyKey() {
|
|
162
179
|
const c = globalThis.crypto;
|
|
163
180
|
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
@@ -206,7 +223,8 @@ var FACTS_KEYS = keysOf()(
|
|
|
206
223
|
"agent_id",
|
|
207
224
|
"limit",
|
|
208
225
|
"offset",
|
|
209
|
-
"exclude_sessions"
|
|
226
|
+
"exclude_sessions",
|
|
227
|
+
"reader"
|
|
210
228
|
);
|
|
211
229
|
var TRIPLE_KEYS = keysOf()(
|
|
212
230
|
"user_id",
|
|
@@ -231,14 +249,18 @@ var CONTEXT_KEYS = keysOf()(
|
|
|
231
249
|
"agent_id",
|
|
232
250
|
"token_budget",
|
|
233
251
|
"known_as_of",
|
|
234
|
-
"exclude_sessions"
|
|
252
|
+
"exclude_sessions",
|
|
253
|
+
"reader",
|
|
254
|
+
"changes"
|
|
235
255
|
);
|
|
236
256
|
var CONTEXT_EXTRA_KEYS = keysOf()(
|
|
237
257
|
"user_id",
|
|
238
258
|
"agent_id",
|
|
239
259
|
"token_budget",
|
|
240
260
|
"known_as_of",
|
|
241
|
-
"exclude_sessions"
|
|
261
|
+
"exclude_sessions",
|
|
262
|
+
"reader",
|
|
263
|
+
"changes"
|
|
242
264
|
);
|
|
243
265
|
function checkOptions(method, opts, known) {
|
|
244
266
|
const problem = unknownOption(method, opts, known);
|
|
@@ -545,6 +567,13 @@ function text(v) {
|
|
|
545
567
|
return typeof v === "string" && v ? v : void 0;
|
|
546
568
|
}
|
|
547
569
|
var Korely = class {
|
|
570
|
+
static {
|
|
571
|
+
/**
|
|
572
|
+
* How a wait is waited: the one seam the tests replace.
|
|
573
|
+
* @internal
|
|
574
|
+
*/
|
|
575
|
+
this._sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
576
|
+
}
|
|
548
577
|
constructor(opts = {}) {
|
|
549
578
|
checkOptions("Korely", opts, CLIENT_KEYS);
|
|
550
579
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
@@ -562,6 +591,16 @@ var Korely = class {
|
|
|
562
591
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
563
592
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
564
593
|
this.retryWrites = opts.retryWrites ?? true;
|
|
594
|
+
const retries = opts.maxRetries ?? RATE_LIMIT_RETRIES;
|
|
595
|
+
if (!Number.isInteger(retries) || retries < 0) {
|
|
596
|
+
throw new KorelyError(`maxRetries is a whole number, 0 or more, not ${String(opts.maxRetries)}.`);
|
|
597
|
+
}
|
|
598
|
+
const wait = opts.maxRetryWait ?? RATE_LIMIT_WAIT;
|
|
599
|
+
if (typeof wait !== "number" || !(wait >= 0)) {
|
|
600
|
+
throw new KorelyError(`maxRetryWait is seconds, 0 or more, not ${String(opts.maxRetryWait)}.`);
|
|
601
|
+
}
|
|
602
|
+
this.maxRetries = retries;
|
|
603
|
+
this.maxRetryWait = wait;
|
|
565
604
|
const f = opts.fetch ?? globalThis.fetch;
|
|
566
605
|
if (!f) {
|
|
567
606
|
throw new KorelyError(
|
|
@@ -611,8 +650,9 @@ var Korely = class {
|
|
|
611
650
|
"No fetch available. On Node < 18 pass a fetch implementation via { fetch }."
|
|
612
651
|
);
|
|
613
652
|
}
|
|
614
|
-
return
|
|
653
|
+
return exchangeWaitingOut(
|
|
615
654
|
{ baseUrl, timeoutMs: opts.timeoutMs ?? 3e4, fetchImpl },
|
|
655
|
+
{ maxRetries: RATE_LIMIT_RETRIES, maxRetryWait: RATE_LIMIT_WAIT },
|
|
616
656
|
"POST",
|
|
617
657
|
"/v1/agents/init",
|
|
618
658
|
{
|
|
@@ -622,7 +662,7 @@ var Korely = class {
|
|
|
622
662
|
}
|
|
623
663
|
// ── transport ─────────────────────────────────────────────────────────────
|
|
624
664
|
request(method, path, opts = {}) {
|
|
625
|
-
return
|
|
665
|
+
return exchangeWaitingOut(
|
|
626
666
|
{
|
|
627
667
|
baseUrl: this.baseUrl,
|
|
628
668
|
timeoutMs: this.timeoutMs,
|
|
@@ -630,6 +670,7 @@ var Korely = class {
|
|
|
630
670
|
apiKey: this.apiKey,
|
|
631
671
|
client: this.client
|
|
632
672
|
},
|
|
673
|
+
{ maxRetries: this.maxRetries, maxRetryWait: this.maxRetryWait },
|
|
633
674
|
method,
|
|
634
675
|
path,
|
|
635
676
|
opts
|
|
@@ -674,9 +715,10 @@ var Korely = class {
|
|
|
674
715
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
675
716
|
* it, with the same shape.
|
|
676
717
|
*
|
|
677
|
-
* The cheapest authenticated call there is: no scope
|
|
678
|
-
*
|
|
679
|
-
*
|
|
718
|
+
* The cheapest authenticated call there is: no scope and no quota, so it is
|
|
719
|
+
* how to check a key that was just minted or rotated without spending any.
|
|
720
|
+
* It does count against the rate limit, as every call does: past it, a 429
|
|
721
|
+
* with `retryAfter`. `users()` was the usual stand-in, and it needs
|
|
680
722
|
* `memories:read` and counts as a query.
|
|
681
723
|
*/
|
|
682
724
|
async ping() {
|
|
@@ -823,8 +865,8 @@ var Korely = class {
|
|
|
823
865
|
* the rest one place down: the next page began with events already returned.
|
|
824
866
|
* The pin makes the export the trail as it stood when it started.
|
|
825
867
|
*
|
|
826
|
-
* A rate limit
|
|
827
|
-
*
|
|
868
|
+
* A rate limit the client could not wait out (`maxRetries`) rejects mid-way,
|
|
869
|
+
* as any call does. To resume, call again with the same `until` (pin it yourself:
|
|
828
870
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
829
871
|
*/
|
|
830
872
|
async *iterAudit(opts = {}) {
|
|
@@ -972,10 +1014,22 @@ var Korely = class {
|
|
|
972
1014
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
973
1015
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
974
1016
|
* audited). The facts its facts had closed are true again: the receipt
|
|
975
|
-
* counts them in `facts_reopened`.
|
|
1017
|
+
* counts them in `facts_reopened`.
|
|
1018
|
+
*
|
|
1019
|
+
* `{ erase: true }` erases it for good instead: the row, its text and the
|
|
1020
|
+
* facts it alone stated go from the database; the receipt says `status`
|
|
1021
|
+
* "erased", `erasure` "permanent" and counts `facts_deleted`. For every
|
|
1022
|
+
* memory of one end user, `deleteAll`. `erase` is `true` or `false` and
|
|
1023
|
+
* nothing else.
|
|
976
1024
|
*/
|
|
977
|
-
async delete(memoryId) {
|
|
978
|
-
|
|
1025
|
+
async delete(memoryId, opts = {}) {
|
|
1026
|
+
checkOptions("delete", opts, ["erase"]);
|
|
1027
|
+
if (opts.erase !== void 0 && typeof opts.erase !== "boolean") {
|
|
1028
|
+
throw new KorelyError(`delete({ erase }) takes true or false, not ${typeof opts.erase}.`);
|
|
1029
|
+
}
|
|
1030
|
+
return this.request("DELETE", `/v1/memories/${seg(memoryId, "memoryId")}`, {
|
|
1031
|
+
params: opts.erase ? { erase: "true" } : void 0
|
|
1032
|
+
});
|
|
979
1033
|
}
|
|
980
1034
|
/**
|
|
981
1035
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
@@ -1085,7 +1139,8 @@ var Korely = class {
|
|
|
1085
1139
|
user_id: opts.user_id,
|
|
1086
1140
|
agent_id: opts.agent_id,
|
|
1087
1141
|
limit: opts.limit ?? 50,
|
|
1088
|
-
offset: opts.offset ?? 0
|
|
1142
|
+
offset: opts.offset ?? 0,
|
|
1143
|
+
reader: opts.reader
|
|
1089
1144
|
};
|
|
1090
1145
|
if (opts.include_invalidated) params.include_invalidated = "true";
|
|
1091
1146
|
if (opts.exclude_sessions) params.exclude_sessions = "true";
|
|
@@ -1205,7 +1260,9 @@ var Korely = class {
|
|
|
1205
1260
|
agent_id: o.agent_id,
|
|
1206
1261
|
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1207
1262
|
known_as_of: o.known_as_of,
|
|
1208
|
-
exclude_sessions: o.exclude_sessions ? "true" : void 0
|
|
1263
|
+
exclude_sessions: o.exclude_sessions ? "true" : void 0,
|
|
1264
|
+
reader: o.reader,
|
|
1265
|
+
changes: o.changes
|
|
1209
1266
|
}
|
|
1210
1267
|
});
|
|
1211
1268
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -112,10 +112,15 @@ interface MemoryPage extends Iterable<Memory> {
|
|
|
112
112
|
*/
|
|
113
113
|
interface DeleteReceipt {
|
|
114
114
|
id?: string;
|
|
115
|
+
/** "forgotten", or "erased" with `delete(id, { erase: true })`. */
|
|
115
116
|
status?: string;
|
|
116
117
|
facts_invalidated?: number;
|
|
117
118
|
audit_id?: string;
|
|
118
119
|
facts_reopened?: number;
|
|
120
|
+
/** "permanent": in an erasure's receipt only. */
|
|
121
|
+
erasure?: string;
|
|
122
|
+
/** In an erasure's receipt only: the facts only this memory stated, deleted for good. */
|
|
123
|
+
facts_deleted?: number;
|
|
119
124
|
}
|
|
120
125
|
/**
|
|
121
126
|
* What `deleteAll` erased. Every row is physically deleted, and `erasure` is
|
|
@@ -166,6 +171,29 @@ interface Context {
|
|
|
166
171
|
volatile?: string;
|
|
167
172
|
/** SHA-256 (hex) of `stable`: the same value means the same prefix, so a cached system prompt is still valid. */
|
|
168
173
|
stable_hash?: string;
|
|
174
|
+
/**
|
|
175
|
+
* With `reader` only (Korely 1.0.15+): the facts this reader was handed
|
|
176
|
+
* before and that changed since, newest first, at most 3, each told once.
|
|
177
|
+
*/
|
|
178
|
+
changed?: ContextChange[];
|
|
179
|
+
/** With `reader` only: how many more changes wait; null when they could not be read this time. */
|
|
180
|
+
changed_more?: number | null;
|
|
181
|
+
}
|
|
182
|
+
/** A fact a reader was handed and that has ended since (agent notices). */
|
|
183
|
+
interface ContextChange {
|
|
184
|
+
fact_id: string;
|
|
185
|
+
/** It, as the block writes a fact; null when the memory that stated it was forgotten. */
|
|
186
|
+
was: string | null;
|
|
187
|
+
/** What holds now in its place, when this key may read it; null: no longer true. */
|
|
188
|
+
now: {
|
|
189
|
+
fact_id: string;
|
|
190
|
+
text: string;
|
|
191
|
+
} | null;
|
|
192
|
+
ended_at: string | null;
|
|
193
|
+
known_at: string | null;
|
|
194
|
+
origin: "corrected" | "forgotten" | "stated" | "read_from_memory";
|
|
195
|
+
source_memory_id: string | null;
|
|
196
|
+
by_agent: string | null;
|
|
169
197
|
}
|
|
170
198
|
/**
|
|
171
199
|
* One item of `batch()`: the body of a single `add()`. `content` is required,
|
|
@@ -281,7 +309,7 @@ interface AgentsPage extends Iterable<AgentScope> {
|
|
|
281
309
|
cap: number;
|
|
282
310
|
/**
|
|
283
311
|
* Agent slots taken across the whole account, by name; reconciles with the
|
|
284
|
-
* `agent_cap_exceeded`
|
|
312
|
+
* `agent_cap_exceeded` 429. Above `total` when other projects use names this
|
|
285
313
|
* project does not.
|
|
286
314
|
*/
|
|
287
315
|
used: number;
|
|
@@ -572,6 +600,13 @@ interface GetFactsOptions {
|
|
|
572
600
|
offset?: number;
|
|
573
601
|
/** Leave out the work sessions (`kind: "session"`) and what only they stated (Korely Agent 0.1.42). */
|
|
574
602
|
exclude_sessions?: boolean;
|
|
603
|
+
/**
|
|
604
|
+
* Your agent's own name (Korely 1.0.15+, agent notices): the server keeps
|
|
605
|
+
* the ids of the facts this read hands over, and a later
|
|
606
|
+
* `getContext({ reader })` of the same name opens with what changed since in
|
|
607
|
+
* them. It filters nothing.
|
|
608
|
+
*/
|
|
609
|
+
reader?: string;
|
|
575
610
|
}
|
|
576
611
|
interface AddFactTripleOptions {
|
|
577
612
|
/**
|
|
@@ -616,6 +651,18 @@ interface GetContextOptions {
|
|
|
616
651
|
known_as_of?: string;
|
|
617
652
|
/** Leave out the work sessions (`kind: "session"`) and what only they stated (Korely Agent 0.1.42). */
|
|
618
653
|
exclude_sessions?: boolean;
|
|
654
|
+
/**
|
|
655
|
+
* Your agent's own name (Korely 1.0.15+, agent notices): the block opens
|
|
656
|
+
* with what changed since in the facts this reader was handed, and the
|
|
657
|
+
* facts it hands over are kept for the next read. Not authenticated:
|
|
658
|
+
* whoever holds the key can give any name.
|
|
659
|
+
*/
|
|
660
|
+
reader?: string;
|
|
661
|
+
/**
|
|
662
|
+
* `"all"`: with `reader`, in an agent's memory with no end user, also the
|
|
663
|
+
* closures the store read from memories; left out, only the explicit ones.
|
|
664
|
+
*/
|
|
665
|
+
changes?: "all";
|
|
619
666
|
}
|
|
620
667
|
/** What `forgetFact` gives back. `status` is "forgotten" the first time and
|
|
621
668
|
* "already_forgotten" afterwards; `invalid_at` is the date it stopped being
|
|
@@ -629,7 +676,7 @@ interface ForgetReceipt {
|
|
|
629
676
|
|
|
630
677
|
/** Must equal `version` in package.json (a test checks). It goes out in the
|
|
631
678
|
* X-Korely-Client header, and said 0.1.1 from 0.1.1 to 0.1.6. */
|
|
632
|
-
declare const VERSION = "0.
|
|
679
|
+
declare const VERSION = "0.4.0";
|
|
633
680
|
interface KorelyOptions {
|
|
634
681
|
/** Your `kor_live_...` key. Falls back to the KORELY_API_KEY env var. */
|
|
635
682
|
apiKey?: string;
|
|
@@ -663,6 +710,20 @@ interface KorelyOptions {
|
|
|
663
710
|
* the key.
|
|
664
711
|
*/
|
|
665
712
|
retryWrites?: boolean;
|
|
713
|
+
/**
|
|
714
|
+
* A call answered 429 with a wait (Retry-After) is sent again after that
|
|
715
|
+
* wait, at most this many more times. Default 3; 0 sends every call once,
|
|
716
|
+
* as up to 0.3.0. A write keeps its Idempotency-Key, so it is made once. A
|
|
717
|
+
* 429 that names no wait (the month's quota, the agent cap) is never sent
|
|
718
|
+
* again.
|
|
719
|
+
*/
|
|
720
|
+
maxRetries?: number;
|
|
721
|
+
/**
|
|
722
|
+
* The most seconds of waiting in all for one call (default 60): a wait the
|
|
723
|
+
* server asks that would pass it is not waited, and the call rejects at
|
|
724
|
+
* once with QuotaExceededError and its `retryAfter`.
|
|
725
|
+
*/
|
|
726
|
+
maxRetryWait?: number;
|
|
666
727
|
}
|
|
667
728
|
/** Where and how `Korely.initAgent()` signs up: the client options a call
|
|
668
729
|
* without a key can use. */
|
|
@@ -681,6 +742,15 @@ declare class Korely {
|
|
|
681
742
|
readonly client?: string;
|
|
682
743
|
/** Whether a write that got no answer is sent once more with its key. */
|
|
683
744
|
readonly retryWrites: boolean;
|
|
745
|
+
/** How many more times a call answered 429 with a wait is sent (`maxRetries`). */
|
|
746
|
+
readonly maxRetries: number;
|
|
747
|
+
/** The most seconds of waiting in all for one call (`maxRetryWait`). */
|
|
748
|
+
readonly maxRetryWait: number;
|
|
749
|
+
/**
|
|
750
|
+
* How a wait is waited: the one seam the tests replace.
|
|
751
|
+
* @internal
|
|
752
|
+
*/
|
|
753
|
+
static _sleep: (ms: number) => Promise<void>;
|
|
684
754
|
private readonly fetchImpl;
|
|
685
755
|
constructor(opts?: KorelyOptions);
|
|
686
756
|
/**
|
|
@@ -724,9 +794,10 @@ declare class Korely {
|
|
|
724
794
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
725
795
|
* it, with the same shape.
|
|
726
796
|
*
|
|
727
|
-
* The cheapest authenticated call there is: no scope
|
|
728
|
-
*
|
|
729
|
-
*
|
|
797
|
+
* The cheapest authenticated call there is: no scope and no quota, so it is
|
|
798
|
+
* how to check a key that was just minted or rotated without spending any.
|
|
799
|
+
* It does count against the rate limit, as every call does: past it, a 429
|
|
800
|
+
* with `retryAfter`. `users()` was the usual stand-in, and it needs
|
|
730
801
|
* `memories:read` and counts as a query.
|
|
731
802
|
*/
|
|
732
803
|
ping(): Promise<PingResponse>;
|
|
@@ -805,8 +876,8 @@ declare class Korely {
|
|
|
805
876
|
* the rest one place down: the next page began with events already returned.
|
|
806
877
|
* The pin makes the export the trail as it stood when it started.
|
|
807
878
|
*
|
|
808
|
-
* A rate limit
|
|
809
|
-
*
|
|
879
|
+
* A rate limit the client could not wait out (`maxRetries`) rejects mid-way,
|
|
880
|
+
* as any call does. To resume, call again with the same `until` (pin it yourself:
|
|
810
881
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
811
882
|
*/
|
|
812
883
|
iterAudit(opts?: IterAuditOptions): AsyncGenerator<AuditEvent, void, undefined>;
|
|
@@ -859,9 +930,17 @@ declare class Korely {
|
|
|
859
930
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
860
931
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
861
932
|
* audited). The facts its facts had closed are true again: the receipt
|
|
862
|
-
* counts them in `facts_reopened`.
|
|
933
|
+
* counts them in `facts_reopened`.
|
|
934
|
+
*
|
|
935
|
+
* `{ erase: true }` erases it for good instead: the row, its text and the
|
|
936
|
+
* facts it alone stated go from the database; the receipt says `status`
|
|
937
|
+
* "erased", `erasure` "permanent" and counts `facts_deleted`. For every
|
|
938
|
+
* memory of one end user, `deleteAll`. `erase` is `true` or `false` and
|
|
939
|
+
* nothing else.
|
|
863
940
|
*/
|
|
864
|
-
delete(memoryId: string
|
|
941
|
+
delete(memoryId: string, opts?: {
|
|
942
|
+
erase?: boolean;
|
|
943
|
+
}): Promise<DeleteReceipt>;
|
|
865
944
|
/**
|
|
866
945
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
867
946
|
* user (GDPR Art. 17). Physical deletion, not a flag: nothing is readable
|
|
@@ -1068,11 +1147,12 @@ declare class APIError extends KorelyError {
|
|
|
1068
1147
|
/**
|
|
1069
1148
|
* The server's Retry-After header in whole seconds, rounded up; undefined
|
|
1070
1149
|
* when it sent none. Two answers carry it: a rate limit (429
|
|
1071
|
-
* `rate_limit_exceeded`) and the pause of the
|
|
1072
|
-
* `
|
|
1150
|
+
* `rate_limit_exceeded`) and the pause of the calls to a model (503
|
|
1151
|
+
* `service_paused`: a spending stop or a provider's brake), whose
|
|
1152
|
+
* Retry-After is an hour.
|
|
1073
1153
|
*
|
|
1074
1154
|
* On every status since 2026-10-06. Only QuotaExceededError kept it, so the
|
|
1075
|
-
* 503
|
|
1155
|
+
* 503 of a pause lost the one number that says when to come back.
|
|
1076
1156
|
*/
|
|
1077
1157
|
readonly retryAfter?: number;
|
|
1078
1158
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
@@ -1082,8 +1162,9 @@ declare class AuthenticationError extends APIError {
|
|
|
1082
1162
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
1083
1163
|
}
|
|
1084
1164
|
/**
|
|
1085
|
-
* 403: the key lacks a required scope, or the
|
|
1086
|
-
* namespace past the plan's cap
|
|
1165
|
+
* 403: the key lacks a required scope, or the call reaches outside what the
|
|
1166
|
+
* key may touch. A new agent namespace past the plan's cap
|
|
1167
|
+
* (`agent_cap_exceeded`) is a 429, thrown as QuotaExceededError.
|
|
1087
1168
|
*/
|
|
1088
1169
|
declare class NamespaceForbiddenError extends APIError {
|
|
1089
1170
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
@@ -1134,6 +1215,9 @@ declare class StaleWriteError extends ConflictError {
|
|
|
1134
1215
|
* `code === "quota_exceeded"`: the monthly quota is used up, there is nothing
|
|
1135
1216
|
* to wait for this month, and `retryAfter` is undefined.
|
|
1136
1217
|
* `code === "too_many_batches"`: thrown as the subclass TooManyBatchesError.
|
|
1218
|
+
* `code === "agent_cap_exceeded"` (Cloud): the write names a new `agent_id`
|
|
1219
|
+
* past the plan's named agents; waiting does not help: reuse a name
|
|
1220
|
+
* (`listAgents()`) or delete one (`deleteAgent()`).
|
|
1137
1221
|
* `retryAfter` comes from APIError, which every error the server answers with
|
|
1138
1222
|
* carries.
|
|
1139
1223
|
*/
|
|
@@ -1169,4 +1253,4 @@ declare class TooManyBatchesError extends QuotaExceededError {
|
|
|
1169
1253
|
constructor(message?: string, opts?: QuotaErrorOptions);
|
|
1170
1254
|
}
|
|
1171
1255
|
|
|
1172
|
-
export { APIError, type AccountDeleteReceipt, type AddFactTripleOptions, type AddOptions, type AgentDeleteReceipt, type AgentInitResult, type AgentScope, type AgentsPage, type AuditAction, type AuditActor, type AuditEvent, type AuditOptions, type AuditPage, type AuditRead, AuthenticationError, type BatchJob, type BatchMemory, type BulkReceipt, ConflictError, type Context, type DeclareNameOptions, type DeclaredName, type DeleteReceipt, type EventsOptions, type EventsResponse, type Fact, type FactList, type ForgetReceipt, type GetContextOptions, type GetFactsOptions, type GetProfileOptions, type HistoryEvent, type HistoryEventType, type InitAgentOptions, type IterAuditOptions, Korely, KorelyError, type KorelyOptions, type ListAgentsOptions, type ListOptions, type Memory, type MemoryEvent, type MemoryHistory, type MemoryPage, type Message, NamespaceForbiddenError, NotFoundError, type PingResponse, type Profile, QuotaExceededError, type SearchHit, type SearchKind, type SearchOptions, StaleWriteError, TooManyBatchesError, type UpdateOptions, type UserScope, type UsersOptions, type UsersPage, VERSION };
|
|
1256
|
+
export { APIError, type AccountDeleteReceipt, type AddFactTripleOptions, type AddOptions, type AgentDeleteReceipt, type AgentInitResult, type AgentScope, type AgentsPage, type AuditAction, type AuditActor, type AuditEvent, type AuditOptions, type AuditPage, type AuditRead, AuthenticationError, type BatchJob, type BatchMemory, type BulkReceipt, ConflictError, type Context, type ContextChange, type DeclareNameOptions, type DeclaredName, type DeleteReceipt, type EventsOptions, type EventsResponse, type Fact, type FactList, type ForgetReceipt, type GetContextOptions, type GetFactsOptions, type GetProfileOptions, type HistoryEvent, type HistoryEventType, type InitAgentOptions, type IterAuditOptions, Korely, KorelyError, type KorelyOptions, type ListAgentsOptions, type ListOptions, type Memory, type MemoryEvent, type MemoryHistory, type MemoryPage, type Message, NamespaceForbiddenError, NotFoundError, type PingResponse, type Profile, QuotaExceededError, type SearchHit, type SearchKind, type SearchOptions, StaleWriteError, TooManyBatchesError, type UpdateOptions, type UserScope, type UsersOptions, type UsersPage, VERSION };
|
package/dist/index.d.ts
CHANGED
|
@@ -112,10 +112,15 @@ interface MemoryPage extends Iterable<Memory> {
|
|
|
112
112
|
*/
|
|
113
113
|
interface DeleteReceipt {
|
|
114
114
|
id?: string;
|
|
115
|
+
/** "forgotten", or "erased" with `delete(id, { erase: true })`. */
|
|
115
116
|
status?: string;
|
|
116
117
|
facts_invalidated?: number;
|
|
117
118
|
audit_id?: string;
|
|
118
119
|
facts_reopened?: number;
|
|
120
|
+
/** "permanent": in an erasure's receipt only. */
|
|
121
|
+
erasure?: string;
|
|
122
|
+
/** In an erasure's receipt only: the facts only this memory stated, deleted for good. */
|
|
123
|
+
facts_deleted?: number;
|
|
119
124
|
}
|
|
120
125
|
/**
|
|
121
126
|
* What `deleteAll` erased. Every row is physically deleted, and `erasure` is
|
|
@@ -166,6 +171,29 @@ interface Context {
|
|
|
166
171
|
volatile?: string;
|
|
167
172
|
/** SHA-256 (hex) of `stable`: the same value means the same prefix, so a cached system prompt is still valid. */
|
|
168
173
|
stable_hash?: string;
|
|
174
|
+
/**
|
|
175
|
+
* With `reader` only (Korely 1.0.15+): the facts this reader was handed
|
|
176
|
+
* before and that changed since, newest first, at most 3, each told once.
|
|
177
|
+
*/
|
|
178
|
+
changed?: ContextChange[];
|
|
179
|
+
/** With `reader` only: how many more changes wait; null when they could not be read this time. */
|
|
180
|
+
changed_more?: number | null;
|
|
181
|
+
}
|
|
182
|
+
/** A fact a reader was handed and that has ended since (agent notices). */
|
|
183
|
+
interface ContextChange {
|
|
184
|
+
fact_id: string;
|
|
185
|
+
/** It, as the block writes a fact; null when the memory that stated it was forgotten. */
|
|
186
|
+
was: string | null;
|
|
187
|
+
/** What holds now in its place, when this key may read it; null: no longer true. */
|
|
188
|
+
now: {
|
|
189
|
+
fact_id: string;
|
|
190
|
+
text: string;
|
|
191
|
+
} | null;
|
|
192
|
+
ended_at: string | null;
|
|
193
|
+
known_at: string | null;
|
|
194
|
+
origin: "corrected" | "forgotten" | "stated" | "read_from_memory";
|
|
195
|
+
source_memory_id: string | null;
|
|
196
|
+
by_agent: string | null;
|
|
169
197
|
}
|
|
170
198
|
/**
|
|
171
199
|
* One item of `batch()`: the body of a single `add()`. `content` is required,
|
|
@@ -281,7 +309,7 @@ interface AgentsPage extends Iterable<AgentScope> {
|
|
|
281
309
|
cap: number;
|
|
282
310
|
/**
|
|
283
311
|
* Agent slots taken across the whole account, by name; reconciles with the
|
|
284
|
-
* `agent_cap_exceeded`
|
|
312
|
+
* `agent_cap_exceeded` 429. Above `total` when other projects use names this
|
|
285
313
|
* project does not.
|
|
286
314
|
*/
|
|
287
315
|
used: number;
|
|
@@ -572,6 +600,13 @@ interface GetFactsOptions {
|
|
|
572
600
|
offset?: number;
|
|
573
601
|
/** Leave out the work sessions (`kind: "session"`) and what only they stated (Korely Agent 0.1.42). */
|
|
574
602
|
exclude_sessions?: boolean;
|
|
603
|
+
/**
|
|
604
|
+
* Your agent's own name (Korely 1.0.15+, agent notices): the server keeps
|
|
605
|
+
* the ids of the facts this read hands over, and a later
|
|
606
|
+
* `getContext({ reader })` of the same name opens with what changed since in
|
|
607
|
+
* them. It filters nothing.
|
|
608
|
+
*/
|
|
609
|
+
reader?: string;
|
|
575
610
|
}
|
|
576
611
|
interface AddFactTripleOptions {
|
|
577
612
|
/**
|
|
@@ -616,6 +651,18 @@ interface GetContextOptions {
|
|
|
616
651
|
known_as_of?: string;
|
|
617
652
|
/** Leave out the work sessions (`kind: "session"`) and what only they stated (Korely Agent 0.1.42). */
|
|
618
653
|
exclude_sessions?: boolean;
|
|
654
|
+
/**
|
|
655
|
+
* Your agent's own name (Korely 1.0.15+, agent notices): the block opens
|
|
656
|
+
* with what changed since in the facts this reader was handed, and the
|
|
657
|
+
* facts it hands over are kept for the next read. Not authenticated:
|
|
658
|
+
* whoever holds the key can give any name.
|
|
659
|
+
*/
|
|
660
|
+
reader?: string;
|
|
661
|
+
/**
|
|
662
|
+
* `"all"`: with `reader`, in an agent's memory with no end user, also the
|
|
663
|
+
* closures the store read from memories; left out, only the explicit ones.
|
|
664
|
+
*/
|
|
665
|
+
changes?: "all";
|
|
619
666
|
}
|
|
620
667
|
/** What `forgetFact` gives back. `status` is "forgotten" the first time and
|
|
621
668
|
* "already_forgotten" afterwards; `invalid_at` is the date it stopped being
|
|
@@ -629,7 +676,7 @@ interface ForgetReceipt {
|
|
|
629
676
|
|
|
630
677
|
/** Must equal `version` in package.json (a test checks). It goes out in the
|
|
631
678
|
* X-Korely-Client header, and said 0.1.1 from 0.1.1 to 0.1.6. */
|
|
632
|
-
declare const VERSION = "0.
|
|
679
|
+
declare const VERSION = "0.4.0";
|
|
633
680
|
interface KorelyOptions {
|
|
634
681
|
/** Your `kor_live_...` key. Falls back to the KORELY_API_KEY env var. */
|
|
635
682
|
apiKey?: string;
|
|
@@ -663,6 +710,20 @@ interface KorelyOptions {
|
|
|
663
710
|
* the key.
|
|
664
711
|
*/
|
|
665
712
|
retryWrites?: boolean;
|
|
713
|
+
/**
|
|
714
|
+
* A call answered 429 with a wait (Retry-After) is sent again after that
|
|
715
|
+
* wait, at most this many more times. Default 3; 0 sends every call once,
|
|
716
|
+
* as up to 0.3.0. A write keeps its Idempotency-Key, so it is made once. A
|
|
717
|
+
* 429 that names no wait (the month's quota, the agent cap) is never sent
|
|
718
|
+
* again.
|
|
719
|
+
*/
|
|
720
|
+
maxRetries?: number;
|
|
721
|
+
/**
|
|
722
|
+
* The most seconds of waiting in all for one call (default 60): a wait the
|
|
723
|
+
* server asks that would pass it is not waited, and the call rejects at
|
|
724
|
+
* once with QuotaExceededError and its `retryAfter`.
|
|
725
|
+
*/
|
|
726
|
+
maxRetryWait?: number;
|
|
666
727
|
}
|
|
667
728
|
/** Where and how `Korely.initAgent()` signs up: the client options a call
|
|
668
729
|
* without a key can use. */
|
|
@@ -681,6 +742,15 @@ declare class Korely {
|
|
|
681
742
|
readonly client?: string;
|
|
682
743
|
/** Whether a write that got no answer is sent once more with its key. */
|
|
683
744
|
readonly retryWrites: boolean;
|
|
745
|
+
/** How many more times a call answered 429 with a wait is sent (`maxRetries`). */
|
|
746
|
+
readonly maxRetries: number;
|
|
747
|
+
/** The most seconds of waiting in all for one call (`maxRetryWait`). */
|
|
748
|
+
readonly maxRetryWait: number;
|
|
749
|
+
/**
|
|
750
|
+
* How a wait is waited: the one seam the tests replace.
|
|
751
|
+
* @internal
|
|
752
|
+
*/
|
|
753
|
+
static _sleep: (ms: number) => Promise<void>;
|
|
684
754
|
private readonly fetchImpl;
|
|
685
755
|
constructor(opts?: KorelyOptions);
|
|
686
756
|
/**
|
|
@@ -724,9 +794,10 @@ declare class Korely {
|
|
|
724
794
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
725
795
|
* it, with the same shape.
|
|
726
796
|
*
|
|
727
|
-
* The cheapest authenticated call there is: no scope
|
|
728
|
-
*
|
|
729
|
-
*
|
|
797
|
+
* The cheapest authenticated call there is: no scope and no quota, so it is
|
|
798
|
+
* how to check a key that was just minted or rotated without spending any.
|
|
799
|
+
* It does count against the rate limit, as every call does: past it, a 429
|
|
800
|
+
* with `retryAfter`. `users()` was the usual stand-in, and it needs
|
|
730
801
|
* `memories:read` and counts as a query.
|
|
731
802
|
*/
|
|
732
803
|
ping(): Promise<PingResponse>;
|
|
@@ -805,8 +876,8 @@ declare class Korely {
|
|
|
805
876
|
* the rest one place down: the next page began with events already returned.
|
|
806
877
|
* The pin makes the export the trail as it stood when it started.
|
|
807
878
|
*
|
|
808
|
-
* A rate limit
|
|
809
|
-
*
|
|
879
|
+
* A rate limit the client could not wait out (`maxRetries`) rejects mid-way,
|
|
880
|
+
* as any call does. To resume, call again with the same `until` (pin it yourself:
|
|
810
881
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
811
882
|
*/
|
|
812
883
|
iterAudit(opts?: IterAuditOptions): AsyncGenerator<AuditEvent, void, undefined>;
|
|
@@ -859,9 +930,17 @@ declare class Korely {
|
|
|
859
930
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
860
931
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
861
932
|
* audited). The facts its facts had closed are true again: the receipt
|
|
862
|
-
* counts them in `facts_reopened`.
|
|
933
|
+
* counts them in `facts_reopened`.
|
|
934
|
+
*
|
|
935
|
+
* `{ erase: true }` erases it for good instead: the row, its text and the
|
|
936
|
+
* facts it alone stated go from the database; the receipt says `status`
|
|
937
|
+
* "erased", `erasure` "permanent" and counts `facts_deleted`. For every
|
|
938
|
+
* memory of one end user, `deleteAll`. `erase` is `true` or `false` and
|
|
939
|
+
* nothing else.
|
|
863
940
|
*/
|
|
864
|
-
delete(memoryId: string
|
|
941
|
+
delete(memoryId: string, opts?: {
|
|
942
|
+
erase?: boolean;
|
|
943
|
+
}): Promise<DeleteReceipt>;
|
|
865
944
|
/**
|
|
866
945
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
867
946
|
* user (GDPR Art. 17). Physical deletion, not a flag: nothing is readable
|
|
@@ -1068,11 +1147,12 @@ declare class APIError extends KorelyError {
|
|
|
1068
1147
|
/**
|
|
1069
1148
|
* The server's Retry-After header in whole seconds, rounded up; undefined
|
|
1070
1149
|
* when it sent none. Two answers carry it: a rate limit (429
|
|
1071
|
-
* `rate_limit_exceeded`) and the pause of the
|
|
1072
|
-
* `
|
|
1150
|
+
* `rate_limit_exceeded`) and the pause of the calls to a model (503
|
|
1151
|
+
* `service_paused`: a spending stop or a provider's brake), whose
|
|
1152
|
+
* Retry-After is an hour.
|
|
1073
1153
|
*
|
|
1074
1154
|
* On every status since 2026-10-06. Only QuotaExceededError kept it, so the
|
|
1075
|
-
* 503
|
|
1155
|
+
* 503 of a pause lost the one number that says when to come back.
|
|
1076
1156
|
*/
|
|
1077
1157
|
readonly retryAfter?: number;
|
|
1078
1158
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
@@ -1082,8 +1162,9 @@ declare class AuthenticationError extends APIError {
|
|
|
1082
1162
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
1083
1163
|
}
|
|
1084
1164
|
/**
|
|
1085
|
-
* 403: the key lacks a required scope, or the
|
|
1086
|
-
* namespace past the plan's cap
|
|
1165
|
+
* 403: the key lacks a required scope, or the call reaches outside what the
|
|
1166
|
+
* key may touch. A new agent namespace past the plan's cap
|
|
1167
|
+
* (`agent_cap_exceeded`) is a 429, thrown as QuotaExceededError.
|
|
1087
1168
|
*/
|
|
1088
1169
|
declare class NamespaceForbiddenError extends APIError {
|
|
1089
1170
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
@@ -1134,6 +1215,9 @@ declare class StaleWriteError extends ConflictError {
|
|
|
1134
1215
|
* `code === "quota_exceeded"`: the monthly quota is used up, there is nothing
|
|
1135
1216
|
* to wait for this month, and `retryAfter` is undefined.
|
|
1136
1217
|
* `code === "too_many_batches"`: thrown as the subclass TooManyBatchesError.
|
|
1218
|
+
* `code === "agent_cap_exceeded"` (Cloud): the write names a new `agent_id`
|
|
1219
|
+
* past the plan's named agents; waiting does not help: reuse a name
|
|
1220
|
+
* (`listAgents()`) or delete one (`deleteAgent()`).
|
|
1137
1221
|
* `retryAfter` comes from APIError, which every error the server answers with
|
|
1138
1222
|
* carries.
|
|
1139
1223
|
*/
|
|
@@ -1169,4 +1253,4 @@ declare class TooManyBatchesError extends QuotaExceededError {
|
|
|
1169
1253
|
constructor(message?: string, opts?: QuotaErrorOptions);
|
|
1170
1254
|
}
|
|
1171
1255
|
|
|
1172
|
-
export { APIError, type AccountDeleteReceipt, type AddFactTripleOptions, type AddOptions, type AgentDeleteReceipt, type AgentInitResult, type AgentScope, type AgentsPage, type AuditAction, type AuditActor, type AuditEvent, type AuditOptions, type AuditPage, type AuditRead, AuthenticationError, type BatchJob, type BatchMemory, type BulkReceipt, ConflictError, type Context, type DeclareNameOptions, type DeclaredName, type DeleteReceipt, type EventsOptions, type EventsResponse, type Fact, type FactList, type ForgetReceipt, type GetContextOptions, type GetFactsOptions, type GetProfileOptions, type HistoryEvent, type HistoryEventType, type InitAgentOptions, type IterAuditOptions, Korely, KorelyError, type KorelyOptions, type ListAgentsOptions, type ListOptions, type Memory, type MemoryEvent, type MemoryHistory, type MemoryPage, type Message, NamespaceForbiddenError, NotFoundError, type PingResponse, type Profile, QuotaExceededError, type SearchHit, type SearchKind, type SearchOptions, StaleWriteError, TooManyBatchesError, type UpdateOptions, type UserScope, type UsersOptions, type UsersPage, VERSION };
|
|
1256
|
+
export { APIError, type AccountDeleteReceipt, type AddFactTripleOptions, type AddOptions, type AgentDeleteReceipt, type AgentInitResult, type AgentScope, type AgentsPage, type AuditAction, type AuditActor, type AuditEvent, type AuditOptions, type AuditPage, type AuditRead, AuthenticationError, type BatchJob, type BatchMemory, type BulkReceipt, ConflictError, type Context, type ContextChange, type DeclareNameOptions, type DeclaredName, type DeleteReceipt, type EventsOptions, type EventsResponse, type Fact, type FactList, type ForgetReceipt, type GetContextOptions, type GetFactsOptions, type GetProfileOptions, type HistoryEvent, type HistoryEventType, type InitAgentOptions, type IterAuditOptions, Korely, KorelyError, type KorelyOptions, type ListAgentsOptions, type ListOptions, type Memory, type MemoryEvent, type MemoryHistory, type MemoryPage, type Message, NamespaceForbiddenError, NotFoundError, type PingResponse, type Profile, QuotaExceededError, type SearchHit, type SearchKind, type SearchOptions, StaleWriteError, TooManyBatchesError, type UpdateOptions, type UserScope, type UsersOptions, type UsersPage, VERSION };
|
package/dist/index.js
CHANGED
package/dist/index.node.cjs
CHANGED
|
@@ -130,7 +130,7 @@ function unknownOption(method, opts, known) {
|
|
|
130
130
|
var DEFAULT_TOKEN_BUDGET = 1500;
|
|
131
131
|
|
|
132
132
|
// src/client.ts
|
|
133
|
-
var VERSION = "0.
|
|
133
|
+
var VERSION = "0.4.0";
|
|
134
134
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
135
135
|
function regionUrl(region = "eu") {
|
|
136
136
|
const url = typeof region === "string" && Object.prototype.hasOwnProperty.call(REGIONS, region) ? REGIONS[region] : void 0;
|
|
@@ -148,7 +148,9 @@ var CLIENT_KEYS = keysOf()(
|
|
|
148
148
|
"timeoutMs",
|
|
149
149
|
"fetch",
|
|
150
150
|
"client",
|
|
151
|
-
"retryWrites"
|
|
151
|
+
"retryWrites",
|
|
152
|
+
"maxRetries",
|
|
153
|
+
"maxRetryWait"
|
|
152
154
|
);
|
|
153
155
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
154
156
|
var ADD_KEYS = keysOf()(
|
|
@@ -161,6 +163,21 @@ var ADD_KEYS = keysOf()(
|
|
|
161
163
|
"idempotency_key"
|
|
162
164
|
);
|
|
163
165
|
var WRITE_RETRY_DELAY_MS = 500;
|
|
166
|
+
var RATE_LIMIT_RETRIES = 3;
|
|
167
|
+
var RATE_LIMIT_WAIT = 60;
|
|
168
|
+
async function exchangeWaitingOut(t, b, method, path, opts = {}) {
|
|
169
|
+
let waited = 0;
|
|
170
|
+
for (let tries = 0; ; tries++) {
|
|
171
|
+
try {
|
|
172
|
+
return await exchange(t, method, path, opts);
|
|
173
|
+
} catch (e) {
|
|
174
|
+
const wait = e instanceof QuotaExceededError && e.status === 429 ? e.retryAfter : void 0;
|
|
175
|
+
if (wait === void 0 || tries >= b.maxRetries || waited + wait > b.maxRetryWait) throw e;
|
|
176
|
+
await Korely._sleep(wait * 1e3);
|
|
177
|
+
waited += wait;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
164
181
|
function newIdempotencyKey() {
|
|
165
182
|
const c = globalThis.crypto;
|
|
166
183
|
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
@@ -209,7 +226,8 @@ var FACTS_KEYS = keysOf()(
|
|
|
209
226
|
"agent_id",
|
|
210
227
|
"limit",
|
|
211
228
|
"offset",
|
|
212
|
-
"exclude_sessions"
|
|
229
|
+
"exclude_sessions",
|
|
230
|
+
"reader"
|
|
213
231
|
);
|
|
214
232
|
var TRIPLE_KEYS = keysOf()(
|
|
215
233
|
"user_id",
|
|
@@ -234,14 +252,18 @@ var CONTEXT_KEYS = keysOf()(
|
|
|
234
252
|
"agent_id",
|
|
235
253
|
"token_budget",
|
|
236
254
|
"known_as_of",
|
|
237
|
-
"exclude_sessions"
|
|
255
|
+
"exclude_sessions",
|
|
256
|
+
"reader",
|
|
257
|
+
"changes"
|
|
238
258
|
);
|
|
239
259
|
var CONTEXT_EXTRA_KEYS = keysOf()(
|
|
240
260
|
"user_id",
|
|
241
261
|
"agent_id",
|
|
242
262
|
"token_budget",
|
|
243
263
|
"known_as_of",
|
|
244
|
-
"exclude_sessions"
|
|
264
|
+
"exclude_sessions",
|
|
265
|
+
"reader",
|
|
266
|
+
"changes"
|
|
245
267
|
);
|
|
246
268
|
function checkOptions(method, opts, known) {
|
|
247
269
|
const problem = unknownOption(method, opts, known);
|
|
@@ -551,6 +573,13 @@ function text(v) {
|
|
|
551
573
|
return typeof v === "string" && v ? v : void 0;
|
|
552
574
|
}
|
|
553
575
|
var Korely = class {
|
|
576
|
+
static {
|
|
577
|
+
/**
|
|
578
|
+
* How a wait is waited: the one seam the tests replace.
|
|
579
|
+
* @internal
|
|
580
|
+
*/
|
|
581
|
+
this._sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
582
|
+
}
|
|
554
583
|
constructor(opts = {}) {
|
|
555
584
|
checkOptions("Korely", opts, CLIENT_KEYS);
|
|
556
585
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
@@ -568,6 +597,16 @@ var Korely = class {
|
|
|
568
597
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
569
598
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
570
599
|
this.retryWrites = opts.retryWrites ?? true;
|
|
600
|
+
const retries = opts.maxRetries ?? RATE_LIMIT_RETRIES;
|
|
601
|
+
if (!Number.isInteger(retries) || retries < 0) {
|
|
602
|
+
throw new KorelyError(`maxRetries is a whole number, 0 or more, not ${String(opts.maxRetries)}.`);
|
|
603
|
+
}
|
|
604
|
+
const wait = opts.maxRetryWait ?? RATE_LIMIT_WAIT;
|
|
605
|
+
if (typeof wait !== "number" || !(wait >= 0)) {
|
|
606
|
+
throw new KorelyError(`maxRetryWait is seconds, 0 or more, not ${String(opts.maxRetryWait)}.`);
|
|
607
|
+
}
|
|
608
|
+
this.maxRetries = retries;
|
|
609
|
+
this.maxRetryWait = wait;
|
|
571
610
|
const f = opts.fetch ?? globalThis.fetch;
|
|
572
611
|
if (!f) {
|
|
573
612
|
throw new KorelyError(
|
|
@@ -617,8 +656,9 @@ var Korely = class {
|
|
|
617
656
|
"No fetch available. On Node < 18 pass a fetch implementation via { fetch }."
|
|
618
657
|
);
|
|
619
658
|
}
|
|
620
|
-
return
|
|
659
|
+
return exchangeWaitingOut(
|
|
621
660
|
{ baseUrl, timeoutMs: opts.timeoutMs ?? 3e4, fetchImpl },
|
|
661
|
+
{ maxRetries: RATE_LIMIT_RETRIES, maxRetryWait: RATE_LIMIT_WAIT },
|
|
622
662
|
"POST",
|
|
623
663
|
"/v1/agents/init",
|
|
624
664
|
{
|
|
@@ -628,7 +668,7 @@ var Korely = class {
|
|
|
628
668
|
}
|
|
629
669
|
// ── transport ─────────────────────────────────────────────────────────────
|
|
630
670
|
request(method, path, opts = {}) {
|
|
631
|
-
return
|
|
671
|
+
return exchangeWaitingOut(
|
|
632
672
|
{
|
|
633
673
|
baseUrl: this.baseUrl,
|
|
634
674
|
timeoutMs: this.timeoutMs,
|
|
@@ -636,6 +676,7 @@ var Korely = class {
|
|
|
636
676
|
apiKey: this.apiKey,
|
|
637
677
|
client: this.client
|
|
638
678
|
},
|
|
679
|
+
{ maxRetries: this.maxRetries, maxRetryWait: this.maxRetryWait },
|
|
639
680
|
method,
|
|
640
681
|
path,
|
|
641
682
|
opts
|
|
@@ -680,9 +721,10 @@ var Korely = class {
|
|
|
680
721
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
681
722
|
* it, with the same shape.
|
|
682
723
|
*
|
|
683
|
-
* The cheapest authenticated call there is: no scope
|
|
684
|
-
*
|
|
685
|
-
*
|
|
724
|
+
* The cheapest authenticated call there is: no scope and no quota, so it is
|
|
725
|
+
* how to check a key that was just minted or rotated without spending any.
|
|
726
|
+
* It does count against the rate limit, as every call does: past it, a 429
|
|
727
|
+
* with `retryAfter`. `users()` was the usual stand-in, and it needs
|
|
686
728
|
* `memories:read` and counts as a query.
|
|
687
729
|
*/
|
|
688
730
|
async ping() {
|
|
@@ -829,8 +871,8 @@ var Korely = class {
|
|
|
829
871
|
* the rest one place down: the next page began with events already returned.
|
|
830
872
|
* The pin makes the export the trail as it stood when it started.
|
|
831
873
|
*
|
|
832
|
-
* A rate limit
|
|
833
|
-
*
|
|
874
|
+
* A rate limit the client could not wait out (`maxRetries`) rejects mid-way,
|
|
875
|
+
* as any call does. To resume, call again with the same `until` (pin it yourself:
|
|
834
876
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
835
877
|
*/
|
|
836
878
|
async *iterAudit(opts = {}) {
|
|
@@ -978,10 +1020,22 @@ var Korely = class {
|
|
|
978
1020
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
979
1021
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
980
1022
|
* audited). The facts its facts had closed are true again: the receipt
|
|
981
|
-
* counts them in `facts_reopened`.
|
|
1023
|
+
* counts them in `facts_reopened`.
|
|
1024
|
+
*
|
|
1025
|
+
* `{ erase: true }` erases it for good instead: the row, its text and the
|
|
1026
|
+
* facts it alone stated go from the database; the receipt says `status`
|
|
1027
|
+
* "erased", `erasure` "permanent" and counts `facts_deleted`. For every
|
|
1028
|
+
* memory of one end user, `deleteAll`. `erase` is `true` or `false` and
|
|
1029
|
+
* nothing else.
|
|
982
1030
|
*/
|
|
983
|
-
async delete(memoryId) {
|
|
984
|
-
|
|
1031
|
+
async delete(memoryId, opts = {}) {
|
|
1032
|
+
checkOptions("delete", opts, ["erase"]);
|
|
1033
|
+
if (opts.erase !== void 0 && typeof opts.erase !== "boolean") {
|
|
1034
|
+
throw new KorelyError(`delete({ erase }) takes true or false, not ${typeof opts.erase}.`);
|
|
1035
|
+
}
|
|
1036
|
+
return this.request("DELETE", `/v1/memories/${seg(memoryId, "memoryId")}`, {
|
|
1037
|
+
params: opts.erase ? { erase: "true" } : void 0
|
|
1038
|
+
});
|
|
985
1039
|
}
|
|
986
1040
|
/**
|
|
987
1041
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
@@ -1091,7 +1145,8 @@ var Korely = class {
|
|
|
1091
1145
|
user_id: opts.user_id,
|
|
1092
1146
|
agent_id: opts.agent_id,
|
|
1093
1147
|
limit: opts.limit ?? 50,
|
|
1094
|
-
offset: opts.offset ?? 0
|
|
1148
|
+
offset: opts.offset ?? 0,
|
|
1149
|
+
reader: opts.reader
|
|
1095
1150
|
};
|
|
1096
1151
|
if (opts.include_invalidated) params.include_invalidated = "true";
|
|
1097
1152
|
if (opts.exclude_sessions) params.exclude_sessions = "true";
|
|
@@ -1211,7 +1266,9 @@ var Korely = class {
|
|
|
1211
1266
|
agent_id: o.agent_id,
|
|
1212
1267
|
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1213
1268
|
known_as_of: o.known_as_of,
|
|
1214
|
-
exclude_sessions: o.exclude_sessions ? "true" : void 0
|
|
1269
|
+
exclude_sessions: o.exclude_sessions ? "true" : void 0,
|
|
1270
|
+
reader: o.reader,
|
|
1271
|
+
changes: o.changes
|
|
1215
1272
|
}
|
|
1216
1273
|
});
|
|
1217
1274
|
}
|
package/dist/index.node.js
CHANGED