xiaozhou-chat 1.0.8 → 1.0.10
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.
- package/README.md +36 -29
- package/lib/chat.js +35 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,25 +10,42 @@ CLI chatbot based on NewAPI with advanced features like Project Context, Multi-P
|
|
|
10
10
|
- **🤖 多模型/多环境管理**:支持通过 `/profile` (或 `/切换模型`) 快速切换不同的 API Key 和模型配置。
|
|
11
11
|
- **📂 项目感知**:通过 `/scan` 自动扫描项目结构、读取 `package.json` 和 `README.md` (智能大文件摘要),让 AI 理解你的项目。
|
|
12
12
|
- **🛠️ AI 编程助手**:内置文件读写 (`read_file`, `write_file`) 和命令执行 (`run_command`) 工具,AI 可以帮你重构代码、运行测试。
|
|
13
|
-
- **🛡️ 安全机制**:`write_file` 覆盖文件前自动创建 `.bak`
|
|
13
|
+
- **🛡️ 安全机制**:`write_file` 覆盖文件前自动创建 `.bak` 备份;敏感文件自动排除;网络请求失败自动指数退避重试。
|
|
14
14
|
- **🚀 效率工具**:`/commit` 自动分析代码变更并生成 Git Commit Message;`/token` 实时监控上下文消耗。
|
|
15
|
-
- **🎨
|
|
15
|
+
- **🎨 交互优化**:
|
|
16
|
+
- **Ctrl+C 中断**:生成过程中按 `Ctrl+C` 仅中断当前输出,不退出程序。
|
|
17
|
+
- **Markdown 渲染**:终端内完美渲染 Markdown(代码高亮、表格、粗体等)。
|
|
18
|
+
- **Esc 快捷键**:按 `Esc` 清除当前输入,空行时按 `Esc` 退出程序。
|
|
16
19
|
- **⚙️ 灵活配置**:支持全局配置及项目级配置文件 (`.newapi-chat-config.json`),可随项目隔离环境。
|
|
17
20
|
- **🔌 MCP 支持**:集成 Model Context Protocol,可连接外部 MCP Server 扩展能力。
|
|
18
|
-
- **📦 跨平台**:支持 macOS (DMG/Binary) 和 Windows 打包。
|
|
19
21
|
|
|
20
22
|
---
|
|
21
23
|
|
|
22
|
-
## 📦
|
|
24
|
+
## 📦 安装与使用 (Installation & Usage)
|
|
23
25
|
|
|
24
|
-
###
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
### 方式一:在当前项目中使用 (推荐)
|
|
27
|
+
无需全局安装,直接在你的项目中添加并初始化配置:
|
|
28
|
+
|
|
29
|
+
1. **安装依赖**
|
|
30
|
+
```bash
|
|
31
|
+
npm install xiaozhou-chat
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
2. **初始化配置** (自动在当前目录生成配置文件)
|
|
35
|
+
```bash
|
|
36
|
+
npx xiaozhou-chat init
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
3. **启动聊天**
|
|
40
|
+
```bash
|
|
41
|
+
npx xiaozhou-chat
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 方式二:全局安装
|
|
45
|
+
如果你希望在任何目录下都能使用:
|
|
28
46
|
|
|
29
|
-
### 快速开始
|
|
30
|
-
在任意终端输入:
|
|
31
47
|
```bash
|
|
48
|
+
npm i -g xiaozhou-chat
|
|
32
49
|
xiaozhou-chat
|
|
33
50
|
```
|
|
34
51
|
|
|
@@ -41,7 +58,7 @@ xiaozhou-chat
|
|
|
41
58
|
### 🔧 配置管理
|
|
42
59
|
| 命令 | 中文别名 | 说明 |
|
|
43
60
|
|---|---|---|
|
|
44
|
-
| `/init` | `/初始化配置` | 在当前目录生成
|
|
61
|
+
| `/init` | `/初始化配置` | (交互式) 在当前目录生成 `.newapi-chat-config.json` |
|
|
45
62
|
| `/config` | `/配置` | 查看或修改当前配置 (如 `/config set model gpt-4`) |
|
|
46
63
|
| `/profile` | `/切换模型` | 管理多套配置 (详情见下文) |
|
|
47
64
|
| `/system` | - | 设置系统提示词 (System Prompt) |
|
|
@@ -50,7 +67,7 @@ xiaozhou-chat
|
|
|
50
67
|
| 命令 | 中文别名 | 说明 |
|
|
51
68
|
|---|---|---|
|
|
52
69
|
| `/scan` | `/当前项目结构` | 扫描当前目录结构并加载关键文件到上下文 |
|
|
53
|
-
| `/load <file>` | - | 加载指定文件内容给 AI |
|
|
70
|
+
| `/load <file>` | - | 加载指定文件内容给 AI (支持大文件行号选择) |
|
|
54
71
|
| `/save <file>` | - | 将 AI 回复中的代码块保存为本地文件 |
|
|
55
72
|
| `/paste` | - | 进入多行粘贴模式 (避免回车直接发送) |
|
|
56
73
|
|
|
@@ -58,7 +75,7 @@ xiaozhou-chat
|
|
|
58
75
|
| 命令 | 说明 |
|
|
59
76
|
|---|---|
|
|
60
77
|
| `/commit` | 自动执行 `git add -A` 并利用 AI 生成提交信息 |
|
|
61
|
-
| `/token` | 查看当前会话 Token 估算与预警
|
|
78
|
+
| `/token` | 查看当前会话 Token 估算与预警 |
|
|
62
79
|
| `/compress` | 智能压缩对话历史,节省 Token |
|
|
63
80
|
| `/mcp` | 管理 MCP 服务器连接 (如连接外部数据库工具) |
|
|
64
81
|
| `/clear` | 清空当前对话历史 (保留 System Prompt) |
|
|
@@ -67,24 +84,13 @@ xiaozhou-chat
|
|
|
67
84
|
|
|
68
85
|
---
|
|
69
86
|
|
|
70
|
-
## 🤖 多环境配置 (Profiles)
|
|
71
|
-
|
|
72
|
-
支持保存多套配置(例如:开发环境用 `gpt-3.5`,生产环境用 `gpt-4`,或者区分不同的 API Key)。
|
|
73
|
-
|
|
74
|
-
**常用操作:**
|
|
75
|
-
1. **查看列表**:`/profile list` 或 `/切换模型`
|
|
76
|
-
2. **创建配置**:`/profile add gpt4` (复制当前配置为 gpt4)
|
|
77
|
-
3. **切换配置**:`/profile use gpt4` 或 `/切换模型 gpt4`
|
|
78
|
-
|
|
79
|
-
---
|
|
80
|
-
|
|
81
87
|
## ⚙️ 配置文件 (Configuration)
|
|
82
88
|
|
|
83
89
|
工具会按以下优先级读取配置:
|
|
84
|
-
1.
|
|
90
|
+
1. **项目级配置**:`./.newapi-chat-config.json` (推荐,由 `init` 生成)
|
|
85
91
|
2. **用户级配置**:`~/.newapi-chat-config.json`
|
|
86
92
|
|
|
87
|
-
**配置文件示例 (
|
|
93
|
+
**配置文件示例 (`.newapi-chat-config.json`):**
|
|
88
94
|
```json
|
|
89
95
|
{
|
|
90
96
|
"apiKey": "sk-xxxx",
|
|
@@ -97,19 +103,20 @@ xiaozhou-chat
|
|
|
97
103
|
}
|
|
98
104
|
```
|
|
99
105
|
|
|
106
|
+
> **注意**:建议将 `.newapi-chat-config.json` 添加到 `.gitignore` 中,防止 API Key 泄露。
|
|
107
|
+
|
|
100
108
|
---
|
|
101
109
|
|
|
102
|
-
## 🛠️
|
|
110
|
+
## 🛠️ 本地开发 (Development)
|
|
103
111
|
|
|
104
112
|
### 依赖安装
|
|
105
113
|
```bash
|
|
106
114
|
npm install
|
|
107
115
|
```
|
|
108
116
|
|
|
109
|
-
###
|
|
117
|
+
### 构建与打包
|
|
110
118
|
- **Windows**: `npm run build:win` -> `dist/newapi-chat.exe`
|
|
111
119
|
- **macOS**: `npm run build:mac` -> `dist/xiaozhou-chat`
|
|
112
|
-
- **macOS DMG**: `npm run pack:dmg` -> `release/xiaozhou-chat_1.0.0_x64.dmg`
|
|
113
120
|
|
|
114
121
|
---
|
|
115
122
|
|
package/lib/chat.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { Spinner, StreamPrinter } from "./ui.js";
|
|
3
3
|
import { sleep } from "./utils.js";
|
|
4
4
|
import { updateConfig, setProfileValue } from "./config.js";
|
|
5
|
+
import { builtInTools } from "./tools.js";
|
|
5
6
|
|
|
6
7
|
// 尝试加载 Markdown 渲染库
|
|
7
8
|
let marked;
|
|
@@ -272,14 +273,46 @@ export async function chatStream(context, userInput = null, options = {}) {
|
|
|
272
273
|
|
|
273
274
|
// Handle Tool Calls
|
|
274
275
|
if (toolCalls.length > 0) {
|
|
276
|
+
// 提取已知工具名
|
|
277
|
+
const knownToolNames = builtInTools.map(t => t.function.name);
|
|
278
|
+
|
|
275
279
|
for (const tc of toolCalls) {
|
|
276
|
-
|
|
280
|
+
let funcName = tc.function.name;
|
|
281
|
+
|
|
282
|
+
// 自动修正工具名粘连 (例如: read_fileread_file -> read_file)
|
|
283
|
+
if (!knownToolNames.includes(funcName)) {
|
|
284
|
+
// 按长度降序排序,优先匹配更长的工具名
|
|
285
|
+
const matched = knownToolNames
|
|
286
|
+
.sort((a, b) => b.length - a.length)
|
|
287
|
+
.find(name => funcName.includes(name));
|
|
288
|
+
|
|
289
|
+
if (matched) {
|
|
290
|
+
console.log(`⚠️ 检测到工具名异常 "${funcName}",自动修正为 "${matched}"`);
|
|
291
|
+
funcName = matched;
|
|
292
|
+
tc.function.name = matched; // 修正原始对象,这对后续消息历史至关重要
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
277
296
|
const argsStr = tc.function.arguments;
|
|
278
297
|
console.log(`🛠️ 调用工具: ${funcName}(${argsStr})`);
|
|
279
298
|
|
|
280
299
|
let result = null;
|
|
281
300
|
try {
|
|
282
|
-
|
|
301
|
+
let args;
|
|
302
|
+
try {
|
|
303
|
+
args = JSON.parse(argsStr);
|
|
304
|
+
} catch (e) {
|
|
305
|
+
// 尝试修复常见的 JSON 粘连问题 (例如: {"a":1}{"b":2})
|
|
306
|
+
if (argsStr.includes("}{")) {
|
|
307
|
+
console.log("⚠️ 检测到 JSON 粘连,尝试修复...");
|
|
308
|
+
// 简单策略:只取第一个 JSON
|
|
309
|
+
const fixStr = argsStr.split("}{")[0] + "}";
|
|
310
|
+
args = JSON.parse(fixStr);
|
|
311
|
+
} else {
|
|
312
|
+
throw e;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
283
316
|
// 1. Try built-in
|
|
284
317
|
result = await toolHandlers(funcName, args);
|
|
285
318
|
|