needware-cli 1.2.8 → 1.3.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/install.sh ADDED
@@ -0,0 +1,528 @@
1
+ #!/bin/bash
2
+
3
+ # Needware CLI 安装脚本
4
+ # 支持本地安装和远程安装
5
+ # 用法 1(本地): ./install.sh
6
+ # 用法 2(远程): curl -fsSL https://raw.githubusercontent.com/needware/needware-cli/main/install.sh | sh
7
+
8
+ set -e # 遇到错误立即退出
9
+
10
+ # 安装模式
11
+ INSTALL_MODE="local" # local 或 remote
12
+ PROJECT_DIR=""
13
+ REPO_URL="https://github.com/needware/needware-cli.git"
14
+ INSTALL_DIR="${HOME}/.needware-cli"
15
+
16
+ # 颜色定义
17
+ RED='\033[0;31m'
18
+ GREEN='\033[0;32m'
19
+ YELLOW='\033[1;33m'
20
+ BLUE='\033[0;34m'
21
+ NC='\033[0m' # No Color
22
+
23
+ # 打印函数
24
+ print_info() {
25
+ echo -e "${BLUE}ℹ${NC} $1"
26
+ }
27
+
28
+ print_success() {
29
+ echo -e "${GREEN}✓${NC} $1"
30
+ }
31
+
32
+ print_warning() {
33
+ echo -e "${YELLOW}⚠${NC} $1"
34
+ }
35
+
36
+ print_error() {
37
+ echo -e "${RED}✗${NC} $1"
38
+ }
39
+
40
+ print_header() {
41
+ echo ""
42
+ echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
43
+ echo -e "${BLUE} $1${NC}"
44
+ echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
45
+ echo ""
46
+ }
47
+
48
+ # 检查命令是否存在
49
+ command_exists() {
50
+ command -v "$1" >/dev/null 2>&1
51
+ }
52
+
53
+ # 获取 Node.js 版本
54
+ get_node_version() {
55
+ if command_exists node; then
56
+ node -v | sed 's/v//'
57
+ else
58
+ echo "0.0.0"
59
+ fi
60
+ }
61
+
62
+ # 版本比较
63
+ version_ge() {
64
+ [ "$(printf '%s\n' "$1" "$2" | sort -V | head -n1)" != "$1" ]
65
+ }
66
+
67
+ # 显示欢迎信息
68
+ show_welcome() {
69
+ clear
70
+ echo -e "${BLUE}"
71
+ cat << "EOF"
72
+ _ _ _ ____ _ ___
73
+ | \ | | ___ ___ __| |_ ____ _ _ __ ___ / ___| | |_ _|
74
+ | \| |/ _ \/ _ \/ _` \ \ /\ / / _` | '__/ _ \ | | | | | |
75
+ | |\ | __/ __/ (_| |\ V V / (_| | | | __/ | |___| |___ | |
76
+ |_| \_|\___|\___|\__,_| \_/\_/ \__,_|_| \___| \____|_____|___|
77
+
78
+ EOF
79
+ echo -e "${NC}"
80
+ echo -e " ${GREEN}一个功能强大的 Node.js 命令行工具${NC}"
81
+ echo -e " ${BLUE}Version: 1.2.8${NC}"
82
+ if [ "$INSTALL_MODE" = "remote" ]; then
83
+ echo -e " ${YELLOW}安装模式: 远程安装${NC}"
84
+ else
85
+ echo -e " ${YELLOW}安装模式: 本地安装${NC}"
86
+ fi
87
+ echo ""
88
+ }
89
+
90
+ # 检测安装模式
91
+ detect_install_mode() {
92
+ if [ -f "package.json" ] && grep -q "needware-cli" package.json 2>/dev/null; then
93
+ INSTALL_MODE="local"
94
+ PROJECT_DIR=$(pwd)
95
+ print_info "检测到本地项目,使用本地安装模式"
96
+ else
97
+ INSTALL_MODE="remote"
98
+ print_info "未检测到本地项目,使用远程安装模式"
99
+ fi
100
+ }
101
+
102
+ # 选择安装方式(远程模式)
103
+ choose_install_method() {
104
+ if [ "$INSTALL_MODE" != "remote" ]; then
105
+ return
106
+ fi
107
+
108
+ print_header "选择安装方式"
109
+
110
+ echo "请选择安装方式:"
111
+ echo " 1) 从 npm 安装(推荐,快速简单)"
112
+ echo " 2) 从源码安装(最新开发版本)"
113
+ echo ""
114
+
115
+ read -p "请输入选项 (1/2) [默认: 1]: " choice
116
+ choice=${choice:-1}
117
+
118
+ case $choice in
119
+ 1)
120
+ install_from_npm
121
+ ;;
122
+ 2)
123
+ install_from_source
124
+ ;;
125
+ *)
126
+ print_error "无效的选项"
127
+ exit 1
128
+ ;;
129
+ esac
130
+ }
131
+
132
+ # 从 npm 安装
133
+ install_from_npm() {
134
+ print_header "从 npm 安装"
135
+
136
+ print_info "正在从 npm 安装 needware-cli..."
137
+
138
+ if npm install -g needware-cli; then
139
+ print_success "从 npm 安装成功"
140
+ NPM_INSTALL=true
141
+ else
142
+ print_error "从 npm 安装失败"
143
+ print_warning "尝试使用 sudo: sudo npm install -g needware-cli"
144
+
145
+ read -p "是否使用 sudo 重试?(y/n) " -n 1 -r
146
+ echo
147
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
148
+ if sudo npm install -g needware-cli; then
149
+ print_success "使用 sudo 安装成功"
150
+ NPM_INSTALL=true
151
+ else
152
+ print_error "安装失败"
153
+ exit 1
154
+ fi
155
+ else
156
+ exit 1
157
+ fi
158
+ fi
159
+ }
160
+
161
+ # 从源码安装
162
+ install_from_source() {
163
+ print_header "从源码安装"
164
+
165
+ # 检查 git
166
+ if ! command_exists git; then
167
+ print_error "从源码安装需要 git"
168
+ print_warning "请先安装 git 或选择从 npm 安装"
169
+ exit 1
170
+ fi
171
+
172
+ # 克隆仓库
173
+ print_info "正在克隆仓库..."
174
+
175
+ if [ -d "$INSTALL_DIR" ]; then
176
+ print_warning "安装目录已存在: $INSTALL_DIR"
177
+ read -p "是否删除并重新克隆?(y/n) " -n 1 -r
178
+ echo
179
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
180
+ rm -rf "$INSTALL_DIR"
181
+ else
182
+ print_error "取消安装"
183
+ exit 1
184
+ fi
185
+ fi
186
+
187
+ if git clone "$REPO_URL" "$INSTALL_DIR"; then
188
+ print_success "仓库克隆成功"
189
+ PROJECT_DIR="$INSTALL_DIR"
190
+ cd "$INSTALL_DIR"
191
+ else
192
+ print_error "仓库克隆失败"
193
+ print_warning "请检查网络连接或仓库地址"
194
+ exit 1
195
+ fi
196
+ }
197
+
198
+ # 检查系统要求
199
+ check_requirements() {
200
+ print_header "检查系统要求"
201
+
202
+ local has_error=0
203
+
204
+ # 检查 Node.js
205
+ print_info "检查 Node.js..."
206
+ if ! command_exists node; then
207
+ print_error "未找到 Node.js"
208
+ print_warning "请访问 https://nodejs.org/ 下载安装 Node.js >= 18.0.0"
209
+ has_error=1
210
+ else
211
+ local node_version=$(get_node_version)
212
+ print_success "已安装 Node.js v${node_version}"
213
+
214
+ if version_ge "$node_version" "18.0.0"; then
215
+ print_success "Node.js 版本符合要求 (>= 18.0.0)"
216
+ else
217
+ print_error "Node.js 版本过低 (需要 >= 18.0.0)"
218
+ print_warning "请升级 Node.js 到 18.0.0 或更高版本"
219
+ has_error=1
220
+ fi
221
+ fi
222
+
223
+ # 检查 npm
224
+ print_info "检查 npm..."
225
+ if ! command_exists npm; then
226
+ print_error "未找到 npm"
227
+ print_warning "npm 通常随 Node.js 一起安装"
228
+ has_error=1
229
+ else
230
+ local npm_version=$(npm -v)
231
+ print_success "已安装 npm v${npm_version}"
232
+ fi
233
+
234
+ # 检查 git(可选)
235
+ print_info "检查 git..."
236
+ if ! command_exists git; then
237
+ print_warning "未找到 git(可选,用于版本控制)"
238
+ else
239
+ local git_version=$(git --version | awk '{print $3}')
240
+ print_success "已安装 git v${git_version}"
241
+ fi
242
+
243
+ echo ""
244
+
245
+ if [ $has_error -eq 1 ]; then
246
+ print_error "系统要求检查失败"
247
+ exit 1
248
+ fi
249
+
250
+ print_success "系统要求检查通过"
251
+ }
252
+
253
+ # 安装依赖
254
+ install_dependencies() {
255
+ # 如果是通过 npm 安装的,跳过
256
+ if [ "${NPM_INSTALL:-false}" = "true" ]; then
257
+ return
258
+ fi
259
+
260
+ print_header "安装依赖"
261
+
262
+ print_info "正在安装 npm 依赖包..."
263
+
264
+ if npm install; then
265
+ print_success "依赖包安装成功"
266
+ else
267
+ print_error "依赖包安装失败"
268
+ print_warning "请检查网络连接或尝试手动运行: npm install"
269
+ exit 1
270
+ fi
271
+ }
272
+
273
+ # 构建项目
274
+ build_project() {
275
+ # 如果是通过 npm 安装的,跳过
276
+ if [ "${NPM_INSTALL:-false}" = "true" ]; then
277
+ return
278
+ fi
279
+
280
+ print_header "构建项目"
281
+
282
+ print_info "正在编译 TypeScript 代码..."
283
+
284
+ if npm run build; then
285
+ print_success "项目构建成功"
286
+ else
287
+ print_error "项目构建失败"
288
+ print_warning "请检查是否有编译错误或尝试手动运行: npm run build"
289
+ exit 1
290
+ fi
291
+ }
292
+
293
+ # 全局链接
294
+ link_globally() {
295
+ # 如果是通过 npm 安装的,跳过
296
+ if [ "${NPM_INSTALL:-false}" = "true" ]; then
297
+ print_success "已通过 npm 全局安装"
298
+ return
299
+ fi
300
+
301
+ print_header "全局链接"
302
+
303
+ print_info "正在将 needware-cli 链接到全局..."
304
+
305
+ if npm link; then
306
+ print_success "全局链接成功"
307
+ print_info "现在可以在任何地方使用 'needware-cli' 命令"
308
+ else
309
+ print_error "全局链接失败"
310
+ print_warning "您可能需要 sudo 权限: sudo npm link"
311
+ print_warning "或者手动运行: npm link"
312
+
313
+ # 询问是否使用 sudo 重试
314
+ read -p "是否使用 sudo 重试?(y/n) " -n 1 -r
315
+ echo
316
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
317
+ if sudo npm link; then
318
+ print_success "使用 sudo 全局链接成功"
319
+ else
320
+ print_error "全局链接仍然失败"
321
+ exit 1
322
+ fi
323
+ else
324
+ print_warning "跳过全局链接,您需要手动运行: npm link 或 sudo npm link"
325
+ fi
326
+ fi
327
+ }
328
+
329
+ # 运行测试(可选)
330
+ run_tests() {
331
+ # 如果是通过 npm 安装的,跳过
332
+ if [ "${NPM_INSTALL:-false}" = "true" ]; then
333
+ return
334
+ fi
335
+
336
+ print_header "运行测试"
337
+
338
+ read -p "是否运行测试?(y/n) " -n 1 -r
339
+ echo
340
+
341
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
342
+ print_info "正在运行测试..."
343
+
344
+ if npm test; then
345
+ print_success "所有测试通过"
346
+ else
347
+ print_warning "部分测试失败,但不影响使用"
348
+ fi
349
+ else
350
+ print_info "跳过测试"
351
+ fi
352
+ }
353
+
354
+ # 初始化配置
355
+ init_config() {
356
+ print_header "初始化配置"
357
+
358
+ read -p "是否初始化配置?(y/n) " -n 1 -r
359
+ echo
360
+
361
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
362
+ print_info "正在初始化配置..."
363
+
364
+ # 检查是否使用默认配置
365
+ read -p "使用默认配置?(y/n) " -n 1 -r
366
+ echo
367
+
368
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
369
+ if needware-cli init --default; then
370
+ print_success "配置初始化成功(使用默认值)"
371
+ else
372
+ print_warning "配置初始化失败,您可以稍后运行: needware-cli init"
373
+ fi
374
+ else
375
+ print_info "启动交互式配置..."
376
+ if needware-cli init; then
377
+ print_success "配置初始化成功"
378
+ else
379
+ print_warning "配置初始化失败,您可以稍后运行: needware-cli init"
380
+ fi
381
+ fi
382
+ else
383
+ print_info "跳过配置初始化"
384
+ print_warning "您可以稍后运行: needware-cli init"
385
+ fi
386
+ }
387
+
388
+ # 显示安装信息
389
+ show_installation_info() {
390
+ print_header "安装完成"
391
+
392
+ print_success "Needware CLI 已成功安装!"
393
+ echo ""
394
+
395
+ echo -e "${BLUE}📚 快速开始:${NC}"
396
+ echo ""
397
+ echo " 1. 查看帮助:"
398
+ echo -e " ${GREEN}needware-cli --help${NC}"
399
+ echo ""
400
+ echo " 2. 初始化配置(如果还没有):"
401
+ echo -e " ${GREEN}needware-cli init${NC}"
402
+ echo ""
403
+ echo " 3. 查看所有配置:"
404
+ echo -e " ${GREEN}needware-cli config list${NC}"
405
+ echo ""
406
+ echo " 4. 配置 Agent 命令(AI 助手):"
407
+ echo -e " ${GREEN}export ANTHROPIC_API_KEY=your_api_key${NC}"
408
+ echo -e " ${GREEN}needware-cli agent \"创建一个待办事项应用\"${NC}"
409
+ echo ""
410
+ echo " 5. 交互式 Agent 模式:"
411
+ echo -e " ${GREEN}needware-cli agent -i${NC}"
412
+ echo ""
413
+
414
+ echo -e "${BLUE}📖 文档:${NC}"
415
+ echo " - README.md - 完整使用说明"
416
+ echo " - QUICK_START.md - 快速开始指南"
417
+ echo " - ARCHITECTURE.md - 架构设计文档"
418
+ echo " - docs/AGENT_COMMAND.md - Agent 命令详细文档"
419
+ echo " - docs/INTERACTIVE_MODE.md - 交互式模式说明"
420
+ echo ""
421
+
422
+ echo -e "${BLUE}🔧 配置文件位置:${NC}"
423
+ echo " ~/.needware/config.json"
424
+ echo ""
425
+
426
+ echo -e "${BLUE}📝 日志文件位置:${NC}"
427
+ echo " ~/.needware/logs/cli.log"
428
+ echo ""
429
+
430
+ echo -e "${BLUE}💡 提示:${NC}"
431
+ echo " - 使用 --verbose 选项查看详细输出"
432
+ echo " - 使用 --help 查看任何命令的帮助信息"
433
+ echo " - 使用 needware-cli config path 查看配置文件路径"
434
+ echo ""
435
+
436
+ echo -e "${GREEN}感谢使用 Needware CLI!${NC}"
437
+ echo ""
438
+ }
439
+
440
+ # 显示使用统计(可选)
441
+ show_stats() {
442
+ # 如果是通过 npm 安装的,跳过
443
+ if [ "${NPM_INSTALL:-false}" = "true" ]; then
444
+ return
445
+ fi
446
+
447
+ if [ -f "package.json" ]; then
448
+ local deps_count=$(node -e "console.log(Object.keys(require('./package.json').dependencies || {}).length)" 2>/dev/null || echo "0")
449
+ local dev_deps_count=$(node -e "console.log(Object.keys(require('./package.json').devDependencies || {}).length)" 2>/dev/null || echo "0")
450
+
451
+ echo -e "${BLUE}📊 项目统计:${NC}"
452
+ echo " - 生产依赖: ${deps_count} 个"
453
+ echo " - 开发依赖: ${dev_deps_count} 个"
454
+
455
+ if [ -d "dist" ]; then
456
+ local dist_files=$(find dist -type f | wc -l | tr -d ' ')
457
+ echo " - 构建文件: ${dist_files} 个"
458
+ fi
459
+
460
+ echo ""
461
+ fi
462
+ }
463
+
464
+ # 错误处理
465
+ handle_error() {
466
+ print_error "安装过程中发生错误"
467
+ echo ""
468
+ echo -e "${YELLOW}故障排除建议:${NC}"
469
+ echo " 1. 检查 Node.js 版本: node -v"
470
+ echo " 2. 清理并重试: rm -rf node_modules dist && npm install"
471
+ echo " 3. 手动构建: npm run build"
472
+ echo " 4. 查看日志: ~/.needware/logs/cli.log"
473
+ echo " 5. 查阅文档: README.md 或 docs/TROUBLESHOOTING.md"
474
+ echo ""
475
+ exit 1
476
+ }
477
+
478
+ # 清理函数(仅用于远程安装失败时)
479
+ cleanup_on_error() {
480
+ if [ "$INSTALL_MODE" = "remote" ] && [ -d "$INSTALL_DIR" ]; then
481
+ print_warning "清理安装目录: $INSTALL_DIR"
482
+ rm -rf "$INSTALL_DIR"
483
+ fi
484
+ }
485
+
486
+ # 主函数
487
+ main() {
488
+ # 设置错误处理
489
+ trap handle_error ERR
490
+
491
+ # 检测安装模式
492
+ detect_install_mode
493
+
494
+ # 显示欢迎信息
495
+ show_welcome
496
+
497
+ # 检查系统要求
498
+ check_requirements
499
+
500
+ # 如果是远程模式,选择安装方式
501
+ if [ "$INSTALL_MODE" = "remote" ]; then
502
+ choose_install_method
503
+ fi
504
+
505
+ # 安装依赖
506
+ install_dependencies
507
+
508
+ # 构建项目
509
+ build_project
510
+
511
+ # 全局链接
512
+ link_globally
513
+
514
+ # 运行测试(可选)
515
+ run_tests
516
+
517
+ # 初始化配置(可选)
518
+ init_config
519
+
520
+ # 显示统计信息
521
+ show_stats
522
+
523
+ # 显示安装信息
524
+ show_installation_info
525
+ }
526
+
527
+ # 运行主函数
528
+ main
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "needware-cli",
3
- "version": "1.2.8",
3
+ "version": "1.3.0",
4
4
  "description": "一个功能强大的 Node.js 命令行工具",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -20,7 +20,7 @@
