korely-memory 0.2.2 → 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 +26 -10
- package/dist/{chunk-UW36NLA6.js → chunk-TX5ICJAX.js} +87 -19
- package/dist/index.cjs +87 -19
- package/dist/index.d.cts +101 -16
- package/dist/index.d.ts +101 -16
- package/dist/index.js +1 -1
- package/dist/index.node.cjs +87 -19
- 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
|
|
|
@@ -204,6 +217,9 @@ The data is stored in the EU (Helsinki). Which model reads a memory, and
|
|
|
204
217
|
where, is the project's region, chosen per project in the dashboard: Europe
|
|
205
218
|
(gpt-oss-120b on Scaleway, in Paris) or Global (Gemini, by Google), the
|
|
206
219
|
default. A memory written to a Global project is read by Google's model.
|
|
220
|
+
`region` names the API, and `"eu"` is the only one: any other value, `"global"`
|
|
221
|
+
among them, throws a `KorelyError`, even with `baseUrl` set (since 0.3.0; up to 0.2.2
|
|
222
|
+
it fell back to `"eu"` in silence).
|
|
207
223
|
|
|
208
224
|
In Node, `new Korely()` with no key and no `KORELY_API_KEY` reads what
|
|
209
225
|
`korely init` saved (the key and the server, in `~/.korely/config.json`, or
|
|
@@ -78,8 +78,17 @@ 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
|
+
function regionUrl(region = "eu") {
|
|
84
|
+
const url = typeof region === "string" && Object.prototype.hasOwnProperty.call(REGIONS, region) ? REGIONS[region] : void 0;
|
|
85
|
+
if (url === void 0) {
|
|
86
|
+
throw new KorelyError(
|
|
87
|
+
`region is "eu", the only API today, not ${JSON.stringify(region)}. Where a model reads the memories is the project's processing region: processingRegion in initAgent, or the project's region in the dashboard.`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return url;
|
|
91
|
+
}
|
|
83
92
|
var CLIENT_KEYS = keysOf()(
|
|
84
93
|
"apiKey",
|
|
85
94
|
"region",
|
|
@@ -87,7 +96,9 @@ var CLIENT_KEYS = keysOf()(
|
|
|
87
96
|
"timeoutMs",
|
|
88
97
|
"fetch",
|
|
89
98
|
"client",
|
|
90
|
-
"retryWrites"
|
|
99
|
+
"retryWrites",
|
|
100
|
+
"maxRetries",
|
|
101
|
+
"maxRetryWait"
|
|
91
102
|
);
|
|
92
103
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
93
104
|
var ADD_KEYS = keysOf()(
|
|
@@ -100,6 +111,21 @@ var ADD_KEYS = keysOf()(
|
|
|
100
111
|
"idempotency_key"
|
|
101
112
|
);
|
|
102
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
|
+
}
|
|
103
129
|
function newIdempotencyKey() {
|
|
104
130
|
const c = globalThis.crypto;
|
|
105
131
|
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
@@ -148,7 +174,8 @@ var FACTS_KEYS = keysOf()(
|
|
|
148
174
|
"agent_id",
|
|
149
175
|
"limit",
|
|
150
176
|
"offset",
|
|
151
|
-
"exclude_sessions"
|
|
177
|
+
"exclude_sessions",
|
|
178
|
+
"reader"
|
|
152
179
|
);
|
|
153
180
|
var TRIPLE_KEYS = keysOf()(
|
|
154
181
|
"user_id",
|
|
@@ -173,14 +200,18 @@ var CONTEXT_KEYS = keysOf()(
|
|
|
173
200
|
"agent_id",
|
|
174
201
|
"token_budget",
|
|
175
202
|
"known_as_of",
|
|
176
|
-
"exclude_sessions"
|
|
203
|
+
"exclude_sessions",
|
|
204
|
+
"reader",
|
|
205
|
+
"changes"
|
|
177
206
|
);
|
|
178
207
|
var CONTEXT_EXTRA_KEYS = keysOf()(
|
|
179
208
|
"user_id",
|
|
180
209
|
"agent_id",
|
|
181
210
|
"token_budget",
|
|
182
211
|
"known_as_of",
|
|
183
|
-
"exclude_sessions"
|
|
212
|
+
"exclude_sessions",
|
|
213
|
+
"reader",
|
|
214
|
+
"changes"
|
|
184
215
|
);
|
|
185
216
|
function checkOptions(method, opts, known) {
|
|
186
217
|
const problem = unknownOption(method, opts, known);
|
|
@@ -490,6 +521,13 @@ function text(v) {
|
|
|
490
521
|
return typeof v === "string" && v ? v : void 0;
|
|
491
522
|
}
|
|
492
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
|
+
}
|
|
493
531
|
constructor(opts = {}) {
|
|
494
532
|
checkOptions("Korely", opts, CLIENT_KEYS);
|
|
495
533
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
@@ -502,10 +540,21 @@ var Korely = class {
|
|
|
502
540
|
this.apiKey = key;
|
|
503
541
|
const envClient = typeof process !== "undefined" ? process.env?.KORELY_CLIENT : void 0;
|
|
504
542
|
this.client = (opts.client ?? envClient ?? "").trim() || void 0;
|
|
505
|
-
|
|
543
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
544
|
+
this.baseUrl = (opts.baseUrl ?? envBase ?? text(saved.base_url) ?? hosted).replace(/\/+$/, "");
|
|
506
545
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
507
546
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
508
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;
|
|
509
558
|
const f = opts.fetch ?? globalThis.fetch;
|
|
510
559
|
if (!f) {
|
|
511
560
|
throw new KorelyError(
|
|
@@ -547,15 +596,17 @@ var Korely = class {
|
|
|
547
596
|
);
|
|
548
597
|
}
|
|
549
598
|
const envBase = typeof process !== "undefined" ? process.env?.KORELY_BASE_URL : void 0;
|
|
550
|
-
const
|
|
599
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
600
|
+
const baseUrl = (opts.baseUrl ?? envBase ?? hosted).replace(/\/+$/, "");
|
|
551
601
|
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
552
602
|
if (!fetchImpl) {
|
|
553
603
|
throw new KorelyError(
|
|
554
604
|
"No fetch available. On Node < 18 pass a fetch implementation via { fetch }."
|
|
555
605
|
);
|
|
556
606
|
}
|
|
557
|
-
return
|
|
607
|
+
return exchangeWaitingOut(
|
|
558
608
|
{ baseUrl, timeoutMs: opts.timeoutMs ?? 3e4, fetchImpl },
|
|
609
|
+
{ maxRetries: RATE_LIMIT_RETRIES, maxRetryWait: RATE_LIMIT_WAIT },
|
|
559
610
|
"POST",
|
|
560
611
|
"/v1/agents/init",
|
|
561
612
|
{
|
|
@@ -565,7 +616,7 @@ var Korely = class {
|
|
|
565
616
|
}
|
|
566
617
|
// ── transport ─────────────────────────────────────────────────────────────
|
|
567
618
|
request(method, path, opts = {}) {
|
|
568
|
-
return
|
|
619
|
+
return exchangeWaitingOut(
|
|
569
620
|
{
|
|
570
621
|
baseUrl: this.baseUrl,
|
|
571
622
|
timeoutMs: this.timeoutMs,
|
|
@@ -573,6 +624,7 @@ var Korely = class {
|
|
|
573
624
|
apiKey: this.apiKey,
|
|
574
625
|
client: this.client
|
|
575
626
|
},
|
|
627
|
+
{ maxRetries: this.maxRetries, maxRetryWait: this.maxRetryWait },
|
|
576
628
|
method,
|
|
577
629
|
path,
|
|
578
630
|
opts
|
|
@@ -617,9 +669,10 @@ var Korely = class {
|
|
|
617
669
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
618
670
|
* it, with the same shape.
|
|
619
671
|
*
|
|
620
|
-
* The cheapest authenticated call there is: no scope
|
|
621
|
-
*
|
|
622
|
-
*
|
|
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
|
|
623
676
|
* `memories:read` and counts as a query.
|
|
624
677
|
*/
|
|
625
678
|
async ping() {
|
|
@@ -766,8 +819,8 @@ var Korely = class {
|
|
|
766
819
|
* the rest one place down: the next page began with events already returned.
|
|
767
820
|
* The pin makes the export the trail as it stood when it started.
|
|
768
821
|
*
|
|
769
|
-
* A rate limit
|
|
770
|
-
*
|
|
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:
|
|
771
824
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
772
825
|
*/
|
|
773
826
|
async *iterAudit(opts = {}) {
|
|
@@ -915,10 +968,22 @@ var Korely = class {
|
|
|
915
968
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
916
969
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
917
970
|
* audited). The facts its facts had closed are true again: the receipt
|
|
918
|
-
* 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.
|
|
919
978
|
*/
|
|
920
|
-
async delete(memoryId) {
|
|
921
|
-
|
|
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
|
+
});
|
|
922
987
|
}
|
|
923
988
|
/**
|
|
924
989
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
@@ -1028,7 +1093,8 @@ var Korely = class {
|
|
|
1028
1093
|
user_id: opts.user_id,
|
|
1029
1094
|
agent_id: opts.agent_id,
|
|
1030
1095
|
limit: opts.limit ?? 50,
|
|
1031
|
-
offset: opts.offset ?? 0
|
|
1096
|
+
offset: opts.offset ?? 0,
|
|
1097
|
+
reader: opts.reader
|
|
1032
1098
|
};
|
|
1033
1099
|
if (opts.include_invalidated) params.include_invalidated = "true";
|
|
1034
1100
|
if (opts.exclude_sessions) params.exclude_sessions = "true";
|
|
@@ -1148,7 +1214,9 @@ var Korely = class {
|
|
|
1148
1214
|
agent_id: o.agent_id,
|
|
1149
1215
|
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1150
1216
|
known_as_of: o.known_as_of,
|
|
1151
|
-
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
|
|
1152
1220
|
}
|
|
1153
1221
|
});
|
|
1154
1222
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -127,8 +127,17 @@ 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
|
+
function regionUrl(region = "eu") {
|
|
133
|
+
const url = typeof region === "string" && Object.prototype.hasOwnProperty.call(REGIONS, region) ? REGIONS[region] : void 0;
|
|
134
|
+
if (url === void 0) {
|
|
135
|
+
throw new KorelyError(
|
|
136
|
+
`region is "eu", the only API today, not ${JSON.stringify(region)}. Where a model reads the memories is the project's processing region: processingRegion in initAgent, or the project's region in the dashboard.`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
return url;
|
|
140
|
+
}
|
|
132
141
|
var CLIENT_KEYS = keysOf()(
|
|
133
142
|
"apiKey",
|
|
134
143
|
"region",
|
|
@@ -136,7 +145,9 @@ var CLIENT_KEYS = keysOf()(
|
|
|
136
145
|
"timeoutMs",
|
|
137
146
|
"fetch",
|
|
138
147
|
"client",
|
|
139
|
-
"retryWrites"
|
|
148
|
+
"retryWrites",
|
|
149
|
+
"maxRetries",
|
|
150
|
+
"maxRetryWait"
|
|
140
151
|
);
|
|
141
152
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
142
153
|
var ADD_KEYS = keysOf()(
|
|
@@ -149,6 +160,21 @@ var ADD_KEYS = keysOf()(
|
|
|
149
160
|
"idempotency_key"
|
|
150
161
|
);
|
|
151
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
|
+
}
|
|
152
178
|
function newIdempotencyKey() {
|
|
153
179
|
const c = globalThis.crypto;
|
|
154
180
|
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
@@ -197,7 +223,8 @@ var FACTS_KEYS = keysOf()(
|
|
|
197
223
|
"agent_id",
|
|
198
224
|
"limit",
|
|
199
225
|
"offset",
|
|
200
|
-
"exclude_sessions"
|
|
226
|
+
"exclude_sessions",
|
|
227
|
+
"reader"
|
|
201
228
|
);
|
|
202
229
|
var TRIPLE_KEYS = keysOf()(
|
|
203
230
|
"user_id",
|
|
@@ -222,14 +249,18 @@ var CONTEXT_KEYS = keysOf()(
|
|
|
222
249
|
"agent_id",
|
|
223
250
|
"token_budget",
|
|
224
251
|
"known_as_of",
|
|
225
|
-
"exclude_sessions"
|
|
252
|
+
"exclude_sessions",
|
|
253
|
+
"reader",
|
|
254
|
+
"changes"
|
|
226
255
|
);
|
|
227
256
|
var CONTEXT_EXTRA_KEYS = keysOf()(
|
|
228
257
|
"user_id",
|
|
229
258
|
"agent_id",
|
|
230
259
|
"token_budget",
|
|
231
260
|
"known_as_of",
|
|
232
|
-
"exclude_sessions"
|
|
261
|
+
"exclude_sessions",
|
|
262
|
+
"reader",
|
|
263
|
+
"changes"
|
|
233
264
|
);
|
|
234
265
|
function checkOptions(method, opts, known) {
|
|
235
266
|
const problem = unknownOption(method, opts, known);
|
|
@@ -536,6 +567,13 @@ function text(v) {
|
|
|
536
567
|
return typeof v === "string" && v ? v : void 0;
|
|
537
568
|
}
|
|
538
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
|
+
}
|
|
539
577
|
constructor(opts = {}) {
|
|
540
578
|
checkOptions("Korely", opts, CLIENT_KEYS);
|
|
541
579
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
@@ -548,10 +586,21 @@ var Korely = class {
|
|
|
548
586
|
this.apiKey = key;
|
|
549
587
|
const envClient = typeof process !== "undefined" ? process.env?.KORELY_CLIENT : void 0;
|
|
550
588
|
this.client = (opts.client ?? envClient ?? "").trim() || void 0;
|
|
551
|
-
|
|
589
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
590
|
+
this.baseUrl = (opts.baseUrl ?? envBase ?? text(saved.base_url) ?? hosted).replace(/\/+$/, "");
|
|
552
591
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
553
592
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
554
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;
|
|
555
604
|
const f = opts.fetch ?? globalThis.fetch;
|
|
556
605
|
if (!f) {
|
|
557
606
|
throw new KorelyError(
|
|
@@ -593,15 +642,17 @@ var Korely = class {
|
|
|
593
642
|
);
|
|
594
643
|
}
|
|
595
644
|
const envBase = typeof process !== "undefined" ? process.env?.KORELY_BASE_URL : void 0;
|
|
596
|
-
const
|
|
645
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
646
|
+
const baseUrl = (opts.baseUrl ?? envBase ?? hosted).replace(/\/+$/, "");
|
|
597
647
|
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
598
648
|
if (!fetchImpl) {
|
|
599
649
|
throw new KorelyError(
|
|
600
650
|
"No fetch available. On Node < 18 pass a fetch implementation via { fetch }."
|
|
601
651
|
);
|
|
602
652
|
}
|
|
603
|
-
return
|
|
653
|
+
return exchangeWaitingOut(
|
|
604
654
|
{ baseUrl, timeoutMs: opts.timeoutMs ?? 3e4, fetchImpl },
|
|
655
|
+
{ maxRetries: RATE_LIMIT_RETRIES, maxRetryWait: RATE_LIMIT_WAIT },
|
|
605
656
|
"POST",
|
|
606
657
|
"/v1/agents/init",
|
|
607
658
|
{
|
|
@@ -611,7 +662,7 @@ var Korely = class {
|
|
|
611
662
|
}
|
|
612
663
|
// ── transport ─────────────────────────────────────────────────────────────
|
|
613
664
|
request(method, path, opts = {}) {
|
|
614
|
-
return
|
|
665
|
+
return exchangeWaitingOut(
|
|
615
666
|
{
|
|
616
667
|
baseUrl: this.baseUrl,
|
|
617
668
|
timeoutMs: this.timeoutMs,
|
|
@@ -619,6 +670,7 @@ var Korely = class {
|
|
|
619
670
|
apiKey: this.apiKey,
|
|
620
671
|
client: this.client
|
|
621
672
|
},
|
|
673
|
+
{ maxRetries: this.maxRetries, maxRetryWait: this.maxRetryWait },
|
|
622
674
|
method,
|
|
623
675
|
path,
|
|
624
676
|
opts
|
|
@@ -663,9 +715,10 @@ var Korely = class {
|
|
|
663
715
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
664
716
|
* it, with the same shape.
|
|
665
717
|
*
|
|
666
|
-
* The cheapest authenticated call there is: no scope
|
|
667
|
-
*
|
|
668
|
-
*
|
|
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
|
|
669
722
|
* `memories:read` and counts as a query.
|
|
670
723
|
*/
|
|
671
724
|
async ping() {
|
|
@@ -812,8 +865,8 @@ var Korely = class {
|
|
|
812
865
|
* the rest one place down: the next page began with events already returned.
|
|
813
866
|
* The pin makes the export the trail as it stood when it started.
|
|
814
867
|
*
|
|
815
|
-
* A rate limit
|
|
816
|
-
*
|
|
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:
|
|
817
870
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
818
871
|
*/
|
|
819
872
|
async *iterAudit(opts = {}) {
|
|
@@ -961,10 +1014,22 @@ var Korely = class {
|
|
|
961
1014
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
962
1015
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
963
1016
|
* audited). The facts its facts had closed are true again: the receipt
|
|
964
|
-
* 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.
|
|
965
1024
|
*/
|
|
966
|
-
async delete(memoryId) {
|
|
967
|
-
|
|
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
|
+
});
|
|
968
1033
|
}
|
|
969
1034
|
/**
|
|
970
1035
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
@@ -1074,7 +1139,8 @@ var Korely = class {
|
|
|
1074
1139
|
user_id: opts.user_id,
|
|
1075
1140
|
agent_id: opts.agent_id,
|
|
1076
1141
|
limit: opts.limit ?? 50,
|
|
1077
|
-
offset: opts.offset ?? 0
|
|
1142
|
+
offset: opts.offset ?? 0,
|
|
1143
|
+
reader: opts.reader
|
|
1078
1144
|
};
|
|
1079
1145
|
if (opts.include_invalidated) params.include_invalidated = "true";
|
|
1080
1146
|
if (opts.exclude_sessions) params.exclude_sessions = "true";
|
|
@@ -1194,7 +1260,9 @@ var Korely = class {
|
|
|
1194
1260
|
agent_id: o.agent_id,
|
|
1195
1261
|
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1196
1262
|
known_as_of: o.known_as_of,
|
|
1197
|
-
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
|
|
1198
1266
|
}
|
|
1199
1267
|
});
|
|
1200
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;
|
|
@@ -637,7 +684,8 @@ interface KorelyOptions {
|
|
|
637
684
|
* The API to call: "eu" (api.korely.ai), the only one today. The data is
|
|
638
685
|
* stored in the EU (Helsinki). The model that reads a memory is the
|
|
639
686
|
* project's region, set per project in the dashboard: Europe (gpt-oss-120b
|
|
640
|
-
* on Scaleway, in Paris) or Global (Gemini, by Google), the default.
|
|
687
|
+
* on Scaleway, in Paris) or Global (Gemini, by Google), the default. Any
|
|
688
|
+
* other value throws a KorelyError.
|
|
641
689
|
*/
|
|
642
690
|
region?: "eu";
|
|
643
691
|
/** Override the base URL (a self-hosted install, or testing). */
|
|
@@ -662,6 +710,20 @@ interface KorelyOptions {
|
|
|
662
710
|
* the key.
|
|
663
711
|
*/
|
|
664
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;
|
|
665
727
|
}
|
|
666
728
|
/** Where and how `Korely.initAgent()` signs up: the client options a call
|
|
667
729
|
* without a key can use. */
|
|
@@ -680,6 +742,15 @@ declare class Korely {
|
|
|
680
742
|
readonly client?: string;
|
|
681
743
|
/** Whether a write that got no answer is sent once more with its key. */
|
|
682
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>;
|
|
683
754
|
private readonly fetchImpl;
|
|
684
755
|
constructor(opts?: KorelyOptions);
|
|
685
756
|
/**
|
|
@@ -723,9 +794,10 @@ declare class Korely {
|
|
|
723
794
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
724
795
|
* it, with the same shape.
|
|
725
796
|
*
|
|
726
|
-
* The cheapest authenticated call there is: no scope
|
|
727
|
-
*
|
|
728
|
-
*
|
|
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
|
|
729
801
|
* `memories:read` and counts as a query.
|
|
730
802
|
*/
|
|
731
803
|
ping(): Promise<PingResponse>;
|
|
@@ -804,8 +876,8 @@ declare class Korely {
|
|
|
804
876
|
* the rest one place down: the next page began with events already returned.
|
|
805
877
|
* The pin makes the export the trail as it stood when it started.
|
|
806
878
|
*
|
|
807
|
-
* A rate limit
|
|
808
|
-
*
|
|
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:
|
|
809
881
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
810
882
|
*/
|
|
811
883
|
iterAudit(opts?: IterAuditOptions): AsyncGenerator<AuditEvent, void, undefined>;
|
|
@@ -858,9 +930,17 @@ declare class Korely {
|
|
|
858
930
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
859
931
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
860
932
|
* audited). The facts its facts had closed are true again: the receipt
|
|
861
|
-
* 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.
|
|
862
940
|
*/
|
|
863
|
-
delete(memoryId: string
|
|
941
|
+
delete(memoryId: string, opts?: {
|
|
942
|
+
erase?: boolean;
|
|
943
|
+
}): Promise<DeleteReceipt>;
|
|
864
944
|
/**
|
|
865
945
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
866
946
|
* user (GDPR Art. 17). Physical deletion, not a flag: nothing is readable
|
|
@@ -1067,11 +1147,12 @@ declare class APIError extends KorelyError {
|
|
|
1067
1147
|
/**
|
|
1068
1148
|
* The server's Retry-After header in whole seconds, rounded up; undefined
|
|
1069
1149
|
* when it sent none. Two answers carry it: a rate limit (429
|
|
1070
|
-
* `rate_limit_exceeded`) and the pause of the
|
|
1071
|
-
* `
|
|
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.
|
|
1072
1153
|
*
|
|
1073
1154
|
* On every status since 2026-10-06. Only QuotaExceededError kept it, so the
|
|
1074
|
-
* 503
|
|
1155
|
+
* 503 of a pause lost the one number that says when to come back.
|
|
1075
1156
|
*/
|
|
1076
1157
|
readonly retryAfter?: number;
|
|
1077
1158
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
@@ -1081,8 +1162,9 @@ declare class AuthenticationError extends APIError {
|
|
|
1081
1162
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
1082
1163
|
}
|
|
1083
1164
|
/**
|
|
1084
|
-
* 403: the key lacks a required scope, or the
|
|
1085
|
-
* 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.
|
|
1086
1168
|
*/
|
|
1087
1169
|
declare class NamespaceForbiddenError extends APIError {
|
|
1088
1170
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
@@ -1133,6 +1215,9 @@ declare class StaleWriteError extends ConflictError {
|
|
|
1133
1215
|
* `code === "quota_exceeded"`: the monthly quota is used up, there is nothing
|
|
1134
1216
|
* to wait for this month, and `retryAfter` is undefined.
|
|
1135
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()`).
|
|
1136
1221
|
* `retryAfter` comes from APIError, which every error the server answers with
|
|
1137
1222
|
* carries.
|
|
1138
1223
|
*/
|
|
@@ -1168,4 +1253,4 @@ declare class TooManyBatchesError extends QuotaExceededError {
|
|
|
1168
1253
|
constructor(message?: string, opts?: QuotaErrorOptions);
|
|
1169
1254
|
}
|
|
1170
1255
|
|
|
1171
|
-
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;
|
|
@@ -637,7 +684,8 @@ interface KorelyOptions {
|
|
|
637
684
|
* The API to call: "eu" (api.korely.ai), the only one today. The data is
|
|
638
685
|
* stored in the EU (Helsinki). The model that reads a memory is the
|
|
639
686
|
* project's region, set per project in the dashboard: Europe (gpt-oss-120b
|
|
640
|
-
* on Scaleway, in Paris) or Global (Gemini, by Google), the default.
|
|
687
|
+
* on Scaleway, in Paris) or Global (Gemini, by Google), the default. Any
|
|
688
|
+
* other value throws a KorelyError.
|
|
641
689
|
*/
|
|
642
690
|
region?: "eu";
|
|
643
691
|
/** Override the base URL (a self-hosted install, or testing). */
|
|
@@ -662,6 +710,20 @@ interface KorelyOptions {
|
|
|
662
710
|
* the key.
|
|
663
711
|
*/
|
|
664
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;
|
|
665
727
|
}
|
|
666
728
|
/** Where and how `Korely.initAgent()` signs up: the client options a call
|
|
667
729
|
* without a key can use. */
|
|
@@ -680,6 +742,15 @@ declare class Korely {
|
|
|
680
742
|
readonly client?: string;
|
|
681
743
|
/** Whether a write that got no answer is sent once more with its key. */
|
|
682
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>;
|
|
683
754
|
private readonly fetchImpl;
|
|
684
755
|
constructor(opts?: KorelyOptions);
|
|
685
756
|
/**
|
|
@@ -723,9 +794,10 @@ declare class Korely {
|
|
|
723
794
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
724
795
|
* it, with the same shape.
|
|
725
796
|
*
|
|
726
|
-
* The cheapest authenticated call there is: no scope
|
|
727
|
-
*
|
|
728
|
-
*
|
|
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
|
|
729
801
|
* `memories:read` and counts as a query.
|
|
730
802
|
*/
|
|
731
803
|
ping(): Promise<PingResponse>;
|
|
@@ -804,8 +876,8 @@ declare class Korely {
|
|
|
804
876
|
* the rest one place down: the next page began with events already returned.
|
|
805
877
|
* The pin makes the export the trail as it stood when it started.
|
|
806
878
|
*
|
|
807
|
-
* A rate limit
|
|
808
|
-
*
|
|
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:
|
|
809
881
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
810
882
|
*/
|
|
811
883
|
iterAudit(opts?: IterAuditOptions): AsyncGenerator<AuditEvent, void, undefined>;
|
|
@@ -858,9 +930,17 @@ declare class Korely {
|
|
|
858
930
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
859
931
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
860
932
|
* audited). The facts its facts had closed are true again: the receipt
|
|
861
|
-
* 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.
|
|
862
940
|
*/
|
|
863
|
-
delete(memoryId: string
|
|
941
|
+
delete(memoryId: string, opts?: {
|
|
942
|
+
erase?: boolean;
|
|
943
|
+
}): Promise<DeleteReceipt>;
|
|
864
944
|
/**
|
|
865
945
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
866
946
|
* user (GDPR Art. 17). Physical deletion, not a flag: nothing is readable
|
|
@@ -1067,11 +1147,12 @@ declare class APIError extends KorelyError {
|
|
|
1067
1147
|
/**
|
|
1068
1148
|
* The server's Retry-After header in whole seconds, rounded up; undefined
|
|
1069
1149
|
* when it sent none. Two answers carry it: a rate limit (429
|
|
1070
|
-
* `rate_limit_exceeded`) and the pause of the
|
|
1071
|
-
* `
|
|
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.
|
|
1072
1153
|
*
|
|
1073
1154
|
* On every status since 2026-10-06. Only QuotaExceededError kept it, so the
|
|
1074
|
-
* 503
|
|
1155
|
+
* 503 of a pause lost the one number that says when to come back.
|
|
1075
1156
|
*/
|
|
1076
1157
|
readonly retryAfter?: number;
|
|
1077
1158
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
@@ -1081,8 +1162,9 @@ declare class AuthenticationError extends APIError {
|
|
|
1081
1162
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
1082
1163
|
}
|
|
1083
1164
|
/**
|
|
1084
|
-
* 403: the key lacks a required scope, or the
|
|
1085
|
-
* 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.
|
|
1086
1168
|
*/
|
|
1087
1169
|
declare class NamespaceForbiddenError extends APIError {
|
|
1088
1170
|
constructor(message?: string, opts?: APIErrorOptions);
|
|
@@ -1133,6 +1215,9 @@ declare class StaleWriteError extends ConflictError {
|
|
|
1133
1215
|
* `code === "quota_exceeded"`: the monthly quota is used up, there is nothing
|
|
1134
1216
|
* to wait for this month, and `retryAfter` is undefined.
|
|
1135
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()`).
|
|
1136
1221
|
* `retryAfter` comes from APIError, which every error the server answers with
|
|
1137
1222
|
* carries.
|
|
1138
1223
|
*/
|
|
@@ -1168,4 +1253,4 @@ declare class TooManyBatchesError extends QuotaExceededError {
|
|
|
1168
1253
|
constructor(message?: string, opts?: QuotaErrorOptions);
|
|
1169
1254
|
}
|
|
1170
1255
|
|
|
1171
|
-
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,8 +130,17 @@ 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
|
+
function regionUrl(region = "eu") {
|
|
136
|
+
const url = typeof region === "string" && Object.prototype.hasOwnProperty.call(REGIONS, region) ? REGIONS[region] : void 0;
|
|
137
|
+
if (url === void 0) {
|
|
138
|
+
throw new KorelyError(
|
|
139
|
+
`region is "eu", the only API today, not ${JSON.stringify(region)}. Where a model reads the memories is the project's processing region: processingRegion in initAgent, or the project's region in the dashboard.`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
return url;
|
|
143
|
+
}
|
|
135
144
|
var CLIENT_KEYS = keysOf()(
|
|
136
145
|
"apiKey",
|
|
137
146
|
"region",
|
|
@@ -139,7 +148,9 @@ var CLIENT_KEYS = keysOf()(
|
|
|
139
148
|
"timeoutMs",
|
|
140
149
|
"fetch",
|
|
141
150
|
"client",
|
|
142
|
-
"retryWrites"
|
|
151
|
+
"retryWrites",
|
|
152
|
+
"maxRetries",
|
|
153
|
+
"maxRetryWait"
|
|
143
154
|
);
|
|
144
155
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
145
156
|
var ADD_KEYS = keysOf()(
|
|
@@ -152,6 +163,21 @@ var ADD_KEYS = keysOf()(
|
|
|
152
163
|
"idempotency_key"
|
|
153
164
|
);
|
|
154
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
|
+
}
|
|
155
181
|
function newIdempotencyKey() {
|
|
156
182
|
const c = globalThis.crypto;
|
|
157
183
|
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
@@ -200,7 +226,8 @@ var FACTS_KEYS = keysOf()(
|
|
|
200
226
|
"agent_id",
|
|
201
227
|
"limit",
|
|
202
228
|
"offset",
|
|
203
|
-
"exclude_sessions"
|
|
229
|
+
"exclude_sessions",
|
|
230
|
+
"reader"
|
|
204
231
|
);
|
|
205
232
|
var TRIPLE_KEYS = keysOf()(
|
|
206
233
|
"user_id",
|
|
@@ -225,14 +252,18 @@ var CONTEXT_KEYS = keysOf()(
|
|
|
225
252
|
"agent_id",
|
|
226
253
|
"token_budget",
|
|
227
254
|
"known_as_of",
|
|
228
|
-
"exclude_sessions"
|
|
255
|
+
"exclude_sessions",
|
|
256
|
+
"reader",
|
|
257
|
+
"changes"
|
|
229
258
|
);
|
|
230
259
|
var CONTEXT_EXTRA_KEYS = keysOf()(
|
|
231
260
|
"user_id",
|
|
232
261
|
"agent_id",
|
|
233
262
|
"token_budget",
|
|
234
263
|
"known_as_of",
|
|
235
|
-
"exclude_sessions"
|
|
264
|
+
"exclude_sessions",
|
|
265
|
+
"reader",
|
|
266
|
+
"changes"
|
|
236
267
|
);
|
|
237
268
|
function checkOptions(method, opts, known) {
|
|
238
269
|
const problem = unknownOption(method, opts, known);
|
|
@@ -542,6 +573,13 @@ function text(v) {
|
|
|
542
573
|
return typeof v === "string" && v ? v : void 0;
|
|
543
574
|
}
|
|
544
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
|
+
}
|
|
545
583
|
constructor(opts = {}) {
|
|
546
584
|
checkOptions("Korely", opts, CLIENT_KEYS);
|
|
547
585
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
@@ -554,10 +592,21 @@ var Korely = class {
|
|
|
554
592
|
this.apiKey = key;
|
|
555
593
|
const envClient = typeof process !== "undefined" ? process.env?.KORELY_CLIENT : void 0;
|
|
556
594
|
this.client = (opts.client ?? envClient ?? "").trim() || void 0;
|
|
557
|
-
|
|
595
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
596
|
+
this.baseUrl = (opts.baseUrl ?? envBase ?? text(saved.base_url) ?? hosted).replace(/\/+$/, "");
|
|
558
597
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
559
598
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
560
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;
|
|
561
610
|
const f = opts.fetch ?? globalThis.fetch;
|
|
562
611
|
if (!f) {
|
|
563
612
|
throw new KorelyError(
|
|
@@ -599,15 +648,17 @@ var Korely = class {
|
|
|
599
648
|
);
|
|
600
649
|
}
|
|
601
650
|
const envBase = typeof process !== "undefined" ? process.env?.KORELY_BASE_URL : void 0;
|
|
602
|
-
const
|
|
651
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
652
|
+
const baseUrl = (opts.baseUrl ?? envBase ?? hosted).replace(/\/+$/, "");
|
|
603
653
|
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
604
654
|
if (!fetchImpl) {
|
|
605
655
|
throw new KorelyError(
|
|
606
656
|
"No fetch available. On Node < 18 pass a fetch implementation via { fetch }."
|
|
607
657
|
);
|
|
608
658
|
}
|
|
609
|
-
return
|
|
659
|
+
return exchangeWaitingOut(
|
|
610
660
|
{ baseUrl, timeoutMs: opts.timeoutMs ?? 3e4, fetchImpl },
|
|
661
|
+
{ maxRetries: RATE_LIMIT_RETRIES, maxRetryWait: RATE_LIMIT_WAIT },
|
|
611
662
|
"POST",
|
|
612
663
|
"/v1/agents/init",
|
|
613
664
|
{
|
|
@@ -617,7 +668,7 @@ var Korely = class {
|
|
|
617
668
|
}
|
|
618
669
|
// ── transport ─────────────────────────────────────────────────────────────
|
|
619
670
|
request(method, path, opts = {}) {
|
|
620
|
-
return
|
|
671
|
+
return exchangeWaitingOut(
|
|
621
672
|
{
|
|
622
673
|
baseUrl: this.baseUrl,
|
|
623
674
|
timeoutMs: this.timeoutMs,
|
|
@@ -625,6 +676,7 @@ var Korely = class {
|
|
|
625
676
|
apiKey: this.apiKey,
|
|
626
677
|
client: this.client
|
|
627
678
|
},
|
|
679
|
+
{ maxRetries: this.maxRetries, maxRetryWait: this.maxRetryWait },
|
|
628
680
|
method,
|
|
629
681
|
path,
|
|
630
682
|
opts
|
|
@@ -669,9 +721,10 @@ var Korely = class {
|
|
|
669
721
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
670
722
|
* it, with the same shape.
|
|
671
723
|
*
|
|
672
|
-
* The cheapest authenticated call there is: no scope
|
|
673
|
-
*
|
|
674
|
-
*
|
|
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
|
|
675
728
|
* `memories:read` and counts as a query.
|
|
676
729
|
*/
|
|
677
730
|
async ping() {
|
|
@@ -818,8 +871,8 @@ var Korely = class {
|
|
|
818
871
|
* the rest one place down: the next page began with events already returned.
|
|
819
872
|
* The pin makes the export the trail as it stood when it started.
|
|
820
873
|
*
|
|
821
|
-
* A rate limit
|
|
822
|
-
*
|
|
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:
|
|
823
876
|
* `until: new Date()`) and `offset` the number of events already read.
|
|
824
877
|
*/
|
|
825
878
|
async *iterAudit(opts = {}) {
|
|
@@ -967,10 +1020,22 @@ var Korely = class {
|
|
|
967
1020
|
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
968
1021
|
* read, and the facts only it asserted are invalidated (kept as history,
|
|
969
1022
|
* audited). The facts its facts had closed are true again: the receipt
|
|
970
|
-
* 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.
|
|
971
1030
|
*/
|
|
972
|
-
async delete(memoryId) {
|
|
973
|
-
|
|
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
|
+
});
|
|
974
1039
|
}
|
|
975
1040
|
/**
|
|
976
1041
|
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
@@ -1080,7 +1145,8 @@ var Korely = class {
|
|
|
1080
1145
|
user_id: opts.user_id,
|
|
1081
1146
|
agent_id: opts.agent_id,
|
|
1082
1147
|
limit: opts.limit ?? 50,
|
|
1083
|
-
offset: opts.offset ?? 0
|
|
1148
|
+
offset: opts.offset ?? 0,
|
|
1149
|
+
reader: opts.reader
|
|
1084
1150
|
};
|
|
1085
1151
|
if (opts.include_invalidated) params.include_invalidated = "true";
|
|
1086
1152
|
if (opts.exclude_sessions) params.exclude_sessions = "true";
|
|
@@ -1200,7 +1266,9 @@ var Korely = class {
|
|
|
1200
1266
|
agent_id: o.agent_id,
|
|
1201
1267
|
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1202
1268
|
known_as_of: o.known_as_of,
|
|
1203
|
-
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
|
|
1204
1272
|
}
|
|
1205
1273
|
});
|
|
1206
1274
|
}
|
package/dist/index.node.js
CHANGED