kie-ai-cli 1.0.0

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.en.md ADDED
@@ -0,0 +1,36 @@
1
+ # kie-cli
2
+
3
+ #### Description
4
+ {**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**}
5
+
6
+ #### Software Architecture
7
+ Software architecture description
8
+
9
+ #### Installation
10
+
11
+ 1. xxxx
12
+ 2. xxxx
13
+ 3. xxxx
14
+
15
+ #### Instructions
16
+
17
+ 1. xxxx
18
+ 2. xxxx
19
+ 3. xxxx
20
+
21
+ #### Contribution
22
+
23
+ 1. Fork the repository
24
+ 2. Create Feat_xxx branch
25
+ 3. Commit your code
26
+ 4. Create Pull Request
27
+
28
+
29
+ #### Gitee Feature
30
+
31
+ 1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
32
+ 2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
33
+ 3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
34
+ 4. The most valuable open source project [GVP](https://gitee.com/gvp)
35
+ 5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
36
+ 6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
package/README.md ADDED
@@ -0,0 +1,503 @@
1
+ # kie-cli
2
+
3
+ 在终端里调用 Kie AI 的命令行工具,支持流式对话、管道输入、会话保存、历史压缩和图片生成。
4
+
5
+ ## 环境要求
6
+
7
+ - Node.js **18** 或更高版本
8
+
9
+ ## 安装
10
+
11
+ ### 本地开发
12
+
13
+ ```bash
14
+ git clone <你的仓库地址>
15
+ cd kie-cli
16
+ npm install
17
+ npm run build
18
+ ```
19
+
20
+ 之后用下面两种方式之一运行:
21
+
22
+ ```bash
23
+ node dist/index.js <命令>
24
+ # 或
25
+ npm start -- <命令>
26
+ ```
27
+
28
+ ### 全局安装(推荐)
29
+
30
+ ```bash
31
+ npm install -g kie-cli
32
+ # 或作用域包:npm install -g @你的用户名/kie-cli
33
+ ```
34
+
35
+ 安装后可直接使用 `kie` 命令。
36
+
37
+ ---
38
+
39
+ ## 第一次使用
40
+
41
+ ### 1. 配置 API Key(必做)
42
+
43
+ ```bash
44
+ kie config --set-api-key <你的API_KEY>
45
+ ```
46
+
47
+ ### 2. (可选)修改 API 地址
48
+
49
+ 默认地址为 `https://api.kie.ai`,一般不用改。若使用自建或测试环境:
50
+
51
+ ```bash
52
+ kie config --set-base-url https://api.kie.ai
53
+ ```
54
+
55
+ ### 3. 查看当前配置
56
+
57
+ ```bash
58
+ kie config
59
+ ```
60
+
61
+ ### 4. 开始对话
62
+
63
+ ```bash
64
+ kie chat
65
+ ```
66
+
67
+ ---
68
+
69
+ ## 命令总览
70
+
71
+ | 命令 | 作用 |
72
+ |------|------|
73
+ | `kie config` | 查看或设置 API Key、Base URL、默认模型 |
74
+ | `kie models` | 列出可用的 Claude 对话模型 |
75
+ | `kie chat` | 与 AI 对话(交互 / 单次 / 管道) |
76
+ | `kie code` | 代码模式(输出 MVP 实现方案、改动清单、验证步骤) |
77
+ | `kie image <提示词>` | 根据文字生成图片 |
78
+ | `kie --help` | 查看帮助 |
79
+ | `kie --version` | 查看版本 |
80
+
81
+ ---
82
+
83
+ ## `kie config` — 配置管理
84
+
85
+ 保存到本机用户目录,**不会**写进代码仓库,也不会随 npm 包发布。
86
+
87
+ ```bash
88
+ # 设置 API Key
89
+ kie config --set-api-key sk-xxxxxxxx
90
+
91
+ # 设置 API 根地址(可选)
92
+ kie config --set-base-url https://api.kie.ai
93
+
94
+ # 设置默认对话模型(Claude @ Kie)
95
+ kie config --set-model claude-opus-4-7
96
+
97
+ # 设置默认图片模型
98
+ kie config --set-image-model gpt-image-2-text-to-image
99
+
100
+ # 查看当前配置
101
+ kie config
102
+ ```
103
+
104
+ ---
105
+
106
+ ## 模型切换
107
+
108
+ 切换模型时会**自动选择接口**:
109
+
110
+ | 系列 | 端点 | 示例模型 |
111
+ |------|------|----------|
112
+ | **Claude** | `POST /claude/v1/messages` | `claude-opus-4-7`、`claude-sonnet-4-6` |
113
+ | **GPT** | `POST /{model}/v1/chat/completions` | `gpt-5-2` |
114
+ | **Responses** | `POST /codex/v1/responses` | `gpt-5-4`、`gpt-5-5` |
115
+ | **Responses** | `POST /api/v1/responses` | `gpt-5.1-codex` |
116
+
117
+ 自定义模型 ID:`gpt-5-2` → Chat Completions;`gpt-5-*` → `/codex/v1/responses`;名称含 `codex` → `/api/v1/responses`;默认 Claude。
118
+
119
+ ### 查看 / 选择对话模型 (Chat)
120
+
121
+ 这些模型只用于 **`kie chat` 对话**,和图片模型 `kie image` 无关。
122
+
123
+ | 场景 | 选择方式 |
124
+ |------|----------|
125
+ | **对话里** `kie chat` 后输入 `/model` | **输入序号**(1、2、3…)或模型 ID,避免 Windows 终端与 readline 冲突 |
126
+ | **对话外** 单独运行 `kie models` | **↑↓ + Enter** 交互选择 |
127
+
128
+ ```bash
129
+ kie models # 独立运行:↑↓ 选择 Chat 模型
130
+ kie models --set # 选择并保存为默认对话模型
131
+ kie models --list # 仅打印列表
132
+ ```
133
+
134
+ 内置推荐:
135
+
136
+ | 模型 ID | 说明 |
137
+ |---------|------|
138
+ | `claude-opus-4-7` | 旗舰,默认 |
139
+ | `claude-opus-4-6` | 上一代旗舰 |
140
+ | `claude-sonnet-4-6` | 速度与质量均衡 |
141
+ | `claude-sonnet-4-5` | Sonnet 4.5 |
142
+ | `claude-haiku-4-5` | 更快、更省 |
143
+ | `gpt-5-2` | GPT 5.2(Chat Completions) |
144
+ | `gpt-5-4` | GPT 5.4(Codex Responses) |
145
+ | `gpt-5-5` | GPT 5.5(/codex/v1/responses) |
146
+ | `gpt-5.1-codex` | GPT 5.1 Codex(/api/v1/responses) |
147
+
148
+ 也可设置 Kie 支持的**任意** model id:
149
+
150
+ ```bash
151
+ kie config --set-model <任意模型ID>
152
+ ```
153
+
154
+ ### 三种切换方式
155
+
156
+ ```bash
157
+ # 1. 全局默认(写入配置,以后 chat 都用它)
158
+ kie config --set-model claude-sonnet-4-6
159
+
160
+ # 2. 仅本次 chat 使用(不改全局配置)
161
+ kie chat --model claude-haiku-4-5
162
+ kie chat -m claude-opus-4-7 "你好"
163
+
164
+ # 3. 对话模式中切换 Chat 模型(会同时更新全局默认)
165
+ kie chat
166
+ (You) > /model # 显示序号列表,输入 1、2、3… 选择
167
+ (You) > /model claude-sonnet-4-6 # 也可直接输入模型 ID
168
+
169
+ # 若想要 ↑↓ 选择,请先退出对话,运行:
170
+ kie models --set
171
+ kie chat
172
+ ```
173
+
174
+ 优先级:**`kie chat --model`** > **交互里 `/model` 设置的会话模型** > **`kie config --set-model` 全局默认** > `claude-opus-4-7`。
175
+
176
+ > **说明:** 本 CLI 专注终端对话、会话持久化与历史压缩。多模态图片、`tools`、`reasoning` 等高级参数请在业务侧直接调 API。
177
+
178
+ ```bash
179
+ kie config --set-model gpt-5-2 # Chat Completions
180
+ kie config --set-model gpt-5.1-codex # /api/v1/responses
181
+ kie config --set-model gpt-5-5 # /codex/v1/responses
182
+ kie chat "你好"
183
+ ```
184
+
185
+ ---
186
+
187
+ ## `kie chat` — AI 对话
188
+
189
+ ### 三种使用方式
190
+
191
+ #### ① 交互模式(多轮连续对话)
192
+
193
+ 不传任何参数,进入 REPL,可一直聊,AI 会记住本轮会话里的上下文:
194
+
195
+ ```bash
196
+ kie chat
197
+ ```
198
+
199
+ 启动后会显示当前**会话 ID**,并自动恢复上次对话(若有)。
200
+
201
+ 输入 `exit` 或 `quit` 退出,退出时会自动保存会话。
202
+
203
+ #### ② 单次提问
204
+
205
+ 问一句,答一句,结束后退出,**不进入**交互模式:
206
+
207
+ ```bash
208
+ kie chat 帮我解释一下什么是 REST API
209
+ ```
210
+
211
+ #### ③ 管道输入
212
+
213
+ 把文件或命令输出「喂」给 AI:
214
+
215
+ ```bash
216
+ # 总结文件内容
217
+ type README.md | kie chat 请用三句话总结
218
+
219
+ # Linux / macOS
220
+ cat error.log | kie chat 分析这个报错
221
+
222
+ # 管道内容 + 额外说明
223
+ type app.ts | kie chat 找出可能的 bug
224
+ ```
225
+
226
+ ---
227
+
228
+ ### `kie chat` 启动参数
229
+
230
+ | 参数 | 说明 |
231
+ |------|------|
232
+ | `--new` | 开启**全新会话**,不加载历史 |
233
+ | `--resume` | 恢复**上一次**使用的会话 |
234
+ | `--resume <id>` | 恢复**指定 ID** 的会话 |
235
+ | `-m, --model <id>` | 指定本次对话使用的模型 |
236
+
237
+ 示例:
238
+
239
+ ```bash
240
+ # 默认:恢复上次会话(会话 ID 一般为 default 或 session-时间戳)
241
+ kie chat
242
+
243
+ # 新开会话,适合聊新话题
244
+ kie chat --new
245
+
246
+ # 恢复名为 my-project 的会话
247
+ kie chat --resume my-project
248
+ ```
249
+
250
+ ---
251
+
252
+ ### 交互模式内的斜杠指令
253
+
254
+ 仅在 `kie chat` 交互模式下可用(以 `/` 开头):
255
+
256
+ | 指令 | 作用 |
257
+ |------|------|
258
+ | `/model` | 显示序号选模型;`/model <id>` 直接切换 |
259
+ | `/clear` | **清空**当前会话的所有上下文(内存 + 磁盘) |
260
+ | `/compress` | **手动压缩**对话历史,把较早内容收成摘要 |
261
+ | `/file <路径>` | 读取本地文件内容,加入对话上下文 |
262
+ | `/run <命令>` | 在本机执行 shell 命令,把输出加入上下文 |
263
+ | `exit` / `quit` | 退出并保存会话 |
264
+
265
+ #### `/clear` — 清空会话
266
+
267
+ 忘记之前聊了什么、想重新开话题时使用:
268
+
269
+ ```
270
+ (You) > /clear
271
+ ✅ 当前会话上下文已清空。
272
+ ```
273
+
274
+ #### `/compress` — 手动压缩
275
+
276
+ 对话太长、想减少占用或避免超出模型上下文时使用。会把**较早的对话**交给 AI 写成摘要,**保留最近几轮完整原文**。
277
+
278
+ - 不足 2 轮对话时会提示「无需压缩」
279
+ - 压缩会额外调用一次 API
280
+
281
+ ```
282
+ (You) > /compress
283
+ ✔ 压缩完成:18 → 7 条消息,24500 → 4200 字符
284
+ ```
285
+
286
+ #### `/file` — 读入文件
287
+
288
+ 让 AI 能「看到」某个文件的内容(例如代码、日志):
289
+
290
+ ```
291
+ (You) > /file ./src/index.ts
292
+ ✅ 已读取文件 ./src/index.ts 并加入记忆上下文。你可以针对它进行提问了。
293
+
294
+ (You) > 这段代码里有没有明显的性能问题?
295
+ ```
296
+
297
+ Windows 下拖拽文件到终端时可能带引号,工具会自动去掉。
298
+
299
+ #### `/run` — 执行命令
300
+
301
+ 在本地跑一条命令,把标准输出交给 AI 分析:
302
+
303
+ ```
304
+ (You) > /run git status
305
+ ✅ 命令执行成功,输出结果已加入记忆上下文。
306
+
307
+ (You) > 根据这个 git 状态,我接下来该做什么?
308
+ ```
309
+
310
+ 命令失败时,错误信息也会写入上下文,你可以问 AI 为什么报错。
311
+
312
+ ---
313
+
314
+ ### 会话保存与自动压缩
315
+
316
+ #### 会话保存在哪?
317
+
318
+ 与配置文件同目录下的 `sessions/` 文件夹,例如:
319
+
320
+ - Windows:`%APPDATA%\kie-cli\Config\sessions\default.json`
321
+ - macOS / Linux:`~/.config/kie-cli/sessions/default.json`
322
+
323
+ 每个会话一个 JSON 文件,包含完整消息列表。
324
+
325
+ #### 自动恢复
326
+
327
+ 再次运行 `kie chat` 时,默认加载**上次活跃的会话**。
328
+
329
+ #### 自动压缩
330
+
331
+ 当会话总字符数超过约 **16000** 时,每轮对话结束后会**自动压缩**:
332
+
333
+ 1. 保留最近约 **6 轮**完整对话
334
+ 2. 把更早的内容发给 AI 生成摘要
335
+ 3. 摘要作为一条 `system` 消息,与最近几轮一起继续对话
336
+
337
+ 也可随时用 `/compress` 手动触发。
338
+
339
+ ---
340
+
341
+ ## `kie code` — 代码模式(MVP)
342
+
343
+ `kie code` 专注于“写代码”场景,默认使用**当前命令执行目录**作为代码工作目录(可用 `--cwd` 覆盖),并且**默认就是 agent 自动执行模式**。
344
+
345
+ - 传入任务:单次返回代码实现方案
346
+ - 不传任务:进入交互对话框(类似 `kie chat`)
347
+ - 可初始化工作区:生成 `.kie/code.json`,并刷新 `.kie/workspace.json`(项目画像)
348
+
349
+ ```bash
350
+ # 直接描述任务(默认 cwd = 当前目录)
351
+ kie code "给 image 命令增加 --dry-run 参数,并更新 README"
352
+
353
+ # 初始化 code 工作区配置 + 工作区画像
354
+ kie code --init
355
+
356
+ # 附带关键文件上下文(可重复 -f)
357
+ kie code "重构模型路由逻辑" -f src/index.ts -f src/models.ts
358
+
359
+ # 管道输入任务描述
360
+ type task.txt | kie code
361
+
362
+ # 默认就是 agent 自动执行(建议先配合 -y)
363
+ kie code -y "修复 lint 报错并跑测试"
364
+
365
+ # 如需明确写法,也可保留 --agent(兼容)
366
+ kie code --agent -y "修复 lint 报错并跑测试"
367
+
368
+ # 如果你只想对话,不自动执行命令
369
+ kie code --chat "先只给方案,不执行命令"
370
+
371
+ # 进入交互 code 对话框
372
+ kie code
373
+ ```
374
+
375
+ ### `kie code` 参数
376
+
377
+ | 参数 | 说明 |
378
+ |------|------|
379
+ | `-m, --model <id>` | 指定本次 code 使用的模型 |
380
+ | `--cwd <dir>` | 指定 code 工作目录(默认当前目录) |
381
+ | `--init` | 初始化当前工作目录,生成 `.kie/code.json` |
382
+ | `--run <cmd>` | 先执行命令并注入上下文(可重复) |
383
+ | `--agent` | 开启自动执行代理循环(默认已开启,兼容保留) |
384
+ | `--chat` | 切换为纯对话模式(关闭 agent 自动执行) |
385
+ | `--route <mode>` | 路由策略:`auto` / `quality` / `balanced` / `fast` |
386
+ | `--max-steps <n>` | agent 最大执行轮数(默认 5) |
387
+ | `-y, --yes` | agent 模式自动确认执行命令 |
388
+ | `--session <id>` | 使用指定 code 会话 |
389
+ | `--new-session` | 新建 code 会话 |
390
+ | `-f, --file <path>` | 附加文件内容到上下文(可重复) |
391
+ | `--no-tree` | 不附带项目结构摘要 |
392
+ | `--tree-limit <n>` | 项目结构最多注入多少条(默认 120) |
393
+ | `--file-max-chars <n>` | 每个附加文件最多注入字符数(默认 8000) |
394
+
395
+ ### `kie code` 交互指令
396
+
397
+ | 指令 | 作用 |
398
+ |------|------|
399
+ | `/help` | 查看帮助 |
400
+ | `/clear` | 清空当前 code 会话上下文 |
401
+ | `/cwd` | 查看当前 code 工作目录 |
402
+ | `/init` | 初始化 `.kie/code.json` |
403
+ | `/model [id|序号]` | 查看或切换模型 |
404
+ | `/route [mode]` | 查看或设置路由策略 |
405
+ | `/models` | 查看分阶段模型池、权重与历史指标 |
406
+ | `/models add <phase> <model> [weight]` | 增加/更新阶段模型权重 |
407
+ | `/models rm <phase> <model>` | 删除阶段模型 |
408
+ | `/models strategy <argmax|weighted-random>` | 设置路由决策策略 |
409
+ | `/approve on|off` | 设置是否自动确认执行命令 |
410
+ | `/sessions [id|序号]` | 查看或切换 code 会话 |
411
+ | `/files` | 查看当前附加文件 |
412
+ | `/file <路径>` | 追加文件到上下文 |
413
+ | `/run <命令>` | 在 code 工作目录执行命令,并把输出注入上下文 |
414
+ | `/tree on|off` | 开关项目结构注入 |
415
+ | `/status` | 查看当前 code 状态 |
416
+ | `exit` / `quit` | 退出 code 对话 |
417
+
418
+ ---
419
+
420
+ ## `kie image` — 生成图片
421
+
422
+ 根据文字描述生成图片,提交异步任务并自动轮询,完成后下载到当前目录的 `kie-image/` 文件夹。
423
+
424
+ ```bash
425
+ kie image 一只在月球上弹吉他的猫,赛博朋克风格
426
+ ```
427
+
428
+ 流程:提交任务 → 等待处理 → 打印图片链接 → 下载到本地。
429
+
430
+ ---
431
+
432
+ ## 常见使用场景
433
+
434
+ ### 快速问一个问题
435
+
436
+ ```bash
437
+ kie chat 用 Python 写个快速排序,加注释
438
+ ```
439
+
440
+ ### 长时间结对编程
441
+
442
+ ```bash
443
+ kie chat --new
444
+ # 进入交互模式后:
445
+ /file ./src/api.ts
446
+ /run npm test
447
+ # 持续提问…
448
+ ```
449
+
450
+ ### 分析日志
451
+
452
+ ```bash
453
+ type build.log | kie chat 总结失败原因并给出修复建议
454
+ ```
455
+
456
+ ### 多项目切换会话
457
+
458
+ ```bash
459
+ kie chat --resume project-a # 项目 A
460
+ kie chat --resume project-b # 项目 B
461
+ ```
462
+
463
+ ---
464
+
465
+ ## 开发与构建
466
+
467
+ ```bash
468
+ npm install # 安装依赖
469
+ npm run build # 编译 TypeScript → dist/
470
+ npm start -- chat # 运行(不全局安装时)
471
+ ```
472
+
473
+ 发布到 npm 前会自动执行 `npm run build`(`prepublishOnly`)。
474
+
475
+ ---
476
+
477
+ ## 常见问题
478
+
479
+ ### 提示缺少 API Key?
480
+
481
+ 先执行:
482
+
483
+ ```bash
484
+ kie config --set-api-key <KEY>
485
+ ```
486
+
487
+ ### `/compress` 之后消息变少了?
488
+
489
+ 这是预期行为:旧对话被合并成一条摘要,只保留最近几轮原文。若误操作可用 `/clear` 清空后重新开始。
490
+
491
+ ### 管道模式会保存会话吗?
492
+
493
+ 不会。只有交互模式(`kie chat` 不带参数)会读写会话文件。
494
+
495
+ ### API Key 会打进 npm 包吗?
496
+
497
+ 不会。Key 只存在你本机配置里;发布包只包含编译后的 `dist/`。
498
+
499
+ ---
500
+
501
+ ## 许可证
502
+
503
+ 请根据项目实际情况补充。
package/dist/api.js ADDED
@@ -0,0 +1,29 @@
1
+ import { DEFAULT_CHAT_MODEL } from './models.js';
2
+ import { sendChatByProvider } from './providers/index.js';
3
+ /** 收集完整回复(用于会话压缩等内部调用) */
4
+ export async function getChatCompletion(options) {
5
+ let full = '';
6
+ await sendChatMessage({
7
+ ...options,
8
+ onChunk: (text) => {
9
+ full += text;
10
+ },
11
+ });
12
+ return full;
13
+ }
14
+ /** 按模型自动路由 Claude / GPT(OpenAI 兼容) 接口 */
15
+ export async function sendChatMessage(options) {
16
+ const { apiKey, baseUrl, messages, onChunk, model = DEFAULT_CHAT_MODEL, maxTokens = 4096, thinkingFlag = true, signal, } = options;
17
+ const request = {
18
+ apiKey,
19
+ baseUrl,
20
+ messages,
21
+ model,
22
+ maxTokens,
23
+ thinkingFlag,
24
+ signal,
25
+ onChunk,
26
+ };
27
+ await sendChatByProvider(request);
28
+ }
29
+ export { kieUploadFile } from './kie-http.js';