@mozaik-ai/core 2.13.8 → 3.0.1

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
@@ -30,6 +30,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ Agent: () => Agent,
34
+ AgentRuntime: () => AgentRuntime,
33
35
  BaseCondition: () => BaseCondition,
34
36
  Context: () => Context,
35
37
  ContextItem: () => ContextItem,
@@ -54,7 +56,7 @@ __export(index_exports, {
54
56
  });
55
57
  module.exports = __toCommonJS(index_exports);
56
58
 
57
- // src/core/context-runtime/context.ts
59
+ // src/domain/model-context/context.ts
58
60
  var Context = class _Context {
59
61
  constructor(id, projectId, items) {
60
62
  this.projectId = projectId;
@@ -90,19 +92,19 @@ var Context = class _Context {
90
92
  }
91
93
  };
92
94
 
93
- // src/core/context-runtime/context-item.ts
95
+ // src/domain/model-context/context-item/context-item.ts
94
96
  var ContextItem = class {
95
97
  getType() {
96
98
  return this.type;
97
99
  }
98
100
  };
99
101
 
100
- // src/core/context-runtime/content.ts
101
- var Content = class {
102
+ // src/domain/model-context/context-item/item-content/item-content.ts
103
+ var ItemContent = class {
102
104
  };
103
105
 
104
- // src/core/context-runtime/content/input-text.ts
105
- var InputText = class _InputText extends Content {
106
+ // src/domain/model-context/context-item/item-content/input-text.ts
107
+ var InputText = class _InputText extends ItemContent {
106
108
  constructor(text) {
107
109
  super();
108
110
  this.text = text;
@@ -124,7 +126,7 @@ var InputText = class _InputText extends Content {
124
126
  }
125
127
  };
126
128
 
127
- // src/core/context-runtime/input/user-message.ts
129
+ // src/domain/model-context/context-item/client-item/user-message.ts
128
130
  var UserMessage = class _UserMessage extends ContextItem {
129
131
  constructor(content) {
130
132
  super();
@@ -149,7 +151,7 @@ var UserMessage = class _UserMessage extends ContextItem {
149
151
  }
150
152
  };
151
153
 
152
- // src/core/context-runtime/input/developer-message.ts
154
+ // src/domain/model-context/context-item/client-item/developer-message.ts
153
155
  var DeveloperMessage = class _DeveloperMessage extends ContextItem {
154
156
  constructor(content) {
155
157
  super();
@@ -174,8 +176,8 @@ var DeveloperMessage = class _DeveloperMessage extends ContextItem {
174
176
  }
175
177
  };
176
178
 
177
- // src/core/context-runtime/content/output-text.ts
178
- var OutputText = class _OutputText extends Content {
179
+ // src/domain/model-context/context-item/item-content/output-text.ts
180
+ var OutputText = class _OutputText extends ItemContent {
179
181
  constructor(text) {
180
182
  super();
181
183
  this.text = text;
@@ -194,7 +196,7 @@ var OutputText = class _OutputText extends Content {
194
196
  }
195
197
  };
196
198
 
197
- // src/core/context-runtime/output/model-message.ts
199
+ // src/domain/model-context/context-item/model-item/model-message.ts
198
200
  var ModelMessage = class _ModelMessage extends ContextItem {
199
201
  constructor(content) {
200
202
  super();
@@ -215,7 +217,7 @@ var ModelMessage = class _ModelMessage extends ContextItem {
215
217
  }
216
218
  };
217
219
 
218
- // src/core/context-runtime/output/function-call.ts
220
+ // src/domain/model-context/context-item/model-item/function-call.ts
219
221
  var FunctionCall = class _FunctionCall extends ContextItem {
220
222
  constructor(callId, name, args) {
221
223
  super();
@@ -237,7 +239,7 @@ var FunctionCall = class _FunctionCall extends ContextItem {
237
239
  }
238
240
  };
239
241
 
240
- // src/core/context-runtime/output/reasoning.ts
242
+ // src/domain/model-context/context-item/model-item/reasoning.ts
241
243
  var Reasoning = class _Reasoning extends ContextItem {
242
244
  constructor(content, encryptedContent, summary = []) {
243
245
  super();
@@ -260,7 +262,7 @@ var Reasoning = class _Reasoning extends ContextItem {
260
262
  }
261
263
  };
262
264
 
263
- // src/core/context-runtime/input/function-call-output.ts
265
+ // src/domain/model-context/context-item/client-item/function-call-output.ts
264
266
  var FunctionCallOutput = class _FunctionCallOutput extends ContextItem {
265
267
  constructor(callId, output) {
266
268
  super();
@@ -284,7 +286,7 @@ var FunctionCallOutput = class _FunctionCallOutput extends ContextItem {
284
286
  }
285
287
  };
286
288
 
287
- // src/core/generative-model/inference-response.ts
289
+ // src/domain/generative-model/inference-response.ts
288
290
  var InferenceResponse = class {
289
291
  constructor(contextItems, tokenUsage) {
290
292
  this.contextItems = contextItems;
@@ -292,7 +294,7 @@ var InferenceResponse = class {
292
294
  }
293
295
  };
294
296
 
295
- // src/core/generative-model/token-usage.ts
297
+ // src/domain/generative-model/token-usage.ts
296
298
  var InputTokenDetails = class {
297
299
  constructor(cached_tokens) {
298
300
  this.cached_tokens = cached_tokens;
@@ -313,7 +315,7 @@ var TokenUsage = class {
313
315
  }
314
316
  };
315
317
 
316
- // src/providers/openai/runtime/openai-responses.ts
318
+ // src/infrastructure/providers/openai/runtime/openai-responses.ts
317
319
  var import_openai = __toESM(require("openai"));
318
320
  var OpenAIResponses = class {
319
321
  constructor() {
@@ -380,7 +382,7 @@ var OpenAIResponses = class {
380
382
  }
381
383
  };
382
384
 
383
- // src/core/generative-model/inference-request.ts
385
+ // src/domain/generative-model/inference-request.ts
384
386
  var InferenceRequest = class {
385
387
  constructor(model, context) {
386
388
  this.model = model;
@@ -388,7 +390,7 @@ var InferenceRequest = class {
388
390
  }
389
391
  };
390
392
 
391
- // src/providers/openai/reasoning-effort.ts
393
+ // src/infrastructure/providers/openai/reasoning-effort.ts
392
394
  var OpenAIReasoningEffort = class {
393
395
  constructor(reasoningEffort) {
394
396
  this.reasoningEffort = reasoningEffort;
@@ -404,7 +406,7 @@ var OpenAIReasoningEffort = class {
404
406
  }
405
407
  };
406
408
 
407
- // src/providers/openai/models/gpt-5-4-nano.ts
409
+ // src/infrastructure/providers/openai/models/gpt-5-4-nano.ts
408
410
  var Gpt54Nano = class {
409
411
  constructor() {
410
412
  this.specification = {
@@ -435,7 +437,7 @@ var Gpt54Nano = class {
435
437
  }
436
438
  };
437
439
 
438
- // src/providers/openai/models/gpt-5-4.ts
440
+ // src/infrastructure/providers/openai/models/gpt-5-4.ts
439
441
  var Gpt54 = class {
440
442
  constructor() {
441
443
  this.specification = {
@@ -466,7 +468,7 @@ var Gpt54 = class {
466
468
  }
467
469
  };
468
470
 
469
- // src/providers/openai/models/gpt-5-4-mini.ts
471
+ // src/infrastructure/providers/openai/models/gpt-5-4-mini.ts
470
472
  var Gpt54Mini = class {
471
473
  constructor() {
472
474
  this.specification = {
@@ -497,7 +499,7 @@ var Gpt54Mini = class {
497
499
  }
498
500
  };
499
501
 
500
- // src/core/context-engine/condition/condition.ts
502
+ // src/domain/agent-loop/rules/condition/condition.ts
501
503
  var BaseCondition = class {
502
504
  and(other) {
503
505
  return new AndCondition(this, other);
@@ -539,7 +541,7 @@ var OrCondition = class extends BaseCondition {
539
541
  }
540
542
  };
541
543
 
542
- // src/core/context-engine/rule/rule.ts
544
+ // src/domain/agent-loop/rules/rule/rule.ts
543
545
  var If = class {
544
546
  constructor({ condition, action }) {
545
547
  this.condition = condition;
@@ -565,7 +567,7 @@ var Loop = class {
565
567
  }
566
568
  };
567
569
 
568
- // src/memory/in-memory-context-repository.ts
570
+ // src/infrastructure/repository/in-memory-context-repository.ts
569
571
  var InMemoryContextRepository = class {
570
572
  constructor() {
571
573
  this.store = /* @__PURE__ */ new Map();
@@ -606,8 +608,355 @@ var InMemoryContextRepository = class {
606
608
  });
607
609
  }
608
610
  };
611
+
612
+ // src/domain/agent-loop/execution.ts
613
+ var Execution = class _Execution {
614
+ constructor(executionId, currentStateId, previousStateId, status, stepCount, retryCounts, history) {
615
+ this.executionId = executionId;
616
+ this.currentStateId = currentStateId;
617
+ this.previousStateId = previousStateId;
618
+ this.status = status;
619
+ this.stepCount = stepCount;
620
+ this.retryCounts = retryCounts;
621
+ this.history = history;
622
+ }
623
+ isTerminal() {
624
+ return this.status == 1 /* COMPLETED */ || this.status == 2 /* FAILED */;
625
+ }
626
+ static create() {
627
+ const executionId = crypto.randomUUID();
628
+ const currentStateId = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
629
+ const previousStateId = null;
630
+ const status = 0 /* RUNNING */;
631
+ const stepCount = 0;
632
+ const retryCounts = /* @__PURE__ */ new Map();
633
+ const history = [];
634
+ return new _Execution(executionId, currentStateId, previousStateId, status, stepCount, retryCounts, history);
635
+ }
636
+ };
637
+
638
+ // src/domain/agent-loop/transition/go-to.ts
639
+ var GoTo = class {
640
+ constructor(next) {
641
+ this.next = next;
642
+ }
643
+ async apply(context) {
644
+ const execution = context.execution;
645
+ execution.previousStateId = execution.currentStateId;
646
+ execution.currentStateId = this.next;
647
+ execution.stepCount++;
648
+ execution.history.push({
649
+ fromState: execution.previousStateId,
650
+ toState: execution.currentStateId,
651
+ timestamp: /* @__PURE__ */ new Date(),
652
+ duration: 0,
653
+ error: null
654
+ });
655
+ }
656
+ };
657
+
658
+ // src/domain/agent-loop/transition/fail.ts
659
+ var Fail = class {
660
+ constructor(error) {
661
+ this.error = error;
662
+ }
663
+ async apply(context) {
664
+ const execution = context.execution;
665
+ execution.status = 2 /* FAILED */;
666
+ }
667
+ };
668
+
669
+ // src/domain/agent-loop/state/function-call.ts
670
+ var FunctionCallState = class {
671
+ constructor() {
672
+ this.id = "FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */;
673
+ this.beforeHookId = "BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */;
674
+ this.afterHookId = "AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */;
675
+ }
676
+ getDetails() {
677
+ return {
678
+ before: this.beforeHookId,
679
+ stateId: this.id,
680
+ after: this.afterHookId
681
+ };
682
+ }
683
+ validateEntry(runtime) {
684
+ var _a;
685
+ const functionCall = (_a = runtime.inferenceResponse) == null ? void 0 : _a.contextItems.find((item) => item.getType() === "function_call");
686
+ if (!functionCall || !(functionCall instanceof FunctionCall)) {
687
+ throw new Error("Function call not found");
688
+ }
689
+ }
690
+ next(runtime) {
691
+ const functionCallOutput = runtime.functionCallOutput;
692
+ if (!functionCallOutput) {
693
+ return new Fail("Function call output not found");
694
+ }
695
+ runtime.context.addItem(functionCallOutput);
696
+ return new GoTo("INFERENCE_PENDING" /* INFERENCE_PENDING */);
697
+ }
698
+ };
699
+
700
+ // src/domain/agent-loop/transition/complete.ts
701
+ var Complete = class {
702
+ constructor(result) {
703
+ this.result = result;
704
+ }
705
+ async apply(context) {
706
+ const execution = context.execution;
707
+ execution.status = 1 /* COMPLETED */;
708
+ }
709
+ };
710
+
711
+ // src/domain/agent-loop/state/model-message.ts
712
+ var ModelMessageState = class {
713
+ constructor() {
714
+ this.id = "MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */;
715
+ this.beforeHookId = "BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */;
716
+ this.afterHookId = "AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */;
717
+ }
718
+ getDetails() {
719
+ return {
720
+ before: this.beforeHookId,
721
+ stateId: this.id,
722
+ after: this.afterHookId
723
+ };
724
+ }
725
+ validateEntry(runtime) {
726
+ if (!runtime.modelMessage) {
727
+ throw new Error("Model message not found");
728
+ }
729
+ }
730
+ next(runtime) {
731
+ if (!runtime.modelMessage) {
732
+ return new Fail("Model message not found");
733
+ }
734
+ return new Complete(runtime.modelMessage.content.text);
735
+ }
736
+ };
737
+
738
+ // src/domain/agent-loop/state/inference-pending.ts
739
+ var InferencePendingState = class {
740
+ constructor() {
741
+ this.id = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
742
+ this.beforeHookId = "BEFORE_INFERENCE" /* BEFORE_INFERENCE */;
743
+ this.afterHookId = "AFTER_INFERENCE" /* AFTER_INFERENCE */;
744
+ }
745
+ getDetails() {
746
+ return {
747
+ before: this.beforeHookId,
748
+ stateId: this.id,
749
+ after: this.afterHookId
750
+ };
751
+ }
752
+ validateEntry(runtime) {
753
+ const inferenceRequest = runtime.inferenceRequest;
754
+ if (!inferenceRequest) {
755
+ throw new Error("Inference request not found");
756
+ }
757
+ }
758
+ next(runtime) {
759
+ const inferenceResponse = runtime.inferenceResponse;
760
+ if (!inferenceResponse) {
761
+ return new Fail("Inference response not found");
762
+ }
763
+ if (inferenceResponse instanceof FunctionCall) {
764
+ return new GoTo("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */);
765
+ }
766
+ if (inferenceResponse instanceof ModelMessage) {
767
+ return new GoTo("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */);
768
+ }
769
+ return new Fail("Invalid response type");
770
+ }
771
+ };
772
+
773
+ // src/domain/agent-loop/loop.ts
774
+ var AgentLoop = class {
775
+ constructor() {
776
+ this.states = /* @__PURE__ */ new Map();
777
+ this.states.set("INFERENCE_PENDING" /* INFERENCE_PENDING */, new InferencePendingState());
778
+ this.states.set("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, new FunctionCallState());
779
+ this.states.set("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, new ModelMessageState());
780
+ }
781
+ validateEntry(runtime) {
782
+ const state = this.states.get(runtime.execution.currentStateId);
783
+ if (!state) {
784
+ throw new Error(`State ${runtime.execution.currentStateId} not found`);
785
+ }
786
+ state.validateEntry(runtime);
787
+ }
788
+ getStateDetails(runtime) {
789
+ const state = this.states.get(runtime.execution.currentStateId);
790
+ if (!state) {
791
+ throw new Error(`State ${runtime.execution.currentStateId} not found`);
792
+ }
793
+ return state.getDetails();
794
+ }
795
+ next(runtime) {
796
+ const state = this.states.get(runtime.execution.currentStateId);
797
+ if (!state) {
798
+ throw new Error(`State ${runtime.execution.currentStateId} not found`);
799
+ }
800
+ return state.next(runtime);
801
+ }
802
+ };
803
+
804
+ // src/domain/agent-loop/hooks/hooks-registry.ts
805
+ var HooksRegistry = class {
806
+ constructor() {
807
+ this.handlers = /* @__PURE__ */ new Map();
808
+ }
809
+ getHandler(hookId) {
810
+ const handler = this.handlers.get(hookId);
811
+ if (!handler) {
812
+ throw new Error(`Hook handler for hook ${hookId} not found`);
813
+ }
814
+ return handler;
815
+ }
816
+ registerHandler(hookId, handler) {
817
+ this.handlers.set(hookId, handler);
818
+ }
819
+ unregisterHandler(hookId) {
820
+ this.handlers.delete(hookId);
821
+ }
822
+ };
823
+
824
+ // src/domain/agent-loop/state/state-registry.ts
825
+ var StateHandlerRegistry = class {
826
+ constructor() {
827
+ this.handlers = /* @__PURE__ */ new Map();
828
+ }
829
+ getHandler(stateId) {
830
+ const handler = this.handlers.get(stateId);
831
+ if (!handler) {
832
+ throw new Error(`Handler for state ${stateId} not found`);
833
+ }
834
+ return handler;
835
+ }
836
+ registerHandler(stateId, handler) {
837
+ this.handlers.set(stateId, handler);
838
+ }
839
+ unregisterHandler(stateId) {
840
+ this.handlers.delete(stateId);
841
+ }
842
+ };
843
+
844
+ // src/application/agent-runtime.ts
845
+ var AgentRuntime = class {
846
+ constructor() {
847
+ this.hooksRegistry = new HooksRegistry();
848
+ this.stateHandlerRegistry = new StateHandlerRegistry();
849
+ this.stateHandlerRegistry.registerHandler("INFERENCE_PENDING" /* INFERENCE_PENDING */, this.onInferencePending);
850
+ this.stateHandlerRegistry.registerHandler("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, this.onFunctionCallPending);
851
+ this.stateHandlerRegistry.registerHandler("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, this.onModelMessageReceived);
852
+ }
853
+ async onInferencePending(context) {
854
+ const openaiResponses = new OpenAIResponses();
855
+ const inferenceRequest = new InferenceRequest(context.model, context.context);
856
+ const inferenceResponse = await openaiResponses.infer(inferenceRequest);
857
+ context.inferenceResponse = inferenceResponse;
858
+ return Promise.resolve();
859
+ }
860
+ async onFunctionCallPending(context) {
861
+ var _a;
862
+ const functionCall = (_a = context.inferenceResponse) == null ? void 0 : _a.contextItems.find((item) => item.getType() === "function_call");
863
+ if (!functionCall || !(functionCall instanceof FunctionCall)) {
864
+ throw new Error("Function call not found");
865
+ }
866
+ const tool = context.model.getTools().find((tool2) => tool2.name === functionCall.name);
867
+ if (!tool) {
868
+ throw new Error("Function not found");
869
+ }
870
+ const functionCallOutput = await tool.invoke(functionCall.args);
871
+ context.functionCallOutput = FunctionCallOutput.create(functionCall.callId, functionCallOutput);
872
+ return Promise.resolve();
873
+ }
874
+ async onModelMessageReceived(context) {
875
+ return Promise.resolve();
876
+ }
877
+ async on(hookId, callback) {
878
+ this.hooksRegistry.registerHandler(hookId, callback);
879
+ }
880
+ async start(userMessage, model, context) {
881
+ const execution = Execution.create();
882
+ const loop = new AgentLoop();
883
+ const runtimeContext = {
884
+ execution,
885
+ userMessage,
886
+ model,
887
+ context
888
+ };
889
+ while (!execution.isTerminal()) {
890
+ const transition = loop.next(runtimeContext);
891
+ await transition.apply(runtimeContext);
892
+ try {
893
+ const stateDetails = loop.getStateDetails(runtimeContext);
894
+ if (stateDetails.before) {
895
+ const handler = this.hooksRegistry.getHandler(stateDetails.before);
896
+ await handler(runtimeContext);
897
+ }
898
+ if (stateDetails.after) {
899
+ const handler = this.hooksRegistry.getHandler(stateDetails.after);
900
+ await handler(runtimeContext);
901
+ }
902
+ } catch (error) {
903
+ execution.status = 2 /* FAILED */;
904
+ break;
905
+ }
906
+ }
907
+ }
908
+ };
909
+
910
+ // src/application/agent.ts
911
+ var Agent = class {
912
+ constructor(runtime) {
913
+ this.runtime = runtime;
914
+ this.runtime.on("BEFORE_INFERENCE" /* BEFORE_INFERENCE */, this.beforeInference);
915
+ this.runtime.on("AFTER_INFERENCE" /* AFTER_INFERENCE */, this.afterInference);
916
+ this.runtime.on("BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */, this.beforeFunctionCall);
917
+ this.runtime.on("AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */, this.afterFunctionCall);
918
+ this.runtime.on("BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */, this.beforeModelMessage);
919
+ this.runtime.on("AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */, this.afterModelMessage);
920
+ }
921
+ async beforeInference(context) {
922
+ return Promise.resolve();
923
+ }
924
+ async afterInference(context) {
925
+ const inferenceResponse = context.inferenceResponse;
926
+ if (inferenceResponse && this.visitor) {
927
+ await this.visitor.afterInference(inferenceResponse);
928
+ }
929
+ return Promise.resolve();
930
+ }
931
+ async beforeFunctionCall(context) {
932
+ return Promise.resolve();
933
+ }
934
+ async afterFunctionCall(context) {
935
+ return Promise.resolve();
936
+ }
937
+ async beforeModelMessage(context) {
938
+ return Promise.resolve();
939
+ }
940
+ async afterModelMessage(context) {
941
+ return Promise.resolve();
942
+ }
943
+ async onStart(context) {
944
+ var _a;
945
+ await ((_a = this.visitor) == null ? void 0 : _a.onStart(context));
946
+ return Promise.resolve();
947
+ }
948
+ setInferenceVisitor(visitor) {
949
+ this.visitor = visitor;
950
+ }
951
+ async run(userMessage, model, context) {
952
+ const userMessageItem = UserMessage.create(userMessage);
953
+ return this.runtime.start(userMessageItem, model, context);
954
+ }
955
+ };
609
956
  // Annotate the CommonJS export names for ESM import in node:
610
957
  0 && (module.exports = {
958
+ Agent,
959
+ AgentRuntime,
611
960
  BaseCondition,
612
961
  Context,
613
962
  ContextItem,