@mtkn/mega-agent 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/README.md +150 -7
  2. package/dist/artifact/artifact.repository.d.ts +2 -1
  3. package/dist/artifact/artifact.repository.d.ts.map +1 -1
  4. package/dist/artifact/artifact.repository.js +1 -1
  5. package/dist/artifact/artifact.repository.js.map +1 -1
  6. package/dist/artifact/artifact.service.d.ts +3 -2
  7. package/dist/artifact/artifact.service.d.ts.map +1 -1
  8. package/dist/artifact/artifact.service.js.map +1 -1
  9. package/dist/artifact/artifact.types.d.ts +2 -1
  10. package/dist/artifact/artifact.types.d.ts.map +1 -1
  11. package/dist/artifact/artifact.types.js.map +1 -1
  12. package/dist/chat/chat-memory.integration.d.ts +3 -2
  13. package/dist/chat/chat-memory.integration.d.ts.map +1 -1
  14. package/dist/chat/chat-memory.integration.js.map +1 -1
  15. package/dist/chat/chat.repository.d.ts +2 -1
  16. package/dist/chat/chat.repository.d.ts.map +1 -1
  17. package/dist/chat/chat.repository.js +1 -1
  18. package/dist/chat/chat.repository.js.map +1 -1
  19. package/dist/chat/chat.service.d.ts +4 -2
  20. package/dist/chat/chat.service.d.ts.map +1 -1
  21. package/dist/chat/chat.service.js +127 -40
  22. package/dist/chat/chat.service.js.map +1 -1
  23. package/dist/chat/chat.types.d.ts +7 -7
  24. package/dist/chat/chat.types.d.ts.map +1 -1
  25. package/dist/chat/chat.types.js.map +1 -1
  26. package/dist/chat/pending-approval.store.d.ts +19 -0
  27. package/dist/chat/pending-approval.store.d.ts.map +1 -0
  28. package/dist/chat/pending-approval.store.js +41 -0
  29. package/dist/chat/pending-approval.store.js.map +1 -0
  30. package/dist/chat/prompts/system.prompts.d.ts.map +1 -1
  31. package/dist/chat/prompts/system.prompts.js +9 -6
  32. package/dist/chat/prompts/system.prompts.js.map +1 -1
  33. package/dist/common/errors.d.ts +3 -0
  34. package/dist/common/errors.d.ts.map +1 -1
  35. package/dist/common/errors.js +6 -0
  36. package/dist/common/errors.js.map +1 -1
  37. package/dist/http/chat.controller.d.ts +1 -1
  38. package/dist/http/chat.controller.d.ts.map +1 -1
  39. package/dist/http/chat.controller.js +15 -3
  40. package/dist/http/chat.controller.js.map +1 -1
  41. package/dist/http/chat.routes.d.ts.map +1 -1
  42. package/dist/http/chat.routes.js +3 -3
  43. package/dist/http/chat.routes.js.map +1 -1
  44. package/dist/http/chat.validation.d.ts +14 -0
  45. package/dist/http/chat.validation.d.ts.map +1 -1
  46. package/dist/http/chat.validation.js +6 -0
  47. package/dist/http/chat.validation.js.map +1 -1
  48. package/dist/incident/incident.correlator.d.ts +3 -2
  49. package/dist/incident/incident.correlator.d.ts.map +1 -1
  50. package/dist/incident/incident.correlator.js.map +1 -1
  51. package/dist/incident/incident.repository.d.ts +5 -4
  52. package/dist/incident/incident.repository.d.ts.map +1 -1
  53. package/dist/incident/incident.repository.js +6 -6
  54. package/dist/incident/incident.repository.js.map +1 -1
  55. package/dist/incident/incident.service.d.ts +3 -2
  56. package/dist/incident/incident.service.d.ts.map +1 -1
  57. package/dist/incident/incident.service.js.map +1 -1
  58. package/dist/incident/incident.types.d.ts +3 -2
  59. package/dist/incident/incident.types.d.ts.map +1 -1
  60. package/dist/index.d.ts +1 -0
  61. package/dist/index.d.ts.map +1 -1
  62. package/dist/index.js.map +1 -1
  63. package/dist/mcp/source.repository.d.ts +5 -4
  64. package/dist/mcp/source.repository.d.ts.map +1 -1
  65. package/dist/mcp/source.repository.js +3 -3
  66. package/dist/mcp/source.repository.js.map +1 -1
  67. package/dist/mcp/source.service.d.ts +3 -2
  68. package/dist/mcp/source.service.d.ts.map +1 -1
  69. package/dist/mcp/source.service.js.map +1 -1
  70. package/dist/memory/memory.repository.d.ts +3 -2
  71. package/dist/memory/memory.repository.d.ts.map +1 -1
  72. package/dist/memory/memory.repository.js +4 -4
  73. package/dist/memory/memory.repository.js.map +1 -1
  74. package/dist/memory/memory.service.d.ts +3 -2
  75. package/dist/memory/memory.service.d.ts.map +1 -1
  76. package/dist/memory/memory.service.js +2 -2
  77. package/dist/memory/memory.service.js.map +1 -1
  78. package/dist/memory/memory.types.d.ts +4 -3
  79. package/dist/memory/memory.types.d.ts.map +1 -1
  80. package/dist/memory/memory.types.js.map +1 -1
  81. package/dist/tools/tool-registry.d.ts +3 -2
  82. package/dist/tools/tool-registry.d.ts.map +1 -1
  83. package/dist/tools/tool-registry.js.map +1 -1
  84. package/dist/types/id.types.d.ts +7 -0
  85. package/dist/types/id.types.d.ts.map +1 -0
  86. package/dist/types/id.types.js +2 -0
  87. package/dist/types/id.types.js.map +1 -0
  88. package/dist/types/index.d.ts +2 -0
  89. package/dist/types/index.d.ts.map +1 -0
  90. package/dist/types/index.js +2 -0
  91. package/dist/types/index.js.map +1 -0
  92. package/dist/workflow/workflow.repository.js.map +1 -1
  93. package/dist/workflow/workflow.service.d.ts.map +1 -1
  94. package/dist/workflow/workflow.service.js +7 -6
  95. package/dist/workflow/workflow.service.js.map +1 -1
  96. package/dist/workflow/workflow.types.d.ts +4 -2
  97. package/dist/workflow/workflow.types.d.ts.map +1 -1
  98. package/dist/workflow/workflow.types.js.map +1 -1
  99. package/package.json +1 -3
  100. package/prisma/schema.prisma +9 -1
  101. package/scripts/prisma-sync.mjs +216 -13
