feat: initial prototype
Node.js CI / Lint and Test (push) Successful in 48s

This commit is contained in:
2025-02-23 16:19:30 -08:00
parent 2c86512196
commit 98fd889647
23 changed files with 5886 additions and 14 deletions
+78
View File
@@ -0,0 +1,78 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ChannelType,
type Message,
} from "discord.js";
import { personality } from "../config/personality.js";
import { ai } from "../utils/ai.js";
import { calculateCost } from "../utils/calculateCost.js";
import { isSubscribedMessage } from "../utils/isSubscribed.js";
import type { MessageParam } from "@anthropic-ai/sdk/resources/index.js";
/**
* Handles the Discord message event.
* @param message - The message payload from Discord.
*/
// eslint-disable-next-line max-lines-per-function -- We're off by one bloody line.
export const onMessage = async(message: Message): Promise<void> => {
try {
if (message.channel.type !== ChannelType.DM) {
return;
}
if (message.author.bot) {
return;
}
const subbed = await isSubscribedMessage(message);
if (!subbed) {
return;
}
const history = await message.channel.messages.fetch({ limit: 6 });
const context: Array<MessageParam>
= history.reverse().map((messageInner) => {
return {
content: messageInner.content,
role:
messageInner.author.id === message.client.user.id
? "assistant"
: "user",
};
});
const messages = await ai.messages.create({
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required key format for SDK.
max_tokens: 3000,
messages: context,
model: "claude-3-5-sonnet-latest",
system: `${personality} Provide a response to the user that continues the story. The user's name is ${message.author.displayName}`,
temperature: 1,
});
const response = messages.content.find((messageInner) => {
return messageInner.type === "text";
});
await message.channel.send(
response?.text ?? "There was an error. Please try again later.",
);
await calculateCost(messages.usage, message.author.username);
} catch (error) {
const button = new ButtonBuilder().
setLabel("Need help?").
setStyle(ButtonStyle.Link).
setURL("https://chat.nhcarrigan.com");
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(button);
await message.reply({
components: [ row ],
content: error instanceof Error
? error.message
: "Something went wrong.",
});
}
};