@mozaik-ai/core 3.10.3 → 3.12.0
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.d.mts +343 -9
- package/dist/index.d.ts +343 -9
- package/dist/index.js +992 -115
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +977 -114
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -1
package/dist/index.mjs
CHANGED
|
@@ -263,10 +263,10 @@ var Gpt54Nano = class {
|
|
|
263
263
|
supportFunctionCalling: true
|
|
264
264
|
};
|
|
265
265
|
this.tools = [];
|
|
266
|
+
this.streaming = false;
|
|
266
267
|
this.effort = new OpenAIReasoningEffort(
|
|
267
268
|
this.specification.defaultReasoningEffort
|
|
268
269
|
);
|
|
269
|
-
this.streaming = false;
|
|
270
270
|
}
|
|
271
271
|
setStreaming(streaming) {
|
|
272
272
|
this.streaming = streaming;
|
|
@@ -339,10 +339,10 @@ var Gpt54Mini = class {
|
|
|
339
339
|
supportFunctionCalling: true
|
|
340
340
|
};
|
|
341
341
|
this.tools = [];
|
|
342
|
+
this.streaming = false;
|
|
342
343
|
this.effort = new OpenAIReasoningEffort(
|
|
343
344
|
this.specification.defaultReasoningEffort
|
|
344
345
|
);
|
|
345
|
-
this.streaming = false;
|
|
346
346
|
}
|
|
347
347
|
setStreaming(streaming) {
|
|
348
348
|
this.streaming = streaming;
|
|
@@ -456,6 +456,7 @@ var SystemMessageItem = class _SystemMessageItem extends ContextItem {
|
|
|
456
456
|
var Participant = class {
|
|
457
457
|
constructor() {
|
|
458
458
|
this.environments = [];
|
|
459
|
+
this.listens = [];
|
|
459
460
|
}
|
|
460
461
|
join(environment) {
|
|
461
462
|
if (this.isJoinedTo(environment)) {
|
|
@@ -477,13 +478,17 @@ var Participant = class {
|
|
|
477
478
|
getEnvironments() {
|
|
478
479
|
return this.environments;
|
|
479
480
|
}
|
|
481
|
+
getListeners() {
|
|
482
|
+
return this.listens;
|
|
483
|
+
}
|
|
480
484
|
};
|
|
481
485
|
|
|
482
486
|
// src/domain/agentic-environment/agentic-environment.ts
|
|
483
487
|
var AgenticEnvironment = class {
|
|
484
|
-
constructor() {
|
|
488
|
+
constructor(name) {
|
|
485
489
|
this.subscribers = [];
|
|
486
490
|
this.isActive = false;
|
|
491
|
+
this.name = name;
|
|
487
492
|
}
|
|
488
493
|
subscribe(participant) {
|
|
489
494
|
this.subscribers.push(participant);
|
|
@@ -507,53 +512,53 @@ var AgenticEnvironment = class {
|
|
|
507
512
|
}
|
|
508
513
|
}
|
|
509
514
|
deliverFunctionCall(source, item) {
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
subscriber.onExternalFunctionCall(source, item);
|
|
515
|
-
}
|
|
516
|
-
}
|
|
515
|
+
this.deliverToSubscribers(source, {
|
|
516
|
+
internal: (src) => src.onFunctionCall(item),
|
|
517
|
+
external: (sub) => sub.onExternalFunctionCall(source, item)
|
|
518
|
+
});
|
|
517
519
|
}
|
|
518
520
|
deliverModelMessage(source, item) {
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
subscriber.onExternalModelMessage(source, item);
|
|
524
|
-
}
|
|
525
|
-
}
|
|
521
|
+
this.deliverToSubscribers(source, {
|
|
522
|
+
internal: (src) => src.onModelMessage(item),
|
|
523
|
+
external: (sub) => sub.onExternalModelMessage(source, item)
|
|
524
|
+
});
|
|
526
525
|
}
|
|
527
526
|
deliverReasoning(source, item) {
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
subscriber.onExternalReasoning(source, item);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
527
|
+
this.deliverToSubscribers(source, {
|
|
528
|
+
internal: (src) => src.onReasoning(item),
|
|
529
|
+
external: (sub) => sub.onExternalReasoning(source, item)
|
|
530
|
+
});
|
|
535
531
|
}
|
|
536
532
|
deliverFunctionCallOutput(source, item) {
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
subscriber.onExternalFunctionCallOutput(source, item);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
533
|
+
this.deliverToSubscribers(source, {
|
|
534
|
+
internal: (src) => src.onFunctionCallOutput(item),
|
|
535
|
+
external: (sub) => sub.onExternalFunctionCallOutput(source, item)
|
|
536
|
+
});
|
|
544
537
|
}
|
|
545
538
|
deliverMessage(source, message) {
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
}
|
|
539
|
+
this.deliverToSubscribers(source, {
|
|
540
|
+
external: (sub) => sub.onMessage(message)
|
|
541
|
+
});
|
|
550
542
|
}
|
|
551
543
|
deliverSemanticEvent(source, item) {
|
|
544
|
+
this.deliverToSubscribers(source, {
|
|
545
|
+
internal: (src) => src.onInternalEvent(item),
|
|
546
|
+
external: (sub) => sub.onExternalEvent(source, item)
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
getListeningParticipants(source) {
|
|
550
|
+
return this.subscribers.filter(
|
|
551
|
+
(sub) => sub !== source && (sub.getListeners().length === 0 || sub.getListeners().some((listener) => source instanceof listener))
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
deliverToSubscribers(source, handlers) {
|
|
555
|
+
var _a, _b;
|
|
556
|
+
const authorizedListeners = this.getListeningParticipants(source);
|
|
552
557
|
for (const subscriber of this.subscribers) {
|
|
553
558
|
if (subscriber === source) {
|
|
554
|
-
|
|
555
|
-
} else {
|
|
556
|
-
|
|
559
|
+
(_a = handlers == null ? void 0 : handlers.internal) == null ? void 0 : _a.call(handlers, subscriber);
|
|
560
|
+
} else if (authorizedListeners.includes(subscriber)) {
|
|
561
|
+
(_b = handlers == null ? void 0 : handlers.external) == null ? void 0 : _b.call(handlers, subscriber);
|
|
557
562
|
}
|
|
558
563
|
}
|
|
559
564
|
}
|
|
@@ -686,86 +691,35 @@ var BaseHuman = class extends Human {
|
|
|
686
691
|
}
|
|
687
692
|
};
|
|
688
693
|
|
|
689
|
-
// src/
|
|
690
|
-
|
|
691
|
-
|
|
694
|
+
// src/domain/generative-model/inference-request.ts
|
|
695
|
+
var InferenceRequest = class {
|
|
696
|
+
constructor(model, context) {
|
|
697
|
+
this.model = model;
|
|
698
|
+
this.context = context;
|
|
699
|
+
}
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
// src/application/runners/inference-runner.ts
|
|
703
|
+
var DefaultInferenceRunner = class {
|
|
704
|
+
constructor(runtime) {
|
|
705
|
+
this.runtime = runtime;
|
|
706
|
+
}
|
|
692
707
|
async *run(context, model, signal) {
|
|
693
|
-
const
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
input,
|
|
698
|
-
stream: model.getStreaming()
|
|
699
|
-
};
|
|
700
|
-
if (specification.supportFunctionCalling && model.getTools().length > 0) {
|
|
701
|
-
request.tools = model.getTools().map((tool) => {
|
|
702
|
-
return {
|
|
703
|
-
type: tool.type,
|
|
704
|
-
name: tool.name,
|
|
705
|
-
description: tool.description,
|
|
706
|
-
parameters: tool.parameters
|
|
707
|
-
};
|
|
708
|
-
});
|
|
709
|
-
}
|
|
710
|
-
if (specification.supportReasoningEffort) {
|
|
711
|
-
request.reasoning = {
|
|
712
|
-
effort: model.getReasoningEffort()
|
|
713
|
-
};
|
|
714
|
-
}
|
|
715
|
-
if (!specification.supportStreaming && model.getStreaming()) {
|
|
716
|
-
throw new Error("Streaming is not supported for this model");
|
|
708
|
+
const request = new InferenceRequest(model, context);
|
|
709
|
+
if (model.getStreaming() && this.isStreamingRuntime(this.runtime)) {
|
|
710
|
+
yield* this.runtime.stream(request, signal);
|
|
711
|
+
return;
|
|
717
712
|
}
|
|
718
|
-
const
|
|
719
|
-
const
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
if (signal == null ? void 0 : signal.aborted) {
|
|
723
|
-
break;
|
|
724
|
-
}
|
|
725
|
-
yield new SemanticEvent(event.type, event);
|
|
726
|
-
}
|
|
727
|
-
} else {
|
|
728
|
-
const contextItems = this.extractContextItems(response);
|
|
729
|
-
const tokenUsage = this.extractTokenUsage(response);
|
|
730
|
-
for (const item of contextItems) {
|
|
731
|
-
if (signal == null ? void 0 : signal.aborted) {
|
|
732
|
-
break;
|
|
733
|
-
}
|
|
734
|
-
yield item;
|
|
713
|
+
const response = await this.runtime.infer(request);
|
|
714
|
+
for (const item of response.contextItems) {
|
|
715
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
716
|
+
break;
|
|
735
717
|
}
|
|
718
|
+
yield item;
|
|
736
719
|
}
|
|
737
720
|
}
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
return void 0;
|
|
741
|
-
}
|
|
742
|
-
return new TokenUsage(
|
|
743
|
-
response.usage.input_tokens,
|
|
744
|
-
response.usage.output_tokens,
|
|
745
|
-
response.usage.total_tokens,
|
|
746
|
-
new InputTokenDetails(response.usage.input_tokens_details.cached_tokens),
|
|
747
|
-
new OutputTokenDetails(response.usage.output_tokens_details.reasoning_tokens)
|
|
748
|
-
);
|
|
749
|
-
}
|
|
750
|
-
mapContextToRequest(context) {
|
|
751
|
-
return context.getItems().map((item) => item.toJSON());
|
|
752
|
-
}
|
|
753
|
-
extractContextItems(response) {
|
|
754
|
-
return response.output.map((item) => {
|
|
755
|
-
if (item.type === "message" && item.role === "assistant") {
|
|
756
|
-
return ModelMessageItem.rehydrate(item.content[0]);
|
|
757
|
-
}
|
|
758
|
-
if (item.type === "function_call") {
|
|
759
|
-
return FunctionCallItem.rehydrate({
|
|
760
|
-
callId: item.call_id,
|
|
761
|
-
name: item.name,
|
|
762
|
-
args: item.arguments
|
|
763
|
-
});
|
|
764
|
-
}
|
|
765
|
-
if (item.type === "reasoning") {
|
|
766
|
-
return ReasoningItem.rehydrate(item);
|
|
767
|
-
}
|
|
768
|
-
});
|
|
721
|
+
isStreamingRuntime(runtime) {
|
|
722
|
+
return typeof runtime.stream === "function";
|
|
769
723
|
}
|
|
770
724
|
};
|
|
771
725
|
|
|
@@ -857,28 +811,937 @@ var BaseObserver = class extends Observer {
|
|
|
857
811
|
onExternalEvent(source, item) {
|
|
858
812
|
}
|
|
859
813
|
};
|
|
814
|
+
|
|
815
|
+
// src/domain/generative-model/inference-response.ts
|
|
816
|
+
var InferenceResponse = class {
|
|
817
|
+
constructor(contextItems, tokenUsage) {
|
|
818
|
+
this.contextItems = contextItems;
|
|
819
|
+
this.tokenUsage = tokenUsage;
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
// src/infrastructure/providers/openai/runtime/openai-responses.ts
|
|
824
|
+
import OpenAI from "openai";
|
|
825
|
+
var OpenAIResponses = class {
|
|
826
|
+
constructor() {
|
|
827
|
+
this.client = new OpenAI();
|
|
828
|
+
}
|
|
829
|
+
async infer(inferenceRequest) {
|
|
830
|
+
const request = this.buildRequest(inferenceRequest);
|
|
831
|
+
const response = await this.client.responses.create(request);
|
|
832
|
+
const contextItems = this.extractContextItems(response);
|
|
833
|
+
const tokenUsage = this.extractTokenUsage(response);
|
|
834
|
+
return new InferenceResponse(contextItems, tokenUsage);
|
|
835
|
+
}
|
|
836
|
+
async *stream(inferenceRequest, signal) {
|
|
837
|
+
const specification = inferenceRequest.model.specification;
|
|
838
|
+
if (!specification.supportStreaming) {
|
|
839
|
+
throw new Error("Streaming is not supported for this model");
|
|
840
|
+
}
|
|
841
|
+
const request = { ...this.buildRequest(inferenceRequest), stream: true };
|
|
842
|
+
const response = await this.client.responses.create(request);
|
|
843
|
+
for await (const event of response) {
|
|
844
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
845
|
+
break;
|
|
846
|
+
}
|
|
847
|
+
yield new SemanticEvent(event.type, event);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
buildRequest(inferenceRequest) {
|
|
851
|
+
const specification = inferenceRequest.model.specification;
|
|
852
|
+
const input = this.mapContextToRequest(inferenceRequest.context);
|
|
853
|
+
const request = {
|
|
854
|
+
model: specification.name,
|
|
855
|
+
input
|
|
856
|
+
};
|
|
857
|
+
if (specification.supportFunctionCalling && inferenceRequest.model.getTools().length > 0) {
|
|
858
|
+
request.tools = inferenceRequest.model.getTools().map((tool) => {
|
|
859
|
+
return {
|
|
860
|
+
type: tool.type,
|
|
861
|
+
name: tool.name,
|
|
862
|
+
description: tool.description,
|
|
863
|
+
parameters: tool.parameters
|
|
864
|
+
};
|
|
865
|
+
});
|
|
866
|
+
}
|
|
867
|
+
if (specification.supportReasoningEffort) {
|
|
868
|
+
request.reasoning = {
|
|
869
|
+
effort: inferenceRequest.model.getReasoningEffort()
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
return request;
|
|
873
|
+
}
|
|
874
|
+
extractTokenUsage(response) {
|
|
875
|
+
if (!response.usage) {
|
|
876
|
+
return void 0;
|
|
877
|
+
}
|
|
878
|
+
return new TokenUsage(
|
|
879
|
+
response.usage.input_tokens,
|
|
880
|
+
response.usage.output_tokens,
|
|
881
|
+
response.usage.total_tokens,
|
|
882
|
+
new InputTokenDetails(response.usage.input_tokens_details.cached_tokens),
|
|
883
|
+
new OutputTokenDetails(response.usage.output_tokens_details.reasoning_tokens)
|
|
884
|
+
);
|
|
885
|
+
}
|
|
886
|
+
mapContextToRequest(context) {
|
|
887
|
+
return context.getItems().map((item) => item.toJSON());
|
|
888
|
+
}
|
|
889
|
+
extractContextItems(response) {
|
|
890
|
+
return response.output.map((item) => {
|
|
891
|
+
if (item.type === "message" && item.role === "assistant") {
|
|
892
|
+
return ModelMessageItem.rehydrate(item.content[0]);
|
|
893
|
+
}
|
|
894
|
+
if (item.type === "function_call") {
|
|
895
|
+
return FunctionCallItem.rehydrate({
|
|
896
|
+
callId: item.call_id,
|
|
897
|
+
name: item.name,
|
|
898
|
+
args: item.arguments
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
if (item.type === "reasoning") {
|
|
902
|
+
return ReasoningItem.rehydrate(item);
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
// src/infrastructure/providers/openai/runtime/openai-compatible-chat-completions.ts
|
|
909
|
+
import OpenAI2 from "openai";
|
|
910
|
+
var OpenAICompatibleChatCompletions = class {
|
|
911
|
+
constructor(config = {}) {
|
|
912
|
+
var _a;
|
|
913
|
+
this.client = new OpenAI2({
|
|
914
|
+
baseURL: config.baseURL,
|
|
915
|
+
apiKey: config.apiKey
|
|
916
|
+
});
|
|
917
|
+
this.extraBody = (_a = config.extraBody) != null ? _a : {};
|
|
918
|
+
}
|
|
919
|
+
async infer(inferenceRequest) {
|
|
920
|
+
const response = await this.client.chat.completions.create(this.buildRequest(inferenceRequest));
|
|
921
|
+
const contextItems = this.extractContextItems(response);
|
|
922
|
+
const tokenUsage = this.extractTokenUsage(response);
|
|
923
|
+
return new InferenceResponse(contextItems, tokenUsage);
|
|
924
|
+
}
|
|
925
|
+
async *stream(inferenceRequest, signal) {
|
|
926
|
+
var _a;
|
|
927
|
+
const stream = await this.client.chat.completions.create({
|
|
928
|
+
...this.buildRequest(inferenceRequest),
|
|
929
|
+
stream: true
|
|
930
|
+
});
|
|
931
|
+
for await (const event of stream) {
|
|
932
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
933
|
+
break;
|
|
934
|
+
}
|
|
935
|
+
yield new SemanticEvent((_a = event.object) != null ? _a : "chat.completion.chunk", event);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
buildRequest(inferenceRequest) {
|
|
939
|
+
const specification = inferenceRequest.model.specification;
|
|
940
|
+
const request = {
|
|
941
|
+
...this.extraBody,
|
|
942
|
+
model: specification.name,
|
|
943
|
+
messages: this.mapContextToRequest(inferenceRequest.context)
|
|
944
|
+
};
|
|
945
|
+
if (specification.supportFunctionCalling && inferenceRequest.model.getTools().length > 0) {
|
|
946
|
+
request.tools = inferenceRequest.model.getTools().map((tool) => {
|
|
947
|
+
return {
|
|
948
|
+
type: "function",
|
|
949
|
+
function: {
|
|
950
|
+
name: tool.name,
|
|
951
|
+
description: tool.description,
|
|
952
|
+
parameters: tool.parameters
|
|
953
|
+
}
|
|
954
|
+
};
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
if (specification.supportReasoningEffort) {
|
|
958
|
+
request.reasoning_effort = inferenceRequest.model.getReasoningEffort();
|
|
959
|
+
}
|
|
960
|
+
return request;
|
|
961
|
+
}
|
|
962
|
+
extractTokenUsage(response) {
|
|
963
|
+
var _a, _b, _c, _d;
|
|
964
|
+
if (!response.usage) {
|
|
965
|
+
return void 0;
|
|
966
|
+
}
|
|
967
|
+
return new TokenUsage(
|
|
968
|
+
response.usage.prompt_tokens,
|
|
969
|
+
response.usage.completion_tokens,
|
|
970
|
+
response.usage.total_tokens,
|
|
971
|
+
new InputTokenDetails((_b = (_a = response.usage.prompt_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0),
|
|
972
|
+
new OutputTokenDetails((_d = (_c = response.usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0)
|
|
973
|
+
);
|
|
974
|
+
}
|
|
975
|
+
mapContextToRequest(context) {
|
|
976
|
+
var _a;
|
|
977
|
+
const messages = [];
|
|
978
|
+
for (const item of context.getItems()) {
|
|
979
|
+
if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem) {
|
|
980
|
+
messages.push({ role: "system", content: item.content.text });
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
if (item instanceof UserMessageItem) {
|
|
984
|
+
messages.push({ role: "user", content: item.content.text });
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
if (item instanceof ModelMessageItem) {
|
|
988
|
+
messages.push({ role: "assistant", content: item.content.text });
|
|
989
|
+
continue;
|
|
990
|
+
}
|
|
991
|
+
if (item instanceof FunctionCallItem) {
|
|
992
|
+
const toolCall = {
|
|
993
|
+
id: item.callId,
|
|
994
|
+
type: "function",
|
|
995
|
+
function: { name: item.name, arguments: item.args }
|
|
996
|
+
};
|
|
997
|
+
const last = messages[messages.length - 1];
|
|
998
|
+
if ((last == null ? void 0 : last.role) === "assistant") {
|
|
999
|
+
last.tool_calls = (_a = last.tool_calls) != null ? _a : [];
|
|
1000
|
+
last.tool_calls.push(toolCall);
|
|
1001
|
+
} else {
|
|
1002
|
+
messages.push({ role: "assistant", content: null, tool_calls: [toolCall] });
|
|
1003
|
+
}
|
|
1004
|
+
continue;
|
|
1005
|
+
}
|
|
1006
|
+
if (item instanceof FunctionCallOutputItem) {
|
|
1007
|
+
messages.push({ role: "tool", tool_call_id: item.callId, content: item.output.text });
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
return messages;
|
|
1011
|
+
}
|
|
1012
|
+
extractContextItems(response) {
|
|
1013
|
+
var _a, _b, _c;
|
|
1014
|
+
const items = [];
|
|
1015
|
+
const message = (_b = (_a = response.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
|
|
1016
|
+
if (!message) {
|
|
1017
|
+
return items;
|
|
1018
|
+
}
|
|
1019
|
+
if (message.reasoning_content) {
|
|
1020
|
+
items.push(
|
|
1021
|
+
ReasoningItem.rehydrate({
|
|
1022
|
+
content: InputText.rehydrate({ text: message.reasoning_content }),
|
|
1023
|
+
encryptedContent: void 0,
|
|
1024
|
+
summary: []
|
|
1025
|
+
})
|
|
1026
|
+
);
|
|
1027
|
+
}
|
|
1028
|
+
if (message.content) {
|
|
1029
|
+
items.push(ModelMessageItem.rehydrate({ text: message.content }));
|
|
1030
|
+
}
|
|
1031
|
+
for (const toolCall of (_c = message.tool_calls) != null ? _c : []) {
|
|
1032
|
+
items.push(
|
|
1033
|
+
FunctionCallItem.rehydrate({
|
|
1034
|
+
callId: toolCall.id,
|
|
1035
|
+
name: toolCall.function.name,
|
|
1036
|
+
args: toolCall.function.arguments
|
|
1037
|
+
})
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
return items;
|
|
1041
|
+
}
|
|
1042
|
+
};
|
|
1043
|
+
|
|
1044
|
+
// src/infrastructure/providers/anthropic/runtime/anthropic-messages.ts
|
|
1045
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
1046
|
+
var AnthropicMessages = class {
|
|
1047
|
+
constructor() {
|
|
1048
|
+
this.client = new Anthropic();
|
|
1049
|
+
}
|
|
1050
|
+
async infer(inferenceRequest) {
|
|
1051
|
+
const response = await this.client.messages.create(this.buildRequest(inferenceRequest));
|
|
1052
|
+
const contextItems = this.extractContextItems(response);
|
|
1053
|
+
const tokenUsage = this.extractTokenUsage(response);
|
|
1054
|
+
return new InferenceResponse(contextItems, tokenUsage);
|
|
1055
|
+
}
|
|
1056
|
+
async *stream(inferenceRequest, signal) {
|
|
1057
|
+
const stream = await this.client.messages.create({
|
|
1058
|
+
...this.buildRequest(inferenceRequest),
|
|
1059
|
+
stream: true
|
|
1060
|
+
});
|
|
1061
|
+
for await (const event of stream) {
|
|
1062
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
1063
|
+
break;
|
|
1064
|
+
}
|
|
1065
|
+
yield new SemanticEvent(event.type, event);
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
buildRequest(inferenceRequest) {
|
|
1069
|
+
const { messages, system } = this.mapContextToRequest(inferenceRequest.context);
|
|
1070
|
+
const specification = inferenceRequest.model.specification;
|
|
1071
|
+
const request = {
|
|
1072
|
+
model: specification.name,
|
|
1073
|
+
max_tokens: specification.maxOutputTokens,
|
|
1074
|
+
messages
|
|
1075
|
+
};
|
|
1076
|
+
if (system) {
|
|
1077
|
+
request.system = system;
|
|
1078
|
+
}
|
|
1079
|
+
if (specification.supportFunctionCalling && inferenceRequest.model.getTools().length > 0) {
|
|
1080
|
+
request.tools = inferenceRequest.model.getTools().map((tool) => {
|
|
1081
|
+
return {
|
|
1082
|
+
name: tool.name,
|
|
1083
|
+
description: tool.description,
|
|
1084
|
+
input_schema: tool.parameters
|
|
1085
|
+
};
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
if (specification.supportReasoningEffort) {
|
|
1089
|
+
const effort = inferenceRequest.model.getReasoningEffort();
|
|
1090
|
+
const budgets = { low: 1024, medium: 4096, high: 8192 };
|
|
1091
|
+
const budget = budgets[effort];
|
|
1092
|
+
if (budget !== void 0) {
|
|
1093
|
+
request.thinking = {
|
|
1094
|
+
type: "enabled",
|
|
1095
|
+
budget_tokens: Math.min(budget, specification.maxOutputTokens - 1)
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
return request;
|
|
1100
|
+
}
|
|
1101
|
+
extractTokenUsage(response) {
|
|
1102
|
+
var _a, _b;
|
|
1103
|
+
if (!response.usage) {
|
|
1104
|
+
return void 0;
|
|
1105
|
+
}
|
|
1106
|
+
return new TokenUsage(
|
|
1107
|
+
response.usage.input_tokens,
|
|
1108
|
+
response.usage.output_tokens,
|
|
1109
|
+
response.usage.input_tokens + response.usage.output_tokens,
|
|
1110
|
+
new InputTokenDetails(
|
|
1111
|
+
((_a = response.usage.cache_creation_input_tokens) != null ? _a : 0) + ((_b = response.usage.cache_read_input_tokens) != null ? _b : 0)
|
|
1112
|
+
),
|
|
1113
|
+
new OutputTokenDetails(0)
|
|
1114
|
+
);
|
|
1115
|
+
}
|
|
1116
|
+
mapContextToRequest(context) {
|
|
1117
|
+
const messages = [];
|
|
1118
|
+
const system = [];
|
|
1119
|
+
for (const item of context.getItems()) {
|
|
1120
|
+
if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem) {
|
|
1121
|
+
system.push(item.content.text);
|
|
1122
|
+
continue;
|
|
1123
|
+
}
|
|
1124
|
+
if (item instanceof UserMessageItem) {
|
|
1125
|
+
this.addContentBlock(messages, "user", { type: "text", text: item.content.text });
|
|
1126
|
+
continue;
|
|
1127
|
+
}
|
|
1128
|
+
if (item instanceof ModelMessageItem) {
|
|
1129
|
+
this.addContentBlock(messages, "assistant", { type: "text", text: item.content.text });
|
|
1130
|
+
continue;
|
|
1131
|
+
}
|
|
1132
|
+
if (item instanceof FunctionCallItem) {
|
|
1133
|
+
let input;
|
|
1134
|
+
try {
|
|
1135
|
+
input = JSON.parse(item.args);
|
|
1136
|
+
} catch (e) {
|
|
1137
|
+
input = item.args;
|
|
1138
|
+
}
|
|
1139
|
+
this.addContentBlock(messages, "assistant", {
|
|
1140
|
+
type: "tool_use",
|
|
1141
|
+
id: item.callId,
|
|
1142
|
+
name: item.name,
|
|
1143
|
+
input
|
|
1144
|
+
});
|
|
1145
|
+
continue;
|
|
1146
|
+
}
|
|
1147
|
+
if (item instanceof FunctionCallOutputItem) {
|
|
1148
|
+
this.addContentBlock(messages, "user", {
|
|
1149
|
+
type: "tool_result",
|
|
1150
|
+
tool_use_id: item.callId,
|
|
1151
|
+
content: item.output.text
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
return {
|
|
1156
|
+
messages,
|
|
1157
|
+
system: system.length > 0 ? system.join("\n\n") : void 0
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
extractContextItems(response) {
|
|
1161
|
+
var _a;
|
|
1162
|
+
const items = [];
|
|
1163
|
+
for (const block of response.content) {
|
|
1164
|
+
if (block.type === "text") {
|
|
1165
|
+
items.push(ModelMessageItem.rehydrate({ text: block.text }));
|
|
1166
|
+
continue;
|
|
1167
|
+
}
|
|
1168
|
+
if (block.type === "tool_use") {
|
|
1169
|
+
items.push(
|
|
1170
|
+
FunctionCallItem.rehydrate({
|
|
1171
|
+
callId: block.id,
|
|
1172
|
+
name: block.name,
|
|
1173
|
+
args: JSON.stringify((_a = block.input) != null ? _a : {})
|
|
1174
|
+
})
|
|
1175
|
+
);
|
|
1176
|
+
continue;
|
|
1177
|
+
}
|
|
1178
|
+
if (block.type === "thinking") {
|
|
1179
|
+
items.push(
|
|
1180
|
+
ReasoningItem.rehydrate({
|
|
1181
|
+
content: block.thinking ? InputText.rehydrate({ text: block.thinking }) : void 0,
|
|
1182
|
+
encryptedContent: void 0,
|
|
1183
|
+
summary: []
|
|
1184
|
+
})
|
|
1185
|
+
);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
return items;
|
|
1189
|
+
}
|
|
1190
|
+
addContentBlock(messages, role, block) {
|
|
1191
|
+
const lastMessage = messages[messages.length - 1];
|
|
1192
|
+
if ((lastMessage == null ? void 0 : lastMessage.role) === role) {
|
|
1193
|
+
lastMessage.content.push(block);
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
messages.push({
|
|
1197
|
+
role,
|
|
1198
|
+
content: [block]
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
1201
|
+
};
|
|
1202
|
+
|
|
1203
|
+
// src/infrastructure/providers/anthropic/reasoning-effort.ts
|
|
1204
|
+
var AnthropicReasoningEffort = class {
|
|
1205
|
+
constructor(reasoningEffort) {
|
|
1206
|
+
this.reasoningEffort = reasoningEffort;
|
|
1207
|
+
}
|
|
1208
|
+
setReasoningEffort(effort) {
|
|
1209
|
+
this.reasoningEffort = effort;
|
|
1210
|
+
}
|
|
1211
|
+
getReasoningEffort() {
|
|
1212
|
+
if (!this.reasoningEffort) {
|
|
1213
|
+
throw new Error("Reasoning effort not supported");
|
|
1214
|
+
}
|
|
1215
|
+
return this.reasoningEffort;
|
|
1216
|
+
}
|
|
1217
|
+
};
|
|
1218
|
+
|
|
1219
|
+
// src/infrastructure/providers/anthropic/models/claude-4-8-opus.ts
|
|
1220
|
+
var ClaudeOpus48 = class {
|
|
1221
|
+
constructor() {
|
|
1222
|
+
this.specification = {
|
|
1223
|
+
name: "claude-opus-4-8",
|
|
1224
|
+
supportReasoningEffort: true,
|
|
1225
|
+
defaultReasoningEffort: "none",
|
|
1226
|
+
supportStreaming: true,
|
|
1227
|
+
contextWindowSize: 2e5,
|
|
1228
|
+
maxOutputTokens: 32e3,
|
|
1229
|
+
supportFunctionCalling: true
|
|
1230
|
+
};
|
|
1231
|
+
this.tools = [];
|
|
1232
|
+
this.streaming = false;
|
|
1233
|
+
this.effort = new AnthropicReasoningEffort(
|
|
1234
|
+
this.specification.defaultReasoningEffort
|
|
1235
|
+
);
|
|
1236
|
+
}
|
|
1237
|
+
setStreaming(streaming) {
|
|
1238
|
+
this.streaming = streaming;
|
|
1239
|
+
}
|
|
1240
|
+
getStreaming() {
|
|
1241
|
+
return this.streaming;
|
|
1242
|
+
}
|
|
1243
|
+
setTools(tools) {
|
|
1244
|
+
this.tools = tools;
|
|
1245
|
+
}
|
|
1246
|
+
getTools() {
|
|
1247
|
+
return this.tools;
|
|
1248
|
+
}
|
|
1249
|
+
setReasoningEffort(effort) {
|
|
1250
|
+
this.effort.setReasoningEffort(effort);
|
|
1251
|
+
}
|
|
1252
|
+
getReasoningEffort() {
|
|
1253
|
+
return this.effort.getReasoningEffort();
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
|
|
1257
|
+
// src/infrastructure/providers/anthropic/models/claude-4-7-opus.ts
|
|
1258
|
+
var ClaudeOpus47 = class {
|
|
1259
|
+
constructor() {
|
|
1260
|
+
this.specification = {
|
|
1261
|
+
name: "claude-opus-4-7",
|
|
1262
|
+
supportReasoningEffort: true,
|
|
1263
|
+
defaultReasoningEffort: "none",
|
|
1264
|
+
supportStreaming: true,
|
|
1265
|
+
contextWindowSize: 2e5,
|
|
1266
|
+
maxOutputTokens: 32e3,
|
|
1267
|
+
supportFunctionCalling: true
|
|
1268
|
+
};
|
|
1269
|
+
this.tools = [];
|
|
1270
|
+
this.streaming = false;
|
|
1271
|
+
this.effort = new AnthropicReasoningEffort(
|
|
1272
|
+
this.specification.defaultReasoningEffort
|
|
1273
|
+
);
|
|
1274
|
+
}
|
|
1275
|
+
setStreaming(streaming) {
|
|
1276
|
+
this.streaming = streaming;
|
|
1277
|
+
}
|
|
1278
|
+
getStreaming() {
|
|
1279
|
+
return this.streaming;
|
|
1280
|
+
}
|
|
1281
|
+
setTools(tools) {
|
|
1282
|
+
this.tools = tools;
|
|
1283
|
+
}
|
|
1284
|
+
getTools() {
|
|
1285
|
+
return this.tools;
|
|
1286
|
+
}
|
|
1287
|
+
setReasoningEffort(effort) {
|
|
1288
|
+
this.effort.setReasoningEffort(effort);
|
|
1289
|
+
}
|
|
1290
|
+
getReasoningEffort() {
|
|
1291
|
+
return this.effort.getReasoningEffort();
|
|
1292
|
+
}
|
|
1293
|
+
};
|
|
1294
|
+
|
|
1295
|
+
// src/infrastructure/providers/anthropic/models/claude-4-6-sonnet.ts
|
|
1296
|
+
var ClaudeSonnet46 = class {
|
|
1297
|
+
constructor() {
|
|
1298
|
+
this.specification = {
|
|
1299
|
+
name: "claude-sonnet-4-6",
|
|
1300
|
+
supportReasoningEffort: true,
|
|
1301
|
+
defaultReasoningEffort: "none",
|
|
1302
|
+
supportStreaming: true,
|
|
1303
|
+
contextWindowSize: 2e5,
|
|
1304
|
+
maxOutputTokens: 64e3,
|
|
1305
|
+
supportFunctionCalling: true
|
|
1306
|
+
};
|
|
1307
|
+
this.tools = [];
|
|
1308
|
+
this.streaming = false;
|
|
1309
|
+
this.effort = new AnthropicReasoningEffort(
|
|
1310
|
+
this.specification.defaultReasoningEffort
|
|
1311
|
+
);
|
|
1312
|
+
}
|
|
1313
|
+
setStreaming(streaming) {
|
|
1314
|
+
this.streaming = streaming;
|
|
1315
|
+
}
|
|
1316
|
+
getStreaming() {
|
|
1317
|
+
return this.streaming;
|
|
1318
|
+
}
|
|
1319
|
+
setTools(tools) {
|
|
1320
|
+
this.tools = tools;
|
|
1321
|
+
}
|
|
1322
|
+
getTools() {
|
|
1323
|
+
return this.tools;
|
|
1324
|
+
}
|
|
1325
|
+
setReasoningEffort(effort) {
|
|
1326
|
+
this.effort.setReasoningEffort(effort);
|
|
1327
|
+
}
|
|
1328
|
+
getReasoningEffort() {
|
|
1329
|
+
return this.effort.getReasoningEffort();
|
|
1330
|
+
}
|
|
1331
|
+
};
|
|
1332
|
+
|
|
1333
|
+
// src/infrastructure/providers/anthropic/models/claude-4-5-haiku.ts
|
|
1334
|
+
var ClaudeHaiku45 = class {
|
|
1335
|
+
constructor() {
|
|
1336
|
+
this.specification = {
|
|
1337
|
+
name: "claude-haiku-4-5",
|
|
1338
|
+
supportReasoningEffort: true,
|
|
1339
|
+
defaultReasoningEffort: "none",
|
|
1340
|
+
supportStreaming: true,
|
|
1341
|
+
contextWindowSize: 2e5,
|
|
1342
|
+
maxOutputTokens: 64e3,
|
|
1343
|
+
supportFunctionCalling: true
|
|
1344
|
+
};
|
|
1345
|
+
this.tools = [];
|
|
1346
|
+
this.streaming = false;
|
|
1347
|
+
this.effort = new AnthropicReasoningEffort(
|
|
1348
|
+
this.specification.defaultReasoningEffort
|
|
1349
|
+
);
|
|
1350
|
+
}
|
|
1351
|
+
setStreaming(streaming) {
|
|
1352
|
+
this.streaming = streaming;
|
|
1353
|
+
}
|
|
1354
|
+
getStreaming() {
|
|
1355
|
+
return this.streaming;
|
|
1356
|
+
}
|
|
1357
|
+
setTools(tools) {
|
|
1358
|
+
this.tools = tools;
|
|
1359
|
+
}
|
|
1360
|
+
getTools() {
|
|
1361
|
+
return this.tools;
|
|
1362
|
+
}
|
|
1363
|
+
setReasoningEffort(effort) {
|
|
1364
|
+
this.effort.setReasoningEffort(effort);
|
|
1365
|
+
}
|
|
1366
|
+
getReasoningEffort() {
|
|
1367
|
+
return this.effort.getReasoningEffort();
|
|
1368
|
+
}
|
|
1369
|
+
};
|
|
1370
|
+
|
|
1371
|
+
// src/infrastructure/providers/deepseek/reasoning-effort.ts
|
|
1372
|
+
var DeepSeekReasoningEffort = class {
|
|
1373
|
+
constructor(reasoningEffort) {
|
|
1374
|
+
this.reasoningEffort = reasoningEffort;
|
|
1375
|
+
}
|
|
1376
|
+
setReasoningEffort(effort) {
|
|
1377
|
+
this.reasoningEffort = effort;
|
|
1378
|
+
}
|
|
1379
|
+
getReasoningEffort() {
|
|
1380
|
+
if (!this.reasoningEffort) {
|
|
1381
|
+
throw new Error("Reasoning effort not supported");
|
|
1382
|
+
}
|
|
1383
|
+
return this.reasoningEffort;
|
|
1384
|
+
}
|
|
1385
|
+
};
|
|
1386
|
+
|
|
1387
|
+
// src/infrastructure/providers/deepseek/models/deepseek-v4-flash.ts
|
|
1388
|
+
var DeepSeekV4Flash = class {
|
|
1389
|
+
constructor() {
|
|
1390
|
+
this.specification = {
|
|
1391
|
+
name: "deepseek-v4-flash",
|
|
1392
|
+
supportReasoningEffort: true,
|
|
1393
|
+
defaultReasoningEffort: "high",
|
|
1394
|
+
supportStreaming: true,
|
|
1395
|
+
contextWindowSize: 1e6,
|
|
1396
|
+
maxOutputTokens: 384e3,
|
|
1397
|
+
supportFunctionCalling: true
|
|
1398
|
+
};
|
|
1399
|
+
this.tools = [];
|
|
1400
|
+
this.streaming = false;
|
|
1401
|
+
this.effort = new DeepSeekReasoningEffort(
|
|
1402
|
+
this.specification.defaultReasoningEffort
|
|
1403
|
+
);
|
|
1404
|
+
}
|
|
1405
|
+
setStreaming(streaming) {
|
|
1406
|
+
this.streaming = streaming;
|
|
1407
|
+
}
|
|
1408
|
+
getStreaming() {
|
|
1409
|
+
return this.streaming;
|
|
1410
|
+
}
|
|
1411
|
+
setTools(tools) {
|
|
1412
|
+
this.tools = tools;
|
|
1413
|
+
}
|
|
1414
|
+
getTools() {
|
|
1415
|
+
return this.tools;
|
|
1416
|
+
}
|
|
1417
|
+
setReasoningEffort(effort) {
|
|
1418
|
+
this.effort.setReasoningEffort(effort);
|
|
1419
|
+
}
|
|
1420
|
+
getReasoningEffort() {
|
|
1421
|
+
return this.effort.getReasoningEffort();
|
|
1422
|
+
}
|
|
1423
|
+
};
|
|
1424
|
+
|
|
1425
|
+
// src/infrastructure/providers/deepseek/models/deepseek-v4-pro.ts
|
|
1426
|
+
var DeepSeekV4Pro = class {
|
|
1427
|
+
constructor() {
|
|
1428
|
+
this.specification = {
|
|
1429
|
+
name: "deepseek-v4-pro",
|
|
1430
|
+
supportReasoningEffort: true,
|
|
1431
|
+
defaultReasoningEffort: "high",
|
|
1432
|
+
supportStreaming: true,
|
|
1433
|
+
contextWindowSize: 1e6,
|
|
1434
|
+
maxOutputTokens: 384e3,
|
|
1435
|
+
supportFunctionCalling: true
|
|
1436
|
+
};
|
|
1437
|
+
this.tools = [];
|
|
1438
|
+
this.streaming = false;
|
|
1439
|
+
this.effort = new DeepSeekReasoningEffort(
|
|
1440
|
+
this.specification.defaultReasoningEffort
|
|
1441
|
+
);
|
|
1442
|
+
}
|
|
1443
|
+
setStreaming(streaming) {
|
|
1444
|
+
this.streaming = streaming;
|
|
1445
|
+
}
|
|
1446
|
+
getStreaming() {
|
|
1447
|
+
return this.streaming;
|
|
1448
|
+
}
|
|
1449
|
+
setTools(tools) {
|
|
1450
|
+
this.tools = tools;
|
|
1451
|
+
}
|
|
1452
|
+
getTools() {
|
|
1453
|
+
return this.tools;
|
|
1454
|
+
}
|
|
1455
|
+
setReasoningEffort(effort) {
|
|
1456
|
+
this.effort.setReasoningEffort(effort);
|
|
1457
|
+
}
|
|
1458
|
+
getReasoningEffort() {
|
|
1459
|
+
return this.effort.getReasoningEffort();
|
|
1460
|
+
}
|
|
1461
|
+
};
|
|
1462
|
+
|
|
1463
|
+
// src/infrastructure/providers/gemini/runtime/gemini-generate-content.ts
|
|
1464
|
+
import { GoogleGenAI } from "@google/genai";
|
|
1465
|
+
var GeminiGenerateContent = class {
|
|
1466
|
+
constructor() {
|
|
1467
|
+
this.client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
|
|
1468
|
+
}
|
|
1469
|
+
async infer(inferenceRequest) {
|
|
1470
|
+
const response = await this.client.models.generateContent(this.buildRequest(inferenceRequest));
|
|
1471
|
+
const contextItems = this.extractContextItems(response);
|
|
1472
|
+
const tokenUsage = this.extractTokenUsage(response);
|
|
1473
|
+
return new InferenceResponse(contextItems, tokenUsage);
|
|
1474
|
+
}
|
|
1475
|
+
async *stream(inferenceRequest, signal) {
|
|
1476
|
+
const stream = await this.client.models.generateContentStream(this.buildRequest(inferenceRequest));
|
|
1477
|
+
for await (const chunk of stream) {
|
|
1478
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
1479
|
+
break;
|
|
1480
|
+
}
|
|
1481
|
+
yield new SemanticEvent("generate_content_chunk", chunk);
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
buildRequest(inferenceRequest) {
|
|
1485
|
+
const specification = inferenceRequest.model.specification;
|
|
1486
|
+
const { contents, systemInstruction } = this.mapContextToRequest(inferenceRequest.context);
|
|
1487
|
+
const config = {};
|
|
1488
|
+
if (systemInstruction) {
|
|
1489
|
+
config.systemInstruction = systemInstruction;
|
|
1490
|
+
}
|
|
1491
|
+
if (specification.supportFunctionCalling && inferenceRequest.model.getTools().length > 0) {
|
|
1492
|
+
config.tools = [
|
|
1493
|
+
{
|
|
1494
|
+
functionDeclarations: inferenceRequest.model.getTools().map((tool) => {
|
|
1495
|
+
return {
|
|
1496
|
+
name: tool.name,
|
|
1497
|
+
description: tool.description,
|
|
1498
|
+
parametersJsonSchema: tool.parameters
|
|
1499
|
+
};
|
|
1500
|
+
})
|
|
1501
|
+
}
|
|
1502
|
+
];
|
|
1503
|
+
}
|
|
1504
|
+
if (specification.supportReasoningEffort) {
|
|
1505
|
+
const effort = inferenceRequest.model.getReasoningEffort();
|
|
1506
|
+
const thinkingLevel = effort === "none" ? "minimal" : effort;
|
|
1507
|
+
config.thinkingConfig = { thinkingLevel, includeThoughts: true };
|
|
1508
|
+
}
|
|
1509
|
+
return { model: specification.name, contents, config };
|
|
1510
|
+
}
|
|
1511
|
+
extractTokenUsage(response) {
|
|
1512
|
+
var _a, _b, _c, _d, _e;
|
|
1513
|
+
const usage = response.usageMetadata;
|
|
1514
|
+
if (!usage) {
|
|
1515
|
+
return void 0;
|
|
1516
|
+
}
|
|
1517
|
+
return new TokenUsage(
|
|
1518
|
+
(_a = usage.promptTokenCount) != null ? _a : 0,
|
|
1519
|
+
(_b = usage.candidatesTokenCount) != null ? _b : 0,
|
|
1520
|
+
(_c = usage.totalTokenCount) != null ? _c : 0,
|
|
1521
|
+
new InputTokenDetails((_d = usage.cachedContentTokenCount) != null ? _d : 0),
|
|
1522
|
+
new OutputTokenDetails((_e = usage.thoughtsTokenCount) != null ? _e : 0)
|
|
1523
|
+
);
|
|
1524
|
+
}
|
|
1525
|
+
mapContextToRequest(context) {
|
|
1526
|
+
var _a;
|
|
1527
|
+
const contents = [];
|
|
1528
|
+
const system = [];
|
|
1529
|
+
const callNames = /* @__PURE__ */ new Map();
|
|
1530
|
+
for (const item of context.getItems()) {
|
|
1531
|
+
if (item instanceof DeveloperMessageItem || item instanceof SystemMessageItem) {
|
|
1532
|
+
system.push(item.content.text);
|
|
1533
|
+
continue;
|
|
1534
|
+
}
|
|
1535
|
+
if (item instanceof UserMessageItem) {
|
|
1536
|
+
this.addPart(contents, "user", { text: item.content.text });
|
|
1537
|
+
continue;
|
|
1538
|
+
}
|
|
1539
|
+
if (item instanceof ModelMessageItem) {
|
|
1540
|
+
this.addPart(contents, "model", { text: item.content.text });
|
|
1541
|
+
continue;
|
|
1542
|
+
}
|
|
1543
|
+
if (item instanceof FunctionCallItem) {
|
|
1544
|
+
callNames.set(item.callId, item.name);
|
|
1545
|
+
let args;
|
|
1546
|
+
try {
|
|
1547
|
+
args = JSON.parse(item.args);
|
|
1548
|
+
} catch (e) {
|
|
1549
|
+
args = {};
|
|
1550
|
+
}
|
|
1551
|
+
this.addPart(contents, "model", { functionCall: { id: item.callId, name: item.name, args } });
|
|
1552
|
+
continue;
|
|
1553
|
+
}
|
|
1554
|
+
if (item instanceof FunctionCallOutputItem) {
|
|
1555
|
+
this.addPart(contents, "user", {
|
|
1556
|
+
functionResponse: {
|
|
1557
|
+
id: item.callId,
|
|
1558
|
+
name: (_a = callNames.get(item.callId)) != null ? _a : "",
|
|
1559
|
+
response: this.parseResponse(item.output.text)
|
|
1560
|
+
}
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
return {
|
|
1565
|
+
contents,
|
|
1566
|
+
systemInstruction: system.length > 0 ? system.join("\n\n") : void 0
|
|
1567
|
+
};
|
|
1568
|
+
}
|
|
1569
|
+
extractContextItems(response) {
|
|
1570
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1571
|
+
const items = [];
|
|
1572
|
+
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 : [];
|
|
1573
|
+
for (const part of parts) {
|
|
1574
|
+
if (part.thought && part.text) {
|
|
1575
|
+
items.push(
|
|
1576
|
+
ReasoningItem.rehydrate({
|
|
1577
|
+
content: InputText.rehydrate({ text: part.text }),
|
|
1578
|
+
encryptedContent: void 0,
|
|
1579
|
+
summary: []
|
|
1580
|
+
})
|
|
1581
|
+
);
|
|
1582
|
+
continue;
|
|
1583
|
+
}
|
|
1584
|
+
if (part.text) {
|
|
1585
|
+
items.push(ModelMessageItem.rehydrate({ text: part.text }));
|
|
1586
|
+
continue;
|
|
1587
|
+
}
|
|
1588
|
+
if (part.functionCall) {
|
|
1589
|
+
items.push(
|
|
1590
|
+
FunctionCallItem.rehydrate({
|
|
1591
|
+
callId: (_e = part.functionCall.id) != null ? _e : "",
|
|
1592
|
+
name: part.functionCall.name,
|
|
1593
|
+
args: JSON.stringify((_f = part.functionCall.args) != null ? _f : {})
|
|
1594
|
+
})
|
|
1595
|
+
);
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
return items;
|
|
1599
|
+
}
|
|
1600
|
+
parseResponse(output) {
|
|
1601
|
+
try {
|
|
1602
|
+
const parsed = JSON.parse(output);
|
|
1603
|
+
return parsed && typeof parsed === "object" ? parsed : { output };
|
|
1604
|
+
} catch (e) {
|
|
1605
|
+
return { output };
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
addPart(contents, role, part) {
|
|
1609
|
+
const last = contents[contents.length - 1];
|
|
1610
|
+
if ((last == null ? void 0 : last.role) === role) {
|
|
1611
|
+
last.parts.push(part);
|
|
1612
|
+
return;
|
|
1613
|
+
}
|
|
1614
|
+
contents.push({ role, parts: [part] });
|
|
1615
|
+
}
|
|
1616
|
+
};
|
|
1617
|
+
|
|
1618
|
+
// src/infrastructure/providers/gemini/reasoning-effort.ts
|
|
1619
|
+
var GeminiReasoningEffort = class {
|
|
1620
|
+
constructor(reasoningEffort) {
|
|
1621
|
+
this.reasoningEffort = reasoningEffort;
|
|
1622
|
+
}
|
|
1623
|
+
setReasoningEffort(effort) {
|
|
1624
|
+
this.reasoningEffort = effort;
|
|
1625
|
+
}
|
|
1626
|
+
getReasoningEffort() {
|
|
1627
|
+
if (!this.reasoningEffort) {
|
|
1628
|
+
throw new Error("Reasoning effort not supported");
|
|
1629
|
+
}
|
|
1630
|
+
return this.reasoningEffort;
|
|
1631
|
+
}
|
|
1632
|
+
};
|
|
1633
|
+
|
|
1634
|
+
// src/infrastructure/providers/gemini/models/gemini-3-5-flash.ts
|
|
1635
|
+
var Gemini35Flash = class {
|
|
1636
|
+
constructor() {
|
|
1637
|
+
this.specification = {
|
|
1638
|
+
name: "gemini-3.5-flash",
|
|
1639
|
+
supportReasoningEffort: true,
|
|
1640
|
+
defaultReasoningEffort: "medium",
|
|
1641
|
+
supportStreaming: true,
|
|
1642
|
+
contextWindowSize: 1048576,
|
|
1643
|
+
maxOutputTokens: 64e3,
|
|
1644
|
+
supportFunctionCalling: true
|
|
1645
|
+
};
|
|
1646
|
+
this.tools = [];
|
|
1647
|
+
this.streaming = false;
|
|
1648
|
+
this.effort = new GeminiReasoningEffort(
|
|
1649
|
+
this.specification.defaultReasoningEffort
|
|
1650
|
+
);
|
|
1651
|
+
}
|
|
1652
|
+
setStreaming(streaming) {
|
|
1653
|
+
this.streaming = streaming;
|
|
1654
|
+
}
|
|
1655
|
+
getStreaming() {
|
|
1656
|
+
return this.streaming;
|
|
1657
|
+
}
|
|
1658
|
+
setTools(tools) {
|
|
1659
|
+
this.tools = tools;
|
|
1660
|
+
}
|
|
1661
|
+
getTools() {
|
|
1662
|
+
return this.tools;
|
|
1663
|
+
}
|
|
1664
|
+
setReasoningEffort(effort) {
|
|
1665
|
+
this.effort.setReasoningEffort(effort);
|
|
1666
|
+
}
|
|
1667
|
+
getReasoningEffort() {
|
|
1668
|
+
return this.effort.getReasoningEffort();
|
|
1669
|
+
}
|
|
1670
|
+
};
|
|
1671
|
+
|
|
1672
|
+
// src/infrastructure/providers/gemini/models/gemini-3-1-pro.ts
|
|
1673
|
+
var Gemini31Pro = class {
|
|
1674
|
+
constructor() {
|
|
1675
|
+
this.specification = {
|
|
1676
|
+
name: "gemini-3.1-pro-preview",
|
|
1677
|
+
supportReasoningEffort: true,
|
|
1678
|
+
defaultReasoningEffort: "high",
|
|
1679
|
+
supportStreaming: true,
|
|
1680
|
+
contextWindowSize: 1048576,
|
|
1681
|
+
maxOutputTokens: 64e3,
|
|
1682
|
+
supportFunctionCalling: true
|
|
1683
|
+
};
|
|
1684
|
+
this.tools = [];
|
|
1685
|
+
this.streaming = false;
|
|
1686
|
+
this.effort = new GeminiReasoningEffort(
|
|
1687
|
+
this.specification.defaultReasoningEffort
|
|
1688
|
+
);
|
|
1689
|
+
}
|
|
1690
|
+
setStreaming(streaming) {
|
|
1691
|
+
this.streaming = streaming;
|
|
1692
|
+
}
|
|
1693
|
+
getStreaming() {
|
|
1694
|
+
return this.streaming;
|
|
1695
|
+
}
|
|
1696
|
+
setTools(tools) {
|
|
1697
|
+
this.tools = tools;
|
|
1698
|
+
}
|
|
1699
|
+
getTools() {
|
|
1700
|
+
return this.tools;
|
|
1701
|
+
}
|
|
1702
|
+
setReasoningEffort(effort) {
|
|
1703
|
+
this.effort.setReasoningEffort(effort);
|
|
1704
|
+
}
|
|
1705
|
+
getReasoningEffort() {
|
|
1706
|
+
return this.effort.getReasoningEffort();
|
|
1707
|
+
}
|
|
1708
|
+
};
|
|
860
1709
|
export {
|
|
861
1710
|
Agent,
|
|
862
1711
|
AgenticEnvironment,
|
|
1712
|
+
AnthropicMessages,
|
|
863
1713
|
BaseAgent,
|
|
864
1714
|
BaseHuman,
|
|
865
1715
|
BaseObserver,
|
|
1716
|
+
ClaudeHaiku45,
|
|
1717
|
+
ClaudeOpus47,
|
|
1718
|
+
ClaudeOpus48,
|
|
1719
|
+
ClaudeSonnet46,
|
|
866
1720
|
ContextItem,
|
|
1721
|
+
DeepSeekV4Flash,
|
|
1722
|
+
DeepSeekV4Pro,
|
|
867
1723
|
DefaultFunctionCallRunner,
|
|
1724
|
+
DefaultInferenceRunner,
|
|
868
1725
|
DeveloperMessageItem,
|
|
869
1726
|
FunctionCallItem,
|
|
870
1727
|
FunctionCallOutputItem,
|
|
1728
|
+
Gemini31Pro,
|
|
1729
|
+
Gemini35Flash,
|
|
1730
|
+
GeminiGenerateContent,
|
|
871
1731
|
Gpt54,
|
|
872
1732
|
Gpt54Mini,
|
|
873
1733
|
Gpt54Nano,
|
|
874
1734
|
Gpt55,
|
|
875
1735
|
Human,
|
|
876
1736
|
InMemoryModelContextRepository,
|
|
1737
|
+
InferenceRequest,
|
|
1738
|
+
InferenceResponse,
|
|
877
1739
|
InputTokenDetails,
|
|
878
1740
|
ModelContext,
|
|
879
1741
|
ModelMessageItem,
|
|
880
1742
|
Observer,
|
|
881
|
-
|
|
1743
|
+
OpenAICompatibleChatCompletions,
|
|
1744
|
+
OpenAIResponses,
|
|
882
1745
|
OutputTokenDetails,
|
|
883
1746
|
Participant,
|
|
884
1747
|
ReasoningItem,
|