@l4yercak3/cli 1.1.12 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,516 @@
1
+ /**
2
+ * Applications Domain Tools
3
+ *
4
+ * Tools for registering and managing connected applications.
5
+ * These represent projects that integrate with L4YERCAK3.
6
+ *
7
+ * @module mcp/registry/domains/applications
8
+ */
9
+
10
+ const crypto = require('crypto');
11
+ const backendClient = require('../../../api/backend-client');
12
+ const configManager = require('../../../config/config-manager');
13
+
14
+ /**
15
+ * Applications domain definition
16
+ */
17
+ module.exports = {
18
+ name: 'applications',
19
+ description: 'Connected application registration and management',
20
+ tools: [
21
+ // ========================================
22
+ // Application Registration
23
+ // ========================================
24
+ {
25
+ name: 'l4yercak3_register_application',
26
+ description: `Register the current project as a connected application with L4YERCAK3.
27
+ This creates a connection between the user's project and their L4YERCAK3 organization.
28
+
29
+ Use this after the user has decided to integrate their project with L4YERCAK3.
30
+ Returns an API key for the application to use.`,
31
+ inputSchema: {
32
+ type: 'object',
33
+ properties: {
34
+ name: {
35
+ type: 'string',
36
+ description: 'Application name (defaults to project name)',
37
+ },
38
+ description: {
39
+ type: 'string',
40
+ description: 'Application description',
41
+ },
42
+ projectPath: {
43
+ type: 'string',
44
+ description: 'Project directory path (for tracking)',
45
+ },
46
+ framework: {
47
+ type: 'string',
48
+ enum: ['nextjs', 'remix', 'astro', 'nuxt', 'sveltekit', 'other'],
49
+ description: 'Framework being used',
50
+ },
51
+ frameworkVersion: {
52
+ type: 'string',
53
+ description: 'Framework version (e.g., "14.0.0")',
54
+ },
55
+ features: {
56
+ type: 'array',
57
+ items: {
58
+ type: 'string',
59
+ enum: ['crm', 'events', 'forms', 'invoicing', 'checkout'],
60
+ },
61
+ description: 'Features to enable for this application',
62
+ },
63
+ hasTypeScript: {
64
+ type: 'boolean',
65
+ description: 'Whether the project uses TypeScript',
66
+ },
67
+ routerType: {
68
+ type: 'string',
69
+ enum: ['app', 'pages'],
70
+ description: 'Next.js router type (if applicable)',
71
+ },
72
+ },
73
+ required: ['name', 'features'],
74
+ },
75
+ requiresAuth: true,
76
+ requiredPermissions: ['manage_applications'],
77
+ handler: async (params, authContext) => {
78
+ // Generate project path hash for tracking
79
+ const projectPathHash = params.projectPath
80
+ ? crypto.createHash('sha256').update(params.projectPath).digest('hex')
81
+ : null;
82
+
83
+ // Check if application already exists
84
+ if (projectPathHash) {
85
+ const existing = await backendClient.checkExistingApplication(
86
+ authContext.organizationId,
87
+ projectPathHash
88
+ );
89
+
90
+ if (existing.found) {
91
+ return {
92
+ success: true,
93
+ applicationId: existing.application._id,
94
+ existingApplication: true,
95
+ message: 'Application already registered for this project',
96
+ application: {
97
+ id: existing.application._id,
98
+ name: existing.application.name,
99
+ features: existing.application.customProperties?.connection?.features || [],
100
+ },
101
+ };
102
+ }
103
+ }
104
+
105
+ // Register new application
106
+ const response = await backendClient.registerApplication({
107
+ organizationId: authContext.organizationId,
108
+ name: params.name,
109
+ description: params.description,
110
+ source: {
111
+ type: 'cli',
112
+ projectPathHash,
113
+ cliVersion: require('../../../../package.json').version,
114
+ framework: params.framework,
115
+ frameworkVersion: params.frameworkVersion,
116
+ hasTypeScript: params.hasTypeScript,
117
+ routerType: params.routerType,
118
+ },
119
+ connection: {
120
+ features: params.features || [],
121
+ hasFrontendDatabase: false,
122
+ },
123
+ });
124
+
125
+ // Save to local config
126
+ if (params.projectPath) {
127
+ configManager.saveProjectConfig(params.projectPath, {
128
+ applicationId: response.applicationId,
129
+ apiKeyId: response.apiKey?.id,
130
+ features: params.features,
131
+ });
132
+ }
133
+
134
+ return {
135
+ success: true,
136
+ applicationId: response.applicationId,
137
+ existingApplication: false,
138
+ apiKey: response.apiKey
139
+ ? {
140
+ id: response.apiKey.id,
141
+ key: response.apiKey.key,
142
+ prefix: response.apiKey.prefix,
143
+ }
144
+ : null,
145
+ backendUrl: response.backendUrl,
146
+ message: `Registered application: ${params.name}`,
147
+ nextSteps: [
148
+ response.apiKey
149
+ ? `Add L4YERCAK3_API_KEY=${response.apiKey.key} to your .env.local`
150
+ : 'Generate an API key with l4yercak3 api-keys generate',
151
+ 'Generate API client with l4yercak3_generate_api_client',
152
+ ],
153
+ };
154
+ },
155
+ },
156
+
157
+ {
158
+ name: 'l4yercak3_get_application',
159
+ description: `Get details about a registered application.`,
160
+ inputSchema: {
161
+ type: 'object',
162
+ properties: {
163
+ applicationId: {
164
+ type: 'string',
165
+ description: 'Application ID (optional if projectPath provided)',
166
+ },
167
+ projectPath: {
168
+ type: 'string',
169
+ description: 'Project path to look up by',
170
+ },
171
+ },
172
+ },
173
+ requiresAuth: true,
174
+ handler: async (params, authContext) => {
175
+ let applicationId = params.applicationId;
176
+
177
+ // If projectPath provided, look up by path
178
+ if (!applicationId && params.projectPath) {
179
+ const projectPathHash = crypto
180
+ .createHash('sha256')
181
+ .update(params.projectPath)
182
+ .digest('hex');
183
+
184
+ const existing = await backendClient.checkExistingApplication(
185
+ authContext.organizationId,
186
+ projectPathHash
187
+ );
188
+
189
+ if (existing.found) {
190
+ applicationId = existing.application._id;
191
+ } else {
192
+ return {
193
+ found: false,
194
+ message: 'No application registered for this project path',
195
+ };
196
+ }
197
+ }
198
+
199
+ if (!applicationId) {
200
+ throw new Error('Either applicationId or projectPath is required');
201
+ }
202
+
203
+ const response = await backendClient.getApplication(applicationId);
204
+ const app = response.application || response;
205
+
206
+ return {
207
+ found: true,
208
+ application: {
209
+ id: app.id,
210
+ name: app.name,
211
+ description: app.description,
212
+ status: app.status,
213
+ framework: app.source?.framework,
214
+ features: app.connection?.features || [],
215
+ modelMappings: app.modelMappings || [],
216
+ sync: app.sync || { lastSyncAt: null },
217
+ registeredAt: app.cli?.registeredAt,
218
+ lastActivityAt: app.cli?.lastActivityAt,
219
+ },
220
+ };
221
+ },
222
+ },
223
+
224
+ {
225
+ name: 'l4yercak3_list_applications',
226
+ description: `List all applications registered with the organization.`,
227
+ inputSchema: {
228
+ type: 'object',
229
+ properties: {
230
+ status: {
231
+ type: 'string',
232
+ enum: ['active', 'inactive', 'paused'],
233
+ description: 'Filter by status',
234
+ },
235
+ limit: {
236
+ type: 'number',
237
+ description: 'Max applications to return',
238
+ },
239
+ },
240
+ },
241
+ requiresAuth: true,
242
+ handler: async (params, authContext) => {
243
+ const response = await backendClient.listApplications(authContext.organizationId);
244
+
245
+ return {
246
+ applications: (response.applications || []).map(app => ({
247
+ id: app.id,
248
+ name: app.name,
249
+ status: app.status,
250
+ framework: app.framework,
251
+ features: app.features || [],
252
+ registeredAt: app.registeredAt,
253
+ lastActivityAt: app.lastActivityAt,
254
+ })),
255
+ total: response.total || (response.applications || []).length,
256
+ };
257
+ },
258
+ },
259
+
260
+ {
261
+ name: 'l4yercak3_update_application',
262
+ description: `Update an application's configuration.`,
263
+ inputSchema: {
264
+ type: 'object',
265
+ properties: {
266
+ applicationId: {
267
+ type: 'string',
268
+ description: 'Application ID to update',
269
+ },
270
+ name: { type: 'string', description: 'New name' },
271
+ description: { type: 'string', description: 'New description' },
272
+ features: {
273
+ type: 'array',
274
+ items: { type: 'string' },
275
+ description: 'New features list',
276
+ },
277
+ modelMappings: {
278
+ type: 'array',
279
+ items: {
280
+ type: 'object',
281
+ properties: {
282
+ localModel: { type: 'string' },
283
+ layerCakeType: { type: 'string' },
284
+ syncDirection: {
285
+ type: 'string',
286
+ enum: ['push', 'pull', 'bidirectional', 'none'],
287
+ },
288
+ },
289
+ },
290
+ description: 'Model mappings for sync',
291
+ },
292
+ },
293
+ required: ['applicationId'],
294
+ },
295
+ requiresAuth: true,
296
+ requiredPermissions: ['manage_applications'],
297
+ handler: async (params, authContext) => {
298
+ const { applicationId, ...updates } = params;
299
+
300
+ await backendClient.updateApplication(applicationId, updates);
301
+
302
+ return {
303
+ success: true,
304
+ applicationId,
305
+ message: 'Application updated successfully',
306
+ };
307
+ },
308
+ },
309
+
310
+ // ========================================
311
+ // Application Sync
312
+ // ========================================
313
+ {
314
+ name: 'l4yercak3_sync_application',
315
+ description: `Sync application data with L4YERCAK3.
316
+ Triggers a sync based on configured model mappings.`,
317
+ inputSchema: {
318
+ type: 'object',
319
+ properties: {
320
+ applicationId: {
321
+ type: 'string',
322
+ description: 'Application ID',
323
+ },
324
+ direction: {
325
+ type: 'string',
326
+ enum: ['push', 'pull', 'bidirectional'],
327
+ description: 'Sync direction (default: bidirectional)',
328
+ },
329
+ models: {
330
+ type: 'array',
331
+ items: { type: 'string' },
332
+ description: 'Specific models to sync (optional, syncs all if not specified)',
333
+ },
334
+ dryRun: {
335
+ type: 'boolean',
336
+ description: 'Preview sync without making changes',
337
+ },
338
+ },
339
+ required: ['applicationId'],
340
+ },
341
+ requiresAuth: true,
342
+ requiredPermissions: ['manage_applications'],
343
+ handler: async (params, authContext) => {
344
+ const response = await backendClient.syncApplication(params.applicationId, {
345
+ direction: params.direction || 'bidirectional',
346
+ models: params.models,
347
+ dryRun: params.dryRun || false,
348
+ });
349
+
350
+ return {
351
+ syncId: response.syncId,
352
+ dryRun: params.dryRun || false,
353
+ direction: params.direction || 'bidirectional',
354
+ modelMappings: response.modelMappings || [],
355
+ instructions: response.instructions,
356
+ message: params.dryRun
357
+ ? 'Dry run completed - no changes made'
358
+ : 'Sync configuration returned - execute sync in your application',
359
+ };
360
+ },
361
+ },
362
+
363
+ // ========================================
364
+ // Model Mapping Management
365
+ // ========================================
366
+ {
367
+ name: 'l4yercak3_add_model_mapping',
368
+ description: `Add a model mapping to an application.
369
+ Model mappings define how local models sync with L4YERCAK3 types.`,
370
+ inputSchema: {
371
+ type: 'object',
372
+ properties: {
373
+ applicationId: {
374
+ type: 'string',
375
+ description: 'Application ID',
376
+ },
377
+ localModel: {
378
+ type: 'string',
379
+ description: 'Local model name (e.g., "User", "Customer")',
380
+ },
381
+ layerCakeType: {
382
+ type: 'string',
383
+ enum: ['crm_contact', 'crm_organization', 'event', 'form', 'product'],
384
+ description: 'L4YERCAK3 type to map to',
385
+ },
386
+ syncDirection: {
387
+ type: 'string',
388
+ enum: ['push', 'pull', 'bidirectional', 'none'],
389
+ description: 'Sync direction for this mapping',
390
+ },
391
+ fieldMappings: {
392
+ type: 'array',
393
+ items: {
394
+ type: 'object',
395
+ properties: {
396
+ local: { type: 'string' },
397
+ layerCake: { type: 'string' },
398
+ },
399
+ },
400
+ description: 'Field-level mappings',
401
+ },
402
+ },
403
+ required: ['applicationId', 'localModel', 'layerCakeType'],
404
+ },
405
+ requiresAuth: true,
406
+ requiredPermissions: ['manage_applications'],
407
+ handler: async (params, authContext) => {
408
+ // Get current application
409
+ const appResponse = await backendClient.getApplication(params.applicationId);
410
+ const app = appResponse.application || appResponse;
411
+ const currentMappings = app.modelMappings || [];
412
+
413
+ // Check if mapping already exists
414
+ const existingIndex = currentMappings.findIndex(
415
+ m => m.localModel === params.localModel
416
+ );
417
+
418
+ const newMapping = {
419
+ localModel: params.localModel,
420
+ layerCakeType: params.layerCakeType,
421
+ syncDirection: params.syncDirection || 'bidirectional',
422
+ fieldMappings: params.fieldMappings || [],
423
+ isAutoDetected: false,
424
+ };
425
+
426
+ if (existingIndex >= 0) {
427
+ currentMappings[existingIndex] = newMapping;
428
+ } else {
429
+ currentMappings.push(newMapping);
430
+ }
431
+
432
+ // Update application
433
+ await backendClient.updateApplication(params.applicationId, {
434
+ modelMappings: currentMappings,
435
+ });
436
+
437
+ return {
438
+ success: true,
439
+ mappingAdded: existingIndex < 0,
440
+ mappingUpdated: existingIndex >= 0,
441
+ mapping: newMapping,
442
+ message: existingIndex >= 0
443
+ ? `Updated mapping: ${params.localModel} -> ${params.layerCakeType}`
444
+ : `Added mapping: ${params.localModel} -> ${params.layerCakeType}`,
445
+ };
446
+ },
447
+ },
448
+
449
+ {
450
+ name: 'l4yercak3_suggest_model_mappings',
451
+ description: `Analyze project structure and suggest model mappings.
452
+ Looks at database schemas, types, and common patterns.`,
453
+ inputSchema: {
454
+ type: 'object',
455
+ properties: {
456
+ projectPath: {
457
+ type: 'string',
458
+ description: 'Project directory to analyze',
459
+ },
460
+ schemaPath: {
461
+ type: 'string',
462
+ description: 'Path to schema file (e.g., prisma/schema.prisma)',
463
+ },
464
+ },
465
+ },
466
+ requiresAuth: true,
467
+ handler: async (params, authContext) => {
468
+ // This tool provides suggestions based on common patterns
469
+ // In a real implementation, it would analyze actual project files
470
+
471
+ return {
472
+ suggestions: [
473
+ {
474
+ localModel: 'User',
475
+ layerCakeType: 'crm_contact',
476
+ confidence: 0.9,
477
+ reason: 'User models typically map to CRM contacts',
478
+ suggestedFieldMappings: [
479
+ { local: 'email', layerCake: 'email' },
480
+ { local: 'firstName', layerCake: 'firstName' },
481
+ { local: 'lastName', layerCake: 'lastName' },
482
+ { local: 'phone', layerCake: 'phone' },
483
+ ],
484
+ },
485
+ {
486
+ localModel: 'Customer',
487
+ layerCakeType: 'crm_contact',
488
+ confidence: 0.95,
489
+ reason: 'Customer models map directly to CRM contacts',
490
+ suggestedFieldMappings: [
491
+ { local: 'email', layerCake: 'email' },
492
+ { local: 'name', layerCake: 'name' },
493
+ ],
494
+ },
495
+ {
496
+ localModel: 'Company',
497
+ layerCakeType: 'crm_organization',
498
+ confidence: 0.9,
499
+ reason: 'Company models map to CRM organizations',
500
+ suggestedFieldMappings: [
501
+ { local: 'name', layerCake: 'name' },
502
+ { local: 'website', layerCake: 'website' },
503
+ { local: 'industry', layerCake: 'industry' },
504
+ ],
505
+ },
506
+ ],
507
+ instructions: [
508
+ 'Review each suggestion and its confidence level',
509
+ 'Use l4yercak3_add_model_mapping to add mappings you want',
510
+ 'Customize field mappings as needed for your schema',
511
+ ],
512
+ };
513
+ },
514
+ },
515
+ ],
516
+ };