fix: add missing 'index' to Gemini streaming tool calls and improve tests

This commit is contained in:
lemon07r 2025-12-22 19:36:01 -05:00
parent 06327a408a
commit ac47e04cf9
3 changed files with 72 additions and 3 deletions

View file

@ -1,7 +1,7 @@
export default {
testEnvironment: 'node',
transform: {
'^.+\\.js$': 'babel-jest',
'^.+\\.(js|mjs)$': 'babel-jest',
},
transformIgnorePatterns: [
'/node_modules/(?!(uuid)/)', // uuid is an ESM module that needs to be transformed
@ -15,8 +15,7 @@ export default {
'^(\\.{1,2}/.*)\\.js$': '$1'
},
testMatch: [
'**/tests/api-server.test.js',
'**/tests/api-integration.test.js'
'**/tests/**/*.test.js'
],
collectCoverageFrom: [
'src/**/*.js',

View file

@ -209,6 +209,7 @@ export class GeminiConverter extends BaseConverter {
}
if (part.functionCall) {
toolCalls.push({
index: toolCalls.length,
id: part.functionCall.id || `call_${uuidv4()}`,
type: 'function',
function: {

View file

@ -0,0 +1,69 @@
import GeminiConverter from '../src/converters/strategies/GeminiConverter.js';
import { jest } from '@jest/globals';
describe('GeminiConverter', () => {
let converter;
beforeEach(() => {
converter = new GeminiConverter();
});
test('toOpenAIStreamChunk adds index to tool_calls', () => {
const geminiChunk = {
candidates: [{
content: {
parts: [{
functionCall: {
name: 'test_tool',
args: { param: 'value' }
}
}]
}
}]
};
const result = converter.toOpenAIStreamChunk(geminiChunk, 'gemini-pro');
expect(result).not.toBeNull();
expect(result.choices[0].delta).toHaveProperty('tool_calls');
expect(result.choices[0].delta.tool_calls).toHaveLength(1);
expect(result.choices[0].delta.tool_calls[0]).toHaveProperty('index');
expect(result.choices[0].delta.tool_calls[0].index).toBe(0);
expect(result.choices[0].delta.tool_calls[0].function.name).toBe('test_tool');
});
test('toOpenAIStreamChunk handles multiple tool_calls with correct indices', () => {
const geminiChunk = {
candidates: [{
content: {
parts: [
{
functionCall: {
name: 'tool_one',
args: {}
}
},
{
functionCall: {
name: 'tool_two',
args: {}
}
}
]
}
}]
};
const result = converter.toOpenAIStreamChunk(geminiChunk, 'gemini-pro');
expect(result).not.toBeNull();
expect(result.choices[0].delta.tool_calls).toHaveLength(2);
expect(result.choices[0].delta.tool_calls[0].index).toBe(0);
expect(result.choices[0].delta.tool_calls[0].function.name).toBe('tool_one');
expect(result.choices[0].delta.tool_calls[1].index).toBe(1);
expect(result.choices[0].delta.tool_calls[1].function.name).toBe('tool_two');
});
});