@hailer/mcp 0.1.16 → 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 (202) 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/webhook-handler.d.ts +2 -2
  37. package/dist/mcp/webhook-handler.js +5 -3
  38. package/dist/mcp-server.d.ts +2 -2
  39. package/dist/mcp-server.js +167 -140
  40. package/package.json +1 -1
  41. package/REFACTOR_STATUS.md +0 -127
  42. package/dist/agents/bot-manager.d.ts +0 -48
  43. package/dist/agents/bot-manager.js +0 -254
  44. package/dist/agents/factory.d.ts +0 -150
  45. package/dist/agents/factory.js +0 -650
  46. package/dist/agents/giuseppe/ai.d.ts +0 -83
  47. package/dist/agents/giuseppe/ai.js +0 -466
  48. package/dist/agents/giuseppe/bot.d.ts +0 -110
  49. package/dist/agents/giuseppe/bot.js +0 -780
  50. package/dist/agents/giuseppe/config.d.ts +0 -25
  51. package/dist/agents/giuseppe/config.js +0 -227
  52. package/dist/agents/giuseppe/files.d.ts +0 -52
  53. package/dist/agents/giuseppe/files.js +0 -338
  54. package/dist/agents/giuseppe/git.d.ts +0 -48
  55. package/dist/agents/giuseppe/git.js +0 -298
  56. package/dist/agents/giuseppe/index.d.ts +0 -97
  57. package/dist/agents/giuseppe/index.js +0 -258
  58. package/dist/agents/giuseppe/lsp.d.ts +0 -113
  59. package/dist/agents/giuseppe/lsp.js +0 -485
  60. package/dist/agents/giuseppe/monitor.d.ts +0 -118
  61. package/dist/agents/giuseppe/monitor.js +0 -621
  62. package/dist/agents/giuseppe/prompt.d.ts +0 -5
  63. package/dist/agents/giuseppe/prompt.js +0 -94
  64. package/dist/agents/giuseppe/registries/pending-classification.d.ts +0 -28
  65. package/dist/agents/giuseppe/registries/pending-classification.js +0 -50
  66. package/dist/agents/giuseppe/registries/pending-fix.d.ts +0 -30
  67. package/dist/agents/giuseppe/registries/pending-fix.js +0 -42
  68. package/dist/agents/giuseppe/registries/pending.d.ts +0 -27
  69. package/dist/agents/giuseppe/registries/pending.js +0 -49
  70. package/dist/agents/giuseppe/specialist.d.ts +0 -47
  71. package/dist/agents/giuseppe/specialist.js +0 -237
  72. package/dist/agents/giuseppe/types.d.ts +0 -123
  73. package/dist/agents/giuseppe/types.js +0 -9
  74. package/dist/agents/hailer-expert/index.d.ts +0 -8
  75. package/dist/agents/hailer-expert/index.js +0 -14
  76. package/dist/agents/hal/daemon.d.ts +0 -142
  77. package/dist/agents/hal/daemon.js +0 -1103
  78. package/dist/agents/hal/definitions.d.ts +0 -55
  79. package/dist/agents/hal/definitions.js +0 -263
  80. package/dist/agents/hal/index.d.ts +0 -3
  81. package/dist/agents/hal/index.js +0 -8
  82. package/dist/agents/index.d.ts +0 -18
  83. package/dist/agents/index.js +0 -48
  84. package/dist/agents/shared/base.d.ts +0 -216
  85. package/dist/agents/shared/base.js +0 -846
  86. package/dist/agents/shared/services/agent-registry.d.ts +0 -107
  87. package/dist/agents/shared/services/agent-registry.js +0 -629
  88. package/dist/agents/shared/services/conversation-manager.d.ts +0 -50
  89. package/dist/agents/shared/services/conversation-manager.js +0 -136
  90. package/dist/agents/shared/services/mcp-client.d.ts +0 -56
  91. package/dist/agents/shared/services/mcp-client.js +0 -124
  92. package/dist/agents/shared/services/message-classifier.d.ts +0 -37
  93. package/dist/agents/shared/services/message-classifier.js +0 -187
  94. package/dist/agents/shared/services/message-formatter.d.ts +0 -89
  95. package/dist/agents/shared/services/message-formatter.js +0 -371
  96. package/dist/agents/shared/services/session-logger.d.ts +0 -106
  97. package/dist/agents/shared/services/session-logger.js +0 -446
  98. package/dist/agents/shared/services/tool-executor.d.ts +0 -41
  99. package/dist/agents/shared/services/tool-executor.js +0 -169
  100. package/dist/agents/shared/services/workspace-schema-cache.d.ts +0 -125
  101. package/dist/agents/shared/services/workspace-schema-cache.js +0 -578
  102. package/dist/agents/shared/specialist.d.ts +0 -91
  103. package/dist/agents/shared/specialist.js +0 -399
  104. package/dist/agents/shared/tool-schema-loader.d.ts +0 -62
  105. package/dist/agents/shared/tool-schema-loader.js +0 -232
  106. package/dist/agents/shared/types.d.ts +0 -327
  107. package/dist/agents/shared/types.js +0 -121
  108. package/dist/client/agents/base.d.ts +0 -207
  109. package/dist/client/agents/base.js +0 -744
  110. package/dist/client/agents/definitions.d.ts +0 -53
  111. package/dist/client/agents/definitions.js +0 -263
  112. package/dist/client/agents/orchestrator.d.ts +0 -141
  113. package/dist/client/agents/orchestrator.js +0 -1062
  114. package/dist/client/agents/specialist.d.ts +0 -86
  115. package/dist/client/agents/specialist.js +0 -340
  116. package/dist/client/bot-entrypoint.d.ts +0 -7
  117. package/dist/client/bot-entrypoint.js +0 -103
  118. package/dist/client/bot-manager.d.ts +0 -44
  119. package/dist/client/bot-manager.js +0 -173
  120. package/dist/client/bot-runner.d.ts +0 -35
  121. package/dist/client/bot-runner.js +0 -188
  122. package/dist/client/chat-agent-daemon.d.ts +0 -464
  123. package/dist/client/chat-agent-daemon.js +0 -1774
  124. package/dist/client/daemon-factory.d.ts +0 -106
  125. package/dist/client/daemon-factory.js +0 -301
  126. package/dist/client/factory.d.ts +0 -111
  127. package/dist/client/factory.js +0 -314
  128. package/dist/client/index.d.ts +0 -17
  129. package/dist/client/index.js +0 -38
  130. package/dist/client/multi-bot-manager.d.ts +0 -42
  131. package/dist/client/multi-bot-manager.js +0 -161
  132. package/dist/client/orchestrator-daemon.d.ts +0 -87
  133. package/dist/client/orchestrator-daemon.js +0 -444
  134. package/dist/client/server.d.ts +0 -8
  135. package/dist/client/server.js +0 -251
  136. package/dist/client/services/agent-registry.d.ts +0 -108
  137. package/dist/client/services/agent-registry.js +0 -630
  138. package/dist/client/services/conversation-manager.d.ts +0 -50
  139. package/dist/client/services/conversation-manager.js +0 -136
  140. package/dist/client/services/mcp-client.d.ts +0 -48
  141. package/dist/client/services/mcp-client.js +0 -105
  142. package/dist/client/services/message-classifier.d.ts +0 -37
  143. package/dist/client/services/message-classifier.js +0 -187
  144. package/dist/client/services/message-formatter.d.ts +0 -84
  145. package/dist/client/services/message-formatter.js +0 -353
  146. package/dist/client/services/session-logger.d.ts +0 -106
  147. package/dist/client/services/session-logger.js +0 -446
  148. package/dist/client/services/tool-executor.d.ts +0 -41
  149. package/dist/client/services/tool-executor.js +0 -169
  150. package/dist/client/services/workspace-schema-cache.d.ts +0 -149
  151. package/dist/client/services/workspace-schema-cache.js +0 -732
  152. package/dist/client/specialist-daemon.d.ts +0 -77
  153. package/dist/client/specialist-daemon.js +0 -197
  154. package/dist/client/specialists.d.ts +0 -53
  155. package/dist/client/specialists.js +0 -178
  156. package/dist/client/tool-schema-loader.d.ts +0 -62
  157. package/dist/client/tool-schema-loader.js +0 -232
  158. package/dist/client/types.d.ts +0 -327
  159. package/dist/client/types.js +0 -121
  160. package/dist/commands/seed-config.d.ts +0 -9
  161. package/dist/commands/seed-config.js +0 -372
  162. package/dist/lib/context-manager.d.ts +0 -111
  163. package/dist/lib/context-manager.js +0 -431
  164. package/dist/lib/prompt-length-manager.d.ts +0 -81
  165. package/dist/lib/prompt-length-manager.js +0 -457
  166. package/dist/mcp/tools/giuseppe-tools.d.ts +0 -21
  167. package/dist/modules/bug-reports/bug-config.d.ts +0 -25
  168. package/dist/modules/bug-reports/bug-config.js +0 -187
  169. package/dist/modules/bug-reports/bug-monitor.d.ts +0 -108
  170. package/dist/modules/bug-reports/bug-monitor.js +0 -510
  171. package/dist/modules/bug-reports/giuseppe-agent.d.ts +0 -58
  172. package/dist/modules/bug-reports/giuseppe-agent.js +0 -467
  173. package/dist/modules/bug-reports/giuseppe-ai.d.ts +0 -83
  174. package/dist/modules/bug-reports/giuseppe-ai.js +0 -466
  175. package/dist/modules/bug-reports/giuseppe-bot.d.ts +0 -110
  176. package/dist/modules/bug-reports/giuseppe-bot.js +0 -804
  177. package/dist/modules/bug-reports/giuseppe-daemon.d.ts +0 -80
  178. package/dist/modules/bug-reports/giuseppe-daemon.js +0 -617
  179. package/dist/modules/bug-reports/giuseppe-files.d.ts +0 -64
  180. package/dist/modules/bug-reports/giuseppe-files.js +0 -375
  181. package/dist/modules/bug-reports/giuseppe-git.d.ts +0 -48
  182. package/dist/modules/bug-reports/giuseppe-git.js +0 -298
  183. package/dist/modules/bug-reports/giuseppe-lsp.d.ts +0 -113
  184. package/dist/modules/bug-reports/giuseppe-lsp.js +0 -485
  185. package/dist/modules/bug-reports/giuseppe-prompt.d.ts +0 -5
  186. package/dist/modules/bug-reports/giuseppe-prompt.js +0 -94
  187. package/dist/modules/bug-reports/index.d.ts +0 -77
  188. package/dist/modules/bug-reports/index.js +0 -215
  189. package/dist/modules/bug-reports/pending-classification-registry.d.ts +0 -28
  190. package/dist/modules/bug-reports/pending-classification-registry.js +0 -50
  191. package/dist/modules/bug-reports/pending-fix-registry.d.ts +0 -30
  192. package/dist/modules/bug-reports/pending-fix-registry.js +0 -42
  193. package/dist/modules/bug-reports/pending-registry.d.ts +0 -27
  194. package/dist/modules/bug-reports/pending-registry.js +0 -49
  195. package/dist/modules/bug-reports/types.d.ts +0 -123
  196. package/dist/modules/bug-reports/types.js +0 -9
  197. package/dist/routes/agents.d.ts +0 -44
  198. package/dist/routes/agents.js +0 -311
  199. package/dist/services/agent-credential-store.d.ts +0 -73
  200. package/dist/services/agent-credential-store.js +0 -212
  201. package/dist/services/bug-monitor.d.ts +0 -23
  202. package/dist/services/bug-monitor.js +0 -275
