@objectql/cli 1.7.2 → 1.7.3

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,508 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import * as yaml from 'js-yaml';
4
+ import * as readline from 'readline';
5
+ import chalk from 'chalk';
6
+ import OpenAI from 'openai';
7
+ import { Validator, ObjectQLAgent } from '@objectql/core';
8
+ import { glob } from 'fast-glob';
9
+
10
+ /**
11
+ * Create an ObjectQL AI agent instance
12
+ */
13
+ export function createAgent(apiKey: string): ObjectQLAgent {
14
+ return new ObjectQLAgent({ apiKey });
15
+ }
16
+
17
+ interface GenerateOptions {
18
+ description: string;
19
+ output?: string;
20
+ type?: 'basic' | 'complete' | 'custom';
21
+ }
22
+
23
+ interface ValidateOptions {
24
+ path: string;
25
+ fix?: boolean;
26
+ verbose?: boolean;
27
+ }
28
+
29
+ interface ChatOptions {
30
+ initialPrompt?: string;
31
+ }
32
+
33
+ interface ConversationalOptions {
34
+ output?: string;
35
+ }
36
+
37
+ /**
38
+ * Conversational generation with step-by-step refinement
39
+ */
40
+ export async function aiConversational(options: ConversationalOptions): Promise<void> {
41
+ const apiKey = process.env.OPENAI_API_KEY;
42
+ if (!apiKey) {
43
+ console.error(chalk.red('Error: OPENAI_API_KEY environment variable is not set.'));
44
+ console.log(chalk.yellow('\nPlease set your OpenAI API key:'));
45
+ console.log(chalk.cyan(' export OPENAI_API_KEY=your-api-key-here'));
46
+ process.exit(1);
47
+ }
48
+
49
+ const outputDir = options.output || './src';
50
+ const agent = createAgent(apiKey);
51
+
52
+ console.log(chalk.blue('šŸ’¬ ObjectQL Conversational Generator\n'));
53
+ console.log(chalk.gray('Build your application step by step through conversation.'));
54
+ console.log(chalk.gray('Type "done" to finish and save, "exit" to quit without saving.\n'));
55
+
56
+ const rl = readline.createInterface({
57
+ input: process.stdin,
58
+ output: process.stdout,
59
+ });
60
+
61
+ let conversationHistory: any[] = [];
62
+ let currentApp: any = null;
63
+ let fileCount = 0;
64
+
65
+ const askQuestion = () => {
66
+ const prompt = currentApp
67
+ ? chalk.cyan('\nWhat would you like to change or add? ')
68
+ : chalk.cyan('Describe your application: ');
69
+
70
+ rl.question(prompt, async (input: string) => {
71
+ if (input.toLowerCase() === 'exit') {
72
+ console.log(chalk.blue('\nšŸ‘‹ Goodbye! No files were saved.'));
73
+ rl.close();
74
+ return;
75
+ }
76
+
77
+ if (input.toLowerCase() === 'done') {
78
+ if (!currentApp || !currentApp.files || currentApp.files.length === 0) {
79
+ console.log(chalk.yellow('\nāš ļø No application generated yet. Continue the conversation or type "exit" to quit.'));
80
+ askQuestion();
81
+ return;
82
+ }
83
+
84
+ // Save files
85
+ console.log(chalk.yellow('\nšŸ’¾ Saving files...'));
86
+
87
+ if (!fs.existsSync(outputDir)) {
88
+ fs.mkdirSync(outputDir, { recursive: true });
89
+ }
90
+
91
+ for (const file of currentApp.files) {
92
+ const filePath = path.join(outputDir, file.filename);
93
+ const fileDir = path.dirname(filePath);
94
+
95
+ if (!fs.existsSync(fileDir)) {
96
+ fs.mkdirSync(fileDir, { recursive: true });
97
+ }
98
+
99
+ fs.writeFileSync(filePath, file.content);
100
+ console.log(chalk.green(` āœ“ ${file.filename}`));
101
+ }
102
+
103
+ console.log(chalk.blue(`\nāœ… Application saved to: ${outputDir}`));
104
+ console.log(chalk.gray('\nNext steps:'));
105
+ console.log(chalk.cyan(' 1. Review the generated files'));
106
+ console.log(chalk.cyan(' 2. Run: objectql ai validate ' + outputDir));
107
+ console.log(chalk.cyan(' 3. Test with: objectql serve --dir ' + outputDir));
108
+
109
+ rl.close();
110
+ return;
111
+ }
112
+
113
+ if (!input.trim()) {
114
+ askQuestion();
115
+ return;
116
+ }
117
+
118
+ console.log(chalk.yellow('\nā³ Generating...'));
119
+
120
+ try {
121
+ const result = await agent.generateConversational({
122
+ message: input,
123
+ conversationHistory,
124
+ currentApp,
125
+ });
126
+
127
+ if (!result.success) {
128
+ console.error(chalk.red('\nāŒ Error:'), result.errors?.join(', ') || 'Unknown error');
129
+ askQuestion();
130
+ return;
131
+ }
132
+
133
+ conversationHistory = result.conversationHistory;
134
+ currentApp = result;
135
+ fileCount = result.files.length;
136
+
137
+ console.log(chalk.green(`\nāœ… Generated/Updated ${fileCount} file(s):`));
138
+
139
+ // Group files by type
140
+ const filesByType: Record<string, string[]> = {};
141
+ result.files.forEach(f => {
142
+ if (!filesByType[f.type]) filesByType[f.type] = [];
143
+ filesByType[f.type].push(f.filename);
144
+ });
145
+
146
+ Object.entries(filesByType).forEach(([type, files]) => {
147
+ console.log(chalk.cyan(` ${type}:`), files.join(', '));
148
+ });
149
+
150
+ // Show suggestions
151
+ if (result.suggestions && result.suggestions.length > 0) {
152
+ console.log(chalk.blue('\nšŸ’” Suggestions:'));
153
+ result.suggestions.forEach(s => console.log(chalk.gray(` • ${s}`)));
154
+ }
155
+
156
+ console.log(chalk.gray('\nYou can now:'));
157
+ console.log(chalk.gray(' • Request changes (e.g., "Add email validation to user")'));
158
+ console.log(chalk.gray(' • Add features (e.g., "Add a workflow for approval")'));
159
+ console.log(chalk.gray(' • Type "done" to save files'));
160
+ console.log(chalk.gray(' • Type "exit" to quit without saving'));
161
+
162
+ } catch (error) {
163
+ console.error(chalk.red('\nāŒ Error:'), error instanceof Error ? error.message : 'Unknown error');
164
+ }
165
+
166
+ askQuestion();
167
+ });
168
+ };
169
+
170
+ askQuestion();
171
+ }
172
+
173
+ /**
174
+ * Generate application metadata using AI
175
+ */
176
+ export async function aiGenerate(options: GenerateOptions): Promise<void> {
177
+ const apiKey = process.env.OPENAI_API_KEY;
178
+ if (!apiKey) {
179
+ console.error(chalk.red('Error: OPENAI_API_KEY environment variable is not set.'));
180
+ console.log(chalk.yellow('\nPlease set your OpenAI API key:'));
181
+ console.log(chalk.cyan(' export OPENAI_API_KEY=your-api-key-here'));
182
+ process.exit(1);
183
+ }
184
+
185
+ const outputDir = options.output || './src';
186
+
187
+ console.log(chalk.blue('šŸ¤– ObjectQL AI Generator\n'));
188
+ console.log(chalk.gray(`Description: ${options.description}`));
189
+ console.log(chalk.gray(`Output directory: ${outputDir}\n`));
190
+
191
+ console.log(chalk.yellow('ā³ Generating metadata...'));
192
+
193
+ try {
194
+ const agent = createAgent(apiKey);
195
+ const result = await agent.generateApp({
196
+ description: options.description,
197
+ type: options.type || 'custom',
198
+ });
199
+
200
+ if (!result.success || result.files.length === 0) {
201
+ console.log(chalk.yellow('\nāš ļø No valid metadata files generated.'));
202
+ if (result.errors) {
203
+ result.errors.forEach(err => console.error(chalk.red(` Error: ${err}`)));
204
+ }
205
+ if (result.rawResponse) {
206
+ console.log(chalk.gray('\nResponse:'));
207
+ console.log(result.rawResponse);
208
+ }
209
+ return;
210
+ }
211
+
212
+ // Create output directory if it doesn't exist
213
+ if (!fs.existsSync(outputDir)) {
214
+ fs.mkdirSync(outputDir, { recursive: true });
215
+ }
216
+
217
+ // Write files
218
+ console.log(chalk.green('\nāœ… Generated files:'));
219
+ for (const file of result.files) {
220
+ const filePath = path.join(outputDir, file.filename);
221
+ const fileDir = path.dirname(filePath);
222
+
223
+ if (!fs.existsSync(fileDir)) {
224
+ fs.mkdirSync(fileDir, { recursive: true });
225
+ }
226
+
227
+ fs.writeFileSync(filePath, file.content);
228
+ console.log(chalk.green(` āœ“ ${file.filename} (${file.type})`));
229
+ }
230
+
231
+ console.log(chalk.blue(`\nšŸ“ Files written to: ${outputDir}`));
232
+ console.log(chalk.gray('\nNext steps:'));
233
+ console.log(chalk.cyan(' 1. Review the generated files'));
234
+ console.log(chalk.cyan(' 2. Run: objectql ai validate <path>'));
235
+ console.log(chalk.cyan(' 3. Test with: objectql serve'));
236
+
237
+ } catch (error) {
238
+ console.error(chalk.red('\nāŒ Error generating metadata:'));
239
+ if (error instanceof Error) {
240
+ console.error(chalk.red(error.message));
241
+ }
242
+ process.exit(1);
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Validate metadata files using AI
248
+ */
249
+ export async function aiValidate(options: ValidateOptions): Promise<void> {
250
+ const apiKey = process.env.OPENAI_API_KEY;
251
+
252
+ if (!apiKey) {
253
+ console.error(chalk.red('Error: OPENAI_API_KEY environment variable is not set.'));
254
+ console.log(chalk.yellow('\nNote: AI validation requires OpenAI API key.'));
255
+ console.log(chalk.yellow('Falling back to basic validation...\n'));
256
+ await basicValidate(options);
257
+ return;
258
+ }
259
+
260
+ console.log(chalk.blue('šŸ” ObjectQL AI Validator\n'));
261
+
262
+ // Find all metadata files
263
+ const patterns = [
264
+ '**/*.object.yml',
265
+ '**/*.validation.yml',
266
+ '**/*.form.yml',
267
+ '**/*.view.yml',
268
+ '**/*.page.yml',
269
+ '**/*.action.yml',
270
+ ];
271
+
272
+ const files = await glob(patterns, {
273
+ cwd: options.path,
274
+ absolute: true,
275
+ ignore: ['**/node_modules/**', '**/dist/**', '**/build/**'],
276
+ });
277
+
278
+ if (files.length === 0) {
279
+ console.log(chalk.yellow('No metadata files found.'));
280
+ return;
281
+ }
282
+
283
+ console.log(chalk.gray(`Found ${files.length} metadata file(s)\n`));
284
+
285
+ const agent = createAgent(apiKey);
286
+ let errorCount = 0;
287
+ let warningCount = 0;
288
+
289
+ for (const filePath of files) {
290
+ const relativePath = path.relative(options.path, filePath);
291
+ console.log(chalk.cyan(`\nšŸ“„ ${relativePath}`));
292
+
293
+ try {
294
+ const content = fs.readFileSync(filePath, 'utf-8');
295
+
296
+ // Validate using AI agent
297
+ const result = await agent.validateMetadata({
298
+ metadata: content,
299
+ filename: relativePath,
300
+ checkBusinessLogic: true,
301
+ checkPerformance: true,
302
+ checkSecurity: true,
303
+ });
304
+
305
+ // Display results
306
+ if (result.errors.length > 0) {
307
+ result.errors.forEach(error => {
308
+ console.log(chalk.red(` āŒ ERROR: ${error.message}`));
309
+ if (error.location) {
310
+ console.log(chalk.gray(` Location: ${error.location}`));
311
+ }
312
+ });
313
+ errorCount += result.errors.length;
314
+ }
315
+
316
+ if (result.warnings.length > 0) {
317
+ result.warnings.forEach(warning => {
318
+ console.log(chalk.yellow(` āš ļø WARNING: ${warning.message}`));
319
+ if (warning.suggestion) {
320
+ console.log(chalk.gray(` Suggestion: ${warning.suggestion}`));
321
+ }
322
+ });
323
+ warningCount += result.warnings.length;
324
+ }
325
+
326
+ if (options.verbose && result.info.length > 0) {
327
+ result.info.forEach(info => {
328
+ console.log(chalk.blue(` ā„¹ļø INFO: ${info.message}`));
329
+ });
330
+ }
331
+
332
+ if (result.valid && result.warnings.length === 0) {
333
+ console.log(chalk.green(' āœ“ No issues found'));
334
+ }
335
+
336
+ } catch (error) {
337
+ console.log(chalk.red(` āŒ Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
338
+ errorCount++;
339
+ }
340
+ }
341
+
342
+ // Summary
343
+ console.log(chalk.blue('\n' + '='.repeat(60)));
344
+ console.log(chalk.blue('Validation Summary:'));
345
+ console.log(chalk.gray(` Files checked: ${files.length}`));
346
+
347
+ if (errorCount > 0) {
348
+ console.log(chalk.red(` Errors: ${errorCount}`));
349
+ }
350
+ if (warningCount > 0) {
351
+ console.log(chalk.yellow(` Warnings: ${warningCount}`));
352
+ }
353
+ if (errorCount === 0 && warningCount === 0) {
354
+ console.log(chalk.green(' āœ“ All files validated successfully!'));
355
+ }
356
+
357
+ if (errorCount > 0) {
358
+ process.exit(1);
359
+ }
360
+ }
361
+
362
+ /**
363
+ * Basic validation without AI (fallback)
364
+ */
365
+ async function basicValidate(options: ValidateOptions): Promise<void> {
366
+ const patterns = [
367
+ '**/*.object.yml',
368
+ '**/*.validation.yml',
369
+ ];
370
+
371
+ const files = await glob(patterns, {
372
+ cwd: options.path,
373
+ absolute: true,
374
+ ignore: ['**/node_modules/**', '**/dist/**', '**/build/**'],
375
+ });
376
+
377
+ if (files.length === 0) {
378
+ console.log(chalk.yellow('No metadata files found.'));
379
+ return;
380
+ }
381
+
382
+ console.log(chalk.gray(`Found ${files.length} metadata file(s)\n`));
383
+
384
+ let errorCount = 0;
385
+ const validator = new Validator({ language: 'en' });
386
+
387
+ for (const filePath of files) {
388
+ const relativePath = path.relative(options.path, filePath);
389
+ console.log(chalk.cyan(`šŸ“„ ${relativePath}`));
390
+
391
+ try {
392
+ const content = fs.readFileSync(filePath, 'utf-8');
393
+ const data = yaml.load(content) as any;
394
+
395
+ // Validate YAML structure
396
+ if (!data || typeof data !== 'object') {
397
+ console.log(chalk.red(' āŒ Invalid YAML structure'));
398
+ errorCount++;
399
+ continue;
400
+ }
401
+
402
+ // Validate based on file type
403
+ if (filePath.endsWith('.validation.yml')) {
404
+ if (!data.rules || !Array.isArray(data.rules)) {
405
+ console.log(chalk.yellow(' āš ļø No validation rules found'));
406
+ } else {
407
+ console.log(chalk.green(` āœ“ ${data.rules.length} validation rule(s) found`));
408
+ }
409
+ } else if (filePath.endsWith('.object.yml')) {
410
+ if (!data.fields || typeof data.fields !== 'object') {
411
+ console.log(chalk.red(' āŒ No fields defined'));
412
+ errorCount++;
413
+ } else {
414
+ const fieldCount = Object.keys(data.fields).length;
415
+ console.log(chalk.green(` āœ“ ${fieldCount} field(s) defined`));
416
+ }
417
+ }
418
+
419
+ } catch (error) {
420
+ console.log(chalk.red(` āŒ Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
421
+ errorCount++;
422
+ }
423
+ }
424
+
425
+ console.log(chalk.blue('\n' + '='.repeat(60)));
426
+ if (errorCount === 0) {
427
+ console.log(chalk.green('āœ“ Basic validation passed'));
428
+ } else {
429
+ console.log(chalk.red(`āŒ Found ${errorCount} error(s)`));
430
+ process.exit(1);
431
+ }
432
+ }
433
+
434
+ /**
435
+ * Interactive AI chat for metadata assistance
436
+ */
437
+ export async function aiChat(options: ChatOptions): Promise<void> {
438
+ const apiKey = process.env.OPENAI_API_KEY;
439
+ if (!apiKey) {
440
+ console.error(chalk.red('Error: OPENAI_API_KEY environment variable is not set.'));
441
+ process.exit(1);
442
+ }
443
+
444
+ const openai = new OpenAI({ apiKey });
445
+
446
+ console.log(chalk.blue('šŸ’¬ ObjectQL AI Assistant\n'));
447
+ console.log(chalk.gray('Ask me anything about ObjectQL metadata, data modeling, or best practices.'));
448
+ console.log(chalk.gray('Type "exit" to quit.\n'));
449
+
450
+ const rl = readline.createInterface({
451
+ input: process.stdin,
452
+ output: process.stdout,
453
+ });
454
+
455
+ const systemPrompt = `You are an expert ObjectQL architect and consultant. Help users with:
456
+ - ObjectQL metadata specifications
457
+ - Data modeling best practices
458
+ - Validation rules and business logic
459
+ - Relationships and field types
460
+ - Application architecture
461
+ - Performance and security considerations
462
+
463
+ Provide clear, actionable advice with examples when appropriate.`;
464
+
465
+ const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
466
+ { role: 'system', content: systemPrompt }
467
+ ];
468
+
469
+ if (options.initialPrompt) {
470
+ messages.push({ role: 'user', content: options.initialPrompt });
471
+ }
472
+
473
+ const askQuestion = () => {
474
+ rl.question(chalk.cyan('You: '), async (input: string) => {
475
+ if (input.toLowerCase() === 'exit') {
476
+ console.log(chalk.blue('\nGoodbye! šŸ‘‹'));
477
+ rl.close();
478
+ return;
479
+ }
480
+
481
+ if (!input.trim()) {
482
+ askQuestion();
483
+ return;
484
+ }
485
+
486
+ messages.push({ role: 'user', content: input });
487
+
488
+ try {
489
+ const completion = await openai.chat.completions.create({
490
+ model: 'gpt-4',
491
+ messages: messages,
492
+ temperature: 0.7,
493
+ });
494
+
495
+ const response = completion.choices[0]?.message?.content || 'No response';
496
+ messages.push({ role: 'assistant', content: response });
497
+
498
+ console.log(chalk.green('\nAssistant: ') + response + '\n');
499
+ } catch (error) {
500
+ console.error(chalk.red('\nError: ') + (error instanceof Error ? error.message : 'Unknown error') + '\n');
501
+ }
502
+
503
+ askQuestion();
504
+ });
505
+ };
506
+
507
+ askQuestion();
508
+ }
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ import { initProject } from './commands/init';
7
7
  import { newMetadata } from './commands/new';
8
8
  import { i18nExtract, i18nInit, i18nValidate } from './commands/i18n';
9
9
  import { migrate, migrateCreate, migrateStatus } from './commands/migrate';
10
+ import { aiGenerate, aiValidate, aiChat, aiConversational } from './commands/ai';
10
11
 
11
12
  const program = new Command();
12
13
 
@@ -189,4 +190,67 @@ program
189
190
  });
190
191
  });
