skyloom 1.10.0 → 1.12.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/src/cli/tui.ts ADDED
@@ -0,0 +1,269 @@
1
+ /**
2
+ * 天空织机 TUI — Full-screen terminal interface
3
+ *
4
+ * Layout:
5
+ * ┌─────────────────────────────────────────┐
6
+ * │ ≋ 雾 Fog · deepseek-chat · $0.02 · ⏻ │ ← header bar
7
+ * ├──────────┬──────────────────────────────┤
8
+ * │ ☼ 晴 Fair│ ✦ 你好!有什么可以帮你的? │
9
+ * │ ✱ 霜 │ │ ← messages
10
+ * │ ≋ 雾 ▸ │ 用户消息右对齐 │
11
+ * │ ❉ 雪 │ │
12
+ * │ ∘ 露 │ │
13
+ * │ ⸽ 雨 │ │
14
+ * ├──────────┴──────────────────────────────┤
15
+ * │ ┌─ /fog /rain /frost /snow ───────┐│ ← command palette (popup)
16
+ * │ ▶ /fog Switch to Fog ││
17
+ * │ /rain Switch to Rain ││
18
+ * │ └──────────────────────────────────────┘│
19
+ * │ > hello world [send] │ ← input bar
20
+ * └─────────────────────────────────────────┘
21
+ */
22
+
23
+ import * as readline from "readline";
24
+ import chalk from "chalk";
25
+
26
+ export interface TUIContext {
27
+ agent: any;
28
+ agents: Map<string, any>;
29
+ model: string;
30
+ cost: string;
31
+ width: number;
32
+ height: number;
33
+ }
34
+
35
+ /* ── Slash commands with icons ── */
36
+ const AGENT_CMDS: [string, string, string][] = [
37
+ ["≋", "/fog", "雾 Fog · 松烟墨"],
38
+ ["⸽", "/rain", "雨 Rain · 石青"],
39
+ ["✱", "/frost", "霜 Frost · 石绿"],
40
+ ["❉", "/snow", "雪 Snow · 铅白"],
41
+ ["∘", "/dew", "露 Dew · 赭石"],
42
+ ["☼", "/fair", "晴 Fair · 朱砂"],
43
+ ];
44
+
45
+ const ACTION_CMDS: [string, string][] = [
46
+ ["/help", "所有命令"],
47
+ ["/clear", "清屏"],
48
+ ["/status", "状态总览"],
49
+ ["/cost", "费用统计"],
50
+ ["/cost reset", "费用归零"],
51
+ ["/compact", "压缩上下文"],
52
+ ["/retry", "重发上条"],
53
+ ["/setup", "配置向导"],
54
+ ["/apikey set <p> <k>", "保存API Key"],
55
+ ["/apikey", "查看API Key"],
56
+ ["/model", "模型管理"],
57
+ ["/task <goal>", "多Agent编排"],
58
+ ["/memory", "记忆状态"],
59
+ ["/memory clear", "清除记忆"],
60
+ ["/sessions", "会话列表"],
61
+ ["/workspace", "工作空间"],
62
+ ["/mcp", "MCP服务器"],
63
+ ["/version", "版本信息"],
64
+ ["/quit", "退出"],
65
+ ];
66
+
67
+ /* ── Box drawing characters ── */
68
+ const B = { tl: "┌", tr: "┐", bl: "└", br: "┘", h: "─", v: "│", l: "├", r: "┤", cross: "┼", t: "┬", b: "┴", L: "░", o: "●" };
69
+
70
+ function bar(start: string, fill: string, end: string, width: number): string {
71
+ return start + fill.repeat(Math.max(0, width)) + end;
72
+ }
73
+
74
+ /* ── Render sidebar ── */
75
+ function renderSidebar(agent: any, agents: Map<string, any>, h: number): string[] {
76
+ const lines: string[] = [];
77
+ const W = 14; // sidebar width in chars
78
+
79
+ // Header
80
+ lines.push(chalk.cyan(bar(B.L + " 天空织机 ".padEnd(W - 2, B.L) + B.r, "", "", 0)));
81
+ lines.push(chalk.dim(B.v + " Skyloom " + B.v));
82
+
83
+ for (const n of ["fog", "rain", "frost", "snow", "dew", "fair"]) {
84
+ const isActive = agent.name === n;
85
+ const display: Record<string, string> = { fog: "≋ 雾 Fog", rain: "⸽ 雨 Rain", frost: "✱ 霜 Frost", snow: "❉ 雪 Snow", dew: "∘ 露 Dew", fair: "☼ 晴 Fair" };
86
+ const line = isActive
87
+ ? chalk.cyan(B.v + " " + B.o + " " + display[n].padEnd(W - 5) + B.v)
88
+ : chalk.dim(B.v + " " + display[n].padEnd(W - 5) + B.v);
89
+ lines.push(line);
90
+ }
91
+
92
+ // Fill remaining space
93
+ for (let i = lines.length; i < h; i++) {
94
+ lines.push(chalk.dim(B.v + " ".repeat(W - 2) + B.v));
95
+ }
96
+
97
+ // Footer
98
+ try {
99
+ const cu = agent.contextUsage();
100
+ const pct = cu.pct || 0;
101
+ lines.push(chalk.dim(B.v + " ctx " + String(pct).padStart(3) + "%" + " ".repeat(W - 10) + B.v));
102
+ } catch { lines.push(chalk.dim(B.v + " ".repeat(W - 2) + B.v)); }
103
+
104
+ lines.push(chalk.dim(bar(B.bl, B.h, B.br, W - 2)));
105
+ return lines;
106
+ }
107
+
108
+ /* ── Render command palette ── */
109
+ function renderPalette(filter: string, selIdx: number, width: number): string[] {
110
+ const lines: string[] = [];
111
+ const W = Math.min(width - 4, 56);
112
+
113
+ // Agent section first
114
+ const agentMatches = AGENT_CMDS.filter(([, cmd]) => cmd.includes(filter) || filter === "/");
115
+ const actionMatches = ACTION_CMDS.filter(([cmd]) => cmd.includes(filter));
116
+
117
+ const allItems: string[] = [];
118
+ for (const [icon, cmd, desc] of agentMatches) allItems.push(`${icon} ${cmd.padEnd(16)} ${desc}`);
119
+ for (const [cmd, desc] of actionMatches) allItems.push(` ${cmd.padEnd(18)} ${desc}`);
120
+
121
+ if (allItems.length === 0 && filter.length > 1) {
122
+ // No matches — show message
123
+ lines.push(chalk.dim(bar(B.tl, B.h, B.tr, W)));
124
+ lines.push(chalk.dim(B.v + " 未找到匹配命令 (esc 关闭)".padEnd(W) + B.v));
125
+ lines.push(chalk.dim(bar(B.bl, B.h, B.br, W)));
126
+ return lines;
127
+ }
128
+
129
+ if (allItems.length === 0) return lines;
130
+
131
+ const start = Math.max(0, Math.min(selIdx - 5, allItems.length - 10));
132
+ const end = Math.min(allItems.length, start + 10);
133
+
134
+ lines.push(chalk.dim(bar(B.tl, B.h, B.tr, W - 5)) + " ".padEnd(5));
135
+
136
+ for (let i = start; i < end; i++) {
137
+ const item = allItems[i];
138
+ const isSelected = i === selIdx;
139
+ const pad = W - item.replace(/\x1b\[[0-9;]*m/g, "").length + 2; // account for ANSI codes
140
+ lines.push(isSelected
141
+ ? chalk.cyan(B.v + " ▶ " + item).padEnd(W + 10) + chalk.cyan(B.v)
142
+ : chalk.dim(B.v + " " + item).padEnd(W + 10) + chalk.dim(B.v));
143
+ }
144
+
145
+ lines.push(chalk.dim(bar(B.bl, B.h, B.br, W - 5)) + " ".padEnd(5));
146
+ return lines;
147
+ }
148
+
149
+ /* ── Render message ── */
150
+ function renderMessage(role: string, text: string, width: number): string[] {
151
+ const lines: string[] = [];
152
+ const maxW = Math.min(width - 24, 60);
153
+ const prefix = role === "user" ? " " : " ";
154
+ const suffix = role === "user" ? "" : "";
155
+
156
+ for (const para of text.split("\n")) {
157
+ let remaining = para;
158
+ while (remaining.length > 0) {
159
+ const cut = remaining.length > maxW ? remaining.lastIndexOf(" ", maxW) : remaining.length;
160
+ const idx = cut > 0 ? cut : maxW;
161
+ const line = remaining.slice(0, idx).trimEnd();
162
+ if (role === "user") {
163
+ lines.push(chalk.dim(" ".repeat(Math.max(0, width - line.length - 4))) + chalk.cyan(line) + " ");
164
+ } else if (role === "assistant") {
165
+ lines.push(prefix + line + suffix);
166
+ } else {
167
+ lines.push(chalk.dim(" " + line));
168
+ }
169
+ remaining = remaining.slice(idx).trimStart();
170
+ }
171
+ }
172
+ return lines;
173
+ }
174
+
175
+ /* ── Read input with command palette ── */
176
+ export function readInput(stdin: NodeJS.ReadStream, stdout: NodeJS.WriteStream, ctx: TUIContext): Promise<string> {
177
+ return new Promise(resolve => {
178
+ let buf = "";
179
+ let cursor = 0;
180
+ let palette = false;
181
+ let selIdx = 0;
182
+
183
+ function render() {
184
+ // Clear screen and render full TUI
185
+ readline.cursorTo(stdout, 0, 0);
186
+ readline.clearScreenDown(stdout);
187
+
188
+ const w = stdout.columns || 80;
189
+ const h = stdout.rows || 24;
190
+ const sidebarW = 16;
191
+
192
+ // Header
193
+ stdout.write(chalk.bgBlack.cyan(" 天空织机 Skyloom v1.10 ".padEnd(w - 20, " ")) + chalk.bgBlack.dim(" deepseek".padEnd(10)) + chalk.bgBlack("\n"));
194
+ stdout.write(chalk.dim(bar("", B.h, "", w)) + "\n");
195
+
196
+ // Sidebar
197
+ const sidebar = renderSidebar(ctx.agent, ctx.agents, h - 5);
198
+ for (let i = 0; i < sidebar.length && i < h - 5; i++) {
199
+ stdout.write(sidebar[i] + "\n");
200
+ }
201
+
202
+ // Command palette (overlaid)
203
+ if (palette) {
204
+ const paletteLines = renderPalette(buf, selIdx, w);
205
+ // Move cursor up to position palette below header
206
+ const paletteY = 2;
207
+ for (let i = 0; i < paletteLines.length; i++) {
208
+ stdout.write(`\x1b[${paletteY + i};${sidebarW}H`); // position cursor
209
+ stdout.write(paletteLines[i]);
210
+ }
211
+ }
212
+
213
+ // Input bar at bottom
214
+ readline.cursorTo(stdout, sidebarW, h - 1);
215
+ stdout.write(chalk.dim(B.l + B.h.repeat(w - sidebarW - 2) + B.r));
216
+ readline.cursorTo(stdout, sidebarW, h);
217
+ stdout.write(chalk.cyan(" > ") + buf.slice(0, cursor) + chalk.inverse(buf[cursor] || " ") + buf.slice(cursor + 1));
218
+ }
219
+
220
+ if (!stdin.isTTY) {
221
+ const rl = readline.createInterface({ input: stdin });
222
+ rl.on("line", (line) => { rl.close(); resolve(line.trim()); });
223
+ return;
224
+ }
225
+
226
+ stdin.setRawMode(true);
227
+ stdin.resume();
228
+ render();
229
+
230
+ let escBuf = "";
231
+ stdin.on("data", (data: Buffer) => {
232
+ const str = data.toString();
233
+ escBuf += str;
234
+
235
+ if (escBuf.startsWith("\x1b[") && escBuf.length >= 3) {
236
+ const code = escBuf[2]; escBuf = "";
237
+ if (code === "A") { if (palette) selIdx = Math.max(0, selIdx - 1); render(); return; }
238
+ if (code === "B") { if (palette) { const all = [...AGENT_CMDS.map(c => c[1]), ...ACTION_CMDS.map(c => c[0])]; selIdx = Math.min(all.filter(a => a.includes(buf)).length - 1, selIdx + 1); } render(); return; }
239
+ if (code === "C") { if (cursor < buf.length) cursor++; render(); return; }
240
+ if (code === "D") { if (cursor > 0) cursor--; render(); return; }
241
+ }
242
+
243
+ for (const ch of escBuf) {
244
+ escBuf = "";
245
+ if (ch === "\x1b") { palette = false; render(); return; }
246
+ if (ch === "\r" || ch === "\n") {
247
+ if (palette) {
248
+ const all = [...AGENT_CMDS.map(c => c[1]), ...ACTION_CMDS.map(c => c[0])];
249
+ const filtered = all.filter(a => a.includes(buf));
250
+ if (filtered[selIdx]) buf = filtered[selIdx];
251
+ palette = false;
252
+ render();
253
+ stdin.setRawMode(false); stdin.pause(); resolve(buf.trim()); return;
254
+ }
255
+ stdin.setRawMode(false); stdin.pause(); resolve(buf.trim()); return;
256
+ }
257
+ if (ch === "\t") { /* ignore */ return; }
258
+ if (ch === "\x7f" || ch === "\b") { if (cursor > 0) { buf = buf.slice(0, cursor - 1) + buf.slice(cursor); cursor--; } if (!buf) palette = false; render(); return; }
259
+ if (ch === "\x03") { stdin.setRawMode(false); stdin.pause(); resolve("/quit"); return; }
260
+ if (ch >= " ") {
261
+ buf = buf.slice(0, cursor) + ch + buf.slice(cursor); cursor++;
262
+ if (ch === "/") { palette = true; selIdx = 0; }
263
+ else if (palette) selIdx = 0;
264
+ render(); return;
265
+ }
266
+ }
267
+ });
268
+ });
269
+ }