@@ -1,630 +0,0 @@
1
- "use strict";
2
- /**
3
- * Agent Registry Service
4
- *
5
- * Handles registration of agents in Hailer workspace workflows:
6
- * - Agent Directory: Main registry of all bots
7
- * - Positions: Job descriptions/roles
8
- * - Teams: Groups of agents
9
- * - Tool Registry: MCP server configurations
10
- * - MCP Config: Per-agent MCP configuration
11
- *
12
- * WORKSPACE ISOLATION: Uses WorkspaceSchemaCacheService for dynamic ID lookup.
13
- * Never relies on hardcoded IDs - each workspace has its own workflow IDs.
14
- */
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.AgentRegistryService = void 0;
17
- class AgentRegistryService {
18
- schemaCache;
19
- logger;
20
- callMcpTool;
21
- getDefaultTeamId;
22
- agentDirectoryId = null;
23
- positionId = null;
24
- teamId = null;
25
- toolRegistryId = null;
26
- mcpConfigId = null;
27
- currentWorkspaceId = null;
28
- constructor(schemaCache, logger, callMcpTool, getDefaultTeamId) {
29
- this.schemaCache = schemaCache;
30
- this.logger = logger;
31
- this.callMcpTool = callMcpTool;
32
- this.getDefaultTeamId = getDefaultTeamId;
33
- }
34
- /**
35
- * Load registration data - always returns false to force fresh Hailer lookup
36
- * Kept for API compatibility with base.ts
37
- */
38
- async loadFromCache(_userId, _workspaceId) {
39
- // Always return false - agent should query Hailer on every startup
40
- // The findAgentByUserId() method in registerAllAgentData handles deduplication
41
- return false;
42
- }
43
- /**
44
- * Get agent directory ID
45
- */
46
- getAgentDirectoryId() {
47
- return this.agentDirectoryId;
48
- }
49
- /**
50
- * Get position ID
51
- */
52
- getPositionId() {
53
- return this.positionId;
54
- }
55
- /**
56
- * Get team ID
57
- */
58
- getTeamId() {
59
- return this.teamId;
60
- }
61
- /**
62
- * Register all agent data across all workflows
63
- * Uses schemaCache for dynamic workflow ID lookup
64
- */
65
- async registerAllAgentData(agentInfo, positionDetails, mcpServerUrl, toolIndex, workspaceId) {
66
- this.currentWorkspaceId = workspaceId;
67
- this.logger.info("Starting full agent registration", { workspaceId });
68
- // Verify schema cache has required workflows
69
- const agentDirSchema = this.schemaCache.getAgentDirectorySchema(workspaceId);
70
- if (!agentDirSchema) {
71
- this.logger.warn("Agent Directory workflow not found in workspace - skipping registration", { workspaceId });
72
- return;
73
- }
74
- try {
75
- // 1. Register in Agent Directory first (required for other links)
76
- await this.registerAgentInDirectory(agentInfo, workspaceId);
77
- // 2. Create/find shared Team for AI agents
78
- await this.registerTeam(workspaceId);
79
- // 3. Register MCP server in Tool Registry (per-agent)
80
- const agentName = `${agentInfo.firstName} ${agentInfo.lastName}`;
81
- await this.registerToolRegistry(mcpServerUrl, toolIndex, workspaceId, agentName);
82
- // 4. Create Position for this agent
83
- await this.registerPosition(positionDetails, workspaceId);
84
- // 5. Create MCP Config for this agent
85
- await this.registerMcpConfig(agentInfo, workspaceId);
86
- // 6. Update Agent Directory with Position and Team links
87
- await this.linkAgentToPositionAndTeam(workspaceId);
88
- this.logger.info("Full agent registration completed", {
89
- workspaceId,
90
- agentDirectoryId: this.agentDirectoryId,
91
- positionId: this.positionId,
92
- teamId: this.teamId,
93
- toolRegistryId: this.toolRegistryId,
94
- mcpConfigId: this.mcpConfigId,
95
- });
96
- }
97
- catch (error) {
98
- this.logger.warn("Agent registration incomplete", {
99
- error: error instanceof Error ? error.message : String(error),
100
- workspaceId,
101
- agentDirectoryId: this.agentDirectoryId,
102
- });
103
- }
104
- }
105
- /**
106
- * Register this agent in the Agent Directory
107
- * Uses dynamic schema lookup for workflow/field IDs
108
- */
109
- async registerAgentInDirectory(agentInfo, workspaceId) {
110
- const schema = this.schemaCache.getAgentDirectorySchema(workspaceId);
111
- if (!schema) {
112
- this.logger.debug("Skipping agent directory registration - no schema");
113
- return;
114
- }
115
- this.logger.info("Registering agent in directory", {
116
- email: agentInfo.email,
117
- userId: agentInfo.userId,
118
- workflowId: schema.workflowId,
119
- });
120
- try {
121
- // First, try to find existing agent by hailerProfile (userId) - most reliable
122
- const existingAgent = await this.findAgentByUserId(agentInfo.userId, schema);
123
- if (existingAgent) {
124
- this.agentDirectoryId = existingAgent._id;
125
- this.logger.info("Found existing agent in directory", {
126
- agentDirectoryId: this.agentDirectoryId,
127
- name: existingAgent.name,
128
- });
129
- return;
130
- }
131
- // Create new agent entry
132
- const wsTeamId = this.getDefaultTeamId();
133
- // Get phase ID - try "deployed agents" first, then partial match, then first available
134
- const phaseId = this.findPhaseId(schema.phases, ["deployed agents", "deployed", "active"]);
135
- this.logger.info("Creating new agent in directory", {
136
- firstName: agentInfo.firstName,
137
- lastName: agentInfo.lastName,
138
- teamId: wsTeamId,
139
- phaseId,
140
- });
141
- // Build fields using dynamic field IDs (support multiple key variants)
142
- const fields = {};
143
- // firstName field
144
- const firstNameFieldId = schema.fields["firstName"] || schema.fields["Etunimi"];
145
- if (firstNameFieldId)
146
- fields[firstNameFieldId] = agentInfo.firstName;
147
- // lastName field
148
- const lastNameFieldId = schema.fields["lastName"] || schema.fields["Sukunimi"];
149
- if (lastNameFieldId)
150
- fields[lastNameFieldId] = agentInfo.lastName;
151
- // description field
152
- const descFieldId = schema.fields["description"] || schema.fields["agentmemoryThatAgentUsesToFindWhatToDo"] || schema.fields["Description"];
153
- if (descFieldId)
154
- fields[descFieldId] = agentInfo.description;
155
- // email field (key varies by template)
156
- const emailFieldId = schema.fields["agentEmailInHailer"] || schema.fields["email"] || schema.fields["Email of Hailer profile"];
157
- if (emailFieldId)
158
- fields[emailFieldId] = agentInfo.email;
159
- // hailerProfile field (user ID link)
160
- const profileFieldId = schema.fields["hailerProfile"] || schema.fields["Agent Hailer profile"];
161
- if (profileFieldId)
162
- fields[profileFieldId] = agentInfo.userId;
163
- // startDate field
164
- const startDateFieldId = schema.fields["startDate"] || schema.fields["Aloituspäivämäärä"];
165
- if (startDateFieldId)
166
- fields[startDateFieldId] = Date.now();
167
- const createParams = {
168
- workflowId: schema.workflowId,
169
- name: `${agentInfo.firstName} ${agentInfo.lastName}`,
170
- phaseId,
171
- fields,
172
- };
173
- if (wsTeamId) {
174
- createParams.teamId = wsTeamId;
175
- }
176
- const result = await this.callMcpTool("create_activity", createParams);
177
- // Parse result to get the created activity ID
178
- const resultText = result?.content?.[0]?.text;
179
- if (resultText) {
180
- this.agentDirectoryId = this.extractActivityId(resultText);
181
- if (this.agentDirectoryId) {
182
- this.logger.info("Agent registered in directory", {
183
- agentDirectoryId: this.agentDirectoryId,
184
- phaseId,
185
- });
186
- }
187
- else {
188
- this.logger.warn("Could not extract activity ID from create response", {
189
- responsePreview: resultText.substring(0, 200),
190
- });
191
- }
192
- }
193
- }
194
- catch (error) {
195
- this.logger.warn("Failed to register agent in directory", {
196
- error: error instanceof Error ? error.message : String(error),
197
- });
198
- }
199
- }
200
- /**
201
- * Find phase ID by trying multiple names (case insensitive, partial match)
202
- */
203
- findPhaseId(phases, preferredNames) {
204
- // Normalize phase keys for lookup
205
- const normalizedPhases = Object.entries(phases).map(([name, id]) => ({
206
- name: name.toLowerCase(),
207
- id,
208
- }));
209
- // Try each preferred name in order
210
- for (const preferred of preferredNames) {
211
- const match = normalizedPhases.find(p => p.name.includes(preferred.toLowerCase()));
212
- if (match)
213
- return match.id;
214
- }
215
- // Fallback to first phase
216
- return Object.values(phases)[0];
217
- }
218
- /**
219
- * Find existing agent entry by hailerProfile (userId) - most reliable method
220
- */
221
- async findAgentByUserId(userId, schema) {
222
- const hailerProfileFieldId = schema.fields["hailerProfile"] || schema.fields["Agent Hailer profile"];
223
- if (!hailerProfileFieldId) {
224
- this.logger.debug("No hailerProfile field in Agent Directory schema");
225
- return null;
226
- }
227
- // Search across all phases (agent might be in any phase)
228
- const phasesToSearch = Object.values(schema.phases);
229
- for (const phaseId of phasesToSearch) {
230
- try {
231
- const result = await this.callMcpTool("list_activities", {
232
- workflowId: schema.workflowId,
233
- phaseId,
234
- fields: [hailerProfileFieldId],
235
- filters: {
236
- [hailerProfileFieldId]: {
237
- operator: "equals",
238
- value: userId,
239
- },
240
- },
241
- limit: 1,
242
- });
243
- const resultText = result?.content?.[0]?.text;
244
- if (resultText) {
245
- const activity = this.parseFirstActivity(resultText);
246
- if (activity) {
247
- this.logger.debug("Found agent by userId", { phaseId, activityId: activity._id });
248
- return activity;
249
- }
250
- }
251
- }
252
- catch (error) {
253
- this.logger.debug("Error searching phase for agent", { phaseId, error });
254
- }
255
- }
256
- return null;
257
- }
258
- /**
259
- * Parse first activity from list_activities response
260
- */
261
- parseFirstActivity(text) {
262
- try {
263
- // Try JSON parse first
264
- const jsonMatch = text.match(/```json\s*([\s\S]*?)\s*```/) ||
265
- text.match(/\[[\s\S]*?\]/);
266
- if (jsonMatch) {
267
- const jsonStr = jsonMatch[1] || jsonMatch[0];
268
- const activities = JSON.parse(jsonStr);
269
- if (Array.isArray(activities) && activities.length > 0) {
270
- return activities[0];
271
- }
272
- }
273
- }
274
- catch {
275
- // Regex fallback
276
- const idMatch = text.match(/"_id":\s*"([a-f0-9]{24})"/i);
277
- const nameMatch = text.match(/"name":\s*"([^"]+)"/i);
278
- if (idMatch) {
279
- return { _id: idMatch[1], name: nameMatch?.[1] || "Unknown" };
280
- }
281
- }
282
- return null;
283
- }
284
- /**
285
- * Register/find shared Team for AI agents
286
- */
287
- async registerTeam(workspaceId) {
288
- const schema = this.schemaCache.getTeamsSchema(workspaceId);
289
- if (!schema) {
290
- this.logger.debug("Teams workflow not found - skipping team registration");
291
- return;
292
- }
293
- const teamName = "AI Agents";
294
- const phaseId = this.findPhaseId(schema.phases, ["tiimit", "teams", "active"]);
295
- const infoFieldId = schema.fields["teamInfo"] || schema.fields["info"] || schema.fields["Tiimi-info"];
296
- try {
297
- // Search for existing team
298
- const result = await this.callMcpTool("list_activities", {
299
- workflowId: schema.workflowId,
300
- phaseId,
301
- fields: infoFieldId ? [infoFieldId] : [],
302
- search: teamName,
303
- limit: 1,
304
- });
305
- const resultText = result?.content?.[0]?.text;
306
- const existingId = this.extractActivityId(resultText);
307
- if (existingId) {
308
- this.teamId = existingId;
309
- this.logger.info("Found existing AI Agents team", { teamId: this.teamId });
310
- return;
311
- }
312
- // Create new team
313
- const wsTeamId = this.getDefaultTeamId();
314
- const fields = {};
315
- if (infoFieldId) {
316
- fields[infoFieldId] = "Team of AI agent bots that assist with workspace operations.";
317
- }
318
- const createParams = {
319
- workflowId: schema.workflowId,
320
- name: teamName,
321
- phaseId,
322
- fields,
323
- };
324
- if (wsTeamId)
325
- createParams.teamId = wsTeamId;
326
- const createResult = await this.callMcpTool("create_activity", createParams);
327
- this.teamId = this.extractActivityId(createResult?.content?.[0]?.text);
328
- this.logger.info("Created AI Agents team", { teamId: this.teamId });
329
- }
330
- catch (error) {
331
- this.logger.warn("Failed to register team", { error });
332
- }
333
- }
334
- /**
335
- * Register MCP server in Tool Registry (per-agent)
336
- */
337
- async registerToolRegistry(mcpServerUrl, toolIndex, workspaceId, agentName) {
338
- const schema = this.schemaCache.getToolRegistrySchema(workspaceId);
339
- if (!schema) {
340
- this.logger.debug("Tool Registry workflow not found - skipping");
341
- return;
342
- }
343
- // Each agent gets its own Tool Registry entry
344
- const serverName = `MCP Tools - ${agentName}`;
345
- const phaseId = this.findPhaseId(schema.phases, ["active", "new phase"]);
346
- try {
347
- // Get field IDs with fallbacks
348
- const baseUrlFieldId = schema.fields["mcpBaseUrl"] || schema.fields["baseUrl"] || schema.fields["mcp_base_url"];
349
- const protocolFieldId = schema.fields["protocolVersion"] || schema.fields["protocol_version"];
350
- const toolRegistryFieldId = schema.fields["toolRegistry"] || schema.fields["tool_registry"];
351
- // Search for existing registry entry
352
- const result = await this.callMcpTool("list_activities", {
353
- workflowId: schema.workflowId,
354
- phaseId,
355
- fields: baseUrlFieldId ? [baseUrlFieldId] : [],
356
- search: serverName,
357
- limit: 1,
358
- });
359
- const resultText = result?.content?.[0]?.text;
360
- const existingId = this.extractActivityId(resultText);
361
- if (existingId) {
362
- this.toolRegistryId = existingId;
363
- this.logger.info("Found existing Tool Registry entry", { toolRegistryId: this.toolRegistryId });
364
- return;
365
- }
366
- // Create new tool registry entry
367
- const wsTeamId = this.getDefaultTeamId();
368
- const toolNames = toolIndex.map((t) => t.name).join(", ");
369
- const fields = {};
370
- if (baseUrlFieldId)
371
- fields[baseUrlFieldId] = mcpServerUrl;
372
- if (protocolFieldId)
373
- fields[protocolFieldId] = "2024-11-05";
374
- if (toolRegistryFieldId)
375
- fields[toolRegistryFieldId] = `Available tools (${toolIndex.length}):\n${toolNames}`;
376
- const createParams = {
377
- workflowId: schema.workflowId,
378
- name: serverName,
379
- phaseId,
380
- fields,
381
- };
382
- if (wsTeamId)
383
- createParams.teamId = wsTeamId;
384
- const createResult = await this.callMcpTool("create_activity", createParams);
385
- this.toolRegistryId = this.extractActivityId(createResult?.content?.[0]?.text);
386
- this.logger.info("Created Tool Registry entry", { toolRegistryId: this.toolRegistryId });
387
- }
388
- catch (error) {
389
- this.logger.warn("Failed to register tool registry", { error });
390
- }
391
- }
392
- /**
393
- * Register Position for this agent
394
- */
395
- async registerPosition(details, workspaceId) {
396
- const schema = this.schemaCache.getPositionsSchema(workspaceId);
397
- if (!schema) {
398
- this.logger.debug("Positions workflow not found - skipping");
399
- return;
400
- }
401
- const phaseId = this.findPhaseId(schema.phases, ["positiot", "positions", "active"]);
402
- // Get field IDs with fallbacks
403
- const purposeFieldId = schema.fields["purpose"] || schema.fields["Purpose"];
404
- const personaFieldId = schema.fields["personaTone"] || schema.fields["Persona & Tone"];
405
- const capabilitiesFieldId = schema.fields["coreCapabilities"] || schema.fields["Core Capabilities"];
406
- const boundariesFieldId = schema.fields["boundaries"] || schema.fields["Boundaries & Restrictions"];
407
- const multiAllowedFieldId = schema.fields["multipleAllowed"] || schema.fields[">1 allowed in role"];
408
- const agentCountFieldId = schema.fields["numberOfAgents"] || schema.fields["Number of agents in position"];
409
- try {
410
- // Search for existing position by name
411
- const result = await this.callMcpTool("list_activities", {
412
- workflowId: schema.workflowId,
413
- phaseId,
414
- fields: purposeFieldId ? [purposeFieldId] : [],
415
- search: details.name,
416
- limit: 1,
417
- });
418
- const resultText = result?.content?.[0]?.text;
419
- const existingId = this.extractActivityId(resultText);
420
- if (existingId) {
421
- this.positionId = existingId;
422
- this.logger.info("Found existing Position", { positionId: this.positionId });
423
- return;
424
- }
425
- // Create new position
426
- const wsTeamId = this.getDefaultTeamId();
427
- const fields = {};
428
- if (purposeFieldId)
429
- fields[purposeFieldId] = details.purpose;
430
- if (personaFieldId)
431
- fields[personaFieldId] = details.personaTone;
432
- if (capabilitiesFieldId)
433
- fields[capabilitiesFieldId] = details.coreCapabilities;
434
- if (boundariesFieldId)
435
- fields[boundariesFieldId] = details.boundaries;
436
- if (multiAllowedFieldId)
437
- fields[multiAllowedFieldId] = "Yes";
438
- if (agentCountFieldId)
439
- fields[agentCountFieldId] = 1;
440
- const createParams = {
441
- workflowId: schema.workflowId,
442
- name: details.name,
443
- phaseId,
444
- fields,
445
- };
446
- if (wsTeamId)
447
- createParams.teamId = wsTeamId;
448
- const createResult = await this.callMcpTool("create_activity", createParams);
449
- this.positionId = this.extractActivityId(createResult?.content?.[0]?.text);
450
- this.logger.info("Created Position", { positionId: this.positionId });
451
- }
452
- catch (error) {
453
- this.logger.warn("Failed to register position", { error });
454
- }
455
- }
456
- /**
457
- * Register MCP Config for this agent
458
- */
459
- async registerMcpConfig(agentInfo, workspaceId) {
460
- if (!this.agentDirectoryId) {
461
- this.logger.debug("Skipping MCP config - no agent directory ID");
462
- return;
463
- }
464
- const schema = this.schemaCache.getMcpConfigSchema(workspaceId);
465
- if (!schema) {
466
- this.logger.debug("MCP Config workflow not found - skipping");
467
- return;
468
- }
469
- const configName = `${agentInfo.firstName} ${agentInfo.lastName} Config`;
470
- const phaseId = this.findPhaseId(schema.phases, ["active", "new phase"]);
471
- // Get field IDs with fallbacks
472
- const agentNameFieldId = schema.fields["agentName"] || schema.fields["agent_name"];
473
- const agentIdFieldId = schema.fields["agentId"] || schema.fields["Agent_ID"];
474
- const workspaceIdFieldId = schema.fields["workspaceId"] || schema.fields["workspace_id"];
475
- const permissionsFieldId = schema.fields["permissions"] || schema.fields["Permissions"];
476
- const permCreateFieldId = schema.fields["permissionCreate"] || schema.fields["Create"];
477
- const permReadFieldId = schema.fields["permissionRead"] || schema.fields["Read"];
478
- const permUpdateFieldId = schema.fields["permissionUpdate"] || schema.fields["Update"];
479
- const permDeleteFieldId = schema.fields["permissionDelete"] || schema.fields["Delete"];
480
- const accessToFieldId = schema.fields["accessTo"] || schema.fields["Access to"];
481
- try {
482
- // Search for existing config
483
- const result = await this.callMcpTool("list_activities", {
484
- workflowId: schema.workflowId,
485
- phaseId,
486
- fields: agentIdFieldId ? [agentIdFieldId] : [],
487
- search: configName,
488
- limit: 1,
489
- });
490
- const resultText = result?.content?.[0]?.text;
491
- const existingId = this.extractActivityId(resultText);
492
- if (existingId) {
493
- this.mcpConfigId = existingId;
494
- this.logger.info("Found existing MCP Config", { mcpConfigId: this.mcpConfigId });
495
- return;
496
- }
497
- // Create new MCP config
498
- const wsTeamId = this.getDefaultTeamId();
499
- const fields = {};
500
- if (agentNameFieldId)
501
- fields[agentNameFieldId] = this.agentDirectoryId;
502
- if (agentIdFieldId)
503
- fields[agentIdFieldId] = agentInfo.userId;
504
- if (workspaceIdFieldId)
505
- fields[workspaceIdFieldId] = workspaceId;
506
- if (permissionsFieldId)
507
- fields[permissionsFieldId] = "read, write";
508
- if (permCreateFieldId)
509
- fields[permCreateFieldId] = 1;
510
- if (permReadFieldId)
511
- fields[permReadFieldId] = 1;
512
- if (permUpdateFieldId)
513
- fields[permUpdateFieldId] = 1;
514
- if (permDeleteFieldId)
515
- fields[permDeleteFieldId] = 0;
516
- // Link to Tool Registry if available
517
- if (this.toolRegistryId && accessToFieldId) {
518
- fields[accessToFieldId] = this.toolRegistryId;
519
- }
520
- const createParams = {
521
- workflowId: schema.workflowId,
522
- name: configName,
523
- phaseId,
524
- fields,
525
- };
526
- if (wsTeamId)
527
- createParams.teamId = wsTeamId;
528
- const createResult = await this.callMcpTool("create_activity", createParams);
529
- this.mcpConfigId = this.extractActivityId(createResult?.content?.[0]?.text);
530
- this.logger.info("Created MCP Config", { mcpConfigId: this.mcpConfigId });
531
- }
532
- catch (error) {
533
- this.logger.warn("Failed to register MCP config", { error });
534
- }
535
- }
536
- /**
537
- * Link Agent Directory entry to Position and Team
538
- */
539
- async linkAgentToPositionAndTeam(workspaceId) {
540
- if (!this.agentDirectoryId) {
541
- this.logger.debug("Skipping agent linking - no agent directory ID");
542
- return;
543
- }
544
- const schema = this.schemaCache.getAgentDirectorySchema(workspaceId);
545
- if (!schema)
546
- return;
547
- // Get field IDs with fallbacks
548
- const positionFieldId = schema.fields["position"] || schema.fields["Työpositio"];
549
- const teamFieldId = schema.fields["team"] || schema.fields["Tiimi"];
550
- const updates = {};
551
- if (this.positionId && positionFieldId) {
552
- updates[positionFieldId] = this.positionId;
553
- }
554
- if (this.teamId && teamFieldId) {
555
- updates[teamFieldId] = this.teamId;
556
- }
557
- if (Object.keys(updates).length === 0) {
558
- this.logger.debug("No links to update");
559
- return;
560
- }
561
- try {
562
- await this.callMcpTool("update_activity", {
563
- activityId: this.agentDirectoryId,
564
- fields: updates,
565
- });
566
- this.logger.info("Linked agent to Position and Team", {
567
- agentDirectoryId: this.agentDirectoryId,
568
- positionId: this.positionId,
569
- teamId: this.teamId,
570
- });
571
- }
572
- catch (error) {
573
- this.logger.warn("Failed to link agent to Position/Team", { error });
574
- }
575
- }
576
- /**
577
- * Extract activity ID from MCP tool response text
578
- * Specifically looks for created activity IDs, not workflow IDs
579
- */
580
- extractActivityId(text) {
581
- if (!text)
582
- return null;
583
- // Priority 1: Look for "**ID**:" pattern from create_activity single response
584
- // Format: "- **ID**: 69576c7cce2c07784299e5a3"
585
- const boldIdMatch = text.match(/\*\*ID\*\*:\s*([a-f0-9]{24})/i);
586
- if (boldIdMatch)
587
- return boldIdMatch[1];
588
- // Priority 2: Look for "(ID: xxx," pattern from create_activity bulk response
589
- // Format: '1. "Name" (ID: 69576c7cce2c07784299e5a3, Phase: ...)'
590
- const parenIdMatch = text.match(/\(ID:\s*([a-f0-9]{24})[,)]/i);
591
- if (parenIdMatch)
592
- return parenIdMatch[1];
593
- // Priority 3: Look in JSON arrays (list_activities response format)
594
- // Use proper JSON parsing like parseFirstActivity
595
- try {
596
- const jsonMatch = text.match(/```json\s*([\s\S]*?)\s*```/) || text.match(/(\[[\s\S]*?\])/);
597
- if (jsonMatch) {
598
- const jsonStr = jsonMatch[1] || jsonMatch[0];
599
- const activities = JSON.parse(jsonStr);
600
- if (Array.isArray(activities) && activities.length > 0 && activities[0]._id) {
601
- return activities[0]._id;
602
- }
603
- }
604
- }
605
- catch {
606
- // Ignore parse errors - try regex fallback
607
- const idMatch = text.match(/"_id":\s*"([a-f0-9]{24})"/i);
608
- if (idMatch)
609
- return idMatch[1];
610
- }
611
- // Priority 4: Look for Activity ID label (from various tool responses)
612
- const activityIdMatch = text.match(/Activity ID[:\s]+`?([a-f0-9]{24})`?/i);
613
- if (activityIdMatch)
614
- return activityIdMatch[1];
615
- // Priority 5: Look for "created_ids" array (backup format)
616
- const createdIdsMatch = text.match(/"created_ids":\s*\[\s*"([a-f0-9]{24})"/i);
617
- if (createdIdsMatch)
618
- return createdIdsMatch[1];
619
- // Priority 6: Last resort - look for backtick-wrapped IDs (but NOT after "workflow")
620
- const backtickIdMatch = text.match(/`([a-f0-9]{24})`/g);
621
- if (backtickIdMatch && backtickIdMatch.length > 0) {
622
- // Get the last backtick-wrapped ID (more likely to be the activity, not workflow)
623
- const lastId = backtickIdMatch[backtickIdMatch.length - 1].replace(/`/g, '');
624
- return lastId;
625
- }
626
- return null;
627
- }
628
- }
629
- exports.AgentRegistryService = AgentRegistryService;
630
- //# sourceMappingURL=agent-registry.js.map
@@ -1,50 +0,0 @@
1
- /**
2
- * Conversation Manager Service
3
- *
4
- * Manages per-discussion conversation state:
5
- * - LRU cache of conversation histories
6
- * - Context size management with summarization
7
- * - Conversation state access
8
- */
9
- import Anthropic from "@anthropic-ai/sdk";
10
- import { Logger } from "../../lib/logger";
11
- /** Conversation message using Anthropic's MessageParam type */
12
- export type ConversationMessage = Anthropic.MessageParam;
13
- export declare class ConversationManager {
14
- private maxConversations;
15
- private maxContextMessages;
16
- private anthropicClient;
17
- private logger;
18
- private conversationsByDiscussion;
19
- constructor(maxConversations: number, maxContextMessages: number, anthropicClient: Anthropic, logger: Logger);
20
- /**
21
- * Get or create conversation history for a specific discussion
22
- * Each discussion has its own isolated context to prevent bloat
23
- * Uses LRU eviction when map exceeds maxConversations
24
- */
25
- getConversation(discussionId: string): ConversationMessage[];
26
- /**
27
- * Add a message to a conversation
28
- */
29
- addMessage(discussionId: string, message: ConversationMessage): void;
30
- /**
31
- * Manage conversation context size - summarize if too large
32
- */
33
- manageContextSize(discussionId: string): Promise<void>;
34
- /**
35
- * Get conversation state for debugging
36
- */
37
- getState(currentDiscussionId?: string): {
38
- discussionCount: number;
39
- currentMessageCount: number;
40
- };
41
- /**
42
- * Get full conversation for a discussion (for debugging)
43
- */
44
- getFullConversation(discussionId?: string): ConversationMessage[];
45
- /**
46
- * Clear all conversations
47
- */
48
- clear(): void;
49
- }
50
- //# sourceMappingURL=conversation-manager.d.ts.map