summaryrefslogtreecommitdiff
path: root/packages/gateway/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gateway/src')
-rw-r--r--packages/gateway/src/listeners/messageCreate/aiCommandHandler/index.ts66
1 files changed, 25 insertions, 41 deletions
diff --git a/packages/gateway/src/listeners/messageCreate/aiCommandHandler/index.ts b/packages/gateway/src/listeners/messageCreate/aiCommandHandler/index.ts
index ccce8d0..d60547a 100644
--- a/packages/gateway/src/listeners/messageCreate/aiCommandHandler/index.ts
+++ b/packages/gateway/src/listeners/messageCreate/aiCommandHandler/index.ts
@@ -1,4 +1,5 @@
import { Message } from "discord.js";
+import OpenAI from "openai";
import { handleSlowmodeCommand } from "./slowmode.js";
import { handlePurgeCommand } from "./purge.js";
@@ -46,20 +47,16 @@ export const handleAICommand = async (message: Message) => {
if (!content) return;
try {
- const response = await fetch(
- "https://openrouter.ai/api/v1/chat/completions",
- {
- method: "POST",
- headers: {
- Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- model: "mistralai/mistral-nemo",
- messages: [
- {
- role: "system",
- content: `You are a Discord bot command interpreter. Parse user input into JSON commands.
+ const openai = new OpenAI({
+ baseURL: "https://openrouter.ai/api/v1",
+ apiKey: process.env.OPENROUTER_API_KEY,
+ });
+ const completion = await openai.chat.completions.create({
+ model: "mistralai/mistral-nemo",
+ messages: [
+ {
+ role: "system",
+ content: `You are a Discord bot command interpreter. Parse user input into JSON commands.
CRITICAL: Respond with ONLY valid JSON. No explanations, no markdown, no other text.
@@ -96,34 +93,21 @@ If input doesn't match available commands, respond with:
{"command": "unknown", "action": "none", "value": 0}
RESPONSE MUST BE ONLY JSON. NO OTHER TEXT.`,
- },
- {
- role: "user",
- content:
- content +
- (mentionedUserIds.length > 0
- ? `\n\nMentioned user IDs: ${mentionedUserIds.join(", ")}`
- : ""),
- },
- ],
- max_tokens: 100,
- temperature: 0.1,
- }),
- },
- );
-
- if (!response.ok) {
- console.error(
- "OpenRouter API error:",
- response.status,
- response.statusText,
- );
-
- return;
- }
+ },
+ {
+ role: "user",
+ content:
+ content +
+ (mentionedUserIds.length > 0
+ ? `\n\nMentioned user IDs: ${mentionedUserIds.join(", ")}`
+ : ""),
+ },
+ ],
+ max_tokens: 200,
+ temperature: 0.1,
+ });
- const data = await response.json();
- const aiResponse = data.choices?.[0]?.message?.content?.trim();
+ const aiResponse = completion.choices?.[0]?.message?.content?.trim();
if (!aiResponse) {
console.error("No response from OpenRouter");