mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-29 21:23:29 -07:00
feat(core): increase fetch timeout and improve error stringification for high-latency models
This commit is contained in:
@@ -73,17 +73,20 @@ describe('CodeAssistServer', () => {
|
||||
LlmRole.MAIN,
|
||||
);
|
||||
|
||||
expect(mockRequest).toHaveBeenCalledWith({
|
||||
url: expect.stringContaining(':generateContent'),
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-custom-header': 'test-value',
|
||||
},
|
||||
responseType: 'json',
|
||||
body: expect.any(String),
|
||||
signal: undefined,
|
||||
});
|
||||
expect(mockRequest).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
url: expect.stringContaining(':generateContent'),
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-custom-header': 'test-value',
|
||||
},
|
||||
responseType: 'json',
|
||||
body: expect.any(String),
|
||||
signal: undefined,
|
||||
timeout: 60000,
|
||||
}),
|
||||
);
|
||||
|
||||
const requestBody = JSON.parse(mockRequest.mock.calls[0][0].body);
|
||||
expect(requestBody.user_prompt_id).toBe('user-prompt-id');
|
||||
@@ -391,17 +394,20 @@ describe('CodeAssistServer', () => {
|
||||
results.push(res);
|
||||
}
|
||||
|
||||
expect(mockRequest).toHaveBeenCalledWith({
|
||||
url: expect.stringContaining(':streamGenerateContent'),
|
||||
method: 'POST',
|
||||
params: { alt: 'sse' },
|
||||
responseType: 'stream',
|
||||
body: expect.any(String),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
signal: undefined,
|
||||
});
|
||||
expect(mockRequest).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
url: expect.stringContaining(':streamGenerateContent'),
|
||||
method: 'POST',
|
||||
params: { alt: 'sse' },
|
||||
responseType: 'stream',
|
||||
body: expect.any(String),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
signal: undefined,
|
||||
timeout: 60000,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(results).toHaveLength(2);
|
||||
expect(results[0].candidates?.[0].content?.parts?.[0].text).toBe('Hello');
|
||||
|
||||
@@ -305,6 +305,7 @@ export class CodeAssistServer implements ContentGenerator {
|
||||
responseType: 'json',
|
||||
body: JSON.stringify(req),
|
||||
signal,
|
||||
timeout: 60000,
|
||||
});
|
||||
return res.data;
|
||||
}
|
||||
@@ -352,6 +353,7 @@ export class CodeAssistServer implements ContentGenerator {
|
||||
responseType: 'stream',
|
||||
body: JSON.stringify(req),
|
||||
signal,
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
return (async function* (): AsyncGenerator<T> {
|
||||
|
||||
@@ -202,7 +202,7 @@ export async function createContentGenerator(
|
||||
'x-gemini-api-privileged-user-id': `${installationId}`,
|
||||
};
|
||||
}
|
||||
const httpOptions = { headers };
|
||||
const httpOptions = { headers, timeout: 60000 };
|
||||
|
||||
const googleGenAI = new GoogleGenAI({
|
||||
apiKey: config.apiKey === '' ? undefined : config.apiKey,
|
||||
|
||||
@@ -29,6 +29,15 @@ export function getErrorMessage(error: unknown): string {
|
||||
if (friendlyError instanceof Error) {
|
||||
return friendlyError.message;
|
||||
}
|
||||
if (
|
||||
typeof friendlyError === 'object' &&
|
||||
friendlyError !== null &&
|
||||
'message' in friendlyError &&
|
||||
typeof (friendlyError as { message: unknown }).message === 'string'
|
||||
) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
||||
return (friendlyError as { message: string }).message;
|
||||
}
|
||||
try {
|
||||
return String(friendlyError);
|
||||
} catch {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { getErrorMessage } from './errors.js';
|
||||
import { type HttpError } from './httpErrors.js';
|
||||
|
||||
describe('getErrorMessage with timeout errors', () => {
|
||||
it('should handle undici HeadersTimeoutError correctly', () => {
|
||||
// Simulate what undici might throw if it's not a proper Error instance
|
||||
// or has a specific code.
|
||||
const timeoutError = {
|
||||
name: 'HeadersTimeoutError',
|
||||
code: 'UND_ERR_HEADERS_TIMEOUT',
|
||||
message: 'Headers timeout error',
|
||||
};
|
||||
|
||||
// If it's a plain object, getErrorMessage might struggle if it expects an Error
|
||||
const message = getErrorMessage(timeoutError);
|
||||
// Based on existing implementation:
|
||||
// friendlyError = toFriendlyError(timeoutError) -> returns timeoutError
|
||||
// if (friendlyError instanceof Error) -> false
|
||||
// return String(friendlyError) -> "[object Object]"
|
||||
|
||||
expect(message).toBe('Headers timeout error');
|
||||
});
|
||||
|
||||
it('should handle undici HeadersTimeoutError as an Error instance', () => {
|
||||
const error = new Error('Headers timeout error');
|
||||
(error as HttpError).name = 'HeadersTimeoutError';
|
||||
(error as HttpError).status = 504; // simulate status for test
|
||||
(error as HttpError & { code?: string }).code = 'UND_ERR_HEADERS_TIMEOUT';
|
||||
|
||||
const message = getErrorMessage(error);
|
||||
expect(message).toBe('Headers timeout error');
|
||||
});
|
||||
|
||||
it('should return String representation for objects without a message property', () => {
|
||||
const error = { some: 'other', object: 123 };
|
||||
const message = getErrorMessage(error);
|
||||
expect(message).toBe('[object Object]');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user