korely-memory 0.2.0 → 0.2.2
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 +18 -9
- package/dist/ai-sdk.cjs +1 -1
- package/dist/ai-sdk.d.cts +1 -1
- package/dist/ai-sdk.d.ts +1 -1
- package/dist/ai-sdk.js +2 -2
- package/dist/{chunk-6SD5RRK3.js → chunk-EQTA322A.js} +3 -1
- package/dist/{chunk-VT7GXOPS.js → chunk-UW36NLA6.js} +129 -35
- package/dist/index.cjs +128 -34
- package/dist/index.d.cts +94 -11
- package/dist/index.d.ts +94 -11
- package/dist/index.js +2 -2
- package/dist/index.node.cjs +128 -34
- package/dist/index.node.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -76,7 +76,7 @@ than `contextTimeoutMs` (5 s) to answer, the call goes on without memory,
|
|
|
76
76
|
`onError` is told, and an answer that arrives later is dropped.
|
|
77
77
|
|
|
78
78
|
Options: `userId` (required), `agentId`, `runId`, `client` (a configured
|
|
79
|
-
`Korely`, default `new Korely()`), `tokenBudget` (
|
|
79
|
+
`Korely`, default `new Korely()`), `tokenBudget` (1500, the API's own default), `contextTimeoutMs`
|
|
80
80
|
(5000; 0 waits for the client's own 30 s), `includeDate` (true), `timeZone`
|
|
81
81
|
("UTC"), `remember` (true), `waitUntil`, `onError`.
|
|
82
82
|
|
|
@@ -94,8 +94,8 @@ Every method maps to one REST endpoint.
|
|
|
94
94
|
|
|
95
95
|
| Method | Endpoint | |
|
|
96
96
|
|---|---|---|
|
|
97
|
-
| `add(content, opts?)` | `POST /v1/memories` | Write. `content` is a string or a list of chat messages. `opts.user_id` or `opts.agent_id` says whose memory it is (Korely 1.0.19 and later refuse neither). `opts.timestamp` (ISO) backfills the past: facts inherit it as `valid_from`. |
|
|
98
|
-
| `search(query, opts?)` | `POST /v1/memories/search` | Semantic search over memories; filter by `user_id`, `agent_id`, `run_id`, `metadata`; `min_score` (0 to 1) leaves out hits scored below it. |
|
|
97
|
+
| `add(content, opts?)` | `POST /v1/memories` | Write. `content` is a string or a list of chat messages. `opts.user_id` or `opts.agent_id` says whose memory it is (Korely 1.0.19 and later refuse neither). `opts.timestamp` (ISO) backfills the past: facts inherit it as `valid_from`. Every write carries an `Idempotency-Key` (`opts.idempotency_key`, else a new one), and one whose answer never came (a timeout, a failed connection) is sent once more with it, made once by Korely 1.0.27 and later (an older server ignores the key). No answer twice rejects with `KorelyError` `write_not_confirmed`, naming the key; `new Korely({ retryWrites: false })` sends once. |
|
|
98
|
+
| `search(query, opts?)` | `POST /v1/memories/search` | Semantic search over memories; filter by `user_id`, `agent_id`, `run_id`, `metadata`; `min_score` (0 to 1) leaves out hits scored below it. `types: ["memories", "facts"]` searches the facts true now too (`["facts"]` them only), and each hit says its `type`, a fact's with its `fact`; `full_text: true` adds the whole text as `content` (`snippet` is its first 280 characters). Korely 1.0.13 and later; sent only when given. |
|
|
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. |
|
|
@@ -112,10 +112,10 @@ Every method maps to one REST endpoint.
|
|
|
112
112
|
| `getProfile({ user_id, as_of?, known_as_of? })` | `GET /v1/profile` | The assembled profile of one end user. |
|
|
113
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. |
|
|
114
114
|
| `events(opts?)` | `GET /v1/events` | Which writes are still being extracted; `limit` up to 200. |
|
|
115
|
-
| `batch(memories)` | `POST /v1/batch` | Bulk import, for migrations. Each item takes `user_id` or `agent_id` and `timestamp`, as `add()` does. |
|
|
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
117
|
| `ping()` | `GET /v1/ping` | Check a key without spending anything (no scope, rate limit or quota): its `tier`, `region` and `scopes`. Both products. |
|
|
118
|
-
| `Korely.initAgent(agentCaller?, opts?)` | `POST /v1/agents/init` | Cloud only. Static, no key needed: sign up for a
|
|
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. |
|
|
121
121
|
| `iterAudit(opts?)` | `GET /v1/audit`, every page | `for await (const e of korely.iterAudit({ user_id }))`: an export, `page_size` events per request, `until` pinned when it starts. |
|
|
@@ -147,7 +147,8 @@ and the `message` of the REST error envelope (`{code, message}`; a self-hosted
|
|
|
147
147
|
install that answers FastAPI's `detail` is read the same way). The common
|
|
148
148
|
statuses also have their own subclass.
|
|
149
149
|
Everything subclasses `KorelyError`, which is also what a client-side problem
|
|
150
|
-
throws (no key, a connection error
|
|
150
|
+
throws (no key, a connection error or a timeout, with `code === "connection_error"`,
|
|
151
|
+
and a write left unconfirmed after its retry, `write_not_confirmed`).
|
|
151
152
|
|
|
152
153
|
```ts
|
|
153
154
|
import { Korely, APIError, QuotaExceededError } from "korely-memory";
|
|
@@ -179,7 +180,8 @@ of all of these, and everything else: 422, 503; a write that names neither
|
|
|
179
180
|
later, and this client sends only the ids each call names). Every `APIError` has
|
|
180
181
|
`retryAfter` when the server sent `Retry-After`: a rate limit, and the Cloud's
|
|
181
182
|
503 `writes_paused` (writes that need a model are paused until 00:00 UTC). The
|
|
182
|
-
SDK does not retry
|
|
183
|
+
SDK does not retry these: its one retry is of a write that got no answer at
|
|
184
|
+
all (see `add()`).
|
|
183
185
|
|
|
184
186
|
A call that is not a `GET` (`add`, `update`, `delete`...) is never redirected:
|
|
185
187
|
a 3xx rejects with an `APIError` of that status, its message naming the origin
|
|
@@ -194,6 +196,7 @@ new Korely({
|
|
|
194
196
|
apiKey: "kor_live_...", // or the KORELY_API_KEY env var
|
|
195
197
|
region: "eu", // the API, api.korely.ai: data stored in the EU
|
|
196
198
|
timeoutMs: 30000, // covers the whole request, response body included
|
|
199
|
+
retryWrites: true, // a write with no answer is sent once more, same key
|
|
197
200
|
});
|
|
198
201
|
```
|
|
199
202
|
|
|
@@ -205,8 +208,14 @@ default. A memory written to a Global project is read by Google's model.
|
|
|
205
208
|
In Node, `new Korely()` with no key and no `KORELY_API_KEY` reads what
|
|
206
209
|
`korely init` saved (the key and the server, in `~/.korely/config.json`, or
|
|
207
210
|
`$KORELY_CONFIG_HOME/config.json`), as the Python client does, with `import`
|
|
208
|
-
and with `require`, from Node 18.
|
|
209
|
-
|
|
211
|
+
and with `require`, from Node 18. `korely` is the command of the Python
|
|
212
|
+
package (`pip install korely-memory`); this package has none. An argument
|
|
213
|
+
comes first, then the environment, then that file. A browser or an edge
|
|
214
|
+
runtime has no such file. With no key from any of them it throws
|
|
215
|
+
`KorelyError` "No API key", which says to make a key once, by hand (a cURL
|
|
216
|
+
of `POST /v1/agents/init`, which it gives, or `Korely.initAgent()` run once:
|
|
217
|
+
each call makes a new account), then to set `KORELY_API_KEY`, and names the
|
|
218
|
+
config file it read.
|
|
210
219
|
|
|
211
220
|
### Your own server (Self-hosted)
|
|
212
221
|
|
package/dist/ai-sdk.cjs
CHANGED
|
@@ -46,13 +46,13 @@ function unknownOption(method, opts, known) {
|
|
|
46
46
|
}
|
|
47
47
|
return void 0;
|
|
48
48
|
}
|
|
49
|
+
var DEFAULT_TOKEN_BUDGET = 1500;
|
|
49
50
|
|
|
50
51
|
// src/ai-sdk.ts
|
|
51
52
|
var QUERY_MAX = 2e3;
|
|
52
53
|
var CONTENT_MAX = 16e3;
|
|
53
54
|
var USER_MAX = 6e3;
|
|
54
55
|
var TURN_OVERHEAD = 18;
|
|
55
|
-
var DEFAULT_TOKEN_BUDGET = 800;
|
|
56
56
|
var CONTEXT_REUSE_MS = 5 * 6e4;
|
|
57
57
|
var FAILURE_REUSE_MS = 3e4;
|
|
58
58
|
var DEFAULT_CONTEXT_TIMEOUT_MS = 5e3;
|
package/dist/ai-sdk.d.cts
CHANGED
|
@@ -47,7 +47,7 @@ interface KorelyToolsOptions {
|
|
|
47
47
|
runId?: string;
|
|
48
48
|
/** The client to use. Default: `new Korely()`, configured from the environment (KORELY_API_KEY, KORELY_BASE_URL). */
|
|
49
49
|
client?: Korely;
|
|
50
|
-
/** Token budget of the context block (`token_budget`). Default
|
|
50
|
+
/** Token budget of the context block (`token_budget`). Default 1500, the API's default since Korely 1.0.35. */
|
|
51
51
|
tokenBudget?: number;
|
|
52
52
|
/** Add a "Current date: YYYY-MM-DD" line. Default true: the reader answers better when its prompt carries the date. */
|
|
53
53
|
includeDate?: boolean;
|
package/dist/ai-sdk.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ interface KorelyToolsOptions {
|
|
|
47
47
|
runId?: string;
|
|
48
48
|
/** The client to use. Default: `new Korely()`, configured from the environment (KORELY_API_KEY, KORELY_BASE_URL). */
|
|
49
49
|
client?: Korely;
|
|
50
|
-
/** Token budget of the context block (`token_budget`). Default
|
|
50
|
+
/** Token budget of the context block (`token_budget`). Default 1500, the API's default since Korely 1.0.35. */
|
|
51
51
|
tokenBudget?: number;
|
|
52
52
|
/** Add a "Current date: YYYY-MM-DD" line. Default true: the reader answers better when its prompt carries the date. */
|
|
53
53
|
includeDate?: boolean;
|
package/dist/ai-sdk.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
+
DEFAULT_TOKEN_BUDGET,
|
|
2
3
|
keysOf,
|
|
3
4
|
unknownOption
|
|
4
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-EQTA322A.js";
|
|
5
6
|
|
|
6
7
|
// src/ai-sdk.ts
|
|
7
8
|
import { gateway, jsonSchema, tool, wrapLanguageModel } from "ai";
|
|
@@ -10,7 +11,6 @@ var QUERY_MAX = 2e3;
|
|
|
10
11
|
var CONTENT_MAX = 16e3;
|
|
11
12
|
var USER_MAX = 6e3;
|
|
12
13
|
var TURN_OVERHEAD = 18;
|
|
13
|
-
var DEFAULT_TOKEN_BUDGET = 800;
|
|
14
14
|
var CONTEXT_REUSE_MS = 5 * 6e4;
|
|
15
15
|
var FAILURE_REUSE_MS = 3e4;
|
|
16
16
|
var DEFAULT_CONTEXT_TIMEOUT_MS = 5e3;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
+
DEFAULT_TOKEN_BUDGET,
|
|
2
3
|
__require,
|
|
3
4
|
keysOf,
|
|
4
5
|
unknownOption
|
|
5
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-EQTA322A.js";
|
|
6
7
|
|
|
7
8
|
// src/errors.ts
|
|
8
9
|
var KorelyError = class extends Error {
|
|
@@ -77,11 +78,45 @@ var TooManyBatchesError = class _TooManyBatchesError extends QuotaExceededError
|
|
|
77
78
|
};
|
|
78
79
|
|
|
79
80
|
// src/client.ts
|
|
80
|
-
var VERSION = "0.2.
|
|
81
|
+
var VERSION = "0.2.2";
|
|
81
82
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
82
|
-
var CLIENT_KEYS = keysOf()(
|
|
83
|
+
var CLIENT_KEYS = keysOf()(
|
|
84
|
+
"apiKey",
|
|
85
|
+
"region",
|
|
86
|
+
"baseUrl",
|
|
87
|
+
"timeoutMs",
|
|
88
|
+
"fetch",
|
|
89
|
+
"client",
|
|
90
|
+
"retryWrites"
|
|
91
|
+
);
|
|
83
92
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
84
|
-
var ADD_KEYS = keysOf()(
|
|
93
|
+
var ADD_KEYS = keysOf()(
|
|
94
|
+
"agent_id",
|
|
95
|
+
"user_id",
|
|
96
|
+
"run_id",
|
|
97
|
+
"metadata",
|
|
98
|
+
"timestamp",
|
|
99
|
+
"kind",
|
|
100
|
+
"idempotency_key"
|
|
101
|
+
);
|
|
102
|
+
var WRITE_RETRY_DELAY_MS = 500;
|
|
103
|
+
function newIdempotencyKey() {
|
|
104
|
+
const c = globalThis.crypto;
|
|
105
|
+
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
106
|
+
const bytes = new Uint8Array(16);
|
|
107
|
+
if (c?.getRandomValues) c.getRandomValues(bytes);
|
|
108
|
+
else for (let i = 0; i < bytes.length; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
109
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
110
|
+
}
|
|
111
|
+
function idempotencyHeader(key) {
|
|
112
|
+
if (key === void 0) return void 0;
|
|
113
|
+
if (typeof key !== "string" || !/^[\x20-\x7e]{1,255}$/.test(key)) {
|
|
114
|
+
throw new KorelyError(
|
|
115
|
+
"idempotency_key must be 1 to 255 printable ASCII characters (space to ~), one per write: a UUID will do."
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
return { "Idempotency-Key": `"${key.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"` };
|
|
119
|
+
}
|
|
85
120
|
var SEARCH_KEYS = keysOf()(
|
|
86
121
|
"user_id",
|
|
87
122
|
"agent_id",
|
|
@@ -89,7 +124,9 @@ var SEARCH_KEYS = keysOf()(
|
|
|
89
124
|
"metadata",
|
|
90
125
|
"limit",
|
|
91
126
|
"min_score",
|
|
92
|
-
"exclude_sessions"
|
|
127
|
+
"exclude_sessions",
|
|
128
|
+
"types",
|
|
129
|
+
"full_text"
|
|
93
130
|
);
|
|
94
131
|
var LIST_KEYS = keysOf()("user_id", "agent_id", "run_id", "limit", "offset", "exclude_sessions");
|
|
95
132
|
var NAME_KEYS = keysOf()("user_id", "agent_id");
|
|
@@ -307,6 +344,7 @@ async function exchange(t, method, path, opts = {}) {
|
|
|
307
344
|
body = JSON.stringify(opts.body);
|
|
308
345
|
headers["Content-Type"] = "application/json";
|
|
309
346
|
}
|
|
347
|
+
Object.assign(headers, opts.headers);
|
|
310
348
|
const controller = new AbortController();
|
|
311
349
|
let timedOut = false;
|
|
312
350
|
const timer = setTimeout(() => {
|
|
@@ -314,7 +352,8 @@ async function exchange(t, method, path, opts = {}) {
|
|
|
314
352
|
controller.abort();
|
|
315
353
|
}, t.timeoutMs);
|
|
316
354
|
const failed = (e) => new KorelyError(
|
|
317
|
-
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}
|
|
355
|
+
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`,
|
|
356
|
+
{ code: "connection_error" }
|
|
318
357
|
);
|
|
319
358
|
let resp;
|
|
320
359
|
let text2;
|
|
@@ -412,17 +451,33 @@ function useNodeFiles(files) {
|
|
|
412
451
|
}
|
|
413
452
|
function savedConfig() {
|
|
414
453
|
try {
|
|
415
|
-
const
|
|
416
|
-
if (!
|
|
417
|
-
const
|
|
418
|
-
if (!files) return {};
|
|
419
|
-
const home = proc.env?.KORELY_CONFIG_HOME || files.join(files.homedir(), ".korely");
|
|
420
|
-
const data = JSON.parse(files.readFileSync(files.join(home, "config.json"), "utf8"));
|
|
454
|
+
const at = configFile();
|
|
455
|
+
if (!at) return {};
|
|
456
|
+
const data = JSON.parse(at.files.readFileSync(at.path, "utf8"));
|
|
421
457
|
return data && typeof data === "object" ? data : {};
|
|
422
458
|
} catch {
|
|
423
459
|
return {};
|
|
424
460
|
}
|
|
425
461
|
}
|
|
462
|
+
function configFile() {
|
|
463
|
+
const proc = typeof process !== "undefined" ? process : void 0;
|
|
464
|
+
if (!proc?.versions?.node) return void 0;
|
|
465
|
+
const files = nodeFiles ?? lookedUp(proc);
|
|
466
|
+
if (!files) return void 0;
|
|
467
|
+
const home = proc.env?.KORELY_CONFIG_HOME || files.join(files.homedir(), ".korely");
|
|
468
|
+
return { files, path: files.join(home, "config.json") };
|
|
469
|
+
}
|
|
470
|
+
function noKey() {
|
|
471
|
+
let file;
|
|
472
|
+
try {
|
|
473
|
+
file = configFile()?.path;
|
|
474
|
+
} catch {
|
|
475
|
+
file = void 0;
|
|
476
|
+
}
|
|
477
|
+
return new KorelyError(
|
|
478
|
+
`No API key. Pass { apiKey: 'kor_live_...' } or set KORELY_API_KEY. No key yet: make one once, by hand, on Korely Cloud: curl -X POST https://api.korely.ai/v1/agents/init -H 'Content-Type: application/json' -d '{"agent_caller": "your-name-here"}' (or Korely.initAgent() run once), then set KORELY_API_KEY. Each call makes a new account; the answer shows the key once and says when it ends. A Self-hosted install makes its keys in its dashboard.` + (file ? ` On Node a key saved by \`korely init\`, the command of the Python package (pip install korely-memory), is read too, from ${file}.` : "")
|
|
479
|
+
);
|
|
480
|
+
}
|
|
426
481
|
function lookedUp(proc) {
|
|
427
482
|
const builtin = (id) => typeof proc.getBuiltinModule === "function" ? proc.getBuiltinModule(id) : typeof __require === "function" ? __require(id) : void 0;
|
|
428
483
|
const fs = builtin("node:fs");
|
|
@@ -442,9 +497,7 @@ var Korely = class {
|
|
|
442
497
|
const saved = opts.apiKey && (opts.baseUrl || envBase) ? {} : savedConfig();
|
|
443
498
|
const key = opts.apiKey ?? envKey ?? text(saved.api_key);
|
|
444
499
|
if (!key) {
|
|
445
|
-
throw
|
|
446
|
-
"No API key. Pass { apiKey: 'kor_live_...' }, set KORELY_API_KEY, or run `korely init`, which saves one to ~/.korely/config.json."
|
|
447
|
-
);
|
|
500
|
+
throw noKey();
|
|
448
501
|
}
|
|
449
502
|
this.apiKey = key;
|
|
450
503
|
const envClient = typeof process !== "undefined" ? process.env?.KORELY_CLIENT : void 0;
|
|
@@ -452,6 +505,7 @@ var Korely = class {
|
|
|
452
505
|
this.baseUrl = (opts.baseUrl ?? envBase ?? text(saved.base_url) ?? REGIONS[opts.region ?? "eu"] ?? REGIONS.eu).replace(/\/+$/, "");
|
|
453
506
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
454
507
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
508
|
+
this.retryWrites = opts.retryWrites ?? true;
|
|
455
509
|
const f = opts.fetch ?? globalThis.fetch;
|
|
456
510
|
if (!f) {
|
|
457
511
|
throw new KorelyError(
|
|
@@ -461,13 +515,16 @@ var Korely = class {
|
|
|
461
515
|
this.fetchImpl = f;
|
|
462
516
|
}
|
|
463
517
|
/**
|
|
464
|
-
* POST /v1/agents/init, with no key: sign up for a
|
|
518
|
+
* POST /v1/agents/init, with no key: sign up for a key that costs nothing.
|
|
465
519
|
*
|
|
466
520
|
* The one call that runs without a key, because it is how one is obtained,
|
|
467
521
|
* so it is static: `await Korely.initAgent("my-app")`, then
|
|
468
522
|
* `new Korely({ apiKey: result.api_key })`. `agentCaller` is a free-form
|
|
469
523
|
* label of who signed up, kept for your reference. The answer carries the
|
|
470
|
-
* key, shown once (save it),
|
|
524
|
+
* key, shown once (save it), when it stops working (`expires_at`), the
|
|
525
|
+
* trial in words (`note`: its limits until the account is adopted, and how
|
|
526
|
+
* to adopt it), its `tier`, `region`, `scopes` and `quotas` (the monthly
|
|
527
|
+
* numbers once adopted).
|
|
471
528
|
* The account it creates has no login: the key is the account, and
|
|
472
529
|
* `deleteAccount({ confirm: "<the account's id>" })` closes it.
|
|
473
530
|
*
|
|
@@ -521,6 +578,40 @@ var Korely = class {
|
|
|
521
578
|
opts
|
|
522
579
|
);
|
|
523
580
|
}
|
|
581
|
+
/**
|
|
582
|
+
* POST a write that creates (`add`, `batch`) with an Idempotency-Key: the
|
|
583
|
+
* caller's, else one made here, the same on both tries. A try with no
|
|
584
|
+
* answer (`connection_error`) may have been made, so it is sent once more
|
|
585
|
+
* after WRITE_RETRY_DELAY_MS, unless `retryWrites` is false. Only a
|
|
586
|
+
* success of the retry tells what became of the first try: anything else it
|
|
587
|
+
* meets (no answer, a 409 `idempotency_key_in_use` for a first try still
|
|
588
|
+
* being made, a 5xx, a 429) leaves the write unconfirmed, as does a first
|
|
589
|
+
* try with no answer and no retry. That rejects with KorelyError
|
|
590
|
+
* `write_not_confirmed`, naming the key and the cause. Never a third try,
|
|
591
|
+
* never a first try the server answered, never a read. `how` is how a
|
|
592
|
+
* caller reads to look for it, for the message.
|
|
593
|
+
*/
|
|
594
|
+
async write(path, body, given, how) {
|
|
595
|
+
const key = given ?? newIdempotencyKey();
|
|
596
|
+
const headers = idempotencyHeader(key);
|
|
597
|
+
const tries = this.retryWrites ? 2 : 1;
|
|
598
|
+
const failed = [];
|
|
599
|
+
for (let attempt = 0; attempt < tries; attempt++) {
|
|
600
|
+
if (attempt) await new Promise((resolve) => setTimeout(resolve, WRITE_RETRY_DELAY_MS));
|
|
601
|
+
try {
|
|
602
|
+
return await this.request("POST", path, { body, headers });
|
|
603
|
+
} catch (e) {
|
|
604
|
+
if (!(e instanceof KorelyError) || attempt === 0 && e.code !== "connection_error") throw e;
|
|
605
|
+
failed.push(e);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
const causes = failed.map((e) => e.status ? `${e.message} (HTTP ${e.status})` : e.message);
|
|
609
|
+
const what = tries > 1 ? causes.join("; the retry with the same key: ") : `${causes[0]} (sent once: retryWrites is false)`;
|
|
610
|
+
throw new KorelyError(
|
|
611
|
+
`POST ${path} was not confirmed: ${what}. The write may have been made. Send it again with the same Idempotency-Key, ${key}: Korely 1.0.27 and later make it at most once within 24 hours. Or look for it first (${how}).`,
|
|
612
|
+
{ code: "write_not_confirmed" }
|
|
613
|
+
);
|
|
614
|
+
}
|
|
524
615
|
// ── the key and its account ───────────────────────────────────────────────
|
|
525
616
|
/**
|
|
526
617
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
@@ -723,23 +814,23 @@ var Korely = class {
|
|
|
723
814
|
"content is empty: pass a non-blank string or messages with content."
|
|
724
815
|
);
|
|
725
816
|
}
|
|
726
|
-
return this.
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
}
|
|
736
|
-
});
|
|
817
|
+
return this.write("/v1/memories", {
|
|
818
|
+
content: text2,
|
|
819
|
+
agent_id: opts.agent_id,
|
|
820
|
+
user_id: opts.user_id,
|
|
821
|
+
run_id: opts.run_id,
|
|
822
|
+
metadata: opts.metadata,
|
|
823
|
+
timestamp: opts.timestamp,
|
|
824
|
+
kind: opts.kind
|
|
825
|
+
}, opts.idempotency_key, "getAll() or search()");
|
|
737
826
|
}
|
|
738
827
|
/**
|
|
739
828
|
* POST /v1/memories/search: semantic search over raw memories, ranked by
|
|
740
|
-
* score (vector similarity to the query)
|
|
741
|
-
*
|
|
742
|
-
*
|
|
829
|
+
* score (vector similarity to the query); with `types`, over the facts too,
|
|
830
|
+
* or the facts only. `run_id` scopes to one session, `metadata` filters on
|
|
831
|
+
* what you stored at write time. `limit` defaults to the server default
|
|
832
|
+
* (15), max 50. `full_text: true` adds each result's whole text as
|
|
833
|
+
* `content`.
|
|
743
834
|
*/
|
|
744
835
|
async search(query, opts = {}) {
|
|
745
836
|
checkOptions("search", opts, SEARCH_KEYS);
|
|
@@ -753,7 +844,10 @@ var Korely = class {
|
|
|
753
844
|
limit: opts.limit,
|
|
754
845
|
min_score: opts.min_score,
|
|
755
846
|
// Sent only when true: an older server does not know the field.
|
|
756
|
-
exclude_sessions: opts.exclude_sessions ? true : void 0
|
|
847
|
+
exclude_sessions: opts.exclude_sessions ? true : void 0,
|
|
848
|
+
// Sent only when given, as `min_score`: Korely 1.0.13 and later.
|
|
849
|
+
types: opts.types,
|
|
850
|
+
full_text: opts.full_text ? true : void 0
|
|
757
851
|
}
|
|
758
852
|
});
|
|
759
853
|
return body.results ?? [];
|
|
@@ -1052,7 +1146,7 @@ var Korely = class {
|
|
|
1052
1146
|
query: o.query,
|
|
1053
1147
|
user_id: o.user_id,
|
|
1054
1148
|
agent_id: o.agent_id,
|
|
1055
|
-
token_budget: o.token_budget ??
|
|
1149
|
+
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1056
1150
|
known_as_of: o.known_as_of,
|
|
1057
1151
|
exclude_sessions: o.exclude_sessions ? "true" : void 0
|
|
1058
1152
|
}
|
|
@@ -1106,8 +1200,8 @@ var Korely = class {
|
|
|
1106
1200
|
* `too_many_batches` (TooManyBatchesError) while three batches are still
|
|
1107
1201
|
* being imported. The Self-hosted meters nothing and refuses neither.
|
|
1108
1202
|
*/
|
|
1109
|
-
async batch(memories) {
|
|
1110
|
-
return this.
|
|
1203
|
+
async batch(memories, opts = {}) {
|
|
1204
|
+
return this.write("/v1/batch", { memories }, opts.idempotency_key, "getAll() once the import has run");
|
|
1111
1205
|
}
|
|
1112
1206
|
/** GET /v1/batch/:id: poll an import job. */
|
|
1113
1207
|
async batchStatus(jobId) {
|