execution: support for requests that don't have channel set

Interactions initiated outside of servers where bot is don't have channel assigned
This commit is contained in:
Wroclaw 2024-04-26 05:38:41 +02:00
parent 2fab1b1b42
commit 9f5dfefb31
2 changed files with 12 additions and 9 deletions

View file

@ -1,4 +1,3 @@
import { Message } from "discord.js";
import { import {
ChatCompletionMessageParam as OpenAIMessage, ChatCompletionMessageParam as OpenAIMessage,
ChatCompletionCreateParamsNonStreaming as ChatCompletionRequestData, ChatCompletionCreateParamsNonStreaming as ChatCompletionRequestData,
@ -6,6 +5,7 @@ import {
import IQuota from "./IQuota"; import IQuota from "./IQuota";
import MessageCount from "./quota/messageCount"; import MessageCount from "./quota/messageCount";
import { apiRequest } from "./execution";
export interface IConfigRequired { export interface IConfigRequired {
/** Tokens to authentiate with */ /** Tokens to authentiate with */
@ -14,7 +14,7 @@ export interface IConfigRequired {
readonly OpenAI: string; readonly OpenAI: string;
}; };
/** Messages to append at the start of every chat history when sending to API */ /** Messages to append at the start of every chat history when sending to API */
systemPrompt(context: Message): OpenAIMessage[]; systemPrompt(context: apiRequest): OpenAIMessage[];
/** OpenAI model config */ /** OpenAI model config */
readonly chatCompletionParams: Omit<ChatCompletionRequestData, "messages" | "function_call" | "tool_call" | "functions" | "n">; readonly chatCompletionParams: Omit<ChatCompletionRequestData, "messages" | "function_call" | "tool_call" | "functions" | "n">;
/** Limits for message selection */ /** Limits for message selection */

View file

@ -73,7 +73,7 @@ export function getAuthor(request: apiRequest) {
* @returns Promise of the done action * @returns Promise of the done action
*/ */
function requestReply( function requestReply(
request: RequestMessage, request: apiRequest,
message: DiscordApi.MessageReplyOptions & DiscordApi.InteractionReplyOptions, message: DiscordApi.MessageReplyOptions & DiscordApi.InteractionReplyOptions,
// TODO: add support for these below // TODO: add support for these below
replyOptions: DiscordApi.MessageReplyOptions = {}, replyOptions: DiscordApi.MessageReplyOptions = {},
@ -176,7 +176,7 @@ export async function queueRequest(request: apiRequest) {
*/ */
function logUsedTokens( function logUsedTokens(
answer: ChatCompletion, answer: ChatCompletion,
message: RequestMessage | undefined = undefined, message: apiRequest | undefined = undefined,
functionRan: number = 0, functionRan: number = 0,
) { ) {
const usage = answer.usage; const usage = answer.usage;
@ -191,14 +191,17 @@ function logUsedTokens(
// it doesn't make sense to store usage in database if we don't know where it came from // it doesn't make sense to store usage in database if we don't know where it came from
return; return;
} }
const channelName: string = !message.channel.isDMBased() ? `${message.channel.name} (${message.guild?.name})` : `@${getAuthor(message).tag}`; const channelName: string = !message.channel ? "[No channel]"
console.log(`Used ${usage.total_tokens} (${usage.prompt_tokens} + ${usage.completion_tokens}) tokens for ${getAuthor(message).tag} (${getAuthor(message).id}) in #${channelName}${functionNames && functionNames.length > 0 ? " [Tools: " + functionNames.join(", ") + "]" : ""}`); : !message.channel.isDMBased() ? `#${message.channel.name} (${message.guild?.name})`
: `#@${getAuthor(message).tag}`
;
console.log(`Used ${usage.total_tokens} (${usage.prompt_tokens} + ${usage.completion_tokens}) tokens for ${getAuthor(message).tag} (${getAuthor(message).id}) in ${channelName}${functionNames && functionNames.length > 0 ? " [Tools: " + functionNames.join(", ") + "]" : ""}`);
database.usage.create({ database.usage.create({
data: { data: {
timestamp: message.createdAt, timestamp: message.createdAt,
user: BigInt(getAuthor(message).id), user: BigInt(getAuthor(message).id),
channel: BigInt(message.channelId), channel: BigInt(message.channelId ?? 0),
guild: message.guildId ? BigInt(message.guildId) : null, guild: message.guildId ? BigInt(message.guildId) : null,
usageRequest: usage.prompt_tokens, usageRequest: usage.prompt_tokens,
usageResponse: usage.completion_tokens, usageResponse: usage.completion_tokens,
@ -312,7 +315,7 @@ async function executeFromQueue(channel: string) {
* @param answerContent - The content of the answer. * @param answerContent - The content of the answer.
* @param message - The request message to reply to. * @param message - The request message to reply to.
*/ */
async function replyInMultiMessage(answerContent: string | null, message: RequestMessage) { async function replyInMultiMessage(answerContent: string | null, message: apiRequest) {
if (answerContent === null || answerContent === "") { if (answerContent === null || answerContent === "") {
if (message instanceof DiscordApi.Message) message.react("😶").catch(() => { }); if (message instanceof DiscordApi.Message) message.react("😶").catch(() => { });
} }
@ -344,7 +347,7 @@ async function replyInMultiMessage(answerContent: string | null, message: Reques
*/ */
async function executeChatCompletion( async function executeChatCompletion(
OpenAImessages: ChatCompletionMessageParam[], OpenAImessages: ChatCompletionMessageParam[],
message: RequestMessage | undefined, message: apiRequest | undefined,
) { ) {
let generatedMessage: ChatCompletionMessage | undefined = undefined; let generatedMessage: ChatCompletionMessage | undefined = undefined;
let answer: Awaited<ReturnType<typeof openai.chat.completions.create>>; let answer: Awaited<ReturnType<typeof openai.chat.completions.create>>;