@mozaik-ai/core 4.0.0-beta.5 → 4.0.0-beta.7

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 CHANGED
@@ -129,6 +129,46 @@ interface FunctionTool {
129
129
  }
130
130
  type Tool = FunctionTool;
131
131
 
132
+ type SituationContext$1<TEvent extends SemanticEvent = SemanticEvent> = {
133
+ readonly event: TEvent;
134
+ readonly participant: Participant;
135
+ };
136
+ declare abstract class SituationSpecification<TEvent extends SemanticEvent = SemanticEvent> {
137
+ abstract isSatisfiedBy(situationContext: SituationContext$1<TEvent>): boolean;
138
+ and(other: SituationSpecification<TEvent>): SituationSpecification<TEvent>;
139
+ or(other: SituationSpecification<TEvent>): SituationSpecification<TEvent>;
140
+ not(): SituationSpecification<TEvent>;
141
+ }
142
+
143
+ interface SituationHandler {
144
+ readonly specification: SituationSpecification;
145
+ readonly processor: SituationProcessor;
146
+ }
147
+ type SituationContext<TEvent extends SemanticEvent = SemanticEvent> = {
148
+ readonly event: TEvent;
149
+ readonly participant: Participant;
150
+ };
151
+ interface SituationProcessor {
152
+ apply(context: SituationContext): void | Promise<void>;
153
+ }
154
+
155
+ type ParticipantRole = "agent" | "human";
156
+ type ParticipantManifest = {
157
+ readonly id: string;
158
+ readonly name: string;
159
+ readonly role: ParticipantRole;
160
+ readonly capabilities?: readonly string[];
161
+ };
162
+ declare abstract class Participant {
163
+ private readonly manifest;
164
+ private handlers;
165
+ protected constructor(manifest: ParticipantManifest, handlers: SituationHandler[]);
166
+ getManifest(): ParticipantManifest;
167
+ getId(): string;
168
+ getHandlers(): SituationHandler[];
169
+ setHandlers(handlers: SituationHandler[]): void;
170
+ }
171
+
132
172
  declare class SemanticEvent<TType extends string = string, TPayload = unknown> {
133
173
  readonly type: TType;
134
174
  readonly producerId: string;
@@ -141,6 +181,71 @@ declare class SemanticEvent<TType extends string = string, TPayload = unknown> {
141
181
  interface FunctionCallRunner {
142
182
  run(call: FunctionCallItem, tool: Tool): Promise<FunctionCallOutputItem>;
143
183
  }
184
+ interface FunctionCallParams {
185
+ call: FunctionCallItem;
186
+ inferenceInput: InferenceInput;
187
+ }
188
+
189
+ type LoopStateId = "context_update" | "inference" | "inference_streaming" | "function_call" | "model_message" | "idle";
190
+ type ReceivedMessage = {
191
+ content: string;
192
+ input: InferenceInput;
193
+ };
194
+ interface FunctionCallExecutionOutput {
195
+ item: FunctionCallOutputItem;
196
+ }
197
+ interface ModelMessageParams {
198
+ answer: ModelMessageItem;
199
+ }
200
+ interface LoopStateContract {
201
+ context_update: {
202
+ input: ReceivedMessage;
203
+ output: InferenceInput;
204
+ };
205
+ inference: {
206
+ input: InferenceInput;
207
+ output: InferenceOutput;
208
+ };
209
+ inference_streaming: {
210
+ input: InferenceInput;
211
+ output: InferenceOutput;
212
+ };
213
+ function_call: {
214
+ input: FunctionCallParams;
215
+ output: FunctionCallExecutionOutput;
216
+ };
217
+ model_message: {
218
+ input: ModelMessageParams;
219
+ output: void;
220
+ };
221
+ interception: {
222
+ input: InterceptionParams;
223
+ output: InterceptionOutput;
224
+ };
225
+ idle: {
226
+ input: undefined;
227
+ output: void;
228
+ };
229
+ }
230
+ type InterceptionOutput = {
231
+ type: "continue";
232
+ transition: LoopTransition;
233
+ } | {
234
+ type: "pause";
235
+ pendingTransition: LoopTransition;
236
+ } | {
237
+ type: "stop";
238
+ reason?: string;
239
+ };
240
+ interface InterceptionParams {
241
+ pendingTransition: LoopTransition;
242
+ }
243
+ type LoopTransition<TStateId extends LoopStateId = LoopStateId> = {
244
+ [K in TStateId]: {
245
+ nextStateId: K;
246
+ input: LoopStateContract[K]["input"];
247
+ };
248
+ }[TStateId];
144
249
 
145
250
  type InferenceInput = {
146
251
  model: string;
@@ -189,6 +294,11 @@ type ModelSpecification = {
189
294
  supportsStructuredOutput: boolean;
190
295
  };
191
296
 
297
+ interface RequestValidationRule {
298
+ readonly name: string;
299
+ isValid(inferenceInput: InferenceInput, model: ModelSpecification): boolean;
300
+ }
301
+
192
302
  type StructuredOutputFormat = {
193
303
  name?: string;
194
304
  schema: Record<string, any>;
@@ -294,46 +404,6 @@ declare class SystemMessageItem extends ContextItem {
294
404
  }): SystemMessageItem;
295
405
  }
296
406
 
297
- type SituationContext$1<TEvent extends SemanticEvent = SemanticEvent> = {
298
- readonly event: TEvent;
299
- readonly participant: Participant;
300
- };
301
- declare abstract class SituationSpecification<TEvent extends SemanticEvent = SemanticEvent> {
302
- abstract isSatisfiedBy(situationContext: SituationContext$1<TEvent>): boolean;
303
- and(other: SituationSpecification<TEvent>): SituationSpecification<TEvent>;
304
- or(other: SituationSpecification<TEvent>): SituationSpecification<TEvent>;
305
- not(): SituationSpecification<TEvent>;
306
- }
307
-
308
- interface SituationHandler {
309
- readonly specification: SituationSpecification;
310
- readonly processor: SituationProcessor;
311
- }
312
- type SituationContext<TEvent extends SemanticEvent = SemanticEvent> = {
313
- readonly event: TEvent;
314
- readonly participant: Participant;
315
- };
316
- interface SituationProcessor {
317
- apply(context: SituationContext): void | Promise<void>;
318
- }
319
-
320
- type ParticipantRole = "agent" | "human";
321
- type ParticipantManifest = {
322
- readonly id: string;
323
- readonly name: string;
324
- readonly role: ParticipantRole;
325
- readonly capabilities?: readonly string[];
326
- };
327
- declare abstract class Participant {
328
- private readonly manifest;
329
- private handlers;
330
- protected constructor(manifest: ParticipantManifest, handlers: SituationHandler[]);
331
- getManifest(): ParticipantManifest;
332
- getId(): string;
333
- getHandlers(): SituationHandler[];
334
- setHandlers(handlers: SituationHandler[]): void;
335
- }
336
-
337
407
  declare class Memory {
338
408
  private readonly context;
339
409
  private constructor();
@@ -389,6 +459,11 @@ declare abstract class RuntimeState {
389
459
  getParticipants(): readonly Participant[];
390
460
  }
391
461
 
462
+ interface InterceptionHandler {
463
+ isSatisfiedBy(transition: LoopTransition): boolean;
464
+ handle(transition: LoopTransition): Promise<LoopTransition>;
465
+ }
466
+
392
467
  declare class EventProcessor {
393
468
  process(event: SemanticEvent, consumer: Participant): void;
394
469
  }
@@ -422,7 +497,7 @@ declare function defineRuntime<TRuntimeState extends RuntimeState>(): {
422
497
  leave: (participant: Participant) => void;
423
498
  sendMessage: (message: string, senderId: string) => void;
424
499
  sendEvent: (event: SemanticEvent, senderId: string) => void;
425
- runLoop: (agentId: string, message: string, inferenceInput: InferenceInput) => void;
500
+ runLoop: (agentId: string, message: string, inferenceInput: InferenceInput, interceptionHandler: InterceptionHandler) => void;
426
501
  };
427
502
 
428
503
  declare const supportedModels: GenerativeModel[];
@@ -526,4 +601,18 @@ declare class GeminiGenerateContent implements Endpoint {
526
601
  stream(inferenceInput: InferenceInput): AsyncIterable<SemanticEvent>;
527
602
  }
528
603
 
529
- export { Agent, AnthropicMessages, ContextItem, DeveloperMessageItem, type Endpoint, FunctionCallItem, FunctionCallOutputItem, GeminiGenerateContent, Human, type InferenceInput, type InferenceOutput, type InferenceRunner, type InferenceRunnerConfig, InputTokenDetails, McpClient, type McpServerConfig, McpToolRegistry, type McpToolSpec, ModelContext, type ModelContextRepository, ModelMessageItem, OpenAIChatCompletions, OpenAIResponses, OutputTokenDetails, Participant, ReasoningItem, RuntimeState, SemanticEvent, type SituationContext, type SituationHandler, type SituationProcessor, SituationSpecification, type StructuredOutputFormat, SystemMessageItem, TokenUsage, type Tool, UserMessageItem, createAgent, createHuman, defineRuntime, supportedModels };
604
+ declare class InferenceInputValidator {
605
+ private readonly rules;
606
+ constructor(rules?: RequestValidationRule[]);
607
+ validate(inferenceInput: InferenceInput, model: ModelSpecification): void;
608
+ }
609
+
610
+ declare class DefaultInferenceRunner implements InferenceRunner {
611
+ private readonly supportedModels;
612
+ private readonly requestValidator;
613
+ constructor(supportedModels: GenerativeModel[], requestValidator: InferenceInputValidator);
614
+ run(input: InferenceInput): Promise<InferenceOutput>;
615
+ stream(input: InferenceInput): AsyncGenerator<SemanticEvent>;
616
+ }
617
+
618
+ export { Agent, AnthropicMessages, ContextItem, DefaultInferenceRunner, DeveloperMessageItem, type Endpoint, FunctionCallItem, FunctionCallOutputItem, GeminiGenerateContent, Human, type InferenceInput, type InferenceOutput, type InferenceRunner, type InferenceRunnerConfig, InputTokenDetails, McpClient, type McpServerConfig, McpToolRegistry, type McpToolSpec, ModelContext, type ModelContextRepository, ModelMessageItem, OpenAIChatCompletions, OpenAIResponses, OutputTokenDetails, Participant, ReasoningItem, RuntimeState, SemanticEvent, type SituationContext, type SituationHandler, type SituationProcessor, SituationSpecification, type StructuredOutputFormat, SystemMessageItem, TokenUsage, type Tool, UserMessageItem, createAgent, createHuman, defineRuntime, supportedModels };
package/dist/index.d.ts CHANGED
@@ -129,6 +129,46 @@ interface FunctionTool {
129
129
  }
130
130
  type Tool = FunctionTool;
131
131
 
132
+ type SituationContext$1<TEvent extends SemanticEvent = SemanticEvent> = {
133
+ readonly event: TEvent;
134
+ readonly participant: Participant;
135
+ };
136
+ declare abstract class SituationSpecification<TEvent extends SemanticEvent = SemanticEvent> {
137
+ abstract isSatisfiedBy(situationContext: SituationContext$1<TEvent>): boolean;
138
+ and(other: SituationSpecification<TEvent>): SituationSpecification<TEvent>;
139
+ or(other: SituationSpecification<TEvent>): SituationSpecification<TEvent>;
140
+ not(): SituationSpecification<TEvent>;
141
+ }
142
+
143
+ interface SituationHandler {
144
+ readonly specification: SituationSpecification;
145
+ readonly processor: SituationProcessor;
146
+ }
147
+ type SituationContext<TEvent extends SemanticEvent = SemanticEvent> = {
148
+ readonly event: TEvent;
149
+ readonly participant: Participant;
150
+ };
151
+ interface SituationProcessor {
152
+ apply(context: SituationContext): void | Promise<void>;
153
+ }
154
+
155
+ type ParticipantRole = "agent" | "human";
156
+ type ParticipantManifest = {
157
+ readonly id: string;
158
+ readonly name: string;
159
+ readonly role: ParticipantRole;
160
+ readonly capabilities?: readonly string[];
161
+ };
162
+ declare abstract class Participant {
163
+ private readonly manifest;
164
+ private handlers;
165
+ protected constructor(manifest: ParticipantManifest, handlers: SituationHandler[]);
166
+ getManifest(): ParticipantManifest;
167
+ getId(): string;
168
+ getHandlers(): SituationHandler[];
169
+ setHandlers(handlers: SituationHandler[]): void;
170
+ }
171
+
132
172
  declare class SemanticEvent<TType extends string = string, TPayload = unknown> {
133
173
  readonly type: TType;
134
174
  readonly producerId: string;
@@ -141,6 +181,71 @@ declare class SemanticEvent<TType extends string = string, TPayload = unknown> {
141
181
  interface FunctionCallRunner {
142
182
  run(call: FunctionCallItem, tool: Tool): Promise<FunctionCallOutputItem>;
143
183
  }
184
+ interface FunctionCallParams {
185
+ call: FunctionCallItem;
186
+ inferenceInput: InferenceInput;
187
+ }
188
+
189
+ type LoopStateId = "context_update" | "inference" | "inference_streaming" | "function_call" | "model_message" | "idle";
190
+ type ReceivedMessage = {
191
+ content: string;
192
+ input: InferenceInput;
193
+ };
194
+ interface FunctionCallExecutionOutput {
195
+ item: FunctionCallOutputItem;
196
+ }
197
+ interface ModelMessageParams {
198
+ answer: ModelMessageItem;
199
+ }
200
+ interface LoopStateContract {
201
+ context_update: {
202
+ input: ReceivedMessage;
203
+ output: InferenceInput;
204
+ };
205
+ inference: {
206
+ input: InferenceInput;
207
+ output: InferenceOutput;
208
+ };
209
+ inference_streaming: {
210
+ input: InferenceInput;
211
+ output: InferenceOutput;
212
+ };
213
+ function_call: {
214
+ input: FunctionCallParams;
215
+ output: FunctionCallExecutionOutput;
216
+ };
217
+ model_message: {
218
+ input: ModelMessageParams;
219
+ output: void;
220
+ };
221
+ interception: {
222
+ input: InterceptionParams;
223
+ output: InterceptionOutput;
224
+ };
225
+ idle: {
226
+ input: undefined;
227
+ output: void;
228
+ };
229
+ }
230
+ type InterceptionOutput = {
231
+ type: "continue";
232
+ transition: LoopTransition;
233
+ } | {
234
+ type: "pause";
235
+ pendingTransition: LoopTransition;
236
+ } | {
237
+ type: "stop";
238
+ reason?: string;
239
+ };
240
+ interface InterceptionParams {
241
+ pendingTransition: LoopTransition;
242
+ }
243
+ type LoopTransition<TStateId extends LoopStateId = LoopStateId> = {
244
+ [K in TStateId]: {
245
+ nextStateId: K;
246
+ input: LoopStateContract[K]["input"];
247
+ };
248
+ }[TStateId];
144
249
 
145
250
  type InferenceInput = {
146
251
  model: string;
@@ -189,6 +294,11 @@ type ModelSpecification = {
189
294
  supportsStructuredOutput: boolean;
190
295
  };
191
296
 
297
+ interface RequestValidationRule {
298
+ readonly name: string;
299
+ isValid(inferenceInput: InferenceInput, model: ModelSpecification): boolean;
300
+ }
301
+
192
302
  type StructuredOutputFormat = {
193
303
  name?: string;
194
304
  schema: Record<string, any>;
@@ -294,46 +404,6 @@ declare class SystemMessageItem extends ContextItem {
294
404
  }): SystemMessageItem;
295
405
  }
296
406
 
297
- type SituationContext$1<TEvent extends SemanticEvent = SemanticEvent> = {
298
- readonly event: TEvent;
299
- readonly participant: Participant;
300
- };
301
- declare abstract class SituationSpecification<TEvent extends SemanticEvent = SemanticEvent> {
302
- abstract isSatisfiedBy(situationContext: SituationContext$1<TEvent>): boolean;
303
- and(other: SituationSpecification<TEvent>): SituationSpecification<TEvent>;
304
- or(other: SituationSpecification<TEvent>): SituationSpecification<TEvent>;
305
- not(): SituationSpecification<TEvent>;
306
- }
307
-
308
- interface SituationHandler {
309
- readonly specification: SituationSpecification;
310
- readonly processor: SituationProcessor;
311
- }
312
- type SituationContext<TEvent extends SemanticEvent = SemanticEvent> = {
313
- readonly event: TEvent;
314
- readonly participant: Participant;
315
- };
316
- interface SituationProcessor {
317
- apply(context: SituationContext): void | Promise<void>;
318
- }
319
-
320
- type ParticipantRole = "agent" | "human";
321
- type ParticipantManifest = {
322
- readonly id: string;
323
- readonly name: string;
324
- readonly role: ParticipantRole;
325
- readonly capabilities?: readonly string[];
326
- };
327
- declare abstract class Participant {
328
- private readonly manifest;
329
- private handlers;
330
- protected constructor(manifest: ParticipantManifest, handlers: SituationHandler[]);
331
- getManifest(): ParticipantManifest;
332
- getId(): string;
333
- getHandlers(): SituationHandler[];
334
- setHandlers(handlers: SituationHandler[]): void;
335
- }
336
-
337
407
  declare class Memory {
338
408
  private readonly context;
339
409
  private constructor();
@@ -389,6 +459,11 @@ declare abstract class RuntimeState {
389
459
  getParticipants(): readonly Participant[];
390
460
  }
391
461
 
462
+ interface InterceptionHandler {
463
+ isSatisfiedBy(transition: LoopTransition): boolean;
464
+ handle(transition: LoopTransition): Promise<LoopTransition>;
465
+ }
466
+
392
467
  declare class EventProcessor {
393
468
  process(event: SemanticEvent, consumer: Participant): void;
394
469
  }
@@ -422,7 +497,7 @@ declare function defineRuntime<TRuntimeState extends RuntimeState>(): {
422
497
  leave: (participant: Participant) => void;
423
498
  sendMessage: (message: string, senderId: string) => void;
424
499
  sendEvent: (event: SemanticEvent, senderId: string) => void;
425
- runLoop: (agentId: string, message: string, inferenceInput: InferenceInput) => void;
500
+ runLoop: (agentId: string, message: string, inferenceInput: InferenceInput, interceptionHandler: InterceptionHandler) => void;
426
501
  };
427
502
 
428
503
  declare const supportedModels: GenerativeModel[];
@@ -526,4 +601,18 @@ declare class GeminiGenerateContent implements Endpoint {
526
601
  stream(inferenceInput: InferenceInput): AsyncIterable<SemanticEvent>;
527
602
  }
528
603
 
529
- export { Agent, AnthropicMessages, ContextItem, DeveloperMessageItem, type Endpoint, FunctionCallItem, FunctionCallOutputItem, GeminiGenerateContent, Human, type InferenceInput, type InferenceOutput, type InferenceRunner, type InferenceRunnerConfig, InputTokenDetails, McpClient, type McpServerConfig, McpToolRegistry, type McpToolSpec, ModelContext, type ModelContextRepository, ModelMessageItem, OpenAIChatCompletions, OpenAIResponses, OutputTokenDetails, Participant, ReasoningItem, RuntimeState, SemanticEvent, type SituationContext, type SituationHandler, type SituationProcessor, SituationSpecification, type StructuredOutputFormat, SystemMessageItem, TokenUsage, type Tool, UserMessageItem, createAgent, createHuman, defineRuntime, supportedModels };
604
+ declare class InferenceInputValidator {
605
+ private readonly rules;
606
+ constructor(rules?: RequestValidationRule[]);
607
+ validate(inferenceInput: InferenceInput, model: ModelSpecification): void;
608
+ }
609
+
610
+ declare class DefaultInferenceRunner implements InferenceRunner {
611
+ private readonly supportedModels;
612
+ private readonly requestValidator;
613
+ constructor(supportedModels: GenerativeModel[], requestValidator: InferenceInputValidator);
614
+ run(input: InferenceInput): Promise<InferenceOutput>;
615
+ stream(input: InferenceInput): AsyncGenerator<SemanticEvent>;
616
+ }
617
+
618
+ export { Agent, AnthropicMessages, ContextItem, DefaultInferenceRunner, DeveloperMessageItem, type Endpoint, FunctionCallItem, FunctionCallOutputItem, GeminiGenerateContent, Human, type InferenceInput, type InferenceOutput, type InferenceRunner, type InferenceRunnerConfig, InputTokenDetails, McpClient, type McpServerConfig, McpToolRegistry, type McpToolSpec, ModelContext, type ModelContextRepository, ModelMessageItem, OpenAIChatCompletions, OpenAIResponses, OutputTokenDetails, Participant, ReasoningItem, RuntimeState, SemanticEvent, type SituationContext, type SituationHandler, type SituationProcessor, SituationSpecification, type StructuredOutputFormat, SystemMessageItem, TokenUsage, type Tool, UserMessageItem, createAgent, createHuman, defineRuntime, supportedModels };
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ __export(index_exports, {
33
33
  Agent: () => Agent,
34
34
  AnthropicMessages: () => AnthropicMessages,
35
35
  ContextItem: () => ContextItem,
36
+ DefaultInferenceRunner: () => DefaultInferenceRunner,
36
37
  DeveloperMessageItem: () => DeveloperMessageItem,
37
38
  FunctionCallItem: () => FunctionCallItem,
38
39
  FunctionCallOutputItem: () => FunctionCallOutputItem,
@@ -377,6 +378,16 @@ var SemanticEvent = class _SemanticEvent {
377
378
  return new _SemanticEvent(type, producerId, occurredAt, payload);
378
379
  }
379
380
  };
381
+ var ParticipantJoinedEvent = class extends SemanticEvent {
382
+ static init(manifest) {
383
+ return SemanticEvent.create("participant.joined", manifest.id, manifest);
384
+ }
385
+ };
386
+ var ParticipantLeftEvent = class extends SemanticEvent {
387
+ static init(manifest) {
388
+ return SemanticEvent.create("participant.left", manifest.id, manifest);
389
+ }
390
+ };
380
391
  var MessageSentEvent = class extends SemanticEvent {
381
392
  static init(producerId, message) {
382
393
  return SemanticEvent.create("message.sent", producerId, { message });
@@ -535,9 +546,11 @@ var RuntimeService = class {
535
546
  }
536
547
  join(participant) {
537
548
  this.state.addParticipant(participant);
549
+ this.publish(ParticipantJoinedEvent.init(participant.getManifest()));
538
550
  }
539
551
  leave(participant) {
540
552
  this.state.removeParticipant(participant);
553
+ this.publish(ParticipantLeftEvent.init(participant.getManifest()));
541
554
  }
542
555
  publish(event) {
543
556
  for (const participant of this.state.getParticipants()) {
@@ -601,28 +614,43 @@ function createSendMessage(resolveRuntime) {
601
614
  }
602
615
 
603
616
  // src/domain/agentic-environment/loop/agent-loop.ts
604
- var AgentLoop = class {
605
- constructor(stateExecutor, transitionResolver, loopVisitor) {
617
+ var AgentLoop = class _AgentLoop {
618
+ constructor(loopId, stateExecutor, transitionResolver, interceptionHandler) {
619
+ this.loopId = loopId;
606
620
  this.stateExecutor = stateExecutor;
607
621
  this.transitionResolver = transitionResolver;
608
- this.loopVisitor = loopVisitor;
622
+ this.interceptionHandler = interceptionHandler;
609
623
  }
610
- async run(message) {
624
+ async run(message, loopVisitor) {
611
625
  let transition = {
612
- nextStateId: "context_preparation",
626
+ nextStateId: "context_update",
613
627
  input: message
614
628
  };
615
629
  while (transition.nextStateId !== "idle") {
616
- const execution = await this.stateExecutor.execute(transition, this.loopVisitor);
617
- transition = this.transitionResolver.resolve(execution);
630
+ const isInterceptionSatisfied = this.interceptionHandler.isSatisfiedBy(transition);
631
+ if (isInterceptionSatisfied) {
632
+ loopVisitor.visitInterceptionStarted(transition);
633
+ transition = await this.interceptionHandler.handle(transition);
634
+ loopVisitor.visitInterceptionFinished(transition);
635
+ } else {
636
+ const execution = await this.stateExecutor.execute(transition, loopVisitor);
637
+ transition = this.transitionResolver.resolve(execution);
638
+ }
618
639
  }
619
640
  }
641
+ getLoopId() {
642
+ return this.loopId;
643
+ }
644
+ static create(stateExecutor, transitionResolver, interceptionHandler) {
645
+ const loopId = crypto.randomUUID();
646
+ return new _AgentLoop(loopId, stateExecutor, transitionResolver, interceptionHandler);
647
+ }
620
648
  };
621
649
 
622
650
  // src/domain/agentic-environment/loop/state-executor.ts
623
651
  var LoopStateExecutor = class {
624
- constructor(contextPreparationState, inferenceState, inferenceStreamingState, functionCallState, modelMessageState) {
625
- this.contextPreparationState = contextPreparationState;
652
+ constructor(contextUpdateState, inferenceState, inferenceStreamingState, functionCallState, modelMessageState) {
653
+ this.contextUpdateState = contextUpdateState;
626
654
  this.inferenceState = inferenceState;
627
655
  this.inferenceStreamingState = inferenceStreamingState;
628
656
  this.functionCallState = functionCallState;
@@ -630,8 +658,8 @@ var LoopStateExecutor = class {
630
658
  }
631
659
  async execute(transition, loopVisitor) {
632
660
  switch (transition.nextStateId) {
633
- case "context_preparation":
634
- return await this.contextPreparationState.run(transition.input, loopVisitor);
661
+ case "context_update":
662
+ return await this.contextUpdateState.run(transition.input, loopVisitor);
635
663
  case "inference":
636
664
  return await this.inferenceState.run(transition.input, loopVisitor);
637
665
  case "inference_streaming":
@@ -676,19 +704,19 @@ var FunctionCallState = class {
676
704
  }
677
705
  };
678
706
 
679
- // src/application/states/context-preparation.ts
680
- var ContextPreparationState = class {
707
+ // src/application/states/context-update.ts
708
+ var ContextUpdateState = class {
681
709
  constructor() {
682
- this.id = "context_preparation";
710
+ this.id = "context_update";
683
711
  }
684
712
  async run(input, loopVisitor) {
685
- loopVisitor.visitContextPreparationStarted(input);
713
+ loopVisitor.visitContextUpdateStarted(input);
686
714
  const userMessage = UserMessageItem.create(input.content);
687
715
  const output = {
688
716
  ...input.input,
689
717
  context: input.input.context.addContextItems([userMessage])
690
718
  };
691
- loopVisitor.visitContextPreparationCompleted(output);
719
+ loopVisitor.visitContextUpdateCompleted(output);
692
720
  return {
693
721
  stateId: this.id,
694
722
  input,
@@ -729,7 +757,7 @@ var TransitionResolver = class {
729
757
  // src/domain/agentic-environment/loop/transition-specification.ts
730
758
  var MessageReceivedSpecification = class {
731
759
  isSatisfiedBy(execution) {
732
- return execution.stateId === "context_preparation";
760
+ return execution.stateId === "context_update";
733
761
  }
734
762
  };
735
763
  function isInferenceExecution(execution) {
@@ -762,7 +790,7 @@ var ContextPreparationToInferenceRule = class {
762
790
  this.specification = new MessageReceivedSpecification();
763
791
  }
764
792
  createTransition(execution) {
765
- if (execution.stateId !== "context_preparation") {
793
+ if (execution.stateId !== "context_update") {
766
794
  throw new Error("Expected context_preparation execution");
767
795
  }
768
796
  const nextStateId = execution.output.streaming ? "inference_streaming" : "inference";
@@ -850,47 +878,47 @@ var ModelMessageToIdleRule = class {
850
878
 
851
879
  // src/domain/agentic-environment/loop/event-publisher-visitor.ts
852
880
  var EventPublisherLoopVisitor = class {
853
- constructor(agentId, runtime) {
881
+ constructor(agentId, loopId, runtime) {
854
882
  this.agentId = agentId;
883
+ this.loopId = loopId;
855
884
  this.runtime = runtime;
856
885
  }
857
- visitContextPreparationStarted(input) {
858
- console.log("Visiting message received started: ", input);
859
- this.publish("context_preparation.started", input);
886
+ visitContextUpdateStarted(input) {
887
+ this.publish("context_update.started", { ...input, loopId: this.loopId });
860
888
  }
861
- visitContextPreparationCompleted(output) {
862
- console.log("Visiting message received completed: ", output);
863
- this.publish("context_preparation.completed", output);
889
+ visitContextUpdateCompleted(output) {
890
+ this.publish("context_update.completed", { ...output, loopId: this.loopId });
864
891
  }
865
892
  visitInferenceStarted(input) {
866
- console.log("Visiting inference started: ", input);
867
893
  this.publish("inference.started", input);
868
894
  }
869
895
  visitInferenceEvent(event) {
870
896
  this.publish("inference.stream", event);
871
897
  }
872
898
  visitInferenceCompleted(output) {
873
- console.log("Visiting inference completed: ", output);
874
899
  this.publish("inference.completed", output);
875
900
  }
876
901
  visitFunctionCallStarted(input) {
877
- console.log("Visiting function call started: ", input);
878
902
  this.publish("function_call.started", input);
879
903
  }
880
904
  visitFunctionCallCompleted(output) {
881
- console.log("Visiting function call completed: ", output);
882
905
  this.publish("function_call.completed", output);
883
906
  }
884
907
  visitModelAnswer(input) {
885
- console.log("Visiting model answer: ", input.answer);
886
908
  this.publish("model.answer", input);
887
909
  }
910
+ visitInterceptionStarted(transition) {
911
+ this.publish("interception.started", transition);
912
+ }
913
+ visitInterceptionFinished(transition) {
914
+ this.publish("interception.finished", transition);
915
+ }
888
916
  publish(type, payload) {
889
917
  this.runtime.publish({
890
918
  type,
891
919
  producerId: this.agentId,
892
920
  occurredAt: /* @__PURE__ */ new Date(),
893
- payload
921
+ payload: { ...payload, loopId: this.loopId }
894
922
  });
895
923
  }
896
924
  };
@@ -924,7 +952,7 @@ var InferenceStreamingState = class {
924
952
 
925
953
  // src/application/use-cases/run-loop.ts
926
954
  function createRunLoop(resolveRuntime) {
927
- return function runLoop(agentId, message, inferenceInput) {
955
+ return function runLoop(agentId, message, inferenceInput, interceptionHandler) {
928
956
  const runtime = resolveRuntime();
929
957
  const inferenceRunner = runtime.getInferenceRunner();
930
958
  const functionCallRunner = runtime.getFunctionCallRunner();
@@ -937,21 +965,21 @@ function createRunLoop(resolveRuntime) {
937
965
  new ModelMessageToIdleRule()
938
966
  ]);
939
967
  const stateExecutor = new LoopStateExecutor(
940
- new ContextPreparationState(),
968
+ new ContextUpdateState(),
941
969
  new InferenceState(inferenceRunner),
942
970
  new InferenceStreamingState(inferenceRunner),
943
971
  new FunctionCallState(functionCallRunner),
944
972
  new ModelMessageState()
945
973
  );
946
- const agentLoop = new AgentLoop(
947
- stateExecutor,
948
- transitionResolver,
949
- new EventPublisherLoopVisitor(agentId, runtime)
974
+ const agentLoop = AgentLoop.create(stateExecutor, transitionResolver, interceptionHandler);
975
+ const loopVisitor = new EventPublisherLoopVisitor(agentId, agentLoop.getLoopId(), runtime);
976
+ agentLoop.run(
977
+ {
978
+ content: message,
979
+ input: inferenceInput
980
+ },
981
+ loopVisitor
950
982
  );
951
- agentLoop.run({
952
- content: message,
953
- input: inferenceInput
954
- });
955
983
  };
956
984
  }
957
985
 
@@ -2218,6 +2246,7 @@ var NotSituationSpecification = class extends SituationSpecification {
2218
2246
  Agent,
2219
2247
  AnthropicMessages,
2220
2248
  ContextItem,
2249
+ DefaultInferenceRunner,
2221
2250
  DeveloperMessageItem,
2222
2251
  FunctionCallItem,
2223
2252
  FunctionCallOutputItem,