Skip to content

Commit 4a1a17e

Browse files
author
Pip Build
committed
fix: log AI responses and user messages to conversations with guild_id
- Add guild_id parameter to addToHistory() function - Update INSERT to include guild_id column - Call addToHistory() when sending AI responses in triage-respond.js - Call addToHistory() for user messages in accumulateMessage() - Update tests to expect 6 parameters instead of 5
1 parent f576bf1 commit 4a1a17e

5 files changed

Lines changed: 40 additions & 20 deletions

File tree

src/modules/ai.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,10 @@ export async function getHistoryAsync(channelId) {
202202
* @param {string} role - Message role (e.g., "user" or "assistant").
203203
* @param {string} content - Message text content.
204204
* @param {string} [username] - Optional display name associated with the message.
205-
* @param {string} [discordMessageId] - Optional native Discord message ID (used to construct jump URLs in the dashboard).
205+
* @param {string} [discordMessageId] - Optional native Discord message ID
206+
* @param {string} [guildId] - Optional guild ID for the conversation (used to construct jump URLs in the dashboard).
206207
*/
207-
export function addToHistory(channelId, role, content, username, discordMessageId) {
208+
export function addToHistory(channelId, role, content, username, discordMessageId, guildId) {
208209
if (!conversationHistory.has(channelId)) {
209210
conversationHistory.set(channelId, []);
210211
}
@@ -223,15 +224,16 @@ export function addToHistory(channelId, role, content, username, discordMessageI
223224
if (pool) {
224225
pool
225226
.query(
226-
`INSERT INTO conversations (channel_id, role, content, username, discord_message_id)
227-
VALUES ($1, $2, $3, $4, $5)`,
228-
[channelId, role, content, username || null, discordMessageId || null],
227+
`INSERT INTO conversations (channel_id, role, content, username, discord_message_id, guild_id)
228+
VALUES ($1, $2, $3, $4, $5, $6)`,
229+
[channelId, role, content, username || null, discordMessageId || null, guildId || null],
229230
)
230231
.catch((err) => {
231232
logError('Failed to persist message to DB', {
232233
channelId,
233234
role,
234235
username: username || null,
236+
guildId: guildId || null,
235237
error: err.message,
236238
});
237239
});

src/modules/triage-respond.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { info, error as logError, warn } from '../logger.js';
88
import { buildDebugEmbed, extractStats, logAiUsage } from '../utils/debugFooter.js';
99
import { safeSend } from '../utils/safeSend.js';
1010
import { splitMessage } from '../utils/splitMessage.js';
11+
import { addToHistory } from './ai.js';
1112
import { resolveMessageId, sanitizeText } from './triage-filter.js';
1213

1314
/** Maximum characters to keep from fetched context messages. */
@@ -214,7 +215,19 @@ export async function sendResponses(
214215
const msgOpts = { content: chunks[i] };
215216
if (debugEmbed && i === 0) msgOpts.embeds = [debugEmbed];
216217
if (replyRef && i === 0) msgOpts.reply = { messageReference: replyRef };
217-
await safeSend(channel, msgOpts);
218+
const sentMsg = await safeSend(channel, msgOpts);
219+
220+
// Log AI response to conversation history
221+
if (sentMsg && !Array.isArray(sentMsg)) {
222+
addToHistory(
223+
channelId,
224+
'assistant',
225+
chunks[i],
226+
null, // username - bot doesn't have one in this context
227+
sentMsg.id,
228+
channel.guild?.id || null
229+
);
230+
}
218231
}
219232

220233
info('Triage response sent', {

src/modules/triage.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
pushToBuffer,
3131
} from './triage-buffer.js';
3232
import { getDynamicInterval, isChannelEligible, resolveTriageConfig } from './triage-config.js';
33+
import { addToHistory } from './ai.js';
3334
import { checkTriggerWords, sanitizeText } from './triage-filter.js';
3435
import { parseClassifyResult, parseRespondResult } from './triage-parse.js';
3536
import { buildClassifyPrompt, buildRespondPrompt } from './triage-prompt.js';
@@ -597,6 +598,16 @@ export async function accumulateMessage(message, msgConfig) {
597598
// Push to ring buffer (with truncation warning)
598599
pushToBuffer(channelId, entry, maxBufferSize);
599600

601+
// Log user message to conversation history
602+
addToHistory(
603+
channelId,
604+
'user',
605+
entry.content,
606+
entry.author,
607+
entry.messageId,
608+
message.guild?.id || null
609+
);
610+
600611
// Check for trigger words -- instant evaluation
601612
if (checkTriggerWords(message.content, msgConfig)) {
602613
info('Trigger word detected, forcing evaluation', { channelId });

tests/modules/ai.coverage.test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,10 @@ describe('ai module coverage', () => {
180180

181181
// Give the fire-and-forget a tick to run
182182
await new Promise((resolve) => setTimeout(resolve, 0));
183-
expect(mockQuery).toHaveBeenCalledWith(expect.stringContaining('INSERT INTO conversations'), [
184-
'ch1',
185-
'user',
186-
'hello',
187-
'testuser',
188-
null,
189-
]);
183+
expect(mockQuery).toHaveBeenCalledWith(
184+
expect.stringContaining('INSERT INTO conversations'),
185+
['ch1', 'user', 'hello', 'testuser', null, null]
186+
);
190187
});
191188

192189
it('logs error when DB write fails', async () => {

tests/modules/ai.test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,10 @@ describe('ai module', () => {
149149

150150
addToHistory('ch1', 'user', 'hello', 'testuser');
151151

152-
expect(mockQuery).toHaveBeenCalledWith(expect.stringContaining('INSERT INTO conversations'), [
153-
'ch1',
154-
'user',
155-
'hello',
156-
'testuser',
157-
null,
158-
]);
152+
expect(mockQuery).toHaveBeenCalledWith(
153+
expect.stringContaining('INSERT INTO conversations'),
154+
['ch1', 'user', 'hello', 'testuser', null, null]
155+
);
159156
});
160157
});
161158

0 commit comments

Comments
 (0)