@flare-ai-sdk/server 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +55 -41
- package/dist/index.d.ts +55 -41
- package/dist/index.js +390 -511
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +390 -511
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/dist/index.mjs
CHANGED
|
@@ -5,9 +5,6 @@ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
6
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
7
|
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
|
|
8
|
-
var __typeError = (msg) => {
|
|
9
|
-
throw TypeError(msg);
|
|
10
|
-
};
|
|
11
8
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
9
|
var __spreadValues = (a, b) => {
|
|
13
10
|
for (var prop in b || (b = {}))
|
|
@@ -57,34 +54,6 @@ var __asyncGenerator = (__this, __arguments, generator) => {
|
|
|
57
54
|
}, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
|
|
58
55
|
return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
|
|
59
56
|
};
|
|
60
|
-
var __yieldStar = (value) => {
|
|
61
|
-
var obj = value[__knownSymbol("asyncIterator")], isAwait = false, method, it = {};
|
|
62
|
-
if (obj == null) {
|
|
63
|
-
obj = value[__knownSymbol("iterator")]();
|
|
64
|
-
method = (k) => it[k] = (x) => obj[k](x);
|
|
65
|
-
} else {
|
|
66
|
-
obj = obj.call(value);
|
|
67
|
-
method = (k) => it[k] = (v) => {
|
|
68
|
-
if (isAwait) {
|
|
69
|
-
isAwait = false;
|
|
70
|
-
if (k === "throw") throw v;
|
|
71
|
-
return v;
|
|
72
|
-
}
|
|
73
|
-
isAwait = true;
|
|
74
|
-
return {
|
|
75
|
-
done: false,
|
|
76
|
-
value: new __await(new Promise((resolve) => {
|
|
77
|
-
var x = obj[k](v);
|
|
78
|
-
if (!(x instanceof Object)) __typeError("Object expected");
|
|
79
|
-
resolve(x);
|
|
80
|
-
}), 1)
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
return it[__knownSymbol("iterator")] = () => it, method("next"), "throw" in obj ? method("throw") : it.throw = (x) => {
|
|
85
|
-
throw x;
|
|
86
|
-
}, "return" in obj && method("return"), it;
|
|
87
|
-
};
|
|
88
57
|
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
|
|
89
58
|
|
|
90
59
|
// src/utils/errors.ts
|
|
@@ -835,24 +804,10 @@ var BaseProvider = class {
|
|
|
835
804
|
__publicField(this, "config");
|
|
836
805
|
this.config = config;
|
|
837
806
|
}
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
}, options.headers || {});
|
|
843
|
-
if (this.config.apiKey) {
|
|
844
|
-
headers["Authorization"] = `Bearer ${this.config.apiKey}`;
|
|
845
|
-
}
|
|
846
|
-
const response = yield fetch(url, __spreadProps(__spreadValues({}, options), {
|
|
847
|
-
headers
|
|
848
|
-
}));
|
|
849
|
-
if (!response.ok && !stream) {
|
|
850
|
-
const errorText = yield response.text();
|
|
851
|
-
throw new Error(`Provider request failed: ${response.status} ${errorText}`);
|
|
852
|
-
}
|
|
853
|
-
return response;
|
|
854
|
-
});
|
|
855
|
-
}
|
|
807
|
+
/**
|
|
808
|
+
* Format messages for API request
|
|
809
|
+
* Handles system prompts and conversation history
|
|
810
|
+
*/
|
|
856
811
|
formatMessages(request) {
|
|
857
812
|
const messages = [];
|
|
858
813
|
if (request.systemPrompt) {
|
|
@@ -861,16 +816,29 @@ var BaseProvider = class {
|
|
|
861
816
|
messages.push(...request.messages);
|
|
862
817
|
return messages;
|
|
863
818
|
}
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
819
|
+
/**
|
|
820
|
+
* Validate configuration
|
|
821
|
+
*/
|
|
822
|
+
validateConfig() {
|
|
823
|
+
if (!this.config.apiKey) {
|
|
824
|
+
throw new Error(`API key is required for ${this.config.provider} provider`);
|
|
825
|
+
}
|
|
826
|
+
if (!this.config.model) {
|
|
827
|
+
throw new Error(`Model is required for ${this.config.provider} provider`);
|
|
828
|
+
}
|
|
867
829
|
}
|
|
868
830
|
};
|
|
869
831
|
|
|
870
832
|
// src/providers/anthropic/anthropic.ts
|
|
833
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
871
834
|
var AnthropicProvider = class extends BaseProvider {
|
|
872
|
-
|
|
873
|
-
|
|
835
|
+
constructor(config) {
|
|
836
|
+
super(config);
|
|
837
|
+
__publicField(this, "client");
|
|
838
|
+
this.client = new Anthropic({
|
|
839
|
+
apiKey: this.config.apiKey,
|
|
840
|
+
baseURL: this.config.baseUrl
|
|
841
|
+
});
|
|
874
842
|
}
|
|
875
843
|
generateText(request) {
|
|
876
844
|
return __async(this, null, function* () {
|
|
@@ -878,31 +846,22 @@ var AnthropicProvider = class extends BaseProvider {
|
|
|
878
846
|
const messages = this.formatMessages(request);
|
|
879
847
|
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
880
848
|
const userMessages = messages.filter((m) => m.role !== "system");
|
|
881
|
-
const response = yield this.
|
|
882
|
-
this.
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
model: this.config.model,
|
|
890
|
-
messages: userMessages,
|
|
891
|
-
system: systemPrompt,
|
|
892
|
-
max_tokens: this.config.maxTokens || 1024,
|
|
893
|
-
temperature: this.config.temperature
|
|
894
|
-
})
|
|
895
|
-
}
|
|
896
|
-
);
|
|
897
|
-
const data = yield response.json();
|
|
849
|
+
const response = yield this.client.messages.create({
|
|
850
|
+
model: this.config.model,
|
|
851
|
+
messages: userMessages,
|
|
852
|
+
system: systemPrompt,
|
|
853
|
+
max_tokens: this.config.maxTokens || 1024,
|
|
854
|
+
temperature: this.config.temperature
|
|
855
|
+
});
|
|
856
|
+
const textContent = response.content.find((c) => c.type === "text");
|
|
898
857
|
return {
|
|
899
|
-
text:
|
|
858
|
+
text: (textContent == null ? void 0 : textContent.type) === "text" ? textContent.text : "",
|
|
900
859
|
usage: {
|
|
901
|
-
promptTokens:
|
|
902
|
-
completionTokens:
|
|
903
|
-
totalTokens:
|
|
860
|
+
promptTokens: response.usage.input_tokens,
|
|
861
|
+
completionTokens: response.usage.output_tokens,
|
|
862
|
+
totalTokens: response.usage.input_tokens + response.usage.output_tokens
|
|
904
863
|
},
|
|
905
|
-
finishReason:
|
|
864
|
+
finishReason: response.stop_reason
|
|
906
865
|
};
|
|
907
866
|
});
|
|
908
867
|
}
|
|
@@ -912,47 +871,43 @@ var AnthropicProvider = class extends BaseProvider {
|
|
|
912
871
|
const messages = this.formatMessages(request);
|
|
913
872
|
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
914
873
|
const userMessages = messages.filter((m) => m.role !== "system");
|
|
915
|
-
const
|
|
916
|
-
this.
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
body: JSON.stringify({
|
|
923
|
-
model: this.config.model,
|
|
924
|
-
messages: userMessages,
|
|
925
|
-
system: systemPrompt,
|
|
926
|
-
max_tokens: this.config.maxTokens || 1024,
|
|
927
|
-
stream: true
|
|
928
|
-
})
|
|
929
|
-
},
|
|
930
|
-
true
|
|
931
|
-
));
|
|
932
|
-
const reader = response.body.getReader();
|
|
933
|
-
const decoder = new TextDecoder();
|
|
934
|
-
let buffer = "";
|
|
874
|
+
const stream = yield new __await(this.client.messages.stream({
|
|
875
|
+
model: this.config.model,
|
|
876
|
+
messages: userMessages,
|
|
877
|
+
system: systemPrompt,
|
|
878
|
+
max_tokens: this.config.maxTokens || 1024,
|
|
879
|
+
temperature: this.config.temperature
|
|
880
|
+
}));
|
|
935
881
|
try {
|
|
936
|
-
|
|
937
|
-
const
|
|
938
|
-
if (
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
882
|
+
for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
883
|
+
const event = temp.value;
|
|
884
|
+
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
885
|
+
yield { type: "content", content: event.delta.text };
|
|
886
|
+
}
|
|
887
|
+
if (event.type === "message_stop") {
|
|
888
|
+
const message = yield new __await(stream.finalMessage());
|
|
889
|
+
yield {
|
|
890
|
+
type: "done",
|
|
891
|
+
done: {
|
|
892
|
+
finishReason: message.stop_reason,
|
|
893
|
+
usage: {
|
|
894
|
+
promptTokens: message.usage.input_tokens,
|
|
895
|
+
completionTokens: message.usage.output_tokens,
|
|
896
|
+
totalTokens: message.usage.input_tokens + message.usage.output_tokens
|
|
948
897
|
}
|
|
949
|
-
} catch (e) {
|
|
950
898
|
}
|
|
951
|
-
}
|
|
899
|
+
};
|
|
952
900
|
}
|
|
953
901
|
}
|
|
902
|
+
} catch (temp) {
|
|
903
|
+
error = [temp];
|
|
954
904
|
} finally {
|
|
955
|
-
|
|
905
|
+
try {
|
|
906
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
907
|
+
} finally {
|
|
908
|
+
if (error)
|
|
909
|
+
throw error[0];
|
|
910
|
+
}
|
|
956
911
|
}
|
|
957
912
|
});
|
|
958
913
|
}
|
|
@@ -1010,35 +965,128 @@ var AnthropicProvider = class extends BaseProvider {
|
|
|
1010
965
|
}
|
|
1011
966
|
};
|
|
1012
967
|
|
|
1013
|
-
// src/providers/custom
|
|
1014
|
-
var
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
968
|
+
// src/providers/custom-base.ts
|
|
969
|
+
var CustomBaseProvider = class extends BaseProvider {
|
|
970
|
+
/**
|
|
971
|
+
* Make HTTP request with proper error handling
|
|
972
|
+
*/
|
|
973
|
+
makeRequest(url, options, stream = false) {
|
|
974
|
+
return __async(this, null, function* () {
|
|
975
|
+
const headers = __spreadValues({
|
|
976
|
+
"Content-Type": "application/json"
|
|
977
|
+
}, options.headers || {});
|
|
978
|
+
if (this.config.apiKey) {
|
|
979
|
+
headers["Authorization"] = `Bearer ${this.config.apiKey}`;
|
|
980
|
+
}
|
|
981
|
+
const response = yield fetch(url, __spreadProps(__spreadValues({}, options), {
|
|
982
|
+
headers
|
|
983
|
+
}));
|
|
984
|
+
if (!response.ok && !stream) {
|
|
985
|
+
const errorText = yield response.text();
|
|
986
|
+
throw new Error(`Provider request failed: ${response.status} ${errorText}`);
|
|
987
|
+
}
|
|
988
|
+
return response;
|
|
989
|
+
});
|
|
1021
990
|
}
|
|
1022
|
-
|
|
1023
|
-
|
|
991
|
+
/**
|
|
992
|
+
* Get API endpoint URL
|
|
993
|
+
*/
|
|
994
|
+
getEndpoint(path) {
|
|
995
|
+
const baseUrl = this.config.baseUrl || this.getDefaultBaseUrl();
|
|
996
|
+
return `${baseUrl}${path}`;
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* Parse streaming SSE response
|
|
1000
|
+
*/
|
|
1001
|
+
parseSSEStream(response) {
|
|
1002
|
+
return __asyncGenerator(this, null, function* () {
|
|
1003
|
+
const reader = response.body.getReader();
|
|
1004
|
+
const decoder = new TextDecoder();
|
|
1005
|
+
let buffer = "";
|
|
1006
|
+
try {
|
|
1007
|
+
while (true) {
|
|
1008
|
+
const { done, value } = yield new __await(reader.read());
|
|
1009
|
+
if (done) break;
|
|
1010
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1011
|
+
const lines = buffer.split("\n");
|
|
1012
|
+
buffer = lines.pop() || "";
|
|
1013
|
+
for (const line of lines) {
|
|
1014
|
+
if (line.startsWith("data: ")) {
|
|
1015
|
+
const data = line.slice(6).trim();
|
|
1016
|
+
if (data === "[DONE]") continue;
|
|
1017
|
+
try {
|
|
1018
|
+
yield JSON.parse(data);
|
|
1019
|
+
} catch (e) {
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
} finally {
|
|
1025
|
+
reader.releaseLock();
|
|
1026
|
+
}
|
|
1027
|
+
});
|
|
1024
1028
|
}
|
|
1025
1029
|
/**
|
|
1026
|
-
*
|
|
1030
|
+
* Build request body with common parameters
|
|
1027
1031
|
*/
|
|
1028
|
-
|
|
1029
|
-
|
|
1032
|
+
buildRequestBody(request, stream = false) {
|
|
1033
|
+
return {
|
|
1034
|
+
model: this.config.model,
|
|
1035
|
+
messages: this.formatMessages(request),
|
|
1036
|
+
max_tokens: this.config.maxTokens,
|
|
1037
|
+
temperature: this.config.temperature,
|
|
1038
|
+
top_p: this.config.topP,
|
|
1039
|
+
frequency_penalty: this.config.frequencyPenalty,
|
|
1040
|
+
presence_penalty: this.config.presencePenalty,
|
|
1041
|
+
stop: this.config.stop,
|
|
1042
|
+
stream
|
|
1043
|
+
};
|
|
1044
|
+
}
|
|
1045
|
+
};
|
|
1046
|
+
|
|
1047
|
+
// src/providers/custom/custom.ts
|
|
1048
|
+
var CustomProvider = class extends CustomBaseProvider {
|
|
1049
|
+
getDefaultBaseUrl() {
|
|
1050
|
+
if (!this.config.baseUrl) {
|
|
1051
|
+
throw new ProviderError(
|
|
1052
|
+
"custom",
|
|
1053
|
+
"baseUrl is required for custom provider",
|
|
1054
|
+
void 0,
|
|
1055
|
+
{ config: this.config }
|
|
1056
|
+
);
|
|
1057
|
+
}
|
|
1058
|
+
return this.config.baseUrl;
|
|
1030
1059
|
}
|
|
1031
1060
|
generateText(request) {
|
|
1032
1061
|
return __async(this, null, function* () {
|
|
1033
|
-
|
|
1062
|
+
var _a, _b, _c;
|
|
1063
|
+
this.validateConfig();
|
|
1034
1064
|
try {
|
|
1035
|
-
const
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1065
|
+
const response = yield this.makeRequest(
|
|
1066
|
+
this.getEndpoint("/chat/completions"),
|
|
1067
|
+
{
|
|
1068
|
+
method: "POST",
|
|
1069
|
+
body: JSON.stringify(this.buildRequestBody(request))
|
|
1070
|
+
}
|
|
1071
|
+
);
|
|
1040
1072
|
const data = yield response.json();
|
|
1041
|
-
|
|
1073
|
+
if (!data.choices || data.choices.length === 0) {
|
|
1074
|
+
throw new ProviderError(
|
|
1075
|
+
"custom",
|
|
1076
|
+
"No choices returned from custom API",
|
|
1077
|
+
response.status,
|
|
1078
|
+
{ response: data }
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1081
|
+
return {
|
|
1082
|
+
text: data.choices[0].message.content,
|
|
1083
|
+
usage: {
|
|
1084
|
+
promptTokens: ((_a = data.usage) == null ? void 0 : _a.prompt_tokens) || 0,
|
|
1085
|
+
completionTokens: ((_b = data.usage) == null ? void 0 : _b.completion_tokens) || 0,
|
|
1086
|
+
totalTokens: ((_c = data.usage) == null ? void 0 : _c.total_tokens) || 0
|
|
1087
|
+
},
|
|
1088
|
+
finishReason: data.choices[0].finish_reason || "stop"
|
|
1089
|
+
};
|
|
1042
1090
|
} catch (error) {
|
|
1043
1091
|
throw new ProviderError(
|
|
1044
1092
|
"custom",
|
|
@@ -1051,26 +1099,56 @@ var CustomProvider = class extends BaseProvider {
|
|
|
1051
1099
|
}
|
|
1052
1100
|
streamText(request) {
|
|
1053
1101
|
return __asyncGenerator(this, null, function* () {
|
|
1054
|
-
|
|
1102
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1103
|
+
this.validateConfig();
|
|
1055
1104
|
try {
|
|
1056
|
-
const requestBody = this.formatRequestBody(messages, true);
|
|
1057
1105
|
const response = yield new __await(this.makeRequest(
|
|
1058
1106
|
this.getEndpoint("/chat/completions"),
|
|
1059
1107
|
{
|
|
1060
1108
|
method: "POST",
|
|
1061
|
-
body: JSON.stringify(
|
|
1109
|
+
body: JSON.stringify(this.buildRequestBody(request, true))
|
|
1062
1110
|
},
|
|
1063
1111
|
true
|
|
1064
1112
|
));
|
|
1065
|
-
|
|
1066
|
-
|
|
1113
|
+
try {
|
|
1114
|
+
for (var iter = __forAwait(this.parseSSEStream(response)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
1115
|
+
const data = temp.value;
|
|
1116
|
+
const content = (_c = (_b = (_a = data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.content;
|
|
1117
|
+
if (content) {
|
|
1118
|
+
yield { type: "content", content };
|
|
1119
|
+
}
|
|
1120
|
+
if ((_e = (_d = data.choices) == null ? void 0 : _d[0]) == null ? void 0 : _e.finish_reason) {
|
|
1121
|
+
yield {
|
|
1122
|
+
type: "done",
|
|
1123
|
+
done: {
|
|
1124
|
+
finishReason: data.choices[0].finish_reason || "stop",
|
|
1125
|
+
usage: {
|
|
1126
|
+
promptTokens: ((_f = data.usage) == null ? void 0 : _f.prompt_tokens) || 0,
|
|
1127
|
+
completionTokens: ((_g = data.usage) == null ? void 0 : _g.completion_tokens) || 0,
|
|
1128
|
+
totalTokens: ((_h = data.usage) == null ? void 0 : _h.total_tokens) || 0
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
} catch (temp) {
|
|
1135
|
+
error = [temp];
|
|
1136
|
+
} finally {
|
|
1137
|
+
try {
|
|
1138
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
1139
|
+
} finally {
|
|
1140
|
+
if (error)
|
|
1141
|
+
throw error[0];
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
} catch (error2) {
|
|
1067
1145
|
yield {
|
|
1068
1146
|
type: "error",
|
|
1069
1147
|
error: new ProviderError(
|
|
1070
1148
|
"custom",
|
|
1071
|
-
|
|
1149
|
+
error2 instanceof Error ? error2.message : "Streaming error",
|
|
1072
1150
|
void 0,
|
|
1073
|
-
{ originalError:
|
|
1151
|
+
{ originalError: error2 }
|
|
1074
1152
|
)
|
|
1075
1153
|
};
|
|
1076
1154
|
}
|
|
@@ -1083,18 +1161,13 @@ var CustomProvider = class extends BaseProvider {
|
|
|
1083
1161
|
...request.messages,
|
|
1084
1162
|
{
|
|
1085
1163
|
role: "user",
|
|
1086
|
-
content: `Respond with valid JSON matching this schema: ${JSON.stringify(request.schema)}
|
|
1164
|
+
content: `Respond with valid JSON matching this schema: ${JSON.stringify(request.schema)}`
|
|
1087
1165
|
}
|
|
1088
1166
|
]
|
|
1089
1167
|
});
|
|
1090
1168
|
const textResponse = yield this.generateText(enhancedRequest);
|
|
1091
|
-
let jsonText = textResponse.text.trim();
|
|
1092
|
-
const codeBlockMatch = jsonText.match(/```(?:json)?\s*([\s\S]*?)\s*```/);
|
|
1093
|
-
if (codeBlockMatch) {
|
|
1094
|
-
jsonText = codeBlockMatch[1];
|
|
1095
|
-
}
|
|
1096
1169
|
try {
|
|
1097
|
-
const object = JSON.parse(
|
|
1170
|
+
const object = JSON.parse(textResponse.text);
|
|
1098
1171
|
return {
|
|
1099
1172
|
object,
|
|
1100
1173
|
text: textResponse.text,
|
|
@@ -1118,13 +1191,8 @@ var CustomProvider = class extends BaseProvider {
|
|
|
1118
1191
|
const chunk = temp.value;
|
|
1119
1192
|
if (chunk.type === "content") {
|
|
1120
1193
|
accumulatedText += chunk.content;
|
|
1121
|
-
let jsonText2 = accumulatedText.trim();
|
|
1122
|
-
const codeBlockMatch2 = jsonText2.match(/```(?:json)?\s*([\s\S]*?)\s*```/);
|
|
1123
|
-
if (codeBlockMatch2) {
|
|
1124
|
-
jsonText2 = codeBlockMatch2[1];
|
|
1125
|
-
}
|
|
1126
1194
|
try {
|
|
1127
|
-
const partial = JSON.parse(
|
|
1195
|
+
const partial = JSON.parse(accumulatedText);
|
|
1128
1196
|
yield { type: "partial", partial, text: accumulatedText };
|
|
1129
1197
|
} catch (e) {
|
|
1130
1198
|
}
|
|
@@ -1140,203 +1208,70 @@ var CustomProvider = class extends BaseProvider {
|
|
|
1140
1208
|
throw error[0];
|
|
1141
1209
|
}
|
|
1142
1210
|
}
|
|
1143
|
-
let jsonText = accumulatedText.trim();
|
|
1144
|
-
const codeBlockMatch = jsonText.match(/```(?:json)?\s*([\s\S]*?)\s*```/);
|
|
1145
|
-
if (codeBlockMatch) {
|
|
1146
|
-
jsonText = codeBlockMatch[1];
|
|
1147
|
-
}
|
|
1148
1211
|
try {
|
|
1149
|
-
const object = JSON.parse(
|
|
1212
|
+
const object = JSON.parse(accumulatedText);
|
|
1150
1213
|
yield { type: "complete", object, text: accumulatedText };
|
|
1151
1214
|
} catch (e) {
|
|
1152
1215
|
yield { type: "error", error: new Error("Invalid JSON in response") };
|
|
1153
1216
|
}
|
|
1154
1217
|
});
|
|
1155
1218
|
}
|
|
1156
|
-
/**
|
|
1157
|
-
* Format request body based on provider format
|
|
1158
|
-
*/
|
|
1159
|
-
formatRequestBody(messages, stream) {
|
|
1160
|
-
var _a;
|
|
1161
|
-
if (this.requestFormat === "anthropic") {
|
|
1162
|
-
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
1163
|
-
const userMessages = messages.filter((m) => m.role !== "system");
|
|
1164
|
-
return {
|
|
1165
|
-
model: this.config.model,
|
|
1166
|
-
messages: userMessages,
|
|
1167
|
-
system: systemPrompt,
|
|
1168
|
-
max_tokens: this.config.maxTokens || 1024,
|
|
1169
|
-
temperature: this.config.temperature,
|
|
1170
|
-
top_p: this.config.topP,
|
|
1171
|
-
stream
|
|
1172
|
-
};
|
|
1173
|
-
}
|
|
1174
|
-
return {
|
|
1175
|
-
model: this.config.model,
|
|
1176
|
-
messages,
|
|
1177
|
-
max_tokens: this.config.maxTokens,
|
|
1178
|
-
temperature: this.config.temperature,
|
|
1179
|
-
top_p: this.config.topP,
|
|
1180
|
-
frequency_penalty: this.config.frequencyPenalty,
|
|
1181
|
-
presence_penalty: this.config.presencePenalty,
|
|
1182
|
-
stop: this.config.stop,
|
|
1183
|
-
stream
|
|
1184
|
-
};
|
|
1185
|
-
}
|
|
1186
|
-
/**
|
|
1187
|
-
* Parse text response based on provider format
|
|
1188
|
-
*/
|
|
1189
|
-
parseTextResponse(data) {
|
|
1190
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1191
|
-
if (data.choices && ((_a = data.choices[0]) == null ? void 0 : _a.message)) {
|
|
1192
|
-
return {
|
|
1193
|
-
text: data.choices[0].message.content,
|
|
1194
|
-
usage: {
|
|
1195
|
-
promptTokens: ((_b = data.usage) == null ? void 0 : _b.prompt_tokens) || 0,
|
|
1196
|
-
completionTokens: ((_c = data.usage) == null ? void 0 : _c.completion_tokens) || 0,
|
|
1197
|
-
totalTokens: ((_d = data.usage) == null ? void 0 : _d.total_tokens) || 0
|
|
1198
|
-
},
|
|
1199
|
-
finishReason: data.choices[0].finish_reason || "stop"
|
|
1200
|
-
};
|
|
1201
|
-
}
|
|
1202
|
-
if (data.content && Array.isArray(data.content)) {
|
|
1203
|
-
return {
|
|
1204
|
-
text: ((_e = data.content[0]) == null ? void 0 : _e.text) || "",
|
|
1205
|
-
usage: {
|
|
1206
|
-
promptTokens: ((_f = data.usage) == null ? void 0 : _f.input_tokens) || 0,
|
|
1207
|
-
completionTokens: ((_g = data.usage) == null ? void 0 : _g.output_tokens) || 0,
|
|
1208
|
-
totalTokens: (((_h = data.usage) == null ? void 0 : _h.input_tokens) || 0) + (((_i = data.usage) == null ? void 0 : _i.output_tokens) || 0)
|
|
1209
|
-
},
|
|
1210
|
-
finishReason: data.stop_reason || "stop"
|
|
1211
|
-
};
|
|
1212
|
-
}
|
|
1213
|
-
return {
|
|
1214
|
-
text: data.text || data.response || JSON.stringify(data),
|
|
1215
|
-
usage: {
|
|
1216
|
-
promptTokens: 0,
|
|
1217
|
-
completionTokens: 0,
|
|
1218
|
-
totalTokens: 0
|
|
1219
|
-
},
|
|
1220
|
-
finishReason: "stop"
|
|
1221
|
-
};
|
|
1222
|
-
}
|
|
1223
|
-
/**
|
|
1224
|
-
* Parse streaming response
|
|
1225
|
-
*/
|
|
1226
|
-
parseStreamResponse(response) {
|
|
1227
|
-
return __asyncGenerator(this, null, function* () {
|
|
1228
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
1229
|
-
const reader = response.body.getReader();
|
|
1230
|
-
const decoder = new TextDecoder();
|
|
1231
|
-
let buffer = "";
|
|
1232
|
-
try {
|
|
1233
|
-
while (true) {
|
|
1234
|
-
const { done, value } = yield new __await(reader.read());
|
|
1235
|
-
if (done) break;
|
|
1236
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1237
|
-
const lines = buffer.split("\n");
|
|
1238
|
-
buffer = lines.pop() || "";
|
|
1239
|
-
for (const line of lines) {
|
|
1240
|
-
if (line.startsWith("data: ")) {
|
|
1241
|
-
const data = line.slice(6);
|
|
1242
|
-
if (data === "[DONE]") continue;
|
|
1243
|
-
try {
|
|
1244
|
-
const parsed = JSON.parse(data);
|
|
1245
|
-
if ((_c = (_b = (_a = parsed.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.content) {
|
|
1246
|
-
yield { type: "content", content: parsed.choices[0].delta.content };
|
|
1247
|
-
}
|
|
1248
|
-
if (parsed.type === "content_block_delta" && ((_d = parsed.delta) == null ? void 0 : _d.text)) {
|
|
1249
|
-
yield { type: "content", content: parsed.delta.text };
|
|
1250
|
-
}
|
|
1251
|
-
if (parsed.content && !parsed.choices) {
|
|
1252
|
-
yield { type: "content", content: parsed.content };
|
|
1253
|
-
}
|
|
1254
|
-
if (((_f = (_e = parsed.choices) == null ? void 0 : _e[0]) == null ? void 0 : _f.finish_reason) || parsed.stop_reason) {
|
|
1255
|
-
yield {
|
|
1256
|
-
type: "done",
|
|
1257
|
-
done: {
|
|
1258
|
-
finishReason: ((_h = (_g = parsed.choices) == null ? void 0 : _g[0]) == null ? void 0 : _h.finish_reason) || parsed.stop_reason || "stop",
|
|
1259
|
-
usage: {
|
|
1260
|
-
promptTokens: ((_i = parsed.usage) == null ? void 0 : _i.prompt_tokens) || ((_j = parsed.usage) == null ? void 0 : _j.input_tokens) || 0,
|
|
1261
|
-
completionTokens: ((_k = parsed.usage) == null ? void 0 : _k.completion_tokens) || ((_l = parsed.usage) == null ? void 0 : _l.output_tokens) || 0,
|
|
1262
|
-
totalTokens: ((_m = parsed.usage) == null ? void 0 : _m.total_tokens) || 0
|
|
1263
|
-
}
|
|
1264
|
-
}
|
|
1265
|
-
};
|
|
1266
|
-
}
|
|
1267
|
-
} catch (e) {
|
|
1268
|
-
console.error("Failed to parse streaming chunk:", e);
|
|
1269
|
-
}
|
|
1270
|
-
}
|
|
1271
|
-
}
|
|
1272
|
-
}
|
|
1273
|
-
} finally {
|
|
1274
|
-
reader.releaseLock();
|
|
1275
|
-
}
|
|
1276
|
-
});
|
|
1277
|
-
}
|
|
1278
1219
|
};
|
|
1279
1220
|
|
|
1280
1221
|
// src/providers/google/google.ts
|
|
1222
|
+
import { GoogleGenerativeAI } from "@google/generative-ai";
|
|
1281
1223
|
var GoogleProvider = class extends BaseProvider {
|
|
1282
|
-
|
|
1283
|
-
|
|
1224
|
+
constructor(config) {
|
|
1225
|
+
super(config);
|
|
1226
|
+
__publicField(this, "genAI");
|
|
1227
|
+
__publicField(this, "model");
|
|
1228
|
+
this.genAI = new GoogleGenerativeAI(this.config.apiKey);
|
|
1229
|
+
this.model = this.genAI.getGenerativeModel({
|
|
1230
|
+
model: this.config.model,
|
|
1231
|
+
generationConfig: {
|
|
1232
|
+
maxOutputTokens: this.config.maxTokens,
|
|
1233
|
+
temperature: this.config.temperature,
|
|
1234
|
+
topP: this.config.topP,
|
|
1235
|
+
stopSequences: this.config.stop
|
|
1236
|
+
}
|
|
1237
|
+
});
|
|
1284
1238
|
}
|
|
1285
1239
|
/**
|
|
1286
1240
|
* Format messages for Google's API format
|
|
1287
1241
|
*/
|
|
1288
1242
|
formatGoogleMessages(messages) {
|
|
1289
1243
|
return messages.map((msg) => ({
|
|
1290
|
-
role: msg.role === "assistant" ? "model" :
|
|
1244
|
+
role: msg.role === "assistant" ? "model" : "user",
|
|
1291
1245
|
parts: [{ text: msg.content }]
|
|
1292
1246
|
}));
|
|
1293
1247
|
}
|
|
1294
1248
|
generateText(request) {
|
|
1295
1249
|
return __async(this, null, function* () {
|
|
1296
|
-
var _a, _b, _c, _d;
|
|
1250
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1297
1251
|
const messages = this.formatMessages(request);
|
|
1298
1252
|
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
1299
1253
|
const conversationMessages = messages.filter((m) => m.role !== "system");
|
|
1300
|
-
const endpoint = this.getEndpoint(
|
|
1301
|
-
`/models/${this.config.model}:generateContent?key=${this.config.apiKey}`
|
|
1302
|
-
);
|
|
1303
1254
|
try {
|
|
1304
|
-
const
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
} : void 0,
|
|
1311
|
-
generationConfig: {
|
|
1312
|
-
maxOutputTokens: this.config.maxTokens,
|
|
1313
|
-
temperature: this.config.temperature,
|
|
1314
|
-
topP: this.config.topP,
|
|
1315
|
-
stopSequences: this.config.stop
|
|
1316
|
-
}
|
|
1317
|
-
})
|
|
1255
|
+
const chat = this.model.startChat({
|
|
1256
|
+
history: this.formatGoogleMessages(conversationMessages.slice(0, -1)),
|
|
1257
|
+
systemInstruction: systemPrompt ? {
|
|
1258
|
+
role: "system",
|
|
1259
|
+
parts: [{ text: systemPrompt }]
|
|
1260
|
+
} : void 0
|
|
1318
1261
|
});
|
|
1319
|
-
const
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
"google",
|
|
1323
|
-
"No candidates returned from Google API",
|
|
1324
|
-
response.status,
|
|
1325
|
-
{ response: data }
|
|
1326
|
-
);
|
|
1327
|
-
}
|
|
1328
|
-
const candidate = data.candidates[0];
|
|
1329
|
-
const text = candidate.content.parts.map((p) => p.text).join("");
|
|
1262
|
+
const lastMessage = conversationMessages[conversationMessages.length - 1];
|
|
1263
|
+
const result = yield chat.sendMessage(lastMessage.content);
|
|
1264
|
+
const response = result.response;
|
|
1330
1265
|
return {
|
|
1331
|
-
text,
|
|
1266
|
+
text: response.text(),
|
|
1332
1267
|
usage: {
|
|
1333
|
-
promptTokens: ((_b =
|
|
1334
|
-
completionTokens: ((_c =
|
|
1335
|
-
totalTokens: ((_d =
|
|
1268
|
+
promptTokens: ((_b = response.usageMetadata) == null ? void 0 : _b.promptTokenCount) || 0,
|
|
1269
|
+
completionTokens: ((_c = response.usageMetadata) == null ? void 0 : _c.candidatesTokenCount) || 0,
|
|
1270
|
+
totalTokens: ((_d = response.usageMetadata) == null ? void 0 : _d.totalTokenCount) || 0
|
|
1336
1271
|
},
|
|
1337
|
-
finishReason: this.mapGoogleFinishReason(
|
|
1272
|
+
finishReason: this.mapGoogleFinishReason((_f = (_e = response.candidates) == null ? void 0 : _e[0]) == null ? void 0 : _f.finishReason),
|
|
1338
1273
|
metadata: {
|
|
1339
|
-
safetyRatings:
|
|
1274
|
+
safetyRatings: (_h = (_g = response.candidates) == null ? void 0 : _g[0]) == null ? void 0 : _h.safetyRatings
|
|
1340
1275
|
}
|
|
1341
1276
|
};
|
|
1342
1277
|
} catch (error) {
|
|
@@ -1351,79 +1286,55 @@ var GoogleProvider = class extends BaseProvider {
|
|
|
1351
1286
|
}
|
|
1352
1287
|
streamText(request) {
|
|
1353
1288
|
return __asyncGenerator(this, null, function* () {
|
|
1354
|
-
var _a, _b, _c, _d, _e, _f
|
|
1289
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1355
1290
|
const messages = this.formatMessages(request);
|
|
1356
1291
|
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
1357
1292
|
const conversationMessages = messages.filter((m) => m.role !== "system");
|
|
1358
|
-
const endpoint = this.getEndpoint(
|
|
1359
|
-
`/models/${this.config.model}:streamGenerateContent?key=${this.config.apiKey}`
|
|
1360
|
-
);
|
|
1361
1293
|
try {
|
|
1362
|
-
const
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
topP: this.config.topP
|
|
1375
|
-
}
|
|
1376
|
-
})
|
|
1377
|
-
},
|
|
1378
|
-
true
|
|
1379
|
-
));
|
|
1380
|
-
const reader = response.body.getReader();
|
|
1381
|
-
const decoder = new TextDecoder();
|
|
1382
|
-
let buffer = "";
|
|
1383
|
-
while (true) {
|
|
1384
|
-
const { done, value } = yield new __await(reader.read());
|
|
1385
|
-
if (done) break;
|
|
1386
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1387
|
-
const lines = buffer.split("\n");
|
|
1388
|
-
buffer = lines.pop() || "";
|
|
1389
|
-
for (const line of lines) {
|
|
1390
|
-
if (!line.trim() || !line.startsWith("data: ")) continue;
|
|
1391
|
-
try {
|
|
1392
|
-
const jsonStr = line.slice(6).trim();
|
|
1393
|
-
if (!jsonStr) continue;
|
|
1394
|
-
const data = JSON.parse(jsonStr);
|
|
1395
|
-
if (data.candidates && ((_c = (_b = data.candidates[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts)) {
|
|
1396
|
-
const text = data.candidates[0].content.parts.map((p) => p.text).join("");
|
|
1397
|
-
if (text) {
|
|
1398
|
-
yield { type: "content", content: text };
|
|
1399
|
-
}
|
|
1400
|
-
}
|
|
1401
|
-
if (data.candidates && ((_d = data.candidates[0]) == null ? void 0 : _d.finishReason)) {
|
|
1402
|
-
yield {
|
|
1403
|
-
type: "done",
|
|
1404
|
-
done: {
|
|
1405
|
-
finishReason: this.mapGoogleFinishReason(data.candidates[0].finishReason),
|
|
1406
|
-
usage: {
|
|
1407
|
-
promptTokens: ((_e = data.usageMetadata) == null ? void 0 : _e.promptTokenCount) || 0,
|
|
1408
|
-
completionTokens: ((_f = data.usageMetadata) == null ? void 0 : _f.candidatesTokenCount) || 0,
|
|
1409
|
-
totalTokens: ((_g = data.usageMetadata) == null ? void 0 : _g.totalTokenCount) || 0
|
|
1410
|
-
}
|
|
1411
|
-
}
|
|
1412
|
-
};
|
|
1413
|
-
}
|
|
1414
|
-
} catch (e) {
|
|
1415
|
-
console.error("Failed to parse Google streaming response:", e);
|
|
1294
|
+
const chat = this.model.startChat({
|
|
1295
|
+
history: this.formatGoogleMessages(conversationMessages.slice(0, -1)),
|
|
1296
|
+
systemInstruction: systemPrompt
|
|
1297
|
+
});
|
|
1298
|
+
const lastMessage = conversationMessages[conversationMessages.length - 1];
|
|
1299
|
+
const result = yield new __await(chat.sendMessageStream(lastMessage.content));
|
|
1300
|
+
try {
|
|
1301
|
+
for (var iter = __forAwait(result.stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
1302
|
+
const chunk = temp.value;
|
|
1303
|
+
const text = chunk.text();
|
|
1304
|
+
if (text) {
|
|
1305
|
+
yield { type: "content", content: text };
|
|
1416
1306
|
}
|
|
1417
1307
|
}
|
|
1308
|
+
} catch (temp) {
|
|
1309
|
+
error = [temp];
|
|
1310
|
+
} finally {
|
|
1311
|
+
try {
|
|
1312
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
1313
|
+
} finally {
|
|
1314
|
+
if (error)
|
|
1315
|
+
throw error[0];
|
|
1316
|
+
}
|
|
1418
1317
|
}
|
|
1419
|
-
|
|
1318
|
+
const response = yield new __await(result.response);
|
|
1319
|
+
yield {
|
|
1320
|
+
type: "done",
|
|
1321
|
+
done: {
|
|
1322
|
+
finishReason: this.mapGoogleFinishReason((_c = (_b = response.candidates) == null ? void 0 : _b[0]) == null ? void 0 : _c.finishReason),
|
|
1323
|
+
usage: {
|
|
1324
|
+
promptTokens: ((_d = response.usageMetadata) == null ? void 0 : _d.promptTokenCount) || 0,
|
|
1325
|
+
completionTokens: ((_e = response.usageMetadata) == null ? void 0 : _e.candidatesTokenCount) || 0,
|
|
1326
|
+
totalTokens: ((_f = response.usageMetadata) == null ? void 0 : _f.totalTokenCount) || 0
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
};
|
|
1330
|
+
} catch (error2) {
|
|
1420
1331
|
yield {
|
|
1421
1332
|
type: "error",
|
|
1422
1333
|
error: new ProviderError(
|
|
1423
1334
|
"google",
|
|
1424
|
-
|
|
1335
|
+
error2 instanceof Error ? error2.message : "Streaming error",
|
|
1425
1336
|
void 0,
|
|
1426
|
-
{ originalError:
|
|
1337
|
+
{ originalError: error2 }
|
|
1427
1338
|
)
|
|
1428
1339
|
};
|
|
1429
1340
|
}
|
|
@@ -1509,9 +1420,6 @@ var GoogleProvider = class extends BaseProvider {
|
|
|
1509
1420
|
}
|
|
1510
1421
|
});
|
|
1511
1422
|
}
|
|
1512
|
-
/**
|
|
1513
|
-
* Map Google's finish reason to standard format
|
|
1514
|
-
*/
|
|
1515
1423
|
mapGoogleFinishReason(reason) {
|
|
1516
1424
|
const mapping = {
|
|
1517
1425
|
"STOP": "stop",
|
|
@@ -1520,89 +1428,85 @@ var GoogleProvider = class extends BaseProvider {
|
|
|
1520
1428
|
"RECITATION": "content_filter",
|
|
1521
1429
|
"OTHER": "stop"
|
|
1522
1430
|
};
|
|
1523
|
-
return mapping[reason] || "stop";
|
|
1431
|
+
return mapping[reason || "STOP"] || "stop";
|
|
1524
1432
|
}
|
|
1525
1433
|
};
|
|
1526
1434
|
|
|
1527
1435
|
// src/providers/openai/openai.ts
|
|
1436
|
+
import OpenAI from "openai";
|
|
1528
1437
|
var OpenAIProvider = class extends BaseProvider {
|
|
1529
|
-
|
|
1530
|
-
|
|
1438
|
+
constructor(config) {
|
|
1439
|
+
super(config);
|
|
1440
|
+
__publicField(this, "client");
|
|
1441
|
+
this.client = new OpenAI({
|
|
1442
|
+
apiKey: this.config.apiKey,
|
|
1443
|
+
baseURL: this.config.baseUrl
|
|
1444
|
+
});
|
|
1531
1445
|
}
|
|
1532
1446
|
generateText(request) {
|
|
1533
1447
|
return __async(this, null, function* () {
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
presence_penalty: this.config.presencePenalty,
|
|
1546
|
-
stop: this.config.stop
|
|
1547
|
-
})
|
|
1548
|
-
}
|
|
1549
|
-
);
|
|
1550
|
-
const data = yield response.json();
|
|
1448
|
+
var _a, _b, _c;
|
|
1449
|
+
const completion = yield this.client.chat.completions.create({
|
|
1450
|
+
model: this.config.model,
|
|
1451
|
+
messages: this.formatMessages(request),
|
|
1452
|
+
max_tokens: this.config.maxTokens,
|
|
1453
|
+
temperature: this.config.temperature,
|
|
1454
|
+
top_p: this.config.topP,
|
|
1455
|
+
frequency_penalty: this.config.frequencyPenalty,
|
|
1456
|
+
presence_penalty: this.config.presencePenalty,
|
|
1457
|
+
stop: this.config.stop
|
|
1458
|
+
});
|
|
1551
1459
|
return {
|
|
1552
|
-
text:
|
|
1460
|
+
text: completion.choices[0].message.content || "",
|
|
1553
1461
|
usage: {
|
|
1554
|
-
promptTokens:
|
|
1555
|
-
completionTokens:
|
|
1556
|
-
totalTokens:
|
|
1462
|
+
promptTokens: ((_a = completion.usage) == null ? void 0 : _a.prompt_tokens) || 0,
|
|
1463
|
+
completionTokens: ((_b = completion.usage) == null ? void 0 : _b.completion_tokens) || 0,
|
|
1464
|
+
totalTokens: ((_c = completion.usage) == null ? void 0 : _c.total_tokens) || 0
|
|
1557
1465
|
},
|
|
1558
|
-
finishReason:
|
|
1466
|
+
finishReason: completion.choices[0].finish_reason
|
|
1559
1467
|
};
|
|
1560
1468
|
});
|
|
1561
1469
|
}
|
|
1562
1470
|
streamText(request) {
|
|
1563
1471
|
return __asyncGenerator(this, null, function* () {
|
|
1564
|
-
var _a, _b;
|
|
1565
|
-
const
|
|
1566
|
-
this.
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
max_tokens: this.config.maxTokens,
|
|
1573
|
-
temperature: this.config.temperature,
|
|
1574
|
-
stream: true
|
|
1575
|
-
})
|
|
1576
|
-
},
|
|
1577
|
-
true
|
|
1578
|
-
));
|
|
1579
|
-
const reader = response.body.getReader();
|
|
1580
|
-
const decoder = new TextDecoder();
|
|
1581
|
-
let buffer = "";
|
|
1472
|
+
var _a, _b, _c;
|
|
1473
|
+
const stream = yield new __await(this.client.chat.completions.create({
|
|
1474
|
+
model: this.config.model,
|
|
1475
|
+
messages: this.formatMessages(request),
|
|
1476
|
+
max_tokens: this.config.maxTokens,
|
|
1477
|
+
temperature: this.config.temperature,
|
|
1478
|
+
stream: true
|
|
1479
|
+
}));
|
|
1582
1480
|
try {
|
|
1583
|
-
|
|
1584
|
-
const
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
}
|
|
1599
|
-
} catch (e) {
|
|
1481
|
+
for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
1482
|
+
const chunk = temp.value;
|
|
1483
|
+
const content = (_b = (_a = chunk.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.content;
|
|
1484
|
+
if (content) {
|
|
1485
|
+
yield { type: "content", content };
|
|
1486
|
+
}
|
|
1487
|
+
if ((_c = chunk.choices[0]) == null ? void 0 : _c.finish_reason) {
|
|
1488
|
+
yield {
|
|
1489
|
+
type: "done",
|
|
1490
|
+
done: {
|
|
1491
|
+
finishReason: chunk.choices[0].finish_reason,
|
|
1492
|
+
usage: chunk.usage ? {
|
|
1493
|
+
promptTokens: chunk.usage.prompt_tokens || 0,
|
|
1494
|
+
completionTokens: chunk.usage.completion_tokens || 0,
|
|
1495
|
+
totalTokens: chunk.usage.total_tokens || 0
|
|
1496
|
+
} : void 0
|
|
1600
1497
|
}
|
|
1601
|
-
}
|
|
1498
|
+
};
|
|
1602
1499
|
}
|
|
1603
1500
|
}
|
|
1501
|
+
} catch (temp) {
|
|
1502
|
+
error = [temp];
|
|
1604
1503
|
} finally {
|
|
1605
|
-
|
|
1504
|
+
try {
|
|
1505
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
1506
|
+
} finally {
|
|
1507
|
+
if (error)
|
|
1508
|
+
throw error[0];
|
|
1509
|
+
}
|
|
1606
1510
|
}
|
|
1607
1511
|
});
|
|
1608
1512
|
}
|
|
@@ -1661,29 +1565,22 @@ var OpenAIProvider = class extends BaseProvider {
|
|
|
1661
1565
|
};
|
|
1662
1566
|
|
|
1663
1567
|
// src/providers/sarvam/sarvam.ts
|
|
1664
|
-
var SarvamProvider = class extends
|
|
1568
|
+
var SarvamProvider = class extends CustomBaseProvider {
|
|
1665
1569
|
getDefaultBaseUrl() {
|
|
1666
1570
|
return "https://api.sarvam.ai/v1";
|
|
1667
1571
|
}
|
|
1668
1572
|
generateText(request) {
|
|
1669
1573
|
return __async(this, null, function* () {
|
|
1670
1574
|
var _a, _b, _c;
|
|
1671
|
-
|
|
1575
|
+
this.validateConfig();
|
|
1672
1576
|
try {
|
|
1673
|
-
const response = yield this.makeRequest(
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
})),
|
|
1681
|
-
max_tokens: this.config.maxTokens,
|
|
1682
|
-
temperature: this.config.temperature,
|
|
1683
|
-
top_p: this.config.topP,
|
|
1684
|
-
stop: this.config.stop
|
|
1685
|
-
})
|
|
1686
|
-
});
|
|
1577
|
+
const response = yield this.makeRequest(
|
|
1578
|
+
this.getEndpoint("/chat/completions"),
|
|
1579
|
+
{
|
|
1580
|
+
method: "POST",
|
|
1581
|
+
body: JSON.stringify(this.buildRequestBody(request))
|
|
1582
|
+
}
|
|
1583
|
+
);
|
|
1687
1584
|
const data = yield response.json();
|
|
1688
1585
|
if (!data.choices || data.choices.length === 0) {
|
|
1689
1586
|
throw new ProviderError(
|
|
@@ -1700,7 +1597,7 @@ var SarvamProvider = class extends BaseProvider {
|
|
|
1700
1597
|
completionTokens: ((_b = data.usage) == null ? void 0 : _b.completion_tokens) || 0,
|
|
1701
1598
|
totalTokens: ((_c = data.usage) == null ? void 0 : _c.total_tokens) || 0
|
|
1702
1599
|
},
|
|
1703
|
-
finishReason: this.
|
|
1600
|
+
finishReason: this.mapFinishReason(data.choices[0].finish_reason)
|
|
1704
1601
|
};
|
|
1705
1602
|
} catch (error) {
|
|
1706
1603
|
throw new ProviderError(
|
|
@@ -1715,70 +1612,55 @@ var SarvamProvider = class extends BaseProvider {
|
|
|
1715
1612
|
streamText(request) {
|
|
1716
1613
|
return __asyncGenerator(this, null, function* () {
|
|
1717
1614
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1718
|
-
|
|
1615
|
+
this.validateConfig();
|
|
1719
1616
|
try {
|
|
1720
1617
|
const response = yield new __await(this.makeRequest(
|
|
1721
1618
|
this.getEndpoint("/chat/completions"),
|
|
1722
1619
|
{
|
|
1723
1620
|
method: "POST",
|
|
1724
|
-
body: JSON.stringify(
|
|
1725
|
-
model: this.config.model,
|
|
1726
|
-
messages: messages.map((m) => ({
|
|
1727
|
-
role: m.role,
|
|
1728
|
-
content: m.content
|
|
1729
|
-
})),
|
|
1730
|
-
max_tokens: this.config.maxTokens,
|
|
1731
|
-
temperature: this.config.temperature,
|
|
1732
|
-
stream: true
|
|
1733
|
-
})
|
|
1621
|
+
body: JSON.stringify(this.buildRequestBody(request, true))
|
|
1734
1622
|
},
|
|
1735
1623
|
true
|
|
1736
1624
|
));
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
yield { type: "content", content };
|
|
1755
|
-
}
|
|
1756
|
-
if ((_e = (_d = parsed.choices) == null ? void 0 : _d[0]) == null ? void 0 : _e.finish_reason) {
|
|
1757
|
-
yield {
|
|
1758
|
-
type: "done",
|
|
1759
|
-
done: {
|
|
1760
|
-
finishReason: this.mapSarvamFinishReason(parsed.choices[0].finish_reason),
|
|
1761
|
-
usage: {
|
|
1762
|
-
promptTokens: ((_f = parsed.usage) == null ? void 0 : _f.prompt_tokens) || 0,
|
|
1763
|
-
completionTokens: ((_g = parsed.usage) == null ? void 0 : _g.completion_tokens) || 0,
|
|
1764
|
-
totalTokens: ((_h = parsed.usage) == null ? void 0 : _h.total_tokens) || 0
|
|
1765
|
-
}
|
|
1766
|
-
}
|
|
1767
|
-
};
|
|
1625
|
+
try {
|
|
1626
|
+
for (var iter = __forAwait(this.parseSSEStream(response)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
1627
|
+
const data = temp.value;
|
|
1628
|
+
const content = (_c = (_b = (_a = data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.content;
|
|
1629
|
+
if (content) {
|
|
1630
|
+
yield { type: "content", content };
|
|
1631
|
+
}
|
|
1632
|
+
if ((_e = (_d = data.choices) == null ? void 0 : _d[0]) == null ? void 0 : _e.finish_reason) {
|
|
1633
|
+
yield {
|
|
1634
|
+
type: "done",
|
|
1635
|
+
done: {
|
|
1636
|
+
finishReason: this.mapFinishReason(data.choices[0].finish_reason),
|
|
1637
|
+
usage: {
|
|
1638
|
+
promptTokens: ((_f = data.usage) == null ? void 0 : _f.prompt_tokens) || 0,
|
|
1639
|
+
completionTokens: ((_g = data.usage) == null ? void 0 : _g.completion_tokens) || 0,
|
|
1640
|
+
totalTokens: ((_h = data.usage) == null ? void 0 : _h.total_tokens) || 0
|
|
1641
|
+
}
|
|
1768
1642
|
}
|
|
1769
|
-
}
|
|
1770
|
-
}
|
|
1643
|
+
};
|
|
1771
1644
|
}
|
|
1772
1645
|
}
|
|
1646
|
+
} catch (temp) {
|
|
1647
|
+
error = [temp];
|
|
1648
|
+
} finally {
|
|
1649
|
+
try {
|
|
1650
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
1651
|
+
} finally {
|
|
1652
|
+
if (error)
|
|
1653
|
+
throw error[0];
|
|
1654
|
+
}
|
|
1773
1655
|
}
|
|
1774
|
-
} catch (
|
|
1656
|
+
} catch (error2) {
|
|
1775
1657
|
yield {
|
|
1776
1658
|
type: "error",
|
|
1777
1659
|
error: new ProviderError(
|
|
1778
1660
|
"sarvam",
|
|
1779
|
-
|
|
1661
|
+
error2 instanceof Error ? error2.message : "Streaming error",
|
|
1780
1662
|
void 0,
|
|
1781
|
-
{ originalError:
|
|
1663
|
+
{ originalError: error2 }
|
|
1782
1664
|
)
|
|
1783
1665
|
};
|
|
1784
1666
|
}
|
|
@@ -1846,10 +1728,7 @@ var SarvamProvider = class extends BaseProvider {
|
|
|
1846
1728
|
}
|
|
1847
1729
|
});
|
|
1848
1730
|
}
|
|
1849
|
-
|
|
1850
|
-
* Map Sarvam's finish reason to standard format
|
|
1851
|
-
*/
|
|
1852
|
-
mapSarvamFinishReason(reason) {
|
|
1731
|
+
mapFinishReason(reason) {
|
|
1853
1732
|
const mapping = {
|
|
1854
1733
|
"stop": "stop",
|
|
1855
1734
|
"length": "length",
|