bom-pi 0.0.0-stage → 0.0.2

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.
Files changed (131) hide show
  1. package/README.md +33 -2
  2. package/apps/README.md +7 -0
  3. package/apps/web/README.md +21 -0
  4. package/apps/web/pocket-adapter.mjs +323 -0
  5. package/apps/web/public/LICENSE.pocket +21 -0
  6. package/apps/web/public/activity-fold.js +28 -0
  7. package/apps/web/public/agent-now.js +37 -0
  8. package/apps/web/public/agent-tile.js +79 -0
  9. package/apps/web/public/app.js +975 -0
  10. package/apps/web/public/apple-touch-icon.png +0 -0
  11. package/apps/web/public/artifact-bridge.js +687 -0
  12. package/apps/web/public/avatar.js +87 -0
  13. package/apps/web/public/back.js +554 -0
  14. package/apps/web/public/box-keys.js +216 -0
  15. package/apps/web/public/browser-panel.js +1097 -0
  16. package/apps/web/public/browser.js +68 -0
  17. package/apps/web/public/calls.js +139 -0
  18. package/apps/web/public/chat.js +919 -0
  19. package/apps/web/public/clock.js +34 -0
  20. package/apps/web/public/commands.js +578 -0
  21. package/apps/web/public/composer.js +2068 -0
  22. package/apps/web/public/diff-parse.js +672 -0
  23. package/apps/web/public/diff.js +1646 -0
  24. package/apps/web/public/dock.js +299 -0
  25. package/apps/web/public/extension-ui.js +2734 -0
  26. package/apps/web/public/files-panel.js +775 -0
  27. package/apps/web/public/files.js +368 -0
  28. package/apps/web/public/fonts/FiraCode-VF.woff2 +0 -0
  29. package/apps/web/public/fonts/Geist.woff2 +0 -0
  30. package/apps/web/public/fonts/IBMPlexMono-Variable-Italic.woff2 +0 -0
  31. package/apps/web/public/fonts/IBMPlexMono-Variable.woff2 +0 -0
  32. package/apps/web/public/fonts/InterVariable-Italic.woff2 +0 -0
  33. package/apps/web/public/fonts/InterVariable.woff2 +0 -0
  34. package/apps/web/public/fonts/Iosevka-Bold.woff2 +0 -0
  35. package/apps/web/public/fonts/Iosevka-BoldItalic.woff2 +0 -0
  36. package/apps/web/public/fonts/Iosevka-Italic.woff2 +0 -0
  37. package/apps/web/public/fonts/Iosevka-Regular.woff2 +0 -0
  38. package/apps/web/public/fonts/Iosevka-SemiBold.woff2 +0 -0
  39. package/apps/web/public/fonts/Iosevka-SemiBoldItalic.woff2 +0 -0
  40. package/apps/web/public/fonts/JetBrainsMono-Italic.woff2 +0 -0
  41. package/apps/web/public/fonts/JetBrainsMono.woff2 +0 -0
  42. package/apps/web/public/fonts/NerdFontsSymbolsMono.woff2 +0 -0
  43. package/apps/web/public/fonts/SOURCES.md +69 -0
  44. package/apps/web/public/gestures.js +610 -0
  45. package/apps/web/public/highlight.js +480 -0
  46. package/apps/web/public/history.js +70 -0
  47. package/apps/web/public/home.js +169 -0
  48. package/apps/web/public/icon-192.png +0 -0
  49. package/apps/web/public/icon-512.png +0 -0
  50. package/apps/web/public/icon-maskable-512.png +0 -0
  51. package/apps/web/public/icon.svg +16 -0
  52. package/apps/web/public/index.html +71 -0
  53. package/apps/web/public/launcher.js +1293 -0
  54. package/apps/web/public/linked-sessions.js +55 -0
  55. package/apps/web/public/live.js +39 -0
  56. package/apps/web/public/manifest.webmanifest +32 -0
  57. package/apps/web/public/model-refresh.js +73 -0
  58. package/apps/web/public/notify.js +251 -0
  59. package/apps/web/public/peeks.js +920 -0
  60. package/apps/web/public/pip-attachments.js +182 -0
  61. package/apps/web/public/pip-window.js +141 -0
  62. package/apps/web/public/pip.js +1217 -0
  63. package/apps/web/public/quote-reply.js +109 -0
  64. package/apps/web/public/quote-text.js +31 -0
  65. package/apps/web/public/rich.js +403 -0
  66. package/apps/web/public/run-frame.js +106 -0
  67. package/apps/web/public/safe-mode.js +71 -0
  68. package/apps/web/public/seen.js +169 -0
  69. package/apps/web/public/session-tree.js +117 -0
  70. package/apps/web/public/sessions.js +2695 -0
  71. package/apps/web/public/share.js +126 -0
  72. package/apps/web/public/sheets/appearance.js +820 -0
  73. package/apps/web/public/sheets/branch.js +353 -0
  74. package/apps/web/public/sheets/extensions.js +818 -0
  75. package/apps/web/public/sheets/file.js +318 -0
  76. package/apps/web/public/sheets/find.js +250 -0
  77. package/apps/web/public/sheets/folder.js +680 -0
  78. package/apps/web/public/sheets/gallery.js +2340 -0
  79. package/apps/web/public/sheets/hooks.js +350 -0
  80. package/apps/web/public/sheets/memory.js +531 -0
  81. package/apps/web/public/sheets/menu.js +580 -0
  82. package/apps/web/public/sheets/message.js +220 -0
  83. package/apps/web/public/sheets/model.js +412 -0
  84. package/apps/web/public/sheets/people.js +414 -0
  85. package/apps/web/public/sheets/pi-sessions.js +264 -0
  86. package/apps/web/public/sheets/providers.js +525 -0
  87. package/apps/web/public/sheets/running.js +130 -0
  88. package/apps/web/public/sheets/schedules.js +71 -0
  89. package/apps/web/public/sheets/session.js +249 -0
  90. package/apps/web/public/sheets/shortcuts.js +101 -0
  91. package/apps/web/public/sheets/spend.js +150 -0
  92. package/apps/web/public/sheets/text.js +53 -0
  93. package/apps/web/public/sheets/viewers.js +363 -0
  94. package/apps/web/public/sheets/worktree.js +50 -0
  95. package/apps/web/public/sheets.js +410 -0
  96. package/apps/web/public/signin.js +57 -0
  97. package/apps/web/public/slots.js +208 -0
  98. package/apps/web/public/store.js +1760 -0
  99. package/apps/web/public/style.css +18625 -0
  100. package/apps/web/public/subagents-board.js +879 -0
  101. package/apps/web/public/subagents.js +273 -0
  102. package/apps/web/public/sw.js +169 -0
  103. package/apps/web/public/taps.js +26 -0
  104. package/apps/web/public/theme-surface.d.ts +4 -0
  105. package/apps/web/public/theme-surface.js +266 -0
  106. package/apps/web/public/theme.js +845 -0
  107. package/apps/web/public/themes/lantern/colors.toml +25 -0
  108. package/apps/web/public/themes/lantern/direction.css +60 -0
  109. package/apps/web/public/themes/lantern/frame.css +254 -0
  110. package/apps/web/public/themes/lantern/theme.css +10 -0
  111. package/apps/web/public/themes/lantern/theme.json +7 -0
  112. package/apps/web/public/themes/pocket/theme.css +189 -0
  113. package/apps/web/public/themes/pocket/theme.json +6 -0
  114. package/apps/web/public/themes.js +556 -0
  115. package/apps/web/public/transcript.js +3348 -0
  116. package/apps/web/public/trust.js +295 -0
  117. package/apps/web/public/turns.js +507 -0
  118. package/apps/web/public/ui.js +1910 -0
  119. package/apps/web/public/voice.js +345 -0
  120. package/apps/web/server.mjs +774 -0
  121. package/package.json +43 -4
  122. package/packages/cli/src/bom-pi.mjs +452 -0
  123. package/packages/cli/templates/blank.factory.json +7 -0
  124. package/packages/cli/templates/dev.factory.json +11 -0
  125. package/packages/cli/templates/docs.factory.json +12 -0
  126. package/packages/cli/templates/ops.factory.json +14 -0
  127. package/packages/cli/templates/triage.factory.json +10 -0
  128. package/packages/core/src/daemon.mjs +2047 -0
  129. package/packages/core/src/limit-gate.mjs +192 -0
  130. package/packages/core/src/rpc-client.mjs +66 -0
  131. package/systemd/README.md +11 -0
