diff options
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/gateway/bun.lock | 3 | ||||
| -rw-r--r-- | packages/gateway/package.json | 1 | ||||
| -rw-r--r-- | packages/gateway/src/listeners/messageCreate/aiCommandHandler/index.ts | 66 |
3 files changed, 29 insertions, 41 deletions
diff --git a/packages/gateway/bun.lock b/packages/gateway/bun.lock index 7a1803e..e23ad2d 100644 --- a/packages/gateway/bun.lock +++ b/packages/gateway/bun.lock @@ -11,6 +11,7 @@ "dotenv": "^16.0.3", "iqdb-client": "^3.0.0", "libsodium-wrappers": "^0.7.15", + "openai": "^6.1.0", "sodium": "^3.0.2", "ws": "^8.18.3", }, @@ -196,6 +197,8 @@ "nth-check": ["[email protected]", "", { "dependencies": { "boolbase": "^1.0.0" } }, "sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w=="], + "openai": ["[email protected]", "", { "peerDependencies": { "ws": "^8.18.0", "zod": "^3.25 || ^4.0" }, "optionalPeers": ["ws", "zod"], "bin": { "openai": "bin/cli" } }, "sha512-5sqb1wK67HoVgGlsPwcH2bUbkg66nnoIYKoyV9zi5pZPqh7EWlmSrSDjAh4O5jaIg/0rIlcDKBtWvZBuacmGZg=="], + "parse5": ["[email protected]", "", { "dependencies": { "entities": "^6.0.0" } }, "sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw=="], "parse5-htmlparser2-tree-adapter": ["[email protected]", "", { "dependencies": { "domhandler": "^5.0.3", "parse5": "^7.0.0" } }, "sha512-ruw5xyKs6lrpo9x9rCZqZZnIUntICjQAd0Wsmp396Ul9lN/h+ifgVV1x1gZHi8euej6wTfpqX8j+BFQxF0NS/g=="], diff --git a/packages/gateway/package.json b/packages/gateway/package.json index f86b5e8..db22d09 100644 --- a/packages/gateway/package.json +++ b/packages/gateway/package.json @@ -18,6 +18,7 @@ "dotenv": "^16.0.3", "iqdb-client": "^3.0.0", "libsodium-wrappers": "^0.7.15", + "openai": "^6.1.0", "sodium": "^3.0.2", "ws": "^8.18.3" }, 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"); |