@yivan-lab/pretty-please 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.
Files changed (86) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +380 -0
  3. package/bin/pls.js +681 -0
  4. package/bin/pls.tsx +541 -0
  5. package/dist/bin/pls.d.ts +2 -0
  6. package/dist/bin/pls.js +429 -0
  7. package/dist/src/ai.d.ts +48 -0
  8. package/dist/src/ai.js +295 -0
  9. package/dist/src/builtin-detector.d.ts +15 -0
  10. package/dist/src/builtin-detector.js +83 -0
  11. package/dist/src/chat-history.d.ts +26 -0
  12. package/dist/src/chat-history.js +81 -0
  13. package/dist/src/components/Chat.d.ts +13 -0
  14. package/dist/src/components/Chat.js +80 -0
  15. package/dist/src/components/ChatStatus.d.ts +9 -0
  16. package/dist/src/components/ChatStatus.js +34 -0
  17. package/dist/src/components/CodeColorizer.d.ts +12 -0
  18. package/dist/src/components/CodeColorizer.js +82 -0
  19. package/dist/src/components/CommandBox.d.ts +10 -0
  20. package/dist/src/components/CommandBox.js +45 -0
  21. package/dist/src/components/CommandGenerator.d.ts +20 -0
  22. package/dist/src/components/CommandGenerator.js +116 -0
  23. package/dist/src/components/ConfigDisplay.d.ts +9 -0
  24. package/dist/src/components/ConfigDisplay.js +42 -0
  25. package/dist/src/components/ConfigWizard.d.ts +9 -0
  26. package/dist/src/components/ConfigWizard.js +72 -0
  27. package/dist/src/components/ConfirmationPrompt.d.ts +12 -0
  28. package/dist/src/components/ConfirmationPrompt.js +26 -0
  29. package/dist/src/components/Duration.d.ts +9 -0
  30. package/dist/src/components/Duration.js +21 -0
  31. package/dist/src/components/HistoryDisplay.d.ts +9 -0
  32. package/dist/src/components/HistoryDisplay.js +51 -0
  33. package/dist/src/components/HookManager.d.ts +10 -0
  34. package/dist/src/components/HookManager.js +88 -0
  35. package/dist/src/components/InlineRenderer.d.ts +12 -0
  36. package/dist/src/components/InlineRenderer.js +75 -0
  37. package/dist/src/components/MarkdownDisplay.d.ts +13 -0
  38. package/dist/src/components/MarkdownDisplay.js +197 -0
  39. package/dist/src/components/MultiStepCommandGenerator.d.ts +25 -0
  40. package/dist/src/components/MultiStepCommandGenerator.js +142 -0
  41. package/dist/src/components/TableRenderer.d.ts +12 -0
  42. package/dist/src/components/TableRenderer.js +66 -0
  43. package/dist/src/config.d.ts +29 -0
  44. package/dist/src/config.js +203 -0
  45. package/dist/src/history.d.ts +20 -0
  46. package/dist/src/history.js +113 -0
  47. package/dist/src/mastra-agent.d.ts +7 -0
  48. package/dist/src/mastra-agent.js +31 -0
  49. package/dist/src/multi-step.d.ts +41 -0
  50. package/dist/src/multi-step.js +67 -0
  51. package/dist/src/shell-hook.d.ts +35 -0
  52. package/dist/src/shell-hook.js +348 -0
  53. package/dist/src/sysinfo.d.ts +15 -0
  54. package/dist/src/sysinfo.js +52 -0
  55. package/dist/src/ui/theme.d.ts +26 -0
  56. package/dist/src/ui/theme.js +31 -0
  57. package/dist/src/utils/console.d.ts +44 -0
  58. package/dist/src/utils/console.js +114 -0
  59. package/package.json +78 -0
  60. package/src/ai.js +324 -0
  61. package/src/builtin-detector.js +98 -0
  62. package/src/chat-history.js +94 -0
  63. package/src/components/Chat.tsx +122 -0
  64. package/src/components/ChatStatus.tsx +53 -0
  65. package/src/components/CodeColorizer.tsx +128 -0
  66. package/src/components/CommandBox.tsx +60 -0
  67. package/src/components/CommandGenerator.tsx +184 -0
  68. package/src/components/ConfigDisplay.tsx +64 -0
  69. package/src/components/ConfigWizard.tsx +101 -0
  70. package/src/components/ConfirmationPrompt.tsx +41 -0
  71. package/src/components/Duration.tsx +24 -0
  72. package/src/components/HistoryDisplay.tsx +69 -0
  73. package/src/components/HookManager.tsx +150 -0
  74. package/src/components/InlineRenderer.tsx +123 -0
  75. package/src/components/MarkdownDisplay.tsx +288 -0
  76. package/src/components/MultiStepCommandGenerator.tsx +229 -0
  77. package/src/components/TableRenderer.tsx +110 -0
  78. package/src/config.js +221 -0
  79. package/src/history.js +131 -0
  80. package/src/mastra-agent.ts +35 -0
  81. package/src/multi-step.ts +93 -0
  82. package/src/shell-hook.js +393 -0
  83. package/src/sysinfo.js +57 -0
  84. package/src/ui/theme.ts +37 -0
  85. package/src/utils/console.js +130 -0
  86. package/tsconfig.json +23 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 yivan-lab
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.
package/README.md ADDED
@@ -0,0 +1,380 @@
1
+ # Pretty Please (pls)
2
+
3
+ > 让 AI 把自然语言变成 Shell 命令
4
+
5
+ 一个 AI 驱动的命令行工具,用自然语言描述你想做什么,AI 帮你生成并执行对应的 Shell 命令。
6
+
7
+ ## ✨ 特性
8
+
9
+ - 🤖 **自然语言转命令** - 用人话说你想干什么,AI 自动生成命令
10
+ - 🔄 **多步骤命令** - AI 自动规划多个步骤,后续命令依赖前面的执行结果
11
+ - 🛡️ **智能错误恢复** - 命令失败时 AI 自动分析并调整策略
12
+ - 💬 **AI 对话模式** - 随时问 AI 命令怎么用
13
+ - 📜 **历史记录** - 保存执行过的命令,方便复用
14
+ - 🎨 **精美界面** - 基于 React + Ink 的终端 UI,Markdown 渲染
15
+ - 🔧 **多 Provider 支持** - 支持 OpenAI、DeepSeek、Anthropic 等多种 AI
16
+
17
+ ## 📦 安装
18
+
19
+ ### 方式一:npm/pnpm 全局安装(推荐)
20
+
21
+ ```bash
22
+ # 使用 npm
23
+ npm install -g @yivan-lab/pretty-please
24
+
25
+ # 或使用 pnpm
26
+ pnpm add -g @yivan-lab/pretty-please
27
+
28
+ # 或使用 yarn
29
+ yarn global add @yivan-lab/pretty-please
30
+ ```
31
+
32
+ 安装后,确保全局安装了 `tsx`:
33
+
34
+ ```bash
35
+ npm install -g tsx
36
+ # 或
37
+ pnpm add -g tsx
38
+ ```
39
+
40
+ ### 方式二:从源码安装
41
+
42
+ ```bash
43
+ # 克隆项目
44
+ git clone https://github.com/yivan-lab/pretty-please.git
45
+ cd pretty-please
46
+
47
+ # 安装依赖
48
+ pnpm install
49
+
50
+ # 全局链接
51
+ pnpm link --global
52
+
53
+ # 确保全局安装了 tsx
54
+ pnpm add -g tsx
55
+ ```
56
+
57
+ 安装完成后,你可以在任何目录使用 `pls` 或 `please` 命令了!
58
+
59
+ ## 🚀 快速开始
60
+
61
+ ### 1. 配置 API
62
+
63
+ 首次使用需要配置 AI API:
64
+
65
+ ```bash
66
+ pls config
67
+ ```
68
+
69
+ 会启动交互式配置向导,按提示输入:
70
+ - **Provider**: openai / deepseek / anthropic 等(默认 openai)
71
+ - **Base URL**: API 地址
72
+ - **API Key**: 你的 API 密钥
73
+ - **Model**: 模型名称(如 gpt-4-turbo / deepseek-chat)
74
+ - **Shell Hook**: 是否启用Shell Hook(Shell Hook 可以记录你在终端执行的历史命令,让 AI 更智能地理解上下文,推荐启用)
75
+
76
+ ### 2. 开始使用
77
+
78
+ ```bash
79
+ # 生成并执行命令
80
+ pls 查看当前目录
81
+
82
+ # 复杂任务(多步骤)
83
+ pls 查找大于100MB的日志文件并压缩
84
+
85
+ # AI 对话模式
86
+ pls chat grep 命令怎么用
87
+ ```
88
+
89
+ ## 📖 使用示例
90
+
91
+ ### 单步命令
92
+
93
+ 最基础的用法,用自然语言描述你想做什么:
94
+
95
+ ```bash
96
+ pls 查看当前目录
97
+ pls 安装 git
98
+ pls 查看当前 IP 地址
99
+ pls 删除所有 .DS_Store 文件
100
+ ```
101
+
102
+ AI 会生成对应的命令,你确认后执行。
103
+
104
+ ### 多步骤命令
105
+
106
+ 对于需要多个步骤的复杂任务,AI 会自动规划并逐步执行:
107
+
108
+ ```bash
109
+ pls 查找大于100MB的日志文件并压缩
110
+ ```
111
+
112
+ 执行过程:
113
+ 1. **步骤 1**: AI 生成 `find . -name '*.log' -size +100M` 查找大文件
114
+ 2. 你确认后执行,找到了 `app.log` 和 `system.log`
115
+ 3. **步骤 2**: AI 根据上一步的输出,生成 `tar -czf logs.tar.gz app.log system.log` 压缩命令
116
+ 4. 完成!
117
+
118
+ 每步执行后,AI 会根据实际输出结果智能地决定下一步怎么做。
119
+
120
+ ### 智能引用历史
121
+
122
+ AI 能记住你执行过的命令,支持引用之前的操作:
123
+
124
+ ```bash
125
+ # 第一步:创建文件
126
+ pls 创建一个名为 test.txt 的文件
127
+
128
+ # 第二步:引用刚才的操作
129
+ pls 删除刚才创建的文件
130
+ # AI 会知道你说的是 test.txt
131
+
132
+ # 或者手动执行了某个命令
133
+ mkdir my-project
134
+ cd my-project
135
+
136
+ # 然后让 AI 基于当前上下文工作
137
+ pls 在这个目录初始化 git 仓库
138
+ ```
139
+
140
+ 支持的引用方式:
141
+ - "刚才的文件"
142
+ - "上一个命令"
143
+ - "刚创建的目录"
144
+ - AI 会自动从历史记录中提取相关信息
145
+
146
+ ### 错误恢复
147
+
148
+ 命令执行失败?AI 会自动分析并调整策略:
149
+
150
+ ```bash
151
+ pls 将 test.zip 移动到 a、b、c 三个文件夹
152
+ ```
153
+
154
+ 执行过程:
155
+ 1. **步骤 1**: `mv test.zip a/` ✓ 成功
156
+ 2. **步骤 2**: `mv test.zip b/` ✗ 失败(文件已被移走)
157
+ 3. AI 分析错误,调整策略
158
+ 4. **步骤 3**: `cp a/test.zip b/ && cp a/test.zip c/` ✓ 改用复制
159
+
160
+ AI 不会因为一次失败就放弃,而是理解错误原因并找到解决方案。
161
+
162
+ ### 对话模式:命令讲解和问答
163
+
164
+ 想了解某个命令怎么用?用 `pls chat` 开启对话模式:
165
+
166
+ ```bash
167
+ # 询问命令用法
168
+ pls chat tar 命令怎么用
169
+
170
+ # 解释刚才执行的命令
171
+ pls 找到所有 node_modules 目录并计算大小
172
+ pls chat 刚才那个命令是干嘛的?
173
+ # AI 会详细解释刚才生成的命令的含义和参数
174
+
175
+ # 提问题
176
+ pls chat 如何批量重命名文件
177
+ pls chat grep 和 awk 有什么区别
178
+ pls chat 如何在 Linux 下挂载硬盘
179
+
180
+ # 清空对话历史
181
+ pls chat clear
182
+ ```
183
+
184
+ `pls chat` 的特点:
185
+ - 📖 **命令讲解** - 解释命令的含义、参数、用法
186
+ - 💡 **问题解答** - 回答 Shell、Linux、命令行相关问题
187
+ - 🎯 **上下文感知** - 能引用你刚才执行的命令
188
+ - 📝 **Markdown 渲染** - 支持代码高亮、表格、列表等精美格式
189
+ - 💬 **连续对话** - 保留历史,可以追问和深入讨论
190
+
191
+ ### 查看历史记录
192
+
193
+ ```bash
194
+ # 查看所有执行过的命令
195
+ pls history
196
+
197
+ # 清空历史记录
198
+ pls history clear
199
+ ```
200
+
201
+ 历史记录包含:
202
+ - 你的原始需求(自然语言)
203
+ - AI 生成的命令
204
+ - 执行状态(成功/失败/退出码)
205
+ - 执行时间
206
+
207
+ ## ⚙️ 配置管理
208
+
209
+ ```bash
210
+ pls config # 交互式配置向导
211
+ pls config list # 查看当前配置
212
+ pls config show # 同上
213
+ pls config set <key> <value> # 设置单个配置项
214
+ ```
215
+
216
+ ### 配置项说明
217
+
218
+ | 配置项 | 说明 | 默认值 |
219
+ |--------|------|--------|
220
+ | `apiKey` | AI API 密钥 | - |
221
+ | `baseUrl` | API 基础 URL | `https://api.openai.com/v1` |
222
+ | `provider` | AI Provider | `openai` |
223
+ | `model` | 模型名称 | `gpt-4-turbo` |
224
+ | `shellHook` | 启用终端历史记录 | `false` |
225
+ | `chatHistoryLimit` | 对话历史保留轮数 | `10` |
226
+
227
+ ### 支持的 Provider
228
+
229
+ - `openai` - OpenAI GPT 系列
230
+ - `deepseek` - DeepSeek
231
+ - `anthropic` - Claude
232
+ - `google` - Gemini
233
+ - `groq` - Groq
234
+ - `mistral` - Mistral AI
235
+ - `cohere` - Cohere
236
+ - `fireworks` - Fireworks AI
237
+ - `together` - Together AI
238
+
239
+ ## 🔧 Shell Hook(可选)
240
+
241
+ Shell Hook 可以记录你在终端执行的所有命令,让 AI 更智能地理解上下文。
242
+
243
+ ```bash
244
+ pls hook install # 安装 hook
245
+ pls hook status # 查看状态
246
+ pls hook uninstall # 卸载 hook
247
+ ```
248
+
249
+ 支持的 Shell:
250
+ - zsh
251
+ - bash
252
+ - PowerShell
253
+
254
+ ## 🎯 命令参考
255
+
256
+ ### 基础命令
257
+
258
+ ```bash
259
+ pls <自然语言描述> # 生成并执行命令
260
+ pls -d <描述> # Debug 模式(显示完整提示词)
261
+ pls --debug <描述> # 同上
262
+ pls -v # 查看版本
263
+ pls -h # 查看帮助
264
+ ```
265
+
266
+ ### 子命令
267
+
268
+ ```bash
269
+ # 配置
270
+ pls config
271
+ pls config list
272
+ pls config set <key> <value>
273
+
274
+ # 历史
275
+ pls history
276
+ pls history clear
277
+
278
+ # 对话
279
+ pls chat <问题>
280
+ pls chat clear
281
+
282
+ # Shell Hook
283
+ pls hook
284
+ pls hook install
285
+ pls hook uninstall
286
+ pls hook status
287
+ ```
288
+
289
+ ## 🏗️ 技术架构
290
+
291
+ ### 核心技术栈
292
+
293
+ - **React 19 + Ink 6** - 终端 UI 组件化框架
294
+ - **Mastra 0.24** - AI Agent 框架,支持 Structured Output
295
+ - **TypeScript 5.9** - 类型安全
296
+ - **Zod 3.25** - Schema 验证
297
+ - **Commander 14** - CLI 参数解析
298
+
299
+ ## 📁 目录结构
300
+
301
+ ```
302
+ pretty-please/
303
+ ├── bin/
304
+ │ └── pls.tsx # 主入口
305
+ ├── src/
306
+ │ ├── ai.js # OpenAI 客户端 + 提示词
307
+ │ ├── mastra-agent.ts # Mastra Agent 配置
308
+ │ ├── multi-step.ts # 多步骤命令核心
309
+ │ ├── config.js # 配置管理
310
+ │ ├── history.js # 命令历史
311
+ │ ├── chat-history.js # 对话历史
312
+ │ ├── shell-hook.js # Shell 集成
313
+ │ ├── components/ # React Ink 组件
314
+ │ │ ├── MultiStepCommandGenerator.tsx
315
+ │ │ ├── Chat.tsx
316
+ │ │ ├── MarkdownDisplay.tsx
317
+ │ │ └── ...
318
+ │ └── utils/
319
+ │ └── console.js # 原生输出工具
320
+ ├── package.json
321
+ └── tsconfig.json
322
+ ```
323
+
324
+ ## 🔨 开发指南
325
+
326
+ ### 开发模式
327
+
328
+ ```bash
329
+ # 直接运行(热重载)
330
+ pls <命令>
331
+
332
+ # 或使用 dev 脚本
333
+ pnpm dev <参数>
334
+ ```
335
+
336
+ 代码修改会立即生效,无需重新编译。
337
+
338
+ ### 编译
339
+
340
+ ```bash
341
+ pnpm build
342
+ ```
343
+
344
+ ### 目录说明
345
+
346
+ - 配置文件:`~/.please/config.json`
347
+ - 命令历史:`~/.please/history.json`
348
+ - 对话历史:`~/.please/chat_history.json`
349
+ - Shell 历史:`~/.please/shell_history.jsonl`
350
+
351
+ ## 🐛 常见问题
352
+
353
+ ### 为什么 Chat 会清空终端历史?
354
+
355
+ 这是 Ink 的设计局限。我们采用了混合渲染模式,命令执行使用原生输出来保留历史,只有 Chat 模式会清空。
356
+
357
+ ### AI 返回的命令格式不对?
358
+
359
+ 如果遇到 AI 不遵守格式的情况,可以:
360
+ 1. 使用 `--debug` 查看完整提示词
361
+ 2. 检查 provider 和 model 配置是否正确
362
+ 3. 某些模型需要 `jsonPromptInjection`(代码已自动处理)
363
+
364
+ ### 为什么要用 Zod 3.x?
365
+
366
+ AI SDK 和 Mastra 依赖 Zod 3.23.8+,Zod 4.x 有 breaking changes 不兼容。
367
+
368
+ ## 📄 许可证
369
+
370
+ MIT
371
+
372
+ ## 🙏 致谢
373
+
374
+ - [fuckit.sh](https://github.com/faithleysath/fuckit.sh) - 提供了最初的灵感和思路,一个优雅的 AI 命令行工具
375
+ - [Ink](https://github.com/vadimdemedes/ink) - 终端 React 渲染器
376
+ - [Mastra](https://mastra.ai) - AI Agent 框架
377
+
378
+ ---
379
+
380
+ **Made with ❤️ and AI**