dsh-tg-bridge 1.41.2 → 1.43.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.
@@ -13,6 +13,7 @@ export const CONTROL_COMMANDS = [
13
13
  'prices', 'cost', 'help', 'doctor', 'logs', 'report', 'lang', 'reasoning', 'pin', 'goal', 'task', 'tasks',
14
14
  'thoughts', 'version', 'update', 'crew', 'agentset', 'subscribe', 'settings',
15
15
  'rewind', 'fork', 'branches', 'menu', 'fallback', 'load', 'diff', 'endpoint', 'dsh', 'compact', 'card', 'find', 'start',
16
+ 'ls', 'view',
16
17
  ];
17
18
 
18
19
  /**
package/chat/control.mjs CHANGED
@@ -34,13 +34,14 @@ import { handleFallbackCommands } from '../control/cmd-fallback.mjs';
34
34
  import { handleDiffCommands } from '../control/cmd-diff.mjs';
35
35
  import { handleLoadCommands } from '../control/cmd-load.mjs';
36
36
  import { handlePluginCommands } from '../control/cmd-plugin.mjs';
37
- import { handleSessionCommands } from '../control/cmd-session.mjs';
37
+ import { handleSessionCommands } from '../control/cmd-session.mjs';
38
38
  import { handleStartCommands } from '../control/cmd-start.mjs';
39
39
  import { handleSkillCommands } from '../control/cmd-skills.mjs';
40
40
  import { handleTaskCommands } from '../control/cmd-task.mjs';
41
41
  import { handleViewCommands } from '../control/cmd-views.mjs';
42
42
  import { handleEndpointCommands } from '../control/cmd-endpoint.mjs';
43
43
  import { handleEveryCommands } from '../control/cmd-every.mjs';
44
+ import { handleFileCommands } from '../control/cmd-files.mjs';
44
45
  import { handleFindCommands } from '../control/cmd-find.mjs';
45
46
  import { handleDshCommands } from '../control/cmd-dsh.mjs';
46
47
  import { handleCompactCommands } from '../control/cmd-compact.mjs';
@@ -115,7 +116,7 @@ export async function handleControl(ctx) {
115
116
  // that is not its job. The order is alphabetical, not meaningful: no two
116
117
  // groups claim the same command, so a miss falls through to `default` below.
117
118
  const child = { ...ctx, effective, command, arg: effectiveArgText, send, reply, log, profile, state, key, config, lang };
118
- for (const handler of [handleAgentCommands, handleBridgeCommands, handleCardCommands, handleCompactCommands, handleDiffCommands, handleEveryCommands, handleDshCommands, handleFallbackCommands, handleFindCommands, handleLoadCommands, handlePluginCommands, handleSessionCommands, handleSkillCommands, handleStartCommands, handleTaskCommands, handleViewCommands, handleEndpointCommands]) {
119
+ for (const handler of [handleAgentCommands, handleBridgeCommands, handleCardCommands, handleCompactCommands, handleDiffCommands, handleEveryCommands, handleDshCommands, handleFallbackCommands, handleFileCommands, handleFindCommands, handleLoadCommands, handlePluginCommands, handleSessionCommands, handleSkillCommands, handleStartCommands, handleTaskCommands, handleViewCommands, handleEndpointCommands]) {
119
120
  if (await handler(child)) return true;
120
121
  }
121
122
  // Nothing claimed it: the bridge answers an unknown `/` command itself, with
@@ -0,0 +1,59 @@
1
+ // One folder of the topic as rows of buttons.
2
+ //
3
+ // `/ls` lists a folder and `/view` opens a file, but the press that walks into
4
+ // the next folder lives in the callback router — so the drawing and the reading
5
+ // sit here, in `chat/`, and both sides import the same functions instead of one
6
+ // importing the other.
7
+ import { readdirSync } from 'node:fs';
8
+ import { resolveInRoot } from '../lib/file-fence.mjs';
9
+ import { t } from '../core/i18n.mjs';
10
+
11
+ /** Rows one folder card shows; more than this and the answer says how many. */
12
+ export const LIST_CAP = 40;
13
+
14
+ /** Folders a listing skips: real, but noise for a person reading from a phone. */
15
+ const SKIP = new Set(['node_modules', '.git']);
16
+
17
+ /**
18
+ * One folder as rows: folders first, then files, both alphabetically.
19
+ *
20
+ * @returns {{ refusal: string } | { rows: {name: string, dir: boolean}[],
21
+ * total: number, dirs: number, files: number }}
22
+ */
23
+ export function listDir(root, rel) {
24
+ const fenced = resolveInRoot(root, rel || '.');
25
+ if (fenced.refusal) return { refusal: fenced.refusal };
26
+ if (!fenced.dir) return { refusal: 'notDir' };
27
+ let names;
28
+ try {
29
+ names = readdirSync(fenced.path, { withFileTypes: true });
30
+ } catch {
31
+ return { refusal: 'missing' };
32
+ }
33
+ const rows = names
34
+ .filter((row) => !SKIP.has(row.name))
35
+ .map((row) => ({ name: row.name, dir: row.isDirectory() }))
36
+ .sort((a, b) => (a.dir !== b.dir ? (a.dir ? -1 : 1) : a.name.localeCompare(b.name)));
37
+ const dirs = rows.filter((row) => row.dir).length;
38
+ return { rows, total: rows.length, dirs, files: rows.length - dirs };
39
+ }
40
+
41
+ /** Text and buttons for one folder card; the rows are already capped. */
42
+ export function listingCard(rel, result, lang) {
43
+ const lines = [t(lang, 'ctrl.ls.head', { path: rel || '.', dirs: result.dirs, files: result.files })];
44
+ const shown = result.rows.slice(0, LIST_CAP);
45
+ if (result.total > shown.length) lines.push(t(lang, 'ctrl.ls.cap', { shown: shown.length, total: result.total }));
46
+ if (shown.length === 0) lines.push(t(lang, 'ctrl.ls.emptyDir'));
47
+ return {
48
+ text: lines.join('\n'),
49
+ keyboard: shown.map((row, index) => [{
50
+ text: row.dir ? `${row.name}/` : row.name,
51
+ // The button carries the row number, not the path: callback data is capped
52
+ // at 64 bytes and a path is longer than that. The listing the card was
53
+ // drawn from is kept in the state, so the number still means the same row
54
+ // until the next listing replaces it — and a replaced one answers "stale"
55
+ // instead of opening whatever moved into that place.
56
+ data: `fv:${index}`,
57
+ }]),
58
+ };
59
+ }
@@ -1,19 +1,24 @@
1
- // A `/find` result pressed: open the file at the matched line, and page through it.
1
+ // A `/find` or `/ls` result pressed: open the file at the matched line, walk
2
+ // into a folder, and page through either.
2
3
  //
