Skip to content

Commit 34958bc

Browse files
committed
Include last 30 Discord messages as agent context.
This gathers the most recent thread messages (including bot messages) and passes them to the prompt agent so replies can use conversation history instead of only the latest message.
1 parent 2d90e21 commit 34958bc

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

app/src/lib/discord/prompt-agent.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import { createGateway, generateText, tool } from "ai";
88
import { z } from "zod";
99

1010
const DISCORD_AGENT_MODEL = "anthropic/claude-sonnet-4.6";
11+
const MAX_CONTEXT_MESSAGE_LENGTH = 500;
12+
13+
type RecentDiscordMessage = {
14+
author: string;
15+
text: string;
16+
isBot: boolean;
17+
};
1118

1219
const updateEventInputSchema = z.object({
1320
slug: z.string().min(1),
@@ -188,12 +195,27 @@ function buildTools() {
188195

189196
export async function runDiscordPromptAgent(input: {
190197
prompt: string;
198+
recentMessages?: RecentDiscordMessage[];
191199
}): Promise<string> {
192200
const prompt = input.prompt.trim();
193201
if (!prompt) {
194202
return "Please provide a prompt.";
195203
}
196204

205+
const contextText =
206+
input.recentMessages && input.recentMessages.length > 0
207+
? `Recent Discord messages (oldest to newest):\n${input.recentMessages
208+
.map((message) => {
209+
const role = message.isBot ? "bot" : "user";
210+
const text = message.text
211+
.replace(/\s+/g, " ")
212+
.trim()
213+
.slice(0, MAX_CONTEXT_MESSAGE_LENGTH);
214+
return `- [${role}] ${message.author}: ${text}`;
215+
})
216+
.join("\n")}\n\nLatest user prompt:\n${prompt}`
217+
: prompt;
218+
197219
const { text } = await generateText({
198220
model: getGatewayModel(),
199221
tools: buildTools(),
@@ -203,7 +225,7 @@ Use tools whenever the answer depends on current data.
203225
Before updating event data, confirm intent from the user message and summarize what changed.
204226
If user intent is ambiguous, ask a clarifying question instead of calling update tools.
205227
Be concise and action-oriented.`,
206-
prompt,
228+
prompt: contextText,
207229
});
208230

209231
return text.trim();

app/src/lib/discord/review-bot.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ type ReviewCardInput = {
2727
isDraft: boolean;
2828
};
2929

30+
type RecentMessageContext = {
31+
author: string;
32+
text: string;
33+
isBot: boolean;
34+
};
35+
3036
let reviewBot: Chat<{
3137
discord: ReturnType<typeof createDiscordAdapter>;
3238
}> | null = null;
@@ -114,8 +120,29 @@ function registerHandlers(
114120

115121
try {
116122
await thread.startTyping();
123+
const recentMessages: RecentMessageContext[] = [];
124+
for await (const recentMessage of thread.messages) {
125+
const text = recentMessage.text?.trim();
126+
if (!text) {
127+
continue;
128+
}
129+
recentMessages.push({
130+
author:
131+
recentMessage.author.fullName ||
132+
recentMessage.author.userName ||
133+
recentMessage.author.userId,
134+
text,
135+
isBot: recentMessage.author.isBot,
136+
});
137+
if (recentMessages.length >= 30) {
138+
break;
139+
}
140+
}
141+
recentMessages.reverse();
142+
117143
const response = await runDiscordPromptAgent({
118144
prompt: userPrompt.slice(0, 4000),
145+
recentMessages,
119146
});
120147
await thread.post(response || "I couldn't generate a response.");
121148
} catch (error) {

0 commit comments

Comments
 (0)