@mozaik-ai/core 3.3.2 → 3.3.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.
package/dist/index.mjs CHANGED
@@ -75,7 +75,7 @@ var InputText = class _InputText extends ItemContent {
75
75
  };
76
76
 
77
77
  // src/domain/model-context/context-item/client-item/user-message.ts
78
- var UserMessage = class _UserMessage extends ContextItem {
78
+ var UserMessageItem = class _UserMessageItem extends ContextItem {
79
79
  constructor(content) {
80
80
  super();
81
81
  this.type = "message";
@@ -84,11 +84,11 @@ var UserMessage = class _UserMessage extends ContextItem {
84
84
  }
85
85
  static create(text) {
86
86
  const content = InputText.create(text);
87
- return new _UserMessage(content);
87
+ return new _UserMessageItem(content);
88
88
  }
89
89
  static rehydrate(data) {
90
90
  const content = InputText.rehydrate(data);
91
- return new _UserMessage(content);
91
+ return new _UserMessageItem(content);
92
92
  }
93
93
  toJSON() {
94
94
  return {
@@ -100,7 +100,7 @@ var UserMessage = class _UserMessage extends ContextItem {
100
100
  };
101
101
 
102
102
  // src/domain/model-context/context-item/client-item/developer-message.ts
103
- var DeveloperMessage = class _DeveloperMessage extends ContextItem {
103
+ var DeveloperMessageItem = class _DeveloperMessageItem extends ContextItem {
104
104
  constructor(content) {
105
105
  super();
106
106
  this.type = "message";
@@ -116,11 +116,11 @@ var DeveloperMessage = class _DeveloperMessage extends ContextItem {
116
116
  }
117
117
  static create(text) {
118
118
  const content = InputText.create(text);
119
- return new _DeveloperMessage(content);
119
+ return new _DeveloperMessageItem(content);
120
120
  }
121
121
  static rehydrate(data) {
122
122
  const content = InputText.rehydrate(data);
123
- return new _DeveloperMessage(content);
123
+ return new _DeveloperMessageItem(content);
124
124
  }
125
125
  };
126
126
 
@@ -145,7 +145,7 @@ var OutputText = class _OutputText extends ItemContent {
145
145
  };
146
146
 
147
147
  // src/domain/model-context/context-item/model-item/model-message.ts
148
- var ModelMessage = class _ModelMessage extends ContextItem {
148
+ var ModelMessageItem = class _ModelMessageItem extends ContextItem {
149
149
  constructor(content) {
150
150
  super();
151
151
  this.type = "message";
@@ -154,7 +154,7 @@ var ModelMessage = class _ModelMessage extends ContextItem {
154
154
  }
155
155
  static rehydrate(data) {
156
156
  const content = OutputText.rehydrate(data);
157
- return new _ModelMessage(content);
157
+ return new _ModelMessageItem(content);
158
158
  }
159
159
  toJSON() {
160
160
  return {
@@ -166,7 +166,7 @@ var ModelMessage = class _ModelMessage extends ContextItem {
166
166
  };
167
167
 
168
168
  // src/domain/model-context/context-item/model-item/function-call.ts
169
- var FunctionCall = class _FunctionCall extends ContextItem {
169
+ var FunctionCallItem = class _FunctionCallItem extends ContextItem {
170
170
  constructor(callId, name, args) {
171
171
  super();
172
172
  this.type = "function_call";
@@ -175,7 +175,7 @@ var FunctionCall = class _FunctionCall extends ContextItem {
175
175
  this.args = args;
176
176
  }
177
177
  static rehydrate(data) {
178
- return new _FunctionCall(data.callId, data.name, data.args);
178
+ return new _FunctionCallItem(data.callId, data.name, data.args);
179
179
  }
180
180
  toJSON() {
181
181
  return {
@@ -188,7 +188,7 @@ var FunctionCall = class _FunctionCall extends ContextItem {
188
188
  };
189
189
 
190
190
  // src/domain/model-context/context-item/model-item/reasoning.ts
191
- var Reasoning = class _Reasoning extends ContextItem {
191
+ var ReasoningItem = class _ReasoningItem extends ContextItem {
192
192
  constructor(content, encryptedContent, summary = []) {
193
193
  super();
194
194
  this.type = "reasoning";
@@ -197,7 +197,7 @@ var Reasoning = class _Reasoning extends ContextItem {
197
197
  this.summary = summary;
198
198
  }
199
199
  static rehydrate(data) {
200
- return new _Reasoning(data.content, data.encryptedContent, data.summary);
200
+ return new _ReasoningItem(data.content, data.encryptedContent, data.summary);
201
201
  }
202
202
  toJSON() {
203
203
  var _a;
@@ -211,7 +211,7 @@ var Reasoning = class _Reasoning extends ContextItem {
211
211
  };
212
212
 
213
213
  // src/domain/model-context/context-item/client-item/function-call-output.ts
214
- var FunctionCallOutput = class _FunctionCallOutput extends ContextItem {
214
+ var FunctionCallOutputItem = class _FunctionCallOutputItem extends ContextItem {
215
215
  constructor(callId, output) {
216
216
  super();
217
217
  this.type = "function_call_output";
@@ -220,10 +220,10 @@ var FunctionCallOutput = class _FunctionCallOutput extends ContextItem {
220
220
  }
221
221
  static create(callId, output) {
222
222
  const outputText = InputText.create(output);
223
- return new _FunctionCallOutput(callId, outputText);
223
+ return new _FunctionCallOutputItem(callId, outputText);
224
224
  }
225
225
  static rehydrate(data) {
226
- return new _FunctionCallOutput(data.callId, data.output);
226
+ return new _FunctionCallOutputItem(data.callId, data.output);
227
227
  }
228
228
  toJSON() {
229
229
  return {
@@ -314,17 +314,17 @@ var OpenAIResponses = class {
314
314
  extractContextItems(response) {
315
315
  return response.output.map((item) => {
316
316
  if (item.type === "message" && item.role === "assistant") {
317
- return ModelMessage.rehydrate(item.content[0]);
317
+ return ModelMessageItem.rehydrate(item.content[0]);
318
318
  }
319
319
  if (item.type === "function_call") {
320
- return FunctionCall.rehydrate({
320
+ return FunctionCallItem.rehydrate({
321
321
  callId: item.call_id,
322
322
  name: item.name,
323
323
  args: item.arguments
324
324
  });
325
325
  }
326
326
  if (item.type === "reasoning") {
327
- return Reasoning.rehydrate(item);
327
+ return ReasoningItem.rehydrate(item);
328
328
  }
329
329
  });
330
330
  }
@@ -615,32 +615,32 @@ var Fail = class {
615
615
  };
616
616
 
617
617
  // src/domain/model-context/specifications/function-call-requested.ts
618
- var FunctionCallRequestedSpecification = class extends BaseSpecification {
618
+ var FunctionCallRequestedSpec = class extends BaseSpecification {
619
619
  isSatisfiedBy(context) {
620
620
  if (context.getItems().length === 0) {
621
621
  return false;
622
622
  }
623
- const lastItem = context.getItems()[context.getItems().length - 1];
624
- return lastItem instanceof FunctionCall;
623
+ const lastItem = context.getLastItem();
624
+ return lastItem instanceof FunctionCallItem;
625
625
  }
626
626
  };
627
627
 
628
628
  // src/domain/model-context/specifications/function-call-output-present.ts
629
- var FunctionCallOutputPresentSpecification = class extends BaseSpecification {
629
+ var FunctionCallOutputPresentSpec = class extends BaseSpecification {
630
630
  isSatisfiedBy(context) {
631
- const lasteItem = context.getItems()[context.getItems().length - 1];
632
- return lasteItem instanceof FunctionCallOutput;
631
+ const lasteItem = context.getLastItem();
632
+ return lasteItem instanceof FunctionCallOutputItem;
633
633
  }
634
634
  };
635
635
 
636
- // src/domain/agent-loop/state/function-call.ts
637
- var FunctionCallState = class {
636
+ // src/domain/agent-loop/state/function-call-pending.ts
637
+ var FunctionCallPendingState = class {
638
638
  constructor() {
639
639
  this.id = "FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */;
640
640
  this.beforeHookId = "BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */;
641
641
  this.afterHookId = "AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */;
642
- this.functionCallRequestedSpec = new FunctionCallRequestedSpecification();
643
- this.functionCallOutputPresentSpec = new FunctionCallOutputPresentSpecification();
642
+ this.functionCallRequestedSpec = new FunctionCallRequestedSpec();
643
+ this.functionCallOutputPresentSpec = new FunctionCallOutputPresentSpec();
644
644
  }
645
645
  getDetails() {
646
646
  return {
@@ -674,23 +674,23 @@ var Complete = class {
674
674
  };
675
675
 
676
676
  // src/domain/model-context/specifications/model-responded.ts
677
- var ModelRespondedSpecification = class extends BaseSpecification {
677
+ var ModelRespondedSpec = class extends BaseSpecification {
678
678
  isSatisfiedBy(context) {
679
679
  if (context.getItems().length === 0) {
680
680
  return false;
681
681
  }
682
- const lastItem = context.getItems()[context.getItems().length - 1];
683
- return lastItem instanceof ModelMessage && lastItem.role === "assistant";
682
+ const lastItem = context.getLastItem();
683
+ return lastItem instanceof ModelMessageItem && lastItem.role === "assistant";
684
684
  }
685
685
  };
686
686
 
687
- // src/domain/agent-loop/state/model-message.ts
688
- var ModelMessageState = class {
687
+ // src/domain/agent-loop/state/model-responded.ts
688
+ var ModelRespondedState = class {
689
689
  constructor() {
690
- this.id = "MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */;
691
- this.beforeHookId = "BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */;
692
- this.afterHookId = "AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */;
693
- this.modelResponded = new ModelRespondedSpecification();
690
+ this.id = "MODEL_RESPONDED" /* MODEL_RESPONDED */;
691
+ this.beforeHookId = "BEFORE_MODEL_RESPONDED" /* BEFORE_MODEL_RESPONDED */;
692
+ this.afterHookId = "AFTER_MODEL_RESPONDED" /* AFTER_MODEL_RESPONDED */;
693
+ this.modelRespondedSpec = new ModelRespondedSpec();
694
694
  }
695
695
  getDetails() {
696
696
  return {
@@ -700,12 +700,12 @@ var ModelMessageState = class {
700
700
  };
701
701
  }
702
702
  validateEntry(runtime) {
703
- if (!this.modelResponded.isSatisfiedBy(runtime.context)) {
703
+ if (!this.modelRespondedSpec.isSatisfiedBy(runtime.context)) {
704
704
  throw new Error("Model message is not present in the context");
705
705
  }
706
706
  }
707
707
  next(runtime) {
708
- if (!this.modelResponded.isSatisfiedBy(runtime.context)) {
708
+ if (!this.modelRespondedSpec.isSatisfiedBy(runtime.context)) {
709
709
  return new Fail("Model message is not present in the context");
710
710
  }
711
711
  const lastItem = runtime.context.getLastItem();
@@ -719,8 +719,8 @@ var InferencePendingState = class {
719
719
  this.id = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
720
720
  this.beforeHookId = "BEFORE_INFERENCE" /* BEFORE_INFERENCE */;
721
721
  this.afterHookId = "AFTER_INFERENCE" /* AFTER_INFERENCE */;
722
- this.functionCallRequested = new FunctionCallRequestedSpecification();
723
- this.modelResponded = new ModelRespondedSpecification();
722
+ this.functionCallRequested = new FunctionCallRequestedSpec();
723
+ this.modelResponded = new ModelRespondedSpec();
724
724
  }
725
725
  getDetails() {
726
726
  return {
@@ -735,40 +735,48 @@ var InferencePendingState = class {
735
735
  if (this.functionCallRequested.isSatisfiedBy(runtime.context)) {
736
736
  return new GoTo("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */);
737
737
  } else if (this.modelResponded.isSatisfiedBy(runtime.context)) {
738
- return new GoTo("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */);
738
+ return new GoTo("MODEL_RESPONDED" /* MODEL_RESPONDED */);
739
739
  }
740
740
  return new Fail("Invalid response type");
741
741
  }
742
742
  };
743
743
 
744
- // src/domain/agent-loop/loop.ts
744
+ // src/domain/agent-loop/state/message-received.ts
745
+ var MessageReceivedState = class {
746
+ constructor() {
747
+ this.id = "MESSAGE_RECEIVED" /* MESSAGE_RECEIVED */;
748
+ this.beforeHookId = "BEFORE_MESSAGE_RECEIVED" /* BEFORE_MESSAGE_RECEIVED */;
749
+ this.afterHookId = "AFTER_MESSAGE_RECEIVED" /* AFTER_MESSAGE_RECEIVED */;
750
+ }
751
+ getDetails() {
752
+ return {
753
+ before: this.beforeHookId,
754
+ stateId: this.id,
755
+ after: this.afterHookId
756
+ };
757
+ }
758
+ validateEntry(context) {
759
+ }
760
+ next(context) {
761
+ return new GoTo("INFERENCE_PENDING" /* INFERENCE_PENDING */);
762
+ }
763
+ };
764
+
765
+ // src/domain/agent-loop/agent-loop.ts
745
766
  var AgentLoop = class {
746
767
  constructor() {
747
768
  this.states = /* @__PURE__ */ new Map();
769
+ this.states.set("MESSAGE_RECEIVED" /* MESSAGE_RECEIVED */, new MessageReceivedState());
748
770
  this.states.set("INFERENCE_PENDING" /* INFERENCE_PENDING */, new InferencePendingState());
749
- this.states.set("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, new FunctionCallState());
750
- this.states.set("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, new ModelMessageState());
771
+ this.states.set("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, new FunctionCallPendingState());
772
+ this.states.set("MODEL_RESPONDED" /* MODEL_RESPONDED */, new ModelRespondedState());
751
773
  }
752
- validateEntry(runtime) {
774
+ getState(runtime) {
753
775
  const state = this.states.get(runtime.execution.currentStateId);
754
776
  if (!state) {
755
777
  throw new Error(`State ${runtime.execution.currentStateId} not found`);
756
778
  }
757
- state.validateEntry(runtime);
758
- }
759
- getStateDetails(runtime) {
760
- const state = this.states.get(runtime.execution.currentStateId);
761
- if (!state) {
762
- throw new Error(`State ${runtime.execution.currentStateId} not found`);
763
- }
764
- return state.getDetails();
765
- }
766
- next(runtime) {
767
- const state = this.states.get(runtime.execution.currentStateId);
768
- if (!state) {
769
- throw new Error(`State ${runtime.execution.currentStateId} not found`);
770
- }
771
- return state.next(runtime);
779
+ return state;
772
780
  }
773
781
  };
774
782
 
@@ -817,11 +825,15 @@ var AgentRuntime = class {
817
825
  constructor() {
818
826
  this.hooksRegistry = new HooksRegistry();
819
827
  this.stateHandlerRegistry = new StateHandlerRegistry();
828
+ this.stateHandlerRegistry.registerHandler("MESSAGE_RECEIVED" /* MESSAGE_RECEIVED */, this.onMessageReceived);
820
829
  this.stateHandlerRegistry.registerHandler("INFERENCE_PENDING" /* INFERENCE_PENDING */, this.onInferencePending);
821
830
  this.stateHandlerRegistry.registerHandler("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, this.onFunctionCallPending);
822
- this.stateHandlerRegistry.registerHandler("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, this.onModelMessageReceived);
831
+ this.stateHandlerRegistry.registerHandler("MODEL_RESPONDED" /* MODEL_RESPONDED */, this.onModelResponded);
823
832
  this.hooksRegistry.registerHandler("ON_ERROR" /* ON_ERROR */, this.onError);
824
833
  }
834
+ async onMessageReceived(runtimeContext) {
835
+ return Promise.resolve();
836
+ }
825
837
  async onInferencePending(runtimeContext) {
826
838
  const openaiResponses = new OpenAIResponses();
827
839
  const inferenceRequest = new InferenceRequest(runtimeContext.model, runtimeContext.context);
@@ -838,7 +850,7 @@ var AgentRuntime = class {
838
850
  }
839
851
  const functionCallOutput = await tool.invoke(functionCall.args);
840
852
  runtimeContext.context.addContextItem(
841
- FunctionCallOutput.create(functionCall.callId, JSON.stringify(functionCallOutput))
853
+ FunctionCallOutputItem.create(functionCall.callId, JSON.stringify(functionCallOutput))
842
854
  );
843
855
  return Promise.resolve();
844
856
  }
@@ -850,7 +862,7 @@ var AgentRuntime = class {
850
862
  console.error(error);
851
863
  return Promise.resolve();
852
864
  }
853
- async onModelMessageReceived(context) {
865
+ async onModelResponded(context) {
854
866
  return Promise.resolve();
855
867
  }
856
868
  async on(hookId, callback) {
@@ -868,9 +880,10 @@ var AgentRuntime = class {
868
880
  };
869
881
  while (!execution.isTerminal()) {
870
882
  console.log("Executing state", execution.currentStateId);
883
+ const state = loop.getState(runtimeContext);
871
884
  try {
872
- loop.validateEntry(runtimeContext);
873
- const stateDetails = loop.getStateDetails(runtimeContext);
885
+ state.validateEntry(runtimeContext);
886
+ const stateDetails = state.getDetails();
874
887
  if (stateDetails.before) {
875
888
  const handler = this.hooksRegistry.getHandler(stateDetails.before);
876
889
  await handler(runtimeContext);
@@ -888,7 +901,7 @@ var AgentRuntime = class {
888
901
  runtimeContext.error = error;
889
902
  await handler(runtimeContext);
890
903
  }
891
- const transition = loop.next(runtimeContext);
904
+ const transition = state.next(runtimeContext);
892
905
  await transition.apply(runtimeContext);
893
906
  }
894
907
  }
@@ -896,16 +909,24 @@ var AgentRuntime = class {
896
909
 
897
910
  // src/application/agent.ts
898
911
  var Agent = class {
899
- constructor(runtime) {
900
- this.runtime = runtime;
912
+ constructor() {
913
+ this.runtime = new AgentRuntime();
914
+ this.runtime.on("BEFORE_MESSAGE_RECEIVED" /* BEFORE_MESSAGE_RECEIVED */, this.beforeMessageReceived);
915
+ this.runtime.on("AFTER_MESSAGE_RECEIVED" /* AFTER_MESSAGE_RECEIVED */, this.afterMessageReceived);
901
916
  this.runtime.on("BEFORE_INFERENCE" /* BEFORE_INFERENCE */, this.beforeInference);
902
917
  this.runtime.on("AFTER_INFERENCE" /* AFTER_INFERENCE */, this.afterInference);
903
918
  this.runtime.on("BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */, this.beforeFunctionCall);
904
919
  this.runtime.on("AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */, this.afterFunctionCall);
905
- this.runtime.on("BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */, this.beforeModelMessage);
906
- this.runtime.on("AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */, this.afterModelMessage);
920
+ this.runtime.on("BEFORE_MODEL_RESPONDED" /* BEFORE_MODEL_RESPONDED */, this.beforeModelResponded);
921
+ this.runtime.on("AFTER_MODEL_RESPONDED" /* AFTER_MODEL_RESPONDED */, this.afterModelResponded);
907
922
  this.runtime.on("ON_ERROR" /* ON_ERROR */, this.onError);
908
923
  }
924
+ async beforeMessageReceived(context) {
925
+ return Promise.resolve();
926
+ }
927
+ async afterMessageReceived(context) {
928
+ return Promise.resolve();
929
+ }
909
930
  async beforeInference(context) {
910
931
  return Promise.resolve();
911
932
  }
@@ -928,10 +949,10 @@ var Agent = class {
928
949
  }
929
950
  return Promise.resolve();
930
951
  }
931
- async beforeModelMessage(context) {
952
+ async beforeModelResponded(context) {
932
953
  return Promise.resolve();
933
954
  }
934
- async afterModelMessage(context) {
955
+ async afterModelResponded(context) {
935
956
  return Promise.resolve();
936
957
  }
937
958
  async onError(context) {
@@ -951,7 +972,7 @@ var Agent = class {
951
972
  this.functionCallVisitor = visitor;
952
973
  }
953
974
  async run(userMessage, model, context) {
954
- const userMessageItem = UserMessage.create(userMessage);
975
+ const userMessageItem = UserMessageItem.create(userMessage);
955
976
  return this.runtime.start(userMessageItem, model, context);
956
977
  }
957
978
  };
@@ -981,15 +1002,39 @@ var AgentSociety = class {
981
1002
  this.isActive = false;
982
1003
  }
983
1004
  };
1005
+
1006
+ // src/domain/model-context/context-item/client-item/system-message.ts
1007
+ var SystemMessageItem = class _SystemMessageItem extends ContextItem {
1008
+ constructor(content) {
1009
+ super();
1010
+ this.type = "message";
1011
+ this.role = "system";
1012
+ this.content = content;
1013
+ }
1014
+ toJSON() {
1015
+ return {
1016
+ type: this.type,
1017
+ role: this.role,
1018
+ content: this.content.toJSON()
1019
+ };
1020
+ }
1021
+ static create(text) {
1022
+ const content = InputText.create(text);
1023
+ return new _SystemMessageItem(content);
1024
+ }
1025
+ static rehydrate(data) {
1026
+ const content = InputText.rehydrate(data);
1027
+ return new _SystemMessageItem(content);
1028
+ }
1029
+ };
984
1030
  export {
985
1031
  Agent,
986
- AgentRuntime,
987
1032
  AgentSociety,
988
1033
  BaseSpecification,
989
1034
  ContextItem,
990
- DeveloperMessage,
991
- FunctionCall,
992
- FunctionCallOutput,
1035
+ DeveloperMessageItem,
1036
+ FunctionCallItem,
1037
+ FunctionCallOutputItem,
993
1038
  Gpt54,
994
1039
  Gpt54Mini,
995
1040
  Gpt54Nano,
@@ -1000,11 +1045,12 @@ export {
1000
1045
  InputTokenDetails,
1001
1046
  Loop,
1002
1047
  ModelContext,
1003
- ModelMessage,
1048
+ ModelMessageItem,
1004
1049
  OpenAIResponses,
1005
1050
  OutputTokenDetails,
1006
- Reasoning,
1051
+ ReasoningItem,
1052
+ SystemMessageItem,
1007
1053
  TokenUsage,
1008
- UserMessage
1054
+ UserMessageItem
1009
1055
  };
1010
1056
  //# sourceMappingURL=index.mjs.map