3
4
  // Kept out of the callback router because the router was already at the size
4
5
  // ceiling, and because this is the one place a press reads a file from disk: the
5
6
  // fence, the preview and the "more" button belong together.
6
7
  import { previewWindow } from './file-preview.mjs';
8
+ import { listDir, listingCard } from './file-listing.mjs';
7
9
  import { t } from '../core/i18n.mjs';
8
10
  import { saveState, state } from '../core/state.mjs';
9
11
  import { inlineKeyboard, tg } from './telegram.mjs';
10
12
 
11
13
  /**
12
- * Handle one `fs:` press.
14
+ * Handle one `fs:` or `fv:` press.
13
15
  *
14
16
  * @returns {Promise<boolean>} true when the press was this module's to answer.
15
17
  */
16
18
  export async function handleFilePress({ data, key, lang, chatId, threadId, messageId, answer }) {
19
+ if (String(data || '').startsWith('fv:')) {
20
+ return pressEntry({ data: String(data).slice(3), key, lang, chatId, threadId, messageId, answer });
21
+ }
17
22
  if (!String(data || '').startsWith('fs:')) return false;
18
23
  const body = String(data).slice(3);
19
24
  if (body.startsWith('more:')) {
@@ -64,4 +69,51 @@ export async function handleFilePress({ data, key, lang, chatId, threadId, messa
64
69
  }).catch(() => {});
65
70
  await answer(t(lang, 'ctrl.find.opened', { line: hit.line }));
66
71
  return true;
72
+ }
73
+
74
+ /** A `/ls` row pressed: walk into a folder, or open the file it named. */
75
+ async function pressEntry({ data, key, lang, chatId, threadId, messageId, answer }) {
76
+ const cur = state.listing?.[key];
77
+ const row = cur?.rows?.[Number(data)];
78
+ if (!row) {
79
+ await answer(t(lang, 'ctrl.ls.stale'));
80
+ return true;
81
+ }
82
+ const rel = cur.dir && cur.dir !== '.' ? `${cur.dir}/${row.name}` : row.name;
83
+ if (row.dir) {
84
+ const result = listDir(cur.root, rel);
85
+ if (result.refusal) {
86
+ await answer(t(lang, `ctrl.find.${result.refusal}`, { root: cur.root }));
87
+ return true;
88
+ }
89
+ state.listing[key] = { root: cur.root, dir: rel, rows: result.rows, at: Date.now() };
90
+ saveState();
91
+ const card = listingCard(rel, result, lang);
92
+ // The folder replaces the card it was reached from, so walking back is one
93
+ // `/ls ..` rather than a growing stack of messages in the chat.
94
+ await tg('editMessageText', {
95
+ chat_id: chatId,
96
+ message_id: messageId,
97
+ text: card.text,
98
+ reply_markup: { inline_keyboard: inlineKeyboard(card.keyboard) },
99
+ }).catch(() => {});
100
+ await answer(t(lang, 'ctrl.ls.entered', { dir: row.name }));
101
+ return true;
102
+ }
103
+ const view = previewWindow({ root: cur.root, file: rel, line: 1, lang });
104
+ if (view.refusal) {
105
+ await answer(t(lang, `ctrl.find.file.${view.refusal}`));
106
+ return true;
107
+ }
108
+ state.preview = state.preview || {};
109
+ state.preview[key] = { root: cur.root, file: rel, line: 1, start: 0 };
110
+ saveState();
111
+ await tg('sendMessage', {
112
+ chat_id: chatId,
113
+ ...(threadId > 0 ? { message_thread_id: threadId } : {}),
114
+ text: view.text,
115
+ reply_markup: { inline_keyboard: inlineKeyboard(view.keyboard) },
116
+ }).catch(() => {});
117
+ await answer(t(lang, 'ctrl.ls.opened'));
118
+ return true;
67
119
  }
@@ -10,6 +10,7 @@ import { t } from '../core/i18n.mjs';
10
10
  /** The /help group each command is listed under. */
11
11
  const SECTION = {
12
12
  start: 'Other', cd: 'Work', new: 'Work', stop: 'Work', send: 'Work', seed: 'Work', diff: 'Work', find: 'Work',
13
+ ls: 'Work', view: 'Work',
13
14
  goal: 'Goals', task: 'Goals', tasks: 'Goals',
14
15
  status: 'Sessions', sessions: 'Sessions', rewind: 'Sessions', profile: 'Sessions',
15
16
  agents: 'Agents', model: 'Models', prices: 'Models', cost: 'Models', thoughts: 'Models', fallback: 'Models', endpoint: 'Models',
@@ -15,7 +15,7 @@ import { CONTROL_COMMANDS, DOCUMENTED_COMMANDS } from './commands-registry.mjs';
15
15
  * a panel that hides half the bot sends people back to typing.
16
16
  */
17
17
  const SECTIONS = {
18
- work: ['cd', 'seed', 'send', 'diff'],
18
+ work: ['cd', 'ls', 'view', 'seed', 'send', 'diff'],
19
19
  session: ['sessions', 'rewind', 'profile', 'agents'],
20
20
  goals: ['goal', 'task', 'tasks', 'plan', 'jobs', 'remind', 'every'],
21
21
  talk: ['effort', 'reasoning', 'pin', 'thoughts', 'prices', 'cost', 'lang', 'endpoint', 'fallback'],
@@ -0,0 +1,81 @@
1
+ // `/ls` and `/view`: see what the topic folder holds, from the phone.
2
+ //
3
+ // `/find` answers "where is this line", but a person who only wants to look
4
+ // around had no way to. `/ls` lists one folder as buttons, `/view` opens a file
5
+ // with line numbers, and both reuse the same fence and the same paged window the
6
+ // search results already use, so the rules learned once still hold: nothing
7
+ // outside the topic folder, nothing binary, nothing over 512 KB.
8
+ import { resolveInRoot } from '../lib/file-fence.mjs';
9
+ import { t } from '../core/i18n.mjs';
10
+ import { previewWindow } from '../chat/file-preview.mjs';
11
+ import { listDir, listingCard } from '../chat/file-listing.mjs';
12
+ import { saveState as saveStateNow, state as liveState } from '../core/state.mjs';
13
+
14
+ /** The folder this topic itself is bound to — no install-wide fallback. */
15
+ function ownRoot(st, key) {
16
+ return (st?.cwd && st.cwd[key]) || '';
17
+ }
18
+
19
+ /** Open one file with line numbers and remember the window for the "more" button. */
20
+ export async function openFile(ctx, root, file, line = 1) {
21
+ const { key, lang, reply } = ctx;
22
+ const view = previewWindow({ root, file, line, lang });
23
+ if (view.refusal) {
24
+ await reply(t(lang, `ctrl.find.file.${view.refusal}`));
25
+ return true;
26
+ }
27
+ const st = ctx.state || liveState;
28
+ st.preview = st.preview || {};
29
+ st.preview[key] = { root, file, line, start: 0 };
30
+ (ctx.saveState || saveStateNow)();
31
+ await reply(view.text, { keyboard: view.keyboard });
32
+ return true;
33
+ }
34
+
35
+ /** One `/ls` or `/view` command, or false when it is neither. */
36
+ export async function handleFileCommands(ctx) {
37
+ const { effective, arg, state: st, key, lang, reply } = ctx;
38
+ if (effective !== '/ls' && effective !== '/view') return false;
39
+ const root = ownRoot(st, key);
40
+ if (!root) {
41
+ await reply(t(lang, 'ctrl.find.empty'));
42
+ return true;
43
+ }
44
+ if (effective === '/view') {
45
+ const asked = String(arg || '').trim();
46
+ if (!asked) {
47
+ await reply(t(lang, 'ctrl.view.usage'));
48
+ return true;
49
+ }
50
+ // `file:412` and `file 412` both name a line: a path copied out of another
51
+ // app arrives with the colon, and a path typed on a phone arrives with a
52
+ // space, so both have to work.
53
+ const cut = asked.match(/^(.*?):(\d+)$/);
54
+ const spaced = cut ? null : asked.match(/^(.*?)\s+(\d+)$/);
55
+ const file = (cut ? cut[1] : (spaced ? spaced[1] : asked)).trim();
56
+ const line = Math.max(1, Number((cut || spaced || [])[2]) || 1);
57
+ const fenced = resolveInRoot(root, file);
58
+ if (fenced.refusal) {
59
+ await reply(t(lang, `ctrl.find.${fenced.refusal}`, { root }));
60
+ return true;
61
+ }
62
+ return openFile(ctx, root, file, line);
63
+ }
64
+ const rel = String(arg || '').trim();
65
+ const result = listDir(root, rel);
66
+ if (result.refusal === 'notDir') {
67
+ // A file named where a folder was expected: reading it is what they meant.
68
+ return openFile(ctx, root, rel, 1);
69
+ }
70
+ if (result.refusal) {
71
+ await reply(t(lang, `ctrl.find.${result.refusal}`, { root }));
72
+ return true;
73
+ }
74
+ const st2 = ctx.state || liveState;
75
+ st2.listing = st2.listing || {};
76
+ st2.listing[key] = { root, dir: rel || '.', rows: result.rows, at: Date.now() };
77
+ (ctx.saveState || saveStateNow)();
78
+ const card = listingCard(rel, result, lang);
79
+ await reply(card.text, { keyboard: card.keyboard });
80
+ return true;
81
+ }