@saber2pr/ai-agent 0.0.47 → 0.0.49

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.
@@ -43,12 +43,12 @@ export default class McpGraphAgent<T extends AgentGraphModel = any> {
43
43
  private loadMcpConfigs;
44
44
  private initMcpTools;
45
45
  private prepareTools;
46
- ensureInitialized(): Promise<void>;
46
+ ensureInitialized(): Promise<T>;
47
47
  private closeMcpClients;
48
48
  private showLoading;
49
49
  private startLoading;
50
50
  private stopLoading;
51
- getModel(): Promise<T>;
51
+ private getModel;
52
52
  private askForConfig;
53
53
  chat(query?: string): Promise<void>;
54
54
  /**
@@ -180,17 +180,29 @@ class McpGraphAgent {
180
180
  const allToolInfos = [...builtinToolInfos, ...(this.options.tools || []), ...mcpToolInfos];
181
181
  this.langchainTools = allToolInfos.map(t => (0, convertToLangChainTool_1.convertToLangChainTool)(t));
182
182
  this.toolNode = new prebuilt_1.ToolNode(this.langchainTools);
183
+ return {
184
+ builtinToolInfos,
185
+ mcpToolInfos,
186
+ tools: this.options.tools,
187
+ langchainTools: this.langchainTools,
188
+ };
183
189
  }
184
190
  // ✅ 修改:初始化逻辑
185
191
  async ensureInitialized() {
186
- if (this.model && this.langchainTools.length > 0)
187
- return;
192
+ if (this.model && this.langchainTools.length > 0) {
193
+ return this.getModel();
194
+ }
195
+ ;
188
196
  // 1. 加载所有工具(含 MCP)
189
- await this.prepareTools();
197
+ const toolsInfo = await this.prepareTools();
190
198
  // 2. 初始化模型
191
- await this.getModel();
199
+ const apiModel = await this.getModel();
200
+ if (toolsInfo.mcpToolInfos && apiModel.setMcpTools) {
201
+ apiModel.setMcpTools(toolsInfo.mcpToolInfos);
202
+ }
192
203
  // 3. 打印工具状态
193
204
  this.printLoadedTools();
205
+ return apiModel;
194
206
  }
195
207
  // ✅ 新增:关闭连接
196
208
  async closeMcpClients() {
@@ -268,7 +280,6 @@ class McpGraphAgent {
268
280
  async chat(query = '开始代码审计') {
269
281
  try {
270
282
  await this.ensureInitialized();
271
- await this.getModel();
272
283
  const app = await this.createGraph();
273
284
  const graphStream = await app.stream({
274
285
  messages: [new messages_1.HumanMessage(query)],
@@ -298,7 +309,6 @@ class McpGraphAgent {
298
309
  this.streamEnabled = true;
299
310
  try {
300
311
  await this.ensureInitialized();
301
- await this.getModel();
302
312
  const app = await this.createGraph();
303
313
  const graphStream = await app.stream({
304
314
  messages: [new messages_1.HumanMessage(query)],
@@ -322,7 +332,6 @@ class McpGraphAgent {
322
332
  }
323
333
  async start() {
324
334
  await this.ensureInitialized();
325
- await this.getModel();
326
335
  const app = await this.createGraph();
327
336
  const rl = readline_1.default.createInterface({ input: process.stdin, output: process.stdout });
328
337
  rl.on('SIGINT', () => {
@@ -11,9 +11,16 @@ export interface AgentGraphLLMResponse {
11
11
  export type StreamChunkCallback = (chunk: string) => void;
12
12
  export declare abstract class AgentGraphModel extends BaseChatModel {
13
13
  protected boundTools?: any[];
14
+ private mcpEnabled?;
15
+ private mcpTools?;
16
+ setMcpTools(tools: any[]): void;
17
+ setMcpEnabled(enabled?: boolean): void;
18
+ getMcpEnabled(): boolean;
19
+ getMcpTools(): any[];
20
+ private isMcpTool;
14
21
  constructor(fields?: BaseChatModelParams);
15
22
  bindTools(tools: any[]): any;
16
- abstract callApi(prompt: string): Promise<AgentGraphLLMResponse>;
23
+ abstract callApi(prompt: string, lastMsg: BaseMessage): Promise<AgentGraphLLMResponse>;
17
24
  /**
18
25
  * 流式调用 API,子类可覆盖以实现真正的 SSE 流式传输。
19
26
  * 默认回退到 callApi 非流式调用。
@@ -21,7 +28,7 @@ export declare abstract class AgentGraphModel extends BaseChatModel {
21
28
  * @param onChunk 每收到一段文本时的回调
22
29
  * @returns 完整的响应结果
23
30
  */
24
- callApiStream(prompt: string, onChunk: StreamChunkCallback): Promise<AgentGraphLLMResponse>;
31
+ callApiStream(prompt: string, lastMsg: BaseMessage, onChunk: StreamChunkCallback): Promise<AgentGraphLLMResponse>;
25
32
  _generate(messages: BaseMessage[]): Promise<ChatResult>;
26
33
  private parseToolCalls;
27
34
  /**
@@ -5,9 +5,28 @@ 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
7
  const cleanToolDefinition_1 = require("../utils/cleanToolDefinition");
8
+ const kit_1 = require("../utils/kit");
8
9
  class AgentGraphModel extends chat_models_1.BaseChatModel {
10
+ setMcpTools(tools) {
11
+ this.mcpTools = tools;
12
+ }
13
+ setMcpEnabled(enabled = true) {
14
+ this.mcpEnabled = enabled;
15
+ }
16
+ getMcpEnabled() {
17
+ return this.mcpEnabled;
18
+ }
19
+ getMcpTools() {
20
+ return this.mcpTools;
21
+ }
22
+ isMcpTool(tool) {
23
+ const mcpTools = (0, kit_1.getArray)(this.mcpTools);
24
+ return mcpTools.some(t => { var _a, _b; return ((_a = t === null || t === void 0 ? void 0 : t.function) === null || _a === void 0 ? void 0 : _a.name) === ((_b = tool === null || tool === void 0 ? void 0 : tool.function) === null || _b === void 0 ? void 0 : _b.name); });
25
+ }
9
26
  constructor(fields) {
10
27
  super(fields || {});
28
+ this.mcpEnabled = true;
29
+ this.mcpTools = [];
11
30
  }
12
31
  bindTools(tools) {
13
32
  this.boundTools = tools.map(t => (0, function_calling_1.convertToOpenAITool)(t));
@@ -20,14 +39,15 @@ class AgentGraphModel extends chat_models_1.BaseChatModel {
20
39
  * @param onChunk 每收到一段文本时的回调
21
40
  * @returns 完整的响应结果
22
41
  */
23
- async callApiStream(prompt, onChunk) {
24
- const result = await this.callApi(prompt);
42
+ async callApiStream(prompt, lastMsg, onChunk) {
43
+ const result = await this.callApi(prompt, lastMsg);
25
44
  onChunk(result.text);
26
45
  return result;
27
46
  }
28
47
  async _generate(messages) {
29
48
  const fullPrompt = this.serializeMessages(messages);
30
- const response = await this.callApi(fullPrompt);
49
+ const lastMsg = messages[messages.length - 1];
50
+ const response = await this.callApi(fullPrompt, lastMsg);
31
51
  let { text, reasoning } = response;
32
52
  // 1. 处理思考内容
33
53
  if (!reasoning && text.includes('<think>')) {
@@ -111,7 +131,8 @@ class AgentGraphModel extends chat_models_1.BaseChatModel {
111
131
  */
112
132
  async streamGenerate(messages, onChunk) {
113
133
  const fullPrompt = this.serializeMessages(messages);
114
- const response = await this.callApiStream(fullPrompt, onChunk);
134
+ const lastMsg = messages[messages.length - 1];
135
+ const response = await this.callApiStream(fullPrompt, lastMsg, onChunk);
115
136
  let { text, reasoning } = response;
116
137
  // 1. 处理思考内容
117
138
  if (!reasoning && text.includes('<think>')) {
@@ -152,8 +173,9 @@ class AgentGraphModel extends chat_models_1.BaseChatModel {
152
173
  const content = typeof m.content === 'string' ? m.content : JSON.stringify(m.content, null, 2);
153
174
  return `${m._getType().toUpperCase()}: ${content}`;
154
175
  };
155
- const toolsContext = this.boundTools
156
- ? `\n[Tools]\n${JSON.stringify(this.boundTools.map(cleanToolDefinition_1.cleanToolDefinition), null, 2)}`
176
+ const tools = this.getMcpEnabled() ? (0, kit_1.getArray)(this.boundTools) : (0, kit_1.getArray)(this.boundTools).filter(tool => !this.isMcpTool(tool));
177
+ const toolsContext = tools.length
178
+ ? `\n[Tools]\n${JSON.stringify(tools.map(cleanToolDefinition_1.cleanToolDefinition), null, 2)}`
157
179
  : '';
158
180
  return `
159
181
  ${format(systemMsg)}
@@ -0,0 +1 @@
1
+ export declare const getArray: <T = any>(array: T[]) => T[];
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getArray = void 0;
4
+ const getArray = (array) => (Array.isArray(array) ? array : []);
5
+ exports.getArray = getArray;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saber2pr/ai-agent",
3
- "version": "0.0.47",
3
+ "version": "0.0.49",
4
4
  "description": "AI Assistant CLI.",
5
5
  "author": "saber2pr",
6
6
  "license": "ISC",