yuangs 1.3.25 → 1.3.29

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/index.js CHANGED
@@ -4,6 +4,7 @@ const fs = require('fs');
4
4
  const path = require('path');
5
5
 
6
6
  // Store conversation history
7
+ // 存储结构标准为: [{ role: 'user', content: '...' }, { role: 'assistant', content: '...' }]
7
8
  let conversationHistory = [];
8
9
 
9
10
  // Default apps (fallback if no config file exists)
@@ -99,51 +100,67 @@ function getConversationHistory() {
99
100
  return conversationHistory;
100
101
  }
101
102
 
103
+ /**
104
+ * 获取 AI 回复 (OpenAI 兼容接口版)
105
+ */
102
106
  async function getAIAnswer(question, model, includeHistory = true) {
103
- const url = 'https://aiproxy.want.biz/ai/explain';
104
-
105
- // Prepare the prompt with conversation history if enabled
106
- let prompt;
107
- if (includeHistory) {
108
- prompt = JSON.stringify({
109
- history: conversationHistory,
110
- query: question
111
- }, null, 2);
112
- } else {
113
- // If not including history, still use JSON format for consistency but with empty history
114
- prompt = JSON.stringify({
115
- history: [],
116
- query: question
117
- }, null, 2);
118
- }
107
+ // 1. 修改接口地址为 OpenAI 标准兼容路径
108
+ const url = 'https://aiproxy.want.biz/v1/chat/completions';
119
109
 
110
+ // 2. 准备 Headers (包含客户端标识)
120
111
  const headers = {
112
+ 'Content-Type': 'application/json',
113
+ 'X-Client-ID': 'npm_yuangs', // 客户端 标识
114
+ 'Origin': 'https://cli.want.biz', // 配合后端白名单
121
115
  'Referer': 'https://cli.want.biz/',
122
116
  'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1',
123
- 'Accept': 'application/json',
124
- 'Content-Type': 'application/json'
117
+ 'Accept': 'application/json'
125
118
  };
126
119
 
120
+ // 3. 构建 messages 数组 (上下文 + 当前问题)
121
+ let messages = [];
122
+ if (includeHistory) {
123
+ // history 已经是 [{role, content}, ...] 格式,直接展开
124
+ messages = [...conversationHistory];
125
+ }
126
+
127
+ // 添加当前用户提问
128
+ messages.push({ role: 'user', content: question });
129
+
130
+ // 4. 构建 OpenAI 标准请求体
127
131
  const data = {
128
- text: prompt,
129
- model: model || "gemini-flash-lite-latest"
132
+ model: model || "gemini-flash-lite-latest",
133
+ messages: messages,
134
+ stream: false
130
135
  };
131
136
 
132
137
  try {
138
+ // 发送请求
133
139
  const response = await axios.post(url, data, { headers });
134
- const answer = response.data;
140
+
141
+ // 5. 解析 OpenAI 格式响应 (choices[0].message.content)
142
+ const aiContent = response.data?.choices?.[0]?.message?.content;
143
+
144
+ if (!aiContent) {
145
+ throw new Error('Invalid response structure from AI API');
146
+ }
135
147
 
136
- // Add the user's question to the conversation history
148
+ // 6. 更新历史记录
149
+ // 只有请求成功才记录
137
150
  addToConversationHistory('user', question);
151
+ addToConversationHistory('assistant', aiContent);
138
152
 
139
- // Add the AI response to the conversation history
140
- if (answer && answer.explanation) {
141
- addToConversationHistory('assistant', answer.explanation);
142
- }
153
+ // 返回结果
154
+ // 为了兼容旧代码可能使用的 .explanation 属性,这里做一层包装
155
+ return {
156
+ explanation: aiContent, // 兼容字段
157
+ content: aiContent, // 标准字段建议
158
+ raw: response.data // 原始响应
159
+ };
143
160
 
144
- return answer;
145
161
  } catch (error) {
146
- console.error('AI 请求失败:', error.response?.data?.message || error.message || '未知错误');
162
+ const errorMsg = error.response?.data?.error?.message || error.response?.data?.message || error.message || '未知错误';
163
+ console.error('AI 请求失败:', errorMsg);
147
164
  return null;
148
165
  }
149
166
  }
@@ -172,4 +189,4 @@ module.exports = {
172
189
  addToConversationHistory,
173
190
  clearConversationHistory,
174
191
  getConversationHistory
175
- };
192
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yuangs",
3
- "version": "1.3.25",
3
+ "version": "1.3.29",
4
4
  "description": "苑广山的个人应用集合 CLI(彩色版)",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -0,0 +1,9 @@
1
+ {
2
+ "shici": "https://wealth.want.biz/shici/index.html",
3
+ "dict": "https://wealth.want.biz/pages/dict.html",
4
+ "pong": "https://wealth.want.biz/pages/pong.html",
5
+ "mail": "https://mail.google.com",
6
+ "github": "https://github.com",
7
+ "calendar": "https://calendar.google.com",
8
+ "homepage": "https://i.want.biz"
9
+ }