diff --git a/src/configDefault.ts b/src/configDefault.ts index 620a363..dcd4dd9 100644 --- a/src/configDefault.ts +++ b/src/configDefault.ts @@ -1,4 +1,3 @@ -import { Message } from "discord.js"; import { ChatCompletionMessageParam as OpenAIMessage, ChatCompletionCreateParamsNonStreaming as ChatCompletionRequestData, @@ -6,6 +5,7 @@ import { import IQuota from "./IQuota"; import MessageCount from "./quota/messageCount"; +import { apiRequest } from "./execution"; export interface IConfigRequired { /** Tokens to authentiate with */ @@ -14,7 +14,7 @@ export interface IConfigRequired { readonly OpenAI: string; }; /** 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 */ readonly chatCompletionParams: Omit; /** Limits for message selection */ diff --git a/src/execution.ts b/src/execution.ts index a78131f..f715564 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -73,7 +73,7 @@ export function getAuthor(request: apiRequest) { * @returns Promise of the done action */ function requestReply( - request: RequestMessage, + request: apiRequest, message: DiscordApi.MessageReplyOptions & DiscordApi.InteractionReplyOptions, // TODO: add support for these below replyOptions: DiscordApi.MessageReplyOptions = {}, @@ -176,7 +176,7 @@ export async function queueRequest(request: apiRequest) { */ function logUsedTokens( answer: ChatCompletion, - message: RequestMessage | undefined = undefined, + message: apiRequest | undefined = undefined, functionRan: number = 0, ) { 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 return; } - const channelName: string = !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(", ") + "]" : ""}`); + const channelName: string = !message.channel ? "[No channel]" + : !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({ data: { timestamp: message.createdAt, user: BigInt(getAuthor(message).id), - channel: BigInt(message.channelId), + channel: BigInt(message.channelId ?? 0), guild: message.guildId ? BigInt(message.guildId) : null, usageRequest: usage.prompt_tokens, usageResponse: usage.completion_tokens, @@ -312,7 +315,7 @@ async function executeFromQueue(channel: string) { * @param answerContent - The content of the answer. * @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 (message instanceof DiscordApi.Message) message.react("😶").catch(() => { }); } @@ -344,7 +347,7 @@ async function replyInMultiMessage(answerContent: string | null, message: Reques */ async function executeChatCompletion( OpenAImessages: ChatCompletionMessageParam[], - message: RequestMessage | undefined, + message: apiRequest | undefined, ) { let generatedMessage: ChatCompletionMessage | undefined = undefined; let answer: Awaited>;