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