Skip to content

Commit e9524c1

Browse files
committed
refactor: Enhance message processing by extracting code from attachments and updating GIF usage guidelines
1 parent 504062c commit e9524c1

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

src/events/ai/ai.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
makeImageParts,
1515
MAX_MESSAGES_PER_CHANNEL,
1616
TOOLS,
17+
extractCodeFromAttachments,
1718
} from "./utils";
1819

1920
@Discord()
@@ -50,6 +51,15 @@ export class AiChat {
5051

5152
const { replyContext, repliedImages } = await this.getReplyContext(message);
5253

54+
// Extract code from current message attachments
55+
let attachmentContext = "";
56+
if (message.attachments.size > 0) {
57+
const codeContent = await extractCodeFromAttachments(message);
58+
if (codeContent) {
59+
attachmentContext = `\n\n[Code from attachment]:\n${codeContent}`;
60+
}
61+
}
62+
5363
if (
5464
!userMsg &&
5565
message.attachments.size === 0 &&
@@ -64,7 +74,7 @@ export class AiChat {
6474

6575
// Get user context
6676
const userContext = await this.getUserContext(message.author.id, message);
67-
const fullMessage = `${userMsg}${replyContext}${userContext}`;
77+
const fullMessage = `${userMsg}${attachmentContext}${replyContext}${userContext}`;
6878

6979
// Retry logic with exponential backoff
7080
let lastError: Error | null = null;
@@ -229,7 +239,6 @@ export class AiChat {
229239
// Get context for the replied user too
230240
const repliedUserContext = await this.getUserContext(
231241
repliedUser.id,
232-
233242
message
234243
);
235244

@@ -280,4 +289,4 @@ export class AiChat {
280289
}
281290
return null;
282291
}
283-
}
292+
}

src/events/ai/prompt.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
export const AI_SYSTEM_PROMPT = `You are Coding Global, the official Discord bot for the coding.global programming server (discord.gg/coding). Be sarcastic yet helpful and concise - dry humor but still useful.
1+
export const AI_SYSTEM_PROMPT = `You are Coding Global, the official Discord bot for the coding.global programming server (discord.gg/coding). Be sarcastic yet helpful and concise few senteces max no matter the question - dry humor but still useful.
22
33
PERSONALITY:
44
- Never start with "Oh" - use varied openings
55
- Sarcastic but not mean: "Sure, whatever" or "Here's your code..."
66
- Use ellipses (...) for indifference
77
- Professional programmers, trainees, students, and apprentices hang out here
8-
- FREQUENTLY use GIFs to enhance responses - search for relevant reaction GIFs when appropriate
9-
- Use GIFs for: reactions to code problems, celebrations, frustrations, programming humor, etc.
108
119
GIF USAGE GUIDELINES:
12-
- Use GIFs sparingly - only when they genuinely enhance the conversation
10+
- Use GIFs sometimes - only when they genuinely enhance the conversation
1311
- Good for: major celebrations, epic fails, or when specifically asked
1412
- Avoid using GIFs for routine responses or simple questions
1513
- Never use GIFs just to fill space - substance over entertainment

src/events/ai/utils.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,24 @@ export async function gatherMessageContext(
5151

5252
const contextParts = await Promise.all(
5353
allMessages.map(async (msg) => {
54+
let content = msg.content?.trim() || "";
55+
5456
if (msg.attachments.size > 0) {
5557
try {
5658
const msgImages = await makeImageParts(msg);
5759
images.push(...msgImages);
60+
61+
// Extract code from attachments
62+
const codeContent = await extractCodeFromAttachments(msg);
63+
if (codeContent) {
64+
content += `\n\n[Code from attachment]:\n${codeContent}`;
65+
}
5866
} catch (error) {
59-
console.error("Error processing images:", error);
67+
console.error("Error processing attachments:", error);
6068
}
6169
}
62-
return msg.content?.trim() || "";
70+
71+
return content;
6372
})
6473
);
6574

@@ -71,6 +80,55 @@ export async function gatherMessageContext(
7180
}
7281
}
7382

83+
export async function extractCodeFromAttachments(
84+
message: Message
85+
): Promise<string | null> {
86+
let extractedCode = "";
87+
88+
for (const attachment of message.attachments.values()) {
89+
const { name, url, contentType, size } = attachment;
90+
91+
// Skip if file is too large (>1MB)
92+
if (size && size > 1024 * 1024) {
93+
console.log(`Skipping large file: ${name} (${size} bytes)`);
94+
continue;
95+
}
96+
97+
// Check if it's text-based content
98+
if (contentType?.startsWith("text/") || !contentType) {
99+
try {
100+
console.log(`Fetching code from attachment: ${name}`);
101+
const response = await fetch(url);
102+
103+
if (!response.ok) {
104+
console.error(`Failed to fetch ${name}: ${response.status}`);
105+
continue;
106+
}
107+
108+
const content = await response.text();
109+
110+
// Limit content length to prevent token overflow
111+
const maxLength = 8000;
112+
const truncatedContent =
113+
content.length > maxLength
114+
? content.substring(0, maxLength) + "\n... (content truncated)"
115+
: content;
116+
117+
if (extractedCode) {
118+
extractedCode += `\n\n--- File: ${name} ---\n`;
119+
} else {
120+
extractedCode += `--- File: ${name} ---\n`;
121+
}
122+
extractedCode += truncatedContent;
123+
} catch (error) {
124+
console.error(`Error fetching attachment ${name}:`, error);
125+
}
126+
}
127+
}
128+
129+
return extractedCode || null;
130+
}
131+
74132
export async function makeImageParts(message: Message): Promise<string[]> {
75133
const images: string[] = [];
76134

0 commit comments

Comments
 (0)