korely-memory 0.1.6 → 0.1.7
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 +36 -27
- package/dist/index.cjs +224 -83
- package/dist/index.d.cts +281 -67
- package/dist/index.d.ts +281 -67
- package/dist/index.js +224 -83
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# korely-memory
|
|
2
2
|
|
|
3
|
-
The JavaScript / TypeScript SDK for [Korely Agents](https://korely.ai/agents)
|
|
4
|
-
memory for AI agents, with
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
The JavaScript / TypeScript SDK for [Korely Agents](https://korely.ai/agents):
|
|
4
|
+
memory for AI agents, with typed bi-temporal facts behind every write. A thin,
|
|
5
|
+
**zero-dependency** client over the Korely REST API (uses the native `fetch`).
|
|
6
|
+
Same package name as the Python twin on
|
|
7
7
|
[PyPI](https://pypi.org/project/korely-memory/).
|
|
8
8
|
|
|
9
9
|
```bash
|
|
@@ -22,7 +22,7 @@ await korely.add("Maria prefers email over Slack, and her renewal is in October.
|
|
|
22
22
|
user_id: "customer-4812",
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
// Later
|
|
25
|
+
// Later, even in a new session, pull a prompt-ready block back
|
|
26
26
|
const ctx = await korely.getContext({
|
|
27
27
|
query: "how does Maria like to be contacted?",
|
|
28
28
|
user_id: "customer-4812",
|
|
@@ -31,8 +31,7 @@ console.log(ctx.context); // drop straight into your system prompt
|
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
That's the whole loop: `add` to remember, `getContext` (or `search`) to recall.
|
|
34
|
-
Behind `add`, Korely extracts typed, bi-temporal facts
|
|
35
|
-
just hand it text.
|
|
34
|
+
Behind `add`, Korely extracts typed, bi-temporal facts; you just hand it text.
|
|
36
35
|
|
|
37
36
|
## Methods
|
|
38
37
|
|
|
@@ -40,22 +39,25 @@ Every method maps to one REST endpoint.
|
|
|
40
39
|
|
|
41
40
|
| Method | Endpoint | |
|
|
42
41
|
|---|---|---|
|
|
43
|
-
| `add(content, opts?)` | `POST /v1/memories` | Write. `content` is a string or a list of chat messages. `opts.timestamp` (ISO) backfills the past
|
|
44
|
-
| `search(query, opts?)` | `POST /v1/memories/search` |
|
|
45
|
-
| `getAll(opts?)` | `GET /v1/memories` | List a scope, newest first. |
|
|
42
|
+
| `add(content, opts?)` | `POST /v1/memories` | Write. `content` is a string or a list of chat messages. `opts.timestamp` (ISO) backfills the past: facts inherit it as `valid_from`. |
|
|
43
|
+
| `search(query, opts?)` | `POST /v1/memories/search` | Semantic search over memories; filter by `user_id`, `agent_id`, `run_id`, `metadata`. |
|
|
44
|
+
| `getAll(opts?)` | `GET /v1/memories` | List a scope, newest first. The page is iterable; `limit` up to 200. |
|
|
46
45
|
| `get(id)` | `GET /v1/memories/:id` | One memory, with its facts. |
|
|
47
46
|
| `update(id, { content })` | `PATCH /v1/memories/:id` | Re-runs extraction. |
|
|
48
47
|
| `delete(id)` | `DELETE /v1/memories/:id` | Forget one (audited). |
|
|
49
|
-
| `deleteAll({ user_id })` | `DELETE /v1/users/:user_id/memories` |
|
|
48
|
+
| `deleteAll({ user_id })` | `DELETE /v1/users/:user_id/memories` | Erase everything for a user (GDPR). The receipt counts `memories_deleted` and `facts_deleted`; `memories_forgotten` and `facts_invalidated` are deprecated aliases. |
|
|
50
49
|
| `history(id)` | `GET /v1/memories/:id/history` | A memory's timeline + the facts it produced. |
|
|
51
50
|
| `users(opts?)` | `GET /v1/users` | Your end users, with counts. |
|
|
52
|
-
| `listAgents(opts?)` | `GET /v1/agents` |
|
|
53
|
-
| `deleteAgent(agentId)` | `DELETE /v1/agents/:agent_id` | Purge an agent namespace
|
|
54
|
-
| `getFacts(opts?)` | `GET /v1/facts` | Typed facts; `as_of` for point-in-time. |
|
|
55
|
-
| `addFactTriple(s, p, o, opts?)` | `POST /v1/facts` | Write a fact directly (bi-temporal). |
|
|
51
|
+
| `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. |
|
|
52
|
+
| `deleteAgent(agentId)` | `DELETE /v1/agents/:agent_id` | Purge an agent namespace of this project. `NotFoundError` for a name the project does not use; `slot_freed` says whether the cap slot is free now. |
|
|
53
|
+
| `getFacts(opts?)` | `GET /v1/facts` | Typed facts; `as_of` for point-in-time. The array also carries `total`. |
|
|
54
|
+
| `addFactTriple(s, p, o, opts?)` | `POST /v1/facts` | Write a fact directly (bi-temporal; `tense: "past"` for one that is over). |
|
|
55
|
+
| `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: []`). |
|
|
56
|
+
| `forgetFact(id, { at? })` | `POST /v1/facts/:id/forget` | Close a fact; it stays in history. |
|
|
56
57
|
| `getProfile({ user_id, as_of? })` | `GET /v1/profile` | The assembled profile of one end user. |
|
|
57
|
-
| `getContext({ query, user_id? })` | `GET /v1/context` | One call
|
|
58
|
-
| `
|
|
58
|
+
| `getContext({ query, user_id? })` | `GET /v1/context` | One call, a prompt-ready context block. |
|
|
59
|
+
| `events(opts?)` | `GET /v1/events` | Which writes are still being extracted; `limit` up to 200. |
|
|
60
|
+
| `batch(memories)` | `POST /v1/batch` | Bulk import, for migrations. Each item takes `timestamp`, as `add()` does. |
|
|
59
61
|
| `batchStatus(jobId)` | `GET /v1/batch/:id` | Poll an import job. |
|
|
60
62
|
|
|
61
63
|
## Bi-temporal facts (the moat)
|
|
@@ -76,17 +78,22 @@ await korely.addFactTriple("Marco", "works_at", "Acme GmbH", {
|
|
|
76
78
|
|
|
77
79
|
## Errors
|
|
78
80
|
|
|
79
|
-
Every error
|
|
81
|
+
Every error the server answers with is an `APIError` carrying the stable `code`
|
|
82
|
+
and the `message` of the REST error envelope (`{code, message}`; a self-hosted
|
|
83
|
+
install that answers FastAPI's `detail` is read the same way). The common
|
|
84
|
+
statuses also have their own subclass.
|
|
85
|
+
Everything subclasses `KorelyError`, which is also what a client-side problem
|
|
86
|
+
throws (no key, a connection error, a timeout).
|
|
80
87
|
|
|
81
88
|
```ts
|
|
82
|
-
import { Korely,
|
|
89
|
+
import { Korely, APIError, QuotaExceededError } from "korely-memory";
|
|
83
90
|
|
|
84
91
|
try {
|
|
85
92
|
await korely.add("...", { user_id: "u" });
|
|
86
93
|
} catch (e) {
|
|
87
|
-
if (e instanceof QuotaExceededError) {
|
|
88
|
-
console.log(`Rate limited
|
|
89
|
-
} else if (e instanceof
|
|
94
|
+
if (e instanceof QuotaExceededError && e.retryAfter !== undefined) {
|
|
95
|
+
console.log(`Rate limited, retry after ${e.retryAfter}s`);
|
|
96
|
+
} else if (e instanceof APIError && e.code === "invalid_key") {
|
|
90
97
|
console.log("Bad or missing API key");
|
|
91
98
|
} else {
|
|
92
99
|
throw e;
|
|
@@ -94,17 +101,19 @@ try {
|
|
|
94
101
|
}
|
|
95
102
|
```
|
|
96
103
|
|
|
97
|
-
`AuthenticationError` (401) · `NamespaceForbiddenError` (403
|
|
98
|
-
|
|
99
|
-
`
|
|
104
|
+
`AuthenticationError` (401) · `NamespaceForbiddenError` (403, e.g.
|
|
105
|
+
`agent_cap_exceeded`) · `NotFoundError` (404) · `StaleWriteError` (409) ·
|
|
106
|
+
`QuotaExceededError` (429: `retryAfter` is set for `rate_limit_exceeded`,
|
|
107
|
+
undefined for the monthly `quota_exceeded`) · `APIError` (the base of all of
|
|
108
|
+
these, and everything else: 422, 503). The SDK does not retry on its own.
|
|
100
109
|
|
|
101
110
|
## Configuration
|
|
102
111
|
|
|
103
112
|
```ts
|
|
104
113
|
new Korely({
|
|
105
114
|
apiKey: "kor_live_...", // or the KORELY_API_KEY env var
|
|
106
|
-
region: "eu", // EU only
|
|
107
|
-
timeoutMs: 30000,
|
|
115
|
+
region: "eu", // EU only: data stored and processed in the EU
|
|
116
|
+
timeoutMs: 30000, // covers the whole request, response body included
|
|
108
117
|
});
|
|
109
118
|
```
|
|
110
119
|
|
package/dist/index.cjs
CHANGED
|
@@ -42,35 +42,42 @@ var KorelyError = class extends Error {
|
|
|
42
42
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
43
43
|
}
|
|
44
44
|
};
|
|
45
|
-
var
|
|
45
|
+
var APIError = class extends KorelyError {
|
|
46
|
+
constructor(message = "", opts = {}) {
|
|
47
|
+
super(message, opts);
|
|
48
|
+
this.name = "APIError";
|
|
49
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var AuthenticationError = class _AuthenticationError extends APIError {
|
|
46
53
|
constructor(message = "", opts = {}) {
|
|
47
54
|
super(message, opts);
|
|
48
55
|
this.name = "AuthenticationError";
|
|
49
56
|
Object.setPrototypeOf(this, _AuthenticationError.prototype);
|
|
50
57
|
}
|
|
51
58
|
};
|
|
52
|
-
var NamespaceForbiddenError = class _NamespaceForbiddenError extends
|
|
59
|
+
var NamespaceForbiddenError = class _NamespaceForbiddenError extends APIError {
|
|
53
60
|
constructor(message = "", opts = {}) {
|
|
54
61
|
super(message, opts);
|
|
55
62
|
this.name = "NamespaceForbiddenError";
|
|
56
63
|
Object.setPrototypeOf(this, _NamespaceForbiddenError.prototype);
|
|
57
64
|
}
|
|
58
65
|
};
|
|
59
|
-
var NotFoundError = class _NotFoundError extends
|
|
66
|
+
var NotFoundError = class _NotFoundError extends APIError {
|
|
60
67
|
constructor(message = "", opts = {}) {
|
|
61
68
|
super(message, opts);
|
|
62
69
|
this.name = "NotFoundError";
|
|
63
70
|
Object.setPrototypeOf(this, _NotFoundError.prototype);
|
|
64
71
|
}
|
|
65
72
|
};
|
|
66
|
-
var StaleWriteError = class _StaleWriteError extends
|
|
73
|
+
var StaleWriteError = class _StaleWriteError extends APIError {
|
|
67
74
|
constructor(message = "", opts = {}) {
|
|
68
75
|
super(message, opts);
|
|
69
76
|
this.name = "StaleWriteError";
|
|
70
77
|
Object.setPrototypeOf(this, _StaleWriteError.prototype);
|
|
71
78
|
}
|
|
72
79
|
};
|
|
73
|
-
var QuotaExceededError = class _QuotaExceededError extends
|
|
80
|
+
var QuotaExceededError = class _QuotaExceededError extends APIError {
|
|
74
81
|
constructor(message = "", opts = {}) {
|
|
75
82
|
super(message, opts);
|
|
76
83
|
this.name = "QuotaExceededError";
|
|
@@ -78,16 +85,9 @@ var QuotaExceededError = class _QuotaExceededError extends KorelyError {
|
|
|
78
85
|
Object.setPrototypeOf(this, _QuotaExceededError.prototype);
|
|
79
86
|
}
|
|
80
87
|
};
|
|
81
|
-
var APIError = class _APIError extends KorelyError {
|
|
82
|
-
constructor(message = "", opts = {}) {
|
|
83
|
-
super(message, opts);
|
|
84
|
-
this.name = "APIError";
|
|
85
|
-
Object.setPrototypeOf(this, _APIError.prototype);
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
88
|
|
|
89
89
|
// src/client.ts
|
|
90
|
-
var VERSION = "0.1.
|
|
90
|
+
var VERSION = "0.1.7";
|
|
91
91
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
92
92
|
function coerceContent(content) {
|
|
93
93
|
if (typeof content === "string") return content;
|
|
@@ -109,6 +109,38 @@ function coerceContent(content) {
|
|
|
109
109
|
}
|
|
110
110
|
return String(content);
|
|
111
111
|
}
|
|
112
|
+
function seg(value, what) {
|
|
113
|
+
const s = value == null ? "" : String(value);
|
|
114
|
+
if (!s) throw new KorelyError(`${what} is empty.`);
|
|
115
|
+
return encodeURIComponent(s);
|
|
116
|
+
}
|
|
117
|
+
function detailLine(detail) {
|
|
118
|
+
if (typeof detail === "string") return detail.trim() ? detail : void 0;
|
|
119
|
+
if (Array.isArray(detail) && detail.length) {
|
|
120
|
+
const parts = detail.slice(0, 3).map((e) => {
|
|
121
|
+
if (!e || typeof e !== "object") return String(e);
|
|
122
|
+
const loc = Array.isArray(e.loc) ? e.loc : [];
|
|
123
|
+
const where = loc.filter((x) => x !== "body").map(String).join(".");
|
|
124
|
+
const msg = String(e.msg ?? "invalid");
|
|
125
|
+
return where ? `${where}: ${msg}` : msg;
|
|
126
|
+
});
|
|
127
|
+
const more = detail.length - 3;
|
|
128
|
+
return parts.join("; ") + (more > 0 ? ` (and ${more} more)` : "");
|
|
129
|
+
}
|
|
130
|
+
return void 0;
|
|
131
|
+
}
|
|
132
|
+
function errorFields(status, body) {
|
|
133
|
+
const text = (v) => typeof v === "string" && v.trim() ? v : void 0;
|
|
134
|
+
const b = body && typeof body === "object" ? body : {};
|
|
135
|
+
const pair = b.detail && typeof b.detail === "object" && !Array.isArray(b.detail) ? b.detail : {};
|
|
136
|
+
const code = text(b.code) ?? text(pair.code);
|
|
137
|
+
const message = text(b.message) ?? text(pair.message) ?? detailLine(b.detail) ?? code ?? `HTTP ${status}`;
|
|
138
|
+
return { code, message };
|
|
139
|
+
}
|
|
140
|
+
function fillPair(obj, current, deprecated) {
|
|
141
|
+
if (obj[current] == null && obj[deprecated] != null) obj[current] = obj[deprecated];
|
|
142
|
+
if (obj[deprecated] == null && obj[current] != null) obj[deprecated] = obj[current];
|
|
143
|
+
}
|
|
112
144
|
function iterableOver(page, key) {
|
|
113
145
|
const out = page;
|
|
114
146
|
Object.defineProperty(out, Symbol.iterator, {
|
|
@@ -184,26 +216,46 @@ var Korely = class {
|
|
|
184
216
|
headers["Content-Type"] = "application/json";
|
|
185
217
|
}
|
|
186
218
|
const controller = new AbortController();
|
|
187
|
-
|
|
219
|
+
let timedOut = false;
|
|
220
|
+
const timer = setTimeout(() => {
|
|
221
|
+
timedOut = true;
|
|
222
|
+
controller.abort();
|
|
223
|
+
}, this.timeoutMs);
|
|
224
|
+
const failed = (e) => new KorelyError(
|
|
225
|
+
timedOut ? `Request timed out after ${this.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`
|
|
226
|
+
);
|
|
188
227
|
let resp;
|
|
228
|
+
let text;
|
|
189
229
|
try {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
230
|
+
try {
|
|
231
|
+
resp = await this.fetchImpl(url, {
|
|
232
|
+
method,
|
|
233
|
+
headers,
|
|
234
|
+
body,
|
|
235
|
+
signal: controller.signal
|
|
236
|
+
});
|
|
237
|
+
} catch (e) {
|
|
238
|
+
throw failed(e);
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
text = await resp.text();
|
|
242
|
+
} catch (e) {
|
|
243
|
+
throw failed(e);
|
|
244
|
+
}
|
|
198
245
|
} finally {
|
|
199
246
|
clearTimeout(timer);
|
|
200
247
|
}
|
|
201
|
-
const text = await resp.text();
|
|
202
248
|
let parsed = {};
|
|
203
249
|
if (text) {
|
|
204
250
|
try {
|
|
205
251
|
parsed = JSON.parse(text);
|
|
206
252
|
} catch {
|
|
253
|
+
if (resp.ok) {
|
|
254
|
+
throw new KorelyError(
|
|
255
|
+
`The server answered ${resp.status} to ${method} ${path} with a body that is not JSON: ${JSON.stringify(text.slice(0, 200))}`,
|
|
256
|
+
{ status: resp.status }
|
|
257
|
+
);
|
|
258
|
+
}
|
|
207
259
|
parsed = { message: text };
|
|
208
260
|
}
|
|
209
261
|
}
|
|
@@ -213,8 +265,7 @@ var Korely = class {
|
|
|
213
265
|
return parsed && typeof parsed === "object" ? parsed : {};
|
|
214
266
|
}
|
|
215
267
|
raise(status, body, retryAfter) {
|
|
216
|
-
const code = body
|
|
217
|
-
const msg = body?.message || code || `HTTP ${status}`;
|
|
268
|
+
const { code, message: msg } = errorFields(status, body);
|
|
218
269
|
if (status === 401) throw new AuthenticationError(msg, { status, code });
|
|
219
270
|
if (status === 403) throw new NamespaceForbiddenError(msg, { status, code });
|
|
220
271
|
if (status === 404) throw new NotFoundError(msg, { status, code });
|
|
@@ -223,8 +274,8 @@ var Korely = class {
|
|
|
223
274
|
const raw = retryAfter ?? body?.retry_after ?? body?._retry_after;
|
|
224
275
|
let ra;
|
|
225
276
|
if (raw != null) {
|
|
226
|
-
const n =
|
|
227
|
-
ra = Number.
|
|
277
|
+
const n = Math.ceil(Number(raw));
|
|
278
|
+
ra = Number.isFinite(n) && n >= 0 ? n : void 0;
|
|
228
279
|
}
|
|
229
280
|
throw new QuotaExceededError(msg, { status, code, retryAfter: ra });
|
|
230
281
|
}
|
|
@@ -232,7 +283,7 @@ var Korely = class {
|
|
|
232
283
|
}
|
|
233
284
|
// ── memories ────────────────────────────────────────────────────────────
|
|
234
285
|
/**
|
|
235
|
-
* POST /v1/memories
|
|
286
|
+
* POST /v1/memories: store a memory; resolves to it with extracted facts.
|
|
236
287
|
* `content` is a string, or a list of chat messages (role/content), joined
|
|
237
288
|
* into one block before sending. Pass `timestamp` (ISO date/datetime) for
|
|
238
289
|
* backfill: facts extracted inherit it as `valid_from`.
|
|
@@ -241,7 +292,7 @@ var Korely = class {
|
|
|
241
292
|
const text = coerceContent(content);
|
|
242
293
|
if (!text.trim()) {
|
|
243
294
|
throw new KorelyError(
|
|
244
|
-
"content is empty
|
|
295
|
+
"content is empty: pass a non-blank string or messages with content."
|
|
245
296
|
);
|
|
246
297
|
}
|
|
247
298
|
return this.request("POST", "/v1/memories", {
|
|
@@ -256,8 +307,10 @@ var Korely = class {
|
|
|
256
307
|
});
|
|
257
308
|
}
|
|
258
309
|
/**
|
|
259
|
-
* POST /v1/memories/search
|
|
260
|
-
*
|
|
310
|
+
* POST /v1/memories/search: semantic search over raw memories, ranked by
|
|
311
|
+
* score (vector similarity to the query). `run_id` scopes to one session,
|
|
312
|
+
* `metadata` filters on what you stored at write time. `limit` defaults to
|
|
313
|
+
* the server default (15), max 50.
|
|
261
314
|
*/
|
|
262
315
|
async search(query, opts = {}) {
|
|
263
316
|
const body = await this.request("POST", "/v1/memories/search", {
|
|
@@ -265,61 +318,86 @@ var Korely = class {
|
|
|
265
318
|
query,
|
|
266
319
|
user_id: opts.user_id,
|
|
267
320
|
agent_id: opts.agent_id,
|
|
321
|
+
run_id: opts.run_id,
|
|
322
|
+
metadata: opts.metadata,
|
|
268
323
|
limit: opts.limit
|
|
269
324
|
}
|
|
270
325
|
});
|
|
271
326
|
return body.results ?? [];
|
|
272
327
|
}
|
|
273
|
-
/**
|
|
328
|
+
/**
|
|
329
|
+
* GET /v1/memories: list a scope, newest first. The page is iterable.
|
|
330
|
+
* `limit` goes up to 200 (the server capped it at 100 before 2026-09-28);
|
|
331
|
+
* `total` and `offset` walk the rest.
|
|
332
|
+
*/
|
|
274
333
|
async getAll(opts = {}) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
334
|
+
const page = await this.request(
|
|
335
|
+
"GET",
|
|
336
|
+
"/v1/memories",
|
|
337
|
+
{
|
|
338
|
+
params: {
|
|
339
|
+
user_id: opts.user_id,
|
|
340
|
+
agent_id: opts.agent_id,
|
|
341
|
+
run_id: opts.run_id,
|
|
342
|
+
limit: opts.limit ?? 50,
|
|
343
|
+
offset: opts.offset ?? 0
|
|
344
|
+
}
|
|
281
345
|
}
|
|
282
|
-
|
|
346
|
+
);
|
|
347
|
+
return iterableOver(page, "memories");
|
|
283
348
|
}
|
|
284
|
-
/** GET /v1/memories/:id
|
|
349
|
+
/** GET /v1/memories/:id: full content, metadata, extracted facts. */
|
|
285
350
|
async get(memoryId) {
|
|
286
|
-
return this.request("GET", `/v1/memories/${memoryId}`);
|
|
351
|
+
return this.request("GET", `/v1/memories/${seg(memoryId, "memoryId")}`);
|
|
287
352
|
}
|
|
288
353
|
/**
|
|
289
|
-
* PATCH /v1/memories/:id
|
|
354
|
+
* PATCH /v1/memories/:id: re-runs extraction. Pass `expected_updated_at`
|
|
290
355
|
* for optimistic concurrency (throws StaleWriteError instead of clobbering).
|
|
291
356
|
*/
|
|
292
357
|
async update(memoryId, opts) {
|
|
293
|
-
return this.request("PATCH", `/v1/memories/${memoryId}`, {
|
|
358
|
+
return this.request("PATCH", `/v1/memories/${seg(memoryId, "memoryId")}`, {
|
|
294
359
|
body: {
|
|
295
360
|
content: opts.content,
|
|
296
361
|
expected_updated_at: opts.expected_updated_at
|
|
297
362
|
}
|
|
298
363
|
});
|
|
299
364
|
}
|
|
300
|
-
/**
|
|
365
|
+
/**
|
|
366
|
+
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
367
|
+
* read, and the facts only it asserted are invalidated (kept as history,
|
|
368
|
+
* audited). For erasure use `deleteAll`.
|
|
369
|
+
*/
|
|
301
370
|
async delete(memoryId) {
|
|
302
|
-
return this.request("DELETE", `/v1/memories/${memoryId}`);
|
|
371
|
+
return this.request("DELETE", `/v1/memories/${seg(memoryId, "memoryId")}`);
|
|
303
372
|
}
|
|
304
373
|
/**
|
|
305
|
-
* DELETE /v1/users/:user_id/memories
|
|
306
|
-
*
|
|
374
|
+
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
375
|
+
* user (GDPR Art. 17). Physical deletion, not a flag: nothing is readable
|
|
376
|
+
* afterwards. The audit row (counts, never content) survives.
|
|
377
|
+
*
|
|
378
|
+
* The receipt counts the rows in `memories_deleted` and `facts_deleted`.
|
|
379
|
+
* `memories_forgotten` and `facts_invalidated` are deprecated aliases with
|
|
380
|
+
* the same numbers; whichever pair the server sends fills both.
|
|
307
381
|
*/
|
|
308
382
|
async deleteAll(opts) {
|
|
309
|
-
|
|
383
|
+
const r = await this.request(
|
|
310
384
|
"DELETE",
|
|
311
|
-
`/v1/users/${opts.user_id}/memories`
|
|
385
|
+
`/v1/users/${seg(opts.user_id, "user_id")}/memories`
|
|
312
386
|
);
|
|
387
|
+
const fields = r;
|
|
388
|
+
fillPair(fields, "memories_deleted", "memories_forgotten");
|
|
389
|
+
fillPair(fields, "facts_deleted", "facts_invalidated");
|
|
390
|
+
return r;
|
|
313
391
|
}
|
|
314
392
|
/**
|
|
315
|
-
* GET /v1/memories/:id/history
|
|
393
|
+
* GET /v1/memories/:id/history: the lifecycle timeline of a memory,
|
|
316
394
|
* created / updated / deleted, plus every typed fact it produced.
|
|
317
395
|
*/
|
|
318
396
|
async history(memoryId) {
|
|
319
|
-
return this.request("GET", `/v1/memories/${memoryId}/history`);
|
|
397
|
+
return this.request("GET", `/v1/memories/${seg(memoryId, "memoryId")}/history`);
|
|
320
398
|
}
|
|
321
399
|
/**
|
|
322
|
-
* GET /v1/users
|
|
400
|
+
* GET /v1/users: the end users you've stored data for (distinct user_id
|
|
323
401
|
* namespaces), each with active memory + fact counts and last-active time.
|
|
324
402
|
*/
|
|
325
403
|
async users(opts = {}) {
|
|
@@ -338,12 +416,17 @@ var Korely = class {
|
|
|
338
416
|
}
|
|
339
417
|
// ── agents ────────────────────────────────────────────────────────────────
|
|
340
418
|
/**
|
|
341
|
-
* GET /v1/agents
|
|
342
|
-
* non-null agent_id values), each with active memory + fact
|
|
343
|
-
* last-active time
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
419
|
+
* GET /v1/agents: the agent namespaces written under in this key's project
|
|
420
|
+
* (the distinct non-null agent_id values), each with active memory + fact
|
|
421
|
+
* counts and last-active time. The antidote to the agent-cap trap: when a
|
|
422
|
+
* write is rejected with `agent_cap_exceeded`, call this to see which
|
|
423
|
+
* namespaces already exist and reuse one instead of minting a new id.
|
|
424
|
+
*
|
|
425
|
+
* `total` counts this project's namespaces (exactly the ones this key can
|
|
426
|
+
* delete); `used` counts the cap slots taken across the whole account, by
|
|
427
|
+
* name, which is what the 403 compares with `cap`. `used` is above `total`
|
|
428
|
+
* when other projects use names this one does not. A self-hosted install
|
|
429
|
+
* sets no cap and answers `cap: 0`.
|
|
347
430
|
*/
|
|
348
431
|
async listAgents(opts = {}) {
|
|
349
432
|
const page = await this.request("GET", "/v1/agents", {
|
|
@@ -355,18 +438,26 @@ var Korely = class {
|
|
|
355
438
|
return iterableOver(page, "agents");
|
|
356
439
|
}
|
|
357
440
|
/**
|
|
358
|
-
* DELETE /v1/agents/:agent_id
|
|
359
|
-
* memory + fact written under this agent_id
|
|
360
|
-
* (soft-forgetting its data does NOT free
|
|
361
|
-
* counts
|
|
441
|
+
* DELETE /v1/agents/:agent_id: hard-delete an agent namespace, purging every
|
|
442
|
+
* memory + fact written under this agent_id in this key's project
|
|
443
|
+
* (soft-forgetting its data does NOT free its cap slot; this does). Resolves
|
|
444
|
+
* to the purge counts, an audit id and `slot_freed`.
|
|
445
|
+
*
|
|
446
|
+
* The namespace must be one `listAgents()` shows for this key: anything else
|
|
447
|
+
* rejects with NotFoundError, including a name only another project of the
|
|
448
|
+
* account uses (the server answered 200 with zero counts for it before
|
|
449
|
+
* 2026-09-28, and freed nothing). The cap counts a name across the account,
|
|
450
|
+
* so while another project still uses the same agent_id its rows stay and
|
|
451
|
+
* `slot_freed` is false.
|
|
362
452
|
*/
|
|
363
453
|
async deleteAgent(agentId) {
|
|
364
|
-
return this.request("DELETE", `/v1/agents/${agentId}`);
|
|
454
|
+
return this.request("DELETE", `/v1/agents/${seg(agentId, "agentId")}`);
|
|
365
455
|
}
|
|
366
456
|
// ── facts ─────────────────────────────────────────────────────────────────
|
|
367
457
|
/**
|
|
368
|
-
* GET /v1/facts
|
|
369
|
-
* validity. Pass `as_of` (ISO date) for a point-in-time query.
|
|
458
|
+
* GET /v1/facts: typed (subject, predicate, object) triples with bi-temporal
|
|
459
|
+
* validity. Pass `as_of` (ISO date) for a point-in-time query. Resolves to an
|
|
460
|
+
* array that also carries `total`, the matches across all pages.
|
|
370
461
|
*/
|
|
371
462
|
async getFacts(opts = {}) {
|
|
372
463
|
const params = {
|
|
@@ -382,13 +473,19 @@ var Korely = class {
|
|
|
382
473
|
};
|
|
383
474
|
if (opts.include_invalidated) params.include_invalidated = "true";
|
|
384
475
|
const body = await this.request("GET", "/v1/facts", { params });
|
|
385
|
-
|
|
476
|
+
const facts = [...body.facts ?? []];
|
|
477
|
+
Object.defineProperty(facts, "total", {
|
|
478
|
+
value: typeof body.total === "number" ? body.total : facts.length,
|
|
479
|
+
enumerable: false
|
|
480
|
+
});
|
|
481
|
+
return facts;
|
|
386
482
|
}
|
|
387
483
|
/**
|
|
388
|
-
* POST /v1/facts
|
|
484
|
+
* POST /v1/facts: write a typed (subject, predicate, object) triple directly,
|
|
389
485
|
* skipping extraction. The server runs the contradiction check; the fact is
|
|
390
|
-
* bi-temporal (pass `valid_from` for a historical fact
|
|
391
|
-
* written Fact, with `invalidated`
|
|
486
|
+
* bi-temporal (pass `valid_from` for a historical fact, `tense: "past"` for
|
|
487
|
+
* one that is over). Resolves to the written Fact, with `invalidated`
|
|
488
|
+
* listing any fact ids it superseded.
|
|
392
489
|
*/
|
|
393
490
|
async addFactTriple(subject, predicate, object, opts = {}) {
|
|
394
491
|
return this.request("POST", "/v1/facts", {
|
|
@@ -402,17 +499,13 @@ var Korely = class {
|
|
|
402
499
|
subject_type: opts.subject_type ?? "unknown",
|
|
403
500
|
object_is_literal: opts.object_is_literal ?? false,
|
|
404
501
|
confidence: opts.confidence ?? 0.9,
|
|
405
|
-
valid_from: opts.valid_from
|
|
502
|
+
valid_from: opts.valid_from,
|
|
503
|
+
tense: opts.tense
|
|
406
504
|
}
|
|
407
505
|
});
|
|
408
506
|
}
|
|
409
507
|
/**
|
|
410
|
-
*
|
|
411
|
-
* facts known about them, the end user's own facts first, grouped by family.
|
|
412
|
-
* Pass `as_of` (ISO date) for the point-in-time profile.
|
|
413
|
-
*/
|
|
414
|
-
/**
|
|
415
|
-
* POST /v1/facts/:id/forget — close a fact: it stops being current and stays
|
|
508
|
+
* POST /v1/facts/:id/forget: close a fact; it stops being current and stays
|
|
416
509
|
* in history.
|
|
417
510
|
*
|
|
418
511
|
* `at` is the date it STOPPED being true, not the date you noticed. Reading
|
|
@@ -426,22 +519,34 @@ var Korely = class {
|
|
|
426
519
|
* knows a fact is finished says so, and nothing has to infer it.
|
|
427
520
|
*/
|
|
428
521
|
async forgetFact(factId, opts = {}) {
|
|
429
|
-
return this.request("POST", `/v1/facts/${
|
|
522
|
+
return this.request("POST", `/v1/facts/${seg(factId, "factId")}/forget`, {
|
|
430
523
|
body: { at: opts.at }
|
|
431
524
|
});
|
|
432
525
|
}
|
|
433
526
|
/**
|
|
434
|
-
* PATCH /v1/facts/:id
|
|
527
|
+
* PATCH /v1/facts/:id: supersede a fact with a corrected one.
|
|
435
528
|
*
|
|
436
529
|
* Not an edit: the old row keeps its dates and gains a pointer to the new
|
|
437
530
|
* one, so `as_of` before the correction still returns what you believed then.
|
|
438
531
|
* At least one of the three fields is required.
|
|
532
|
+
*
|
|
533
|
+
* Resolves to the new Fact in the write shape. Its `invalidated` lists every
|
|
534
|
+
* fact the correction superseded: the corrected one first, then any other
|
|
535
|
+
* that the contradiction check on the new fact closed. It is not always one
|
|
536
|
+
* id. A correction that names the fact as it already stands (same subject,
|
|
537
|
+
* predicate and object, still open) supersedes nothing: the server
|
|
538
|
+
* reconfirms the fact and returns it, same `id`, with `invalidated: []`.
|
|
439
539
|
*/
|
|
440
540
|
async correctFact(factId, changes) {
|
|
441
|
-
return this.request("PATCH", `/v1/facts/${
|
|
541
|
+
return this.request("PATCH", `/v1/facts/${seg(factId, "factId")}`, {
|
|
442
542
|
body: changes
|
|
443
543
|
});
|
|
444
544
|
}
|
|
545
|
+
/**
|
|
546
|
+
* GET /v1/profile: the assembled profile of one end user, the active typed
|
|
547
|
+
* facts known about them, the end user's own facts first, grouped by family.
|
|
548
|
+
* Pass `as_of` (ISO date) for the point-in-time profile.
|
|
549
|
+
*/
|
|
445
550
|
async getProfile(opts) {
|
|
446
551
|
return this.request("GET", "/v1/profile", {
|
|
447
552
|
params: {
|
|
@@ -453,7 +558,7 @@ var Korely = class {
|
|
|
453
558
|
}
|
|
454
559
|
// ── context ─────────────────────────────────────────────────────────────
|
|
455
560
|
/**
|
|
456
|
-
* GET /v1/context
|
|
561
|
+
* GET /v1/context: one call that assembles a prompt-ready context block
|
|
457
562
|
* (profile + relevant facts + memories) within a token budget.
|
|
458
563
|
*/
|
|
459
564
|
async getContext(opts) {
|
|
@@ -466,14 +571,50 @@ var Korely = class {
|
|
|
466
571
|
}
|
|
467
572
|
});
|
|
468
573
|
}
|
|
574
|
+
// ── processing state ───────────────────────────────────────────────────────
|
|
575
|
+
/**
|
|
576
|
+
* GET /v1/events: which writes have finished being processed.
|
|
577
|
+
*
|
|
578
|
+
* `add()` resolves as soon as the memory is stored; fact extraction runs
|
|
579
|
+
* behind it. Each event carries a memory's `status` ("processing", "ready"
|
|
580
|
+
* or "error"), newest first. `status` filters before `limit` (up to 200) is
|
|
581
|
+
* applied, so `{ status: "error" }` returns the latest errors however many
|
|
582
|
+
* ready writes came after them. `processing` counts every write of this
|
|
583
|
+
* key's project still in flight (of `user_id`, when given), whatever
|
|
584
|
+
* `status` and `limit` say. No webhook fires when extraction finishes, so
|
|
585
|
+
* this is how to know.
|
|
586
|
+
*/
|
|
587
|
+
async events(opts = {}) {
|
|
588
|
+
return this.request("GET", "/v1/events", {
|
|
589
|
+
params: {
|
|
590
|
+
user_id: opts.user_id,
|
|
591
|
+
status: opts.status,
|
|
592
|
+
limit: opts.limit ?? 50
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
}
|
|
469
596
|
// ── batch ─────────────────────────────────────────────────────────────────
|
|
470
|
-
/**
|
|
597
|
+
/**
|
|
598
|
+
* POST /v1/batch: bulk import, up to 500 memory objects, processed
|
|
599
|
+
* asynchronously. Each object is the body of one `add()` (see
|
|
600
|
+
* `BatchMemory`): `content` and optionally `user_id`, `agent_id`, `run_id`,
|
|
601
|
+
* `metadata` and `timestamp`.
|
|
602
|
+
*
|
|
603
|
+
* `timestamp` means what it means on `add()`: when the events happened.
|
|
604
|
+
* The facts extracted from the item inherit it as `valid_from`, so a
|
|
605
|
+
* migration keeps its real dates; without it an item is dated when it is
|
|
606
|
+
* imported. A value that is not an ISO 8601 date or datetime refuses the
|
|
607
|
+
* whole batch before anything is queued: a 422 (APIError,
|
|
608
|
+
* `code === "invalid_request"`) whose message names the item, e.g.
|
|
609
|
+
* `memories[3].timestamp`. Any key not listed above is refused the same
|
|
610
|
+
* way. Servers older than 2026-09-28 refuse `timestamp` itself.
|
|
611
|
+
*/
|
|
471
612
|
async batch(memories) {
|
|
472
613
|
return this.request("POST", "/v1/batch", { body: { memories } });
|
|
473
614
|
}
|
|
474
|
-
/** GET /v1/batch/:id
|
|
615
|
+
/** GET /v1/batch/:id: poll an import job. */
|
|
475
616
|
async batchStatus(jobId) {
|
|
476
|
-
return this.request("GET", `/v1/batch/${jobId}`);
|
|
617
|
+
return this.request("GET", `/v1/batch/${seg(jobId, "jobId")}`);
|
|
477
618
|
}
|
|
478
619
|
};
|
|
479
620
|
// Annotate the CommonJS export names for ESM import in node:
|