diff --git a/README.md b/README.md index 83cbb0e..5dc880e 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ FastDigest is a Chrome extension designed to summarize Hacker News comments usin 2. **Configure LLM Provider** - - Go to background.js. At the top, you will see a variable to set your OpenAI API Key. + - Go to service_worker.js. At the top, you will see a variable to set your OpenAI API Key. - FastDigest also supports Ollama(llama3.1) for using local LLMs. To use Ollama, you will need to do the following: - shut down Ollama if it is currently running - run `launchctl setenv OLLAMA_ORIGINS "*"` diff --git a/content.js b/content.js index 04c913e..8c09d4f 100644 --- a/content.js +++ b/content.js @@ -147,7 +147,7 @@ async function summarizeComment(commentElement) { commentsArray.push([parentComment, ...currentChunk]); } - // Send the array of chunks to the background script in one message + // Send the array of chunks to the service_worker script in one message try { await chrome.runtime.sendMessage({ action: "summarize_comments", @@ -261,7 +261,7 @@ async function summarizeAllComments() { commentsArray.push([...currentChunk]); } - // Send the array of chunks to the background script + // Send the array of chunks to the service_worker script try { await chrome.runtime.sendMessage({ action: "summarize_comments", @@ -296,7 +296,7 @@ async function sendSummarizationRequest(commentsText, accumulatedSummary) { window.addEventListener("load", () => { addSummarizeButtons(); - // listen for alert messages from the background script + // listen for alert messages from the service_worker script chrome.runtime.onMessage.addListener((message) => { if (message.action === "alert") { alert(message.message); diff --git a/llm.js b/llm.js index 49d7e9b..5885692 100644 --- a/llm.js +++ b/llm.js @@ -7,6 +7,17 @@ class LLMProvider { async summarizeStream(prompt, onChunk) { throw new Error("summarizeStream method must be implemented."); } + + async post(url, body) { + const headers = {'Content-Type': 'application/json'}; + if (this.apiKey) headers.authorization = `Bearer ${this.apiKey}`; + return fetch(url, {method: 'POST', headers, body: JSON.stringify(body)}); + } + + async postJson(url, body) { + const r = await this.post(url, body); + return r.json(); + } } // OpenAIProvider.js @@ -19,41 +30,25 @@ export class OpenAIProvider extends LLMProvider { } async summarize(prompt) { - const response = await fetch(this.apiUrl, { - method: "POST", - headers: { - Authorization: `Bearer ${this.apiKey}`, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - model: this.model, - messages: [ - { role: "system", content: "You are a helpful assistant." }, - { role: "user", content: prompt }, - ], - stream: false, - }), + const data = await this.postJson(this.apiUrl, { + model: this.model, + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: prompt }, + ], + stream: false, }); - - const data = await response.json(); return data.choices[0].message.content; } async summarizeStream(prompt, onChunk) { - const response = await fetch(this.apiUrl, { - method: "POST", - headers: { - Authorization: `Bearer ${this.apiKey}`, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - model: this.model, - messages: [ - { role: "system", content: "You are a helpful assistant." }, - { role: "user", content: prompt }, - ], - stream: true, - }), + const response = await this.post(this.apiUrl, { + model: this.model, + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: prompt }, + ], + stream: true, }); const reader = response.body.getReader(); @@ -91,29 +86,19 @@ export class OllamaProvider extends LLMProvider { } async summarize(prompt) { - const response = await fetch(this.apiUrl, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - model: this.model, - prompt: prompt, - stream: false, - }), + const data = await this.postJson(this.apiUrl, { + model: this.model, + prompt, + stream: false, }); - - const data = await response.json(); return data.response; } async summarizeStream(prompt, onChunk) { - const response = await fetch(this.apiUrl, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - model: this.model, - prompt: prompt, - stream: true, - }), + const response = await this.post(this.apiUrl, { + model: this.model, + prompt, + stream: true, }); const reader = response.body.getReader(); diff --git a/manifest.json b/manifest.json index 0857f33..8dd8843 100644 --- a/manifest.json +++ b/manifest.json @@ -11,7 +11,7 @@ "activeTab","tabs","sidePanel" ], "background": { - "service_worker": "background.js", + "service_worker": "service_worker.js", "type": "module" }, "host_permissions": ["http://*/*", "https://*/*", "*://*/*"], diff --git a/background.js b/service_worker.js similarity index 99% rename from background.js rename to service_worker.js index 0e5ee0a..8e806b6 100644 --- a/background.js +++ b/service_worker.js @@ -35,7 +35,7 @@ chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => { } }); -// background.js +// service_worker.js chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => { if (message.action === "summarize_comments") { if (isSummarizing) { diff --git a/sidepanel.js b/sidepanel.js index 7202e43..71c2a4c 100644 --- a/sidepanel.js +++ b/sidepanel.js @@ -55,7 +55,7 @@ chrome.runtime.onMessage.addListener((message) => { } }); -// Ensure the listener is set up before sending messages from the background +// Ensure the listener is set up before sending messages from the service_worker console.log("Side panel script loaded and listener set up."); chrome.runtime.sendMessage({ action: "sidepanel_ready" });