Spaces:
Runtime error
Runtime error
File size: 6,581 Bytes
077865a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import { describe, it, expect } from 'vitest';
import { sanitizeForGemini } from '../../providers/google.js';
describe('sanitizeForGemini', () => {
it('strips top-level JSON-Schema-only fields that Google rejects', () => {
const input = {
$schema: 'https://json-schema.org/draft/2020-12/schema',
$id: 'http://example.com/foo.json',
type: 'object',
properties: { name: { type: 'string' } },
};
expect(sanitizeForGemini(input)).toEqual({
type: 'object',
properties: { name: { type: 'string' } },
});
});
it('strips exclusiveMinimum / exclusiveMaximum recursively in nested properties', () => {
const input = {
type: 'object',
properties: {
temp_threshold: { type: 'number', exclusiveMinimum: 0, exclusiveMaximum: 100 },
nested: {
type: 'object',
properties: {
count: { type: 'integer', exclusiveMinimum: 1 },
},
},
},
};
expect(sanitizeForGemini(input)).toEqual({
type: 'object',
properties: {
temp_threshold: { type: 'number' },
nested: {
type: 'object',
properties: {
count: { type: 'integer' },
},
},
},
});
});
it('walks through arrays (e.g. anyOf branches)', () => {
const input = {
anyOf: [
{ type: 'string', $ref: '#/definitions/foo' },
{ type: 'number', exclusiveMinimum: 0 },
],
};
expect(sanitizeForGemini(input)).toEqual({
anyOf: [
{ type: 'string' },
{ type: 'number' },
],
});
});
it('removes $defs / definitions / patternProperties / if-then-else', () => {
const input = {
type: 'object',
$defs: { Foo: { type: 'string' } },
definitions: { Bar: { type: 'integer' } },
patternProperties: { '^x-': { type: 'string' } },
if: { properties: { kind: { const: 'A' } } },
then: { required: ['extra'] },
else: { required: [] },
properties: { kind: { type: 'string' } },
};
expect(sanitizeForGemini(input)).toEqual({
type: 'object',
properties: { kind: { type: 'string' } },
});
});
it('passes through supported OpenAPI fields untouched', () => {
const input = {
type: 'object',
description: 'A tool input',
required: ['city'],
properties: {
city: { type: 'string', description: 'City name', enum: ['Karachi', 'Lahore'] },
items: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 10 },
},
};
expect(sanitizeForGemini(input)).toEqual(input);
});
it('strips additionalProperties (Gemini rejects it with 400)', () => {
const input = {
type: 'object',
properties: {
name: { type: 'string' },
address: {
type: 'object',
properties: { street: { type: 'string' } },
additionalProperties: false,
},
},
additionalProperties: false,
};
expect(sanitizeForGemini(input)).toEqual({
type: 'object',
properties: {
name: { type: 'string' },
address: {
type: 'object',
properties: { street: { type: 'string' } },
},
},
});
});
it('handles primitives and null safely', () => {
expect(sanitizeForGemini(null)).toBe(null);
expect(sanitizeForGemini(undefined)).toBe(undefined);
expect(sanitizeForGemini('hello')).toBe('hello');
expect(sanitizeForGemini(42)).toBe(42);
expect(sanitizeForGemini(true)).toBe(true);
});
it('strips examples / const / readOnly / writeOnly (Gemini rejects all with 400)', () => {
// These fields are emitted by Zod, Pydantic, and most JSON-Schema generators
// (used by opencode, Continue, Cline tool definitions) but rejected by Google's
// Gemini and Gemma 4 IT generateContent endpoint with "Invalid JSON payload
// received. Unknown name 'X' at tools[0].function_declarations[0].parameters".
const input = {
type: 'object',
properties: {
command: {
type: 'string',
description: 'Shell command to execute',
examples: ['ls -la', 'pwd'],
readOnly: false,
},
mode: { type: 'string', const: 'sync' },
secret: { type: 'string', writeOnly: true },
},
required: ['command'],
};
expect(sanitizeForGemini(input)).toEqual({
type: 'object',
properties: {
command: { type: 'string', description: 'Shell command to execute' },
mode: { type: 'string' },
secret: { type: 'string' },
},
required: ['command'],
});
});
it('strips uniqueItems (Gemini rejects arrays with uniqueItems with 400)', () => {
// Confirmed via live Google AI Studio call:
// "Invalid JSON payload received. Unknown name 'uniqueItems' at
// 'tools[0].function_declarations[0].parameters.properties[2].value':
// Cannot find field."
const input = {
type: 'object',
properties: {
files: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
minItems: 0,
maxItems: 10,
},
},
};
expect(sanitizeForGemini(input)).toEqual({
type: 'object',
properties: {
files: {
type: 'array',
items: { type: 'string' },
minItems: 0,
maxItems: 10,
},
},
});
});
it('strips composition / structural keywords absent from Google Schema proto', () => {
// not / allOf / oneOf / prefixItems / contains / propertyNames / multipleOf /
// dependencies / deprecated are all absent from Google's Schema proto
// (https://ai.google.dev/api/caching#Schema). Only anyOf is supported.
const input = {
type: 'object',
not: { type: 'null' },
allOf: [{ type: 'object' }],
oneOf: [{ type: 'string' }, { type: 'number' }],
dependencies: { a: ['b'] },
deprecated: true,
properties: {
tuple: { type: 'array', prefixItems: [{ type: 'string' }] },
bag: { type: 'array', contains: { type: 'string' }, minContains: 1, maxContains: 5 },
names: { type: 'object', propertyNames: { pattern: '^x_' } },
count: { type: 'number', multipleOf: 0.5 },
},
};
expect(sanitizeForGemini(input)).toEqual({
type: 'object',
properties: {
tuple: { type: 'array' },
bag: { type: 'array' },
names: { type: 'object' },
count: { type: 'number' },
},
});
});
});
|