opencode-azure-devops-pr-review 0.4.2 → 0.4.3

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/src/tui.js CHANGED
@@ -9,7 +9,7 @@ import { insert as _$insert } from "@opentui/solid";
9
9
  import { memo as _$memo } from "@opentui/solid";
10
10
  import { setProp as _$setProp } from "@opentui/solid";
11
11
  import { createElement as _$createElement } from "@opentui/solid";
12
- // AZPR's part in OpenCode's terminal UI: a panel beside the conversation.
12
+ // AZPR's part in OpenCode's terminal UI: a panel beside the conversation, and the dialogs of /pr-setup.
13
13
  //
14
14
  // Its upper part lists the sessions of the run under way or last ended, each a row that opens
15
15
  // that session when it is clicked; its lower part shows the newest report or comment preview as
@@ -22,11 +22,16 @@ import { createElement as _$createElement } from "@opentui/solid";
22
22
  // no prompt, runs no command of the server part and reads no settings file: the server part writes
23
23
  // that setting into the metadata of its notices.
24
24
  //
25
+ // The dialogs of /pr-setup ask for AZPR's settings (src/tui-setup.mjs says what and in which order) and
26
+ // hand every answer to the server part over the plugin's RPC. They read and write no file and make no
27
+ // request to Azure DevOps; a token is typed into a field that shows nothing of it, and is kept nowhere.
28
+ //
25
29
  // This is the source. OpenCode loads src/tui.js, which scripts/build-tui.mjs compiles from it:
26
30
  // OpenCode 2.3.0 compiles no JSX of a package it installed, so the package carries the result.
27
31
  import { For, Show, createEffect, createMemo, createSignal, onCleanup, onMount } from "solid-js";
28
32
  import { MouseButton, SyntaxStyle, TextAttributes } from "@opentui/core";
29
33
  import { PANEL, buildPanel, conversationOf, createReader, opensPanel } from "./tui-model.mjs";
34
+ import { SETUP_RPC, createSetup, isSetupNotice } from "./tui-setup.mjs";
30
35
 
31
36
  // Short enough for one line of a toast, which the host wraps at about fifty characters.
32
37
  const READY = "AZPR: report or preview ready. /pr-panel shows it.";
@@ -532,185 +537,453 @@ function Panel(props) {
532
537
  return _el$6;
533
538
  })();
534
539
  }
