-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
120 lines (107 loc) · 3.33 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const ANTHROPIC_API_KEY = // your anthropic key
const OPENAI_API_KEY = // your openai key
async function sendToClaudeAI(text) {
const apiUrl = 'https://api.anthropic.com/v1/messages';
const requestBody = {
model: "claude-3-opus-20240229",
max_tokens: 1000,
messages: [
{
role: "user",
content: text
}
]
};
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': ANTHROPIC_API_KEY,
'anthropic-version': '2023-06-01'
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.content[0].text;
} catch (error) {
console.error('Error calling Claude AI:', error);
throw error;
}
}
async function sendToOpenAI(text) {
const apiUrl = 'https://api.openai.com/v1/chat/completions';
console.log('openai', text);
const requestBody = {
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: "You are a helpful assistant that summarizes web page content."
},
{
role: "user",
content: text
}
],
max_tokens: 500
};
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OPENAI_API_KEY}`
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.choices[0].message.content;
} catch (error) {
console.error('Error calling OpenAI:', error);
throw error;
}
}
async function summarizeText(text, aiService = 'openai') {
const promptWrapper = `Please determine the subject matter based on the following text and provide a summary. If some of the text does not relate to the overall subject matter, please remove it from your analysis summary:\n\n${text}`;
try {
if (aiService === 'claude') {
return await sendToClaudeAI(promptWrapper);
} else if (aiService === 'openai') {
return await sendToOpenAI(promptWrapper);
} else {
throw new Error('Invalid AI service specified');
}
} catch (error) {
console.error('Error in summarizeText:', error);
throw error;
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "summarize") {
chrome.tabs.query({active: true, currentWindow: true}, async function(tabs) {
try {
const response = await chrome.tabs.sendMessage(tabs[0].id, {action: "extractText"});
const extractedText = response.text;
const summary = await summarizeText(extractedText, 'openai');
console.log('summary from ai:', summary)
sendResponse({summary: summary});
} catch (error) {
console.error('Error:', error); // Debug log
sendResponse({error: error.message});
}
});
return true; // Indicates that the response is sent asynchronously
}
});
chrome.action.onClicked.addListener((tab) => {
console.log(' open side panel with tab id', tab.id)
chrome.sidePanel.open({tabId: tab.id});
});
console.log('Sending message to content script.');