@llmnative/agentico 0.24.1 → 0.25.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/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +11 -8
- package/dist/index.mjs.map +1 -1
- package/dist/server.js +1 -1
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +271 -201
- package/dist/server.mjs.map +1 -1
- package/dist/types/backend/agenticoBackend.d.ts +2 -1
- package/dist/types/server/index.d.ts +2 -2
- package/dist/types/server/memories.d.ts +39 -5
- package/dist/types/server/server.d.ts +12 -5
- package/package.json +1 -1
package/dist/server.mjs
CHANGED
|
@@ -164,31 +164,31 @@ function S(e) {
|
|
|
164
164
|
}
|
|
165
165
|
//#endregion
|
|
166
166
|
//#region src/server/memories.ts
|
|
167
|
-
var C = "agentico_memories", w = class extends Error {
|
|
167
|
+
var C = "agentico_memories", w = (e, t) => e.platform || !!t.owners?.includes(e.namespace), T = class extends Error {
|
|
168
168
|
code;
|
|
169
169
|
constructor(e, t) {
|
|
170
170
|
super(t), this.name = "MemoryError", this.code = e;
|
|
171
171
|
}
|
|
172
|
-
},
|
|
173
|
-
function
|
|
172
|
+
}, E = (e) => `memory/${e}/key`, ee = /^[A-Za-z0-9_.:-]{1,120}$/;
|
|
173
|
+
function D(e, t) {
|
|
174
174
|
let n = String(e ?? "").trim().replace(/\/+$/, "");
|
|
175
175
|
if (!n) {
|
|
176
|
-
if (t) throw new
|
|
176
|
+
if (t) throw new T("invalid", "URL_REQUIRED");
|
|
177
177
|
return;
|
|
178
178
|
}
|
|
179
179
|
let r;
|
|
180
180
|
try {
|
|
181
181
|
r = new URL(n);
|
|
182
182
|
} catch {
|
|
183
|
-
throw new
|
|
183
|
+
throw new T("invalid", "INVALID_URL");
|
|
184
184
|
}
|
|
185
|
-
if (r.protocol !== "https:" && !(r.protocol === "http:" && ["localhost", "127.0.0.1"].includes(r.hostname))) throw new
|
|
185
|
+
if (r.protocol !== "https:" && !(r.protocol === "http:" && ["localhost", "127.0.0.1"].includes(r.hostname))) throw new T("invalid", "INVALID_URL");
|
|
186
186
|
return n;
|
|
187
187
|
}
|
|
188
|
-
function
|
|
188
|
+
function O(e) {
|
|
189
189
|
let { records: t, secrets: n } = e, r = e.fetch ?? fetch, i = e.newId ?? (() => crypto.randomUUID().replace(/-/g, "").slice(0, 12)), a = e.now ?? Date.now, o = async (e) => {
|
|
190
190
|
let n = await t.get(C, e);
|
|
191
|
-
if (!n) throw new
|
|
191
|
+
if (!n) throw new T("not-found", "MEMORY_NOT_FOUND");
|
|
192
192
|
return n;
|
|
193
193
|
}, s = async (e, t, n, i, a) => {
|
|
194
194
|
try {
|
|
@@ -203,23 +203,28 @@ function D(e) {
|
|
|
203
203
|
signal: AbortSignal.timeout(2e4)
|
|
204
204
|
});
|
|
205
205
|
} catch {
|
|
206
|
-
throw new
|
|
206
|
+
throw new T("unreachable", "MEMORY_UNREACHABLE");
|
|
207
207
|
}
|
|
208
208
|
}, c = (e) => `/ns/${encodeURIComponent(e)}`, l = async (e) => {
|
|
209
|
-
let t = await o(e), r = await n.get(
|
|
210
|
-
if (!r) throw new
|
|
209
|
+
let t = await o(e), r = await n.get(E(e));
|
|
210
|
+
if (!r) throw new T("not-found", "KEY_NOT_FOUND");
|
|
211
211
|
return {
|
|
212
212
|
record: t,
|
|
213
213
|
key: r
|
|
214
214
|
};
|
|
215
215
|
}, u = {
|
|
216
|
-
async list() {
|
|
217
|
-
return (await t.list(C)).map(({ createdAt: e, ...t }) => t).sort((e, t) => e.name.localeCompare(t.name));
|
|
216
|
+
async list(e) {
|
|
217
|
+
return (await t.list(C)).filter((t) => !e || w(e, t)).map(({ createdAt: e, ...t }) => t).sort((e, t) => e.name.localeCompare(t.name));
|
|
218
|
+
},
|
|
219
|
+
async of(e, t) {
|
|
220
|
+
let n = await o(e);
|
|
221
|
+
if (t && !w(t, n)) throw new T("not-found", "MEMORY_NOT_FOUND");
|
|
222
|
+
return n;
|
|
218
223
|
},
|
|
219
224
|
async check(e) {
|
|
220
225
|
let t;
|
|
221
226
|
try {
|
|
222
|
-
t =
|
|
227
|
+
t = D(e.apiUrl, !0);
|
|
223
228
|
} catch {
|
|
224
229
|
return {
|
|
225
230
|
ok: !1,
|
|
@@ -247,53 +252,86 @@ function D(e) {
|
|
|
247
252
|
};
|
|
248
253
|
}
|
|
249
254
|
},
|
|
250
|
-
async add(e) {
|
|
251
|
-
let
|
|
252
|
-
if ((await t.list("agentico_memories")).some((e) => e.apiUrl ===
|
|
253
|
-
let
|
|
254
|
-
apiUrl:
|
|
255
|
+
async add(e, r) {
|
|
256
|
+
let o = D(e.apiUrl, !0), s = D(e.mcpUrl, !1), c = (e) => r ? !!e.owners?.includes(r) : !e.owners?.length;
|
|
257
|
+
if ((await t.list("agentico_memories")).some((e) => e.apiUrl === o && c(e))) throw new T("conflict", "MEMORY_ALREADY_CONNECTED");
|
|
258
|
+
let l = await u.check({
|
|
259
|
+
apiUrl: o,
|
|
255
260
|
key: e.key
|
|
256
261
|
});
|
|
257
|
-
if (!
|
|
258
|
-
let
|
|
259
|
-
await n.set(
|
|
260
|
-
let
|
|
261
|
-
id:
|
|
262
|
-
name: String(e.name ?? "").trim().slice(0, 100) || new URL(
|
|
263
|
-
apiUrl:
|
|
264
|
-
...
|
|
262
|
+
if (!l.ok) throw new T(l.error === "invalid" ? "invalid" : l.error, l.error.toUpperCase());
|
|
263
|
+
let d = i();
|
|
264
|
+
await n.set(E(d), e.key.trim());
|
|
265
|
+
let f = {
|
|
266
|
+
id: d,
|
|
267
|
+
name: String(e.name ?? "").trim().slice(0, 100) || new URL(o).hostname,
|
|
268
|
+
apiUrl: o,
|
|
269
|
+
...s ? { mcpUrl: s } : {},
|
|
270
|
+
...r ? { owners: [r] } : {},
|
|
265
271
|
createdAt: a()
|
|
266
272
|
};
|
|
267
|
-
await t.put(C,
|
|
268
|
-
let { createdAt:
|
|
269
|
-
return
|
|
273
|
+
await t.put(C, f);
|
|
274
|
+
let { createdAt: p, ...m } = f;
|
|
275
|
+
return m;
|
|
270
276
|
},
|
|
271
|
-
async update(e, r) {
|
|
272
|
-
let
|
|
277
|
+
async update(e, r, i) {
|
|
278
|
+
let a = await u.of(e, i);
|
|
273
279
|
if (r.key?.trim()) {
|
|
274
280
|
let t = await u.check({
|
|
275
|
-
apiUrl:
|
|
281
|
+
apiUrl: a.apiUrl,
|
|
276
282
|
key: r.key
|
|
277
283
|
});
|
|
278
|
-
if (!t.ok) throw new
|
|
279
|
-
await n.set(
|
|
284
|
+
if (!t.ok) throw new T(t.error === "invalid" ? "invalid" : t.error, t.error.toUpperCase());
|
|
285
|
+
await n.set(E(e), r.key.trim());
|
|
280
286
|
}
|
|
281
|
-
let
|
|
282
|
-
typeof r.name == "string" && r.name.trim() && (
|
|
283
|
-
let { createdAt: s, ...c } =
|
|
287
|
+
let o = { ...a };
|
|
288
|
+
typeof r.name == "string" && r.name.trim() && (o.name = r.name.trim().slice(0, 100)), r.mcpUrl === null || r.mcpUrl === "" ? delete o.mcpUrl : typeof r.mcpUrl == "string" && (o.mcpUrl = D(r.mcpUrl, !1)), await t.put(C, o);
|
|
289
|
+
let { createdAt: s, ...c } = o;
|
|
284
290
|
return c;
|
|
285
291
|
},
|
|
286
|
-
async remove(e, r) {
|
|
287
|
-
|
|
288
|
-
|
|
292
|
+
async remove(e, r, i) {
|
|
293
|
+
let a = await u.of(e, i);
|
|
294
|
+
if (i && !i.platform && (a.owners?.length ?? 0) > 1) {
|
|
295
|
+
await t.put(C, {
|
|
296
|
+
...a,
|
|
297
|
+
owners: a.owners.filter((e) => e !== i.namespace)
|
|
298
|
+
});
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (r) throw new T("conflict", "MEMORY_IN_USE");
|
|
302
|
+
await n.delete(E(e)), await t.delete(C, e);
|
|
303
|
+
},
|
|
304
|
+
async share(e, n) {
|
|
305
|
+
let r = await o(e);
|
|
306
|
+
r.owners?.includes(n) || await t.put(C, {
|
|
307
|
+
...r,
|
|
308
|
+
owners: [...r.owners ?? [], n]
|
|
309
|
+
});
|
|
310
|
+
},
|
|
311
|
+
async unshare(e, n) {
|
|
312
|
+
let r = await o(e);
|
|
313
|
+
r.owners?.includes(n) && await t.put(C, {
|
|
314
|
+
...r,
|
|
315
|
+
owners: r.owners.filter((e) => e !== n)
|
|
316
|
+
});
|
|
317
|
+
},
|
|
318
|
+
async put(e, r) {
|
|
319
|
+
r && await n.set(E(e.id), r.trim()), await t.put(C, {
|
|
320
|
+
...e,
|
|
321
|
+
apiUrl: D(e.apiUrl, !0),
|
|
322
|
+
createdAt: e.createdAt ?? a()
|
|
323
|
+
});
|
|
324
|
+
},
|
|
325
|
+
async keyOf(e) {
|
|
326
|
+
return n.get(E(e));
|
|
289
327
|
},
|
|
290
328
|
async search(e, t, n, r = 10) {
|
|
291
|
-
if (!String(n ?? "").trim()) throw new
|
|
329
|
+
if (!String(n ?? "").trim()) throw new T("invalid", "QUERY_REQUIRED");
|
|
292
330
|
let { record: i, key: a } = await l(e), o = await s(i.apiUrl, a, "POST", `${c(t)}/search`, {
|
|
293
331
|
query: n.trim().slice(0, 2e3),
|
|
294
332
|
limit: Math.min(Math.max(Math.trunc(r) || 10, 1), 50)
|
|
295
333
|
});
|
|
296
|
-
if (!o.ok) throw new
|
|
334
|
+
if (!o.ok) throw new T(o.status === 401 || o.status === 403 ? "rejected" : "unreachable", `MEMORY_${o.status}`);
|
|
297
335
|
return ((await o.json().catch(() => ({}))).results ?? []).map((e) => {
|
|
298
336
|
let t = e.metadata && typeof e.metadata == "object" ? e.metadata : void 0;
|
|
299
337
|
return {
|
|
@@ -307,8 +345,8 @@ function D(e) {
|
|
|
307
345
|
},
|
|
308
346
|
async write(e, t, n) {
|
|
309
347
|
let r = String(n.title ?? "").trim().slice(0, 200), i = String(n.content ?? "").trim();
|
|
310
|
-
if (!r || !i || i.length > 2e5) throw new
|
|
311
|
-
if (n.documentId !== void 0 && !ee.test(n.documentId)) throw new
|
|
348
|
+
if (!r || !i || i.length > 2e5) throw new T("invalid", "DOCUMENT_INVALID");
|
|
349
|
+
if (n.documentId !== void 0 && !ee.test(n.documentId)) throw new T("invalid", "DOCUMENT_ID_INVALID");
|
|
312
350
|
let { record: a, key: o } = await l(e), u = {
|
|
313
351
|
content: `# ${r}\n\n${i}`,
|
|
314
352
|
metadata: {
|
|
@@ -319,13 +357,13 @@ function D(e) {
|
|
|
319
357
|
if (n.documentId) {
|
|
320
358
|
let e = await s(a.apiUrl, o, "PATCH", `${c(t)}/document/${encodeURIComponent(n.documentId)}`, u);
|
|
321
359
|
if (e.ok) return { id: n.documentId };
|
|
322
|
-
if (e.status !== 404) throw new
|
|
360
|
+
if (e.status !== 404) throw new T(e.status === 401 || e.status === 403 ? "rejected" : "unreachable", `MEMORY_${e.status}`);
|
|
323
361
|
}
|
|
324
362
|
let d = await s(a.apiUrl, o, "POST", `${c(t)}/document`, {
|
|
325
363
|
...u,
|
|
326
364
|
...n.documentId ? { id: n.documentId } : {}
|
|
327
365
|
});
|
|
328
|
-
if (!d.ok) throw new
|
|
366
|
+
if (!d.ok) throw new T(d.status === 401 || d.status === 403 ? "rejected" : "unreachable", `MEMORY_${d.status}`);
|
|
329
367
|
let f = await d.json().catch(() => ({}));
|
|
330
368
|
return typeof f.id == "string" ? { id: f.id } : {};
|
|
331
369
|
},
|
|
@@ -342,80 +380,80 @@ function D(e) {
|
|
|
342
380
|
}
|
|
343
381
|
//#endregion
|
|
344
382
|
//#region src/server/namespace.ts
|
|
345
|
-
var
|
|
383
|
+
var k = /^[a-z0-9]{1,32}$/, A = /^[a-z0-9][a-z0-9-]{0,29}$/, j = class extends Error {
|
|
346
384
|
constructor(e) {
|
|
347
385
|
super(e), this.name = "NamespaceError";
|
|
348
386
|
}
|
|
349
387
|
};
|
|
350
|
-
function
|
|
351
|
-
if (!
|
|
388
|
+
function M(e, t) {
|
|
389
|
+
if (!k.test(e)) throw new j(`invalid project "${e}"`);
|
|
352
390
|
if (t === void 0 || t === "") return e;
|
|
353
|
-
if (!
|
|
391
|
+
if (!A.test(t)) throw new j(`invalid tenant "${t}"`);
|
|
354
392
|
return `${e}-${t}`;
|
|
355
393
|
}
|
|
356
|
-
var
|
|
394
|
+
var N = (e, t) => t === e || t.startsWith(`${e}-`), P = (e, t) => t === e ? void 0 : t.slice(e.length + 1), F = "agentico_runners", I = "agentico_runner_pairings", L = "agentico_namespaces", R = "agentico_runner_chats", z = "agentico_runner_installs", B = class extends Error {
|
|
357
395
|
code;
|
|
358
396
|
constructor(e, t) {
|
|
359
397
|
super(t), this.name = "FleetError", this.code = e;
|
|
360
398
|
}
|
|
361
|
-
},
|
|
399
|
+
}, V = [
|
|
362
400
|
"complete",
|
|
363
401
|
"chat",
|
|
364
402
|
"chat:tools",
|
|
365
403
|
"accounts",
|
|
366
404
|
"act-as-user"
|
|
367
|
-
],
|
|
368
|
-
function
|
|
405
|
+
], H = [...V, "accounts:tenant"], U = (e) => `runner/${e}/platform`, W = (e) => `runner-pairing/${e}`, G = (e, t) => `namespace/${e}/runner/${t}`;
|
|
406
|
+
function K(e) {
|
|
369
407
|
let t;
|
|
370
408
|
try {
|
|
371
409
|
t = new URL(e.trim().replace(/\/+$/, ""));
|
|
372
410
|
} catch {
|
|
373
|
-
throw new
|
|
411
|
+
throw new B("invalid", "INVALID_URL");
|
|
374
412
|
}
|
|
375
|
-
if (t.protocol !== "https:" || t.pathname !== "/" || t.search || t.hash) throw new
|
|
413
|
+
if (t.protocol !== "https:" || t.pathname !== "/" || t.search || t.hash) throw new B("invalid", "INVALID_URL");
|
|
376
414
|
return t.origin;
|
|
377
415
|
}
|
|
378
|
-
var
|
|
416
|
+
var q = (e) => ({
|
|
379
417
|
id: e.id,
|
|
380
418
|
name: e.name,
|
|
381
419
|
url: e.url,
|
|
382
420
|
isDefault: e.isDefault,
|
|
383
421
|
draining: e.draining
|
|
384
422
|
});
|
|
385
|
-
function
|
|
423
|
+
function J(t) {
|
|
386
424
|
let { project: n, records: r, secrets: i } = t, a = t.connect ?? ((t, n) => new e({
|
|
387
425
|
url: t,
|
|
388
426
|
...n ? { key: n } : {}
|
|
389
427
|
})), o = t.install ?? c, s = t.newId ?? (() => crypto.randomUUID().replace(/-/g, "").slice(0, 12)), l = t.now ?? Date.now, u = async (e) => {
|
|
390
|
-
let t = await r.get(
|
|
391
|
-
if (!t) throw new
|
|
428
|
+
let t = await r.get(F, e);
|
|
429
|
+
if (!t) throw new B("not-found", "RUNNER_NOT_FOUND");
|
|
392
430
|
return t;
|
|
393
431
|
}, d = async (e) => {
|
|
394
432
|
let t = await i.get(e);
|
|
395
|
-
if (!t) throw new
|
|
433
|
+
if (!t) throw new B("not-found", "KEY_NOT_FOUND");
|
|
396
434
|
return t;
|
|
397
|
-
}, f = async (e) => a(e.url, await d(
|
|
398
|
-
let t =
|
|
399
|
-
if ((await r.list("agentico_runners")).some((e) => e.url === t)) throw new
|
|
435
|
+
}, f = async (e) => a(e.url, await d(U(e.id))), p = async (e) => {
|
|
436
|
+
let t = K(e);
|
|
437
|
+
if ((await r.list("agentico_runners")).some((e) => e.url === t)) throw new B("conflict", "RUNNER_ALREADY_CONNECTED");
|
|
400
438
|
return t;
|
|
401
439
|
}, m = (e, t) => String(t ?? "").trim() || new URL(e).hostname, h = async (e) => {
|
|
402
440
|
let t = s(), n = l();
|
|
403
|
-
await i.set(
|
|
441
|
+
await i.set(U(t), e.key);
|
|
404
442
|
let a = {
|
|
405
443
|
id: t,
|
|
406
444
|
name: e.name,
|
|
407
445
|
url: e.url,
|
|
408
|
-
isDefault: (await r.list(
|
|
446
|
+
isDefault: (await r.list(F)).length === 0,
|
|
409
447
|
draining: !1,
|
|
410
448
|
...e.keyId ? { keyId: e.keyId } : {},
|
|
411
449
|
...e.hostFingerprint ? { hostFingerprint: e.hostFingerprint } : {},
|
|
412
450
|
createdAt: n,
|
|
413
451
|
updatedAt: n
|
|
414
452
|
};
|
|
415
|
-
return await r.put(
|
|
453
|
+
return await r.put(F, a), q(a);
|
|
416
454
|
}, g = async (e) => await r.get("agentico_namespaces", e) ?? { id: e }, _ = {
|
|
417
455
|
async list() {
|
|
418
|
-
return (await r.list(
|
|
456
|
+
return (await r.list(F)).map(q).sort((e, t) => e.name.localeCompare(t.name));
|
|
419
457
|
},
|
|
420
458
|
async add(e, r = {}) {
|
|
421
459
|
let i = await p(String(e.url ?? "")), o = m(i, e.name);
|
|
@@ -438,11 +476,11 @@ function q(t) {
|
|
|
438
476
|
...r
|
|
439
477
|
});
|
|
440
478
|
}
|
|
441
|
-
throw new
|
|
479
|
+
throw new B("invalid", "CODE_OR_KEY_REQUIRED");
|
|
442
480
|
},
|
|
443
481
|
async startPairing(e, o) {
|
|
444
482
|
let c = await p(String(e.url ?? "")), l = await a(c).requestPairing(t.platformName, n), u = s();
|
|
445
|
-
return await i.set(
|
|
483
|
+
return await i.set(W(u), l.request), await r.put(I, {
|
|
446
484
|
id: u,
|
|
447
485
|
name: m(c, e.name),
|
|
448
486
|
url: c,
|
|
@@ -456,8 +494,8 @@ function q(t) {
|
|
|
456
494
|
};
|
|
457
495
|
},
|
|
458
496
|
async pairingStatus(e, t) {
|
|
459
|
-
let n = await r.get(
|
|
460
|
-
await r.delete(
|
|
497
|
+
let n = await r.get(I, e), o = await i.get(W(e)), s = async () => {
|
|
498
|
+
await r.delete(I, e), await i.delete(W(e));
|
|
461
499
|
};
|
|
462
500
|
if (!n || n.startedBy !== t || !o) return { status: "expired" };
|
|
463
501
|
if (Date.parse(n.expiresAt) <= l()) return await s(), { status: "expired" };
|
|
@@ -477,45 +515,45 @@ function q(t) {
|
|
|
477
515
|
...await u(e),
|
|
478
516
|
updatedAt: l()
|
|
479
517
|
};
|
|
480
|
-
if (typeof t.name == "string" && t.name.trim() && (n.name = t.name.trim().slice(0, 100)), typeof t.draining == "boolean" && (n.draining = t.draining), typeof t.isDefault == "boolean" && (n.isDefault = t.isDefault, t.isDefault)) for (let t of await r.list(
|
|
518
|
+
if (typeof t.name == "string" && t.name.trim() && (n.name = t.name.trim().slice(0, 100)), typeof t.draining == "boolean" && (n.draining = t.draining), typeof t.isDefault == "boolean" && (n.isDefault = t.isDefault, t.isDefault)) for (let t of await r.list(F)) t.id !== e && t.isDefault && await r.put(F, {
|
|
481
519
|
...t,
|
|
482
520
|
isDefault: !1,
|
|
483
521
|
updatedAt: l()
|
|
484
522
|
});
|
|
485
|
-
await r.put(
|
|
523
|
+
await r.put(F, n);
|
|
486
524
|
},
|
|
487
525
|
async remove(e) {
|
|
488
526
|
let t = await u(e);
|
|
489
|
-
if ((await r.list("agentico_namespaces")).some((t) => t.runnerId === e)) throw new
|
|
490
|
-
t.keyId && await f(t).then((e) => e.platform.revokePlatformKey(t.keyId)).catch(() => !1), await i.delete(
|
|
527
|
+
if ((await r.list("agentico_namespaces")).some((t) => t.runnerId === e)) throw new B("conflict", "RUNNER_HAS_NAMESPACES");
|
|
528
|
+
t.keyId && await f(t).then((e) => e.platform.revokePlatformKey(t.keyId)).catch(() => !1), await i.delete(U(e)), await r.delete(F, e);
|
|
491
529
|
},
|
|
492
530
|
async status(e) {
|
|
493
531
|
return (await f(await u(e))).platform.status();
|
|
494
532
|
},
|
|
495
533
|
async assign(e, n, a = {}) {
|
|
496
534
|
let o = await g(e), s = await u(n);
|
|
497
|
-
if (o.runnerId === n && await i.get(
|
|
498
|
-
if (s.draining) throw new
|
|
535
|
+
if (o.runnerId === n && await i.get(G(e, n))) return;
|
|
536
|
+
if (s.draining) throw new B("conflict", "RUNNER_DRAINING");
|
|
499
537
|
let c = await f(s), l = (await c.platform.tenants()).some((t) => t.id === e), d, p;
|
|
500
538
|
if (l) {
|
|
501
539
|
let n = await c.platform.createKey(e, {
|
|
502
540
|
name: t.platformName,
|
|
503
|
-
permissions:
|
|
541
|
+
permissions: H
|
|
504
542
|
});
|
|
505
543
|
d = n.key, p = n.record.id;
|
|
506
544
|
} else {
|
|
507
545
|
let t = await c.platform.createTenant(e, {
|
|
508
546
|
...a.name ? { name: a.name } : {},
|
|
509
|
-
keyPermissions:
|
|
547
|
+
keyPermissions: H
|
|
510
548
|
});
|
|
511
549
|
if (!t.key || !t.keyRecord) throw Error("the runner made the tenant without a key");
|
|
512
550
|
d = t.key, p = t.keyRecord.id;
|
|
513
551
|
}
|
|
514
|
-
if (await i.set(
|
|
515
|
-
let t = await r.get(
|
|
516
|
-
t && await f(t).then((t) => t.platform.revokeKey(e, o.runnerKeyId)).catch(() => !1), await i.delete(
|
|
552
|
+
if (await i.set(G(e, n), d), !a.keepBefore && o.runnerId && o.runnerId !== n && o.runnerKeyId) {
|
|
553
|
+
let t = await r.get(F, o.runnerId);
|
|
554
|
+
t && await f(t).then((t) => t.platform.revokeKey(e, o.runnerKeyId)).catch(() => !1), await i.delete(G(e, o.runnerId));
|
|
517
555
|
}
|
|
518
|
-
await r.put(
|
|
556
|
+
await r.put(L, {
|
|
519
557
|
...o,
|
|
520
558
|
runnerId: n,
|
|
521
559
|
runnerKeyId: p
|
|
@@ -523,20 +561,20 @@ function q(t) {
|
|
|
523
561
|
},
|
|
524
562
|
async route(e, t = {}) {
|
|
525
563
|
if (t.chat && t.user) {
|
|
526
|
-
let n = await r.get(
|
|
564
|
+
let n = await r.get(R, `${e}_${t.user}_${t.chat}`), a = n ? await r.get(F, n.runnerId) : null, o = a ? await i.get(G(e, a.id)) : null;
|
|
527
565
|
if (a && o) return {
|
|
528
566
|
runnerId: a.id,
|
|
529
567
|
url: a.url,
|
|
530
568
|
key: o
|
|
531
569
|
};
|
|
532
570
|
}
|
|
533
|
-
let n = await r.list(
|
|
571
|
+
let n = await r.list(F), a = await g(e), o = a.runnerId ? n.find((e) => e.id === a.runnerId) : void 0, s = n.find((e) => e.isDefault && !e.draining && e.id !== o?.id) ?? (o ? void 0 : n.find((e) => !e.draining));
|
|
534
572
|
if (o) t.newChat && o.draining && s && (await _.assign(e, s.id, { keepBefore: !0 }), a = await g(e));
|
|
535
573
|
else {
|
|
536
574
|
if (!s) return null;
|
|
537
575
|
await _.assign(e, s.id), a = await g(e);
|
|
538
576
|
}
|
|
539
|
-
let c = a.runnerId ? await r.get(
|
|
577
|
+
let c = a.runnerId ? await r.get(F, a.runnerId) : null, l = c ? await i.get(G(e, c.id)) : null;
|
|
540
578
|
return c && l ? {
|
|
541
579
|
runnerId: c.id,
|
|
542
580
|
url: c.url,
|
|
@@ -544,26 +582,26 @@ function q(t) {
|
|
|
544
582
|
} : null;
|
|
545
583
|
},
|
|
546
584
|
async chatStarted(e, t, n, i) {
|
|
547
|
-
await r.put(
|
|
585
|
+
await r.put(R, {
|
|
548
586
|
id: `${e}_${t}_${n}`,
|
|
549
587
|
runnerId: i
|
|
550
588
|
});
|
|
551
589
|
},
|
|
552
590
|
async chatClosed(e, t, n) {
|
|
553
|
-
await r.delete(
|
|
591
|
+
await r.delete(R, `${e}_${t}_${n}`);
|
|
554
592
|
},
|
|
555
593
|
async install(e, t, n) {
|
|
556
|
-
if (!e.access?.host || !e.access.username || !e.access.privateKey && !e.access.password) throw new
|
|
557
|
-
if (!String(e.domain ?? "").trim()) throw new
|
|
594
|
+
if (!e.access?.host || !e.access.username || !e.access.privateKey && !e.access.password) throw new B("invalid", "SSH_ACCESS_REQUIRED");
|
|
595
|
+
if (!String(e.domain ?? "").trim()) throw new B("invalid", "DOMAIN_REQUIRED");
|
|
558
596
|
let i = s(), a = {
|
|
559
597
|
id: i,
|
|
560
598
|
status: "running",
|
|
561
599
|
lines: [],
|
|
562
600
|
startedBy: t
|
|
563
601
|
};
|
|
564
|
-
await r.put(
|
|
602
|
+
await r.put(z, a);
|
|
565
603
|
let c = Promise.resolve(), l = (e) => {
|
|
566
|
-
n?.(e), a.lines = [...a.lines, e.line].slice(-2e3), c = c.then(() => r.put(
|
|
604
|
+
n?.(e), a.lines = [...a.lines, e.line].slice(-2e3), c = c.then(() => r.put(z, a)).catch(() => void 0);
|
|
567
605
|
}, u = (async () => {
|
|
568
606
|
try {
|
|
569
607
|
let t = await o(e.access, {
|
|
@@ -575,13 +613,13 @@ function q(t) {
|
|
|
575
613
|
url: t.url,
|
|
576
614
|
code: t.pairingCode
|
|
577
615
|
}, { hostFingerprint: t.hostFingerprint });
|
|
578
|
-
return await c, await r.put(
|
|
616
|
+
return await c, await r.put(z, {
|
|
579
617
|
...a,
|
|
580
618
|
status: "done",
|
|
581
619
|
runnerId: n.id
|
|
582
620
|
}), n;
|
|
583
621
|
} catch (e) {
|
|
584
|
-
throw await c, await r.put(
|
|
622
|
+
throw await c, await r.put(z, {
|
|
585
623
|
...a,
|
|
586
624
|
status: "error",
|
|
587
625
|
error: e instanceof Error ? e.message : String(e)
|
|
@@ -594,7 +632,7 @@ function q(t) {
|
|
|
594
632
|
};
|
|
595
633
|
},
|
|
596
634
|
async installState(e, t, n = 0) {
|
|
597
|
-
let i = await r.get(
|
|
635
|
+
let i = await r.get(z, e);
|
|
598
636
|
return !i || i.startedBy !== t ? null : {
|
|
599
637
|
status: i.status,
|
|
600
638
|
lines: i.lines.slice(n),
|
|
@@ -608,16 +646,16 @@ function q(t) {
|
|
|
608
646
|
}
|
|
609
647
|
//#endregion
|
|
610
648
|
//#region src/server/server.ts
|
|
611
|
-
var
|
|
649
|
+
var Y = class extends Error {
|
|
612
650
|
status;
|
|
613
651
|
constructor(e, t) {
|
|
614
652
|
super(t), this.status = e;
|
|
615
653
|
}
|
|
616
|
-
},
|
|
654
|
+
}, X = {
|
|
617
655
|
member: 0,
|
|
618
656
|
admin: 1,
|
|
619
657
|
platform: 2
|
|
620
|
-
},
|
|
658
|
+
}, Z = (e, t) => new Response(JSON.stringify(t), {
|
|
621
659
|
status: e,
|
|
622
660
|
headers: {
|
|
623
661
|
"content-type": "application/json",
|
|
@@ -637,14 +675,14 @@ function ie(e) {
|
|
|
637
675
|
records: n,
|
|
638
676
|
secrets: r,
|
|
639
677
|
...e.adapter ? { adapter: e.adapter } : {}
|
|
640
|
-
}), s =
|
|
678
|
+
}), s = O({
|
|
641
679
|
records: n,
|
|
642
680
|
secrets: r,
|
|
643
681
|
fetch: a,
|
|
644
682
|
...e.now ? { now: e.now } : {}
|
|
645
683
|
}), c = /* @__PURE__ */ new Map(), l = (t) => {
|
|
646
684
|
let i = c.get(t);
|
|
647
|
-
return i || (i =
|
|
685
|
+
return i || (i = J({
|
|
648
686
|
project: t,
|
|
649
687
|
platformName: e.platformName,
|
|
650
688
|
records: n,
|
|
@@ -654,7 +692,7 @@ function ie(e) {
|
|
|
654
692
|
...e.now ? { now: e.now } : {}
|
|
655
693
|
}), c.set(t, i)), i;
|
|
656
694
|
}, u = (e, t) => {
|
|
657
|
-
if (
|
|
695
|
+
if (X[e.role] < X[t]) throw new Y(403, "forbidden");
|
|
658
696
|
}, d = async (e) => {
|
|
659
697
|
try {
|
|
660
698
|
let t = await e.json();
|
|
@@ -663,19 +701,19 @@ function ie(e) {
|
|
|
663
701
|
return {};
|
|
664
702
|
}
|
|
665
703
|
}, f = (e, t) => {
|
|
666
|
-
let n =
|
|
704
|
+
let n = M(e.project, e.tenant), r = t.searchParams.get("namespace");
|
|
667
705
|
if (!r || r === n) return n;
|
|
668
|
-
if (u(e, "platform"), !
|
|
669
|
-
return
|
|
670
|
-
}, p = async (e) => (await n.list(
|
|
706
|
+
if (u(e, "platform"), !N(e.project, r)) throw new Y(404, "not found");
|
|
707
|
+
return M(e.project, P(e.project, r)), r;
|
|
708
|
+
}, p = async (e) => (await n.list(L)).filter((t) => N(e, t.id)), m = async (e) => await n.get("agentico_namespaces", e) ?? { id: e };
|
|
671
709
|
async function h(e, t, n, r) {
|
|
672
710
|
if (!te.test(n) || r && !n.startsWith("/v1/accounts") || ![
|
|
673
711
|
"GET",
|
|
674
712
|
"POST",
|
|
675
713
|
"PUT",
|
|
676
714
|
"DELETE"
|
|
677
|
-
].includes(e.method)) return
|
|
678
|
-
let o =
|
|
715
|
+
].includes(e.method)) return Z(404, { error: "not found" });
|
|
716
|
+
let o = M(t.project, t.tenant), s = l(t.project), c = ne.exec(n)?.[1], u = n === "/v1/chats" && e.method === "POST", d = e.method === "GET" || e.method === "DELETE" ? void 0 : await e.arrayBuffer(), f = u && d ? (() => {
|
|
679
717
|
try {
|
|
680
718
|
let e = JSON.parse(new TextDecoder().decode(d));
|
|
681
719
|
return typeof e.id == "string" ? e.id : void 0;
|
|
@@ -689,7 +727,7 @@ function ie(e) {
|
|
|
689
727
|
user: t.user.id
|
|
690
728
|
} : {}
|
|
691
729
|
}).catch(() => null);
|
|
692
|
-
if (!p) return
|
|
730
|
+
if (!p) return Z(409, { error: "no-runner" });
|
|
693
731
|
let m = { authorization: `Bearer ${p.key}` };
|
|
694
732
|
r || (m["x-runner-user"] = t.user.id);
|
|
695
733
|
let h = e.headers.get("content-type");
|
|
@@ -703,7 +741,7 @@ function ie(e) {
|
|
|
703
741
|
signal: e.signal
|
|
704
742
|
});
|
|
705
743
|
} catch {
|
|
706
|
-
return
|
|
744
|
+
return Z(502, { error: "unreachable" });
|
|
707
745
|
}
|
|
708
746
|
return f && g.ok && await s.chatStarted(o, t.user.id, f, p.runnerId), c && e.method === "DELETE" && n === `/v1/chats/${c}` && await s.chatClosed(o, t.user.id, c).catch(() => void 0), i("runner request", {
|
|
709
747
|
namespace: o,
|
|
@@ -721,23 +759,27 @@ function ie(e) {
|
|
|
721
759
|
}
|
|
722
760
|
async function g(t, r, c, g) {
|
|
723
761
|
let _ = c.split("/").filter(Boolean).map(decodeURIComponent), [v, y, b, x] = _, S = t.method, C = r.project;
|
|
724
|
-
if (v === "me" && S === "GET")
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
762
|
+
if (v === "me" && S === "GET") {
|
|
763
|
+
let e = M(C, r.tenant), t = !!(await m(e)).runnerId || (await l(C).list()).some((e) => !e.draining);
|
|
764
|
+
return Z(200, {
|
|
765
|
+
user: r.user,
|
|
766
|
+
role: r.role,
|
|
767
|
+
project: C,
|
|
768
|
+
...r.tenant ? { tenant: r.tenant } : {},
|
|
769
|
+
namespace: e,
|
|
770
|
+
hasRunner: t
|
|
771
|
+
});
|
|
772
|
+
}
|
|
731
773
|
if (v === "connections") {
|
|
732
774
|
let e = f(r, g);
|
|
733
|
-
if (!y && S === "GET") return
|
|
775
|
+
if (!y && S === "GET") return Z(200, await o.snapshot(e));
|
|
734
776
|
if (y === "order" && !b && S === "PUT") {
|
|
735
777
|
u(r, "admin");
|
|
736
778
|
let n = (await d(t)).order;
|
|
737
|
-
return await o.setOrder(e, Array.isArray(n) ? n.map(String) : null),
|
|
779
|
+
return await o.setOrder(e, Array.isArray(n) ? n.map(String) : null), Z(200, { ok: !0 });
|
|
738
780
|
}
|
|
739
|
-
if (y && !b && S === "GET") return
|
|
740
|
-
if (y && b === "models" && S === "GET") return
|
|
781
|
+
if (y && !b && S === "GET") return Z(404, { error: "not found" });
|
|
782
|
+
if (y && b === "models" && S === "GET") return Z(200, { models: await o.models(e, y) });
|
|
741
783
|
u(r, "admin");
|
|
742
784
|
let n = async () => {
|
|
743
785
|
let e = (await d(t)).fields;
|
|
@@ -750,40 +792,40 @@ function ie(e) {
|
|
|
750
792
|
provider: y,
|
|
751
793
|
ok: t.ok,
|
|
752
794
|
by: r.user.id
|
|
753
|
-
}),
|
|
795
|
+
}), Z(t.ok ? 200 : 422, t);
|
|
754
796
|
}
|
|
755
|
-
if (y && !b && S === "DELETE") return await o.disconnect(e, y),
|
|
797
|
+
if (y && !b && S === "DELETE") return await o.disconnect(e, y), Z(200, { ok: !0 });
|
|
756
798
|
if (y && b === "accounts" && !x && S === "POST") {
|
|
757
799
|
let t = await o.addAccount(e, y, await n());
|
|
758
|
-
return
|
|
800
|
+
return Z(t.ok ? 200 : 422, t);
|
|
759
801
|
}
|
|
760
802
|
if (y && b === "accounts" && x === "order" && S === "PUT") {
|
|
761
803
|
let n = (await d(t)).ids;
|
|
762
|
-
return await o.orderAccounts(e, y, Array.isArray(n) ? n.map(String) : []),
|
|
804
|
+
return await o.orderAccounts(e, y, Array.isArray(n) ? n.map(String) : []), Z(200, { ok: !0 });
|
|
763
805
|
}
|
|
764
|
-
if (y && b === "accounts" && x && S === "DELETE") return await o.removeAccount(e, y, x),
|
|
806
|
+
if (y && b === "accounts" && x && S === "DELETE") return await o.removeAccount(e, y, x), Z(200, { ok: !0 });
|
|
765
807
|
}
|
|
766
808
|
if (v === "ai" && y === "complete" && S === "POST") {
|
|
767
|
-
let e = await d(t), n = String(e.provider ?? ""), a = await o.complete(
|
|
768
|
-
throw new
|
|
809
|
+
let e = await d(t), n = String(e.provider ?? ""), a = await o.complete(M(C, r.tenant), n, e.request).catch((e) => {
|
|
810
|
+
throw new Y(e instanceof Error && e.message === "no-provider" ? 409 : 502, e instanceof Error && e.message === "no-provider" ? "no-provider" : "failed");
|
|
769
811
|
});
|
|
770
812
|
return i("ai complete", {
|
|
771
|
-
namespace:
|
|
813
|
+
namespace: M(C, r.tenant),
|
|
772
814
|
provider: n,
|
|
773
815
|
user: r.user.id
|
|
774
|
-
}),
|
|
816
|
+
}), Z(200, { result: a });
|
|
775
817
|
}
|
|
776
818
|
if (v === "ai" && y === "proxy") {
|
|
777
819
|
let n = g.searchParams.get("url") ?? "", i = /* @__PURE__ */ new Set([...re, ...(e.sessionHeaders ?? []).map((e) => e.toLowerCase())]), s = {};
|
|
778
820
|
t.headers.forEach((e, t) => {
|
|
779
821
|
i.has(t.toLowerCase()) || (s[t] = e);
|
|
780
822
|
});
|
|
781
|
-
let c = await o.resolve(
|
|
823
|
+
let c = await o.resolve(M(C, r.tenant), {
|
|
782
824
|
url: n,
|
|
783
825
|
headers: s
|
|
784
826
|
});
|
|
785
|
-
if (!c.ok) return
|
|
786
|
-
if (new URL(c.url).protocol !== "https:") return
|
|
827
|
+
if (!c.ok) return Z(c.status, { error: c.error });
|
|
828
|
+
if (new URL(c.url).protocol !== "https:") return Z(403, { error: "forbidden-target" });
|
|
787
829
|
let l = S === "GET" || S === "HEAD" ? void 0 : await t.arrayBuffer(), u = await a(c.url, {
|
|
788
830
|
method: S,
|
|
789
831
|
headers: c.headers,
|
|
@@ -796,14 +838,14 @@ function ie(e) {
|
|
|
796
838
|
"content-type": u.headers.get("content-type") ?? "application/json",
|
|
797
839
|
"cache-control": "no-store"
|
|
798
840
|
}
|
|
799
|
-
}) :
|
|
841
|
+
}) : Z(502, { error: "unreachable" });
|
|
800
842
|
}
|
|
801
843
|
if (v === "runner") return h(t, r, `/${_.slice(1).map(encodeURIComponent).join("/")}`, !1);
|
|
802
844
|
if (v === "runner-namespace") return u(r, "admin"), h(t, r, `/${_.slice(1).map(encodeURIComponent).join("/")}`, !0);
|
|
803
845
|
if (v === "runners") {
|
|
804
846
|
u(r, "platform");
|
|
805
847
|
let e = l(C);
|
|
806
|
-
if (!y && S === "GET") return
|
|
848
|
+
if (!y && S === "GET") return Z(200, { runners: await e.list() });
|
|
807
849
|
if (!y && S === "POST") {
|
|
808
850
|
let n = await d(t), a = await e.add({
|
|
809
851
|
url: String(n.url ?? ""),
|
|
@@ -815,16 +857,16 @@ function ie(e) {
|
|
|
815
857
|
project: C,
|
|
816
858
|
runner: a.id,
|
|
817
859
|
by: r.user.id
|
|
818
|
-
}),
|
|
860
|
+
}), Z(201, { runner: a });
|
|
819
861
|
}
|
|
820
862
|
if (y === "pairings" && !b && S === "POST") {
|
|
821
863
|
let n = await d(t);
|
|
822
|
-
return
|
|
864
|
+
return Z(201, await e.startPairing({
|
|
823
865
|
url: String(n.url ?? ""),
|
|
824
866
|
...typeof n.name == "string" ? { name: n.name } : {}
|
|
825
867
|
}, r.user.id));
|
|
826
868
|
}
|
|
827
|
-
if (y === "pairings" && b && S === "GET") return
|
|
869
|
+
if (y === "pairings" && b && S === "GET") return Z(200, await e.pairingStatus(b, r.user.id));
|
|
828
870
|
if (y === "installs" && !b && S === "POST") {
|
|
829
871
|
let n = await d(t), a = await e.install({
|
|
830
872
|
domain: String(n.domain ?? ""),
|
|
@@ -836,11 +878,11 @@ function ie(e) {
|
|
|
836
878
|
project: C,
|
|
837
879
|
install: a.installId,
|
|
838
880
|
by: r.user.id
|
|
839
|
-
}),
|
|
881
|
+
}), Z(202, { installId: a.installId });
|
|
840
882
|
}
|
|
841
883
|
if (y === "installs" && b && S === "GET") {
|
|
842
884
|
let t = await e.installState(b, r.user.id, Number(g.searchParams.get("from")) || 0);
|
|
843
|
-
return t ?
|
|
885
|
+
return t ? Z(200, t) : Z(404, { error: "not found" });
|
|
844
886
|
}
|
|
845
887
|
if (y && !b && S === "PATCH") {
|
|
846
888
|
let n = await d(t);
|
|
@@ -848,52 +890,67 @@ function ie(e) {
|
|
|
848
890
|
...typeof n.name == "string" ? { name: n.name } : {},
|
|
849
891
|
...typeof n.isDefault == "boolean" ? { isDefault: n.isDefault } : {},
|
|
850
892
|
...typeof n.draining == "boolean" ? { draining: n.draining } : {}
|
|
851
|
-
}),
|
|
893
|
+
}), Z(200, { ok: !0 });
|
|
852
894
|
}
|
|
853
|
-
if (y && !b && S === "DELETE") return await e.remove(y),
|
|
854
|
-
if (y && b === "status" && S === "GET") return
|
|
895
|
+
if (y && !b && S === "DELETE") return await e.remove(y), Z(200, { ok: !0 });
|
|
896
|
+
if (y && b === "status" && S === "GET") return Z(200, await e.status(y));
|
|
855
897
|
}
|
|
856
898
|
if (v === "memories") {
|
|
857
|
-
|
|
899
|
+
u(r, "admin");
|
|
900
|
+
let e = r.role === "platform" ? { platform: !0 } : {
|
|
901
|
+
platform: !1,
|
|
902
|
+
namespace: M(C, r.tenant)
|
|
903
|
+
};
|
|
904
|
+
if (!y && S === "GET") return Z(200, { memories: await s.list(e) });
|
|
858
905
|
if (y === "check" && S === "POST") {
|
|
859
906
|
let e = await d(t);
|
|
860
|
-
return
|
|
907
|
+
return Z(200, await s.check({
|
|
861
908
|
apiUrl: String(e.apiUrl ?? ""),
|
|
862
909
|
key: String(e.key ?? "")
|
|
863
910
|
}));
|
|
864
911
|
}
|
|
865
912
|
if (!y && S === "POST") {
|
|
866
|
-
let
|
|
867
|
-
apiUrl: String(
|
|
868
|
-
key: String(
|
|
869
|
-
...typeof
|
|
870
|
-
...typeof
|
|
871
|
-
});
|
|
913
|
+
let n = await d(t), a = await s.add({
|
|
914
|
+
apiUrl: String(n.apiUrl ?? ""),
|
|
915
|
+
key: String(n.key ?? ""),
|
|
916
|
+
...typeof n.name == "string" ? { name: n.name } : {},
|
|
917
|
+
...typeof n.mcpUrl == "string" ? { mcpUrl: n.mcpUrl } : {}
|
|
918
|
+
}, e.platform ? void 0 : e.namespace);
|
|
872
919
|
return i("memory connected", {
|
|
873
920
|
project: C,
|
|
874
|
-
memory:
|
|
921
|
+
memory: a.id,
|
|
875
922
|
by: r.user.id
|
|
876
|
-
}),
|
|
923
|
+
}), Z(201, { memory: a });
|
|
877
924
|
}
|
|
878
925
|
if (y && S === "PATCH") {
|
|
879
|
-
let
|
|
880
|
-
return
|
|
881
|
-
...typeof
|
|
882
|
-
...
|
|
883
|
-
...typeof
|
|
884
|
-
}) });
|
|
926
|
+
let n = await d(t);
|
|
927
|
+
return Z(200, { memory: await s.update(y, {
|
|
928
|
+
...typeof n.name == "string" ? { name: n.name } : {},
|
|
929
|
+
...n.mcpUrl === null || typeof n.mcpUrl == "string" ? { mcpUrl: n.mcpUrl } : {},
|
|
930
|
+
...typeof n.key == "string" ? { key: n.key } : {}
|
|
931
|
+
}, e) });
|
|
932
|
+
}
|
|
933
|
+
if (y && S === "DELETE") {
|
|
934
|
+
let t = await s.of(y, e), r = (await n.list(L)).filter((e) => e.memoryId === y);
|
|
935
|
+
if (!e.platform && (t.owners?.length ?? 0) > 1) {
|
|
936
|
+
await s.remove(y, !1, e);
|
|
937
|
+
for (let t of r.filter((t) => t.id === e.namespace)) {
|
|
938
|
+
let { memoryId: e, ...r } = t;
|
|
939
|
+
await n.put(L, r);
|
|
940
|
+
}
|
|
941
|
+
} else await s.remove(y, r.length > 0, e);
|
|
942
|
+
return Z(200, { ok: !0 });
|
|
885
943
|
}
|
|
886
|
-
if (y && S === "DELETE") return await s.remove(y, (await n.list(I)).some((e) => e.memoryId === y)), X(200, { ok: !0 });
|
|
887
944
|
}
|
|
888
945
|
if (v === "memory") {
|
|
889
|
-
let e =
|
|
890
|
-
if (!y && S === "GET") return
|
|
946
|
+
let e = M(C, r.tenant), n = (await m(e)).memoryId;
|
|
947
|
+
if (!y && S === "GET") return Z(200, n ? {
|
|
891
948
|
configured: !0,
|
|
892
949
|
...await s.coordinates(n, e)
|
|
893
950
|
} : { configured: !1 });
|
|
894
|
-
if (!n) return
|
|
951
|
+
if (!n) return Z(409, { error: "no-memory" });
|
|
895
952
|
let a = await d(t);
|
|
896
|
-
if (y === "search" && S === "POST") return
|
|
953
|
+
if (y === "search" && S === "POST") return Z(200, { results: await s.search(n, e, String(a.query ?? ""), Number(a.limit) || 10) });
|
|
897
954
|
if (y === "write" && S === "POST") {
|
|
898
955
|
let t = await s.write(n, e, {
|
|
899
956
|
title: String(a.title ?? ""),
|
|
@@ -903,27 +960,30 @@ function ie(e) {
|
|
|
903
960
|
return i("memory write", {
|
|
904
961
|
namespace: e,
|
|
905
962
|
user: r.user.id
|
|
906
|
-
}),
|
|
963
|
+
}), Z(200, t);
|
|
907
964
|
}
|
|
908
965
|
}
|
|
909
966
|
if (v === "projects" && !y && S === "GET") {
|
|
910
967
|
u(r, "admin");
|
|
911
968
|
let [t, n, i, a] = await Promise.all([
|
|
912
|
-
|
|
913
|
-
|
|
969
|
+
X[r.role] >= X.platform ? l(C).list() : Promise.resolve([]),
|
|
970
|
+
s.list(r.role === "platform" ? { platform: !0 } : {
|
|
971
|
+
platform: !1,
|
|
972
|
+
namespace: M(C, r.tenant)
|
|
973
|
+
}),
|
|
914
974
|
p(C),
|
|
915
975
|
e.tenants && r.role === "platform" ? e.tenants(r) : Promise.resolve([])
|
|
916
|
-
]), o =
|
|
976
|
+
]), o = M(C, r.tenant), c = /* @__PURE__ */ new Map();
|
|
917
977
|
if (c.set(o, void 0), r.role === "platform") {
|
|
918
978
|
for (let e of i) c.set(e.id, void 0);
|
|
919
979
|
for (let e of a) try {
|
|
920
|
-
c.set(
|
|
980
|
+
c.set(M(C, e.id), e.name);
|
|
921
981
|
} catch {}
|
|
922
982
|
}
|
|
923
|
-
return
|
|
983
|
+
return Z(200, {
|
|
924
984
|
project: C,
|
|
925
985
|
namespaces: [...c.entries()].map(([e, r]) => {
|
|
926
|
-
let a = i.find((t) => t.id === e), o = t.find((e) => e.id === a?.runnerId), s = n.find((e) => e.id === a?.memoryId), c =
|
|
986
|
+
let a = i.find((t) => t.id === e), o = t.find((e) => e.id === a?.runnerId), s = n.find((e) => e.id === a?.memoryId), c = P(C, e);
|
|
927
987
|
return {
|
|
928
988
|
namespace: e,
|
|
929
989
|
...c ? { tenant: c } : {},
|
|
@@ -941,24 +1001,34 @@ function ie(e) {
|
|
|
941
1001
|
});
|
|
942
1002
|
}
|
|
943
1003
|
if (v === "namespaces" && y && !b && S === "PUT") {
|
|
944
|
-
if (u(r, "
|
|
945
|
-
|
|
946
|
-
let e =
|
|
947
|
-
if (
|
|
1004
|
+
if (u(r, "admin"), !N(C, y)) throw new Y(404, "not found");
|
|
1005
|
+
M(C, P(C, y));
|
|
1006
|
+
let e = r.role === "platform";
|
|
1007
|
+
if (!e && y !== M(C, r.tenant)) throw new Y(403, "forbidden");
|
|
1008
|
+
let a = await d(t);
|
|
1009
|
+
if (typeof a.runnerId == "string" && a.runnerId) {
|
|
1010
|
+
if (!e) throw new Y(403, "forbidden");
|
|
1011
|
+
await l(C).assign(y, a.runnerId);
|
|
1012
|
+
}
|
|
1013
|
+
if (a.memoryId !== void 0) {
|
|
948
1014
|
let t = await m(y);
|
|
949
|
-
if (
|
|
1015
|
+
if (a.memoryId === null || a.memoryId === "") delete t.memoryId;
|
|
950
1016
|
else {
|
|
951
|
-
|
|
952
|
-
|
|
1017
|
+
let n = e ? { platform: !0 } : {
|
|
1018
|
+
platform: !1,
|
|
1019
|
+
namespace: y
|
|
1020
|
+
};
|
|
1021
|
+
if (!(await s.list(n)).some((e) => e.id === a.memoryId)) throw new Y(404, "memory not found");
|
|
1022
|
+
t.memoryId = String(a.memoryId);
|
|
953
1023
|
}
|
|
954
|
-
await n.put(
|
|
1024
|
+
await n.put(L, t);
|
|
955
1025
|
}
|
|
956
1026
|
return i("namespace set", {
|
|
957
1027
|
namespace: y,
|
|
958
1028
|
by: r.user.id
|
|
959
|
-
}),
|
|
1029
|
+
}), Z(200, { ok: !0 });
|
|
960
1030
|
}
|
|
961
|
-
return
|
|
1031
|
+
return Z(404, { error: "not found" });
|
|
962
1032
|
}
|
|
963
1033
|
return {
|
|
964
1034
|
connections: o,
|
|
@@ -966,16 +1036,16 @@ function ie(e) {
|
|
|
966
1036
|
fleet: l,
|
|
967
1037
|
async handle(n) {
|
|
968
1038
|
let r = new URL(n.url), a = (e.basePath ?? "").replace(/\/+$/, "");
|
|
969
|
-
if (a && r.pathname !== a && !r.pathname.startsWith(`${a}/`)) return
|
|
1039
|
+
if (a && r.pathname !== a && !r.pathname.startsWith(`${a}/`)) return Z(404, { error: "not found" });
|
|
970
1040
|
let o = r.pathname.slice(a.length) || "/";
|
|
971
1041
|
try {
|
|
972
1042
|
let t = await e.identity(n);
|
|
973
|
-
return t ? await g(n, t, o, r) :
|
|
1043
|
+
return t ? await g(n, t, o, r) : Z(401, { error: "unauthenticated" });
|
|
974
1044
|
} catch (e) {
|
|
975
|
-
if (e instanceof
|
|
976
|
-
if (e instanceof
|
|
977
|
-
if (e instanceof
|
|
978
|
-
if (e instanceof
|
|
1045
|
+
if (e instanceof Y) return Z(e.status, { error: e.message });
|
|
1046
|
+
if (e instanceof j) return Z(400, { error: "invalid namespace" });
|
|
1047
|
+
if (e instanceof B) return Z(e.code === "invalid" ? 400 : e.code === "not-found" ? 404 : 409, { error: e.message });
|
|
1048
|
+
if (e instanceof T) {
|
|
979
1049
|
let t = {
|
|
980
1050
|
invalid: 400,
|
|
981
1051
|
"not-found": 404,
|
|
@@ -983,19 +1053,19 @@ function ie(e) {
|
|
|
983
1053
|
unreachable: 502,
|
|
984
1054
|
rejected: 422
|
|
985
1055
|
}[e.code];
|
|
986
|
-
return
|
|
1056
|
+
return Z(t, { error: e.message });
|
|
987
1057
|
}
|
|
988
|
-
return e instanceof t ?
|
|
1058
|
+
return e instanceof t ? Z(502, { error: e.code }) : (i("agentico server error", {
|
|
989
1059
|
path: o,
|
|
990
1060
|
error: e instanceof Error ? e.name : "error"
|
|
991
|
-
}),
|
|
1061
|
+
}), Z(500, { error: "failed" }));
|
|
992
1062
|
}
|
|
993
1063
|
}
|
|
994
1064
|
};
|
|
995
1065
|
}
|
|
996
1066
|
//#endregion
|
|
997
1067
|
//#region src/server/stores.ts
|
|
998
|
-
function
|
|
1068
|
+
function Q(e, t) {
|
|
999
1069
|
return {
|
|
1000
1070
|
async get(t, n) {
|
|
1001
1071
|
return e()[t]?.[n] ?? null;
|
|
@@ -1016,7 +1086,7 @@ function Z(e, t) {
|
|
|
1016
1086
|
}
|
|
1017
1087
|
};
|
|
1018
1088
|
}
|
|
1019
|
-
function
|
|
1089
|
+
function $(e, t) {
|
|
1020
1090
|
return {
|
|
1021
1091
|
async get(t) {
|
|
1022
1092
|
return e()[t] ?? null;
|
|
@@ -1036,37 +1106,37 @@ function Q(e, t) {
|
|
|
1036
1106
|
function ae() {
|
|
1037
1107
|
let e = {}, t = {};
|
|
1038
1108
|
return {
|
|
1039
|
-
records:
|
|
1109
|
+
records: Q(() => structuredClone(e), (t) => {
|
|
1040
1110
|
e = t;
|
|
1041
1111
|
}),
|
|
1042
|
-
secrets:
|
|
1112
|
+
secrets: $(() => ({ ...t }), (e) => {
|
|
1043
1113
|
t = e;
|
|
1044
1114
|
})
|
|
1045
1115
|
};
|
|
1046
1116
|
}
|
|
1047
|
-
function
|
|
1117
|
+
function oe(e, t) {
|
|
1048
1118
|
try {
|
|
1049
1119
|
return l(e) ? JSON.parse(d(e, "utf8")) : t;
|
|
1050
1120
|
} catch {
|
|
1051
1121
|
return t;
|
|
1052
1122
|
}
|
|
1053
1123
|
}
|
|
1054
|
-
function
|
|
1124
|
+
function se(e, t) {
|
|
1055
1125
|
let n = `${e}.tmp`;
|
|
1056
1126
|
p(n, JSON.stringify(t, null, 2), { mode: 384 }), f(n, e);
|
|
1057
1127
|
}
|
|
1058
|
-
function
|
|
1128
|
+
function ce(e) {
|
|
1059
1129
|
u(e, {
|
|
1060
1130
|
recursive: !0,
|
|
1061
1131
|
mode: 448
|
|
1062
1132
|
});
|
|
1063
1133
|
let t = m(e, "records.json"), n = m(e, "secrets.json");
|
|
1064
1134
|
return {
|
|
1065
|
-
records:
|
|
1066
|
-
secrets:
|
|
1135
|
+
records: Q(() => oe(t, {}), (e) => se(t, e)),
|
|
1136
|
+
secrets: $(() => oe(n, {}), (e) => se(n, e))
|
|
1067
1137
|
};
|
|
1068
1138
|
}
|
|
1069
1139
|
//#endregion
|
|
1070
|
-
export { h as CONNECTIONS,
|
|
1140
|
+
export { h as CONNECTIONS, B as FleetError, C as MEMORIES, T as MemoryError, L as NAMESPACES, V as NAMESPACE_KEY_PERMISSIONS, j as NamespaceError, k as PROJECT_ID, F as RUNNERS, A as TENANT_ID, w as administers, ie as createAgenticoServer, S as createConnections, J as createFleet, O as createMemories, v as defaultAdapterFactory, ce as fileStores, N as inProject, ae as memoryStores, M as namespaceOf, K as normalizeRunnerUrl, P as tenantOf };
|
|
1071
1141
|
|
|
1072
1142
|
//# sourceMappingURL=server.mjs.map
|