ptywright 0.1.0 → 0.2.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 (68) hide show
  1. package/README.md +459 -116
  2. package/dist/agent.mjs +2 -0
  3. package/dist/bin/ptywright.mjs +6 -0
  4. package/dist/cli-DIUx2w6X.mjs +3587 -0
  5. package/dist/cli.mjs +2 -0
  6. package/{src/index.ts → dist/index.mjs} +7 -9
  7. package/dist/mcp.mjs +2 -0
  8. package/dist/pty-cassette.mjs +24 -0
  9. package/dist/pty_like-Cpkh_O9B.mjs +404 -0
  10. package/dist/runner-DzZlFrt1.mjs +1897 -0
  11. package/dist/runner-zApMYWZx.mjs +3257 -0
  12. package/dist/script.mjs +2 -0
  13. package/dist/server-VHuEWWj_.mjs +3068 -0
  14. package/dist/session.mjs +2 -0
  15. package/dist/terminal_session-DopC7Xg6.mjs +893 -0
  16. package/package.json +28 -21
  17. package/schemas/ptywright-agent-cassette.schema.json +57 -0
  18. package/schemas/ptywright-agent-check.schema.json +122 -0
  19. package/schemas/ptywright-agent-manifest.schema.json +107 -0
  20. package/schemas/ptywright-agent-promote.schema.json +146 -0
  21. package/schemas/ptywright-agent-replay-summary.schema.json +140 -0
  22. package/schemas/ptywright-agent-run.schema.json +126 -0
  23. package/schemas/ptywright-agent.schema.json +182 -0
  24. package/schemas/ptywright-pty-cassette.schema.json +86 -0
  25. package/schemas/ptywright-script-manifest.schema.json +75 -0
  26. package/schemas/ptywright-script-run-summary.schema.json +114 -0
  27. package/schemas/ptywright-script.schema.json +55 -3
  28. package/skills/ptywright-testing/SKILL.md +53 -33
  29. package/bin/ptywright +0 -4
  30. package/src/cli.ts +0 -414
  31. package/src/generator/doc_parser.ts +0 -341
  32. package/src/generator/generate.ts +0 -161
  33. package/src/generator/index.ts +0 -10
  34. package/src/generator/script_generator.ts +0 -209
  35. package/src/generator/step_extractor.ts +0 -397
  36. package/src/mcp/http_server.ts +0 -174
  37. package/src/mcp/script_recording.ts +0 -238
  38. package/src/mcp/server.ts +0 -1348
  39. package/src/pty/bun_pty_adapter.ts +0 -34
  40. package/src/pty/bun_terminal_adapter.ts +0 -149
  41. package/src/pty/pty_adapter.ts +0 -31
  42. package/src/script/dsl.ts +0 -188
  43. package/src/script/module.ts +0 -43
  44. package/src/script/path.ts +0 -151
  45. package/src/script/run.ts +0 -108
  46. package/src/script/run_all.ts +0 -229
  47. package/src/script/runner.ts +0 -983
  48. package/src/script/schema.ts +0 -237
  49. package/src/script/steps/assert_snapshot_equals.ts +0 -21
  50. package/src/script/steps/index.ts +0 -2
  51. package/src/script/suite_report.ts +0 -626
  52. package/src/session/session_manager.ts +0 -145
  53. package/src/session/terminal_session.ts +0 -473
  54. package/src/terminal/ansi.ts +0 -142
  55. package/src/terminal/keys.ts +0 -180
  56. package/src/terminal/mask.ts +0 -70
  57. package/src/terminal/mouse.ts +0 -75
  58. package/src/terminal/snapshot.ts +0 -196
  59. package/src/terminal/style.ts +0 -121
  60. package/src/terminal/view.ts +0 -49
  61. package/src/trace/asciicast.ts +0 -20
  62. package/src/trace/asciinema_player_assets.ts +0 -44
  63. package/src/trace/cast_to_txt.ts +0 -116
  64. package/src/trace/recorder.ts +0 -110
  65. package/src/trace/report.ts +0 -2092
  66. package/src/types.ts +0 -86
  67. package/src/util/hash.ts +0 -8
  68. package/src/util/sleep.ts +0 -5
