fix(google-search): add 30s timeout to Gemini API call (#1139)
This commit is contained in:
parent
62a8be03da
commit
65fe3c2adc
1 changed files with 21 additions and 8 deletions
|
|
@ -265,14 +265,27 @@ export default function (pi: ExtensionAPI) {
|
|||
try {
|
||||
if (process.env.GEMINI_API_KEY) {
|
||||
const ai = await getClient();
|
||||
const response = await ai.models.generateContent({
|
||||
model: process.env.GEMINI_SEARCH_MODEL || "gemini-2.5-flash",
|
||||
contents: params.query,
|
||||
config: {
|
||||
tools: [{ googleSearch: {} }],
|
||||
abortSignal: signal,
|
||||
},
|
||||
});
|
||||
|
||||
// Add a 30-second timeout to prevent hanging (#1100)
|
||||
const timeoutController = new AbortController();
|
||||
const timeoutId = setTimeout(() => timeoutController.abort(), 30_000);
|
||||
const combinedSignal = signal
|
||||
? AbortSignal.any([signal, timeoutController.signal])
|
||||
: timeoutController.signal;
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = await ai.models.generateContent({
|
||||
model: process.env.GEMINI_SEARCH_MODEL || "gemini-2.5-flash",
|
||||
contents: params.query,
|
||||
config: {
|
||||
tools: [{ googleSearch: {} }],
|
||||
abortSignal: combinedSignal,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
// Extract answer text
|
||||
const answer = response.text ?? "";
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue