@mozaik-ai/core 3.5.0 → 3.6.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/dist/index.d.mts +48 -186
- package/dist/index.d.ts +48 -186
- package/dist/index.js +77 -587
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +75 -582
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -30,10 +30,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
Agent: () => Agent,
|
|
34
|
-
AgentSociety: () => AgentSociety,
|
|
35
33
|
AgenticEnvironment: () => AgenticEnvironment,
|
|
36
|
-
|
|
34
|
+
BaseAgentParticipant: () => BaseAgentParticipant,
|
|
35
|
+
BaseHumanParticipant: () => BaseHumanParticipant,
|
|
37
36
|
ContextItem: () => ContextItem,
|
|
38
37
|
DeveloperMessageItem: () => DeveloperMessageItem,
|
|
39
38
|
FunctionCallItem: () => FunctionCallItem,
|
|
@@ -41,12 +40,10 @@ __export(index_exports, {
|
|
|
41
40
|
Gpt54: () => Gpt54,
|
|
42
41
|
Gpt54Mini: () => Gpt54Mini,
|
|
43
42
|
Gpt54Nano: () => Gpt54Nano,
|
|
44
|
-
If: () => If,
|
|
45
43
|
InMemoryModelContextRepository: () => InMemoryModelContextRepository,
|
|
46
44
|
InferenceRequest: () => InferenceRequest,
|
|
47
45
|
InferenceResponse: () => InferenceResponse,
|
|
48
46
|
InputTokenDetails: () => InputTokenDetails,
|
|
49
|
-
Loop: () => Loop,
|
|
50
47
|
ModelContext: () => ModelContext,
|
|
51
48
|
ModelMessageItem: () => ModelMessageItem,
|
|
52
49
|
OpenAIResponses: () => OpenAIResponses,
|
|
@@ -508,74 +505,6 @@ var Gpt54Mini = class {
|
|
|
508
505
|
}
|
|
509
506
|
};
|
|
510
507
|
|
|
511
|
-
// src/domain/specification/specification/specification.ts
|
|
512
|
-
var BaseSpecification = class {
|
|
513
|
-
and(other) {
|
|
514
|
-
return new AndSpecification(this, other);
|
|
515
|
-
}
|
|
516
|
-
or(other) {
|
|
517
|
-
return new OrSpecification(this, other);
|
|
518
|
-
}
|
|
519
|
-
not() {
|
|
520
|
-
return new NotSpecification(this);
|
|
521
|
-
}
|
|
522
|
-
};
|
|
523
|
-
var AndSpecification = class extends BaseSpecification {
|
|
524
|
-
constructor(left, right) {
|
|
525
|
-
super();
|
|
526
|
-
this.left = left;
|
|
527
|
-
this.right = right;
|
|
528
|
-
}
|
|
529
|
-
isSatisfiedBy(candidate) {
|
|
530
|
-
return this.left.isSatisfiedBy(candidate) && this.right.isSatisfiedBy(candidate);
|
|
531
|
-
}
|
|
532
|
-
};
|
|
533
|
-
var NotSpecification = class extends BaseSpecification {
|
|
534
|
-
constructor(specification) {
|
|
535
|
-
super();
|
|
536
|
-
this.specification = specification;
|
|
537
|
-
}
|
|
538
|
-
isSatisfiedBy(candidate) {
|
|
539
|
-
return !this.specification.isSatisfiedBy(candidate);
|
|
540
|
-
}
|
|
541
|
-
};
|
|
542
|
-
var OrSpecification = class extends BaseSpecification {
|
|
543
|
-
constructor(left, right) {
|
|
544
|
-
super();
|
|
545
|
-
this.left = left;
|
|
546
|
-
this.right = right;
|
|
547
|
-
}
|
|
548
|
-
isSatisfiedBy(candidate) {
|
|
549
|
-
return this.left.isSatisfiedBy(candidate) || this.right.isSatisfiedBy(candidate);
|
|
550
|
-
}
|
|
551
|
-
};
|
|
552
|
-
|
|
553
|
-
// src/domain/specification/rule/rule.ts
|
|
554
|
-
var If = class {
|
|
555
|
-
constructor({ specification, action }) {
|
|
556
|
-
this.specification = specification;
|
|
557
|
-
this.action = action;
|
|
558
|
-
}
|
|
559
|
-
async apply(candidate) {
|
|
560
|
-
if (this.specification.isSatisfiedBy(candidate)) {
|
|
561
|
-
return await this.action.apply(candidate);
|
|
562
|
-
}
|
|
563
|
-
return candidate;
|
|
564
|
-
}
|
|
565
|
-
};
|
|
566
|
-
var Loop = class {
|
|
567
|
-
constructor({ specification, action }) {
|
|
568
|
-
this.specification = specification;
|
|
569
|
-
this.action = action;
|
|
570
|
-
}
|
|
571
|
-
async apply(candidate) {
|
|
572
|
-
while (this.specification.isSatisfiedBy(candidate)) {
|
|
573
|
-
candidate = await this.action.apply(candidate);
|
|
574
|
-
}
|
|
575
|
-
return candidate;
|
|
576
|
-
}
|
|
577
|
-
};
|
|
578
|
-
|
|
579
508
|
// src/infrastructure/repository/in-memory-model-context-repository.ts
|
|
580
509
|
var InMemoryModelContextRepository = class {
|
|
581
510
|
constructor() {
|
|
@@ -618,452 +547,6 @@ var InMemoryModelContextRepository = class {
|
|
|
618
547
|
}
|
|
619
548
|
};
|
|
620
549
|
|
|
621
|
-
// src/domain/agent-loop/execution.ts
|
|
622
|
-
var Execution = class _Execution {
|
|
623
|
-
constructor(executionId, currentStateId, previousStateId, status, stepCount, retryCounts, history) {
|
|
624
|
-
this.executionId = executionId;
|
|
625
|
-
this.currentStateId = currentStateId;
|
|
626
|
-
this.previousStateId = previousStateId;
|
|
627
|
-
this.status = status;
|
|
628
|
-
this.stepCount = stepCount;
|
|
629
|
-
this.retryCounts = retryCounts;
|
|
630
|
-
this.history = history;
|
|
631
|
-
}
|
|
632
|
-
isTerminal() {
|
|
633
|
-
return this.status == 1 /* COMPLETED */ || this.status == 2 /* FAILED */;
|
|
634
|
-
}
|
|
635
|
-
static create() {
|
|
636
|
-
const executionId = crypto.randomUUID();
|
|
637
|
-
const currentStateId = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
|
|
638
|
-
const previousStateId = null;
|
|
639
|
-
const status = 0 /* RUNNING */;
|
|
640
|
-
const stepCount = 0;
|
|
641
|
-
const retryCounts = /* @__PURE__ */ new Map();
|
|
642
|
-
const history = [];
|
|
643
|
-
return new _Execution(executionId, currentStateId, previousStateId, status, stepCount, retryCounts, history);
|
|
644
|
-
}
|
|
645
|
-
};
|
|
646
|
-
|
|
647
|
-
// src/domain/agent-loop/transition/go-to.ts
|
|
648
|
-
var GoTo = class {
|
|
649
|
-
constructor(next) {
|
|
650
|
-
this.next = next;
|
|
651
|
-
}
|
|
652
|
-
async apply(context) {
|
|
653
|
-
const execution = context.execution;
|
|
654
|
-
execution.previousStateId = execution.currentStateId;
|
|
655
|
-
execution.currentStateId = this.next;
|
|
656
|
-
execution.stepCount++;
|
|
657
|
-
execution.history.push({
|
|
658
|
-
fromState: execution.previousStateId,
|
|
659
|
-
toState: execution.currentStateId,
|
|
660
|
-
timestamp: /* @__PURE__ */ new Date(),
|
|
661
|
-
duration: 0,
|
|
662
|
-
error: null
|
|
663
|
-
});
|
|
664
|
-
}
|
|
665
|
-
};
|
|
666
|
-
|
|
667
|
-
// src/domain/agent-loop/transition/fail.ts
|
|
668
|
-
var Fail = class {
|
|
669
|
-
constructor(error) {
|
|
670
|
-
this.error = error;
|
|
671
|
-
}
|
|
672
|
-
async apply(context) {
|
|
673
|
-
const execution = context.execution;
|
|
674
|
-
execution.status = 2 /* FAILED */;
|
|
675
|
-
}
|
|
676
|
-
};
|
|
677
|
-
|
|
678
|
-
// src/domain/model-context/specifications/function-call-requested.ts
|
|
679
|
-
var FunctionCallRequestedSpec = class extends BaseSpecification {
|
|
680
|
-
isSatisfiedBy(context) {
|
|
681
|
-
if (context.getItems().length === 0) {
|
|
682
|
-
return false;
|
|
683
|
-
}
|
|
684
|
-
const lastItem = context.getLastItem();
|
|
685
|
-
return lastItem instanceof FunctionCallItem;
|
|
686
|
-
}
|
|
687
|
-
};
|
|
688
|
-
|
|
689
|
-
// src/domain/model-context/specifications/function-call-output-present.ts
|
|
690
|
-
var FunctionCallOutputPresentSpec = class extends BaseSpecification {
|
|
691
|
-
isSatisfiedBy(context) {
|
|
692
|
-
const lasteItem = context.getLastItem();
|
|
693
|
-
return lasteItem instanceof FunctionCallOutputItem;
|
|
694
|
-
}
|
|
695
|
-
};
|
|
696
|
-
|
|
697
|
-
// src/domain/agent-loop/state/function-call-pending.ts
|
|
698
|
-
var FunctionCallPendingState = class {
|
|
699
|
-
constructor() {
|
|
700
|
-
this.id = "FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */;
|
|
701
|
-
this.beforeHookId = "BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */;
|
|
702
|
-
this.afterHookId = "AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */;
|
|
703
|
-
this.functionCallRequestedSpec = new FunctionCallRequestedSpec();
|
|
704
|
-
this.functionCallOutputPresentSpec = new FunctionCallOutputPresentSpec();
|
|
705
|
-
}
|
|
706
|
-
getDetails() {
|
|
707
|
-
return {
|
|
708
|
-
before: this.beforeHookId,
|
|
709
|
-
stateId: this.id,
|
|
710
|
-
after: this.afterHookId
|
|
711
|
-
};
|
|
712
|
-
}
|
|
713
|
-
validateEntry(runtime) {
|
|
714
|
-
if (!this.functionCallRequestedSpec.isSatisfiedBy(runtime.context)) {
|
|
715
|
-
throw new Error("Function call requested not found");
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
next(runtime) {
|
|
719
|
-
if (!this.functionCallOutputPresentSpec.isSatisfiedBy(runtime.context)) {
|
|
720
|
-
return new Fail("Function call output not found");
|
|
721
|
-
}
|
|
722
|
-
return new GoTo("INFERENCE_PENDING" /* INFERENCE_PENDING */);
|
|
723
|
-
}
|
|
724
|
-
};
|
|
725
|
-
|
|
726
|
-
// src/domain/agent-loop/transition/complete.ts
|
|
727
|
-
var Complete = class {
|
|
728
|
-
constructor(result) {
|
|
729
|
-
this.result = result;
|
|
730
|
-
}
|
|
731
|
-
async apply(context) {
|
|
732
|
-
const execution = context.execution;
|
|
733
|
-
execution.status = 1 /* COMPLETED */;
|
|
734
|
-
}
|
|
735
|
-
};
|
|
736
|
-
|
|
737
|
-
// src/domain/model-context/specifications/model-responded.ts
|
|
738
|
-
var ModelRespondedSpec = class extends BaseSpecification {
|
|
739
|
-
isSatisfiedBy(context) {
|
|
740
|
-
if (context.getItems().length === 0) {
|
|
741
|
-
return false;
|
|
742
|
-
}
|
|
743
|
-
const lastItem = context.getLastItem();
|
|
744
|
-
return lastItem instanceof ModelMessageItem && lastItem.role === "assistant";
|
|
745
|
-
}
|
|
746
|
-
};
|
|
747
|
-
|
|
748
|
-
// src/domain/agent-loop/state/model-responded.ts
|
|
749
|
-
var ModelRespondedState = class {
|
|
750
|
-
constructor() {
|
|
751
|
-
this.id = "MODEL_RESPONDED" /* MODEL_RESPONDED */;
|
|
752
|
-
this.beforeHookId = "BEFORE_MODEL_RESPONDED" /* BEFORE_MODEL_RESPONDED */;
|
|
753
|
-
this.afterHookId = "AFTER_MODEL_RESPONDED" /* AFTER_MODEL_RESPONDED */;
|
|
754
|
-
this.modelRespondedSpec = new ModelRespondedSpec();
|
|
755
|
-
}
|
|
756
|
-
getDetails() {
|
|
757
|
-
return {
|
|
758
|
-
before: this.beforeHookId,
|
|
759
|
-
stateId: this.id,
|
|
760
|
-
after: this.afterHookId
|
|
761
|
-
};
|
|
762
|
-
}
|
|
763
|
-
validateEntry(runtime) {
|
|
764
|
-
if (!this.modelRespondedSpec.isSatisfiedBy(runtime.context)) {
|
|
765
|
-
throw new Error("Model message is not present in the context");
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
next(runtime) {
|
|
769
|
-
if (!this.modelRespondedSpec.isSatisfiedBy(runtime.context)) {
|
|
770
|
-
return new Fail("Model message is not present in the context");
|
|
771
|
-
}
|
|
772
|
-
const lastItem = runtime.context.getLastItem();
|
|
773
|
-
return new Complete(lastItem.content.text);
|
|
774
|
-
}
|
|
775
|
-
};
|
|
776
|
-
|
|
777
|
-
// src/domain/agent-loop/state/inference-pending.ts
|
|
778
|
-
var InferencePendingState = class {
|
|
779
|
-
constructor() {
|
|
780
|
-
this.id = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
|
|
781
|
-
this.beforeHookId = "BEFORE_INFERENCE" /* BEFORE_INFERENCE */;
|
|
782
|
-
this.afterHookId = "AFTER_INFERENCE" /* AFTER_INFERENCE */;
|
|
783
|
-
this.functionCallRequested = new FunctionCallRequestedSpec();
|
|
784
|
-
this.modelResponded = new ModelRespondedSpec();
|
|
785
|
-
}
|
|
786
|
-
getDetails() {
|
|
787
|
-
return {
|
|
788
|
-
before: this.beforeHookId,
|
|
789
|
-
stateId: this.id,
|
|
790
|
-
after: this.afterHookId
|
|
791
|
-
};
|
|
792
|
-
}
|
|
793
|
-
validateEntry(runtime) {
|
|
794
|
-
}
|
|
795
|
-
next(runtime) {
|
|
796
|
-
if (this.functionCallRequested.isSatisfiedBy(runtime.context)) {
|
|
797
|
-
return new GoTo("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */);
|
|
798
|
-
} else if (this.modelResponded.isSatisfiedBy(runtime.context)) {
|
|
799
|
-
return new GoTo("MODEL_RESPONDED" /* MODEL_RESPONDED */);
|
|
800
|
-
}
|
|
801
|
-
return new Fail("Invalid response type");
|
|
802
|
-
}
|
|
803
|
-
};
|
|
804
|
-
|
|
805
|
-
// src/domain/agent-loop/state/message-received.ts
|
|
806
|
-
var MessageReceivedState = class {
|
|
807
|
-
constructor() {
|
|
808
|
-
this.id = "MESSAGE_RECEIVED" /* MESSAGE_RECEIVED */;
|
|
809
|
-
this.beforeHookId = "BEFORE_MESSAGE_RECEIVED" /* BEFORE_MESSAGE_RECEIVED */;
|
|
810
|
-
this.afterHookId = "AFTER_MESSAGE_RECEIVED" /* AFTER_MESSAGE_RECEIVED */;
|
|
811
|
-
}
|
|
812
|
-
getDetails() {
|
|
813
|
-
return {
|
|
814
|
-
before: this.beforeHookId,
|
|
815
|
-
stateId: this.id,
|
|
816
|
-
after: this.afterHookId
|
|
817
|
-
};
|
|
818
|
-
}
|
|
819
|
-
validateEntry(context) {
|
|
820
|
-
}
|
|
821
|
-
next(context) {
|
|
822
|
-
return new GoTo("INFERENCE_PENDING" /* INFERENCE_PENDING */);
|
|
823
|
-
}
|
|
824
|
-
};
|
|
825
|
-
|
|
826
|
-
// src/domain/agent-loop/agent-loop.ts
|
|
827
|
-
var AgentLoop = class {
|
|
828
|
-
constructor() {
|
|
829
|
-
this.states = /* @__PURE__ */ new Map();
|
|
830
|
-
this.states.set("MESSAGE_RECEIVED" /* MESSAGE_RECEIVED */, new MessageReceivedState());
|
|
831
|
-
this.states.set("INFERENCE_PENDING" /* INFERENCE_PENDING */, new InferencePendingState());
|
|
832
|
-
this.states.set("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, new FunctionCallPendingState());
|
|
833
|
-
this.states.set("MODEL_RESPONDED" /* MODEL_RESPONDED */, new ModelRespondedState());
|
|
834
|
-
}
|
|
835
|
-
getState(runtime) {
|
|
836
|
-
const state = this.states.get(runtime.execution.currentStateId);
|
|
837
|
-
if (!state) {
|
|
838
|
-
throw new Error(`State ${runtime.execution.currentStateId} not found`);
|
|
839
|
-
}
|
|
840
|
-
return state;
|
|
841
|
-
}
|
|
842
|
-
};
|
|
843
|
-
|
|
844
|
-
// src/domain/agent-loop/hooks/hooks-registry.ts
|
|
845
|
-
var HooksRegistry = class {
|
|
846
|
-
constructor() {
|
|
847
|
-
this.handlers = /* @__PURE__ */ new Map();
|
|
848
|
-
}
|
|
849
|
-
getHandler(hookId) {
|
|
850
|
-
const handler = this.handlers.get(hookId);
|
|
851
|
-
if (!handler) {
|
|
852
|
-
throw new Error(`Hook handler for hook ${hookId} not found`);
|
|
853
|
-
}
|
|
854
|
-
return handler;
|
|
855
|
-
}
|
|
856
|
-
registerHandler(hookId, handler) {
|
|
857
|
-
this.handlers.set(hookId, handler);
|
|
858
|
-
}
|
|
859
|
-
unregisterHandler(hookId) {
|
|
860
|
-
this.handlers.delete(hookId);
|
|
861
|
-
}
|
|
862
|
-
};
|
|
863
|
-
|
|
864
|
-
// src/domain/agent-loop/state/state-registry.ts
|
|
865
|
-
var StateHandlerRegistry = class {
|
|
866
|
-
constructor() {
|
|
867
|
-
this.handlers = /* @__PURE__ */ new Map();
|
|
868
|
-
}
|
|
869
|
-
getHandler(stateId) {
|
|
870
|
-
const handler = this.handlers.get(stateId);
|
|
871
|
-
if (!handler) {
|
|
872
|
-
throw new Error(`Handler for state ${stateId} not found`);
|
|
873
|
-
}
|
|
874
|
-
return handler;
|
|
875
|
-
}
|
|
876
|
-
registerHandler(stateId, handler) {
|
|
877
|
-
this.handlers.set(stateId, handler);
|
|
878
|
-
}
|
|
879
|
-
unregisterHandler(stateId) {
|
|
880
|
-
this.handlers.delete(stateId);
|
|
881
|
-
}
|
|
882
|
-
};
|
|
883
|
-
|
|
884
|
-
// src/application/agent-runtime.ts
|
|
885
|
-
var AgentRuntime = class {
|
|
886
|
-
constructor() {
|
|
887
|
-
this.hooksRegistry = new HooksRegistry();
|
|
888
|
-
this.stateHandlerRegistry = new StateHandlerRegistry();
|
|
889
|
-
this.stateHandlerRegistry.registerHandler("MESSAGE_RECEIVED" /* MESSAGE_RECEIVED */, this.onMessageReceived);
|
|
890
|
-
this.stateHandlerRegistry.registerHandler("INFERENCE_PENDING" /* INFERENCE_PENDING */, this.onInferencePending);
|
|
891
|
-
this.stateHandlerRegistry.registerHandler("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, this.onFunctionCallPending);
|
|
892
|
-
this.stateHandlerRegistry.registerHandler("MODEL_RESPONDED" /* MODEL_RESPONDED */, this.onModelResponded);
|
|
893
|
-
this.hooksRegistry.registerHandler("ON_ERROR" /* ON_ERROR */, this.onError);
|
|
894
|
-
}
|
|
895
|
-
async onMessageReceived(runtimeContext) {
|
|
896
|
-
return Promise.resolve();
|
|
897
|
-
}
|
|
898
|
-
async onInferencePending(runtimeContext) {
|
|
899
|
-
const openaiResponses = new OpenAIResponses();
|
|
900
|
-
const inferenceRequest = new InferenceRequest(runtimeContext.model, runtimeContext.context);
|
|
901
|
-
const inferenceResponse = await openaiResponses.infer(inferenceRequest);
|
|
902
|
-
runtimeContext.inferenceResponse = inferenceResponse;
|
|
903
|
-
runtimeContext.context.applyModelOutput(inferenceResponse.contextItems);
|
|
904
|
-
return Promise.resolve();
|
|
905
|
-
}
|
|
906
|
-
async onFunctionCallPending(runtimeContext) {
|
|
907
|
-
const functionCall = runtimeContext.context.getLastItem();
|
|
908
|
-
const tool = runtimeContext.model.getTools().find((tool2) => tool2.name === functionCall.name);
|
|
909
|
-
if (!tool) {
|
|
910
|
-
throw new Error("Function not found");
|
|
911
|
-
}
|
|
912
|
-
const functionCallOutput = await tool.invoke(functionCall.args);
|
|
913
|
-
runtimeContext.context.addContextItem(
|
|
914
|
-
FunctionCallOutputItem.create(functionCall.callId, JSON.stringify(functionCallOutput))
|
|
915
|
-
);
|
|
916
|
-
return Promise.resolve();
|
|
917
|
-
}
|
|
918
|
-
async onError(context) {
|
|
919
|
-
const error = context.error;
|
|
920
|
-
if (!error) {
|
|
921
|
-
throw new Error("Error not found");
|
|
922
|
-
}
|
|
923
|
-
console.error(error);
|
|
924
|
-
return Promise.resolve();
|
|
925
|
-
}
|
|
926
|
-
async onModelResponded(context) {
|
|
927
|
-
return Promise.resolve();
|
|
928
|
-
}
|
|
929
|
-
async on(hookId, callback) {
|
|
930
|
-
this.hooksRegistry.registerHandler(hookId, callback);
|
|
931
|
-
}
|
|
932
|
-
async start(userMessage, model, context) {
|
|
933
|
-
console.log("Starting agent runtime");
|
|
934
|
-
const execution = Execution.create();
|
|
935
|
-
const loop = new AgentLoop();
|
|
936
|
-
const runtimeContext = {
|
|
937
|
-
execution,
|
|
938
|
-
userMessage,
|
|
939
|
-
model,
|
|
940
|
-
context
|
|
941
|
-
};
|
|
942
|
-
while (!execution.isTerminal()) {
|
|
943
|
-
console.log("Executing state", execution.currentStateId);
|
|
944
|
-
const state = loop.getState(runtimeContext);
|
|
945
|
-
try {
|
|
946
|
-
state.validateEntry(runtimeContext);
|
|
947
|
-
const stateDetails = state.getDetails();
|
|
948
|
-
if (stateDetails.before) {
|
|
949
|
-
const handler = this.hooksRegistry.getHandler(stateDetails.before);
|
|
950
|
-
await handler(runtimeContext);
|
|
951
|
-
}
|
|
952
|
-
const stateHandler = this.stateHandlerRegistry.getHandler(execution.currentStateId);
|
|
953
|
-
await stateHandler(runtimeContext);
|
|
954
|
-
if (stateDetails.after) {
|
|
955
|
-
const handler = this.hooksRegistry.getHandler(stateDetails.after);
|
|
956
|
-
await handler(runtimeContext);
|
|
957
|
-
}
|
|
958
|
-
} catch (error) {
|
|
959
|
-
console.log("Error occurred", error);
|
|
960
|
-
execution.status = 2 /* FAILED */;
|
|
961
|
-
const handler = this.hooksRegistry.getHandler("ON_ERROR" /* ON_ERROR */);
|
|
962
|
-
runtimeContext.error = error;
|
|
963
|
-
await handler(runtimeContext);
|
|
964
|
-
}
|
|
965
|
-
const transition = state.next(runtimeContext);
|
|
966
|
-
await transition.apply(runtimeContext);
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
};
|
|
970
|
-
|
|
971
|
-
// src/application/agent.ts
|
|
972
|
-
var Agent = class {
|
|
973
|
-
constructor() {
|
|
974
|
-
this.runtime = new AgentRuntime();
|
|
975
|
-
this.runtime.on("BEFORE_MESSAGE_RECEIVED" /* BEFORE_MESSAGE_RECEIVED */, this.beforeMessageReceived);
|
|
976
|
-
this.runtime.on("AFTER_MESSAGE_RECEIVED" /* AFTER_MESSAGE_RECEIVED */, this.afterMessageReceived);
|
|
977
|
-
this.runtime.on("BEFORE_INFERENCE" /* BEFORE_INFERENCE */, this.beforeInference);
|
|
978
|
-
this.runtime.on("AFTER_INFERENCE" /* AFTER_INFERENCE */, this.afterInference);
|
|
979
|
-
this.runtime.on("BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */, this.beforeFunctionCall);
|
|
980
|
-
this.runtime.on("AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */, this.afterFunctionCall);
|
|
981
|
-
this.runtime.on("BEFORE_MODEL_RESPONDED" /* BEFORE_MODEL_RESPONDED */, this.beforeModelResponded);
|
|
982
|
-
this.runtime.on("AFTER_MODEL_RESPONDED" /* AFTER_MODEL_RESPONDED */, this.afterModelResponded);
|
|
983
|
-
this.runtime.on("ON_ERROR" /* ON_ERROR */, this.onError);
|
|
984
|
-
}
|
|
985
|
-
async beforeMessageReceived(context) {
|
|
986
|
-
return Promise.resolve();
|
|
987
|
-
}
|
|
988
|
-
async afterMessageReceived(context) {
|
|
989
|
-
return Promise.resolve();
|
|
990
|
-
}
|
|
991
|
-
async beforeInference(context) {
|
|
992
|
-
return Promise.resolve();
|
|
993
|
-
}
|
|
994
|
-
async afterInference(context) {
|
|
995
|
-
const inferenceResponse = context.inferenceResponse;
|
|
996
|
-
if (inferenceResponse && this.visitor !== void 0) {
|
|
997
|
-
await this.visitor.afterInference(inferenceResponse);
|
|
998
|
-
}
|
|
999
|
-
return Promise.resolve();
|
|
1000
|
-
}
|
|
1001
|
-
async beforeFunctionCall(context) {
|
|
1002
|
-
if (this.functionCallVisitor !== void 0) {
|
|
1003
|
-
await this.functionCallVisitor.beforeFunctionCall(context);
|
|
1004
|
-
}
|
|
1005
|
-
return Promise.resolve();
|
|
1006
|
-
}
|
|
1007
|
-
async afterFunctionCall(context) {
|
|
1008
|
-
if (this.functionCallVisitor !== void 0) {
|
|
1009
|
-
await this.functionCallVisitor.afterFunctionCall(context);
|
|
1010
|
-
}
|
|
1011
|
-
return Promise.resolve();
|
|
1012
|
-
}
|
|
1013
|
-
async beforeModelResponded(context) {
|
|
1014
|
-
return Promise.resolve();
|
|
1015
|
-
}
|
|
1016
|
-
async afterModelResponded(context) {
|
|
1017
|
-
return Promise.resolve();
|
|
1018
|
-
}
|
|
1019
|
-
async onError(context) {
|
|
1020
|
-
console.log("Error occurred", context.error);
|
|
1021
|
-
return Promise.resolve();
|
|
1022
|
-
}
|
|
1023
|
-
async onStart(context) {
|
|
1024
|
-
if (this.visitor !== void 0) {
|
|
1025
|
-
await this.visitor.onStart(context);
|
|
1026
|
-
}
|
|
1027
|
-
return Promise.resolve();
|
|
1028
|
-
}
|
|
1029
|
-
setInferenceVisitor(visitor) {
|
|
1030
|
-
this.visitor = visitor;
|
|
1031
|
-
}
|
|
1032
|
-
setFunctionCallVisitor(visitor) {
|
|
1033
|
-
this.functionCallVisitor = visitor;
|
|
1034
|
-
}
|
|
1035
|
-
async run(userMessage, model, context) {
|
|
1036
|
-
const userMessageItem = UserMessageItem.create(userMessage);
|
|
1037
|
-
return this.runtime.start(userMessageItem, model, context);
|
|
1038
|
-
}
|
|
1039
|
-
};
|
|
1040
|
-
|
|
1041
|
-
// src/application/agent-society.ts
|
|
1042
|
-
var AgentSociety = class {
|
|
1043
|
-
constructor(name) {
|
|
1044
|
-
this.name = name;
|
|
1045
|
-
this.agents = [];
|
|
1046
|
-
this.isActive = false;
|
|
1047
|
-
}
|
|
1048
|
-
join(agent) {
|
|
1049
|
-
this.agents.push(agent);
|
|
1050
|
-
}
|
|
1051
|
-
enter(messageText, model, context) {
|
|
1052
|
-
for (const agent of this.agents) {
|
|
1053
|
-
agent.run(messageText, model, context);
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
async start() {
|
|
1057
|
-
this.isActive = true;
|
|
1058
|
-
while (this.isActive) {
|
|
1059
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
stop() {
|
|
1063
|
-
this.isActive = false;
|
|
1064
|
-
}
|
|
1065
|
-
};
|
|
1066
|
-
|
|
1067
550
|
// src/domain/model-context/context-item/client-item/system-message.ts
|
|
1068
551
|
var SystemMessageItem = class _SystemMessageItem extends ContextItem {
|
|
1069
552
|
constructor(content) {
|
|
@@ -1091,56 +574,26 @@ var SystemMessageItem = class _SystemMessageItem extends ContextItem {
|
|
|
1091
574
|
|
|
1092
575
|
// src/domain/agentic-environment/participant.ts
|
|
1093
576
|
var Participant = class {
|
|
1094
|
-
constructor(
|
|
577
|
+
constructor() {
|
|
1095
578
|
this.environments = [];
|
|
1096
|
-
this.toolExecutor = toolExecutor;
|
|
1097
|
-
this.messageSender = messageSender;
|
|
1098
|
-
this.inferenceHandler = inferenceHandler;
|
|
1099
|
-
}
|
|
1100
|
-
async sendMessage(message, environment) {
|
|
1101
|
-
if (!this.environments.includes(environment)) {
|
|
1102
|
-
return;
|
|
1103
|
-
}
|
|
1104
|
-
for await (const messagePart of this.messageSender.stream(message)) {
|
|
1105
|
-
environment.notifyMessage(this, messagePart);
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
async executeFunctionCall(functionCallItem, environment) {
|
|
1109
|
-
if (!this.environments.includes(environment)) {
|
|
1110
|
-
return;
|
|
1111
|
-
}
|
|
1112
|
-
for await (const item of this.toolExecutor.produce(functionCallItem)) {
|
|
1113
|
-
environment.notifyFunctionCallOutput(this, item);
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
async runInference(environment, context, model, signal) {
|
|
1117
|
-
if (!this.environments.includes(environment)) {
|
|
1118
|
-
return;
|
|
1119
|
-
}
|
|
1120
|
-
for await (const item of this.inferenceHandler.produce(context, model, signal)) {
|
|
1121
|
-
if (item instanceof FunctionCallItem) {
|
|
1122
|
-
environment.notifyFunctionCall(this, item);
|
|
1123
|
-
} else if (item instanceof ReasoningItem) {
|
|
1124
|
-
environment.notifyReasoning(this, item);
|
|
1125
|
-
} else if (item instanceof ModelMessageItem) {
|
|
1126
|
-
environment.notifyOutputMessage(this, item);
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
579
|
}
|
|
1130
580
|
join(environment) {
|
|
1131
|
-
if (this.
|
|
581
|
+
if (this.isJoinedTo(environment)) {
|
|
1132
582
|
return;
|
|
1133
583
|
}
|
|
1134
584
|
environment.subscribe(this);
|
|
1135
585
|
this.environments.push(environment);
|
|
1136
586
|
}
|
|
1137
587
|
leave(environment) {
|
|
1138
|
-
if (!this.
|
|
588
|
+
if (!this.isJoinedTo(environment)) {
|
|
1139
589
|
return;
|
|
1140
590
|
}
|
|
1141
591
|
environment.unsubscribe(this);
|
|
1142
592
|
this.environments = this.environments.filter((e) => e !== environment);
|
|
1143
593
|
}
|
|
594
|
+
isJoinedTo(environment) {
|
|
595
|
+
return this.environments.includes(environment);
|
|
596
|
+
}
|
|
1144
597
|
};
|
|
1145
598
|
|
|
1146
599
|
// src/domain/agentic-environment/agentic-environment.ts
|
|
@@ -1149,6 +602,17 @@ var AgenticEnvironment = class {
|
|
|
1149
602
|
this.subscribers = [];
|
|
1150
603
|
this.isActive = false;
|
|
1151
604
|
}
|
|
605
|
+
subscribe(subscriber) {
|
|
606
|
+
this.subscribers.push(subscriber);
|
|
607
|
+
}
|
|
608
|
+
unsubscribe(user) {
|
|
609
|
+
this.subscribers = this.subscribers.filter((p) => p !== user);
|
|
610
|
+
}
|
|
611
|
+
deliverContextItem(source, item) {
|
|
612
|
+
for (const subscriber of this.subscribers) {
|
|
613
|
+
subscriber.onContextItem(source, item);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
1152
616
|
async start() {
|
|
1153
617
|
this.isActive = true;
|
|
1154
618
|
while (this.isActive) {
|
|
@@ -1158,47 +622,75 @@ var AgenticEnvironment = class {
|
|
|
1158
622
|
stop() {
|
|
1159
623
|
this.isActive = false;
|
|
1160
624
|
}
|
|
1161
|
-
|
|
1162
|
-
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
// src/application/deliver-stream.ts
|
|
628
|
+
async function deliverStream(environment, initiator, stream) {
|
|
629
|
+
for await (const item of stream) {
|
|
630
|
+
await environment.deliverContextItem(initiator, item);
|
|
1163
631
|
}
|
|
1164
|
-
|
|
1165
|
-
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// src/application/agent.ts
|
|
635
|
+
var BaseAgentParticipant = class extends Participant {
|
|
636
|
+
constructor(inputSource, inferenceRunner, functionCallRunner) {
|
|
637
|
+
super();
|
|
638
|
+
this.inputSource = inputSource;
|
|
639
|
+
this.inferenceRunner = inferenceRunner;
|
|
640
|
+
this.functionCallRunner = functionCallRunner;
|
|
1166
641
|
}
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
subscriber.onFunctionCallOutput(subscriber, item);
|
|
1170
|
-
}
|
|
642
|
+
onContextItem(source, item) {
|
|
643
|
+
return Promise.resolve();
|
|
1171
644
|
}
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
645
|
+
async executeFunctionCall(environment, functionCallItem, signal) {
|
|
646
|
+
if (!this.isJoinedTo(environment)) return;
|
|
647
|
+
await deliverStream(
|
|
648
|
+
environment,
|
|
649
|
+
this,
|
|
650
|
+
this.functionCallRunner.run(functionCallItem, signal)
|
|
651
|
+
);
|
|
1179
652
|
}
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
653
|
+
async runInference(environment, context, model, signal) {
|
|
654
|
+
if (!this.isJoinedTo(environment)) return;
|
|
655
|
+
await deliverStream(
|
|
656
|
+
environment,
|
|
657
|
+
this,
|
|
658
|
+
this.inferenceRunner.run(context, model, signal)
|
|
659
|
+
);
|
|
1184
660
|
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
661
|
+
async streamInput(environment) {
|
|
662
|
+
if (!this.isJoinedTo(environment)) return;
|
|
663
|
+
await deliverStream(
|
|
664
|
+
environment,
|
|
665
|
+
this,
|
|
666
|
+
this.inputSource.stream()
|
|
667
|
+
);
|
|
1189
668
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
// src/application/human.ts
|
|
672
|
+
var BaseHumanParticipant = class extends Participant {
|
|
673
|
+
constructor(inputSource) {
|
|
674
|
+
super();
|
|
675
|
+
this.inputSource = inputSource;
|
|
676
|
+
}
|
|
677
|
+
onContextItem(source, item) {
|
|
678
|
+
return Promise.resolve();
|
|
679
|
+
}
|
|
680
|
+
async streamInput(environment) {
|
|
681
|
+
if (!this.isJoinedTo(environment)) return;
|
|
682
|
+
await deliverStream(
|
|
683
|
+
environment,
|
|
684
|
+
this,
|
|
685
|
+
this.inputSource.stream()
|
|
686
|
+
);
|
|
1194
687
|
}
|
|
1195
688
|
};
|
|
1196
689
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1197
690
|
0 && (module.exports = {
|
|
1198
|
-
Agent,
|
|
1199
|
-
AgentSociety,
|
|
1200
691
|
AgenticEnvironment,
|
|
1201
|
-
|
|
692
|
+
BaseAgentParticipant,
|
|
693
|
+
BaseHumanParticipant,
|
|
1202
694
|
ContextItem,
|
|
1203
695
|
DeveloperMessageItem,
|
|
1204
696
|
FunctionCallItem,
|
|
@@ -1206,12 +698,10 @@ var AgenticEnvironment = class {
|
|
|
1206
698
|
Gpt54,
|
|
1207
699
|
Gpt54Mini,
|
|
1208
700
|
Gpt54Nano,
|
|
1209
|
-
If,
|
|
1210
701
|
InMemoryModelContextRepository,
|
|
1211
702
|
InferenceRequest,
|
|
1212
703
|
InferenceResponse,
|
|
1213
704
|
InputTokenDetails,
|
|
1214
|
-
Loop,
|
|
1215
705
|
ModelContext,
|
|
1216
706
|
ModelMessageItem,
|
|
1217
707
|
OpenAIResponses,
|