@@ -6,19 +6,27 @@
6
6
  * Reads the mega-agent reference schema and appends/updates
7
7
  * enum and model blocks into the host application's schema.
8
8
  *
9
+ * Supports ID type customization:
10
+ * --id-type Primary key strategy: cuid (default), uuid, autoincrement
11
+ * --user-id-type User ID field type: int (default), string
12
+ *
9
13
  * Usage:
10
- * node scripts/prisma-sync.mjs --target prisma/schema.prisma
11
14
  * npx mega-agent-prisma-sync --target prisma/schema.prisma
15
+ * npx mega-agent-prisma-sync --target prisma/schema.prisma --id-type uuid --user-id-type string
12
16
  *
13
17
  * Options:
14
- * --source Path to mega-agent schema (default: auto-detect)
15
- * --target Path to host schema (required)
16
- * --dry-run Print result without writing
18
+ * --source Path to mega-agent schema (default: auto-detect)
19
+ * --target Path to host schema (required)
20
+ * --id-type Primary key strategy: cuid | uuid | autoincrement (default: cuid)
21
+ * --user-id-type User ID field type: int | string (default: int)
22
+ * --no-interactive Skip interactive prompts, use defaults or flags
23
+ * --dry-run Print result without writing
17
24
  */
18
25
 
19
26
  import { readFileSync, writeFileSync, existsSync } from 'node:fs';
20
- import { resolve, dirname, join } from 'node:path';
27
+ import { resolve, dirname } from 'node:path';
21
28
  import { fileURLToPath } from 'node:url';
29
+ import { createInterface } from 'node:readline/promises';
22
30
 
23
31
  const __filename = fileURLToPath(import.meta.url);
