yuangs 1.1.4 → 1.2.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 (4) hide show
  1. package/README.md +9 -0
  2. package/cli.js +56 -25
  3. package/index.js +1 -1
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -33,6 +33,15 @@ yuangs ai "李白是谁?"
33
33
  yuangs ai "用 Python 写个 Hello World" --model gemini-pro-latest
34
34
  ```
35
35
 
36
+ ### 交互模式(v1.1.x)
37
+
38
+ 直接输入不带问题的 `ai` 命令,会进入交互式问答模式:
39
+
40
+ ```bash
41
+ yuangs ai
42
+ # 进入后直接输入问题,每次回车提问一次,按 Ctrl+C 退出
43
+ ```
44
+
36
45
  ## 应用列表
37
46
 
38
47
  古诗词 PWA: https://wealth.want.biz/shici/index.html
package/cli.js CHANGED
@@ -9,15 +9,16 @@ const command = args[0];
9
9
 
10
10
  function printHelp() {
11
11
  console.log(chalk.bold.cyan('\n🎨 苑广山的个人应用启动器\n'));
12
- console.log(chalk.yellow(`当前版本: ${version}\n`)); // 显示版本号
12
+ console.log(chalk.yellow(`当前版本: ${version}`)); // 显示版本号
13
+ console.log(chalk.gray('仓库地址: https://www.npmjs.com/package/yuangs?activeTab=readme\n'));
13
14
  console.log(chalk.white('使用方法:') + chalk.gray(' yuangs <命令> [参数]\n'));
14
15
  console.log(chalk.bold('命令列表:'));
15
16
  console.log(` ${chalk.green('shici')} 打开古诗词 PWA`);
16
17
  console.log(` ${chalk.green('dict')} 打开英语词典`);
17
18
  console.log(` ${chalk.green('pong')} 打开 Pong 游戏`);
18
19
  console.log(` ${chalk.green('list')} 列出所有应用链接`);
19
- console.log(` ${chalk.green('ai')} "<问题>" 向 AI 提问`);
20
- console.log(` ${chalk.gray('--model <模型名称>')} 指定 AI 模型 (可选)`);
20
+ console.log(` ${chalk.green('ai')} "<问题>" 向 AI 提问(不写问题进入交互模式)`);
21
+ console.log(` ${chalk.gray('--model, -m <模型名称>')} 指定 AI 模型 (可选)`);
21
22
  console.log(` ${chalk.green('help')} 显示帮助信息\n`);
22
23
  console.log(chalk.gray('AI 示例: yuangs ai "你好" --model gemini-pro-latest'));
23
24
  console.log(chalk.gray('普通示例: yuangs shici\n'));
@@ -28,27 +29,7 @@ function printSuccess(app, url) {
28
29
  console.log(chalk.gray(` ${url}`));
29
30
  }
30
31
 
31
- async function handleAICommand() {
32
- const commandArgs = args.slice(1);
33
- const modelIndex = commandArgs.indexOf('--model');
34
-
35
- let model = null; // Default model will be handled in index.js
36
- let questionParts = commandArgs;
37
-
38
- if (modelIndex !== -1 && commandArgs.length > modelIndex + 1) {
39
- model = commandArgs[modelIndex + 1];
40
- // Filter out --model and its value
41
- questionParts = commandArgs.filter((_, index) => index !== modelIndex && index !== modelIndex + 1);
42
- }
43
-
44
- const question = questionParts.join(' ');
45
-
46
- if (!question) {
47
- console.log(chalk.red('错误: 请在 ai 命令后输入你的问题。'));
48
- printHelp();
49
- return;
50
- }
51
-
32
+ async function askOnce(question, model) {
52
33
  const startTime = Date.now(); // Record start time
53
34
  let i = 0;
54
35
  const spinner = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
@@ -75,11 +56,61 @@ async function handleAICommand() {
75
56
  process.stdout.clearLine(0);
76
57
  process.stdout.cursorTo(0);
77
58
  const totalElapsedTime = (Date.now() - startTime) / 1000; // Calculate total elapsed time on error
78
- console.error(chalk.red('处理 AI 请求时出错:'), error);
59
+ console.error(chalk.red('处理 AI 请求时出错:'), error.message || error);
79
60
  console.log(chalk.gray(`\n请求耗时: ${totalElapsedTime.toFixed(2)}s\n`)); // Display total elapsed time on error
80
61
  }
81
62
  }
82
63
 
64
+ async function handleAICommand() {
65
+ const commandArgs = args.slice(1);
66
+
67
+ // 支持 --model 和 -m 两种写法
68
+ const longIndex = commandArgs.indexOf('--model');
69
+ const shortIndex = commandArgs.indexOf('-m');
70
+ const modelIndex = longIndex !== -1 ? longIndex : shortIndex;
71
+
72
+ let model = null; // Default model will be handled in index.js
73
+ let questionParts = commandArgs;
74
+
75
+ if (modelIndex !== -1 && commandArgs.length > modelIndex + 1) {
76
+ model = commandArgs[modelIndex + 1];
77
+ // Filter out --model/-m and its value
78
+ questionParts = commandArgs.filter((_, index) => index !== modelIndex && index !== modelIndex + 1);
79
+ }
80
+
81
+ const question = questionParts.join(' ').trim();
82
+
83
+ // 如果用户直接输入 `yuangs ai`,进入交互式模式
84
+ if (!question) {
85
+ console.log(chalk.bold.cyan('\n🤖 进入 AI 交互模式 (按 Ctrl+C 退出)\n'));
86
+ console.log(chalk.gray('直接输入你的问题,每回车一次提一个问题。\n'));
87
+
88
+ const readline = require('readline');
89
+ const rl = readline.createInterface({
90
+ input: process.stdin,
91
+ output: process.stdout
92
+ });
93
+
94
+ const askLoop = () => {
95
+ rl.question(chalk.green('你:'), async (q) => {
96
+ const trimmed = q.trim();
97
+ if (!trimmed) {
98
+ // 空行就继续问
99
+ return askLoop();
100
+ }
101
+ await askOnce(trimmed, model);
102
+ askLoop();
103
+ });
104
+ };
105
+
106
+ askLoop();
107
+ return;
108
+ }
109
+
110
+ // 有问题时,直接请求一次
111
+ await askOnce(question, model);
112
+ }
113
+
83
114
  switch (command) {
84
115
  case 'shici':
85
116
  printSuccess('古诗词应用', yuangs.urls.shici);
package/index.js CHANGED
@@ -37,7 +37,7 @@ async function getAIAnswer(question, model) {
37
37
  const response = await axios.post(url, data, { headers });
38
38
  return response.data;
39
39
  } catch (error) {
40
- console.error('AI 请求失败:', error.response ? error.response.data : error.message);
40
+ console.error('AI 请求失败:', error.response?.data?.message || error.message || '未知错误');
41
41
  return null;
42
42
  }
43
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yuangs",
3
- "version": "1.1.4",
3
+ "version": "1.2.0",
4
4
  "description": "苑广山的个人应用集合 CLI(彩色版)",
5
5
  "main": "index.js",
6
6
  "bin": {