@mozaik-ai/core 2.13.8 → 3.0.1
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 +1 -1
- package/dist/index.d.mts +88 -10
- package/dist/index.d.ts +88 -10
- package/dist/index.js +374 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +372 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/domain/model-context/context.ts
|
|
2
2
|
var Context = class _Context {
|
|
3
3
|
constructor(id, projectId, items) {
|
|
4
4
|
this.projectId = projectId;
|
|
@@ -34,19 +34,19 @@ var Context = class _Context {
|
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
-
// src/
|
|
37
|
+
// src/domain/model-context/context-item/context-item.ts
|
|
38
38
|
var ContextItem = class {
|
|
39
39
|
getType() {
|
|
40
40
|
return this.type;
|
|
41
41
|
}
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
// src/
|
|
45
|
-
var
|
|
44
|
+
// src/domain/model-context/context-item/item-content/item-content.ts
|
|
45
|
+
var ItemContent = class {
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
-
// src/
|
|
49
|
-
var InputText = class _InputText extends
|
|
48
|
+
// src/domain/model-context/context-item/item-content/input-text.ts
|
|
49
|
+
var InputText = class _InputText extends ItemContent {
|
|
50
50
|
constructor(text) {
|
|
51
51
|
super();
|
|
52
52
|
this.text = text;
|
|
@@ -68,7 +68,7 @@ var InputText = class _InputText extends Content {
|
|
|
68
68
|
}
|
|
69
69
|
};
|
|
70
70
|
|
|
71
|
-
// src/
|
|
71
|
+
// src/domain/model-context/context-item/client-item/user-message.ts
|
|
72
72
|
var UserMessage = class _UserMessage extends ContextItem {
|
|
73
73
|
constructor(content) {
|
|
74
74
|
super();
|
|
@@ -93,7 +93,7 @@ var UserMessage = class _UserMessage extends ContextItem {
|
|
|
93
93
|
}
|
|
94
94
|
};
|
|
95
95
|
|
|
96
|
-
// src/
|
|
96
|
+
// src/domain/model-context/context-item/client-item/developer-message.ts
|
|
97
97
|
var DeveloperMessage = class _DeveloperMessage extends ContextItem {
|
|
98
98
|
constructor(content) {
|
|
99
99
|
super();
|
|
@@ -118,8 +118,8 @@ var DeveloperMessage = class _DeveloperMessage extends ContextItem {
|
|
|
118
118
|
}
|
|
119
119
|
};
|
|
120
120
|
|
|
121
|
-
// src/
|
|
122
|
-
var OutputText = class _OutputText extends
|
|
121
|
+
// src/domain/model-context/context-item/item-content/output-text.ts
|
|
122
|
+
var OutputText = class _OutputText extends ItemContent {
|
|
123
123
|
constructor(text) {
|
|
124
124
|
super();
|
|
125
125
|
this.text = text;
|
|
@@ -138,7 +138,7 @@ var OutputText = class _OutputText extends Content {
|
|
|
138
138
|
}
|
|
139
139
|
};
|
|
140
140
|
|
|
141
|
-
// src/
|
|
141
|
+
// src/domain/model-context/context-item/model-item/model-message.ts
|
|
142
142
|
var ModelMessage = class _ModelMessage extends ContextItem {
|
|
143
143
|
constructor(content) {
|
|
144
144
|
super();
|
|
@@ -159,7 +159,7 @@ var ModelMessage = class _ModelMessage extends ContextItem {
|
|
|
159
159
|
}
|
|
160
160
|
};
|
|
161
161
|
|
|
162
|
-
// src/
|
|
162
|
+
// src/domain/model-context/context-item/model-item/function-call.ts
|
|
163
163
|
var FunctionCall = class _FunctionCall extends ContextItem {
|
|
164
164
|
constructor(callId, name, args) {
|
|
165
165
|
super();
|
|
@@ -181,7 +181,7 @@ var FunctionCall = class _FunctionCall extends ContextItem {
|
|
|
181
181
|
}
|
|
182
182
|
};
|
|
183
183
|
|
|
184
|
-
// src/
|
|
184
|
+
// src/domain/model-context/context-item/model-item/reasoning.ts
|
|
185
185
|
var Reasoning = class _Reasoning extends ContextItem {
|
|
186
186
|
constructor(content, encryptedContent, summary = []) {
|
|
187
187
|
super();
|
|
@@ -204,7 +204,7 @@ var Reasoning = class _Reasoning extends ContextItem {
|
|
|
204
204
|
}
|
|
205
205
|
};
|
|
206
206
|
|
|
207
|
-
// src/
|
|
207
|
+
// src/domain/model-context/context-item/client-item/function-call-output.ts
|
|
208
208
|
var FunctionCallOutput = class _FunctionCallOutput extends ContextItem {
|
|
209
209
|
constructor(callId, output) {
|
|
210
210
|
super();
|
|
@@ -228,7 +228,7 @@ var FunctionCallOutput = class _FunctionCallOutput extends ContextItem {
|
|
|
228
228
|
}
|
|
229
229
|
};
|
|
230
230
|
|
|
231
|
-
// src/
|
|
231
|
+
// src/domain/generative-model/inference-response.ts
|
|
232
232
|
var InferenceResponse = class {
|
|
233
233
|
constructor(contextItems, tokenUsage) {
|
|
234
234
|
this.contextItems = contextItems;
|
|
@@ -236,7 +236,7 @@ var InferenceResponse = class {
|
|
|
236
236
|
}
|
|
237
237
|
};
|
|
238
238
|
|
|
239
|
-
// src/
|
|
239
|
+
// src/domain/generative-model/token-usage.ts
|
|
240
240
|
var InputTokenDetails = class {
|
|
241
241
|
constructor(cached_tokens) {
|
|
242
242
|
this.cached_tokens = cached_tokens;
|
|
@@ -257,7 +257,7 @@ var TokenUsage = class {
|
|
|
257
257
|
}
|
|
258
258
|
};
|
|
259
259
|
|
|
260
|
-
// src/providers/openai/runtime/openai-responses.ts
|
|
260
|
+
// src/infrastructure/providers/openai/runtime/openai-responses.ts
|
|
261
261
|
import OpenAI from "openai";
|
|
262
262
|
var OpenAIResponses = class {
|
|
263
263
|
constructor() {
|
|
@@ -324,7 +324,7 @@ var OpenAIResponses = class {
|
|
|
324
324
|
}
|
|
325
325
|
};
|
|
326
326
|
|
|
327
|
-
// src/
|
|
327
|
+
// src/domain/generative-model/inference-request.ts
|
|
328
328
|
var InferenceRequest = class {
|
|
329
329
|
constructor(model, context) {
|
|
330
330
|
this.model = model;
|
|
@@ -332,7 +332,7 @@ var InferenceRequest = class {
|
|
|
332
332
|
}
|
|
333
333
|
};
|
|
334
334
|
|
|
335
|
-
// src/providers/openai/reasoning-effort.ts
|
|
335
|
+
// src/infrastructure/providers/openai/reasoning-effort.ts
|
|
336
336
|
var OpenAIReasoningEffort = class {
|
|
337
337
|
constructor(reasoningEffort) {
|
|
338
338
|
this.reasoningEffort = reasoningEffort;
|
|
@@ -348,7 +348,7 @@ var OpenAIReasoningEffort = class {
|
|
|
348
348
|
}
|
|
349
349
|
};
|
|
350
350
|
|
|
351
|
-
// src/providers/openai/models/gpt-5-4-nano.ts
|
|
351
|
+
// src/infrastructure/providers/openai/models/gpt-5-4-nano.ts
|
|
352
352
|
var Gpt54Nano = class {
|
|
353
353
|
constructor() {
|
|
354
354
|
this.specification = {
|
|
@@ -379,7 +379,7 @@ var Gpt54Nano = class {
|
|
|
379
379
|
}
|
|
380
380
|
};
|
|
381
381
|
|
|
382
|
-
// src/providers/openai/models/gpt-5-4.ts
|
|
382
|
+
// src/infrastructure/providers/openai/models/gpt-5-4.ts
|
|
383
383
|
var Gpt54 = class {
|
|
384
384
|
constructor() {
|
|
385
385
|
this.specification = {
|
|
@@ -410,7 +410,7 @@ var Gpt54 = class {
|
|
|
410
410
|
}
|
|
411
411
|
};
|
|
412
412
|
|
|
413
|
-
// src/providers/openai/models/gpt-5-4-mini.ts
|
|
413
|
+
// src/infrastructure/providers/openai/models/gpt-5-4-mini.ts
|
|
414
414
|
var Gpt54Mini = class {
|
|
415
415
|
constructor() {
|
|
416
416
|
this.specification = {
|
|
@@ -441,7 +441,7 @@ var Gpt54Mini = class {
|
|
|
441
441
|
}
|
|
442
442
|
};
|
|
443
443
|
|
|
444
|
-
// src/
|
|
444
|
+
// src/domain/agent-loop/rules/condition/condition.ts
|
|
445
445
|
var BaseCondition = class {
|
|
446
446
|
and(other) {
|
|
447
447
|
return new AndCondition(this, other);
|
|
@@ -483,7 +483,7 @@ var OrCondition = class extends BaseCondition {
|
|
|
483
483
|
}
|
|
484
484
|
};
|
|
485
485
|
|
|
486
|
-
// src/
|
|
486
|
+
// src/domain/agent-loop/rules/rule/rule.ts
|
|
487
487
|
var If = class {
|
|
488
488
|
constructor({ condition, action }) {
|
|
489
489
|
this.condition = condition;
|
|
@@ -509,7 +509,7 @@ var Loop = class {
|
|
|
509
509
|
}
|
|
510
510
|
};
|
|
511
511
|
|
|
512
|
-
// src/
|
|
512
|
+
// src/infrastructure/repository/in-memory-context-repository.ts
|
|
513
513
|
var InMemoryContextRepository = class {
|
|
514
514
|
constructor() {
|
|
515
515
|
this.store = /* @__PURE__ */ new Map();
|
|
@@ -550,7 +550,354 @@ var InMemoryContextRepository = class {
|
|
|
550
550
|
});
|
|
551
551
|
}
|
|
552
552
|
};
|
|
553
|
+
|
|
554
|
+
// src/domain/agent-loop/execution.ts
|
|
555
|
+
var Execution = class _Execution {
|
|
556
|
+
constructor(executionId, currentStateId, previousStateId, status, stepCount, retryCounts, history) {
|
|
557
|
+
this.executionId = executionId;
|
|
558
|
+
this.currentStateId = currentStateId;
|
|
559
|
+
this.previousStateId = previousStateId;
|
|
560
|
+
this.status = status;
|
|
561
|
+
this.stepCount = stepCount;
|
|
562
|
+
this.retryCounts = retryCounts;
|
|
563
|
+
this.history = history;
|
|
564
|
+
}
|
|
565
|
+
isTerminal() {
|
|
566
|
+
return this.status == 1 /* COMPLETED */ || this.status == 2 /* FAILED */;
|
|
567
|
+
}
|
|
568
|
+
static create() {
|
|
569
|
+
const executionId = crypto.randomUUID();
|
|
570
|
+
const currentStateId = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
|
|
571
|
+
const previousStateId = null;
|
|
572
|
+
const status = 0 /* RUNNING */;
|
|
573
|
+
const stepCount = 0;
|
|
574
|
+
const retryCounts = /* @__PURE__ */ new Map();
|
|
575
|
+
const history = [];
|
|
576
|
+
return new _Execution(executionId, currentStateId, previousStateId, status, stepCount, retryCounts, history);
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
// src/domain/agent-loop/transition/go-to.ts
|
|
581
|
+
var GoTo = class {
|
|
582
|
+
constructor(next) {
|
|
583
|
+
this.next = next;
|
|
584
|
+
}
|
|
585
|
+
async apply(context) {
|
|
586
|
+
const execution = context.execution;
|
|
587
|
+
execution.previousStateId = execution.currentStateId;
|
|
588
|
+
execution.currentStateId = this.next;
|
|
589
|
+
execution.stepCount++;
|
|
590
|
+
execution.history.push({
|
|
591
|
+
fromState: execution.previousStateId,
|
|
592
|
+
toState: execution.currentStateId,
|
|
593
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
594
|
+
duration: 0,
|
|
595
|
+
error: null
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
// src/domain/agent-loop/transition/fail.ts
|
|
601
|
+
var Fail = class {
|
|
602
|
+
constructor(error) {
|
|
603
|
+
this.error = error;
|
|
604
|
+
}
|
|
605
|
+
async apply(context) {
|
|
606
|
+
const execution = context.execution;
|
|
607
|
+
execution.status = 2 /* FAILED */;
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
|
|
611
|
+
// src/domain/agent-loop/state/function-call.ts
|
|
612
|
+
var FunctionCallState = class {
|
|
613
|
+
constructor() {
|
|
614
|
+
this.id = "FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */;
|
|
615
|
+
this.beforeHookId = "BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */;
|
|
616
|
+
this.afterHookId = "AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */;
|
|
617
|
+
}
|
|
618
|
+
getDetails() {
|
|
619
|
+
return {
|
|
620
|
+
before: this.beforeHookId,
|
|
621
|
+
stateId: this.id,
|
|
622
|
+
after: this.afterHookId
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
validateEntry(runtime) {
|
|
626
|
+
var _a;
|
|
627
|
+
const functionCall = (_a = runtime.inferenceResponse) == null ? void 0 : _a.contextItems.find((item) => item.getType() === "function_call");
|
|
628
|
+
if (!functionCall || !(functionCall instanceof FunctionCall)) {
|
|
629
|
+
throw new Error("Function call not found");
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
next(runtime) {
|
|
633
|
+
const functionCallOutput = runtime.functionCallOutput;
|
|
634
|
+
if (!functionCallOutput) {
|
|
635
|
+
return new Fail("Function call output not found");
|
|
636
|
+
}
|
|
637
|
+
runtime.context.addItem(functionCallOutput);
|
|
638
|
+
return new GoTo("INFERENCE_PENDING" /* INFERENCE_PENDING */);
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// src/domain/agent-loop/transition/complete.ts
|
|
643
|
+
var Complete = class {
|
|
644
|
+
constructor(result) {
|
|
645
|
+
this.result = result;
|
|
646
|
+
}
|
|
647
|
+
async apply(context) {
|
|
648
|
+
const execution = context.execution;
|
|
649
|
+
execution.status = 1 /* COMPLETED */;
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// src/domain/agent-loop/state/model-message.ts
|
|
654
|
+
var ModelMessageState = class {
|
|
655
|
+
constructor() {
|
|
656
|
+
this.id = "MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */;
|
|
657
|
+
this.beforeHookId = "BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */;
|
|
658
|
+
this.afterHookId = "AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */;
|
|
659
|
+
}
|
|
660
|
+
getDetails() {
|
|
661
|
+
return {
|
|
662
|
+
before: this.beforeHookId,
|
|
663
|
+
stateId: this.id,
|
|
664
|
+
after: this.afterHookId
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
validateEntry(runtime) {
|
|
668
|
+
if (!runtime.modelMessage) {
|
|
669
|
+
throw new Error("Model message not found");
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
next(runtime) {
|
|
673
|
+
if (!runtime.modelMessage) {
|
|
674
|
+
return new Fail("Model message not found");
|
|
675
|
+
}
|
|
676
|
+
return new Complete(runtime.modelMessage.content.text);
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
// src/domain/agent-loop/state/inference-pending.ts
|
|
681
|
+
var InferencePendingState = class {
|
|
682
|
+
constructor() {
|
|
683
|
+
this.id = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
|
|
684
|
+
this.beforeHookId = "BEFORE_INFERENCE" /* BEFORE_INFERENCE */;
|
|
685
|
+
this.afterHookId = "AFTER_INFERENCE" /* AFTER_INFERENCE */;
|
|
686
|
+
}
|
|
687
|
+
getDetails() {
|
|
688
|
+
return {
|
|
689
|
+
before: this.beforeHookId,
|
|
690
|
+
stateId: this.id,
|
|
691
|
+
after: this.afterHookId
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
validateEntry(runtime) {
|
|
695
|
+
const inferenceRequest = runtime.inferenceRequest;
|
|
696
|
+
if (!inferenceRequest) {
|
|
697
|
+
throw new Error("Inference request not found");
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
next(runtime) {
|
|
701
|
+
const inferenceResponse = runtime.inferenceResponse;
|
|
702
|
+
if (!inferenceResponse) {
|
|
703
|
+
return new Fail("Inference response not found");
|
|
704
|
+
}
|
|
705
|
+
if (inferenceResponse instanceof FunctionCall) {
|
|
706
|
+
return new GoTo("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */);
|
|
707
|
+
}
|
|
708
|
+
if (inferenceResponse instanceof ModelMessage) {
|
|
709
|
+
return new GoTo("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */);
|
|
710
|
+
}
|
|
711
|
+
return new Fail("Invalid response type");
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
// src/domain/agent-loop/loop.ts
|
|
716
|
+
var AgentLoop = class {
|
|
717
|
+
constructor() {
|
|
718
|
+
this.states = /* @__PURE__ */ new Map();
|
|
719
|
+
this.states.set("INFERENCE_PENDING" /* INFERENCE_PENDING */, new InferencePendingState());
|
|
720
|
+
this.states.set("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, new FunctionCallState());
|
|
721
|
+
this.states.set("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, new ModelMessageState());
|
|
722
|
+
}
|
|
723
|
+
validateEntry(runtime) {
|
|
724
|
+
const state = this.states.get(runtime.execution.currentStateId);
|
|
725
|
+
if (!state) {
|
|
726
|
+
throw new Error(`State ${runtime.execution.currentStateId} not found`);
|
|
727
|
+
}
|
|
728
|
+
state.validateEntry(runtime);
|
|
729
|
+
}
|
|
730
|
+
getStateDetails(runtime) {
|
|
731
|
+
const state = this.states.get(runtime.execution.currentStateId);
|
|
732
|
+
if (!state) {
|
|
733
|
+
throw new Error(`State ${runtime.execution.currentStateId} not found`);
|
|
734
|
+
}
|
|
735
|
+
return state.getDetails();
|
|
736
|
+
}
|
|
737
|
+
next(runtime) {
|
|
738
|
+
const state = this.states.get(runtime.execution.currentStateId);
|
|
739
|
+
if (!state) {
|
|
740
|
+
throw new Error(`State ${runtime.execution.currentStateId} not found`);
|
|
741
|
+
}
|
|
742
|
+
return state.next(runtime);
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
// src/domain/agent-loop/hooks/hooks-registry.ts
|
|
747
|
+
var HooksRegistry = class {
|
|
748
|
+
constructor() {
|
|
749
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
750
|
+
}
|
|
751
|
+
getHandler(hookId) {
|
|
752
|
+
const handler = this.handlers.get(hookId);
|
|
753
|
+
if (!handler) {
|
|
754
|
+
throw new Error(`Hook handler for hook ${hookId} not found`);
|
|
755
|
+
}
|
|
756
|
+
return handler;
|
|
757
|
+
}
|
|
758
|
+
registerHandler(hookId, handler) {
|
|
759
|
+
this.handlers.set(hookId, handler);
|
|
760
|
+
}
|
|
761
|
+
unregisterHandler(hookId) {
|
|
762
|
+
this.handlers.delete(hookId);
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
// src/domain/agent-loop/state/state-registry.ts
|
|
767
|
+
var StateHandlerRegistry = class {
|
|
768
|
+
constructor() {
|
|
769
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
770
|
+
}
|
|
771
|
+
getHandler(stateId) {
|
|
772
|
+
const handler = this.handlers.get(stateId);
|
|
773
|
+
if (!handler) {
|
|
774
|
+
throw new Error(`Handler for state ${stateId} not found`);
|
|
775
|
+
}
|
|
776
|
+
return handler;
|
|
777
|
+
}
|
|
778
|
+
registerHandler(stateId, handler) {
|
|
779
|
+
this.handlers.set(stateId, handler);
|
|
780
|
+
}
|
|
781
|
+
unregisterHandler(stateId) {
|
|
782
|
+
this.handlers.delete(stateId);
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
// src/application/agent-runtime.ts
|
|
787
|
+
var AgentRuntime = class {
|
|
788
|
+
constructor() {
|
|
789
|
+
this.hooksRegistry = new HooksRegistry();
|
|
790
|
+
this.stateHandlerRegistry = new StateHandlerRegistry();
|
|
791
|
+
this.stateHandlerRegistry.registerHandler("INFERENCE_PENDING" /* INFERENCE_PENDING */, this.onInferencePending);
|
|
792
|
+
this.stateHandlerRegistry.registerHandler("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, this.onFunctionCallPending);
|
|
793
|
+
this.stateHandlerRegistry.registerHandler("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, this.onModelMessageReceived);
|
|
794
|
+
}
|
|
795
|
+
async onInferencePending(context) {
|
|
796
|
+
const openaiResponses = new OpenAIResponses();
|
|
797
|
+
const inferenceRequest = new InferenceRequest(context.model, context.context);
|
|
798
|
+
const inferenceResponse = await openaiResponses.infer(inferenceRequest);
|
|
799
|
+
context.inferenceResponse = inferenceResponse;
|
|
800
|
+
return Promise.resolve();
|
|
801
|
+
}
|
|
802
|
+
async onFunctionCallPending(context) {
|
|
803
|
+
var _a;
|
|
804
|
+
const functionCall = (_a = context.inferenceResponse) == null ? void 0 : _a.contextItems.find((item) => item.getType() === "function_call");
|
|
805
|
+
if (!functionCall || !(functionCall instanceof FunctionCall)) {
|
|
806
|
+
throw new Error("Function call not found");
|
|
807
|
+
}
|
|
808
|
+
const tool = context.model.getTools().find((tool2) => tool2.name === functionCall.name);
|
|
809
|
+
if (!tool) {
|
|
810
|
+
throw new Error("Function not found");
|
|
811
|
+
}
|
|
812
|
+
const functionCallOutput = await tool.invoke(functionCall.args);
|
|
813
|
+
context.functionCallOutput = FunctionCallOutput.create(functionCall.callId, functionCallOutput);
|
|
814
|
+
return Promise.resolve();
|
|
815
|
+
}
|
|
816
|
+
async onModelMessageReceived(context) {
|
|
817
|
+
return Promise.resolve();
|
|
818
|
+
}
|
|
819
|
+
async on(hookId, callback) {
|
|
820
|
+
this.hooksRegistry.registerHandler(hookId, callback);
|
|
821
|
+
}
|
|
822
|
+
async start(userMessage, model, context) {
|
|
823
|
+
const execution = Execution.create();
|
|
824
|
+
const loop = new AgentLoop();
|
|
825
|
+
const runtimeContext = {
|
|
826
|
+
execution,
|
|
827
|
+
userMessage,
|
|
828
|
+
model,
|
|
829
|
+
context
|
|
830
|
+
};
|
|
831
|
+
while (!execution.isTerminal()) {
|
|
832
|
+
const transition = loop.next(runtimeContext);
|
|
833
|
+
await transition.apply(runtimeContext);
|
|
834
|
+
try {
|
|
835
|
+
const stateDetails = loop.getStateDetails(runtimeContext);
|
|
836
|
+
if (stateDetails.before) {
|
|
837
|
+
const handler = this.hooksRegistry.getHandler(stateDetails.before);
|
|
838
|
+
await handler(runtimeContext);
|
|
839
|
+
}
|
|
840
|
+
if (stateDetails.after) {
|
|
841
|
+
const handler = this.hooksRegistry.getHandler(stateDetails.after);
|
|
842
|
+
await handler(runtimeContext);
|
|
843
|
+
}
|
|
844
|
+
} catch (error) {
|
|
845
|
+
execution.status = 2 /* FAILED */;
|
|
846
|
+
break;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
|
|
852
|
+
// src/application/agent.ts
|
|
853
|
+
var Agent = class {
|
|
854
|
+
constructor(runtime) {
|
|
855
|
+
this.runtime = runtime;
|
|
856
|
+
this.runtime.on("BEFORE_INFERENCE" /* BEFORE_INFERENCE */, this.beforeInference);
|
|
857
|
+
this.runtime.on("AFTER_INFERENCE" /* AFTER_INFERENCE */, this.afterInference);
|
|
858
|
+
this.runtime.on("BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */, this.beforeFunctionCall);
|
|
859
|
+
this.runtime.on("AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */, this.afterFunctionCall);
|
|
860
|
+
this.runtime.on("BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */, this.beforeModelMessage);
|
|
861
|
+
this.runtime.on("AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */, this.afterModelMessage);
|
|
862
|
+
}
|
|
863
|
+
async beforeInference(context) {
|
|
864
|
+
return Promise.resolve();
|
|
865
|
+
}
|
|
866
|
+
async afterInference(context) {
|
|
867
|
+
const inferenceResponse = context.inferenceResponse;
|
|
868
|
+
if (inferenceResponse && this.visitor) {
|
|
869
|
+
await this.visitor.afterInference(inferenceResponse);
|
|
870
|
+
}
|
|
871
|
+
return Promise.resolve();
|
|
872
|
+
}
|
|
873
|
+
async beforeFunctionCall(context) {
|
|
874
|
+
return Promise.resolve();
|
|
875
|
+
}
|
|
876
|
+
async afterFunctionCall(context) {
|
|
877
|
+
return Promise.resolve();
|
|
878
|
+
}
|
|
879
|
+
async beforeModelMessage(context) {
|
|
880
|
+
return Promise.resolve();
|
|
881
|
+
}
|
|
882
|
+
async afterModelMessage(context) {
|
|
883
|
+
return Promise.resolve();
|
|
884
|
+
}
|
|
885
|
+
async onStart(context) {
|
|
886
|
+
var _a;
|
|
887
|
+
await ((_a = this.visitor) == null ? void 0 : _a.onStart(context));
|
|
888
|
+
return Promise.resolve();
|
|
889
|
+
}
|
|
890
|
+
setInferenceVisitor(visitor) {
|
|
891
|
+
this.visitor = visitor;
|
|
892
|
+
}
|
|
893
|
+
async run(userMessage, model, context) {
|
|
894
|
+
const userMessageItem = UserMessage.create(userMessage);
|
|
895
|
+
return this.runtime.start(userMessageItem, model, context);
|
|
896
|
+
}
|
|
897
|
+
};
|
|
553
898
|
export {
|
|
899
|
+
Agent,
|
|
900
|
+
AgentRuntime,
|
|
554
901
|
BaseCondition,
|
|
555
902
|
Context,
|
|
556
903
|
ContextItem,
|