Overview
AI tools are functions that the AI can call to perform actions or retrieve information. Gorkie uses the Vercel AI SDK’stool() function to define tools with type-safe parameters and execution logic.
There are two patterns for creating tools:
- Context-aware tools - Need access to Slack context and streaming (most common)
- Stateless tools - Pure functions that don’t need context
Tool Anatomy
Every tool has:- Description - Tells the AI when to use the tool
- Input schema - Zod schema defining parameters
- Execute function - The actual implementation
- Optional lifecycle hooks -
onInputStart,onInputComplete
Pattern 1: Context-Aware Tools
Use this pattern when your tool needs:- Access to Slack client (to send messages, reactions, etc.)
- Access to the message context (channel, user, timestamp)
- Ability to stream status updates
Factory Pattern
Context-aware tools use a factory function:Real Example: React Tool
Adds emoji reactions to Slack messages:server/lib/ai/tools/chat/react.ts
Pattern 2: Stateless Tools
Use this pattern for tools that:- Don’t need Slack context
- Are pure functions
- Call external APIs or perform calculations
Simple Export
Stateless tools are exported directly:Real Example: Get Weather
Fetches weather data from an external API:server/lib/ai/tools/chat/get-weather.ts
Zod Schema Guidelines
Use Descriptive Descriptions
The AI uses parameter descriptions to understand how to use the tool:Constrain Values
Use Zod’s validation to prevent invalid inputs:Provide Defaults
Return Value Conventions
Success Response
Return structured data the AI can understand:Error Response
Always include error details:Rich Data
For complex results, return structured objects:Task Tracking
Create Task
Start tracking when the tool begins:Update Task
Show progress during execution:Finish Task
Mark complete or failed:Registering Tools
Add your tool to the orchestrator:Example: Creating a New Tool from Scratch
Let’s create a tool that fetches a random programming joke:server/lib/ai/tools/chat/get-joke.ts
Best Practices
Clear Descriptions
Write clear tool descriptions that tell the AI:- What the tool does
- When to use it
- What parameters it needs
Validate Inputs
Use Zod schemas to validate all inputs:- Constrain string lengths
- Validate number ranges
- Require specific enum values
- Use
.nonempty()for arrays that must have items
Handle Errors Gracefully
Always catch and log errors:- Use try-catch blocks
- Log errors with context
- Return structured error responses
- Update task status to ‘error’
Log with Context
Include relevant information in logs:Stream Updates
Keep users informed with task updates:- Create task on start
- Update during execution
- Finish with success or error
Return Structured Data
Make it easy for the AI to use your results:- Use consistent response shapes
- Include
successboolean - Provide meaningful error messages
- Return rich data when helpful
Next Steps
- Review existing tools for inspiration
- Learn about code style guidelines
- Explore the Vercel AI SDK documentation