@mozaik-ai/core 3.1.4 → 3.3.0
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/README.md +14 -14
- package/dist/index.d.mts +39 -40
- package/dist/index.d.ts +39 -40
- package/dist/index.js +94 -72
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +91 -69
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// src/domain/model-context/context.ts
|
|
2
|
-
var
|
|
1
|
+
// src/domain/model-context/model-context.ts
|
|
2
|
+
var ModelContext = class _ModelContext {
|
|
3
3
|
constructor(id, projectId, items) {
|
|
4
4
|
this.projectId = projectId;
|
|
5
5
|
this.id = id;
|
|
6
6
|
this.items = items;
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
addContextItem(item) {
|
|
9
9
|
this.items.push(item);
|
|
10
10
|
return this;
|
|
11
11
|
}
|
|
@@ -22,12 +22,18 @@ var Context = class _Context {
|
|
|
22
22
|
getItems() {
|
|
23
23
|
return this.items;
|
|
24
24
|
}
|
|
25
|
+
getLastItem() {
|
|
26
|
+
if (this.items.length === 0) {
|
|
27
|
+
throw new Error("No items in context");
|
|
28
|
+
}
|
|
29
|
+
return this.items[this.items.length - 1];
|
|
30
|
+
}
|
|
25
31
|
static create(projectId) {
|
|
26
32
|
const id = crypto.randomUUID();
|
|
27
|
-
return new
|
|
33
|
+
return new _ModelContext(id, projectId, []);
|
|
28
34
|
}
|
|
29
35
|
static rehydrate(data) {
|
|
30
|
-
return new
|
|
36
|
+
return new _ModelContext(data.id, data.projectId, data.items);
|
|
31
37
|
}
|
|
32
38
|
toJSON() {
|
|
33
39
|
return this.items.map((item) => item.toJSON());
|
|
@@ -441,19 +447,19 @@ var Gpt54Mini = class {
|
|
|
441
447
|
}
|
|
442
448
|
};
|
|
443
449
|
|
|
444
|
-
// src/domain/
|
|
445
|
-
var
|
|
450
|
+
// src/domain/specification/specification/specification.ts
|
|
451
|
+
var BaseSpecification = class {
|
|
446
452
|
and(other) {
|
|
447
|
-
return new
|
|
453
|
+
return new AndSpecification(this, other);
|
|
448
454
|
}
|
|
449
455
|
or(other) {
|
|
450
|
-
return new
|
|
456
|
+
return new OrSpecification(this, other);
|
|
451
457
|
}
|
|
452
458
|
not() {
|
|
453
|
-
return new
|
|
459
|
+
return new NotSpecification(this);
|
|
454
460
|
}
|
|
455
461
|
};
|
|
456
|
-
var
|
|
462
|
+
var AndSpecification = class extends BaseSpecification {
|
|
457
463
|
constructor(left, right) {
|
|
458
464
|
super();
|
|
459
465
|
this.left = left;
|
|
@@ -463,16 +469,16 @@ var AndCondition = class extends BaseCondition {
|
|
|
463
469
|
return this.left.isSatisfiedBy(candidate) && this.right.isSatisfiedBy(candidate);
|
|
464
470
|
}
|
|
465
471
|
};
|
|
466
|
-
var
|
|
467
|
-
constructor(
|
|
472
|
+
var NotSpecification = class extends BaseSpecification {
|
|
473
|
+
constructor(specification) {
|
|
468
474
|
super();
|
|
469
|
-
this.
|
|
475
|
+
this.specification = specification;
|
|
470
476
|
}
|
|
471
477
|
isSatisfiedBy(candidate) {
|
|
472
|
-
return !this.
|
|
478
|
+
return !this.specification.isSatisfiedBy(candidate);
|
|
473
479
|
}
|
|
474
480
|
};
|
|
475
|
-
var
|
|
481
|
+
var OrSpecification = class extends BaseSpecification {
|
|
476
482
|
constructor(left, right) {
|
|
477
483
|
super();
|
|
478
484
|
this.left = left;
|
|
@@ -483,34 +489,34 @@ var OrCondition = class extends BaseCondition {
|
|
|
483
489
|
}
|
|
484
490
|
};
|
|
485
491
|
|
|
486
|
-
// src/domain/
|
|
492
|
+
// src/domain/specification/rule/rule.ts
|
|
487
493
|
var If = class {
|
|
488
|
-
constructor({
|
|
489
|
-
this.
|
|
494
|
+
constructor({ specification, action }) {
|
|
495
|
+
this.specification = specification;
|
|
490
496
|
this.action = action;
|
|
491
497
|
}
|
|
492
498
|
async apply(candidate) {
|
|
493
|
-
if (this.
|
|
499
|
+
if (this.specification.isSatisfiedBy(candidate)) {
|
|
494
500
|
return await this.action.apply(candidate);
|
|
495
501
|
}
|
|
496
502
|
return candidate;
|
|
497
503
|
}
|
|
498
504
|
};
|
|
499
505
|
var Loop = class {
|
|
500
|
-
constructor({
|
|
501
|
-
this.
|
|
506
|
+
constructor({ specification, action }) {
|
|
507
|
+
this.specification = specification;
|
|
502
508
|
this.action = action;
|
|
503
509
|
}
|
|
504
510
|
async apply(candidate) {
|
|
505
|
-
while (this.
|
|
511
|
+
while (this.specification.isSatisfiedBy(candidate)) {
|
|
506
512
|
candidate = await this.action.apply(candidate);
|
|
507
513
|
}
|
|
508
514
|
return candidate;
|
|
509
515
|
}
|
|
510
516
|
};
|
|
511
517
|
|
|
512
|
-
// src/infrastructure/repository/in-memory-context-repository.ts
|
|
513
|
-
var
|
|
518
|
+
// src/infrastructure/repository/in-memory-model-context-repository.ts
|
|
519
|
+
var InMemoryModelContextRepository = class {
|
|
514
520
|
constructor() {
|
|
515
521
|
this.store = /* @__PURE__ */ new Map();
|
|
516
522
|
this.projectIndex = /* @__PURE__ */ new Map();
|
|
@@ -543,7 +549,7 @@ var InMemoryContextRepository = class {
|
|
|
543
549
|
return [...ids].map((id) => this.store.get(id)).filter((c) => Boolean(c)).map((c) => this.clone(c));
|
|
544
550
|
}
|
|
545
551
|
clone(context) {
|
|
546
|
-
return
|
|
552
|
+
return ModelContext.rehydrate({
|
|
547
553
|
id: context.id,
|
|
548
554
|
projectId: context.projectId,
|
|
549
555
|
items: [...context.getItems()]
|
|
@@ -608,12 +614,33 @@ var Fail = class {
|
|
|
608
614
|
}
|
|
609
615
|
};
|
|
610
616
|
|
|
617
|
+
// src/domain/model-context/specifications/function-call-requested.ts
|
|
618
|
+
var FunctionCallRequestedSpecification = class extends BaseSpecification {
|
|
619
|
+
isSatisfiedBy(context) {
|
|
620
|
+
if (context.getItems().length === 0) {
|
|
621
|
+
return false;
|
|
622
|
+
}
|
|
623
|
+
const lastItem = context.getItems()[context.getItems().length - 1];
|
|
624
|
+
return lastItem instanceof FunctionCall;
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
// src/domain/model-context/specifications/function-call-output-present.ts
|
|
629
|
+
var FunctionCallOutputPresentSpecification = class extends BaseSpecification {
|
|
630
|
+
isSatisfiedBy(context) {
|
|
631
|
+
const lasteItem = context.getItems()[context.getItems().length - 1];
|
|
632
|
+
return lasteItem instanceof FunctionCallOutput;
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
|
|
611
636
|
// src/domain/agent-loop/state/function-call.ts
|
|
612
637
|
var FunctionCallState = class {
|
|
613
638
|
constructor() {
|
|
614
639
|
this.id = "FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */;
|
|
615
640
|
this.beforeHookId = "BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */;
|
|
616
641
|
this.afterHookId = "AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */;
|
|
642
|
+
this.functionCallRequestedSpec = new FunctionCallRequestedSpecification();
|
|
643
|
+
this.functionCallOutputPresentSpec = new FunctionCallOutputPresentSpecification();
|
|
617
644
|
}
|
|
618
645
|
getDetails() {
|
|
619
646
|
return {
|
|
@@ -623,15 +650,12 @@ var FunctionCallState = class {
|
|
|
623
650
|
};
|
|
624
651
|
}
|
|
625
652
|
validateEntry(runtime) {
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
if (!functionCall || !(functionCall instanceof FunctionCall)) {
|
|
629
|
-
throw new Error("Function call not found");
|
|
653
|
+
if (!this.functionCallRequestedSpec.isSatisfiedBy(runtime.context)) {
|
|
654
|
+
throw new Error("Function call requested not found");
|
|
630
655
|
}
|
|
631
656
|
}
|
|
632
657
|
next(runtime) {
|
|
633
|
-
|
|
634
|
-
if (!functionCallOutput) {
|
|
658
|
+
if (!this.functionCallOutputPresentSpec.isSatisfiedBy(runtime.context)) {
|
|
635
659
|
return new Fail("Function call output not found");
|
|
636
660
|
}
|
|
637
661
|
return new GoTo("INFERENCE_PENDING" /* INFERENCE_PENDING */);
|
|
@@ -649,12 +673,24 @@ var Complete = class {
|
|
|
649
673
|
}
|
|
650
674
|
};
|
|
651
675
|
|
|
676
|
+
// src/domain/model-context/specifications/model-responded.ts
|
|
677
|
+
var ModelRespondedSpecification = class extends BaseSpecification {
|
|
678
|
+
isSatisfiedBy(context) {
|
|
679
|
+
if (context.getItems().length === 0) {
|
|
680
|
+
return false;
|
|
681
|
+
}
|
|
682
|
+
const lastItem = context.getItems()[context.getItems().length - 1];
|
|
683
|
+
return lastItem instanceof ModelMessage && lastItem.role === "assistant";
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
|
|
652
687
|
// src/domain/agent-loop/state/model-message.ts
|
|
653
688
|
var ModelMessageState = class {
|
|
654
689
|
constructor() {
|
|
655
690
|
this.id = "MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */;
|
|
656
691
|
this.beforeHookId = "BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */;
|
|
657
692
|
this.afterHookId = "AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */;
|
|
693
|
+
this.modelResponded = new ModelRespondedSpecification();
|
|
658
694
|
}
|
|
659
695
|
getDetails() {
|
|
660
696
|
return {
|
|
@@ -664,15 +700,16 @@ var ModelMessageState = class {
|
|
|
664
700
|
};
|
|
665
701
|
}
|
|
666
702
|
validateEntry(runtime) {
|
|
667
|
-
if (!runtime.
|
|
668
|
-
throw new Error("Model message not
|
|
703
|
+
if (!this.modelResponded.isSatisfiedBy(runtime.context)) {
|
|
704
|
+
throw new Error("Model message is not present in the context");
|
|
669
705
|
}
|
|
670
706
|
}
|
|
671
707
|
next(runtime) {
|
|
672
|
-
if (!runtime.
|
|
673
|
-
return new Fail("Model message not
|
|
708
|
+
if (!this.modelResponded.isSatisfiedBy(runtime.context)) {
|
|
709
|
+
return new Fail("Model message is not present in the context");
|
|
674
710
|
}
|
|
675
|
-
|
|
711
|
+
const lastItem = runtime.context.getLastItem();
|
|
712
|
+
return new Complete(lastItem.content.text);
|
|
676
713
|
}
|
|
677
714
|
};
|
|
678
715
|
|
|
@@ -682,6 +719,8 @@ var InferencePendingState = class {
|
|
|
682
719
|
this.id = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
|
|
683
720
|
this.beforeHookId = "BEFORE_INFERENCE" /* BEFORE_INFERENCE */;
|
|
684
721
|
this.afterHookId = "AFTER_INFERENCE" /* AFTER_INFERENCE */;
|
|
722
|
+
this.functionCallRequested = new FunctionCallRequestedSpecification();
|
|
723
|
+
this.modelResponded = new ModelRespondedSpecification();
|
|
685
724
|
}
|
|
686
725
|
getDetails() {
|
|
687
726
|
return {
|
|
@@ -691,25 +730,11 @@ var InferencePendingState = class {
|
|
|
691
730
|
};
|
|
692
731
|
}
|
|
693
732
|
validateEntry(runtime) {
|
|
694
|
-
const inferenceRequest = runtime.inferenceRequest;
|
|
695
|
-
if (!inferenceRequest) {
|
|
696
|
-
throw new Error("Inference request not found");
|
|
697
|
-
}
|
|
698
733
|
}
|
|
699
734
|
next(runtime) {
|
|
700
|
-
|
|
701
|
-
if (!inferenceResponse) {
|
|
702
|
-
return new Fail("Inference response not found");
|
|
703
|
-
}
|
|
704
|
-
const contextItems = inferenceResponse.contextItems;
|
|
705
|
-
if (contextItems.length === 0) {
|
|
706
|
-
return new Fail("No context items found");
|
|
707
|
-
}
|
|
708
|
-
const lastItem = contextItems[contextItems.length - 1];
|
|
709
|
-
if (lastItem instanceof FunctionCall) {
|
|
735
|
+
if (this.functionCallRequested.isSatisfiedBy(runtime.context)) {
|
|
710
736
|
return new GoTo("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */);
|
|
711
|
-
}
|
|
712
|
-
if (lastItem instanceof ModelMessage) {
|
|
737
|
+
} else if (this.modelResponded.isSatisfiedBy(runtime.context)) {
|
|
713
738
|
return new GoTo("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */);
|
|
714
739
|
}
|
|
715
740
|
return new Fail("Invalid response type");
|
|
@@ -797,27 +822,24 @@ var AgentRuntime = class {
|
|
|
797
822
|
this.stateHandlerRegistry.registerHandler("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, this.onModelMessageReceived);
|
|
798
823
|
this.hooksRegistry.registerHandler("ON_ERROR" /* ON_ERROR */, this.onError);
|
|
799
824
|
}
|
|
800
|
-
async onInferencePending(
|
|
825
|
+
async onInferencePending(runtimeContext) {
|
|
801
826
|
const openaiResponses = new OpenAIResponses();
|
|
802
|
-
const inferenceRequest = new InferenceRequest(
|
|
827
|
+
const inferenceRequest = new InferenceRequest(runtimeContext.model, runtimeContext.context);
|
|
803
828
|
const inferenceResponse = await openaiResponses.infer(inferenceRequest);
|
|
804
|
-
|
|
805
|
-
|
|
829
|
+
runtimeContext.inferenceResponse = inferenceResponse;
|
|
830
|
+
runtimeContext.context.applyModelOutput(inferenceResponse.contextItems);
|
|
806
831
|
return Promise.resolve();
|
|
807
832
|
}
|
|
808
|
-
async onFunctionCallPending(
|
|
809
|
-
|
|
810
|
-
const
|
|
811
|
-
if (!functionCall || !(functionCall instanceof FunctionCall)) {
|
|
812
|
-
throw new Error("Function call not found");
|
|
813
|
-
}
|
|
814
|
-
const tool = context.model.getTools().find((tool2) => tool2.name === functionCall.name);
|
|
833
|
+
async onFunctionCallPending(runtimeContext) {
|
|
834
|
+
const functionCall = runtimeContext.context.getLastItem();
|
|
835
|
+
const tool = runtimeContext.model.getTools().find((tool2) => tool2.name === functionCall.name);
|
|
815
836
|
if (!tool) {
|
|
816
837
|
throw new Error("Function not found");
|
|
817
838
|
}
|
|
818
839
|
const functionCallOutput = await tool.invoke(functionCall.args);
|
|
819
|
-
context.
|
|
820
|
-
|
|
840
|
+
runtimeContext.context.addContextItem(
|
|
841
|
+
FunctionCallOutput.create(functionCall.callId, JSON.stringify(functionCallOutput))
|
|
842
|
+
);
|
|
821
843
|
return Promise.resolve();
|
|
822
844
|
}
|
|
823
845
|
async onError(context) {
|
|
@@ -962,8 +984,7 @@ export {
|
|
|
962
984
|
Agent,
|
|
963
985
|
AgentRuntime,
|
|
964
986
|
AgentSociety,
|
|
965
|
-
|
|
966
|
-
Context,
|
|
987
|
+
BaseSpecification,
|
|
967
988
|
ContextItem,
|
|
968
989
|
DeveloperMessage,
|
|
969
990
|
FunctionCall,
|
|
@@ -972,11 +993,12 @@ export {
|
|
|
972
993
|
Gpt54Mini,
|
|
973
994
|
Gpt54Nano,
|
|
974
995
|
If,
|
|
975
|
-
|
|
996
|
+
InMemoryModelContextRepository,
|
|
976
997
|
InferenceRequest,
|
|
977
998
|
InferenceResponse,
|
|
978
999
|
InputTokenDetails,
|
|
979
1000
|
Loop,
|
|
1001
|
+
ModelContext,
|
|
980
1002
|
ModelMessage,
|
|
981
1003
|
OpenAIResponses,
|
|
982
1004
|
OutputTokenDetails,
|