added a timeout

This commit is contained in:
tcsenpai 2025-03-11 11:31:42 +01:00
parent 050a74adad
commit 71278d18b3
5 changed files with 104 additions and 40 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
extension extension
pack_extension.sh pack_extension.sh
SpaceLLama.zip SpaceLLama.zip
yarn.lock
dist/*.xpi
dist/*.zip
node_modules

View File

@ -1,6 +1,6 @@
console.log("Background script loaded"); console.log("Background script loaded");
let isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox has `InstallTrigger` let isFirefox = typeof InstallTrigger !== "undefined"; // Firefox has `InstallTrigger`
let browser = isFirefox ? window.browser : chrome; let browser = isFirefox ? window.browser : chrome;
// Check if chrome.action or browser.action is available // Check if chrome.action or browser.action is available
@ -16,32 +16,36 @@ if (isFirefox && browser.browserAction) {
console.log("Injecting sidebar iframe into the page"); console.log("Injecting sidebar iframe into the page");
// Use the tab object properly here // Use the tab object properly here
browser.scripting.executeScript({ browser.scripting.executeScript(
{
target: { tabId: tab.id }, // Pass the tab ID correctly target: { tabId: tab.id }, // Pass the tab ID correctly
function: injectSidebar function: injectSidebar,
}, () => { },
() => {
if (browser.runtime.lastError) { if (browser.runtime.lastError) {
console.error("Error injecting sidebar:", browser.runtime.lastError.message); console.error(
"Error injecting sidebar:",
browser.runtime.lastError.message
);
} else { } else {
console.log("Sidebar injected successfully."); console.log("Sidebar injected successfully.");
} }
}); }
);
}); });
} }
// Function to inject the sidebar as an iframe in browsers like Chrome // Function to inject the sidebar as an iframe in browsers like Chrome
function injectSidebar() { function injectSidebar() {
// Check if the sidebar iframe is already injected // Check if the sidebar iframe is already injected
if (document.getElementById('sidebar-frame')) { if (document.getElementById("sidebar-frame")) {
console.log("Sidebar is already injected."); console.log("Sidebar is already injected.");
return; return;
} }
// Create an iframe for the sidebar // Create an iframe for the sidebar
const sidebarFrame = document.createElement('iframe'); const sidebarFrame = document.createElement("iframe");
sidebarFrame.id = 'sidebar-frame'; // Add an ID to prevent multiple injections sidebarFrame.id = "sidebar-frame"; // Add an ID to prevent multiple injections
sidebarFrame.src = chrome.runtime.getURL('sidebar/sidebar.html'); // Use the sidebar.html sidebarFrame.src = chrome.runtime.getURL("sidebar/sidebar.html"); // Use the sidebar.html
sidebarFrame.style.cssText = ` sidebarFrame.style.cssText = `
position: fixed; position: fixed;
top: 0; top: 0;
@ -175,7 +179,16 @@ async function summarizeChunk(
model, model,
tokenLimit tokenLimit
) { ) {
const response = await fetch(endpoint, { let response;
let maxRetries = 3;
let retryCount = 0;
let retryDelay = 1000;
// We will retry the request if it fails (three times)
// Each time we will wait longer before retrying (1, 2, 4 seconds)
// Each request will timeout after 25 * retryDelay
while (retryCount < maxRetries) {
try {
response = await fetch(endpoint, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -186,8 +199,30 @@ async function summarizeChunk(
stream: false, stream: false,
num_ctx: tokenLimit, num_ctx: tokenLimit,
}), }),
signal: AbortSignal.timeout(25 * retryDelay),
}); });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
break; // Success - exit the retry loop
} catch (error) {
console.error("Error in summarizeChunk:", error);
retryCount++;
if (retryCount >= maxRetries) {
throw new Error(
`Failed to summarize chunk after ${maxRetries} retries: ${error.message}`
);
}
console.log(`Retry ${retryCount}/${maxRetries} after ${retryDelay}ms`);
await new Promise((resolve) => setTimeout(resolve, retryDelay));
retryDelay *= 2;
}
}
// TODO Add bespoke-minicheck validation here // TODO Add bespoke-minicheck validation here
// LINK https://ollama.com/library/bespoke-minicheck // LINK https://ollama.com/library/bespoke-minicheck
let factCheck = false; let factCheck = false;
@ -196,13 +231,6 @@ async function summarizeChunk(
console.log(bespokeResponse); console.log(bespokeResponse);
} }
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`HTTP error! status: ${response.status}, message: ${errorText}`
);
}
const data = await response.json(); const data = await response.json();
return data.response; return data.response;
} }
@ -259,5 +287,5 @@ async function bespokeMinicheck(chunk, summary) {
}); });
// TODO Error handling // TODO Error handling
let response_text = await bespoke_response.text(); let response_text = await bespoke_response.text();
return response_text return response_text;
} }

26
build_xpi.sh Executable file
View File

@ -0,0 +1,26 @@
#!/bin/bash
# Set extension name
EXTENSION_NAME="SpaceLLama"
# Create a temporary directory for building
BUILD_DIR="./build"
mkdir -p $BUILD_DIR
# Copy all necessary files to the build directory
echo "Copying files to build directory..."
cp -r background.js content_scripts icon.png manifest.json options sidebar model_tokens.json $BUILD_DIR
# Navigate to the build directory
cd $BUILD_DIR
# Create the XPI file (which is just a ZIP file with .xpi extension)
echo "Creating XPI file..."
zip -r ../${EXTENSION_NAME}.xpi *
# Clean up
cd ..
echo "Cleaning up build directory..."
rm -rf $BUILD_DIR
echo "XPI file created: ${EXTENSION_NAME}.xpi"

6
build_xpi_webext.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
# Build the extension using web-ext
web-ext build --source-dir ./ --artifacts-dir ./dist --overwrite-dest
echo "XPI file created in ./dist directory"

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "SpaceLLama", "name": "SpaceLLama",
"version": "1.3", "version": "1.4",
"description": "Summarize web pages using Ollama. Supports custom models, token limits, system prompts, chunking, and more. See https://github.com/tcsenpai/spacellama for more information.", "description": "Summarize web pages using Ollama. Supports custom models, token limits, system prompts, chunking, and more. See https://github.com/tcsenpai/spacellama for more information.",
"permissions": ["activeTab", "storage", "<all_urls>", "tabs"], "permissions": ["activeTab", "storage", "<all_urls>", "tabs"],
"browser_action": { "browser_action": {