@vectorx/agent-simulator 0.1.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.
Files changed (67) hide show
  1. package/README.md +76 -0
  2. package/bin/agent.js +55 -0
  3. package/lib/index.js +5 -0
  4. package/lib/simulator.js +224 -0
  5. package/lib/types.js +2 -0
  6. package/lib/utils.js +7 -0
  7. package/lib/webview-ui/Agent.js +16 -0
  8. package/lib/webview-ui/App.js +359 -0
  9. package/lib/webview-ui/ErrorBoundary.js +57 -0
  10. package/lib/webview-ui/components/KnowledgeBaseSearchBlock.js +62 -0
  11. package/lib/webview-ui/components/KnowledgeBaseSearchResult.js +37 -0
  12. package/lib/webview-ui/components/MarkdownRenderer.js +27 -0
  13. package/lib/webview-ui/components/MessageRenderer.js +36 -0
  14. package/lib/webview-ui/components/ThinkingBlock.js +105 -0
  15. package/lib/webview-ui/components/ToolUseBlock.js +20 -0
  16. package/lib/webview-ui/components/ToolUseBlockHeader.js +48 -0
  17. package/lib/webview-ui/components/index.js +23 -0
  18. package/lib/webview-ui/constants.js +24 -0
  19. package/lib/webview-ui/contexts/ThemeContext.js +50 -0
  20. package/lib/webview-ui/hooks/useAgentStatus.js +96 -0
  21. package/lib/webview-ui/index.js +17 -0
  22. package/lib/webview-ui/services/agent-service.js +33 -0
  23. package/lib/webview-ui/services/logger-service.js +51 -0
  24. package/lib/webview-ui/styles/copilot.js +118 -0
  25. package/lib/webview-ui/styles/workarea.js +72 -0
  26. package/lib/webview-ui/types/index.js +2 -0
  27. package/lib/webview-ui/types.js +2 -0
  28. package/lib/webview-ui/widgets/BubbleComponents.js +114 -0
  29. package/lib/webview-ui/widgets/LogPanel.js +66 -0
  30. package/lib/webview-ui/widgets/SystemLogs.js +404 -0
  31. package/lib/webview-ui/widgets/ToolBar.js +109 -0
  32. package/lib/webview-ui/widgets/index.js +20 -0
  33. package/package.json +79 -0
  34. package/public/assets/index-j_rEEDWU.js +93 -0
  35. package/public/assets/main.js +580 -0
  36. package/public/assets/style.css +1 -0
  37. package/public/index.html +15 -0
  38. package/types/index.d.ts +3 -0
  39. package/types/simulator.d.ts +15 -0
  40. package/types/types.d.ts +6 -0
  41. package/types/utils.d.ts +1 -0
  42. package/types/webview-ui/Agent.d.ts +3 -0
  43. package/types/webview-ui/App.d.ts +3 -0
  44. package/types/webview-ui/ErrorBoundary.d.ts +18 -0
  45. package/types/webview-ui/components/KnowledgeBaseSearchBlock.d.ts +19 -0
  46. package/types/webview-ui/components/KnowledgeBaseSearchResult.d.ts +8 -0
  47. package/types/webview-ui/components/MarkdownRenderer.d.ts +6 -0
  48. package/types/webview-ui/components/MessageRenderer.d.ts +6 -0
  49. package/types/webview-ui/components/ThinkingBlock.d.ts +7 -0
  50. package/types/webview-ui/components/ToolUseBlock.d.ts +9 -0
  51. package/types/webview-ui/components/ToolUseBlockHeader.d.ts +9 -0
  52. package/types/webview-ui/components/index.d.ts +7 -0
  53. package/types/webview-ui/constants.d.ts +17 -0
  54. package/types/webview-ui/contexts/ThemeContext.d.ts +11 -0
  55. package/types/webview-ui/hooks/useAgentStatus.d.ts +11 -0
  56. package/types/webview-ui/index.d.ts +2 -0
  57. package/types/webview-ui/services/agent-service.d.ts +27 -0
  58. package/types/webview-ui/services/logger-service.d.ts +29 -0
  59. package/types/webview-ui/styles/copilot.d.ts +13 -0
  60. package/types/webview-ui/styles/workarea.d.ts +10 -0
  61. package/types/webview-ui/types/index.d.ts +20 -0
  62. package/types/webview-ui/types.d.ts +48 -0
  63. package/types/webview-ui/widgets/BubbleComponents.d.ts +12 -0
  64. package/types/webview-ui/widgets/LogPanel.d.ts +14 -0
  65. package/types/webview-ui/widgets/SystemLogs.d.ts +23 -0
  66. package/types/webview-ui/widgets/ToolBar.d.ts +13 -0
  67. package/types/webview-ui/widgets/index.d.ts +4 -0
