Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

function-declaration-tool.ts

33 lines1.0 KB

1import type { FunctionDeclaration } from '@google/genai'
2​
3export type FunctionDeclarationTool = FunctionDeclaration
4​
5const validateFunctionDeclarationTool = (tool: FunctionDeclarationTool) => {
6 const nameRegex = /^[a-zA-Z0-9_:.-]{1,64}$/
7 if (tool.name === undefined || !nameRegex.test(tool.name)) {
8 throw new Error(
9 `Invalid function name: ${tool.name}. Must be 1-64 characters long and contain only a-z, A-Z, 0-9, underscores, colons, dots, and dashes.`,
10 )
11 }
12​
13 if (tool.parameters && tool.parametersJsonSchema) {
14 throw new Error(
15 `FunctionDeclarationTool cannot have both 'parameters' and 'parametersJsonSchema' defined. Please use only one.`,
16 )
17 }
18​
19 if (tool.response && tool.responseJsonSchema) {
20 throw new Error(
21 `FunctionDeclarationTool cannot have both 'response' and 'responseJsonSchema' defined. Please use only one.`,
22 )
23 }
25​
26export function functionDeclarationTools(
27 tools: Array<FunctionDeclarationTool>,
28) {
29 tools.forEach(validateFunctionDeclarationTool)
30 return {
31 functionDeclarations: tools,
32 }