Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/test-bot/src/app/commands/(interactions)/+middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { MessageFlags } from 'discord.js';
export function beforeExecute(ctx: MiddlewareContext) {
Logger.info('Pre-command middleware');

console.log({ isAI: ctx.ai });

const user = ctx.isInteraction() ? ctx.interaction.user : ctx.message.author;

if (ctx.commandName === 'prompt' && user.id === '159985870458322944') {
Expand Down
32 changes: 32 additions & 0 deletions apps/test-bot/src/app/commands/(interactions)/confirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ import {
ChatInputCommandContext,
CommandData,
OnButtonKitClick,
MessageCommandContext,
} from 'commandkit';
import { ButtonStyle, MessageFlags } from 'discord.js';
import { AiConfig, AiContext } from '@commandkit/ai';
import { z } from 'zod';

export const command: CommandData = {
name: 'confirmation',
description: 'This is a confirm command.',
};

export const aiConfig = {
parameters: z.object({
message: z
.string()
.describe('The message to be shown in the confirmation.'),
}),
description: 'Confirm an action with buttons.',
} satisfies AiConfig;

const handleConfirm: OnButtonKitClick = async (interaction, context) => {
await interaction.reply({
content: 'The item was deleted successfully.',
Expand Down Expand Up @@ -47,3 +59,23 @@ export async function chatInput({ interaction }: ChatInputCommandContext) {
components: [buttons],
});
}

export async function ai(ctx: MessageCommandContext) {
const message = ctx.ai?.params?.message as string;

const buttons = (
<ActionRow>
<Button onClick={handleCancel} style={ButtonStyle.Primary}>
Cancel
</Button>
<Button onClick={handleConfirm} style={ButtonStyle.Danger}>
Confirm
</Button>
</ActionRow>
);

await ctx.message.reply({
content: message || 'There was no confirmation message provided.',
components: [buttons],
});
}
21 changes: 12 additions & 9 deletions apps/test-bot/src/app/commands/(leveling)/xp.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { ChatInputCommandContext, CommandData } from 'commandkit';
import {
ChatInputCommandContext,
CommandData,
MessageCommandContext,
} from 'commandkit';
import { database } from '@/database/store.ts';
import { cacheTag } from '@commandkit/cache';
import { AiConfig, AiContext } from '@commandkit/ai';
import { AiConfig } from '@commandkit/ai';
import { z } from 'zod';

export const command: CommandData = {
name: 'xp',
description: 'This is an xp command.',
};

export const aiConfig: AiConfig = {
export const aiConfig = {
description: 'Get the XP of a user in a guild.',
parameters: z.object({
guildId: z.string().describe('The ID of the guild.'),
userId: z.string().describe('The ID of the user.'),
}),
};
} satisfies AiConfig;

async function getUserXP(guildId: string, userId: string) {
'use cache';
Expand Down Expand Up @@ -50,7 +54,7 @@ export async function chatInput({ interaction }: ChatInputCommandContext) {
});
}

export async function ai(ctx: AiContext) {
export async function ai(ctx: MessageCommandContext) {
const message = ctx.message;

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

const { guildId, userId } = ctx.params as {
guildId: string;
userId: string;
};
const { guildId, userId } = ctx.ai?.params as z.infer<
(typeof aiConfig)['parameters']
>;

const xp = await getUserXP(guildId, userId);

Expand Down
8 changes: 7 additions & 1 deletion apps/website/docs/api-reference/ai/classes/ai-context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## AiContext

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

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

The CommandKit instance associated with the AI command.
### store

<MemberInfo kind="property" type={``} />

A key-value store to hold additional data.
### constructor

<MemberInfo kind="method" type={`(options: <a href='/docs/next/api-reference/ai/interfaces/ai-context-options#aicontextoptions'>AiContextOptions</a>&#60;T&#62;) => AiContext`} />
Expand Down
12 changes: 9 additions & 3 deletions apps/website/docs/api-reference/ai/classes/ai-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## AiPlugin

<GenerationInfo sourceFile="packages/ai/src/plugin.ts" sourceLine="78" packageName="@commandkit/ai" />
<GenerationInfo sourceFile="packages/ai/src/plugin.ts" sourceLine="38" packageName="@commandkit/ai" />



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


### executeAI

<MemberInfo kind="method" type={`(message: Message, commandkit?: <a href='/docs/next/api-reference/commandkit/classes/command-kit#commandkit'>CommandKit</a>) => Promise&#60;void&#62;`} />

Executes the AI for a given message.
### onBeforeCommandsLoad

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


### onAfterCommandsLoad
Expand Down
2 changes: 1 addition & 1 deletion apps/website/docs/api-reference/ai/functions/ai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## ai

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

Defines the AI plugin for the application.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## configureAI

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

Configures the AI plugin with the provided options.
This function allows you to set a message filter, select an AI model, and generate a system prompt.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: "CreateSystemPrompt"
isDefaultIndex: false
generated: true
---

import MemberInfo from '@site/src/components/MemberInfo';
import GenerationInfo from '@site/src/components/GenerationInfo';
import MemberDescription from '@site/src/components/MemberDescription';

<!-- This file was generated from the CommandKit source. Do not modify. Instead, re-run the "docgen" script -->


## createSystemPrompt

<GenerationInfo sourceFile="packages/ai/src/system-prompt.ts" sourceLine="7" packageName="@commandkit/ai" />

Creates the default system prompt for the AI bot based on the provided message context.
This prompt includes the bot's role, current channel information, and response guidelines.

```ts title="Signature"
function createSystemPrompt(message: Message): string
```
Parameters

### message

<MemberInfo kind="parameter" type={`Message`} />

51 changes: 51 additions & 0 deletions apps/website/docs/api-reference/ai/functions/create-tool.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: "CreateTool"
isDefaultIndex: false
generated: true
---

import MemberInfo from '@site/src/components/MemberInfo';
import GenerationInfo from '@site/src/components/GenerationInfo';
import MemberDescription from '@site/src/components/MemberDescription';

<!-- This file was generated from the CommandKit source. Do not modify. Instead, re-run the "docgen" script -->


## createTool

<GenerationInfo sourceFile="packages/ai/src/tools/common/index.ts" sourceLine="88" packageName="@commandkit/ai" />

Creates a new AI tool with the specified configuration.
This function wraps the underlying AI library's tool creation with additional
context management and parameter validation.



*Example*

```typescript
const myTool = createTool({
name: 'calculate',
description: 'Performs basic arithmetic calculations',
parameters: z.object({
operation: z.enum(['add', 'subtract']),
a: z.number(),
b: z.number(),
}),
execute: async (ctx, params) => {
return params.operation === 'add'
? params.a + params.b
: params.a - params.b;
},
});
```

```ts title="Signature"
function createTool<T extends ToolParameterType, R = unknown>(options: CreateToolOptions<T, R>): void
```
Parameters

### options

<MemberInfo kind="parameter" type={`<a href='/docs/next/api-reference/ai/interfaces/create-tool-options#createtooloptions'>CreateToolOptions</a>&#60;T, R&#62;`} />

22 changes: 22 additions & 0 deletions apps/website/docs/api-reference/ai/functions/get-aiconfig.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: "GetAIConfig"
isDefaultIndex: false
generated: true
---

import MemberInfo from '@site/src/components/MemberInfo';
import GenerationInfo from '@site/src/components/GenerationInfo';
import MemberDescription from '@site/src/components/MemberDescription';

<!-- This file was generated from the CommandKit source. Do not modify. Instead, re-run the "docgen" script -->


## getAIConfig

<GenerationInfo sourceFile="packages/ai/src/configure.ts" sourceLine="119" packageName="@commandkit/ai" />

Retrieves the current AI configuration.

```ts title="Signature"
function getAIConfig(): Required<ConfigureAI>
```
22 changes: 22 additions & 0 deletions apps/website/docs/api-reference/ai/functions/use-ai.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: "UseAI"
isDefaultIndex: false
generated: true
---

import MemberInfo from '@site/src/components/MemberInfo';
import GenerationInfo from '@site/src/components/GenerationInfo';
import MemberDescription from '@site/src/components/MemberDescription';

<!-- This file was generated from the CommandKit source. Do not modify. Instead, re-run the "docgen" script -->


## useAI

<GenerationInfo sourceFile="packages/ai/src/index.ts" sourceLine="18" packageName="@commandkit/ai" />

Fetches the AI plugin instance.

```ts title="Signature"
function useAI(): void
```
22 changes: 22 additions & 0 deletions apps/website/docs/api-reference/ai/functions/use-aicontext.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: "UseAIContext"
isDefaultIndex: false
generated: true
---

import MemberInfo from '@site/src/components/MemberInfo';
import GenerationInfo from '@site/src/components/GenerationInfo';
import MemberDescription from '@site/src/components/MemberDescription';

<!-- This file was generated from the CommandKit source. Do not modify. Instead, re-run the "docgen" script -->


## useAIContext

<GenerationInfo sourceFile="packages/ai/src/index.ts" sourceLine="10" packageName="@commandkit/ai" />

Retrieves the AI context.

```ts title="Signature"
function useAIContext(): void
```
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## AiConfig

<GenerationInfo sourceFile="packages/ai/src/plugin.ts" sourceLine="22" packageName="@commandkit/ai" />
<GenerationInfo sourceFile="packages/ai/src/plugin.ts" sourceLine="19" packageName="@commandkit/ai" />

Represents the configuration options for the AI plugin scoped to a specific command.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import MemberDescription from '@site/src/components/MemberDescription';

## AiContextOptions

<GenerationInfo sourceFile="packages/ai/src/context.ts" sourceLine="4" packageName="@commandkit/ai" />

<GenerationInfo sourceFile="packages/ai/src/context.ts" sourceLine="7" packageName="@commandkit/ai" />

Options for the AI context.

```ts title="Signature"
interface AiContextOptions<T extends Record<string, unknown> = Record<string, unknown>> {
Expand All @@ -31,17 +31,17 @@ interface AiContextOptions<T extends Record<string, unknown> = Record<string, un

<MemberInfo kind="property" type={`Message`} />


The message that triggered the AI command.
### params

<MemberInfo kind="property" type={`T`} />


The parameters passed to the AI command.
### commandkit

<MemberInfo kind="property" type={`<a href='/docs/next/api-reference/commandkit/classes/command-kit#commandkit'>CommandKit</a>`} />


The CommandKit instance associated with the AI command.


</div>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';

## AiPluginOptions

<GenerationInfo sourceFile="packages/ai/src/types.ts" sourceLine="33" packageName="@commandkit/ai" />
<GenerationInfo sourceFile="packages/ai/src/types.ts" sourceLine="43" packageName="@commandkit/ai" />

Options for the AI plugin.

Expand Down
Loading