@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.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
- makeRequest(url, options, stream = false) {
915
- return __async(this, null, function* () {
916
- const headers = __spreadValues({
917
- "Content-Type": "application/json"
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
- getEndpoint(path) {
941
- const baseUrl = this.config.baseUrl || this.getDefaultBaseUrl();
942
- return `${baseUrl}${path}`;
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
- getDefaultBaseUrl() {
949
- return "https://api.anthropic.com/v1";
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.makeRequest(
958
- this.getEndpoint("/messages"),
959
- {
960
- method: "POST",
961
- headers: {
962
- "anthropic-version": "2023-06-01"
963
- },
964
- body: JSON.stringify({
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: data.content[0].text,
934
+ text: (textContent == null ? void 0 : textContent.type) === "text" ? textContent.text : "",
976
935
  usage: {
977
- promptTokens: data.usage.input_tokens,
978
- completionTokens: data.usage.output_tokens,
979
- totalTokens: data.usage.input_tokens + data.usage.output_tokens
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: data.stop_reason
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 response = yield new __await(this.makeRequest(
992
- this.getEndpoint("/messages"),
993
- {
994
- method: "POST",
995
- headers: {
996
- "anthropic-version": "2023-06-01"
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
- while (true) {
1013
- const { done, value } = yield new __await(reader.read());
1014
- if (done) break;
1015
- buffer += decoder.decode(value, { stream: true });
1016
- const lines = buffer.split("\n");
1017
- buffer = lines.pop() || "";
1018
- for (const line of lines) {
1019
- if (line.startsWith("data: ")) {
1020
- try {
1021
- const data = JSON.parse(line.slice(6));
1022
- if (data.type === "content_block_delta") {
1023
- yield { type: "content", content: data.delta.text };
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
- reader.releaseLock();
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/custom.ts
1090
- var CustomProvider = class extends BaseProvider {
1091
- constructor() {
1092
- super(...arguments);
1093
- /**
1094
- * Request format for custom provider
1095
- */
1096
- __publicField(this, "requestFormat", "openai");
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
- getDefaultBaseUrl() {
1099
- return this.config.baseUrl || "http://localhost:8000/v1";
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
- * Set request format for custom provider
1106
+ * Build request body with common parameters
1103
1107
  */
1104
- setRequestFormat(format) {
1105
- this.requestFormat = format;
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
- const messages = this.formatMessages(request);
1138
+ var _a, _b, _c;
1139
+ this.validateConfig();
1110
1140
  try {
1111
- const requestBody = this.formatRequestBody(messages, false);
1112
- const response = yield this.makeRequest(this.getEndpoint("/chat/completions"), {
1113
- method: "POST",
1114
- body: JSON.stringify(requestBody)
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
- return this.parseTextResponse(data);
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
- const messages = this.formatMessages(request);
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(requestBody)
1185
+ body: JSON.stringify(this.buildRequestBody(request, true))
1138
1186
  },
1139
1187
  true
1140
1188
  ));
1141
- yield* __yieldStar(this.parseStreamResponse(response));
1142
- } catch (error) {
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
- error instanceof Error ? error.message : "Streaming error",
1225
+ error2 instanceof Error ? error2.message : "Streaming error",
1148
1226
  void 0,
1149
- { originalError: error }
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)}. Only return the JSON object.`
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(jsonText);
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(jsonText2);
1271
+ const partial = JSON.parse(accumulatedText);
1204
1272
  yield { type: "partial", partial, text: accumulatedText };
1205
1273
  } catch (e) {
1206
1274
  }
@@ -1216,203 +1284,70 @@ 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(jsonText);
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
- getDefaultBaseUrl() {
1359
- return "https://generativelanguage.googleapis.com/v1beta";
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" : msg.role,
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 response = yield this.makeRequest(endpoint, {
1381
- method: "POST",
1382
- body: JSON.stringify({
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 ? {
1334
+ role: "system",
1335
+ parts: [{ text: systemPrompt }]
1336
+ } : void 0
1394
1337
  });
1395
- const data = yield response.json();
1396
- if (!data.candidates || data.candidates.length === 0) {
1397
- throw new ProviderError(
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("");
1338
+ const lastMessage = conversationMessages[conversationMessages.length - 1];
1339
+ const result = yield chat.sendMessage(lastMessage.content);
1340
+ const response = result.response;
1406
1341
  return {
1407
- text,
1342
+ text: response.text(),
1408
1343
  usage: {
1409
- promptTokens: ((_b = data.usageMetadata) == null ? void 0 : _b.promptTokenCount) || 0,
1410
- completionTokens: ((_c = data.usageMetadata) == null ? void 0 : _c.candidatesTokenCount) || 0,
1411
- totalTokens: ((_d = data.usageMetadata) == null ? void 0 : _d.totalTokenCount) || 0
1344
+ promptTokens: ((_b = response.usageMetadata) == null ? void 0 : _b.promptTokenCount) || 0,
1345
+ completionTokens: ((_c = response.usageMetadata) == null ? void 0 : _c.candidatesTokenCount) || 0,
1346
+ totalTokens: ((_d = response.usageMetadata) == null ? void 0 : _d.totalTokenCount) || 0
1412
1347
  },
1413
- finishReason: this.mapGoogleFinishReason(candidate.finishReason),
1348
+ finishReason: this.mapGoogleFinishReason((_f = (_e = response.candidates) == null ? void 0 : _e[0]) == null ? void 0 : _f.finishReason),
1414
1349
  metadata: {
1415
- safetyRatings: candidate.safetyRatings
1350
+ safetyRatings: (_h = (_g = response.candidates) == null ? void 0 : _g[0]) == null ? void 0 : _h.safetyRatings
1416
1351
  }
1417
1352
  };
1418
1353
  } catch (error) {
@@ -1427,79 +1362,55 @@ var GoogleProvider = class extends BaseProvider {
1427
1362
  }
1428
1363
  streamText(request) {
1429
1364
  return __asyncGenerator(this, null, function* () {
1430
- var _a, _b, _c, _d, _e, _f, _g;
1365
+ var _a, _b, _c, _d, _e, _f;
1431
1366
  const messages = this.formatMessages(request);
1432
1367
  const systemPrompt = (_a = messages.find((m) => m.role === "system")) == null ? void 0 : _a.content;
1433
1368
  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
1369
  try {
1438
- const response = yield new __await(this.makeRequest(
1439
- endpoint,
1440
- {
1441
- method: "POST",
1442
- body: JSON.stringify({
1443
- contents: this.formatGoogleMessages(conversationMessages),
1444
- systemInstruction: systemPrompt ? {
1445
- parts: [{ text: systemPrompt }]
1446
- } : void 0,
1447
- generationConfig: {
1448
- maxOutputTokens: this.config.maxTokens,
1449
- temperature: this.config.temperature,
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);
1370
+ const chat = this.model.startChat({
1371
+ history: this.formatGoogleMessages(conversationMessages.slice(0, -1)),
1372
+ systemInstruction: systemPrompt
1373
+ });
1374
+ const lastMessage = conversationMessages[conversationMessages.length - 1];
1375
+ const result = yield new __await(chat.sendMessageStream(lastMessage.content));
1376
+ try {
1377
+ for (var iter = __forAwait(result.stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
1378
+ const chunk = temp.value;
1379
+ const text = chunk.text();
1380
+ if (text) {
1381
+ yield { type: "content", content: text };
1492
1382
  }
1493
1383
  }
1384
+ } catch (temp) {
1385
+ error = [temp];
1386
+ } finally {
1387
+ try {
1388
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
1389
+ } finally {
1390
+ if (error)
1391
+ throw error[0];
1392
+ }
1494
1393
  }
1495
- } catch (error) {
1394
+ const response = yield new __await(result.response);
1395
+ yield {
1396
+ type: "done",
1397
+ done: {
1398
+ finishReason: this.mapGoogleFinishReason((_c = (_b = response.candidates) == null ? void 0 : _b[0]) == null ? void 0 : _c.finishReason),
1399
+ usage: {
1400
+ promptTokens: ((_d = response.usageMetadata) == null ? void 0 : _d.promptTokenCount) || 0,
1401
+ completionTokens: ((_e = response.usageMetadata) == null ? void 0 : _e.candidatesTokenCount) || 0,
1402
+ totalTokens: ((_f = response.usageMetadata) == null ? void 0 : _f.totalTokenCount) || 0
1403
+ }
1404
+ }
1405
+ };
1406
+ } catch (error2) {
1496
1407
  yield {
1497
1408
  type: "error",
1498
1409
  error: new ProviderError(
1499
1410
  "google",
1500
- error instanceof Error ? error.message : "Streaming error",
1411
+ error2 instanceof Error ? error2.message : "Streaming error",
1501
1412
  void 0,
1502
- { originalError: error }
1413
+ { originalError: error2 }
1503
1414
  )
1504
1415
  };
1505
1416
  }
@@ -1585,9 +1496,6 @@ var GoogleProvider = class extends BaseProvider {
1585
1496
  }
1586
1497
  });
1587
1498
  }
1588
- /**
1589
- * Map Google's finish reason to standard format
1590
- */
1591
1499
  mapGoogleFinishReason(reason) {
1592
1500
  const mapping = {
1593
1501
  "STOP": "stop",
@@ -1596,89 +1504,85 @@ var GoogleProvider = class extends BaseProvider {
1596
1504
  "RECITATION": "content_filter",
1597
1505
  "OTHER": "stop"
1598
1506
  };
1599
- return mapping[reason] || "stop";
1507
+ return mapping[reason || "STOP"] || "stop";
1600
1508
  }
1601
1509
  };
1602
1510
 
1603
1511
  // src/providers/openai/openai.ts
1512
+ var import_openai = __toESM(require("openai"));
1604
1513
  var OpenAIProvider = class extends BaseProvider {
1605
- getDefaultBaseUrl() {
1606
- return "https://api.openai.com/v1";
1514
+ constructor(config) {
1515
+ super(config);
1516
+ __publicField(this, "client");
1517
+ this.client = new import_openai.default({
1518
+ apiKey: this.config.apiKey,
1519
+ baseURL: this.config.baseUrl
1520
+ });
1607
1521
  }
1608
1522
  generateText(request) {
1609
1523
  return __async(this, null, function* () {
1610
- const response = yield this.makeRequest(
1611
- this.getEndpoint("/chat/completions"),
1612
- {
1613
- method: "POST",
1614
- body: JSON.stringify({
1615
- model: this.config.model,
1616
- messages: this.formatMessages(request),
1617
- max_tokens: this.config.maxTokens,
1618
- temperature: this.config.temperature,
1619
- top_p: this.config.topP,
1620
- frequency_penalty: this.config.frequencyPenalty,
1621
- presence_penalty: this.config.presencePenalty,
1622
- stop: this.config.stop
1623
- })
1624
- }
1625
- );
1626
- const data = yield response.json();
1524
+ var _a, _b, _c;
1525
+ const completion = yield this.client.chat.completions.create({
1526
+ model: this.config.model,
1527
+ messages: this.formatMessages(request),
1528
+ max_tokens: this.config.maxTokens,
1529
+ temperature: this.config.temperature,
1530
+ top_p: this.config.topP,
1531
+ frequency_penalty: this.config.frequencyPenalty,
1532
+ presence_penalty: this.config.presencePenalty,
1533
+ stop: this.config.stop
1534
+ });
1627
1535
  return {
1628
- text: data.choices[0].message.content,
1536
+ text: completion.choices[0].message.content || "",
1629
1537
  usage: {
1630
- promptTokens: data.usage.prompt_tokens,
1631
- completionTokens: data.usage.completion_tokens,
1632
- totalTokens: data.usage.total_tokens
1538
+ promptTokens: ((_a = completion.usage) == null ? void 0 : _a.prompt_tokens) || 0,
1539
+ completionTokens: ((_b = completion.usage) == null ? void 0 : _b.completion_tokens) || 0,
1540
+ totalTokens: ((_c = completion.usage) == null ? void 0 : _c.total_tokens) || 0
1633
1541
  },
1634
- finishReason: data.choices[0].finish_reason
1542
+ finishReason: completion.choices[0].finish_reason
1635
1543
  };
1636
1544
  });
1637
1545
  }
1638
1546
  streamText(request) {
1639
1547
  return __asyncGenerator(this, null, function* () {
1640
- var _a, _b;
1641
- const response = yield new __await(this.makeRequest(
1642
- this.getEndpoint("/chat/completions"),
1643
- {
1644
- method: "POST",
1645
- body: JSON.stringify({
1646
- model: this.config.model,
1647
- messages: this.formatMessages(request),
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 = "";
1548
+ var _a, _b, _c;
1549
+ const stream = yield new __await(this.client.chat.completions.create({
1550
+ model: this.config.model,
1551
+ messages: this.formatMessages(request),
1552
+ max_tokens: this.config.maxTokens,
1553
+ temperature: this.config.temperature,
1554
+ stream: true
1555
+ }));
1658
1556
  try {
1659
- while (true) {
1660
- const { done, value } = yield new __await(reader.read());
1661
- if (done) break;
1662
- buffer += decoder.decode(value, { stream: true });
1663
- const lines = buffer.split("\n");
1664
- buffer = lines.pop() || "";
1665
- for (const line of lines) {
1666
- if (line.startsWith("data: ")) {
1667
- const data = line.slice(6);
1668
- if (data === "[DONE]") continue;
1669
- try {
1670
- const parsed = JSON.parse(data);
1671
- const content = (_b = (_a = parsed.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.content;
1672
- if (content) {
1673
- yield { type: "content", content };
1674
- }
1675
- } catch (e) {
1557
+ for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
1558
+ const chunk = temp.value;
1559
+ const content = (_b = (_a = chunk.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.content;
1560
+ if (content) {
1561
+ yield { type: "content", content };
1562
+ }
1563
+ if ((_c = chunk.choices[0]) == null ? void 0 : _c.finish_reason) {
1564
+ yield {
1565
+ type: "done",
1566
+ done: {
1567
+ finishReason: chunk.choices[0].finish_reason,
1568
+ usage: chunk.usage ? {
1569
+ promptTokens: chunk.usage.prompt_tokens || 0,
1570
+ completionTokens: chunk.usage.completion_tokens || 0,
1571
+ totalTokens: chunk.usage.total_tokens || 0
1572
+ } : void 0
1676
1573
  }
1677
- }
1574
+ };
1678
1575
  }
1679
1576
  }
1577
+ } catch (temp) {
1578
+ error = [temp];
1680
1579
  } finally {
1681
- reader.releaseLock();
1580
+ try {
1581
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
1582
+ } finally {
1583
+ if (error)
1584
+ throw error[0];
1585
+ }
1682
1586
  }
1683
1587
  });
1684
1588
  }
@@ -1737,29 +1641,22 @@ var OpenAIProvider = class extends BaseProvider {
1737
1641
  };
1738
1642
 
1739
1643
  // src/providers/sarvam/sarvam.ts
1740
- var SarvamProvider = class extends BaseProvider {
1644
+ var SarvamProvider = class extends CustomBaseProvider {
1741
1645
  getDefaultBaseUrl() {
1742
1646
  return "https://api.sarvam.ai/v1";
1743
1647
  }
1744
1648
  generateText(request) {
1745
1649
  return __async(this, null, function* () {
1746
1650
  var _a, _b, _c;
1747
- const messages = this.formatMessages(request);
1651
+ this.validateConfig();
1748
1652
  try {
1749
- const response = yield this.makeRequest(this.getEndpoint("/chat/completions"), {
1750
- method: "POST",
1751
- body: JSON.stringify({
1752
- model: this.config.model,
1753
- messages: messages.map((m) => ({
1754
- role: m.role,
1755
- content: m.content
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
- });
1653
+ const response = yield this.makeRequest(
1654
+ this.getEndpoint("/chat/completions"),
1655
+ {
1656
+ method: "POST",
1657
+ body: JSON.stringify(this.buildRequestBody(request))
1658
+ }
1659
+ );
1763
1660
  const data = yield response.json();
1764
1661
  if (!data.choices || data.choices.length === 0) {
1765
1662
  throw new ProviderError(
@@ -1776,7 +1673,7 @@ var SarvamProvider = class extends BaseProvider {
1776
1673
  completionTokens: ((_b = data.usage) == null ? void 0 : _b.completion_tokens) || 0,
1777
1674
  totalTokens: ((_c = data.usage) == null ? void 0 : _c.total_tokens) || 0
1778
1675
  },
1779
- finishReason: this.mapSarvamFinishReason(data.choices[0].finish_reason)
1676
+ finishReason: this.mapFinishReason(data.choices[0].finish_reason)
1780
1677
  };
1781
1678
  } catch (error) {
1782
1679
  throw new ProviderError(
@@ -1791,70 +1688,55 @@ var SarvamProvider = class extends BaseProvider {
1791
1688
  streamText(request) {
1792
1689
  return __asyncGenerator(this, null, function* () {
1793
1690
  var _a, _b, _c, _d, _e, _f, _g, _h;
1794
- const messages = this.formatMessages(request);
1691
+ this.validateConfig();
1795
1692
  try {
1796
1693
  const response = yield new __await(this.makeRequest(
1797
1694
  this.getEndpoint("/chat/completions"),
1798
1695
  {
1799
1696
  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
- })
1697
+ body: JSON.stringify(this.buildRequestBody(request, true))
1810
1698
  },
1811
1699
  true
1812
1700
  ));
1813
- const reader = response.body.getReader();
1814
- const decoder = new TextDecoder();
1815
- let buffer = "";
1816
- while (true) {
1817
- const { done, value } = yield new __await(reader.read());
1818
- if (done) break;
1819
- buffer += decoder.decode(value, { stream: true });
1820
- const lines = buffer.split("\n");
1821
- buffer = lines.pop() || "";
1822
- for (const line of lines) {
1823
- if (line.startsWith("data: ")) {
1824
- const data = line.slice(6);
1825
- if (data === "[DONE]") continue;
1826
- try {
1827
- const parsed = JSON.parse(data);
1828
- const content = (_c = (_b = (_a = parsed.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.content;
1829
- if (content) {
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
- };
1701
+ try {
1702
+ for (var iter = __forAwait(this.parseSSEStream(response)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
1703
+ const data = temp.value;
1704
+ const content = (_c = (_b = (_a = data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.content;
1705
+ if (content) {
1706
+ yield { type: "content", content };
1707
+ }
1708
+ if ((_e = (_d = data.choices) == null ? void 0 : _d[0]) == null ? void 0 : _e.finish_reason) {
1709
+ yield {
1710
+ type: "done",
1711
+ done: {
1712
+ finishReason: this.mapFinishReason(data.choices[0].finish_reason),
1713
+ usage: {
1714
+ promptTokens: ((_f = data.usage) == null ? void 0 : _f.prompt_tokens) || 0,
1715
+ completionTokens: ((_g = data.usage) == null ? void 0 : _g.completion_tokens) || 0,
1716
+ totalTokens: ((_h = data.usage) == null ? void 0 : _h.total_tokens) || 0
1717
+ }
1844
1718
  }
1845
- } catch (e) {
1846
- }
1719
+ };
1847
1720
  }
1848
1721
  }
1722
+ } catch (temp) {
1723
+ error = [temp];
1724
+ } finally {
1725
+ try {
1726
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
1727
+ } finally {
1728
+ if (error)
1729
+ throw error[0];
1730
+ }
1849
1731
  }
1850
- } catch (error) {
1732
+ } catch (error2) {
1851
1733
  yield {
1852
1734
  type: "error",
1853
1735
  error: new ProviderError(
1854
1736
  "sarvam",
1855
- error instanceof Error ? error.message : "Streaming error",
1737
+ error2 instanceof Error ? error2.message : "Streaming error",
1856
1738
  void 0,
1857
- { originalError: error }
1739
+ { originalError: error2 }
1858
1740
  )
1859
1741
  };
1860
1742
  }
@@ -1922,10 +1804,7 @@ var SarvamProvider = class extends BaseProvider {
1922
1804
  }
1923
1805
  });
1924
1806
  }
1925
- /**
1926
- * Map Sarvam's finish reason to standard format
1927
- */
1928
- mapSarvamFinishReason(reason) {
1807
+ mapFinishReason(reason) {
1929
1808
  const mapping = {
1930
1809
  "stop": "stop",
1931
1810
  "length": "length",