Skip to content

Commit a1ed97a

Browse files
Copilottwlite
andauthored
feat(ai): export defaultTools from @commandkit/ai for selective tool configuration (#598)
* Initial plan * Export defaultTools from @commandkit/ai and update docs Co-authored-by: twlite <46562212+twlite@users.noreply.github.com> * Rename instance property to avoid shadowing defaultTools constant Co-authored-by: twlite <46562212+twlite@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: twlite <46562212+twlite@users.noreply.github.com>
1 parent 162afe2 commit a1ed97a

5 files changed

Lines changed: 128 additions & 19 deletions

File tree

apps/website/docs/api-reference/ai/classes/ai-plugin.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## AiPlugin
1515

16-
<GenerationInfo sourceFile="packages/ai/src/plugin.ts" sourceLine="43" packageName="@commandkit/ai" />
16+
<GenerationInfo sourceFile="packages/ai/src/plugin.ts" sourceLine="66" packageName="@commandkit/ai" />
1717

1818

1919

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: "DefaultTools"
3+
isDefaultIndex: false
4+
generated: true
5+
---
6+
7+
import MemberInfo from '@site/src/components/MemberInfo';
8+
import GenerationInfo from '@site/src/components/GenerationInfo';
9+
import MemberDescription from '@site/src/components/MemberDescription';
10+
11+
<!-- This file was generated from the CommandKit source. Do not modify. Instead, re-run the "docgen" script -->
12+
13+
14+
## defaultTools
15+
16+
<GenerationInfo sourceFile="packages/ai/src/plugin.ts" sourceLine="56" packageName="@commandkit/ai" />
17+
18+
A record of all built-in tools provided by the AI plugin.
19+
These tools are automatically available to the AI unless `disableBuiltInTools` is set to `true`.
20+
21+
You can use this to selectively include specific built-in tools when `disableBuiltInTools` is enabled:
22+
23+
24+
25+
*Example*
26+
27+
```ts
28+
import { configureAI, defaultTools } from '@commandkit/ai';
29+
30+
configureAI({
31+
disableBuiltInTools: true,
32+
selectAiModel: async (ctx, message) => ({
33+
model: google.languageModel('gemini-2.0-flash'),
34+
// Only include specific built-in tools
35+
tools: {
36+
getAvailableCommands: defaultTools.getAvailableCommands,
37+
getUserById: defaultTools.getUserById,
38+
},
39+
}),
40+
});
41+
```
42+

apps/website/docs/guide/05-official-plugins/01-commandkit-ai.mdx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,12 @@ CommandKit provides built-in tools that the AI can use automatically:
258258
- **`getCurrentClientInfo`** - Gets information about the bot
259259
- **`getGuildById`** - Fetches server information by ID
260260
- **`getUserById`** - Fetches user information by ID
261+
- **`getMemberById`** - Fetches guild member information by ID
262+
- **`createEmbed`** - Creates and sends an embed message
261263

262-
You can disable these tools if needed:
264+
### Disabling built-in tools
265+
266+
You can disable all built-in tools if needed:
263267

264268
```ts
265269
configureAI({
@@ -268,6 +272,46 @@ configureAI({
268272
});
269273
```
270274

275+
### Selectively enabling built-in tools
276+
277+
In many cases, you may not need all built-in tools. You can selectively enable
278+
specific tools by disabling built-in tools and then manually adding only the
279+
ones you need using the exported `defaultTools` record:
280+
281+
```ts title="src/ai.ts"
282+
import { createGoogleGenerativeAI } from '@ai-sdk/google';
283+
import { configureAI, defaultTools } from '@commandkit/ai';
284+
285+
const google = createGoogleGenerativeAI({
286+
apiKey: process.env.GOOGLE_API_KEY,
287+
});
288+
289+
configureAI({
290+
// Disable all built-in tools
291+
disableBuiltInTools: true,
292+
293+
selectAiModel: async (ctx, message) => ({
294+
model: google.languageModel('gemini-2.0-flash'),
295+
maxSteps: 5,
296+
temperature: 0.7,
297+
// Manually add only the tools you need
298+
tools: {
299+
getAvailableCommands: defaultTools.getAvailableCommands,
300+
getUserById: defaultTools.getUserById,
301+
getGuildById: defaultTools.getGuildById,
302+
// Omit tools you don't want, like createEmbed
303+
},
304+
}),
305+
306+
messageFilter: async (commandkit, message) => {
307+
return message.mentions.users.has(message.client.user.id);
308+
},
309+
});
310+
```
311+
312+
This approach gives you full control over which built-in tools are available to
313+
the AI, making your implementation cleaner and more intentional.
314+
271315
## Creating custom tools
272316

273317
Extend the AI's capabilities by creating custom tools:

apps/website/vercel.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"$schema": "https://openapi.vercel.sh/vercel.json",
3-
"redirects": [
4-
{
5-
"source": "/discord",
6-
"destination": "https://discord.gg/T4faJeH84A",
7-
"permanent": true
8-
},
9-
{
10-
"source": "/github",
11-
"destination": "https://github.com/neplextech/commandkit",
12-
"permanent": true
13-
}
14-
]
15-
}
2+
"$schema": "https://openapi.vercel.sh/vercel.json",
3+
"redirects": [
4+
{
5+
"source": "/discord",
6+
"destination": "https://discord.gg/T4faJeH84A",
7+
"permanent": true
8+
},
9+
{
10+
"source": "/github",
11+
"destination": "https://github.com/neplextech/commandkit",
12+
"permanent": true
13+
}
14+
]
15+
}

