-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.js
More file actions
48 lines (41 loc) · 1.59 KB
/
ai.js
File metadata and controls
48 lines (41 loc) · 1.59 KB
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
async function makeSummarizer(opts = {}) {
if (typeof Summarizer === 'undefined') return { error: 'SUMMARIZER_API_NOT_FOUND' };
const options = {
type: opts.type || 'key-points',
format: 'markdown',
length: opts.length || 'medium',
outputLanguage: 'en', // required by newer builds
expectedInputLanguages: ['en'],
monitor(m) {
m.addEventListener('downloadprogress', e =>
console.log('[Summarizer] downloadprogress', e.loaded)
);
}
};
// availability must match create() options
const availability = await Summarizer.availability(options);
if (availability === 'unavailable') return { error: 'SUMMARIZER_UNAVAILABLE' };
const summarizer = await Summarizer.create(options);
return { summarizer };
}
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
// ⚡ ping handler so SW can verify this page is ready
if (msg?.kind === 'AI_PING') { sendResponse({ ok: true }); return; }
if (msg?.kind !== 'AI_DO') return;
(async () => {
const inner = msg.payload;
try {
if (inner.kind === 'AI_SUMMARIZE') {
const { summarizer, error } = await makeSummarizer({ type: inner.style, length: inner.length });
if (error) { sendResponse({ error }); return; }
const summary = await summarizer.summarize(inner.text, { context: inner.context || '' });
sendResponse({ ok: true, summary });
return;
}
sendResponse({ error: 'UNKNOWN_KIND' });
} catch (e) {
sendResponse({ error: String(e) });
}
})();
return true; // keep channel open for async work
});