-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanstruct.go
More file actions
400 lines (337 loc) · 11.1 KB
/
anstruct.go
File metadata and controls
400 lines (337 loc) · 11.1 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
package anstruct
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/alberdjuniawan/anstruct/internal/ai"
"github.com/alberdjuniawan/anstruct/internal/converter"
"github.com/alberdjuniawan/anstruct/internal/core"
"github.com/alberdjuniawan/anstruct/internal/generator"
"github.com/alberdjuniawan/anstruct/internal/history"
"github.com/alberdjuniawan/anstruct/internal/parser"
"github.com/alberdjuniawan/anstruct/internal/reverser"
"github.com/alberdjuniawan/anstruct/internal/validator"
"github.com/alberdjuniawan/anstruct/internal/watcher"
)
type Service struct {
Gen *ai.AIGenerator
Parser core.Parser
Reverser core.Reverser
Validator core.Validator
History core.History
Writer *generator.Generator
}
type OperationRecreator struct {
svc *Service
}
func NewService(endpoint, historyPath string) *Service {
p := parser.New()
provider := ai.NewGeminiProvider(endpoint)
s := &Service{
Gen: ai.NewAIGenerator(provider, p),
Parser: p,
Reverser: reverser.New(),
Validator: validator.New(),
History: history.New(historyPath),
Writer: generator.New(),
}
recreator := &OperationRecreator{svc: s}
if hist, ok := s.History.(*history.History); ok {
hist.SetRecreator(recreator)
}
return s
}
func (r *OperationRecreator) RecreateOperation(ctx context.Context, op core.Operation) error {
switch op.Type {
case core.OpCreate:
return r.recreateCreate(ctx, op)
case core.OpAIApply:
return r.recreateAIApply(ctx, op)
case core.OpReverse:
return r.recreateReverse(ctx, op)
case core.OpAI:
return r.recreateAIBlueprint(ctx, op)
default:
return fmt.Errorf("unknown operation type: %s", op.Type)
}
}
func (r *OperationRecreator) recreateCreate(ctx context.Context, op core.Operation) error {
if op.BlueprintPath == "" {
return fmt.Errorf("cannot recreate: blueprint path not saved in operation")
}
if _, err := os.Stat(op.BlueprintPath); os.IsNotExist(err) {
return fmt.Errorf("cannot recreate: blueprint file not found: %s", op.BlueprintPath)
}
fmt.Printf("📄 Recreating from blueprint: %s\n", op.BlueprintPath)
tree, err := r.svc.Parser.Parse(ctx, op.BlueprintPath)
if err != nil {
return fmt.Errorf("failed to parse blueprint: %w", err)
}
if err := r.svc.Validator.Validate(ctx, tree); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
receipt, err := r.svc.Writer.Generate(ctx, tree, op.Target, core.GenerateOptions{
DryRun: false,
Force: true,
})
if err != nil {
return fmt.Errorf("generation failed: %w", err)
}
op.Receipt = receipt
fmt.Printf("✅ Recreated: %d dirs, %d files\n", len(receipt.CreatedDirs), len(receipt.CreatedFiles))
return nil
}
func (r *OperationRecreator) recreateAIApply(ctx context.Context, op core.Operation) error {
if op.SourcePrompt == "" {
return fmt.Errorf("cannot recreate: source prompt not saved in operation")
}
fmt.Printf("🤖 Regenerating from AI prompt: %s\n", op.SourcePrompt)
tree, _, err := r.svc.Gen.FromPrompt(ctx, op.SourcePrompt, 2)
if err != nil {
return fmt.Errorf("AI generation failed: %w", err)
}
if err := r.svc.Validator.Validate(ctx, tree); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
receipt, err := r.svc.Writer.Generate(ctx, tree, op.Target, core.GenerateOptions{
DryRun: false,
Force: true,
})
if err != nil {
return fmt.Errorf("generation failed: %w", err)
}
op.Receipt = receipt
fmt.Printf("✅ Regenerated: %d dirs, %d files\n", len(receipt.CreatedDirs), len(receipt.CreatedFiles))
return nil
}
func (r *OperationRecreator) recreateReverse(ctx context.Context, op core.Operation) error {
return fmt.Errorf("cannot automatically recreate reverse operation - please run 'anstruct rstruct' manually")
}
func (r *OperationRecreator) recreateAIBlueprint(ctx context.Context, op core.Operation) error {
if op.SourcePrompt == "" {
return fmt.Errorf("cannot recreate: source prompt not saved in operation")
}
fmt.Printf("🤖 Regenerating AI blueprint from prompt: %s\n", op.SourcePrompt)
tree, rawOutput, err := r.svc.Gen.FromPrompt(ctx, op.SourcePrompt, 2)
if err != nil {
if rawOutput != "" {
fallbackFile := "ai_invalid_" + time.Now().Format("20060102_150405") + ".struct"
_ = os.WriteFile(fallbackFile, []byte(rawOutput), 0644)
return fmt.Errorf("%w\n💾 Raw output saved to: %s", err, fallbackFile)
}
return err
}
dir := filepath.Dir(op.Target)
if dir != "." && dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
}
if err := r.svc.Parser.Write(ctx, tree, op.Target); err != nil {
return fmt.Errorf("failed to write blueprint: %w", err)
}
fmt.Printf("✅ Blueprint recreated: %s\n", op.Target)
return nil
}
func (s *Service) AIStruct(ctx context.Context, prompt, outPath string, opts core.AIOptions) error {
fmt.Printf("🤖 Generating from prompt: %s\n", prompt)
tree, rawOutput, err := s.Gen.FromPrompt(ctx, prompt, opts.Retries)
if opts.Verbose && rawOutput != "" {
fmt.Println("\n📋 Raw AI Output:")
fmt.Println("---")
fmt.Println(rawOutput)
fmt.Println("---")
}
if err != nil {
if rawOutput != "" {
fallbackFile := "ai_invalid_" + time.Now().Format("20060102_150405") + ".struct"
_ = os.WriteFile(fallbackFile, []byte(rawOutput), 0644)
return fmt.Errorf("%w\n💾 Raw output saved to: %s", err, fallbackFile)
}
return err
}
if opts.DryRun {
fmt.Println("\n📂 Preview of generated structure:")
displayTree(tree.Root, 0)
fmt.Printf("\n✅ Dry run complete. No files written.\n")
return nil
}
if opts.Apply {
return s.applyDirectly(ctx, tree, outPath, prompt, opts)
}
return s.saveBlueprint(ctx, tree, outPath, prompt)
}
func (s *Service) applyDirectly(ctx context.Context, tree *core.Tree, outPath, prompt string, opts core.AIOptions) error {
fmt.Printf("\n📁 Generating project folder: %s\n", outPath)
if err := s.Validator.ValidateWithOptions(ctx, tree, opts.AllowReserved); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
receipt, err := s.Writer.Generate(ctx, tree, outPath, core.GenerateOptions{
DryRun: false,
Force: opts.Force,
})
if err != nil {
return fmt.Errorf("generation failed: %w", err)
}
_ = s.History.Record(ctx, core.Operation{
Type: core.OpAIApply,
Target: outPath,
Receipt: receipt,
SourcePrompt: prompt,
})
fmt.Printf("\n✅ Project generated successfully!\n")
fmt.Printf(" 📂 %d directories created\n", len(receipt.CreatedDirs))
fmt.Printf(" 📄 %d files created\n", len(receipt.CreatedFiles))
fmt.Printf("\n📍 Location: %s\n", outPath)
return nil
}
func (s *Service) saveBlueprint(ctx context.Context, tree *core.Tree, outPath, prompt string) error {
fmt.Printf("\n📁 Saving blueprint: %s\n", outPath)
dir := filepath.Dir(outPath)
if dir != "." && dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
}
if err := s.Parser.Write(ctx, tree, outPath); err != nil {
return fmt.Errorf("failed to write blueprint: %w", err)
}
_ = s.History.Record(ctx, core.Operation{
Type: core.OpAI,
Target: outPath,
SourcePrompt: prompt,
})
fmt.Printf("✅ Blueprint saved: %s\n", outPath)
return nil
}
func displayTree(n *core.Node, depth int) {
if depth > 0 {
indent := ""
for i := 0; i < depth-1; i++ {
indent += " "
}
symbol := "📄"
if n.Type == core.NodeDir {
symbol = "📁"
}
fmt.Printf("%s%s %s\n", indent, symbol, n.Name)
}
for _, c := range n.Children {
displayTree(c, depth+1)
}
}
func (s *Service) MStruct(ctx context.Context, structFile, outputDir string, opts core.GenerateOptions) (core.Receipt, error) {
tree, err := s.Parser.Parse(ctx, structFile)
if err != nil {
return core.Receipt{}, err
}
if err := s.Validator.ValidateWithOptions(ctx, tree, opts.AllowReserved); err != nil {
return core.Receipt{}, err
}
receipt, err := s.Writer.Generate(ctx, tree, outputDir, opts)
if err != nil {
return receipt, err
}
_ = s.History.Record(ctx, core.Operation{
Type: core.OpCreate,
Target: outputDir,
Receipt: receipt,
BlueprintPath: structFile,
})
return receipt, nil
}
func (s *Service) RStruct(ctx context.Context, inputDir string, outPath string) error {
tree, err := s.Reverser.Reverse(ctx, inputDir)
if err != nil {
return err
}
if err := s.Parser.Write(ctx, tree, outPath); err != nil {
return err
}
_ = s.History.Record(ctx, core.Operation{
Type: core.OpReverse,
Target: outPath,
})
return nil
}
func (s *Service) NormalizeStruct(ctx context.Context, inputContent, outPath string, opts core.AIOptions) error {
fmt.Println("📄 Starting normalization with converter system...")
converter := converter.NewWithAI(s.Gen.Provider, s.Parser, converter.ModeAuto)
tree, detected, err := converter.Convert(ctx, inputContent)
if err != nil {
return fmt.Errorf("conversion failed (%s): %w", detected, err)
}
fmt.Printf("✅ Detected input format: %s\n", detected)
if opts.Verbose {
fmt.Println("\n📋 Normalized Structure (.struct):")
fmt.Println("---")
fmt.Println(converter.ConvertToString(tree))
fmt.Println("---")
}
if opts.DryRun {
fmt.Println("\n📂 Preview of normalized structure:")
displayTree(tree.Root, 0)
fmt.Println("\n✅ Dry run complete. No files written.")
return nil
}
dir := filepath.Dir(outPath)
if dir != "." && dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
}
if err := s.Parser.Write(ctx, tree, outPath); err != nil {
return fmt.Errorf("failed to write normalized blueprint: %w", err)
}
_ = s.History.Record(ctx, core.Operation{
Type: core.OpNormalize,
Target: outPath,
Meta: map[string]string{
"detected_format": string(detected),
"mode": string(converter.Normalizer.Mode),
},
})
fmt.Printf("\n✅ Structure normalized and saved to: %s\n", outPath)
fmt.Println("💡 You can now use: anstruct mstruct " + outPath)
return nil
}
func (s *Service) Watch(ctx context.Context, projectPath, blueprintPath string, debounce time.Duration, verbose bool) error {
w := watcher.New()
cfg := watcher.SyncConfig{
ProjectPath: projectPath,
BlueprintPath: blueprintPath,
Debounce: debounce,
Verbose: verbose,
}
return w.Run(ctx, cfg,
func() {
if err := s.RStruct(ctx, projectPath, blueprintPath); err != nil && verbose {
fmt.Println("Reverse error:", err)
}
},
func() {
tree, err := s.Parser.Parse(ctx, blueprintPath)
if err == nil {
receipt, genErr := s.Writer.Generate(ctx, tree, projectPath, core.GenerateOptions{Force: true})
if genErr == nil {
allowed := map[string]bool{}
for _, c := range tree.Root.Children {
generator.CollectAllowed(c, "", allowed)
}
if err := generator.CleanupExtra(projectPath, allowed); err != nil && verbose {
fmt.Println("Cleanup error:", err)
}
if verbose {
fmt.Println("✅ Synced:", len(receipt.CreatedDirs), "dirs,", len(receipt.CreatedFiles), "files")
}
} else if verbose {
fmt.Println("Generate error:", genErr)
}
} else if verbose {
fmt.Println("Parse error:", err)
}
},
)
}