Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "*"`
Expand Down
6 changes: 3 additions & 3 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand Down
81 changes: 33 additions & 48 deletions llm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 post(url, body);
return r.json();
}
}

// OpenAIProvider.js
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"activeTab","tabs","sidePanel"
],
"background": {
"service_worker": "background.js",
"service_worker": "service_worker.js",
"type": "module"
},
"host_permissions": ["http://*/*", "https://*/*", "*://*/*"],
Expand Down
2 changes: 1 addition & 1 deletion background.js → service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion sidepanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });