sumo-agents 0.1.0 → 0.4.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.
package/src/editor.mjs CHANGED
@@ -4,8 +4,15 @@
4
4
  * function from one state to the next, so the screen only has to draw it.
5
5
  */
6
6
 
7
- /** An empty box. `history` is what Up recalls, oldest first; `at` is the entry shown, one past the end for the text being written. */
8
- export const editor = (history = []) => ({ text: '', cursor: 0, history, at: history.length, draft: '', pick: 0 });
7
+ /**
8
+ * An empty box. `history` is what Up recalls, oldest first; `at` is the entry shown, one past the end for the text being written.
9
+ * `undo` is what the text was before each change, newest last; `kill` is the last thing cut, for Ctrl-Y; `search`, while Ctrl-R
10
+ * looks back through history, what has been typed to find and where it was found.
11
+ */
12
+ export const editor = (history = [], kill = '') => ({ text: '', cursor: 0, history, at: history.length, draft: '', pick: 0, undo: [], kill, search: null });
13
+
14
+ /** How many changes Ctrl-_ can take back. */
15
+ const UNDO_KEPT = 100;
9
16
 
10
17
  /**
11
18
  * The menu for what is being typed: the commands a half-typed slash could mean, or, once a command's
@@ -13,8 +20,28 @@ export const editor = (history = []) => ({ text: '', cursor: 0, history, at: his
13
20
  * command that offers none. Each entry says what to show (`label`), its hint, and the whole text
14
21
  * picking it leaves in the box (`complete`).
15
22
  */
