@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.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;
@@ -204,46 +191,96 @@ var TokenUsage = class {
204
191
  }
205
192
  };
206
193
 
207
- // src/infrastructure/repository/in-memory-model-context-repository.ts
208
- var InMemoryModelContextRepository = class {
209
- constructor() {
210
- this.store = /* @__PURE__ */ new Map();
211
- this.projectIndex = /* @__PURE__ */ new Map();
212
- }
213
- async save(context) {
214
- const existing = this.store.get(context.id);
215
- if (existing && existing.projectId !== context.projectId) {
216
- const prevSet = this.projectIndex.get(existing.projectId);
217
- prevSet == null ? void 0 : prevSet.delete(existing.id);
218
- if (prevSet && prevSet.size === 0) this.projectIndex.delete(existing.projectId);
219
- }
220
- this.store.set(context.id, this.clone(context));
221
- let set = this.projectIndex.get(context.projectId);
222
- if (!set) {
223
- set = /* @__PURE__ */ new Set();
224
- this.projectIndex.set(context.projectId, set);
225
- }
226
- set.add(context.id);
227
- }
228
- async get(id) {
229
- const context = this.store.get(id);
230
- if (!context) {
231
- throw new Error(`Context not found: ${id}`);
232
- }
233
- return this.clone(context);
234
- }
235
- async getByProjectId(projectId) {
236
- const ids = this.projectIndex.get(projectId);
237
- if (!ids || ids.size === 0) return [];
238
- return [...ids].map((id) => this.store.get(id)).filter((c) => Boolean(c)).map((c) => this.clone(c));
239
- }
240
- clone(context) {
241
- return ModelContext.rehydrate({
242
- id: context.id,
243
- projectId: context.projectId,
244
- items: [...context.getItems()]
194
+ // src/infrastructure/mcp/mcp-client.ts
195
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
196
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
197
+ var McpClient = class {
198
+ constructor(config) {
199
+ this.config = config;
200
+ this.connected = false;
201
+ var _a, _b;
202
+ const headers = { ...(_a = config.headers) != null ? _a : {} };
203
+ if (config.authToken) headers.Authorization = `Bearer ${config.authToken}`;
204
+ this.transport = new StreamableHTTPClientTransport(
205
+ new URL(config.url),
206
+ Object.keys(headers).length > 0 ? { requestInit: { headers } } : void 0
207
+ );
208
+ this.client = new Client({ name: (_b = config.name) != null ? _b : "mozaik", version: "1.0.0" }, { capabilities: {} });
209
+ }
210
+ async connect() {
211
+ if (this.connected) return;
212
+ await this.client.connect(this.transport);
213
+ this.connected = true;
214
+ }
215
+ /** The tools this server exposes. */
216
+ async listTools() {
217
+ var _a;
218
+ await this.connect();
219
+ const res = await this.client.listTools();
220
+ return ((_a = res.tools) != null ? _a : []).map((t) => {
221
+ var _a2, _b;
222
+ return {
223
+ name: t.name,
224
+ description: (_a2 = t.description) != null ? _a2 : "",
225
+ inputSchema: (_b = t.inputSchema) != null ? _b : { type: "object", properties: {} }
226
+ };
245
227
  });
246
228
  }
229
+ /** Call a tool by name; returns its text output. Throws if the server flags an error. */
230
+ async callTool(name, args) {
231
+ await this.connect();
232
+ const res = await this.client.callTool({ name, arguments: args != null ? args : {} });
233
+ const text = extractText(res);
234
+ if (res.isError) {
235
+ throw new Error(`MCP tool "${name}" failed: ${text || "unknown error"}`);
236
+ }
237
+ return text;
238
+ }
239
+ async close() {
240
+ if (!this.connected) return;
241
+ try {
242
+ await this.client.close();
243
+ } catch (e) {
244
+ }
245
+ this.connected = false;
246
+ }
247
+ };
248
+ function extractText(res) {
249
+ const content = res.content;
250
+ if (!Array.isArray(content)) return "";
251
+ return content.filter((c) => (c == null ? void 0 : c.type) === "text" && typeof c.text === "string").map((c) => c.text).join("\n");
252
+ }
253
+
254
+ // src/infrastructure/mcp/mcp-tool-registry.ts
255
+ var McpToolRegistry = class {
256
+ constructor(servers, createClient = (config) => new McpClient(config)) {
257
+ this.servers = servers;
258
+ this.createClient = createClient;
259
+ this.clients = [];
260
+ }
261
+ /** Discover every tool across the configured servers, as FunctionTools ready to pass to a model. */
262
+ async discoverTools() {
263
+ const tools = [];
264
+ for (const server of this.servers) {
265
+ const client = this.createClient(server);
266
+ this.clients.push(client);
267
+ for (const spec of await client.listTools()) {
268
+ tools.push({
269
+ type: "function",
270
+ name: spec.name,
271
+ description: spec.description,
272
+ parameters: spec.inputSchema,
273
+ strict: false,
274
+ invoke: (args) => client.callTool(spec.name, args != null ? args : {})
275
+ });
276
+ }
277
+ }
278
+ return tools;
279
+ }
280
+ /** Close all underlying MCP connections. */
281
+ async close() {
282
+ await Promise.all(this.clients.map((c) => c.close()));
283
+ }
247
284
  };
248
285
 
249
286
  // src/domain/model-context/context-item/client-item/system-message.ts
@@ -264,397 +301,598 @@ var SystemMessageItem = class _SystemMessageItem extends ContextItem {
264
301
  }
265
302
  };
266
303
 
267
- // src/domain/agentic-environment/participant.ts
268
- var Participant = class {
269
- constructor() {
270
- this.environments = [];
271
- this.listens = [];
272
- this.active = /* @__PURE__ */ new Map();
273
- }
274
- join(environment) {
275
- if (this.isJoinedTo(environment)) {
276
- return;
277
- }
278
- environment.subscribe(this);
279
- this.environments.push(environment);
280
- this.active.set(environment, true);
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;
281
311
  }
282
- leave(environment) {
283
- if (!this.isJoinedTo(environment)) {
284
- return;
285
- }
286
- environment.unsubscribe(this);
287
- this.environments = this.environments.filter((e) => e !== environment);
288
- this.active.delete(environment);
312
+ static create(type, producerId, payload) {
313
+ const occurredAt = /* @__PURE__ */ new Date();
314
+ return new _SemanticEvent(type, producerId, occurredAt, payload);
289
315
  }
290
- isJoinedTo(environment) {
291
- 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 });
292
320
  }
293
- getEnvironments() {
294
- 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
+ };
295
338
  }
296
- getListeners() {
297
- 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;
298
346
  }
299
- getEnvironmentState(environment) {
300
- return this.active.get(environment);
347
+ getManifest() {
348
+ return this.manifest;
301
349
  }
302
- markInactive(environment) {
303
- if (this.active.has(environment)) {
304
- this.active.set(environment, false);
305
- }
350
+ getId() {
351
+ return this.manifest.id;
306
352
  }
307
- markActive(environment) {
308
- if (this.active.has(environment)) {
309
- this.active.set(environment, true);
310
- }
353
+ getHandlers() {
354
+ return this.handlers;
311
355
  }
312
356
  };
313
357
 
314
- // src/domain/agentic-environment/errors/base-error.ts
315
- var AgenticError = class extends Error {
316
- constructor({ message, stack, source, enviornment }) {
317
- super();
318
- this.message = message;
319
- this.stack = stack;
320
- this.source = source;
321
- this.environment = enviornment;
358
+ // src/domain/agentic-environment/participant/memory.ts
359
+ var Memory = class _Memory {
360
+ constructor(context) {
361
+ this.context = context;
322
362
  }
323
- getSource() {
324
- return this.source;
363
+ getContext() {
364
+ return this.context;
325
365
  }
326
- getEnvironment() {
327
- return this.environment;
366
+ static create() {
367
+ const context = ModelContext.create();
368
+ return new _Memory(context);
328
369
  }
329
370
  };
330
371
 
331
- // src/domain/agentic-environment/agentic-environment.ts
332
- var AgenticEnvironment = class {
333
- constructor(name, opts) {
334
- this.subscribers = [];
335
- var _a;
336
- this.name = name;
337
- this.options = opts;
338
- this.showLogs = ((_a = this.options) == null ? void 0 : _a.silent) !== false;
339
- }
340
- subscribe(participant) {
341
- this.subscribers.push(participant);
342
- for (const subscriber of this.subscribers) {
343
- if (participant === subscriber) {
344
- participant.onJoined();
345
- } else {
346
- subscriber.onParticipantJoined(participant);
347
- }
348
- }
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);
349
393
  }
350
- unsubscribe(participant) {
351
- const exists = this.subscribers.includes(participant);
352
- if (!exists) {
353
- return;
354
- }
355
- participant.onLeft();
356
- this.subscribers = this.subscribers.filter((p) => p !== participant);
357
- for (const subscriber of this.subscribers) {
358
- subscriber.onParticipantLeft(participant);
359
- }
394
+ getTools() {
395
+ return this.tools;
360
396
  }
361
- deliverFunctionCall(source, item) {
362
- this.deliverToSubscribers(source, {
363
- internal: (src) => src.onFunctionCall(item),
364
- external: (sub) => sub.onExternalFunctionCall(source, item)
365
- });
397
+ getDeveloperMessage() {
398
+ return this.developerMessage;
366
399
  }
367
- deliverModelMessage(source, item) {
368
- this.deliverToSubscribers(source, {
369
- internal: (src) => src.onModelMessage(item),
370
- external: (sub) => sub.onExternalModelMessage(source, item)
371
- });
400
+ getMemory() {
401
+ return this.memory;
372
402
  }
373
- deliverReasoning(source, item) {
374
- this.deliverToSubscribers(source, {
375
- internal: (src) => src.onReasoning(item),
376
- external: (sub) => sub.onExternalReasoning(source, item)
377
- });
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);
378
429
  }
379
- deliverFunctionCallOutput(source, item) {
380
- this.deliverToSubscribers(source, {
381
- internal: (src) => src.onFunctionCallOutput(item),
382
- external: (sub) => sub.onExternalFunctionCallOutput(source, item)
383
- });
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();
384
445
  }
385
- deliverMessage(source, message) {
386
- this.deliverToSubscribers(source, {
387
- external: (sub) => sub.onMessage(message)
388
- });
446
+ addParticipant(participant) {
447
+ const isExists = this.participants.get(participant.getId());
448
+ if (isExists) return;
449
+ this.participants.set(participant.getId(), participant);
389
450
  }
390
- deliverSemanticEvent(source, item) {
391
- this.deliverToSubscribers(source, {
392
- internal: (src) => src.onInternalEvent(item),
393
- external: (sub) => sub.onExternalEvent(source, item)
394
- });
451
+ removeParticipant(participant) {
452
+ this.participants.delete(participant.getId());
395
453
  }
396
- deliverError(source, error) {
397
- this.deliverToSubscribers(source, {
398
- internal: (src) => {
399
- try {
400
- src.onError(error);
401
- } catch (error2) {
402
- if (this.showLogs) {
403
- console.warn(
404
- `[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
405
- ${JSON.stringify({ error: error2 }, null, 2)}`
406
- );
407
- }
408
- } finally {
409
- src.markInactive(this);
410
- }
411
- },
412
- external: (sub) => {
413
- try {
414
- sub.onParticipantError(source, error);
415
- } catch (error2) {
416
- if (this.showLogs) {
417
- console.warn(
418
- `[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
419
- ${JSON.stringify({ error: error2 }, null, 2)}`
420
- );
421
- }
422
- }
423
- }
424
- });
454
+ getParticipant(id) {
455
+ return this.participants.get(id);
425
456
  }
426
- getListeningParticipants(source) {
427
- return this.subscribers.filter(
428
- (sub) => sub !== source && (sub.getListeners().length === 0 || sub.getListeners().some((listener) => source instanceof listener))
429
- );
457
+ getParticipants() {
458
+ return [...this.participants.values()];
430
459
  }
431
- deliverToSubscribers(source, handlers) {
432
- var _a, _b;
433
- const authorizedListeners = this.getListeningParticipants(source);
434
- for (const subscriber of this.subscribers) {
435
- const isActive = subscriber.getEnvironmentState(this);
436
- if (isActive) {
437
- try {
438
- if (subscriber === source) {
439
- (_a = handlers == null ? void 0 : handlers.internal) == null ? void 0 : _a.call(handlers, subscriber);
440
- } else if (authorizedListeners.includes(subscriber)) {
441
- (_b = handlers == null ? void 0 : handlers.external) == null ? void 0 : _b.call(handlers, subscriber);
442
- }
443
- } catch (error) {
444
- this.handleError(subscriber, error);
445
- }
446
- }
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);
447
479
  }
448
480
  }
449
- handleError(subscriber, error) {
450
- if (error instanceof AgenticError) {
451
- return this.deliverError(subscriber, error);
481
+ getParticipant(id) {
482
+ if (!this.state.getParticipant(id)) {
483
+ throw new Error(`Participant ${id} not found`);
452
484
  }
453
- const agenticError = new AgenticError({
454
- message: error.message,
455
- stack: error.stack,
456
- enviornment: this,
457
- source: subscriber
458
- });
459
- return this.deliverError(subscriber, agenticError);
485
+ return this.state.getParticipant(id);
460
486
  }
461
- };
462
-
463
- // src/domain/model-context/semantic-event/semantic-event.ts
464
- var SemanticEvent = class {
465
- constructor(type, data) {
466
- this.type = type;
467
- this.data = data;
487
+ getInferenceRunner() {
488
+ return this.inferenceRunner;
468
489
  }
469
- getType() {
470
- return this.type;
490
+ getFunctionCallRunner() {
491
+ return this.functionCallRunner;
471
492
  }
472
493
  };
473
494
 
474
- // src/domain/agentic-environment/inference/response.ts
475
- var InferenceResponse = class {
476
- constructor(contextItems, tokenUsage) {
477
- this.contextItems = contextItems;
478
- 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
+ }
479
507
  }
480
508
  };
481
509
 
482
- // src/application/services/inference-runner.ts
483
- var StreamingInference = class {
484
- async *run(request, endpoint) {
485
- var _a;
486
- if ((_a = request.signal) == null ? void 0 : _a.aborted) {
487
- 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);
488
552
  }
489
- yield* endpoint.stream(request, request.signal);
490
553
  }
491
554
  };
492
- var NonStreamingInference = class {
493
- async *run(request, endpoint) {
494
- var _a, _b;
495
- if ((_a = request.signal) == null ? void 0 : _a.aborted) {
496
- return;
497
- }
498
- const response = await endpoint.infer(request);
499
- for (const item of response.contextItems) {
500
- if ((_b = request.signal) == null ? void 0 : _b.aborted) {
501
- break;
502
- }
503
- 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);
504
574
  }
505
575
  }
506
576
  };
507
577
 
508
- // src/application/use-cases/run-inference.ts
509
- var RunInference = class {
510
- constructor(generativeModelRepository2, requestValidator) {
511
- this.generativeModelRepository = generativeModelRepository2;
512
- 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";
513
583
  }
514
- async execute(inferenceParams) {
584
+ async run(input, loopVisitor) {
515
585
  var _a;
516
- const { caller, environment, signal } = inferenceParams;
517
- const generativeModel = await this.generativeModelRepository.getByModelName(inferenceParams.model);
518
- this.requestValidator.validate(inferenceParams, generativeModel.specification);
519
- const resolvedParams = {
520
- ...inferenceParams,
521
- maxOutputTokens: (_a = inferenceParams.maxOutputTokens) != null ? _a : generativeModel.specification.maxOutputTokens
522
- };
523
- const inferenceRunner = inferenceParams.streaming ? new StreamingInference() : new NonStreamingInference();
524
- const result = inferenceRunner.run(resolvedParams, generativeModel.endpoint);
525
- for await (const item of result) {
526
- if (signal == null ? void 0 : signal.aborted) {
527
- break;
528
- }
529
- if (item instanceof ReasoningItem) {
530
- environment.deliverReasoning(caller, item);
531
- } else if (item instanceof FunctionCallItem) {
532
- environment.deliverFunctionCall(caller, item);
533
- } else if (item instanceof ModelMessageItem) {
534
- environment.deliverModelMessage(caller, item);
535
- } else if (item instanceof SemanticEvent) {
536
- environment.deliverSemanticEvent(caller, item);
537
- }
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
+ };
538
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
+ };
539
607
  }
540
608
  };
541
609
 
542
- // src/application/use-cases/execute-function-call.ts
543
- var ExecuteFunctionCall = class {
544
- constructor(functionCallRunner) {
545
- this.functionCallRunner = functionCallRunner;
546
- }
547
- async execute(environment, functionCallItem, tool, caller, signal) {
548
- const stream = this.functionCallRunner.run(functionCallItem, tool, signal);
549
- for await (const item of stream) {
550
- environment.deliverFunctionCallOutput(caller, item);
551
- }
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
+ };
552
628
  }
553
629
  };
554
630
 
555
- // src/application/services/function-call-runner.ts
556
- var FunctionCallRunner = class {
557
- async *run(call, tool, signal) {
558
- if (signal == null ? void 0 : signal.aborted) {
559
- return;
560
- }
561
- if (!tool) throw new Error(`Unknown tool: ${call.name}`);
562
- const result = await tool.invoke(JSON.parse(call.args));
563
- 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
+ };
564
643
  }
565
644
  };
566
645
 
567
- // src/application/participants/participant.ts
568
- var BaseParticipant = class extends Participant {
569
- onFunctionCall(item) {
646
+ // src/domain/agentic-environment/loop/transition-resolver.ts
647
+ var TransitionResolver = class {
648
+ constructor(rules) {
649
+ this.rules = rules;
570
650
  }
571
- 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);
572
657
  }
573
- 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";
574
664
  }
575
- 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");
576
669
  }
577
- 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");
578
674
  }
579
- onExternalReasoning(source, item) {
675
+ };
676
+ var FunctionCallCompletedSpecification = class {
677
+ isSatisfiedBy(execution) {
678
+ return execution.stateId === "function_call";
580
679
  }
581
- onModelMessage(item) {
680
+ };
681
+ var ModelMessageCompletedSpecification = class {
682
+ isSatisfiedBy(execution) {
683
+ return execution.stateId === "model_message";
582
684
  }
583
- 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();
584
691
  }
585
- 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
+ };
586
700
  }
587
- onJoined() {
701
+ };
702
+ var InferenceToFunctionCallRule = class {
703
+ constructor() {
704
+ this.specification = new InferenceProducedFunctionCallSpecification();
588
705
  }
589
- 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
+ };
590
721
  }
591
- onParticipantJoined(participant) {
722
+ };
723
+ var InferenceToModelMessageRule = class {
724
+ constructor() {
725
+ this.specification = new InferenceProducedModelMessageSpecification();
592
726
  }
593
- 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
+ };
594
743
  }
595
- onInternalEvent(item) {
744
+ };
745
+ var FunctionCallToInferenceRule = class {
746
+ constructor() {
747
+ this.specification = new FunctionCallCompletedSpecification();
596
748
  }
597
- 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
+ };
598
761
  }
599
- onError(error) {
762
+ };
763
+ var ModelMessageToIdleRule = class {
764
+ constructor() {
765
+ this.specification = new ModelMessageCompletedSpecification();
600
766
  }
601
- 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
+ };
602
775
  }
603
776
  };
604
777
 
605
- // src/application/use-cases/send-message.ts
606
- var SendMessage = class {
607
- async execute(environment, message, caller) {
608
- if (!caller.isJoinedTo(environment)) {
609
- throw new Error("Not joined to environment");
610
- }
611
- 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
+ });
612
823
  }
613
824
  };
614
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
+
615
858
  // src/infrastructure/providers/anthropic/endpoints/anthropic-messages-mapper.ts
616
859
  var AnthropicMessagesMapper = class {
617
- toRequest(inferenceParams) {
618
- const { messages, system } = this.mapContextItems(inferenceParams);
860
+ toRequest(inferenceInput) {
861
+ const { messages, system } = this.mapContextItems(inferenceInput);
619
862
  const outputConfig = {};
620
863
  const request = {
621
- model: inferenceParams.model,
864
+ model: inferenceInput.model,
622
865
  messages
623
866
  };
624
- request.max_tokens = inferenceParams.maxOutputTokens;
867
+ request.max_tokens = inferenceInput.maxOutputTokens;
625
868
  if (system) {
626
869
  request.system = system;
627
870
  }
628
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
629
- request.tools = inferenceParams.tools.map((tool) => ({
871
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
872
+ request.tools = inferenceInput.tools.map((tool) => ({
630
873
  name: tool.name,
631
874
  description: tool.description,
632
875
  input_schema: tool.parameters
633
876
  }));
634
877
  }
635
- if (inferenceParams.structuredOutput) {
878
+ if (inferenceInput.structuredOutput) {
636
879
  outputConfig.format = {
637
880
  type: "json_schema",
638
- json_schema: inferenceParams.structuredOutput.schema
881
+ json_schema: inferenceInput.structuredOutput.schema
639
882
  };
640
883
  }
641
- if (inferenceParams.reasoningEffort) {
884
+ if (inferenceInput.reasoningEffort) {
642
885
  request.thinking = { type: "adaptive" };
643
- outputConfig.effort = inferenceParams.reasoningEffort;
886
+ outputConfig.effort = inferenceInput.reasoningEffort;
644
887
  }
645
888
  if (Object.keys(outputConfig).length > 0) {
646
889
  request.output_config = outputConfig;
647
890
  }
648
- if (inferenceParams.streaming) {
649
- request.stream = inferenceParams.streaming;
891
+ if (inferenceInput.streaming) {
892
+ request.stream = inferenceInput.streaming;
650
893
  }
651
894
  return request;
652
895
  }
653
- toResponse(response) {
654
- const contextItems = this.extractContextItems(response);
655
- const tokenUsage = this.extractTokenUsage(response);
656
- return new InferenceResponse(contextItems, tokenUsage);
657
- }
658
896
  addContentBlock(messages, role, block) {
659
897
  const lastMessage = messages[messages.length - 1];
660
898
  if ((lastMessage == null ? void 0 : lastMessage.role) === role) {
@@ -666,9 +904,9 @@ var AnthropicMessagesMapper = class {
666
904
  content: [block]
667
905
  });
668
906
  }
669
- mapContextItems(inferenceParams) {
907
+ mapContextItems(inferenceInput) {
670
908
  var _a, _b, _c;
671
- const context = inferenceParams.context;
909
+ const context = inferenceInput.context;
672
910
  const messages = [];
673
911
  const system = [];
674
912
  for (const item of context.getItems()) {
@@ -735,7 +973,7 @@ var AnthropicMessagesMapper = class {
735
973
  new OutputTokenDetails(0)
736
974
  );
737
975
  }
738
- extractContextItems(response) {
976
+ toResponse(response) {
739
977
  var _a;
740
978
  const items = [];
741
979
  for (const block of response.content) {
@@ -763,30 +1001,27 @@ var AnthropicMessagesMapper = class {
763
1001
  );
764
1002
  }
765
1003
  }
766
- return items;
1004
+ return { items, rowResponse: response };
767
1005
  }
768
1006
  };
769
1007
 
770
1008
  // src/infrastructure/providers/anthropic/endpoints/anthropic-messages.ts
771
1009
  import Anthropic from "@anthropic-ai/sdk";
772
1010
  var AnthropicMessages = class {
773
- constructor(endpointMapper = new AnthropicMessagesMapper()) {
1011
+ constructor(endpointMapper = new AnthropicMessagesMapper(), config = {}) {
774
1012
  this.endpointMapper = endpointMapper;
775
- this.client = new Anthropic();
1013
+ this.client = new Anthropic({ baseURL: config.baseURL, apiKey: config.apiKey });
776
1014
  }
777
- async infer(inferenceParams) {
778
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
779
- 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);
780
1018
  return this.endpointMapper.toResponse(response);
781
1019
  }
782
- async *stream(inferenceParams, signal) {
783
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
784
- 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);
785
1023
  for await (const event of stream) {
786
- if (signal == null ? void 0 : signal.aborted) {
787
- break;
788
- }
789
- yield new SemanticEvent(event.type, event);
1024
+ yield event;
790
1025
  }
791
1026
  }
792
1027
  };
@@ -808,27 +1043,27 @@ var SummaryText = class _SummaryText extends ItemContent {
808
1043
 
809
1044
  // src/infrastructure/providers/openai/endpoints/openai-responses-mapper.ts
810
1045
  var OpenAIResponsesMapper = class {
811
- toRequest(inferenceParams) {
1046
+ toRequest(inferenceInput) {
812
1047
  var _a, _b;
813
1048
  const request = {
814
- model: inferenceParams.model,
815
- input: this.mapContextItems(inferenceParams)
1049
+ model: inferenceInput.model,
1050
+ input: this.mapContextItems(inferenceInput)
816
1051
  };
817
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
818
- request.tools = inferenceParams.tools.map((tool) => ({
1052
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1053
+ request.tools = inferenceInput.tools.map((tool) => ({
819
1054
  type: tool.type,
820
1055
  name: tool.name,
821
1056
  description: tool.description,
822
1057
  parameters: tool.parameters
823
1058
  }));
824
1059
  }
825
- if (inferenceParams.reasoningEffort) {
1060
+ if (inferenceInput.reasoningEffort) {
826
1061
  request.reasoning = {
827
- effort: inferenceParams.reasoningEffort
1062
+ effort: inferenceInput.reasoningEffort
828
1063
  };
829
1064
  }
830
- if (inferenceParams.structuredOutput) {
831
- const format = inferenceParams.structuredOutput;
1065
+ if (inferenceInput.structuredOutput) {
1066
+ const format = inferenceInput.structuredOutput;
832
1067
  request.text = {
833
1068
  format: {
834
1069
  type: "json_schema",
@@ -838,19 +1073,14 @@ var OpenAIResponsesMapper = class {
838
1073
  }
839
1074
  };
840
1075
  }
841
- if (inferenceParams.streaming) {
842
- request.stream = inferenceParams.streaming;
1076
+ if (inferenceInput.streaming) {
1077
+ request.stream = inferenceInput.streaming;
843
1078
  }
844
1079
  return request;
845
1080
  }
846
- toResponse(response) {
847
- const contextItems = this.extractContextItems(response);
848
- const tokenUsage = this.extractTokenUsage(response);
849
- return new InferenceResponse(contextItems, tokenUsage);
850
- }
851
- mapContextItems(inferenceParams) {
1081
+ mapContextItems(inferenceInput) {
852
1082
  const input = [];
853
- for (const item of inferenceParams.context.getItems()) {
1083
+ for (const item of inferenceInput.context.getItems()) {
854
1084
  if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem || item instanceof UserMessageItem) {
855
1085
  input.push({
856
1086
  type: item.type,
@@ -910,7 +1140,7 @@ var OpenAIResponsesMapper = class {
910
1140
  new OutputTokenDetails((_d = (_c = response.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
911
1141
  );
912
1142
  }
913
- extractContextItems(response) {
1143
+ toResponse(response) {
914
1144
  var _a, _b, _c;
915
1145
  const items = [];
916
1146
  for (const item of (_a = response.output) != null ? _a : []) {
@@ -943,7 +1173,10 @@ var OpenAIResponsesMapper = class {
943
1173
  );
944
1174
  }
945
1175
  }
946
- return items;
1176
+ return {
1177
+ items,
1178
+ rowResponse: response
1179
+ };
947
1180
  }
948
1181
  };
949
1182
 
@@ -953,19 +1186,16 @@ var OpenAIResponses = class {
953
1186
  this.endpointMapper = endpointMapper;
954
1187
  this.client = new OpenAI();
955
1188
  }
956
- async infer(inferenceParams) {
957
- const request = this.endpointMapper.toRequest(inferenceParams);
1189
+ async infer(inferenceInput) {
1190
+ const request = this.endpointMapper.toRequest(inferenceInput);
958
1191
  const response = await this.client.responses.create(request);
959
1192
  return this.endpointMapper.toResponse(response);
960
1193
  }
961
- async *stream(inferenceParams, signal) {
962
- const request = this.endpointMapper.toRequest(inferenceParams);
1194
+ async *stream(inferenceInput) {
1195
+ const request = this.endpointMapper.toRequest(inferenceInput);
963
1196
  const response = await this.client.responses.create(request);
964
1197
  for await (const event of response) {
965
- if (signal == null ? void 0 : signal.aborted) {
966
- break;
967
- }
968
- yield new SemanticEvent(event.type, event);
1198
+ yield event;
969
1199
  }
970
1200
  }
971
1201
  };
@@ -975,16 +1205,16 @@ import { GoogleGenAI } from "@google/genai";
975
1205
 
976
1206
  // src/infrastructure/providers/gemini/endpoints/gemini-generate-content-mapper.ts
977
1207
  var GeminiGenerateContentMapper = class {
978
- toRequest(inferenceParams) {
979
- const { contents, systemInstruction } = this.mapContextItems(inferenceParams);
1208
+ toRequest(inferenceInput) {
1209
+ const { contents, systemInstruction } = this.mapContextItems(inferenceInput);
980
1210
  const config = {};
981
1211
  if (systemInstruction) {
982
1212
  config.systemInstruction = systemInstruction;
983
1213
  }
984
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
1214
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
985
1215
  config.tools = [
986
1216
  {
987
- functionDeclarations: inferenceParams.tools.map((tool) => ({
1217
+ functionDeclarations: inferenceInput.tools.map((tool) => ({
988
1218
  name: tool.name,
989
1219
  description: tool.description,
990
1220
  parametersJsonSchema: tool.parameters
@@ -992,34 +1222,29 @@ var GeminiGenerateContentMapper = class {
992
1222
  }
993
1223
  ];
994
1224
  }
995
- if (inferenceParams.structuredOutput) {
1225
+ if (inferenceInput.structuredOutput) {
996
1226
  config.responseMimeType = "application/json";
997
- config.responseSchema = inferenceParams.structuredOutput.schema;
1227
+ config.responseSchema = inferenceInput.structuredOutput.schema;
998
1228
  }
999
- if (inferenceParams.reasoningEffort) {
1229
+ if (inferenceInput.reasoningEffort) {
1000
1230
  config.thinkingConfig = {
1001
- thinkingLevel: inferenceParams.reasoningEffort,
1231
+ thinkingLevel: inferenceInput.reasoningEffort,
1002
1232
  includeThoughts: true
1003
1233
  };
1004
1234
  }
1005
1235
  const request = {
1006
- model: inferenceParams.model,
1236
+ model: inferenceInput.model,
1007
1237
  contents,
1008
1238
  config
1009
1239
  };
1010
- if (inferenceParams.streaming) {
1011
- request.stream = inferenceParams.streaming;
1240
+ if (inferenceInput.streaming) {
1241
+ request.stream = inferenceInput.streaming;
1012
1242
  }
1013
1243
  return request;
1014
1244
  }
1015
- toResponse(response) {
1016
- const contextItems = this.extractContextItems(response);
1017
- const tokenUsage = this.extractTokenUsage(response);
1018
- return new InferenceResponse(contextItems, tokenUsage);
1019
- }
1020
- mapContextItems(inferenceParams) {
1245
+ mapContextItems(inferenceInput) {
1021
1246
  var _a;
1022
- const context = inferenceParams.context;
1247
+ const context = inferenceInput.context;
1023
1248
  const contents = [];
1024
1249
  const system = [];
1025
1250
  const callNames = /* @__PURE__ */ new Map();
@@ -1092,7 +1317,7 @@ var GeminiGenerateContentMapper = class {
1092
1317
  new OutputTokenDetails((_e = usage.thoughtsTokenCount) != null ? _e : 0)
1093
1318
  );
1094
1319
  }
1095
- extractContextItems(response) {
1320
+ toResponse(response) {
1096
1321
  var _a, _b, _c, _d, _e, _f;
1097
1322
  const items = [];
1098
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 : [];
@@ -1121,42 +1346,43 @@ var GeminiGenerateContentMapper = class {
1121
1346
  );
1122
1347
  }
1123
1348
  }
1124
- return items;
1349
+ return { items, rowResponse: response };
1125
1350
  }
1126
1351
  };
1127
1352
 
1128
1353
  // src/infrastructure/providers/gemini/endpoints/gemini-generate-content.ts
1129
1354
  var GeminiGenerateContent = class {
1130
- constructor(endpointMapper = new GeminiGenerateContentMapper()) {
1355
+ constructor(endpointMapper = new GeminiGenerateContentMapper(), config = {}) {
1356
+ var _a;
1131
1357
  this.endpointMapper = endpointMapper;
1132
- 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
+ });
1133
1362
  }
1134
- async infer(inferenceParams) {
1135
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
1136
- 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);
1137
1366
  return this.endpointMapper.toResponse(response);
1138
1367
  }
1139
- async *stream(inferenceParams, signal) {
1140
- const inferenceRequest = this.endpointMapper.toRequest(inferenceParams);
1141
- 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);
1142
1371
  for await (const chunk of stream) {
1143
- if (signal == null ? void 0 : signal.aborted) {
1144
- break;
1145
- }
1146
- yield new SemanticEvent("generate_content_chunk", chunk);
1372
+ yield chunk;
1147
1373
  }
1148
1374
  }
1149
1375
  };
1150
1376
 
1151
1377
  // src/infrastructure/providers/openai/endpoints/openai-chat-completions-mapper.ts
1152
1378
  var OpenAIChatCompletionsMapper = class {
1153
- toRequest(inferenceParams) {
1379
+ toRequest(inferenceInput) {
1154
1380
  const request = {
1155
- model: inferenceParams.model,
1156
- messages: this.mapContextItems(inferenceParams)
1381
+ model: inferenceInput.model,
1382
+ messages: this.mapContextItems(inferenceInput)
1157
1383
  };
1158
- if (inferenceParams.tools && inferenceParams.tools.length > 0) {
1159
- request.tools = inferenceParams.tools.map((tool) => ({
1384
+ if (inferenceInput.tools && inferenceInput.tools.length > 0) {
1385
+ request.tools = inferenceInput.tools.map((tool) => ({
1160
1386
  type: "function",
1161
1387
  function: {
1162
1388
  name: tool.name,
@@ -1165,23 +1391,18 @@ var OpenAIChatCompletionsMapper = class {
1165
1391
  }
1166
1392
  }));
1167
1393
  }
1168
- if (inferenceParams.reasoningEffort) {
1169
- request.reasoning_effort = inferenceParams.reasoningEffort;
1394
+ if (inferenceInput.reasoningEffort) {
1395
+ request.reasoning_effort = inferenceInput.reasoningEffort;
1170
1396
  }
1171
- if (inferenceParams.streaming) {
1172
- request.stream = inferenceParams.streaming;
1397
+ if (inferenceInput.streaming) {
1398
+ request.stream = inferenceInput.streaming;
1173
1399
  }
1174
1400
  return request;
1175
1401
  }
1176
- toResponse(response) {
1177
- const contextItems = this.extractContextItems(response);
1178
- const tokenUsage = this.extractTokenUsage(response);
1179
- return new InferenceResponse(contextItems, tokenUsage);
1180
- }
1181
- mapContextItems(inferenceParams) {
1402
+ mapContextItems(inferenceInput) {
1182
1403
  var _a;
1183
1404
  const messages = [];
1184
- for (const item of inferenceParams.context.getItems()) {
1405
+ for (const item of inferenceInput.context.getItems()) {
1185
1406
  if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem) {
1186
1407
  messages.push({ role: "system", content: item.content.text });
1187
1408
  continue;
@@ -1228,12 +1449,12 @@ var OpenAIChatCompletionsMapper = class {
1228
1449
  new OutputTokenDetails((_d = (_c = response.usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
1229
1450
  );
1230
1451
  }
1231
- extractContextItems(response) {
1452
+ toResponse(response) {
1232
1453
  var _a, _b, _c;
1233
1454
  const items = [];
1234
1455
  const message = (_b = (_a = response.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1235
1456
  if (!message) {
1236
- return items;
1457
+ return { items: [], rowResponse: response };
1237
1458
  }
1238
1459
  if (message.reasoning_content) {
1239
1460
  items.push(
@@ -1256,7 +1477,7 @@ var OpenAIChatCompletionsMapper = class {
1256
1477
  })
1257
1478
  );
1258
1479
  }
1259
- return items;
1480
+ return { items, rowResponse: response };
1260
1481
  }
1261
1482
  };
1262
1483
 
@@ -1272,24 +1493,20 @@ var OpenAIChatCompletions = class {
1272
1493
  });
1273
1494
  this.extraBody = (_a = config.extraBody) != null ? _a : {};
1274
1495
  }
1275
- buildRequest(inferenceParams) {
1496
+ buildRequest(inferenceInput) {
1276
1497
  return {
1277
1498
  ...this.extraBody,
1278
- ...this.endpointMapper.toRequest(inferenceParams)
1499
+ ...this.endpointMapper.toRequest(inferenceInput)
1279
1500
  };
1280
1501
  }
1281
- async infer(inferenceParams) {
1282
- 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));
1283
1504
  return this.endpointMapper.toResponse(response);
1284
1505
  }
1285
- async *stream(inferenceParams, signal) {
1286
- var _a;
1287
- 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));
1288
1508
  for await (const event of stream) {
1289
- if (signal == null ? void 0 : signal.aborted) {
1290
- break;
1291
- }
1292
- yield new SemanticEvent((_a = event.object) != null ? _a : "chat.completion.chunk", event);
1509
+ yield event;
1293
1510
  }
1294
1511
  }
1295
1512
  };
@@ -1558,103 +1775,71 @@ var claudeHaiku45Specification = {
1558
1775
  supportsStructuredOutput: true
1559
1776
  };
1560
1777
 
1561
- // src/infrastructure/repository/generative-model-repository.ts
1562
- var InMemoryGenerativeModelRepository = class {
1563
- getByModelName(modelName) {
1564
- let generativeModel = void 0;
1565
- switch (modelName) {
1566
- case "gpt-5.4":
1567
- generativeModel = {
1568
- endpoint: new OpenAIResponses(),
1569
- specification: gpt54Specification
1570
- };
1571
- break;
1572
- case "gpt-5.4-mini":
1573
- generativeModel = {
1574
- endpoint: new OpenAIResponses(),
1575
- specification: gpt54MiniSpecification
1576
- };
1577
- break;
1578
- case "gpt-5.4-nano":
1579
- generativeModel = {
1580
- endpoint: new OpenAIResponses(),
1581
- specification: gpt54NanoSpecification
1582
- };
1583
- break;
1584
- case "gpt-5.5":
1585
- generativeModel = {
1586
- endpoint: new OpenAIResponses(),
1587
- specification: gpt55Specification
1588
- };
1589
- break;
1590
- case "claude-haiku-4-5":
1591
- generativeModel = {
1592
- endpoint: new AnthropicMessages(),
1593
- specification: claudeHaiku45Specification
1594
- };
1595
- break;
1596
- case "claude-sonnet-4-6":
1597
- generativeModel = {
1598
- endpoint: new AnthropicMessages(),
1599
- specification: claudeSonnet46Specification
1600
- };
1601
- break;
1602
- case "claude-opus-4-7":
1603
- generativeModel = {
1604
- endpoint: new AnthropicMessages(),
1605
- specification: claudeOpus47Specification
1606
- };
1607
- break;
1608
- case "claude-opus-4-8":
1609
- generativeModel = {
1610
- endpoint: new AnthropicMessages(),
1611
- specification: claudeOpus48Specification
1612
- };
1613
- break;
1614
- case "gemini-3.5-flash":
1615
- generativeModel = {
1616
- endpoint: new GeminiGenerateContent(),
1617
- specification: gemini35FlashSpecification
1618
- };
1619
- break;
1620
- case "gemini-3.1-pro-preview":
1621
- generativeModel = {
1622
- endpoint: new GeminiGenerateContent(),
1623
- specification: gemini31ProSpecification
1624
- };
1625
- break;
1626
- case "deepseek-v4-flash":
1627
- generativeModel = {
1628
- endpoint: new OpenAIChatCompletions(),
1629
- specification: deepSeekV4FlashSpecification
1630
- };
1631
- break;
1632
- case "deepseek-v4-pro":
1633
- generativeModel = {
1634
- endpoint: new OpenAIChatCompletions(),
1635
- specification: deepSeekV4ProSpecification
1636
- };
1637
- }
1638
- if (!generativeModel) {
1639
- throw new Error(`Unsupported model: ${modelName}`);
1640
- }
1641
- 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
1642
1827
  }
1643
- };
1828
+ ];
1644
1829
 
1645
1830
  // src/domain/generative-model/request-validation/reasoning-effort.ts
1646
1831
  var ReasoningEffortValidation = class {
1647
1832
  constructor() {
1648
1833
  this.name = "reasoning-effort";
1649
1834
  }
1650
- isValid(inferenceParams, model) {
1651
- if (inferenceParams.reasoningEffort === void 0) {
1835
+ isValid(inferenceInput, model) {
1836
+ if (inferenceInput.reasoningEffort === void 0) {
1652
1837
  return true;
1653
1838
  }
1654
1839
  if (!model.supportsReasoningEffort) {
1655
1840
  return false;
1656
1841
  }
1657
- return model.supportedReasoningEfforts.includes(inferenceParams.reasoningEffort);
1842
+ return model.supportedReasoningEfforts.includes(inferenceInput.reasoningEffort);
1658
1843
  }
1659
1844
  };
1660
1845
 
@@ -1663,8 +1848,8 @@ var ToolCallingValidation = class {
1663
1848
  constructor() {
1664
1849
  this.name = "tool-calling";
1665
1850
  }
1666
- isValid(inferenceParams, model) {
1667
- if (inferenceParams.tools === void 0) {
1851
+ isValid(inferenceInput, model) {
1852
+ if (inferenceInput.tools === void 0) {
1668
1853
  return true;
1669
1854
  }
1670
1855
  return model.supportsFunctionCalling;
@@ -1676,8 +1861,8 @@ var StreamingValidation = class {
1676
1861
  constructor() {
1677
1862
  this.name = "streaming";
1678
1863
  }
1679
- isValid(inferenceParams, model) {
1680
- if (!inferenceParams.streaming) {
1864
+ isValid(inferenceInput, model) {
1865
+ if (!inferenceInput.streaming) {
1681
1866
  return true;
1682
1867
  }
1683
1868
  return model.supportsStreaming;
@@ -1704,8 +1889,8 @@ var ContextValidation = class {
1704
1889
  constructor() {
1705
1890
  this.name = "context";
1706
1891
  }
1707
- isValid(inferenceParams, model) {
1708
- return inferenceParams.context.items.every(
1892
+ isValid(inferenceInput, model) {
1893
+ return inferenceInput.context.items.every(
1709
1894
  (item) => model.supportedContextItemTypes.includes(getContextItemValidationKey(item))
1710
1895
  );
1711
1896
  }
@@ -1719,56 +1904,122 @@ var defaultRequestValidationRules = [
1719
1904
  new StructuredOutputValidation(),
1720
1905
  new ContextValidation()
1721
1906
  ];
1722
- var InferenceRequestValidator = class {
1907
+ var InferenceInputValidator = class {
1723
1908
  constructor(rules = defaultRequestValidationRules) {
1724
1909
  this.rules = rules;
1725
1910
  }
1726
- validate(inferenceParams, model) {
1911
+ validate(inferenceInput, model) {
1727
1912
  for (const rule of this.rules) {
1728
- if (!rule.isValid(inferenceParams, model)) {
1913
+ if (!rule.isValid(inferenceInput, model)) {
1729
1914
  throw new Error(`Request validation "${rule.name}" failed for model "${model.name}"`);
1730
1915
  }
1731
1916
  }
1732
1917
  }
1733
1918
  };
1734
1919
 
1735
- // src/index.ts
1736
- var generativeModelRepository = new InMemoryGenerativeModelRepository();
1737
- var inferenceRequestValidator = new InferenceRequestValidator();
1738
- var runInferenceUseCase = new RunInference(generativeModelRepository, inferenceRequestValidator);
1739
- var executeFunctionCallUseCase = new ExecuteFunctionCall(new FunctionCallRunner());
1740
- var sendMessageUseCase = new SendMessage();
1741
- var runInference = (inferenceParams) => {
1742
- runInferenceUseCase.execute(inferenceParams);
1743
- };
1744
- var executeFunctionCall = (environment, functionCallItem, tool, caller) => {
1745
- 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
+ }
1746
1942
  };
1747
- var sendMessage = (environment, message, caller) => {
1748
- 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
+ }
1749
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
+ }
1750
2003
  export {
1751
- AgenticEnvironment,
1752
- AgenticError,
1753
- BaseParticipant,
1754
2004
  ContextItem,
1755
2005
  DeveloperMessageItem,
1756
2006
  FunctionCallItem,
1757
2007
  FunctionCallOutputItem,
1758
- InMemoryModelContextRepository,
1759
- InferenceResponse,
1760
2008
  InputTokenDetails,
2009
+ McpClient,
2010
+ McpToolRegistry,
1761
2011
  ModelContext,
1762
2012
  ModelMessageItem,
1763
2013
  OutputTokenDetails,
1764
2014
  Participant,
1765
2015
  ReasoningItem,
2016
+ RuntimeState,
1766
2017
  SemanticEvent,
1767
2018
  SystemMessageItem,
1768
2019
  TokenUsage,
1769
2020
  UserMessageItem,
1770
- executeFunctionCall,
1771
- runInference,
1772
- sendMessage
2021
+ createAgent,
2022
+ createHuman,
2023
+ defineRuntime
1773
2024
  };
1774
2025
  //# sourceMappingURL=index.mjs.map