telecoder 0.0.0-stage → 1.3.0

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 (235) hide show
  1. package/.env.example +375 -0
  2. package/CHANGELOG.md +1867 -0
  3. package/LICENSE +22 -0
  4. package/README.md +130 -2
  5. package/dist/audio/transcribe.js +41 -0
  6. package/dist/bin/mcp-server.js +363 -0
  7. package/dist/bin/tool-result.js +22 -0
  8. package/dist/bot/access/group-access.js +147 -0
  9. package/dist/bot/bot.js +573 -0
  10. package/dist/bot/command-catalog.js +263 -0
  11. package/dist/bot/deleted-threads.js +103 -0
  12. package/dist/bot/file-access.js +150 -0
  13. package/dist/bot/handlers/album.handler.js +128 -0
  14. package/dist/bot/handlers/branch.handler.js +1096 -0
  15. package/dist/bot/handlers/command/access.js +407 -0
  16. package/dist/bot/handlers/command/admin.js +803 -0
  17. package/dist/bot/handlers/command/all-view.js +33 -0
  18. package/dist/bot/handlers/command/auto-approve.js +138 -0
  19. package/dist/bot/handlers/command/background.js +37 -0
  20. package/dist/bot/handlers/command/botname.js +76 -0
  21. package/dist/bot/handlers/command/btw.js +175 -0
  22. package/dist/bot/handlers/command/cleanup.js +329 -0
  23. package/dist/bot/handlers/command/model.js +226 -0
  24. package/dist/bot/handlers/command/prefs-scope.js +362 -0
  25. package/dist/bot/handlers/command/project.js +458 -0
  26. package/dist/bot/handlers/command/resume.js +325 -0
  27. package/dist/bot/handlers/command/session.js +299 -0
  28. package/dist/bot/handlers/command/settings.js +657 -0
  29. package/dist/bot/handlers/command/shared.js +207 -0
  30. package/dist/bot/handlers/command/tasks.js +681 -0
  31. package/dist/bot/handlers/command/thread.js +128 -0
  32. package/dist/bot/handlers/command/topic-store.js +74 -0
  33. package/dist/bot/handlers/command/topic.js +27 -0
  34. package/dist/bot/handlers/command/usage.js +382 -0
  35. package/dist/bot/handlers/command-fallback.js +179 -0
  36. package/dist/bot/handlers/command.handler.js +446 -0
  37. package/dist/bot/handlers/loop.handler.js +389 -0
  38. package/dist/bot/handlers/media-fallback.js +126 -0
  39. package/dist/bot/handlers/message/replies.js +56 -0
  40. package/dist/bot/handlers/message/shared.js +95 -0
  41. package/dist/bot/handlers/message/turn-notify.js +135 -0
  42. package/dist/bot/handlers/message/turn-runner.js +56 -0
  43. package/dist/bot/handlers/message.handler.js +114 -0
  44. package/dist/bot/handlers/newbot.handler.js +230 -0
  45. package/dist/bot/handlers/photo.handler.js +286 -0
  46. package/dist/bot/handlers/prompts.handler.js +343 -0
  47. package/dist/bot/handlers/running-note.js +44 -0
  48. package/dist/bot/handlers/session-guard.js +78 -0
  49. package/dist/bot/handlers/suggestion.handler.js +84 -0
  50. package/dist/bot/handlers/tree.handler.js +480 -0
  51. package/dist/bot/handlers/view.js +41 -0
  52. package/dist/bot/handlers/voice.handler.js +147 -0
  53. package/dist/bot/kept-threads.js +51 -0
  54. package/dist/bot/middleware/admin-only.js +49 -0
  55. package/dist/bot/middleware/album-collector.js +69 -0
  56. package/dist/bot/middleware/auth.middleware.js +122 -0
  57. package/dist/bot/middleware/group-mention.middleware.js +145 -0
  58. package/dist/bot/middleware/group-role.middleware.js +75 -0
  59. package/dist/bot/middleware/message-batcher.js +87 -0
  60. package/dist/bot/middleware/queued-messages.js +1082 -0
  61. package/dist/bot/middleware/stale-filter.js +9 -0
  62. package/dist/build-info.json +8 -0
  63. package/dist/claude/ask-user-form.js +517 -0
  64. package/dist/claude/ask-user.js +363 -0
  65. package/dist/claude/auto-approve.js +225 -0
  66. package/dist/claude/auto-topic-haiku.js +143 -0
  67. package/dist/claude/available-commands.js +156 -0
  68. package/dist/claude/background-hint.js +91 -0
  69. package/dist/claude/branch-tree.js +393 -0
  70. package/dist/claude/carry-over.js +25 -0
  71. package/dist/claude/ccr-shim.js +48 -0
  72. package/dist/claude/charter-judge.js +214 -0
  73. package/dist/claude/claude-auth.js +117 -0
  74. package/dist/claude/command-parser.js +206 -0
  75. package/dist/claude/cost-overlay-scraper.js +134 -0
  76. package/dist/claude/effort-level.js +5 -0
  77. package/dist/claude/enabled-plugins.js +52 -0
  78. package/dist/claude/in-flight-tracker.js +82 -0
  79. package/dist/claude/ipc-server.js +192 -0
  80. package/dist/claude/mcp-bridge.js +481 -0
  81. package/dist/claude/memory-guard.js +400 -0
  82. package/dist/claude/message-offsets.js +88 -0
  83. package/dist/claude/modal-relay.js +817 -0
  84. package/dist/claude/model-catalog.js +102 -0
  85. package/dist/claude/monitor-relay.js +712 -0
  86. package/dist/claude/native-commands.js +40 -0
  87. package/dist/claude/pending-branches.js +93 -0
  88. package/dist/claude/pending-suggestions.js +50 -0
  89. package/dist/claude/permission-gate.js +338 -0
  90. package/dist/claude/permission-mode.js +152 -0
  91. package/dist/claude/poll-user.js +51 -0
  92. package/dist/claude/prewarm.js +70 -0
  93. package/dist/claude/project-commands.js +69 -0
  94. package/dist/claude/prompt-hold.js +124 -0
  95. package/dist/claude/prompt-suggestion-scraper.js +89 -0
  96. package/dist/claude/pty-idle-reaper.js +96 -0
  97. package/dist/claude/pty-provider.js +2499 -0
  98. package/dist/claude/pty-spawn-config.js +343 -0
  99. package/dist/claude/pty-stream.js +363 -0
  100. package/dist/claude/pty-watch.js +201 -0
  101. package/dist/claude/request-queue.js +107 -0
  102. package/dist/claude/rewind.js +111 -0
  103. package/dist/claude/scheduled-runner.js +89 -0
  104. package/dist/claude/scheduler.js +241 -0
  105. package/dist/claude/scope-guard.js +279 -0
  106. package/dist/claude/screen-marks.js +83 -0
  107. package/dist/claude/screen-render.js +96 -0
  108. package/dist/claude/secret-request.js +576 -0
  109. package/dist/claude/session-history.js +288 -0
  110. package/dist/claude/session-jsonl.js +1067 -0
  111. package/dist/claude/session-manager.js +216 -0
  112. package/dist/claude/session-move.js +70 -0
  113. package/dist/claude/session-scan.js +270 -0
  114. package/dist/claude/setup-token.js +285 -0
  115. package/dist/claude/shell-mode-gate.js +60 -0
  116. package/dist/claude/side-call-options.js +45 -0
  117. package/dist/claude/side-question.js +252 -0
  118. package/dist/claude/stage-images.js +62 -0
  119. package/dist/claude/subagent-tools.js +12 -0
  120. package/dist/claude/synthetic-ctx.js +51 -0
  121. package/dist/claude/tasks-overlay-scraper.js +217 -0
  122. package/dist/claude/thread-judge-haiku.js +88 -0
  123. package/dist/claude/tip-scraper.js +77 -0
  124. package/dist/claude/tui-modal.js +746 -0
  125. package/dist/claude/tui-state.js +456 -0
  126. package/dist/claude/turn-end.js +105 -0
  127. package/dist/claude/turn-prose.js +64 -0
  128. package/dist/claude/typed-answer.js +104 -0
  129. package/dist/claude/update-banner-relay.js +70 -0
  130. package/dist/cli/commands/doctor.js +338 -0
  131. package/dist/cli/commands/help.js +34 -0
  132. package/dist/cli/commands/init.js +126 -0
  133. package/dist/cli/commands/service.js +710 -0
  134. package/dist/cli/commands/start.js +63 -0
  135. package/dist/cli/commands/version.js +11 -0
  136. package/dist/cli/commands/watch.js +322 -0
  137. package/dist/cli/dispatch.js +84 -0
  138. package/dist/cli/init/env-file.js +99 -0
  139. package/dist/cli/init/flow.js +705 -0
  140. package/dist/cli/init/prompter.js +124 -0
  141. package/dist/cli/init/telegram.js +120 -0
  142. package/dist/cli/service/render.js +212 -0
  143. package/dist/cli.js +18 -0
  144. package/dist/config-report.js +107 -0
  145. package/dist/config.js +525 -0
  146. package/dist/index.js +443 -0
  147. package/dist/launcher.js +960 -0
  148. package/dist/node-version-check.js +27 -0
  149. package/dist/providers/ccr-health.js +119 -0
  150. package/dist/providers/ccr-introspect.js +142 -0
  151. package/dist/providers/ccr-provider.js +59 -0
  152. package/dist/providers/claude-provider.js +10 -0
  153. package/dist/providers/prefs-sync.js +121 -0
  154. package/dist/providers/project-favorites.js +74 -0
  155. package/dist/providers/provider-router.js +128 -0
  156. package/dist/providers/provider-switch.js +91 -0
  157. package/dist/providers/spawn-settings.js +45 -0
  158. package/dist/providers/types.js +8 -0
  159. package/dist/providers/user-preferences.js +322 -0
  160. package/dist/restart-when-idle.js +173 -0
  161. package/dist/startup/auto-continue.js +67 -0
  162. package/dist/startup/auto-resume.js +161 -0
  163. package/dist/startup/notices.js +120 -0
  164. package/dist/startup/shared.js +44 -0
  165. package/dist/startup/verify-token.js +31 -0
  166. package/dist/telegram/access-request.js +153 -0
  167. package/dist/telegram/action-logger.js +522 -0
  168. package/dist/telegram/admin-mention.js +107 -0
  169. package/dist/telegram/background-activity-card.js +502 -0
  170. package/dist/telegram/botname-settings.js +282 -0
  171. package/dist/telegram/button-text.js +26 -0
  172. package/dist/telegram/client-options.js +26 -0
  173. package/dist/telegram/deduplication.js +33 -0
  174. package/dist/telegram/entities.js +295 -0
  175. package/dist/telegram/force-reply-tracker.js +96 -0
  176. package/dist/telegram/lettered-options.js +87 -0
  177. package/dist/telegram/markdown.js +362 -0
  178. package/dist/telegram/message-sender.js +1019 -0
  179. package/dist/telegram/prompt-label.js +148 -0
  180. package/dist/telegram/prompt-suggestion.js +54 -0
  181. package/dist/telegram/send.js +123 -0
  182. package/dist/telegram/streaming-turn.js +103 -0
  183. package/dist/telegram/suggestions-settings.js +30 -0
  184. package/dist/telegram/table-lists.js +56 -0
  185. package/dist/telegram/task-list.js +367 -0
  186. package/dist/telegram/task-tracker.js +128 -0
  187. package/dist/telegram/terminal-renderer.js +667 -0
  188. package/dist/telegram/thread-titles.js +262 -0
  189. package/dist/telegram/threads-setup.js +117 -0
  190. package/dist/utils/admins.js +143 -0
  191. package/dist/utils/agent-timer.js +55 -0
  192. package/dist/utils/atomic-write.js +24 -0
  193. package/dist/utils/bot-icon.js +248 -0
  194. package/dist/utils/bot-token.js +77 -0
  195. package/dist/utils/bounded-map.js +55 -0
  196. package/dist/utils/build-info.js +57 -0
  197. package/dist/utils/caffeinate.js +53 -0
  198. package/dist/utils/claude-env.js +53 -0
  199. package/dist/utils/claude-version.js +48 -0
  200. package/dist/utils/config-home.js +62 -0
  201. package/dist/utils/download.js +137 -0
  202. package/dist/utils/exit-codes.js +59 -0
  203. package/dist/utils/file-type.js +149 -0
  204. package/dist/utils/format.js +78 -0
  205. package/dist/utils/home-project.js +30 -0
  206. package/dist/utils/host-stall.js +51 -0
  207. package/dist/utils/id-list.js +44 -0
  208. package/dist/utils/idle-restart.js +92 -0
  209. package/dist/utils/install-mode.js +63 -0
  210. package/dist/utils/instance-config.js +148 -0
  211. package/dist/utils/instances.js +95 -0
  212. package/dist/utils/json-store.js +140 -0
  213. package/dist/utils/keyed-settings.js +62 -0
  214. package/dist/utils/legacy-env.js +35 -0
  215. package/dist/utils/memory-pressure.js +60 -0
  216. package/dist/utils/npm-version.js +134 -0
  217. package/dist/utils/prefs-relay.js +107 -0
  218. package/dist/utils/proc-children.js +102 -0
  219. package/dist/utils/resolve-bin.js +86 -0
  220. package/dist/utils/resolve-claude-bin.js +52 -0
  221. package/dist/utils/respawn-backoff.js +28 -0
  222. package/dist/utils/sanitize.js +91 -0
  223. package/dist/utils/session-key.js +37 -0
  224. package/dist/utils/stale-launcher.js +109 -0
  225. package/dist/utils/state-dir-migration.js +92 -0
  226. package/dist/utils/systemd.js +126 -0
  227. package/dist/utils/telegram-proxy.js +72 -0
  228. package/dist/utils/uploads-dir.js +27 -0
  229. package/dist/utils/user-roster.js +275 -0
  230. package/dist/utils/verbosity.js +118 -0
  231. package/dist/utils/version-check.js +166 -0
  232. package/dist/utils/workspace-guard.js +44 -0
  233. package/dist/worker-restart.js +141 -0
  234. package/instances.json.example +41 -0
  235. package/package.json +75 -4
