-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
192 lines (165 loc) · 5.67 KB
/
background.js
File metadata and controls
192 lines (165 loc) · 5.67 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
/**
* imoji Enterprise Background System v20.0
* Architecture: Service Worker with LRU Caching and Circuit Breaker Pattern
* @author R H A Ashan Imalka
*/
// --- Configuration Constants ---
const CONFIG = {
API_ENDPOINT: "https://imoji-server-production.up.railway.app/get-ai-response",
TIMEOUT_MS: 25000,
CACHE_TTL_MS: 1000 * 60 * 60, // 1 Hour Cache
MAX_CACHE_SIZE: 100,
CIRCUIT_BREAKER: {
FAILURE_THRESHOLD: 3,
RESET_TIMEOUT_MS: 30000 // 30 seconds cooldown after failures
}
};
// --- State Management ---
const STATE = {
consecutiveFailures: 0,
circuitOpenTime: 0,
cache: new Map() // Map<string, {timestamp: number, data: string}>
};
// --- Lifecycle Hooks ---
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
initializeDefaults();
}
setupContextMenu();
});
function initializeDefaults() {
const defaultSchema = {
emojis: {
':lol:': '😂', ':heart:': '❤️', ':thumbsup:': '👍',
':rocket:': '🚀', ':fire:': '🔥', ':check:': '✅',
':warning:': '⚠️', ':bug:': '🐛'
},
stats: {
savedSeconds: 0,
aiQueries: 0,
emojisExpanded: 0
},
isEnabled: true,
aiModel: 'deepseek/deepseek-chat:free'
};
chrome.storage.sync.set(defaultSchema);
}
function setupContextMenu() {
chrome.contextMenus.removeAll(() => {
chrome.contextMenus.create({
id: "imoji-enterprise-context",
title: "Ask imoji Intelligence about '%s'",
contexts: ["selection"]
});
});
}
// --- Context Menu Handler ---
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "imoji-enterprise-context" && info.selectionText) {
safeSendMessage(tab.id, {
action: "trigger_ai_overlay",
payload: info.selectionText
});
}
});
// --- Networking Core (The Enterprise Part) ---
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "execute_ai_query") {
executeSmartQuery(request.prompt, request.model)
.then(response => sendResponse(response))
.catch(err => sendResponse({ success: false, error: err.message }));
return true; // Keep channel open for async
}
if (request.action === "telemetry_event") {
updateTelemetry(request.metric);
}
});
async function executeSmartQuery(prompt, model) {
const cacheKey = `query:${model}:${prompt.trim().toLowerCase()}`;
// 1. Circuit Breaker Check
if (isCircuitOpen()) {
return {
success: false,
error: "Service temporarily unavailable due to high failure rate. Retrying in 30s."
};
}
// 2. Cache Check (High Performance)
const cached = getCache(cacheKey);
if (cached) {
console.log("[imoji Core] Cache Hit - Zero Latency");
return { success: true, text: cached, source: 'edge_cache' };
}
// 3. Network Request
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), CONFIG.TIMEOUT_MS);
const response = await fetch(CONFIG.API_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt, model }),
signal: controller.signal
});
clearTimeout(timeout);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
const content = data.choices?.[0]?.message?.content;
if (!content) throw new Error("Malformed AI Response");
// Success: Reset Circuit Breaker & Cache Result
STATE.consecutiveFailures = 0;
setCache(cacheKey, content);
// Telemetry: Increment query count
updateTelemetry('aiQueries');
return { success: true, text: content, source: 'network' };
} catch (error) {
STATE.consecutiveFailures++;
if (STATE.consecutiveFailures >= CONFIG.CIRCUIT_BREAKER.FAILURE_THRESHOLD) {
STATE.circuitOpenTime = Date.now();
console.warn("[imoji Core] Circuit Breaker Tripped ⚠️");
}
const isTimeout = error.name === 'AbortError';
return {
success: false,
error: isTimeout ? "Gateway Timeout (30s). Server warming up." : error.message
};
}
}
// --- Helpers ---
function isCircuitOpen() {
if (STATE.consecutiveFailures < CONFIG.CIRCUIT_BREAKER.FAILURE_THRESHOLD) return false;
const cooldownElapsed = (Date.now() - STATE.circuitOpenTime) > CONFIG.CIRCUIT_BREAKER.RESET_TIMEOUT_MS;
if (cooldownElapsed) {
STATE.consecutiveFailures = 0; // Reset
return false;
}
return true;
}
function getCache(key) {
const entry = STATE.cache.get(key);
if (!entry) return null;
if (Date.now() - entry.timestamp > CONFIG.CACHE_TTL_MS) {
STATE.cache.delete(key);
return null;
}
return entry.data;
}
function setCache(key, data) {
if (STATE.cache.size >= CONFIG.MAX_CACHE_SIZE) {
const firstKey = STATE.cache.keys().next().value;
STATE.cache.delete(firstKey);
}
STATE.cache.set(key, { timestamp: Date.now(), data });
}
function updateTelemetry(metric) {
chrome.storage.sync.get('stats', (data) => {
const stats = data.stats || { savedSeconds: 0, aiQueries: 0, emojisExpanded: 0 };
stats[metric] = (stats[metric] || 0) + 1;
if (metric === 'emojisExpanded') stats.savedSeconds += 2; // Assume 2s saved per emoji
if (metric === 'aiQueries') stats.savedSeconds += 30; // Assume 30s saved per AI query
chrome.storage.sync.set({ stats });
});
}
function safeSendMessage(tabId, message) {
chrome.tabs.sendMessage(tabId, message).catch(() => {
// Tab might be a protected page or closed, ignore safely
});
}