|
1 | 1 | import { t } from "i18next" |
| 2 | +import { FunctionCallingConfigMode } from "@google/genai" |
2 | 3 |
|
3 | 4 | import { GeminiHandler } from "../gemini" |
4 | 5 | import type { ApiHandlerOptions } from "../../../shared/api" |
@@ -141,4 +142,152 @@ describe("GeminiHandler backend support", () => { |
141 | 142 | }).rejects.toThrow(t("common:errors.gemini.generate_stream", { error: "API rate limit exceeded" })) |
142 | 143 | }) |
143 | 144 | }) |
| 145 | + |
| 146 | + describe("allowedFunctionNames support", () => { |
| 147 | + const testTools = [ |
| 148 | + { |
| 149 | + type: "function" as const, |
| 150 | + function: { |
| 151 | + name: "read_file", |
| 152 | + description: "Read a file", |
| 153 | + parameters: { type: "object", properties: {} }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + { |
| 157 | + type: "function" as const, |
| 158 | + function: { |
| 159 | + name: "write_to_file", |
| 160 | + description: "Write to a file", |
| 161 | + parameters: { type: "object", properties: {} }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + { |
| 165 | + type: "function" as const, |
| 166 | + function: { |
| 167 | + name: "execute_command", |
| 168 | + description: "Execute a command", |
| 169 | + parameters: { type: "object", properties: {} }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + ] |
| 173 | + |
| 174 | + it("should pass allowedFunctionNames to toolConfig when provided", async () => { |
| 175 | + const options = { |
| 176 | + apiProvider: "gemini", |
| 177 | + } as ApiHandlerOptions |
| 178 | + const handler = new GeminiHandler(options) |
| 179 | + const stub = vi.fn().mockReturnValue((async function* () {})()) |
| 180 | + // @ts-ignore access private client |
| 181 | + handler["client"].models.generateContentStream = stub |
| 182 | + |
| 183 | + await handler |
| 184 | + .createMessage("test", [] as any, { |
| 185 | + taskId: "test-task", |
| 186 | + tools: testTools, |
| 187 | + allowedFunctionNames: ["read_file", "write_to_file"], |
| 188 | + }) |
| 189 | + .next() |
| 190 | + |
| 191 | + const config = stub.mock.calls[0][0].config |
| 192 | + expect(config.toolConfig).toEqual({ |
| 193 | + functionCallingConfig: { |
| 194 | + mode: FunctionCallingConfigMode.ANY, |
| 195 | + allowedFunctionNames: ["read_file", "write_to_file"], |
| 196 | + }, |
| 197 | + }) |
| 198 | + }) |
| 199 | + |
| 200 | + it("should include all tools but restrict callable functions via allowedFunctionNames", async () => { |
| 201 | + const options = { |
| 202 | + apiProvider: "gemini", |
| 203 | + } as ApiHandlerOptions |
| 204 | + const handler = new GeminiHandler(options) |
| 205 | + const stub = vi.fn().mockReturnValue((async function* () {})()) |
| 206 | + // @ts-ignore access private client |
| 207 | + handler["client"].models.generateContentStream = stub |
| 208 | + |
| 209 | + await handler |
| 210 | + .createMessage("test", [] as any, { |
| 211 | + taskId: "test-task", |
| 212 | + tools: testTools, |
| 213 | + allowedFunctionNames: ["read_file"], |
| 214 | + }) |
| 215 | + .next() |
| 216 | + |
| 217 | + const config = stub.mock.calls[0][0].config |
| 218 | + // All tools should be passed to the model |
| 219 | + expect(config.tools[0].functionDeclarations).toHaveLength(3) |
| 220 | + // But only read_file should be allowed to be called |
| 221 | + expect(config.toolConfig.functionCallingConfig.allowedFunctionNames).toEqual(["read_file"]) |
| 222 | + }) |
| 223 | + |
| 224 | + it("should take precedence over tool_choice when allowedFunctionNames is provided", async () => { |
| 225 | + const options = { |
| 226 | + apiProvider: "gemini", |
| 227 | + } as ApiHandlerOptions |
| 228 | + const handler = new GeminiHandler(options) |
| 229 | + const stub = vi.fn().mockReturnValue((async function* () {})()) |
| 230 | + // @ts-ignore access private client |
| 231 | + handler["client"].models.generateContentStream = stub |
| 232 | + |
| 233 | + await handler |
| 234 | + .createMessage("test", [] as any, { |
| 235 | + taskId: "test-task", |
| 236 | + tools: testTools, |
| 237 | + tool_choice: "auto", |
| 238 | + allowedFunctionNames: ["read_file"], |
| 239 | + }) |
| 240 | + .next() |
| 241 | + |
| 242 | + const config = stub.mock.calls[0][0].config |
| 243 | + // allowedFunctionNames should take precedence - mode should be ANY, not AUTO |
| 244 | + expect(config.toolConfig.functionCallingConfig.mode).toBe(FunctionCallingConfigMode.ANY) |
| 245 | + expect(config.toolConfig.functionCallingConfig.allowedFunctionNames).toEqual(["read_file"]) |
| 246 | + }) |
| 247 | + |
| 248 | + it("should fall back to tool_choice when allowedFunctionNames is empty", async () => { |
| 249 | + const options = { |
| 250 | + apiProvider: "gemini", |
| 251 | + } as ApiHandlerOptions |
| 252 | + const handler = new GeminiHandler(options) |
| 253 | + const stub = vi.fn().mockReturnValue((async function* () {})()) |
| 254 | + // @ts-ignore access private client |
| 255 | + handler["client"].models.generateContentStream = stub |
| 256 | + |
| 257 | + await handler |
| 258 | + .createMessage("test", [] as any, { |
| 259 | + taskId: "test-task", |
| 260 | + tools: testTools, |
| 261 | + tool_choice: "auto", |
| 262 | + allowedFunctionNames: [], |
| 263 | + }) |
| 264 | + .next() |
| 265 | + |
| 266 | + const config = stub.mock.calls[0][0].config |
| 267 | + // Empty allowedFunctionNames should fall back to tool_choice behavior |
| 268 | + expect(config.toolConfig.functionCallingConfig.mode).toBe(FunctionCallingConfigMode.AUTO) |
| 269 | + expect(config.toolConfig.functionCallingConfig.allowedFunctionNames).toBeUndefined() |
| 270 | + }) |
| 271 | + |
| 272 | + it("should not set toolConfig when allowedFunctionNames is undefined and no tool_choice", async () => { |
| 273 | + const options = { |
| 274 | + apiProvider: "gemini", |
| 275 | + } as ApiHandlerOptions |
| 276 | + const handler = new GeminiHandler(options) |
| 277 | + const stub = vi.fn().mockReturnValue((async function* () {})()) |
| 278 | + // @ts-ignore access private client |
| 279 | + handler["client"].models.generateContentStream = stub |
| 280 | + |
| 281 | + await handler |
| 282 | + .createMessage("test", [] as any, { |
| 283 | + taskId: "test-task", |
| 284 | + tools: testTools, |
| 285 | + }) |
| 286 | + .next() |
| 287 | + |
| 288 | + const config = stub.mock.calls[0][0].config |
| 289 | + // No toolConfig should be set when neither allowedFunctionNames nor tool_choice is provided |
| 290 | + expect(config.toolConfig).toBeUndefined() |
| 291 | + }) |
| 292 | + }) |
144 | 293 | }) |
0 commit comments