@celljs/tui 0.0.0-stage → 4.0.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 (144) hide show
  1. package/README.md +37 -2
  2. package/esm/node/application-lifecycle.d.ts +6 -0
  3. package/esm/node/application-lifecycle.js +25 -0
  4. package/esm/node/application-lifecycle.js.map +1 -0
  5. package/esm/node/chat/agent-chat-view.d.ts +26 -0
  6. package/esm/node/chat/agent-chat-view.js +61 -0
  7. package/esm/node/chat/agent-chat-view.js.map +1 -0
  8. package/esm/node/chat/agent-chat.d.ts +5 -0
  9. package/esm/node/chat/agent-chat.js +75 -0
  10. package/esm/node/chat/agent-chat.js.map +1 -0
  11. package/esm/node/chat/index.d.ts +2 -0
  12. package/esm/node/chat/index.js +3 -0
  13. package/esm/node/chat/index.js.map +1 -0
  14. package/esm/node/error/errors.d.ts +3 -0
  15. package/esm/node/error/errors.js +4 -0
  16. package/esm/node/error/errors.js.map +1 -0
  17. package/esm/node/error/index.d.ts +1 -0
  18. package/esm/node/error/index.js +2 -0
  19. package/esm/node/error/index.js.map +1 -0
  20. package/esm/node/index.d.ts +6 -0
  21. package/esm/node/index.js +7 -0
  22. package/esm/node/index.js.map +1 -0
  23. package/esm/node/module.d.ts +3 -0
  24. package/esm/node/module.js +4 -0
  25. package/esm/node/module.js.map +1 -0
  26. package/esm/node/pi/index.d.ts +2 -0
  27. package/esm/node/pi/index.js +3 -0
  28. package/esm/node/pi/index.js.map +1 -0
  29. package/esm/node/pi/pi-tui-util.d.ts +6 -0
  30. package/esm/node/pi/pi-tui-util.js +15 -0
  31. package/esm/node/pi/pi-tui-util.js.map +1 -0
  32. package/esm/node/pi/pi-types.d.ts +5 -0
  33. package/esm/node/pi/pi-types.js +2 -0
  34. package/esm/node/pi/pi-types.js.map +1 -0
  35. package/esm/node/test/fake-terminal.d.ts +28 -0
  36. package/esm/node/test/fake-terminal.js +45 -0
  37. package/esm/node/test/fake-terminal.js.map +1 -0
  38. package/esm/node/test/index.d.ts +1 -0
  39. package/esm/node/test/index.js +2 -0
  40. package/esm/node/test/index.js.map +1 -0
  41. package/esm/node/tui/index.d.ts +2 -0
  42. package/esm/node/tui/index.js +3 -0
  43. package/esm/node/tui/index.js.map +1 -0
  44. package/esm/node/tui/tui-factory.d.ts +7 -0
  45. package/esm/node/tui/tui-factory.js +37 -0
  46. package/esm/node/tui/tui-factory.js.map +1 -0
  47. package/esm/node/tui/tui-theme-util.d.ts +15 -0
  48. package/esm/node/tui/tui-theme-util.js +41 -0
  49. package/esm/node/tui/tui-theme-util.js.map +1 -0
  50. package/esm/node/tui-protocol.d.ts +32 -0
  51. package/esm/node/tui-protocol.js +3 -0
  52. package/esm/node/tui-protocol.js.map +1 -0
  53. package/esm/package.json +3 -0
  54. package/lib/node/application-lifecycle.d.ts +7 -0
  55. package/lib/node/application-lifecycle.d.ts.map +1 -0
  56. package/lib/node/application-lifecycle.js +28 -0
  57. package/lib/node/application-lifecycle.js.map +1 -0
  58. package/lib/node/chat/agent-chat-view.d.ts +27 -0
  59. package/lib/node/chat/agent-chat-view.d.ts.map +1 -0
  60. package/lib/node/chat/agent-chat-view.js +65 -0
  61. package/lib/node/chat/agent-chat-view.js.map +1 -0
  62. package/lib/node/chat/agent-chat.d.ts +6 -0
  63. package/lib/node/chat/agent-chat.d.ts.map +1 -0
  64. package/lib/node/chat/agent-chat.js +78 -0
  65. package/lib/node/chat/agent-chat.js.map +1 -0
  66. package/lib/node/chat/agent-chat.spec.d.ts +2 -0
  67. package/lib/node/chat/agent-chat.spec.d.ts.map +1 -0
  68. package/lib/node/chat/agent-chat.spec.js +83 -0
  69. package/lib/node/chat/agent-chat.spec.js.map +1 -0
  70. package/lib/node/chat/index.d.ts +3 -0
  71. package/lib/node/chat/index.d.ts.map +1 -0
  72. package/lib/node/chat/index.js +19 -0
  73. package/lib/node/chat/index.js.map +1 -0
  74. package/lib/node/error/errors.d.ts +4 -0
  75. package/lib/node/error/errors.d.ts.map +1 -0
  76. package/lib/node/error/errors.js +8 -0
  77. package/lib/node/error/errors.js.map +1 -0
  78. package/lib/node/error/index.d.ts +2 -0
  79. package/lib/node/error/index.d.ts.map +1 -0
  80. package/lib/node/error/index.js +18 -0
  81. package/lib/node/error/index.js.map +1 -0
  82. package/lib/node/index.d.ts +7 -0
  83. package/lib/node/index.d.ts.map +1 -0
  84. package/lib/node/index.js +23 -0
  85. package/lib/node/index.js.map +1 -0
  86. package/lib/node/module.d.ts +4 -0
  87. package/lib/node/module.d.ts.map +1 -0
  88. package/lib/node/module.js +6 -0
  89. package/lib/node/module.js.map +1 -0
  90. package/lib/node/pi/index.d.ts +3 -0
  91. package/lib/node/pi/index.d.ts.map +1 -0
  92. package/lib/node/pi/index.js +19 -0
  93. package/lib/node/pi/index.js.map +1 -0
  94. package/lib/node/pi/pi-tui-util.d.ts +7 -0
  95. package/lib/node/pi/pi-tui-util.d.ts.map +1 -0
  96. package/lib/node/pi/pi-tui-util.js +18 -0
  97. package/lib/node/pi/pi-tui-util.js.map +1 -0
  98. package/lib/node/pi/pi-types.d.ts +6 -0
  99. package/lib/node/pi/pi-types.d.ts.map +1 -0
  100. package/lib/node/pi/pi-types.js +3 -0
  101. package/lib/node/pi/pi-types.js.map +1 -0
  102. package/lib/node/test/fake-terminal.d.ts +29 -0
  103. package/lib/node/test/fake-terminal.d.ts.map +1 -0
  104. package/lib/node/test/fake-terminal.js +49 -0
  105. package/lib/node/test/fake-terminal.js.map +1 -0
  106. package/lib/node/test/index.d.ts +2 -0
  107. package/lib/node/test/index.d.ts.map +1 -0
  108. package/lib/node/test/index.js +18 -0
  109. package/lib/node/test/index.js.map +1 -0
  110. package/lib/node/tui/index.d.ts +3 -0
  111. package/lib/node/tui/index.d.ts.map +1 -0
  112. package/lib/node/tui/index.js +19 -0
  113. package/lib/node/tui/index.js.map +1 -0
  114. package/lib/node/tui/tui-factory.d.ts +8 -0
  115. package/lib/node/tui/tui-factory.d.ts.map +1 -0
  116. package/lib/node/tui/tui-factory.js +40 -0
  117. package/lib/node/tui/tui-factory.js.map +1 -0
  118. package/lib/node/tui/tui-theme-util.d.ts +16 -0
  119. package/lib/node/tui/tui-theme-util.d.ts.map +1 -0
  120. package/lib/node/tui/tui-theme-util.js +44 -0
  121. package/lib/node/tui/tui-theme-util.js.map +1 -0
  122. package/lib/node/tui-protocol.d.ts +33 -0
  123. package/lib/node/tui-protocol.d.ts.map +1 -0
  124. package/lib/node/tui-protocol.js +6 -0
  125. package/lib/node/tui-protocol.js.map +1 -0
  126. package/package.json +89 -4
  127. package/src/node/application-lifecycle.ts +12 -0
  128. package/src/node/chat/agent-chat-view.ts +73 -0
  129. package/src/node/chat/agent-chat.spec.ts +86 -0
  130. package/src/node/chat/agent-chat.ts +61 -0
  131. package/src/node/chat/index.ts +2 -0
  132. package/src/node/error/errors.ts +3 -0
  133. package/src/node/error/index.ts +1 -0
  134. package/src/node/index.ts +6 -0
  135. package/src/node/module.ts +4 -0
  136. package/src/node/pi/index.ts +2 -0
  137. package/src/node/pi/pi-tui-util.ts +17 -0
  138. package/src/node/pi/pi-types.ts +7 -0
  139. package/src/node/test/fake-terminal.ts +52 -0
  140. package/src/node/test/index.ts +1 -0
  141. package/src/node/tui/index.ts +2 -0
  142. package/src/node/tui/tui-factory.ts +27 -0
  143. package/src/node/tui/tui-theme-util.ts +42 -0
  144. package/src/node/tui-protocol.ts +38 -0
