korely-memory 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -4
- 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-MZLXQMBY.js → chunk-E6ER37KI.js} +82 -23
- package/dist/{chunk-6SD5RRK3.js → chunk-EQTA322A.js} +3 -1
- package/dist/index.cjs +81 -22
- package/dist/index.d.cts +40 -8
- package/dist/index.d.ts +40 -8
- package/dist/index.js +2 -2
- package/dist/index.node.cjs +81 -22
- 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,7 +94,7 @@ 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`.
|
|
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
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. |
|
|
@@ -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
|
|
|
@@ -201,6 +204,9 @@ The data is stored in the EU (Helsinki). Which model reads a memory, and
|
|
|
201
204
|
where, is the project's region, chosen per project in the dashboard: Europe
|
|
202
205
|
(gpt-oss-120b on Scaleway, in Paris) or Global (Gemini, by Google), the
|
|
203
206
|
default. A memory written to a Global project is read by Google's model.
|
|
207
|
+
`region` names the API, and `"eu"` is the only one: any other value, `"global"`
|
|
208
|
+
among them, throws a `KorelyError`, even with `baseUrl` set (since 0.3.0; up to 0.2.2
|
|
209
|
+
it fell back to `"eu"` in silence).
|
|
204
210
|
|
|
205
211
|
In Node, `new Korely()` with no key and no `KORELY_API_KEY` reads what
|
|
206
212
|
`korely init` saved (the key and the server, in `~/.korely/config.json`, or
|
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,9 +78,26 @@ var TooManyBatchesError = class _TooManyBatchesError extends QuotaExceededError
|
|
|
77
78
|
};
|
|
78
79
|
|
|
79
80
|
// src/client.ts
|
|
80
|
-
var VERSION = "0.
|
|
81
|
+
var VERSION = "0.3.0";
|
|
81
82
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
82
|
-
|
|
83
|
+
function regionUrl(region = "eu") {
|
|
84
|
+
const url = typeof region === "string" && Object.prototype.hasOwnProperty.call(REGIONS, region) ? REGIONS[region] : void 0;
|
|
85
|
+
if (url === void 0) {
|
|
86
|
+
throw new KorelyError(
|
|
87
|
+
`region is "eu", the only API today, not ${JSON.stringify(region)}. Where a model reads the memories is the project's processing region: processingRegion in initAgent, or the project's region in the dashboard.`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return url;
|
|
91
|
+
}
|
|
92
|
+
var CLIENT_KEYS = keysOf()(
|
|
93
|
+
"apiKey",
|
|
94
|
+
"region",
|
|
95
|
+
"baseUrl",
|
|
96
|
+
"timeoutMs",
|
|
97
|
+
"fetch",
|
|
98
|
+
"client",
|
|
99
|
+
"retryWrites"
|
|
100
|
+
);
|
|
83
101
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
84
102
|
var ADD_KEYS = keysOf()(
|
|
85
103
|
"agent_id",
|
|
@@ -90,6 +108,15 @@ var ADD_KEYS = keysOf()(
|
|
|
90
108
|
"kind",
|
|
91
109
|
"idempotency_key"
|
|
92
110
|
);
|
|
111
|
+
var WRITE_RETRY_DELAY_MS = 500;
|
|
112
|
+
function newIdempotencyKey() {
|
|
113
|
+
const c = globalThis.crypto;
|
|
114
|
+
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
115
|
+
const bytes = new Uint8Array(16);
|
|
116
|
+
if (c?.getRandomValues) c.getRandomValues(bytes);
|
|
117
|
+
else for (let i = 0; i < bytes.length; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
118
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
119
|
+
}
|
|
93
120
|
function idempotencyHeader(key) {
|
|
94
121
|
if (key === void 0) return void 0;
|
|
95
122
|
if (typeof key !== "string" || !/^[\x20-\x7e]{1,255}$/.test(key)) {
|
|
@@ -334,7 +361,8 @@ async function exchange(t, method, path, opts = {}) {
|
|
|
334
361
|
controller.abort();
|
|
335
362
|
}, t.timeoutMs);
|
|
336
363
|
const failed = (e) => new KorelyError(
|
|
337
|
-
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}
|
|
364
|
+
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`,
|
|
365
|
+
{ code: "connection_error" }
|
|
338
366
|
);
|
|
339
367
|
let resp;
|
|
340
368
|
let text2;
|
|
@@ -483,9 +511,11 @@ var Korely = class {
|
|
|
483
511
|
this.apiKey = key;
|
|
484
512
|
const envClient = typeof process !== "undefined" ? process.env?.KORELY_CLIENT : void 0;
|
|
485
513
|
this.client = (opts.client ?? envClient ?? "").trim() || void 0;
|
|
486
|
-
|
|
514
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
515
|
+
this.baseUrl = (opts.baseUrl ?? envBase ?? text(saved.base_url) ?? hosted).replace(/\/+$/, "");
|
|
487
516
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
488
517
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
518
|
+
this.retryWrites = opts.retryWrites ?? true;
|
|
489
519
|
const f = opts.fetch ?? globalThis.fetch;
|
|
490
520
|
if (!f) {
|
|
491
521
|
throw new KorelyError(
|
|
@@ -527,7 +557,8 @@ var Korely = class {
|
|
|
527
557
|
);
|
|
528
558
|
}
|
|
529
559
|
const envBase = typeof process !== "undefined" ? process.env?.KORELY_BASE_URL : void 0;
|
|
530
|
-
const
|
|
560
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
561
|
+
const baseUrl = (opts.baseUrl ?? envBase ?? hosted).replace(/\/+$/, "");
|
|
531
562
|
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
532
563
|
if (!fetchImpl) {
|
|
533
564
|
throw new KorelyError(
|
|
@@ -558,6 +589,40 @@ var Korely = class {
|
|
|
558
589
|
opts
|
|
559
590
|
);
|
|
560
591
|
}
|
|
592
|
+
/**
|
|
593
|
+
* POST a write that creates (`add`, `batch`) with an Idempotency-Key: the
|
|
594
|
+
* caller's, else one made here, the same on both tries. A try with no
|
|
595
|
+
* answer (`connection_error`) may have been made, so it is sent once more
|
|
596
|
+
* after WRITE_RETRY_DELAY_MS, unless `retryWrites` is false. Only a
|
|
597
|
+
* success of the retry tells what became of the first try: anything else it
|
|
598
|
+
* meets (no answer, a 409 `idempotency_key_in_use` for a first try still
|
|
599
|
+
* being made, a 5xx, a 429) leaves the write unconfirmed, as does a first
|
|
600
|
+
* try with no answer and no retry. That rejects with KorelyError
|
|
601
|
+
* `write_not_confirmed`, naming the key and the cause. Never a third try,
|
|
602
|
+
* never a first try the server answered, never a read. `how` is how a
|
|
603
|
+
* caller reads to look for it, for the message.
|
|
604
|
+
*/
|
|
605
|
+
async write(path, body, given, how) {
|
|
606
|
+
const key = given ?? newIdempotencyKey();
|
|
607
|
+
const headers = idempotencyHeader(key);
|
|
608
|
+
const tries = this.retryWrites ? 2 : 1;
|
|
609
|
+
const failed = [];
|
|
610
|
+
for (let attempt = 0; attempt < tries; attempt++) {
|
|
611
|
+
if (attempt) await new Promise((resolve) => setTimeout(resolve, WRITE_RETRY_DELAY_MS));
|
|
612
|
+
try {
|
|
613
|
+
return await this.request("POST", path, { body, headers });
|
|
614
|
+
} catch (e) {
|
|
615
|
+
if (!(e instanceof KorelyError) || attempt === 0 && e.code !== "connection_error") throw e;
|
|
616
|
+
failed.push(e);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
const causes = failed.map((e) => e.status ? `${e.message} (HTTP ${e.status})` : e.message);
|
|
620
|
+
const what = tries > 1 ? causes.join("; the retry with the same key: ") : `${causes[0]} (sent once: retryWrites is false)`;
|
|
621
|
+
throw new KorelyError(
|
|
622
|
+
`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}).`,
|
|
623
|
+
{ code: "write_not_confirmed" }
|
|
624
|
+
);
|
|
625
|
+
}
|
|
561
626
|
// ── the key and its account ───────────────────────────────────────────────
|
|
562
627
|
/**
|
|
563
628
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
@@ -760,18 +825,15 @@ var Korely = class {
|
|
|
760
825
|
"content is empty: pass a non-blank string or messages with content."
|
|
761
826
|
);
|
|
762
827
|
}
|
|
763
|
-
return this.
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
},
|
|
773
|
-
headers: idempotencyHeader(opts.idempotency_key)
|
|
774
|
-
});
|
|
828
|
+
return this.write("/v1/memories", {
|
|
829
|
+
content: text2,
|
|
830
|
+
agent_id: opts.agent_id,
|
|
831
|
+
user_id: opts.user_id,
|
|
832
|
+
run_id: opts.run_id,
|
|
833
|
+
metadata: opts.metadata,
|
|
834
|
+
timestamp: opts.timestamp,
|
|
835
|
+
kind: opts.kind
|
|
836
|
+
}, opts.idempotency_key, "getAll() or search()");
|
|
775
837
|
}
|
|
776
838
|
/**
|
|
777
839
|
* POST /v1/memories/search: semantic search over raw memories, ranked by
|
|
@@ -1095,7 +1157,7 @@ var Korely = class {
|
|
|
1095
1157
|
query: o.query,
|
|
1096
1158
|
user_id: o.user_id,
|
|
1097
1159
|
agent_id: o.agent_id,
|
|
1098
|
-
token_budget: o.token_budget ??
|
|
1160
|
+
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1099
1161
|
known_as_of: o.known_as_of,
|
|
1100
1162
|
exclude_sessions: o.exclude_sessions ? "true" : void 0
|
|
1101
1163
|
}
|
|
@@ -1150,10 +1212,7 @@ var Korely = class {
|
|
|
1150
1212
|
* being imported. The Self-hosted meters nothing and refuses neither.
|
|
1151
1213
|
*/
|
|
1152
1214
|
async batch(memories, opts = {}) {
|
|
1153
|
-
return this.
|
|
1154
|
-
body: { memories },
|
|
1155
|
-
headers: idempotencyHeader(opts.idempotency_key)
|
|
1156
|
-
});
|
|
1215
|
+
return this.write("/v1/batch", { memories }, opts.idempotency_key, "getAll() once the import has run");
|
|
1157
1216
|
}
|
|
1158
1217
|
/** GET /v1/batch/:id: poll an import job. */
|
|
1159
1218
|
async batchStatus(jobId) {
|
package/dist/index.cjs
CHANGED
|
@@ -124,11 +124,29 @@ function unknownOption(method, opts, known) {
|
|
|
124
124
|
}
|
|
125
125
|
return void 0;
|
|
126
126
|
}
|
|
127
|
+
var DEFAULT_TOKEN_BUDGET = 1500;
|
|
127
128
|
|
|
128
129
|
// src/client.ts
|
|
129
|
-
var VERSION = "0.
|
|
130
|
+
var VERSION = "0.3.0";
|
|
130
131
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
131
|
-
|
|
132
|
+
function regionUrl(region = "eu") {
|
|
133
|
+
const url = typeof region === "string" && Object.prototype.hasOwnProperty.call(REGIONS, region) ? REGIONS[region] : void 0;
|
|
134
|
+
if (url === void 0) {
|
|
135
|
+
throw new KorelyError(
|
|
136
|
+
`region is "eu", the only API today, not ${JSON.stringify(region)}. Where a model reads the memories is the project's processing region: processingRegion in initAgent, or the project's region in the dashboard.`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
return url;
|
|
140
|
+
}
|
|
141
|
+
var CLIENT_KEYS = keysOf()(
|
|
142
|
+
"apiKey",
|
|
143
|
+
"region",
|
|
144
|
+
"baseUrl",
|
|
145
|
+
"timeoutMs",
|
|
146
|
+
"fetch",
|
|
147
|
+
"client",
|
|
148
|
+
"retryWrites"
|
|
149
|
+
);
|
|
132
150
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
133
151
|
var ADD_KEYS = keysOf()(
|
|
134
152
|
"agent_id",
|
|
@@ -139,6 +157,15 @@ var ADD_KEYS = keysOf()(
|
|
|
139
157
|
"kind",
|
|
140
158
|
"idempotency_key"
|
|
141
159
|
);
|
|
160
|
+
var WRITE_RETRY_DELAY_MS = 500;
|
|
161
|
+
function newIdempotencyKey() {
|
|
162
|
+
const c = globalThis.crypto;
|
|
163
|
+
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
164
|
+
const bytes = new Uint8Array(16);
|
|
165
|
+
if (c?.getRandomValues) c.getRandomValues(bytes);
|
|
166
|
+
else for (let i = 0; i < bytes.length; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
167
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
168
|
+
}
|
|
142
169
|
function idempotencyHeader(key) {
|
|
143
170
|
if (key === void 0) return void 0;
|
|
144
171
|
if (typeof key !== "string" || !/^[\x20-\x7e]{1,255}$/.test(key)) {
|
|
@@ -383,7 +410,8 @@ async function exchange(t, method, path, opts = {}) {
|
|
|
383
410
|
controller.abort();
|
|
384
411
|
}, t.timeoutMs);
|
|
385
412
|
const failed = (e) => new KorelyError(
|
|
386
|
-
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}
|
|
413
|
+
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`,
|
|
414
|
+
{ code: "connection_error" }
|
|
387
415
|
);
|
|
388
416
|
let resp;
|
|
389
417
|
let text2;
|
|
@@ -529,9 +557,11 @@ var Korely = class {
|
|
|
529
557
|
this.apiKey = key;
|
|
530
558
|
const envClient = typeof process !== "undefined" ? process.env?.KORELY_CLIENT : void 0;
|
|
531
559
|
this.client = (opts.client ?? envClient ?? "").trim() || void 0;
|
|
532
|
-
|
|
560
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
561
|
+
this.baseUrl = (opts.baseUrl ?? envBase ?? text(saved.base_url) ?? hosted).replace(/\/+$/, "");
|
|
533
562
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
534
563
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
564
|
+
this.retryWrites = opts.retryWrites ?? true;
|
|
535
565
|
const f = opts.fetch ?? globalThis.fetch;
|
|
536
566
|
if (!f) {
|
|
537
567
|
throw new KorelyError(
|
|
@@ -573,7 +603,8 @@ var Korely = class {
|
|
|
573
603
|
);
|
|
574
604
|
}
|
|
575
605
|
const envBase = typeof process !== "undefined" ? process.env?.KORELY_BASE_URL : void 0;
|
|
576
|
-
const
|
|
606
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
607
|
+
const baseUrl = (opts.baseUrl ?? envBase ?? hosted).replace(/\/+$/, "");
|
|
577
608
|
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
578
609
|
if (!fetchImpl) {
|
|
579
610
|
throw new KorelyError(
|
|
@@ -604,6 +635,40 @@ var Korely = class {
|
|
|
604
635
|
opts
|
|
605
636
|
);
|
|
606
637
|
}
|
|
638
|
+
/**
|
|
639
|
+
* POST a write that creates (`add`, `batch`) with an Idempotency-Key: the
|
|
640
|
+
* caller's, else one made here, the same on both tries. A try with no
|
|
641
|
+
* answer (`connection_error`) may have been made, so it is sent once more
|
|
642
|
+
* after WRITE_RETRY_DELAY_MS, unless `retryWrites` is false. Only a
|
|
643
|
+
* success of the retry tells what became of the first try: anything else it
|
|
644
|
+
* meets (no answer, a 409 `idempotency_key_in_use` for a first try still
|
|
645
|
+
* being made, a 5xx, a 429) leaves the write unconfirmed, as does a first
|
|
646
|
+
* try with no answer and no retry. That rejects with KorelyError
|
|
647
|
+
* `write_not_confirmed`, naming the key and the cause. Never a third try,
|
|
648
|
+
* never a first try the server answered, never a read. `how` is how a
|
|
649
|
+
* caller reads to look for it, for the message.
|
|
650
|
+
*/
|
|
651
|
+
async write(path, body, given, how) {
|
|
652
|
+
const key = given ?? newIdempotencyKey();
|
|
653
|
+
const headers = idempotencyHeader(key);
|
|
654
|
+
const tries = this.retryWrites ? 2 : 1;
|
|
655
|
+
const failed = [];
|
|
656
|
+
for (let attempt = 0; attempt < tries; attempt++) {
|
|
657
|
+
if (attempt) await new Promise((resolve) => setTimeout(resolve, WRITE_RETRY_DELAY_MS));
|
|
658
|
+
try {
|
|
659
|
+
return await this.request("POST", path, { body, headers });
|
|
660
|
+
} catch (e) {
|
|
661
|
+
if (!(e instanceof KorelyError) || attempt === 0 && e.code !== "connection_error") throw e;
|
|
662
|
+
failed.push(e);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
const causes = failed.map((e) => e.status ? `${e.message} (HTTP ${e.status})` : e.message);
|
|
666
|
+
const what = tries > 1 ? causes.join("; the retry with the same key: ") : `${causes[0]} (sent once: retryWrites is false)`;
|
|
667
|
+
throw new KorelyError(
|
|
668
|
+
`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}).`,
|
|
669
|
+
{ code: "write_not_confirmed" }
|
|
670
|
+
);
|
|
671
|
+
}
|
|
607
672
|
// ── the key and its account ───────────────────────────────────────────────
|
|
608
673
|
/**
|
|
609
674
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
@@ -806,18 +871,15 @@ var Korely = class {
|
|
|
806
871
|
"content is empty: pass a non-blank string or messages with content."
|
|
807
872
|
);
|
|
808
873
|
}
|
|
809
|
-
return this.
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
},
|
|
819
|
-
headers: idempotencyHeader(opts.idempotency_key)
|
|
820
|
-
});
|
|
874
|
+
return this.write("/v1/memories", {
|
|
875
|
+
content: text2,
|
|
876
|
+
agent_id: opts.agent_id,
|
|
877
|
+
user_id: opts.user_id,
|
|
878
|
+
run_id: opts.run_id,
|
|
879
|
+
metadata: opts.metadata,
|
|
880
|
+
timestamp: opts.timestamp,
|
|
881
|
+
kind: opts.kind
|
|
882
|
+
}, opts.idempotency_key, "getAll() or search()");
|
|
821
883
|
}
|
|
822
884
|
/**
|
|
823
885
|
* POST /v1/memories/search: semantic search over raw memories, ranked by
|
|
@@ -1141,7 +1203,7 @@ var Korely = class {
|
|
|
1141
1203
|
query: o.query,
|
|
1142
1204
|
user_id: o.user_id,
|
|
1143
1205
|
agent_id: o.agent_id,
|
|
1144
|
-
token_budget: o.token_budget ??
|
|
1206
|
+
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1145
1207
|
known_as_of: o.known_as_of,
|
|
1146
1208
|
exclude_sessions: o.exclude_sessions ? "true" : void 0
|
|
1147
1209
|
}
|
|
@@ -1196,10 +1258,7 @@ var Korely = class {
|
|
|
1196
1258
|
* being imported. The Self-hosted meters nothing and refuses neither.
|
|
1197
1259
|
*/
|
|
1198
1260
|
async batch(memories, opts = {}) {
|
|
1199
|
-
return this.
|
|
1200
|
-
body: { memories },
|
|
1201
|
-
headers: idempotencyHeader(opts.idempotency_key)
|
|
1202
|
-
});
|
|
1261
|
+
return this.write("/v1/batch", { memories }, opts.idempotency_key, "getAll() once the import has run");
|
|
1203
1262
|
}
|
|
1204
1263
|
/** GET /v1/batch/:id: poll an import job. */
|
|
1205
1264
|
async batchStatus(jobId) {
|
package/dist/index.d.cts
CHANGED
|
@@ -440,11 +440,14 @@ interface AddOptions {
|
|
|
440
440
|
*/
|
|
441
441
|
kind?: "note" | "session";
|
|
442
442
|
/**
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
443
|
+
* The same write sent again with the same key is made once (Korely Agent
|
|
444
|
+
* 1.0.27 and later; an older server ignores it). Without one the client
|
|
445
|
+
* makes one for this write (0.2.2). A write whose answer never came (a
|
|
446
|
+
* timeout, a connection that failed) may have been made: the client sends
|
|
447
|
+
* it once more with the same key (`retryWrites`), and if that does not
|
|
448
|
+
* succeed either (no answer, a 5xx, a 429) it rejects with KorelyError
|
|
449
|
+
* `write_not_confirmed`, naming the key and the cause. Pass your own key to send the write again later yourself: one
|
|
450
|
+
* new key per write, the same one only to send that write again.
|
|
448
451
|
*/
|
|
449
452
|
idempotency_key?: string;
|
|
450
453
|
}
|
|
@@ -604,6 +607,7 @@ interface GetContextOptions {
|
|
|
604
607
|
query: string;
|
|
605
608
|
user_id?: string;
|
|
606
609
|
agent_id?: string;
|
|
610
|
+
/** 50 to 8000 tokens. Default 1500, the API's default since Korely 1.0.35. */
|
|
607
611
|
token_budget?: number;
|
|
608
612
|
/**
|
|
609
613
|
* ISO date: the block as the store could have built it then, the facts it
|
|
@@ -625,7 +629,7 @@ interface ForgetReceipt {
|
|
|
625
629
|
|
|
626
630
|
/** Must equal `version` in package.json (a test checks). It goes out in the
|
|
627
631
|
* X-Korely-Client header, and said 0.1.1 from 0.1.1 to 0.1.6. */
|
|
628
|
-
declare const VERSION = "0.
|
|
632
|
+
declare const VERSION = "0.3.0";
|
|
629
633
|
interface KorelyOptions {
|
|
630
634
|
/** Your `kor_live_...` key. Falls back to the KORELY_API_KEY env var. */
|
|
631
635
|
apiKey?: string;
|
|
@@ -633,7 +637,8 @@ interface KorelyOptions {
|
|
|
633
637
|
* The API to call: "eu" (api.korely.ai), the only one today. The data is
|
|
634
638
|
* stored in the EU (Helsinki). The model that reads a memory is the
|
|
635
639
|
* project's region, set per project in the dashboard: Europe (gpt-oss-120b
|
|
636
|
-
* on Scaleway, in Paris) or Global (Gemini, by Google), the default.
|
|
640
|
+
* on Scaleway, in Paris) or Global (Gemini, by Google), the default. Any
|
|
641
|
+
* other value throws a KorelyError.
|
|
637
642
|
*/
|
|
638
643
|
region?: "eu";
|
|
639
644
|
/** Override the base URL (a self-hosted install, or testing). */
|
|
@@ -648,6 +653,16 @@ interface KorelyOptions {
|
|
|
648
653
|
* label no agent cap counts. Left out, the header names this library.
|
|
649
654
|
*/
|
|
650
655
|
client?: string;
|
|
656
|
+
/**
|
|
657
|
+
* A write (`add`, `batch`) whose answer never came, a timeout or a
|
|
658
|
+
* connection that failed, is sent once more, after half a second, with the
|
|
659
|
+
* same Idempotency-Key, which Korely 1.0.27 and later make once. Default
|
|
660
|
+
* true; false sends every write once. A retry that does not succeed (no
|
|
661
|
+
* answer again, a 5xx, a 429) leaves it unconfirmed. Either way a write left
|
|
662
|
+
* unconfirmed rejects with a KorelyError `write_not_confirmed` that names
|
|
663
|
+
* the key.
|
|
664
|
+
*/
|
|
665
|
+
retryWrites?: boolean;
|
|
651
666
|
}
|
|
652
667
|
/** Where and how `Korely.initAgent()` signs up: the client options a call
|
|
653
668
|
* without a key can use. */
|
|
@@ -664,6 +679,8 @@ declare class Korely {
|
|
|
664
679
|
readonly baseUrl: string;
|
|
665
680
|
readonly timeoutMs: number;
|
|
666
681
|
readonly client?: string;
|
|
682
|
+
/** Whether a write that got no answer is sent once more with its key. */
|
|
683
|
+
readonly retryWrites: boolean;
|
|
667
684
|
private readonly fetchImpl;
|
|
668
685
|
constructor(opts?: KorelyOptions);
|
|
669
686
|
/**
|
|
@@ -689,6 +706,20 @@ declare class Korely {
|
|
|
689
706
|
*/
|
|
690
707
|
static initAgent(agentCaller?: string, opts?: InitAgentOptions): Promise<AgentInitResult>;
|
|
691
708
|
private request;
|
|
709
|
+
/**
|
|
710
|
+
* POST a write that creates (`add`, `batch`) with an Idempotency-Key: the
|
|
711
|
+
* caller's, else one made here, the same on both tries. A try with no
|
|
712
|
+
* answer (`connection_error`) may have been made, so it is sent once more
|
|
713
|
+
* after WRITE_RETRY_DELAY_MS, unless `retryWrites` is false. Only a
|
|
714
|
+
* success of the retry tells what became of the first try: anything else it
|
|
715
|
+
* meets (no answer, a 409 `idempotency_key_in_use` for a first try still
|
|
716
|
+
* being made, a 5xx, a 429) leaves the write unconfirmed, as does a first
|
|
717
|
+
* try with no answer and no retry. That rejects with KorelyError
|
|
718
|
+
* `write_not_confirmed`, naming the key and the cause. Never a third try,
|
|
719
|
+
* never a first try the server answered, never a read. `how` is how a
|
|
720
|
+
* caller reads to look for it, for the message.
|
|
721
|
+
*/
|
|
722
|
+
private write;
|
|
692
723
|
/**
|
|
693
724
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
694
725
|
* it, with the same shape.
|
|
@@ -1006,7 +1037,8 @@ declare class Korely {
|
|
|
1006
1037
|
*
|
|
1007
1038
|
* A KorelyError that is not an APIError never reached the server, or got no
|
|
1008
1039
|
* usable answer: no API key, a key and address that do not belong together,
|
|
1009
|
-
* a connection error or a timeout
|
|
1040
|
+
* a connection error or a timeout (code `connection_error`; a write left
|
|
1041
|
+
* unconfirmed after its retry is `write_not_confirmed`, see `add()`).
|
|
1010
1042
|
*/
|
|
1011
1043
|
interface KorelyErrorOptions {
|
|
1012
1044
|
status?: number;
|
package/dist/index.d.ts
CHANGED
|
@@ -440,11 +440,14 @@ interface AddOptions {
|
|
|
440
440
|
*/
|
|
441
441
|
kind?: "note" | "session";
|
|
442
442
|
/**
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
443
|
+
* The same write sent again with the same key is made once (Korely Agent
|
|
444
|
+
* 1.0.27 and later; an older server ignores it). Without one the client
|
|
445
|
+
* makes one for this write (0.2.2). A write whose answer never came (a
|
|
446
|
+
* timeout, a connection that failed) may have been made: the client sends
|
|
447
|
+
* it once more with the same key (`retryWrites`), and if that does not
|
|
448
|
+
* succeed either (no answer, a 5xx, a 429) it rejects with KorelyError
|
|
449
|
+
* `write_not_confirmed`, naming the key and the cause. Pass your own key to send the write again later yourself: one
|
|
450
|
+
* new key per write, the same one only to send that write again.
|
|
448
451
|
*/
|
|
449
452
|
idempotency_key?: string;
|
|
450
453
|
}
|
|
@@ -604,6 +607,7 @@ interface GetContextOptions {
|
|
|
604
607
|
query: string;
|
|
605
608
|
user_id?: string;
|
|
606
609
|
agent_id?: string;
|
|
610
|
+
/** 50 to 8000 tokens. Default 1500, the API's default since Korely 1.0.35. */
|
|
607
611
|
token_budget?: number;
|
|
608
612
|
/**
|
|
609
613
|
* ISO date: the block as the store could have built it then, the facts it
|
|
@@ -625,7 +629,7 @@ interface ForgetReceipt {
|
|
|
625
629
|
|
|
626
630
|
/** Must equal `version` in package.json (a test checks). It goes out in the
|
|
627
631
|
* X-Korely-Client header, and said 0.1.1 from 0.1.1 to 0.1.6. */
|
|
628
|
-
declare const VERSION = "0.
|
|
632
|
+
declare const VERSION = "0.3.0";
|
|
629
633
|
interface KorelyOptions {
|
|
630
634
|
/** Your `kor_live_...` key. Falls back to the KORELY_API_KEY env var. */
|
|
631
635
|
apiKey?: string;
|
|
@@ -633,7 +637,8 @@ interface KorelyOptions {
|
|
|
633
637
|
* The API to call: "eu" (api.korely.ai), the only one today. The data is
|
|
634
638
|
* stored in the EU (Helsinki). The model that reads a memory is the
|
|
635
639
|
* project's region, set per project in the dashboard: Europe (gpt-oss-120b
|
|
636
|
-
* on Scaleway, in Paris) or Global (Gemini, by Google), the default.
|
|
640
|
+
* on Scaleway, in Paris) or Global (Gemini, by Google), the default. Any
|
|
641
|
+
* other value throws a KorelyError.
|
|
637
642
|
*/
|
|
638
643
|
region?: "eu";
|
|
639
644
|
/** Override the base URL (a self-hosted install, or testing). */
|
|
@@ -648,6 +653,16 @@ interface KorelyOptions {
|
|
|
648
653
|
* label no agent cap counts. Left out, the header names this library.
|
|
649
654
|
*/
|
|
650
655
|
client?: string;
|
|
656
|
+
/**
|
|
657
|
+
* A write (`add`, `batch`) whose answer never came, a timeout or a
|
|
658
|
+
* connection that failed, is sent once more, after half a second, with the
|
|
659
|
+
* same Idempotency-Key, which Korely 1.0.27 and later make once. Default
|
|
660
|
+
* true; false sends every write once. A retry that does not succeed (no
|
|
661
|
+
* answer again, a 5xx, a 429) leaves it unconfirmed. Either way a write left
|
|
662
|
+
* unconfirmed rejects with a KorelyError `write_not_confirmed` that names
|
|
663
|
+
* the key.
|
|
664
|
+
*/
|
|
665
|
+
retryWrites?: boolean;
|
|
651
666
|
}
|
|
652
667
|
/** Where and how `Korely.initAgent()` signs up: the client options a call
|
|
653
668
|
* without a key can use. */
|
|
@@ -664,6 +679,8 @@ declare class Korely {
|
|
|
664
679
|
readonly baseUrl: string;
|
|
665
680
|
readonly timeoutMs: number;
|
|
666
681
|
readonly client?: string;
|
|
682
|
+
/** Whether a write that got no answer is sent once more with its key. */
|
|
683
|
+
readonly retryWrites: boolean;
|
|
667
684
|
private readonly fetchImpl;
|
|
668
685
|
constructor(opts?: KorelyOptions);
|
|
669
686
|
/**
|
|
@@ -689,6 +706,20 @@ declare class Korely {
|
|
|
689
706
|
*/
|
|
690
707
|
static initAgent(agentCaller?: string, opts?: InitAgentOptions): Promise<AgentInitResult>;
|
|
691
708
|
private request;
|
|
709
|
+
/**
|
|
710
|
+
* POST a write that creates (`add`, `batch`) with an Idempotency-Key: the
|
|
711
|
+
* caller's, else one made here, the same on both tries. A try with no
|
|
712
|
+
* answer (`connection_error`) may have been made, so it is sent once more
|
|
713
|
+
* after WRITE_RETRY_DELAY_MS, unless `retryWrites` is false. Only a
|
|
714
|
+
* success of the retry tells what became of the first try: anything else it
|
|
715
|
+
* meets (no answer, a 409 `idempotency_key_in_use` for a first try still
|
|
716
|
+
* being made, a 5xx, a 429) leaves the write unconfirmed, as does a first
|
|
717
|
+
* try with no answer and no retry. That rejects with KorelyError
|
|
718
|
+
* `write_not_confirmed`, naming the key and the cause. Never a third try,
|
|
719
|
+
* never a first try the server answered, never a read. `how` is how a
|
|
720
|
+
* caller reads to look for it, for the message.
|
|
721
|
+
*/
|
|
722
|
+
private write;
|
|
692
723
|
/**
|
|
693
724
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
694
725
|
* it, with the same shape.
|
|
@@ -1006,7 +1037,8 @@ declare class Korely {
|
|
|
1006
1037
|
*
|
|
1007
1038
|
* A KorelyError that is not an APIError never reached the server, or got no
|
|
1008
1039
|
* usable answer: no API key, a key and address that do not belong together,
|
|
1009
|
-
* a connection error or a timeout
|
|
1040
|
+
* a connection error or a timeout (code `connection_error`; a write left
|
|
1041
|
+
* unconfirmed after its retry is `write_not_confirmed`, see `add()`).
|
|
1010
1042
|
*/
|
|
1011
1043
|
interface KorelyErrorOptions {
|
|
1012
1044
|
status?: number;
|
package/dist/index.js
CHANGED
package/dist/index.node.cjs
CHANGED
|
@@ -127,11 +127,29 @@ function unknownOption(method, opts, known) {
|
|
|
127
127
|
}
|
|
128
128
|
return void 0;
|
|
129
129
|
}
|
|
130
|
+
var DEFAULT_TOKEN_BUDGET = 1500;
|
|
130
131
|
|
|
131
132
|
// src/client.ts
|
|
132
|
-
var VERSION = "0.
|
|
133
|
+
var VERSION = "0.3.0";
|
|
133
134
|
var REGIONS = { eu: "https://api.korely.ai" };
|
|
134
|
-
|
|
135
|
+
function regionUrl(region = "eu") {
|
|
136
|
+
const url = typeof region === "string" && Object.prototype.hasOwnProperty.call(REGIONS, region) ? REGIONS[region] : void 0;
|
|
137
|
+
if (url === void 0) {
|
|
138
|
+
throw new KorelyError(
|
|
139
|
+
`region is "eu", the only API today, not ${JSON.stringify(region)}. Where a model reads the memories is the project's processing region: processingRegion in initAgent, or the project's region in the dashboard.`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
return url;
|
|
143
|
+
}
|
|
144
|
+
var CLIENT_KEYS = keysOf()(
|
|
145
|
+
"apiKey",
|
|
146
|
+
"region",
|
|
147
|
+
"baseUrl",
|
|
148
|
+
"timeoutMs",
|
|
149
|
+
"fetch",
|
|
150
|
+
"client",
|
|
151
|
+
"retryWrites"
|
|
152
|
+
);
|
|
135
153
|
var INIT_KEYS = keysOf()("region", "baseUrl", "timeoutMs", "fetch", "processingRegion");
|
|
136
154
|
var ADD_KEYS = keysOf()(
|
|
137
155
|
"agent_id",
|
|
@@ -142,6 +160,15 @@ var ADD_KEYS = keysOf()(
|
|
|
142
160
|
"kind",
|
|
143
161
|
"idempotency_key"
|
|
144
162
|
);
|
|
163
|
+
var WRITE_RETRY_DELAY_MS = 500;
|
|
164
|
+
function newIdempotencyKey() {
|
|
165
|
+
const c = globalThis.crypto;
|
|
166
|
+
if (c?.randomUUID) return c.randomUUID().replace(/-/g, "");
|
|
167
|
+
const bytes = new Uint8Array(16);
|
|
168
|
+
if (c?.getRandomValues) c.getRandomValues(bytes);
|
|
169
|
+
else for (let i = 0; i < bytes.length; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
170
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
171
|
+
}
|
|
145
172
|
function idempotencyHeader(key) {
|
|
146
173
|
if (key === void 0) return void 0;
|
|
147
174
|
if (typeof key !== "string" || !/^[\x20-\x7e]{1,255}$/.test(key)) {
|
|
@@ -386,7 +413,8 @@ async function exchange(t, method, path, opts = {}) {
|
|
|
386
413
|
controller.abort();
|
|
387
414
|
}, t.timeoutMs);
|
|
388
415
|
const failed = (e) => new KorelyError(
|
|
389
|
-
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}
|
|
416
|
+
timedOut ? `Request timed out after ${t.timeoutMs} ms (${method} ${path}).` : `Connection error: ${e?.message ?? String(e)}`,
|
|
417
|
+
{ code: "connection_error" }
|
|
390
418
|
);
|
|
391
419
|
let resp;
|
|
392
420
|
let text2;
|
|
@@ -535,9 +563,11 @@ var Korely = class {
|
|
|
535
563
|
this.apiKey = key;
|
|
536
564
|
const envClient = typeof process !== "undefined" ? process.env?.KORELY_CLIENT : void 0;
|
|
537
565
|
this.client = (opts.client ?? envClient ?? "").trim() || void 0;
|
|
538
|
-
|
|
566
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
567
|
+
this.baseUrl = (opts.baseUrl ?? envBase ?? text(saved.base_url) ?? hosted).replace(/\/+$/, "");
|
|
539
568
|
refuseAMismatchedPair(this.apiKey, this.baseUrl);
|
|
540
569
|
this.timeoutMs = opts.timeoutMs ?? 3e4;
|
|
570
|
+
this.retryWrites = opts.retryWrites ?? true;
|
|
541
571
|
const f = opts.fetch ?? globalThis.fetch;
|
|
542
572
|
if (!f) {
|
|
543
573
|
throw new KorelyError(
|
|
@@ -579,7 +609,8 @@ var Korely = class {
|
|
|
579
609
|
);
|
|
580
610
|
}
|
|
581
611
|
const envBase = typeof process !== "undefined" ? process.env?.KORELY_BASE_URL : void 0;
|
|
582
|
-
const
|
|
612
|
+
const hosted = regionUrl(opts.region ?? "eu");
|
|
613
|
+
const baseUrl = (opts.baseUrl ?? envBase ?? hosted).replace(/\/+$/, "");
|
|
583
614
|
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
584
615
|
if (!fetchImpl) {
|
|
585
616
|
throw new KorelyError(
|
|
@@ -610,6 +641,40 @@ var Korely = class {
|
|
|
610
641
|
opts
|
|
611
642
|
);
|
|
612
643
|
}
|
|
644
|
+
/**
|
|
645
|
+
* POST a write that creates (`add`, `batch`) with an Idempotency-Key: the
|
|
646
|
+
* caller's, else one made here, the same on both tries. A try with no
|
|
647
|
+
* answer (`connection_error`) may have been made, so it is sent once more
|
|
648
|
+
* after WRITE_RETRY_DELAY_MS, unless `retryWrites` is false. Only a
|
|
649
|
+
* success of the retry tells what became of the first try: anything else it
|
|
650
|
+
* meets (no answer, a 409 `idempotency_key_in_use` for a first try still
|
|
651
|
+
* being made, a 5xx, a 429) leaves the write unconfirmed, as does a first
|
|
652
|
+
* try with no answer and no retry. That rejects with KorelyError
|
|
653
|
+
* `write_not_confirmed`, naming the key and the cause. Never a third try,
|
|
654
|
+
* never a first try the server answered, never a read. `how` is how a
|
|
655
|
+
* caller reads to look for it, for the message.
|
|
656
|
+
*/
|
|
657
|
+
async write(path, body, given, how) {
|
|
658
|
+
const key = given ?? newIdempotencyKey();
|
|
659
|
+
const headers = idempotencyHeader(key);
|
|
660
|
+
const tries = this.retryWrites ? 2 : 1;
|
|
661
|
+
const failed = [];
|
|
662
|
+
for (let attempt = 0; attempt < tries; attempt++) {
|
|
663
|
+
if (attempt) await new Promise((resolve) => setTimeout(resolve, WRITE_RETRY_DELAY_MS));
|
|
664
|
+
try {
|
|
665
|
+
return await this.request("POST", path, { body, headers });
|
|
666
|
+
} catch (e) {
|
|
667
|
+
if (!(e instanceof KorelyError) || attempt === 0 && e.code !== "connection_error") throw e;
|
|
668
|
+
failed.push(e);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
const causes = failed.map((e) => e.status ? `${e.message} (HTTP ${e.status})` : e.message);
|
|
672
|
+
const what = tries > 1 ? causes.join("; the retry with the same key: ") : `${causes[0]} (sent once: retryWrites is false)`;
|
|
673
|
+
throw new KorelyError(
|
|
674
|
+
`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}).`,
|
|
675
|
+
{ code: "write_not_confirmed" }
|
|
676
|
+
);
|
|
677
|
+
}
|
|
613
678
|
// ── the key and its account ───────────────────────────────────────────────
|
|
614
679
|
/**
|
|
615
680
|
* GET /v1/ping: does this key work, and what may it do? Both products answer
|
|
@@ -812,18 +877,15 @@ var Korely = class {
|
|
|
812
877
|
"content is empty: pass a non-blank string or messages with content."
|
|
813
878
|
);
|
|
814
879
|
}
|
|
815
|
-
return this.
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
},
|
|
825
|
-
headers: idempotencyHeader(opts.idempotency_key)
|
|
826
|
-
});
|
|
880
|
+
return this.write("/v1/memories", {
|
|
881
|
+
content: text2,
|
|
882
|
+
agent_id: opts.agent_id,
|
|
883
|
+
user_id: opts.user_id,
|
|
884
|
+
run_id: opts.run_id,
|
|
885
|
+
metadata: opts.metadata,
|
|
886
|
+
timestamp: opts.timestamp,
|
|
887
|
+
kind: opts.kind
|
|
888
|
+
}, opts.idempotency_key, "getAll() or search()");
|
|
827
889
|
}
|
|
828
890
|
/**
|
|
829
891
|
* POST /v1/memories/search: semantic search over raw memories, ranked by
|
|
@@ -1147,7 +1209,7 @@ var Korely = class {
|
|
|
1147
1209
|
query: o.query,
|
|
1148
1210
|
user_id: o.user_id,
|
|
1149
1211
|
agent_id: o.agent_id,
|
|
1150
|
-
token_budget: o.token_budget ??
|
|
1212
|
+
token_budget: o.token_budget ?? DEFAULT_TOKEN_BUDGET,
|
|
1151
1213
|
known_as_of: o.known_as_of,
|
|
1152
1214
|
exclude_sessions: o.exclude_sessions ? "true" : void 0
|
|
1153
1215
|
}
|
|
@@ -1202,10 +1264,7 @@ var Korely = class {
|
|
|
1202
1264
|
* being imported. The Self-hosted meters nothing and refuses neither.
|
|
1203
1265
|
*/
|
|
1204
1266
|
async batch(memories, opts = {}) {
|
|
1205
|
-
return this.
|
|
1206
|
-
body: { memories },
|
|
1207
|
-
headers: idempotencyHeader(opts.idempotency_key)
|
|
1208
|
-
});
|
|
1267
|
+
return this.write("/v1/batch", { memories }, opts.idempotency_key, "getAll() once the import has run");
|
|
1209
1268
|
}
|
|
1210
1269
|
/** GET /v1/batch/:id: poll an import job. */
|
|
1211
1270
|
async batchStatus(jobId) {
|
package/dist/index.node.js
CHANGED