@mozaik-ai/core 4.0.0-beta.2 → 4.0.0-beta.4

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
@@ -543,7 +543,7 @@ var AgentLoop = class {
543
543
  }
544
544
  async run(message) {
545
545
  let transition = {
546
- nextStateId: "message_received",
546
+ nextStateId: "context_preparation",
547
547
  input: message
548
548
  };
549
549
  while (transition.nextStateId !== "idle") {
@@ -555,18 +555,21 @@ var AgentLoop = class {
555
555
 
556
556
  // src/domain/agentic-environment/loop/state-executor.ts
557
557
  var LoopStateExecutor = class {
558
- constructor(messageReceivedState, inferenceState, functionCallState, modelMessageState) {
559
- this.messageReceivedState = messageReceivedState;
558
+ constructor(contextPreparationState, inferenceState, inferenceStreamingState, functionCallState, modelMessageState) {
559
+ this.contextPreparationState = contextPreparationState;
560
560
  this.inferenceState = inferenceState;
561
+ this.inferenceStreamingState = inferenceStreamingState;
561
562
  this.functionCallState = functionCallState;
562
563
  this.modelMessageState = modelMessageState;
563
564
  }
564
565
  async execute(transition, loopVisitor) {
565
566
  switch (transition.nextStateId) {
566
- case "message_received":
567
- return await this.messageReceivedState.run(transition.input, loopVisitor);
567
+ case "context_preparation":
568
+ return await this.contextPreparationState.run(transition.input, loopVisitor);
568
569
  case "inference":
569
570
  return await this.inferenceState.run(transition.input, loopVisitor);
571
+ case "inference_streaming":
572
+ return await this.inferenceStreamingState.run(transition.input, loopVisitor);
570
573
  case "function_call":
571
574
  return await this.functionCallState.run(transition.input, loopVisitor);
572
575
  case "model_message":
@@ -607,19 +610,19 @@ var FunctionCallState = class {
607
610
  }
608
611
  };
609
612
 
610
- // src/application/states/message-received.ts
611
- var MessageReceivedState = class {
613
+ // src/application/states/context-preparation.ts
614
+ var ContextPreparationState = class {
612
615
  constructor() {
613
- this.id = "message_received";
616
+ this.id = "context_preparation";
614
617
  }
615
618
  async run(input, loopVisitor) {
616
- loopVisitor.visitMessageReceivedStarted(input);
619
+ loopVisitor.visitContextPreparationStarted(input);
617
620
  const userMessage = UserMessageItem.create(input.content);
618
621
  const output = {
619
622
  ...input.input,
620
623
  context: input.input.context.addContextItems([userMessage])
621
624
  };
622
- loopVisitor.visitMessageReceivedCompleted(output);
625
+ loopVisitor.visitContextPreparationCompleted(output);
623
626
  return {
624
627
  stateId: this.id,
625
628
  input,
@@ -660,17 +663,20 @@ var TransitionResolver = class {
660
663
  // src/domain/agentic-environment/loop/transition-specification.ts
661
664
  var MessageReceivedSpecification = class {
662
665
  isSatisfiedBy(execution) {
663
- return execution.stateId === "message_received";
666
+ return execution.stateId === "context_preparation";
664
667
  }
665
668
  };
669
+ function isInferenceExecution(execution) {
670
+ return execution.stateId === "inference" || execution.stateId === "inference_streaming";
671
+ }
666
672
  var InferenceProducedFunctionCallSpecification = class {
667
673
  isSatisfiedBy(execution) {
668
- return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "function_call");
674
+ return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "function_call");
669
675
  }
670
676
  };
671
677
  var InferenceProducedModelMessageSpecification = class {
672
678
  isSatisfiedBy(execution) {
673
- return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
679
+ return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
674
680
  }
675
681
  };
676
682
  var FunctionCallCompletedSpecification = class {
@@ -685,16 +691,17 @@ var ModelMessageCompletedSpecification = class {
685
691
  };
686
692
 
687
693
  // src/domain/agentic-environment/loop/transition-rule.ts
688
- var MessageReceivedToInferenceRule = class {
694
+ var ContextPreparationToInferenceRule = class {
689
695
  constructor() {
690
696
  this.specification = new MessageReceivedSpecification();
691
697
  }
692
698
  createTransition(execution) {
693
- if (execution.stateId !== "message_received") {
694
- throw new Error("Expected message_received execution");
699
+ if (execution.stateId !== "context_preparation") {
700
+ throw new Error("Expected context_preparation execution");
695
701
  }
702
+ const nextStateId = execution.output.streaming ? "inference_streaming" : "inference";
696
703
  return {
697
- nextStateId: "inference",
704
+ nextStateId,
698
705
  input: execution.output
699
706
  };
700
707
  }
@@ -704,7 +711,7 @@ var InferenceToFunctionCallRule = class {
704
711
  this.specification = new InferenceProducedFunctionCallSpecification();
705
712
  }
706
713
  createTransition(execution) {
707
- if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "function_call")) {
714
+ if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "function_call")) {
708
715
  throw new Error("Expected inference function-call output");
709
716
  }
710
717
  const functionCallItem = execution.output.items.find((item) => item.type === "function_call");
@@ -725,7 +732,7 @@ var InferenceToModelMessageRule = class {
725
732
  this.specification = new InferenceProducedModelMessageSpecification();
726
733
  }
727
734
  createTransition(execution) {
728
- if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
735
+ if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
729
736
  throw new Error("Expected inference model-message output");
730
737
  }
731
738
  const modelMessageItem = execution.output.items.find(
@@ -753,7 +760,7 @@ var FunctionCallToInferenceRule = class {
753
760
  const previousRequest = execution.input.inferenceInput;
754
761
  previousRequest.context.addContextItems([execution.input.call, execution.output.item]);
755
762
  return {
756
- nextStateId: "inference",
763
+ nextStateId: previousRequest.streaming ? "inference_streaming" : "inference",
757
764
  input: {
758
765
  ...previousRequest
759
766
  }
@@ -781,20 +788,19 @@ var EventPublisherLoopVisitor = class {
781
788
  this.agentId = agentId;
782
789
  this.runtime = runtime;
783
790
  }
784
- visitMessageReceivedStarted(input) {
791
+ visitContextPreparationStarted(input) {
785
792
  console.log("Visiting message received started: ", input);
786
- this.publish("message_received.started", input);
793
+ this.publish("context_preparation.started", input);
787
794
  }
788
- visitMessageReceivedCompleted(output) {
795
+ visitContextPreparationCompleted(output) {
789
796
  console.log("Visiting message received completed: ", output);
790
- this.publish("message_received.completed", output);
797
+ this.publish("context_preparation.completed", output);
791
798
  }
792
799
  visitInferenceStarted(input) {
793
800
  console.log("Visiting inference started: ", input);
794
801
  this.publish("inference.started", input);
795
802
  }
796
803
  visitInferenceEvent(event) {
797
- console.log("Visiting inference event: ", event);
798
804
  this.publish("inference.stream", event);
799
805
  }
800
806
  visitInferenceCompleted(output) {
@@ -823,6 +829,33 @@ var EventPublisherLoopVisitor = class {
823
829
  }
824
830
  };
825
831
 
832
+ // src/application/states/inference-streaming.ts
833
+ var InferenceStreamingState = class {
834
+ constructor(inferenceRunner) {
835
+ this.inferenceRunner = inferenceRunner;
836
+ this.id = "inference_streaming";
837
+ }
838
+ async run(input, loopVisitor) {
839
+ loopVisitor.visitInferenceStarted(input);
840
+ let output = void 0;
841
+ for await (const event of this.inferenceRunner.stream(input)) {
842
+ loopVisitor.visitInferenceEvent(event);
843
+ if (event.type === "inference.output") {
844
+ output = event.payload;
845
+ }
846
+ }
847
+ if (!output) {
848
+ throw new Error("Inference output not found");
849
+ }
850
+ loopVisitor.visitInferenceCompleted(output);
851
+ return {
852
+ stateId: "inference_streaming",
853
+ input,
854
+ output
855
+ };
856
+ }
857
+ };
858
+
826
859
  // src/application/use-cases/run-loop.ts
827
860
  function createRunLoop(resolveRuntime) {
828
861
  return function runLoop(agentId, message, inferenceInput) {
@@ -831,15 +864,16 @@ function createRunLoop(resolveRuntime) {
831
864
  const functionCallRunner = runtime.getFunctionCallRunner();
832
865
  const transitionResolver = new TransitionResolver([
833
866
  // High-priority interception rules would go first.
834
- new MessageReceivedToInferenceRule(),
867
+ new ContextPreparationToInferenceRule(),
835
868
  new InferenceToFunctionCallRule(),
836
869
  new InferenceToModelMessageRule(),
837
870
  new FunctionCallToInferenceRule(),
838
871
  new ModelMessageToIdleRule()
839
872
  ]);
840
873
  const stateExecutor = new LoopStateExecutor(
841
- new MessageReceivedState(),
874
+ new ContextPreparationState(),
842
875
  new InferenceState(inferenceRunner),
876
+ new InferenceStreamingState(inferenceRunner),
843
877
  new FunctionCallState(functionCallRunner),
844
878
  new ModelMessageState()
845
879
  );
@@ -1019,10 +1053,17 @@ var AnthropicMessages = class {
1019
1053
  }
1020
1054
  async *stream(inferenceInput) {
1021
1055
  const request = this.endpointMapper.toRequest(inferenceInput);
1022
- const stream = await this.client.messages.create(request);
1056
+ const stream = this.client.messages.stream(request);
1023
1057
  for await (const event of stream) {
1024
1058
  yield event;
1025
1059
  }
1060
+ const output = this.endpointMapper.toResponse(await stream.finalMessage());
1061
+ yield {
1062
+ type: "inference.output",
1063
+ producerId: request.model,
1064
+ occurredAt: /* @__PURE__ */ new Date(),
1065
+ payload: output
1066
+ };
1026
1067
  }
1027
1068
  };
1028
1069
 
@@ -1193,10 +1234,24 @@ var OpenAIResponses = class {
1193
1234
  }
1194
1235
  async *stream(inferenceInput) {
1195
1236
  const request = this.endpointMapper.toRequest(inferenceInput);
1196
- const response = await this.client.responses.create(request);
1197
- for await (const event of response) {
1237
+ const stream = await this.client.responses.create({ ...request, stream: true });
1238
+ let completedResponse = void 0;
1239
+ for await (const event of stream) {
1240
+ if (event.type === "response.completed") {
1241
+ completedResponse = event.response;
1242
+ }
1198
1243
  yield event;
1199
1244
  }
1245
+ if (!completedResponse) {
1246
+ throw new Error("Stream ended without a completed response");
1247
+ }
1248
+ const output = this.endpointMapper.toResponse(completedResponse);
1249
+ yield {
1250
+ type: "inference.output",
1251
+ payload: output,
1252
+ occurredAt: /* @__PURE__ */ new Date(),
1253
+ producerId: inferenceInput.model
1254
+ };
1200
1255
  }
1201
1256
  };
1202
1257
 
@@ -1368,9 +1423,21 @@ var GeminiGenerateContent = class {
1368
1423
  async *stream(inferenceInput) {
1369
1424
  const request = this.endpointMapper.toRequest(inferenceInput);
1370
1425
  const stream = await this.client.models.generateContentStream(request);
1426
+ let lastEvent = void 0;
1371
1427
  for await (const chunk of stream) {
1428
+ lastEvent = chunk;
1372
1429
  yield chunk;
1373
1430
  }
1431
+ if (!lastEvent) {
1432
+ throw new Error("Last event not found");
1433
+ }
1434
+ const output = this.endpointMapper.toResponse(lastEvent);
1435
+ yield {
1436
+ type: "inference.output",
1437
+ producerId: request.model,
1438
+ occurredAt: /* @__PURE__ */ new Date(),
1439
+ payload: output
1440
+ };
1374
1441
  }
1375
1442
  };
1376
1443
 
@@ -1504,10 +1571,17 @@ var OpenAIChatCompletions = class {
1504
1571
  return this.endpointMapper.toResponse(response);
1505
1572
  }
1506
1573
  async *stream(inferenceInput) {
1507
- const stream = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
1508
- for await (const event of stream) {
1509
- yield event;
1574
+ const stream = this.client.chat.completions.stream(this.buildRequest(inferenceInput));
1575
+ for await (const chunk of stream) {
1576
+ yield chunk;
1510
1577
  }
1578
+ const output = this.endpointMapper.toResponse(await stream.finalChatCompletion());
1579
+ yield {
1580
+ type: "inference.output",
1581
+ payload: output,
1582
+ occurredAt: /* @__PURE__ */ new Date(),
1583
+ producerId: inferenceInput.model
1584
+ };
1511
1585
  }
1512
1586
  };
1513
1587
 
@@ -1931,13 +2005,13 @@ var DefaultInferenceRunner = class {
1931
2005
  this.requestValidator.validate(input, generativeModel.specification);
1932
2006
  return await generativeModel.endpoint.infer(input);
1933
2007
  }
1934
- async streamInference(input) {
2008
+ async *stream(input) {
1935
2009
  const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
1936
2010
  if (!generativeModel) {
1937
2011
  throw new Error(`Unsupported model: ${input.model}`);
1938
2012
  }
1939
2013
  this.requestValidator.validate(input, generativeModel.specification);
1940
- return await generativeModel.endpoint.stream(input);
2014
+ yield* generativeModel.endpoint.stream(input);
1941
2015
  }
1942
2016
  };
1943
2017
 
@@ -1953,6 +2027,18 @@ var DefaultFunctionCallRunner = class {
1953
2027
  }
1954
2028
  };
1955
2029
 
2030
+ // src/application/use-cases/send-event.ts
2031
+ function createSendEvent(resolveRuntime) {
2032
+ return function sendEvent(event, senderId) {
2033
+ const runtime = resolveRuntime();
2034
+ const participant = runtime.getParticipant(senderId);
2035
+ if (!participant) {
2036
+ throw new Error(`Participant ${senderId} not found`);
2037
+ }
2038
+ runtime.publish(event);
2039
+ };
2040
+ }
2041
+
1956
2042
  // src/define-runtime.ts
1957
2043
  function defineRuntime() {
1958
2044
  let runtime = null;
@@ -1989,6 +2075,7 @@ function defineRuntime() {
1989
2075
  const join = createJoin(resolveRuntime);
1990
2076
  const leave = createLeave(resolveRuntime);
1991
2077
  const sendMessage = createSendMessage(resolveRuntime);
2078
+ const sendEvent = createSendEvent(resolveRuntime);
1992
2079
  const runLoop = createRunLoop(resolveRuntime);
1993
2080
  return {
1994
2081
  initializeRuntime,
@@ -1997,6 +2084,7 @@ function defineRuntime() {
1997
2084
  join,
1998
2085
  leave,
1999
2086
  sendMessage,
2087
+ sendEvent,
2000
2088
  runLoop
2001
2089
  };
2002
2090
  }