forked from htadashi/AI-Event-Scheduler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice_worker.js
65 lines (62 loc) · 2.71 KB
/
service_worker.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
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "create-gcal-url",
title: "Create Google Calendar event",
contexts: ["selection"]
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId == "create-gcal-url") {
chrome.storage.sync.get(["apiKey"], function(result) {
chrome.scripting.insertCSS({
target: { tabId: tab.id },
css: 'body { cursor: wait; }'
});
if (result.apiKey === undefined) {
alert("API key is not set. Please set it in the extension options.");
chrome.runtime.openOptionsPage();
} else {
const apiKey = result.apiKey;
const endpoint = "https://api.openai.com/v1/chat/completions";
const now = new Date();
const model = "gpt-3.5-turbo";
const prompt = `${info.selectionText}`
const max_tokens = 256;
const messages = [
{
role: "system",
content: `You are part of a chrome extension that creates google calendar events. Users select some text on a website and send it to you. You must then create a google calendar event URL based on this text. Return only a google calendar URL and nothing else.le calendar URLs from text. In all your responses, return only the google calendar URL. Note that the current date is ${now}.`
},
{
role: "user",
content: prompt
}
];
console.log(messages);
fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({
model: model,
messages: messages, // corrected line
max_tokens: max_tokens
})
})
.then(response => response.json())
.then(data => {
const calendarURL = data.choices[0].message.content;
chrome.scripting.insertCSS({
target: { tabId: tab.id },
css: 'body { cursor: default; }'
});
chrome.tabs.create({
url: calendarURL
});
});
}
});
}
});