一个 struct,四端可用。
在结构体上挂一个 #[derive(App)],框架就派生出 CLI、TUI、Web 与 MCP 四个入口。
参数、校验、进度、结果都从同一份 schema 来;连界面组件也只是后端的声明,
换 shadcn/ui 或其他 UI,业务代码一行不改。
写一次,四个入口
// 一个命令 = 一个 struct
#[derive(App)]
#[app(name = "count", about = "统计文件", run = "run_count")]
struct Count {
/// 要统计的文件
#[arg(must_exist = true)]
file: PathBuf,
/// 最多显示多少行
#[arg(default = 10, range = 0..=1000000)]
limit: u32,
}
fn main() {
lilyco::run::<Count>(); // ← 全部入口就在这一行
}
脚本与管道
count --file README.md --json-stream
逐行事件,末行 done。
一命令一表单
同一 schema 自动生成;Web 只监听 127.0.0.1,令牌随进程轮换。
天生 AI-callable
count --mcp 直接是 stdio 服务器,tools/list 自动发现。
界面组件:后端只声明,前端自己画
ctx.view(id, Component::…) 发一份带 kind 的 JSON;
内置 GUI 渲染成零依赖 HTML,React 侧换成 shadcn 的 <Progress> 即可。
协议文档与 51 条映射 →
ctx.view("job", Component::Progress {
label: Some("下载中".into()),
current: Some(3), total: Some(10),
});
Rust 声明
ctx.view("job", Component::Progress {
label: Some("下载中".into()),
current: Some(3), total: Some(10),
});Rust 声明
Component::Badge {
text: "就绪".into(),
tone: Tone::Success,
}Rust 声明
Component::Alert {
title: "提示".into(),
description: Some("组件由后端声明".into()),
tone: Tone::Info,
}| 文件 | 行 |
|---|---|
| a.rs | 42 |
| b.rs | 7 |
Rust 声明
Component::Table {
caption: None,
columns: vec!["文件".into(), "行".into()],
rows: vec![vec!["a.rs".into(), "42".into()]],
sortable: false,
}Rust 声明
Component::KeyValue {
items: vec![KeyValueItem {
key: "版本".into(), value: "0.3.0".into(),
}],
}[warn] 已发送取消请求
Rust 声明
Component::Log {
lines: vec![LogLine { level: LogLevel::Info, text: "已启动".into() }],
}
// TUI 无富组件层:降级为一行「视图 id: kind」Rust 声明
Component::Tabs {
items: vec![TabItem { id: "overview".into(), label: "概览".into(), children: vec![] }],
active: Some("overview".into()),
}Rust 声明
Component::Chart {
chart_kind: ChartKind::Bar,
labels: vec!["一".into()],
series: vec![35.0, 60.0, 85.0],
}Rust 声明
Component::Avatar {
name: "lyco".into(),
src: None,
}Rust 声明
Component::Skeleton {
lines: 2,
}Rust 声明
Component::Result {
ok: true,
title: "完成".into(),
value: Some(serde_json::json!({ "steps": 3 })),
}Rust 声明
Component::Dialog {
id: "confirm".into(), title: "确认删除?".into(),
open: true, children: vec![],
description: Some("不可撤销".into()),
}三分钟跑起来
建 crate
只依赖 facade,Web/TUI 用特性开关;默认构建是可移植的 CLI + MCP。
cargo new filecount && cd filecount
# Cargo.toml
lilyco = { version = "0.3", features = ["tui", "web"] }
serde_json = "1"
写一个命令
字段即参数:PathBuf 带校验、枚举变下拉、bool 变开关。
#[derive(App)]
#[app(name = "count", run = "run_count")]
struct Count { #[arg(must_exist = true)] file: PathBuf }
fn run_count(app: &Count, ctx: &Context) -> Result<Value, AppError> {
ctx.done(serde_json::json!({ "file": app.file.display().to_string() }), 0);
Ok(...)
}
挑一个入口
终端里自动进表单,管道里自动落 CLI;脚本与 agent 各有自己的口。
cargo run -- --file README.md # CLI
cargo run -- --file README.md --json-stream
cargo run -- --gui # http://127.0.0.1:8080
cargo run -- --mcp # stdio server
教程:从空 crate 到四端可跑
为什么是这套形状
同一份契约
参数、必填、区间、安全等级只在 Rust 侧写一次;CLI 帮助、Web 表单、MCP inputSchema 都从它派生。
零外部依赖
内置 Web 端是内嵌单页:没有 CDN、没有 webfont、没有图标库,CSP 默认 default-src 'none'。
前端可替换
视图组件是带 kind 的 JSON;内置 GUI 只是其中一种实现,React + shadcn 是另一种。
人来确认,agent 不越权
MCP 面走 DenyElevated:T1+ 命令能被发现,但执行要人点头。
可验收
颜色进表、资源预算、装配顺序、无障碍结构都有测试盯着;设计文档与代码同仓。
全平台
默认特性就是 headless 的 CLI + MCP;Android / Termux 去掉 crossterm 即可编译。
可视化编排(Ultra)
一个进程、三张画布:Axure 式原型、Scratch 式积木、LiteGraph 节点图。启动:bash scripts/ultra-demo.sh → 127.0.0.1:8090。
原型设计器
13 组件拖入画布、图层/属性/预览、8px 吸附、zoom、导出 React。
积木编程
7 个积木垂直咬合、可嵌套;改参数实时生成 JS,导出 JSON/JS。
节点图
LiteGraph 内嵌;端口连线、组拖动/撤销、分层执行与持久化。