diff --git a/langchain/src/agents/tests/discord.int.test.ts b/langchain/src/agents/tests/discord.int.test.ts index f5710bdd5842..df716f3f0001 100644 --- a/langchain/src/agents/tests/discord.int.test.ts +++ b/langchain/src/agents/tests/discord.int.test.ts @@ -16,7 +16,7 @@ test.skip("DiscordSendMessagesTool should tell a joke in the discord channel", a verbose: true, }); - const res = await executor.call({ + const res = await executor.invoke({ input: `Tell a joke in the discord channel`, }); diff --git a/langchain/src/tools/discord.ts b/langchain/src/tools/discord.ts index efc3ee5a0ea9..7144433fbbb6 100644 --- a/langchain/src/tools/discord.ts +++ b/langchain/src/tools/discord.ts @@ -8,6 +8,33 @@ import { import { getEnvironmentVariable } from "../util/env.js"; import { Tool } from "./base.js"; +/** + * Base tool parameters for the Discord tools + */ +interface DiscordToolParams { + botToken?: string; +} + +/** + * Tool parameters for the DiscordGetMessagesTool + */ +interface DiscordGetMessagesToolParams extends DiscordToolParams { + messageLimit?: number; +} + +/** + * Tool parameters for the DiscordSendMessageTool + */ +interface DiscordSendMessageToolParams extends DiscordToolParams { + channelId?: string; +} + +/** + * Tool parameters for the DiscordChannelSearch + */ +interface DiscordChannelSearchParams extends DiscordToolParams { + channelId?: string; +} /** * A tool for retrieving messages from a discord channel using a bot. * It extends the base Tool class and implements the _call method to @@ -36,15 +63,17 @@ export class DiscordGetMessagesTool extends Tool { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN"), - messageLimit: number | undefined = 10 - ) { - super(...arguments); + constructor(fields: DiscordGetMessagesToolParams = {}) { + super(); + + const { + botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"), + messageLimit = 10, + } = fields; if (!botToken) { throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" ); } @@ -96,14 +125,15 @@ export class DiscordGetGuildsTool extends Tool { intents: [GatewayIntentBits.Guilds], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN") - ) { - super(...arguments); + constructor(fields: DiscordToolParams = {}) { + super(); + + const { botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN") } = + fields || {}; if (!botToken) { throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" ); } this.botToken = botToken; @@ -149,14 +179,15 @@ export class DiscordGetTextChannelsTool extends Tool { intents: [GatewayIntentBits.Guilds], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN") - ) { - super(...arguments); + constructor(fields: DiscordToolParams = {}) { + super(); + + const { botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN") } = + fields || {}; if (!botToken) { throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" ); } this.botToken = botToken; @@ -186,7 +217,7 @@ export class DiscordGetTextChannelsTool extends Tool { /** * A tool for sending messages to a discord channel using a bot. * It extends the base Tool class and implements the _call method to - * perform the retrieve operation. Requires a bot token which can be set + * perform the retrieve operation. Requires a bot token and channelId which can be set * in the environment variables. The _call method takes the message to be * sent as the input argument. */ @@ -208,23 +239,26 @@ export class DiscordSendMessagesTool extends Tool { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN"), - channelId: string | undefined = getEnvironmentVariable("DISCORD_CHANNEL_ID") - ) { - super(...arguments); + constructor(fields: DiscordSendMessageToolParams = {}) { + super(); + + const { + botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"), + channelId = getEnvironmentVariable("DISCORD_CHANNEL_ID"), + } = fields; if (!botToken) { throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" ); } - this.botToken = botToken; + if (!channelId) { throw new Error( - "Discord channel not set. You can set it as DISCORD_CHANNEL_ID in your .env file." + "Discord API key not set. You can set it as DISCORD_CHANNEL_ID" ); } + this.botToken = botToken; this.channelId = channelId; } @@ -285,23 +319,26 @@ export class DiscordChannelSearchTool extends Tool { intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages], }); - constructor( - botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN"), - channelId: string | undefined = getEnvironmentVariable("DISCORD_CHANNEL_ID") - ) { - super(...arguments); + constructor(fields: DiscordChannelSearchParams = {}) { + super(); + + const { + botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"), + channelId = getEnvironmentVariable("DISCORD_CHANNEL_ID"), + } = fields; if (!botToken) { throw new Error( - "Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file." + "Discord API key not set. You can set it as DISCORD_BOT_TOKEN" ); } - this.botToken = botToken; + if (!channelId) { throw new Error( - "Discord channel not set. You can set it as DISCORD_CHANNEL_ID in your .env file." + "Discord API key not set. You can set it as DISCORD_CHANNEL_ID" ); } + this.botToken = botToken; this.channelId = channelId; } @@ -334,7 +371,7 @@ export class DiscordChannelSearchTool extends Tool { return JSON.stringify(results); } catch (err) { await this.client.destroy(); - return "Error sending message"; + return "Error searching through channel."; } } } diff --git a/langchain/src/tools/tests/discord.int.test.ts b/langchain/src/tools/tests/discord.int.test.ts index 1c86f1e5f461..00f5bbb431d4 100644 --- a/langchain/src/tools/tests/discord.int.test.ts +++ b/langchain/src/tools/tests/discord.int.test.ts @@ -9,8 +9,9 @@ import { test.skip("DiscordGetMessagesTool", async () => { const tool = new DiscordGetMessagesTool(); + try { - const result = await tool.call("1153400523718938780"); + const result = await tool.invoke("1153400523718938780"); console.log(result); } catch (error) { console.error(error); @@ -20,7 +21,7 @@ test.skip("DiscordGetMessagesTool", async () => { test.skip("DiscordGetGuildsTool", async () => { const tool = new DiscordGetGuildsTool(); try { - const result = await tool.call(""); + const result = await tool.invoke(""); console.log(result); } catch (error) { console.error(error); @@ -30,7 +31,7 @@ test.skip("DiscordGetGuildsTool", async () => { test.skip("DiscordChannelSearchTool", async () => { const tool = new DiscordChannelSearchTool(); try { - const result = await tool.call("Test"); + const result = await tool.invoke("Test"); console.log(result); } catch (error) { console.error(error); @@ -40,7 +41,7 @@ test.skip("DiscordChannelSearchTool", async () => { test.skip("DiscordGetTextChannelsTool", async () => { const tool = new DiscordGetTextChannelsTool(); try { - const result = await tool.call("1153400523718938775"); + const result = await tool.invoke("1153400523718938775"); console.log(result); } catch (error) { console.error(error); @@ -50,7 +51,7 @@ test.skip("DiscordGetTextChannelsTool", async () => { test.skip("DiscordSendMessagesTool", async () => { const tool = new DiscordSendMessagesTool(); try { - const result = await tool.call("test message from new code"); + const result = await tool.invoke("test message from new code"); console.log(result); } catch (err) { console.log(err);