@harars/opencode-switch-openai-auth-plugin 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +50 -21
- package/dist/tui.js +50 -21
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -39,6 +39,7 @@ import { insert as _$insert } from "@opentui/solid";
|
|
|
39
39
|
import { setProp as _$setProp } from "@opentui/solid";
|
|
40
40
|
import { createElement as _$createElement } from "@opentui/solid";
|
|
41
41
|
import { TextAttributes } from "@opentui/core";
|
|
42
|
+
import { onMount } from "solid-js";
|
|
42
43
|
import { useKeyboard } from "@opentui/solid";
|
|
43
44
|
|
|
44
45
|
// src/login-helpers.ts
|
|
@@ -74,12 +75,23 @@ async function clip(text) {
|
|
|
74
75
|
function target(authz) {
|
|
75
76
|
return authz.instructions.match(/[A-Z0-9]{4}-[A-Z0-9]{4,5}/)?.[0] ?? authz.url;
|
|
76
77
|
}
|
|
78
|
+
function detail(err) {
|
|
79
|
+
if (err instanceof Error)
|
|
80
|
+
return err.message;
|
|
81
|
+
if (typeof err === "string")
|
|
82
|
+
return err;
|
|
83
|
+
try {
|
|
84
|
+
return JSON.stringify(err);
|
|
85
|
+
} catch {
|
|
86
|
+
return String(err);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
77
89
|
async function runOAuthCallback(callback, input) {
|
|
78
90
|
try {
|
|
79
91
|
const res = await callback(input);
|
|
80
|
-
return
|
|
81
|
-
} catch {
|
|
82
|
-
return false;
|
|
92
|
+
return res.error ? { ok: false, error: res.error } : { ok: true };
|
|
93
|
+
} catch (err) {
|
|
94
|
+
return { ok: false, error: detail(err) };
|
|
83
95
|
}
|
|
84
96
|
}
|
|
85
97
|
|
|
@@ -428,6 +440,8 @@ function visible(prompt, values) {
|
|
|
428
440
|
if (!prompt.when)
|
|
429
441
|
return true;
|
|
430
442
|
const cur = values[prompt.when.key];
|
|
443
|
+
if (cur === undefined)
|
|
444
|
+
return false;
|
|
431
445
|
if (prompt.when.op === "eq")
|
|
432
446
|
return cur === prompt.when.value;
|
|
433
447
|
return cur !== prompt.when.value;
|
|
@@ -446,12 +460,14 @@ function unwrap(input) {
|
|
|
446
460
|
}
|
|
447
461
|
if ("error" in input && input.error !== undefined) {
|
|
448
462
|
return {
|
|
449
|
-
ok: false
|
|
463
|
+
ok: false,
|
|
464
|
+
error: input.error
|
|
450
465
|
};
|
|
451
466
|
}
|
|
452
467
|
if ("data" in input) {
|
|
453
468
|
return input.data === undefined ? {
|
|
454
|
-
ok: false
|
|
469
|
+
ok: false,
|
|
470
|
+
error: "Missing response data"
|
|
455
471
|
} : {
|
|
456
472
|
ok: true,
|
|
457
473
|
data: input.data
|
|
@@ -482,6 +498,9 @@ function bind(api, authz) {
|
|
|
482
498
|
}
|
|
483
499
|
function WaitView(props) {
|
|
484
500
|
bind(props.api, props.authz);
|
|
501
|
+
onMount(() => {
|
|
502
|
+
props.run();
|
|
503
|
+
});
|
|
485
504
|
return (() => {
|
|
486
505
|
var _el$ = _$createElement("box"), _el$2 = _$createElement("text"), _el$3 = _$createElement("text"), _el$4 = _$createElement("text"), _el$5 = _$createElement("text"), _el$7 = _$createElement("text");
|
|
487
506
|
_$insertNode(_el$, _el$2);
|
|
@@ -532,9 +551,9 @@ function wait(api, title, authz, run) {
|
|
|
532
551
|
api.ui.dialog.replace(() => _$createComponent(WaitView, {
|
|
533
552
|
api,
|
|
534
553
|
title,
|
|
535
|
-
authz
|
|
554
|
+
authz,
|
|
555
|
+
run
|
|
536
556
|
}));
|
|
537
|
-
run();
|
|
538
557
|
}
|
|
539
558
|
async function choose(api, methods) {
|
|
540
559
|
if (methods.length === 1)
|
|
@@ -660,19 +679,25 @@ async function code(api, index, method, authz) {
|
|
|
660
679
|
},
|
|
661
680
|
authz,
|
|
662
681
|
onConfirm: async (value) => {
|
|
663
|
-
resolve(await runOAuthCallback(api.client.provider.oauth.callback, {
|
|
682
|
+
resolve(await runOAuthCallback((input) => api.client.provider.oauth.callback(input), {
|
|
664
683
|
providerID: "openai",
|
|
665
684
|
method: index,
|
|
666
685
|
code: value
|
|
667
686
|
}));
|
|
668
687
|
},
|
|
669
|
-
onCancel: () => resolve(
|
|
670
|
-
|
|
688
|
+
onCancel: () => resolve({
|
|
689
|
+
ok: false,
|
|
690
|
+
error: "Cancelled"
|
|
691
|
+
})
|
|
692
|
+
}), () => resolve({
|
|
693
|
+
ok: false,
|
|
694
|
+
error: "Cancelled"
|
|
695
|
+
}));
|
|
671
696
|
});
|
|
672
|
-
if (!ok) {
|
|
697
|
+
if (!ok.ok) {
|
|
673
698
|
api.ui.toast({
|
|
674
699
|
variant: "error",
|
|
675
|
-
message:
|
|
700
|
+
message: `Login failed: ${detail(ok.error)}`
|
|
676
701
|
});
|
|
677
702
|
return false;
|
|
678
703
|
}
|
|
@@ -682,16 +707,16 @@ async function auto(api, index, method, authz) {
|
|
|
682
707
|
const prev = await readCurrentAuth();
|
|
683
708
|
const ok = await new Promise((resolve) => {
|
|
684
709
|
wait(api, method.label, authz, async () => {
|
|
685
|
-
resolve(await runOAuthCallback(api.client.provider.oauth.callback, {
|
|
710
|
+
resolve(await runOAuthCallback((input) => api.client.provider.oauth.callback(input), {
|
|
686
711
|
providerID: "openai",
|
|
687
712
|
method: index
|
|
688
713
|
}));
|
|
689
714
|
});
|
|
690
715
|
});
|
|
691
|
-
if (!ok) {
|
|
716
|
+
if (!ok.ok) {
|
|
692
717
|
api.ui.toast({
|
|
693
718
|
variant: "error",
|
|
694
|
-
message:
|
|
719
|
+
message: `Login failed: ${detail(ok.error)}`
|
|
695
720
|
});
|
|
696
721
|
return false;
|
|
697
722
|
}
|
|
@@ -723,9 +748,13 @@ async function loginOpenAI(api) {
|
|
|
723
748
|
return false;
|
|
724
749
|
const picked = methods[index];
|
|
725
750
|
const method = picked.method;
|
|
726
|
-
|
|
727
|
-
if (method.prompts?.length
|
|
728
|
-
|
|
751
|
+
let inputs;
|
|
752
|
+
if (method.prompts?.length) {
|
|
753
|
+
const value = await prompts(api, method.label, method);
|
|
754
|
+
if (!value)
|
|
755
|
+
return false;
|
|
756
|
+
inputs = value;
|
|
757
|
+
}
|
|
729
758
|
const authz = unwrap(await api.client.provider.oauth.authorize({
|
|
730
759
|
providerID: "openai",
|
|
731
760
|
method: picked.index,
|
|
@@ -734,7 +763,7 @@ async function loginOpenAI(api) {
|
|
|
734
763
|
if (!authz.ok) {
|
|
735
764
|
api.ui.toast({
|
|
736
765
|
variant: "error",
|
|
737
|
-
message:
|
|
766
|
+
message: `Login failed: ${detail(authz.error)}`
|
|
738
767
|
});
|
|
739
768
|
return false;
|
|
740
769
|
}
|
|
@@ -747,10 +776,10 @@ async function loginOpenAI(api) {
|
|
|
747
776
|
message: "Unsupported auth method"
|
|
748
777
|
});
|
|
749
778
|
return false;
|
|
750
|
-
} catch {
|
|
779
|
+
} catch (err) {
|
|
751
780
|
api.ui.toast({
|
|
752
781
|
variant: "error",
|
|
753
|
-
message:
|
|
782
|
+
message: `Login failed: ${detail(err)}`
|
|
754
783
|
});
|
|
755
784
|
return false;
|
|
756
785
|
}
|
package/dist/tui.js
CHANGED
|
@@ -39,6 +39,7 @@ import { insert as _$insert } from "@opentui/solid";
|
|
|
39
39
|
import { setProp as _$setProp } from "@opentui/solid";
|
|
40
40
|
import { createElement as _$createElement } from "@opentui/solid";
|
|
41
41
|
import { TextAttributes } from "@opentui/core";
|
|
42
|
+
import { onMount } from "solid-js";
|
|
42
43
|
import { useKeyboard } from "@opentui/solid";
|
|
43
44
|
|
|
44
45
|
// src/login-helpers.ts
|
|
@@ -74,12 +75,23 @@ async function clip(text) {
|
|
|
74
75
|
function target(authz) {
|
|
75
76
|
return authz.instructions.match(/[A-Z0-9]{4}-[A-Z0-9]{4,5}/)?.[0] ?? authz.url;
|
|
76
77
|
}
|
|
78
|
+
function detail(err) {
|
|
79
|
+
if (err instanceof Error)
|
|
80
|
+
return err.message;
|
|
81
|
+
if (typeof err === "string")
|
|
82
|
+
return err;
|
|
83
|
+
try {
|
|
84
|
+
return JSON.stringify(err);
|
|
85
|
+
} catch {
|
|
86
|
+
return String(err);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
77
89
|
async function runOAuthCallback(callback, input) {
|
|
78
90
|
try {
|
|
79
91
|
const res = await callback(input);
|
|
80
|
-
return
|
|
81
|
-
} catch {
|
|
82
|
-
return false;
|
|
92
|
+
return res.error ? { ok: false, error: res.error } : { ok: true };
|
|
93
|
+
} catch (err) {
|
|
94
|
+
return { ok: false, error: detail(err) };
|
|
83
95
|
}
|
|
84
96
|
}
|
|
85
97
|
|
|
@@ -428,6 +440,8 @@ function visible(prompt, values) {
|
|
|
428
440
|
if (!prompt.when)
|
|
429
441
|
return true;
|
|
430
442
|
const cur = values[prompt.when.key];
|
|
443
|
+
if (cur === undefined)
|
|
444
|
+
return false;
|
|
431
445
|
if (prompt.when.op === "eq")
|
|
432
446
|
return cur === prompt.when.value;
|
|
433
447
|
return cur !== prompt.when.value;
|
|
@@ -446,12 +460,14 @@ function unwrap(input) {
|
|
|
446
460
|
}
|
|
447
461
|
if ("error" in input && input.error !== undefined) {
|
|
448
462
|
return {
|
|
449
|
-
ok: false
|
|
463
|
+
ok: false,
|
|
464
|
+
error: input.error
|
|
450
465
|
};
|
|
451
466
|
}
|
|
452
467
|
if ("data" in input) {
|
|
453
468
|
return input.data === undefined ? {
|
|
454
|
-
ok: false
|
|
469
|
+
ok: false,
|
|
470
|
+
error: "Missing response data"
|
|
455
471
|
} : {
|
|
456
472
|
ok: true,
|
|
457
473
|
data: input.data
|
|
@@ -482,6 +498,9 @@ function bind(api, authz) {
|
|
|
482
498
|
}
|
|
483
499
|
function WaitView(props) {
|
|
484
500
|
bind(props.api, props.authz);
|
|
501
|
+
onMount(() => {
|
|
502
|
+
props.run();
|
|
503
|
+
});
|
|
485
504
|
return (() => {
|
|
486
505
|
var _el$ = _$createElement("box"), _el$2 = _$createElement("text"), _el$3 = _$createElement("text"), _el$4 = _$createElement("text"), _el$5 = _$createElement("text"), _el$7 = _$createElement("text");
|
|
487
506
|
_$insertNode(_el$, _el$2);
|
|
@@ -532,9 +551,9 @@ function wait(api, title, authz, run) {
|
|
|
532
551
|
api.ui.dialog.replace(() => _$createComponent(WaitView, {
|
|
533
552
|
api,
|
|
534
553
|
title,
|
|
535
|
-
authz
|
|
554
|
+
authz,
|
|
555
|
+
run
|
|
536
556
|
}));
|
|
537
|
-
run();
|
|
538
557
|
}
|
|
539
558
|
async function choose(api, methods) {
|
|
540
559
|
if (methods.length === 1)
|
|
@@ -660,19 +679,25 @@ async function code(api, index, method, authz) {
|
|
|
660
679
|
},
|
|
661
680
|
authz,
|
|
662
681
|
onConfirm: async (value) => {
|
|
663
|
-
resolve(await runOAuthCallback(api.client.provider.oauth.callback, {
|
|
682
|
+
resolve(await runOAuthCallback((input) => api.client.provider.oauth.callback(input), {
|
|
664
683
|
providerID: "openai",
|
|
665
684
|
method: index,
|
|
666
685
|
code: value
|
|
667
686
|
}));
|
|
668
687
|
},
|
|
669
|
-
onCancel: () => resolve(
|
|
670
|
-
|
|
688
|
+
onCancel: () => resolve({
|
|
689
|
+
ok: false,
|
|
690
|
+
error: "Cancelled"
|
|
691
|
+
})
|
|
692
|
+
}), () => resolve({
|
|
693
|
+
ok: false,
|
|
694
|
+
error: "Cancelled"
|
|
695
|
+
}));
|
|
671
696
|
});
|
|
672
|
-
if (!ok) {
|
|
697
|
+
if (!ok.ok) {
|
|
673
698
|
api.ui.toast({
|
|
674
699
|
variant: "error",
|
|
675
|
-
message:
|
|
700
|
+
message: `Login failed: ${detail(ok.error)}`
|
|
676
701
|
});
|
|
677
702
|
return false;
|
|
678
703
|
}
|
|
@@ -682,16 +707,16 @@ async function auto(api, index, method, authz) {
|
|
|
682
707
|
const prev = await readCurrentAuth();
|
|
683
708
|
const ok = await new Promise((resolve) => {
|
|
684
709
|
wait(api, method.label, authz, async () => {
|
|
685
|
-
resolve(await runOAuthCallback(api.client.provider.oauth.callback, {
|
|
710
|
+
resolve(await runOAuthCallback((input) => api.client.provider.oauth.callback(input), {
|
|
686
711
|
providerID: "openai",
|
|
687
712
|
method: index
|
|
688
713
|
}));
|
|
689
714
|
});
|
|
690
715
|
});
|
|
691
|
-
if (!ok) {
|
|
716
|
+
if (!ok.ok) {
|
|
692
717
|
api.ui.toast({
|
|
693
718
|
variant: "error",
|
|
694
|
-
message:
|
|
719
|
+
message: `Login failed: ${detail(ok.error)}`
|
|
695
720
|
});
|
|
696
721
|
return false;
|
|
697
722
|
}
|
|
@@ -723,9 +748,13 @@ async function loginOpenAI(api) {
|
|
|
723
748
|
return false;
|
|
724
749
|
const picked = methods[index];
|
|
725
750
|
const method = picked.method;
|
|
726
|
-
|
|
727
|
-
if (method.prompts?.length
|
|
728
|
-
|
|
751
|
+
let inputs;
|
|
752
|
+
if (method.prompts?.length) {
|
|
753
|
+
const value = await prompts(api, method.label, method);
|
|
754
|
+
if (!value)
|
|
755
|
+
return false;
|
|
756
|
+
inputs = value;
|
|
757
|
+
}
|
|
729
758
|
const authz = unwrap(await api.client.provider.oauth.authorize({
|
|
730
759
|
providerID: "openai",
|
|
731
760
|
method: picked.index,
|
|
@@ -734,7 +763,7 @@ async function loginOpenAI(api) {
|
|
|
734
763
|
if (!authz.ok) {
|
|
735
764
|
api.ui.toast({
|
|
736
765
|
variant: "error",
|
|
737
|
-
message:
|
|
766
|
+
message: `Login failed: ${detail(authz.error)}`
|
|
738
767
|
});
|
|
739
768
|
return false;
|
|
740
769
|
}
|
|
@@ -747,10 +776,10 @@ async function loginOpenAI(api) {
|
|
|
747
776
|
message: "Unsupported auth method"
|
|
748
777
|
});
|
|
749
778
|
return false;
|
|
750
|
-
} catch {
|
|
779
|
+
} catch (err) {
|
|
751
780
|
api.ui.toast({
|
|
752
781
|
variant: "error",
|
|
753
|
-
message:
|
|
782
|
+
message: `Login failed: ${detail(err)}`
|
|
754
783
|
});
|
|
755
784
|
return false;
|
|
756
785
|
}
|