Created
December 15, 2025 14:14
-
-
Save zirkelc/99f5a613127007e9bca69a76368c1e94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { UIMessage, InferUITools, tool } from 'ai'; | |
| type ToolSet = ReturnType<typeof createToolSet>['tools']; | |
| // Create a tool set | |
| // Optionally accept common tool params like a UIStreamWriter | |
| export const createToolSet = () => { | |
| const tools = { | |
| firstTool: tool({ | |
| description: 'Greets the user', | |
| inputSchema: z.object({ name: z.string() }), | |
| execute: async ({ name }) => `Hello, ${name}!`, | |
| }), | |
| secondTool: tool({ | |
| description: 'Tells the user their age', | |
| inputSchema: z.object({ age: z.number() }), | |
| execute: async ({ age }) => `You are ${age} years old!`, | |
| }), | |
| thirdTool: tool({ | |
| description: 'Tells the user their favorite color', | |
| inputSchema: z.object({ color: z.string() }), | |
| execute: async ({ color }) => `Your favorite color is ${color}!`, | |
| }), | |
| } as const; | |
| const allToolNames = Object.keys(tools) as Array<keyof typeof tools>; | |
| // Type-safe function accepting only available tools | |
| const inactiveTools = (toolNames: Array<keyof typeof tools>) => { | |
| return allToolNames.filter((toolName) => !toolNames.includes(toolName)); | |
| }; | |
| return { tools, inactiveTools }; | |
| }; | |
| // Metadata schema (optional) | |
| type MyMetadata = {}; | |
| // Data part schema (optional) | |
| type MyDataPart = {}; | |
| // Tool set | |
| type MyToolSet = InferUITools<ToolSet>; | |
| // Typed UI messages for client and server | |
| export type MyUIMessage = UIMessage<MyMetadata, MyDataPart, MyToolSet>; | |
| async function generateSomething(prompt: string) { | |
| // Create tool set | |
| const { tools, inactiveTools } = createToolSet(); | |
| return generateText({ | |
| prompt, | |
| model: 'openai/gpt-5.1-codex', | |
| // Deactive only one tool | |
| activeTools: inactiveTools(['thirdTool']), // typesafe! | |
| // Sames as using: | |
| // activeTools: ['firstTool', 'secondTool'], | |
| tools, | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment