@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 +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +109 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +109 -35
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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: "
|
|
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(
|
|
559
|
-
this.
|
|
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 "
|
|
567
|
-
return await this.
|
|
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/
|
|
611
|
-
var
|
|
613
|
+
// src/application/states/context-preparation.ts
|
|
614
|
+
var ContextPreparationState = class {
|
|
612
615
|
constructor() {
|
|
613
|
-
this.id = "
|
|
616
|
+
this.id = "context_preparation";
|
|
614
617
|
}
|
|
615
618
|
async run(input, loopVisitor) {
|
|
616
|
-
loopVisitor.
|
|
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.
|
|
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 === "
|
|
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
|
|
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
|
|
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
|
|
694
|
+
var ContextPreparationToInferenceRule = class {
|
|
689
695
|
constructor() {
|
|
690
696
|
this.specification = new MessageReceivedSpecification();
|
|
691
697
|
}
|
|
692
698
|
createTransition(execution) {
|
|
693
|
-
if (execution.stateId !== "
|
|
694
|
-
throw new Error("Expected
|
|
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
|
|
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
|
-
|
|
791
|
+
visitContextPreparationStarted(input) {
|
|
785
792
|
console.log("Visiting message received started: ", input);
|
|
786
|
-
this.publish("
|
|
793
|
+
this.publish("context_preparation.started", input);
|
|
787
794
|
}
|
|
788
|
-
|
|
795
|
+
visitContextPreparationCompleted(output) {
|
|
789
796
|
console.log("Visiting message received completed: ", output);
|
|
790
|
-
this.publish("
|
|
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
|
|
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
|
|
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 =
|
|
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
|
|
1197
|
-
|
|
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 =
|
|
1508
|
-
for await (const
|
|
1509
|
-
yield
|
|
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
|
|
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
|
-
|
|
2014
|
+
yield* generativeModel.endpoint.stream(input);
|
|
1941
2015
|
}
|
|
1942
2016
|
};
|
|
1943
2017
|
|