-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindicator.js
More file actions
364 lines (305 loc) · 11.5 KB
/
Copy pathindicator.js
File metadata and controls
364 lines (305 loc) · 11.5 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import GObject from 'gi://GObject';
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import St from 'gi://St';
import Soup from 'gi://Soup';
import Clutter from 'gi://Clutter';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import {AgentSection} from './ui/agentSection.js';
import {getUsageClass} from './ui/usageClass.js';
import {pickPanelUtilization} from './ui/panelUtils.js';
import {ClaudeProvider} from './providers/claude.js';
import {CodexProvider} from './providers/codex.js';
import {GeminiProvider} from './providers/gemini.js';
import {clampPercent} from './providers/parsers.js';
const PROVIDER_REGISTRY = {
'claude': ClaudeProvider,
'codex': CodexProvider,
'gemini': GeminiProvider,
};
const PANEL_BAR_MAX_PX = 50;
export const AgentsUsageIndicator = GObject.registerClass(
class AgentsUsageIndicator extends PanelMenu.Button {
_init(extensionPath, settings, openPrefsCallback) {
super._init(0.5, 'Agents Usage Indicator', false);
this._extensionPath = extensionPath;
this._settings = settings;
this._openPrefsCallback = openPrefsCallback;
this._timerId = null;
this._inFlight = false;
this._destroyed = false;
this._session = new Soup.Session({timeout: 30});
// Array of { provider, section, windows }
this._providers = [];
this._lastUpdated = null;
this._buildPanel();
this._buildMenu();
this._initProviders();
this._settingsChangedId = this._settings.connect('changed', (_s, key) => {
this._onSettingChanged(key);
});
this._startTimer();
this._refreshAll();
}
_buildPanel() {
this._panelBox = new St.BoxLayout({
style_class: 'panel-status-menu-box',
});
this.add_child(this._panelBox);
this._panelItems = new Map();
// Generic items for when no providers are configured
this._genericBox = new St.BoxLayout({y_align: Clutter.ActorAlign.CENTER});
this._panelBox.add_child(this._genericBox);
this._panelIcon = new St.Icon({
gicon: Gio.FileIcon.new(
Gio.File.new_for_path(`${this._extensionPath}/icons/agents-symbolic.svg`)
),
style_class: 'system-status-icon agent-icon',
});
this._genericBox.add_child(this._panelIcon);
this._panelLabel = new St.Label({
text: '—',
style_class: 'agent-usage-label',
y_align: Clutter.ActorAlign.CENTER,
});
this._genericBox.add_child(this._panelLabel);
this._updatePanel();
}
_buildMenu() {
this._sectionsBox = new PopupMenu.PopupMenuSection();
this.menu.addMenuItem(this._sectionsBox);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
const footerItem = new PopupMenu.PopupBaseMenuItem({
reactive: false,
can_focus: false,
});
const footerBox = new St.BoxLayout({
style_class: 'agent-footer-box',
x_expand: true,
});
footerItem.add_child(footerBox);
this._lastUpdatedLabel = new St.Label({
text: 'Not updated yet',
style_class: 'agent-last-updated-label',
x_expand: true,
y_align: Clutter.ActorAlign.CENTER,
});
footerBox.add_child(this._lastUpdatedLabel);
const refreshButton = new St.Button({
label: '↻ Refresh',
style_class: 'agent-refresh-button',
});
refreshButton.connect('clicked', () => this._refreshAll());
footerBox.add_child(refreshButton);
const settingsButton = new St.Button({
label: '⚙ Settings',
style_class: 'agent-refresh-button',
});
settingsButton.connect('clicked', () => {
this.menu.close();
this._openPrefsCallback?.();
});
footerBox.add_child(settingsButton);
this.menu.addMenuItem(footerItem);
}
_initProviders() {
// Destroy existing providers — their cancellables fire, so in-flight
// fetch callbacks never reach the _refreshAll handler below.
for (const entry of this._providers)
entry.provider.destroy();
this._providers = [];
this._sectionsBox.removeAll();
const enabledIds = this._settings.get_strv('enabled-providers');
// Remove panel items for providers that are no longer enabled.
for (const [id, items] of this._panelItems.entries()) {
if (!enabledIds.includes(id)) {
items.box.destroy();
this._panelItems.delete(id);
}
}
for (const id of enabledIds) {
const ProviderClass = PROVIDER_REGISTRY[id];
if (!ProviderClass)
continue;
const provider = new ProviderClass(this._settings, this._session);
const iconPath = `${this._extensionPath}/icons/${provider.iconName}`;
const section = new AgentSection(provider.displayName, iconPath);
if (this._providers.length > 0)
this._sectionsBox.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._sectionsBox.addMenuItem(section);
this._providers.push({provider, section, windows: null});
}
}
_startTimer() {
this._stopTimer();
const interval = this._settings.get_int('refresh-interval');
this._timerId = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
interval,
() => {
this._refreshAll();
return GLib.SOURCE_CONTINUE;
},
);
}
_stopTimer() {
if (this._timerId) {
GLib.source_remove(this._timerId);
this._timerId = null;
}
}
_refreshAll() {
if (this._destroyed) return;
if (this._inFlight) return;
if (this._providers.length === 0) {
this._updatePanel();
return;
}
this._inFlight = true;
let pending = this._providers.length;
const done = () => {
pending--;
if (pending === 0) {
this._inFlight = false;
if (!this._destroyed) this._onAllRefreshComplete();
}
};
for (const entry of this._providers) {
if (!entry.provider.isConfigured()) {
entry.windows = null;
entry.section.update(null, null);
done();
continue;
}
entry.provider.fetchUsage((error, windows) => {
if (this._destroyed) {
done();
return;
}
entry.windows = windows;
entry.section.update(windows, error);
done();
});
}
}
_onAllRefreshComplete() {
this._lastUpdated = new Date();
const timeStr = this._lastUpdated.toLocaleTimeString(undefined, {
hour: '2-digit',
minute: '2-digit',
});
this._lastUpdatedLabel.text = `Updated ${timeStr}`;
this._updatePanel();
}
_ensurePanelItem(entry) {
let items = this._panelItems.get(entry.provider.id);
if (items) return items;
const box = new St.BoxLayout({y_align: Clutter.ActorAlign.CENTER});
const icon = new St.Icon({
gicon: Gio.FileIcon.new(
Gio.File.new_for_path(`${this._extensionPath}/icons/${entry.provider.iconName}`)
),
style_class: 'system-status-icon agent-icon',
});
box.add_child(icon);
const label = new St.Label({
text: '—',
style_class: 'agent-usage-label',
y_align: Clutter.ActorAlign.CENTER,
});
box.add_child(label);
const progressBg = new St.Widget({
style_class: 'agent-panel-progress-bg',
y_align: Clutter.ActorAlign.CENTER,
});
const progressBar = new St.Widget({
style_class: 'agent-panel-progress-bar',
});
progressBg.add_child(progressBar);
box.add_child(progressBg);
this._panelBox.add_child(box);
items = {box, icon, progressBg, progressBar, label};
this._panelItems.set(entry.provider.id, items);
return items;
}
_renderPanelItem(items, entry, mode, showIcon, isFirst) {
items.box.visible = true;
if (isFirst)
items.box.remove_style_class_name('agent-panel-item-spaced');
else
items.box.add_style_class_name('agent-panel-item-spaced');
items.icon.visible = showIcon;
items.progressBg.visible = (mode === 'bar' || mode === 'both');
items.label.visible = (mode === 'text' || mode === 'both');
const utilization = pickPanelUtilization(entry.windows);
if (utilization == null) {
items.label.text = '—';
items.progressBar.width = 0;
items.progressBar.style_class = 'agent-panel-progress-bar';
return;
}
const pct = clampPercent(utilization);
items.label.text = `${Math.round(pct)}%`;
items.progressBar.width = Math.round(pct * PANEL_BAR_MAX_PX / 100);
items.progressBar.style_class = `agent-panel-progress-bar ${getUsageClass(pct)}`;
}
_updatePanel() {
const mode = this._settings.get_string('display-mode');
const showIcon = this._settings.get_boolean('show-icon');
const activeProviders = this._providers.filter(e => e.provider.isConfigured());
if (activeProviders.length === 0) {
this._genericBox.visible = true;
this._panelIcon.visible = showIcon;
this._panelLabel.visible = (mode === 'text' || mode === 'both');
for (const item of this._panelItems.values())
item.box.visible = false;
return;
}
this._genericBox.visible = false;
activeProviders.forEach((entry, i) => {
const items = this._ensurePanelItem(entry);
this._renderPanelItem(items, entry, mode, showIcon, i === 0);
});
// Hide boxes for providers that are configured-off (still in registry but no creds).
const activeIds = new Set(activeProviders.map(e => e.provider.id));
for (const [id, items] of this._panelItems.entries()) {
if (!activeIds.has(id))
items.box.visible = false;
}
}
_onSettingChanged(key) {
switch (key) {
case 'refresh-interval':
this._startTimer();
break;
case 'enabled-providers':
this._initProviders();
this._refreshAll();
break;
case 'display-mode':
case 'show-icon':
this._updatePanel();
break;
case 'claude-credentials-path':
case 'codex-credentials-path':
case 'gemini-credentials-path':
this._refreshAll();
break;
}
}
destroy() {
this._destroyed = true;
this._stopTimer();
if (this._settingsChangedId) {
this._settings.disconnect(this._settingsChangedId);
this._settingsChangedId = null;
}
for (const entry of this._providers)
entry.provider.destroy();
this._providers = [];
this._session?.abort();
this._session = null;
super.destroy();
}
});