|
| 1 | +package compat |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "chuchu/internal/llm" |
| 11 | +) |
| 12 | + |
| 13 | +type DeprecatedAPI struct { |
| 14 | + Name string |
| 15 | + Version string |
| 16 | + File string |
| 17 | + Line int |
| 18 | + Replacement string |
| 19 | + Reason string |
| 20 | +} |
| 21 | + |
| 22 | +type CompatibilityReport struct { |
| 23 | + DeprecatedAPIs []DeprecatedAPI |
| 24 | + WrapperCode string |
| 25 | + MigrationGuide string |
| 26 | + BreakingIn string |
| 27 | +} |
| 28 | + |
| 29 | +type CompatManager struct { |
| 30 | + provider llm.Provider |
| 31 | + model string |
| 32 | + workDir string |
| 33 | +} |
| 34 | + |
| 35 | +func NewCompatManager(provider llm.Provider, model, workDir string) *CompatManager { |
| 36 | + return &CompatManager{ |
| 37 | + provider: provider, |
| 38 | + model: model, |
| 39 | + workDir: workDir, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (m *CompatManager) AddDeprecation(ctx context.Context, oldAPI, newAPI, version, reason string) (*CompatibilityReport, error) { |
| 44 | + wrapperCode, err := m.generateWrapperCode(ctx, oldAPI, newAPI, version, reason) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("failed to generate wrapper: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + migrationGuide, err := m.generateMigrationGuide(ctx, oldAPI, newAPI, version, reason) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to generate migration guide: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + report := &CompatibilityReport{ |
| 55 | + DeprecatedAPIs: []DeprecatedAPI{ |
| 56 | + { |
| 57 | + Name: oldAPI, |
| 58 | + Replacement: newAPI, |
| 59 | + Version: version, |
| 60 | + Reason: reason, |
| 61 | + }, |
| 62 | + }, |
| 63 | + WrapperCode: wrapperCode, |
| 64 | + MigrationGuide: migrationGuide, |
| 65 | + BreakingIn: m.calculateBreakingVersion(version), |
| 66 | + } |
| 67 | + |
| 68 | + return report, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (m *CompatManager) generateWrapperCode(ctx context.Context, oldAPI, newAPI, version, reason string) (string, error) { |
| 72 | + prompt := fmt.Sprintf(`Generate backward compatibility wrapper code: |
| 73 | +
|
| 74 | +Old API: %s |
| 75 | +New API: %s |
| 76 | +Deprecated in: %s |
| 77 | +Reason: %s |
| 78 | +
|
| 79 | +Requirements: |
| 80 | +1. Keep old API working |
| 81 | +2. Add deprecation warnings/comments |
| 82 | +3. Internally call new API |
| 83 | +4. Include version info |
| 84 | +5. Suggest migration path |
| 85 | +
|
| 86 | +Return ONLY the wrapper code with comments.`, oldAPI, newAPI, version, reason) |
| 87 | + |
| 88 | + resp, err := m.provider.Chat(ctx, llm.ChatRequest{ |
| 89 | + UserPrompt: prompt, |
| 90 | + Model: m.model, |
| 91 | + }) |
| 92 | + |
| 93 | + if err != nil { |
| 94 | + return "", err |
| 95 | + } |
| 96 | + |
| 97 | + return m.extractCode(resp.Text), nil |
| 98 | +} |
| 99 | + |
| 100 | +func (m *CompatManager) generateMigrationGuide(ctx context.Context, oldAPI, newAPI, version, reason string) (string, error) { |
| 101 | + prompt := fmt.Sprintf(`Generate migration guide: |
| 102 | +
|
| 103 | +From: %s |
| 104 | +To: %s |
| 105 | +Version: %s |
| 106 | +Reason: %s |
| 107 | +
|
| 108 | +Include: |
| 109 | +1. What changed and why |
| 110 | +2. Step-by-step migration |
| 111 | +3. Code examples (before/after) |
| 112 | +4. Timeline (deprecation → removal) |
| 113 | +5. Testing recommendations |
| 114 | +
|
| 115 | +Format as Markdown.`, oldAPI, newAPI, version, reason) |
| 116 | + |
| 117 | + resp, err := m.provider.Chat(ctx, llm.ChatRequest{ |
| 118 | + UserPrompt: prompt, |
| 119 | + Model: m.model, |
| 120 | + }) |
| 121 | + |
| 122 | + if err != nil { |
| 123 | + return "", err |
| 124 | + } |
| 125 | + |
| 126 | + return resp.Text, nil |
| 127 | +} |
| 128 | + |
| 129 | +func (m *CompatManager) calculateBreakingVersion(currentVersion string) string { |
| 130 | + parts := strings.Split(currentVersion, ".") |
| 131 | + if len(parts) < 2 { |
| 132 | + return "next major version" |
| 133 | + } |
| 134 | + |
| 135 | + return fmt.Sprintf("%s.0.0 (next major)", parts[0]) |
| 136 | +} |
| 137 | + |
| 138 | +func (m *CompatManager) SaveCompatibilityFiles(report *CompatibilityReport) error { |
| 139 | + compatDir := filepath.Join(m.workDir, "compat") |
| 140 | + if err := os.MkdirAll(compatDir, 0755); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + wrapperFile := filepath.Join(compatDir, "deprecated.go") |
| 145 | + if err := os.WriteFile(wrapperFile, []byte(report.WrapperCode), 0644); err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + |
| 149 | + guideFile := filepath.Join(m.workDir, "MIGRATION.md") |
| 150 | + if err := os.WriteFile(guideFile, []byte(report.MigrationGuide), 0644); err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func (m *CompatManager) extractCode(text string) string { |
| 158 | + text = strings.TrimSpace(text) |
| 159 | + |
| 160 | + if strings.HasPrefix(text, "```go") { |
| 161 | + text = strings.TrimPrefix(text, "```go\n") |
| 162 | + text = strings.TrimSuffix(text, "```") |
| 163 | + } else if strings.HasPrefix(text, "```") { |
| 164 | + text = strings.TrimPrefix(text, "```\n") |
| 165 | + text = strings.TrimSuffix(text, "```") |
| 166 | + } |
| 167 | + |
| 168 | + return strings.TrimSpace(text) |
| 169 | +} |
0 commit comments