-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
background__.js
113 lines (101 loc) · 2.94 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
// Initialize context menu items
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "wAI",
title: "wAI",
contexts: ["selection"],
});
});
// Handle context menu clicks
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "wAI") {
sendMessageToTab(tab.id, { action: "showMenu", text: info.selectionText });
}
});
// Listen for commands
chrome.commands.onCommand.addListener((command) => {
if (command === "show-overlay") {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
sendMessageToTab(tabs[0].id, { action: "showMenu" });
}
});
}
});
// Function to send messages to tabs with error handling
function sendMessageToTab(tabId, message) {
chrome.tabs.sendMessage(tabId, message, (response) => {
if (chrome.runtime.lastError) {
console.error("Error sending message:", chrome.runtime.lastError.message);
chrome.scripting.executeScript(
{
target: { tabId: tabId },
files: ["content.js"],
},
() => {
chrome.tabs.sendMessage(tabId, message);
}
);
}
});
}
// Function to query AI
async function queryAI(prompt) {
const { apiKey, endpoint, provider, selectedModel } = await chrome.storage.local.get([
"apiKey",
"endpoint",
"provider",
"selectedModel",
]);
if (!apiKey || !endpoint) {
throw new Error("API Key or Endpoint not set. Please complete the onboarding process.");
}
let headers = {
"Content-Type": "application/json",
};
let body = {};
if (provider === "openai") {
headers["Authorization"] = `Bearer ${apiKey}`;
body = {
model: selectedModel || "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
};
} else if (provider === "anthropic") {
headers["x-api-key"] = apiKey;
body = {
model: selectedModel || "claude-3-sonnet-20240229",
messages: [{ role: "user", content: prompt }],
};
} else {
// Custom provider
headers["Authorization"] = `Bearer ${apiKey}`;
body = {
model: selectedModel,
prompt: prompt,
};
}
const response = await fetch(endpoint, {
method: "POST",
headers: headers,
body: JSON.stringify(body),
});
if (!response.ok) {
throw new Error(`API request failed: ${response.statusText}`);
}
const data = await response.json();
return provider === "openai" ? data.choices[0].message.content : data.content[0].text;
}
// Listen for messages from content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "queryAI") {
queryAI(request.prompt)
.then((result) => {
sendResponse({ result: result });
})
.catch((error) => {
console.error("Error querying AI:", error);
sendResponse({ error: error.message });
});
return true; // Indicates we will send a response asynchronously
}
});