@dnotifier-realtime/dnotifier 1.1.12 → 1.1.17

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.
Files changed (49) hide show
  1. package/dist/cjs/dnotifier.js +1 -1
  2. package/dist/cjs/providers/openai.js +1 -1
  3. package/dist/cjs/transport/httpTransport.js +1 -1
  4. package/dist/cjs/transport/websocketTransport.js +1 -1
  5. package/dist/cjs/utils/buffer.js +1 -1
  6. package/dist/cjs/utils/frame.js +1 -1
  7. package/dist/cjs/utils/id.js +1 -1
  8. package/dist/cjs/utils/payload.js +1 -1
  9. package/dist/config/endpoints.js +1 -1
  10. package/dist/dnotifier.cjs +399 -69
  11. package/dist/dnotifier.d.ts +5 -0
  12. package/dist/dnotifier.d.ts.map +1 -1
  13. package/dist/dnotifier.js +1 -1
  14. package/dist/providers/openai.js +1 -1
  15. package/dist/transport/httpTransport.d.ts +3 -1
  16. package/dist/transport/httpTransport.d.ts.map +1 -1
  17. package/dist/transport/httpTransport.js +1 -1
  18. package/dist/transport/websocketTransport.js +1 -1
  19. package/dist/utils/buffer.js +1 -1
  20. package/dist/utils/frame.js +1 -1
  21. package/dist/utils/id.js +1 -1
  22. package/dist/utils/logs.js +1 -1
  23. package/dist/utils/payload.js +1 -1
  24. package/dist/workflow/agent.js +1 -1
  25. package/dist/workflow/context.d.ts +49 -1
  26. package/dist/workflow/context.d.ts.map +1 -1
  27. package/dist/workflow/context.js +1 -1
  28. package/dist/workflow/errors.js +1 -1
  29. package/dist/workflow/graphParents.d.ts +13 -0
  30. package/dist/workflow/graphParents.d.ts.map +1 -0
  31. package/dist/workflow/graphParents.js +1 -0
  32. package/dist/workflow/ids.js +1 -1
  33. package/dist/workflow/index.d.ts +1 -0
  34. package/dist/workflow/index.js +1 -1
  35. package/dist/workflow/jsonPayload.js +1 -1
  36. package/dist/workflow/mockObservabilityClient.js +1 -1
  37. package/dist/workflow/observability.d.ts +9 -2
  38. package/dist/workflow/observability.d.ts.map +1 -1
  39. package/dist/workflow/observability.js +1 -1
  40. package/dist/workflow/runner.d.ts.map +1 -1
  41. package/dist/workflow/runner.js +1 -1
  42. package/dist/workflow/stepTypes.d.ts +4 -0
  43. package/dist/workflow/stepTypes.d.ts.map +1 -1
  44. package/dist/workflow/stepTypes.js +1 -1
  45. package/dist/workflow/workflow.js +1 -1
  46. package/dist/workflow/workflowObservabilityClient.d.ts +3 -2
  47. package/dist/workflow/workflowObservabilityClient.d.ts.map +1 -1
  48. package/dist/workflow/workflowObservabilityClient.js +1 -1
  49. package/package.json +1 -1