191
192
 
193
+ // AI command - Interactive by default, with specific subcommands for other modes
194
+ const aiCmd = program
195
+ .command('ai')
196
+ .description('AI-powered interactive application builder (starts conversational mode by default)');
197
+
198
+ // Default action: Interactive conversational mode
199
+ aiCmd
200
+ .argument('[output-dir]', 'Output directory for generated files', './src')
201
+ .action(async (outputDir) => {
202
+ try {
203
+ await aiConversational({ output: outputDir });
204
+ } catch (error) {
205
+ console.error(error);
206
+ process.exit(1);
207
+ }
208
+ });
209
+
210
+ // Subcommand: Generate (one-shot generation)
211
+ aiCmd
212
+ .command('generate')
213
+ .description('Generate application from description (one-shot, non-interactive)')
214
+ .requiredOption('-d, --description <text>', 'Application description')
215
+ .option('-o, --output <path>', 'Output directory', './src')
216
+ .option('-t, --type <type>', 'Generation type: basic, complete, or custom', 'custom')
217
+ .action(async (options) => {
218
+ try {
219
+ await aiGenerate(options);
220
+ } catch (error) {
221
+ console.error(error);
222
+ process.exit(1);
223
+ }
224
+ });
225
+
226
+ // Subcommand: Validate
227
+ aiCmd
228
+ .command('validate')
229
+ .description('Validate metadata files with AI analysis')
230
+ .argument('<path>', 'Path to metadata files directory')
231
+ .option('--fix', 'Automatically fix issues')
232
+ .option('-v, --verbose', 'Detailed output')
233
+ .action(async (pathArg, options) => {
234
+ try {
235
+ await aiValidate({ path: pathArg, ...options });
236
+ } catch (error) {
237
+ console.error(error);
238
+ process.exit(1);
239
+ }
240
+ });
241
+
242
+ // Subcommand: Chat
243
+ aiCmd
244
+ .command('chat')
245
+ .description('AI assistant for questions and guidance')
246
+ .option('-p, --prompt <text>', 'Initial prompt')
247
+ .action(async (options) => {
248
+ try {
249
+ await aiChat({ initialPrompt: options.prompt });
250
+ } catch (error) {
251
+ console.error(error);
252
+ process.exit(1);
253
+ }
254
+ });
255
+
192
256
  program.parse();