@shun-js/aibaiban-server 1.1.9 → 1.2.0
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.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "aibaiban.com server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai aibaiban"
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"access": "public",
|
|
46
46
|
"registry": "https://registry.npmjs.org/"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "7b094023fede487784f42ca71277997906841cb1"
|
|
49
49
|
}
|
|
@@ -12,6 +12,44 @@ const llm = OpenAIAPI(finalLLMConfig);
|
|
|
12
12
|
const modelName = finalLLMConfig.modelName;
|
|
13
13
|
const thinking = finalLLMConfig.thinking;
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* 从 LLM 返回文本中提取 JSON(兼容 markdown 代码块、前后有多余文字等情况)
|
|
17
|
+
*/
|
|
18
|
+
function extractJSON(text) {
|
|
19
|
+
if (typeof text === 'object' && text !== null) return text;
|
|
20
|
+
const str = String(text).trim();
|
|
21
|
+
|
|
22
|
+
// 1. 尝试直接解析
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(str);
|
|
25
|
+
} catch (_) {
|
|
26
|
+
/* ignore */
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 2. 尝试从 markdown 代码块提取
|
|
30
|
+
const codeBlockMatch = str.match(/```(?:json)?\s*\n?([\s\S]*?)\n?```/);
|
|
31
|
+
if (codeBlockMatch) {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(codeBlockMatch[1].trim());
|
|
34
|
+
} catch (_) {
|
|
35
|
+
/* ignore */
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 3. 尝试提取第一个 {...} 块
|
|
40
|
+
const firstBrace = str.indexOf('{');
|
|
41
|
+
const lastBrace = str.lastIndexOf('}');
|
|
42
|
+
if (firstBrace !== -1 && lastBrace > firstBrace) {
|
|
43
|
+
try {
|
|
44
|
+
return JSON.parse(str.slice(firstBrace, lastBrace + 1));
|
|
45
|
+
} catch (_) {
|
|
46
|
+
/* ignore */
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
throw new Error(`Cannot parse JSON from LLM response: ${str.slice(0, 200)}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
15
53
|
/**
|
|
16
54
|
* drawAgentV2 - 对话式 Agent
|
|
17
55
|
* 1 次决策 LLM → reply / generate(elaborate+generate) / irrelevant
|
|
@@ -50,11 +88,10 @@ exports.drawAgent = async (req, res) => {
|
|
|
50
88
|
llm,
|
|
51
89
|
modelName,
|
|
52
90
|
thinking,
|
|
53
|
-
isJson: true,
|
|
54
91
|
messages: [{ role: 'system', content: prompts.AGENT_PROMPT }, ...messages],
|
|
55
92
|
},
|
|
56
93
|
agentEndCallback: (result) => {
|
|
57
|
-
decision = result;
|
|
94
|
+
decision = extractJSON(result);
|
|
58
95
|
const duration = Date.now() - startTime;
|
|
59
96
|
req.logger.info(methodName, 'decision', JSON.stringify(decision), `${duration}ms`);
|
|
60
97
|
chatFeishuMsg(req, `v2-decision-${decision.action}`);
|