@mozaik-ai/core 3.1.4 → 3.3.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.js CHANGED
@@ -33,8 +33,7 @@ __export(index_exports, {
33
33
  Agent: () => Agent,
34
34
  AgentRuntime: () => AgentRuntime,
35
35
  AgentSociety: () => AgentSociety,
36
- BaseCondition: () => BaseCondition,
37
- Context: () => Context,
36
+ BaseSpecification: () => BaseSpecification,
38
37
  ContextItem: () => ContextItem,
39
38
  DeveloperMessage: () => DeveloperMessage,
40
39
  FunctionCall: () => FunctionCall,
@@ -43,11 +42,12 @@ __export(index_exports, {
43
42
  Gpt54Mini: () => Gpt54Mini,
44
43
  Gpt54Nano: () => Gpt54Nano,
45
44
  If: () => If,
46
- InMemoryContextRepository: () => InMemoryContextRepository,
45
+ InMemoryModelContextRepository: () => InMemoryModelContextRepository,
47
46
  InferenceRequest: () => InferenceRequest,
48
47
  InferenceResponse: () => InferenceResponse,
49
48
  InputTokenDetails: () => InputTokenDetails,
50
49
  Loop: () => Loop,
50
+ ModelContext: () => ModelContext,
51
51
  ModelMessage: () => ModelMessage,
52
52
  OpenAIResponses: () => OpenAIResponses,
53
53
  OutputTokenDetails: () => OutputTokenDetails,
@@ -57,14 +57,14 @@ __export(index_exports, {
57
57
  });
58
58
  module.exports = __toCommonJS(index_exports);
59
59
 
60
- // src/domain/model-context/context.ts
61
- var Context = class _Context {
60
+ // src/domain/model-context/model-context.ts
61
+ var ModelContext = class _ModelContext {
62
62
  constructor(id, projectId, items) {
63
63
  this.projectId = projectId;
64
64
  this.id = id;
65
65
  this.items = items;
66
66
  }
67
- addItem(item) {
67
+ addContextItem(item) {
68
68
  this.items.push(item);
69
69
  return this;
70
70
  }
@@ -81,12 +81,18 @@ var Context = class _Context {
81
81
  getItems() {
82
82
  return this.items;
83
83
  }
84
+ getLastItem() {
85
+ if (this.items.length === 0) {
86
+ throw new Error("No items in context");
87
+ }
88
+ return this.items[this.items.length - 1];
89
+ }
84
90
  static create(projectId) {
85
91
  const id = crypto.randomUUID();
86
- return new _Context(id, projectId, []);
92
+ return new _ModelContext(id, projectId, []);
87
93
  }
88
94
  static rehydrate(data) {
89
- return new _Context(data.id, data.projectId, data.items);
95
+ return new _ModelContext(data.id, data.projectId, data.items);
90
96
  }
91
97
  toJSON() {
92
98
  return this.items.map((item) => item.toJSON());
@@ -500,19 +506,19 @@ var Gpt54Mini = class {
500
506
  }
501
507
  };
502
508
 
503
- // src/domain/agent-loop/rules/condition/condition.ts
504
- var BaseCondition = class {
509
+ // src/domain/specification/specification/specification.ts
510
+ var BaseSpecification = class {
505
511
  and(other) {
506
- return new AndCondition(this, other);
512
+ return new AndSpecification(this, other);
507
513
  }
508
514
  or(other) {
509
- return new OrCondition(this, other);
515
+ return new OrSpecification(this, other);
510
516
  }
511
517
  not() {
512
- return new NotCondition(this);
518
+ return new NotSpecification(this);
513
519
  }
514
520
  };
515
- var AndCondition = class extends BaseCondition {
521
+ var AndSpecification = class extends BaseSpecification {
516
522
  constructor(left, right) {
517
523
  super();
518
524
  this.left = left;
@@ -522,16 +528,16 @@ var AndCondition = class extends BaseCondition {
522
528
  return this.left.isSatisfiedBy(candidate) && this.right.isSatisfiedBy(candidate);
523
529
  }
524
530
  };
525
- var NotCondition = class extends BaseCondition {
526
- constructor(condition) {
531
+ var NotSpecification = class extends BaseSpecification {
532
+ constructor(specification) {
527
533
  super();
528
- this.condition = condition;
534
+ this.specification = specification;
529
535
  }
530
536
  isSatisfiedBy(candidate) {
531
- return !this.condition.isSatisfiedBy(candidate);
537
+ return !this.specification.isSatisfiedBy(candidate);
532
538
  }
533
539
  };
534
- var OrCondition = class extends BaseCondition {
540
+ var OrSpecification = class extends BaseSpecification {
535
541
  constructor(left, right) {
536
542
  super();
537
543
  this.left = left;
@@ -542,34 +548,34 @@ var OrCondition = class extends BaseCondition {
542
548
  }
543
549
  };
544
550
 
545
- // src/domain/agent-loop/rules/rule/rule.ts
551
+ // src/domain/specification/rule/rule.ts
546
552
  var If = class {
547
- constructor({ condition, action }) {
548
- this.condition = condition;
553
+ constructor({ specification, action }) {
554
+ this.specification = specification;
549
555
  this.action = action;
550
556
  }
551
557
  async apply(candidate) {
552
- if (this.condition.isSatisfiedBy(candidate)) {
558
+ if (this.specification.isSatisfiedBy(candidate)) {
553
559
  return await this.action.apply(candidate);
554
560
  }
555
561
  return candidate;
556
562
  }
557
563
  };
558
564
  var Loop = class {
559
- constructor({ condition, action }) {
560
- this.condition = condition;
565
+ constructor({ specification, action }) {
566
+ this.specification = specification;
561
567
  this.action = action;
562
568
  }
563
569
  async apply(candidate) {
564
- while (this.condition.isSatisfiedBy(candidate)) {
570
+ while (this.specification.isSatisfiedBy(candidate)) {
565
571
  candidate = await this.action.apply(candidate);
566
572
  }
567
573
  return candidate;
568
574
  }
569
575
  };
570
576
 
571
- // src/infrastructure/repository/in-memory-context-repository.ts
572
- var InMemoryContextRepository = class {
577
+ // src/infrastructure/repository/in-memory-model-context-repository.ts
578
+ var InMemoryModelContextRepository = class {
573
579
  constructor() {
574
580
  this.store = /* @__PURE__ */ new Map();
575
581
  this.projectIndex = /* @__PURE__ */ new Map();
@@ -602,7 +608,7 @@ var InMemoryContextRepository = class {
602
608
  return [...ids].map((id) => this.store.get(id)).filter((c) => Boolean(c)).map((c) => this.clone(c));
603
609
  }
604
610
  clone(context) {
605
- return Context.rehydrate({
611
+ return ModelContext.rehydrate({
606
612
  id: context.id,
607
613
  projectId: context.projectId,
608
614
  items: [...context.getItems()]
@@ -667,12 +673,33 @@ var Fail = class {
667
673
  }
668
674
  };
669
675
 
676
+ // src/domain/model-context/specifications/function-call-requested.ts
677
+ var FunctionCallRequestedSpecification = class extends BaseSpecification {
678
+ isSatisfiedBy(context) {
679
+ if (context.getItems().length === 0) {
680
+ return false;
681
+ }
682
+ const lastItem = context.getItems()[context.getItems().length - 1];
683
+ return lastItem instanceof FunctionCall;
684
+ }
685
+ };
686
+
687
+ // src/domain/model-context/specifications/function-call-output-present.ts
688
+ var FunctionCallOutputPresentSpecification = class extends BaseSpecification {
689
+ isSatisfiedBy(context) {
690
+ const lasteItem = context.getItems()[context.getItems().length - 1];
691
+ return lasteItem instanceof FunctionCallOutput;
692
+ }
693
+ };
694
+
670
695
  // src/domain/agent-loop/state/function-call.ts
671
696
  var FunctionCallState = class {
672
697
  constructor() {
673
698
  this.id = "FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */;
674
699
  this.beforeHookId = "BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */;
675
700
  this.afterHookId = "AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */;
701
+ this.functionCallRequestedSpec = new FunctionCallRequestedSpecification();
702
+ this.functionCallOutputPresentSpec = new FunctionCallOutputPresentSpecification();
676
703
  }
677
704
  getDetails() {
678
705
  return {
@@ -682,15 +709,12 @@ var FunctionCallState = class {
682
709
  };
683
710
  }
684
711
  validateEntry(runtime) {
685
- var _a;
686
- const functionCall = (_a = runtime.inferenceResponse) == null ? void 0 : _a.contextItems.find((item) => item.getType() === "function_call");
687
- if (!functionCall || !(functionCall instanceof FunctionCall)) {
688
- throw new Error("Function call not found");
712
+ if (!this.functionCallRequestedSpec.isSatisfiedBy(runtime.context)) {
713
+ throw new Error("Function call requested not found");
689
714
  }
690
715
  }
691
716
  next(runtime) {
692
- const functionCallOutput = runtime.functionCallOutput;
693
- if (!functionCallOutput) {
717
+ if (!this.functionCallOutputPresentSpec.isSatisfiedBy(runtime.context)) {
694
718
  return new Fail("Function call output not found");
695
719
  }
696
720
  return new GoTo("INFERENCE_PENDING" /* INFERENCE_PENDING */);
@@ -708,12 +732,24 @@ var Complete = class {
708
732
  }
709
733
  };
710
734
 
735
+ // src/domain/model-context/specifications/model-responded.ts
736
+ var ModelRespondedSpecification = class extends BaseSpecification {
737
+ isSatisfiedBy(context) {
738
+ if (context.getItems().length === 0) {
739
+ return false;
740
+ }
741
+ const lastItem = context.getItems()[context.getItems().length - 1];
742
+ return lastItem instanceof ModelMessage && lastItem.role === "assistant";
743
+ }
744
+ };
745
+
711
746
  // src/domain/agent-loop/state/model-message.ts
712
747
  var ModelMessageState = class {
713
748
  constructor() {
714
749
  this.id = "MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */;
715
750
  this.beforeHookId = "BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */;
716
751
  this.afterHookId = "AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */;
752
+ this.modelResponded = new ModelRespondedSpecification();
717
753
  }
718
754
  getDetails() {
719
755
  return {
@@ -723,15 +759,16 @@ var ModelMessageState = class {
723
759
  };
724
760
  }
725
761
  validateEntry(runtime) {
726
- if (!runtime.modelMessage) {
727
- throw new Error("Model message not found");
762
+ if (!this.modelResponded.isSatisfiedBy(runtime.context)) {
763
+ throw new Error("Model message is not present in the context");
728
764
  }
729
765
  }
730
766
  next(runtime) {
731
- if (!runtime.modelMessage) {
732
- return new Fail("Model message not found");
767
+ if (!this.modelResponded.isSatisfiedBy(runtime.context)) {
768
+ return new Fail("Model message is not present in the context");
733
769
  }
734
- return new Complete(runtime.modelMessage.content.text);
770
+ const lastItem = runtime.context.getLastItem();
771
+ return new Complete(lastItem.content.text);
735
772
  }
736
773
  };
737
774
 
@@ -741,6 +778,8 @@ var InferencePendingState = class {
741
778
  this.id = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
742
779
  this.beforeHookId = "BEFORE_INFERENCE" /* BEFORE_INFERENCE */;
743
780
  this.afterHookId = "AFTER_INFERENCE" /* AFTER_INFERENCE */;
781
+ this.functionCallRequested = new FunctionCallRequestedSpecification();
782
+ this.modelResponded = new ModelRespondedSpecification();
744
783
  }
745
784
  getDetails() {
746
785
  return {
@@ -750,25 +789,11 @@ var InferencePendingState = class {
750
789
  };
751
790
  }
752
791
  validateEntry(runtime) {
753
- const inferenceRequest = runtime.inferenceRequest;
754
- if (!inferenceRequest) {
755
- throw new Error("Inference request not found");
756
- }
757
792
  }
758
793
  next(runtime) {
759
- const inferenceResponse = runtime.inferenceResponse;
760
- if (!inferenceResponse) {
761
- return new Fail("Inference response not found");
762
- }
763
- const contextItems = inferenceResponse.contextItems;
764
- if (contextItems.length === 0) {
765
- return new Fail("No context items found");
766
- }
767
- const lastItem = contextItems[contextItems.length - 1];
768
- if (lastItem instanceof FunctionCall) {
794
+ if (this.functionCallRequested.isSatisfiedBy(runtime.context)) {
769
795
  return new GoTo("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */);
770
- }
771
- if (lastItem instanceof ModelMessage) {
796
+ } else if (this.modelResponded.isSatisfiedBy(runtime.context)) {
772
797
  return new GoTo("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */);
773
798
  }
774
799
  return new Fail("Invalid response type");
@@ -856,27 +881,24 @@ var AgentRuntime = class {
856
881
  this.stateHandlerRegistry.registerHandler("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, this.onModelMessageReceived);
857
882
  this.hooksRegistry.registerHandler("ON_ERROR" /* ON_ERROR */, this.onError);
858
883
  }
859
- async onInferencePending(context) {
884
+ async onInferencePending(runtimeContext) {
860
885
  const openaiResponses = new OpenAIResponses();
861
- const inferenceRequest = new InferenceRequest(context.model, context.context);
886
+ const inferenceRequest = new InferenceRequest(runtimeContext.model, runtimeContext.context);
862
887
  const inferenceResponse = await openaiResponses.infer(inferenceRequest);
863
- context.inferenceResponse = inferenceResponse;
864
- context.context.applyModelOutput(inferenceResponse.contextItems);
888
+ runtimeContext.inferenceResponse = inferenceResponse;
889
+ runtimeContext.context.applyModelOutput(inferenceResponse.contextItems);
865
890
  return Promise.resolve();
866
891
  }
867
- async onFunctionCallPending(context) {
868
- var _a;
869
- const functionCall = (_a = context.inferenceResponse) == null ? void 0 : _a.contextItems.find((item) => item.getType() === "function_call");
870
- if (!functionCall || !(functionCall instanceof FunctionCall)) {
871
- throw new Error("Function call not found");
872
- }
873
- const tool = context.model.getTools().find((tool2) => tool2.name === functionCall.name);
892
+ async onFunctionCallPending(runtimeContext) {
893
+ const functionCall = runtimeContext.context.getLastItem();
894
+ const tool = runtimeContext.model.getTools().find((tool2) => tool2.name === functionCall.name);
874
895
  if (!tool) {
875
896
  throw new Error("Function not found");
876
897
  }
877
898
  const functionCallOutput = await tool.invoke(functionCall.args);
878
- context.functionCallOutput = functionCallOutput;
879
- context.context.addItem(FunctionCallOutput.create(functionCall.callId, JSON.stringify(functionCallOutput)));
899
+ runtimeContext.context.addContextItem(
900
+ FunctionCallOutput.create(functionCall.callId, JSON.stringify(functionCallOutput))
901
+ );
880
902
  return Promise.resolve();
881
903
  }
882
904
  async onError(context) {
@@ -1022,8 +1044,7 @@ var AgentSociety = class {
1022
1044
  Agent,
1023
1045
  AgentRuntime,
1024
1046
  AgentSociety,
1025
- BaseCondition,
1026
- Context,
1047
+ BaseSpecification,
1027
1048
  ContextItem,
1028
1049
  DeveloperMessage,
1029
1050
  FunctionCall,
@@ -1032,11 +1053,12 @@ var AgentSociety = class {
1032
1053
  Gpt54Mini,
1033
1054
  Gpt54Nano,
1034
1055
  If,
1035
- InMemoryContextRepository,
1056
+ InMemoryModelContextRepository,
1036
1057
  InferenceRequest,
1037
1058
  InferenceResponse,
1038
1059
  InputTokenDetails,
1039
1060
  Loop,
1061
+ ModelContext,
1040
1062
  ModelMessage,
1041
1063
  OpenAIResponses,
1042
1064
  OutputTokenDetails,