@cydm/pie 2.7.4 → 2.7.5

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.
@@ -4,7 +4,7 @@ import {
4
4
  disposeModelTransports,
5
5
  streamSimple,
6
6
  validateToolArguments
7
- } from "./chunk-Z4EIBY2E.js";
7
+ } from "./chunk-SUPAWIKP.js";
8
8
 
9
9
  // ../../packages/agent-core/src/agent-loop.ts
10
10
  function createLoopErrorMessage(error, config, signal) {
@@ -290,6 +290,7 @@ async function executeToolCalls(tools, assistantMessage, signal, stream, getStee
290
290
  } catch (e) {
291
291
  console.warn(`[AgentLoop] beforeToolCall hook failed for ${toolCall.name}:`, e);
292
292
  }
293
+ if (signal?.aborted) break;
293
294
  let result;
294
295
  let isError = false;
295
296
  let executionArgs = toolCall.arguments;
@@ -637,6 +638,8 @@ var Agent = class {
637
638
  return this.runningPrompt ?? Promise.resolve();
638
639
  }
639
640
  reset() {
641
+ this.abortController?.abort();
642
+ this.abortController = void 0;
640
643
  this.releaseTransport();
641
644
  this._state.messages = [];
642
645
  this._state.isStreaming = false;
@@ -660,6 +663,7 @@ var Agent = class {
660
663
  * Useful when resuming a session that was saved mid-processing.
661
664
  */
662
665
  resetProcessingState() {
666
+ this.abortController?.abort();
663
667
  this._state.isStreaming = false;
664
668
  this._state.streamMessage = null;
665
669
  this._state.pendingToolCalls = /* @__PURE__ */ new Set();
@@ -766,8 +770,10 @@ var Agent = class {
766
770
  async _runLoop(messages, options) {
767
771
  const model = this._state.model;
768
772
  if (!model) throw new Error("No model configured");
769
- this.abortController = new AbortController();
770
- bindToolCallerIdentity(this.abortController.signal, this.toolCallerIdentity);
773
+ const runController = new AbortController();
774
+ this.abortController = runController;
775
+ const ownsRun = () => this.abortController === runController;
776
+ bindToolCallerIdentity(runController.signal, this.toolCallerIdentity);
771
777
  this._state.isStreaming = true;
772
778
  this._state.streamMessage = null;
773
779
  this._state.error = void 0;
@@ -791,7 +797,7 @@ var Agent = class {
791
797
  getApiKey: this.getApiKey,
792
798
  hooks: this.hooks,
793
799
  getSteeringMessages: async () => {
794
- if (options?.suppressSteeringPolls) {
800
+ if (!ownsRun() || options?.suppressSteeringPolls) {
795
801
  return [];
796
802
  }
797
803
  if (skipInitialSteeringPoll) {
@@ -801,7 +807,7 @@ var Agent = class {
801
807
  return this.dequeueSteeringMessages();
802
808
  },
803
809
  getFollowUpMessages: async () => {
804
- if (options?.suppressFollowUpPolls) {
810
+ if (!ownsRun() || options?.suppressFollowUpPolls) {
805
811
  return [];
806
812
  }
807
813
  return this.dequeueFollowUpMessages();
@@ -809,8 +815,9 @@ var Agent = class {
809
815
  };
810
816
  let partial = null;
811
817
  try {
812
- const stream = messages ? agentLoop(messages, context, config, this.abortController.signal, this.streamFn) : agentLoopContinue(context, config, this.abortController.signal, this.streamFn);
818
+ const stream = messages ? agentLoop(messages, context, config, runController.signal, this.streamFn) : agentLoopContinue(context, config, runController.signal, this.streamFn);
813
819
  for await (const event of stream) {
820
+ if (!ownsRun()) return;
814
821
  switch (event.type) {
815
822
  case "message_start":
816
823
  partial = event.message;
@@ -847,9 +854,9 @@ var Agent = class {
847
854
  this._state.streamMessage = null;
848
855
  break;
849
856
  }
850
- this.emit(event);
857
+ this.emit(event, ownsRun);
851
858
  }
852
- if (partial && partial.role === "assistant" && partial.content?.length > 0) {
859
+ if (ownsRun() && partial && partial.role === "assistant" && partial.content?.length > 0) {
853
860
  const onlyEmpty = !partial.content.some(
854
861
  (c) => c.type === "thinking" && c.thinking?.trim().length > 0 || c.type === "text" && c.text?.trim().length > 0 || c.type === "toolCall" && c.name?.trim().length > 0
855
862
  );
@@ -859,6 +866,7 @@ var Agent = class {
859
866
  }
860
867
  }
861
868
  } catch (err) {
869
+ if (!ownsRun()) return;
862
870
  const errorMsg = {
863
871
  role: "assistant",
864
872
  content: [{ type: "text", text: "" }],
@@ -879,19 +887,22 @@ var Agent = class {
879
887
  };
880
888
  this.appendMessage(errorMsg);
881
889
  this._state.error = err?.message || String(err);
882
- this.emit({ type: "agent_end", messages: [errorMsg] });
890
+ this.emit({ type: "agent_end", messages: [errorMsg] }, ownsRun);
883
891
  } finally {
884
- this._state.isStreaming = false;
885
- this._state.streamMessage = null;
886
- this._state.pendingToolCalls = /* @__PURE__ */ new Set();
887
- this.abortController = void 0;
892
+ if (ownsRun()) {
893
+ this._state.isStreaming = false;
894
+ this._state.streamMessage = null;
895
+ this._state.pendingToolCalls = /* @__PURE__ */ new Set();
896
+ this.abortController = void 0;
897
+ }
888
898
  }
889
899
  }
890
900
  reportObserverError(kind, eventType, error) {
891
901
  console.warn(`[Agent] ${kind} failed while handling ${eventType}:`, error);
892
902
  }
893
- emit(e) {
903
+ emit(e, ownsRun = () => true) {
894
904
  for (const listener of this.listeners) {
905
+ if (!ownsRun()) return;
895
906
  try {
896
907
  listener(e);
897
908
  } catch (error) {
@@ -899,13 +910,14 @@ var Agent = class {
899
910
  }
900
911
  }
901
912
  try {
902
- this.emitDerivedSemanticEvents(e);
913
+ if (ownsRun()) this.emitDerivedSemanticEvents(e, ownsRun);
903
914
  } catch (error) {
904
915
  this.reportObserverError("derived semantic event", e.type, error);
905
916
  }
906
917
  }
907
- emitSemantic(e) {
918
+ emitSemantic(e, ownsRun = () => true) {
908
919
  for (const listener of this.semanticListeners) {
920
+ if (!ownsRun()) return;
909
921
  try {
910
922
  listener(e);
911
923
  } catch (error) {
@@ -913,7 +925,8 @@ var Agent = class {
913
925
  }
914
926
  }
915
927
  }
916
- updateStatusSnapshot(next) {
928
+ updateStatusSnapshot(next, ownsRun = () => true) {
929
+ if (!ownsRun()) return;
917
930
  const snapshot = {
918
931
  ...this.statusSnapshot,
919
932
  ...next
@@ -922,26 +935,32 @@ var Agent = class {
922
935
  return;
923
936
  }
924
937
  this.statusSnapshot = snapshot;
925
- this.emitSemantic({ type: "status_snapshot_changed", snapshot: { ...snapshot } });
938
+ this.emitSemantic({ type: "status_snapshot_changed", snapshot: { ...snapshot } }, ownsRun);
926
939
  }
927
- emitDerivedSemanticEvents(event) {
940
+ emitDerivedSemanticEvents(event, ownsRun) {
941
+ const publish = {
942
+ updateStatusSnapshot: (next) => this.updateStatusSnapshot(next, ownsRun),
943
+ emitSemantic: (event2) => {
944
+ if (ownsRun()) this.emitSemantic(event2, ownsRun);
945
+ }
946
+ };
928
947
  switch (event.type) {
929
948
  case "message_start":
930
949
  if (event.message.role === "assistant") {
931
- this.updateStatusSnapshot({
950
+ publish.updateStatusSnapshot({
932
951
  phase: "responding",
933
952
  turnState: "continuing",
934
953
  activeToolName: void 0,
935
954
  lastStopReason: void 0,
936
955
  hasPendingToolContinuation: false
937
956
  });
938
- this.emitSemantic({ type: "assistant_response_started", message: event.message });
957
+ publish.emitSemantic({ type: "assistant_response_started", message: event.message });
939
958
  }
940
959
  break;
941
960
  case "message_update":
942
961
  if (event.message.role === "assistant" && event.assistantMessageEvent.type === "text_delta") {
943
- this.updateStatusSnapshot({ phase: "responding" });
944
- this.emitSemantic({
962
+ publish.updateStatusSnapshot({ phase: "responding" });
963
+ publish.emitSemantic({
945
964
  type: "assistant_response_delta",
946
965
  message: event.message,
947
966
  delta: event.assistantMessageEvent.delta
@@ -950,18 +969,18 @@ var Agent = class {
950
969
  break;
951
970
  case "message_end":
952
971
  if (event.message.role === "assistant") {
953
- this.emitSemantic({ type: "assistant_response_finished", message: event.message });
972
+ publish.emitSemantic({ type: "assistant_response_finished", message: event.message });
954
973
  }
955
974
  break;
956
975
  case "tool_execution_start":
957
- this.updateStatusSnapshot({
976
+ publish.updateStatusSnapshot({
958
977
  phase: "running_tool",
959
978
  turnState: "continuing",
960
979
  activeToolName: event.toolName,
961
980
  lastStopReason: void 0,
962
981
  hasPendingToolContinuation: false
963
982
  });
964
- this.emitSemantic({
983
+ publish.emitSemantic({
965
984
  type: "tool_started",
966
985
  toolCallId: event.toolCallId,
967
986
  toolName: event.toolName,
@@ -969,7 +988,7 @@ var Agent = class {
969
988
  });
970
989
  break;
971
990
  case "tool_execution_update":
972
- this.emitSemantic({
991
+ publish.emitSemantic({
973
992
  type: "tool_progressed",
974
993
  toolCallId: event.toolCallId,
975
994
  toolName: event.toolName,
@@ -978,11 +997,11 @@ var Agent = class {
978
997
  });
979
998
  break;
980
999
  case "tool_execution_end":
981
- this.updateStatusSnapshot({
1000
+ publish.updateStatusSnapshot({
982
1001
  phase: this._state.isStreaming ? "responding" : "completed",
983
1002
  activeToolName: void 0
984
1003
  });
985
- this.emitSemantic({
1004
+ publish.emitSemantic({
986
1005
  type: "tool_finished",
987
1006
  toolCallId: event.toolCallId,
988
1007
  toolName: event.toolName,
@@ -996,40 +1015,40 @@ var Agent = class {
996
1015
  const hasToolCalls = event.message.role === "assistant" && Array.isArray(event.message.content) && event.message.content.some((block) => block.type === "toolCall");
997
1016
  if (stopReason === "error" || stopReason === "aborted") {
998
1017
  this.reportedFailureMessage = event.message;
999
- this.updateStatusSnapshot({
1018
+ publish.updateStatusSnapshot({
1000
1019
  phase: "failed",
1001
1020
  turnState: "failed",
1002
1021
  lastStopReason: stopReason,
1003
1022
  hasPendingToolContinuation: false,
1004
1023
  activeToolName: void 0
1005
1024
  });
1006
- this.emitSemantic({
1025
+ publish.emitSemantic({
1007
1026
  type: "turn_failed",
1008
1027
  message: event.message,
1009
1028
  toolResults: event.toolResults
1010
1029
  });
1011
1030
  } else if (hasToolCalls) {
1012
- this.updateStatusSnapshot({
1031
+ publish.updateStatusSnapshot({
1013
1032
  phase: "responding",
1014
1033
  turnState: "continuing",
1015
1034
  lastStopReason: stopReason,
1016
1035
  hasPendingToolContinuation: true,
1017
1036
  activeToolName: void 0
1018
1037
  });
1019
- this.emitSemantic({
1038
+ publish.emitSemantic({
1020
1039
  type: "turn_continues",
1021
1040
  message: event.message,
1022
1041
  toolResults: event.toolResults
1023
1042
  });
1024
1043
  } else {
1025
- this.updateStatusSnapshot({
1044
+ publish.updateStatusSnapshot({
1026
1045
  phase: "completed",
1027
1046
  turnState: "completed",
1028
1047
  lastStopReason: stopReason,
1029
1048
  hasPendingToolContinuation: false,
1030
1049
  activeToolName: void 0
1031
1050
  });
1032
- this.emitSemantic({
1051
+ publish.emitSemantic({
1033
1052
  type: "turn_completed",
1034
1053
  message: event.message,
1035
1054
  toolResults: event.toolResults
@@ -1042,20 +1061,20 @@ var Agent = class {
1042
1061
  const alreadyReported = lastMessage !== void 0 && lastMessage === this.reportedFailureMessage;
1043
1062
  this.reportedFailureMessage = void 0;
1044
1063
  if (!alreadyReported && lastMessage?.role === "assistant" && (lastMessage.stopReason === "error" || lastMessage.stopReason === "aborted")) {
1045
- this.updateStatusSnapshot({
1064
+ publish.updateStatusSnapshot({
1046
1065
  phase: "failed",
1047
1066
  turnState: "failed",
1048
1067
  lastStopReason: lastMessage.stopReason,
1049
1068
  hasPendingToolContinuation: false,
1050
1069
  activeToolName: void 0
1051
1070
  });
1052
- this.emitSemantic({
1071
+ publish.emitSemantic({
1053
1072
  type: "turn_failed",
1054
1073
  message: lastMessage,
1055
1074
  toolResults: []
1056
1075
  });
1057
1076
  }
1058
- this.updateStatusSnapshot({
1077
+ publish.updateStatusSnapshot({
1059
1078
  phase: "idle",
1060
1079
  activeToolName: void 0,
1061
1080
  hasPendingToolContinuation: false
@@ -6,10 +6,10 @@ import {
6
6
  createSharedWebSearchTool,
7
7
  interpretShellExit,
8
8
  requestInteraction
9
- } from "./chunk-FIE3SSLW.js";
9
+ } from "./chunk-BFHCZVNO.js";
10
10
  import {
11
11
  getLogger
12
- } from "./chunk-Z4EIBY2E.js";
12
+ } from "./chunk-SUPAWIKP.js";
13
13
  import {
14
14
  Type
15
15
  } from "./chunk-XXGYUYXI.js";