@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.mjs CHANGED
@@ -1,39 +1,26 @@
1
1
  // src/domain/model-context/model-context.ts
2
2
  var ModelContext = class _ModelContext {
3
- constructor(id, projectId, items) {
4
- this.projectId = projectId;
3
+ constructor(id, items) {
5
4
  this.id = id;
6
5
  this.items = items;
7
6
  }
8
- addContextItem(item) {
9
- this.items.push(item);
10
- return this;
11
- }
12
- applyModelOutput(items) {
13
- for (const item of items) {
14
- const itemType = item.getType();
15
- if (itemType !== "function_call" && itemType !== "message" && itemType !== "reasoning") {
16
- throw new Error(`Invalid item type: ${itemType}`);
17
- }
18
- }
19
- this.items.push(...items);
20
- return this;
21
- }
22
7
  getItems() {
23
8
  return this.items;
24
9
  }
25
- getLastItem() {
26
- if (this.items.length === 0) {
27
- throw new Error("No items in context");
28
- }
29
- return this.items[this.items.length - 1];
10
+ addContextItems(contextItems) {
11
+ this.items.push(...contextItems);
12
+ return this;
13
+ }
14
+ addItem(item) {
15
+ this.items.push(item);
16
+ return this;
30
17
  }
31
- static create(projectId) {
18
+ static create() {
32
19
  const id = crypto.randomUUID();
33
- return new _ModelContext(id, projectId, []);
20
+ return new _ModelContext(id, []);
34
21
  }
35
22
  static rehydrate(data) {
36
- return new _ModelContext(data.id, data.projectId, data.items);
23
+ return new _ModelContext(data.id, data.items);
37
24
  }
38
25
  };
39
26
 
@@ -158,8 +145,8 @@ var StructuredOutputValidation = class {
158
145
  constructor() {
159
146
  this.name = "structured-output";
160
147
  }
161
- isValid(inferenceParams, model) {
162
- if (inferenceParams.structuredOutput === void 0) {
148
+ isValid(inferenceInput, model) {
149
+ if (inferenceInput.structuredOutput === void 0) {
163
150
  return true;
164
151
  }
165
152
  return model.supportsStructuredOutput;
@@ -296,48 +283,6 @@ var McpToolRegistry = class {
296
283
  }
297
284
  };
298
285
 
299
- // src/infrastructure/repository/in-memory-model-context-repository.ts
300
- var InMemoryModelContextRepository = class {
301
- constructor() {
302
- this.store = /* @__PURE__ */ new Map();
303
- this.projectIndex = /* @__PURE__ */ new Map();
304
- }
305
- async save(context) {
306
- const existing = this.store.get(context.id);
307
- if (existing && existing.projectId !== context.projectId) {
308
- const prevSet = this.projectIndex.get(existing.projectId);
309
- prevSet == null ? void 0 : prevSet.delete(existing.id);
310
- if (prevSet && prevSet.size === 0) this.projectIndex.delete(existing.projectId);
311
- }
312
- this.store.set(context.id, this.clone(context));
313
- let set = this.projectIndex.get(context.projectId);
314
- if (!set) {
315
- set = /* @__PURE__ */ new Set();
316
- this.projectIndex.set(context.projectId, set);
317
- }
318
- set.add(context.id);
319
- }
320
- async get(id) {
321
- const context = this.store.get(id);
322
- if (!context) {
323
- throw new Error(`Context not found: ${id}`);
324
- }
325
- return this.clone(context);
326
- }
327
- async getByProjectId(projectId) {
328
- const ids = this.projectIndex.get(projectId);
329
- if (!ids || ids.size === 0) return [];
330
- return [...ids].map((id) => this.store.get(id)).filter((c) => Boolean(c)).map((c) => this.clone(c));
331
- }
332
- clone(context) {
333
- return ModelContext.rehydrate({
334
- id: context.id,
335
- projectId: context.projectId,
336
- items: [...context.getItems()]
337
- });
338
- }
339
- };
340
-
341
286
  // src/domain/model-context/context-item/client-item/system-message.ts
342
287
  var SystemMessageItem = class _SystemMessageItem extends ContextItem {
343
288
  constructor(content) {
@@ -356,397 +301,598 @@ var SystemMessageItem = class _SystemMessageItem extends ContextItem {
356
301
  }
357
302
  };
358
303
 
359
- // src/domain/agentic-environment/participant.ts
360
- var Participant = class {
361
- constructor() {
362
- this.environments = [];
363
- this.listens = [];
364
- this.active = /* @__PURE__ */ new Map();
304
+ // src/domain/agentic-environment/semantic-event/event.ts
305
+ var SemanticEvent = class _SemanticEvent {
306
+ constructor(type, producerId, occurredAt, payload) {
307
+ this.type = type;
308
+ this.producerId = producerId;
309
+ this.occurredAt = occurredAt;
310
+ this.payload = payload;
365
311
  }
366
- join(environment) {
367
- if (this.isJoinedTo(environment)) {
368
- return;
369
- }
370
- environment.subscribe(this);
371
- this.environments.push(environment);
372
- this.active.set(environment, true);
312
+ static create(type, producerId, payload) {
313
+ const occurredAt = /* @__PURE__ */ new Date();
314
+ return new _SemanticEvent(type, producerId, occurredAt, payload);
373
315
  }
374
- leave(environment) {
375
- if (!this.isJoinedTo(environment)) {
376
- return;
377
- }
378
- environment.unsubscribe(this);
379
- this.environments = this.environments.filter((e) => e !== environment);
380
- this.active.delete(environment);
381
- }
382
- isJoinedTo(environment) {
383
- return this.environments.includes(environment);
316
+ };
317
+ var MessageSentEvent = class extends SemanticEvent {
318
+ static init(producerId, message) {
319
+ return SemanticEvent.create("message.sent", producerId, { message });
384
320
  }
385
- getEnvironments() {
386
- return this.environments;
321
+ };
322
+
323
+ // src/application/states/inference.ts
324
+ var InferenceState = class {
325
+ constructor(inferenceRunner) {
326
+ this.inferenceRunner = inferenceRunner;
327
+ this.id = "inference";
328
+ }
329
+ async run(input, loopVisitor) {
330
+ loopVisitor.visitInferenceStarted(input);
331
+ const output = await this.inferenceRunner.run(input);
332
+ loopVisitor.visitInferenceCompleted(output);
333
+ return {
334
+ stateId: this.id,
335
+ input,
336
+ output
337
+ };
387
338
  }
388
- getListeners() {
389
- return this.listens;
339
+ };
340
+
341
+ // src/domain/agentic-environment/participant/participant.ts
342
+ var Participant = class {
343
+ constructor(manifest, handlers) {
344
+ this.manifest = manifest;
345
+ this.handlers = handlers;
390
346
  }
391
- getEnvironmentState(environment) {
392
- return this.active.get(environment);
347
+ getManifest() {
348
+ return this.manifest;
393
349
  }
394
- markInactive(environment) {
395
- if (this.active.has(environment)) {
396
- this.active.set(environment, false);
397
- }
350
+ getId() {
351
+ return this.manifest.id;
398
352
  }
399
- markActive(environment) {
400
- if (this.active.has(environment)) {
401
- this.active.set(environment, true);
402
- }
353
+ getHandlers() {
354
+ return this.handlers;
403
355
  }
404
356
  };
405
357
 
406
- // src/domain/agentic-environment/errors/base-error.ts
407
- var AgenticError = class extends Error {
408
- constructor({ message, stack, source, enviornment }) {
409
- super();
410
- this.message = message;
411
- this.stack = stack;
412
- this.source = source;
413
- this.environment = enviornment;
358
+ // src/domain/agentic-environment/participant/memory.ts
359
+ var Memory = class _Memory {
360
+ constructor(context) {
361
+ this.context = context;
414
362
  }
415
- getSource() {
416
- return this.source;
363
+ getContext() {
364
+ return this.context;
417
365
  }
418
- getEnvironment() {
419
- return this.environment;
366
+ static create() {
367
+ const context = ModelContext.create();
368
+ return new _Memory(context);
420
369
  }
421
370
  };
422
371
 
423
- // src/domain/agentic-environment/agentic-environment.ts
424
- var AgenticEnvironment = class {
425
- constructor(name, opts) {
426
- this.subscribers = [];
427
- var _a;
428
- this.name = name;
429
- this.options = opts;
430
- this.showLogs = ((_a = this.options) == null ? void 0 : _a.silent) !== false;
431
- }
432
- subscribe(participant) {
433
- this.subscribers.push(participant);
434
- for (const subscriber of this.subscribers) {
435
- if (participant === subscriber) {
436
- participant.onJoined();
437
- } else {
438
- subscriber.onParticipantJoined(participant);
439
- }
440
- }
372
+ // src/domain/agentic-environment/participant/agent.ts
373
+ var Agent = class _Agent extends Participant {
374
+ constructor(manifest, developerMessage, tools, memory, handlers) {
375
+ super(manifest, handlers);
376
+ this.memory = memory;
377
+ this.developerMessage = developerMessage;
378
+ this.tools = tools;
379
+ }
380
+ static create({
381
+ name,
382
+ instruction,
383
+ tools,
384
+ capabilities,
385
+ handlers
386
+ }) {
387
+ const id = crypto.randomUUID();
388
+ const memory = Memory.create();
389
+ const manifest = { id, name, capabilities, role: "agent" };
390
+ const developerMessage = DeveloperMessageItem.create(instruction);
391
+ memory.getContext().addItem(developerMessage);
392
+ return new _Agent(manifest, instruction, tools, memory, handlers);
441
393
  }
442
- unsubscribe(participant) {
443
- const exists = this.subscribers.includes(participant);
444
- if (!exists) {
445
- return;
446
- }
447
- participant.onLeft();
448
- this.subscribers = this.subscribers.filter((p) => p !== participant);
449
- for (const subscriber of this.subscribers) {
450
- subscriber.onParticipantLeft(participant);
451
- }
394
+ getTools() {
395
+ return this.tools;
452
396
  }
453
- deliverFunctionCall(source, item) {
454
- this.deliverToSubscribers(source, {
455
- internal: (src) => src.onFunctionCall(item),
456
- external: (sub) => sub.onExternalFunctionCall(source, item)
457
- });
397
+ getDeveloperMessage() {
398
+ return this.developerMessage;
458
399
  }
459
- deliverModelMessage(source, item) {
460
- this.deliverToSubscribers(source, {
461
- internal: (src) => src.onModelMessage(item),
462
- external: (sub) => sub.onExternalModelMessage(source, item)
463
- });
400
+ getMemory() {
401
+ return this.memory;
464
402
  }
465
- deliverReasoning(source, item) {
466
- this.deliverToSubscribers(source, {
467
- internal: (src) => src.onReasoning(item),
468
- external: (sub) => sub.onExternalReasoning(source, item)
469
- });
403
+ };
404
+
405
+ // src/application/use-cases/create-agent.ts
406
+ function createAgent({
407
+ name,
408
+ capabilities,
409
+ instruction,
410
+ tools,
411
+ handlers
412
+ }) {
413
+ return Agent.create({ name, instruction, capabilities, tools, handlers });
414
+ }
415
+
416
+ // src/domain/agentic-environment/participant/human.ts
417
+ var Human = class _Human extends Participant {
418
+ constructor(manifest, handlers) {
419
+ super(manifest, handlers);
420
+ }
421
+ static create({
422
+ name,
423
+ capabilities,
424
+ handlers
425
+ }) {
426
+ const id = crypto.randomUUID();
427
+ const manifest = { id, name, capabilities, role: "human" };
428
+ return new _Human(manifest, handlers);
470
429
  }
471
- deliverFunctionCallOutput(source, item) {
472
- this.deliverToSubscribers(source, {
473
- internal: (src) => src.onFunctionCallOutput(item),
474
- external: (sub) => sub.onExternalFunctionCallOutput(source, item)
475
- });
430
+ };
431
+
432
+ // src/application/use-cases/create-human.ts
433
+ function createHuman({
434
+ name,
435
+ capabilities,
436
+ handlers
437
+ }) {
438
+ return Human.create({ name, capabilities, handlers });
439
+ }
440
+
441
+ // src/domain/agentic-environment/runtime-state.ts
442
+ var RuntimeState = class {
443
+ constructor() {
444
+ this.participants = /* @__PURE__ */ new Map();
476
445
  }
477
- deliverMessage(source, message) {
478
- this.deliverToSubscribers(source, {
479
- external: (sub) => sub.onMessage(message)
480
- });
446
+ addParticipant(participant) {
447
+ const isExists = this.participants.get(participant.getId());
448
+ if (isExists) return;
449
+ this.participants.set(participant.getId(), participant);
481
450
  }
482
- deliverSemanticEvent(source, item) {
483
- this.deliverToSubscribers(source, {
484
- internal: (src) => src.onInternalEvent(item),
485
- external: (sub) => sub.onExternalEvent(source, item)
486
- });
451
+ removeParticipant(participant) {
452
+ this.participants.delete(participant.getId());
487
453
  }
488
- deliverError(source, error) {
489
- this.deliverToSubscribers(source, {
490
- internal: (src) => {
491
- try {
492
- src.onError(error);
493
- } catch (error2) {
494
- if (this.showLogs) {
495
- console.warn(
496
- `[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
497
- ${JSON.stringify({ error: error2 }, null, 2)}`
498
- );
499
- }
500
- } finally {
501
- src.markInactive(this);
502
- }
503
- },
504
- external: (sub) => {
505
- try {
506
- sub.onParticipantError(source, error);
507
- } catch (error2) {
508
- if (this.showLogs) {
509
- console.warn(
510
- `[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
511
- ${JSON.stringify({ error: error2 }, null, 2)}`
512
- );
513
- }
514
- }
515
- }
516
- });
454
+ getParticipant(id) {
455
+ return this.participants.get(id);
517
456
  }
518
- getListeningParticipants(source) {
519
- return this.subscribers.filter(
520
- (sub) => sub !== source && (sub.getListeners().length === 0 || sub.getListeners().some((listener) => source instanceof listener))
521
- );
457
+ getParticipants() {
458
+ return [...this.participants.values()];
522
459
  }
523
- deliverToSubscribers(source, handlers) {
524
- var _a, _b;
525
- const authorizedListeners = this.getListeningParticipants(source);
526
- for (const subscriber of this.subscribers) {
527
- const isActive = subscriber.getEnvironmentState(this);
528
- if (isActive) {
529
- try {
530
- if (subscriber === source) {
531
- (_a = handlers == null ? void 0 : handlers.internal) == null ? void 0 : _a.call(handlers, subscriber);
532
- } else if (authorizedListeners.includes(subscriber)) {
533
- (_b = handlers == null ? void 0 : handlers.external) == null ? void 0 : _b.call(handlers, subscriber);
534
- }
535
- } catch (error) {
536
- this.handleError(subscriber, error);
537
- }
538
- }
460
+ };
461
+
462
+ // src/application/services/runtime.ts
463
+ var RuntimeService = class {
464
+ constructor(state, processor, inferenceRunner, functionCallRunner) {
465
+ this.state = state;
466
+ this.processor = processor;
467
+ this.inferenceRunner = inferenceRunner;
468
+ this.functionCallRunner = functionCallRunner;
469
+ }
470
+ join(participant) {
471
+ this.state.addParticipant(participant);
472
+ }
473
+ leave(participant) {
474
+ this.state.removeParticipant(participant);
475
+ }
476
+ publish(event) {
477
+ for (const participant of this.state.getParticipants()) {
478
+ this.processor.process(event, participant);
539
479
  }
540
480
  }
541
- handleError(subscriber, error) {
542
- if (error instanceof AgenticError) {
543
- return this.deliverError(subscriber, error);
481
+ getParticipant(id) {
482
+ if (!this.state.getParticipant(id)) {
483
+ throw new Error(`Participant ${id} not found`);
544
484
  }
545
- const agenticError = new AgenticError({
546
- message: error.message,
547
- stack: error.stack,
548
- enviornment: this,
549
- source: subscriber
550
- });
551
- return this.deliverError(subscriber, agenticError);
485
+ return this.state.getParticipant(id);
552
486
  }
553
- };
554
-
555
- // src/domain/model-context/semantic-event/semantic-event.ts
556
- var SemanticEvent = class {
557
- constructor(type, data) {
558
- this.type = type;
559
- this.data = data;
487
+ getInferenceRunner() {
488
+ return this.inferenceRunner;
560
489
  }
561
- getType() {
562
- return this.type;
490
+ getFunctionCallRunner() {
491
+ return this.functionCallRunner;
563
492
  }
564
493
  };
565
494
 
566
- // src/domain/agentic-environment/inference/response.ts
567
- var InferenceResponse = class {
568
- constructor(contextItems, tokenUsage) {
569
- this.contextItems = contextItems;
570
- this.tokenUsage = tokenUsage;
495
+ // src/domain/agentic-environment/semantic-event/event-processor.ts
496
+ var EventProcessor = class {
497
+ process(event, consumer) {
498
+ for (const handler of consumer.getHandlers()) {
499
+ const isSatisfied = handler.specification.isSatisfiedBy({
500
+ event,
501
+ participant: consumer
502
+ });
503
+ if (isSatisfied) {
504
+ handler.processor.apply({ event, participant: consumer });
505
+ }
506
+ }
571
507
  }
572
508
  };
573
509
 
574
- // src/application/services/inference-runner.ts
575
- var StreamingInference = class {
576
- async *run(request, endpoint) {
577
- var _a;
578
- if ((_a = request.signal) == null ? void 0 : _a.aborted) {
579
- return;
510
+ // src/application/use-cases/join.ts
511
+ function createJoin(resolveRuntime) {
512
+ return function join(participant) {
513
+ resolveRuntime().join(participant);
514
+ };
515
+ }
516
+
517
+ // src/application/use-cases/leave.ts
518
+ function createLeave(resolveRuntime) {
519
+ return function leave(participant) {
520
+ resolveRuntime().leave(participant);
521
+ };
522
+ }
523
+
524
+ // src/application/use-cases/send-message.ts
525
+ function createSendMessage(resolveRuntime) {
526
+ return function sendMessage(message, senderId) {
527
+ const runtime = resolveRuntime();
528
+ const participant = runtime.getParticipant(senderId);
529
+ if (!participant) {
530
+ throw new Error(`Participant ${senderId} not found`);
531
+ }
532
+ const userMessage = MessageSentEvent.init(senderId, message);
533
+ runtime.publish(userMessage);
534
+ };
535
+ }
536
+
537
+ // src/domain/agentic-environment/loop/agent-loop.ts
538
+ var AgentLoop = class {
539
+ constructor(stateExecutor, transitionResolver, loopVisitor) {
540
+ this.stateExecutor = stateExecutor;
541
+ this.transitionResolver = transitionResolver;
542
+ this.loopVisitor = loopVisitor;
543
+ }
544
+ async run(message) {
545
+ let transition = {
546
+ nextStateId: "message_received",
547
+ input: message
548
+ };
549
+ while (transition.nextStateId !== "idle") {
550
+ const execution = await this.stateExecutor.execute(transition, this.loopVisitor);
551
+ transition = this.transitionResolver.resolve(execution);
580
552
  }
581
- yield* endpoint.stream(request, request.signal);
582
553
  }
583
554
  };
584
- var NonStreamingInference = class {
585
- async *run(request, endpoint) {
586
- var _a, _b;
587
- if ((_a = request.signal) == null ? void 0 : _a.aborted) {
588
- return;
589
- }
590
- const response = await endpoint.infer(request);
591
- for (const item of response.contextItems) {
592
- if ((_b = request.signal) == null ? void 0 : _b.aborted) {
593
- break;
594
- }
595
- yield item;
555
+
556
+ // src/domain/agentic-environment/loop/state-executor.ts
557
+ var LoopStateExecutor = class {
558
+ constructor(messageReceivedState, inferenceState, functionCallState, modelMessageState) {
559
+ this.messageReceivedState = messageReceivedState;
560
+ this.inferenceState = inferenceState;
561
+ this.functionCallState = functionCallState;
562
+ this.modelMessageState = modelMessageState;
563
+ }
564
+ async execute(transition, loopVisitor) {
565
+ switch (transition.nextStateId) {
566
+ case "message_received":
567
+ return await this.messageReceivedState.run(transition.input, loopVisitor);
568
+ case "inference":
569
+ return await this.inferenceState.run(transition.input, loopVisitor);
570
+ case "function_call":
571
+ return await this.functionCallState.run(transition.input, loopVisitor);
572
+ case "model_message":
573
+ return await this.modelMessageState.run(transition.input, loopVisitor);
596
574
  }
597
575
  }
598
576
  };
599
577
 
600
- // src/application/use-cases/run-inference.ts
601
- var RunInference = class {
602
- constructor(generativeModelRepository2, requestValidator) {
603
- this.generativeModelRepository = generativeModelRepository2;
604
- this.requestValidator = requestValidator;
578
+ // src/application/states/function-call.ts
579
+ var FunctionCallState = class {
580
+ constructor(functionCallRunner) {
581
+ this.functionCallRunner = functionCallRunner;
582
+ this.id = "function_call";
605
583
  }
606
- async execute(inferenceParams) {
584
+ async run(input, loopVisitor) {
607
585
  var _a;
608
- const { caller, environment, signal } = inferenceParams;
609
- const generativeModel = await this.generativeModelRepository.getByModelName(inferenceParams.model);
610
- this.requestValidator.validate(inferenceParams, generativeModel.specification);
611
- const resolvedParams = {
612
- ...inferenceParams,
613
- maxOutputTokens: (_a = inferenceParams.maxOutputTokens) != null ? _a : generativeModel.specification.maxOutputTokens
614
- };
615
- const inferenceRunner = inferenceParams.streaming ? new StreamingInference() : new NonStreamingInference();
616
- const result = inferenceRunner.run(resolvedParams, generativeModel.endpoint);
617
- for await (const item of result) {
618
- if (signal == null ? void 0 : signal.aborted) {
619
- break;
620
- }
621
- if (item instanceof ReasoningItem) {
622
- environment.deliverReasoning(caller, item);
623
- } else if (item instanceof FunctionCallItem) {
624
- environment.deliverFunctionCall(caller, item);
625
- } else if (item instanceof ModelMessageItem) {
626
- environment.deliverModelMessage(caller, item);
627
- } else if (item instanceof SemanticEvent) {
628
- environment.deliverSemanticEvent(caller, item);
629
- }
586
+ loopVisitor.visitFunctionCallStarted(input);
587
+ const { call, inferenceInput } = input;
588
+ let tool = (_a = inferenceInput.tools) == null ? void 0 : _a.find((tool2) => tool2.name === call.name);
589
+ if (!tool) {
590
+ return {
591
+ stateId: this.id,
592
+ input,
593
+ output: {
594
+ item: FunctionCallOutputItem.create(call.callId, `Error: unknown tool "${call.name}"`)
595
+ }
596
+ };
630
597
  }
598
+ const item = await this.functionCallRunner.run(call, tool);
599
+ loopVisitor.visitFunctionCallCompleted(item);
600
+ return {
601
+ stateId: this.id,
602
+ input,
603
+ output: {
604
+ item
605
+ }
606
+ };
631
607
  }
632
608
  };
633
609
 
634
- // src/application/use-cases/execute-function-call.ts
635
- var ExecuteFunctionCall = class {
636
- constructor(functionCallRunner) {
637
- this.functionCallRunner = functionCallRunner;
638
- }
639
- async execute(environment, functionCallItem, tool, caller, signal) {
640
- const stream = this.functionCallRunner.run(functionCallItem, tool, signal);
641
- for await (const item of stream) {
642
- environment.deliverFunctionCallOutput(caller, item);
643
- }
610
+ // src/application/states/message-received.ts
611
+ var MessageReceivedState = class {
612
+ constructor() {
613
+ this.id = "message_received";
614
+ }
615
+ async run(input, loopVisitor) {
616
+ loopVisitor.visitMessageReceivedStarted(input);
617
+ const userMessage = UserMessageItem.create(input.content);
618
+ const output = {
619
+ ...input.input,
620
+ context: input.input.context.addContextItems([userMessage])
621
+ };
622
+ loopVisitor.visitMessageReceivedCompleted(output);
623
+ return {
624
+ stateId: this.id,
625
+ input,
626
+ output
627
+ };
644
628
  }
645
629
  };
646
630
 
647
- // src/application/services/function-call-runner.ts
648
- var FunctionCallRunner = class {
649
- async *run(call, tool, signal) {
650
- if (signal == null ? void 0 : signal.aborted) {
651
- return;
652
- }
653
- if (!tool) throw new Error(`Unknown tool: ${call.name}`);
654
- const result = await tool.invoke(JSON.parse(call.args));
655
- yield FunctionCallOutputItem.create(call.callId, JSON.stringify(result));
631
+ // src/application/states/model-message.ts
632
+ var ModelMessageState = class {
633
+ constructor() {
634
+ this.id = "model_message";
635
+ }
636
+ async run(input, loopVisitor) {
637
+ loopVisitor.visitModelAnswer(input);
638
+ return {
639
+ stateId: this.id,
640
+ input,
641
+ output: void 0
642
+ };
656
643
  }
657
644
  };
658
645
 
659
- // src/application/participants/participant.ts
660
- var BaseParticipant = class extends Participant {
661
- onFunctionCall(_item) {
646
+ // src/domain/agentic-environment/loop/transition-resolver.ts
647
+ var TransitionResolver = class {
648
+ constructor(rules) {
649
+ this.rules = rules;
662
650
  }
663
- onExternalFunctionCall(_source, _item) {
651
+ resolve(execution) {
652
+ const rule = this.rules.find((rule2) => rule2.specification.isSatisfiedBy(execution));
653
+ if (!rule) {
654
+ throw new Error(`No transition rule found after "${execution.stateId}"`);
655
+ }
656
+ return rule.createTransition(execution);
664
657
  }
665
- onFunctionCallOutput(_item) {
658
+ };
659
+
660
+ // src/domain/agentic-environment/loop/transition-specification.ts
661
+ var MessageReceivedSpecification = class {
662
+ isSatisfiedBy(execution) {
663
+ return execution.stateId === "message_received";
666
664
  }
667
- onExternalFunctionCallOutput(_source, _item) {
665
+ };
666
+ var InferenceProducedFunctionCallSpecification = class {
667
+ isSatisfiedBy(execution) {
668
+ return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "function_call");
668
669
  }
669
- onReasoning(_item) {
670
+ };
671
+ var InferenceProducedModelMessageSpecification = class {
672
+ isSatisfiedBy(execution) {
673
+ return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
670
674
  }
671
- onExternalReasoning(_source, _item) {
675
+ };
676
+ var FunctionCallCompletedSpecification = class {
677
+ isSatisfiedBy(execution) {
678
+ return execution.stateId === "function_call";
672
679
  }
673
- onModelMessage(_item) {
680
+ };
681
+ var ModelMessageCompletedSpecification = class {
682
+ isSatisfiedBy(execution) {
683
+ return execution.stateId === "model_message";
674
684
  }
675
- onExternalModelMessage(_source, _item) {
685
+ };
686
+
687
+ // src/domain/agentic-environment/loop/transition-rule.ts
688
+ var MessageReceivedToInferenceRule = class {
689
+ constructor() {
690
+ this.specification = new MessageReceivedSpecification();
676
691
  }
677
- onMessage(_message) {
692
+ createTransition(execution) {
693
+ if (execution.stateId !== "message_received") {
694
+ throw new Error("Expected message_received execution");
695
+ }
696
+ return {
697
+ nextStateId: "inference",
698
+ input: execution.output
699
+ };
678
700
  }
679
- onJoined() {
701
+ };
702
+ var InferenceToFunctionCallRule = class {
703
+ constructor() {
704
+ this.specification = new InferenceProducedFunctionCallSpecification();
680
705
  }
681
- onLeft() {
706
+ createTransition(execution) {
707
+ if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "function_call")) {
708
+ throw new Error("Expected inference function-call output");
709
+ }
710
+ const functionCallItem = execution.output.items.find((item) => item.type === "function_call");
711
+ if (!functionCallItem) {
712
+ throw new Error("Expected inference function-call output");
713
+ }
714
+ return {
715
+ nextStateId: "function_call",
716
+ input: {
717
+ call: functionCallItem,
718
+ inferenceInput: execution.input
719
+ }
720
+ };
682
721
  }
683
- onParticipantJoined(_participant) {
722
+ };
723
+ var InferenceToModelMessageRule = class {
724
+ constructor() {
725
+ this.specification = new InferenceProducedModelMessageSpecification();
684
726
  }
685
- onParticipantLeft(_participant) {
727
+ createTransition(execution) {
728
+ if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
729
+ throw new Error("Expected inference model-message output");
730
+ }
731
+ const modelMessageItem = execution.output.items.find(
732
+ (item) => item.type === "message" && item.role === "assistant"
733
+ );
734
+ if (!modelMessageItem) {
735
+ throw new Error("Expected inference model-message output");
736
+ }
737
+ return {
738
+ nextStateId: "model_message",
739
+ input: {
740
+ answer: modelMessageItem
741
+ }
742
+ };
686
743
  }
687
- onInternalEvent(_item) {
744
+ };
745
+ var FunctionCallToInferenceRule = class {
746
+ constructor() {
747
+ this.specification = new FunctionCallCompletedSpecification();
688
748
  }
689
- onExternalEvent(_source, _item) {
749
+ createTransition(execution) {
750
+ if (execution.stateId !== "function_call") {
751
+ throw new Error("Expected function_call execution");
752
+ }
753
+ const previousRequest = execution.input.inferenceInput;
754
+ previousRequest.context.addContextItems([execution.input.call, execution.output.item]);
755
+ return {
756
+ nextStateId: "inference",
757
+ input: {
758
+ ...previousRequest
759
+ }
760
+ };
690
761
  }
691
- onError(_error) {
762
+ };
763
+ var ModelMessageToIdleRule = class {
764
+ constructor() {
765
+ this.specification = new ModelMessageCompletedSpecification();
692
766
  }
693
- onParticipantError(_source, _error) {
767
+ createTransition(execution) {
768
+ if (execution.stateId !== "model_message") {
769
+ throw new Error("Expected model_message execution");
770
+ }
771
+ return {
772
+ nextStateId: "idle",
773
+ input: void 0
774
+ };
694
775
  }
695
776
  };
696
777
 
697
- // src/application/use-cases/send-message.ts
698
- var SendMessage = class {
699
- async execute(environment, message, caller) {
700
- if (!caller.isJoinedTo(environment)) {
701
- throw new Error("Not joined to environment");
702
- }
703
- environment.deliverMessage(caller, message);
778
+ // src/domain/agentic-environment/loop/event-publisher-visitor.ts
779
+ var EventPublisherLoopVisitor = class {
780
+ constructor(agentId, runtime) {
781
+ this.agentId = agentId;
782
+ this.runtime = runtime;
783
+ }
784
+ visitMessageReceivedStarted(input) {
785
+ console.log("Visiting message received started: ", input);
786
+ this.publish("message_received.started", input);
787
+ }
788
+ visitMessageReceivedCompleted(output) {
789
+ console.log("Visiting message received completed: ", output);
790
+ this.publish("message_received.completed", output);
791
+ }
792
+ visitInferenceStarted(input) {
793
+ console.log("Visiting inference started: ", input);
794
+ this.publish("inference.started", input);
795
+ }
796
+ visitInferenceEvent(event) {
797
+ console.log("Visiting inference event: ", event);
798
+ this.publish("inference.stream", event);
799
+ }
800
+ visitInferenceCompleted(output) {
801
+ console.log("Visiting inference completed: ", output);
802
+ this.publish("inference.completed", output);
803
+ }
804
+ visitFunctionCallStarted(input) {
805
+ console.log("Visiting function call started: ", input);
806
+ this.publish("function_call.started", input);
807
+ }
808
+ visitFunctionCallCompleted(output) {
809
+ console.log("Visiting function call completed: ", output);
810
+ this.publish("function_call.completed", output);
811
+ }
812
+ visitModelAnswer(input) {
813
+ console.log("Visiting model answer: ", input.answer);
814
+ this.publish("model.answer", input);
815
+ }
816
+ publish(type, payload) {
817
+ this.runtime.publish({
818
+ type,
819
+ producerId: this.agentId,
820
+ occurredAt: /* @__PURE__ */ new Date(),
821
+ payload
822
+ });
704
823
  }
705
824
  };
706
825
 
826
+ // src/application/use-cases/run-loop.ts
827
+ function createRunLoop(resolveRuntime) {
828
+ return function runLoop(agentId, message, inferenceInput) {
829
+ const runtime = resolveRuntime();
830
+ const inferenceRunner = runtime.getInferenceRunner();
831
+ const functionCallRunner = runtime.getFunctionCallRunner();
832
+ const transitionResolver = new TransitionResolver([
833
+ // High-priority interception rules would go first.
834
+ new MessageReceivedToInferenceRule(),
835
+ new InferenceToFunctionCallRule(),
836
+ new InferenceToModelMessageRule(),
837
+ new FunctionCallToInferenceRule(),
838
+ new ModelMessageToIdleRule()
839
+ ]);
840
+ const stateExecutor = new LoopStateExecutor(
841
+ new MessageReceivedState(),
842
+ new InferenceState(inferenceRunner),
843
+ new FunctionCallState(functionCallRunner),
844
+ new ModelMessageState()
845
+ );
846
+ const agentLoop = new AgentLoop(
847
+ stateExecutor,
848
+ transitionResolver,
849
+ new EventPublisherLoopVisitor(agentId, runtime)
850
+ );
851
+ agentLoop.run({
852
+ content: message,
853
+ input: inferenceInput
854
+ });
855
+ };
856
+ }
857
+
707
858
  // src/infrastructure/providers/anthropic/endpoints/anthropic-messages-mapper.ts
708
859
  var AnthropicMessagesMapper = class {
709
- toRequest(inferenceParams) {
710
- const { messages, system } = this.mapContextItems(inferenceParams);
860
+ toRequest(inferenceInput) {
861
+ const { messages, system } = this.mapContextItems(inferenceInput);
711
862
  const outputConfig = {};
712
863
  const request = {
713
- model: inferenceParams.model,
864
+ model: inferenceInput.model,
714
865
  messages
715
866
  };
716
- request.max_tokens = inferenceParams.maxOutputTokens;
867
+ request.max_tokens = inferenceInput.maxOutputTokens;
717
868
  if (system) {
718
869
  request.system = system;
719
870
  }
720
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
721
- request.tools = inferenceParams.tools.map((tool) => ({
871
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
872
+ request.tools = inferenceInput.tools.map((tool) => ({
722
873
  name: tool.name,
723
874
  description: tool.description,
724
875
  input_schema: tool.parameters
725
876
  }));
726
877
  }
727
- if (inferenceParams.structuredOutput) {
878
+ if (inferenceInput.structuredOutput) {
728
879
  outputConfig.format = {
729
880
  type: "json_schema",
730
- json_schema: inferenceParams.structuredOutput.schema
881
+ json_schema: inferenceInput.structuredOutput.schema
731
882
  };
732
883
  }
733
- if (inferenceParams.reasoningEffort) {
884
+ if (inferenceInput.reasoningEffort) {
734
885
  request.thinking = { type: "adaptive" };
735
- outputConfig.effort = inferenceParams.reasoningEffort;
886
+ outputConfig.effort = inferenceInput.reasoningEffort;
736
887
  }
737
888
  if (Object.keys(outputConfig).length > 0) {
738
889
  request.output_config = outputConfig;
739
890
  }
740
- if (inferenceParams.streaming) {
741
- request.stream = inferenceParams.streaming;
891
+ if (inferenceInput.streaming) {
892
+ request.stream = inferenceInput.streaming;
742
893
  }
743
894
  return request;
744
895
  }
745
- toResponse(response) {
746
- const contextItems = this.extractContextItems(response);
747
- const tokenUsage = this.extractTokenUsage(response);
748
- return new InferenceResponse(contextItems, tokenUsage);
749
- }
750
896
  addContentBlock(messages, role, block) {
751
897
  const lastMessage = messages[messages.length - 1];
752
898
  if ((lastMessage == null ? void 0 : lastMessage.role) === role) {
@@ -758,9 +904,9 @@ var AnthropicMessagesMapper = class {
758
904
  content: [block]
759
905
  });
760
906
  }
761
- mapContextItems(inferenceParams) {
907
+ mapContextItems(inferenceInput) {
762
908
  var _a, _b, _c;
763
- const context = inferenceParams.context;
909
+ const context = inferenceInput.context;
764
910
  const messages = [];
765
911
  const system = [];
766
912
  for (const item of context.getItems()) {
@@ -827,7 +973,7 @@ var AnthropicMessagesMapper = class {
827
973
  new OutputTokenDetails(0)
828
974
  );
829
975
  }
830
- extractContextItems(response) {
976
+ toResponse(response) {
831
977
  var _a;
832
978
  const items = [];
833
979
  for (const block of response.content) {
@@ -855,30 +1001,27 @@ var AnthropicMessagesMapper = class {
855
1001
  );
856
1002
  }
857
1003
  }
858
- return items;
1004
+ return { items, rowResponse: response };
859
1005
  }
860
1006
  };
861
1007
 
862
1008
  // src/infrastructure/providers/anthropic/endpoints/anthropic-messages.ts
863
1009
  import Anthropic from "@anthropic-ai/sdk";
864
1010
  var AnthropicMessages = class {
865
- constructor(endpointMapper = new AnthropicMessagesMapper()) {
1011
+ constructor(endpointMapper = new AnthropicMessagesMapper(), config = {}) {
866
1012
  this.endpointMapper = endpointMapper;
867
- this.client = new Anthropic();
1013
+ this.client = new Anthropic({ baseURL: config.baseURL, apiKey: config.apiKey });
868
1014
  }
869
- async infer(inferenceParams) {
870
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
871
- const response = await this.client.messages.create(inferenceRequest);
1015
+ async infer(inferenceInput) {
1016
+ const request = this.endpointMapper.toRequest(inferenceInput);
1017
+ const response = await this.client.messages.create(request);
872
1018
  return this.endpointMapper.toResponse(response);
873
1019
  }
874
- async *stream(inferenceParams, signal) {
875
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
876
- const stream = await this.client.messages.create(inferenceRequest);
1020
+ async *stream(inferenceInput) {
1021
+ const request = this.endpointMapper.toRequest(inferenceInput);
1022
+ const stream = await this.client.messages.create(request);
877
1023
  for await (const event of stream) {
878
- if (signal == null ? void 0 : signal.aborted) {
879
- break;
880
- }
881
- yield new SemanticEvent(event.type, event);
1024
+ yield event;
882
1025
  }
883
1026
  }
884
1027
  };
@@ -900,27 +1043,27 @@ var SummaryText = class _SummaryText extends ItemContent {
900
1043
 
901
1044
  // src/infrastructure/providers/openai/endpoints/openai-responses-mapper.ts
902
1045
  var OpenAIResponsesMapper = class {
903
- toRequest(inferenceParams) {
1046
+ toRequest(inferenceInput) {
904
1047
  var _a, _b;
905
1048
  const request = {
906
- model: inferenceParams.model,
907
- input: this.mapContextItems(inferenceParams)
1049
+ model: inferenceInput.model,
1050
+ input: this.mapContextItems(inferenceInput)
908
1051
  };
909
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
910
- request.tools = inferenceParams.tools.map((tool) => ({
1052
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1053
+ request.tools = inferenceInput.tools.map((tool) => ({
911
1054
  type: tool.type,
912
1055
  name: tool.name,
913
1056
  description: tool.description,
914
1057
  parameters: tool.parameters
915
1058
  }));
916
1059
  }
917
- if (inferenceParams.reasoningEffort) {
1060
+ if (inferenceInput.reasoningEffort) {
918
1061
  request.reasoning = {
919
- effort: inferenceParams.reasoningEffort
1062
+ effort: inferenceInput.reasoningEffort
920
1063
  };
921
1064
  }
922
- if (inferenceParams.structuredOutput) {
923
- const format = inferenceParams.structuredOutput;
1065
+ if (inferenceInput.structuredOutput) {
1066
+ const format = inferenceInput.structuredOutput;
924
1067
  request.text = {
925
1068
  format: {
926
1069
  type: "json_schema",
@@ -930,19 +1073,14 @@ var OpenAIResponsesMapper = class {
930
1073
  }
931
1074
  };
932
1075
  }
933
- if (inferenceParams.streaming) {
934
- request.stream = inferenceParams.streaming;
1076
+ if (inferenceInput.streaming) {
1077
+ request.stream = inferenceInput.streaming;
935
1078
  }
936
1079
  return request;
937
1080
  }
938
- toResponse(response) {
939
- const contextItems = this.extractContextItems(response);
940
- const tokenUsage = this.extractTokenUsage(response);
941
- return new InferenceResponse(contextItems, tokenUsage);
942
- }
943
- mapContextItems(inferenceParams) {
1081
+ mapContextItems(inferenceInput) {
944
1082
  const input = [];
945
- for (const item of inferenceParams.context.getItems()) {
1083
+ for (const item of inferenceInput.context.getItems()) {
946
1084
  if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem || item instanceof UserMessageItem) {
947
1085
  input.push({
948
1086
  type: item.type,
@@ -1002,7 +1140,7 @@ var OpenAIResponsesMapper = class {
1002
1140
  new OutputTokenDetails((_d = (_c = response.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
1003
1141
  );
1004
1142
  }
1005
- extractContextItems(response) {
1143
+ toResponse(response) {
1006
1144
  var _a, _b, _c;
1007
1145
  const items = [];
1008
1146
  for (const item of (_a = response.output) != null ? _a : []) {
@@ -1035,7 +1173,10 @@ var OpenAIResponsesMapper = class {
1035
1173
  );
1036
1174
  }
1037
1175
  }
1038
- return items;
1176
+ return {
1177
+ items,
1178
+ rowResponse: response
1179
+ };
1039
1180
  }
1040
1181
  };
1041
1182
 
@@ -1045,19 +1186,16 @@ var OpenAIResponses = class {
1045
1186
  this.endpointMapper = endpointMapper;
1046
1187
  this.client = new OpenAI();
1047
1188
  }
1048
- async infer(inferenceParams) {
1049
- const request = this.endpointMapper.toRequest(inferenceParams);
1189
+ async infer(inferenceInput) {
1190
+ const request = this.endpointMapper.toRequest(inferenceInput);
1050
1191
  const response = await this.client.responses.create(request);
1051
1192
  return this.endpointMapper.toResponse(response);
1052
1193
  }
1053
- async *stream(inferenceParams, signal) {
1054
- const request = this.endpointMapper.toRequest(inferenceParams);
1194
+ async *stream(inferenceInput) {
1195
+ const request = this.endpointMapper.toRequest(inferenceInput);
1055
1196
  const response = await this.client.responses.create(request);
1056
1197
  for await (const event of response) {
1057
- if (signal == null ? void 0 : signal.aborted) {
1058
- break;
1059
- }
1060
- yield new SemanticEvent(event.type, event);
1198
+ yield event;
1061
1199
  }
1062
1200
  }
1063
1201
  };
@@ -1067,16 +1205,16 @@ import { GoogleGenAI } from "@google/genai";
1067
1205
 
1068
1206
  // src/infrastructure/providers/gemini/endpoints/gemini-generate-content-mapper.ts
1069
1207
  var GeminiGenerateContentMapper = class {
1070
- toRequest(inferenceParams) {
1071
- const { contents, systemInstruction } = this.mapContextItems(inferenceParams);
1208
+ toRequest(inferenceInput) {
1209
+ const { contents, systemInstruction } = this.mapContextItems(inferenceInput);
1072
1210
  const config = {};
1073
1211
  if (systemInstruction) {
1074
1212
  config.systemInstruction = systemInstruction;
1075
1213
  }
1076
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
1214
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1077
1215
  config.tools = [
1078
1216
  {
1079
- functionDeclarations: inferenceParams.tools.map((tool) => ({
1217
+ functionDeclarations: inferenceInput.tools.map((tool) => ({
1080
1218
  name: tool.name,
1081
1219
  description: tool.description,
1082
1220
  parametersJsonSchema: tool.parameters
@@ -1084,34 +1222,29 @@ var GeminiGenerateContentMapper = class {
1084
1222
  }
1085
1223
  ];
1086
1224
  }
1087
- if (inferenceParams.structuredOutput) {
1225
+ if (inferenceInput.structuredOutput) {
1088
1226
  config.responseMimeType = "application/json";
1089
- config.responseSchema = inferenceParams.structuredOutput.schema;
1227
+ config.responseSchema = inferenceInput.structuredOutput.schema;
1090
1228
  }
1091
- if (inferenceParams.reasoningEffort) {
1229
+ if (inferenceInput.reasoningEffort) {
1092
1230
  config.thinkingConfig = {
1093
- thinkingLevel: inferenceParams.reasoningEffort,
1231
+ thinkingLevel: inferenceInput.reasoningEffort,
1094
1232
  includeThoughts: true
1095
1233
  };
1096
1234
  }
1097
1235
  const request = {
1098
- model: inferenceParams.model,
1236
+ model: inferenceInput.model,
1099
1237
  contents,
1100
1238
  config
1101
1239
  };
1102
- if (inferenceParams.streaming) {
1103
- request.stream = inferenceParams.streaming;
1240
+ if (inferenceInput.streaming) {
1241
+ request.stream = inferenceInput.streaming;
1104
1242
  }
1105
1243
  return request;
1106
1244
  }
1107
- toResponse(response) {
1108
- const contextItems = this.extractContextItems(response);
1109
- const tokenUsage = this.extractTokenUsage(response);
1110
- return new InferenceResponse(contextItems, tokenUsage);
1111
- }
1112
- mapContextItems(inferenceParams) {
1245
+ mapContextItems(inferenceInput) {
1113
1246
  var _a;
1114
- const context = inferenceParams.context;
1247
+ const context = inferenceInput.context;
1115
1248
  const contents = [];
1116
1249
  const system = [];
1117
1250
  const callNames = /* @__PURE__ */ new Map();
@@ -1184,7 +1317,7 @@ var GeminiGenerateContentMapper = class {
1184
1317
  new OutputTokenDetails((_e = usage.thoughtsTokenCount) != null ? _e : 0)
1185
1318
  );
1186
1319
  }
1187
- extractContextItems(response) {
1320
+ toResponse(response) {
1188
1321
  var _a, _b, _c, _d, _e, _f;
1189
1322
  const items = [];
1190
1323
  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 : [];
@@ -1213,42 +1346,43 @@ var GeminiGenerateContentMapper = class {
1213
1346
  );
1214
1347
  }
1215
1348
  }
1216
- return items;
1349
+ return { items, rowResponse: response };
1217
1350
  }
1218
1351
  };
1219
1352
 
1220
1353
  // src/infrastructure/providers/gemini/endpoints/gemini-generate-content.ts
1221
1354
  var GeminiGenerateContent = class {
1222
- constructor(endpointMapper = new GeminiGenerateContentMapper()) {
1355
+ constructor(endpointMapper = new GeminiGenerateContentMapper(), config = {}) {
1356
+ var _a;
1223
1357
  this.endpointMapper = endpointMapper;
1224
- this.client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
1358
+ this.client = new GoogleGenAI({
1359
+ apiKey: (_a = config.apiKey) != null ? _a : process.env.GEMINI_API_KEY,
1360
+ ...config.baseURL ? { httpOptions: { baseUrl: config.baseURL } } : {}
1361
+ });
1225
1362
  }
1226
- async infer(inferenceParams) {
1227
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
1228
- const response = await this.client.models.generateContent(inferenceRequest);
1363
+ async infer(inferenceInput) {
1364
+ const request = this.endpointMapper.toRequest(inferenceInput);
1365
+ const response = await this.client.models.generateContent(request);
1229
1366
  return this.endpointMapper.toResponse(response);
1230
1367
  }
1231
- async *stream(inferenceParams, signal) {
1232
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
1233
- const stream = await this.client.models.generateContentStream(inferenceRequest);
1368
+ async *stream(inferenceInput) {
1369
+ const request = this.endpointMapper.toRequest(inferenceInput);
1370
+ const stream = await this.client.models.generateContentStream(request);
1234
1371
  for await (const chunk of stream) {
1235
- if (signal == null ? void 0 : signal.aborted) {
1236
- break;
1237
- }
1238
- yield new SemanticEvent("generate_content_chunk", chunk);
1372
+ yield chunk;
1239
1373
  }
1240
1374
  }
1241
1375
  };
1242
1376
 
1243
1377
  // src/infrastructure/providers/openai/endpoints/openai-chat-completions-mapper.ts
1244
1378
  var OpenAIChatCompletionsMapper = class {
1245
- toRequest(inferenceParams) {
1379
+ toRequest(inferenceInput) {
1246
1380
  const request = {
1247
- model: inferenceParams.model,
1248
- messages: this.mapContextItems(inferenceParams)
1381
+ model: inferenceInput.model,
1382
+ messages: this.mapContextItems(inferenceInput)
1249
1383
  };
1250
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
1251
- request.tools = inferenceParams.tools.map((tool) => ({
1384
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1385
+ request.tools = inferenceInput.tools.map((tool) => ({
1252
1386
  type: "function",
1253
1387
  function: {
1254
1388
  name: tool.name,
@@ -1257,23 +1391,18 @@ var OpenAIChatCompletionsMapper = class {
1257
1391
  }
1258
1392
  }));
1259
1393
  }
1260
- if (inferenceParams.reasoningEffort) {
1261
- request.reasoning_effort = inferenceParams.reasoningEffort;
1394
+ if (inferenceInput.reasoningEffort) {
1395
+ request.reasoning_effort = inferenceInput.reasoningEffort;
1262
1396
  }
1263
- if (inferenceParams.streaming) {
1264
- request.stream = inferenceParams.streaming;
1397
+ if (inferenceInput.streaming) {
1398
+ request.stream = inferenceInput.streaming;
1265
1399
  }
1266
1400
  return request;
1267
1401
  }
1268
- toResponse(response) {
1269
- const contextItems = this.extractContextItems(response);
1270
- const tokenUsage = this.extractTokenUsage(response);
1271
- return new InferenceResponse(contextItems, tokenUsage);
1272
- }
1273
- mapContextItems(inferenceParams) {
1402
+ mapContextItems(inferenceInput) {
1274
1403
  var _a;
1275
1404
  const messages = [];
1276
- for (const item of inferenceParams.context.getItems()) {
1405
+ for (const item of inferenceInput.context.getItems()) {
1277
1406
  if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem) {
1278
1407
  messages.push({ role: "system", content: item.content.text });
1279
1408
  continue;
@@ -1320,12 +1449,12 @@ var OpenAIChatCompletionsMapper = class {
1320
1449
  new OutputTokenDetails((_d = (_c = response.usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
1321
1450
  );
1322
1451
  }
1323
- extractContextItems(response) {
1452
+ toResponse(response) {
1324
1453
  var _a, _b, _c;
1325
1454
  const items = [];
1326
1455
  const message = (_b = (_a = response.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1327
1456
  if (!message) {
1328
- return items;
1457
+ return { items: [], rowResponse: response };
1329
1458
  }
1330
1459
  if (message.reasoning_content) {
1331
1460
  items.push(
@@ -1348,7 +1477,7 @@ var OpenAIChatCompletionsMapper = class {
1348
1477
  })
1349
1478
  );
1350
1479
  }
1351
- return items;
1480
+ return { items, rowResponse: response };
1352
1481
  }
1353
1482
  };
1354
1483
 
@@ -1364,24 +1493,20 @@ var OpenAIChatCompletions = class {
1364
1493
  });
1365
1494
  this.extraBody = (_a = config.extraBody) != null ? _a : {};
1366
1495
  }
1367
- buildRequest(inferenceParams) {
1496
+ buildRequest(inferenceInput) {
1368
1497
  return {
1369
1498
  ...this.extraBody,
1370
- ...this.endpointMapper.toRequest(inferenceParams)
1499
+ ...this.endpointMapper.toRequest(inferenceInput)
1371
1500
  };
1372
1501
  }
1373
- async infer(inferenceParams) {
1374
- const response = await this.client.chat.completions.create(this.buildRequest(inferenceParams));
1502
+ async infer(inferenceInput) {
1503
+ const response = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
1375
1504
  return this.endpointMapper.toResponse(response);
1376
1505
  }
1377
- async *stream(inferenceParams, signal) {
1378
- var _a;
1379
- const stream = await this.client.chat.completions.create(this.buildRequest(inferenceParams));
1506
+ async *stream(inferenceInput) {
1507
+ const stream = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
1380
1508
  for await (const event of stream) {
1381
- if (signal == null ? void 0 : signal.aborted) {
1382
- break;
1383
- }
1384
- yield new SemanticEvent((_a = event.object) != null ? _a : "chat.completion.chunk", event);
1509
+ yield event;
1385
1510
  }
1386
1511
  }
1387
1512
  };
@@ -1650,103 +1775,71 @@ var claudeHaiku45Specification = {
1650
1775
  supportsStructuredOutput: true
1651
1776
  };
1652
1777
 
1653
- // src/infrastructure/repository/generative-model-repository.ts
1654
- var InMemoryGenerativeModelRepository = class {
1655
- getByModelName(modelName) {
1656
- let generativeModel = void 0;
1657
- switch (modelName) {
1658
- case "gpt-5.4":
1659
- generativeModel = {
1660
- endpoint: new OpenAIResponses(),
1661
- specification: gpt54Specification
1662
- };
1663
- break;
1664
- case "gpt-5.4-mini":
1665
- generativeModel = {
1666
- endpoint: new OpenAIResponses(),
1667
- specification: gpt54MiniSpecification
1668
- };
1669
- break;
1670
- case "gpt-5.4-nano":
1671
- generativeModel = {
1672
- endpoint: new OpenAIResponses(),
1673
- specification: gpt54NanoSpecification
1674
- };
1675
- break;
1676
- case "gpt-5.5":
1677
- generativeModel = {
1678
- endpoint: new OpenAIResponses(),
1679
- specification: gpt55Specification
1680
- };
1681
- break;
1682
- case "claude-haiku-4-5":
1683
- generativeModel = {
1684
- endpoint: new AnthropicMessages(),
1685
- specification: claudeHaiku45Specification
1686
- };
1687
- break;
1688
- case "claude-sonnet-4-6":
1689
- generativeModel = {
1690
- endpoint: new AnthropicMessages(),
1691
- specification: claudeSonnet46Specification
1692
- };
1693
- break;
1694
- case "claude-opus-4-7":
1695
- generativeModel = {
1696
- endpoint: new AnthropicMessages(),
1697
- specification: claudeOpus47Specification
1698
- };
1699
- break;
1700
- case "claude-opus-4-8":
1701
- generativeModel = {
1702
- endpoint: new AnthropicMessages(),
1703
- specification: claudeOpus48Specification
1704
- };
1705
- break;
1706
- case "gemini-3.5-flash":
1707
- generativeModel = {
1708
- endpoint: new GeminiGenerateContent(),
1709
- specification: gemini35FlashSpecification
1710
- };
1711
- break;
1712
- case "gemini-3.1-pro-preview":
1713
- generativeModel = {
1714
- endpoint: new GeminiGenerateContent(),
1715
- specification: gemini31ProSpecification
1716
- };
1717
- break;
1718
- case "deepseek-v4-flash":
1719
- generativeModel = {
1720
- endpoint: new OpenAIChatCompletions(),
1721
- specification: deepSeekV4FlashSpecification
1722
- };
1723
- break;
1724
- case "deepseek-v4-pro":
1725
- generativeModel = {
1726
- endpoint: new OpenAIChatCompletions(),
1727
- specification: deepSeekV4ProSpecification
1728
- };
1729
- }
1730
- if (!generativeModel) {
1731
- throw new Error(`Unsupported model: ${modelName}`);
1732
- }
1733
- return Promise.resolve(generativeModel);
1778
+ // src/application/services/models.ts
1779
+ var supportedModels = [
1780
+ {
1781
+ endpoint: new OpenAIResponses(),
1782
+ specification: gpt54Specification
1783
+ },
1784
+ {
1785
+ endpoint: new OpenAIResponses(),
1786
+ specification: gpt54MiniSpecification
1787
+ },
1788
+ {
1789
+ endpoint: new OpenAIResponses(),
1790
+ specification: gpt54NanoSpecification
1791
+ },
1792
+ {
1793
+ endpoint: new OpenAIResponses(),
1794
+ specification: gpt55Specification
1795
+ },
1796
+ {
1797
+ endpoint: new AnthropicMessages(),
1798
+ specification: claudeHaiku45Specification
1799
+ },
1800
+ {
1801
+ endpoint: new AnthropicMessages(),
1802
+ specification: claudeSonnet46Specification
1803
+ },
1804
+ {
1805
+ endpoint: new AnthropicMessages(),
1806
+ specification: claudeOpus47Specification
1807
+ },
1808
+ {
1809
+ endpoint: new AnthropicMessages(),
1810
+ specification: claudeOpus48Specification
1811
+ },
1812
+ {
1813
+ endpoint: new GeminiGenerateContent(),
1814
+ specification: gemini35FlashSpecification
1815
+ },
1816
+ {
1817
+ endpoint: new GeminiGenerateContent(),
1818
+ specification: gemini31ProSpecification
1819
+ },
1820
+ {
1821
+ endpoint: new OpenAIChatCompletions(),
1822
+ specification: deepSeekV4FlashSpecification
1823
+ },
1824
+ {
1825
+ endpoint: new OpenAIChatCompletions(),
1826
+ specification: deepSeekV4ProSpecification
1734
1827
  }
1735
- };
1828
+ ];
1736
1829
 
1737
1830
  // src/domain/generative-model/request-validation/reasoning-effort.ts
1738
1831
  var ReasoningEffortValidation = class {
1739
1832
  constructor() {
1740
1833
  this.name = "reasoning-effort";
1741
1834
  }
1742
- isValid(inferenceParams, model) {
1743
- if (inferenceParams.reasoningEffort === void 0) {
1835
+ isValid(inferenceInput, model) {
1836
+ if (inferenceInput.reasoningEffort === void 0) {
1744
1837
  return true;
1745
1838
  }
1746
1839
  if (!model.supportsReasoningEffort) {
1747
1840
  return false;
1748
1841
  }
1749
- return model.supportedReasoningEfforts.includes(inferenceParams.reasoningEffort);
1842
+ return model.supportedReasoningEfforts.includes(inferenceInput.reasoningEffort);
1750
1843
  }
1751
1844
  };
1752
1845
 
@@ -1755,8 +1848,8 @@ var ToolCallingValidation = class {
1755
1848
  constructor() {
1756
1849
  this.name = "tool-calling";
1757
1850
  }
1758
- isValid(inferenceParams, model) {
1759
- if (inferenceParams.tools === void 0) {
1851
+ isValid(inferenceInput, model) {
1852
+ if (inferenceInput.tools === void 0) {
1760
1853
  return true;
1761
1854
  }
1762
1855
  return model.supportsFunctionCalling;
@@ -1768,8 +1861,8 @@ var StreamingValidation = class {
1768
1861
  constructor() {
1769
1862
  this.name = "streaming";
1770
1863
  }
1771
- isValid(inferenceParams, model) {
1772
- if (!inferenceParams.streaming) {
1864
+ isValid(inferenceInput, model) {
1865
+ if (!inferenceInput.streaming) {
1773
1866
  return true;
1774
1867
  }
1775
1868
  return model.supportsStreaming;
@@ -1796,8 +1889,8 @@ var ContextValidation = class {
1796
1889
  constructor() {
1797
1890
  this.name = "context";
1798
1891
  }
1799
- isValid(inferenceParams, model) {
1800
- return inferenceParams.context.items.every(
1892
+ isValid(inferenceInput, model) {
1893
+ return inferenceInput.context.items.every(
1801
1894
  (item) => model.supportedContextItemTypes.includes(getContextItemValidationKey(item))
1802
1895
  );
1803
1896
  }
@@ -1811,58 +1904,127 @@ var defaultRequestValidationRules = [
1811
1904
  new StructuredOutputValidation(),
1812
1905
  new ContextValidation()
1813
1906
  ];
1814
- var InferenceRequestValidator = class {
1907
+ var InferenceInputValidator = class {
1815
1908
  constructor(rules = defaultRequestValidationRules) {
1816
1909
  this.rules = rules;
1817
1910
  }
1818
- validate(inferenceParams, model) {
1911
+ validate(inferenceInput, model) {
1819
1912
  for (const rule of this.rules) {
1820
- if (!rule.isValid(inferenceParams, model)) {
1913
+ if (!rule.isValid(inferenceInput, model)) {
1821
1914
  throw new Error(`Request validation "${rule.name}" failed for model "${model.name}"`);
1822
1915
  }
1823
1916
  }
1824
1917
  }
1825
1918
  };
1826
1919
 
1827
- // src/index.ts
1828
- var generativeModelRepository = new InMemoryGenerativeModelRepository();
1829
- var inferenceRequestValidator = new InferenceRequestValidator();
1830
- var runInferenceUseCase = new RunInference(generativeModelRepository, inferenceRequestValidator);
1831
- var executeFunctionCallUseCase = new ExecuteFunctionCall(new FunctionCallRunner());
1832
- var sendMessageUseCase = new SendMessage();
1833
- var runInference = (inferenceParams) => {
1834
- runInferenceUseCase.execute(inferenceParams);
1835
- };
1836
- var executeFunctionCall = (environment, functionCallItem, tool, caller) => {
1837
- executeFunctionCallUseCase.execute(environment, functionCallItem, tool, caller);
1920
+ // src/application/services/inference-runner.ts
1921
+ var DefaultInferenceRunner = class {
1922
+ constructor(supportedModels2, requestValidator) {
1923
+ this.supportedModels = supportedModels2;
1924
+ this.requestValidator = requestValidator;
1925
+ }
1926
+ async run(input) {
1927
+ const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
1928
+ if (!generativeModel) {
1929
+ throw new Error(`Unsupported model: ${input.model}`);
1930
+ }
1931
+ this.requestValidator.validate(input, generativeModel.specification);
1932
+ return await generativeModel.endpoint.infer(input);
1933
+ }
1934
+ async streamInference(input) {
1935
+ const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
1936
+ if (!generativeModel) {
1937
+ throw new Error(`Unsupported model: ${input.model}`);
1938
+ }
1939
+ this.requestValidator.validate(input, generativeModel.specification);
1940
+ return await generativeModel.endpoint.stream(input);
1941
+ }
1838
1942
  };
1839
- var sendMessage = (environment, message, caller) => {
1840
- sendMessageUseCase.execute(environment, message, caller);
1943
+
1944
+ // src/application/services/function-call.ts
1945
+ var DefaultFunctionCallRunner = class {
1946
+ async run(input, tool) {
1947
+ try {
1948
+ const result = await tool.invoke(JSON.parse(input.args));
1949
+ return FunctionCallOutputItem.create(input.callId, JSON.stringify(result));
1950
+ } catch (error) {
1951
+ return FunctionCallOutputItem.create(input.callId, `Error calling tool: ${error}`);
1952
+ }
1953
+ }
1841
1954
  };
1955
+
1956
+ // src/define-runtime.ts
1957
+ function defineRuntime() {
1958
+ let runtime = null;
1959
+ const processor = new EventProcessor();
1960
+ function initializeRuntime(config) {
1961
+ var _a, _b, _c, _d;
1962
+ if (runtime) {
1963
+ throw new Error("Runtime already initialized");
1964
+ }
1965
+ const inferenceRunner = (_d = (_a = config.inferenceRunnerConfig) == null ? void 0 : _a.runner) != null ? _d : new DefaultInferenceRunner(
1966
+ (_c = (_b = config.inferenceRunnerConfig) == null ? void 0 : _b.supportedModels) != null ? _c : supportedModels,
1967
+ new InferenceInputValidator()
1968
+ );
1969
+ const functionCallRunner = new DefaultFunctionCallRunner();
1970
+ runtime = new RuntimeService(config.state, processor, inferenceRunner, functionCallRunner);
1971
+ return runtime;
1972
+ }
1973
+ function resolveRuntime() {
1974
+ if (!runtime) {
1975
+ throw new Error("Runtime not initialized");
1976
+ }
1977
+ return runtime;
1978
+ }
1979
+ function resolveParticipant(id) {
1980
+ if (!runtime) {
1981
+ throw new Error("Runtime not initialized");
1982
+ }
1983
+ const participant = runtime.state.getParticipant(id);
1984
+ if (!participant) {
1985
+ throw new Error(`Participant ${id} not found`);
1986
+ }
1987
+ return participant;
1988
+ }
1989
+ const join = createJoin(resolveRuntime);
1990
+ const leave = createLeave(resolveRuntime);
1991
+ const sendMessage = createSendMessage(resolveRuntime);
1992
+ const runLoop = createRunLoop(resolveRuntime);
1993
+ return {
1994
+ initializeRuntime,
1995
+ resolveRuntime,
1996
+ resolveParticipant,
1997
+ join,
1998
+ leave,
1999
+ sendMessage,
2000
+ runLoop
2001
+ };
2002
+ }
1842
2003
  export {
1843
- AgenticEnvironment,
1844
- AgenticError,
1845
- BaseParticipant,
2004
+ AnthropicMessages,
1846
2005
  ContextItem,
1847
2006
  DeveloperMessageItem,
1848
2007
  FunctionCallItem,
1849
2008
  FunctionCallOutputItem,
1850
- InMemoryModelContextRepository,
1851
- InferenceResponse,
2009
+ GeminiGenerateContent,
1852
2010
  InputTokenDetails,
1853
2011
  McpClient,
1854
2012
  McpToolRegistry,
1855
2013
  ModelContext,
1856
2014
  ModelMessageItem,
2015
+ OpenAIChatCompletions,
2016
+ OpenAIResponses,
1857
2017
  OutputTokenDetails,
1858
2018
  Participant,
1859
2019
  ReasoningItem,
2020
+ RuntimeState,
1860
2021
  SemanticEvent,
1861
2022
  SystemMessageItem,
1862
2023
  TokenUsage,
1863
2024
  UserMessageItem,
1864
- executeFunctionCall,
1865
- runInference,
1866
- sendMessage
2025
+ createAgent,
2026
+ createHuman,
2027
+ defineRuntime,
2028
+ supportedModels
1867
2029
  };
1868
2030
  //# sourceMappingURL=index.mjs.map