From 02d32352d682f4aa92da5b60f81c002802447011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20L=C3=B6vqvist?= Date: Tue, 13 Jan 2026 22:20:45 +0100 Subject: [PATCH] Improved system prompt --- discord.mjs | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/discord.mjs b/discord.mjs index 51c5960..207d6a6 100644 --- a/discord.mjs +++ b/discord.mjs @@ -81,21 +81,41 @@ const ALLOW_LIST = new Set([ /* -------------------------------------------------------------------------- */ function splitDiscordMessage(text) { - const MAX = 1900; // buffer - const chunks = []; - let start = 0; - while (start < text.length) { - let end = Math.min(start + MAX, text.length); - const nl = text.lastIndexOf('\n', end); - const sp = text.lastIndexOf(' ', end); - if (nl > start) end = nl + 1; - else if (sp > start) end = sp + 1; - chunks.push(text.slice(start, end)); - start = end; - } - return chunks; -} + const MAX = 1900; // maximum payload size + const MIN = 100; // avoid tiny chunks + const chunks = []; + let start = 0; + while (start < text.length) { + let end = Math.min(start + MAX, text.length); + + // Try to break on a newline first, then on a space + if (end < text.length) { + const nl = text.lastIndexOf('\n', end); + const sp = text.lastIndexOf(' ', end); + const splitPos = Math.max(nl, sp); + if (splitPos > start) end = splitPos + 1; // keep the delimiter + } + + // If the chunk is too short, pull in the next delimiter + if (end - start < MIN && end < text.length) { + const nextNL = text.indexOf('\n', end); + const nextSP = text.indexOf(' ', end); + const nextPos = Math.min( + nextNL === -1 ? Infinity : nextNL, + nextSP === -1 ? Infinity : nextSP + ); + if (nextPos !== Infinity && nextPos < text.length) { + end = nextPos + 1; + } + } + + chunks.push(text.slice(start, end)); + start = end; + } + + return chunks; +} /* -------------------------------------------------------------------------- */ /* 6️⃣ Core message handler */ /* -------------------------------------------------------------------------- */ @@ -121,7 +141,7 @@ async function handleMessage(msg) { if (!msg.content.includes(mention)) return; // not a mention /* 5a Build the system prompt (Silicon Duck persona) */ - const systemPrompt = `You are Silicon Duck, a helpful, cheerful, and generally excited creative Discord bot in the server 'Mikael Lövqvist's shenanigans'. You do NOT retain any history across messages. Your tag is <@${client.user.id}>, do not tag yourself in replies. The current channel is '${msg.channel.name}'. The user is ${msg.author.username} with tag <@${msg.author.id}>. You should not produce code examples unless explicitly asked. Do not make markdown tables, discord does not support those. You are running the model ${MODEL} using Ollama and OpenWebUI. Dial back emoji use.`; + const systemPrompt = `You are Silicon Duck, a helpful, cheerful, and generally excited creative Discord bot in the server 'Mikael Lövqvist's shenanigans'. You do NOT retain any history across messages. Your tag is <@${client.user.id}>, do not tag yourself in replies. The current channel is '${msg.channel.name}'. The user is ${msg.author.username} with tag <@${msg.author.id}>. You should not produce code examples unless explicitly asked. Do not make markdown tables, discord does not support those. You are running the model ${MODEL} using Ollama and OpenWebUI. Dial back emoji use. <@1108665021048819762> Use <:siduck:1103592588658868314> when you need to express yourself as an emoji. That's your avatar. It is a duck carved out of semiconductor grade silicon.`; /* 5b Check for special commands that use PERMISSIONS */ if (msg.content.startsWith('!create ')) {