angelia-gateway 0.3.27 → 0.3.28
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/dist/brain/tmux.js +45 -6
- package/dist/brain/tui.js +166 -10
- package/dist/build.json +3 -3
- package/dist/core/orchestrator.js +29 -12
- package/package.json +1 -1
package/dist/brain/tmux.js
CHANGED
|
@@ -62,17 +62,43 @@ export async function loadBuffer(name, text, dir, run) {
|
|
|
62
62
|
* so the last spinner row seen decides. */
|
|
63
63
|
const PROMPT_MARKERS = ['\n❯', '\n> ', '│ >'];
|
|
64
64
|
export const PERMISSION_MARKER = 'Do you want to proceed?';
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
/** The glyphs the spinner cycles through; its "done" row keeps one of them. Any one or two
|
|
66
|
+
* characters used to count, which was loose both ways: a row such as "∴ Thinking…" or "! ls…" at the
|
|
67
|
+
* left edge read as a turn that never ends, and a spinner of several words read as idle. The
|
|
68
|
+
* one-word shape is measured (the fixtures); several words, as in "✻ Compacting conversation… (12s)",
|
|
69
|
+
* are allowed for but not measured on a live pane. `*` stands in for `✳` where the terminal is not
|
|
70
|
+
* a Mac's (Linux), from Claude Code's source as remembered, not measured: without it one frame in
|
|
71
|
+
* six of a working pane would read idle there.
|
|
72
|
+
*
|
|
73
|
+
* The done row is the verb, "for", and a time ("for 14s", "for 1m 2s"). What follows the time varies
|
|
74
|
+
* ("· done", "· 1 shell still running…"), so it is not asked for: a done row that ends in "…" would
|
|
75
|
+
* otherwise read as a turn that never ends. A spinner text such as "Searching for 2 patterns…" has
|
|
76
|
+
* no time unit after the number and stays a spinner. */
|
|
77
|
+
const SPIN = '[·✢✳✶✻✽*]';
|
|
78
|
+
const BUSY_RE = new RegExp(`^${SPIN} [^…]{1,60}…`);
|
|
79
|
+
const DONE_RE = new RegExp(`^${SPIN} \\S+ for \\d+[smh]( |$)`);
|
|
67
80
|
/** The spinner is always drawn just above the input box, so only the foot of the pane counts.
|
|
68
81
|
* Further up it would be scrollback — an old assistant line ending in "…" would otherwise read
|
|
69
|
-
* as a turn that never finishes, and nothing could ever be typed again.
|
|
82
|
+
* as a turn that never finishes, and nothing could ever be typed again. Used when the screen has
|
|
83
|
+
* no input box to measure from. */
|
|
70
84
|
const TAIL_LINES = 14;
|
|
85
|
+
/** How many rows above the input box may hold the spinner. Claude Code draws rows between the two
|
|
86
|
+
* (a tip, a progress bar, the task list), and rows under the box that grow with the work: two
|
|
87
|
+
* status rows, then one row per running teammate (measured on claude 2.1.329, fixture
|
|
88
|
+
* pane-busy-teammates.txt, one teammate: read as busy from the screen's end too). Counted from the
|
|
89
|
+
* screen's end, as TAIL_LINES is, three more teammate rows push the spinner out of sight and a
|
|
90
|
+
* working pane reads as idle (pane-busy-teammates-4.txt: the same capture with three rows added by
|
|
91
|
+
* hand). So the box is the anchor, and what is under it is never read. */
|
|
92
|
+
const ABOVE_BOX = 16;
|
|
71
93
|
/** Claude collapses a long paste into a chip like `[Pasted text +12 lines]`. */
|
|
72
94
|
export const PASTED_MARKER = '[Pasted text';
|
|
73
95
|
export function paneBusy(pane) {
|
|
96
|
+
const lines = pane.replace(/\n+$/, '').split('\n');
|
|
97
|
+
const box = boxSpan(lines);
|
|
98
|
+
// No box (a dialog, an older drawing cut off): the foot of the capture, as before.
|
|
99
|
+
const foot = box ? lines.slice(Math.max(0, box.top - ABOVE_BOX), box.top) : pane.split('\n').slice(-TAIL_LINES);
|
|
74
100
|
let busy = false;
|
|
75
|
-
for (const line of
|
|
101
|
+
for (const line of foot) {
|
|
76
102
|
if (DONE_RE.test(line))
|
|
77
103
|
busy = false;
|
|
78
104
|
else if (BUSY_RE.test(line))
|
|
@@ -291,12 +317,25 @@ export function inputBox(pane) {
|
|
|
291
317
|
* drawing, inside the last `╭…╰` frame. A dialog draws neither. */
|
|
292
318
|
function inputBoxStrict(pane) {
|
|
293
319
|
const lines = pane.replace(/\n+$/, '').split('\n');
|
|
320
|
+
const box = boxSpan(lines);
|
|
321
|
+
return box ? lines.slice(box.top + 1, box.bottom).join('\n') : null;
|
|
322
|
+
}
|
|
323
|
+
/** The screen down to the input box's bottom border, without what is drawn under the box: the status
|
|
324
|
+
* rows and the list of teammates, whose clocks tick while the session itself stands still. The whole
|
|
325
|
+
* capture when the screen has no box. For telling whether the session's own part of the screen moved. */
|
|
326
|
+
export function paneAboveFoot(pane) {
|
|
327
|
+
const lines = pane.replace(/\n+$/, '').split('\n');
|
|
328
|
+
const box = boxSpan(lines);
|
|
329
|
+
return box ? lines.slice(0, box.bottom + 1).join('\n') : pane;
|
|
330
|
+
}
|
|
331
|
+
/** Where the input box is drawn: the lines of its top and bottom border, or null without a box. */
|
|
332
|
+
function boxSpan(lines) {
|
|
294
333
|
const rules = lines.flatMap((l, i) => (RULE_RE.test(l) ? [i] : []));
|
|
295
334
|
if (rules.length >= 2)
|
|
296
|
-
return
|
|
335
|
+
return { top: rules[rules.length - 2], bottom: rules[rules.length - 1] };
|
|
297
336
|
const bottom = lastLine(lines, /^\s*╰/, lines.length);
|
|
298
337
|
const top = bottom > 0 ? lastLine(lines, /^\s*╭/, bottom) : -1;
|
|
299
|
-
return top >= 0 ?
|
|
338
|
+
return top >= 0 ? { top, bottom } : null;
|
|
300
339
|
}
|
|
301
340
|
function lastLine(lines, re, before) {
|
|
302
341
|
for (let i = before - 1; i >= 0; i--)
|
package/dist/brain/tui.js
CHANGED
|
@@ -8,8 +8,8 @@ import { fileURLToPath } from 'node:url';
|
|
|
8
8
|
import { cleanText } from '../core/types.js';
|
|
9
9
|
import { childEnv, CLAUDE_ENV, claudeTuiArgv, MIN_CLAUDE_VERSION, remoteControlName, STRIP_ENV, versionAtLeast } from './argv.js';
|
|
10
10
|
import { PermissionBook } from './brain.js';
|
|
11
|
-
import { addDirAccepted, addDirDialog, importsAccepted, importsDialogOpen, loadBuffer, paneBusy, paneIdle, pasteLanded, permissionDialog, submitPasted, tmux, TMUX_SOCKET, trustAccepted, trustDialogOpen } from './tmux.js';
|
|
12
|
-
import { paneVerdict, PermissionRelay, RELAY_BEAT_MS, relayedPermission, ScreenDialogs } from './tui-permissions.js';
|
|
11
|
+
import { addDirAccepted, addDirDialog, importsAccepted, importsDialogOpen, loadBuffer, paneAboveFoot, paneBusy, paneIdle, pasteLanded, permissionDialog, submitPasted, tmux, TMUX_SOCKET, trustAccepted, trustDialogOpen } from './tmux.js';
|
|
12
|
+
import { paneVerdict, PermissionRelay, REJECTED, RELAY_BEAT_MS, relayedPermission, ScreenDialogs } from './tui-permissions.js';
|
|
13
13
|
import { LOST_SESSION_LINE, placeTranscript, transcriptPath } from './transcripts.js';
|
|
14
14
|
import { readRecord } from '../capabilities/compile.js';
|
|
15
15
|
export { transcriptPath } from './transcripts.js';
|
|
@@ -37,6 +37,20 @@ const STILL_MS = 30 * 60_000;
|
|
|
37
37
|
* is handled by the CLI itself and ends without a Stop event, so without this the turn would
|
|
38
38
|
* simply hang. */
|
|
39
39
|
const QUIET_MS = 12_000;
|
|
40
|
+
/** The same wait when the turn's last transcript row is the assistant's text. That is the answer of
|
|
41
|
+
* a turn whose Stop hook failed, or text that a tool call still follows: the call's row is written
|
|
42
|
+
* when its input is whole, which can take longer than QUIET_MS for a large file, and the rows do not
|
|
43
|
+
* say which (stop_reason on a row is not measured). The Stop hook lands within a second of a real
|
|
44
|
+
* end, so only a failed hook waits this long. Counted from the last transcript row of any kind.
|
|
45
|
+
* BrainOptions.quietTextMs shortens it (tests). */
|
|
46
|
+
const QUIET_TEXT_MS = 90_000;
|
|
47
|
+
/** The same wait when the transcript shows the turn open (a tool runs, the model owes an answer): the
|
|
48
|
+
* net under row shapes turnRows does not know, and under a turn that ended with no Stop hook and no
|
|
49
|
+
* closing row. Counted from the last transcript row of any kind and from the last change of the
|
|
50
|
+
* screen above the input box's foot: a working CLI redraws its spinner clock there every second, so
|
|
51
|
+
* a turn at work is never ended by this, and a teammate's clock under the box does not hold a dead
|
|
52
|
+
* one open. The orchestrator's stall clock does not run for a tmux turn, so this is the only bound. */
|
|
53
|
+
const QUIET_OPEN_MS = 3 * 60_000;
|
|
40
54
|
/** After a chat answer, a dialog that still asks the same thing this long is taken as asking again. */
|
|
41
55
|
const REASK_MS = 2500;
|
|
42
56
|
/** How long a request answered in the pane may go without a result in the transcript before it counts
|
|
@@ -192,6 +206,9 @@ export class TuiBrain extends EventEmitter {
|
|
|
192
206
|
at = 0;
|
|
193
207
|
/** Where this turn's rows begin in the transcript, for reading how a request answered in the pane went. */
|
|
194
208
|
from = 0;
|
|
209
|
+
/** Where the turn stands by the rows read from `from` on (turnRows): the second sign, next to the
|
|
210
|
+
* screen, of whether the turn is over. */
|
|
211
|
+
rows = 'none';
|
|
195
212
|
/** One line for the chat before the next turn's answer, when the launch had to say something (a lost conversation). */
|
|
196
213
|
notice = null;
|
|
197
214
|
lastUsedAt = Date.now();
|
|
@@ -464,6 +481,18 @@ export class TuiBrain extends EventEmitter {
|
|
|
464
481
|
await this.tm(['send-keys', '-t', this.name, 'C-u']); // clear whatever landed, submit nothing
|
|
465
482
|
return false;
|
|
466
483
|
}
|
|
484
|
+
/** Keep the screen of a pane whose turn ended or was held on what the screen showed, next to its
|
|
485
|
+
* settings: the one way to see what it showed, without a terminal open at the right second. One
|
|
486
|
+
* file per reason, overwritten. In the pane's private folder, not the log: it can show the conversation. */
|
|
487
|
+
keepScreen(file, why) {
|
|
488
|
+
void (async () => {
|
|
489
|
+
try {
|
|
490
|
+
mkdirSync(this.dir, { recursive: true, mode: 0o700 });
|
|
491
|
+
writeFileSync(join(this.dir, file), `${new Date().toISOString()} ${why}\n${await this.capture(40)}\n`, { mode: 0o600 });
|
|
492
|
+
}
|
|
493
|
+
catch { /* the log line stands on its own */ }
|
|
494
|
+
})();
|
|
495
|
+
}
|
|
467
496
|
async *turn(text) {
|
|
468
497
|
this.lastUsedAt = Date.now();
|
|
469
498
|
const problem = await (this.ready ?? Promise.resolve('brain not started'));
|
|
@@ -484,6 +513,7 @@ export class TuiBrain extends EventEmitter {
|
|
|
484
513
|
const gen = this.gen;
|
|
485
514
|
const submit = () => {
|
|
486
515
|
this.at = this.from = fileSize(this.transcript); // only rows written from here on are this turn's
|
|
516
|
+
this.rows = 'none';
|
|
487
517
|
rmSync(this.markerPath, { force: true });
|
|
488
518
|
sentAt = Date.now();
|
|
489
519
|
};
|
|
@@ -492,7 +522,7 @@ export class TuiBrain extends EventEmitter {
|
|
|
492
522
|
return;
|
|
493
523
|
}
|
|
494
524
|
this.turnSentAt = sentAt;
|
|
495
|
-
yield* this.watch(sentAt, false, gen);
|
|
525
|
+
yield* this.watch(sentAt, false, gen, text.trimStart().startsWith('/'));
|
|
496
526
|
}
|
|
497
527
|
/**
|
|
498
528
|
* Keep the running turn going with nobody reading it: a handoff moved the chat to another session
|
|
@@ -515,7 +545,11 @@ export class TuiBrain extends EventEmitter {
|
|
|
515
545
|
this.up = true;
|
|
516
546
|
this.ready = Promise.resolve(null);
|
|
517
547
|
this.lastUsedAt = Date.now();
|
|
518
|
-
|
|
548
|
+
// The rows written while nobody read are not shown again (the events are dropped), but they say
|
|
549
|
+
// where the turn stands (a tool that runs still, an answer owed), so the quiet pane cannot end it
|
|
550
|
+
// early from here either. Read as every poll reads, so the turn's rows start on a whole line.
|
|
551
|
+
this.readTranscript();
|
|
552
|
+
this.from = this.at;
|
|
519
553
|
return this.watch(this.turnSentAt ?? Date.now(), false, gen);
|
|
520
554
|
}
|
|
521
555
|
/** Take over a pane that runs already, without starting one: a restarted daemon finds a turn it had
|
|
@@ -529,8 +563,9 @@ export class TuiBrain extends EventEmitter {
|
|
|
529
563
|
}
|
|
530
564
|
/** The polling half of a turn. `background`: no permission book and no relay (nobody in the chat
|
|
531
565
|
* answers), and no hourly line. Ends early, as a failed result, once another reader takes the pane
|
|
532
|
-
* (`gen` moved).
|
|
533
|
-
|
|
566
|
+
* (`gen` moved). `command`: the turn is a slash command typed as is, which the CLI may answer
|
|
567
|
+
* itself, so an end with no answer is expected. */
|
|
568
|
+
async *watch(sentAt, background, gen, command = false) {
|
|
534
569
|
let pending = null;
|
|
535
570
|
// Relayed requests by id, and those answered in the pane or the app whose outcome the transcript
|
|
536
571
|
// has yet to show.
|
|
@@ -538,11 +573,16 @@ export class TuiBrain extends EventEmitter {
|
|
|
538
573
|
const deciding = new Map();
|
|
539
574
|
const dialogs = new DialogWatch();
|
|
540
575
|
const screen = new ScreenDialogs();
|
|
541
|
-
let quietSince = 0;
|
|
576
|
+
let quietSince = 0, held = false, grewAt = 0;
|
|
577
|
+
// The screen above the box's foot, and when it last changed.
|
|
578
|
+
let lastAbove = '', aboveAt = 0;
|
|
579
|
+
const quietMs = this.opts.quietMs ?? QUIET_MS, quietTextMs = this.opts.quietTextMs ?? QUIET_TEXT_MS, quietOpenMs = this.opts.quietOpenMs ?? QUIET_OPEN_MS;
|
|
542
580
|
let hours = 0, lastPane = '', movedAt = Date.now();
|
|
543
581
|
// A dialog still open when the turn ends can no longer be answered from the chat: its timer goes too.
|
|
544
582
|
const over = () => { this.turnSentAt = undefined; this.lastUsedAt = Date.now(); this.permissions.clear(); if (!background)
|
|
545
583
|
this.stopBeat(); };
|
|
584
|
+
// How the turn ended, for the log: which path, how long it ran and how long its answer is, never the words.
|
|
585
|
+
const ended = (how, answer) => { this.emit('log', `tmux: turn ended by ${how} after ${Math.round((Date.now() - sentAt) / 1000)} s, answer ${answer.length} chars pane=${this.name}`); };
|
|
546
586
|
if (!background)
|
|
547
587
|
this.startBeat(gen);
|
|
548
588
|
try {
|
|
@@ -561,7 +601,10 @@ export class TuiBrain extends EventEmitter {
|
|
|
561
601
|
? `Nothing has moved in this turn for ${still} minutes; it may be stuck. /stop ends it.`
|
|
562
602
|
: `Still working on this turn after ${h === 1 ? 'an hour' : `${h} hours`}. /stop ends it.` };
|
|
563
603
|
}
|
|
604
|
+
const readTo = this.at;
|
|
564
605
|
const rows = this.readTranscript();
|
|
606
|
+
if (this.at !== readTo)
|
|
607
|
+
grewAt = Date.now(); // a row of any kind, bookkeeping too
|
|
565
608
|
if (rows.length) {
|
|
566
609
|
quietSince = 0;
|
|
567
610
|
movedAt = Date.now();
|
|
@@ -582,6 +625,7 @@ export class TuiBrain extends EventEmitter {
|
|
|
582
625
|
yield* this.decide(deciding, done !== null);
|
|
583
626
|
if (done !== null) {
|
|
584
627
|
over();
|
|
628
|
+
ended('the Stop marker', done);
|
|
585
629
|
yield { kind: 'result', text: done, isError: false };
|
|
586
630
|
return;
|
|
587
631
|
}
|
|
@@ -608,6 +652,11 @@ export class TuiBrain extends EventEmitter {
|
|
|
608
652
|
if (pane !== lastPane) {
|
|
609
653
|
lastPane = pane;
|
|
610
654
|
movedAt = Date.now();
|
|
655
|
+
const above = paneAboveFoot(pane);
|
|
656
|
+
if (above !== lastAbove) {
|
|
657
|
+
lastAbove = above;
|
|
658
|
+
aboveAt = Date.now();
|
|
659
|
+
}
|
|
611
660
|
}
|
|
612
661
|
const open = permissionDialog(pane);
|
|
613
662
|
if (background) {
|
|
@@ -630,11 +679,36 @@ export class TuiBrain extends EventEmitter {
|
|
|
630
679
|
if (paneIdle(pane) && !dialogs.open && !open && !this.permissions.size) {
|
|
631
680
|
if (!quietSince)
|
|
632
681
|
quietSince = Date.now();
|
|
633
|
-
else if (Date.now() - quietSince
|
|
682
|
+
else if (Date.now() - quietSince <= quietMs) { /* not quiet long enough yet */ }
|
|
683
|
+
else if (this.rows === 'open' && Date.now() - Math.max(quietSince, grewAt, aboveAt) <= quietOpenMs) {
|
|
684
|
+
// The screen reads idle, and the transcript says the turn is open: a tool runs, or the
|
|
685
|
+
// model owes its answer. The screen is a drawing that changes between versions: a foot it
|
|
686
|
+
// had not seen before read as idle and cut working turns (2.1.329). So the screen alone
|
|
687
|
+
// never ends an open turn: the Stop marker does, or rows that close it, or QUIET_OPEN_MS
|
|
688
|
+
// with no new row and nothing moving above the box's foot.
|
|
689
|
+
if (!held) {
|
|
690
|
+
held = true;
|
|
691
|
+
this.emit('log', `tmux: the pane reads idle ${Math.round((Date.now() - sentAt) / 1000)} s into the turn, but its transcript shows the turn open; waiting for the Stop marker (screen kept in idle-open.txt) pane=${this.name}`);
|
|
692
|
+
this.keepScreen('idle-open.txt', 'the pane read idle while the transcript showed the turn open');
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
else if (this.rows === 'said' && Date.now() - Math.max(quietSince, grewAt) <= quietTextMs) {
|
|
696
|
+
// Text is the last row: a tool call may still follow it (QUIET_TEXT_MS).
|
|
697
|
+
}
|
|
698
|
+
else {
|
|
634
699
|
// No Stop event is coming: either the CLI answered the message itself, or the hook failed
|
|
635
700
|
// and the transcript holds the answer.
|
|
636
701
|
over();
|
|
637
|
-
|
|
702
|
+
const stood = { none: 'has no turn', command: 'has a slash command only', said: 'ends with text', error: 'ends with an error row of the CLI',
|
|
703
|
+
open: `open, no new row and nothing moving above the input box for ${Math.round((Date.now() - Math.max(quietSince, grewAt, aboveAt)) / 1000)} s` }[this.rows];
|
|
704
|
+
const how = `the quiet pane (no Stop marker; transcript ${stood})`;
|
|
705
|
+
const answer = pending ?? '';
|
|
706
|
+
ended(how, answer);
|
|
707
|
+
// No answer at all, and not to a slash command: the chat may be told the agent gave none, so
|
|
708
|
+
// keep the screen that read as idle.
|
|
709
|
+
if (!answer && (!command || this.rows === 'open'))
|
|
710
|
+
this.keepScreen('quiet-empty.txt', `the turn ended by ${how} with no answer`);
|
|
711
|
+
yield { kind: 'result', text: answer, isError: false };
|
|
638
712
|
return;
|
|
639
713
|
}
|
|
640
714
|
}
|
|
@@ -687,7 +761,9 @@ export class TuiBrain extends EventEmitter {
|
|
|
687
761
|
if (cut < 0)
|
|
688
762
|
return [];
|
|
689
763
|
this.at += cut + 1;
|
|
690
|
-
|
|
764
|
+
const rows = buf.subarray(0, cut).toString('utf8');
|
|
765
|
+
this.rows = turnRows(rows, this.rows);
|
|
766
|
+
return transcriptEvents(rows);
|
|
691
767
|
}
|
|
692
768
|
catch {
|
|
693
769
|
return [];
|
|
@@ -842,6 +918,86 @@ export function addDirListed(dir, addDirs, home = homedir(), base = process.cwd(
|
|
|
842
918
|
const want = forms(dir);
|
|
843
919
|
return addDirs.some((d) => forms(d).some((f) => want.includes(f)));
|
|
844
920
|
}
|
|
921
|
+
/** A user row that is the CLI's record of a slash command, not a prompt for the model. */
|
|
922
|
+
const COMMAND_ROW = /^<command-(name|message|args)>/;
|
|
923
|
+
/** What the CLI wrote around a command it ran itself: its output, and the caveat before it. */
|
|
924
|
+
const LOCAL_ROW = /^<local-command-[a-z]+>/;
|
|
925
|
+
/** What Claude Code writes as a user row when a turn is cut short with Esc (with or without "for
|
|
926
|
+
* tool use"); no Stop hook runs after it. Not measured on 2.1.329: the text is Claude Code's own. */
|
|
927
|
+
const INTERRUPTED = '[Request interrupted by user';
|
|
928
|
+
/** A tool result that says the person refused the call in Claude Code's own dialog (paneVerdict in
|
|
929
|
+
* tui-permissions.ts reads the same two shapes). The turn stops there, with no Stop hook. */
|
|
930
|
+
function refused(row, b) {
|
|
931
|
+
return row.toolUseResult === REJECTED || (b.is_error === true && (typeof b.content === 'string' ? b.content : JSON.stringify(b.content ?? '')).startsWith("The user doesn't want to proceed"));
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Where the turn stands after the rows in `chunk`, starting from `state`. The last row that counts
|
|
935
|
+
* decides. Rows that count are the session's own user and assistant message rows, as the other
|
|
936
|
+
* readers here take them (history.ts, the Stop hook's finalText): not a subagent's (isSidechain), not
|
|
937
|
+
* the bookkeeping Claude Code writes between them (system and queue-operation rows, snapshots). A row
|
|
938
|
+
* may hold one content block or several; they are read in order, so text followed by a tool call
|
|
939
|
+
* reads `open`. A meta row counts only right after a slash command's row: it is then the prompt the
|
|
940
|
+
* command made for the model (a skill's text), and the model works from there.
|
|
941
|
+
*/
|
|
942
|
+
export function turnRows(chunk, state = 'none') {
|
|
943
|
+
for (const line of chunk.split('\n')) {
|
|
944
|
+
if (!line.includes('"user"') && !line.includes('"assistant"'))
|
|
945
|
+
continue; // most rows are bookkeeping
|
|
946
|
+
let row;
|
|
947
|
+
try {
|
|
948
|
+
row = JSON.parse(line);
|
|
949
|
+
}
|
|
950
|
+
catch {
|
|
951
|
+
continue;
|
|
952
|
+
}
|
|
953
|
+
if (!row || typeof row !== 'object' || row.isSidechain === true)
|
|
954
|
+
continue;
|
|
955
|
+
const content = row.message?.content;
|
|
956
|
+
const blocks = (typeof content === 'string' ? [{ type: 'text', text: content }] : Array.isArray(content) ? content : []);
|
|
957
|
+
if (row.type === 'assistant') {
|
|
958
|
+
if (row.isApiErrorMessage === true || (typeof row.error === 'string' && row.error.length > 0)) {
|
|
959
|
+
state = 'error';
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
for (const b of blocks) {
|
|
963
|
+
if (b?.type === 'text') {
|
|
964
|
+
if (String(b.text ?? '').trim())
|
|
965
|
+
state = 'said';
|
|
966
|
+
}
|
|
967
|
+
// A tool call, or thinking: the model is at work.
|
|
968
|
+
else if (b?.type === 'tool_use' || b?.type === 'thinking' || b?.type === 'redacted_thinking')
|
|
969
|
+
state = 'open';
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
else if (row.type === 'user') {
|
|
973
|
+
if (row.isCompactSummary === true || row.isVisibleInTranscriptOnly === true)
|
|
974
|
+
continue;
|
|
975
|
+
const text = blocks.map((b) => (b?.type === 'text' ? String(b.text ?? '') : '')).join('\n').trim();
|
|
976
|
+
if (row.isMeta === true) {
|
|
977
|
+
if (state === 'command' && !LOCAL_ROW.test(text))
|
|
978
|
+
state = 'open';
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
const results = blocks.filter((b) => b?.type === 'tool_result');
|
|
982
|
+
if (text.startsWith(INTERRUPTED) || results.some((b) => refused(row, b)))
|
|
983
|
+
state = 'none';
|
|
984
|
+
else if (results.length)
|
|
985
|
+
state = 'open';
|
|
986
|
+
else if (LOCAL_ROW.test(text)) {
|
|
987
|
+
if (state === 'command')
|
|
988
|
+
state = 'none';
|
|
989
|
+
}
|
|
990
|
+
else if (COMMAND_ROW.test(text)) {
|
|
991
|
+
if (state === 'none')
|
|
992
|
+
state = 'command';
|
|
993
|
+
}
|
|
994
|
+
// A prompt: typed, or one Claude Code wrote for the model (a hook's feedback, an image).
|
|
995
|
+
else if (text || blocks.length)
|
|
996
|
+
state = 'open';
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
return state;
|
|
1000
|
+
}
|
|
845
1001
|
/** Assistant text and tool calls, in order, from a slice of a Claude Code transcript. */
|
|
846
1002
|
export function transcriptEvents(chunk) {
|
|
847
1003
|
const out = [];
|
package/dist/build.json
CHANGED
|
@@ -737,38 +737,52 @@ export class Orchestrator {
|
|
|
737
737
|
// Delivery can fail on its own - a platform hiccup, a chat that no longer exists. That must not
|
|
738
738
|
// escape the turn: the queue entry would reject and the agent child would be left running with
|
|
739
739
|
// nobody holding it.
|
|
740
|
+
// Set before a line of Angelia's own is sent (the no-answer line, "Denied", "/x done.", the usage
|
|
741
|
+
// limit and its hint): when that send fails, the line is not kept for later.
|
|
742
|
+
let own = false, limited = false;
|
|
743
|
+
const status = (line) => { own = true; return this.reply(i, line); };
|
|
744
|
+
// The CLI often sends its answer as a last progress line too: never deliver it twice.
|
|
745
|
+
const tail = unsent.length && unsent[unsent.length - 1] === result.text ? unsent.slice(0, -1) : unsent;
|
|
746
|
+
const fresh = unsent.length > 0 || result.text !== progress.lastSent;
|
|
740
747
|
try {
|
|
741
|
-
// The CLI often sends its answer as a last progress line too: never deliver it twice.
|
|
742
|
-
const tail = unsent.length && unsent[unsent.length - 1] === result.text ? unsent.slice(0, -1) : unsent;
|
|
743
|
-
const fresh = unsent.length > 0 || result.text !== progress.lastSent;
|
|
744
748
|
if (result.text && isLimitText(result.text)) {
|
|
749
|
+
limited = true;
|
|
745
750
|
this.log(`usage limit key=${key}`);
|
|
746
751
|
if (tail.length)
|
|
747
752
|
await this.replyFromAgent(i, tail.join('\n\n'), profile);
|
|
753
|
+
own = true; // the limit is the CLI's line, not the agent's answer
|
|
748
754
|
if (fresh)
|
|
749
755
|
await this.reply(i, result.text);
|
|
750
|
-
await
|
|
756
|
+
await status(limitHint(profile.backend));
|
|
751
757
|
}
|
|
752
758
|
else if (result.text && fresh)
|
|
753
759
|
await this.replyFromAgent(i, [...tail, result.text].join('\n\n'), profile);
|
|
754
760
|
else if (tail.length)
|
|
755
761
|
await this.replyFromAgent(i, tail.join('\n\n'), profile);
|
|
756
762
|
else if (!result.text && !progress.lastSent && bare)
|
|
757
|
-
await
|
|
763
|
+
await status(`${i.text.trim().split(/\s/)[0]} done.`);
|
|
758
764
|
// Nothing new for the chat at all. After a denial the chat is told whatever was said before it; with
|
|
759
765
|
// no denial, only when nothing at all reached the chat this turn (a progress line is an answer of
|
|
760
766
|
// sorts).
|
|
761
767
|
else if (!result.text && !bare && this.denied.has(key))
|
|
762
|
-
await
|
|
763
|
-
else if (!result.text && !bare && !progress.lastSent)
|
|
764
|
-
|
|
768
|
+
await status(DENIED_LINE);
|
|
769
|
+
else if (!result.text && !bare && !progress.lastSent) {
|
|
770
|
+
// Counted in the log: the line means a turn ended with nothing for the chat, which is either an
|
|
771
|
+
// agent that said nothing or a turn whose end was read wrong (tmux mode logs how it ended).
|
|
772
|
+
this.log(`no answer key=${key}`);
|
|
773
|
+
await status(NO_ANSWER_LINE);
|
|
774
|
+
}
|
|
765
775
|
}
|
|
766
776
|
catch (err) {
|
|
767
777
|
// Kept, and sent with the next thing this chat is told: the answer exists, the platform was down.
|
|
768
|
-
|
|
778
|
+
// Only the agent's words are kept. A line of Angelia's own described the turn as it ended; sent
|
|
779
|
+
// later, under "An answer that could not be delivered earlier", it reads as the agent's answer.
|
|
780
|
+
this.log(`deliver failed key=${key} ${err.message}${own ? ' (a status line of Angelia: dropped, not kept for later)' : ''}`);
|
|
769
781
|
const owed = this.owed.get(key) ?? [];
|
|
770
|
-
|
|
771
|
-
|
|
782
|
+
// A send that failed before any part went out says nothing of what is left: then it is the whole
|
|
783
|
+
// answer. Never the usage limit: of a limited turn only the agent's lines before it are kept.
|
|
784
|
+
const rest = err.unsent ?? (limited ? tail.join('\n\n') : result.text);
|
|
785
|
+
if (rest && !own && owed.length < OWED_MAX)
|
|
772
786
|
this.owed.set(key, [...owed, rest]);
|
|
773
787
|
}
|
|
774
788
|
return undefined;
|
|
@@ -1426,7 +1440,10 @@ export class Orchestrator {
|
|
|
1426
1440
|
}
|
|
1427
1441
|
catch (e) {
|
|
1428
1442
|
this.log(`media failed key=${sessionKey(i)} ${e.message}`);
|
|
1429
|
-
|
|
1443
|
+
// Angelia's own line: when it cannot be sent either it is logged and dropped. Thrown on, it
|
|
1444
|
+
// would be kept as an answer the chat is owed, and posted later as the agent's words.
|
|
1445
|
+
await this.reply(i, e instanceof MediaError ? `Could not attach that file: ${e.message}` : 'Could not attach that file.')
|
|
1446
|
+
.catch((err) => this.log(`media failure line not sent key=${sessionKey(i)} ${err.message}`));
|
|
1430
1447
|
}
|
|
1431
1448
|
}
|
|
1432
1449
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "angelia-gateway",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.28",
|
|
4
4
|
"description": "Turn the coding agent CLI you already know into the brain of your personal assistants, on WhatsApp and Telegram. One chat, one folder, one session.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"claude-code",
|