|
| 1 | +import path from "path" |
| 2 | +import { Bus } from "@/bus" |
| 3 | +import { FileWatcher } from "@/file/watcher" |
| 4 | +import { Flag } from "@/flag/flag" |
| 5 | +import { Log } from "@/util/log" |
| 6 | +import { Instance } from "./instance" |
| 7 | + |
| 8 | +export namespace HotReload { |
| 9 | + const log = Log.create({ service: "project.hotreload" }) |
| 10 | + |
| 11 | + const watched = new Set([ |
| 12 | + "agent", |
| 13 | + "agents", |
| 14 | + "command", |
| 15 | + "commands", |
| 16 | + "mode", |
| 17 | + "modes", |
| 18 | + "plugin", |
| 19 | + "plugins", |
| 20 | + "skill", |
| 21 | + "skills", |
| 22 | + "tool", |
| 23 | + "tools", |
| 24 | + ]) |
| 25 | + |
| 26 | + function normalize(file: string) { |
| 27 | + return file.split(path.sep).join("/") |
| 28 | + } |
| 29 | + |
| 30 | + function temp(file: string) { |
| 31 | + const base = file.split("/").at(-1) ?? file |
| 32 | + if (!base) return true |
| 33 | + if (base === ".DS_Store" || base === "Thumbs.db") return true |
| 34 | + if (base.startsWith(".#")) return true |
| 35 | + if (base.endsWith("~")) return true |
| 36 | + if (base.endsWith(".tmp")) return true |
| 37 | + if (base.endsWith(".swp")) return true |
| 38 | + if (base.endsWith(".swo")) return true |
| 39 | + if (base.endsWith(".swx")) return true |
| 40 | + if (base.endsWith(".bak")) return true |
| 41 | + if (base.endsWith(".orig")) return true |
| 42 | + if (base.endsWith(".rej")) return true |
| 43 | + if (base.endsWith(".crdownload")) return true |
| 44 | + return false |
| 45 | + } |
| 46 | + |
| 47 | + function rel(root: string, file: string) { |
| 48 | + const roots = new Set([normalize(root).replace(/\/+$/, "")]) |
| 49 | + const files = new Set([normalize(file)]) |
| 50 | + |
| 51 | + if (process.platform === "darwin") { |
| 52 | + for (const item of [...roots]) { |
| 53 | + if (item.startsWith("/private/")) roots.add(item.slice("/private".length)) |
| 54 | + if (item.startsWith("/var/")) roots.add(`/private${item}`) |
| 55 | + } |
| 56 | + for (const item of [...files]) { |
| 57 | + if (item.startsWith("/private/")) files.add(item.slice("/private".length)) |
| 58 | + if (item.startsWith("/var/")) files.add(`/private${item}`) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + for (const rootItem of roots) { |
| 63 | + for (const fileItem of files) { |
| 64 | + if (fileItem.includes("/.git/")) continue |
| 65 | + if (fileItem === rootItem) continue |
| 66 | + if (!fileItem.startsWith(`${rootItem}/`)) continue |
| 67 | + return fileItem.slice(rootItem.length + 1) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + export function classify(root: string, file: string) { |
| 73 | + const relFile = rel(root, file) |
| 74 | + if (!relFile) return |
| 75 | + if (temp(relFile)) return |
| 76 | + if (relFile === "opencode.json") return relFile |
| 77 | + if (relFile === "opencode.jsonc") return relFile |
| 78 | + if (relFile === "AGENTS.md") return relFile |
| 79 | + if (relFile === ".opencode/opencode.json") return relFile |
| 80 | + if (relFile === ".opencode/opencode.jsonc") return relFile |
| 81 | + if (!relFile.startsWith(".opencode/")) return |
| 82 | + if (relFile.startsWith(".opencode/openwork/")) return |
| 83 | + |
| 84 | + const parts = relFile.split("/") |
| 85 | + if (parts.length < 3) return |
| 86 | + if (!watched.has(parts[1])) return |
| 87 | + |
| 88 | + const base = parts.at(-1) ?? "" |
| 89 | + if (!base.includes(".")) return |
| 90 | + return relFile |
| 91 | + } |
| 92 | + |
| 93 | + const state = Instance.state( |
| 94 | + () => { |
| 95 | + if (!Flag.OPENCODE_HOT_RELOAD) return {} |
| 96 | + |
| 97 | + const debounce = Flag.OPENCODE_HOT_RELOAD_DEBOUNCE_MS ?? 700 |
| 98 | + const cooldown = Flag.OPENCODE_HOT_RELOAD_COOLDOWN_MS ?? 1500 |
| 99 | + let timer: ReturnType<typeof setTimeout> | undefined |
| 100 | + let busy = false |
| 101 | + let last = 0 |
| 102 | + let latest: |
| 103 | + | { |
| 104 | + file: string |
| 105 | + event: "add" | "change" | "unlink" |
| 106 | + } |
| 107 | + | undefined |
| 108 | + |
| 109 | + const flush = () => { |
| 110 | + timer = undefined |
| 111 | + if (busy) return |
| 112 | + |
| 113 | + const now = Date.now() |
| 114 | + const wait = cooldown - (now - last) |
| 115 | + if (wait > 0) { |
| 116 | + timer = setTimeout(flush, wait) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + const hit = latest |
| 121 | + if (!hit) return |
| 122 | + |
| 123 | + busy = true |
| 124 | + last = now |
| 125 | + log.info("hot reload triggered", { file: hit.file, event: hit.event }) |
| 126 | + void Instance.dispose() |
| 127 | + .catch((error) => { |
| 128 | + log.error("hot reload failed", { error, file: hit.file, event: hit.event }) |
| 129 | + }) |
| 130 | + .finally(() => { |
| 131 | + busy = false |
| 132 | + }) |
| 133 | + } |
| 134 | + |
| 135 | + const schedule = () => { |
| 136 | + if (timer) clearTimeout(timer) |
| 137 | + timer = setTimeout(flush, debounce) |
| 138 | + } |
| 139 | + |
| 140 | + const unsub = Bus.subscribe(FileWatcher.Event.Updated, (event) => { |
| 141 | + const rel = classify(Instance.directory, event.properties.file) |
| 142 | + if (!rel) return |
| 143 | + latest = { |
| 144 | + file: rel, |
| 145 | + event: event.properties.event, |
| 146 | + } |
| 147 | + schedule() |
| 148 | + }) |
| 149 | + |
| 150 | + log.info("hot reload enabled", { debounce, cooldown }) |
| 151 | + return { |
| 152 | + unsub, |
| 153 | + clear() { |
| 154 | + if (!timer) return |
| 155 | + clearTimeout(timer) |
| 156 | + timer = undefined |
| 157 | + }, |
| 158 | + } |
| 159 | + }, |
| 160 | + async (entry) => { |
| 161 | + entry.unsub?.() |
| 162 | + entry.clear?.() |
| 163 | + }, |
| 164 | + ) |
| 165 | + |
| 166 | + export function init() { |
| 167 | + state() |
| 168 | + } |
| 169 | +} |
0 commit comments