ag-common 0.0.906 → 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;
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
export type ModelPreference = "quality" | "fast" | undefined;
|
|
2
|
+
/** Provenance of a successful response, with generation time in Unix milliseconds. */
|
|
3
|
+
export type GenerationMetadata = {
|
|
4
|
+
model: string;
|
|
5
|
+
generatedAt: number;
|
|
6
|
+
};
|
|
2
7
|
export declare const isTextGenerationModel: (name: string) => boolean;
|
|
3
|
-
export declare const geminiPromptImage: ({ prompt, urls, ident, prefer, }: {
|
|
8
|
+
export declare const geminiPromptImage: ({ prompt, urls, ident, prefer, onGenerated, }: {
|
|
4
9
|
prompt: string;
|
|
5
10
|
urls?: string[];
|
|
6
11
|
ident: string | undefined;
|
|
7
12
|
prefer?: ModelPreference;
|
|
13
|
+
onGenerated?: (metadata: GenerationMetadata) => void;
|
|
8
14
|
}) => Promise<string>;
|
|
9
|
-
export declare const geminiPromptDirect: ({ prompt, images, ident, prefer, groundedSearch, }: {
|
|
15
|
+
export declare const geminiPromptDirect: ({ prompt, images, ident, prefer, groundedSearch, onGenerated, }: {
|
|
10
16
|
prompt: string;
|
|
11
17
|
images?: {
|
|
12
18
|
arraybuffer: ArrayBuffer;
|
|
@@ -15,5 +21,6 @@ export declare const geminiPromptDirect: ({ prompt, images, ident, prefer, groun
|
|
|
15
21
|
ident: string | undefined;
|
|
16
22
|
prefer?: ModelPreference;
|
|
17
23
|
groundedSearch?: boolean;
|
|
24
|
+
onGenerated?: (metadata: GenerationMetadata) => void;
|
|
18
25
|
}) => Promise<string>;
|
|
19
26
|
export declare const resolveGroundedUrl: (url: string) => Promise<string>;
|
|
@@ -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]);
|
|
@@ -121,7 +121,7 @@ const getAvailableGeminiCombinations = async (prefer) => {
|
|
|
121
121
|
}
|
|
122
122
|
return combinations;
|
|
123
123
|
};
|
|
124
|
-
const geminiPromptImage = async ({ prompt, urls, ident, prefer, }) => {
|
|
124
|
+
const geminiPromptImage = async ({ prompt, urls, ident, prefer, onGenerated, }) => {
|
|
125
125
|
let images = [];
|
|
126
126
|
if (urls && urls.length > 0) {
|
|
127
127
|
images = await (0, async_1.asyncMap)(urls, (i) => (0, fetch_1.fetchToMemory)(i));
|
|
@@ -134,11 +134,12 @@ const geminiPromptImage = async ({ prompt, urls, ident, prefer, }) => {
|
|
|
134
134
|
images: images.filter(array_1.notEmpty),
|
|
135
135
|
ident,
|
|
136
136
|
prefer,
|
|
137
|
+
onGenerated,
|
|
137
138
|
});
|
|
138
139
|
return r;
|
|
139
140
|
};
|
|
140
141
|
exports.geminiPromptImage = geminiPromptImage;
|
|
141
|
-
const geminiPromptDirect = async ({ prompt, images = [], ident, prefer, groundedSearch = false, }) => {
|
|
142
|
+
const geminiPromptDirect = async ({ prompt, images = [], ident, prefer, groundedSearch = false, onGenerated, }) => {
|
|
142
143
|
const parts = images.map((i) => ({
|
|
143
144
|
inlineData: {
|
|
144
145
|
data: Buffer.from(i.arraybuffer).toString("base64"),
|
|
@@ -150,37 +151,54 @@ const geminiPromptDirect = async ({ prompt, images = [], ident, prefer, grounded
|
|
|
150
151
|
if (combinations.length === 0) {
|
|
151
152
|
throw new Error("No available API key and model combinations");
|
|
152
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
|
-
|
|
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;
|
|
181
199
|
}
|
|
182
|
-
throw e;
|
|
183
200
|
}
|
|
201
|
+
throw lastFailure ?? new Error("No available API key and model combinations");
|
|
184
202
|
}, 1, 5000);
|
|
185
203
|
};
|
|
186
204
|
exports.geminiPromptDirect = geminiPromptDirect;
|