lingclaude 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lingclaude-0.3.0/LICENSE +21 -0
- lingclaude-0.3.0/PKG-INFO +399 -0
- lingclaude-0.3.0/README.md +380 -0
- lingclaude-0.3.0/lingclaude/__init__.py +25 -0
- lingclaude-0.3.0/lingclaude/api.py +514 -0
- lingclaude-0.3.0/lingclaude/cli/__init__.py +1 -0
- lingclaude-0.3.0/lingclaude/cli/__main__.py +7 -0
- lingclaude-0.3.0/lingclaude/cli/app.py +389 -0
- lingclaude-0.3.0/lingclaude/core/__init__.py +40 -0
- lingclaude-0.3.0/lingclaude/core/behavior.py +191 -0
- lingclaude-0.3.0/lingclaude/core/behavior_aware_router.py +282 -0
- lingclaude-0.3.0/lingclaude/core/config.py +197 -0
- lingclaude-0.3.0/lingclaude/core/context_cache.py +423 -0
- lingclaude-0.3.0/lingclaude/core/intel.py +573 -0
- lingclaude-0.3.0/lingclaude/core/models.py +32 -0
- lingclaude-0.3.0/lingclaude/core/permissions.py +28 -0
- lingclaude-0.3.0/lingclaude/core/query_engine.py +794 -0
- lingclaude-0.3.0/lingclaude/core/session.py +223 -0
- lingclaude-0.3.0/lingclaude/core/task_aggregation.py +593 -0
- lingclaude-0.3.0/lingclaude/core/task_scheduler.py +278 -0
- lingclaude-0.3.0/lingclaude/core/token_monitor.py +834 -0
- lingclaude-0.3.0/lingclaude/core/types.py +30 -0
- lingclaude-0.3.0/lingclaude/engine/__init__.py +16 -0
- lingclaude-0.3.0/lingclaude/engine/ast_edit.py +243 -0
- lingclaude-0.3.0/lingclaude/engine/bash.py +155 -0
- lingclaude-0.3.0/lingclaude/engine/bash_lingxi.py +192 -0
- lingclaude-0.3.0/lingclaude/engine/coding.py +582 -0
- lingclaude-0.3.0/lingclaude/engine/file_edit.py +326 -0
- lingclaude-0.3.0/lingclaude/engine/file_ops.py +195 -0
- lingclaude-0.3.0/lingclaude/engine/file_read.py +241 -0
- lingclaude-0.3.0/lingclaude/engine/git.py +211 -0
- lingclaude-0.3.0/lingclaude/engine/grep.py +159 -0
- lingclaude-0.3.0/lingclaude/engine/indexer.py +216 -0
- lingclaude-0.3.0/lingclaude/engine/stt.py +207 -0
- lingclaude-0.3.0/lingclaude/engine/tools.py +61 -0
- lingclaude-0.3.0/lingclaude/mcp/__init__.py +5 -0
- lingclaude-0.3.0/lingclaude/mcp/lingxi_client.py +318 -0
- lingclaude-0.3.0/lingclaude/mcp/server.py +494 -0
- lingclaude-0.3.0/lingclaude/model/__init__.py +21 -0
- lingclaude-0.3.0/lingclaude/model/anthropic_provider.py +219 -0
- lingclaude-0.3.0/lingclaude/model/factory.py +86 -0
- lingclaude-0.3.0/lingclaude/model/intelligent_router.py +472 -0
- lingclaude-0.3.0/lingclaude/model/openai_provider.py +308 -0
- lingclaude-0.3.0/lingclaude/model/types.py +140 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/__init__.py +27 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/advisor.py +198 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/daemon.py +357 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/evaluator.py +144 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/learner/__init__.py +55 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/learner/knowledge.py +364 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/learner/models.py +79 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/learner/patterns.py +416 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/learner/rule_extractor.py +246 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/optimizer.py +162 -0
- lingclaude-0.3.0/lingclaude/self_optimizer/trigger.py +286 -0
- lingclaude-0.3.0/lingclaude.egg-info/PKG-INFO +399 -0
- lingclaude-0.3.0/lingclaude.egg-info/SOURCES.txt +81 -0
- lingclaude-0.3.0/lingclaude.egg-info/dependency_links.txt +1 -0
- lingclaude-0.3.0/lingclaude.egg-info/entry_points.txt +3 -0
- lingclaude-0.3.0/lingclaude.egg-info/requires.txt +12 -0
- lingclaude-0.3.0/lingclaude.egg-info/top_level.txt +1 -0
- lingclaude-0.3.0/pyproject.toml +43 -0
- lingclaude-0.3.0/setup.cfg +4 -0
- lingclaude-0.3.0/tests/test_adaptive.py +273 -0
- lingclaude-0.3.0/tests/test_agent_loop.py +398 -0
- lingclaude-0.3.0/tests/test_ast_edit.py +415 -0
- lingclaude-0.3.0/tests/test_behavior.py +156 -0
- lingclaude-0.3.0/tests/test_behavior_aware_router.py +207 -0
- lingclaude-0.3.0/tests/test_behavior_trigger.py +218 -0
- lingclaude-0.3.0/tests/test_core.py +556 -0
- lingclaude-0.3.0/tests/test_daemon.py +232 -0
- lingclaude-0.3.0/tests/test_daemon_extras.py +126 -0
- lingclaude-0.3.0/tests/test_file_edit.py +225 -0
- lingclaude-0.3.0/tests/test_file_read.py +165 -0
- lingclaude-0.3.0/tests/test_git.py +274 -0
- lingclaude-0.3.0/tests/test_grep.py +129 -0
- lingclaude-0.3.0/tests/test_indexer.py +293 -0
- lingclaude-0.3.0/tests/test_intel.py +598 -0
- lingclaude-0.3.0/tests/test_mcp_server.py +300 -0
- lingclaude-0.3.0/tests/test_model.py +558 -0
- lingclaude-0.3.0/tests/test_optimization_integration.py +239 -0
- lingclaude-0.3.0/tests/test_strict.py +317 -0
- lingclaude-0.3.0/tests/test_stt.py +210 -0
lingclaude-0.3.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 LingClaude Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: lingclaude
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: 灵克 (LingClaude) — 开源 AI 编程助手,内置自优化能力
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: tiktoken>=0.5.0
|
|
10
|
+
Requires-Dist: aiohttp>=3.8.0
|
|
11
|
+
Requires-Dist: pyyaml>=6.0
|
|
12
|
+
Requires-Dist: mcp>=1.0
|
|
13
|
+
Provides-Extra: dev
|
|
14
|
+
Requires-Dist: pytest>=7.4.0; extra == "dev"
|
|
15
|
+
Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
|
|
16
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
|
|
17
|
+
Provides-Extra: optimization
|
|
18
|
+
Requires-Dist: optuna>=3.3.0; extra == "optimization"
|
|
19
|
+
|
|
20
|
+
# 灵克 (LingClaude)
|
|
21
|
+
|
|
22
|
+
> 开源 AI 编程助手,对标 Claude Code,内置自优化能力——越用越懂你。
|
|
23
|
+
|
|
24
|
+
**Version**: 0.2.1 | **Python**: >=3.10 | **License**: MIT
|
|
25
|
+
|
|
26
|
+
## 为什么做灵克?
|
|
27
|
+
|
|
28
|
+
Claude Code 很强,但它是闭源的、云端的、不可定制的。
|
|
29
|
+
|
|
30
|
+
灵克的答案是:**一个你可以完全掌控的 AI 编程助手。**
|
|
31
|
+
|
|
32
|
+
- 本地运行,数据不出你的机器
|
|
33
|
+
- 内置自优化框架,不是静态工具,会持续自我进化
|
|
34
|
+
- 开源,社区驱动,你可以改任何东西
|
|
35
|
+
|
|
36
|
+
## 灵克 vs Claude Code
|
|
37
|
+
|
|
38
|
+
| 能力 | Claude Code | 灵克 |
|
|
39
|
+
|------|------------|------|
|
|
40
|
+
| 代码理解与编辑 | ✅ | ✅ |
|
|
41
|
+
| 终端/Shell 操作 | ✅ | ✅ |
|
|
42
|
+
| 会话管理 | ✅ | ✅ |
|
|
43
|
+
| 权限控制 | ✅ | ✅ |
|
|
44
|
+
| 自我优化 | ❌ | ✅ 7类触发 + AST评估 |
|
|
45
|
+
| 自我学习 | ❌ | ✅ 6种模式检测 + 知识库 |
|
|
46
|
+
| 开源 | ❌ | ✅ MIT |
|
|
47
|
+
| 本地运行 | ❌ | ✅ 零云端依赖 |
|
|
48
|
+
|
|
49
|
+
## 快速开始
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# 安装
|
|
53
|
+
pip install -e .
|
|
54
|
+
|
|
55
|
+
# 设置 API Key(二选一)
|
|
56
|
+
export OPENAI_API_KEY="sk-..."
|
|
57
|
+
# 或
|
|
58
|
+
export ANTHROPIC_API_KEY="sk-ant-..."
|
|
59
|
+
|
|
60
|
+
# 单次提问
|
|
61
|
+
lingclaude run "你的编程问题"
|
|
62
|
+
|
|
63
|
+
# 交互模式
|
|
64
|
+
lingclaude run -i "开始对话"
|
|
65
|
+
|
|
66
|
+
# 查看用量
|
|
67
|
+
lingclaude run "hello" -v
|
|
68
|
+
|
|
69
|
+
# 其他命令
|
|
70
|
+
lingclaude analyze <项目路径>
|
|
71
|
+
lingclaude optimize -t <项目路径> -g "降低复杂度"
|
|
72
|
+
lingclaude session list
|
|
73
|
+
lingclaude knowledge stats
|
|
74
|
+
|
|
75
|
+
# HTTP API服务(需设置环境变量 LINGCLAUDE_API_KEYS)
|
|
76
|
+
export LINGCLAUDE_API_KEYS="your-api-key-1,your-api-key-2"
|
|
77
|
+
python3 -m lingclaude.api
|
|
78
|
+
|
|
79
|
+
# API端点(认证通过 X-API-Key 头)
|
|
80
|
+
curl -X POST http://localhost:8000/v1/submit \
|
|
81
|
+
-H "X-API-Key: your-api-key-1" \
|
|
82
|
+
-H "Content-Type: application/json" \
|
|
83
|
+
-d '{"query": "解释这段代码"}'
|
|
84
|
+
|
|
85
|
+
# 或不安装直接运行
|
|
86
|
+
python3 -m lingclaude.cli --help
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## 架构
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
灵克 (LingClaude)
|
|
93
|
+
├── core/ # 基础层
|
|
94
|
+
│ ├── types.py # Result[T] 单子(ok/fail 工厂方法)
|
|
95
|
+
│ ├── config.py # YAML 配置 → dataclass
|
|
96
|
+
│ ├── models.py # ToolDefinition, PermissionDenial, UsageSummary 等
|
|
97
|
+
│ ├── session.py # Session(不可变)+ SessionManager(JSON 持久化)
|
|
98
|
+
│ ├── permissions.py # PermissionContext(deny_tools + deny_prefixes)
|
|
99
|
+
│ ├── behavior.py # BehaviorMetrics(情绪/意图/幻觉检测 + 行为追踪)
|
|
100
|
+
│ ├── intel.py # IntelCollector + DailyDigest + IntelRelay(情报系统)
|
|
101
|
+
│ └── query_engine.py # QueryEngine:turn循环、流式输出、自动压缩、情报收集
|
|
102
|
+
│
|
|
103
|
+
├── model/ # 模型抽象层
|
|
104
|
+
│ ├── types.py # ModelMessage, ModelResponse, ModelProvider ABC
|
|
105
|
+
│ ├── openai_provider.py # OpenAI API 实现(sync + async)
|
|
106
|
+
│ ├── anthropic_provider.py # Anthropic API 实现(sync + async)
|
|
107
|
+
│ └── factory.py # create_provider() 工厂 + 自动检测
|
|
108
|
+
│
|
|
109
|
+
├── engine/ # 工具执行层
|
|
110
|
+
│ ├── tools.py # ToolRegistry(注册/注销/执行)
|
|
111
|
+
│ ├── bash.py # BashExecutor(黑名单 + 资源限制 + 超时)
|
|
112
|
+
│ ├── file_ops.py # FileOps(读写/编辑/搜索 + 路径包含检查)
|
|
113
|
+
│ ├── file_read.py # FileReadTool(文件读取工具 + 路径遍历防护)
|
|
114
|
+
│ ├── file_edit.py # FileEditTool(文件编辑工具 + 路径遍历防护)
|
|
115
|
+
│ ├── grep.py # GrepTool(代码搜索工具)
|
|
116
|
+
│ ├── stt.py # STTEngine(语音转文字引擎)
|
|
117
|
+
│ └── coding.py # CodingRuntime(工具 + 评估 + 优化 + 模式检测)
|
|
118
|
+
│
|
|
119
|
+
├── api/ # HTTP API层
|
|
120
|
+
│ └── api.py # FastAPI HTTP服务(认证、路径遍历防护、文件操作)
|
|
121
|
+
│
|
|
122
|
+
├── self_optimizer/ # 自优化框架
|
|
123
|
+
│ ├── trigger.py # OptimizationTrigger(8类触发条件)
|
|
124
|
+
│ ├── evaluator.py # StructureEvaluator(AST 分析)
|
|
125
|
+
│ ├── optimizer.py # SynchronousOptimizer + SimpleSearchSpace
|
|
126
|
+
│ ├── advisor.py # OptimizationAdvisor(Markdown 报告)
|
|
127
|
+
│ └── learner/ # Phase 5:自学习引擎
|
|
128
|
+
│ ├── models.py # FeedbackItem, Pattern, LearnedRule
|
|
129
|
+
│ ├── rule_extractor.py # 规则提取 + 去重 + 验证
|
|
130
|
+
│ ├── knowledge.py # SQLite 知识库 + 内存知识库
|
|
131
|
+
│ └── patterns.py # 6种检测器
|
|
132
|
+
│
|
|
133
|
+
└── cli/ # 命令行界面
|
|
134
|
+
└── app.py # 子命令:run, optimize, analyze, session, knowledge
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## 自优化流程
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
触发(8类条件)→ 评估(AST指标)→ 优化(optuna/网格搜索)→ 报告(Markdown)
|
|
141
|
+
↓
|
|
142
|
+
知识库(规则提取 + 模式识别)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 8类触发条件
|
|
146
|
+
|
|
147
|
+
用户请求 | 质量低于阈值 | 行为异常 | 结构违规 | 性能退化 | 规模增长 | 技术债累积 | 定时优化
|
|
148
|
+
|
|
149
|
+
### 6种模式检测器
|
|
150
|
+
|
|
151
|
+
长方法 | 未用变量 | 硬编码密钥 | 重复代码 | 空块 | 高复杂度
|
|
152
|
+
|
|
153
|
+
## 模型配置
|
|
154
|
+
|
|
155
|
+
灵克支持 OpenAI 和 Anthropic 两种模型提供商,通过 `config.yaml` 配置:
|
|
156
|
+
|
|
157
|
+
```yaml
|
|
158
|
+
model:
|
|
159
|
+
provider: openai # openai 或 anthropic
|
|
160
|
+
model: gpt-4o # 模型名称
|
|
161
|
+
api_key: "" # 建议用环境变量,不要明文写
|
|
162
|
+
base_url: null # 可选:自定义 API 端点
|
|
163
|
+
max_tokens: 4096
|
|
164
|
+
temperature: 0.7
|
|
165
|
+
system_prompt: "你是灵克,一个 AI 编程助手。"
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**优先级**: `config.yaml` 中的 `api_key` > 环境变量 `OPENAI_API_KEY` / `ANTHROPIC_API_KEY`
|
|
169
|
+
|
|
170
|
+
**自动检测**: 如果不指定 `provider`,会根据模型名称自动选择:
|
|
171
|
+
- `gpt-*`、`o1-*`、`o3-*`、`o4-*` → OpenAI
|
|
172
|
+
- `claude-*` → Anthropic
|
|
173
|
+
- 其他 → 默认 OpenAI
|
|
174
|
+
|
|
175
|
+
## 通用配置
|
|
176
|
+
|
|
177
|
+
编辑项目根目录 `config.yaml`:
|
|
178
|
+
|
|
179
|
+
```yaml
|
|
180
|
+
system:
|
|
181
|
+
name: 灵克
|
|
182
|
+
log_level: INFO
|
|
183
|
+
|
|
184
|
+
engine:
|
|
185
|
+
max_turns: 8
|
|
186
|
+
max_budget_tokens: 200000
|
|
187
|
+
compact_after_turns: 12
|
|
188
|
+
structured_output: false
|
|
189
|
+
|
|
190
|
+
permissions:
|
|
191
|
+
deny_tools: []
|
|
192
|
+
deny_prefixes: []
|
|
193
|
+
|
|
194
|
+
self_optimizer:
|
|
195
|
+
triggers:
|
|
196
|
+
quality:
|
|
197
|
+
enabled: true
|
|
198
|
+
review_score_threshold: 60
|
|
199
|
+
structure:
|
|
200
|
+
enabled: true
|
|
201
|
+
max_complexity: 15
|
|
202
|
+
max_method_lines: 50
|
|
203
|
+
optimization:
|
|
204
|
+
goal: structure
|
|
205
|
+
max_trials: 50
|
|
206
|
+
timeout_seconds: 120
|
|
207
|
+
|
|
208
|
+
session:
|
|
209
|
+
save_dir: .lingclaude/sessions/
|
|
210
|
+
max_history: 100
|
|
211
|
+
|
|
212
|
+
intel:
|
|
213
|
+
enabled: true
|
|
214
|
+
output_dir: .lingclaude/intel/
|
|
215
|
+
session_history_path: data/session_history.json
|
|
216
|
+
auto_collect_behavior: true
|
|
217
|
+
auto_relay: true
|
|
218
|
+
relay_target: lingyi
|
|
219
|
+
digest_hour: 23
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## 情报系统
|
|
223
|
+
|
|
224
|
+
灵克内置情报收集系统,每日自动汇总情报中继给灵依 (LingYi):
|
|
225
|
+
|
|
226
|
+
```
|
|
227
|
+
行为感知 → IntelCollector(8类情报)→ DailyDigestGenerator(日报)→ IntelRelay(文件输出)
|
|
228
|
+
↓
|
|
229
|
+
session_history.json(灵依消费)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### 8类情报类别
|
|
233
|
+
|
|
234
|
+
文件变更 | 代码模式 | 行为异常 | 错误情报 | 优化记录 | 结构情报 | 质量情报 | 安全情报
|
|
235
|
+
|
|
236
|
+
## HTTP API
|
|
237
|
+
|
|
238
|
+
灵克提供FastAPI HTTP服务,支持远程调用和集成:
|
|
239
|
+
|
|
240
|
+
### 启动服务
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
export LINGCLAUDE_API_KEYS="key1,key2,key3"
|
|
244
|
+
python3 -m lingclaude.api --host 0.0.0.0 --port 8000
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### API端点
|
|
248
|
+
|
|
249
|
+
| 端点 | 方法 | 说明 | 认证 |
|
|
250
|
+
|------|------|------|------|
|
|
251
|
+
| `/v1/submit` | POST | 提交查询 | `X-API-Key` 头 |
|
|
252
|
+
| `/v1/sessions` | GET | 列出会话 | `X-API-Key` 头 |
|
|
253
|
+
| `/v1/sessions/{id}` | GET | 获取会话详情 | `X-API-Key` 头 |
|
|
254
|
+
| `/v1/read-file` | POST | 读取文件 | `X-API-Key` 头 + 路径验证 |
|
|
255
|
+
| `/v1/write-file` | POST | 写入文件 | `X-API-Key` 头 + 路径验证 |
|
|
256
|
+
| `/health` | GET | 健康检查 | 无需认证 |
|
|
257
|
+
|
|
258
|
+
### 安全特性
|
|
259
|
+
|
|
260
|
+
- API Key认证(禁止空密钥)
|
|
261
|
+
- 路径遍历防护(禁止访问项目目录外文件)
|
|
262
|
+
- 文件操作白名单
|
|
263
|
+
- Session ID使用加密安全随机数生成器
|
|
264
|
+
|
|
265
|
+
详细API文档见:[docs/AUDIT_REPORT_2026-04-06.md](docs/AUDIT_REPORT_2026-04-06.md)
|
|
266
|
+
|
|
267
|
+
## LingXi 集成
|
|
268
|
+
|
|
269
|
+
灵克支持使用 LingXi(灵犀)MCP 服务器进行安全的终端命令执行。LingXi 是一个基于 MCP(Model Context Protocol)的终端执行服务,提供增强的安全性和监控能力。
|
|
270
|
+
|
|
271
|
+
### 什么是 LingXi?
|
|
272
|
+
|
|
273
|
+
LingXi 是一个 MCP 服务器,通过 stdio JSON-RPC 2.0 协议提供终端命令执行服务。它具有以下特性:
|
|
274
|
+
|
|
275
|
+
- **增强安全性**:使用 `execFile` 而非 `exec`,防止 shell 注入攻击
|
|
276
|
+
- **黑名单/白名单过滤**:支持自定义命令过滤规则
|
|
277
|
+
- **性能监控**:自动跟踪响应时间和错误率
|
|
278
|
+
- **会话管理**:支持多会话终端管理
|
|
279
|
+
|
|
280
|
+
### BashLingXiExecutor vs BashExecutor
|
|
281
|
+
|
|
282
|
+
| 特性 | BashExecutor | BashLingXiExecutor |
|
|
283
|
+
|------|--------------|-------------------|
|
|
284
|
+
| 执行方式 | subprocess.run (shell=True) | LingXi MCP (execFile) |
|
|
285
|
+
| 安全性 | 内置规则 | LingXi + 自定义规则 |
|
|
286
|
+
| Shell 注入风险 | 存在 | 无(execFile 保护) |
|
|
287
|
+
| 工作目录 | 支持 | 不支持 |
|
|
288
|
+
| 管道/操作符 | 支持 | 阻止(安全考虑) |
|
|
289
|
+
| 性能监控 | 基础(耗时) | 高级(性能跟踪) |
|
|
290
|
+
| 初始化开销 | 无 | ~350ms |
|
|
291
|
+
| 后续命令开销 | ~5-50ms | ~4-10ms |
|
|
292
|
+
|
|
293
|
+
### 配置
|
|
294
|
+
|
|
295
|
+
#### 方法 1:通过配置文件
|
|
296
|
+
|
|
297
|
+
编辑 `config.yaml`:
|
|
298
|
+
|
|
299
|
+
```yaml
|
|
300
|
+
engine:
|
|
301
|
+
bash_executor_type: lingxi # "native" 或 "lingxi"
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
#### 方法 2:通过命令行参数
|
|
305
|
+
|
|
306
|
+
```bash
|
|
307
|
+
# 使用 LingXi 执行器
|
|
308
|
+
lingclaude run "列出当前目录文件" --bash-executor lingxi
|
|
309
|
+
|
|
310
|
+
# 使用原生 bash 执行器(默认)
|
|
311
|
+
lingclaude run "列出当前目录文件" --bash-executor native
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### 工具对比
|
|
315
|
+
|
|
316
|
+
灵克提供两个 bash 工具:
|
|
317
|
+
|
|
318
|
+
1. **`bash`**:使用原生 subprocess 执行
|
|
319
|
+
- 支持所有 shell 特性(管道、重定向等)
|
|
320
|
+
- 适合需要复杂 shell 操作的场景
|
|
321
|
+
|
|
322
|
+
2. **`bash_lingxi`**:通过 LingXi MCP 服务器执行
|
|
323
|
+
- 增强的安全性(防注入)
|
|
324
|
+
- 内置性能监控
|
|
325
|
+
- 适合生产环境和安全敏感场景
|
|
326
|
+
|
|
327
|
+
### 使用示例
|
|
328
|
+
|
|
329
|
+
```python
|
|
330
|
+
from lingclaude.engine.bash_lingxi import BashLingXiExecutor
|
|
331
|
+
|
|
332
|
+
# 基本使用
|
|
333
|
+
executor = BashLingXiExecutor()
|
|
334
|
+
result = executor.run('echo "Hello from LingXi"')
|
|
335
|
+
print(result.stdout) # 输出: Hello from LingXi
|
|
336
|
+
|
|
337
|
+
# 带安全规则
|
|
338
|
+
executor = BashLingXiExecutor(
|
|
339
|
+
blocked_commands=['rm', 'sudo', 'curl'],
|
|
340
|
+
allowed_commands=['echo', 'ls', 'cat'] # 可选白名单
|
|
341
|
+
)
|
|
342
|
+
result = executor.run('echo "Allowed"')
|
|
343
|
+
print(result.stdout) # 输出: Allowed
|
|
344
|
+
|
|
345
|
+
result = executor.run('rm -rf test')
|
|
346
|
+
print(result.stderr) # 输出: 命令被阻止: rm -rf test(原因: 匹配黑名单规则 'rm')
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### 限制
|
|
350
|
+
|
|
351
|
+
1. **工作目录**:LingXi 的 `execute_command` 不支持 `cwd` 参数
|
|
352
|
+
2. **Shell 管道**:命令如 `ls | grep foo` 会被 LingXi 的安全规则阻止
|
|
353
|
+
3. **固定超时**:LingXi 使用内部 60 秒超时
|
|
354
|
+
4. **初始化开销**:首次命令执行需要 ~350ms(服务器启动 + 初始化)
|
|
355
|
+
|
|
356
|
+
### 测试
|
|
357
|
+
|
|
358
|
+
运行 LingXi 集成测试:
|
|
359
|
+
|
|
360
|
+
```bash
|
|
361
|
+
# 测试 MCP SDK 集成
|
|
362
|
+
python3 scripts/test_mcp_sdk.py
|
|
363
|
+
|
|
364
|
+
# 测试 BashLingXiExecutor
|
|
365
|
+
python3 scripts/test_bash_lingxi.py
|
|
366
|
+
|
|
367
|
+
# 运行使用示例
|
|
368
|
+
python3 scripts/example_bash_lingxi.py
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
详细文档见:[docs/lingxi-integration.md](docs/lingxi-integration.md)
|
|
372
|
+
|
|
373
|
+
## 依赖
|
|
374
|
+
|
|
375
|
+
**必须**: tiktoken, aiohttp, pyyaml, fastapi, uvicorn
|
|
376
|
+
|
|
377
|
+
**可选**: optuna(高级优化), psutil(系统指标), openai(语音识别)
|
|
378
|
+
|
|
379
|
+
## 开发
|
|
380
|
+
|
|
381
|
+
```bash
|
|
382
|
+
python3 -m pytest tests/ -v # 260 tests
|
|
383
|
+
python3 -c "from lingclaude.model import create_provider; print('OK')"
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
## 路线图
|
|
387
|
+
|
|
388
|
+
完整路线图见 [CHARTER.md](CHARTER.md#路线图)。
|
|
389
|
+
|
|
390
|
+
- [x] v0.1.0 — 核心框架:查询引擎、会话、权限、工具执行、自优化、自学习
|
|
391
|
+
- [x] v0.1.1 — **安全审计**:bash 沙箱加固、文件操作路径包含检查、敏感路径保护
|
|
392
|
+
- [x] v0.1.2 — **开源准备**:贡献指南、Issue/PR 模板
|
|
393
|
+
- [x] v0.2.0 — **模型对接 + 行为感知 + 自适应引擎**:OpenAI/Anthropic API、行为感知系统、自适应查询引擎、Agent Loop
|
|
394
|
+
- [x] v0.2.1 — **情报系统 + HTTP API + 安全审计**:情报收集、日报生成、灵依中继、会话历史输出;FastAPI HTTP服务;安全漏洞修复(认证绕过、路径遍历、会话管理)
|
|
395
|
+
- [ ] v1.0.0 — 完整的 AI 编程助手
|
|
396
|
+
|
|
397
|
+
## License
|
|
398
|
+
|
|
399
|
+
MIT
|