@atlisp/agent 0.1.9 → 0.1.10
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/agent.js +86 -2
- package/config.js +1 -1
- package/package.json +1 -1
package/agent.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createProvider } from './providers/index.js';
|
|
|
2
2
|
import { createMcpClient, connectMcp } from './mcp/index.js';
|
|
3
3
|
import { buildSystemMessage, buildToolsDescription } from './prompt.js';
|
|
4
4
|
import { getAgentConfig } from './config.js';
|
|
5
|
+
import { spawn } from 'child_process';
|
|
5
6
|
|
|
6
7
|
export class Agent {
|
|
7
8
|
constructor(options = {}) {
|
|
@@ -14,11 +15,94 @@ export class Agent {
|
|
|
14
15
|
this.verbose = this.config.verbose || false;
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
async startMcpService() {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
console.error('[Agent] 正在启动 npx @atlisp/mcp...');
|
|
21
|
+
|
|
22
|
+
const mcpProc = spawn('npx', ['@atlisp/mcp', '--http'], {
|
|
23
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
24
|
+
detached: true,
|
|
25
|
+
shell: true,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
let output = '';
|
|
29
|
+
mcpProc.stdout.on('data', (data) => {
|
|
30
|
+
output += data.toString();
|
|
31
|
+
});
|
|
32
|
+
mcpProc.stderr.on('data', (data) => {
|
|
33
|
+
output += data.toString();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const timeout = setTimeout(() => {
|
|
37
|
+
mcpProc.kill();
|
|
38
|
+
reject(new Error('MCP 服务启动超时'));
|
|
39
|
+
}, 30000);
|
|
40
|
+
|
|
41
|
+
const checkConnection = async () => {
|
|
42
|
+
try {
|
|
43
|
+
const tools = await this.mcp.listTools();
|
|
44
|
+
if (tools?.tools?.length > 0) {
|
|
45
|
+
clearTimeout(timeout);
|
|
46
|
+
console.error('[Agent] MCP 服务已启动');
|
|
47
|
+
resolve(true);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
} catch {}
|
|
51
|
+
|
|
52
|
+
if (output.includes('started') || output.includes('listen') || output.includes('8120')) {
|
|
53
|
+
clearTimeout(timeout);
|
|
54
|
+
console.error('[Agent] MCP 服务已启动');
|
|
55
|
+
resolve(true);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
setTimeout(checkConnection, 1000);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
setTimeout(checkConnection, 3000);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async checkMcpAvailable() {
|
|
67
|
+
try {
|
|
68
|
+
const tools = await this.mcp.listTools();
|
|
69
|
+
return tools?.tools?.length > 0;
|
|
70
|
+
} catch (e) {
|
|
71
|
+
if (this.mcp.config?.mode === 'http') {
|
|
72
|
+
console.error('[Agent] MCP HTTP 模式连接失败,请确保 MCP 服务已启动');
|
|
73
|
+
console.error(`[Agent] 尝试连接: ${this.mcp.config?.url || 'http://localhost:8110'}`);
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
17
79
|
async initialize() {
|
|
18
80
|
console.error('[Agent] 初始化...');
|
|
19
81
|
|
|
20
|
-
|
|
21
|
-
|
|
82
|
+
try {
|
|
83
|
+
await connectMcp(this.mcp);
|
|
84
|
+
console.error('[Agent] MCP 连接成功');
|
|
85
|
+
} catch (e) {
|
|
86
|
+
const isHttpMode = this.mcp.config?.mode === 'http' || !this.mcp.config?.command;
|
|
87
|
+
if (isHttpMode) {
|
|
88
|
+
console.error('[Agent] MCP HTTP 模式连接失败,尝试启动 MCP 服务...');
|
|
89
|
+
await this.startMcpService();
|
|
90
|
+
} else {
|
|
91
|
+
throw e;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const available = await this.checkMcpAvailable();
|
|
96
|
+
if (!available) {
|
|
97
|
+
console.error('\n========================================');
|
|
98
|
+
console.error('[错误] 无法连接到 MCP 服务');
|
|
99
|
+
console.error('');
|
|
100
|
+
console.error('请先运行 atlisp-mcp 服务:');
|
|
101
|
+
console.error(' - 方式1: npx atlisp-mcp');
|
|
102
|
+
console.error(' - 方式2: 在 CAD 中加载 @lisp 后执行 (@MCP)');
|
|
103
|
+
console.error('========================================\n');
|
|
104
|
+
throw new Error('MCP 服务不可用,请先启动 atlisp-mcp 服务');
|
|
105
|
+
}
|
|
22
106
|
|
|
23
107
|
this.tools = await this.mcp.listTools();
|
|
24
108
|
console.error(`[Agent] 已加载 ${this.tools?.tools?.length || 0} 个工具`);
|
package/config.js
CHANGED