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