-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
34 lines (30 loc) · 968 Bytes
/
Copy pathbackground.js
File metadata and controls
34 lines (30 loc) · 968 Bytes
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
import { API_KEY } from './config.js';
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "summarize") {
summarizeText(request.text)
.then(summary => sendResponse({summary: summary}))
.catch(error => sendResponse({error: error.message}));
return true; // Indicates we will send a response asynchronously
}
});
async function summarizeText(text) {
const response = await fetch('https://api.cohere.ai/v1/summarize', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
text: text,
length: 'short',
format: 'paragraph',
model: 'summarize-xlarge',
additional_command: 'Summarize the following text concisely:'
}),
});
if (!response.ok) {
throw new Error('Failed to summarize text');
}
const data = await response.json();
return data.summary;
}