@caspian2026/agent-sdk 0.0.29 → 0.0.31
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/gateway/index.js +158 -1
- package/dist/gateway/pdfworker.js +43622 -0
- package/dist/gateway/worker.js +1649 -1524
- package/package.json +4 -1
package/dist/gateway/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { createInterface } from "node:readline";
|
|
3
3
|
import { resolve, dirname } from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { Worker as Worker2 } from "node:worker_threads";
|
|
5
6
|
|
|
6
7
|
// src/entrypoints/gateway/protocol.ts
|
|
7
8
|
function writeJsonRpc(msg) {
|
|
@@ -49,6 +50,18 @@ class SessionRouter {
|
|
|
49
50
|
start() {
|
|
50
51
|
this.idleTimer = setInterval(() => this.evictIdle(), IDLE_CHECK_INTERVAL_MS);
|
|
51
52
|
}
|
|
53
|
+
broadcastStreamControl(paused) {
|
|
54
|
+
for (const entry of this.sessions.values()) {
|
|
55
|
+
if (entry.destroyed)
|
|
56
|
+
continue;
|
|
57
|
+
try {
|
|
58
|
+
entry.worker.postMessage({ type: "stream_control", paused });
|
|
59
|
+
} catch (err) {
|
|
60
|
+
process.stderr.write(`[SessionRouter] Failed to broadcast stream_control to ${entry.sessionKey}: ${err instanceof Error ? err.message : String(err)}
|
|
61
|
+
`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
52
65
|
async create(sessionKey, config) {
|
|
53
66
|
if (this.sessions.has(sessionKey)) {
|
|
54
67
|
process.stderr.write(`[SessionRouter] Session already exists, destroying old: ${sessionKey}
|
|
@@ -256,9 +269,57 @@ class SessionRouter {
|
|
|
256
269
|
// src/entrypoints/gateway/index.ts
|
|
257
270
|
var __dirname2 = dirname(fileURLToPath(import.meta.url));
|
|
258
271
|
var WORKER_PATH = resolve(__dirname2, "worker.js");
|
|
272
|
+
var PDF_WORKER_PATH = resolve(__dirname2, "pdfworker.js");
|
|
273
|
+
var pdfWorker = null;
|
|
274
|
+
var pdfInflight = new Map;
|
|
275
|
+
function ensurePdfWorker() {
|
|
276
|
+
if (pdfWorker)
|
|
277
|
+
return pdfWorker;
|
|
278
|
+
const w = new Worker2(PDF_WORKER_PATH);
|
|
279
|
+
w.unref();
|
|
280
|
+
w.on("message", (msg) => {
|
|
281
|
+
if (msg?.type !== "pdf-response" || typeof msg.id !== "number")
|
|
282
|
+
return;
|
|
283
|
+
const sessionKey = pdfInflight.get(msg.id);
|
|
284
|
+
if (sessionKey === undefined)
|
|
285
|
+
return;
|
|
286
|
+
pdfInflight.delete(msg.id);
|
|
287
|
+
try {
|
|
288
|
+
router.send(sessionKey, {
|
|
289
|
+
type: "pdf-response",
|
|
290
|
+
id: msg.id,
|
|
291
|
+
ok: msg.ok ?? false,
|
|
292
|
+
payload: msg.payload
|
|
293
|
+
});
|
|
294
|
+
} catch {}
|
|
295
|
+
});
|
|
296
|
+
w.on("error", (err) => {
|
|
297
|
+
process.stderr.write(`[PDFService] worker error: ${err instanceof Error ? err.message : String(err)}
|
|
298
|
+
`);
|
|
299
|
+
});
|
|
300
|
+
w.on("exit", (code) => {
|
|
301
|
+
process.stderr.write(`[PDFService] worker exited (code ${code}); failing in-flight requests, restarting on demand
|
|
302
|
+
`);
|
|
303
|
+
for (const [id, sessionKey] of pdfInflight) {
|
|
304
|
+
try {
|
|
305
|
+
router.send(sessionKey, {
|
|
306
|
+
type: "pdf-response",
|
|
307
|
+
id,
|
|
308
|
+
ok: false,
|
|
309
|
+
payload: { message: "PDF service worker exited unexpectedly" }
|
|
310
|
+
});
|
|
311
|
+
} catch {}
|
|
312
|
+
}
|
|
313
|
+
pdfInflight.clear();
|
|
314
|
+
pdfWorker = null;
|
|
315
|
+
});
|
|
316
|
+
pdfWorker = w;
|
|
317
|
+
return w;
|
|
318
|
+
}
|
|
259
319
|
var outputQueue = "";
|
|
260
320
|
var outputBlocked = false;
|
|
261
321
|
var MAX_OUTPUT_BUFFER_SIZE = 10 * 1024 * 1024;
|
|
322
|
+
var SALVAGE_TARGET_SIZE = 8 * 1024 * 1024;
|
|
262
323
|
var lastWorkerErrors = new Map;
|
|
263
324
|
function notify(msg) {
|
|
264
325
|
let line;
|
|
@@ -270,6 +331,16 @@ function notify(msg) {
|
|
|
270
331
|
return;
|
|
271
332
|
}
|
|
272
333
|
if (outputQueue.length + line.length > MAX_OUTPUT_BUFFER_SIZE) {
|
|
334
|
+
const headroom = Math.max(0, MAX_OUTPUT_BUFFER_SIZE - outputQueue.length);
|
|
335
|
+
const salvaged = salvageOversizedMessage(msg, headroom);
|
|
336
|
+
if (salvaged !== null && outputQueue.length + salvaged.length <= MAX_OUTPUT_BUFFER_SIZE) {
|
|
337
|
+
process.stderr.write(`[Gateway] Output buffer overflow (${outputQueue.length + line.length} > ${MAX_OUTPUT_BUFFER_SIZE}), salvaged ${msg.method} by truncation
|
|
338
|
+
`);
|
|
339
|
+
outputQueue += salvaged;
|
|
340
|
+
flushOutput();
|
|
341
|
+
maybePauseStreams();
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
273
344
|
process.stderr.write(`[Gateway] Output buffer overflow (${outputQueue.length + line.length} > ${MAX_OUTPUT_BUFFER_SIZE}), dropping ${msg.method}
|
|
274
345
|
`);
|
|
275
346
|
const sessionKey = msg.params?.session_key;
|
|
@@ -281,10 +352,84 @@ function notify(msg) {
|
|
|
281
352
|
});
|
|
282
353
|
} catch {}
|
|
283
354
|
}
|
|
355
|
+
maybePauseStreams();
|
|
284
356
|
return;
|
|
285
357
|
}
|
|
286
358
|
outputQueue += line;
|
|
287
359
|
flushOutput();
|
|
360
|
+
maybePauseStreams();
|
|
361
|
+
}
|
|
362
|
+
var STREAM_PAUSE_HIGH_WATER = 3 * 1024 * 1024;
|
|
363
|
+
var streamPaused = false;
|
|
364
|
+
function maybePauseStreams() {
|
|
365
|
+
if (!streamPaused && outputBlocked && outputQueue.length > STREAM_PAUSE_HIGH_WATER) {
|
|
366
|
+
streamPaused = true;
|
|
367
|
+
router.broadcastStreamControl(true);
|
|
368
|
+
process.stderr.write(`[Gateway] Stream paused: output queue at ${outputQueue.length} bytes under backpressure
|
|
369
|
+
`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
function resumeStreamsIfDrained() {
|
|
373
|
+
if (streamPaused && !outputBlocked && outputQueue.length === 0) {
|
|
374
|
+
streamPaused = false;
|
|
375
|
+
router.broadcastStreamControl(false);
|
|
376
|
+
process.stderr.write(`[Gateway] Stream resumed: host drained
|
|
377
|
+
`);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function collectSalvageSlots(node, slots) {
|
|
381
|
+
if (Array.isArray(node)) {
|
|
382
|
+
node.forEach((child, i) => {
|
|
383
|
+
if (typeof child === "string" && child.length > 4096) {
|
|
384
|
+
slots.push({
|
|
385
|
+
size: child.length,
|
|
386
|
+
replace: () => {
|
|
387
|
+
node[i] = `[gateway-truncated: ${Math.round(child.length / 1024)}KB]`;
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
} else {
|
|
391
|
+
collectSalvageSlots(child, slots);
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (typeof node === "object" && node !== null) {
|
|
397
|
+
for (const [key, value] of Object.entries(node)) {
|
|
398
|
+
if (typeof value === "string" && value.length > 4096) {
|
|
399
|
+
slots.push({
|
|
400
|
+
size: value.length,
|
|
401
|
+
replace: () => {
|
|
402
|
+
node[key] = `[gateway-truncated: ${Math.round(value.length / 1024)}KB]`;
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
} else {
|
|
406
|
+
collectSalvageSlots(value, slots);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
function salvageOversizedMessage(msg, targetSize = SALVAGE_TARGET_SIZE) {
|
|
412
|
+
try {
|
|
413
|
+
const copy = JSON.parse(JSON.stringify(msg));
|
|
414
|
+
copy.jsonrpc = "2.0";
|
|
415
|
+
const slots = [];
|
|
416
|
+
collectSalvageSlots(copy, slots);
|
|
417
|
+
slots.sort((a, b) => b.size - a.size);
|
|
418
|
+
for (const slot of slots) {
|
|
419
|
+
const serialized = JSON.stringify(copy) + `
|
|
420
|
+
`;
|
|
421
|
+
if (serialized.length <= targetSize)
|
|
422
|
+
break;
|
|
423
|
+
slot.replace();
|
|
424
|
+
}
|
|
425
|
+
const finalLine = JSON.stringify(copy) + `
|
|
426
|
+
`;
|
|
427
|
+
if (finalLine.length > MAX_OUTPUT_BUFFER_SIZE)
|
|
428
|
+
return null;
|
|
429
|
+
return finalLine;
|
|
430
|
+
} catch {
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
288
433
|
}
|
|
289
434
|
function flushOutput() {
|
|
290
435
|
if (outputBlocked || !outputQueue)
|
|
@@ -296,6 +441,7 @@ function flushOutput() {
|
|
|
296
441
|
process.stdout.once("drain", () => {
|
|
297
442
|
outputBlocked = false;
|
|
298
443
|
flushOutput();
|
|
444
|
+
resumeStreamsIfDrained();
|
|
299
445
|
});
|
|
300
446
|
}
|
|
301
447
|
}
|
|
@@ -351,6 +497,12 @@ var router = new SessionRouter({
|
|
|
351
497
|
process.stderr.write(`[Gateway] Session updated: ${sessionKey}
|
|
352
498
|
`);
|
|
353
499
|
break;
|
|
500
|
+
case "pdf-request": {
|
|
501
|
+
const w = ensurePdfWorker();
|
|
502
|
+
pdfInflight.set(msg.id, sessionKey);
|
|
503
|
+
w.postMessage({ type: "pdf-request", id: msg.id, op: msg.op, args: msg.args });
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
354
506
|
}
|
|
355
507
|
},
|
|
356
508
|
onWorkerExit: (sessionKey, code) => {
|
|
@@ -371,7 +523,8 @@ async function handleRequest(req) {
|
|
|
371
523
|
session: config,
|
|
372
524
|
permission,
|
|
373
525
|
env,
|
|
374
|
-
resume
|
|
526
|
+
resume,
|
|
527
|
+
pdfService: true
|
|
375
528
|
};
|
|
376
529
|
try {
|
|
377
530
|
await router.create(session_key, workerConfig);
|
|
@@ -499,3 +652,7 @@ process.on("SIGTERM", async () => {
|
|
|
499
652
|
router.stop();
|
|
500
653
|
process.exit(0);
|
|
501
654
|
});
|
|
655
|
+
export {
|
|
656
|
+
salvageOversizedMessage,
|
|
657
|
+
SALVAGE_TARGET_SIZE
|
|
658
|
+
};
|