@shun-js/aibaiban-server 1.4.1 → 1.4.3
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/app.js +9 -9
- package/assets/sw.js +2 -2
- package/package.json +3 -2
- package/server/controller/IndexController.js +2 -2
- package/server/controller/LLMController.js +8 -2
- package/server/controller/SEOController.js +8 -8
- package/server/log-options.js +7 -7
- package/server/service/CCService.js +111 -0
- package/server/service/IndexService.js +1 -1
- package/server/service/LLMService.js +309 -135
- package/server/service/SEOService.js +10 -10
- package/server/util/check.js +2 -2
- package/server/util/feishu.js +10 -5
- package/server/util/prompt-agent.js +137 -93
- package/server/util/relay-client.js +70 -0
- package/views/index.html +51 -15
package/app.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// config
|
|
2
|
-
const { parseServerConfig } = require(
|
|
2
|
+
const { parseServerConfig } = require("@shun-js/shun-config");
|
|
3
3
|
|
|
4
4
|
// init
|
|
5
5
|
(async () => {
|
|
6
6
|
// config
|
|
7
7
|
const config = await parseServerConfig(process.argv);
|
|
8
8
|
if (!config) {
|
|
9
|
-
console.log(
|
|
9
|
+
console.log("read server config fail");
|
|
10
10
|
return;
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -20,23 +20,23 @@ const { parseServerConfig } = require('@shun-js/shun-config');
|
|
|
20
20
|
options.config = config;
|
|
21
21
|
|
|
22
22
|
// options.redis
|
|
23
|
-
options.redis = require(
|
|
23
|
+
options.redis = require("qiao-redis");
|
|
24
24
|
options.redisOptions = config.redisOptions;
|
|
25
25
|
|
|
26
26
|
// options log
|
|
27
|
-
options.log = require(
|
|
28
|
-
options.logOptions = require(
|
|
27
|
+
options.log = require("qiao-log");
|
|
28
|
+
options.logOptions = require("./server/log-options.js")();
|
|
29
29
|
|
|
30
30
|
// options rate limit
|
|
31
|
-
options.rateLimitLib = require(
|
|
31
|
+
options.rateLimitLib = require("qiao-rate-limit");
|
|
32
32
|
options.rateLimitOptions = config.rateLimitOptions;
|
|
33
33
|
|
|
34
34
|
// options checks
|
|
35
|
-
options.checks = [require(
|
|
35
|
+
options.checks = [require("./server/util/check.js").checkUserAuth];
|
|
36
36
|
|
|
37
37
|
// options modules
|
|
38
|
-
options.modules = [require(
|
|
38
|
+
options.modules = [require("qiao-z-sms").init, require("qiao-z-nuser").init];
|
|
39
39
|
|
|
40
|
-
const app = await require(
|
|
40
|
+
const app = await require("qiao-z")(options);
|
|
41
41
|
app.listen(config.port);
|
|
42
42
|
})();
|
package/assets/sw.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// Service Worker - 仅用于满足 PWA 安装条件
|
|
2
|
-
self.addEventListener(
|
|
3
|
-
self.addEventListener(
|
|
2
|
+
self.addEventListener("install", () => self.skipWaiting());
|
|
3
|
+
self.addEventListener("activate", (e) => e.waitUntil(self.clients.claim()));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shun-js/aibaiban-server",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"description": "aibaiban.com server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai aibaiban"
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"qiao-z-service": "^6.0.0",
|
|
39
39
|
"qiao-z-sms": "^6.0.0",
|
|
40
40
|
"viho-llm": "^1.1.0",
|
|
41
|
+
"ws": "^8.20.0",
|
|
41
42
|
"zod": "^4.3.6",
|
|
42
43
|
"zod-to-json-schema": "^3.25.1"
|
|
43
44
|
},
|
|
@@ -45,5 +46,5 @@
|
|
|
45
46
|
"access": "public",
|
|
46
47
|
"registry": "https://registry.npmjs.org/"
|
|
47
48
|
},
|
|
48
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "a07951954c905f106f4bbec45d23b133a73d15dd"
|
|
49
50
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// service
|
|
2
|
-
const service = require(
|
|
2
|
+
const service = require("../service/IndexService.js");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* controller
|
|
6
6
|
*/
|
|
7
7
|
module.exports = (app) => {
|
|
8
8
|
// index
|
|
9
|
-
app.get(
|
|
9
|
+
app.get("/", (req, res) => {
|
|
10
10
|
service.index(req, res);
|
|
11
11
|
});
|
|
12
12
|
};
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
// service
|
|
2
|
-
const service = require(
|
|
2
|
+
const service = require("../service/LLMService.js");
|
|
3
|
+
const ccService = require("../service/CCService.js");
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* controller
|
|
6
7
|
*/
|
|
7
8
|
module.exports = (app) => {
|
|
8
9
|
// draw agent
|
|
9
|
-
app.post(
|
|
10
|
+
app.post("/draw-agent", (req, res) => {
|
|
10
11
|
service.drawAgent(req, res);
|
|
11
12
|
});
|
|
13
|
+
|
|
14
|
+
// draw cc (Claude Code via Docker)
|
|
15
|
+
app.post("/draw-cc", (req, res) => {
|
|
16
|
+
ccService.drawCC(req, res);
|
|
17
|
+
});
|
|
12
18
|
};
|
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
// service
|
|
2
|
-
const service = require(
|
|
2
|
+
const service = require("../service/SEOService.js");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* controller
|
|
6
6
|
*/
|
|
7
7
|
module.exports = (app) => {
|
|
8
8
|
// seo
|
|
9
|
-
app.get(
|
|
9
|
+
app.get("/robots.txt", (req, res) => {
|
|
10
10
|
service.robots(req, res);
|
|
11
11
|
});
|
|
12
|
-
app.get(
|
|
12
|
+
app.get("/sitemap.xml", (req, res) => {
|
|
13
13
|
service.sitemap(req, res);
|
|
14
14
|
});
|
|
15
|
-
app.get(
|
|
15
|
+
app.get("/4cb288d7aef5469c92616c0f5b5aeb89.txt", (req, res) => {
|
|
16
16
|
service.bingIndexNow(req, res);
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
// pwa
|
|
20
|
-
app.get(
|
|
20
|
+
app.get("/manifest.json", (req, res) => {
|
|
21
21
|
service.manifestJson(req, res);
|
|
22
22
|
});
|
|
23
|
-
app.get(
|
|
23
|
+
app.get("/sw.js", (req, res) => {
|
|
24
24
|
service.swJs(req, res);
|
|
25
25
|
});
|
|
26
|
-
app.get(
|
|
26
|
+
app.get("/icon-192.png", (req, res) => {
|
|
27
27
|
service.icon192Png(req, res);
|
|
28
28
|
});
|
|
29
|
-
app.get(
|
|
29
|
+
app.get("/icon-512.png", (req, res) => {
|
|
30
30
|
service.icon512Png(req, res);
|
|
31
31
|
});
|
|
32
32
|
};
|
package/server/log-options.js
CHANGED
|
@@ -4,19 +4,19 @@
|
|
|
4
4
|
*/
|
|
5
5
|
module.exports = () => {
|
|
6
6
|
// log options
|
|
7
|
-
const logLevel =
|
|
8
|
-
const logPattern =
|
|
9
|
-
const logPath = require(
|
|
7
|
+
const logLevel = "debug";
|
|
8
|
+
const logPattern = "yyyy-MM-dd-hh";
|
|
9
|
+
const logPath = require("path").resolve(__dirname, "../logs/qiao-z.log");
|
|
10
10
|
|
|
11
11
|
return {
|
|
12
12
|
pm2: true,
|
|
13
|
-
pm2InstanceVar:
|
|
13
|
+
pm2InstanceVar: "INSTANCE_ID",
|
|
14
14
|
appenders: {
|
|
15
15
|
stdout: {
|
|
16
|
-
type:
|
|
16
|
+
type: "stdout",
|
|
17
17
|
},
|
|
18
18
|
datefile: {
|
|
19
|
-
type:
|
|
19
|
+
type: "dateFile",
|
|
20
20
|
pattern: logPattern,
|
|
21
21
|
filename: logPath,
|
|
22
22
|
keepFileExt: true,
|
|
@@ -27,7 +27,7 @@ module.exports = () => {
|
|
|
27
27
|
categories: {
|
|
28
28
|
default: {
|
|
29
29
|
level: logLevel,
|
|
30
|
-
appenders: [
|
|
30
|
+
appenders: ["stdout", "datefile"],
|
|
31
31
|
},
|
|
32
32
|
},
|
|
33
33
|
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const { chatFeishuMsg, errorFeishuMsg } = require("../util/feishu.js");
|
|
2
|
+
const { sendPromptViaRelay } = require("../util/relay-client.js");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* drawCC - Claude Code via Docker
|
|
6
|
+
* 所有请求直接通过 WS relay 发送给 Docker 内的 Claude Code 处理
|
|
7
|
+
*/
|
|
8
|
+
exports.drawCC = async (req, res) => {
|
|
9
|
+
const methodName = "drawCC";
|
|
10
|
+
const messages = req.body.messages;
|
|
11
|
+
|
|
12
|
+
if (!messages?.length) {
|
|
13
|
+
const msg = "need messages";
|
|
14
|
+
req.logger.error(methodName, msg);
|
|
15
|
+
res.jsonFail(msg);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const relayConfig = global.QZ_CONFIG.relay;
|
|
20
|
+
if (!relayConfig?.wsUrl) {
|
|
21
|
+
req.logger.error(methodName, "relay config missing");
|
|
22
|
+
res.jsonFail("relay not configured");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
res.streamingStart();
|
|
27
|
+
|
|
28
|
+
const lastUserMsg =
|
|
29
|
+
messages.filter((m) => m.role === "user").pop()?.content || "";
|
|
30
|
+
req.logger.info(methodName, "lastUserMsg", lastUserMsg);
|
|
31
|
+
chatFeishuMsg(req, `cc-${lastUserMsg}`);
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const startTime = Date.now();
|
|
35
|
+
|
|
36
|
+
res.streaming(
|
|
37
|
+
`data: ${JSON.stringify({ type: "status", step: "thinking" })}\n\n`,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const keepalive = setInterval(() => {
|
|
41
|
+
res.streaming(": keepalive\n\n");
|
|
42
|
+
}, 15000);
|
|
43
|
+
|
|
44
|
+
const result = await sendPromptViaRelay({
|
|
45
|
+
wsUrl: relayConfig.wsUrl,
|
|
46
|
+
authSecret: relayConfig.authSecret,
|
|
47
|
+
userId: req.user?.id || "guest",
|
|
48
|
+
prompt: lastUserMsg,
|
|
49
|
+
timeout: relayConfig.timeout || 600000,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
clearInterval(keepalive);
|
|
53
|
+
const duration = Date.now() - startTime;
|
|
54
|
+
|
|
55
|
+
// 解析 Claude Code 返回结果
|
|
56
|
+
// Claude Code 可能返回:纯 JSON、markdown 包裹的 JSON、或普通文本
|
|
57
|
+
let parsed = null;
|
|
58
|
+
try {
|
|
59
|
+
let raw = result;
|
|
60
|
+
// 剥离 markdown 代码块: ```json\n...\n```
|
|
61
|
+
const fenceMatch = raw.match(/```(?:json)?\s*\n([\s\S]*?)\n```/);
|
|
62
|
+
if (fenceMatch) {
|
|
63
|
+
raw = fenceMatch[1].trim();
|
|
64
|
+
}
|
|
65
|
+
// 处理可能的双重 JSON 编码
|
|
66
|
+
let decoded = raw;
|
|
67
|
+
if (typeof decoded === "string") {
|
|
68
|
+
decoded = JSON.parse(decoded);
|
|
69
|
+
}
|
|
70
|
+
if (typeof decoded === "string") {
|
|
71
|
+
decoded = JSON.parse(decoded);
|
|
72
|
+
}
|
|
73
|
+
if (decoded && decoded.type === "excalidraw" && decoded.elements) {
|
|
74
|
+
parsed = decoded;
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
// 非 excalidraw JSON
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (parsed) {
|
|
81
|
+
req.logger.info(
|
|
82
|
+
methodName,
|
|
83
|
+
"excalidraw elements",
|
|
84
|
+
parsed.elements.length,
|
|
85
|
+
`${duration}ms`,
|
|
86
|
+
);
|
|
87
|
+
chatFeishuMsg(req, `cc-excalidraw-${parsed.elements.length}-elements`);
|
|
88
|
+
res.streaming(`data: ${JSON.stringify({ type: "clear" })}\n\n`);
|
|
89
|
+
res.streaming(
|
|
90
|
+
`data: ${JSON.stringify({ type: "draw", elements: parsed.elements, duration })}\n\n`,
|
|
91
|
+
);
|
|
92
|
+
} else {
|
|
93
|
+
res.streaming(
|
|
94
|
+
`data: ${JSON.stringify({ type: "message", content: result })}\n\n`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
res.streamingEnd();
|
|
99
|
+
} catch (error) {
|
|
100
|
+
req.logger.error(methodName, "error", error);
|
|
101
|
+
errorFeishuMsg(req, error.message);
|
|
102
|
+
const userMessage =
|
|
103
|
+
error.message === "AI agent is offline"
|
|
104
|
+
? "AI 助手离线中,请稍后重试"
|
|
105
|
+
: "服务暂时出错,请稍后重试";
|
|
106
|
+
res.streaming(
|
|
107
|
+
`data: ${JSON.stringify({ type: "error", message: userMessage })}\n\n`,
|
|
108
|
+
);
|
|
109
|
+
res.streamingEnd();
|
|
110
|
+
}
|
|
111
|
+
};
|