@@ -48,24 +48,40 @@ var HttpTransport = class {
48
48
  // this.appSecret = secret;
49
49
  // this.userId = userId;
50
50
  // }
51
- constructor({ url, headers = {} }) {
51
+ constructor({ url, headers = {}, timeoutMs = null }) {
52
52
  this.url = url;
53
53
  this.headers = headers;
54
+ this.timeoutMs = timeoutMs;
54
55
  }
55
56
  async send(payload) {
56
- const res = await fetch(`${this.url}`, {
57
- method: "POST",
58
- headers: {
59
- "Content-Type": "application/json",
60
- ...this.headers
61
- },
62
- body: JSON.stringify(payload)
63
- });
64
- if (!res.ok) {
65
- const text = await res.text();
66
- throw new Error(`DNotifier HTTP error: ${text}`);
57
+ const controller = new AbortController();
58
+ let timeoutId = null;
59
+ if (this.timeoutMs != null && this.timeoutMs > 0) {
60
+ timeoutId = setTimeout(() => controller.abort(), this.timeoutMs);
61
+ }
62
+ try {
63
+ const res = await fetch(`${this.url}`, {
64
+ method: "POST",
65
+ headers: {
66
+ "Content-Type": "application/json",
67
+ ...this.headers
68
+ },
69
+ body: JSON.stringify(payload),
70
+ signal: controller.signal
71
+ });
72
+ if (!res.ok) {
73
+ const text = await res.text();
74
+ throw new Error(`DNotifier HTTP error: ${text}`);
75
+ }
76
+ return await res.json();
77
+ } catch (err) {
78
+ if (err?.name === "AbortError") {
79
+ throw new Error(`DNotifier HTTP timeout after ${this.timeoutMs}ms: ${this.url}`);
80
+ }
81
+ throw err;
82
+ } finally {
83
+ if (timeoutId) clearTimeout(timeoutId);
67
84
  }
68
- return await res.json();
69
85
  }
70
86
  sendBinary() {
71
87
  throw new Error("Binary is not supported over HTTP yet");
@@ -510,9 +526,50 @@ var Workflow = class {
510
526
  }
511
527
  };
512
528
 
529
+ // src/workflow/graphParents.js
530
+ function formatParentStepId(value) {
531
+ if (value == null) return null;
532
+ if (Array.isArray(value)) {
533
+ const ids = value.filter((id) => id != null && String(id).trim() !== "");
534
+ return ids.length > 0 ? ids.join(",") : null;
535
+ }
536
+ const text = String(value).trim();
537
+ return text === "" ? null : text;
538
+ }
539
+ function resolveGraphParentStepId(step) {
540
+ const multi = step.parentStepIds ?? step.dependsOn;
541
+ if (multi?.length) {
542
+ return formatParentStepId(multi);
543
+ }
544
+ return formatParentStepId(step.parentStepId);
545
+ }
546
+ function resolveRecordedParentStepId(options = {}) {
547
+ const multi = options.parentStepIds ?? options.dependsOn;
548
+ if (multi?.length) {
549
+ return formatParentStepId(multi);
550
+ }
551
+ if (options.parentStepId !== void 0) {
552
+ return formatParentStepId(options.parentStepId);
553
+ }
554
+ if (options.resolveParent) {
555
+ return formatParentStepId(options.resolveParent());
556
+ }
557
+ return null;
558
+ }
559
+
513
560
  // src/workflow/stepTypes.js
514
561
  var STEP_TYPE_AGENT = "Agent";
562
+ var STEP_TYPE_PARALLEL = "Parallel";
563
+ var STEP_TYPE_ENTRY = "Entry";
564
+ var STEP_TYPE_END = "End";
515
565
  var STEP_TYPE_CUSTOM = "Custom";
566
+ var STRUCTURAL_STEP_TYPES = [
567
+ STEP_TYPE_AGENT,
568
+ STEP_TYPE_PARALLEL,
569
+ STEP_TYPE_ENTRY,
570
+ STEP_TYPE_END,
571
+ STEP_TYPE_CUSTOM
572
+ ];
516
573
  var SDK_API_STEP_TYPES = [
517
574
  "sendAI",
518
575
  "fetchAIHistory",
@@ -564,6 +621,7 @@ function resolveApiName(step) {
564
621
  function resolveStepType(step) {
565
622
  if (step?.type != null && String(step.type).trim() !== "") {
566
623
  const t = String(step.type).trim();
624
+ if (STRUCTURAL_STEP_TYPES.includes(t)) return t;
567
625
  if (Object.values(LOG_TYPE_BY_API).includes(t)) return t;
568
626
  return toLogStepType(t);
569
627
  }
@@ -593,9 +651,35 @@ function snapshot(value) {
593
651
  }
594
652
  function apiInput(opts) {
595
653
  if (!opts || typeof opts !== "object") return opts ?? null;
596
- const { label, ...rest } = opts;
654
+ const { label, dependsOn, parentStepIds, branchKey, siblingIndex, parentStepId, input, ...rest } = opts;
597
655
  return Object.keys(rest).length > 0 ? rest : null;
598
656
  }
657
+ function normalizeRunAgentsSpec(spec) {
658
+ if (Array.isArray(spec)) {
659
+ return spec.map((item, index) => {
660
+ if (typeof item === "string") {
661
+ return { name: item, input: void 0, key: String(index) };
662
+ }
663
+ if (!item || typeof item !== "object" || !item.name) {
664
+ throw new WorkflowError("runAgents entry requires name");
665
+ }
666
+ return { name: item.name, input: item.input, key: String(index) };
667
+ });
668
+ }
669
+ if (spec && typeof spec === "object") {
670
+ return Object.entries(spec).map(([key, value]) => {
671
+ if (value && typeof value === "object" && "name" in value) {
672
+ return {
673
+ name: value.name,
674
+ input: value.input,
675
+ key
676
+ };
677
+ }
678
+ return { name: key, input: value, key };
679
+ });
680
+ }
681
+ throw new WorkflowError("runAgents expects an array or object spec");
682
+ }
599
683
  var WorkflowContext = class _WorkflowContext {
600
684
  /**
601
685
  * @param {{
@@ -607,6 +691,8 @@ var WorkflowContext = class _WorkflowContext {
607
691
  * observability?: import("./observability.js").WorkflowObservability | null,
608
692
  * executionId?: string | null,
609
693
  * agentName?: string | null,
694
+ * entryStepId?: string | null,
695
+ * traceParentStepId?: string | null,
610
696
  * }} options
611
697
  */
612
698
  constructor({
@@ -617,7 +703,9 @@ var WorkflowContext = class _WorkflowContext {
617
703
  state = {},
618
704
  observability = null,
619
705
  executionId = null,
620
- agentName = null
706
+ agentName = null,
707
+ entryStepId = null,
708
+ traceParentStepId = null
621
709
  }) {
622
710
  this.notifier = notifier;
623
711
  this.senderId = senderId;
@@ -628,6 +716,53 @@ var WorkflowContext = class _WorkflowContext {
628
716
  this._executionId = executionId;
629
717
  this._observabilityEnabled = Boolean(workflow.observability && observability && executionId);
630
718
  this.agentName = agentName;
719
+ this._entryStepId = entryStepId;
720
+ this._chainHeadStepId = entryStepId;
721
+ this._traceParentStepId = traceParentStepId;
722
+ this._currentStepId = null;
723
+ this.rootStepId = null;
724
+ this._terminalStepIds = [];
725
+ }
726
+ /**
727
+ * Comma-separated parent step ids for the workflow End node.
728
+ * @returns {string | null}
729
+ */
730
+ getEndParentStepId() {
731
+ if (this._terminalStepIds.length > 0) {
732
+ return formatParentStepId(this._terminalStepIds);
733
+ }
734
+ if (this._entryStepId) return this._entryStepId;
735
+ return null;
736
+ }
737
+ /**
738
+ * @param {string[]} stepIds
739
+ */
740
+ _markTerminalSteps(stepIds) {
741
+ if (this._currentStepId) return;
742
+ this._terminalStepIds = stepIds.filter(
743
+ (id) => id != null && String(id).trim() !== ""
744
+ );
745
+ }
746
+ /**
747
+ * @returns {string | null}
748
+ */
749
+ _resolveParentStepId(options = {}) {
750
+ if (options.parentStepId !== void 0) return options.parentStepId;
751
+ if (this._currentStepId) return this._currentStepId;
752
+ if (this._traceParentStepId) return this._traceParentStepId;
753
+ if (this._chainHeadStepId) return this._chainHeadStepId;
754
+ if (this._entryStepId) return this._entryStepId;
755
+ return null;
756
+ }
757
+ /**
758
+ * @returns {{ executionId: string | null, workflowId: string, parentStepId: string | null }}
759
+ */
760
+ exportTraceContext() {
761
+ return {
762
+ executionId: this._executionId,
763
+ workflowId: this.workflow.name,
764
+ parentStepId: this._currentStepId ?? this._chainHeadStepId ?? this._traceParentStepId ?? this._entryStepId ?? null
765
+ };
631
766
  }
632
767
  /**
633
768
  * @param {string} apiName
@@ -651,12 +786,6 @@ var WorkflowContext = class _WorkflowContext {
651
786
  if (this._observabilityEnabled) {
652
787
  const usage = meta.usesTokens ? usageFromResponse(output) : { tokensIn: 0, tokensOut: 0, model: null };
653
788
  const stepKind = this.agentName ?? apiName;
654
- console.log("[WorkflowContext] recordStep kind:", {
655
- kind: stepKind,
656
- agentName: this.agentName,
657
- apiName,
658
- type: logType
659
- });
660
789
  await this._observability.recordStep(this._executionId, {
661
790
  apiName,
662
791
  agentName: this.agentName,
@@ -665,9 +794,11 @@ var WorkflowContext = class _WorkflowContext {
665
794
  label: meta.label ?? logType,
666
795
  status,
667
796
  latencyMs: Date.now() - started,
797
+ parentStepId: formatParentStepId(this._currentStepId),
668
798
  ...usage,
669
799
  input: snapshot(meta.input),
670
800
  output: snapshot(output),
801
+ state: snapshot(this.state),
671
802
  error
672
803
  });
673
804
  }
@@ -675,25 +806,130 @@ var WorkflowContext = class _WorkflowContext {
675
806
  }
676
807
  /**
677
808
  * @param {string} name
678
- * @param {{ input?: * }} [options]
809
+ * @param {{
810
+ * input?: *,
811
+ * dependsOn?: string[],
812
+ * parentStepIds?: string[],
813
+ * branchKey?: string,
814
+ * siblingIndex?: number,
815
+ * parentStepId?: string | null,
816
+ * }} [options]
817
+ * @returns {Promise<{ output: *, stepId: string | null }>}
679
818
  */
680
- async runAgent(name, options = {}) {
819
+ async _runAgentObserved(name, options = {}) {
681
820
  const agent = this.workflow.getAgent(name);
682
821
  if (!agent) {
683
822
  throw new WorkflowError(`Agent not found: ${name}`);
684
823
  }
685
824
  const childInput = options.input !== void 0 ? options.input : this.input;
686
- const childCtx = new _WorkflowContext({
687
- notifier: this.notifier,
688
- senderId: this.senderId,
689
- workflow: this.workflow,
690
- input: childInput,
691
- state: this.state,
692
- observability: this._observability,
693
- executionId: this._executionId,
694
- agentName: name
825
+ const parentStepId = resolveRecordedParentStepId({
826
+ parentStepId: options.parentStepId,
827
+ parentStepIds: options.parentStepIds,
828
+ dependsOn: options.dependsOn,
829
+ resolveParent: () => this._resolveParentStepId(options)
830
+ });
831
+ const agentStepId = this._observabilityEnabled ? this._observability.allocateStepId() : null;
832
+ const started = Date.now();
833
+ let status = "ok";
834
+ let output = null;
835
+ let error = null;
836
+ try {
837
+ const childCtx = new _WorkflowContext({
838
+ notifier: this.notifier,
839
+ senderId: this.senderId,
840
+ workflow: this.workflow,
841
+ input: childInput,
842
+ state: this.state,
843
+ observability: this._observability,
844
+ executionId: this._executionId,
845
+ agentName: name,
846
+ entryStepId: this._entryStepId,
847
+ traceParentStepId: this._traceParentStepId
848
+ });
849
+ childCtx._currentStepId = agentStepId;
850
+ output = await agent.run(childCtx);
851
+ if (agentStepId) {
852
+ childCtx.rootStepId = agentStepId;
853
+ }
854
+ return { output, stepId: agentStepId };
855
+ } catch (err) {
856
+ status = "error";
857
+ error = { message: err?.message ?? String(err), stack: err?.stack };
858
+ throw err;
859
+ } finally {
860
+ if (this._observabilityEnabled && agentStepId) {
861
+ await this._observability.recordStep(this._executionId, {
862
+ id: agentStepId,
863
+ parentStepId,
864
+ branchKey: options.branchKey ?? null,
865
+ siblingIndex: options.siblingIndex ?? null,
866
+ agentName: name,
867
+ kind: name,
868
+ type: STEP_TYPE_AGENT,
869
+ label: name,
870
+ status,
871
+ latencyMs: Date.now() - started,
872
+ input: snapshot(childInput),
873
+ output: snapshot(output),
874
+ state: snapshot(this.state),
875
+ error
876
+ });
877
+ this.rootStepId = agentStepId;
878
+ if (status === "ok" && !this._currentStepId) {
879
+ this._chainHeadStepId = agentStepId;
880
+ }
881
+ }
882
+ }
883
+ }
884
+ /**
885
+ * @param {string} name
886
+ * @param {{
887
+ * input?: *,
888
+ * dependsOn?: string[],
889
+ * parentStepIds?: string[],
890
+ * branchKey?: string,
891
+ * siblingIndex?: number,
892
+ * parentStepId?: string | null,
893
+ * }} [options]
894
+ */
895
+ async runAgent(name, options = {}) {
896
+ const { output, stepId } = await this._runAgentObserved(name, options);
897
+ if (stepId && !this._currentStepId) {
898
+ this._markTerminalSteps([stepId]);
899
+ }
900
+ return output;
901
+ }
902
+ /**
903
+ * @param {Array<string | { name: string, input?: * }> | Record<string, *>} spec
904
+ * @param {{ label?: string }} [options]
905
+ * @returns {Promise<{ results: *[] | Record<string, *>, stepIds: (string | null)[] }>}
906
+ */
907
+ async runAgents(spec, options = {}) {
908
+ const entries = normalizeRunAgentsSpec(spec);
909
+ const parentStepId = resolveRecordedParentStepId({
910
+ parentStepId: options.parentStepId,
911
+ resolveParent: () => this._resolveParentStepId(options)
912
+ });
913
+ const outcomes = await Promise.all(
914
+ entries.map(
915
+ (entry, index) => this._runAgentObserved(entry.name, {
916
+ input: entry.input,
917
+ parentStepId,
918
+ siblingIndex: index
919
+ })
920
+ )
921
+ );
922
+ const results = outcomes.map((outcome) => outcome.output);
923
+ const stepIds = outcomes.map((outcome) => outcome.stepId);
924
+ this._markTerminalSteps(stepIds);
925
+ if (Array.isArray(spec)) {
926
+ return { results, stepIds };
927
+ }
928
+ const keyed = {};
929
+ entries.forEach((entry, index) => {
930
+ keyed[entry.key] = results[index];
695
931
  });
696
- return agent.run(childCtx);
932
+ return { results: keyed, stepIds };
697
933
  }
698
934
  /** @param {object} opts */
699
935
  async sendAI(opts) {
@@ -802,6 +1038,12 @@ var WorkflowContext = class _WorkflowContext {
802
1038
  * error?: object | null,
803
1039
  * type?: string,
804
1040
  * latencyMs?: number,
1041
+ * parentStepId?: string | null,
1042
+ * dependsOn?: string[],
1043
+ * parentStepIds?: string[],
1044
+ * branchKey?: string,
1045
+ * siblingIndex?: number,
1046
+ * state?: *,
805
1047
  * }} step
806
1048
  */
807
1049
  async recordStep(step) {
@@ -820,8 +1062,10 @@ var WorkflowContext = class _WorkflowContext {
820
1062
  latencyMs: step.latencyMs ?? 0,
821
1063
  status: step.status ?? "ok",
822
1064
  error: step.error ?? null,
1065
+ parentStepId: step.parentStepId != null ? formatParentStepId(step.parentStepId) : formatParentStepId(this._currentStepId),
823
1066
  input: snapshot(step.input),
824
- output: snapshot(step.output)
1067
+ output: snapshot(step.output),
1068
+ state: snapshot(step.state !== void 0 ? step.state : this.state)
825
1069
  });
826
1070
  }
827
1071
  };
@@ -889,30 +1133,29 @@ var WorkflowObservabilityClient = class {
889
1133
  /**
890
1134
  * @param {{ appId?: string, token?: string | null }} [options]
891
1135
  */
892
- constructor({ appId = null, token = null } = {}) {
1136
+ constructor({ appId = null, token = null, requestTimeoutMs = 5e3 } = {}) {
893
1137
  this.appId = appId;
894
1138
  this.token = token;
1139
+ this.requestTimeoutMs = requestTimeoutMs;
895
1140
  }
896
- configure({ appId, token } = {}) {
1141
+ configure({ appId, token, requestTimeoutMs } = {}) {
897
1142
  if (appId !== void 0) this.appId = appId;
898
1143
  if (token !== void 0) this.token = token;
1144
+ if (requestTimeoutMs !== void 0) this.requestTimeoutMs = requestTimeoutMs;
899
1145
  }
900
1146
  _transport(url) {
901
1147
  const headers = this.token ? { authorization: `${this.token}` } : {};
902
- return new HttpTransport({ url, headers });
1148
+ return new HttpTransport({
1149
+ url,
1150
+ headers,
1151
+ timeoutMs: this.requestTimeoutMs
1152
+ });
903
1153
  }
904
1154
  async _post(url, body) {
905
- if (!this.token) {
906
- console.warn("[WorkflowObservability] skip API call: not connected (no auth token)");
907
- return;
908
- }
1155
+ if (!this.token) return;
909
1156
  try {
910
1157
  await this._transport(url).send(body);
911
- } catch (err) {
912
- console.log(
913
- `[WorkflowObservability] API failed (${url}):`,
914
- err?.message ?? err
915
- );
1158
+ } catch {
916
1159
  }
917
1160
  }
918
1161
  /**
@@ -994,21 +1237,22 @@ var WorkflowObservabilityClient = class {
994
1237
  if (step.agentName != null && step.agentName !== "") {
995
1238
  body.agentName = step.agentName;
996
1239
  }
997
- console.log("[WorkflowObservability] step kind:", {
998
- kind: body.kind,
999
- type: body.type,
1000
- agentName: body.agentName,
1001
- apiName: body.apiName,
1002
- stepKind: step.kind,
1003
- stepAgentName: step.agentName,
1004
- stepApiName: step.apiName
1005
- });
1006
1240
  if (step.model != null && step.model !== "") {
1007
1241
  body.model = typeof step.model === "string" ? step.model : String(step.model);
1008
1242
  }
1009
1243
  setJsonColumn(body, "input", step.input);
1010
1244
  setJsonColumn(body, "output", step.output);
1245
+ setJsonColumn(body, "state", step.state);
1011
1246
  setJsonColumn(body, "error", step.error);
1247
+ if (step.parentStepId != null && step.parentStepId !== "") {
1248
+ body.parentStepId = step.parentStepId;
1249
+ }
1250
+ if (step.siblingIndex != null && step.siblingIndex !== "") {
1251
+ body.siblingIndex = step.siblingIndex;
1252
+ }
1253
+ if (step.branchKey != null && step.branchKey !== "") {
1254
+ body.branchKey = step.branchKey;
1255
+ }
1012
1256
  await this._post(
1013
1257
  DNotifierEndpoints.workflowExecutionStepsUrl(executionId),
1014
1258
  body
@@ -1020,15 +1264,27 @@ var WorkflowObservabilityClient = class {
1020
1264
  // src/workflow/observability.js
1021
1265
  var WorkflowObservability = class {
1022
1266
  /**
1023
- * @param {{ appId: string, token?: string | null, client?: WorkflowObservabilityClient }} options
1267
+ * @param {{
1268
+ * appId: string,
1269
+ * token?: string | null,
1270
+ * client?: WorkflowObservabilityClient,
1271
+ * appendOnly?: boolean,
1272
+ * }} options
1024
1273
  */
1025
- constructor({ appId, token = null, client }) {
1274
+ constructor({ appId, token = null, client, appendOnly = false }) {
1026
1275
  this.appId = appId;
1027
1276
  this.token = token;
1028
1277
  this.client = client ?? new WorkflowObservabilityClient({ appId, token });
1029
1278
  this.client.configure({ appId, token });
1030
1279
  this._registeredWorkflowNames = /* @__PURE__ */ new Set();
1031
1280
  this._stepCounter = 0;
1281
+ this._appendOnly = Boolean(appendOnly);
1282
+ }
1283
+ /**
1284
+ * @returns {string}
1285
+ */
1286
+ allocateStepId() {
1287
+ return shortId("step");
1032
1288
  }
1033
1289
  /**
1034
1290
  * @param {import("./workflow.js").Workflow} workflow
@@ -1043,14 +1299,16 @@ var WorkflowObservability = class {
1043
1299
  name: workflow.name,
1044
1300
  description: workflow.description ?? null
1045
1301
  });
1046
- } catch (err) {
1047
- console.warn("[WorkflowObservability] registerWorkflow failed:", err?.message ?? err);
1302
+ } catch {
1048
1303
  }
1049
1304
  }
1050
1305
  /**
1051
1306
  * @param {object} params
1052
1307
  */
1053
1308
  async startExecution(params) {
1309
+ if (this._appendOnly) {
1310
+ throw new Error("startExecution is not allowed in append-only workflow observability mode");
1311
+ }
1054
1312
  const executionId = shortId("exec");
1055
1313
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
1056
1314
  try {
@@ -1062,8 +1320,7 @@ var WorkflowObservability = class {
1062
1320
  input: params.input ?? null,
1063
1321
  startedAt
1064
1322
  });
1065
- } catch (err) {
1066
- console.warn("[WorkflowObservability] startExecution failed:", err?.message ?? err);
1323
+ } catch {
1067
1324
  }
1068
1325
  this._stepCounter = 0;
1069
1326
  return { executionId, startedAt };
@@ -1073,11 +1330,12 @@ var WorkflowObservability = class {
1073
1330
  * @param {object} step
1074
1331
  */
1075
1332
  async recordStep(executionId, step) {
1076
- if (!executionId) return;
1333
+ if (!executionId) return null;
1077
1334
  this._stepCounter += 1;
1078
- const id = step.id ?? `n${this._stepCounter}`;
1335
+ const id = step.id ?? this.allocateStepId();
1079
1336
  const type = resolveStepType(step);
1080
1337
  const kind = step.agentName ?? step.kind ?? step.apiName ?? null;
1338
+ const parentStepId = resolveGraphParentStepId(step);
1081
1339
  const record = {
1082
1340
  ...step,
1083
1341
  id,
@@ -1087,12 +1345,16 @@ var WorkflowObservability = class {
1087
1345
  agentName: step.agentName ?? null,
1088
1346
  kind,
1089
1347
  type,
1090
- step_type: type
1348
+ step_type: type,
1349
+ parentStepId,
1350
+ siblingIndex: step.siblingIndex ?? null,
1351
+ branchKey: step.branchKey ?? null
1091
1352
  };
1353
+ delete record.dependsOn;
1354
+ delete record.parentStepIds;
1092
1355
  try {
1093
1356
  await this.client.addStep(executionId, record);
1094
- } catch (err) {
1095
- console.warn("[WorkflowObservability] recordStep failed:", err?.message ?? err);
1357
+ } catch {
1096
1358
  }
1097
1359
  return record;
1098
1360
  }
@@ -1100,6 +1362,7 @@ var WorkflowObservability = class {
1100
1362
  * @param {object} params
1101
1363
  */
1102
1364
  async completeExecution(params) {
1365
+ if (this._appendOnly) return;
1103
1366
  if (!params.executionId) return;
1104
1367
  try {
1105
1368
  await this.client.updateExecution(params.executionId, {
@@ -1110,8 +1373,7 @@ var WorkflowObservability = class {
1110
1373
  error: params.error ?? null,
1111
1374
  result: params.result ?? null
1112
1375
  });
1113
- } catch (err) {
1114
- console.warn("[WorkflowObservability] completeExecution failed:", err?.message ?? err);
1376
+ } catch {
1115
1377
  }
1116
1378
  }
1117
1379
  getStepCount() {
@@ -1123,6 +1385,14 @@ function createWorkflowObservability(params) {
1123
1385
  }
1124
1386
 
1125
1387
  // src/workflow/runner.js
1388
+ function snapshot2(value) {
1389
+ try {
1390
+ if (value === void 0) return null;
1391
+ return JSON.parse(JSON.stringify(value));
1392
+ } catch {
1393
+ return String(value);
1394
+ }
1395
+ }
1126
1396
  var WorkflowRunner = class {
1127
1397
  /**
1128
1398
  * @param {{
@@ -1163,6 +1433,20 @@ var WorkflowRunner = class {
1163
1433
  executionId = started.executionId;
1164
1434
  startedAt = started.startedAt;
1165
1435
  }
1436
+ let entryStepId = null;
1437
+ if (observability && executionId) {
1438
+ const entryRecord = await observability.recordStep(executionId, {
1439
+ type: STEP_TYPE_ENTRY,
1440
+ kind: "entry",
1441
+ label: this.workflow.name,
1442
+ status: "ok",
1443
+ latencyMs: 0,
1444
+ parentStepId: null,
1445
+ input: snapshot2(input),
1446
+ state: snapshot2(state)
1447
+ });
1448
+ entryStepId = entryRecord?.id ?? null;
1449
+ }
1166
1450
  const ctx = new WorkflowContext({
1167
1451
  notifier: this.notifier,
1168
1452
  senderId: this.senderId,
@@ -1170,7 +1454,8 @@ var WorkflowRunner = class {
1170
1454
  input,
1171
1455
  state,
1172
1456
  observability,
1173
- executionId
1457
+ executionId,
1458
+ entryStepId
1174
1459
  });
1175
1460
  let result;
1176
1461
  let status = "ok";
@@ -1186,6 +1471,17 @@ var WorkflowRunner = class {
1186
1471
  throw err;
1187
1472
  } finally {
1188
1473
  if (observability && executionId) {
1474
+ await observability.recordStep(executionId, {
1475
+ type: STEP_TYPE_END,
1476
+ kind: "end",
1477
+ label: this.workflow.name,
1478
+ status,
1479
+ latencyMs: Date.now() - runStarted,
1480
+ parentStepId: ctx.getEndParentStepId(),
1481
+ output: snapshot2(status === "ok" ? result : null),
1482
+ state: snapshot2(ctx.state),
1483
+ error: errorPayload
1484
+ });
1189
1485
  const endedAt = (/* @__PURE__ */ new Date()).toISOString();
1190
1486
  const stepCount = observability.getStepCount();
1191
1487
  await observability.completeExecution({
@@ -1913,6 +2209,40 @@ var DNotifier = class {
1913
2209
  });
1914
2210
  return runner.run({ input });
1915
2211
  }
2212
+ /**
2213
+ * Resume a workflow context on a remote server (append-only observability).
2214
+ * @param {{
2215
+ * executionId: string,
2216
+ * workflowId: string,
2217
+ * parentStepId?: string | null,
2218
+ * }} traceCtx
2219
+ */
2220
+ async resumeWorkflowContext(traceCtx) {
2221
+ if (!traceCtx?.executionId || !traceCtx?.workflowId) {
2222
+ throw new Error("traceCtx requires executionId and workflowId");
2223
+ }
2224
+ const observability = createWorkflowObservability({
2225
+ appId: this.appId,
2226
+ token: this.authToken ?? null,
2227
+ appendOnly: true
2228
+ });
2229
+ const workflow = new Workflow({
2230
+ name: traceCtx.workflowId,
2231
+ observability: true,
2232
+ entry: async () => {
2233
+ }
2234
+ });
2235
+ await observability.registerWorkflow(workflow);
2236
+ return new WorkflowContext({
2237
+ notifier: this,
2238
+ senderId: this.userId,
2239
+ workflow,
2240
+ input: null,
2241
+ observability,
2242
+ executionId: traceCtx.executionId,
2243
+ traceParentStepId: traceCtx.parentStepId ?? null
2244
+ });
2245
+ }
1916
2246
  };
1917
2247
  // Annotate the CommonJS export names for ESM import in node:
1918
2248
  0 && (module.exports = {