@holokai/neuron-sdk 0.4.0 → 0.5.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.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +38 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +38 -16
- package/dist/index.mjs.map +1 -1
- package/dist/ipc.d.mts +1 -1
- package/dist/ipc.d.ts +1 -1
- package/dist/{transport-Cs5tY2IO.d.mts → transport-Ckcwj-Do.d.mts} +4 -1
- package/dist/{transport-Cs5tY2IO.d.ts → transport-Ckcwj-Do.d.ts} +4 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -551,6 +551,7 @@ var TerminalFailure = class extends Error {
|
|
|
551
551
|
this.name = "TerminalFailure";
|
|
552
552
|
}
|
|
553
553
|
};
|
|
554
|
+
var DEFAULT_PROGRESS_TIMEOUT_MS = 5e3;
|
|
554
555
|
function validateInput(zod, validator, jsonSchema, data) {
|
|
555
556
|
if (zod) {
|
|
556
557
|
const result = zod.safeParse(data);
|
|
@@ -573,7 +574,8 @@ async function runLease(opts) {
|
|
|
573
574
|
transport,
|
|
574
575
|
validator,
|
|
575
576
|
logger,
|
|
576
|
-
signal
|
|
577
|
+
signal,
|
|
578
|
+
progressTimeoutMs = DEFAULT_PROGRESS_TIMEOUT_MS
|
|
577
579
|
} = opts;
|
|
578
580
|
const inputCheck = validateInput(zodInput, validator, inputSchema, lease.task.input);
|
|
579
581
|
if (!inputCheck.ok) {
|
|
@@ -588,13 +590,13 @@ async function runLease(opts) {
|
|
|
588
590
|
return;
|
|
589
591
|
}
|
|
590
592
|
const parsedInput = inputCheck.value;
|
|
591
|
-
const
|
|
593
|
+
const reporter = makeProgressReporter(transport, lease.leaseId, logger, signal, progressTimeoutMs);
|
|
592
594
|
const ctx = {
|
|
593
595
|
task: lease.task,
|
|
594
596
|
leaseId: lease.leaseId,
|
|
595
597
|
signal,
|
|
596
598
|
log: scopedLogger(logger, lease),
|
|
597
|
-
progress:
|
|
599
|
+
progress: reporter.progress,
|
|
598
600
|
fail: (reason, opts2) => {
|
|
599
601
|
throw new TerminalFailure(reason, opts2?.code);
|
|
600
602
|
}
|
|
@@ -603,7 +605,9 @@ async function runLease(opts) {
|
|
|
603
605
|
try {
|
|
604
606
|
output = await handler(parsedInput, ctx);
|
|
605
607
|
} catch (err) {
|
|
606
|
-
|
|
608
|
+
const cancelled = signal.aborted || err.name === "AbortError";
|
|
609
|
+
if (!cancelled) await reporter.drained();
|
|
610
|
+
if (cancelled) {
|
|
607
611
|
await sendNack(
|
|
608
612
|
transport,
|
|
609
613
|
lease.leaseId,
|
|
@@ -637,6 +641,7 @@ async function runLease(opts) {
|
|
|
637
641
|
);
|
|
638
642
|
return;
|
|
639
643
|
}
|
|
644
|
+
await reporter.drained();
|
|
640
645
|
const outputCheck = validateInput(zodOutput, validator, outputSchema, output);
|
|
641
646
|
if (!outputCheck.ok) {
|
|
642
647
|
await sendNack(
|
|
@@ -678,20 +683,37 @@ async function sendNack(transport, leaseId, kind, reason, extras = {}, logger) {
|
|
|
678
683
|
);
|
|
679
684
|
});
|
|
680
685
|
}
|
|
681
|
-
function
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
686
|
+
function makeProgressReporter(transport, leaseId, logger, signal, timeoutMs) {
|
|
687
|
+
let tail = Promise.resolve();
|
|
688
|
+
return {
|
|
689
|
+
progress: (update) => {
|
|
690
|
+
const frame = {
|
|
691
|
+
type: "progress",
|
|
692
|
+
leaseId,
|
|
693
|
+
percent: update.percent,
|
|
694
|
+
message: update.message,
|
|
695
|
+
data: update.data,
|
|
696
|
+
sentAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
697
|
+
};
|
|
698
|
+
tail = tail.then(async () => {
|
|
699
|
+
if (signal.aborted) return;
|
|
700
|
+
try {
|
|
701
|
+
await withTimeout(transport.post(POST_PATH.progress, frame), timeoutMs);
|
|
702
|
+
} catch (err) {
|
|
703
|
+
logger.warn({ err, leaseId }, "progress POST failed; continuing");
|
|
704
|
+
}
|
|
705
|
+
});
|
|
706
|
+
},
|
|
707
|
+
drained: () => tail
|
|
693
708
|
};
|
|
694
709
|
}
|
|
710
|
+
function withTimeout(work, ms) {
|
|
711
|
+
let timer;
|
|
712
|
+
const timeout = new Promise((_resolve, reject) => {
|
|
713
|
+
timer = setTimeout(() => reject(new Error(`timed out after ${ms}ms`)), ms);
|
|
714
|
+
});
|
|
715
|
+
return Promise.race([work, timeout]).finally(() => clearTimeout(timer));
|
|
716
|
+
}
|
|
695
717
|
function scopedLogger(logger, lease) {
|
|
696
718
|
const scope = { leaseId: lease.leaseId, taskId: lease.task.taskId };
|
|
697
719
|
return {
|