-
Notifications
You must be signed in to change notification settings - Fork 2
fix: log AI responses and user messages to conversations with guild_id #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
4a1a17e
c7c187d
726ede4
dc0ba14
55f5240
80d1705
9ca303a
2c2482e
0c1de4f
395e199
8a672b7
a6946eb
66153ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,35 @@ import { info, error as logError, warn } from '../logger.js'; | |
| import { buildDebugEmbed, extractStats, logAiUsage } from '../utils/debugFooter.js'; | ||
| import { safeSend } from '../utils/safeSend.js'; | ||
| import { splitMessage } from '../utils/splitMessage.js'; | ||
| import { addToHistory } from './ai.js'; | ||
| import { resolveMessageId, sanitizeText } from './triage-filter.js'; | ||
|
|
||
| /** Maximum characters to keep from fetched context messages. */ | ||
| const CONTEXT_MESSAGE_CHAR_LIMIT = 500; | ||
|
|
||
| // ── History helpers ────────────────────────────────────────────────────────── | ||
|
|
||
| /** | ||
| * Log an assistant message (or multiple messages when safeSend splits into an array) | ||
| * to conversation history. | ||
| * | ||
| * `safeSend` can return either a single Message object or an array of Message objects | ||
| * when the content was split across multiple Discord messages. Both cases are handled | ||
| * here so history is never silently dropped. | ||
| * | ||
| * @param {string} channelId - The channel the message was sent in. | ||
| * @param {string|null} guildId - The guild ID, or null for DMs. | ||
| * @param {string} fallbackContent - Text to use when the sent message has no `.content`. | ||
| * @param {import('discord.js').Message|import('discord.js').Message[]|null} sentMsg - Return value of safeSend. | ||
| */ | ||
| function logAssistantHistory(channelId, guildId, fallbackContent, sentMsg) { | ||
| const sentMessages = Array.isArray(sentMsg) ? sentMsg : [sentMsg]; | ||
| for (const m of sentMessages) { | ||
| if (!m?.id) continue; | ||
| addToHistory(channelId, 'assistant', m.content || fallbackContent, null, m.id, guildId || null); | ||
| } | ||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
Comment on lines
+19
to
+38
|
||
|
|
||
| // ── Channel context fetching ───────────────────────────────────────────────── | ||
|
|
||
| /** | ||
|
|
@@ -177,7 +201,8 @@ export async function sendResponses( | |
| const msgOpts = { content: chunks[i] }; | ||
| if (debugEmbed && i === 0) msgOpts.embeds = [debugEmbed]; | ||
| if (replyRef && i === 0) msgOpts.reply = { messageReference: replyRef }; | ||
| await safeSend(channel, msgOpts); | ||
| const sentMsg = await safeSend(channel, msgOpts); | ||
| logAssistantHistory(channelId, channel.guild?.id || null, chunks[i], sentMsg); | ||
| } | ||
| } | ||
| } catch (err) { | ||
|
|
@@ -214,7 +239,9 @@ export async function sendResponses( | |
| const msgOpts = { content: chunks[i] }; | ||
| if (debugEmbed && i === 0) msgOpts.embeds = [debugEmbed]; | ||
| if (replyRef && i === 0) msgOpts.reply = { messageReference: replyRef }; | ||
| await safeSend(channel, msgOpts); | ||
| const sentMsg = await safeSend(channel, msgOpts); | ||
| // Log AI response to conversation history | ||
| logAssistantHistory(channelId, channel.guild?.id || null, chunks[i], sentMsg); | ||
| } | ||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| info('Triage response sent', { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,6 +186,7 @@ describe('ai module coverage', () => { | |
| 'hello', | ||
| 'testuser', | ||
| null, | ||
| null, | ||
| ]); | ||
| }); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.