skyloom 1.13.0 → 1.13.1

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 CHANGED
@@ -1,272 +1,220 @@
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
- }
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
+ const TUI_VERSION = (() => { try { return require("../../package.json").version; } catch { return ""; } })();
17
+
18
+ export interface TUIContext {
19
+ agent: any;
20
+ agents: Map<string, any>;
21
+ model: string;
22
+ cost: string;
23
+ width: number;
24
+ height: number;
25
+ }
26
+
27
+ /* ── Slash commands (for tab-completion + the inline palette) ── */
28
+ export const SLASH_COMMANDS: [string, string][] = [
29
+ ["/fog", "≋ · 探索洞察"],
30
+ ["/rain", "⸽ 雨 · 创造产出"],
31
+ ["/frost", "✱ 霜 · 精炼品质"],
32
+ ["/snow", "❉ 雪 · 架构规划"],
33
+ ["/dew", "∘ 露 · 可靠守护"],
34
+ ["/fair", "☼ 晴 · 情感陪伴"],
35
+ ["/help", "查看所有命令"],
36
+ ["/setup", "配置向导"],
37
+ ["/model", "模型信息"],
38
+ ["/cost", "费用统计"],
39
+ ["/status", "状态总览"],
40
+ ["/memory", "记忆状态"],
41
+ ["/sessions", "会话列表"],
42
+ ["/workspace", "工作空间"],
43
+ ["/compact", "压缩上下文"],
44
+ ["/clear", "清屏"],
45
+ ["/task ", " Agent 编排"],
46
+ ["/mcp", "MCP 服务器"],
47
+ ["/version", "版本信息"],
48
+ ["/quit", "退出"],
49
+ ];
50
+
51
+ /* ════════════════════════════════════════
52
+ CJK-aware display width
53
+ ════════════════════════════════════════ */
54
+ /** Visual columns occupied by a single code point (CJK / fullwidth = 2). */
55
+ export function charWidth(cp: number): number {
56
+ if (cp === 0) return 0;
57
+ if (cp < 32 || (cp >= 0x7f && cp < 0xa0)) return 0; // control
58
+ // East-Asian wide / fullwidth ranges
59
+ if (
60
+ (cp >= 0x1100 && cp <= 0x115f) || // Hangul Jamo
61
+ (cp >= 0x2e80 && cp <= 0x303e) || // CJK radicals, Kangxi, punctuation
62
+ (cp >= 0x3041 && cp <= 0x33ff) || // Hiragana…CJK symbols
63
+ (cp >= 0x3400 && cp <= 0x4dbf) || // CJK Ext A
64
+ (cp >= 0x4e00 && cp <= 0x9fff) || // CJK Unified
65
+ (cp >= 0xa000 && cp <= 0xa4cf) || // Yi
66
+ (cp >= 0xac00 && cp <= 0xd7a3) || // Hangul syllables
67
+ (cp >= 0xf900 && cp <= 0xfaff) || // CJK compat
68
+ (cp >= 0xfe10 && cp <= 0xfe19) ||
69
+ (cp >= 0xfe30 && cp <= 0xfe6f) || // CJK compat forms
70
+ (cp >= 0xff00 && cp <= 0xff60) || // Fullwidth forms
71
+ (cp >= 0xffe0 && cp <= 0xffe6) ||
72
+ (cp >= 0x1f300 && cp <= 0x1faff) // emoji / pictographs
73
+ ) return 2;
74
+ return 1;
75
+ }
76
+
77
+ const ANSI_RE = /\x1b\[[0-9;]*m/g;
78
+
79
+ /** Visual width of a string, ignoring ANSI color codes. */
80
+ export function visualWidth(s: string): number {
81
+ let w = 0;
82
+ for (const ch of s.replace(ANSI_RE, "")) w += charWidth(ch.codePointAt(0) || 0);
83
+ return w;
84
+ }
85
+
86
+ /** Pad a string (containing ANSI) to a visual width. */
87
+ export function padVisual(s: string, width: number): string {
88
+ const diff = width - visualWidth(s);
89
+ return diff > 0 ? s + " ".repeat(diff) : s;
90
+ }
91
+
92
+ /* ════════════════════════════════════════
93
+ Streaming renderer — word-wrap aware, CJK aware
94
+ ════════════════════════════════════════ */
95
+ /**
96
+ * Writes streamed text with a fixed left gutter, wrapping at the terminal
97
+ * width. English wraps on word boundaries; CJK wraps per glyph. Color is
98
+ * applied per flushed chunk so styling survives wrapping.
99
+ */
100
+ export class StreamRenderer {
101
+ private col = 0;
102
+ private word = "";
103
+ private atLineStart = true;
104
+ private out: NodeJS.WriteStream;
105
+ private gutter: string;
106
+ private maxCols: number;
107
+ private color: (s: string) => string;
108
+
109
+ constructor(out: NodeJS.WriteStream, opts?: { gutter?: string; color?: (s: string) => string }) {
110
+ this.out = out;
111
+ this.gutter = opts?.gutter ?? " ";
112
+ this.color = opts?.color ?? ((s) => s);
113
+ const cols = out.columns || 80;
114
+ // content width excludes the gutter; clamp for readability
115
+ this.maxCols = Math.max(32, Math.min(cols - visualWidth(this.gutter) - 1, 96));
116
+ }
117
+
118
+ /** Lazily emit the left gutter at the start of each visual line. */
119
+ private startLine() { if (this.atLineStart) { this.out.write(this.gutter); this.atLineStart = false; } }
120
+ private newline() { this.out.write("\n"); this.atLineStart = true; this.col = 0; }
121
+
122
+ private flushWord() {
123
+ if (!this.word) return;
124
+ const w = visualWidth(this.word);
125
+ if (this.col > 0 && this.col + w > this.maxCols) this.newline();
126
+ this.startLine();
127
+ this.out.write(this.color(this.word));
128
+ this.col += w;
129
+ this.word = "";
130
+ }
131
+
132
+ /** Feed a chunk of streamed text. */
133
+ write(text: string) {
134
+ for (const ch of text) {
135
+ if (ch === "\r") continue; // normalize CRLF / stray CR from providers
136
+ if (ch === "\n") { this.flushWord(); this.newline(); continue; }
137
+ if (ch === " " || ch === "\t") {
138
+ this.flushWord();
139
+ if (this.col > 0 && this.col < this.maxCols) { this.startLine(); this.out.write(" "); this.col += 1; }
140
+ continue;
141
+ }
142
+ const cp = ch.codePointAt(0) || 0;
143
+ if (charWidth(cp) === 2) {
144
+ // CJK / wide: flush any pending latin word, then place this glyph
145
+ this.flushWord();
146
+ if (this.col > 0 && this.col + 2 > this.maxCols) this.newline();
147
+ this.startLine();
148
+ this.out.write(this.color(ch));
149
+ this.col += 2;
150
+ } else {
151
+ this.word += ch;
152
+ // very long unbroken token: hard-break to avoid overflow
153
+ if (visualWidth(this.word) >= this.maxCols) this.flushWord();
154
+ }
155
+ }
156
+ }
157
+
158
+ /** Flush any buffered word (call before switching styles / ending). */
159
+ flush() { this.flushWord(); }
160
+ }
161
+
162
+ /* ════════════════════════════════════════
163
+ Input readline-based, robust line editing
164
+ ════════════════════════════════════════ */
165
+ /** Tab-completer for slash commands. */
166
+ function slashCompleter(line: string): [string[], string] {
167
+ if (!line.startsWith("/")) return [[], line];
168
+ const names = SLASH_COMMANDS.map(([c]) => c.trimEnd());
169
+ const hits = names.filter((c) => c.startsWith(line));
170
+ return [hits.length ? hits : names, line];
171
+ }
172
+
173
+ /** The prompt string for an agent: a small mineral seal + chevron. */
174
+ export function promptFor(agentName: string): string {
175
+ const t = agentTheme(agentName);
176
+ return chalk.hex(t.hex)(` ${t.symbol} ${t.kanji} `) + chalk.hex(PALETTE.inkLight)("❯ ");
177
+ }
178
+
179
+ /** Cross-turn input history (↑/↓), shared by every per-turn reader. */
180
+ const inputHistory: string[] = [];
181
+
182
+ /**
183
+ * Read one line with the agent-themed prompt. A fresh readline interface is
184
+ * created and closed per call — this deliberately avoids clashing with the
185
+ * separate readline prompts used by the setup wizard and tool-approval flow
186
+ * (two live interfaces on one stdin corrupt input). History is preserved
187
+ * manually across turns.
188
+ */
189
+ export function readLine(agentName: string, out: NodeJS.WriteStream = process.stdout): Promise<string> {
190
+ return new Promise((resolve) => {
191
+ const rl = readline.createInterface({
192
+ input: process.stdin,
193
+ output: out,
194
+ completer: slashCompleter,
195
+ terminal: process.stdin.isTTY ?? false,
196
+ history: [...inputHistory],
197
+ historySize: 200,
198
+ } as any);
199
+ rl.on("SIGINT", () => { out.write("\n" + chalk.dim(" 再会。\n")); rl.close(); process.exit(0); });
200
+ rl.question(promptFor(agentName), (answer) => {
201
+ const trimmed = answer.trim();
202
+ if (trimmed) inputHistory.unshift(trimmed);
203
+ rl.close();
204
+ resolve(trimmed);
205
+ });
206
+ });
207
+ }
208
+
209
+ /** Render the inline slash-command palette (printed, not full-screen). */
210
+ export function renderPalette(filter: string): string {
211
+ const f = filter.toLowerCase();
212
+ const matches = SLASH_COMMANDS.filter(([c]) => c.toLowerCase().startsWith(f));
213
+ const list = matches.length ? matches : SLASH_COMMANDS;
214
+ const lines = list.slice(0, 12).map(([cmd, desc]) => {
215
+ const isAgent = ["/fog", "/rain", "/frost", "/snow", "/dew", "/fair"].includes(cmd.trim());
216
+ const name = isAgent ? chalk.hex(agentTheme(cmd.trim().slice(1)).hex)(cmd.padEnd(12)) : chalk.hex(PALETTE.inkMid)(cmd.padEnd(12));
217
+ return " " + name + chalk.hex(PALETTE.inkLight)(desc);
218
+ });
219
+ return chalk.dim(" 命令 · Tab 补全\n") + lines.join("\n") + "\n";
220
+ }
@@ -0,0 +1,67 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { charWidth, visualWidth, padVisual, StreamRenderer } from "../src/cli/tui";
3
+
4
+ describe("CJK-aware width", () => {
5
+ it("counts ascii as 1, CJK as 2", () => {
6
+ expect(charWidth("a".codePointAt(0)!)).toBe(1);
7
+ expect(charWidth("雾".codePointAt(0)!)).toBe(2);
8
+ expect(charWidth(",".codePointAt(0)!)).toBe(2); // fullwidth comma
9
+ });
10
+
11
+ it("treats control chars as width 0", () => {
12
+ expect(charWidth("\r".codePointAt(0)!)).toBe(0);
13
+ expect(charWidth("\n".codePointAt(0)!)).toBe(0);
14
+ });
15
+
16
+ it("visualWidth sums correctly and ignores ANSI", () => {
17
+ expect(visualWidth("abc")).toBe(3);
18
+ expect(visualWidth("雾雨")).toBe(4);
19
+ expect(visualWidth("a雾b")).toBe(4);
20
+ expect(visualWidth("\x1b[36m雾\x1b[39m")).toBe(2); // color codes don't count
21
+ });
22
+
23
+ it("padVisual pads to a visual column count", () => {
24
+ expect(visualWidth(padVisual("雾", 6))).toBe(6);
25
+ expect(padVisual("abc", 2)).toBe("abc"); // never truncates
26
+ });
27
+ });
28
+
29
+ /** Capture writes from a StreamRenderer into a string. */
30
+ function render(text: string, columns = 40, chunk = 3): string {
31
+ let buf = "";
32
+ const fakeOut = { columns, write: (s: string) => { buf += s; return true; } } as any;
33
+ const r = new StreamRenderer(fakeOut, { gutter: " " });
34
+ for (let i = 0; i < text.length; i += chunk) r.write(text.slice(i, i + chunk));
35
+ r.flush();
36
+ return buf;
37
+ }
38
+
39
+ describe("StreamRenderer", () => {
40
+ it("prefixes every line with the gutter", () => {
41
+ const out = render("hello world", 80);
42
+ expect(out.startsWith(" ")).toBe(true);
43
+ });
44
+
45
+ it("never exceeds the content width per visual line", () => {
46
+ const out = render("天空织机是一个本地优先的多智能体终端框架用于验证换行宽度限制是否生效啊", 40);
47
+ const maxContent = Math.min(40 - 2 - 1, 96);
48
+ for (const line of out.split("\n")) {
49
+ expect(visualWidth(line)).toBeLessThanOrEqual(2 + maxContent); // gutter + content
50
+ }
51
+ });
52
+
53
+ it("strips stray carriage returns (CRLF from providers)", () => {
54
+ const out = render("line one\r\nline two", 80);
55
+ expect(out.includes("\r")).toBe(false);
56
+ expect(out).toContain("line one");
57
+ expect(out).toContain("line two");
58
+ });
59
+
60
+ it("wraps English on word boundaries without splitting short words", () => {
61
+ // maxCols floors at 32, so use text long enough to exceed it.
62
+ const out = render("alpha beta gamma delta epsilon zeta eta theta iota kappa", 40);
63
+ expect(out.split("\n").length).toBeGreaterThan(1);
64
+ // no whole word should be broken across a wrap (each appears intact)
65
+ for (const w of ["alpha", "epsilon", "kappa"]) expect(out).toContain(w);
66
+ });
67
+ });