@mozaik-ai/core 3.14.0 → 4.0.0-beta.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.js CHANGED
@@ -30,15 +30,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- AgenticEnvironment: () => AgenticEnvironment,
34
- AgenticError: () => AgenticError,
35
- BaseParticipant: () => BaseParticipant,
36
33
  ContextItem: () => ContextItem,
37
34
  DeveloperMessageItem: () => DeveloperMessageItem,
38
35
  FunctionCallItem: () => FunctionCallItem,
39
36
  FunctionCallOutputItem: () => FunctionCallOutputItem,
40
- InMemoryModelContextRepository: () => InMemoryModelContextRepository,
41
- InferenceResponse: () => InferenceResponse,
42
37
  InputTokenDetails: () => InputTokenDetails,
43
38
  McpClient: () => McpClient,
44
39
  McpToolRegistry: () => McpToolRegistry,
@@ -47,52 +42,40 @@ __export(index_exports, {
47
42
  OutputTokenDetails: () => OutputTokenDetails,
48
43
  Participant: () => Participant,
49
44
  ReasoningItem: () => ReasoningItem,
45
+ RuntimeState: () => RuntimeState,
50
46
  SemanticEvent: () => SemanticEvent,
51
47
  SystemMessageItem: () => SystemMessageItem,
52
48
  TokenUsage: () => TokenUsage,
53
49
  UserMessageItem: () => UserMessageItem,
54
- executeFunctionCall: () => executeFunctionCall,
55
- runInference: () => runInference,
56
- sendMessage: () => sendMessage
50
+ createAgent: () => createAgent,
51
+ createHuman: () => createHuman,
52
+ defineRuntime: () => defineRuntime
57
53
  });
58
54
  module.exports = __toCommonJS(index_exports);
59
55
 
60
56
  // src/domain/model-context/model-context.ts
