Skip to content

Commit 0264992

Browse files
committed
feat: ai plugin
1 parent f8d81c3 commit 0264992

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

.github/workflows/publish-dev.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ jobs:
5959
"@commandkit/devtools:packages/devtools"
6060
"@commandkit/cache:packages/cache"
6161
"@commandkit/analytics:packages/analytics"
62+
"@commandkit/ai:packages/ai"
6263
)
6364
6465
for entry in "${PACKAGES[@]}"; do
@@ -80,6 +81,7 @@ jobs:
8081
"@commandkit/devtools"
8182
"@commandkit/cache"
8283
"@commandkit/analytics"
84+
"@commandkit/ai"
8385
)
8486
8587
for pkg in "${PACKAGES[@]}"; do
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: AI Plugin
3+
description: Learn how to use the AI plugin to power your bot's commands and events.
4+
---
5+
6+
## AI Plugin
7+
8+
The AI plugin allows you to execute your bot commands using large language models. This enables you to use your bot's features entirely through natural language.
9+
10+
Please refer to the [AI Powered Commands](../../13-ai-powered-commands/01-introduction.mdx) guide for more details.
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
title: AI Powered Commands
3+
description: Learn how to use a large language model to power your commands.
4+
---
5+
6+
## Introduction
7+
8+
CommandKit's `@commandkit/ai` plugin allows you to execute your bot commands using large language models. This enables you to use your bot's features entirely through natural language.
9+
10+
:::warning
11+
This is an experimental feature and is subject to change.
12+
:::
13+
14+
## Installation
15+
16+
```bash
17+
npm install @commandkit/ai
18+
```
19+
20+
## Usage
21+
22+
```typescript title="commandkit.config.ts"
23+
import { defineConfig } from 'commandkit';
24+
import { ai } from '@commandkit/ai';
25+
26+
export default defineConfig({
27+
plugins: [ai()],
28+
});
29+
```
30+
31+
## Setting up the AI model
32+
33+
CommandKit allows you to dynamically specify the AI model to use for your bot.
34+
35+
```typescript title="src/ai.ts"
36+
import { createGoogleGenerativeAI } from '@ai-sdk/google';
37+
import { configureAI } from '@commandkit/ai';
38+
39+
const google = createGoogleGenerativeAI({
40+
apiKey: process.env.GOOGLE_API_KEY,
41+
});
42+
43+
const model = google.languageModel('gemini-2.0-flash');
44+
45+
configureAI({
46+
// commandkit will call this function
47+
// to determine which AI model to use
48+
selectAiModel: async () => {
49+
return { model };
50+
},
51+
messageFilter: async (message) => {
52+
// only respond to messages in guilds that mention the bot
53+
return (
54+
message.inGuild() && message.mentions.users.has(message.client.user.id)
55+
);
56+
},
57+
});
58+
```
59+
60+
Now you can simply import this file in your `app.ts`,
61+
62+
```ts title="app.ts"
63+
import { Client } from 'discord.js';
64+
// simply import the ai file
65+
import './ai';
66+
67+
const client = new Client({...});
68+
69+
export default client;
70+
```
71+
72+
## Creating AI commands
73+
74+
AI commands can be created by exporting a function called `ai` from your command file. You can also export `aiConfig` object along with the `ai` function to specify the parameters for the command.
75+
76+
```typescript title="src/commands/balance.ts"
77+
import { ApplicationCommandOptionType } from 'discord.js';
78+
import { CommandData, ChatInputCommand } from 'commandkit';
79+
import { AiCommand } from '@commandkit/ai';
80+
import { z } from 'zod';
81+
82+
export const command: CommandData = {
83+
name: 'balance',
84+
description: 'Get the current balance of the user.',
85+
options: [
86+
{
87+
name: 'user',
88+
description: 'The user to get the balance for.',
89+
type: ApplicationCommandOptionType.User,
90+
},
91+
],
92+
};
93+
94+
export const aiConfig: AiConfig = {
95+
parameters: z.object({
96+
userId: z.string().describe('The ID of the user to get the balance of'),
97+
}),
98+
};
99+
100+
export const chatInput: ChatInputCommand = async (ctx) => {
101+
const { interaction } = ctx;
102+
const user = interaction.options.getUser('user');
103+
const balance = await db.getBalance(user.id);
104+
105+
await interaction.reply({
106+
content: `The balance of ${user.username} is ${balance}`,
107+
});
108+
};
109+
110+
// AI will call this function to get the balance of the user
111+
export const ai: AiCommand = async (ctx) => {
112+
const { userId } = ctx.params;
113+
const balance = await db.getBalance(userId);
114+
115+
// return object with the balance
116+
return {
117+
userId,
118+
balance,
119+
};
120+
};
121+
```
122+
123+
Now, you can simply mention the bot in a message to get the balance of the user. Eg:
124+
125+
```text
126+
@bot what is the balance of @user?
127+
```
128+
129+
AI can also call multiple commands in a single message. Eg:
130+
131+
```text
132+
@bot show me the basic details of the user @user and also include the balance of that user.
133+
```
134+
135+
The above prompt will call the built-in `getUserInfo` tool and the `balance` command.

0 commit comments

Comments
 (0)