@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.js
CHANGED
|
@@ -30,69 +30,61 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
BaseParticipant: () => BaseParticipant,
|
|
33
|
+
Agent: () => Agent,
|
|
34
|
+
AnthropicMessages: () => AnthropicMessages,
|
|
36
35
|
ContextItem: () => ContextItem,
|
|
36
|
+
DefaultInferenceRunner: () => DefaultInferenceRunner,
|
|
37
37
|
DeveloperMessageItem: () => DeveloperMessageItem,
|
|
38
38
|
FunctionCallItem: () => FunctionCallItem,
|
|
39
39
|
FunctionCallOutputItem: () => FunctionCallOutputItem,
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
GeminiGenerateContent: () => GeminiGenerateContent,
|
|
41
|
+
Human: () => Human,
|
|
42
42
|
InputTokenDetails: () => InputTokenDetails,
|
|
43
43
|
McpClient: () => McpClient,
|
|
44
44
|
McpToolRegistry: () => McpToolRegistry,
|
|
45
45
|
ModelContext: () => ModelContext,
|
|
46
46
|
ModelMessageItem: () => ModelMessageItem,
|
|
47
|
+
OpenAIChatCompletions: () => OpenAIChatCompletions,
|
|
48
|
+
OpenAIResponses: () => OpenAIResponses,
|
|
47
49
|
OutputTokenDetails: () => OutputTokenDetails,
|
|
48
50
|
Participant: () => Participant,
|
|
49
51
|
ReasoningItem: () => ReasoningItem,
|
|
52
|
+
RuntimeState: () => RuntimeState,
|
|
50
53
|
SemanticEvent: () => SemanticEvent,
|
|
54
|
+
SituationSpecification: () => SituationSpecification,
|
|
51
55
|
SystemMessageItem: () => SystemMessageItem,
|
|
52
56
|
TokenUsage: () => TokenUsage,
|
|
53
57
|
UserMessageItem: () => UserMessageItem,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
createAgent: () => createAgent,
|
|
59
|
+
createHuman: () => createHuman,
|
|
60
|
+
defineRuntime: () => defineRuntime,
|
|
61
|
+
supportedModels: () => supportedModels
|
|
57
62
|
});
|
|
58
63
|
module.exports = __toCommonJS(index_exports);
|
|
59
64
|
|
|
60
65
|
// src/domain/model-context/model-context.ts
|
|
61
66
|
var ModelContext = class _ModelContext {
|
|
62
|
-
constructor(id,
|
|
63
|
-
this.projectId = projectId;
|
|
67
|
+
constructor(id, items) {
|
|
64
68
|
this.id = id;
|
|
65
69
|
this.items = items;
|
|
66
70
|
}
|
|
67
|
-
addContextItem(item) {
|
|
68
|
-
this.items.push(item);
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
applyModelOutput(items) {
|
|
72
|
-
for (const item of items) {
|
|
73
|
-
const itemType = item.getType();
|
|
74
|
-
if (itemType !== "function_call" && itemType !== "message" && itemType !== "reasoning") {
|
|
75
|
-
throw new Error(`Invalid item type: ${itemType}`);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
this.items.push(...items);
|
|
79
|
-
return this;
|
|
80
|
-
}
|
|
81
71
|
getItems() {
|
|
82
72
|
return this.items;
|
|
83
73
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
return this.items[this.items.length - 1];
|
|
74
|
+
addContextItems(contextItems) {
|
|
75
|
+
this.items.push(...contextItems);
|
|
76
|
+
return this;
|
|
89
77
|
}
|
|
90
|
-
|
|
78
|
+
addItem(item) {
|
|
79
|
+
this.items.push(item);
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
static create() {
|
|
91
83
|
const id = crypto.randomUUID();
|
|
92
|
-
return new _ModelContext(id,
|
|
84
|
+
return new _ModelContext(id, []);
|
|
93
85
|
}
|
|
94
86
|
static rehydrate(data) {
|
|
95
|
-
return new _ModelContext(data.id, data.
|
|
87
|
+
return new _ModelContext(data.id, data.items);
|
|
96
88
|
}
|
|
97
89
|
};
|
|
98
90
|
|
|
@@ -217,8 +209,8 @@ var StructuredOutputValidation = class {
|
|
|
217
209
|
constructor() {
|
|
218
210
|
this.name = "structured-output";
|
|
219
211
|
}
|
|
220
|
-
isValid(
|
|
221
|
-
if (
|
|
212
|
+
isValid(inferenceInput, model) {
|
|
213
|
+
if (inferenceInput.structuredOutput === void 0) {
|
|
222
214
|
return true;
|
|
223
215
|
}
|
|
224
216
|
return model.supportsStructuredOutput;
|
|
@@ -355,48 +347,6 @@ var McpToolRegistry = class {
|
|
|
355
347
|
}
|
|
356
348
|
};
|
|
357
349
|
|
|
358
|
-
// src/infrastructure/repository/in-memory-model-context-repository.ts
|
|
359
|
-
var InMemoryModelContextRepository = class {
|
|
360
|
-
constructor() {
|
|
361
|
-
this.store = /* @__PURE__ */ new Map();
|
|
362
|
-
this.projectIndex = /* @__PURE__ */ new Map();
|
|
363
|
-
}
|
|
364
|
-
async save(context) {
|
|
365
|
-
const existing = this.store.get(context.id);
|
|
366
|
-
if (existing && existing.projectId !== context.projectId) {
|
|
367
|
-
const prevSet = this.projectIndex.get(existing.projectId);
|
|
368
|
-
prevSet == null ? void 0 : prevSet.delete(existing.id);
|
|
369
|
-
if (prevSet && prevSet.size === 0) this.projectIndex.delete(existing.projectId);
|
|
370
|
-
}
|
|
371
|
-
this.store.set(context.id, this.clone(context));
|
|
372
|
-
let set = this.projectIndex.get(context.projectId);
|
|
373
|
-
if (!set) {
|
|
374
|
-
set = /* @__PURE__ */ new Set();
|
|
375
|
-
this.projectIndex.set(context.projectId, set);
|
|
376
|
-
}
|
|
377
|
-
set.add(context.id);
|
|
378
|
-
}
|
|
379
|
-
async get(id) {
|
|
380
|
-
const context = this.store.get(id);
|
|
381
|
-
if (!context) {
|
|
382
|
-
throw new Error(`Context not found: ${id}`);
|
|
383
|
-
}
|
|
384
|
-
return this.clone(context);
|
|
385
|
-
}
|
|
386
|
-
async getByProjectId(projectId) {
|
|
387
|
-
const ids = this.projectIndex.get(projectId);
|
|
388
|
-
if (!ids || ids.size === 0) return [];
|
|
389
|
-
return [...ids].map((id) => this.store.get(id)).filter((c) => Boolean(c)).map((c) => this.clone(c));
|
|
390
|
-
}
|
|
391
|
-
clone(context) {
|
|
392
|
-
return ModelContext.rehydrate({
|
|
393
|
-
id: context.id,
|
|
394
|
-
projectId: context.projectId,
|
|
395
|
-
items: [...context.getItems()]
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
};
|
|
399
|
-
|
|
400
350
|
// src/domain/model-context/context-item/client-item/system-message.ts
|
|
401
351
|
var SystemMessageItem = class _SystemMessageItem extends ContextItem {
|
|
402
352
|
constructor(content) {
|
|
@@ -415,397 +365,664 @@ var SystemMessageItem = class _SystemMessageItem extends ContextItem {
|
|
|
415
365
|
}
|
|
416
366
|
};
|
|
417
367
|
|
|
418
|
-
// src/domain/agentic-environment/
|
|
419
|
-
var
|
|
420
|
-
constructor() {
|
|
421
|
-
this.
|
|
422
|
-
this.
|
|
423
|
-
this.
|
|
368
|
+
// src/domain/agentic-environment/semantic-event/event.ts
|
|
369
|
+
var SemanticEvent = class _SemanticEvent {
|
|
370
|
+
constructor(type, producerId, occurredAt, payload) {
|
|
371
|
+
this.type = type;
|
|
372
|
+
this.producerId = producerId;
|
|
373
|
+
this.occurredAt = occurredAt;
|
|
374
|
+
this.payload = payload;
|
|
424
375
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
}
|
|
429
|
-
environment.subscribe(this);
|
|
430
|
-
this.environments.push(environment);
|
|
431
|
-
this.active.set(environment, true);
|
|
376
|
+
static create(type, producerId, payload) {
|
|
377
|
+
const occurredAt = /* @__PURE__ */ new Date();
|
|
378
|
+
return new _SemanticEvent(type, producerId, occurredAt, payload);
|
|
432
379
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
environment.unsubscribe(this);
|
|
438
|
-
this.environments = this.environments.filter((e) => e !== environment);
|
|
439
|
-
this.active.delete(environment);
|
|
380
|
+
};
|
|
381
|
+
var ParticipantJoinedEvent = class extends SemanticEvent {
|
|
382
|
+
static init(manifest) {
|
|
383
|
+
return SemanticEvent.create("participant.joined", manifest.id, manifest);
|
|
440
384
|
}
|
|
441
|
-
|
|
442
|
-
|
|
385
|
+
};
|
|
386
|
+
var ParticipantLeftEvent = class extends SemanticEvent {
|
|
387
|
+
static init(manifest) {
|
|
388
|
+
return SemanticEvent.create("participant.left", manifest.id, manifest);
|
|
443
389
|
}
|
|
444
|
-
|
|
445
|
-
|
|
390
|
+
};
|
|
391
|
+
var MessageSentEvent = class extends SemanticEvent {
|
|
392
|
+
static init(producerId, message) {
|
|
393
|
+
return SemanticEvent.create("message.sent", producerId, { message });
|
|
446
394
|
}
|
|
447
|
-
|
|
448
|
-
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
// src/application/states/inference.ts
|
|
398
|
+
var InferenceState = class {
|
|
399
|
+
constructor(inferenceRunner) {
|
|
400
|
+
this.inferenceRunner = inferenceRunner;
|
|
401
|
+
this.id = "inference";
|
|
402
|
+
}
|
|
403
|
+
async run(input, loopVisitor) {
|
|
404
|
+
loopVisitor.visitInferenceStarted(input);
|
|
405
|
+
const output = await this.inferenceRunner.run(input);
|
|
406
|
+
loopVisitor.visitInferenceCompleted(output);
|
|
407
|
+
return {
|
|
408
|
+
stateId: this.id,
|
|
409
|
+
input,
|
|
410
|
+
output
|
|
411
|
+
};
|
|
449
412
|
}
|
|
450
|
-
|
|
451
|
-
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
// src/domain/agentic-environment/participant/participant.ts
|
|
416
|
+
var Participant = class {
|
|
417
|
+
constructor(manifest, handlers) {
|
|
418
|
+
this.manifest = manifest;
|
|
419
|
+
this.handlers = handlers;
|
|
452
420
|
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
this.active.set(environment, false);
|
|
456
|
-
}
|
|
421
|
+
getManifest() {
|
|
422
|
+
return this.manifest;
|
|
457
423
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
424
|
+
getId() {
|
|
425
|
+
return this.manifest.id;
|
|
426
|
+
}
|
|
427
|
+
getHandlers() {
|
|
428
|
+
return this.handlers;
|
|
429
|
+
}
|
|
430
|
+
setHandlers(handlers) {
|
|
431
|
+
this.handlers = handlers;
|
|
462
432
|
}
|
|
463
433
|
};
|
|
464
434
|
|
|
465
|
-
// src/domain/agentic-environment/
|
|
466
|
-
var
|
|
467
|
-
constructor(
|
|
468
|
-
|
|
469
|
-
this.message = message;
|
|
470
|
-
this.stack = stack;
|
|
471
|
-
this.source = source;
|
|
472
|
-
this.environment = enviornment;
|
|
435
|
+
// src/domain/agentic-environment/participant/memory.ts
|
|
436
|
+
var Memory = class _Memory {
|
|
437
|
+
constructor(context) {
|
|
438
|
+
this.context = context;
|
|
473
439
|
}
|
|
474
|
-
|
|
475
|
-
return this.
|
|
440
|
+
getContext() {
|
|
441
|
+
return this.context;
|
|
476
442
|
}
|
|
477
|
-
|
|
478
|
-
|
|
443
|
+
static create() {
|
|
444
|
+
const context = ModelContext.create();
|
|
445
|
+
return new _Memory(context);
|
|
479
446
|
}
|
|
480
447
|
};
|
|
481
448
|
|
|
482
|
-
// src/domain/agentic-environment/
|
|
483
|
-
var
|
|
484
|
-
constructor(
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
this.
|
|
488
|
-
this.
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
}
|
|
449
|
+
// src/domain/agentic-environment/participant/agent.ts
|
|
450
|
+
var Agent = class _Agent extends Participant {
|
|
451
|
+
constructor(manifest, developerMessage, tools, memory, handlers) {
|
|
452
|
+
super(manifest, handlers);
|
|
453
|
+
this.memory = memory;
|
|
454
|
+
this.developerMessage = developerMessage;
|
|
455
|
+
this.tools = tools;
|
|
456
|
+
}
|
|
457
|
+
static create({
|
|
458
|
+
name,
|
|
459
|
+
instruction,
|
|
460
|
+
tools,
|
|
461
|
+
capabilities,
|
|
462
|
+
handlers
|
|
463
|
+
}) {
|
|
464
|
+
const id = crypto.randomUUID();
|
|
465
|
+
const memory = Memory.create();
|
|
466
|
+
const manifest = { id, name, capabilities, role: "agent" };
|
|
467
|
+
const developerMessage = DeveloperMessageItem.create(instruction);
|
|
468
|
+
memory.getContext().addItem(developerMessage);
|
|
469
|
+
return new _Agent(manifest, instruction, tools, memory, handlers);
|
|
500
470
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
if (!exists) {
|
|
504
|
-
return;
|
|
505
|
-
}
|
|
506
|
-
participant.onLeft();
|
|
507
|
-
this.subscribers = this.subscribers.filter((p) => p !== participant);
|
|
508
|
-
for (const subscriber of this.subscribers) {
|
|
509
|
-
subscriber.onParticipantLeft(participant);
|
|
510
|
-
}
|
|
471
|
+
getTools() {
|
|
472
|
+
return this.tools;
|
|
511
473
|
}
|
|
512
|
-
|
|
513
|
-
this.
|
|
514
|
-
internal: (src) => src.onFunctionCall(item),
|
|
515
|
-
external: (sub) => sub.onExternalFunctionCall(source, item)
|
|
516
|
-
});
|
|
474
|
+
getDeveloperMessage() {
|
|
475
|
+
return this.developerMessage;
|
|
517
476
|
}
|
|
518
|
-
|
|
519
|
-
this.
|
|
520
|
-
internal: (src) => src.onModelMessage(item),
|
|
521
|
-
external: (sub) => sub.onExternalModelMessage(source, item)
|
|
522
|
-
});
|
|
477
|
+
getMemory() {
|
|
478
|
+
return this.memory;
|
|
523
479
|
}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
// src/application/use-cases/create-agent.ts
|
|
483
|
+
function createAgent({
|
|
484
|
+
name,
|
|
485
|
+
capabilities,
|
|
486
|
+
instruction,
|
|
487
|
+
tools,
|
|
488
|
+
handlers
|
|
489
|
+
}) {
|
|
490
|
+
return Agent.create({ name, instruction, capabilities, tools, handlers });
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// src/domain/agentic-environment/participant/human.ts
|
|
494
|
+
var Human = class _Human extends Participant {
|
|
495
|
+
constructor(manifest, handlers) {
|
|
496
|
+
super(manifest, handlers);
|
|
497
|
+
}
|
|
498
|
+
static create({
|
|
499
|
+
name,
|
|
500
|
+
capabilities,
|
|
501
|
+
handlers
|
|
502
|
+
}) {
|
|
503
|
+
const id = crypto.randomUUID();
|
|
504
|
+
const manifest = { id, name, capabilities, role: "human" };
|
|
505
|
+
return new _Human(manifest, handlers);
|
|
529
506
|
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
// src/application/use-cases/create-human.ts
|
|
510
|
+
function createHuman({
|
|
511
|
+
name,
|
|
512
|
+
capabilities,
|
|
513
|
+
handlers
|
|
514
|
+
}) {
|
|
515
|
+
return Human.create({ name, capabilities, handlers });
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// src/domain/agentic-environment/runtime-state.ts
|
|
519
|
+
var RuntimeState = class {
|
|
520
|
+
constructor() {
|
|
521
|
+
this.participants = /* @__PURE__ */ new Map();
|
|
535
522
|
}
|
|
536
|
-
|
|
537
|
-
this.
|
|
538
|
-
|
|
539
|
-
|
|
523
|
+
addParticipant(participant) {
|
|
524
|
+
const isExists = this.participants.get(participant.getId());
|
|
525
|
+
if (isExists) return;
|
|
526
|
+
this.participants.set(participant.getId(), participant);
|
|
540
527
|
}
|
|
541
|
-
|
|
542
|
-
this.
|
|
543
|
-
internal: (src) => src.onInternalEvent(item),
|
|
544
|
-
external: (sub) => sub.onExternalEvent(source, item)
|
|
545
|
-
});
|
|
528
|
+
removeParticipant(participant) {
|
|
529
|
+
this.participants.delete(participant.getId());
|
|
546
530
|
}
|
|
547
|
-
|
|
548
|
-
this.
|
|
549
|
-
internal: (src) => {
|
|
550
|
-
try {
|
|
551
|
-
src.onError(error);
|
|
552
|
-
} catch (error2) {
|
|
553
|
-
if (this.showLogs) {
|
|
554
|
-
console.warn(
|
|
555
|
-
`[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
|
|
556
|
-
${JSON.stringify({ error: error2 }, null, 2)}`
|
|
557
|
-
);
|
|
558
|
-
}
|
|
559
|
-
} finally {
|
|
560
|
-
src.markInactive(this);
|
|
561
|
-
}
|
|
562
|
-
},
|
|
563
|
-
external: (sub) => {
|
|
564
|
-
try {
|
|
565
|
-
sub.onParticipantError(source, error);
|
|
566
|
-
} catch (error2) {
|
|
567
|
-
if (this.showLogs) {
|
|
568
|
-
console.warn(
|
|
569
|
-
`[AgenticEnvironment] Participant.onError() failed. Reason: ${error2}
|
|
570
|
-
${JSON.stringify({ error: error2 }, null, 2)}`
|
|
571
|
-
);
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
});
|
|
531
|
+
getParticipant(id) {
|
|
532
|
+
return this.participants.get(id);
|
|
576
533
|
}
|
|
577
|
-
|
|
578
|
-
return this.
|
|
579
|
-
(sub) => sub !== source && (sub.getListeners().length === 0 || sub.getListeners().some((listener) => source instanceof listener))
|
|
580
|
-
);
|
|
534
|
+
getParticipants() {
|
|
535
|
+
return [...this.participants.values()];
|
|
581
536
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
// src/application/services/runtime.ts
|
|
540
|
+
var RuntimeService = class {
|
|
541
|
+
constructor(state, processor, inferenceRunner, functionCallRunner) {
|
|
542
|
+
this.state = state;
|
|
543
|
+
this.processor = processor;
|
|
544
|
+
this.inferenceRunner = inferenceRunner;
|
|
545
|
+
this.functionCallRunner = functionCallRunner;
|
|
546
|
+
}
|
|
547
|
+
join(participant) {
|
|
548
|
+
this.state.addParticipant(participant);
|
|
549
|
+
this.publish(ParticipantJoinedEvent.init(participant.getManifest()));
|
|
550
|
+
}
|
|
551
|
+
leave(participant) {
|
|
552
|
+
this.state.removeParticipant(participant);
|
|
553
|
+
this.publish(ParticipantLeftEvent.init(participant.getManifest()));
|
|
554
|
+
}
|
|
555
|
+
publish(event) {
|
|
556
|
+
for (const participant of this.state.getParticipants()) {
|
|
557
|
+
this.processor.process(event, participant);
|
|
598
558
|
}
|
|
599
559
|
}
|
|
600
|
-
|
|
601
|
-
if (
|
|
602
|
-
|
|
560
|
+
getParticipant(id) {
|
|
561
|
+
if (!this.state.getParticipant(id)) {
|
|
562
|
+
throw new Error(`Participant ${id} not found`);
|
|
603
563
|
}
|
|
604
|
-
|
|
605
|
-
message: error.message,
|
|
606
|
-
stack: error.stack,
|
|
607
|
-
enviornment: this,
|
|
608
|
-
source: subscriber
|
|
609
|
-
});
|
|
610
|
-
return this.deliverError(subscriber, agenticError);
|
|
564
|
+
return this.state.getParticipant(id);
|
|
611
565
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
// src/domain/model-context/semantic-event/semantic-event.ts
|
|
615
|
-
var SemanticEvent = class {
|
|
616
|
-
constructor(type, data) {
|
|
617
|
-
this.type = type;
|
|
618
|
-
this.data = data;
|
|
566
|
+
getInferenceRunner() {
|
|
567
|
+
return this.inferenceRunner;
|
|
619
568
|
}
|
|
620
|
-
|
|
621
|
-
return this.
|
|
569
|
+
getFunctionCallRunner() {
|
|
570
|
+
return this.functionCallRunner;
|
|
622
571
|
}
|
|
623
572
|
};
|
|
624
573
|
|
|
625
|
-
// src/domain/agentic-environment/
|
|
626
|
-
var
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
574
|
+
// src/domain/agentic-environment/semantic-event/event-processor.ts
|
|
575
|
+
var EventProcessor = class {
|
|
576
|
+
process(event, consumer) {
|
|
577
|
+
for (const handler of consumer.getHandlers()) {
|
|
578
|
+
const isSatisfied = handler.specification.isSatisfiedBy({
|
|
579
|
+
event,
|
|
580
|
+
participant: consumer
|
|
581
|
+
});
|
|
582
|
+
if (isSatisfied) {
|
|
583
|
+
handler.processor.apply({ event, participant: consumer });
|
|
584
|
+
}
|
|
585
|
+
}
|
|
630
586
|
}
|
|
631
587
|
};
|
|
632
588
|
|
|
633
|
-
// src/application/
|
|
634
|
-
|
|
635
|
-
|
|
589
|
+
// src/application/use-cases/join.ts
|
|
590
|
+
function createJoin(resolveRuntime) {
|
|
591
|
+
return function join(participant) {
|
|
592
|
+
resolveRuntime().join(participant);
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// src/application/use-cases/leave.ts
|
|
597
|
+
function createLeave(resolveRuntime) {
|
|
598
|
+
return function leave(participant) {
|
|
599
|
+
resolveRuntime().leave(participant);
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// src/application/use-cases/send-message.ts
|
|
604
|
+
function createSendMessage(resolveRuntime) {
|
|
605
|
+
return function sendMessage(message, senderId) {
|
|
606
|
+
const runtime = resolveRuntime();
|
|
607
|
+
const participant = runtime.getParticipant(senderId);
|
|
608
|
+
if (!participant) {
|
|
609
|
+
throw new Error(`Participant ${senderId} not found`);
|
|
610
|
+
}
|
|
611
|
+
const userMessage = MessageSentEvent.init(senderId, message);
|
|
612
|
+
runtime.publish(userMessage);
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// src/domain/agentic-environment/loop/agent-loop.ts
|
|
617
|
+
var AgentLoop = class _AgentLoop {
|
|
618
|
+
constructor(loopId, stateExecutor, transitionResolver, interceptionHandler) {
|
|
619
|
+
this.loopId = loopId;
|
|
620
|
+
this.stateExecutor = stateExecutor;
|
|
621
|
+
this.transitionResolver = transitionResolver;
|
|
622
|
+
this.interceptionHandler = interceptionHandler;
|
|
623
|
+
}
|
|
624
|
+
async run(message, loopVisitor) {
|
|
636
625
|
var _a;
|
|
637
|
-
|
|
638
|
-
|
|
626
|
+
let transition = {
|
|
627
|
+
nextStateId: "context_update",
|
|
628
|
+
input: message
|
|
629
|
+
};
|
|
630
|
+
while (transition.nextStateId !== "idle") {
|
|
631
|
+
const isInterceptionSatisfied = (_a = this.interceptionHandler) == null ? void 0 : _a.isSatisfiedBy(transition);
|
|
632
|
+
if (isInterceptionSatisfied && this.interceptionHandler) {
|
|
633
|
+
loopVisitor.visitInterceptionStarted(transition);
|
|
634
|
+
transition = await this.interceptionHandler.handle(transition);
|
|
635
|
+
loopVisitor.visitInterceptionFinished(transition);
|
|
636
|
+
} else {
|
|
637
|
+
const execution = await this.stateExecutor.execute(transition, loopVisitor);
|
|
638
|
+
transition = this.transitionResolver.resolve(execution);
|
|
639
|
+
}
|
|
639
640
|
}
|
|
640
|
-
|
|
641
|
+
}
|
|
642
|
+
getLoopId() {
|
|
643
|
+
return this.loopId;
|
|
644
|
+
}
|
|
645
|
+
static create(stateExecutor, transitionResolver, interceptionHandler) {
|
|
646
|
+
const loopId = crypto.randomUUID();
|
|
647
|
+
return new _AgentLoop(loopId, stateExecutor, transitionResolver, interceptionHandler);
|
|
641
648
|
}
|
|
642
649
|
};
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
650
|
+
|
|
651
|
+
// src/domain/agentic-environment/loop/state-executor.ts
|
|
652
|
+
var LoopStateExecutor = class {
|
|
653
|
+
constructor(contextUpdateState, inferenceState, inferenceStreamingState, functionCallState, modelMessageState) {
|
|
654
|
+
this.contextUpdateState = contextUpdateState;
|
|
655
|
+
this.inferenceState = inferenceState;
|
|
656
|
+
this.inferenceStreamingState = inferenceStreamingState;
|
|
657
|
+
this.functionCallState = functionCallState;
|
|
658
|
+
this.modelMessageState = modelMessageState;
|
|
659
|
+
}
|
|
660
|
+
async execute(transition, loopVisitor) {
|
|
661
|
+
switch (transition.nextStateId) {
|
|
662
|
+
case "context_update":
|
|
663
|
+
return await this.contextUpdateState.run(transition.input, loopVisitor);
|
|
664
|
+
case "inference":
|
|
665
|
+
return await this.inferenceState.run(transition.input, loopVisitor);
|
|
666
|
+
case "inference_streaming":
|
|
667
|
+
return await this.inferenceStreamingState.run(transition.input, loopVisitor);
|
|
668
|
+
case "function_call":
|
|
669
|
+
return await this.functionCallState.run(transition.input, loopVisitor);
|
|
670
|
+
case "model_message":
|
|
671
|
+
return await this.modelMessageState.run(transition.input, loopVisitor);
|
|
655
672
|
}
|
|
656
673
|
}
|
|
657
674
|
};
|
|
658
675
|
|
|
659
|
-
// src/application/
|
|
660
|
-
var
|
|
661
|
-
constructor(
|
|
662
|
-
this.
|
|
663
|
-
this.
|
|
676
|
+
// src/application/states/function-call.ts
|
|
677
|
+
var FunctionCallState = class {
|
|
678
|
+
constructor(functionCallRunner) {
|
|
679
|
+
this.functionCallRunner = functionCallRunner;
|
|
680
|
+
this.id = "function_call";
|
|
664
681
|
}
|
|
665
|
-
async
|
|
682
|
+
async run(input, loopVisitor) {
|
|
666
683
|
var _a;
|
|
667
|
-
|
|
668
|
-
const
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
break;
|
|
679
|
-
}
|
|
680
|
-
if (item instanceof ReasoningItem) {
|
|
681
|
-
environment.deliverReasoning(caller, item);
|
|
682
|
-
} else if (item instanceof FunctionCallItem) {
|
|
683
|
-
environment.deliverFunctionCall(caller, item);
|
|
684
|
-
} else if (item instanceof ModelMessageItem) {
|
|
685
|
-
environment.deliverModelMessage(caller, item);
|
|
686
|
-
} else if (item instanceof SemanticEvent) {
|
|
687
|
-
environment.deliverSemanticEvent(caller, item);
|
|
688
|
-
}
|
|
684
|
+
loopVisitor.visitFunctionCallStarted(input);
|
|
685
|
+
const { call, inferenceInput } = input;
|
|
686
|
+
let tool = (_a = inferenceInput.tools) == null ? void 0 : _a.find((tool2) => tool2.name === call.name);
|
|
687
|
+
if (!tool) {
|
|
688
|
+
return {
|
|
689
|
+
stateId: this.id,
|
|
690
|
+
input,
|
|
691
|
+
output: {
|
|
692
|
+
item: FunctionCallOutputItem.create(call.callId, `Error: unknown tool "${call.name}"`)
|
|
693
|
+
}
|
|
694
|
+
};
|
|
689
695
|
}
|
|
696
|
+
const item = await this.functionCallRunner.run(call, tool);
|
|
697
|
+
loopVisitor.visitFunctionCallCompleted(item);
|
|
698
|
+
return {
|
|
699
|
+
stateId: this.id,
|
|
700
|
+
input,
|
|
701
|
+
output: {
|
|
702
|
+
item
|
|
703
|
+
}
|
|
704
|
+
};
|
|
690
705
|
}
|
|
691
706
|
};
|
|
692
707
|
|
|
693
|
-
// src/application/
|
|
694
|
-
var
|
|
695
|
-
constructor(
|
|
696
|
-
this.
|
|
708
|
+
// src/application/states/context-update.ts
|
|
709
|
+
var ContextUpdateState = class {
|
|
710
|
+
constructor() {
|
|
711
|
+
this.id = "context_update";
|
|
712
|
+
}
|
|
713
|
+
async run(input, loopVisitor) {
|
|
714
|
+
loopVisitor.visitContextUpdateStarted(input);
|
|
715
|
+
const userMessage = UserMessageItem.create(input.content);
|
|
716
|
+
const output = {
|
|
717
|
+
...input.input,
|
|
718
|
+
context: input.input.context.addContextItems([userMessage])
|
|
719
|
+
};
|
|
720
|
+
loopVisitor.visitContextUpdateCompleted(output);
|
|
721
|
+
return {
|
|
722
|
+
stateId: this.id,
|
|
723
|
+
input,
|
|
724
|
+
output
|
|
725
|
+
};
|
|
697
726
|
}
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
// src/application/states/model-message.ts
|
|
730
|
+
var ModelMessageState = class {
|
|
731
|
+
constructor() {
|
|
732
|
+
this.id = "model_message";
|
|
733
|
+
}
|
|
734
|
+
async run(input, loopVisitor) {
|
|
735
|
+
loopVisitor.visitModelAnswer(input);
|
|
736
|
+
return {
|
|
737
|
+
stateId: this.id,
|
|
738
|
+
input,
|
|
739
|
+
output: void 0
|
|
740
|
+
};
|
|
703
741
|
}
|
|
704
742
|
};
|
|
705
743
|
|
|
706
|
-
// src/
|
|
707
|
-
var
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
744
|
+
// src/domain/agentic-environment/loop/transition-resolver.ts
|
|
745
|
+
var TransitionResolver = class {
|
|
746
|
+
constructor(rules) {
|
|
747
|
+
this.rules = rules;
|
|
748
|
+
}
|
|
749
|
+
resolve(execution) {
|
|
750
|
+
const rule = this.rules.find((rule2) => rule2.specification.isSatisfiedBy(execution));
|
|
751
|
+
if (!rule) {
|
|
752
|
+
throw new Error(`No transition rule found after "${execution.stateId}"`);
|
|
711
753
|
}
|
|
712
|
-
|
|
713
|
-
const result = await tool.invoke(JSON.parse(call.args));
|
|
714
|
-
yield FunctionCallOutputItem.create(call.callId, JSON.stringify(result));
|
|
754
|
+
return rule.createTransition(execution);
|
|
715
755
|
}
|
|
716
756
|
};
|
|
717
757
|
|
|
718
|
-
// src/
|
|
719
|
-
var
|
|
720
|
-
|
|
758
|
+
// src/domain/agentic-environment/loop/transition-specification.ts
|
|
759
|
+
var MessageReceivedSpecification = class {
|
|
760
|
+
isSatisfiedBy(execution) {
|
|
761
|
+
return execution.stateId === "context_update";
|
|
721
762
|
}
|
|
722
|
-
|
|
763
|
+
};
|
|
764
|
+
function isInferenceExecution(execution) {
|
|
765
|
+
return execution.stateId === "inference" || execution.stateId === "inference_streaming";
|
|
766
|
+
}
|
|
767
|
+
var InferenceProducedFunctionCallSpecification = class {
|
|
768
|
+
isSatisfiedBy(execution) {
|
|
769
|
+
return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "function_call");
|
|
723
770
|
}
|
|
724
|
-
|
|
771
|
+
};
|
|
772
|
+
var InferenceProducedModelMessageSpecification = class {
|
|
773
|
+
isSatisfiedBy(execution) {
|
|
774
|
+
return isInferenceExecution(execution) && execution.output.items.some((item) => item.type === "message" && item.role === "assistant");
|
|
725
775
|
}
|
|
726
|
-
|
|
776
|
+
};
|
|
777
|
+
var FunctionCallCompletedSpecification = class {
|
|
778
|
+
isSatisfiedBy(execution) {
|
|
779
|
+
return execution.stateId === "function_call";
|
|
727
780
|
}
|
|
728
|
-
|
|
781
|
+
};
|
|
782
|
+
var ModelMessageCompletedSpecification = class {
|
|
783
|
+
isSatisfiedBy(execution) {
|
|
784
|
+
return execution.stateId === "model_message";
|
|
729
785
|
}
|
|
730
|
-
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
// src/domain/agentic-environment/loop/transition-rule.ts
|
|
789
|
+
var ContextPreparationToInferenceRule = class {
|
|
790
|
+
constructor() {
|
|
791
|
+
this.specification = new MessageReceivedSpecification();
|
|
731
792
|
}
|
|
732
|
-
|
|
793
|
+
createTransition(execution) {
|
|
794
|
+
if (execution.stateId !== "context_update") {
|
|
795
|
+
throw new Error("Expected context_preparation execution");
|
|
796
|
+
}
|
|
797
|
+
const nextStateId = execution.output.streaming ? "inference_streaming" : "inference";
|
|
798
|
+
return {
|
|
799
|
+
nextStateId,
|
|
800
|
+
input: execution.output
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
};
|
|
804
|
+
var InferenceToFunctionCallRule = class {
|
|
805
|
+
constructor() {
|
|
806
|
+
this.specification = new InferenceProducedFunctionCallSpecification();
|
|
807
|
+
}
|
|
808
|
+
createTransition(execution) {
|
|
809
|
+
if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "function_call")) {
|
|
810
|
+
throw new Error("Expected inference function-call output");
|
|
811
|
+
}
|
|
812
|
+
const functionCallItem = execution.output.items.find((item) => item.type === "function_call");
|
|
813
|
+
if (!functionCallItem) {
|
|
814
|
+
throw new Error("Expected inference function-call output");
|
|
815
|
+
}
|
|
816
|
+
return {
|
|
817
|
+
nextStateId: "function_call",
|
|
818
|
+
input: {
|
|
819
|
+
call: functionCallItem,
|
|
820
|
+
inferenceInput: execution.input
|
|
821
|
+
}
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
var InferenceToModelMessageRule = class {
|
|
826
|
+
constructor() {
|
|
827
|
+
this.specification = new InferenceProducedModelMessageSpecification();
|
|
828
|
+
}
|
|
829
|
+
createTransition(execution) {
|
|
830
|
+
if (execution.stateId !== "inference" && execution.stateId !== "inference_streaming" || !execution.output.items.some((item) => item.type === "message" && item.role === "assistant")) {
|
|
831
|
+
throw new Error("Expected inference model-message output");
|
|
832
|
+
}
|
|
833
|
+
const modelMessageItem = execution.output.items.find(
|
|
834
|
+
(item) => item.type === "message" && item.role === "assistant"
|
|
835
|
+
);
|
|
836
|
+
if (!modelMessageItem) {
|
|
837
|
+
throw new Error("Expected inference model-message output");
|
|
838
|
+
}
|
|
839
|
+
return {
|
|
840
|
+
nextStateId: "model_message",
|
|
841
|
+
input: {
|
|
842
|
+
answer: modelMessageItem
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
var FunctionCallToInferenceRule = class {
|
|
848
|
+
constructor() {
|
|
849
|
+
this.specification = new FunctionCallCompletedSpecification();
|
|
850
|
+
}
|
|
851
|
+
createTransition(execution) {
|
|
852
|
+
if (execution.stateId !== "function_call") {
|
|
853
|
+
throw new Error("Expected function_call execution");
|
|
854
|
+
}
|
|
855
|
+
const previousRequest = execution.input.inferenceInput;
|
|
856
|
+
previousRequest.context.addContextItems([execution.input.call, execution.output.item]);
|
|
857
|
+
return {
|
|
858
|
+
nextStateId: previousRequest.streaming ? "inference_streaming" : "inference",
|
|
859
|
+
input: {
|
|
860
|
+
...previousRequest
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
var ModelMessageToIdleRule = class {
|
|
866
|
+
constructor() {
|
|
867
|
+
this.specification = new ModelMessageCompletedSpecification();
|
|
868
|
+
}
|
|
869
|
+
createTransition(execution) {
|
|
870
|
+
if (execution.stateId !== "model_message") {
|
|
871
|
+
throw new Error("Expected model_message execution");
|
|
872
|
+
}
|
|
873
|
+
return {
|
|
874
|
+
nextStateId: "idle",
|
|
875
|
+
input: void 0
|
|
876
|
+
};
|
|
733
877
|
}
|
|
734
|
-
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
// src/domain/agentic-environment/loop/event-publisher-visitor.ts
|
|
881
|
+
var import_cloud_sdk = require("@mozaik-ai/cloud-sdk");
|
|
882
|
+
var EventPublisherLoopVisitor = class {
|
|
883
|
+
constructor(agentId, loopId, runtime) {
|
|
884
|
+
this.agentId = agentId;
|
|
885
|
+
this.loopId = loopId;
|
|
886
|
+
this.runtime = runtime;
|
|
887
|
+
this.cloud = (0, import_cloud_sdk.createCloudClient)();
|
|
888
|
+
}
|
|
889
|
+
visitContextUpdateStarted(input) {
|
|
890
|
+
this.publish("context_update.started", { ...input, loopId: this.loopId });
|
|
735
891
|
}
|
|
736
|
-
|
|
892
|
+
visitContextUpdateCompleted(output) {
|
|
893
|
+
this.publish("context_update.completed", { ...output, loopId: this.loopId });
|
|
737
894
|
}
|
|
738
|
-
|
|
895
|
+
visitInferenceStarted(input) {
|
|
896
|
+
this.publish("inference.started", input);
|
|
739
897
|
}
|
|
740
|
-
|
|
898
|
+
visitInferenceEvent(event) {
|
|
899
|
+
this.publish("inference.stream", event);
|
|
741
900
|
}
|
|
742
|
-
|
|
901
|
+
visitInferenceCompleted(output) {
|
|
902
|
+
this.publish("inference.completed", output);
|
|
743
903
|
}
|
|
744
|
-
|
|
904
|
+
visitFunctionCallStarted(input) {
|
|
905
|
+
this.publish("function_call.started", input);
|
|
745
906
|
}
|
|
746
|
-
|
|
907
|
+
visitFunctionCallCompleted(output) {
|
|
908
|
+
this.publish("function_call.completed", output);
|
|
747
909
|
}
|
|
748
|
-
|
|
910
|
+
visitModelAnswer(input) {
|
|
911
|
+
this.publish("model.answer", input);
|
|
749
912
|
}
|
|
750
|
-
|
|
913
|
+
visitInterceptionStarted(transition) {
|
|
914
|
+
this.publish("interception.started", transition);
|
|
751
915
|
}
|
|
752
|
-
|
|
916
|
+
visitInterceptionFinished(transition) {
|
|
917
|
+
this.publish("interception.finished", transition);
|
|
918
|
+
}
|
|
919
|
+
publish(type, payload) {
|
|
920
|
+
const event = new SemanticEvent(type, this.agentId, /* @__PURE__ */ new Date(), payload);
|
|
921
|
+
this.runtime.publish(event);
|
|
922
|
+
if (this.cloud.enabled) {
|
|
923
|
+
this.cloud.send(event);
|
|
924
|
+
}
|
|
753
925
|
}
|
|
754
926
|
};
|
|
755
927
|
|
|
756
|
-
// src/application/
|
|
757
|
-
var
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
928
|
+
// src/application/states/inference-streaming.ts
|
|
929
|
+
var InferenceStreamingState = class {
|
|
930
|
+
constructor(inferenceRunner) {
|
|
931
|
+
this.inferenceRunner = inferenceRunner;
|
|
932
|
+
this.id = "inference_streaming";
|
|
933
|
+
}
|
|
934
|
+
async run(input, loopVisitor) {
|
|
935
|
+
loopVisitor.visitInferenceStarted(input);
|
|
936
|
+
let output = void 0;
|
|
937
|
+
for await (const event of this.inferenceRunner.stream(input)) {
|
|
938
|
+
loopVisitor.visitInferenceEvent(event);
|
|
939
|
+
if (event.type === "inference.output") {
|
|
940
|
+
output = event.payload;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
if (!output) {
|
|
944
|
+
throw new Error("Inference output not found");
|
|
761
945
|
}
|
|
762
|
-
|
|
946
|
+
loopVisitor.visitInferenceCompleted(output);
|
|
947
|
+
return {
|
|
948
|
+
stateId: "inference_streaming",
|
|
949
|
+
input,
|
|
950
|
+
output
|
|
951
|
+
};
|
|
763
952
|
}
|
|
764
953
|
};
|
|
765
954
|
|
|
955
|
+
// src/application/use-cases/run-loop.ts
|
|
956
|
+
function createRunLoop(resolveRuntime) {
|
|
957
|
+
return function runLoop(agentId, message, inferenceInput, interceptionHandler) {
|
|
958
|
+
const runtime = resolveRuntime();
|
|
959
|
+
const inferenceRunner = runtime.getInferenceRunner();
|
|
960
|
+
const functionCallRunner = runtime.getFunctionCallRunner();
|
|
961
|
+
const transitionResolver = new TransitionResolver([
|
|
962
|
+
// High-priority interception rules would go first.
|
|
963
|
+
new ContextPreparationToInferenceRule(),
|
|
964
|
+
new InferenceToFunctionCallRule(),
|
|
965
|
+
new InferenceToModelMessageRule(),
|
|
966
|
+
new FunctionCallToInferenceRule(),
|
|
967
|
+
new ModelMessageToIdleRule()
|
|
968
|
+
]);
|
|
969
|
+
const stateExecutor = new LoopStateExecutor(
|
|
970
|
+
new ContextUpdateState(),
|
|
971
|
+
new InferenceState(inferenceRunner),
|
|
972
|
+
new InferenceStreamingState(inferenceRunner),
|
|
973
|
+
new FunctionCallState(functionCallRunner),
|
|
974
|
+
new ModelMessageState()
|
|
975
|
+
);
|
|
976
|
+
const agentLoop = AgentLoop.create(stateExecutor, transitionResolver, interceptionHandler);
|
|
977
|
+
const loopVisitor = new EventPublisherLoopVisitor(agentId, agentLoop.getLoopId(), runtime);
|
|
978
|
+
agentLoop.run(
|
|
979
|
+
{
|
|
980
|
+
content: message,
|
|
981
|
+
input: inferenceInput
|
|
982
|
+
},
|
|
983
|
+
loopVisitor
|
|
984
|
+
);
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
|
|
766
988
|
// src/infrastructure/providers/anthropic/endpoints/anthropic-messages-mapper.ts
|
|
767
989
|
var AnthropicMessagesMapper = class {
|
|
768
|
-
toRequest(
|
|
769
|
-
const { messages, system } = this.mapContextItems(
|
|
990
|
+
toRequest(inferenceInput) {
|
|
991
|
+
const { messages, system } = this.mapContextItems(inferenceInput);
|
|
770
992
|
const outputConfig = {};
|
|
771
993
|
const request = {
|
|
772
|
-
model:
|
|
994
|
+
model: inferenceInput.model,
|
|
773
995
|
messages
|
|
774
996
|
};
|
|
775
|
-
request.max_tokens =
|
|
997
|
+
request.max_tokens = inferenceInput.maxOutputTokens;
|
|
776
998
|
if (system) {
|
|
777
999
|
request.system = system;
|
|
778
1000
|
}
|
|
779
|
-
if (
|
|
780
|
-
request.tools =
|
|
1001
|
+
if (inferenceInput.tools && inferenceInput.tools.length > 0) {
|
|
1002
|
+
request.tools = inferenceInput.tools.map((tool) => ({
|
|
781
1003
|
name: tool.name,
|
|
782
1004
|
description: tool.description,
|
|
783
1005
|
input_schema: tool.parameters
|
|
784
1006
|
}));
|
|
785
1007
|
}
|
|
786
|
-
if (
|
|
1008
|
+
if (inferenceInput.structuredOutput) {
|
|
787
1009
|
outputConfig.format = {
|
|
788
1010
|
type: "json_schema",
|
|
789
|
-
json_schema:
|
|
1011
|
+
json_schema: inferenceInput.structuredOutput.schema
|
|
790
1012
|
};
|
|
791
1013
|
}
|
|
792
|
-
if (
|
|
1014
|
+
if (inferenceInput.reasoningEffort) {
|
|
793
1015
|
request.thinking = { type: "adaptive" };
|
|
794
|
-
outputConfig.effort =
|
|
1016
|
+
outputConfig.effort = inferenceInput.reasoningEffort;
|
|
795
1017
|
}
|
|
796
1018
|
if (Object.keys(outputConfig).length > 0) {
|
|
797
1019
|
request.output_config = outputConfig;
|
|
798
1020
|
}
|
|
799
|
-
if (
|
|
800
|
-
request.stream =
|
|
1021
|
+
if (inferenceInput.streaming) {
|
|
1022
|
+
request.stream = inferenceInput.streaming;
|
|
801
1023
|
}
|
|
802
1024
|
return request;
|
|
803
1025
|
}
|
|
804
|
-
toResponse(response) {
|
|
805
|
-
const contextItems = this.extractContextItems(response);
|
|
806
|
-
const tokenUsage = this.extractTokenUsage(response);
|
|
807
|
-
return new InferenceResponse(contextItems, tokenUsage);
|
|
808
|
-
}
|
|
809
1026
|
addContentBlock(messages, role, block) {
|
|
810
1027
|
const lastMessage = messages[messages.length - 1];
|
|
811
1028
|
if ((lastMessage == null ? void 0 : lastMessage.role) === role) {
|
|
@@ -817,9 +1034,9 @@ var AnthropicMessagesMapper = class {
|
|
|
817
1034
|
content: [block]
|
|
818
1035
|
});
|
|
819
1036
|
}
|
|
820
|
-
mapContextItems(
|
|
1037
|
+
mapContextItems(inferenceInput) {
|
|
821
1038
|
var _a, _b, _c;
|
|
822
|
-
const context =
|
|
1039
|
+
const context = inferenceInput.context;
|
|
823
1040
|
const messages = [];
|
|
824
1041
|
const system = [];
|
|
825
1042
|
for (const item of context.getItems()) {
|
|
@@ -886,7 +1103,7 @@ var AnthropicMessagesMapper = class {
|
|
|
886
1103
|
new OutputTokenDetails(0)
|
|
887
1104
|
);
|
|
888
1105
|
}
|
|
889
|
-
|
|
1106
|
+
toResponse(response) {
|
|
890
1107
|
var _a;
|
|
891
1108
|
const items = [];
|
|
892
1109
|
for (const block of response.content) {
|
|
@@ -914,31 +1131,42 @@ var AnthropicMessagesMapper = class {
|
|
|
914
1131
|
);
|
|
915
1132
|
}
|
|
916
1133
|
}
|
|
917
|
-
return items;
|
|
1134
|
+
return { items, rowResponse: response };
|
|
918
1135
|
}
|
|
919
1136
|
};
|
|
920
1137
|
|
|
921
1138
|
// src/infrastructure/providers/anthropic/endpoints/anthropic-messages.ts
|
|
922
1139
|
var import_sdk = __toESM(require("@anthropic-ai/sdk"));
|
|
923
1140
|
var AnthropicMessages = class {
|
|
924
|
-
constructor(endpointMapper = new AnthropicMessagesMapper()) {
|
|
1141
|
+
constructor(endpointMapper = new AnthropicMessagesMapper(), config = {}) {
|
|
925
1142
|
this.endpointMapper = endpointMapper;
|
|
926
|
-
this.
|
|
1143
|
+
this.clientConfig = config;
|
|
927
1144
|
}
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
1145
|
+
get client() {
|
|
1146
|
+
var _a;
|
|
1147
|
+
return (_a = this._client) != null ? _a : this._client = new import_sdk.default({
|
|
1148
|
+
baseURL: this.clientConfig.baseURL,
|
|
1149
|
+
apiKey: this.clientConfig.apiKey
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
async infer(inferenceInput) {
|
|
1153
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1154
|
+
const response = await this.client.messages.create(request);
|
|
931
1155
|
return this.endpointMapper.toResponse(response);
|
|
932
1156
|
}
|
|
933
|
-
async *stream(
|
|
934
|
-
const
|
|
935
|
-
const stream =
|
|
1157
|
+
async *stream(inferenceInput) {
|
|
1158
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1159
|
+
const stream = this.client.messages.stream(request);
|
|
936
1160
|
for await (const event of stream) {
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
1161
|
+
yield event;
|
|
1162
|
+
}
|
|
1163
|
+
const output = this.endpointMapper.toResponse(await stream.finalMessage());
|
|
1164
|
+
yield {
|
|
1165
|
+
type: "inference.output",
|
|
1166
|
+
producerId: request.model,
|
|
1167
|
+
occurredAt: /* @__PURE__ */ new Date(),
|
|
1168
|
+
payload: output
|
|
1169
|
+
};
|
|
942
1170
|
}
|
|
943
1171
|
};
|
|
944
1172
|
|
|
@@ -959,27 +1187,27 @@ var SummaryText = class _SummaryText extends ItemContent {
|
|
|
959
1187
|
|
|
960
1188
|
// src/infrastructure/providers/openai/endpoints/openai-responses-mapper.ts
|
|
961
1189
|
var OpenAIResponsesMapper = class {
|
|
962
|
-
toRequest(
|
|
1190
|
+
toRequest(inferenceInput) {
|
|
963
1191
|
var _a, _b;
|
|
964
1192
|
const request = {
|
|
965
|
-
model:
|
|
966
|
-
input: this.mapContextItems(
|
|
1193
|
+
model: inferenceInput.model,
|
|
1194
|
+
input: this.mapContextItems(inferenceInput)
|
|
967
1195
|
};
|
|
968
|
-
if (
|
|
969
|
-
request.tools =
|
|
1196
|
+
if (inferenceInput.tools && inferenceInput.tools.length > 0) {
|
|
1197
|
+
request.tools = inferenceInput.tools.map((tool) => ({
|
|
970
1198
|
type: tool.type,
|
|
971
1199
|
name: tool.name,
|
|
972
1200
|
description: tool.description,
|
|
973
1201
|
parameters: tool.parameters
|
|
974
1202
|
}));
|
|
975
1203
|
}
|
|
976
|
-
if (
|
|
1204
|
+
if (inferenceInput.reasoningEffort) {
|
|
977
1205
|
request.reasoning = {
|
|
978
|
-
effort:
|
|
1206
|
+
effort: inferenceInput.reasoningEffort
|
|
979
1207
|
};
|
|
980
1208
|
}
|
|
981
|
-
if (
|
|
982
|
-
const format =
|
|
1209
|
+
if (inferenceInput.structuredOutput) {
|
|
1210
|
+
const format = inferenceInput.structuredOutput;
|
|
983
1211
|
request.text = {
|
|
984
1212
|
format: {
|
|
985
1213
|
type: "json_schema",
|
|
@@ -989,19 +1217,14 @@ var OpenAIResponsesMapper = class {
|
|
|
989
1217
|
}
|
|
990
1218
|
};
|
|
991
1219
|
}
|
|
992
|
-
if (
|
|
993
|
-
request.stream =
|
|
1220
|
+
if (inferenceInput.streaming) {
|
|
1221
|
+
request.stream = inferenceInput.streaming;
|
|
994
1222
|
}
|
|
995
1223
|
return request;
|
|
996
1224
|
}
|
|
997
|
-
|
|
998
|
-
const contextItems = this.extractContextItems(response);
|
|
999
|
-
const tokenUsage = this.extractTokenUsage(response);
|
|
1000
|
-
return new InferenceResponse(contextItems, tokenUsage);
|
|
1001
|
-
}
|
|
1002
|
-
mapContextItems(inferenceParams) {
|
|
1225
|
+
mapContextItems(inferenceInput) {
|
|
1003
1226
|
const input = [];
|
|
1004
|
-
for (const item of
|
|
1227
|
+
for (const item of inferenceInput.context.getItems()) {
|
|
1005
1228
|
if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem || item instanceof UserMessageItem) {
|
|
1006
1229
|
input.push({
|
|
1007
1230
|
type: item.type,
|
|
@@ -1061,7 +1284,7 @@ var OpenAIResponsesMapper = class {
|
|
|
1061
1284
|
new OutputTokenDetails((_d = (_c = response.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
|
|
1062
1285
|
);
|
|
1063
1286
|
}
|
|
1064
|
-
|
|
1287
|
+
toResponse(response) {
|
|
1065
1288
|
var _a, _b, _c;
|
|
1066
1289
|
const items = [];
|
|
1067
1290
|
for (const item of (_a = response.output) != null ? _a : []) {
|
|
@@ -1094,7 +1317,10 @@ var OpenAIResponsesMapper = class {
|
|
|
1094
1317
|
);
|
|
1095
1318
|
}
|
|
1096
1319
|
}
|
|
1097
|
-
return
|
|
1320
|
+
return {
|
|
1321
|
+
items,
|
|
1322
|
+
rowResponse: response
|
|
1323
|
+
};
|
|
1098
1324
|
}
|
|
1099
1325
|
};
|
|
1100
1326
|
|
|
@@ -1102,22 +1328,36 @@ var OpenAIResponsesMapper = class {
|
|
|
1102
1328
|
var OpenAIResponses = class {
|
|
1103
1329
|
constructor(endpointMapper = new OpenAIResponsesMapper()) {
|
|
1104
1330
|
this.endpointMapper = endpointMapper;
|
|
1105
|
-
this.client = new import_openai.default();
|
|
1106
1331
|
}
|
|
1107
|
-
|
|
1108
|
-
|
|
1332
|
+
get client() {
|
|
1333
|
+
var _a;
|
|
1334
|
+
return (_a = this._client) != null ? _a : this._client = new import_openai.default();
|
|
1335
|
+
}
|
|
1336
|
+
async infer(inferenceInput) {
|
|
1337
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1109
1338
|
const response = await this.client.responses.create(request);
|
|
1110
1339
|
return this.endpointMapper.toResponse(response);
|
|
1111
1340
|
}
|
|
1112
|
-
async *stream(
|
|
1113
|
-
const request = this.endpointMapper.toRequest(
|
|
1114
|
-
const
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1341
|
+
async *stream(inferenceInput) {
|
|
1342
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1343
|
+
const stream = await this.client.responses.create({ ...request, stream: true });
|
|
1344
|
+
let completedResponse = void 0;
|
|
1345
|
+
for await (const event of stream) {
|
|
1346
|
+
if (event.type === "response.completed") {
|
|
1347
|
+
completedResponse = event.response;
|
|
1118
1348
|
}
|
|
1119
|
-
yield
|
|
1349
|
+
yield event;
|
|
1120
1350
|
}
|
|
1351
|
+
if (!completedResponse) {
|
|
1352
|
+
throw new Error("Stream ended without a completed response");
|
|
1353
|
+
}
|
|
1354
|
+
const output = this.endpointMapper.toResponse(completedResponse);
|
|
1355
|
+
yield {
|
|
1356
|
+
type: "inference.output",
|
|
1357
|
+
payload: output,
|
|
1358
|
+
occurredAt: /* @__PURE__ */ new Date(),
|
|
1359
|
+
producerId: inferenceInput.model
|
|
1360
|
+
};
|
|
1121
1361
|
}
|
|
1122
1362
|
};
|
|
1123
1363
|
|
|
@@ -1126,16 +1366,16 @@ var import_genai = require("@google/genai");
|
|
|
1126
1366
|
|
|
1127
1367
|
// src/infrastructure/providers/gemini/endpoints/gemini-generate-content-mapper.ts
|
|
1128
1368
|
var GeminiGenerateContentMapper = class {
|
|
1129
|
-
toRequest(
|
|
1130
|
-
const { contents, systemInstruction } = this.mapContextItems(
|
|
1369
|
+
toRequest(inferenceInput) {
|
|
1370
|
+
const { contents, systemInstruction } = this.mapContextItems(inferenceInput);
|
|
1131
1371
|
const config = {};
|
|
1132
1372
|
if (systemInstruction) {
|
|
1133
1373
|
config.systemInstruction = systemInstruction;
|
|
1134
1374
|
}
|
|
1135
|
-
if (
|
|
1375
|
+
if (inferenceInput.tools && inferenceInput.tools.length > 0) {
|
|
1136
1376
|
config.tools = [
|
|
1137
1377
|
{
|
|
1138
|
-
functionDeclarations:
|
|
1378
|
+
functionDeclarations: inferenceInput.tools.map((tool) => ({
|
|
1139
1379
|
name: tool.name,
|
|
1140
1380
|
description: tool.description,
|
|
1141
1381
|
parametersJsonSchema: tool.parameters
|
|
@@ -1143,34 +1383,29 @@ var GeminiGenerateContentMapper = class {
|
|
|
1143
1383
|
}
|
|
1144
1384
|
];
|
|
1145
1385
|
}
|
|
1146
|
-
if (
|
|
1386
|
+
if (inferenceInput.structuredOutput) {
|
|
1147
1387
|
config.responseMimeType = "application/json";
|
|
1148
|
-
config.responseSchema =
|
|
1388
|
+
config.responseSchema = inferenceInput.structuredOutput.schema;
|
|
1149
1389
|
}
|
|
1150
|
-
if (
|
|
1390
|
+
if (inferenceInput.reasoningEffort) {
|
|
1151
1391
|
config.thinkingConfig = {
|
|
1152
|
-
thinkingLevel:
|
|
1392
|
+
thinkingLevel: inferenceInput.reasoningEffort,
|
|
1153
1393
|
includeThoughts: true
|
|
1154
1394
|
};
|
|
1155
1395
|
}
|
|
1156
1396
|
const request = {
|
|
1157
|
-
model:
|
|
1397
|
+
model: inferenceInput.model,
|
|
1158
1398
|
contents,
|
|
1159
1399
|
config
|
|
1160
1400
|
};
|
|
1161
|
-
if (
|
|
1162
|
-
request.stream =
|
|
1401
|
+
if (inferenceInput.streaming) {
|
|
1402
|
+
request.stream = inferenceInput.streaming;
|
|
1163
1403
|
}
|
|
1164
1404
|
return request;
|
|
1165
1405
|
}
|
|
1166
|
-
|
|
1167
|
-
const contextItems = this.extractContextItems(response);
|
|
1168
|
-
const tokenUsage = this.extractTokenUsage(response);
|
|
1169
|
-
return new InferenceResponse(contextItems, tokenUsage);
|
|
1170
|
-
}
|
|
1171
|
-
mapContextItems(inferenceParams) {
|
|
1406
|
+
mapContextItems(inferenceInput) {
|
|
1172
1407
|
var _a;
|
|
1173
|
-
const context =
|
|
1408
|
+
const context = inferenceInput.context;
|
|
1174
1409
|
const contents = [];
|
|
1175
1410
|
const system = [];
|
|
1176
1411
|
const callNames = /* @__PURE__ */ new Map();
|
|
@@ -1243,7 +1478,7 @@ var GeminiGenerateContentMapper = class {
|
|
|
1243
1478
|
new OutputTokenDetails((_e = usage.thoughtsTokenCount) != null ? _e : 0)
|
|
1244
1479
|
);
|
|
1245
1480
|
}
|
|
1246
|
-
|
|
1481
|
+
toResponse(response) {
|
|
1247
1482
|
var _a, _b, _c, _d, _e, _f;
|
|
1248
1483
|
const items = [];
|
|
1249
1484
|
const parts = (_d = (_c = (_b = (_a = response.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts) != null ? _d : [];
|
|
@@ -1272,42 +1507,58 @@ var GeminiGenerateContentMapper = class {
|
|
|
1272
1507
|
);
|
|
1273
1508
|
}
|
|
1274
1509
|
}
|
|
1275
|
-
return items;
|
|
1510
|
+
return { items, rowResponse: response };
|
|
1276
1511
|
}
|
|
1277
1512
|
};
|
|
1278
1513
|
|
|
1279
1514
|
// src/infrastructure/providers/gemini/endpoints/gemini-generate-content.ts
|
|
1280
1515
|
var GeminiGenerateContent = class {
|
|
1281
|
-
constructor(endpointMapper = new GeminiGenerateContentMapper()) {
|
|
1516
|
+
constructor(endpointMapper = new GeminiGenerateContentMapper(), config = {}) {
|
|
1282
1517
|
this.endpointMapper = endpointMapper;
|
|
1283
|
-
this.
|
|
1518
|
+
this.clientConfig = config;
|
|
1284
1519
|
}
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1520
|
+
get client() {
|
|
1521
|
+
var _a, _b;
|
|
1522
|
+
return (_b = this._client) != null ? _b : this._client = new import_genai.GoogleGenAI({
|
|
1523
|
+
apiKey: (_a = this.clientConfig.apiKey) != null ? _a : process.env.GEMINI_API_KEY,
|
|
1524
|
+
...this.clientConfig.baseURL ? { httpOptions: { baseUrl: this.clientConfig.baseURL } } : {}
|
|
1525
|
+
});
|
|
1526
|
+
}
|
|
1527
|
+
async infer(inferenceInput) {
|
|
1528
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1529
|
+
const response = await this.client.models.generateContent(request);
|
|
1288
1530
|
return this.endpointMapper.toResponse(response);
|
|
1289
1531
|
}
|
|
1290
|
-
async *stream(
|
|
1291
|
-
const
|
|
1292
|
-
const stream = await this.client.models.generateContentStream(
|
|
1532
|
+
async *stream(inferenceInput) {
|
|
1533
|
+
const request = this.endpointMapper.toRequest(inferenceInput);
|
|
1534
|
+
const stream = await this.client.models.generateContentStream(request);
|
|
1535
|
+
let lastEvent = void 0;
|
|
1293
1536
|
for await (const chunk of stream) {
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1537
|
+
lastEvent = chunk;
|
|
1538
|
+
yield chunk;
|
|
1539
|
+
}
|
|
1540
|
+
if (!lastEvent) {
|
|
1541
|
+
throw new Error("Last event not found");
|
|
1542
|
+
}
|
|
1543
|
+
const output = this.endpointMapper.toResponse(lastEvent);
|
|
1544
|
+
yield {
|
|
1545
|
+
type: "inference.output",
|
|
1546
|
+
producerId: request.model,
|
|
1547
|
+
occurredAt: /* @__PURE__ */ new Date(),
|
|
1548
|
+
payload: output
|
|
1549
|
+
};
|
|
1299
1550
|
}
|
|
1300
1551
|
};
|
|
1301
1552
|
|
|
1302
1553
|
// src/infrastructure/providers/openai/endpoints/openai-chat-completions-mapper.ts
|
|
1303
1554
|
var OpenAIChatCompletionsMapper = class {
|
|
1304
|
-
toRequest(
|
|
1555
|
+
toRequest(inferenceInput) {
|
|
1305
1556
|
const request = {
|
|
1306
|
-
model:
|
|
1307
|
-
messages: this.mapContextItems(
|
|
1557
|
+
model: inferenceInput.model,
|
|
1558
|
+
messages: this.mapContextItems(inferenceInput)
|
|
1308
1559
|
};
|
|
1309
|
-
if (
|
|
1310
|
-
request.tools =
|
|
1560
|
+
if (inferenceInput.tools && inferenceInput.tools.length > 0) {
|
|
1561
|
+
request.tools = inferenceInput.tools.map((tool) => ({
|
|
1311
1562
|
type: "function",
|
|
1312
1563
|
function: {
|
|
1313
1564
|
name: tool.name,
|
|
@@ -1316,23 +1567,18 @@ var OpenAIChatCompletionsMapper = class {
|
|
|
1316
1567
|
}
|
|
1317
1568
|
}));
|
|
1318
1569
|
}
|
|
1319
|
-
if (
|
|
1320
|
-
request.reasoning_effort =
|
|
1570
|
+
if (inferenceInput.reasoningEffort) {
|
|
1571
|
+
request.reasoning_effort = inferenceInput.reasoningEffort;
|
|
1321
1572
|
}
|
|
1322
|
-
if (
|
|
1323
|
-
request.stream =
|
|
1573
|
+
if (inferenceInput.streaming) {
|
|
1574
|
+
request.stream = inferenceInput.streaming;
|
|
1324
1575
|
}
|
|
1325
1576
|
return request;
|
|
1326
1577
|
}
|
|
1327
|
-
|
|
1328
|
-
const contextItems = this.extractContextItems(response);
|
|
1329
|
-
const tokenUsage = this.extractTokenUsage(response);
|
|
1330
|
-
return new InferenceResponse(contextItems, tokenUsage);
|
|
1331
|
-
}
|
|
1332
|
-
mapContextItems(inferenceParams) {
|
|
1578
|
+
mapContextItems(inferenceInput) {
|
|
1333
1579
|
var _a;
|
|
1334
1580
|
const messages = [];
|
|
1335
|
-
for (const item of
|
|
1581
|
+
for (const item of inferenceInput.context.getItems()) {
|
|
1336
1582
|
if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem) {
|
|
1337
1583
|
messages.push({ role: "system", content: item.content.text });
|
|
1338
1584
|
continue;
|
|
@@ -1379,12 +1625,12 @@ var OpenAIChatCompletionsMapper = class {
|
|
|
1379
1625
|
new OutputTokenDetails((_d = (_c = response.usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
|
|
1380
1626
|
);
|
|
1381
1627
|
}
|
|
1382
|
-
|
|
1628
|
+
toResponse(response) {
|
|
1383
1629
|
var _a, _b, _c;
|
|
1384
1630
|
const items = [];
|
|
1385
1631
|
const message = (_b = (_a = response.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
|
|
1386
1632
|
if (!message) {
|
|
1387
|
-
return items;
|
|
1633
|
+
return { items: [], rowResponse: response };
|
|
1388
1634
|
}
|
|
1389
1635
|
if (message.reasoning_content) {
|
|
1390
1636
|
items.push(
|
|
@@ -1407,7 +1653,7 @@ var OpenAIChatCompletionsMapper = class {
|
|
|
1407
1653
|
})
|
|
1408
1654
|
);
|
|
1409
1655
|
}
|
|
1410
|
-
return items;
|
|
1656
|
+
return { items, rowResponse: response };
|
|
1411
1657
|
}
|
|
1412
1658
|
};
|
|
1413
1659
|
|
|
@@ -1417,31 +1663,38 @@ var OpenAIChatCompletions = class {
|
|
|
1417
1663
|
constructor(endpointMapper = new OpenAIChatCompletionsMapper(), config = {}) {
|
|
1418
1664
|
var _a;
|
|
1419
1665
|
this.endpointMapper = endpointMapper;
|
|
1420
|
-
this.
|
|
1421
|
-
baseURL: config.baseURL,
|
|
1422
|
-
apiKey: config.apiKey
|
|
1423
|
-
});
|
|
1666
|
+
this.clientConfig = config;
|
|
1424
1667
|
this.extraBody = (_a = config.extraBody) != null ? _a : {};
|
|
1425
1668
|
}
|
|
1426
|
-
|
|
1669
|
+
get client() {
|
|
1670
|
+
var _a;
|
|
1671
|
+
return (_a = this._client) != null ? _a : this._client = new import_openai2.default({
|
|
1672
|
+
baseURL: this.clientConfig.baseURL,
|
|
1673
|
+
apiKey: this.clientConfig.apiKey
|
|
1674
|
+
});
|
|
1675
|
+
}
|
|
1676
|
+
buildRequest(inferenceInput) {
|
|
1427
1677
|
return {
|
|
1428
1678
|
...this.extraBody,
|
|
1429
|
-
...this.endpointMapper.toRequest(
|
|
1679
|
+
...this.endpointMapper.toRequest(inferenceInput)
|
|
1430
1680
|
};
|
|
1431
1681
|
}
|
|
1432
|
-
async infer(
|
|
1433
|
-
const response = await this.client.chat.completions.create(this.buildRequest(
|
|
1682
|
+
async infer(inferenceInput) {
|
|
1683
|
+
const response = await this.client.chat.completions.create(this.buildRequest(inferenceInput));
|
|
1434
1684
|
return this.endpointMapper.toResponse(response);
|
|
1435
1685
|
}
|
|
1436
|
-
async *stream(
|
|
1437
|
-
|
|
1438
|
-
const
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1686
|
+
async *stream(inferenceInput) {
|
|
1687
|
+
const stream = this.client.chat.completions.stream(this.buildRequest(inferenceInput));
|
|
1688
|
+
for await (const chunk of stream) {
|
|
1689
|
+
yield chunk;
|
|
1690
|
+
}
|
|
1691
|
+
const output = this.endpointMapper.toResponse(await stream.finalChatCompletion());
|
|
1692
|
+
yield {
|
|
1693
|
+
type: "inference.output",
|
|
1694
|
+
payload: output,
|
|
1695
|
+
occurredAt: /* @__PURE__ */ new Date(),
|
|
1696
|
+
producerId: inferenceInput.model
|
|
1697
|
+
};
|
|
1445
1698
|
}
|
|
1446
1699
|
};
|
|
1447
1700
|
|
|
@@ -1709,103 +1962,71 @@ var claudeHaiku45Specification = {
|
|
|
1709
1962
|
supportsStructuredOutput: true
|
|
1710
1963
|
};
|
|
1711
1964
|
|
|
1712
|
-
// src/
|
|
1713
|
-
var
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
endpoint: new AnthropicMessages(),
|
|
1762
|
-
specification: claudeOpus48Specification
|
|
1763
|
-
};
|
|
1764
|
-
break;
|
|
1765
|
-
case "gemini-3.5-flash":
|
|
1766
|
-
generativeModel = {
|
|
1767
|
-
endpoint: new GeminiGenerateContent(),
|
|
1768
|
-
specification: gemini35FlashSpecification
|
|
1769
|
-
};
|
|
1770
|
-
break;
|
|
1771
|
-
case "gemini-3.1-pro-preview":
|
|
1772
|
-
generativeModel = {
|
|
1773
|
-
endpoint: new GeminiGenerateContent(),
|
|
1774
|
-
specification: gemini31ProSpecification
|
|
1775
|
-
};
|
|
1776
|
-
break;
|
|
1777
|
-
case "deepseek-v4-flash":
|
|
1778
|
-
generativeModel = {
|
|
1779
|
-
endpoint: new OpenAIChatCompletions(),
|
|
1780
|
-
specification: deepSeekV4FlashSpecification
|
|
1781
|
-
};
|
|
1782
|
-
break;
|
|
1783
|
-
case "deepseek-v4-pro":
|
|
1784
|
-
generativeModel = {
|
|
1785
|
-
endpoint: new OpenAIChatCompletions(),
|
|
1786
|
-
specification: deepSeekV4ProSpecification
|
|
1787
|
-
};
|
|
1788
|
-
}
|
|
1789
|
-
if (!generativeModel) {
|
|
1790
|
-
throw new Error(`Unsupported model: ${modelName}`);
|
|
1791
|
-
}
|
|
1792
|
-
return Promise.resolve(generativeModel);
|
|
1965
|
+
// src/application/services/models.ts
|
|
1966
|
+
var supportedModels = [
|
|
1967
|
+
{
|
|
1968
|
+
endpoint: new OpenAIResponses(),
|
|
1969
|
+
specification: gpt54Specification
|
|
1970
|
+
},
|
|
1971
|
+
{
|
|
1972
|
+
endpoint: new OpenAIResponses(),
|
|
1973
|
+
specification: gpt54MiniSpecification
|
|
1974
|
+
},
|
|
1975
|
+
{
|
|
1976
|
+
endpoint: new OpenAIResponses(),
|
|
1977
|
+
specification: gpt54NanoSpecification
|
|
1978
|
+
},
|
|
1979
|
+
{
|
|
1980
|
+
endpoint: new OpenAIResponses(),
|
|
1981
|
+
specification: gpt55Specification
|
|
1982
|
+
},
|
|
1983
|
+
{
|
|
1984
|
+
endpoint: new AnthropicMessages(),
|
|
1985
|
+
specification: claudeHaiku45Specification
|
|
1986
|
+
},
|
|
1987
|
+
{
|
|
1988
|
+
endpoint: new AnthropicMessages(),
|
|
1989
|
+
specification: claudeSonnet46Specification
|
|
1990
|
+
},
|
|
1991
|
+
{
|
|
1992
|
+
endpoint: new AnthropicMessages(),
|
|
1993
|
+
specification: claudeOpus47Specification
|
|
1994
|
+
},
|
|
1995
|
+
{
|
|
1996
|
+
endpoint: new AnthropicMessages(),
|
|
1997
|
+
specification: claudeOpus48Specification
|
|
1998
|
+
},
|
|
1999
|
+
{
|
|
2000
|
+
endpoint: new GeminiGenerateContent(),
|
|
2001
|
+
specification: gemini35FlashSpecification
|
|
2002
|
+
},
|
|
2003
|
+
{
|
|
2004
|
+
endpoint: new GeminiGenerateContent(),
|
|
2005
|
+
specification: gemini31ProSpecification
|
|
2006
|
+
},
|
|
2007
|
+
{
|
|
2008
|
+
endpoint: new OpenAIChatCompletions(),
|
|
2009
|
+
specification: deepSeekV4FlashSpecification
|
|
2010
|
+
},
|
|
2011
|
+
{
|
|
2012
|
+
endpoint: new OpenAIChatCompletions(),
|
|
2013
|
+
specification: deepSeekV4ProSpecification
|
|
1793
2014
|
}
|
|
1794
|
-
|
|
2015
|
+
];
|
|
1795
2016
|
|
|
1796
2017
|
// src/domain/generative-model/request-validation/reasoning-effort.ts
|
|
1797
2018
|
var ReasoningEffortValidation = class {
|
|
1798
2019
|
constructor() {
|
|
1799
2020
|
this.name = "reasoning-effort";
|
|
1800
2021
|
}
|
|
1801
|
-
isValid(
|
|
1802
|
-
if (
|
|
2022
|
+
isValid(inferenceInput, model) {
|
|
2023
|
+
if (inferenceInput.reasoningEffort === void 0) {
|
|
1803
2024
|
return true;
|
|
1804
2025
|
}
|
|
1805
2026
|
if (!model.supportsReasoningEffort) {
|
|
1806
2027
|
return false;
|
|
1807
2028
|
}
|
|
1808
|
-
return model.supportedReasoningEfforts.includes(
|
|
2029
|
+
return model.supportedReasoningEfforts.includes(inferenceInput.reasoningEffort);
|
|
1809
2030
|
}
|
|
1810
2031
|
};
|
|
1811
2032
|
|
|
@@ -1814,8 +2035,8 @@ var ToolCallingValidation = class {
|
|
|
1814
2035
|
constructor() {
|
|
1815
2036
|
this.name = "tool-calling";
|
|
1816
2037
|
}
|
|
1817
|
-
isValid(
|
|
1818
|
-
if (
|
|
2038
|
+
isValid(inferenceInput, model) {
|
|
2039
|
+
if (inferenceInput.tools === void 0) {
|
|
1819
2040
|
return true;
|
|
1820
2041
|
}
|
|
1821
2042
|
return model.supportsFunctionCalling;
|
|
@@ -1827,8 +2048,8 @@ var StreamingValidation = class {
|
|
|
1827
2048
|
constructor() {
|
|
1828
2049
|
this.name = "streaming";
|
|
1829
2050
|
}
|
|
1830
|
-
isValid(
|
|
1831
|
-
if (!
|
|
2051
|
+
isValid(inferenceInput, model) {
|
|
2052
|
+
if (!inferenceInput.streaming) {
|
|
1832
2053
|
return true;
|
|
1833
2054
|
}
|
|
1834
2055
|
return model.supportsStreaming;
|
|
@@ -1855,8 +2076,8 @@ var ContextValidation = class {
|
|
|
1855
2076
|
constructor() {
|
|
1856
2077
|
this.name = "context";
|
|
1857
2078
|
}
|
|
1858
|
-
isValid(
|
|
1859
|
-
return
|
|
2079
|
+
isValid(inferenceInput, model) {
|
|
2080
|
+
return inferenceInput.context.items.every(
|
|
1860
2081
|
(item) => model.supportedContextItemTypes.includes(getContextItemValidationKey(item))
|
|
1861
2082
|
);
|
|
1862
2083
|
}
|
|
@@ -1870,59 +2091,188 @@ var defaultRequestValidationRules = [
|
|
|
1870
2091
|
new StructuredOutputValidation(),
|
|
1871
2092
|
new ContextValidation()
|
|
1872
2093
|
];
|
|
1873
|
-
var
|
|
2094
|
+
var InferenceInputValidator = class {
|
|
1874
2095
|
constructor(rules = defaultRequestValidationRules) {
|
|
1875
2096
|
this.rules = rules;
|
|
1876
2097
|
}
|
|
1877
|
-
validate(
|
|
2098
|
+
validate(inferenceInput, model) {
|
|
1878
2099
|
for (const rule of this.rules) {
|
|
1879
|
-
if (!rule.isValid(
|
|
2100
|
+
if (!rule.isValid(inferenceInput, model)) {
|
|
1880
2101
|
throw new Error(`Request validation "${rule.name}" failed for model "${model.name}"`);
|
|
1881
2102
|
}
|
|
1882
2103
|
}
|
|
1883
2104
|
}
|
|
1884
2105
|
};
|
|
1885
2106
|
|
|
1886
|
-
// src/
|
|
1887
|
-
var
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
2107
|
+
// src/application/services/inference-runner.ts
|
|
2108
|
+
var DefaultInferenceRunner = class {
|
|
2109
|
+
constructor(supportedModels2, requestValidator) {
|
|
2110
|
+
this.supportedModels = supportedModels2;
|
|
2111
|
+
this.requestValidator = requestValidator;
|
|
2112
|
+
}
|
|
2113
|
+
async run(input) {
|
|
2114
|
+
const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
|
|
2115
|
+
if (!generativeModel) {
|
|
2116
|
+
throw new Error(`Unsupported model: ${input.model}`);
|
|
2117
|
+
}
|
|
2118
|
+
this.requestValidator.validate(input, generativeModel.specification);
|
|
2119
|
+
return await generativeModel.endpoint.infer(input);
|
|
2120
|
+
}
|
|
2121
|
+
async *stream(input) {
|
|
2122
|
+
const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
|
|
2123
|
+
if (!generativeModel) {
|
|
2124
|
+
throw new Error(`Unsupported model: ${input.model}`);
|
|
2125
|
+
}
|
|
2126
|
+
this.requestValidator.validate(input, generativeModel.specification);
|
|
2127
|
+
yield* generativeModel.endpoint.stream(input);
|
|
2128
|
+
}
|
|
1894
2129
|
};
|
|
1895
|
-
|
|
1896
|
-
|
|
2130
|
+
|
|
2131
|
+
// src/application/services/function-call.ts
|
|
2132
|
+
var DefaultFunctionCallRunner = class {
|
|
2133
|
+
async run(input, tool) {
|
|
2134
|
+
try {
|
|
2135
|
+
const result = await tool.invoke(JSON.parse(input.args));
|
|
2136
|
+
return FunctionCallOutputItem.create(input.callId, JSON.stringify(result));
|
|
2137
|
+
} catch (error) {
|
|
2138
|
+
return FunctionCallOutputItem.create(input.callId, `Error calling tool: ${error}`);
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
1897
2141
|
};
|
|
1898
|
-
|
|
1899
|
-
|
|
2142
|
+
|
|
2143
|
+
// src/application/use-cases/send-event.ts
|
|
2144
|
+
function createSendEvent(resolveRuntime) {
|
|
2145
|
+
return function sendEvent(event, senderId) {
|
|
2146
|
+
const runtime = resolveRuntime();
|
|
2147
|
+
const participant = runtime.getParticipant(senderId);
|
|
2148
|
+
if (!participant) {
|
|
2149
|
+
throw new Error(`Participant ${senderId} not found`);
|
|
2150
|
+
}
|
|
2151
|
+
runtime.publish(event);
|
|
2152
|
+
};
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
// src/define-runtime.ts
|
|
2156
|
+
function defineRuntime() {
|
|
2157
|
+
let runtime = null;
|
|
2158
|
+
const processor = new EventProcessor();
|
|
2159
|
+
function initializeRuntime(config) {
|
|
2160
|
+
var _a, _b, _c, _d;
|
|
2161
|
+
if (runtime) {
|
|
2162
|
+
throw new Error("Runtime already initialized");
|
|
2163
|
+
}
|
|
2164
|
+
const inferenceRunner = (_d = (_a = config.inferenceRunnerConfig) == null ? void 0 : _a.runner) != null ? _d : new DefaultInferenceRunner(
|
|
2165
|
+
(_c = (_b = config.inferenceRunnerConfig) == null ? void 0 : _b.supportedModels) != null ? _c : supportedModels,
|
|
2166
|
+
new InferenceInputValidator()
|
|
2167
|
+
);
|
|
2168
|
+
const functionCallRunner = new DefaultFunctionCallRunner();
|
|
2169
|
+
runtime = new RuntimeService(config.state, processor, inferenceRunner, functionCallRunner);
|
|
2170
|
+
return runtime;
|
|
2171
|
+
}
|
|
2172
|
+
function resolveRuntime() {
|
|
2173
|
+
if (!runtime) {
|
|
2174
|
+
throw new Error("Runtime not initialized");
|
|
2175
|
+
}
|
|
2176
|
+
return runtime;
|
|
2177
|
+
}
|
|
2178
|
+
function resolveParticipant(id) {
|
|
2179
|
+
if (!runtime) {
|
|
2180
|
+
throw new Error("Runtime not initialized");
|
|
2181
|
+
}
|
|
2182
|
+
const participant = runtime.state.getParticipant(id);
|
|
2183
|
+
if (!participant) {
|
|
2184
|
+
throw new Error(`Participant ${id} not found`);
|
|
2185
|
+
}
|
|
2186
|
+
return participant;
|
|
2187
|
+
}
|
|
2188
|
+
const join = createJoin(resolveRuntime);
|
|
2189
|
+
const leave = createLeave(resolveRuntime);
|
|
2190
|
+
const sendMessage = createSendMessage(resolveRuntime);
|
|
2191
|
+
const sendEvent = createSendEvent(resolveRuntime);
|
|
2192
|
+
const runLoop = createRunLoop(resolveRuntime);
|
|
2193
|
+
return {
|
|
2194
|
+
initializeRuntime,
|
|
2195
|
+
resolveRuntime,
|
|
2196
|
+
resolveParticipant,
|
|
2197
|
+
join,
|
|
2198
|
+
leave,
|
|
2199
|
+
sendMessage,
|
|
2200
|
+
sendEvent,
|
|
2201
|
+
runLoop
|
|
2202
|
+
};
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
// src/domain/agentic-environment/situation/situation-specification.ts
|
|
2206
|
+
var SituationSpecification = class {
|
|
2207
|
+
and(other) {
|
|
2208
|
+
return new AndSituationSpecification(this, other);
|
|
2209
|
+
}
|
|
2210
|
+
or(other) {
|
|
2211
|
+
return new OrSituationSpecification(this, other);
|
|
2212
|
+
}
|
|
2213
|
+
not() {
|
|
2214
|
+
return new NotSituationSpecification(this);
|
|
2215
|
+
}
|
|
2216
|
+
};
|
|
2217
|
+
var AndSituationSpecification = class extends SituationSpecification {
|
|
2218
|
+
constructor(left, right) {
|
|
2219
|
+
super();
|
|
2220
|
+
this.left = left;
|
|
2221
|
+
this.right = right;
|
|
2222
|
+
}
|
|
2223
|
+
isSatisfiedBy(situationContext) {
|
|
2224
|
+
return this.left.isSatisfiedBy(situationContext) && this.right.isSatisfiedBy(situationContext);
|
|
2225
|
+
}
|
|
2226
|
+
};
|
|
2227
|
+
var OrSituationSpecification = class extends SituationSpecification {
|
|
2228
|
+
constructor(left, right) {
|
|
2229
|
+
super();
|
|
2230
|
+
this.left = left;
|
|
2231
|
+
this.right = right;
|
|
2232
|
+
}
|
|
2233
|
+
isSatisfiedBy(situationContext) {
|
|
2234
|
+
return this.left.isSatisfiedBy(situationContext) || this.right.isSatisfiedBy(situationContext);
|
|
2235
|
+
}
|
|
2236
|
+
};
|
|
2237
|
+
var NotSituationSpecification = class extends SituationSpecification {
|
|
2238
|
+
constructor(rule) {
|
|
2239
|
+
super();
|
|
2240
|
+
this.rule = rule;
|
|
2241
|
+
}
|
|
2242
|
+
isSatisfiedBy(situationContext) {
|
|
2243
|
+
return !this.rule.isSatisfiedBy(situationContext);
|
|
2244
|
+
}
|
|
1900
2245
|
};
|
|
1901
2246
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1902
2247
|
0 && (module.exports = {
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
BaseParticipant,
|
|
2248
|
+
Agent,
|
|
2249
|
+
AnthropicMessages,
|
|
1906
2250
|
ContextItem,
|
|
2251
|
+
DefaultInferenceRunner,
|
|
1907
2252
|
DeveloperMessageItem,
|
|
1908
2253
|
FunctionCallItem,
|
|
1909
2254
|
FunctionCallOutputItem,
|
|
1910
|
-
|
|
1911
|
-
|
|
2255
|
+
GeminiGenerateContent,
|
|
2256
|
+
Human,
|
|
1912
2257
|
InputTokenDetails,
|
|
1913
2258
|
McpClient,
|
|
1914
2259
|
McpToolRegistry,
|
|
1915
2260
|
ModelContext,
|
|
1916
2261
|
ModelMessageItem,
|
|
2262
|
+
OpenAIChatCompletions,
|
|
2263
|
+
OpenAIResponses,
|
|
1917
2264
|
OutputTokenDetails,
|
|
1918
2265
|
Participant,
|
|
1919
2266
|
ReasoningItem,
|
|
2267
|
+
RuntimeState,
|
|
1920
2268
|
SemanticEvent,
|
|
2269
|
+
SituationSpecification,
|
|
1921
2270
|
SystemMessageItem,
|
|
1922
2271
|
TokenUsage,
|
|
1923
2272
|
UserMessageItem,
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
2273
|
+
createAgent,
|
|
2274
|
+
createHuman,
|
|
2275
|
+
defineRuntime,
|
|
2276
|
+
supportedModels
|
|
1927
2277
|
});
|
|
1928
2278
|
//# sourceMappingURL=index.js.map
|