ZeroClaw Reflex UI
完整搭建流程
ZeroClaw Gateway + LM Studio + Reflex 本地 AI 管理面板
前言:为什么要给 ZeroClaw 做 Web UI?
ZeroClaw 是一个用 Rust 编写的高性能本地 AI 网关工具,设计目标是速度快、体积小、无依赖。但它本身只有命令行界面(CLI),每次使用都需要手动输入命令,管理起来不够直观。
本文记录了从零开始,用 Python Reflex 框架 为 ZeroClaw 打造一个现代化 Web 管理面板的完整过程,包括踩过的所有坑和最终解决方案。
Python Reflex 框架
GitHub - reflex-dev/reflex: Web apps in pure Python
| 组件 | 说明 |
|---|---|
| ZeroClaw | Rust 编写的本地 AI 网关,提供 /webhook HTTP 接口 |
| LM Studio | 本地大模型运行环境,提供 OpenAI 兼容 API |
| Reflex | Python 全栈 Web 框架,前后端均用 Python 编写 |
| llama.cpp | 底层推理引擎(可选) |
第一步:环境准备
1.1 安装依赖
在 ZeroClaw 项目根目录,激活虚拟环境后安装所需 Python 包:
# 激活虚拟环境(Windows PowerShell)
.venv\Scripts\Activate.ps1
# 安装依赖
pip install reflex psutil python-dotenv requests pywin32
1.2 初始化 Reflex 项目
mkdir zeroclaw-reflex-ui
cd zeroclaw-reflex-ui
reflex init # 选择模板 0(空白)
⚠️ Reflex init 会生成同名的 Python 包目录和入口文件,注意不要覆盖错位置。
1.3 放置主文件
将我们编写的 zeroclaw_reflex_ui.py 覆盖到 Reflex 自动生成的同名文件:
# Windows 命令
move zeroclaw_reflex_ui.py zeroclaw_reflex_ui\
# 提示覆盖时选 Yes(Y)
zeroclaw_reflex_ui.py 完整内容示例:
import re
import time
import tomllib
reflex rx
requests
subprocess
os
threading
dotenv load_dotenv
typing , ,
_ANSI_ESCAPE = re.()
load_dotenv()
GATEWAY_URL =
ZEROCLAW_PATH =
ZEROCLAW_CONFIG = os.path.expanduser()
_gateway_process: [subprocess.Popen] =
_gateway_lock = threading.Lock()
() -> subprocess.Popen:
env = os.environ.copy()
env[] = lm_url
env[] = lm_url
env[] = lm_key
env[] = lm_url
env[] = lm_key
env[] = model
proc = subprocess.Popen(
[ZEROCLAW_PATH, ],
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
)
proc
() -> :
:
r = requests.get(, timeout=)
r.status_code ==
Exception:
(rx.State):
lm_studio_api_url: = os.getenv(, )
lm_studio_api_key: = os.getenv(, )
model_id: = os.getenv(, )
models: [] = []
user_message: =
system_prompt: =
chat_history: [[, ]] = []
is_loading: =
gpu_usage: =
lm_studio_status: =
gateway_status: =
zeroclaw_bin_status: =
():
.lm_studio_api_url = value
():
.lm_studio_api_key = value
():
.model_id = value
():
.user_message = value
():
.system_prompt = value
():
State.send_message
():
:
(ZEROCLAW_CONFIG, ) f:
config = tomllib.load(f)
.system_prompt = config.get(, .system_prompt)
Exception:
():
:
(ZEROCLAW_CONFIG, , encoding=) f:
content = f.read()
escaped = .system_prompt.replace(, ).replace(, )
new_line =
re.search(, content, re.MULTILINE):
content = re.sub(
, new_line, content, flags=re.MULTILINE
)
:
content = new_line + + content
(ZEROCLAW_CONFIG, , encoding=) f:
f.write(content)
State.stop_gateway
State.start_gateway
rx.toast.success()
Exception e:
rx.toast.error()
():
_gateway_process
_gateway_lock:
_check_gateway_alive():
.gateway_status =
rx.toast.info()
_gateway_process _gateway_process.poll() :
_gateway_process.kill()
:
_gateway_process = _start_gateway_process(
.lm_studio_api_url,
.lm_studio_api_key,
.model_id
)
_ ():
time.sleep()
_check_gateway_alive():
.gateway_status =
rx.toast.success()
.gateway_status =
rx.toast.error()
FileNotFoundError:
.gateway_status =
rx.toast.error()
Exception e:
.gateway_status =
rx.toast.error()
():
_gateway_process
_gateway_lock:
_gateway_process _gateway_process.poll() :
_gateway_process.kill()
_gateway_process =
.gateway_status =
rx.toast.success()
:
.gateway_status =
rx.toast.info()
():
:
response = requests.get(
,
headers={: },
timeout=
)
response.status_code == :
data = response.json()
.models = [model[] model data.get(, [])]
.lm_studio_status =
.model_id .models:
.model_id = .models[]
:
.lm_studio_status =
.models = []
Exception:
.lm_studio_status =
.models = []
():
(, ) f:
f.write()
f.write()
f.write()
rx.toast.success()
():
.user_message.strip():
rx.toast.error()
_check_gateway_alive():
rx.toast.error()
user_text = .user_message
.chat_history.append({: , : user_text})
.is_loading =
.user_message =
:
response = requests.post(
,
json={: user_text, : .system_prompt},
timeout=
)
response.status_code == :
data = response.json()
(data, ):
raw = (
data.get()
data.get()
data.get()
data.get()
(data)
)
:
raw = (data)
clean = _ANSI_ESCAPE.sub(, raw)
lines = clean.splitlines()
reply_lines = [
ln ln lines
re.(, ln)
]
reply = .join(reply_lines).strip() clean.strip()
.chat_history.append({: , : reply})
:
.chat_history.append({
: ,
:
})
requests.exceptions.Timeout:
.chat_history.append({
: ,
:
})
Exception e:
.chat_history.append({
: ,
:
})
:
.is_loading =
():
.chat_history = []
():
.zeroclaw_bin_status = os.path.exists(ZEROCLAW_PATH)
.gateway_status = _check_gateway_alive()
:
result = subprocess.run(
[, , ],
capture_output=,
text=,
timeout=
)
.gpu_usage = result.returncode ==
Exception:
.gpu_usage =
.fetch_lm_studio_models()
() -> rx.Component:
rx.box(
rx.text(label, size=, color=, margin_bottom=),
rx.text(value, size=, font_weight=),
border_radius=,
background_color=,
)
() -> rx.Component:
rx.card(
rx.vstack(
rx.heading(, size=),
rx.grid(
status_card(, State.zeroclaw_bin_status),
status_card(, State.gateway_status),
status_card(, State.lm_studio_status),
status_card(, State.gpu_usage),
columns=,
gap=
),
rx.hstack(
rx.button(, on_click=State.start_gateway, color_scheme=, size=),
rx.button(, on_click=State.stop_gateway, color_scheme=, size=),
rx.button(, on_click=State.update_system_status, size=),
spacing=
),
rx.callout(
rx.text(, size=),
color=,
size=
),
spacing=,
),
margin_bottom=
)
() -> rx.Component:
rx.card(
rx.vstack(
rx.heading(, size=),
rx.text(, size=, color=),
rx.(
value=State.lm_studio_api_url,
on_change=State.set_lm_studio_api_url,
placeholder=,
),
rx.text(, size=, color=),
rx.(
value=State.lm_studio_api_key,
on_change=State.set_lm_studio_api_key,
placeholder=,
=,
),
rx.text(, size=, color=),
rx.select(
State.models,
value=State.model_id,
on_change=State.set_model_id,
placeholder=,
),
rx.hstack(
rx.button(, on_click=State.fetch_lm_studio_models, size=),
rx.button(, on_click=State.save_config, color_scheme=, size=),
spacing=
),
rx.divider(),
rx.text(, size=, color=),
rx.callout(
rx.text(, size=),
color=,
size=
),
rx.text_area(
value=State.system_prompt,
on_change=State.set_system_prompt,
placeholder=,
rows=
),
rx.button(
,
on_click=State.save_system_prompt_to_config,
color_scheme=,
size=,
),
spacing=,
),
margin_bottom=
)
() -> rx.Component:
is_user = msg[] ==
rx.box(
rx.hstack(
rx.text(
rx.cond(is_user, , ),
font_weight=,
color=rx.cond(is_user, , ),
white_space=,
min_width=
),
rx.text(, color=),
rx.cond(
is_user,
rx.text(msg[], flex=),
rx.box(
rx.markdown(msg[]),
flex=,
class_name=
)
),
),
background_color=rx.cond(is_user, , ),
border_left=rx.cond(is_user, , ),
border_radius=,
margin_bottom=,
)
() -> rx.Component:
rx.card(
rx.vstack(
rx.hstack(
rx.heading(, size=),
rx.spacer(),
rx.button(, on_click=State.clear_chat, size=, color_scheme=),
),
rx.box(
rx.cond(
State.chat_history.length() == ,
rx.center(
rx.text(, color=, size=),
),
rx.foreach(State.chat_history, chat_bubble)
),
overflow_y=,
border_radius=,
),
rx.form(
rx.hstack(
rx.(
placeholder=,
value=State.user_message,
on_change=State.set_user_message,
name=,
disabled=State.is_loading
),
rx.button(
rx.cond(
State.is_loading,
rx.hstack(rx.spinner(size=), rx.text(), spacing=),
rx.text()
),
=,
disabled=State.is_loading,
color_scheme=,
size=
),
spacing=
),
on_submit=State.handle_form_submit,
),
spacing=,
)
)
() -> rx.Component:
rx.container(
rx.vstack(
rx.heading(, size=, margin_bottom=),
rx.text(, size=, color=, margin_bottom=),
gateway_panel(),
config_panel(),
chat_interface(),
max_width=,
spacing=,
)
)
app = rx.App()
app.add_page(
index,
title=,
on_load=[State.update_system_status, State.load_system_prompt_from_config]
)
__name__ == :
app.run()


