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
|
// Handle tools and tool_choice
|
||||||
if (openaiRequest.tools?.length) {
|
if (openaiRequest.tools?.length) {
|
||||||
geminiRequest.tools = [{
|
geminiRequest.tools = openaiRequest.tools.map(t => {
|
||||||
functionDeclarations: openaiRequest.tools.map(t => ({
|
const tool = {};
|
||||||
name: t.function.name,
|
tool[t.function.name] = t.function.parameters || {};
|
||||||
description: t.function.description,
|
return tool;
|
||||||
parameters: t.function.parameters
|
});
|
||||||
}))
|
|
||||||
}];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (openaiRequest.tool_choice) {
|
if (openaiRequest.tool_choice) {
|
||||||
|
|
@ -1698,24 +1696,20 @@ export function toGeminiRequestFromClaude(claudeRequest) {
|
||||||
|
|
||||||
// Handle tools
|
// Handle tools
|
||||||
if (Array.isArray(claudeRequest.tools)) {
|
if (Array.isArray(claudeRequest.tools)) {
|
||||||
geminiRequest.tools = [{
|
geminiRequest.tools = claudeRequest.tools.map(tool => {
|
||||||
functionDeclarations: claudeRequest.tools.map(tool => {
|
// Ensure tool is a valid object and has a name
|
||||||
// Ensure tool is a valid object and has a name
|
if (!tool || typeof tool !== 'object' || !tool.name) {
|
||||||
if (!tool || typeof tool !== 'object' || !tool.name) {
|
console.warn("Skipping invalid tool declaration in claudeRequest.tools.");
|
||||||
console.warn("Skipping invalid tool declaration in claudeRequest.tools.");
|
return null; // Return null for invalid tools, filter out later
|
||||||
return null; // Return null for invalid tools, filter out later
|
}
|
||||||
}
|
|
||||||
|
|
||||||
delete tool.input_schema.$schema;
|
const geminiTool = {};
|
||||||
return {
|
geminiTool[tool.name] = tool.input_schema && typeof tool.input_schema === 'object' ? tool.input_schema : {};
|
||||||
name: String(tool.name), // Ensure name is string
|
return geminiTool;
|
||||||
description: String(tool.description || ''), // Ensure description is string
|
}).filter(Boolean); // Filter out any nulls from invalid tool declarations
|
||||||
parameters: tool.input_schema && typeof tool.input_schema === 'object' ? tool.input_schema : { type: 'object', properties: {} }
|
|
||||||
};
|
// If no valid tools, remove the tools array
|
||||||
}).filter(Boolean) // Filter out any nulls from invalid tool declarations
|
if (geminiRequest.tools.length === 0) {
|
||||||
}];
|
|
||||||
// If no valid functionDeclarations, remove the tools array
|
|
||||||
if (geminiRequest.tools[0].functionDeclarations.length === 0) {
|
|
||||||
delete geminiRequest.tools;
|
delete geminiRequest.tools;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue