@flare-ai-sdk/server 1.0.0 → 1.0.1
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 +387 -511
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +387 -511
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/dist/index.js
CHANGED
|
@@ -10,9 +10,6 @@ var __getProtoOf = Object.getPrototypeOf;
|
|
|
10
10
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
11
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
12
|
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
|
|
13
|
-
var __typeError = (msg) => {
|
|
14
|
-
throw TypeError(msg);
|
|
15
|
-
};
|
|
16
13
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
17
14
|
var __spreadValues = (a, b) => {
|
|
18
15
|
for (var prop in b || (b = {}))
|
|
@@ -83,34 +80,6 @@ var __asyncGenerator = (__this, __arguments, generator) => {
|
|
|
83
80
|
}, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
|
|
84
81
|
return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
|
|
85
82
|
};
|
|
86
|
-
var __yieldStar = (value) => {
|
|
87
|
-
var obj = value[__knownSymbol("asyncIterator")], isAwait = false, method, it = {};
|
|
88
|
-
if (obj == null) {
|
|
89
|
-
obj = value[__knownSymbol("iterator")]();
|
|
90
|
-
method = (k) => it[k] = (x) => obj[k](x);
|
|
91
|
-
} else {
|
|
92
|
-
obj = obj.call(value);
|
|
93
|
-
method = (k) => it[k] = (v) => {
|
|
94
|
-
if (isAwait) {
|
|
95
|
-
isAwait = false;
|
|
96
|
-
if (k === "throw") throw v;
|
|
97
|
-
return v;
|
|
98
|
-
}
|
|
99
|
-
isAwait = true;
|
|
100
|
-
return {
|
|
101
|
-
done: false,
|
|
102
|
-
value: new __await(new Promise((resolve) => {
|
|
103
|
-
var x = obj[k](v);
|
|
104
|
-
if (!(x instanceof Object)) __typeError("Object expected");
|
|
105
|
-
resolve(x);
|
|
106
|
-
}), 1)
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
return it[__knownSymbol("iterator")] = () => it, method("next"), "throw" in obj ? method("throw") : it.throw = (x) => {
|
|
111
|
-
throw x;
|
|
112
|
-
}, "return" in obj && method("return"), it;
|
|
113
|
-
};
|
|
114
83
|
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);
|
|
115
84
|
|
|
116
85
|
// src/index.ts
|
|
@@ -911,24 +880,10 @@ var BaseProvider = class {
|
|
|
911
880
|
__publicField(this, "config");
|
|
912
881
|
this.config = config;
|
|
913
882
|
}
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
}, options.headers || {});
|
|
919
|
-
if (this.config.apiKey) {
|
|
920
|
-
headers["Authorization"] = `Bearer ${this.config.apiKey}`;
|
|
921
|
-
}
|
|
922
|
-
const response = yield fetch(url, __spreadProps(__spreadValues({}, options), {
|
|
923
|
-
headers
|
|
924
|
-
}));
|
|
925
|
-
if (!response.ok && !stream) {
|
|
926
|
-
const errorText = yield response.text();
|
|
927
|
-
throw new Error(`Provider request failed: ${response.status} ${errorText}`);
|
|
928
|
-
}
|
|
929
|
-
return response;
|
|
930
|
-
});
|
|
931
|
-
}
|
|
883
|
+
/**
|
|
884
|
+
* Format messages for API request
|
|
885
|
+
* Handles system prompts and conversation history
|
|
886
|
+
*/
|
|
932
887
|
formatMessages(request) {
|
|
933
888
|
const messages = [];
|
|
934
889
|
if (request.systemPrompt) {
|
|
@@ -937,16 +892,29 @@ var BaseProvider = class {
|
|
|
937
892
|
messages.push(...request.messages);
|
|
938
893
|
return messages;
|
|
939
894
|
}
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
895
|
+
/**
|
|
896
|
+
* Validate configuration
|
|
897
|
+
*/
|
|
898
|
+
validateConfig() {
|
|
899
|
+
if (!this.config.apiKey) {
|
|
900
|
+
throw new Error(`API key is required for ${this.config.provider} provider`);
|
|
901
|
+
}
|
|
902
|
+
if (!this.config.model) {
|
|
903
|
+
throw new Error(`Model is required for ${this.config.provider} provider`);
|
|
904
|
+
}
|
|
943
905
|
}
|
|
944
906
|
};
|
|
945
907
|
|
|
946
908
|
// src/providers/anthropic/anthropic.ts
|
|
909
|
+
var import_sdk = __toESM(require("@anthropic-ai/sdk"));
|
|
947
910
|
var AnthropicProvider = class extends BaseProvider {
|
|
948
|
-
|
|
949
|
-
|
|
911
|
+
constructor(config) {
|
|
912
|
+
super(config);
|
|
913
|
+
__publicField(this, "client");
|
|
914
|
+
this.client = new import_sdk.default({
|
|
915
|
+
apiKey: this.config.apiKey,
|
|
916
|
+
baseURL: this.config.baseUrl
|
|
917
|
+
});
|
|
950
918
|
}
|
|
951
919
|
generateText(request) {
|
|
952
920
|
return __async(this, null, function* () {
|
|
@@ -954,31 +922,22 @@ var AnthropicProvider = class extends BaseProvider {
|
|
|
954
922
|
const messages = this.formatMessages(request);
|
|
955
923
|
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
956
924
|
const userMessages = messages.filter((m) => m.role !== "system");
|
|
957
|
-
const response = yield this.
|
|
958
|
-
this.
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
model: this.config.model,
|
|
966
|
-
messages: userMessages,
|
|
967
|
-
system: systemPrompt,
|
|
968
|
-
max_tokens: this.config.maxTokens || 1024,
|
|
969
|
-
temperature: this.config.temperature
|
|
970
|
-
})
|
|
971
|
-
}
|
|
972
|
-
);
|
|
973
|
-
const data = yield response.json();
|
|
925
|
+
const response = yield this.client.messages.create({
|
|
926
|
+
model: this.config.model,
|
|
927
|
+
messages: userMessages,
|
|
928
|
+
system: systemPrompt,
|
|
929
|
+
max_tokens: this.config.maxTokens || 1024,
|
|
930
|
+
temperature: this.config.temperature
|
|
931
|
+
});
|
|
932
|
+
const textContent = response.content.find((c) => c.type === "text");
|
|
974
933
|
return {
|
|
975
|
-
text:
|
|
934
|
+
text: (textContent == null ? void 0 : textContent.type) === "text" ? textContent.text : "",
|
|
976
935
|
usage: {
|
|
977
|
-
promptTokens:
|
|
978
|
-
completionTokens:
|
|
979
|
-
totalTokens:
|
|
936
|
+
promptTokens: response.usage.input_tokens,
|
|
937
|
+
completionTokens: response.usage.output_tokens,
|
|
938
|
+
totalTokens: response.usage.input_tokens + response.usage.output_tokens
|
|
980
939
|
},
|
|
981
|
-
finishReason:
|
|
940
|
+
finishReason: response.stop_reason
|
|
982
941
|
};
|
|
983
942
|
});
|
|
984
943
|
}
|
|
@@ -988,47 +947,43 @@ var AnthropicProvider = class extends BaseProvider {
|
|
|
988
947
|
const messages = this.formatMessages(request);
|
|
989
948
|
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
990
949
|
const userMessages = messages.filter((m) => m.role !== "system");
|
|
991
|
-
const
|
|
992
|
-
this.
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
body: JSON.stringify({
|
|
999
|
-
model: this.config.model,
|
|
1000
|
-
messages: userMessages,
|
|
1001
|
-
system: systemPrompt,
|
|
1002
|
-
max_tokens: this.config.maxTokens || 1024,
|
|
1003
|
-
stream: true
|
|
1004
|
-
})
|
|
1005
|
-
},
|
|
1006
|
-
true
|
|
1007
|
-
));
|
|
1008
|
-
const reader = response.body.getReader();
|
|
1009
|
-
const decoder = new TextDecoder();
|
|
1010
|
-
let buffer = "";
|
|
950
|
+
const stream = yield new __await(this.client.messages.stream({
|
|
951
|
+
model: this.config.model,
|
|
952
|
+
messages: userMessages,
|
|
953
|
+
system: systemPrompt,
|
|
954
|
+
max_tokens: this.config.maxTokens || 1024,
|
|
955
|
+
temperature: this.config.temperature
|
|
956
|
+
}));
|
|
1011
957
|
try {
|
|
1012
|
-
|
|
1013
|
-
const
|
|
1014
|
-
if (
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
958
|
+
for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
959
|
+
const event = temp.value;
|
|
960
|
+
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
961
|
+
yield { type: "content", content: event.delta.text };
|
|
962
|
+
}
|
|
963
|
+
if (event.type === "message_stop") {
|
|
964
|
+
const message = yield new __await(stream.finalMessage());
|
|
965
|
+
yield {
|
|
966
|
+
type: "done",
|
|
967
|
+
done: {
|
|
968
|
+
finishReason: message.stop_reason,
|
|
969
|
+
usage: {
|
|
970
|
+
promptTokens: message.usage.input_tokens,
|
|
971
|
+
completionTokens: message.usage.output_tokens,
|
|
972
|
+
totalTokens: message.usage.input_tokens + message.usage.output_tokens
|
|
1024
973
|
}
|
|
1025
|
-
} catch (e) {
|
|
1026
974
|
}
|
|
1027
|
-
}
|
|
975
|
+
};
|
|
1028
976
|
}
|
|
1029
977
|
}
|
|
978
|
+
} catch (temp) {
|
|
979
|
+
error = [temp];
|
|
1030
980
|
} finally {
|
|
1031
|
-
|
|
981
|
+
try {
|
|
982
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
983
|
+
} finally {
|
|
984
|
+
if (error)
|
|
985
|
+
throw error[0];
|
|
986
|
+
}
|
|
1032
987
|
}
|
|
1033
988
|
});
|
|
1034
989
|
}
|
|
@@ -1086,35 +1041,128 @@ var AnthropicProvider = class extends BaseProvider {
|
|
|
1086
1041
|
}
|
|
1087
1042
|
};
|
|
1088
1043
|
|
|
1089
|
-
// src/providers/custom
|
|
1090
|
-
var
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1044
|
+
// src/providers/custom-base.ts
|
|
1045
|
+
var CustomBaseProvider = class extends BaseProvider {
|
|
1046
|
+
/**
|
|
1047
|
+
* Make HTTP request with proper error handling
|
|
1048
|
+
*/
|
|
1049
|
+
makeRequest(url, options, stream = false) {
|
|
1050
|
+
return __async(this, null, function* () {
|
|
1051
|
+
const headers = __spreadValues({
|
|
1052
|
+
"Content-Type": "application/json"
|
|
1053
|
+
}, options.headers || {});
|
|
1054
|
+
if (this.config.apiKey) {
|
|
1055
|
+
headers["Authorization"] = `Bearer ${this.config.apiKey}`;
|
|
1056
|
+
}
|
|
1057
|
+
const response = yield fetch(url, __spreadProps(__spreadValues({}, options), {
|
|
1058
|
+
headers
|
|
1059
|
+
}));
|
|
1060
|
+
if (!response.ok && !stream) {
|
|
1061
|
+
const errorText = yield response.text();
|
|
1062
|
+
throw new Error(`Provider request failed: ${response.status} ${errorText}`);
|
|
1063
|
+
}
|
|
1064
|
+
return response;
|
|
1065
|
+
});
|
|
1097
1066
|
}
|
|
1098
|
-
|
|
1099
|
-
|
|
1067
|
+
/**
|
|
1068
|
+
* Get API endpoint URL
|
|
1069
|
+
*/
|
|
1070
|
+
getEndpoint(path) {
|
|
1071
|
+
const baseUrl = this.config.baseUrl || this.getDefaultBaseUrl();
|
|
1072
|
+
return `${baseUrl}${path}`;
|
|
1073
|
+
}
|
|
1074
|
+
/**
|
|
1075
|
+
* Parse streaming SSE response
|
|
1076
|
+
*/
|
|
1077
|
+
parseSSEStream(response) {
|
|
1078
|
+
return __asyncGenerator(this, null, function* () {
|
|
1079
|
+
const reader = response.body.getReader();
|
|
1080
|
+
const decoder = new TextDecoder();
|
|
1081
|
+
let buffer = "";
|
|
1082
|
+
try {
|
|
1083
|
+
while (true) {
|
|
1084
|
+
const { done, value } = yield new __await(reader.read());
|
|
1085
|
+
if (done) break;
|
|
1086
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1087
|
+
const lines = buffer.split("\n");
|
|
1088
|
+
buffer = lines.pop() || "";
|
|
1089
|
+
for (const line of lines) {
|
|
1090
|
+
if (line.startsWith("data: ")) {
|
|
1091
|
+
const data = line.slice(6).trim();
|
|
1092
|
+
if (data === "[DONE]") continue;
|
|
1093
|
+
try {
|
|
1094
|
+
yield JSON.parse(data);
|
|
1095
|
+
} catch (e) {
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
} finally {
|
|
1101
|
+
reader.releaseLock();
|
|
1102
|
+
}
|
|
1103
|
+
});
|
|
1100
1104
|
}
|
|
1101
1105
|
/**
|
|
1102
|
-
*
|
|
1106
|
+
* Build request body with common parameters
|
|
1103
1107
|
*/
|
|
1104
|
-
|
|
1105
|
-
|
|
1108
|
+
buildRequestBody(request, stream = false) {
|
|
1109
|
+
return {
|
|
1110
|
+
model: this.config.model,
|
|
1111
|
+
messages: this.formatMessages(request),
|
|
1112
|
+
max_tokens: this.config.maxTokens,
|
|
1113
|
+
temperature: this.config.temperature,
|
|
1114
|
+
top_p: this.config.topP,
|
|
1115
|
+
frequency_penalty: this.config.frequencyPenalty,
|
|
1116
|
+
presence_penalty: this.config.presencePenalty,
|
|
1117
|
+
stop: this.config.stop,
|
|
1118
|
+
stream
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
};
|
|
1122
|
+
|
|
1123
|
+
// src/providers/custom/custom.ts
|
|
1124
|
+
var CustomProvider = class extends CustomBaseProvider {
|
|
1125
|
+
getDefaultBaseUrl() {
|
|
1126
|
+
if (!this.config.baseUrl) {
|
|
1127
|
+
throw new ProviderError(
|
|
1128
|
+
"custom",
|
|
1129
|
+
"baseUrl is required for custom provider",
|
|
1130
|
+
void 0,
|
|
1131
|
+
{ config: this.config }
|
|
1132
|
+
);
|
|
1133
|
+
}
|
|
1134
|
+
return this.config.baseUrl;
|
|
1106
1135
|
}
|
|
1107
1136
|
generateText(request) {
|
|
1108
1137
|
return __async(this, null, function* () {
|
|
1109
|
-
|
|
1138
|
+
var _a, _b, _c;
|
|
1139
|
+
this.validateConfig();
|
|
1110
1140
|
try {
|
|
1111
|
-
const
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1141
|
+
const response = yield this.makeRequest(
|
|
1142
|
+
this.getEndpoint("/chat/completions"),
|
|
1143
|
+
{
|
|
1144
|
+
method: "POST",
|
|
1145
|
+
body: JSON.stringify(this.buildRequestBody(request))
|
|
1146
|
+
}
|
|
1147
|
+
);
|
|
1116
1148
|
const data = yield response.json();
|
|
1117
|
-
|
|
1149
|
+
if (!data.choices || data.choices.length === 0) {
|
|
1150
|
+
throw new ProviderError(
|
|
1151
|
+
"custom",
|
|
1152
|
+
"No choices returned from custom API",
|
|
1153
|
+
response.status,
|
|
1154
|
+
{ response: data }
|
|
1155
|
+
);
|
|
1156
|
+
}
|
|
1157
|
+
return {
|
|
1158
|
+
text: data.choices[0].message.content,
|
|
1159
|
+
usage: {
|
|
1160
|
+
promptTokens: ((_a = data.usage) == null ? void 0 : _a.prompt_tokens) || 0,
|
|
1161
|
+
completionTokens: ((_b = data.usage) == null ? void 0 : _b.completion_tokens) || 0,
|
|
1162
|
+
totalTokens: ((_c = data.usage) == null ? void 0 : _c.total_tokens) || 0
|
|
1163
|
+
},
|
|
1164
|
+
finishReason: data.choices[0].finish_reason || "stop"
|
|
1165
|
+
};
|
|
1118
1166
|
} catch (error) {
|
|
1119
1167
|
throw new ProviderError(
|
|
1120
1168
|
"custom",
|
|
@@ -1127,26 +1175,56 @@ var CustomProvider = class extends BaseProvider {
|
|
|
1127
1175
|
}
|
|
1128
1176
|
streamText(request) {
|
|
1129
1177
|
return __asyncGenerator(this, null, function* () {
|
|
1130
|
-
|
|
1178
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1179
|
+
this.validateConfig();
|
|
1131
1180
|
try {
|
|
1132
|
-
const requestBody = this.formatRequestBody(messages, true);
|
|
1133
1181
|
const response = yield new __await(this.makeRequest(
|
|
1134
1182
|
this.getEndpoint("/chat/completions"),
|
|
1135
1183
|
{
|
|
1136
1184
|
method: "POST",
|
|
1137
|
-
body: JSON.stringify(
|
|
1185
|
+
body: JSON.stringify(this.buildRequestBody(request, true))
|
|
1138
1186
|
},
|
|
1139
1187
|
true
|
|
1140
1188
|
));
|
|
1141
|
-
|
|
1142
|
-
|
|
1189
|
+
try {
|
|
1190
|
+
for (var iter = __forAwait(this.parseSSEStream(response)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
1191
|
+
const data = temp.value;
|
|
1192
|
+
const content = (_c = (_b = (_a = data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.content;
|
|
1193
|
+
if (content) {
|
|
1194
|
+
yield { type: "content", content };
|
|
1195
|
+
}
|
|
1196
|
+
if ((_e = (_d = data.choices) == null ? void 0 : _d[0]) == null ? void 0 : _e.finish_reason) {
|
|
1197
|
+
yield {
|
|
1198
|
+
type: "done",
|
|
1199
|
+
done: {
|
|
1200
|
+
finishReason: data.choices[0].finish_reason || "stop",
|
|
1201
|
+
usage: {
|
|
1202
|
+
promptTokens: ((_f = data.usage) == null ? void 0 : _f.prompt_tokens) || 0,
|
|
1203
|
+
completionTokens: ((_g = data.usage) == null ? void 0 : _g.completion_tokens) || 0,
|
|
1204
|
+
totalTokens: ((_h = data.usage) == null ? void 0 : _h.total_tokens) || 0
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
} catch (temp) {
|
|
1211
|
+
error = [temp];
|
|
1212
|
+
} finally {
|
|
1213
|
+
try {
|
|
1214
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
1215
|
+
} finally {
|
|
1216
|
+
if (error)
|
|
1217
|
+
throw error[0];
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
} catch (error2) {
|
|
1143
1221
|
yield {
|
|
1144
1222
|
type: "error",
|
|
1145
1223
|
error: new ProviderError(
|
|
1146
1224
|
"custom",
|
|
1147
|
-
|
|
1225
|
+
error2 instanceof Error ? error2.message : "Streaming error",
|
|
1148
1226
|
void 0,
|
|
1149
|
-
{ originalError:
|
|
1227
|
+
{ originalError: error2 }
|
|
1150
1228
|
)
|
|
1151
1229
|
};
|
|
1152
1230
|
}
|
|
@@ -1159,18 +1237,13 @@ var CustomProvider = class extends BaseProvider {
|
|
|
1159
1237
|
...request.messages,
|
|
1160
1238
|
{
|
|
1161
1239
|
role: "user",
|
|
1162
|
-
content: `Respond with valid JSON matching this schema: ${JSON.stringify(request.schema)}
|
|
1240
|
+
content: `Respond with valid JSON matching this schema: ${JSON.stringify(request.schema)}`
|
|
1163
1241
|
}
|
|
1164
1242
|
]
|
|
1165
1243
|
});
|
|
1166
1244
|
const textResponse = yield this.generateText(enhancedRequest);
|
|
1167
|
-
let jsonText = textResponse.text.trim();
|
|
1168
|
-
const codeBlockMatch = jsonText.match(/```(?:json)?\s*([\s\S]*?)\s*```/);
|
|
1169
|
-
if (codeBlockMatch) {
|
|
1170
|
-
jsonText = codeBlockMatch[1];
|
|
1171
|
-
}
|
|
1172
1245
|
try {
|
|
1173
|
-
const object = JSON.parse(
|
|
1246
|
+
const object = JSON.parse(textResponse.text);
|
|
1174
1247
|
return {
|
|
1175
1248
|
object,
|
|
1176
1249
|
text: textResponse.text,
|
|
@@ -1194,13 +1267,8 @@ var CustomProvider = class extends BaseProvider {
|
|
|
1194
1267
|
const chunk = temp.value;
|
|
1195
1268
|
if (chunk.type === "content") {
|
|
1196
1269
|
accumulatedText += chunk.content;
|
|
1197
|
-
let jsonText2 = accumulatedText.trim();
|
|
1198
|
-
const codeBlockMatch2 = jsonText2.match(/```(?:json)?\s*([\s\S]*?)\s*```/);
|
|
1199
|
-
if (codeBlockMatch2) {
|
|
1200
|
-
jsonText2 = codeBlockMatch2[1];
|
|
1201
|
-
}
|
|
1202
1270
|
try {
|
|
1203
|
-
const partial = JSON.parse(
|
|
1271
|
+
const partial = JSON.parse(accumulatedText);
|
|
1204
1272
|
yield { type: "partial", partial, text: accumulatedText };
|
|
1205
1273
|
} catch (e) {
|
|
1206
1274
|
}
|
|
@@ -1216,203 +1284,67 @@ var CustomProvider = class extends BaseProvider {
|
|
|
1216
1284
|
throw error[0];
|
|
1217
1285
|
}
|
|
1218
1286
|
}
|
|
1219
|
-
let jsonText = accumulatedText.trim();
|
|
1220
|
-
const codeBlockMatch = jsonText.match(/```(?:json)?\s*([\s\S]*?)\s*```/);
|
|
1221
|
-
if (codeBlockMatch) {
|
|
1222
|
-
jsonText = codeBlockMatch[1];
|
|
1223
|
-
}
|
|
1224
1287
|
try {
|
|
1225
|
-
const object = JSON.parse(
|
|
1288
|
+
const object = JSON.parse(accumulatedText);
|
|
1226
1289
|
yield { type: "complete", object, text: accumulatedText };
|
|
1227
1290
|
} catch (e) {
|
|
1228
1291
|
yield { type: "error", error: new Error("Invalid JSON in response") };
|
|
1229
1292
|
}
|
|
1230
1293
|
});
|
|
1231
1294
|
}
|
|
1232
|
-
/**
|
|
1233
|
-
* Format request body based on provider format
|
|
1234
|
-
*/
|
|
1235
|
-
formatRequestBody(messages, stream) {
|
|
1236
|
-
var _a;
|
|
1237
|
-
if (this.requestFormat === "anthropic") {
|
|
1238
|
-
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
1239
|
-
const userMessages = messages.filter((m) => m.role !== "system");
|
|
1240
|
-
return {
|
|
1241
|
-
model: this.config.model,
|
|
1242
|
-
messages: userMessages,
|
|
1243
|
-
system: systemPrompt,
|
|
1244
|
-
max_tokens: this.config.maxTokens || 1024,
|
|
1245
|
-
temperature: this.config.temperature,
|
|
1246
|
-
top_p: this.config.topP,
|
|
1247
|
-
stream
|
|
1248
|
-
};
|
|
1249
|
-
}
|
|
1250
|
-
return {
|
|
1251
|
-
model: this.config.model,
|
|
1252
|
-
messages,
|
|
1253
|
-
max_tokens: this.config.maxTokens,
|
|
1254
|
-
temperature: this.config.temperature,
|
|
1255
|
-
top_p: this.config.topP,
|
|
1256
|
-
frequency_penalty: this.config.frequencyPenalty,
|
|
1257
|
-
presence_penalty: this.config.presencePenalty,
|
|
1258
|
-
stop: this.config.stop,
|
|
1259
|
-
stream
|
|
1260
|
-
};
|
|
1261
|
-
}
|
|
1262
|
-
/**
|
|
1263
|
-
* Parse text response based on provider format
|
|
1264
|
-
*/
|
|
1265
|
-
parseTextResponse(data) {
|
|
1266
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1267
|
-
if (data.choices && ((_a = data.choices[0]) == null ? void 0 : _a.message)) {
|
|
1268
|
-
return {
|
|
1269
|
-
text: data.choices[0].message.content,
|
|
1270
|
-
usage: {
|
|
1271
|
-
promptTokens: ((_b = data.usage) == null ? void 0 : _b.prompt_tokens) || 0,
|
|
1272
|
-
completionTokens: ((_c = data.usage) == null ? void 0 : _c.completion_tokens) || 0,
|
|
1273
|
-
totalTokens: ((_d = data.usage) == null ? void 0 : _d.total_tokens) || 0
|
|
1274
|
-
},
|
|
1275
|
-
finishReason: data.choices[0].finish_reason || "stop"
|
|
1276
|
-
};
|
|
1277
|
-
}
|
|
1278
|
-
if (data.content && Array.isArray(data.content)) {
|
|
1279
|
-
return {
|
|
1280
|
-
text: ((_e = data.content[0]) == null ? void 0 : _e.text) || "",
|
|
1281
|
-
usage: {
|
|
1282
|
-
promptTokens: ((_f = data.usage) == null ? void 0 : _f.input_tokens) || 0,
|
|
1283
|
-
completionTokens: ((_g = data.usage) == null ? void 0 : _g.output_tokens) || 0,
|
|
1284
|
-
totalTokens: (((_h = data.usage) == null ? void 0 : _h.input_tokens) || 0) + (((_i = data.usage) == null ? void 0 : _i.output_tokens) || 0)
|
|
1285
|
-
},
|
|
1286
|
-
finishReason: data.stop_reason || "stop"
|
|
1287
|
-
};
|
|
1288
|
-
}
|
|
1289
|
-
return {
|
|
1290
|
-
text: data.text || data.response || JSON.stringify(data),
|
|
1291
|
-
usage: {
|
|
1292
|
-
promptTokens: 0,
|
|
1293
|
-
completionTokens: 0,
|
|
1294
|
-
totalTokens: 0
|
|
1295
|
-
},
|
|
1296
|
-
finishReason: "stop"
|
|
1297
|
-
};
|
|
1298
|
-
}
|
|
1299
|
-
/**
|
|
1300
|
-
* Parse streaming response
|
|
1301
|
-
*/
|
|
1302
|
-
parseStreamResponse(response) {
|
|
1303
|
-
return __asyncGenerator(this, null, function* () {
|
|
1304
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
1305
|
-
const reader = response.body.getReader();
|
|
1306
|
-
const decoder = new TextDecoder();
|
|
1307
|
-
let buffer = "";
|
|
1308
|
-
try {
|
|
1309
|
-
while (true) {
|
|
1310
|
-
const { done, value } = yield new __await(reader.read());
|
|
1311
|
-
if (done) break;
|
|
1312
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1313
|
-
const lines = buffer.split("\n");
|
|
1314
|
-
buffer = lines.pop() || "";
|
|
1315
|
-
for (const line of lines) {
|
|
1316
|
-
if (line.startsWith("data: ")) {
|
|
1317
|
-
const data = line.slice(6);
|
|
1318
|
-
if (data === "[DONE]") continue;
|
|
1319
|
-
try {
|
|
1320
|
-
const parsed = JSON.parse(data);
|
|
1321
|
-
if ((_c = (_b = (_a = parsed.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.content) {
|
|
1322
|
-
yield { type: "content", content: parsed.choices[0].delta.content };
|
|
1323
|
-
}
|
|
1324
|
-
if (parsed.type === "content_block_delta" && ((_d = parsed.delta) == null ? void 0 : _d.text)) {
|
|
1325
|
-
yield { type: "content", content: parsed.delta.text };
|
|
1326
|
-
}
|
|
1327
|
-
if (parsed.content && !parsed.choices) {
|
|
1328
|
-
yield { type: "content", content: parsed.content };
|
|
1329
|
-
}
|
|
1330
|
-
if (((_f = (_e = parsed.choices) == null ? void 0 : _e[0]) == null ? void 0 : _f.finish_reason) || parsed.stop_reason) {
|
|
1331
|
-
yield {
|
|
1332
|
-
type: "done",
|
|
1333
|
-
done: {
|
|
1334
|
-
finishReason: ((_h = (_g = parsed.choices) == null ? void 0 : _g[0]) == null ? void 0 : _h.finish_reason) || parsed.stop_reason || "stop",
|
|
1335
|
-
usage: {
|
|
1336
|
-
promptTokens: ((_i = parsed.usage) == null ? void 0 : _i.prompt_tokens) || ((_j = parsed.usage) == null ? void 0 : _j.input_tokens) || 0,
|
|
1337
|
-
completionTokens: ((_k = parsed.usage) == null ? void 0 : _k.completion_tokens) || ((_l = parsed.usage) == null ? void 0 : _l.output_tokens) || 0,
|
|
1338
|
-
totalTokens: ((_m = parsed.usage) == null ? void 0 : _m.total_tokens) || 0
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
};
|
|
1342
|
-
}
|
|
1343
|
-
} catch (e) {
|
|
1344
|
-
console.error("Failed to parse streaming chunk:", e);
|
|
1345
|
-
}
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
}
|
|
1349
|
-
} finally {
|
|
1350
|
-
reader.releaseLock();
|
|
1351
|
-
}
|
|
1352
|
-
});
|
|
1353
|
-
}
|
|
1354
1295
|
};
|
|
1355
1296
|
|
|
1356
1297
|
// src/providers/google/google.ts
|
|
1298
|
+
var import_generative_ai = require("@google/generative-ai");
|
|
1357
1299
|
var GoogleProvider = class extends BaseProvider {
|
|
1358
|
-
|
|
1359
|
-
|
|
1300
|
+
constructor(config) {
|
|
1301
|
+
super(config);
|
|
1302
|
+
__publicField(this, "genAI");
|
|
1303
|
+
__publicField(this, "model");
|
|
1304
|
+
this.genAI = new import_generative_ai.GoogleGenerativeAI(this.config.apiKey);
|
|
1305
|
+
this.model = this.genAI.getGenerativeModel({
|
|
1306
|
+
model: this.config.model,
|
|
1307
|
+
generationConfig: {
|
|
1308
|
+
maxOutputTokens: this.config.maxTokens,
|
|
1309
|
+
temperature: this.config.temperature,
|
|
1310
|
+
topP: this.config.topP,
|
|
1311
|
+
stopSequences: this.config.stop
|
|
1312
|
+
}
|
|
1313
|
+
});
|
|
1360
1314
|
}
|
|
1361
1315
|
/**
|
|
1362
1316
|
* Format messages for Google's API format
|
|
1363
1317
|
*/
|
|
1364
1318
|
formatGoogleMessages(messages) {
|
|
1365
1319
|
return messages.map((msg) => ({
|
|
1366
|
-
role: msg.role === "assistant" ? "model" :
|
|
1320
|
+
role: msg.role === "assistant" ? "model" : "user",
|
|
1367
1321
|
parts: [{ text: msg.content }]
|
|
1368
1322
|
}));
|
|
1369
1323
|
}
|
|
1370
1324
|
generateText(request) {
|
|
1371
1325
|
return __async(this, null, function* () {
|
|
1372
|
-
var _a, _b, _c, _d;
|
|
1326
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1373
1327
|
const messages = this.formatMessages(request);
|
|
1374
1328
|
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
1375
1329
|
const conversationMessages = messages.filter((m) => m.role !== "system");
|
|
1376
|
-
const endpoint = this.getEndpoint(
|
|
1377
|
-
`/models/${this.config.model}:generateContent?key=${this.config.apiKey}`
|
|
1378
|
-
);
|
|
1379
1330
|
try {
|
|
1380
|
-
const
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
contents: this.formatGoogleMessages(conversationMessages),
|
|
1384
|
-
systemInstruction: systemPrompt ? {
|
|
1385
|
-
parts: [{ text: systemPrompt }]
|
|
1386
|
-
} : void 0,
|
|
1387
|
-
generationConfig: {
|
|
1388
|
-
maxOutputTokens: this.config.maxTokens,
|
|
1389
|
-
temperature: this.config.temperature,
|
|
1390
|
-
topP: this.config.topP,
|
|
1391
|
-
stopSequences: this.config.stop
|
|
1392
|
-
}
|
|
1393
|
-
})
|
|
1331
|
+
const chat = this.model.startChat({
|
|
1332
|
+
history: this.formatGoogleMessages(conversationMessages.slice(0, -1)),
|
|
1333
|
+
systemInstruction: systemPrompt
|
|
1394
1334
|
});
|
|
1395
|
-
const
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
"google",
|
|
1399
|
-
"No candidates returned from Google API",
|
|
1400
|
-
response.status,
|
|
1401
|
-
{ response: data }
|
|
1402
|
-
);
|
|
1403
|
-
}
|
|
1404
|
-
const candidate = data.candidates[0];
|
|
1405
|
-
const text = candidate.content.parts.map((p) => p.text).join("");
|
|
1335
|
+
const lastMessage = conversationMessages[conversationMessages.length - 1];
|
|
1336
|
+
const result = yield chat.sendMessage(lastMessage.content);
|
|
1337
|
+
const response = result.response;
|
|
1406
1338
|
return {
|
|
1407
|
-
text,
|
|
1339
|
+
text: response.text(),
|
|
1408
1340
|
usage: {
|
|
1409
|
-
promptTokens: ((_b =
|
|
1410
|
-
completionTokens: ((_c =
|
|
1411
|
-
totalTokens: ((_d =
|
|
1341
|
+
promptTokens: ((_b = response.usageMetadata) == null ? void 0 : _b.promptTokenCount) || 0,
|
|
1342
|
+
completionTokens: ((_c = response.usageMetadata) == null ? void 0 : _c.candidatesTokenCount) || 0,
|
|
1343
|
+
totalTokens: ((_d = response.usageMetadata) == null ? void 0 : _d.totalTokenCount) || 0
|
|
1412
1344
|
},
|
|
1413
|
-
finishReason: this.mapGoogleFinishReason(
|
|
1345
|
+
finishReason: this.mapGoogleFinishReason((_f = (_e = response.candidates) == null ? void 0 : _e[0]) == null ? void 0 : _f.finishReason),
|
|
1414
1346
|
metadata: {
|
|
1415
|
-
safetyRatings:
|
|
1347
|
+
safetyRatings: (_h = (_g = response.candidates) == null ? void 0 : _g[0]) == null ? void 0 : _h.safetyRatings
|
|
1416
1348
|
}
|
|
1417
1349
|
};
|
|
1418
1350
|
} catch (error) {
|
|
@@ -1427,79 +1359,55 @@ var GoogleProvider = class extends BaseProvider {
|
|
|
1427
1359
|
}
|
|
1428
1360
|
streamText(request) {
|
|
1429
1361
|
return __asyncGenerator(this, null, function* () {
|
|
1430
|
-
var _a, _b, _c, _d, _e, _f
|
|
1362
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1431
1363
|
const messages = this.formatMessages(request);
|
|
1432
1364
|
const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
|
|
1433
1365
|
const conversationMessages = messages.filter((m) => m.role !== "system");
|
|
1434
|
-
const endpoint = this.getEndpoint(
|
|
1435
|
-
`/models/${this.config.model}:streamGenerateContent?key=${this.config.apiKey}`
|
|
1436
|
-
);
|
|
1437
1366
|
try {
|
|
1438
|
-
const
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
topP: this.config.topP
|
|
1451
|
-
}
|
|
1452
|
-
})
|
|
1453
|
-
},
|
|
1454
|
-
true
|
|
1455
|
-
));
|
|
1456
|
-
const reader = response.body.getReader();
|
|
1457
|
-
const decoder = new TextDecoder();
|
|
1458
|
-
let buffer = "";
|
|
1459
|
-
while (true) {
|
|
1460
|
-
const { done, value } = yield new __await(reader.read());
|
|
1461
|
-
if (done) break;
|
|
1462
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1463
|
-
const lines = buffer.split("\n");
|
|
1464
|
-
buffer = lines.pop() || "";
|
|
1465
|
-
for (const line of lines) {
|
|
1466
|
-
if (!line.trim() || !line.startsWith("data: ")) continue;
|
|
1467
|
-
try {
|
|
1468
|
-
const jsonStr = line.slice(6).trim();
|
|
1469
|
-
if (!jsonStr) continue;
|
|
1470
|
-
const data = JSON.parse(jsonStr);
|
|
1471
|
-
if (data.candidates && ((_c = (_b = data.candidates[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts)) {
|
|
1472
|
-
const text = data.candidates[0].content.parts.map((p) => p.text).join("");
|
|
1473
|
-
if (text) {
|
|
1474
|
-
yield { type: "content", content: text };
|
|
1475
|
-
}
|
|
1476
|
-
}
|
|
1477
|
-
if (data.candidates && ((_d = data.candidates[0]) == null ? void 0 : _d.finishReason)) {
|
|
1478
|
-
yield {
|
|
1479
|
-
type: "done",
|
|
1480
|
-
done: {
|
|
1481
|
-
finishReason: this.mapGoogleFinishReason(data.candidates[0].finishReason),
|
|
1482
|
-
usage: {
|
|
1483
|
-
promptTokens: ((_e = data.usageMetadata) == null ? void 0 : _e.promptTokenCount) || 0,
|
|
1484
|
-
completionTokens: ((_f = data.usageMetadata) == null ? void 0 : _f.candidatesTokenCount) || 0,
|
|
1485
|
-
totalTokens: ((_g = data.usageMetadata) == null ? void 0 : _g.totalTokenCount) || 0
|
|
1486
|
-
}
|
|
1487
|
-
}
|
|
1488
|
-
};
|
|
1489
|
-
}
|
|
1490
|
-
} catch (e) {
|
|
1491
|
-
console.error("Failed to parse Google streaming response:", e);
|
|
1367
|
+
const chat = this.model.startChat({
|
|
1368
|
+
history: this.formatGoogleMessages(conversationMessages.slice(0, -1)),
|
|
1369
|
+
systemInstruction: systemPrompt
|
|
1370
|
+
});
|
|
1371
|
+
const lastMessage = conversationMessages[conversationMessages.length - 1];
|
|
1372
|
+
const result = yield new __await(chat.sendMessageStream(lastMessage.content));
|
|
1373
|
+
try {
|
|
1374
|
+
for (var iter = __forAwait(result.stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
1375
|
+
const chunk = temp.value;
|
|
1376
|
+
const text = chunk.text();
|
|
1377
|
+
if (text) {
|
|
1378
|
+
yield { type: "content", content: text };
|
|
1492
1379
|
}
|
|
1493
1380
|
}
|
|
1381
|
+
} catch (temp) {
|
|
1382
|
+
error = [temp];
|
|
1383
|
+
} finally {
|
|
1384
|
+
try {
|
|
1385
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
1386
|
+
} finally {
|
|
1387
|
+
if (error)
|
|
1388
|
+
throw error[0];
|
|
1389
|
+
}
|
|
1494
1390
|
}
|
|
1495
|
-
|
|
1391
|
+
const response = yield new __await(result.response);
|
|
1392
|
+
yield {
|
|
1393
|
+
type: "done",
|
|
1394
|
+
done: {
|
|
1395
|
+
finishReason: this.mapGoogleFinishReason((_c = (_b = response.candidates) == null ? void 0 : _b[0]) == null ? void 0 : _c.finishReason),
|
|
1396
|
+
usage: {
|
|
1397
|
+
promptTokens: ((_d = response.usageMetadata) == null ? void 0 : _d.promptTokenCount) || 0,
|
|
1398
|
+
completionTokens: ((_e = response.usageMetadata) == null ? void 0 : _e.candidatesTokenCount) || 0,
|
|
1399
|
+
totalTokens: ((_f = response.usageMetadata) == null ? void 0 : _f.totalTokenCount) || 0
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
} catch (error2) {
|
|
1496
1404
|
yield {
|
|
1497
1405
|
type: "error",
|
|
1498
1406
|
error: new ProviderError(
|
|
1499
1407
|
"google",
|
|
1500
|
-
|
|
1408
|
+
error2 instanceof Error ? error2.message : "Streaming error",
|
|
1501
1409
|
void 0,
|
|
1502
|
-
{ originalError:
|
|
1410
|
+
{ originalError: error2 }
|
|
1503
1411
|
)
|
|
1504
1412
|
};
|
|
1505
1413
|
}
|
|
@@ -1585,9 +1493,6 @@ var GoogleProvider = class extends BaseProvider {
|
|
|
1585
1493
|
}
|
|
1586
1494
|
});
|
|
1587
1495
|
}
|
|
1588
|
-
/**
|
|
1589
|
-
* Map Google's finish reason to standard format
|
|
1590
|
-
*/
|
|
1591
1496
|
mapGoogleFinishReason(reason) {
|
|
1592
1497
|
const mapping = {
|
|
1593
1498
|
"STOP": "stop",
|
|
@@ -1596,89 +1501,85 @@ var GoogleProvider = class extends BaseProvider {
|
|
|
1596
1501
|
"RECITATION": "content_filter",
|
|
1597
1502
|
"OTHER": "stop"
|
|
1598
1503
|
};
|
|
1599
|
-
return mapping[reason] || "stop";
|
|
1504
|
+
return mapping[reason || "STOP"] || "stop";
|
|
1600
1505
|
}
|
|
1601
1506
|
};
|
|
1602
1507
|
|
|
1603
1508
|
// src/providers/openai/openai.ts
|
|
1509
|
+
var import_openai = __toESM(require("openai"));
|
|
1604
1510
|
var OpenAIProvider = class extends BaseProvider {
|
|
1605
|
-
|
|
1606
|
-
|
|
1511
|
+
constructor(config) {
|
|
1512
|
+
super(config);
|
|
1513
|
+
__publicField(this, "client");
|
|
1514
|
+
this.client = new import_openai.default({
|
|
1515
|
+
apiKey: this.config.apiKey,
|
|
1516
|
+
baseURL: this.config.baseUrl
|
|
1517
|
+
});
|
|
1607
1518
|
}
|
|
1608
1519
|
generateText(request) {
|
|
1609
1520
|
return __async(this, null, function* () {
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
presence_penalty: this.config.presencePenalty,
|
|
1622
|
-
stop: this.config.stop
|
|
1623
|
-
})
|
|
1624
|
-
}
|
|
1625
|
-
);
|
|
1626
|
-
const data = yield response.json();
|
|
1521
|
+
var _a, _b, _c;
|
|
1522
|
+
const completion = yield this.client.chat.completions.create({
|
|
1523
|
+
model: this.config.model,
|
|
1524
|
+
messages: this.formatMessages(request),
|
|
1525
|
+
max_tokens: this.config.maxTokens,
|
|
1526
|
+
temperature: this.config.temperature,
|
|
1527
|
+
top_p: this.config.topP,
|
|
1528
|
+
frequency_penalty: this.config.frequencyPenalty,
|
|
1529
|
+
presence_penalty: this.config.presencePenalty,
|
|
1530
|
+
stop: this.config.stop
|
|
1531
|
+
});
|
|
1627
1532
|
return {
|
|
1628
|
-
text:
|
|
1533
|
+
text: completion.choices[0].message.content || "",
|
|
1629
1534
|
usage: {
|
|
1630
|
-
promptTokens:
|
|
1631
|
-
completionTokens:
|
|
1632
|
-
totalTokens:
|
|
1535
|
+
promptTokens: ((_a = completion.usage) == null ? void 0 : _a.prompt_tokens) || 0,
|
|
1536
|
+
completionTokens: ((_b = completion.usage) == null ? void 0 : _b.completion_tokens) || 0,
|
|
1537
|
+
totalTokens: ((_c = completion.usage) == null ? void 0 : _c.total_tokens) || 0
|
|
1633
1538
|
},
|
|
1634
|
-
finishReason:
|
|
1539
|
+
finishReason: completion.choices[0].finish_reason
|
|
1635
1540
|
};
|
|
1636
1541
|
});
|
|
1637
1542
|
}
|
|
1638
1543
|
streamText(request) {
|
|
1639
1544
|
return __asyncGenerator(this, null, function* () {
|
|
1640
|
-
var _a, _b;
|
|
1641
|
-
const
|
|
1642
|
-
this.
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
max_tokens: this.config.maxTokens,
|
|
1649
|
-
temperature: this.config.temperature,
|
|
1650
|
-
stream: true
|
|
1651
|
-
})
|
|
1652
|
-
},
|
|
1653
|
-
true
|
|
1654
|
-
));
|
|
1655
|
-
const reader = response.body.getReader();
|
|
1656
|
-
const decoder = new TextDecoder();
|
|
1657
|
-
let buffer = "";
|
|
1545
|
+
var _a, _b, _c;
|
|
1546
|
+
const stream = yield new __await(this.client.chat.completions.create({
|
|
1547
|
+
model: this.config.model,
|
|
1548
|
+
messages: this.formatMessages(request),
|
|
1549
|
+
max_tokens: this.config.maxTokens,
|
|
1550
|
+
temperature: this.config.temperature,
|
|
1551
|
+
stream: true
|
|
1552
|
+
}));
|
|
1658
1553
|
try {
|
|
1659
|
-
|
|
1660
|
-
const
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
}
|
|
1675
|
-
} catch (e) {
|
|
1554
|
+
for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
1555
|
+
const chunk = temp.value;
|
|
1556
|
+
const content = (_b = (_a = chunk.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.content;
|
|
1557
|
+
if (content) {
|
|
1558
|
+
yield { type: "content", content };
|
|
1559
|
+
}
|
|
1560
|
+
if ((_c = chunk.choices[0]) == null ? void 0 : _c.finish_reason) {
|
|
1561
|
+
yield {
|
|
1562
|
+
type: "done",
|
|
1563
|
+
done: {
|
|
1564
|
+
finishReason: chunk.choices[0].finish_reason,
|
|
1565
|
+
usage: chunk.usage ? {
|
|
1566
|
+
promptTokens: chunk.usage.prompt_tokens || 0,
|
|
1567
|
+
completionTokens: chunk.usage.completion_tokens || 0,
|
|
1568
|
+
totalTokens: chunk.usage.total_tokens || 0
|
|
1569
|
+
} : void 0
|
|
1676
1570
|
}
|
|
1677
|
-
}
|
|
1571
|
+
};
|
|
1678
1572
|
}
|
|
1679
1573
|
}
|
|
1574
|
+
} catch (temp) {
|
|
1575
|
+
error = [temp];
|
|
1680
1576
|
} finally {
|
|
1681
|
-
|
|
1577
|
+
try {
|
|
1578
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
1579
|
+
} finally {
|
|
1580
|
+
if (error)
|
|
1581
|
+
throw error[0];
|
|
1582
|
+
}
|
|
1682
1583
|
}
|
|
1683
1584
|
});
|
|
1684
1585
|
}
|
|
@@ -1737,29 +1638,22 @@ var OpenAIProvider = class extends BaseProvider {
|
|
|
1737
1638
|
};
|
|
1738
1639
|
|
|
1739
1640
|
// src/providers/sarvam/sarvam.ts
|
|
1740
|
-
var SarvamProvider = class extends
|
|
1641
|
+
var SarvamProvider = class extends CustomBaseProvider {
|
|
1741
1642
|
getDefaultBaseUrl() {
|
|
1742
1643
|
return "https://api.sarvam.ai/v1";
|
|
1743
1644
|
}
|
|
1744
1645
|
generateText(request) {
|
|
1745
1646
|
return __async(this, null, function* () {
|
|
1746
1647
|
var _a, _b, _c;
|
|
1747
|
-
|
|
1648
|
+
this.validateConfig();
|
|
1748
1649
|
try {
|
|
1749
|
-
const response = yield this.makeRequest(
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
})),
|
|
1757
|
-
max_tokens: this.config.maxTokens,
|
|
1758
|
-
temperature: this.config.temperature,
|
|
1759
|
-
top_p: this.config.topP,
|
|
1760
|
-
stop: this.config.stop
|
|
1761
|
-
})
|
|
1762
|
-
});
|
|
1650
|
+
const response = yield this.makeRequest(
|
|
1651
|
+
this.getEndpoint("/chat/completions"),
|
|
1652
|
+
{
|
|
1653
|
+
method: "POST",
|
|
1654
|
+
body: JSON.stringify(this.buildRequestBody(request))
|
|
1655
|
+
}
|
|
1656
|
+
);
|
|
1763
1657
|
const data = yield response.json();
|
|
1764
1658
|
if (!data.choices || data.choices.length === 0) {
|
|
1765
1659
|
throw new ProviderError(
|
|
@@ -1776,7 +1670,7 @@ var SarvamProvider = class extends BaseProvider {
|
|
|
1776
1670
|
completionTokens: ((_b = data.usage) == null ? void 0 : _b.completion_tokens) || 0,
|
|
1777
1671
|
totalTokens: ((_c = data.usage) == null ? void 0 : _c.total_tokens) || 0
|
|
1778
1672
|
},
|
|
1779
|
-
finishReason: this.
|
|
1673
|
+
finishReason: this.mapFinishReason(data.choices[0].finish_reason)
|
|
1780
1674
|
};
|
|
1781
1675
|
} catch (error) {
|
|
1782
1676
|
throw new ProviderError(
|
|
@@ -1791,70 +1685,55 @@ var SarvamProvider = class extends BaseProvider {
|
|
|
1791
1685
|
streamText(request) {
|
|
1792
1686
|
return __asyncGenerator(this, null, function* () {
|
|
1793
1687
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1794
|
-
|
|
1688
|
+
this.validateConfig();
|
|
1795
1689
|
try {
|
|
1796
1690
|
const response = yield new __await(this.makeRequest(
|
|
1797
1691
|
this.getEndpoint("/chat/completions"),
|
|
1798
1692
|
{
|
|
1799
1693
|
method: "POST",
|
|
1800
|
-
body: JSON.stringify(
|
|
1801
|
-
model: this.config.model,
|
|
1802
|
-
messages: messages.map((m) => ({
|
|
1803
|
-
role: m.role,
|
|
1804
|
-
content: m.content
|
|
1805
|
-
})),
|
|
1806
|
-
max_tokens: this.config.maxTokens,
|
|
1807
|
-
temperature: this.config.temperature,
|
|
1808
|
-
stream: true
|
|
1809
|
-
})
|
|
1694
|
+
body: JSON.stringify(this.buildRequestBody(request, true))
|
|
1810
1695
|
},
|
|
1811
1696
|
true
|
|
1812
1697
|
));
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
yield { type: "content", content };
|
|
1831
|
-
}
|
|
1832
|
-
if ((_e = (_d = parsed.choices) == null ? void 0 : _d[0]) == null ? void 0 : _e.finish_reason) {
|
|
1833
|
-
yield {
|
|
1834
|
-
type: "done",
|
|
1835
|
-
done: {
|
|
1836
|
-
finishReason: this.mapSarvamFinishReason(parsed.choices[0].finish_reason),
|
|
1837
|
-
usage: {
|
|
1838
|
-
promptTokens: ((_f = parsed.usage) == null ? void 0 : _f.prompt_tokens) || 0,
|
|
1839
|
-
completionTokens: ((_g = parsed.usage) == null ? void 0 : _g.completion_tokens) || 0,
|
|
1840
|
-
totalTokens: ((_h = parsed.usage) == null ? void 0 : _h.total_tokens) || 0
|
|
1841
|
-
}
|
|
1842
|
-
}
|
|
1843
|
-
};
|
|
1698
|
+
try {
|
|
1699
|
+
for (var iter = __forAwait(this.parseSSEStream(response)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
1700
|
+
const data = temp.value;
|
|
1701
|
+
const content = (_c = (_b = (_a = data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.content;
|
|
1702
|
+
if (content) {
|
|
1703
|
+
yield { type: "content", content };
|
|
1704
|
+
}
|
|
1705
|
+
if ((_e = (_d = data.choices) == null ? void 0 : _d[0]) == null ? void 0 : _e.finish_reason) {
|
|
1706
|
+
yield {
|
|
1707
|
+
type: "done",
|
|
1708
|
+
done: {
|
|
1709
|
+
finishReason: this.mapFinishReason(data.choices[0].finish_reason),
|
|
1710
|
+
usage: {
|
|
1711
|
+
promptTokens: ((_f = data.usage) == null ? void 0 : _f.prompt_tokens) || 0,
|
|
1712
|
+
completionTokens: ((_g = data.usage) == null ? void 0 : _g.completion_tokens) || 0,
|
|
1713
|
+
totalTokens: ((_h = data.usage) == null ? void 0 : _h.total_tokens) || 0
|
|
1714
|
+
}
|
|
1844
1715
|
}
|
|
1845
|
-
}
|
|
1846
|
-
}
|
|
1716
|
+
};
|
|
1847
1717
|
}
|
|
1848
1718
|
}
|
|
1719
|
+
} catch (temp) {
|
|
1720
|
+
error = [temp];
|
|
1721
|
+
} finally {
|
|
1722
|
+
try {
|
|
1723
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
1724
|
+
} finally {
|
|
1725
|
+
if (error)
|
|
1726
|
+
throw error[0];
|
|
1727
|
+
}
|
|
1849
1728
|
}
|
|
1850
|
-
} catch (
|
|
1729
|
+
} catch (error2) {
|
|
1851
1730
|
yield {
|
|
1852
1731
|
type: "error",
|
|
1853
1732
|
error: new ProviderError(
|
|
1854
1733
|
"sarvam",
|
|
1855
|
-
|
|
1734
|
+
error2 instanceof Error ? error2.message : "Streaming error",
|
|
1856
1735
|
void 0,
|
|
1857
|
-
{ originalError:
|
|
1736
|
+
{ originalError: error2 }
|
|
1858
1737
|
)
|
|
1859
1738
|
};
|
|
1860
1739
|
}
|
|
@@ -1922,10 +1801,7 @@ var SarvamProvider = class extends BaseProvider {
|
|
|
1922
1801
|
}
|
|
1923
1802
|
});
|
|
1924
1803
|
}
|
|
1925
|
-
|
|
1926
|
-
* Map Sarvam's finish reason to standard format
|
|
1927
|
-
*/
|
|
1928
|
-
mapSarvamFinishReason(reason) {
|
|
1804
|
+
mapFinishReason(reason) {
|
|
1929
1805
|
const mapping = {
|
|
1930
1806
|
"stop": "stop",
|
|
1931
1807
|
"length": "length",
|