Skip to content

Commit 7da0d48

Browse files
authored
Merge pull request #243 from underctrl-io/ai
refactor: ai plugin improvements
2 parents 3f04de1 + 0adc37f commit 7da0d48

File tree

86 files changed

+4910
-660
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+4910
-660
lines changed

apps/test-bot/src/app/commands/(interactions)/+middleware.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { MessageFlags } from 'discord.js';
44
export function beforeExecute(ctx: MiddlewareContext) {
55
Logger.info('Pre-command middleware');
66

7+
console.log({ isAI: ctx.ai });
8+
79
const user = ctx.isInteraction() ? ctx.interaction.user : ctx.message.author;
810

911
if (ctx.commandName === 'prompt' && user.id === '159985870458322944') {

apps/test-bot/src/app/commands/(interactions)/confirmation.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,26 @@ import {
44
ChatInputCommandContext,
55
CommandData,
66
OnButtonKitClick,
7+
MessageCommandContext,
78
} from 'commandkit';
89
import { ButtonStyle, MessageFlags } from 'discord.js';
10+
import { AiConfig, AiContext } from '@commandkit/ai';
11+
import { z } from 'zod';
912

1013
export const command: CommandData = {
1114
name: 'confirmation',
1215
description: 'This is a confirm command.',
1316
};
1417

18+
export const aiConfig = {
19+
parameters: z.object({
20+
message: z
21+
.string()
22+
.describe('The message to be shown in the confirmation.'),
23+
}),
24+
description: 'Confirm an action with buttons.',
25+
} satisfies AiConfig;
26+
1527
const handleConfirm: OnButtonKitClick = async (interaction, context) => {
1628
await interaction.reply({
1729
content: 'The item was deleted successfully.',
@@ -47,3 +59,23 @@ export async function chatInput({ interaction }: ChatInputCommandContext) {
4759
components: [buttons],
4860
});
4961
}
62+
63+
export async function ai(ctx: MessageCommandContext) {
64+
const message = ctx.ai?.params?.message as string;
65+
66+
const buttons = (
67+
<ActionRow>
68+
<Button onClick={handleCancel} style={ButtonStyle.Primary}>
69+
Cancel
70+
</Button>
71+
<Button onClick={handleConfirm} style={ButtonStyle.Danger}>
72+
Confirm
73+
</Button>
74+
</ActionRow>
75+
);
76+
77+
await ctx.message.reply({
78+
content: message || 'There was no confirmation message provided.',
79+
components: [buttons],
80+
});
81+
}

apps/test-bot/src/app/commands/(leveling)/xp.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
import { ChatInputCommandContext, CommandData } from 'commandkit';
1+
import {
2+
ChatInputCommandContext,
3+
CommandData,
4+
MessageCommandContext,
5+
} from 'commandkit';
26
import { database } from '@/database/store.ts';
37
import { cacheTag } from '@commandkit/cache';
4-
import { AiConfig, AiContext } from '@commandkit/ai';
8+
import { AiConfig } from '@commandkit/ai';
59
import { z } from 'zod';
610

711
export const command: CommandData = {
812
name: 'xp',
913
description: 'This is an xp command.',
1014
};
1115

12-
export const aiConfig: AiConfig = {
16+
export const aiConfig = {
1317
description: 'Get the XP of a user in a guild.',
1418
parameters: z.object({
1519
guildId: z.string().describe('The ID of the guild.'),
1620
userId: z.string().describe('The ID of the user.'),
1721
}),
18-
};
22+
} satisfies AiConfig;
1923

2024
async function getUserXP(guildId: string, userId: string) {
2125
'use cache';
@@ -50,7 +54,7 @@ export async function chatInput({ interaction }: ChatInputCommandContext) {
5054
});
5155
}
5256

53-
export async function ai(ctx: AiContext) {
57+
export async function ai(ctx: MessageCommandContext) {
5458
const message = ctx.message;
5559

5660
if (!message.inGuild()) {
@@ -59,10 +63,9 @@ export async function ai(ctx: AiContext) {
5963
};
6064
}
6165

62-
const { guildId, userId } = ctx.params as {
63-
guildId: string;
64-
userId: string;
65-
};
66+
const { guildId, userId } = ctx.ai?.params as z.infer<
67+
(typeof aiConfig)['parameters']
68+
>;
6669

6770
const xp = await getUserXP(guildId, userId);
6871

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

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

1414
## AiContext
1515

16-
<GenerationInfo sourceFile="packages/ai/src/context.ts" sourceLine="17" packageName="@commandkit/ai" />
16+
<GenerationInfo sourceFile="packages/ai/src/context.ts" sourceLine="29" packageName="@commandkit/ai" />
1717

1818
Represents the context in which an AI command is executed.
1919
This includes the parameters passed to the command, the message that triggered it,
@@ -25,6 +25,7 @@ class AiContext<T extends Record<string, unknown> = Record<string, unknown>> {
2525
public message!: Message;
2626
public client!: Client;
2727
public commandkit!: CommandKit;
28+
public store = new Map<string, any>();
2829
constructor(options: AiContextOptions<T>)
2930
setParams(params: T) => void;
3031
}
@@ -52,6 +53,11 @@ The client instance associated with the AI command.
5253
<MemberInfo kind="property" type={`<a href='/docs/next/api-reference/commandkit/classes/command-kit#commandkit'>CommandKit</a>`} />
5354

5455
The CommandKit instance associated with the AI command.
56+
### store
57+
58+
<MemberInfo kind="property" type={``} />
59+
60+
A key-value store to hold additional data.
5561
### constructor
5662

5763
<MemberInfo kind="method" type={`(options: <a href='/docs/next/api-reference/ai/interfaces/ai-context-options#aicontextoptions'>AiContextOptions</a>&#60;T&#62;) => AiContext`} />

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

Lines changed: 9 additions & 3 deletions
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="78" packageName="@commandkit/ai" />
16+
<GenerationInfo sourceFile="packages/ai/src/plugin.ts" sourceLine="38" packageName="@commandkit/ai" />
1717

1818

1919

@@ -23,7 +23,8 @@ class AiPlugin extends RuntimePlugin<AiPluginOptions> {
2323
constructor(options: AiPluginOptions)
2424
activate(ctx: CommandKitPluginRuntime) => Promise<void>;
2525
deactivate(ctx: CommandKitPluginRuntime) => Promise<void>;
26-
onBeforeCommandsLoad(ctx: CommandKitPluginRuntime) => Promise<void>;
26+
executeAI(message: Message, commandkit?: CommandKit) => Promise<void>;
27+
onBeforeCommandsLoad() => Promise<void>;
2728
onAfterCommandsLoad(ctx: CommandKitPluginRuntime) => Promise<void>;
2829
}
2930
```
@@ -53,9 +54,14 @@ class AiPlugin extends RuntimePlugin<AiPluginOptions> {
5354
<MemberInfo kind="method" type={`(ctx: <a href='/docs/next/api-reference/commandkit/classes/command-kit-plugin-runtime#commandkitpluginruntime'>CommandKitPluginRuntime</a>) => Promise&#60;void&#62;`} />
5455

5556

57+
### executeAI
58+
59+
<MemberInfo kind="method" type={`(message: Message, commandkit?: <a href='/docs/next/api-reference/commandkit/classes/command-kit#commandkit'>CommandKit</a>) => Promise&#60;void&#62;`} />
60+
61+
Executes the AI for a given message.
5662
### onBeforeCommandsLoad
5763

58-
<MemberInfo kind="method" type={`(ctx: <a href='/docs/next/api-reference/commandkit/classes/command-kit-plugin-runtime#commandkitpluginruntime'>CommandKitPluginRuntime</a>) => Promise&#60;void&#62;`} />
64+
<MemberInfo kind="method" type={`() => Promise&#60;void&#62;`} />
5965

6066

6167
### onAfterCommandsLoad

apps/website/docs/api-reference/ai/functions/ai.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
## ai
1515

16-
<GenerationInfo sourceFile="packages/ai/src/index.ts" sourceLine="9" packageName="@commandkit/ai" />
16+
<GenerationInfo sourceFile="packages/ai/src/index.ts" sourceLine="36" packageName="@commandkit/ai" />
1717

1818
Defines the AI plugin for the application.
1919

apps/website/docs/api-reference/ai/functions/configure-ai.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
## configureAI
1515

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

1818
Configures the AI plugin with the provided options.
1919
This function allows you to set a message filter, select an AI model, and generate a system prompt.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: "CreateSystemPrompt"
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+
## createSystemPrompt
15+
16+
<GenerationInfo sourceFile="packages/ai/src/system-prompt.ts" sourceLine="7" packageName="@commandkit/ai" />
17+
18+
Creates the default system prompt for the AI bot based on the provided message context.
19+
This prompt includes the bot's role, current channel information, and response guidelines.
20+
21+
```ts title="Signature"
22+
function createSystemPrompt(message: Message): string
23+
```
24+
Parameters
25+
26+
### message
27+
28+
<MemberInfo kind="parameter" type={`Message`} />
29+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: "CreateTool"
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+
## createTool
15+
16+
<GenerationInfo sourceFile="packages/ai/src/tools/common/index.ts" sourceLine="88" packageName="@commandkit/ai" />
17+
18+
Creates a new AI tool with the specified configuration.
19+
This function wraps the underlying AI library's tool creation with additional
20+
context management and parameter validation.
21+
22+
23+
24+
*Example*
25+
26+
```typescript
27+
const myTool = createTool({
28+
name: 'calculate',
29+
description: 'Performs basic arithmetic calculations',
30+
parameters: z.object({
31+
operation: z.enum(['add', 'subtract']),
32+
a: z.number(),
33+
b: z.number(),
34+
}),
35+
execute: async (ctx, params) => {
36+
return params.operation === 'add'
37+
? params.a + params.b
38+
: params.a - params.b;
39+
},
40+
});
41+
```
42+
43+
```ts title="Signature"
44+
function createTool<T extends ToolParameterType, R = unknown>(options: CreateToolOptions<T, R>): void
45+
```
46+
Parameters
47+
48+
### options
49+
50+
<MemberInfo kind="parameter" type={`<a href='/docs/next/api-reference/ai/interfaces/create-tool-options#createtooloptions'>CreateToolOptions</a>&#60;T, R&#62;`} />
51+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: "GetAIConfig"
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+
## getAIConfig
15+
16+
<GenerationInfo sourceFile="packages/ai/src/configure.ts" sourceLine="119" packageName="@commandkit/ai" />
17+
18+
Retrieves the current AI configuration.
19+
20+
```ts title="Signature"
21+
function getAIConfig(): Required<ConfigureAI>
22+
```

0 commit comments

Comments
 (0)