@commandkit/ai plugin integrates AI capabilities into your CommandKit bot using the Vercel AI SDK. Enable natural language command execution, conversational interactions, and intelligent responses powered by any AI model.
Installation
Setup
Import and configure the plugin
Import the AI plugin and configure it with your preferred AI model.
src/index.ts
Create AI-enabled commands
Define commands that can be executed by AI using natural language.
src/commands/weather.ts
Configuration
Customize AI behavior usingconfigureAI():
Function that selects the AI model to use for processing messages.Parameters:
ctx- AI context objectmessage- Discord message that triggered the AI
model- The AI model from Vercel AI SDKmaxSteps- Maximum tool call steps (optional)tools- Additional custom tools (optional)- Other AI SDK options
Filter which messages should be processed by AI.Default: Processes messages that mention the bot and don’t start with a command prefix.
Customize the system prompt sent to the AI model.
Customize the user prompt or conversation history.
Handle the AI response before sending it to Discord.
Handle errors during AI processing.
Disable built-in tools like
getAvailableCommands, getUserById, etc.Built-in Tools
The AI plugin includes several built-in tools accessible from@commandkit/ai:
Custom Tools
Add custom tools for the AI to use:Context API
Access AI context in your commands:src/commands/example.ts
Hooks
Advanced Examples
Multi-step Commands
src/commands/playlist.ts
Context-aware Responses
src/index.ts
Best Practices
Validate Inputs
Use Zod schemas to validate AI command parameters and ensure type safety.
Set Step Limits
Configure
maxSteps to prevent excessive tool calls and control costs.Handle Errors
Always implement
onError to gracefully handle AI failures.Filter Messages
Use
messageFilter to limit AI processing to relevant channels or users.TypeScript Support
Full type inference for AI command parameters:API Reference
ai(options?)
Creates the AI plugin instance.
Returns: [AiPlugin, AiCliPlugin]
configureAI(config)
Configures global AI behavior. See Configuration for all options.
useAI()
Returns the active AI plugin instance.
Throws: Error if AI plugin is not registered.
useAIContext()
Returns the current AI context within a command execution.
Returns: AiContext
executeAI(message)
Manually triggers AI processing for a message.
Parameters:
message- Discord.js Message object
Promise<void>