Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/core/src/core/__tests__/openaiTimeoutHandling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,12 @@ describe('OpenAIContentGenerator Timeout Handling', () => {
expect(errorMessage).toContain('Reduce input length or complexity');
expect(errorMessage).toContain('Increase timeout in config');
expect(errorMessage).toContain('Check network connectivity');
expect(errorMessage).toContain('Consider using streaming mode');
}
});
});

describe('generateContentStream timeout handling', () => {
it('should handle streaming timeout errors', async () => {
it('should handle streaming timeout errors with the shared timeout message', async () => {
const timeoutError = new Error('Streaming timeout');
mockOpenAIClient.chat.completions.create.mockRejectedValue(timeoutError);

Expand All @@ -233,11 +232,11 @@ describe('OpenAIContentGenerator Timeout Handling', () => {
await expect(
generator.generateContentStream(request, 'test-prompt-id'),
).rejects.toThrow(
/Streaming request timeout after \d+s\. Try reducing input length or increasing timeout in config\./,
/Request timeout after \d+s\. Try reducing input length or increasing timeout in config\./,
);
});

it('should include streaming-specific troubleshooting tips', async () => {
it('should include the shared troubleshooting tips for streaming timeouts', async () => {
const timeoutError = new Error('request timed out');
mockOpenAIClient.chat.completions.create.mockRejectedValue(timeoutError);

Expand All @@ -251,9 +250,10 @@ describe('OpenAIContentGenerator Timeout Handling', () => {
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : String(error);
expect(errorMessage).toContain('Streaming timeout troubleshooting:');
expect(errorMessage).toContain('Troubleshooting tips:');
expect(errorMessage).toContain('Reduce input length or complexity');
expect(errorMessage).toContain('Increase timeout in config');
expect(errorMessage).toContain('Check network connectivity');
expect(errorMessage).toContain('Consider using non-streaming mode');
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,22 @@ vi.mock('../../utils/openaiLogger.js', () => ({
}));

const realConvertGeminiRequestToOpenAI =
OpenAIContentConverter.prototype.convertGeminiRequestToOpenAI;
OpenAIContentConverter.convertGeminiRequestToOpenAI;
const convertGeminiRequestToOpenAISpy = vi
.spyOn(OpenAIContentConverter.prototype, 'convertGeminiRequestToOpenAI')
.spyOn(OpenAIContentConverter, 'convertGeminiRequestToOpenAI')
.mockReturnValue([{ role: 'user', content: 'converted' }]);
const convertGeminiToolsToOpenAISpy = vi
.spyOn(OpenAIContentConverter.prototype, 'convertGeminiToolsToOpenAI')
.spyOn(OpenAIContentConverter, 'convertGeminiToolsToOpenAI')
.mockResolvedValue([{ type: 'function', function: { name: 'tool' } }]);
const convertGeminiResponseToOpenAISpy = vi
.spyOn(OpenAIContentConverter.prototype, 'convertGeminiResponseToOpenAI')
.spyOn(OpenAIContentConverter, 'convertGeminiResponseToOpenAI')
.mockReturnValue({
id: 'openai-response',
object: 'chat.completion',
created: 123456789,
model: 'test-model',
choices: [],
} as OpenAI.Chat.ChatCompletion);
const setModalitiesSpy = vi.spyOn(
OpenAIContentConverter.prototype,
'setModalities',
);

const createConfig = (overrides: Record<string, unknown> = {}): Config => {
const configContent = {
Expand Down Expand Up @@ -121,7 +117,6 @@ describe('LoggingContentGenerator', () => {
convertGeminiRequestToOpenAISpy.mockClear();
convertGeminiToolsToOpenAISpy.mockClear();
convertGeminiResponseToOpenAISpy.mockClear();
setModalitiesSpy.mockClear();
});

it('logs request/response, normalizes thought parts, and logs OpenAI interaction', async () => {
Expand Down Expand Up @@ -409,13 +404,13 @@ describe('LoggingContentGenerator', () => {
});

it('uses generator modalities when converting logged OpenAI requests', async () => {
convertGeminiRequestToOpenAISpy.mockImplementationOnce(function (
this: OpenAIContentConverter,
request,
options,
) {
return realConvertGeminiRequestToOpenAI.call(this, request, options);
});
convertGeminiRequestToOpenAISpy.mockImplementationOnce(
(request, requestContext, options) => realConvertGeminiRequestToOpenAI(
request,
requestContext,
options,
),
);

const wrapped = createWrappedGenerator(
vi
Expand Down Expand Up @@ -458,7 +453,14 @@ describe('LoggingContentGenerator', () => {

await generator.generateContent(request, 'prompt-5');

expect(setModalitiesSpy).toHaveBeenCalledWith({ image: true });
expect(convertGeminiRequestToOpenAISpy).toHaveBeenCalledWith(
request,
expect.objectContaining({
model: 'test-model',
modalities: { image: true },
}),
{ cleanOrphanToolCalls: false },
);

const openaiLoggerInstance = vi.mocked(OpenAILogger).mock.results[0]
?.value as { logInteraction: ReturnType<typeof vi.fn> };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type {
InputModalities,
} from '../contentGenerator.js';
import { OpenAIContentConverter } from '../openaiContentGenerator/converter.js';
import type { RequestContext } from '../openaiContentGenerator/types.js';
import { OpenAILogger } from '../../utils/openaiLogger.js';
import {
getErrorMessage,
Expand Down Expand Up @@ -295,24 +296,26 @@ export class LoggingContentGenerator implements ContentGenerator {
return undefined;
}

const converter = new OpenAIContentConverter(
request.model,
this.schemaCompliance,
const requestContext = this.createLoggingRequestContext(request.model);
const messages = OpenAIContentConverter.convertGeminiRequestToOpenAI(
request,
requestContext,
{
cleanOrphanToolCalls: false,
},
);
converter.setModalities(this.modalities ?? {});
const messages = converter.convertGeminiRequestToOpenAI(request, {
cleanOrphanToolCalls: false,
});

const openaiRequest: OpenAI.Chat.ChatCompletionCreateParams = {
model: request.model,
messages,
};

if (request.config?.tools) {
openaiRequest.tools = await converter.convertGeminiToolsToOpenAI(
request.config.tools,
);
openaiRequest.tools =
await OpenAIContentConverter.convertGeminiToolsToOpenAI(
request.config.tools,
this.schemaCompliance ?? 'auto',
);
}

if (request.config?.temperature !== undefined) {
Expand All @@ -334,6 +337,14 @@ export class LoggingContentGenerator implements ContentGenerator {
return openaiRequest;
}

private createLoggingRequestContext(model: string): RequestContext {
return {
model,
modalities: this.modalities ?? {},
startTime: 0,
};
}

private async logOpenAIInteraction(
openaiRequest: OpenAI.Chat.ChatCompletionCreateParams | undefined,
response?: GenerateContentResponse,
Expand Down Expand Up @@ -362,12 +373,10 @@ export class LoggingContentGenerator implements ContentGenerator {
response: GenerateContentResponse,
openaiRequest: OpenAI.Chat.ChatCompletionCreateParams,
): OpenAI.Chat.ChatCompletion {
const converter = new OpenAIContentConverter(
openaiRequest.model,
this.schemaCompliance,
return OpenAIContentConverter.convertGeminiResponseToOpenAI(
response,
this.createLoggingRequestContext(openaiRequest.model),
);

return converter.convertGeminiResponseToOpenAI(response);
}

private consolidateGeminiResponsesForLogging(
Expand Down
Loading
Loading