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

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
@@ -353,6 +353,9 @@ var Participant = class {
353
353
  getHandlers() {
354
354
  return this.handlers;
355
355
  }
356
+ setHandlers(handlers) {
357
+ this.handlers = handlers;
358
+ }
356
359
  };
357
360
 
358
361
  // src/domain/agentic-environment/participant/memory.ts
@@ -543,7 +546,7 @@ var AgentLoop = class {
543
546
  }
544
547
  async run(message) {
545
548
  let transition = {
546
- nextStateId: "message_received",
549
+ nextStateId: "context_preparation",
547
550
  input: message
548
551
  };
549
552
  while (transition.nextStateId !== "idle") {
@@ -555,18 +558,21 @@ var AgentLoop = class {
555
558
 
556
559
  // src/domain/agentic-environment/loop/state-executor.ts
557
560
  var LoopStateExecutor = class {
558
- constructor(messageReceivedState, inferenceState, functionCallState, modelMessageState) {
559
- this.messageReceivedState = messageReceivedState;
561
+ constructor(contextPreparationState, inferenceState, inferenceStreamingState, functionCallState, modelMessageState) {
562
+ this.contextPreparationState = contextPreparationState;
560
563
  this.inferenceState = inferenceState;
564
+ this.inferenceStreamingState = inferenceStreamingState;
561
565
  this.functionCallState = functionCallState;
562
566
  this.modelMessageState = modelMessageState;
563
567
  }
564
568
  async execute(transition, loopVisitor) {
565
569
  switch (transition.nextStateId) {
566
- case "message_received":
567
- return await this.messageReceivedState.run(transition.input, loopVisitor);
570
+ case "context_preparation":
571
+ return await this.contextPreparationState.run(transition.input, loopVisitor);
568
572
  case "inference":
569
573
  return await this.inferenceState.run(transition.input, loopVisitor);
574
+ case "inference_streaming":
575
+ return await this.inferenceStreamingState.run(transition.input, loopVisitor);
570
576
  case "function_call":
571
577
  return await this.functionCallState.run(transition.input, loopVisitor);
572
578
  case "model_message":
@@ -607,19 +613,19 @@ var FunctionCallState = class {
607
613
  }
608
614
  };
609
615
 
610
- // src/application/states/message-received.ts
611
- var MessageReceivedState = class {
616
+ // src/application/states/context-preparation.ts
617
+ var ContextPreparationState = class {
612
618
  constructor() {
613
- this.id = "message_received";
619
+ this.id = "context_preparation";
614
620
  }
615
621
  async run(input, loopVisitor) {
616
- loopVisitor.visitMessageReceivedStarted(input);
622
+ loopVisitor.visitContextPreparationStarted(input);
617
623
  const userMessage = UserMessageItem.create(input.content);
618
624
  const output = {
619
625
  ...input.input,
620
626
  context: input.input.context.addContextItems([userMessage])
621
627
  };
622
- loopVisitor.visitMessageReceivedCompleted(output);
628
+ loopVisitor.visitContextPreparationCompleted(output);
623
629
  return {
624
630
  stateId: this.id,
625
631
  input,
@@ -660,17 +666,20 @@ var TransitionResolver = class {
660
666
  // src/domain/agentic-environment/loop/transition-specification.ts
661
667
  var MessageReceivedSpecification = class {
662
668
  isSatisfiedBy(execution) {
663
- return execution.stateId === "message_received";
669
+ return execution.stateId === "context_preparation";
664
670
  }
665
671
  };
672
+ function isInferenceExecution(execution) {
673
+ return execution.stateId === "inference" || execution.stateId === "inference_streaming";
674
+ }
666
675
  var InferenceProducedFunctionCallSpecification = class {
667
676
  isSatisfiedBy(execution) {
668
- return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "function_call");
677
+ return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "function_call");
669
678
  }
670
679
  };
671
680
  var InferenceProducedModelMessageSpecification = class {
672
681
  isSatisfiedBy(execution) {
673
- return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
682
+ return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
674
683
  }
675
684
  };
676
685
  var FunctionCallCompletedSpecification = class {
@@ -685,16 +694,17 @@ var ModelMessageCompletedSpecification = class {
685
694
  };
686
695
 
687
696
  // src/domain/agentic-environment/loop/transition-rule.ts
688
- var MessageReceivedToInferenceRule = class {
697
+ var ContextPreparationToInferenceRule = class {
689
698
  constructor() {
690
699
  this.specification = new MessageReceivedSpecification();
691
700
  }
692
701
  createTransition(execution) {
693
- if (execution.stateId !== "message_received") {
694
- throw new Error("Expected message_received execution");
702
+ if (execution.stateId !== "context_preparation") {
703
+ throw new Error("Expected context_preparation execution");
695
704
  }
705
+ const nextStateId = execution.output.streaming ? "inference_streaming" : "inference";
696
706
  return {
697
- nextStateId: "inference",
707
+ nextStateId,
698
708
  input: execution.output
699
709
  };
700
710
  }
@@ -704,7 +714,7 @@ var InferenceToFunctionCallRule = class {
704
714
  this.specification = new InferenceProducedFunctionCallSpecification();
705
715
  }
706
716
  createTransition(execution) {
707
- if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "function_call")) {
717
+ if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "function_call")) {
708
718
  throw new Error("Expected inference function-call output");
709
719
  }
710
720
  const functionCallItem = execution.output.items.find((item) => item.type === "function_call");
@@ -725,7 +735,7 @@ var InferenceToModelMessageRule = class {
725
735
  this.specification = new InferenceProducedModelMessageSpecification();
726
736
  }
727
737
  createTransition(execution) {
728
- if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
738
+ if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
729
739
  throw new Error("Expected inference model-message output");
730
740
  }
731
741
  const modelMessageItem = execution.output.items.find(
@@ -753,7 +763,7 @@ var FunctionCallToInferenceRule = class {
753
763
  const previousRequest = execution.input.inferenceInput;
754
764
  previousRequest.context.addContextItems([execution.input.call, execution.output.item]);
755
765
  return {
756
- nextStateId: "inference",
766
+ nextStateId: previousRequest.streaming ? "inference_streaming" : "inference",
757
767
  input: {
758
768
  ...previousRequest
759
769
  }
@@ -781,20 +791,19 @@ var EventPublisherLoopVisitor = class {
781
791
  this.agentId = agentId;
782
792
  this.runtime = runtime;
783
793
  }
784
- visitMessageReceivedStarted(input) {
794
+ visitContextPreparationStarted(input) {
785
795
  console.log("Visiting message received started: ", input);
786
- this.publish("message_received.started", input);
796
+ this.publish("context_preparation.started", input);
787
797
  }
788
- visitMessageReceivedCompleted(output) {
798
+ visitContextPreparationCompleted(output) {
789
799
  console.log("Visiting message received completed: ", output);
790
- this.publish("message_received.completed", output);
800
+ this.publish("context_preparation.completed", output);
791
801
  }
792
802
  visitInferenceStarted(input) {
793
803
  console.log("Visiting inference started: ", input);
794
804
  this.publish("inference.started", input);
795
805
  }
796
806
  visitInferenceEvent(event) {
797
- console.log("Visiting inference event: ", event);
798
807
  this.publish("inference.stream", event);
799
808
  }
800
809
  visitInferenceCompleted(output) {
@@ -823,6 +832,33 @@ var EventPublisherLoopVisitor = class {
823
832
  }
824
833
  };
825
834
 
835
+ // src/application/states/inference-streaming.ts
836
+ var InferenceStreamingState = class {
837
+ constructor(inferenceRunner) {
838
+ this.inferenceRunner = inferenceRunner;
839
+ this.id = "inference_streaming";
840
+ }
841
+ async run(input, loopVisitor) {
842
+ loopVisitor.visitInferenceStarted(input);
843
+ let output = void 0;
844
+ for await (const event of this.inferenceRunner.stream(input)) {
845
+ loopVisitor.visitInferenceEvent(event);
846
+ if (event.type === "inference.output") {
847
+ output = event.payload;
848
+ }
849
+ }
850
+ if (!output) {
851
+ throw new Error("Inference output not found");
852
+ }
853
+ loopVisitor.visitInferenceCompleted(output);
854
+ return {
855
+ stateId: "inference_streaming",
856
+ input,
857
+ output
858
+ };
859
+ }
860
+ };
861
+
826
862
  // src/application/use-cases/run-loop.ts
827
863
  function createRunLoop(resolveRuntime) {
828
864
  return function runLoop(agentId, message, inferenceInput) {
@@ -831,15 +867,16 @@ function createRunLoop(resolveRuntime) {
831
867
  const functionCallRunner = runtime.getFunctionCallRunner();
832
868
  const transitionResolver = new TransitionResolver([
833
869
  // High-priority interception rules would go first.
834
- new MessageReceivedToInferenceRule(),
870
+ new ContextPreparationToInferenceRule(),
835
871
  new InferenceToFunctionCallRule(),
836
872
  new InferenceToModelMessageRule(),
837
873
  new FunctionCallToInferenceRule(),
838
874
  new ModelMessageToIdleRule()
839
875
  ]);
840
876
  const stateExecutor = new LoopStateExecutor(
841
- new MessageReceivedState(),
877
+ new ContextPreparationState(),
842
878
  new InferenceState(inferenceRunner),
879
+ new InferenceStreamingState(inferenceRunner),
843
880
  new FunctionCallState(functionCallRunner),
844
881
  new ModelMessageState()
845
882
  );
@@ -1010,7 +1047,14 @@ import Anthropic from "@anthropic-ai/sdk";
1010
1047
  var AnthropicMessages = class {
1011
1048
  constructor(endpointMapper = new AnthropicMessagesMapper(), config = {}) {
1012
1049
  this.endpointMapper = endpointMapper;
1013
- this.client = new Anthropic({ baseURL: config.baseURL, apiKey: config.apiKey });
1050
+ this.clientConfig = config;
1051
+ }
1052
+ get client() {
1053
+ var _a;
1054
+ return (_a = this._client) != null ? _a : this._client = new Anthropic({
1055
+ baseURL: this.clientConfig.baseURL,
1056
+ apiKey: this.clientConfig.apiKey
1057
+ });
1014
1058
  }
1015
1059
  async infer(inferenceInput) {
1016
1060
  const request = this.endpointMapper.toRequest(inferenceInput);
@@ -1019,10 +1063,17 @@ var AnthropicMessages = class {
1019
1063
  }
1020
1064
  async *stream(inferenceInput) {
1021
1065
  const request = this.endpointMapper.toRequest(inferenceInput);
1022
- const stream = await this.client.messages.create(request);
1066
+ const stream = this.client.messages.stream(request);
1023
1067
  for await (const event of stream) {
1024
1068
  yield event;
1025
1069
  }
1070
+ const output = this.endpointMapper.toResponse(await stream.finalMessage());
1071
+ yield {
1072
+ type: "inference.output",
1073
+ producerId: request.model,
1074
+ occurredAt: /* @__PURE__ */ new Date(),
1075
+ payload: output
1076
+ };
1026
1077
  }
1027
1078
  };
1028
1079
 
@@ -1184,7 +1235,10 @@ var OpenAIResponsesMapper = class {
1184
1235
  var OpenAIResponses = class {
1185
1236
  constructor(endpointMapper = new OpenAIResponsesMapper()) {
1186
1237
  this.endpointMapper = endpointMapper;
1187
- this.client = new OpenAI();
1238
+ }
1239
+ get client() {
1240
+ var _a;
1241
+ return (_a = this._client) != null ? _a : this._client = new OpenAI();
1188
1242
  }
1189
1243
  async infer(inferenceInput) {
1190
1244
  const request = this.endpointMapper.toRequest(inferenceInput);
@@ -1193,10 +1247,24 @@ var OpenAIResponses = class {
1193
1247
  }
1194
1248
  async *stream(inferenceInput) {
1195
1249
  const request = this.endpointMapper.toRequest(inferenceInput);
1196
- const response = await this.client.responses.create(request);
1197
- for await (const event of response) {
1250
+ const stream = await this.client.responses.create({ ...request, stream: true });
1251
+ let completedResponse = void 0;
1252
+ for await (const event of stream) {
1253
+ if (event.type === "response.completed") {
1254
+ completedResponse = event.response;
1255
+ }
1198
1256
  yield event;
1199
1257
  }
1258
+ if (!completedResponse) {
1259
+ throw new Error("Stream ended without a completed response");
1260
+ }
1261
+ const output = this.endpointMapper.toResponse(completedResponse);
1262
+ yield {
1263
+ type: "inference.output",
1264
+ payload: output,
1265
+ occurredAt: /* @__PURE__ */ new Date(),
1266
+ producerId: inferenceInput.model
1267
+ };
1200
1268
  }
1201
1269
  };
1202
1270
 
@@ -1353,11 +1421,14 @@ var GeminiGenerateContentMapper = class {
1353
1421
  // src/infrastructure/providers/gemini/endpoints/gemini-generate-content.ts
1354
1422
  var GeminiGenerateContent = class {
1355
1423
  constructor(endpointMapper = new GeminiGenerateContentMapper(), config = {}) {
1356
- var _a;
1357
1424
  this.endpointMapper = endpointMapper;
1358
- this.client = new GoogleGenAI({
1359
- apiKey: (_a = config.apiKey) != null ? _a : process.env.GEMINI_API_KEY,
1360
- ...config.baseURL ? { httpOptions: { baseUrl: config.baseURL } } : {}
1425
+ this.clientConfig = config;
1426
+ }
1427
+ get client() {
1428
+ var _a, _b;
1429
+ return (_b = this._client) != null ? _b : this._client = new GoogleGenAI({
1430
+ apiKey: (_a = this.clientConfig.apiKey) != null ? _a : process.env.GEMINI_API_KEY,
1431
+ ...this.clientConfig.baseURL ? { httpOptions: { baseUrl: this.clientConfig.baseURL } } : {}
1361
1432
  });
1362
1433
  }
1363
1434
  async infer(inferenceInput) {
@@ -1368,9 +1439,21 @@ var GeminiGenerateContent = class {
1368
1439
  async *stream(inferenceInput) {
1369
1440
  const request = this.endpointMapper.toRequest(inferenceInput);
1370
1441
  const stream = await this.client.models.generateContentStream(request);
1442
+ let lastEvent = void 0;
1371
1443
  for await (const chunk of stream) {
1444
+ lastEvent = chunk;
1372
1445
  yield chunk;
1373
1446
  }
1447
+ if (!lastEvent) {
1448
+ throw new Error("Last event not found");
1449
+ }
1450
+ const output = this.endpointMapper.toResponse(lastEvent);
1451
+ yield {
1452
+ type: "inference.output",
1453
+ producerId: request.model,
1454
+ occurredAt: /* @__PURE__ */ new Date(),
1455
+ payload: output
1456
+ };
1374
1457
  }
1375
1458
  };
1376
1459
 
@@ -1487,12 +1570,16 @@ var OpenAIChatCompletions = class {
1487
1570
  constructor(endpointMapper = new OpenAIChatCompletionsMapper(), config = {}) {
1488
1571
  var _a;
1489
1572
  this.endpointMapper = endpointMapper;
1490
- this.client = new OpenAI2({
1491
- baseURL: config.baseURL,
1492
- apiKey: config.apiKey
1493
- });
1573
+ this.clientConfig = config;
1494
1574
  this.extraBody = (_a = config.extraBody) != null ? _a : {};
1495
1575
  }
1576
+ get client() {
1577
+ var _a;
1578
+ return (_a = this._client) != null ? _a : this._client = new OpenAI2({
1579
+ baseURL: this.clientConfig.baseURL,
1580
+ apiKey: this.clientConfig.apiKey
1581
+ });
1582
+ }
1496
1583
  buildRequest(inferenceInput) {
1497
1584
  return {
1498
1585
  ...this.extraBody,
@@ -1504,10 +1591,17 @@ var OpenAIChatCompletions = class {
1504
1591
  return this.endpointMapper.toResponse(response);
1505
1592
  }
1506
1593
  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;
1594
+ const stream = this.client.chat.completions.stream(this.buildRequest(inferenceInput));
1595
+ for await (const chunk of stream) {
1596
+ yield chunk;
1510
1597
  }
1598
+ const output = this.endpointMapper.toResponse(await stream.finalChatCompletion());
1599
+ yield {
1600
+ type: "inference.output",
1601
+ payload: output,
1602
+ occurredAt: /* @__PURE__ */ new Date(),
1603
+ producerId: inferenceInput.model
1604
+ };
1511
1605
  }
1512
1606
  };
1513
1607
 
@@ -1931,13 +2025,13 @@ var DefaultInferenceRunner = class {
1931
2025
  this.requestValidator.validate(input, generativeModel.specification);
1932
2026
  return await generativeModel.endpoint.infer(input);
1933
2027
  }
1934
- async streamInference(input) {
2028
+ async *stream(input) {
1935
2029
  const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
1936
2030
  if (!generativeModel) {
1937
2031
  throw new Error(`Unsupported model: ${input.model}`);
1938
2032
  }
1939
2033
  this.requestValidator.validate(input, generativeModel.specification);
1940
- return await generativeModel.endpoint.stream(input);
2034
+ yield* generativeModel.endpoint.stream(input);
1941
2035
  }
1942
2036
  };
1943
2037
 
@@ -2014,13 +2108,57 @@ function defineRuntime() {
2014
2108
  runLoop
2015
2109
  };
2016
2110
  }
2111
+
2112
+ // src/domain/agentic-environment/situation/situation-specification.ts
2113
+ var SituationSpecification = class {
2114
+ and(other) {
2115
+ return new AndSituationSpecification(this, other);
2116
+ }
2117
+ or(other) {
2118
+ return new OrSituationSpecification(this, other);
2119
+ }
2120
+ not() {
2121
+ return new NotSituationSpecification(this);
2122
+ }
2123
+ };
2124
+ var AndSituationSpecification = class extends SituationSpecification {
2125
+ constructor(left, right) {
2126
+ super();
2127
+ this.left = left;
2128
+ this.right = right;
2129
+ }
2130
+ isSatisfiedBy(situationContext) {
2131
+ return this.left.isSatisfiedBy(situationContext) && this.right.isSatisfiedBy(situationContext);
2132
+ }
2133
+ };
2134
+ var OrSituationSpecification = class extends SituationSpecification {
2135
+ constructor(left, right) {
2136
+ super();
2137
+ this.left = left;
2138
+ this.right = right;
2139
+ }
2140
+ isSatisfiedBy(situationContext) {
2141
+ return this.left.isSatisfiedBy(situationContext) || this.right.isSatisfiedBy(situationContext);
2142
+ }
2143
+ };
2144
+ var NotSituationSpecification = class extends SituationSpecification {
2145
+ constructor(rule) {
2146
+ super();
2147
+ this.rule = rule;
2148
+ }
2149
+ isSatisfiedBy(situationContext) {
2150
+ return !this.rule.isSatisfiedBy(situationContext);
2151
+ }
2152
+ };
2017
2153
  export {
2154
+ Agent,
2018
2155
  AnthropicMessages,
2019
2156
  ContextItem,
2020
2157
  DeveloperMessageItem,
2021
2158
  FunctionCallItem,
2022
2159
  FunctionCallOutputItem,
2023
2160
  GeminiGenerateContent,
2161
+ Human,
2024
2162
  InputTokenDetails,
2025
2163
  McpClient,
2026
2164
  McpToolRegistry,
@@ -2033,6 +2171,7 @@ export {
2033
2171
  ReasoningItem,
2034
2172
  RuntimeState,
2035
2173
  SemanticEvent,
2174
+ SituationSpecification,
2036
2175
  SystemMessageItem,
2037
2176
  TokenUsage,
2038
2177
  UserMessageItem,