@mozaik-ai/core 3.13.1 → 4.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -30,67 +30,52 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- AgenticEnvironment: () => AgenticEnvironment,
34
- AgenticError: () => AgenticError,
35
- BaseParticipant: () => BaseParticipant,
36
33
  ContextItem: () => ContextItem,
37
34
  DeveloperMessageItem: () => DeveloperMessageItem,
38
35
  FunctionCallItem: () => FunctionCallItem,
39
36
  FunctionCallOutputItem: () => FunctionCallOutputItem,
40
- InMemoryModelContextRepository: () => InMemoryModelContextRepository,
41
- InferenceResponse: () => InferenceResponse,
42
37
  InputTokenDetails: () => InputTokenDetails,
38
+ McpClient: () => McpClient,
39
+ McpToolRegistry: () => McpToolRegistry,
43
40
  ModelContext: () => ModelContext,
44
41
  ModelMessageItem: () => ModelMessageItem,
45
42
  OutputTokenDetails: () => OutputTokenDetails,
46
43
  Participant: () => Participant,
47
44
  ReasoningItem: () => ReasoningItem,
45
+ RuntimeState: () => RuntimeState,
48
46
  SemanticEvent: () => SemanticEvent,
49
47
  SystemMessageItem: () => SystemMessageItem,
50
48
  TokenUsage: () => TokenUsage,
51
49
  UserMessageItem: () => UserMessageItem,
52
- executeFunctionCall: () => executeFunctionCall,
53
- runInference: () => runInference,
54
- sendMessage: () => sendMessage
50
+ createAgent: () => createAgent,
51
+ createHuman: () => createHuman,
52
+ defineRuntime: () => defineRuntime
55
53
  });
56
54
  module.exports = __toCommonJS(index_exports);
57
55
 
58
56
  // src/domain/model-context/model-context.ts