@@ -0,0 +1,362 @@
1
+ /**
2
+ * How far a /model or /effort choice reaches: this thread, this thread and
3
+ * new ones, every thread in this chat, or every chat on every bot.
4
+ *
5
+ * A bare command or a picker tap sets the thread it's in: the cheap case, and
6
+ * the one undone by typing the command again. The wider levels are a word on
7
+ * the command (`/model sonnet new`, `… chat`, `… all`) or a tap on the
8
+ * confirmation, which offers each level wider than the one just applied.
9
+ * That holds in the "All" view too, where Telegram opens a fresh thread for
10
+ * whatever is typed: the bare command pins that new thread (see all-view.ts).
11
+ *
12
+ * A wider level drops the narrower pins it covers, so one "everywhere" brings
13
+ * every thread back in line, branches of pinned threads included. "New threads"
14
+ * is the exception: it leaves threads that chose their own model alone.
15
+ */
16
+ import { EntityText } from '../../../telegram/entities.js';
17
+ import { editCallbackMessage } from '../../../telegram/send.js';
18
+ import { broadcastPrefsChange, siblingBotNames, } from '../../../providers/prefs-sync.js';
19
+ import { getActiveProviderName, getAvailableModels, isValidEffortLevel } from '../../../providers/provider-router.js';
20
+ import { applySpawnSetting } from '../../../providers/spawn-settings.js';
21
+ import { userPreferences, } from '../../../providers/user-preferences.js';
22
+ import { isPassthroughModelId } from '../../../claude/model-catalog.js';
23
+ import { threadDisplayName } from '../../../telegram/thread-titles.js';
24
+ import { isAdmin } from '../../../utils/admins.js';
25
+ import { config } from '../../../config.js';
26
+ import { getSessionKeyFromCtx, parseSessionKey } from '../../../utils/session-key.js';
27
+ import { EFFORT_LEVELS, PTY_RESTART_NOTE } from './shared.js';
28
+ import { isFreshAllViewThread } from './all-view.js';
29
+ /** Callback-data prefix for the widen-the-scope buttons. */
30
+ export const PREFS_SCOPE_PREFIX = 'pscope:';
31
+ /** Telegram's hard cap on callback_data. */
32
+ const CALLBACK_DATA_MAX_BYTES = 64;
33
+ const LEVEL_WORDS = {
34
+ this: 'thread',
35
+ here: 'thread',
36
+ one: 'thread',
37
+ thread: 'thread',
38
+ new: 'default',
39
+ default: 'default',
40
+ chat: 'chat',
41
+ threads: 'chat',
42
+ all: 'all',
43
+ everywhere: 'all',
44
+ };
45
+ const LEVEL_ORDER = ['thread', 'default', 'chat', 'all'];
46
+ /**
47
+ * Split a trailing level word off a command argument.
48
+ *
49
+ * `/model sonnet chat` → `{ value: 'sonnet', level: 'chat' }`. The word is
50
+ * only taken as a level when something precedes it, so `/model all` still
51
+ * reaches the "that needs a model" path and `/effort default` stays the
52
+ * effort reset it always was. No word leaves `level` undefined, for the
53
+ * caller to decide (see resolveLevel).
54
+ */
55
+ export function parseScopeArg(raw) {
56
+ const parts = raw.trim().split(/\s+/).filter(Boolean);
57
+ if (parts.length < 2)
58
+ return { value: raw.trim() };
59
+ const level = LEVEL_WORDS[parts[parts.length - 1].toLowerCase()];
60
+ if (!level)
61
+ return { value: raw.trim() };
62
+ return { value: parts.slice(0, -1).join(' '), level };
63
+ }
64
+ /** Whether `word` is a level word typed on its own, with no value before it. */
65
+ export function isBareLevelWord(word) {
66
+ return word.toLowerCase() in LEVEL_WORDS && LEVEL_WORDS[word.toLowerCase()] !== 'thread';
67
+ }
68
+ export function scopeSite(ctx) {
69
+ const keyInfo = getSessionKeyFromCtx(ctx);
70
+ return {
71
+ inThread: keyInfo?.threadId !== undefined,
72
+ allView: isFreshAllViewThread(ctx),
73
+ admin: isAdmin(ctx.from?.id),
74
+ };
75
+ }
76
+ /** " on **<bot>**" when there are siblings it could be confused with. */
77
+ function withBotName(b) {
78
+ return hasSiblings() ? b.add(' on ').bold(config.BOT_NAME) : b;
79
+ }
80
+ /** Where a level reaches, worded for where it was applied from. */
81
+ function levelPhrase(level, site) {
82
+ switch (level) {
83
+ case 'all':
84
+ return 'everywhere';
85
+ case 'chat':
86
+ return 'for every thread in this chat';
87
+ case 'default':
88
+ return site.inThread ? 'for this thread and new threads in this chat' : 'in this chat';
89
+ case 'thread':
90
+ return site.inThread ? 'in this thread' : 'in this chat';
91
+ }
92
+ }
93
+ /**
94
+ * "✅ Model set to **opus** in this thread", plus " on **TeleCoder 2**" once
95
+ * there is more than one bot it could have meant. Everywhere names no bot:
96
+ * the summary under it lists them.
97
+ */
98
+ export function prefsConfirmation(setting, displayLabel, level = 'thread', site = { inThread: true }) {
99
+ const what = setting === 'model' ? 'Model set to' : 'Effort set to';
100
+ const b = new EntityText().add(`✅ ${what} `).bold(displayLabel).add(` ${levelPhrase(level, site)}`);
101
+ return (level === 'all' ? b : withBotName(b)).build();
102
+ }
103
+ const MAX_NAMED_THREADS = 5;
104
+ function threadNames(keys) {
105
+ const names = keys.map((k) => threadDisplayName(k) ?? `thread ${parseSessionKey(k).threadId ?? k}`);
106
+ if (names.length <= MAX_NAMED_THREADS)
107
+ return names.join(', ');
108
+ return `${names.slice(0, MAX_NAMED_THREADS).join(', ')} and ${names.length - MAX_NAMED_THREADS} more`;
109
+ }
110
+ function plural(n, one, many = `${one}s`) {
111
+ return `${n} ${n === 1 ? one : many}`;
112
+ }
113
+ /**
114
+ * What a change did beyond the setting itself: pins it replaced or left
115
+ * alone, and which claudes restart when. A block that opens with a blank
116
+ * line, empty when there is nothing to say.
117
+ */
118
+ export function describeOutcome(outcome, opts) {
119
+ const b = new EntityText();
120
+ const { level, current, keptPins } = opts;
121
+ if (level === 'chat' && outcome.dropped.length > 0) {
122
+ b.add('\n\n').add(`Now following it instead of their own choice: ${threadNames(outcome.dropped)}.`);
123
+ }
124
+ else if (level === 'all' && outcome.dropped.length > 0) {
125
+ b.add('\n\n').add(`${plural(outcome.dropped.length, 'thread or chat', 'threads and chats')} that had chosen their own follow it now.`);
126
+ }
127
+ if (keptPins.length > 0) {
128
+ b.add('\n\n').add(`${plural(keptPins.length, 'thread')} chose their own and keep it: ${threadNames(keptPins)}. `)
129
+ .bold('All threads here').add(' sets those too.');
130
+ }
131
+ const others = outcome.restarted.filter((k) => k !== current);
132
+ if (outcome.restarted.includes(current) && others.length === 0) {
133
+ b.append(PTY_RESTART_NOTE);
134
+ }
135
+ else if (outcome.restarted.length > 0) {
136
+ b.add('\n\n').italic(`Claude Code restarts in ${plural(outcome.restarted.length, 'thread')} on the next message there (the conversations are kept).`);
137
+ }
138
+ if (outcome.busy.length > 0) {
139
+ b.add('\n\n').italic(`${plural(outcome.busy.length, 'thread is', 'threads are')} mid-turn and switch${outcome.busy.length === 1 ? 'es' : ''} when that turn ends.`);
140
+ }
141
+ return b.build();
142
+ }
143
+ /**
144
+ * Buttons for every level wider than the one just applied that means
145
+ * something here, plus, once a wider level was applied from the "All" view,
146
+ * a way to remove the empty thread it opened. Undefined when there is
147
+ * nothing to offer.
148
+ */
149
+ export function buildWidenKeyboard(setting, buttonValue, level, site) {
150
+ const wider = LEVEL_ORDER.slice(LEVEL_ORDER.indexOf(level) + 1);
151
+ const buttons = [];
152
+ const offer = (l, text) => {
153
+ const data = `${PREFS_SCOPE_PREFIX}${l}:${setting}:${buttonValue}`;
154
+ if (Buffer.byteLength(data, 'utf8') > CALLBACK_DATA_MAX_BYTES)
155
+ return;
156
+ buttons.push({ text, callback_data: data });
157
+ };
158
+ if (wider.includes('default') && site.inThread)
159
+ offer('default', '🆕 New threads too');
160
+ if (wider.includes('chat') && site.inThread)
161
+ offer('chat', '🧵 All threads here');
162
+ if (wider.includes('all') && site.admin) {
163
+ const bots = siblingBotNames().length + 1;
164
+ offer('all', bots > 1 ? `🌐 Everywhere (${bots} bots)` : '🌐 Every chat');
165
+ }
166
+ const rows = [];
167
+ for (let i = 0; i < buttons.length; i += 2)
168
+ rows.push(buttons.slice(i, i + 2));
169
+ // A thread-level pick there is the thread being started on that model;
170
+ // anything wider left it with nothing to do.
171
+ if (site.allView && level !== 'thread')
172
+ rows.push([{ text: '🗑️ Delete this thread', callback_data: 'threaddelete:confirm' }]);
173
+ return rows.length > 0 ? rows : undefined;
174
+ }
175
+ /**
176
+ * Apply a choice at a level from the session the user is in, and build the
177
+ * confirmation and buttons for it. The caller adds anything specific to the
178
+ * setting (the premium note, the unverified-model warning) and sends it.
179
+ */
180
+ export async function applyAtLevel(ctx, args) {
181
+ const { sessionKey, setting, value, label, level, site } = args;
182
+ const chatId = parseSessionKey(sessionKey).chatId;
183
+ const outcome = applySpawnSetting(sessionKey, setting, value, level, sessionKey);
184
+ const keptPins = level === 'default' && site.inThread
185
+ ? userPreferences.threadsPinnedOtherThan(chatId, setting, value).filter((k) => k !== sessionKey)
186
+ : [];
187
+ const message = new EntityText()
188
+ .append(prefsConfirmation(setting, label, level, site))
189
+ .append(describeOutcome(outcome, { level, current: sessionKey, keptPins }));
190
+ if (level === 'all') {
191
+ message.append(await applyToAllBots({
192
+ chatId,
193
+ setting,
194
+ value,
195
+ provider: getActiveProviderName(sessionKey),
196
+ level: 'all',
197
+ }));
198
+ }
199
+ const buttonValue = value ?? 'auto';
200
+ return { message, keyboard: buildWidenKeyboard(setting, buttonValue, level, site) };
201
+ }
202
+ /** The refusal for a non-admin asking for every chat. */
203
+ export const EVERYWHERE_ADMIN_ONLY = 'Only an admin can set every chat. Use chat to set every thread here.';
204
+ /**
205
+ * Relay a change to the other instances and describe what happened, as a
206
+ * block (opening with a blank line) to append to the local confirmation.
207
+ * Says nothing in a single-bot setup, where "everywhere" is this bot.
208
+ */
209
+ export async function applyToAllBots(change) {
210
+ const result = await broadcastPrefsChange(change);
211
+ return formatBroadcastResult(result);
212
+ }
213
+ /** Render a broadcast outcome. Exported for tests. */
214
+ export function formatBroadcastResult(result) {
215
+ const nothing = new EntityText().build();
216
+ if (!result.multiInstance)
217
+ return nothing;
218
+ if (result.timedOut) {
219
+ return new EntityText()
220
+ .add('\n\n⚠️ The launcher didn\'t answer, so the other bots were ')
221
+ .bold('not')
222
+ .add(' changed. It may be running code from before fleet-wide settings existed — /botrestart all picks it up.')
223
+ .build();
224
+ }
225
+ // A launcher with a one-instance config: multi-instance plumbing, nobody on
226
+ // the other end of it.
227
+ if (result.applied.length + result.skipped.length + result.unreachable.length === 0) {
228
+ return nothing;
229
+ }
230
+ // Each line opens with its own newline; this one more sets the block off
231
+ // from the confirmation with a blank line.
232
+ const b = new EntityText().add('\n');
233
+ if (result.applied.length > 0) {
234
+ const names = result.applied.map((o) => o.name).join(', ');
235
+ b.add('\n🌐 Also applied to ')
236
+ .bold(String(result.applied.length))
237
+ .add(` other bot${result.applied.length === 1 ? '' : 's'}: ${names}`);
238
+ // A busy bot switches when its turn ends; say so rather than let the
239
+ // reply it's writing now look like the setting didn't take.
240
+ const busy = result.applied.filter((o) => (o.busy ?? 0) > 0);
241
+ if (busy.length > 0) {
242
+ b.add('\n').italic(`${busy.map((o) => o.name).join(', ')} ${busy.length === 1 ? 'is' : 'are'} mid-turn — ` +
243
+ 'switching when that turn ends.');
244
+ }
245
+ }
246
+ else {
247
+ b.add('\n🌐 No other bot took the change.');
248
+ }
249
+ for (const o of result.skipped) {
250
+ b.add('\n⚠️ Not applied to ').bold(o.name).add(`${o.reason ? ` — ${o.reason}` : ''}.`);
251
+ }
252
+ if (result.unreachable.length > 0) {
253
+ b.add(`\n⚠️ Not running, so unchanged: ${result.unreachable.join(', ')}.`);
254
+ }
255
+ return b.build();
256
+ }
257
+ /**
258
+ * Whether a confirmation should name the bot it applied to. Pointless when
259
+ * there is only one.
260
+ */
261
+ export function hasSiblings() {
262
+ return siblingBotNames().length > 0;
263
+ }
264
+ /**
265
+ * Where a scope's model or effort comes from, as the pickers and /info say
266
+ * it: "pinned in this thread", "this chat's default", "the default
267
+ * everywhere", or Claude Code's own default when nothing chose.
268
+ */
269
+ export function describeSource(scope, setting) {
270
+ const source = userPreferences.sourceOf(scope, setting);
271
+ const inThread = parseSessionKey(String(scope)).threadId !== undefined;
272
+ switch (source) {
273
+ case 'thread':
274
+ return 'pinned in this thread';
275
+ case 'chat':
276
+ return inThread ? "this chat's default" : 'set for this chat';
277
+ case 'bot':
278
+ return 'the default everywhere';
279
+ default:
280
+ return "Claude Code's default";
281
+ }
282
+ }
283
+ /** The picker's closing note on how far a choice reaches. */
284
+ export function levelHelp(command, example, site) {
285
+ const b = new EntityText().add('\n\n');
286
+ if (site.inThread) {
287
+ b.add('Picking sets this thread; the confirmation can widen it to new threads, every thread here');
288
+ b.add(site.admin ? ' or everywhere.' : '.');
289
+ }
290
+ else {
291
+ // Without threads, new and chat are this chat again: only all says more.
292
+ b.add('Picking sets this chat');
293
+ if (!site.admin)
294
+ return b.add('.').build();
295
+ return b.add('; ').code(`/${command} ${example} all`).add(' sets every chat.').build();
296
+ }
297
+ b.add(' Typed: ').code(`/${command} ${example} new`).add(', ').code('chat');
298
+ if (site.admin)
299
+ b.add(' or ').code('all');
300
+ return b.add('.').build();
301
+ }
302
+ /** Human-readable name for a value, matching what the pickers show. */
303
+ async function describeValue(scope, setting, value) {
304
+ if (setting === 'effort') {
305
+ if (value === null)
306
+ return 'Auto (CLI default)';
307
+ return EFFORT_LEVELS.find((l) => l.id === value)?.label ?? value;
308
+ }
309
+ if (value === null)
310
+ return 'default';
311
+ const models = await getAvailableModels(scope);
312
+ return models.find((m) => m.id === value)?.label ?? value;
313
+ }
314
+ /**
315
+ * A widen button under a /model or /effort confirmation. The narrower
316
+ * change already happened when the message was sent; this applies the wider
317
+ * one and rewrites the message with what it did.
318
+ */
319
+ export async function handlePrefsScopeCallback(ctx) {
320
+ const keyInfo = getSessionKeyFromCtx(ctx);
321
+ const data = ctx.callbackQuery?.data;
322
+ if (!keyInfo || !data)
323
+ return;
324
+ const { sessionKey } = keyInfo;
325
+ if (!data.startsWith(PREFS_SCOPE_PREFIX))
326
+ return;
327
+ const [word, setting, ...rest] = data.slice(PREFS_SCOPE_PREFIX.length).split(':');
328
+ if (!LEVEL_ORDER.includes(word) || rest.length === 0)
329
+ return;
330
+ const level = word;
331
+ const raw = rest.join(':');
332
+ if (setting !== 'model' && setting !== 'effort')
333
+ return;
334
+ // 'auto' is how the effort picker spells "no preference"; a model has no
335
+ // such spelling.
336
+ const value = setting === 'effort' && raw === 'auto' ? null : raw;
337
+ const valid = setting === 'effort'
338
+ ? value === null || isValidEffortLevel(value)
339
+ : (await getAvailableModels(sessionKey)).some((m) => m.id === raw) || isPassthroughModelId(raw);
340
+ if (!valid) {
341
+ await ctx.answerCallbackQuery({ text: 'Unknown value' });
342
+ return;
343
+ }
344
+ const site = scopeSite(ctx);
345
+ if (level === 'all' && !site.admin) {
346
+ await ctx.answerCallbackQuery({ text: EVERYWHERE_ADMIN_ONLY, show_alert: true });
347
+ return;
348
+ }
349
+ await ctx.answerCallbackQuery({ text: level === 'all' ? 'Applying everywhere…' : 'Applying…' });
350
+ // Drop the buttons first: an everywhere change takes a round trip, and a
351
+ // second tap would apply it again.
352
+ try {
353
+ await ctx.editMessageReplyMarkup({ reply_markup: { inline_keyboard: [] } });
354
+ }
355
+ catch {
356
+ // ignore — the message may already have been edited
357
+ }
358
+ const label = await describeValue(sessionKey, setting, value);
359
+ const { message, keyboard } = await applyAtLevel(ctx, { sessionKey, setting, value, label, level, site });
360
+ await editCallbackMessage(ctx, message.build(), keyboard ? { reply_markup: { inline_keyboard: keyboard } } : {});
361
+ }
362
+ //# sourceMappingURL=prefs-scope.js.map