|
| 1 | +import { execFile } from "node:child_process" |
| 2 | +import { basename, dirname, resolve } from "node:path" |
| 3 | +import { promisify } from "node:util" |
| 4 | + |
| 5 | +import type { Plugin } from "@opencode-ai/plugin" |
| 6 | +import { tool } from "@opencode-ai/plugin" |
| 7 | + |
| 8 | +const execFileAsync = promisify(execFile) |
| 9 | + |
| 10 | +type ValidationResult = { |
| 11 | + ok: boolean |
| 12 | + command: string |
| 13 | + cwd: string |
| 14 | + output: string |
| 15 | + error?: string |
| 16 | +} |
| 17 | + |
| 18 | +export const RenderPlugin: Plugin = async ({ client }) => { |
| 19 | + await client.app.log({ |
| 20 | + body: { |
| 21 | + service: "render-opencode-plugin", |
| 22 | + level: "info", |
| 23 | + message: "Render OpenCode plugin initialized", |
| 24 | + }, |
| 25 | + }) |
| 26 | + |
| 27 | + return { |
| 28 | + tool: { |
| 29 | + render_validate_blueprint: tool({ |
| 30 | + description: "Validate a Render Blueprint file with the Render CLI.", |
| 31 | + args: { |
| 32 | + path: tool.schema.string().describe("Path to render.yaml or render.yml."), |
| 33 | + }, |
| 34 | + async execute(args) { |
| 35 | + const result = await validateBlueprint(args.path) |
| 36 | + return { |
| 37 | + title: result.ok ? "Render Blueprint valid" : "Render Blueprint validation failed", |
| 38 | + output: result.output, |
| 39 | + metadata: { |
| 40 | + command: result.command, |
| 41 | + cwd: result.cwd, |
| 42 | + ok: result.ok, |
| 43 | + error: result.error, |
| 44 | + }, |
| 45 | + } |
| 46 | + }, |
| 47 | + }), |
| 48 | + }, |
| 49 | + |
| 50 | + "tool.execute.after": async (input, output) => { |
| 51 | + const blueprintPath = extractTouchedFiles(input.args).find(isBlueprintFile) |
| 52 | + if (!blueprintPath) { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + const result = await validateBlueprint(blueprintPath) |
| 57 | + if (result.ok) { |
| 58 | + await client.app.log({ |
| 59 | + body: { |
| 60 | + service: "render-opencode-plugin", |
| 61 | + level: "info", |
| 62 | + message: "Validated Render Blueprint", |
| 63 | + extra: { |
| 64 | + path: blueprintPath, |
| 65 | + cwd: result.cwd, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + output.title = "Render Blueprint validation" |
| 73 | + output.output = [output.output, result.output].filter(Boolean).join("\n\n") |
| 74 | + output.metadata = { |
| 75 | + ...output.metadata, |
| 76 | + renderBlueprintValidation: { |
| 77 | + ok: false, |
| 78 | + command: result.command, |
| 79 | + cwd: result.cwd, |
| 80 | + error: result.error, |
| 81 | + }, |
| 82 | + } |
| 83 | + }, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function isBlueprintFile(filePath: string) { |
| 88 | + const name = basename(filePath) |
| 89 | + return name === "render.yaml" || name === "render.yml" |
| 90 | +} |
| 91 | + |
| 92 | +function getBlueprintWorkingDirectory(filePath: string) { |
| 93 | + return dirname(resolve(filePath)) |
| 94 | +} |
| 95 | + |
| 96 | +async function validateBlueprint(filePath: string): Promise<ValidationResult> { |
| 97 | + const cwd = getBlueprintWorkingDirectory(filePath) |
| 98 | + const command = "render blueprints validate" |
| 99 | + |
| 100 | + try { |
| 101 | + const result = await execFileAsync("render", ["blueprints", "validate"], { cwd }) |
| 102 | + const output = [result.stdout, result.stderr].filter(Boolean).join("\n").trim() |
| 103 | + return { |
| 104 | + ok: true, |
| 105 | + command, |
| 106 | + cwd, |
| 107 | + output: output || "render blueprints validate completed successfully.", |
| 108 | + } |
| 109 | + } catch (error) { |
| 110 | + if (isNodeError(error) && error.code === "ENOENT") { |
| 111 | + return { |
| 112 | + ok: false, |
| 113 | + command, |
| 114 | + cwd, |
| 115 | + output: "Render CLI not found. Install it to validate render.yaml:\n macOS: brew install render\n Linux: curl -fsSL https://raw.githubusercontent.com/render-oss/cli/main/bin/install.sh | sh", |
| 116 | + error: "render-cli-not-found", |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + const output = getExecErrorOutput(error) |
| 121 | + return { |
| 122 | + ok: false, |
| 123 | + command, |
| 124 | + cwd, |
| 125 | + output: output || String(error), |
| 126 | + error: "validation-failed", |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +function extractTouchedFiles(value: unknown): string[] { |
| 132 | + const files: string[] = [] |
| 133 | + collectTouchedFiles(value, files) |
| 134 | + return files |
| 135 | +} |
| 136 | + |
| 137 | +const filePathKeys = new Set(["file", "filePath", "file_path", "filename", "path"]) |
| 138 | + |
| 139 | +function collectTouchedFiles(value: unknown, files: string[], key?: string) { |
| 140 | + if (typeof value === "string") { |
| 141 | + if (key && filePathKeys.has(key)) { |
| 142 | + files.push(value) |
| 143 | + } |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + if (Array.isArray(value)) { |
| 148 | + for (const item of value) { |
| 149 | + collectTouchedFiles(item, files) |
| 150 | + } |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + if (typeof value !== "object" || value === null) { |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + for (const [entryKey, entryValue] of Object.entries(value)) { |
| 159 | + collectTouchedFiles(entryValue, files, entryKey) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +function getExecErrorOutput(error: unknown) { |
| 164 | + if (typeof error === "object" && error !== null) { |
| 165 | + const withOutput = error as { stdout?: unknown; stderr?: unknown; message?: unknown } |
| 166 | + return [withOutput.stdout, withOutput.stderr, withOutput.message] |
| 167 | + .filter((value): value is string => typeof value === "string" && value.length > 0) |
| 168 | + .join("\n") |
| 169 | + .trim() |
| 170 | + } |
| 171 | + |
| 172 | + return "" |
| 173 | +} |
| 174 | + |
| 175 | +function isNodeError(error: unknown): error is NodeJS.ErrnoException { |
| 176 | + return typeof error === "object" && error !== null && "code" in error |
| 177 | +} |
| 178 | + |
| 179 | +export default RenderPlugin |
0 commit comments