-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
306 lines (265 loc) · 7.6 KB
/
Copy pathbackground.js
File metadata and controls
306 lines (265 loc) · 7.6 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
importScripts('utils.js', 'oauth.js', 'api-client.js');
let pollTimer = null;
const POLL_NORMAL = 15000;
const POLL_FAST = 3000;
let currentPollInterval = POLL_NORMAL;
const sseControllers = new Map();
const lastKnownPhases = new Map();
let fastPollTimeout = null;
function broadcast(message) {
try {
chrome.runtime.sendMessage(message);
} catch (_) {}
}
let polling = false;
let cachedSessionsLocal = [];
async function pollSessions() {
if (polling) return;
polling = true;
try {
await _pollSessionsInner();
} finally {
polling = false;
}
}
async function _pollSessionsInner() {
if (!(await isAuthenticated())) return;
const { projectName } = await getConfig();
if (!projectName) return;
let data;
try {
data = await api.sessions.list({ size: 100 });
} catch (err) {
if (err.status === 401 || /auth|unauthorized|expired/i.test(err.message)) {
broadcast({ type: 'AUTH_EXPIRED' });
stopPolling();
return;
}
console.warn('pollSessions failed:', err);
return;
}
const sessions = data.items || [];
cachedSessionsLocal = sessions;
const changed = JSON.stringify(sessions) !== JSON.stringify(
(await chrome.storage.local.get('cachedSessions')).cachedSessions || []
);
await chrome.storage.local.set({ cachedSessions: sessions });
if (changed) {
broadcast({ type: 'SESSIONS_UPDATED', sessions });
}
for (const s of sessions) {
lastKnownPhases.set(s.id, s.phase);
}
const activeIds = new Set();
for (const s of sessions) {
if (s.phase === 'Running' || s.phase === 'Creating') {
activeIds.add(s.id);
if (!sseControllers.has(s.id)) {
connectSSE(s.id);
}
}
}
for (const id of sseControllers.keys()) {
if (!activeIds.has(id)) {
disconnectSSE(id);
}
}
const needsFast = sessions.some(s =>
s.phase === 'Creating' || s.phase === 'Pending' || s.phase === 'Stopping'
);
const desired = needsFast ? POLL_FAST : POLL_NORMAL;
if (desired !== currentPollInterval) {
currentPollInterval = desired;
restartTimer();
}
}
const sseBackoff = new Map();
async function connectSSE(sessionId) {
if (sseControllers.has(sessionId)) return;
const controller = new AbortController();
sseControllers.set(sessionId, controller);
let response;
try {
response = await api.sessions.streamEvents(sessionId);
sseBackoff.delete(sessionId);
} catch (err) {
sseControllers.delete(sessionId);
if (/auth|expired|unauthorized/i.test(err.message)) {
stopPolling();
return;
}
console.warn(`SSE connect failed for ${sessionId}:`, err);
scheduleSSEReconnect(sessionId);
return;
}
parseSSEStream(
response,
(event) => {
handleSSEEvent(sessionId, event);
broadcast({ type: 'SSE_EVENT', sessionId, event });
},
(err) => {
if (controller.signal.aborted) return;
sseControllers.delete(sessionId);
if (/auth|expired|unauthorized/i.test(String(err))) {
stopPolling();
return;
}
console.warn(`SSE error for ${sessionId}:`, err);
scheduleSSEReconnect(sessionId);
},
controller.signal
);
}
function scheduleSSEReconnect(sessionId) {
const current = sseBackoff.get(sessionId) || 1000;
const delay = Math.min(current, 30000) * (0.5 + Math.random());
sseBackoff.set(sessionId, current * 2);
setTimeout(() => {
if (!sseControllers.has(sessionId)) {
connectSSE(sessionId);
}
}, delay);
}
function disconnectSSE(sessionId) {
const controller = sseControllers.get(sessionId);
if (controller) {
controller.abort();
}
sseControllers.delete(sessionId);
}
async function handleSSEEvent(sessionId, event) {
const type = event.event_type || '';
const match = cachedSessionsLocal.find(s => s.id === sessionId);
const sessionName = match?.name || sessionId;
if (/TOOL_CALL_START/i.test(type)) {
const toolName = event.tool_name || event.name || '';
if (/ask.*user|user.*question|askuserquestion/i.test(toolName)) {
createNotification(sessionId, 'input_needed',
'Input Needed',
`Session "${sessionName}" is waiting for your input`
);
}
}
if (type === 'RUN_FINISHED') {
createNotification(sessionId, 'run_finished',
'Run Finished',
`Session "${sessionName}" completed`
);
pollSessions();
}
if (type === 'RUN_ERROR') {
const msg = event.error || event.message || 'An error occurred';
createNotification(sessionId, 'error',
'Run Error',
`Session "${sessionName}": ${msg}`
);
}
}
async function createNotification(sessionId, kind, title, body) {
const { notifications = [] } = await chrome.storage.local.get('notifications');
notifications.push({
id: Date.now(),
read: false,
ts: new Date().toISOString(),
sessionId,
kind,
title,
body,
});
while (notifications.length > 50) {
notifications.shift();
}
await chrome.storage.local.set({ notifications });
const unread = notifications.filter(n => !n.read).length;
chrome.action.setBadgeText({ text: unread > 0 ? String(unread) : '' });
chrome.action.setBadgeBackgroundColor({ color: '#ef4444' });
broadcast({ type: 'NOTIFICATION_ADDED' });
}
function restartTimer() {
if (pollTimer) clearInterval(pollTimer);
pollTimer = setInterval(pollSessions, currentPollInterval);
}
function startPolling() {
if (pollTimer) clearInterval(pollTimer);
pollSessions();
pollTimer = setInterval(pollSessions, currentPollInterval);
}
function stopPolling() {
if (pollTimer) {
clearInterval(pollTimer);
pollTimer = null;
}
for (const id of sseControllers.keys()) {
disconnectSSE(id);
}
}
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
switch (msg.type) {
case 'GET_NOTIFICATIONS': {
chrome.storage.local.get('notifications').then(({ notifications = [] }) => {
sendResponse({ notifications });
});
return true;
}
case 'MARK_ALL_READ': {
chrome.storage.local.get('notifications').then(async ({ notifications = [] }) => {
for (const n of notifications) n.read = true;
await chrome.storage.local.set({ notifications });
chrome.action.setBadgeText({ text: '' });
sendResponse({ ok: true });
});
return true;
}
case 'REFRESH_SESSIONS': {
pollSessions().then(() => sendResponse({ ok: true }));
return true;
}
case 'SESSION_TRANSITIONING': {
currentPollInterval = POLL_FAST;
restartTimer();
if (fastPollTimeout) clearTimeout(fastPollTimeout);
fastPollTimeout = setTimeout(() => {
currentPollInterval = POLL_NORMAL;
restartTimer();
fastPollTimeout = null;
}, 30000);
sendResponse({ ok: true });
return false;
}
case 'OAUTH_LOGIN': {
oauthLogin(msg.serverUrl, msg.issuerUrl)
.then(() => {
sendResponse({ ok: true });
startPolling();
})
.catch((err) => {
sendResponse({ ok: false, error: err.message || String(err) });
});
return true;
}
case 'OAUTH_LOGOUT': {
oauthLogout()
.then(async () => {
stopPolling();
await chrome.storage.local.remove('cachedSessions');
sendResponse({ ok: true });
})
.catch((err) => {
sendResponse({ ok: false, error: err.message || String(err) });
});
return true;
}
}
});
chrome.action.onClicked.addListener((tab) => {
chrome.sidePanel.open({ windowId: tab.windowId });
});
(async () => {
if (await isAuthenticated()) {
const { projectName } = await getConfig();
if (projectName) {
startPolling();
}
}
})();