skyloom 1.14.8 → 1.15.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/.github/workflows/ci.yml +2 -2
- package/.github/workflows/publish.yml +51 -4
- package/CONVERSION_PLAN.md +191 -191
- package/config/default.yaml +46 -43
- package/config/models.yaml +928 -155
- package/config/providers.yaml +109 -6
- package/dist/agents/snow.d.ts +2 -0
- package/dist/agents/snow.d.ts.map +1 -1
- package/dist/agents/snow.js +36 -5
- package/dist/agents/snow.js.map +1 -1
- package/dist/cli/loom_chat.d.ts.map +1 -1
- package/dist/cli/loom_chat.js +207 -1
- package/dist/cli/loom_chat.js.map +1 -1
- package/dist/cli/main.js +190 -40
- package/dist/cli/main.js.map +1 -1
- package/dist/cli/tui.d.ts.map +1 -1
- package/dist/cli/tui.js +6 -31
- package/dist/cli/tui.js.map +1 -1
- package/dist/core/agent.d.ts +6 -4
- package/dist/core/agent.d.ts.map +1 -1
- package/dist/core/agent.js +61 -20
- package/dist/core/agent.js.map +1 -1
- package/dist/core/catalog.d.ts.map +1 -1
- package/dist/core/catalog.js +30 -9
- package/dist/core/catalog.js.map +1 -1
- package/dist/core/commands.d.ts +110 -0
- package/dist/core/commands.d.ts.map +1 -0
- package/dist/core/commands.js +633 -0
- package/dist/core/commands.js.map +1 -0
- package/dist/core/concurrency.d.ts +38 -0
- package/dist/core/concurrency.d.ts.map +1 -0
- package/dist/core/concurrency.js +65 -0
- package/dist/core/concurrency.js.map +1 -0
- package/dist/core/factory.js +16 -16
- package/dist/core/file_checkpoint.d.ts +9 -0
- package/dist/core/file_checkpoint.d.ts.map +1 -1
- package/dist/core/file_checkpoint.js +33 -1
- package/dist/core/file_checkpoint.js.map +1 -1
- package/dist/core/llm.d.ts.map +1 -1
- package/dist/core/llm.js +66 -13
- package/dist/core/llm.js.map +1 -1
- package/dist/core/memory.js +51 -51
- package/dist/core/schemas.d.ts +16 -0
- package/dist/core/schemas.d.ts.map +1 -1
- package/dist/core/schemas.js +32 -0
- package/dist/core/schemas.js.map +1 -1
- package/dist/core/security.d.ts.map +1 -1
- package/dist/core/security.js +27 -0
- package/dist/core/security.js.map +1 -1
- package/dist/core/skymd.js +14 -14
- package/dist/core/trace.d.ts +105 -0
- package/dist/core/trace.d.ts.map +1 -0
- package/dist/core/trace.js +213 -0
- package/dist/core/trace.js.map +1 -0
- package/dist/tools/builtin.d.ts +2 -6
- package/dist/tools/builtin.d.ts.map +1 -1
- package/dist/tools/builtin.js +18 -111
- package/dist/tools/builtin.js.map +1 -1
- package/dist/tools/extra.d.ts +13 -0
- package/dist/tools/extra.d.ts.map +1 -0
- package/dist/tools/extra.js +827 -0
- package/dist/tools/extra.js.map +1 -0
- package/dist/tools/guards.d.ts +12 -0
- package/dist/tools/guards.d.ts.map +1 -0
- package/dist/tools/guards.js +143 -0
- package/dist/tools/guards.js.map +1 -0
- package/dist/tools/model_tool.d.ts.map +1 -1
- package/dist/tools/model_tool.js +24 -4
- package/dist/tools/model_tool.js.map +1 -1
- package/dist/web/markdown.d.ts +32 -0
- package/dist/web/markdown.d.ts.map +1 -0
- package/dist/web/markdown.js +202 -0
- package/dist/web/markdown.js.map +1 -0
- package/dist/web/server.d.ts +4 -0
- package/dist/web/server.d.ts.map +1 -1
- package/dist/web/server.js +14 -582
- package/dist/web/server.js.map +1 -1
- package/dist/web/ui.d.ts +31 -0
- package/dist/web/ui.d.ts.map +1 -0
- package/dist/web/ui.js +1009 -0
- package/dist/web/ui.js.map +1 -0
- package/docs/AESTHETIC_DESIGN.md +152 -152
- package/docs/OPTIMIZATION_PLAN.md +178 -178
- package/package.json +1 -1
- package/src/agents/snow.ts +38 -5
- package/src/cli/commands_md.ts +112 -112
- package/src/cli/input_macros.ts +83 -83
- package/src/cli/loom.ts +1041 -1041
- package/src/cli/loom_chat.ts +772 -603
- package/src/cli/main.ts +853 -723
- package/src/cli/tui.ts +264 -289
- package/src/core/agent/guard.ts +133 -133
- package/src/core/agent/task.ts +100 -100
- package/src/core/agent.ts +1630 -1590
- package/src/core/agent_helpers.ts +500 -500
- package/src/core/bus.ts +221 -221
- package/src/core/cache.ts +153 -153
- package/src/core/catalog.ts +199 -178
- package/src/core/circuit_breaker.ts +119 -119
- package/src/core/commands.ts +704 -0
- package/src/core/concurrency.ts +73 -0
- package/src/core/config.ts +365 -365
- package/src/core/constants.ts +95 -95
- package/src/core/factory.ts +656 -656
- package/src/core/file_checkpoint.ts +163 -136
- package/src/core/hooks.ts +126 -126
- package/src/core/llm.ts +972 -915
- package/src/core/logger.ts +143 -143
- package/src/core/mcp.ts +1001 -1001
- package/src/core/memory.ts +1201 -1201
- package/src/core/middleware.ts +350 -350
- package/src/core/model_config.ts +159 -159
- package/src/core/pipelines.ts +424 -424
- package/src/core/schemas.ts +319 -282
- package/src/core/security.ts +27 -0
- package/src/core/semantic.ts +211 -211
- package/src/core/skill.ts +384 -384
- package/src/core/skymd.ts +143 -143
- package/src/core/theme.ts +65 -65
- package/src/core/tool.ts +457 -457
- package/src/core/trace.ts +236 -0
- package/src/core/verify.ts +71 -71
- package/src/plugins/loader.ts +91 -91
- package/src/skills/loader.ts +75 -75
- package/src/tools/builtin.ts +571 -642
- package/src/tools/computer.ts +279 -279
- package/src/tools/extra.ts +662 -0
- package/src/tools/guards.ts +82 -0
- package/src/tools/model_tool.ts +93 -74
- package/src/tools/todo.ts +76 -76
- package/src/web/markdown.ts +193 -0
- package/src/web/server.ts +117 -693
- package/src/web/ui.ts +949 -0
- package/tests/agent.test.ts +211 -159
- package/tests/agent_helpers.test.ts +48 -48
- package/tests/catalog.test.ts +86 -86
- package/tests/checkpoint_commands.test.ts +124 -124
- package/tests/claude_compat.test.ts +110 -110
- package/tests/commands.test.ts +103 -0
- package/tests/concurrency.test.ts +102 -0
- package/tests/config.test.ts +41 -41
- package/tests/extra_tools.test.ts +212 -0
- package/tests/fence_plugin.test.ts +52 -52
- package/tests/guard.test.ts +75 -75
- package/tests/loom.test.ts +337 -337
- package/tests/memory.test.ts +170 -170
- package/tests/model_config.test.ts +109 -109
- package/tests/skymd.test.ts +146 -146
- package/tests/ssrf.test.ts +38 -38
- package/tests/structured_retry.test.ts +87 -0
- package/tests/task.test.ts +60 -60
- package/tests/todo_toolstats.test.ts +94 -94
- package/tests/trace.test.ts +128 -0
- package/tests/tui.test.ts +67 -67
- package/tests/web.test.ts +169 -0
- package/tsconfig.json +38 -38
package/src/cli/tui.ts
CHANGED
|
@@ -1,289 +1,264 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 天空织机 TUI — a polished *linear* terminal interface.
|
|
3
|
-
*
|
|
4
|
-
* Design note: the previous version tried to be a full-screen app, redrawing
|
|
5
|
-
* the whole screen on every keystroke while the reply streamed linearly below
|
|
6
|
-
* it — the two fought, the conversation never persisted, and hand-rolled
|
|
7
|
-
* raw-mode editing mangled CJK width. This rewrite is linear (like Claude Code
|
|
8
|
-
* / opencode): real readline line-editing + a CJK-aware wrapping stream
|
|
9
|
-
* renderer. Robust, flicker-free, and it actually reads like a conversation.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import * as readline from "readline";
|
|
13
|
-
import chalk from "chalk";
|
|
14
|
-
import { agentTheme, PALETTE } from "../core/theme";
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
.
|
|
66
|
-
.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
/** Visual
|
|
110
|
-
export function
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
private
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
this.
|
|
166
|
-
this.
|
|
167
|
-
this.color
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
history: [...inputHistory],
|
|
266
|
-
historySize: 200,
|
|
267
|
-
} as any);
|
|
268
|
-
rl.on("SIGINT", () => { out.write("\n" + chalk.dim(" 再会。\n")); rl.close(); process.exit(0); });
|
|
269
|
-
rl.question(promptFor(agentName), (answer) => {
|
|
270
|
-
const trimmed = answer.trim();
|
|
271
|
-
if (trimmed) inputHistory.unshift(trimmed);
|
|
272
|
-
rl.close();
|
|
273
|
-
resolve(trimmed);
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/** Render the inline slash-command palette (printed, not full-screen). */
|
|
279
|
-
export function renderPalette(filter: string): string {
|
|
280
|
-
const f = filter.toLowerCase();
|
|
281
|
-
const matches = SLASH_COMMANDS.filter(([c]) => c.toLowerCase().startsWith(f));
|
|
282
|
-
const list = matches.length ? matches : SLASH_COMMANDS;
|
|
283
|
-
const lines = list.slice(0, 12).map(([cmd, desc]) => {
|
|
284
|
-
const isAgent = ["/fog", "/rain", "/frost", "/snow", "/dew", "/fair"].includes(cmd.trim());
|
|
285
|
-
const name = isAgent ? chalk.hex(agentTheme(cmd.trim().slice(1)).hex)(cmd.padEnd(12)) : chalk.hex(PALETTE.inkMid)(cmd.padEnd(12));
|
|
286
|
-
return " " + name + chalk.hex(PALETTE.inkLight)(desc);
|
|
287
|
-
});
|
|
288
|
-
return chalk.dim(" 命令 · Tab 补全\n") + lines.join("\n") + "\n";
|
|
289
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 天空织机 TUI — a polished *linear* terminal interface.
|
|
3
|
+
*
|
|
4
|
+
* Design note: the previous version tried to be a full-screen app, redrawing
|
|
5
|
+
* the whole screen on every keystroke while the reply streamed linearly below
|
|
6
|
+
* it — the two fought, the conversation never persisted, and hand-rolled
|
|
7
|
+
* raw-mode editing mangled CJK width. This rewrite is linear (like Claude Code
|
|
8
|
+
* / opencode): real readline line-editing + a CJK-aware wrapping stream
|
|
9
|
+
* renderer. Robust, flicker-free, and it actually reads like a conversation.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import * as readline from "readline";
|
|
13
|
+
import chalk from "chalk";
|
|
14
|
+
import { agentTheme, PALETTE } from "../core/theme";
|
|
15
|
+
import { registry } from "../core/commands";
|
|
16
|
+
|
|
17
|
+
const TUI_VERSION = (() => { try { return require("../../package.json").version; } catch { return ""; } })();
|
|
18
|
+
|
|
19
|
+
export interface TUIContext {
|
|
20
|
+
agent: any;
|
|
21
|
+
agents: Map<string, any>;
|
|
22
|
+
model: string;
|
|
23
|
+
cost: string;
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/* ── Slash commands (for tab-completion + the inline palette) ──
|
|
29
|
+
Derived from the central command registry (src/core/commands.ts) so the
|
|
30
|
+
palette, tab-completer, and /help all stay in sync with a single source of
|
|
31
|
+
truth — no parallel hand-kept array to drift. */
|
|
32
|
+
export const SLASH_COMMANDS: [string, string][] = registry.slashItems("zh");
|
|
33
|
+
|
|
34
|
+
/* ════════════════════════════════════════
|
|
35
|
+
Markdown stripping — clean raw md for terminal display
|
|
36
|
+
════════════════════════════════════════ */
|
|
37
|
+
/** Strip common markdown formatting for clean terminal output. */
|
|
38
|
+
export function stripMarkdown(s: string): string {
|
|
39
|
+
return s
|
|
40
|
+
.replace(/^#{1,4}\s+/gm, '') // headings
|
|
41
|
+
.replace(/\*\*(.+?)\*\*/g, '$1') // bold
|
|
42
|
+
.replace(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/g, '$1') // italic
|
|
43
|
+
.replace(/__([^_]+)__/g, '$1') // alt bold
|
|
44
|
+
.replace(/_([^_]+)_/g, '$1') // alt italic
|
|
45
|
+
.replace(/`([^`]+)`/g, '$1') // inline code
|
|
46
|
+
.replace(/~~(.+?)~~/g, '$1') // strikethrough
|
|
47
|
+
.replace(/^\s*[-*+]\s+/gm, '• ') // bullets
|
|
48
|
+
.replace(/^\s*\d+[.)]\s+/gm, ' ') // numbered lists
|
|
49
|
+
.replace(/^\|.*\|$/gm, (line) => { // tables → spaced columns
|
|
50
|
+
return line.replace(/\|/g, ' ').replace(/-{2,}/g, '──');
|
|
51
|
+
})
|
|
52
|
+
.replace(/\n{3,}/g, '\n\n'); // collapse excess newlines
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Page output through a simple pager (Enter to continue, q to quit) */
|
|
56
|
+
export async function pageOutput(out: NodeJS.WriteStream, text: string): Promise<void> {
|
|
57
|
+
const lines = text.split('\n');
|
|
58
|
+
const h = (out.rows || 24) - 2;
|
|
59
|
+
if (lines.length <= h) { out.write(text + '\n'); return; }
|
|
60
|
+
|
|
61
|
+
const readline = require('readline');
|
|
62
|
+
const rl = readline.createInterface({ input: process.stdin, output: out });
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < lines.length; i += h) {
|
|
65
|
+
const chunk = lines.slice(i, i + h).join('\n');
|
|
66
|
+
out.write(chunk + '\n');
|
|
67
|
+
if (i + h < lines.length) {
|
|
68
|
+
const remaining = lines.length - i - h;
|
|
69
|
+
out.write(`\x1b[7m ── ${remaining} more lines · Enter=next · q=quit ── \x1b[0m\n`);
|
|
70
|
+
const key: string = await new Promise(r => {
|
|
71
|
+
process.stdin.setRawMode?.(true);
|
|
72
|
+
process.stdin.resume();
|
|
73
|
+
process.stdin.once('data', (d: Buffer) => { process.stdin.setRawMode?.(false); process.stdin.pause(); r(d.toString()); });
|
|
74
|
+
});
|
|
75
|
+
if (key === 'q' || key === '\x03') { rl.close(); return; }
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
rl.close();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* ════════════════════════════════════════
|
|
82
|
+
CJK-aware display width
|
|
83
|
+
════════════════════════════════════════ */
|
|
84
|
+
/** Visual columns occupied by a single code point (CJK / fullwidth = 2). */
|
|
85
|
+
export function charWidth(cp: number): number {
|
|
86
|
+
if (cp === 0) return 0;
|
|
87
|
+
if (cp < 32 || (cp >= 0x7f && cp < 0xa0)) return 0; // control
|
|
88
|
+
// East-Asian wide / fullwidth ranges
|
|
89
|
+
if (
|
|
90
|
+
(cp >= 0x1100 && cp <= 0x115f) || // Hangul Jamo
|
|
91
|
+
(cp >= 0x2e80 && cp <= 0x303e) || // CJK radicals, Kangxi, punctuation
|
|
92
|
+
(cp >= 0x3041 && cp <= 0x33ff) || // Hiragana…CJK symbols
|
|
93
|
+
(cp >= 0x3400 && cp <= 0x4dbf) || // CJK Ext A
|
|
94
|
+
(cp >= 0x4e00 && cp <= 0x9fff) || // CJK Unified
|
|
95
|
+
(cp >= 0xa000 && cp <= 0xa4cf) || // Yi
|
|
96
|
+
(cp >= 0xac00 && cp <= 0xd7a3) || // Hangul syllables
|
|
97
|
+
(cp >= 0xf900 && cp <= 0xfaff) || // CJK compat
|
|
98
|
+
(cp >= 0xfe10 && cp <= 0xfe19) ||
|
|
99
|
+
(cp >= 0xfe30 && cp <= 0xfe6f) || // CJK compat forms
|
|
100
|
+
(cp >= 0xff00 && cp <= 0xff60) || // Fullwidth forms
|
|
101
|
+
(cp >= 0xffe0 && cp <= 0xffe6) ||
|
|
102
|
+
(cp >= 0x1f300 && cp <= 0x1faff) // emoji / pictographs
|
|
103
|
+
) return 2;
|
|
104
|
+
return 1;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const ANSI_RE = /\x1b\[[0-9;]*m/g;
|
|
108
|
+
|
|
109
|
+
/** Visual width of a string, ignoring ANSI color codes. */
|
|
110
|
+
export function visualWidth(s: string): number {
|
|
111
|
+
let w = 0;
|
|
112
|
+
for (const ch of s.replace(ANSI_RE, "")) w += charWidth(ch.codePointAt(0) || 0);
|
|
113
|
+
return w;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Pad a string (containing ANSI) to a visual width. */
|
|
117
|
+
export function padVisual(s: string, width: number): string {
|
|
118
|
+
const diff = width - visualWidth(s);
|
|
119
|
+
return diff > 0 ? s + " ".repeat(diff) : s;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* ════════════════════════════════════════
|
|
123
|
+
Streaming renderer — word-wrap aware, CJK aware
|
|
124
|
+
════════════════════════════════════════ */
|
|
125
|
+
/**
|
|
126
|
+
* Writes streamed text with a fixed left gutter, wrapping at the terminal
|
|
127
|
+
* width. English wraps on word boundaries; CJK wraps per glyph. Color is
|
|
128
|
+
* applied per flushed chunk so styling survives wrapping.
|
|
129
|
+
*/
|
|
130
|
+
export class StreamRenderer {
|
|
131
|
+
private col = 0;
|
|
132
|
+
private word = "";
|
|
133
|
+
private atLineStart = true;
|
|
134
|
+
private out: NodeJS.WriteStream;
|
|
135
|
+
private gutter: string;
|
|
136
|
+
private maxCols: number;
|
|
137
|
+
private color: (s: string) => string;
|
|
138
|
+
|
|
139
|
+
constructor(out: NodeJS.WriteStream, opts?: { gutter?: string; color?: (s: string) => string }) {
|
|
140
|
+
this.out = out;
|
|
141
|
+
this.gutter = opts?.gutter ?? " ";
|
|
142
|
+
this.color = opts?.color ?? ((s) => s);
|
|
143
|
+
const cols = out.columns || 80;
|
|
144
|
+
// content width excludes the gutter; clamp for readability
|
|
145
|
+
this.maxCols = Math.max(32, Math.min(cols - visualWidth(this.gutter) - 1, 96));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Lazily emit the left gutter at the start of each visual line. */
|
|
149
|
+
private startLine() { if (this.atLineStart) { this.out.write(this.gutter); this.atLineStart = false; } }
|
|
150
|
+
private newline() { this.out.write("\n"); this.atLineStart = true; this.col = 0; }
|
|
151
|
+
|
|
152
|
+
/** Strip asterisk formatting: **text** → text, *text* → text, __text__ → text */
|
|
153
|
+
private cleanWord(s: string): string {
|
|
154
|
+
return s.replace(/\*\*(.+?)\*\*/g, (_, t) => t)
|
|
155
|
+
.replace(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/g, (_, t) => t)
|
|
156
|
+
.replace(/__([^_]+)__/g, (_, t) => t)
|
|
157
|
+
.replace(/_([^_]+)_/g, (_, t) => t);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private flushWord() {
|
|
161
|
+
if (!this.word) return;
|
|
162
|
+
const cleaned = this.cleanWord(this.word);
|
|
163
|
+
if (!cleaned) { this.word = ""; return; }
|
|
164
|
+
const w = visualWidth(cleaned);
|
|
165
|
+
if (this.col > 0 && this.col + w > this.maxCols) this.newline();
|
|
166
|
+
this.startLine();
|
|
167
|
+
this.out.write(this.color(cleaned));
|
|
168
|
+
this.col += w;
|
|
169
|
+
this.word = "";
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Feed a chunk of streamed text. Filters out raw md formatting. */
|
|
173
|
+
write(text: string) {
|
|
174
|
+
for (const ch of text) {
|
|
175
|
+
if (ch === "\r") continue;
|
|
176
|
+
if (ch === "\n") { this.flushWord(); this.newline(); continue; }
|
|
177
|
+
if (ch === " " || ch === "\t") {
|
|
178
|
+
this.flushWord();
|
|
179
|
+
if (this.col > 0 && this.col < this.maxCols) { this.startLine(); this.out.write(" "); this.col += 1; }
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
// Strip heading markers (# ## ###) at line start
|
|
183
|
+
if (ch === "#" && this.atLineStart) { continue; }
|
|
184
|
+
// Strip inline code backticks
|
|
185
|
+
if (ch === "`") { this.flushWord(); continue; }
|
|
186
|
+
const cp = ch.codePointAt(0) || 0;
|
|
187
|
+
if (charWidth(cp) === 2) {
|
|
188
|
+
// CJK / wide: flush any pending latin word, then place this glyph
|
|
189
|
+
this.flushWord();
|
|
190
|
+
if (this.col > 0 && this.col + 2 > this.maxCols) this.newline();
|
|
191
|
+
this.startLine();
|
|
192
|
+
this.out.write(this.color(ch));
|
|
193
|
+
this.col += 2;
|
|
194
|
+
} else {
|
|
195
|
+
this.word += ch;
|
|
196
|
+
// very long unbroken token: hard-break to avoid overflow
|
|
197
|
+
if (visualWidth(this.word) >= this.maxCols) this.flushWord();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Flush any buffered word (call before switching styles / ending). */
|
|
203
|
+
flush() { this.flushWord(); }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/* ════════════════════════════════════════
|
|
207
|
+
Input — readline-based, robust line editing
|
|
208
|
+
════════════════════════════════════════ */
|
|
209
|
+
/** Tab-completer for slash commands. */
|
|
210
|
+
function slashCompleter(line: string): [string[], string] {
|
|
211
|
+
if (!line.startsWith("/")) return [[], line];
|
|
212
|
+
const names = SLASH_COMMANDS.map(([c]) => c.trimEnd());
|
|
213
|
+
const hits = names.filter((c) => c.startsWith(line));
|
|
214
|
+
return [hits.length ? hits : names, line];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The prompt string for an agent: a small mineral seal + chevron. */
|
|
218
|
+
export function promptFor(agentName: string): string {
|
|
219
|
+
const t = agentTheme(agentName);
|
|
220
|
+
return chalk.hex(t.hex)(` ${t.symbol} ${t.kanji} `) + chalk.hex(PALETTE.inkLight)("❯ ");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Cross-turn input history (↑/↓), shared by every per-turn reader. */
|
|
224
|
+
const inputHistory: string[] = [];
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Read one line with the agent-themed prompt. A fresh readline interface is
|
|
228
|
+
* created and closed per call — this deliberately avoids clashing with the
|
|
229
|
+
* separate readline prompts used by the setup wizard and tool-approval flow
|
|
230
|
+
* (two live interfaces on one stdin corrupt input). History is preserved
|
|
231
|
+
* manually across turns.
|
|
232
|
+
*/
|
|
233
|
+
export function readLine(agentName: string, out: NodeJS.WriteStream = process.stdout): Promise<string> {
|
|
234
|
+
return new Promise((resolve) => {
|
|
235
|
+
const rl = readline.createInterface({
|
|
236
|
+
input: process.stdin,
|
|
237
|
+
output: out,
|
|
238
|
+
completer: slashCompleter,
|
|
239
|
+
terminal: process.stdin.isTTY ?? false,
|
|
240
|
+
history: [...inputHistory],
|
|
241
|
+
historySize: 200,
|
|
242
|
+
} as any);
|
|
243
|
+
rl.on("SIGINT", () => { out.write("\n" + chalk.dim(" 再会。\n")); rl.close(); process.exit(0); });
|
|
244
|
+
rl.question(promptFor(agentName), (answer) => {
|
|
245
|
+
const trimmed = answer.trim();
|
|
246
|
+
if (trimmed) inputHistory.unshift(trimmed);
|
|
247
|
+
rl.close();
|
|
248
|
+
resolve(trimmed);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Render the inline slash-command palette (printed, not full-screen). */
|
|
254
|
+
export function renderPalette(filter: string): string {
|
|
255
|
+
const f = filter.toLowerCase();
|
|
256
|
+
const matches = SLASH_COMMANDS.filter(([c]) => c.toLowerCase().startsWith(f));
|
|
257
|
+
const list = matches.length ? matches : SLASH_COMMANDS;
|
|
258
|
+
const lines = list.slice(0, 12).map(([cmd, desc]) => {
|
|
259
|
+
const isAgent = ["/fog", "/rain", "/frost", "/snow", "/dew", "/fair"].includes(cmd.trim());
|
|
260
|
+
const name = isAgent ? chalk.hex(agentTheme(cmd.trim().slice(1)).hex)(cmd.padEnd(12)) : chalk.hex(PALETTE.inkMid)(cmd.padEnd(12));
|
|
261
|
+
return " " + name + chalk.hex(PALETTE.inkLight)(desc);
|
|
262
|
+
});
|
|
263
|
+
return chalk.dim(" 命令 · Tab 补全\n") + lines.join("\n") + "\n";
|
|
264
|
+
}
|