korely-memory 0.1.6 → 0.1.8
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 +115 -27
- package/dist/ai-sdk.cjs +361 -0
- package/dist/ai-sdk.d.cts +156 -0
- package/dist/ai-sdk.d.ts +156 -0
- package/dist/ai-sdk.js +334 -0
- package/dist/index.cjs +240 -90
- package/dist/index.d.cts +302 -68
- package/dist/index.d.ts +302 -68
- package/dist/index.js +237 -87
- package/package.json +26 -3
package/dist/index.cjs
CHANGED
|
@@ -18,8 +18,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
23
|
APIError: () => APIError,
|
|
24
24
|
AuthenticationError: () => AuthenticationError,
|
|
25
25
|
Korely: () => Korely,
|
|
@@ -30,7 +30,7 @@ __export(index_exports, {
|
|
|
30
30
|
StaleWriteError: () => StaleWriteError,
|
|
31
31
|
VERSION: () => VERSION
|
|
32
32
|
});
|
|
33
|
-
module.exports = __toCommonJS(
|
|
33
|
+
module.exports = __toCommonJS(src_exports);
|
|
34
34
|
|
|
35
35
|
// src/errors.ts
|
|
36
36
|
var KorelyError = class extends Error {
|
|
@@ -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.8";
|
|
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, {
|
|
@@ -142,6 +174,11 @@ function refuseAMismatchedPair(apiKey, baseUrl) {
|
|
|
142
174
|
}
|
|
143
175
|
var Korely = class {
|
|
144
176
|
constructor(opts = {}) {
|
|
177
|
+
if (opts && Object.prototype.hasOwnProperty.call(opts, "base_url")) {
|
|
178
|
+
throw new KorelyError(
|
|
179
|
+
"Unknown option base_url: the JavaScript SDK takes baseUrl (new Korely({ baseUrl: 'https://...' })) or the KORELY_BASE_URL env var."
|
|
180
|
+
);
|
|
181
|
+
}
|
|
145
182
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
146
183
|
const key = opts.apiKey ?? envKey;
|
|
147
184
|
if (!key) {
|
|
@@ -184,26 +221,46 @@ var Korely = class {
|
|
|
184
221
|
headers["Content-Type"] = "application/json";
|
|
185
222
|
}
|
|
186
223
|
const controller = new AbortController();
|
|
187
|
-
|
|
224
|
+
let timedOut = false;
|
|
225
|
+
const timer = setTimeout(() => {
|
|
226
|
+
timedOut = true;
|
|
227
|
+
controller.abort();
|
|
228
|
+
}, this.timeoutMs);
|
|
229
|
+
const failed = (e) => new KorelyError(
|
|
230
|
+
timedOut ? `Request timed out after ${this.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`
|
|
231
|
+
);
|
|
188
232
|
let resp;
|
|
233
|
+
let text;
|
|
189
234
|
try {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
235
|
+
try {
|
|
236
|
+
resp = await this.fetchImpl(url, {
|
|
237
|
+
method,
|
|
238
|
+
headers,
|
|
239
|
+
body,
|
|
240
|
+
signal: controller.signal
|
|
241
|
+
});
|
|
242
|
+
} catch (e) {
|
|
243
|
+
throw failed(e);
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
text = await resp.text();
|
|
247
|
+
} catch (e) {
|
|
248
|
+
throw failed(e);
|
|
249
|
+
}
|
|
198
250
|
} finally {
|
|
199
251
|
clearTimeout(timer);
|
|
200
252
|
}
|
|
201
|
-
const text = await resp.text();
|
|
202
253
|
let parsed = {};
|
|
203
254
|
if (text) {
|
|
204
255
|
try {
|
|
205
256
|
parsed = JSON.parse(text);
|
|
206
257
|
} catch {
|
|
258
|
+
if (resp.ok) {
|
|
259
|
+
throw new KorelyError(
|
|
260
|
+
`The server answered ${resp.status} to ${method} ${path} with a body that is not JSON: ${JSON.stringify(text.slice(0, 200))}`,
|
|
261
|
+
{ status: resp.status }
|
|
262
|
+
);
|
|
263
|
+
}
|
|
207
264
|
parsed = { message: text };
|
|
208
265
|
}
|
|
209
266
|
}
|
|
@@ -213,8 +270,7 @@ var Korely = class {
|
|
|
213
270
|
return parsed && typeof parsed === "object" ? parsed : {};
|
|
214
271
|
}
|
|
215
272
|
raise(status, body, retryAfter) {
|
|
216
|
-
const code = body
|
|
217
|
-
const msg = body?.message || code || `HTTP ${status}`;
|
|
273
|
+
const { code, message: msg } = errorFields(status, body);
|
|
218
274
|
if (status === 401) throw new AuthenticationError(msg, { status, code });
|
|
219
275
|
if (status === 403) throw new NamespaceForbiddenError(msg, { status, code });
|
|
220
276
|
if (status === 404) throw new NotFoundError(msg, { status, code });
|
|
@@ -223,8 +279,8 @@ var Korely = class {
|
|
|
223
279
|
const raw = retryAfter ?? body?.retry_after ?? body?._retry_after;
|
|
224
280
|
let ra;
|
|
225
281
|
if (raw != null) {
|
|
226
|
-
const n =
|
|
227
|
-
ra = Number.
|
|
282
|
+
const n = Math.ceil(Number(raw));
|
|
283
|
+
ra = Number.isFinite(n) && n >= 0 ? n : void 0;
|
|
228
284
|
}
|
|
229
285
|
throw new QuotaExceededError(msg, { status, code, retryAfter: ra });
|
|
230
286
|
}
|
|
@@ -232,7 +288,7 @@ var Korely = class {
|
|
|
232
288
|
}
|
|
233
289
|
// ── memories ────────────────────────────────────────────────────────────
|
|
234
290
|
/**
|
|
235
|
-
* POST /v1/memories
|
|
291
|
+
* POST /v1/memories: store a memory; resolves to it with extracted facts.
|
|
236
292
|
* `content` is a string, or a list of chat messages (role/content), joined
|
|
237
293
|
* into one block before sending. Pass `timestamp` (ISO date/datetime) for
|
|
238
294
|
* backfill: facts extracted inherit it as `valid_from`.
|
|
@@ -241,7 +297,7 @@ var Korely = class {
|
|
|
241
297
|
const text = coerceContent(content);
|
|
242
298
|
if (!text.trim()) {
|
|
243
299
|
throw new KorelyError(
|
|
244
|
-
"content is empty
|
|
300
|
+
"content is empty: pass a non-blank string or messages with content."
|
|
245
301
|
);
|
|
246
302
|
}
|
|
247
303
|
return this.request("POST", "/v1/memories", {
|
|
@@ -256,8 +312,10 @@ var Korely = class {
|
|
|
256
312
|
});
|
|
257
313
|
}
|
|
258
314
|
/**
|
|
259
|
-
* POST /v1/memories/search
|
|
260
|
-
*
|
|
315
|
+
* POST /v1/memories/search: semantic search over raw memories, ranked by
|
|
316
|
+
* score (vector similarity to the query). `run_id` scopes to one session,
|
|
317
|
+
* `metadata` filters on what you stored at write time. `limit` defaults to
|
|
318
|
+
* the server default (15), max 50.
|
|
261
319
|
*/
|
|
262
320
|
async search(query, opts = {}) {
|
|
263
321
|
const body = await this.request("POST", "/v1/memories/search", {
|
|
@@ -265,61 +323,86 @@ var Korely = class {
|
|
|
265
323
|
query,
|
|
266
324
|
user_id: opts.user_id,
|
|
267
325
|
agent_id: opts.agent_id,
|
|
326
|
+
run_id: opts.run_id,
|
|
327
|
+
metadata: opts.metadata,
|
|
268
328
|
limit: opts.limit
|
|
269
329
|
}
|
|
270
330
|
});
|
|
271
331
|
return body.results ?? [];
|
|
272
332
|
}
|
|
273
|
-
/**
|
|
333
|
+
/**
|
|
334
|
+
* GET /v1/memories: list a scope, newest first. The page is iterable.
|
|
335
|
+
* `limit` goes up to 200 (the server capped it at 100 before 2026-09-28);
|
|
336
|
+
* `total` and `offset` walk the rest.
|
|
337
|
+
*/
|
|
274
338
|
async getAll(opts = {}) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
339
|
+
const page = await this.request(
|
|
340
|
+
"GET",
|
|
341
|
+
"/v1/memories",
|
|
342
|
+
{
|
|
343
|
+
params: {
|
|
344
|
+
user_id: opts.user_id,
|
|
345
|
+
agent_id: opts.agent_id,
|
|
346
|
+
run_id: opts.run_id,
|
|
347
|
+
limit: opts.limit ?? 50,
|
|
348
|
+
offset: opts.offset ?? 0
|
|
349
|
+
}
|
|
281
350
|
}
|
|
282
|
-
|
|
351
|
+
);
|
|
352
|
+
return iterableOver(page, "memories");
|
|
283
353
|
}
|
|
284
|
-
/** GET /v1/memories/:id
|
|
354
|
+
/** GET /v1/memories/:id: full content, metadata, extracted facts. */
|
|
285
355
|
async get(memoryId) {
|
|
286
|
-
return this.request("GET", `/v1/memories/${memoryId}`);
|
|
356
|
+
return this.request("GET", `/v1/memories/${seg(memoryId, "memoryId")}`);
|
|
287
357
|
}
|
|
288
358
|
/**
|
|
289
|
-
* PATCH /v1/memories/:id
|
|
359
|
+
* PATCH /v1/memories/:id: re-runs extraction. Pass `expected_updated_at`
|
|
290
360
|
* for optimistic concurrency (throws StaleWriteError instead of clobbering).
|
|
291
361
|
*/
|
|
292
362
|
async update(memoryId, opts) {
|
|
293
|
-
return this.request("PATCH", `/v1/memories/${memoryId}`, {
|
|
363
|
+
return this.request("PATCH", `/v1/memories/${seg(memoryId, "memoryId")}`, {
|
|
294
364
|
body: {
|
|
295
365
|
content: opts.content,
|
|
296
366
|
expected_updated_at: opts.expected_updated_at
|
|
297
367
|
}
|
|
298
368
|
});
|
|
299
369
|
}
|
|
300
|
-
/**
|
|
370
|
+
/**
|
|
371
|
+
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
372
|
+
* read, and the facts only it asserted are invalidated (kept as history,
|
|
373
|
+
* audited). For erasure use `deleteAll`.
|
|
374
|
+
*/
|
|
301
375
|
async delete(memoryId) {
|
|
302
|
-
return this.request("DELETE", `/v1/memories/${memoryId}`);
|
|
376
|
+
return this.request("DELETE", `/v1/memories/${seg(memoryId, "memoryId")}`);
|
|
303
377
|
}
|
|
304
378
|
/**
|
|
305
|
-
* DELETE /v1/users/:user_id/memories
|
|
306
|
-
*
|
|
379
|
+
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
380
|
+
* user (GDPR Art. 17). Physical deletion, not a flag: nothing is readable
|
|
381
|
+
* afterwards. The audit row (counts, never content) survives.
|
|
382
|
+
*
|
|
383
|
+
* The receipt counts the rows in `memories_deleted` and `facts_deleted`.
|
|
384
|
+
* `memories_forgotten` and `facts_invalidated` are deprecated aliases with
|
|
385
|
+
* the same numbers; whichever pair the server sends fills both.
|
|
307
386
|
*/
|
|
308
387
|
async deleteAll(opts) {
|
|
309
|
-
|
|
388
|
+
const r = await this.request(
|
|
310
389
|
"DELETE",
|
|
311
|
-
`/v1/users/${opts.user_id}/memories`
|
|
390
|
+
`/v1/users/${seg(opts.user_id, "user_id")}/memories`
|
|
312
391
|
);
|
|
392
|
+
const fields = r;
|
|
393
|
+
fillPair(fields, "memories_deleted", "memories_forgotten");
|
|
394
|
+
fillPair(fields, "facts_deleted", "facts_invalidated");
|
|
395
|
+
return r;
|
|
313
396
|
}
|
|
314
397
|
/**
|
|
315
|
-
* GET /v1/memories/:id/history
|
|
398
|
+
* GET /v1/memories/:id/history: the lifecycle timeline of a memory,
|
|
316
399
|
* created / updated / deleted, plus every typed fact it produced.
|
|
317
400
|
*/
|
|
318
401
|
async history(memoryId) {
|
|
319
|
-
return this.request("GET", `/v1/memories/${memoryId}/history`);
|
|
402
|
+
return this.request("GET", `/v1/memories/${seg(memoryId, "memoryId")}/history`);
|
|
320
403
|
}
|
|
321
404
|
/**
|
|
322
|
-
* GET /v1/users
|
|
405
|
+
* GET /v1/users: the end users you've stored data for (distinct user_id
|
|
323
406
|
* namespaces), each with active memory + fact counts and last-active time.
|
|
324
407
|
*/
|
|
325
408
|
async users(opts = {}) {
|
|
@@ -338,12 +421,17 @@ var Korely = class {
|
|
|
338
421
|
}
|
|
339
422
|
// ── agents ────────────────────────────────────────────────────────────────
|
|
340
423
|
/**
|
|
341
|
-
* GET /v1/agents
|
|
342
|
-
* non-null agent_id values), each with active memory + fact
|
|
343
|
-
* last-active time
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
424
|
+
* GET /v1/agents: the agent namespaces written under in this key's project
|
|
425
|
+
* (the distinct non-null agent_id values), each with active memory + fact
|
|
426
|
+
* counts and last-active time. The antidote to the agent-cap trap: when a
|
|
427
|
+
* write is rejected with `agent_cap_exceeded`, call this to see which
|
|
428
|
+
* namespaces already exist and reuse one instead of minting a new id.
|
|
429
|
+
*
|
|
430
|
+
* `total` counts this project's namespaces (exactly the ones this key can
|
|
431
|
+
* delete); `used` counts the cap slots taken across the whole account, by
|
|
432
|
+
* name, which is what the 403 compares with `cap`. `used` is above `total`
|
|
433
|
+
* when other projects use names this one does not. A self-hosted install
|
|
434
|
+
* sets no cap and answers `cap: 0`.
|
|
347
435
|
*/
|
|
348
436
|
async listAgents(opts = {}) {
|
|
349
437
|
const page = await this.request("GET", "/v1/agents", {
|
|
@@ -355,18 +443,26 @@ var Korely = class {
|
|
|
355
443
|
return iterableOver(page, "agents");
|
|
356
444
|
}
|
|
357
445
|
/**
|
|
358
|
-
* DELETE /v1/agents/:agent_id
|
|
359
|
-
* memory + fact written under this agent_id
|
|
360
|
-
* (soft-forgetting its data does NOT free
|
|
361
|
-
* counts
|
|
446
|
+
* DELETE /v1/agents/:agent_id: hard-delete an agent namespace, purging every
|
|
447
|
+
* memory + fact written under this agent_id in this key's project
|
|
448
|
+
* (soft-forgetting its data does NOT free its cap slot; this does). Resolves
|
|
449
|
+
* to the purge counts, an audit id and `slot_freed`.
|
|
450
|
+
*
|
|
451
|
+
* The namespace must be one `listAgents()` shows for this key: anything else
|
|
452
|
+
* rejects with NotFoundError, including a name only another project of the
|
|
453
|
+
* account uses (the server answered 200 with zero counts for it before
|
|
454
|
+
* 2026-09-28, and freed nothing). The cap counts a name across the account,
|
|
455
|
+
* so while another project still uses the same agent_id its rows stay and
|
|
456
|
+
* `slot_freed` is false.
|
|
362
457
|
*/
|
|
363
458
|
async deleteAgent(agentId) {
|
|
364
|
-
return this.request("DELETE", `/v1/agents/${agentId}`);
|
|
459
|
+
return this.request("DELETE", `/v1/agents/${seg(agentId, "agentId")}`);
|
|
365
460
|
}
|
|
366
461
|
// ── facts ─────────────────────────────────────────────────────────────────
|
|
367
462
|
/**
|
|
368
|
-
* GET /v1/facts
|
|
369
|
-
* validity. Pass `as_of` (ISO date) for a point-in-time query.
|
|
463
|
+
* GET /v1/facts: typed (subject, predicate, object) triples with bi-temporal
|
|
464
|
+
* validity. Pass `as_of` (ISO date) for a point-in-time query. Resolves to an
|
|
465
|
+
* array that also carries `total`, the matches across all pages.
|
|
370
466
|
*/
|
|
371
467
|
async getFacts(opts = {}) {
|
|
372
468
|
const params = {
|
|
@@ -382,13 +478,19 @@ var Korely = class {
|
|
|
382
478
|
};
|
|
383
479
|
if (opts.include_invalidated) params.include_invalidated = "true";
|
|
384
480
|
const body = await this.request("GET", "/v1/facts", { params });
|
|
385
|
-
|
|
481
|
+
const facts = [...body.facts ?? []];
|
|
482
|
+
Object.defineProperty(facts, "total", {
|
|
483
|
+
value: typeof body.total === "number" ? body.total : facts.length,
|
|
484
|
+
enumerable: false
|
|
485
|
+
});
|
|
486
|
+
return facts;
|
|
386
487
|
}
|
|
387
488
|
/**
|
|
388
|
-
* POST /v1/facts
|
|
489
|
+
* POST /v1/facts: write a typed (subject, predicate, object) triple directly,
|
|
389
490
|
* 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`
|
|
491
|
+
* bi-temporal (pass `valid_from` for a historical fact, `tense: "past"` for
|
|
492
|
+
* one that is over). Resolves to the written Fact, with `invalidated`
|
|
493
|
+
* listing any fact ids it superseded.
|
|
392
494
|
*/
|
|
393
495
|
async addFactTriple(subject, predicate, object, opts = {}) {
|
|
394
496
|
return this.request("POST", "/v1/facts", {
|
|
@@ -402,17 +504,13 @@ var Korely = class {
|
|
|
402
504
|
subject_type: opts.subject_type ?? "unknown",
|
|
403
505
|
object_is_literal: opts.object_is_literal ?? false,
|
|
404
506
|
confidence: opts.confidence ?? 0.9,
|
|
405
|
-
valid_from: opts.valid_from
|
|
507
|
+
valid_from: opts.valid_from,
|
|
508
|
+
tense: opts.tense
|
|
406
509
|
}
|
|
407
510
|
});
|
|
408
511
|
}
|
|
409
512
|
/**
|
|
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
|
|
513
|
+
* POST /v1/facts/:id/forget: close a fact; it stops being current and stays
|
|
416
514
|
* in history.
|
|
417
515
|
*
|
|
418
516
|
* `at` is the date it STOPPED being true, not the date you noticed. Reading
|
|
@@ -426,22 +524,34 @@ var Korely = class {
|
|
|
426
524
|
* knows a fact is finished says so, and nothing has to infer it.
|
|
427
525
|
*/
|
|
428
526
|
async forgetFact(factId, opts = {}) {
|
|
429
|
-
return this.request("POST", `/v1/facts/${
|
|
527
|
+
return this.request("POST", `/v1/facts/${seg(factId, "factId")}/forget`, {
|
|
430
528
|
body: { at: opts.at }
|
|
431
529
|
});
|
|
432
530
|
}
|
|
433
531
|
/**
|
|
434
|
-
* PATCH /v1/facts/:id
|
|
532
|
+
* PATCH /v1/facts/:id: supersede a fact with a corrected one.
|
|
435
533
|
*
|
|
436
534
|
* Not an edit: the old row keeps its dates and gains a pointer to the new
|
|
437
535
|
* one, so `as_of` before the correction still returns what you believed then.
|
|
438
536
|
* At least one of the three fields is required.
|
|
537
|
+
*
|
|
538
|
+
* Resolves to the new Fact in the write shape. Its `invalidated` lists every
|
|
539
|
+
* fact the correction superseded: the corrected one first, then any other
|
|
540
|
+
* that the contradiction check on the new fact closed. It is not always one
|
|
541
|
+
* id. A correction that names the fact as it already stands (same subject,
|
|
542
|
+
* predicate and object, still open) supersedes nothing: the server
|
|
543
|
+
* reconfirms the fact and returns it, same `id`, with `invalidated: []`.
|
|
439
544
|
*/
|
|
440
545
|
async correctFact(factId, changes) {
|
|
441
|
-
return this.request("PATCH", `/v1/facts/${
|
|
546
|
+
return this.request("PATCH", `/v1/facts/${seg(factId, "factId")}`, {
|
|
442
547
|
body: changes
|
|
443
548
|
});
|
|
444
549
|
}
|
|
550
|
+
/**
|
|
551
|
+
* GET /v1/profile: the assembled profile of one end user, the active typed
|
|
552
|
+
* facts known about them, the end user's own facts first, grouped by family.
|
|
553
|
+
* Pass `as_of` (ISO date) for the point-in-time profile.
|
|
554
|
+
*/
|
|
445
555
|
async getProfile(opts) {
|
|
446
556
|
return this.request("GET", "/v1/profile", {
|
|
447
557
|
params: {
|
|
@@ -453,27 +563,67 @@ var Korely = class {
|
|
|
453
563
|
}
|
|
454
564
|
// ── context ─────────────────────────────────────────────────────────────
|
|
455
565
|
/**
|
|
456
|
-
* GET /v1/context
|
|
566
|
+
* GET /v1/context: one call that assembles a prompt-ready context block
|
|
457
567
|
* (profile + relevant facts + memories) within a token budget.
|
|
458
568
|
*/
|
|
459
|
-
async getContext(opts) {
|
|
569
|
+
async getContext(opts, extra = {}) {
|
|
570
|
+
const o = typeof opts === "string" ? { ...extra, query: opts } : opts;
|
|
571
|
+
if (!o || typeof o.query !== "string" || !o.query.trim()) {
|
|
572
|
+
throw new KorelyError("getContext needs a query.");
|
|
573
|
+
}
|
|
460
574
|
return this.request("GET", "/v1/context", {
|
|
461
575
|
params: {
|
|
462
|
-
query:
|
|
576
|
+
query: o.query,
|
|
577
|
+
user_id: o.user_id,
|
|
578
|
+
agent_id: o.agent_id,
|
|
579
|
+
token_budget: o.token_budget ?? 800
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
// ── processing state ───────────────────────────────────────────────────────
|
|
584
|
+
/**
|
|
585
|
+
* GET /v1/events: which writes have finished being processed.
|
|
586
|
+
*
|
|
587
|
+
* `add()` resolves as soon as the memory is stored; fact extraction runs
|
|
588
|
+
* behind it. Each event carries a memory's `status` ("processing", "ready"
|
|
589
|
+
* or "error"), newest first. `status` filters before `limit` (up to 200) is
|
|
590
|
+
* applied, so `{ status: "error" }` returns the latest errors however many
|
|
591
|
+
* ready writes came after them. `processing` counts every write of this
|
|
592
|
+
* key's project still in flight (of `user_id`, when given), whatever
|
|
593
|
+
* `status` and `limit` say. No webhook fires when extraction finishes, so
|
|
594
|
+
* this is how to know.
|
|
595
|
+
*/
|
|
596
|
+
async events(opts = {}) {
|
|
597
|
+
return this.request("GET", "/v1/events", {
|
|
598
|
+
params: {
|
|
463
599
|
user_id: opts.user_id,
|
|
464
|
-
|
|
465
|
-
|
|
600
|
+
status: opts.status,
|
|
601
|
+
limit: opts.limit ?? 50
|
|
466
602
|
}
|
|
467
603
|
});
|
|
468
604
|
}
|
|
469
605
|
// ── batch ─────────────────────────────────────────────────────────────────
|
|
470
|
-
/**
|
|
606
|
+
/**
|
|
607
|
+
* POST /v1/batch: bulk import, up to 500 memory objects, processed
|
|
608
|
+
* asynchronously. Each object is the body of one `add()` (see
|
|
609
|
+
* `BatchMemory`): `content` and optionally `user_id`, `agent_id`, `run_id`,
|
|
610
|
+
* `metadata` and `timestamp`.
|
|
611
|
+
*
|
|
612
|
+
* `timestamp` means what it means on `add()`: when the events happened.
|
|
613
|
+
* The facts extracted from the item inherit it as `valid_from`, so a
|
|
614
|
+
* migration keeps its real dates; without it an item is dated when it is
|
|
615
|
+
* imported. A value that is not an ISO 8601 date or datetime refuses the
|
|
616
|
+
* whole batch before anything is queued: a 422 (APIError,
|
|
617
|
+
* `code === "invalid_request"`) whose message names the item, e.g.
|
|
618
|
+
* `memories[3].timestamp`. Any key not listed above is refused the same
|
|
619
|
+
* way. Servers older than 2026-09-28 refuse `timestamp` itself.
|
|
620
|
+
*/
|
|
471
621
|
async batch(memories) {
|
|
472
622
|
return this.request("POST", "/v1/batch", { body: { memories } });
|
|
473
623
|
}
|
|
474
|
-
/** GET /v1/batch/:id
|
|
624
|
+
/** GET /v1/batch/:id: poll an import job. */
|
|
475
625
|
async batchStatus(jobId) {
|
|
476
|
-
return this.request("GET", `/v1/batch/${jobId}`);
|
|
626
|
+
return this.request("GET", `/v1/batch/${seg(jobId, "jobId")}`);
|
|
477
627
|
}
|
|
478
628
|
};
|
|
479
629
|
// Annotate the CommonJS export names for ESM import in node:
|