@shun-js/aibaiban-server 1.4.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shun-js/aibaiban-server",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "description": "aibaiban.com server",
5
5
  "keywords": [
6
6
  "ai aibaiban"
@@ -46,5 +46,5 @@
46
46
  "access": "public",
47
47
  "registry": "https://registry.npmjs.org/"
48
48
  },
49
- "gitHead": "b7519073e9eca8171a0b54a5039901f1be1bb2e9"
49
+ "gitHead": "a07951954c905f106f4bbec45d23b133a73d15dd"
50
50
  }
@@ -53,26 +53,43 @@ exports.drawCC = async (req, res) => {
53
53
  const duration = Date.now() - startTime;
54
54
 
55
55
  // 解析 Claude Code 返回结果
56
+ // Claude Code 可能返回:纯 JSON、markdown 包裹的 JSON、或普通文本
57
+ let parsed = null;
56
58
  try {
57
- const data = JSON.parse(result);
58
- if (data.type === "excalidraw" && data.elements) {
59
- req.logger.info(
60
- methodName,
61
- "excalidraw elements",
62
- data.elements.length,
63
- `${duration}ms`,
64
- );
65
- chatFeishuMsg(req, `cc-excalidraw-${data.elements.length}-elements`);
66
- res.streaming(
67
- `data: ${JSON.stringify({ type: "draw", elements: data.elements, duration })}\n\n`,
68
- );
69
- } else {
70
- res.streaming(
71
- `data: ${JSON.stringify({ type: "message", content: result })}\n\n`,
72
- );
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;
73
75
  }
74
76
  } catch {
75
- // 非 JSON,当文字回复
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 {
76
93
  res.streaming(
77
94
  `data: ${JSON.stringify({ type: "message", content: result })}\n\n`,
78
95
  );