Compare commits

..

No commits in common. "c9f7e3a62e9b7e56aa07079fffec0f1b8d273556" and "0c0cbceb467fb25de330b11c63612118009b1e9b" have entirely different histories.

2 changed files with 7 additions and 15 deletions

View file

@ -267,7 +267,6 @@ async function executeFromQueue(channel: string) {
messages.forEach(m => { Moderation.checkMessageNoReturn(m); }); messages.forEach(m => { Moderation.checkMessageNoReturn(m); });
const questionMessageId: string = message instanceof DiscordApi.Message ? message.id : '';
if (message instanceof DiscordApi.Message) { if (message instanceof DiscordApi.Message) {
channelQueue.startTyping(); channelQueue.startTyping();
} }
@ -275,13 +274,7 @@ async function executeFromQueue(channel: string) {
await message.deferReply(); await message.deferReply();
} }
messages.sort((a, b) => { OpenAImessages = toOpenAIMessages(messages);
if (a.id === questionMessageId) return -1;
if (b.id === questionMessageId) return 1;
return b.createdTimestamp - a.createdTimestamp;
});
OpenAImessages = toOpenAIMessages(messages.values());
let generatedMessage: ChatCompletionResponseMessage | undefined = undefined; let generatedMessage: ChatCompletionResponseMessage | undefined = undefined;
let answer: Awaited<ReturnType<typeof openai.createChatCompletion>>; let answer: Awaited<ReturnType<typeof openai.createChatCompletion>>;

View file

@ -92,18 +92,17 @@ function getAuthorUsername(message: DiscordMessage): string | undefined {
} }
/** /**
* Converts the Iterable of Discord Messages to array of OpenAI Messages to send * Converts the Collection of Discord Messages to array of OpenAI Messages to send
* first message in the interable will be the last of the returned array (reverse order) * @param messages the collection to convert
* @param messages the iterable to convert
* @returns the converted messages * @returns the converted messages
*/ */
export default function toOpenAIMessages( export default function toOpenAIMessages(messages: Collection<string, DiscordMessage>): OpenAIMessage[] {
messages: Iterable<DiscordMessage>,
): OpenAIMessage[] {
const rvalue: OpenAIMessage[] = []; const rvalue: OpenAIMessage[] = [];
let tokenCount = 0; let tokenCount = 0;
for (const message of messages) { messages.sort((a, b) => b.createdTimestamp - a.createdTimestamp);
for (const message of messages.values()) {
const content = formatMessage(message); const content = formatMessage(message);
// FIXME: tokens are not being counted properly (it's lower than it is) but it's enough for me for now. // FIXME: tokens are not being counted properly (it's lower than it is) but it's enough for me for now.
tokenCount += countTokens(content); tokenCount += countTokens(content);