23
+ /** How well a few typed letters name a command: from its start best, then anywhere in it, then in order with gaps between; -1 when they do not. */
24
+ function nameRank(name, typed) {
25
+ if (name.startsWith(typed)) return 0;
26
+ if (name.includes(typed)) return 1;
27
+ let at = 0;
28
+ for (const ch of typed) {
29
+ at = name.indexOf(ch, at) + 1;
30
+ if (at === 0) return -1;
31
+ }
32
+ return 2;
33
+ }
34
+
16
35
  export function menuFor(text, commands) {
17
- if (/^\/\w*$/.test(text)) return commands.filter((c) => c.name.startsWith(text.slice(1))).map((c) => ({ name: c.name, label: `/${c.name}`, hint: c.hint, complete: `/${c.name} ` }));
36
+ if (/^\/\w*$/.test(text)) {
37
+ // A command is found by any of its letters, in order: /rsm is /resume. The ones it starts with come first, in the menu's own order.
38
+ const typed = text.slice(1);
39
+ return commands
40
+ .map((c, i) => [nameRank(c.name, typed), i, c])
41
+ .filter(([rank]) => rank !== -1)
42
+ .sort((a, b) => a[0] - b[0] || a[1] - b[1])
43
+ .map(([, , c]) => ({ name: c.name, label: `/${c.name}`, hint: c.hint, complete: `/${c.name} ` }));
44
+ }
18
45
  const m = /^\/(\w+)\s+([^\n]*)$/.exec(text);
19
46
  const command = m && commands.find((c) => c.name === m[1] && c.choices);
20
47
  if (!command) return [];
@@ -89,7 +116,56 @@ const insert = (state, chunk) => edit(state, state.text.slice(0, state.cursor) +
89
116
  const cut = (state, from, to) => edit(state, state.text.slice(0, from) + state.text.slice(to), from);
90
117
 
91
118
  /** Pasted text goes in whole, its line ends made plain; a newline in it is never Enter. */
92
- export const paste = (state, text) => insert(state, text.replace(/\r\n?/g, '\n'));
119
+ export const paste = (state, text) => remember(state, insert(state, text.replace(/\r\n?/g, '\n')));
120
+
121
+ /**
122
+ * Characters nobody sees that change what text says: direction overrides, zero-width spaces, the invisible tag letters that
123
+ * can carry a hidden instruction. The joiner and the selectors an emoji is built from are not among them.
124
+ */
125
+ const INVISIBLE = /[\u200b\u200c\u200e\u200f\u202a-\u202e\u2060-\u2064\u2066-\u2069\ufeff\u{e0000}-\u{e007f}]/gu;
126
+
127
+ /** Text from the clipboard as the box takes it: without the characters nobody sees, and how many were taken out. */
128
+ export function cleanPaste(text) {
129
+ const removed = String(text).match(INVISIBLE)?.length ?? 0;
130
+ return { text: removed ? String(text).replace(INVISIBLE, '') : String(text), removed };
131
+ }
132
+
133
+ /** A marker standing for something put in the box — an image, a long paste — ending at the cursor: deleted whole, never a character of it. */
134
+ const MARKER_BEFORE = /\[(?:Image #\d+|Pasted text #\d+[^\]\n]*)\] ?$/;
135
+
136
+ /** The state after a change, carrying what the text was before it so Ctrl-_ can take it back. A run of letters typed is one change. */
137
+ function remember(before, after, typing = false) {
138
+ if (after.text === before.text) return after;
139
+ const run = typing && before.typing;
140
+ const undo = run ? before.undo : [...before.undo, { text: before.text, cursor: before.cursor }].slice(-UNDO_KEPT);
141
+ return { ...after, undo, typing };
142
+ }
143
+
144
+ /** What was cut is kept for Ctrl-Y as well as gone from the box. */
145
+ const killed = (state, from, to) => ({ ...cut(state, from, to), kill: state.text.slice(from, to) || state.kill });
146
+
147
+ /** Ctrl-R: the newest line sent before `below` that holds `query`, put in the box with the cursor where it was found. */
148
+ function find(state, query, below, further = false) {
149
+ const { search } = state;
150
+ for (let i = Math.min(below, state.history.length) - 1; i >= 0; i--) {
151
+ const at = query ? state.history[i].toLowerCase().indexOf(query.toLowerCase()) : -1;
152
+ if (at !== -1) return { ...state, text: state.history[i], cursor: at, search: { ...search, query, at: i, failed: false, end: false } };
153
+ }
154
+ // Nothing further back keeps what was found; nothing at all says so.
155
+ if (further && search.at !== null) return { ...state, search: { ...search, end: true } };
156
+ return { ...state, search: { ...search, query, at: null, failed: query !== '', end: false } };
157
+ }
158
+
159
+ /** A key while Ctrl-R looks back: typing narrows it, Ctrl-R goes further back, Enter keeps what was found, Esc puts the draft back. */
160
+ function searching(state, input, key) {
161
+ const { search } = state;
162
+ if (key.escape || (key.ctrl && (input === 'g' || input === 'c'))) return { ...state, text: search.draft.text, cursor: search.draft.cursor, search: null };
163
+ if (key.ctrl && input === 'r') return find(state, search.query, search.at ?? state.history.length, true);
164
+ if (key.backspace) return find(state, search.query.slice(0, -1), state.history.length);
165
+ if (input && !key.ctrl && !key.meta && !key.return && !key.tab) return find(state, search.query + input, state.history.length);
166
+ // Anything else keeps what was found in the box, to send or to edit.
167
+ return { ...state, at: state.history.length, search: null };
168
+ }
93
169
 
94
170
  const BURST_KEYS = { '\r': ['\r', { return: true }], '\t': ['', { tab: true }], '\x7f': ['', { backspace: true }] };
95
171
  /** A control character is Ctrl and a letter: 3 is Ctrl-C. */
@@ -141,7 +217,7 @@ function send(state) {
141
217
  const text = state.text.trim();
142
218
  if (!text) return { state: edit(state, '', 0) };
143
219
  const history = state.history.at(-1) === text ? state.history : [...state.history, text];
144
- return { state: editor(history), submit: text };
220
+ return { state: editor(history, state.kill), submit: text };
145
221
  }
146
222
 
147
223
  /**
@@ -150,6 +226,22 @@ function send(state) {
150
226
  * sent the text.
151
227
  */
152
228
  export function press(state, input, key, commands = [], files = null) {
229
+ if (state.search) return { state: searching(state, input, key) };
230
+ if (key.ctrl && input === 'r') return { state: find({ ...state, search: { query: '', at: null, failed: false, draft: { text: state.text, cursor: state.cursor } } }, '', state.history.length) };
231
+ // Ctrl-_ (Ctrl-/ on most keyboards): the text as it was before the last change.
232
+ if (input === '\x1f') {
233
+ const last = state.undo.at(-1);
234
+ return { state: last ? { ...state, text: last.text, cursor: last.cursor, undo: state.undo.slice(0, -1), typing: false, pick: 0 } : state };
235
+ }
236
+ const pressed = keyed(state, input, key, commands, files);
237
+ const typing = !key.ctrl && !key.meta && input.length === 1 && /\S/.test(input) && !key.return && !key.tab;
238
+ // Sent, the box starts afresh: there is nothing to take back into it. A key that changed nothing hands back the same state:
239
+ // the screen reads that as Down having left the box.
240
+ if (pressed.submit !== undefined || pressed.state === state) return pressed;
241
+ return { ...pressed, state: { ...remember(state, pressed.state, typing), typing } };
242
+ }
243
+
244
+ function keyed(state, input, key, commands, files) {
153
245
  const menu = menuOf(state, commands, files);
154
246
  const { text, cursor } = state;
155
247
  const word = key.meta || key.ctrl;
@@ -171,7 +263,11 @@ export function press(state, input, key, commands = [], files = null) {
171
263
  if (key.rightArrow) return { state: { ...state, cursor: word ? wordRight(state) : cursor + under(state).length } };
172
264
  if (key.home) return { state: { ...state, cursor: lineStart(state) } };
173
265
  if (key.end) return { state: { ...state, cursor: lineEnd(state) } };
174
- if (key.backspace) return { state: cut(state, key.meta ? wordLeft(state) : cursor - before(state).length, cursor) };
266
+ if (key.backspace && key.meta) return { state: killed(state, wordLeft(state), cursor) };
267
+ if (key.backspace) {
268
+ const marker = MARKER_BEFORE.exec(text.slice(0, cursor));
269
+ return { state: cut(state, marker ? cursor - marker[0].length : cursor - before(state).length, cursor) };
270
+ }
175
271
  if (key.delete) return { state: cut(state, cursor, cursor + under(state).length) };
176
272
 
177
273
  if (key.ctrl) {
@@ -181,16 +277,21 @@ export function press(state, input, key, commands = [], files = null) {
181
277
  if (input === 'u') {
182
278
  // At the start of a line nothing of it is before the cursor: the line above goes instead, so pressing it again clears a box of many lines.
183
279
  const start = lineStart(state);
184
- return { state: cut(state, start === cursor && cursor > 0 ? lineStart({ text: state.text, cursor: cursor - 1 }) : start, cursor) };
280
+ return { state: killed(state, start === cursor && cursor > 0 ? lineStart({ text: state.text, cursor: cursor - 1 }) : start, cursor) };
185
281
  }
186
- if (input === 'k') return { state: cut(state, cursor, lineEnd(state)) };
187
- if (input === 'w') return { state: cut(state, wordLeft(state), cursor) };
282
+ if (input === 'k') return { state: killed(state, cursor, lineEnd(state)) };
283
+ if (input === 'w') return { state: killed(state, wordLeft(state), cursor) };
188
284
  if (input === 'd') return { state: cut(state, cursor, cursor + under(state).length) };
285
+ // What Ctrl-U, Ctrl-K, Ctrl-W or Option-Backspace cut, put back where the cursor is.
286
+ if (input === 'y') return { state: state.kill ? insert(state, state.kill) : state };
287
+ if (input === 'b') return { state: { ...state, cursor: cursor - before(state).length } };
288
+ if (input === 'f') return { state: { ...state, cursor: cursor + under(state).length } };
189
289
  return { state };
190
290
  }
191
291
  if (key.meta) {
192
292
  if (input === 'b') return { state: { ...state, cursor: wordLeft(state) } };
193
293
  if (input === 'f') return { state: { ...state, cursor: wordRight(state) } };
294
+ if (input === 'd') return { state: killed(state, cursor, wordRight(state)) };
194
295
  return { state };
195
296
  }
196
297
  if (!input) return { state };
package/src/export.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import { chmodSync, existsSync, mkdirSync, readdirSync, rmSync } from 'node:fs';
2
2
  import { join } from 'node:path';
3
- import { DatabaseSync } from 'node:sqlite';
4
3
  import { getMeta, setMeta } from './db.mjs';
5
4
  import { paths } from './paths.mjs';
5
+ import { DatabaseSync } from './sqlite.mjs';
6
6
 
7
7
  const KEEP_BACKUPS = 5;
8
8
  export const SECTIONS = [
package/src/herdr.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { spawnSync } from 'node:child_process';
2
- import { paths } from './paths.mjs';
2
+ import { existsSync } from 'node:fs';
3
+ import { paths, SELF } from './paths.mjs';
3
4
 
4
5
  /**
5
6
  * Herdr is where a delegated job can be watched. The job itself runs in the chat's process; inside Herdr it also
@@ -37,7 +38,9 @@ export function openWatchTab(herdr, { job, project, env = process.env, onFail =
37
38
  if (tab.error) throw new Error(tab.error);
38
39
  // Marked as the tab's own, so the watcher may close it behind a job that is done; the home goes with the command,
39
40
  // or a chat on another home would be watched from the default one.
40
- const command = `env SUMO_JOB_TAB=1 SUMO_AGENTS_HOME=${shellWord(paths().home)} ${shellWord(paths().launcher)} job watch ${job.id}`;
41
+ // The launcher setup made, where there is one; else this very sumo, node and entry by their full paths — a clone not set up yet still has a watcher.
42
+ const sumo = existsSync(paths().launcher) ? shellWord(paths().launcher) : SELF.map(shellWord).join(' ');
43
+ const command = `env SUMO_JOB_TAB=1 SUMO_AGENTS_HOME=${shellWord(paths().home)} ${sumo} job watch ${job.id}`;
41
44
  startWatcher(herdr, { pane: tab.id, job: job.id, command, sleep })
42
45
  .then((started) => started || onFail('its Herdr tab never started the watcher'))
43
46
  .catch((cause) => onFail(cause.message));
package/src/hooks.mjs CHANGED
@@ -7,7 +7,7 @@ import { guardCommand, guardPath } from './guard.mjs';
7
7
  import { autoGuides, firingGuides, matchGuide, renderGuide } from './guides.mjs';
8
8
  import { paths } from './paths.mjs';
9
9
  import { prime } from './prime.mjs';
10
- import { detect, keepScratch, touch } from './projects.mjs';
10
+ import { detect, keepScratch, projectAt, touch } from './projects.mjs';
11
11
  import { closingQuestions, recallBeforeAsking } from './recall.mjs';
12
12
  import { line } from './render.mjs';
13
13
  import { shouldRun, spawnDetached } from './scribe.mjs';
@@ -35,6 +35,7 @@ export function fieldsOf(p) {
35
35
  prompt: p.prompt ?? '',
36
36
  toolName: String(p.tool_name ?? '').toLowerCase(),
37
37
  command: p.tool_input?.command ?? '',
38
+ question: p.tool_input?.question ?? '',
38
39
  path: p.tool_input?.path ?? p.tool_input?.file_path ?? '',
39
40
  agentId: p.agent_id ?? null,
40
41
  project: p.project ?? null,
@@ -81,7 +82,7 @@ function sizeNudge(db, e, now) {
81
82
  const slug = `context:${band}`;
82
83
  if (db.prepare('SELECT 1 FROM session_injections WHERE session_id = ? AND slug = ?').get(e.sessionId, slug)) return '';
83
84
  markInjected(db, e.sessionId, slug, now);
84
- return contextNudge(band, tokens, focusOf(db, e.sessionId));
85
+ return contextNudge(band, focusOf(db, e.sessionId));
85
86
  }
86
87
 
87
88
  /**
@@ -158,7 +159,10 @@ const HANDLERS = {
158
159
 
159
160
  const shown = new Set(injectedSlugs(db, e.sessionId));
160
161
  const cards = [];
161
- for (const slug of detect(db, e.prompt)) {
162
+ // A session started inside a project, whose first words name none, is about that project: it is the one meant.
163
+ const named = detect(db, e.prompt);
164
+ const here = named.length === 0 && shown.size === 0 && e.cwd ? projectAt(db, e.cwd) : null;
165
+ for (const slug of here ? [here] : named) {
162
166
  touch(db, slug, now);
163
167
  // Every mention is stamped, not only the first: the project named last is the one the session is about, also when its card was shown before.
164
168
  markInjected(db, e.sessionId, slug, now);
@@ -184,6 +188,12 @@ const HANDLERS = {
184
188
  * is to put the steps there at the moment of action, not to argue.
185
189
  */
186
190
  'pre-tool'(db, e, now) {
191
+ // A question on its way to the user: what memory holds close to it is read first, and holds it back once.
192
+ if (e.toolName === 'ask') {
193
+ ensureSession(db, { id: e.sessionId, cwd: e.cwd, transcriptPath: e.transcriptPath, now });
194
+ const known = recallBeforeAsking(db, { sessionId: e.sessionId, agentId: e.agentId, asked: e.question, now });
195
+ return known.length > 0 ? deny(`Not asked yet: ${alreadyKnown(known)} If none does, ask again.`) : '';
196
+ }
187
197
  if (e.toolName !== 'bash' || !e.command) return '';
188
198
  // Teaching or reading a workflow names its own cue; `sumo` must never be gated by what it manages — and nothing else rides through on it.
189
199
  const judged = withoutSumoCalls(e.command);