mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-18 14:25:51 +00:00
feat: 添加gemini协议适配 (#125)
* feat: 添加gemini协议适配 * Remove unrelated local files from Gemini PR
This commit is contained in:
130
src/services/api/gemini/__tests__/convertTools.test.ts
Normal file
130
src/services/api/gemini/__tests__/convertTools.test.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
import {
|
||||
anthropicToolChoiceToGemini,
|
||||
anthropicToolsToGemini,
|
||||
} from '../convertTools.js'
|
||||
|
||||
describe('anthropicToolsToGemini', () => {
|
||||
test('converts basic tool to parametersJsonSchema', () => {
|
||||
const tools = [
|
||||
{
|
||||
type: 'custom',
|
||||
name: 'bash',
|
||||
description: 'Run a bash command',
|
||||
input_schema: {
|
||||
type: 'object',
|
||||
properties: { command: { type: 'string' } },
|
||||
required: ['command'],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
expect(anthropicToolsToGemini(tools as any)).toEqual([
|
||||
{
|
||||
functionDeclarations: [
|
||||
{
|
||||
name: 'bash',
|
||||
description: 'Run a bash command',
|
||||
parametersJsonSchema: {
|
||||
type: 'object',
|
||||
properties: { command: { type: 'string' } },
|
||||
propertyOrdering: ['command'],
|
||||
required: ['command'],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
test('sanitizes unsupported JSON Schema fields for Gemini', () => {
|
||||
const tools = [
|
||||
{
|
||||
type: 'custom',
|
||||
name: 'complex',
|
||||
description: 'Complex schema',
|
||||
input_schema: {
|
||||
$schema: 'http://json-schema.org/draft-07/schema#',
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
propertyNames: { pattern: '^[a-z]+$' },
|
||||
properties: {
|
||||
mode: { const: 'strict' },
|
||||
retries: {
|
||||
type: 'integer',
|
||||
exclusiveMinimum: 0,
|
||||
},
|
||||
metadata: {
|
||||
type: 'object',
|
||||
additionalProperties: {
|
||||
type: 'string',
|
||||
propertyNames: { pattern: '^[a-z]+$' },
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ['mode'],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
expect(anthropicToolsToGemini(tools as any)).toEqual([
|
||||
{
|
||||
functionDeclarations: [
|
||||
{
|
||||
name: 'complex',
|
||||
description: 'Complex schema',
|
||||
parametersJsonSchema: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
mode: {
|
||||
type: 'string',
|
||||
enum: ['strict'],
|
||||
},
|
||||
retries: {
|
||||
type: 'integer',
|
||||
minimum: 0,
|
||||
},
|
||||
metadata: {
|
||||
type: 'object',
|
||||
additionalProperties: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
},
|
||||
propertyOrdering: ['mode', 'retries', 'metadata'],
|
||||
required: ['mode'],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
test('returns empty array when no tools are provided', () => {
|
||||
expect(anthropicToolsToGemini([])).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('anthropicToolChoiceToGemini', () => {
|
||||
test('maps auto', () => {
|
||||
expect(anthropicToolChoiceToGemini({ type: 'auto' })).toEqual({
|
||||
mode: 'AUTO',
|
||||
})
|
||||
})
|
||||
|
||||
test('maps any', () => {
|
||||
expect(anthropicToolChoiceToGemini({ type: 'any' })).toEqual({
|
||||
mode: 'ANY',
|
||||
})
|
||||
})
|
||||
|
||||
test('maps explicit tool choice', () => {
|
||||
expect(
|
||||
anthropicToolChoiceToGemini({ type: 'tool', name: 'bash' }),
|
||||
).toEqual({
|
||||
mode: 'ANY',
|
||||
allowedFunctionNames: ['bash'],
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user