190proof 1.0.69 → 1.0.71
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 +100 -156
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +100 -156
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -54,6 +54,31 @@ var GeminiModel = /* @__PURE__ */ ((GeminiModel2) => {
|
|
|
54
54
|
return GeminiModel2;
|
|
55
55
|
})(GeminiModel || {});
|
|
56
56
|
|
|
57
|
+
// logger.ts
|
|
58
|
+
function formatIdentifier(identifier) {
|
|
59
|
+
if (Array.isArray(identifier)) {
|
|
60
|
+
return identifier.map((id) => `[${id}]`).join(" ");
|
|
61
|
+
}
|
|
62
|
+
return `[${identifier}]`;
|
|
63
|
+
}
|
|
64
|
+
function formatMessage(level, identifier, message) {
|
|
65
|
+
return `[${level}] ${formatIdentifier(identifier)} ${message}`;
|
|
66
|
+
}
|
|
67
|
+
function log(identifier, message, ...args) {
|
|
68
|
+
console.log(formatMessage("LOG", identifier, message), ...args);
|
|
69
|
+
}
|
|
70
|
+
function warn(identifier, message, ...args) {
|
|
71
|
+
console.warn(formatMessage("WARN", identifier, message), ...args);
|
|
72
|
+
}
|
|
73
|
+
function error(identifier, message, ...args) {
|
|
74
|
+
console.error(formatMessage("ERROR", identifier, message), ...args);
|
|
75
|
+
}
|
|
76
|
+
var logger_default = {
|
|
77
|
+
log,
|
|
78
|
+
warn,
|
|
79
|
+
error
|
|
80
|
+
};
|
|
81
|
+
|
|
57
82
|
// index.ts
|
|
58
83
|
import {
|
|
59
84
|
BedrockRuntimeClient,
|
|
@@ -89,13 +114,13 @@ function parseStreamedResponse(identifier, paragraph, functionCallName, function
|
|
|
89
114
|
name: functionCallName,
|
|
90
115
|
arguments: JSON.parse(functionCallArgs)
|
|
91
116
|
};
|
|
92
|
-
} catch (
|
|
93
|
-
|
|
94
|
-
throw
|
|
117
|
+
} catch (error2) {
|
|
118
|
+
logger_default.error(identifier, "Error parsing functionCallArgs:", functionCallArgs);
|
|
119
|
+
throw error2;
|
|
95
120
|
}
|
|
96
121
|
}
|
|
97
122
|
if (!paragraph && !functionCall) {
|
|
98
|
-
|
|
123
|
+
logger_default.error(
|
|
99
124
|
identifier,
|
|
100
125
|
"Stream error: received message without content or function_call, raw:",
|
|
101
126
|
JSON.stringify({ paragraph, functionCallName, functionCallArgs })
|
|
@@ -113,7 +138,7 @@ function parseStreamedResponse(identifier, paragraph, functionCallName, function
|
|
|
113
138
|
}
|
|
114
139
|
async function callOpenAiWithRetries(identifier, openAiPayload, openAiConfig, retries = 5, chunkTimeoutMs = 15e3) {
|
|
115
140
|
var _a, _b;
|
|
116
|
-
|
|
141
|
+
logger_default.log(
|
|
117
142
|
identifier,
|
|
118
143
|
"Calling OpenAI API with retries:",
|
|
119
144
|
openAiConfig == null ? void 0 : openAiConfig.service,
|
|
@@ -133,27 +158,15 @@ async function callOpenAiWithRetries(identifier, openAiPayload, openAiConfig, re
|
|
|
133
158
|
chunkTimeoutMs
|
|
134
159
|
);
|
|
135
160
|
}
|
|
136
|
-
} catch (
|
|
137
|
-
|
|
138
|
-
console.error(
|
|
161
|
+
} catch (error2) {
|
|
162
|
+
logger_default.error(
|
|
139
163
|
identifier,
|
|
140
|
-
`
|
|
164
|
+
`Retry #${i} error: ${error2.message}`,
|
|
165
|
+
((_a = error2.response) == null ? void 0 : _a.data) || error2.data || error2
|
|
141
166
|
);
|
|
142
|
-
const errorCode = (_b =
|
|
143
|
-
if (errorCode) {
|
|
144
|
-
console.error(
|
|
145
|
-
identifier,
|
|
146
|
-
`Retry #${i} failed with API error: ${errorCode}`,
|
|
147
|
-
JSON.stringify({
|
|
148
|
-
data: error.data
|
|
149
|
-
})
|
|
150
|
-
);
|
|
151
|
-
}
|
|
167
|
+
const errorCode = (_b = error2.data) == null ? void 0 : _b.code;
|
|
152
168
|
if (errorCode === "content_policy_violation") {
|
|
153
|
-
|
|
154
|
-
identifier,
|
|
155
|
-
`Removing images due to content policy violation error`
|
|
156
|
-
);
|
|
169
|
+
logger_default.log(identifier, "Removing images due to content policy violation error");
|
|
157
170
|
openAiPayload.messages.forEach((message) => {
|
|
158
171
|
if (Array.isArray(message.content)) {
|
|
159
172
|
message.content = message.content.filter(
|
|
@@ -163,34 +176,25 @@ async function callOpenAiWithRetries(identifier, openAiPayload, openAiConfig, re
|
|
|
163
176
|
});
|
|
164
177
|
}
|
|
165
178
|
if (i >= 2 && (openAiConfig == null ? void 0 : openAiConfig.service) === "azure" && errorCode === "content_filter") {
|
|
166
|
-
|
|
167
|
-
identifier,
|
|
168
|
-
`Switching to OpenAI service due to content filter error`
|
|
169
|
-
);
|
|
179
|
+
logger_default.log(identifier, "Switching to OpenAI service due to content filter error");
|
|
170
180
|
openAiConfig.service = "openai";
|
|
171
181
|
}
|
|
172
182
|
if (i === 3) {
|
|
173
183
|
if ((openAiConfig == null ? void 0 : openAiConfig.service) === "azure") {
|
|
174
|
-
|
|
175
|
-
identifier,
|
|
176
|
-
`Switching to OpenAI service due to Azure service error`
|
|
177
|
-
);
|
|
184
|
+
logger_default.log(identifier, "Switching to OpenAI service due to Azure service error");
|
|
178
185
|
openAiConfig.service = "openai";
|
|
179
186
|
}
|
|
180
187
|
}
|
|
181
188
|
if (i === 4) {
|
|
182
189
|
if (openAiPayload.tools) {
|
|
183
|
-
|
|
184
|
-
identifier,
|
|
185
|
-
`Switching to no tool choice due to persistent error`
|
|
186
|
-
);
|
|
190
|
+
logger_default.log(identifier, "Switching to no tool choice due to persistent error");
|
|
187
191
|
openAiPayload.tool_choice = "none";
|
|
188
192
|
}
|
|
189
193
|
}
|
|
190
194
|
await timeout(250);
|
|
191
195
|
}
|
|
192
196
|
}
|
|
193
|
-
|
|
197
|
+
logger_default.error(
|
|
194
198
|
identifier,
|
|
195
199
|
`Failed to call OpenAI API after ${retries} attempts. Please lookup OpenAI status for active issues.`,
|
|
196
200
|
errorObj
|
|
@@ -212,7 +216,7 @@ async function callOpenAIStream(identifier, openAiPayload, openAiConfig, chunkTi
|
|
|
212
216
|
let response;
|
|
213
217
|
const controller = new AbortController();
|
|
214
218
|
if (openAiConfig.service === "azure") {
|
|
215
|
-
|
|
219
|
+
logger_default.log(identifier, "Using Azure OpenAI service", openAiPayload.model);
|
|
216
220
|
const model = openAiPayload.model;
|
|
217
221
|
if (!openAiConfig.modelConfigMap) {
|
|
218
222
|
throw new Error(
|
|
@@ -226,19 +230,15 @@ async function callOpenAIStream(identifier, openAiPayload, openAiConfig, chunkTi
|
|
|
226
230
|
} else {
|
|
227
231
|
throw new Error("Azure OpenAI endpoint is required in modelConfigMap.");
|
|
228
232
|
}
|
|
229
|
-
|
|
233
|
+
logger_default.log(identifier, "Using endpoint", endpoint);
|
|
230
234
|
try {
|
|
231
235
|
const stringifiedPayload = JSON.stringify({
|
|
232
236
|
...openAiPayload,
|
|
233
237
|
stream: true
|
|
234
238
|
});
|
|
235
239
|
const parsedPayload = JSON.parse(stringifiedPayload);
|
|
236
|
-
} catch (
|
|
237
|
-
|
|
238
|
-
identifier,
|
|
239
|
-
"Stream error: Azure OpenAI JSON parsing error:",
|
|
240
|
-
JSON.stringify(error)
|
|
241
|
-
);
|
|
240
|
+
} catch (error2) {
|
|
241
|
+
logger_default.error(identifier, "Stream error: Azure OpenAI JSON parsing error:", error2);
|
|
242
242
|
}
|
|
243
243
|
response = await fetch(endpoint, {
|
|
244
244
|
method: "POST",
|
|
@@ -253,10 +253,10 @@ async function callOpenAIStream(identifier, openAiPayload, openAiConfig, chunkTi
|
|
|
253
253
|
signal: controller.signal
|
|
254
254
|
});
|
|
255
255
|
} else {
|
|
256
|
-
|
|
256
|
+
logger_default.log(identifier, "Using OpenAI service", openAiPayload.model);
|
|
257
257
|
const endpoint = `https://api.openai.com/v1/chat/completions`;
|
|
258
258
|
if (openAiConfig.orgId) {
|
|
259
|
-
|
|
259
|
+
logger_default.log(identifier, "Using orgId", openAiConfig.orgId);
|
|
260
260
|
}
|
|
261
261
|
response = await fetch(endpoint, {
|
|
262
262
|
method: "POST",
|
|
@@ -283,11 +283,7 @@ async function callOpenAIStream(identifier, openAiPayload, openAiConfig, chunkTi
|
|
|
283
283
|
const startAbortTimeout = () => {
|
|
284
284
|
abortTimeout && clearTimeout(abortTimeout);
|
|
285
285
|
return setTimeout(() => {
|
|
286
|
-
|
|
287
|
-
identifier,
|
|
288
|
-
`Stream error: aborted due to timeout after ${chunkTimeoutMs} ms.`,
|
|
289
|
-
JSON.stringify({ paragraph })
|
|
290
|
-
);
|
|
286
|
+
logger_default.error(identifier, `Stream timeout after ${chunkTimeoutMs}ms`);
|
|
291
287
|
controller.abort();
|
|
292
288
|
}, chunkTimeoutMs);
|
|
293
289
|
};
|
|
@@ -298,11 +294,7 @@ async function callOpenAIStream(identifier, openAiPayload, openAiConfig, chunkTi
|
|
|
298
294
|
const { done, value } = await reader.read();
|
|
299
295
|
clearTimeout(abortTimeout2);
|
|
300
296
|
if (done) {
|
|
301
|
-
|
|
302
|
-
identifier,
|
|
303
|
-
`Stream error: ended after ${chunkIndex + 1} chunks via reader done flag.`,
|
|
304
|
-
rawStreamedBody
|
|
305
|
-
);
|
|
297
|
+
logger_default.error(identifier, `Stream ended prematurely after ${chunkIndex + 1} chunks`);
|
|
306
298
|
throw new Error("Stream error: ended prematurely");
|
|
307
299
|
}
|
|
308
300
|
let chunk = new TextDecoder().decode(value);
|
|
@@ -317,10 +309,6 @@ async function callOpenAIStream(identifier, openAiPayload, openAiConfig, chunkTi
|
|
|
317
309
|
continue;
|
|
318
310
|
}
|
|
319
311
|
if (jsonString.includes("[DONE]")) {
|
|
320
|
-
console.log(
|
|
321
|
-
identifier,
|
|
322
|
-
`Stream explicitly marked as done after ${chunkIndex + 1} chunks.`
|
|
323
|
-
);
|
|
324
312
|
try {
|
|
325
313
|
return parseStreamedResponse(
|
|
326
314
|
identifier,
|
|
@@ -329,40 +317,29 @@ async function callOpenAIStream(identifier, openAiPayload, openAiConfig, chunkTi
|
|
|
329
317
|
functionCallArgs,
|
|
330
318
|
functionNames
|
|
331
319
|
);
|
|
332
|
-
} catch (
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
"Stream error: parsing response:",
|
|
336
|
-
rawStreamedBody
|
|
337
|
-
);
|
|
338
|
-
throw error;
|
|
320
|
+
} catch (error2) {
|
|
321
|
+
logger_default.error(identifier, "Stream error: parsing response");
|
|
322
|
+
throw error2;
|
|
339
323
|
}
|
|
340
324
|
}
|
|
341
325
|
let json;
|
|
342
326
|
try {
|
|
343
327
|
json = JSON.parse(jsonString.trim());
|
|
344
|
-
} catch (
|
|
328
|
+
} catch (error2) {
|
|
345
329
|
partialChunk = jsonString;
|
|
346
330
|
continue;
|
|
347
331
|
}
|
|
348
332
|
if (!json.choices || !json.choices.length) {
|
|
349
333
|
if (json.error) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
error
|
|
358
|
-
throw error;
|
|
334
|
+
logger_default.error(identifier, "Stream error: OpenAI error:", json.error);
|
|
335
|
+
const error2 = new Error("Stream error: OpenAI error");
|
|
336
|
+
error2.data = json.error;
|
|
337
|
+
error2.requestBody = truncatePayload(openAiPayload);
|
|
338
|
+
throw error2;
|
|
339
|
+
}
|
|
340
|
+
if (chunkIndex !== 0) {
|
|
341
|
+
logger_default.error(identifier, "Stream error: no choices in JSON:", json);
|
|
359
342
|
}
|
|
360
|
-
if (chunkIndex !== 0)
|
|
361
|
-
console.error(
|
|
362
|
-
identifier,
|
|
363
|
-
"Stream error: no choices in JSON:",
|
|
364
|
-
json
|
|
365
|
-
);
|
|
366
343
|
continue;
|
|
367
344
|
}
|
|
368
345
|
const dToolCall = (_d = (_c = (_b = (_a = json.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.tool_calls) == null ? void 0 : _d[0];
|
|
@@ -397,7 +374,7 @@ async function callOpenAI(identifier, openAiPayload, openAiConfig) {
|
|
|
397
374
|
}
|
|
398
375
|
let response;
|
|
399
376
|
if (openAiConfig.service === "azure") {
|
|
400
|
-
|
|
377
|
+
logger_default.log(identifier, "Using Azure OpenAI service", openAiPayload.model);
|
|
401
378
|
const model = openAiPayload.model;
|
|
402
379
|
if (!openAiConfig.modelConfigMap) {
|
|
403
380
|
throw new Error(
|
|
@@ -411,20 +388,16 @@ async function callOpenAI(identifier, openAiPayload, openAiConfig) {
|
|
|
411
388
|
} else {
|
|
412
389
|
throw new Error("Azure OpenAI endpoint is required in modelConfigMap.");
|
|
413
390
|
}
|
|
414
|
-
|
|
391
|
+
logger_default.log(identifier, "Using endpoint", endpoint);
|
|
415
392
|
try {
|
|
416
393
|
const stringifiedPayload = JSON.stringify({
|
|
417
394
|
...openAiPayload,
|
|
418
395
|
stream: false
|
|
419
396
|
});
|
|
420
397
|
const parsedPayload = JSON.parse(stringifiedPayload);
|
|
421
|
-
} catch (
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
"OpenAI JSON parsing error:",
|
|
425
|
-
JSON.stringify(error)
|
|
426
|
-
);
|
|
427
|
-
throw error;
|
|
398
|
+
} catch (error2) {
|
|
399
|
+
logger_default.error(identifier, "OpenAI JSON parsing error:", error2);
|
|
400
|
+
throw error2;
|
|
428
401
|
}
|
|
429
402
|
response = await fetch(endpoint, {
|
|
430
403
|
method: "POST",
|
|
@@ -438,10 +411,10 @@ async function callOpenAI(identifier, openAiPayload, openAiConfig) {
|
|
|
438
411
|
})
|
|
439
412
|
});
|
|
440
413
|
} else {
|
|
441
|
-
|
|
414
|
+
logger_default.log(identifier, "Using OpenAI service", openAiPayload.model);
|
|
442
415
|
const endpoint = `https://api.openai.com/v1/chat/completions`;
|
|
443
416
|
if (openAiConfig.orgId) {
|
|
444
|
-
|
|
417
|
+
logger_default.log(identifier, "Using orgId", openAiConfig.orgId);
|
|
445
418
|
}
|
|
446
419
|
response = await fetch(endpoint, {
|
|
447
420
|
method: "POST",
|
|
@@ -458,13 +431,13 @@ async function callOpenAI(identifier, openAiPayload, openAiConfig) {
|
|
|
458
431
|
}
|
|
459
432
|
if (!response.ok) {
|
|
460
433
|
const errorData = await response.json();
|
|
461
|
-
|
|
434
|
+
logger_default.error(identifier, "OpenAI API error:", errorData);
|
|
462
435
|
throw new Error(`OpenAI API Error: ${errorData.error.message}`);
|
|
463
436
|
}
|
|
464
437
|
const data = await response.json();
|
|
465
438
|
if (!data.choices || !data.choices.length) {
|
|
466
439
|
if (data.error) {
|
|
467
|
-
|
|
440
|
+
logger_default.error(identifier, "OpenAI error:", data.error);
|
|
468
441
|
throw new Error("OpenAI error: " + data.error.message);
|
|
469
442
|
}
|
|
470
443
|
throw new Error("OpenAI error: No choices returned.");
|
|
@@ -505,29 +478,28 @@ function truncatePayload(payload) {
|
|
|
505
478
|
}
|
|
506
479
|
async function callAnthropicWithRetries(identifier, AiPayload, AiConfig, attempts = 5) {
|
|
507
480
|
var _a, _b, _c, _d;
|
|
508
|
-
|
|
481
|
+
logger_default.log(identifier, "Calling Anthropic API with retries");
|
|
509
482
|
let lastResponse;
|
|
510
483
|
for (let i = 0; i < attempts; i++) {
|
|
511
484
|
try {
|
|
512
485
|
lastResponse = await callAnthropic(identifier, AiPayload, AiConfig);
|
|
513
486
|
return lastResponse;
|
|
514
487
|
} catch (e) {
|
|
515
|
-
|
|
516
|
-
console.error(
|
|
488
|
+
logger_default.error(
|
|
517
489
|
identifier,
|
|
518
|
-
`
|
|
519
|
-
|
|
490
|
+
`Retry #${i} error: ${e.message}`,
|
|
491
|
+
((_a = e.response) == null ? void 0 : _a.data) || e
|
|
520
492
|
);
|
|
521
493
|
if (((_d = (_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.error) == null ? void 0 : _d.type) === "rate_limit_error") {
|
|
522
494
|
}
|
|
523
495
|
await timeout(125 * i);
|
|
524
496
|
}
|
|
525
497
|
}
|
|
526
|
-
const
|
|
498
|
+
const error2 = new Error(
|
|
527
499
|
`Failed to call Anthropic API after ${attempts} attempts`
|
|
528
500
|
);
|
|
529
|
-
|
|
530
|
-
throw
|
|
501
|
+
error2.response = lastResponse;
|
|
502
|
+
throw error2;
|
|
531
503
|
}
|
|
532
504
|
async function callAnthropic(identifier, AiPayload, AiConfig) {
|
|
533
505
|
var _a, _b;
|
|
@@ -586,14 +558,14 @@ async function callAnthropic(identifier, AiPayload, AiConfig) {
|
|
|
586
558
|
}
|
|
587
559
|
const answers = data.content;
|
|
588
560
|
if (!answers[0]) {
|
|
589
|
-
|
|
561
|
+
logger_default.error(identifier, "Missing answer in Anthropic API:", data);
|
|
590
562
|
throw new Error("Missing answer in Anthropic API");
|
|
591
563
|
}
|
|
592
564
|
let textResponse = "";
|
|
593
565
|
let functionCalls = [];
|
|
594
566
|
for (const answer of answers) {
|
|
595
567
|
if (!answer.type) {
|
|
596
|
-
|
|
568
|
+
logger_default.error(identifier, "Missing answer type in Anthropic API:", data);
|
|
597
569
|
throw new Error("Missing answer type in Anthropic API");
|
|
598
570
|
}
|
|
599
571
|
let text = "";
|
|
@@ -604,7 +576,7 @@ async function callAnthropic(identifier, AiPayload, AiConfig) {
|
|
|
604
576
|
/<thinking>|<\/thinking>|<answer>|<\/answer>/gs,
|
|
605
577
|
""
|
|
606
578
|
);
|
|
607
|
-
|
|
579
|
+
logger_default.log(identifier, "No text in answer, returning text within tags:", text);
|
|
608
580
|
}
|
|
609
581
|
if (textResponse) {
|
|
610
582
|
textResponse += `
|
|
@@ -622,11 +594,7 @@ ${text}`;
|
|
|
622
594
|
}
|
|
623
595
|
}
|
|
624
596
|
if (!textResponse && !functionCalls.length) {
|
|
625
|
-
|
|
626
|
-
identifier,
|
|
627
|
-
"Missing text & fns in Anthropic API response:",
|
|
628
|
-
JSON.stringify(data)
|
|
629
|
-
);
|
|
597
|
+
logger_default.error(identifier, "Missing text & fns in Anthropic API response:", data);
|
|
630
598
|
throw new Error("Missing text & fns in Anthropic API response");
|
|
631
599
|
}
|
|
632
600
|
return {
|
|
@@ -736,9 +704,7 @@ async function prepareGoogleAIPayload(payload) {
|
|
|
736
704
|
}
|
|
737
705
|
for (const file of message.files || []) {
|
|
738
706
|
if (!((_a = file.mimeType) == null ? void 0 : _a.startsWith("image"))) {
|
|
739
|
-
|
|
740
|
-
"Google AI API does not support non-image file types. Skipping file."
|
|
741
|
-
);
|
|
707
|
+
logger_default.warn("payload", "Google AI API does not support non-image file types. Skipping file.");
|
|
742
708
|
continue;
|
|
743
709
|
}
|
|
744
710
|
if (file.url) {
|
|
@@ -776,9 +742,8 @@ async function prepareGoogleAIPayload(payload) {
|
|
|
776
742
|
}
|
|
777
743
|
async function callGoogleAI(identifier, payload) {
|
|
778
744
|
var _a, _b, _c;
|
|
779
|
-
|
|
745
|
+
logger_default.log(identifier, "Calling Google AI API");
|
|
780
746
|
const googleMessages = jigGoogleMessages(payload.messages);
|
|
781
|
-
console.log(identifier, "Google AI API messages:", googleMessages);
|
|
782
747
|
const history = googleMessages.slice(0, -1);
|
|
783
748
|
const lastMessage = googleMessages.slice(-1)[0];
|
|
784
749
|
const genAI = new GoogleGenAI({
|
|
@@ -821,11 +786,7 @@ async function callGoogleAI(identifier, payload) {
|
|
|
821
786
|
};
|
|
822
787
|
});
|
|
823
788
|
if (!text && !(parsedFunctionCalls == null ? void 0 : parsedFunctionCalls.length) && !files.length) {
|
|
824
|
-
|
|
825
|
-
identifier,
|
|
826
|
-
"Missing text & fns in Google AI API response:",
|
|
827
|
-
response
|
|
828
|
-
);
|
|
789
|
+
logger_default.error(identifier, "Missing text & fns in Google AI API response:", response);
|
|
829
790
|
throw new Error("Missing text & fns in Google AI API response");
|
|
830
791
|
}
|
|
831
792
|
return {
|
|
@@ -836,32 +797,26 @@ async function callGoogleAI(identifier, payload) {
|
|
|
836
797
|
};
|
|
837
798
|
}
|
|
838
799
|
async function callGoogleAIWithRetries(identifier, payload, retries = 5) {
|
|
839
|
-
|
|
800
|
+
logger_default.log(identifier, "Calling Google AI API with retries");
|
|
840
801
|
let lastError;
|
|
841
802
|
for (let i = 0; i < retries; i++) {
|
|
842
803
|
try {
|
|
843
804
|
return await callGoogleAI(identifier, payload);
|
|
844
805
|
} catch (e) {
|
|
845
806
|
lastError = e;
|
|
846
|
-
|
|
847
|
-
console.error(
|
|
848
|
-
identifier,
|
|
849
|
-
`Retrying due to error: received bad response from Google AI API: ${e.message}`,
|
|
850
|
-
JSON.stringify(e)
|
|
851
|
-
// Google AI errors might not have a response.data structure like others
|
|
852
|
-
);
|
|
807
|
+
logger_default.error(identifier, `Retry #${i} error: ${e.message}`, e);
|
|
853
808
|
await timeout(125 * i);
|
|
854
809
|
}
|
|
855
810
|
}
|
|
856
|
-
const
|
|
811
|
+
const error2 = new Error(
|
|
857
812
|
`Failed to call Google AI API after ${retries} attempts`
|
|
858
813
|
);
|
|
859
|
-
|
|
860
|
-
throw
|
|
814
|
+
error2.cause = lastError;
|
|
815
|
+
throw error2;
|
|
861
816
|
}
|
|
862
817
|
async function callWithRetries(identifier, aiPayload, aiConfig, retries = 5, chunkTimeoutMs = 15e3) {
|
|
863
818
|
if (isAnthropicPayload(aiPayload)) {
|
|
864
|
-
|
|
819
|
+
logger_default.log(identifier, "Delegating call to Anthropic API");
|
|
865
820
|
return await callAnthropicWithRetries(
|
|
866
821
|
identifier,
|
|
867
822
|
await prepareAnthropicPayload(aiPayload),
|
|
@@ -869,7 +824,7 @@ async function callWithRetries(identifier, aiPayload, aiConfig, retries = 5, chu
|
|
|
869
824
|
retries
|
|
870
825
|
);
|
|
871
826
|
} else if (isOpenAiPayload(aiPayload)) {
|
|
872
|
-
|
|
827
|
+
logger_default.log(identifier, "Delegating call to OpenAI API");
|
|
873
828
|
return await callOpenAiWithRetries(
|
|
874
829
|
identifier,
|
|
875
830
|
await prepareOpenAIPayload(aiPayload),
|
|
@@ -878,13 +833,13 @@ async function callWithRetries(identifier, aiPayload, aiConfig, retries = 5, chu
|
|
|
878
833
|
chunkTimeoutMs
|
|
879
834
|
);
|
|
880
835
|
} else if (isGroqPayload(aiPayload)) {
|
|
881
|
-
|
|
836
|
+
logger_default.log(identifier, "Delegating call to Groq API");
|
|
882
837
|
return await callGroqWithRetries(
|
|
883
838
|
identifier,
|
|
884
839
|
await prepareGroqPayload(aiPayload)
|
|
885
840
|
);
|
|
886
841
|
} else if (isGoogleAIPayload(aiPayload)) {
|
|
887
|
-
|
|
842
|
+
logger_default.log(identifier, "Delegating call to Google AI API");
|
|
888
843
|
return await callGoogleAIWithRetries(
|
|
889
844
|
identifier,
|
|
890
845
|
await prepareGoogleAIPayload(aiPayload),
|
|
@@ -919,9 +874,7 @@ async function prepareAnthropicPayload(payload) {
|
|
|
919
874
|
}
|
|
920
875
|
for (const file of message.files || []) {
|
|
921
876
|
if (!((_a = file.mimeType) == null ? void 0 : _a.startsWith("image"))) {
|
|
922
|
-
|
|
923
|
-
"Anthropic API does not support non-image file types. Skipping file."
|
|
924
|
-
);
|
|
877
|
+
logger_default.warn("payload", "Anthropic API does not support non-image file types. Skipping file.");
|
|
925
878
|
continue;
|
|
926
879
|
}
|
|
927
880
|
if (file.url) {
|
|
@@ -1011,10 +964,7 @@ async function prepareOpenAIPayload(payload) {
|
|
|
1011
964
|
});
|
|
1012
965
|
}
|
|
1013
966
|
} else {
|
|
1014
|
-
|
|
1015
|
-
"Skipping file in message. File or image type not supported by OpenAI API:",
|
|
1016
|
-
file.mimeType
|
|
1017
|
-
);
|
|
967
|
+
logger_default.warn("payload", "Skipping file in message. File or image type not supported by OpenAI API:", file.mimeType);
|
|
1018
968
|
}
|
|
1019
969
|
}
|
|
1020
970
|
preparedPayload.messages.push({
|
|
@@ -1066,7 +1016,7 @@ async function callGroq(identifier, payload) {
|
|
|
1066
1016
|
const data = response.data;
|
|
1067
1017
|
const answer = data.choices[0].message;
|
|
1068
1018
|
if (!answer) {
|
|
1069
|
-
|
|
1019
|
+
logger_default.error(identifier, "Missing answer in Groq API:", data);
|
|
1070
1020
|
throw new Error("Missing answer in Groq API");
|
|
1071
1021
|
}
|
|
1072
1022
|
const textResponse = answer.content || null;
|
|
@@ -1087,30 +1037,24 @@ async function callGroq(identifier, payload) {
|
|
|
1087
1037
|
}
|
|
1088
1038
|
async function callGroqWithRetries(identifier, payload, retries = 5) {
|
|
1089
1039
|
var _a;
|
|
1090
|
-
|
|
1040
|
+
logger_default.log(identifier, "Calling Groq API with retries");
|
|
1091
1041
|
let lastResponse;
|
|
1092
1042
|
for (let i = 0; i < retries; i++) {
|
|
1093
1043
|
try {
|
|
1094
1044
|
lastResponse = await callGroq(identifier, payload);
|
|
1095
1045
|
return lastResponse;
|
|
1096
1046
|
} catch (e) {
|
|
1097
|
-
|
|
1098
|
-
console.error(
|
|
1099
|
-
identifier,
|
|
1100
|
-
`Retrying due to error: received bad response from Groq API: ${e.message}`,
|
|
1101
|
-
JSON.stringify((_a = e.response) == null ? void 0 : _a.data)
|
|
1102
|
-
);
|
|
1047
|
+
logger_default.error(identifier, `Retry #${i} error: ${e.message}`, ((_a = e.response) == null ? void 0 : _a.data) || e);
|
|
1103
1048
|
await timeout(125 * i);
|
|
1104
1049
|
}
|
|
1105
1050
|
}
|
|
1106
|
-
const
|
|
1051
|
+
const error2 = new Error(
|
|
1107
1052
|
`Failed to call Groq API after ${retries} attempts`
|
|
1108
1053
|
);
|
|
1109
|
-
|
|
1110
|
-
throw
|
|
1054
|
+
error2.response = lastResponse;
|
|
1055
|
+
throw error2;
|
|
1111
1056
|
}
|
|
1112
1057
|
async function getNormalizedBase64PNG(url, mime) {
|
|
1113
|
-
console.log("Normalizing image", url);
|
|
1114
1058
|
const response = await axios.get(url, { responseType: "arraybuffer" });
|
|
1115
1059
|
let imageBuffer = Buffer.from(response.data);
|
|
1116
1060
|
let sharpOptions = {};
|