laohuang 0.4.2 → 0.5.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/README.md +10 -6
- package/dist/agent.d.ts +7 -27
- package/dist/agent.js +53 -567
- package/dist/agent.js.map +1 -1
- package/dist/capabilities.d.ts +19 -0
- package/dist/capabilities.js +29 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/cli.d.ts +9 -6
- package/dist/cli.js +83 -70
- package/dist/cli.js.map +1 -1
- package/dist/commands.d.ts +20 -3
- package/dist/commands.js +39 -38
- package/dist/commands.js.map +1 -1
- package/dist/keybindings/default-keybindings.d.ts +2 -0
- package/dist/keybindings/default-keybindings.js +10 -0
- package/dist/keybindings/default-keybindings.js.map +1 -0
- package/dist/keybindings/hints.d.ts +3 -0
- package/dist/keybindings/hints.js +7 -0
- package/dist/keybindings/hints.js.map +1 -0
- package/dist/keybindings/key-id.d.ts +20 -0
- package/dist/keybindings/key-id.js +11 -0
- package/dist/keybindings/key-id.js.map +1 -0
- package/dist/keybindings/key-parser.d.ts +7 -0
- package/dist/keybindings/key-parser.js +92 -0
- package/dist/keybindings/key-parser.js.map +1 -0
- package/dist/keybindings/keybindings.d.ts +21 -0
- package/dist/keybindings/keybindings.js +65 -0
- package/dist/keybindings/keybindings.js.map +1 -0
- package/dist/routing.d.ts +2 -57
- package/dist/routing.js +3 -161
- package/dist/routing.js.map +1 -1
- package/dist/runtime/agent-step-runner.d.ts +51 -0
- package/dist/runtime/agent-step-runner.js +361 -0
- package/dist/runtime/agent-step-runner.js.map +1 -0
- package/dist/runtime/agent-turn-loop.d.ts +36 -0
- package/dist/runtime/agent-turn-loop.js +71 -0
- package/dist/runtime/agent-turn-loop.js.map +1 -0
- package/dist/runtime/guard-policy.d.ts +21 -0
- package/dist/runtime/guard-policy.js +93 -0
- package/dist/runtime/guard-policy.js.map +1 -0
- package/dist/runtime/history-committer.d.ts +34 -0
- package/dist/runtime/history-committer.js +77 -0
- package/dist/runtime/history-committer.js.map +1 -0
- package/dist/runtime/human-intent-router.d.ts +5 -0
- package/dist/runtime/human-intent-router.js +38 -0
- package/dist/runtime/human-intent-router.js.map +1 -0
- package/dist/runtime/model-runtime.d.ts +33 -0
- package/dist/runtime/model-runtime.js +70 -0
- package/dist/runtime/model-runtime.js.map +1 -0
- package/dist/runtime/queue-bridge.d.ts +11 -0
- package/dist/runtime/queue-bridge.js +2 -0
- package/dist/runtime/queue-bridge.js.map +1 -0
- package/dist/runtime/queue-dispatcher.d.ts +45 -0
- package/dist/runtime/queue-dispatcher.js +120 -0
- package/dist/runtime/queue-dispatcher.js.map +1 -0
- package/dist/runtime/runtime-events.d.ts +26 -0
- package/dist/runtime/runtime-events.js +21 -0
- package/dist/runtime/runtime-events.js.map +1 -0
- package/dist/runtime/session-action-protocol.d.ts +10 -0
- package/dist/runtime/session-action-protocol.js +64 -0
- package/dist/runtime/session-action-protocol.js.map +1 -0
- package/dist/runtime/session-action.d.ts +42 -0
- package/dist/runtime/session-action.js +3 -0
- package/dist/runtime/session-action.js.map +1 -0
- package/dist/runtime/task-lifecycle.d.ts +73 -0
- package/dist/runtime/task-lifecycle.js +267 -0
- package/dist/runtime/task-lifecycle.js.map +1 -0
- package/dist/runtime/tool-runtime.d.ts +33 -0
- package/dist/runtime/tool-runtime.js +89 -0
- package/dist/runtime/tool-runtime.js.map +1 -0
- package/dist/runtime/user-intent.d.ts +48 -0
- package/dist/runtime/user-intent.js +17 -0
- package/dist/runtime/user-intent.js.map +1 -0
- package/dist/session.d.ts +19 -9
- package/dist/session.js +119 -169
- package/dist/session.js.map +1 -1
- package/dist/terminal/editor.d.ts +16 -5
- package/dist/terminal/editor.js +99 -4
- package/dist/terminal/editor.js.map +1 -1
- package/dist/terminal/input.d.ts +2 -0
- package/dist/terminal/input.js +32 -7
- package/dist/terminal/input.js.map +1 -1
- package/dist/terminal/ui.d.ts +32 -21
- package/dist/terminal/ui.js +413 -252
- package/dist/terminal/ui.js.map +1 -1
- package/dist/tools.d.ts +7 -0
- package/dist/tools.js.map +1 -1
- package/dist/tui/components.d.ts +5 -0
- package/dist/tui/components.js +12 -0
- package/dist/tui/components.js.map +1 -0
- package/dist/tui/focus-manager.d.ts +7 -0
- package/dist/tui/focus-manager.js +13 -0
- package/dist/tui/focus-manager.js.map +1 -0
- package/dist/tui/overlay-manager.d.ts +12 -0
- package/dist/tui/overlay-manager.js +27 -0
- package/dist/tui/overlay-manager.js.map +1 -0
- package/dist/ui/display-actions.d.ts +23 -0
- package/dist/ui/display-actions.js +15 -0
- package/dist/ui/display-actions.js.map +1 -0
- package/dist/ui/display-policy.d.ts +38 -0
- package/dist/ui/display-policy.js +97 -0
- package/dist/ui/display-policy.js.map +1 -0
- package/dist/ui/frame-builder.d.ts +39 -0
- package/dist/ui/frame-builder.js +111 -0
- package/dist/ui/frame-builder.js.map +1 -0
- package/dist/ui/transcript-store.d.ts +35 -0
- package/dist/ui/transcript-store.js +172 -0
- package/dist/ui/transcript-store.js.map +1 -0
- package/dist/ui-state.js +6 -6
- package/dist/ui-state.js.map +1 -1
- package/dist/web.js +14 -1
- package/dist/web.js.map +1 -1
- package/package.json +2 -2
package/dist/terminal/ui.js
CHANGED
|
@@ -17,10 +17,32 @@
|
|
|
17
17
|
*/
|
|
18
18
|
var _a, _b;
|
|
19
19
|
import { appendFileSync } from "node:fs";
|
|
20
|
+
export { toTuiInputEvent } from "./editor.js";
|
|
21
|
+
import { ACTION_CAPABILITIES, DEFAULT_RUNTIME_CAPABILITIES, unavailableActionNotice, } from "../capabilities.js";
|
|
22
|
+
import { DEFAULT_KEYBINDINGS } from "../keybindings/default-keybindings.js";
|
|
23
|
+
import { makeKeyInput } from "../keybindings/key-id.js";
|
|
24
|
+
import { KeybindingsManager, } from "../keybindings/keybindings.js";
|
|
20
25
|
import { createUIState, UIEventReducer, } from "../ui-state.js";
|
|
26
|
+
import { DisplayPolicy, displayGapMessage, } from "../ui/display-policy.js";
|
|
27
|
+
import { TranscriptStore, createTranscriptBlock, } from "../ui/transcript-store.js";
|
|
28
|
+
import { FrameBuilder } from "../ui/frame-builder.js";
|
|
21
29
|
import { renderMarkdownLines } from "./markdown.js";
|
|
22
30
|
import { resolveTerminalTheme } from "./theme.js";
|
|
23
31
|
import { PiMainScreenRenderer, charCellWidth, truncateToWidth, visibleWidth, wrapTextToWidth, } from "./screen.js";
|
|
32
|
+
import { COMPLETION_OVERLAY, COMPOSER_COMPONENT } from "../tui/components.js";
|
|
33
|
+
import { FocusManager } from "../tui/focus-manager.js";
|
|
34
|
+
import { OverlayManager } from "../tui/overlay-manager.js";
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Shared input contracts (implemented by terminal/input.ts once landed)
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
const WELCOME_TEXT = "hello, welcome to laoHuang";
|
|
39
|
+
const KITTY_LOCK_MODIFIER_MASK = 64 | 128;
|
|
40
|
+
function matchesKittyModifiers(modifier, expected) {
|
|
41
|
+
return (modifier & ~KITTY_LOCK_MODIFIER_MASK) === expected;
|
|
42
|
+
}
|
|
43
|
+
function isKittyPressEvent(eventType) {
|
|
44
|
+
return eventType === undefined || eventType === "1";
|
|
45
|
+
}
|
|
24
46
|
// ---------------------------------------------------------------------------
|
|
25
47
|
// BasicEditorState — default line editor behind the raw loop
|
|
26
48
|
// ---------------------------------------------------------------------------
|
|
@@ -102,7 +124,9 @@ export class BasicEditorState {
|
|
|
102
124
|
if (action.kind === "submit") {
|
|
103
125
|
if (this.completionVisible) {
|
|
104
126
|
this.#acceptCompletion();
|
|
105
|
-
|
|
127
|
+
if (!this.text.startsWith("/")) {
|
|
128
|
+
return {};
|
|
129
|
+
}
|
|
106
130
|
}
|
|
107
131
|
return this.#submit();
|
|
108
132
|
}
|
|
@@ -302,12 +326,17 @@ const CSI_ARROW_ACTIONS = {
|
|
|
302
326
|
"\x1bOD": "cursor_left",
|
|
303
327
|
};
|
|
304
328
|
const CONTROL_ACTIONS = {
|
|
305
|
-
3: "cancel",
|
|
306
329
|
4: "eof",
|
|
307
330
|
9: "complete",
|
|
308
331
|
8: "backspace",
|
|
309
332
|
127: "backspace",
|
|
310
333
|
};
|
|
334
|
+
const CONTROL_KEYS = {
|
|
335
|
+
3: makeKeyInput("ctrl_c", { ctrl: true }),
|
|
336
|
+
12: makeKeyInput("ctrl_l", { ctrl: true }),
|
|
337
|
+
15: makeKeyInput("character", { text: "o", ctrl: true }),
|
|
338
|
+
20: makeKeyInput("character", { text: "t", ctrl: true }),
|
|
339
|
+
};
|
|
311
340
|
const SPECIAL_ESCAPE_ACTIONS = {
|
|
312
341
|
"\x1b[13;2u": "newline",
|
|
313
342
|
"\x1b[57414;2u": "newline",
|
|
@@ -466,6 +495,12 @@ export class BasicInputDecoder {
|
|
|
466
495
|
}
|
|
467
496
|
continue;
|
|
468
497
|
}
|
|
498
|
+
const key = CONTROL_KEYS[byte];
|
|
499
|
+
if (key !== undefined) {
|
|
500
|
+
index += 1;
|
|
501
|
+
actions.push({ kind: "key", key });
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
469
504
|
const control = CONTROL_ACTIONS[byte];
|
|
470
505
|
if (control !== undefined) {
|
|
471
506
|
index += 1;
|
|
@@ -497,7 +532,10 @@ export class BasicInputDecoder {
|
|
|
497
532
|
}
|
|
498
533
|
const second = bytes[start + 1];
|
|
499
534
|
if (second === 10 || second === 13) {
|
|
500
|
-
return {
|
|
535
|
+
return {
|
|
536
|
+
next: start + 2,
|
|
537
|
+
action: { kind: "key", key: makeKeyInput("enter", { alt: true }) },
|
|
538
|
+
};
|
|
501
539
|
}
|
|
502
540
|
if (second === 0x5b) {
|
|
503
541
|
let final = -1;
|
|
@@ -552,6 +590,13 @@ export class BasicInputDecoder {
|
|
|
552
590
|
// Other negotiation responses (or their abandoned tails) are swallowed.
|
|
553
591
|
return undefined;
|
|
554
592
|
}
|
|
593
|
+
if (sequence === "\x1b[Z") {
|
|
594
|
+
return { kind: "key", key: makeKeyInput("tab", { shift: true }) };
|
|
595
|
+
}
|
|
596
|
+
const modifiedControl = this.#modifiedControlKey(sequence);
|
|
597
|
+
if (modifiedControl !== undefined) {
|
|
598
|
+
return modifiedControl;
|
|
599
|
+
}
|
|
555
600
|
const special = SPECIAL_ESCAPE_ACTIONS[sequence];
|
|
556
601
|
if (special !== undefined) {
|
|
557
602
|
return { kind: special };
|
|
@@ -607,6 +652,37 @@ export class BasicInputDecoder {
|
|
|
607
652
|
}
|
|
608
653
|
return undefined;
|
|
609
654
|
}
|
|
655
|
+
#modifiedControlKey(sequence) {
|
|
656
|
+
const kitty = /^\x1b\[(13|57414|9);(\d+)(?::(\d+))?u$/.exec(sequence);
|
|
657
|
+
if (kitty !== null) {
|
|
658
|
+
const code = kitty[1];
|
|
659
|
+
const modifier = Number.parseInt(kitty[2], 10) - 1;
|
|
660
|
+
if (!isKittyPressEvent(kitty[3])) {
|
|
661
|
+
return undefined;
|
|
662
|
+
}
|
|
663
|
+
if ((code === "13" || code === "57414")
|
|
664
|
+
&& matchesKittyModifiers(modifier, 2)) {
|
|
665
|
+
return { kind: "key", key: makeKeyInput("enter", { alt: true }) };
|
|
666
|
+
}
|
|
667
|
+
if (code === "9" && matchesKittyModifiers(modifier, 1)) {
|
|
668
|
+
return { kind: "key", key: makeKeyInput("tab", { shift: true }) };
|
|
669
|
+
}
|
|
670
|
+
return undefined;
|
|
671
|
+
}
|
|
672
|
+
const modifyOtherKeys = /^\x1b\[27;(\d+);(\d+)~$/.exec(sequence);
|
|
673
|
+
if (modifyOtherKeys === null) {
|
|
674
|
+
return undefined;
|
|
675
|
+
}
|
|
676
|
+
const modifier = Number.parseInt(modifyOtherKeys[1], 10) - 1;
|
|
677
|
+
const codepoint = Number.parseInt(modifyOtherKeys[2], 10);
|
|
678
|
+
if (codepoint === 13 && modifier === 2) {
|
|
679
|
+
return { kind: "key", key: makeKeyInput("enter", { alt: true }) };
|
|
680
|
+
}
|
|
681
|
+
if (codepoint === 9 && modifier === 1) {
|
|
682
|
+
return { kind: "key", key: makeKeyInput("tab", { shift: true }) };
|
|
683
|
+
}
|
|
684
|
+
return undefined;
|
|
685
|
+
}
|
|
610
686
|
#isKittyRelease(sequence) {
|
|
611
687
|
if (sequence.includes("\x1b[200~")) {
|
|
612
688
|
return false;
|
|
@@ -614,21 +690,7 @@ export class BasicInputDecoder {
|
|
|
614
690
|
return [":3u", ":3~", ":3A", ":3B", ":3C", ":3D", ":3H", ":3F"].some((marker) => sequence.includes(marker));
|
|
615
691
|
}
|
|
616
692
|
}
|
|
617
|
-
export
|
|
618
|
-
return {
|
|
619
|
-
kind,
|
|
620
|
-
key,
|
|
621
|
-
text: fields.text ?? "",
|
|
622
|
-
mutable: fields.mutable ?? false,
|
|
623
|
-
name: fields.name ?? "",
|
|
624
|
-
subject: fields.subject ?? "",
|
|
625
|
-
status: fields.status ?? "",
|
|
626
|
-
exitCode: fields.exitCode ?? null,
|
|
627
|
-
durationMs: fields.durationMs ?? null,
|
|
628
|
-
streamError: fields.streamError ?? "",
|
|
629
|
-
style: fields.style ?? "",
|
|
630
|
-
};
|
|
631
|
-
}
|
|
693
|
+
export { createTranscriptBlock } from "../ui/transcript-store.js";
|
|
632
694
|
/** Local command feedback sharing the loop's event queue. */
|
|
633
695
|
class LocalMessage {
|
|
634
696
|
text;
|
|
@@ -641,40 +703,9 @@ class LocalMessage {
|
|
|
641
703
|
function isRecord(value) {
|
|
642
704
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
643
705
|
}
|
|
644
|
-
/** Keep captured tool stdout out of user-facing terminal render queues. */
|
|
645
|
-
function isHiddenToolStdout(event) {
|
|
646
|
-
if (!isRecord(event)) {
|
|
647
|
-
return false;
|
|
648
|
-
}
|
|
649
|
-
if (String(event.kind ?? "") !== "tool.output_delta") {
|
|
650
|
-
return false;
|
|
651
|
-
}
|
|
652
|
-
const payload = event.payload;
|
|
653
|
-
return isRecord(payload) && String(payload.stream ?? "stdout") === "stdout";
|
|
654
|
-
}
|
|
655
|
-
function eventKind(event) {
|
|
656
|
-
return isRecord(event) ? String(event.kind ?? "") : "";
|
|
657
|
-
}
|
|
658
|
-
function eventPayload(event) {
|
|
659
|
-
return isRecord(event) && isRecord(event.payload) ? event.payload : {};
|
|
660
|
-
}
|
|
661
|
-
/** Mirror Python's `int(...)`: truncate numbers, parse numeric strings. */
|
|
662
|
-
function toInt(value) {
|
|
663
|
-
const parsed = typeof value === "string" ? Number.parseInt(value, 10) : Number(value);
|
|
664
|
-
return Number.isFinite(parsed) ? Math.trunc(parsed) : 0;
|
|
665
|
-
}
|
|
666
|
-
function projectionDropped(event) {
|
|
667
|
-
const payload = eventPayload(event);
|
|
668
|
-
return toInt(payload._projection_dropped ?? 0);
|
|
669
|
-
}
|
|
670
706
|
function clip(value, limit) {
|
|
671
707
|
return value.length <= limit ? value : `${value.slice(0, limit)}…`;
|
|
672
708
|
}
|
|
673
|
-
const HIGH_FREQUENCY_KINDS = new Set([
|
|
674
|
-
"model.reasoning_delta",
|
|
675
|
-
"model.tool_call_delta",
|
|
676
|
-
"tool.output_delta",
|
|
677
|
-
]);
|
|
678
709
|
// ---------------------------------------------------------------------------
|
|
679
710
|
// ANSI style helpers (rich-style "italic #rrggbb" / "bg:#rrggbb" strings)
|
|
680
711
|
// ---------------------------------------------------------------------------
|
|
@@ -755,6 +786,48 @@ function ansiStyledText(style, text) {
|
|
|
755
786
|
}
|
|
756
787
|
return `\x1b[${codes.join(";")}m${text}\x1b[0m`;
|
|
757
788
|
}
|
|
789
|
+
function inputEventFromAction(action) {
|
|
790
|
+
if (action.kind === "insert") {
|
|
791
|
+
return { type: "text", text: action.text ?? "" };
|
|
792
|
+
}
|
|
793
|
+
if (action.kind === "key") {
|
|
794
|
+
if (action.key === undefined) {
|
|
795
|
+
throw new Error("key input action is missing its key");
|
|
796
|
+
}
|
|
797
|
+
return { type: "key", key: action.key };
|
|
798
|
+
}
|
|
799
|
+
const key = action.kind === "submit" ? makeKeyInput("enter")
|
|
800
|
+
: action.kind === "newline" ? makeKeyInput("enter", { alt: true })
|
|
801
|
+
: action.kind === "complete" ? makeKeyInput("tab")
|
|
802
|
+
: action.kind === "history_up" ? makeKeyInput("up")
|
|
803
|
+
: action.kind === "history_down" ? makeKeyInput("down")
|
|
804
|
+
: action.kind === "cursor_left" ? makeKeyInput("left")
|
|
805
|
+
: action.kind === "cursor_right" ? makeKeyInput("right")
|
|
806
|
+
: action.kind === "backspace" ? makeKeyInput("backspace")
|
|
807
|
+
: action.kind === "dismiss" ? makeKeyInput("escape")
|
|
808
|
+
: action.kind === "cancel" ? makeKeyInput("ctrl_c", { ctrl: true })
|
|
809
|
+
: makeKeyInput("ctrl_d", { ctrl: true });
|
|
810
|
+
return { type: "key", key };
|
|
811
|
+
}
|
|
812
|
+
function editorActionForKey(key) {
|
|
813
|
+
if (key.id === "enter")
|
|
814
|
+
return { kind: "submit" };
|
|
815
|
+
if (key.id === "tab")
|
|
816
|
+
return { kind: "complete" };
|
|
817
|
+
if (key.id === "up")
|
|
818
|
+
return { kind: "history_up" };
|
|
819
|
+
if (key.id === "down")
|
|
820
|
+
return { kind: "history_down" };
|
|
821
|
+
if (key.id === "left")
|
|
822
|
+
return { kind: "cursor_left" };
|
|
823
|
+
if (key.id === "right")
|
|
824
|
+
return { kind: "cursor_right" };
|
|
825
|
+
if (key.id === "backspace")
|
|
826
|
+
return { kind: "backspace" };
|
|
827
|
+
if (key.id === "ctrl_d")
|
|
828
|
+
return { kind: "eof" };
|
|
829
|
+
return null;
|
|
830
|
+
}
|
|
758
831
|
/** Serialize stdin, UI events, and all terminal writes in one loop. */
|
|
759
832
|
export class InteractiveTerminalLoop {
|
|
760
833
|
static ESCAPE_TIMEOUT_MS = 50;
|
|
@@ -765,6 +838,8 @@ export class InteractiveTerminalLoop {
|
|
|
765
838
|
#editor;
|
|
766
839
|
#decoder;
|
|
767
840
|
#renderer;
|
|
841
|
+
#overlays = new OverlayManager();
|
|
842
|
+
#focus = new FocusManager(this.#overlays, COMPOSER_COMPONENT);
|
|
768
843
|
#onSubmit = () => { };
|
|
769
844
|
#exitRequested = false;
|
|
770
845
|
#closed = false;
|
|
@@ -808,14 +883,18 @@ export class InteractiveTerminalLoop {
|
|
|
808
883
|
this.#running = true;
|
|
809
884
|
}
|
|
810
885
|
publishEvent(event) {
|
|
811
|
-
if (this.#closed
|
|
886
|
+
if (this.#closed) {
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
if (!this.#ui.shouldQueueDisplayEvent(event)) {
|
|
812
890
|
return;
|
|
813
891
|
}
|
|
814
892
|
if (this.#work.length >= _a.WORK_QUEUE_LIMIT - 128 &&
|
|
815
|
-
|
|
893
|
+
this.#ui.isHighFrequencyDisplayEvent(event)) {
|
|
894
|
+
this.#ui.recordDisplayDrop(event);
|
|
816
895
|
return;
|
|
817
896
|
}
|
|
818
|
-
this.#work.push({ type: "event", event });
|
|
897
|
+
this.#work.push({ type: "event", event: this.#ui.withDisplayDropMarker(event) });
|
|
819
898
|
this.#scheduleWakeup();
|
|
820
899
|
}
|
|
821
900
|
feedInputBytes(data) {
|
|
@@ -828,10 +907,15 @@ export class InteractiveTerminalLoop {
|
|
|
828
907
|
/** Synchronously consume queued work; this is also the test hook. */
|
|
829
908
|
drain() {
|
|
830
909
|
const changed = this.#drainWork();
|
|
831
|
-
|
|
910
|
+
const dropped = this.#ui.flushDisplayDropMarker();
|
|
911
|
+
if (changed || dropped || this.#needsRender) {
|
|
832
912
|
this.#render();
|
|
833
913
|
}
|
|
834
914
|
}
|
|
915
|
+
requestRender() {
|
|
916
|
+
this.#needsRender = true;
|
|
917
|
+
this.#scheduleWakeup();
|
|
918
|
+
}
|
|
835
919
|
ask(message, options = {}) {
|
|
836
920
|
if (this.#closed || !this.#running) {
|
|
837
921
|
return Promise.resolve("");
|
|
@@ -996,6 +1080,9 @@ export class InteractiveTerminalLoop {
|
|
|
996
1080
|
while (item !== undefined) {
|
|
997
1081
|
if (item.type === "input") {
|
|
998
1082
|
this.#applyActions(this.#decoder.feed(item.data));
|
|
1083
|
+
if (!this.#wakeupEnabled) {
|
|
1084
|
+
this.#applyActions(this.#decoder.flush());
|
|
1085
|
+
}
|
|
999
1086
|
changed = true;
|
|
1000
1087
|
}
|
|
1001
1088
|
else if (item.event instanceof LocalMessage) {
|
|
@@ -1031,18 +1118,114 @@ export class InteractiveTerminalLoop {
|
|
|
1031
1118
|
}
|
|
1032
1119
|
#applyActions(actions) {
|
|
1033
1120
|
for (const action of actions) {
|
|
1034
|
-
if (
|
|
1035
|
-
this.#
|
|
1121
|
+
if (action.kind === "newline") {
|
|
1122
|
+
this.#applyEditorAction(action);
|
|
1036
1123
|
continue;
|
|
1037
1124
|
}
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1125
|
+
this.#applyInputEvent(inputEventFromAction(action));
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
#applyInputEvent(event) {
|
|
1129
|
+
if (event.type === "text" || event.type === "paste") {
|
|
1130
|
+
this.#applyEditorAction({ kind: "insert", text: event.text });
|
|
1131
|
+
return;
|
|
1132
|
+
}
|
|
1133
|
+
const action = this.#ui.keybindings.resolve(event.key, ["terminal", "editor"]);
|
|
1134
|
+
if (action !== null) {
|
|
1135
|
+
this.#applyKeyAction(action);
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
const editorAction = editorActionForKey(event.key);
|
|
1139
|
+
if (editorAction !== null) {
|
|
1140
|
+
this.#applyEditorAction(editorAction);
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
#applyKeyAction(action) {
|
|
1144
|
+
const capability = ACTION_CAPABILITIES[action];
|
|
1145
|
+
if (capability !== undefined && !this.#ui.capabilities[capability]) {
|
|
1146
|
+
this.#appendNotice(unavailableActionNotice(action));
|
|
1147
|
+
this.#needsRender = true;
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
if (action === "editor_newline") {
|
|
1151
|
+
this.#applyEditorAction({ kind: "newline" });
|
|
1152
|
+
}
|
|
1153
|
+
else if (action === "submit_follow_up") {
|
|
1154
|
+
this.#applyFollowUpSubmit();
|
|
1155
|
+
}
|
|
1156
|
+
else if (action === "dismiss") {
|
|
1157
|
+
this.#applyEditorAction({ kind: "dismiss" });
|
|
1158
|
+
}
|
|
1159
|
+
else if (action === "cancel") {
|
|
1160
|
+
this.#applyEditorAction({ kind: "cancel" });
|
|
1161
|
+
}
|
|
1162
|
+
else if (action === "toggle_tool_output") {
|
|
1163
|
+
this.#ui.toggleToolOutputFromKeybinding();
|
|
1164
|
+
this.#needsRender = true;
|
|
1165
|
+
}
|
|
1166
|
+
else {
|
|
1167
|
+
this.#ui.handleKeyAction(action);
|
|
1043
1168
|
this.#needsRender = true;
|
|
1044
1169
|
}
|
|
1045
1170
|
}
|
|
1171
|
+
#applyFollowUpSubmit() {
|
|
1172
|
+
if (this.#applyQuestionAction({ kind: "submit" })) {
|
|
1173
|
+
this.#needsRender = true;
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
if (this.#applyCompletionAction({ kind: "submit" })) {
|
|
1177
|
+
this.#needsRender = true;
|
|
1178
|
+
return;
|
|
1179
|
+
}
|
|
1180
|
+
const effect = this.#editor.apply({ kind: "submit" }, { runtimeActive: this.#ui.isRunning() });
|
|
1181
|
+
if (effect.submit !== null && effect.submit !== undefined) {
|
|
1182
|
+
this.#ui.acceptUserInput(effect.submit);
|
|
1183
|
+
this.#onSubmit(effect.submit, { strategy: "follow_up" });
|
|
1184
|
+
}
|
|
1185
|
+
if (effect.notice) {
|
|
1186
|
+
this.#appendNotice(effect.notice);
|
|
1187
|
+
}
|
|
1188
|
+
if (effect.cancelRequested) {
|
|
1189
|
+
this.#ui.cancelFromKeybinding();
|
|
1190
|
+
}
|
|
1191
|
+
if (effect.exitRequested) {
|
|
1192
|
+
this.requestExit();
|
|
1193
|
+
}
|
|
1194
|
+
this.#syncCompletionOverlay();
|
|
1195
|
+
this.#needsRender = true;
|
|
1196
|
+
}
|
|
1197
|
+
#applyEditorAction(action) {
|
|
1198
|
+
if (this.#applyQuestionAction(action)) {
|
|
1199
|
+
this.#needsRender = true;
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
if (this.#applyCompletionAction(action)) {
|
|
1203
|
+
this.#needsRender = true;
|
|
1204
|
+
return;
|
|
1205
|
+
}
|
|
1206
|
+
const effect = this.#editor.apply(action, {
|
|
1207
|
+
runtimeActive: this.#ui.isRunning(),
|
|
1208
|
+
});
|
|
1209
|
+
this.#applyEffect(effect);
|
|
1210
|
+
this.#refreshCompletions();
|
|
1211
|
+
this.#needsRender = true;
|
|
1212
|
+
}
|
|
1213
|
+
/** Completion owns navigation and acceptance while text edits stay in the composer. */
|
|
1214
|
+
#applyCompletionAction(action) {
|
|
1215
|
+
if (this.#focus.current() !== COMPLETION_OVERLAY.id) {
|
|
1216
|
+
return false;
|
|
1217
|
+
}
|
|
1218
|
+
if (action.kind !== "dismiss" &&
|
|
1219
|
+
action.kind !== "history_up" &&
|
|
1220
|
+
action.kind !== "history_down" &&
|
|
1221
|
+
action.kind !== "complete" &&
|
|
1222
|
+
action.kind !== "submit") {
|
|
1223
|
+
return false;
|
|
1224
|
+
}
|
|
1225
|
+
this.#applyEffect(this.#editor.apply(action, { runtimeActive: this.#ui.isRunning() }));
|
|
1226
|
+
this.#syncCompletionOverlay();
|
|
1227
|
+
return true;
|
|
1228
|
+
}
|
|
1046
1229
|
#applyEof() {
|
|
1047
1230
|
const effect = this.#editor.apply({ kind: "eof" }, { runtimeActive: this.#ui.isRunning() });
|
|
1048
1231
|
this.#applyEffect(effect);
|
|
@@ -1057,10 +1240,7 @@ export class InteractiveTerminalLoop {
|
|
|
1057
1240
|
this.#ui.cancelFromKeybinding();
|
|
1058
1241
|
}
|
|
1059
1242
|
if (effect.notice) {
|
|
1060
|
-
this.#
|
|
1061
|
-
text: effect.notice,
|
|
1062
|
-
style: "yellow",
|
|
1063
|
-
}));
|
|
1243
|
+
this.#appendNotice(effect.notice);
|
|
1064
1244
|
}
|
|
1065
1245
|
if (effect.exitRequested) {
|
|
1066
1246
|
this.requestExit();
|
|
@@ -1090,22 +1270,27 @@ export class InteractiveTerminalLoop {
|
|
|
1090
1270
|
}
|
|
1091
1271
|
const effect = this.#editor.apply(action, { runtimeActive: false });
|
|
1092
1272
|
if (effect.notice) {
|
|
1093
|
-
this.#
|
|
1094
|
-
text: effect.notice,
|
|
1095
|
-
style: "yellow",
|
|
1096
|
-
}));
|
|
1273
|
+
this.#appendNotice(effect.notice);
|
|
1097
1274
|
}
|
|
1098
1275
|
return true;
|
|
1099
1276
|
}
|
|
1277
|
+
#appendNotice(text) {
|
|
1278
|
+
this.#ui.appendTranscript(createTranscriptBlock("notice", this.#ui.newBlockId(), {
|
|
1279
|
+
text,
|
|
1280
|
+
style: "yellow",
|
|
1281
|
+
}));
|
|
1282
|
+
}
|
|
1100
1283
|
#resetEditor() {
|
|
1101
1284
|
this.#editor.text = "";
|
|
1102
1285
|
this.#editor.cursor = 0;
|
|
1103
1286
|
this.#editor.historyIndex = null;
|
|
1104
1287
|
this.#editor.setCompletions([]);
|
|
1288
|
+
this.#syncCompletionOverlay();
|
|
1105
1289
|
}
|
|
1106
1290
|
#refreshCompletions() {
|
|
1107
1291
|
if (this.#question !== null) {
|
|
1108
1292
|
this.#editor.setCompletions([]);
|
|
1293
|
+
this.#syncCompletionOverlay();
|
|
1109
1294
|
return;
|
|
1110
1295
|
}
|
|
1111
1296
|
const registry = this.#ui.commandRegistry;
|
|
@@ -1114,6 +1299,15 @@ export class InteractiveTerminalLoop {
|
|
|
1114
1299
|
state: this.#ui.isRunning() ? "RUNNING_MODEL" : this.#ui.state.sessionState,
|
|
1115
1300
|
}));
|
|
1116
1301
|
}
|
|
1302
|
+
this.#syncCompletionOverlay();
|
|
1303
|
+
}
|
|
1304
|
+
#syncCompletionOverlay() {
|
|
1305
|
+
if (this.#editor.completions.length > 0) {
|
|
1306
|
+
this.#overlays.open(COMPLETION_OVERLAY);
|
|
1307
|
+
}
|
|
1308
|
+
else {
|
|
1309
|
+
this.#overlays.close(COMPLETION_OVERLAY.id);
|
|
1310
|
+
}
|
|
1117
1311
|
}
|
|
1118
1312
|
}
|
|
1119
1313
|
_a = InteractiveTerminalLoop;
|
|
@@ -1149,26 +1343,46 @@ export class TerminalUI {
|
|
|
1149
1343
|
provider;
|
|
1150
1344
|
model;
|
|
1151
1345
|
dashboardUrl;
|
|
1346
|
+
capabilities;
|
|
1347
|
+
keybindings;
|
|
1152
1348
|
#output;
|
|
1153
1349
|
#askFallback;
|
|
1154
1350
|
#loop = null;
|
|
1155
|
-
#transcript
|
|
1156
|
-
#
|
|
1157
|
-
|
|
1351
|
+
#transcript;
|
|
1352
|
+
#displayPolicy = new DisplayPolicy({
|
|
1353
|
+
audience: "terminal",
|
|
1354
|
+
foldToolOutput: false,
|
|
1355
|
+
});
|
|
1356
|
+
#frameBuilder;
|
|
1357
|
+
#pendingDisplayDrops = 0;
|
|
1358
|
+
#keyActionCallback;
|
|
1158
1359
|
constructor(options = {}) {
|
|
1159
1360
|
this.theme = resolveTerminalTheme(options.theme);
|
|
1160
1361
|
this.projectRoot = options.projectRoot ?? null;
|
|
1161
1362
|
this.provider = options.provider ?? null;
|
|
1162
1363
|
this.model = options.model ?? null;
|
|
1163
1364
|
this.dashboardUrl = options.dashboardUrl ?? null;
|
|
1365
|
+
this.capabilities = { ...DEFAULT_RUNTIME_CAPABILITIES, ...options.capabilities };
|
|
1366
|
+
this.keybindings = new KeybindingsManager(DEFAULT_KEYBINDINGS, options.keybindingOverrides);
|
|
1164
1367
|
this.commandRegistry = options.commandRegistry ?? null;
|
|
1165
1368
|
this.cancelCallback = options.cancelCallback ?? null;
|
|
1166
1369
|
this.#output = options.output ?? ((text) => console.log(text));
|
|
1167
1370
|
this.#askFallback = options.askFallback ?? null;
|
|
1371
|
+
this.#keyActionCallback = options.keyActionCallback ?? null;
|
|
1168
1372
|
this.state = createUIState();
|
|
1169
1373
|
this.state.provider = options.provider ?? "";
|
|
1170
1374
|
this.state.model = options.model ?? "";
|
|
1171
1375
|
this.reducer = new UIEventReducer(this.state);
|
|
1376
|
+
this.#transcript = new TranscriptStore({
|
|
1377
|
+
errorStyle: `bold ${this.theme.color("error")}`,
|
|
1378
|
+
});
|
|
1379
|
+
this.#frameBuilder = new FrameBuilder({
|
|
1380
|
+
state: this.state,
|
|
1381
|
+
transcript: this.#transcript,
|
|
1382
|
+
projectRoot: this.projectRoot,
|
|
1383
|
+
provider: this.provider,
|
|
1384
|
+
model: this.model,
|
|
1385
|
+
});
|
|
1172
1386
|
if (options.driver) {
|
|
1173
1387
|
const editorFactory = options.editorFactory ?? (() => new BasicEditorState());
|
|
1174
1388
|
const decoderFactory = options.decoderFactory ?? ((hooks) => new BasicInputDecoder(hooks));
|
|
@@ -1244,12 +1458,88 @@ export class TerminalUI {
|
|
|
1244
1458
|
setRuntimeRunningCallback(callback) {
|
|
1245
1459
|
this.runtimeRunningCallback = callback;
|
|
1246
1460
|
}
|
|
1461
|
+
setKeyActionCallback(callback) {
|
|
1462
|
+
this.#keyActionCallback = callback;
|
|
1463
|
+
}
|
|
1464
|
+
handleKeyAction(action) {
|
|
1465
|
+
if (this.#keyActionCallback === null) {
|
|
1466
|
+
this.write(`Key action is unavailable: ${action}.`);
|
|
1467
|
+
return;
|
|
1468
|
+
}
|
|
1469
|
+
this.#keyActionCallback(action);
|
|
1470
|
+
}
|
|
1471
|
+
toggleToolOutputFromKeybinding() {
|
|
1472
|
+
this.applyDisplayAction({
|
|
1473
|
+
type: "toggle_tool_output",
|
|
1474
|
+
expanded: !this.#transcript.toolOutputExpanded(),
|
|
1475
|
+
});
|
|
1476
|
+
}
|
|
1247
1477
|
isRunning() {
|
|
1248
1478
|
if (this.runtimeRunningCallback !== null) {
|
|
1249
1479
|
return Boolean(this.runtimeRunningCallback());
|
|
1250
1480
|
}
|
|
1251
1481
|
return ["RUNNING_MODEL", "RUNNING_TOOL", "RUNNING_TOOLS", "CANCELLING"].includes(this.state.sessionState);
|
|
1252
1482
|
}
|
|
1483
|
+
shouldQueueDisplayEvent(event) {
|
|
1484
|
+
if (!isRecord(event) || !("kind" in event)) {
|
|
1485
|
+
return true;
|
|
1486
|
+
}
|
|
1487
|
+
return this.#displayPolicy.shouldQueue({
|
|
1488
|
+
kind: event.kind,
|
|
1489
|
+
payload: event.payload,
|
|
1490
|
+
correlation_id: event.correlation_id,
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
isHighFrequencyDisplayEvent(event) {
|
|
1494
|
+
if (!isRecord(event) || !("kind" in event)) {
|
|
1495
|
+
return false;
|
|
1496
|
+
}
|
|
1497
|
+
return this.#displayPolicy.isHighFrequency({
|
|
1498
|
+
kind: event.kind,
|
|
1499
|
+
payload: event.payload,
|
|
1500
|
+
correlation_id: event.correlation_id,
|
|
1501
|
+
});
|
|
1502
|
+
}
|
|
1503
|
+
recordDisplayDrop(event) {
|
|
1504
|
+
if (!isRecord(event) || !("kind" in event)) {
|
|
1505
|
+
this.#pendingDisplayDrops += 1;
|
|
1506
|
+
return;
|
|
1507
|
+
}
|
|
1508
|
+
this.#pendingDisplayDrops += 1 + this.#displayPolicy.droppedCount({
|
|
1509
|
+
kind: event.kind,
|
|
1510
|
+
payload: event.payload,
|
|
1511
|
+
correlation_id: event.correlation_id,
|
|
1512
|
+
});
|
|
1513
|
+
}
|
|
1514
|
+
withDisplayDropMarker(event) {
|
|
1515
|
+
if (this.#pendingDisplayDrops === 0 || !isRecord(event) || !("kind" in event)) {
|
|
1516
|
+
return event;
|
|
1517
|
+
}
|
|
1518
|
+
const pending = this.#pendingDisplayDrops;
|
|
1519
|
+
this.#pendingDisplayDrops = 0;
|
|
1520
|
+
const payload = isRecord(event.payload) ? event.payload : {};
|
|
1521
|
+
const inherited = this.#displayPolicy.droppedCount({
|
|
1522
|
+
kind: event.kind,
|
|
1523
|
+
payload,
|
|
1524
|
+
correlation_id: event.correlation_id,
|
|
1525
|
+
});
|
|
1526
|
+
return {
|
|
1527
|
+
...event,
|
|
1528
|
+
payload: { ...payload, _projection_dropped: inherited + pending },
|
|
1529
|
+
};
|
|
1530
|
+
}
|
|
1531
|
+
flushDisplayDropMarker() {
|
|
1532
|
+
if (this.#pendingDisplayDrops === 0) {
|
|
1533
|
+
return false;
|
|
1534
|
+
}
|
|
1535
|
+
const dropped = this.#pendingDisplayDrops;
|
|
1536
|
+
this.#pendingDisplayDrops = 0;
|
|
1537
|
+
this.appendTranscript(createTranscriptBlock("notice", this.newBlockId(), {
|
|
1538
|
+
text: displayGapMessage(dropped),
|
|
1539
|
+
style: "yellow",
|
|
1540
|
+
}));
|
|
1541
|
+
return true;
|
|
1542
|
+
}
|
|
1253
1543
|
cancelFromKeybinding() {
|
|
1254
1544
|
if (this.state.sessionState === "CANCELLING") {
|
|
1255
1545
|
this.write("Cancelling…");
|
|
@@ -1259,24 +1549,16 @@ export class TerminalUI {
|
|
|
1259
1549
|
}
|
|
1260
1550
|
// -- transcript ---------------------------------------------------------
|
|
1261
1551
|
newBlockId() {
|
|
1262
|
-
this.#
|
|
1263
|
-
return `local-${this.#nextBlockId}`;
|
|
1552
|
+
return this.#transcript.newBlockId();
|
|
1264
1553
|
}
|
|
1265
1554
|
appendTranscript(block) {
|
|
1266
|
-
this.#transcript.
|
|
1267
|
-
if (block.key) {
|
|
1268
|
-
this.#byCorrelation.set(`${block.kind}:${block.key}`, block);
|
|
1269
|
-
}
|
|
1555
|
+
this.#transcript.append(block);
|
|
1270
1556
|
}
|
|
1271
1557
|
acceptUserInput(text) {
|
|
1272
1558
|
this.appendTranscript(createTranscriptBlock("user", this.newBlockId(), { text }));
|
|
1273
1559
|
}
|
|
1274
1560
|
blockFor(kind, key) {
|
|
1275
|
-
|
|
1276
|
-
if (block === undefined) {
|
|
1277
|
-
throw new Error(`no transcript block for ${kind}:${key}`);
|
|
1278
|
-
}
|
|
1279
|
-
return block;
|
|
1561
|
+
return this.#transcript.blockFor(kind, key);
|
|
1280
1562
|
}
|
|
1281
1563
|
/** Build every persisted transcript line; never crop history here. */
|
|
1282
1564
|
buildHistoryLines(width) {
|
|
@@ -1284,7 +1566,7 @@ export class TerminalUI {
|
|
|
1284
1566
|
}
|
|
1285
1567
|
#buildHistoryFrameParts(width) {
|
|
1286
1568
|
const usableWidth = Math.max(12, width);
|
|
1287
|
-
const blocks =
|
|
1569
|
+
const blocks = this.#transcript.blocks();
|
|
1288
1570
|
const lines = [];
|
|
1289
1571
|
let activeStart = null;
|
|
1290
1572
|
for (const block of blocks) {
|
|
@@ -1304,27 +1586,14 @@ export class TerminalUI {
|
|
|
1304
1586
|
}
|
|
1305
1587
|
buildFrame(options) {
|
|
1306
1588
|
const { width, editor } = options;
|
|
1307
|
-
const prompt = options.prompt ?? "❯ ";
|
|
1308
|
-
const secret = options.secret ?? false;
|
|
1309
1589
|
const { lines: history, activeStart } = this.#buildHistoryFrameParts(width);
|
|
1310
|
-
const editorResult = editor.renderLines(width, { prompt, mask: secret });
|
|
1311
1590
|
const completion = this.#completionLines(width, editor);
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
...completion,
|
|
1319
|
-
...(footer !== null ? [footer] : []),
|
|
1320
|
-
];
|
|
1321
|
-
const editorStart = history.length + 1;
|
|
1322
|
-
return {
|
|
1323
|
-
lines: rows,
|
|
1324
|
-
activeStart: activeStart ?? editorStart,
|
|
1325
|
-
cursorRow: editorStart + editorResult.cursorRow,
|
|
1326
|
-
cursorCol: editorResult.cursorCol,
|
|
1327
|
-
};
|
|
1591
|
+
return this.#frameBuilder.build({
|
|
1592
|
+
...options,
|
|
1593
|
+
historyLines: history,
|
|
1594
|
+
activeStart,
|
|
1595
|
+
completionLines: completion,
|
|
1596
|
+
}).screen;
|
|
1328
1597
|
}
|
|
1329
1598
|
#completionLines(width, editor) {
|
|
1330
1599
|
const rows = [];
|
|
@@ -1335,28 +1604,6 @@ export class TerminalUI {
|
|
|
1335
1604
|
});
|
|
1336
1605
|
return rows;
|
|
1337
1606
|
}
|
|
1338
|
-
#footerLine(width) {
|
|
1339
|
-
const text = this.#footerText();
|
|
1340
|
-
return text ? truncateToWidth(text, width) : null;
|
|
1341
|
-
}
|
|
1342
|
-
#footerText() {
|
|
1343
|
-
const details = [];
|
|
1344
|
-
if (this.projectRoot !== null) {
|
|
1345
|
-
details.push(this.projectRoot);
|
|
1346
|
-
}
|
|
1347
|
-
if (this.state.pendingCount || this.state.heldCount) {
|
|
1348
|
-
details.push(`queue ${this.state.pendingCount} pending / ${this.state.heldCount} held`);
|
|
1349
|
-
}
|
|
1350
|
-
if (this.state.totalTokens) {
|
|
1351
|
-
details.push(`↑${this.state.inputTokens} ↓${this.state.outputTokens}`);
|
|
1352
|
-
}
|
|
1353
|
-
const model = this.state.model || (this.model ?? "");
|
|
1354
|
-
if (model) {
|
|
1355
|
-
const provider = this.state.provider || (this.provider ?? "");
|
|
1356
|
-
details.push(provider ? `${provider}/${model}` : model);
|
|
1357
|
-
}
|
|
1358
|
-
return details.join(" · ");
|
|
1359
|
-
}
|
|
1360
1607
|
#renderTranscriptItem(item, width) {
|
|
1361
1608
|
if (item.kind === "user") {
|
|
1362
1609
|
return this.#backgroundLines(item.text, width, "user_bg", "text");
|
|
@@ -1394,6 +1641,9 @@ export class TerminalUI {
|
|
|
1394
1641
|
if (item.streamError) {
|
|
1395
1642
|
detail += `\n${clip(item.streamError, 1_200)}`;
|
|
1396
1643
|
}
|
|
1644
|
+
if (item.toolOutputExpanded && item.toolOutput) {
|
|
1645
|
+
detail += `\n${clip(item.toolOutput, 1_200)}`;
|
|
1646
|
+
}
|
|
1397
1647
|
const rendered = this.#backgroundLines(`${title}\n${detail}`, width, background, "text");
|
|
1398
1648
|
const first = rendered[0];
|
|
1399
1649
|
if (first !== undefined) {
|
|
@@ -1433,134 +1683,36 @@ export class TerminalUI {
|
|
|
1433
1683
|
}));
|
|
1434
1684
|
return;
|
|
1435
1685
|
}
|
|
1436
|
-
if (isHiddenToolStdout(event)) {
|
|
1437
|
-
return;
|
|
1438
|
-
}
|
|
1439
|
-
const dropped = projectionDropped(event);
|
|
1440
|
-
if (dropped) {
|
|
1441
|
-
this.appendTranscript(createTranscriptBlock("notice", this.newBlockId(), {
|
|
1442
|
-
text: `… 省略了 ${dropped} 个流式展示事件;Agent 仍继续运行。`,
|
|
1443
|
-
style: "yellow",
|
|
1444
|
-
}));
|
|
1445
|
-
}
|
|
1446
1686
|
this.applyProjectedEvent(event);
|
|
1447
1687
|
}
|
|
1448
1688
|
/** Reduce a projected event and apply its append-only block change. */
|
|
1449
1689
|
applyProjectedEvent(event) {
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
}
|
|
1453
|
-
const update = this.reducer.apply(event);
|
|
1454
|
-
if (update !== null) {
|
|
1455
|
-
this.applyTranscriptUpdate(update);
|
|
1690
|
+
for (const projected of this.#displayPolicy.project(event)) {
|
|
1691
|
+
this.#applyDisplayEvent(projected);
|
|
1456
1692
|
}
|
|
1457
1693
|
}
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
const kind = update.kind;
|
|
1461
|
-
const correlationId = update.correlationId;
|
|
1462
|
-
if (kind === "ui.message") {
|
|
1463
|
-
this.appendTranscript(createTranscriptBlock("notice", this.newBlockId(), {
|
|
1464
|
-
text: update.text,
|
|
1465
|
-
style: String(update.payload.style ?? ""),
|
|
1466
|
-
}));
|
|
1467
|
-
return;
|
|
1468
|
-
}
|
|
1469
|
-
if (kind === "model.text_delta") {
|
|
1470
|
-
this.#freezeThinking();
|
|
1471
|
-
const key = `assistant:${correlationId}`;
|
|
1472
|
-
let item = this.#byCorrelation.get(key);
|
|
1473
|
-
if (item === undefined) {
|
|
1474
|
-
item = createTranscriptBlock("assistant", correlationId, { mutable: true });
|
|
1475
|
-
this.#transcript.push(item);
|
|
1476
|
-
this.#byCorrelation.set(key, item);
|
|
1477
|
-
}
|
|
1478
|
-
if (item.mutable) {
|
|
1479
|
-
item.text += update.text;
|
|
1480
|
-
}
|
|
1481
|
-
return;
|
|
1482
|
-
}
|
|
1483
|
-
if (kind === "model.reasoning_delta") {
|
|
1484
|
-
const key = `thinking:${correlationId}`;
|
|
1485
|
-
let item = this.#byCorrelation.get(key);
|
|
1486
|
-
if (item === undefined) {
|
|
1487
|
-
item = createTranscriptBlock("thinking", correlationId, { mutable: true });
|
|
1488
|
-
this.#transcript.push(item);
|
|
1489
|
-
this.#byCorrelation.set(key, item);
|
|
1490
|
-
}
|
|
1491
|
-
if (item.mutable) {
|
|
1492
|
-
item.text += update.text;
|
|
1493
|
-
}
|
|
1494
|
-
return;
|
|
1495
|
-
}
|
|
1496
|
-
if (kind === "model.response_committed" ||
|
|
1497
|
-
kind === "model.response_aborted" ||
|
|
1498
|
-
kind === "model.request_failed") {
|
|
1499
|
-
for (const blockKind of ["assistant", "thinking"]) {
|
|
1500
|
-
const block = this.#byCorrelation.get(`${blockKind}:${correlationId}`);
|
|
1501
|
-
if (block !== undefined) {
|
|
1502
|
-
block.mutable = false;
|
|
1503
|
-
}
|
|
1504
|
-
}
|
|
1694
|
+
applyDisplayAction(action) {
|
|
1695
|
+
if (action.type !== "toggle_tool_output") {
|
|
1505
1696
|
return;
|
|
1506
1697
|
}
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
subject = String(args.command || args.path || "");
|
|
1513
|
-
}
|
|
1514
|
-
this.appendTranscript(createTranscriptBlock("tool", correlationId, {
|
|
1515
|
-
mutable: true,
|
|
1516
|
-
name: String(update.payload.name ?? "tool"),
|
|
1517
|
-
subject,
|
|
1518
|
-
status: "running",
|
|
1519
|
-
}));
|
|
1520
|
-
return;
|
|
1521
|
-
}
|
|
1522
|
-
if (kind === "tool.output_delta" && update.stream === "stderr") {
|
|
1523
|
-
const item = this.#byCorrelation.get(`tool:${correlationId}`);
|
|
1524
|
-
if (item !== undefined) {
|
|
1525
|
-
item.streamError = (item.streamError + update.text).slice(-20_000);
|
|
1526
|
-
}
|
|
1527
|
-
return;
|
|
1528
|
-
}
|
|
1529
|
-
if (kind === "tool.finished") {
|
|
1530
|
-
const key = `tool:${correlationId}`;
|
|
1531
|
-
let item = this.#byCorrelation.get(key);
|
|
1532
|
-
if (item === undefined) {
|
|
1533
|
-
item = createTranscriptBlock("tool", correlationId);
|
|
1534
|
-
this.#transcript.push(item);
|
|
1535
|
-
this.#byCorrelation.set(key, item);
|
|
1536
|
-
}
|
|
1537
|
-
item.status = String(update.payload.status ?? "completed");
|
|
1538
|
-
const rawExitCode = update.payload.exit_code;
|
|
1539
|
-
item.exitCode = typeof rawExitCode === "number" ? Math.trunc(rawExitCode) : null;
|
|
1540
|
-
const rawDuration = update.payload.duration_ms;
|
|
1541
|
-
item.durationMs = typeof rawDuration === "number" ? Math.trunc(rawDuration) : null;
|
|
1542
|
-
item.mutable = false;
|
|
1543
|
-
return;
|
|
1544
|
-
}
|
|
1545
|
-
if (kind === "task.cancelled") {
|
|
1698
|
+
this.#transcript.setToolOutputExpanded(action.expanded);
|
|
1699
|
+
this.#loop?.requestRender();
|
|
1700
|
+
}
|
|
1701
|
+
#applyDisplayEvent(event) {
|
|
1702
|
+
if (event.kind === "display.gap") {
|
|
1546
1703
|
this.appendTranscript(createTranscriptBlock("notice", this.newBlockId(), {
|
|
1547
|
-
text:
|
|
1704
|
+
text: event.text,
|
|
1548
1705
|
style: "yellow",
|
|
1549
1706
|
}));
|
|
1550
1707
|
return;
|
|
1551
1708
|
}
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
#freezeThinking() {
|
|
1560
|
-
for (const block of this.#transcript) {
|
|
1561
|
-
if (block.kind === "thinking") {
|
|
1562
|
-
block.mutable = false;
|
|
1563
|
-
}
|
|
1709
|
+
const update = this.reducer.apply({
|
|
1710
|
+
kind: event.kind,
|
|
1711
|
+
correlation_id: event.correlationId,
|
|
1712
|
+
payload: event.payload,
|
|
1713
|
+
});
|
|
1714
|
+
if (update !== null) {
|
|
1715
|
+
this.#transcript.apply(update);
|
|
1564
1716
|
}
|
|
1565
1717
|
}
|
|
1566
1718
|
// -- direct user-facing writes -------------------------------------------
|
|
@@ -1605,8 +1757,8 @@ export class TerminalUI {
|
|
|
1605
1757
|
details.push(`trace: ${this.dashboardUrl}`);
|
|
1606
1758
|
}
|
|
1607
1759
|
if (this.#loop !== null) {
|
|
1608
|
-
this.appendTranscript(createTranscriptBlock("notice",
|
|
1609
|
-
text:
|
|
1760
|
+
this.appendTranscript(createTranscriptBlock("notice", "welcome", {
|
|
1761
|
+
text: WELCOME_TEXT,
|
|
1610
1762
|
style: `bold ${this.theme.color("accent")}`,
|
|
1611
1763
|
}));
|
|
1612
1764
|
if (details.length > 0) {
|
|
@@ -1617,7 +1769,7 @@ export class TerminalUI {
|
|
|
1617
1769
|
}
|
|
1618
1770
|
return;
|
|
1619
1771
|
}
|
|
1620
|
-
this.#output(ansiStyledText(`bold ${this.theme.color("accent")}`,
|
|
1772
|
+
this.#output(ansiStyledText(`bold ${this.theme.color("accent")}`, WELCOME_TEXT) +
|
|
1621
1773
|
" " +
|
|
1622
1774
|
ansiStyledText(this.theme.color("dim"), "/help for commands"));
|
|
1623
1775
|
if (details.length > 0) {
|
|
@@ -1653,16 +1805,32 @@ export class PlainEventSink {
|
|
|
1653
1805
|
outputFn;
|
|
1654
1806
|
#stopped = false;
|
|
1655
1807
|
#modelBuffers = new Map();
|
|
1808
|
+
#displayPolicy = new DisplayPolicy({ audience: "terminal" });
|
|
1656
1809
|
renderError = null;
|
|
1657
1810
|
constructor(outputFn = (text) => console.log(text)) {
|
|
1658
1811
|
this.outputFn = outputFn;
|
|
1659
1812
|
}
|
|
1660
1813
|
publishEvent(event) {
|
|
1661
|
-
if (this.#stopped ||
|
|
1814
|
+
if (this.#stopped || !isRecord(event) || !("kind" in event)) {
|
|
1662
1815
|
return;
|
|
1663
1816
|
}
|
|
1664
1817
|
try {
|
|
1665
|
-
|
|
1818
|
+
const displayEvent = {
|
|
1819
|
+
kind: event.kind,
|
|
1820
|
+
payload: event.payload,
|
|
1821
|
+
correlation_id: event.correlation_id,
|
|
1822
|
+
};
|
|
1823
|
+
for (const projected of this.#displayPolicy.project(displayEvent)) {
|
|
1824
|
+
if (projected.kind === "display.gap") {
|
|
1825
|
+
this.outputFn(`[stream output omitted: ${projected.payload.dropped} event(s); runtime continued]`);
|
|
1826
|
+
continue;
|
|
1827
|
+
}
|
|
1828
|
+
this.#renderEvent({
|
|
1829
|
+
kind: projected.kind,
|
|
1830
|
+
correlation_id: projected.correlationId,
|
|
1831
|
+
payload: projected.payload,
|
|
1832
|
+
});
|
|
1833
|
+
}
|
|
1666
1834
|
}
|
|
1667
1835
|
catch (error) {
|
|
1668
1836
|
this.renderError = error;
|
|
@@ -1684,14 +1852,7 @@ export class PlainEventSink {
|
|
|
1684
1852
|
return;
|
|
1685
1853
|
}
|
|
1686
1854
|
const kind = String(event.kind ?? "");
|
|
1687
|
-
const payload =
|
|
1688
|
-
if (kind === "tool.output_delta" && String(payload.stream ?? "stdout") === "stdout") {
|
|
1689
|
-
return;
|
|
1690
|
-
}
|
|
1691
|
-
const dropped = toInt(payload._projection_dropped ?? 0);
|
|
1692
|
-
if (dropped) {
|
|
1693
|
-
this.outputFn(`[stream output omitted: ${dropped} event(s); runtime continued]`);
|
|
1694
|
-
}
|
|
1855
|
+
const payload = isRecord(event.payload) ? event.payload : {};
|
|
1695
1856
|
const correlationId = String(event.correlation_id ?? "").slice(-8);
|
|
1696
1857
|
if (kind === "model.text_delta") {
|
|
1697
1858
|
const text = String(payload.text ?? payload.chunk ?? "");
|