@mozaik-ai/core 3.14.0 → 4.0.0-beta.10
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/README.md +18 -118
- package/dist/index.d.mts +436 -199
- package/dist/index.d.ts +436 -199
- package/dist/index.js +939 -589
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +926 -581
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
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,
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
return this.items[this.items.length - 1];
|
|
10
|
+
addContextItems(contextItems) {
|
|
11
|
+
this.items.push(...contextItems);
|
|
12
|
+
return this;
|
|
30
13
|
}
|
|
31
|
-
|
|
14
|
+
addItem(item) {
|
|
15
|
+
this.items.push(item);
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
static create() {
|
|
32
19
|
const id = crypto.randomUUID();
|
|
33
|
-
return new _ModelContext(id,
|
|
20
|
+
return new _ModelContext(id, []);
|
|
34
21
|
}
|
|
35
22
|
static rehydrate(data) {
|
|
36
|
-
return new _ModelContext(data.id, data.
|
|
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(
|
|
162
|
-
if (
|
|
148
|
+
isValid(inferenceInput, model) {
|
|
149
|
+
if (inferenceInput.structuredOutput === void 0) {
|
|
163
150
|
return true;
|
|
164
151
|
}
|
|
165
152
|
return model.supportsStructuredOutput;
|
|
@@ -296,48 +283,6 @@ var McpToolRegistry = class {
|
|
|
296
283
|
}
|
|
297
284
|
};
|
|
298
285
|
|
|
299
|
-
// src/infrastructure/repository/in-memory-model-context-repository.ts
|
|
300
|
-
var InMemoryModelContextRepository = class {
|
|
301
|
-
constructor() {
|
|
302
|
-
this.store = /* @__PURE__ */ new Map();
|
|
303
|
-
this.projectIndex = /* @__PURE__ */ new Map();
|
|
304
|
-
}
|
|
305
|
-
async save(context) {
|
|
306
|
-
const existing = this.store.get(context.id);
|
|
307
|
-
if (existing && existing.projectId !== context.projectId) {
|
|
308
|
-
const prevSet = this.projectIndex.get(existing.projectId);
|
|
309
|
-
prevSet == null ? void 0 : prevSet.delete(existing.id);
|
|
310
|
-
if (prevSet && prevSet.size === 0) this.projectIndex.delete(existing.projectId);
|
|
311
|
-
}
|
|
312
|
-
this.store.set(context.id, this.clone(context));
|
|
313
|
-
let set = this.projectIndex.get(context.projectId);
|
|
314
|
-
if (!set) {
|
|
315
|
-
set = /* @__PURE__ */ new Set();
|
|
316
|
-
this.projectIndex.set(context.projectId, set);
|
|
317
|
-
}
|
|
318
|
-
set.add(context.id);
|
|
319
|
-
}
|
|
320
|
-
async get(id) {
|
|
321
|
-
const context = this.store.get(id);
|
|
322
|
-
if (!context) {
|
|
323
|
-
throw new Error(`Context not found: ${id}`);
|
|
324
|
-
}
|
|
325
|
-
return this.clone(context);
|
|
326
|
-
}
|
|
327
|
-
async getByProjectId(projectId) {
|
|
328
|
-
const ids = this.projectIndex.get(projectId);
|
|
329
|
-
if (!ids || ids.size === 0) return [];
|
|
330
|
-
return [...ids].map((id) => this.store.get(id)).filter((c) => Boolean(c)).map((c) => this.clone(c));
|
|
331
|
-
}
|
|
332
|
-
clone(context) {
|
|
333
|
-
return ModelContext.rehydrate({
|
|
334
|
-
id: context.id,
|
|
335
|
-
projectId: context.projectId,
|
|
336
|
-
items: [...context.getItems()]
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
};
|
|
340
|
-
|
|
341
286
|
// src/domain/model-context/context-item/client-item/system-message.ts
|
|
342
287
|
var SystemMessageItem = class _SystemMessageItem extends ContextItem {
|
|
343
288
|
constructor(content) {
|
|
@@ -356,397 +301,664 @@ var SystemMessageItem = class _SystemMessageItem extends ContextItem {
|
|
|
356
301
|
}
|
|
357
302
|
};
|
|
358
303
|
|
|
359
|
-
// src/domain/agentic-environment/
|
|
360
|
-
var
|
|
361
|
-
constructor() {
|
|
362
|
-
this.
|
|
363
|
-
this.
|
|
364
|
-
this.
|
|
304
|
+
// src/domain/agentic-environment/semantic-event/event.ts
|
|
305
|
+
var SemanticEvent = class _SemanticEvent {
|
|
306
|
+
constructor(type, producerId, occurredAt, payload) {
|
|
307
|
+
this.type = type;
|
|
308
|
+
this.producerId = producerId;
|
|
309
|
+
this.occurredAt = occurredAt;
|
|
310
|
+
this.payload = payload;
|
|
365
311
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
}
|
|
370
|
-
environment.subscribe(this);
|
|
371
|
-
this.environments.push(environment);
|
|
372
|
-
this.active.set(environment, true);
|
|
312
|
+
static create(type, producerId, payload) {
|
|
313
|
+
const occurredAt = /* @__PURE__ */ new Date();
|
|
314
|
+
return new _SemanticEvent(type, producerId, occurredAt, payload);
|
|
373
315
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
316
|
+
};
|
|
317
|
+
var ParticipantJoinedEvent = class extends SemanticEvent {
|
|
318
|
+
static init(manifest) {
|
|
319
|
+
return SemanticEvent.create("participant.joined", manifest.id, manifest);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
var ParticipantLeftEvent = class extends SemanticEvent {
|
|
323
|
+
static init(manifest) {
|
|
324
|
+
return SemanticEvent.create("participant.left", manifest.id, manifest);
|
|
381
325
|
}
|
|
382
|
-
|
|
383
|
-
|
|
326
|
+
};
|
|
327
|
+
var MessageSentEvent = class extends SemanticEvent {
|
|
328
|
+
static init(producerId, message) {
|
|
329
|
+
return SemanticEvent.create("message.sent", producerId, { message });
|
|
384
330
|
}
|
|
385
|
-
|
|
386
|
-
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
// src/application/states/inference.ts
|
|
334
|
+
var InferenceState = class {
|
|
335
|
+
constructor(inferenceRunner) {
|
|
336
|
+
this.inferenceRunner = inferenceRunner;
|
|
337
|
+
this.id = "inference";
|
|
338
|
+
}
|
|
339
|
+
async run(input, loopVisitor) {
|
|
340
|
+
loopVisitor.visitInferenceStarted(input);
|
|
341
|
+
const output = await this.inferenceRunner.run(input);
|
|
342
|
+
loopVisitor.visitInferenceCompleted(output);
|
|
343
|
+
return {
|
|
344
|
+
stateId: this.id,
|
|
345
|
+
input,
|
|
346
|
+
output
|
|
347
|
+
};
|
|
387
348
|
}
|
|
388
|
-
|
|
389
|
-
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
// src/domain/agentic-environment/participant/participant.ts
|
|
352
|
+
var Participant = class {
|
|
353
|
+
constructor(manifest, handlers) {
|
|
354
|
+
this.manifest = manifest;
|
|
355
|
+
this.handlers = handlers;
|
|
390
356
|
}
|
|
391
|
-
|
|
392
|
-
return this.
|
|
357
|
+
getManifest() {
|
|
358
|
+
return this.manifest;
|
|
393
359
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
this.active.set(environment, false);
|
|
397
|
-
}
|
|
360
|
+
getId() {
|
|
361
|
+
return this.manifest.id;
|
|
398
362
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
363
|
+
getHandlers() {
|
|
364
|
+
return this.handlers;
|
|
365
|
+
}
|
|
366
|
+
setHandlers(handlers) {
|
|
367
|
+
this.handlers = handlers;
|
|
403
368
|
}
|
|
404
369
|
};
|
|
405
370
|
|
|
406
|
-
// src/domain/agentic-environment/
|
|
407
|
-
var
|
|
408
|
-
constructor(
|
|
409
|
-
|
|
410
|
-
this.message = message;
|
|
411
|
-
this.stack = stack;
|
|
412
|
-
this.source = source;
|
|
413
|
-
this.environment = enviornment;
|
|
371
|
+
// src/domain/agentic-environment/participant/memory.ts
|
|
372
|
+
var Memory = class _Memory {
|
|
373
|
+
constructor(context) {
|
|
374
|
+
this.context = context;
|
|
414
375
|
}
|
|
415
|
-
|
|
416
|
-
return this.
|
|
376
|
+
getContext() {
|
|
377
|
+
return this.context;
|
|
417
378
|
}
|
|
418
|
-
|
|
419
|
-
|
|
379
|
+
static create() {
|
|
380
|
+
const context = ModelContext.create();
|
|
381
|
+
return new _Memory(context);
|
|
420
382
|
}
|
|
421
383
|
};
|
|
422
384
|
|
|
423
|
-
// src/domain/agentic-environment/
|
|
424
|
-
var
|
|
425
|
-
constructor(
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
this.
|
|
429
|
-
this.
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
}
|
|
385
|
+
// src/domain/agentic-environment/participant/agent.ts
|
|
386
|
+
var Agent = class _Agent extends Participant {
|
|
387
|
+
constructor(manifest, developerMessage, tools, memory, handlers) {
|
|
388
|
+
super(manifest, handlers);
|
|
389
|
+
this.memory = memory;
|
|
390
|
+
this.developerMessage = developerMessage;
|
|
391
|
+
this.tools = tools;
|
|
392
|
+
}
|
|
393
|
+
static create({
|
|
394
|
+
name,
|
|
395
|
+
instruction,
|
|
396
|
+
tools,
|
|
397
|
+
capabilities,
|
|
398
|
+
handlers
|
|
399
|
+
}) {
|
|
400
|
+
const id = crypto.randomUUID();
|
|
401
|
+
const memory = Memory.create();
|
|
402
|
+
const manifest = { id, name, capabilities, role: "agent" };
|
|
403
|
+
const developerMessage = DeveloperMessageItem.create(instruction);
|
|
404
|
+
memory.getContext().addItem(developerMessage);
|
|
405
|
+
return new _Agent(manifest, instruction, tools, memory, handlers);
|
|
441
406
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
if (!exists) {
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
participant.onLeft();
|
|
448
|
-
this.subscribers = this.subscribers.filter((p) => p !== participant);
|
|
449
|
-
for (const subscriber of this.subscribers) {
|
|
450
|
-
subscriber.onParticipantLeft(participant);
|
|
451
|
-
}
|
|
407
|
+
getTools() {
|
|
408
|
+
return this.tools;
|
|
452
409
|
}
|
|
453
|
-
|
|
454
|
-
this.
|
|
455
|
-
internal: (src) => src.onFunctionCall(item),
|
|
456
|
-
external: (sub) => sub.onExternalFunctionCall(source, item)
|
|
457
|
-
});
|
|
410
|
+
getDeveloperMessage() {
|
|
411
|
+
return this.developerMessage;
|
|
458
412
|
}
|
|
459
|
-
|
|
460
|
-
this.
|
|
461
|
-
internal: (src) => src.onModelMessage(item),
|
|
462
|
-
external: (sub) => sub.onExternalModelMessage(source, item)
|
|
463
|
-
});
|
|
413
|
+
getMemory() {
|
|
414
|
+
return this.memory;
|
|
464
415
|
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
// src/application/use-cases/create-agent.ts
|
|
419
|
+
function createAgent({
|
|
420
|
+
name,
|
|
421
|
+
capabilities,
|
|
422
|
+
instruction,
|
|
423
|
+
tools,
|
|
424
|
+
handlers
|
|
425
|
+
}) {
|
|
426
|
+
return Agent.create({ name, instruction, capabilities, tools, handlers });
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/domain/agentic-environment/participant/human.ts
|
|
430
|
+
var Human = class _Human extends Participant {
|
|
431
|
+
constructor(manifest, handlers) {
|
|
432
|
+
super(manifest, handlers);
|
|
433
|
+
}
|
|
434
|
+
static create({
|
|
435
|
+
name,
|
|
436
|
+
capabilities,
|
|
437
|
+
handlers
|
|
438
|
+
}) {
|
|
439
|
+
const id = crypto.randomUUID();
|
|
440
|
+
const manifest = { id, name, capabilities, role: "human" };
|
|
441
|
+
return new _Human(manifest, handlers);
|
|
470
442
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
// src/application/use-cases/create-human.ts
|
|
446
|
+
function createHuman({
|
|
447
|
+
name,
|
|
448
|
+
capabilities,
|
|
449
|
+
handlers
|
|
450
|
+
}) {
|
|
451
|
+
return Human.create({ name, capabilities, handlers });
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// src/domain/agentic-environment/runtime-state.ts
|
|
455
|
+
var RuntimeState = class {
|
|
456
|
+
constructor() {
|
|
457
|
+
this.participants = /* @__PURE__ */ new Map();
|
|
476
458
|
}
|
|
477
|
-
|
|
478
|
-
this.
|
|
479
|
-
|
|
480
|
-
|
|
459
|
+
addParticipant(participant) {
|
|
460
|
+
const isExists = this.participants.get(participant.getId());
|
|
461
|
+
if (isExists) return;
|
|
462
|
+
this.participants.set(participant.getId(), participant);
|
|
481
463
|
}
|
|
482
|
-
|
|
483
|
-
this.
|
|
484
|
-
internal: (src) => src.onInternalEvent(item),
|
|
485
|
-
external: (sub) => sub.onExternalEvent(source, item)
|
|
486
|
-
});
|
|
464
|
+
removeParticipant(participant) {
|
|
465
|
+
this.participants.delete(participant.getId());
|
|
487
466
|
}
|
|
488
|
-
|
|
489
|
-
this.
|
|
490
|
-
internal: (src) => {
|
|
491
|
-
try {
|
|
492
|
-
src.onError(error);
|
|
493
|
-
} catch (error2) {
|
|
494
|
-
if (this.showLogs) {
|
|
495
|
-
console.warn(
|
|
496
|
-
`[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
|
|
497
|
-
${JSON.stringify({ error: error2 }, null, 2)}`
|
|
498
|
-
);
|
|
499
|
-
}
|
|
500
|
-
} finally {
|
|
501
|
-
src.markInactive(this);
|
|
502
|
-
}
|
|
503
|
-
},
|
|
504
|
-
external: (sub) => {
|
|
505
|
-
try {
|
|
506
|
-
sub.onParticipantError(source, error);
|
|
507
|
-
} catch (error2) {
|
|
508
|
-
if (this.showLogs) {
|
|
509
|
-
console.warn(
|
|
510
|
-
`[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
|
|
511
|
-
${JSON.stringify({ error: error2 }, null, 2)}`
|
|
512
|
-
);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
});
|
|
467
|
+
getParticipant(id) {
|
|
468
|
+
return this.participants.get(id);
|
|
517
469
|
}
|
|
518
|
-
|
|
519
|
-
return this.
|
|
520
|
-
(sub) => sub !== source && (sub.getListeners().length === 0 || sub.getListeners().some((listener) => source instanceof listener))
|
|
521
|
-
);
|
|
470
|
+
getParticipants() {
|
|
471
|
+
return [...this.participants.values()];
|
|
522
472
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
// src/application/services/runtime.ts
|
|
476
|
+
var RuntimeService = class {
|
|
477
|
+
constructor(state, processor, inferenceRunner, functionCallRunner) {
|
|
478
|
+
this.state = state;
|
|
479
|
+
this.processor = processor;
|
|
480
|
+
this.inferenceRunner = inferenceRunner;
|
|
481
|
+
this.functionCallRunner = functionCallRunner;
|
|
482
|
+
}
|
|
483
|
+
join(participant) {
|
|
484
|
+
this.state.addParticipant(participant);
|
|
485
|
+
this.publish(ParticipantJoinedEvent.init(participant.getManifest()));
|
|
486
|
+
}
|
|
487
|
+
leave(participant) {
|
|
488
|
+
this.state.removeParticipant(participant);
|
|
489
|
+
this.publish(ParticipantLeftEvent.init(participant.getManifest()));
|
|
490
|
+
}
|
|
491
|
+
publish(event) {
|
|
492
|
+
for (const participant of this.state.getParticipants()) {
|
|
493
|
+
this.processor.process(event, participant);
|
|
539
494
|
}
|
|
540
495
|
}
|
|
541
|
-
|
|
542
|
-
if (
|
|
543
|
-
|
|
496
|
+
getParticipant(id) {
|
|
497
|
+
if (!this.state.getParticipant(id)) {
|
|
498
|
+
throw new Error(`Participant ${id} not found`);
|
|
544
499
|
}
|
|
545
|
-
|
|
546
|
-
message: error.message,
|
|
547
|
-
stack: error.stack,
|
|
548
|
-
enviornment: this,
|
|
549
|
-
source: subscriber
|
|
550
|
-
});
|
|
551
|
-
return this.deliverError(subscriber, agenticError);
|
|
500
|
+
return this.state.getParticipant(id);
|
|
552
501
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
// src/domain/model-context/semantic-event/semantic-event.ts
|
|
556
|
-
var SemanticEvent = class {
|
|
557
|
-
constructor(type, data) {
|
|
558
|
-
this.type = type;
|
|
559
|
-
this.data = data;
|
|
502
|
+
getInferenceRunner() {
|
|
503
|
+
return this.inferenceRunner;
|
|
560
504
|
}
|
|
561
|
-
|
|
562
|
-
return this.
|
|
505
|
+
getFunctionCallRunner() {
|
|
506
|
+
return this.functionCallRunner;
|
|
563
507
|
}
|
|
564
508
|
};
|
|
565
509
|
|
|
566
|
-
// src/domain/agentic-environment/
|
|
567
|
-
var
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
510
|
+
// src/domain/agentic-environment/semantic-event/event-processor.ts
|
|
511
|
+
var EventProcessor = class {
|
|
512
|
+
process(event, consumer) {
|
|
513
|
+
for (const handler of consumer.getHandlers()) {
|
|
514
|
+
const isSatisfied = handler.specification.isSatisfiedBy({
|
|
515
|
+
event,
|
|
516
|
+
participant: consumer
|
|
517
|
+
});
|
|
518
|
+
if (isSatisfied) {
|
|
519
|
+
handler.processor.apply({ event, participant: consumer });
|
|
520
|
+
}
|
|
521
|
+
}
|
|
571
522
|
}
|
|
572
523
|
};
|
|
573
524
|
|
|
574
|
-
// src/application/
|
|
575
|
-
|
|
576
|
-
|
|
525
|
+
// src/application/use-cases/join.ts
|
|
526
|
+
function createJoin(resolveRuntime) {
|
|
527
|
+
return function join(participant) {
|
|
528
|
+
resolveRuntime().join(participant);
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// src/application/use-cases/leave.ts
|
|
533
|
+
function createLeave(resolveRuntime) {
|
|
534
|
+
return function leave(participant) {
|
|
535
|
+
resolveRuntime().leave(participant);
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// src/application/use-cases/send-message.ts
|
|
540
|
+
function createSendMessage(resolveRuntime) {
|
|
541
|
+
return function sendMessage(message, senderId) {
|
|
542
|
+
const runtime = resolveRuntime();
|
|
543
|
+
const participant = runtime.getParticipant(senderId);
|
|
544
|
+
if (!participant) {
|
|
545
|
+
throw new Error(`Participant ${senderId} not found`);
|
|
546
|
+
}
|
|
547
|
+
const userMessage = MessageSentEvent.init(senderId, message);
|
|
548
|
+
runtime.publish(userMessage);
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// src/domain/agentic-environment/loop/agent-loop.ts
|
|
553
|
+
var AgentLoop = class _AgentLoop {
|
|
554
|
+
constructor(loopId, stateExecutor, transitionResolver, interceptionHandler) {
|
|
555
|
+
this.loopId = loopId;
|
|
556
|
+
this.stateExecutor = stateExecutor;
|
|
557
|
+
this.transitionResolver = transitionResolver;
|
|
558
|
+
this.interceptionHandler = interceptionHandler;
|
|
559
|
+
}
|
|
560
|
+
async run(message, loopVisitor) {
|
|
577
561
|
var _a;
|
|
578
|
-
|
|
579
|
-
|
|
562
|
+
let transition = {
|
|
563
|
+
nextStateId: "context_update",
|
|
564
|
+
input: message
|
|
565
|
+
};
|
|
566
|
+
while (transition.nextStateId !== "idle") {
|
|
567
|
+
const isInterceptionSatisfied = (_a = this.interceptionHandler) == null ? void 0 : _a.isSatisfiedBy(transition);
|
|
568
|
+
if (isInterceptionSatisfied && this.interceptionHandler) {
|
|
569
|
+
loopVisitor.visitInterceptionStarted(transition);
|
|
570
|
+
transition = await this.interceptionHandler.handle(transition);
|
|
571
|
+
loopVisitor.visitInterceptionFinished(transition);
|
|
572
|
+
} else {
|
|
573
|
+
const execution = await this.stateExecutor.execute(transition, loopVisitor);
|
|
574
|
+
transition = this.transitionResolver.resolve(execution);
|
|
575
|
+
}
|
|
580
576
|
}
|
|
581
|
-
|
|
577
|
+
}
|
|
578
|
+
getLoopId() {
|
|
579
|
+
return this.loopId;
|
|
580
|
+
}
|
|
581
|
+
static create(stateExecutor, transitionResolver, interceptionHandler) {
|
|
582
|
+
const loopId = crypto.randomUUID();
|
|
583
|
+
return new _AgentLoop(loopId, stateExecutor, transitionResolver, interceptionHandler);
|
|
582
584
|
}
|
|
583
585
|
};
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
586
|
+
|
|
587
|
+
// src/domain/agentic-environment/loop/state-executor.ts
|
|
588
|
+
var LoopStateExecutor = class {
|
|
589
|
+
constructor(contextUpdateState, inferenceState, inferenceStreamingState, functionCallState, modelMessageState) {
|
|
590
|
+
this.contextUpdateState = contextUpdateState;
|
|
591
|
+
this.inferenceState = inferenceState;
|
|
592
|
+
this.inferenceStreamingState = inferenceStreamingState;
|
|
593
|
+
this.functionCallState = functionCallState;
|
|
594
|
+
this.modelMessageState = modelMessageState;
|
|
595
|
+
}
|
|
596
|
+
async execute(transition, loopVisitor) {
|
|
597
|
+
switch (transition.nextStateId) {
|
|
598
|
+
case "context_update":
|
|
599
|
+
return await this.contextUpdateState.run(transition.input, loopVisitor);
|
|
600
|
+
case "inference":
|
|
601
|
+
return await this.inferenceState.run(transition.input, loopVisitor);
|
|
602
|
+
case "inference_streaming":
|
|
603
|
+
return await this.inferenceStreamingState.run(transition.input, loopVisitor);
|
|
604
|
+
case "function_call":
|
|
605
|
+
return await this.functionCallState.run(transition.input, loopVisitor);
|
|
606
|
+
case "model_message":
|
|
607
|
+
return await this.modelMessageState.run(transition.input, loopVisitor);
|
|
596
608
|
}
|
|
597
609
|
}
|
|
598
610
|
};
|
|
599
611
|
|
|
600
|
-
// src/application/
|
|
601
|
-
var
|
|
602
|
-
constructor(
|
|
603
|
-
this.
|
|
604
|
-
this.
|
|
612
|
+
// src/application/states/function-call.ts
|
|
613
|
+
var FunctionCallState = class {
|
|
614
|
+
constructor(functionCallRunner) {
|
|
615
|
+
this.functionCallRunner = functionCallRunner;
|
|
616
|
+
this.id = "function_call";
|
|
605
617
|
}
|
|
606
|
-
async
|
|
618
|
+
async run(input, loopVisitor) {
|
|
607
619
|
var _a;
|
|
608
|
-
|
|
609
|
-
const
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
break;
|
|
620
|
-
}
|
|
621
|
-
if (item instanceof ReasoningItem) {
|
|
622
|
-
environment.deliverReasoning(caller, item);
|
|
623
|
-
} else if (item instanceof FunctionCallItem) {
|
|
624
|
-
environment.deliverFunctionCall(caller, item);
|
|
625
|
-
} else if (item instanceof ModelMessageItem) {
|
|
626
|
-
environment.deliverModelMessage(caller, item);
|
|
627
|
-
} else if (item instanceof SemanticEvent) {
|
|
628
|
-
environment.deliverSemanticEvent(caller, item);
|
|
629
|
-
}
|
|
620
|
+
loopVisitor.visitFunctionCallStarted(input);
|
|
621
|
+
const { call, inferenceInput } = input;
|
|
622
|
+
let tool = (_a = inferenceInput.tools) == null ? void 0 : _a.find((tool2) => tool2.name === call.name);
|
|
623
|
+
if (!tool) {
|
|
624
|
+
return {
|
|
625
|
+
stateId: this.id,
|
|
626
|
+
input,
|
|
627
|
+
output: {
|
|
628
|
+
item: FunctionCallOutputItem.create(call.callId, `Error: unknown tool "${call.name}"`)
|
|
629
|
+
}
|
|
630
|
+
};
|
|
630
631
|
}
|
|
632
|
+
const item = await this.functionCallRunner.run(call, tool);
|
|
633
|
+
loopVisitor.visitFunctionCallCompleted(item);
|
|
634
|
+
return {
|
|
635
|
+
stateId: this.id,
|
|
636
|
+
input,
|
|
637
|
+
output: {
|
|
638
|
+
item
|
|
639
|
+
}
|
|
640
|
+
};
|
|
631
641
|
}
|
|
632
642
|
};
|
|
633
643
|
|
|
634
|
-
// src/application/
|
|
635
|
-
var
|
|
636
|
-
constructor(
|
|
637
|
-
this.
|
|
644
|
+
// src/application/states/context-update.ts
|
|
645
|
+
var ContextUpdateState = class {
|
|
646
|
+
constructor() {
|
|
647
|
+
this.id = "context_update";
|
|
648
|
+
}
|
|
649
|
+
async run(input, loopVisitor) {
|
|
650
|
+
loopVisitor.visitContextUpdateStarted(input);
|
|
651
|
+
const userMessage = UserMessageItem.create(input.content);
|
|
652
|
+
const output = {
|
|
653
|
+
...input.input,
|
|
654
|
+
context: input.input.context.addContextItems([userMessage])
|
|
655
|
+
};
|
|
656
|
+
loopVisitor.visitContextUpdateCompleted(output);
|
|
657
|
+
return {
|
|
658
|
+
stateId: this.id,
|
|
659
|
+
input,
|
|
660
|
+
output
|
|
661
|
+
};
|
|
638
662
|
}
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
// src/application/states/model-message.ts
|
|
666
|
+
var ModelMessageState = class {
|
|
667
|
+
constructor() {
|
|
668
|
+
this.id = "model_message";
|
|
669
|
+
}
|
|
670
|
+
async run(input, loopVisitor) {
|
|
671
|
+
loopVisitor.visitModelAnswer(input);
|
|
672
|
+
return {
|
|
673
|
+
stateId: this.id,
|
|
674
|
+
input,
|
|
675
|
+
output: void 0
|
|
676
|
+
};
|
|
644
677
|
}
|
|
645
678
|
};
|
|
646
679
|
|
|
647
|
-
// src/
|
|
648
|
-
var
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
680
|
+
// src/domain/agentic-environment/loop/transition-resolver.ts
|
|
681
|
+
var TransitionResolver = class {
|
|
682
|
+
constructor(rules) {
|
|
683
|
+
this.rules = rules;
|
|
684
|
+
}
|
|
685
|
+
resolve(execution) {
|
|
686
|
+
const rule = this.rules.find((rule2) => rule2.specification.isSatisfiedBy(execution));
|
|
687
|
+
if (!rule) {
|
|
688
|
+
throw new Error(`No transition rule found after "${execution.stateId}"`);
|
|
652
689
|
}
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
690
|
+
return rule.createTransition(execution);
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
// src/domain/agentic-environment/loop/transition-specification.ts
|
|
695
|
+
var MessageReceivedSpecification = class {
|
|
696
|
+
isSatisfiedBy(execution) {
|
|
697
|
+
return execution.stateId === "context_update";
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
function isInferenceExecution(execution) {
|
|
701
|
+
return execution.stateId === "inference" || execution.stateId === "inference_streaming";
|
|
702
|
+
}
|
|
703
|
+
var InferenceProducedFunctionCallSpecification = class {
|
|
704
|
+
isSatisfiedBy(execution) {
|
|
705
|
+
return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "function_call");
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
var InferenceProducedModelMessageSpecification = class {
|
|
709
|
+
isSatisfiedBy(execution) {
|
|
710
|
+
return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
var FunctionCallCompletedSpecification = class {
|
|
714
|
+
isSatisfiedBy(execution) {
|
|
715
|
+
return execution.stateId === "function_call";
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
var ModelMessageCompletedSpecification = class {
|
|
719
|
+
isSatisfiedBy(execution) {
|
|
720
|
+
return execution.stateId === "model_message";
|
|
656
721
|
}
|
|
657
722
|
};
|
|
658
723
|
|
|
659
|
-
// src/
|
|
660
|
-
var
|
|
661
|
-
|
|
724
|
+
// src/domain/agentic-environment/loop/transition-rule.ts
|
|
725
|
+
var ContextPreparationToInferenceRule = class {
|
|
726
|
+
constructor() {
|
|
727
|
+
this.specification = new MessageReceivedSpecification();
|
|
662
728
|
}
|
|
663
|
-
|
|
729
|
+
createTransition(execution) {
|
|
730
|
+
if (execution.stateId !== "context_update") {
|
|
731
|
+
throw new Error("Expected context_preparation execution");
|
|
732
|
+
}
|
|
733
|
+
const nextStateId = execution.output.streaming ? "inference_streaming" : "inference";
|
|
734
|
+
return {
|
|
735
|
+
nextStateId,
|
|
736
|
+
input: execution.output
|
|
737
|
+
};
|
|
664
738
|
}
|
|
665
|
-
|
|
739
|
+
};
|
|
740
|
+
var InferenceToFunctionCallRule = class {
|
|
741
|
+
constructor() {
|
|
742
|
+
this.specification = new InferenceProducedFunctionCallSpecification();
|
|
666
743
|
}
|
|
667
|
-
|
|
744
|
+
createTransition(execution) {
|
|
745
|
+
if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "function_call")) {
|
|
746
|
+
throw new Error("Expected inference function-call output");
|
|
747
|
+
}
|
|
748
|
+
const functionCallItem = execution.output.items.find((item) => item.type === "function_call");
|
|
749
|
+
if (!functionCallItem) {
|
|
750
|
+
throw new Error("Expected inference function-call output");
|
|
751
|
+
}
|
|
752
|
+
return {
|
|
753
|
+
nextStateId: "function_call",
|
|
754
|
+
input: {
|
|
755
|
+
call: functionCallItem,
|
|
756
|
+
inferenceInput: execution.input
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
var InferenceToModelMessageRule = class {
|
|
762
|
+
constructor() {
|
|
763
|
+
this.specification = new InferenceProducedModelMessageSpecification();
|
|
764
|
+
}
|
|
765
|
+
createTransition(execution) {
|
|
766
|
+
if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
|
|
767
|
+
throw new Error("Expected inference model-message output");
|
|
768
|
+
}
|
|
769
|
+
const modelMessageItem = execution.output.items.find(
|
|
770
|
+
(item) => item.type === "message" && item.role === "assistant"
|
|
771
|
+
);
|
|
772
|
+
if (!modelMessageItem) {
|
|
773
|
+
throw new Error("Expected inference model-message output");
|
|
774
|
+
}
|
|
775
|
+
return {
|
|
776
|
+
nextStateId: "model_message",
|
|
777
|
+
input: {
|
|
778
|
+
answer: modelMessageItem
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
var FunctionCallToInferenceRule = class {
|
|
784
|
+
constructor() {
|
|
785
|
+
this.specification = new FunctionCallCompletedSpecification();
|
|
786
|
+
}
|
|
787
|
+
createTransition(execution) {
|
|
788
|
+
if (execution.stateId !== "function_call") {
|
|
789
|
+
throw new Error("Expected function_call execution");
|
|
790
|
+
}
|
|
791
|
+
const previousRequest = execution.input.inferenceInput;
|
|
792
|
+
previousRequest.context.addContextItems([execution.input.call, execution.output.item]);
|
|
793
|
+
return {
|
|
794
|
+
nextStateId: previousRequest.streaming ? "inference_streaming" : "inference",
|
|
795
|
+
input: {
|
|
796
|
+
...previousRequest
|
|
797
|
+
}
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
var ModelMessageToIdleRule = class {
|
|
802
|
+
constructor() {
|
|
803
|
+
this.specification = new ModelMessageCompletedSpecification();
|
|
668
804
|
}
|
|
669
|
-
|
|
805
|
+
createTransition(execution) {
|
|
806
|
+
if (execution.stateId !== "model_message") {
|
|
807
|
+
throw new Error("Expected model_message execution");
|
|
808
|
+
}
|
|
809
|
+
return {
|
|
810
|
+
nextStateId: "idle",
|
|
811
|
+
input: void 0
|
|
812
|
+
};
|
|
670
813
|
}
|
|
671
|
-
|
|
814
|
+
};
|
|
815
|
+
|
|
816
|
+
// src/domain/agentic-environment/loop/event-publisher-visitor.ts
|
|
817
|
+
import { createCloudClient } from "@mozaik-ai/cloud-sdk";
|
|
818
|
+
var EventPublisherLoopVisitor = class {
|
|
819
|
+
constructor(agentId, loopId, runtime) {
|
|
820
|
+
this.agentId = agentId;
|
|
821
|
+
this.loopId = loopId;
|
|
822
|
+
this.runtime = runtime;
|
|
823
|
+
this.cloud = createCloudClient();
|
|
672
824
|
}
|
|
673
|
-
|
|
825
|
+
visitContextUpdateStarted(input) {
|
|
826
|
+
this.publish("context_update.started", { ...input, loopId: this.loopId });
|
|
674
827
|
}
|
|
675
|
-
|
|
828
|
+
visitContextUpdateCompleted(output) {
|
|
829
|
+
this.publish("context_update.completed", { ...output, loopId: this.loopId });
|
|
676
830
|
}
|
|
677
|
-
|
|
831
|
+
visitInferenceStarted(input) {
|
|
832
|
+
this.publish("inference.started", input);
|
|
678
833
|
}
|
|
679
|
-
|
|
834
|
+
visitInferenceEvent(event) {
|
|
835
|
+
this.publish("inference.stream", event);
|
|
680
836
|
}
|
|
681
|
-
|
|
837
|
+
visitInferenceCompleted(output) {
|
|
838
|
+
this.publish("inference.completed", output);
|
|
682
839
|
}
|
|
683
|
-
|
|
840
|
+
visitFunctionCallStarted(input) {
|
|
841
|
+
this.publish("function_call.started", input);
|
|
684
842
|
}
|
|
685
|
-
|
|
843
|
+
visitFunctionCallCompleted(output) {
|
|
844
|
+
this.publish("function_call.completed", output);
|
|
686
845
|
}
|
|
687
|
-
|
|
846
|
+
visitModelAnswer(input) {
|
|
847
|
+
this.publish("model.answer", input);
|
|
688
848
|
}
|
|
689
|
-
|
|
849
|
+
visitInterceptionStarted(transition) {
|
|
850
|
+
this.publish("interception.started", transition);
|
|
690
851
|
}
|
|
691
|
-
|
|
852
|
+
visitInterceptionFinished(transition) {
|
|
853
|
+
this.publish("interception.finished", transition);
|
|
692
854
|
}
|
|
693
|
-
|
|
855
|
+
publish(type, payload) {
|
|
856
|
+
const event = new SemanticEvent(type, this.agentId, /* @__PURE__ */ new Date(), payload);
|
|
857
|
+
this.runtime.publish(event);
|
|
858
|
+
if (this.cloud.enabled) {
|
|
859
|
+
this.cloud.send(event);
|
|
860
|
+
}
|
|
694
861
|
}
|
|
695
862
|
};
|
|
696
863
|
|
|
697
|
-
// src/application/
|
|
698
|
-
var
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
864
|
+
// src/application/states/inference-streaming.ts
|
|
865
|
+
var InferenceStreamingState = class {
|
|
866
|
+
constructor(inferenceRunner) {
|
|
867
|
+
this.inferenceRunner = inferenceRunner;
|
|
868
|
+
this.id = "inference_streaming";
|
|
869
|
+
}
|
|
870
|
+
async run(input, loopVisitor) {
|
|
871
|
+
loopVisitor.visitInferenceStarted(input);
|
|
872
|
+
let output = void 0;
|
|
873
|
+
for await (const event of this.inferenceRunner.stream(input)) {
|
|
874
|
+
loopVisitor.visitInferenceEvent(event);
|
|
875
|
+
if (event.type === "inference.output") {
|
|
876
|
+
output = event.payload;
|
|
877
|
+
}
|
|
702
878
|
}
|
|
703
|
-
|
|
879
|
+
if (!output) {
|
|
880
|
+
throw new Error("Inference output not found");
|
|
881
|
+
}
|
|
882
|
+
loopVisitor.visitInferenceCompleted(output);
|
|
883
|
+
return {
|
|
884
|
+
stateId: "inference_streaming",
|
|
885
|
+
input,
|
|
886
|
+
output
|
|
887
|
+
};
|
|
704
888
|
}
|
|
705
889
|
};
|
|
706
890
|
|
|
891
|
+
// src/application/use-cases/run-loop.ts
|
|
892
|
+
function createRunLoop(resolveRuntime) {
|
|
893
|
+
return function runLoop(agentId, message, inferenceInput, interceptionHandler) {
|
|
894
|
+
const runtime = resolveRuntime();
|
|
895
|
+
const inferenceRunner = runtime.getInferenceRunner();
|
|
896
|
+
const functionCallRunner = runtime.getFunctionCallRunner();
|
|
897
|
+
const transitionResolver = new TransitionResolver([
|
|
898
|
+
// High-priority interception rules would go first.
|
|
899
|
+
new ContextPreparationToInferenceRule(),
|
|
900
|
+
new InferenceToFunctionCallRule(),
|
|
901
|
+
new InferenceToModelMessageRule(),
|
|
902
|
+
new FunctionCallToInferenceRule(),
|
|
903
|
+
new ModelMessageToIdleRule()
|
|
904
|
+
]);
|
|
905
|
+
const stateExecutor = new LoopStateExecutor(
|
|
906
|
+
new ContextUpdateState(),
|
|
907
|
+
new InferenceState(inferenceRunner),
|
|
908
|
+
new InferenceStreamingState(inferenceRunner),
|
|
909
|
+
new FunctionCallState(functionCallRunner),
|
|
910
|
+
new ModelMessageState()
|
|
911
|
+
);
|
|
912
|
+
const agentLoop = AgentLoop.create(stateExecutor, transitionResolver, interceptionHandler);
|
|
913
|
+
const loopVisitor = new EventPublisherLoopVisitor(agentId, agentLoop.getLoopId(), runtime);
|
|
914
|
+
agentLoop.run(
|
|
915
|
+
{
|
|
916
|
+
content: message,
|
|
917
|
+
input: inferenceInput
|
|
918
|
+
},
|
|
919
|
+
loopVisitor
|
|
920
|
+
);
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
|
|
707
924
|
// src/infrastructure/providers/anthropic/endpoints/anthropic-messages-mapper.ts
|
|
708
925
|
var AnthropicMessagesMapper = class {
|
|
709
|
-
toRequest(
|
|
710
|
-
const { messages, system } = this.mapContextItems(
|
|
926
|
+
toRequest(inferenceInput) {
|
|
927
|
+
const { messages, system } = this.mapContextItems(inferenceInput);
|
|
711
928
|
const outputConfig = {};
|
|
712
929
|
const request = {
|
|
713
|
-
model:
|
|
930
|
+
model: inferenceInput.model,
|
|
714
931
|
messages
|
|
715
932
|
};
|
|
716
|
-
request.max_tokens =
|
|
933
|
+
request.max_tokens = inferenceInput.maxOutputTokens;
|
|
717
934
|
if (system) {
|
|
718
935
|
request.system = system;
|
|
719
936
|
}
|
|
720
|
-
if (
|
|
721
|
-
request.tools =
|
|
937
|
+
if (inferenceInput.tools && inferenceInput.tools.length > 0) {
|
|
938
|
+
request.tools = inferenceInput.tools.map((tool) => ({
|
|
722
939
|
name: tool.name,
|
|
723
940
|
description: tool.description,
|
|
724
941
|
input_schema: tool.parameters
|
|
725
942
|
}));
|
|
726
943
|
}
|
|
727
|
-
if (
|
|
944
|
+
if (inferenceInput.structuredOutput) {
|
|
728
945
|
outputConfig.format = {
|
|
729
946
|
type: "json_schema",
|
|
730
|
-
json_schema:
|
|
947
|
+
json_schema: inferenceInput.structuredOutput.schema
|
|
731
948
|
};
|
|
732
949
|
}
|
|
733
|
-
if (
|
|
950
|
+
if (inferenceInput.reasoningEffort) {
|
|
734
951
|
request.thinking = { type: "adaptive" };
|
|
735
|
-
outputConfig.effort =
|
|
952
|
+
outputConfig.effort = inferenceInput.reasoningEffort;
|
|
736
953
|
}
|
|
737
954
|
if (Object.keys(outputConfig).length > 0) {
|
|
738
955
|
request.output_config = outputConfig;
|
|
739
956
|
}
|
|
740
|
-
if (
|
|
741
|
-
request.stream =
|
|
957
|
+
if (inferenceInput.streaming) {
|
|
958
|
+
request.stream = inferenceInput.streaming;
|
|
742
959
|
}
|
|
743
960
|
return request;
|
|
744
961
|
}
|
|
745
|
-
toResponse(response) {
|
|
746
|
-
const contextItems = this.extractContextItems(response);
|
|
747
|
-
const tokenUsage = this.extractTokenUsage(response);
|
|
748
|
-
return new InferenceResponse(contextItems, tokenUsage);
|
|
749
|
-
}
|
|
750
962
|
addContentBlock(messages, role, block) {
|
|
751
963
|
const lastMessage = messages[messages.length - 1];
|
|
752
964
|
if ((lastMessage == null ? void 0 : lastMessage.role) === role) {
|
|
@@ -758,9 +970,9 @@ var AnthropicMessagesMapper = class {
|
|
|
758
970
|
content: [block]
|
|
759
971
|
});
|
|
760
972
|
}
|
|
761
|
-
mapContextItems(
|
|
973
|
+
mapContextItems(inferenceInput) {
|
|
762
974
|
var _a, _b, _c;
|
|
763
|
-
const context =
|
|
975
|
+
const context = inferenceInput.context;
|
|
764
976
|
const messages = [];
|
|
765
977
|
const system = [];
|
|
766
978
|
for (const item of context.getItems()) {
|
|
@@ -827,7 +1039,7 @@ var AnthropicMessagesMapper = class {
|
|
|
827
1039
|
new OutputTokenDetails(0)
|
|
828
1040
|
);
|
|
829
1041
|
}
|
|
830
|
-
|
|
1042
|
+
toResponse(response) {
|
|
831
1043
|
var _a;
|
|
832
1044
|
const items = [];
|
|
833
1045
|
for (const block of response.content) {
|
|
@@ -855,31 +1067,42 @@ var AnthropicMessagesMapper = class {
|
|
|
855
1067
|
);
|
|
856
1068
|
}
|
|
857
1069
|
}
|
|
858
|
-
return items;
|
|
1070
|
+
return { items, rowResponse: response };
|
|
859
1071
|
}
|
|
860
1072
|
};
|
|
861
1073
|
|
|
862
1074
|
// src/infrastructure/providers/anthropic/endpoints/anthropic-messages.ts
|
|
863
1075
|
import Anthropic from "@anthropic-ai/sdk";
|
|
864
1076
|
var AnthropicMessages = class {
|
|
865
|
-
constructor(endpointMapper = new AnthropicMessagesMapper()) {
|
|
1077
|
+
constructor(endpointMapper = new AnthropicMessagesMapper(), config = {}) {
|
|
866
1078
|
this.endpointMapper = endpointMapper;
|
|
867
|
-
this.
|
|
1079
|
+
this.clientConfig = config;
|
|
868
1080
|
}
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
1081
|
+
get client() {
|
|
1082
|
+
var _a;
|
|
1083
|
+
return (_a = this._client) != null ? _a : this._client = new Anthropic({
|
|
1084
|
+
baseURL: this.clientConfig.baseURL,
|
|
1085
|
+
apiKey: this.clientConfig.apiKey
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
async infer(inferenceInput) {
|
|
1089
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1090
|
+
const response = await this.client.messages.create(request);
|
|
872
1091
|
return this.endpointMapper.toResponse(response);
|
|
873
1092
|
}
|
|
874
|
-
async *stream(
|
|
875
|
-
const
|
|
876
|
-
const stream =
|
|
1093
|
+
async *stream(inferenceInput) {
|
|
1094
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1095
|
+
const stream = this.client.messages.stream(request);
|
|
877
1096
|
for await (const event of stream) {
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
1097
|
+
yield event;
|
|
1098
|
+
}
|
|
1099
|
+
const output = this.endpointMapper.toResponse(await stream.finalMessage());
|
|
1100
|
+
yield {
|
|
1101
|
+
type: "inference.output",
|
|
1102
|
+
producerId: request.model,
|
|
1103
|
+
occurredAt: /* @__PURE__ */ new Date(),
|
|
1104
|
+
payload: output
|
|
1105
|
+
};
|
|
883
1106
|
}
|
|
884
1107
|
};
|
|
885
1108
|
|
|
@@ -900,27 +1123,27 @@ var SummaryText = class _SummaryText extends ItemContent {
|
|
|
900
1123
|
|
|
901
1124
|
// src/infrastructure/providers/openai/endpoints/openai-responses-mapper.ts
|
|
902
1125
|
var OpenAIResponsesMapper = class {
|
|
903
|
-
toRequest(
|
|
1126
|
+
toRequest(inferenceInput) {
|
|
904
1127
|
var _a, _b;
|
|
905
1128
|
const request = {
|
|
906
|
-
model:
|
|
907
|
-
input: this.mapContextItems(
|
|
1129
|
+
model: inferenceInput.model,
|
|
1130
|
+
input: this.mapContextItems(inferenceInput)
|
|
908
1131
|
};
|
|
909
|
-
if (
|
|
910
|
-
request.tools =
|
|
1132
|
+
if (inferenceInput.tools && inferenceInput.tools.length > 0) {
|
|
1133
|
+
request.tools = inferenceInput.tools.map((tool) => ({
|
|
911
1134
|
type: tool.type,
|
|
912
1135
|
name: tool.name,
|
|
913
1136
|
description: tool.description,
|
|
914
1137
|
parameters: tool.parameters
|
|
915
1138
|
}));
|
|
916
1139
|
}
|
|
917
|
-
if (
|
|
1140
|
+
if (inferenceInput.reasoningEffort) {
|
|
918
1141
|
request.reasoning = {
|
|
919
|
-
effort:
|
|
1142
|
+
effort: inferenceInput.reasoningEffort
|
|
920
1143
|
};
|
|
921
1144
|
}
|
|
922
|
-
if (
|
|
923
|
-
const format =
|
|
1145
|
+
if (inferenceInput.structuredOutput) {
|
|
1146
|
+
const format = inferenceInput.structuredOutput;
|
|
924
1147
|
request.text = {
|
|
925
1148
|
format: {
|
|
926
1149
|
type: "json_schema",
|
|
@@ -930,19 +1153,14 @@ var OpenAIResponsesMapper = class {
|
|
|
930
1153
|
}
|
|
931
1154
|
};
|
|
932
1155
|
}
|
|
933
|
-
if (
|
|
934
|
-
request.stream =
|
|
1156
|
+
if (inferenceInput.streaming) {
|
|
1157
|
+
request.stream = inferenceInput.streaming;
|
|
935
1158
|
}
|
|
936
1159
|
return request;
|
|
937
1160
|
}
|
|
938
|
-
|
|
939
|
-
const contextItems = this.extractContextItems(response);
|
|
940
|
-
const tokenUsage = this.extractTokenUsage(response);
|
|
941
|
-
return new InferenceResponse(contextItems, tokenUsage);
|
|
942
|
-
}
|
|
943
|
-
mapContextItems(inferenceParams) {
|
|
1161
|
+
mapContextItems(inferenceInput) {
|
|
944
1162
|
const input = [];
|
|
945
|
-
for (const item of
|
|
1163
|
+
for (const item of inferenceInput.context.getItems()) {
|
|
946
1164
|
if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem || item instanceof UserMessageItem) {
|
|
947
1165
|
input.push({
|
|
948
1166
|
type: item.type,
|
|
@@ -1002,7 +1220,7 @@ var OpenAIResponsesMapper = class {
|
|
|
1002
1220
|
new OutputTokenDetails((_d = (_c = response.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
|
|
1003
1221
|
);
|
|
1004
1222
|
}
|
|
1005
|
-
|
|
1223
|
+
toResponse(response) {
|
|
1006
1224
|
var _a, _b, _c;
|
|
1007
1225
|
const items = [];
|
|
1008
1226
|
for (const item of (_a = response.output) != null ? _a : []) {
|
|
@@ -1035,7 +1253,10 @@ var OpenAIResponsesMapper = class {
|
|
|
1035
1253
|
);
|
|
1036
1254
|
}
|
|
1037
1255
|
}
|
|
1038
|
-
return
|
|
1256
|
+
return {
|
|
1257
|
+
items,
|
|
1258
|
+
rowResponse: response
|
|
1259
|
+
};
|
|
1039
1260
|
}
|
|
1040
1261
|
};
|
|
1041
1262
|
|
|
@@ -1043,22 +1264,36 @@ var OpenAIResponsesMapper = class {
|
|
|
1043
1264
|
var OpenAIResponses = class {
|
|
1044
1265
|
constructor(endpointMapper = new OpenAIResponsesMapper()) {
|
|
1045
1266
|
this.endpointMapper = endpointMapper;
|
|
1046
|
-
this.client = new OpenAI();
|
|
1047
1267
|
}
|
|
1048
|
-
|
|
1049
|
-
|
|
1268
|
+
get client() {
|
|
1269
|
+
var _a;
|
|
1270
|
+
return (_a = this._client) != null ? _a : this._client = new OpenAI();
|
|
1271
|
+
}
|
|
1272
|
+
async infer(inferenceInput) {
|
|
1273
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1050
1274
|
const response = await this.client.responses.create(request);
|
|
1051
1275
|
return this.endpointMapper.toResponse(response);
|
|
1052
1276
|
}
|
|
1053
|
-
async *stream(
|
|
1054
|
-
const request = this.endpointMapper.toRequest(
|
|
1055
|
-
const
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1277
|
+
async *stream(inferenceInput) {
|
|
1278
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1279
|
+
const stream = await this.client.responses.create({ ...request, stream: true });
|
|
1280
|
+
let completedResponse = void 0;
|
|
1281
|
+
for await (const event of stream) {
|
|
1282
|
+
if (event.type === "response.completed") {
|
|
1283
|
+
completedResponse = event.response;
|
|
1059
1284
|
}
|
|
1060
|
-
yield
|
|
1285
|
+
yield event;
|
|
1286
|
+
}
|
|
1287
|
+
if (!completedResponse) {
|
|
1288
|
+
throw new Error("Stream ended without a completed response");
|
|
1061
1289
|
}
|
|
1290
|
+
const output = this.endpointMapper.toResponse(completedResponse);
|
|
1291
|
+
yield {
|
|
1292
|
+
type: "inference.output",
|
|
1293
|
+
payload: output,
|
|
1294
|
+
occurredAt: /* @__PURE__ */ new Date(),
|
|
1295
|
+
producerId: inferenceInput.model
|
|
1296
|
+
};
|
|
1062
1297
|
}
|
|
1063
1298
|
};
|
|
1064
1299
|
|
|
@@ -1067,16 +1302,16 @@ import { GoogleGenAI } from "@google/genai";
|
|
|
1067
1302
|
|
|
1068
1303
|
// src/infrastructure/providers/gemini/endpoints/gemini-generate-content-mapper.ts
|
|
1069
1304
|
var GeminiGenerateContentMapper = class {
|
|
1070
|
-
toRequest(
|
|
1071
|
-
const { contents, systemInstruction } = this.mapContextItems(
|
|
1305
|
+
toRequest(inferenceInput) {
|
|
1306
|
+
const { contents, systemInstruction } = this.mapContextItems(inferenceInput);
|
|
1072
1307
|
const config = {};
|
|
1073
1308
|
if (systemInstruction) {
|
|
1074
1309
|
config.systemInstruction = systemInstruction;
|
|
1075
1310
|
}
|
|
1076
|
-
if (
|
|
1311
|
+
if (inferenceInput.tools && inferenceInput.tools.length > 0) {
|
|
1077
1312
|
config.tools = [
|
|
1078
1313
|
{
|
|
1079
|
-
functionDeclarations:
|
|
1314
|
+
functionDeclarations: inferenceInput.tools.map((tool) => ({
|
|
1080
1315
|
name: tool.name,
|
|
1081
1316
|
description: tool.description,
|
|
1082
1317
|
parametersJsonSchema: tool.parameters
|
|
@@ -1084,34 +1319,29 @@ var GeminiGenerateContentMapper = class {
|
|
|
1084
1319
|
}
|
|
1085
1320
|
];
|
|
1086
1321
|
}
|
|
1087
|
-
if (
|
|
1322
|
+
if (inferenceInput.structuredOutput) {
|
|
1088
1323
|
config.responseMimeType = "application/json";
|
|
1089
|
-
config.responseSchema =
|
|
1324
|
+
config.responseSchema = inferenceInput.structuredOutput.schema;
|
|
1090
1325
|
}
|
|
1091
|
-
if (
|
|
1326
|
+
if (inferenceInput.reasoningEffort) {
|
|
1092
1327
|
config.thinkingConfig = {
|
|
1093
|
-
thinkingLevel:
|
|
1328
|
+
thinkingLevel: inferenceInput.reasoningEffort,
|
|
1094
1329
|
includeThoughts: true
|
|
1095
1330
|
};
|
|
1096
1331
|
}
|
|
1097
1332
|
const request = {
|
|
1098
|
-
model:
|
|
1333
|
+
model: inferenceInput.model,
|
|
1099
1334
|
contents,
|
|
1100
1335
|
config
|
|
1101
1336
|
};
|
|
1102
|
-
if (
|
|
1103
|
-
request.stream =
|
|
1337
|
+
if (inferenceInput.streaming) {
|
|
1338
|
+
request.stream = inferenceInput.streaming;
|
|
1104
1339
|
}
|
|
1105
1340
|
return request;
|
|
1106
1341
|
}
|
|
1107
|
-
|
|
1108
|
-
const contextItems = this.extractContextItems(response);
|
|
1109
|
-
const tokenUsage = this.extractTokenUsage(response);
|
|
1110
|
-
return new InferenceResponse(contextItems, tokenUsage);
|
|
1111
|
-
}
|
|
1112
|
-
mapContextItems(inferenceParams) {
|
|
1342
|
+
mapContextItems(inferenceInput) {
|
|
1113
1343
|
var _a;
|
|
1114
|
-
const context =
|
|
1344
|
+
const context = inferenceInput.context;
|
|
1115
1345
|
const contents = [];
|
|
1116
1346
|
const system = [];
|
|
1117
1347
|
const callNames = /* @__PURE__ */ new Map();
|
|
@@ -1184,7 +1414,7 @@ var GeminiGenerateContentMapper = class {
|
|
|
1184
1414
|
new OutputTokenDetails((_e = usage.thoughtsTokenCount) != null ? _e : 0)
|
|
1185
1415
|
);
|
|
1186
1416
|
}
|
|
1187
|
-
|
|
1417
|
+
toResponse(response) {
|
|
1188
1418
|
var _a, _b, _c, _d, _e, _f;
|
|
1189
1419
|
const items = [];
|
|
1190
1420
|
const parts = (_d = (_c = (_b = (_a = response.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts) != null ? _d : [];
|
|
@@ -1213,42 +1443,58 @@ var GeminiGenerateContentMapper = class {
|
|
|
1213
1443
|
);
|
|
1214
1444
|
}
|
|
1215
1445
|
}
|
|
1216
|
-
return items;
|
|
1446
|
+
return { items, rowResponse: response };
|
|
1217
1447
|
}
|
|
1218
1448
|
};
|
|
1219
1449
|
|
|
1220
1450
|
// src/infrastructure/providers/gemini/endpoints/gemini-generate-content.ts
|
|
1221
1451
|
var GeminiGenerateContent = class {
|
|
1222
|
-
constructor(endpointMapper = new GeminiGenerateContentMapper()) {
|
|
1452
|
+
constructor(endpointMapper = new GeminiGenerateContentMapper(), config = {}) {
|
|
1223
1453
|
this.endpointMapper = endpointMapper;
|
|
1224
|
-
this.
|
|
1454
|
+
this.clientConfig = config;
|
|
1455
|
+
}
|
|
1456
|
+
get client() {
|
|
1457
|
+
var _a, _b;
|
|
1458
|
+
return (_b = this._client) != null ? _b : this._client = new GoogleGenAI({
|
|
1459
|
+
apiKey: (_a = this.clientConfig.apiKey) != null ? _a : process.env.GEMINI_API_KEY,
|
|
1460
|
+
...this.clientConfig.baseURL ? { httpOptions: { baseUrl: this.clientConfig.baseURL } } : {}
|
|
1461
|
+
});
|
|
1225
1462
|
}
|
|
1226
|
-
async infer(
|
|
1227
|
-
const
|
|
1228
|
-
const response = await this.client.models.generateContent(
|
|
1463
|
+
async infer(inferenceInput) {
|
|
1464
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1465
|
+
const response = await this.client.models.generateContent(request);
|
|
1229
1466
|
return this.endpointMapper.toResponse(response);
|
|
1230
1467
|
}
|
|
1231
|
-
async *stream(
|
|
1232
|
-
const
|
|
1233
|
-
const stream = await this.client.models.generateContentStream(
|
|
1468
|
+
async *stream(inferenceInput) {
|
|
1469
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1470
|
+
const stream = await this.client.models.generateContentStream(request);
|
|
1471
|
+
let lastEvent = void 0;
|
|
1234
1472
|
for await (const chunk of stream) {
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1473
|
+
lastEvent = chunk;
|
|
1474
|
+
yield chunk;
|
|
1475
|
+
}
|
|
1476
|
+
if (!lastEvent) {
|
|
1477
|
+
throw new Error("Last event not found");
|
|
1478
|
+
}
|
|
1479
|
+
const output = this.endpointMapper.toResponse(lastEvent);
|
|
1480
|
+
yield {
|
|
1481
|
+
type: "inference.output",
|
|
1482
|
+
producerId: request.model,
|
|
1483
|
+
occurredAt: /* @__PURE__ */ new Date(),
|
|
1484
|
+
payload: output
|
|
1485
|
+
};
|
|
1240
1486
|
}
|
|
1241
1487
|
};
|
|
1242
1488
|
|
|
1243
1489
|
// src/infrastructure/providers/openai/endpoints/openai-chat-completions-mapper.ts
|
|
1244
1490
|
var OpenAIChatCompletionsMapper = class {
|
|
1245
|
-
toRequest(
|
|
1491
|
+
toRequest(inferenceInput) {
|
|
1246
1492
|
const request = {
|
|
1247
|
-
model:
|
|
1248
|
-
messages: this.mapContextItems(
|
|
1493
|
+
model: inferenceInput.model,
|
|
1494
|
+
messages: this.mapContextItems(inferenceInput)
|
|
1249
1495
|
};
|
|
1250
|
-
if (
|
|
1251
|
-
request.tools =
|
|
1496
|
+
if (inferenceInput.tools && inferenceInput.tools.length > 0) {
|
|
1497
|
+
request.tools = inferenceInput.tools.map((tool) => ({
|
|
1252
1498
|
type: "function",
|
|
1253
1499
|
function: {
|
|
1254
1500
|
name: tool.name,
|
|
@@ -1257,23 +1503,18 @@ var OpenAIChatCompletionsMapper = class {
|
|
|
1257
1503
|
}
|
|
1258
1504
|
}));
|
|
1259
1505
|
}
|
|
1260
|
-
if (
|
|
1261
|
-
request.reasoning_effort =
|
|
1506
|
+
if (inferenceInput.reasoningEffort) {
|
|
1507
|
+
request.reasoning_effort = inferenceInput.reasoningEffort;
|
|
1262
1508
|
}
|
|
1263
|
-
if (
|
|
1264
|
-
request.stream =
|
|
1509
|
+
if (inferenceInput.streaming) {
|
|
1510
|
+
request.stream = inferenceInput.streaming;
|
|
1265
1511
|
}
|
|
1266
1512
|
return request;
|
|
1267
1513
|
}
|
|
1268
|
-
|
|
1269
|
-
const contextItems = this.extractContextItems(response);
|
|
1270
|
-
const tokenUsage = this.extractTokenUsage(response);
|
|
1271
|
-
return new InferenceResponse(contextItems, tokenUsage);
|
|
1272
|
-
}
|
|
1273
|
-
mapContextItems(inferenceParams) {
|
|
1514
|
+
mapContextItems(inferenceInput) {
|
|
1274
1515
|
var _a;
|
|
1275
1516
|
const messages = [];
|
|
1276
|
-
for (const item of
|
|
1517
|
+
for (const item of inferenceInput.context.getItems()) {
|
|
1277
1518
|
if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem) {
|
|
1278
1519
|
messages.push({ role: "system", content: item.content.text });
|
|
1279
1520
|
continue;
|
|
@@ -1320,12 +1561,12 @@ var OpenAIChatCompletionsMapper = class {
|
|
|
1320
1561
|
new OutputTokenDetails((_d = (_c = response.usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
|
|
1321
1562
|
);
|
|
1322
1563
|
}
|
|
1323
|
-
|
|
1564
|
+
toResponse(response) {
|
|
1324
1565
|
var _a, _b, _c;
|
|
1325
1566
|
const items = [];
|
|
1326
1567
|
const message = (_b = (_a = response.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
|
|
1327
1568
|
if (!message) {
|
|
1328
|
-
return items;
|
|
1569
|
+
return { items: [], rowResponse: response };
|
|
1329
1570
|
}
|
|
1330
1571
|
if (message.reasoning_content) {
|
|
1331
1572
|
items.push(
|
|
@@ -1348,7 +1589,7 @@ var OpenAIChatCompletionsMapper = class {
|
|
|
1348
1589
|
})
|
|
1349
1590
|
);
|
|
1350
1591
|
}
|
|
1351
|
-
return items;
|
|
1592
|
+
return { items, rowResponse: response };
|
|
1352
1593
|
}
|
|
1353
1594
|
};
|
|
1354
1595
|
|
|
@@ -1358,31 +1599,38 @@ var OpenAIChatCompletions = class {
|
|
|
1358
1599
|
constructor(endpointMapper = new OpenAIChatCompletionsMapper(), config = {}) {
|
|
1359
1600
|
var _a;
|
|
1360
1601
|
this.endpointMapper = endpointMapper;
|
|
1361
|
-
this.
|
|
1362
|
-
baseURL: config.baseURL,
|
|
1363
|
-
apiKey: config.apiKey
|
|
1364
|
-
});
|
|
1602
|
+
this.clientConfig = config;
|
|
1365
1603
|
this.extraBody = (_a = config.extraBody) != null ? _a : {};
|
|
1366
1604
|
}
|
|
1367
|
-
|
|
1605
|
+
get client() {
|
|
1606
|
+
var _a;
|
|
1607
|
+
return (_a = this._client) != null ? _a : this._client = new OpenAI2({
|
|
1608
|
+
baseURL: this.clientConfig.baseURL,
|
|
1609
|
+
apiKey: this.clientConfig.apiKey
|
|
1610
|
+
});
|
|
1611
|
+
}
|
|
1612
|
+
buildRequest(inferenceInput) {
|
|
1368
1613
|
return {
|
|
1369
1614
|
...this.extraBody,
|
|
1370
|
-
...this.endpointMapper.toRequest(
|
|
1615
|
+
...this.endpointMapper.toRequest(inferenceInput)
|
|
1371
1616
|
};
|
|
1372
1617
|
}
|
|
1373
|
-
async infer(
|
|
1374
|
-
const response = await this.client.chat.completions.create(this.buildRequest(
|
|
1618
|
+
async infer(inferenceInput) {
|
|
1619
|
+
const response = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
|
|
1375
1620
|
return this.endpointMapper.toResponse(response);
|
|
1376
1621
|
}
|
|
1377
|
-
async *stream(
|
|
1378
|
-
|
|
1379
|
-
const
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1622
|
+
async *stream(inferenceInput) {
|
|
1623
|
+
const stream = this.client.chat.completions.stream(this.buildRequest(inferenceInput));
|
|
1624
|
+
for await (const chunk of stream) {
|
|
1625
|
+
yield chunk;
|
|
1626
|
+
}
|
|
1627
|
+
const output = this.endpointMapper.toResponse(await stream.finalChatCompletion());
|
|
1628
|
+
yield {
|
|
1629
|
+
type: "inference.output",
|
|
1630
|
+
payload: output,
|
|
1631
|
+
occurredAt: /* @__PURE__ */ new Date(),
|
|
1632
|
+
producerId: inferenceInput.model
|
|
1633
|
+
};
|
|
1386
1634
|
}
|
|
1387
1635
|
};
|
|
1388
1636
|
|
|
@@ -1650,103 +1898,71 @@ var claudeHaiku45Specification = {
|
|
|
1650
1898
|
supportsStructuredOutput: true
|
|
1651
1899
|
};
|
|
1652
1900
|
|
|
1653
|
-
// src/
|
|
1654
|
-
var
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
endpoint: new AnthropicMessages(),
|
|
1703
|
-
specification: claudeOpus48Specification
|
|
1704
|
-
};
|
|
1705
|
-
break;
|
|
1706
|
-
case "gemini-3.5-flash":
|
|
1707
|
-
generativeModel = {
|
|
1708
|
-
endpoint: new GeminiGenerateContent(),
|
|
1709
|
-
specification: gemini35FlashSpecification
|
|
1710
|
-
};
|
|
1711
|
-
break;
|
|
1712
|
-
case "gemini-3.1-pro-preview":
|
|
1713
|
-
generativeModel = {
|
|
1714
|
-
endpoint: new GeminiGenerateContent(),
|
|
1715
|
-
specification: gemini31ProSpecification
|
|
1716
|
-
};
|
|
1717
|
-
break;
|
|
1718
|
-
case "deepseek-v4-flash":
|
|
1719
|
-
generativeModel = {
|
|
1720
|
-
endpoint: new OpenAIChatCompletions(),
|
|
1721
|
-
specification: deepSeekV4FlashSpecification
|
|
1722
|
-
};
|
|
1723
|
-
break;
|
|
1724
|
-
case "deepseek-v4-pro":
|
|
1725
|
-
generativeModel = {
|
|
1726
|
-
endpoint: new OpenAIChatCompletions(),
|
|
1727
|
-
specification: deepSeekV4ProSpecification
|
|
1728
|
-
};
|
|
1729
|
-
}
|
|
1730
|
-
if (!generativeModel) {
|
|
1731
|
-
throw new Error(`Unsupported model: ${modelName}`);
|
|
1732
|
-
}
|
|
1733
|
-
return Promise.resolve(generativeModel);
|
|
1901
|
+
// src/application/services/models.ts
|
|
1902
|
+
var supportedModels = [
|
|
1903
|
+
{
|
|
1904
|
+
endpoint: new OpenAIResponses(),
|
|
1905
|
+
specification: gpt54Specification
|
|
1906
|
+
},
|
|
1907
|
+
{
|
|
1908
|
+
endpoint: new OpenAIResponses(),
|
|
1909
|
+
specification: gpt54MiniSpecification
|
|
1910
|
+
},
|
|
1911
|
+
{
|
|
1912
|
+
endpoint: new OpenAIResponses(),
|
|
1913
|
+
specification: gpt54NanoSpecification
|
|
1914
|
+
},
|
|
1915
|
+
{
|
|
1916
|
+
endpoint: new OpenAIResponses(),
|
|
1917
|
+
specification: gpt55Specification
|
|
1918
|
+
},
|
|
1919
|
+
{
|
|
1920
|
+
endpoint: new AnthropicMessages(),
|
|
1921
|
+
specification: claudeHaiku45Specification
|
|
1922
|
+
},
|
|
1923
|
+
{
|
|
1924
|
+
endpoint: new AnthropicMessages(),
|
|
1925
|
+
specification: claudeSonnet46Specification
|
|
1926
|
+
},
|
|
1927
|
+
{
|
|
1928
|
+
endpoint: new AnthropicMessages(),
|
|
1929
|
+
specification: claudeOpus47Specification
|
|
1930
|
+
},
|
|
1931
|
+
{
|
|
1932
|
+
endpoint: new AnthropicMessages(),
|
|
1933
|
+
specification: claudeOpus48Specification
|
|
1934
|
+
},
|
|
1935
|
+
{
|
|
1936
|
+
endpoint: new GeminiGenerateContent(),
|
|
1937
|
+
specification: gemini35FlashSpecification
|
|
1938
|
+
},
|
|
1939
|
+
{
|
|
1940
|
+
endpoint: new GeminiGenerateContent(),
|
|
1941
|
+
specification: gemini31ProSpecification
|
|
1942
|
+
},
|
|
1943
|
+
{
|
|
1944
|
+
endpoint: new OpenAIChatCompletions(),
|
|
1945
|
+
specification: deepSeekV4FlashSpecification
|
|
1946
|
+
},
|
|
1947
|
+
{
|
|
1948
|
+
endpoint: new OpenAIChatCompletions(),
|
|
1949
|
+
specification: deepSeekV4ProSpecification
|
|
1734
1950
|
}
|
|
1735
|
-
|
|
1951
|
+
];
|
|
1736
1952
|
|
|
1737
1953
|
// src/domain/generative-model/request-validation/reasoning-effort.ts
|
|
1738
1954
|
var ReasoningEffortValidation = class {
|
|
1739
1955
|
constructor() {
|
|
1740
1956
|
this.name = "reasoning-effort";
|
|
1741
1957
|
}
|
|
1742
|
-
isValid(
|
|
1743
|
-
if (
|
|
1958
|
+
isValid(inferenceInput, model) {
|
|
1959
|
+
if (inferenceInput.reasoningEffort === void 0) {
|
|
1744
1960
|
return true;
|
|
1745
1961
|
}
|
|
1746
1962
|
if (!model.supportsReasoningEffort) {
|
|
1747
1963
|
return false;
|
|
1748
1964
|
}
|
|
1749
|
-
return model.supportedReasoningEfforts.includes(
|
|
1965
|
+
return model.supportedReasoningEfforts.includes(inferenceInput.reasoningEffort);
|
|
1750
1966
|
}
|
|
1751
1967
|
};
|
|
1752
1968
|
|
|
@@ -1755,8 +1971,8 @@ var ToolCallingValidation = class {
|
|
|
1755
1971
|
constructor() {
|
|
1756
1972
|
this.name = "tool-calling";
|
|
1757
1973
|
}
|
|
1758
|
-
isValid(
|
|
1759
|
-
if (
|
|
1974
|
+
isValid(inferenceInput, model) {
|
|
1975
|
+
if (inferenceInput.tools === void 0) {
|
|
1760
1976
|
return true;
|
|
1761
1977
|
}
|
|
1762
1978
|
return model.supportsFunctionCalling;
|
|
@@ -1768,8 +1984,8 @@ var StreamingValidation = class {
|
|
|
1768
1984
|
constructor() {
|
|
1769
1985
|
this.name = "streaming";
|
|
1770
1986
|
}
|
|
1771
|
-
isValid(
|
|
1772
|
-
if (!
|
|
1987
|
+
isValid(inferenceInput, model) {
|
|
1988
|
+
if (!inferenceInput.streaming) {
|
|
1773
1989
|
return true;
|
|
1774
1990
|
}
|
|
1775
1991
|
return model.supportsStreaming;
|
|
@@ -1796,8 +2012,8 @@ var ContextValidation = class {
|
|
|
1796
2012
|
constructor() {
|
|
1797
2013
|
this.name = "context";
|
|
1798
2014
|
}
|
|
1799
|
-
isValid(
|
|
1800
|
-
return
|
|
2015
|
+
isValid(inferenceInput, model) {
|
|
2016
|
+
return inferenceInput.context.items.every(
|
|
1801
2017
|
(item) => model.supportedContextItemTypes.includes(getContextItemValidationKey(item))
|
|
1802
2018
|
);
|
|
1803
2019
|
}
|
|
@@ -1811,58 +2027,187 @@ var defaultRequestValidationRules = [
|
|
|
1811
2027
|
new StructuredOutputValidation(),
|
|
1812
2028
|
new ContextValidation()
|
|
1813
2029
|
];
|
|
1814
|
-
var
|
|
2030
|
+
var InferenceInputValidator = class {
|
|
1815
2031
|
constructor(rules = defaultRequestValidationRules) {
|
|
1816
2032
|
this.rules = rules;
|
|
1817
2033
|
}
|
|
1818
|
-
validate(
|
|
2034
|
+
validate(inferenceInput, model) {
|
|
1819
2035
|
for (const rule of this.rules) {
|
|
1820
|
-
if (!rule.isValid(
|
|
2036
|
+
if (!rule.isValid(inferenceInput, model)) {
|
|
1821
2037
|
throw new Error(`Request validation "${rule.name}" failed for model "${model.name}"`);
|
|
1822
2038
|
}
|
|
1823
2039
|
}
|
|
1824
2040
|
}
|
|
1825
2041
|
};
|
|
1826
2042
|
|
|
1827
|
-
// src/
|
|
1828
|
-
var
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
2043
|
+
// src/application/services/inference-runner.ts
|
|
2044
|
+
var DefaultInferenceRunner = class {
|
|
2045
|
+
constructor(supportedModels2, requestValidator) {
|
|
2046
|
+
this.supportedModels = supportedModels2;
|
|
2047
|
+
this.requestValidator = requestValidator;
|
|
2048
|
+
}
|
|
2049
|
+
async run(input) {
|
|
2050
|
+
const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
|
|
2051
|
+
if (!generativeModel) {
|
|
2052
|
+
throw new Error(`Unsupported model: ${input.model}`);
|
|
2053
|
+
}
|
|
2054
|
+
this.requestValidator.validate(input, generativeModel.specification);
|
|
2055
|
+
return await generativeModel.endpoint.infer(input);
|
|
2056
|
+
}
|
|
2057
|
+
async *stream(input) {
|
|
2058
|
+
const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
|
|
2059
|
+
if (!generativeModel) {
|
|
2060
|
+
throw new Error(`Unsupported model: ${input.model}`);
|
|
2061
|
+
}
|
|
2062
|
+
this.requestValidator.validate(input, generativeModel.specification);
|
|
2063
|
+
yield* generativeModel.endpoint.stream(input);
|
|
2064
|
+
}
|
|
1835
2065
|
};
|
|
1836
|
-
|
|
1837
|
-
|
|
2066
|
+
|
|
2067
|
+
// src/application/services/function-call.ts
|
|
2068
|
+
var DefaultFunctionCallRunner = class {
|
|
2069
|
+
async run(input, tool) {
|
|
2070
|
+
try {
|
|
2071
|
+
const result = await tool.invoke(JSON.parse(input.args));
|
|
2072
|
+
return FunctionCallOutputItem.create(input.callId, JSON.stringify(result));
|
|
2073
|
+
} catch (error) {
|
|
2074
|
+
return FunctionCallOutputItem.create(input.callId, `Error calling tool: ${error}`);
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
1838
2077
|
};
|
|
1839
|
-
|
|
1840
|
-
|
|
2078
|
+
|
|
2079
|
+
// src/application/use-cases/send-event.ts
|
|
2080
|
+
function createSendEvent(resolveRuntime) {
|
|
2081
|
+
return function sendEvent(event, senderId) {
|
|
2082
|
+
const runtime = resolveRuntime();
|
|
2083
|
+
const participant = runtime.getParticipant(senderId);
|
|
2084
|
+
if (!participant) {
|
|
2085
|
+
throw new Error(`Participant ${senderId} not found`);
|
|
2086
|
+
}
|
|
2087
|
+
runtime.publish(event);
|
|
2088
|
+
};
|
|
2089
|
+
}
|
|
2090
|
+
|
|
2091
|
+
// src/define-runtime.ts
|
|
2092
|
+
function defineRuntime() {
|
|
2093
|
+
let runtime = null;
|
|
2094
|
+
const processor = new EventProcessor();
|
|
2095
|
+
function initializeRuntime(config) {
|
|
2096
|
+
var _a, _b, _c, _d;
|
|
2097
|
+
if (runtime) {
|
|
2098
|
+
throw new Error("Runtime already initialized");
|
|
2099
|
+
}
|
|
2100
|
+
const inferenceRunner = (_d = (_a = config.inferenceRunnerConfig) == null ? void 0 : _a.runner) != null ? _d : new DefaultInferenceRunner(
|
|
2101
|
+
(_c = (_b = config.inferenceRunnerConfig) == null ? void 0 : _b.supportedModels) != null ? _c : supportedModels,
|
|
2102
|
+
new InferenceInputValidator()
|
|
2103
|
+
);
|
|
2104
|
+
const functionCallRunner = new DefaultFunctionCallRunner();
|
|
2105
|
+
runtime = new RuntimeService(config.state, processor, inferenceRunner, functionCallRunner);
|
|
2106
|
+
return runtime;
|
|
2107
|
+
}
|
|
2108
|
+
function resolveRuntime() {
|
|
2109
|
+
if (!runtime) {
|
|
2110
|
+
throw new Error("Runtime not initialized");
|
|
2111
|
+
}
|
|
2112
|
+
return runtime;
|
|
2113
|
+
}
|
|
2114
|
+
function resolveParticipant(id) {
|
|
2115
|
+
if (!runtime) {
|
|
2116
|
+
throw new Error("Runtime not initialized");
|
|
2117
|
+
}
|
|
2118
|
+
const participant = runtime.state.getParticipant(id);
|
|
2119
|
+
if (!participant) {
|
|
2120
|
+
throw new Error(`Participant ${id} not found`);
|
|
2121
|
+
}
|
|
2122
|
+
return participant;
|
|
2123
|
+
}
|
|
2124
|
+
const join = createJoin(resolveRuntime);
|
|
2125
|
+
const leave = createLeave(resolveRuntime);
|
|
2126
|
+
const sendMessage = createSendMessage(resolveRuntime);
|
|
2127
|
+
const sendEvent = createSendEvent(resolveRuntime);
|
|
2128
|
+
const runLoop = createRunLoop(resolveRuntime);
|
|
2129
|
+
return {
|
|
2130
|
+
initializeRuntime,
|
|
2131
|
+
resolveRuntime,
|
|
2132
|
+
resolveParticipant,
|
|
2133
|
+
join,
|
|
2134
|
+
leave,
|
|
2135
|
+
sendMessage,
|
|
2136
|
+
sendEvent,
|
|
2137
|
+
runLoop
|
|
2138
|
+
};
|
|
2139
|
+
}
|
|
2140
|
+
|
|
2141
|
+
// src/domain/agentic-environment/situation/situation-specification.ts
|
|
2142
|
+
var SituationSpecification = class {
|
|
2143
|
+
and(other) {
|
|
2144
|
+
return new AndSituationSpecification(this, other);
|
|
2145
|
+
}
|
|
2146
|
+
or(other) {
|
|
2147
|
+
return new OrSituationSpecification(this, other);
|
|
2148
|
+
}
|
|
2149
|
+
not() {
|
|
2150
|
+
return new NotSituationSpecification(this);
|
|
2151
|
+
}
|
|
2152
|
+
};
|
|
2153
|
+
var AndSituationSpecification = class extends SituationSpecification {
|
|
2154
|
+
constructor(left, right) {
|
|
2155
|
+
super();
|
|
2156
|
+
this.left = left;
|
|
2157
|
+
this.right = right;
|
|
2158
|
+
}
|
|
2159
|
+
isSatisfiedBy(situationContext) {
|
|
2160
|
+
return this.left.isSatisfiedBy(situationContext) && this.right.isSatisfiedBy(situationContext);
|
|
2161
|
+
}
|
|
2162
|
+
};
|
|
2163
|
+
var OrSituationSpecification = class extends SituationSpecification {
|
|
2164
|
+
constructor(left, right) {
|
|
2165
|
+
super();
|
|
2166
|
+
this.left = left;
|
|
2167
|
+
this.right = right;
|
|
2168
|
+
}
|
|
2169
|
+
isSatisfiedBy(situationContext) {
|
|
2170
|
+
return this.left.isSatisfiedBy(situationContext) || this.right.isSatisfiedBy(situationContext);
|
|
2171
|
+
}
|
|
2172
|
+
};
|
|
2173
|
+
var NotSituationSpecification = class extends SituationSpecification {
|
|
2174
|
+
constructor(rule) {
|
|
2175
|
+
super();
|
|
2176
|
+
this.rule = rule;
|
|
2177
|
+
}
|
|
2178
|
+
isSatisfiedBy(situationContext) {
|
|
2179
|
+
return !this.rule.isSatisfiedBy(situationContext);
|
|
2180
|
+
}
|
|
1841
2181
|
};
|
|
1842
2182
|
export {
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
BaseParticipant,
|
|
2183
|
+
Agent,
|
|
2184
|
+
AnthropicMessages,
|
|
1846
2185
|
ContextItem,
|
|
2186
|
+
DefaultInferenceRunner,
|
|
1847
2187
|
DeveloperMessageItem,
|
|
1848
2188
|
FunctionCallItem,
|
|
1849
2189
|
FunctionCallOutputItem,
|
|
1850
|
-
|
|
1851
|
-
|
|
2190
|
+
GeminiGenerateContent,
|
|
2191
|
+
Human,
|
|
1852
2192
|
InputTokenDetails,
|
|
1853
2193
|
McpClient,
|
|
1854
2194
|
McpToolRegistry,
|
|
1855
2195
|
ModelContext,
|
|
1856
2196
|
ModelMessageItem,
|
|
2197
|
+
OpenAIChatCompletions,
|
|
2198
|
+
OpenAIResponses,
|
|
1857
2199
|
OutputTokenDetails,
|
|
1858
2200
|
Participant,
|
|
1859
2201
|
ReasoningItem,
|
|
2202
|
+
RuntimeState,
|
|
1860
2203
|
SemanticEvent,
|
|
2204
|
+
SituationSpecification,
|
|
1861
2205
|
SystemMessageItem,
|
|
1862
2206
|
TokenUsage,
|
|
1863
2207
|
UserMessageItem,
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
2208
|
+
createAgent,
|
|
2209
|
+
createHuman,
|
|
2210
|
+
defineRuntime,
|
|
2211
|
+
supportedModels
|
|
1867
2212
|
};
|
|
1868
2213
|
//# sourceMappingURL=index.mjs.map
|