@saber2pr/ai-agent 0.0.35 → 0.0.38
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.
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BaseMessage } from '@langchain/core/messages';
|
|
2
2
|
import { GraphAgentOptions } from '../types/type';
|
|
3
|
+
import { AgentGraphModel } from '../model/AgentGraphModel';
|
|
3
4
|
export declare const CONFIG_FILE: string;
|
|
4
5
|
interface TokenUsage {
|
|
5
6
|
total: number;
|
|
@@ -12,7 +13,7 @@ declare const AgentState: import("@langchain/langgraph").AnnotationRoot<{
|
|
|
12
13
|
tokenUsage: import("@langchain/langgraph").BinaryOperatorAggregate<TokenUsage, TokenUsage>;
|
|
13
14
|
totalDuration: import("@langchain/langgraph").BinaryOperatorAggregate<number, number>;
|
|
14
15
|
}>;
|
|
15
|
-
export default class McpGraphAgent {
|
|
16
|
+
export default class McpGraphAgent<T extends AgentGraphModel = any> {
|
|
16
17
|
private model;
|
|
17
18
|
private toolNode;
|
|
18
19
|
private targetDir;
|
|
@@ -27,7 +28,7 @@ export default class McpGraphAgent {
|
|
|
27
28
|
private maxTargetCount;
|
|
28
29
|
private maxTokens;
|
|
29
30
|
private mcpClients;
|
|
30
|
-
constructor(options?: GraphAgentOptions);
|
|
31
|
+
constructor(options?: GraphAgentOptions<T>);
|
|
31
32
|
private printLoadedTools;
|
|
32
33
|
private loadMcpConfigs;
|
|
33
34
|
private initMcpTools;
|
|
@@ -37,13 +38,13 @@ export default class McpGraphAgent {
|
|
|
37
38
|
private showLoading;
|
|
38
39
|
private startLoading;
|
|
39
40
|
private stopLoading;
|
|
40
|
-
|
|
41
|
+
getModel(): Promise<T>;
|
|
41
42
|
private askForConfig;
|
|
42
43
|
chat(query?: string): Promise<void>;
|
|
43
44
|
start(): Promise<void>;
|
|
44
45
|
private renderOutput;
|
|
45
46
|
callModel(state: typeof AgentState.State): Promise<{
|
|
46
|
-
messages:
|
|
47
|
+
messages: import("@langchain/core/messages").AIMessageChunk[];
|
|
47
48
|
tokenUsage: {
|
|
48
49
|
total: number;
|
|
49
50
|
};
|
|
@@ -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;
|
package/lib/types/type.d.ts
CHANGED
|
@@ -34,8 +34,8 @@ export interface AgentOptions {
|
|
|
34
34
|
verbose?: boolean;
|
|
35
35
|
apiConfig?: ApiConfig;
|
|
36
36
|
}
|
|
37
|
-
export interface GraphAgentOptions extends AgentOptions {
|
|
38
|
-
apiModel?:
|
|
37
|
+
export interface GraphAgentOptions<T extends AgentGraphModel = any> extends AgentOptions {
|
|
38
|
+
apiModel?: T;
|
|
39
39
|
alwaysSystem?: boolean;
|
|
40
40
|
recursionLimit?: number;
|
|
41
41
|
maxTargetCount?: number;
|
|
@@ -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
|
+
}
|