package/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # VectorX Agent Simulator
2
+
3
+ VectorX Agent模拟器是一个开发工具,用于帮助开发和调试AI代理应用。它提供了一个简单的界面和调试环境,允许开发者在本地模拟API请求和响应。
4
+
5
+ ## 特性
6
+
7
+ - 提供Web界面用于实时查看模拟器状态
8
+ - 使用标准 Fetch API 直接连接Agent服务
9
+ - 支持流式响应和普通 JSON 响应
10
+ - 提供命令行工具,便于快速启动和配置
11
+
12
+ ## 安装
13
+
14
+ ```bash
15
+ # 在项目根目录安装依赖
16
+ npm install
17
+
18
+ # 构建模拟器
19
+ cd packages/vectorx-agent-simulator
20
+ npm run build
21
+ ```
22
+
23
+ ## 实现方式
24
+
25
+ VectorX Agent Simulator 使用标准的 Fetch API 直接连接Agent服务。
26
+
27
+ 这种实现方式具有以下优势:
28
+ - 使用标准 Web API,无需额外依赖
29
+ - 直接与Agent服务通信,无需模拟层
30
+ - 支持流式响应,实时更新内容
31
+ - 便于理解和扩展
32
+
33
+ ## 使用方法
34
+
35
+ ### 命令行
36
+
37
+ 使用以下命令启动模拟器:
38
+
39
+ ```bash
40
+ agent -p 3001 --agent-server-url http://localhost:3000/v1/aiagent/agents/123
41
+ ```
42
+
43
+ #### 命令行参数
44
+
45
+ - `-p, --port <port>`: 模拟器HTTP服务端口 (默认: 3001)
46
+ - `--agent-server-url <agentServerUrl>`: **[必填]** Agent服务器完整URL地址,例如: http://localhost:3000/v1/aiagent/agents/123
47
+ - `-o, --open`: 启动后自动打开浏览器 (默认: true)
48
+
49
+ #### 示例
50
+
51
+ ```bash
52
+ # 基本使用
53
+ agent --agent-server-url http://localhost:3000/v1/aiagent/agents/my-agent
54
+
55
+ # 指定端口
56
+ agent -p 8080 --agent-server-url http://localhost:3000/v1/aiagent/agents/my-agent
57
+
58
+ # 不自动打开浏览器
59
+ agent --agent-server-url http://localhost:3000/v1/aiagent/agents/my-agent --no-open
60
+ ```
61
+
62
+ ## 开发
63
+
64
+ 要在开发模式下运行模拟器:
65
+
66
+ ```bash
67
+ # 监视文件变化并重新构建
68
+ npm run dev
69
+
70
+ # 在另一个终端启动模拟器
71
+ node bin/agent.js --agent-server-url http://localhost:3000/v1/aiagent/agents/demo-agent
72
+ ```
73
+
74
+ ## 许可证
75
+
76
+ ISC
package/bin/agent.js ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { AgentSimulator } = require('../lib/simulator');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+ const { Command } = require('commander');
7
+
8
+ // 获取版本信息
9
+ const packageJsonPath = path.resolve(__dirname, '../package.json');
10
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
11
+
12
+ // 创建命令行解析器
13
+ const program = new Command();
14
+
15
+ program
16
+ .name('agent')
17
+ .description('VectorX Agent模拟器 - 用于开发和调试AI代理应用')
18
+ .version(packageJson.version);
19
+
20
+ program
21
+ .option('-p, --port <port>', '模拟器服务端口', '3001')
22
+ .requiredOption('--agent-server-url <agentServerUrl>', 'Agent服务器完整URL地址,例如: http://localhost:3000/v1/aiagent/agents/123')
23
+ .option('-o, --open', '启动后自动打开浏览器', true)
24
+ .action(async (options) => {
25
+ try {
26
+ // 参数处理
27
+ const simulatorOptions = {
28
+ port: parseInt(options.port, 10),
29
+ agentServerUrl: options.agentServerUrl,
30
+ open: options.open === true || options.open === 'true'
31
+ };
32
+
33
+ // 创建并启动模拟器
34
+ const simulator = new AgentSimulator(simulatorOptions);
35
+
36
+ console.log('\x1b[34m正在启动VectorX Agent模拟器...\x1b[0m');
37
+
38
+ // 处理进程结束信号
39
+ process.on('SIGINT', async () => {
40
+ console.log('\n' + '\x1b[33m接收到终止信号,正在关闭模拟器...\x1b[0m');
41
+ await simulator.stop();
42
+ process.exit(0);
43
+ });
44
+
45
+ // 启动模拟器
46
+ await simulator.start();
47
+
48
+ } catch (error) {
49
+ console.error('\x1b[31m模拟器启动失败:\x1b[0m', error);
50
+ process.exit(1);
51
+ }
52
+ });
53
+
54
+ // 启动CLI
55
+ program.parse(process.argv);
package/lib/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AgentSimulator = void 0;
4
+ const simulator_1 = require("./simulator");
5
+ Object.defineProperty(exports, "AgentSimulator", { enumerable: true, get: function () { return simulator_1.AgentSimulator; } });
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AgentSimulator = void 0;
16
+ const path_1 = __importDefault(require("path"));
17
+ const chalk_1 = __importDefault(require("chalk"));
18
+ const cors_1 = __importDefault(require("cors"));
19
+ const express_1 = __importDefault(require("express"));
20
+ const http_proxy_middleware_1 = require("http-proxy-middleware");
21
+ const node_fetch_1 = __importDefault(require("node-fetch"));
22
+ function parseAgentServerUrl(url) {
23
+ try {
24
+ const urlObj = new URL(url);
25
+ return {
26
+ protocol: urlObj.protocol,
27
+ hostname: urlObj.hostname,
28
+ port: urlObj.port || (urlObj.protocol === "https:" ? "443" : "80"),
29
+ pathname: urlObj.pathname,
30
+ baseUrl: `${urlObj.protocol}//${urlObj.hostname}${urlObj.port ? ":" + urlObj.port : ""}`,
31
+ fullUrl: url,
32
+ };
33
+ }
34
+ catch (error) {
35
+ throw new Error(`无效的 Agent Server URL: ${url}`);
36
+ }
37
+ }
38
+ class AgentSimulator {
39
+ constructor(options) {
40
+ this.server = null;
41
+ this.agentStatus = "normal";
42
+ this.options = Object.assign({ open: true }, options);
43
+ this.parsedServerUrl = parseAgentServerUrl(options.agentServerUrl);
44
+ this.app = (0, express_1.default)();
45
+ }
46
+ start() {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ this.app.use((0, cors_1.default)());
49
+ this.app.use(express_1.default.json());
50
+ const publicPath = path_1.default.resolve(__dirname, "../public");
51
+ this.app.use(express_1.default.static(publicPath));
52
+ this.setupProxy();
53
+ this.setupRoutes();
54
+ return new Promise((resolve) => {
55
+ this.server = this.app.listen(this.options.port, () => {
56
+ console.log(chalk_1.default.green(`
57
+ ┌────────────────────────────────────────────────────┐
58
+ │ │
59
+ │ 🚀 Agent模拟器已启动! │
60
+ │ │
61
+ │ 📡 模拟器地址: ${chalk_1.default.blue(`http://localhost:${this.options.port}`)} │
62
+ │ 🔄 Agent服务: ${chalk_1.default.blue(this.options.agentServerUrl)} │
63
+ │ │
64
+ └────────────────────────────────────────────────────┘
65
+ `));
66
+ if (this.options.open) {
67
+ console.log(chalk_1.default.yellow(`请手动在浏览器中打开: ${chalk_1.default.blue(`http://localhost:${this.options.port}`)}`));
68
+ }
69
+ resolve();
70
+ });
71
+ });
72
+ });
73
+ }
74
+ stop() {
75
+ return new Promise((resolve, reject) => {
76
+ if (this.server) {
77
+ this.server.close((err) => {
78
+ if (err) {
79
+ return reject(err);
80
+ }
81
+ console.log(chalk_1.default.yellow("模拟器已停止"));
82
+ resolve();
83
+ });
84
+ this.server.closeAllConnections();
85
+ setTimeout(() => {
86
+ resolve();
87
+ }, 2000);
88
+ }
89
+ else {
90
+ resolve();
91
+ }
92
+ });
93
+ }
94
+ setupProxy() {
95
+ const proxyOptions = {
96
+ target: this.parsedServerUrl.baseUrl,
97
+ changeOrigin: true,
98
+ pathRewrite: {
99
+ "^/api": "",
100
+ },
101
+ logLevel: "silent",
102
+ };
103
+ this.app.use("/api", (req, res, next) => {
104
+ if (req.path === "/sse" || req.path === "/chat" || req.path === "/control" || req.path === "/restart") {
105
+ return next();
106
+ }
107
+ return (0, http_proxy_middleware_1.createProxyMiddleware)(proxyOptions)(req, res, next);
108
+ });
109
+ }
110
+ setupRoutes() {
111
+ this.app.get("/agentInfo/get", (req, res) => __awaiter(this, void 0, void 0, function* () {
112
+ try {
113
+ const agentSimulatorInfo = yield this.getAgentSimulatorInfo();
114
+ res.json(agentSimulatorInfo);
115
+ }
116
+ catch (error) {
117
+ console.error("获取Agent信息失败:", error);
118
+ res.status(500).json({
119
+ error: error instanceof Error ? error.message : "获取Agent信息失败",
120
+ });
121
+ }
122
+ }));
123
+ this.app.get("/agentInfo/status", (req, res) => __awaiter(this, void 0, void 0, function* () {
124
+ try {
125
+ const healthUrl = `${this.parsedServerUrl.fullUrl}/health`;
126
+ const response = yield (0, node_fetch_1.default)(healthUrl);
127
+ if (response.ok) {
128
+ const healthData = yield response.json();
129
+ this.agentStatus = "normal";
130
+ res.json({
131
+ status: "normal",
132
+ health: healthData,
133
+ timestamp: Date.now(),
134
+ agentServerUrl: this.options.agentServerUrl,
135
+ parsedUrl: {
136
+ hostname: this.parsedServerUrl.hostname,
137
+ port: this.parsedServerUrl.port,
138
+ baseUrl: this.parsedServerUrl.baseUrl,
139
+ },
140
+ });
141
+ }
142
+ else {
143
+ this.agentStatus = "error";
144
+ res.json({
145
+ status: "error",
146
+ message: `后端服务响应异常: ${response.status}`,
147
+ timestamp: Date.now(),
148
+ agentServerUrl: this.options.agentServerUrl,
149
+ parsedUrl: {
150
+ hostname: this.parsedServerUrl.hostname,
151
+ port: this.parsedServerUrl.port,
152
+ baseUrl: this.parsedServerUrl.baseUrl,
153
+ },
154
+ });
155
+ }
156
+ }
157
+ catch (error) {
158
+ this.agentStatus = "error";
159
+ res.json({
160
+ status: "error",
161
+ message: `无法连接到后端服务: ${error.message}`,
162
+ timestamp: Date.now(),
163
+ agentServerUrl: this.options.agentServerUrl,
164
+ parsedUrl: {
165
+ hostname: this.parsedServerUrl.hostname,
166
+ port: this.parsedServerUrl.port,
167
+ baseUrl: this.parsedServerUrl.baseUrl,
168
+ },
169
+ });
170
+ }
171
+ }));
172
+ this.app.get("/simulator/status", (req, res) => {
173
+ res.json({
174
+ status: "running",
175
+ port: this.options.port,
176
+ agentServerUrl: this.options.agentServerUrl,
177
+ agentStatus: this.agentStatus,
178
+ parsedUrl: {
179
+ hostname: this.parsedServerUrl.hostname,
180
+ port: this.parsedServerUrl.port,
181
+ baseUrl: this.parsedServerUrl.baseUrl,
182
+ },
183
+ });
184
+ });
185
+ this.app.get("*", (req, res, next) => {
186
+ if (req.path.startsWith("/api") || req.path.startsWith("/simulator")) {
187
+ return next();
188
+ }
189
+ res.sendFile(path_1.default.resolve(__dirname, "../public/index.html"));
190
+ });
191
+ this.app.use((req, res) => {
192
+ res.status(404).json({ error: "未找到请求路径" });
193
+ });
194
+ }
195
+ getAgentSimulatorInfo() {
196
+ return __awaiter(this, void 0, void 0, function* () {
197
+ const healthUrl = `${this.parsedServerUrl.fullUrl}/info`;
198
+ const response = yield (0, node_fetch_1.default)(healthUrl);
199
+ if (!response.ok) {
200
+ throw new Error(`无法连接到后端服务: ${response.status} ${response.statusText}`);
201
+ }
202
+ const resData = yield response.json();
203
+ if (resData.code !== 0) {
204
+ throw new Error(`后端服务返回错误: ${resData.message || "未知错误"}`);
205
+ }
206
+ const { agent_info } = resData.data;
207
+ if (!agent_info) {
208
+ throw new Error("后端服务返回的数据中缺少 agent_info 字段");
209
+ }
210
+ return this.transformToAgentSimulatorInfo(agent_info);
211
+ });
212
+ }
213
+ transformToAgentSimulatorInfo(agentInfo) {
214
+ return {
215
+ agentInfo: {
216
+ agent_icon: agentInfo.agent_icon,
217
+ agent_id: agentInfo.agent_id,
218
+ agent_name: agentInfo.agent_name,
219
+ },
220
+ agentServerUrl: this.options.agentServerUrl,
221
+ };
222
+ }
223
+ }
224
+ exports.AgentSimulator = AgentSimulator;
package/lib/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/utils.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateId = void 0;
4
+ const generateId = () => {
5
+ return Date.now() + "-" + Math.random().toString(36).substr(2, 9);
6
+ };
7
+ exports.generateId = generateId;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSendMessageUri = getSendMessageUri;
4
+ exports.getHealthUri = getHealthUri;
5
+ function getSendMessageUri(agentSimulatorInfo) {
6
+ if (!agentSimulatorInfo) {
7
+ throw new Error("agentSimulatorInfo is not set");
8
+ }
9
+ return `${agentSimulatorInfo.agentServerUrl}/send-message`;
10
+ }
11
+ function getHealthUri(agentSimulatorInfo) {
12
+ if (!agentSimulatorInfo) {
13
+ throw new Error("agentSimulatorInfo is not set");
14
+ }
15
+ return `${agentSimulatorInfo.agentServerUrl}/health`;
16
+ }