@@ -1,145 +0,0 @@
1
- import { BunPtyAdapter } from "../pty/bun_pty_adapter";
2
- import { BunTerminalAdapter } from "../pty/bun_terminal_adapter";
3
- import type { PtyAdapter } from "../pty/pty_adapter";
4
- import { TerminalSession } from "./terminal_session";
5
-
6
- import type { LaunchSessionArgs, SessionId } from "../types";
7
-
8
- export type SessionManagerOptions = {
9
- ptyAdapter?: PtyAdapter;
10
- snapshotRingSize?: number;
11
- };
12
-
13
- export class SessionManager {
14
- private readonly ptyAdapter: PtyAdapter;
15
- private readonly snapshotRingSize: number;
16
- private readonly sessions = new Map<SessionId, TerminalSession>();
17
-
18
- constructor(options?: SessionManagerOptions) {
19
- this.ptyAdapter = options?.ptyAdapter ?? createDefaultPtyAdapter();
20
- this.snapshotRingSize = options?.snapshotRingSize ?? 50;
21
- }
22
-
23
- listSessionIds(): SessionId[] {
24
- return [...this.sessions.keys()];
25
- }
26
-
27
- listSessions(): TerminalSession[] {
28
- return [...this.sessions.values()];
29
- }
30
-
31
- getSession(sessionId: SessionId): TerminalSession | undefined {
32
- return this.sessions.get(sessionId);
33
- }
34
-
35
- launchSession(args: LaunchSessionArgs): TerminalSession {
36
- const sessionId = crypto.randomUUID();
37
- const cols = clampInt(args.cols ?? 80, 1, 500);
38
- const rows = clampInt(args.rows ?? 24, 1, 300);
39
- const cwd = args.cwd ?? process.cwd();
40
- const term = args.env?.TERM ?? args.name ?? "xterm-256color";
41
-
42
- const env = mergeEnv(
43
- {
44
- TERM: term,
45
- COLORTERM: "truecolor",
46
- },
47
- args.env,
48
- );
49
-
50
- const pty = this.ptyAdapter.spawn(args.command, args.args ?? [], {
51
- cols,
52
- rows,
53
- cwd,
54
- name: term,
55
- env,
56
- });
57
-
58
- const traceEnv = pickTraceEnv(env);
59
- const traceCommand = [args.command, ...(args.args ?? [])].join(" ").trim();
60
-
61
- const session = new TerminalSession({
62
- id: sessionId,
63
- pty,
64
- cols,
65
- rows,
66
- snapshotRingSize: this.snapshotRingSize,
67
- trace: {
68
- command: traceCommand,
69
- args: args.args ?? [],
70
- cwd,
71
- env: traceEnv,
72
- },
73
- });
74
-
75
- this.sessions.set(sessionId, session);
76
- return session;
77
- }
78
-
79
- closeSession(sessionId: SessionId): boolean {
80
- const session = this.sessions.get(sessionId);
81
- if (!session) return false;
82
- session.close();
83
- this.sessions.delete(sessionId);
84
- return true;
85
- }
86
-
87
- closeAll(): void {
88
- for (const [id, session] of this.sessions) {
89
- session.close();
90
- this.sessions.delete(id);
91
- }
92
- }
93
- }
94
-
95
- function createDefaultPtyAdapter(): PtyAdapter {
96
- const backend = (process.env.TUI_TEST_PTY_BACKEND ?? "auto").toLowerCase();
97
-
98
- if (backend === "bun-pty") return new BunPtyAdapter();
99
- if (backend === "bun-terminal") return new BunTerminalAdapter();
100
-
101
- // auto
102
- return process.platform === "win32" ? new BunPtyAdapter() : new BunTerminalAdapter();
103
- }
104
-
105
- function clampInt(value: number, min: number, max: number): number {
106
- if (!Number.isFinite(value)) return min;
107
- const int = Math.trunc(value);
108
- if (int < min) return min;
109
- if (int > max) return max;
110
- return int;
111
- }
112
-
113
- function mergeEnv(
114
- base: Record<string, string>,
115
- override?: Record<string, string>,
116
- ): Record<string, string> {
117
- const env: Record<string, string> = {};
118
-
119
- for (const [k, v] of Object.entries(process.env)) {
120
- if (typeof v === "string") {
121
- env[k] = v;
122
- }
123
- }
124
-
125
- for (const [k, v] of Object.entries(base)) {
126
- env[k] = v;
127
- }
128
-
129
- if (override) {
130
- for (const [k, v] of Object.entries(override)) {
131
- env[k] = v;
132
- }
133
- }
134
-
135
- return env;
136
- }
137
-
138
- function pickTraceEnv(env: Record<string, string>): Record<string, string> {
139
- const picked: Record<string, string> = {};
140
- for (const key of ["TERM", "COLORTERM", "LANG", "LC_ALL"]) {
141
- const value = env[key];
142
- if (value) picked[key] = value;
143
- }
144
- return picked;
145
- }
@@ -1,473 +0,0 @@
1
- import { Terminal } from "@xterm/headless";
2
-
3
- import type { Disposable, PtyProcess } from "../pty/pty_adapter";
4
- import { encodeKey } from "../terminal/keys";
5
- import { renderAnsiLines } from "../terminal/ansi";
6
- import { encodeSgrMouse } from "../terminal/mouse";
7
- import type { MouseEvent } from "../terminal/mouse";
8
- import type { AnsiRenderedLine } from "../terminal/ansi";
9
- import { snapshotGrid, snapshotLines } from "../terminal/snapshot";
10
- import type { TerminalSnapshotGrid } from "../terminal/snapshot";
11
- import type { SnapshotScope } from "../terminal/snapshot";
12
- import type { TerminalMeta } from "../terminal/view";
13
- import { applyTextMaskRules } from "../terminal/mask";
14
- import type { TextMaskRule } from "../terminal/mask";
15
- import { fnv1a32 } from "../util/hash";
16
- import { sleep } from "../util/sleep";
17
- import { TraceRecorder } from "../trace/recorder";
18
- import type { TraceSnapshot } from "../trace/recorder";
19
-
20
- export type TerminalSessionOptions = {
21
- id: string;
22
- pty: PtyProcess;
23
- cols: number;
24
- rows: number;
25
- snapshotRingSize: number;
26
- trace?: {
27
- command?: string;
28
- args?: string[];
29
- cwd?: string;
30
- env?: Record<string, string>;
31
- title?: string;
32
- };
33
- };
34
-
35
- export type SnapshotFrame = {
36
- atMs: number;
37
- hash: string;
38
- text: string;
39
- };
40
-
41
- export type TerminalSessionCloseReason =
42
- | { type: "closed_by_user" }
43
- | { type: "process_exit"; exitCode: number; signal?: number | string };
44
-
45
- const ESC = "\x1b";
46
-
47
- export class TerminalSession {
48
- readonly id: string;
49
-
50
- private readonly pty: PtyProcess;
51
- private readonly terminal: Terminal;
52
- private readonly snapshotRingSize: number;
53
- private readonly trace: TraceRecorder;
54
-
55
- private writeChain: Promise<void> = Promise.resolve();
56
- private readonly disposables: Disposable[] = [];
57
- private readonly rawOutputRing: string[] = [];
58
- private readonly snapshotRing: SnapshotFrame[] = [];
59
-
60
- private closed: TerminalSessionCloseReason | null = null;
61
-
62
- constructor(options: TerminalSessionOptions) {
63
- this.id = options.id;
64
- this.pty = options.pty;
65
- this.snapshotRingSize = options.snapshotRingSize;
66
-
67
- this.terminal = new Terminal({
68
- cols: options.cols,
69
- rows: options.rows,
70
- allowProposedApi: true,
71
- scrollback: 2000,
72
- convertEol: true,
73
- });
74
-
75
- this.trace = new TraceRecorder({
76
- version: 2,
77
- width: options.cols,
78
- height: options.rows,
79
- timestamp: Math.floor(Date.now() / 1000),
80
- env: options.trace?.env,
81
- title: options.trace?.title ?? options.id,
82
- command: options.trace?.command,
83
- term: options.trace?.env?.TERM,
84
- });
85
-
86
- this.disposables.push(
87
- this.terminal.parser.registerCsiHandler({ final: "n" }, (params) =>
88
- this.handleCsiDsr(params),
89
- ),
90
- );
91
-
92
- this.disposables.push(
93
- this.terminal.parser.registerCsiHandler({ final: "c" }, (params) => this.handleCsiDa(params)),
94
- );
95
-
96
- this.disposables.push(
97
- this.pty.onData((data) => {
98
- this.appendRawOutput(data);
99
- this.trace.recordOutput(data);
100
- this.enqueueWrite(data);
101
- }),
102
- );
103
-
104
- this.disposables.push(
105
- this.pty.onExit((event) => {
106
- this.closed = { type: "process_exit", exitCode: event.exitCode, signal: event.signal };
107
- }),
108
- );
109
- }
110
-
111
- get cols(): number {
112
- return this.terminal.cols;
113
- }
114
-
115
- get rows(): number {
116
- return this.terminal.rows;
117
- }
118
-
119
- isClosed(): boolean {
120
- return this.closed !== null;
121
- }
122
-
123
- getCloseReason(): TerminalSessionCloseReason | null {
124
- return this.closed;
125
- }
126
-
127
- resize(cols: number, rows: number): void {
128
- this.trace.recordResize(cols, rows);
129
- this.pty.resize(cols, rows);
130
- this.terminal.resize(cols, rows);
131
- }
132
-
133
- sendText(text: string, options?: { enter?: boolean }): void {
134
- const enter = options?.enter ?? false;
135
- const payload = enter ? `${text}\r` : text;
136
- this.trace.recordInput(payload);
137
- this.pty.write(payload);
138
- }
139
-
140
- pressKey(key: string): void {
141
- const encoded = encodeKey(key);
142
- this.trace.recordInput(encoded);
143
- this.pty.write(encoded);
144
- }
145
-
146
- sendMouse(event: MouseEvent): void {
147
- const encoded = encodeSgrMouse(event);
148
- this.trace.recordInput(encoded);
149
- this.pty.write(encoded);
150
- }
151
-
152
- async flush(): Promise<void> {
153
- await this.writeChain;
154
- }
155
-
156
- getMeta(): TerminalMeta {
157
- const buffer = this.terminal.buffer.active;
158
- return {
159
- cols: this.terminal.cols,
160
- rows: this.terminal.rows,
161
- bufferType: buffer.type,
162
- viewportY: buffer.viewportY,
163
- baseY: buffer.baseY,
164
- length: buffer.length,
165
- cursorX: buffer.cursorX,
166
- cursorY: buffer.cursorY,
167
- };
168
- }
169
-
170
- async snapshotText(options?: SnapshotTextOptions): Promise<{
171
- text: string;
172
- hash: string;
173
- }> {
174
- await this.flush();
175
- if (options?.maxLines !== undefined && options.tailLines !== undefined) {
176
- throw new Error("snapshotText: maxLines and tailLines are mutually exclusive");
177
- }
178
-
179
- let lines = snapshotLines(this.terminal, {
180
- scope: options?.scope,
181
- trimRight: options?.trimRight,
182
- });
183
-
184
- const trimBottom = options?.trimBottom ?? true;
185
- if (trimBottom) {
186
- lines = trimBottomEmptyLines(lines);
187
- }
188
-
189
- if (options?.maxLines !== undefined) {
190
- const max = Math.max(0, Math.trunc(options.maxLines));
191
- lines = lines.slice(0, max);
192
- }
193
-
194
- if (options?.tailLines !== undefined) {
195
- const tail = Math.max(0, Math.trunc(options.tailLines));
196
- lines = lines.slice(Math.max(0, lines.length - tail));
197
- }
198
-
199
- lines = applyTextMaskRules(lines, options?.mask);
200
-
201
- const text = lines.join("\n");
202
- const hash = fnv1a32(text);
203
- if (options?.captureFrame ?? true) {
204
- this.captureFrame(text, hash);
205
- }
206
- return { text, hash };
207
- }
208
-
209
- async snapshotAnsi(options?: SnapshotAnsiOptions): Promise<{
210
- ansi: string;
211
- plain: string;
212
- hash: string;
213
- lines: AnsiRenderedLine[];
214
- }> {
215
- await this.flush();
216
- if (options?.maxLines !== undefined && options.tailLines !== undefined) {
217
- throw new Error("snapshotAnsi: maxLines and tailLines are mutually exclusive");
218
- }
219
-
220
- let lines = renderAnsiLines(this.terminal, {
221
- scope: options?.scope,
222
- trimRight: options?.trimRight,
223
- });
224
-
225
- const trimBottom = options?.trimBottom ?? true;
226
- if (trimBottom) {
227
- lines = trimBottomEmptyAnsiLines(lines);
228
- }
229
-
230
- if (options?.maxLines !== undefined) {
231
- const max = Math.max(0, Math.trunc(options.maxLines));
232
- lines = lines.slice(0, max);
233
- }
234
-
235
- if (options?.tailLines !== undefined) {
236
- const tail = Math.max(0, Math.trunc(options.tailLines));
237
- lines = lines.slice(Math.max(0, lines.length - tail));
238
- }
239
-
240
- if (options?.mask && options.mask.length > 0) {
241
- const maskedPlain = applyTextMaskRules(
242
- lines.map((line) => line.plain),
243
- options.mask,
244
- );
245
- const maskedAnsi = applyTextMaskRules(
246
- lines.map((line) => line.ansi),
247
- options.mask,
248
- );
249
-
250
- lines = lines.map((line, idx) => ({
251
- ...line,
252
- plain: maskedPlain[idx] ?? "",
253
- ansi: maskedAnsi[idx] ?? "",
254
- }));
255
- }
256
-
257
- const ansi = lines.map((l) => l.ansi).join("\n");
258
- const plain = lines.map((l) => l.plain).join("\n");
259
- const hash = fnv1a32(ansi);
260
- return { ansi, plain, hash, lines };
261
- }
262
-
263
- async snapshotGrid(options?: {
264
- trimRight?: boolean;
265
- includeStyles?: boolean;
266
- captureFrame?: boolean;
267
- }): Promise<{
268
- grid: TerminalSnapshotGrid;
269
- hash: string;
270
- }> {
271
- await this.flush();
272
-
273
- const grid = snapshotGrid(this.terminal, {
274
- trimRight: options?.trimRight,
275
- includeStyles: options?.includeStyles,
276
- });
277
-
278
- const hash = fnv1a32(JSON.stringify(grid));
279
- if (options?.captureFrame ?? true) {
280
- this.captureFrame(grid.lines.join("\n"), hash);
281
- }
282
-
283
- return { grid, hash };
284
- }
285
-
286
- async snapshotCast(options?: { tailEvents?: number }): Promise<TraceSnapshot> {
287
- await this.flush();
288
- return this.trace.snapshot({ tailEvents: options?.tailEvents });
289
- }
290
-
291
- mark(label?: string): void {
292
- this.trace.mark(label);
293
- }
294
-
295
- getSnapshotFrames(): SnapshotFrame[] {
296
- return [...this.snapshotRing];
297
- }
298
-
299
- getRawOutputChunks(): string[] {
300
- return [...this.rawOutputRing];
301
- }
302
-
303
- async waitForText(args: {
304
- scope?: SnapshotScope;
305
- text?: string;
306
- regex?: RegExp;
307
- timeoutMs: number;
308
- intervalMs: number;
309
- }): Promise<{ found: boolean; text: string; hash: string }> {
310
- const startedAt = Date.now();
311
- let closedSince: number | null = null;
312
- while (Date.now() - startedAt <= args.timeoutMs) {
313
- const snapshot = await this.snapshotText({ captureFrame: true, scope: args.scope });
314
- if (args.text && snapshot.text.includes(args.text)) {
315
- return { found: true, ...snapshot };
316
- }
317
- if (args.regex && args.regex.test(snapshot.text)) {
318
- return { found: true, ...snapshot };
319
- }
320
-
321
- if (this.isClosed()) {
322
- closedSince ??= Date.now();
323
- const drainMs = Math.max(500, args.intervalMs * 4);
324
- if (Date.now() - closedSince >= drainMs) {
325
- break;
326
- }
327
- }
328
-
329
- await sleep(args.intervalMs);
330
- }
331
-
332
- const snapshot = await this.snapshotText({ captureFrame: true, scope: args.scope });
333
- return { found: false, ...snapshot };
334
- }
335
-
336
- async waitForStableScreen(args: {
337
- quietMs: number;
338
- timeoutMs: number;
339
- intervalMs: number;
340
- }): Promise<{ stable: boolean; text: string; hash: string }> {
341
- const startedAt = Date.now();
342
- let stableSince: number | null = null;
343
- let lastHash: string | null = null;
344
-
345
- while (Date.now() - startedAt <= args.timeoutMs) {
346
- const snapshot = await this.snapshotText({ captureFrame: true });
347
- if (snapshot.hash === lastHash) {
348
- stableSince ??= Date.now();
349
- } else {
350
- stableSince = null;
351
- lastHash = snapshot.hash;
352
- }
353
-
354
- if (stableSince !== null && Date.now() - stableSince >= args.quietMs) {
355
- return { stable: true, ...snapshot };
356
- }
357
-
358
- await sleep(args.intervalMs);
359
- }
360
-
361
- const snapshot = await this.snapshotText({ captureFrame: true });
362
- return { stable: false, ...snapshot };
363
- }
364
-
365
- close(): void {
366
- if (this.closed === null) {
367
- this.closed = { type: "closed_by_user" };
368
- }
369
- this.pty.kill();
370
- for (const d of this.disposables) d.dispose();
371
- this.terminal.dispose();
372
- }
373
-
374
- private enqueueWrite(data: string): void {
375
- this.writeChain = this.writeChain.then(
376
- () =>
377
- new Promise<void>((resolve) => {
378
- this.terminal.write(data, resolve);
379
- }),
380
- );
381
- }
382
-
383
- private appendRawOutput(data: string): void {
384
- this.rawOutputRing.push(data);
385
- if (this.rawOutputRing.length > 2000) {
386
- this.rawOutputRing.splice(0, this.rawOutputRing.length - 2000);
387
- }
388
- }
389
-
390
- private captureFrame(text: string, hash: string): void {
391
- this.snapshotRing.push({ atMs: Date.now(), hash, text });
392
- if (this.snapshotRing.length > this.snapshotRingSize) {
393
- this.snapshotRing.splice(0, this.snapshotRing.length - this.snapshotRingSize);
394
- }
395
- }
396
-
397
- private writePtySafely(data: string): void {
398
- if (this.isClosed()) return;
399
- try {
400
- this.pty.write(data);
401
- } catch {
402
- // Ignore: PTY may have closed between trigger and response.
403
- }
404
- }
405
-
406
- private handleCsiDsr(params: (number | number[])[]): boolean {
407
- if (params.length !== 1) return false;
408
-
409
- const raw = params[0];
410
- const value = Array.isArray(raw) ? raw[0] : raw;
411
- if (value === 5) {
412
- this.writePtySafely(`${ESC}[0n`);
413
- return true;
414
- }
415
-
416
- if (value !== 6) return false;
417
-
418
- const meta = this.getMeta();
419
- const row = meta.baseY + meta.cursorY - meta.viewportY + 1;
420
- const col = meta.cursorX + 1;
421
- this.writePtySafely(`${ESC}[${row};${col}R`);
422
- return true;
423
- }
424
-
425
- private handleCsiDa(params: (number | number[])[]): boolean {
426
- if (params.length > 1) return false;
427
-
428
- const raw = params[0];
429
- const value = raw === undefined ? 0 : Array.isArray(raw) ? raw[0] : raw;
430
- if (value !== 0) return false;
431
-
432
- this.writePtySafely(`${ESC}[?1;2c`);
433
- return true;
434
- }
435
- }
436
-
437
- type SnapshotTextOptions = {
438
- scope?: SnapshotScope;
439
- trimRight?: boolean;
440
- trimBottom?: boolean;
441
- maxLines?: number;
442
- tailLines?: number;
443
- captureFrame?: boolean;
444
- mask?: TextMaskRule[];
445
- };
446
-
447
- function trimBottomEmptyLines(lines: string[]): string[] {
448
- let end = lines.length;
449
- while (end > 0 && lines[end - 1] === "") {
450
- end -= 1;
451
- }
452
- return end === lines.length ? lines : lines.slice(0, end);
453
- }
454
-
455
- type SnapshotAnsiOptions = {
456
- scope?: SnapshotScope;
457
- trimRight?: boolean;
458
- trimBottom?: boolean;
459
- maxLines?: number;
460
- tailLines?: number;
461
- mask?: TextMaskRule[];
462
- };
463
-
464
- function trimBottomEmptyAnsiLines(lines: AnsiRenderedLine[]): AnsiRenderedLine[] {
465
- let end = lines.length;
466
- while (end > 0) {
467
- const line = lines[end - 1];
468
- const isBlank = !line?.hasStyle && (line?.plain ?? "").trim() === "";
469
- if (!isBlank) break;
470
- end -= 1;
471
- }
472
- return end === lines.length ? lines : lines.slice(0, end);
473
- }