@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 CHANGED
@@ -1,5 +1,5 @@
1
- import { J as JsonSchema, C as Capability, a as CapabilityHandleOptions, S as ServerToNeuronFrame, N as NeuronLogger, T as Transport, A as AuthCallback, H as Handler, b as CapabilityRegistration, c as TransportCallbacks, d as ClientFrame, P as PostPath } from './transport-Cs5tY2IO.mjs';
2
- export { e as HandlerContext, f as HeartbeatInFlightEntry, L as LeasePayload, g as TaskPayload } from './transport-Cs5tY2IO.mjs';
1
+ import { J as JsonSchema, C as Capability, a as CapabilityHandleOptions, S as ServerToNeuronFrame, N as NeuronLogger, T as Transport, A as AuthCallback, H as Handler, b as CapabilityRegistration, c as TransportCallbacks, d as ClientFrame, P as PostPath } from './transport-Ckcwj-Do.mjs';
2
+ export { e as HandlerContext, f as HeartbeatInFlightEntry, L as LeasePayload, g as TaskPayload } from './transport-Ckcwj-Do.mjs';
3
3
  import { EventEmitter } from 'eventemitter3';
4
4
  import { z } from 'zod';
5
5
  import { ErrorObject } from 'ajv';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { J as JsonSchema, C as Capability, a as CapabilityHandleOptions, S as ServerToNeuronFrame, N as NeuronLogger, T as Transport, A as AuthCallback, H as Handler, b as CapabilityRegistration, c as TransportCallbacks, d as ClientFrame, P as PostPath } from './transport-Cs5tY2IO.js';
2
- export { e as HandlerContext, f as HeartbeatInFlightEntry, L as LeasePayload, g as TaskPayload } from './transport-Cs5tY2IO.js';
1
+ import { J as JsonSchema, C as Capability, a as CapabilityHandleOptions, S as ServerToNeuronFrame, N as NeuronLogger, T as Transport, A as AuthCallback, H as Handler, b as CapabilityRegistration, c as TransportCallbacks, d as ClientFrame, P as PostPath } from './transport-Ckcwj-Do.js';
2
+ export { e as HandlerContext, f as HeartbeatInFlightEntry, L as LeasePayload, g as TaskPayload } from './transport-Ckcwj-Do.js';
3
3
  import { EventEmitter } from 'eventemitter3';
4
4
  import { z } from 'zod';
5
5
  import { ErrorObject } from 'ajv';
package/dist/index.js CHANGED
@@ -558,6 +558,7 @@ var TerminalFailure = class extends Error {
558
558
  this.name = "TerminalFailure";
559
559
  }
560
560
  };
561
+ var DEFAULT_PROGRESS_TIMEOUT_MS = 5e3;
561
562
  function validateInput(zod, validator, jsonSchema, data) {
562
563
  if (zod) {
563
564
  const result = zod.safeParse(data);
@@ -580,7 +581,8 @@ async function runLease(opts) {
580
581
  transport,
581
582
  validator,
582
583
  logger,
583
- signal
584
+ signal,
585
+ progressTimeoutMs = DEFAULT_PROGRESS_TIMEOUT_MS
584
586
  } = opts;
585
587
  const inputCheck = validateInput(zodInput, validator, inputSchema, lease.task.input);
586
588
  if (!inputCheck.ok) {
@@ -595,13 +597,13 @@ async function runLease(opts) {
595
597
  return;
596
598
  }
597
599
  const parsedInput = inputCheck.value;
598
- const progressFn = makeProgressFn(transport, lease.leaseId, logger);
600
+ const reporter = makeProgressReporter(transport, lease.leaseId, logger, signal, progressTimeoutMs);
599
601
  const ctx = {
600
602
  task: lease.task,
601
603
  leaseId: lease.leaseId,
602
604
  signal,
603
605
  log: scopedLogger(logger, lease),
604
- progress: progressFn,
606
+ progress: reporter.progress,
605
607
  fail: (reason, opts2) => {
606
608
  throw new TerminalFailure(reason, opts2?.code);
607
609
  }
@@ -610,7 +612,9 @@ async function runLease(opts) {
610
612
  try {
611
613
  output = await handler(parsedInput, ctx);
612
614
  } catch (err) {
613
- if (signal.aborted || err.name === "AbortError") {
615
+ const cancelled = signal.aborted || err.name === "AbortError";
616
+ if (!cancelled) await reporter.drained();
617
+ if (cancelled) {
614
618
  await sendNack(
615
619
  transport,
616
620
  lease.leaseId,
@@ -644,6 +648,7 @@ async function runLease(opts) {
644
648
  );
645
649
  return;
646
650
  }
651
+ await reporter.drained();
647
652
  const outputCheck = validateInput(zodOutput, validator, outputSchema, output);
648
653
  if (!outputCheck.ok) {
649
654
  await sendNack(
@@ -685,20 +690,37 @@ async function sendNack(transport, leaseId, kind, reason, extras = {}, logger) {
685
690
  );
686
691
  });
687
692
  }
688
- function makeProgressFn(transport, leaseId, logger) {
689
- return (update) => {
690
- void transport.post(POST_PATH.progress, {
691
- type: "progress",
692
- leaseId,
693
- percent: update.percent,
694
- message: update.message,
695
- data: update.data,
696
- sentAt: (/* @__PURE__ */ new Date()).toISOString()
697
- }).catch((err) => {
698
- logger.warn({ err, leaseId }, "progress POST failed; continuing");
699
- });
693
+ function makeProgressReporter(transport, leaseId, logger, signal, timeoutMs) {
694
+ let tail = Promise.resolve();
695
+ return {
696
+ progress: (update) => {
697
+ const frame = {
698
+ type: "progress",
699
+ leaseId,
700
+ percent: update.percent,
701
+ message: update.message,
702
+ data: update.data,
703
+ sentAt: (/* @__PURE__ */ new Date()).toISOString()
704
+ };
705
+ tail = tail.then(async () => {
706
+ if (signal.aborted) return;
707
+ try {
708
+ await withTimeout(transport.post(POST_PATH.progress, frame), timeoutMs);
709
+ } catch (err) {
710
+ logger.warn({ err, leaseId }, "progress POST failed; continuing");
711
+ }
712
+ });
713
+ },
714
+ drained: () => tail
700
715
  };
701
716
  }
717
+ function withTimeout(work, ms) {
718
+ let timer;
719
+ const timeout = new Promise((_resolve, reject) => {
720
+ timer = setTimeout(() => reject(new Error(`timed out after ${ms}ms`)), ms);
721
+ });
722
+ return Promise.race([work, timeout]).finally(() => clearTimeout(timer));
723
+ }
702
724
  function scopedLogger(logger, lease) {
703
725
  const scope = { leaseId: lease.leaseId, taskId: lease.task.taskId };
704
726
  return {