@mozaik-ai/core 4.0.0-beta.5 → 4.0.0-beta.7

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
@@ -314,6 +314,16 @@ var SemanticEvent = class _SemanticEvent {
314
314
  return new _SemanticEvent(type, producerId, occurredAt, payload);
315
315
  }
316
316
  };
317
+ var ParticipantJoinedEvent = class extends SemanticEvent {
318
+ static init(manifest) {
319
+ return SemanticEvent.create("participant.joined", manifest.id, manifest);
320
+ }
321
+ };
322
+ var ParticipantLeftEvent = class extends SemanticEvent {
323
+ static init(manifest) {
324
+ return SemanticEvent.create("participant.left", manifest.id, manifest);
325
+ }
326
+ };
317
327
  var MessageSentEvent = class extends SemanticEvent {
318
328
  static init(producerId, message) {
319
329
  return SemanticEvent.create("message.sent", producerId, { message });
@@ -472,9 +482,11 @@ var RuntimeService = class {
472
482
  }
473
483
  join(participant) {
474
484
  this.state.addParticipant(participant);
485
+ this.publish(ParticipantJoinedEvent.init(participant.getManifest()));
475
486
  }
476
487
  leave(participant) {
477
488
  this.state.removeParticipant(participant);
489
+ this.publish(ParticipantLeftEvent.init(participant.getManifest()));
478
490
  }
479
491
  publish(event) {
480
492
  for (const participant of this.state.getParticipants()) {
@@ -538,28 +550,43 @@ function createSendMessage(resolveRuntime) {
538
550
  }
539
551
 
540
552
  // src/domain/agentic-environment/loop/agent-loop.ts
541
- var AgentLoop = class {
542
- constructor(stateExecutor, transitionResolver, loopVisitor) {
553
+ var AgentLoop = class _AgentLoop {
554
+ constructor(loopId, stateExecutor, transitionResolver, interceptionHandler) {
555
+ this.loopId = loopId;
543
556
  this.stateExecutor = stateExecutor;
544
557
  this.transitionResolver = transitionResolver;
545
- this.loopVisitor = loopVisitor;
558
+ this.interceptionHandler = interceptionHandler;
546
559
  }
547
- async run(message) {
560
+ async run(message, loopVisitor) {
548
561
  let transition = {
549
- nextStateId: "context_preparation",
562
+ nextStateId: "context_update",
550
563
  input: message
551
564
  };
552
565
  while (transition.nextStateId !== "idle") {
553
- const execution = await this.stateExecutor.execute(transition, this.loopVisitor);
554
- transition = this.transitionResolver.resolve(execution);
566
+ const isInterceptionSatisfied = this.interceptionHandler.isSatisfiedBy(transition);
567
+ if (isInterceptionSatisfied) {
568
+ loopVisitor.visitInterceptionStarted(transition);
569
+ transition = await this.interceptionHandler.handle(transition);
570
+ loopVisitor.visitInterceptionFinished(transition);
571
+ } else {
572
+ const execution = await this.stateExecutor.execute(transition, loopVisitor);
573
+ transition = this.transitionResolver.resolve(execution);
574
+ }
555
575
  }
556
576
  }
577
+ getLoopId() {
578
+ return this.loopId;
579
+ }
580
+ static create(stateExecutor, transitionResolver, interceptionHandler) {
581
+ const loopId = crypto.randomUUID();
582
+ return new _AgentLoop(loopId, stateExecutor, transitionResolver, interceptionHandler);
583
+ }
557
584
  };
558
585
 
559
586
  // src/domain/agentic-environment/loop/state-executor.ts
560
587
  var LoopStateExecutor = class {
561
- constructor(contextPreparationState, inferenceState, inferenceStreamingState, functionCallState, modelMessageState) {
562
- this.contextPreparationState = contextPreparationState;
588
+ constructor(contextUpdateState, inferenceState, inferenceStreamingState, functionCallState, modelMessageState) {
589
+ this.contextUpdateState = contextUpdateState;
563
590
  this.inferenceState = inferenceState;
564
591
  this.inferenceStreamingState = inferenceStreamingState;
565
592
  this.functionCallState = functionCallState;
@@ -567,8 +594,8 @@ var LoopStateExecutor = class {
567
594
  }
568
595
  async execute(transition, loopVisitor) {
569
596
  switch (transition.nextStateId) {
570
- case "context_preparation":
571
- return await this.contextPreparationState.run(transition.input, loopVisitor);
597
+ case "context_update":
598
+ return await this.contextUpdateState.run(transition.input, loopVisitor);
572
599
  case "inference":
573
600
  return await this.inferenceState.run(transition.input, loopVisitor);
574
601
  case "inference_streaming":
@@ -613,19 +640,19 @@ var FunctionCallState = class {
613
640
  }
614
641
  };
615
642
 
616
- // src/application/states/context-preparation.ts
617
- var ContextPreparationState = class {
643
+ // src/application/states/context-update.ts
644
+ var ContextUpdateState = class {
618
645
  constructor() {
619
- this.id = "context_preparation";
646
+ this.id = "context_update";
620
647
  }
621
648
  async run(input, loopVisitor) {
622
- loopVisitor.visitContextPreparationStarted(input);
649
+ loopVisitor.visitContextUpdateStarted(input);
623
650
  const userMessage = UserMessageItem.create(input.content);
624
651
  const output = {
625
652
  ...input.input,
626
653
  context: input.input.context.addContextItems([userMessage])
627
654
  };
628
- loopVisitor.visitContextPreparationCompleted(output);
655
+ loopVisitor.visitContextUpdateCompleted(output);
629
656
  return {
630
657
  stateId: this.id,
631
658
  input,
@@ -666,7 +693,7 @@ var TransitionResolver = class {
666
693
  // src/domain/agentic-environment/loop/transition-specification.ts
667
694
  var MessageReceivedSpecification = class {
668
695
  isSatisfiedBy(execution) {
669
- return execution.stateId === "context_preparation";
696
+ return execution.stateId === "context_update";
670
697
  }
671
698
  };
672
699
  function isInferenceExecution(execution) {
@@ -699,7 +726,7 @@ var ContextPreparationToInferenceRule = class {
699
726
  this.specification = new MessageReceivedSpecification();
700
727
  }
701
728
  createTransition(execution) {
702
- if (execution.stateId !== "context_preparation") {
729
+ if (execution.stateId !== "context_update") {
703
730
  throw new Error("Expected context_preparation execution");
704
731
  }
705
732
  const nextStateId = execution.output.streaming ? "inference_streaming" : "inference";
@@ -787,47 +814,47 @@ var ModelMessageToIdleRule = class {
787
814
 
788
815
  // src/domain/agentic-environment/loop/event-publisher-visitor.ts
789
816
  var EventPublisherLoopVisitor = class {
790
- constructor(agentId, runtime) {
817
+ constructor(agentId, loopId, runtime) {
791
818
  this.agentId = agentId;
819
+ this.loopId = loopId;
792
820
  this.runtime = runtime;
793
821
  }
794
- visitContextPreparationStarted(input) {
795
- console.log("Visiting message received started: ", input);
796
- this.publish("context_preparation.started", input);
822
+ visitContextUpdateStarted(input) {
823
+ this.publish("context_update.started", { ...input, loopId: this.loopId });
797
824
  }
798
- visitContextPreparationCompleted(output) {
799
- console.log("Visiting message received completed: ", output);
800
- this.publish("context_preparation.completed", output);
825
+ visitContextUpdateCompleted(output) {
826
+ this.publish("context_update.completed", { ...output, loopId: this.loopId });
801
827
  }
802
828
  visitInferenceStarted(input) {
803
- console.log("Visiting inference started: ", input);
804
829
  this.publish("inference.started", input);
805
830
  }
806
831
  visitInferenceEvent(event) {
807
832
  this.publish("inference.stream", event);
808
833
  }
809
834
  visitInferenceCompleted(output) {
810
- console.log("Visiting inference completed: ", output);
811
835
  this.publish("inference.completed", output);
812
836
  }
813
837
  visitFunctionCallStarted(input) {
814
- console.log("Visiting function call started: ", input);
815
838
  this.publish("function_call.started", input);
816
839
  }
817
840
  visitFunctionCallCompleted(output) {
818
- console.log("Visiting function call completed: ", output);
819
841
  this.publish("function_call.completed", output);
820
842
  }
821
843
  visitModelAnswer(input) {
822
- console.log("Visiting model answer: ", input.answer);
823
844
  this.publish("model.answer", input);
824
845
  }
846
+ visitInterceptionStarted(transition) {
847
+ this.publish("interception.started", transition);
848
+ }
849
+ visitInterceptionFinished(transition) {
850
+ this.publish("interception.finished", transition);
851
+ }
825
852
  publish(type, payload) {
826
853
  this.runtime.publish({
827
854
  type,
828
855
  producerId: this.agentId,
829
856
  occurredAt: /* @__PURE__ */ new Date(),
830
- payload
857
+ payload: { ...payload, loopId: this.loopId }
831
858
  });
832
859
  }
833
860
  };
@@ -861,7 +888,7 @@ var InferenceStreamingState = class {
861
888
 
862
889
  // src/application/use-cases/run-loop.ts
863
890
  function createRunLoop(resolveRuntime) {
864
- return function runLoop(agentId, message, inferenceInput) {
891
+ return function runLoop(agentId, message, inferenceInput, interceptionHandler) {
865
892
  const runtime = resolveRuntime();
866
893
  const inferenceRunner = runtime.getInferenceRunner();
867
894
  const functionCallRunner = runtime.getFunctionCallRunner();
@@ -874,21 +901,21 @@ function createRunLoop(resolveRuntime) {
874
901
  new ModelMessageToIdleRule()
875
902
  ]);
876
903
  const stateExecutor = new LoopStateExecutor(
877
- new ContextPreparationState(),
904
+ new ContextUpdateState(),
878
905
  new InferenceState(inferenceRunner),
879
906
  new InferenceStreamingState(inferenceRunner),
880
907
  new FunctionCallState(functionCallRunner),
881
908
  new ModelMessageState()
882
909
  );
883
- const agentLoop = new AgentLoop(
884
- stateExecutor,
885
- transitionResolver,
886
- new EventPublisherLoopVisitor(agentId, runtime)
910
+ const agentLoop = AgentLoop.create(stateExecutor, transitionResolver, interceptionHandler);
911
+ const loopVisitor = new EventPublisherLoopVisitor(agentId, agentLoop.getLoopId(), runtime);
912
+ agentLoop.run(
913
+ {
914
+ content: message,
915
+ input: inferenceInput
916
+ },
917
+ loopVisitor
887
918
  );
888
- agentLoop.run({
889
- content: message,
890
- input: inferenceInput
891
- });
892
919
  };
893
920
  }
894
921
 
@@ -2154,6 +2181,7 @@ export {
2154
2181
  Agent,
2155
2182
  AnthropicMessages,
2156
2183
  ContextItem,
2184
+ DefaultInferenceRunner,
2157
2185
  DeveloperMessageItem,
2158
2186
  FunctionCallItem,
2159
2187
  FunctionCallOutputItem,