535
- export default {
536
- id: "azpr.tui",
537
- setup(context) {
538
- // A host without these parts of the plugin API has no place for the panel: stay out of its way.
539
- const needed = [context?.ui?.slot, context?.ui?.panel?.open, context?.ui?.router?.navigate, context?.keymap?.layer, context?.data?.listen, context?.data?.session?.pending?.list];
540
- if (needed.some(part => typeof part !== "function")) return;
541
- const data = context.data;
542
- // Set when the TUI unloads the plugin: nothing is asked of the host after that.
543
- let disposed = false;
544
- // The host keeps the messages and the inbox of the session on screen and of open tabs, and drops those of
545
- // other sessions again. What the panel needs of another session (the report waiting in a review session's
546
- // inbox, the notices of the conversation when one of AZPR's own sessions is on screen) is read from the
547
- // server by the reader in tui-model.mjs, which also knows which sessions are gone. `version` makes what it
548
- // holds reactive.
549
- const [version, setVersion] = createSignal(0);
550
- const reader = createReader({
551
- inbox: sessionID => context.client.session.inbox.list({
552
- sessionID
553
- }),
554
- context: typeof context.client?.session?.context === "function" ? sessionID => context.client.session.context({
555
- sessionID
556
- }) : undefined,
557
- lookup: sessionID => data.session.sync(sessionID),
558
- present: sessionID => data.session.get(sessionID),
559
- list: cursor => context.client.session.list(cursor ? {
560
- cursor
561
- } : {}),
562
- changed: () => setVersion(count => count + 1)
563
- });
564
- const source = {
565
- session: id => data.session.get(id),
566
- sessions: () => data.session.list(),
567
- status: id => data.session.status(id),
568
- messages: id => data.session.message.list(id),
569
- pending: id => (version(), [...data.session.pending.list(id), ...reader.entries(id)])
570
- };
571
- const known = () => (version(), reader.known());
572
- const read = () => (version(), reader.read());
573
540
 
574
- // Sessions whose panel this plugin opened and the user has not closed. The host closes a panel when the
575
- // screen leaves its session; coming back to such a session opens it again.
576
- const wanted = new Set();
577
- // Why the panel is being opened without the user asking: "notice" or "return"; undefined when asked.
578
- let unasked;
579
- // The session in which another plugin's panel is open; the host shows one panel at a time.
580
- let occupied;
581
- const state = {
582
- source,
583
- known,
584
- read,
585
- titles: new Map(),
586
- lookup: reader.lookup,
587
- readInbox: reader.readInbox,
588
- dismiss: sessionID => void wanted.delete(sessionID),
589
- takeUnasked: () => {
590
- const value = unasked;
591
- unasked = undefined;
592
- return value;
593
- }
594
- };
595
- context.ui.slot({
596
- append: "session.panel",
597
- render: panel => {
598
- // Every contribution is told which panel is open: remember where it is someone else's.
599
- createEffect(() => {
600
- occupied = panel.name === PANEL ? undefined : panel.sessionID;
601
- });
602
- onCleanup(() => {
603
- occupied = undefined;
604
- });
605
- return _$createComponent(Show, {
606
- get when() {
607
- return panel.name === PANEL;
608
- },
609
- get children() {
610
- return _$createComponent(Panel, {
611
- context: context,
612
- panel: panel,
613
- state: state
614
- });
615
- }
616
- });
541
+ /** The review panel: opens by itself or with /pr-panel, and only reads. Returns its cleanup, or nothing on a host without a place for it. */
542
+ function panelPart(context) {
543
+ // A host without these parts of the plugin API has no place for the panel: stay out of its way.
544
+ const needed = [context?.ui?.slot, context?.ui?.panel?.open, context?.ui?.router?.navigate, context?.keymap?.layer, context?.data?.listen, context?.data?.session?.pending?.list];
545
+ if (needed.some(part => typeof part !== "function")) return undefined;
546
+ const data = context.data;
547
+ // Set when the TUI unloads the plugin: nothing is asked of the host after that.
548
+ let disposed = false;
549
+ // The host keeps the messages and the inbox of the session on screen and of open tabs, and drops those of
550
+ // other sessions again. What the panel needs of another session (the report waiting in a review session's
551
+ // inbox, the notices of the conversation when one of AZPR's own sessions is on screen) is read from the
552
+ // server by the reader in tui-model.mjs, which also knows which sessions are gone. `version` makes what it
553
+ // holds reactive.
554
+ const [version, setVersion] = createSignal(0);
555
+ const reader = createReader({
556
+ inbox: sessionID => context.client.session.inbox.list({
557
+ sessionID
558
+ }),
559
+ context: typeof context.client?.session?.context === "function" ? sessionID => context.client.session.context({
560
+ sessionID
561
+ }) : undefined,
562
+ lookup: sessionID => data.session.sync(sessionID),
563
+ present: sessionID => data.session.get(sessionID),
564
+ list: cursor => context.client.session.list(cursor ? {
565
+ cursor
566
+ } : {}),
567
+ changed: () => setVersion(count => count + 1)
568
+ });
569
+ const source = {
570
+ session: id => data.session.get(id),
571
+ sessions: () => data.session.list(),
572
+ status: id => data.session.status(id),
573
+ messages: id => data.session.message.list(id),
574
+ pending: id => (version(), [...data.session.pending.list(id), ...reader.entries(id)])
575
+ };
576
+ const known = () => (version(), reader.known());
577
+ const read = () => (version(), reader.read());
578
+
579
+ // Sessions whose panel this plugin opened and the user has not closed. The host closes a panel when the
580
+ // screen leaves its session; coming back to such a session opens it again.
581
+ const wanted = new Set();
582
+ // Why the panel is being opened without the user asking: "notice" or "return"; undefined when asked.
583
+ let unasked;
584
+ // The session in which another plugin's panel is open; the host shows one panel at a time.
585
+ let occupied;
586
+ const state = {
587
+ source,
588
+ known,
589
+ read,
590
+ titles: new Map(),
591
+ lookup: reader.lookup,
592
+ readInbox: reader.readInbox,
593
+ dismiss: sessionID => void wanted.delete(sessionID),
594
+ takeUnasked: () => {
595
+ const value = unasked;
596
+ unasked = undefined;
597
+ return value;
598
+ }
599
+ };
600
+ context.ui.slot({
601
+ append: "session.panel",
602
+ render: panel => {
603
+ // Every contribution is told which panel is open: remember where it is someone else's.
604
+ createEffect(() => {
605
+ occupied = panel.name === PANEL ? undefined : panel.sessionID;
606
+ });
607
+ onCleanup(() => {
608
+ occupied = undefined;
609
+ });
610
+ return _$createComponent(Show, {
611
+ get when() {
612
+ return panel.name === PANEL;
613
+ },
614
+ get children() {
615
+ return _$createComponent(Panel, {
616
+ context: context,
617
+ panel: panel,
618
+ state: state
619
+ });
620
+ }
621
+ });
622
+ }
623
+ });
624
+ const shown = sessionID => {
625
+ const current = context.ui.panel.current();
626
+ return current?.name === PANEL && current.sessionID === sessionID;
627
+ };
628
+ /**
629
+ * Show the panel in the session on screen. `byItself` says why when the user did not ask ("notice": one
630
+ * arrived; "return": the screen came back to a session that had the panel): then neither a dialog nor
631
+ * another plugin's panel is disturbed.
632
+ */
633
+ const show = byItself => {
634
+ if (disposed) return false;
635
+ const route = context.ui.router.current();
636
+ if (route.type !== "session") return false;
637
+ const sessionID = route.sessionID;
638
+ // After a TUI restart the host has not loaded a run's sessions: the reader asks the server for them.
639
+ void reader.discover(conversationOf(data.session.get(sessionID)) ?? sessionID);
640
+ wanted.add(sessionID);
641
+ if (shown(sessionID)) return true;
642
+ if (byItself && (occupied === sessionID || context.keymap.mode?.current?.() === "modal")) {
643
+ if (byItself === "notice") context.ui.toast.show({
644
+ message: READY,
645
+ variant: "info"
646
+ });
647
+ return false;
648
+ }
649
+ unasked = byItself;
650
+ return context.ui.panel.open(PANEL);
651
+ };
652
+ const stop = data.listen(event => {
653
+ if (disposed) return;
654
+ const details = event?.details;
655
+ if (details?.type === "session.deleted") return reader.deleted(details.data?.sessionID);
656
+ if (details?.type === "server.connected") {
657
+ // What was published while the TUI was away was missed: look everything up again, and let nothing that
658
+ // was asked before the reconnection answer for now.
659
+ reader.reconnected();
660
+ return;
661
+ }
662
+ if (details?.type !== "session.inbox.enqueued") return;
663
+ const item = details.data?.item;
664
+ // What was read of a session from the server is kept current: the host tells of each new notice.
665
+ reader.arrived(details.data?.sessionID, item);
666
+ // A run that starts, and a report or preview that arrives, opens the panel in the session on screen,
667
+ // unless the notice says that the settings leave the panel to /pr-panel.
668
+ if (item?.type !== "synthetic" || !opensPanel(item.payload?.text, item.payload?.metadata)) return;
669
+ const route = context.ui.router.current();
670
+ if (route.type === "session" && route.sessionID === details.data.sessionID) show("notice");
671
+ });
672
+ context.ui.slot({
673
+ append: "app",
674
+ render: () => {
675
+ createEffect(() => {
676
+ const route = context.ui.router.current();
677
+ if (route.type !== "session" || !wanted.has(route.sessionID)) return;
678
+ const sessionID = route.sessionID;
679
+ // After the session's view has mounted.
680
+ setTimeout(() => {
681
+ const now = context.ui.router.current();
682
+ if (now.type === "session" && now.sessionID === sessionID && wanted.has(sessionID)) show("return");
683
+ }, 0);
684
+ });
685
+ return null;
686
+ }
687
+ });
688
+ context.keymap.layer(() => ({
689
+ mode: "global",
690
+ commands: [{
691
+ id: "azpr.panel",
692
+ title: "AZPR: show or hide the review panel",
693
+ description: "The sessions of the review in this conversation and its report or comment preview, rendered.",
694
+ group: "AZPR",
695
+ palette: true,
696
+ slash: {
697
+ name: "pr-panel"
698
+ },
699
+ run() {
700
+ const route = context.ui.router.current();
701
+ if (route.type !== "session") {
702
+ context.ui.toast.show({
703
+ message: "AZPR: open a conversation first.",
704
+ variant: "info"
705
+ });
706
+ return;
707
+ }
708
+ if (shown(route.sessionID)) {
709
+ wanted.delete(route.sessionID);
710
+ context.ui.panel.close();
711
+ } else show();
617
712
  }
618
- });
619
- const shown = sessionID => {
620
- const current = context.ui.panel.current();
621
- return current?.name === PANEL && current.sessionID === sessionID;
713
+ }]
714
+ }));
715
+ return () => {
716
+ disposed = true;
717
+ reader.dispose();
718
+ stop?.();
719
+ };
720
+ }
721
+
722
+ /**
723
+ * A dialog that takes a secret and shows nothing of it. The host's own text prompt shows what is typed, and
724
+ * its text fields have no hidden mode, so keys and pastes are taken here: the value lives in this closure
725
+ * only and the screen holds bullets and a count, which neither a glance nor the terminal's own copy can read.
726
+ * Resolves with the value ('' only where `optional`), or with undefined when the dialog was closed.
727
+ */
728
+ function askSecret(context, options) {
729
+ return new Promise(resolve => {
730
+ let settled = false;
731
+ const done = value => {
732
+ if (settled) return;
733
+ settled = true;
734
+ resolve(value);
622
735
  };
623
- /**
624
- * Show the panel in the session on screen. `byItself` says why when the user did not ask ("notice": one
625
- * arrived; "return": the screen came back to a session that had the panel): then neither a dialog nor
626
- * another plugin's panel is disturbed.
627
- */
628
- const show = byItself => {
629
- if (disposed) return false;
630
- const route = context.ui.router.current();
631
- if (route.type !== "session") return false;
632
- const sessionID = route.sessionID;
633
- // After a TUI restart the host has not loaded a run's sessions: the reader asks the server for them.
634
- void reader.discover(conversationOf(data.session.get(sessionID)) ?? sessionID);
635
- wanted.add(sessionID);
636
- if (shown(sessionID)) return true;
637
- if (byItself && (occupied === sessionID || context.keymap.mode?.current?.() === "modal")) {
638
- if (byItself === "notice") context.ui.toast.show({
639
- message: READY,
640
- variant: "info"
641
- });
642
- return false;
736
+ context.ui.dialog.show(() => _$createComponent(Secret, {
737
+ context: context,
738
+ options: options,
739
+ done: value => {
740
+ done(value);
741
+ context.ui.dialog.clear();
643
742
  }
644
- unasked = byItself;
645
- return context.ui.panel.open(PANEL);
646
- };
647
- const stop = data.listen(event => {
648
- if (disposed) return;
649
- const details = event?.details;
650
- if (details?.type === "session.deleted") return reader.deleted(details.data?.sessionID);
651
- if (details?.type === "server.connected") {
652
- // What was published while the TUI was away was missed: look everything up again, and let nothing that
653
- // was asked before the reconnection answer for now.
654
- reader.reconnected();
655
- return;
743
+ }), () => done(undefined));
744
+ });
745
+ }
746
+ function Secret(props) {
747
+ let field;
748
+ let value = "";
749
+ const [length, setLength] = createSignal(0);
750
+ const palette = createMemo(() => colors(props.context.theme));
751
+ // Far longer than any token: what is pasted beyond that is not kept.
752
+ const set = next => {
753
+ value = next.slice(0, 2048);
754
+ setLength(value.length);
755
+ };
756
+ const submit = () => {
757
+ if (value || props.options.optional) props.done(value);
758
+ };
759
+ const key = event => {
760
+ const name = event?.name;
761
+ if (name === "return" || name === "enter" || name === "kpenter") {
762
+ event.preventDefault?.();
763
+ return submit();
764
+ }
765
+ if (name === "backspace" || name === "delete") {
766
+ event.preventDefault?.();
767
+ return set(value.slice(0, -1));
768
+ }
769
+ if (event?.ctrl && name === "u") {
770
+ event.preventDefault?.();
771
+ return set("");
772
+ }
773
+ const typed = event?.sequence;
774
+ // One printable character; what a terminal sends for other keys is longer or starts with a control character.
775
+ if (!event?.ctrl && !event?.meta && typeof typed === "string" && [...typed].length === 1 && typed >= " " && typed !== "\x7f") {
776
+ event.preventDefault?.();
777
+ set(value + typed);
778
+ }
779
+ };
780
+ // A token is one word: line breaks and blanks around a pasted one are not part of it.
781
+ const paste = event => {
782
+ event?.preventDefault?.();
783
+ let pasted = "";
784
+ try {
785
+ pasted = new TextDecoder().decode(event?.bytes ?? new Uint8Array());
786
+ } catch {}
787
+ set(value + pasted.replace(/\s+/g, ""));
788
+ };
789
+ onMount(() => {
790
+ const timer = setTimeout(() => {
791
+ if (field && !field.isDestroyed) field.focus();
792
+ }, 1);
793
+ onCleanup(() => clearTimeout(timer));
794
+ });
795
+ onCleanup(() => {
796
+ value = "";
797
+ });
798
+ return (() => {
799
+ var _el$26 = _$createElement("box"),
800
+ _el$27 = _$createElement("text"),
801
+ _el$28 = _$createElement("b"),
802
+ _el$30 = _$createElement("text"),
803
+ _el$31 = _$createElement("box"),
804
+ _el$32 = _$createElement("text"),
805
+ _el$33 = _$createElement("text");
806
+ _$insertNode(_el$26, _el$27);
807
+ _$insertNode(_el$26, _el$30);
808
+ _$insertNode(_el$26, _el$31);
809
+ _$insertNode(_el$26, _el$33);
810
+ _$setProp(_el$26, "flexDirection", "column");
811
+ _$setProp(_el$26, "paddingLeft", 2);
812
+ _$setProp(_el$26, "paddingRight", 2);
813
+ _$setProp(_el$26, "paddingTop", 1);
814
+ _$setProp(_el$26, "paddingBottom", 1);
815
+ _$setProp(_el$26, "gap", 1);
816
+ _$insertNode(_el$27, _el$28);
817
+ _$insert(_el$28, () => props.options.title);
818
+ _$insert(_el$26, _$createComponent(Show, {
819
+ get when() {
820
+ return props.options.problem;
821
+ },
822
+ get children() {
823
+ var _el$29 = _$createElement("text");
824
+ _$insert(_el$29, () => props.options.problem);
825
+ _$effect(_$p => _$setProp(_el$29, "fg", palette().failed, _$p));
826
+ return _el$29;
656
827
  }
657
- if (details?.type !== "session.inbox.enqueued") return;
658
- const item = details.data?.item;
659
- // What was read of a session from the server is kept current: the host tells of each new notice.
660
- reader.arrived(details.data?.sessionID, item);
661
- // A run that starts, and a report or preview that arrives, opens the panel in the session on screen,
662
- // unless the notice says that the settings leave the panel to /pr-panel.
663
- if (item?.type !== "synthetic" || !opensPanel(item.payload?.text, item.payload?.metadata)) return;
664
- const route = context.ui.router.current();
665
- if (route.type === "session" && route.sessionID === details.data.sessionID) show("notice");
828
+ }), _el$30);
829
+ _$insert(_el$30, () => props.options.description);
830
+ _$insertNode(_el$31, _el$32);
831
+ _$use(node => {
832
+ field = node;
833
+ }, _el$31);
834
+ _$setProp(_el$31, "focusable", true);
835
+ _$setProp(_el$31, "onKeyDown", key);
836
+ _$setProp(_el$31, "onPaste", paste);
837
+ _$setProp(_el$31, "onMouseDown", () => {
838
+ if (field && !field.isDestroyed) field.focus();
666
839
  });
667
- context.ui.slot({
668
- append: "app",
669
- render: () => {
670
- createEffect(() => {
671
- const route = context.ui.router.current();
672
- if (route.type !== "session" || !wanted.has(route.sessionID)) return;
673
- const sessionID = route.sessionID;
674
- // After the session's view has mounted.
675
- setTimeout(() => {
676
- const now = context.ui.router.current();
677
- if (now.type === "session" && now.sessionID === sessionID && wanted.has(sessionID)) show("return");
678
- }, 0);
679
- });
680
- return null;
681
- }
840
+ _$setProp(_el$31, "height", 1);
841
+ _$setProp(_el$31, "paddingLeft", 1);
842
+ _$setProp(_el$31, "flexShrink", 0);
843
+ _$insert(_el$32, (() => {
844
+ var _c$2 = _$memo(() => !!length());
845
+ return () => _c$2() ? `${"•".repeat(Math.min(length(), 16))} (${length()} characters, hidden)` : "Paste the token here; it is not shown";
846
+ })());
847
+ _$insertNode(_el$33, _$createTextNode(`enter save · ctrl+u clear · esc leave`));
848
+ _$effect(_p$ => {
849
+ var _v$11 = palette().base,
850
+ _v$12 = palette().muted,
851
+ _v$13 = palette().hover ?? palette().surface,
852
+ _v$14 = length() ? palette().base : palette().muted,
853
+ _v$15 = palette().muted;
854
+ _v$11 !== _p$.e && (_p$.e = _$setProp(_el$27, "fg", _v$11, _p$.e));
855
+ _v$12 !== _p$.t && (_p$.t = _$setProp(_el$30, "fg", _v$12, _p$.t));
856
+ _v$13 !== _p$.a && (_p$.a = _$setProp(_el$31, "backgroundColor", _v$13, _p$.a));
857
+ _v$14 !== _p$.o && (_p$.o = _$setProp(_el$32, "fg", _v$14, _p$.o));
858
+ _v$15 !== _p$.i && (_p$.i = _$setProp(_el$33, "fg", _v$15, _p$.i));
859
+ return _p$;
860
+ }, {
861
+ e: undefined,
862
+ t: undefined,
863
+ a: undefined,
864
+ o: undefined,
865
+ i: undefined
682
866
  });
683
- context.keymap.layer(() => ({
684
- mode: "global",
685
- commands: [{
686
- id: "azpr.panel",
687
- title: "AZPR: show or hide the review panel",
688
- description: "The sessions of the review in this conversation and its report or comment preview, rendered.",
689
- group: "AZPR",
690
- palette: true,
691
- slash: {
692
- name: "pr-panel"
693
- },
694
- run() {
695
- const route = context.ui.router.current();
696
- if (route.type !== "session") {
697
- context.ui.toast.show({
698
- message: "AZPR: open a conversation first.",
699
- variant: "info"
700
- });
701
- return;
702
- }
703
- if (shown(route.sessionID)) {
704
- wanted.delete(route.sessionID);
705
- context.ui.panel.close();
706
- } else show();
707
- }
708
- }]
867
+ return _el$26;
868
+ })();
869
+ }
870
+
871
+ /**
872
+ * The dialogs of /pr-setup: they open when the server part answers /pr-setup without options in the session
873
+ * on screen, and from the command palette. Returns its cleanup, or nothing on a host without dialogs or RPC.
874
+ */
875
+ function setupPart(context) {
876
+ const needed = [context?.ui?.dialog?.select, context?.ui?.dialog?.prompt, context?.ui?.dialog?.confirm, context?.ui?.dialog?.alert, context?.ui?.dialog?.show, context?.ui?.toast?.show, context?.client?.rpc, context?.keymap?.layer, context?.data?.listen, context?.data?.location?.model?.list];
877
+ if (needed.some(part => typeof part !== "function")) return undefined;
878
+ let disposed = false;
879
+ const rpc = context.client.rpc(SETUP_RPC);
880
+ // Each project directory has a server part of its own. Setup talks to the one of the session on screen, where
881
+ // the review commands that follow will run; without a session, to the one of the directory this TUI works in.
882
+ const onScreen = () => {
883
+ const route = context.ui.router?.current?.();
884
+ const session = route?.type === "session" ? context.data.session?.get?.(route.sessionID) : undefined;
885
+ return session?.location ?? context.location ?? context.data.location.default?.();
886
+ };
887
+ // While a setup is open its place is fixed: another project's session may come on screen meanwhile, and what
888
+ // was checked for one project's server part must not be saved by another's.
889
+ let pinned;
890
+ const place = () => pinned ? pinned.at : onScreen();
891
+ const call = (method, input = {}) => {
892
+ const directory = place()?.directory;
893
+ return rpc[method](input, directory ? {
894
+ location: {
895
+ directory
896
+ }
897
+ } : undefined);
898
+ };
899
+ // The models the host offers that can call tools, as the server part will accept them.
900
+ const models = async () => {
901
+ const at = place();
902
+ await Promise.all([context.data.location.model.sync?.(at), context.data.location.provider?.sync?.(at)]).catch(() => {});
903
+ const providers = new Map((context.data.location.provider?.list?.(at) ?? []).map(provider => [provider.id, provider.name ?? provider.id]));
904
+ return (context.data.location.model.list(at) ?? []).filter(model => model?.enabled !== false && model?.capabilities?.tools === true && typeof model.providerID === "string" && typeof model.id === "string").map(model => ({
905
+ id: `${model.providerID}/${model.id}`,
906
+ name: typeof model.name === "string" && model.name ? model.name : model.id,
907
+ provider: providers.get(model.providerID) ?? model.providerID,
908
+ variants: (Array.isArray(model.variants) ? model.variants : []).map(variant => variant?.id).filter(id => typeof id === "string" && id)
709
909
  }));
710
- return () => {
711
- disposed = true;
712
- reader.dispose();
713
- stop?.();
714
- };
910
+ };
911
+ // The commands as the server has them now. OpenCode 2.3.0 does not tell its clients where commands changed
912
+ // when a plugin changes them after it was loaded, so after a first setup the list is read again here.
913
+ const commandsChanged = () => {
914
+ if (disposed) return;
915
+ const at = place();
916
+ try {
917
+ context.data.location.command?.invalidate?.(at);
918
+ void Promise.resolve(context.data.location.command?.sync?.(at)).catch(() => {});
919
+ } catch {}
920
+ };
921
+ const setup = createSetup({
922
+ call,
923
+ dialog: context.ui.dialog,
924
+ secret: options => askSecret(context, options),
925
+ toast: options => {
926
+ if (!disposed) context.ui.toast.show(options);
927
+ },
928
+ models,
929
+ loaded: commandsChanged,
930
+ pin: () => {
931
+ const mine = pinned = {
932
+ at: onScreen()
933
+ };
934
+ return () => {
935
+ if (pinned === mine) pinned = undefined;
936
+ };
937
+ }
938
+ });
939
+ // Also when the settings were saved with options, or loaded by another command: the server part says so.
940
+ let unsubscribe;
941
+ try {
942
+ unsubscribe = rpc.events?.on?.("loaded", commandsChanged);
943
+ } catch {}
944
+ const stop = context.data.listen(event => {
945
+ if (disposed) return;
946
+ const details = event?.details;
947
+ if (details?.type !== "session.inbox.enqueued") return;
948
+ const item = details.data?.item;
949
+ if (item?.type !== "synthetic" || !isSetupNotice(item.payload?.metadata)) return;
950
+ // Only where the person just typed /pr-setup: the session on screen.
951
+ const route = context.ui.router?.current?.();
952
+ if (route?.type === "session" && route.sessionID === details.data.sessionID) void setup.open();
953
+ });
954
+ context.keymap.layer(() => ({
955
+ mode: "global",
956
+ commands: [{
957
+ id: "azpr.setup",
958
+ title: "AZPR: settings",
959
+ description: "Set AZPR up, or show and change its settings: organization, token, proxy, models and more.",
960
+ group: "AZPR",
961
+ palette: true,
962
+ run() {
963
+ void setup.open();
964
+ }
965
+ }]
966
+ }));
967
+
968
+ // Say once how to set AZPR up when it has no usable settings. The server part may still be loading when the
969
+ // TUI starts, so it is asked a second time a little later; neither timer outlives this part.
970
+ const timers = [1500, 6000].map(delay => setTimeout(() => !disposed && void setup.hint(), delay));
971
+ return () => {
972
+ disposed = true;
973
+ // A setup that is open ends at its next step: nothing is saved by a part that was unloaded.
974
+ setup.stop();
975
+ timers.forEach(clearTimeout);
976
+ stop?.();
977
+ try {
978
+ unsubscribe?.();
979
+ } catch {}
980
+ };
981
+ }
982
+ export default {
983
+ id: "azpr.tui",
984
+ setup(context) {
985
+ // Each part checks for itself what it needs of the host, and stays out of the way where that is missing.
986
+ const stops = [panelPart(context), setupPart(context)].filter(stop => typeof stop === "function");
987
+ return () => stops.forEach(stop => stop());
715
988
  }
716
989
  };