@mozaik-ai/core 4.2.0-beta.1 → 4.2.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -365,10 +365,2219 @@ var NotSituationSpecification = class extends SituationSpecification {
365
365
  return !this.rule.isSatisfiedBy(situationContext);
366
366
  }
367
367
  };
368
+
369
+ // src/domain/agent/agent.ts
370
+ var Agent = class _Agent extends Participant {
371
+ constructor(manifest, tools, memory, handlers) {
372
+ super(manifest, handlers);
373
+ this.memory = memory;
374
+ this.tools = tools;
375
+ }
376
+ getTools() {
377
+ return this.tools;
378
+ }
379
+ getMemory() {
380
+ return this.memory;
381
+ }
382
+ static create({
383
+ manifest,
384
+ tools,
385
+ memory,
386
+ handlers
387
+ }) {
388
+ return new _Agent(manifest, tools, memory, handlers);
389
+ }
390
+ static rehydrate(record) {
391
+ return new _Agent(record.manifest, record.tools, record.memory, record.handlers);
392
+ }
393
+ };
394
+
395
+ // src/application/use-cases/create-agent.ts
396
+ var CreateAgentUseCase = class {
397
+ constructor(agentRepository, memoryFactory) {
398
+ this.agentRepository = agentRepository;
399
+ this.memoryFactory = memoryFactory;
400
+ }
401
+ async execute(name, instruction, tools, handlers) {
402
+ const id = UuidGenerator.create();
403
+ const manifest = { id, name, role: "agent" };
404
+ const memory = this.memoryFactory.create();
405
+ const developerMessageItem = {
406
+ type: "developer_message",
407
+ text: instruction
408
+ };
409
+ memory.saveItem(developerMessageItem);
410
+ const agentRecord = { id, manifest, tools, memory, handlers };
411
+ const agent = Agent.create(agentRecord);
412
+ await this.agentRepository.save(agent);
413
+ return agentRecord;
414
+ }
415
+ };
416
+
417
+ // src/infrastructure/repositories/in-memory-agent-repository.ts
418
+ var InMemoryAgentRepository = class {
419
+ constructor() {
420
+ this.agents = [];
421
+ }
422
+ exists(id) {
423
+ return Promise.resolve(this.agents.some((agent) => agent.getManifest().id === id));
424
+ }
425
+ findById(id) {
426
+ return Promise.resolve(this.agents.find((agent) => agent.getManifest().id === id));
427
+ }
428
+ findAll() {
429
+ return Promise.resolve(this.agents);
430
+ }
431
+ async save(agent) {
432
+ this.agents.push(agent);
433
+ }
434
+ };
435
+
436
+ // src/domain/agent/loop/loop.ts
437
+ var Loop = class _Loop {
438
+ constructor(loopId, agentId, subject, createdAt, state, inferenceRequest, pendingOperation, transitionHistory, operationHistory, rules) {
439
+ this.loopId = loopId;
440
+ this.agentId = agentId;
441
+ this.subject = subject;
442
+ this.createdAt = createdAt;
443
+ this.state = state;
444
+ this.inferenceRequest = inferenceRequest;
445
+ this.pendingOperation = pendingOperation;
446
+ this.transitionHistory = transitionHistory;
447
+ this.operationHistory = operationHistory;
448
+ this.loopRules = rules;
449
+ }
450
+ get id() {
451
+ return this.loopId;
452
+ }
453
+ get stateId() {
454
+ return this.state;
455
+ }
456
+ get pending() {
457
+ return this.pendingOperation;
458
+ }
459
+ get history() {
460
+ return this.transitionHistory;
461
+ }
462
+ get completedOperations() {
463
+ return this.operationHistory;
464
+ }
465
+ get request() {
466
+ return this.inferenceRequest;
467
+ }
468
+ get rules() {
469
+ return this.loopRules;
470
+ }
471
+ getAgentId() {
472
+ return this.agentId;
473
+ }
474
+ record() {
475
+ return {
476
+ id: this.id,
477
+ agentId: this.agentId,
478
+ subject: this.subject,
479
+ createdAt: this.createdAt,
480
+ state: this.state,
481
+ inferenceRequest: this.inferenceRequest,
482
+ pendingOperation: this.pendingOperation,
483
+ transitionHistory: [...this.transitionHistory],
484
+ operationHistory: [...this.operationHistory],
485
+ rules: [...this.rules]
486
+ };
487
+ }
488
+ moveToIdle(reason, transitionId) {
489
+ this.transitionTo("idle", reason, transitionId);
490
+ this.pendingOperation = void 0;
491
+ }
492
+ moveToAwaitingInference(operation) {
493
+ this.assertAwaitingInferenceRequest();
494
+ this.pendingOperation = operation;
495
+ this.transitionTo("awaiting_inference", "inference_requested", operation.id);
496
+ }
497
+ moveToAwaitingToolOutput(operation, occurredAt) {
498
+ this.assertIdle();
499
+ this.pendingOperation = operation;
500
+ this.transitionTo("awaiting_tool_output", "tool_execution_requested", operation.id);
501
+ }
502
+ requestInference(request) {
503
+ this.assertAwaitingInferenceRequest();
504
+ this.inferenceRequest = request;
505
+ const operation = {
506
+ id: UuidGenerator.create(),
507
+ type: "inference",
508
+ requestedAt: SystemClock.now(),
509
+ request: this.inferenceRequest
510
+ };
511
+ this.moveToAwaitingInference(operation);
512
+ return operation;
513
+ }
514
+ requestToolUse(operationId, call, requestedAt) {
515
+ this.assertIdle();
516
+ const operation = {
517
+ id: operationId,
518
+ type: "tool_execution",
519
+ requestedAt,
520
+ call
521
+ };
522
+ this.moveToAwaitingToolOutput(operation, requestedAt);
523
+ return operation;
524
+ }
525
+ transitionTo(nextState, reason, operationId) {
526
+ const occurredAt = SystemClock.now();
527
+ const previousState = this.state;
528
+ this.state = nextState;
529
+ this.transitionHistory.push({
530
+ occurredAt,
531
+ previousState,
532
+ nextState,
533
+ reason,
534
+ operationId
535
+ });
536
+ }
537
+ assertIdle() {
538
+ if (this.state !== "idle") {
539
+ throw new Error(`Expected idle loop, but loop is ${this.state}`);
540
+ }
541
+ }
542
+ assertAwaitingInferenceRequest() {
543
+ if (this.state !== "awaiting_inference_request") {
544
+ throw new Error(`Expected awaiting inference request loop, but loop is ${this.state}`);
545
+ }
546
+ }
547
+ receiveInferenceResult(operationId, result) {
548
+ if (this.state !== "awaiting_inference") {
549
+ throw new Error(`Cannot receive inference result while loop is ${this.state}`);
550
+ }
551
+ const operation = this.pendingOperation;
552
+ if ((operation == null ? void 0 : operation.type) !== "inference" || operation.id !== operationId) {
553
+ throw new Error(`Inference result does not match pending operation ${operationId}`);
554
+ }
555
+ if (!this.inferenceRequest) {
556
+ throw new Error("Inference request is not provided");
557
+ }
558
+ this.moveToIdle("inference_completed", operationId);
559
+ this.operationHistory.push({
560
+ type: "inference",
561
+ operationId,
562
+ requestedAt: operation.requestedAt,
563
+ completedAt: SystemClock.now(),
564
+ request: operation.request,
565
+ result
566
+ });
567
+ this.inferenceRequest.context.items.push(...result.items);
568
+ const call = result.items.find((item) => item.type === "tool_use_request");
569
+ if (call) {
570
+ this.pendingOperation = {
571
+ id: call.requestId,
572
+ type: "tool_execution",
573
+ requestedAt: SystemClock.now(),
574
+ call
575
+ };
576
+ this.transitionTo("awaiting_tool_output", "tool_use_requested", call.requestId);
577
+ return;
578
+ }
579
+ this.pendingOperation = void 0;
580
+ }
581
+ receiveToolUseResult(operationId, result) {
582
+ if (this.state !== "awaiting_tool_output") {
583
+ throw new Error(`Cannot receive tool output while loop is ${this.state}`);
584
+ }
585
+ const operation = this.pendingOperation;
586
+ if ((operation == null ? void 0 : operation.type) !== "tool_execution" || operation.id !== operationId) {
587
+ throw new Error(`Tool output does not match pending operation ${operationId}`);
588
+ }
589
+ if (!this.inferenceRequest) {
590
+ throw new Error("Inference request is not provided");
591
+ }
592
+ this.operationHistory.push({
593
+ type: "tool_use",
594
+ operationId,
595
+ requestedAt: operation.requestedAt,
596
+ completedAt: SystemClock.now(),
597
+ call: operation.call,
598
+ result
599
+ });
600
+ this.inferenceRequest.context.items.push(result);
601
+ this.pendingOperation = void 0;
602
+ this.transitionTo("awaiting_inference_request", "tool_use_completed", operationId);
603
+ }
604
+ complete(reason) {
605
+ this.assertNotSettled("complete");
606
+ this.pendingOperation = void 0;
607
+ this.transitionTo("completed", reason);
608
+ }
609
+ stop(reason) {
610
+ this.assertNotSettled("stop");
611
+ this.pendingOperation = void 0;
612
+ this.transitionTo("stopped", reason);
613
+ }
614
+ wait(reason) {
615
+ this.assertNotSettled("wait");
616
+ this.pendingOperation = void 0;
617
+ this.transitionTo("idle", reason);
618
+ }
619
+ assertNotSettled(intent) {
620
+ if (this.state === "completed" || this.state === "stopped") {
621
+ throw new Error(`Cannot ${intent} a loop that is already ${this.state}`);
622
+ }
623
+ }
624
+ static create(id, agentId, subject, rules) {
625
+ const createdAt = SystemClock.now();
626
+ return new _Loop(
627
+ id,
628
+ agentId,
629
+ subject,
630
+ createdAt,
631
+ "awaiting_inference_request",
632
+ void 0,
633
+ void 0,
634
+ [],
635
+ [],
636
+ rules
637
+ );
638
+ }
639
+ static rehydrate(record) {
640
+ return new _Loop(
641
+ record.id,
642
+ record.agentId,
643
+ record.subject,
644
+ record.createdAt,
645
+ record.state,
646
+ record.inferenceRequest,
647
+ record.pendingOperation,
648
+ [...record.transitionHistory],
649
+ [...record.operationHistory],
650
+ [...record.rules]
651
+ );
652
+ }
653
+ };
654
+
655
+ // src/application/use-cases/create-loop.ts
656
+ var CreateAgentLoopUseCase = class {
657
+ constructor(loopRepository) {
658
+ this.loopRepository = loopRepository;
659
+ }
660
+ async execute(subject, agentId, rules) {
661
+ const loop = Loop.create(UuidGenerator.create(), agentId, subject, rules);
662
+ await this.loopRepository.save(loop);
663
+ return loop;
664
+ }
665
+ };
666
+
667
+ // src/infrastructure/repositories/in-memory-loop-repository.ts
668
+ var InMemoryLoopRepository = class {
669
+ constructor() {
670
+ this.loops = [];
671
+ }
672
+ findById(id) {
673
+ return Promise.resolve(this.loops.find((loop) => loop.id === id));
674
+ }
675
+ save(loop) {
676
+ const index = this.loops.findIndex((stored) => stored.id === loop.id);
677
+ if (index === -1) {
678
+ this.loops.push(loop);
679
+ } else {
680
+ this.loops[index] = loop;
681
+ }
682
+ return Promise.resolve();
683
+ }
684
+ };
685
+
686
+ // src/domain/inference/tool-use-item-factory.ts
687
+ var ToolUseItemFactory = class {
688
+ create(toolUseRequest, result) {
689
+ const toolUseResult = {
690
+ type: "tool_use_result",
691
+ requestId: toolUseRequest.requestId,
692
+ result: {
693
+ type: "input_text",
694
+ text: result
695
+ }
696
+ };
697
+ return toolUseResult;
698
+ }
699
+ };
700
+
701
+ // src/application/runners/local-tool-runner.ts
702
+ var LocalToolRunner = class {
703
+ constructor() {
704
+ this.toolUseItemFactory = new ToolUseItemFactory();
705
+ }
706
+ async run(input, tool) {
707
+ try {
708
+ const result = await tool.invoke(JSON.parse(input.toolArguments));
709
+ const resultText = typeof result === "string" ? result : JSON.stringify(result);
710
+ return this.toolUseItemFactory.create(input, resultText);
711
+ } catch (error) {
712
+ const errorText = `Error calling tool: ${error}`;
713
+ return this.toolUseItemFactory.create(input, errorText);
714
+ }
715
+ }
716
+ };
717
+
718
+ // src/application/runners/inference-runner.ts
719
+ var DefaultInferenceRunner = class {
720
+ constructor(supportedModels2, requestValidator) {
721
+ this.supportedModels = supportedModels2;
722
+ this.requestValidator = requestValidator;
723
+ }
724
+ async run(input) {
725
+ const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
726
+ if (!generativeModel) {
727
+ throw new Error(`Unsupported model: ${input.model}`);
728
+ }
729
+ this.requestValidator.validate(input, generativeModel.specification);
730
+ return await generativeModel.endpoint.infer(input);
731
+ }
732
+ async *stream(input) {
733
+ const generativeModel = this.supportedModels.find((model) => model.specification.name === input.model);
734
+ if (!generativeModel) {
735
+ throw new Error(`Unsupported model: ${input.model}`);
736
+ }
737
+ this.requestValidator.validate(input, generativeModel.specification);
738
+ yield* generativeModel.endpoint.stream(input);
739
+ }
740
+ };
741
+
742
+ // src/domain/inference/request-validation/reasoning-effort.ts
743
+ var ReasoningEffortValidation = class {
744
+ constructor() {
745
+ this.name = "reasoning-effort";
746
+ }
747
+ isValid(inferenceRequest, model) {
748
+ if (inferenceRequest.reasoningEffort === void 0) {
749
+ return true;
750
+ }
751
+ if (!model.supportsReasoningEffort) {
752
+ return false;
753
+ }
754
+ return model.supportedReasoningEfforts.includes(inferenceRequest.reasoningEffort);
755
+ }
756
+ };
757
+
758
+ // src/domain/inference/request-validation/tool-calling.ts
759
+ var ToolUseRequestingValidation = class {
760
+ constructor() {
761
+ this.name = "tool-calling";
762
+ }
763
+ isValid(inferenceRequest, model) {
764
+ if (inferenceRequest.tools === void 0) {
765
+ return true;
766
+ }
767
+ return model.supportsFunctionCalling;
768
+ }
769
+ };
770
+
771
+ // src/domain/inference/request-validation/streaming.ts
772
+ var StreamingValidation = class {
773
+ constructor() {
774
+ this.name = "streaming";
775
+ }
776
+ isValid(inferenceRequest, model) {
777
+ if (!inferenceRequest.streaming) {
778
+ return true;
779
+ }
780
+ return model.supportsStreaming;
781
+ }
782
+ };
783
+
784
+ // src/domain/inference/request-validation/structured-output.ts
785
+ var StructuredOutputValidation = class {
786
+ constructor() {
787
+ this.name = "structured-output";
788
+ }
789
+ isValid(inferenceRequest, model) {
790
+ if (inferenceRequest.structuredOutput === void 0) {
791
+ return true;
792
+ }
793
+ return model.supportsStructuredOutput;
794
+ }
795
+ };
796
+
797
+ // src/domain/inference/request-validation/context.ts
798
+ function getContextItemValidationKey(item) {
799
+ if (item.type === "user_message") {
800
+ return "user_message";
801
+ }
802
+ if (item.type === "system_message") {
803
+ return "system_message";
804
+ }
805
+ if (item.type === "developer_message") {
806
+ return "developer_message";
807
+ }
808
+ if (item.type === "model_message") {
809
+ return "model_message";
810
+ }
811
+ return item.type;
812
+ }
813
+ var ContextValidation = class {
814
+ constructor() {
815
+ this.name = "context";
816
+ }
817
+ isValid(inferenceRequest, model) {
818
+ return inferenceRequest.context.items.every(
819
+ (item) => model.supportedContextItemTypes.includes(getContextItemValidationKey(item))
820
+ );
821
+ }
822
+ };
823
+
824
+ // src/domain/inference/request-validation/inference-request-validator.ts
825
+ var defaultRequestValidationRules = [
826
+ new ReasoningEffortValidation(),
827
+ new ToolUseRequestingValidation(),
828
+ new StreamingValidation(),
829
+ new StructuredOutputValidation(),
830
+ new ContextValidation()
831
+ ];
832
+ var InferenceRequestValidator = class {
833
+ constructor(rules = defaultRequestValidationRules) {
834
+ this.rules = rules;
835
+ }
836
+ validate(inferenceRequest, model) {
837
+ for (const rule of this.rules) {
838
+ if (!rule.isValid(inferenceRequest, model)) {
839
+ throw new Error(`Request validation "${rule.name}" failed for model "${model.name}"`);
840
+ }
841
+ }
842
+ }
843
+ };
844
+
845
+ // src/domain/inference/token-usage.ts
846
+ var InputTokenDetails = class {
847
+ constructor(cached_tokens) {
848
+ this.cached_tokens = cached_tokens;
849
+ }
850
+ };
851
+ var OutputTokenDetails = class {
852
+ constructor(reasoning_tokens) {
853
+ this.reasoning_tokens = reasoning_tokens;
854
+ }
855
+ };
856
+ var TokenUsage = class {
857
+ constructor(inputTokens, outputTokens, totalTokens, inputTokenDetails, outputTokenDetails) {
858
+ this.inputTokens = inputTokens;
859
+ this.outputTokens = outputTokens;
860
+ this.totalTokens = totalTokens;
861
+ this.inputTokenDetails = inputTokenDetails;
862
+ this.outputTokenDetails = outputTokenDetails;
863
+ }
864
+ };
865
+
866
+ // src/infrastructure/providers/anthropic/anthropic-messages-mapper.ts
867
+ var AnthropicMessagesMapper = class {
868
+ toRequest(inferenceRequest) {
869
+ const { messages, system } = this.mapContextItems(inferenceRequest);
870
+ const outputConfig = {};
871
+ const request = {
872
+ model: inferenceRequest.model,
873
+ messages
874
+ };
875
+ request.max_tokens = inferenceRequest.maxOutputTokens;
876
+ if (system) {
877
+ request.system = system;
878
+ }
879
+ if (inferenceRequest.tools && inferenceRequest.tools.length > 0) {
880
+ request.tools = inferenceRequest.tools.map((tool) => ({
881
+ name: tool.name,
882
+ description: tool.description,
883
+ input_schema: tool.parameters
884
+ }));
885
+ }
886
+ if (inferenceRequest.structuredOutput) {
887
+ outputConfig.format = {
888
+ type: "json_schema",
889
+ schema: inferenceRequest.structuredOutput.schema
890
+ };
891
+ }
892
+ if (inferenceRequest.reasoningEffort) {
893
+ request.thinking = { type: "adaptive" };
894
+ outputConfig.effort = inferenceRequest.reasoningEffort;
895
+ }
896
+ if (Object.keys(outputConfig).length > 0) {
897
+ request.output_config = outputConfig;
898
+ }
899
+ if (inferenceRequest.streaming) {
900
+ request.stream = inferenceRequest.streaming;
901
+ }
902
+ return request;
903
+ }
904
+ addContentBlock(messages, role, block) {
905
+ const lastMessage = messages[messages.length - 1];
906
+ if ((lastMessage == null ? void 0 : lastMessage.role) === role) {
907
+ lastMessage.content.push(block);
908
+ return;
909
+ }
910
+ messages.push({
911
+ role,
912
+ content: [block]
913
+ });
914
+ }
915
+ mapContextItems(inferenceRequest) {
916
+ var _a, _b, _c;
917
+ const context = inferenceRequest.context;
918
+ const messages = [];
919
+ const system = [];
920
+ for (const item of context.items) {
921
+ if (item.type === "developer_message" || item.type === "system_message") {
922
+ system.push(item.text);
923
+ continue;
924
+ }
925
+ if (item.type === "user_message") {
926
+ this.addContentBlock(messages, "user", { type: "text", text: item.text });
927
+ continue;
928
+ }
929
+ if (item.type === "model_message") {
930
+ this.addContentBlock(messages, "assistant", { type: "text", text: item.text });
931
+ continue;
932
+ }
933
+ if (item.type === "reasoning") {
934
+ const reasoning = item;
935
+ this.addContentBlock(messages, "assistant", {
936
+ type: "thinking",
937
+ thinking: (_b = (_a = reasoning.content) == null ? void 0 : _a.text) != null ? _b : "",
938
+ signature: (_c = reasoning.encryptedContent) != null ? _c : ""
939
+ });
940
+ continue;
941
+ }
942
+ if (item.type === "tool_use_request") {
943
+ const toolUseRequest = item;
944
+ let input;
945
+ try {
946
+ input = JSON.parse(toolUseRequest.toolArguments);
947
+ } catch (e) {
948
+ input = toolUseRequest.toolArguments;
949
+ }
950
+ this.addContentBlock(messages, "assistant", {
951
+ type: "tool_use",
952
+ id: toolUseRequest.requestId,
953
+ name: toolUseRequest.toolName,
954
+ input
955
+ });
956
+ continue;
957
+ }
958
+ if (item.type === "tool_use_result") {
959
+ const toolUseResult = item;
960
+ this.addContentBlock(messages, "user", {
961
+ type: "tool_result",
962
+ tool_use_id: toolUseResult.requestId,
963
+ content: toolUseResult.result.text
964
+ });
965
+ }
966
+ }
967
+ return {
968
+ messages,
969
+ system: system.length > 0 ? system.join("\n\n") : void 0
970
+ };
971
+ }
972
+ extractTokenUsage(response) {
973
+ var _a, _b;
974
+ if (!response.usage) {
975
+ return void 0;
976
+ }
977
+ return new TokenUsage(
978
+ response.usage.input_tokens,
979
+ response.usage.output_tokens,
980
+ response.usage.input_tokens + response.usage.output_tokens,
981
+ new InputTokenDetails(
982
+ ((_a = response.usage.cache_creation_input_tokens) != null ? _a : 0) + ((_b = response.usage.cache_read_input_tokens) != null ? _b : 0)
983
+ ),
984
+ new OutputTokenDetails(0)
985
+ );
986
+ }
987
+ toResponse(response) {
988
+ var _a;
989
+ const items = [];
990
+ for (const block of response.content) {
991
+ if (block.type === "text") {
992
+ items.push({
993
+ type: "model_message",
994
+ text: block.text
995
+ });
996
+ continue;
997
+ }
998
+ if (block.type === "tool_use") {
999
+ items.push({
1000
+ type: "tool_use_request",
1001
+ requestId: block.id,
1002
+ toolName: block.name,
1003
+ toolArguments: JSON.stringify((_a = block.input) != null ? _a : {})
1004
+ });
1005
+ continue;
1006
+ }
1007
+ if (block.type === "thinking") {
1008
+ items.push({
1009
+ type: "reasoning",
1010
+ content: block.thinking ? { type: "input_text", text: block.thinking } : void 0,
1011
+ encryptedContent: block.signature,
1012
+ summary: []
1013
+ });
1014
+ }
1015
+ }
1016
+ return { items, tokenUsage: this.extractTokenUsage(response), rowResponse: response };
1017
+ }
1018
+ };
1019
+
1020
+ // src/infrastructure/providers/anthropic/anthropic-messages.ts
1021
+ import Anthropic from "@anthropic-ai/sdk";
1022
+ var AnthropicMessages = class {
1023
+ constructor(endpointMapper = new AnthropicMessagesMapper(), config = {}) {
1024
+ this.endpointMapper = endpointMapper;
1025
+ this.clientConfig = config;
1026
+ }
1027
+ get client() {
1028
+ var _a;
1029
+ return (_a = this._client) != null ? _a : this._client = new Anthropic({
1030
+ baseURL: this.clientConfig.baseURL,
1031
+ apiKey: this.clientConfig.apiKey
1032
+ });
1033
+ }
1034
+ async infer(inferenceRequest) {
1035
+ const request = this.endpointMapper.toRequest(inferenceRequest);
1036
+ const response = await this.client.messages.create(request);
1037
+ return this.endpointMapper.toResponse(response);
1038
+ }
1039
+ async *stream(inferenceRequest) {
1040
+ const request = this.endpointMapper.toRequest(inferenceRequest);
1041
+ const stream = this.client.messages.stream(request);
1042
+ for await (const event of stream) {
1043
+ yield event;
1044
+ }
1045
+ const output = this.endpointMapper.toResponse(await stream.finalMessage());
1046
+ yield {
1047
+ type: "inference.output",
1048
+ producerId: request.model,
1049
+ occurredAt: /* @__PURE__ */ new Date(),
1050
+ payload: output
1051
+ };
1052
+ }
1053
+ };
1054
+
1055
+ // src/infrastructure/providers/openai/openai-responses-mapper.ts
1056
+ var OpenAIResponsesMapper = class {
1057
+ toRequest(inferenceRequest) {
1058
+ var _a, _b;
1059
+ const request = {
1060
+ model: inferenceRequest.model,
1061
+ input: this.mapContextItems(inferenceRequest)
1062
+ };
1063
+ if (inferenceRequest.tools && inferenceRequest.tools.length > 0) {
1064
+ request.tools = inferenceRequest.tools.map((tool) => ({
1065
+ type: tool.type,
1066
+ name: tool.name,
1067
+ description: tool.description,
1068
+ parameters: tool.parameters
1069
+ }));
1070
+ }
1071
+ if (inferenceRequest.reasoningEffort) {
1072
+ request.reasoning = {
1073
+ effort: inferenceRequest.reasoningEffort
1074
+ };
1075
+ }
1076
+ if (inferenceRequest.structuredOutput) {
1077
+ const format = inferenceRequest.structuredOutput;
1078
+ request.text = {
1079
+ format: {
1080
+ type: "json_schema",
1081
+ name: (_a = format.name) != null ? _a : "response",
1082
+ schema: format.schema,
1083
+ strict: (_b = format.strict) != null ? _b : true
1084
+ }
1085
+ };
1086
+ }
1087
+ if (inferenceRequest.streaming) {
1088
+ request.stream = inferenceRequest.streaming;
1089
+ }
1090
+ return request;
1091
+ }
1092
+ mapContextItems(inferenceRequest) {
1093
+ const input = [];
1094
+ for (const item of inferenceRequest.context.items) {
1095
+ if (item.type === "developer_message" || item.type === "system_message" || item.type === "user_message") {
1096
+ const role = item.type === "developer_message" ? "developer" : item.type === "system_message" ? "system" : "user";
1097
+ input.push({
1098
+ type: "message",
1099
+ role,
1100
+ content: [{ type: "input_text", text: item.text }]
1101
+ });
1102
+ continue;
1103
+ }
1104
+ if (item.type === "model_message") {
1105
+ input.push({
1106
+ type: "message",
1107
+ role: "assistant",
1108
+ content: [{ type: "output_text", text: item.text }]
1109
+ });
1110
+ continue;
1111
+ }
1112
+ if (item.type === "tool_use_request") {
1113
+ const toolUseRequest = item;
1114
+ input.push({
1115
+ type: "function_call",
1116
+ call_id: toolUseRequest.requestId,
1117
+ name: toolUseRequest.toolName,
1118
+ arguments: toolUseRequest.toolArguments
1119
+ });
1120
+ continue;
1121
+ }
1122
+ if (item.type === "tool_use_result") {
1123
+ const toolUseResult = item;
1124
+ input.push({
1125
+ type: "function_call_output",
1126
+ call_id: toolUseResult.requestId,
1127
+ output: [{ type: "input_text", text: toolUseResult.result.text }]
1128
+ });
1129
+ continue;
1130
+ }
1131
+ if (item.type === "reasoning") {
1132
+ const reasoning = item;
1133
+ const reasoningInput = {
1134
+ type: "reasoning",
1135
+ summary: reasoning.summary.map((summary) => ({ type: "summary_text", text: summary.text }))
1136
+ };
1137
+ if (reasoning.encryptedContent !== void 0) {
1138
+ reasoningInput.encrypted_content = reasoning.encryptedContent;
1139
+ }
1140
+ input.push(reasoningInput);
1141
+ }
1142
+ }
1143
+ return input;
1144
+ }
1145
+ extractTokenUsage(response) {
1146
+ var _a, _b, _c, _d;
1147
+ if (!response.usage) {
1148
+ return void 0;
1149
+ }
1150
+ return new TokenUsage(
1151
+ response.usage.input_tokens,
1152
+ response.usage.output_tokens,
1153
+ response.usage.total_tokens,
1154
+ new InputTokenDetails((_b = (_a = response.usage.input_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0),
1155
+ new OutputTokenDetails((_d = (_c = response.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
1156
+ );
1157
+ }
1158
+ toResponse(response) {
1159
+ var _a, _b, _c;
1160
+ const items = [];
1161
+ for (const item of (_a = response.output) != null ? _a : []) {
1162
+ if (item.type === "message" && item.role === "assistant") {
1163
+ const firstContent = (_b = item.content) == null ? void 0 : _b[0];
1164
+ if (firstContent) {
1165
+ items.push({
1166
+ type: "model_message",
1167
+ text: firstContent.text
1168
+ });
1169
+ }
1170
+ continue;
1171
+ }
1172
+ if (item.type === "function_call") {
1173
+ const toolUseRequest = {
1174
+ type: "tool_use_request",
1175
+ requestId: item.call_id,
1176
+ toolName: item.name,
1177
+ toolArguments: item.arguments
1178
+ };
1179
+ items.push(toolUseRequest);
1180
+ continue;
1181
+ }
1182
+ if (item.type === "reasoning") {
1183
+ items.push({
1184
+ type: "reasoning",
1185
+ content: void 0,
1186
+ encryptedContent: item.encrypted_content,
1187
+ summary: ((_c = item.summary) != null ? _c : []).map((summary) => ({
1188
+ type: "summary_text",
1189
+ text: summary.text
1190
+ }))
1191
+ });
1192
+ }
1193
+ }
1194
+ return {
1195
+ items,
1196
+ tokenUsage: this.extractTokenUsage(response),
1197
+ rowResponse: response
1198
+ };
1199
+ }
1200
+ };
1201
+
1202
+ // src/infrastructure/providers/openai/openai-responses.ts
1203
+ import OpenAI from "openai";
1204
+ var OpenAIResponses = class {
1205
+ constructor(endpointMapper = new OpenAIResponsesMapper(), config = {}) {
1206
+ this.endpointMapper = endpointMapper;
1207
+ this.clientConfig = config;
1208
+ }
1209
+ get client() {
1210
+ var _a;
1211
+ return (_a = this._client) != null ? _a : this._client = new OpenAI({
1212
+ baseURL: this.clientConfig.baseURL,
1213
+ apiKey: this.clientConfig.apiKey
1214
+ });
1215
+ }
1216
+ async infer(inferenceRequest) {
1217
+ const request = this.endpointMapper.toRequest(inferenceRequest);
1218
+ const response = await this.client.responses.create(request);
1219
+ return this.endpointMapper.toResponse(response);
1220
+ }
1221
+ async *stream(inferenceRequest) {
1222
+ const request = this.endpointMapper.toRequest(inferenceRequest);
1223
+ const stream = await this.client.responses.create({ ...request, stream: true });
1224
+ let completedResponse = void 0;
1225
+ for await (const event of stream) {
1226
+ if (event.type === "response.completed") {
1227
+ completedResponse = event.response;
1228
+ }
1229
+ yield event;
1230
+ }
1231
+ if (!completedResponse) {
1232
+ throw new Error("Stream ended without a completed response");
1233
+ }
1234
+ const output = this.endpointMapper.toResponse(completedResponse);
1235
+ yield {
1236
+ type: "inference.output",
1237
+ payload: output,
1238
+ occurredAt: /* @__PURE__ */ new Date(),
1239
+ producerId: inferenceRequest.model
1240
+ };
1241
+ }
1242
+ };
1243
+
1244
+ // src/infrastructure/providers/gemini/gemini-generate-content.ts
1245
+ import { GoogleGenAI } from "@google/genai";
1246
+
1247
+ // src/infrastructure/providers/gemini/gemini-generate-content-mapper.ts
1248
+ var GeminiGenerateContentMapper = class {
1249
+ toRequest(inferenceRequest) {
1250
+ const { contents, systemInstruction } = this.mapContextItems(inferenceRequest);
1251
+ const config = {};
1252
+ if (systemInstruction) {
1253
+ config.systemInstruction = systemInstruction;
1254
+ }
1255
+ if (inferenceRequest.tools && inferenceRequest.tools.length > 0) {
1256
+ config.tools = [
1257
+ {
1258
+ functionDeclarations: inferenceRequest.tools.map((tool) => ({
1259
+ name: tool.name,
1260
+ description: tool.description,
1261
+ parametersJsonSchema: tool.parameters
1262
+ }))
1263
+ }
1264
+ ];
1265
+ }
1266
+ if (inferenceRequest.structuredOutput) {
1267
+ config.responseMimeType = "application/json";
1268
+ config.responseSchema = inferenceRequest.structuredOutput.schema;
1269
+ }
1270
+ if (inferenceRequest.reasoningEffort) {
1271
+ config.thinkingConfig = {
1272
+ thinkingLevel: inferenceRequest.reasoningEffort,
1273
+ includeThoughts: true
1274
+ };
1275
+ }
1276
+ const request = {
1277
+ model: inferenceRequest.model,
1278
+ contents,
1279
+ config
1280
+ };
1281
+ if (inferenceRequest.streaming) {
1282
+ request.stream = inferenceRequest.streaming;
1283
+ }
1284
+ return request;
1285
+ }
1286
+ mapContextItems(inferenceRequest) {
1287
+ var _a;
1288
+ const context = inferenceRequest.context;
1289
+ const contents = [];
1290
+ const system = [];
1291
+ const callNames = /* @__PURE__ */ new Map();
1292
+ for (const item of context.items) {
1293
+ if (item.type === "developer_message" || item.type === "system_message") {
1294
+ system.push(item.text);
1295
+ continue;
1296
+ }
1297
+ if (item.type === "user_message") {
1298
+ this.addPart(contents, "user", { text: item.text });
1299
+ continue;
1300
+ }
1301
+ if (item.type === "model_message") {
1302
+ this.addPart(contents, "model", { text: item.text });
1303
+ continue;
1304
+ }
1305
+ if (item.type === "tool_use_request") {
1306
+ const toolUseRequest = item;
1307
+ callNames.set(toolUseRequest.requestId, toolUseRequest.toolName);
1308
+ let args;
1309
+ try {
1310
+ args = JSON.parse(toolUseRequest.toolArguments);
1311
+ } catch (e) {
1312
+ args = {};
1313
+ }
1314
+ this.addPart(contents, "model", {
1315
+ functionCall: { id: toolUseRequest.requestId, name: toolUseRequest.toolName, args }
1316
+ });
1317
+ continue;
1318
+ }
1319
+ if (item.type === "tool_use_result") {
1320
+ const toolUseResult = item;
1321
+ this.addPart(contents, "user", {
1322
+ functionResponse: {
1323
+ id: toolUseResult.requestId,
1324
+ name: (_a = callNames.get(toolUseResult.requestId)) != null ? _a : "",
1325
+ response: this.parseResponse(toolUseResult.result.text)
1326
+ }
1327
+ });
1328
+ }
1329
+ }
1330
+ return {
1331
+ contents,
1332
+ systemInstruction: system.length > 0 ? system.join("\n\n") : void 0
1333
+ };
1334
+ }
1335
+ addPart(contents, role, part) {
1336
+ const last = contents[contents.length - 1];
1337
+ if ((last == null ? void 0 : last.role) === role) {
1338
+ last.parts.push(part);
1339
+ return;
1340
+ }
1341
+ contents.push({ role, parts: [part] });
1342
+ }
1343
+ parseResponse(output) {
1344
+ try {
1345
+ const parsed = JSON.parse(output);
1346
+ return parsed && typeof parsed === "object" ? parsed : { output };
1347
+ } catch (e) {
1348
+ return { output };
1349
+ }
1350
+ }
1351
+ extractTokenUsage(response) {
1352
+ var _a, _b, _c, _d, _e;
1353
+ const usage = response.usageMetadata;
1354
+ if (!usage) {
1355
+ return void 0;
1356
+ }
1357
+ return new TokenUsage(
1358
+ (_a = usage.promptTokenCount) != null ? _a : 0,
1359
+ (_b = usage.candidatesTokenCount) != null ? _b : 0,
1360
+ (_c = usage.totalTokenCount) != null ? _c : 0,
1361
+ new InputTokenDetails((_d = usage.cachedContentTokenCount) != null ? _d : 0),
1362
+ new OutputTokenDetails((_e = usage.thoughtsTokenCount) != null ? _e : 0)
1363
+ );
1364
+ }
1365
+ toResponse(response) {
1366
+ var _a, _b, _c, _d, _e, _f;
1367
+ const items = [];
1368
+ 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 : [];
1369
+ for (const part of parts) {
1370
+ if (part.thought && part.text) {
1371
+ items.push({
1372
+ type: "reasoning",
1373
+ content: { type: "input_text", text: part.text },
1374
+ encryptedContent: void 0,
1375
+ summary: []
1376
+ });
1377
+ continue;
1378
+ }
1379
+ if (part.text) {
1380
+ items.push({
1381
+ type: "model_message",
1382
+ text: part.text
1383
+ });
1384
+ continue;
1385
+ }
1386
+ if (part.functionCall) {
1387
+ items.push({
1388
+ type: "tool_use_request",
1389
+ requestId: (_e = part.functionCall.id) != null ? _e : "",
1390
+ toolName: part.functionCall.name,
1391
+ toolArguments: JSON.stringify((_f = part.functionCall.args) != null ? _f : {})
1392
+ });
1393
+ }
1394
+ }
1395
+ return { items, tokenUsage: this.extractTokenUsage(response), rowResponse: response };
1396
+ }
1397
+ };
1398
+
1399
+ // src/infrastructure/providers/gemini/gemini-generate-content.ts
1400
+ var GeminiGenerateContent = class {
1401
+ constructor(endpointMapper = new GeminiGenerateContentMapper(), config = {}) {
1402
+ this.endpointMapper = endpointMapper;
1403
+ this.clientConfig = config;
1404
+ }
1405
+ get client() {
1406
+ var _a, _b;
1407
+ return (_b = this._client) != null ? _b : this._client = new GoogleGenAI({
1408
+ apiKey: (_a = this.clientConfig.apiKey) != null ? _a : process.env.GEMINI_API_KEY,
1409
+ ...this.clientConfig.baseURL ? { httpOptions: { baseUrl: this.clientConfig.baseURL } } : {}
1410
+ });
1411
+ }
1412
+ async infer(inferenceRequest) {
1413
+ const request = this.endpointMapper.toRequest(inferenceRequest);
1414
+ const response = await this.client.models.generateContent(request);
1415
+ return this.endpointMapper.toResponse(response);
1416
+ }
1417
+ async *stream(inferenceRequest) {
1418
+ const request = this.endpointMapper.toRequest(inferenceRequest);
1419
+ const stream = await this.client.models.generateContentStream(request);
1420
+ let lastEvent = void 0;
1421
+ for await (const chunk of stream) {
1422
+ lastEvent = chunk;
1423
+ yield chunk;
1424
+ }
1425
+ if (!lastEvent) {
1426
+ throw new Error("Last event not found");
1427
+ }
1428
+ const output = this.endpointMapper.toResponse(lastEvent);
1429
+ yield {
1430
+ type: "inference.output",
1431
+ producerId: request.model,
1432
+ occurredAt: /* @__PURE__ */ new Date(),
1433
+ payload: output
1434
+ };
1435
+ }
1436
+ };
1437
+
1438
+ // src/infrastructure/providers/openai/openai-chat-completions-mapper.ts
1439
+ var OpenAIChatCompletionsMapper = class {
1440
+ toRequest(inferenceRequest) {
1441
+ const request = {
1442
+ model: inferenceRequest.model,
1443
+ messages: this.mapContextItems(inferenceRequest)
1444
+ };
1445
+ if (inferenceRequest.tools && inferenceRequest.tools.length > 0) {
1446
+ request.tools = inferenceRequest.tools.map((tool) => ({
1447
+ type: "function",
1448
+ function: {
1449
+ name: tool.name,
1450
+ description: tool.description,
1451
+ parameters: tool.parameters
1452
+ }
1453
+ }));
1454
+ }
1455
+ if (inferenceRequest.reasoningEffort) {
1456
+ request.reasoning_effort = inferenceRequest.reasoningEffort;
1457
+ }
1458
+ if (inferenceRequest.streaming) {
1459
+ request.stream = inferenceRequest.streaming;
1460
+ }
1461
+ return request;
1462
+ }
1463
+ mapContextItems(inferenceRequest) {
1464
+ var _a;
1465
+ const messages = [];
1466
+ for (const item of inferenceRequest.context.items) {
1467
+ if (item.type === "developer_message" || item.type === "system_message") {
1468
+ messages.push({ role: "system", content: item.text });
1469
+ continue;
1470
+ }
1471
+ if (item.type === "user_message") {
1472
+ messages.push({ role: "user", content: item.text });
1473
+ continue;
1474
+ }
1475
+ if (item.type === "model_message") {
1476
+ messages.push({ role: "assistant", content: item.text });
1477
+ continue;
1478
+ }
1479
+ if (item.type === "tool_use_request") {
1480
+ const toolUseRequest = item;
1481
+ const toolCall = {
1482
+ id: toolUseRequest.requestId,
1483
+ type: "function",
1484
+ function: { name: toolUseRequest.toolName, arguments: toolUseRequest.toolArguments }
1485
+ };
1486
+ const last = messages[messages.length - 1];
1487
+ if ((last == null ? void 0 : last.role) === "assistant") {
1488
+ last.tool_calls = (_a = last.tool_calls) != null ? _a : [];
1489
+ last.tool_calls.push(toolCall);
1490
+ } else {
1491
+ messages.push({ role: "assistant", content: null, tool_calls: [toolCall] });
1492
+ }
1493
+ continue;
1494
+ }
1495
+ if (item.type === "tool_use_result") {
1496
+ const toolUseResult = item;
1497
+ messages.push({
1498
+ role: "tool",
1499
+ tool_call_id: toolUseResult.requestId,
1500
+ content: toolUseResult.result.text
1501
+ });
1502
+ }
1503
+ }
1504
+ return messages;
1505
+ }
1506
+ extractTokenUsage(response) {
1507
+ var _a, _b, _c, _d;
1508
+ if (!response.usage) {
1509
+ return void 0;
1510
+ }
1511
+ return new TokenUsage(
1512
+ response.usage.prompt_tokens,
1513
+ response.usage.completion_tokens,
1514
+ response.usage.total_tokens,
1515
+ new InputTokenDetails((_b = (_a = response.usage.prompt_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0),
1516
+ new OutputTokenDetails((_d = (_c = response.usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
1517
+ );
1518
+ }
1519
+ toResponse(response) {
1520
+ var _a, _b, _c;
1521
+ const items = [];
1522
+ const message = (_b = (_a = response.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1523
+ if (!message) {
1524
+ return { items: [], tokenUsage: this.extractTokenUsage(response), rowResponse: response };
1525
+ }
1526
+ if (message.reasoning_content) {
1527
+ items.push({
1528
+ type: "reasoning",
1529
+ content: { type: "input_text", text: message.reasoning_content },
1530
+ encryptedContent: void 0,
1531
+ summary: []
1532
+ });
1533
+ }
1534
+ if (message.content) {
1535
+ items.push({
1536
+ type: "model_message",
1537
+ text: message.content
1538
+ });
1539
+ }
1540
+ for (const toolCall of (_c = message.tool_calls) != null ? _c : []) {
1541
+ items.push({
1542
+ type: "tool_use_request",
1543
+ requestId: toolCall.id,
1544
+ toolName: toolCall.function.name,
1545
+ toolArguments: toolCall.function.arguments
1546
+ });
1547
+ }
1548
+ return { items, tokenUsage: this.extractTokenUsage(response), rowResponse: response };
1549
+ }
1550
+ };
1551
+
1552
+ // src/infrastructure/providers/openai/openai-chat-completions.ts
1553
+ import OpenAI2 from "openai";
1554
+ var OpenAIChatCompletions = class {
1555
+ constructor(endpointMapper = new OpenAIChatCompletionsMapper(), config = {}) {
1556
+ var _a;
1557
+ this.endpointMapper = endpointMapper;
1558
+ this.clientConfig = config;
1559
+ this.extraBody = (_a = config.extraBody) != null ? _a : {};
1560
+ }
1561
+ get client() {
1562
+ var _a;
1563
+ return (_a = this._client) != null ? _a : this._client = new OpenAI2({
1564
+ baseURL: this.clientConfig.baseURL,
1565
+ apiKey: this.clientConfig.apiKey
1566
+ });
1567
+ }
1568
+ buildRequest(inferenceRequest) {
1569
+ return {
1570
+ ...this.extraBody,
1571
+ ...this.endpointMapper.toRequest(inferenceRequest)
1572
+ };
1573
+ }
1574
+ async infer(inferenceRequest) {
1575
+ const response = await this.client.chat.completions.create(this.buildRequest(inferenceRequest));
1576
+ return this.endpointMapper.toResponse(response);
1577
+ }
1578
+ async *stream(inferenceRequest) {
1579
+ const stream = this.client.chat.completions.stream(this.buildRequest(inferenceRequest));
1580
+ for await (const chunk of stream) {
1581
+ yield chunk;
1582
+ }
1583
+ const output = this.endpointMapper.toResponse(await stream.finalChatCompletion());
1584
+ yield {
1585
+ type: "inference.output",
1586
+ payload: output,
1587
+ occurredAt: /* @__PURE__ */ new Date(),
1588
+ producerId: inferenceRequest.model
1589
+ };
1590
+ }
1591
+ };
1592
+
1593
+ // src/infrastructure/providers/anthropic/models/claude-4-6-sonnet.ts
1594
+ var claudeSonnet46Specification = {
1595
+ name: "claude-sonnet-4-6",
1596
+ provider: "anthropic",
1597
+ supportsReasoningEffort: true,
1598
+ supportedReasoningEfforts: ["max", "high", "medium", "low"],
1599
+ supportedContextItemTypes: [
1600
+ "user_message",
1601
+ "system_message",
1602
+ "developer_message",
1603
+ "reasoning",
1604
+ "tool_use_request",
1605
+ "tool_use_result",
1606
+ "model_message"
1607
+ ],
1608
+ supportsStreaming: true,
1609
+ contextWindowSize: 2e5,
1610
+ maxOutputTokens: 64e3,
1611
+ supportsFunctionCalling: true,
1612
+ supportsStructuredOutput: true
1613
+ };
1614
+
1615
+ // src/infrastructure/providers/anthropic/models/claude-4-8-opus.ts
1616
+ var claudeOpus48Specification = {
1617
+ name: "claude-opus-4-8",
1618
+ provider: "anthropic",
1619
+ supportsReasoningEffort: true,
1620
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low"],
1621
+ supportedContextItemTypes: [
1622
+ "user_message",
1623
+ "system_message",
1624
+ "developer_message",
1625
+ "reasoning",
1626
+ "tool_use_request",
1627
+ "tool_use_result",
1628
+ "model_message"
1629
+ ],
1630
+ supportsStreaming: true,
1631
+ contextWindowSize: 2e5,
1632
+ maxOutputTokens: 32e3,
1633
+ supportsFunctionCalling: true,
1634
+ supportsStructuredOutput: true
1635
+ };
1636
+
1637
+ // src/infrastructure/providers/anthropic/models/claude-4-7-opus.ts
1638
+ var claudeOpus47Specification = {
1639
+ name: "claude-opus-4-7",
1640
+ provider: "anthropic",
1641
+ supportsReasoningEffort: true,
1642
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low"],
1643
+ supportedContextItemTypes: [
1644
+ "user_message",
1645
+ "system_message",
1646
+ "developer_message",
1647
+ "reasoning",
1648
+ "tool_use_request",
1649
+ "tool_use_result",
1650
+ "model_message"
1651
+ ],
1652
+ supportsStreaming: true,
1653
+ contextWindowSize: 2e5,
1654
+ maxOutputTokens: 32e3,
1655
+ supportsFunctionCalling: true,
1656
+ supportsStructuredOutput: true
1657
+ };
1658
+
1659
+ // src/infrastructure/providers/gemini/models/gemini-3-5-flash.ts
1660
+ var gemini35FlashSpecification = {
1661
+ name: "gemini-3.5-flash",
1662
+ provider: "google",
1663
+ supportsReasoningEffort: true,
1664
+ supportedReasoningEfforts: ["high", "medium", "low", "minimal", "none"],
1665
+ supportedContextItemTypes: [
1666
+ "user_message",
1667
+ "system_message",
1668
+ "developer_message",
1669
+ "reasoning",
1670
+ "tool_use_request",
1671
+ "tool_use_result",
1672
+ "model_message"
1673
+ ],
1674
+ supportsStreaming: true,
1675
+ contextWindowSize: 1048576,
1676
+ maxOutputTokens: 64e3,
1677
+ supportsFunctionCalling: true,
1678
+ supportsStructuredOutput: true
1679
+ };
1680
+
1681
+ // src/infrastructure/providers/gemini/models/gemini-3-1-pro.ts
1682
+ var gemini31ProSpecification = {
1683
+ name: "gemini-3.1-pro-preview",
1684
+ provider: "google",
1685
+ supportsReasoningEffort: true,
1686
+ supportedReasoningEfforts: ["high", "medium", "low", "minimal", "none"],
1687
+ supportedContextItemTypes: [
1688
+ "user_message",
1689
+ "system_message",
1690
+ "developer_message",
1691
+ "reasoning",
1692
+ "tool_use_request",
1693
+ "tool_use_result",
1694
+ "model_message"
1695
+ ],
1696
+ supportsStreaming: true,
1697
+ contextWindowSize: 1048576,
1698
+ maxOutputTokens: 64e3,
1699
+ supportsFunctionCalling: true,
1700
+ supportsStructuredOutput: true
1701
+ };
1702
+
1703
+ // src/infrastructure/providers/deepseek/models/deepseek-v4-flash.ts
1704
+ var deepSeekV4FlashSpecification = {
1705
+ name: "deepseek-v4-flash",
1706
+ provider: "deepseek",
1707
+ supportsReasoningEffort: true,
1708
+ supportedReasoningEfforts: ["max", "high", "medium", "low", "none"],
1709
+ supportedContextItemTypes: [
1710
+ "user_message",
1711
+ "system_message",
1712
+ "developer_message",
1713
+ "reasoning",
1714
+ "tool_use_request",
1715
+ "tool_use_result",
1716
+ "model_message"
1717
+ ],
1718
+ supportsStreaming: true,
1719
+ contextWindowSize: 1e6,
1720
+ maxOutputTokens: 384e3,
1721
+ supportsFunctionCalling: true,
1722
+ supportsStructuredOutput: false
1723
+ };
1724
+
1725
+ // src/infrastructure/providers/deepseek/models/deepseek-v4-pro.ts
1726
+ var deepSeekV4ProSpecification = {
1727
+ name: "deepseek-v4-pro",
1728
+ provider: "deepseek",
1729
+ supportsReasoningEffort: true,
1730
+ supportedReasoningEfforts: ["max", "high", "medium", "low", "none"],
1731
+ supportedContextItemTypes: [
1732
+ "user_message",
1733
+ "system_message",
1734
+ "developer_message",
1735
+ "reasoning",
1736
+ "tool_use_request",
1737
+ "tool_use_result",
1738
+ "model_message"
1739
+ ],
1740
+ supportsStreaming: true,
1741
+ contextWindowSize: 1e6,
1742
+ maxOutputTokens: 384e3,
1743
+ supportsFunctionCalling: true,
1744
+ supportsStructuredOutput: false
1745
+ };
1746
+
1747
+ // src/infrastructure/providers/openai/models/gpt-5-4.ts
1748
+ var gpt54Specification = {
1749
+ name: "gpt-5.4",
1750
+ provider: "openai",
1751
+ supportsReasoningEffort: true,
1752
+ supportedReasoningEfforts: ["xhigh", "high", "medium", "low", "none"],
1753
+ supportedContextItemTypes: [
1754
+ "user_message",
1755
+ "system_message",
1756
+ "developer_message",
1757
+ "reasoning",
1758
+ "tool_use_request",
1759
+ "tool_use_result",
1760
+ "model_message"
1761
+ ],
1762
+ supportsStreaming: true,
1763
+ contextWindowSize: 105e4,
1764
+ maxOutputTokens: 128e3,
1765
+ supportsFunctionCalling: true,
1766
+ supportsStructuredOutput: true
1767
+ };
1768
+
1769
+ // src/infrastructure/providers/openai/models/gpt-5-4-mini.ts
1770
+ var gpt54MiniSpecification = {
1771
+ name: "gpt-5.4-mini",
1772
+ provider: "openai",
1773
+ supportsReasoningEffort: true,
1774
+ supportedReasoningEfforts: ["xhigh", "high", "medium", "low", "none"],
1775
+ supportedContextItemTypes: [
1776
+ "user_message",
1777
+ "system_message",
1778
+ "developer_message",
1779
+ "reasoning",
1780
+ "tool_use_request",
1781
+ "tool_use_result",
1782
+ "model_message"
1783
+ ],
1784
+ supportsStreaming: true,
1785
+ contextWindowSize: 4e5,
1786
+ maxOutputTokens: 128e3,
1787
+ supportsFunctionCalling: true,
1788
+ supportsStructuredOutput: true
1789
+ };
1790
+
1791
+ // src/infrastructure/providers/openai/models/gpt-5-4-nano.ts
1792
+ var gpt54NanoSpecification = {
1793
+ name: "gpt-5.4-nano",
1794
+ provider: "openai",
1795
+ supportsReasoningEffort: true,
1796
+ supportedReasoningEfforts: ["xhigh", "high", "medium", "low", "none"],
1797
+ supportedContextItemTypes: [
1798
+ "user_message",
1799
+ "system_message",
1800
+ "developer_message",
1801
+ "reasoning",
1802
+ "tool_use_request",
1803
+ "tool_use_result",
1804
+ "model_message"
1805
+ ],
1806
+ supportsStreaming: true,
1807
+ contextWindowSize: 4e5,
1808
+ maxOutputTokens: 128e3,
1809
+ supportsFunctionCalling: true,
1810
+ supportsStructuredOutput: true
1811
+ };
1812
+
1813
+ // src/infrastructure/providers/openai/models/gpt-5-5.ts
1814
+ var gpt55Specification = {
1815
+ name: "gpt-5.5",
1816
+ provider: "openai",
1817
+ supportsReasoningEffort: true,
1818
+ supportedReasoningEfforts: ["xhigh", "high", "medium", "low", "none"],
1819
+ supportedContextItemTypes: [
1820
+ "user_message",
1821
+ "system_message",
1822
+ "developer_message",
1823
+ "reasoning",
1824
+ "tool_use_request",
1825
+ "tool_use_result",
1826
+ "model_message"
1827
+ ],
1828
+ supportsStreaming: true,
1829
+ contextWindowSize: 105e4,
1830
+ maxOutputTokens: 128e3,
1831
+ supportsFunctionCalling: true,
1832
+ supportsStructuredOutput: true
1833
+ };
1834
+
1835
+ // src/infrastructure/providers/anthropic/models/claude-4-5-haiku.ts
1836
+ var claudeHaiku45Specification = {
1837
+ name: "claude-haiku-4-5",
1838
+ provider: "anthropic",
1839
+ supportsReasoningEffort: true,
1840
+ supportedReasoningEfforts: ["high", "medium", "low", "none"],
1841
+ supportedContextItemTypes: [
1842
+ "user_message",
1843
+ "system_message",
1844
+ "developer_message",
1845
+ "reasoning",
1846
+ "tool_use_request",
1847
+ "tool_use_result",
1848
+ "model_message"
1849
+ ],
1850
+ supportsStreaming: true,
1851
+ contextWindowSize: 2e5,
1852
+ maxOutputTokens: 64e3,
1853
+ supportsFunctionCalling: true,
1854
+ supportsStructuredOutput: true
1855
+ };
1856
+
1857
+ // src/infrastructure/providers/openai/models/gpt-6-luna.ts
1858
+ var gpt6LunaSpecification = {
1859
+ name: "gpt-6-luna",
1860
+ provider: "openai",
1861
+ supportsReasoningEffort: true,
1862
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low", "none"],
1863
+ supportedContextItemTypes: [
1864
+ "user_message",
1865
+ "system_message",
1866
+ "developer_message",
1867
+ "reasoning",
1868
+ "function_call",
1869
+ "function_call_output",
1870
+ "model_message"
1871
+ ],
1872
+ supportsStreaming: true,
1873
+ contextWindowSize: 105e4,
1874
+ maxOutputTokens: 128e3,
1875
+ supportsFunctionCalling: true,
1876
+ supportsStructuredOutput: true
1877
+ };
1878
+
1879
+ // src/infrastructure/providers/openai/models/gpt-6-sol.ts
1880
+ var gpt6SolSpecification = {
1881
+ name: "gpt-6-sol",
1882
+ provider: "openai",
1883
+ supportsReasoningEffort: true,
1884
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low", "none"],
1885
+ supportedContextItemTypes: [
1886
+ "user_message",
1887
+ "system_message",
1888
+ "developer_message",
1889
+ "reasoning",
1890
+ "function_call",
1891
+ "function_call_output",
1892
+ "model_message"
1893
+ ],
1894
+ supportsStreaming: true,
1895
+ contextWindowSize: 105e4,
1896
+ maxOutputTokens: 128e3,
1897
+ supportsFunctionCalling: true,
1898
+ supportsStructuredOutput: true
1899
+ };
1900
+
1901
+ // src/infrastructure/providers/openai/models/gpt-6-1-sol.ts
1902
+ var gpt61SolSpecification = {
1903
+ name: "gpt-6.1-sol",
1904
+ provider: "openai",
1905
+ supportsReasoningEffort: true,
1906
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low"],
1907
+ supportedContextItemTypes: [
1908
+ "user_message",
1909
+ "system_message",
1910
+ "developer_message",
1911
+ "reasoning",
1912
+ "function_call",
1913
+ "function_call_output",
1914
+ "model_message"
1915
+ ],
1916
+ supportsStreaming: true,
1917
+ contextWindowSize: 105e4,
1918
+ maxOutputTokens: 128e3,
1919
+ supportsFunctionCalling: true,
1920
+ supportsStructuredOutput: true
1921
+ };
1922
+
1923
+ // src/infrastructure/providers/openai/models/gpt-6-astra.ts
1924
+ var gpt6AstraSpecification = {
1925
+ name: "gpt-6-astra",
1926
+ provider: "openai",
1927
+ supportsReasoningEffort: true,
1928
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low"],
1929
+ supportedContextItemTypes: [
1930
+ "user_message",
1931
+ "system_message",
1932
+ "developer_message",
1933
+ "reasoning",
1934
+ "function_call",
1935
+ "function_call_output",
1936
+ "model_message"
1937
+ ],
1938
+ supportsStreaming: true,
1939
+ contextWindowSize: 105e4,
1940
+ maxOutputTokens: 128e3,
1941
+ supportsFunctionCalling: true,
1942
+ supportsStructuredOutput: true
1943
+ };
1944
+
1945
+ // src/infrastructure/providers/anthropic/models/claude-5-sonnet.ts
1946
+ var claudeSonnet5Specification = {
1947
+ name: "claude-sonnet-5",
1948
+ provider: "anthropic",
1949
+ supportsReasoningEffort: true,
1950
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low"],
1951
+ supportedContextItemTypes: [
1952
+ "user_message",
1953
+ "system_message",
1954
+ "developer_message",
1955
+ "reasoning",
1956
+ "function_call",
1957
+ "function_call_output",
1958
+ "model_message"
1959
+ ],
1960
+ supportsStreaming: true,
1961
+ contextWindowSize: 1e6,
1962
+ maxOutputTokens: 128e3,
1963
+ supportsFunctionCalling: true,
1964
+ supportsStructuredOutput: true
1965
+ };
1966
+
1967
+ // src/infrastructure/providers/anthropic/models/claude-5-5-sonnet.ts
1968
+ var claudeSonnet55Specification = {
1969
+ name: "claude-sonnet-5-5",
1970
+ provider: "anthropic",
1971
+ supportsReasoningEffort: true,
1972
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low"],
1973
+ supportedContextItemTypes: [
1974
+ "user_message",
1975
+ "system_message",
1976
+ "developer_message",
1977
+ "reasoning",
1978
+ "function_call",
1979
+ "function_call_output",
1980
+ "model_message"
1981
+ ],
1982
+ supportsStreaming: true,
1983
+ contextWindowSize: 1e6,
1984
+ maxOutputTokens: 128e3,
1985
+ supportsFunctionCalling: true,
1986
+ supportsStructuredOutput: true
1987
+ };
1988
+
1989
+ // src/infrastructure/providers/anthropic/models/claude-5-5-opus.ts
1990
+ var claudeOpus55Specification = {
1991
+ name: "claude-opus-5-5",
1992
+ provider: "anthropic",
1993
+ supportsReasoningEffort: true,
1994
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low"],
1995
+ supportedContextItemTypes: [
1996
+ "user_message",
1997
+ "system_message",
1998
+ "developer_message",
1999
+ "reasoning",
2000
+ "function_call",
2001
+ "function_call_output",
2002
+ "model_message"
2003
+ ],
2004
+ supportsStreaming: true,
2005
+ contextWindowSize: 1e6,
2006
+ maxOutputTokens: 128e3,
2007
+ supportsFunctionCalling: true,
2008
+ supportsStructuredOutput: true
2009
+ };
2010
+
2011
+ // src/infrastructure/providers/anthropic/models/claude-5-1-fable.ts
2012
+ var claudeFable51Specification = {
2013
+ name: "claude-fable-5-1",
2014
+ provider: "anthropic",
2015
+ supportsReasoningEffort: true,
2016
+ supportedReasoningEfforts: ["max", "xhigh", "high", "medium", "low"],
2017
+ supportedContextItemTypes: [
2018
+ "user_message",
2019
+ "system_message",
2020
+ "developer_message",
2021
+ "reasoning",
2022
+ "function_call",
2023
+ "function_call_output",
2024
+ "model_message"
2025
+ ],
2026
+ supportsStreaming: true,
2027
+ contextWindowSize: 1e6,
2028
+ maxOutputTokens: 128e3,
2029
+ supportsFunctionCalling: true,
2030
+ supportsStructuredOutput: true
2031
+ };
2032
+
2033
+ // src/infrastructure/providers/gemini/models/gemini-3-5-flash-lite.ts
2034
+ var gemini35FlashLiteSpecification = {
2035
+ name: "gemini-3.5-flash-lite",
2036
+ provider: "google",
2037
+ supportsReasoningEffort: true,
2038
+ supportedReasoningEfforts: ["high", "medium", "low", "minimal"],
2039
+ supportedContextItemTypes: [
2040
+ "user_message",
2041
+ "system_message",
2042
+ "developer_message",
2043
+ "reasoning",
2044
+ "function_call",
2045
+ "function_call_output",
2046
+ "model_message"
2047
+ ],
2048
+ supportsStreaming: true,
2049
+ contextWindowSize: 1048576,
2050
+ maxOutputTokens: 65536,
2051
+ supportsFunctionCalling: true,
2052
+ supportsStructuredOutput: true
2053
+ };
2054
+
2055
+ // src/infrastructure/providers/gemini/models/gemini-3-8-flash.ts
2056
+ var gemini38FlashSpecification = {
2057
+ name: "gemini-3.8-flash",
2058
+ provider: "google",
2059
+ supportsReasoningEffort: true,
2060
+ supportedReasoningEfforts: ["high", "medium", "low"],
2061
+ supportedContextItemTypes: [
2062
+ "user_message",
2063
+ "system_message",
2064
+ "developer_message",
2065
+ "reasoning",
2066
+ "function_call",
2067
+ "function_call_output",
2068
+ "model_message"
2069
+ ],
2070
+ supportsStreaming: true,
2071
+ contextWindowSize: 1048576,
2072
+ maxOutputTokens: 65536,
2073
+ supportsFunctionCalling: true,
2074
+ supportsStructuredOutput: true
2075
+ };
2076
+
2077
+ // src/infrastructure/providers/deepseek/models/deepseek-flash.ts
2078
+ var deepSeekFlashSpecification = {
2079
+ name: "deepseek-flash",
2080
+ provider: "deepseek",
2081
+ supportsReasoningEffort: true,
2082
+ supportedReasoningEfforts: ["max", "high", "medium", "low", "none"],
2083
+ supportedContextItemTypes: [
2084
+ "user_message",
2085
+ "system_message",
2086
+ "developer_message",
2087
+ "reasoning",
2088
+ "function_call",
2089
+ "function_call_output",
2090
+ "model_message"
2091
+ ],
2092
+ supportsStreaming: true,
2093
+ contextWindowSize: 1e6,
2094
+ maxOutputTokens: 384e3,
2095
+ supportsFunctionCalling: true,
2096
+ supportsStructuredOutput: false
2097
+ };
2098
+
2099
+ // src/infrastructure/providers/supported-models.ts
2100
+ var supportedModels = [
2101
+ {
2102
+ endpoint: new OpenAIResponses(),
2103
+ specification: gpt54Specification
2104
+ },
2105
+ {
2106
+ endpoint: new OpenAIResponses(),
2107
+ specification: gpt54MiniSpecification
2108
+ },
2109
+ {
2110
+ endpoint: new OpenAIResponses(),
2111
+ specification: gpt54NanoSpecification
2112
+ },
2113
+ {
2114
+ endpoint: new OpenAIResponses(),
2115
+ specification: gpt55Specification
2116
+ },
2117
+ {
2118
+ endpoint: new OpenAIResponses(),
2119
+ specification: gpt6LunaSpecification
2120
+ },
2121
+ {
2122
+ endpoint: new OpenAIResponses(),
2123
+ specification: gpt6SolSpecification
2124
+ },
2125
+ {
2126
+ endpoint: new OpenAIResponses(),
2127
+ specification: gpt61SolSpecification
2128
+ },
2129
+ {
2130
+ endpoint: new OpenAIResponses(),
2131
+ specification: gpt6AstraSpecification
2132
+ },
2133
+ {
2134
+ endpoint: new AnthropicMessages(),
2135
+ specification: claudeHaiku45Specification
2136
+ },
2137
+ {
2138
+ endpoint: new AnthropicMessages(),
2139
+ specification: claudeSonnet46Specification
2140
+ },
2141
+ {
2142
+ endpoint: new AnthropicMessages(),
2143
+ specification: claudeOpus47Specification
2144
+ },
2145
+ {
2146
+ endpoint: new AnthropicMessages(),
2147
+ specification: claudeOpus48Specification
2148
+ },
2149
+ {
2150
+ endpoint: new AnthropicMessages(),
2151
+ specification: claudeSonnet5Specification
2152
+ },
2153
+ {
2154
+ endpoint: new AnthropicMessages(),
2155
+ specification: claudeSonnet55Specification
2156
+ },
2157
+ {
2158
+ endpoint: new AnthropicMessages(),
2159
+ specification: claudeOpus55Specification
2160
+ },
2161
+ {
2162
+ endpoint: new AnthropicMessages(),
2163
+ specification: claudeFable51Specification
2164
+ },
2165
+ {
2166
+ endpoint: new GeminiGenerateContent(),
2167
+ specification: gemini35FlashSpecification
2168
+ },
2169
+ {
2170
+ endpoint: new GeminiGenerateContent(),
2171
+ specification: gemini31ProSpecification
2172
+ },
2173
+ {
2174
+ endpoint: new GeminiGenerateContent(),
2175
+ specification: gemini35FlashLiteSpecification
2176
+ },
2177
+ {
2178
+ endpoint: new GeminiGenerateContent(),
2179
+ specification: gemini38FlashSpecification
2180
+ },
2181
+ {
2182
+ endpoint: new OpenAIChatCompletions(),
2183
+ specification: deepSeekV4FlashSpecification
2184
+ },
2185
+ {
2186
+ endpoint: new OpenAIChatCompletions(),
2187
+ specification: deepSeekV4ProSpecification
2188
+ },
2189
+ {
2190
+ endpoint: new OpenAIChatCompletions(),
2191
+ specification: deepSeekFlashSpecification
2192
+ }
2193
+ ];
2194
+
2195
+ // src/domain/agent/loop/rule-engine.ts
2196
+ var RuleEngine = class {
2197
+ decide(agent, loop) {
2198
+ const agentLoop = { agent, loop };
2199
+ const satisfiedRules = loop.rules.filter((rule) => rule.condition.isSatisfiedBy(agentLoop));
2200
+ if (satisfiedRules.length === 0) {
2201
+ return void 0;
2202
+ }
2203
+ const highestPriorityRule = satisfiedRules.reduce((highest, current) => {
2204
+ return highest.priority > current.priority ? highest : current;
2205
+ });
2206
+ return highestPriorityRule.action.execute(agentLoop);
2207
+ }
2208
+ };
2209
+
2210
+ // src/application/use-cases/run-loop.ts
2211
+ var RunLoopUseCase = class {
2212
+ constructor(agentRepository, loopRepository, directiveExecutionStrategyResolver) {
2213
+ this.agentRepository = agentRepository;
2214
+ this.loopRepository = loopRepository;
2215
+ this.directiveExecutionStrategyResolver = directiveExecutionStrategyResolver;
2216
+ this.ruleEngine = new RuleEngine();
2217
+ }
2218
+ async execute(loopId) {
2219
+ const loop = await this.loopRepository.findById(loopId);
2220
+ if (!loop) {
2221
+ throw new Error("Loop not found");
2222
+ }
2223
+ const agent = await this.agentRepository.findById(loop.getAgentId());
2224
+ if (!agent) {
2225
+ throw new Error("Agent not found");
2226
+ }
2227
+ while (loop.stateId !== "completed") {
2228
+ const directive = this.ruleEngine.decide(agent, loop);
2229
+ if (!directive) return loop;
2230
+ const strategy = this.directiveExecutionStrategyResolver.resolve(directive);
2231
+ await strategy.execute(directive, agent, loop);
2232
+ }
2233
+ await this.loopRepository.save(loop);
2234
+ return loop;
2235
+ }
2236
+ };
2237
+
2238
+ // src/domain/agent/loop/specification.ts
2239
+ var LoopSpecification = class {
2240
+ and(other) {
2241
+ return new AndLoopSpecification(this, other);
2242
+ }
2243
+ or(other) {
2244
+ return new OrLoopSpecification(this, other);
2245
+ }
2246
+ not() {
2247
+ return new NotLoopSpecification(this);
2248
+ }
2249
+ };
2250
+ var AndLoopSpecification = class extends LoopSpecification {
2251
+ constructor(left, right) {
2252
+ super();
2253
+ this.left = left;
2254
+ this.right = right;
2255
+ }
2256
+ isSatisfiedBy(agentLoop) {
2257
+ return this.left.isSatisfiedBy(agentLoop) && this.right.isSatisfiedBy(agentLoop);
2258
+ }
2259
+ };
2260
+ var OrLoopSpecification = class extends LoopSpecification {
2261
+ constructor(left, right) {
2262
+ super();
2263
+ this.left = left;
2264
+ this.right = right;
2265
+ }
2266
+ isSatisfiedBy(agentLoop) {
2267
+ return this.left.isSatisfiedBy(agentLoop) || this.right.isSatisfiedBy(agentLoop);
2268
+ }
2269
+ };
2270
+ var NotLoopSpecification = class extends LoopSpecification {
2271
+ constructor(rule) {
2272
+ super();
2273
+ this.rule = rule;
2274
+ }
2275
+ isSatisfiedBy(agentLoop) {
2276
+ return !this.rule.isSatisfiedBy(agentLoop);
2277
+ }
2278
+ };
2279
+ var Idle = class extends LoopSpecification {
2280
+ isSatisfiedBy(agentLoop) {
2281
+ return agentLoop.loop.stateId === "idle";
2282
+ }
2283
+ };
2284
+ var AwaitingInference = class extends LoopSpecification {
2285
+ isSatisfiedBy(agentLoop) {
2286
+ return agentLoop.loop.stateId === "awaiting_inference";
2287
+ }
2288
+ };
2289
+ var AwaitingInferenceRequest = class extends LoopSpecification {
2290
+ isSatisfiedBy(agentLoop) {
2291
+ return agentLoop.loop.stateId === "awaiting_inference_request";
2292
+ }
2293
+ };
2294
+ var AwaitingToolOutput = class extends LoopSpecification {
2295
+ isSatisfiedBy(agentLoop) {
2296
+ return agentLoop.loop.stateId === "awaiting_tool_output";
2297
+ }
2298
+ };
2299
+ var Stopped = class extends LoopSpecification {
2300
+ isSatisfiedBy(agentLoop) {
2301
+ return agentLoop.loop.stateId === "stopped";
2302
+ }
2303
+ };
2304
+ var Completed = class extends LoopSpecification {
2305
+ isSatisfiedBy(agentLoop) {
2306
+ return agentLoop.loop.stateId === "completed";
2307
+ }
2308
+ };
2309
+ var ModelAnswered = class extends LoopSpecification {
2310
+ isSatisfiedBy(agentLoop) {
2311
+ const operations = agentLoop.loop.completedOperations;
2312
+ const lastOperation = operations[operations.length - 1];
2313
+ if ((lastOperation == null ? void 0 : lastOperation.type) !== "inference") {
2314
+ return false;
2315
+ }
2316
+ return lastOperation.result.items.some((item) => item.type === "model_message");
2317
+ }
2318
+ };
2319
+
2320
+ // src/application/use-cases/loop-state.ts
2321
+ var LoopStateUseCase = class {
2322
+ execute(loopStateId) {
2323
+ if (loopStateId === "idle") {
2324
+ return new Idle();
2325
+ }
2326
+ if (loopStateId === "awaiting_inference_request") {
2327
+ return new AwaitingInferenceRequest();
2328
+ }
2329
+ if (loopStateId === "awaiting_inference") {
2330
+ return new AwaitingInference();
2331
+ }
2332
+ if (loopStateId === "awaiting_tool_output") {
2333
+ return new AwaitingToolOutput();
2334
+ }
2335
+ if (loopStateId === "stopped") {
2336
+ return new Stopped();
2337
+ }
2338
+ if (loopStateId === "completed") {
2339
+ return new Completed();
2340
+ }
2341
+ throw new Error(`Invalid loop state: ${loopStateId}`);
2342
+ }
2343
+ };
2344
+
2345
+ // src/domain/agent/loop/action.ts
2346
+ var LoopAction = class {
2347
+ };
2348
+ var InferenceAction = class extends LoopAction {
2349
+ execute(agentLoop) {
2350
+ const request = agentLoop.loop.request;
2351
+ if (!request) {
2352
+ return { type: "wait", reason: "no request" };
2353
+ }
2354
+ return { type: "inference", request };
2355
+ }
2356
+ };
2357
+ var ToolUseAction = class extends LoopAction {
2358
+ execute(agentLoop) {
2359
+ if (!agentLoop.loop.pending) {
2360
+ return { type: "wait" };
2361
+ }
2362
+ const pendingOperation = agentLoop.loop.pending;
2363
+ if (pendingOperation.type === "tool_execution") {
2364
+ return { type: "tool_use", call: pendingOperation.call };
2365
+ }
2366
+ return { type: "wait" };
2367
+ }
2368
+ };
2369
+ var CompleteAction = class extends LoopAction {
2370
+ constructor(reason) {
2371
+ super();
2372
+ this.reason = reason;
2373
+ }
2374
+ execute() {
2375
+ return { type: "complete", reason: this.reason };
2376
+ }
2377
+ };
2378
+ var WaitAction = class extends LoopAction {
2379
+ constructor(reason) {
2380
+ super();
2381
+ this.reason = reason;
2382
+ }
2383
+ execute() {
2384
+ return { type: "wait", reason: this.reason };
2385
+ }
2386
+ };
2387
+
2388
+ // src/domain/agent/loop/rule.ts
2389
+ var LoopRule = class _LoopRule {
2390
+ constructor(id, condition, action, priority) {
2391
+ this._id = id;
2392
+ this._priority = priority;
2393
+ this._condition = condition;
2394
+ this._action = action;
2395
+ }
2396
+ get id() {
2397
+ return this._id;
2398
+ }
2399
+ get priority() {
2400
+ return this._priority;
2401
+ }
2402
+ get condition() {
2403
+ return this._condition;
2404
+ }
2405
+ get action() {
2406
+ return this._action;
2407
+ }
2408
+ static create(spec) {
2409
+ var _a;
2410
+ const priority = (_a = spec.priority) != null ? _a : 0;
2411
+ const id = crypto.randomUUID();
2412
+ return new _LoopRule(id, spec.when, spec.then, priority);
2413
+ }
2414
+ static rehydrate(spec) {
2415
+ var _a;
2416
+ const priority = (_a = spec.priority) != null ? _a : 0;
2417
+ const rule = new _LoopRule(spec.id, spec.condition, spec.action, priority);
2418
+ return rule;
2419
+ }
2420
+ };
2421
+
2422
+ // src/infrastructure/memory/runtime-memory.ts
2423
+ var RuntimeMemory = class {
2424
+ constructor(context) {
2425
+ this.context = context;
2426
+ }
2427
+ getContext() {
2428
+ return this.context;
2429
+ }
2430
+ saveItem(item) {
2431
+ this.context.items.push(item);
2432
+ }
2433
+ saveItems(items) {
2434
+ this.context.items.push(...items);
2435
+ }
2436
+ };
2437
+
2438
+ // src/infrastructure/memory/runtime-memory-factory.ts
2439
+ var RuntimeMemoryFactory = class {
2440
+ create() {
2441
+ const context = {
2442
+ items: []
2443
+ };
2444
+ return new RuntimeMemory(context);
2445
+ }
2446
+ };
2447
+
2448
+ // src/application/directive-execution/directive-execution-strategy-resolver.ts
2449
+ var DirectiveExecutionStrategyResolver = class {
2450
+ constructor(strategies) {
2451
+ this.strategies = strategies;
2452
+ }
2453
+ resolve(directive) {
2454
+ return this.strategies[directive.type];
2455
+ }
2456
+ };
2457
+
2458
+ // src/application/directive-execution/complete-execution-strategy.ts
2459
+ var CompleteExecutionStrategy = class {
2460
+ async execute(directive, agent, loop) {
2461
+ if (directive.type !== "complete") {
2462
+ throw new Error("Expected a completion directive");
2463
+ }
2464
+ loop.complete(directive.reason);
2465
+ }
2466
+ };
2467
+
2468
+ // src/application/directive-execution/inference-execution-strategy.ts
2469
+ var InferenceExecutionStrategy = class {
2470
+ constructor(inferenceRunner) {
2471
+ this.inferenceRunner = inferenceRunner;
2472
+ }
2473
+ async execute(directive, agent, loop) {
2474
+ if (directive.type !== "inference") {
2475
+ throw new Error("Expected an inference directive");
2476
+ }
2477
+ const pending = loop.requestInference(directive.request);
2478
+ const result = await this.inferenceRunner.run(directive.request);
2479
+ loop.receiveInferenceResult(pending.id, result);
2480
+ }
2481
+ };
2482
+
2483
+ // src/application/directive-execution/tool-use-execution-strategy.ts
2484
+ var ToolUseExecutionStrategy = class {
2485
+ constructor(toolRunner) {
2486
+ this.toolRunner = toolRunner;
2487
+ }
2488
+ async execute(directive, agent, loop) {
2489
+ if (directive.type !== "tool_use") {
2490
+ throw new Error("Expected a tool-use directive");
2491
+ }
2492
+ const pending = loop.pending;
2493
+ if ((pending == null ? void 0 : pending.type) !== "tool_execution") {
2494
+ throw new Error("The loop has no pending tool execution");
2495
+ }
2496
+ const tool = agent.getTools().find((tool2) => tool2.name === directive.call.toolName);
2497
+ if (!tool) {
2498
+ throw new Error(`Tool "${directive.call.toolName}" not found`);
2499
+ }
2500
+ const result = await this.toolRunner.run(directive.call, tool);
2501
+ loop.receiveToolUseResult(pending.id, result);
2502
+ }
2503
+ };
2504
+
2505
+ // src/application/directive-execution/wait-execution-strategy.ts
2506
+ var WaitExecutionStrategy = class {
2507
+ async execute(directive, agent, loop) {
2508
+ if (directive.type !== "wait") {
2509
+ throw new Error("Expected a wait directive");
2510
+ }
2511
+ loop.wait(directive.reason);
2512
+ }
2513
+ };
2514
+
2515
+ // src/agent-module.ts
2516
+ function defineAgentModule(config) {
2517
+ var _a, _b, _c, _d, _e, _f, _g;
2518
+ const agentRepository = (_a = config.agentRepository) != null ? _a : new InMemoryAgentRepository();
2519
+ const agentLoopRepository = (_b = config.agentLoopRepository) != null ? _b : new InMemoryLoopRepository();
2520
+ const inferenceRunner = (_f = (_c = config.inferenceRunnerConfig) == null ? void 0 : _c.runner) != null ? _f : new DefaultInferenceRunner(
2521
+ (_e = (_d = config.inferenceRunnerConfig) == null ? void 0 : _d.supportedModels) != null ? _e : supportedModels,
2522
+ new InferenceRequestValidator()
2523
+ );
2524
+ const toolRunner = (_g = config.toolRunner) != null ? _g : new LocalToolRunner();
2525
+ const memoryFactory = new RuntimeMemoryFactory();
2526
+ const createAgentUseCase = new CreateAgentUseCase(agentRepository, memoryFactory);
2527
+ const createLoopUseCase = new CreateAgentLoopUseCase(agentLoopRepository);
2528
+ async function createAgent(config2) {
2529
+ return await createAgentUseCase.execute(config2.name, config2.instruction, config2.tools, config2.handlers);
2530
+ }
2531
+ async function createLoop(params) {
2532
+ const rules = params.rules.map((rule) => LoopRule.create(rule));
2533
+ return await createLoopUseCase.execute(params.subject, params.agentId, rules);
2534
+ }
2535
+ const directiveExecutionStrategyResolver = new DirectiveExecutionStrategyResolver({
2536
+ inference: new InferenceExecutionStrategy(inferenceRunner),
2537
+ tool_use: new ToolUseExecutionStrategy(toolRunner),
2538
+ complete: new CompleteExecutionStrategy(),
2539
+ wait: new WaitExecutionStrategy()
2540
+ });
2541
+ const runLoopUseCase = new RunLoopUseCase(agentRepository, agentLoopRepository, directiveExecutionStrategyResolver);
2542
+ async function runLoop(loopId) {
2543
+ return await runLoopUseCase.execute(loopId);
2544
+ }
2545
+ const getLoopStateUseCase = new LoopStateUseCase();
2546
+ function state(loopStateId) {
2547
+ return getLoopStateUseCase.execute(loopStateId);
2548
+ }
2549
+ function modelAnswered() {
2550
+ return new ModelAnswered();
2551
+ }
2552
+ function runInference() {
2553
+ return new InferenceAction();
2554
+ }
2555
+ function toolUse() {
2556
+ return new ToolUseAction();
2557
+ }
2558
+ function complete(reason) {
2559
+ return new CompleteAction(reason != null ? reason : "completed");
2560
+ }
2561
+ function wait(reason) {
2562
+ return new WaitAction(reason != null ? reason : "waiting");
2563
+ }
2564
+ return {
2565
+ createAgent,
2566
+ createLoop,
2567
+ runLoop,
2568
+ state,
2569
+ modelAnswered,
2570
+ runInference,
2571
+ toolUse,
2572
+ complete,
2573
+ wait
2574
+ };
2575
+ }
368
2576
  export {
369
2577
  Participant,
370
2578
  SituationSpecification,
371
2579
  SpaceEvent,
2580
+ defineAgentModule,
372
2581
  defineRuntime
373
2582
  };
374
2583
  //# sourceMappingURL=index.mjs.map