@oyasmi/pipiclaw 0.7.0 → 0.7.2
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 +1 -0
- package/dist/agent/channel-runner.d.ts +21 -4
- package/dist/agent/channel-runner.js +134 -18
- package/dist/agent/model-fallback.d.ts +64 -0
- package/dist/agent/model-fallback.js +125 -0
- 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/types.d.ts +9 -4
- package/dist/index.d.ts +2 -1
- package/dist/log.d.ts +7 -0
- package/dist/log.js +46 -24
- package/dist/main.js +15 -2
- package/dist/runtime/bootstrap.d.ts +17 -0
- package/dist/runtime/bootstrap.js +46 -76
- 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/sandbox.js +7 -77
- package/dist/security/command-guard.js +0 -4
- package/dist/security/path-guard.js +0 -4
- package/dist/settings.d.ts +20 -0
- package/dist/settings.js +18 -0
- package/dist/shared/shell-escape.d.ts +0 -7
- package/dist/shared/shell-escape.js +0 -11
- package/dist/tools/bash.js +2 -2
- package/dist/tools/edit.js +2 -2
- package/dist/tools/read.js +6 -6
- package/dist/tools/write-content.js +3 -3
- 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 +13 -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 +266 -0
- package/package.json +2 -1
- package/dist/security/platform.d.ts +0 -1
- package/dist/security/platform.js +0 -3
package/dist/tui/cli.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `pipiclaw tui` CLI: argument parsing and entry. Parsing is a pure function
|
|
3
|
+
* (`parseTuiArgs`) so it can be unit-tested; `runTui` wires it to `runTuiApp`.
|
|
4
|
+
*/
|
|
5
|
+
import { BootstrapExitError, readCliVersion } from "../runtime/bootstrap.js";
|
|
6
|
+
import { parseSandboxArg, SandboxConfigError } from "../sandbox.js";
|
|
7
|
+
import { runTuiApp } from "./app.js";
|
|
8
|
+
/** Parse `pipiclaw tui` arguments (everything after the `tui` subcommand). */
|
|
9
|
+
export function parseTuiArgs(args) {
|
|
10
|
+
let channel;
|
|
11
|
+
let sandbox = { type: "host" };
|
|
12
|
+
let print = false;
|
|
13
|
+
let quiet = false;
|
|
14
|
+
let plain = false;
|
|
15
|
+
const positional = [];
|
|
16
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
17
|
+
const arg = args[index];
|
|
18
|
+
if (arg === "--channel") {
|
|
19
|
+
channel = args[index + 1];
|
|
20
|
+
index += 1;
|
|
21
|
+
}
|
|
22
|
+
else if (arg.startsWith("--channel=")) {
|
|
23
|
+
channel = arg.slice("--channel=".length);
|
|
24
|
+
}
|
|
25
|
+
else if (arg === "--sandbox") {
|
|
26
|
+
sandbox = parseSandboxArg(args[index + 1] ?? "");
|
|
27
|
+
index += 1;
|
|
28
|
+
}
|
|
29
|
+
else if (arg.startsWith("--sandbox=")) {
|
|
30
|
+
sandbox = parseSandboxArg(arg.slice("--sandbox=".length));
|
|
31
|
+
}
|
|
32
|
+
else if (arg === "--print" || arg === "-p") {
|
|
33
|
+
print = true;
|
|
34
|
+
}
|
|
35
|
+
else if (arg === "--quiet" || arg === "-q") {
|
|
36
|
+
quiet = true;
|
|
37
|
+
}
|
|
38
|
+
else if (arg === "--plain") {
|
|
39
|
+
plain = true;
|
|
40
|
+
}
|
|
41
|
+
else if (arg === "--help" || arg === "-h") {
|
|
42
|
+
return { kind: "help" };
|
|
43
|
+
}
|
|
44
|
+
else if (arg === "--version") {
|
|
45
|
+
return { kind: "version" };
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
positional.push(arg);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { kind: "run", channel, sandbox, print, quiet, plain, positional };
|
|
52
|
+
}
|
|
53
|
+
function printTuiHelp(io) {
|
|
54
|
+
io.log("Usage: pipiclaw tui [options] [prompt]");
|
|
55
|
+
io.log("");
|
|
56
|
+
io.log("Chat with the pipiclaw agent in the terminal, reusing the same config,");
|
|
57
|
+
io.log("memory and session as the DingTalk runtime.");
|
|
58
|
+
io.log("");
|
|
59
|
+
io.log("Resume: there is no /resume command — a channel resumes itself.");
|
|
60
|
+
io.log("Re-running with the same --channel restores the previous conversation");
|
|
61
|
+
io.log("automatically (context is persisted per channel); memory carries longer-");
|
|
62
|
+
io.log("term facts across sessions. Use --channel to continue any past conversation.");
|
|
63
|
+
io.log("");
|
|
64
|
+
io.log("Options:");
|
|
65
|
+
io.log(" --channel <id> Channel to attach to (default: tui_local).");
|
|
66
|
+
io.log(" Use dm_<staffId> to share a DingTalk conversation's memory.");
|
|
67
|
+
io.log(" --sandbox=host Run tools on host (default)");
|
|
68
|
+
io.log(" --sandbox=docker:<name> Run tools in a Docker container");
|
|
69
|
+
io.log(" --print, -p One-shot: run [prompt] (or stdin), print the answer, exit");
|
|
70
|
+
io.log(" --quiet, -q Plain mode: print only the final answer");
|
|
71
|
+
io.log(" --plain Force the plain frontend (no full-screen UI)");
|
|
72
|
+
io.log(" --version Print the version and exit");
|
|
73
|
+
io.log("");
|
|
74
|
+
io.log("Note: do not attach --channel to a DingTalk conversation the daemon is actively serving.");
|
|
75
|
+
}
|
|
76
|
+
async function readStdin() {
|
|
77
|
+
const chunks = [];
|
|
78
|
+
for await (const chunk of process.stdin) {
|
|
79
|
+
chunks.push(chunk);
|
|
80
|
+
}
|
|
81
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
82
|
+
}
|
|
83
|
+
export async function runTui(argv, io = console) {
|
|
84
|
+
let parsed;
|
|
85
|
+
try {
|
|
86
|
+
parsed = parseTuiArgs(argv.slice(3));
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
if (err instanceof SandboxConfigError) {
|
|
90
|
+
io.error(`Error: ${err.message}`);
|
|
91
|
+
throw new BootstrapExitError(1);
|
|
92
|
+
}
|
|
93
|
+
throw err;
|
|
94
|
+
}
|
|
95
|
+
if (parsed.kind === "help") {
|
|
96
|
+
printTuiHelp(io);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (parsed.kind === "version") {
|
|
100
|
+
io.log(readCliVersion());
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
let initialPrompt = parsed.positional.join(" ").trim() || undefined;
|
|
104
|
+
if (parsed.print && !initialPrompt && process.stdin.isTTY !== true) {
|
|
105
|
+
initialPrompt = (await readStdin()).trim() || undefined;
|
|
106
|
+
}
|
|
107
|
+
await runTuiApp({
|
|
108
|
+
sandbox: parsed.sandbox,
|
|
109
|
+
channel: parsed.channel,
|
|
110
|
+
print: parsed.print,
|
|
111
|
+
quiet: parsed.quiet,
|
|
112
|
+
plain: parsed.plain,
|
|
113
|
+
initialPrompt,
|
|
114
|
+
io,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const bold: (text: string) => string;
|
|
2
|
+
export declare const dim: (text: string) => string;
|
|
3
|
+
export declare const italic: (text: string) => string;
|
|
4
|
+
export declare const underline: (text: string) => string;
|
|
5
|
+
export declare const strikethrough: (text: string) => string;
|
|
6
|
+
export declare const red: (text: string) => string;
|
|
7
|
+
export declare const yellow: (text: string) => string;
|
|
8
|
+
export declare const cyan: (text: string) => string;
|
|
9
|
+
export declare const gray: (text: string) => string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal ANSI color helpers for the TUI. pipiclaw has no chalk dependency, so
|
|
3
|
+
* these are hand-rolled SGR wrappers. Honors NO_COLOR (https://no-color.org) and
|
|
4
|
+
* a non-TTY stdout by degrading to identity functions.
|
|
5
|
+
*/
|
|
6
|
+
const enabled = !process.env.NO_COLOR && process.stdout.isTTY === true;
|
|
7
|
+
function sgr(open, close = 0) {
|
|
8
|
+
if (!enabled)
|
|
9
|
+
return (text) => text;
|
|
10
|
+
return (text) => `\x1b[${open}m${text}\x1b[${close}m`;
|
|
11
|
+
}
|
|
12
|
+
export const bold = sgr(1, 22);
|
|
13
|
+
export const dim = sgr(2, 22);
|
|
14
|
+
export const italic = sgr(3, 23);
|
|
15
|
+
export const underline = sgr(4, 24);
|
|
16
|
+
export const strikethrough = sgr(9, 29);
|
|
17
|
+
export const red = sgr(31);
|
|
18
|
+
export const yellow = sgr(33);
|
|
19
|
+
export const cyan = sgr(36);
|
|
20
|
+
export const gray = sgr(90);
|
|
@@ -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,13 @@
|
|
|
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
|
+
if (options.plain || !isTty) {
|
|
10
|
+
return new PlainFrontend({ quiet: options.quiet, interactive: options.interactive });
|
|
11
|
+
}
|
|
12
|
+
return new PiTuiFrontend({ commands: options.commands, basePath: options.basePath });
|
|
13
|
+
}
|
|
@@ -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;
|