xiaozhou-chat 1.0.7 → 1.0.9

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 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
- - **🎨 视觉优化**:支持 Markdown 渲染 (终端内显示粗体/颜色),流式输出平滑打字机效果。
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
- ## 📦 安装 (Installation)
24
+ ## 📦 安装与使用 (Installation & Usage)
23
25
 
24
- ### 全局安装
25
- ```bash
26
- npm i -g xiaozhou-chat
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` | `/初始化配置` | 在当前目录生成 `newapi-chat.config.json` 配置文件 |
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. **项目级配置**:`./newapi-chat.config.json` (推荐使用 `/init` 生成)
90
+ 1. **项目级配置**:`./.newapi-chat-config.json` (推荐,由 `init` 生成)
85
91
  2. **用户级配置**:`~/.newapi-chat-config.json`
86
92
 
87
- **配置文件示例 (`newapi-chat.config.json`):**
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
- ## 🛠️ 打包与构建 (Build)
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/bin/cli.js CHANGED
@@ -6,6 +6,7 @@ import minimist from "minimist";
6
6
  import { MCPClient } from "../lib/mcp-lite.js";
7
7
  import {
8
8
  initConfigFile,
9
+ initProjectConfigFile,
9
10
  loadConfig,
10
11
  updateConfig,
11
12
  getActiveConfig,
@@ -27,13 +28,19 @@ import {
27
28
  } from "../lib/tools.js";
28
29
  import { estimateTokens } from "../lib/utils.js";
29
30
 
31
+ const args = minimist(process.argv.slice(2));
32
+
33
+ // 处理 init 命令:直接在当前目录生成配置文件并退出
34
+ if (args._.includes("init")) {
35
+ initProjectConfigFile();
36
+ process.exit(0);
37
+ }
38
+
30
39
  // --- 初始化 ---
31
40
  initConfigFile();
32
41
  let config = loadConfig();
33
42
  let activeConfig = getActiveConfig(config);
34
43
 
35
- const args = minimist(process.argv.slice(2));
36
-
37
44
  // 覆盖配置 (命令行参数 > 配置文件)
38
45
  if (args["api-key"]) activeConfig.apiKey = args["api-key"];
39
46
  if (args["base-url"]) activeConfig.baseUrl = args["base-url"];
package/lib/chat.js CHANGED
@@ -1,6 +1,7 @@
1
1
 
2
2
  import { Spinner, StreamPrinter } from "./ui.js";
3
3
  import { sleep } from "./utils.js";
4
+ import { updateConfig, setProfileValue } from "./config.js";
4
5
 
5
6
  // 尝试加载 Markdown 渲染库
6
7
  let marked;
@@ -118,8 +119,7 @@ export async function chatStream(context, userInput = null, options = {}) {
118
119
  const contentType = res.headers.get("content-type");
119
120
  if (contentType && contentType.includes("text/html")) {
120
121
  if (!config.baseUrl.endsWith("/v1") && !config.baseUrl.endsWith("/v1/")) {
121
- console.log("\n⚠️ 检测到 API 返回 HTML,Base URL 可能缺少 '/v1'。");
122
- console.log("🔄 正在尝试自动追加 '/v1' 重试...");
122
+ // 静默重试,不打扰用户
123
123
  requestUrl = `${config.baseUrl}/v1/chat/completions`;
124
124
  shouldRetryWithV1 = true;
125
125
 
@@ -140,8 +140,23 @@ export async function chatStream(context, userInput = null, options = {}) {
140
140
  spinner.stop();
141
141
 
142
142
  if (shouldRetryWithV1 && res.ok && !res.headers.get("content-type")?.includes("text/html")) {
143
- console.log(`✅ 自动修复成功!建议运行以下命令永久修改配置:`);
144
- console.log(`👉 /config baseUrl ${config.baseUrl}/v1`);
143
+ // 静默保存配置
144
+ try {
145
+ const correctBaseUrl = config.baseUrl.endsWith("/")
146
+ ? config.baseUrl + "v1"
147
+ : config.baseUrl + "/v1";
148
+
149
+ // 1. 更新内存配置
150
+ config.baseUrl = correctBaseUrl;
151
+
152
+ // 2. 永久保存配置 (同时更新顶层和当前 Profile)
153
+ updateConfig("baseUrl", correctBaseUrl);
154
+ if (config.currentProfile) {
155
+ setProfileValue(config.currentProfile, "baseUrl", correctBaseUrl);
156
+ }
157
+ } catch (e) {
158
+ // ignore save error
159
+ }
145
160
  }
146
161
 
147
162
  if (!res.body) throw new Error("Response body is empty");
package/lib/config.js CHANGED
@@ -45,6 +45,37 @@ export function initConfigFile() {
45
45
  console.log("⚠️ 请务必编辑该文件,填入你的 apiKey (sk-...),否则无法使用。");
46
46
  }
47
47
 
48
+ // 在当前目录初始化项目级配置文件
49
+ export function initProjectConfigFile() {
50
+ const targetFile = path.join(process.cwd(), ".newapi-chat-config.json");
51
+
52
+ if (fs.existsSync(targetFile)) {
53
+ console.log(`⚠️ 配置文件已存在: ${targetFile}`);
54
+ return;
55
+ }
56
+
57
+ const defaultConfig = {
58
+ apiKey: "", // 用户需手动填入
59
+ baseUrl: "https://paid.tribiosapi.top/v1",
60
+ model: "claude-sonnet-4-5-20250929",
61
+ profiles: {
62
+ default: {
63
+ apiKey: "",
64
+ baseUrl: "https://paid.tribiosapi.top/v1",
65
+ model: "claude-sonnet-4-5-20250929"
66
+ }
67
+ },
68
+ currentProfile: "default"
69
+ };
70
+
71
+ fs.writeFileSync(targetFile, JSON.stringify(defaultConfig, null, 2), "utf-8");
72
+ console.log(`✅ 已在当前目录创建配置文件: ${targetFile}`);
73
+ console.log("📝 请编辑此文件填入你的 API Key。");
74
+
75
+ // 提示用户添加到 .gitignore
76
+ console.log("🔒 建议将 .newapi-chat-config.json 添加到 .gitignore 以防泄露。");
77
+ }
78
+
48
79
  function loadConfigFrom(file) {
49
80
  if (!fs.existsSync(file)) return {};
50
81
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xiaozhou-chat",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "CLI chatbot based on NewAPI",
5
5
  "bin": {
6
6
  "xiaozhou-chat": "bin/cli.js"