@mozaik-ai/core 3.0.0 → 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/dist/index.d.mts +84 -1
- package/dist/index.d.ts +84 -1
- package/dist/index.js +349 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +347 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -323,4 +323,87 @@ declare class InMemoryContextRepository implements ContextRepository {
|
|
|
323
323
|
private clone;
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
|
|
326
|
+
declare enum HookId {
|
|
327
|
+
BEFORE_INFERENCE = "BEFORE_INFERENCE",
|
|
328
|
+
AFTER_INFERENCE = "AFTER_INFERENCE",
|
|
329
|
+
BEFORE_FUNCTION_CALL = "BEFORE_FUNCTION_CALL",
|
|
330
|
+
AFTER_FUNCTION_CALL = "AFTER_FUNCTION_CALL",
|
|
331
|
+
BEFORE_MODEL_MESSAGE = "BEFORE_MODEL_MESSAGE",
|
|
332
|
+
AFTER_MODEL_MESSAGE = "AFTER_MODEL_MESSAGE"
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
interface TransitionRecord {
|
|
336
|
+
fromState: StateId;
|
|
337
|
+
toState: StateId;
|
|
338
|
+
timestamp: Date;
|
|
339
|
+
duration: number;
|
|
340
|
+
error: Error | null;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
declare enum StateId {
|
|
344
|
+
INFERENCE_PENDING = "INFERENCE_PENDING",
|
|
345
|
+
FUNCTION_CALL_PENDING = "FUNCTION_CALL_PENDING",
|
|
346
|
+
MODEL_MESSAGE_RECEIVED = "MODEL_MESSAGE_RECEIVED"
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
declare enum ExecutionStatus {
|
|
350
|
+
RUNNING = 0,
|
|
351
|
+
COMPLETED = 1,
|
|
352
|
+
FAILED = 2
|
|
353
|
+
}
|
|
354
|
+
declare class Execution {
|
|
355
|
+
executionId: string;
|
|
356
|
+
currentStateId: StateId;
|
|
357
|
+
previousStateId: StateId | null;
|
|
358
|
+
status: ExecutionStatus;
|
|
359
|
+
stepCount: number;
|
|
360
|
+
retryCounts: Map<StateId, number>;
|
|
361
|
+
history: TransitionRecord[];
|
|
362
|
+
constructor(executionId: string, currentStateId: StateId, previousStateId: StateId | null, status: ExecutionStatus, stepCount: number, retryCounts: Map<StateId, number>, history: TransitionRecord[]);
|
|
363
|
+
isTerminal(): boolean;
|
|
364
|
+
static create(): Execution;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
interface RuntimeContext {
|
|
368
|
+
execution: Execution;
|
|
369
|
+
userMessage: UserMessage;
|
|
370
|
+
inferenceRequest?: InferenceRequest;
|
|
371
|
+
model: GenerativeModel & ReasoningEffort<string> & ToolCallingCapability;
|
|
372
|
+
context: Context;
|
|
373
|
+
inferenceResponse?: InferenceResponse;
|
|
374
|
+
functionCallOutput?: FunctionCallOutput;
|
|
375
|
+
modelMessage?: ModelMessage;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
declare class AgentRuntime {
|
|
379
|
+
private hooksRegistry;
|
|
380
|
+
private stateHandlerRegistry;
|
|
381
|
+
constructor();
|
|
382
|
+
onInferencePending(context: RuntimeContext): Promise<void>;
|
|
383
|
+
onFunctionCallPending(context: RuntimeContext): Promise<void>;
|
|
384
|
+
onModelMessageReceived(context: RuntimeContext): Promise<void>;
|
|
385
|
+
on(hookId: HookId, callback: (data: any) => Promise<void>): Promise<void>;
|
|
386
|
+
start(userMessage: UserMessage, model: GenerativeModel & ReasoningEffort<string> & ToolCallingCapability, context: Context): Promise<void>;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
interface InferenceVisitor {
|
|
390
|
+
onStart(context: RuntimeContext): Promise<void>;
|
|
391
|
+
afterInference(inferenceResponse: InferenceResponse): Promise<void>;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
declare class Agent {
|
|
395
|
+
private readonly runtime;
|
|
396
|
+
private visitor;
|
|
397
|
+
constructor(runtime: AgentRuntime);
|
|
398
|
+
beforeInference(context: RuntimeContext): Promise<void>;
|
|
399
|
+
afterInference(context: RuntimeContext): Promise<void>;
|
|
400
|
+
beforeFunctionCall(context: RuntimeContext): Promise<void>;
|
|
401
|
+
afterFunctionCall(context: RuntimeContext): Promise<void>;
|
|
402
|
+
beforeModelMessage(context: RuntimeContext): Promise<void>;
|
|
403
|
+
afterModelMessage(context: RuntimeContext): Promise<void>;
|
|
404
|
+
onStart(context: RuntimeContext): Promise<void>;
|
|
405
|
+
setInferenceVisitor(visitor: InferenceVisitor): void;
|
|
406
|
+
run(userMessage: string, model: GenerativeModel & ReasoningEffort<string> & ToolCallingCapability, context: Context): Promise<void>;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export { type Action, Agent, AgentRuntime, type AsyncAction, type AsyncRule, BaseCondition, type Condition, Context, ContextItem, type ContextRepository, DeveloperMessage, FunctionCall, FunctionCallOutput, type GenerativeModel, Gpt54, Gpt54Mini, Gpt54Nano, If, InMemoryContextRepository, InferenceRequest, InferenceResponse, type InferenceVisitor, InputTokenDetails, Loop, ModelMessage, OpenAIResponses, OutputTokenDetails, Reasoning, TokenUsage, type Tool, UserMessage };
|
package/dist/index.d.ts
CHANGED
|
@@ -323,4 +323,87 @@ declare class InMemoryContextRepository implements ContextRepository {
|
|
|
323
323
|
private clone;
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
|
|
326
|
+
declare enum HookId {
|
|
327
|
+
BEFORE_INFERENCE = "BEFORE_INFERENCE",
|
|
328
|
+
AFTER_INFERENCE = "AFTER_INFERENCE",
|
|
329
|
+
BEFORE_FUNCTION_CALL = "BEFORE_FUNCTION_CALL",
|
|
330
|
+
AFTER_FUNCTION_CALL = "AFTER_FUNCTION_CALL",
|
|
331
|
+
BEFORE_MODEL_MESSAGE = "BEFORE_MODEL_MESSAGE",
|
|
332
|
+
AFTER_MODEL_MESSAGE = "AFTER_MODEL_MESSAGE"
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
interface TransitionRecord {
|
|
336
|
+
fromState: StateId;
|
|
337
|
+
toState: StateId;
|
|
338
|
+
timestamp: Date;
|
|
339
|
+
duration: number;
|
|
340
|
+
error: Error | null;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
declare enum StateId {
|
|
344
|
+
INFERENCE_PENDING = "INFERENCE_PENDING",
|
|
345
|
+
FUNCTION_CALL_PENDING = "FUNCTION_CALL_PENDING",
|
|
346
|
+
MODEL_MESSAGE_RECEIVED = "MODEL_MESSAGE_RECEIVED"
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
declare enum ExecutionStatus {
|
|
350
|
+
RUNNING = 0,
|
|
351
|
+
COMPLETED = 1,
|
|
352
|
+
FAILED = 2
|
|
353
|
+
}
|
|
354
|
+
declare class Execution {
|
|
355
|
+
executionId: string;
|
|
356
|
+
currentStateId: StateId;
|
|
357
|
+
previousStateId: StateId | null;
|
|
358
|
+
status: ExecutionStatus;
|
|
359
|
+
stepCount: number;
|
|
360
|
+
retryCounts: Map<StateId, number>;
|
|
361
|
+
history: TransitionRecord[];
|
|
362
|
+
constructor(executionId: string, currentStateId: StateId, previousStateId: StateId | null, status: ExecutionStatus, stepCount: number, retryCounts: Map<StateId, number>, history: TransitionRecord[]);
|
|
363
|
+
isTerminal(): boolean;
|
|
364
|
+
static create(): Execution;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
interface RuntimeContext {
|
|
368
|
+
execution: Execution;
|
|
369
|
+
userMessage: UserMessage;
|
|
370
|
+
inferenceRequest?: InferenceRequest;
|
|
371
|
+
model: GenerativeModel & ReasoningEffort<string> & ToolCallingCapability;
|
|
372
|
+
context: Context;
|
|
373
|
+
inferenceResponse?: InferenceResponse;
|
|
374
|
+
functionCallOutput?: FunctionCallOutput;
|
|
375
|
+
modelMessage?: ModelMessage;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
declare class AgentRuntime {
|
|
379
|
+
private hooksRegistry;
|
|
380
|
+
private stateHandlerRegistry;
|
|
381
|
+
constructor();
|
|
382
|
+
onInferencePending(context: RuntimeContext): Promise<void>;
|
|
383
|
+
onFunctionCallPending(context: RuntimeContext): Promise<void>;
|
|
384
|
+
onModelMessageReceived(context: RuntimeContext): Promise<void>;
|
|
385
|
+
on(hookId: HookId, callback: (data: any) => Promise<void>): Promise<void>;
|
|
386
|
+
start(userMessage: UserMessage, model: GenerativeModel & ReasoningEffort<string> & ToolCallingCapability, context: Context): Promise<void>;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
interface InferenceVisitor {
|
|
390
|
+
onStart(context: RuntimeContext): Promise<void>;
|
|
391
|
+
afterInference(inferenceResponse: InferenceResponse): Promise<void>;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
declare class Agent {
|
|
395
|
+
private readonly runtime;
|
|
396
|
+
private visitor;
|
|
397
|
+
constructor(runtime: AgentRuntime);
|
|
398
|
+
beforeInference(context: RuntimeContext): Promise<void>;
|
|
399
|
+
afterInference(context: RuntimeContext): Promise<void>;
|
|
400
|
+
beforeFunctionCall(context: RuntimeContext): Promise<void>;
|
|
401
|
+
afterFunctionCall(context: RuntimeContext): Promise<void>;
|
|
402
|
+
beforeModelMessage(context: RuntimeContext): Promise<void>;
|
|
403
|
+
afterModelMessage(context: RuntimeContext): Promise<void>;
|
|
404
|
+
onStart(context: RuntimeContext): Promise<void>;
|
|
405
|
+
setInferenceVisitor(visitor: InferenceVisitor): void;
|
|
406
|
+
run(userMessage: string, model: GenerativeModel & ReasoningEffort<string> & ToolCallingCapability, context: Context): Promise<void>;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export { type Action, Agent, AgentRuntime, type AsyncAction, type AsyncRule, BaseCondition, type Condition, Context, ContextItem, type ContextRepository, DeveloperMessage, FunctionCall, FunctionCallOutput, type GenerativeModel, Gpt54, Gpt54Mini, Gpt54Nano, If, InMemoryContextRepository, InferenceRequest, InferenceResponse, type InferenceVisitor, InputTokenDetails, Loop, ModelMessage, OpenAIResponses, OutputTokenDetails, Reasoning, TokenUsage, type Tool, UserMessage };
|
package/dist/index.js
CHANGED
|
@@ -30,6 +30,8 @@ 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
|
+
AgentRuntime: () => AgentRuntime,
|
|
33
35
|
BaseCondition: () => BaseCondition,
|
|
34
36
|
Context: () => Context,
|
|
35
37
|
ContextItem: () => ContextItem,
|
|
@@ -606,8 +608,355 @@ var InMemoryContextRepository = class {
|
|
|
606
608
|
});
|
|
607
609
|
}
|
|
608
610
|
};
|
|
611
|
+
|
|
612
|
+
// src/domain/agent-loop/execution.ts
|
|
613
|
+
var Execution = class _Execution {
|
|
614
|
+
constructor(executionId, currentStateId, previousStateId, status, stepCount, retryCounts, history) {
|
|
615
|
+
this.executionId = executionId;
|
|
616
|
+
this.currentStateId = currentStateId;
|
|
617
|
+
this.previousStateId = previousStateId;
|
|
618
|
+
this.status = status;
|
|
619
|
+
this.stepCount = stepCount;
|
|
620
|
+
this.retryCounts = retryCounts;
|
|
621
|
+
this.history = history;
|
|
622
|
+
}
|
|
623
|
+
isTerminal() {
|
|
624
|
+
return this.status == 1 /* COMPLETED */ || this.status == 2 /* FAILED */;
|
|
625
|
+
}
|
|
626
|
+
static create() {
|
|
627
|
+
const executionId = crypto.randomUUID();
|
|
628
|
+
const currentStateId = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
|
|
629
|
+
const previousStateId = null;
|
|
630
|
+
const status = 0 /* RUNNING */;
|
|
631
|
+
const stepCount = 0;
|
|
632
|
+
const retryCounts = /* @__PURE__ */ new Map();
|
|
633
|
+
const history = [];
|
|
634
|
+
return new _Execution(executionId, currentStateId, previousStateId, status, stepCount, retryCounts, history);
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
// src/domain/agent-loop/transition/go-to.ts
|
|
639
|
+
var GoTo = class {
|
|
640
|
+
constructor(next) {
|
|
641
|
+
this.next = next;
|
|
642
|
+
}
|
|
643
|
+
async apply(context) {
|
|
644
|
+
const execution = context.execution;
|
|
645
|
+
execution.previousStateId = execution.currentStateId;
|
|
646
|
+
execution.currentStateId = this.next;
|
|
647
|
+
execution.stepCount++;
|
|
648
|
+
execution.history.push({
|
|
649
|
+
fromState: execution.previousStateId,
|
|
650
|
+
toState: execution.currentStateId,
|
|
651
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
652
|
+
duration: 0,
|
|
653
|
+
error: null
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
// src/domain/agent-loop/transition/fail.ts
|
|
659
|
+
var Fail = class {
|
|
660
|
+
constructor(error) {
|
|
661
|
+
this.error = error;
|
|
662
|
+
}
|
|
663
|
+
async apply(context) {
|
|
664
|
+
const execution = context.execution;
|
|
665
|
+
execution.status = 2 /* FAILED */;
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
// src/domain/agent-loop/state/function-call.ts
|
|
670
|
+
var FunctionCallState = class {
|
|
671
|
+
constructor() {
|
|
672
|
+
this.id = "FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */;
|
|
673
|
+
this.beforeHookId = "BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */;
|
|
674
|
+
this.afterHookId = "AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */;
|
|
675
|
+
}
|
|
676
|
+
getDetails() {
|
|
677
|
+
return {
|
|
678
|
+
before: this.beforeHookId,
|
|
679
|
+
stateId: this.id,
|
|
680
|
+
after: this.afterHookId
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
validateEntry(runtime) {
|
|
684
|
+
var _a;
|
|
685
|
+
const functionCall = (_a = runtime.inferenceResponse) == null ? void 0 : _a.contextItems.find((item) => item.getType() === "function_call");
|
|
686
|
+
if (!functionCall || !(functionCall instanceof FunctionCall)) {
|
|
687
|
+
throw new Error("Function call not found");
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
next(runtime) {
|
|
691
|
+
const functionCallOutput = runtime.functionCallOutput;
|
|
692
|
+
if (!functionCallOutput) {
|
|
693
|
+
return new Fail("Function call output not found");
|
|
694
|
+
}
|
|
695
|
+
runtime.context.addItem(functionCallOutput);
|
|
696
|
+
return new GoTo("INFERENCE_PENDING" /* INFERENCE_PENDING */);
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
// src/domain/agent-loop/transition/complete.ts
|
|
701
|
+
var Complete = class {
|
|
702
|
+
constructor(result) {
|
|
703
|
+
this.result = result;
|
|
704
|
+
}
|
|
705
|
+
async apply(context) {
|
|
706
|
+
const execution = context.execution;
|
|
707
|
+
execution.status = 1 /* COMPLETED */;
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
// src/domain/agent-loop/state/model-message.ts
|
|
712
|
+
var ModelMessageState = class {
|
|
713
|
+
constructor() {
|
|
714
|
+
this.id = "MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */;
|
|
715
|
+
this.beforeHookId = "BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */;
|
|
716
|
+
this.afterHookId = "AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */;
|
|
717
|
+
}
|
|
718
|
+
getDetails() {
|
|
719
|
+
return {
|
|
720
|
+
before: this.beforeHookId,
|
|
721
|
+
stateId: this.id,
|
|
722
|
+
after: this.afterHookId
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
validateEntry(runtime) {
|
|
726
|
+
if (!runtime.modelMessage) {
|
|
727
|
+
throw new Error("Model message not found");
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
next(runtime) {
|
|
731
|
+
if (!runtime.modelMessage) {
|
|
732
|
+
return new Fail("Model message not found");
|
|
733
|
+
}
|
|
734
|
+
return new Complete(runtime.modelMessage.content.text);
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
// src/domain/agent-loop/state/inference-pending.ts
|
|
739
|
+
var InferencePendingState = class {
|
|
740
|
+
constructor() {
|
|
741
|
+
this.id = "INFERENCE_PENDING" /* INFERENCE_PENDING */;
|
|
742
|
+
this.beforeHookId = "BEFORE_INFERENCE" /* BEFORE_INFERENCE */;
|
|
743
|
+
this.afterHookId = "AFTER_INFERENCE" /* AFTER_INFERENCE */;
|
|
744
|
+
}
|
|
745
|
+
getDetails() {
|
|
746
|
+
return {
|
|
747
|
+
before: this.beforeHookId,
|
|
748
|
+
stateId: this.id,
|
|
749
|
+
after: this.afterHookId
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
validateEntry(runtime) {
|
|
753
|
+
const inferenceRequest = runtime.inferenceRequest;
|
|
754
|
+
if (!inferenceRequest) {
|
|
755
|
+
throw new Error("Inference request not found");
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
next(runtime) {
|
|
759
|
+
const inferenceResponse = runtime.inferenceResponse;
|
|
760
|
+
if (!inferenceResponse) {
|
|
761
|
+
return new Fail("Inference response not found");
|
|
762
|
+
}
|
|
763
|
+
if (inferenceResponse instanceof FunctionCall) {
|
|
764
|
+
return new GoTo("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */);
|
|
765
|
+
}
|
|
766
|
+
if (inferenceResponse instanceof ModelMessage) {
|
|
767
|
+
return new GoTo("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */);
|
|
768
|
+
}
|
|
769
|
+
return new Fail("Invalid response type");
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
// src/domain/agent-loop/loop.ts
|
|
774
|
+
var AgentLoop = class {
|
|
775
|
+
constructor() {
|
|
776
|
+
this.states = /* @__PURE__ */ new Map();
|
|
777
|
+
this.states.set("INFERENCE_PENDING" /* INFERENCE_PENDING */, new InferencePendingState());
|
|
778
|
+
this.states.set("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, new FunctionCallState());
|
|
779
|
+
this.states.set("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, new ModelMessageState());
|
|
780
|
+
}
|
|
781
|
+
validateEntry(runtime) {
|
|
782
|
+
const state = this.states.get(runtime.execution.currentStateId);
|
|
783
|
+
if (!state) {
|
|
784
|
+
throw new Error(`State ${runtime.execution.currentStateId} not found`);
|
|
785
|
+
}
|
|
786
|
+
state.validateEntry(runtime);
|
|
787
|
+
}
|
|
788
|
+
getStateDetails(runtime) {
|
|
789
|
+
const state = this.states.get(runtime.execution.currentStateId);
|
|
790
|
+
if (!state) {
|
|
791
|
+
throw new Error(`State ${runtime.execution.currentStateId} not found`);
|
|
792
|
+
}
|
|
793
|
+
return state.getDetails();
|
|
794
|
+
}
|
|
795
|
+
next(runtime) {
|
|
796
|
+
const state = this.states.get(runtime.execution.currentStateId);
|
|
797
|
+
if (!state) {
|
|
798
|
+
throw new Error(`State ${runtime.execution.currentStateId} not found`);
|
|
799
|
+
}
|
|
800
|
+
return state.next(runtime);
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
// src/domain/agent-loop/hooks/hooks-registry.ts
|
|
805
|
+
var HooksRegistry = class {
|
|
806
|
+
constructor() {
|
|
807
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
808
|
+
}
|
|
809
|
+
getHandler(hookId) {
|
|
810
|
+
const handler = this.handlers.get(hookId);
|
|
811
|
+
if (!handler) {
|
|
812
|
+
throw new Error(`Hook handler for hook ${hookId} not found`);
|
|
813
|
+
}
|
|
814
|
+
return handler;
|
|
815
|
+
}
|
|
816
|
+
registerHandler(hookId, handler) {
|
|
817
|
+
this.handlers.set(hookId, handler);
|
|
818
|
+
}
|
|
819
|
+
unregisterHandler(hookId) {
|
|
820
|
+
this.handlers.delete(hookId);
|
|
821
|
+
}
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
// src/domain/agent-loop/state/state-registry.ts
|
|
825
|
+
var StateHandlerRegistry = class {
|
|
826
|
+
constructor() {
|
|
827
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
828
|
+
}
|
|
829
|
+
getHandler(stateId) {
|
|
830
|
+
const handler = this.handlers.get(stateId);
|
|
831
|
+
if (!handler) {
|
|
832
|
+
throw new Error(`Handler for state ${stateId} not found`);
|
|
833
|
+
}
|
|
834
|
+
return handler;
|
|
835
|
+
}
|
|
836
|
+
registerHandler(stateId, handler) {
|
|
837
|
+
this.handlers.set(stateId, handler);
|
|
838
|
+
}
|
|
839
|
+
unregisterHandler(stateId) {
|
|
840
|
+
this.handlers.delete(stateId);
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
|
|
844
|
+
// src/application/agent-runtime.ts
|
|
845
|
+
var AgentRuntime = class {
|
|
846
|
+
constructor() {
|
|
847
|
+
this.hooksRegistry = new HooksRegistry();
|
|
848
|
+
this.stateHandlerRegistry = new StateHandlerRegistry();
|
|
849
|
+
this.stateHandlerRegistry.registerHandler("INFERENCE_PENDING" /* INFERENCE_PENDING */, this.onInferencePending);
|
|
850
|
+
this.stateHandlerRegistry.registerHandler("FUNCTION_CALL_PENDING" /* FUNCTION_CALL_PENDING */, this.onFunctionCallPending);
|
|
851
|
+
this.stateHandlerRegistry.registerHandler("MODEL_MESSAGE_RECEIVED" /* MODEL_MESSAGE_RECEIVED */, this.onModelMessageReceived);
|
|
852
|
+
}
|
|
853
|
+
async onInferencePending(context) {
|
|
854
|
+
const openaiResponses = new OpenAIResponses();
|
|
855
|
+
const inferenceRequest = new InferenceRequest(context.model, context.context);
|
|
856
|
+
const inferenceResponse = await openaiResponses.infer(inferenceRequest);
|
|
857
|
+
context.inferenceResponse = inferenceResponse;
|
|
858
|
+
return Promise.resolve();
|
|
859
|
+
}
|
|
860
|
+
async onFunctionCallPending(context) {
|
|
861
|
+
var _a;
|
|
862
|
+
const functionCall = (_a = context.inferenceResponse) == null ? void 0 : _a.contextItems.find((item) => item.getType() === "function_call");
|
|
863
|
+
if (!functionCall || !(functionCall instanceof FunctionCall)) {
|
|
864
|
+
throw new Error("Function call not found");
|
|
865
|
+
}
|
|
866
|
+
const tool = context.model.getTools().find((tool2) => tool2.name === functionCall.name);
|
|
867
|
+
if (!tool) {
|
|
868
|
+
throw new Error("Function not found");
|
|
869
|
+
}
|
|
870
|
+
const functionCallOutput = await tool.invoke(functionCall.args);
|
|
871
|
+
context.functionCallOutput = FunctionCallOutput.create(functionCall.callId, functionCallOutput);
|
|
872
|
+
return Promise.resolve();
|
|
873
|
+
}
|
|
874
|
+
async onModelMessageReceived(context) {
|
|
875
|
+
return Promise.resolve();
|
|
876
|
+
}
|
|
877
|
+
async on(hookId, callback) {
|
|
878
|
+
this.hooksRegistry.registerHandler(hookId, callback);
|
|
879
|
+
}
|
|
880
|
+
async start(userMessage, model, context) {
|
|
881
|
+
const execution = Execution.create();
|
|
882
|
+
const loop = new AgentLoop();
|
|
883
|
+
const runtimeContext = {
|
|
884
|
+
execution,
|
|
885
|
+
userMessage,
|
|
886
|
+
model,
|
|
887
|
+
context
|
|
888
|
+
};
|
|
889
|
+
while (!execution.isTerminal()) {
|
|
890
|
+
const transition = loop.next(runtimeContext);
|
|
891
|
+
await transition.apply(runtimeContext);
|
|
892
|
+
try {
|
|
893
|
+
const stateDetails = loop.getStateDetails(runtimeContext);
|
|
894
|
+
if (stateDetails.before) {
|
|
895
|
+
const handler = this.hooksRegistry.getHandler(stateDetails.before);
|
|
896
|
+
await handler(runtimeContext);
|
|
897
|
+
}
|
|
898
|
+
if (stateDetails.after) {
|
|
899
|
+
const handler = this.hooksRegistry.getHandler(stateDetails.after);
|
|
900
|
+
await handler(runtimeContext);
|
|
901
|
+
}
|
|
902
|
+
} catch (error) {
|
|
903
|
+
execution.status = 2 /* FAILED */;
|
|
904
|
+
break;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
// src/application/agent.ts
|
|
911
|
+
var Agent = class {
|
|
912
|
+
constructor(runtime) {
|
|
913
|
+
this.runtime = runtime;
|
|
914
|
+
this.runtime.on("BEFORE_INFERENCE" /* BEFORE_INFERENCE */, this.beforeInference);
|
|
915
|
+
this.runtime.on("AFTER_INFERENCE" /* AFTER_INFERENCE */, this.afterInference);
|
|
916
|
+
this.runtime.on("BEFORE_FUNCTION_CALL" /* BEFORE_FUNCTION_CALL */, this.beforeFunctionCall);
|
|
917
|
+
this.runtime.on("AFTER_FUNCTION_CALL" /* AFTER_FUNCTION_CALL */, this.afterFunctionCall);
|
|
918
|
+
this.runtime.on("BEFORE_MODEL_MESSAGE" /* BEFORE_MODEL_MESSAGE */, this.beforeModelMessage);
|
|
919
|
+
this.runtime.on("AFTER_MODEL_MESSAGE" /* AFTER_MODEL_MESSAGE */, this.afterModelMessage);
|
|
920
|
+
}
|
|
921
|
+
async beforeInference(context) {
|
|
922
|
+
return Promise.resolve();
|
|
923
|
+
}
|
|
924
|
+
async afterInference(context) {
|
|
925
|
+
const inferenceResponse = context.inferenceResponse;
|
|
926
|
+
if (inferenceResponse && this.visitor) {
|
|
927
|
+
await this.visitor.afterInference(inferenceResponse);
|
|
928
|
+
}
|
|
929
|
+
return Promise.resolve();
|
|
930
|
+
}
|
|
931
|
+
async beforeFunctionCall(context) {
|
|
932
|
+
return Promise.resolve();
|
|
933
|
+
}
|
|
934
|
+
async afterFunctionCall(context) {
|
|
935
|
+
return Promise.resolve();
|
|
936
|
+
}
|
|
937
|
+
async beforeModelMessage(context) {
|
|
938
|
+
return Promise.resolve();
|
|
939
|
+
}
|
|
940
|
+
async afterModelMessage(context) {
|
|
941
|
+
return Promise.resolve();
|
|
942
|
+
}
|
|
943
|
+
async onStart(context) {
|
|
944
|
+
var _a;
|
|
945
|
+
await ((_a = this.visitor) == null ? void 0 : _a.onStart(context));
|
|
946
|
+
return Promise.resolve();
|
|
947
|
+
}
|
|
948
|
+
setInferenceVisitor(visitor) {
|
|
949
|
+
this.visitor = visitor;
|
|
950
|
+
}
|
|
951
|
+
async run(userMessage, model, context) {
|
|
952
|
+
const userMessageItem = UserMessage.create(userMessage);
|
|
953
|
+
return this.runtime.start(userMessageItem, model, context);
|
|
954
|
+
}
|
|
955
|
+
};
|
|
609
956
|
// Annotate the CommonJS export names for ESM import in node:
|
|
610
957
|
0 && (module.exports = {
|
|
958
|
+
Agent,
|
|
959
|
+
AgentRuntime,
|
|
611
960
|
BaseCondition,
|
|
612
961
|
Context,
|
|
613
962
|
ContextItem,
|