-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
255 lines (220 loc) · 8.32 KB
/
background.js
File metadata and controls
255 lines (220 loc) · 8.32 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// background.js (service worker)
// ----- Helpers to load settings -----
function getSettings() {
return new Promise((resolve) => {
chrome.storage.local.get(["provider", "apiKey", "model"], (data) => {
resolve({
provider: data.provider || "openai",
apiKey: data.apiKey || "",
model:
data.model ||
(data.provider === "gemini" ? "gemini-1.5-flash" : "gpt-3.5-turbo"),
});
});
});
}
// ----- Prompt builder -----
function buildPrompt(postText, style, tone) {
const baseInstruction = `
You are an assistant that writes high-quality LinkedIn comments.
Requirements:
- Language: natural, professional, friendly.
- Must be 1 short paragraph (2–4 sentences) for short, 3–6 sentences for long.
- Emoji+ style should include relevant emojis but not look spammy.
- DO NOT sound like generic AI; sound human, specific to the post.
- The user will review before posting, so it's okay to be slightly opinionated but stay respectful.
Post content:
"""${postText || ""}"""
`;
// Add tone-specific instructions
let toneInstruction = "";
switch (tone) {
case "default":
toneInstruction = "Generate a comment based on the post content using simple, easy-to-understand words. For short comments, use 5-10 words. For long comments, use 20-30 words.";
break;
case "professional":
toneInstruction = "Use a professional tone with industry-appropriate terminology.";
break;
case "casual":
toneInstruction = "Use a casual, conversational tone as if talking to a colleague.";
break;
case "supportive":
toneInstruction = "Use an encouraging, supportive tone that validates the original poster.";
break;
case "thoughtful":
toneInstruction = "Use a thoughtful tone that adds insight or asks meaningful questions.";
break;
case "enthusiastic":
toneInstruction = "Use an enthusiastic tone with positive energy and excitement.";
break;
default:
toneInstruction = "Use a balanced, neutral tone.";
};
// Style-specific instructions
const styleInstruction =
style === "short"
? "Write a very short comment (5-10 words only). Be concise and to the point."
: style === "long"
? "Write a longer comment (20-30 words) that adds value or perspective."
: "Write a short comment that includes 2–3 relevant emojis naturally.";
return `${baseInstruction}
Tone: ${toneInstruction}
Style: ${styleInstruction}
Return ONLY the comment text, no explanations.`;
}
// ----- OpenAI call (chat completions) -----
async function callOpenAI(apiKey, model, prompt) {
if (!apiKey) throw new Error("Missing OpenAI API key");
try {
const res = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
model,
messages: [
{
role: "system",
content: "You write concise, natural LinkedIn comments.",
},
{ role: "user", content: prompt },
],
temperature: 0.8,
max_tokens: 80,
}),
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
const errorMessage = errorData.error?.message || `HTTP ${res.status}: ${res.statusText}`;
// Provide more user-friendly error messages
if (res.status === 401) {
throw new Error("Invalid OpenAI API key. Please check your API key in the extension options.");
} else if (res.status === 429) {
throw new Error("OpenAI API rate limit exceeded. Please try again later.");
} else if (res.status === 404) {
throw new Error(`Model "${model}" not found. Please check your model name in the extension options.`);
} else {
throw new Error(`OpenAI API error: ${errorMessage}`);
}
}
const data = await res.json();
const comment = data.choices?.[0]?.message?.content?.trim() || "";
if (!comment) {
throw new Error("OpenAI returned an empty response. Please try again.");
}
return comment;
} catch (error) {
console.error("OpenAI API call failed:", error);
// Re-throw with a more user-friendly message if it's not already user-friendly
if (error.message.startsWith("OpenAI") ||
error.message.includes("API key") ||
error.message.includes("rate limit") ||
error.message.includes("Model") ||
error.message.includes("empty response")) {
throw error;
} else {
throw new Error("Failed to connect to OpenAI. Please check your internet connection and try again.");
}
}
}
// ----- Gemini call (REST generateContent) -----
async function callGemini(apiKey, model, prompt) {
if (!apiKey) throw new Error("Missing Gemini API key");
try {
const url = `https://generativelanguage.googleapis.com/v1beta/models/${encodeURIComponent(
model
)}:generateContent`;
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-goog-api-key": apiKey,
},
body: JSON.stringify({
contents: [
{
parts: [{ text: prompt }],
},
],
}),
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
const errorMessage = errorData.error?.message || `HTTP ${res.status}: ${res.statusText}`;
// Provide more user-friendly error messages
if (res.status === 401 || res.status === 403) {
throw new Error("Invalid Gemini API key. Please check your API key in the extension options.");
} else if (res.status === 429) {
throw new Error("Gemini API rate limit exceeded. Please try again later.");
} else if (res.status === 404) {
throw new Error(`Model "${model}" not found. Please check your model name in the extension options.`);
} else {
throw new Error(`Gemini API error: ${errorMessage}`);
}
}
const data = await res.json();
// Check for safety filters or other issues
if (data.promptFeedback?.blockReason) {
throw new Error(`Content blocked by Gemini safety filters: ${data.promptFeedback.blockReason}`);
}
const candidate = data.candidates?.[0];
if (!candidate) {
throw new Error("Gemini returned no candidates. Please try again.");
}
// Check for content safety issues in the response
if (candidate.finishReason === "SAFETY") {
throw new Error("Gemini blocked the generated comment due to safety concerns. Please try with a different post.");
}
const parts = candidate?.content?.parts || [];
const text = parts
.map((p) => p.text || "")
.join(" ")
.trim();
if (!text) {
throw new Error("Gemini returned an empty response. Please try again.");
}
return text;
} catch (error) {
console.error("Gemini API call failed:", error);
// Re-throw with a more user-friendly message if it's not already user-friendly
if (error.message.startsWith("Gemini") ||
error.message.includes("API key") ||
error.message.includes("rate limit") ||
error.message.includes("Model") ||
error.message.includes("empty response") ||
error.message.includes("blocked") ||
error.message.includes("safety")) {
throw error;
} else {
throw new Error("Failed to connect to Gemini. Please check your internet connection and try again.");
}
}
}
// ----- Main entry for content script -----
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message?.type === "generateComment") {
(async () => {
try {
const { provider, apiKey, model } = await getSettings();
if (!apiKey) {
throw new Error("No API key set in extension options.");
}
const prompt = buildPrompt(message.postText, message.style, message.tone);
let commentText;
if (provider === "gemini") {
commentText = await callGemini(apiKey, model, prompt);
} else {
commentText = await callOpenAI(apiKey, model, prompt);
}
sendResponse({ ok: true, text: commentText });
} catch (err) {
console.error(err);
sendResponse({ ok: false, error: String(err) });
}
})();
// Tell Chrome we'll respond async
return true;
}
});