generated from nhcarrigan/template
feat: error handling (#3)
All checks were successful
Node.js CI / Lint and Test (push) Successful in 38s
All checks were successful
Node.js CI / Lint and Test (push) Successful in 38s
### Explanation _No response_ ### Issue _No response_ ### Attestations - [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [ ] I have pinned the dependencies to a specific patch version. ### Style - [x] I have run the linter and resolved any errors. - [x] My pull request uses an appropriate title, matching the conventional commit standards. - [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Reviewed-on: #3 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit is contained in:
parent
c63003bb75
commit
bb1c327160
16
src/index.ts
16
src/index.ts
@ -28,6 +28,22 @@ const commands: Record<
|
||||
"summarise": summarise,
|
||||
};
|
||||
|
||||
process.on("unhandledRejection", (error) => {
|
||||
if (error instanceof Error) {
|
||||
void logger.error("Unhandled Rejection", error);
|
||||
return;
|
||||
}
|
||||
void logger.error("unhandled rejection", new Error(String(error)));
|
||||
});
|
||||
|
||||
process.on("uncaughtException", (error) => {
|
||||
if (error instanceof Error) {
|
||||
void logger.error("Uncaught Exception", error);
|
||||
return;
|
||||
}
|
||||
void logger.error("uncaught exception", new Error(String(error)));
|
||||
});
|
||||
|
||||
const client = new Client({
|
||||
intents: [],
|
||||
});
|
||||
|
@ -13,56 +13,66 @@ import {
|
||||
MessageFlags,
|
||||
type ChatInputCommandInteraction,
|
||||
} from "discord.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
|
||||
/**
|
||||
* Responds with information about the bot.
|
||||
* @param interaction -- The interaction payload from Discord.
|
||||
*/
|
||||
// eslint-disable-next-line max-lines-per-function -- Refactor at a later time.
|
||||
export const about = async(
|
||||
interaction: ChatInputCommandInteraction,
|
||||
): Promise<void> => {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
|
||||
const version = process.env.npm_package_version ?? "Unknown";
|
||||
const commit = execSync("git rev-parse --short HEAD").toString().
|
||||
trim();
|
||||
const version = process.env.npm_package_version ?? "Unknown";
|
||||
const commit = execSync("git rev-parse --short HEAD").toString().
|
||||
trim();
|
||||
|
||||
const embed = new EmbedBuilder();
|
||||
embed.setTitle("About Cordelia Taryne");
|
||||
embed.setDescription(
|
||||
const embed = new EmbedBuilder();
|
||||
embed.setTitle("About Cordelia Taryne");
|
||||
embed.setDescription(
|
||||
// eslint-disable-next-line stylistic/max-len -- It's a long string.
|
||||
"Cordelia Taryne is a Discord bot that uses Anthropic to provide assistive features. She is developed by NHCarrigan. To use the bot, type `/` and select one of her commands!",
|
||||
);
|
||||
embed.addFields(
|
||||
{
|
||||
name: "Running Version",
|
||||
value: version,
|
||||
},
|
||||
{
|
||||
name: "Current Commit",
|
||||
value: commit,
|
||||
},
|
||||
);
|
||||
"Cordelia Taryne is a Discord bot that uses Anthropic to provide assistive features. She is developed by NHCarrigan. To use the bot, type `/` and select one of her commands!",
|
||||
);
|
||||
embed.addFields(
|
||||
{
|
||||
name: "Running Version",
|
||||
value: version,
|
||||
},
|
||||
{
|
||||
name: "Current Commit",
|
||||
value: commit,
|
||||
},
|
||||
);
|
||||
|
||||
const supportButton = new ButtonBuilder().
|
||||
setLabel("Need help?").
|
||||
setStyle(ButtonStyle.Link).
|
||||
setURL("https://chat.nhcarrigan.com");
|
||||
const sourceButton = new ButtonBuilder().
|
||||
setLabel("Source Code").
|
||||
setStyle(ButtonStyle.Link).
|
||||
setURL("https://git.nhcarrigan.com/nhcarrigan/aria-iuvo");
|
||||
const subscribeButton = new ButtonBuilder().
|
||||
setStyle(ButtonStyle.Premium).
|
||||
setSKUId("1338672773261951026");
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
|
||||
supportButton,
|
||||
sourceButton,
|
||||
subscribeButton,
|
||||
);
|
||||
const supportButton = new ButtonBuilder().
|
||||
setLabel("Need help?").
|
||||
setStyle(ButtonStyle.Link).
|
||||
setURL("https://chat.nhcarrigan.com");
|
||||
const sourceButton = new ButtonBuilder().
|
||||
setLabel("Source Code").
|
||||
setStyle(ButtonStyle.Link).
|
||||
setURL("https://git.nhcarrigan.com/nhcarrigan/aria-iuvo");
|
||||
const subscribeButton = new ButtonBuilder().
|
||||
setStyle(ButtonStyle.Premium).
|
||||
setSKUId("1338672773261951026");
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
|
||||
supportButton,
|
||||
sourceButton,
|
||||
subscribeButton,
|
||||
);
|
||||
|
||||
await interaction.editReply({
|
||||
components: [ row ],
|
||||
embeds: [ embed ],
|
||||
});
|
||||
await interaction.editReply({
|
||||
components: [ row ],
|
||||
embeds: [ embed ],
|
||||
});
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("about command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
import type { ImageBlockParam } from "@anthropic-ai/sdk/resources/index.js";
|
||||
|
||||
const isValidContentType = (
|
||||
@ -31,86 +33,93 @@ const isValidContentType = (
|
||||
export const alt = async(
|
||||
interaction: ChatInputCommandInteraction,
|
||||
): Promise<void> => {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
|
||||
const image = interaction.options.getAttachment("image", true);
|
||||
const { contentType, height, width, size, url } = image;
|
||||
const image = interaction.options.getAttachment("image", true);
|
||||
const { contentType, height, width, size, url } = image;
|
||||
|
||||
// Claude supports JPG, PNG, GIF, WEBP
|
||||
if (
|
||||
contentType === null
|
||||
// Claude supports JPG, PNG, GIF, WEBP
|
||||
if (
|
||||
contentType === null
|
||||
|| !isValidContentType(contentType)
|
||||
|| height === null
|
||||
|| width === null
|
||||
) {
|
||||
await interaction.editReply({
|
||||
content: "That does not appear to be a valid image.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
) {
|
||||
await interaction.editReply({
|
||||
content: "That does not appear to be a valid image.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Max file size is 5MB
|
||||
if (size > 5 * 1024 * 1024) {
|
||||
await interaction.editReply({
|
||||
content:
|
||||
// Max file size is 5MB
|
||||
if (size > 5 * 1024 * 1024) {
|
||||
await interaction.editReply({
|
||||
content:
|
||||
// eslint-disable-next-line stylistic/max-len -- It's a long string.
|
||||
"That image is too large. Please provide an image that is less than 5MB.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Max dimensions are 8000px
|
||||
if (height > 8000 || width > 8000) {
|
||||
await interaction.editReply({
|
||||
content:
|
||||
// Max dimensions are 8000px
|
||||
if (height > 8000 || width > 8000) {
|
||||
await interaction.editReply({
|
||||
content:
|
||||
// eslint-disable-next-line stylistic/max-len -- It's a long string.
|
||||
"That image is too large. Please provide an image that is less than 8000 pixels high or wide.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const downloadRequest = await fetch(url);
|
||||
const downloadRequest = await fetch(url);
|
||||
|
||||
const blob = await downloadRequest.arrayBuffer();
|
||||
const base64 = Buffer.from(blob).toString("base64");
|
||||
const blob = await downloadRequest.arrayBuffer();
|
||||
const base64 = Buffer.from(blob).toString("base64");
|
||||
|
||||
const messages = await ai.messages.create({
|
||||
const messages = await ai.messages.create({
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required key format for SDK.
|
||||
max_tokens: 2000,
|
||||
messages: [
|
||||
{
|
||||
content: [
|
||||
{
|
||||
source: {
|
||||
data: base64,
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required property syntax for SDK.
|
||||
media_type: contentType,
|
||||
type: "base64",
|
||||
max_tokens: 2000,
|
||||
messages: [
|
||||
{
|
||||
content: [
|
||||
{
|
||||
source: {
|
||||
data: base64,
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required property syntax for SDK.
|
||||
media_type: contentType,
|
||||
type: "base64",
|
||||
},
|
||||
type: "image",
|
||||
},
|
||||
type: "image",
|
||||
},
|
||||
],
|
||||
role: "user",
|
||||
},
|
||||
],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to generate descriptive and accessible alt-text for the user's image. Be as descriptive as possible. Do not include ANYTHING in your response EXCEPT the actual alt-text. Wrap the text in a multi-line code block for easy copying.`,
|
||||
temperature: 1,
|
||||
});
|
||||
],
|
||||
role: "user",
|
||||
},
|
||||
],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to generate descriptive and accessible alt-text for the user's image. Be as descriptive as possible. Do not include ANYTHING in your response EXCEPT the actual alt-text. Wrap the text in a multi-line code block for easy copying.`,
|
||||
temperature: 1,
|
||||
});
|
||||
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "alt-text");
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "alt-text");
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("alt-text command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
|
||||
/**
|
||||
* Accepts an arbitrary code snippet from the user, then sends
|
||||
@ -17,31 +19,38 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
export const evaluate = async(
|
||||
interaction: ChatInputCommandInteraction,
|
||||
): Promise<void> => {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
|
||||
const code = interaction.options.getString("code", true);
|
||||
const messages = await ai.messages.create({
|
||||
const code = interaction.options.getString("code", true);
|
||||
const messages = await ai.messages.create({
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required key format for SDK.
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: code, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to evaluate the user's code and provide the result. Wrap ONLY THE CODE RESULT in a multi-line code block for easy copying.`,
|
||||
temperature: 1,
|
||||
});
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: code, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to evaluate the user's code and provide the result. Wrap ONLY THE CODE RESULT in a multi-line code block for easy copying.`,
|
||||
temperature: 1,
|
||||
});
|
||||
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "evaluate");
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "evaluate");
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("evaluate command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
|
||||
/**
|
||||
* Accepts a text snippet from the user. Submits it to Anthropic
|
||||
@ -17,32 +19,39 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
export const mood = async(
|
||||
interaction: ChatInputCommandInteraction,
|
||||
): Promise<void> => {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
|
||||
const prompt = interaction.options.getString("text", true);
|
||||
const prompt = interaction.options.getString("text", true);
|
||||
|
||||
const messages = await ai.messages.create({
|
||||
const messages = await ai.messages.create({
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required key format for SDK.
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: prompt, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to analyse the text the user provides for the overall sentiment and mood of the author.`,
|
||||
temperature: 1,
|
||||
});
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: prompt, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to analyse the text the user provides for the overall sentiment and mood of the author.`,
|
||||
temperature: 1,
|
||||
});
|
||||
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "mood");
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "mood");
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("mood command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
|
||||
/**
|
||||
* Accepts a text snippet from the user. Submits it to Anthropic
|
||||
@ -17,32 +19,39 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
export const proofread = async(
|
||||
interaction: ChatInputCommandInteraction,
|
||||
): Promise<void> => {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
|
||||
const prompt = interaction.options.getString("text", true);
|
||||
const prompt = interaction.options.getString("text", true);
|
||||
|
||||
const messages = await ai.messages.create({
|
||||
const messages = await ai.messages.create({
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required key format for SDK.
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: prompt, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to proofread the text the user has provided. You should identify spelling and grammatical errors using British English.`,
|
||||
temperature: 1,
|
||||
});
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: prompt, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to proofread the text the user has provided. You should identify spelling and grammatical errors using British English.`,
|
||||
temperature: 1,
|
||||
});
|
||||
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "proofread");
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "proofread");
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("proofread command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
|
||||
/**
|
||||
* Accepts an arbitrary question from the user, then sends it to Anthropic
|
||||
@ -17,32 +19,39 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
export const query = async(
|
||||
interaction: ChatInputCommandInteraction,
|
||||
): Promise<void> => {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
|
||||
const prompt = interaction.options.getString("prompt", true);
|
||||
const prompt = interaction.options.getString("prompt", true);
|
||||
|
||||
const messages = await ai.messages.create({
|
||||
const messages = await ai.messages.create({
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required key format for SDK.
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: prompt, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to answer the user's question to the best of your abilities. When possible, include links to relevant sources.`,
|
||||
temperature: 1,
|
||||
});
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: prompt, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to answer the user's question to the best of your abilities. When possible, include links to relevant sources.`,
|
||||
temperature: 1,
|
||||
});
|
||||
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "query");
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "query");
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("query command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
|
||||
/**
|
||||
* Accepts a text snippet from the user. Submits it to Anthropic
|
||||
@ -17,32 +19,39 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
export const summarise = async(
|
||||
interaction: ChatInputCommandInteraction,
|
||||
): Promise<void> => {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const sub = await isSubscribed(interaction);
|
||||
if (!sub) {
|
||||
return;
|
||||
}
|
||||
|
||||
const prompt = interaction.options.getString("text", true);
|
||||
const prompt = interaction.options.getString("text", true);
|
||||
|
||||
const messages = await ai.messages.create({
|
||||
const messages = await ai.messages.create({
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required key format for SDK.
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: prompt, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to summarise the text the user has provided. Your goal is to reach 250 words or less. Wrap ONLY THE SUMMARY in multi-line code block so it is easy to copy.`,
|
||||
temperature: 1,
|
||||
});
|
||||
max_tokens: 2000,
|
||||
messages: [ { content: prompt, role: "user" } ],
|
||||
model: "claude-3-5-sonnet-latest",
|
||||
system: `${personality} Your role in this conversation is to summarise the text the user has provided. Your goal is to reach 250 words or less. Wrap ONLY THE SUMMARY in multi-line code block so it is easy to copy.`,
|
||||
temperature: 1,
|
||||
});
|
||||
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
const response = messages.content.find((message) => {
|
||||
return message.type === "text";
|
||||
});
|
||||
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
await interaction.editReply(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "summarise");
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "summarise");
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("summarise command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
38
src/utils/replyToError.ts
Normal file
38
src/utils/replyToError.ts
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonBuilder,
|
||||
ButtonStyle,
|
||||
type ChatInputCommandInteraction,
|
||||
type MessageContextMenuCommandInteraction,
|
||||
} from "discord.js";
|
||||
|
||||
/**
|
||||
* Responds to an interaction with a generic error message.
|
||||
* @param interaction -- The interaction payload from Discord.
|
||||
*/
|
||||
export const replyToError = async(
|
||||
interaction:
|
||||
| ChatInputCommandInteraction
|
||||
| MessageContextMenuCommandInteraction,
|
||||
): Promise<void> => {
|
||||
const button = new ButtonBuilder().setLabel("Need help?").
|
||||
setStyle(ButtonStyle.Link).
|
||||
setURL("https://chat.nhcarrigan.com");
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(button);
|
||||
if (interaction.deferred || interaction.replied) {
|
||||
await interaction.editReply({
|
||||
components: [ row ],
|
||||
content: "An error occurred while running this command.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
await interaction.reply({
|
||||
components: [ row ],
|
||||
content: "An error occurred while running this command.",
|
||||
});
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user