ag-common 0.0.907 → 0.0.908
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.
|
@@ -22,7 +22,7 @@ const getApiKeys = () => {
|
|
|
22
22
|
return (process.env.GOOGLE_API_KEY ?? "").split(",").filter(array_1.notEmpty);
|
|
23
23
|
};
|
|
24
24
|
// Get available key+service combinations
|
|
25
|
-
const getAvailableCombinations = (service) => {
|
|
25
|
+
const getAvailableCombinations = (service, resetIfExhausted = true) => {
|
|
26
26
|
const keys = getApiKeys();
|
|
27
27
|
(0, log_1.debug)(`got ${keys.length} GOOGLE_API_KEY keys`);
|
|
28
28
|
const combinations = [];
|
|
@@ -32,7 +32,7 @@ const getAvailableCombinations = (service) => {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
// If all combinations are blocklisted, clear the blocklist and try again
|
|
35
|
-
if (combinations.length === 0) {
|
|
35
|
+
if (combinations.length === 0 && resetIfExhausted) {
|
|
36
36
|
(0, log_1.warn)(`All API key + service combinations were blocklisted for ${service}. Clearing blocklist and trying again.`);
|
|
37
37
|
blocklist.flushAll();
|
|
38
38
|
for (const key of keys) {
|
|
@@ -45,7 +45,7 @@ const getAvailableCombinations = (service) => {
|
|
|
45
45
|
exports.getAvailableCombinations = getAvailableCombinations;
|
|
46
46
|
// Block a key+service combination
|
|
47
47
|
const blockKeyService = (apiKey, service) => {
|
|
48
|
-
(0, log_1.warn)(`key+service blocklisted
|
|
48
|
+
(0, log_1.warn)(`key+service blocklisted for ${service}`);
|
|
49
49
|
addToBlocklist(apiKey, service);
|
|
50
50
|
};
|
|
51
51
|
exports.blockKeyService = blockKeyService;
|
|
@@ -112,7 +112,7 @@ const getAvailableGeminiCombinations = async (prefer) => {
|
|
|
112
112
|
const combinations = [];
|
|
113
113
|
for (const [key, ai] of genAIs) {
|
|
114
114
|
for (const model of sortedModels) {
|
|
115
|
-
const keyServiceCombinations = (0, apikey_1.getAvailableCombinations)(`gemini-${model}
|
|
115
|
+
const keyServiceCombinations = (0, apikey_1.getAvailableCombinations)(`gemini-${model}`, false);
|
|
116
116
|
const isAvailable = keyServiceCombinations.some((k) => k.key === key);
|
|
117
117
|
if (isAvailable) {
|
|
118
118
|
combinations.push([key, ai, model]);
|
|
@@ -151,38 +151,54 @@ const geminiPromptDirect = async ({ prompt, images = [], ident, prefer, grounded
|
|
|
151
151
|
if (combinations.length === 0) {
|
|
152
152
|
throw new Error("No available API key and model combinations");
|
|
153
153
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
154
|
+
let lastFailure;
|
|
155
|
+
for (const [key, ai, selectedModel] of combinations) {
|
|
156
|
+
// Another concurrent prompt may have exhausted this combination already.
|
|
157
|
+
if (!(0, apikey_1.getAvailableCombinations)(`gemini-${selectedModel}`, false).some((entry) => entry.key === key))
|
|
158
|
+
continue;
|
|
159
|
+
// Prepare the request configuration
|
|
160
|
+
const requestConfig = {
|
|
161
|
+
model: selectedModel,
|
|
162
|
+
contents: [{ parts: [{ text: prompt }, ...parts] }],
|
|
163
|
+
config: {},
|
|
164
|
+
};
|
|
165
|
+
// Add grounded search configuration if enabled
|
|
166
|
+
if (groundedSearch) {
|
|
167
|
+
requestConfig.config.tools = [{ googleSearch: {} }];
|
|
168
|
+
}
|
|
169
|
+
(0, log_1.info)("gem query on:" + selectedModel, requestConfig);
|
|
170
|
+
try {
|
|
171
|
+
// oxlint-disable-next-line no-await-in-loop -- fallback depends on the previous model failing
|
|
172
|
+
const response = await ai.models.generateContent(requestConfig);
|
|
173
|
+
const rawtext = (response.text ?? "")
|
|
174
|
+
.replace(/```(json)?/gi, "")
|
|
175
|
+
.replace(/:[ ]+undefined/gim, ": null");
|
|
176
|
+
(0, log_1.info)("gem response");
|
|
177
|
+
(0, log_1.debug)("gem prompt:" + prompt, ident);
|
|
178
|
+
(0, log_1.debug)("gem response:" + rawtext);
|
|
179
|
+
(0, log_1.debug)("gem query usage:" + JSON.stringify(response.usageMetadata));
|
|
180
|
+
onGenerated?.({ model: response.modelVersion || selectedModel, generatedAt: Date.now() });
|
|
181
|
+
return rawtext;
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
const mod = `gemini-${selectedModel}`;
|
|
185
|
+
const status = e.status;
|
|
186
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
187
|
+
const unavailableModel = status === 404 && message.includes(selectedModel);
|
|
188
|
+
if (status === 429 || (0, retryOnError_1.isOverloadedApiKeyError)(e) || unavailableModel) {
|
|
189
|
+
(0, log_1.warn)("Gemini model attempt failed; trying next available combination", {
|
|
190
|
+
model: selectedModel,
|
|
191
|
+
status,
|
|
192
|
+
reason: unavailableModel ? "model unavailable" : "quota or capacity exhausted",
|
|
193
|
+
});
|
|
194
|
+
(0, apikey_1.blockKeyService)(key, mod);
|
|
195
|
+
lastFailure = e;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
throw e;
|
|
183
199
|
}
|
|
184
|
-
throw e;
|
|
185
200
|
}
|
|
201
|
+
throw lastFailure ?? new Error("No available API key and model combinations");
|
|
186
202
|
}, 1, 5000);
|
|
187
203
|
};
|
|
188
204
|
exports.geminiPromptDirect = geminiPromptDirect;
|