Skip to content

Commit 3656d6a

Browse files
committed
feat!: ai sdk 5
1 parent 1e83e1d commit 3656d6a

File tree

11 files changed

+110
-150
lines changed

11 files changed

+110
-150
lines changed

apps/test-bot/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"node": "node"
1111
},
1212
"dependencies": {
13-
"@ai-sdk/google": "^1.2.19",
13+
"@ai-sdk/google": "^2.0.8",
1414
"@commandkit/ai": "workspace:*",
1515
"@commandkit/cache": "workspace:*",
1616
"@commandkit/devtools": "workspace:*",
@@ -27,4 +27,4 @@
2727
"@types/ms": "^2.1.0",
2828
"tsx": "^4.7.0"
2929
}
30-
}
30+
}

packages/ai/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"typescript": "catalog:build"
3636
},
3737
"dependencies": {
38-
"ai": "^4.3.16",
39-
"zod": "^3.25.48"
38+
"ai": "^5.0.22",
39+
"zod": "^4.1.0"
4040
}
41-
}
41+
}

packages/ai/src/plugin.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import { AiPluginOptions, CommandTool } from './types';
33
import CommandKit, { getCommandKit, Logger } from 'commandkit';
44
import { AiContext } from './context';
55
import { Collection, Events, Message } from 'discord.js';
6-
import { tool, Tool, generateText } from 'ai';
6+
import { tool, Tool, generateText, stepCountIs, ModelMessage } from 'ai';
77
import { getAiWorkerContext, runInAiWorkerContext } from './ai-context-worker';
88
import { getAvailableCommands } from './tools/get-available-commands';
99
import { getChannelById } from './tools/get-channel-by-id';
1010
import { getCurrentClientInfo } from './tools/get-current-client-info';
1111
import { getGuildById } from './tools/get-guild-by-id';
1212
import { getUserById } from './tools/get-user-by-id';
13-
import { AiMessage, getAIConfig } from './configure';
13+
import { getAIConfig } from './configure';
1414
import { augmentCommandKit } from './augmentation';
15+
import { ToolParameterType } from './tools/common';
1516

1617
/**
1718
* Represents the configuration options for the AI plugin scoped to a specific command.
@@ -24,7 +25,7 @@ export interface AiConfig {
2425
/**
2526
* A zod schema defining the parameters that the AI command accepts.
2627
*/
27-
parameters: any;
28+
inputSchema: ToolParameterType;
2829
}
2930

