Skip to content

Cell - TUI Component ​

用 pi-tui 给 Cell 命令行应用做终端界面:差分渲染、无闪烁,内置编辑器、Markdown、列表、加载动画等组件。

  • TuiFactory:创建主屏(保留滚动历史)或全屏的 TUI;应用停止时自动停止所有 TUI、恢复终端;
  • AgentChat:终端里的 agent 对话。目标可以是 @celljs/ai-agent 的 Agent,也可以是 @celljs/ai-coding-agent 的会话:回答以 Markdown 流式显示,工具调用和错误单独成行;Enter 发送,Escape 中止当前运行,Ctrl+C 退出。

pi-tui 用动态 import() 加载,需要 Node.js >= 22.19 或 Bun。

使用 ​

typescript
import { AgentFactory } from '@celljs/ai-agent';
import { Autowired, Component } from '@celljs/core';
import { AgentChat } from '@celljs/tui';

@Component()
export class ChatCommand {
    @Autowired(AgentFactory)
    protected readonly agentFactory: AgentFactory;

    @Autowired(AgentChat)
    protected readonly agentChat: AgentChat;

    async run(): Promise<void> {
        const agent = await this.agentFactory.create();
        await this.agentChat.run(agent, { title: 'ACME assistant' });
    }
}

自定义界面时用 TuiFactory.create({ screen: 'alt' }) 拿到 TUI,再用 pi-tui 的组件(PiTuiUtil.load() 返回 pi-tui 模块)。TuiThemeUtil 提供跟随终端配色的默认主题。

完整的命令行程序(cli preset、启动后进入对话、Ctrl+C 退出进程)见 examples/tui-app。

测试 ​

@celljs/tui/lib/node/test 的 FakeTerminal 记录输出(text 去掉了转义序列),type() 模拟逐键输入,传给 create({ terminal }) 或 run(target, { terminal })。