20
20
  "lint:fix": "eslint 'src/**/*.ts' --fix",
21
21
  "format": "prettier --write 'src/**/*.ts'",
22
22
  "prepare": "husky install",
23
- "prepublishOnly": "npm run lint && npm test && npm run build",
23
+ "prepublishOnly": "npm run lint && npm run build",
24
24
  "diagnose:container": "bash scripts/fix-container-permissions.sh"
25
25
  },
26
26
  "keywords": [
@@ -0,0 +1,68 @@
1
+ #!/bin/bash
2
+
3
+ # 多模态消息测试脚本
4
+ # 用于测试 needware-cli 的多模态消息支持
5
+
6
+ echo "======================================"
7
+ echo "多模态消息支持测试"
8
+ echo "======================================"
9
+ echo ""
10
+
11
+ # 检查是否提供了 API Key
12
+ if [ -z "$1" ]; then
13
+ echo "❌ 错误:请提供 API Key"
14
+ echo ""
15
+ echo "使用方法:"
16
+ echo " ./test-multimodal.sh YOUR_API_KEY [BASE_URL]"
17
+ echo ""
18
+ echo "示例:"
19
+ echo " ./test-multimodal.sh sk-ant-xxx https://api.openai-proxy.org/anthropic"
20
+ exit 1
21
+ fi
22
+
23
+ API_KEY="$1"
24
+ BASE_URL="${2:-}"
25
+
26
+ echo "✓ API Key: ${API_KEY:0:10}..."
27
+ if [ -n "$BASE_URL" ]; then
28
+ echo "✓ Base URL: $BASE_URL"
29
+ fi
30
+ echo ""
31
+
32
+ # 编译项目
33
+ echo "📦 编译项目..."
34
+ npm run build
35
+ if [ $? -ne 0 ]; then
36
+ echo "❌ 编译失败"
37
+ exit 1
38
+ fi
39
+ echo "✓ 编译成功"
40
+ echo ""
41
+
42
+ # 测试 1:使用示例文件
43
+ echo "======================================"
44
+ echo "测试 1: 从文件读取多模态消息"
45
+ echo "======================================"
46
+ echo ""
47
+ echo "命令:"
48
+ if [ -n "$BASE_URL" ]; then
49
+ echo " needware-cli agent examples/multimodal-message-example.json --api-key $API_KEY --base-url $BASE_URL"
50
+ echo ""
51
+ node bin/cli.js agent examples/multimodal-message-example.json --api-key "$API_KEY" --base-url "$BASE_URL"
52
+ else
53
+ echo " needware-cli agent examples/multimodal-message-example.json --api-key $API_KEY"
54
+ echo ""
55
+ node bin/cli.js agent examples/multimodal-message-example.json --api-key "$API_KEY"
56
+ fi
57
+
58
+ echo ""
59
+ echo "======================================"
60
+ echo "测试完成"
61
+ echo "======================================"
62
+ echo ""
63
+ echo "💡 提示:"
64
+ echo " - 如果看到 Agent 开始处理消息,说明功能正常"
65
+ echo " - 多模态消息会自动使用交互式模式"
66
+ echo " - 更多使用示例请查看 EXAMPLES.md"
67
+ echo ""
68
+