|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + |
| 12 | + "github.com/supermodeltools/cli/internal/api" |
| 13 | + "github.com/supermodeltools/cli/internal/cache" |
| 14 | + "github.com/supermodeltools/cli/internal/config" |
| 15 | + "github.com/supermodeltools/cli/internal/factory" |
| 16 | +) |
| 17 | + |
| 18 | +func init() { |
| 19 | + factoryCmd := &cobra.Command{ |
| 20 | + Use: "factory", |
| 21 | + Short: "AI-native SDLC orchestration via graph intelligence", |
| 22 | + Long: `Factory is an AI-native SDLC system that uses the Supermodel code graph API |
| 23 | +to power graph-first development workflows. |
| 24 | +
|
| 25 | +Inspired by Big Iron (github.com/supermodeltools/bigiron), factory provides |
| 26 | +three commands: |
| 27 | +
|
| 28 | + health — Analyse codebase health: circular deps, domain coupling, blast radius |
| 29 | + run — Generate a graph-enriched 8-phase SDLC execution plan for a goal |
| 30 | + improve — Generate a prioritised, graph-driven improvement plan |
| 31 | +
|
| 32 | +All commands require an API key (run 'supermodel login' to configure). |
| 33 | +
|
| 34 | +Examples: |
| 35 | +
|
| 36 | + supermodel factory health |
| 37 | + supermodel factory run "Add rate limiting to the order API" |
| 38 | + supermodel factory improve`, |
| 39 | + SilenceUsage: true, |
| 40 | + } |
| 41 | + |
| 42 | + // ── health ──────────────────────────────────────────────────────────────── |
| 43 | + var healthDir string |
| 44 | + healthCmd := &cobra.Command{ |
| 45 | + Use: "health", |
| 46 | + Short: "Alias for 'supermodel audit'", |
| 47 | + Long: "Health is an alias for 'supermodel audit'. See 'supermodel audit --help' for full documentation.", |
| 48 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 49 | + return runAudit(cmd, healthDir) |
| 50 | + }, |
| 51 | + SilenceUsage: true, |
| 52 | + } |
| 53 | + healthCmd.Flags().StringVar(&healthDir, "dir", "", "project directory (default: current working directory)") |
| 54 | + |
| 55 | + // ── run ─────────────────────────────────────────────────────────────────── |
| 56 | + var runDir string |
| 57 | + runCmd := &cobra.Command{ |
| 58 | + Use: "run <goal>", |
| 59 | + Short: "Generate a graph-enriched SDLC execution plan for a goal", |
| 60 | + Long: `Run analyses the codebase and generates a graph-enriched 8-phase SDLC |
| 61 | +execution plan tailored to the supplied goal. |
| 62 | +
|
| 63 | +The output is a Markdown prompt designed to be consumed by Claude Code or any |
| 64 | +AI agent. Pipe it directly into an agent session: |
| 65 | +
|
| 66 | + supermodel factory run "Add rate limiting to the order API" | claude --print |
| 67 | +
|
| 68 | +The plan includes codebase context (domains, key files, tech stack), the goal, |
| 69 | +and phase-by-phase instructions with graph-aware quality gates.`, |
| 70 | + Args: cobra.ExactArgs(1), |
| 71 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 72 | + return runFactoryRun(cmd, runDir, args[0]) |
| 73 | + }, |
| 74 | + SilenceUsage: true, |
| 75 | + } |
| 76 | + runCmd.Flags().StringVar(&runDir, "dir", "", "project directory (default: current working directory)") |
| 77 | + |
| 78 | + // ── improve ─────────────────────────────────────────────────────────────── |
| 79 | + var improveDir string |
| 80 | + improveCmd := &cobra.Command{ |
| 81 | + Use: "improve", |
| 82 | + Short: "Generate a graph-driven improvement plan", |
| 83 | + Long: `Improve runs a health analysis and generates a prioritised improvement plan |
| 84 | +using the Supermodel code graph. |
| 85 | +
|
| 86 | +The output is a Markdown prompt that guides an AI agent through: |
| 87 | +
|
| 88 | + 1. Scoring improvement targets (circular deps, coupling, dead code, depth) |
| 89 | + 2. Executing refactors in bottom-up topological order |
| 90 | + 3. Running quality gates after each change |
| 91 | + 4. A final dead code sweep and health check |
| 92 | +
|
| 93 | +Pipe it into an agent session: |
| 94 | +
|
| 95 | + supermodel factory improve | claude --print`, |
| 96 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 97 | + return runFactoryImprove(cmd, improveDir) |
| 98 | + }, |
| 99 | + SilenceUsage: true, |
| 100 | + } |
| 101 | + improveCmd.Flags().StringVar(&improveDir, "dir", "", "project directory (default: current working directory)") |
| 102 | + |
| 103 | + factoryCmd.AddCommand(healthCmd, runCmd, improveCmd) |
| 104 | + rootCmd.AddCommand(factoryCmd) |
| 105 | +} |
| 106 | + |
| 107 | +// ── run ─────────────────────────────────────────────────────────────────────── |
| 108 | + |
| 109 | +func runFactoryRun(cmd *cobra.Command, dir, goal string) error { |
| 110 | + rootDir, projectName, err := resolveFactoryDir(dir) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + ir, err := factoryAnalyze(cmd, rootDir, projectName) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + report := factory.Analyze(ir, projectName) |
| 121 | + data := factoryPromptData(report, goal) |
| 122 | + factory.RenderRunPrompt(cmd.OutOrStdout(), data) |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +// ── improve ─────────────────────────────────────────────────────────────────── |
| 127 | + |
| 128 | +func runFactoryImprove(cmd *cobra.Command, dir string) error { |
| 129 | + rootDir, projectName, err := resolveFactoryDir(dir) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + ir, err := factoryAnalyze(cmd, rootDir, projectName) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + report := factory.Analyze(ir, projectName) |
| 140 | + data := factoryPromptData(report, "") |
| 141 | + factory.RenderImprovePrompt(cmd.OutOrStdout(), data) |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +// ── shared helpers ──────────────────────────────────────────────────────────── |
| 146 | + |
| 147 | +func resolveFactoryDir(dir string) (rootDir, projectName string, err error) { |
| 148 | + if dir == "" { |
| 149 | + dir, err = os.Getwd() |
| 150 | + if err != nil { |
| 151 | + return "", "", fmt.Errorf("get working directory: %w", err) |
| 152 | + } |
| 153 | + } |
| 154 | + rootDir = findGitRoot(dir) |
| 155 | + projectName = filepath.Base(rootDir) |
| 156 | + return rootDir, projectName, nil |
| 157 | +} |
| 158 | + |
| 159 | +func factoryAnalyze(cmd *cobra.Command, rootDir, projectName string) (*api.SupermodelIR, error) { |
| 160 | + cfg, err := config.Load() |
| 161 | + if err != nil { |
| 162 | + return nil, err |
| 163 | + } |
| 164 | + if cfg.APIKey == "" { |
| 165 | + return nil, fmt.Errorf("no API key configured — run 'supermodel login' first") |
| 166 | + } |
| 167 | + |
| 168 | + fmt.Fprintln(cmd.ErrOrStderr(), "Creating repository archive…") |
| 169 | + zipPath, err := factory.CreateZip(rootDir) |
| 170 | + if err != nil { |
| 171 | + return nil, fmt.Errorf("create archive: %w", err) |
| 172 | + } |
| 173 | + defer func() { _ = os.Remove(zipPath) }() |
| 174 | + |
| 175 | + hash, err := cache.HashFile(zipPath) |
| 176 | + if err != nil { |
| 177 | + return nil, fmt.Errorf("hash archive: %w", err) |
| 178 | + } |
| 179 | + |
| 180 | + client := api.New(cfg) |
| 181 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) |
| 182 | + defer cancel() |
| 183 | + |
| 184 | + fmt.Fprintf(cmd.ErrOrStderr(), "Analyzing %s…\n", projectName) |
| 185 | + ir, err := client.AnalyzeDomains(ctx, zipPath, "factory-"+hash[:16]) |
| 186 | + if err != nil { |
| 187 | + return nil, err |
| 188 | + } |
| 189 | + return ir, nil |
| 190 | +} |
| 191 | + |
| 192 | +func factoryPromptData(report *factory.HealthReport, goal string) *factory.SDLCPromptData { |
| 193 | + domains := make([]factory.DomainHealth, len(report.Domains)) |
| 194 | + copy(domains, report.Domains) |
| 195 | + |
| 196 | + criticalFiles := make([]factory.CriticalFile, len(report.CriticalFiles)) |
| 197 | + copy(criticalFiles, report.CriticalFiles) |
| 198 | + |
| 199 | + data := &factory.SDLCPromptData{ |
| 200 | + ProjectName: report.ProjectName, |
| 201 | + Language: report.Language, |
| 202 | + TotalFiles: report.TotalFiles, |
| 203 | + TotalFunctions: report.TotalFunctions, |
| 204 | + ExternalDeps: report.ExternalDeps, |
| 205 | + Domains: domains, |
| 206 | + CriticalFiles: criticalFiles, |
| 207 | + CircularDeps: report.CircularDeps, |
| 208 | + Goal: goal, |
| 209 | + GeneratedAt: report.AnalyzedAt.Format("2006-01-02 15:04:05 UTC"), |
| 210 | + } |
| 211 | + if goal == "" { |
| 212 | + data.HealthReport = report |
| 213 | + } |
| 214 | + return data |
| 215 | +} |
0 commit comments