@mozaik-ai/core 3.14.0 → 4.0.0-beta.2

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