xiaozhou-chat 1.0.15 → 1.0.16
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 +28 -1
- package/package.json +3 -2
package/lib/chat.js
CHANGED
|
@@ -3,6 +3,7 @@ import { Spinner, StreamPrinter } from "./ui.js";
|
|
|
3
3
|
import { sleep } from "./utils.js";
|
|
4
4
|
import { updateConfig, setProfileValue } from "./config.js";
|
|
5
5
|
import { builtInTools } from "./tools.js";
|
|
6
|
+
import { Agent, fetch as undiciFetch } from "undici";
|
|
6
7
|
|
|
7
8
|
// 尝试加载 Markdown 渲染库
|
|
8
9
|
let marked;
|
|
@@ -14,9 +15,21 @@ try {
|
|
|
14
15
|
|
|
15
16
|
export async function requestWithRetry(url, options, maxRetries = 3) {
|
|
16
17
|
let lastError;
|
|
18
|
+
let useNoProxy = false;
|
|
19
|
+
|
|
17
20
|
for (let i = 0; i <= maxRetries; i++) {
|
|
18
21
|
try {
|
|
19
|
-
|
|
22
|
+
let res;
|
|
23
|
+
if (useNoProxy) {
|
|
24
|
+
const dispatcher = new Agent({
|
|
25
|
+
connect: { timeout: 30000 }
|
|
26
|
+
});
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
res = await undiciFetch(url, { ...options, dispatcher });
|
|
29
|
+
} else {
|
|
30
|
+
res = await fetch(url, options);
|
|
31
|
+
}
|
|
32
|
+
|
|
20
33
|
if (!res.ok) {
|
|
21
34
|
const text = await res.text();
|
|
22
35
|
// 4xx errors: do not retry
|
|
@@ -30,6 +43,20 @@ export async function requestWithRetry(url, options, maxRetries = 3) {
|
|
|
30
43
|
} catch (e) {
|
|
31
44
|
lastError = e;
|
|
32
45
|
if (e.name === 'AbortError') throw e;
|
|
46
|
+
|
|
47
|
+
// 智能代理 fallback: 如果失败且环境中有代理配置,尝试直连
|
|
48
|
+
const isNetworkError = e.message.includes("fetch failed") || e.cause?.code === 'ECONNREFUSED' || e.cause?.code === 'ETIMEDOUT';
|
|
49
|
+
const hasProxyEnv = process.env.HTTP_PROXY || process.env.HTTPS_PROXY || process.env.ALL_PROXY;
|
|
50
|
+
|
|
51
|
+
if (isNetworkError && hasProxyEnv && !useNoProxy) {
|
|
52
|
+
console.log(`\n⚠️ 检测到网络错误,且存在代理环境变量 (${process.env.HTTPS_PROXY || process.env.HTTP_PROXY})`);
|
|
53
|
+
console.log("🔄 自动尝试忽略代理进行直连...");
|
|
54
|
+
useNoProxy = true;
|
|
55
|
+
// 不计入重试次数,立即重试
|
|
56
|
+
i--;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
|
|
33
60
|
if (i === maxRetries) break;
|
|
34
61
|
|
|
35
62
|
const delay = 1000 * Math.pow(2, i);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xiaozhou-chat",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"description": "CLI chatbot based on NewAPI",
|
|
5
5
|
"bin": {
|
|
6
6
|
"xiaozhou-chat": "bin/cli.js"
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"marked": "^15.0.12",
|
|
23
23
|
"marked-terminal": "^7.3.0",
|
|
24
24
|
"minimist": "^1.2.8",
|
|
25
|
-
"moment": "^2.30.1"
|
|
25
|
+
"moment": "^2.30.1",
|
|
26
|
+
"undici": "^7.19.0"
|
|
26
27
|
},
|
|
27
28
|
"pkg": {
|
|
28
29
|
"assets": []
|