-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprefs.js
More file actions
990 lines (894 loc) · 29.9 KB
/
Copy pathprefs.js
File metadata and controls
990 lines (894 loc) · 29.9 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
import Gio from "gi://Gio";
import GioUnix from "gi://GioUnix";
import GLib from "gi://GLib";
import GObject from "gi://GObject";
import Gtk from "gi://Gtk";
import Adw from "gi://Adw";
import {
ExtensionPreferences,
gettext as _,
} from "resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js";
import {
storeToken,
loadToken,
clearToken,
nullTokenSchema,
} from "./secret.js";
/**
* Predefined providers list.
* Lista de proveedores predefinidos.
*/
const PREDEFINED_PROVIDERS = [
{ id: "codex", name: "Codex", useApi: true, defaultCommand: "" },
{ id: "ollama", name: "Ollama Cloud", useApi: true, defaultCommand: "" },
{
id: "claude",
name: "Claude",
useApi: false,
defaultCommand: "codexbar --provider claude --source cli --format json",
},
{
id: "antigravity",
name: "Antigravity",
useApi: false,
defaultCommand:
"codexbar --provider antigravity --source cli --format json",
},
{
id: "gemini",
name: "Gemini CLI",
useApi: false,
defaultCommand: "codexbar --provider gemini --source api --format json",
},
{
id: "deepseek",
name: "DeepSeek",
useApi: false,
defaultCommand: "codexbar --provider deepseek --source api --format json",
},
{
id: "copilot",
name: "Copilot",
useApi: false,
defaultCommand: "codexbar --provider copilot --source api --format json",
},
{
id: "openrouter",
name: "OpenRouter",
useApi: false,
defaultCommand: "codexbar --provider openrouter --source api --format json",
},
{
id: "perplexity",
name: "Perplexity",
useApi: false,
defaultCommand: "codexbar --provider perplexity --source api --format json",
},
{
id: "mistral",
name: "Mistral",
useApi: false,
defaultCommand: "codexbar --provider mistral --source api --format json",
},
];
/**
* Preferences page for CodexBar.
* Página de preferencias para CodexBar.
*/
const CodexBarPrefsPage = GObject.registerClass(
class CodexBarPrefsPage extends Adw.PreferencesPage {
_init(settings, extensionPath = null) {
super._init({
title: _("General"),
icon_name: "dialog-information-symbolic",
});
this._settings = settings;
this._extensionPath = extensionPath;
this._providerRows = [];
this.add(this._buildSettingsGroup());
this.add(this._buildProvidersGroup());
this.add(this._buildDeveloperGroup());
this.add(this._buildContributeGroup());
this.add(this._buildMaintenanceGroup());
this.connect("destroy", () => {
this._settings = null;
this._providerRows = [];
});
}
/**
* From here I can pass a simulated codexbar-cli output and thus test providers that I don't have.
*/
_buildDeveloperGroup() {
const group = new Adw.PreferencesGroup({
title: _("Developer & Testing Options"),
description: _(
"Simulate custom CLI outputs and view debug logs.",
),
});
// 1. Toggle Custom Output
const enabledRow = new Adw.SwitchRow({
title: _("Test with Custom CLI Output"),
subtitle: _(
"Simulate Codexbar-CLI outputs to test providers or other things and see how the extension renders the data",
),
active: this._settings.get_boolean("dev-custom-output-enabled"),
});
this._settings.bind(
"dev-custom-output-enabled",
enabledRow,
"active",
Gio.SettingsBindFlags.DEFAULT,
);
group.add(enabledRow);
// 2. Mock Provider Name
const providerNameRow = new Adw.EntryRow({
title: _("Simulated Provider Name"),
text: this._settings.get_string("dev-custom-output-provider-name") || "Mock Provider",
});
this._settings.bind(
"dev-custom-output-provider-name",
providerNameRow,
"text",
Gio.SettingsBindFlags.DEFAULT,
);
group.add(providerNameRow);
// 3. Mock JSON Input
const jsonRow = new Adw.ActionRow({
title: _("Codexbar-CLI custom output"),
subtitle: _("Paste the raw JSON output to test"),
});
const scrolled = new Gtk.ScrolledWindow({
min_content_height: 120,
hexpand: true,
vexpand: false,
});
scrolled.set_has_frame(true);
const jsonView = new Gtk.TextView({
monospace: true,
wrap_mode: Gtk.WrapMode.WORD,
});
jsonView.get_buffer().set_text(
this._settings.get_string("dev-custom-output-json") || "[]",
-1,
);
jsonView.get_buffer().connect("changed", () => {
const buffer = jsonView.get_buffer();
const start = buffer.get_start_iter();
const end = buffer.get_end_iter();
const text = buffer.get_text(start, end, true).trim();
this._settings.set_string("dev-custom-output-json", text);
});
scrolled.set_child(jsonView);
const jsonBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 6,
margin_top: 8,
margin_bottom: 8,
margin_start: 8,
margin_end: 8,
});
jsonBox.append(scrolled);
jsonRow.add_suffix(jsonBox);
group.add(jsonRow);
return group;
}
/**
* Build the contribute and contact group.
* Construye el grupo de contribución y contacto.
*/
_buildContributeGroup() {
const group = new Adw.PreferencesGroup({
title: _("Contribute & Contact"),
description: _(
"Enjoy the community of Codexbar contributors and other Inled projects. We are waiting for you!",
),
});
// Pull Request / Contribute
const prRow = new Adw.ActionRow({
title: _("Add a New Provider"),
subtitle: _(
"Help the community by submitting a Pull Request on GitHub",
),
});
prRow.add_prefix(new Gtk.Image({ icon_name: "window-new-symbolic" }));
const prBtn = new Gtk.Button({
icon_name: "go-next-symbolic",
valign: Gtk.Align.CENTER,
css_classes: ["flat"],
});
prBtn.connect("clicked", () => {
Gio.AppInfo.launch_default_for_uri(
"https://github.com/InledGroup/codexbar-gnome",
null,
);
});
prRow.add_suffix(prBtn);
group.add(prRow);
// Review on GNOME Extensions
const reviewRow = new Adw.ActionRow({
title: _("Leave a Review"),
subtitle: _(
"Rate the extension on EGO or share on social media.",
),
});
reviewRow.add_prefix(new Gtk.Image({ icon_name: "starred-symbolic" }));
const reviewBtn = new Gtk.Button({
icon_name: "go-next-symbolic",
valign: Gtk.Align.CENTER,
css_classes: ["flat"],
});
reviewBtn.connect("clicked", () => {
Gio.AppInfo.launch_default_for_uri(
"https://extensions.gnome.org/extension/9841/codexbar/",
null,
);
});
reviewRow.add_suffix(reviewBtn);
group.add(reviewRow);
// Contact Inled
const contactRow = new Adw.ActionRow({
title: _("Contact Inled"),
subtitle: _("Questions? Suggestions? Write to us at [email protected]"),
});
contactRow.add_prefix(
new Gtk.Image({ icon_name: "mail-message-new-symbolic" }),
);
const contactBtn = new Gtk.Button({
icon_name: "go-next-symbolic",
valign: Gtk.Align.CENTER,
css_classes: ["flat"],
});
contactBtn.connect("clicked", () => {
Gio.AppInfo.launch_default_for_uri("mailto:[email protected]", null);
});
contactRow.add_suffix(contactBtn);
group.add(contactRow);
// Visit Website
const webRow = new Adw.ActionRow({
title: _("Visit inled.es"),
subtitle: _(
"Discover more tools and projects that might interest you!",
),
});
webRow.add_prefix(new Gtk.Image({ icon_name: "web-browser-symbolic" }));
const webBtn = new Gtk.Button({
icon_name: "go-next-symbolic",
valign: Gtk.Align.CENTER,
css_classes: ["suggested-action"],
});
webBtn.connect("clicked", () => {
Gio.AppInfo.launch_default_for_uri("https://inled.es", null);
});
webRow.add_suffix(webBtn);
group.add(webRow);
// Social Media
const socialRow = new Adw.ActionRow({
title: _("Join the Community"),
subtitle: _("¡We have new Discord and Matrix channels!"),
});
socialRow.add_prefix(
new Gtk.Image({ icon_name: "system-users-symbolic" }),
);
const socialBox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 12,
valign: Gtk.Align.CENTER,
});
const createSocialBtn = (name, url, tooltip) => {
const iconPath = GLib.build_filenamev([
this._extensionPath,
"media",
"logos",
`${name}-symbolic.svg`,
]);
const btn = new Gtk.Button({
css_classes: ["flat"],
tooltip_text: tooltip,
});
if (GLib.file_test(iconPath, GLib.FileTest.EXISTS)) {
const gicon = Gio.Icon.new_for_string(iconPath);
btn.set_child(new Gtk.Image({ gicon: gicon }));
} else {
btn.set_label(tooltip);
}
btn.connect("clicked", () => {
Gio.AppInfo.launch_default_for_uri(url, null);
});
return btn;
};
socialBox.append(
createSocialBtn(
"discord",
"https://discord.com/invite/PSeTkDMnr",
"Discord",
),
);
socialBox.append(
createSocialBtn(
"matrix",
"https://matrix.to/#/!POXBFohLnmrKwRBxTi:matrix.org?via=matrix.org",
"Matrix",
),
);
socialBox.append(
createSocialBtn(
"mastodon",
"https://mastodon.social/@inled",
"Mastodon",
),
);
socialBox.append(
createSocialBtn(
"youtube",
"https://www.youtube.com/@inledgroup",
"YouTube",
),
);
socialBox.append(
createSocialBtn("x", "https://x.com/inledgroup", "X (Twitter)"),
);
socialRow.add_suffix(socialBox);
group.add(socialRow);
return group;
}
/**
* Build the general settings group (Refresh interval, Display mode).
* Construye el grupo de ajustes generales (Intervalo de refresco, Modo de visualización).
*/
_buildSettingsGroup() {
const group = new Adw.PreferencesGroup({
title: _("Settings"),
});
const refreshRow = new Adw.SpinRow({
title: _("Refresh Interval (minutes)"),
adjustment: new Gtk.Adjustment({
lower: 1,
upper: 1440,
step_increment: 1,
value: this._settings.get_int("refresh-interval"),
}),
});
this._settings.bind(
"refresh-interval",
refreshRow.adjustment,
"value",
Gio.SettingsBindFlags.DEFAULT,
);
group.add(refreshRow);
const displayModeRow = new Adw.ComboRow({
title: _("Display Mode"),
model: new Gtk.StringList({
strings: [_("Used"), _("Remaining")],
}),
selected: this._settings.get_string("display-mode") === "used" ? 0 : 1,
});
displayModeRow.connect("notify::selected", () => {
this._settings.set_string(
"display-mode",
displayModeRow.selected === 0 ? "used" : "remaining",
);
});
group.add(displayModeRow);
const showLogosRow = new Adw.SwitchRow({
title: _("Show Provider Logos"),
subtitle: _(
"Display the logo of each AI provider in the selection tabs",
),
active: this._settings.get_boolean("show-logos"),
});
this._settings.bind(
"show-logos",
showLogosRow,
"active",
Gio.SettingsBindFlags.DEFAULT,
);
group.add(showLogosRow);
const showPacingInfoRow = new Adw.SwitchRow({
title: _("Show Usage Pacing"),
subtitle: _(
"Display weekly pacing estimates (reserve or over pace) for large windows",
),
active: this._settings.get_boolean("show-pacing-info"),
});
this._settings.bind(
"show-pacing-info",
showPacingInfoRow,
"active",
Gio.SettingsBindFlags.DEFAULT,
);
group.add(showPacingInfoRow);
return group;
}
/**
* Build the AI providers configuration group.
* Construye el grupo de configuración de proveedores de IA.
*/
_buildProvidersGroup() {
const group = new Adw.PreferencesGroup({
title: _("AI Providers"),
description: _(
"Enable providers. Codex uses Direct API, Antigravity needs the plugin and others use codexbar-cli with --source api.",
),
});
const activeProvidersJson = this._settings.get_string("providers");
let activeProviders = [];
try {
activeProviders = JSON.parse(activeProvidersJson);
} catch (e) {
activeProviders = [];
}
const saveProviders = async () => {
const newProviders = [];
for (const row of this._providerRows) {
if (row._enabledSwitch.active) {
newProviders.push({
id: row._id,
name: row._name,
command: row._commandEntry.get_text(),
useApi: row._useApi,
});
if (row._useApi) {
const token = row._tokenEntry.get_text();
if (token) {
await storeToken(row._id, token);
}
}
}
}
this._settings.set_string("providers", JSON.stringify(newProviders));
};
const createProviderRow = (info, activeData = null) => {
const isEnabled = activeData !== null;
const isPredefined = PREDEFINED_PROVIDERS.some((p) => p.id === info.id);
// Handle command defaults and migrations
// Manejar valores por defecto y migraciones de comandos
let command = info.defaultCommand || "";
if (activeData) {
if (
activeData.command &&
(activeData.command.includes("--provider") ||
info.useApi ||
!isPredefined)
) {
command = activeData.command;
} else {
command = info.defaultCommand;
}
}
const row = new Adw.ExpanderRow({
title: info.name,
subtitle: isEnabled ? _("Enabled") : _("Disabled"),
expanded: isEnabled,
});
row._id = activeData ? activeData.id : info.id;
row._name = info.name;
row._useApi = info.useApi;
const enabledSwitch = new Gtk.Switch({
active: isEnabled,
valign: Gtk.Align.CENTER,
});
enabledSwitch.connect("notify::active", () => {
row.set_subtitle(enabledSwitch.active ? _("Enabled") : _("Disabled"));
row.expanded = enabledSwitch.active;
saveProviders();
});
row.add_suffix(enabledSwitch);
row._enabledSwitch = enabledSwitch;
const box = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 6,
margin_top: 12,
margin_bottom: 12,
margin_start: 12,
margin_end: 12,
});
if (info.useApi) {
// For Direct API providers like Codex
// Para proveedores de API directa como Codex
const tokenEntry = new Gtk.PasswordEntry({
placeholder_text: _(
"Authentication Cookie (starts with __Secure...)",
),
});
loadToken(row._id).then((token) => {
if (token) tokenEntry.set_text(token);
}).catch(() => {});
tokenEntry.connect("changed", async () => {
await storeToken(row._id, tokenEntry.get_text().trim());
saveProviders();
});
row._tokenEntry = tokenEntry;
const importBtn = new Gtk.Button({
label: _("Auto-Login from Browser"),
margin_top: 6,
tooltip_text: _(
"Uses a local Python script to extract cookies from Chrome/Brave for this provider.",
),
});
const spinner = new Gtk.Spinner({
valign: Gtk.Align.CENTER,
margin_start: 6,
});
const btnBox = new Gtk.Box({ spacing: 6, margin_top: 6 });
btnBox.append(importBtn);
btnBox.append(spinner);
importBtn.connect("clicked", () => {
importBtn.sensitive = false;
spinner.start();
this._importFromBrowser(row._id, tokenEntry, () => {
importBtn.sensitive = true;
spinner.stop();
});
});
box.append(
new Gtk.Label({
label: _("Session Cookies:"),
xalign: 0,
css_classes: ["caption"],
}),
);
box.append(tokenEntry);
box.append(btnBox);
box.append(
new Gtk.Label({
label: _("Manual entry: Paste your session cookies here."),
xalign: 0,
css_classes: ["dim-label"],
}),
);
row._commandEntry = new Gtk.Entry({ text: "" }); // Dummy
} else {
const commandEntry = new Gtk.Entry({
placeholder_text: _("CLI Command (e.g. codexbar --provider ...)"),
text: command,
});
commandEntry.connect("changed", saveProviders);
row._commandEntry = commandEntry;
const labelBox = new Gtk.Box({ spacing: 6 });
labelBox.append(
new Gtk.Label({ label: _("CLI Command:"), xalign: 0 }),
);
if (isPredefined) {
const resetBtn = new Gtk.Button({
label: _("Reset Default"),
valign: Gtk.Align.CENTER,
css_classes: ["flat"],
});
resetBtn.connect("clicked", () => {
commandEntry.set_text(info.defaultCommand);
saveProviders();
});
labelBox.append(resetBtn);
}
box.append(labelBox);
box.append(commandEntry);
if (info.id === "antigravity") {
const hasLsof = !!GLib.find_program_in_path("lsof");
if (!hasLsof) {
let lsofCmd = "sudo apt install lsof";
if (GLib.find_program_in_path("pacman")) {
lsofCmd = "sudo pacman -S lsof";
} else if (GLib.find_program_in_path("dnf")) {
lsofCmd = "sudo dnf install lsof";
}
const lsofLabel = new Gtk.Label({
label: _(
`⚠️ Antigravity requires the 'lsof' utility to detect the local server ports. Run this command to install it:\n\n${lsofCmd}`,
),
xalign: 0,
use_markup: false,
wrap: true,
css_classes: ["dim-label"],
});
lsofLabel.set_margin_top(6);
box.append(lsofLabel);
}
// Antigravity SSL certificate setup instruction
const certLabel = new Gtk.Label({
label: _(
"💡 Antigravity requires a trusted local SSL certificate to work. So we have made a python script to help you do this in a single command:\n\npip install codexbar-ssl-helper && codexbar-ssl-helper",
),
xalign: 0,
use_markup: false,
wrap: true,
css_classes: ["dim-label"],
});
certLabel.set_margin_top(12);
box.append(certLabel);
}
}
if (!isPredefined) {
const deleteBtn = new Gtk.Button({
label: _("Remove Provider"),
margin_top: 12,
css_classes: ["destructive-action"],
});
deleteBtn.connect("clicked", () => {
group.remove(row);
this._providerRows = this._providerRows.filter((r) => r !== row);
saveProviders();
});
box.append(deleteBtn);
}
row.add_row(box);
this._providerRows.push(row);
return row;
};
const processedIds = new Set();
const processedNames = new Set();
// 1. Predefined Providers
PREDEFINED_PROVIDERS.forEach((info) => {
const infoNameLower = info.name.toLowerCase();
const activeData =
activeProviders.find((p) => p.id === info.id) ||
activeProviders.find((p) => p.name.toLowerCase() === infoNameLower);
if (activeData) {
processedIds.add(activeData.id);
processedNames.add(activeData.name.toLowerCase());
}
group.add(createProviderRow(info, activeData));
});
// 2. Custom Providers
activeProviders.forEach((p) => {
const pNameLower = p.name.toLowerCase();
if (!processedIds.has(p.id) && !processedNames.has(pNameLower)) {
group.add(
createProviderRow(
{
id: p.id,
name: p.name,
useApi: p.useApi || false,
defaultCommand: p.command,
},
p,
),
);
}
});
// 3. Add Custom Provider Button
const addBtnRow = new Adw.ActionRow({
title: _("Add Custom Provider"),
subtitle: _("The extension should parse it correctly."),
});
const addBtn = new Gtk.Button({
icon_name: "list-add-symbolic",
valign: Gtk.Align.CENTER,
css_classes: ["suggested-action"],
});
addBtn.connect("clicked", () => {
const dialog = new Adw.MessageDialog({
heading: _("New Provider"),
body: _("Enter the details for your custom AI provider."),
transient_for: this.get_root(),
modal: true,
});
const content = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 12,
});
const nameEntry = new Gtk.Entry({
placeholder_text: _("Provider Name (e.g. MyLocalModel)"),
});
content.append(nameEntry);
const cmdEntry = new Gtk.Entry({
placeholder_text: _("Command (e.g. codexbar --provider ...)"),
});
content.append(cmdEntry);
dialog.set_extra_child(content);
dialog.add_response("cancel", _("Cancel"));
dialog.add_response("add", _("Add"));
dialog.set_response_appearance("add", Adw.ResponseAppearance.SUGGESTED);
dialog.connect("response", (d, response) => {
if (response === "add") {
const name = nameEntry.get_text().trim();
const cmd = cmdEntry.get_text().trim();
if (name && cmd) {
const newProvider = {
id: `custom-${Date.now()}`,
name: name,
command: cmd,
useApi: false,
};
const row = createProviderRow(
{
id: newProvider.id,
name: newProvider.name,
useApi: false,
defaultCommand: newProvider.command,
},
newProvider,
);
// Add before the "Add Custom Provider" button
// Añadir antes del botón "Añadir Proveedor Personalizado"
group.add(row);
// Manually move it up if needed, but Adw.PreferencesGroup appends
// In GNOME 45+ we can't easily reorder children in PreferencesGroup
// without removing the button and re-adding it.
saveProviders();
}
}
});
dialog.present();
});
addBtnRow.add_suffix(addBtn);
group.add(addBtnRow);
return group;
}
/**
* Build the maintenance/debug group.
* Construye el grupo de mantenimiento/depuración.
*/
_buildMaintenanceGroup() {
const group = new Adw.PreferencesGroup({
title: _("Maintenance"),
});
const setupBtnRow = new Adw.ActionRow({
title: _("Show Welcome Screen"),
subtitle: _("Reset first-run state"),
});
const setupBtn = new Gtk.Button({
icon_name: "help-about-symbolic",
valign: Gtk.Align.CENTER,
});
setupBtn.connect("clicked", () => {
this._settings.set_boolean("first-run", true);
});
setupBtnRow.add_suffix(setupBtn);
group.add(setupBtnRow);
return group;
}
/**
* Import cookies from browser using local Python script.
* Importa cookies desde el navegador usando un script de Python local.
*/
/**
* Import cookies from browser using local Python script.
* Importa cookies desde el navegador usando un script de Python local.
*/
async _importFromBrowser(providerId, tokenEntry, callback = null) {
const finish = () => {
if (callback) callback();
};
const showError = (title, message) => {
const dialog = new Adw.MessageDialog({
heading: title,
body: message,
transient_for: this.get_root(),
modal: true,
});
dialog.add_response("ok", _("OK"));
dialog.present();
finish();
};
const showMissingDialog = () => {
const dialog = new Adw.MessageDialog({
heading: _("Cookie Importer Missing"),
body: _(
"To import cookies automatically from your browser, please install the python dependency by running:\n\npip install codexbar-cookie-importer",
),
transient_for: this.get_root(),
modal: true,
});
dialog.add_response("ok", _("OK"));
dialog.present();
finish();
};
// Find the executable binary or fallback to python module
// Encontrar el binario ejecutable o recurrir al módulo python
let argv = null;
if (GLib.find_program_in_path("codexbar-cookie-importer")) {
argv = ["codexbar-cookie-importer"];
} else {
const localBin = GLib.build_filenamev([
GLib.get_home_dir(),
".local",
"bin",
"codexbar-cookie-importer",
]);
if (GLib.file_test(localBin, GLib.FileTest.EXISTS)) {
argv = [localBin];
} else {
// Try executing as module / Intentar ejecutar como módulo
argv = ["/usr/bin/python3", "-m", "codexbar_cookie_importer"];
}
}
if (providerId === "ollama") {
argv.push("--provider", "ollama");
}
try {
const proc = new Gio.Subprocess({
argv: argv,
flags:
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE,
});
proc.init(null);
const cancellable = new Gio.Cancellable();
let timedOut = false;
const timeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 15000, () => {
timedOut = true;
cancellable.cancel();
proc.force_exit();
showError(
_("Timeout"),
_(
"The cookie importer is taking too long. Please ensure your browser is closed and try again.",
),
);
return GLib.SOURCE_REMOVE;
});
proc.communicate_utf8_async(null, cancellable, async (p, res) => {
if (timeoutId > 0 && !timedOut) {
GLib.source_remove(timeoutId);
}
try {
const [success, stdout, stderr] = p.communicate_utf8_finish(res);
if (timedOut) return;
// If the module is not found, python returns exit code 1 or 2 and No module named in stderr
// Si el módulo no se encuentra, python devuelve código de salida 1 o 2 y No module named en stderr
if (
!success &&
stderr &&
(stderr.includes("No module named") ||
stderr.includes("No such file"))
) {
showMissingDialog();
return;
}
if (success && stdout) {
const result = JSON.parse(stdout.trim());
if (result.error === "DEPENDENCIES_MISSING") {
showMissingDialog();
} else if (result.error) {
showError(_("Import Failed"), result.details || result.error);
} else if (result.cookie_header) {
tokenEntry.set_text(result.cookie_header);
await storeToken(providerId, result.cookie_header);
const toast = new Adw.Toast({
title: _("Cookies imported successfully!"),
});
this.get_root().add_toast(toast);
finish();
}
} else {
const err = stderr || _("The importer returned no data.");
if (err.includes("No module named")) {
showMissingDialog();
} else {
showError(_("Error"), err);
}
}
} catch (e) {
if (!timedOut) {
// Check if it's a module not found issue
if (e.message && e.message.includes("No module named")) {
showMissingDialog();
} else {
console.error(e, "CodexBar: Error reading importer output");
showError(_("Unexpected Error"), e.message);
}
}
}
});
} catch (e) {
console.error(e, "CodexBar: Error in _importFromBrowser");
showMissingDialog();
}
}
}
);
/**
* Main preferences entry point.
* Punto de entrada principal para las preferencias.
*/
export default class CodexBarPreferences extends ExtensionPreferences {
fillPreferencesWindow(window) {
const settings = this.getSettings();
const page = new CodexBarPrefsPage(settings, this.path);
window.add(page);
// Null out token schema when preferences window is closed
// Anular el esquema del token cuando se cierre la ventana de preferencias
window.connect("destroy", () => {
nullTokenSchema();
});
}
}