|
| 1 | +import { Octokit } from "octokit"; |
| 2 | +import { getOctokit } from "./octokit/core"; |
| 3 | +import { getGithubConfigs } from "@utils/github/configs"; |
| 4 | +import { DEFAULT_TEMPLATE_REPO, DEFAULT_GITHUB_OWNER } from "@github-utils"; |
| 5 | +import { getDb } from "@db"; |
| 6 | +import { standardizationRules } from "@db/schemas/app/standardization"; |
| 7 | +import { eq } from "drizzle-orm"; |
| 8 | +import type { Agent } from "@openai/agents"; |
| 9 | +import { createRunner, resolveDefaultAiModel, resolveDefaultAiProvider } from "@/ai/agents/base/agent-ai"; |
| 10 | + |
| 11 | +export class StandardizationService { |
| 12 | + private static STANDARD_REPO_OWNER = DEFAULT_GITHUB_OWNER; |
| 13 | + private static STANDARD_REPO_NAME = DEFAULT_TEMPLATE_REPO; |
| 14 | + |
| 15 | + /** |
| 16 | + * Enforce standards on a repository |
| 17 | + * @param env Worker Environment |
| 18 | + * @param targetRepo Target repository metadata |
| 19 | + */ |
| 20 | + static async enforce(env: Env, targetRepo: { owner: { login: string }, name: string, default_branch?: string }) { |
| 21 | + console.log(`[Standardization] Enforcing standards on ${targetRepo.owner.login}/${targetRepo.name}`); |
| 22 | + |
| 23 | + const octokit = await getOctokit(env) as unknown as Octokit; |
| 24 | + const config = getGithubConfigs(env); |
| 25 | + const db = getDb(env.DB); |
| 26 | + |
| 27 | + // 1. Infer Infrastructure Tags for Target Repo |
| 28 | + // We need to list files to infer tags. |
| 29 | + let infraTags: string[] = ["Repository"]; |
| 30 | + try { |
| 31 | + const { data: tree } = await octokit.rest.git.getTree({ |
| 32 | + owner: targetRepo.owner.login, |
| 33 | + repo: targetRepo.name, |
| 34 | + tree_sha: targetRepo.default_branch || 'main', |
| 35 | + recursive: '1' |
| 36 | + }); |
| 37 | + |
| 38 | + const paths = (tree.tree || []).map(t => t.path || "").filter(Boolean); |
| 39 | + infraTags = this.inferProjectTags(paths); |
| 40 | + console.log(`[Standardization] Inferred tags for ${targetRepo.name}:`, infraTags); |
| 41 | + } catch (e) { |
| 42 | + console.warn(`[Standardization] Failed to infer tags (likely empty repo or no access), defaulting to basic tags.`, e); |
| 43 | + } |
| 44 | + |
| 45 | + // 2. Fetch Rules from DB |
| 46 | + const rules = await db.select().from(standardizationRules).all(); |
| 47 | + |
| 48 | + // 3. Apply Rules |
| 49 | + for (const rule of rules) { |
| 50 | + await this.applyRule(env, octokit, config, rule, targetRepo, infraTags); |
| 51 | + } |
| 52 | + |
| 53 | + console.log(`[Standardization] Completed enforcement for ${targetRepo.owner.login}/${targetRepo.name}`); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Apply a single rule |
| 58 | + */ |
| 59 | + private static async applyRule( |
| 60 | + env: Env, |
| 61 | + octokit: Octokit, |
| 62 | + config: any, |
| 63 | + rule: typeof standardizationRules.$inferSelect, |
| 64 | + targetRepo: { owner: { login: string }, name: string, default_branch?: string }, |
| 65 | + targetTags: string[] |
| 66 | + ) { |
| 67 | + // A. Check Relevance |
| 68 | + const relevantInfra = JSON.parse(rule.relevantInfra); |
| 69 | + const irrelevantInfra = JSON.parse(rule.irrelevantInfra); |
| 70 | + |
| 71 | + // Logic: |
| 72 | + // - If relevantInfra is empty, it applies to ALL (unless excluded). |
| 73 | + // - If relevantInfra has items, target MUST have at least one. |
| 74 | + // - If target has ANY tag in irrelevantInfra, skip. |
| 75 | + |
| 76 | + if (irrelevantInfra.length > 0 && targetTags.some(tag => irrelevantInfra.includes(tag))) { |
| 77 | + // console.debug(`[Standardization] Skipping ${rule.filePath} (Irrelevant Infra)`); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (relevantInfra.length > 0 && !targetTags.some(tag => relevantInfra.includes(tag))) { |
| 82 | + // console.debug(`[Standardization] Skipping ${rule.filePath} (Missing Relevant Infra)`); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // B. Fetch Source Content |
| 87 | + let content: string | null = null; |
| 88 | + let sourceSha: string | undefined; |
| 89 | + |
| 90 | + try { |
| 91 | + const [sourceOwner, sourceRepo] = rule.sourceRepo.split('/'); |
| 92 | + const { data: sourceFile } = await octokit.rest.repos.getContent({ |
| 93 | + owner: sourceOwner, |
| 94 | + repo: sourceRepo, |
| 95 | + path: rule.filePath |
| 96 | + }); |
| 97 | + |
| 98 | + if (!Array.isArray(sourceFile) && sourceFile.type === "file" && sourceFile.content) { |
| 99 | + content = Buffer.from(sourceFile.content, "base64").toString("utf8"); |
| 100 | + sourceSha = sourceFile.sha; |
| 101 | + } |
| 102 | + } catch (e: any) { |
| 103 | + console.warn(`[Standardization] Source file ${rule.sourceRepo}/${rule.filePath} not found.`, e.message); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (!content) return; |
| 108 | + |
| 109 | + // C. AI Customization |
| 110 | + if (rule.aiInstructions) { |
| 111 | + try { |
| 112 | + const provider = resolveDefaultAiProvider(env); |
| 113 | + const model = resolveDefaultAiModel(env, provider); |
| 114 | + |
| 115 | + const runner = await createRunner(env, provider, model); |
| 116 | + const { Agent: OpenAIAgent } = await import("@openai/agents"); |
| 117 | + const agent = new OpenAIAgent({ |
| 118 | + name: "StandardizationCustomizer", |
| 119 | + model, |
| 120 | + instructions: "Customize the file content based on the instructions. Return ONLY the full customized file content. No markdown fences. Do not output any conversational text.", |
| 121 | + }); |
| 122 | + |
| 123 | + const prompt = ` |
| 124 | + Instructions: "${rule.aiInstructions}" |
| 125 | +
|
| 126 | + Target Context: |
| 127 | + Repo: ${targetRepo.owner.login}/${targetRepo.name} |
| 128 | + Tags: ${targetTags.join(', ')} |
| 129 | +
|
| 130 | + File Content: |
| 131 | + ${content} |
| 132 | + `; |
| 133 | + |
| 134 | + const result = await runner.run(agent, prompt); |
| 135 | + let customized = typeof result.finalOutput === 'string' ? result.finalOutput : String(result.finalOutput); |
| 136 | + |
| 137 | + // Strip markdown fences if present |
| 138 | + customized = customized.replace(/^```[a-z]*\n/i, '').replace(/\n```$/, '').trim(); |
| 139 | + |
| 140 | + if (customized) { |
| 141 | + content = customized; |
| 142 | + sourceSha = undefined; |
| 143 | + } |
| 144 | + |
| 145 | + } catch (aiError) { |
| 146 | + console.error(`[Standardization] AI Customization failed for ${rule.filePath}`, aiError); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // D. Sync to Target |
| 151 | + try { |
| 152 | + // Check existence |
| 153 | + let targetSha: string | undefined; |
| 154 | + try { |
| 155 | + const { data: targetFile } = await octokit.rest.repos.getContent({ |
| 156 | + owner: targetRepo.owner.login, |
| 157 | + repo: targetRepo.name, |
| 158 | + path: rule.filePath |
| 159 | + }); |
| 160 | + |
| 161 | + if (!Array.isArray(targetFile) && targetFile.type === "file") { |
| 162 | + targetSha = targetFile.sha; |
| 163 | + |
| 164 | + // If we shouldn't overwrite and it exists, skip. |
| 165 | + // Exception: if we want to enforce updates, we check overWrite policy. |
| 166 | + if (!rule.shouldOverwrite) { |
| 167 | + // console.debug(`[Standardization] Skipping ${rule.filePath} (Exists & No Overwrite)`); |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + // Optimization: If no AI customization happened, we can compare SHAs (if source was pure). |
| 172 | + if (sourceSha && targetFile.sha === sourceSha) { |
| 173 | + return; |
| 174 | + } |
| 175 | + } |
| 176 | + } catch (err: any) { |
| 177 | + if (err.status !== 404) throw err; |
| 178 | + } |
| 179 | + |
| 180 | + // Write |
| 181 | + await octokit.rest.repos.createOrUpdateFileContents({ |
| 182 | + owner: targetRepo.owner.login, |
| 183 | + repo: targetRepo.name, |
| 184 | + path: rule.filePath, |
| 185 | + message: `chore(standards): sync ${rule.filePath}`, |
| 186 | + content: Buffer.from(content || "").toString("base64"), |
| 187 | + sha: targetSha, |
| 188 | + branch: targetRepo.default_branch |
| 189 | + }); |
| 190 | + |
| 191 | + console.log(`[Standardization] Synced ${rule.filePath} to ${targetRepo.name}`); |
| 192 | + |
| 193 | + } catch (e: any) { |
| 194 | + console.error(`[Standardization] Failed to write ${rule.filePath}`, e); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + /** |
| 200 | + * Infer project tags (Simplified version of logic in projects.ts) |
| 201 | + * We duplicate slightly to avoid circular dependency on "routes" logic or we should move logic to shared util. |
| 202 | + * Moving to shared util is better but for now let's keep it self-contained or use what we can. |
| 203 | + */ |
| 204 | + private static inferProjectTags(paths: string[]): string[] { |
| 205 | + const tags = new Set<string>(["Repository"]); |
| 206 | + const lowerPaths = paths.map(p => p.toLowerCase()); |
| 207 | + |
| 208 | + if (lowerPaths.some(p => p.endsWith("wrangler.toml") || p.endsWith("wrangler.json") || p.endsWith("wrangler.jsonc"))) { |
| 209 | + tags.add("cloudflare_worker"); |
| 210 | + tags.add("cloudflare"); |
| 211 | + } |
| 212 | + if (lowerPaths.some(p => p.endsWith("package.json"))) tags.add("nodejs"); |
| 213 | + if (lowerPaths.some(p => p.endsWith(".py") || p.endsWith("requirements.txt"))) tags.add("python"); |
| 214 | + if (lowerPaths.some(p => p.includes("next.config"))) tags.add("nextjs"); |
| 215 | + if (lowerPaths.some(p => p.includes("astro.config"))) tags.add("astro"); |
| 216 | + |
| 217 | + return Array.from(tags); |
| 218 | + } |
| 219 | +} |
| 220 | + |
0 commit comments