Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
128 lines (106 loc) · 4.57 KB
/
Copy pathpopup.js
File metadata and controls
128 lines (106 loc) · 4.57 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
import { InferenceClient } from "@huggingface/inference";
let originalPrompt = '';
async function callPromptOptimiser(promptToOptimise, apiKey) {
const client = new InferenceClient(apiKey);
try {
const response = await client.chatCompletion({
model: "Qwen/Qwen2.5-7B-Instruct",
messages: [
{
role: "system",
content: "You are an assistant that rewrites prompts to be as short as possible without changing their meaning. Remove politeness and unnecessary words.",
},
{
role: "user",
content: `Rewrite the following prompt to be as short as possible without changing its meaning. Remove politeness and unnecessary words. Return ONLY the rewritten prompt, with NO extra words, explanations, or quotes.\nInput prompt: """${promptToOptimise}"""`,
},
],
max_tokens: 60,
temperature: 0.1,
});
const rewrittenText = response.choices[0].message.content.trim();
return {
rewrittenText: rewrittenText,
inputTokens: promptToOptimise.split(/\s+/).length,
outputTokens: rewrittenText.split(/\s+/).length,
};
} catch (error) {
console.error("Error calling prompt optimiser:", error);
throw error;
}
}
document.addEventListener('DOMContentLoaded', () => {
const optionsButton = document.getElementById('options-btn');
const promptInputElement = document.getElementById('prompt-input');
const optimiseButton = document.getElementById('optimise-btn');
const revertButton = document.getElementById('revert-btn');
const helpButton = document.getElementById('help-btn');
const statusDiv = document.getElementById('status');
const inputTokensSpan = document.getElementById('input-tokens');
const outputTokensSpan = document.getElementById('output-tokens');
revertButton.classList.add('hidden');
if (optionsButton) {
optionsButton.addEventListener('click', () => {
chrome.runtime.openOptionsPage();
});
}
if (helpButton) {
helpButton.addEventListener('click', () => {
window.open('https://github.com/LaurencePy/GreenPromptV2/issues', '_blank');
});
}
optimiseButton.addEventListener('click', async () => {
const originalButtonText = 'Optimise & Copy';
originalPrompt = promptInputElement.value.trim();
if (!originalPrompt) {
statusDiv.textContent = 'Please enter a prompt.';
return;
}
statusDiv.textContent = '';
optimiseButton.disabled = true;
optimiseButton.textContent = 'Optimising...';
revertButton.classList.add('hidden');
chrome.storage.sync.get('hfApiKey', async (data) => {
const apiKey = data.hfApiKey;
if (!apiKey) {
statusDiv.textContent = 'Hugging Face API Key not set. Please set it in the settings.';
optimiseButton.disabled = false;
optimiseButton.textContent = originalButtonText;
return;
}
try {
// Call the updated function name
const result = await callPromptOptimiser(originalPrompt, apiKey);
if (result && result.rewrittenText) {
promptInputElement.value = result.rewrittenText;
await navigator.clipboard.writeText(result.rewrittenText);
inputTokensSpan.textContent = result.inputTokens;
outputTokensSpan.textContent = result.outputTokens;
localStorage.setItem('lastPrompt', result.rewrittenText);
optimiseButton.textContent = 'Optimised & Copied!';
revertButton.classList.remove('hidden');
} else {
throw new Error("The API did not return a valid response.");
}
} catch (err) {
console.error("An error occurred during optimisation:", err);
statusDiv.textContent = `Error: ${err.message}`;
optimiseButton.textContent = 'Error!';
} finally {
setTimeout(() => {
optimiseButton.disabled = false;
optimiseButton.textContent = originalButtonText;
}, 2000);
}
});
});
revertButton.addEventListener('click', () => {
if (originalPrompt) {
promptInputElement.value = originalPrompt;
}
revertButton.classList.add('hidden');
});
promptInputElement.addEventListener('input', () => {
revertButton.classList.add('hidden');
});
});