24
32
  const __dirname = dirname(__filename);
@@ -26,17 +34,40 @@ const __dirname = dirname(__filename);
26
34
  const MARKER_START = '// ═══ @7style/mega-agent MODELS START ═══';
27
35
  const MARKER_END = '// ═══ @7style/mega-agent MODELS END ═══';
28
36
 
37
+ const VALID_ID_TYPES = ['cuid', 'uuid', 'autoincrement'];
38
+ const VALID_USER_ID_TYPES = ['int', 'string'];
39
+
40
+ // Foreign key fields that reference mega-agent model IDs (need type change for autoincrement)
41
+ const INTERNAL_FK_FIELDS = {
42
+ AiChatMessage: ['chatId'],
43
+ AiWorkflowExecution: ['workflowId'],
44
+ AiReport: ['artifactId'],
45
+ };
46
+
29
47
  // ── CLI args ──────────────────────────────────────────────────
30
48
 
31
49
  function parseArgs() {
32
50
  const args = process.argv.slice(2);
33
- const parsed = { source: null, target: null, dryRun: false };
51
+ const parsed = {
52
+ source: null,
53
+ target: null,
54
+ idType: null,
55
+ userIdType: null,
56
+ noInteractive: false,
57
+ dryRun: false,
58
+ };
34
59
 
35
60
  for (let i = 0; i < args.length; i++) {
36
61
  if (args[i] === '--source' && args[i + 1]) {
37
62
  parsed.source = args[++i];
38
63
  } else if (args[i] === '--target' && args[i + 1]) {
39
64
  parsed.target = args[++i];
65
+ } else if (args[i] === '--id-type' && args[i + 1]) {
66
+ parsed.idType = args[++i];
67
+ } else if (args[i] === '--user-id-type' && args[i + 1]) {
68
+ parsed.userIdType = args[++i];
69
+ } else if (args[i] === '--no-interactive') {
70
+ parsed.noInteractive = true;
40
71
  } else if (args[i] === '--dry-run') {
41
72
  parsed.dryRun = true;
42
73
  } else if (args[i] === '--help' || args[i] === '-h') {
@@ -45,12 +76,29 @@ function parseArgs() {
45
76
 
46
77
  Usage:
47
78
  npx mega-agent-prisma-sync --target prisma/schema.prisma
79
+ npx mega-agent-prisma-sync --target prisma/schema.prisma --id-type uuid --user-id-type string
48
80
 
49
81
  Options:
50
- --source Path to mega-agent reference schema (auto-detected by default)
51
- --target Path to host schema.prisma (required)
52
- --dry-run Print result without writing to file
53
- --help Show this help message
82
+ --source Path to mega-agent reference schema (auto-detected by default)
83
+ --target Path to host schema.prisma (required)
84
+ --id-type Primary key strategy: cuid | uuid | autoincrement (default: cuid)
85
+ --user-id-type User ID field type: int | string (default: int)
86
+ --no-interactive Skip interactive prompts, use defaults or previous config
87
+ --dry-run Print result without writing to file
88
+ --help Show this help message
89
+
90
+ Examples:
91
+ # Interactive mode (first time setup)
92
+ npx mega-agent-prisma-sync --target prisma/schema.prisma
93
+
94
+ # UUID primary keys + String user IDs (for UUID-based User models)
95
+ npx mega-agent-prisma-sync --target prisma/schema.prisma --id-type uuid --user-id-type string
96
+
97
+ # Autoincrement primary keys (integer IDs)
98
+ npx mega-agent-prisma-sync --target prisma/schema.prisma --id-type autoincrement
99
+
100
+ # CI/CD: skip prompts, use previous config or defaults
101
+ npx mega-agent-prisma-sync --target prisma/schema.prisma --no-interactive
54
102
  `);
55
103
  process.exit(0);
56
104
  }
@@ -59,6 +107,61 @@ Options:
59
107
  return parsed;
60
108
  }
61
109
 
110
+ // ── Config detection from existing markers ────────────────────
111
+
112
+ /**
113
+ * Detect previous ID config from existing marker comments in target schema.
114
+ * Returns { idType, userIdType } or null if not found.
115
+ */
116
+ function detectPreviousConfig(targetSchema) {
117
+ const configMatch = targetSchema.match(
118
+ /\/\/ Config: id-type=(\w+), user-id-type=(\w+)/
119
+ );
120
+ if (configMatch) {
121
+ return {
122
+ idType: configMatch[1],
123
+ userIdType: configMatch[2],
124
+ };
125
+ }
126
+ return null;
127
+ }
128
+
129
+ // ── Interactive prompts ───────────────────────────────────────
130
+
131
+ async function promptForConfig(defaults = {}) {
132
+ const rl = createInterface({
133
+ input: process.stdin,
134
+ output: process.stdout,
135
+ });
136
+
137
+ console.log('\n📋 mega-agent Prisma Schema Configuration\n');
138
+
139
+ const idDefault = defaults.idType || 'cuid';
140
+ const idAnswer = await rl.question(
141
+ ` Primary key strategy for mega-agent models? (cuid/uuid/autoincrement) [${idDefault}]: `
142
+ );
143
+ const idType = idAnswer.trim().toLowerCase() || idDefault;
144
+
145
+ const userIdDefault = defaults.userIdType || 'int';
146
+ const userIdAnswer = await rl.question(
147
+ ` User ID type in your app? (int/string) [${userIdDefault}]: `
148
+ );
149
+ const userIdType = userIdAnswer.trim().toLowerCase() || userIdDefault;
150
+
151
+ rl.close();
152
+
153
+ if (!VALID_ID_TYPES.includes(idType)) {
154
+ console.error(`\nError: Invalid id-type "${idType}". Must be one of: ${VALID_ID_TYPES.join(', ')}`);
155
+ process.exit(1);
156
+ }
157
+ if (!VALID_USER_ID_TYPES.includes(userIdType)) {
158
+ console.error(`\nError: Invalid user-id-type "${userIdType}". Must be one of: ${VALID_USER_ID_TYPES.join(', ')}`);
159
+ process.exit(1);
160
+ }
161
+
162
+ return { idType, userIdType };
163
+ }
164
+
62
165
  // ── Schema parsing ────────────────────────────────────────────
63
166
 
64
167
  /**
@@ -104,16 +207,78 @@ function extractBlocks(schema) {
104
207
  return blocks;
105
208
  }
106
209
 
210
+ // ── Schema transformation ─────────────────────────────────────
211
+
212
+ /**
213
+ * Transform a model block based on ID type configuration.
214
+ * Enum blocks are returned unchanged.
215
+ */
216
+ function transformBlock(block, idType, userIdType) {
217
+ if (block.type === 'enum') return block;
218
+
219
+ const lines = block.content.split('\n');
220
+ const fkFields = INTERNAL_FK_FIELDS[block.name] || [];
221
+
222
+ const transformedLines = lines.map((line) => {
223
+ const trimmed = line.trim();
224
+
225
+ // 1. Transform primary key @id @default(cuid()) lines
226
+ if (trimmed.includes('@id') && trimmed.includes('@default(cuid())')) {
227
+ if (idType === 'uuid') {
228
+ return line.replace('@default(cuid())', '@default(uuid())');
229
+ }
230
+ if (idType === 'autoincrement') {
231
+ return line
232
+ .replace(/(\s+)String(\s+)@id/, '$1Int $2@id')
233
+ .replace('@default(cuid())', '@default(autoincrement())');
234
+ }
235
+ }
236
+
237
+ // 2. Transform userId fields
238
+ if (userIdType === 'string' && /^\s*userId\s+Int\s/.test(line)) {
239
+ return line.replace(/userId(\s+)Int/, 'userId$1String');
240
+ }
241
+
242
+ // 3. Transform internal FK fields when idType is autoincrement
243
+ if (idType === 'autoincrement') {
244
+ for (const fkField of fkFields) {
245
+ // Match: fieldName String (with optional ?)
246
+ const fkPattern = new RegExp(`^(\\s*${fkField}\\s+)String(\\??)(\\s)`);
247
+ if (fkPattern.test(line)) {
248
+ return line.replace(fkPattern, `$1Int$2 $3`);
249
+ }
250
+ }
251
+ }
252
+
253
+ return line;
254
+ });
255
+
256
+ return {
257
+ ...block,
258
+ content: transformedLines.join('\n'),
259
+ };
260
+ }
261
+
107
262
  // ── Main ──────────────────────────────────────────────────────
108
263
 
109
- function main() {
110
- const { source, target, dryRun } = parseArgs();
264
+ async function main() {
265
+ const { source, target, idType: flagIdType, userIdType: flagUserIdType, noInteractive, dryRun } = parseArgs();
111
266
 
112
267
  if (!target) {
113
268
  console.error('Error: --target is required. Example: --target prisma/schema.prisma');
114
269
  process.exit(1);
115
270
  }
116
271
 
272
+ // Validate flags if provided
273
+ if (flagIdType && !VALID_ID_TYPES.includes(flagIdType)) {
274
+ console.error(`Error: Invalid --id-type "${flagIdType}". Must be one of: ${VALID_ID_TYPES.join(', ')}`);
275
+ process.exit(1);
276
+ }
277
+ if (flagUserIdType && !VALID_USER_ID_TYPES.includes(flagUserIdType)) {
278
+ console.error(`Error: Invalid --user-id-type "${flagUserIdType}". Must be one of: ${VALID_USER_ID_TYPES.join(', ')}`);
279
+ process.exit(1);
280
+ }
281
+
117
282
  // Resolve source path
118
283
  const sourcePath = source
119
284
  ? resolve(source)
@@ -138,17 +303,55 @@ function main() {
138
303
  const sourceSchema = readFileSync(sourcePath, 'utf-8');
139
304
  const targetSchema = readFileSync(targetPath, 'utf-8');
140
305
 
306
+ // ── Determine ID config ───────────────────────────────────
307
+ let idType, userIdType;
308
+
309
+ if (flagIdType && flagUserIdType) {
310
+ // Both flags provided → use them directly
311
+ idType = flagIdType;
312
+ userIdType = flagUserIdType;
313
+ } else {
314
+ // Try to detect from existing markers
315
+ const previousConfig = detectPreviousConfig(targetSchema);
316
+
317
+ if (flagIdType || flagUserIdType) {
318
+ // Partial flags: use flag + previous/default for the other
319
+ idType = flagIdType || previousConfig?.idType || 'cuid';
320
+ userIdType = flagUserIdType || previousConfig?.userIdType || 'int';
321
+ } else if (noInteractive) {
322
+ // No interactive: use previous config or defaults
323
+ idType = previousConfig?.idType || 'cuid';
324
+ userIdType = previousConfig?.userIdType || 'int';
325
+ if (previousConfig) {
326
+ console.log(`\nUsing previous config: id-type=${idType}, user-id-type=${userIdType}`);
327
+ } else {
328
+ console.log(`\nUsing defaults: id-type=${idType}, user-id-type=${userIdType}`);
329
+ }
330
+ } else {
331
+ // Interactive mode
332
+ const config = await promptForConfig(previousConfig || {});
333
+ idType = config.idType;
334
+ userIdType = config.userIdType;
335
+ }
336
+ }
337
+
338
+ console.log(`\nConfig: id-type=${idType}, user-id-type=${userIdType}`);
339
+
141
340
  // Extract blocks from source
142
341
  const blocks = extractBlocks(sourceSchema);
143
342
  console.log(`Found ${blocks.length} blocks (${blocks.filter(b => b.type === 'enum').length} enums, ${blocks.filter(b => b.type === 'model').length} models)`);
144
343
 
344
+ // Transform blocks based on config
345
+ const transformedBlocks = blocks.map((b) => transformBlock(b, idType, userIdType));
346
+
145
347
  // Build the managed section
146
348
  const managedContent = [
147
349
  MARKER_START,
148
350
  '// Auto-generated by @7style/mega-agent prisma-sync. Do not edit manually.',
149
351
  `// Last sync: ${new Date().toISOString()}`,
352
+ `// Config: id-type=${idType}, user-id-type=${userIdType}`,
150
353
  '',
151
- ...blocks.map(b => b.content),
354
+ ...transformedBlocks.map(b => b.content),
152
355
  '',
153
356
  MARKER_END,
154
357
  ].join('\n');