refactor(convert): 简化工具转换逻辑并统一处理方式
重构了从OpenAI和Claude请求转换Gemini工具的逻辑,移除了functionDeclarations包装层,直接映射工具名称和参数。同时优化了无效工具的过滤处理,保持代码一致性。
This commit is contained in:
parent
1f9a26aa5a
commit
7f7fc78072
1 changed files with 18 additions and 24 deletions
|
|
@ -1042,13 +1042,11 @@ export function toGeminiRequestFromOpenAI(openaiRequest) {
|
|||
|
||||
// Handle tools and tool_choice
|
||||
if (openaiRequest.tools?.length) {
|
||||
geminiRequest.tools = [{
|
||||
functionDeclarations: openaiRequest.tools.map(t => ({
|
||||
name: t.function.name,
|
||||
description: t.function.description,
|
||||
parameters: t.function.parameters
|
||||
}))
|
||||
}];
|
||||
geminiRequest.tools = openaiRequest.tools.map(t => {
|
||||
const tool = {};
|
||||
tool[t.function.name] = t.function.parameters || {};
|
||||
return tool;
|
||||
});
|
||||
}
|
||||
|
||||
if (openaiRequest.tool_choice) {
|
||||
|
|
@ -1698,24 +1696,20 @@ export function toGeminiRequestFromClaude(claudeRequest) {
|
|||
|
||||
// Handle tools
|
||||
if (Array.isArray(claudeRequest.tools)) {
|
||||
geminiRequest.tools = [{
|
||||
functionDeclarations: claudeRequest.tools.map(tool => {
|
||||
// Ensure tool is a valid object and has a name
|
||||
if (!tool || typeof tool !== 'object' || !tool.name) {
|
||||
console.warn("Skipping invalid tool declaration in claudeRequest.tools.");
|
||||
return null; // Return null for invalid tools, filter out later
|
||||
}
|
||||
geminiRequest.tools = claudeRequest.tools.map(tool => {
|
||||
// Ensure tool is a valid object and has a name
|
||||
if (!tool || typeof tool !== 'object' || !tool.name) {
|
||||
console.warn("Skipping invalid tool declaration in claudeRequest.tools.");
|
||||
return null; // Return null for invalid tools, filter out later
|
||||
}
|
||||
|
||||
delete tool.input_schema.$schema;
|
||||
return {
|
||||
name: String(tool.name), // Ensure name is string
|
||||
description: String(tool.description || ''), // Ensure description is string
|
||||
parameters: tool.input_schema && typeof tool.input_schema === 'object' ? tool.input_schema : { type: 'object', properties: {} }
|
||||
};
|
||||
}).filter(Boolean) // Filter out any nulls from invalid tool declarations
|
||||
}];
|
||||
// If no valid functionDeclarations, remove the tools array
|
||||
if (geminiRequest.tools[0].functionDeclarations.length === 0) {
|
||||
const geminiTool = {};
|
||||
geminiTool[tool.name] = tool.input_schema && typeof tool.input_schema === 'object' ? tool.input_schema : {};
|
||||
return geminiTool;
|
||||
}).filter(Boolean); // Filter out any nulls from invalid tool declarations
|
||||
|
||||
// If no valid tools, remove the tools array
|
||||
if (geminiRequest.tools.length === 0) {
|
||||
delete geminiRequest.tools;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue