@hailer/mcp 0.1.17 → 0.2.1

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 (200) hide show
  1. package/dist/app.js +24 -20
  2. package/dist/core.d.ts +33 -9
  3. package/dist/core.js +279 -147
  4. package/dist/mcp/UserContextCache.js +18 -0
  5. package/dist/mcp/hailer-clients.d.ts +9 -1
  6. package/dist/mcp/hailer-clients.js +13 -3
  7. package/dist/mcp/signal-handler.js +1 -1
  8. package/dist/mcp/tool-registry.d.ts +3 -1
  9. package/dist/mcp/tool-registry.js +4 -1
  10. package/dist/mcp/tools/activity.js +43 -34
  11. package/dist/mcp/tools/bot-config/constants.d.ts +23 -0
  12. package/dist/mcp/tools/bot-config/constants.js +94 -0
  13. package/dist/mcp/tools/{bot-config.d.ts → bot-config/core.d.ts} +6 -6
  14. package/dist/mcp/tools/{bot-config.js → bot-config/core.js} +15 -15
  15. package/dist/mcp/tools/bot-config/index.d.ts +10 -0
  16. package/dist/mcp/tools/bot-config/index.js +59 -0
  17. package/dist/mcp/tools/bot-config/tools.d.ts +7 -0
  18. package/dist/mcp/tools/bot-config/tools.js +15 -0
  19. package/dist/mcp/tools/bot-config/types.d.ts +50 -0
  20. package/dist/mcp/tools/bot-config/types.js +6 -0
  21. package/dist/mcp/tools/bug-fixer-tools.d.ts +21 -0
  22. package/dist/mcp/tools/{giuseppe-tools.js → bug-fixer-tools.js} +61 -61
  23. package/dist/mcp/tools/user.js +10 -29
  24. package/dist/mcp/tools/workflow.js +36 -2
  25. package/dist/mcp/utils/data-transformers.d.ts +0 -8
  26. package/dist/mcp/utils/data-transformers.js +0 -28
  27. package/dist/mcp/utils/index.d.ts +4 -1
  28. package/dist/mcp/utils/index.js +17 -3
  29. package/dist/mcp/utils/pagination.d.ts +40 -0
  30. package/dist/mcp/utils/pagination.js +55 -0
  31. package/dist/mcp/utils/response-builder.d.ts +53 -0
  32. package/dist/mcp/utils/response-builder.js +110 -0
  33. package/dist/mcp/utils/tool-helpers.d.ts +0 -8
  34. package/dist/mcp/utils/tool-helpers.js +0 -24
  35. package/dist/mcp/utils/types.d.ts +1 -33
  36. package/dist/mcp-server.d.ts +2 -2
  37. package/dist/mcp-server.js +161 -139
  38. package/package.json +1 -1
  39. package/REFACTOR_STATUS.md +0 -127
  40. package/dist/agents/bot-manager.d.ts +0 -48
  41. package/dist/agents/bot-manager.js +0 -254
  42. package/dist/agents/factory.d.ts +0 -150
  43. package/dist/agents/factory.js +0 -650
  44. package/dist/agents/giuseppe/ai.d.ts +0 -83
  45. package/dist/agents/giuseppe/ai.js +0 -466
  46. package/dist/agents/giuseppe/bot.d.ts +0 -110
  47. package/dist/agents/giuseppe/bot.js +0 -780
  48. package/dist/agents/giuseppe/config.d.ts +0 -25
  49. package/dist/agents/giuseppe/config.js +0 -227
  50. package/dist/agents/giuseppe/files.d.ts +0 -52
  51. package/dist/agents/giuseppe/files.js +0 -338
  52. package/dist/agents/giuseppe/git.d.ts +0 -48
  53. package/dist/agents/giuseppe/git.js +0 -298
  54. package/dist/agents/giuseppe/index.d.ts +0 -97
  55. package/dist/agents/giuseppe/index.js +0 -258
  56. package/dist/agents/giuseppe/lsp.d.ts +0 -113
  57. package/dist/agents/giuseppe/lsp.js +0 -485
  58. package/dist/agents/giuseppe/monitor.d.ts +0 -118
  59. package/dist/agents/giuseppe/monitor.js +0 -621
  60. package/dist/agents/giuseppe/prompt.d.ts +0 -5
  61. package/dist/agents/giuseppe/prompt.js +0 -94
  62. package/dist/agents/giuseppe/registries/pending-classification.d.ts +0 -28
  63. package/dist/agents/giuseppe/registries/pending-classification.js +0 -50
  64. package/dist/agents/giuseppe/registries/pending-fix.d.ts +0 -30
  65. package/dist/agents/giuseppe/registries/pending-fix.js +0 -42
  66. package/dist/agents/giuseppe/registries/pending.d.ts +0 -27
  67. package/dist/agents/giuseppe/registries/pending.js +0 -49
  68. package/dist/agents/giuseppe/specialist.d.ts +0 -47
  69. package/dist/agents/giuseppe/specialist.js +0 -237
  70. package/dist/agents/giuseppe/types.d.ts +0 -123
  71. package/dist/agents/giuseppe/types.js +0 -9
  72. package/dist/agents/hailer-expert/index.d.ts +0 -8
  73. package/dist/agents/hailer-expert/index.js +0 -14
  74. package/dist/agents/hal/daemon.d.ts +0 -142
  75. package/dist/agents/hal/daemon.js +0 -1103
  76. package/dist/agents/hal/definitions.d.ts +0 -55
  77. package/dist/agents/hal/definitions.js +0 -263
  78. package/dist/agents/hal/index.d.ts +0 -3
  79. package/dist/agents/hal/index.js +0 -8
  80. package/dist/agents/index.d.ts +0 -18
  81. package/dist/agents/index.js +0 -48
  82. package/dist/agents/shared/base.d.ts +0 -216
  83. package/dist/agents/shared/base.js +0 -846
  84. package/dist/agents/shared/services/agent-registry.d.ts +0 -107
  85. package/dist/agents/shared/services/agent-registry.js +0 -629
  86. package/dist/agents/shared/services/conversation-manager.d.ts +0 -50
  87. package/dist/agents/shared/services/conversation-manager.js +0 -136
  88. package/dist/agents/shared/services/mcp-client.d.ts +0 -56
  89. package/dist/agents/shared/services/mcp-client.js +0 -124
  90. package/dist/agents/shared/services/message-classifier.d.ts +0 -37
  91. package/dist/agents/shared/services/message-classifier.js +0 -187
  92. package/dist/agents/shared/services/message-formatter.d.ts +0 -89
  93. package/dist/agents/shared/services/message-formatter.js +0 -371
  94. package/dist/agents/shared/services/session-logger.d.ts +0 -106
  95. package/dist/agents/shared/services/session-logger.js +0 -446
  96. package/dist/agents/shared/services/tool-executor.d.ts +0 -41
  97. package/dist/agents/shared/services/tool-executor.js +0 -169
  98. package/dist/agents/shared/services/workspace-schema-cache.d.ts +0 -125
  99. package/dist/agents/shared/services/workspace-schema-cache.js +0 -578
  100. package/dist/agents/shared/specialist.d.ts +0 -91
  101. package/dist/agents/shared/specialist.js +0 -399
  102. package/dist/agents/shared/tool-schema-loader.d.ts +0 -62
  103. package/dist/agents/shared/tool-schema-loader.js +0 -232
  104. package/dist/agents/shared/types.d.ts +0 -327
  105. package/dist/agents/shared/types.js +0 -121
  106. package/dist/client/agents/base.d.ts +0 -207
  107. package/dist/client/agents/base.js +0 -744
  108. package/dist/client/agents/definitions.d.ts +0 -53
  109. package/dist/client/agents/definitions.js +0 -263
  110. package/dist/client/agents/orchestrator.d.ts +0 -141
  111. package/dist/client/agents/orchestrator.js +0 -1062
  112. package/dist/client/agents/specialist.d.ts +0 -86
  113. package/dist/client/agents/specialist.js +0 -340
  114. package/dist/client/bot-entrypoint.d.ts +0 -7
  115. package/dist/client/bot-entrypoint.js +0 -103
  116. package/dist/client/bot-manager.d.ts +0 -44
  117. package/dist/client/bot-manager.js +0 -173
  118. package/dist/client/bot-runner.d.ts +0 -35
  119. package/dist/client/bot-runner.js +0 -188
  120. package/dist/client/chat-agent-daemon.d.ts +0 -464
  121. package/dist/client/chat-agent-daemon.js +0 -1774
  122. package/dist/client/daemon-factory.d.ts +0 -106
  123. package/dist/client/daemon-factory.js +0 -301
  124. package/dist/client/factory.d.ts +0 -111
  125. package/dist/client/factory.js +0 -314
  126. package/dist/client/index.d.ts +0 -17
  127. package/dist/client/index.js +0 -38
  128. package/dist/client/multi-bot-manager.d.ts +0 -42
  129. package/dist/client/multi-bot-manager.js +0 -161
  130. package/dist/client/orchestrator-daemon.d.ts +0 -87
  131. package/dist/client/orchestrator-daemon.js +0 -444
  132. package/dist/client/server.d.ts +0 -8
  133. package/dist/client/server.js +0 -251
  134. package/dist/client/services/agent-registry.d.ts +0 -108
  135. package/dist/client/services/agent-registry.js +0 -630
  136. package/dist/client/services/conversation-manager.d.ts +0 -50
  137. package/dist/client/services/conversation-manager.js +0 -136
  138. package/dist/client/services/mcp-client.d.ts +0 -48
  139. package/dist/client/services/mcp-client.js +0 -105
  140. package/dist/client/services/message-classifier.d.ts +0 -37
  141. package/dist/client/services/message-classifier.js +0 -187
  142. package/dist/client/services/message-formatter.d.ts +0 -84
  143. package/dist/client/services/message-formatter.js +0 -353
  144. package/dist/client/services/session-logger.d.ts +0 -106
  145. package/dist/client/services/session-logger.js +0 -446
  146. package/dist/client/services/tool-executor.d.ts +0 -41
  147. package/dist/client/services/tool-executor.js +0 -169
  148. package/dist/client/services/workspace-schema-cache.d.ts +0 -149
  149. package/dist/client/services/workspace-schema-cache.js +0 -732
  150. package/dist/client/specialist-daemon.d.ts +0 -77
  151. package/dist/client/specialist-daemon.js +0 -197
  152. package/dist/client/specialists.d.ts +0 -53
  153. package/dist/client/specialists.js +0 -178
  154. package/dist/client/tool-schema-loader.d.ts +0 -62
  155. package/dist/client/tool-schema-loader.js +0 -232
  156. package/dist/client/types.d.ts +0 -327
  157. package/dist/client/types.js +0 -121
  158. package/dist/commands/seed-config.d.ts +0 -9
  159. package/dist/commands/seed-config.js +0 -372
  160. package/dist/lib/context-manager.d.ts +0 -111
  161. package/dist/lib/context-manager.js +0 -431
  162. package/dist/lib/prompt-length-manager.d.ts +0 -81
  163. package/dist/lib/prompt-length-manager.js +0 -457
  164. package/dist/mcp/tools/giuseppe-tools.d.ts +0 -21
  165. package/dist/modules/bug-reports/bug-config.d.ts +0 -25
  166. package/dist/modules/bug-reports/bug-config.js +0 -187
  167. package/dist/modules/bug-reports/bug-monitor.d.ts +0 -108
  168. package/dist/modules/bug-reports/bug-monitor.js +0 -510
  169. package/dist/modules/bug-reports/giuseppe-agent.d.ts +0 -58
  170. package/dist/modules/bug-reports/giuseppe-agent.js +0 -467
  171. package/dist/modules/bug-reports/giuseppe-ai.d.ts +0 -83
  172. package/dist/modules/bug-reports/giuseppe-ai.js +0 -466
  173. package/dist/modules/bug-reports/giuseppe-bot.d.ts +0 -110
  174. package/dist/modules/bug-reports/giuseppe-bot.js +0 -804
  175. package/dist/modules/bug-reports/giuseppe-daemon.d.ts +0 -80
  176. package/dist/modules/bug-reports/giuseppe-daemon.js +0 -617
  177. package/dist/modules/bug-reports/giuseppe-files.d.ts +0 -64
  178. package/dist/modules/bug-reports/giuseppe-files.js +0 -375
  179. package/dist/modules/bug-reports/giuseppe-git.d.ts +0 -48
  180. package/dist/modules/bug-reports/giuseppe-git.js +0 -298
  181. package/dist/modules/bug-reports/giuseppe-lsp.d.ts +0 -113
  182. package/dist/modules/bug-reports/giuseppe-lsp.js +0 -485
  183. package/dist/modules/bug-reports/giuseppe-prompt.d.ts +0 -5
  184. package/dist/modules/bug-reports/giuseppe-prompt.js +0 -94
  185. package/dist/modules/bug-reports/index.d.ts +0 -77
  186. package/dist/modules/bug-reports/index.js +0 -215
  187. package/dist/modules/bug-reports/pending-classification-registry.d.ts +0 -28
  188. package/dist/modules/bug-reports/pending-classification-registry.js +0 -50
  189. package/dist/modules/bug-reports/pending-fix-registry.d.ts +0 -30
  190. package/dist/modules/bug-reports/pending-fix-registry.js +0 -42
  191. package/dist/modules/bug-reports/pending-registry.d.ts +0 -27
  192. package/dist/modules/bug-reports/pending-registry.js +0 -49
  193. package/dist/modules/bug-reports/types.d.ts +0 -123
  194. package/dist/modules/bug-reports/types.js +0 -9
  195. package/dist/routes/agents.d.ts +0 -44
  196. package/dist/routes/agents.js +0 -311
  197. package/dist/services/agent-credential-store.d.ts +0 -73
  198. package/dist/services/agent-credential-store.js +0 -212
  199. package/dist/services/bug-monitor.d.ts +0 -23
  200. package/dist/services/bug-monitor.js +0 -275
@@ -1,173 +0,0 @@
1
- "use strict";
2
- /**
3
- * Multi-Bot Client Manager
4
- * Manages multiple bot clients, each with their own Hailer connection
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.MultiBotManager = void 0;
8
- const hailer_clients_1 = require("../mcp/hailer-clients");
9
- const signal_handler_1 = require("../mcp/signal-handler");
10
- const workspace_cache_1 = require("../mcp/workspace-cache");
11
- const config_1 = require("../config");
12
- class MultiBotManager {
13
- botConfigs;
14
- botClients = new Map();
15
- constructor(botConfigs) {
16
- this.botConfigs = botConfigs;
17
- }
18
- // for client we may put initializeAllConfigClients method to do it at once
19
- // in case we only use MCP Server:
20
- // for MCP Server it may just get a specific client and this class initialize it specifically and even kill it later if not needed
21
- async initializeAllHailerClientsFromConfig() {
22
- console.log(`🤖 Initializing ${this.botConfigs.length} bot clients...`);
23
- const appConfig = (0, config_1.createApplicationConfig)();
24
- for (const config of this.botConfigs) {
25
- try {
26
- // Use shared connection pool via API key lookup
27
- const client = await (0, hailer_clients_1.createHailerClientByApiKey)(config.mcpServerApiKey);
28
- // Get user ID automatically from authenticated session
29
- const userId = await (0, hailer_clients_1.getCurrentUserId)(client);
30
- // Fetch init data to get workspace cache with user names and teams
31
- let workspaceCache;
32
- try {
33
- const init = await client.socket.request('v2.core.init', [['users', 'network', 'networks', 'teams']]);
34
- workspaceCache = (0, workspace_cache_1.createWorkspaceCache)(init, appConfig.mcpConfig);
35
- const teamCount = Object.keys(init.teams || {}).length;
36
- console.log(`📋 Loaded workspace cache with ${workspaceCache.users.length} users, ${teamCount} teams`);
37
- }
38
- catch (initError) {
39
- console.warn(`⚠️ Could not load workspace cache for ${config.email}:`, initError);
40
- }
41
- const signalHandler = new signal_handler_1.SignalHandler(client, workspaceCache);
42
- // Look up user's name from workspace cache
43
- let firstName = 'Bot';
44
- let lastName = '';
45
- if (workspaceCache) {
46
- const userInfo = workspaceCache.usersById[userId];
47
- if (userInfo) {
48
- firstName = userInfo.firstname || 'Bot';
49
- lastName = userInfo.lastname || '';
50
- }
51
- }
52
- const botClient = {
53
- userId,
54
- firstName,
55
- lastName,
56
- config,
57
- client,
58
- signalHandler,
59
- workspaceCache,
60
- };
61
- this.botClients.set(userId, botClient);
62
- console.log(`✅ Bot client initialized for ${config.email} (${userId}) via API key ${config.mcpServerApiKey.substring(0, 8)}...`);
63
- }
64
- catch (error) {
65
- console.error(`❌ Failed to initialize bot client for ${config.email}:`, error);
66
- // Continue with other bots even if one fails
67
- }
68
- }
69
- console.log(`✅ Initialized ${this.botClients.size} bot clients successfully`);
70
- }
71
- getBotClient(userId) {
72
- const botClient = this.botClients.get(userId);
73
- if (!botClient) {
74
- console.warn(`❌ No bot client found for userId: ${userId}`);
75
- }
76
- return botClient;
77
- }
78
- getAllBotClients() {
79
- return Array.from(this.botClients.values());
80
- }
81
- getBotIds() {
82
- return Array.from(this.botClients.keys());
83
- }
84
- // Simple in-memory cache for user lookups (refreshed on demand)
85
- userNameCache = new Map();
86
- static USER_CACHE_TTL = 5 * 60 * 1000; // 5 minutes
87
- /**
88
- * Look up a user's full name by their user ID
89
- * Uses workspace cache first, then falls back to socket API call
90
- */
91
- getUserName(userId) {
92
- // Check in-memory cache first (for API-fetched users)
93
- const cached = this.userNameCache.get(userId);
94
- if (cached && Date.now() - cached.fetchedAt < MultiBotManager.USER_CACHE_TTL) {
95
- return cached.name;
96
- }
97
- // Try each bot client's workspace cache until we find the user
98
- for (const botClient of this.botClients.values()) {
99
- if (botClient.workspaceCache) {
100
- const user = (0, workspace_cache_1.getUserById)(botClient.workspaceCache, userId);
101
- if (user) {
102
- const fullName = `${user.firstname || ''} ${user.lastname || ''}`.trim();
103
- return fullName || user.fullName || undefined;
104
- }
105
- }
106
- }
107
- return undefined;
108
- }
109
- /**
110
- * Look up a user's full name by their user ID (async version)
111
- * Falls back to socket API if not in cache
112
- */
113
- async getUserNameAsync(userId) {
114
- // Try sync lookup first
115
- const syncResult = this.getUserName(userId);
116
- if (syncResult) {
117
- return syncResult;
118
- }
119
- // Fall back to socket API call
120
- const botClient = this.botClients.values().next().value;
121
- if (!botClient) {
122
- return undefined;
123
- }
124
- try {
125
- // Fetch fresh init data with users
126
- const init = await botClient.client.socket.request('v2.core.init', [['users']]);
127
- if (init.users && init.users[userId]) {
128
- const user = init.users[userId];
129
- const fullName = `${user.firstname || ''} ${user.lastname || ''}`.trim();
130
- // Cache the result
131
- if (fullName) {
132
- this.userNameCache.set(userId, { name: fullName, fetchedAt: Date.now() });
133
- return fullName;
134
- }
135
- }
136
- }
137
- catch (error) {
138
- console.warn(`⚠️ Could not fetch user info for ${userId}:`, error);
139
- }
140
- return undefined;
141
- }
142
- /**
143
- * Get user info by ID (returns full UserInfo object)
144
- */
145
- getUserInfo(userId) {
146
- for (const botClient of this.botClients.values()) {
147
- if (botClient.workspaceCache) {
148
- const user = (0, workspace_cache_1.getUserById)(botClient.workspaceCache, userId);
149
- if (user) {
150
- return user;
151
- }
152
- }
153
- }
154
- return undefined;
155
- }
156
- async shutdown() {
157
- console.log("🤖 Shutting down all bot clients...");
158
- for (const [userId, botClient] of this.botClients) {
159
- try {
160
- // Properly disconnect from shared connection pool
161
- (0, hailer_clients_1.disconnectHailerClientByApiKey)(botClient.config.mcpServerApiKey);
162
- console.log(`✅ Bot client ${userId} shut down (API key: ${botClient.config.mcpServerApiKey.substring(0, 8)}...)`);
163
- }
164
- catch (error) {
165
- console.error(`❌ Error shutting down bot client ${userId}:`, error);
166
- }
167
- }
168
- this.botClients.clear();
169
- console.log("✅ All bot clients shut down");
170
- }
171
- }
172
- exports.MultiBotManager = MultiBotManager;
173
- //# sourceMappingURL=bot-manager.js.map
@@ -1,35 +0,0 @@
1
- /**
2
- * Bot Runner - Manages isolated bot processes per workspace
3
- *
4
- * Uses API key authentication (resolved to email/password internally via CLIENT_CONFIGS)
5
- */
6
- import type { WorkspaceConfig } from './types';
7
- export declare class BotRunner {
8
- private runningBots;
9
- private botEntryPoint;
10
- private maxRestarts;
11
- private restartDelayMs;
12
- constructor();
13
- private getKey;
14
- /**
15
- * Start a bot for a workspace
16
- */
17
- startBot(workspace: WorkspaceConfig, botId: string): Promise<boolean>;
18
- /**
19
- * Stop a bot for a workspace
20
- */
21
- stopBot(workspaceId: string, botId: string): Promise<void>;
22
- /**
23
- * Check if a bot is running
24
- */
25
- isRunning(workspaceId: string, botId: string): boolean;
26
- /**
27
- * Get all running bots for a workspace
28
- */
29
- getRunningBots(workspaceId: string): string[];
30
- /**
31
- * Stop all bots
32
- */
33
- stopAll(): Promise<void>;
34
- }
35
- //# sourceMappingURL=bot-runner.d.ts.map
@@ -1,188 +0,0 @@
1
- "use strict";
2
- /**
3
- * Bot Runner - Manages isolated bot processes per workspace
4
- *
5
- * Uses API key authentication (resolved to email/password internally via CLIENT_CONFIGS)
6
- */
7
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
- if (k2 === undefined) k2 = k;
9
- var desc = Object.getOwnPropertyDescriptor(m, k);
10
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
- desc = { enumerable: true, get: function() { return m[k]; } };
12
- }
13
- Object.defineProperty(o, k2, desc);
14
- }) : (function(o, m, k, k2) {
15
- if (k2 === undefined) k2 = k;
16
- o[k2] = m[k];
17
- }));
18
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
19
- Object.defineProperty(o, "default", { enumerable: true, value: v });
20
- }) : function(o, v) {
21
- o["default"] = v;
22
- });
23
- var __importStar = (this && this.__importStar) || (function () {
24
- var ownKeys = function(o) {
25
- ownKeys = Object.getOwnPropertyNames || function (o) {
26
- var ar = [];
27
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
28
- return ar;
29
- };
30
- return ownKeys(o);
31
- };
32
- return function (mod) {
33
- if (mod && mod.__esModule) return mod;
34
- var result = {};
35
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
36
- __setModuleDefault(result, mod);
37
- return result;
38
- };
39
- })();
40
- Object.defineProperty(exports, "__esModule", { value: true });
41
- exports.BotRunner = void 0;
42
- const child_process_1 = require("child_process");
43
- const path = __importStar(require("path"));
44
- const logger_1 = require("../lib/logger");
45
- const logger = (0, logger_1.createLogger)({ component: 'bot-runner' });
46
- class BotRunner {
47
- runningBots = new Map();
48
- botEntryPoint;
49
- maxRestarts = 5;
50
- restartDelayMs = 5000;
51
- constructor() {
52
- // In production, this would be the compiled JS
53
- this.botEntryPoint = path.join(__dirname, 'bot-entrypoint.js');
54
- }
55
- getKey(workspaceId, botId) {
56
- return `${workspaceId}:${botId}`;
57
- }
58
- /**
59
- * Start a bot for a workspace
60
- */
61
- async startBot(workspace, botId) {
62
- const key = this.getKey(workspace.id, botId);
63
- if (this.runningBots.has(key)) {
64
- logger.warn('Bot already running', { workspaceId: workspace.id, botId });
65
- return true;
66
- }
67
- logger.info('Starting bot', { workspaceId: workspace.id, botId });
68
- try {
69
- const child = (0, child_process_1.fork)(this.botEntryPoint, [], {
70
- env: {
71
- ...process.env,
72
- BOT_WORKSPACE_ID: workspace.id,
73
- BOT_WORKSPACE_NAME: workspace.name,
74
- BOT_API_KEY: workspace.apiKey, // Use API key instead of email/password
75
- BOT_ID: botId,
76
- // Isolate per workspace
77
- NODE_UNIQUE_ID: workspace.id
78
- },
79
- stdio: ['pipe', 'pipe', 'pipe', 'ipc']
80
- });
81
- const running = {
82
- workspaceId: workspace.id,
83
- botId,
84
- process: child,
85
- startedAt: new Date(),
86
- restartCount: 0
87
- };
88
- this.runningBots.set(key, running);
89
- // Log output
90
- child.stdout?.on('data', (data) => {
91
- logger.info(`[${workspace.id}:${botId}] ${data.toString().trim()}`);
92
- });
93
- child.stderr?.on('data', (data) => {
94
- logger.error(`[${workspace.id}:${botId}] ${data.toString().trim()}`);
95
- });
96
- // Handle exit
97
- child.on('exit', (code, signal) => {
98
- logger.warn('Bot process exited', { workspaceId: workspace.id, botId, code, signal });
99
- this.runningBots.delete(key);
100
- // Auto-restart on crash
101
- if (code !== 0 && running.restartCount < this.maxRestarts) {
102
- running.restartCount++;
103
- setTimeout(() => {
104
- this.startBot(workspace, botId);
105
- }, this.restartDelayMs);
106
- }
107
- });
108
- // Wait for ready signal
109
- return new Promise((resolve) => {
110
- const timeout = setTimeout(() => {
111
- logger.warn('Bot startup timeout', { workspaceId: workspace.id, botId });
112
- resolve(false);
113
- }, 30000);
114
- child.on('message', (msg) => {
115
- if (msg.type === 'ready') {
116
- clearTimeout(timeout);
117
- logger.info('Bot ready', { workspaceId: workspace.id, botId });
118
- resolve(true);
119
- }
120
- else if (msg.type === 'error') {
121
- clearTimeout(timeout);
122
- logger.error('Bot startup error', { workspaceId: workspace.id, botId, error: msg.error });
123
- resolve(false);
124
- }
125
- });
126
- });
127
- }
128
- catch (error) {
129
- logger.error('Failed to start bot', { workspaceId: workspace.id, botId, error });
130
- return false;
131
- }
132
- }
133
- /**
134
- * Stop a bot for a workspace
135
- */
136
- async stopBot(workspaceId, botId) {
137
- const key = this.getKey(workspaceId, botId);
138
- const running = this.runningBots.get(key);
139
- if (!running) {
140
- logger.warn('Bot not running', { workspaceId, botId });
141
- return;
142
- }
143
- logger.info('Stopping bot', { workspaceId, botId });
144
- // Prevent auto-restart
145
- running.restartCount = this.maxRestarts;
146
- // Send graceful shutdown
147
- running.process.send({ type: 'shutdown' });
148
- // Wait for exit or force kill
149
- await new Promise((resolve) => {
150
- const timeout = setTimeout(() => {
151
- running.process.kill('SIGKILL');
152
- resolve();
153
- }, 10000);
154
- running.process.once('exit', () => {
155
- clearTimeout(timeout);
156
- resolve();
157
- });
158
- });
159
- this.runningBots.delete(key);
160
- }
161
- /**
162
- * Check if a bot is running
163
- */
164
- isRunning(workspaceId, botId) {
165
- return this.runningBots.has(this.getKey(workspaceId, botId));
166
- }
167
- /**
168
- * Get all running bots for a workspace
169
- */
170
- getRunningBots(workspaceId) {
171
- const running = [];
172
- for (const [, bot] of this.runningBots) {
173
- if (bot.workspaceId === workspaceId) {
174
- running.push(bot.botId);
175
- }
176
- }
177
- return running;
178
- }
179
- /**
180
- * Stop all bots
181
- */
182
- async stopAll() {
183
- const stops = Array.from(this.runningBots.values()).map(bot => this.stopBot(bot.workspaceId, bot.botId));
184
- await Promise.all(stops);
185
- }
186
- }
187
- exports.BotRunner = BotRunner;
188
- //# sourceMappingURL=bot-runner.js.map