61
57
  var ModelContext = class _ModelContext {
62
- constructor(id, projectId, items) {
63
- this.projectId = projectId;
58
+ constructor(id, items) {
64
59
  this.id = id;
65
60
  this.items = items;
66
61
  }
67
- addContextItem(item) {
68
- this.items.push(item);
69
- return this;
70
- }
71
- applyModelOutput(items) {
72
- for (const item of items) {
73
- const itemType = item.getType();
74
- if (itemType !== "function_call" && itemType !== "message" && itemType !== "reasoning") {
75
- throw new Error(`Invalid item type: ${itemType}`);
76
- }
77
- }
78
- this.items.push(...items);
79
- return this;
80
- }
81
62
  getItems() {
82
63
  return this.items;
83
64
  }
84
- getLastItem() {
85
- if (this.items.length === 0) {
86
- throw new Error("No items in context");
87
- }
88
- return this.items[this.items.length - 1];
65
+ addContextItems(contextItems) {
66
+ this.items.push(...contextItems);
67
+ return this;
68
+ }
69
+ addItem(item) {
70
+ this.items.push(item);
71
+ return this;
89
72
  }
90
- static create(projectId) {
73
+ static create() {
91
74
  const id = crypto.randomUUID();
92
- return new _ModelContext(id, projectId, []);
75
+ return new _ModelContext(id, []);
93
76
  }
94
77
  static rehydrate(data) {
95
- return new _ModelContext(data.id, data.projectId, data.items);
78
+ return new _ModelContext(data.id, data.items);
96
79
  }
97
80
  };
98
81
 
@@ -217,8 +200,8 @@ var StructuredOutputValidation = class {
217
200
  constructor() {
218
201
  this.name = "structured-output";
219
202
  }
220
- isValid(inferenceParams, model) {
221
- if (inferenceParams.structuredOutput === void 0) {
203
+ isValid(inferenceInput, model) {
204
+ if (inferenceInput.structuredOutput === void 0) {
222
205
  return true;
223
206
  }
224
207
  return model.supportsStructuredOutput;
@@ -355,48 +338,6 @@ var McpToolRegistry = class {
355
338
  }
356
339
  };
357
340
 
358
- // src/infrastructure/repository/in-memory-model-context-repository.ts
359
- var InMemoryModelContextRepository = class {
360
- constructor() {
361
- this.store = /* @__PURE__ */ new Map();
362
- this.projectIndex = /* @__PURE__ */ new Map();
363
- }
364
- async save(context) {
365
- const existing = this.store.get(context.id);
366
- if (existing && existing.projectId !== context.projectId) {
367
- const prevSet = this.projectIndex.get(existing.projectId);
368
- prevSet == null ? void 0 : prevSet.delete(existing.id);
369
- if (prevSet && prevSet.size === 0) this.projectIndex.delete(existing.projectId);
370
- }
371
- this.store.set(context.id, this.clone(context));
372
- let set = this.projectIndex.get(context.projectId);
373
- if (!set) {
374
- set = /* @__PURE__ */ new Set();
375
- this.projectIndex.set(context.projectId, set);
376
- }
377
- set.add(context.id);
378
- }
379
- async get(id) {
380
- const context = this.store.get(id);
381
- if (!context) {
382
- throw new Error(`Context not found: ${id}`);
383
- }
384
- return this.clone(context);
385
- }
386
- async getByProjectId(projectId) {
387
- const ids = this.projectIndex.get(projectId);
388
- if (!ids || ids.size === 0) return [];
389
- return [...ids].map((id) => this.store.get(id)).filter((c) => Boolean(c)).map((c) => this.clone(c));
390
- }
391
- clone(context) {
392
- return ModelContext.rehydrate({
393
- id: context.id,
394
- projectId: context.projectId,
395
- items: [...context.getItems()]
396
- });
397
- }
398
- };
399
-
400
341
  // src/domain/model-context/context-item/client-item/system-message.ts
401
342
  var SystemMessageItem = class _SystemMessageItem extends ContextItem {
402
343
  constructor(content) {
@@ -415,397 +356,598 @@ var SystemMessageItem = class _SystemMessageItem extends ContextItem {
415
356
  }
416
357
  };
417
358
 
418
- // src/domain/agentic-environment/participant.ts
419
- var Participant = class {
420
- constructor() {
421
- this.environments = [];
422
- this.listens = [];
423
- this.active = /* @__PURE__ */ new Map();
359
+ // src/domain/agentic-environment/semantic-event/event.ts
360
+ var SemanticEvent = class _SemanticEvent {
361
+ constructor(type, producerId, occurredAt, payload) {
362
+ this.type = type;
363
+ this.producerId = producerId;
364
+ this.occurredAt = occurredAt;
365
+ this.payload = payload;
424
366
  }
425
- join(environment) {
426
- if (this.isJoinedTo(environment)) {
427
- return;
428
- }
429
- environment.subscribe(this);
430
- this.environments.push(environment);
431
- this.active.set(environment, true);
367
+ static create(type, producerId, payload) {
368
+ const occurredAt = /* @__PURE__ */ new Date();
369
+ return new _SemanticEvent(type, producerId, occurredAt, payload);
432
370
  }
433
- leave(environment) {
434
- if (!this.isJoinedTo(environment)) {
435
- return;
436
- }
437
- environment.unsubscribe(this);
438
- this.environments = this.environments.filter((e) => e !== environment);
439
- this.active.delete(environment);
440
- }
441
- isJoinedTo(environment) {
442
- return this.environments.includes(environment);
371
+ };
372
+ var MessageSentEvent = class extends SemanticEvent {
373
+ static init(producerId, message) {
374
+ return SemanticEvent.create("message.sent", producerId, { message });
443
375
  }
444
- getEnvironments() {
445
- return this.environments;
376
+ };
377
+
378
+ // src/application/states/inference.ts
379
+ var InferenceState = class {
380
+ constructor(inferenceRunner) {
381
+ this.inferenceRunner = inferenceRunner;
382
+ this.id = "inference";
383
+ }
384
+ async run(input, loopVisitor) {
385
+ loopVisitor.visitInferenceStarted(input);
386
+ const output = await this.inferenceRunner.run(input);
387
+ loopVisitor.visitInferenceCompleted(output);
388
+ return {
389
+ stateId: this.id,
390
+ input,
391
+ output
392
+ };
446
393
  }
447
- getListeners() {
448
- return this.listens;
394
+ };
395
+
396
+ // src/domain/agentic-environment/participant/participant.ts
397
+ var Participant = class {
398
+ constructor(manifest, handlers) {
399
+ this.manifest = manifest;
400
+ this.handlers = handlers;
449
401
  }
450
- getEnvironmentState(environment) {
451
- return this.active.get(environment);
402
+ getManifest() {
403
+ return this.manifest;
452
404
  }
453
- markInactive(environment) {
454
- if (this.active.has(environment)) {
455
- this.active.set(environment, false);
456
- }
405
+ getId() {
406
+ return this.manifest.id;
457
407
  }
458
- markActive(environment) {
459
- if (this.active.has(environment)) {
460
- this.active.set(environment, true);
461
- }
408
+ getHandlers() {
409
+ return this.handlers;
462
410
  }
463
411
  };
464
412
 
465
- // src/domain/agentic-environment/errors/base-error.ts
466
- var AgenticError = class extends Error {
467
- constructor({ message, stack, source, enviornment }) {
468
- super();
469
- this.message = message;
470
- this.stack = stack;
471
- this.source = source;
472
- this.environment = enviornment;
413
+ // src/domain/agentic-environment/participant/memory.ts
414
+ var Memory = class _Memory {
415
+ constructor(context) {
416
+ this.context = context;
473
417
  }
474
- getSource() {
475
- return this.source;
418
+ getContext() {
419
+ return this.context;
476
420
  }
477
- getEnvironment() {
478
- return this.environment;
421
+ static create() {
422
+ const context = ModelContext.create();
423
+ return new _Memory(context);
479
424
  }
480
425
  };
481
426
 
482
- // src/domain/agentic-environment/agentic-environment.ts
483
- var AgenticEnvironment = class {
484
- constructor(name, opts) {
485
- this.subscribers = [];
486
- var _a;
487
- this.name = name;
488
- this.options = opts;
489
- this.showLogs = ((_a = this.options) == null ? void 0 : _a.silent) !== false;
490
- }
491
- subscribe(participant) {
492
- this.subscribers.push(participant);
493
- for (const subscriber of this.subscribers) {
494
- if (participant === subscriber) {
495
- participant.onJoined();
496
- } else {
497
- subscriber.onParticipantJoined(participant);
498
- }
499
- }
427
+ // src/domain/agentic-environment/participant/agent.ts
428
+ var Agent = class _Agent extends Participant {
429
+ constructor(manifest, developerMessage, tools, memory, handlers) {
430
+ super(manifest, handlers);
431
+ this.memory = memory;
432
+ this.developerMessage = developerMessage;
433
+ this.tools = tools;
434
+ }
435
+ static create({
436
+ name,
437
+ instruction,
438
+ tools,
439
+ capabilities,
440
+ handlers
441
+ }) {
442
+ const id = crypto.randomUUID();
443
+ const memory = Memory.create();
444
+ const manifest = { id, name, capabilities, role: "agent" };
445
+ const developerMessage = DeveloperMessageItem.create(instruction);
446
+ memory.getContext().addItem(developerMessage);
447
+ return new _Agent(manifest, instruction, tools, memory, handlers);
500
448
  }
501
- unsubscribe(participant) {
502
- const exists = this.subscribers.includes(participant);
503
- if (!exists) {
504
- return;
505
- }
506
- participant.onLeft();
507
- this.subscribers = this.subscribers.filter((p) => p !== participant);
508
- for (const subscriber of this.subscribers) {
509
- subscriber.onParticipantLeft(participant);
510
- }
449
+ getTools() {
450
+ return this.tools;
511
451
  }
512
- deliverFunctionCall(source, item) {
513
- this.deliverToSubscribers(source, {
514
- internal: (src) => src.onFunctionCall(item),
515
- external: (sub) => sub.onExternalFunctionCall(source, item)
516
- });
452
+ getDeveloperMessage() {
453
+ return this.developerMessage;
517
454
  }
518
- deliverModelMessage(source, item) {
519
- this.deliverToSubscribers(source, {
520
- internal: (src) => src.onModelMessage(item),
521
- external: (sub) => sub.onExternalModelMessage(source, item)
522
- });
455
+ getMemory() {
456
+ return this.memory;
523
457
  }
524
- deliverReasoning(source, item) {
525
- this.deliverToSubscribers(source, {
526
- internal: (src) => src.onReasoning(item),
527
- external: (sub) => sub.onExternalReasoning(source, item)
528
- });
458
+ };
459
+
460
+ // src/application/use-cases/create-agent.ts
461
+ function createAgent({
462
+ name,
463
+ capabilities,
464
+ instruction,
465
+ tools,
466
+ handlers
467
+ }) {
468
+ return Agent.create({ name, instruction, capabilities, tools, handlers });
469
+ }
470
+
471
+ // src/domain/agentic-environment/participant/human.ts
472
+ var Human = class _Human extends Participant {
473
+ constructor(manifest, handlers) {
474
+ super(manifest, handlers);
475
+ }
476
+ static create({
477
+ name,
478
+ capabilities,
479
+ handlers
480
+ }) {
481
+ const id = crypto.randomUUID();
482
+ const manifest = { id, name, capabilities, role: "human" };
483
+ return new _Human(manifest, handlers);
529
484
  }
530
- deliverFunctionCallOutput(source, item) {
531
- this.deliverToSubscribers(source, {
532
- internal: (src) => src.onFunctionCallOutput(item),
533
- external: (sub) => sub.onExternalFunctionCallOutput(source, item)
534
- });
485
+ };
486
+
487
+ // src/application/use-cases/create-human.ts
488
+ function createHuman({
489
+ name,
490
+ capabilities,
491
+ handlers
492
+ }) {
493
+ return Human.create({ name, capabilities, handlers });
494
+ }
495
+
496
+ // src/domain/agentic-environment/runtime-state.ts
497
+ var RuntimeState = class {
498
+ constructor() {
499
+ this.participants = /* @__PURE__ */ new Map();
535
500
  }
536
- deliverMessage(source, message) {
537
- this.deliverToSubscribers(source, {
538
- external: (sub) => sub.onMessage(message)
539
- });
501
+ addParticipant(participant) {
502
+ const isExists = this.participants.get(participant.getId());
503
+ if (isExists) return;
504
+ this.participants.set(participant.getId(), participant);
540
505
  }
541
- deliverSemanticEvent(source, item) {
542
- this.deliverToSubscribers(source, {
543
- internal: (src) => src.onInternalEvent(item),
544
- external: (sub) => sub.onExternalEvent(source, item)
545
- });
506
+ removeParticipant(participant) {
507
+ this.participants.delete(participant.getId());
546
508
  }
547
- deliverError(source, error) {
548
- this.deliverToSubscribers(source, {
549
- internal: (src) => {
550
- try {
551
- src.onError(error);
552
- } catch (error2) {
553
- if (this.showLogs) {
554
- console.warn(
555
- `[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
556
- ${JSON.stringify({ error: error2 }, null, 2)}`
557
- );
558
- }
559
- } finally {
560
- src.markInactive(this);
561
- }
562
- },
563
- external: (sub) => {
564
- try {
565
- sub.onParticipantError(source, error);
566
- } catch (error2) {
567
- if (this.showLogs) {
568
- console.warn(
569
- `[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
570
- ${JSON.stringify({ error: error2 }, null, 2)}`
571
- );
572
- }
573
- }
574
- }
575
- });
509
+ getParticipant(id) {
510
+ return this.participants.get(id);
576
511
  }
577
- getListeningParticipants(source) {
578
- return this.subscribers.filter(
579
- (sub) => sub !== source && (sub.getListeners().length === 0 || sub.getListeners().some((listener) => source instanceof listener))
580
- );
512
+ getParticipants() {
513
+ return [...this.participants.values()];
581
514
  }
582
- deliverToSubscribers(source, handlers) {
583
- var _a, _b;
584
- const authorizedListeners = this.getListeningParticipants(source);
585
- for (const subscriber of this.subscribers) {
586
- const isActive = subscriber.getEnvironmentState(this);
587
- if (isActive) {
588
- try {
589
- if (subscriber === source) {
590
- (_a = handlers == null ? void 0 : handlers.internal) == null ? void 0 : _a.call(handlers, subscriber);
591
- } else if (authorizedListeners.includes(subscriber)) {
592
- (_b = handlers == null ? void 0 : handlers.external) == null ? void 0 : _b.call(handlers, subscriber);
593
- }
594
- } catch (error) {
595
- this.handleError(subscriber, error);
596
- }
597
- }
515
+ };
516
+
517
+ // src/application/services/runtime.ts
518
+ var RuntimeService = class {
519
+ constructor(state, processor, inferenceRunner, functionCallRunner) {
520
+ this.state = state;
521
+ this.processor = processor;
522
+ this.inferenceRunner = inferenceRunner;
523
+ this.functionCallRunner = functionCallRunner;
524
+ }
525
+ join(participant) {
526
+ this.state.addParticipant(participant);
527
+ }
528
+ leave(participant) {
529
+ this.state.removeParticipant(participant);
530
+ }
531
+ publish(event) {
532
+ for (const participant of this.state.getParticipants()) {
533
+ this.processor.process(event, participant);
598
534
  }
599
535
  }
600
- handleError(subscriber, error) {
601
- if (error instanceof AgenticError) {
602
- return this.deliverError(subscriber, error);
536
+ getParticipant(id) {
537
+ if (!this.state.getParticipant(id)) {
538
+ throw new Error(`Participant ${id} not found`);
603
539
  }
604
- const agenticError = new AgenticError({
605
- message: error.message,
606
- stack: error.stack,
607
- enviornment: this,
608
- source: subscriber
609
- });
610
- return this.deliverError(subscriber, agenticError);
540
+ return this.state.getParticipant(id);
611
541
  }
612
- };
613
-
614
- // src/domain/model-context/semantic-event/semantic-event.ts
615
- var SemanticEvent = class {
616
- constructor(type, data) {
617
- this.type = type;
618
- this.data = data;
542
+ getInferenceRunner() {
543
+ return this.inferenceRunner;
619
544
  }
620
- getType() {
621
- return this.type;
545
+ getFunctionCallRunner() {
546
+ return this.functionCallRunner;
622
547
  }
623
548
  };
624
549
 
625
- // src/domain/agentic-environment/inference/response.ts
626
- var InferenceResponse = class {
627
- constructor(contextItems, tokenUsage) {
628
- this.contextItems = contextItems;
629
- this.tokenUsage = tokenUsage;
550
+ // src/domain/agentic-environment/semantic-event/event-processor.ts
551
+ var EventProcessor = class {
552
+ process(event, consumer) {
553
+ for (const handler of consumer.getHandlers()) {
554
+ const isSatisfied = handler.specification.isSatisfiedBy({
555
+ event,
556
+ participant: consumer
557
+ });
558
+ if (isSatisfied) {
559
+ handler.processor.apply({ event, participant: consumer });
560
+ }
561
+ }
630
562
  }
631
563
  };
632
564
 
633
- // src/application/services/inference-runner.ts
634
- var StreamingInference = class {
635
- async *run(request, endpoint) {
636
- var _a;
637
- if ((_a = request.signal) == null ? void 0 : _a.aborted) {
638
- return;
565
+ // src/application/use-cases/join.ts
566
+ function createJoin(resolveRuntime) {
567
+ return function join(participant) {
568
+ resolveRuntime().join(participant);
569
+ };
570
+ }
571
+
572
+ // src/application/use-cases/leave.ts
573
+ function createLeave(resolveRuntime) {
574
+ return function leave(participant) {
575
+ resolveRuntime().leave(participant);
576
+ };
577
+ }
578
+
579
+ // src/application/use-cases/send-message.ts
580
+ function createSendMessage(resolveRuntime) {
581
+ return function sendMessage(message, senderId) {
582
+ const runtime = resolveRuntime();
583
+ const participant = runtime.getParticipant(senderId);
584
+ if (!participant) {
585
+ throw new Error(`Participant ${senderId} not found`);
586
+ }
587
+ const userMessage = MessageSentEvent.init(senderId, message);
588
+ runtime.publish(userMessage);
589
+ };
590
+ }
591
+
592
+ // src/domain/agentic-environment/loop/agent-loop.ts
593
+ var AgentLoop = class {
594
+ constructor(stateExecutor, transitionResolver, loopVisitor) {
595
+ this.stateExecutor = stateExecutor;
596
+ this.transitionResolver = transitionResolver;
597
+ this.loopVisitor = loopVisitor;
598
+ }
599
+ async run(message) {
600
+ let transition = {
601
+ nextStateId: "message_received",
602
+ input: message
603
+ };
604
+ while (transition.nextStateId !== "idle") {
605
+ const execution = await this.stateExecutor.execute(transition, this.loopVisitor);
606
+ transition = this.transitionResolver.resolve(execution);
639
607
  }
640
- yield* endpoint.stream(request, request.signal);
641
608
  }
642
609
  };
643
- var NonStreamingInference = class {
644
- async *run(request, endpoint) {
645
- var _a, _b;
646
- if ((_a = request.signal) == null ? void 0 : _a.aborted) {
647
- return;
648
- }
649
- const response = await endpoint.infer(request);
650
- for (const item of response.contextItems) {
651
- if ((_b = request.signal) == null ? void 0 : _b.aborted) {
652
- break;
653
- }
654
- yield item;
610
+
611
+ // src/domain/agentic-environment/loop/state-executor.ts
612
+ var LoopStateExecutor = class {
613
+ constructor(messageReceivedState, inferenceState, functionCallState, modelMessageState) {
614
+ this.messageReceivedState = messageReceivedState;
615
+ this.inferenceState = inferenceState;
616
+ this.functionCallState = functionCallState;
617
+ this.modelMessageState = modelMessageState;
618
+ }
619
+ async execute(transition, loopVisitor) {
620
+ switch (transition.nextStateId) {
621
+ case "message_received":
622
+ return await this.messageReceivedState.run(transition.input, loopVisitor);
623
+ case "inference":
624
+ return await this.inferenceState.run(transition.input, loopVisitor);
625
+ case "function_call":
626
+ return await this.functionCallState.run(transition.input, loopVisitor);
627
+ case "model_message":
628
+ return await this.modelMessageState.run(transition.input, loopVisitor);
655
629
  }
656
630
  }
657
631
  };
658
632
 
659
- // src/application/use-cases/run-inference.ts
660
- var RunInference = class {
661
- constructor(generativeModelRepository2, requestValidator) {
662
- this.generativeModelRepository = generativeModelRepository2;
663
- this.requestValidator = requestValidator;
633
+ // src/application/states/function-call.ts
634
+ var FunctionCallState = class {
635
+ constructor(functionCallRunner) {
636
+ this.functionCallRunner = functionCallRunner;
637
+ this.id = "function_call";
664
638
  }
665
- async execute(inferenceParams) {
639
+ async run(input, loopVisitor) {
666
640
  var _a;
667
- const { caller, environment, signal } = inferenceParams;
668
- const generativeModel = await this.generativeModelRepository.getByModelName(inferenceParams.model);
669
- this.requestValidator.validate(inferenceParams, generativeModel.specification);
670
- const resolvedParams = {
671
- ...inferenceParams,
672
- maxOutputTokens: (_a = inferenceParams.maxOutputTokens) != null ? _a : generativeModel.specification.maxOutputTokens
673
- };
674
- const inferenceRunner = inferenceParams.streaming ? new StreamingInference() : new NonStreamingInference();
675
- const result = inferenceRunner.run(resolvedParams, generativeModel.endpoint);
676
- for await (const item of result) {
677
- if (signal == null ? void 0 : signal.aborted) {
678
- break;
679
- }
680
- if (item instanceof ReasoningItem) {
681
- environment.deliverReasoning(caller, item);
682
- } else if (item instanceof FunctionCallItem) {
683
- environment.deliverFunctionCall(caller, item);
684
- } else if (item instanceof ModelMessageItem) {
685
- environment.deliverModelMessage(caller, item);
686
- } else if (item instanceof SemanticEvent) {
687
- environment.deliverSemanticEvent(caller, item);
688
- }
641
+ loopVisitor.visitFunctionCallStarted(input);
642
+ const { call, inferenceInput } = input;
643
+ let tool = (_a = inferenceInput.tools) == null ? void 0 : _a.find((tool2) => tool2.name === call.name);
644
+ if (!tool) {
645
+ return {
646
+ stateId: this.id,
647
+ input,
648
+ output: {
649
+ item: FunctionCallOutputItem.create(call.callId, `Error: unknown tool "${call.name}"`)
650
+ }
651
+ };
689
652
  }
653
+ const item = await this.functionCallRunner.run(call, tool);
654
+ loopVisitor.visitFunctionCallCompleted(item);
655
+ return {
656
+ stateId: this.id,
657
+ input,
658
+ output: {
659
+ item
660
+ }
661
+ };
690
662
  }
691
663
  };
692
664
 
693
- // src/application/use-cases/execute-function-call.ts
694
- var ExecuteFunctionCall = class {
695
- constructor(functionCallRunner) {
696
- this.functionCallRunner = functionCallRunner;
697
- }
698
- async execute(environment, functionCallItem, tool, caller, signal) {
699
- const stream = this.functionCallRunner.run(functionCallItem, tool, signal);
700
- for await (const item of stream) {
701
- environment.deliverFunctionCallOutput(caller, item);
702
- }
665
+ // src/application/states/message-received.ts
666
+ var MessageReceivedState = class {
667
+ constructor() {
668
+ this.id = "message_received";
669
+ }
670
+ async run(input, loopVisitor) {
671
+ loopVisitor.visitMessageReceivedStarted(input);
672
+ const userMessage = UserMessageItem.create(input.content);
673
+ const output = {
674
+ ...input.input,
675
+ context: input.input.context.addContextItems([userMessage])
676
+ };
677
+ loopVisitor.visitMessageReceivedCompleted(output);
678
+ return {
679
+ stateId: this.id,
680
+ input,
681
+ output
682
+ };
703
683
  }
704
684
  };
705
685
 
706
- // src/application/services/function-call-runner.ts
707
- var FunctionCallRunner = class {
708
- async *run(call, tool, signal) {
709
- if (signal == null ? void 0 : signal.aborted) {
710
- return;
711
- }
712
- if (!tool) throw new Error(`Unknown tool: ${call.name}`);
713
- const result = await tool.invoke(JSON.parse(call.args));
714
- yield FunctionCallOutputItem.create(call.callId, JSON.stringify(result));
686
+ // src/application/states/model-message.ts
687
+ var ModelMessageState = class {
688
+ constructor() {
689
+ this.id = "model_message";
690
+ }
691
+ async run(input, loopVisitor) {
692
+ loopVisitor.visitModelAnswer(input);
693
+ return {
694
+ stateId: this.id,
695
+ input,
696
+ output: void 0
697
+ };
715
698
  }
716
699
  };
717
700
 
718
- // src/application/participants/participant.ts
719
- var BaseParticipant = class extends Participant {
720
- onFunctionCall(_item) {
701
+ // src/domain/agentic-environment/loop/transition-resolver.ts
702
+ var TransitionResolver = class {
703
+ constructor(rules) {
704
+ this.rules = rules;
721
705
  }
722
- onExternalFunctionCall(_source, _item) {
706
+ resolve(execution) {
707
+ const rule = this.rules.find((rule2) => rule2.specification.isSatisfiedBy(execution));
708
+ if (!rule) {
709
+ throw new Error(`No transition rule found after "${execution.stateId}"`);
710
+ }
711
+ return rule.createTransition(execution);
723
712
  }
724
- onFunctionCallOutput(_item) {
713
+ };
714
+
715
+ // src/domain/agentic-environment/loop/transition-specification.ts
716
+ var MessageReceivedSpecification = class {
717
+ isSatisfiedBy(execution) {
718
+ return execution.stateId === "message_received";
725
719
  }
726
- onExternalFunctionCallOutput(_source, _item) {
720
+ };
721
+ var InferenceProducedFunctionCallSpecification = class {
722
+ isSatisfiedBy(execution) {
723
+ return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "function_call");
727
724
  }
728
- onReasoning(_item) {
725
+ };
726
+ var InferenceProducedModelMessageSpecification = class {
727
+ isSatisfiedBy(execution) {
728
+ return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
729
729
  }
730
- onExternalReasoning(_source, _item) {
730
+ };
731
+ var FunctionCallCompletedSpecification = class {
732
+ isSatisfiedBy(execution) {
733
+ return execution.stateId === "function_call";
731
734
  }
732
- onModelMessage(_item) {
735
+ };
736
+ var ModelMessageCompletedSpecification = class {
737
+ isSatisfiedBy(execution) {
738
+ return execution.stateId === "model_message";
733
739
  }
734
- onExternalModelMessage(_source, _item) {
740
+ };
741
+
742
+ // src/domain/agentic-environment/loop/transition-rule.ts
743
+ var MessageReceivedToInferenceRule = class {
744
+ constructor() {
745
+ this.specification = new MessageReceivedSpecification();
735
746
  }
736
- onMessage(_message) {
747
+ createTransition(execution) {
748
+ if (execution.stateId !== "message_received") {
749
+ throw new Error("Expected message_received execution");
750
+ }
751
+ return {
752
+ nextStateId: "inference",
753
+ input: execution.output
754
+ };
737
755
  }
738
- onJoined() {
756
+ };
757
+ var InferenceToFunctionCallRule = class {
758
+ constructor() {
759
+ this.specification = new InferenceProducedFunctionCallSpecification();
739
760
  }
740
- onLeft() {
761
+ createTransition(execution) {
762
+ if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "function_call")) {
763
+ throw new Error("Expected inference function-call output");
764
+ }
765
+ const functionCallItem = execution.output.items.find((item) => item.type === "function_call");
766
+ if (!functionCallItem) {
767
+ throw new Error("Expected inference function-call output");
768
+ }
769
+ return {
770
+ nextStateId: "function_call",
771
+ input: {
772
+ call: functionCallItem,
773
+ inferenceInput: execution.input
774
+ }
775
+ };
741
776
  }
742
- onParticipantJoined(_participant) {
777
+ };
778
+ var InferenceToModelMessageRule = class {
779
+ constructor() {
780
+ this.specification = new InferenceProducedModelMessageSpecification();
743
781
  }
744
- onParticipantLeft(_participant) {
782
+ createTransition(execution) {
783
+ if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
784
+ throw new Error("Expected inference model-message output");
785
+ }
786
+ const modelMessageItem = execution.output.items.find(
787
+ (item) => item.type === "message" && item.role === "assistant"
788
+ );
789
+ if (!modelMessageItem) {
790
+ throw new Error("Expected inference model-message output");
791
+ }
792
+ return {
793
+ nextStateId: "model_message",
794
+ input: {
795
+ answer: modelMessageItem
796
+ }
797
+ };
745
798
  }
746
- onInternalEvent(_item) {
799
+ };
800
+ var FunctionCallToInferenceRule = class {
801
+ constructor() {
802
+ this.specification = new FunctionCallCompletedSpecification();
747
803
  }
748
- onExternalEvent(_source, _item) {
804
+ createTransition(execution) {
805
+ if (execution.stateId !== "function_call") {
806
+ throw new Error("Expected function_call execution");
807
+ }
808
+ const previousRequest = execution.input.inferenceInput;
809
+ previousRequest.context.addContextItems([execution.input.call, execution.output.item]);
810
+ return {
811
+ nextStateId: "inference",
812
+ input: {
813
+ ...previousRequest
814
+ }
815
+ };
749
816
  }
750
- onError(_error) {
817
+ };
818
+ var ModelMessageToIdleRule = class {
819
+ constructor() {
820
+ this.specification = new ModelMessageCompletedSpecification();
751
821
  }
752
- onParticipantError(_source, _error) {
822
+ createTransition(execution) {
823
+ if (execution.stateId !== "model_message") {
824
+ throw new Error("Expected model_message execution");
825
+ }
826
+ return {
827
+ nextStateId: "idle",
828
+ input: void 0
829
+ };
753
830
  }
754
831
  };
755
832
 
756
- // src/application/use-cases/send-message.ts
757
- var SendMessage = class {
758
- async execute(environment, message, caller) {
759
- if (!caller.isJoinedTo(environment)) {
760
- throw new Error("Not joined to environment");
761
- }
762
- environment.deliverMessage(caller, message);
833
+ // src/domain/agentic-environment/loop/event-publisher-visitor.ts
834
+ var EventPublisherLoopVisitor = class {
835
+ constructor(agentId, runtime) {
836
+ this.agentId = agentId;
837
+ this.runtime = runtime;
838
+ }
839
+ visitMessageReceivedStarted(input) {
840
+ console.log("Visiting message received started: ", input);
841
+ this.publish("message_received.started", input);
842
+ }
843
+ visitMessageReceivedCompleted(output) {
844
+ console.log("Visiting message received completed: ", output);
845
+ this.publish("message_received.completed", output);
846
+ }
847
+ visitInferenceStarted(input) {
848
+ console.log("Visiting inference started: ", input);
849
+ this.publish("inference.started", input);
850
+ }
851
+ visitInferenceEvent(event) {
852
+ console.log("Visiting inference event: ", event);
853
+ this.publish("inference.stream", event);
854
+ }
855
+ visitInferenceCompleted(output) {
856
+ console.log("Visiting inference completed: ", output);
857
+ this.publish("inference.completed", output);
858
+ }
859
+ visitFunctionCallStarted(input) {
860
+ console.log("Visiting function call started: ", input);
861
+ this.publish("function_call.started", input);
862
+ }
863
+ visitFunctionCallCompleted(output) {
864
+ console.log("Visiting function call completed: ", output);
865
+ this.publish("function_call.completed", output);
866
+ }
867
+ visitModelAnswer(input) {
868
+ console.log("Visiting model answer: ", input.answer);
869
+ this.publish("model.answer", input);
870
+ }
871
+ publish(type, payload) {
872
+ this.runtime.publish({
873
+ type,
874
+ producerId: this.agentId,
875
+ occurredAt: /* @__PURE__ */ new Date(),
876
+ payload
877
+ });
763
878
  }
764
879
  };
765
880
 
881
+ // src/application/use-cases/run-loop.ts
882
+ function createRunLoop(resolveRuntime) {
883
+ return function runLoop(agentId, message, inferenceInput) {
884
+ const runtime = resolveRuntime();
885
+ const inferenceRunner = runtime.getInferenceRunner();
886
+ const functionCallRunner = runtime.getFunctionCallRunner();
887
+ const transitionResolver = new TransitionResolver([
888
+ // High-priority interception rules would go first.
889
+ new MessageReceivedToInferenceRule(),
890
+ new InferenceToFunctionCallRule(),
891
+ new InferenceToModelMessageRule(),
892
+ new FunctionCallToInferenceRule(),
893
+ new ModelMessageToIdleRule()
894
+ ]);
895
+ const stateExecutor = new LoopStateExecutor(
896
+ new MessageReceivedState(),
897
+ new InferenceState(inferenceRunner),
898
+ new FunctionCallState(functionCallRunner),
899
+ new ModelMessageState()
900
+ );
901
+ const agentLoop = new AgentLoop(
902
+ stateExecutor,
903
+ transitionResolver,
904
+ new EventPublisherLoopVisitor(agentId, runtime)
905
+ );
906
+ agentLoop.run({
907
+ content: message,
908
+ input: inferenceInput
909
+ });
910
+ };
911
+ }
912
+
766
913
  // src/infrastructure/providers/anthropic/endpoints/anthropic-messages-mapper.ts
767
914
  var AnthropicMessagesMapper = class {
768
- toRequest(inferenceParams) {
769
- const { messages, system } = this.mapContextItems(inferenceParams);
915
+ toRequest(inferenceInput) {
916
+ const { messages, system } = this.mapContextItems(inferenceInput);
770
917
  const outputConfig = {};
771
918
  const request = {
772
- model: inferenceParams.model,
919
+ model: inferenceInput.model,
773
920
  messages
774
921
  };
775
- request.max_tokens = inferenceParams.maxOutputTokens;
922
+ request.max_tokens = inferenceInput.maxOutputTokens;
776
923
  if (system) {
777
924
  request.system = system;
778
925
  }
779
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
780
- request.tools = inferenceParams.tools.map((tool) => ({
926
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
927
+ request.tools = inferenceInput.tools.map((tool) => ({
781
928
  name: tool.name,
782
929
  description: tool.description,
783
930
  input_schema: tool.parameters
784
931
  }));
785
932
  }
786
- if (inferenceParams.structuredOutput) {
933
+ if (inferenceInput.structuredOutput) {
787
934
  outputConfig.format = {
788
935
  type: "json_schema",
789
- json_schema: inferenceParams.structuredOutput.schema
936
+ json_schema: inferenceInput.structuredOutput.schema
790
937
  };
791
938
  }
792
- if (inferenceParams.reasoningEffort) {
939
+ if (inferenceInput.reasoningEffort) {
793
940
  request.thinking = { type: "adaptive" };
794
- outputConfig.effort = inferenceParams.reasoningEffort;
941
+ outputConfig.effort = inferenceInput.reasoningEffort;
795
942
  }
796
943
  if (Object.keys(outputConfig).length > 0) {
797
944
  request.output_config = outputConfig;
798
945
  }
799
- if (inferenceParams.streaming) {
800
- request.stream = inferenceParams.streaming;
946
+ if (inferenceInput.streaming) {
947
+ request.stream = inferenceInput.streaming;
801
948
  }
802
949
  return request;
803
950
  }
804
- toResponse(response) {
805
- const contextItems = this.extractContextItems(response);
806
- const tokenUsage = this.extractTokenUsage(response);
807
- return new InferenceResponse(contextItems, tokenUsage);
808
- }
809
951
  addContentBlock(messages, role, block) {
810
952
  const lastMessage = messages[messages.length - 1];
811
953
  if ((lastMessage == null ? void 0 : lastMessage.role) === role) {
@@ -817,9 +959,9 @@ var AnthropicMessagesMapper = class {
817
959
  content: [block]
818
960
  });
819
961
  }
820
- mapContextItems(inferenceParams) {
962
+ mapContextItems(inferenceInput) {
821
963
  var _a, _b, _c;
822
- const context = inferenceParams.context;
964
+ const context = inferenceInput.context;
823
965
  const messages = [];
824
966
  const system = [];
825
967
  for (const item of context.getItems()) {
@@ -886,7 +1028,7 @@ var AnthropicMessagesMapper = class {
886
1028
  new OutputTokenDetails(0)
887
1029
  );
888
1030
  }
889
- extractContextItems(response) {
1031
+ toResponse(response) {
890
1032
  var _a;
891
1033
  const items = [];
892
1034
  for (const block of response.content) {
@@ -914,30 +1056,27 @@ var AnthropicMessagesMapper = class {
914
1056
  );
915
1057
  }
916
1058
  }
917
- return items;
1059
+ return { items, rowResponse: response };
918
1060
  }
919
1061
  };
920
1062
 
921
1063
  // src/infrastructure/providers/anthropic/endpoints/anthropic-messages.ts
922
1064
  var import_sdk = __toESM(require("@anthropic-ai/sdk"));
923
1065
  var AnthropicMessages = class {
924
- constructor(endpointMapper = new AnthropicMessagesMapper()) {
1066
+ constructor(endpointMapper = new AnthropicMessagesMapper(), config = {}) {
925
1067
  this.endpointMapper = endpointMapper;
926
- this.client = new import_sdk.default();
1068
+ this.client = new import_sdk.default({ baseURL: config.baseURL, apiKey: config.apiKey });
927
1069
  }
928
- async infer(inferenceParams) {
929
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
930
- const response = await this.client.messages.create(inferenceRequest);
1070
+ async infer(inferenceInput) {
1071
+ const request = this.endpointMapper.toRequest(inferenceInput);
1072
+ const response = await this.client.messages.create(request);
931
1073
  return this.endpointMapper.toResponse(response);
932
1074
  }
933
- async *stream(inferenceParams, signal) {
934
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
935
- const stream = await this.client.messages.create(inferenceRequest);
1075
+ async *stream(inferenceInput) {
1076
+ const request = this.endpointMapper.toRequest(inferenceInput);
1077
+ const stream = await this.client.messages.create(request);
936
1078
  for await (const event of stream) {
937
- if (signal == null ? void 0 : signal.aborted) {
938
- break;
939
- }
940
- yield new SemanticEvent(event.type, event);
1079
+ yield event;
941
1080
  }
942
1081
  }
943
1082
  };
@@ -959,27 +1098,27 @@ var SummaryText = class _SummaryText extends ItemContent {
959
1098
 
960
1099
  // src/infrastructure/providers/openai/endpoints/openai-responses-mapper.ts
961
1100
  var OpenAIResponsesMapper = class {
962
- toRequest(inferenceParams) {
1101
+ toRequest(inferenceInput) {
963
1102
  var _a, _b;
964
1103
  const request = {
965
- model: inferenceParams.model,
966
- input: this.mapContextItems(inferenceParams)
1104
+ model: inferenceInput.model,
1105
+ input: this.mapContextItems(inferenceInput)
967
1106
  };
968
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
969
- request.tools = inferenceParams.tools.map((tool) => ({
1107
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1108
+ request.tools = inferenceInput.tools.map((tool) => ({
970
1109
  type: tool.type,
971
1110
  name: tool.name,
972
1111
  description: tool.description,
973
1112
  parameters: tool.parameters
974
1113
  }));
975
1114
  }
976
- if (inferenceParams.reasoningEffort) {
1115
+ if (inferenceInput.reasoningEffort) {
977
1116
  request.reasoning = {
978
- effort: inferenceParams.reasoningEffort
1117
+ effort: inferenceInput.reasoningEffort
979
1118
  };
980
1119
  }
981
- if (inferenceParams.structuredOutput) {
982
- const format = inferenceParams.structuredOutput;
1120
+ if (inferenceInput.structuredOutput) {
1121
+ const format = inferenceInput.structuredOutput;
983
1122
  request.text = {
984
1123
  format: {
985
1124
  type: "json_schema",
@@ -989,19 +1128,14 @@ var OpenAIResponsesMapper = class {
989
1128
  }
990
1129
  };
991
1130
  }
992
- if (inferenceParams.streaming) {
993
- request.stream = inferenceParams.streaming;
1131
+ if (inferenceInput.streaming) {
1132
+ request.stream = inferenceInput.streaming;
994
1133
  }
995
1134
  return request;
996
1135
  }
997
- toResponse(response) {
998
- const contextItems = this.extractContextItems(response);
999
- const tokenUsage = this.extractTokenUsage(response);
1000
- return new InferenceResponse(contextItems, tokenUsage);
1001
- }
1002
- mapContextItems(inferenceParams) {
1136
+ mapContextItems(inferenceInput) {
1003
1137
  const input = [];
1004
- for (const item of inferenceParams.context.getItems()) {
1138
+ for (const item of inferenceInput.context.getItems()) {
1005
1139
  if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem || item instanceof UserMessageItem) {
1006
1140
  input.push({
1007
1141
  type: item.type,
@@ -1061,7 +1195,7 @@ var OpenAIResponsesMapper = class {
1061
1195
  new OutputTokenDetails((_d = (_c = response.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
1062
1196
  );
1063
1197
  }
1064
- extractContextItems(response) {
1198
+ toResponse(response) {
1065
1199
  var _a, _b, _c;
1066
1200
  const items = [];
1067
1201
  for (const item of (_a = response.output) != null ? _a : []) {
@@ -1094,7 +1228,10 @@ var OpenAIResponsesMapper = class {
1094
1228
  );
1095
1229
  }
1096
1230
  }
1097
- return items;
1231
+ return {
1232
+ items,
1233
+ rowResponse: response
1234
+ };
1098
1235
  }
1099
1236
  };
1100
1237
 
@@ -1104,19 +1241,16 @@ var OpenAIResponses = class {
1104
1241
  this.endpointMapper = endpointMapper;
1105
1242
  this.client = new import_openai.default();
1106
1243
  }
1107
- async infer(inferenceParams) {
1108
- const request = this.endpointMapper.toRequest(inferenceParams);
1244
+ async infer(inferenceInput) {
1245
+ const request = this.endpointMapper.toRequest(inferenceInput);
1109
1246
  const response = await this.client.responses.create(request);
1110
1247
  return this.endpointMapper.toResponse(response);
1111
1248
  }
1112
- async *stream(inferenceParams, signal) {
1113
- const request = this.endpointMapper.toRequest(inferenceParams);
1249
+ async *stream(inferenceInput) {
1250
+ const request = this.endpointMapper.toRequest(inferenceInput);
1114
1251
  const response = await this.client.responses.create(request);
1115
1252
  for await (const event of response) {
1116
- if (signal == null ? void 0 : signal.aborted) {
1117
- break;
1118
- }
1119
- yield new SemanticEvent(event.type, event);
1253
+ yield event;
1120
1254
  }
1121
1255
  }
1122
1256
  };
@@ -1126,16 +1260,16 @@ var import_genai = require("@google/genai");
1126
1260
 
1127
1261
  // src/infrastructure/providers/gemini/endpoints/gemini-generate-content-mapper.ts
1128
1262
  var GeminiGenerateContentMapper = class {
1129
- toRequest(inferenceParams) {
1130
- const { contents, systemInstruction } = this.mapContextItems(inferenceParams);
1263
+ toRequest(inferenceInput) {
1264
+ const { contents, systemInstruction } = this.mapContextItems(inferenceInput);
1131
1265
  const config = {};
1132
1266
  if (systemInstruction) {
1133
1267
  config.systemInstruction = systemInstruction;
1134
1268
  }
1135
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
1269
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1136
1270
  config.tools = [
1137
1271
  {
1138
- functionDeclarations: inferenceParams.tools.map((tool) => ({
1272
+ functionDeclarations: inferenceInput.tools.map((tool) => ({
1139
1273
  name: tool.name,
1140
1274
  description: tool.description,
1141
1275
  parametersJsonSchema: tool.parameters
@@ -1143,34 +1277,29 @@ var GeminiGenerateContentMapper = class {
1143
1277
  }
1144
1278
  ];
1145
1279
  }
1146
- if (inferenceParams.structuredOutput) {
1280
+ if (inferenceInput.structuredOutput) {
1147
1281
  config.responseMimeType = "application/json";
1148
- config.responseSchema = inferenceParams.structuredOutput.schema;
1282
+ config.responseSchema = inferenceInput.structuredOutput.schema;
1149
1283
  }
1150
- if (inferenceParams.reasoningEffort) {
1284
+ if (inferenceInput.reasoningEffort) {
1151
1285
  config.thinkingConfig = {
1152
- thinkingLevel: inferenceParams.reasoningEffort,
1286
+ thinkingLevel: inferenceInput.reasoningEffort,
1153
1287
  includeThoughts: true
1154
1288
  };
1155
1289
  }
1156
1290
  const request = {
1157
- model: inferenceParams.model,
1291
+ model: inferenceInput.model,
1158
1292
  contents,
1159
1293
  config
1160
1294
  };
1161
- if (inferenceParams.streaming) {
1162
- request.stream = inferenceParams.streaming;
1295
+ if (inferenceInput.streaming) {
1296
+ request.stream = inferenceInput.streaming;
1163
1297
  }
1164
1298
  return request;
1165
1299
  }
1166
- toResponse(response) {
1167
- const contextItems = this.extractContextItems(response);
1168
- const tokenUsage = this.extractTokenUsage(response);
1169
- return new InferenceResponse(contextItems, tokenUsage);
1170
- }
1171
- mapContextItems(inferenceParams) {
1300
+ mapContextItems(inferenceInput) {
1172
1301
  var _a;
1173
- const context = inferenceParams.context;
1302
+ const context = inferenceInput.context;
1174
1303
  const contents = [];
1175
1304
  const system = [];
1176
1305
  const callNames = /* @__PURE__ */ new Map();
@@ -1243,7 +1372,7 @@ var GeminiGenerateContentMapper = class {
1243
1372
  new OutputTokenDetails((_e = usage.thoughtsTokenCount) != null ? _e : 0)
1244
1373
  );
1245
1374
  }
1246
- extractContextItems(response) {
1375
+ toResponse(response) {
1247
1376
  var _a, _b, _c, _d, _e, _f;
1248
1377
  const items = [];
1249
1378
  const parts = (_d = (_c = (_b = (_a = response.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts) != null ? _d : [];
@@ -1272,42 +1401,43 @@ var GeminiGenerateContentMapper = class {
1272
1401
  );
1273
1402
  }
1274
1403
  }
1275
- return items;
1404
+ return { items, rowResponse: response };
1276
1405
  }
1277
1406
  };
1278
1407
 
1279
1408
  // src/infrastructure/providers/gemini/endpoints/gemini-generate-content.ts
1280
1409
  var GeminiGenerateContent = class {
1281
- constructor(endpointMapper = new GeminiGenerateContentMapper()) {
1410
+ constructor(endpointMapper = new GeminiGenerateContentMapper(), config = {}) {
1411
+ var _a;
1282
1412
  this.endpointMapper = endpointMapper;
1283
- this.client = new import_genai.GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
1413
+ this.client = new import_genai.GoogleGenAI({
1414
+ apiKey: (_a = config.apiKey) != null ? _a : process.env.GEMINI_API_KEY,
1415
+ ...config.baseURL ? { httpOptions: { baseUrl: config.baseURL } } : {}
1416
+ });
1284
1417
  }
1285
- async infer(inferenceParams) {
1286
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
1287
- const response = await this.client.models.generateContent(inferenceRequest);
1418
+ async infer(inferenceInput) {
1419
+ const request = this.endpointMapper.toRequest(inferenceInput);
1420
+ const response = await this.client.models.generateContent(request);
1288
1421
  return this.endpointMapper.toResponse(response);
1289
1422
  }
1290
- async *stream(inferenceParams, signal) {
1291
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
1292
- const stream = await this.client.models.generateContentStream(inferenceRequest);
1423
+ async *stream(inferenceInput) {
1424
+ const request = this.endpointMapper.toRequest(inferenceInput);
1425
+ const stream = await this.client.models.generateContentStream(request);
1293
1426
  for await (const chunk of stream) {
1294
- if (signal == null ? void 0 : signal.aborted) {
1295
- break;
1296
- }
1297
- yield new SemanticEvent("generate_content_chunk", chunk);
1427
+ yield chunk;
1298
1428
  }
1299
1429
  }
1300
1430
  };
1301
1431
 
1302
1432
  // src/infrastructure/providers/openai/endpoints/openai-chat-completions-mapper.ts
1303
1433
  var OpenAIChatCompletionsMapper = class {
1304
- toRequest(inferenceParams) {
1434
+ toRequest(inferenceInput) {
1305
1435
  const request = {
1306
- model: inferenceParams.model,
1307
- messages: this.mapContextItems(inferenceParams)
1436
+ model: inferenceInput.model,
1437
+ messages: this.mapContextItems(inferenceInput)
1308
1438
  };
1309
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
1310
- request.tools = inferenceParams.tools.map((tool) => ({
1439
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1440
+ request.tools = inferenceInput.tools.map((tool) => ({
1311
1441
  type: "function",
1312
1442
  function: {
1313
1443
  name: tool.name,
@@ -1316,23 +1446,18 @@ var OpenAIChatCompletionsMapper = class {
1316
1446
  }
1317
1447
  }));
1318
1448
  }
1319
- if (inferenceParams.reasoningEffort) {
1320
- request.reasoning_effort = inferenceParams.reasoningEffort;
1449
+ if (inferenceInput.reasoningEffort) {
1450
+ request.reasoning_effort = inferenceInput.reasoningEffort;
1321
1451
  }
1322
- if (inferenceParams.streaming) {
1323
- request.stream = inferenceParams.streaming;
1452
+ if (inferenceInput.streaming) {
1453
+ request.stream = inferenceInput.streaming;
1324
1454
  }
1325
1455
  return request;
1326
1456
  }
1327
- toResponse(response) {
1328
- const contextItems = this.extractContextItems(response);
1329
- const tokenUsage = this.extractTokenUsage(response);
1330
- return new InferenceResponse(contextItems, tokenUsage);
1331
- }
1332
- mapContextItems(inferenceParams) {
1457
+ mapContextItems(inferenceInput) {
1333
1458
  var _a;
1334
1459
  const messages = [];
1335
- for (const item of inferenceParams.context.getItems()) {
1460
+ for (const item of inferenceInput.context.getItems()) {
1336
1461
  if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem) {
1337
1462
  messages.push({ role: "system", content: item.content.text });
1338
1463
  continue;
@@ -1379,12 +1504,12 @@ var OpenAIChatCompletionsMapper = class {
1379
1504
  new OutputTokenDetails((_d = (_c = response.usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
1380
1505
  );
1381
1506
  }
1382
- extractContextItems(response) {
1507
+ toResponse(response) {
1383
1508
  var _a, _b, _c;
1384
1509
  const items = [];
1385
1510
  const message = (_b = (_a = response.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1386
1511
  if (!message) {
1387
- return items;
1512
+ return { items: [], rowResponse: response };
1388
1513
  }
1389
1514
  if (message.reasoning_content) {
1390
1515
  items.push(
@@ -1407,7 +1532,7 @@ var OpenAIChatCompletionsMapper = class {
1407
1532
  })
1408
1533
  );
1409
1534
  }
1410
- return items;
1535
+ return { items, rowResponse: response };
1411
1536
  }
1412
1537
  };
1413
1538
 
@@ -1423,24 +1548,20 @@ var OpenAIChatCompletions = class {
1423
1548
  });
1424
1549
  this.extraBody = (_a = config.extraBody) != null ? _a : {};
1425
1550
  }
1426
- buildRequest(inferenceParams) {
1551
+ buildRequest(inferenceInput) {
1427
1552
  return {
1428
1553
  ...this.extraBody,
1429
- ...this.endpointMapper.toRequest(inferenceParams)
1554
+ ...this.endpointMapper.toRequest(inferenceInput)
1430
1555
  };
1431
1556
  }
1432
- async infer(inferenceParams) {
1433
- const response = await this.client.chat.completions.create(this.buildRequest(inferenceParams));
1557
+ async infer(inferenceInput) {
1558
+ const response = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
1434
1559
  return this.endpointMapper.toResponse(response);
1435
1560
  }
1436
- async *stream(inferenceParams, signal) {
1437
- var _a;
1438
- const stream = await this.client.chat.completions.create(this.buildRequest(inferenceParams));
1561
+ async *stream(inferenceInput) {
1562
+ const stream = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
1439
1563
  for await (const event of stream) {
1440
- if (signal == null ? void 0 : signal.aborted) {
1441
- break;
1442
- }
1443
- yield new SemanticEvent((_a = event.object) != null ? _a : "chat.completion.chunk", event);
1564
+ yield event;
1444
1565
  }
1445
1566
  }
1446
1567
  };
@@ -1709,103 +1830,71 @@ var claudeHaiku45Specification = {
1709
1830
  supportsStructuredOutput: true
1710
1831
  };
1711
1832
 
1712
- // src/infrastructure/repository/generative-model-repository.ts
1713
- var InMemoryGenerativeModelRepository = class {
1714
- getByModelName(modelName) {
1715
- let generativeModel = void 0;
1716
- switch (modelName) {
1717
- case "gpt-5.4":
1718
- generativeModel = {
1719
- endpoint: new OpenAIResponses(),
1720
- specification: gpt54Specification
1721
- };
1722
- break;
1723
- case "gpt-5.4-mini":
1724
- generativeModel = {
1725
- endpoint: new OpenAIResponses(),
1726
- specification: gpt54MiniSpecification
1727
- };
1728
- break;
1729
- case "gpt-5.4-nano":
1730
- generativeModel = {
1731
- endpoint: new OpenAIResponses(),
1732
- specification: gpt54NanoSpecification
1733
- };
1734
- break;
1735
- case "gpt-5.5":
1736
- generativeModel = {
1737
- endpoint: new OpenAIResponses(),
1738
- specification: gpt55Specification
1739
- };
1740
- break;
1741
- case "claude-haiku-4-5":
1742
- generativeModel = {
1743
- endpoint: new AnthropicMessages(),
1744
- specification: claudeHaiku45Specification
1745
- };
1746
- break;
1747
- case "claude-sonnet-4-6":
1748
- generativeModel = {
1749
- endpoint: new AnthropicMessages(),
1750
- specification: claudeSonnet46Specification
1751
- };
1752
- break;
1753
- case "claude-opus-4-7":
1754
- generativeModel = {
1755
- endpoint: new AnthropicMessages(),
1756
- specification: claudeOpus47Specification
1757
- };
1758
- break;
1759
- case "claude-opus-4-8":
1760
- generativeModel = {
1761
- endpoint: new AnthropicMessages(),
1762
- specification: claudeOpus48Specification
1763
- };
1764
- break;
1765
- case "gemini-3.5-flash":
1766
- generativeModel = {
1767
- endpoint: new GeminiGenerateContent(),
1768
- specification: gemini35FlashSpecification
1769
- };
1770
- break;
1771
- case "gemini-3.1-pro-preview":
1772
- generativeModel = {
1773
- endpoint: new GeminiGenerateContent(),
1774
- specification: gemini31ProSpecification
1775
- };
1776
- break;
1777
- case "deepseek-v4-flash":
1778
- generativeModel = {
1779
- endpoint: new OpenAIChatCompletions(),
1780
- specification: deepSeekV4FlashSpecification
1781
- };
1782
- break;
1783
- case "deepseek-v4-pro":
1784
- generativeModel = {
1785
- endpoint: new OpenAIChatCompletions(),
1786
- specification: deepSeekV4ProSpecification
1787
- };
1788
- }
1789
- if (!generativeModel) {
1790
- throw new Error(`Unsupported model: ${modelName}`);
1791
- }
1792
- return Promise.resolve(generativeModel);
1833
+ // src/application/services/models.ts
1834
+ var supportedModels = [
1835
+ {
1836
+ endpoint: new OpenAIResponses(),
1837
+ specification: gpt54Specification
1838
+ },
1839
+ {
1840
+ endpoint: new OpenAIResponses(),
1841
+ specification: gpt54MiniSpecification
1842
+ },
1843
+ {
1844
+ endpoint: new OpenAIResponses(),
1845
+ specification: gpt54NanoSpecification
1846
+ },
1847
+ {
1848
+ endpoint: new OpenAIResponses(),
1849
+ specification: gpt55Specification
1850
+ },
1851
+ {
1852
+ endpoint: new AnthropicMessages(),
1853
+ specification: claudeHaiku45Specification
1854
+ },
1855
+ {
1856
+ endpoint: new AnthropicMessages(),
1857
+ specification: claudeSonnet46Specification
1858
+ },
1859
+ {
1860
+ endpoint: new AnthropicMessages(),
1861
+ specification: claudeOpus47Specification
1862
+ },
1863
+ {
1864
+ endpoint: new AnthropicMessages(),
1865
+ specification: claudeOpus48Specification
1866
+ },
1867
+ {
1868
+ endpoint: new GeminiGenerateContent(),
1869
+ specification: gemini35FlashSpecification
1870
+ },
1871
+ {
1872
+ endpoint: new GeminiGenerateContent(),
1873
+ specification: gemini31ProSpecification
1874
+ },
1875
+ {
1876
+ endpoint: new OpenAIChatCompletions(),
1877
+ specification: deepSeekV4FlashSpecification
1878
+ },
1879
+ {
1880
+ endpoint: new OpenAIChatCompletions(),
1881
+ specification: deepSeekV4ProSpecification
1793
1882
  }
1794
- };
1883
+ ];
1795
1884
 
1796
1885
  // src/domain/generative-model/request-validation/reasoning-effort.ts
1797
1886
  var ReasoningEffortValidation = class {
1798
1887
  constructor() {
1799
1888
  this.name = "reasoning-effort";
1800
1889
  }
1801
- isValid(inferenceParams, model) {
1802
- if (inferenceParams.reasoningEffort === void 0) {
1890
+ isValid(inferenceInput, model) {
1891
+ if (inferenceInput.reasoningEffort === void 0) {
1803
1892
  return true;
1804
1893
  }
1805
1894
  if (!model.supportsReasoningEffort) {
1806
1895
  return false;
1807
1896
  }
1808
- return model.supportedReasoningEfforts.includes(inferenceParams.reasoningEffort);
1897
+ return model.supportedReasoningEfforts.includes(inferenceInput.reasoningEffort);
1809
1898
  }
1810
1899
  };
1811
1900
 
@@ -1814,8 +1903,8 @@ var ToolCallingValidation = class {
1814
1903
  constructor() {
1815
1904
  this.name = "tool-calling";
1816
1905
  }
1817
- isValid(inferenceParams, model) {
1818
- if (inferenceParams.tools === void 0) {
1906
+ isValid(inferenceInput, model) {
1907
+ if (inferenceInput.tools === void 0) {
1819
1908
  return true;
1820
1909
  }
1821
1910
  return model.supportsFunctionCalling;
@@ -1827,8 +1916,8 @@ var StreamingValidation = class {
1827
1916
  constructor() {
1828
1917
  this.name = "streaming";
1829
1918
  }
1830
- isValid(inferenceParams, model) {
1831
- if (!inferenceParams.streaming) {
1919
+ isValid(inferenceInput, model) {
1920
+ if (!inferenceInput.streaming) {
1832
1921
  return true;
1833
1922
  }
1834
1923
  return model.supportsStreaming;
@@ -1855,8 +1944,8 @@ var ContextValidation = class {
1855
1944
  constructor() {
1856
1945
  this.name = "context";
1857
1946
  }
1858
- isValid(inferenceParams, model) {
1859
- return inferenceParams.context.items.every(
1947
+ isValid(inferenceInput, model) {
1948
+ return inferenceInput.context.items.every(
1860
1949
  (item) => model.supportedContextItemTypes.includes(getContextItemValidationKey(item))
1861
1950
  );
1862
1951
  }
@@ -1870,45 +1959,108 @@ var defaultRequestValidationRules = [
1870
1959
  new StructuredOutputValidation(),
1871
1960
  new ContextValidation()
1872
1961
  ];
1873
- var InferenceRequestValidator = class {
1962
+ var InferenceInputValidator = class {
1874
1963
  constructor(rules = defaultRequestValidationRules) {
1875
1964
  this.rules = rules;
1876
1965
  }
1877
- validate(inferenceParams, model) {
1966
+ validate(inferenceInput, model) {
1878
1967
  for (const rule of this.rules) {
1879
- if (!rule.isValid(inferenceParams, model)) {
1968
+ if (!rule.isValid(inferenceInput, model)) {
1880
1969
  throw new Error(`Request validation "${rule.name}" failed for model "${model.name}"`);
1881
1970
  }
1882
1971
  }
1883
1972
  }
1884
1973
  };
1885
1974
 
1886
- // src/index.ts
1887
- var generativeModelRepository = new InMemoryGenerativeModelRepository();
1888
- var inferenceRequestValidator = new InferenceRequestValidator();
1889
- var runInferenceUseCase = new RunInference(generativeModelRepository, inferenceRequestValidator);
1890
- var executeFunctionCallUseCase = new ExecuteFunctionCall(new FunctionCallRunner());
1891
- var sendMessageUseCase = new SendMessage();
1892
- var runInference = (inferenceParams) => {
1893
- runInferenceUseCase.execute(inferenceParams);
1894
- };
1895
- var executeFunctionCall = (environment, functionCallItem, tool, caller) => {
1896
- executeFunctionCallUseCase.execute(environment, functionCallItem, tool, caller);
1975
+ // src/application/services/inference-runner.ts
1976
+ var DefaultInferenceRunner = class {
1977
+ constructor(supportedModels2, requestValidator) {
1978
+ this.supportedModels = supportedModels2;
1979
+ this.requestValidator = requestValidator;
1980
+ }
1981
+ async run(input) {
1982
+ const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
1983
+ if (!generativeModel) {
1984
+ throw new Error(`Unsupported model: ${input.model}`);
1985
+ }
1986
+ this.requestValidator.validate(input, generativeModel.specification);
1987
+ return await generativeModel.endpoint.infer(input);
1988
+ }
1989
+ async streamInference(input) {
1990
+ const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
1991
+ if (!generativeModel) {
1992
+ throw new Error(`Unsupported model: ${input.model}`);
1993
+ }
1994
+ this.requestValidator.validate(input, generativeModel.specification);
1995
+ return await generativeModel.endpoint.stream(input);
1996
+ }
1897
1997
  };
1898
- var sendMessage = (environment, message, caller) => {
1899
- sendMessageUseCase.execute(environment, message, caller);
1998
+
1999
+ // src/application/services/function-call.ts
2000
+ var DefaultFunctionCallRunner = class {
2001
+ async run(input, tool) {
2002
+ try {
2003
+ const result = await tool.invoke(JSON.parse(input.args));
2004
+ return FunctionCallOutputItem.create(input.callId, JSON.stringify(result));
2005
+ } catch (error) {
2006
+ return FunctionCallOutputItem.create(input.callId, `Error calling tool: ${error}`);
2007
+ }
2008
+ }
1900
2009
  };
2010
+
2011
+ // src/define-runtime.ts
2012
+ function defineRuntime() {
2013
+ let runtime = null;
2014
+ const processor = new EventProcessor();
2015
+ function initializeRuntime(config) {
2016
+ var _a, _b, _c, _d;
2017
+ if (runtime) {
2018
+ throw new Error("Runtime already initialized");
2019
+ }
2020
+ const inferenceRunner = (_d = (_a = config.inferenceRunnerConfig) == null ? void 0 : _a.runner) != null ? _d : new DefaultInferenceRunner(
2021
+ (_c = (_b = config.inferenceRunnerConfig) == null ? void 0 : _b.supportedModels) != null ? _c : supportedModels,
2022
+ new InferenceInputValidator()
2023
+ );
2024
+ const functionCallRunner = new DefaultFunctionCallRunner();
2025
+ runtime = new RuntimeService(config.state, processor, inferenceRunner, functionCallRunner);
2026
+ return runtime;
2027
+ }
2028
+ function resolveRuntime() {
2029
+ if (!runtime) {
2030
+ throw new Error("Runtime not initialized");
2031
+ }
2032
+ return runtime;
2033
+ }
2034
+ function resolveParticipant(id) {
2035
+ if (!runtime) {
2036
+ throw new Error("Runtime not initialized");
2037
+ }
2038
+ const participant = runtime.state.getParticipant(id);
2039
+ if (!participant) {
2040
+ throw new Error(`Participant ${id} not found`);
2041
+ }
2042
+ return participant;
2043
+ }
2044
+ const join = createJoin(resolveRuntime);
2045
+ const leave = createLeave(resolveRuntime);
2046
+ const sendMessage = createSendMessage(resolveRuntime);
2047
+ const runLoop = createRunLoop(resolveRuntime);
2048
+ return {
2049
+ initializeRuntime,
2050
+ resolveRuntime,
2051
+ resolveParticipant,
2052
+ join,
2053
+ leave,
2054
+ sendMessage,
2055
+ runLoop
2056
+ };
2057
+ }
1901
2058
  // Annotate the CommonJS export names for ESM import in node:
1902
2059
  0 && (module.exports = {
1903
- AgenticEnvironment,
1904
- AgenticError,
1905
- BaseParticipant,
1906
2060
  ContextItem,
1907
2061
  DeveloperMessageItem,
1908
2062
  FunctionCallItem,
1909
2063
  FunctionCallOutputItem,
1910
- InMemoryModelContextRepository,
1911
- InferenceResponse,
1912
2064
  InputTokenDetails,
1913
2065
  McpClient,
1914
2066
  McpToolRegistry,
@@ -1917,12 +2069,13 @@ var sendMessage = (environment, message, caller) => {
1917
2069
  OutputTokenDetails,
1918
2070
  Participant,
1919
2071
  ReasoningItem,
2072
+ RuntimeState,
1920
2073
  SemanticEvent,
1921
2074
  SystemMessageItem,
1922
2075
  TokenUsage,
1923
2076
  UserMessageItem,
1924
- executeFunctionCall,
1925
- runInference,
1926
- sendMessage
2077
+ createAgent,
2078
+ createHuman,
2079
+ defineRuntime
1927
2080
  });
1928
2081
  //# sourceMappingURL=index.js.map