aegiscode 6.8.3 → 6.8.4
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/bin/aegiscode.js +35 -5
- package/package.json +1 -1
- package/scripts/predist.mjs +99 -0
- package/src/app.js +40 -8
- package/src/chatflow.js +181 -33
- package/src/commands.js +154 -17
- package/src/deps.js +2 -1
- package/src/events.js +37 -14
- package/src/mcp.js +683 -0
- package/src/panels.js +46 -13
- package/src/screen.js +130 -5
- package/src/title.js +217 -0
- package/src/tokens.js +74 -95
- package/src/update.js +75 -1
- package/vendor/desktop/lib/local/engine.js +24 -0
- package/vendor/desktop/lib/settings.js +67 -0
- package/vendor/desktop/renderer/usage.js +372 -41
- package/vendor/hosts/install.js +638 -0
- package/vendor/hosts/openai-shim.js +419 -0
- package/vendor/hosts/spec.js +164 -0
- package/vendor/hosts/targets.js +346 -0
- package/vendor/mcp/server.js +186 -0
package/src/panels.js
CHANGED
|
@@ -60,6 +60,30 @@ function str(v) {
|
|
|
60
60
|
return v == null ? '' : String(v);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
/**
|
|
64
|
+
* The four token buckets these panels render, in the shared shape.
|
|
65
|
+
*
|
|
66
|
+
* `input` is the MISS half only — that is what `usageBuckets` normalises it to
|
|
67
|
+
* for every provider, and what the ledger rows and the desktop roll store — so
|
|
68
|
+
* the prompt a context window actually holds is `input + cacheRead +
|
|
69
|
+
* cacheWrite`, NOT `input` alone. Totalling `input + output` (what this file
|
|
70
|
+
* did before) therefore silently dropped the entire cached prompt: the exact
|
|
71
|
+
* turns where the cache was working and the context was largest were the turns
|
|
72
|
+
* the meter under-reported.
|
|
73
|
+
*
|
|
74
|
+
* `tok.total` wins when a caller supplies one (the live session tally does), so
|
|
75
|
+
* this fallback can only ever add information, never overwrite it. One formula,
|
|
76
|
+
* one place: /cost, /context and /tokens must not disagree about the same turn.
|
|
77
|
+
*/
|
|
78
|
+
function tokenBuckets(tok) {
|
|
79
|
+
const input = num(tok.input);
|
|
80
|
+
const output = num(tok.output);
|
|
81
|
+
const cacheRead = num(tok.cacheRead);
|
|
82
|
+
const cacheWrite = num(tok.cacheWrite);
|
|
83
|
+
const prompt = input + cacheRead + cacheWrite;
|
|
84
|
+
return { input, output, cacheRead, cacheWrite, prompt, total: num(tok.total) || prompt + output };
|
|
85
|
+
}
|
|
86
|
+
|
|
63
87
|
/** Group digits: 1562 -> "1,562". Matches the CLI's format.js rule. */
|
|
64
88
|
function fmtTokens(n) {
|
|
65
89
|
const v = Number(n);
|
|
@@ -267,9 +291,7 @@ function buildCost(state, ctx = {}) {
|
|
|
267
291
|
const { cols } = getSize();
|
|
268
292
|
const W = Math.max(30, cols - 2);
|
|
269
293
|
const tok = s.tokens && typeof s.tokens === 'object' ? s.tokens : {};
|
|
270
|
-
const input =
|
|
271
|
-
const output = num(tok.output);
|
|
272
|
-
const total = num(tok.total) || input + output;
|
|
294
|
+
const { input, output, cacheRead, cacheWrite, prompt, total } = tokenBuckets(tok);
|
|
273
295
|
const cost = num(s.costEur);
|
|
274
296
|
const model = str(s.model) || 'the pinned model';
|
|
275
297
|
const window = num(s.contextWindow) || 200000;
|
|
@@ -290,7 +312,14 @@ function buildCost(state, ctx = {}) {
|
|
|
290
312
|
lines.push([span('', ' '), span(t.white, 'Turns:'), span(t.gray, ` ${num(s.turns)} turns · ${num(s.calls)} calls`)]);
|
|
291
313
|
}
|
|
292
314
|
lines.push([span('', ' '), span(t.white, ' Usage by model:')]);
|
|
293
|
-
|
|
315
|
+
// The prompt, not the miss. `input` is the miss half only (see tokenBuckets),
|
|
316
|
+
// so printing it as "input" reported 1.2k for a turn that had sent 31.2k —
|
|
317
|
+
// the same under-report /context was fixed for, in the panel that sits right
|
|
318
|
+
// next to it. With a cache in play the row is a sum, so show the split too.
|
|
319
|
+
lines.push([span('', ' '), span(t.gray, ` ${model}: ${fmtTokens(prompt)} input, ${fmtTokens(output)} output (${fmtEur(cost)})`)]);
|
|
320
|
+
if (cacheRead || cacheWrite) {
|
|
321
|
+
lines.push([span('', ' '), span(t.dim, ` ↳ miss ${fmtTokens(input)} · cache read ${fmtTokens(cacheRead)} · cache write ${fmtTokens(cacheWrite)}`)]);
|
|
322
|
+
}
|
|
294
323
|
lines.push([span('', ' ')]);
|
|
295
324
|
lines.push([span('', ' '), span(t.white + BOLD, 'Current session'), span(BOLD_OFF, '')]);
|
|
296
325
|
lines.push(contextBar(total, window, t));
|
|
@@ -309,9 +338,7 @@ function buildContext(state, ctx = {}) {
|
|
|
309
338
|
const { cols } = getSize();
|
|
310
339
|
const W = Math.max(30, cols - 2);
|
|
311
340
|
const tok = s.tokens && typeof s.tokens === 'object' ? s.tokens : {};
|
|
312
|
-
const input =
|
|
313
|
-
const output = num(tok.output);
|
|
314
|
-
const total = num(tok.total) || input + output;
|
|
341
|
+
const { input, output, cacheRead, cacheWrite, prompt, total } = tokenBuckets(tok);
|
|
315
342
|
const window = num(s.contextWindow) || 200000;
|
|
316
343
|
|
|
317
344
|
const lines = [];
|
|
@@ -324,7 +351,17 @@ function buildContext(state, ctx = {}) {
|
|
|
324
351
|
span('', ' '.repeat(Math.max(1, 10 - fmtTokens(tokens).length))),
|
|
325
352
|
...contextBar(tokens, window, t),
|
|
326
353
|
];
|
|
327
|
-
lines.push(row('Prompt tokens',
|
|
354
|
+
lines.push(row('Prompt tokens', prompt));
|
|
355
|
+
// With a cache in play the prompt row is a SUM, and a sum hides the one thing
|
|
356
|
+
// the operator is looking for: how much of the prompt was re-sent at full
|
|
357
|
+
// price. Show the split, so "280.0k" cannot be misread as 280k of fresh
|
|
358
|
+
// input. Absent a cache the row is byte-identical to before.
|
|
359
|
+
if (cacheRead || cacheWrite) {
|
|
360
|
+
lines.push([
|
|
361
|
+
span('', ' '),
|
|
362
|
+
span(t.dim, ` ↳ miss ${fmtTokens(input)} · cache read ${fmtTokens(cacheRead)} · cache write ${fmtTokens(cacheWrite)}`),
|
|
363
|
+
]);
|
|
364
|
+
}
|
|
328
365
|
lines.push(row('Output tokens', output));
|
|
329
366
|
lines.push([span('', ' ')]);
|
|
330
367
|
lines.push([span('', ' '), span(t.gray, '─'.repeat(Math.max(1, W - 2)))]);
|
|
@@ -896,11 +933,7 @@ function buildTokens(state, ctx = {}) {
|
|
|
896
933
|
const { cols } = getSize();
|
|
897
934
|
const W = Math.max(24, cols - 2);
|
|
898
935
|
const tok = s.tokens && typeof s.tokens === 'object' ? s.tokens : {};
|
|
899
|
-
const input =
|
|
900
|
-
const output = num(tok.output);
|
|
901
|
-
const cacheRead = num(tok.cacheRead);
|
|
902
|
-
const cacheWrite = num(tok.cacheWrite);
|
|
903
|
-
const total = num(tok.total) || input + output;
|
|
936
|
+
const { input, output, cacheRead, cacheWrite, total } = tokenBuckets(tok);
|
|
904
937
|
const cost = num(s.costEur);
|
|
905
938
|
const window = num(s.contextWindow) || 200000;
|
|
906
939
|
const pct = window > 0 ? Math.min(100, Math.round((total / window) * 100)) : 0;
|
package/src/screen.js
CHANGED
|
@@ -224,11 +224,129 @@ const isAltScreen = () => inAlt;
|
|
|
224
224
|
const enableBracketedPaste = () => process.stdout.write('\x1b[?2004h');
|
|
225
225
|
const disableBracketedPaste = () => process.stdout.write('\x1b[?2004l');
|
|
226
226
|
|
|
227
|
-
// Mouse
|
|
228
|
-
//
|
|
229
|
-
//
|
|
230
|
-
|
|
231
|
-
|
|
227
|
+
// Mouse tracking + SGR coordinates. DECSET 1000 is the press/wheel report the
|
|
228
|
+
// click-to-toggle path needs; 1002 adds DRAG reports, without which a
|
|
229
|
+
// drag-to-mark gesture is invisible — the terminal reports the press and the
|
|
230
|
+
// release and nothing in between, so a sweep over three lines arrives as a
|
|
231
|
+
// single-cell marking. The terminal's own selection can't substitute for it:
|
|
232
|
+
// the alt screen has no scrollback for the terminal to select from. Disabled on
|
|
233
|
+
// exit so the shell we return to isn't left forwarding raw wheel bytes as input.
|
|
234
|
+
const enableMouseTracking = () => process.stdout.write('\x1b[?1000h\x1b[?1002h\x1b[?1006h');
|
|
235
|
+
const disableMouseTracking = () => process.stdout.write('\x1b[?1000l\x1b[?1002l\x1b[?1006l');
|
|
236
|
+
|
|
237
|
+
// Reverse video, used to paint a mouse marking (text selection) over the
|
|
238
|
+
// already-styled grid: the selected slice keeps its own colors and inverts its
|
|
239
|
+
// background. SELECT_OFF (SGR 27) turns inversion back off for the spans that
|
|
240
|
+
// follow the selection on the same row — paint() only resets attributes at the
|
|
241
|
+
// start of a row, so a leftover inverse would wash out the row's tail.
|
|
242
|
+
const SELECT_ON = '\x1b[7m';
|
|
243
|
+
const SELECT_OFF = '\x1b[27m';
|
|
244
|
+
|
|
245
|
+
// ── mouse marking (text selection) ─────────────────────────────────────────
|
|
246
|
+
// The TUI owns the alternate screen, so the terminal's own drag-to-select is
|
|
247
|
+
// unreachable — a marking has to be built from the SGR drag stream (events.js)
|
|
248
|
+
// against the grid the last frame painted (chatflow.js keeps it as
|
|
249
|
+
// `frameLines`). All of it is pure and cell-based: coordinates are grid row +
|
|
250
|
+
// CELL column (0-based), never string indices, so wide glyphs and padded rows
|
|
251
|
+
// can't shift the selection off the text the reader is pointing at.
|
|
252
|
+
// Ported from aegiscodex-dev/src/screen.js so both hosts mark identically.
|
|
253
|
+
|
|
254
|
+
/** Two drag endpoints → the same range in reading order (top-left first). */
|
|
255
|
+
function selectionRange(a, b) {
|
|
256
|
+
if (a.row < b.row || (a.row === b.row && a.col <= b.col)) {
|
|
257
|
+
return { startRow: a.row, startCol: a.col, endRow: b.row, endCol: b.col };
|
|
258
|
+
}
|
|
259
|
+
return { startRow: b.row, startCol: b.col, endRow: a.row, endCol: a.col };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Clip a span line to the cell range [from, to). Edges are cut on whole code
|
|
264
|
+
* points via w() — slicing by character index would split a 2-cell glyph and
|
|
265
|
+
* shift every later column by one. Spans keep their own style, so a marking
|
|
266
|
+
* across a colored diff block stays colored.
|
|
267
|
+
*/
|
|
268
|
+
function sliceLine(line, from, to) {
|
|
269
|
+
const out = [];
|
|
270
|
+
let col = 0;
|
|
271
|
+
for (const sp of line) {
|
|
272
|
+
const start = col;
|
|
273
|
+
col += sp.w;
|
|
274
|
+
if (col <= from || start >= to) continue;
|
|
275
|
+
let text = sp.t;
|
|
276
|
+
let seen = start; // cell column of text[0]
|
|
277
|
+
if (seen < from) {
|
|
278
|
+
const drop = from - seen;
|
|
279
|
+
let acc = 0;
|
|
280
|
+
let cut = 0;
|
|
281
|
+
for (const ch of text) {
|
|
282
|
+
if (acc >= drop) break;
|
|
283
|
+
acc += w(ch);
|
|
284
|
+
cut += ch.length;
|
|
285
|
+
}
|
|
286
|
+
text = text.slice(cut);
|
|
287
|
+
seen += acc;
|
|
288
|
+
}
|
|
289
|
+
if (seen + w(text) > to) {
|
|
290
|
+
let acc = 0;
|
|
291
|
+
let keep = '';
|
|
292
|
+
for (const ch of text) {
|
|
293
|
+
const cw = w(ch);
|
|
294
|
+
if (seen + acc + cw > to) break;
|
|
295
|
+
keep += ch;
|
|
296
|
+
acc += cw;
|
|
297
|
+
}
|
|
298
|
+
text = keep;
|
|
299
|
+
}
|
|
300
|
+
if (text) out.push({ t: text, s: sp.s, w: w(text) });
|
|
301
|
+
}
|
|
302
|
+
return out;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* The text a marking covers, read as a terminal would copy it: hard newlines
|
|
307
|
+
* between rows, trailing padding trimmed (rows are padded to the full width,
|
|
308
|
+
* and nobody wants 200 spaces per line on the clipboard), leading/trailing
|
|
309
|
+
* blank rows dropped.
|
|
310
|
+
*/
|
|
311
|
+
function selectionText(lines, sel) {
|
|
312
|
+
const rows = [];
|
|
313
|
+
for (let r = sel.startRow; r <= sel.endRow; r++) {
|
|
314
|
+
const line = lines[r] || [];
|
|
315
|
+
const from = r === sel.startRow ? sel.startCol : 0;
|
|
316
|
+
const to = r === sel.endRow ? sel.endCol : Infinity;
|
|
317
|
+
rows.push(sliceLine(line, from, to).map((sp) => sp.t).join('').replace(/[ \t]+$/, ''));
|
|
318
|
+
}
|
|
319
|
+
while (rows.length && rows[0] === '') rows.shift();
|
|
320
|
+
while (rows.length && rows[rows.length - 1] === '') rows.pop();
|
|
321
|
+
return rows.join('\n');
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* A copy of `lines` with the marked cells wrapped in reverse video. Rows
|
|
326
|
+
* outside the range are shared, not cloned — a marking repaints up to ~30x/sec
|
|
327
|
+
* during a sweep and only the rows it touches should allocate. Each is padded
|
|
328
|
+
* first so a short row's selection is still painted over the blank cells the
|
|
329
|
+
* reader dragged across.
|
|
330
|
+
*/
|
|
331
|
+
function applySelection(lines, sel, width) {
|
|
332
|
+
const out = lines.slice();
|
|
333
|
+
for (let r = sel.startRow; r <= sel.endRow; r++) {
|
|
334
|
+
if (r < 0 || r >= out.length) continue;
|
|
335
|
+
const line = padLine(out[r] || [], width);
|
|
336
|
+
const from = r === sel.startRow ? sel.startCol : 0;
|
|
337
|
+
const to = r === sel.endRow ? sel.endCol : width;
|
|
338
|
+
const mid = sliceLine(line, from, to);
|
|
339
|
+
if (!mid.length) continue;
|
|
340
|
+
out[r] = [
|
|
341
|
+
...sliceLine(line, 0, from),
|
|
342
|
+
{ t: '', s: SELECT_ON, w: 0 },
|
|
343
|
+
...mid,
|
|
344
|
+
{ t: '', s: SELECT_OFF, w: 0 },
|
|
345
|
+
...sliceLine(line, to, width),
|
|
346
|
+
];
|
|
347
|
+
}
|
|
348
|
+
return out;
|
|
349
|
+
}
|
|
232
350
|
|
|
233
351
|
/**
|
|
234
352
|
* Paint span lines top-aligned and clear the rest of the frame. Used by the
|
|
@@ -346,6 +464,13 @@ module.exports = {
|
|
|
346
464
|
disableBracketedPaste,
|
|
347
465
|
enableMouseTracking,
|
|
348
466
|
disableMouseTracking,
|
|
467
|
+
// mouse marking (text selection)
|
|
468
|
+
SELECT_ON,
|
|
469
|
+
SELECT_OFF,
|
|
470
|
+
selectionRange,
|
|
471
|
+
sliceLine,
|
|
472
|
+
selectionText,
|
|
473
|
+
applySelection,
|
|
349
474
|
// the live region + sizing (app/bin)
|
|
350
475
|
LiveRegion,
|
|
351
476
|
termWidth,
|
package/src/title.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The terminal title bar — the session's *topic*.
|
|
5
|
+
*
|
|
6
|
+
* Claude Code keeps the window/tab title pointed at what the session is about
|
|
7
|
+
* ("✳ Fixing the nexus hang") rather than at a static product string, so a row
|
|
8
|
+
* of terminal tabs is readable at a glance. This host shipped the static half
|
|
9
|
+
* only: `chatflow.js`'s `setTitle()` wrote "AEGIS Code" forever, spinning a
|
|
10
|
+
* braille frame while a turn ran and never naming the work. Every tab of every
|
|
11
|
+
* session read the same, which is exactly the problem the topic solves.
|
|
12
|
+
*
|
|
13
|
+
* The topic is derived from the user's own prompt, locally — no model call, so
|
|
14
|
+
* it costs nothing, adds no latency to the first frame, and cannot invent a
|
|
15
|
+
* task the user never asked for. It is re-derived per prompt and adopted only
|
|
16
|
+
* when the ask is about something new (`adoptTopic`), so a follow-up like
|
|
17
|
+
* "now add a test for that" keeps the title it inherited.
|
|
18
|
+
*
|
|
19
|
+
* Everything here is pure and returns strings, so the whole surface is
|
|
20
|
+
* assertable without a TTY; `writeTitle` is the one function that touches a
|
|
21
|
+
* stream, and it swallows a non-writable one.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** The title the app wears before a topic exists (and after one is cleared). */
|
|
25
|
+
const DEFAULT_TITLE = 'AEGIS Code';
|
|
26
|
+
|
|
27
|
+
/** The braille frames the title spins while a turn runs (the reference's). */
|
|
28
|
+
const TITLE_SPIN = ['⠐', '⠂', '⠄', '⠆', '⠈', '⠠', '⠰', '⠁'];
|
|
29
|
+
|
|
30
|
+
/** Longest topic, in cells, before it is clipped on a word boundary. */
|
|
31
|
+
const TOPIC_MAX = 48;
|
|
32
|
+
|
|
33
|
+
/** Longest string that ever reaches the terminal (topic + " · AEGIS Code"). */
|
|
34
|
+
const TITLE_MAX = 96;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Politeness and throat-clearing, stripped from the front of a prompt so the
|
|
38
|
+
* title says "Add a retry to the fetcher" instead of "Please can you add a
|
|
39
|
+
* retry to the fetcher". Applied repeatedly — "ok so please can you…" is one
|
|
40
|
+
* habit, not four.
|
|
41
|
+
*/
|
|
42
|
+
const FILLER =
|
|
43
|
+
/^(?:please|pls|plz|hey|hi|hello|ok(?:ay)?|so|now|just|also|and|then|maybe|well|um|uh|can you|could you|would you|will you|i want you to|i want to|i need you to|i need to|i'?d like you to|i would like you to|i'?d like to|help me|help|let'?s|lets|go ahead and|i'?m trying to|i am trying to|i'?m working on|i am working on)\b[\s,:;-]*/i;
|
|
44
|
+
|
|
45
|
+
/** Leading markdown/list/quote noise: `> `, `- `, `1. `, `## `. */
|
|
46
|
+
const LEADING_NOISE = /^(?:[>#*•·\-–—]+|\d+[.)])\s*/;
|
|
47
|
+
|
|
48
|
+
/** Words that carry no topic signal when comparing two asks. */
|
|
49
|
+
const STOPWORDS = new Set([
|
|
50
|
+
'the', 'and', 'for', 'with', 'that', 'this', 'these', 'those', 'you', 'your', 'yours',
|
|
51
|
+
'can', 'could', 'would', 'should', 'will', 'please', 'pls', 'hey', 'now', 'then',
|
|
52
|
+
'from', 'into', 'onto', 'over', 'under', 'about', 'also', 'just', 'like', 'want',
|
|
53
|
+
'need', 'make', 'made', 'use', 'using', 'get', 'got', 'let', 'lets', 'its', 'it',
|
|
54
|
+
'are', 'was', 'were', 'been', 'has', 'have', 'had', 'but', 'not', 'all', 'any',
|
|
55
|
+
'how', 'what', 'when', 'where', 'which', 'who', 'why', 'there', 'here', 'some',
|
|
56
|
+
'one', 'two', 'add', 'new', 'fix', 'fixing', 'help', 'doing', 'does', 'did', 'me',
|
|
57
|
+
'my', 'our', 'we', 'i', 'a', 'an', 'to', 'of', 'in', 'on', 'at', 'is', 'be', 'do',
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
/** Strip anything that could break out of the OSC title sequence. */
|
|
61
|
+
function sanitize(text, max = TITLE_MAX) {
|
|
62
|
+
const flat = String(text == null ? '' : text)
|
|
63
|
+
// Control chars (including ESC, BEL, CR, LF, TAB) become a space, so a
|
|
64
|
+
// prompt can never inject an escape sequence into the title bar — the one
|
|
65
|
+
// place in this UI where user text reaches the terminal unsanitized.
|
|
66
|
+
.replace(/[\u0000-\u001f\u007f]/g, ' ')
|
|
67
|
+
.replace(/\s+/g, ' ')
|
|
68
|
+
.trim();
|
|
69
|
+
if (flat.length <= max) return flat;
|
|
70
|
+
return `${flat.slice(0, Math.max(0, max - 1)).trimEnd()}…`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A short, single-line topic for a user prompt.
|
|
75
|
+
*
|
|
76
|
+
* Uses the first line that has anything on it (a pasted block's first line is
|
|
77
|
+
* its subject), drops politeness and list noise, collapses whitespace, and
|
|
78
|
+
* clips on a word boundary with an ellipsis. Returns '' when there is nothing
|
|
79
|
+
* to say — the caller then keeps the title it already had.
|
|
80
|
+
*/
|
|
81
|
+
function topicFrom(text, { max = TOPIC_MAX } = {}) {
|
|
82
|
+
if (text == null) return '';
|
|
83
|
+
const lines = String(text).split(/[\r\n]+/);
|
|
84
|
+
let line = '';
|
|
85
|
+
for (const raw of lines) {
|
|
86
|
+
const cleaned = sanitize(raw, 4_000).replace(LEADING_NOISE, '').trim();
|
|
87
|
+
// A pasted block's fence marker is not the topic; the code under it is.
|
|
88
|
+
if (!cleaned || /^```/.test(cleaned)) continue;
|
|
89
|
+
line = cleaned;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
if (!line) return '';
|
|
93
|
+
let body = line;
|
|
94
|
+
for (let i = 0; i < 6; i++) {
|
|
95
|
+
const next = body.replace(FILLER, '');
|
|
96
|
+
if (next === body) break;
|
|
97
|
+
body = next.replace(LEADING_NOISE, '').trim();
|
|
98
|
+
}
|
|
99
|
+
body = body.replace(/[.!?]+$/, '').trim();
|
|
100
|
+
if (!body) body = line;
|
|
101
|
+
if (/^[a-z]/.test(body)) body = body[0].toUpperCase() + body.slice(1);
|
|
102
|
+
return sanitize(body, max);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Topic-defining words of a phrase: lowercase, deduped, stopwords dropped. */
|
|
106
|
+
function keywords(phrase) {
|
|
107
|
+
const out = new Set();
|
|
108
|
+
for (const word of String(phrase || '').toLowerCase().match(/[\p{L}\p{N}_]+/gu) || []) {
|
|
109
|
+
if (word.length < 3 || STOPWORDS.has(word)) continue;
|
|
110
|
+
out.add(word);
|
|
111
|
+
}
|
|
112
|
+
return [...out];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Does `next` read as a *different* task from `prev`?
|
|
117
|
+
*
|
|
118
|
+
* The overlap is measured against the shorter keyword set, so a long follow-up
|
|
119
|
+
* that repeats the subject ("also add a test for the config loader") is not
|
|
120
|
+
* mistaken for a new task, while a genuinely unrelated ask ("now write the
|
|
121
|
+
* changelog") is. An empty `next` is never a shift — it means "no topic", and
|
|
122
|
+
* the caller keeps what it had.
|
|
123
|
+
*/
|
|
124
|
+
function titleShift(prev, next, { threshold = 0.34 } = {}) {
|
|
125
|
+
if (!next) return false;
|
|
126
|
+
if (!prev) return true;
|
|
127
|
+
const a = keywords(prev);
|
|
128
|
+
const b = keywords(next);
|
|
129
|
+
if (!a.length || !b.length) return true;
|
|
130
|
+
const set = new Set(a);
|
|
131
|
+
let hits = 0;
|
|
132
|
+
for (const word of b) if (set.has(word)) hits++;
|
|
133
|
+
return hits / Math.min(a.length, b.length) < threshold;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The topic after this prompt: the new one only when it is a new task. */
|
|
137
|
+
function adoptTopic(prev, prompt, opts) {
|
|
138
|
+
const next = topicFrom(prompt);
|
|
139
|
+
if (!next) return prev || '';
|
|
140
|
+
return titleShift(prev, next, opts) ? next : prev || '';
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The composed title.
|
|
145
|
+
*
|
|
146
|
+
* working, with topic `⠂ Fixing the config loader`
|
|
147
|
+
* idle, with topic `Fixing the config loader · AEGIS Code`
|
|
148
|
+
* no topic `AEGIS Code`
|
|
149
|
+
*
|
|
150
|
+
* The idle form keeps the product name so a tab is still identifiable as this
|
|
151
|
+
* CLI; the working form is the topic alone, which is what Claude Code shows
|
|
152
|
+
* while a turn runs.
|
|
153
|
+
*/
|
|
154
|
+
function titleText({ topic = '', frame = null, app = DEFAULT_TITLE } = {}) {
|
|
155
|
+
const clean = sanitize(topic, TOPIC_MAX);
|
|
156
|
+
if (!clean) return sanitize(app, TITLE_MAX) || DEFAULT_TITLE;
|
|
157
|
+
const head = frame ? `${frame} ` : '';
|
|
158
|
+
return sanitize(frame ? `${head}${clean}` : `${clean} · ${app}`, TITLE_MAX);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Truthiness for the env flags: `1`, `true`, `yes`, `on` (any case). */
|
|
162
|
+
function flagOn(value) {
|
|
163
|
+
if (value === true) return true;
|
|
164
|
+
if (value == null || value === false) return false;
|
|
165
|
+
return /^(?:1|true|yes|on)$/i.test(String(value).trim());
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Is the title bar ours to write?
|
|
170
|
+
*
|
|
171
|
+
* On by default — a title that follows the work is the feature. Off via
|
|
172
|
+
* `AEGIS_DISABLE_TERMINAL_TITLE=1` (and Claude Code's own
|
|
173
|
+
* `CLAUDE_CODE_DISABLE_TERMINAL_TITLE`, so a shell configured for the
|
|
174
|
+
* reference behaves the same here), or `terminalTitle: false` in config.json.
|
|
175
|
+
* `AEGIS_TERMINAL_TITLE=1` wins over the config, for a one-off run.
|
|
176
|
+
*/
|
|
177
|
+
function titleEnabled(env = {}, config = {}) {
|
|
178
|
+
const e = env || {};
|
|
179
|
+
if (flagOn(e.AEGIS_TERMINAL_TITLE)) return true;
|
|
180
|
+
if (flagOn(e.AEGIS_DISABLE_TERMINAL_TITLE)) return false;
|
|
181
|
+
if (flagOn(e.CLAUDE_CODE_DISABLE_TERMINAL_TITLE)) return false;
|
|
182
|
+
const cfg = config || {};
|
|
183
|
+
if (cfg.terminalTitle === false || cfg.terminalTitleBar === false) return false;
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Write one title to a stream as an OSC 0 sequence. `''` clears it (what the
|
|
189
|
+
* teardown wants, so the shell that gets the terminal back is not left wearing
|
|
190
|
+
* this app's title). Never throws: a stubbed, closed or non-TTY stream is a
|
|
191
|
+
* missing nicety, not a reason to lose a turn.
|
|
192
|
+
*/
|
|
193
|
+
function writeTitle(text, out) {
|
|
194
|
+
const stream = out || process.stdout;
|
|
195
|
+
if (!stream || typeof stream.write !== 'function') return false;
|
|
196
|
+
try {
|
|
197
|
+
stream.write(`\x1b]0;${sanitize(text, TITLE_MAX)}\x07`);
|
|
198
|
+
return true;
|
|
199
|
+
} catch {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
module.exports = {
|
|
205
|
+
DEFAULT_TITLE,
|
|
206
|
+
TITLE_SPIN,
|
|
207
|
+
TOPIC_MAX,
|
|
208
|
+
TITLE_MAX,
|
|
209
|
+
sanitize,
|
|
210
|
+
topicFrom,
|
|
211
|
+
keywords,
|
|
212
|
+
titleShift,
|
|
213
|
+
adoptTopic,
|
|
214
|
+
titleText,
|
|
215
|
+
titleEnabled,
|
|
216
|
+
writeTitle,
|
|
217
|
+
};
|