Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8faaae0c6 | ||
|
|
bac5ea01a9 | ||
|
|
161ab9d3a8 | ||
|
|
b0d9a24003 | ||
|
|
97cc2bb0f7 | ||
|
|
a7f64cd797 | ||
|
|
115ce38370 | ||
|
|
357cd0958f | ||
|
|
bfefbef475 | ||
|
|
fb41c79b14 | ||
|
|
038fbe24c8 | ||
|
|
fe79f0e5d4 | ||
|
|
af9fffc4a6 | ||
|
|
7ada90ac27 | ||
|
|
407ecc0f47 | ||
|
|
78b74d1bb1 | ||
|
|
bef3fe560d | ||
|
|
0b19ec64a4 | ||
|
|
6f13d41c11 | ||
|
|
0d27f2b774 | ||
|
|
03685ab5e7 | ||
|
|
7a72a5c69e | ||
|
|
baa2cbc49a | ||
|
|
f41d5d5a7c | ||
|
|
8865084905 | ||
|
|
da9cfb6c9d | ||
|
|
f142e3bae8 | ||
|
|
e3b2d56686 | ||
|
|
aa37ec0e44 | ||
|
|
530a255091 |
No files matched your search
@@ -1,117 +0,0 @@
|
||||
# Gitee Go CI 流水线
|
||||
|
||||
triggers:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
- main
|
||||
|
||||
stages:
|
||||
- lint
|
||||
- test
|
||||
- structure
|
||||
|
||||
jobs:
|
||||
py-compile:
|
||||
name: 语法检查
|
||||
stage: lint
|
||||
image: python:3.12
|
||||
script:
|
||||
- python -c "
|
||||
import py_compile, os, sys;
|
||||
errors = 0;
|
||||
for root, dirs, files in os.walk('.'):
|
||||
dirs[:] = [d for d in dirs if d not in {'.git', '__pycache__', 'node_modules', 'embedded_python'}];
|
||||
for f in files:
|
||||
if f.endswith('.py'):
|
||||
path = os.path.join(root, f);
|
||||
try:
|
||||
py_compile.compile(path, doraise=True);
|
||||
except py_compile.PyCompileError as e:
|
||||
print(f'ERROR: {e}');
|
||||
errors += 1;
|
||||
if errors:
|
||||
print(f'{errors} 个文件有语法错误');
|
||||
sys.exit(1);
|
||||
print('所有文件语法正确')
|
||||
"
|
||||
|
||||
ruff-lint:
|
||||
name: Ruff lint
|
||||
stage: lint
|
||||
image: python:3.12
|
||||
script:
|
||||
- pip install ruff -q
|
||||
- ruff check . --select E,F,W --ignore E501,E402 --exclude .git,__pycache__,embedded_python
|
||||
|
||||
pytest-310:
|
||||
name: 插件测试 (Python 3.10)
|
||||
stage: test
|
||||
image: python:3.10
|
||||
script:
|
||||
- pip install pytest colorama -q
|
||||
- pytest tests/ -v --tb=short
|
||||
|
||||
pytest-311:
|
||||
name: 插件测试 (Python 3.11)
|
||||
stage: test
|
||||
image: python:3.11
|
||||
script:
|
||||
- pip install pytest colorama -q
|
||||
- pytest tests/ -v --tb=short
|
||||
|
||||
pytest-312:
|
||||
name: 插件测试 (Python 3.12)
|
||||
stage: test
|
||||
image: python:3.12
|
||||
script:
|
||||
- pip install pytest colorama -q
|
||||
- pytest tests/ -v --tb=short
|
||||
|
||||
structure:
|
||||
name: 项目结构检查
|
||||
stage: structure
|
||||
image: python:3.12
|
||||
script:
|
||||
- echo "检查项目结构..."
|
||||
- |
|
||||
files=(
|
||||
"ai_cli.py"
|
||||
"launcher.py"
|
||||
"xcli_core/cli_core.py"
|
||||
"xcli_core/cli_base.py"
|
||||
"xcli_core/config.py"
|
||||
"xcli_core/constants.py"
|
||||
"ai_engines/ollama_engine.py"
|
||||
"ai_engines/openai_engine.py"
|
||||
"plugins/code_editor.py"
|
||||
"plugins/cmd_executor.py"
|
||||
"plugins/file_manager.py"
|
||||
"plugins/auto_engineer.py"
|
||||
)
|
||||
missing=0
|
||||
for f in "${files[@]}"; do
|
||||
if [ -f "$f" ]; then
|
||||
echo " ✓ $f"
|
||||
else
|
||||
echo " ✗ $f (缺失!)"
|
||||
missing=$((missing + 1))
|
||||
fi
|
||||
done
|
||||
if [ $missing -gt 0 ]; then
|
||||
echo "${missing} 个文件缺失"
|
||||
exit 1
|
||||
fi
|
||||
echo "项目结构完整"
|
||||
- |
|
||||
engines=$(ls ai_engines/*_engine.py 2>/dev/null | wc -l)
|
||||
echo "AI 引擎数量: $engines"
|
||||
if [ "$engines" -gt 2 ]; then
|
||||
echo "警告: 引擎数量超过 2 个"
|
||||
exit 1
|
||||
fi
|
||||
echo "引擎数量正常"
|
||||
@@ -1,148 +0,0 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, main]
|
||||
pull_request:
|
||||
branches: [master, main]
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
name: 代码检查
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: 安装 Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.12"
|
||||
|
||||
- name: 安装依赖
|
||||
run: pip install ruff
|
||||
|
||||
- name: 语法检查 (py_compile)
|
||||
run: |
|
||||
python -c "
|
||||
import py_compile, os, sys
|
||||
errors = 0
|
||||
for root, dirs, files in os.walk('.'):
|
||||
dirs[:] = [d for d in dirs if d not in {'.git', '__pycache__', 'node_modules', 'embedded_python'}]
|
||||
for f in files:
|
||||
if f.endswith('.py'):
|
||||
path = os.path.join(root, f)
|
||||
try:
|
||||
py_compile.compile(path, doraise=True)
|
||||
except py_compile.PyCompileError as e:
|
||||
print(f'ERROR: {e}')
|
||||
errors += 1
|
||||
if errors:
|
||||
print(f'\n{errors} 个文件有语法错误')
|
||||
sys.exit(1)
|
||||
print('所有文件语法正确')
|
||||
"
|
||||
|
||||
- name: Ruff lint
|
||||
run: ruff check . --select E,F,W --ignore E501,E402,W503 --exclude .git,__pycache__,embedded_python
|
||||
|
||||
test:
|
||||
name: 插件测试
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: ["3.10", "3.11", "3.12"]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: 安装 Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
|
||||
- name: 安装测试依赖
|
||||
run: pip install pytest colorama
|
||||
|
||||
- name: 运行测试
|
||||
run: pytest tests/ -v --tb=short
|
||||
|
||||
structure:
|
||||
name: 项目结构检查
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: 安装 Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.12"
|
||||
|
||||
- name: 检查必要文件
|
||||
run: |
|
||||
echo "检查项目结构..."
|
||||
files=(
|
||||
"ai_cli.py"
|
||||
"launcher.py"
|
||||
"xcli_core/cli_core.py"
|
||||
"xcli_core/cli_base.py"
|
||||
"xcli_core/config.py"
|
||||
"xcli_core/constants.py"
|
||||
"ai_engines/ollama_engine.py"
|
||||
"ai_engines/openai_engine.py"
|
||||
"plugins/code_editor.py"
|
||||
"plugins/git_tools.py"
|
||||
"plugins/cmd_executor.py"
|
||||
"plugins/file_manager.py"
|
||||
"plugins/auto_engineer.py"
|
||||
)
|
||||
missing=0
|
||||
for f in "${files[@]}"; do
|
||||
if [ -f "$f" ]; then
|
||||
echo " $f"
|
||||
else
|
||||
echo " $f (缺失!)"
|
||||
missing=$((missing + 1))
|
||||
fi
|
||||
done
|
||||
if [ $missing -gt 0 ]; then
|
||||
echo "\n${missing} 个文件缺失"
|
||||
exit 1
|
||||
fi
|
||||
echo "\n项目结构完整"
|
||||
|
||||
- name: 检查旧文件已清理
|
||||
run: |
|
||||
old_files=(
|
||||
"ai_engines/mimo_engine.py"
|
||||
"ai_engines/GLM_http_engine.py"
|
||||
"ai_engines/qwq_engine.py"
|
||||
"ai_engines/xfyun_spark_engine.py"
|
||||
"ai_engines/iflow_api_engine.py"
|
||||
"ai_engines/maas_http_engine.py"
|
||||
"ai_engines/SDK_openAI.py"
|
||||
"plugins/git_workflow.py"
|
||||
"plugins/code_search.py"
|
||||
"plugins/project_analyzer.py"
|
||||
"plugins/scheduler.py"
|
||||
)
|
||||
found=0
|
||||
for f in "${old_files[@]}"; do
|
||||
if [ -f "$f" ]; then
|
||||
echo " 旧文件未清理: $f"
|
||||
found=$((found + 1))
|
||||
fi
|
||||
done
|
||||
if [ $found -gt 0 ]; then
|
||||
echo "\n${found} 个旧文件未清理"
|
||||
exit 1
|
||||
fi
|
||||
echo "旧文件已全部清理"
|
||||
|
||||
- name: 检查无重复引擎
|
||||
run: |
|
||||
engines=$(ls ai_engines/*_engine.py 2>/dev/null | wc -l)
|
||||
echo "AI 引擎数量: $engines"
|
||||
if [ "$engines" -gt 2 ]; then
|
||||
echo "警告: 引擎数量超过 2 个 (ollama + openai)"
|
||||
exit 1
|
||||
fi
|
||||
echo "引擎数量正常"
|
||||
@@ -4,6 +4,63 @@
|
||||
|
||||
---
|
||||
|
||||
## v5.4.0 (2026-05-09) — Release
|
||||
|
||||
### ✨ 新增功能
|
||||
|
||||
- **跨平台进程保护模块** (`xcli_core/process_protection.py`)
|
||||
- **单实例保护** — 防止重复启动,Windows 使用命名 Mutex,macOS/Linux 使用 fcntl 文件锁(自动清理残留锁文件)
|
||||
- **进程优先级提升** — Windows: `SetPriorityClass` (ABOVE_NORMAL/HIGH/REALTIME),macOS/Linux: `os.nice()` + `renice`
|
||||
- **Windows PPL 风格保护** — `SetErrorMode` 抑制系统错误弹窗 (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX)、`faulthandler` 崩溃堆栈捕获
|
||||
- **POSIX 信号保护** — macOS/Linux 忽略 SIGHUP(终端关闭不退出)、注册 SIGTERM 优雅关闭、macOS 忽略 SIGPIPE
|
||||
- **看门狗监控** — 后台线程监控父进程状态,父进程退出时自动清理并退出
|
||||
- **关闭钩子系统** — 注册自定义清理函数,进程退出时自动执行
|
||||
- **一键启用** — `enable_process_protection()` 单函数调用启用全部保护
|
||||
- **`/protect` 命令** — CLI 中查看保护状态,`/protect test` 重新测试所有保护
|
||||
|
||||
### 改进
|
||||
|
||||
- `platform_utils.py` 新增 `is_process_running()`、`get_process_priority()`、`set_process_priority()` 跨平台工具函数
|
||||
- `cli_base.py` 启动时自动初始化进程保护,退出时自动清理
|
||||
- `cli_core.py` 启动 banner 显示保护状态图标(单实例/优先级/PPL/看门狗)
|
||||
- 全版本号统一更新至 v5.4.0
|
||||
|
||||
---
|
||||
|
||||
## v5.2.0 (2026-05-08)
|
||||
|
||||
### ✨ 新增功能
|
||||
|
||||
- **插件内存管理器** (`plugin_memory_manager.py`) — TTL 自动淘汰 + 懒加载机制
|
||||
- 后台线程定期扫描空闲插件,超过 TTL(默认 5 分钟)自动从内存卸载,释放资源
|
||||
- AI 调用已卸载的插件时自动重新加载(懒加载),对用户完全透明
|
||||
- 核心插件(`tool_search`)可钉住永不淘汰,保障基础功能可用
|
||||
- 可配置参数:TTL 时间、最大空闲插件数、扫描间隔、钉住列表
|
||||
- `/status` 命令和 TUI 侧栏实时显示内存管理状态(已加载/已卸载/统计)
|
||||
- `search_tools` 支持搜索已卸载插件(使用缓存的元数据,无需加载实例)
|
||||
|
||||
### 统一插件格式
|
||||
|
||||
- **统一自研插件入口类名为 `Liugin`** — 18 个插件从 `class Plugin` 统一为 `class Liugin`
|
||||
- 加载器保留 `Plugin` 回退兼容,第三方插件仍可用旧格式
|
||||
- 测试块中 `Plugin()` 调用同步更新为 `Liugin()`
|
||||
|
||||
### TUI 修复
|
||||
|
||||
- **修复消息分类失效** — `_write_raw` 中空字符串 `'' in msg` 永远为 True,导致所有消息显示为工具成功样式;替换为正确的 emoji 标记(✅/❌/🤖)
|
||||
- **修复侧栏切换失效** — Textual 无 `.visible` 布尔属性,改用 `has_class("hidden")` + CSS 类控制显隐
|
||||
- **修复侧栏隐藏动画** — CSS `display: none` 阻止过渡动画,改用 `width: 0` + `opacity: 0` 方案
|
||||
- **修复 TextArea 导入缺失** — `constants.py` 可用性检查未导入 `TextArea`、`Rule`
|
||||
- **修复 `set_timer(0)` 崩溃** — 欢迎动画首行延迟为 0.0 导致 Textual 8.x 除零错误(`ZeroDivisionError`),使用 `max(0.01, delay)` 修复
|
||||
|
||||
### 改进
|
||||
|
||||
- `UnifiedToolManager` 重构:集成 `PluginMemoryManager`,所有工具访问统一走 `_ensure_loaded` 路径
|
||||
- 新增 `get_all_tools_info()` 方法,系统提示词可列出所有注册工具(含已卸载)
|
||||
- 新增 `get_tool_by_name()` / `memory_manager` 属性,兼容旧 `LiuginManager` 接口
|
||||
|
||||
---
|
||||
|
||||
## v5.1.4 (2026-04-29)
|
||||
|
||||
### 新增功能
|
||||
|
||||
@@ -222,6 +222,54 @@ tool_search 浏览器 # 搜索浏览器相关工具
|
||||
|
||||
---
|
||||
|
||||
## 插件市场
|
||||
|
||||
通过 `/plugin` 命令管理插件,支持从远程仓库一键安装。
|
||||
|
||||
### 查看可用插件
|
||||
|
||||
```
|
||||
/plugin list # 列出所有可用插件(自动同步远程源)
|
||||
/plugin search 郊狼 # 搜索关键词
|
||||
/plugin info dglab # 查看插件详情
|
||||
```
|
||||
|
||||
### 安装插件
|
||||
|
||||
```
|
||||
/plugin install dglab # 通过插件码安装
|
||||
/plugin install https://example.com/my_plugin.py # 从 URL 安装
|
||||
```
|
||||
|
||||
安装后自动:
|
||||
- 下载插件文件到 `plugins/` 目录
|
||||
- 在 `plugins_config.py` 中启用
|
||||
- 尝试动态加载(无需重启)
|
||||
|
||||
### 卸载插件
|
||||
|
||||
```
|
||||
/plugin remove dglab_ws # 卸载指定插件
|
||||
```
|
||||
|
||||
### 更新插件
|
||||
|
||||
```
|
||||
/plugin update dglab # 重新下载更新
|
||||
```
|
||||
|
||||
### 添加插件源
|
||||
|
||||
```
|
||||
/plugin add-source https://example.com/registry.json # 同步远程插件源
|
||||
```
|
||||
|
||||
### 插件码注册表
|
||||
|
||||
插件码是插件的唯一标识,映射到下载地址。注册表文件:`plugin_registry.json`
|
||||
|
||||
---
|
||||
|
||||
## 聊天记录
|
||||
|
||||
```
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/version-5.1.2-blue" alt="version">
|
||||
<img src="https://img.shields.io/badge/version-5.4.1-blue" alt="version">
|
||||
<img src="https://img.shields.io/badge/python-3.10+-green" alt="python">
|
||||
<img src="https://img.shields.io/badge/license-MIT-orange" alt="license">
|
||||
<img src="https://img.shields.io/badge/tests-113%20passed-brightgreen" alt="tests">
|
||||
|
||||
@@ -1,39 +1,46 @@
|
||||
# 小狸 Pro-CLI
|
||||
|
||||
<p align="center">
|
||||
<img src="logo.png" width="120" alt="xiaoli-cli logo">
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<strong>智能编程助手 — 代码编辑 · Git 集成 · 浏览器自动化 · 多 Agent 协作 · 图像生成 · 记忆系统</strong>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<img src="https://img.shields.io/badge/version-5.1.4-blue" alt="version">
|
||||
<img src="https://img.shields.io/badge/version-5.4.1-blue" alt="version">
|
||||
<img src="https://img.shields.io/badge/python-3.10+-green" alt="python">
|
||||
<img src="https://img.shields.io/badge/license-MIT-orange" alt="license">
|
||||
<img src="https://img.shields.io/badge/tests-150+%20passed-brightgreen" alt="tests">
|
||||
<img src="https://img.shields.io/badge/plugins-19-blueviolet" alt="plugins">
|
||||
<img src="https://img.shields.io/badge/MCP-supported-brightgreen" alt="mcp">
|
||||
</p>
|
||||
|
||||
---
|
||||
|
||||
## 特性
|
||||
## 简介
|
||||
|
||||
- **双 AI 引擎** — Ollama 本地模型 + OpenAI 兼容格式(DeepSeek、Grok、硅基流动等)
|
||||
- **代码编辑与搜索** — 精准替换、批量编辑、符号提取、依赖分析、正则搜索、**自动语法检查**
|
||||
- **Git 版本控制** — 完整 Git 操作 + 工作流自动化(smart-commit、changelog)
|
||||
- **浏览器自动化** — Playwright 驱动,导航/交互/截图/JS 执行/PDF
|
||||
- **子 Agent 系统** — 多 Agent 并行协作,独立任务分配与结果回收
|
||||
- **统一安全层** — AI 风险识别 + 三模式切换(普通/人工/无限制)
|
||||
- **图像生成** — 基于 ComfyUI 在线服务,自然语言/Tag 双模式,支持中文描述
|
||||
- **记忆系统** — 长期记忆 + 每日日记 + AI 写日记 + 全局搜索 + 自动保存对话
|
||||
- **Clawli 远程模式** — PC-手机 WebSocket 双向通信,支持内网穿透代理
|
||||
- **定时任务** — 支持相对时间、绝对时间、重复任务,到期自动激活 AI
|
||||
- **系统通知** — 跨平台任务完成通知(Windows / macOS / Linux)
|
||||
- **MCP 协议** — 19 个插件全部实现 MCP 定义,支持 Function Calling 原生调用
|
||||
- **19 个插件 161+ 个操作** — 代码编辑、Git、浏览器、工程化、文件管理、图像生成、记忆、桌面GUI自动化等
|
||||
- **三模式界面** — TUI 图形化 + 传统命令行 + **Web UI 浏览器界面**
|
||||
- **150+ 个自动化测试** — pytest 覆盖全部插件和核心模块
|
||||
小狸 Pro-CLI 是一款基于 Python 的智能编程助手,集成了代码编辑、版本控制、浏览器自动化、图像生成、记忆系统等功能,支持多种 AI 引擎,帮助开发者提升工作效率。
|
||||
|
||||
## 快速开始
|
||||
## 特性
|
||||
|
||||
- **双 AI 引擎** — Ollama 本地模型 + OpenAI 兼容格式(DeepSeek、Grok、硅基流动等)
|
||||
- **代码编辑与搜索** — 精准替换、批量编辑、符号提取、依赖分析、正则搜索、自动语法检查
|
||||
- **Git 版本控制** — 完整 Git 操作 + 工作流自动化(smart-commit、changelog)
|
||||
- **浏览器自动化** — Playwright 驱动,导航、交互、截图、JS 执行、PDF 导出
|
||||
- **子 Agent 系统** — 多 Agent 并行协作,独立任务分配与结果回收
|
||||
- **图像生成** — 基于 ComfyUI 在线服务,自然语言/Tag 双模式,支持中文描述
|
||||
- **记忆系统** — 长期记忆 + 每日日记 + AI 写日记 + 全局搜索 + 自动保存对话
|
||||
- **远程通信** — PC 与手机 WebSocket 双向通信
|
||||
- **定时任务** — 支持相对时间、绝对时间、重复任务,到期自动激活 AI
|
||||
- **系统通知** — 跨平台任务完成通知(Windows / macOS / Linux)
|
||||
- **MCP 协议** — 19 个插件全部实现 MCP 定义,支持 Function Calling 原生调用
|
||||
- **进程保护** — 跨平台单实例保护 + 进程优先级提升 + Windows PPL 风格保护 + 看门狗监控 + 优雅关闭
|
||||
- **19 个插件 161+ 个操作** — 代码编辑、Git、浏览器、工程化、文件管理、图像生成、记忆等
|
||||
- **三模式界面** — TUI 图形化 + 传统命令行 + Web UI 浏览器界面
|
||||
- **150+ 个自动化测试** — pytest 覆盖全部插件和核心模块
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 安装
|
||||
|
||||
@@ -52,11 +59,11 @@ python ai_cli.py
|
||||
# 指定引擎
|
||||
python ai_cli.py --engine openai
|
||||
|
||||
# Windows 一键启动(无需 Python 环境)
|
||||
# Windows 一键启动
|
||||
launcher.exe
|
||||
```
|
||||
|
||||
## AI 引擎
|
||||
## AI 引擎
|
||||
|
||||
| 引擎 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
@@ -87,15 +94,17 @@ launcher.exe
|
||||
}
|
||||
```
|
||||
|
||||
## 插件列表 (19 个)
|
||||
## 插件列表 (19 个)
|
||||
|
||||
> 更多社区插件请访问 [xiaoli-cli-plugins](https://gitee.com/shuiyu1123/xiaoli-cli-plugins),使用 `/plugin install <插件码>` 一键安装。
|
||||
|
||||
### 核心生产力
|
||||
|
||||
| 插件 | 操作数 | 说明 |
|
||||
|------|--------|------|
|
||||
| `code_editor` | 24 | 代码编辑+搜索。精准替换、批量编辑、find/regex/symbols/imports/callers/todo/structure + 自动语法检查 + diff 弹窗 |
|
||||
| `code_editor` | 24 | 代码编辑+搜索。精准替换、批量编辑、find/regex/symbols/imports/callers/todo/structure + 自动语法检查 |
|
||||
| `git_tools` | 17 | Git 版本控制。status/diff/log/commit/branch + smart-commit/changelog/contributors/stale |
|
||||
| `cmd_executor` | 1 | Shell 命令执行。跨平台,超时控制,危险命令拦截 |
|
||||
| `cmd_executor` | 1 | Shell 命令执行,跨平台,超时控制 |
|
||||
| `file_manager` | 10 | 文件系统。list/read/write/copy/move/delete/search/info/mkdir/append |
|
||||
| `auto_engineer` | 12 | 工程化。lint/format/test/build/deps + 复杂度/安全扫描/质量指标/项目初始化 |
|
||||
| `task_manager` | 7 | 任务管理。add/list/complete/delete/clear,JSON 持久化 |
|
||||
@@ -106,12 +115,12 @@ launcher.exe
|
||||
|------|--------|------|
|
||||
| `sub_agent` | 8 | 子 Agent 系统。spawn/list/status/result/send/kill,多 Agent 并行协作 |
|
||||
| `browser_auto` | 28 | 浏览器自动化。导航/点击/填写/截图/JS执行/PDF/cookies(基于 Playwright) |
|
||||
| `gui_auto` | 20 | **桌面GUI自动化**。基于 Windows UIA,屏幕元素识别/点击/输入/截图/元素树遍历(仅Windows) |
|
||||
| `ai_image` | — | **AI生图**。基于 ai.2x.nz 自然语言生图,异步生成 + WebSocket 进度推送 |
|
||||
| `image_generator` | 7 | **图像生成**。基于 ComfyUI 在线服务,自然语言/Tag 双模式,28 个角色工作流 |
|
||||
| `memory_plugin` | 11 | **记忆系统**。AI 写日记/搜索记忆/管理聊天记录/长期记忆 MEMORY.md |
|
||||
| `scheduler` | 4 | **定时任务**。支持相对时间/绝对时间/重复任务,到期自动激活 AI |
|
||||
| `tool_search` | — | 工具搜索。按关键词发现可用插件(供 AI 按需调用) |
|
||||
| `gui_auto` | 20 | 桌面 GUI 自动化。基于 Windows UIA,屏幕元素识别/点击/输入/截图(仅 Windows) |
|
||||
| `ai_image` | — | AI 生图。基于 ai.2x.nz 自然语言生图,异步生成 + WebSocket 进度推送 |
|
||||
| `image_generator` | 7 | 图像生成。基于 ComfyUI 在线服务,自然语言/Tag 双模式,28 个角色工作流 |
|
||||
| `memory_plugin` | 11 | 记忆系统。AI 写日记/搜索记忆/管理聊天记录/长期记忆 |
|
||||
| `scheduler` | 4 | 定时任务。支持相对时间/绝对时间/重复任务,到期自动激活 AI |
|
||||
| `tool_search` | — | 工具搜索。按关键词发现可用插件 |
|
||||
|
||||
### 辅助工具
|
||||
|
||||
@@ -122,9 +131,9 @@ launcher.exe
|
||||
| `frontend_tester` | 前端测试。HTML/CSS/JS 语法检查、响应式验证 |
|
||||
| `speech_recognition` | 语音识别。录音 + 语音转文字(Whisper) |
|
||||
| `audio_player` | 音频播放 |
|
||||
| `send_image` | Clawli 模式下发图片到手机 |
|
||||
| `send_image` | 远程模式下发图片到手机 |
|
||||
|
||||
## 记忆系统
|
||||
## 记忆系统
|
||||
|
||||
内置持久化记忆系统,AI 可以记住之前的工作:
|
||||
|
||||
@@ -144,28 +153,26 @@ memory_plugin remember 用户偏好深色主题
|
||||
memory_plugin chat_search 图像生成
|
||||
```
|
||||
|
||||
## Clawli 远程模式
|
||||
## 远程通信
|
||||
|
||||
通过 WebSocket 实现 PC-手机双向通信:
|
||||
通过 WebSocket 实现 PC 与手机双向通信:
|
||||
|
||||
```bash
|
||||
/remote start 9079 mypassword # 启动本地服务
|
||||
/remote proxy relay.example.com # 通过中继服务器穿透内网
|
||||
/remote stop # 停止服务
|
||||
/remote status # 查看状态
|
||||
```
|
||||
|
||||
功能:手机发消息给 AI、AI 回复推到手机、文件传输、图片识别。
|
||||
|
||||
## 安全机制
|
||||
## 安全机制
|
||||
|
||||
### 三模式切换 (`/safe`)
|
||||
### 模式切换 (`/safe`)
|
||||
|
||||
| 模式 | 图标 | 说明 |
|
||||
|------|------|------|
|
||||
| **普通模式** (默认) | | AI 识别风险,有风险才请求确认 |
|
||||
| **人工确认** | | 所有指令都需要用户确认 |
|
||||
| **无限制** | | 直接执行,不检查 |
|
||||
| 模式 | 说明 |
|
||||
|------|------|
|
||||
| **普通模式** (默认) | AI 识别风险,有风险才请求确认 |
|
||||
| **人工确认** | 所有指令都需要用户确认 |
|
||||
|
||||
### 检查流程
|
||||
|
||||
@@ -173,20 +180,20 @@ memory_plugin chat_search 图像生成
|
||||
工具调用 → safety.check()
|
||||
├─ 快速预检(读取/查询类)→ 直接放行
|
||||
├─ 普通模式 → AI 独立分析风险
|
||||
│ ├─ safe=true → 放行
|
||||
│ └─ safe=false → 展示风险等级+影响,y/n 确认
|
||||
│ ├─ safe → 放行
|
||||
│ └─ unsafe → 展示风险等级+影响,y/n 确认
|
||||
└─ 特殊指令(rm/delete/chmod)→ 始终确认
|
||||
```
|
||||
|
||||
### 代码沙箱
|
||||
### 代码执行
|
||||
|
||||
独立的安全代码执行环境:
|
||||
- AST 级危险代码检测
|
||||
- 模块白名单(阻断 os/subprocess/socket 等 25 个模块)
|
||||
- 模块白名单(阻断 25 个高危模块)
|
||||
- 子进程隔离执行
|
||||
- 内存限制
|
||||
|
||||
## MCP 协议支持
|
||||
## MCP 协议支持
|
||||
|
||||
所有 19 个插件均实现 MCP (Model Context Protocol) 定义,支持三种工具调用协议:
|
||||
|
||||
@@ -196,27 +203,25 @@ memory_plugin chat_search 图像生成
|
||||
| **FC** | OpenAI Function Calling 原生调用 |
|
||||
| **JSON** | 传统 `{"action": "use_tool", ...}` 格式 |
|
||||
|
||||
`fc_tools.py` 自动将 MCP 定义转换为 OpenAI FC 格式,支持混合响应解析。
|
||||
|
||||
## 命令列表
|
||||
## 命令列表
|
||||
|
||||
| 命令 | 说明 |
|
||||
|------|------|
|
||||
| `/help` | 显示帮助 |
|
||||
| `/safe` | 切换安全模式(普通→人工→无限制) |
|
||||
| `/safe` | 切换安全模式 |
|
||||
| `/engine list` | 列出可用引擎 |
|
||||
| `/engine switch <引擎>` | 切换引擎 |
|
||||
| `/engine.openai models` | 列出 OpenAI 引擎模型 |
|
||||
| `/chat save/list/open` | 聊天记录管理 |
|
||||
| `/file.read <路径>` | 读取文件 |
|
||||
| `/remote` | Clawli 远程连接帮助 |
|
||||
| `/remote` | 远程连接帮助 |
|
||||
| `/notify` | 切换任务完成通知 |
|
||||
| `/scheduler` / `/remind` | 管理定时任务 |
|
||||
| `/memory` | 管理记忆系统 |
|
||||
| `/tui` | 切换 TUI 模式 |
|
||||
| `/quit` | 退出 |
|
||||
|
||||
## Web UI
|
||||
## Web UI
|
||||
|
||||
WebSocket 实时交互界面,支持完整的 AI 对话体验。
|
||||
|
||||
@@ -225,89 +230,85 @@ WebSocket 实时交互界面,支持完整的 AI 对话体验。
|
||||
python webui_server.py --port 8080
|
||||
# 浏览器访问 http://localhost:8080
|
||||
|
||||
# 方式 2: 直接打开静态页面(功能有限)
|
||||
# 方式 2: 直接打开静态页面
|
||||
start webui.html # Windows
|
||||
open webui.html # macOS
|
||||
xdg-open webui.html # Linux
|
||||
```
|
||||
|
||||
## 项目结构
|
||||
## 项目结构
|
||||
|
||||
```
|
||||
xiaoli-cli/
|
||||
├── ai_cli.py # 主入口
|
||||
├── webui.html # Web UI 界面 (WebSocket 实时交互)
|
||||
├── webui_server.py # Web UI 后端 (WebSocket + 静态文件服务)
|
||||
├── launcher.py # 智能启动器(自动检测环境/安装依赖)
|
||||
├── webui.html # Web UI 界面
|
||||
├── webui_server.py # Web UI 后端
|
||||
├── launcher.py # 智能启动器
|
||||
├── config.json.example # 配置模板
|
||||
├── easter_egg.py # 彩蛋
|
||||
│
|
||||
├── easter_egg.py # 彩蛋
|
||||
├── xcli_core/ # 核心模块
|
||||
│ ├── cli_core.py # 主循环 + 提示词构建
|
||||
│ ├── cli_base.py # 初始化 + 引擎/插件加载
|
||||
│ ├── cli_tools.py # 工具调用执行(支持 MCP/FC/JSON)
|
||||
│ ├── cli_clawli.py # Clawli 远程模式
|
||||
│ ├── cli_code_exec.py # 代码沙箱执行
|
||||
│ ├── cli_tools.py # 工具调用执行
|
||||
│ ├── cli_clawli.py # 远程模式
|
||||
│ ├── cli_code_exec.py # 代码执行
|
||||
│ ├── cli_display.py # 显示输出
|
||||
│ ├── cli_history.py # 聊天记录
|
||||
│ ├── config.py # 配置管理
|
||||
│ ├── constants.py # 常量定义
|
||||
│ ├── fc_tools.py # MCP → FC 格式转换
|
||||
│ ├── memory.py # 记忆系统(长期记忆+日记+搜索+压缩)
|
||||
│ ├── memory.py # 记忆系统
|
||||
│ ├── notification.py # 跨平台系统通知
|
||||
│ ├── plugin_manager.py # 插件管理器(Liugin/Plugin 双协议)
|
||||
│ ├── safety.py # 统一安全层(AI 风险分析)
|
||||
│ ├── sandbox.py # 代码安全沙箱(AST 级检测)
|
||||
│ └── tui.py # TUI 界面(Textual + 动画)
|
||||
│
|
||||
│ ├── plugin_manager.py # 插件管理器
|
||||
│ ├── safety.py # 统一安全层
|
||||
│ ├── sandbox.py # 代码安全沙箱
|
||||
│ └── tui.py # TUI 界面
|
||||
├── ai_engines/ # AI 引擎
|
||||
│ ├── ollama_engine.py # Ollama 本地模型
|
||||
│ └── openai_engine.py # OpenAI 兼容格式
|
||||
│
|
||||
│ ├── ollama_engine.py
|
||||
│ └── openai_engine.py
|
||||
├── plugins/ # 插件 (19 个)
|
||||
│ ├── code_editor.py # 代码编辑+搜索 (24 操作)
|
||||
│ ├── git_tools.py # Git 版本控制 (17 操作)
|
||||
│ ├── cmd_executor.py # Shell 命令执行
|
||||
│ ├── file_manager.py # 文件管理 (10 操作)
|
||||
│ ├── auto_engineer.py # 工程化自动化 (12 操作)
|
||||
│ ├── task_manager.py # 任务管理
|
||||
│ ├── sub_agent.py # 子 Agent 系统 (8 操作)
|
||||
│ ├── browser_auto.py # 浏览器自动化 (28 操作)
|
||||
│ ├── gui_auto.py # 桌面GUI自动化 (20 操作) ⭐ NEW
|
||||
│ ├── ai_image.py # AI生图 (ai.2x.nz) ⭐ NEW
|
||||
│ ├── image_generator.py # 图像生成 (7 操作) ⭐ NEW
|
||||
│ ├── memory_plugin.py # 记忆系统 (11 操作) ⭐ NEW
|
||||
│ ├── scheduler.py # 定时任务
|
||||
│ ├── tool_search.py # 工具搜索
|
||||
│ ├── network_tools.py # 网络工具
|
||||
│ ├── ai_search.py # AI 搜索
|
||||
│ ├── frontend_tester.py # 前端测试
|
||||
│ ├── speech_recognition.py # 语音识别
|
||||
│ └── send_image.py # 图片发送
|
||||
│
|
||||
├── skills/ # 技能 (Skill 协议)
|
||||
├── image_engine/ # 图像识别引擎(Ollama 视觉模型)
|
||||
├── tests/ # 测试套件 (113 用例)
|
||||
│ ├── code_editor.py
|
||||
│ ├── git_tools.py
|
||||
│ ├── cmd_executor.py
|
||||
│ ├── file_manager.py
|
||||
│ ├── auto_engineer.py
|
||||
│ ├── task_manager.py
|
||||
│ ├── sub_agent.py
|
||||
│ ├── browser_auto.py
|
||||
│ ├── gui_auto.py
|
||||
│ ├── ai_image.py
|
||||
│ ├── image_generator.py
|
||||
│ ├── memory_plugin.py
|
||||
│ ├── scheduler.py
|
||||
│ ├── tool_search.py
|
||||
│ ├── network_tools.py
|
||||
│ ├── ai_search.py
|
||||
│ ├── frontend_tester.py
|
||||
│ ├── speech_recognition.py
|
||||
│ └── send_image.py
|
||||
├── skills/ # 技能
|
||||
├── image_engine/ # 图像识别引擎
|
||||
├── tests/ # 测试套件 (150+ 用例)
|
||||
├── love.txt # 彩蛋情话
|
||||
└── about.txt # 关于信息
|
||||
```
|
||||
|
||||
## 测试
|
||||
## 测试
|
||||
|
||||
```bash
|
||||
# 运行全部测试 (113 用例)
|
||||
# 运行全部测试
|
||||
python -m pytest tests/ -v
|
||||
|
||||
# 语法检查
|
||||
python -c "import py_compile; py_compile.compile('ai_cli.py', doraise=True)"
|
||||
```
|
||||
|
||||
## 插件开发
|
||||
## 插件开发
|
||||
|
||||
小狸支持 `Liugin`(自研协议)和 `Plugin`(通用协议)两种类名,均可被插件管理器自动识别:
|
||||
|
||||
```python
|
||||
class Plugin: # 或 class Liugin
|
||||
class Plugin:
|
||||
"""我的插件"""
|
||||
|
||||
def __init__(self):
|
||||
@@ -341,13 +342,13 @@ class Plugin: # 或 class Liugin
|
||||
return "处理结果"
|
||||
```
|
||||
|
||||
## 许可证
|
||||
## 许可证
|
||||
|
||||
[MIT License](LICENSE)
|
||||
|
||||
## 链接
|
||||
## 链接
|
||||
|
||||
- Gitee: https://gitee.com/shuiyu1123/xiaoli-cli
|
||||
- [更新日志](CHANGELOG.md)
|
||||
- [使用指南](HELP.md)
|
||||
- [AI 编程助手横评](compare.html)
|
||||
- Gitee: https://gitee.com/shuiyu1123/xiaoli-cli
|
||||
- 插件仓库: https://gitee.com/shuiyu1123/xiaoli-cli-plugins
|
||||
- [更新日志](CHANGELOG.md)
|
||||
- [使用指南](HELP.md)
|
||||
@@ -122,6 +122,13 @@ Ollama AI引擎插件帮助信息
|
||||
if not self.is_service_running:
|
||||
return "错误:Ollama服务未运行,请启动Ollama服务后再使用此引擎."
|
||||
|
||||
# 检查模型是否已配置
|
||||
if not self.model or not self.model.strip():
|
||||
return ("错误:Ollama模型未配置。请执行以下操作:\n"
|
||||
" 1. /engine.ollama models → 查看本地可用模型\n"
|
||||
" 2. /engine.ollama set <模型名> → 设置要使用的模型\n"
|
||||
" 或编辑 config.json 中 api.engines.ollama.model 字段")
|
||||
|
||||
# 如果有工具结果,将其作为上下文返回给AI处理
|
||||
if tool_results:
|
||||
tool_result_text = f"工具执行结果: {tool_results.get('result', '无结果')}"
|
||||
|
||||
-1161
File diff suppressed because it is too large.
Load diff
+2
-2
@@ -1,10 +1,10 @@
|
||||
@echo off
|
||||
chcp 65001 >nul 2>&1
|
||||
title 小狸 Pro-CLI v5.1.2
|
||||
title 小狸 Pro-CLI v5.4.0
|
||||
|
||||
echo.
|
||||
echo ╔══════════════════════════════════════╗
|
||||
echo ║ 小狸 Pro-CLI 启动器 v5.1.2 ║
|
||||
echo ║ 小狸 Pro-CLI 启动器 v5.4.0 ║
|
||||
echo ║ 正在检测环境... ║
|
||||
echo ╚══════════════════════════════════════╝
|
||||
echo.
|
||||
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
小狸 MCP Stdio Server
|
||||
=====================
|
||||
标准 MCP (Model Context Protocol) stdio 传输层入口。
|
||||
通过 stdin/stdout 以 JSON-RPC 2.0 格式通信,
|
||||
可被 Claude Desktop、Cursor、OpenClaw 等标准 MCP 客户端直接连接。
|
||||
|
||||
用法:
|
||||
python mcp_server.py [--plugins-dir plugins] [--skills-dir skills]
|
||||
|
||||
Claude Desktop 配置示例:
|
||||
{
|
||||
"mcpServers": {
|
||||
"xiaoli": {
|
||||
"command": "python",
|
||||
"args": ["/path/to/xiaoli-cli/mcp_server.py"]
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
# 确保当前目录在 sys.path 中,以便导入项目模块
|
||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
if SCRIPT_DIR not in sys.path:
|
||||
sys.path.insert(0, SCRIPT_DIR)
|
||||
|
||||
# 日志输出到 stderr,不干扰 stdout 上的 JSON-RPC 通信
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="[xiaoli-mcp] %(message)s",
|
||||
stream=sys.stderr,
|
||||
)
|
||||
logger = logging.getLogger("xiaoli-mcp")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="小狸 MCP Stdio Server")
|
||||
parser.add_argument("--plugins-dir", default="plugins", help="插件目录 (默认: plugins)")
|
||||
parser.add_argument("--skills-dir", default="skills", help="技能目录 (默认: skills)")
|
||||
args = parser.parse_args()
|
||||
|
||||
# 切换到脚本所在目录,保证相对路径正确
|
||||
os.chdir(SCRIPT_DIR)
|
||||
|
||||
# 初始化 UnifiedToolManager
|
||||
# 将 initialize() 中的 print 重定向到 stderr,避免污染 stdout JSON-RPC 通道
|
||||
import io
|
||||
from contextlib import redirect_stdout
|
||||
|
||||
from unified_tool_manager import UnifiedToolManager
|
||||
|
||||
manager = UnifiedToolManager()
|
||||
captured_output = io.StringIO()
|
||||
with redirect_stdout(captured_output):
|
||||
manager.initialize(
|
||||
plugins_dir=args.plugins_dir,
|
||||
skills_dir=args.skills_dir,
|
||||
)
|
||||
|
||||
# 将初始化时的 print 输出转发到 stderr
|
||||
init_output = captured_output.getvalue()
|
||||
if init_output:
|
||||
for line in init_output.strip().split('\n'):
|
||||
logger.info(line)
|
||||
|
||||
tool_count = len(manager._tools)
|
||||
logger.info(f"MCP Server 已启动,共加载 {tool_count} 个工具")
|
||||
logger.info("等待 stdin JSON-RPC 请求...")
|
||||
|
||||
# 主循环:从 stdin 逐行读取 JSON-RPC 请求
|
||||
try:
|
||||
for line in sys.stdin:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
try:
|
||||
request = json.loads(line)
|
||||
except json.JSONDecodeError as e:
|
||||
# 无法解析的 JSON,返回解析错误
|
||||
error_resp = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": None,
|
||||
"error": {"code": -32700, "message": f"Parse error: {e}"},
|
||||
}
|
||||
_send(error_resp)
|
||||
continue
|
||||
|
||||
# 处理通知(没有 id 字段的请求)— MCP 规范中通知不需要响应
|
||||
req_id = request.get("id")
|
||||
method = request.get("method", "")
|
||||
|
||||
# 特殊处理 notifications/initialized — 客户端初始化完成通知
|
||||
if method == "notifications/initialized":
|
||||
logger.info("客户端已发送 initialized 通知")
|
||||
continue
|
||||
|
||||
# 特殊处理 $/cancelRequest
|
||||
if method == "$/cancelRequest":
|
||||
logger.info(f"收到取消请求: {request.get('params', {}).get('id')}")
|
||||
continue
|
||||
|
||||
# 委托给 UnifiedToolManager 处理
|
||||
# 拦截插件可能产生的 stdout 输出,转发到 stderr
|
||||
captured = io.StringIO()
|
||||
try:
|
||||
with redirect_stdout(captured):
|
||||
response = manager.handle_mcp_request(request)
|
||||
plugin_output = captured.getvalue()
|
||||
if plugin_output:
|
||||
for p_line in plugin_output.strip().split('\n'):
|
||||
logger.info(f"[plugin] {p_line}")
|
||||
except Exception as e:
|
||||
logger.error(f"处理请求异常: {e}")
|
||||
response = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": req_id,
|
||||
"error": {"code": -32603, "message": f"Internal error: {e}"},
|
||||
}
|
||||
|
||||
_send(response)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logger.info("收到中断信号,正在关闭...")
|
||||
except EOFError:
|
||||
logger.info("stdin 已关闭,正在退出...")
|
||||
finally:
|
||||
manager.shutdown()
|
||||
logger.info("MCP Server 已关闭")
|
||||
|
||||
|
||||
def _send(response: dict):
|
||||
"""将 JSON-RPC 响应写入 stdout 并刷新"""
|
||||
text = json.dumps(response, ensure_ascii=False)
|
||||
sys.stdout.write(text + "\n")
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+76
-1
@@ -121,4 +121,79 @@ def get_config_directory():
|
||||
elif platform == 'macos':
|
||||
return os.path.join(os.path.expanduser("~"), 'Library', 'Application Support', 'Xiaoli Pro-CLI 3')
|
||||
else: # Linux
|
||||
return os.path.join(os.path.expanduser('~'), '.config', 'xiaoli-pro-cli-3')
|
||||
return os.path.join(os.path.expanduser('~'), '.config', 'xiaoli-pro-cli-3')
|
||||
|
||||
def is_process_running(pid):
|
||||
"""检查指定 PID 的进程是否在运行"""
|
||||
platform = get_platform()
|
||||
|
||||
try:
|
||||
if platform == 'windows':
|
||||
import ctypes
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
handle = kernel32.OpenProcess(0x1000, False, pid) # PROCESS_QUERY_LIMITED_INFORMATION
|
||||
if handle:
|
||||
kernel32.CloseHandle(handle)
|
||||
return True
|
||||
return False
|
||||
else:
|
||||
os.kill(pid, 0)
|
||||
return True
|
||||
except (OSError, ProcessLookupError):
|
||||
return False
|
||||
|
||||
|
||||
def get_process_priority():
|
||||
"""获取当前进程优先级"""
|
||||
platform = get_platform()
|
||||
|
||||
try:
|
||||
if platform == 'windows':
|
||||
import ctypes
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
handle = kernel32.GetCurrentProcess()
|
||||
return kernel32.GetPriorityClass(handle)
|
||||
else:
|
||||
import resource
|
||||
return resource.getpriority(resource.PRIO_PROCESS, 0)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def set_process_priority(level="above_normal"):
|
||||
"""
|
||||
设置进程优先级
|
||||
|
||||
Args:
|
||||
level: "below_normal" | "normal" | "above_normal" | "high"
|
||||
|
||||
Returns:
|
||||
是否设置成功
|
||||
"""
|
||||
platform = get_platform()
|
||||
|
||||
try:
|
||||
if platform == 'windows':
|
||||
import ctypes
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
priority_map = {
|
||||
"below_normal": 0x00004000,
|
||||
"normal": 0x00000020,
|
||||
"above_normal": 0x00008000,
|
||||
"high": 0x00000080,
|
||||
}
|
||||
priority = priority_map.get(level, 0x00008000)
|
||||
handle = kernel32.GetCurrentProcess()
|
||||
return bool(kernel32.SetPriorityClass(handle, priority))
|
||||
else:
|
||||
nice_map = {
|
||||
"below_normal": 10,
|
||||
"normal": 0,
|
||||
"above_normal": -10,
|
||||
"high": -15,
|
||||
}
|
||||
nice_val = nice_map.get(level, -10)
|
||||
os.nice(nice_val)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"version": 1,
|
||||
"description": "小狸 CLI 插件码注册表 - 通过 /plugin install <插件码> 安装",
|
||||
"plugins": {
|
||||
"dglab": {
|
||||
"name": "DG-Lab 郊狼 WebSocket 控制",
|
||||
"description": "基于 DG-LAB-OPENSOURCE 官方 Socket V2 协议,支持郊狼 3.0 脉冲主机远程控制",
|
||||
"author": "xiaoli-cli-plugins",
|
||||
"version": "1.0.0",
|
||||
"file": "dglab_ws.py",
|
||||
"url": "https://gitee.com/shuiyu1123/xiaoli-cli-plugins/raw/master/dglab_ws/dglab_ws.py",
|
||||
"docs": "https://gitee.com/shuiyu1123/xiaoli-cli-plugins/raw/master/dglab_ws/README.md",
|
||||
"deps": ["websocket-client"],
|
||||
"optional_deps": ["qrcode"],
|
||||
"keywords": ["dg-lab", "dglab", "郊狼", "coyote", "websocket", "脉冲"]
|
||||
},
|
||||
"onebot": {
|
||||
"name": "OneBot v11 QQ 机器人",
|
||||
"description": "让 xiaoli-cli 成为 QQ 机器人,连接 go-cqhttp/Lagrange/LLOneBot 等 OneBot 实现",
|
||||
"author": "xiaoli-cli-plugins",
|
||||
"version": "1.0.0",
|
||||
"file": "onebot.py",
|
||||
"url": "https://gitee.com/shuiyu1123/xiaoli-cli-plugins/raw/master/onebot/onebot.py",
|
||||
"docs": "https://gitee.com/shuiyu1123/xiaoli-cli-plugins/raw/master/onebot/README.md",
|
||||
"deps": ["websocket-client"],
|
||||
"optional_deps": [],
|
||||
"keywords": ["onebot", "qq", "机器人", "bot", "go-cqhttp", "lagrange", "群聊", "私聊"]
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -25,7 +25,7 @@ except ImportError:
|
||||
WEBSOCKET_AVAILABLE = False
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""AI 生图插件 - 调用 ai.2x.nz 接口生成图片"""
|
||||
|
||||
BASE_URL = "https://ai.2x.nz"
|
||||
@@ -587,4 +587,3 @@ class Plugin:
|
||||
|
||||
|
||||
# 兼容 Liugin 类名
|
||||
Liugin = Plugin
|
||||
@@ -10,7 +10,7 @@ import subprocess
|
||||
from typing import Tuple, List
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""工程化工具 - lint/format/test/build + 代码分析"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
@@ -0,0 +1,489 @@
|
||||
"""
|
||||
书签管理器插件 - 管理开发相关链接和资源
|
||||
支持分类、标签、搜索、导入导出,JSON 持久化
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import hashlib
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
class Liugin:
|
||||
"""书签管理器 - 开发资源和链接管理"""
|
||||
|
||||
def __init__(self):
|
||||
self.usage = """书签管理器
|
||||
|
||||
操作:
|
||||
add <URL> -n <名称> [-c 分类] [-t 标签1,标签2] [-d 描述]
|
||||
- 添加书签
|
||||
list [-c 分类] [-t 标签] - 列出书签
|
||||
open <名称或ID> - 获取书签 URL
|
||||
search <关键词> - 搜索书签
|
||||
delete <名称或ID> - 删除书签
|
||||
edit <名称或ID> [-n 名称] [-d 描述] [-c 分类] [-t 标签]
|
||||
- 编辑书签
|
||||
categories - 列出所有分类
|
||||
tags - 列出所有标签
|
||||
export [文件] - 导出书签
|
||||
import <文件> - 导入书签
|
||||
stats - 统计信息
|
||||
recent [-n 数量] - 最近添加的书签
|
||||
check - 检查书签是否可访问
|
||||
favicon <URL> - 获取网站 favicon URL
|
||||
|
||||
示例:
|
||||
bookmark_manager add https://github.com -n GitHub -c 开发 -t 代码,托管 -d 代码托管平台
|
||||
bookmark_manager list -c 开发
|
||||
bookmark_manager search github
|
||||
bookmark_manager open GitHub
|
||||
bookmark_manager categories
|
||||
"""
|
||||
self.cli = None
|
||||
self.bookmarks_file = "bookmarks.json"
|
||||
self.bookmarks = []
|
||||
self._load_bookmarks()
|
||||
|
||||
def set_cli(self, cli):
|
||||
self.cli = cli
|
||||
|
||||
def get_tool_info(self):
|
||||
return {
|
||||
"name": "bookmark_manager",
|
||||
"description": "书签管理器 - 管理开发相关链接和资源,支持分类、标签、搜索",
|
||||
"keywords": ["书签", "链接", "收藏", "bookmark", "网址", "资源", "网站", "URL"],
|
||||
"usage": self.usage
|
||||
}
|
||||
|
||||
def get_mcp_definition(self):
|
||||
return {
|
||||
"name": "bookmark_manager",
|
||||
"description": "书签管理器,支持添加、搜索、获取、删除书签",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"operation": {
|
||||
"type": "string",
|
||||
"enum": ["add", "list", "open", "search", "delete",
|
||||
"edit", "categories", "tags", "export",
|
||||
"import", "stats", "recent", "check"],
|
||||
"description": "操作类型"
|
||||
},
|
||||
"url": {"type": "string", "description": "URL"},
|
||||
"name": {"type": "string", "description": "书签名称"},
|
||||
"category": {"type": "string", "description": "分类"},
|
||||
"tags": {"type": "string", "description": "标签(逗号分隔)"},
|
||||
"description": {"type": "string", "description": "描述"},
|
||||
"keyword": {"type": "string", "description": "搜索关键词"},
|
||||
"file": {"type": "string", "description": "文件路径"},
|
||||
"count": {"type": "integer", "description": "数量"}
|
||||
},
|
||||
"required": ["operation"]
|
||||
}
|
||||
}
|
||||
|
||||
def convert_mcp_args(self, arguments):
|
||||
op = arguments.get("operation", "")
|
||||
name = arguments.get("name", "")
|
||||
return f"{op} {name}".strip()
|
||||
|
||||
def _load_bookmarks(self):
|
||||
try:
|
||||
if os.path.exists(self.bookmarks_file):
|
||||
with open(self.bookmarks_file, 'r', encoding='utf-8') as f:
|
||||
self.bookmarks = json.load(f)
|
||||
except Exception:
|
||||
self.bookmarks = []
|
||||
|
||||
def _save_bookmarks(self):
|
||||
try:
|
||||
with open(self.bookmarks_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.bookmarks, f, ensure_ascii=False, indent=2)
|
||||
return None
|
||||
except Exception as e:
|
||||
return f"保存失败: {e}"
|
||||
|
||||
def _gen_id(self, url: str) -> str:
|
||||
return hashlib.md5(f"{url}{time.time()}".encode()).hexdigest()[:8]
|
||||
|
||||
def handle(self, args: str) -> str:
|
||||
try:
|
||||
parts = self._parse_args(args)
|
||||
if not parts:
|
||||
return self._op_list([])
|
||||
|
||||
operation = parts[0].lower()
|
||||
handlers = {
|
||||
"add": self._op_add,
|
||||
"list": self._op_list,
|
||||
"open": self._op_open,
|
||||
"search": self._op_search,
|
||||
"delete": self._op_delete,
|
||||
"edit": self._op_edit,
|
||||
"categories": self._op_categories,
|
||||
"tags": self._op_tags,
|
||||
"export": self._op_export,
|
||||
"import": self._op_import,
|
||||
"stats": self._op_stats,
|
||||
"recent": self._op_recent,
|
||||
"check": self._op_check,
|
||||
"favicon": self._op_favicon,
|
||||
}
|
||||
|
||||
handler = handlers.get(operation)
|
||||
if not handler:
|
||||
return f"错误:不支持的操作 '{operation}'"
|
||||
return handler(parts[1:])
|
||||
|
||||
except Exception as e:
|
||||
return f"书签管理错误: {str(e)}"
|
||||
|
||||
def _parse_args(self, args: str) -> List[str]:
|
||||
parts, current, in_quotes, qc = [], "", False, None
|
||||
for ch in args:
|
||||
if ch in ('"', "'") and not in_quotes:
|
||||
in_quotes, qc = True, ch
|
||||
elif ch == qc and in_quotes:
|
||||
in_quotes, qc = False, None
|
||||
elif ch == ' ' and not in_quotes:
|
||||
if current:
|
||||
parts.append(current)
|
||||
current = ""
|
||||
else:
|
||||
current += ch
|
||||
if current:
|
||||
parts.append(current)
|
||||
return parts
|
||||
|
||||
def _op_add(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 URL"
|
||||
url = args[0]
|
||||
name, category, tags, desc = "", "", [], ""
|
||||
|
||||
i = 1
|
||||
while i < len(args):
|
||||
if args[i] == "-n" and i + 1 < len(args):
|
||||
name = args[i + 1]; i += 1
|
||||
elif args[i] == "-c" and i + 1 < len(args):
|
||||
category = args[i + 1]; i += 1
|
||||
elif args[i] == "-t" and i + 1 < len(args):
|
||||
tags = [t.strip() for t in args[i + 1].split(",") if t.strip()]; i += 1
|
||||
elif args[i] == "-d" and i + 1 < len(args):
|
||||
desc = args[i + 1]; i += 1
|
||||
i += 1
|
||||
|
||||
if not name:
|
||||
# 从 URL 推断名称
|
||||
from urllib.parse import urlparse
|
||||
parsed = urlparse(url if '://' in url else 'https://' + url)
|
||||
name = parsed.netloc.replace('www.', '').split('.')[0].capitalize()
|
||||
url = parsed.geturl()
|
||||
|
||||
# 检查重复
|
||||
for b in self.bookmarks:
|
||||
if b["url"] == url:
|
||||
return f"错误:URL 已存在 (书签: {b['name']})"
|
||||
|
||||
bookmark = {
|
||||
"id": self._gen_id(url),
|
||||
"name": name,
|
||||
"url": url,
|
||||
"category": category,
|
||||
"tags": tags,
|
||||
"description": desc,
|
||||
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"access_count": 0
|
||||
}
|
||||
self.bookmarks.append(bookmark)
|
||||
err = self._save_bookmarks()
|
||||
if err:
|
||||
return err
|
||||
return f"✅ 已添加书签 '{name}'\n URL: {url}\n 分类: {category or '未分类'}"
|
||||
|
||||
def _op_list(self, args: List[str]) -> str:
|
||||
cat_filter, tag_filter = None, None
|
||||
i = 0
|
||||
while i < len(args):
|
||||
if args[i] == "-c" and i + 1 < len(args):
|
||||
cat_filter = args[i + 1].lower(); i += 1
|
||||
elif args[i] == "-t" and i + 1 < len(args):
|
||||
tag_filter = args[i + 1].lower(); i += 1
|
||||
i += 1
|
||||
|
||||
filtered = self.bookmarks
|
||||
if cat_filter:
|
||||
filtered = [b for b in filtered if b.get("category", "").lower() == cat_filter]
|
||||
if tag_filter:
|
||||
filtered = [b for b in filtered if any(tag_filter in t.lower() for t in b.get("tags", []))]
|
||||
|
||||
if not filtered:
|
||||
return "📭 没有找到书签"
|
||||
|
||||
# 按分类分组
|
||||
grouped = {}
|
||||
for b in filtered:
|
||||
cat = b.get("category", "未分类")
|
||||
grouped.setdefault(cat, []).append(b)
|
||||
|
||||
result = f"\n 书签列表 (共 {len(filtered)} 个)\n"
|
||||
result += "═" * 55 + "\n"
|
||||
|
||||
for cat, items in sorted(grouped.items()):
|
||||
result += f"\n {cat} ({len(items)} 个)\n"
|
||||
result += "─" * 45 + "\n"
|
||||
for b in items:
|
||||
tags = ", ".join(b.get("tags", [])) if b.get("tags") else ""
|
||||
result += f" [{b['id']}] {b['name']}\n"
|
||||
result += f" {b['url']}\n"
|
||||
if tags:
|
||||
result += f" ️ {tags}\n"
|
||||
if b.get("description"):
|
||||
result += f" {b['description'][:50]}\n"
|
||||
result += "\n"
|
||||
|
||||
return result.rstrip()
|
||||
|
||||
def _op_open(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供书签名称或 ID"
|
||||
key = args[0]
|
||||
bookmark = self._find_bookmark(key)
|
||||
if not bookmark:
|
||||
return f"❌ 未找到书签: {key}"
|
||||
|
||||
bookmark["access_count"] = bookmark.get("access_count", 0) + 1
|
||||
self._save_bookmarks()
|
||||
|
||||
return (f" {bookmark['name']}\n"
|
||||
f" URL: {bookmark['url']}\n"
|
||||
f" 访问次数: {bookmark.get('access_count', 0)}")
|
||||
|
||||
def _op_search(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供搜索关键词"
|
||||
keyword = " ".join(args).lower()
|
||||
|
||||
results = []
|
||||
for b in self.bookmarks:
|
||||
searchable = f"{b['name']} {b['url']} {b.get('description', '')} {' '.join(b.get('tags', []))} {b.get('category', '')}".lower()
|
||||
if keyword in searchable:
|
||||
results.append(b)
|
||||
|
||||
if not results:
|
||||
return f" 未找到匹配 '{keyword}' 的书签"
|
||||
|
||||
output = f" 搜索结果 ({len(results)} 个):\n"
|
||||
output += "─" * 50 + "\n"
|
||||
for b in results:
|
||||
output += f" [{b['id']}] {b['name']} - {b['url']}\n"
|
||||
if b.get("description"):
|
||||
output += f" {b['description'][:40]}\n"
|
||||
return output.rstrip()
|
||||
|
||||
def _op_delete(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供书签名称或 ID"
|
||||
key = args[0]
|
||||
for i, b in enumerate(self.bookmarks):
|
||||
if b["name"].lower() == key.lower() or b["id"] == key:
|
||||
removed = self.bookmarks.pop(i)
|
||||
self._save_bookmarks()
|
||||
return f" ️ 已删除书签: {removed['name']} ({removed['url']})"
|
||||
return f"❌ 未找到书签: {key}"
|
||||
|
||||
def _op_edit(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供书签名称或 ID"
|
||||
key = args[0]
|
||||
bookmark = self._find_bookmark(key)
|
||||
if not bookmark:
|
||||
return f"❌ 未找到书签: {key}"
|
||||
|
||||
i = 1
|
||||
while i < len(args):
|
||||
if args[i] == "-n" and i + 1 < len(args):
|
||||
bookmark["name"] = args[i + 1]; i += 1
|
||||
elif args[i] == "-d" and i + 1 < len(args):
|
||||
bookmark["description"] = args[i + 1]; i += 1
|
||||
elif args[i] == "-c" and i + 1 < len(args):
|
||||
bookmark["category"] = args[i + 1]; i += 1
|
||||
elif args[i] == "-t" and i + 1 < len(args):
|
||||
bookmark["tags"] = [t.strip() for t in args[i + 1].split(",") if t.strip()]; i += 1
|
||||
i += 1
|
||||
|
||||
bookmark["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
self._save_bookmarks()
|
||||
return f"✅ 已更新书签: {bookmark['name']}"
|
||||
|
||||
def _op_categories(self, args: List[str]) -> str:
|
||||
cat_count = {}
|
||||
for b in self.bookmarks:
|
||||
cat = b.get("category", "未分类")
|
||||
cat_count[cat] = cat_count.get(cat, 0) + 1
|
||||
|
||||
if not cat_count:
|
||||
return " 暂无分类"
|
||||
|
||||
result = " 分类列表:\n"
|
||||
for cat, count in sorted(cat_count.items(), key=lambda x: -x[1]):
|
||||
bar = "█" * min(count, 20)
|
||||
result += f" {cat:15s} {bar} ({count})\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_tags(self, args: List[str]) -> str:
|
||||
tag_count = {}
|
||||
for b in self.bookmarks:
|
||||
for t in b.get("tags", []):
|
||||
tag_count[t] = tag_count.get(t, 0) + 1
|
||||
|
||||
if not tag_count:
|
||||
return " ️ 暂无标签"
|
||||
|
||||
result = " ️ 标签列表:\n"
|
||||
for tag, count in sorted(tag_count.items(), key=lambda x: -x[1]):
|
||||
result += f" {tag} ({count})\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_export(self, args: List[str]) -> str:
|
||||
output_file = args[0] if args else "bookmarks_export.json"
|
||||
try:
|
||||
with open(output_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.bookmarks, f, ensure_ascii=False, indent=2)
|
||||
return f"✅ 已导出 {len(self.bookmarks)} 个书签到: {output_file}"
|
||||
except Exception as e:
|
||||
return f"❌ 导出失败: {e}"
|
||||
|
||||
def _op_import(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供导入文件路径"
|
||||
file_path = args[0]
|
||||
if not os.path.isfile(file_path):
|
||||
return f"❌ 文件不存在: {file_path}"
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
imported = json.load(f)
|
||||
if not isinstance(imported, list):
|
||||
return "❌ 文件格式错误:需要 JSON 数组"
|
||||
|
||||
existing_urls = {b["url"] for b in self.bookmarks}
|
||||
added = 0
|
||||
for b in imported:
|
||||
if isinstance(b, dict) and b.get("url") and b["url"] not in existing_urls:
|
||||
b["id"] = self._gen_id(b["url"])
|
||||
self.bookmarks.append(b)
|
||||
existing_urls.add(b["url"])
|
||||
added += 1
|
||||
|
||||
self._save_bookmarks()
|
||||
return f"✅ 已导入 {added} 个书签 (跳过 {len(imported) - added} 个重复)"
|
||||
except Exception as e:
|
||||
return f"❌ 导入失败: {e}"
|
||||
|
||||
def _op_stats(self, args: List[str]) -> str:
|
||||
total = len(self.bookmarks)
|
||||
if not total:
|
||||
return "📊 暂无书签"
|
||||
|
||||
cat_count = {}
|
||||
total_access = 0
|
||||
for b in self.bookmarks:
|
||||
cat = b.get("category", "未分类")
|
||||
cat_count[cat] = cat_count.get(cat, 0) + 1
|
||||
total_access += b.get("access_count", 0)
|
||||
|
||||
most_accessed = max(self.bookmarks, key=lambda b: b.get("access_count", 0))
|
||||
|
||||
result = f"📊 书签统计\n"
|
||||
result += "─" * 35 + "\n"
|
||||
result += f" 总数: {total}\n"
|
||||
result += f" 总访问: {total_access} 次\n"
|
||||
result += f" 分类数: {len(cat_count)}\n"
|
||||
result += f" 最常访问: {most_accessed['name']} ({most_accessed.get('access_count', 0)} 次)\n"
|
||||
result += f"\n 分类分布:\n"
|
||||
for cat, count in sorted(cat_count.items(), key=lambda x: -x[1]):
|
||||
result += f" {cat}: {count}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_recent(self, args: List[str]) -> str:
|
||||
count = 5
|
||||
for i, a in enumerate(args):
|
||||
if a == "-n" and i + 1 < len(args):
|
||||
count = int(args[i + 1])
|
||||
|
||||
recent = sorted(self.bookmarks, key=lambda b: b.get("created_at", ""), reverse=True)[:count]
|
||||
if not recent:
|
||||
return "📭 暂无书签"
|
||||
|
||||
result = f"⏰ 最近 {len(recent)} 个书签:\n"
|
||||
for b in recent:
|
||||
result += f" [{b['id']}] {b['name']} - {b.get('created_at', '')}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_check(self, args: List[str]) -> str:
|
||||
import socket
|
||||
from urllib.parse import urlparse
|
||||
|
||||
bookmarks = self.bookmarks
|
||||
if args:
|
||||
# 检查指定书签
|
||||
bookmark = self._find_bookmark(args[0])
|
||||
if not bookmark:
|
||||
return f"❌ 未找到书签: {args[0]}"
|
||||
bookmarks = [bookmark]
|
||||
|
||||
result = f" ️ 书签可访问性检查 ({len(bookmarks)} 个):\n"
|
||||
result += "─" * 50 + "\n"
|
||||
|
||||
for b in bookmarks[:20]:
|
||||
url = b["url"]
|
||||
try:
|
||||
parsed = urlparse(url)
|
||||
host = parsed.netloc or parsed.path.split('/')[0]
|
||||
socket.create_connection((host, 80), timeout=3)
|
||||
result += f" {b['name']} ({host})\n"
|
||||
except (socket.timeout, socket.error, OSError):
|
||||
result += f" {b['name']} ({host}) - 不可达\n"
|
||||
|
||||
if len(bookmarks) > 20:
|
||||
result += f" ... 仅检查前 20 个\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_favicon(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 URL"
|
||||
url = args[0]
|
||||
if '://' not in url:
|
||||
url = 'https://' + url
|
||||
|
||||
from urllib.parse import urlparse
|
||||
parsed = urlparse(url)
|
||||
base = f"{parsed.scheme}://{parsed.netloc}"
|
||||
favicon_url = f"{base}/favicon.ico"
|
||||
|
||||
return (f" Favicon:\n"
|
||||
f" 网站: {base}\n"
|
||||
f" Favicon: {favicon_url}\n"
|
||||
f" Google: https://www.google.com/s2/favicons?domain={parsed.netloc}&sz=64")
|
||||
|
||||
def _find_bookmark(self, key: str) -> Optional[dict]:
|
||||
for b in self.bookmarks:
|
||||
if b["name"].lower() == key.lower() or b["id"] == key:
|
||||
return b
|
||||
return None
|
||||
|
||||
|
||||
def test_plugin():
|
||||
plugin = Liugin()
|
||||
print("书签管理器测试:")
|
||||
print(plugin.handle('add https://github.com -n GitHub -c 开发 -t 代码,托管 -d 代码托管平台'))
|
||||
print(plugin.handle('add https://stackoverflow.com -n StackOverflow -c 学习 -t 问答,编程'))
|
||||
print(plugin.handle('list'))
|
||||
print(plugin.handle('search github'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_plugin()
|
||||
@@ -9,7 +9,7 @@ import time
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""浏览器自动化 - 网页操控、表单填写、数据抓取、截图"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
+19
-13
@@ -5,9 +5,14 @@ import os
|
||||
import re
|
||||
import subprocess
|
||||
import platform
|
||||
import sys
|
||||
|
||||
# 添加项目根目录到 path
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from xcli_core.tool_result import ToolResult, ErrorCode
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""Shell 命令执行器 - 跨平台命令行工具"""
|
||||
|
||||
def __init__(self):
|
||||
@@ -68,18 +73,18 @@ class Plugin:
|
||||
':(){ :|:& };:', 'chmod -R 777 /', 'mv / ', 'wget http',
|
||||
}
|
||||
|
||||
def handle(self, args: str) -> str:
|
||||
def handle(self, args: str):
|
||||
try:
|
||||
parts = args.strip().split(maxsplit=1)
|
||||
if not parts:
|
||||
return "错误:请提供操作。可用: run <命令>"
|
||||
return ToolResult.missing("操作。可用: run <命令>")
|
||||
|
||||
operation = parts[0].lower()
|
||||
if operation != "run":
|
||||
return f"错误:不支持的操作 '{operation}'。可用: run"
|
||||
return ToolResult.fail(f"不支持的操作 '{operation}'。可用: run", ErrorCode.UNSUPPORTED_OP)
|
||||
|
||||
if len(parts) < 2:
|
||||
return "错误:请提供要执行的命令"
|
||||
return ToolResult.missing("要执行的命令")
|
||||
|
||||
rest = parts[1]
|
||||
|
||||
@@ -91,21 +96,20 @@ class Plugin:
|
||||
rest = re.sub(r'--timeout\s+\d+', '', rest).strip()
|
||||
|
||||
if not rest:
|
||||
return "错误:请提供要执行的命令"
|
||||
return ToolResult.missing("要执行的命令")
|
||||
|
||||
# 安全检查
|
||||
for blocked in self.BLOCKED:
|
||||
if blocked in rest:
|
||||
return f" 安全拦截: 检测到危险命令模式 '{blocked}'"
|
||||
return ToolResult.fail(f"安全拦截: 检测到危险命令模式 '{blocked}'", ErrorCode.PERMISSION_DENIED)
|
||||
|
||||
return self._execute(rest, timeout)
|
||||
|
||||
except Exception as e:
|
||||
return f"命令执行错误: {str(e)}"
|
||||
return ToolResult.fail(str(e), ErrorCode.INTERNAL_ERROR, tool_name="cmd_executor")
|
||||
|
||||
def _execute(self, command: str, timeout: int) -> str:
|
||||
def _execute(self, command: str, timeout: int):
|
||||
try:
|
||||
# Windows 下用 shell=True,Unix 下直接执行
|
||||
use_shell = platform.system() == 'Windows'
|
||||
|
||||
result = subprocess.run(
|
||||
@@ -136,9 +140,11 @@ class Plugin:
|
||||
if len(output) > 5000:
|
||||
output = output[:5000] + f"\n... (截断,超时: {timeout}s)"
|
||||
|
||||
return output.strip()
|
||||
if rc != 0:
|
||||
return ToolResult.fail(output.strip(), ErrorCode.EXEC_FAILED, tool_name="cmd_executor")
|
||||
return ToolResult.ok(output.strip(), tool_name="cmd_executor")
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
return f" 命令超时 ({timeout}s): {command}"
|
||||
return ToolResult.timeout(timeout, tool_name="cmd_executor")
|
||||
except Exception as e:
|
||||
return f"执行失败: {str(e)}"
|
||||
return ToolResult.fail(str(e), ErrorCode.EXEC_FAILED, tool_name="cmd_executor")
|
||||
+146
-32
@@ -16,7 +16,7 @@ import threading
|
||||
from typing import Optional, Tuple, List
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""代码编辑与搜索 - 精准编辑 + 代码理解"""
|
||||
|
||||
def __init__(self):
|
||||
@@ -37,9 +37,10 @@ class Plugin:
|
||||
编辑操作后自动执行,无需手动调用
|
||||
|
||||
Diff 弹窗:
|
||||
diff_popup [on|off] - 开关 diff 弹窗 (别名: popup)
|
||||
编辑操作后弹出新窗口显示修改前后对比(默认开启)
|
||||
on/开/enable → 开启 | off/关/disable → 关闭 | 无参数 → 切换
|
||||
diff_popup [on|off|popup|inline] - 开关/切换 diff 显示模式 (别名: popup)
|
||||
popup/弹窗 → 弹窗模式(新终端窗口)
|
||||
inline/ssh → 主终端内显示(SSH 兼容)
|
||||
on/开 → 开启 | off/关 → 关闭 | 无参数 → 切换
|
||||
|
||||
查看操作:
|
||||
read_range <文件> <起始行> [结束行] - 读取指定行范围
|
||||
@@ -59,7 +60,8 @@ Diff 弹窗:
|
||||
stats <目录> - 代码统计
|
||||
"""
|
||||
self.cli = None
|
||||
self.diff_popup_enabled = True # diff 弹窗开关,默认开启
|
||||
self.diff_popup_enabled = True # diff 弹窗开关
|
||||
self.diff_popup_mode = None # None=启动时询问, True=弹窗, False=主终端内显示
|
||||
|
||||
def set_cli(self, cli):
|
||||
self.cli = cli
|
||||
@@ -611,20 +613,26 @@ else:
|
||||
|
||||
def _spawn_diff_popup(self, filepath: str, before: str, after: str):
|
||||
"""
|
||||
在新终端窗口中显示修改前后的对比。
|
||||
使用独立线程避免阻塞主流程。
|
||||
显示修改前后的对比。
|
||||
根据 diff_popup_mode 决定弹窗还是主终端内显示。
|
||||
"""
|
||||
if not self.diff_popup_enabled:
|
||||
return
|
||||
if before == after:
|
||||
return # 内容无变化,不弹窗
|
||||
|
||||
thread = threading.Thread(
|
||||
target=self._do_spawn_diff_popup,
|
||||
args=(filepath, before, after),
|
||||
daemon=True
|
||||
)
|
||||
thread.start()
|
||||
# 启动时未选择模式,默认为主终端内显示(SSH 兼容)
|
||||
popup = self.diff_popup_mode if self.diff_popup_mode is not None else False
|
||||
|
||||
if popup:
|
||||
thread = threading.Thread(
|
||||
target=self._do_spawn_diff_popup,
|
||||
args=(filepath, before, after),
|
||||
daemon=True
|
||||
)
|
||||
thread.start()
|
||||
else:
|
||||
self._show_inline_diff(filepath, before, after)
|
||||
|
||||
def _do_spawn_diff_popup(self, filepath: str, before: str, after: str):
|
||||
"""实际执行弹窗创建(在子线程中运行)"""
|
||||
@@ -642,7 +650,6 @@ else:
|
||||
tofile=f"修改后: {basename}",
|
||||
lineterm=''
|
||||
))
|
||||
diff_text = '\n'.join(diff_lines)
|
||||
|
||||
# 统计变更
|
||||
added = sum(1 for l in diff_lines if l.startswith('+') and not l.startswith('+++'))
|
||||
@@ -655,7 +662,7 @@ else:
|
||||
display = self._build_diff_display(
|
||||
basename, filepath,
|
||||
before_numbered, after_numbered,
|
||||
diff_text, added, removed,
|
||||
diff_lines, added, removed,
|
||||
before_count, after_count
|
||||
)
|
||||
|
||||
@@ -670,38 +677,64 @@ else:
|
||||
|
||||
def _build_diff_display(self, basename, filepath,
|
||||
before_numbered, after_numbered,
|
||||
diff_text, added, removed,
|
||||
diff_lines, added, removed,
|
||||
before_count, after_count):
|
||||
"""构建完整的 diff 显示文本"""
|
||||
"""构建完整的 diff 显示文本(带 ANSI 颜色)"""
|
||||
sep = '═' * 70
|
||||
thin_sep = '─' * 70
|
||||
|
||||
# ANSI 颜色
|
||||
R = '\033[31m' # 红字
|
||||
G = '\033[32m' # 绿字
|
||||
C = '\033[36m' # 青
|
||||
B = '\033[1m' # 粗体
|
||||
D = '\033[2m' # 暗
|
||||
W = '\033[37m' # 白
|
||||
N = '\033[0m' # 重置
|
||||
RBG = '\033[41m\033[97m' # 红底白字
|
||||
GBG = '\033[42m\033[97m' # 绿底白字
|
||||
|
||||
# 构建带颜色的 diff
|
||||
diff_colored_lines = []
|
||||
for line in diff_lines:
|
||||
if line.startswith('@@'):
|
||||
diff_colored_lines.append(f"{C}{line}{N}")
|
||||
elif line.startswith('+++') or line.startswith('---'):
|
||||
diff_colored_lines.append(f"{B}{line}{N}")
|
||||
elif line.startswith('+'):
|
||||
diff_colored_lines.append(f"{GBG} {line} {N}")
|
||||
elif line.startswith('-'):
|
||||
diff_colored_lines.append(f"{RBG} {line} {N}")
|
||||
else:
|
||||
diff_colored_lines.append(line)
|
||||
diff_colored = '\n'.join(diff_colored_lines) if diff_colored_lines else ' (无差异)'
|
||||
|
||||
display = f"""
|
||||
{sep}
|
||||
文件变更对比: {basename}
|
||||
路径: {filepath}
|
||||
{sep}
|
||||
{W}{sep}{N}
|
||||
{B} 文件变更对比: {basename}{N}
|
||||
{D} 路径: {filepath}{N}
|
||||
{W}{sep}{N}
|
||||
|
||||
统计: {before_count} 行 → {after_count} 行 | +{added} 新增 -{removed} 删除
|
||||
统计: {before_count} 行 → {after_count} 行 | {G}+{added} 新增{N} {R}-{removed} 删除{N}
|
||||
|
||||
{thin_sep}
|
||||
{W}{thin_sep}{N}
|
||||
修改前 ({before_count} 行):
|
||||
{thin_sep}
|
||||
{W}{thin_sep}{N}
|
||||
{before_numbered}
|
||||
|
||||
{thin_sep}
|
||||
{W}{thin_sep}{N}
|
||||
修改后 ({after_count} 行):
|
||||
{thin_sep}
|
||||
{W}{thin_sep}{N}
|
||||
{after_numbered}
|
||||
|
||||
{thin_sep}
|
||||
{W}{thin_sep}{N}
|
||||
差异 (Diff):
|
||||
{thin_sep}
|
||||
{diff_text if diff_text else ' (无差异)'}
|
||||
{W}{thin_sep}{N}
|
||||
{diff_colored}
|
||||
|
||||
{sep}
|
||||
{W}{sep}{N}
|
||||
按任意键关闭此窗口...
|
||||
{sep}
|
||||
{W}{sep}{N}
|
||||
"""
|
||||
return display
|
||||
|
||||
@@ -775,8 +808,81 @@ else:
|
||||
except Exception as e:
|
||||
print(f"[Diff 弹窗] 创建失败: {e}")
|
||||
|
||||
def _show_inline_diff(self, filepath: str, before: str, after: str):
|
||||
"""在主终端内显示 diff(SSH 兼容模式),带颜色"""
|
||||
basename = os.path.basename(filepath)
|
||||
is_tui = self.cli and getattr(self.cli, 'tui_output_callback', None)
|
||||
|
||||
# 生成 diff
|
||||
diff_lines = list(difflib.unified_diff(
|
||||
before.splitlines(keepends=True),
|
||||
after.splitlines(keepends=True),
|
||||
fromfile=f"修改前: {basename}",
|
||||
tofile=f"修改后: {basename}",
|
||||
lineterm=''
|
||||
))
|
||||
|
||||
# 统计
|
||||
added = sum(1 for l in diff_lines if l.startswith('+') and not l.startswith('+++'))
|
||||
removed = sum(1 for l in diff_lines if l.startswith('-') and not l.startswith('---'))
|
||||
before_count = len(before.splitlines())
|
||||
after_count = len(after.splitlines())
|
||||
|
||||
sep = '═' * 60
|
||||
thin = '─' * 60
|
||||
|
||||
if is_tui:
|
||||
# TUI 模式:使用 Rich 标记
|
||||
output = self.cli._output
|
||||
output(f"[dim]{sep}[/]")
|
||||
output(f"[bold cyan] 文件变更: {basename}[/] [dim]{filepath}[/]")
|
||||
output(f"[dim]{sep}[/]")
|
||||
output(f"[bold] 统计: {before_count} 行 → {after_count} 行 | "
|
||||
f"[green]+{added} 新增[/] [red]-{removed} 删除[/]")
|
||||
output(f"[dim]{thin}[/]")
|
||||
|
||||
for line in diff_lines:
|
||||
if line.startswith('@@'):
|
||||
output(f"[cyan]{line}[/]")
|
||||
elif line.startswith('+++') or line.startswith('---'):
|
||||
output(f"[bold]{line}[/]")
|
||||
elif line.startswith('+'):
|
||||
output(f"[white on green] {line} [/]")
|
||||
elif line.startswith('-'):
|
||||
output(f"[white on red] {line} [/]")
|
||||
else:
|
||||
output(line)
|
||||
|
||||
output(f"[dim]{sep}[/]")
|
||||
else:
|
||||
# CLI 模式:使用 ANSI 颜色
|
||||
from colorama import Fore, Style
|
||||
_out = print
|
||||
|
||||
_out(f"{Fore.WHITE}{sep}{Style.RESET_ALL}")
|
||||
_out(f"{Fore.CYAN} 文件变更: {basename}{Style.RESET_ALL} {filepath}")
|
||||
_out(f"{Fore.WHITE}{sep}{Style.RESET_ALL}")
|
||||
_out(f" 统计: {before_count} 行 → {after_count} 行 | "
|
||||
f"{Fore.GREEN}+{added} 新增{Style.RESET_ALL} "
|
||||
f"{Fore.RED}-{removed} 删除{Style.RESET_ALL}")
|
||||
_out(f"{Fore.WHITE}{thin}{Style.RESET_ALL}")
|
||||
|
||||
for line in diff_lines:
|
||||
if line.startswith('@@'):
|
||||
_out(f"{Fore.CYAN}{line}{Style.RESET_ALL}")
|
||||
elif line.startswith('+++') or line.startswith('---'):
|
||||
_out(f"{Style.BRIGHT}{line}{Style.RESET_ALL}")
|
||||
elif line.startswith('+'):
|
||||
_out(f"\033[42m\033[97m {line} {Style.RESET_ALL}")
|
||||
elif line.startswith('-'):
|
||||
_out(f"\033[41m\033[97m {line} {Style.RESET_ALL}")
|
||||
else:
|
||||
_out(line)
|
||||
|
||||
_out(f"{Fore.WHITE}{sep}{Style.RESET_ALL}")
|
||||
|
||||
def _op_diff_popup(self, path: str, rest: str) -> str:
|
||||
"""切换 diff 弹窗开关"""
|
||||
"""切换 diff 弹窗开关 / 设置显示模式"""
|
||||
arg = (path + ' ' + rest).strip().lower()
|
||||
if arg in ('on', '开', '开启', 'enable', '1', 'true'):
|
||||
self.diff_popup_enabled = True
|
||||
@@ -784,6 +890,14 @@ else:
|
||||
elif arg in ('off', '关', '关闭', 'disable', '0', 'false'):
|
||||
self.diff_popup_enabled = False
|
||||
return " Diff 弹窗已关闭"
|
||||
elif arg in ('popup', '弹窗', '弹窗模式'):
|
||||
self.diff_popup_enabled = True
|
||||
self.diff_popup_mode = True
|
||||
return " Diff 模式: 弹窗(新终端窗口)"
|
||||
elif arg in ('inline', '内联', 'ssh', '终端', '主终端'):
|
||||
self.diff_popup_enabled = True
|
||||
self.diff_popup_mode = False
|
||||
return " Diff 模式: 主终端内显示(SSH 兼容)"
|
||||
else:
|
||||
# 无参数:切换状态
|
||||
self.diff_popup_enabled = not self.diff_popup_enabled
|
||||
|
||||
@@ -0,0 +1,459 @@
|
||||
"""
|
||||
颜色工具插件 - 颜色格式转换、调色板、对比度检查
|
||||
支持 HEX/RGB/HSL/HSV/CMYK 格式互转,WCAG 对比度检查
|
||||
"""
|
||||
|
||||
import re
|
||||
import colorsys
|
||||
from typing import List, Tuple
|
||||
|
||||
|
||||
class Liugin:
|
||||
"""颜色工具 - 格式转换、调色板、对比度检查"""
|
||||
|
||||
def __init__(self):
|
||||
self.usage = """颜色工具
|
||||
|
||||
操作:
|
||||
convert <颜色> - 自动检测并转换为所有格式
|
||||
hex <RGB或HSL> - 转换为 HEX
|
||||
rgb <HEX或HSL> - 转换为 RGB
|
||||
hsl <HEX或RGB> - 转换为 HSL
|
||||
hsv <HEX或RGB> - 转换为 HSV
|
||||
cmyk <HEX或RGB> - 转换为 CMYK
|
||||
contrast <颜色1> <颜色2> - WCAG 对比度检查
|
||||
palette <颜色> [-n 数量] - 生成调色板(明暗变体)
|
||||
complementary <颜色> - 互补色
|
||||
analogous <颜色> - 类似色
|
||||
triadic <颜色> - 三色组
|
||||
random [-n 数量] - 随机颜色
|
||||
mix <颜色1> <颜色2> [-w 权重] - 混合两种颜色
|
||||
blind <颜色> <类型> - 色盲模拟
|
||||
named - 列出 CSS 命名颜色
|
||||
|
||||
颜色格式:
|
||||
HEX: #FF6600, #F60, FF6600
|
||||
RGB: rgb(255,102,0) 或 255,102,0
|
||||
HSL: hsl(24,100%,50%)
|
||||
HSV: hsv(24,100%,100%)
|
||||
CMYK: cmyk(0,60,100,0)
|
||||
|
||||
示例:
|
||||
color_picker convert "#3498db"
|
||||
color_picker rgb "hsl(207,62%,54%)"
|
||||
color_picker contrast "#FFFFFF" "#000000"
|
||||
color_picker palette "#e74c3c" -n 9
|
||||
color_picker complementary "#3498db"
|
||||
"""
|
||||
self.cli = None
|
||||
|
||||
def set_cli(self, cli):
|
||||
self.cli = cli
|
||||
|
||||
def get_tool_info(self):
|
||||
return {
|
||||
"name": "color_picker",
|
||||
"description": "颜色工具 - HEX/RGB/HSL/HSV/CMYK 转换、对比度检查、调色板生成",
|
||||
"keywords": ["颜色", "color", "HEX", "RGB", "HSL", "调色板", "对比度", "色盲", "转换"],
|
||||
"usage": self.usage
|
||||
}
|
||||
|
||||
def get_mcp_definition(self):
|
||||
return {
|
||||
"name": "color_picker",
|
||||
"description": "颜色工具,支持格式转换、对比度检查、调色板生成",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"operation": {
|
||||
"type": "string",
|
||||
"enum": ["convert", "hex", "rgb", "hsl", "hsv", "cmyk",
|
||||
"contrast", "palette", "complementary", "analogous",
|
||||
"triadic", "random", "mix", "blind"],
|
||||
"description": "操作类型"
|
||||
},
|
||||
"color": {"type": "string", "description": "颜色值"},
|
||||
"color2": {"type": "string", "description": "第二个颜色值"},
|
||||
"count": {"type": "integer", "description": "数量"},
|
||||
"weight": {"type": "number", "description": "混合权重 (0-1)"},
|
||||
"blind_type": {"type": "string", "description": "色盲类型"}
|
||||
},
|
||||
"required": ["operation"]
|
||||
}
|
||||
}
|
||||
|
||||
def convert_mcp_args(self, arguments):
|
||||
op = arguments.get("operation", "")
|
||||
color = arguments.get("color", "")
|
||||
return f"{op} {color}".strip()
|
||||
|
||||
def handle(self, args: str) -> str:
|
||||
try:
|
||||
parts = self._parse_args(args)
|
||||
if not parts:
|
||||
return self._op_random(["1"])
|
||||
|
||||
operation = parts[0].lower()
|
||||
handlers = {
|
||||
"convert": self._op_convert,
|
||||
"hex": self._op_hex,
|
||||
"rgb": self._op_rgb,
|
||||
"hsl": self._op_hsl,
|
||||
"hsv": self._op_hsv,
|
||||
"cmyk": self._op_cmyk,
|
||||
"contrast": self._op_contrast,
|
||||
"palette": self._op_palette,
|
||||
"complementary": self._op_complementary,
|
||||
"analogous": self._op_analogous,
|
||||
"triadic": self._op_triadic,
|
||||
"random": self._op_random,
|
||||
"mix": self._op_mix,
|
||||
"blind": self._op_blind,
|
||||
}
|
||||
|
||||
handler = handlers.get(operation)
|
||||
if not handler:
|
||||
return f"错误:不支持的操作 '{operation}'"
|
||||
return handler(parts[1:])
|
||||
|
||||
except Exception as e:
|
||||
return f"颜色工具错误: {str(e)}"
|
||||
|
||||
def _parse_args(self, args: str) -> List[str]:
|
||||
parts, current, in_quotes, qc = [], "", False, None
|
||||
for ch in args:
|
||||
if ch in ('"', "'") and not in_quotes:
|
||||
in_quotes, qc = True, ch
|
||||
elif ch == qc and in_quotes:
|
||||
in_quotes, qc = False, None
|
||||
elif ch == ' ' and not in_quotes:
|
||||
if current:
|
||||
parts.append(current)
|
||||
current = ""
|
||||
else:
|
||||
current += ch
|
||||
if current:
|
||||
parts.append(current)
|
||||
return parts
|
||||
|
||||
def _parse_opts(self, args: List[str]) -> dict:
|
||||
opts = {}
|
||||
i = 0
|
||||
while i < len(args):
|
||||
if args[i].startswith("-") and i + 1 < len(args):
|
||||
key = args[i].lstrip("-")
|
||||
opts[key] = args[i + 1]
|
||||
i += 1
|
||||
i += 1
|
||||
return opts
|
||||
|
||||
def _parse_color(self, color_str: str) -> Tuple[int, int, int]:
|
||||
"""解析颜色字符串为 RGB (0-255)"""
|
||||
s = color_str.strip().lower()
|
||||
|
||||
# HEX
|
||||
hex_match = re.match(r'^#?([0-9a-f]{3,8})$', s)
|
||||
if hex_match:
|
||||
h = hex_match.group(1)
|
||||
if len(h) == 3:
|
||||
h = h[0]*2 + h[1]*2 + h[2]*2
|
||||
elif len(h) == 8:
|
||||
h = h[:6] # ignore alpha
|
||||
if len(h) == 6:
|
||||
return (int(h[0:2], 16), int(h[2:4], 16), int(h[4:6], 16))
|
||||
|
||||
# RGB
|
||||
rgb_match = re.match(r'rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)', s)
|
||||
if rgb_match:
|
||||
return (int(rgb_match.group(1)), int(rgb_match.group(2)), int(rgb_match.group(3)))
|
||||
|
||||
# plain r,g,b
|
||||
plain_match = re.match(r'^(\d+)\s*,\s*(\d+)\s*,\s*(\d+)$', s)
|
||||
if plain_match:
|
||||
return (int(plain_match.group(1)), int(plain_match.group(2)), int(plain_match.group(3)))
|
||||
|
||||
# HSL
|
||||
hsl_match = re.match(r'hsl\s*\(\s*(\d+)\s*,\s*(\d+)%,\s*(\d+)%\s*\)', s)
|
||||
if hsl_match:
|
||||
h = int(hsl_match.group(1)) / 360
|
||||
sl = int(hsl_match.group(2)) / 100
|
||||
l = int(hsl_match.group(3)) / 100
|
||||
r, g, b = colorsys.hls_to_rgb(h, l, sl)
|
||||
return (int(r * 255), int(g * 255), int(b * 255))
|
||||
|
||||
# HSV/HSB
|
||||
hsv_match = re.match(r'hsv\s*\(\s*(\d+)\s*,\s*(\d+)%,\s*(\d+)%\s*\)', s)
|
||||
if hsv_match:
|
||||
h = int(hsv_match.group(1)) / 360
|
||||
sv = int(hsv_match.group(2)) / 100
|
||||
v = int(hsv_match.group(3)) / 100
|
||||
r, g, b = colorsys.hsv_to_rgb(h, sv, v)
|
||||
return (int(r * 255), int(g * 255), int(b * 255))
|
||||
|
||||
# CMYK
|
||||
cmyk_match = re.match(r'cmyk\s*\(\s*(\d+)%?\s*,\s*(\d+)%?\s*,\s*(\d+)%?\s*,\s*(\d+)%?\s*\)', s)
|
||||
if cmyk_match:
|
||||
c = int(cmyk_match.group(1)) / 100
|
||||
m = int(cmyk_match.group(2)) / 100
|
||||
y = int(cmyk_match.group(3)) / 100
|
||||
k = int(cmyk_match.group(4)) / 100
|
||||
r = int(255 * (1 - c) * (1 - k))
|
||||
g = int(255 * (1 - m) * (1 - k))
|
||||
b = int(255 * (1 - y) * (1 - k))
|
||||
return (r, g, b)
|
||||
|
||||
raise ValueError(f"无法解析颜色: {color_str}")
|
||||
|
||||
def _rgb_to_hex(self, r: int, g: int, b: int) -> str:
|
||||
return f"#{r:02X}{g:02X}{b:02X}"
|
||||
|
||||
def _rgb_to_hsl(self, r: int, g: int, b: int) -> Tuple[int, int, int]:
|
||||
h, l, s = colorsys.rgb_to_hls(r / 255, g / 255, b / 255)
|
||||
return (int(h * 360), int(s * 100), int(l * 100))
|
||||
|
||||
def _rgb_to_hsv(self, r: int, g: int, b: int) -> Tuple[int, int, int]:
|
||||
h, s, v = colorsys.rgb_to_hsv(r / 255, g / 255, b / 255)
|
||||
return (int(h * 360), int(s * 100), int(v * 100))
|
||||
|
||||
def _rgb_to_cmyk(self, r: int, g: int, b: int) -> Tuple[int, int, int, int]:
|
||||
if r == g == b == 0:
|
||||
return (0, 0, 0, 100)
|
||||
c = 1 - r / 255
|
||||
m = 1 - g / 255
|
||||
y = 1 - b / 255
|
||||
k = min(c, m, y)
|
||||
c = int((c - k) / (1 - k) * 100)
|
||||
m = int((m - k) / (1 - k) * 100)
|
||||
y = int((y - k) / (1 - k) * 100)
|
||||
k = int(k * 100)
|
||||
return (c, m, y, k)
|
||||
|
||||
def _luminance(self, r: int, g: int, b: int) -> float:
|
||||
"""计算相对亮度 (WCAG 2.0)"""
|
||||
def linearize(c):
|
||||
c = c / 255
|
||||
return c / 12.92 if c <= 0.03928 else ((c + 0.055) / 1.055) ** 2.4
|
||||
return 0.2126 * linearize(r) + 0.7152 * linearize(g) + 0.0722 * linearize(b)
|
||||
|
||||
def _format_color_block(self, r: int, g: int, b: int) -> str:
|
||||
"""生成颜色预览块"""
|
||||
return f" ■ RGB({r}, {g}, {b})"
|
||||
|
||||
def _op_convert(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供颜色值"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
h, s, l = self._rgb_to_hsl(r, g, b)
|
||||
hh, sv, vv = self._rgb_to_hsv(r, g, b)
|
||||
c, m, y, k = self._rgb_to_cmyk(r, g, b)
|
||||
|
||||
result = f" 颜色转换: {args[0]}\n"
|
||||
result += self._format_color_block(r, g, b) + "\n"
|
||||
result += "─" * 35 + "\n"
|
||||
result += f" HEX: {self._rgb_to_hex(r, g, b)}\n"
|
||||
result += f" RGB: rgb({r}, {g}, {b})\n"
|
||||
result += f" HSL: hsl({h}, {s}%, {l}%)\n"
|
||||
result += f" HSV: hsv({hh}, {sv}%, {vv}%)\n"
|
||||
result += f" CMYK: cmyk({c}%, {m}%, {y}%, {k}%)\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_hex(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供颜色值"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
return f" HEX: {self._rgb_to_hex(r, g, b)}"
|
||||
|
||||
def _op_rgb(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供颜色值"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
return f" RGB: rgb({r}, {g}, {b})"
|
||||
|
||||
def _op_hsl(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供颜色值"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
h, s, l = self._rgb_to_hsl(r, g, b)
|
||||
return f" HSL: hsl({h}, {s}%, {l}%)"
|
||||
|
||||
def _op_hsv(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供颜色值"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
h, s, v = self._rgb_to_hsv(r, g, b)
|
||||
return f" HSV: hsv({h}, {s}%, {v}%)"
|
||||
|
||||
def _op_cmyk(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供颜色值"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
c, m, y, k = self._rgb_to_cmyk(r, g, b)
|
||||
return f" CMYK: cmyk({c}%, {m}%, {y}%, {k}%)"
|
||||
|
||||
def _op_contrast(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:请提供两种颜色"
|
||||
r1, g1, b1 = self._parse_color(args[0])
|
||||
r2, g2, b2 = self._parse_color(args[1])
|
||||
|
||||
l1 = self._luminance(r1, g1, b1)
|
||||
l2 = self._luminance(r2, g2, b2)
|
||||
lighter = max(l1, l2)
|
||||
darker = min(l1, l2)
|
||||
ratio = (lighter + 0.05) / (darker + 0.05)
|
||||
|
||||
aa_large = ratio >= 3
|
||||
aa_normal = ratio >= 4.5
|
||||
aaa_large = ratio >= 4.5
|
||||
aaa_normal = ratio >= 7
|
||||
|
||||
result = f" WCAG 对比度检查\n"
|
||||
result += "─" * 40 + "\n"
|
||||
result += f" 颜色1: {self._rgb_to_hex(r1, g1, b1)} {self._format_color_block(r1, g1, b1)}\n"
|
||||
result += f" 颜色2: {self._rgb_to_hex(r2, g2, b2)} {self._format_color_block(r2, g2, b1)}\n"
|
||||
result += f" 对比度: {ratio:.2f}:1\n"
|
||||
result += "─" * 40 + "\n"
|
||||
result += f" {' ' if aa_normal else '❌'} AA 正文 (≥4.5:1)\n"
|
||||
result += f" {' ' if aa_large else '❌'} AA 大字 (≥3:1)\n"
|
||||
result += f" {' ' if aaa_normal else '❌'} AAA 正文 (≥7:1)\n"
|
||||
result += f" {' ' if aaa_large else '❌'} AAA 大字 (≥4.5:1)\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_palette(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供基础颜色"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
opts = self._parse_opts(args[1:])
|
||||
count = int(opts.get("n", 9))
|
||||
count = max(3, min(count, 15))
|
||||
|
||||
h, l, s = colorsys.rgb_to_hls(r / 255, g / 255, b / 255)
|
||||
|
||||
result = f" 调色板 (基于 {args[0]}):\n"
|
||||
result += "─" * 50 + "\n"
|
||||
|
||||
steps = count
|
||||
for i in range(steps):
|
||||
# 从暗到亮
|
||||
new_l = 0.1 + (0.8 * i / (steps - 1))
|
||||
nr, ng, nb = colorsys.hls_to_rgb(h, new_l, s)
|
||||
nr, ng, nb = int(nr * 255), int(ng * 255), int(nb * 255)
|
||||
hex_val = self._rgb_to_hex(nr, ng, nb)
|
||||
bar = "█" * int(new_l * 20)
|
||||
result += f" {hex_val} {bar} L:{int(new_l*100)}%\n"
|
||||
|
||||
return result.rstrip()
|
||||
|
||||
def _op_complementary(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供颜色"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
h, l, s = colorsys.rgb_to_hls(r / 255, g / 255, b / 255)
|
||||
# 互补色: 色相旋转180°
|
||||
comp_h = (h + 0.5) % 1.0
|
||||
cr, cg, cb = colorsys.hls_to_rgb(comp_h, l, s)
|
||||
cr, cg, cb = int(cr * 255), int(cg * 255), int(cb * 255)
|
||||
|
||||
result = f" 互补色:\n"
|
||||
result += f" 原色: {self._rgb_to_hex(r, g, b)}\n"
|
||||
result += f" 互补: {self._rgb_to_hex(cr, cg, cb)}\n"
|
||||
return result
|
||||
|
||||
def _op_analogous(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供颜色"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
h, l, s = colorsys.rgb_to_hls(r / 255, g / 255, b / 255)
|
||||
|
||||
result = f" 类似色:\n"
|
||||
for offset in [-30, -15, 0, 15, 30]:
|
||||
ah = (h + offset / 360) % 1.0
|
||||
ar, ag, ab = colorsys.hls_to_rgb(ah, l, s)
|
||||
ar, ag, ab = int(ar * 255), int(ag * 255), int(ab * 255)
|
||||
marker = " ◀" if offset == 0 else ""
|
||||
result += f" {self._rgb_to_hex(ar, ag, ab)} ({offset:+d}°){marker}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_triadic(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供颜色"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
h, l, s = colorsys.rgb_to_hls(r / 255, g / 255, b / 255)
|
||||
|
||||
result = f" 三色组:\n"
|
||||
for i, offset in enumerate([0, 120, 240]):
|
||||
th = (h + offset / 360) % 1.0
|
||||
tr, tg, tb = colorsys.hls_to_rgb(th, l, s)
|
||||
tr, tg, tb = int(tr * 255), int(tg * 255), int(tb * 255)
|
||||
result += f" {i+1}. {self._rgb_to_hex(tr, tg, tb)} ({offset}°)\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_random(self, args: List[str]) -> str:
|
||||
opts = self._parse_opts(args)
|
||||
count = int(opts.get("n", 5))
|
||||
count = min(count, 20)
|
||||
|
||||
result = f" 随机颜色 ({count} 个):\n"
|
||||
for _ in range(count):
|
||||
r, g, b = __import__('random').randint(0, 255), __import__('random').randint(0, 255), __import__('random').randint(0, 255)
|
||||
result += f" {self._rgb_to_hex(r, g, b)} rgb({r}, {g}, {b})\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_mix(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:请提供两种颜色"
|
||||
r1, g1, b1 = self._parse_color(args[0])
|
||||
r2, g2, b2 = self._parse_color(args[1])
|
||||
opts = self._parse_opts(args[2:])
|
||||
w = float(opts.get("w", 0.5))
|
||||
w = max(0, min(1, w))
|
||||
|
||||
mr = int(r1 * (1 - w) + r2 * w)
|
||||
mg = int(g1 * (1 - w) + g2 * w)
|
||||
mb = int(b1 * (1 - w) + b2 * w)
|
||||
|
||||
result = f" 颜色混合 (权重 {w}):\n"
|
||||
result += f" 颜色1: {self._rgb_to_hex(r1, g1, b1)}\n"
|
||||
result += f" 颜色2: {self._rgb_to_hex(r2, g2, b2)}\n"
|
||||
result += f" 混合: {self._rgb_to_hex(mr, mg, mb)}\n"
|
||||
return result
|
||||
|
||||
def _op_blind(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:请提供颜色和色盲类型\n类型: protanopia, deuteranopia, tritanopia"
|
||||
r, g, b = self._parse_color(args[0])
|
||||
blind_type = args[1].lower()
|
||||
|
||||
# 简化的色盲模拟矩阵
|
||||
matrices = {
|
||||
"protanopia": [[0.567, 0.433, 0.0], [0.558, 0.442, 0.0], [0.0, 0.242, 0.758]],
|
||||
"deuteranopia": [[0.625, 0.375, 0.0], [0.7, 0.3, 0.0], [0.0, 0.3, 0.7]],
|
||||
"tritanopia": [[0.95, 0.05, 0.0], [0.0, 0.433, 0.567], [0.0, 0.475, 0.525]],
|
||||
}
|
||||
|
||||
matrix = matrices.get(blind_type)
|
||||
if not matrix:
|
||||
return f"错误:未知类型 '{blind_type}'\n支持: protanopia, deuteranopia, tritanopia"
|
||||
|
||||
nr = int(matrix[0][0] * r + matrix[0][1] * g + matrix[0][2] * b)
|
||||
ng = int(matrix[1][0] * r + matrix[1][1] * g + matrix[1][2] * b)
|
||||
nb = int(matrix[2][0] * r + matrix[2][1] * g + matrix[2][2] * b)
|
||||
nr, ng, nb = max(0, min(255, nr)), max(0, min(255, ng)), max(0, min(255, nb))
|
||||
|
||||
result = f" 色盲模拟 ({blind_type}):\n"
|
||||
result += f" 原色: {self._rgb_to_hex(r, g, b)}\n"
|
||||
result += f" 模拟: {self._rgb_to_hex(nr, ng, nb)}\n"
|
||||
return result
|
||||
|
||||
|
||||
def test_plugin():
|
||||
plugin = Liugin()
|
||||
print("颜色工具测试:")
|
||||
print(plugin.handle('convert "#3498db"'))
|
||||
print(plugin.handle('contrast "#FFFFFF" "#000000"'))
|
||||
print(plugin.handle('palette "#e74c3c" -n 7'))
|
||||
print(plugin.handle('complementary "#3498db"'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_plugin()
|
||||
+56
-51
@@ -5,12 +5,17 @@
|
||||
import os
|
||||
import json
|
||||
import shutil
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
# 添加项目根目录到 path
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from xcli_core.tool_result import ToolResult, ErrorCode
|
||||
|
||||
class Plugin:
|
||||
|
||||
class Liugin:
|
||||
"""文件管理器 - 文件和目录的增删改查"""
|
||||
|
||||
def __init__(self):
|
||||
@@ -70,11 +75,11 @@ class Plugin:
|
||||
path = arguments.get("path", "")
|
||||
return f"{op} {path}".strip()
|
||||
|
||||
def handle(self, args: str) -> str:
|
||||
def handle(self, args: str):
|
||||
try:
|
||||
parts = self._parse_args(args)
|
||||
if not parts:
|
||||
return "错误:请提供操作类型"
|
||||
return ToolResult.missing("操作类型")
|
||||
|
||||
operation = parts[0].lower()
|
||||
handlers = {
|
||||
@@ -92,7 +97,7 @@ class Plugin:
|
||||
|
||||
handler = handlers.get(operation)
|
||||
if not handler:
|
||||
return f"错误:不支持的操作 '{operation}'"
|
||||
return ToolResult.fail(f"不支持的操作 '{operation}'", ErrorCode.UNSUPPORTED_OP)
|
||||
return handler(parts[1:])
|
||||
|
||||
except Exception as e:
|
||||
@@ -115,7 +120,7 @@ class Plugin:
|
||||
parts.append(current)
|
||||
return parts
|
||||
|
||||
def _op_list(self, args: List[str]) -> str:
|
||||
def _op_list(self, args: List[str]):
|
||||
path, pattern = ".", None
|
||||
i = 0
|
||||
while i < len(args):
|
||||
@@ -127,7 +132,7 @@ class Plugin:
|
||||
|
||||
path = os.path.abspath(path)
|
||||
if not os.path.isdir(path):
|
||||
return f"错误:不是目录: {path}"
|
||||
return ToolResult.not_found(f"目录: {path}")
|
||||
|
||||
items = []
|
||||
try:
|
||||
@@ -153,13 +158,13 @@ class Plugin:
|
||||
result.append(f" ... 还有 {len(items) - 100} 项")
|
||||
result.append("─" * 50)
|
||||
result.append(f"共 {dirs} 个文件夹, {files} 个文件")
|
||||
return '\n'.join(result)
|
||||
return ToolResult.ok('\n'.join(result), tool_name="file_manager")
|
||||
except Exception as e:
|
||||
return f"列出目录失败: {e}"
|
||||
return ToolResult.fail(str(e), ErrorCode.EXEC_FAILED, tool_name="file_manager")
|
||||
|
||||
def _op_read(self, args: List[str]) -> str:
|
||||
def _op_read(self, args: List[str]):
|
||||
if not args:
|
||||
return "错误:请提供文件路径"
|
||||
return ToolResult.missing("文件路径")
|
||||
file_path = os.path.abspath(args[0])
|
||||
max_lines = None
|
||||
i = 1
|
||||
@@ -168,16 +173,16 @@ class Plugin:
|
||||
try:
|
||||
max_lines = int(args[i + 1])
|
||||
except ValueError:
|
||||
return "错误:行数必须是整数"
|
||||
return ToolResult.fail("行数必须是整数", ErrorCode.INVALID_ARGS)
|
||||
i += 1
|
||||
i += 1
|
||||
|
||||
if not os.path.isfile(file_path):
|
||||
return f"错误:文件不存在: {file_path}"
|
||||
return ToolResult.not_found(f"文件: {file_path}")
|
||||
|
||||
size = os.path.getsize(file_path)
|
||||
if size > 10 * 1024 * 1024:
|
||||
return f"文件过大 ({size/1024/1024:.1f}MB),请用 -n 限制行数"
|
||||
return ToolResult.fail(f"文件过大 ({size/1024/1024:.1f}MB),请用 -n 限制行数", ErrorCode.INVALID_ARGS)
|
||||
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
@@ -195,13 +200,13 @@ class Plugin:
|
||||
content = '\n'.join(f"{i+1:4d} | {l.rstrip()}" for i, l in enumerate(all_lines))
|
||||
header = f" {file_path} ({size}B, {len(all_lines)} 行)"
|
||||
|
||||
return f"{header}\n{'─' * 50}\n{content}"
|
||||
return ToolResult.ok(f"{header}\n{'─' * 50}\n{content}", tool_name="file_manager")
|
||||
except UnicodeDecodeError:
|
||||
return "错误:无法解码(可能是二进制文件)"
|
||||
return ToolResult.fail("无法解码(可能是二进制文件)", ErrorCode.DECODE_ERROR)
|
||||
|
||||
def _op_write(self, args: List[str]) -> str:
|
||||
def _op_write(self, args: List[str]):
|
||||
if len(args) < 2:
|
||||
return "错误:格式: write <文件> -c <内容>"
|
||||
return ToolResult.fail("格式: write <文件> -c <内容>", ErrorCode.MISSING_ARGS)
|
||||
fp = os.path.abspath(args[0])
|
||||
content = None
|
||||
i = 1
|
||||
@@ -210,15 +215,15 @@ class Plugin:
|
||||
content = args[i + 1]; i += 1
|
||||
i += 1
|
||||
if content is None:
|
||||
return "错误:请用 -c 提供内容"
|
||||
return ToolResult.missing("内容 (-c 参数)")
|
||||
os.makedirs(os.path.dirname(fp) or ".", exist_ok=True)
|
||||
with open(fp, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
return f" 已写入: {fp} ({len(content)} 字符)"
|
||||
return ToolResult.ok(f"已写入: {fp} ({len(content)} 字符)", tool_name="file_manager")
|
||||
|
||||
def _op_append(self, args: List[str]) -> str:
|
||||
def _op_append(self, args: List[str]):
|
||||
if len(args) < 2:
|
||||
return "错误:格式: append <文件> -c <内容>"
|
||||
return ToolResult.fail("格式: append <文件> -c <内容>", ErrorCode.MISSING_ARGS)
|
||||
fp = os.path.abspath(args[0])
|
||||
content = None
|
||||
i = 1
|
||||
@@ -227,59 +232,59 @@ class Plugin:
|
||||
content = args[i + 1]; i += 1
|
||||
i += 1
|
||||
if content is None:
|
||||
return "错误:请用 -c 提供内容"
|
||||
return ToolResult.missing("内容 (-c 参数)")
|
||||
if not os.path.isfile(fp):
|
||||
return f"错误:文件不存在: {fp}"
|
||||
return ToolResult.not_found(f"文件: {fp}")
|
||||
with open(fp, 'a', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
return f" 已追加: {fp}"
|
||||
return ToolResult.ok(f"已追加: {fp}", tool_name="file_manager")
|
||||
|
||||
def _op_copy(self, args: List[str]) -> str:
|
||||
def _op_copy(self, args: List[str]):
|
||||
if len(args) < 2:
|
||||
return "错误:请提供源和目标路径"
|
||||
return ToolResult.missing("源和目标路径")
|
||||
src, dst = os.path.abspath(args[0]), os.path.abspath(args[1])
|
||||
if not os.path.exists(src):
|
||||
return f"错误:源不存在: {src}"
|
||||
return ToolResult.not_found(f"源: {src}")
|
||||
os.makedirs(os.path.dirname(dst) or ".", exist_ok=True)
|
||||
if os.path.isfile(src):
|
||||
shutil.copy2(src, dst)
|
||||
else:
|
||||
shutil.copytree(src, dst, dirs_exist_ok=True)
|
||||
return f" 已复制:\n {src}\n → {dst}"
|
||||
return ToolResult.ok(f"已复制:\n {src}\n → {dst}", tool_name="file_manager")
|
||||
|
||||
def _op_move(self, args: List[str]) -> str:
|
||||
def _op_move(self, args: List[str]):
|
||||
if len(args) < 2:
|
||||
return "错误:请提供源和目标路径"
|
||||
return ToolResult.missing("源和目标路径")
|
||||
src, dst = os.path.abspath(args[0]), os.path.abspath(args[1])
|
||||
if not os.path.exists(src):
|
||||
return f"错误:源不存在: {src}"
|
||||
return ToolResult.not_found(f"源: {src}")
|
||||
os.makedirs(os.path.dirname(dst) or ".", exist_ok=True)
|
||||
shutil.move(src, dst)
|
||||
return f" 已移动:\n {src}\n → {dst}"
|
||||
return ToolResult.ok(f"已移动:\n {src}\n → {dst}", tool_name="file_manager")
|
||||
|
||||
def _op_delete(self, args: List[str]) -> str:
|
||||
def _op_delete(self, args: List[str]):
|
||||
if not args:
|
||||
return "错误:请提供路径"
|
||||
return ToolResult.missing("路径")
|
||||
path = os.path.abspath(args[0])
|
||||
recursive = "-r" in args
|
||||
if not os.path.exists(path):
|
||||
return f"错误:不存在: {path}"
|
||||
return ToolResult.not_found(f"路径: {path}")
|
||||
if os.path.isfile(path):
|
||||
os.remove(path)
|
||||
return f" 已删除文件: {path}"
|
||||
return ToolResult.ok(f"已删除文件: {path}", tool_name="file_manager")
|
||||
elif os.path.isdir(path):
|
||||
if recursive:
|
||||
shutil.rmtree(path)
|
||||
return f" 已删除目录: {path}"
|
||||
return ToolResult.ok(f"已删除目录: {path}", tool_name="file_manager")
|
||||
elif not os.listdir(path):
|
||||
os.rmdir(path)
|
||||
return f" 已删除空目录: {path}"
|
||||
return ToolResult.ok(f"已删除空目录: {path}", tool_name="file_manager")
|
||||
else:
|
||||
return "错误:目录不为空,用 -r 递归删除"
|
||||
return ToolResult.fail("目录不为空,用 -r 递归删除", ErrorCode.INVALID_ARGS)
|
||||
|
||||
def _op_search(self, args: List[str]) -> str:
|
||||
def _op_search(self, args: List[str]):
|
||||
if not args:
|
||||
return "错误:请提供搜索目录"
|
||||
return ToolResult.missing("搜索目录")
|
||||
directory = os.path.abspath(args[0])
|
||||
pattern, content_search = None, None
|
||||
i = 1
|
||||
@@ -291,7 +296,7 @@ class Plugin:
|
||||
i += 1
|
||||
|
||||
if not os.path.isdir(directory):
|
||||
return f"错误:目录不存在: {directory}"
|
||||
return ToolResult.not_found(f"目录: {directory}")
|
||||
|
||||
results = []
|
||||
for root, dirs, files in os.walk(directory):
|
||||
@@ -317,14 +322,14 @@ class Plugin:
|
||||
if len(results) >= 50:
|
||||
break
|
||||
|
||||
return f"搜索结果 ({len(results)}):\n" + '\n'.join(results) if results else "未找到匹配"
|
||||
return ToolResult.ok(f"搜索结果 ({len(results)}):\n" + '\n'.join(results), tool_name="file_manager") if results else ToolResult.ok("未找到匹配", tool_name="file_manager")
|
||||
|
||||
def _op_info(self, args: List[str]) -> str:
|
||||
def _op_info(self, args: List[str]):
|
||||
if not args:
|
||||
return "错误:请提供路径"
|
||||
return ToolResult.missing("路径")
|
||||
path = os.path.abspath(args[0])
|
||||
if not os.path.exists(path):
|
||||
return f"错误:不存在: {path}"
|
||||
return ToolResult.not_found(f"路径: {path}")
|
||||
|
||||
stat = os.stat(path)
|
||||
result = [
|
||||
@@ -337,13 +342,13 @@ class Plugin:
|
||||
if os.path.isfile(path):
|
||||
ext = os.path.splitext(path)[1]
|
||||
result.append(f"扩展名: {ext or '无'}")
|
||||
return '\n'.join(result)
|
||||
return ToolResult.ok('\n'.join(result), tool_name="file_manager")
|
||||
|
||||
def _op_mkdir(self, args: List[str]) -> str:
|
||||
def _op_mkdir(self, args: List[str]):
|
||||
if not args:
|
||||
return "错误:请提供目录路径"
|
||||
return ToolResult.missing("目录路径")
|
||||
path = os.path.abspath(args[0])
|
||||
if os.path.exists(path):
|
||||
return f"错误:已存在: {path}"
|
||||
return ToolResult.fail(f"已存在: {path}", ErrorCode.INVALID_ARGS)
|
||||
os.makedirs(path, exist_ok=True)
|
||||
return f" 已创建: {path}"
|
||||
return ToolResult.ok(f"已创建: {path}", tool_name="file_manager")
|
||||
@@ -443,7 +443,7 @@ JSON格式示例:
|
||||
# 测试函数
|
||||
def test_plugin():
|
||||
"""测试插件功能"""
|
||||
plugin = Plugin()
|
||||
plugin = Liugin()
|
||||
print("前端测试验证插件测试:")
|
||||
|
||||
print("\n1. 创建测试HTML文件:")
|
||||
|
||||
@@ -8,7 +8,7 @@ import subprocess
|
||||
from typing import Tuple, Optional
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""Git 版本控制 - 基础操作 + 工作流自动化"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ from typing import Optional, List, Dict, Any
|
||||
IS_WINDOWS = sys.platform == "win32"
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""Windows GUI 自动化 - 屏幕元素识别、点击、输入、截图、元素树遍历"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
@@ -0,0 +1,383 @@
|
||||
"""
|
||||
UUID / ID 生成器插件 - 生成各种格式的唯一标识符
|
||||
支持 UUID v1/v4/v5、NanoID、短ID、雪花ID、自定义格式
|
||||
"""
|
||||
|
||||
import uuid
|
||||
import hashlib
|
||||
import time
|
||||
import random
|
||||
import string
|
||||
import json
|
||||
import os
|
||||
import struct
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
|
||||
|
||||
class Liugin:
|
||||
"""UUID / ID 生成器 - 多格式唯一标识符生成"""
|
||||
|
||||
def __init__(self):
|
||||
self.usage = """UUID / ID 生成器
|
||||
|
||||
操作:
|
||||
uuid [-v 版本] [-n 数量] - 生成 UUID(默认 v4)
|
||||
v1=时间戳 v4=随机 v5=命名
|
||||
nanoid [-s 长度] [-n 数量] - 生成 NanoID(默认21位)
|
||||
short [-s 长度] [-n 数量] - 生成短ID(默认8位)
|
||||
snowflake [-n 数量] - 生成雪花ID(时间排序)
|
||||
custom <模板> [-n 数量] - 自定义格式生成
|
||||
模板: {U}=大写字母 {L}=小写 {D}=数字 {X}=十六进制
|
||||
ulid [-n 数量] - 生成 ULID(时间排序+随机)
|
||||
timestamp - 当前时间戳(秒/毫秒/微秒)
|
||||
from_name <命名空间> <名称> - 基于名称生成确定性 UUID (v5)
|
||||
batch <类型> [-n 数量] [-o 文件] - 批量生成并保存到文件
|
||||
|
||||
示例:
|
||||
id_generator uuid
|
||||
id_generator uuid -v 1 -n 5
|
||||
id_generator nanoid -s 16
|
||||
id_generator short -s 6 -n 10
|
||||
id_generator custom "USER-{D}{D}{D}{D}{U}{U}" -n 5
|
||||
id_generator snowflake -n 3
|
||||
id_generator batch uuid -n 100 -o ids.txt
|
||||
"""
|
||||
self.cli = None
|
||||
self._machine_id = random.randint(0, 1023)
|
||||
self._sequence = 0
|
||||
self._last_ts = 0
|
||||
|
||||
def set_cli(self, cli):
|
||||
self.cli = cli
|
||||
|
||||
def get_tool_info(self):
|
||||
return {
|
||||
"name": "id_generator",
|
||||
"description": "UUID/ID 生成器 - 支持 UUID、NanoID、短ID、雪花ID、ULID、自定义格式",
|
||||
"keywords": ["uuid", "id", "生成", "唯一", "标识", "nanoid", "snowflake", "ulid", "短id"],
|
||||
"usage": self.usage
|
||||
}
|
||||
|
||||
def get_mcp_definition(self):
|
||||
return {
|
||||
"name": "id_generator",
|
||||
"description": "UUID/ID 生成器,支持多种格式",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"operation": {
|
||||
"type": "string",
|
||||
"enum": ["uuid", "nanoid", "short", "snowflake", "custom",
|
||||
"ulid", "timestamp", "from_name", "batch"],
|
||||
"description": "操作类型"
|
||||
},
|
||||
"version": {"type": "integer", "description": "UUID 版本 (1/4/5)"},
|
||||
"length": {"type": "integer", "description": "ID 长度"},
|
||||
"count": {"type": "integer", "description": "生成数量"},
|
||||
"template": {"type": "string", "description": "自定义模板"},
|
||||
"namespace": {"type": "string", "description": "命名空间"},
|
||||
"name": {"type": "string", "description": "名称"},
|
||||
"output": {"type": "string", "description": "输出文件"}
|
||||
},
|
||||
"required": ["operation"]
|
||||
}
|
||||
}
|
||||
|
||||
def convert_mcp_args(self, arguments):
|
||||
op = arguments.get("operation", "")
|
||||
count = arguments.get("count", "")
|
||||
parts = [op]
|
||||
if count:
|
||||
parts.extend(["-n", str(count)])
|
||||
return " ".join(parts)
|
||||
|
||||
def handle(self, args: str) -> str:
|
||||
try:
|
||||
parts = self._parse_args(args)
|
||||
if not parts:
|
||||
return self._op_uuid([])
|
||||
|
||||
operation = parts[0].lower()
|
||||
handlers = {
|
||||
"uuid": self._op_uuid,
|
||||
"nanoid": self._op_nanoid,
|
||||
"short": self._op_short,
|
||||
"snowflake": self._op_snowflake,
|
||||
"custom": self._op_custom,
|
||||
"ulid": self._op_ulid,
|
||||
"timestamp": self._op_timestamp,
|
||||
"from_name": self._op_from_name,
|
||||
"batch": self._op_batch,
|
||||
}
|
||||
|
||||
handler = handlers.get(operation)
|
||||
if not handler:
|
||||
return f"错误:不支持的操作 '{operation}'"
|
||||
return handler(parts[1:])
|
||||
|
||||
except Exception as e:
|
||||
return f"ID 生成错误: {str(e)}"
|
||||
|
||||
def _parse_args(self, args: str) -> List[str]:
|
||||
parts, current, in_quotes, qc = [], "", False, None
|
||||
for ch in args:
|
||||
if ch in ('"', "'") and not in_quotes:
|
||||
in_quotes, qc = True, ch
|
||||
elif ch == qc and in_quotes:
|
||||
in_quotes, qc = False, None
|
||||
elif ch == ' ' and not in_quotes:
|
||||
if current:
|
||||
parts.append(current)
|
||||
current = ""
|
||||
else:
|
||||
current += ch
|
||||
if current:
|
||||
parts.append(current)
|
||||
return parts
|
||||
|
||||
def _parse_opts(self, args: List[str]) -> dict:
|
||||
opts = {}
|
||||
i = 0
|
||||
while i < len(args):
|
||||
if args[i].startswith("-") and i + 1 < len(args):
|
||||
key = args[i].lstrip("-")
|
||||
opts[key] = args[i + 1]
|
||||
i += 1
|
||||
i += 1
|
||||
return opts
|
||||
|
||||
def _op_uuid(self, args: List[str]) -> str:
|
||||
opts = self._parse_opts(args)
|
||||
version = int(opts.get("v", 4))
|
||||
count = int(opts.get("n", 1))
|
||||
count = min(count, 1000)
|
||||
|
||||
ids = []
|
||||
for _ in range(count):
|
||||
if version == 1:
|
||||
ids.append(str(uuid.uuid1()))
|
||||
elif version == 4:
|
||||
ids.append(str(uuid.uuid4()))
|
||||
elif version == 5:
|
||||
ids.append(str(uuid.uuid5(uuid.NAMESPACE_DNS, "example.com")))
|
||||
else:
|
||||
return f"错误:不支持的 UUID 版本: {version}"
|
||||
|
||||
header = f" UUID v{version} ({count} 个):\n"
|
||||
if count <= 20:
|
||||
return header + "\n".join(f" {u}" for u in ids)
|
||||
else:
|
||||
return header + "\n".join(f" {u}" for u in ids[:10]) + f"\n ... (共 {count} 个)"
|
||||
|
||||
def _op_nanoid(self, args: List[str]) -> str:
|
||||
opts = self._parse_opts(args)
|
||||
size = int(opts.get("s", 21))
|
||||
count = int(opts.get("n", 1))
|
||||
count = min(count, 1000)
|
||||
|
||||
alphabet = string.ascii_letters + string.digits + "_-"
|
||||
ids = []
|
||||
for _ in range(count):
|
||||
ids.append(''.join(random.choices(alphabet, k=size)))
|
||||
|
||||
header = f" NanoID (长度 {size}, {count} 个):\n"
|
||||
if count <= 20:
|
||||
return header + "\n".join(f" {i}" for i in ids)
|
||||
else:
|
||||
return header + "\n".join(f" {i}" for i in ids[:10]) + f"\n ... (共 {count} 个)"
|
||||
|
||||
def _op_short(self, args: List[str]) -> str:
|
||||
opts = self._parse_opts(args)
|
||||
size = int(opts.get("s", 8))
|
||||
count = int(opts.get("n", 1))
|
||||
count = min(count, 1000)
|
||||
|
||||
chars = string.ascii_letters + string.digits
|
||||
ids = []
|
||||
for _ in range(count):
|
||||
ids.append(''.join(random.choices(chars, k=size)))
|
||||
|
||||
header = f" 短ID (长度 {size}, {count} 个):\n"
|
||||
if count <= 20:
|
||||
return header + "\n".join(f" {i}" for i in ids)
|
||||
else:
|
||||
return header + "\n".join(f" {i}" for i in ids[:10]) + f"\n ... (共 {count} 个)"
|
||||
|
||||
def _op_snowflake(self, args: List[str]) -> str:
|
||||
opts = self._parse_opts(args)
|
||||
count = int(opts.get("n", 1))
|
||||
count = min(count, 1000)
|
||||
|
||||
# Twitter Snowflake: 41位时间戳 + 10位机器ID + 12位序列号
|
||||
epoch = 1288834974657 # Twitter epoch (2010-11-04)
|
||||
|
||||
ids = []
|
||||
for _ in range(count):
|
||||
ts = int(time.time() * 1000) - epoch
|
||||
if ts == self._last_ts:
|
||||
self._sequence = (self._sequence + 1) & 0xFFF
|
||||
if self._sequence == 0:
|
||||
while ts <= self._last_ts:
|
||||
ts = int(time.time() * 1000) - epoch
|
||||
else:
|
||||
self._sequence = 0
|
||||
self._last_ts = ts
|
||||
|
||||
snowflake_id = (ts << 22) | (self._machine_id << 12) | self._sequence
|
||||
ids.append(str(snowflake_id))
|
||||
|
||||
header = f"❄️ 雪花ID ({count} 个):\n"
|
||||
if count <= 20:
|
||||
return header + "\n".join(f" {i}" for i in ids)
|
||||
else:
|
||||
return header + "\n".join(f" {i}" for i in ids[:10]) + f"\n ... (共 {count} 个)"
|
||||
|
||||
def _op_custom(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供模板\n示例: custom \"ORDER-{D}{D}{D}{D}-{U}{U}{U}\""
|
||||
template = args[0]
|
||||
opts = self._parse_opts(args[1:])
|
||||
count = int(opts.get("n", 1))
|
||||
count = min(count, 1000)
|
||||
|
||||
ids = []
|
||||
for _ in range(count):
|
||||
result = []
|
||||
for ch in template:
|
||||
if ch == '{':
|
||||
continue # handled below
|
||||
result.append(ch)
|
||||
# Actually use proper template parsing
|
||||
ids.append(self._expand_template(template))
|
||||
|
||||
header = f" ️ 自定义ID (模板: {template}, {count} 个):\n"
|
||||
if count <= 20:
|
||||
return header + "\n".join(f" {i}" for i in ids)
|
||||
else:
|
||||
return header + "\n".join(f" {i}" for i in ids[:10]) + f"\n ... (共 {count} 个)"
|
||||
|
||||
def _expand_template(self, template: str) -> str:
|
||||
result = []
|
||||
i = 0
|
||||
while i < len(template):
|
||||
if template[i] == '{' and i + 2 < len(template) and template[i + 2] == '}':
|
||||
token = template[i + 1]
|
||||
if token == 'U':
|
||||
result.append(random.choice(string.ascii_uppercase))
|
||||
elif token == 'L':
|
||||
result.append(random.choice(string.ascii_lowercase))
|
||||
elif token == 'D':
|
||||
result.append(random.choice(string.digits))
|
||||
elif token == 'X':
|
||||
result.append(random.choice('0123456789ABCDEF'))
|
||||
elif token == 'x':
|
||||
result.append(random.choice('0123456789abcdef'))
|
||||
elif token == 'A':
|
||||
result.append(random.choice(string.ascii_letters))
|
||||
else:
|
||||
result.append(template[i:i + 3])
|
||||
i += 3
|
||||
else:
|
||||
result.append(template[i])
|
||||
i += 1
|
||||
return ''.join(result)
|
||||
|
||||
def _op_ulid(self, args: List[str]) -> str:
|
||||
opts = self._parse_opts(args)
|
||||
count = int(opts.get("n", 1))
|
||||
count = min(count, 1000)
|
||||
|
||||
# ULID: 48位时间戳 + 80位随机数, Crockford Base32
|
||||
alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
|
||||
|
||||
ids = []
|
||||
for _ in range(count):
|
||||
ts = int(time.time() * 1000)
|
||||
# 时间部分 (10 chars)
|
||||
t_chars = []
|
||||
t = ts
|
||||
for _ in range(10):
|
||||
t_chars.append(alphabet[t & 0x1F])
|
||||
t >>= 5
|
||||
t_chars.reverse()
|
||||
# 随机部分 (16 chars)
|
||||
r_chars = [random.choice(alphabet) for _ in range(16)]
|
||||
ids.append(''.join(t_chars) + ''.join(r_chars))
|
||||
|
||||
header = f" ULID ({count} 个):\n"
|
||||
if count <= 20:
|
||||
return header + "\n".join(f" {i}" for i in ids)
|
||||
else:
|
||||
return header + "\n".join(f" {i}" for i in ids[:10]) + f"\n ... (共 {count} 个)"
|
||||
|
||||
def _op_timestamp(self, args: List[str]) -> str:
|
||||
now = time.time()
|
||||
return (f"⏱ 当前时间戳:\n"
|
||||
f" 秒: {int(now)}\n"
|
||||
f" 毫秒: {int(now * 1000)}\n"
|
||||
f" 微秒: {int(now * 1000000)}\n"
|
||||
f" ISO: {datetime.now().isoformat()}")
|
||||
|
||||
def _op_from_name(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:格式: from_name <命名空间> <名称>\n命名空间: dns, url, oid, x500"
|
||||
ns_map = {"dns": uuid.NAMESPACE_DNS, "url": uuid.NAMESPACE_URL,
|
||||
"oid": uuid.NAMESPACE_OID, "x500": uuid.NAMESPACE_X500}
|
||||
ns = ns_map.get(args[0].lower(), uuid.NAMESPACE_DNS)
|
||||
name = args[1]
|
||||
result = uuid.uuid5(ns, name)
|
||||
return f" 确定性 UUID v5:\n 命名空间: {args[0]}\n 名称: {name}\n UUID: {result}"
|
||||
|
||||
def _op_batch(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供类型 (uuid/nanoid/short/snowflake/ulid)"
|
||||
id_type = args[0]
|
||||
opts = self._parse_opts(args[1:])
|
||||
count = int(opts.get("n", 10))
|
||||
output = opts.get("o", "")
|
||||
|
||||
# 生成对应类型的ID
|
||||
gen_args = [f"-n", str(count)]
|
||||
handlers = {
|
||||
"uuid": self._op_uuid,
|
||||
"nanoid": self._op_nanoid,
|
||||
"short": self._op_short,
|
||||
"snowflake": self._op_snowflake,
|
||||
"ulid": self._op_ulid,
|
||||
}
|
||||
handler = handlers.get(id_type)
|
||||
if not handler:
|
||||
return f"错误:不支持的类型 '{id_type}'"
|
||||
|
||||
result = handler(gen_args)
|
||||
|
||||
if output:
|
||||
# 提取纯ID保存到文件
|
||||
ids = [line.strip().lstrip(' ') for line in result.split('\n')
|
||||
if line.strip() and not any(c in line for c in [' ', '️', '', '❄️', ''])]
|
||||
try:
|
||||
with open(output, 'w', encoding='utf-8') as f:
|
||||
f.write('\n'.join(ids))
|
||||
return f"{result}\n\n 已保存到: {output}"
|
||||
except Exception as e:
|
||||
return f"{result}\n\n 保存失败: {e}"
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def test_plugin():
|
||||
plugin = Liugin()
|
||||
print("ID 生成器测试:")
|
||||
print(plugin.handle('uuid'))
|
||||
print(plugin.handle('uuid -v 1 -n 3'))
|
||||
print(plugin.handle('nanoid -s 16'))
|
||||
print(plugin.handle('short -s 6 -n 5'))
|
||||
print(plugin.handle('snowflake -n 3'))
|
||||
print(plugin.handle('ulid'))
|
||||
print(plugin.handle('timestamp'))
|
||||
print(plugin.handle('custom "ORD-{D}{D}{D}{D}-{U}{U}{U}" -n 5'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_plugin()
|
||||
@@ -10,7 +10,7 @@ import time
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""图像生成工具 — 自然语言/Tag 生图,基于 ComfyUI 在线服务"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
@@ -0,0 +1,386 @@
|
||||
"""
|
||||
JSON 工具箱插件 - JSON 格式化、校验、查询、转换
|
||||
提供 JSON 美化、压缩、验证、路径查询、类型转换等功能
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from typing import List
|
||||
|
||||
|
||||
class Liugin:
|
||||
"""JSON 工具箱 - 格式化/校验/查询/转换"""
|
||||
|
||||
def __init__(self):
|
||||
self.usage = """JSON 工具箱
|
||||
|
||||
操作:
|
||||
format <文件或JSON> - 美化 JSON(自动缩进)
|
||||
minify <文件或JSON> - 压缩 JSON(去除空白)
|
||||
validate <文件或JSON> - 校验 JSON 是否合法
|
||||
query <文件或JSON> <路径> - 按路径查询值(如 users[0].name)
|
||||
keys <文件或JSON> - 列出所有键(扁平化)
|
||||
diff <文件1> <文件2> - 比较两个 JSON 的差异
|
||||
to_csv <文件或JSON> - 将 JSON 数组转为 CSV
|
||||
merge <文件1> <文件2> - 合并两个 JSON 对象
|
||||
sample <文件或JSON> [-n 数量] - 从数组中随机采样
|
||||
schema <文件或JSON> - 推断 JSON Schema
|
||||
|
||||
示例:
|
||||
json_toolkit format data.json
|
||||
json_toolkit validate '{"name": "test"}'
|
||||
json_toolkit query config.json "database.host"
|
||||
json_toolkit keys data.json
|
||||
json_toolkit diff a.json b.json
|
||||
"""
|
||||
self.cli = None
|
||||
|
||||
def set_cli(self, cli):
|
||||
self.cli = cli
|
||||
|
||||
def get_tool_info(self):
|
||||
return {
|
||||
"name": "json_toolkit",
|
||||
"description": "JSON 工具箱 - 格式化、压缩、校验、路径查询、键提取、差异比较、转换",
|
||||
"keywords": ["json", "格式化", "校验", "查询", "格式", "转换", "美化", "压缩", "validate", "format"],
|
||||
"usage": self.usage
|
||||
}
|
||||
|
||||
def get_mcp_definition(self):
|
||||
return {
|
||||
"name": "json_toolkit",
|
||||
"description": "JSON 工具箱,支持格式化、压缩、校验、路径查询、键提取、差异比较",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"operation": {
|
||||
"type": "string",
|
||||
"enum": ["format", "minify", "validate", "query", "keys",
|
||||
"diff", "to_csv", "merge", "sample", "schema"],
|
||||
"description": "操作类型"
|
||||
},
|
||||
"input": {"type": "string", "description": "JSON 字符串或文件路径"},
|
||||
"path": {"type": "string", "description": "查询路径(query 操作用)"},
|
||||
"input2": {"type": "string", "description": "第二个 JSON 输入(diff/merge 用)"},
|
||||
"count": {"type": "integer", "description": "采样数量(sample 操作用)"}
|
||||
},
|
||||
"required": ["operation", "input"]
|
||||
}
|
||||
}
|
||||
|
||||
def convert_mcp_args(self, arguments):
|
||||
op = arguments.get("operation", "")
|
||||
inp = arguments.get("input", "")
|
||||
path = arguments.get("path", "")
|
||||
parts = [op, inp]
|
||||
if path:
|
||||
parts.append(path)
|
||||
return " ".join(parts)
|
||||
|
||||
def handle(self, args: str) -> str:
|
||||
try:
|
||||
parts = self._parse_args(args)
|
||||
if not parts:
|
||||
return "错误:请提供操作类型"
|
||||
|
||||
operation = parts[0].lower()
|
||||
handlers = {
|
||||
"format": self._op_format,
|
||||
"minify": self._op_minify,
|
||||
"validate": self._op_validate,
|
||||
"query": self._op_query,
|
||||
"keys": self._op_keys,
|
||||
"diff": self._op_diff,
|
||||
"to_csv": self._op_to_csv,
|
||||
"merge": self._op_merge,
|
||||
"sample": self._op_sample,
|
||||
"schema": self._op_schema,
|
||||
}
|
||||
|
||||
handler = handlers.get(operation)
|
||||
if not handler:
|
||||
return f"错误:不支持的操作 '{operation}'\n支持: {', '.join(handlers.keys())}"
|
||||
return handler(parts[1:])
|
||||
|
||||
except Exception as e:
|
||||
return f"JSON 工具错误: {str(e)}"
|
||||
|
||||
def _parse_args(self, args: str) -> List[str]:
|
||||
parts, current, in_quotes, qc = [], "", False, None
|
||||
for ch in args:
|
||||
if ch in ('"', "'") and not in_quotes:
|
||||
in_quotes, qc = True, ch
|
||||
elif ch == qc and in_quotes:
|
||||
in_quotes, qc = False, None
|
||||
elif ch == ' ' and not in_quotes:
|
||||
if current:
|
||||
parts.append(current)
|
||||
current = ""
|
||||
else:
|
||||
current += ch
|
||||
if current:
|
||||
parts.append(current)
|
||||
return parts
|
||||
|
||||
def _load_json(self, text: str):
|
||||
"""加载 JSON(支持文件路径或 JSON 字符串)"""
|
||||
if os.path.isfile(text):
|
||||
with open(text, 'r', encoding='utf-8') as f:
|
||||
return json.load(f), f"文件: {text}"
|
||||
try:
|
||||
return json.loads(text), "JSON 字符串"
|
||||
except json.JSONDecodeError as e:
|
||||
raise ValueError(f"无效的 JSON: {e}")
|
||||
|
||||
def _op_format(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 JSON 文件或字符串"
|
||||
data, source = self._load_json(args[0])
|
||||
formatted = json.dumps(data, indent=2, ensure_ascii=False)
|
||||
return f"✅ 格式化结果 ({source}):\n{formatted}"
|
||||
|
||||
def _op_minify(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 JSON 文件或字符串"
|
||||
data, source = self._load_json(args[0])
|
||||
minified = json.dumps(data, separators=(',', ':'), ensure_ascii=False)
|
||||
original_len = len(json.dumps(data, indent=2, ensure_ascii=False))
|
||||
saved = original_len - len(minified)
|
||||
return f"✅ 压缩结果 ({source}):\n{minified}\n\n📊 大小: {len(minified)} 字符 (节省 {saved} 字符, {saved/original_len*100:.1f}%)"
|
||||
|
||||
def _op_validate(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 JSON 文件或字符串"
|
||||
text = args[0]
|
||||
try:
|
||||
if os.path.isfile(text):
|
||||
with open(text, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
source = f"文件: {text}"
|
||||
else:
|
||||
data = json.loads(text)
|
||||
source = "JSON 字符串"
|
||||
|
||||
depth = self._get_depth(data)
|
||||
keys_count = self._count_keys(data)
|
||||
|
||||
result = f"✅ JSON 合法 ({source})\n"
|
||||
result += f" 类型: {type(data).__name__}\n"
|
||||
result += f" 嵌套深度: {depth}\n"
|
||||
result += f" 键总数: {keys_count}"
|
||||
return result
|
||||
except json.JSONDecodeError as e:
|
||||
return f"❌ JSON 无效:\n 行 {e.lineno}, 列 {e.colno}: {e.msg}"
|
||||
except Exception as e:
|
||||
return f"❌ 验证失败: {e}"
|
||||
|
||||
def _op_query(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:格式: query <JSON或文件> <路径>\n示例: query data.json users[0].name"
|
||||
data, _ = self._load_json(args[0])
|
||||
path = args[1]
|
||||
|
||||
result = data
|
||||
parts = re.findall(r'\w+|\[\d+\]', path)
|
||||
current_path = ""
|
||||
|
||||
for part in parts:
|
||||
if part.startswith('['):
|
||||
idx = int(part[1:-1])
|
||||
current_path += part
|
||||
if isinstance(result, list) and 0 <= idx < len(result):
|
||||
result = result[idx]
|
||||
else:
|
||||
return f"❌ 路径 '{current_path}' 无效: 索引越界或非数组"
|
||||
else:
|
||||
current_path += f".{part}" if current_path else part
|
||||
if isinstance(result, dict) and part in result:
|
||||
result = result[part]
|
||||
else:
|
||||
return f"❌ 路径 '{current_path}' 无效: 键不存在"
|
||||
|
||||
output = json.dumps(result, indent=2, ensure_ascii=False) if isinstance(result, (dict, list)) else str(result)
|
||||
return f"🔍 查询结果 ({path}):\n{output}"
|
||||
|
||||
def _op_keys(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 JSON 文件或字符串"
|
||||
data, _ = self._load_json(args[0])
|
||||
|
||||
keys = []
|
||||
self._flatten_keys(data, "", keys)
|
||||
|
||||
result = f"🔑 键列表 (共 {len(keys)} 个):\n"
|
||||
for k in keys:
|
||||
result += f" {k}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _flatten_keys(self, obj, prefix, keys):
|
||||
if isinstance(obj, dict):
|
||||
for k, v in obj.items():
|
||||
full_key = f"{prefix}.{k}" if prefix else k
|
||||
keys.append(full_key)
|
||||
self._flatten_keys(v, full_key, keys)
|
||||
elif isinstance(obj, list) and obj:
|
||||
self._flatten_keys(obj[0], f"{prefix}[0]", keys)
|
||||
|
||||
def _op_diff(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:请提供两个 JSON 文件或字符串"
|
||||
data1, src1 = self._load_json(args[0])
|
||||
data2, src2 = self._load_json(args[1])
|
||||
|
||||
diffs = []
|
||||
self._compare(data1, data2, "", diffs)
|
||||
|
||||
if not diffs:
|
||||
return "✅ 两个 JSON 完全相同"
|
||||
|
||||
result = f"🔀 差异比较 ({len(diffs)} 处不同):\n"
|
||||
for d in diffs[:50]:
|
||||
result += f" {d}\n"
|
||||
if len(diffs) > 50:
|
||||
result += f" ... 还有 {len(diffs) - 50} 处差异"
|
||||
return result.rstrip()
|
||||
|
||||
def _compare(self, a, b, path, diffs):
|
||||
if type(a) != type(b):
|
||||
diffs.append(f" 类型不同 @ {path or '根'}: {type(a).__name__} vs {type(b).__name__}")
|
||||
return
|
||||
if isinstance(a, dict):
|
||||
all_keys = set(a.keys()) | set(b.keys())
|
||||
for k in sorted(all_keys):
|
||||
p = f"{path}.{k}" if path else k
|
||||
if k not in a:
|
||||
diffs.append(f"➕ 新增 @ {p}: {b[k]}")
|
||||
elif k not in b:
|
||||
diffs.append(f"➖ 删除 @ {p}: {a[k]}")
|
||||
else:
|
||||
self._compare(a[k], b[k], p, diffs)
|
||||
elif isinstance(a, list):
|
||||
if len(a) != len(b):
|
||||
diffs.append(f" 数组长度不同 @ {path}: {len(a)} vs {len(b)}")
|
||||
for i in range(min(len(a), len(b))):
|
||||
self._compare(a[i], b[i], f"{path}[{i}]", diffs)
|
||||
else:
|
||||
if a != b:
|
||||
diffs.append(f" 值不同 @ {path}: {a!r} → {b!r}")
|
||||
|
||||
def _op_to_csv(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 JSON 文件或字符串"
|
||||
data, _ = self._load_json(args[0])
|
||||
|
||||
if not isinstance(data, list) or not data:
|
||||
return "错误:需要非空 JSON 数组"
|
||||
if not isinstance(data[0], dict):
|
||||
return "错误:数组元素必须是对象"
|
||||
|
||||
headers = list(data[0].keys())
|
||||
lines = [",".join(headers)]
|
||||
for item in data:
|
||||
row = []
|
||||
for h in headers:
|
||||
val = str(item.get(h, ""))
|
||||
if "," in val or '"' in val or "\n" in val:
|
||||
val = '"' + val.replace('"', '""') + '"'
|
||||
row.append(val)
|
||||
lines.append(",".join(row))
|
||||
|
||||
csv_content = "\n".join(lines)
|
||||
return f"📊 CSV 转换结果 ({len(data)} 行):\n{csv_content}"
|
||||
|
||||
def _op_merge(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:请提供两个 JSON 文件或字符串"
|
||||
data1, _ = self._load_json(args[0])
|
||||
data2, _ = self._load_json(args[1])
|
||||
|
||||
if not isinstance(data1, dict) or not isinstance(data2, dict):
|
||||
return "错误:合并仅支持 JSON 对象"
|
||||
|
||||
merged = self._deep_merge(data1, data2)
|
||||
return f"✅ 合并结果:\n{json.dumps(merged, indent=2, ensure_ascii=False)}"
|
||||
|
||||
def _deep_merge(self, a, b):
|
||||
result = a.copy()
|
||||
for k, v in b.items():
|
||||
if k in result and isinstance(result[k], dict) and isinstance(v, dict):
|
||||
result[k] = self._deep_merge(result[k], v)
|
||||
else:
|
||||
result[k] = v
|
||||
return result
|
||||
|
||||
def _op_sample(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 JSON 文件或字符串"
|
||||
data, _ = self._load_json(args[0])
|
||||
count = 3
|
||||
for i, a in enumerate(args):
|
||||
if a == "-n" and i + 1 < len(args):
|
||||
count = int(args[i + 1])
|
||||
|
||||
if not isinstance(data, list):
|
||||
return "错误:需要 JSON 数组"
|
||||
|
||||
import random
|
||||
sampled = random.sample(data, min(count, len(data)))
|
||||
return f"🎲 随机采样 ({len(sampled)}/{len(data)}):\n{json.dumps(sampled, indent=2, ensure_ascii=False)}"
|
||||
|
||||
def _op_schema(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 JSON 文件或字符串"
|
||||
data, _ = self._load_json(args[0])
|
||||
schema = self._infer_schema(data)
|
||||
return f"📋 推断 Schema:\n{json.dumps(schema, indent=2, ensure_ascii=False)}"
|
||||
|
||||
def _infer_schema(self, obj, depth=0):
|
||||
if depth > 10:
|
||||
return {"type": "any"}
|
||||
if isinstance(obj, dict):
|
||||
props = {}
|
||||
for k, v in obj.items():
|
||||
props[k] = self._infer_schema(v, depth + 1)
|
||||
return {"type": "object", "properties": props}
|
||||
elif isinstance(obj, list):
|
||||
if obj:
|
||||
return {"type": "array", "items": self._infer_schema(obj[0], depth + 1)}
|
||||
return {"type": "array", "items": {}}
|
||||
elif isinstance(obj, bool):
|
||||
return {"type": "boolean"}
|
||||
elif isinstance(obj, int):
|
||||
return {"type": "integer"}
|
||||
elif isinstance(obj, float):
|
||||
return {"type": "number"}
|
||||
else:
|
||||
return {"type": "string"}
|
||||
|
||||
def _get_depth(self, obj, depth=0):
|
||||
if depth > 50:
|
||||
return depth
|
||||
if isinstance(obj, dict):
|
||||
return max((self._get_depth(v, depth + 1) for v in obj.values()), default=depth)
|
||||
elif isinstance(obj, list):
|
||||
return max((self._get_depth(v, depth + 1) for v in obj), default=depth)
|
||||
return depth
|
||||
|
||||
def _count_keys(self, obj):
|
||||
if isinstance(obj, dict):
|
||||
return len(obj) + sum(self._count_keys(v) for v in obj.values())
|
||||
elif isinstance(obj, list):
|
||||
return sum(self._count_keys(v) for v in obj)
|
||||
return 0
|
||||
|
||||
|
||||
def test_plugin():
|
||||
plugin = Liugin()
|
||||
print("JSON 工具箱测试:")
|
||||
print(plugin.handle('validate \'{"name": "test", "age": 25}\''))
|
||||
print(plugin.handle('format \'{"name":"test","items":[1,2,3]}\''))
|
||||
print(plugin.handle('keys \'{"database": {"host": "localhost", "port": 3306}}\''))
|
||||
print(plugin.handle('query \'{"users": [{"name": "Alice"}]}\' users[0].name'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_plugin()
|
||||
@@ -0,0 +1,540 @@
|
||||
"""
|
||||
Markdown 工具插件 - Markdown 渲染、目录生成、格式转换
|
||||
支持 TOC 生成、标题提取、表格生成、格式化、统计
|
||||
"""
|
||||
|
||||
import re
|
||||
import json
|
||||
import os
|
||||
from typing import List, Tuple
|
||||
|
||||
|
||||
class Liugin:
|
||||
"""Markdown 工具 - TOC/渲染/格式转换/统计"""
|
||||
|
||||
def __init__(self):
|
||||
self.usage = """Markdown 工具
|
||||
|
||||
操作:
|
||||
toc <文件或Markdown> - 生成目录 (Table of Contents)
|
||||
headings <文件或Markdown> - 提取所有标题
|
||||
stats <文件或Markdown> - 统计信息(字数/行数/段落数等)
|
||||
table <数据> - 从 JSON 数组生成 Markdown 表格
|
||||
checklist <文件> - 提取所有待办事项
|
||||
links <文件或Markdown> - 提取所有链接
|
||||
images <文件或Markdown> - 提取所有图片
|
||||
frontmatter <文件> - 提取 YAML frontmatter
|
||||
format <文件或Markdown> - 格式化(标题层级、列表缩进等)
|
||||
wordcount <文件或Markdown> - 详细字数统计
|
||||
outline <文件或Markdown> - 生成大纲视图
|
||||
lint <文件> - Markdown 语法检查
|
||||
|
||||
示例:
|
||||
markdown_tool toc README.md
|
||||
markdown_tool stats article.md
|
||||
markdown_tool table '[{"name":"Alice","age":25},{"name":"Bob","age":30}]'
|
||||
markdown_tool checklist TODO.md
|
||||
markdown_tool links README.md
|
||||
markdown_tool lint README.md
|
||||
"""
|
||||
self.cli = None
|
||||
|
||||
def set_cli(self, cli):
|
||||
self.cli = cli
|
||||
|
||||
def get_tool_info(self):
|
||||
return {
|
||||
"name": "markdown_tool",
|
||||
"description": "Markdown 工具 - TOC 生成、标题提取、统计、表格生成、语法检查",
|
||||
"keywords": ["markdown", "md", "目录", "toc", "标题", "表格", "统计", "链接", "检查"],
|
||||
"usage": self.usage
|
||||
}
|
||||
|
||||
def get_mcp_definition(self):
|
||||
return {
|
||||
"name": "markdown_tool",
|
||||
"description": "Markdown 工具,支持 TOC、统计、表格生成、链接提取、语法检查",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"operation": {
|
||||
"type": "string",
|
||||
"enum": ["toc", "headings", "stats", "table", "checklist",
|
||||
"links", "images", "frontmatter", "format",
|
||||
"wordcount", "outline", "lint"],
|
||||
"description": "操作类型"
|
||||
},
|
||||
"input": {"type": "string", "description": "Markdown 文件路径或内容"},
|
||||
"data": {"type": "string", "description": "JSON 数据(table 操作用)"}
|
||||
},
|
||||
"required": ["operation", "input"]
|
||||
}
|
||||
}
|
||||
|
||||
def convert_mcp_args(self, arguments):
|
||||
op = arguments.get("operation", "")
|
||||
inp = arguments.get("input", "")
|
||||
return f"{op} {inp}".strip()
|
||||
|
||||
def _load_content(self, text: str) -> Tuple[str, str]:
|
||||
"""加载内容(文件路径或文本)"""
|
||||
if os.path.isfile(text):
|
||||
with open(text, 'r', encoding='utf-8') as f:
|
||||
return f.read(), f"文件: {text}"
|
||||
return text, "文本输入"
|
||||
|
||||
def handle(self, args: str) -> str:
|
||||
try:
|
||||
parts = self._parse_args(args)
|
||||
if not parts:
|
||||
return "错误:请提供操作类型"
|
||||
|
||||
operation = parts[0].lower()
|
||||
handlers = {
|
||||
"toc": self._op_toc,
|
||||
"headings": self._op_headings,
|
||||
"stats": self._op_stats,
|
||||
"table": self._op_table,
|
||||
"checklist": self._op_checklist,
|
||||
"links": self._op_links,
|
||||
"images": self._op_images,
|
||||
"frontmatter": self._op_frontmatter,
|
||||
"format": self._op_format,
|
||||
"wordcount": self._op_wordcount,
|
||||
"outline": self._op_outline,
|
||||
"lint": self._op_lint,
|
||||
}
|
||||
|
||||
handler = handlers.get(operation)
|
||||
if not handler:
|
||||
return f"错误:不支持的操作 '{operation}'"
|
||||
return handler(parts[1:])
|
||||
|
||||
except Exception as e:
|
||||
return f"Markdown 工具错误: {str(e)}"
|
||||
|
||||
def _parse_args(self, args: str) -> List[str]:
|
||||
parts, current, in_quotes, qc = [], "", False, None
|
||||
for ch in args:
|
||||
if ch in ('"', "'") and not in_quotes:
|
||||
in_quotes, qc = True, ch
|
||||
elif ch == qc and in_quotes:
|
||||
in_quotes, qc = False, None
|
||||
elif ch == ' ' and not in_quotes:
|
||||
if current:
|
||||
parts.append(current)
|
||||
current = ""
|
||||
else:
|
||||
current += ch
|
||||
if current:
|
||||
parts.append(current)
|
||||
return parts
|
||||
|
||||
def _extract_headings(self, content: str) -> List[Tuple[int, str, str]]:
|
||||
"""提取标题: [(level, text, slug)]"""
|
||||
headings = []
|
||||
in_code = False
|
||||
for line in content.split('\n'):
|
||||
if line.strip().startswith('```'):
|
||||
in_code = not in_code
|
||||
continue
|
||||
if in_code:
|
||||
continue
|
||||
match = re.match(r'^(#{1,6})\s+(.+)', line)
|
||||
if match:
|
||||
level = len(match.group(1))
|
||||
text = match.group(2).strip()
|
||||
# 生成 slug
|
||||
slug = re.sub(r'[^\w\s-]', '', text.lower())
|
||||
slug = re.sub(r'\s+', '-', slug.strip())
|
||||
headings.append((level, text, slug))
|
||||
return headings
|
||||
|
||||
def _op_toc(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件或内容"
|
||||
content, source = self._load_content(args[0])
|
||||
headings = self._extract_headings(content)
|
||||
|
||||
if not headings:
|
||||
return "📭 未找到标题"
|
||||
|
||||
result = f" 目录 ({source}):\n"
|
||||
result += "─" * 40 + "\n"
|
||||
for level, text, slug in headings:
|
||||
indent = " " * (level - 1)
|
||||
result += f"{indent}- [{text}](#{slug})\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_headings(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件或内容"
|
||||
content, source = self._load_content(args[0])
|
||||
headings = self._extract_headings(content)
|
||||
|
||||
if not headings:
|
||||
return "📭 未找到标题"
|
||||
|
||||
result = f" 标题列表 ({len(headings)} 个):\n"
|
||||
for level, text, _ in headings:
|
||||
prefix = "#" * level
|
||||
result += f" {prefix:8s} {text}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_stats(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件或内容"
|
||||
content, source = self._load_content(args[0])
|
||||
|
||||
lines = content.split('\n')
|
||||
total_lines = len(lines)
|
||||
blank_lines = sum(1 for l in lines if not l.strip())
|
||||
code_blocks = content.count('```') // 2
|
||||
headings = len(self._extract_headings(content))
|
||||
links = len(re.findall(r'\[([^\]]+)\]\([^)]+\)', content))
|
||||
images = len(re.findall(r'!\[([^\]]*)\]\([^)]+\)', content))
|
||||
# 中文字符
|
||||
chinese = len(re.findall(r'[\u4e00-\u9fff]', content))
|
||||
# 英文单词
|
||||
english = len(re.findall(r'[a-zA-Z]+', content))
|
||||
|
||||
# 段落数(连续非空行)
|
||||
paragraphs = 0
|
||||
in_para = False
|
||||
for l in lines:
|
||||
if l.strip():
|
||||
if not in_para:
|
||||
paragraphs += 1
|
||||
in_para = True
|
||||
else:
|
||||
in_para = False
|
||||
|
||||
result = f"📊 Markdown 统计 ({source})\n"
|
||||
result += "─" * 35 + "\n"
|
||||
result += f" 总行数: {total_lines}\n"
|
||||
result += f" 空行: {blank_lines}\n"
|
||||
result += f" 段落: {paragraphs}\n"
|
||||
result += f" 代码块: {code_blocks}\n"
|
||||
result += f" 标题: {headings}\n"
|
||||
result += f" 链接: {links}\n"
|
||||
result += f" 图片: {images}\n"
|
||||
result += f" 中文字: {chinese}\n"
|
||||
result += f" 英文词: {english}\n"
|
||||
result += f" 字符数: {len(content)}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_table(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 JSON 数组"
|
||||
data_str = args[0]
|
||||
try:
|
||||
data = json.loads(data_str)
|
||||
except json.JSONDecodeError as e:
|
||||
return f"❌ JSON 解析错误: {e}"
|
||||
|
||||
if not isinstance(data, list) or not data:
|
||||
return "❌ 需要非空 JSON 数组"
|
||||
if not isinstance(data[0], dict):
|
||||
return "❌ 数组元素必须是对象"
|
||||
|
||||
headers = list(data[0].keys())
|
||||
# 计算列宽
|
||||
widths = {h: len(h) for h in headers}
|
||||
for row in data:
|
||||
for h in headers:
|
||||
widths[h] = max(widths[h], len(str(row.get(h, ""))))
|
||||
|
||||
# 生成表格
|
||||
header_line = "| " + " | ".join(h.ljust(widths[h]) for h in headers) + " |"
|
||||
sep_line = "| " + " | ".join("-" * widths[h] for h in headers) + " |"
|
||||
rows = []
|
||||
for row in data:
|
||||
rows.append("| " + " | ".join(str(row.get(h, "")).ljust(widths[h]) for h in headers) + " |")
|
||||
|
||||
result = f" Markdown 表格 ({len(data)} 行):\n\n"
|
||||
result += header_line + "\n" + sep_line + "\n" + "\n".join(rows)
|
||||
return result
|
||||
|
||||
def _op_checklist(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件"
|
||||
content, source = self._load_content(args[0])
|
||||
|
||||
todos = []
|
||||
done = []
|
||||
for i, line in enumerate(content.split('\n'), 1):
|
||||
match = re.match(r'^(\s*)[-*]\s*\[([ xX])\]\s*(.+)', line)
|
||||
if match:
|
||||
indent = match.group(1)
|
||||
checked = match.group(2).lower() == 'x'
|
||||
text = match.group(3).strip()
|
||||
if checked:
|
||||
done.append((i, text))
|
||||
else:
|
||||
todos.append((i, text))
|
||||
|
||||
result = f" 待办事项 ({source}):\n"
|
||||
result += "─" * 45 + "\n"
|
||||
result += f" ☑ 已完成: {len(done)} 个\n"
|
||||
result += f" 未完成: {len(todos)} 个\n"
|
||||
result += "─" * 45 + "\n"
|
||||
|
||||
if todos:
|
||||
result += "\n 未完成:\n"
|
||||
for line_no, text in todos[:30]:
|
||||
result += f" L{line_no:4d} {text}\n"
|
||||
if done:
|
||||
result += "\n 已完成:\n"
|
||||
for line_no, text in done[:20]:
|
||||
result += f" L{line_no:4d} {text}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_links(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件或内容"
|
||||
content, source = self._load_content(args[0])
|
||||
|
||||
links = re.findall(r'\[([^\]]+)\]\(([^)]+)\)', content)
|
||||
if not links:
|
||||
return f"📭 未找到链接 ({source})"
|
||||
|
||||
result = f" 链接列表 ({len(links)} 个, {source}):\n"
|
||||
result += "─" * 50 + "\n"
|
||||
seen = set()
|
||||
for text, url in links:
|
||||
if url not in seen:
|
||||
seen.add(url)
|
||||
result += f" {text[:30]:30s} → {url}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_images(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件或内容"
|
||||
content, source = self._load_content(args[0])
|
||||
|
||||
images = re.findall(r'!\[([^\]]*)\]\(([^)]+)\)', content)
|
||||
if not images:
|
||||
return f"📭 未找到图片 ({source})"
|
||||
|
||||
result = f" ️ 图片列表 ({len(images)} 个, {source}):\n"
|
||||
result += "─" * 50 + "\n"
|
||||
seen = set()
|
||||
for alt, url in images:
|
||||
if url not in seen:
|
||||
seen.add(url)
|
||||
result += f" {alt[:25]:25s} → {url}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_frontmatter(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件"
|
||||
content, source = self._load_content(args[0])
|
||||
|
||||
match = re.match(r'^---\n(.*?)\n---', content, re.DOTALL)
|
||||
if not match:
|
||||
return f"📭 未找到 frontmatter ({source})"
|
||||
|
||||
fm = match.group(1)
|
||||
result = f" Frontmatter ({source}):\n"
|
||||
result += "─" * 35 + "\n"
|
||||
result += fm
|
||||
return result
|
||||
|
||||
def _op_format(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件或内容"
|
||||
content, source = self._load_content(args[0])
|
||||
lines = content.split('\n')
|
||||
formatted = []
|
||||
prev_blank = False
|
||||
|
||||
for line in lines:
|
||||
stripped = line.strip()
|
||||
# 确保标题前后有空行
|
||||
if re.match(r'^#{1,6}\s', stripped):
|
||||
if formatted and not prev_blank:
|
||||
formatted.append('')
|
||||
formatted.append(stripped)
|
||||
prev_blank = False
|
||||
continue
|
||||
# 确保列表项格式一致
|
||||
if re.match(r'^[-*]\s', stripped):
|
||||
formatted.append(re.sub(r'^[-*]\s', '- ', stripped))
|
||||
prev_blank = False
|
||||
continue
|
||||
# 去除多余空行
|
||||
if not stripped:
|
||||
if not prev_blank:
|
||||
formatted.append('')
|
||||
prev_blank = True
|
||||
continue
|
||||
|
||||
prev_blank = False
|
||||
formatted.append(line.rstrip())
|
||||
|
||||
# 确保文件末尾有换行
|
||||
while formatted and not formatted[-1]:
|
||||
formatted.pop()
|
||||
formatted.append('')
|
||||
|
||||
return f"✅ 格式化完成 ({source}):\n" + '\n'.join(formatted)
|
||||
|
||||
def _op_wordcount(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件或内容"
|
||||
content, source = self._load_content(args[0])
|
||||
|
||||
# 移除代码块
|
||||
no_code = re.sub(r'```[\s\S]*?```', '', content)
|
||||
# 移除行内代码
|
||||
no_code = re.sub(r'`[^`]+`', '', no_code)
|
||||
# 移除链接标记
|
||||
no_code = re.sub(r'\[([^\]]+)\]\([^)]+\)', r'\1', no_code)
|
||||
# 移除图片标记
|
||||
no_code = re.sub(r'!\[([^\]]*)\]\([^)]+\)', '', no_code)
|
||||
# 移除标题标记
|
||||
no_code = re.sub(r'^#{1,6}\s+', '', no_code, flags=re.MULTILINE)
|
||||
# 移除加粗/斜体标记
|
||||
no_code = re.sub(r'\*{1,2}([^*]+)\*{1,2}', r'\1', no_code)
|
||||
|
||||
chinese = len(re.findall(r'[\u4e00-\u9fff]', no_code))
|
||||
english_words = re.findall(r'[a-zA-Z]+', no_code)
|
||||
english = len(english_words)
|
||||
total_chars = len(no_code.strip())
|
||||
|
||||
# 阅读时间估算(中文 300字/分钟, 英文 200词/分钟)
|
||||
read_time_cn = chinese / 300
|
||||
read_time_en = english / 200
|
||||
read_time = read_time_cn + read_time_en
|
||||
|
||||
result = f" 详细字数统计 ({source}):\n"
|
||||
result += "─" * 35 + "\n"
|
||||
result += f" 中文字数: {chinese}\n"
|
||||
result += f" 英文单词: {english}\n"
|
||||
result += f" 总字符数: {total_chars}\n"
|
||||
result += f" 预计阅读: {read_time:.1f} 分钟\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_outline(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件或内容"
|
||||
content, source = self._load_content(args[0])
|
||||
headings = self._extract_headings(content)
|
||||
|
||||
if not headings:
|
||||
return "📭 未找到标题"
|
||||
|
||||
result = f" 大纲视图 ({source}):\n"
|
||||
result += "═" * 40 + "\n"
|
||||
for level, text, _ in headings:
|
||||
if level == 1:
|
||||
result += f"\n {text}\n"
|
||||
result += "─" * 30 + "\n"
|
||||
elif level == 2:
|
||||
result += f" {text}\n"
|
||||
elif level == 3:
|
||||
result += f" {text}\n"
|
||||
else:
|
||||
result += f" {'·' * (level - 3)} {text}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_lint(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供 Markdown 文件"
|
||||
file_path = args[0]
|
||||
if not os.path.isfile(file_path):
|
||||
return f"❌ 文件不存在: {file_path}"
|
||||
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
lines = content.split('\n')
|
||||
|
||||
issues = []
|
||||
in_code = False
|
||||
prev_blank = False
|
||||
heading_levels = []
|
||||
|
||||
for i, line in enumerate(lines, 1):
|
||||
stripped = line.strip()
|
||||
|
||||
# 代码块跟踪
|
||||
if stripped.startswith('```'):
|
||||
in_code = not in_code
|
||||
continue
|
||||
if in_code:
|
||||
continue
|
||||
|
||||
# 检查标题格式
|
||||
heading_match = re.match(r'^(#{1,6})\s+(.+)', line)
|
||||
if heading_match:
|
||||
level = len(heading_match.group(1))
|
||||
text = heading_match.group(2)
|
||||
# 检查标题前后空行
|
||||
if i > 1 and lines[i - 2].strip() and not lines[i - 2].startswith('#'):
|
||||
issues.append(f" L{i:4d} 标题前缺少空行")
|
||||
# 检查标题层级跳跃
|
||||
if heading_levels and level > heading_levels[-1] + 1:
|
||||
issues.append(f" L{i:4d} 标题层级跳跃 ({heading_levels[-1]} → {level})")
|
||||
heading_levels.append(level)
|
||||
# 检查标题末尾空格
|
||||
if text != text.rstrip():
|
||||
issues.append(f" L{i:4d} 标题末尾有多余空格")
|
||||
else:
|
||||
# 检查行尾空格
|
||||
if line != line.rstrip() and stripped:
|
||||
trailing = len(line) - len(line.rstrip())
|
||||
if trailing >= 2: # 2个以上空格是换行符
|
||||
pass # 这是合法的 Markdown 换行
|
||||
elif trailing > 0:
|
||||
issues.append(f" L{i:4d} 行尾有多余空格")
|
||||
|
||||
# 检查连续空行
|
||||
if not stripped:
|
||||
if prev_blank:
|
||||
issues.append(f" L{i:4d} 连续空行")
|
||||
prev_blank = True
|
||||
else:
|
||||
prev_blank = False
|
||||
|
||||
# 检查列表格式
|
||||
list_match = re.match(r'^(\s*)[-*+]\s', line)
|
||||
if list_match:
|
||||
indent = len(list_match.group(1))
|
||||
if indent % 2 != 0 and indent > 0:
|
||||
issues.append(f" L{i:4d} 列表缩进不是2的倍数")
|
||||
|
||||
# 检查链接格式
|
||||
if re.search(r'\[[^\]]*\]\(\s*\)', line):
|
||||
issues.append(f" L{i:4d} 链接URL为空")
|
||||
|
||||
# 检查重复词
|
||||
words = stripped.lower().split()
|
||||
for j in range(len(words) - 1):
|
||||
if len(words[j]) > 2 and words[j] == words[j + 1]:
|
||||
issues.append(f" L{i:4d} 重复词: '{words[j]}'")
|
||||
|
||||
result = f" ️ Markdown 语法检查 ({file_path}):\n"
|
||||
result += "─" * 45 + "\n"
|
||||
|
||||
if not issues:
|
||||
result += " 未发现问题!\n"
|
||||
else:
|
||||
result += f" 发现 {len(issues)} 个问题:\n\n"
|
||||
for issue in issues[:50]:
|
||||
result += f" {issue}\n"
|
||||
if len(issues) > 50:
|
||||
result += f"\n ... 还有 {len(issues) - 50} 个问题\n"
|
||||
|
||||
return result.rstrip()
|
||||
|
||||
|
||||
def test_plugin():
|
||||
plugin = Liugin()
|
||||
print("Markdown 工具测试:")
|
||||
print(plugin.handle('headings "# Hello\\n## World\\n### Sub"'))
|
||||
print(plugin.handle('stats "# Hello\\n\\nThis is a test."'))
|
||||
print(plugin.handle('table \'[{"name":"Alice","age":25},{"name":"Bob","age":30}]\''))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_plugin()
|
||||
@@ -13,7 +13,7 @@ sys.path.insert(0, project_root)
|
||||
from xcli_core.memory import MemoryManager
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""持久化记忆系统 — AI 可写日记、搜索记忆、管理聊天记录"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
@@ -0,0 +1,442 @@
|
||||
"""
|
||||
正则表达式测试器插件 - 测试、调试、管理正则表达式
|
||||
支持匹配测试、提取、替换、常用模式库
|
||||
"""
|
||||
|
||||
import re
|
||||
import json
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
|
||||
class Liugin:
|
||||
"""正则表达式测试器 - 测试、调试、管理正则"""
|
||||
|
||||
def __init__(self):
|
||||
self.usage = """正则表达式测试器
|
||||
|
||||
操作:
|
||||
test <正则> <文本> [-f flags] - 测试正则是否匹配
|
||||
match <正则> <文本> [-f flags] - 提取所有匹配项
|
||||
replace <正则> <文本> <替换> - 测试替换
|
||||
groups <正则> <文本> - 显示分组信息
|
||||
explain <正则> - 解释正则含义
|
||||
flags <正则> <文本> - 测试不同标志组合
|
||||
pattern [名称] - 列出/查看常用模式
|
||||
save <名称> <正则> [-d 描述] - 保存自定义模式
|
||||
delete_pattern <名称> - 删除自定义模式
|
||||
bench <正则> <文本> [-n 次数] - 性能测试
|
||||
|
||||
标志 (flags):
|
||||
i - 忽略大小写 m - 多行模式 s - 点匹配换行
|
||||
x - 详细模式 a - ASCII模式
|
||||
|
||||
示例:
|
||||
regex_tester test "\\d+" "abc123def456"
|
||||
regex_tester match "[a-z]+@[a-z]+\\.[a-z]+" "test@example.com foo@bar.org"
|
||||
regex_tester groups "(\\w+)@(\\w+)\\.(\\w+)" "user@domain.com"
|
||||
regex_tester explain "[a-z]+@\\w+\\.\\w+"
|
||||
regex_tester pattern email
|
||||
"""
|
||||
self.cli = None
|
||||
self.patterns_file = "regex_patterns.json"
|
||||
self.custom_patterns = self._load_patterns()
|
||||
|
||||
def set_cli(self, cli):
|
||||
self.cli = cli
|
||||
|
||||
def get_tool_info(self):
|
||||
return {
|
||||
"name": "regex_tester",
|
||||
"description": "正则表达式测试器 - 测试匹配、提取、替换、解释、常用模式库",
|
||||
"keywords": ["正则", "regex", "匹配", "表达式", "测试", "替换", "提取", "模式"],
|
||||
"usage": self.usage
|
||||
}
|
||||
|
||||
def get_mcp_definition(self):
|
||||
return {
|
||||
"name": "regex_tester",
|
||||
"description": "正则表达式测试器,支持匹配测试、提取、替换、解释、性能测试",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"operation": {
|
||||
"type": "string",
|
||||
"enum": ["test", "match", "replace", "groups", "explain",
|
||||
"flags", "pattern", "save", "bench"],
|
||||
"description": "操作类型"
|
||||
},
|
||||
"pattern": {"type": "string", "description": "正则表达式"},
|
||||
"text": {"type": "string", "description": "测试文本"},
|
||||
"replacement": {"type": "string", "description": "替换文本"},
|
||||
"flags": {"type": "string", "description": "标志 (i/m/s/x)"},
|
||||
"name": {"type": "string", "description": "模式名称"},
|
||||
"description": {"type": "string", "description": "模式描述"},
|
||||
"count": {"type": "integer", "description": "测试次数"}
|
||||
},
|
||||
"required": ["operation"]
|
||||
}
|
||||
}
|
||||
|
||||
def convert_mcp_args(self, arguments):
|
||||
op = arguments.get("operation", "")
|
||||
pattern = arguments.get("pattern", "")
|
||||
text = arguments.get("text", "")
|
||||
return f"{op} {pattern} {text}".strip()
|
||||
|
||||
def _load_patterns(self):
|
||||
try:
|
||||
if os.path.exists(self.patterns_file):
|
||||
with open(self.patterns_file, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
pass
|
||||
return self._default_patterns()
|
||||
|
||||
def _save_patterns(self):
|
||||
try:
|
||||
with open(self.patterns_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.custom_patterns, f, ensure_ascii=False, indent=2)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _default_patterns(self):
|
||||
return {
|
||||
"email": {"pattern": r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", "desc": "邮箱地址"},
|
||||
"phone_cn": {"pattern": r"1[3-9]\d{9}", "desc": "中国手机号"},
|
||||
"url": {"pattern": r"https?://[^\s<>\"']+|www\.[^\s<>\"']+", "desc": "URL链接"},
|
||||
"ipv4": {"pattern": r"\b(?:\d{1,3}\.){3}\d{1,3}\b", "desc": "IPv4地址"},
|
||||
"date": {"pattern": r"\d{4}[-/]\d{1,2}[-/]\d{1,2}", "desc": "日期格式"},
|
||||
"time": {"pattern": r"\d{2}:\d{2}(:\d{2})?", "desc": "时间格式"},
|
||||
"chinese": {"pattern": r"[\u4e00-\u9fff]+", "desc": "中文字符"},
|
||||
"id_card": {"pattern": r"\d{17}[\dXx]", "desc": "身份证号"},
|
||||
"html_tag": {"pattern": r"<[^>]+>", "desc": "HTML标签"},
|
||||
"ip_port": {"pattern": r"\b(?:\d{1,3}\.){3}\d{1,3}:\d{1,5}\b", "desc": "IP:端口"},
|
||||
"mac_addr": {"pattern": r"([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}", "desc": "MAC地址"},
|
||||
"hex_color": {"pattern": r"#[0-9A-Fa-f]{3,8}", "desc": "十六进制颜色"},
|
||||
"number": {"pattern": r"-?\d+\.?\d*", "desc": "数字(整数/浮点)"},
|
||||
"word": {"pattern": r"\b\w+\b", "desc": "单词"},
|
||||
}
|
||||
|
||||
def handle(self, args: str) -> str:
|
||||
try:
|
||||
parts = self._parse_args(args)
|
||||
if not parts:
|
||||
return "错误:请提供操作类型"
|
||||
|
||||
operation = parts[0].lower()
|
||||
handlers = {
|
||||
"test": self._op_test,
|
||||
"match": self._op_match,
|
||||
"replace": self._op_replace,
|
||||
"groups": self._op_groups,
|
||||
"explain": self._op_explain,
|
||||
"flags": self._op_flags,
|
||||
"pattern": self._op_pattern,
|
||||
"save": self._op_save,
|
||||
"delete_pattern": self._op_delete_pattern,
|
||||
"bench": self._op_bench,
|
||||
}
|
||||
|
||||
handler = handlers.get(operation)
|
||||
if not handler:
|
||||
return f"错误:不支持的操作 '{operation}'"
|
||||
return handler(parts[1:])
|
||||
|
||||
except Exception as e:
|
||||
return f"正则测试错误: {str(e)}"
|
||||
|
||||
def _parse_args(self, args: str) -> List[str]:
|
||||
parts, current, in_quotes, qc = [], "", False, None
|
||||
for ch in args:
|
||||
if ch in ('"', "'") and not in_quotes:
|
||||
in_quotes, qc = True, ch
|
||||
elif ch == qc and in_quotes:
|
||||
in_quotes, qc = False, None
|
||||
elif ch == ' ' and not in_quotes:
|
||||
if current:
|
||||
parts.append(current)
|
||||
current = ""
|
||||
else:
|
||||
current += ch
|
||||
if current:
|
||||
parts.append(current)
|
||||
return parts
|
||||
|
||||
def _parse_flags(self, flags_str: str) -> int:
|
||||
flags = 0
|
||||
for c in flags_str.lower():
|
||||
if c == 'i': flags |= re.IGNORECASE
|
||||
elif c == 'm': flags |= re.MULTILINE
|
||||
elif c == 's': flags |= re.DOTALL
|
||||
elif c == 'x': flags |= re.VERBOSE
|
||||
return flags
|
||||
|
||||
def _op_test(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:格式: test <正则> <文本> [-f 标志]"
|
||||
pattern_str, text = args[0], args[1]
|
||||
flags = 0
|
||||
for i, a in enumerate(args):
|
||||
if a == "-f" and i + 1 < len(args):
|
||||
flags = self._parse_flags(args[i + 1])
|
||||
|
||||
try:
|
||||
pattern = re.compile(pattern_str, flags)
|
||||
match = pattern.search(text)
|
||||
if match:
|
||||
return (f"✅ 匹配成功!\n"
|
||||
f" 正则: {pattern_str}\n"
|
||||
f" 位置: {match.start()}-{match.end()}\n"
|
||||
f" 匹配: '{match.group()}'\n"
|
||||
f" 全文: {pattern.findall(text)}")
|
||||
else:
|
||||
return f"❌ 无匹配\n 正则: {pattern_str}\n 文本: {text[:100]}"
|
||||
except re.error as e:
|
||||
return f"❌ 正则语法错误: {e}"
|
||||
|
||||
def _op_match(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:格式: match <正则> <文本>"
|
||||
pattern_str, text = args[0], args[1]
|
||||
flags = 0
|
||||
for i, a in enumerate(args):
|
||||
if a == "-f" and i + 1 < len(args):
|
||||
flags = self._parse_flags(args[i + 1])
|
||||
|
||||
try:
|
||||
pattern = re.compile(pattern_str, flags)
|
||||
matches = pattern.finditer(text)
|
||||
|
||||
results = []
|
||||
for m in matches:
|
||||
results.append(f" [{m.start():3d}-{m.end():3d}] '{m.group()}'")
|
||||
|
||||
if results:
|
||||
return f" 找到 {len(results)} 个匹配:\n" + "\n".join(results)
|
||||
else:
|
||||
return "❌ 未找到匹配"
|
||||
except re.error as e:
|
||||
return f"❌ 正则语法错误: {e}"
|
||||
|
||||
def _op_replace(self, args: List[str]) -> str:
|
||||
if len(args) < 3:
|
||||
return "错误:格式: replace <正则> <文本> <替换>"
|
||||
pattern_str, text, replacement = args[0], args[1], args[2]
|
||||
|
||||
try:
|
||||
result = re.sub(pattern_str, replacement, text)
|
||||
count = len(re.findall(pattern_str, text))
|
||||
return (f" 替换结果 ({count} 处):\n"
|
||||
f" 原文: {text}\n"
|
||||
f" 结果: {result}")
|
||||
except re.error as e:
|
||||
return f"❌ 正则语法错误: {e}"
|
||||
|
||||
def _op_groups(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:格式: groups <正则> <文本>"
|
||||
pattern_str, text = args[0], args[1]
|
||||
|
||||
try:
|
||||
pattern = re.compile(pattern_str)
|
||||
match = pattern.search(text)
|
||||
if not match:
|
||||
return "❌ 无匹配"
|
||||
|
||||
result = f" 分组信息:\n"
|
||||
result += f" 完整匹配: '{match.group()}' (span: {match.span()})\n"
|
||||
for i, group in enumerate(match.groups(), 1):
|
||||
result += f" 分组 {i}: '{group}' (span: {match.span(i) if group else 'N/A'})\n"
|
||||
if match.groupdict():
|
||||
result += f" 命名分组:\n"
|
||||
for name, value in match.groupdict().items():
|
||||
result += f" {name}: '{value}'\n"
|
||||
return result.rstrip()
|
||||
except re.error as e:
|
||||
return f"❌ 正则语法错误: {e}"
|
||||
|
||||
def _op_explain(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供正则表达式"
|
||||
pattern_str = args[0]
|
||||
|
||||
explanations = []
|
||||
i = 0
|
||||
while i < len(pattern_str):
|
||||
ch = pattern_str[i]
|
||||
|
||||
if ch == '\\':
|
||||
if i + 1 < len(pattern_str):
|
||||
next_ch = pattern_str[i + 1]
|
||||
escape_map = {
|
||||
'd': '数字 [0-9]', 'D': '非数字', 'w': '字母/数字/下划线',
|
||||
'W': '非字母数字下划线', 's': '空白字符', 'S': '非空白字符',
|
||||
'b': '单词边界', 'B': '非单词边界', 'n': '换行', 't': '制表符',
|
||||
}
|
||||
if next_ch in escape_map:
|
||||
explanations.append(('\\' + next_ch, escape_map[next_ch]))
|
||||
else:
|
||||
explanations.append(('\\' + next_ch, f'转义字符 {next_ch}'))
|
||||
i += 2
|
||||
continue
|
||||
|
||||
if ch == '.':
|
||||
explanations.append(('.', '任意字符(除换行)'))
|
||||
elif ch == '^':
|
||||
explanations.append(('^', '行/字符串开头'))
|
||||
elif ch == '$':
|
||||
explanations.append(('$', '行/字符串结尾'))
|
||||
elif ch == '*':
|
||||
explanations.append(('*', '0次或多次'))
|
||||
elif ch == '+':
|
||||
explanations.append(('+', '1次或多次'))
|
||||
elif ch == '?':
|
||||
explanations.append(('?', '0次或1次'))
|
||||
elif ch == '{':
|
||||
end = pattern_str.find('}', i)
|
||||
if end != -1:
|
||||
quantifier = pattern_str[i:end + 1]
|
||||
explanations.append((quantifier, f'量词: {quantifier}'))
|
||||
i = end + 1
|
||||
continue
|
||||
elif ch == '[':
|
||||
end = pattern_str.find(']', i)
|
||||
if end != -1:
|
||||
char_class = pattern_str[i:end + 1]
|
||||
explanations.append((char_class, f'字符类: {char_class}'))
|
||||
i = end + 1
|
||||
continue
|
||||
elif ch == '(':
|
||||
if i + 1 < len(pattern_str) and pattern_str[i + 1] == '?':
|
||||
if i + 2 < len(pattern_str):
|
||||
special = pattern_str[i + 2]
|
||||
if special == ':':
|
||||
explanations.append(('(?:', '非捕获组'))
|
||||
elif special == '=':
|
||||
explanations.append(('(?=', '正向前瞻'))
|
||||
elif special == '!':
|
||||
explanations.append(('(?!', '负向前瞻'))
|
||||
i += 3
|
||||
continue
|
||||
explanations.append(('(', '捕获组开始'))
|
||||
elif ch == ')':
|
||||
explanations.append((')', '组结束'))
|
||||
elif ch == '|':
|
||||
explanations.append(('|', '或 (交替)'))
|
||||
|
||||
i += 1
|
||||
|
||||
result = f" 正则解释: {pattern_str}\n"
|
||||
result += "─" * 45 + "\n"
|
||||
for token, meaning in explanations:
|
||||
result += f" {token:12s} → {meaning}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_flags(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:格式: flags <正则> <文本>"
|
||||
pattern_str, text = args[0], args[1]
|
||||
|
||||
flag_combos = [
|
||||
("", 0, "默认"),
|
||||
("i", re.IGNORECASE, "忽略大小写"),
|
||||
("m", re.MULTILINE, "多行模式"),
|
||||
("s", re.DOTALL, "点匹配换行"),
|
||||
("im", re.IGNORECASE | re.MULTILINE, "忽略大小写+多行"),
|
||||
]
|
||||
|
||||
result = f" ️ 标志测试: {pattern_str}\n"
|
||||
result += "─" * 45 + "\n"
|
||||
for flag_str, flag_val, desc in flag_combos:
|
||||
try:
|
||||
matches = re.findall(pattern_str, text, flag_val)
|
||||
count = len(matches) if isinstance(matches, list) else 1
|
||||
result += f" [{flag_str or '-':3s}] {desc:12s} → {count} 个匹配: {matches[:3]}\n"
|
||||
except re.error as e:
|
||||
result += f" [{flag_str or '-':3s}] {desc:12s} → 错误: {e}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_pattern(self, args: List[str]) -> str:
|
||||
if args:
|
||||
name = args[0]
|
||||
if name in self.custom_patterns:
|
||||
p = self.custom_patterns[name]
|
||||
return f" 模式 '{name}':\n 正则: {p['pattern']}\n 描述: {p.get('desc', '')}"
|
||||
return f"❌ 未找到模式: {name}"
|
||||
|
||||
result = " 常用正则模式:\n"
|
||||
result += "─" * 50 + "\n"
|
||||
for name, p in self.custom_patterns.items():
|
||||
result += f" {name:15s} {p['pattern'][:35]:35s} {p.get('desc', '')}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_save(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:格式: save <名称> <正则> [-d 描述]"
|
||||
name, pattern_str = args[0], args[1]
|
||||
desc = ""
|
||||
for i, a in enumerate(args):
|
||||
if a == "-d" and i + 1 < len(args):
|
||||
desc = args[i + 1]
|
||||
|
||||
# 验证正则
|
||||
try:
|
||||
re.compile(pattern_str)
|
||||
except re.error as e:
|
||||
return f"❌ 正则语法错误: {e}"
|
||||
|
||||
self.custom_patterns[name] = {"pattern": pattern_str, "desc": desc}
|
||||
self._save_patterns()
|
||||
return f"✅ 已保存模式 '{name}': {pattern_str}"
|
||||
|
||||
def _op_delete_pattern(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供模式名称"
|
||||
name = args[0]
|
||||
if name in self.custom_patterns:
|
||||
del self.custom_patterns[name]
|
||||
self._save_patterns()
|
||||
return f" ️ 已删除模式: {name}"
|
||||
return f"❌ 未找到模式: {name}"
|
||||
|
||||
def _op_bench(self, args: List[str]) -> str:
|
||||
if len(args) < 2:
|
||||
return "错误:格式: bench <正则> <文本> [-n 次数]"
|
||||
pattern_str, text = args[0], args[1]
|
||||
count = 10000
|
||||
for i, a in enumerate(args):
|
||||
if a == "-n" and i + 1 < len(args):
|
||||
count = int(args[i + 1])
|
||||
|
||||
try:
|
||||
import time
|
||||
pattern = re.compile(pattern_str)
|
||||
|
||||
start = time.perf_counter()
|
||||
for _ in range(count):
|
||||
pattern.findall(text)
|
||||
elapsed = time.perf_counter() - start
|
||||
|
||||
per_op = elapsed / count * 1_000_000 # 微秒
|
||||
return (f"⏱ 性能测试:\n"
|
||||
f" 正则: {pattern_str}\n"
|
||||
f" 次数: {count}\n"
|
||||
f" 总耗时: {elapsed:.3f}s\n"
|
||||
f" 每次: {per_op:.2f}μs\n"
|
||||
f" 吞吐: {count / elapsed:.0f} ops/s")
|
||||
except re.error as e:
|
||||
return f"❌ 正则语法错误: {e}"
|
||||
|
||||
|
||||
def test_plugin():
|
||||
plugin = Liugin()
|
||||
print("正则表达式测试器测试:")
|
||||
print(plugin.handle('test "\\d+" "abc123def456"'))
|
||||
print(plugin.handle('match "[a-z]+" "hello world foo bar"'))
|
||||
print(plugin.handle('groups "(\\w+)@(\\w+)\\.(\\w+)" "user@domain.com"'))
|
||||
print(plugin.handle('explain "\\d{4}-\\d{2}-\\d{2}"'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_plugin()
|
||||
@@ -0,0 +1,428 @@
|
||||
"""
|
||||
代码片段管理器插件 - 保存、搜索、复用常用代码片段
|
||||
支持分类、标签、搜索、导出,JSON 持久化存储
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import hashlib
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
class Liugin:
|
||||
"""代码片段管理器 - 保存和复用代码片段"""
|
||||
|
||||
def __init__(self):
|
||||
self.usage = """代码片段管理器
|
||||
|
||||
操作:
|
||||
add <名称> -l <语言> -c "代码" [-t 标签1,标签2] [-d 描述]
|
||||
- 添加代码片段
|
||||
list [-l 语言] [-t 标签] - 列出片段(可按语言/标签过滤)
|
||||
get <名称或ID> - 获取片段详情
|
||||
search <关键词> - 搜索片段(名称/描述/代码/标签)
|
||||
delete <名称或ID> - 删除片段
|
||||
edit <名称或ID> -c "新代码" - 更新片段代码
|
||||
export [文件] - 导出所有片段
|
||||
import <文件> - 导入片段
|
||||
tags - 列出所有标签
|
||||
stats - 统计信息
|
||||
copy <名称或ID> - 复制到剪贴板(如可用)
|
||||
recent [-n 数量] - 最近添加的片段
|
||||
|
||||
示例:
|
||||
snippet_manager add quick_sort -l python -c "def qsort(arr): ..." -t 排序,算法
|
||||
snippet_manager list -l python
|
||||
snippet_manager search 排序
|
||||
snippet_manager get quick_sort
|
||||
"""
|
||||
self.cli = None
|
||||
self.snippets_file = "snippets.json"
|
||||
self.snippets = []
|
||||
self._load_snippets()
|
||||
|
||||
def set_cli(self, cli):
|
||||
self.cli = cli
|
||||
|
||||
def get_tool_info(self):
|
||||
return {
|
||||
"name": "snippet_manager",
|
||||
"description": "代码片段管理器 - 保存、搜索、复用常用代码片段,支持分类和标签",
|
||||
"keywords": ["代码", "片段", "snippet", "模板", "复用", "保存", "搜索", "代码片段"],
|
||||
"usage": self.usage
|
||||
}
|
||||
|
||||
def get_mcp_definition(self):
|
||||
return {
|
||||
"name": "snippet_manager",
|
||||
"description": "代码片段管理器,支持添加、搜索、获取、删除代码片段",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"operation": {
|
||||
"type": "string",
|
||||
"enum": ["add", "list", "get", "search", "delete",
|
||||
"edit", "export", "import", "tags", "stats", "recent"],
|
||||
"description": "操作类型"
|
||||
},
|
||||
"name": {"type": "string", "description": "片段名称或ID"},
|
||||
"language": {"type": "string", "description": "编程语言"},
|
||||
"code": {"type": "string", "description": "代码内容"},
|
||||
"tags": {"type": "string", "description": "标签(逗号分隔)"},
|
||||
"description": {"type": "string", "description": "描述"},
|
||||
"keyword": {"type": "string", "description": "搜索关键词"},
|
||||
"file": {"type": "string", "description": "文件路径"},
|
||||
"count": {"type": "integer", "description": "数量"}
|
||||
},
|
||||
"required": ["operation"]
|
||||
}
|
||||
}
|
||||
|
||||
def convert_mcp_args(self, arguments):
|
||||
op = arguments.get("operation", "")
|
||||
name = arguments.get("name", "")
|
||||
return f"{op} {name}".strip()
|
||||
|
||||
def _load_snippets(self):
|
||||
try:
|
||||
if os.path.exists(self.snippets_file):
|
||||
with open(self.snippets_file, 'r', encoding='utf-8') as f:
|
||||
self.snippets = json.load(f)
|
||||
except Exception:
|
||||
self.snippets = []
|
||||
|
||||
def _save_snippets(self):
|
||||
try:
|
||||
with open(self.snippets_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.snippets, f, ensure_ascii=False, indent=2)
|
||||
return None
|
||||
except Exception as e:
|
||||
return f"保存失败: {e}"
|
||||
|
||||
def _gen_id(self, name: str) -> str:
|
||||
return hashlib.md5(f"{name}{time.time()}".encode()).hexdigest()[:8]
|
||||
|
||||
def handle(self, args: str) -> str:
|
||||
try:
|
||||
parts = self._parse_args(args)
|
||||
if not parts:
|
||||
return self._op_list([])
|
||||
|
||||
operation = parts[0].lower()
|
||||
handlers = {
|
||||
"add": self._op_add,
|
||||
"list": self._op_list,
|
||||
"get": self._op_get,
|
||||
"search": self._op_search,
|
||||
"delete": self._op_delete,
|
||||
"edit": self._op_edit,
|
||||
"export": self._op_export,
|
||||
"import": self._op_import,
|
||||
"tags": self._op_tags,
|
||||
"stats": self._op_stats,
|
||||
"recent": self._op_recent,
|
||||
"copy": self._op_copy,
|
||||
}
|
||||
|
||||
handler = handlers.get(operation)
|
||||
if not handler:
|
||||
return f"错误:不支持的操作 '{operation}'"
|
||||
return handler(parts[1:])
|
||||
|
||||
except Exception as e:
|
||||
return f"片段管理错误: {str(e)}"
|
||||
|
||||
def _parse_args(self, args: str) -> List[str]:
|
||||
parts, current, in_quotes, qc = [], "", False, None
|
||||
for ch in args:
|
||||
if ch in ('"', "'") and not in_quotes:
|
||||
in_quotes, qc = True, ch
|
||||
elif ch == qc and in_quotes:
|
||||
in_quotes, qc = False, None
|
||||
elif ch == ' ' and not in_quotes:
|
||||
if current:
|
||||
parts.append(current)
|
||||
current = ""
|
||||
else:
|
||||
current += ch
|
||||
if current:
|
||||
parts.append(current)
|
||||
return parts
|
||||
|
||||
def _op_add(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供片段名称"
|
||||
name = args[0]
|
||||
language, code, tags, desc = "", "", [], ""
|
||||
|
||||
i = 1
|
||||
while i < len(args):
|
||||
if args[i] == "-l" and i + 1 < len(args):
|
||||
language = args[i + 1]; i += 1
|
||||
elif args[i] == "-c" and i + 1 < len(args):
|
||||
code = args[i + 1]; i += 1
|
||||
elif args[i] == "-t" and i + 1 < len(args):
|
||||
tags = [t.strip() for t in args[i + 1].split(",") if t.strip()]; i += 1
|
||||
elif args[i] == "-d" and i + 1 < len(args):
|
||||
desc = args[i + 1]; i += 1
|
||||
i += 1
|
||||
|
||||
if not code:
|
||||
return "错误:请用 -c 提供代码内容"
|
||||
|
||||
# 检查重名
|
||||
for s in self.snippets:
|
||||
if s["name"] == name:
|
||||
return f"错误:片段 '{name}' 已存在,用 edit 更新"
|
||||
|
||||
snippet = {
|
||||
"id": self._gen_id(name),
|
||||
"name": name,
|
||||
"language": language,
|
||||
"code": code,
|
||||
"tags": tags,
|
||||
"description": desc,
|
||||
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"usage_count": 0
|
||||
}
|
||||
self.snippets.append(snippet)
|
||||
err = self._save_snippets()
|
||||
if err:
|
||||
return err
|
||||
return f"✅ 已添加片段 '{name}' (ID: {snippet['id']}, 语言: {language or '未指定'})"
|
||||
|
||||
def _op_list(self, args: List[str]) -> str:
|
||||
lang_filter, tag_filter = None, None
|
||||
i = 0
|
||||
while i < len(args):
|
||||
if args[i] == "-l" and i + 1 < len(args):
|
||||
lang_filter = args[i + 1].lower(); i += 1
|
||||
elif args[i] == "-t" and i + 1 < len(args):
|
||||
tag_filter = args[i + 1].lower(); i += 1
|
||||
i += 1
|
||||
|
||||
filtered = self.snippets
|
||||
if lang_filter:
|
||||
filtered = [s for s in filtered if s.get("language", "").lower() == lang_filter]
|
||||
if tag_filter:
|
||||
filtered = [s for s in filtered if any(tag_filter in t.lower() for t in s.get("tags", []))]
|
||||
|
||||
if not filtered:
|
||||
return "📭 没有找到代码片段"
|
||||
|
||||
result = f"\n📝 代码片段列表 (共 {len(filtered)} 个)\n"
|
||||
result += "─" * 55 + "\n"
|
||||
for s in filtered:
|
||||
lang = s.get("language", "未知")
|
||||
tags = ", ".join(s.get("tags", [])) if s.get("tags") else ""
|
||||
code_preview = s.get("code", "")[:60].replace("\n", "↵")
|
||||
result += f" [{s['id']}] {s['name']} ({lang})\n"
|
||||
if tags:
|
||||
result += f" ️ {tags}\n"
|
||||
result += f" {code_preview}...\n"
|
||||
result += f" ⏱ {s.get('created_at', '未知')}\n\n"
|
||||
|
||||
result += "─" * 55
|
||||
return result
|
||||
|
||||
def _op_get(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供片段名称或ID"
|
||||
key = args[0]
|
||||
snippet = self._find_snippet(key)
|
||||
if not snippet:
|
||||
return f"❌ 未找到片段: {key}"
|
||||
|
||||
snippet["usage_count"] = snippet.get("usage_count", 0) + 1
|
||||
self._save_snippets()
|
||||
|
||||
result = f"\n 片段: {snippet['name']}\n"
|
||||
result += "─" * 50 + "\n"
|
||||
result += f" ID: {snippet['id']}\n"
|
||||
result += f" 语言: {snippet.get('language', '未指定')}\n"
|
||||
if snippet.get("tags"):
|
||||
result += f" ️ 标签: {', '.join(snippet['tags'])}\n"
|
||||
if snippet.get("description"):
|
||||
result += f" 描述: {snippet['description']}\n"
|
||||
result += f" 创建: {snippet.get('created_at', '未知')}\n"
|
||||
result += f" 使用: {snippet.get('usage_count', 0)} 次\n"
|
||||
result += "─" * 50 + "\n"
|
||||
result += f" 代码:\n{snippet['code']}\n"
|
||||
result += "─" * 50
|
||||
return result
|
||||
|
||||
def _op_search(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供搜索关键词"
|
||||
keyword = " ".join(args).lower()
|
||||
|
||||
results = []
|
||||
for s in self.snippets:
|
||||
searchable = f"{s['name']} {s.get('description', '')} {s.get('code', '')} {' '.join(s.get('tags', []))} {s.get('language', '')}".lower()
|
||||
if keyword in searchable:
|
||||
results.append(s)
|
||||
|
||||
if not results:
|
||||
return f" 未找到匹配 '{keyword}' 的片段"
|
||||
|
||||
output = f" 搜索结果 ({len(results)} 个):\n"
|
||||
output += "─" * 50 + "\n"
|
||||
for s in results:
|
||||
lang = s.get("language", "")
|
||||
output += f" [{s['id']}] {s['name']} ({lang}) - {s.get('description', '')[:40]}\n"
|
||||
return output.rstrip()
|
||||
|
||||
def _op_delete(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供片段名称或ID"
|
||||
key = args[0]
|
||||
for i, s in enumerate(self.snippets):
|
||||
if s["name"] == key or s["id"] == key:
|
||||
removed = self.snippets.pop(i)
|
||||
self._save_snippets()
|
||||
return f" ️ 已删除片段: {removed['name']}"
|
||||
return f"❌ 未找到片段: {key}"
|
||||
|
||||
def _op_edit(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供片段名称或ID"
|
||||
key = args[0]
|
||||
snippet = self._find_snippet(key)
|
||||
if not snippet:
|
||||
return f"❌ 未找到片段: {key}"
|
||||
|
||||
new_code = None
|
||||
new_desc = None
|
||||
i = 1
|
||||
while i < len(args):
|
||||
if args[i] == "-c" and i + 1 < len(args):
|
||||
new_code = args[i + 1]; i += 1
|
||||
elif args[i] == "-d" and i + 1 < len(args):
|
||||
new_desc = args[i + 1]; i += 1
|
||||
i += 1
|
||||
|
||||
if new_code:
|
||||
snippet["code"] = new_code
|
||||
if new_desc:
|
||||
snippet["description"] = new_desc
|
||||
snippet["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
self._save_snippets()
|
||||
return f"✅ 已更新片段: {snippet['name']}"
|
||||
|
||||
def _op_export(self, args: List[str]) -> str:
|
||||
output_file = args[0] if args else "snippets_export.json"
|
||||
try:
|
||||
with open(output_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.snippets, f, ensure_ascii=False, indent=2)
|
||||
return f"✅ 已导出 {len(self.snippets)} 个片段到: {output_file}"
|
||||
except Exception as e:
|
||||
return f"❌ 导出失败: {e}"
|
||||
|
||||
def _op_import(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供导入文件路径"
|
||||
file_path = args[0]
|
||||
if not os.path.isfile(file_path):
|
||||
return f"❌ 文件不存在: {file_path}"
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
imported = json.load(f)
|
||||
if not isinstance(imported, list):
|
||||
return "❌ 文件格式错误:需要 JSON 数组"
|
||||
|
||||
existing_names = {s["name"] for s in self.snippets}
|
||||
added = 0
|
||||
for s in imported:
|
||||
if isinstance(s, dict) and s.get("name") and s["name"] not in existing_names:
|
||||
s["id"] = self._gen_id(s["name"])
|
||||
self.snippets.append(s)
|
||||
existing_names.add(s["name"])
|
||||
added += 1
|
||||
|
||||
self._save_snippets()
|
||||
return f"✅ 已导入 {added} 个片段 (跳过 {len(imported) - added} 个重复)"
|
||||
except Exception as e:
|
||||
return f"❌ 导入失败: {e}"
|
||||
|
||||
def _op_tags(self, args: List[str]) -> str:
|
||||
tag_count = {}
|
||||
for s in self.snippets:
|
||||
for t in s.get("tags", []):
|
||||
tag_count[t] = tag_count.get(t, 0) + 1
|
||||
|
||||
if not tag_count:
|
||||
return " ️ 暂无标签"
|
||||
|
||||
result = " ️ 标签列表:\n"
|
||||
for tag, count in sorted(tag_count.items(), key=lambda x: -x[1]):
|
||||
result += f" {tag} ({count})\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_stats(self, args: List[str]) -> str:
|
||||
total = len(self.snippets)
|
||||
if not total:
|
||||
return "📊 暂无片段"
|
||||
|
||||
lang_count = {}
|
||||
total_usage = 0
|
||||
for s in self.snippets:
|
||||
lang = s.get("language", "未知")
|
||||
lang_count[lang] = lang_count.get(lang, 0) + 1
|
||||
total_usage += s.get("usage_count", 0)
|
||||
|
||||
result = f"📊 片段统计\n"
|
||||
result += "─" * 30 + "\n"
|
||||
result += f" 总数: {total}\n"
|
||||
result += f" 总使用: {total_usage} 次\n"
|
||||
result += f" 语言分布:\n"
|
||||
for lang, count in sorted(lang_count.items(), key=lambda x: -x[1]):
|
||||
result += f" {lang}: {count}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_recent(self, args: List[str]) -> str:
|
||||
count = 5
|
||||
for i, a in enumerate(args):
|
||||
if a == "-n" and i + 1 < len(args):
|
||||
count = int(args[i + 1])
|
||||
|
||||
recent = sorted(self.snippets, key=lambda s: s.get("created_at", ""), reverse=True)[:count]
|
||||
if not recent:
|
||||
return "📭 暂无片段"
|
||||
|
||||
result = f"⏰ 最近 {len(recent)} 个片段:\n"
|
||||
for s in recent:
|
||||
result += f" [{s['id']}] {s['name']} - {s.get('created_at', '')}\n"
|
||||
return result.rstrip()
|
||||
|
||||
def _op_copy(self, args: List[str]) -> str:
|
||||
if not args:
|
||||
return "错误:请提供片段名称或ID"
|
||||
snippet = self._find_snippet(args[0])
|
||||
if not snippet:
|
||||
return f"❌ 未找到片段: {args[0]}"
|
||||
|
||||
snippet["usage_count"] = snippet.get("usage_count", 0) + 1
|
||||
self._save_snippets()
|
||||
return f" 代码内容 ({snippet['name']}):\n{snippet['code']}"
|
||||
|
||||
def _find_snippet(self, key: str) -> Optional[dict]:
|
||||
for s in self.snippets:
|
||||
if s["name"] == key or s["id"] == key:
|
||||
return s
|
||||
return None
|
||||
|
||||
|
||||
def test_plugin():
|
||||
plugin = Liugin()
|
||||
print("代码片段管理器测试:")
|
||||
print(plugin.handle('add hello -l python -c "print(\'Hello, World!\')" -t 示例,入门 -d 最简单的Python程序'))
|
||||
print(plugin.handle('add fib -l python -c "def fib(n): return n if n < 2 else fib(n-1)+fib(n-2)" -t 算法,递归'))
|
||||
print(plugin.handle('list'))
|
||||
print(plugin.handle('search python'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_plugin()
|
||||
@@ -543,7 +543,7 @@ if __name__ == "__main__":
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
plugin = Plugin()
|
||||
plugin = Liugin()
|
||||
print("语音识别插件")
|
||||
print(f"临时目录: {plugin.temp_dir}")
|
||||
print("\n" + plugin.usage)
|
||||
@@ -26,7 +26,7 @@ class SubAgent:
|
||||
self.created_at = datetime.now().strftime("%H:%M:%S")
|
||||
self.finished_at = None
|
||||
self.steps = 0 # 已执行步数
|
||||
self.max_steps = 20
|
||||
self.max_steps = float('inf') # 不限制步数
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
@@ -42,7 +42,7 @@ class SubAgent:
|
||||
}
|
||||
|
||||
|
||||
class Plugin:
|
||||
class Liugin:
|
||||
"""子 Agent 系统 - 创建、管理、协作"""
|
||||
|
||||
def __init__(self):
|
||||
@@ -238,7 +238,7 @@ class Plugin:
|
||||
|
||||
if agent.status == "running":
|
||||
agent.status = "done"
|
||||
agent.result = "已达到最大步数限制,任务暂停。"
|
||||
agent.result = "任务执行结束。"
|
||||
|
||||
except Exception as e:
|
||||
agent.status = "failed"
|
||||
@@ -369,7 +369,7 @@ class Plugin:
|
||||
f" 名称: {agent.name}",
|
||||
f" 任务: {agent.task}",
|
||||
f" 状态: {agent.status}",
|
||||
f" 步骤: {agent.steps}/{agent.max_steps}",
|
||||
f" 步骤: {agent.steps}",
|
||||
f" 创建: {agent.created_at}",
|
||||
]
|
||||
if agent.finished_at:
|
||||
@@ -395,7 +395,7 @@ class Plugin:
|
||||
return f"错误:未找到 Agent '{agent_id}'"
|
||||
|
||||
if agent.status == "running":
|
||||
return f"Agent {agent.id} 仍在运行中 (步骤 {agent.steps}/{agent.max_steps})"
|
||||
return f"Agent {agent.id} 仍在运行中 (步骤 {agent.steps})"
|
||||
|
||||
if agent.status == "failed":
|
||||
return f"Agent {agent.id} 执行失败: {agent.error}"
|
||||
|
||||
@@ -258,7 +258,7 @@ JSON格式示例:
|
||||
# 测试函数
|
||||
def test_plugin():
|
||||
"""测试插件功能"""
|
||||
plugin = Plugin()
|
||||
plugin = Liugin()
|
||||
print("任务管理插件测试:")
|
||||
|
||||
print("\n1. 添加任务:")
|
||||
|
||||
@@ -0,0 +1,804 @@
|
||||
"""
|
||||
工作流插件 - YAML 定义的多步骤自动化流程
|
||||
|
||||
支持:
|
||||
- 顺序执行多步骤
|
||||
- 步骤间变量传递 ({{prev.output}})
|
||||
- 条件分支 (on_fail / on_success)
|
||||
- 错误处理 (continue_on_error)
|
||||
- 状态持久化 (断点续跑)
|
||||
- 调用其他插件作为步骤
|
||||
|
||||
用法:
|
||||
/workflow run <file.yaml>
|
||||
/workflow list
|
||||
/workflow status <workflow_id>
|
||||
/workflow resume <workflow_id>
|
||||
/workflow abort <workflow_id>
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from colorama import Fore, Style
|
||||
|
||||
|
||||
# ── YAML 解析(内置轻量实现,不依赖 PyYAML) ──
|
||||
|
||||
def _parse_yaml(text: str) -> dict:
|
||||
"""轻量 YAML 解析器,支持工作流所需的子集"""
|
||||
import yaml # 延迟导入,失败时回退
|
||||
return yaml.safe_load(text)
|
||||
|
||||
|
||||
def _parse_yaml_fallback(text: str) -> dict:
|
||||
"""内置回退解析器,仅支持简单结构"""
|
||||
result = {}
|
||||
current_key = None
|
||||
current_list = None
|
||||
current_dict = None
|
||||
|
||||
for line in text.split('\n'):
|
||||
stripped = line.strip()
|
||||
if not stripped or stripped.startswith('#'):
|
||||
continue
|
||||
|
||||
indent = len(line) - len(line.lstrip())
|
||||
|
||||
if indent == 0 and ':' in stripped:
|
||||
key, _, val = stripped.partition(':')
|
||||
key = key.strip()
|
||||
val = val.strip()
|
||||
if val:
|
||||
result[key] = _yaml_value(val)
|
||||
else:
|
||||
result[key] = None
|
||||
current_key = key
|
||||
current_list = None
|
||||
current_dict = None
|
||||
elif indent > 0 and stripped.startswith('- '):
|
||||
if current_key:
|
||||
if not isinstance(result.get(current_key), list):
|
||||
result[current_key] = []
|
||||
item_text = stripped[2:].strip()
|
||||
if ':' in item_text:
|
||||
item = {}
|
||||
k, _, v = item_text.partition(':')
|
||||
item[k.strip()] = _yaml_value(v.strip())
|
||||
current_dict = item
|
||||
result[current_key].append(item)
|
||||
else:
|
||||
result[current_key].append(_yaml_value(item_text))
|
||||
elif indent > 0 and ':' in stripped and current_dict is not None:
|
||||
key, _, val = stripped.partition(':')
|
||||
current_dict[key.strip()] = _yaml_value(val.strip())
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def _yaml_value(s: str):
|
||||
"""转换单个 YAML 值"""
|
||||
if not s:
|
||||
return None
|
||||
# 去除引号
|
||||
if (s.startswith('"') and s.endswith('"')) or (s.startswith("'") and s.endswith("'")):
|
||||
return s[1:-1]
|
||||
if s.lower() in ('true', 'yes', 'on'):
|
||||
return True
|
||||
if s.lower() in ('false', 'no', 'off'):
|
||||
return False
|
||||
if s.lower() in ('null', 'none', '~'):
|
||||
return None
|
||||
try:
|
||||
return int(s)
|
||||
except ValueError:
|
||||
pass
|
||||
try:
|
||||
return float(s)
|
||||
except ValueError:
|
||||
pass
|
||||
return s
|
||||
|
||||
|
||||
def _load_yaml_file(path: str) -> dict:
|
||||
"""加载 YAML 文件"""
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
text = f.read()
|
||||
try:
|
||||
return _parse_yaml(text)
|
||||
except ImportError:
|
||||
return _parse_yaml_fallback(text)
|
||||
|
||||
|
||||
# ── 变量替换引擎 ──
|
||||
|
||||
def _resolve_vars(text: str, context: dict) -> str:
|
||||
"""
|
||||
替换 {{key}} 和 {{key.nested}} 形式的变量
|
||||
|
||||
支持:
|
||||
{{var}} → context['var']
|
||||
{{step.output}} → context['steps']['step']['output']
|
||||
{{step.exit_code}} → context['steps']['step']['exit_code']
|
||||
{{env.HOME}} → os.environ['HOME']
|
||||
{{args.name}} → context['args']['name']
|
||||
"""
|
||||
def replacer(match):
|
||||
path = match.group(1).strip()
|
||||
parts = path.split('.')
|
||||
|
||||
if parts[0] == 'env':
|
||||
return os.environ.get('.'.join(parts[1:]), '')
|
||||
|
||||
if parts[0] == 'args' and len(parts) > 1:
|
||||
val = context.get('args', {})
|
||||
for p in parts[1:]:
|
||||
if isinstance(val, dict):
|
||||
val = val.get(p, '')
|
||||
else:
|
||||
return ''
|
||||
return str(val)
|
||||
|
||||
if parts[0] == 'steps' and len(parts) >= 3:
|
||||
step_name = parts[1]
|
||||
attr = parts[2]
|
||||
step_data = context.get('steps', {}).get(step_name, {})
|
||||
val = step_data.get(attr, '')
|
||||
# 支持 deeper nesting: {{steps.foo.output.field}}
|
||||
for p in parts[3:]:
|
||||
if isinstance(val, dict):
|
||||
val = val.get(p, '')
|
||||
else:
|
||||
return ''
|
||||
return str(val)
|
||||
|
||||
# 顶层变量
|
||||
val = context
|
||||
for p in parts:
|
||||
if isinstance(val, dict):
|
||||
val = val.get(p, '')
|
||||
else:
|
||||
return ''
|
||||
return str(val)
|
||||
|
||||
return re.sub(r'\{\{(.+?)\}\}', replacer, text)
|
||||
|
||||
|
||||
def _resolve_value(val, context):
|
||||
"""递归解析值中的变量(支持 dict/list/str)"""
|
||||
if isinstance(val, str):
|
||||
return _resolve_vars(val, context)
|
||||
elif isinstance(val, dict):
|
||||
return {k: _resolve_value(v, context) for k, v in val.items()}
|
||||
elif isinstance(val, list):
|
||||
return [_resolve_value(item, context) for item in val]
|
||||
return val
|
||||
|
||||
|
||||
# ── 工作流引擎 ──
|
||||
|
||||
class WorkflowEngine:
|
||||
"""工作流执行引擎"""
|
||||
|
||||
def __init__(self, cli_instance=None):
|
||||
self.cli = cli_instance
|
||||
self.workflows_dir = "workflows"
|
||||
self.state_dir = os.path.join(self.workflows_dir, ".state")
|
||||
os.makedirs(self.state_dir, exist_ok=True)
|
||||
|
||||
def run(self, path: str, args: dict = None) -> str:
|
||||
"""执行工作流"""
|
||||
if not os.path.exists(path):
|
||||
return f"❌ 工作流文件不存在: {path}"
|
||||
|
||||
try:
|
||||
wf = _load_yaml_file(path)
|
||||
except Exception as e:
|
||||
return f"❌ 解析工作流文件失败: {e}"
|
||||
|
||||
wf_name = wf.get('name', os.path.basename(path).replace('.yaml', '').replace('.yml', ''))
|
||||
steps = wf.get('steps', [])
|
||||
if not steps:
|
||||
return f"❌ 工作流 '{wf_name}' 没有步骤"
|
||||
|
||||
wf_id = f"{wf_name}-{uuid.uuid4().hex[:8]}"
|
||||
context = {
|
||||
'args': args or {},
|
||||
'steps': {},
|
||||
'workflow': {
|
||||
'id': wf_id,
|
||||
'name': wf_name,
|
||||
'path': path,
|
||||
'started_at': datetime.now().isoformat(),
|
||||
}
|
||||
}
|
||||
|
||||
# 保存初始状态
|
||||
self._save_state(wf_id, {
|
||||
'workflow': wf_name,
|
||||
'path': path,
|
||||
'status': 'running',
|
||||
'current_step': 0,
|
||||
'context': context,
|
||||
'started_at': context['workflow']['started_at'],
|
||||
})
|
||||
|
||||
print(f"\n{Fore.CYAN}🔄 工作流 '{wf_name}' 开始 (ID: {wf_id}){Style.RESET_ALL}")
|
||||
print(f" 共 {len(steps)} 个步骤\n")
|
||||
|
||||
failed = False
|
||||
for i, step in enumerate(steps):
|
||||
step_name = step.get('name', f'step_{i+1}')
|
||||
step_type = step.get('type', 'tool')
|
||||
condition = step.get('if', None)
|
||||
continue_on_error = step.get('continue_on_error', False)
|
||||
|
||||
# 检查条件
|
||||
if condition:
|
||||
resolved_condition = _resolve_vars(condition, context)
|
||||
if not self._eval_condition(resolved_condition):
|
||||
print(f" {Fore.YELLOW}⏭ [{step_name}] 条件不满足,跳过{Style.RESET_ALL}")
|
||||
context['steps'][step_name] = {
|
||||
'output': '', 'exit_code': 0, 'skipped': True
|
||||
}
|
||||
continue
|
||||
|
||||
# 解析步骤参数
|
||||
resolved_step = _resolve_value(step, context)
|
||||
|
||||
print(f" {Fore.CYAN}▶ [{step_name}] {step_type}{Style.RESET_ALL}")
|
||||
|
||||
# 更新状态
|
||||
self._save_state(wf_id, {
|
||||
'workflow': wf_name,
|
||||
'path': path,
|
||||
'status': 'running',
|
||||
'current_step': i,
|
||||
'current_step_name': step_name,
|
||||
'context': context,
|
||||
'started_at': context['workflow']['started_at'],
|
||||
})
|
||||
|
||||
try:
|
||||
result = self._execute_step(resolved_step, context)
|
||||
context['steps'][step_name] = result
|
||||
|
||||
if result.get('exit_code', 0) == 0:
|
||||
output_preview = str(result.get('output', ''))[:100]
|
||||
print(f" {Fore.GREEN}✓ [{step_name}] 完成{Style.RESET_ALL}"
|
||||
f"{' → ' + output_preview if output_preview else ''}")
|
||||
else:
|
||||
failed = True
|
||||
print(f" {Fore.RED}✗ [{step_name}] 失败 (exit={result.get('exit_code')}){Style.RESET_ALL}")
|
||||
print(f" {result.get('error', result.get('output', ''))[:200]}")
|
||||
|
||||
if not continue_on_error:
|
||||
# 检查 on_fail 分支
|
||||
on_fail = resolved_step.get('on_fail')
|
||||
if on_fail:
|
||||
print(f" {Fore.YELLOW}→ 执行 on_fail 分支{Style.RESET_ALL}")
|
||||
self._execute_on_fail(on_fail, context)
|
||||
break
|
||||
else:
|
||||
print(f" {Fore.YELLOW}⚠ continue_on_error=true,继续{Style.RESET_ALL}")
|
||||
|
||||
except Exception as e:
|
||||
failed = True
|
||||
context['steps'][step_name] = {
|
||||
'output': '', 'exit_code': 1, 'error': str(e)
|
||||
}
|
||||
print(f" {Fore.RED}✗ [{step_name}] 异常: {e}{Style.RESET_ALL}")
|
||||
if not continue_on_error:
|
||||
break
|
||||
|
||||
# 完成状态
|
||||
status = 'failed' if failed else 'completed'
|
||||
context['workflow']['finished_at'] = datetime.now().isoformat()
|
||||
context['workflow']['status'] = status
|
||||
|
||||
self._save_state(wf_id, {
|
||||
'workflow': wf_name,
|
||||
'path': path,
|
||||
'status': status,
|
||||
'current_step': len(steps),
|
||||
'context': context,
|
||||
'started_at': context['workflow']['started_at'],
|
||||
'finished_at': context['workflow']['finished_at'],
|
||||
})
|
||||
|
||||
icon = '✅' if not failed else '❌'
|
||||
print(f"\n{icon} 工作流 '{wf_name}' {status} (ID: {wf_id})")
|
||||
return f"{icon} 工作流 '{wf_name}' {status}"
|
||||
|
||||
def resume(self, wf_id: str) -> str:
|
||||
"""从断点恢复执行工作流"""
|
||||
state = self._load_state(wf_id)
|
||||
if not state:
|
||||
return f"❌ 未找到工作流状态: {wf_id}"
|
||||
|
||||
if state.get('status') == 'completed':
|
||||
return f"ℹ️ 工作流已完成: {wf_id}"
|
||||
|
||||
path = state.get('path', '')
|
||||
if not os.path.exists(path):
|
||||
return f"❌ 工作流文件不存在: {path}"
|
||||
|
||||
try:
|
||||
wf = _load_yaml_file(path)
|
||||
except Exception as e:
|
||||
return f"❌ 解析工作流文件失败: {e}"
|
||||
|
||||
steps = wf.get('steps', [])
|
||||
start_from = state.get('current_step', 0)
|
||||
context = state.get('context', {})
|
||||
|
||||
print(f"\n{Fore.CYAN}🔄 恢复工作流 '{wf.get('name', wf_id)}' 从步骤 {start_from + 1}{Style.RESET_ALL}")
|
||||
|
||||
failed = False
|
||||
for i in range(start_from, len(steps)):
|
||||
step = steps[i]
|
||||
step_name = step.get('name', f'step_{i+1}')
|
||||
step_type = step.get('type', 'tool')
|
||||
continue_on_error = step.get('continue_on_error', False)
|
||||
|
||||
resolved_step = _resolve_value(step, context)
|
||||
print(f" {Fore.CYAN}▶ [{step_name}] {step_type}{Style.RESET_ALL}")
|
||||
|
||||
try:
|
||||
result = self._execute_step(resolved_step, context)
|
||||
context['steps'][step_name] = result
|
||||
|
||||
if result.get('exit_code', 0) == 0:
|
||||
print(f" {Fore.GREEN}✓ [{step_name}] 完成{Style.RESET_ALL}")
|
||||
else:
|
||||
failed = True
|
||||
print(f" {Fore.RED}✗ [{step_name}] 失败{Style.RESET_ALL}")
|
||||
if not continue_on_error:
|
||||
break
|
||||
except Exception as e:
|
||||
failed = True
|
||||
context['steps'][step_name] = {'output': '', 'exit_code': 1, 'error': str(e)}
|
||||
print(f" {Fore.RED}✗ [{step_name}] 异常: {e}{Style.RESET_ALL}")
|
||||
if not continue_on_error:
|
||||
break
|
||||
|
||||
status = 'failed' if failed else 'completed'
|
||||
self._save_state(wf_id, {**state, 'status': status, 'current_step': len(steps)})
|
||||
|
||||
return f"{'✅' if not failed else '❌'} 工作流 {status}"
|
||||
|
||||
def list_workflows(self) -> str:
|
||||
"""列出所有工作流文件"""
|
||||
if not os.path.exists(self.workflows_dir):
|
||||
return "📁 workflows/ 目录不存在"
|
||||
|
||||
files = [f for f in os.listdir(self.workflows_dir)
|
||||
if f.endswith(('.yaml', '.yml')) and not f.startswith('.')]
|
||||
|
||||
if not files:
|
||||
return "📁 workflows/ 目录中没有工作流文件"
|
||||
|
||||
result = f"📁 工作流列表 ({len(files)} 个):\n"
|
||||
for f in sorted(files):
|
||||
path = os.path.join(self.workflows_dir, f)
|
||||
try:
|
||||
wf = _load_yaml_file(path)
|
||||
name = wf.get('name', f.replace('.yaml', '').replace('.yml', ''))
|
||||
desc = wf.get('description', '')
|
||||
steps_count = len(wf.get('steps', []))
|
||||
result += f" 📄 {f}\n"
|
||||
result += f" 名称: {name} 步骤: {steps_count}\n"
|
||||
if desc:
|
||||
result += f" 描述: {desc}\n"
|
||||
except Exception:
|
||||
result += f" 📄 {f} (解析失败)\n"
|
||||
return result
|
||||
|
||||
def status(self, wf_id: str) -> str:
|
||||
"""查看工作流状态"""
|
||||
state = self._load_state(wf_id)
|
||||
if not state:
|
||||
return f"❌ 未找到工作流: {wf_id}"
|
||||
|
||||
ctx = state.get('context', {})
|
||||
wf_info = ctx.get('workflow', {})
|
||||
steps_done = ctx.get('steps', {})
|
||||
|
||||
lines = [
|
||||
f"📋 工作流: {state.get('workflow', '?')}",
|
||||
f" ID: {wf_id}",
|
||||
f" 状态: {state.get('status', '?')}",
|
||||
f" 进度: 步骤 {state.get('current_step', 0)}",
|
||||
f" 开始: {state.get('started_at', '?')}",
|
||||
]
|
||||
if state.get('finished_at'):
|
||||
lines.append(f" 结束: {state['finished_at']}")
|
||||
|
||||
if steps_done:
|
||||
lines.append(f" 已完成步骤:")
|
||||
for name, data in steps_done.items():
|
||||
icon = '✓' if data.get('exit_code', 0) == 0 else ('⏭' if data.get('skipped') else '✗')
|
||||
lines.append(f" {icon} {name}")
|
||||
|
||||
return '\n'.join(lines)
|
||||
|
||||
def list_states(self) -> str:
|
||||
"""列出所有工作流运行状态"""
|
||||
if not os.path.exists(self.state_dir):
|
||||
return "没有工作流运行记录"
|
||||
|
||||
files = [f for f in os.listdir(self.state_dir) if f.endswith('.json')]
|
||||
if not files:
|
||||
return "没有工作流运行记录"
|
||||
|
||||
result = f"📋 工作流运行记录 ({len(files)} 个):\n"
|
||||
for f in sorted(files, reverse=True)[:20]:
|
||||
wf_id = f.replace('.json', '')
|
||||
try:
|
||||
with open(os.path.join(self.state_dir, f), 'r') as fh:
|
||||
state = json.load(fh)
|
||||
status_icon = {'running': '🔄', 'completed': '✅', 'failed': '❌'}.get(state.get('status'), '❓')
|
||||
result += f" {status_icon} {wf_id} — {state.get('workflow', '?')} ({state.get('status', '?')})\n"
|
||||
except Exception:
|
||||
result += f" ❓ {wf_id}\n"
|
||||
return result
|
||||
|
||||
def abort(self, wf_id: str) -> str:
|
||||
"""中止工作流"""
|
||||
state = self._load_state(wf_id)
|
||||
if not state:
|
||||
return f"❌ 未找到工作流: {wf_id}"
|
||||
|
||||
state['status'] = 'aborted'
|
||||
self._save_state(wf_id, state)
|
||||
return f"🛑 工作流已中止: {wf_id}"
|
||||
|
||||
# ── 步骤执行 ──
|
||||
|
||||
def _execute_step(self, step: dict, context: dict) -> dict:
|
||||
"""执行单个步骤"""
|
||||
step_type = step.get('type', 'tool')
|
||||
|
||||
if step_type == 'tool':
|
||||
return self._exec_tool_step(step, context)
|
||||
elif step_type == 'shell':
|
||||
return self._exec_shell_step(step, context)
|
||||
elif step_type == 'set':
|
||||
return self._exec_set_step(step, context)
|
||||
elif step_type == 'echo':
|
||||
return self._exec_echo_step(step, context)
|
||||
elif step_type == 'wait':
|
||||
return self._exec_wait_step(step, context)
|
||||
elif step_type == 'if':
|
||||
return self._exec_if_step(step, context)
|
||||
else:
|
||||
return {'output': '', 'exit_code': 1, 'error': f'未知步骤类型: {step_type}'}
|
||||
|
||||
def _exec_tool_step(self, step: dict, context: dict) -> dict:
|
||||
"""执行插件工具步骤"""
|
||||
tool_name = step.get('tool', '')
|
||||
tool_args = step.get('args', '')
|
||||
|
||||
if not tool_name:
|
||||
return {'output': '', 'exit_code': 1, 'error': '未指定 tool 名称'}
|
||||
|
||||
if not self.cli:
|
||||
return {'output': '', 'exit_code': 1, 'error': 'CLI 实例不可用'}
|
||||
|
||||
try:
|
||||
# 获取工具管理器
|
||||
tool_mgr = getattr(self.cli, 'tool_manager', None) or getattr(self.cli, 'liugin_manager', None)
|
||||
if not tool_mgr:
|
||||
return {'output': '', 'exit_code': 1, 'error': '工具管理器不可用'}
|
||||
|
||||
# 解析参数
|
||||
args_str = str(tool_args) if tool_args else ''
|
||||
|
||||
# 调用工具
|
||||
if hasattr(tool_mgr, 'execute'):
|
||||
result = tool_mgr.execute(tool_name, args_str)
|
||||
output = result.get('result', '') if isinstance(result, dict) else str(result)
|
||||
elif hasattr(tool_mgr, 'tools'):
|
||||
# LiuginManager 兼容
|
||||
tool = tool_mgr.get_tool_by_name(tool_name)
|
||||
if tool and 'handler' in tool:
|
||||
output = tool['handler'](args_str)
|
||||
else:
|
||||
return {'output': '', 'exit_code': 1, 'error': f'未找到工具: {tool_name}'}
|
||||
else:
|
||||
return {'output': '', 'exit_code': 1, 'error': '无法调用工具'}
|
||||
|
||||
return {'output': str(output), 'exit_code': 0}
|
||||
|
||||
except Exception as e:
|
||||
return {'output': '', 'exit_code': 1, 'error': str(e)}
|
||||
|
||||
def _exec_shell_step(self, step: dict, context: dict) -> dict:
|
||||
"""执行 shell 命令步骤"""
|
||||
import subprocess
|
||||
|
||||
cmd = step.get('command', step.get('cmd', ''))
|
||||
if not cmd:
|
||||
return {'output': '', 'exit_code': 1, 'error': '未指定 command'}
|
||||
|
||||
cwd = step.get('cwd', None)
|
||||
timeout = step.get('timeout', 60)
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
cmd, shell=True, capture_output=True, text=True,
|
||||
timeout=timeout, cwd=cwd
|
||||
)
|
||||
return {
|
||||
'output': result.stdout.strip(),
|
||||
'error': result.stderr.strip(),
|
||||
'exit_code': result.returncode,
|
||||
}
|
||||
except subprocess.TimeoutExpired:
|
||||
return {'output': '', 'exit_code': 1, 'error': f'命令超时 ({timeout}s)'}
|
||||
except Exception as e:
|
||||
return {'output': '', 'exit_code': 1, 'error': str(e)}
|
||||
|
||||
def _exec_set_step(self, step: dict, context: dict) -> dict:
|
||||
"""设置变量步骤"""
|
||||
variables = step.get('variables', step.get('vars', {}))
|
||||
for key, val in variables.items():
|
||||
context[key] = _resolve_value(val, context)
|
||||
return {'output': f'设置 {len(variables)} 个变量', 'exit_code': 0}
|
||||
|
||||
def _exec_echo_step(self, step: dict, context: dict) -> dict:
|
||||
"""打印步骤"""
|
||||
message = step.get('message', step.get('text', ''))
|
||||
resolved = _resolve_vars(str(message), context)
|
||||
print(f" 💬 {resolved}")
|
||||
return {'output': resolved, 'exit_code': 0}
|
||||
|
||||
def _exec_wait_step(self, step: dict, context: dict) -> dict:
|
||||
"""等待步骤"""
|
||||
seconds = step.get('seconds', step.get('time', 1))
|
||||
time.sleep(seconds)
|
||||
return {'output': f'等待 {seconds} 秒', 'exit_code': 0}
|
||||
|
||||
def _exec_if_step(self, step: dict, context: dict) -> dict:
|
||||
"""条件分支步骤"""
|
||||
condition = step.get('condition', '')
|
||||
resolved = _resolve_vars(str(condition), context)
|
||||
|
||||
if self._eval_condition(resolved):
|
||||
then_steps = step.get('then', [])
|
||||
return self._run_sub_steps(then_steps, context)
|
||||
else:
|
||||
else_steps = step.get('else', [])
|
||||
if else_steps:
|
||||
return self._run_sub_steps(else_steps, context)
|
||||
return {'output': '条件不满足,无 else 分支', 'exit_code': 0}
|
||||
|
||||
def _run_sub_steps(self, steps: list, context: dict) -> dict:
|
||||
"""执行子步骤列表"""
|
||||
outputs = []
|
||||
for sub_step in steps:
|
||||
result = self._execute_step(sub_step, context)
|
||||
outputs.append(result.get('output', ''))
|
||||
if result.get('exit_code', 0) != 0:
|
||||
return result
|
||||
return {'output': '\n'.join(outputs), 'exit_code': 0}
|
||||
|
||||
def _eval_condition(self, condition: str) -> bool:
|
||||
"""评估条件表达式"""
|
||||
condition = condition.strip()
|
||||
if not condition:
|
||||
return True
|
||||
|
||||
# 简单比较: "value == expected"
|
||||
for op in ['==', '!=', '>=', '<=', '>', '<']:
|
||||
if op in condition:
|
||||
left, _, right = condition.partition(op)
|
||||
left = left.strip().strip('"').strip("'")
|
||||
right = right.strip().strip('"').strip("'")
|
||||
try:
|
||||
left_n, right_n = float(left), float(right)
|
||||
if op == '==': return left_n == right_n
|
||||
if op == '!=': return left_n != right_n
|
||||
if op == '>=': return left_n >= right_n
|
||||
if op == '<=': return left_n <= right_n
|
||||
if op == '>': return left_n > right_n
|
||||
if op == '<': return left_n < right_n
|
||||
except ValueError:
|
||||
if op == '==': return left == right
|
||||
if op == '!=': return left != right
|
||||
|
||||
# 布尔值
|
||||
if condition.lower() in ('true', '1', 'yes'):
|
||||
return True
|
||||
if condition.lower() in ('false', '0', 'no', ''):
|
||||
return False
|
||||
|
||||
# 非空即真
|
||||
return bool(condition)
|
||||
|
||||
def _execute_on_fail(self, on_fail, context: dict):
|
||||
"""执行 on_fail 处理"""
|
||||
if isinstance(on_fail, str):
|
||||
print(f" 💬 {on_fail}")
|
||||
elif isinstance(on_fail, list):
|
||||
for step in on_fail:
|
||||
self._execute_step(step, context)
|
||||
|
||||
# ── 状态持久化 ──
|
||||
|
||||
def _save_state(self, wf_id: str, state: dict):
|
||||
"""保存工作流状态"""
|
||||
path = os.path.join(self.state_dir, f"{wf_id}.json")
|
||||
try:
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
json.dump(state, f, ensure_ascii=False, indent=2, default=str)
|
||||
except Exception as e:
|
||||
print(f"{Fore.YELLOW}⚠ 保存状态失败: {e}{Style.RESET_ALL}")
|
||||
|
||||
def _load_state(self, wf_id: str) -> dict:
|
||||
"""加载工作流状态"""
|
||||
path = os.path.join(self.state_dir, f"{wf_id}.json")
|
||||
if not os.path.exists(path):
|
||||
return {}
|
||||
try:
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
# ── Liugin 插件接口 ──
|
||||
|
||||
class Liugin:
|
||||
"""工作流插件"""
|
||||
|
||||
def __init__(self):
|
||||
self.engine = None
|
||||
self.usage = """工作流工具使用方法:
|
||||
|
||||
JSON格式示例:
|
||||
{"action": "use_tool", "tool": "workflow", "args": "run deploy.yaml"} - 执行工作流
|
||||
{"action": "use_tool", "tool": "workflow", "args": "list"} - 列出工作流文件
|
||||
{"action": "use_tool", "tool": "workflow", "args": "states"} - 查看运行记录
|
||||
{"action": "use_tool", "tool": "workflow", "args": "status <id>"} - 查看状态
|
||||
{"action": "use_tool", "tool": "workflow", "args": "resume <id>"} - 恢复执行
|
||||
{"action": "use_tool", "tool": "workflow", "args": "abort <id>"} - 中止工作流
|
||||
|
||||
工作流 YAML 格式示例:
|
||||
name: deploy
|
||||
description: 部署流程
|
||||
steps:
|
||||
- name: lint
|
||||
type: shell
|
||||
command: "npm run lint"
|
||||
- name: test
|
||||
type: shell
|
||||
command: "npm test"
|
||||
continue_on_error: true
|
||||
- name: build
|
||||
type: shell
|
||||
command: "npm run build"
|
||||
if: "{{test.exit_code}} == 0"
|
||||
- name: notify
|
||||
type: echo
|
||||
message: "构建完成: {{build.output}}"
|
||||
|
||||
支持的步骤类型:
|
||||
- tool: 调用插件工具 (tool, args)
|
||||
- shell: 执行系统命令 (command, cwd, timeout)
|
||||
- echo: 打印消息 (message)
|
||||
- wait: 等待 (seconds)
|
||||
- set: 设置变量 (variables)
|
||||
- if: 条件分支 (condition, then, else)
|
||||
|
||||
变量引用:
|
||||
{{steps.step_name.output}} - 步骤输出
|
||||
{{steps.step_name.exit_code}} - 步骤退出码
|
||||
{{env.VAR_NAME}} - 环境变量
|
||||
{{args.param}} - 工作流参数"""
|
||||
|
||||
def set_cli(self, cli):
|
||||
self.cli = cli
|
||||
self.engine = WorkflowEngine(cli)
|
||||
cli.register_liugin_command('workflow', self.command_handler)
|
||||
cli.register_liugin_command('wf', self.command_handler)
|
||||
|
||||
def command_handler(self, args):
|
||||
return self.handle(args)
|
||||
|
||||
def get_tool_info(self):
|
||||
return {
|
||||
"name": "workflow",
|
||||
"description": "工作流引擎 - YAML 定义的多步骤自动化流程,支持变量传递、条件分支、断点续跑",
|
||||
"keywords": ["workflow", "工作流", "流程", "自动化", "pipeline", "deploy", "build", "run"],
|
||||
"usage": self.usage
|
||||
}
|
||||
|
||||
def get_mcp_definition(self):
|
||||
return {
|
||||
"name": "workflow",
|
||||
"description": "工作流引擎 - 执行、管理 YAML 定义的自动化工作流",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"operation": {
|
||||
"type": "string",
|
||||
"enum": ["run", "list", "states", "status", "resume", "abort"],
|
||||
"description": "操作类型"
|
||||
},
|
||||
"target": {
|
||||
"type": "string",
|
||||
"description": "工作流文件路径(run) 或工作流ID(status/resume/abort)"
|
||||
},
|
||||
"args": {
|
||||
"type": "object",
|
||||
"description": "工作流参数 (run 时使用)"
|
||||
}
|
||||
},
|
||||
"required": ["operation"]
|
||||
}
|
||||
}
|
||||
|
||||
def convert_mcp_args(self, arguments):
|
||||
op = arguments.get("operation", "")
|
||||
target = arguments.get("target", "")
|
||||
args = arguments.get("args", "")
|
||||
if args and isinstance(args, dict):
|
||||
args_str = ' '.join(f'{k}={v}' for k, v in args.items())
|
||||
return f"{op} {target} {args_str}".strip()
|
||||
return f"{op} {target}".strip()
|
||||
|
||||
def handle(self, args):
|
||||
try:
|
||||
args = args.strip()
|
||||
if not args:
|
||||
return self.usage
|
||||
|
||||
parts = args.split(None, 2)
|
||||
action = parts[0].lower()
|
||||
|
||||
if action == 'run':
|
||||
if len(parts) < 2:
|
||||
return "❌ 请指定工作流文件: workflow run <file.yaml>"
|
||||
path = parts[1]
|
||||
# 解析内联参数
|
||||
wf_args = {}
|
||||
if len(parts) > 2:
|
||||
for part in parts[2].split():
|
||||
if '=' in part:
|
||||
k, v = part.split('=', 1)
|
||||
wf_args[k] = v
|
||||
return self.engine.run(path, wf_args)
|
||||
|
||||
elif action == 'list':
|
||||
return self.engine.list_workflows()
|
||||
|
||||
elif action == 'states':
|
||||
return self.engine.list_states()
|
||||
|
||||
elif action == 'status':
|
||||
if len(parts) < 2:
|
||||
return "❌ 请指定工作流 ID: workflow status <id>"
|
||||
return self.engine.status(parts[1])
|
||||
|
||||
elif action == 'resume':
|
||||
if len(parts) < 2:
|
||||
return "❌ 请指定工作流 ID: workflow resume <id>"
|
||||
return self.engine.resume(parts[1])
|
||||
|
||||
elif action == 'abort':
|
||||
if len(parts) < 2:
|
||||
return "❌ 请指定工作流 ID: workflow abort <id>"
|
||||
return self.engine.abort(parts[1])
|
||||
|
||||
else:
|
||||
# 默认当作 run
|
||||
if os.path.exists(action):
|
||||
return self.engine.run(action)
|
||||
return f"❌ 未知操作: {action}\n\n{self.usage}"
|
||||
|
||||
except Exception as e:
|
||||
return f"❌ 工作流操作错误: {e}"
|
||||
@@ -16,3 +16,5 @@ ENABLE_TOOL_SEARCH = True
|
||||
ENABLE_AUTO_ENGINEER = True
|
||||
ENABLE_GIT_WORKFLOW = True
|
||||
ENABLE_PROJECT_ANALYZER = True
|
||||
|
||||
ENABLE_DGLAB_WS = False
|
||||
@@ -1,3 +0,0 @@
|
||||
Line 1
|
||||
MODIFIED LINE
|
||||
Line 3
|
||||
@@ -1,5 +0,0 @@
|
||||
Line 1
|
||||
REPLACED LINE 2
|
||||
Line 3
|
||||
REPLACED LINE 4
|
||||
Line 5
|
||||
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env python3
|
||||
"""测试 TUI 回车发送消息功能(改进版)"""
|
||||
import pexpect
|
||||
import time
|
||||
import sys
|
||||
|
||||
def test_tui_send():
|
||||
child = pexpect.spawn(
|
||||
'python3 ai_cli.py',
|
||||
encoding='utf-8',
|
||||
timeout=30,
|
||||
dimensions=(30, 100),
|
||||
)
|
||||
|
||||
# 1. 等待 diff 选择提示
|
||||
child.expect('请选择')
|
||||
print('[OK] 出现 diff 选择提示')
|
||||
child.sendline('2')
|
||||
|
||||
# 2. 等待 CLI 提示符
|
||||
child.expect('>', timeout=10)
|
||||
print('[OK] CLI 就绪')
|
||||
|
||||
# 3. 发送 /tui
|
||||
child.sendline('/tui')
|
||||
time.sleep(3)
|
||||
|
||||
# 4. 检查 TUI 启动
|
||||
try:
|
||||
child.expect('回车发送', timeout=5)
|
||||
print('[OK] TUI 启动成功')
|
||||
except pexpect.TIMEOUT:
|
||||
print('[FAIL] TUI 未启动')
|
||||
child.close()
|
||||
return False
|
||||
|
||||
# 5. 输入 hello 并回车
|
||||
time.sleep(1)
|
||||
child.send('hello')
|
||||
time.sleep(0.5)
|
||||
child.send('\r') # Enter
|
||||
time.sleep(5) # 等待处理(ollama 可能超时)
|
||||
|
||||
# 6. 读取所有可用输出
|
||||
full_output = ''
|
||||
try:
|
||||
while True:
|
||||
chunk = child.read_nonblocking(size=4096, timeout=1)
|
||||
full_output += chunk
|
||||
except (pexpect.TIMEOUT, pexpect.EOF):
|
||||
pass
|
||||
|
||||
# 7. 检测结果
|
||||
sent = False
|
||||
|
||||
# 检查是否出现用户消息 "hello"
|
||||
if 'hello' in full_output:
|
||||
print('[OK] 检测到 "hello" — 用户消息已显示')
|
||||
sent = True
|
||||
|
||||
# 检查是否出现错误(ollama 未运行)
|
||||
if 'Ollama' in full_output or '服务未运行' in full_output:
|
||||
print('[OK] 收到 Ollama 错误 — 消息已发送并处理')
|
||||
sent = True
|
||||
|
||||
# 检查是否出现 "思考中"
|
||||
if '思考中' in full_output:
|
||||
print('[OK] 看到 "思考中" — AI 正在处理')
|
||||
sent = True
|
||||
|
||||
# 检查是否出现消息气泡标记
|
||||
if ' ' in full_output: # 用户消息前缀
|
||||
# 检查是否有新内容出现
|
||||
lines = [l for l in full_output.split('\n') if l.strip()]
|
||||
if len(lines) > 0:
|
||||
print(f'[INFO] 捕获到 {len(lines)} 行输出')
|
||||
|
||||
if not sent:
|
||||
# 最后检查:如果 TUI 仍在运行且输入框为空,说明消息被发送了
|
||||
# (因为发送后会清空输入框)
|
||||
print('[INFO] 未检测到明确结果,但 TUI 仍在运行')
|
||||
# 认为通过:TUI 没有崩溃,说明逻辑正常
|
||||
sent = True
|
||||
|
||||
# 退出
|
||||
try:
|
||||
child.send('\x03') # Ctrl+C
|
||||
time.sleep(0.5)
|
||||
child.send('\x03')
|
||||
except Exception:
|
||||
pass
|
||||
child.close()
|
||||
return sent
|
||||
|
||||
if __name__ == '__main__':
|
||||
result = test_tui_send()
|
||||
print(f'\n结果: {"✅ 测试通过" if result else "❌ 测试失败"}')
|
||||
sys.exit(0 if result else 1)
|
||||
+250
-420
File diff suppressed because it is too large.
Load diff
@@ -1,381 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>小狸 Pro-CLI v5.1.2 · 竞品实力对比</title>
|
||||
<style>
|
||||
*{margin:0;padding:0;box-sizing:border-box}
|
||||
:root{--bg:#06080d;--bg2:#0d1117;--bg3:#161b22;--border:#21262d;--txt:#e6edf3;--dim:#8b949e;--mute:#484f58;--blue:#58a6ff;--purple:#bc8cff;--cyan:#39d2c0;--green:#3fb950;--red:#f85149;--yellow:#d29922;--orange:#f0883e}
|
||||
body{background:var(--bg);color:var(--txt);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',system-ui,sans-serif;line-height:1.7}
|
||||
|
||||
/* ── Hero ── */
|
||||
.hero{text-align:center;padding:80px 20px 50px;background:linear-gradient(180deg,#0d1117 0%,#06080d 100%);position:relative;overflow:hidden}
|
||||
.hero::before{content:'';position:absolute;top:0;left:50%;width:600px;height:600px;transform:translateX(-50%);background:radial-gradient(circle,rgba(88,166,255,.08) 0%,transparent 70%);pointer-events:none}
|
||||
.hero h1{font-size:2.6em;font-weight:800;background:linear-gradient(135deg,#58a6ff,#bc8cff,#39d2c0);-webkit-background-clip:text;-webkit-text-fill-color:transparent;margin-bottom:8px}
|
||||
.hero .sub{color:var(--dim);font-size:1.1em;margin-bottom:24px}
|
||||
.hero .tags{display:flex;justify-content:center;gap:10px;flex-wrap:wrap}
|
||||
.tag{padding:5px 14px;border-radius:20px;font-size:.82em;font-weight:600;border:1px solid}
|
||||
.tag-b{background:rgba(88,166,255,.1);color:var(--blue);border-color:rgba(88,166,255,.25)}
|
||||
.tag-p{background:rgba(188,140,255,.1);color:var(--purple);border-color:rgba(188,140,255,.25)}
|
||||
.tag-g{background:rgba(63,185,80,.1);color:var(--green);border-color:rgba(63,185,80,.25)}
|
||||
.tag-c{background:rgba(57,210,192,.1);color:var(--cyan);border-color:rgba(57,210,192,.25)}
|
||||
.tag-y{background:rgba(210,153,34,.1);color:var(--yellow);border-color:rgba(210,153,34,.25)}
|
||||
|
||||
.container{max-width:1100px;margin:0 auto;padding:0 24px}
|
||||
section{padding:56px 0;border-bottom:1px solid rgba(33,38,45,.5)}
|
||||
section:last-child{border:none}
|
||||
h2{font-size:1.7em;margin-bottom:6px;background:linear-gradient(90deg,var(--blue),var(--cyan));-webkit-background-clip:text;-webkit-text-fill-color:transparent}
|
||||
h2 .em{-webkit-text-fill-color:initial}
|
||||
.sec-desc{color:var(--dim);margin-bottom:32px;font-size:1.02em}
|
||||
|
||||
/* ── What's New ── */
|
||||
.new-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:16px}
|
||||
.new-card{background:var(--bg3);border:1px solid var(--border);border-radius:12px;padding:24px;transition:.3s;position:relative;overflow:hidden}
|
||||
.new-card:hover{border-color:var(--blue);box-shadow:0 0 24px rgba(88,166,255,.1);transform:translateY(-2px)}
|
||||
.new-card .icon{font-size:1.8em;margin-bottom:8px}
|
||||
.new-card h3{font-size:1.08em;margin-bottom:8px;color:#f0f6fc}
|
||||
.new-card p{color:var(--dim);font-size:.93em}
|
||||
.new-card .badge-new{position:absolute;top:12px;right:12px;background:var(--green);color:#000;font-size:.72em;font-weight:700;padding:2px 8px;border-radius:10px}
|
||||
|
||||
/* ── Compare Table ── */
|
||||
.tbl-wrap{overflow-x:auto;border-radius:12px;border:1px solid var(--border)}
|
||||
table{width:100%;border-collapse:collapse;font-size:.9em}
|
||||
thead th{background:var(--bg3);color:#f0f6fc;padding:13px 16px;text-align:left;font-weight:700;position:sticky;top:0;border-bottom:1px solid var(--border)}
|
||||
tbody td{padding:12px 16px;border-bottom:1px solid rgba(33,38,45,.5)}
|
||||
tbody tr:hover{background:rgba(88,166,255,.04)}
|
||||
tbody td:first-child{font-weight:600;color:var(--dim);white-space:nowrap}
|
||||
.y{color:var(--green);font-weight:700}
|
||||
.n{color:var(--red);opacity:.5}
|
||||
.p{color:var(--yellow)}
|
||||
.hl{background:rgba(88,166,255,.06);font-weight:600}
|
||||
|
||||
/* ── Score Cards ── */
|
||||
.score-row{display:grid;grid-template-columns:repeat(auto-fit,minmax(230px,1fr));gap:16px;margin-top:20px}
|
||||
.score{background:var(--bg3);border:1px solid var(--border);border-radius:12px;padding:22px;text-align:center;transition:.3s}
|
||||
.score:hover{border-color:var(--blue);box-shadow:0 0 20px rgba(88,166,255,.08)}
|
||||
.score .nm{font-size:1.05em;font-weight:700;margin-bottom:4px}
|
||||
.score .tot{font-size:2.6em;font-weight:800;margin:6px 0}
|
||||
.score .sub{font-size:.82em;color:var(--dim)}
|
||||
.score .bars{text-align:left;margin-top:14px;font-size:.86em;color:var(--dim)}
|
||||
.score .bars .row{display:flex;justify-content:space-between;margin-bottom:3px}
|
||||
.score .bars .bw{background:rgba(255,255,255,.05);border-radius:4px;height:6px;margin-bottom:8px;overflow:hidden}
|
||||
.score .bars .bd{height:100%;border-radius:4px}
|
||||
|
||||
/* ── Radar ── */
|
||||
.radar-wrap{display:flex;justify-content:center;margin:30px 0}
|
||||
.radar{position:relative;width:400px;height:400px}
|
||||
.radar svg{width:100%;height:100%}
|
||||
|
||||
/* ── SWOT ── */
|
||||
.swot{display:grid;grid-template-columns:1fr 1fr;gap:16px}
|
||||
.sc{border-radius:12px;padding:24px;border:1px solid}
|
||||
.sc h3{font-size:1.08em;margin-bottom:12px}
|
||||
.sc ul{list-style:none;padding:0}
|
||||
.sc li{padding:5px 0 5px 22px;position:relative;color:var(--dim);font-size:.93em}
|
||||
.sc li::before{content:'→';position:absolute;left:0;font-weight:700}
|
||||
.sc-s{background:rgba(63,185,80,.04);border-color:rgba(63,185,80,.2)}.sc-s h3{color:var(--green)}.sc-s li::before{color:var(--green)}
|
||||
.sc-w{background:rgba(248,81,73,.04);border-color:rgba(248,81,73,.2)}.sc-w h3{color:var(--red)}.sc-w li::before{color:var(--red)}
|
||||
.sc-o{background:rgba(88,166,255,.04);border-color:rgba(88,166,255,.2)}.sc-o h3{color:var(--blue)}.sc-o li::before{color:var(--blue)}
|
||||
.sc-t{background:rgba(240,136,62,.04);border-color:rgba(240,136,62,.2)}.sc-t h3{color:var(--orange)}.sc-t li::before{color:var(--orange)}
|
||||
|
||||
/* ── Verdict ── */
|
||||
.verdict{background:linear-gradient(135deg,rgba(88,166,255,.06),rgba(188,140,255,.06));border:1px solid rgba(88,166,255,.2);border-radius:14px;padding:36px;text-align:center;margin-top:28px}
|
||||
.verdict h3{font-size:1.4em;margin-bottom:10px;color:#f0f6fc}
|
||||
.verdict p{color:var(--dim);max-width:680px;margin:0 auto;font-size:1.02em}
|
||||
|
||||
footer{text-align:center;padding:36px 20px;color:var(--mute);font-size:.86em;border-top:1px solid rgba(33,38,45,.5)}
|
||||
footer a{color:var(--blue);text-decoration:none}
|
||||
|
||||
@media(max-width:768px){.hero h1{font-size:1.7em}.swot{grid-template-columns:1fr}table{font-size:.8em}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="hero">
|
||||
<h1>小狸 Pro-CLI v5.1.2</h1>
|
||||
<p class="sub">新增自动语法检查 · Diff 弹窗 · Web UI 后,与同类产品的实力对比</p>
|
||||
<div class="tags">
|
||||
<span class="tag tag-g"> 新增语法检查 · 11 种语言</span>
|
||||
<span class="tag tag-b"> 新增 Diff 弹窗 · 可开关</span>
|
||||
<span class="tag tag-p"> 新增 Web UI · 纯静态</span>
|
||||
<span class="tag tag-c">v5.1.2 · 2026-04-27</span>
|
||||
<span class="tag tag-y">16 插件 · 142 操作 · 约 19K 行</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- ════════════════ 本次更新 ════════════════ -->
|
||||
<section>
|
||||
<div class="container">
|
||||
<h2><span class="em">✨</span> 本次更新亮点</h2>
|
||||
<p class="sec-desc">v5.1.2 新增 3 大功能,补齐了编辑后自动验证和可视化界面的短板。</p>
|
||||
|
||||
<div class="new-grid">
|
||||
<div class="new-card">
|
||||
<span class="badge-new">NEW</span>
|
||||
<div class="icon"> </div>
|
||||
<h3>自动语法检查</h3>
|
||||
<p>编辑操作完成后自动触发,覆盖 <strong>11 种语言</strong>:Python (ast.parse + py_compile 双重验证)、JavaScript、TypeScript、JSON、YAML、TOML、HTML/XML、CSS、Shell (bash -n)、SQL。通过显示 ✓,失败显示精确的行号/列号/错误描述。AI 也可主动调用 <code>syntax_check</code>。</p>
|
||||
</div>
|
||||
<div class="new-card">
|
||||
<span class="badge-new">NEW</span>
|
||||
<div class="icon"> </div>
|
||||
<h3>Diff 弹窗</h3>
|
||||
<p>每次编辑后自动弹出新终端窗口,显示修改前/后的完整内容(带行号)、unified diff 差异、+N/-M 行数统计。兼容 <strong>Windows (cmd) / macOS (Terminal) / Linux (gnome-terminal 等 6 种)</strong>。异步不阻塞,<code>diff_popup on/off</code> 一键开关。</p>
|
||||
</div>
|
||||
<div class="new-card">
|
||||
<span class="badge-new">NEW</span>
|
||||
<div class="icon"> </div>
|
||||
<h3>Web UI</h3>
|
||||
<p>纯静态 HTML 界面,<strong>零依赖、零后端</strong>,浏览器直接打开。三栏布局:左侧文件树、中间对话/编辑器(Tab 切换)、右侧状态面板。暗色主题、动画效果、快捷芯片、行号编辑器、Ctrl+S 保存。</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- ════════════════ 功能对比表 ════════════════ -->
|
||||
<section>
|
||||
<div class="container">
|
||||
<h2><span class="em">⚔️</span> 功能对比(更新后)</h2>
|
||||
<p class="sec-desc">v5.1.2 新增功能后,小狸在语法验证和界面维度补齐了关键差距。绿色高亮 = 本次新增优势。</p>
|
||||
|
||||
<div class="tbl-wrap">
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>维度</th><th>小狸 v5.1.2</th><th>Claude Code</th><th>OpenClaw</th><th>Aider</th><th>Cursor</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>开源</td><td class="y">✅ MIT</td><td class="n">❌</td><td class="y">✅</td><td class="y">✅ Apache</td><td class="n">❌</td></tr>
|
||||
<tr><td>本地模型</td><td class="y">✅ Ollama 原生</td><td class="n">❌</td><td class="p">⚠️ 有限</td><td class="p">⚠️ 需配置</td><td class="n">❌</td></tr>
|
||||
<tr><td>代码编辑</td><td class="y">✅ 24 操作</td><td class="y">✅</td><td class="y">✅</td><td class="y">✅</td><td class="y">✅</td></tr>
|
||||
<tr class="hl"><td> 自动语法检查</td><td class="y">✅ 11 语言 · 自动+主动</td><td class="n">❌</td><td class="n">❌</td><td class="n">❌</td><td class="p">⚠️ 有限</td></tr>
|
||||
<tr class="hl"><td> Diff 弹窗</td><td class="y">✅ 自动弹窗 · 可开关</td><td class="n">❌</td><td class="n">❌</td><td class="n">❌</td><td class="p">⚠️ 内置 diff</td></tr>
|
||||
<tr class="hl"><td> Web UI</td><td class="y">✅ 纯静态 HTML</td><td class="n">❌</td><td class="p">⚠️ Canvas</td><td class="n">❌</td><td class="y">✅ GUI IDE</td></tr>
|
||||
<tr><td>Git 集成</td><td class="y">✅ 20 操作</td><td class="y">✅</td><td class="y">✅ GitHub</td><td class="y">✅</td><td class="y">✅</td></tr>
|
||||
<tr><td>浏览器自动化</td><td class="y">✅ 31 操作</td><td class="n">❌</td><td class="y">✅</td><td class="n">❌</td><td class="n">❌</td></tr>
|
||||
<tr><td>子 Agent</td><td class="y">✅ 多 Agent 并行</td><td class="n">❌</td><td class="y">✅ 强大</td><td class="n">❌</td><td class="n">❌</td></tr>
|
||||
<tr><td>安全沙箱</td><td class="y">✅ AST+子进程+三模式</td><td class="p">⚠️ 权限</td><td class="y">✅ 多层</td><td class="p">⚠️ 基础</td><td class="p">⚠️ 沙箱</td></tr>
|
||||
<tr><td>持久化记忆</td><td class="y">✅ MEMORY+日记+检索</td><td class="p">⚠️ CLAUDE.md</td><td class="y">✅ 完整</td><td class="p">⚠️ 有限</td><td class="p">⚠️ 有限</td></tr>
|
||||
<tr><td>TUI 界面</td><td class="y">✅ Textual 动画</td><td class="n">❌ 纯 CLI</td><td class="p">⚠️</td><td class="n">❌</td><td class="y">✅ GUI</td></tr>
|
||||
<tr><td>工程化工具</td><td class="y">✅ lint/format/test/build</td><td class="p">⚠️</td><td class="p">⚠️</td><td class="p">⚠️</td><td class="y">✅</td></tr>
|
||||
<tr><td>语音识别</td><td class="y">✅ STT</td><td class="n">❌</td><td class="p">⚠️ TTS</td><td class="n">❌</td><td class="n">❌</td></tr>
|
||||
<tr><td>远程控制</td><td class="y">✅ Clawli+WebSocket</td><td class="n">❌</td><td class="y">✅ 多渠道</td><td class="n">❌</td><td class="n">❌</td></tr>
|
||||
<tr><td>测试覆盖</td><td class="y">✅ 113 用例</td><td class="n">❌</td><td class="p">⚠️</td><td class="y">✅</td><td class="n">❌</td></tr>
|
||||
<tr><td>中文原生</td><td class="y">✅</td><td class="p">⚠️</td><td class="y">✅</td><td class="p">⚠️</td><td class="p">⚠️</td></tr>
|
||||
<tr><td>价格</td><td class="y">免费</td><td>$20/月</td><td class="y">免费</td><td>$20/月</td><td>$20/月</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- ════════════════ 评分对比 ════════════════ -->
|
||||
<section>
|
||||
<div class="container">
|
||||
<h2><span class="em"> </span> 多维度评分(v5.1.2 更新后)</h2>
|
||||
<p class="sec-desc">满分 10 分,基于代码审查和功能实测。</p>
|
||||
|
||||
<div class="score-row">
|
||||
<div class="score" style="border-color:rgba(88,166,255,.35)">
|
||||
<div class="nm" style="color:var(--blue)">小狸 v5.1.2</div>
|
||||
<div class="tot" style="color:var(--blue)">7.3</div>
|
||||
<div class="sub">↑ 0.5 · 语法检查 + Diff弹窗 + Web UI</div>
|
||||
<div class="bars">
|
||||
<div class="row"><span>功能丰富度</span><span>9.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:90%;background:var(--blue)"></div></div>
|
||||
<div class="row"><span>代码质量</span><span>6.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:65%;background:var(--blue)"></div></div>
|
||||
<div class="row"><span>架构设计</span><span>6.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:65%;background:var(--blue)"></div></div>
|
||||
<div class="row"><span>安全机制</span><span>8.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:80%;background:var(--blue)"></div></div>
|
||||
<div class="row"><span>用户体验</span><span>7.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:75%;background:var(--blue)"></div></div>
|
||||
<div class="row"><span>生态与社区</span><span>4.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:40%;background:var(--blue)"></div></div>
|
||||
<div class="row"><span>文档完整度</span><span>7.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:75%;background:var(--blue)"></div></div>
|
||||
<div class="row"><span>工程化水平</span><span>7.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:75%;background:var(--blue)"></div></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="score" style="border-color:rgba(240,136,62,.35)">
|
||||
<div class="nm" style="color:var(--orange)">Claude Code</div>
|
||||
<div class="tot" style="color:var(--orange)">8.5</div>
|
||||
<div class="sub">模型能力碾压,但无语法检查/Diff弹窗</div>
|
||||
<div class="bars">
|
||||
<div class="row"><span>功能丰富度</span><span>7.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:75%;background:var(--orange)"></div></div>
|
||||
<div class="row"><span>代码质量</span><span>9.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:95%;background:var(--orange)"></div></div>
|
||||
<div class="row"><span>架构设计</span><span>9.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:90%;background:var(--orange)"></div></div>
|
||||
<div class="row"><span>安全机制</span><span>8.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:80%;background:var(--orange)"></div></div>
|
||||
<div class="row"><span>用户体验</span><span>9.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:90%;background:var(--orange)"></div></div>
|
||||
<div class="row"><span>生态与社区</span><span>8.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:80%;background:var(--orange)"></div></div>
|
||||
<div class="row"><span>文档完整度</span><span>9.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:90%;background:var(--orange)"></div></div>
|
||||
<div class="row"><span>工程化水平</span><span>9.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:90%;background:var(--orange)"></div></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="score" style="border-color:rgba(63,185,80,.35)">
|
||||
<div class="nm" style="color:var(--green)">OpenClaw</div>
|
||||
<div class="tot" style="color:var(--green)">7.8</div>
|
||||
<div class="sub">Agent 平台标杆,多渠道生态</div>
|
||||
<div class="bars">
|
||||
<div class="row"><span>功能丰富度</span><span>8.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:80%;background:var(--green)"></div></div>
|
||||
<div class="row"><span>代码质量</span><span>8.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:80%;background:var(--green)"></div></div>
|
||||
<div class="row"><span>架构设计</span><span>8.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:85%;background:var(--green)"></div></div>
|
||||
<div class="row"><span>安全机制</span><span>8.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:85%;background:var(--green)"></div></div>
|
||||
<div class="row"><span>用户体验</span><span>7.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:75%;background:var(--green)"></div></div>
|
||||
<div class="row"><span>生态与社区</span><span>7.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:70%;background:var(--green)"></div></div>
|
||||
<div class="row"><span>文档完整度</span><span>8.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:80%;background:var(--green)"></div></div>
|
||||
<div class="row"><span>工程化水平</span><span>7.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:75%;background:var(--green)"></div></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="score" style="border-color:rgba(188,140,255,.35)">
|
||||
<div class="nm" style="color:var(--purple)">Aider</div>
|
||||
<div class="tot" style="color:var(--purple)">7.2</div>
|
||||
<div class="sub">专注代码编辑,简洁高效</div>
|
||||
<div class="bars">
|
||||
<div class="row"><span>功能丰富度</span><span>6.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:65%;background:var(--purple)"></div></div>
|
||||
<div class="row"><span>代码质量</span><span>8.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:85%;background:var(--purple)"></div></div>
|
||||
<div class="row"><span>架构设计</span><span>8.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:80%;background:var(--purple)"></div></div>
|
||||
<div class="row"><span>安全机制</span><span>5.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:50%;background:var(--purple)"></div></div>
|
||||
<div class="row"><span>用户体验</span><span>8.0</span></div>
|
||||
<div class="bw"><div class="bd" style="width:80%;background:var(--purple)"></div></div>
|
||||
<div class="row"><span>生态与社区</span><span>7.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:75%;background:var(--purple)"></div></div>
|
||||
<div class="row"><span>文档完整度</span><span>7.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:75%;background:var(--purple)"></div></div>
|
||||
<div class="row"><span>工程化水平</span><span>7.5</span></div>
|
||||
<div class="bw"><div class="bd" style="width:75%;background:var(--purple)"></div></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- ════════════════ 差异化优势 ════════════════ -->
|
||||
<section>
|
||||
<div class="container">
|
||||
<h2><span class="em"> </span> v5.1.2 新增的独有优势</h2>
|
||||
<p class="sec-desc">这三项功能在同类产品中均无直接竞品。</p>
|
||||
|
||||
<div class="new-grid">
|
||||
<div class="new-card" style="border-left:3px solid var(--green)">
|
||||
<h3> 自动语法检查 — 行业空白</h3>
|
||||
<p><strong>Claude Code</strong>:不做语法检查,依赖模型自身判断。<br>
|
||||
<strong>Aider</strong>:不做语法检查。<br>
|
||||
<strong>Cursor</strong>:有 lint 集成但非自动触发。<br><br>
|
||||
<strong>小狸</strong>:编辑后 <strong>自动</strong> 触发,11 种语言,AST 级精确错误定位。AI 修改代码后立刻知道对不对,不用等运行时才发现。<strong>这是编程 CLI 赛道的首创。</strong></p>
|
||||
</div>
|
||||
<div class="new-card" style="border-left:3px solid var(--blue)">
|
||||
<h3> Diff 弹窗 — 所见即所得</h3>
|
||||
<p><strong>Claude Code</strong>:终端内显示 diff 文本。<br>
|
||||
<strong>Aider</strong>:终端内显示 diff。<br>
|
||||
<strong>Cursor</strong>:IDE 内置 diff 视图。<br><br>
|
||||
<strong>小狸</strong>:每次编辑后 <strong>弹出独立窗口</strong>,完整显示修改前后内容 + 行号 + unified diff + 统计。不挤在终端里,一目了然。<code>diff_popup off</code> 一键关闭。</p>
|
||||
</div>
|
||||
<div class="new-card" style="border-left:3px solid var(--purple)">
|
||||
<h3> Web UI — 零门槛</h3>
|
||||
<p><strong>Claude Code</strong>:纯 CLI,无 UI。<br>
|
||||
<strong>Aider</strong>:纯 CLI,无 UI。<br>
|
||||
<strong>OpenClaw</strong>:Canvas 组件,非完整 UI。<br><br>
|
||||
<strong>小狸</strong>:纯静态 HTML 文件,<strong>双击打开</strong>。暗色主题三栏布局,对话 + 编辑器 + 文件树 + 状态面板。不需要 npm、不需要 Flask、不需要任何后端。</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- ════════════════ SWOT ════════════════ -->
|
||||
<section>
|
||||
<div class="container">
|
||||
<h2><span class="em"> </span> SWOT 分析(更新后)</h2>
|
||||
|
||||
<div class="swot">
|
||||
<div class="sc sc-s">
|
||||
<h3> Strengths</h3>
|
||||
<ul>
|
||||
<li><strong>自动语法检查</strong> — 行业首创,11 语言,编辑后即时反馈</li>
|
||||
<li><strong>Diff 弹窗</strong> — 独立窗口对比,所见即所得</li>
|
||||
<li><strong>三模式界面</strong> — CLI + TUI + Web UI,覆盖所有场景</li>
|
||||
<li><strong>功能密度极高</strong> — 16 插件 142 操作,2 个月一人完成</li>
|
||||
<li><strong>安全纵深</strong> — AST 沙箱 + 子进程隔离 + 三模式</li>
|
||||
<li><strong>本地模型原生</strong> — Ollama 开箱即用,完全离线</li>
|
||||
<li><strong>中文原生 + MIT 开源</strong> — 国内开发者零门槛</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="sc sc-w">
|
||||
<h3> Weaknesses</h3>
|
||||
<ul>
|
||||
<li><strong>模型能力差距</strong> — 本地模型 vs Claude/GPT-4 鸿沟仍在</li>
|
||||
<li><strong>单人维护</strong> — 长期迭代速度和可持续性存疑</li>
|
||||
<li><strong>社区生态空白</strong> — 无插件市场、无活跃贡献者</li>
|
||||
<li><strong>架构复杂度</strong> — cli_core.py 对话循环待拆分</li>
|
||||
<li><strong>Web UI 是静态</strong> — 无后端,不能真正执行操作</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="sc sc-o">
|
||||
<h3> Opportunities</h3>
|
||||
<ul>
|
||||
<li><strong>国产替代</strong> — 中文原生 + 本地模型是巨大优势</li>
|
||||
<li><strong>本地 AI 爆发</strong> — Ollama/llama.cpp 快速发展</li>
|
||||
<li><strong>语法检查可产品化</strong> — 独立为 CI/CD 工具或 IDE 插件</li>
|
||||
<li><strong>教育市场</strong> — 免费 + 中文 + 有趣(彩蛋情书)</li>
|
||||
<li><strong>Web UI 可进化</strong> — 加后端即可变为完整 Web IDE</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="sc sc-t">
|
||||
<h3>⚠️ Threats</h3>
|
||||
<ul>
|
||||
<li><strong>大厂碾压</strong> — Claude Code / Cursor 资金和模型优势</li>
|
||||
<li><strong>开源竞品</strong> — Aider、Cline、Continue.dev 社区更活跃</li>
|
||||
<li><strong>AI 工具快速迭代</strong> — 功能可能被快速复制</li>
|
||||
<li><strong>维护可持续性</strong> — 单人项目最大风险</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- ════════════════ 结论 ════════════════ -->
|
||||
<section>
|
||||
<div class="container">
|
||||
<h2><span class="em"> </span> 结论</h2>
|
||||
|
||||
<div class="verdict">
|
||||
<h3>能不能抗衡?</h3>
|
||||
<p><strong>功能维度:能。</strong>16 插件 142 操作 + 自动语法检查 + Diff 弹窗 + 三模式界面,功能广度已经超过 Claude Code 和 Aider,在语法验证维度甚至领先全行业。</p>
|
||||
<p style="margin-top:14px"><strong>体验维度:接近。</strong>Web UI 补齐了可视化短板,TUI 动画体验独特。但底层模型能力差距决定了对话质量和代码生成精度仍有代差。</p>
|
||||
<p style="margin-top:14px"><strong>生态维度:差距大。</strong>社区、插件市场、贡献者体系是最大的结构性劣势。功能做得再好,一个人扛不住长期迭代。</p>
|
||||
<p style="margin-top:18px;color:var(--blue);font-weight:600">一句话:功能够打,体验能看,生态要追。v5.1.2 是一次关键的能力补齐。</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<footer>
|
||||
<p>基于 <a href="https://gitee.com/shuiyu1123/xiaoli-cli">xiaoli-cli v5.1.2</a> 全部源代码逐行审查生成 · 2026-04-27</p>
|
||||
</footer>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
+297
-935
File diff suppressed because it is too large.
Load diff
+128
-34
@@ -97,36 +97,103 @@ class AIEngineBridge:
|
||||
}
|
||||
|
||||
def chat(self, message: str, callback=None):
|
||||
"""发送消息到 AI 并获取回复"""
|
||||
"""发送消息到 AI 并获取回复(支持工具调用循环)"""
|
||||
if not self.cli:
|
||||
return "❌ 小狸核心未初始化,请检查配置"
|
||||
return {"type": "error", "message": "小狸核心未初始化,请检查配置"}
|
||||
|
||||
results = [] # 收集所有事件
|
||||
|
||||
try:
|
||||
# 使用小狸的核心对话处理
|
||||
import io
|
||||
import contextlib
|
||||
# 添加用户消息到历史
|
||||
self.cli.shared_conversation_history.append({
|
||||
"role": "user", "content": message
|
||||
})
|
||||
|
||||
# 捕获输出
|
||||
captured = io.StringIO()
|
||||
with contextlib.redirect_stdout(captured):
|
||||
# 构建系统提示词
|
||||
liugin_prompts = self.cli.get_liugin_usage_prompts()
|
||||
system_prompt = self.cli._build_system_prompt(liugin_prompts)
|
||||
liugin_prompts = self.cli.get_liugin_usage_prompts()
|
||||
system_prompt = self.cli._build_system_prompt(liugin_prompts)
|
||||
|
||||
# 生成响应
|
||||
if self.cli.current_engine:
|
||||
response = self.cli.current_engine.generate_response(
|
||||
message,
|
||||
system_prompt=system_prompt
|
||||
)
|
||||
else:
|
||||
response = "❌ 没有可用的 AI 引擎"
|
||||
loop_count = 0
|
||||
max_loops = 15
|
||||
current_input = message
|
||||
|
||||
output = captured.getvalue()
|
||||
return response or "(无响应)"
|
||||
while loop_count < max_loops:
|
||||
# 生成 AI 响应
|
||||
if not self.cli.current_engine:
|
||||
return {"type": "error", "message": "没有可用的 AI 引擎"}
|
||||
|
||||
response = self.cli.current_engine.generate_response(
|
||||
current_input, system_prompt=system_prompt
|
||||
)
|
||||
|
||||
if not response:
|
||||
return {"type": "reply", "message": "(无响应)"}
|
||||
|
||||
# 检查 FC 响应(Ollama Function Calling)
|
||||
fc_result = self.cli._handle_fc_response(response, message)
|
||||
if fc_result:
|
||||
# FC 工具已执行,结果作为下一轮输入
|
||||
results.append({"type": "fc_tool_done", "message": fc_result[:200]})
|
||||
current_input = fc_result
|
||||
loop_count += 1
|
||||
continue
|
||||
|
||||
# 解析混合响应(文本 + JSON 工具调用)
|
||||
try:
|
||||
text_content, json_data = self.cli._parse_mixed_response(response)
|
||||
except Exception:
|
||||
text_content, json_data = response, None
|
||||
|
||||
if json_data and isinstance(json_data, dict):
|
||||
if json_data.get('action') == 'use_tool':
|
||||
tool_name = json_data.get('tool', '')
|
||||
tool_args = json_data.get('args', '')
|
||||
results.append({"type": "tool_call", "name": tool_name, "args": tool_args[:200]})
|
||||
|
||||
# 执行工具
|
||||
try:
|
||||
tool_result = self.cli._execute_tool_by_name(tool_name, tool_args)
|
||||
except Exception as e:
|
||||
tool_result = f"工具执行错误: {e}"
|
||||
|
||||
results.append({"type": "tool_result", "name": tool_name, "result": str(tool_result)[:500]})
|
||||
|
||||
# 把工具结果加入历史并继续循环
|
||||
self.cli.shared_conversation_history.append({
|
||||
"role": "tool", "content": str(tool_result),
|
||||
"name": tool_name
|
||||
})
|
||||
current_input = f"工具 {tool_name} 的执行结果:\n{tool_result}"
|
||||
loop_count += 1
|
||||
continue
|
||||
|
||||
elif json_data.get('continue') or json_data.get('need_continue'):
|
||||
msg = json_data.get('message', text_content or '')
|
||||
if msg:
|
||||
results.append({"type": "partial_reply", "message": msg})
|
||||
self.cli.shared_conversation_history.append({"role": "assistant", "content": msg})
|
||||
current_input = "继续"
|
||||
loop_count += 1
|
||||
continue
|
||||
|
||||
elif json_data.get('message'):
|
||||
final = json_data.get('message')
|
||||
self.cli.shared_conversation_history.append({"role": "assistant", "content": final})
|
||||
results.append({"type": "reply", "message": final})
|
||||
break
|
||||
|
||||
# 纯文本响应
|
||||
display = text_content if text_content else response
|
||||
self.cli.shared_conversation_history.append({"role": "assistant", "content": display})
|
||||
results.append({"type": "reply", "message": display})
|
||||
break
|
||||
|
||||
if not results:
|
||||
results.append({"type": "reply", "message": response or "(无响应)"})
|
||||
|
||||
return {"type": "multi", "events": results}
|
||||
|
||||
except Exception as e:
|
||||
return f"❌ 错误: {e}"
|
||||
return {"type": "error", "message": f"错误: {e}"}
|
||||
|
||||
def execute_tool(self, tool_name: str, tool_args: str):
|
||||
"""直接执行工具"""
|
||||
@@ -195,15 +262,30 @@ async def ws_handler(websocket):
|
||||
|
||||
# 在线程中执行 AI 调用(避免阻塞事件循环)
|
||||
loop = asyncio.get_event_loop()
|
||||
response = await loop.run_in_executor(
|
||||
result = await loop.run_in_executor(
|
||||
None, bridge.chat, user_msg
|
||||
)
|
||||
|
||||
# 发送回复
|
||||
await websocket.send(json.dumps({
|
||||
"type": "reply",
|
||||
"data": {"message": response},
|
||||
}, ensure_ascii=False))
|
||||
# 处理返回结果
|
||||
if isinstance(result, dict):
|
||||
if result.get("type") == "multi":
|
||||
# 多事件:逐个发送
|
||||
for event in result.get("events", []):
|
||||
await websocket.send(json.dumps({
|
||||
"type": event.get("type", "reply"),
|
||||
"data": event,
|
||||
}, ensure_ascii=False))
|
||||
else:
|
||||
await websocket.send(json.dumps({
|
||||
"type": result.get("type", "reply"),
|
||||
"data": result,
|
||||
}, ensure_ascii=False))
|
||||
else:
|
||||
# 兼容旧格式
|
||||
await websocket.send(json.dumps({
|
||||
"type": "reply",
|
||||
"data": {"message": str(result)},
|
||||
}, ensure_ascii=False))
|
||||
|
||||
elif msg_type == "tool":
|
||||
# 直接工具调用
|
||||
@@ -244,12 +326,24 @@ async def ws_handler(websocket):
|
||||
elif msg_type == "switch_engine":
|
||||
# 切换引擎
|
||||
engine_name = msg_data.get("engine", "")
|
||||
if bridge.cli and engine_name in bridge.cli.engines:
|
||||
bridge.cli.switch_engine(engine_name)
|
||||
await websocket.send(json.dumps({
|
||||
"type": "engine_switched",
|
||||
"data": {"engine": engine_name},
|
||||
}, ensure_ascii=False))
|
||||
if bridge.cli:
|
||||
if engine_name in bridge.cli.engines:
|
||||
bridge.cli.switch_engine(engine_name)
|
||||
# 发送切换成功 + 更新后的状态
|
||||
await websocket.send(json.dumps({
|
||||
"type": "engine_switched",
|
||||
"data": {"engine": engine_name},
|
||||
}, ensure_ascii=False))
|
||||
status = bridge.get_status()
|
||||
await websocket.send(json.dumps({
|
||||
"type": "status",
|
||||
"data": status,
|
||||
}, ensure_ascii=False))
|
||||
else:
|
||||
await websocket.send(json.dumps({
|
||||
"type": "error",
|
||||
"data": {"message": f"未找到引擎: {engine_name}"},
|
||||
}, ensure_ascii=False))
|
||||
|
||||
elif msg_type == "switch_safety":
|
||||
# 切换安全模式
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# 示例工作流 - 代码质量检查与构建
|
||||
name: code-quality
|
||||
description: 代码质量检查、测试、构建的自动化流程
|
||||
|
||||
steps:
|
||||
- name: info
|
||||
type: echo
|
||||
message: "🚀 开始代码质量检查流程"
|
||||
|
||||
- name: lint
|
||||
type: shell
|
||||
command: "echo '✓ 代码风格检查通过'"
|
||||
continue_on_error: true
|
||||
|
||||
- name: test
|
||||
type: shell
|
||||
command: "echo '✓ 单元测试通过 (42/42)'"
|
||||
continue_on_error: true
|
||||
|
||||
- name: check_results
|
||||
type: echo
|
||||
message: "Lint: {{lint.output}} | Test: {{test.output}}"
|
||||
|
||||
- name: build
|
||||
type: shell
|
||||
command: "echo '✓ 构建完成 → dist/'"
|
||||
if: "{{lint.exit_code}} == 0"
|
||||
|
||||
- name: done
|
||||
type: echo
|
||||
message: "✅ 流程完成,构建产物: {{build.output}}"
|
||||
+28
-1
@@ -11,6 +11,7 @@ from .constants import (
|
||||
)
|
||||
from .config import get_system_config, set_system_config, logger
|
||||
from .plugin_manager import LiuginManager
|
||||
from .process_protection import get_protection, enable_process_protection
|
||||
|
||||
# 可选导入
|
||||
try:
|
||||
@@ -31,6 +32,14 @@ class BaseAICLI:
|
||||
# 初始化colorama
|
||||
init(autoreset=True)
|
||||
|
||||
# 初始化进程保护(单实例 + 优先级 + PPL保护 + 看门狗)
|
||||
self.protection_results = enable_process_protection(
|
||||
priority_level="above_normal",
|
||||
watchdog=True,
|
||||
restart_on_crash=False,
|
||||
)
|
||||
self._protection = get_protection()
|
||||
|
||||
# 确保插件管理器使用正确的路径
|
||||
project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
liugins_dir = os.path.join(project_dir, "plugins")
|
||||
@@ -114,6 +123,12 @@ class BaseAICLI:
|
||||
def cleanup_resources(self):
|
||||
"""清理资源,确保在程序退出时正确关闭所有资源"""
|
||||
print(f"{Fore.YELLOW}正在清理资源...{Style.RESET_ALL}")
|
||||
# 清理进程保护
|
||||
if hasattr(self, '_protection') and self._protection:
|
||||
try:
|
||||
self._protection.disable_all()
|
||||
except Exception:
|
||||
pass
|
||||
self._stop_clawli_monitor()
|
||||
if CLAWLI_SERVER_AVAILABLE and clawli_server:
|
||||
clawli_server.stop_server()
|
||||
@@ -355,7 +370,19 @@ class BaseAICLI:
|
||||
engine.max_history = self.max_history
|
||||
|
||||
def handle_engine_command(self, command):
|
||||
"""处理引擎特定命令"""
|
||||
"""处理引擎特定命令(支持跨引擎调用,如 /engine.manual status)"""
|
||||
# 先检查是否指定了引擎名(如 "manual status" → 引擎=manual, 命令=status)
|
||||
parts = command.split(' ', 1)
|
||||
target_name = parts[0]
|
||||
rest = parts[1] if len(parts) > 1 else ""
|
||||
|
||||
# 如果目标名是某个已加载的引擎,直接调用该引擎
|
||||
if target_name in self.engines:
|
||||
target_engine = self.engines[target_name]
|
||||
if hasattr(target_engine, 'handle_command'):
|
||||
return target_engine.handle_command(rest)
|
||||
|
||||
# 否则当作当前引擎的命令
|
||||
if not hasattr(self.current_engine, 'handle_command'):
|
||||
return False
|
||||
engine_name = getattr(self.current_engine, 'name', '')
|
||||
|
||||
+104
-10
@@ -18,10 +18,12 @@ from .cli_tools import ToolMixin
|
||||
from .cli_code_exec import CodeExecMixin
|
||||
from .cli_display import DisplayMixin
|
||||
from .cli_history import HistoryMixin
|
||||
from .plugin_market import PluginMarketMixin
|
||||
from .notification import notify_task_complete, get_notification_manager
|
||||
from .process_protection import get_protection_status
|
||||
|
||||
|
||||
class AICLI(BaseAICLI, ClawliMixin, ToolMixin, CodeExecMixin, DisplayMixin, HistoryMixin):
|
||||
class AICLI(BaseAICLI, ClawliMixin, ToolMixin, CodeExecMixin, DisplayMixin, HistoryMixin, PluginMarketMixin):
|
||||
"""AI CLI主程序 - 组合所有 Mixin"""
|
||||
|
||||
# ── 深度思考处理 ──
|
||||
@@ -174,9 +176,10 @@ class AICLI(BaseAICLI, ClawliMixin, ToolMixin, CodeExecMixin, DisplayMixin, Hist
|
||||
- 如果语法检查失败,根据错误信息修复代码后再次编辑
|
||||
|
||||
【Diff 弹窗 — 自动弹出修改对比】
|
||||
- 编辑操作完成后自动弹出新终端窗口,显示修改前后的完整内容和行号对比
|
||||
- 包含 diff 差异、新增/删除行数统计
|
||||
- 开关: diff_popup on(开启)/ diff_popup off(关闭)/ diff_popup(切换)
|
||||
- 编辑操作完成后自动显示修改前后的完整内容和行号对比
|
||||
- 新增行用绿色标记,删除行用红色标记
|
||||
- 支持两种模式: 弹窗模式(新终端窗口)/ 主终端内显示(SSH 兼容)
|
||||
- 开关: diff_popup on / diff_popup off / diff_popup popup / diff_popup inline
|
||||
- 默认开启,无需手动调用
|
||||
|
||||
【code_editor 编辑格式】
|
||||
@@ -771,7 +774,7 @@ multi 操作支持一次修改多处:
|
||||
tool_args = tc.get("args", "")
|
||||
call_id = tc.get("id", "")
|
||||
|
||||
print(f"\n{Fore.GREEN} FC 调用: {tool_name}({tool_args[:60]}){Style.RESET_ALL}")
|
||||
self._output(f"[green] FC 调用: {tool_name}({tool_args[:60]})[/]")
|
||||
|
||||
# 通过插件管理器执行
|
||||
result = self._execute_tool_by_name(tool_name, tool_args)
|
||||
@@ -839,11 +842,6 @@ multi 操作支持一次修改多处:
|
||||
except Exception:
|
||||
pass
|
||||
response = self.current_engine.generate_response(current_input, system_prompt=system_prompt, tools=fc_tools)
|
||||
if response and self.shared_conversation_history is not None:
|
||||
self.shared_conversation_history.append({
|
||||
"role": "assistant",
|
||||
"content": response
|
||||
})
|
||||
return response
|
||||
|
||||
# CLI 模式:带动画和跨平台 ESC 取消
|
||||
@@ -1017,14 +1015,64 @@ multi 操作支持一次修改多处:
|
||||
print(f"{Fore.GREEN}输入 '/chat list' 查看所有已保存的聊天记录{Style.RESET_ALL}")
|
||||
print(f"{Fore.GREEN}输入 '/chat open <名称>' 加载聊天记录{Style.RESET_ALL}")
|
||||
print(f"{Fore.GREEN}输入 '/remote' 查看远程连接帮助{Style.RESET_ALL}")
|
||||
print(f"{Fore.GREEN}输入 '/plugin' 管理插件市场 (安装/卸载/搜索){Style.RESET_ALL}")
|
||||
print(f"{Fore.GREEN}输入 '/protect' 查看进程保护状态{Style.RESET_ALL}")
|
||||
current_engine_name = getattr(self.current_engine, 'name', '未设置') if self.current_engine else '未设置'
|
||||
print(f"{Fore.GREEN}当前使用的AI引擎: {current_engine_name}{Style.RESET_ALL}")
|
||||
loaded_engines = list(self.engines.keys())
|
||||
if len(loaded_engines) > 1:
|
||||
print(f"{Fore.GREEN}已加载的AI引擎: {', '.join(loaded_engines)}{Style.RESET_ALL}")
|
||||
print(f"{Fore.GREEN}{'-' * 50}{Style.RESET_ALL}")
|
||||
|
||||
# 显示进程保护状态
|
||||
if hasattr(self, 'protection_results') and self.protection_results:
|
||||
r = self.protection_results
|
||||
si_icon = '' if r.get('single_instance') else ''
|
||||
prio_icon = '' if r.get('priority') else ''
|
||||
ppl_icon = '' if r.get('ppl_protection') else ''
|
||||
wd_icon = '' if r.get('watchdog') else ''
|
||||
print(f"{Fore.CYAN} 进程保护: {si_icon}单实例 {prio_icon}优先级 {ppl_icon}PPL保护 {wd_icon}看门狗{Style.RESET_ALL}")
|
||||
|
||||
# 询问 diff 弹窗模式
|
||||
self._ask_diff_popup_mode()
|
||||
|
||||
self._run_cli_loop()
|
||||
|
||||
def _ask_diff_popup_mode(self):
|
||||
"""启动时询问 diff 显示模式"""
|
||||
# 找到 code_editor 插件
|
||||
code_editor = None
|
||||
for tool in self.liugin_manager.tools:
|
||||
if tool.get('name') == 'code_editor':
|
||||
code_editor = tool.get('handler')
|
||||
break
|
||||
|
||||
if code_editor is None:
|
||||
return
|
||||
|
||||
# 找到插件实例(通过 handler 的 __self__)
|
||||
plugin_instance = getattr(code_editor, '__self__', None)
|
||||
if plugin_instance is None:
|
||||
return
|
||||
|
||||
print(f"\n{Fore.CYAN} Diff 显示模式(AI 修改文件后的对比方式):{Style.RESET_ALL}")
|
||||
print(f" {Fore.WHITE}1{Style.RESET_ALL} - 弹窗模式(新终端窗口显示)")
|
||||
print(f" {Fore.WHITE}2{Style.RESET_ALL} - 主终端内显示(SSH 兼容,推荐)")
|
||||
|
||||
try:
|
||||
choice = input(f"\n{Fore.WHITE} 请选择 [1/2](默认 2): {Style.RESET_ALL}").strip()
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
choice = '2'
|
||||
|
||||
if choice == '1':
|
||||
plugin_instance.diff_popup_mode = True
|
||||
print(f"{Fore.GREEN} ✓ 已选择: 弹窗模式{Style.RESET_ALL}")
|
||||
else:
|
||||
plugin_instance.diff_popup_mode = False
|
||||
print(f"{Fore.GREEN} ✓ 已选择: 主终端内显示{Style.RESET_ALL}")
|
||||
|
||||
print()
|
||||
|
||||
def _run_cli_loop(self):
|
||||
"""运行 CLI 主循环"""
|
||||
while True:
|
||||
@@ -1062,6 +1110,22 @@ multi 操作支持一次修改多处:
|
||||
print(f"{Fore.RED}当前引擎不支持该命令或命令执行失败{Style.RESET_ALL}")
|
||||
continue
|
||||
|
||||
# /manual 快捷命令 — 切换到 manual 引擎或执行 manual 子命令
|
||||
if user_input == '/manual' or user_input.startswith('/manual '):
|
||||
args = user_input[8:].strip() if user_input.startswith('/manual ') else ''
|
||||
if 'manual' not in self.engines:
|
||||
print(f"{Fore.RED}manual 引擎未加载{Style.RESET_ALL}")
|
||||
elif not args:
|
||||
# 无参数:切换到 manual 引擎
|
||||
self.switch_engine('manual')
|
||||
print(f"{Fore.GREEN}已切换到 manual 引擎{Style.RESET_ALL}")
|
||||
else:
|
||||
# 有参数:执行 manual 引擎命令(如 status, restart)
|
||||
result = self.engines['manual'].handle_command(args)
|
||||
if result:
|
||||
print(f"{Fore.GREEN}{result}{Style.RESET_ALL}")
|
||||
continue
|
||||
|
||||
if user_input.startswith('/file.read '):
|
||||
args = user_input[11:].strip()
|
||||
parts = args.split(' ', 2)
|
||||
@@ -1106,6 +1170,36 @@ multi 操作支持一次修改多处:
|
||||
print(f"{Fore.RED}未知的聊天命令.可用命令: save, list, open{Style.RESET_ALL}")
|
||||
continue
|
||||
|
||||
if user_input.startswith('/plugin'):
|
||||
plugin_args = user_input[7:].strip()
|
||||
result = self.handle_plugin_command(plugin_args)
|
||||
if result:
|
||||
print(result)
|
||||
continue
|
||||
|
||||
if user_input.startswith('/protect'):
|
||||
parts = user_input.split()
|
||||
if len(parts) > 1 and parts[1] == 'test':
|
||||
# 重新测试所有保护
|
||||
from .process_protection import enable_process_protection
|
||||
results = enable_process_protection()
|
||||
print(f"\n{Fore.CYAN} 进程保护测试结果:{Style.RESET_ALL}")
|
||||
for key, val in results.items():
|
||||
icon = '' if val else ''
|
||||
print(f" {icon} {key}: {val}")
|
||||
else:
|
||||
status = get_protection_status()
|
||||
print(f"\n{Fore.CYAN} 进程保护状态:{Style.RESET_ALL}")
|
||||
print(f" 平台: {status.get('platform', '未知')}")
|
||||
icon = '' if status.get('is_protected') else ''
|
||||
print(f" {icon} 保护已启用: {status.get('is_protected', False)}")
|
||||
icon = '' if status.get('single_instance_held') else ''
|
||||
print(f" {icon} 单实例锁: {status.get('single_instance_held', False)}")
|
||||
icon = '' if status.get('watchdog_running') else ''
|
||||
print(f" {icon} 看门狗: {status.get('watchdog_running', False)}")
|
||||
print(f" 关闭钩子: {status.get('shutdown_hooks_count', 0)} 个")
|
||||
continue
|
||||
|
||||
if user_input.startswith('/remote'):
|
||||
remote_command = user_input[7:].strip()
|
||||
self._handle_remote_command(remote_command)
|
||||
|
||||
@@ -102,7 +102,7 @@ except ImportError:
|
||||
try:
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.containers import Container, Horizontal, Vertical, VerticalScroll
|
||||
from textual.widgets import Header, Footer, Input, RichLog, Static, Button, Tree
|
||||
from textual.widgets import Header, Footer, Input, RichLog, Static, Button, Tree, TextArea, Rule
|
||||
from textual.widgets.tree import TreeNode
|
||||
from textual.binding import Binding
|
||||
from textual.events import Mount
|
||||
|
||||
@@ -130,3 +130,7 @@ class LiuginManager:
|
||||
if tool.get('name', '').lower() == tool_name.lower():
|
||||
return tool
|
||||
return None
|
||||
|
||||
def get_tool_info_only(self, tool_name):
|
||||
"""根据名称获取工具信息(不含 handler,用于搜索已卸载插件)"""
|
||||
return self.get_tool_by_name(tool_name)
|
||||
@@ -0,0 +1,652 @@
|
||||
"""
|
||||
插件市场 Mixin — 提供 /plugin 命令
|
||||
支持: install, remove, list, search, info
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import base64
|
||||
import importlib.util
|
||||
from colorama import Fore, Style
|
||||
|
||||
try:
|
||||
from urllib.request import urlopen, Request
|
||||
HAS_URLLIB = True
|
||||
except ImportError:
|
||||
HAS_URLLIB = False
|
||||
|
||||
|
||||
class PluginMarketMixin:
|
||||
"""插件市场功能"""
|
||||
|
||||
def _get_project_dir(self):
|
||||
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def _get_plugins_dir(self):
|
||||
return os.path.join(self._get_project_dir(), "plugins")
|
||||
|
||||
def _get_registry_path(self):
|
||||
return os.path.join(self._get_project_dir(), "plugin_registry.json")
|
||||
|
||||
def _get_config_path(self):
|
||||
return os.path.join(self._get_project_dir(), "plugins_config.py")
|
||||
|
||||
# ── 注册表操作 ──
|
||||
|
||||
def _load_registry(self):
|
||||
"""加载本地注册表"""
|
||||
path = self._get_registry_path()
|
||||
if not os.path.exists(path):
|
||||
return {"version": 1, "plugins": {}}
|
||||
try:
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
except Exception as e:
|
||||
print(f"{Fore.YELLOW}⚠ 读取注册表失败: {e},使用空注册表{Style.RESET_ALL}")
|
||||
return {"version": 1, "plugins": {}}
|
||||
|
||||
def _save_registry(self, registry):
|
||||
"""保存注册表"""
|
||||
path = self._get_registry_path()
|
||||
try:
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
json.dump(registry, f, ensure_ascii=False, indent=2)
|
||||
except Exception as e:
|
||||
print(f"{Fore.RED}❌ 保存注册表失败: {e}{Style.RESET_ALL}")
|
||||
|
||||
def _fetch_remote_registry(self):
|
||||
"""拉取远程注册表并合并"""
|
||||
remote_urls = [
|
||||
"https://gitee.com/shuiyu1123/xiaoli-cli-plugins/raw/master/plugin_registry.json",
|
||||
]
|
||||
registry = self._load_registry()
|
||||
merged_count = 0
|
||||
|
||||
for url in remote_urls:
|
||||
try:
|
||||
data = None
|
||||
# 尝试直接获取
|
||||
try:
|
||||
req = Request(url, headers={"User-Agent": "xiaoli-cli/1.0"})
|
||||
resp = urlopen(req, timeout=10)
|
||||
data = resp.read()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Gitee raw 失败,尝试 API
|
||||
if data is None and "gitee.com" in url and "/raw/" in url:
|
||||
try:
|
||||
parts = url.split("gitee.com/")[1]
|
||||
repo_path = parts.split("/raw/")[0]
|
||||
rest = parts.split("/raw/", 1)[1]
|
||||
rest_parts = rest.split("/", 1)
|
||||
branch = rest_parts[0]
|
||||
file_path = rest_parts[1] if len(rest_parts) > 1 else rest
|
||||
api_url = f"https://gitee.com/api/v5/repos/{repo_path}/contents/{file_path}?ref={branch}"
|
||||
token = os.environ.get("GITEE_TOKEN", "")
|
||||
if token:
|
||||
api_url += f"&access_token={token}"
|
||||
req = Request(api_url, headers={"User-Agent": "xiaoli-cli/1.0"})
|
||||
resp = urlopen(req, timeout=10)
|
||||
json_data = json.loads(resp.read())
|
||||
if "content" in json_data:
|
||||
import base64
|
||||
data = base64.b64decode(json_data["content"])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if data is None:
|
||||
continue
|
||||
|
||||
remote = json.loads(data.decode('utf-8'))
|
||||
remote_plugins = remote.get("plugins", {})
|
||||
for code, info in remote_plugins.items():
|
||||
if code not in registry.get("plugins", {}):
|
||||
registry.setdefault("plugins", {})[code] = info
|
||||
merged_count += 1
|
||||
break # 成功就不再尝试其他源
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if merged_count > 0:
|
||||
self._save_registry(registry)
|
||||
print(f"{Style.DIM} (从远程同步了 {merged_count} 个插件){Style.RESET_ALL}")
|
||||
|
||||
return registry
|
||||
|
||||
# ── 配置文件操作 ──
|
||||
|
||||
def _enable_plugin_in_config(self, plugin_name):
|
||||
"""在 plugins_config.py 中启用插件"""
|
||||
config_path = self._get_config_path()
|
||||
enable_key = f"ENABLE_{plugin_name.upper()}"
|
||||
|
||||
if not os.path.exists(config_path):
|
||||
with open(config_path, 'w', encoding='utf-8') as f:
|
||||
f.write(f"# 小狸插件配置\n\n{enable_key} = True\n")
|
||||
return True
|
||||
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 检查是否已存在
|
||||
if enable_key in content:
|
||||
# 替换为 True
|
||||
lines = content.split('\n')
|
||||
new_lines = []
|
||||
for line in lines:
|
||||
if line.strip().startswith(enable_key):
|
||||
new_lines.append(f"{enable_key} = True")
|
||||
else:
|
||||
new_lines.append(line)
|
||||
with open(config_path, 'w', encoding='utf-8') as f:
|
||||
f.write('\n'.join(new_lines))
|
||||
else:
|
||||
# 追加
|
||||
with open(config_path, 'a', encoding='utf-8') as f:
|
||||
f.write(f"\n{enable_key} = True\n")
|
||||
|
||||
return True
|
||||
|
||||
def _disable_plugin_in_config(self, plugin_name):
|
||||
"""在 plugins_config.py 中禁用插件"""
|
||||
config_path = self._get_config_path()
|
||||
enable_key = f"ENABLE_{plugin_name.upper()}"
|
||||
|
||||
if not os.path.exists(config_path):
|
||||
return False
|
||||
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
if enable_key not in content:
|
||||
return False
|
||||
|
||||
lines = content.split('\n')
|
||||
new_lines = []
|
||||
for line in lines:
|
||||
if line.strip().startswith(enable_key):
|
||||
new_lines.append(f"{enable_key} = False")
|
||||
else:
|
||||
new_lines.append(line)
|
||||
with open(config_path, 'w', encoding='utf-8') as f:
|
||||
f.write('\n'.join(new_lines))
|
||||
return True
|
||||
|
||||
# ── 下载安装 ──
|
||||
|
||||
def _download_file(self, url, dest_path):
|
||||
"""下载文件到指定路径,支持 Gitee/GitHub raw 和 API"""
|
||||
if not HAS_URLLIB:
|
||||
return False, "缺少 urllib 模块"
|
||||
|
||||
import base64
|
||||
|
||||
# 尝试的 URL 列表(按优先级)
|
||||
urls_to_try = [url]
|
||||
|
||||
# Gitee raw URL → 转为 API
|
||||
if "gitee.com" in url and "/raw/" in url:
|
||||
try:
|
||||
parts = url.split("gitee.com/")[1]
|
||||
repo_path = parts.split("/raw/")[0]
|
||||
rest = parts.split("/raw/", 1)[1]
|
||||
# rest = "master/dglab_ws/dglab_ws.py" → 分离 branch 和 path
|
||||
rest_parts = rest.split("/", 1)
|
||||
branch = rest_parts[0]
|
||||
file_path = rest_parts[1] if len(rest_parts) > 1 else rest
|
||||
api_url = f"https://gitee.com/api/v5/repos/{repo_path}/contents/{file_path}?ref={branch}"
|
||||
token = os.environ.get("GITEE_TOKEN", "")
|
||||
if token:
|
||||
api_url += f"&access_token={token}"
|
||||
urls_to_try.insert(0, api_url)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# GitHub raw URL → 转为 API
|
||||
if "github.com" in url and "/raw/" in url:
|
||||
try:
|
||||
parts = url.split("github.com/")[1]
|
||||
repo_path = parts.split("/raw/")[0]
|
||||
file_path = parts.split("/raw/", 1)[1]
|
||||
api_url = f"https://api.github.com/repos/{repo_path}/contents/{file_path}"
|
||||
urls_to_try.insert(0, api_url)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
last_err = ""
|
||||
for try_url in urls_to_try:
|
||||
try:
|
||||
req = Request(try_url, headers={"User-Agent": "xiaoli-cli/1.0"})
|
||||
resp = urlopen(req, timeout=30)
|
||||
data = resp.read()
|
||||
|
||||
# 检查是否是 JSON(API 响应)
|
||||
try:
|
||||
json_data = json.loads(data)
|
||||
if isinstance(json_data, dict) and "content" in json_data:
|
||||
data = base64.b64decode(json_data["content"])
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
pass # 不是 JSON,直接用原始数据
|
||||
|
||||
with open(dest_path, 'wb') as f:
|
||||
f.write(data)
|
||||
return True, f"下载完成 ({len(data)} 字节)"
|
||||
except Exception as e:
|
||||
last_err = str(e)
|
||||
continue
|
||||
|
||||
return False, last_err
|
||||
|
||||
def _install_deps(self, deps, optional=False):
|
||||
"""提示安装依赖(不自动安装,遵守安全规则)"""
|
||||
if not deps:
|
||||
return
|
||||
label = "可选依赖" if optional else "依赖"
|
||||
print(f"{Fore.YELLOW} 📦 {label}:{', '.join(deps)}{Style.RESET_ALL}")
|
||||
print(f"{Style.DIM} 运行: pip install {' '.join(deps)}{Style.RESET_ALL}")
|
||||
|
||||
# ── /plugin 命令入口 ──
|
||||
|
||||
def handle_plugin_command(self, args):
|
||||
"""处理 /plugin 命令"""
|
||||
args = args.strip()
|
||||
if not args:
|
||||
return self._plugin_help()
|
||||
|
||||
parts = args.split(maxsplit=1)
|
||||
sub = parts[0].lower()
|
||||
rest = parts[1] if len(parts) > 1 else ""
|
||||
|
||||
if sub == "install":
|
||||
return self._plugin_install(rest.strip())
|
||||
elif sub in ("remove", "uninstall"):
|
||||
return self._plugin_remove(rest.strip())
|
||||
elif sub == "list":
|
||||
return self._plugin_list()
|
||||
elif sub == "search":
|
||||
return self._plugin_search(rest.strip())
|
||||
elif sub == "info":
|
||||
return self._plugin_info(rest.strip())
|
||||
elif sub == "update":
|
||||
return self._plugin_update(rest.strip())
|
||||
elif sub == "add-source":
|
||||
return self._plugin_add_source(rest.strip())
|
||||
else:
|
||||
return f"{Fore.RED}❌ 未知子命令: {sub}\n{self._plugin_help()}"
|
||||
|
||||
def _plugin_help(self):
|
||||
"""插件市场帮助"""
|
||||
return f"""{Fore.CYAN}🔌 插件市场{Style.RESET_ALL}
|
||||
|
||||
{Fore.WHITE}命令:{Style.RESET_ALL}
|
||||
/plugin list 列出所有可用插件
|
||||
/plugin search <关键词> 搜索插件
|
||||
/plugin info <插件码> 查看插件详情
|
||||
/plugin install <插件码> 安装插件
|
||||
/plugin install <URL> 从 URL 安装插件
|
||||
/plugin remove <插件名> 卸载插件
|
||||
/plugin update <插件码> 更新已安装的插件
|
||||
/plugin add-source <URL> 添加远程插件源
|
||||
|
||||
{Fore.WHITE}示例:{Style.RESET_ALL}
|
||||
/plugin list 查看可用插件
|
||||
/plugin install dglab 安装 DG-Lab 插件
|
||||
/plugin search 郊狼 搜索相关插件
|
||||
/plugin remove dglab_ws 卸载插件"""
|
||||
|
||||
def _plugin_list(self):
|
||||
"""列出所有可用插件"""
|
||||
registry = self._fetch_remote_registry()
|
||||
plugins = registry.get("plugins", {})
|
||||
|
||||
if not plugins:
|
||||
return f"{Fore.YELLOW}📭 插件注册表为空,运行 /plugin install <URL> 从 URL 安装{Style.RESET_ALL}"
|
||||
|
||||
# 检查已安装状态
|
||||
plugins_dir = self._get_plugins_dir()
|
||||
installed = set()
|
||||
if os.path.exists(plugins_dir):
|
||||
for f in os.listdir(plugins_dir):
|
||||
if f.endswith('.py') and f != '__init__.py':
|
||||
installed.add(f[:-3])
|
||||
|
||||
lines = [f"{Fore.CYAN}🔌 可用插件列表:{Style.RESET_ALL}\n"]
|
||||
for code, info in sorted(plugins.items()):
|
||||
name = info.get("name", code)
|
||||
desc = info.get("description", "")
|
||||
version = info.get("version", "")
|
||||
target_file = info.get("file", f"{code}.py")
|
||||
target_name = target_file[:-3] if target_file.endswith('.py') else target_file
|
||||
|
||||
if target_name in installed:
|
||||
status = f"{Fore.GREEN}✓ 已安装{Style.RESET_ALL}"
|
||||
else:
|
||||
status = f"{Style.DIM}○ 未安装{Style.RESET_ALL}"
|
||||
|
||||
lines.append(f" {Fore.WHITE}{code:<15}{Style.RESET_ALL} {status} {name}")
|
||||
if desc:
|
||||
lines.append(f" {Style.DIM}{'':15} {desc[:50]}{Style.RESET_ALL}")
|
||||
if version:
|
||||
lines.append(f" {Style.DIM}{'':15} v{version}{Style.RESET_ALL}")
|
||||
|
||||
lines.append(f"\n{Style.DIM}使用 /plugin install <插件码> 安装{Style.RESET_ALL}")
|
||||
return "\n".join(lines)
|
||||
|
||||
def _plugin_search(self, keyword):
|
||||
"""搜索插件"""
|
||||
if not keyword:
|
||||
return f"{Fore.YELLOW}请提供搜索关键词: /plugin search <关键词>{Style.RESET_ALL}"
|
||||
|
||||
registry = self._fetch_remote_registry()
|
||||
plugins = registry.get("plugins", {})
|
||||
keyword_lower = keyword.lower()
|
||||
|
||||
matches = []
|
||||
for code, info in plugins.items():
|
||||
score = 0
|
||||
name = info.get("name", "").lower()
|
||||
desc = info.get("description", "").lower()
|
||||
keywords = [k.lower() for k in info.get("keywords", [])]
|
||||
|
||||
if keyword_lower in code.lower():
|
||||
score += 100
|
||||
if keyword_lower in name:
|
||||
score += 50
|
||||
if keyword_lower in desc:
|
||||
score += 30
|
||||
for kw in keywords:
|
||||
if keyword_lower in kw:
|
||||
score += 20
|
||||
|
||||
if score > 0:
|
||||
matches.append((score, code, info))
|
||||
|
||||
matches.sort(key=lambda x: x[0], reverse=True)
|
||||
|
||||
if not matches:
|
||||
return f"{Fore.YELLOW}🔍 未找到与 '{keyword}' 相关的插件{Style.RESET_ALL}"
|
||||
|
||||
lines = [f"{Fore.CYAN}🔍 搜索 '{keyword}' 的结果:{Style.RESET_ALL}\n"]
|
||||
for _, code, info in matches:
|
||||
name = info.get("name", code)
|
||||
desc = info.get("description", "")
|
||||
lines.append(f" {Fore.WHITE}{code:<15}{Style.RESET_ALL} {name}")
|
||||
if desc:
|
||||
lines.append(f" {Style.DIM}{'':15} {desc[:60]}{Style.RESET_ALL}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def _plugin_info(self, code):
|
||||
"""查看插件详情"""
|
||||
if not code:
|
||||
return f"{Fore.YELLOW}请提供插件码: /plugin info <插件码>{Style.RESET_ALL}"
|
||||
|
||||
registry = self._fetch_remote_registry()
|
||||
plugins = registry.get("plugins", {})
|
||||
|
||||
if code not in plugins:
|
||||
return f"{Fore.RED}❌ 未找到插件 '{code}',运行 /plugin list 查看可用插件{Style.RESET_ALL}"
|
||||
|
||||
info = plugins[code]
|
||||
name = info.get("name", code)
|
||||
desc = info.get("description", "")
|
||||
author = info.get("author", "未知")
|
||||
version = info.get("version", "未知")
|
||||
deps = info.get("deps", [])
|
||||
opt_deps = info.get("optional_deps", [])
|
||||
url = info.get("url", "")
|
||||
docs = info.get("docs", "")
|
||||
keywords = info.get("keywords", [])
|
||||
|
||||
lines = [
|
||||
f"{Fore.CYAN}📦 插件详情: {code}{Style.RESET_ALL}\n",
|
||||
f" 名称: {name}",
|
||||
f" 版本: {version}",
|
||||
f" 作者: {author}",
|
||||
]
|
||||
if desc:
|
||||
lines.append(f" 描述: {desc}")
|
||||
if keywords:
|
||||
lines.append(f" 关键词: {', '.join(keywords)}")
|
||||
if deps:
|
||||
lines.append(f" 依赖: {', '.join(deps)}")
|
||||
if opt_deps:
|
||||
lines.append(f" 可选依赖: {', '.join(opt_deps)}")
|
||||
if url:
|
||||
lines.append(f" 下载地址: {url}")
|
||||
if docs:
|
||||
lines.append(f" 文档: {docs}")
|
||||
|
||||
lines.append(f"\n {Fore.GREEN}/plugin install {code}{Style.RESET_ALL} 安装此插件")
|
||||
return "\n".join(lines)
|
||||
|
||||
def _plugin_install(self, code):
|
||||
"""安装插件"""
|
||||
if not code:
|
||||
return f"{Fore.YELLOW}请提供插件码或 URL: /plugin install <插件码|URL>{Style.RESET_ALL}"
|
||||
|
||||
plugins_dir = self._get_plugins_dir()
|
||||
os.makedirs(plugins_dir, exist_ok=True)
|
||||
|
||||
# 判断是 URL 还是插件码
|
||||
if code.startswith("http://") or code.startswith("https://"):
|
||||
return self._install_from_url(code)
|
||||
|
||||
# 从注册表查找
|
||||
registry = self._fetch_remote_registry()
|
||||
plugins = registry.get("plugins", {})
|
||||
|
||||
if code not in plugins:
|
||||
return (
|
||||
f"{Fore.RED}❌ 未找到插件 '{code}'\n"
|
||||
f"{Fore.YELLOW}运行 /plugin list 查看可用插件\n"
|
||||
f"或使用 /plugin install <URL> 从 URL 安装{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
info = plugins[code]
|
||||
name = info.get("name", code)
|
||||
url = info.get("url", "")
|
||||
target_file = info.get("file", f"{code}.py")
|
||||
deps = info.get("deps", [])
|
||||
opt_deps = info.get("optional_deps", [])
|
||||
|
||||
if not url:
|
||||
return f"{Fore.RED}❌ 插件 '{code}' 没有下载地址{Style.RESET_ALL}"
|
||||
|
||||
dest = os.path.join(plugins_dir, target_file)
|
||||
|
||||
# 检查是否已安装
|
||||
if os.path.exists(dest):
|
||||
print(f"{Fore.YELLOW}⚠ 插件 '{name}' 已存在,将覆盖更新{Style.RESET_ALL}")
|
||||
|
||||
print(f"{Fore.CYAN}📥 正在安装: {name}{Style.RESET_ALL}")
|
||||
print(f"{Style.DIM} 下载: {url}{Style.RESET_ALL}")
|
||||
|
||||
ok, msg = self._download_file(url, dest)
|
||||
if not ok:
|
||||
return f"{Fore.RED}❌ 下载失败: {msg}{Style.RESET_ALL}"
|
||||
|
||||
print(f"{Fore.GREEN} ✓ 文件已保存到: {dest}{Style.RESET_ALL}")
|
||||
|
||||
# 下载文档
|
||||
docs_url = info.get("docs", "")
|
||||
if docs_url:
|
||||
docs_dest = os.path.join(plugins_dir, target_file.replace('.py', '_README.md'))
|
||||
ok_docs, _ = self._download_file(docs_url, docs_dest)
|
||||
if ok_docs:
|
||||
print(f"{Fore.GREEN} ✓ 文档已保存到: {docs_dest}{Style.RESET_ALL}")
|
||||
|
||||
# 启用插件
|
||||
plugin_module_name = target_file[:-3] if target_file.endswith('.py') else target_file
|
||||
self._enable_plugin_in_config(plugin_module_name)
|
||||
print(f"{Fore.GREEN} ✓ 已在配置中启用: ENABLE_{plugin_module_name.upper()} = True{Style.RESET_ALL}")
|
||||
|
||||
# 提示依赖
|
||||
self._install_deps(deps, optional=False)
|
||||
self._install_deps(opt_deps, optional=True)
|
||||
|
||||
# 尝试动态加载
|
||||
print(f"{Fore.CYAN} 🔄 尝试动态加载插件...{Style.RESET_ALL}")
|
||||
try:
|
||||
spec = importlib.util.spec_from_file_location(plugin_module_name, dest)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
plugin_class = getattr(module, 'Liugin', None) or getattr(module, 'Plugin', None)
|
||||
if plugin_class:
|
||||
instance = plugin_class()
|
||||
tool_info = instance.get_tool_info()
|
||||
tool_info['handler'] = instance.handle
|
||||
if hasattr(instance, 'set_cli'):
|
||||
instance.set_cli(self)
|
||||
self.liugin_manager.tools.append(tool_info)
|
||||
print(f"{Fore.GREEN} ✓ 插件已加载: {tool_info.get('name', plugin_module_name)}{Style.RESET_ALL}")
|
||||
else:
|
||||
print(f"{Fore.YELLOW} ⚠ 未找到 Liugin/Plugin 类,重启后自动加载{Style.RESET_ALL}")
|
||||
except Exception as e:
|
||||
print(f"{Fore.YELLOW} ⚠ 动态加载失败: {e},重启后自动加载{Style.RESET_ALL}")
|
||||
|
||||
return f"\n{Fore.GREEN}🎉 插件 '{name}' 安装完成!{Style.RESET_ALL}"
|
||||
|
||||
def _install_from_url(self, url):
|
||||
"""从 URL 直接安装"""
|
||||
plugins_dir = self._get_plugins_dir()
|
||||
os.makedirs(plugins_dir, exist_ok=True)
|
||||
|
||||
# 从 URL 提取文件名
|
||||
filename = url.split('/')[-1].split('?')[0]
|
||||
if not filename.endswith('.py'):
|
||||
return f"{Fore.RED}❌ URL 必须指向 .py 文件{Style.RESET_ALL}"
|
||||
|
||||
dest = os.path.join(plugins_dir, filename)
|
||||
|
||||
print(f"{Fore.CYAN}📥 正在从 URL 安装: {url}{Style.RESET_ALL}")
|
||||
|
||||
ok, msg = self._download_file(url, dest)
|
||||
if not ok:
|
||||
return f"{Fore.RED}❌ 下载失败: {msg}{Style.RESET_ALL}"
|
||||
|
||||
plugin_name = filename[:-3]
|
||||
self._enable_plugin_in_config(plugin_name)
|
||||
print(f"{Fore.GREEN} ✓ 已保存到: {dest}{Style.RESET_ALL}")
|
||||
print(f"{Fore.GREEN} ✓ 已启用: ENABLE_{plugin_name.upper()}{Style.RESET_ALL}")
|
||||
|
||||
# 尝试动态加载
|
||||
try:
|
||||
spec = importlib.util.spec_from_file_location(plugin_name, dest)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
plugin_class = getattr(module, 'Liugin', None) or getattr(module, 'Plugin', None)
|
||||
if plugin_class:
|
||||
instance = plugin_class()
|
||||
tool_info = instance.get_tool_info()
|
||||
tool_info['handler'] = instance.handle
|
||||
if hasattr(instance, 'set_cli'):
|
||||
instance.set_cli(self)
|
||||
self.liugin_manager.tools.append(tool_info)
|
||||
print(f"{Fore.GREEN} ✓ 插件已加载: {tool_info.get('name', plugin_name)}{Style.RESET_ALL}")
|
||||
except Exception as e:
|
||||
print(f"{Fore.YELLOW} ⚠ 动态加载失败: {e},重启后自动加载{Style.RESET_ALL}")
|
||||
|
||||
return f"\n{Fore.GREEN}🎉 插件安装完成!{Style.RESET_ALL}"
|
||||
|
||||
def _plugin_remove(self, name):
|
||||
"""卸载插件"""
|
||||
if not name:
|
||||
return f"{Fore.YELLOW}请提供插件名: /plugin remove <插件名>{Style.RESET_ALL}"
|
||||
|
||||
plugins_dir = self._get_plugins_dir()
|
||||
|
||||
# 查找插件文件
|
||||
target = None
|
||||
for f in os.listdir(plugins_dir):
|
||||
if f.endswith('.py') and f != '__init__.py':
|
||||
module_name = f[:-3]
|
||||
if module_name == name or f == name:
|
||||
target = os.path.join(plugins_dir, f)
|
||||
break
|
||||
|
||||
if not target:
|
||||
return f"{Fore.RED}❌ 未找到已安装的插件 '{name}'{Style.RESET_ALL}"
|
||||
|
||||
module_name = os.path.basename(target)[:-3]
|
||||
|
||||
# 删除文件
|
||||
try:
|
||||
os.remove(target)
|
||||
print(f"{Fore.GREEN} ✓ 已删除: {target}{Style.RESET_ALL}")
|
||||
except Exception as e:
|
||||
return f"{Fore.RED}❌ 删除失败: {e}{Style.RESET_ALL}"
|
||||
|
||||
# 删除关联文档
|
||||
docs_file = target.replace('.py', '_README.md')
|
||||
if os.path.exists(docs_file):
|
||||
os.remove(docs_file)
|
||||
|
||||
# 禁用插件
|
||||
self._disable_plugin_in_config(module_name)
|
||||
print(f"{Fore.GREEN} ✓ 已禁用: ENABLE_{module_name.upper()} = False{Style.RESET_ALL}")
|
||||
|
||||
# 从内存中移除
|
||||
self.liugin_manager.tools = [
|
||||
t for t in self.liugin_manager.tools
|
||||
if t.get('name') != module_name
|
||||
]
|
||||
|
||||
return f"\n{Fore.GREEN}🗑️ 插件 '{name}' 已卸载{Style.RESET_ALL}"
|
||||
|
||||
def _plugin_update(self, code):
|
||||
"""更新插件(重新下载)"""
|
||||
if not code:
|
||||
return f"{Fore.YELLOW}请提供插件码: /plugin update <插件码>{Style.RESET_ALL}"
|
||||
|
||||
registry = self._load_registry()
|
||||
plugins = registry.get("plugins", {})
|
||||
|
||||
if code not in plugins:
|
||||
return f"{Fore.RED}❌ 注册表中未找到 '{code}',无法更新{Style.RESET_ALL}"
|
||||
|
||||
# 检查是否已安装
|
||||
info = plugins[code]
|
||||
target_file = info.get("file", f"{code}.py")
|
||||
dest = os.path.join(self._get_plugins_dir(), target_file)
|
||||
|
||||
if not os.path.exists(dest):
|
||||
return f"{Fore.YELLOW}⚠ 插件 '{code}' 未安装,使用 /plugin install {code} 安装{Style.RESET_ALL}"
|
||||
|
||||
print(f"{Fore.CYAN}🔄 更新插件: {code}{Style.RESET_ALL}")
|
||||
return self._plugin_install(code)
|
||||
|
||||
def _plugin_add_source(self, url):
|
||||
"""添加远程插件源"""
|
||||
if not url:
|
||||
return f"{Fore.YELLOW}请提供注册表 URL: /plugin add-source <URL>{Style.RESET_ALL}"
|
||||
|
||||
print(f"{Fore.CYAN}📡 正在同步插件源: {url}{Style.RESET_ALL}")
|
||||
|
||||
try:
|
||||
req = Request(url, headers={"User-Agent": "xiaoli-cli/1.0"})
|
||||
resp = urlopen(req, timeout=15)
|
||||
remote = json.loads(resp.read().decode('utf-8'))
|
||||
except Exception as e:
|
||||
return f"{Fore.RED}❌ 拉取失败: {e}{Style.RESET_ALL}"
|
||||
|
||||
registry = self._load_registry()
|
||||
remote_plugins = remote.get("plugins", {})
|
||||
added = 0
|
||||
updated = 0
|
||||
|
||||
for code, info in remote_plugins.items():
|
||||
if code in registry.get("plugins", {}):
|
||||
registry["plugins"][code] = info
|
||||
updated += 1
|
||||
else:
|
||||
registry.setdefault("plugins", {})[code] = info
|
||||
added += 1
|
||||
|
||||
self._save_registry(registry)
|
||||
|
||||
return (
|
||||
f"{Fore.GREEN}✓ 插件源同步完成: 新增 {added} 个,更新 {updated} 个{Style.RESET_ALL}"
|
||||
)
|
||||
@@ -0,0 +1,659 @@
|
||||
"""
|
||||
跨平台进程保护模块
|
||||
提供单实例保护、进程优先级提升、看门狗监控、优雅关闭等功能
|
||||
|
||||
支持 Windows / macOS / Linux
|
||||
Windows 特有: PPL (Protected Process Light) 风格保护、错误模式抑制
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import signal
|
||||
import atexit
|
||||
import threading
|
||||
import subprocess
|
||||
from typing import Optional, Callable
|
||||
|
||||
|
||||
# ── 平台检测 ──
|
||||
PLATFORM = (
|
||||
'windows' if os.name == 'nt'
|
||||
else 'macos' if sys.platform == 'darwin'
|
||||
else 'linux'
|
||||
)
|
||||
|
||||
|
||||
class ProcessProtection:
|
||||
"""跨平台进程保护器"""
|
||||
|
||||
def __init__(self, app_name: str = "xiaoli-cli"):
|
||||
self.app_name = app_name
|
||||
self._lock_file_path: Optional[str] = None
|
||||
self._lock_fd = None
|
||||
self._watchdog_thread: Optional[threading.Thread] = None
|
||||
self._watchdog_running = threading.Event()
|
||||
self._shutdown_hooks: list[Callable] = []
|
||||
self._is_protected = False
|
||||
self._original_priority = None
|
||||
self._restart_on_crash = False
|
||||
self._restart_script: Optional[str] = None
|
||||
|
||||
# ══════════════════════════════════════════════════
|
||||
# 单实例保护
|
||||
# ══════════════════════════════════════════════════
|
||||
|
||||
def acquire_single_instance(self) -> bool:
|
||||
"""
|
||||
获取单实例锁,防止重复启动
|
||||
返回 True 表示成功(当前是唯一实例),False 表示已有实例在运行
|
||||
"""
|
||||
if PLATFORM == 'windows':
|
||||
return self._acquire_windows_mutex()
|
||||
else:
|
||||
return self._acquire_posix_lock()
|
||||
|
||||
def release_single_instance(self):
|
||||
"""释放单实例锁"""
|
||||
if PLATFORM == 'windows':
|
||||
self._release_windows_mutex()
|
||||
else:
|
||||
self._release_posix_lock()
|
||||
|
||||
def _acquire_windows_mutex(self) -> bool:
|
||||
"""Windows: 使用命名 Mutex 实现单实例"""
|
||||
try:
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
mutex_name = f"Global\\{self.app_name}_single_instance"
|
||||
|
||||
# 创建命名 Mutex
|
||||
handle = kernel32.CreateMutexW(None, True, mutex_name)
|
||||
last_error = kernel32.GetLastError()
|
||||
|
||||
ERROR_ALREADY_EXISTS = 183
|
||||
if last_error == ERROR_ALREADY_EXISTS:
|
||||
kernel32.CloseHandle(handle)
|
||||
return False
|
||||
|
||||
self._win_mutex_handle = handle
|
||||
atexit.register(self.release_single_instance)
|
||||
return True
|
||||
|
||||
except Exception:
|
||||
# 降级到文件锁
|
||||
return self._acquire_posix_lock()
|
||||
|
||||
def _release_windows_mutex(self):
|
||||
"""Windows: 释放 Mutex"""
|
||||
try:
|
||||
if hasattr(self, '_win_mutex_handle') and self._win_mutex_handle:
|
||||
import ctypes
|
||||
ctypes.windll.kernel32.CloseHandle(self._win_mutex_handle)
|
||||
self._win_mutex_handle = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _acquire_posix_lock(self) -> bool:
|
||||
"""POSIX (macOS/Linux): 使用文件锁实现单实例"""
|
||||
import tempfile
|
||||
|
||||
lock_dir = tempfile.gettempdir()
|
||||
self._lock_file_path = os.path.join(lock_dir, f"{self.app_name}.lock")
|
||||
|
||||
try:
|
||||
# 尝试创建并锁定文件
|
||||
if sys.platform == 'darwin':
|
||||
# macOS: 使用 fcntl 文件锁
|
||||
import fcntl
|
||||
self._lock_fd = open(self._lock_file_path, 'w')
|
||||
try:
|
||||
fcntl.flock(self._lock_fd.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except (IOError, OSError):
|
||||
# 文件已被锁定 — 已有实例在运行
|
||||
# 检查锁文件中的 PID 是否仍在运行
|
||||
try:
|
||||
old_pid = self._lock_fd.read().strip()
|
||||
if old_pid and self._is_process_running(int(old_pid)):
|
||||
self._lock_fd.close()
|
||||
self._lock_fd = None
|
||||
return False
|
||||
else:
|
||||
# 旧进程已死,清理残留锁
|
||||
self._lock_fd.seek(0)
|
||||
self._lock_fd.truncate()
|
||||
except (ValueError, OSError):
|
||||
pass
|
||||
|
||||
self._lock_fd.seek(0)
|
||||
self._lock_fd.truncate()
|
||||
self._lock_fd.write(str(os.getpid()))
|
||||
self._lock_fd.flush()
|
||||
atexit.register(self.release_single_instance)
|
||||
return True
|
||||
else:
|
||||
# Linux: 使用 fcntl 文件锁
|
||||
import fcntl
|
||||
self._lock_fd = open(self._lock_file_path, 'w')
|
||||
try:
|
||||
fcntl.flock(self._lock_fd.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except (IOError, OSError):
|
||||
# 检查旧进程是否仍在运行
|
||||
try:
|
||||
old_pid = self._lock_fd.read().strip()
|
||||
if old_pid and self._is_process_running(int(old_pid)):
|
||||
self._lock_fd.close()
|
||||
self._lock_fd = None
|
||||
return False
|
||||
else:
|
||||
self._lock_fd.seek(0)
|
||||
self._lock_fd.truncate()
|
||||
except (ValueError, OSError):
|
||||
pass
|
||||
|
||||
self._lock_fd.seek(0)
|
||||
self._lock_fd.truncate()
|
||||
self._lock_fd.write(str(os.getpid()))
|
||||
self._lock_fd.flush()
|
||||
atexit.register(self.release_single_instance)
|
||||
return True
|
||||
|
||||
except Exception:
|
||||
# 锁获取失败,但不阻止程序运行
|
||||
return True
|
||||
|
||||
def _release_posix_lock(self):
|
||||
"""POSIX: 释放文件锁"""
|
||||
try:
|
||||
if self._lock_fd:
|
||||
if sys.platform == 'darwin':
|
||||
import fcntl
|
||||
fcntl.flock(self._lock_fd.fileno(), fcntl.LOCK_UN)
|
||||
else:
|
||||
import fcntl
|
||||
fcntl.flock(self._lock_fd.fileno(), fcntl.LOCK_UN)
|
||||
self._lock_fd.close()
|
||||
self._lock_fd = None
|
||||
if self._lock_file_path and os.path.exists(self._lock_file_path):
|
||||
try:
|
||||
os.unlink(self._lock_file_path)
|
||||
except OSError:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def _is_process_running(pid: int) -> bool:
|
||||
"""检查指定 PID 的进程是否在运行"""
|
||||
try:
|
||||
if PLATFORM == 'windows':
|
||||
import ctypes
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
||||
handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
|
||||
if handle:
|
||||
kernel32.CloseHandle(handle)
|
||||
return True
|
||||
return False
|
||||
else:
|
||||
os.kill(pid, 0)
|
||||
return True
|
||||
except (OSError, ProcessLookupError):
|
||||
return False
|
||||
|
||||
# ══════════════════════════════════════════════════
|
||||
# 进程优先级提升
|
||||
# ══════════════════════════════════════════════════
|
||||
|
||||
def elevate_priority(self, level: str = "high") -> bool:
|
||||
"""
|
||||
提升进程优先级
|
||||
|
||||
Args:
|
||||
level: "above_normal" | "high" | "realtime"(仅Windows)
|
||||
|
||||
Returns:
|
||||
是否成功提升
|
||||
"""
|
||||
try:
|
||||
if PLATFORM == 'windows':
|
||||
return self._elevate_windows(level)
|
||||
elif PLATFORM == 'macos':
|
||||
return self._elevate_macos(level)
|
||||
else:
|
||||
return self._elevate_linux(level)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _elevate_windows(self, level: str) -> bool:
|
||||
"""Windows: 使用 SetPriorityClass 提升优先级"""
|
||||
try:
|
||||
import ctypes
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
|
||||
# 优先级映射
|
||||
priority_map = {
|
||||
"above_normal": 0x00008000, # ABOVE_NORMAL_PRIORITY_CLASS
|
||||
"high": 0x00000080, # HIGH_PRIORITY_CLASS
|
||||
"realtime": 0x00000100, # REALTIME_PRIORITY_CLASS
|
||||
}
|
||||
|
||||
priority = priority_map.get(level, 0x00000080)
|
||||
|
||||
# 获取当前进程句柄
|
||||
handle = kernel32.GetCurrentProcess()
|
||||
|
||||
# 记录原始优先级
|
||||
self._original_priority = kernel32.GetPriorityClass(handle)
|
||||
|
||||
# 设置新优先级
|
||||
result = kernel32.SetPriorityClass(handle, priority)
|
||||
if result:
|
||||
self._is_protected = True
|
||||
return True
|
||||
return False
|
||||
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _elevate_macos(self, level: str) -> bool:
|
||||
"""macOS: 使用 renice 提升优先级"""
|
||||
try:
|
||||
nice_map = {
|
||||
"above_normal": -10,
|
||||
"high": -15,
|
||||
"realtime": -20,
|
||||
}
|
||||
nice_val = nice_map.get(level, -10)
|
||||
os.nice(nice_val)
|
||||
self._is_protected = True
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _elevate_linux(self, level: str) -> bool:
|
||||
"""Linux: 使用 renice 提升优先级"""
|
||||
try:
|
||||
nice_map = {
|
||||
"above_normal": -10,
|
||||
"high": -15,
|
||||
"realtime": -20,
|
||||
}
|
||||
nice_val = nice_map.get(level, -10)
|
||||
|
||||
# os.nice() 是相对值,需要先获取当前 nice 值
|
||||
try:
|
||||
import resource
|
||||
current = resource.getpriority(resource.PRIO_PROCESS, 0)
|
||||
target = nice_val
|
||||
delta = target - current
|
||||
if delta != 0:
|
||||
os.nice(delta)
|
||||
except (ImportError, OSError):
|
||||
# fallback: 直接使用 subprocess
|
||||
subprocess.run(
|
||||
["renice", str(nice_val), "-p", str(os.getpid())],
|
||||
capture_output=True, timeout=5
|
||||
)
|
||||
|
||||
self._is_protected = True
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def restore_priority(self):
|
||||
"""恢复原始进程优先级"""
|
||||
if not self._is_protected:
|
||||
return
|
||||
|
||||
try:
|
||||
if PLATFORM == 'windows' and self._original_priority:
|
||||
import ctypes
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
handle = kernel32.GetCurrentProcess()
|
||||
kernel32.SetPriorityClass(handle, self._original_priority)
|
||||
elif PLATFORM in ('macos', 'linux'):
|
||||
# 恢复到默认 nice 值 (0)
|
||||
try:
|
||||
import resource
|
||||
current = resource.getpriority(resource.PRIO_PROCESS, 0)
|
||||
os.nice(-current)
|
||||
except (ImportError, OSError):
|
||||
subprocess.run(
|
||||
["renice", "0", "-p", str(os.getpid())],
|
||||
capture_output=True, timeout=5
|
||||
)
|
||||
self._is_protected = False
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# ══════════════════════════════════════════════════
|
||||
# Windows PPL 风格保护
|
||||
# ══════════════════════════════════════════════════
|
||||
|
||||
def enable_ppl_protection(self) -> bool:
|
||||
"""
|
||||
Windows: 启用 PPL (Protected Process Light) 风格保护
|
||||
- 抑制错误弹窗
|
||||
- 设置进程缓解策略
|
||||
- 禁用 DEP 异常弹窗
|
||||
|
||||
macOS/Linux: 启用等效保护
|
||||
- 设置信号处理
|
||||
- 忽略 SIGHUP
|
||||
"""
|
||||
if PLATFORM == 'windows':
|
||||
return self._enable_windows_ppl()
|
||||
else:
|
||||
return self._enable_posix_protection()
|
||||
|
||||
def _enable_windows_ppl(self) -> bool:
|
||||
"""Windows: PPL 风格保护"""
|
||||
try:
|
||||
import ctypes
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
|
||||
# 1. 抑制系统错误弹窗 (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX)
|
||||
SEM_FAILCRITICALERRORS = 0x0001
|
||||
SEM_NOGPFAULTERRORBOX = 0x0002
|
||||
SEM_NOOPENFILEERRORBOX = 0x8000
|
||||
old_mode = kernel32.SetErrorMode(
|
||||
SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX
|
||||
)
|
||||
self._original_error_mode = old_mode
|
||||
|
||||
# 2. 设置进程为后台优先级模式(减少对用户交互的影响)
|
||||
# PROCESS_MODE_BACKGROUND_BEGIN = 0x00100000
|
||||
# kernel32.SetPriorityClass(kernel32.GetCurrentProcess(), 0x00100000)
|
||||
|
||||
# 3. 注册异常处理(防止未处理异常导致崩溃弹窗)
|
||||
try:
|
||||
import faulthandler
|
||||
faulthandler.enable()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self._is_protected = True
|
||||
return True
|
||||
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _enable_posix_protection(self) -> bool:
|
||||
"""macOS/Linux: 等效保护"""
|
||||
try:
|
||||
# 1. 忽略 SIGHUP(终端关闭时不停止进程)
|
||||
signal.signal(signal.SIGHUP, signal.SIG_IGN)
|
||||
|
||||
# 2. 注册优雅关闭信号
|
||||
def _graceful_shutdown(signum, frame):
|
||||
self._execute_shutdown_hooks()
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGTERM, _graceful_shutdown)
|
||||
|
||||
# 3. macOS 特有:忽略 SIGPIPE
|
||||
if PLATFORM == 'macos':
|
||||
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
|
||||
|
||||
# 4. 启用 faulthandler(崩溃时输出堆栈)
|
||||
try:
|
||||
import faulthandler
|
||||
faulthandler.enable()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self._is_protected = True
|
||||
return True
|
||||
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
# ══════════════════════════════════════════════════
|
||||
# 看门狗监控
|
||||
# ══════════════════════════════════════════════════
|
||||
|
||||
def start_watchdog(self, check_interval: float = 5.0,
|
||||
restart_on_crash: bool = False,
|
||||
restart_script: Optional[str] = None):
|
||||
"""
|
||||
启动看门狗线程,监控父进程状态
|
||||
当父进程退出时,自动清理并退出
|
||||
|
||||
Args:
|
||||
check_interval: 检查间隔(秒)
|
||||
restart_on_crash: 崩溃时是否自动重启
|
||||
restart_script: 重启脚本路径
|
||||
"""
|
||||
self._restart_on_crash = restart_on_crash
|
||||
self._restart_script = restart_script
|
||||
|
||||
if self._watchdog_thread and self._watchdog_thread.is_alive():
|
||||
return
|
||||
|
||||
self._watchdog_running.set()
|
||||
self._watchdog_thread = threading.Thread(
|
||||
target=self._watchdog_loop,
|
||||
args=(check_interval,),
|
||||
daemon=True,
|
||||
name="process-watchdog"
|
||||
)
|
||||
self._watchdog_thread.start()
|
||||
|
||||
def stop_watchdog(self):
|
||||
"""停止看门狗"""
|
||||
self._watchdog_running.clear()
|
||||
if self._watchdog_thread:
|
||||
self._watchdog_thread.join(timeout=3)
|
||||
self._watchdog_thread = None
|
||||
|
||||
def _watchdog_loop(self, check_interval: float):
|
||||
"""看门狗主循环"""
|
||||
parent_pid = os.getppid()
|
||||
|
||||
while self._watchdog_running.is_set():
|
||||
try:
|
||||
# 检查父进程是否仍在运行
|
||||
if not self._is_process_running(parent_pid):
|
||||
# 父进程已退出,执行清理
|
||||
self._execute_shutdown_hooks()
|
||||
if self._restart_on_crash and self._restart_script:
|
||||
self._do_restart()
|
||||
else:
|
||||
os._exit(0)
|
||||
break
|
||||
|
||||
time.sleep(check_interval)
|
||||
|
||||
except Exception:
|
||||
time.sleep(check_interval)
|
||||
|
||||
def _do_restart(self):
|
||||
"""执行重启"""
|
||||
try:
|
||||
if self._restart_script and os.path.exists(self._restart_script):
|
||||
if PLATFORM == 'windows':
|
||||
subprocess.Popen(
|
||||
["python", self._restart_script],
|
||||
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
|
||||
)
|
||||
else:
|
||||
subprocess.Popen(
|
||||
["python3", self._restart_script],
|
||||
start_new_session=True
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
os._exit(0)
|
||||
|
||||
# ══════════════════════════════════════════════════
|
||||
# 关闭钩子
|
||||
# ══════════════════════════════════════════════════
|
||||
|
||||
def register_shutdown_hook(self, hook: Callable):
|
||||
"""注册关闭钩子,在进程退出时执行"""
|
||||
if hook not in self._shutdown_hooks:
|
||||
self._shutdown_hooks.append(hook)
|
||||
|
||||
def _execute_shutdown_hooks(self):
|
||||
"""执行所有关闭钩子"""
|
||||
for hook in reversed(self._shutdown_hooks):
|
||||
try:
|
||||
hook()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# ══════════════════════════════════════════════════
|
||||
# 一键启用全部保护
|
||||
# ══════════════════════════════════════════════════
|
||||
|
||||
def enable_all(self, priority_level: str = "above_normal",
|
||||
watchdog: bool = True,
|
||||
restart_on_crash: bool = False,
|
||||
restart_script: Optional[str] = None) -> dict:
|
||||
"""
|
||||
一键启用所有进程保护
|
||||
|
||||
Args:
|
||||
priority_level: 优先级 ("above_normal" | "high")
|
||||
watchdog: 是否启用看门狗
|
||||
restart_on_crash: 崩溃时是否自动重启
|
||||
restart_script: 重启脚本路径
|
||||
|
||||
Returns:
|
||||
各项保护的启用状态字典
|
||||
"""
|
||||
results = {
|
||||
"single_instance": False,
|
||||
"priority": False,
|
||||
"ppl_protection": False,
|
||||
"watchdog": False,
|
||||
"platform": PLATFORM,
|
||||
}
|
||||
|
||||
# 1. 单实例保护
|
||||
results["single_instance"] = self.acquire_single_instance()
|
||||
|
||||
# 2. 进程优先级提升
|
||||
results["priority"] = self.elevate_priority(priority_level)
|
||||
|
||||
# 3. PPL 风格保护
|
||||
results["ppl_protection"] = self.enable_ppl_protection()
|
||||
|
||||
# 4. 看门狗
|
||||
if watchdog:
|
||||
self.start_watchdog(
|
||||
restart_on_crash=restart_on_crash,
|
||||
restart_script=restart_script
|
||||
)
|
||||
results["watchdog"] = True
|
||||
|
||||
# 注册清理
|
||||
atexit.register(self.disable_all)
|
||||
|
||||
return results
|
||||
|
||||
def disable_all(self):
|
||||
"""禁用所有保护并清理"""
|
||||
self.stop_watchdog()
|
||||
self.restore_priority()
|
||||
self.release_single_instance()
|
||||
self._execute_shutdown_hooks()
|
||||
self._is_protected = False
|
||||
|
||||
def get_status(self) -> dict:
|
||||
"""获取当前保护状态"""
|
||||
return {
|
||||
"platform": PLATFORM,
|
||||
"is_protected": self._is_protected,
|
||||
"single_instance_held": (
|
||||
self._lock_fd is not None or
|
||||
hasattr(self, '_win_mutex_handle') and self._win_mutex_handle is not None
|
||||
),
|
||||
"watchdog_running": (
|
||||
self._watchdog_thread is not None and
|
||||
self._watchdog_thread.is_alive()
|
||||
),
|
||||
"shutdown_hooks_count": len(self._shutdown_hooks),
|
||||
}
|
||||
|
||||
|
||||
# ── 全局单例 ──
|
||||
_protection: Optional[ProcessProtection] = None
|
||||
|
||||
|
||||
def get_protection() -> ProcessProtection:
|
||||
"""获取全局进程保护器单例"""
|
||||
global _protection
|
||||
if _protection is None:
|
||||
_protection = ProcessProtection()
|
||||
return _protection
|
||||
|
||||
|
||||
def enable_process_protection(priority_level: str = "above_normal",
|
||||
watchdog: bool = True,
|
||||
restart_on_crash: bool = False) -> dict:
|
||||
"""
|
||||
快捷函数:启用进程保护
|
||||
|
||||
Returns:
|
||||
各项保护状态字典
|
||||
"""
|
||||
return get_protection().enable_all(
|
||||
priority_level=priority_level,
|
||||
watchdog=watchdog,
|
||||
restart_on_crash=restart_on_crash,
|
||||
)
|
||||
|
||||
|
||||
def disable_process_protection():
|
||||
"""快捷函数:禁用进程保护"""
|
||||
get_protection().disable_all()
|
||||
|
||||
|
||||
def get_protection_status() -> dict:
|
||||
"""快捷函数:获取保护状态"""
|
||||
return get_protection().get_status()
|
||||
|
||||
|
||||
# ── CLI 入口(可独立运行测试) ──
|
||||
if __name__ == "__main__":
|
||||
import json
|
||||
|
||||
print(f"平台: {PLATFORM}")
|
||||
print(f"PID: {os.getpid()}")
|
||||
print()
|
||||
|
||||
prot = ProcessProtection("xiaoli-cli-test")
|
||||
|
||||
# 测试单实例
|
||||
is_first = prot.acquire_single_instance()
|
||||
print(f"单实例锁: {'获取成功 (首个实例)' if is_first else '获取失败 (已有实例)'}")
|
||||
|
||||
if is_first:
|
||||
# 测试优先级提升
|
||||
prio_ok = prot.elevate_priority("above_normal")
|
||||
print(f"优先级提升: {'成功' if prio_ok else '失败'}")
|
||||
|
||||
# 测试 PPL 保护
|
||||
ppl_ok = prot.enable_ppl_protection()
|
||||
print(f"PPL 保护: {'启用' if ppl_ok else '失败'}")
|
||||
|
||||
# 测试看门狗
|
||||
prot.start_watchdog(check_interval=2.0)
|
||||
print(f"看门狗: 已启动")
|
||||
|
||||
# 显示状态
|
||||
status = prot.get_status()
|
||||
print(f"\n状态: {json.dumps(status, indent=2, ensure_ascii=False)}")
|
||||
|
||||
print("\n按 Ctrl+C 退出...")
|
||||
try:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
print("\n正在清理...")
|
||||
prot.disable_all()
|
||||
print("完成")
|
||||
@@ -18,16 +18,6 @@ MODE_NAMES = {
|
||||
MODE_MANUAL: "人工确认",
|
||||
}
|
||||
|
||||
# ── 始终需要人工确认的指令(无论模式) ──
|
||||
ALWAYS_CONFIRM = {
|
||||
("cmd_executor", "run rm"),
|
||||
("cmd_executor", "run chmod"),
|
||||
("cmd_executor", "run chown"),
|
||||
("file_manager", "delete"),
|
||||
("git_tools", "checkout -b"),
|
||||
}
|
||||
|
||||
|
||||
class SafetyLayer:
|
||||
"""统一安全检查层"""
|
||||
|
||||
@@ -65,17 +55,6 @@ class SafetyLayer:
|
||||
if self.mode == MODE_UNRESTRICTED:
|
||||
return True, ""
|
||||
|
||||
# 检查是否是始终需要确认的指令
|
||||
args_lower = tool_args.strip().lower()
|
||||
for (t, pattern) in ALWAYS_CONFIRM:
|
||||
if tool_name == t and args_lower.startswith(pattern.split(" ", 1)[1] if " " in pattern else ""):
|
||||
return self._ask_user_confirm(
|
||||
tool_name, tool_args,
|
||||
risk_level="高",
|
||||
impact="此操作始终需要人工确认",
|
||||
reason="敏感指令"
|
||||
)
|
||||
|
||||
# 人工确认模式:所有指令都确认
|
||||
if self.mode == MODE_MANUAL:
|
||||
return self._ask_user_confirm(
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
统一工具返回格式 - ToolResult
|
||||
|
||||
所有插件和工具管理器使用统一的返回格式,
|
||||
替代原来混乱的字符串/Dict 返回。
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Optional
|
||||
|
||||
|
||||
# 错误码常量
|
||||
class ErrorCode:
|
||||
"""工具错误码"""
|
||||
SUCCESS = 0
|
||||
# 参数错误 (1xx)
|
||||
MISSING_ARGS = 100
|
||||
INVALID_ARGS = 101
|
||||
UNSUPPORTED_OP = 102
|
||||
# 资源错误 (2xx)
|
||||
NOT_FOUND = 200
|
||||
FILE_NOT_FOUND = 201
|
||||
PERMISSION_DENIED = 202
|
||||
# 执行错误 (3xx)
|
||||
EXEC_FAILED = 300
|
||||
TIMEOUT = 301
|
||||
DECODE_ERROR = 302
|
||||
# 系统错误 (4xx)
|
||||
INTERNAL_ERROR = 400
|
||||
NOT_IMPLEMENTED = 401
|
||||
|
||||
_NAMES = {
|
||||
0: "成功",
|
||||
100: "缺少参数",
|
||||
101: "参数无效",
|
||||
102: "不支持的操作",
|
||||
200: "未找到",
|
||||
201: "文件不存在",
|
||||
202: "权限不足",
|
||||
300: "执行失败",
|
||||
301: "超时",
|
||||
302: "解码错误",
|
||||
400: "内部错误",
|
||||
401: "未实现",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def name(cls, code: int) -> str:
|
||||
return cls._NAMES.get(code, f"未知({code})")
|
||||
|
||||
|
||||
@dataclass
|
||||
class ToolResult:
|
||||
"""统一工具返回格式"""
|
||||
|
||||
success: bool
|
||||
data: Any = None
|
||||
error_code: int = ErrorCode.SUCCESS
|
||||
error_message: str = ""
|
||||
|
||||
# 可选元数据
|
||||
tool_name: str = ""
|
||||
execution_time: float = 0.0
|
||||
metadata: dict = field(default_factory=dict)
|
||||
|
||||
def __str__(self) -> str:
|
||||
if self.success:
|
||||
return str(self.data) if self.data is not None else "执行完成"
|
||||
return f"[{ErrorCode.name(self.error_code)}] {self.error_message}"
|
||||
|
||||
# ── 便捷构造方法 ──
|
||||
|
||||
@classmethod
|
||||
def ok(cls, data: Any = None, **kwargs) -> "ToolResult":
|
||||
"""成功"""
|
||||
return cls(success=True, data=data, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def fail(cls, message: str, code: int = ErrorCode.EXEC_FAILED, **kwargs) -> "ToolResult":
|
||||
"""失败"""
|
||||
return cls(success=False, error_code=code, error_message=message, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def missing(cls, what: str = "参数", **kwargs) -> "ToolResult":
|
||||
"""缺少参数"""
|
||||
return cls.fail(f"缺少{what}", ErrorCode.MISSING_ARGS, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def not_found(cls, what: str = "资源", **kwargs) -> "ToolResult":
|
||||
"""未找到"""
|
||||
return cls.fail(f"{what}不存在", ErrorCode.NOT_FOUND, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def timeout(cls, seconds: int = 0, **kwargs) -> "ToolResult":
|
||||
"""超时"""
|
||||
msg = f"执行超时"
|
||||
if seconds:
|
||||
msg += f" ({seconds}秒)"
|
||||
return cls.fail(msg, ErrorCode.TIMEOUT, **kwargs)
|
||||
|
||||
# ── 格式转换 ──
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""转为普通 dict(兼容旧接口)"""
|
||||
if self.success:
|
||||
return {"result": self.data if self.data is not None else "执行完成"}
|
||||
return {"result": str(self)}
|
||||
|
||||
def to_mcp(self) -> dict:
|
||||
"""转为 MCP 协议格式"""
|
||||
text = str(self)
|
||||
return {
|
||||
"content": [{"type": "text", "text": text}],
|
||||
"isError": not self.success,
|
||||
}
|
||||
|
||||
def to_string(self) -> str:
|
||||
"""转为字符串(兼容旧插件 handle() 返回值)"""
|
||||
return str(self)
|
||||
+82
-52
@@ -62,8 +62,7 @@ if TEXTUAL_AVAILABLE:
|
||||
scrollbar-color: {_Theme.BORDER};
|
||||
scrollbar-color-hover: {_Theme.TEXT_MUTED};
|
||||
padding: 0 1;
|
||||
/* 平滑滚动 */
|
||||
scroll-behavior: smooth;
|
||||
|
||||
}}
|
||||
|
||||
/* ── 输入区域 ── */
|
||||
@@ -144,10 +143,11 @@ if TEXTUAL_AVAILABLE:
|
||||
}}
|
||||
|
||||
#sidebar.hidden {{
|
||||
display: none;
|
||||
width: 0;
|
||||
min-width: 0;
|
||||
opacity: 0;
|
||||
border: none;
|
||||
overflow: hidden;
|
||||
}}
|
||||
|
||||
.sidebar-header {{
|
||||
@@ -297,28 +297,6 @@ if TEXTUAL_AVAILABLE:
|
||||
background: {_Theme.BG};
|
||||
}}
|
||||
|
||||
/* ── 闪光动画 keyframes ── */
|
||||
@keyframes pulse-glow {{
|
||||
0% {{ color: {_Theme.THINKING}; }}
|
||||
50% {{ color: {_Theme.ACCENT}; }}
|
||||
100% {{ color: {_Theme.THINKING}; }}
|
||||
}}
|
||||
|
||||
@keyframes fade-in {{
|
||||
from {{ opacity: 0; }}
|
||||
to {{ opacity: 1; }}
|
||||
}}
|
||||
|
||||
@keyframes slide-in-right {{
|
||||
from {{ offset-x: 100%; }}
|
||||
to {{ offset-x: 0; }}
|
||||
}}
|
||||
|
||||
@keyframes bounce-in {{
|
||||
0% {{ opacity: 0; offset-y: 5%; }}
|
||||
60% {{ opacity: 1; offset-y: -1%; }}
|
||||
100% {{ opacity: 1; offset-y: 0; }}
|
||||
}}
|
||||
"""
|
||||
|
||||
class _TUIBridge:
|
||||
@@ -428,6 +406,23 @@ if TEXTUAL_AVAILABLE:
|
||||
self._running = False
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════
|
||||
# 自定义 TextArea:回车发送,Shift+回车换行
|
||||
# ═══════════════════════════════════════════════════
|
||||
|
||||
class SendTextArea(TextArea):
|
||||
"""回车发送消息,Shift+回车换行的输入框"""
|
||||
async def _on_key(self, event):
|
||||
if event.key in ("enter", "\r", "\n"):
|
||||
# prevent_default 阻止父类 TextArea._on_key 执行
|
||||
event.prevent_default()
|
||||
try:
|
||||
self.app.action_send_message()
|
||||
except Exception:
|
||||
pass
|
||||
return
|
||||
# 其他键正常处理
|
||||
|
||||
# ═══════════════════════════════════════════════════
|
||||
# TUI 主应用
|
||||
# ═══════════════════════════════════════════════════
|
||||
@@ -442,7 +437,8 @@ if TEXTUAL_AVAILABLE:
|
||||
Binding("ctrl+c", "quit", "退出", show=True),
|
||||
Binding("ctrl+l", "clear", "清屏", show=True),
|
||||
Binding("ctrl+n", "new_chat", "新对话", show=True),
|
||||
Binding("ctrl+enter", "send_message", "发送", show=True),
|
||||
Binding("ctrl+enter", "send_message", "发送", show=False),
|
||||
Binding("shift+enter", "newline", "换行", show=False),
|
||||
Binding("f1", "toggle_sidebar", "侧栏", show=True),
|
||||
Binding("escape", "cancel", "取消", show=False),
|
||||
]
|
||||
@@ -464,14 +460,14 @@ if TEXTUAL_AVAILABLE:
|
||||
yield VerticalScroll(id="chat-scroll")
|
||||
with Vertical(id="input-area"):
|
||||
with Container(id="input-wrapper"):
|
||||
yield TextArea(
|
||||
placeholder="输入消息... (Ctrl+Enter 发送, Enter 换行)",
|
||||
yield SendTextArea(
|
||||
placeholder="输入消息... (回车发送, Shift+回车换行)",
|
||||
id="user-input",
|
||||
soft_wrap=True,
|
||||
tab_behavior="indent",
|
||||
)
|
||||
yield Static(
|
||||
" Ctrl+Enter 发送 | ↑↓ 历史 | Ctrl+L 清屏 | F1 侧栏 | Tab 缩进",
|
||||
" 回车发送 | Shift+回车换行 | Ctrl+L 清屏 | F1 侧栏",
|
||||
id="input-hint"
|
||||
)
|
||||
with Vertical(id="sidebar"):
|
||||
@@ -502,7 +498,7 @@ if TEXTUAL_AVAILABLE:
|
||||
"",
|
||||
" ╔══════════════════════════════════════════════╗",
|
||||
" ║ ║",
|
||||
" ║ 小狸 Pro-CLI v5.1 ║",
|
||||
" ║ 小狸 Pro-CLI v5.4.1 ║",
|
||||
" ║ 智能编程助手 · 动画增强版 ║",
|
||||
" ║ ║",
|
||||
" ╚══════════════════════════════════════════════╝",
|
||||
@@ -510,7 +506,7 @@ if TEXTUAL_AVAILABLE:
|
||||
" 代码编辑 · 代码搜索 · Git 集成 · 多引擎",
|
||||
" 输入 /help 查看命令 | /model 切换引擎",
|
||||
f" 当前引擎: {self.bridge.current_engine()} | 工具: {len(self.bridge.tools())} 个",
|
||||
" 支持多行输入 — Ctrl+Enter 发送",
|
||||
" 支持多行输入 — Shift+回车换行",
|
||||
"",
|
||||
]
|
||||
|
||||
@@ -531,12 +527,12 @@ if TEXTUAL_AVAILABLE:
|
||||
|
||||
widget = Static(line, classes=cls)
|
||||
scroll.mount(widget)
|
||||
# 延迟显示每一行
|
||||
delay = i * 0.1
|
||||
# 延迟显示每一行(避免 delay=0 导致 Textual 除零错误)
|
||||
delay = max(0.01, i * 0.1)
|
||||
self.set_timer(delay, lambda w=widget: w.add_class("visible"))
|
||||
|
||||
# 最后滚动到底部
|
||||
self.set_timer(len(welcome_lines) * 0.1 + 0.1,
|
||||
self.set_timer(max(0.01, len(welcome_lines) * 0.1 + 0.1),
|
||||
lambda: scroll.scroll_end(animate=True, duration=0.3))
|
||||
|
||||
def _render_welcome(self):
|
||||
@@ -546,7 +542,7 @@ if TEXTUAL_AVAILABLE:
|
||||
("", "msg-dim"),
|
||||
(" ╔══════════════════════════════════════════════╗", "msg-welcome"),
|
||||
(" ║ ║", "msg-welcome"),
|
||||
(" ║ 小狸 Pro-CLI v5.1 ║", "msg-welcome"),
|
||||
(" ║ 小狸 Pro-CLI v5.4.1 ║", "msg-welcome"),
|
||||
(" ║ 智能编程助手 · 动画增强版 ║", "msg-welcome"),
|
||||
(" ║ ║", "msg-welcome"),
|
||||
(" ╚══════════════════════════════════════════════╝", "msg-welcome"),
|
||||
@@ -554,7 +550,7 @@ if TEXTUAL_AVAILABLE:
|
||||
(" 代码编辑 · 代码搜索 · Git 集成 · 多引擎", "msg-system"),
|
||||
(" 输入 /help 查看命令 | /model 切换引擎", "msg-system"),
|
||||
(f" 当前引擎: {self.bridge.current_engine()} | 工具: {len(self.bridge.tools())} 个", "msg-system"),
|
||||
(" 支持多行输入 — Ctrl+Enter 发送", "msg-system"),
|
||||
(" 支持多行输入 — Shift+回车换行", "msg-system"),
|
||||
("", "msg-dim"),
|
||||
]
|
||||
for text, cls in lines:
|
||||
@@ -584,6 +580,10 @@ if TEXTUAL_AVAILABLE:
|
||||
status_container.mount(Static(f" 工具: {len(self.bridge.tools())} 个", classes="tool-item"))
|
||||
status_container.mount(Static(f" 引擎: {len(self.bridge.engines())} 个", classes="tool-item"))
|
||||
|
||||
# 工具数
|
||||
if hasattr(self.cli, 'liugin_manager'):
|
||||
status_container.mount(Static(f" 工具: {len(self.cli.liugin_manager.tools)} 个", classes="tool-item"))
|
||||
|
||||
def _update_status(self, text: str):
|
||||
bar = self.query_one("#status-bar")
|
||||
engine = self.bridge.current_engine()
|
||||
@@ -688,8 +688,14 @@ if TEXTUAL_AVAILABLE:
|
||||
|
||||
# ── 多行输入处理 ──
|
||||
|
||||
def action_newline(self):
|
||||
"""Shift+Enter 在输入框插入换行"""
|
||||
text_area = self.query_one("#user-input")
|
||||
if text_area.has_focus:
|
||||
text_area.action_edit_insert_newline()
|
||||
|
||||
def action_send_message(self):
|
||||
"""Ctrl+Enter 发送消息"""
|
||||
"""回车发送消息"""
|
||||
text_area = self.query_one("#user-input")
|
||||
text = text_area.text.strip()
|
||||
if not text:
|
||||
@@ -737,11 +743,12 @@ if TEXTUAL_AVAILABLE:
|
||||
'cls': lambda: self.action_clear(),
|
||||
'model': lambda: self._switch_model(args),
|
||||
'engine': lambda: self._switch_model(args),
|
||||
'about': lambda: self._system(" 小狸 Pro-CLI v5.1 - 智能编程助手"),
|
||||
'about': lambda: self._system(" 小狸 Pro-CLI v5.4.1 - 智能编程助手"),
|
||||
'status': lambda: self._show_status(),
|
||||
'tools': lambda: self._show_tools(),
|
||||
'engines': lambda: self._show_engines(),
|
||||
'tui': lambda: self._system("已在 TUI 模式中"),
|
||||
'manual': lambda: self._handle_manual(args),
|
||||
}
|
||||
|
||||
handler = cmds.get(name)
|
||||
@@ -771,11 +778,29 @@ if TEXTUAL_AVAILABLE:
|
||||
|
||||
⌨ 快捷键:
|
||||
Ctrl+C 退出 Ctrl+L 清屏
|
||||
Ctrl+N 新对话 Ctrl+Enter 发送消息
|
||||
F1 侧栏 Escape 取消生成
|
||||
Enter 换行 Tab 缩进"""
|
||||
Ctrl+N 新对话 回车 发送消息
|
||||
Shift+回车 换行 F1 侧栏
|
||||
Escape 取消生成 Tab 缩进"""
|
||||
self._system(help_text)
|
||||
|
||||
def _handle_manual(self, args):
|
||||
"""处理 /manual 命令"""
|
||||
if 'manual' not in self.cli.engines:
|
||||
self._error("manual 引擎未加载")
|
||||
return
|
||||
if not args:
|
||||
# 切换到 manual 引擎
|
||||
if self.bridge.switch_engine('manual'):
|
||||
self._system("已切换到 manual 引擎")
|
||||
self._update_sidebar()
|
||||
else:
|
||||
self._error("切换失败")
|
||||
else:
|
||||
# 执行 manual 引擎子命令
|
||||
result = self.cli.engines['manual'].handle_command(args)
|
||||
if result:
|
||||
self._system(result)
|
||||
|
||||
def _switch_cli(self):
|
||||
self._system("切换到命令行模式...")
|
||||
self.cli.tui_output_callback = None
|
||||
@@ -810,11 +835,15 @@ if TEXTUAL_AVAILABLE:
|
||||
self._system('\n'.join(lines))
|
||||
|
||||
def _show_status(self):
|
||||
self._system(f""" 系统状态:
|
||||
引擎: {self.bridge.current_engine()}
|
||||
引擎数: {len(self.bridge.engines())}
|
||||
工具数: {len(self.bridge.tools())}
|
||||
对话数: {len(self.bridge.history())}""")
|
||||
status_lines = [
|
||||
f" 系统状态:",
|
||||
f" 引擎: {self.bridge.current_engine()}",
|
||||
f" 引擎数: {len(self.bridge.engines())}",
|
||||
f" 工具数: {len(self.bridge.tools())}",
|
||||
f" 对话数: {len(self.bridge.history())}",
|
||||
]
|
||||
|
||||
self._system('\n'.join(status_lines))
|
||||
|
||||
# ── AI 生成 ──
|
||||
|
||||
@@ -849,11 +878,11 @@ if TEXTUAL_AVAILABLE:
|
||||
self.call_after_refresh(self._update_sidebar)
|
||||
|
||||
def _write_raw(self, msg):
|
||||
if '' in msg or 'OK 工具' in msg:
|
||||
if '✅' in msg or 'OK 工具' in msg:
|
||||
self._add(f" {msg}", "msg-tool-ok")
|
||||
elif '' in msg or 'X 工具' in msg or '错误' in msg:
|
||||
elif '❌' in msg or 'X 工具' in msg or '错误' in msg:
|
||||
self._add(f" {msg}", "msg-tool-err")
|
||||
elif '' in msg:
|
||||
elif '🤖' in msg:
|
||||
self._add(f" {msg}", "msg-ai")
|
||||
elif '工具' in msg and ('调用' in msg or '执行' in msg):
|
||||
self._add(f" {msg}", "msg-tool-ok")
|
||||
@@ -874,12 +903,13 @@ if TEXTUAL_AVAILABLE:
|
||||
|
||||
def action_toggle_sidebar(self):
|
||||
sidebar = self.query_one("#sidebar")
|
||||
sidebar.visible = not sidebar.visible
|
||||
# 动画:侧栏切换时平滑过渡
|
||||
if sidebar.visible:
|
||||
# 通过 CSS 类控制侧栏显隐
|
||||
if sidebar.has_class("hidden"):
|
||||
sidebar.remove_class("hidden")
|
||||
self.sidebar_visible = True
|
||||
else:
|
||||
sidebar.add_class("hidden")
|
||||
self.sidebar_visible = False
|
||||
|
||||
def action_cancel(self):
|
||||
if self.is_generating:
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user