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