@superatomai/sdk-node 0.0.93-dsp → 0.0.95-dsp
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 +942 -942
- package/dist/index.d.mts +220 -800
- package/dist/index.d.ts +220 -800
- package/dist/index.js +505 -10
- package/dist/index.mjs +503 -10
- package/package.json +9 -10
package/dist/index.js
CHANGED
|
@@ -45,6 +45,8 @@ __export(index_exports, {
|
|
|
45
45
|
MainAgent: () => MainAgent,
|
|
46
46
|
MissingRequestContextError: () => MissingRequestContextError,
|
|
47
47
|
NUMERIC_KEYS: () => NUMERIC_KEYS,
|
|
48
|
+
PARCEL_THRESHOLD_BYTES: () => PARCEL_THRESHOLD_BYTES,
|
|
49
|
+
ParcelFile: () => ParcelFile,
|
|
48
50
|
STORAGE_CONFIG: () => STORAGE_CONFIG,
|
|
49
51
|
ScriptMatcher: () => ScriptMatcher,
|
|
50
52
|
ScriptStore: () => ScriptStore,
|
|
@@ -124,7 +126,7 @@ __export(index_exports, {
|
|
|
124
126
|
writeShims: () => writeShims
|
|
125
127
|
});
|
|
126
128
|
module.exports = __toCommonJS(index_exports);
|
|
127
|
-
var
|
|
129
|
+
var import_crypto8 = require("crypto");
|
|
128
130
|
|
|
129
131
|
// src/websocket.ts
|
|
130
132
|
var import_ws = __toESM(require("ws"));
|
|
@@ -3027,6 +3029,7 @@ async function handleDataRequestInner(data, collections, sendMessage, tools) {
|
|
|
3027
3029
|
let requestId;
|
|
3028
3030
|
let collection;
|
|
3029
3031
|
let op;
|
|
3032
|
+
const fromId = typeof data?.from?.id === "string" ? data.from.id : void 0;
|
|
3030
3033
|
try {
|
|
3031
3034
|
const dataRequest = DataRequestMessageSchema.parse(data);
|
|
3032
3035
|
const { id, payload } = dataRequest;
|
|
@@ -3037,13 +3040,13 @@ async function handleDataRequestInner(data, collections, sendMessage, tools) {
|
|
|
3037
3040
|
if (!collections[collection]) {
|
|
3038
3041
|
sendDataResponse(id, collection, op, null, {
|
|
3039
3042
|
error: `Collection '${collection}' not found`
|
|
3040
|
-
}, sendMessage);
|
|
3043
|
+
}, sendMessage, fromId);
|
|
3041
3044
|
return;
|
|
3042
3045
|
}
|
|
3043
3046
|
if (!collections[collection][op]) {
|
|
3044
3047
|
sendDataResponse(id, collection, op, null, {
|
|
3045
3048
|
error: `Operation '${op}' not found for collection '${collection}'`
|
|
3046
|
-
}, sendMessage);
|
|
3049
|
+
}, sendMessage, fromId);
|
|
3047
3050
|
return;
|
|
3048
3051
|
}
|
|
3049
3052
|
const startTime = performance.now();
|
|
@@ -3115,21 +3118,22 @@ async function handleDataRequestInner(data, collections, sendMessage, tools) {
|
|
|
3115
3118
|
logger.warn(`UIBlock ${uiBlockId} not found in threads`);
|
|
3116
3119
|
}
|
|
3117
3120
|
}
|
|
3118
|
-
sendDataResponse(id, collection, op, result, { executionMs }, sendMessage);
|
|
3121
|
+
sendDataResponse(id, collection, op, result, { executionMs }, sendMessage, fromId);
|
|
3119
3122
|
} catch (error) {
|
|
3120
3123
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
3121
3124
|
logger.error("Failed to handle data request:", error);
|
|
3122
3125
|
if (requestId) {
|
|
3123
3126
|
sendDataResponse(requestId, collection || "unknown", op || "unknown", null, {
|
|
3124
3127
|
error: errorMsg
|
|
3125
|
-
}, sendMessage);
|
|
3128
|
+
}, sendMessage, fromId);
|
|
3126
3129
|
}
|
|
3127
3130
|
}
|
|
3128
3131
|
}
|
|
3129
|
-
function sendDataResponse(id, collection, op, data, meta, sendMessage) {
|
|
3132
|
+
function sendDataResponse(id, collection, op, data, meta, sendMessage, fromId) {
|
|
3130
3133
|
const response = {
|
|
3131
3134
|
id,
|
|
3132
3135
|
from: { type: "data-agent" },
|
|
3136
|
+
to: fromId ? { id: fromId } : void 0,
|
|
3133
3137
|
type: "DATA_RES",
|
|
3134
3138
|
payload: {
|
|
3135
3139
|
collection,
|
|
@@ -28361,6 +28365,459 @@ var ReportManager = class {
|
|
|
28361
28365
|
}
|
|
28362
28366
|
};
|
|
28363
28367
|
|
|
28368
|
+
// src/parcels/index.ts
|
|
28369
|
+
var import_crypto7 = require("crypto");
|
|
28370
|
+
var PARCEL_THRESHOLD_BYTES = 256 * 1024;
|
|
28371
|
+
var MAX_INLINE_BYTES = 900 * 1024;
|
|
28372
|
+
var MAX_PARCEL_BYTES = 95 * 1024 * 1024;
|
|
28373
|
+
var PART_BYTES = 600 * 1024;
|
|
28374
|
+
var PART_WAIT_MS = 6e4;
|
|
28375
|
+
var NEVER_PARCEL = /* @__PURE__ */ new Set(["PING", "PONG", "BUNDLE_CHUNK", "BUNDLE_CSS_CHUNK"]);
|
|
28376
|
+
var RETRY_DELAYS_MS = [500, 1500];
|
|
28377
|
+
var REQUEST_TIMEOUT_MS = 6e4;
|
|
28378
|
+
var MAX_DEPTH = 32;
|
|
28379
|
+
var PARCEL_FIELD = "__parcel";
|
|
28380
|
+
var FILE_FIELD = "__parcelFile";
|
|
28381
|
+
var PART_FIELD = "__parcelPart";
|
|
28382
|
+
var partOf = (payload) => {
|
|
28383
|
+
const p = payload?.[PART_FIELD];
|
|
28384
|
+
return p && typeof p.id === "string" && typeof p.data === "string" && Number.isInteger(p.total) && Number.isInteger(p.index) && p.total > 0 && p.index >= 0 && p.index < p.total ? p : null;
|
|
28385
|
+
};
|
|
28386
|
+
var isRef = (ref) => !!ref && typeof ref === "object" && typeof ref.projectId === "string" && typeof ref.hash === "string" && typeof ref.ext === "string" && typeof ref.size === "number";
|
|
28387
|
+
function parcelRefOf(payload) {
|
|
28388
|
+
const ref = payload?.[PARCEL_FIELD];
|
|
28389
|
+
return isRef(ref) ? ref : null;
|
|
28390
|
+
}
|
|
28391
|
+
var fileRefOf = (value) => {
|
|
28392
|
+
const ref = value?.[FILE_FIELD];
|
|
28393
|
+
return typeof ref?.contentType === "string" && isRef(ref) ? ref : null;
|
|
28394
|
+
};
|
|
28395
|
+
var sha256Hex = (bytes) => (0, import_crypto7.createHash)("sha256").update(bytes).digest("hex");
|
|
28396
|
+
var sleep = (ms) => new Promise((resolve4) => setTimeout(resolve4, ms));
|
|
28397
|
+
var ParcelHttpError = class extends Error {
|
|
28398
|
+
constructor(message, status) {
|
|
28399
|
+
super(message);
|
|
28400
|
+
this.status = status;
|
|
28401
|
+
}
|
|
28402
|
+
};
|
|
28403
|
+
var retryable = (err) => !(err instanceof ParcelHttpError) || err.status >= 500 || err.status === 408 || err.status === 429;
|
|
28404
|
+
async function withRetries(what, run) {
|
|
28405
|
+
let lastError;
|
|
28406
|
+
for (let attempt = 0; attempt <= RETRY_DELAYS_MS.length; attempt++) {
|
|
28407
|
+
try {
|
|
28408
|
+
return await run();
|
|
28409
|
+
} catch (err) {
|
|
28410
|
+
lastError = err;
|
|
28411
|
+
if (!retryable(err) || attempt === RETRY_DELAYS_MS.length) break;
|
|
28412
|
+
logger.warn(`[parcels] ${what} failed (attempt ${attempt + 1}), retrying: ${err?.message ?? err}`);
|
|
28413
|
+
await sleep(RETRY_DELAYS_MS[attempt] ?? 0);
|
|
28414
|
+
}
|
|
28415
|
+
}
|
|
28416
|
+
throw lastError;
|
|
28417
|
+
}
|
|
28418
|
+
var ParcelClient = class {
|
|
28419
|
+
constructor(opts) {
|
|
28420
|
+
this.opts = opts;
|
|
28421
|
+
this.base = opts.apiUrl.replace(/\/+$/, "");
|
|
28422
|
+
this.fetchImpl = opts.fetchImpl ?? fetch;
|
|
28423
|
+
this.timeoutMs = opts.timeoutMs ?? REQUEST_TIMEOUT_MS;
|
|
28424
|
+
}
|
|
28425
|
+
url(ref) {
|
|
28426
|
+
return `${this.base}/parcels/${encodeURIComponent(ref.projectId)}/${ref.hash}.${ref.ext}`;
|
|
28427
|
+
}
|
|
28428
|
+
async call(url, init) {
|
|
28429
|
+
const res = await this.fetchImpl(url, {
|
|
28430
|
+
...init,
|
|
28431
|
+
headers: { ...init.headers, Authorization: `Bearer ${this.opts.apiKey}` },
|
|
28432
|
+
signal: AbortSignal.timeout(this.timeoutMs)
|
|
28433
|
+
});
|
|
28434
|
+
if (!res.ok) {
|
|
28435
|
+
const text = await res.text().catch(() => "");
|
|
28436
|
+
throw new ParcelHttpError(`HTTP ${res.status}${text ? ` ${text.slice(0, 200)}` : ""}`, res.status);
|
|
28437
|
+
}
|
|
28438
|
+
return res;
|
|
28439
|
+
}
|
|
28440
|
+
/** The receipt the bytes would get, without storing them. */
|
|
28441
|
+
refFor(bytes, ext) {
|
|
28442
|
+
return { projectId: this.opts.projectId, hash: sha256Hex(bytes), ext, size: bytes.byteLength };
|
|
28443
|
+
}
|
|
28444
|
+
/** Stores the bytes and returns the receipt for them. */
|
|
28445
|
+
async upload(bytes, ext = "json", contentType = "application/json") {
|
|
28446
|
+
if (bytes.byteLength > MAX_PARCEL_BYTES) {
|
|
28447
|
+
throw new Error(`Parcel too large (${bytes.byteLength} bytes, limit ${MAX_PARCEL_BYTES})`);
|
|
28448
|
+
}
|
|
28449
|
+
const ref = this.refFor(bytes, ext);
|
|
28450
|
+
await withRetries(
|
|
28451
|
+
"upload",
|
|
28452
|
+
() => this.call(this.url(ref), { method: "PUT", headers: { "Content-Type": contentType }, body: bytes })
|
|
28453
|
+
);
|
|
28454
|
+
return ref;
|
|
28455
|
+
}
|
|
28456
|
+
/** The bytes behind a receipt, checked against its hash. */
|
|
28457
|
+
async download(ref) {
|
|
28458
|
+
return withRetries("download", async () => {
|
|
28459
|
+
const res = await this.call(this.url(ref), { method: "GET" });
|
|
28460
|
+
const bytes = new Uint8Array(await res.arrayBuffer());
|
|
28461
|
+
if (sha256Hex(bytes) !== ref.hash) {
|
|
28462
|
+
throw new ParcelHttpError("Parcel content does not match its hash", 502);
|
|
28463
|
+
}
|
|
28464
|
+
return bytes;
|
|
28465
|
+
});
|
|
28466
|
+
}
|
|
28467
|
+
};
|
|
28468
|
+
var ParcelFile = class {
|
|
28469
|
+
constructor(ref, client) {
|
|
28470
|
+
this.ref = ref;
|
|
28471
|
+
this.client = client;
|
|
28472
|
+
this.name = ref.name;
|
|
28473
|
+
this.size = ref.size;
|
|
28474
|
+
this.contentType = ref.contentType;
|
|
28475
|
+
}
|
|
28476
|
+
/**
|
|
28477
|
+
* The file's bytes, checked against their hash — from the message itself when
|
|
28478
|
+
* it carried them (storage was down), otherwise from storage. Throws if they
|
|
28479
|
+
* cannot be fetched.
|
|
28480
|
+
*/
|
|
28481
|
+
async download() {
|
|
28482
|
+
if (typeof this.ref.data === "string") {
|
|
28483
|
+
const bytes = new Uint8Array(Buffer.from(this.ref.data, "base64"));
|
|
28484
|
+
if (sha256Hex(bytes) !== this.ref.hash) throw new Error("File content does not match its hash");
|
|
28485
|
+
return bytes;
|
|
28486
|
+
}
|
|
28487
|
+
if (!this.client) throw new Error("Cannot download files: SA_API_URL or the API key is not set");
|
|
28488
|
+
return this.client.download(this.ref);
|
|
28489
|
+
}
|
|
28490
|
+
toJSON() {
|
|
28491
|
+
return { [FILE_FIELD]: this.ref };
|
|
28492
|
+
}
|
|
28493
|
+
};
|
|
28494
|
+
var isBinary = (v) => v instanceof Uint8Array || v instanceof ArrayBuffer;
|
|
28495
|
+
var isPlain = (v) => {
|
|
28496
|
+
if (!v || typeof v !== "object") return false;
|
|
28497
|
+
const proto = Object.getPrototypeOf(v);
|
|
28498
|
+
return proto === Object.prototype || proto === null;
|
|
28499
|
+
};
|
|
28500
|
+
function hasBinary(value, depth = 0) {
|
|
28501
|
+
if (isBinary(value)) return true;
|
|
28502
|
+
if (depth >= MAX_DEPTH) return false;
|
|
28503
|
+
if (Array.isArray(value)) return value.some((v) => hasBinary(v, depth + 1));
|
|
28504
|
+
if (isPlain(value)) return Object.values(value).some((v) => hasBinary(v, depth + 1));
|
|
28505
|
+
return false;
|
|
28506
|
+
}
|
|
28507
|
+
function hasFileRef(value, depth = 0) {
|
|
28508
|
+
if (fileRefOf(value)) return true;
|
|
28509
|
+
if (depth >= MAX_DEPTH) return false;
|
|
28510
|
+
if (Array.isArray(value)) return value.some((v) => hasFileRef(v, depth + 1));
|
|
28511
|
+
if (isPlain(value)) return Object.values(value).some((v) => hasFileRef(v, depth + 1));
|
|
28512
|
+
return false;
|
|
28513
|
+
}
|
|
28514
|
+
var extOf = (name) => typeof name === "string" && /\.([a-z0-9]{1,10})$/i.exec(name)?.[1]?.toLowerCase() || "bin";
|
|
28515
|
+
async function uploadFiles(value, client, storage, parent, depth = 0) {
|
|
28516
|
+
if (isBinary(value)) {
|
|
28517
|
+
const bytes = value instanceof Uint8Array ? value : new Uint8Array(value);
|
|
28518
|
+
const name = parent?.name ?? parent?.fileName;
|
|
28519
|
+
const contentType = String(parent?.contentType ?? parent?.mimeType ?? "application/octet-stream");
|
|
28520
|
+
const named = typeof name === "string" ? { name } : {};
|
|
28521
|
+
if (!storage.down) {
|
|
28522
|
+
try {
|
|
28523
|
+
const ref = await client.upload(bytes, extOf(name), contentType);
|
|
28524
|
+
const fileRef2 = { ...ref, contentType, ...named };
|
|
28525
|
+
return { [FILE_FIELD]: fileRef2 };
|
|
28526
|
+
} catch (err) {
|
|
28527
|
+
if (bytes.byteLength > MAX_PARCEL_BYTES) throw err;
|
|
28528
|
+
storage.down = true;
|
|
28529
|
+
logger.warn(`[parcels] file upload failed, sending it in the message instead: ${err?.message ?? err}`);
|
|
28530
|
+
}
|
|
28531
|
+
}
|
|
28532
|
+
const fileRef = {
|
|
28533
|
+
...client.refFor(bytes, extOf(name)),
|
|
28534
|
+
contentType,
|
|
28535
|
+
...named,
|
|
28536
|
+
data: Buffer.from(bytes).toString("base64")
|
|
28537
|
+
};
|
|
28538
|
+
return { [FILE_FIELD]: fileRef };
|
|
28539
|
+
}
|
|
28540
|
+
if (depth >= MAX_DEPTH) return value;
|
|
28541
|
+
if (Array.isArray(value)) {
|
|
28542
|
+
const out = [];
|
|
28543
|
+
for (const v of value) out.push(await uploadFiles(v, client, storage, void 0, depth + 1));
|
|
28544
|
+
return out;
|
|
28545
|
+
}
|
|
28546
|
+
if (isPlain(value)) {
|
|
28547
|
+
const out = {};
|
|
28548
|
+
for (const [k, v] of Object.entries(value)) out[k] = await uploadFiles(v, client, storage, value, depth + 1);
|
|
28549
|
+
return out;
|
|
28550
|
+
}
|
|
28551
|
+
return value;
|
|
28552
|
+
}
|
|
28553
|
+
function splitPayload(payload) {
|
|
28554
|
+
const bytes = Buffer.from(JSON.stringify(payload ?? null), "utf8");
|
|
28555
|
+
const total = Math.max(1, Math.ceil(bytes.byteLength / PART_BYTES));
|
|
28556
|
+
const id = (0, import_crypto7.randomUUID)();
|
|
28557
|
+
return Array.from({ length: total }, (_, index) => ({
|
|
28558
|
+
id,
|
|
28559
|
+
index,
|
|
28560
|
+
total,
|
|
28561
|
+
data: bytes.subarray(index * PART_BYTES, (index + 1) * PART_BYTES).toString("base64")
|
|
28562
|
+
}));
|
|
28563
|
+
}
|
|
28564
|
+
function toFileHandles(value, client, depth = 0) {
|
|
28565
|
+
const ref = fileRefOf(value);
|
|
28566
|
+
if (ref) return new ParcelFile(ref, client);
|
|
28567
|
+
if (depth >= MAX_DEPTH) return value;
|
|
28568
|
+
if (Array.isArray(value)) return value.map((v) => toFileHandles(v, client, depth + 1));
|
|
28569
|
+
if (isPlain(value)) {
|
|
28570
|
+
const out = {};
|
|
28571
|
+
for (const [k, v] of Object.entries(value)) out[k] = toFileHandles(v, client, depth + 1);
|
|
28572
|
+
return out;
|
|
28573
|
+
}
|
|
28574
|
+
return value;
|
|
28575
|
+
}
|
|
28576
|
+
var asError = (message, reason) => ({
|
|
28577
|
+
...message,
|
|
28578
|
+
payload: { error: reason, parcelError: true }
|
|
28579
|
+
});
|
|
28580
|
+
var recipientOf = (message) => {
|
|
28581
|
+
const id = message.to?.id;
|
|
28582
|
+
return typeof id === "string" ? id : "";
|
|
28583
|
+
};
|
|
28584
|
+
var InOrder = class {
|
|
28585
|
+
/** `onIdle` runs each time the last waiting task finishes. */
|
|
28586
|
+
constructor(onIdle) {
|
|
28587
|
+
this.onIdle = onIdle;
|
|
28588
|
+
this.pending = 0;
|
|
28589
|
+
this.chain = Promise.resolve();
|
|
28590
|
+
}
|
|
28591
|
+
get busy() {
|
|
28592
|
+
return this.pending > 0;
|
|
28593
|
+
}
|
|
28594
|
+
run(task) {
|
|
28595
|
+
this.pending++;
|
|
28596
|
+
this.chain = this.chain.then(task).catch((err) => logger.error("[parcels] queue fault:", err)).finally(() => {
|
|
28597
|
+
if (--this.pending === 0) this.onIdle?.();
|
|
28598
|
+
});
|
|
28599
|
+
}
|
|
28600
|
+
idle() {
|
|
28601
|
+
return this.chain;
|
|
28602
|
+
}
|
|
28603
|
+
};
|
|
28604
|
+
var OutgoingParcels = class {
|
|
28605
|
+
constructor(client, jsonParcels, deliver, threshold = PARCEL_THRESHOLD_BYTES) {
|
|
28606
|
+
this.client = client;
|
|
28607
|
+
this.jsonParcels = jsonParcels;
|
|
28608
|
+
this.deliver = deliver;
|
|
28609
|
+
this.threshold = threshold;
|
|
28610
|
+
/** Only recipients with something waiting; a queue is dropped once it empties. */
|
|
28611
|
+
this.queues = /* @__PURE__ */ new Map();
|
|
28612
|
+
this.warnedNoClient = false;
|
|
28613
|
+
}
|
|
28614
|
+
/**
|
|
28615
|
+
* Same contract as the SDK's `send`: never throws, returns whether the message
|
|
28616
|
+
* went out (or, for a queued one, was accepted).
|
|
28617
|
+
*/
|
|
28618
|
+
send(message) {
|
|
28619
|
+
if (NEVER_PARCEL.has(message.type)) return this.now(message);
|
|
28620
|
+
let files = false;
|
|
28621
|
+
try {
|
|
28622
|
+
files = hasBinary(message.payload);
|
|
28623
|
+
} catch {
|
|
28624
|
+
}
|
|
28625
|
+
if (files && !this.client) {
|
|
28626
|
+
if (!this.warnedNoClient) {
|
|
28627
|
+
this.warnedNoClient = true;
|
|
28628
|
+
logger.warn("[parcels] a message carries a file but SA_API_URL or the API key is missing \u2014 sent as-is");
|
|
28629
|
+
}
|
|
28630
|
+
files = false;
|
|
28631
|
+
}
|
|
28632
|
+
if (!files) {
|
|
28633
|
+
if (!this.jsonParcels || !this.client) return this.now(message);
|
|
28634
|
+
let wire;
|
|
28635
|
+
try {
|
|
28636
|
+
wire = JSON.stringify(message);
|
|
28637
|
+
} catch {
|
|
28638
|
+
return this.now(message);
|
|
28639
|
+
}
|
|
28640
|
+
if (Buffer.byteLength(wire, "utf8") < this.threshold) return this.now(message, wire);
|
|
28641
|
+
}
|
|
28642
|
+
this.queueFor(message).run(async () => {
|
|
28643
|
+
for (const out of await this.prepare(message, files)) this.deliver(out.message, out.wire);
|
|
28644
|
+
});
|
|
28645
|
+
return true;
|
|
28646
|
+
}
|
|
28647
|
+
/** Delivers now, unless something for the same recipient is already waiting — then behind it. */
|
|
28648
|
+
now(message, wire) {
|
|
28649
|
+
if (!this.queues.get(recipientOf(message))?.busy) return this.deliver(message, wire);
|
|
28650
|
+
this.queueFor(message).run(async () => {
|
|
28651
|
+
this.deliver(message, wire);
|
|
28652
|
+
});
|
|
28653
|
+
return true;
|
|
28654
|
+
}
|
|
28655
|
+
queueFor(message) {
|
|
28656
|
+
const key = recipientOf(message);
|
|
28657
|
+
let queue = this.queues.get(key);
|
|
28658
|
+
if (!queue) {
|
|
28659
|
+
queue = new InOrder(() => this.queues.delete(key));
|
|
28660
|
+
this.queues.set(key, queue);
|
|
28661
|
+
}
|
|
28662
|
+
return queue;
|
|
28663
|
+
}
|
|
28664
|
+
/** Resolves when everything queued so far has been handed to the socket. For tests and shutdown. */
|
|
28665
|
+
async idle() {
|
|
28666
|
+
await Promise.all(Array.from(this.queues.values(), (queue) => queue.idle()));
|
|
28667
|
+
}
|
|
28668
|
+
/**
|
|
28669
|
+
* What actually goes on the socket for this message: usually one message (as
|
|
28670
|
+
* is, or as a parcel receipt); several parts when storage is down and the
|
|
28671
|
+
* message is too large for the relay.
|
|
28672
|
+
*/
|
|
28673
|
+
async prepare(message, files) {
|
|
28674
|
+
let current = message;
|
|
28675
|
+
const storage = { down: false };
|
|
28676
|
+
if (files) {
|
|
28677
|
+
const started = Date.now();
|
|
28678
|
+
try {
|
|
28679
|
+
current = { ...message, payload: await uploadFiles(message.payload, this.client, storage) };
|
|
28680
|
+
if (!storage.down) logger.info(`[parcels] ${message.type} ${message.id ?? ""}: file(s) uploaded (${Date.now() - started}ms)`);
|
|
28681
|
+
} catch (err) {
|
|
28682
|
+
const reason2 = err?.message ?? String(err);
|
|
28683
|
+
logger.error(`[parcels] file upload failed for ${message.type}, sending an error instead: ${reason2}`);
|
|
28684
|
+
return [{ message: asError(message, `Could not send a file: ${reason2}`) }];
|
|
28685
|
+
}
|
|
28686
|
+
}
|
|
28687
|
+
let wire;
|
|
28688
|
+
try {
|
|
28689
|
+
wire = JSON.stringify(current);
|
|
28690
|
+
} catch {
|
|
28691
|
+
return [{ message: current }];
|
|
28692
|
+
}
|
|
28693
|
+
const size = Buffer.byteLength(wire, "utf8");
|
|
28694
|
+
if (size < this.threshold) return [{ message: current, wire }];
|
|
28695
|
+
let reason = "large-JSON parcels are off";
|
|
28696
|
+
if (this.jsonParcels && !storage.down) {
|
|
28697
|
+
const started = Date.now();
|
|
28698
|
+
try {
|
|
28699
|
+
const bytes = Buffer.from(JSON.stringify(current.payload ?? null), "utf8");
|
|
28700
|
+
const ref = await this.client.upload(bytes);
|
|
28701
|
+
logger.info(
|
|
28702
|
+
`[parcels] ${message.type} ${message.id ?? ""} \u2192 parcel ${ref.hash.slice(0, 12)} (${ref.size} bytes, ${Date.now() - started}ms)`
|
|
28703
|
+
);
|
|
28704
|
+
return [{ message: { ...current, payload: { [PARCEL_FIELD]: ref } } }];
|
|
28705
|
+
} catch (err) {
|
|
28706
|
+
reason = err?.message ?? String(err);
|
|
28707
|
+
}
|
|
28708
|
+
} else if (storage.down) {
|
|
28709
|
+
reason = "storage is down";
|
|
28710
|
+
}
|
|
28711
|
+
if (size <= MAX_INLINE_BYTES) {
|
|
28712
|
+
if (this.jsonParcels) logger.warn(`[parcels] ${message.type} not stored (${reason}), sending inline (${size} bytes)`);
|
|
28713
|
+
return [{ message: current, wire }];
|
|
28714
|
+
}
|
|
28715
|
+
if (size > MAX_PARCEL_BYTES) {
|
|
28716
|
+
logger.error(`[parcels] ${message.type} (${size} bytes) is too large to send`);
|
|
28717
|
+
return [{ message: asError(message, `The message was too large to deliver (${size} bytes): ${reason}`) }];
|
|
28718
|
+
}
|
|
28719
|
+
const parts = splitPayload(current.payload);
|
|
28720
|
+
logger.warn(`[parcels] ${message.type} not stored (${reason}), sending in ${parts.length} parts (${size} bytes)`);
|
|
28721
|
+
return parts.map((part) => ({ message: { ...current, payload: { [PART_FIELD]: part } } }));
|
|
28722
|
+
}
|
|
28723
|
+
};
|
|
28724
|
+
var IncomingParcels = class {
|
|
28725
|
+
constructor(client, dispatch) {
|
|
28726
|
+
this.client = client;
|
|
28727
|
+
this.dispatch = dispatch;
|
|
28728
|
+
this.queue = new InOrder();
|
|
28729
|
+
/** Split messages still waiting for parts, by part id. */
|
|
28730
|
+
this.partial = /* @__PURE__ */ new Map();
|
|
28731
|
+
/** Split messages already rebuilt, so a second copy of their parts is ignored. */
|
|
28732
|
+
this.rebuilt = /* @__PURE__ */ new Set();
|
|
28733
|
+
}
|
|
28734
|
+
/**
|
|
28735
|
+
* `raw` is the message's text when it is at hand: a message that never
|
|
28736
|
+
* mentions a file placeholder is not searched for one.
|
|
28737
|
+
*/
|
|
28738
|
+
receive(message, raw) {
|
|
28739
|
+
const part = partOf(message?.payload);
|
|
28740
|
+
if (part) {
|
|
28741
|
+
const whole = this.collect(message, part);
|
|
28742
|
+
if (whole) this.receive(whole);
|
|
28743
|
+
return;
|
|
28744
|
+
}
|
|
28745
|
+
const ref = parcelRefOf(message?.payload);
|
|
28746
|
+
if (!ref) {
|
|
28747
|
+
const withFiles = raw === void 0 || raw.includes(FILE_FIELD) ? this.withFiles(message) : message;
|
|
28748
|
+
if (this.queue.busy) {
|
|
28749
|
+
this.queue.run(async () => this.dispatch(withFiles));
|
|
28750
|
+
} else {
|
|
28751
|
+
this.dispatch(withFiles);
|
|
28752
|
+
}
|
|
28753
|
+
return;
|
|
28754
|
+
}
|
|
28755
|
+
this.queue.run(async () => this.dispatch(await this.restore(message, ref)));
|
|
28756
|
+
}
|
|
28757
|
+
idle() {
|
|
28758
|
+
return this.queue.idle();
|
|
28759
|
+
}
|
|
28760
|
+
/**
|
|
28761
|
+
* Holds one part; once all of them are here, the original message (envelope
|
|
28762
|
+
* from the last part, payload rebuilt). Null while parts are still missing.
|
|
28763
|
+
*/
|
|
28764
|
+
collect(message, part) {
|
|
28765
|
+
if (this.rebuilt.has(part.id)) return null;
|
|
28766
|
+
let entry = this.partial.get(part.id);
|
|
28767
|
+
if (!entry) {
|
|
28768
|
+
const timer = setTimeout(() => {
|
|
28769
|
+
this.partial.delete(part.id);
|
|
28770
|
+
logger.warn(`[parcels] ${message.type}: gave up waiting for the rest of a split message (${part.id})`);
|
|
28771
|
+
}, PART_WAIT_MS).unref();
|
|
28772
|
+
entry = { parts: new Array(part.total), got: 0, timer };
|
|
28773
|
+
this.partial.set(part.id, entry);
|
|
28774
|
+
}
|
|
28775
|
+
if (entry.parts.length !== part.total || entry.parts[part.index] !== void 0) return null;
|
|
28776
|
+
entry.parts[part.index] = part.data;
|
|
28777
|
+
if (++entry.got < part.total) return null;
|
|
28778
|
+
clearTimeout(entry.timer);
|
|
28779
|
+
this.partial.delete(part.id);
|
|
28780
|
+
this.rebuilt.add(part.id);
|
|
28781
|
+
setTimeout(() => this.rebuilt.delete(part.id), PART_WAIT_MS).unref();
|
|
28782
|
+
try {
|
|
28783
|
+
const bytes = Buffer.concat(entry.parts.map((data) => Buffer.from(data, "base64")));
|
|
28784
|
+
logger.info(`[parcels] ${message.type} ${message.id ?? ""} rebuilt from ${part.total} parts (${bytes.byteLength} bytes)`);
|
|
28785
|
+
return { ...message, payload: JSON.parse(bytes.toString("utf8")) };
|
|
28786
|
+
} catch (err) {
|
|
28787
|
+
const reason = err?.message ?? String(err);
|
|
28788
|
+
logger.error(`[parcels] could not rebuild a split ${message.type}: ${reason}`);
|
|
28789
|
+
return asError(message, `Could not rebuild the message: ${reason}`);
|
|
28790
|
+
}
|
|
28791
|
+
}
|
|
28792
|
+
withFiles(message) {
|
|
28793
|
+
try {
|
|
28794
|
+
return hasFileRef(message.payload) ? { ...message, payload: toFileHandles(message.payload, this.client) } : message;
|
|
28795
|
+
} catch {
|
|
28796
|
+
return message;
|
|
28797
|
+
}
|
|
28798
|
+
}
|
|
28799
|
+
async restore(message, ref) {
|
|
28800
|
+
if (!this.client) {
|
|
28801
|
+
logger.error(`[parcels] received a parcel for ${message.type} but SA_API_URL or the API key is not set`);
|
|
28802
|
+
return asError(message, "This engine cannot download parcels: SA_API_URL or the API key is not set.");
|
|
28803
|
+
}
|
|
28804
|
+
const started = Date.now();
|
|
28805
|
+
try {
|
|
28806
|
+
const bytes = await this.client.download(ref);
|
|
28807
|
+
const payload = JSON.parse(Buffer.from(bytes).toString("utf8"));
|
|
28808
|
+
logger.info(
|
|
28809
|
+
`[parcels] ${message.type} ${message.id ?? ""} \u2190 parcel ${ref.hash.slice(0, 12)} (${ref.size} bytes, ${Date.now() - started}ms)`
|
|
28810
|
+
);
|
|
28811
|
+
return this.withFiles({ ...message, payload });
|
|
28812
|
+
} catch (err) {
|
|
28813
|
+
const reason = err?.message ?? String(err);
|
|
28814
|
+
logger.error(`[parcels] download failed for ${message.type} (${ref.hash.slice(0, 12)}): ${reason}`);
|
|
28815
|
+
return asError(message, `Could not download the message content: ${reason}`);
|
|
28816
|
+
}
|
|
28817
|
+
}
|
|
28818
|
+
};
|
|
28819
|
+
var parcelsFlagFromEnv = () => !/^(off|false|0|no)$/i.test((process.env.SA_PARCELS ?? "").trim());
|
|
28820
|
+
|
|
28364
28821
|
// src/userResponse/scripts/fmt.ts
|
|
28365
28822
|
var DASH = "\u2014";
|
|
28366
28823
|
var DEFAULT_FMT_CONFIG = { locale: "en-IN", currency: "INR" };
|
|
@@ -29712,6 +30169,18 @@ var SuperatomSDK = class {
|
|
|
29712
30169
|
this.type = config2.type || "data-agent";
|
|
29713
30170
|
this.bundleDir = config2.bundleDir;
|
|
29714
30171
|
this.url = config2.url || process.env.SA_WEBSOCKET_URL || DEFAULT_WS_URL;
|
|
30172
|
+
const apiUrl = config2.apiUrl || process.env.SA_API_URL;
|
|
30173
|
+
this.parcelClient = apiUrl && this.apiKey ? new ParcelClient({ apiUrl, projectId: this.projectId, apiKey: this.apiKey }) : null;
|
|
30174
|
+
const jsonParcels = config2.parcels ?? parcelsFlagFromEnv();
|
|
30175
|
+
if (jsonParcels && !this.parcelClient) {
|
|
30176
|
+
logger.warn("[parcels] parcels are on but SA_API_URL or the API key is missing \u2014 large messages go inline");
|
|
30177
|
+
}
|
|
30178
|
+
this.outgoing = new OutgoingParcels(
|
|
30179
|
+
this.parcelClient,
|
|
30180
|
+
jsonParcels,
|
|
30181
|
+
(message, wire) => this.deliver(message, wire)
|
|
30182
|
+
);
|
|
30183
|
+
this.incoming = new IncomingParcels(this.parcelClient, (parsed) => this.dispatchMessage(parsed));
|
|
29715
30184
|
this.anthropicApiKey = config2.ANTHROPIC_API_KEY || process.env.ANTHROPIC_API_KEY || "";
|
|
29716
30185
|
this.groqApiKey = config2.GROQ_API_KEY || process.env.GROQ_API_KEY || "";
|
|
29717
30186
|
this.geminiApiKey = config2.GEMINI_API_KEY || process.env.GEMINI_API_KEY || "";
|
|
@@ -29863,11 +30332,24 @@ var SuperatomSDK = class {
|
|
|
29863
30332
|
});
|
|
29864
30333
|
}
|
|
29865
30334
|
/**
|
|
29866
|
-
* Handle incoming WebSocket messages
|
|
30335
|
+
* Handle incoming WebSocket messages.
|
|
30336
|
+
*
|
|
30337
|
+
* Parsed here, then handed to the parcel layer, which restores a parcel's
|
|
30338
|
+
* payload (and holds later messages back until it has) before routing.
|
|
29867
30339
|
*/
|
|
29868
30340
|
handleMessage(data) {
|
|
30341
|
+
let parsed;
|
|
30342
|
+
try {
|
|
30343
|
+
parsed = JSON.parse(data);
|
|
30344
|
+
} catch (error) {
|
|
30345
|
+
logger.error("Failed to parse incoming message:", error);
|
|
30346
|
+
return;
|
|
30347
|
+
}
|
|
30348
|
+
this.incoming.receive(parsed, data);
|
|
30349
|
+
}
|
|
30350
|
+
/** Routes one parsed message — everything `handleMessage` did after parsing. */
|
|
30351
|
+
dispatchMessage(parsed) {
|
|
29869
30352
|
try {
|
|
29870
|
-
const parsed = JSON.parse(data);
|
|
29871
30353
|
const message = IncomingMessageSchema.parse(parsed);
|
|
29872
30354
|
logger.debug("Received message:", message.type);
|
|
29873
30355
|
switch (message.type) {
|
|
@@ -30057,8 +30539,19 @@ var SuperatomSDK = class {
|
|
|
30057
30539
|
* Send a message to the Superatom service.
|
|
30058
30540
|
* Returns true if the message was sent, false if the WebSocket is not connected.
|
|
30059
30541
|
* Does NOT throw on closed connections — callers can check the return value if needed.
|
|
30542
|
+
*
|
|
30543
|
+
* Large payloads and files go as parcels (see ./parcels): a file is any
|
|
30544
|
+
* Buffer / Uint8Array / ArrayBuffer inside the payload, with an optional
|
|
30545
|
+
* `name` and `contentType` beside it — e.g. `payload: { file: { name, bytes } }`.
|
|
30546
|
+
* Such a message goes out once its upload is done (true = accepted), and later
|
|
30547
|
+
* messages keep their order. A received file arrives as a `ParcelFile` handle
|
|
30548
|
+
* whose `download()` fetches the bytes.
|
|
30060
30549
|
*/
|
|
30061
30550
|
send(message) {
|
|
30551
|
+
return this.outgoing.send(message);
|
|
30552
|
+
}
|
|
30553
|
+
/** Puts one message on the socket, or in the reconnect outbox. `wire` is its JSON, when already made. */
|
|
30554
|
+
deliver(message, wire) {
|
|
30062
30555
|
if (!this.ws || !this.connected) {
|
|
30063
30556
|
logger.warn("WebSocket is not connected, queueing message for reconnect:", message.type);
|
|
30064
30557
|
this.queuePendingMessage(message);
|
|
@@ -30070,7 +30563,7 @@ var SuperatomSDK = class {
|
|
|
30070
30563
|
return false;
|
|
30071
30564
|
}
|
|
30072
30565
|
try {
|
|
30073
|
-
const payload = JSON.stringify(message);
|
|
30566
|
+
const payload = wire ?? JSON.stringify(message);
|
|
30074
30567
|
this.ws.send(payload);
|
|
30075
30568
|
return true;
|
|
30076
30569
|
} catch (error) {
|
|
@@ -30323,7 +30816,7 @@ var SuperatomSDK = class {
|
|
|
30323
30816
|
waiters.push(finish);
|
|
30324
30817
|
this.registryWaiters.set(resType, waiters);
|
|
30325
30818
|
const sent = this.send({
|
|
30326
|
-
id: `${reqType.toLowerCase()}_${(0,
|
|
30819
|
+
id: `${reqType.toLowerCase()}_${(0, import_crypto8.randomUUID)()}`,
|
|
30327
30820
|
type: reqType,
|
|
30328
30821
|
from: { type: this.type },
|
|
30329
30822
|
to: { type: "runtime" },
|
|
@@ -30414,6 +30907,8 @@ var SuperatomSDK = class {
|
|
|
30414
30907
|
MainAgent,
|
|
30415
30908
|
MissingRequestContextError,
|
|
30416
30909
|
NUMERIC_KEYS,
|
|
30910
|
+
PARCEL_THRESHOLD_BYTES,
|
|
30911
|
+
ParcelFile,
|
|
30417
30912
|
STORAGE_CONFIG,
|
|
30418
30913
|
ScriptMatcher,
|
|
30419
30914
|
ScriptStore,
|