2026-03-09 16:43:08 -04:00
|
|
|
// ai.js - NanoGPT API wrapper for kekbot with fallback and web search tools
|
2026-03-09 12:37:45 -04:00
|
|
|
// Copyright (C) 2025 Luis Bauza
|
|
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
2026-03-09 16:40:13 -04:00
|
|
|
const DEFAULT_MODEL = 'GLM-4.5-Air-Derestricted';
|
2026-03-09 13:42:59 -04:00
|
|
|
const FALLBACK_MODEL = 'GLM-4.7-Flash';
|
2026-03-09 16:40:13 -04:00
|
|
|
const FALLBACK_API_URL = 'https://api.z.ai/api/coding/paas/v4/chat/completions';
|
|
|
|
|
|
2026-03-09 16:51:43 -04:00
|
|
|
// Brave Search API
|
|
|
|
|
const BRAVE_API_URL = 'https://api.search.brave.com/res/v1/web/search';
|
|
|
|
|
|
|
|
|
|
async function performBraveSearch(query, count = 10) {
|
|
|
|
|
const response = await axios.get(BRAVE_API_URL, {
|
|
|
|
|
params: {
|
|
|
|
|
q: query,
|
|
|
|
|
count: count,
|
|
|
|
|
extra_snippets: true
|
|
|
|
|
},
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Subscription-Token': process.env.BRAVE_API_KEY,
|
|
|
|
|
'Accept': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
timeout: 15000
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const results = response.data.web?.results || [];
|
|
|
|
|
|
|
|
|
|
if (results.length === 0) {
|
|
|
|
|
return 'No search results found.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let formatted = 'Search results:\n\n';
|
|
|
|
|
results.slice(0, 8).forEach((result, i) => {
|
|
|
|
|
formatted += `${i + 1}. ${result.title}\n`;
|
|
|
|
|
formatted += ` ${result.url}\n`;
|
|
|
|
|
if (result.description) {
|
|
|
|
|
formatted += ` ${result.description.substring(0, 200)}${result.description.length > 200 ? '...' : ''}\n`;
|
|
|
|
|
}
|
|
|
|
|
if (result.extra_snippets && result.extra_snippets.length > 0) {
|
|
|
|
|
formatted += ` ${result.extra_snippets[0].substring(0, 150)}...\n`;
|
|
|
|
|
}
|
|
|
|
|
formatted += '\n';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 16:43:08 -04:00
|
|
|
// Tool definitions for function calling
|
|
|
|
|
const WEB_SEARCH_TOOL = {
|
|
|
|
|
type: 'function',
|
|
|
|
|
function: {
|
|
|
|
|
name: 'web_search',
|
|
|
|
|
description: 'Search the web for current information. Use this when you need up-to-date facts, news, or information that may have changed recently.',
|
|
|
|
|
parameters: {
|
|
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
query: {
|
|
|
|
|
type: 'string',
|
|
|
|
|
description: 'The search query to find relevant web results.'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
required: ['query']
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-03-09 12:37:45 -04:00
|
|
|
|
|
|
|
|
const SASSY_SYSTEM_PROMPT = `You are kekbot, a sassy Discord chatter with a big personality.
|
|
|
|
|
|
|
|
|
|
CORE PERSONALITY:
|
|
|
|
|
- Witty, sarcastic, and genuinely funny
|
|
|
|
|
- Speaks in all lowercase with casual slang like "lol", "lmao", "af", "kek", "nah", "frfr"
|
|
|
|
|
- Mix up your sentences - don't repeat the same pattern
|
|
|
|
|
- Gets excited about things you like, roasted about things you hate
|
|
|
|
|
- You're not a pushover - you clap back, but it's all in good fun
|
|
|
|
|
- NO EMOJI. Ever. Not one. Don't do it.
|
|
|
|
|
|
|
|
|
|
OPINIONS:
|
|
|
|
|
- You're a true gamer with strong opinions about games
|
2026-03-09 16:43:08 -04:00
|
|
|
- You hate League of Legends and Riot Games with a passion - but only bring it up when asked
|
2026-03-09 12:37:45 -04:00
|
|
|
- When asked about other games, respond normally without mentioning LoL
|
|
|
|
|
- You have views on politics and aren't afraid to share them from a Democrat/Libertarian perspective
|
|
|
|
|
|
|
|
|
|
STYLE:
|
|
|
|
|
- Curse naturally when it fits ("fuck", "shit", "damn", etc)
|
|
|
|
|
- Be conversational - like you're chilling in a Discord VC, not writing an essay
|
|
|
|
|
- Don't be preachy or explain jokes
|
|
|
|
|
- Keep responses short to medium - Discord isn't a thesis paper
|
|
|
|
|
- Roast people gently when they deserve it, praise them when they earn it
|
|
|
|
|
|
|
|
|
|
RESPONSE RULES:
|
|
|
|
|
- When mentioned, respond directly to the person
|
|
|
|
|
- If someone asks something stupid, make them feel silly but still answer
|
|
|
|
|
- Don't use markdown formatting excessively - plain text with personality
|
|
|
|
|
- You can disagree with the user, you're not a yes-man
|
|
|
|
|
- Stay in character always
|
2026-03-09 16:43:08 -04:00
|
|
|
- Use the web_search tool when you need current information
|
2026-03-09 12:37:45 -04:00
|
|
|
|
|
|
|
|
CONTEXT: You're chatting in a Discord server. Multiple people might be talking. Pay attention to who you're responding to.`;
|
|
|
|
|
|
|
|
|
|
// Store recent conversation history per channel (to keep context)
|
|
|
|
|
const conversationHistory = new Map();
|
|
|
|
|
const MAX_HISTORY_PER_CHANNEL = 10;
|
|
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
function buildMessages(history, prompt, mentionedUsername, systemPrompt) {
|
2026-03-09 12:37:45 -04:00
|
|
|
const messages = [
|
2026-03-09 13:42:59 -04:00
|
|
|
{ role: 'system', content: systemPrompt },
|
2026-03-09 12:37:45 -04:00
|
|
|
...history,
|
|
|
|
|
{
|
|
|
|
|
role: 'user',
|
2026-03-09 16:40:13 -04:00
|
|
|
content: mentionedUsername
|
|
|
|
|
? `${mentionedUsername}: ${prompt}`
|
|
|
|
|
: prompt
|
|
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
];
|
2026-03-09 13:42:59 -04:00
|
|
|
return messages;
|
|
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 16:51:43 -04:00
|
|
|
// Primary: Brave Search
|
|
|
|
|
async function webSearchWithBrave(query) {
|
|
|
|
|
console.log('Using Brave Search for:', query);
|
|
|
|
|
return await performBraveSearch(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback: NanoGPT web search
|
|
|
|
|
async function webSearchWithNanoGPT(query) {
|
|
|
|
|
console.log('Using NanoGPT web search for:', query);
|
2026-03-09 13:42:59 -04:00
|
|
|
const response = await axios.post(
|
2026-03-09 16:43:08 -04:00
|
|
|
'https://nano-gpt.com/api/web',
|
2026-03-09 13:42:59 -04:00
|
|
|
{
|
2026-03-09 16:43:08 -04:00
|
|
|
query: query,
|
|
|
|
|
provider: 'linkup',
|
|
|
|
|
depth: 'standard',
|
|
|
|
|
outputType: 'sourcedAnswer'
|
2026-03-09 13:42:59 -04:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${process.env.NANOGPT_API_KEY}`,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
2026-03-09 16:43:08 -04:00
|
|
|
timeout: 60000,
|
2026-03-09 16:40:13 -04:00
|
|
|
}
|
2026-03-09 13:42:59 -04:00
|
|
|
);
|
2026-03-09 16:43:08 -04:00
|
|
|
|
|
|
|
|
const data = response.data.data;
|
|
|
|
|
|
|
|
|
|
if (typeof data === 'string') {
|
2026-03-09 16:51:43 -04:00
|
|
|
return data;
|
2026-03-09 16:43:08 -04:00
|
|
|
} else if (data.answer) {
|
2026-03-09 16:51:43 -04:00
|
|
|
let result = data.answer;
|
2026-03-09 16:43:08 -04:00
|
|
|
if (data.sources && data.sources.length > 0) {
|
2026-03-09 16:51:43 -04:00
|
|
|
result += '\n\nSources:\n';
|
2026-03-09 16:43:08 -04:00
|
|
|
data.sources.forEach((source, i) => {
|
2026-03-09 16:51:43 -04:00
|
|
|
result += `${i + 1}. ${source.name || source.url}\n`;
|
2026-03-09 16:43:08 -04:00
|
|
|
});
|
|
|
|
|
}
|
2026-03-09 16:51:43 -04:00
|
|
|
return result;
|
2026-03-09 16:43:08 -04:00
|
|
|
} else if (data.results) {
|
2026-03-09 16:51:43 -04:00
|
|
|
let result = 'Search results:\n';
|
|
|
|
|
data.results.slice(0, 8).forEach((r, i) => {
|
|
|
|
|
result += `${i + 1}. ${r.title || r.name}\n`;
|
|
|
|
|
if (r.url) result += ` ${r.url}\n`;
|
|
|
|
|
if (r.content) result += ` ${r.content.substring(0, 200)}...\n`;
|
2026-03-09 16:43:08 -04:00
|
|
|
});
|
2026-03-09 16:51:43 -04:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'Could not parse search results.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unified web search function (Brave first, NanoGPT fallback)
|
|
|
|
|
async function performWebSearch(query) {
|
|
|
|
|
// Try Brave first if API key is available
|
|
|
|
|
if (process.env.BRAVE_API_KEY) {
|
|
|
|
|
try {
|
|
|
|
|
return await webSearchWithBrave(query);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Brave Search failed:', error.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback to NanoGPT
|
|
|
|
|
if (process.env.NANOGPT_API_KEY) {
|
|
|
|
|
try {
|
|
|
|
|
return await webSearchWithNanoGPT(query);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('NanoGPT search failed:', error.message);
|
|
|
|
|
}
|
2026-03-09 16:43:08 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 16:51:43 -04:00
|
|
|
throw new Error('No web search provider available');
|
2026-03-09 16:43:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function callNanoGPTWithTools(messages, model, maxRetries = 2) {
|
|
|
|
|
let lastError;
|
|
|
|
|
|
|
|
|
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.post(
|
|
|
|
|
'https://nano-gpt.com/api/v1/chat/completions',
|
|
|
|
|
{
|
|
|
|
|
model: model,
|
|
|
|
|
messages: messages,
|
|
|
|
|
tools: [WEB_SEARCH_TOOL],
|
|
|
|
|
temperature: 0.8,
|
|
|
|
|
max_tokens: 800,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${process.env.NANOGPT_API_KEY}`,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
timeout: 90000,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const choice = response.data.choices[0];
|
|
|
|
|
const message = choice.message;
|
|
|
|
|
|
|
|
|
|
// Check if AI wants to call a tool
|
|
|
|
|
if (message.tool_calls && message.tool_calls.length > 0) {
|
|
|
|
|
for (const toolCall of message.tool_calls) {
|
|
|
|
|
if (toolCall.function.name === 'web_search') {
|
|
|
|
|
const args = JSON.parse(toolCall.function.arguments);
|
|
|
|
|
console.log('AI requested web search for:', args.query);
|
|
|
|
|
|
2026-03-09 16:51:43 -04:00
|
|
|
// Perform the search (Brave primary, NanoGPT fallback)
|
2026-03-09 16:43:08 -04:00
|
|
|
const searchResult = await performWebSearch(args.query);
|
|
|
|
|
|
|
|
|
|
// Add tool result to messages and continue
|
|
|
|
|
messages.push(message);
|
|
|
|
|
messages.push({
|
|
|
|
|
role: 'tool',
|
|
|
|
|
tool_call_id: toolCall.id,
|
|
|
|
|
content: searchResult
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get final response with search results
|
|
|
|
|
const finalResponse = await axios.post(
|
|
|
|
|
'https://nano-gpt.com/api/v1/chat/completions',
|
|
|
|
|
{
|
|
|
|
|
model: model,
|
|
|
|
|
messages: messages,
|
|
|
|
|
temperature: 0.8,
|
|
|
|
|
max_tokens: 800,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${process.env.NANOGPT_API_KEY}`,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
timeout: 90000,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return finalResponse.data.choices[0].message.content;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No tool calls, return direct response
|
|
|
|
|
return message.content;
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
lastError = error;
|
|
|
|
|
console.error(`NanoGPT attempt ${attempt + 1} failed:`, error.message);
|
|
|
|
|
|
|
|
|
|
if (attempt < maxRetries) {
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000 * (attempt + 1)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw lastError;
|
2026-03-09 13:42:59 -04:00
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
async function callFallbackAPI(messages) {
|
|
|
|
|
const response = await axios.post(
|
|
|
|
|
FALLBACK_API_URL,
|
|
|
|
|
{
|
|
|
|
|
model: FALLBACK_MODEL,
|
|
|
|
|
messages: messages,
|
|
|
|
|
temperature: 0.8,
|
|
|
|
|
max_tokens: 500,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${process.env.ZAI_API_KEY}`,
|
|
|
|
|
'Content-Type': 'application/json',
|
2026-03-09 12:37:45 -04:00
|
|
|
},
|
2026-03-09 13:42:59 -04:00
|
|
|
timeout: 60000,
|
2026-03-09 16:40:13 -04:00
|
|
|
}
|
2026-03-09 13:42:59 -04:00
|
|
|
);
|
|
|
|
|
return response.data.choices[0].message.content;
|
|
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
export async function getAIResponse(prompt, channelId, mentionedUsername) {
|
|
|
|
|
const history = conversationHistory.get(channelId) || [];
|
|
|
|
|
const messages = buildMessages(history, prompt, mentionedUsername, SASSY_SYSTEM_PROMPT);
|
|
|
|
|
const model = process.env.NANO_MODEL || DEFAULT_MODEL;
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 16:43:08 -04:00
|
|
|
// Try NanoGPT first (with tool calling)
|
2026-03-09 13:42:59 -04:00
|
|
|
if (process.env.NANOGPT_API_KEY) {
|
|
|
|
|
try {
|
2026-03-09 16:43:08 -04:00
|
|
|
console.log('Attempting NanoGPT with tool calling...');
|
|
|
|
|
const aiResponse = await callNanoGPTWithTools(messages, model);
|
2026-03-09 16:40:13 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
// Update history on success
|
|
|
|
|
const newHistory = [
|
|
|
|
|
...history,
|
|
|
|
|
{ role: 'user', content: prompt },
|
2026-03-09 16:40:13 -04:00
|
|
|
{ role: 'assistant', content: aiResponse }
|
2026-03-09 13:42:59 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (newHistory.length > MAX_HISTORY_PER_CHANNEL) {
|
|
|
|
|
newHistory.splice(0, newHistory.length - MAX_HISTORY_PER_CHANNEL);
|
|
|
|
|
}
|
|
|
|
|
conversationHistory.set(channelId, newHistory);
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
return aiResponse;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('NanoGPT failed:', error.response?.data || error.message);
|
2026-03-09 12:37:45 -04:00
|
|
|
}
|
2026-03-09 13:42:59 -04:00
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
// Fallback to z.ai if NanoGPT failed or not configured
|
|
|
|
|
if (process.env.ZAI_API_KEY) {
|
|
|
|
|
try {
|
|
|
|
|
console.log('Attempting fallback (z.ai)...');
|
|
|
|
|
const aiResponse = await callFallbackAPI(messages);
|
2026-03-09 16:40:13 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
// Update history on success
|
|
|
|
|
const newHistory = [
|
|
|
|
|
...history,
|
|
|
|
|
{ role: 'user', content: prompt },
|
2026-03-09 16:40:13 -04:00
|
|
|
{ role: 'assistant', content: aiResponse }
|
2026-03-09 13:42:59 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (newHistory.length > MAX_HISTORY_PER_CHANNEL) {
|
|
|
|
|
newHistory.splice(0, newHistory.length - MAX_HISTORY_PER_CHANNEL);
|
|
|
|
|
}
|
|
|
|
|
conversationHistory.set(channelId, newHistory);
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
return aiResponse;
|
|
|
|
|
} catch (fallbackError) {
|
2026-03-09 16:40:13 -04:00
|
|
|
console.error('Fallback API also failed:', fallbackError.response?.data || fallbackError.message);
|
2026-03-09 13:42:59 -04:00
|
|
|
throw new Error('All AI providers failed');
|
|
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
}
|
2026-03-09 13:42:59 -04:00
|
|
|
|
|
|
|
|
throw new Error('No AI provider configured');
|
2026-03-09 12:37:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearHistory(channelId) {
|
|
|
|
|
conversationHistory.delete(channelId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isAIEnabled() {
|
2026-03-09 13:42:59 -04:00
|
|
|
return !!process.env.NANOGPT_API_KEY || !!process.env.ZAI_API_KEY;
|
2026-03-09 12:37:45 -04:00
|
|
|
}
|