xiaozhou-chat 1.0.6 → 1.0.7
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/lib/chat.js +33 -1
- package/package.json +1 -1
package/lib/chat.js
CHANGED
|
@@ -98,8 +98,13 @@ export async function chatStream(context, userInput = null, options = {}) {
|
|
|
98
98
|
|
|
99
99
|
const printer = new StreamPrinter();
|
|
100
100
|
|
|
101
|
+
let requestUrl = `${config.baseUrl}/chat/completions`;
|
|
102
|
+
|
|
103
|
+
// 自动尝试逻辑
|
|
104
|
+
let shouldRetryWithV1 = false;
|
|
105
|
+
|
|
101
106
|
try {
|
|
102
|
-
|
|
107
|
+
let res = await requestWithRetry(requestUrl, {
|
|
103
108
|
method: "POST",
|
|
104
109
|
headers: {
|
|
105
110
|
"Content-Type": "application/json",
|
|
@@ -109,9 +114,36 @@ export async function chatStream(context, userInput = null, options = {}) {
|
|
|
109
114
|
signal: signal
|
|
110
115
|
});
|
|
111
116
|
|
|
117
|
+
// 智能检测:如果返回的是 HTML (通常是 404 页或首页),且 URL 没带 v1,可能是用户配错了
|
|
118
|
+
const contentType = res.headers.get("content-type");
|
|
119
|
+
if (contentType && contentType.includes("text/html")) {
|
|
120
|
+
if (!config.baseUrl.endsWith("/v1") && !config.baseUrl.endsWith("/v1/")) {
|
|
121
|
+
console.log("\n⚠️ 检测到 API 返回 HTML,Base URL 可能缺少 '/v1'。");
|
|
122
|
+
console.log("🔄 正在尝试自动追加 '/v1' 重试...");
|
|
123
|
+
requestUrl = `${config.baseUrl}/v1/chat/completions`;
|
|
124
|
+
shouldRetryWithV1 = true;
|
|
125
|
+
|
|
126
|
+
// 重试请求
|
|
127
|
+
res = await requestWithRetry(requestUrl, {
|
|
128
|
+
method: "POST",
|
|
129
|
+
headers: {
|
|
130
|
+
"Content-Type": "application/json",
|
|
131
|
+
Authorization: `Bearer ${config.apiKey}`
|
|
132
|
+
},
|
|
133
|
+
body: JSON.stringify(body),
|
|
134
|
+
signal: signal
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
112
139
|
// 停止 Spinner,准备流式输出
|
|
113
140
|
spinner.stop();
|
|
114
141
|
|
|
142
|
+
if (shouldRetryWithV1 && res.ok && !res.headers.get("content-type")?.includes("text/html")) {
|
|
143
|
+
console.log(`✅ 自动修复成功!建议运行以下命令永久修改配置:`);
|
|
144
|
+
console.log(`👉 /config baseUrl ${config.baseUrl}/v1`);
|
|
145
|
+
}
|
|
146
|
+
|
|
115
147
|
if (!res.body) throw new Error("Response body is empty");
|
|
116
148
|
|
|
117
149
|
const reader = res.body.getReader();
|