packages/ai/src/plugin.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,30 @@ export interface AiConfig<T extends ToolParameterType = ToolParameterType> {
3030
inputSchema: T;
3131
}
3232

33-
const defaultTools: Record<string, Tool> = {
33+
/**
34+
* A record of all built-in tools provided by the AI plugin.
35+
* These tools are automatically available to the AI unless `disableBuiltInTools` is set to `true`.
36+
*
37+
* You can use this to selectively include specific built-in tools when `disableBuiltInTools` is enabled:
38+
*
39+
* @example
40+
* ```ts
41+
* import { configureAI, defaultTools } from '@commandkit/ai';
42+
*
43+
* configureAI({
44+
* disableBuiltInTools: true,
45+
* selectAiModel: async (ctx, message) => ({
46+
* model: google.languageModel('gemini-2.0-flash'),
47+
* // Only include specific built-in tools
48+
* tools: {
49+
* getAvailableCommands: defaultTools.getAvailableCommands,
50+
* getUserById: defaultTools.getUserById,
51+
* },
52+
* }),
53+
* });
54+
* ```
55+
*/
56+
export const defaultTools: Record<string, Tool> = {
3457
getAvailableCommands,
3558
getChannelById,
3659
getCurrentClientInfo,
@@ -43,7 +66,7 @@ const defaultTools: Record<string, Tool> = {
4366
export class AiPlugin extends RuntimePlugin<AiPluginOptions> {
4467
public readonly name = 'AiPlugin';
4568
private toolsRecord: Record<string, Tool> = {};
46-
private defaultTools = defaultTools;
69+
private builtInTools = defaultTools;
4770
private onMessageFunc: ((message: Message) => Promise<void>) | null = null;
4871

4972
public constructor(options: AiPluginOptions) {
@@ -125,7 +148,7 @@ export class AiPlugin extends RuntimePlugin<AiPluginOptions> {
125148
...modelOptions,
126149
tools: {
127150
// Include built-in least significant tools if not disabled
128-
...(!disableBuiltInTools && this.defaultTools),
151+
...(!disableBuiltInTools && this.builtInTools),
129152
// include tools added by configureAI()
130153
// this should be able to override built-in tools
131154
...modelOptions.tools,

0 commit comments

Comments
 (0)