@@ -0,0 +1,61 @@
1
+ import { Autowired, Component } from '@celljs/core';
2
+ import { PiTuiUtil } from '../pi';
3
+ import { TuiThemeUtil } from '../tui/tui-theme-util';
4
+ import { AgentChat, type AgentChatOptions, type AgentChatTarget, TuiFactory } from '../tui-protocol';
5
+ import { AgentChatView } from './agent-chat-view';
6
+
7
+ @Component(AgentChat)
8
+ export class DefaultAgentChat implements AgentChat {
9
+ @Autowired(TuiFactory)
10
+ protected readonly tuiFactory: TuiFactory;
11
+
12
+ async run(target: AgentChatTarget, options: AgentChatOptions = {}): Promise<void> {
13
+ const pi = await PiTuiUtil.load();
14
+ const tui = await this.tuiFactory.create(options);
15
+ const view = new AgentChatView(pi, options.title);
16
+ const editor = new pi.Editor(tui, TuiThemeUtil.editorTheme());
17
+ tui.addChild(view);
18
+ tui.addChild(editor);
19
+ tui.setFocus(editor);
20
+ const unsubscribe = target.subscribe(event => {
21
+ if (view.handle(event)) {
22
+ tui.requestRender();
23
+ }
24
+ });
25
+ let running = false;
26
+ editor.onSubmit = text => {
27
+ const prompt = text.trim();
28
+ if (!prompt || running) {
29
+ return;
30
+ }
31
+ running = true;
32
+ editor.disableSubmit = true;
33
+ view.addPrompt(prompt);
34
+ tui.requestRender();
35
+ target
36
+ .prompt(prompt)
37
+ .catch(error => view.addError(error instanceof Error ? error.message : String(error)))
38
+ .finally(() => {
39
+ running = false;
40
+ editor.disableSubmit = false;
41
+ tui.requestRender();
42
+ });
43
+ };
44
+ return new Promise<void>(resolve => {
45
+ tui.addInputListener(data => {
46
+ if (pi.matchesKey(data, 'ctrl+c')) {
47
+ unsubscribe();
48
+ tui.stop();
49
+ resolve();
50
+ return { consume: true };
51
+ }
52
+ if (pi.matchesKey(data, 'escape') && running) {
53
+ target.abort?.();
54
+ return { consume: true };
55
+ }
56
+ return undefined;
57
+ });
58
+ tui.start();
59
+ });
60
+ }
61
+ }
@@ -0,0 +1,2 @@
1
+ export * from './agent-chat';
2
+ export * from './agent-chat-view';
@@ -0,0 +1,3 @@
1
+ import { CustomError } from '@celljs/core';
2
+
3
+ export class TuiError extends CustomError {}
@@ -0,0 +1 @@
1
+ export * from './errors';
@@ -0,0 +1,6 @@
1
+ export * from './application-lifecycle';
2
+ export * from './chat';
3
+ export * from './error';
4
+ export * from './pi';
5
+ export * from './tui';
6
+ export * from './tui-protocol';
@@ -0,0 +1,4 @@
1
+ import { autoBind } from '@celljs/core';
2
+ import './index';
3
+
4
+ export default autoBind();
@@ -0,0 +1,2 @@
1
+ export * from './pi-tui-util';
2
+ export * from './pi-types';
@@ -0,0 +1,17 @@
1
+ import { TuiError } from '../error';
2
+
3
+ /** The module namespace of `@earendil-works/pi-tui`. */
4
+ export type PiTui = typeof import('@earendil-works/pi-tui', { with: { 'resolution-mode': 'import' }});
5
+
6
+ export namespace PiTuiUtil {
7
+ let tui: Promise<PiTui> | undefined;
8
+
9
+ /** Loads pi-tui once, with a dynamic `import()`: it is an ES module package. */
10
+ export function load(): Promise<PiTui> {
11
+ tui ??= import('@earendil-works/pi-tui').catch(cause => {
12
+ tui = undefined;
13
+ throw new TuiError('Cannot load @earendil-works/pi-tui. Install it next to @celljs/tui; it needs Node.js >= 22.19.0 or Bun.', { cause });
14
+ });
15
+ return tui;
16
+ }
17
+ }
@@ -0,0 +1,7 @@
1
+ // pi-tui is an ES module package: the CommonJS build of this package can only name its types through
2
+ // import types with `resolution-mode`.
3
+ export type TUI = import('@earendil-works/pi-tui', { with: { 'resolution-mode': 'import' }}).TUI;
4
+ export type Terminal = import('@earendil-works/pi-tui', { with: { 'resolution-mode': 'import' }}).Terminal;
5
+ export type Component = import('@earendil-works/pi-tui', { with: { 'resolution-mode': 'import' }}).Component;
6
+ export type EditorTheme = import('@earendil-works/pi-tui', { with: { 'resolution-mode': 'import' }}).EditorTheme;
7
+ export type MarkdownTheme = import('@earendil-works/pi-tui', { with: { 'resolution-mode': 'import' }}).MarkdownTheme;
@@ -0,0 +1,52 @@
1
+ import type { Terminal } from '../pi';
2
+
3
+ /** A terminal for tests: keeps what the TUI writes and lets the test type. */
4
+ export class FakeTerminal {
5
+ output = '';
6
+ stopped = false;
7
+ columns = 80;
8
+ rows = 24;
9
+ readonly kittyProtocolActive = false;
10
+ protected onInput?: (data: string) => void;
11
+
12
+ get terminal(): Terminal {
13
+ return this as unknown as Terminal;
14
+ }
15
+
16
+ /** What was written, without terminal escape sequences. */
17
+ get text(): string {
18
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: terminal escape sequences
19
+ return this.output.replace(/\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b\][^\x07]*\x07|\x1b_[^\x1b]*\x1b\\/g, '');
20
+ }
21
+
22
+ start(onInput: (data: string) => void): void {
23
+ this.onInput = onInput;
24
+ }
25
+
26
+ stop(): void {
27
+ this.stopped = true;
28
+ }
29
+
30
+ /** Sends key presses one by one, as a person types. */
31
+ type(text: string): void {
32
+ for (const char of text) {
33
+ this.onInput?.(char);
34
+ }
35
+ }
36
+
37
+ async drainInput(): Promise<void> {}
38
+
39
+ write(data: string): void {
40
+ this.output += data;
41
+ }
42
+
43
+ moveBy(): void {}
44
+ hideCursor(): void {}
45
+ showCursor(): void {}
46
+ clearLine(): void {}
47
+ clearFromCursor(): void {}
48
+ clearScreen(): void {}
49
+ setTitle(): void {}
50
+ setProgress(): void {}
51
+ setProgramStatus(): void {}
52
+ }
@@ -0,0 +1 @@
1
+ export * from './fake-terminal';
@@ -0,0 +1,2 @@
1
+ export * from './tui-factory';
2
+ export * from './tui-theme-util';
@@ -0,0 +1,27 @@
1
+ import { Component } from '@celljs/core';
2
+ import { PiTuiUtil, type TUI } from '../pi';
3
+ import { TuiFactory, type TuiOptions } from '../tui-protocol';
4
+
5
+ @Component(TuiFactory)
6
+ export class DefaultTuiFactory implements TuiFactory {
7
+ protected readonly tuis = new Set<TUI>();
8
+
9
+ async create(options: TuiOptions = {}): Promise<TUI> {
10
+ const pi = await PiTuiUtil.load();
11
+ const terminal = options.terminal ?? new pi.ProcessTerminal();
12
+ const tui: TUI = options.screen === 'alt' ? new pi.TuiAltScreen(terminal) : new pi.TuiMainScreen(terminal);
13
+ this.tuis.add(tui);
14
+ const stop = tui.stop.bind(tui);
15
+ tui.stop = (...args) => {
16
+ this.tuis.delete(tui);
17
+ stop(...args);
18
+ };
19
+ return tui;
20
+ }
21
+
22
+ stopAll(): void {
23
+ for (const tui of [...this.tuis]) {
24
+ tui.stop();
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,42 @@
1
+ import type { EditorTheme, MarkdownTheme } from '../pi';
2
+
3
+ const style = (open: number, close: number) => (text: string) => `\x1b[${open}m${text}\x1b[${close}m`;
4
+
5
+ /** Default themes in the 16 ANSI colors, so they follow the palette of the terminal. */
6
+ export namespace TuiThemeUtil {
7
+ export const bold = style(1, 22);
8
+ export const dim = style(2, 22);
9
+ export const italic = style(3, 23);
10
+ export const underline = style(4, 24);
11
+ export const strikethrough = style(9, 29);
12
+ export const cyan = style(36, 39);
13
+ export const green = style(32, 39);
14
+ export const yellow = style(33, 39);
15
+ export const red = style(31, 39);
16
+
17
+ export function editorTheme(): EditorTheme {
18
+ return {
19
+ borderColor: dim,
20
+ selectList: { selectedPrefix: cyan, selectedText: bold, description: dim, scrollInfo: dim, noMatch: dim },
21
+ };
22
+ }
23
+
24
+ export function markdownTheme(): MarkdownTheme {
25
+ return {
26
+ heading: text => bold(cyan(text)),
27
+ link: underline,
28
+ linkUrl: dim,
29
+ code: yellow,
30
+ codeBlock: text => text,
31
+ codeBlockBorder: dim,
32
+ quote: italic,
33
+ quoteBorder: dim,
34
+ hr: dim,
35
+ listBullet: cyan,
36
+ bold,
37
+ italic,
38
+ strikethrough,
39
+ underline,
40
+ };
41
+ }
42
+ }
@@ -0,0 +1,38 @@
1
+ import type { Terminal, TUI } from './pi';
2
+
3
+ export const TuiFactory = Symbol('TuiFactory');
4
+ export const AgentChat = Symbol('AgentChat');
5
+
6
+ export interface TuiOptions {
7
+ /** `main` keeps the terminal scrollback; `alt` draws a full-screen viewport. Default: main. */
8
+ screen?: 'main' | 'alt';
9
+ /** Default: the terminal of the process (stdin/stdout). */
10
+ terminal?: Terminal;
11
+ }
12
+
13
+ /** Creates pi-tui `TUI`s and stops them, restoring the terminal, when the application stops. */
14
+ export interface TuiFactory {
15
+ create(options?: TuiOptions): Promise<TUI>;
16
+ stopAll(): void;
17
+ }
18
+
19
+ /**
20
+ * What {@link AgentChat} talks to: a pi-agent-core `Agent` (@celljs/ai-agent) or a pi coding agent
21
+ * `AgentSession` (@celljs/ai-coding-agent).
22
+ */
23
+ export interface AgentChatTarget {
24
+ subscribe(listener: (event: any) => unknown): () => void;
25
+ prompt(text: string): Promise<unknown>;
26
+ abort?(): unknown;
27
+ }
28
+
29
+ export interface AgentChatOptions extends TuiOptions {
30
+ /** Shown above the conversation. */
31
+ title?: string;
32
+ }
33
+
34
+ /** An interactive chat with an agent in the terminal: Enter sends, Escape aborts the run, Ctrl+C quits. */
35
+ export interface AgentChat {
36
+ /** Resolves when the user quits. */
37
+ run(target: AgentChatTarget, options?: AgentChatOptions): Promise<void>;
38
+ }