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