@oyasmi/pipiclaw 0.7.1 → 0.7.3
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 +8 -19
- package/dist/agent/channel-runner.d.ts +8 -4
- package/dist/agent/channel-runner.js +14 -12
- package/dist/agent/model-fallback.js +11 -5
- package/dist/agent/run-queue.d.ts +2 -2
- package/dist/agent/runner-factory.d.ts +7 -2
- package/dist/agent/runner-factory.js +18 -6
- package/dist/agent/session-events.d.ts +2 -2
- package/dist/agent/status-render.d.ts +22 -0
- package/dist/agent/status-render.js +61 -0
- package/dist/agent/type-guards.js +3 -4
- package/dist/agent/types.d.ts +7 -10
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/log.d.ts +6 -0
- package/dist/log.js +38 -25
- package/dist/main.js +15 -2
- package/dist/memory/candidates.js +5 -17
- package/dist/memory/consolidation.js +1 -6
- package/dist/memory/files.d.ts +1 -0
- package/dist/memory/files.js +0 -0
- package/dist/memory/maintenance-jobs.js +1 -0
- package/dist/memory/session-corpus.js +3 -16
- package/dist/runtime/bootstrap.d.ts +17 -0
- package/dist/runtime/bootstrap.js +44 -79
- package/dist/runtime/channel-context.d.ts +44 -0
- package/dist/runtime/channel-context.js +15 -0
- package/dist/runtime/delivery.d.ts +3 -2
- package/dist/runtime/dingtalk.d.ts +1 -25
- package/dist/runtime/dingtalk.js +1 -1
- package/dist/runtime/event-commands.js +1 -3
- package/dist/runtime/events.js +1 -3
- package/dist/runtime/store.d.ts +1 -7
- package/dist/runtime/store.js +6 -80
- package/dist/settings.d.ts +13 -0
- package/dist/settings.js +6 -0
- package/dist/shared/fs-utils.d.ts +8 -0
- package/dist/shared/fs-utils.js +21 -0
- package/dist/shared/text-utils.d.ts +2 -0
- package/dist/shared/text-utils.js +4 -0
- package/dist/tools/index.d.ts +1 -7
- package/dist/tools/index.js +0 -20
- package/dist/tools/memory-save.d.ts +0 -1
- package/dist/tools/memory-save.js +0 -1
- package/dist/tui/app.d.ts +18 -0
- package/dist/tui/app.js +104 -0
- package/dist/tui/cli.d.ts +22 -0
- package/dist/tui/cli.js +116 -0
- package/dist/tui/colors.d.ts +9 -0
- package/dist/tui/colors.js +20 -0
- package/dist/tui/commands.d.ts +47 -0
- package/dist/tui/commands.js +64 -0
- package/dist/tui/pitui-frontend.d.ts +47 -0
- package/dist/tui/pitui-frontend.js +154 -0
- package/dist/tui/plain-frontend.d.ts +23 -0
- package/dist/tui/plain-frontend.js +56 -0
- package/dist/tui/renderer.d.ts +62 -0
- package/dist/tui/renderer.js +16 -0
- package/dist/tui/terminal-context.d.ts +27 -0
- package/dist/tui/terminal-context.js +84 -0
- package/dist/tui/turn-controller.d.ts +68 -0
- package/dist/tui/turn-controller.js +286 -0
- package/package.json +2 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export type DispatchOutcome =
|
|
2
|
+
/** Text to show the user immediately (help/status/usage/events output, or a hint). */
|
|
3
|
+
{
|
|
4
|
+
kind: "reply";
|
|
5
|
+
text: string;
|
|
6
|
+
}
|
|
7
|
+
/** Send this text to the runner as a normal turn. */
|
|
8
|
+
| {
|
|
9
|
+
kind: "run";
|
|
10
|
+
text: string;
|
|
11
|
+
}
|
|
12
|
+
/** Steer the in-flight turn (app falls back to `run` when idle). */
|
|
13
|
+
| {
|
|
14
|
+
kind: "steer";
|
|
15
|
+
text: string;
|
|
16
|
+
}
|
|
17
|
+
/** Queue this to run after the current turn (app runs it now when idle). */
|
|
18
|
+
| {
|
|
19
|
+
kind: "followup";
|
|
20
|
+
text: string;
|
|
21
|
+
}
|
|
22
|
+
/** Abort the in-flight turn. */
|
|
23
|
+
| {
|
|
24
|
+
kind: "stop";
|
|
25
|
+
}
|
|
26
|
+
/** Leave the TUI. */
|
|
27
|
+
| {
|
|
28
|
+
kind: "exit";
|
|
29
|
+
}
|
|
30
|
+
/** Nothing to do; optional hint to show. */
|
|
31
|
+
| {
|
|
32
|
+
kind: "noop";
|
|
33
|
+
text?: string;
|
|
34
|
+
};
|
|
35
|
+
export interface DispatchDeps {
|
|
36
|
+
renderHelp(): string;
|
|
37
|
+
renderStatus(): string;
|
|
38
|
+
renderUsage(args: string): string;
|
|
39
|
+
runEvents(args: string): Promise<string>;
|
|
40
|
+
}
|
|
41
|
+
export declare function dispatch(input: string, deps: DispatchDeps): Promise<DispatchOutcome>;
|
|
42
|
+
/** Slash commands offered in editor autocomplete: transport + session + TUI-only. */
|
|
43
|
+
export declare const TUI_SLASH_COMMANDS: Array<{
|
|
44
|
+
name: string;
|
|
45
|
+
description: string;
|
|
46
|
+
argumentHint?: string;
|
|
47
|
+
}>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TUI command dispatch.
|
|
3
|
+
*
|
|
4
|
+
* Transport-layer commands (`/help /stop /steer /followup /status /usage
|
|
5
|
+
* /events`, plus the TUI-only `/exit`) are handled locally and never reach the
|
|
6
|
+
* runner. Everything else — session commands (`/model /new /compact /session`)
|
|
7
|
+
* and plain messages — becomes a `run` intent; the SDK command extension handles
|
|
8
|
+
* session commands inside the turn (results are delivered back through the
|
|
9
|
+
* channel context), so the TUI gets them for free.
|
|
10
|
+
*
|
|
11
|
+
* `dispatch` is pure over its injected deps: it returns an intent the app acts
|
|
12
|
+
* on, and renders info-command text via the deps. Steer/followup semantics
|
|
13
|
+
* depend on whether a turn is running, which only the app knows — so those come
|
|
14
|
+
* back as intents rather than being applied here.
|
|
15
|
+
*/
|
|
16
|
+
import { parseBuiltInCommand } from "../agent/commands.js";
|
|
17
|
+
export async function dispatch(input, deps) {
|
|
18
|
+
const trimmed = input.trim();
|
|
19
|
+
if (!trimmed)
|
|
20
|
+
return { kind: "noop" };
|
|
21
|
+
const lower = trimmed.toLowerCase();
|
|
22
|
+
if (lower === "/exit" || lower === "/quit")
|
|
23
|
+
return { kind: "exit" };
|
|
24
|
+
const command = parseBuiltInCommand(trimmed);
|
|
25
|
+
if (!command) {
|
|
26
|
+
// Session commands (/model, /new, /compact, /session) and plain messages.
|
|
27
|
+
return { kind: "run", text: input };
|
|
28
|
+
}
|
|
29
|
+
switch (command.name) {
|
|
30
|
+
case "help":
|
|
31
|
+
return { kind: "reply", text: deps.renderHelp() };
|
|
32
|
+
case "status":
|
|
33
|
+
return { kind: "reply", text: deps.renderStatus() };
|
|
34
|
+
case "usage":
|
|
35
|
+
return { kind: "reply", text: deps.renderUsage(command.args) };
|
|
36
|
+
case "events":
|
|
37
|
+
return { kind: "reply", text: await deps.runEvents(command.args) };
|
|
38
|
+
case "stop":
|
|
39
|
+
return { kind: "stop" };
|
|
40
|
+
case "steer":
|
|
41
|
+
return command.args.trim()
|
|
42
|
+
? { kind: "steer", text: command.args }
|
|
43
|
+
: { kind: "noop", text: "/steer requires a message." };
|
|
44
|
+
case "followup":
|
|
45
|
+
return command.args.trim()
|
|
46
|
+
? { kind: "followup", text: command.args }
|
|
47
|
+
: { kind: "noop", text: "/followup requires a message." };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Slash commands offered in editor autocomplete: transport + session + TUI-only. */
|
|
51
|
+
export const TUI_SLASH_COMMANDS = [
|
|
52
|
+
{ name: "help", description: "Show command help" },
|
|
53
|
+
{ name: "stop", description: "Stop the current task" },
|
|
54
|
+
{ name: "steer", description: "Adjust the running task", argumentHint: "<message>" },
|
|
55
|
+
{ name: "followup", description: "Queue a request after the current task", argumentHint: "<message>" },
|
|
56
|
+
{ name: "status", description: "Show model, context and run state" },
|
|
57
|
+
{ name: "usage", description: "Show LLM cost for this channel", argumentHint: "[7d|month]" },
|
|
58
|
+
{ name: "events", description: "Manage scheduled events", argumentHint: "<list|show|delete|history>" },
|
|
59
|
+
{ name: "model", description: "Show or switch the model", argumentHint: "[provider/model]" },
|
|
60
|
+
{ name: "new", description: "Start a new session" },
|
|
61
|
+
{ name: "compact", description: "Compact the session context", argumentHint: "[instructions]" },
|
|
62
|
+
{ name: "session", description: "Show session state" },
|
|
63
|
+
{ name: "exit", description: "Leave the TUI" },
|
|
64
|
+
];
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rich pi-tui frontend: differential rendering, a scrolling transcript, a live
|
|
3
|
+
* status line + working spinner, and an editor with history and slash-command
|
|
4
|
+
* autocomplete.
|
|
5
|
+
*
|
|
6
|
+
* Layout (top → bottom): transcript, status line, spinner, editor.
|
|
7
|
+
*
|
|
8
|
+
* Ctrl-C and Ctrl-D are intercepted via a global input listener (raw mode does
|
|
9
|
+
* not raise SIGINT). The two-stage Ctrl-C policy lives in the app; here we just
|
|
10
|
+
* surface the events.
|
|
11
|
+
*/
|
|
12
|
+
import { type SlashCommand } from "@earendil-works/pi-tui";
|
|
13
|
+
import type { Frontend, FrontendCallbacks } from "./renderer.js";
|
|
14
|
+
export interface PiTuiFrontendOptions {
|
|
15
|
+
/** Slash commands to offer in editor autocomplete. */
|
|
16
|
+
commands?: SlashCommand[];
|
|
17
|
+
/** Base path for the autocomplete provider (file completion is disabled). */
|
|
18
|
+
basePath?: string;
|
|
19
|
+
}
|
|
20
|
+
export declare class PiTuiFrontend implements Frontend {
|
|
21
|
+
private readonly ui;
|
|
22
|
+
private readonly transcript;
|
|
23
|
+
private readonly statusLine;
|
|
24
|
+
private readonly spinnerContainer;
|
|
25
|
+
private readonly editor;
|
|
26
|
+
private readonly loader;
|
|
27
|
+
private readonly editorContainer;
|
|
28
|
+
private turnProgress;
|
|
29
|
+
private removeInputListener;
|
|
30
|
+
private spinnerShown;
|
|
31
|
+
constructor(options?: PiTuiFrontendOptions);
|
|
32
|
+
start(callbacks: FrontendCallbacks): void;
|
|
33
|
+
showBanner(text: string): void;
|
|
34
|
+
/** Echo the user's submitted message into the transcript. */
|
|
35
|
+
showUser(text: string): void;
|
|
36
|
+
appendProgress(text: string): void;
|
|
37
|
+
showFinal(markdown: string): void;
|
|
38
|
+
showNotice(text: string): void;
|
|
39
|
+
clearProgress(): void;
|
|
40
|
+
setWorking(on: boolean): void;
|
|
41
|
+
setStatus(text: string): void;
|
|
42
|
+
setBusy(busy: boolean): void;
|
|
43
|
+
private showSpinner;
|
|
44
|
+
stop(): void;
|
|
45
|
+
/** Render the final error to the transcript when the app cannot continue. */
|
|
46
|
+
showError(text: string): void;
|
|
47
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rich pi-tui frontend: differential rendering, a scrolling transcript, a live
|
|
3
|
+
* status line + working spinner, and an editor with history and slash-command
|
|
4
|
+
* autocomplete.
|
|
5
|
+
*
|
|
6
|
+
* Layout (top → bottom): transcript, status line, spinner, editor.
|
|
7
|
+
*
|
|
8
|
+
* Ctrl-C and Ctrl-D are intercepted via a global input listener (raw mode does
|
|
9
|
+
* not raise SIGINT). The two-stage Ctrl-C policy lives in the app; here we just
|
|
10
|
+
* surface the events.
|
|
11
|
+
*/
|
|
12
|
+
import { CombinedAutocompleteProvider, Container, Editor, Loader, Markdown, matchesKey, ProcessTerminal, Text, TUI, } from "@earendil-works/pi-tui";
|
|
13
|
+
import { bold, cyan, dim, gray, italic, red, strikethrough, underline, yellow } from "./colors.js";
|
|
14
|
+
const MARKDOWN_THEME = {
|
|
15
|
+
heading: (t) => bold(cyan(t)),
|
|
16
|
+
link: (t) => underline(cyan(t)),
|
|
17
|
+
linkUrl: (t) => dim(t),
|
|
18
|
+
code: (t) => yellow(t),
|
|
19
|
+
codeBlock: (t) => yellow(t),
|
|
20
|
+
codeBlockBorder: (t) => gray(t),
|
|
21
|
+
quote: (t) => dim(t),
|
|
22
|
+
quoteBorder: (t) => gray(t),
|
|
23
|
+
hr: (t) => gray(t),
|
|
24
|
+
listBullet: (t) => cyan(t),
|
|
25
|
+
bold: (t) => bold(t),
|
|
26
|
+
italic: (t) => italic(t),
|
|
27
|
+
strikethrough: (t) => strikethrough(t),
|
|
28
|
+
underline: (t) => underline(t),
|
|
29
|
+
};
|
|
30
|
+
const EDITOR_THEME = {
|
|
31
|
+
borderColor: (t) => gray(t),
|
|
32
|
+
selectList: {
|
|
33
|
+
selectedPrefix: (t) => cyan(t),
|
|
34
|
+
selectedText: (t) => bold(t),
|
|
35
|
+
description: (t) => gray(t),
|
|
36
|
+
scrollInfo: (t) => gray(t),
|
|
37
|
+
noMatch: (t) => gray(t),
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
export class PiTuiFrontend {
|
|
41
|
+
constructor(options = {}) {
|
|
42
|
+
this.transcript = new Container();
|
|
43
|
+
this.statusLine = new Text("", 1, 0);
|
|
44
|
+
this.spinnerContainer = new Container();
|
|
45
|
+
this.editorContainer = new Container();
|
|
46
|
+
this.turnProgress = [];
|
|
47
|
+
this.spinnerShown = false;
|
|
48
|
+
this.ui = new TUI(new ProcessTerminal(), true);
|
|
49
|
+
this.editor = new Editor(this.ui, EDITOR_THEME, { paddingX: 1 });
|
|
50
|
+
this.loader = new Loader(this.ui, (s) => cyan(s), (s) => gray(s), "Working…");
|
|
51
|
+
if (options.commands?.length) {
|
|
52
|
+
this.editor.setAutocompleteProvider(new CombinedAutocompleteProvider(options.commands, options.basePath ?? process.cwd(), null));
|
|
53
|
+
}
|
|
54
|
+
this.editorContainer.addChild(this.editor);
|
|
55
|
+
this.ui.addChild(this.transcript);
|
|
56
|
+
this.ui.addChild(this.statusLine);
|
|
57
|
+
this.ui.addChild(this.spinnerContainer);
|
|
58
|
+
this.ui.addChild(this.editorContainer);
|
|
59
|
+
}
|
|
60
|
+
start(callbacks) {
|
|
61
|
+
this.editor.onSubmit = (text) => {
|
|
62
|
+
if (text.length === 0)
|
|
63
|
+
return;
|
|
64
|
+
// Raw mode gives no terminal echo and the editor clears on submit, so
|
|
65
|
+
// render the user's message into the transcript ourselves.
|
|
66
|
+
this.showUser(text);
|
|
67
|
+
callbacks.onSubmit(text);
|
|
68
|
+
};
|
|
69
|
+
// Use pi-tui's key matcher, not a raw byte compare: with the Kitty keyboard
|
|
70
|
+
// protocol active, Ctrl-C/Ctrl-D arrive as CSI escape sequences, not \x03/\x04.
|
|
71
|
+
this.removeInputListener = this.ui.addInputListener((data) => {
|
|
72
|
+
if (matchesKey(data, "ctrl+c")) {
|
|
73
|
+
callbacks.onInterrupt();
|
|
74
|
+
return { consume: true };
|
|
75
|
+
}
|
|
76
|
+
if (matchesKey(data, "ctrl+d") && this.editor.getText().length === 0) {
|
|
77
|
+
callbacks.onEof();
|
|
78
|
+
return { consume: true };
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
});
|
|
82
|
+
this.ui.setFocus(this.editor);
|
|
83
|
+
this.ui.start();
|
|
84
|
+
this.ui.requestRender();
|
|
85
|
+
}
|
|
86
|
+
showBanner(text) {
|
|
87
|
+
this.transcript.addChild(new Text(text, 1, 1));
|
|
88
|
+
this.ui.requestRender();
|
|
89
|
+
}
|
|
90
|
+
/** Echo the user's submitted message into the transcript. */
|
|
91
|
+
showUser(text) {
|
|
92
|
+
this.transcript.addChild(new Text(`${cyan(bold("›"))} ${text}`, 1, 0));
|
|
93
|
+
this.ui.requestRender();
|
|
94
|
+
}
|
|
95
|
+
appendProgress(text) {
|
|
96
|
+
const line = new Text(dim(text), 1, 0);
|
|
97
|
+
this.transcript.addChild(line);
|
|
98
|
+
this.turnProgress.push(line);
|
|
99
|
+
this.ui.requestRender();
|
|
100
|
+
}
|
|
101
|
+
showFinal(markdown) {
|
|
102
|
+
this.transcript.addChild(new Markdown(markdown, 1, 1, MARKDOWN_THEME));
|
|
103
|
+
// The turn's progress becomes permanent scrollback; stop tracking it so a
|
|
104
|
+
// later clearProgress cannot retract it.
|
|
105
|
+
this.turnProgress = [];
|
|
106
|
+
this.ui.requestRender();
|
|
107
|
+
}
|
|
108
|
+
showNotice(text) {
|
|
109
|
+
this.transcript.addChild(new Text(gray(text), 1, 0));
|
|
110
|
+
this.ui.requestRender();
|
|
111
|
+
}
|
|
112
|
+
clearProgress() {
|
|
113
|
+
for (const component of this.turnProgress) {
|
|
114
|
+
this.transcript.removeChild(component);
|
|
115
|
+
}
|
|
116
|
+
this.turnProgress = [];
|
|
117
|
+
this.ui.requestRender();
|
|
118
|
+
}
|
|
119
|
+
setWorking(on) {
|
|
120
|
+
this.showSpinner(on);
|
|
121
|
+
}
|
|
122
|
+
setStatus(text) {
|
|
123
|
+
this.statusLine.setText(text ? gray(text) : "");
|
|
124
|
+
this.ui.requestRender();
|
|
125
|
+
}
|
|
126
|
+
setBusy(busy) {
|
|
127
|
+
this.showSpinner(busy);
|
|
128
|
+
}
|
|
129
|
+
showSpinner(on) {
|
|
130
|
+
if (on === this.spinnerShown)
|
|
131
|
+
return;
|
|
132
|
+
this.spinnerShown = on;
|
|
133
|
+
if (on) {
|
|
134
|
+
this.spinnerContainer.addChild(this.loader);
|
|
135
|
+
this.loader.start();
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
this.loader.stop();
|
|
139
|
+
this.spinnerContainer.clear();
|
|
140
|
+
}
|
|
141
|
+
this.ui.requestRender();
|
|
142
|
+
}
|
|
143
|
+
stop() {
|
|
144
|
+
this.removeInputListener?.();
|
|
145
|
+
this.removeInputListener = undefined;
|
|
146
|
+
this.loader.stop();
|
|
147
|
+
this.ui.stop();
|
|
148
|
+
}
|
|
149
|
+
/** Render the final error to the transcript when the app cannot continue. */
|
|
150
|
+
showError(text) {
|
|
151
|
+
this.transcript.addChild(new Text(red(text), 1, 0));
|
|
152
|
+
this.ui.requestRender();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Frontend, FrontendCallbacks } from "./renderer.js";
|
|
2
|
+
export interface PlainFrontendOptions {
|
|
3
|
+
/** Suppress progress/notice output on stderr; only the final answer is printed. */
|
|
4
|
+
quiet?: boolean;
|
|
5
|
+
/** When false, do not read input (one-shot `--print` where the app supplies the prompt). */
|
|
6
|
+
interactive?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare class PlainFrontend implements Frontend {
|
|
9
|
+
private rl;
|
|
10
|
+
private readonly quiet;
|
|
11
|
+
private readonly interactive;
|
|
12
|
+
constructor(options?: PlainFrontendOptions);
|
|
13
|
+
start(callbacks: FrontendCallbacks): void;
|
|
14
|
+
appendProgress(text: string): void;
|
|
15
|
+
showFinal(markdown: string): void;
|
|
16
|
+
showNotice(text: string): void;
|
|
17
|
+
clearProgress(): void;
|
|
18
|
+
setWorking(_on: boolean): void;
|
|
19
|
+
setStatus(text: string): void;
|
|
20
|
+
setBusy(_busy: boolean): void;
|
|
21
|
+
showBanner(text: string): void;
|
|
22
|
+
stop(): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plain-stream frontend: no raw mode, no differential rendering.
|
|
3
|
+
*
|
|
4
|
+
* Used when stdout/stdin is not a TTY (piped, redirected) or when `--print` is
|
|
5
|
+
* requested. Progress and notices go to stderr (silenceable with `--quiet`); the
|
|
6
|
+
* final answer goes to stdout so it can be captured by scripts. Input is read
|
|
7
|
+
* line-by-line via readline, which also handles piped stdin (each line → a turn,
|
|
8
|
+
* then EOF).
|
|
9
|
+
*/
|
|
10
|
+
import { createInterface } from "node:readline";
|
|
11
|
+
import { dim, gray } from "./colors.js";
|
|
12
|
+
export class PlainFrontend {
|
|
13
|
+
constructor(options = {}) {
|
|
14
|
+
this.quiet = options.quiet ?? false;
|
|
15
|
+
this.interactive = options.interactive ?? true;
|
|
16
|
+
}
|
|
17
|
+
start(callbacks) {
|
|
18
|
+
if (!this.interactive)
|
|
19
|
+
return;
|
|
20
|
+
this.rl = createInterface({ input: process.stdin, output: process.stdout, prompt: "" });
|
|
21
|
+
this.rl.on("line", (line) => callbacks.onSubmit(line));
|
|
22
|
+
this.rl.on("SIGINT", () => callbacks.onInterrupt());
|
|
23
|
+
this.rl.on("close", () => callbacks.onEof());
|
|
24
|
+
}
|
|
25
|
+
appendProgress(text) {
|
|
26
|
+
if (this.quiet)
|
|
27
|
+
return;
|
|
28
|
+
process.stderr.write(`${dim(text)}\n`);
|
|
29
|
+
}
|
|
30
|
+
showFinal(markdown) {
|
|
31
|
+
process.stdout.write(`${markdown}\n`);
|
|
32
|
+
}
|
|
33
|
+
showNotice(text) {
|
|
34
|
+
if (this.quiet)
|
|
35
|
+
return;
|
|
36
|
+
process.stderr.write(`${gray(text)}\n`);
|
|
37
|
+
}
|
|
38
|
+
// A plain stream cannot retract already-written lines.
|
|
39
|
+
clearProgress() { }
|
|
40
|
+
setWorking(_on) { }
|
|
41
|
+
setStatus(text) {
|
|
42
|
+
if (this.quiet || !text.trim())
|
|
43
|
+
return;
|
|
44
|
+
process.stderr.write(`${gray(text)}\n`);
|
|
45
|
+
}
|
|
46
|
+
setBusy(_busy) { }
|
|
47
|
+
showBanner(text) {
|
|
48
|
+
if (this.quiet)
|
|
49
|
+
return;
|
|
50
|
+
process.stderr.write(`${text}\n`);
|
|
51
|
+
}
|
|
52
|
+
stop() {
|
|
53
|
+
this.rl?.close();
|
|
54
|
+
this.rl = undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rendering + input contracts for the terminal TUI.
|
|
3
|
+
*
|
|
4
|
+
* `TranscriptRenderer` is the output-only surface the delivery controller
|
|
5
|
+
* (`terminal-context.ts`) writes to — progress, final answers, notices. It is
|
|
6
|
+
* deliberately small so it is trivial to fake in tests.
|
|
7
|
+
*
|
|
8
|
+
* `Frontend` is the full interactive shell the app drives: a `TranscriptRenderer`
|
|
9
|
+
* plus input events and lifecycle. Two implementations exist — a pi-tui rich UI
|
|
10
|
+
* (`pitui-frontend.ts`) and a plain stdout/readline fallback (`plain-frontend.ts`)
|
|
11
|
+
* for non-TTY / piped / `--print` use.
|
|
12
|
+
*/
|
|
13
|
+
import type { SlashCommand } from "@earendil-works/pi-tui";
|
|
14
|
+
export interface TranscriptRenderer {
|
|
15
|
+
/** A streamed progress line (tool call, thinking, assistant increment, error). */
|
|
16
|
+
appendProgress(text: string): void;
|
|
17
|
+
/** The final answer for a turn, rendered prominently (Markdown where supported). */
|
|
18
|
+
showFinal(markdown: string): void;
|
|
19
|
+
/** A side notice (skill change, model fallback), rendered subtly. */
|
|
20
|
+
showNotice(text: string): void;
|
|
21
|
+
/** Collapse the current turn's progress region (used before a replacing final / silence). */
|
|
22
|
+
clearProgress(): void;
|
|
23
|
+
/** Toggle the working indicator (spinner). */
|
|
24
|
+
setWorking(on: boolean): void;
|
|
25
|
+
}
|
|
26
|
+
export interface FrontendCallbacks {
|
|
27
|
+
/** User submitted a line of input. */
|
|
28
|
+
onSubmit(text: string): void;
|
|
29
|
+
/** User pressed the interrupt key (Ctrl-C). Two-stage policy lives in the app. */
|
|
30
|
+
onInterrupt(): void;
|
|
31
|
+
/** End of input (Ctrl-D on an empty line, or stdin closed). */
|
|
32
|
+
onEof(): void;
|
|
33
|
+
}
|
|
34
|
+
export interface Frontend extends TranscriptRenderer {
|
|
35
|
+
/** Begin surfacing input events. Non-blocking. */
|
|
36
|
+
start(callbacks: FrontendCallbacks): void;
|
|
37
|
+
/** Update the status line (model, context %, hints). */
|
|
38
|
+
setStatus(text: string): void;
|
|
39
|
+
/** Mark a turn in-flight: disable input echo of a new turn and show the spinner. */
|
|
40
|
+
setBusy(busy: boolean): void;
|
|
41
|
+
/** Show a one-time welcome banner at startup. */
|
|
42
|
+
showBanner(text: string): void;
|
|
43
|
+
/** Tear down: restore the terminal, stop reading input. */
|
|
44
|
+
stop(): void;
|
|
45
|
+
}
|
|
46
|
+
export interface FrontendOptions {
|
|
47
|
+
/** Force the plain frontend even on a TTY. */
|
|
48
|
+
plain?: boolean;
|
|
49
|
+
/** Plain frontend only: suppress progress/notice output. */
|
|
50
|
+
quiet?: boolean;
|
|
51
|
+
/** Plain frontend only: false for a one-shot run where the app supplies the prompt. */
|
|
52
|
+
interactive?: boolean;
|
|
53
|
+
/** Slash commands offered in editor autocomplete (pi-tui frontend). */
|
|
54
|
+
commands?: SlashCommand[];
|
|
55
|
+
/** Base path for autocomplete (pi-tui frontend). */
|
|
56
|
+
basePath?: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Pick a frontend: the rich pi-tui UI on a real TTY, else the plain stream
|
|
60
|
+
* (piped stdin/stdout, redirected output, or `--print`).
|
|
61
|
+
*/
|
|
62
|
+
export declare function createFrontend(options?: FrontendOptions): Frontend;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { PiTuiFrontend } from "./pitui-frontend.js";
|
|
2
|
+
import { PlainFrontend } from "./plain-frontend.js";
|
|
3
|
+
/**
|
|
4
|
+
* Pick a frontend: the rich pi-tui UI on a real TTY, else the plain stream
|
|
5
|
+
* (piped stdin/stdout, redirected output, or `--print`).
|
|
6
|
+
*/
|
|
7
|
+
export function createFrontend(options = {}) {
|
|
8
|
+
const isTty = process.stdout.isTTY === true && process.stdin.isTTY === true;
|
|
9
|
+
// `--print`/one-shot (interactive === false) must use the plain stream even on a
|
|
10
|
+
// TTY, or the answer is trapped in a full-screen pi-tui frame and never reaches a
|
|
11
|
+
// clean stdout.
|
|
12
|
+
if (options.plain || !isTty || options.interactive === false) {
|
|
13
|
+
return new PlainFrontend({ quiet: options.quiet, interactive: options.interactive });
|
|
14
|
+
}
|
|
15
|
+
return new PiTuiFrontend({ commands: options.commands, basePath: options.basePath });
|
|
16
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal implementation of the `ChannelContext` delivery contract.
|
|
3
|
+
*
|
|
4
|
+
* The DingTalk delivery controller (`runtime/delivery.ts`) drives AI Card
|
|
5
|
+
* streaming; the terminal has none of that protocol, so this controller is a
|
|
6
|
+
* thin mapping from the contract onto a `TranscriptRenderer`. Archiving
|
|
7
|
+
* semantics mirror `delivery.ts` so a channel produces the same conversation log
|
|
8
|
+
* regardless of which surface it is driven from:
|
|
9
|
+
* - `respond` / `respondPlain` archive when `shouldLog` is set;
|
|
10
|
+
* - `replaceMessage` / `deleteMessage` do not archive (they only re-render).
|
|
11
|
+
*/
|
|
12
|
+
import type { ChannelContext, FinalDelivery, ProgressStyle } from "../runtime/channel-context.js";
|
|
13
|
+
import type { ChannelStore } from "../runtime/store.js";
|
|
14
|
+
import type { TranscriptRenderer } from "./renderer.js";
|
|
15
|
+
/** The minimal per-turn input the terminal context needs (no DingTalk types). */
|
|
16
|
+
export interface TurnInput {
|
|
17
|
+
text: string;
|
|
18
|
+
user: string;
|
|
19
|
+
userName?: string;
|
|
20
|
+
channel: string;
|
|
21
|
+
ts: string;
|
|
22
|
+
}
|
|
23
|
+
export interface DeliveryTraits {
|
|
24
|
+
progressStyle: ProgressStyle;
|
|
25
|
+
finalDelivery: FinalDelivery;
|
|
26
|
+
}
|
|
27
|
+
export declare function createTerminalContext(input: TurnInput, renderer: TranscriptRenderer, store: ChannelStore, traits: DeliveryTraits): ChannelContext;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
class TerminalDeliveryController {
|
|
2
|
+
constructor(input, renderer, store, traits) {
|
|
3
|
+
this.input = input;
|
|
4
|
+
this.renderer = renderer;
|
|
5
|
+
this.store = store;
|
|
6
|
+
this.traits = traits;
|
|
7
|
+
this.closed = false;
|
|
8
|
+
this.finalDelivered = false;
|
|
9
|
+
}
|
|
10
|
+
buildContext() {
|
|
11
|
+
const { text, user, userName, channel, ts } = this.input;
|
|
12
|
+
return {
|
|
13
|
+
message: { text, rawText: text, user, userName, channel, ts },
|
|
14
|
+
channelName: channel,
|
|
15
|
+
respond: async (progress, shouldLog = true) => this.appendProgress(progress, shouldLog),
|
|
16
|
+
respondPlain: async (final, shouldLog = true) => this.sendFinal(final, shouldLog),
|
|
17
|
+
replaceMessage: async (final) => this.replaceWithFinal(final),
|
|
18
|
+
respondInThread: async (notice) => {
|
|
19
|
+
if (notice.trim()) {
|
|
20
|
+
this.renderer.showNotice(notice);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
setTyping: async () => { },
|
|
24
|
+
setWorking: async (working) => {
|
|
25
|
+
this.renderer.setWorking(working);
|
|
26
|
+
},
|
|
27
|
+
deleteMessage: async () => this.silence(),
|
|
28
|
+
primeCard: () => { },
|
|
29
|
+
flush: async () => { },
|
|
30
|
+
close: async () => {
|
|
31
|
+
this.closed = true;
|
|
32
|
+
},
|
|
33
|
+
progressStyle: this.traits.progressStyle,
|
|
34
|
+
finalDelivery: this.traits.finalDelivery,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
appendProgress(text, shouldLog) {
|
|
38
|
+
if (this.closed || this.finalDelivered || !text.trim())
|
|
39
|
+
return;
|
|
40
|
+
// final_card_only shows no progress; drop stray progress writes.
|
|
41
|
+
if (this.traits.progressStyle === "none")
|
|
42
|
+
return;
|
|
43
|
+
this.renderer.appendProgress(text);
|
|
44
|
+
if (shouldLog) {
|
|
45
|
+
this.archive(text);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
sendFinal(text, shouldLog) {
|
|
49
|
+
if (this.closed || this.finalDelivered)
|
|
50
|
+
return this.finalDelivered;
|
|
51
|
+
if (!text.trim())
|
|
52
|
+
return false;
|
|
53
|
+
this.renderer.showFinal(text);
|
|
54
|
+
if (shouldLog) {
|
|
55
|
+
this.archive(text);
|
|
56
|
+
}
|
|
57
|
+
this.finalDelivered = true;
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
replaceWithFinal(text) {
|
|
61
|
+
if (this.closed || this.finalDelivered)
|
|
62
|
+
return;
|
|
63
|
+
this.renderer.clearProgress();
|
|
64
|
+
if (text.trim()) {
|
|
65
|
+
this.renderer.showFinal(text);
|
|
66
|
+
}
|
|
67
|
+
this.finalDelivered = true;
|
|
68
|
+
}
|
|
69
|
+
silence() {
|
|
70
|
+
if (this.closed || this.finalDelivered)
|
|
71
|
+
return;
|
|
72
|
+
this.renderer.clearProgress();
|
|
73
|
+
this.finalDelivered = true;
|
|
74
|
+
}
|
|
75
|
+
archive(text) {
|
|
76
|
+
void this.store.logBotResponse(this.input.channel, text, Date.now().toString()).catch(() => {
|
|
77
|
+
// Best-effort: a failed archive must never break rendering. delivery.ts
|
|
78
|
+
// logs a warning here; the TUI keeps quiet to avoid corrupting the view.
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export function createTerminalContext(input, renderer, store, traits) {
|
|
83
|
+
return new TerminalDeliveryController(input, renderer, store, traits).buildContext();
|
|
84
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The TUI run-state machine: one channel, one user, turns serialized while still
|
|
3
|
+
* accepting steer / follow-up / stop mid-turn, two-stage Ctrl-C, and graceful
|
|
4
|
+
* shutdown with a memory flush.
|
|
5
|
+
*
|
|
6
|
+
* Collaborators (runner, frontend, store) are injected so the machine can be
|
|
7
|
+
* unit-tested with fakes and a fake clock. `app.ts` builds the real ones.
|
|
8
|
+
*/
|
|
9
|
+
import type { AgentRunner } from "../agent/types.js";
|
|
10
|
+
import type { ChannelStore } from "../runtime/store.js";
|
|
11
|
+
import type { SandboxConfig } from "../sandbox.js";
|
|
12
|
+
import type { Frontend } from "./renderer.js";
|
|
13
|
+
import { type DeliveryTraits } from "./terminal-context.js";
|
|
14
|
+
export interface TurnControllerDeps {
|
|
15
|
+
runner: AgentRunner;
|
|
16
|
+
frontend: Frontend;
|
|
17
|
+
store: ChannelStore;
|
|
18
|
+
traits: DeliveryTraits;
|
|
19
|
+
channelId: string;
|
|
20
|
+
userName: string;
|
|
21
|
+
/** Info commands that do not depend on run state. */
|
|
22
|
+
renderHelp: () => string;
|
|
23
|
+
renderUsage: (args: string) => string;
|
|
24
|
+
runEvents: (args: string) => Promise<string>;
|
|
25
|
+
/** Static bits the status renderer needs alongside the live run state. */
|
|
26
|
+
statusInfo: {
|
|
27
|
+
version: string;
|
|
28
|
+
sandbox: SandboxConfig;
|
|
29
|
+
startedAt: number;
|
|
30
|
+
};
|
|
31
|
+
/** Injectable clock for deterministic Ctrl-C timing in tests. */
|
|
32
|
+
now?: () => number;
|
|
33
|
+
}
|
|
34
|
+
export declare class TurnController {
|
|
35
|
+
private readonly deps;
|
|
36
|
+
private readonly now;
|
|
37
|
+
private readonly dispatchDeps;
|
|
38
|
+
private running;
|
|
39
|
+
private currentTaskText;
|
|
40
|
+
private currentTurn;
|
|
41
|
+
private readonly followups;
|
|
42
|
+
private submitChain;
|
|
43
|
+
private exitArmed;
|
|
44
|
+
private exiting;
|
|
45
|
+
private exitResolve;
|
|
46
|
+
private readonly exitPromise;
|
|
47
|
+
constructor(deps: TurnControllerDeps);
|
|
48
|
+
/** Interactive mode: wire input, run an optional first prompt, resolve on exit. */
|
|
49
|
+
startInteractive(initialPrompt?: string): Promise<void>;
|
|
50
|
+
/** One-shot mode: run a single prompt (if any) then shut down. */
|
|
51
|
+
runOnce(prompt?: string): Promise<void>;
|
|
52
|
+
/** Test/observability hook. */
|
|
53
|
+
isRunning(): boolean;
|
|
54
|
+
submit(text: string): void;
|
|
55
|
+
private processSubmit;
|
|
56
|
+
/** A plain message or /steer: steer the in-flight turn, else start a fresh one. */
|
|
57
|
+
private applyText;
|
|
58
|
+
private applyFollowup;
|
|
59
|
+
private beginTurn;
|
|
60
|
+
private drainFollowups;
|
|
61
|
+
private makeTurnInput;
|
|
62
|
+
private archiveIncoming;
|
|
63
|
+
private handleInterrupt;
|
|
64
|
+
private requestExit;
|
|
65
|
+
private buildWelcome;
|
|
66
|
+
private updateStatus;
|
|
67
|
+
private shutdown;
|
|
68
|
+
}
|