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.
@@ -1,4 +1,4 @@
1
- export declare const getAvailableCombinations: (service: string) => {
1
+ export declare const getAvailableCombinations: (service: string, resetIfExhausted?: boolean) => {
2
2
  service: string;
3
3
  key: string;
4
4
  }[];
@@ -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: ${(0, truncate_1.truncate)(apiKey, 10)}_${service}`);
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
- const [key, ai, selectedModel] = combinations[0];
155
- // Prepare the request configuration
156
- const requestConfig = {
157
- model: selectedModel,
158
- contents: [{ parts: [{ text: prompt }, ...parts] }],
159
- config: {},
160
- };
161
- // Add grounded search configuration if enabled
162
- if (groundedSearch) {
163
- requestConfig.config.tools = [{ googleSearch: {} }];
164
- }
165
- (0, log_1.info)("gem query on:" + selectedModel, requestConfig);
166
- try {
167
- const response = await ai.models.generateContent(requestConfig);
168
- const rawtext = (response.text ?? "")
169
- .replace(/```(json)?/gi, "")
170
- .replace(/:[ ]+undefined/gim, ": null");
171
- (0, log_1.info)("gem response");
172
- (0, log_1.debug)("gem prompt:" + prompt, ident);
173
- (0, log_1.debug)("gem response:" + rawtext);
174
- (0, log_1.debug)("gem query usage:" + JSON.stringify(response.usageMetadata));
175
- onGenerated?.({ model: response.modelVersion || selectedModel, generatedAt: Date.now() });
176
- return rawtext;
177
- }
178
- catch (e) {
179
- const mod = `gemini-${selectedModel}`;
180
- if ((0, retryOnError_1.isOverloadedApiKeyError)(e)) {
181
- (0, log_1.warn)("throughput exceeded for gemini:", mod);
182
- (0, apikey_1.blockKeyService)(key, mod);
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.907",
3
+ "version": "0.0.908",
4
4
  "license": "ISC",
5
5
  "author": "admin@gec.dev",
6
6
  "repository": {