@saber2pr/ai-agent 0.0.37 → 0.0.39
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.
|
@@ -4,6 +4,7 @@ exports.AgentGraphModel = void 0;
|
|
|
4
4
|
const chat_models_1 = require("@langchain/core/language_models/chat_models");
|
|
5
5
|
const messages_1 = require("@langchain/core/messages");
|
|
6
6
|
const function_calling_1 = require("@langchain/core/utils/function_calling");
|
|
7
|
+
const cleanToolDefinition_1 = require("../utils/cleanToolDefinition");
|
|
7
8
|
class AgentGraphModel extends chat_models_1.BaseChatModel {
|
|
8
9
|
constructor(fields) {
|
|
9
10
|
super(fields || {});
|
|
@@ -15,36 +16,38 @@ class AgentGraphModel extends chat_models_1.BaseChatModel {
|
|
|
15
16
|
async _generate(messages) {
|
|
16
17
|
const fullPrompt = this.serializeMessages(messages);
|
|
17
18
|
const response = await this.callApi(fullPrompt);
|
|
18
|
-
let { text, reasoning
|
|
19
|
+
let { text, reasoning } = response;
|
|
19
20
|
// 1. 处理思考内容
|
|
20
|
-
if (!reasoning && text.includes(
|
|
21
|
+
if (!reasoning && text.includes('<think>')) {
|
|
21
22
|
const match = text.match(/<think>([\s\S]*?)<\/think>/);
|
|
22
23
|
if (match) {
|
|
23
24
|
reasoning = match[1].trim();
|
|
24
|
-
text = text.replace(/<think>[\s\S]*?<\/think>/,
|
|
25
|
+
text = text.replace(/<think>[\s\S]*?<\/think>/, '').trim();
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
// 2. 解析工具调用
|
|
28
29
|
const toolCalls = this.parseToolCalls(text);
|
|
29
30
|
// AgentGraphModel.ts 的 _generate 方法内
|
|
30
31
|
return {
|
|
31
|
-
generations: [
|
|
32
|
+
generations: [
|
|
33
|
+
{
|
|
32
34
|
text,
|
|
33
35
|
message: new messages_1.AIMessage({
|
|
34
36
|
content: text,
|
|
35
37
|
tool_calls: toolCalls,
|
|
36
38
|
additional_kwargs: {
|
|
37
|
-
reasoning: reasoning ||
|
|
39
|
+
reasoning: reasoning || '',
|
|
38
40
|
token: response.token, // 👈 必须
|
|
39
41
|
duration: response.duration, // 👈 必须
|
|
40
42
|
},
|
|
41
43
|
response_metadata: {
|
|
42
|
-
reasoning: reasoning ||
|
|
44
|
+
reasoning: reasoning || '',
|
|
43
45
|
token: response.token, // 👈 McpGraphAgent 读取路径
|
|
44
46
|
duration: response.duration,
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
}
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
},
|
|
50
|
+
],
|
|
48
51
|
};
|
|
49
52
|
}
|
|
50
53
|
parseToolCalls(text) {
|
|
@@ -80,12 +83,14 @@ class AgentGraphModel extends chat_models_1.BaseChatModel {
|
|
|
80
83
|
}
|
|
81
84
|
}
|
|
82
85
|
// ✅ 此时返回的 args 必须是 object 类型
|
|
83
|
-
return [
|
|
86
|
+
return [
|
|
87
|
+
{
|
|
84
88
|
name: actionMatch[1],
|
|
85
89
|
args: typeof args === 'object' ? args : {},
|
|
86
90
|
id: `call_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`,
|
|
87
|
-
type:
|
|
88
|
-
}
|
|
91
|
+
type: 'tool_call',
|
|
92
|
+
},
|
|
93
|
+
];
|
|
89
94
|
}
|
|
90
95
|
serializeMessages(messages) {
|
|
91
96
|
const systemMsg = messages.find(m => m._getType() === 'system');
|
|
@@ -94,7 +99,9 @@ class AgentGraphModel extends chat_models_1.BaseChatModel {
|
|
|
94
99
|
const content = typeof m.content === 'string' ? m.content : JSON.stringify(m.content, null, 2);
|
|
95
100
|
return `${m._getType().toUpperCase()}: ${content}`;
|
|
96
101
|
};
|
|
97
|
-
const toolsContext = this.boundTools
|
|
102
|
+
const toolsContext = this.boundTools
|
|
103
|
+
? `\n[Tools]\n${JSON.stringify(this.boundTools.map(cleanToolDefinition_1.cleanToolDefinition), null, 2)}`
|
|
104
|
+
: '';
|
|
98
105
|
return `
|
|
99
106
|
${format(systemMsg)}
|
|
100
107
|
${toolsContext}
|
|
@@ -106,6 +113,8 @@ ${format(lastMsg)}
|
|
|
106
113
|
3. Arguments: {JSON}
|
|
107
114
|
`.trim();
|
|
108
115
|
}
|
|
109
|
-
_llmType() {
|
|
116
|
+
_llmType() {
|
|
117
|
+
return 'agent_graph_model';
|
|
118
|
+
}
|
|
110
119
|
}
|
|
111
120
|
exports.AgentGraphModel = AgentGraphModel;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cleanToolDefinition = cleanToolDefinition;
|
|
4
|
+
/**
|
|
5
|
+
* 清理工具定义:剔除外层包装和 parameters 内部的冗余元数据字段
|
|
6
|
+
*/
|
|
7
|
+
function cleanToolDefinition(tool) {
|
|
8
|
+
// 1. 提取核心 function 对象
|
|
9
|
+
const fn = tool.function || tool;
|
|
10
|
+
// 2. 解构 parameters,剔除不需要的 schema 描述字段
|
|
11
|
+
const { $schema, additionalProperties, ...cleanParameters } = fn.parameters || {};
|
|
12
|
+
// 3. 返回精简后的结构
|
|
13
|
+
return {
|
|
14
|
+
name: fn.name,
|
|
15
|
+
description: fn.description,
|
|
16
|
+
parameters: {
|
|
17
|
+
type: cleanParameters.type || "object",
|
|
18
|
+
properties: cleanParameters.properties || {},
|
|
19
|
+
required: cleanParameters.required || []
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saber2pr/ai-agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.39",
|
|
4
4
|
"description": "AI Assistant CLI.",
|
|
5
5
|
"author": "saber2pr",
|
|
6
6
|
"license": "ISC",
|
|
@@ -27,16 +27,16 @@
|
|
|
27
27
|
"@langchain/langgraph": "^0.2.74",
|
|
28
28
|
"@langchain/openai": "0.4.9",
|
|
29
29
|
"@modelcontextprotocol/sdk": "^1.25.3",
|
|
30
|
-
"@saber2pr/ts-context-mcp": "^0.0.
|
|
30
|
+
"@saber2pr/ts-context-mcp": "^0.0.9",
|
|
31
31
|
"diff": "^8.0.3",
|
|
32
32
|
"glob": "^10.5.0",
|
|
33
33
|
"js-tiktoken": "^1.0.21",
|
|
34
34
|
"minimatch": "^10.0.1",
|
|
35
35
|
"openai": "^6.16.0",
|
|
36
|
+
"typescript": "^5.9.3",
|
|
36
37
|
"zod": "3.25",
|
|
37
38
|
"zod-from-json-schema": "^0.1.0",
|
|
38
|
-
"zod-to-json-schema": "3.23.2"
|
|
39
|
-
"typescript": "^5.9.3"
|
|
39
|
+
"zod-to-json-schema": "3.23.2"
|
|
40
40
|
},
|
|
41
41
|
"resolutions": {
|
|
42
42
|
"zod": "3.25",
|