3031
const defaultTools: Record<string, Tool> = {
@@ -99,11 +100,19 @@ export class AiPlugin extends RuntimePlugin<AiPluginOptions> {
99100
await runInAiWorkerContext(ctx, message, async () => {
100101
const systemPrompt = await prepareSystemPrompt(ctx, message);
101102
const prompt = await preparePrompt(ctx, message);
102-
const { model, abortSignal, maxSteps, ...modelOptions } =
103-
await selectAiModel(ctx, message);
104-
105-
const promptOrMessage =
106-
typeof prompt === 'string' ? { prompt } : { messages: prompt };
103+
const {
104+
model,
105+
abortSignal,
106+
stopWhen,
107+
prompt: _prompt,
108+
...modelOptions
109+
} = await selectAiModel(ctx, message);
110+
111+
const promptOrMessage = (
112+
typeof prompt === 'string' ? { prompt } : { messages: prompt }
113+
) as
114+
| { prompt: string; messages: never }
115+
| { messages: ModelMessage[]; prompt: never };
107116

108117
await onProcessingStart(ctx, message);
109118

@@ -112,7 +121,7 @@ export class AiPlugin extends RuntimePlugin<AiPluginOptions> {
112121
model,
113122
abortSignal: abortSignal ?? AbortSignal.timeout(60_000),
114123
system: systemPrompt,
115-
maxSteps: maxSteps ?? 5,
124+
stopWhen: stopWhen ?? stepCountIs(5),
116125
...modelOptions,
117126
tools: {
118127
// Include built-in least significant tools if not disabled
@@ -181,12 +190,12 @@ export class AiPlugin extends RuntimePlugin<AiPluginOptions> {
181190

182191
const cmdTool = tool({
183192
description,
184-
parameters: cmd.data.aiConfig.parameters,
193+
inputSchema: cmd.data.aiConfig.inputSchema,
185194
async execute(params) {
186195
const config = getAIConfig();
187196
const ctx = getAiWorkerContext();
188197

189-
ctx.ctx.setParams(params);
198+
ctx.ctx.setParams(params as Record<string, unknown>);
190199

191200
try {
192201
const target = await commandkit.commandHandler.prepareCommandRun(

packages/ai/src/tools/common/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Awaitable<T> = T | Promise<T>;
1414
* Type representing the parameters schema for AI tools.
1515
* Extracted from the first parameter of the `tool` function from the 'ai' library.
1616
*/
17-
export type ToolParameterType = Parameters<typeof tool>[0]['parameters'];
17+
export type ToolParameterType = z.ZodType | Schema<any>;
1818

1919
/**
2020
* Utility type that infers the TypeScript type from a tool parameter schema.
@@ -39,7 +39,7 @@ export interface CreateToolOptions<T extends ToolParameterType, R = unknown> {
3939
/** A human-readable description of what the tool does */
4040
description: string;
4141
/** The parameter schema that defines the tool's input structure */
42-
parameters: T;
42+
inputSchema: T;
4343
/** The function that executes when the tool is called */
4444
execute: ToolExecuteFunction<T, R>;
4545
}
@@ -49,12 +49,12 @@ export interface CreateToolOptions<T extends ToolParameterType, R = unknown> {
4949
* @template T - The parameter schema type
5050
* @template R - The return type of the function
5151
* @param ctx - The AI context containing request and response information
52-
* @param parameters - The validated parameters passed to the tool
52+
* @param inputSchema - The validated inputSchema passed to the tool
5353
* @returns The result of the tool execution, which can be synchronous or asynchronous
5454
*/
5555
export type ToolExecuteFunction<T extends ToolParameterType, R> = (
5656
ctx: AiContext,
57-
parameters: InferParameters<T>,
57+
inputSchema: InferParameters<T>,
5858
) => Awaitable<R>;
5959

6060
/**
@@ -92,11 +92,11 @@ export function createTool<T extends ToolParameterType, R = unknown>(
9292
const _tool = tool<T, R>({
9393
name: options.name,
9494
description: options.description,
95-
parameters: options.parameters,
95+
inputSchema: options.inputSchema,
9696
async execute(params) {
9797
const { ctx } = getAiWorkerContext();
9898

99-
ctx.setParams(params);
99+
ctx.setParams(params as Record<string, unknown>);
100100

101101
return options.execute(ctx, params as InferParameters<T>);
102102
},

packages/ai/src/tools/get-available-commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createTool } from './common';
44
export const getAvailableCommands = createTool({
55
description: 'Get a list of all available commands.',
66
name: 'getAvailableCommands',
7-
parameters: z.object({}),
7+
inputSchema: z.object({}),
88
execute(ctx, params) {
99
const { commandkit } = ctx;
1010

packages/ai/src/tools/get-channel-by-id.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Logger } from 'commandkit';
55
export const getChannelById = createTool({
66
description: 'Get a channel by its ID.',
77
name: 'getChannelById',
8-
parameters: z.object({
8+
inputSchema: z.object({
99
channelId: z.string().describe('The ID of the channel to retrieve.'),
1010
}),
1111
async execute(ctx, params) {

packages/ai/src/tools/get-current-client-info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createTool } from './common';
44
export const getCurrentClientInfo = createTool({
55
name: 'getCurrentClientInfo',
66
description: 'Get information about the current discord bot user',
7-
parameters: z.object({}),
7+
inputSchema: z.object({}),
88
execute: async (ctx, params) => {
99
const { client } = ctx;
1010
const user = client.user;

packages/ai/src/tools/get-guild-by-id.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Logger } from 'commandkit';
55
export const getGuildById = createTool({
66
description: 'Get a guild by its ID.',
77
name: 'getGuildById',
8-
parameters: z.object({
8+
inputSchema: z.object({
99
guildId: z.string().describe('The ID of the guild to retrieve.'),
1010
}),
1111
async execute(ctx, params) {

packages/ai/src/tools/get-user-by-id.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Logger } from 'commandkit';
55
export const getUserById = createTool({
66
description: 'Get a user by their ID.',
77
name: 'getUserById',
8-
parameters: z.object({
8+
inputSchema: z.object({
99
userId: z.string().describe('The ID of the user to retrieve.'),
1010
}),
1111
async execute(ctx, params) {

packages/ai/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface AiPluginOptions {}
4444
* Extracts the AI configuration params.
4545
*/
4646
export type ExtractAiConfig<T extends Record<string, unknown>> =
47-
T extends AiConfig ? InferParameters<T['parameters']> : T;
47+
T extends AiConfig ? InferParameters<T['inputSchema']> : T;
4848

4949
/**
5050
* Represents the context in which an AI command is executed.

0 commit comments

Comments
 (0)