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.js
CHANGED
|
@@ -8,35 +8,42 @@ var KorelyError = class extends Error {
|
|
|
8
8
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
9
9
|
}
|
|
10
10
|
};
|
|
11
|
-
var
|
|
11
|
+
var APIError = class extends KorelyError {
|
|
12
|
+
constructor(message = "", opts = {}) {
|
|
13
|
+
super(message, opts);
|
|
14
|
+
this.name = "APIError";
|
|
15
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var AuthenticationError = class _AuthenticationError extends APIError {
|
|
12
19
|
constructor(message = "", opts = {}) {
|
|
13
20
|
super(message, opts);
|
|
14
21
|
this.name = "AuthenticationError";
|
|
15
22
|
Object.setPrototypeOf(this, _AuthenticationError.prototype);
|
|
16
23
|
}
|
|
17
24
|
};
|
|
18
|
-
var NamespaceForbiddenError = class _NamespaceForbiddenError extends
|
|
25
|
+
var NamespaceForbiddenError = class _NamespaceForbiddenError extends APIError {
|
|
19
26
|
constructor(message = "", opts = {}) {
|
|
20
27
|
super(message, opts);
|
|
21
28
|
this.name = "NamespaceForbiddenError";
|
|
22
29
|
Object.setPrototypeOf(this, _NamespaceForbiddenError.prototype);
|
|
23
30
|
}
|
|
24
31
|
};
|
|
25
|
-
var NotFoundError = class _NotFoundError extends
|
|
32
|
+
var NotFoundError = class _NotFoundError extends APIError {
|
|
26
33
|
constructor(message = "", opts = {}) {
|
|
27
34
|
super(message, opts);
|
|
28
35
|
this.name = "NotFoundError";
|
|
29
36
|
Object.setPrototypeOf(this, _NotFoundError.prototype);
|
|
30
37
|
}
|
|
31
38
|
};
|
|
32
|
-
var StaleWriteError = class _StaleWriteError extends
|
|
39
|
+
var StaleWriteError = class _StaleWriteError extends APIError {
|
|
33
40
|
constructor(message = "", opts = {}) {
|
|
34
41
|
super(message, opts);
|
|
35
42
|
this.name = "StaleWriteError";
|
|
36
43
|
Object.setPrototypeOf(this, _StaleWriteError.prototype);
|
|
37
44
|
}
|
|
38
45
|
};
|
|
39
|
-
var QuotaExceededError = class _QuotaExceededError extends
|
|
46
|
+
var QuotaExceededError = class _QuotaExceededError extends APIError {
|
|
40
47
|
constructor(message = "", opts = {}) {
|
|
41
48
|
super(message, opts);
|
|
42
49
|
this.name = "QuotaExceededError";
|
|
@@ -44,16 +51,9 @@ var QuotaExceededError = class _QuotaExceededError extends KorelyError {
|
|
|
44
51
|
Object.setPrototypeOf(this, _QuotaExceededError.prototype);
|
|
45
52
|
}
|
|
46
53
|
};
|
|
47
|
-
var APIError = class _APIError extends KorelyError {
|
|
48
|
-
constructor(message = "", opts = {}) {
|
|
49
|
-
super(message, opts);
|
|
50
|
-
this.name = "APIError";
|
|
51
|
-
Object.setPrototypeOf(this, _APIError.prototype);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
54
|
|
|
55
55
|
// src/client.ts
|
|
56
|
-
var VERSION = "0.1.
|
|
56
|
+
var VERSION = "0.1.8";
|
|
57
57
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
58
58
|
function coerceContent(content) {
|
|
59
59
|
if (typeof content === "string") return content;
|
|
@@ -75,6 +75,38 @@ function coerceContent(content) {
|
|
|
75
75
|
}
|
|
76
76
|
return String(content);
|
|
77
77
|
}
|
|
78
|
+
function seg(value, what) {
|
|
79
|
+
const s = value == null ? "" : String(value);
|
|
80
|
+
if (!s) throw new KorelyError(`${what} is empty.`);
|
|
81
|
+
return encodeURIComponent(s);
|
|
82
|
+
}
|
|
83
|
+
function detailLine(detail) {
|
|
84
|
+
if (typeof detail === "string") return detail.trim() ? detail : void 0;
|
|
85
|
+
if (Array.isArray(detail) && detail.length) {
|
|
86
|
+
const parts = detail.slice(0, 3).map((e) => {
|
|
87
|
+
if (!e || typeof e !== "object") return String(e);
|
|
88
|
+
const loc = Array.isArray(e.loc) ? e.loc : [];
|
|
89
|
+
const where = loc.filter((x) => x !== "body").map(String).join(".");
|
|
90
|
+
const msg = String(e.msg ?? "invalid");
|
|
91
|
+
return where ? `${where}: ${msg}` : msg;
|
|
92
|
+
});
|
|
93
|
+
const more = detail.length - 3;
|
|
94
|
+
return parts.join("; ") + (more > 0 ? ` (and ${more} more)` : "");
|
|
95
|
+
}
|
|
96
|
+
return void 0;
|
|
97
|
+
}
|
|
98
|
+
function errorFields(status, body) {
|
|
99
|
+
const text = (v) => typeof v === "string" && v.trim() ? v : void 0;
|
|
100
|
+
const b = body && typeof body === "object" ? body : {};
|
|
101
|
+
const pair = b.detail && typeof b.detail === "object" && !Array.isArray(b.detail) ? b.detail : {};
|
|
102
|
+
const code = text(b.code) ?? text(pair.code);
|
|
103
|
+
const message = text(b.message) ?? text(pair.message) ?? detailLine(b.detail) ?? code ?? `HTTP ${status}`;
|
|
104
|
+
return { code, message };
|
|
105
|
+
}
|
|
106
|
+
function fillPair(obj, current, deprecated) {
|
|
107
|
+
if (obj[current] == null && obj[deprecated] != null) obj[current] = obj[deprecated];
|
|
108
|
+
if (obj[deprecated] == null && obj[current] != null) obj[deprecated] = obj[current];
|
|
109
|
+
}
|
|
78
110
|
function iterableOver(page, key) {
|
|
79
111
|
const out = page;
|
|
80
112
|
Object.defineProperty(out, Symbol.iterator, {
|
|
@@ -108,6 +140,11 @@ function refuseAMismatchedPair(apiKey, baseUrl) {
|
|
|
108
140
|
}
|
|
109
141
|
var Korely = class {
|
|
110
142
|
constructor(opts = {}) {
|
|
143
|
+
if (opts && Object.prototype.hasOwnProperty.call(opts, "base_url")) {
|
|
144
|
+
throw new KorelyError(
|
|
145
|
+
"Unknown option base_url: the JavaScript SDK takes baseUrl (new Korely({ baseUrl: 'https://...' })) or the KORELY_BASE_URL env var."
|
|
146
|
+
);
|
|
147
|
+
}
|
|
111
148
|
const envKey = typeof process !== "undefined" ? process.env?.KORELY_API_KEY : void 0;
|
|
112
149
|
const key = opts.apiKey ?? envKey;
|
|
113
150
|
if (!key) {
|
|
@@ -150,26 +187,46 @@ var Korely = class {
|
|
|
150
187
|
headers["Content-Type"] = "application/json";
|
|
151
188
|
}
|
|
152
189
|
const controller = new AbortController();
|
|
153
|
-
|
|
190
|
+
let timedOut = false;
|
|
191
|
+
const timer = setTimeout(() => {
|
|
192
|
+
timedOut = true;
|
|
193
|
+
controller.abort();
|
|
194
|
+
}, this.timeoutMs);
|
|
195
|
+
const failed = (e) => new KorelyError(
|
|
196
|
+
timedOut ? `Request timed out after ${this.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`
|
|
197
|
+
);
|
|
154
198
|
let resp;
|
|
199
|
+
let text;
|
|
155
200
|
try {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
201
|
+
try {
|
|
202
|
+
resp = await this.fetchImpl(url, {
|
|
203
|
+
method,
|
|
204
|
+
headers,
|
|
205
|
+
body,
|
|
206
|
+
signal: controller.signal
|
|
207
|
+
});
|
|
208
|
+
} catch (e) {
|
|
209
|
+
throw failed(e);
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
text = await resp.text();
|
|
213
|
+
} catch (e) {
|
|
214
|
+
throw failed(e);
|
|
215
|
+
}
|
|
164
216
|
} finally {
|
|
165
217
|
clearTimeout(timer);
|
|
166
218
|
}
|
|
167
|
-
const text = await resp.text();
|
|
168
219
|
let parsed = {};
|
|
169
220
|
if (text) {
|
|
170
221
|
try {
|
|
171
222
|
parsed = JSON.parse(text);
|
|
172
223
|
} catch {
|
|
224
|
+
if (resp.ok) {
|
|
225
|
+
throw new KorelyError(
|
|
226
|
+
`The server answered ${resp.status} to ${method} ${path} with a body that is not JSON: ${JSON.stringify(text.slice(0, 200))}`,
|
|
227
|
+
{ status: resp.status }
|
|
228
|
+
);
|
|
229
|
+
}
|
|
173
230
|
parsed = { message: text };
|
|
174
231
|
}
|
|
175
232
|
}
|
|
@@ -179,8 +236,7 @@ var Korely = class {
|
|
|
179
236
|
return parsed && typeof parsed === "object" ? parsed : {};
|
|
180
237
|
}
|
|
181
238
|
raise(status, body, retryAfter) {
|
|
182
|
-
const code = body
|
|
183
|
-
const msg = body?.message || code || `HTTP ${status}`;
|
|
239
|
+
const { code, message: msg } = errorFields(status, body);
|
|
184
240
|
if (status === 401) throw new AuthenticationError(msg, { status, code });
|
|
185
241
|
if (status === 403) throw new NamespaceForbiddenError(msg, { status, code });
|
|
186
242
|
if (status === 404) throw new NotFoundError(msg, { status, code });
|
|
@@ -189,8 +245,8 @@ var Korely = class {
|
|
|
189
245
|
const raw = retryAfter ?? body?.retry_after ?? body?._retry_after;
|
|
190
246
|
let ra;
|
|
191
247
|
if (raw != null) {
|
|
192
|
-
const n =
|
|
193
|
-
ra = Number.
|
|
248
|
+
const n = Math.ceil(Number(raw));
|
|
249
|
+
ra = Number.isFinite(n) && n >= 0 ? n : void 0;
|
|
194
250
|
}
|
|
195
251
|
throw new QuotaExceededError(msg, { status, code, retryAfter: ra });
|
|
196
252
|
}
|
|
@@ -198,7 +254,7 @@ var Korely = class {
|
|
|
198
254
|
}
|
|
199
255
|
// ── memories ────────────────────────────────────────────────────────────
|
|
200
256
|
/**
|
|
201
|
-
* POST /v1/memories
|
|
257
|
+
* POST /v1/memories: store a memory; resolves to it with extracted facts.
|
|
202
258
|
* `content` is a string, or a list of chat messages (role/content), joined
|
|
203
259
|
* into one block before sending. Pass `timestamp` (ISO date/datetime) for
|
|
204
260
|
* backfill: facts extracted inherit it as `valid_from`.
|
|
@@ -207,7 +263,7 @@ var Korely = class {
|
|
|
207
263
|
const text = coerceContent(content);
|
|
208
264
|
if (!text.trim()) {
|
|
209
265
|
throw new KorelyError(
|
|
210
|
-
"content is empty
|
|
266
|
+
"content is empty: pass a non-blank string or messages with content."
|
|
211
267
|
);
|
|
212
268
|
}
|
|
213
269
|
return this.request("POST", "/v1/memories", {
|
|
@@ -222,8 +278,10 @@ var Korely = class {
|
|
|
222
278
|
});
|
|
223
279
|
}
|
|
224
280
|
/**
|
|
225
|
-
* POST /v1/memories/search
|
|
226
|
-
*
|
|
281
|
+
* POST /v1/memories/search: semantic search over raw memories, ranked by
|
|
282
|
+
* score (vector similarity to the query). `run_id` scopes to one session,
|
|
283
|
+
* `metadata` filters on what you stored at write time. `limit` defaults to
|
|
284
|
+
* the server default (15), max 50.
|
|
227
285
|
*/
|
|
228
286
|
async search(query, opts = {}) {
|
|
229
287
|
const body = await this.request("POST", "/v1/memories/search", {
|
|
@@ -231,61 +289,86 @@ var Korely = class {
|
|
|
231
289
|
query,
|
|
232
290
|
user_id: opts.user_id,
|
|
233
291
|
agent_id: opts.agent_id,
|
|
292
|
+
run_id: opts.run_id,
|
|
293
|
+
metadata: opts.metadata,
|
|
234
294
|
limit: opts.limit
|
|
235
295
|
}
|
|
236
296
|
});
|
|
237
297
|
return body.results ?? [];
|
|
238
298
|
}
|
|
239
|
-
/**
|
|
299
|
+
/**
|
|
300
|
+
* GET /v1/memories: list a scope, newest first. The page is iterable.
|
|
301
|
+
* `limit` goes up to 200 (the server capped it at 100 before 2026-09-28);
|
|
302
|
+
* `total` and `offset` walk the rest.
|
|
303
|
+
*/
|
|
240
304
|
async getAll(opts = {}) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
305
|
+
const page = await this.request(
|
|
306
|
+
"GET",
|
|
307
|
+
"/v1/memories",
|
|
308
|
+
{
|
|
309
|
+
params: {
|
|
310
|
+
user_id: opts.user_id,
|
|
311
|
+
agent_id: opts.agent_id,
|
|
312
|
+
run_id: opts.run_id,
|
|
313
|
+
limit: opts.limit ?? 50,
|
|
314
|
+
offset: opts.offset ?? 0
|
|
315
|
+
}
|
|
247
316
|
}
|
|
248
|
-
|
|
317
|
+
);
|
|
318
|
+
return iterableOver(page, "memories");
|
|
249
319
|
}
|
|
250
|
-
/** GET /v1/memories/:id
|
|
320
|
+
/** GET /v1/memories/:id: full content, metadata, extracted facts. */
|
|
251
321
|
async get(memoryId) {
|
|
252
|
-
return this.request("GET", `/v1/memories/${memoryId}`);
|
|
322
|
+
return this.request("GET", `/v1/memories/${seg(memoryId, "memoryId")}`);
|
|
253
323
|
}
|
|
254
324
|
/**
|
|
255
|
-
* PATCH /v1/memories/:id
|
|
325
|
+
* PATCH /v1/memories/:id: re-runs extraction. Pass `expected_updated_at`
|
|
256
326
|
* for optimistic concurrency (throws StaleWriteError instead of clobbering).
|
|
257
327
|
*/
|
|
258
328
|
async update(memoryId, opts) {
|
|
259
|
-
return this.request("PATCH", `/v1/memories/${memoryId}`, {
|
|
329
|
+
return this.request("PATCH", `/v1/memories/${seg(memoryId, "memoryId")}`, {
|
|
260
330
|
body: {
|
|
261
331
|
content: opts.content,
|
|
262
332
|
expected_updated_at: opts.expected_updated_at
|
|
263
333
|
}
|
|
264
334
|
});
|
|
265
335
|
}
|
|
266
|
-
/**
|
|
336
|
+
/**
|
|
337
|
+
* DELETE /v1/memories/:id: forget one memory. It drops out of every default
|
|
338
|
+
* read, and the facts only it asserted are invalidated (kept as history,
|
|
339
|
+
* audited). For erasure use `deleteAll`.
|
|
340
|
+
*/
|
|
267
341
|
async delete(memoryId) {
|
|
268
|
-
return this.request("DELETE", `/v1/memories/${memoryId}`);
|
|
342
|
+
return this.request("DELETE", `/v1/memories/${seg(memoryId, "memoryId")}`);
|
|
269
343
|
}
|
|
270
344
|
/**
|
|
271
|
-
* DELETE /v1/users/:user_id/memories
|
|
272
|
-
*
|
|
345
|
+
* DELETE /v1/users/:user_id/memories: ERASE every memory and fact of one end
|
|
346
|
+
* user (GDPR Art. 17). Physical deletion, not a flag: nothing is readable
|
|
347
|
+
* afterwards. The audit row (counts, never content) survives.
|
|
348
|
+
*
|
|
349
|
+
* The receipt counts the rows in `memories_deleted` and `facts_deleted`.
|
|
350
|
+
* `memories_forgotten` and `facts_invalidated` are deprecated aliases with
|
|
351
|
+
* the same numbers; whichever pair the server sends fills both.
|
|
273
352
|
*/
|
|
274
353
|
async deleteAll(opts) {
|
|
275
|
-
|
|
354
|
+
const r = await this.request(
|
|
276
355
|
"DELETE",
|
|
277
|
-
`/v1/users/${opts.user_id}/memories`
|
|
356
|
+
`/v1/users/${seg(opts.user_id, "user_id")}/memories`
|
|
278
357
|
);
|
|
358
|
+
const fields = r;
|
|
359
|
+
fillPair(fields, "memories_deleted", "memories_forgotten");
|
|
360
|
+
fillPair(fields, "facts_deleted", "facts_invalidated");
|
|
361
|
+
return r;
|
|
279
362
|
}
|
|
280
363
|
/**
|
|
281
|
-
* GET /v1/memories/:id/history
|
|
364
|
+
* GET /v1/memories/:id/history: the lifecycle timeline of a memory,
|
|
282
365
|
* created / updated / deleted, plus every typed fact it produced.
|
|
283
366
|
*/
|
|
284
367
|
async history(memoryId) {
|
|
285
|
-
return this.request("GET", `/v1/memories/${memoryId}/history`);
|
|
368
|
+
return this.request("GET", `/v1/memories/${seg(memoryId, "memoryId")}/history`);
|
|
286
369
|
}
|
|
287
370
|
/**
|
|
288
|
-
* GET /v1/users
|
|
371
|
+
* GET /v1/users: the end users you've stored data for (distinct user_id
|
|
289
372
|
* namespaces), each with active memory + fact counts and last-active time.
|
|
290
373
|
*/
|
|
291
374
|
async users(opts = {}) {
|
|
@@ -304,12 +387,17 @@ var Korely = class {
|
|
|
304
387
|
}
|
|
305
388
|
// ── agents ────────────────────────────────────────────────────────────────
|
|
306
389
|
/**
|
|
307
|
-
* GET /v1/agents
|
|
308
|
-
* non-null agent_id values), each with active memory + fact
|
|
309
|
-
* last-active time
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
390
|
+
* GET /v1/agents: the agent namespaces written under in this key's project
|
|
391
|
+
* (the distinct non-null agent_id values), each with active memory + fact
|
|
392
|
+
* counts and last-active time. The antidote to the agent-cap trap: when a
|
|
393
|
+
* write is rejected with `agent_cap_exceeded`, call this to see which
|
|
394
|
+
* namespaces already exist and reuse one instead of minting a new id.
|
|
395
|
+
*
|
|
396
|
+
* `total` counts this project's namespaces (exactly the ones this key can
|
|
397
|
+
* delete); `used` counts the cap slots taken across the whole account, by
|
|
398
|
+
* name, which is what the 403 compares with `cap`. `used` is above `total`
|
|
399
|
+
* when other projects use names this one does not. A self-hosted install
|
|
400
|
+
* sets no cap and answers `cap: 0`.
|
|
313
401
|
*/
|
|
314
402
|
async listAgents(opts = {}) {
|
|
315
403
|
const page = await this.request("GET", "/v1/agents", {
|
|
@@ -321,18 +409,26 @@ var Korely = class {
|
|
|
321
409
|
return iterableOver(page, "agents");
|
|
322
410
|
}
|
|
323
411
|
/**
|
|
324
|
-
* DELETE /v1/agents/:agent_id
|
|
325
|
-
* memory + fact written under this agent_id
|
|
326
|
-
* (soft-forgetting its data does NOT free
|
|
327
|
-
* counts
|
|
412
|
+
* DELETE /v1/agents/:agent_id: hard-delete an agent namespace, purging every
|
|
413
|
+
* memory + fact written under this agent_id in this key's project
|
|
414
|
+
* (soft-forgetting its data does NOT free its cap slot; this does). Resolves
|
|
415
|
+
* to the purge counts, an audit id and `slot_freed`.
|
|
416
|
+
*
|
|
417
|
+
* The namespace must be one `listAgents()` shows for this key: anything else
|
|
418
|
+
* rejects with NotFoundError, including a name only another project of the
|
|
419
|
+
* account uses (the server answered 200 with zero counts for it before
|
|
420
|
+
* 2026-09-28, and freed nothing). The cap counts a name across the account,
|
|
421
|
+
* so while another project still uses the same agent_id its rows stay and
|
|
422
|
+
* `slot_freed` is false.
|
|
328
423
|
*/
|
|
329
424
|
async deleteAgent(agentId) {
|
|
330
|
-
return this.request("DELETE", `/v1/agents/${agentId}`);
|
|
425
|
+
return this.request("DELETE", `/v1/agents/${seg(agentId, "agentId")}`);
|
|
331
426
|
}
|
|
332
427
|
// ── facts ─────────────────────────────────────────────────────────────────
|
|
333
428
|
/**
|
|
334
|
-
* GET /v1/facts
|
|
335
|
-
* validity. Pass `as_of` (ISO date) for a point-in-time query.
|
|
429
|
+
* GET /v1/facts: typed (subject, predicate, object) triples with bi-temporal
|
|
430
|
+
* validity. Pass `as_of` (ISO date) for a point-in-time query. Resolves to an
|
|
431
|
+
* array that also carries `total`, the matches across all pages.
|
|
336
432
|
*/
|
|
337
433
|
async getFacts(opts = {}) {
|
|
338
434
|
const params = {
|
|
@@ -348,13 +444,19 @@ var Korely = class {
|
|
|
348
444
|
};
|
|
349
445
|
if (opts.include_invalidated) params.include_invalidated = "true";
|
|
350
446
|
const body = await this.request("GET", "/v1/facts", { params });
|
|
351
|
-
|
|
447
|
+
const facts = [...body.facts ?? []];
|
|
448
|
+
Object.defineProperty(facts, "total", {
|
|
449
|
+
value: typeof body.total === "number" ? body.total : facts.length,
|
|
450
|
+
enumerable: false
|
|
451
|
+
});
|
|
452
|
+
return facts;
|
|
352
453
|
}
|
|
353
454
|
/**
|
|
354
|
-
* POST /v1/facts
|
|
455
|
+
* POST /v1/facts: write a typed (subject, predicate, object) triple directly,
|
|
355
456
|
* skipping extraction. The server runs the contradiction check; the fact is
|
|
356
|
-
* bi-temporal (pass `valid_from` for a historical fact
|
|
357
|
-
* written Fact, with `invalidated`
|
|
457
|
+
* bi-temporal (pass `valid_from` for a historical fact, `tense: "past"` for
|
|
458
|
+
* one that is over). Resolves to the written Fact, with `invalidated`
|
|
459
|
+
* listing any fact ids it superseded.
|
|
358
460
|
*/
|
|
359
461
|
async addFactTriple(subject, predicate, object, opts = {}) {
|
|
360
462
|
return this.request("POST", "/v1/facts", {
|
|
@@ -368,17 +470,13 @@ var Korely = class {
|
|
|
368
470
|
subject_type: opts.subject_type ?? "unknown",
|
|
369
471
|
object_is_literal: opts.object_is_literal ?? false,
|
|
370
472
|
confidence: opts.confidence ?? 0.9,
|
|
371
|
-
valid_from: opts.valid_from
|
|
473
|
+
valid_from: opts.valid_from,
|
|
474
|
+
tense: opts.tense
|
|
372
475
|
}
|
|
373
476
|
});
|
|
374
477
|
}
|
|
375
478
|
/**
|
|
376
|
-
*
|
|
377
|
-
* facts known about them, the end user's own facts first, grouped by family.
|
|
378
|
-
* Pass `as_of` (ISO date) for the point-in-time profile.
|
|
379
|
-
*/
|
|
380
|
-
/**
|
|
381
|
-
* POST /v1/facts/:id/forget — close a fact: it stops being current and stays
|
|
479
|
+
* POST /v1/facts/:id/forget: close a fact; it stops being current and stays
|
|
382
480
|
* in history.
|
|
383
481
|
*
|
|
384
482
|
* `at` is the date it STOPPED being true, not the date you noticed. Reading
|
|
@@ -392,22 +490,34 @@ var Korely = class {
|
|
|
392
490
|
* knows a fact is finished says so, and nothing has to infer it.
|
|
393
491
|
*/
|
|
394
492
|
async forgetFact(factId, opts = {}) {
|
|
395
|
-
return this.request("POST", `/v1/facts/${
|
|
493
|
+
return this.request("POST", `/v1/facts/${seg(factId, "factId")}/forget`, {
|
|
396
494
|
body: { at: opts.at }
|
|
397
495
|
});
|
|
398
496
|
}
|
|
399
497
|
/**
|
|
400
|
-
* PATCH /v1/facts/:id
|
|
498
|
+
* PATCH /v1/facts/:id: supersede a fact with a corrected one.
|
|
401
499
|
*
|
|
402
500
|
* Not an edit: the old row keeps its dates and gains a pointer to the new
|
|
403
501
|
* one, so `as_of` before the correction still returns what you believed then.
|
|
404
502
|
* At least one of the three fields is required.
|
|
503
|
+
*
|
|
504
|
+
* Resolves to the new Fact in the write shape. Its `invalidated` lists every
|
|
505
|
+
* fact the correction superseded: the corrected one first, then any other
|
|
506
|
+
* that the contradiction check on the new fact closed. It is not always one
|
|
507
|
+
* id. A correction that names the fact as it already stands (same subject,
|
|
508
|
+
* predicate and object, still open) supersedes nothing: the server
|
|
509
|
+
* reconfirms the fact and returns it, same `id`, with `invalidated: []`.
|
|
405
510
|
*/
|
|
406
511
|
async correctFact(factId, changes) {
|
|
407
|
-
return this.request("PATCH", `/v1/facts/${
|
|
512
|
+
return this.request("PATCH", `/v1/facts/${seg(factId, "factId")}`, {
|
|
408
513
|
body: changes
|
|
409
514
|
});
|
|
410
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* GET /v1/profile: the assembled profile of one end user, the active typed
|
|
518
|
+
* facts known about them, the end user's own facts first, grouped by family.
|
|
519
|
+
* Pass `as_of` (ISO date) for the point-in-time profile.
|
|
520
|
+
*/
|
|
411
521
|
async getProfile(opts) {
|
|
412
522
|
return this.request("GET", "/v1/profile", {
|
|
413
523
|
params: {
|
|
@@ -419,27 +529,67 @@ var Korely = class {
|
|
|
419
529
|
}
|
|
420
530
|
// ── context ─────────────────────────────────────────────────────────────
|
|
421
531
|
/**
|
|
422
|
-
* GET /v1/context
|
|
532
|
+
* GET /v1/context: one call that assembles a prompt-ready context block
|
|
423
533
|
* (profile + relevant facts + memories) within a token budget.
|
|
424
534
|
*/
|
|
425
|
-
async getContext(opts) {
|
|
535
|
+
async getContext(opts, extra = {}) {
|
|
536
|
+
const o = typeof opts === "string" ? { ...extra, query: opts } : opts;
|
|
537
|
+
if (!o || typeof o.query !== "string" || !o.query.trim()) {
|
|
538
|
+
throw new KorelyError("getContext needs a query.");
|
|
539
|
+
}
|
|
426
540
|
return this.request("GET", "/v1/context", {
|
|
427
541
|
params: {
|
|
428
|
-
query:
|
|
542
|
+
query: o.query,
|
|
543
|
+
user_id: o.user_id,
|
|
544
|
+
agent_id: o.agent_id,
|
|
545
|
+
token_budget: o.token_budget ?? 800
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
// ── processing state ───────────────────────────────────────────────────────
|
|
550
|
+
/**
|
|
551
|
+
* GET /v1/events: which writes have finished being processed.
|
|
552
|
+
*
|
|
553
|
+
* `add()` resolves as soon as the memory is stored; fact extraction runs
|
|
554
|
+
* behind it. Each event carries a memory's `status` ("processing", "ready"
|
|
555
|
+
* or "error"), newest first. `status` filters before `limit` (up to 200) is
|
|
556
|
+
* applied, so `{ status: "error" }` returns the latest errors however many
|
|
557
|
+
* ready writes came after them. `processing` counts every write of this
|
|
558
|
+
* key's project still in flight (of `user_id`, when given), whatever
|
|
559
|
+
* `status` and `limit` say. No webhook fires when extraction finishes, so
|
|
560
|
+
* this is how to know.
|
|
561
|
+
*/
|
|
562
|
+
async events(opts = {}) {
|
|
563
|
+
return this.request("GET", "/v1/events", {
|
|
564
|
+
params: {
|
|
429
565
|
user_id: opts.user_id,
|
|
430
|
-
|
|
431
|
-
|
|
566
|
+
status: opts.status,
|
|
567
|
+
limit: opts.limit ?? 50
|
|
432
568
|
}
|
|
433
569
|
});
|
|
434
570
|
}
|
|
435
571
|
// ── batch ─────────────────────────────────────────────────────────────────
|
|
436
|
-
/**
|
|
572
|
+
/**
|
|
573
|
+
* POST /v1/batch: bulk import, up to 500 memory objects, processed
|
|
574
|
+
* asynchronously. Each object is the body of one `add()` (see
|
|
575
|
+
* `BatchMemory`): `content` and optionally `user_id`, `agent_id`, `run_id`,
|
|
576
|
+
* `metadata` and `timestamp`.
|
|
577
|
+
*
|
|
578
|
+
* `timestamp` means what it means on `add()`: when the events happened.
|
|
579
|
+
* The facts extracted from the item inherit it as `valid_from`, so a
|
|
580
|
+
* migration keeps its real dates; without it an item is dated when it is
|
|
581
|
+
* imported. A value that is not an ISO 8601 date or datetime refuses the
|
|
582
|
+
* whole batch before anything is queued: a 422 (APIError,
|
|
583
|
+
* `code === "invalid_request"`) whose message names the item, e.g.
|
|
584
|
+
* `memories[3].timestamp`. Any key not listed above is refused the same
|
|
585
|
+
* way. Servers older than 2026-09-28 refuse `timestamp` itself.
|
|
586
|
+
*/
|
|
437
587
|
async batch(memories) {
|
|
438
588
|
return this.request("POST", "/v1/batch", { body: { memories } });
|
|
439
589
|
}
|
|
440
|
-
/** GET /v1/batch/:id
|
|
590
|
+
/** GET /v1/batch/:id: poll an import job. */
|
|
441
591
|
async batchStatus(jobId) {
|
|
442
|
-
return this.request("GET", `/v1/batch/${jobId}`);
|
|
592
|
+
return this.request("GET", `/v1/batch/${seg(jobId, "jobId")}`);
|
|
443
593
|
}
|
|
444
594
|
};
|
|
445
595
|
export {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "korely-memory",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "JavaScript / TypeScript SDK for Korely Agents
|
|
3
|
+
"version": "0.1.8",
|
|
4
|
+
"description": "JavaScript / TypeScript SDK for Korely Agents: memory for AI agents with bi-temporal typed facts.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
7
7
|
"agents",
|
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
"llm",
|
|
10
10
|
"rag",
|
|
11
11
|
"knowledge-graph",
|
|
12
|
-
"typescript"
|
|
12
|
+
"typescript",
|
|
13
|
+
"vercel-ai-sdk",
|
|
14
|
+
"ai-sdk"
|
|
13
15
|
],
|
|
14
16
|
"homepage": "https://korely.ai/agents",
|
|
15
17
|
"repository": {
|
|
@@ -31,6 +33,18 @@
|
|
|
31
33
|
"types": "./dist/index.d.ts",
|
|
32
34
|
"import": "./dist/index.js",
|
|
33
35
|
"require": "./dist/index.cjs"
|
|
36
|
+
},
|
|
37
|
+
"./ai-sdk": {
|
|
38
|
+
"types": "./dist/ai-sdk.d.ts",
|
|
39
|
+
"import": "./dist/ai-sdk.js",
|
|
40
|
+
"require": "./dist/ai-sdk.cjs"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"typesVersions": {
|
|
44
|
+
"*": {
|
|
45
|
+
"ai-sdk": [
|
|
46
|
+
"./dist/ai-sdk.d.ts"
|
|
47
|
+
]
|
|
34
48
|
}
|
|
35
49
|
},
|
|
36
50
|
"files": [
|
|
@@ -48,8 +62,17 @@
|
|
|
48
62
|
"prepublishOnly": "npm run build"
|
|
49
63
|
},
|
|
50
64
|
"dependencies": {},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"ai": "^5.0.0 || ^6.0.0 || ^7.0.0"
|
|
67
|
+
},
|
|
68
|
+
"peerDependenciesMeta": {
|
|
69
|
+
"ai": {
|
|
70
|
+
"optional": true
|
|
71
|
+
}
|
|
72
|
+
},
|
|
51
73
|
"devDependencies": {
|
|
52
74
|
"@types/node": "^22.9.0",
|
|
75
|
+
"ai": "^7.0.127",
|
|
53
76
|
"tsup": "^8.3.5",
|
|
54
77
|
"typescript": "^5.6.3"
|
|
55
78
|
}
|