// ==UserScript==
// @name SmolLLM
// @namespace http://tampermonkey.net/
// @version 0.1.11
// @description LLM utility library
// @author RoCry
// @grant GM_xmlhttpRequest
// @require https://update.greasyfork.org/scripts/528703/1546610/SimpleBalancer.js
// @license MIT
// ==/UserScript==
class SmolLLM {
constructor() {
// Ensure SimpleBalancer is available
if (typeof SimpleBalancer === 'undefined') {
throw new Error('SimpleBalancer is required for SmolLLM to work');
}
// Verify GM_xmlhttpRequest is available
if (typeof GM_xmlhttpRequest === 'undefined') {
throw new Error('GM_xmlhttpRequest is required for SmolLLM to work');
}
this.balancer = new SimpleBalancer();
this.logger = console;
// Buffer for incomplete SSE messages
this.sseBuffer = '';
// Track previously processed data length to avoid duplication
this.processedLength = 0;
}
/**
* Prepares request data based on the provider
*
* @param {string} prompt - User prompt
* @param {string} systemPrompt - System prompt
* @param {string} modelName - Model name
* @param {string} providerName - Provider name (anthropic, openai, gemini)
* @param {string} baseUrl - API base URL
* @returns {Object} - {url, data} for the request
*/
prepareRequestData(prompt, systemPrompt, modelName, providerName, baseUrl) {
let url, data;
if (providerName === 'anthropic') {
url = `${baseUrl}/v1/messages`;
data = {
model: modelName,
max_tokens: 4096,
messages: [{ role: 'user', content: prompt }],
stream: true
};
if (systemPrompt) {
data.system = systemPrompt;
}
} else if (providerName === 'gemini') {
url = `${baseUrl}/v1beta/models/${modelName}:streamGenerateContent?alt=sse`;
data = {
contents: [{ parts: [{ text: prompt }] }]
};
if (systemPrompt) {
data.system_instruction = { parts: [{ text: systemPrompt }] };
}
} else {
// OpenAI compatible APIs
const messages = [];
if (systemPrompt) {
messages.push({ role: 'system', content: systemPrompt });
}
messages.push({ role: 'user', content: prompt });
data = {
messages: messages,
model: modelName,
stream: true
};
// Handle URL based on suffix
if (baseUrl.endsWith('#')) {
url = baseUrl.slice(0, -1); // Remove the # and use exact URL
} else if (baseUrl.endsWith('/')) {
url = `${baseUrl}chat/completions`; // Skip v1 prefix
} else {
url = `${baseUrl}/v1/chat/completions`; // Default pattern
}
}
return { url, data };
}
/**
* Prepares headers for authentication based on the provider
*
* @param {string} providerName - Provider name
* @param {string} apiKey - API key
* @returns {Object} - Request headers
*/
prepareHeaders(providerName, apiKey) {
const headers = {
'Content-Type': 'application/json'
};
if (providerName === 'anthropic') {
headers['X-API-Key'] = apiKey;
headers['Anthropic-Version'] = '2023-06-01';
} else if (providerName === 'gemini') {
headers['X-Goog-Api-Key'] = apiKey;
} else {
headers['Authorization'] = `Bearer ${apiKey}`;
}
return headers;
}
/**
* Process SSE stream data for different providers
*
* @param {string} chunk - Data chunk from SSE
* @param {string} providerName - Provider name
* @returns {string|null} - Extracted text content or null
*/
processStreamChunk(chunk, providerName) {
if (!chunk || chunk === '[DONE]') return null;
try {
console.log(`Processing chunk for ${providerName}:`, chunk.substring(0, 100) + (chunk.length > 100 ? '...' : ''));
const data = JSON.parse(chunk);
if (providerName === 'gemini') {
const candidates = data.candidates || [];
if (candidates.length > 0 && candidates[0].content) {
const parts = candidates[0].content.parts;
if (parts && parts.length > 0) {
return parts[0].text || '';
}
}
return null;
} else if (providerName === 'anthropic') {
// Handle content_block_delta which contains the actual text
if (data.type === 'content_block_delta') {
const delta = data.delta || {};
if (delta.type === 'text_delta' || delta.text) {
return delta.text || '';
}
}
// Anthropic sends various event types - only some contain text
return null;
} else {
// OpenAI compatible format
const choice = (data.choices || [{}])[0];
if (choice.finish_reason !== null && choice.finish_reason !== undefined) {
return null; // End of generation
}
return choice.delta && choice.delta.content ? choice.delta.content : null;
}
} catch (e) {
console.error(`Error parsing chunk: ${e.message}, chunk: ${chunk}`);
return null;
}
}
/**
* Process SSE data and extract complete messages
*
* @param {string} rawData - Raw data received from SSE
* @param {string} providerName - Provider name for chunk processing
* @param {Function} handleText - Callback for processed text chunks
*/
processSseData(rawData, providerName, handleText) {
// Append new data to existing buffer
this.sseBuffer += rawData;
// More robust SSE message extraction
// Match data: lines followed by either double newlines or end of input
const regex = /data: (.*?)(?:\n\n|\r\n\r\n|$)/g;
let match;
let processedToIndex = 0;
while ((match = regex.exec(this.sseBuffer)) !== null) {
try {
const content = match[1];
if (content && content.trim() && content !== '[DONE]') {
const textChunk = this.processStreamChunk(content, providerName);
if (textChunk) {
handleText(textChunk);
}
}
processedToIndex = match.index + match[0].length;
} catch (e) {
console.error('Error processing SSE message:', e);
}
}
// Only trim the fully processed messages from the buffer
if (processedToIndex > 0) {
this.sseBuffer = this.sseBuffer.substring(processedToIndex);
}
}
/**
* Makes a request to the LLM API and handles streaming responses
*
* @param {Object} params - Request parameters
* @returns {Promise<string>} - Full response text
*/
async askLLM({
prompt,
providerName,
systemPrompt = '',
model,
apiKey,
baseUrl,
handler = null,
timeout = 60000
}) {
if (!prompt || !providerName || !model || !apiKey || !baseUrl) {
throw new Error('Required parameters missing');
}
// Use balancer to choose API key and base URL pair
[apiKey, baseUrl] = this.balancer.choosePair(apiKey, baseUrl);
const { url, data } = this.prepareRequestData(
prompt, systemPrompt, model, providerName, baseUrl
);
const headers = this.prepareHeaders(providerName, apiKey);
// Log request info (with masked API key)
const apiKeyPreview = `${apiKey.slice(0, 5)}...${apiKey.slice(-4)}`;
this.logger.info(
`Sending request to ${url} with model=${model}, api_key=${apiKeyPreview}, prompt_length=${prompt.length}`
);
// Additional debug info
this.logger.debug(`Provider: ${providerName}, Request data:`, JSON.stringify(data).substring(0, 500));
// Reset SSE buffer and processed length before new request
this.sseBuffer = '';
this.processedLength = 0;
return new Promise((resolve, reject) => {
let responseText = '';
let timeoutId;
let isResolved = false;
let responseBuffer = '';
// A wrapper for the handler that updates responseText
const handleChunk = (textChunk) => {
if (!textChunk) return;
responseText += textChunk;
if (handler && typeof handler === 'function') {
handler(textChunk);
}
};
// Set timeout
if (timeout) {
timeoutId = setTimeout(() => {
if (!isResolved) {
isResolved = true;
reject(new Error(`Request timed out after ${timeout}ms`));
}
}, timeout);
}
GM_xmlhttpRequest({
method: 'POST',
url: url,
headers: headers,
data: JSON.stringify(data),
responseType: 'text',
onload: (response) => {
if (response.status !== 200) {
clearTimeout(timeoutId);
if (!isResolved) {
isResolved = true;
reject(new Error(`API request failed: ${response.status} - ${response.responseText}`));
}
} else {
// Process any buffered or new content
if (response.responseText) {
responseBuffer = response.responseText;
// Special handling for Chrome which may buffer the whole response
setTimeout(() => {
if (responseText.length === 0 && responseBuffer) {
console.log(`Processing delayed response (${responseBuffer.length} bytes)`);
this.processSseData(responseBuffer, providerName, handleChunk);
}
if (!isResolved) {
isResolved = true;
resolve(responseText);
}
}, 100);
} else if (!isResolved) {
isResolved = true;
resolve(responseText);
}
clearTimeout(timeoutId);
}
},
onreadystatechange: (state) => {
if (state.readyState === 4 && !isResolved) {
// Wait a bit to process any pending chunks
setTimeout(() => {
if (!isResolved) {
isResolved = true;
clearTimeout(timeoutId);
resolve(responseText);
}
}, 100);
}
},
onprogress: (response) => {
// Get only the new data since last progress event
const newData = response.responseText.substring(this.processedLength);
this.processedLength = response.responseText.length;
if (!newData) return;
// Save to buffer in case we need it later (for Chrome)
responseBuffer = response.responseText;
// Process only the new data with our improved SSE parser
this.processSseData(newData, providerName, handleChunk);
},
onerror: (error) => {
clearTimeout(timeoutId);
if (!isResolved) {
isResolved = true;
console.error('Request error:', error);
reject(new Error(`Request failed: ${error.error || JSON.stringify(error)}`));
}
},
ontimeout: () => {
clearTimeout(timeoutId);
if (!isResolved) {
isResolved = true;
reject(new Error(`Request timed out after ${timeout}ms`));
}
}
});
});
}
}
// Make it available globally
window.SmolLLM = SmolLLM;
// Export for module systems if needed
if (typeof module !== 'undefined') {
module.exports = SmolLLM;
}