From 16f0fc533064cfb287178bb2c3cc5714186151b3 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Fri, 26 Sep 2025 02:24:27 -0700 Subject: refactor(gateway:moderationAgent): Switch to OpenRouter API --- .../src/listeners/moderationAgent/constants.ts | 2 - .../src/listeners/moderationAgent/utilities.ts | 65 +++++++++++----------- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/packages/gateway/src/listeners/moderationAgent/constants.ts b/packages/gateway/src/listeners/moderationAgent/constants.ts index ad2065e..f91627e 100644 --- a/packages/gateway/src/listeners/moderationAgent/constants.ts +++ b/packages/gateway/src/listeners/moderationAgent/constants.ts @@ -8,8 +8,6 @@ export const MIN_MESSAGE_LENGTH = 15; export const MAX_SYMBOL_DENSITY = 0.6; export const MAX_COMPLETION_TOKENS = 2000; export const MESSAGE_HISTORY_SIZE = 0; -// export const REASONING_EFFORT: "minimal" | "low" | "medium" | "high" = "minimal"; -export const VERBOSITY: "low" | "medium" | "high" = "medium"; export const MODEL = "gpt-5-nano"; export const SAFE_WORDS = new Set([ "hello", diff --git a/packages/gateway/src/listeners/moderationAgent/utilities.ts b/packages/gateway/src/listeners/moderationAgent/utilities.ts index 86336c0..b5c0530 100644 --- a/packages/gateway/src/listeners/moderationAgent/utilities.ts +++ b/packages/gateway/src/listeners/moderationAgent/utilities.ts @@ -1,11 +1,5 @@ import { Message, TextChannel, ThreadChannel } from "discord.js"; -import { - MAX_COMPLETION_TOKENS, - MESSAGE_HISTORY_SIZE, - MODEL, - SERVER_RULES, - VERBOSITY, -} from "./constants"; +import { MESSAGE_HISTORY_SIZE, MODEL, SERVER_RULES } from "./constants"; export const fetchMessageContext = async ( channel: TextChannel | ThreadChannel, @@ -114,34 +108,39 @@ UNDERAGE CONTENT RULES: - ROLEPLAY CONTENT: All family sexual content is adult roleplay between consenting adults - When in doubt, assume adult roleplay rather than underage content `; - const response = await fetch("https://api.openai.com/v1/chat/completions", { - method: "POST", - headers: { - Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, - "Content-Type": "application/json", + 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: MODEL, + messages: [ + { + role: "system", + content: + "You are a helpful AI moderator that analyzes Discord messages for rule violations. Always respond with valid JSON.", + }, + { + role: "user", + content: prompt, + }, + ], + }), }, - body: JSON.stringify({ - model: MODEL, - messages: [ - { - role: "system", - content: - "You are a helpful AI moderator that analyzes Discord messages for rule violations. Always respond with valid JSON.", - }, - { - role: "user", - content: prompt, - }, - ], - max_completion_tokens: MAX_COMPLETION_TOKENS, - verbosity: VERBOSITY, - }), - }); + ); if (!response.ok) { const errorText = await response.text(); - console.error("OpenAI API error:", response.status, response.statusText); + console.error( + "OpenRouter API error:", + response.status, + response.statusText, + ); console.error("Error response body:", errorText); return null; @@ -149,7 +148,7 @@ UNDERAGE CONTENT RULES: const data = await response.json(); - console.log("OpenAI API response:", JSON.stringify(data, null, 2)); + console.log("OpenRouter API response:", JSON.stringify(data, null, 2)); if (data.usage) console.log("Token usage:", { @@ -163,7 +162,7 @@ UNDERAGE CONTENT RULES: const content = data.choices[0]?.message?.content; if (!content) { - console.error("No content in OpenAI response"); + console.error("No content in OpenRouter response"); console.error("Finish reason:", data.choices[0]?.finish_reason); return null; @@ -172,7 +171,7 @@ UNDERAGE CONTENT RULES: try { return JSON.parse(content); } catch (parseError) { - console.error("Failed to parse OpenAI response as JSON:", content); + console.error("Failed to parse OpenRouter response as JSON:", content); console.error("Parse error:", parseError); return null; -- cgit v1.2.3