feat: Implement retry with backoff for API calls (#613)

This commit is contained in:
N. Taylor Mullen
2025-05-30 10:57:00 -07:00
committed by GitHub
parent c5608869c0
commit 8c46108a85
6 changed files with 542 additions and 40 deletions
+15 -11
View File
@@ -10,6 +10,7 @@ import { BaseTool, ToolResult } from './tools.js';
import { getErrorMessage } from '../utils/errors.js';
import { Config } from '../config/config.js';
import { getResponseText } from '../utils/generateContentResponseUtilities.js';
import { retryWithBackoff } from '../utils/retry.js';
// Interfaces for grounding metadata (similar to web-search.ts)
interface GroundingChunkWeb {
@@ -121,18 +122,21 @@ export class WebFetchTool extends BaseTool<WebFetchToolParams, ToolResult> {
const userPrompt = params.prompt;
try {
const response = await this.ai.models.generateContent({
model: this.modelName,
contents: [
{
role: 'user',
parts: [{ text: userPrompt }],
const apiCall = () =>
this.ai.models.generateContent({
model: this.modelName,
contents: [
{
role: 'user',
parts: [{ text: userPrompt }],
},
],
config: {
tools: [{ urlContext: {} }],
},
],
config: {
tools: [{ urlContext: {} }],
},
});
});
const response = await retryWithBackoff(apiCall);
console.debug(
`[WebFetchTool] Full response for prompt "${userPrompt.substring(0, 50)}...":`,
+11 -7
View File
@@ -11,6 +11,7 @@ import { SchemaValidator } from '../utils/schemaValidator.js';
import { getErrorMessage } from '../utils/errors.js';
import { Config } from '../config/config.js';
import { getResponseText } from '../utils/generateContentResponseUtilities.js';
import { retryWithBackoff } from '../utils/retry.js';
interface GroundingChunkWeb {
uri?: string;
@@ -121,13 +122,16 @@ export class WebSearchTool extends BaseTool<
}
try {
const response = await this.ai.models.generateContent({
model: this.modelName,
contents: [{ role: 'user', parts: [{ text: params.query }] }],
config: {
tools: [{ googleSearch: {} }],
},
});
const apiCall = () =>
this.ai.models.generateContent({
model: this.modelName,
contents: [{ role: 'user', parts: [{ text: params.query }] }],
config: {
tools: [{ googleSearch: {} }],
},
});
const response = await retryWithBackoff(apiCall);
const responseText = getResponseText(response);
const groundingMetadata = response.candidates?.[0]?.groundingMetadata;