forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.ts
More file actions
915 lines (799 loc) · 30.3 KB
/
start.ts
File metadata and controls
915 lines (799 loc) · 30.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
import { Command } from 'commander';
import inquirer from 'inquirer';
import ora, { type Ora } from 'ora';
import chalk from 'chalk';
import { Config } from '../utils/config.js';
import { createOutput } from '../utils/terminal.js';
import { handleError } from '../utils/errors.js';
import { Analytics } from '../utils/analytics.js';
import { MessageBus } from '@auto-engineer/message-bus';
import { createFlowCommandHandler, CreateFlowCommand } from '@auto-engineer/flowlang-agent';
import { type AppSchema } from '@auto-engineer/flowlang';
// Type definitions for better type safety
interface Flow {
name: string;
slices?: Slice[];
}
interface Slice {
name: string;
stream?: string;
integrations?: string[];
specs?: {
client?: ClientSpec;
server?: ServerSpec;
};
client?: ClientSpec;
server?: ServerSpec;
}
interface ClientSpec {
component?: string;
description?: string;
name?: string;
should?: string[];
specs?: string[];
}
interface ServerSpec {
description?: string;
name?: string;
gwt?: GWT;
transitions?: string[];
specs?: string[];
}
interface GWT {
given?: EventRef[];
when?: CommandRef | EventRef[];
then?: EventRef[] | StateRef[];
}
interface EventRef {
eventRef?: string;
name?: string;
}
interface CommandRef {
commandRef?: string;
name?: string;
}
interface StateRef {
stateRef?: string;
name?: string;
}
interface Message {
type: 'command' | 'event' | 'state';
name: string;
description?: string;
fields?: Field[];
}
interface Field {
name: string;
type: string;
required?: boolean;
description?: string;
}
interface Integration {
name: string;
description?: string;
source?: string;
}
type SpecsSchema = AppSchema & {
messages?: Message[];
integrations?: Integration[];
};
// Color constants matching demo.ts
const COLORS = {
SPECS_TEXT: chalk.hex('#A0A0A0'),
SPECS_LABEL: chalk.italic,
EVENTS: chalk.hex('#FFA500'),
COMMANDS: chalk.hex('#7FDBFF'),
STATE: chalk.green,
FLOW_TEXT: chalk.bold.blue,
STREAM_BRACKETS: chalk.yellow,
INTEGRATIONS_BRACKETS: chalk.magenta,
SOURCE_BRACKETS: chalk.gray,
SLICE_TEXT: chalk.white.bold,
CLIENT_SERVER: chalk.hex('#4ECDC4'),
};
// Type guards
const isFlow = (obj: unknown): obj is Flow => {
return (
typeof obj === 'object' &&
obj !== null &&
'name' in obj &&
typeof (obj as Flow).name === 'string' &&
(obj as Flow).name.length > 0
);
};
const isSlice = (obj: unknown): obj is Slice => {
return (
typeof obj === 'object' &&
obj !== null &&
'name' in obj &&
typeof (obj as Slice).name === 'string' &&
(obj as Slice).name.length > 0
);
};
const isCommandRef = (obj: unknown): obj is CommandRef => {
return typeof obj === 'object' && obj !== null && ('commandRef' in obj || 'name' in obj);
};
// Helper functions to break down complexity
const addFlowHeader = (flow: Flow, flowIndex: number, lines: string[]): void => {
if (flowIndex > 0) lines.push(''); // Add spacing between flows
lines.push(`# **Flow:** ${flow.name}`);
};
const addSliceInfo = (slice: Slice, lines: string[]): void => {
let sliceLine = `* **Slice:** ${slice.name}`;
if (slice.stream != null && slice.stream.length > 0) {
sliceLine += ` [stream: ${slice.stream}]`;
}
if (slice.integrations != null && slice.integrations.length > 0) {
sliceLine += ` [integrations: ${slice.integrations.join(', ')}]`;
}
lines.push(sliceLine);
};
const addClientSpecs = (clientData: ClientSpec, lines: string[]): void => {
const clientDesc = clientData.component ?? clientData.description ?? clientData.name ?? 'Component';
lines.push(` * **Client:** ${clientDesc}`);
// Handle "should" array or specs array
const specs = clientData.should ?? clientData.specs ?? [];
if (Array.isArray(specs)) {
specs.forEach((spec: string) => {
if (spec?.length > 0) {
lines.push(` ${spec}`);
}
});
}
};
// Helper to process single event transition
const processSingleEventTransition = (event: unknown, commandName: string, transitions: string[]): void => {
if (typeof event === 'object' && event !== null && 'eventRef' in event) {
const eventName = (event as EventRef).eventRef ?? (event as EventRef).name;
if (eventName != null && eventName.length > 0) {
transitions.push(`Commands.${commandName} => Events.${eventName}`);
}
}
};
const processCommandTransitions = (gwt: GWT, transitions: string[]): void => {
if (gwt.when && isCommandRef(gwt.when) && gwt.then) {
const commandName = gwt.when.commandRef ?? gwt.when.name ?? 'Command';
if (Array.isArray(gwt.then)) {
gwt.then.forEach((event: unknown) => {
processSingleEventTransition(event, commandName, transitions);
});
} else if (gwt.then) {
processSingleEventTransition(gwt.then, commandName, transitions);
}
}
};
// Helper to process state transitions from event
const processStateTransitions = (eventName: string, then: unknown[] | unknown, transitions: string[]): void => {
if (Array.isArray(then)) {
then.forEach((state: unknown) => {
if (typeof state === 'object' && state !== null && 'stateRef' in state) {
const stateName = (state as StateRef).stateRef ?? (state as StateRef).name;
if (stateName != null && stateName.length > 0) {
transitions.push(`Events.${eventName} => State.${stateName}`);
}
}
});
} else if (then != null) {
const stateName = (then as StateRef).stateRef ?? (then as StateRef).name;
if (stateName != null && stateName.length > 0) {
transitions.push(`Events.${eventName} => State.${stateName}`);
}
}
};
const processQueryTransitions = (gwt: GWT, transitions: string[]): void => {
if (gwt.given && gwt.then && !gwt.when) {
if (Array.isArray(gwt.given)) {
gwt.given.forEach((event: unknown) => {
if (typeof event === 'object' && event !== null && 'eventRef' in event) {
const eventName = (event as EventRef).eventRef ?? (event as EventRef).name;
if (eventName != null && eventName.length > 0) {
processStateTransitions(eventName, gwt.then, transitions);
}
}
});
}
}
};
const processReactTransitions = (gwt: GWT, transitions: string[]): void => {
if (gwt.when && gwt.then && Array.isArray(gwt.when)) {
gwt.when.forEach((event: unknown) => {
if (typeof event === 'object' && event !== null && 'eventRef' in event) {
const eventName = (event as EventRef).eventRef ?? (event as EventRef).name;
if (eventName != null && eventName.length > 0) {
if (Array.isArray(gwt.then)) {
gwt.then.forEach((command: unknown) => {
if (typeof command === 'object' && command !== null && 'commandRef' in command) {
const commandName = (command as CommandRef).commandRef ?? (command as CommandRef).name;
if (commandName != null && commandName.length > 0) {
transitions.push(`Events.${eventName} => Commands.${commandName}`);
}
}
});
}
}
}
});
}
};
const addServerSpecs = (serverData: ServerSpec, lines: string[]): void => {
const serverDesc = serverData.description ?? serverData.name ?? 'Server logic';
lines.push(` * **Server:** ${serverDesc}`);
// Extract GWT (Given-When-Then) and convert to transitions
const transitions: string[] = [];
if (serverData.gwt) {
const gwt = serverData.gwt;
processCommandTransitions(gwt, transitions);
processQueryTransitions(gwt, transitions);
processReactTransitions(gwt, transitions);
}
// Also check for pre-formatted transitions
const directTransitions = serverData.transitions || serverData.specs || [];
if (Array.isArray(directTransitions)) {
transitions.push(...directTransitions);
}
// Display transitions
if (transitions.length > 0) {
transitions.forEach((transition: string) => {
lines.push(` ${transition}`);
});
}
};
const addSliceSpecs = (slice: Slice, lines: string[]): void => {
if (slice.specs || slice.client || slice.server) {
// Client specs
const clientData = slice.specs?.client || slice.client;
if (clientData) {
addClientSpecs(clientData, lines);
}
// Server specs
const serverData = slice.specs?.server || slice.server;
if (serverData) {
addServerSpecs(serverData, lines);
}
}
};
const addMessageFields = (message: Message, lines: string[]): void => {
if (message.fields && Array.isArray(message.fields)) {
message.fields.forEach((field: Field) => {
const required = field.required !== false ? ' (required)' : ' (optional)';
const description = field.description != null && field.description.length > 0 ? ` - ${field.description}` : '';
lines.push(` - ${field.name}: ${field.type}${required}${description}`);
});
}
};
const addMessageGroup = (messages: Message[], type: string, lines: string[]): void => {
if (messages.length > 0) {
lines.push(`* **${type}:**`);
messages.forEach((msg: Message) => {
const description = msg.description != null && msg.description.length > 0 ? `: ${msg.description}` : '';
lines.push(` * **${msg.name}**${description}`);
addMessageFields(msg, lines);
});
}
};
const addMessages = (specsSchema: SpecsSchema, lines: string[]): void => {
if (specsSchema.messages && Array.isArray(specsSchema.messages) && specsSchema.messages.length > 0) {
lines.push('');
lines.push('# **Messages**');
// Group messages by type
const commands = specsSchema.messages.filter((m: Message) => m.type === 'command');
const events = specsSchema.messages.filter((m: Message) => m.type === 'event');
const states = specsSchema.messages.filter((m: Message) => m.type === 'state');
addMessageGroup(commands, 'Commands', lines);
addMessageGroup(events, 'Events', lines);
addMessageGroup(states, 'States', lines);
}
};
const addIntegrations = (specsSchema: SpecsSchema, lines: string[]): void => {
if (specsSchema.integrations && Array.isArray(specsSchema.integrations) && specsSchema.integrations.length > 0) {
lines.push('');
lines.push('# **Integrations**');
specsSchema.integrations.forEach((integration: Integration) => {
const description =
integration.description != null && integration.description.length > 0 ? `: ${integration.description}` : '';
lines.push(`* **${integration.name}**${description}`);
if (integration.source != null && integration.source.length > 0) {
lines.push(` Source: ${integration.source}`);
}
});
}
};
// Convert AppSchema to text format for display (matches demo.ts format)
function convertToTextFormat(appSchema: AppSchema, variant: 'flow-names' | 'slice-names' | 'specs'): string[] {
const lines: string[] = [];
if (appSchema.flows == null || appSchema.flows.length === 0) {
return lines;
}
appSchema.flows.forEach((flow: unknown, flowIndex: number) => {
if (!isFlow(flow)) return;
addFlowHeader(flow, flowIndex, lines);
// For flow-names variant, stop here
if (variant === 'flow-names') {
return;
}
// Add slices
if (flow.slices && Array.isArray(flow.slices)) {
flow.slices.forEach((slice: unknown) => {
if (!isSlice(slice)) return;
addSliceInfo(slice, lines);
// For slice-names variant, stop here
if (variant === 'slice-names') {
return;
}
addSliceSpecs(slice, lines);
});
}
});
// Add messages and integrations for specs variant
if (variant === 'specs') {
const specsSchema = appSchema as SpecsSchema; // Cast to specs variant
addMessages(specsSchema, lines);
addIntegrations(specsSchema, lines);
}
return lines;
}
// Helper functions to reduce complexity
const colorSpecsLine = (line: string, parentIndent: string): string => {
const noBullet = line.replace(/^\s*\*\s*/, '');
let specsLine = noBullet.replace(/_+Specs:_+/, COLORS.SPECS_LABEL('Specs:'));
// Color Events, Commands, State in specs
specsLine = specsLine.replace(/\bEvents?\.\w+/gi, (match) => COLORS.EVENTS(match));
specsLine = specsLine.replace(/\bCommands?\.\w+/gi, (match) => COLORS.COMMANDS(match));
specsLine = specsLine.replace(/\bStates?\.\w+/gi, (match) => COLORS.STATE(match));
return COLORS.SPECS_TEXT(parentIndent + specsLine.trimStart());
};
const colorFlowHeader = (line: string): string => {
const match = line.match(/(.*?)(\[[^\]]+\])/i);
if (match) {
let before = match[1].replace(/^#\s*/, '').replace(/\*\*/g, '');
const bracket = match[2];
// Color Events, Commands, State in the text part
before = before.replace(/\bEvents?\.\w+/gi, (match) => COLORS.EVENTS(match));
before = before.replace(/\bCommands?\.\w+/gi, (match) => COLORS.COMMANDS(match));
before = before.replace(/\bStates?\.\w+/gi, (match) => COLORS.STATE(match));
if (/^\[stream:/i.test(bracket)) return COLORS.FLOW_TEXT(before) + COLORS.STREAM_BRACKETS(bracket);
if (/^\[integrations:/i.test(bracket)) return COLORS.FLOW_TEXT(before) + COLORS.INTEGRATIONS_BRACKETS(bracket);
if (/^\[\.?\/?src\//i.test(bracket)) return COLORS.FLOW_TEXT(before) + COLORS.SOURCE_BRACKETS(bracket);
return COLORS.FLOW_TEXT(before) + COLORS.SOURCE_BRACKETS(bracket);
}
let cleanLine = line.replace(/^#\s*/, '').replace(/\*\*/g, '');
// Color Events, Commands, State
cleanLine = cleanLine.replace(/\bEvents?\.\w+/gi, (match) => COLORS.EVENTS(match));
cleanLine = cleanLine.replace(/\bCommands?\.\w+/gi, (match) => COLORS.COMMANDS(match));
cleanLine = cleanLine.replace(/\bStates?\.\w+/gi, (match) => COLORS.STATE(match));
return COLORS.FLOW_TEXT(cleanLine);
};
const colorSliceLine = (line: string): string => {
let cleanLine = line.replace(/^\*\s*/, '').replace(/\*\*/g, '');
// Color brackets
cleanLine = cleanLine.replace(/\[[^\]]+\]/gi, (bracket) => {
if (/^\[stream:/i.test(bracket)) return COLORS.STREAM_BRACKETS(bracket);
if (/^\[integrations:/i.test(bracket)) return COLORS.INTEGRATIONS_BRACKETS(bracket);
if (/^\[\.?\/?src\//i.test(bracket)) return COLORS.SOURCE_BRACKETS(bracket);
return COLORS.SOURCE_BRACKETS(bracket);
});
// Color Events, Commands, State
cleanLine = cleanLine.replace(/\bEvents?\.\w+/gi, (match) => COLORS.EVENTS(match));
cleanLine = cleanLine.replace(/\bCommands?\.\w+/gi, (match) => COLORS.COMMANDS(match));
cleanLine = cleanLine.replace(/\bStates?\.\w+/gi, (match) => COLORS.STATE(match));
return COLORS.SLICE_TEXT(' ' + cleanLine);
};
const colorClientServerLine = (line: string): string => {
let cleanLine = line
.trim()
.replace(/^\*\s*/, '')
.replace(/\*\*/g, '');
// Color Events, Commands, State
cleanLine = cleanLine.replace(/\bEvents?\.\w+/gi, (match) => COLORS.EVENTS(match));
cleanLine = cleanLine.replace(/\bCommands?\.\w+/gi, (match) => COLORS.COMMANDS(match));
cleanLine = cleanLine.replace(/\bStates?\.\w+/gi, (match) => COLORS.STATE(match));
return COLORS.CLIENT_SERVER.italic(' ' + cleanLine);
};
const colorMessageHeader = (line: string): string => {
const cleanLine = line.replace(/^\*\s*/, '');
if (/Commands/i.test(cleanLine)) {
return COLORS.COMMANDS(cleanLine);
} else if (/Events/i.test(cleanLine)) {
return COLORS.EVENTS(cleanLine);
} else if (/States/i.test(cleanLine)) {
return COLORS.STATE(cleanLine);
}
return cleanLine;
};
const colorMessageName = (line: string, arr: string[], idx: number): string => {
let cleanLine = line;
// Check parent category by looking backwards
for (let i = idx - 1; i >= 0; i--) {
if (/^\*\s*\*\*?(Commands)[:]?\*\*/i.test(arr[i])) {
cleanLine = cleanLine.replace(/(\*\*\w+\*\*)/, (match) => COLORS.COMMANDS(match.replace(/\*\*/g, '')));
break;
} else if (/^\*\s*\*\*?(Events)[:]?\*\*/i.test(arr[i])) {
cleanLine = cleanLine.replace(/(\*\*\w+\*\*)/, (match) => COLORS.EVENTS(match.replace(/\*\*/g, '')));
break;
} else if (/^\*\s*\*\*?(States)[:]?\*\*/i.test(arr[i])) {
cleanLine = cleanLine.replace(/(\*\*\w+\*\*)/, (match) => COLORS.STATE(match.replace(/\*\*/g, '')));
break;
}
}
return cleanLine;
};
const colorFieldDefinition = (line: string): string => {
let cleanLine = line;
// Color field types
cleanLine = cleanLine.replace(/:\s*(string|number|boolean|Date|UUID|object|array)(\s|$)/gi, (match, type) => {
return ': ' + chalk.cyan(type) + match.substring(match.indexOf(type) + (type as string).length);
});
// Color (required) and (optional)
cleanLine = cleanLine.replace(/\(required\)/gi, chalk.red('(required)'));
cleanLine = cleanLine.replace(/\(optional\)/gi, chalk.green('(optional)'));
return COLORS.SPECS_TEXT(cleanLine);
};
const colorIntegrationName = (line: string, arr: string[], idx: number): string => {
// Check if we're in the Integrations section
for (let i = idx - 1; i >= 0; i--) {
if (/^#\s*\*\*?Integrations/i.test(arr[i])) {
let cleanLine = line.replace(/^\*\s*/, '');
cleanLine = cleanLine.replace(/\*\*/g, '');
return COLORS.INTEGRATIONS_BRACKETS('* ' + cleanLine);
}
if (/^#\s*\*\*?(Flow|Messages)/i.test(arr[i])) {
break; // Stop if we hit a different section
}
}
return line;
};
const colorBrackets = (line: string): string => {
return line.replace(/\[[^\]]+\]/gi, (bracket) => {
if (/^\[stream:/i.test(bracket)) return COLORS.STREAM_BRACKETS(bracket);
if (/^\[integrations:/i.test(bracket)) return COLORS.INTEGRATIONS_BRACKETS(bracket);
if (/^\[\.?\/?src\//i.test(bracket)) return COLORS.SOURCE_BRACKETS(bracket);
return COLORS.SOURCE_BRACKETS(bracket);
});
};
// Helper to find parent indent for specs lines
const findParentIndent = (idx: number, arr: string[]): string => {
let parentIndent = ' '; // Default to 6 spaces
for (let i = idx - 1; i >= 0; i--) {
if (/^\s*\*\s*\*\*?(Client|Server)[:]?.*/i.test(arr[i])) {
parentIndent = ' ';
break;
}
}
return parentIndent;
};
// Helper to check if line is a specs line
const isSpecsLine = (line: string): boolean => {
return /\*\s*_+Specs:_+/.test(line);
};
// Helper to check if line is a flow header
const isFlowHeader = (line: string): boolean => {
return /^#\s*\*\*?(Flow|Messages|Integrations)[:]?.*/i.test(line);
};
// Helper to check and color specific line types
const colorSpecialLines = (line: string, idx: number, arr: string[]): string | null => {
if (isSpecsLine(line)) {
return colorSpecsLine(line, findParentIndent(idx, arr));
}
if (isFlowHeader(line)) {
return colorFlowHeader(line);
}
if (/^\*\s*\*\*?Slice[:]?.*/i.test(line)) {
return colorSliceLine(line);
}
if (/^\s*\*\s*\*\*?(Client|Server)[:]?.*/i.test(line)) {
return colorClientServerLine(line);
}
return null;
};
// Helper to color message-related lines
const colorMessageLines = (line: string, arr: string[], idx: number): string | null => {
if (/^\*\s*\*\*?(Commands|Events|States)[:]?\*\*/i.test(line)) {
return colorMessageHeader(line);
}
if (/^\s{2}\*\s*\*\*\w+\*\*/.test(line)) {
return colorMessageName(line, arr, idx);
}
if (/^\s+-\s+/.test(line)) {
return colorFieldDefinition(line);
}
return null;
};
// Render text lines with colors (copied from demo.ts)
const colorizeLine = (line: string, idx: number, arr: string[]): string => {
const specialColor = colorSpecialLines(line, idx, arr);
if (specialColor != null) return specialColor;
const messageColor = colorMessageLines(line, arr, idx);
if (messageColor != null) return messageColor;
return colorOtherLines(line, arr, idx);
};
// Helper to color remaining line types
const colorOtherLines = (line: string, arr: string[], idx: number): string => {
// Color integration names (under Integrations section)
if (/^\*\s*\*\*\w+\*\*/.test(line) && idx > 0) {
return colorIntegrationName(line, arr, idx);
}
// Color "Source:" lines
if (/^\s+Source:/.test(line)) {
return COLORS.SOURCE_BRACKETS(line);
}
let coloredLine = colorBrackets(line);
// Color Events, Commands, State
coloredLine = coloredLine.replace(/\bEvents?\.\w+/gi, (match) => COLORS.EVENTS(match));
coloredLine = coloredLine.replace(/\bCommands?\.\w+/gi, (match) => COLORS.COMMANDS(match));
coloredLine = coloredLine.replace(/\bStates?\.\w+/gi, (match) => COLORS.STATE(match));
return coloredLine;
};
function renderColoredText(lines: string[]): string {
return lines.map(colorizeLine).join('\n');
}
// Initialize message bus
const messageBus = new MessageBus();
messageBus.registerCommandHandler(createFlowCommandHandler);
interface StreamData {
flows?: Flow[];
messages?: Message[];
integrations?: Integration[];
}
async function sendFlowCommand(
prompt: string,
variant: 'flow-names' | 'slice-names' | 'client-server-names' | 'specs',
onStream?: (data: unknown) => void,
): Promise<AppSchema> {
const command: CreateFlowCommand = {
type: 'CreateFlow',
requestId: `req-${Date.now()}`,
timestamp: new Date(),
prompt,
variant,
useStreaming: true,
streamCallback: onStream,
};
const response = await messageBus.sendCommand(command);
if (response.status === 'nack') {
throw new Error(response.error ?? 'Failed to create flow');
}
// Parse the response to get the FlowCreatedEvent
const event = JSON.parse(response.message ?? '{}') as { systemData: AppSchema };
return event.systemData;
}
const validateAppPrompt = (input: string): true | string => {
if (input.trim().length === 0) {
return 'Please describe what you want us to build';
}
if (input.trim().length < 10) {
return 'Please provide a more detailed description (at least 10 characters)';
}
return true;
};
const getAppPrompt = async (output: ReturnType<typeof createOutput>): Promise<string> => {
const answers = await inquirer.prompt<{ appPrompt: string }>([
{
type: 'input',
name: 'appPrompt',
message: 'What would you like to start building together?\n(NOTE: NOT FULLY FUNCTIONAL YET!)',
validate: validateAppPrompt,
transformer: (input: string) => input.trim(),
},
]);
output.debug(`User wants to build: ${answers.appPrompt}`);
return answers.appPrompt;
};
const generateFlowNames = async (currentPrompt: string, iteration: number, flowSpinner: Ora): Promise<AppSchema> => {
return await sendFlowCommand(currentPrompt, 'flow-names', (partialData: unknown) => {
const data = partialData as StreamData;
if (data.flows && Array.isArray(data.flows)) {
flowSpinner.text = chalk.gray(`Generated ${data.flows.length} flows...`);
}
});
};
const displayFlows = (flowNamesData: AppSchema): void => {
console.log();
const flowTextLines = convertToTextFormat(flowNamesData, 'flow-names');
console.log(renderColoredText(flowTextLines));
console.log();
};
const getConfirmation = async (): Promise<string> => {
const answers = await inquirer.prompt<{ confirmation: string }>([
{
type: 'input',
name: 'confirmation',
message:
chalk.cyan('Is this on point or would you like to make changes?\n') +
chalk.gray(' Press ') +
chalk.green('Enter') +
chalk.gray(', type ') +
chalk.green('Yes') +
chalk.gray(' or ') +
chalk.green('Y') +
chalk.gray(" to continue, or describe what changes you'd like:"),
transformer: (input: string) => input.trim(),
},
]);
return answers.confirmation ?? '';
};
const isUserSatisfied = (confirmation: string): boolean => {
const lowerConfirmation = confirmation.toLowerCase();
return lowerConfirmation === '' || lowerConfirmation === 'yes' || lowerConfirmation === 'y';
};
const updatePromptForChanges = (appPrompt: string, flowNamesData: AppSchema, confirmation: string): string => {
const previousFlows =
flowNamesData.flows
?.slice(0, 4)
.map((f) => f.name)
.join(', ') || '';
return `Original request: ${appPrompt}
Previously generated flows: ${previousFlows}
User feedback: ${confirmation}
Please generate 4 new flows based on this feedback.`;
};
const buildSpecsPrompt = (appPrompt: string, flowNames: string): string => {
return `Based on the application: ${appPrompt}
For these flows: ${flowNames}
Generate complete specifications following the AppSchema format with:
1. flows: Array of flows, each containing:
- Multiple slices per flow
- For each slice, include:
- name: slice name
- type: 'command' or 'query' or 'react'
- client: {
description: "Component description",
specs: ["should have...", "should display...", etc]
}
- server: {
description: "Server logic description",
gwt: {
given: [{ eventRef: "EventName" }] (optional for commands),
when: { commandRef: "CommandName" } (for commands),
then: [{ eventRef: "EventName" }] (or stateRef for queries)
}
}
2. messages: Array of all Commands, Events, and States referenced in the flows:
- type: 'command' | 'event' | 'state'
- name: The message name (without Commands/Events/State prefix)
- description: What it represents
- fields: Array of field definitions with name, type, required, and description
3. integrations: (optional) Array of external integrations if any slices use them:
- name: Integration name (e.g., "MailChimp", "Twilio")
- description: What it does
- source: NPM package name (e.g., "@auto-engineer/mailchimp-integration")
The GWT (Given-When-Then) structure defines state transitions:
- Command slices: when (command) => then (events)
- Query slices: given (events) => then (state updates)
- React slices: when (events) => then (commands)
Make sure to stream the data progressively, showing flows first, then messages, then integrations.`;
};
// Helper to stop spinner and clear console
const stopSpinnerAndClear = (specSpinner: Ora): void => {
if (specSpinner.isSpinning) {
specSpinner.stop();
}
console.clear();
};
// Helper to render header
const renderStreamHeader = (): void => {
console.log(chalk.cyan('Building up your flows...'));
console.log();
console.log(chalk.gray('Full specifications'));
console.log(chalk.gray('─'.repeat(50)));
};
// Helper to log debug info
const logDebugInfo = (partialData: StreamData, output: ReturnType<typeof createOutput>, updateCount: number): void => {
const firstSlice = partialData.flows?.[0]?.slices?.[0];
if (firstSlice != null) {
output.debug(`Stream update ${updateCount}: ${JSON.stringify(firstSlice, null, 2)}`);
}
};
const handleStreamUpdate = (
partialData: StreamData,
specSpinner: Ora,
output: ReturnType<typeof createOutput>,
updateCount: number,
): void => {
if (partialData.flows != null && Array.isArray(partialData.flows)) {
stopSpinnerAndClear(specSpinner);
renderStreamHeader();
// Render the current state with full specs
const specTextLines = convertToTextFormat(partialData as AppSchema, 'specs');
console.log(renderColoredText(specTextLines));
logDebugInfo(partialData, output, updateCount);
}
};
const generateSpecs = async (
specsPrompt: string,
specSpinner: Ora,
output: ReturnType<typeof createOutput>,
): Promise<void> => {
let updateCount = 0;
await sendFlowCommand(specsPrompt, 'specs', (partialData: unknown) => {
updateCount++;
handleStreamUpdate(partialData as StreamData, specSpinner, output, updateCount);
});
// Final success message
console.log();
console.log(chalk.green('✅ Complete flow structure generated!'));
};
const showNextSteps = (): void => {
console.log();
console.log(chalk.gray('Next steps:'));
console.log(chalk.gray('- Review the generated flow specifications'));
console.log(chalk.gray('- Refine any slices or specifications as needed'));
console.log(chalk.gray('- Generate the actual code implementation'));
};
// Helper to handle flow generation loop
const generateFlowsLoop = async (appPrompt: string, _output: ReturnType<typeof createOutput>): Promise<AppSchema> => {
let flowNamesData: AppSchema;
let currentPrompt = appPrompt;
let iteration = 0;
while (true) {
const flowSpinner = ora({
text: chalk.gray(iteration === 0 ? 'Generating some flows...' : 'Regenerating flows with your feedback...'),
spinner: 'dots',
}).start();
try {
flowNamesData = await generateFlowNames(currentPrompt, iteration, flowSpinner);
flowSpinner.succeed(chalk.green('Flows generated successfully!'));
} catch (error) {
flowSpinner.fail(chalk.red('Failed to generate flows'));
throw error;
}
if (flowNamesData.flows == null || flowNamesData.flows.length === 0) {
console.log(chalk.yellow('No flows were generated. Please try with a different prompt.'));
throw new Error('No flows generated');
}
displayFlows(flowNamesData);
const confirmation = await getConfirmation();
if (isUserSatisfied(confirmation)) {
break;
}
currentPrompt = updatePromptForChanges(appPrompt, flowNamesData, confirmation);
iteration++;
}
return flowNamesData;
};
// Helper to handle specs generation
const generateSpecsStep = async (
appPrompt: string,
flowNamesData: AppSchema,
output: ReturnType<typeof createOutput>,
): Promise<void> => {
console.log();
console.log(chalk.cyan('Building up your flows...'));
console.log();
const specSpinner = ora({
text: chalk.gray('Generating full specifications...'),
spinner: 'dots',
}).start();
try {
const flowNames = flowNamesData.flows?.map((f) => f.name).join(', ') ?? '';
const specsPrompt = buildSpecsPrompt(appPrompt, flowNames);
await generateSpecs(specsPrompt, specSpinner, output);
} catch (error) {
if (specSpinner.isSpinning) {
specSpinner.fail(chalk.red('Failed to generate specifications'));
} else {
console.log(chalk.red('\n❌ Failed to generate specifications'));
}
throw error;
}
};
export const createStartCommand = (config: Config, analytics: Analytics) => {
const output = createOutput(config);
return new Command('start').description('Create flows interactively using AI').action(async () => {
try {
output.debug('Start command initiated');
const appPrompt = await getAppPrompt(output);
const flowNamesData = await generateFlowsLoop(appPrompt, output);
await generateSpecsStep(appPrompt, flowNamesData, output);
showNextSteps();
await analytics.trackCommand('start', true);
output.debug('Start command completed successfully');
} catch (error: unknown) {
await analytics.trackCommand('start', false, error instanceof Error && error.message ? error.message : 'unknown');
if (error instanceof Error) {
handleError(error);
} else {
handleError(new Error(String(error)));
}
}
});
};