Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add discord example #25

Merged
merged 8 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions .changeset/cuddly-mangos-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@callstack/byorg-discord': minor
'discord-example': patch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] implementing example actually accounts as a minor ;-)

Suggested change
'discord-example': patch
'discord-example': minor

---

added start function to abstract away passing bot token
7 changes: 4 additions & 3 deletions examples/discord/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
"dependencies": {
"@ai-sdk/openai": "^1.0.2",
"@callstack/byorg-core": "0.4.2",
Copy link
Member

@mdjastrzebski mdjastrzebski Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you replace all 0.4.2 references with workspace (in every package)? There was a bug if you released it with changeset, but now we do release it with pnpm so we can use workspace

"@callstack/byorg-discord": "0.4.2",
"@callstack/byorg-utils": "0.4.2"
"@callstack/byorg-discord": "workspace:^",
"@callstack/byorg-utils": "0.4.2",
"zlib-sync": "^0.1.9"
},
"devDependencies": {
"dotenv-cli": "^7.4.2",
"@callstack/eslint-config": "^14.2.0",
"@rsbuild/core": "^1.0.5",
"dotenv-cli": "^7.4.2",
"eslint": "^8.57.0",
"prettier": "^3.3.3",
"typescript": "^5.6.3"
Expand Down
5 changes: 5 additions & 0 deletions examples/discord/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export default defineConfig({
'.js': ['.js', '.ts'],
},
},
ignoreWarnings: [
/the request of a dependency is an expression/,
/Can't resolve 'bufferutil'/,
/Can't resolve 'utf-8-validate'/,
],
},
},
});
52 changes: 26 additions & 26 deletions examples/discord/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
// TODO
// import readline from 'readline';
// import { Message, VercelChatModelAdapter, createApp } from '@callstack/byorg-core';
// import { createOpenAI } from '@ai-sdk/openai';
// import { requireEnv } from '@callstack/byorg-utils';
// import { createDiscordApp } from '@callstack/byorg-discord';
import { VercelChatModelAdapter, createApp } from '@callstack/byorg-core';
import { createOpenAI } from '@ai-sdk/openai';
import { requireEnv } from '@callstack/byorg-utils';
import { createDiscordApp } from '@callstack/byorg-discord';

// const LANGUAGE_MODEL = 'gpt-4o-2024-11-20';
// const API_KEY = requireEnv('OPENAI_API_KEY');
const LANGUAGE_MODEL = 'gpt-4o-2024-11-20';
const API_KEY = requireEnv('OPENAI_API_KEY');
const DISCORD_BOT_TOKEN = requireEnv('DISCORD_BOT_TOKEN');

// const openAiProvider = createOpenAI({
// apiKey: API_KEY,
// compatibility: 'strict', // strict mode, enable when using the OpenAI API
// });
const openAiProvider = createOpenAI({
apiKey: API_KEY,
compatibility: 'strict', // strict mode, enable when using the OpenAI API
});

// const openAiModel = openAiProvider.languageModel(LANGUAGE_MODEL);
const openAiModel = openAiProvider.languageModel(LANGUAGE_MODEL);

// const chatModel = new VercelChatModelAdapter({
// languageModel: openAiModel,
// });
const chatModel = new VercelChatModelAdapter({
languageModel: openAiModel,
});

// const systemPrompt = () => {
// return 'Your name is Cassandra. You are an AI Assistant.';
// };
const systemPrompt = () => {
return 'Your name is Cassandra. You are an AI Assistant.';
};

// const app = createApp({
// chatModel,
// systemPrompt,
// });
const app = createApp({
chatModel,
systemPrompt,
});

// // const discord = createDiscordApp({
// // app,
// // });
void createDiscordApp({
app,
botToken: DISCORD_BOT_TOKEN,
});
2 changes: 1 addition & 1 deletion packages/discord/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@callstack/byorg-discord",
"version": "0.4.2",
"version": "0.4.3",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave version updating to changset

"type": "module",
"license": "MIT",
"author": "Szymon Chmal <[email protected]> (https://github.com/V3R0N), Maciej Jastrzebski <[email protected]> (https://github.com/mdjastrzebski), Kewin Wereszczynski <[email protected]> (https://github.com/Q1w1N)",
Expand Down
18 changes: 13 additions & 5 deletions packages/discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ const UPDATE_THROTTLE_TIME = 200;

export type DiscordApplicationConfig = {
app: Application;
botToken: string;
};

export async function createDiscordApp(options: DiscordApplicationConfig) {
const { app } = options;
type ClientStart = {
start: () => Promise<string>;
};

export type DiscordClient = Client & ClientStart;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add code comment documenting this hack


export function createDiscordApp(options: DiscordApplicationConfig): DiscordClient {
const { app, botToken } = options;

const botToken = process.env['DISCORD_BOT_TOKEN'] as string;
const client = new Client({
intents: [
GatewayIntentBits.DirectMessages,
Expand All @@ -20,7 +26,7 @@ export async function createDiscordApp(options: DiscordApplicationConfig) {
GatewayIntentBits.GuildMessages,
],
partials: [Partials.Channel],
});
}) as DiscordClient;

client.once(Events.ClientReady, (readyClient) => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
Expand Down Expand Up @@ -76,7 +82,9 @@ export async function createDiscordApp(options: DiscordApplicationConfig) {
await pendingEffects;
});

await client.login(botToken);
client.start = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we adding methods to existing discord object?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make usage of discord the same as slack, and to abstract away the "login" method that takes in the bot token as an argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see any code in this PR actually calling this method. Is it needed?

Copy link
Collaborator Author

@Q1w1N Q1w1N Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now added

return client.login(botToken);
};

return client;
}
Expand Down
46 changes: 21 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading