@mozaik-ai/core 4.0.0-beta.3 → 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.d.mts CHANGED
@@ -158,6 +158,7 @@ type InferenceOutput = {
158
158
  };
159
159
  interface InferenceRunner {
160
160
  run(request: InferenceInput): Promise<InferenceOutput>;
161
+ stream(request: InferenceInput): AsyncGenerator<SemanticEvent>;
161
162
  }
162
163
 
163
164
  interface InferenceEndpointMapper {
package/dist/index.d.ts CHANGED
@@ -158,6 +158,7 @@ type InferenceOutput = {
158
158
  };
159
159
  interface InferenceRunner {
160
160
  run(request: InferenceInput): Promise<InferenceOutput>;
161
+ stream(request: InferenceInput): AsyncGenerator<SemanticEvent>;
161
162
  }
162
163
 
163
164
  interface InferenceEndpointMapper {
package/dist/index.js CHANGED
@@ -603,7 +603,7 @@ var AgentLoop = class {
603
603
  }
604
604
  async run(message) {
605
605
  let transition = {
606
- nextStateId: "message_received",
606
+ nextStateId: "context_preparation",
607
607
  input: message
608
608
  };
609
609
  while (transition.nextStateId !== "idle") {
@@ -615,18 +615,21 @@ var AgentLoop = class {
615
615
 
616
616
  // src/domain/agentic-environment/loop/state-executor.ts
617
617
  var LoopStateExecutor = class {
618
- constructor(messageReceivedState, inferenceState, functionCallState, modelMessageState) {
619
- this.messageReceivedState = messageReceivedState;
618
+ constructor(contextPreparationState, inferenceState, inferenceStreamingState, functionCallState, modelMessageState) {
619
+ this.contextPreparationState = contextPreparationState;
620
620
  this.inferenceState = inferenceState;
621
+ this.inferenceStreamingState = inferenceStreamingState;
621
622
  this.functionCallState = functionCallState;
622
623
  this.modelMessageState = modelMessageState;
623
624
  }
624
625
  async execute(transition, loopVisitor) {
625
626
  switch (transition.nextStateId) {
626
- case "message_received":
627
- return await this.messageReceivedState.run(transition.input, loopVisitor);
627
+ case "context_preparation":
628
+ return await this.contextPreparationState.run(transition.input, loopVisitor);
628
629
  case "inference":
629
630
  return await this.inferenceState.run(transition.input, loopVisitor);
631
+ case "inference_streaming":
632
+ return await this.inferenceStreamingState.run(transition.input, loopVisitor);
630
633
  case "function_call":
631
634
  return await this.functionCallState.run(transition.input, loopVisitor);
632
635
  case "model_message":
@@ -667,19 +670,19 @@ var FunctionCallState = class {
667
670
  }
668
671
  };
669
672
 
670
- // src/application/states/message-received.ts
671
- var MessageReceivedState = class {
673
+ // src/application/states/context-preparation.ts
674
+ var ContextPreparationState = class {
672
675
  constructor() {
673
- this.id = "message_received";
676
+ this.id = "context_preparation";
674
677
  }
675
678
  async run(input, loopVisitor) {
676
- loopVisitor.visitMessageReceivedStarted(input);
679
+ loopVisitor.visitContextPreparationStarted(input);
677
680
  const userMessage = UserMessageItem.create(input.content);
678
681
  const output = {
679
682
  ...input.input,
680
683
  context: input.input.context.addContextItems([userMessage])
681
684
  };
682
- loopVisitor.visitMessageReceivedCompleted(output);
685
+ loopVisitor.visitContextPreparationCompleted(output);
683
686
  return {
684
687
  stateId: this.id,
685
688
  input,
@@ -720,17 +723,20 @@ var TransitionResolver = class {
720
723
  // src/domain/agentic-environment/loop/transition-specification.ts
721
724
  var MessageReceivedSpecification = class {
722
725
  isSatisfiedBy(execution) {
723
- return execution.stateId === "message_received";
726
+ return execution.stateId === "context_preparation";
724
727
  }
725
728
  };
729
+ function isInferenceExecution(execution) {
730
+ return execution.stateId === "inference" || execution.stateId === "inference_streaming";
731
+ }
726
732
  var InferenceProducedFunctionCallSpecification = class {
727
733
  isSatisfiedBy(execution) {
728
- return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "function_call");
734
+ return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "function_call");
729
735
  }
730
736
  };
731
737
  var InferenceProducedModelMessageSpecification = class {
732
738
  isSatisfiedBy(execution) {
733
- return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
739
+ return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
734
740
  }
735
741
  };
736
742
  var FunctionCallCompletedSpecification = class {
@@ -745,16 +751,17 @@ var ModelMessageCompletedSpecification = class {
745
751
  };
746
752
 
747
753
  // src/domain/agentic-environment/loop/transition-rule.ts
748
- var MessageReceivedToInferenceRule = class {
754
+ var ContextPreparationToInferenceRule = class {
749
755
  constructor() {
750
756
  this.specification = new MessageReceivedSpecification();
751
757
  }
752
758
  createTransition(execution) {
753
- if (execution.stateId !== "message_received") {
754
- throw new Error("Expected message_received execution");
759
+ if (execution.stateId !== "context_preparation") {
760
+ throw new Error("Expected context_preparation execution");
755
761
  }
762
+ const nextStateId = execution.output.streaming ? "inference_streaming" : "inference";
756
763
  return {
757
- nextStateId: "inference",
764
+ nextStateId,
758
765
  input: execution.output
759
766
  };
760
767
  }
@@ -764,7 +771,7 @@ var InferenceToFunctionCallRule = class {
764
771
  this.specification = new InferenceProducedFunctionCallSpecification();
765
772
  }
766
773
  createTransition(execution) {
767
- if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "function_call")) {
774
+ if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "function_call")) {
768
775
  throw new Error("Expected inference function-call output");
769
776
  }
770
777
  const functionCallItem = execution.output.items.find((item) => item.type === "function_call");
@@ -785,7 +792,7 @@ var InferenceToModelMessageRule = class {
785
792
  this.specification = new InferenceProducedModelMessageSpecification();
786
793
  }
787
794
  createTransition(execution) {
788
- if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
795
+ if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
789
796
  throw new Error("Expected inference model-message output");
790
797
  }
791
798
  const modelMessageItem = execution.output.items.find(
@@ -813,7 +820,7 @@ var FunctionCallToInferenceRule = class {
813
820
  const previousRequest = execution.input.inferenceInput;
814
821
  previousRequest.context.addContextItems([execution.input.call, execution.output.item]);
815
822
  return {
816
- nextStateId: "inference",
823
+ nextStateId: previousRequest.streaming ? "inference_streaming" : "inference",
817
824
  input: {
818
825
  ...previousRequest
819
826
  }
@@ -841,20 +848,19 @@ var EventPublisherLoopVisitor = class {
841
848
  this.agentId = agentId;
842
849
  this.runtime = runtime;
843
850
  }
844
- visitMessageReceivedStarted(input) {
851
+ visitContextPreparationStarted(input) {
845
852
  console.log("Visiting message received started: ", input);
846
- this.publish("message_received.started", input);
853
+ this.publish("context_preparation.started", input);
847
854
  }
848
- visitMessageReceivedCompleted(output) {
855
+ visitContextPreparationCompleted(output) {
849
856
  console.log("Visiting message received completed: ", output);
850
- this.publish("message_received.completed", output);
857
+ this.publish("context_preparation.completed", output);
851
858
  }
852
859
  visitInferenceStarted(input) {
853
860
  console.log("Visiting inference started: ", input);
854
861
  this.publish("inference.started", input);
855
862
  }
856
863
  visitInferenceEvent(event) {
857
- console.log("Visiting inference event: ", event);
858
864
  this.publish("inference.stream", event);
859
865
  }
860
866
  visitInferenceCompleted(output) {
@@ -883,6 +889,33 @@ var EventPublisherLoopVisitor = class {
883
889
  }
884
890
  };
885
891
 
892
+ // src/application/states/inference-streaming.ts
893
+ var InferenceStreamingState = class {
894
+ constructor(inferenceRunner) {
895
+ this.inferenceRunner = inferenceRunner;
896
+ this.id = "inference_streaming";
897
+ }
898
+ async run(input, loopVisitor) {
899
+ loopVisitor.visitInferenceStarted(input);
900
+ let output = void 0;
901
+ for await (const event of this.inferenceRunner.stream(input)) {
902
+ loopVisitor.visitInferenceEvent(event);
903
+ if (event.type === "inference.output") {
904
+ output = event.payload;
905
+ }
906
+ }
907
+ if (!output) {
908
+ throw new Error("Inference output not found");
909
+ }
910
+ loopVisitor.visitInferenceCompleted(output);
911
+ return {
912
+ stateId: "inference_streaming",
913
+ input,
914
+ output
915
+ };
916
+ }
917
+ };
918
+
886
919
  // src/application/use-cases/run-loop.ts
887
920
  function createRunLoop(resolveRuntime) {
888
921
  return function runLoop(agentId, message, inferenceInput) {
@@ -891,15 +924,16 @@ function createRunLoop(resolveRuntime) {
891
924
  const functionCallRunner = runtime.getFunctionCallRunner();
892
925
  const transitionResolver = new TransitionResolver([
893
926
  // High-priority interception rules would go first.
894
- new MessageReceivedToInferenceRule(),
927
+ new ContextPreparationToInferenceRule(),
895
928
  new InferenceToFunctionCallRule(),
896
929
  new InferenceToModelMessageRule(),
897
930
  new FunctionCallToInferenceRule(),
898
931
  new ModelMessageToIdleRule()
899
932
  ]);
900
933
  const stateExecutor = new LoopStateExecutor(
901
- new MessageReceivedState(),
934
+ new ContextPreparationState(),
902
935
  new InferenceState(inferenceRunner),
936
+ new InferenceStreamingState(inferenceRunner),
903
937
  new FunctionCallState(functionCallRunner),
904
938
  new ModelMessageState()
905
939
  );
@@ -1079,10 +1113,17 @@ var AnthropicMessages = class {
1079
1113
  }
1080
1114
  async *stream(inferenceInput) {
1081
1115
  const request = this.endpointMapper.toRequest(inferenceInput);
1082
- const stream = await this.client.messages.create(request);
1116
+ const stream = this.client.messages.stream(request);
1083
1117
  for await (const event of stream) {
1084
1118
  yield event;
1085
1119
  }
1120
+ const output = this.endpointMapper.toResponse(await stream.finalMessage());
1121
+ yield {
1122
+ type: "inference.output",
1123
+ producerId: request.model,
1124
+ occurredAt: /* @__PURE__ */ new Date(),
1125
+ payload: output
1126
+ };
1086
1127
  }
1087
1128
  };
1088
1129
 
@@ -1253,10 +1294,24 @@ var OpenAIResponses = class {
1253
1294
  }
1254
1295
  async *stream(inferenceInput) {
1255
1296
  const request = this.endpointMapper.toRequest(inferenceInput);
1256
- const response = await this.client.responses.create(request);
1257
- for await (const event of response) {
1297
+ const stream = await this.client.responses.create({ ...request, stream: true });
1298
+ let completedResponse = void 0;
1299
+ for await (const event of stream) {
1300
+ if (event.type === "response.completed") {
1301
+ completedResponse = event.response;
1302
+ }
1258
1303
  yield event;
1259
1304
  }
1305
+ if (!completedResponse) {
1306
+ throw new Error("Stream ended without a completed response");
1307
+ }
1308
+ const output = this.endpointMapper.toResponse(completedResponse);
1309
+ yield {
1310
+ type: "inference.output",
1311
+ payload: output,
1312
+ occurredAt: /* @__PURE__ */ new Date(),
1313
+ producerId: inferenceInput.model
1314
+ };
1260
1315
  }
1261
1316
  };
1262
1317
 
@@ -1428,9 +1483,21 @@ var GeminiGenerateContent = class {
1428
1483
  async *stream(inferenceInput) {
1429
1484
  const request = this.endpointMapper.toRequest(inferenceInput);
1430
1485
  const stream = await this.client.models.generateContentStream(request);
1486
+ let lastEvent = void 0;
1431
1487
  for await (const chunk of stream) {
1488
+ lastEvent = chunk;
1432
1489
  yield chunk;
1433
1490
  }
1491
+ if (!lastEvent) {
1492
+ throw new Error("Last event not found");
1493
+ }
1494
+ const output = this.endpointMapper.toResponse(lastEvent);
1495
+ yield {
1496
+ type: "inference.output",
1497
+ producerId: request.model,
1498
+ occurredAt: /* @__PURE__ */ new Date(),
1499
+ payload: output
1500
+ };
1434
1501
  }
1435
1502
  };
1436
1503
 
@@ -1564,10 +1631,17 @@ var OpenAIChatCompletions = class {
1564
1631
  return this.endpointMapper.toResponse(response);
1565
1632
  }
1566
1633
  async *stream(inferenceInput) {
1567
- const stream = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
1568
- for await (const event of stream) {
1569
- yield event;
1634
+ const stream = this.client.chat.completions.stream(this.buildRequest(inferenceInput));
1635
+ for await (const chunk of stream) {
1636
+ yield chunk;
1570
1637
  }
1638
+ const output = this.endpointMapper.toResponse(await stream.finalChatCompletion());
1639
+ yield {
1640
+ type: "inference.output",
1641
+ payload: output,
1642
+ occurredAt: /* @__PURE__ */ new Date(),
1643
+ producerId: inferenceInput.model
1644
+ };
1571
1645
  }
1572
1646
  };
1573
1647
 
@@ -1991,13 +2065,13 @@ var DefaultInferenceRunner = class {
1991
2065
  this.requestValidator.validate(input, generativeModel.specification);
1992
2066
  return await generativeModel.endpoint.infer(input);
1993
2067
  }
1994
- async streamInference(input) {
2068
+ async *stream(input) {
1995
2069
  const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
1996
2070
  if (!generativeModel) {
1997
2071
  throw new Error(`Unsupported model: ${input.model}`);
1998
2072
  }
1999
2073
  this.requestValidator.validate(input, generativeModel.specification);
2000
- return await generativeModel.endpoint.stream(input);
2074
+ yield* generativeModel.endpoint.stream(input);
2001
2075
  }
2002
2076
  };
2003
2077