@hailer/mcp 0.1.17 → 0.2.2

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 +27 -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,732 +0,0 @@
1
- "use strict";
2
- /**
3
- * Workspace Schema Cache Service
4
- *
5
- * Dynamically discovers and caches workflow schemas per workspace.
6
- * Replaces hardcoded workflow/field IDs with runtime lookup.
7
- *
8
- * WORKSPACE ISOLATION: Each workspace gets its own workflow IDs.
9
- * Never use hardcoded IDs - always lookup via this cache.
10
- *
11
- * CACHING STRATEGY:
12
- * 1. Check file cache first (~/.hailer-mcp/schema-cache-{workspaceId}.json)
13
- * 2. If valid cache exists, load from file (0 API calls)
14
- * 3. If no cache, discover via API and save to file
15
- * 4. Cache is valid for 7 days (configurable)
16
- *
17
- * On bot startup:
18
- * 1. Check file cache
19
- * 2. If cache miss: Query list_workflows to find AI Agent workflows by name
20
- * 3. If missing, install them from marketplace
21
- * 4. Query get_workflow_schema to get field IDs
22
- * 5. Save to file cache
23
- */
24
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
25
- if (k2 === undefined) k2 = k;
26
- var desc = Object.getOwnPropertyDescriptor(m, k);
27
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
28
- desc = { enumerable: true, get: function() { return m[k]; } };
29
- }
30
- Object.defineProperty(o, k2, desc);
31
- }) : (function(o, m, k, k2) {
32
- if (k2 === undefined) k2 = k;
33
- o[k2] = m[k];
34
- }));
35
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
36
- Object.defineProperty(o, "default", { enumerable: true, value: v });
37
- }) : function(o, v) {
38
- o["default"] = v;
39
- });
40
- var __importStar = (this && this.__importStar) || (function () {
41
- var ownKeys = function(o) {
42
- ownKeys = Object.getOwnPropertyNames || function (o) {
43
- var ar = [];
44
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
45
- return ar;
46
- };
47
- return ownKeys(o);
48
- };
49
- return function (mod) {
50
- if (mod && mod.__esModule) return mod;
51
- var result = {};
52
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
53
- __setModuleDefault(result, mod);
54
- return result;
55
- };
56
- })();
57
- Object.defineProperty(exports, "__esModule", { value: true });
58
- exports.WorkspaceSchemaCacheService = void 0;
59
- const fs = __importStar(require("fs"));
60
- const path = __importStar(require("path"));
61
- const os = __importStar(require("os"));
62
- /** Cache directory for schema files */
63
- const CACHE_DIR = path.join(os.homedir(), ".hailer-mcp");
64
- const CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
65
- /** AI Agent template ID in marketplace (HR for AI Agents) */
66
- const AI_AGENT_TEMPLATE_ID = "685ab3c39af7445dd5384ffd";
67
- /** Known workflow names from the AI Agent template (partial match, case insensitive) */
68
- const TEMPLATE_WORKFLOW_PATTERNS = {
69
- SESSION_LOG: ["session log", "session_log"],
70
- AGENT_DIRECTORY: ["agent directory", "agent_directory"],
71
- POSITIONS: ["positions", "position"],
72
- TEAMS: ["teams", "team"],
73
- TOOL_REGISTRY: ["tool registry", "tool_registry", "mcp server"],
74
- MCP_CONFIG: ["mcp config", "mcp_config", "mcp configuration"],
75
- };
76
- /** Known field labels we need to look up */
77
- const SESSION_LOG_FIELD_LABELS = {
78
- contextSummary: "Context summary",
79
- previousLog: "Previus log entry", // Note: typo in original template
80
- linkedWork: "Most relevant link to work",
81
- cost: "Tokens Used",
82
- madeBy: "Log entry made by",
83
- };
84
- const AGENT_DIRECTORY_FIELD_LABELS = {
85
- firstName: "First name",
86
- lastName: "Last name",
87
- description: "Description",
88
- email: "Email",
89
- password: "Password",
90
- hailerProfile: "Hailer profile",
91
- team: "Team",
92
- supervisor: "Supervisor",
93
- memory: "Memory",
94
- position: "Position",
95
- startDate: "Start date",
96
- };
97
- const POSITIONS_FIELD_LABELS = {
98
- purpose: "Purpose",
99
- personaTone: "Persona / Tone",
100
- coreCapabilities: "Core Capabilities",
101
- boundaries: "Boundaries",
102
- agentCount: "Agents in this position",
103
- multiAllowed: "Multi allowed",
104
- };
105
- const TEAMS_FIELD_LABELS = {
106
- info: "Info",
107
- leader: "Leader",
108
- };
109
- const TOOL_REGISTRY_FIELD_LABELS = {
110
- baseUrl: "Base URL",
111
- protocolVersion: "Protocol Version",
112
- toolRegistry: "Tool Registry",
113
- logEntryBy: "Log entry by",
114
- };
115
- const MCP_CONFIG_FIELD_LABELS = {
116
- agentName: "Agent Name",
117
- agentId: "Agent ID",
118
- publicKey: "Public Key",
119
- accessTo: "Access To",
120
- authToken: "Auth Token",
121
- apiKey: "API Key",
122
- workspaceId: "Workspace ID",
123
- permissions: "Permissions",
124
- sessionId: "Session ID",
125
- resourceLimits: "Resource Limits",
126
- loggingEndpoint: "Logging Endpoint",
127
- callbackUrl: "Callback URL",
128
- customContext: "Custom Context",
129
- permCreate: "Create",
130
- permRead: "Read",
131
- permUpdate: "Update",
132
- permDelete: "Delete",
133
- };
134
- class WorkspaceSchemaCacheService {
135
- logger;
136
- callMcpTool;
137
- cache = new Map();
138
- constructor(logger, callMcpTool) {
139
- this.logger = logger;
140
- this.callMcpTool = callMcpTool;
141
- // Ensure cache directory exists
142
- this.ensureCacheDir();
143
- }
144
- /**
145
- * Ensure cache directory exists
146
- */
147
- ensureCacheDir() {
148
- try {
149
- if (!fs.existsSync(CACHE_DIR)) {
150
- fs.mkdirSync(CACHE_DIR, { recursive: true });
151
- }
152
- }
153
- catch (error) {
154
- this.logger.warn("Failed to create cache directory", {
155
- dir: CACHE_DIR,
156
- error: error instanceof Error ? error.message : String(error),
157
- });
158
- }
159
- }
160
- /**
161
- * Get cache file path for a workspace
162
- */
163
- getCacheFilePath(workspaceId) {
164
- return path.join(CACHE_DIR, `schema-cache-${workspaceId}.json`);
165
- }
166
- /**
167
- * Load schemas from file cache
168
- * Returns null if cache doesn't exist or is expired
169
- */
170
- loadFromFileCache(workspaceId) {
171
- const filePath = this.getCacheFilePath(workspaceId);
172
- try {
173
- if (!fs.existsSync(filePath)) {
174
- return null;
175
- }
176
- const data = fs.readFileSync(filePath, "utf-8");
177
- const schemas = JSON.parse(data);
178
- // Check TTL
179
- if (schemas.cachedAt && Date.now() - schemas.cachedAt > CACHE_TTL_MS) {
180
- this.logger.debug("File cache expired", { workspaceId, cachedAt: schemas.cachedAt });
181
- return null;
182
- }
183
- this.logger.info("Loaded schemas from file cache", {
184
- workspaceId,
185
- cachedAt: schemas.cachedAt ? new Date(schemas.cachedAt).toISOString() : "unknown",
186
- });
187
- return schemas;
188
- }
189
- catch (error) {
190
- this.logger.warn("Failed to load file cache", {
191
- filePath,
192
- error: error instanceof Error ? error.message : String(error),
193
- });
194
- return null;
195
- }
196
- }
197
- /**
198
- * Save schemas to file cache
199
- */
200
- saveToFileCache(schemas) {
201
- const filePath = this.getCacheFilePath(schemas.workspaceId);
202
- try {
203
- schemas.cachedAt = Date.now();
204
- fs.writeFileSync(filePath, JSON.stringify(schemas, null, 2), "utf-8");
205
- this.logger.debug("Saved schemas to file cache", { filePath });
206
- }
207
- catch (error) {
208
- this.logger.warn("Failed to save file cache", {
209
- filePath,
210
- error: error instanceof Error ? error.message : String(error),
211
- });
212
- }
213
- }
214
- /**
215
- * Initialize schema cache for a workspace
216
- * Call this on bot startup after connecting to Hailer
217
- */
218
- async initializeForWorkspace(workspaceId) {
219
- // 1. Check memory cache first
220
- const existing = this.cache.get(workspaceId);
221
- if (existing?.initialized) {
222
- this.logger.debug("Using memory-cached workspace schemas", { workspaceId });
223
- return existing;
224
- }
225
- // 2. Check file cache (persists across restarts)
226
- const fileCache = this.loadFromFileCache(workspaceId);
227
- if (fileCache?.initialized) {
228
- this.cache.set(workspaceId, fileCache);
229
- return fileCache;
230
- }
231
- // 3. Discover via API (expensive - only on first run or cache expiry)
232
- this.logger.info("Discovering workspace schemas via API", { workspaceId });
233
- const schemas = {
234
- workspaceId,
235
- initialized: false,
236
- };
237
- try {
238
- // Fetch all workflows in workspace
239
- const workflowsResult = await this.callMcpTool("list_workflows", {
240
- workspace: workspaceId,
241
- });
242
- const workflowsText = workflowsResult?.content?.[0]?.text;
243
- if (!workflowsText) {
244
- this.logger.warn("No workflows found in workspace", { workspaceId });
245
- schemas.initialized = true;
246
- this.cache.set(workspaceId, schemas);
247
- return schemas;
248
- }
249
- // Parse workflows
250
- const workflows = this.parseWorkflows(workflowsText);
251
- this.logger.debug("Found workflows", { workspaceId, count: workflows.length });
252
- // Find template workflows
253
- this.mapWorkflowsToSchemas(workflows, schemas);
254
- // Check if required workflows are missing
255
- const missingRequired = !schemas.sessionLog || !schemas.agentDirectory;
256
- if (missingRequired) {
257
- this.logger.info("AI Agent workflows not found, installing from marketplace", { workspaceId });
258
- const installed = await this.installAgentTemplate(workspaceId);
259
- if (installed) {
260
- // Re-fetch workflows after installation
261
- const refreshResult = await this.callMcpTool("list_workflows", {
262
- workspace: workspaceId,
263
- });
264
- const refreshText = refreshResult?.content?.[0]?.text;
265
- if (refreshText) {
266
- const refreshedWorkflows = this.parseWorkflows(refreshText);
267
- this.mapWorkflowsToSchemas(refreshedWorkflows, schemas);
268
- }
269
- }
270
- }
271
- // Load detailed schemas for found workflows
272
- await this.loadDetailedSchemas(schemas);
273
- schemas.initialized = true;
274
- this.cache.set(workspaceId, schemas);
275
- // Save to file cache for next startup
276
- this.saveToFileCache(schemas);
277
- this.logger.info("Workspace schemas cached", {
278
- workspaceId,
279
- hasSessionLog: !!schemas.sessionLog,
280
- hasAgentDirectory: !!schemas.agentDirectory,
281
- hasPositions: !!schemas.positions,
282
- });
283
- return schemas;
284
- }
285
- catch (error) {
286
- this.logger.error("Failed to initialize workspace schemas", {
287
- workspaceId,
288
- error: error instanceof Error ? error.message : String(error),
289
- });
290
- schemas.initialized = true; // Mark as initialized even on error to prevent retry loops
291
- this.cache.set(workspaceId, schemas);
292
- return schemas;
293
- }
294
- }
295
- /**
296
- * Get cached schemas for a workspace
297
- */
298
- getSchemas(workspaceId) {
299
- return this.cache.get(workspaceId);
300
- }
301
- /**
302
- * Get Session Log schema for a workspace
303
- */
304
- getSessionLogSchema(workspaceId) {
305
- return this.cache.get(workspaceId)?.sessionLog;
306
- }
307
- /**
308
- * Get Agent Directory schema for a workspace
309
- */
310
- getAgentDirectorySchema(workspaceId) {
311
- return this.cache.get(workspaceId)?.agentDirectory;
312
- }
313
- /**
314
- * Get Positions schema for a workspace
315
- */
316
- getPositionsSchema(workspaceId) {
317
- return this.cache.get(workspaceId)?.positions;
318
- }
319
- /**
320
- * Get Teams schema for a workspace
321
- */
322
- getTeamsSchema(workspaceId) {
323
- return this.cache.get(workspaceId)?.teams;
324
- }
325
- /**
326
- * Get Tool Registry schema for a workspace
327
- */
328
- getToolRegistrySchema(workspaceId) {
329
- return this.cache.get(workspaceId)?.toolRegistry;
330
- }
331
- /**
332
- * Get MCP Config schema for a workspace
333
- */
334
- getMcpConfigSchema(workspaceId) {
335
- return this.cache.get(workspaceId)?.mcpConfig;
336
- }
337
- /**
338
- * Check if a workspace has the AI Agent template installed
339
- */
340
- hasAgentTemplate(workspaceId) {
341
- const schemas = this.cache.get(workspaceId);
342
- return !!(schemas?.sessionLog && schemas?.agentDirectory);
343
- }
344
- /**
345
- * Check if workflow name matches a pattern (case insensitive, ignores emojis)
346
- */
347
- matchesPattern(workflowName, patterns) {
348
- // Normalize: lowercase, remove emojis and extra whitespace
349
- const normalized = workflowName
350
- .toLowerCase()
351
- .replace(/[\u{1F300}-\u{1F9FF}]/gu, "") // Remove emojis
352
- .replace(/\s+/g, " ")
353
- .trim();
354
- return patterns.some(pattern => normalized.includes(pattern));
355
- }
356
- /**
357
- * Map workflow list to schema slots (using pattern matching)
358
- */
359
- mapWorkflowsToSchemas(workflows, schemas) {
360
- for (const workflow of workflows) {
361
- // Filter by workspace if specified
362
- if (schemas.workspaceId && workflow.workspaceId && workflow.workspaceId !== schemas.workspaceId) {
363
- continue;
364
- }
365
- if (this.matchesPattern(workflow.name, TEMPLATE_WORKFLOW_PATTERNS.SESSION_LOG)) {
366
- schemas.sessionLog = { workflowId: workflow.id, name: workflow.name, phases: {}, fields: {} };
367
- }
368
- else if (this.matchesPattern(workflow.name, TEMPLATE_WORKFLOW_PATTERNS.AGENT_DIRECTORY)) {
369
- schemas.agentDirectory = { workflowId: workflow.id, name: workflow.name, phases: {}, fields: {} };
370
- }
371
- else if (this.matchesPattern(workflow.name, TEMPLATE_WORKFLOW_PATTERNS.POSITIONS)) {
372
- schemas.positions = { workflowId: workflow.id, name: workflow.name, phases: {}, fields: {} };
373
- }
374
- else if (this.matchesPattern(workflow.name, TEMPLATE_WORKFLOW_PATTERNS.TEAMS) && workflow.name.toLowerCase().includes("team")) {
375
- // Be more specific for Teams to avoid matching "Teams" workflow in sports data
376
- if (workflow.name.includes("🎖️") || workflow.name.toLowerCase() === "teams") {
377
- schemas.teams = { workflowId: workflow.id, name: workflow.name, phases: {}, fields: {} };
378
- }
379
- }
380
- else if (this.matchesPattern(workflow.name, TEMPLATE_WORKFLOW_PATTERNS.TOOL_REGISTRY)) {
381
- schemas.toolRegistry = { workflowId: workflow.id, name: workflow.name, phases: {}, fields: {} };
382
- }
383
- else if (this.matchesPattern(workflow.name, TEMPLATE_WORKFLOW_PATTERNS.MCP_CONFIG)) {
384
- schemas.mcpConfig = { workflowId: workflow.id, name: workflow.name, phases: {}, fields: {} };
385
- }
386
- }
387
- }
388
- /**
389
- * Load detailed schemas (phases, fields) for all found workflows
390
- */
391
- async loadDetailedSchemas(schemas) {
392
- if (schemas.sessionLog) {
393
- await this.loadWorkflowSchemaDetails(schemas.sessionLog);
394
- this.mapFieldLabelsToIds(schemas.sessionLog, SESSION_LOG_FIELD_LABELS);
395
- }
396
- if (schemas.agentDirectory) {
397
- await this.loadWorkflowSchemaDetails(schemas.agentDirectory);
398
- this.mapFieldLabelsToIds(schemas.agentDirectory, AGENT_DIRECTORY_FIELD_LABELS);
399
- }
400
- if (schemas.positions) {
401
- await this.loadWorkflowSchemaDetails(schemas.positions);
402
- this.mapFieldLabelsToIds(schemas.positions, POSITIONS_FIELD_LABELS);
403
- }
404
- if (schemas.teams) {
405
- await this.loadWorkflowSchemaDetails(schemas.teams);
406
- this.mapFieldLabelsToIds(schemas.teams, TEAMS_FIELD_LABELS);
407
- }
408
- if (schemas.toolRegistry) {
409
- await this.loadWorkflowSchemaDetails(schemas.toolRegistry);
410
- this.mapFieldLabelsToIds(schemas.toolRegistry, TOOL_REGISTRY_FIELD_LABELS);
411
- }
412
- if (schemas.mcpConfig) {
413
- await this.loadWorkflowSchemaDetails(schemas.mcpConfig);
414
- this.mapFieldLabelsToIds(schemas.mcpConfig, MCP_CONFIG_FIELD_LABELS);
415
- }
416
- }
417
- /**
418
- * Install AI Agent template from marketplace
419
- */
420
- async installAgentTemplate(workspaceId) {
421
- try {
422
- // First try to find the template by name in marketplace
423
- const templatesResult = await this.callMcpTool("list_templates", {
424
- publicOnly: true,
425
- });
426
- let templateId = AI_AGENT_TEMPLATE_ID;
427
- const templatesText = templatesResult?.content?.[0]?.text;
428
- if (templatesText) {
429
- try {
430
- const parsed = JSON.parse(templatesText);
431
- const templates = parsed.templates || parsed.data || parsed;
432
- if (Array.isArray(templates)) {
433
- const agentTemplate = templates.find((t) => t.name?.includes("AI Agent") || t.title?.includes("AI Agent"));
434
- if (agentTemplate?._id) {
435
- templateId = agentTemplate._id;
436
- }
437
- }
438
- }
439
- catch {
440
- // Use default template ID
441
- }
442
- }
443
- this.logger.info("Installing AI Agent template", { workspaceId, templateId });
444
- const installResult = await this.callMcpTool("install_template", {
445
- templateId,
446
- workspaceId,
447
- });
448
- const installText = installResult?.content?.[0]?.text || "";
449
- // Check for explicit error indicators
450
- const hasError = installText.toLowerCase().includes("error") ||
451
- installText.toLowerCase().includes("not found") ||
452
- installText.toLowerCase().includes("failed");
453
- const hasSuccess = installText.toLowerCase().includes("success") ||
454
- installText.toLowerCase().includes("installed") ||
455
- installText.toLowerCase().includes("created");
456
- const success = hasSuccess && !hasError;
457
- if (success) {
458
- this.logger.info("AI Agent template installed successfully", { workspaceId });
459
- }
460
- else {
461
- this.logger.warn("Template installation failed", {
462
- workspaceId,
463
- response: installText.substring(0, 300),
464
- });
465
- }
466
- return success;
467
- }
468
- catch (error) {
469
- this.logger.error("Failed to install AI Agent template", {
470
- workspaceId,
471
- error: error instanceof Error ? error.message : String(error),
472
- });
473
- return false;
474
- }
475
- }
476
- /**
477
- * Parse workflows from list_workflows response
478
- * Handles both raw JSON and markdown-wrapped JSON responses
479
- */
480
- parseWorkflows(text) {
481
- const workflows = [];
482
- try {
483
- // First, try to extract JSON array from markdown-wrapped response
484
- // The response often has: "📋 **18 Workflows Found**\n\n[\n {...}\n]\n\n🔗 **Relationships**..."
485
- let jsonText = text;
486
- // Try to extract JSON array from markdown
487
- const jsonArrayMatch = text.match(/\[\s*\{[\s\S]*?\}\s*\]/);
488
- if (jsonArrayMatch) {
489
- jsonText = jsonArrayMatch[0];
490
- }
491
- const parsed = JSON.parse(jsonText);
492
- const items = Array.isArray(parsed) ? parsed : (parsed.workflows || parsed.data || []);
493
- if (Array.isArray(items)) {
494
- for (const item of items) {
495
- // Support both `id` and `_id` field names
496
- const id = item.id || item._id;
497
- const name = item.name;
498
- const workspaceId = item.workspaceId;
499
- if (id && name) {
500
- workflows.push({ id, name, workspaceId });
501
- }
502
- }
503
- }
504
- }
505
- catch {
506
- // Fallback: regex extraction for both id and _id patterns
507
- const idMatches = text.matchAll(/"(?:id|_id)":\s*"([a-f0-9]{24})"/gi);
508
- const nameMatches = text.matchAll(/"name":\s*"([^"]+)"/gi);
509
- const wsMatches = text.matchAll(/"workspaceId":\s*"([a-f0-9]{24})"/gi);
510
- const ids = [...idMatches].map(m => m[1]);
511
- const names = [...nameMatches].map(m => m[1]);
512
- const wsIds = [...wsMatches].map(m => m[1]);
513
- for (let i = 0; i < Math.min(ids.length, names.length); i++) {
514
- workflows.push({
515
- id: ids[i],
516
- name: names[i],
517
- workspaceId: wsIds[i],
518
- });
519
- }
520
- }
521
- this.logger.debug("Parsed workflows from response", { count: workflows.length });
522
- return workflows;
523
- }
524
- /**
525
- * Load detailed workflow schema (phases and fields) for an existing schema
526
- */
527
- async loadWorkflowSchemaDetails(schema) {
528
- try {
529
- // Get phases first
530
- const phasesResult = await this.callMcpTool("list_workflow_phases", {
531
- workflowId: schema.workflowId,
532
- });
533
- const phasesText = phasesResult?.content?.[0]?.text;
534
- if (phasesText) {
535
- this.parsePhases(phasesText, schema);
536
- }
537
- this.logger.debug("Parsed phases for workflow", {
538
- workflowId: schema.workflowId,
539
- workflowName: schema.name,
540
- phaseCount: Object.keys(schema.phases).length,
541
- phases: Object.keys(schema.phases),
542
- });
543
- // Get schema with fields (use first phase)
544
- const firstPhaseId = Object.values(schema.phases)[0];
545
- if (firstPhaseId) {
546
- const schemaResult = await this.callMcpTool("get_workflow_schema", {
547
- workflowId: schema.workflowId,
548
- phaseId: firstPhaseId,
549
- });
550
- const schemaText = schemaResult?.content?.[0]?.text;
551
- if (schemaText) {
552
- this.parseFields(schemaText, schema);
553
- this.logger.debug("Parsed fields for workflow", {
554
- workflowId: schema.workflowId,
555
- workflowName: schema.name,
556
- fieldCount: Object.keys(schema.fields).length,
557
- fieldKeys: Object.keys(schema.fields).slice(0, 10), // First 10 keys
558
- });
559
- }
560
- else {
561
- this.logger.warn("No schema text returned from get_workflow_schema", {
562
- workflowId: schema.workflowId,
563
- });
564
- }
565
- }
566
- else {
567
- this.logger.warn("No phase ID found - skipping field schema load", {
568
- workflowId: schema.workflowId,
569
- workflowName: schema.name,
570
- });
571
- }
572
- }
573
- catch (error) {
574
- this.logger.warn("Failed to load workflow schema details", {
575
- workflowId: schema.workflowId,
576
- workflowName: schema.name,
577
- error: error instanceof Error ? error.message : String(error),
578
- });
579
- }
580
- }
581
- /**
582
- * Parse phases from list_workflow_phases response
583
- * Format: 1. **PhaseName**\n - Phase ID: `phaseId`
584
- */
585
- parsePhases(text, schema) {
586
- this.logger.debug("Parsing phases text", {
587
- workflowId: schema.workflowId,
588
- textLength: text.length,
589
- textPreview: text.substring(0, 500),
590
- });
591
- try {
592
- const parsed = JSON.parse(text);
593
- const phases = parsed.phases || parsed.data || parsed;
594
- if (Array.isArray(phases)) {
595
- for (const phase of phases) {
596
- if (phase.id && phase.name) {
597
- schema.phases[phase.name.toLowerCase()] = phase.id;
598
- }
599
- else if (phase._id && phase.name) {
600
- schema.phases[phase.name.toLowerCase()] = phase._id;
601
- }
602
- }
603
- }
604
- }
605
- catch {
606
- // Markdown format: **PhaseName**\n - Phase ID: `phaseId`
607
- // Match pattern: \d+\. **Name** ... Phase ID: `id`
608
- const phaseBlocks = text.split(/\d+\.\s*\*\*/).filter(Boolean);
609
- for (const block of phaseBlocks) {
610
- const nameMatch = block.match(/^([^*]+)\*\*/);
611
- const phaseIdMatch = block.match(/Phase ID:\s*`([a-f0-9]{24})`/i);
612
- if (nameMatch && phaseIdMatch) {
613
- const phaseName = nameMatch[1].trim();
614
- const phaseId = phaseIdMatch[1];
615
- schema.phases[phaseName.toLowerCase()] = phaseId;
616
- }
617
- }
618
- }
619
- }
620
- /**
621
- * Parse fields from get_workflow_schema response
622
- * Stores fields by both label and key for flexible lookup
623
- */
624
- parseFields(text, schema) {
625
- try {
626
- const parsed = JSON.parse(text);
627
- const fields = parsed.fields || parsed.data || parsed;
628
- if (Array.isArray(fields)) {
629
- for (const field of fields) {
630
- const fieldId = field.id || field._id || field.fieldId;
631
- const label = field.label || field.name;
632
- const key = field.key;
633
- if (fieldId && label) {
634
- schema.fields[label] = fieldId;
635
- }
636
- // Also store by key for direct lookup (e.g., "firstName" -> fieldId)
637
- if (fieldId && key) {
638
- schema.fields[key] = fieldId;
639
- }
640
- }
641
- }
642
- else if (typeof fields === "object") {
643
- // Handle object format {fieldId: {label, type, key, ...}}
644
- for (const [fieldId, fieldData] of Object.entries(fields)) {
645
- const data = fieldData;
646
- const label = data.label || data.name;
647
- const key = data.key;
648
- if (label) {
649
- schema.fields[label] = fieldId;
650
- }
651
- // Also store by key
652
- if (key) {
653
- schema.fields[key] = fieldId;
654
- }
655
- }
656
- }
657
- }
658
- catch {
659
- // Regex fallback for markdown format:
660
- // **Label**
661
- // - Field ID: `fieldId`
662
- // - Key: `key`
663
- const fieldBlocks = text.split(/\d+\.\s*\*\*/).filter(Boolean);
664
- for (const block of fieldBlocks) {
665
- const labelMatch = block.match(/^([^*\n]+)\*\*/);
666
- const fieldIdMatch = block.match(/Field ID:\s*`([a-f0-9]{24})`/i);
667
- const keyMatch = block.match(/Key:\s*`(\w+)`/i);
668
- if (fieldIdMatch) {
669
- const fieldId = fieldIdMatch[1];
670
- if (labelMatch) {
671
- schema.fields[labelMatch[1].trim()] = fieldId;
672
- }
673
- if (keyMatch) {
674
- schema.fields[keyMatch[1]] = fieldId;
675
- }
676
- }
677
- }
678
- }
679
- }
680
- /**
681
- * Map field keys to their IDs in the schema
682
- * Since parseFields now stores by key directly, this is a no-op
683
- * Kept for backwards compatibility but does nothing
684
- */
685
- mapFieldLabelsToIds(_schema, _keyMap) {
686
- // parseFields now stores fields by key directly, so this is a no-op
687
- }
688
- /**
689
- * Clear cache for a workspace (clears both memory and file cache)
690
- */
691
- clearCache(workspaceId) {
692
- if (workspaceId) {
693
- // Clear memory cache
694
- this.cache.delete(workspaceId);
695
- // Clear file cache
696
- const filePath = this.getCacheFilePath(workspaceId);
697
- try {
698
- if (fs.existsSync(filePath)) {
699
- fs.unlinkSync(filePath);
700
- this.logger.info("Cleared file cache", { workspaceId });
701
- }
702
- }
703
- catch (error) {
704
- this.logger.warn("Failed to clear file cache", {
705
- filePath,
706
- error: error instanceof Error ? error.message : String(error),
707
- });
708
- }
709
- }
710
- else {
711
- // Clear all memory cache
712
- this.cache.clear();
713
- // Clear all file caches
714
- try {
715
- const files = fs.readdirSync(CACHE_DIR).filter(f => f.startsWith("schema-cache-"));
716
- for (const file of files) {
717
- fs.unlinkSync(path.join(CACHE_DIR, file));
718
- }
719
- if (files.length > 0) {
720
- this.logger.info("Cleared all file caches", { count: files.length });
721
- }
722
- }
723
- catch (error) {
724
- this.logger.warn("Failed to clear all file caches", {
725
- error: error instanceof Error ? error.message : String(error),
726
- });
727
- }
728
- }
729
- }
730
- }
731
- exports.WorkspaceSchemaCacheService = WorkspaceSchemaCacheService;
732
- //# sourceMappingURL=workspace-schema-cache.js.map