@@ -0,0 +1,220 @@
1
+ // What a message's menu does: copy, fork from here, and send again (with this model or another).
2
+ import { useEffect, useRef, useState } from "preact/hooks";
3
+ import {
4
+ actions,
5
+ attempt,
6
+ canSteer,
7
+ closeSheet,
8
+ navigate,
9
+ notify,
10
+ openSheet,
11
+ scoped,
12
+ store,
13
+ } from "../store.js";
14
+ import {
15
+ copyText,
16
+ html,
17
+ Icon,
18
+ item,
19
+ modelLabel,
20
+ replyText,
21
+ Sheet,
22
+ Switch,
23
+ writtenText,
24
+ } from "../ui.js";
25
+ import { memoryAvailable } from "./memory.js";
26
+
27
+ /** The message to Pi a reply answers: the newest one before it. */
28
+ function promptBefore(entryId) {
29
+ const { view } = store.state;
30
+
31
+ for (let index = view.order.indexOf(entryId) - 1; index >= 0; index--) {
32
+ const entry = view.entries.get(view.order[index]);
33
+
34
+ if (entry?.kind === "user") {
35
+ return entry;
36
+ }
37
+ }
38
+
39
+ return undefined;
40
+ }
41
+
42
+ /** A message as the start of a lesson: one line, cut to a lesson's length. */
43
+ function lessonFrom(text) {
44
+ const flat = text.replace(/\s+/g, " ").trim();
45
+
46
+ return flat.length > 300 ? `${flat.slice(0, 299)}…` : flat;
47
+ }
48
+
49
+ /** A fork or a send again on its way: a second tap waits for it. */
50
+ let branching = false;
51
+
52
+ /** Make a new session from this one and open it. A second tap while the first is on its way does nothing. */
53
+ const openBranch = (run) => {
54
+ if (branching) {
55
+ return;
56
+ }
57
+
58
+ branching = true;
59
+ attempt(async () => {
60
+ const created = await run();
61
+
62
+ navigate(created.id);
63
+ notify("info", "Opened the new session. The original is unchanged.");
64
+ }).finally(() => {
65
+ branching = false;
66
+ });
67
+ };
68
+
69
+ /**
70
+ * A field that shows in a sheet already open takes the keys, where a keyboard is at hand: the sheet focused its field
71
+ * marked autofocus only when it opened.
72
+ */
73
+ function useFocus(ref, shown) {
74
+ useEffect(() => {
75
+ if (shown && !matchMedia("(pointer: coarse)").matches) {
76
+ ref.current?.focus({ preventScroll: true });
77
+ }
78
+ }, [shown]);
79
+ }
80
+
81
+ /** Send a message to Pi again in a fork: with this session's model, or another one. */
82
+ function SendAgain({ prompt, worktree }) {
83
+ const { models, view } = store.state;
84
+ const [choosing, setChoosing] = useState(false);
85
+ const [query, setQuery] = useState("");
86
+ const search = useRef(null);
87
+
88
+ useFocus(search, choosing);
89
+ const current = view.agent?.model;
90
+ const needle = query.trim().toLowerCase();
91
+ const others = models.filter(
92
+ (model) =>
93
+ !(model.provider === current?.provider && model.id === current?.modelId) &&
94
+ (needle === "" ||
95
+ `${model.provider}/${model.id} ${model.name}`.toLowerCase().includes(needle)),
96
+ );
97
+
98
+ return html`<div class="group-title">Send again in a new session</div>
99
+ ${item(`With ${modelLabel(view.agent)}`, () => openBranch(() => actions.resend(prompt.id, { worktree })), "same model")}
100
+ ${
101
+ choosing
102
+ ? html`<div class="message-model-search">
103
+ <label class="search">
104
+ <${Icon} name="search" size=${16} />
105
+ <input
106
+ ref=${search}
107
+ placeholder="Search models"
108
+ value=${query}
109
+ onInput=${(event) => setQuery(event.currentTarget.value)}
110
+ />
111
+ </label>
112
+ </div>
113
+ ${others.map((model) =>
114
+ item(
115
+ model.name,
116
+ () =>
117
+ openBranch(() =>
118
+ actions.resend(prompt.id, {
119
+ model: { provider: model.provider, modelId: model.id },
120
+ worktree,
121
+ }),
122
+ ),
123
+ html`<span class="mono">${model.provider}</span>`,
124
+ ),
125
+ )}`
126
+ : item("With another model…", () => setChoosing(true))
127
+ }`;
128
+ }
129
+
130
+ /** What can be done with one message: fork from it, edit it, send it again, or copy it. */
131
+ export function MessageSheet({ entryId }) {
132
+ const { view } = store.state;
133
+ const entry =
134
+ view.entries.get(entryId) ?? store.state.history?.find((each) => each.id === entryId);
135
+ const [draft, setDraft] = useState(null);
136
+ const [worktree, setWorktree] = useState(false);
137
+ const field = useRef(null);
138
+
139
+ useFocus(field, draft !== null);
140
+
141
+ if (entry?.kind !== "user" && entry?.kind !== "assistant") {
142
+ return html`<${Sheet} title="Message" onClose=${closeSheet}>
143
+ <p class="muted">This message is not here anymore.</p>
144
+ <//>`;
145
+ }
146
+
147
+ // Forks are new sessions: who may start one may fork.
148
+ const canBranch = canSteer() && !scoped() && view.conversation?.kind === "session";
149
+ // A message to Pi (from anyone), or one of its replies.
150
+ const toPi = entry.kind === "user";
151
+ const text = toPi ? writtenText(entry) : replyText(entry);
152
+ // A message of files alone has no words: its preview says so, and there is nothing to copy or remember.
153
+ const filesAlone = toPi && text.trim() === "";
154
+ const prompt = toPi ? entry : promptBefore(entryId);
155
+
156
+ const copy = () => {
157
+ closeSheet();
158
+ copyText(text).then(
159
+ () => notify("info", "Copied."),
160
+ () => notify("error", "Could not copy."),
161
+ );
162
+ };
163
+
164
+ if (draft !== null) {
165
+ return html`<${Sheet} title="Edit and send again" onClose=${closeSheet}>
166
+ <p class="muted small">
167
+ Pi gets the edited message in a new session that forks just before the original. The original stays as it is.
168
+ </p>
169
+ <textarea
170
+ ref=${field}
171
+ rows="6"
172
+ value=${draft}
173
+ onInput=${(event) => setDraft(event.currentTarget.value)}
174
+ ></textarea>
175
+ <div class="row">
176
+ <button class="button" onClick=${() => setDraft(null)}>Back</button>
177
+ <button
178
+ class="button primary grow"
179
+ onClick=${() => openBranch(() => actions.resend(entry.id, { text: draft, worktree }))}
180
+ >
181
+ Send in a new session
182
+ </button>
183
+ </div>
184
+ <//>`;
185
+ }
186
+
187
+ return html`<${Sheet} title=${toPi ? "Message" : "Reply"} onClose=${closeSheet}>
188
+ <div class="message-preview">${filesAlone ? "(attachments)" : text}</div>
189
+ ${
190
+ canBranch &&
191
+ view.conversation.inRepository &&
192
+ html`<div class="sheet-list">
193
+ <div class="extension message-worktree">
194
+ <div class="extension-main">New sessions get a git worktree of their own</div>
195
+ <${Switch}
196
+ on=${worktree}
197
+ label="New sessions get a git worktree of their own"
198
+ onChange=${() => setWorktree(!worktree)}
199
+ />
200
+ </div>
201
+ </div>`
202
+ }
203
+ <div class="sheet-list message-actions">
204
+ ${canBranch && !toPi && item("Fork from here", () => openBranch(() => actions.fork(entry.id, { worktree })), "everything up to this reply")}
205
+ ${canBranch && toPi && item("Edit and send again…", () => setDraft(text))}
206
+ ${canBranch && prompt && html`<${SendAgain} prompt=${prompt} worktree=${worktree} />`}
207
+ ${
208
+ memoryAvailable() &&
209
+ canSteer() &&
210
+ !filesAlone &&
211
+ item(
212
+ "Remember…",
213
+ () => openSheet({ type: "memory", draft: lessonFrom(text) }),
214
+ "keep it as a lesson for Pi",
215
+ )
216
+ }
217
+ ${!filesAlone && item("Copy text", copy)}
218
+ </div>
219
+ <//>`;
220
+ }
@@ -0,0 +1,412 @@
1
+ // The model picker: a menu over the model chip, searchable once there are many models; and the same menu for the model
2
+ // and thinking level new sessions start with.
3
+ import { useEffect, useRef, useState } from "preact/hooks";
4
+ import { actions, api, attempt, closeSheet, openSheet, store } from "../store.js";
5
+ import { useDragToClose } from "../gestures.js";
6
+ import {
7
+ anchorStyle,
8
+ BOTTOM_SHEETS,
9
+ formatTokens,
10
+ html,
11
+ Icon,
12
+ popAnchor,
13
+ scrimPress,
14
+ } from "../ui.js";
15
+
16
+ /** Models past this many get a search box in the picker. */
17
+ const MODEL_SEARCH_AT = 8;
18
+
19
+ /** Over the message box the picker is 330px wide; on a phone, the width `popAnchor` gives. */
20
+ const widthOf = (anchor) =>
21
+ matchMedia("(min-width: 700px)").matches ? Math.min(anchor.width, 330) : anchor.width;
22
+
23
+ const coarsePointer = matchMedia("(pointer: coarse)").matches;
24
+
25
+ /** Where the picker opens: above the message box's model chip (`popAnchor`). */
26
+ const modelAnchor = () => popAnchor(".model-chip");
27
+
28
+ /** Whether the server keeps a default model: one started before it did sends none, and would ignore one. */
29
+ export const defaultsKept = () => store.state.server?.defaultModel !== undefined;
30
+
31
+ /** A model chosen as the default (`server.defaultModel`) and its thinking level, in a few words: "Opus 4.5 · high". */
32
+ export function defaultLabel(chosen, models) {
33
+ if (!chosen) {
34
+ return "";
35
+ }
36
+
37
+ const model = models.find(
38
+ (each) => each.provider === chosen.provider && each.id === chosen.modelId,
39
+ );
40
+
41
+ // Not signed in any more, or gone from its provider: new sessions start with it anyway, and say so, rather than
42
+ // with a model nobody chose for them.
43
+ if (!model) {
44
+ return `${chosen.modelId} (not signed in)`;
45
+ }
46
+
47
+ return `${model.name}${model.reasoning ? ` · ${chosen.thinkingLevel ?? "off"}` : ""}${model.signIn ? " (needs sign-in)" : ""}`;
48
+ }
49
+
50
+ /** Whether a model is one the picker's search finds: each word in its id or name, as `/model` finds them. */
51
+ const matches = (model, query) => {
52
+ const haystack = `${model.provider}/${model.id} ${model.name}`.toLowerCase();
53
+
54
+ return query
55
+ .trim()
56
+ .toLowerCase()
57
+ .split(/\s+/)
58
+ .every((word) => haystack.includes(word));
59
+ };
60
+
61
+ /**
62
+ * The model picker: a menu that opens up from the message box's model chip, with the current model first and checked,
63
+ * and how hard Pi thinks below, then, for the owner, Make default. Arrows and Enter pick, and Tab switches between the
64
+ * scoped models (Pi's `enabledModels`) and all of them; a search box shows when there are many models, or when
65
+ * `/model son` opened it already searching. Opened for the default (`forDefault`, from the menu), it picks the model
66
+ * and thinking level new sessions start with instead, and stays open for both.
67
+ */
68
+ export function ModelPicker() {
69
+ const { models, server, me } = store.state;
70
+ const [pipId] = useState(() => store.state.sheet?.pipId);
71
+ const view = pipId === undefined ? store.state.view : store.state.pipView;
72
+ const locked =
73
+ pipId !== undefined &&
74
+ (view.conversation?.id !== pipId ||
75
+ view.live?.busy === true ||
76
+ (view.live?.compactions?.length ?? 0) > 0);
77
+ const configure = (change) =>
78
+ pipId === undefined
79
+ ? actions.configure(change)
80
+ : api(`c/${pipId}/configure`, { ...change, pipPanel: true });
81
+ // While the picker animates out, the store has no sheet any more.
82
+ const [query, setQuery] = useState(store.state.sheet?.query ?? "");
83
+ const [forDefault] = useState(() => store.state.sheet?.forDefault === true);
84
+ const scopedModels = models.filter((model) => model.scoped);
85
+ // Opened already searching (`/model gem`) for models only outside the scope: it starts with all of them.
86
+ const [showAll, setShowAll] = useState(
87
+ () => query.trim() !== "" && !scopedModels.some((model) => matches(model, query)),
88
+ );
89
+ const choices = showAll || scopedModels.length === 0 ? models : scopedModels;
90
+ // Decided once: a search box that went away when emptied would take the caret with it.
91
+ const [searchable] = useState(() => models.length > MODEL_SEARCH_AT || query !== "");
92
+ const [picked, setPicked] = useState(0);
93
+ const [anchor, setAnchor] = useState(() =>
94
+ forDefault || pipId !== undefined ? null : modelAnchor(),
95
+ );
96
+ const box = useRef(null);
97
+ const search = useRef(null);
98
+ const list = useRef(null);
99
+ // Arrows scroll the list to the model they reach; the mouse does not, or the list would run away under it.
100
+ const keyed = useRef(false);
101
+ const agent = view.agent;
102
+
103
+ // On a phone, a swipe away from where it opens closes it, as a sheet's does.
104
+ useDragToClose(box, {
105
+ dir: anchor?.down ? "up" : "down",
106
+ onClose: closeSheet,
107
+ enabled: BOTTOM_SHEETS.matches,
108
+ });
109
+ const owner = me?.role === "owner";
110
+ const chosen = server?.defaultModel ?? null;
111
+ const isDefault = (model) =>
112
+ chosen?.provider === model?.provider && chosen?.modelId === (model?.id ?? model?.modelId);
113
+ const defaultModel = models.find(isDefault);
114
+ // The model and thinking level this picker sets: the session's, or the default's.
115
+ const current = forDefault ? chosen : agent?.model;
116
+ const isCurrent = (model) =>
117
+ current?.provider === model.provider && current?.modelId === model.id;
118
+ const levels = (forDefault ? defaultModel?.levels : agent?.levels) ?? ["off"];
119
+ // Before there is a default, the first one picked thinks as hard as this session does.
120
+ const level = forDefault
121
+ ? (chosen?.thinkingLevel ?? agent?.thinkingLevel ?? "off")
122
+ : agent?.thinkingLevel;
123
+ const reasoning = forDefault ? defaultModel?.reasoning : agent?.reasoning;
124
+ const shown = choices
125
+ .filter((model) => matches(model, query))
126
+ .sort((a, b) => Number(isCurrent(b)) - Number(isCurrent(a)));
127
+ // The list can change while the picker is open.
128
+ const active = Math.max(0, Math.min(picked, shown.length - 1));
129
+ // The session's model and level are the default already.
130
+ const sessionIsDefault =
131
+ !forDefault &&
132
+ isDefault(agent?.model) &&
133
+ (chosen?.thinkingLevel ?? "off") === (agent?.thinkingLevel ?? "off");
134
+ const setDefault = (model, thinkingLevel) =>
135
+ attempt(() =>
136
+ actions.setDefaultModel({
137
+ provider: model.provider,
138
+ modelId: model.id ?? model.modelId,
139
+ thinkingLevel,
140
+ }),
141
+ );
142
+
143
+ useEffect(() => {
144
+ // The default's picker has no chip to open from: it stays in the middle.
145
+ const place = () => setAnchor(forDefault || pipId !== undefined ? null : modelAnchor());
146
+ const onKey = (event) => event.key === "Escape" && closeSheet();
147
+
148
+ addEventListener("resize", place);
149
+ visualViewport?.addEventListener("resize", place);
150
+ addEventListener("keydown", onKey);
151
+ // Keys go to the picker, not the message box behind it. Phones keep their keyboard down until the search is tapped.
152
+ (searchable && !coarsePointer ? search.current : box.current)?.focus({
153
+ preventScroll: true,
154
+ });
155
+
156
+ return () => {
157
+ removeEventListener("resize", place);
158
+ visualViewport?.removeEventListener("resize", place);
159
+ removeEventListener("keydown", onKey);
160
+ };
161
+ }, []);
162
+ useEffect(() => {
163
+ if (!keyed.current) {
164
+ return;
165
+ }
166
+
167
+ keyed.current = false;
168
+ list.current?.querySelector(".pop-menu-row.on")?.scrollIntoView({ block: "nearest" });
169
+ }, [active]);
170
+
171
+ const pick = (model) => {
172
+ if (locked) {
173
+ return;
174
+ }
175
+
176
+ // The default's picker stays open, for its thinking level.
177
+ if (forDefault) {
178
+ return isCurrent(model) ? undefined : setDefault(model, level);
179
+ }
180
+
181
+ attempt(async () => {
182
+ if (!isCurrent(model)) {
183
+ await configure({ model: { provider: model.provider, modelId: model.id } });
184
+ }
185
+
186
+ closeSheet();
187
+ });
188
+ };
189
+
190
+ const think = (each) =>
191
+ forDefault
192
+ ? setDefault(defaultModel, each)
193
+ : attempt(() => configure({ thinkingLevel: each }));
194
+
195
+ // Arrows and Enter move through the models, and Tab switches scoped and all, from the search box or the picker
196
+ // itself, not from its other buttons: there Tab and Shift+Tab move the focus as anywhere else.
197
+ const onKeyDown = (event) => {
198
+ if (
199
+ event.isComposing ||
200
+ (event.target !== search.current && event.target !== box.current)
201
+ ) {
202
+ return;
203
+ }
204
+
205
+ if (
206
+ event.key === "Tab" &&
207
+ scopedModels.length > 0 &&
208
+ !(event.shiftKey || event.altKey || event.ctrlKey || event.metaKey)
209
+ ) {
210
+ event.preventDefault();
211
+ setShowAll((all) => !all);
212
+ setPicked(0);
213
+ list.current?.scrollTo({ top: 0 });
214
+
215
+ return;
216
+ }
217
+
218
+ if (shown.length === 0) {
219
+ return;
220
+ }
221
+
222
+ // Alt+↑/↓ stays the app's: it switches sessions.
223
+ const step = event.altKey
224
+ ? 0
225
+ : event.key === "ArrowDown"
226
+ ? 1
227
+ : event.key === "ArrowUp"
228
+ ? -1
229
+ : 0;
230
+
231
+ if (step !== 0) {
232
+ event.preventDefault();
233
+ keyed.current = true;
234
+ setPicked((active + step + shown.length) % shown.length);
235
+ } else if (event.key === "Enter" && shown[active]) {
236
+ event.preventDefault();
237
+ pick(shown[active]);
238
+ }
239
+ };
240
+
241
+ return html`<div
242
+ class="overlay pop-overlay"
243
+ onMouseDown=${scrimPress(closeSheet)}
244
+ >
245
+ <section
246
+ class=${`pop-menu model-menu ${anchor ? (anchor.down ? "down" : "") : "free"}`}
247
+ style=${anchorStyle(anchor && { ...anchor, width: widthOf(anchor) })}
248
+ ref=${box}
249
+ role="dialog"
250
+ aria-modal="true"
251
+ aria-label=${forDefault ? "Default model" : "Model"}
252
+ aria-keyshortcuts=${scopedModels.length > 0 ? "Tab" : undefined}
253
+ tabindex="-1"
254
+ onKeyDown=${onKeyDown}
255
+ >
256
+ <div class="grip" aria-hidden="true"></div>
257
+ <header class="pop-menu-head">
258
+ <span>
259
+ ${forDefault ? "Default model" : "Model"}
260
+ ${forDefault && html`<span class="pop-menu-head-sub">for new sessions</span>`}
261
+ ${
262
+ scopedModels.length > 0 &&
263
+ html`<span
264
+ class="model-scope"
265
+ role="group"
266
+ aria-label="Model scope"
267
+ title="Tab switches between scoped and all models"
268
+ >
269
+ <span aria-hidden="true">[</span>
270
+ <button
271
+ class=${!showAll ? "on" : ""}
272
+ aria-pressed=${!showAll}
273
+ title="Models from Pi’s enabledModels setting"
274
+ onClick=${() => {
275
+ setShowAll(false);
276
+ setPicked(0);
277
+ }}
278
+ >
279
+ scoped
280
+ </button>
281
+ <span aria-hidden="true">|</span>
282
+ <button
283
+ class=${showAll ? "on" : ""}
284
+ aria-pressed=${showAll}
285
+ title="All models with a configured sign-in"
286
+ onClick=${() => {
287
+ setShowAll(true);
288
+ setPicked(0);
289
+ }}
290
+ >
291
+ all
292
+ </button>
293
+ <span aria-hidden="true">]</span>
294
+ </span>`
295
+ }
296
+ </span>
297
+ <button class="pop-menu-link" onClick=${() => openSheet({ type: "providers" })}>
298
+ <${Icon} name="key" size=${13} /> Providers
299
+ </button>
300
+ </header>
301
+ ${
302
+ searchable &&
303
+ html`<label class="search pop-menu-search">
304
+ <${Icon} name="search" size=${15} />
305
+ <input
306
+ ref=${search}
307
+ placeholder="Search models"
308
+ aria-keyshortcuts=${scopedModels.length > 0 ? "Tab" : undefined}
309
+ value=${query}
310
+ onInput=${(event) => {
311
+ setQuery(event.currentTarget.value);
312
+ setPicked(0);
313
+ }}
314
+ />
315
+ </label>`
316
+ }
317
+ <div class="pop-menu-list" ref=${list} role="listbox" aria-label="Models">
318
+ ${
319
+ models.length === 0 &&
320
+ html`<p class="muted pop-menu-note">
321
+ No models are available. Add a provider first.
322
+ </p>`
323
+ }
324
+ ${
325
+ models.length > 0 &&
326
+ shown.length === 0 &&
327
+ html`<p class="muted pop-menu-note">No model matches “${query.trim()}”.</p>`
328
+ }
329
+ ${shown.map(
330
+ (model, index) => html`<button
331
+ key=${`${model.provider}/${model.id}`}
332
+ class=${`pop-menu-row ${index === active ? "on" : ""} ${isCurrent(model) ? "current" : ""}`}
333
+ role="option"
334
+ aria-selected=${isCurrent(model)}
335
+ disabled=${locked}
336
+ title=${`${model.provider}/${model.id} · ${formatTokens(model.contextWindow)} context${model.images ? " · images" : ""}`}
337
+ onMouseMove=${() => index !== active && setPicked(index)}
338
+ onClick=${() => pick(model)}
339
+ >
340
+ <span class="pop-menu-row-text">
341
+ <span class="pop-menu-row-name">${model.name}</span>
342
+ <span class="pop-menu-row-sub">
343
+ ${model.provider}${!forDefault && isDefault(model) ? " · default" : ""}${model.signIn ? " · needs sign-in" : ""}
344
+ </span>
345
+ </span>
346
+ ${isCurrent(model) && html`<${Icon} name="check" size=${14} />`}
347
+ </button>`,
348
+ )}
349
+ </div>
350
+ ${
351
+ reasoning &&
352
+ html`<div class="pop-menu-foot">
353
+ <div class="label">Thinking</div>
354
+ <div class="segmented model-levels" role="radiogroup" aria-label="Thinking">
355
+ ${levels.map(
356
+ (each) =>
357
+ html`<button
358
+ role="radio"
359
+ aria-checked=${level === each}
360
+ disabled=${locked}
361
+ class=${level === each ? "on" : ""}
362
+ onClick=${() => think(each)}
363
+ >
364
+ ${each}
365
+ </button>`,
366
+ )}
367
+ </div>
368
+ </div>`
369
+ }
370
+ ${
371
+ pipId !== undefined || !defaultsKept()
372
+ ? null
373
+ : forDefault
374
+ ? html`<div class="pop-menu-foot pop-menu-default">
375
+ <span class="muted">
376
+ The owner default wins over project settings. Clear it to use a trusted project's model and thinking defaults for later new sessions. Existing sessions do not change.
377
+ </span>
378
+ ${
379
+ chosen
380
+ ? html`<button
381
+ class="pop-menu-link"
382
+ onClick=${() => attempt(() => actions.setDefaultModel(null))}
383
+ >
384
+ Clear owner default: use project defaults, then last picked
385
+ </button>`
386
+ : html`<span class="muted">
387
+ No owner default: new sessions use trusted project defaults, then the last model picked, then Pi's global default.
388
+ </span>`
389
+ }
390
+ </div>`
391
+ : // Only the owner makes a default: no one else gets an empty footer.
392
+ owner &&
393
+ models.some((model) => isCurrent(model)) &&
394
+ html`<div class="pop-menu-foot pop-menu-default">
395
+ ${
396
+ sessionIsDefault
397
+ ? html`<span class="pop-menu-default-mark">
398
+ <${Icon} name="check" size=${13} /> Default
399
+ </span>`
400
+ : html`<button
401
+ class="pop-menu-link"
402
+ title="New sessions start with this model and thinking level"
403
+ onClick=${() => setDefault(agent.model, agent.thinkingLevel ?? "off")}
404
+ >
405
+ Make default
406
+ </button>`
407
+ }
408
+ </div>`
409
+ }
410
+ </section>
411
+ </div>`;
412
+ }