59
57
  var ModelContext = class _ModelContext {
60
- constructor(id, projectId, items) {
61
- this.projectId = projectId;
58
+ constructor(id, items) {
62
59
  this.id = id;
63
60
  this.items = items;
64
61
  }
65
- addContextItem(item) {
66
- this.items.push(item);
67
- return this;
68
- }
69
- applyModelOutput(items) {
70
- for (const item of items) {
71
- const itemType = item.getType();
72
- if (itemType !== "function_call" && itemType !== "message" && itemType !== "reasoning") {
73
- throw new Error(`Invalid item type: ${itemType}`);
74
- }
75
- }
76
- this.items.push(...items);
77
- return this;
78
- }
79
62
  getItems() {
80
63
  return this.items;
81
64
  }
82
- getLastItem() {
83
- if (this.items.length === 0) {
84
- throw new Error("No items in context");
85
- }
86
- return this.items[this.items.length - 1];
65
+ addContextItems(contextItems) {
66
+ this.items.push(...contextItems);
67
+ return this;
68
+ }
69
+ addItem(item) {
70
+ this.items.push(item);
71
+ return this;
87
72
  }
88
- static create(projectId) {
73
+ static create() {
89
74
  const id = crypto.randomUUID();
90
- return new _ModelContext(id, projectId, []);
75
+ return new _ModelContext(id, []);
91
76
  }
92
77
  static rehydrate(data) {
93
- return new _ModelContext(data.id, data.projectId, data.items);
78
+ return new _ModelContext(data.id, data.items);
94
79
  }
95
80
  };
96
81
 
@@ -215,8 +200,8 @@ var StructuredOutputValidation = class {
215
200
  constructor() {
216
201
  this.name = "structured-output";
217
202
  }
218
- isValid(inferenceParams, model) {
219
- if (inferenceParams.structuredOutput === void 0) {
203
+ isValid(inferenceInput, model) {
204
+ if (inferenceInput.structuredOutput === void 0) {
220
205
  return true;
221
206
  }
222
207
  return model.supportsStructuredOutput;
@@ -261,46 +246,96 @@ var TokenUsage = class {
261
246
  }
262
247
  };
263
248
 
264
- // src/infrastructure/repository/in-memory-model-context-repository.ts
265
- var InMemoryModelContextRepository = class {
266
- constructor() {
267
- this.store = /* @__PURE__ */ new Map();
268
- this.projectIndex = /* @__PURE__ */ new Map();
269
- }
270
- async save(context) {
271
- const existing = this.store.get(context.id);
272
- if (existing && existing.projectId !== context.projectId) {
273
- const prevSet = this.projectIndex.get(existing.projectId);
274
- prevSet == null ? void 0 : prevSet.delete(existing.id);
275
- if (prevSet && prevSet.size === 0) this.projectIndex.delete(existing.projectId);
276
- }
277
- this.store.set(context.id, this.clone(context));
278
- let set = this.projectIndex.get(context.projectId);
279
- if (!set) {
280
- set = /* @__PURE__ */ new Set();
281
- this.projectIndex.set(context.projectId, set);
282
- }
283
- set.add(context.id);
284
- }
285
- async get(id) {
286
- const context = this.store.get(id);
287
- if (!context) {
288
- throw new Error(`Context not found: ${id}`);
289
- }
290
- return this.clone(context);
291
- }
292
- async getByProjectId(projectId) {
293
- const ids = this.projectIndex.get(projectId);
294
- if (!ids || ids.size === 0) return [];
295
- return [...ids].map((id) => this.store.get(id)).filter((c) => Boolean(c)).map((c) => this.clone(c));
296
- }
297
- clone(context) {
298
- return ModelContext.rehydrate({
299
- id: context.id,
300
- projectId: context.projectId,
301
- items: [...context.getItems()]
249
+ // src/infrastructure/mcp/mcp-client.ts
250
+ var import_client = require("@modelcontextprotocol/sdk/client/index.js");
251
+ var import_streamableHttp = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
252
+ var McpClient = class {
253
+ constructor(config) {
254
+ this.config = config;
255
+ this.connected = false;
256
+ var _a, _b;
257
+ const headers = { ...(_a = config.headers) != null ? _a : {} };
258
+ if (config.authToken) headers.Authorization = `Bearer ${config.authToken}`;
259
+ this.transport = new import_streamableHttp.StreamableHTTPClientTransport(
260
+ new URL(config.url),
261
+ Object.keys(headers).length > 0 ? { requestInit: { headers } } : void 0
262
+ );
263
+ this.client = new import_client.Client({ name: (_b = config.name) != null ? _b : "mozaik", version: "1.0.0" }, { capabilities: {} });
264
+ }
265
+ async connect() {
266
+ if (this.connected) return;
267
+ await this.client.connect(this.transport);
268
+ this.connected = true;
269
+ }
270
+ /** The tools this server exposes. */
271
+ async listTools() {
272
+ var _a;
273
+ await this.connect();
274
+ const res = await this.client.listTools();
275
+ return ((_a = res.tools) != null ? _a : []).map((t) => {
276
+ var _a2, _b;
277
+ return {
278
+ name: t.name,
279
+ description: (_a2 = t.description) != null ? _a2 : "",
280
+ inputSchema: (_b = t.inputSchema) != null ? _b : { type: "object", properties: {} }
281
+ };
302
282
  });
303
283
  }
284
+ /** Call a tool by name; returns its text output. Throws if the server flags an error. */
285
+ async callTool(name, args) {
286
+ await this.connect();
287
+ const res = await this.client.callTool({ name, arguments: args != null ? args : {} });
288
+ const text = extractText(res);
289
+ if (res.isError) {
290
+ throw new Error(`MCP tool "${name}" failed: ${text || "unknown error"}`);
291
+ }
292
+ return text;
293
+ }
294
+ async close() {
295
+ if (!this.connected) return;
296
+ try {
297
+ await this.client.close();
298
+ } catch (e) {
299
+ }
300
+ this.connected = false;
301
+ }
302
+ };
303
+ function extractText(res) {
304
+ const content = res.content;
305
+ if (!Array.isArray(content)) return "";
306
+ return content.filter((c) => (c == null ? void 0 : c.type) === "text" && typeof c.text === "string").map((c) => c.text).join("\n");
307
+ }
308
+
309
+ // src/infrastructure/mcp/mcp-tool-registry.ts
310
+ var McpToolRegistry = class {
311
+ constructor(servers, createClient = (config) => new McpClient(config)) {
312
+ this.servers = servers;
313
+ this.createClient = createClient;
314
+ this.clients = [];
315
+ }
316
+ /** Discover every tool across the configured servers, as FunctionTools ready to pass to a model. */
317
+ async discoverTools() {
318
+ const tools = [];
319
+ for (const server of this.servers) {
320
+ const client = this.createClient(server);
321
+ this.clients.push(client);
322
+ for (const spec of await client.listTools()) {
323
+ tools.push({
324
+ type: "function",
325
+ name: spec.name,
326
+ description: spec.description,
327
+ parameters: spec.inputSchema,
328
+ strict: false,
329
+ invoke: (args) => client.callTool(spec.name, args != null ? args : {})
330
+ });
331
+ }
332
+ }
333
+ return tools;
334
+ }
335
+ /** Close all underlying MCP connections. */
336
+ async close() {
337
+ await Promise.all(this.clients.map((c) => c.close()));
338
+ }
304
339
  };
305
340
 
306
341
  // src/domain/model-context/context-item/client-item/system-message.ts
@@ -321,397 +356,598 @@ var SystemMessageItem = class _SystemMessageItem extends ContextItem {
321
356
  }
322
357
  };
323
358
 
324
- // src/domain/agentic-environment/participant.ts
325
- var Participant = class {
326
- constructor() {
327
- this.environments = [];
328
- this.listens = [];
329
- this.active = /* @__PURE__ */ new Map();
330
- }
331
- join(environment) {
332
- if (this.isJoinedTo(environment)) {
333
- return;
334
- }
335
- environment.subscribe(this);
336
- this.environments.push(environment);
337
- this.active.set(environment, true);
359
+ // src/domain/agentic-environment/semantic-event/event.ts
360
+ var SemanticEvent = class _SemanticEvent {
361
+ constructor(type, producerId, occurredAt, payload) {
362
+ this.type = type;
363
+ this.producerId = producerId;
364
+ this.occurredAt = occurredAt;
365
+ this.payload = payload;
338
366
  }
339
- leave(environment) {
340
- if (!this.isJoinedTo(environment)) {
341
- return;
342
- }
343
- environment.unsubscribe(this);
344
- this.environments = this.environments.filter((e) => e !== environment);
345
- this.active.delete(environment);
367
+ static create(type, producerId, payload) {
368
+ const occurredAt = /* @__PURE__ */ new Date();
369
+ return new _SemanticEvent(type, producerId, occurredAt, payload);
346
370
  }
347
- isJoinedTo(environment) {
348
- return this.environments.includes(environment);
371
+ };
372
+ var MessageSentEvent = class extends SemanticEvent {
373
+ static init(producerId, message) {
374
+ return SemanticEvent.create("message.sent", producerId, { message });
349
375
  }
350
- getEnvironments() {
351
- return this.environments;
376
+ };
377
+
378
+ // src/application/states/inference.ts
379
+ var InferenceState = class {
380
+ constructor(inferenceRunner) {
381
+ this.inferenceRunner = inferenceRunner;
382
+ this.id = "inference";
383
+ }
384
+ async run(input, loopVisitor) {
385
+ loopVisitor.visitInferenceStarted(input);
386
+ const output = await this.inferenceRunner.run(input);
387
+ loopVisitor.visitInferenceCompleted(output);
388
+ return {
389
+ stateId: this.id,
390
+ input,
391
+ output
392
+ };
352
393
  }
353
- getListeners() {
354
- return this.listens;
394
+ };
395
+
396
+ // src/domain/agentic-environment/participant/participant.ts
397
+ var Participant = class {
398
+ constructor(manifest, handlers) {
399
+ this.manifest = manifest;
400
+ this.handlers = handlers;
355
401
  }
356
- getEnvironmentState(environment) {
357
- return this.active.get(environment);
402
+ getManifest() {
403
+ return this.manifest;
358
404
  }
359
- markInactive(environment) {
360
- if (this.active.has(environment)) {
361
- this.active.set(environment, false);
362
- }
405
+ getId() {
406
+ return this.manifest.id;
363
407
  }
364
- markActive(environment) {
365
- if (this.active.has(environment)) {
366
- this.active.set(environment, true);
367
- }
408
+ getHandlers() {
409
+ return this.handlers;
368
410
  }
369
411
  };
370
412
 
371
- // src/domain/agentic-environment/errors/base-error.ts
372
- var AgenticError = class extends Error {
373
- constructor({ message, stack, source, enviornment }) {
374
- super();
375
- this.message = message;
376
- this.stack = stack;
377
- this.source = source;
378
- this.environment = enviornment;
413
+ // src/domain/agentic-environment/participant/memory.ts
414
+ var Memory = class _Memory {
415
+ constructor(context) {
416
+ this.context = context;
379
417
  }
380
- getSource() {
381
- return this.source;
418
+ getContext() {
419
+ return this.context;
382
420
  }
383
- getEnvironment() {
384
- return this.environment;
421
+ static create() {
422
+ const context = ModelContext.create();
423
+ return new _Memory(context);
385
424
  }
386
425
  };
387
426
 
388
- // src/domain/agentic-environment/agentic-environment.ts
389
- var AgenticEnvironment = class {
390
- constructor(name, opts) {
391
- this.subscribers = [];
392
- var _a;
393
- this.name = name;
394
- this.options = opts;
395
- this.showLogs = ((_a = this.options) == null ? void 0 : _a.silent) !== false;
396
- }
397
- subscribe(participant) {
398
- this.subscribers.push(participant);
399
- for (const subscriber of this.subscribers) {
400
- if (participant === subscriber) {
401
- participant.onJoined();
402
- } else {
403
- subscriber.onParticipantJoined(participant);
404
- }
405
- }
427
+ // src/domain/agentic-environment/participant/agent.ts
428
+ var Agent = class _Agent extends Participant {
429
+ constructor(manifest, developerMessage, tools, memory, handlers) {
430
+ super(manifest, handlers);
431
+ this.memory = memory;
432
+ this.developerMessage = developerMessage;
433
+ this.tools = tools;
434
+ }
435
+ static create({
436
+ name,
437
+ instruction,
438
+ tools,
439
+ capabilities,
440
+ handlers
441
+ }) {
442
+ const id = crypto.randomUUID();
443
+ const memory = Memory.create();
444
+ const manifest = { id, name, capabilities, role: "agent" };
445
+ const developerMessage = DeveloperMessageItem.create(instruction);
446
+ memory.getContext().addItem(developerMessage);
447
+ return new _Agent(manifest, instruction, tools, memory, handlers);
406
448
  }
407
- unsubscribe(participant) {
408
- const exists = this.subscribers.includes(participant);
409
- if (!exists) {
410
- return;
411
- }
412
- participant.onLeft();
413
- this.subscribers = this.subscribers.filter((p) => p !== participant);
414
- for (const subscriber of this.subscribers) {
415
- subscriber.onParticipantLeft(participant);
416
- }
449
+ getTools() {
450
+ return this.tools;
417
451
  }
418
- deliverFunctionCall(source, item) {
419
- this.deliverToSubscribers(source, {
420
- internal: (src) => src.onFunctionCall(item),
421
- external: (sub) => sub.onExternalFunctionCall(source, item)
422
- });
452
+ getDeveloperMessage() {
453
+ return this.developerMessage;
423
454
  }
424
- deliverModelMessage(source, item) {
425
- this.deliverToSubscribers(source, {
426
- internal: (src) => src.onModelMessage(item),
427
- external: (sub) => sub.onExternalModelMessage(source, item)
428
- });
455
+ getMemory() {
456
+ return this.memory;
429
457
  }
430
- deliverReasoning(source, item) {
431
- this.deliverToSubscribers(source, {
432
- internal: (src) => src.onReasoning(item),
433
- external: (sub) => sub.onExternalReasoning(source, item)
434
- });
458
+ };
459
+
460
+ // src/application/use-cases/create-agent.ts
461
+ function createAgent({
462
+ name,
463
+ capabilities,
464
+ instruction,
465
+ tools,
466
+ handlers
467
+ }) {
468
+ return Agent.create({ name, instruction, capabilities, tools, handlers });
469
+ }
470
+
471
+ // src/domain/agentic-environment/participant/human.ts
472
+ var Human = class _Human extends Participant {
473
+ constructor(manifest, handlers) {
474
+ super(manifest, handlers);
475
+ }
476
+ static create({
477
+ name,
478
+ capabilities,
479
+ handlers
480
+ }) {
481
+ const id = crypto.randomUUID();
482
+ const manifest = { id, name, capabilities, role: "human" };
483
+ return new _Human(manifest, handlers);
435
484
  }
436
- deliverFunctionCallOutput(source, item) {
437
- this.deliverToSubscribers(source, {
438
- internal: (src) => src.onFunctionCallOutput(item),
439
- external: (sub) => sub.onExternalFunctionCallOutput(source, item)
440
- });
485
+ };
486
+
487
+ // src/application/use-cases/create-human.ts
488
+ function createHuman({
489
+ name,
490
+ capabilities,
491
+ handlers
492
+ }) {
493
+ return Human.create({ name, capabilities, handlers });
494
+ }
495
+
496
+ // src/domain/agentic-environment/runtime-state.ts
497
+ var RuntimeState = class {
498
+ constructor() {
499
+ this.participants = /* @__PURE__ */ new Map();
441
500
  }
442
- deliverMessage(source, message) {
443
- this.deliverToSubscribers(source, {
444
- external: (sub) => sub.onMessage(message)
445
- });
501
+ addParticipant(participant) {
502
+ const isExists = this.participants.get(participant.getId());
503
+ if (isExists) return;
504
+ this.participants.set(participant.getId(), participant);
446
505
  }
447
- deliverSemanticEvent(source, item) {
448
- this.deliverToSubscribers(source, {
449
- internal: (src) => src.onInternalEvent(item),
450
- external: (sub) => sub.onExternalEvent(source, item)
451
- });
506
+ removeParticipant(participant) {
507
+ this.participants.delete(participant.getId());
452
508
  }
453
- deliverError(source, error) {
454
- this.deliverToSubscribers(source, {
455
- internal: (src) => {
456
- try {
457
- src.onError(error);
458
- } catch (error2) {
459
- if (this.showLogs) {
460
- console.warn(
461
- `[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
462
- ${JSON.stringify({ error: error2 }, null, 2)}`
463
- );
464
- }
465
- } finally {
466
- src.markInactive(this);
467
- }
468
- },
469
- external: (sub) => {
470
- try {
471
- sub.onParticipantError(source, error);
472
- } catch (error2) {
473
- if (this.showLogs) {
474
- console.warn(
475
- `[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
476
- ${JSON.stringify({ error: error2 }, null, 2)}`
477
- );
478
- }
479
- }
480
- }
481
- });
509
+ getParticipant(id) {
510
+ return this.participants.get(id);
482
511
  }
483
- getListeningParticipants(source) {
484
- return this.subscribers.filter(
485
- (sub) => sub !== source && (sub.getListeners().length === 0 || sub.getListeners().some((listener) => source instanceof listener))
486
- );
512
+ getParticipants() {
513
+ return [...this.participants.values()];
487
514
  }
488
- deliverToSubscribers(source, handlers) {
489
- var _a, _b;
490
- const authorizedListeners = this.getListeningParticipants(source);
491
- for (const subscriber of this.subscribers) {
492
- const isActive = subscriber.getEnvironmentState(this);
493
- if (isActive) {
494
- try {
495
- if (subscriber === source) {
496
- (_a = handlers == null ? void 0 : handlers.internal) == null ? void 0 : _a.call(handlers, subscriber);
497
- } else if (authorizedListeners.includes(subscriber)) {
498
- (_b = handlers == null ? void 0 : handlers.external) == null ? void 0 : _b.call(handlers, subscriber);
499
- }
500
- } catch (error) {
501
- this.handleError(subscriber, error);
502
- }
503
- }
515
+ };
516
+
517
+ // src/application/services/runtime.ts
518
+ var RuntimeService = class {
519
+ constructor(state, processor, inferenceRunner, functionCallRunner) {
520
+ this.state = state;
521
+ this.processor = processor;
522
+ this.inferenceRunner = inferenceRunner;
523
+ this.functionCallRunner = functionCallRunner;
524
+ }
525
+ join(participant) {
526
+ this.state.addParticipant(participant);
527
+ }
528
+ leave(participant) {
529
+ this.state.removeParticipant(participant);
530
+ }
531
+ publish(event) {
532
+ for (const participant of this.state.getParticipants()) {
533
+ this.processor.process(event, participant);
504
534
  }
505
535
  }
506
- handleError(subscriber, error) {
507
- if (error instanceof AgenticError) {
508
- return this.deliverError(subscriber, error);
536
+ getParticipant(id) {
537
+ if (!this.state.getParticipant(id)) {
538
+ throw new Error(`Participant ${id} not found`);
509
539
  }
510
- const agenticError = new AgenticError({
511
- message: error.message,
512
- stack: error.stack,
513
- enviornment: this,
514
- source: subscriber
515
- });
516
- return this.deliverError(subscriber, agenticError);
540
+ return this.state.getParticipant(id);
517
541
  }
518
- };
519
-
520
- // src/domain/model-context/semantic-event/semantic-event.ts
521
- var SemanticEvent = class {
522
- constructor(type, data) {
523
- this.type = type;
524
- this.data = data;
542
+ getInferenceRunner() {
543
+ return this.inferenceRunner;
525
544
  }
526
- getType() {
527
- return this.type;
545
+ getFunctionCallRunner() {
546
+ return this.functionCallRunner;
528
547
  }
529
548
  };
530
549
 
531
- // src/domain/agentic-environment/inference/response.ts
532
- var InferenceResponse = class {
533
- constructor(contextItems, tokenUsage) {
534
- this.contextItems = contextItems;
535
- this.tokenUsage = tokenUsage;
550
+ // src/domain/agentic-environment/semantic-event/event-processor.ts
551
+ var EventProcessor = class {
552
+ process(event, consumer) {
553
+ for (const handler of consumer.getHandlers()) {
554
+ const isSatisfied = handler.specification.isSatisfiedBy({
555
+ event,
556
+ participant: consumer
557
+ });
558
+ if (isSatisfied) {
559
+ handler.processor.apply({ event, participant: consumer });
560
+ }
561
+ }
536
562
  }
537
563
  };
538
564
 
539
- // src/application/services/inference-runner.ts
540
- var StreamingInference = class {
541
- async *run(request, endpoint) {
542
- var _a;
543
- if ((_a = request.signal) == null ? void 0 : _a.aborted) {
544
- return;
565
+ // src/application/use-cases/join.ts
566
+ function createJoin(resolveRuntime) {
567
+ return function join(participant) {
568
+ resolveRuntime().join(participant);
569
+ };
570
+ }
571
+
572
+ // src/application/use-cases/leave.ts
573
+ function createLeave(resolveRuntime) {
574
+ return function leave(participant) {
575
+ resolveRuntime().leave(participant);
576
+ };
577
+ }
578
+
579
+ // src/application/use-cases/send-message.ts
580
+ function createSendMessage(resolveRuntime) {
581
+ return function sendMessage(message, senderId) {
582
+ const runtime = resolveRuntime();
583
+ const participant = runtime.getParticipant(senderId);
584
+ if (!participant) {
585
+ throw new Error(`Participant ${senderId} not found`);
586
+ }
587
+ const userMessage = MessageSentEvent.init(senderId, message);
588
+ runtime.publish(userMessage);
589
+ };
590
+ }
591
+
592
+ // src/domain/agentic-environment/loop/agent-loop.ts
593
+ var AgentLoop = class {
594
+ constructor(stateExecutor, transitionResolver, loopVisitor) {
595
+ this.stateExecutor = stateExecutor;
596
+ this.transitionResolver = transitionResolver;
597
+ this.loopVisitor = loopVisitor;
598
+ }
599
+ async run(message) {
600
+ let transition = {
601
+ nextStateId: "message_received",
602
+ input: message
603
+ };
604
+ while (transition.nextStateId !== "idle") {
605
+ const execution = await this.stateExecutor.execute(transition, this.loopVisitor);
606
+ transition = this.transitionResolver.resolve(execution);
545
607
  }
546
- yield* endpoint.stream(request, request.signal);
547
608
  }
548
609
  };
549
- var NonStreamingInference = class {
550
- async *run(request, endpoint) {
551
- var _a, _b;
552
- if ((_a = request.signal) == null ? void 0 : _a.aborted) {
553
- return;
554
- }
555
- const response = await endpoint.infer(request);
556
- for (const item of response.contextItems) {
557
- if ((_b = request.signal) == null ? void 0 : _b.aborted) {
558
- break;
559
- }
560
- yield item;
610
+
611
+ // src/domain/agentic-environment/loop/state-executor.ts
612
+ var LoopStateExecutor = class {
613
+ constructor(messageReceivedState, inferenceState, functionCallState, modelMessageState) {
614
+ this.messageReceivedState = messageReceivedState;
615
+ this.inferenceState = inferenceState;
616
+ this.functionCallState = functionCallState;
617
+ this.modelMessageState = modelMessageState;
618
+ }
619
+ async execute(transition, loopVisitor) {
620
+ switch (transition.nextStateId) {
621
+ case "message_received":
622
+ return await this.messageReceivedState.run(transition.input, loopVisitor);
623
+ case "inference":
624
+ return await this.inferenceState.run(transition.input, loopVisitor);
625
+ case "function_call":
626
+ return await this.functionCallState.run(transition.input, loopVisitor);
627
+ case "model_message":
628
+ return await this.modelMessageState.run(transition.input, loopVisitor);
561
629
  }
562
630
  }
563
631
  };
564
632
 
565
- // src/application/use-cases/run-inference.ts
566
- var RunInference = class {
567
- constructor(generativeModelRepository2, requestValidator) {
568
- this.generativeModelRepository = generativeModelRepository2;
569
- this.requestValidator = requestValidator;
633
+ // src/application/states/function-call.ts
634
+ var FunctionCallState = class {
635
+ constructor(functionCallRunner) {
636
+ this.functionCallRunner = functionCallRunner;
637
+ this.id = "function_call";
570
638
  }
571
- async execute(inferenceParams) {
639
+ async run(input, loopVisitor) {
572
640
  var _a;
573
- const { caller, environment, signal } = inferenceParams;
574
- const generativeModel = await this.generativeModelRepository.getByModelName(inferenceParams.model);
575
- this.requestValidator.validate(inferenceParams, generativeModel.specification);
576
- const resolvedParams = {
577
- ...inferenceParams,
578
- maxOutputTokens: (_a = inferenceParams.maxOutputTokens) != null ? _a : generativeModel.specification.maxOutputTokens
579
- };
580
- const inferenceRunner = inferenceParams.streaming ? new StreamingInference() : new NonStreamingInference();
581
- const result = inferenceRunner.run(resolvedParams, generativeModel.endpoint);
582
- for await (const item of result) {
583
- if (signal == null ? void 0 : signal.aborted) {
584
- break;
585
- }
586
- if (item instanceof ReasoningItem) {
587
- environment.deliverReasoning(caller, item);
588
- } else if (item instanceof FunctionCallItem) {
589
- environment.deliverFunctionCall(caller, item);
590
- } else if (item instanceof ModelMessageItem) {
591
- environment.deliverModelMessage(caller, item);
592
- } else if (item instanceof SemanticEvent) {
593
- environment.deliverSemanticEvent(caller, item);
594
- }
641
+ loopVisitor.visitFunctionCallStarted(input);
642
+ const { call, inferenceInput } = input;
643
+ let tool = (_a = inferenceInput.tools) == null ? void 0 : _a.find((tool2) => tool2.name === call.name);
644
+ if (!tool) {
645
+ return {
646
+ stateId: this.id,
647
+ input,
648
+ output: {
649
+ item: FunctionCallOutputItem.create(call.callId, `Error: unknown tool "${call.name}"`)
650
+ }
651
+ };
595
652
  }
653
+ const item = await this.functionCallRunner.run(call, tool);
654
+ loopVisitor.visitFunctionCallCompleted(item);
655
+ return {
656
+ stateId: this.id,
657
+ input,
658
+ output: {
659
+ item
660
+ }
661
+ };
596
662
  }
597
663
  };
598
664
 
599
- // src/application/use-cases/execute-function-call.ts
600
- var ExecuteFunctionCall = class {
601
- constructor(functionCallRunner) {
602
- this.functionCallRunner = functionCallRunner;
603
- }
604
- async execute(environment, functionCallItem, tool, caller, signal) {
605
- const stream = this.functionCallRunner.run(functionCallItem, tool, signal);
606
- for await (const item of stream) {
607
- environment.deliverFunctionCallOutput(caller, item);
608
- }
665
+ // src/application/states/message-received.ts
666
+ var MessageReceivedState = class {
667
+ constructor() {
668
+ this.id = "message_received";
669
+ }
670
+ async run(input, loopVisitor) {
671
+ loopVisitor.visitMessageReceivedStarted(input);
672
+ const userMessage = UserMessageItem.create(input.content);
673
+ const output = {
674
+ ...input.input,
675
+ context: input.input.context.addContextItems([userMessage])
676
+ };
677
+ loopVisitor.visitMessageReceivedCompleted(output);
678
+ return {
679
+ stateId: this.id,
680
+ input,
681
+ output
682
+ };
609
683
  }
610
684
  };
611
685
 
612
- // src/application/services/function-call-runner.ts
613
- var FunctionCallRunner = class {
614
- async *run(call, tool, signal) {
615
- if (signal == null ? void 0 : signal.aborted) {
616
- return;
617
- }
618
- if (!tool) throw new Error(`Unknown tool: ${call.name}`);
619
- const result = await tool.invoke(JSON.parse(call.args));
620
- yield FunctionCallOutputItem.create(call.callId, JSON.stringify(result));
686
+ // src/application/states/model-message.ts
687
+ var ModelMessageState = class {
688
+ constructor() {
689
+ this.id = "model_message";
690
+ }
691
+ async run(input, loopVisitor) {
692
+ loopVisitor.visitModelAnswer(input);
693
+ return {
694
+ stateId: this.id,
695
+ input,
696
+ output: void 0
697
+ };
621
698
  }
622
699
  };
623
700
 
624
- // src/application/participants/participant.ts
625
- var BaseParticipant = class extends Participant {
626
- onFunctionCall(item) {
701
+ // src/domain/agentic-environment/loop/transition-resolver.ts
702
+ var TransitionResolver = class {
703
+ constructor(rules) {
704
+ this.rules = rules;
627
705
  }
628
- onExternalFunctionCall(source, item) {
706
+ resolve(execution) {
707
+ const rule = this.rules.find((rule2) => rule2.specification.isSatisfiedBy(execution));
708
+ if (!rule) {
709
+ throw new Error(`No transition rule found after "${execution.stateId}"`);
710
+ }
711
+ return rule.createTransition(execution);
629
712
  }
630
- onFunctionCallOutput(item) {
713
+ };
714
+
715
+ // src/domain/agentic-environment/loop/transition-specification.ts
716
+ var MessageReceivedSpecification = class {
717
+ isSatisfiedBy(execution) {
718
+ return execution.stateId === "message_received";
631
719
  }
632
- onExternalFunctionCallOutput(source, item) {
720
+ };
721
+ var InferenceProducedFunctionCallSpecification = class {
722
+ isSatisfiedBy(execution) {
723
+ return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "function_call");
633
724
  }
634
- onReasoning(item) {
725
+ };
726
+ var InferenceProducedModelMessageSpecification = class {
727
+ isSatisfiedBy(execution) {
728
+ return execution.stateId === "inference" && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
635
729
  }
636
- onExternalReasoning(source, item) {
730
+ };
731
+ var FunctionCallCompletedSpecification = class {
732
+ isSatisfiedBy(execution) {
733
+ return execution.stateId === "function_call";
637
734
  }
638
- onModelMessage(item) {
735
+ };
736
+ var ModelMessageCompletedSpecification = class {
737
+ isSatisfiedBy(execution) {
738
+ return execution.stateId === "model_message";
639
739
  }
640
- onExternalModelMessage(source, item) {
740
+ };
741
+
742
+ // src/domain/agentic-environment/loop/transition-rule.ts
743
+ var MessageReceivedToInferenceRule = class {
744
+ constructor() {
745
+ this.specification = new MessageReceivedSpecification();
641
746
  }
642
- onMessage(message) {
747
+ createTransition(execution) {
748
+ if (execution.stateId !== "message_received") {
749
+ throw new Error("Expected message_received execution");
750
+ }
751
+ return {
752
+ nextStateId: "inference",
753
+ input: execution.output
754
+ };
643
755
  }
644
- onJoined() {
756
+ };
757
+ var InferenceToFunctionCallRule = class {
758
+ constructor() {
759
+ this.specification = new InferenceProducedFunctionCallSpecification();
645
760
  }
646
- onLeft() {
761
+ createTransition(execution) {
762
+ if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "function_call")) {
763
+ throw new Error("Expected inference function-call output");
764
+ }
765
+ const functionCallItem = execution.output.items.find((item) => item.type === "function_call");
766
+ if (!functionCallItem) {
767
+ throw new Error("Expected inference function-call output");
768
+ }
769
+ return {
770
+ nextStateId: "function_call",
771
+ input: {
772
+ call: functionCallItem,
773
+ inferenceInput: execution.input
774
+ }
775
+ };
647
776
  }
648
- onParticipantJoined(participant) {
777
+ };
778
+ var InferenceToModelMessageRule = class {
779
+ constructor() {
780
+ this.specification = new InferenceProducedModelMessageSpecification();
649
781
  }
650
- onParticipantLeft(participant) {
782
+ createTransition(execution) {
783
+ if (execution.stateId !== "inference" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
784
+ throw new Error("Expected inference model-message output");
785
+ }
786
+ const modelMessageItem = execution.output.items.find(
787
+ (item) => item.type === "message" && item.role === "assistant"
788
+ );
789
+ if (!modelMessageItem) {
790
+ throw new Error("Expected inference model-message output");
791
+ }
792
+ return {
793
+ nextStateId: "model_message",
794
+ input: {
795
+ answer: modelMessageItem
796
+ }
797
+ };
651
798
  }
652
- onInternalEvent(item) {
799
+ };
800
+ var FunctionCallToInferenceRule = class {
801
+ constructor() {
802
+ this.specification = new FunctionCallCompletedSpecification();
653
803
  }
654
- onExternalEvent(source, item) {
804
+ createTransition(execution) {
805
+ if (execution.stateId !== "function_call") {
806
+ throw new Error("Expected function_call execution");
807
+ }
808
+ const previousRequest = execution.input.inferenceInput;
809
+ previousRequest.context.addContextItems([execution.input.call, execution.output.item]);
810
+ return {
811
+ nextStateId: "inference",
812
+ input: {
813
+ ...previousRequest
814
+ }
815
+ };
655
816
  }
656
- onError(error) {
817
+ };
818
+ var ModelMessageToIdleRule = class {
819
+ constructor() {
820
+ this.specification = new ModelMessageCompletedSpecification();
657
821
  }
658
- onParticipantError(source, error) {
822
+ createTransition(execution) {
823
+ if (execution.stateId !== "model_message") {
824
+ throw new Error("Expected model_message execution");
825
+ }
826
+ return {
827
+ nextStateId: "idle",
828
+ input: void 0
829
+ };
659
830
  }
660
831
  };
661
832
 
662
- // src/application/use-cases/send-message.ts
663
- var SendMessage = class {
664
- async execute(environment, message, caller) {
665
- if (!caller.isJoinedTo(environment)) {
666
- throw new Error("Not joined to environment");
667
- }
668
- environment.deliverMessage(caller, message);
833
+ // src/domain/agentic-environment/loop/event-publisher-visitor.ts
834
+ var EventPublisherLoopVisitor = class {
835
+ constructor(agentId, runtime) {
836
+ this.agentId = agentId;
837
+ this.runtime = runtime;
838
+ }
839
+ visitMessageReceivedStarted(input) {
840
+ console.log("Visiting message received started: ", input);
841
+ this.publish("message_received.started", input);
842
+ }
843
+ visitMessageReceivedCompleted(output) {
844
+ console.log("Visiting message received completed: ", output);
845
+ this.publish("message_received.completed", output);
846
+ }
847
+ visitInferenceStarted(input) {
848
+ console.log("Visiting inference started: ", input);
849
+ this.publish("inference.started", input);
850
+ }
851
+ visitInferenceEvent(event) {
852
+ console.log("Visiting inference event: ", event);
853
+ this.publish("inference.stream", event);
854
+ }
855
+ visitInferenceCompleted(output) {
856
+ console.log("Visiting inference completed: ", output);
857
+ this.publish("inference.completed", output);
858
+ }
859
+ visitFunctionCallStarted(input) {
860
+ console.log("Visiting function call started: ", input);
861
+ this.publish("function_call.started", input);
862
+ }
863
+ visitFunctionCallCompleted(output) {
864
+ console.log("Visiting function call completed: ", output);
865
+ this.publish("function_call.completed", output);
866
+ }
867
+ visitModelAnswer(input) {
868
+ console.log("Visiting model answer: ", input.answer);
869
+ this.publish("model.answer", input);
870
+ }
871
+ publish(type, payload) {
872
+ this.runtime.publish({
873
+ type,
874
+ producerId: this.agentId,
875
+ occurredAt: /* @__PURE__ */ new Date(),
876
+ payload
877
+ });
669
878
  }
670
879
  };
671
880
 
881
+ // src/application/use-cases/run-loop.ts
882
+ function createRunLoop(resolveRuntime) {
883
+ return function runLoop(agentId, message, inferenceInput) {
884
+ const runtime = resolveRuntime();
885
+ const inferenceRunner = runtime.getInferenceRunner();
886
+ const functionCallRunner = runtime.getFunctionCallRunner();
887
+ const transitionResolver = new TransitionResolver([
888
+ // High-priority interception rules would go first.
889
+ new MessageReceivedToInferenceRule(),
890
+ new InferenceToFunctionCallRule(),
891
+ new InferenceToModelMessageRule(),
892
+ new FunctionCallToInferenceRule(),
893
+ new ModelMessageToIdleRule()
894
+ ]);
895
+ const stateExecutor = new LoopStateExecutor(
896
+ new MessageReceivedState(),
897
+ new InferenceState(inferenceRunner),
898
+ new FunctionCallState(functionCallRunner),
899
+ new ModelMessageState()
900
+ );
901
+ const agentLoop = new AgentLoop(
902
+ stateExecutor,
903
+ transitionResolver,
904
+ new EventPublisherLoopVisitor(agentId, runtime)
905
+ );
906
+ agentLoop.run({
907
+ content: message,
908
+ input: inferenceInput
909
+ });
910
+ };
911
+ }
912
+
672
913
  // src/infrastructure/providers/anthropic/endpoints/anthropic-messages-mapper.ts
673
914
  var AnthropicMessagesMapper = class {
674
- toRequest(inferenceParams) {
675
- const { messages, system } = this.mapContextItems(inferenceParams);
915
+ toRequest(inferenceInput) {
916
+ const { messages, system } = this.mapContextItems(inferenceInput);
676
917
  const outputConfig = {};
677
918
  const request = {
678
- model: inferenceParams.model,
919
+ model: inferenceInput.model,
679
920
  messages
680
921
  };
681
- request.max_tokens = inferenceParams.maxOutputTokens;
922
+ request.max_tokens = inferenceInput.maxOutputTokens;
682
923
  if (system) {
683
924
  request.system = system;
684
925
  }
685
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
686
- request.tools = inferenceParams.tools.map((tool) => ({
926
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
927
+ request.tools = inferenceInput.tools.map((tool) => ({
687
928
  name: tool.name,
688
929
  description: tool.description,
689
930
  input_schema: tool.parameters
690
931
  }));
691
932
  }
692
- if (inferenceParams.structuredOutput) {
933
+ if (inferenceInput.structuredOutput) {
693
934
  outputConfig.format = {
694
935
  type: "json_schema",
695
- json_schema: inferenceParams.structuredOutput.schema
936
+ json_schema: inferenceInput.structuredOutput.schema
696
937
  };
697
938
  }
698
- if (inferenceParams.reasoningEffort) {
939
+ if (inferenceInput.reasoningEffort) {
699
940
  request.thinking = { type: "adaptive" };
700
- outputConfig.effort = inferenceParams.reasoningEffort;
941
+ outputConfig.effort = inferenceInput.reasoningEffort;
701
942
  }
702
943
  if (Object.keys(outputConfig).length > 0) {
703
944
  request.output_config = outputConfig;
704
945
  }
705
- if (inferenceParams.streaming) {
706
- request.stream = inferenceParams.streaming;
946
+ if (inferenceInput.streaming) {
947
+ request.stream = inferenceInput.streaming;
707
948
  }
708
949
  return request;
709
950
  }
710
- toResponse(response) {
711
- const contextItems = this.extractContextItems(response);
712
- const tokenUsage = this.extractTokenUsage(response);
713
- return new InferenceResponse(contextItems, tokenUsage);
714
- }
715
951
  addContentBlock(messages, role, block) {
716
952
  const lastMessage = messages[messages.length - 1];
717
953
  if ((lastMessage == null ? void 0 : lastMessage.role) === role) {
@@ -723,9 +959,9 @@ var AnthropicMessagesMapper = class {
723
959
  content: [block]
724
960
  });
725
961
  }
726
- mapContextItems(inferenceParams) {
962
+ mapContextItems(inferenceInput) {
727
963
  var _a, _b, _c;
728
- const context = inferenceParams.context;
964
+ const context = inferenceInput.context;
729
965
  const messages = [];
730
966
  const system = [];
731
967
  for (const item of context.getItems()) {
@@ -792,7 +1028,7 @@ var AnthropicMessagesMapper = class {
792
1028
  new OutputTokenDetails(0)
793
1029
  );
794
1030
  }
795
- extractContextItems(response) {
1031
+ toResponse(response) {
796
1032
  var _a;
797
1033
  const items = [];
798
1034
  for (const block of response.content) {
@@ -820,30 +1056,27 @@ var AnthropicMessagesMapper = class {
820
1056
  );
821
1057
  }
822
1058
  }
823
- return items;
1059
+ return { items, rowResponse: response };
824
1060
  }
825
1061
  };
826
1062
 
827
1063
  // src/infrastructure/providers/anthropic/endpoints/anthropic-messages.ts
828
1064
  var import_sdk = __toESM(require("@anthropic-ai/sdk"));
829
1065
  var AnthropicMessages = class {
830
- constructor(endpointMapper = new AnthropicMessagesMapper()) {
1066
+ constructor(endpointMapper = new AnthropicMessagesMapper(), config = {}) {
831
1067
  this.endpointMapper = endpointMapper;
832
- this.client = new import_sdk.default();
1068
+ this.client = new import_sdk.default({ baseURL: config.baseURL, apiKey: config.apiKey });
833
1069
  }
834
- async infer(inferenceParams) {
835
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
836
- const response = await this.client.messages.create(inferenceRequest);
1070
+ async infer(inferenceInput) {
1071
+ const request = this.endpointMapper.toRequest(inferenceInput);
1072
+ const response = await this.client.messages.create(request);
837
1073
  return this.endpointMapper.toResponse(response);
838
1074
  }
839
- async *stream(inferenceParams, signal) {
840
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
841
- const stream = await this.client.messages.create(inferenceRequest);
1075
+ async *stream(inferenceInput) {
1076
+ const request = this.endpointMapper.toRequest(inferenceInput);
1077
+ const stream = await this.client.messages.create(request);
842
1078
  for await (const event of stream) {
843
- if (signal == null ? void 0 : signal.aborted) {
844
- break;
845
- }
846
- yield new SemanticEvent(event.type, event);
1079
+ yield event;
847
1080
  }
848
1081
  }
849
1082
  };
@@ -865,27 +1098,27 @@ var SummaryText = class _SummaryText extends ItemContent {
865
1098
 
866
1099
  // src/infrastructure/providers/openai/endpoints/openai-responses-mapper.ts
867
1100
  var OpenAIResponsesMapper = class {
868
- toRequest(inferenceParams) {
1101
+ toRequest(inferenceInput) {
869
1102
  var _a, _b;
870
1103
  const request = {
871
- model: inferenceParams.model,
872
- input: this.mapContextItems(inferenceParams)
1104
+ model: inferenceInput.model,
1105
+ input: this.mapContextItems(inferenceInput)
873
1106
  };
874
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
875
- request.tools = inferenceParams.tools.map((tool) => ({
1107
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1108
+ request.tools = inferenceInput.tools.map((tool) => ({
876
1109
  type: tool.type,
877
1110
  name: tool.name,
878
1111
  description: tool.description,
879
1112
  parameters: tool.parameters
880
1113
  }));
881
1114
  }
882
- if (inferenceParams.reasoningEffort) {
1115
+ if (inferenceInput.reasoningEffort) {
883
1116
  request.reasoning = {
884
- effort: inferenceParams.reasoningEffort
1117
+ effort: inferenceInput.reasoningEffort
885
1118
  };
886
1119
  }
887
- if (inferenceParams.structuredOutput) {
888
- const format = inferenceParams.structuredOutput;
1120
+ if (inferenceInput.structuredOutput) {
1121
+ const format = inferenceInput.structuredOutput;
889
1122
  request.text = {
890
1123
  format: {
891
1124
  type: "json_schema",
@@ -895,19 +1128,14 @@ var OpenAIResponsesMapper = class {
895
1128
  }
896
1129
  };
897
1130
  }
898
- if (inferenceParams.streaming) {
899
- request.stream = inferenceParams.streaming;
1131
+ if (inferenceInput.streaming) {
1132
+ request.stream = inferenceInput.streaming;
900
1133
  }
901
1134
  return request;
902
1135
  }
903
- toResponse(response) {
904
- const contextItems = this.extractContextItems(response);
905
- const tokenUsage = this.extractTokenUsage(response);
906
- return new InferenceResponse(contextItems, tokenUsage);
907
- }
908
- mapContextItems(inferenceParams) {
1136
+ mapContextItems(inferenceInput) {
909
1137
  const input = [];
910
- for (const item of inferenceParams.context.getItems()) {
1138
+ for (const item of inferenceInput.context.getItems()) {
911
1139
  if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem || item instanceof UserMessageItem) {
912
1140
  input.push({
913
1141
  type: item.type,
@@ -967,7 +1195,7 @@ var OpenAIResponsesMapper = class {
967
1195
  new OutputTokenDetails((_d = (_c = response.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
968
1196
  );
969
1197
  }
970
- extractContextItems(response) {
1198
+ toResponse(response) {
971
1199
  var _a, _b, _c;
972
1200
  const items = [];
973
1201
  for (const item of (_a = response.output) != null ? _a : []) {
@@ -1000,7 +1228,10 @@ var OpenAIResponsesMapper = class {
1000
1228
  );
1001
1229
  }
1002
1230
  }
1003
- return items;
1231
+ return {
1232
+ items,
1233
+ rowResponse: response
1234
+ };
1004
1235
  }
1005
1236
  };
1006
1237
 
@@ -1010,19 +1241,16 @@ var OpenAIResponses = class {
1010
1241
  this.endpointMapper = endpointMapper;
1011
1242
  this.client = new import_openai.default();
1012
1243
  }
1013
- async infer(inferenceParams) {
1014
- const request = this.endpointMapper.toRequest(inferenceParams);
1244
+ async infer(inferenceInput) {
1245
+ const request = this.endpointMapper.toRequest(inferenceInput);
1015
1246
  const response = await this.client.responses.create(request);
1016
1247
  return this.endpointMapper.toResponse(response);
1017
1248
  }
1018
- async *stream(inferenceParams, signal) {
1019
- const request = this.endpointMapper.toRequest(inferenceParams);
1249
+ async *stream(inferenceInput) {
1250
+ const request = this.endpointMapper.toRequest(inferenceInput);
1020
1251
  const response = await this.client.responses.create(request);
1021
1252
  for await (const event of response) {
1022
- if (signal == null ? void 0 : signal.aborted) {
1023
- break;
1024
- }
1025
- yield new SemanticEvent(event.type, event);
1253
+ yield event;
1026
1254
  }
1027
1255
  }
1028
1256
  };
@@ -1032,16 +1260,16 @@ var import_genai = require("@google/genai");
1032
1260
 
1033
1261
  // src/infrastructure/providers/gemini/endpoints/gemini-generate-content-mapper.ts
1034
1262
  var GeminiGenerateContentMapper = class {
1035
- toRequest(inferenceParams) {
1036
- const { contents, systemInstruction } = this.mapContextItems(inferenceParams);
1263
+ toRequest(inferenceInput) {
1264
+ const { contents, systemInstruction } = this.mapContextItems(inferenceInput);
1037
1265
  const config = {};
1038
1266
  if (systemInstruction) {
1039
1267
  config.systemInstruction = systemInstruction;
1040
1268
  }
1041
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
1269
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1042
1270
  config.tools = [
1043
1271
  {
1044
- functionDeclarations: inferenceParams.tools.map((tool) => ({
1272
+ functionDeclarations: inferenceInput.tools.map((tool) => ({
1045
1273
  name: tool.name,
1046
1274
  description: tool.description,
1047
1275
  parametersJsonSchema: tool.parameters
@@ -1049,34 +1277,29 @@ var GeminiGenerateContentMapper = class {
1049
1277
  }
1050
1278
  ];
1051
1279
  }
1052
- if (inferenceParams.structuredOutput) {
1280
+ if (inferenceInput.structuredOutput) {
1053
1281
  config.responseMimeType = "application/json";
1054
- config.responseSchema = inferenceParams.structuredOutput.schema;
1282
+ config.responseSchema = inferenceInput.structuredOutput.schema;
1055
1283
  }
1056
- if (inferenceParams.reasoningEffort) {
1284
+ if (inferenceInput.reasoningEffort) {
1057
1285
  config.thinkingConfig = {
1058
- thinkingLevel: inferenceParams.reasoningEffort,
1286
+ thinkingLevel: inferenceInput.reasoningEffort,
1059
1287
  includeThoughts: true
1060
1288
  };
1061
1289
  }
1062
1290
  const request = {
1063
- model: inferenceParams.model,
1291
+ model: inferenceInput.model,
1064
1292
  contents,
1065
1293
  config
1066
1294
  };
1067
- if (inferenceParams.streaming) {
1068
- request.stream = inferenceParams.streaming;
1295
+ if (inferenceInput.streaming) {
1296
+ request.stream = inferenceInput.streaming;
1069
1297
  }
1070
1298
  return request;
1071
1299
  }
1072
- toResponse(response) {
1073
- const contextItems = this.extractContextItems(response);
1074
- const tokenUsage = this.extractTokenUsage(response);
1075
- return new InferenceResponse(contextItems, tokenUsage);
1076
- }
1077
- mapContextItems(inferenceParams) {
1300
+ mapContextItems(inferenceInput) {
1078
1301
  var _a;
1079
- const context = inferenceParams.context;
1302
+ const context = inferenceInput.context;
1080
1303
  const contents = [];
1081
1304
  const system = [];
1082
1305
  const callNames = /* @__PURE__ */ new Map();
@@ -1149,7 +1372,7 @@ var GeminiGenerateContentMapper = class {
1149
1372
  new OutputTokenDetails((_e = usage.thoughtsTokenCount) != null ? _e : 0)
1150
1373
  );
1151
1374
  }
1152
- extractContextItems(response) {
1375
+ toResponse(response) {
1153
1376
  var _a, _b, _c, _d, _e, _f;
1154
1377
  const items = [];
1155
1378
  const parts = (_d = (_c = (_b = (_a = response.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts) != null ? _d : [];
@@ -1178,42 +1401,43 @@ var GeminiGenerateContentMapper = class {
1178
1401
  );
1179
1402
  }
1180
1403
  }
1181
- return items;
1404
+ return { items, rowResponse: response };
1182
1405
  }
1183
1406
  };
1184
1407
 
1185
1408
  // src/infrastructure/providers/gemini/endpoints/gemini-generate-content.ts
1186
1409
  var GeminiGenerateContent = class {
1187
- constructor(endpointMapper = new GeminiGenerateContentMapper()) {
1410
+ constructor(endpointMapper = new GeminiGenerateContentMapper(), config = {}) {
1411
+ var _a;
1188
1412
  this.endpointMapper = endpointMapper;
1189
- this.client = new import_genai.GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
1413
+ this.client = new import_genai.GoogleGenAI({
1414
+ apiKey: (_a = config.apiKey) != null ? _a : process.env.GEMINI_API_KEY,
1415
+ ...config.baseURL ? { httpOptions: { baseUrl: config.baseURL } } : {}
1416
+ });
1190
1417
  }
1191
- async infer(inferenceParams) {
1192
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
1193
- const response = await this.client.models.generateContent(inferenceRequest);
1418
+ async infer(inferenceInput) {
1419
+ const request = this.endpointMapper.toRequest(inferenceInput);
1420
+ const response = await this.client.models.generateContent(request);
1194
1421
  return this.endpointMapper.toResponse(response);
1195
1422
  }
1196
- async *stream(inferenceParams, signal) {
1197
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
1198
- const stream = await this.client.models.generateContentStream(inferenceRequest);
1423
+ async *stream(inferenceInput) {
1424
+ const request = this.endpointMapper.toRequest(inferenceInput);
1425
+ const stream = await this.client.models.generateContentStream(request);
1199
1426
  for await (const chunk of stream) {
1200
- if (signal == null ? void 0 : signal.aborted) {
1201
- break;
1202
- }
1203
- yield new SemanticEvent("generate_content_chunk", chunk);
1427
+ yield chunk;
1204
1428
  }
1205
1429
  }
1206
1430
  };
1207
1431
 
1208
1432
  // src/infrastructure/providers/openai/endpoints/openai-chat-completions-mapper.ts
1209
1433
  var OpenAIChatCompletionsMapper = class {
1210
- toRequest(inferenceParams) {
1434
+ toRequest(inferenceInput) {
1211
1435
  const request = {
1212
- model: inferenceParams.model,
1213
- messages: this.mapContextItems(inferenceParams)
1436
+ model: inferenceInput.model,
1437
+ messages: this.mapContextItems(inferenceInput)
1214
1438
  };
1215
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
1216
- request.tools = inferenceParams.tools.map((tool) => ({
1439
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1440
+ request.tools = inferenceInput.tools.map((tool) => ({
1217
1441
  type: "function",
1218
1442
  function: {
1219
1443
  name: tool.name,
@@ -1222,23 +1446,18 @@ var OpenAIChatCompletionsMapper = class {
1222
1446
  }
1223
1447
  }));
1224
1448
  }
1225
- if (inferenceParams.reasoningEffort) {
1226
- request.reasoning_effort = inferenceParams.reasoningEffort;
1449
+ if (inferenceInput.reasoningEffort) {
1450
+ request.reasoning_effort = inferenceInput.reasoningEffort;
1227
1451
  }
1228
- if (inferenceParams.streaming) {
1229
- request.stream = inferenceParams.streaming;
1452
+ if (inferenceInput.streaming) {
1453
+ request.stream = inferenceInput.streaming;
1230
1454
  }
1231
1455
  return request;
1232
1456
  }
1233
- toResponse(response) {
1234
- const contextItems = this.extractContextItems(response);
1235
- const tokenUsage = this.extractTokenUsage(response);
1236
- return new InferenceResponse(contextItems, tokenUsage);
1237
- }
1238
- mapContextItems(inferenceParams) {
1457
+ mapContextItems(inferenceInput) {
1239
1458
  var _a;
1240
1459
  const messages = [];
1241
- for (const item of inferenceParams.context.getItems()) {
1460
+ for (const item of inferenceInput.context.getItems()) {
1242
1461
  if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem) {
1243
1462
  messages.push({ role: "system", content: item.content.text });
1244
1463
  continue;
@@ -1285,12 +1504,12 @@ var OpenAIChatCompletionsMapper = class {
1285
1504
  new OutputTokenDetails((_d = (_c = response.usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
1286
1505
  );
1287
1506
  }
1288
- extractContextItems(response) {
1507
+ toResponse(response) {
1289
1508
  var _a, _b, _c;
1290
1509
  const items = [];
1291
1510
  const message = (_b = (_a = response.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1292
1511
  if (!message) {
1293
- return items;
1512
+ return { items: [], rowResponse: response };
1294
1513
  }
1295
1514
  if (message.reasoning_content) {
1296
1515
  items.push(
@@ -1313,7 +1532,7 @@ var OpenAIChatCompletionsMapper = class {
1313
1532
  })
1314
1533
  );
1315
1534
  }
1316
- return items;
1535
+ return { items, rowResponse: response };
1317
1536
  }
1318
1537
  };
1319
1538
 
@@ -1329,24 +1548,20 @@ var OpenAIChatCompletions = class {
1329
1548
  });
1330
1549
  this.extraBody = (_a = config.extraBody) != null ? _a : {};
1331
1550
  }
1332
- buildRequest(inferenceParams) {
1551
+ buildRequest(inferenceInput) {
1333
1552
  return {
1334
1553
  ...this.extraBody,
1335
- ...this.endpointMapper.toRequest(inferenceParams)
1554
+ ...this.endpointMapper.toRequest(inferenceInput)
1336
1555
  };
1337
1556
  }
1338
- async infer(inferenceParams) {
1339
- const response = await this.client.chat.completions.create(this.buildRequest(inferenceParams));
1557
+ async infer(inferenceInput) {
1558
+ const response = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
1340
1559
  return this.endpointMapper.toResponse(response);
1341
1560
  }
1342
- async *stream(inferenceParams, signal) {
1343
- var _a;
1344
- const stream = await this.client.chat.completions.create(this.buildRequest(inferenceParams));
1561
+ async *stream(inferenceInput) {
1562
+ const stream = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
1345
1563
  for await (const event of stream) {
1346
- if (signal == null ? void 0 : signal.aborted) {
1347
- break;
1348
- }
1349
- yield new SemanticEvent((_a = event.object) != null ? _a : "chat.completion.chunk", event);
1564
+ yield event;
1350
1565
  }
1351
1566
  }
1352
1567
  };
@@ -1615,103 +1830,71 @@ var claudeHaiku45Specification = {
1615
1830
  supportsStructuredOutput: true
1616
1831
  };
1617
1832
 
1618
- // src/infrastructure/repository/generative-model-repository.ts
1619
- var InMemoryGenerativeModelRepository = class {
1620
- getByModelName(modelName) {
1621
- let generativeModel = void 0;
1622
- switch (modelName) {
1623
- case "gpt-5.4":
1624
- generativeModel = {
1625
- endpoint: new OpenAIResponses(),
1626
- specification: gpt54Specification
1627
- };
1628
- break;
1629
- case "gpt-5.4-mini":
1630
- generativeModel = {
1631
- endpoint: new OpenAIResponses(),
1632
- specification: gpt54MiniSpecification
1633
- };
1634
- break;
1635
- case "gpt-5.4-nano":
1636
- generativeModel = {
1637
- endpoint: new OpenAIResponses(),
1638
- specification: gpt54NanoSpecification
1639
- };
1640
- break;
1641
- case "gpt-5.5":
1642
- generativeModel = {
1643
- endpoint: new OpenAIResponses(),
1644
- specification: gpt55Specification
1645
- };
1646
- break;
1647
- case "claude-haiku-4-5":
1648
- generativeModel = {
1649
- endpoint: new AnthropicMessages(),
1650
- specification: claudeHaiku45Specification
1651
- };
1652
- break;
1653
- case "claude-sonnet-4-6":
1654
- generativeModel = {
1655
- endpoint: new AnthropicMessages(),
1656
- specification: claudeSonnet46Specification
1657
- };
1658
- break;
1659
- case "claude-opus-4-7":
1660
- generativeModel = {
1661
- endpoint: new AnthropicMessages(),
1662
- specification: claudeOpus47Specification
1663
- };
1664
- break;
1665
- case "claude-opus-4-8":
1666
- generativeModel = {
1667
- endpoint: new AnthropicMessages(),
1668
- specification: claudeOpus48Specification
1669
- };
1670
- break;
1671
- case "gemini-3.5-flash":
1672
- generativeModel = {
1673
- endpoint: new GeminiGenerateContent(),
1674
- specification: gemini35FlashSpecification
1675
- };
1676
- break;
1677
- case "gemini-3.1-pro-preview":
1678
- generativeModel = {
1679
- endpoint: new GeminiGenerateContent(),
1680
- specification: gemini31ProSpecification
1681
- };
1682
- break;
1683
- case "deepseek-v4-flash":
1684
- generativeModel = {
1685
- endpoint: new OpenAIChatCompletions(),
1686
- specification: deepSeekV4FlashSpecification
1687
- };
1688
- break;
1689
- case "deepseek-v4-pro":
1690
- generativeModel = {
1691
- endpoint: new OpenAIChatCompletions(),
1692
- specification: deepSeekV4ProSpecification
1693
- };
1694
- }
1695
- if (!generativeModel) {
1696
- throw new Error(`Unsupported model: ${modelName}`);
1697
- }
1698
- return Promise.resolve(generativeModel);
1833
+ // src/application/services/models.ts
1834
+ var supportedModels = [
1835
+ {
1836
+ endpoint: new OpenAIResponses(),
1837
+ specification: gpt54Specification
1838
+ },
1839
+ {
1840
+ endpoint: new OpenAIResponses(),
1841
+ specification: gpt54MiniSpecification
1842
+ },
1843
+ {
1844
+ endpoint: new OpenAIResponses(),
1845
+ specification: gpt54NanoSpecification
1846
+ },
1847
+ {
1848
+ endpoint: new OpenAIResponses(),
1849
+ specification: gpt55Specification
1850
+ },
1851
+ {
1852
+ endpoint: new AnthropicMessages(),
1853
+ specification: claudeHaiku45Specification
1854
+ },
1855
+ {
1856
+ endpoint: new AnthropicMessages(),
1857
+ specification: claudeSonnet46Specification
1858
+ },
1859
+ {
1860
+ endpoint: new AnthropicMessages(),
1861
+ specification: claudeOpus47Specification
1862
+ },
1863
+ {
1864
+ endpoint: new AnthropicMessages(),
1865
+ specification: claudeOpus48Specification
1866
+ },
1867
+ {
1868
+ endpoint: new GeminiGenerateContent(),
1869
+ specification: gemini35FlashSpecification
1870
+ },
1871
+ {
1872
+ endpoint: new GeminiGenerateContent(),
1873
+ specification: gemini31ProSpecification
1874
+ },
1875
+ {
1876
+ endpoint: new OpenAIChatCompletions(),
1877
+ specification: deepSeekV4FlashSpecification
1878
+ },
1879
+ {
1880
+ endpoint: new OpenAIChatCompletions(),
1881
+ specification: deepSeekV4ProSpecification
1699
1882
  }
1700
- };
1883
+ ];
1701
1884
 
1702
1885
  // src/domain/generative-model/request-validation/reasoning-effort.ts
1703
1886
  var ReasoningEffortValidation = class {
1704
1887
  constructor() {
1705
1888
  this.name = "reasoning-effort";
1706
1889
  }
1707
- isValid(inferenceParams, model) {
1708
- if (inferenceParams.reasoningEffort === void 0) {
1890
+ isValid(inferenceInput, model) {
1891
+ if (inferenceInput.reasoningEffort === void 0) {
1709
1892
  return true;
1710
1893
  }
1711
1894
  if (!model.supportsReasoningEffort) {
1712
1895
  return false;
1713
1896
  }
1714
- return model.supportedReasoningEfforts.includes(inferenceParams.reasoningEffort);
1897
+ return model.supportedReasoningEfforts.includes(inferenceInput.reasoningEffort);
1715
1898
  }
1716
1899
  };
1717
1900
 
@@ -1720,8 +1903,8 @@ var ToolCallingValidation = class {
1720
1903
  constructor() {
1721
1904
  this.name = "tool-calling";
1722
1905
  }
1723
- isValid(inferenceParams, model) {
1724
- if (inferenceParams.tools === void 0) {
1906
+ isValid(inferenceInput, model) {
1907
+ if (inferenceInput.tools === void 0) {
1725
1908
  return true;
1726
1909
  }
1727
1910
  return model.supportsFunctionCalling;
@@ -1733,8 +1916,8 @@ var StreamingValidation = class {
1733
1916
  constructor() {
1734
1917
  this.name = "streaming";
1735
1918
  }
1736
- isValid(inferenceParams, model) {
1737
- if (!inferenceParams.streaming) {
1919
+ isValid(inferenceInput, model) {
1920
+ if (!inferenceInput.streaming) {
1738
1921
  return true;
1739
1922
  }
1740
1923
  return model.supportsStreaming;
@@ -1761,8 +1944,8 @@ var ContextValidation = class {
1761
1944
  constructor() {
1762
1945
  this.name = "context";
1763
1946
  }
1764
- isValid(inferenceParams, model) {
1765
- return inferenceParams.context.items.every(
1947
+ isValid(inferenceInput, model) {
1948
+ return inferenceInput.context.items.every(
1766
1949
  (item) => model.supportedContextItemTypes.includes(getContextItemValidationKey(item))
1767
1950
  );
1768
1951
  }
@@ -1776,57 +1959,123 @@ var defaultRequestValidationRules = [
1776
1959
  new StructuredOutputValidation(),
1777
1960
  new ContextValidation()
1778
1961
  ];
1779
- var InferenceRequestValidator = class {
1962
+ var InferenceInputValidator = class {
1780
1963
  constructor(rules = defaultRequestValidationRules) {
1781
1964
  this.rules = rules;
1782
1965
  }
1783
- validate(inferenceParams, model) {
1966
+ validate(inferenceInput, model) {
1784
1967
  for (const rule of this.rules) {
1785
- if (!rule.isValid(inferenceParams, model)) {
1968
+ if (!rule.isValid(inferenceInput, model)) {
1786
1969
  throw new Error(`Request validation "${rule.name}" failed for model "${model.name}"`);
1787
1970
  }
1788
1971
  }
1789
1972
  }
1790
1973
  };
1791
1974
 
1792
- // src/index.ts
1793
- var generativeModelRepository = new InMemoryGenerativeModelRepository();
1794
- var inferenceRequestValidator = new InferenceRequestValidator();
1795
- var runInferenceUseCase = new RunInference(generativeModelRepository, inferenceRequestValidator);
1796
- var executeFunctionCallUseCase = new ExecuteFunctionCall(new FunctionCallRunner());
1797
- var sendMessageUseCase = new SendMessage();
1798
- var runInference = (inferenceParams) => {
1799
- runInferenceUseCase.execute(inferenceParams);
1800
- };
1801
- var executeFunctionCall = (environment, functionCallItem, tool, caller) => {
1802
- executeFunctionCallUseCase.execute(environment, functionCallItem, tool, caller);
1975
+ // src/application/services/inference-runner.ts
1976
+ var DefaultInferenceRunner = class {
1977
+ constructor(supportedModels2, requestValidator) {
1978
+ this.supportedModels = supportedModels2;
1979
+ this.requestValidator = requestValidator;
1980
+ }
1981
+ async run(input) {
1982
+ const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
1983
+ if (!generativeModel) {
1984
+ throw new Error(`Unsupported model: ${input.model}`);
1985
+ }
1986
+ this.requestValidator.validate(input, generativeModel.specification);
1987
+ return await generativeModel.endpoint.infer(input);
1988
+ }
1989
+ async streamInference(input) {
1990
+ const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
1991
+ if (!generativeModel) {
1992
+ throw new Error(`Unsupported model: ${input.model}`);
1993
+ }
1994
+ this.requestValidator.validate(input, generativeModel.specification);
1995
+ return await generativeModel.endpoint.stream(input);
1996
+ }
1803
1997
  };
1804
- var sendMessage = (environment, message, caller) => {
1805
- sendMessageUseCase.execute(environment, message, caller);
1998
+
1999
+ // src/application/services/function-call.ts
2000
+ var DefaultFunctionCallRunner = class {
2001
+ async run(input, tool) {
2002
+ try {
2003
+ const result = await tool.invoke(JSON.parse(input.args));
2004
+ return FunctionCallOutputItem.create(input.callId, JSON.stringify(result));
2005
+ } catch (error) {
2006
+ return FunctionCallOutputItem.create(input.callId, `Error calling tool: ${error}`);
2007
+ }
2008
+ }
1806
2009
  };
2010
+
2011
+ // src/define-runtime.ts
2012
+ function defineRuntime() {
2013
+ let runtime = null;
2014
+ const processor = new EventProcessor();
2015
+ function initializeRuntime(config) {
2016
+ var _a, _b, _c, _d;
2017
+ if (runtime) {
2018
+ throw new Error("Runtime already initialized");
2019
+ }
2020
+ const inferenceRunner = (_d = (_a = config.inferenceRunnerConfig) == null ? void 0 : _a.runner) != null ? _d : new DefaultInferenceRunner(
2021
+ (_c = (_b = config.inferenceRunnerConfig) == null ? void 0 : _b.supportedModels) != null ? _c : supportedModels,
2022
+ new InferenceInputValidator()
2023
+ );
2024
+ const functionCallRunner = new DefaultFunctionCallRunner();
2025
+ runtime = new RuntimeService(config.state, processor, inferenceRunner, functionCallRunner);
2026
+ return runtime;
2027
+ }
2028
+ function resolveRuntime() {
2029
+ if (!runtime) {
2030
+ throw new Error("Runtime not initialized");
2031
+ }
2032
+ return runtime;
2033
+ }
2034
+ function resolveParticipant(id) {
2035
+ if (!runtime) {
2036
+ throw new Error("Runtime not initialized");
2037
+ }
2038
+ const participant = runtime.state.getParticipant(id);
2039
+ if (!participant) {
2040
+ throw new Error(`Participant ${id} not found`);
2041
+ }
2042
+ return participant;
2043
+ }
2044
+ const join = createJoin(resolveRuntime);
2045
+ const leave = createLeave(resolveRuntime);
2046
+ const sendMessage = createSendMessage(resolveRuntime);
2047
+ const runLoop = createRunLoop(resolveRuntime);
2048
+ return {
2049
+ initializeRuntime,
2050
+ resolveRuntime,
2051
+ resolveParticipant,
2052
+ join,
2053
+ leave,
2054
+ sendMessage,
2055
+ runLoop
2056
+ };
2057
+ }
1807
2058
  // Annotate the CommonJS export names for ESM import in node:
1808
2059
  0 && (module.exports = {
1809
- AgenticEnvironment,
1810
- AgenticError,
1811
- BaseParticipant,
1812
2060
  ContextItem,
1813
2061
  DeveloperMessageItem,
1814
2062
  FunctionCallItem,
1815
2063
  FunctionCallOutputItem,
1816
- InMemoryModelContextRepository,
1817
- InferenceResponse,
1818
2064
  InputTokenDetails,
2065
+ McpClient,
2066
+ McpToolRegistry,
1819
2067
  ModelContext,
1820
2068
  ModelMessageItem,
1821
2069
  OutputTokenDetails,
1822
2070
  Participant,
1823
2071
  ReasoningItem,
2072
+ RuntimeState,
1824
2073
  SemanticEvent,
1825
2074
  SystemMessageItem,
1826
2075
  TokenUsage,
1827
2076
  UserMessageItem,
1828
- executeFunctionCall,
1829
- runInference,
1830
- sendMessage
2077
+ createAgent,
2078
+ createHuman,
2079
+ defineRuntime
1831
2080
  });
1832
2081
  //# sourceMappingURL=index.js.map