|
| 1 | +import { afterEach, describe, expect, test } from "bun:test" |
| 2 | +import { Effect } from "effect" |
| 3 | +import { LayerNode } from "@opencode-ai/core/effect/layer-node" |
| 4 | +import { Agent } from "@/agent/agent" |
| 5 | +import { Config } from "@/config/config" |
| 6 | +import { RuntimeFlags } from "@/effect/runtime-flags" |
| 7 | +import type { Provider } from "@/provider/provider" |
| 8 | +import { ModelID, ProviderID } from "@/provider/schema" |
| 9 | +import { ProviderTransform } from "@/provider/transform" |
| 10 | +import { ToolJsonSchema } from "@/tool/json-schema" |
| 11 | +import { ToolRegistry } from "@/tool/registry" |
| 12 | +import { TestConfig } from "../../fixture/config" |
| 13 | +import { disposeAllInstances } from "../../fixture/fixture" |
| 14 | +import { testEffect } from "../../lib/effect" |
| 15 | + |
| 16 | +const it = testEffect( |
| 17 | + LayerNode.buildLayer(LayerNode.group([ToolRegistry.node, Agent.node]), { |
| 18 | + replacements: [ |
| 19 | + LayerNode.replace(Config.node, TestConfig.layer()), |
| 20 | + LayerNode.replace(RuntimeFlags.node, RuntimeFlags.layer({ enableParallel: false })), |
| 21 | + ], |
| 22 | + }), |
| 23 | +) |
| 24 | + |
| 25 | +afterEach(disposeAllInstances) |
| 26 | + |
| 27 | +function isObject(value: unknown): value is Record<string, unknown> { |
| 28 | + return typeof value === "object" && value !== null && !Array.isArray(value) |
| 29 | +} |
| 30 | + |
| 31 | +// Gemini rejects a function declaration whose enum (or const) contains an empty string: |
| 32 | +// "function_declarations[i].parameters...enum[0]: cannot be empty". |
| 33 | +function emptyEnumValues(schema: unknown, location = "#"): string[] { |
| 34 | + if (Array.isArray(schema)) return schema.flatMap((child, index) => emptyEnumValues(child, `${location}/${index}`)) |
| 35 | + if (!isObject(schema)) return [] |
| 36 | + const found: string[] = [] |
| 37 | + if (Array.isArray(schema.enum) && schema.enum.some((value) => String(value) === "")) found.push(`${location}/enum`) |
| 38 | + if (schema.const === "") found.push(`${location}/const`) |
| 39 | + for (const [key, child] of Object.entries(schema)) { |
| 40 | + if (key === "enum" || key === "const" || key === "default") continue |
| 41 | + found.push(...emptyEnumValues(child, `${location}/${key}`)) |
| 42 | + } |
| 43 | + return found |
| 44 | +} |
| 45 | + |
| 46 | +function model(providerID: ProviderID): Provider.Model { |
| 47 | + const id = "gemini-4-argon" |
| 48 | + return { |
| 49 | + id: ModelID.make(id), |
| 50 | + providerID, |
| 51 | + api: { id, npm: providerID === ProviderID.google ? "@ai-sdk/google" : "@ai-sdk/google-vertex", url: "https://example.invalid" }, |
| 52 | + name: id, |
| 53 | + capabilities: { |
| 54 | + temperature: true, |
| 55 | + reasoning: true, |
| 56 | + attachment: false, |
| 57 | + toolcall: true, |
| 58 | + input: { text: true, audio: false, image: false, video: false, pdf: false }, |
| 59 | + output: { text: true, audio: false, image: false, video: false, pdf: false }, |
| 60 | + interleaved: false, |
| 61 | + }, |
| 62 | + cost: { input: 0, output: 0, cache: { read: 0, write: 0 } }, |
| 63 | + limit: { context: 1_000_000, output: 64_000 }, |
| 64 | + status: "active", |
| 65 | + release_date: "2026-01-01", |
| 66 | + options: {}, |
| 67 | + headers: {}, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +describe("Gemini tool JSON schemas", () => { |
| 72 | + for (const providerID of [ProviderID.google, ProviderID.googleVertex]) { |
| 73 | + for (const name of ["analyst", "builder"]) { |
| 74 | + it.instance(`${providerID}: no registered ${name} tool declares an empty-string enum or const`, () => |
| 75 | + Effect.gen(function* () { |
| 76 | + const registry = yield* ToolRegistry.Service |
| 77 | + const agents = yield* Agent.Service |
| 78 | + const agent = yield* agents.get(name) |
| 79 | + if (!agent) throw new Error(`${name} agent not found`) |
| 80 | + const target = model(providerID) |
| 81 | + const tools = yield* registry.tools({ providerID, modelID: target.id, agent }) |
| 82 | + expect(tools.some((tool) => tool.id === "answer_commit")).toBe(name === "analyst") |
| 83 | + const errors = tools.flatMap((tool) => |
| 84 | + emptyEnumValues(ProviderTransform.schema(target, ToolJsonSchema.fromTool(tool))).map( |
| 85 | + (location) => `${tool.id}: ${location}`, |
| 86 | + ), |
| 87 | + ) |
| 88 | + expect(errors).toEqual([]) |
| 89 | + }), |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + test("drops empty-string enum and const values but keeps the remaining enum", () => { |
| 95 | + const result = ProviderTransform.schema(model(ProviderID.googleVertex), { |
| 96 | + type: "object", |
| 97 | + properties: { |
| 98 | + unresolved: { anyOf: [{ type: "object", properties: {} }, { type: "string", enum: [""] }] }, |
| 99 | + blank: { type: "string", const: "" }, |
| 100 | + mode: { type: "string", enum: ["", "fast", "slow"] }, |
| 101 | + }, |
| 102 | + } as any) as any |
| 103 | + expect(result.properties.unresolved.anyOf[1]).toEqual({ type: "string" }) |
| 104 | + expect(result.properties.blank).toEqual({ type: "string" }) |
| 105 | + expect(result.properties.mode.enum).toEqual(["fast", "slow"]) |
| 106 | + }) |
| 107 | +}) |
0 commit comments