@@ -4,31 +4,34 @@ import { define } from "@opencode/plugin/effect/plugin" |
4 | 4 | import { FSUtil } from "@opencode/util/fs-util" |
5 | 5 | import { Global } from "@opencode/util/global" |
6 | 6 | import { sameDirectory } from "@opencode/util/path" |
7 | | -import { dirname, join } from "path" |
8 | | -import { Effect, PubSub, Semaphore, Stream } from "effect" |
| 7 | +import path, { dirname, join } from "path" |
| 8 | +import { Effect, FiberMap, PubSub, Semaphore, Stream } from "effect" |
| 9 | +import { HttpClient, HttpClientRequest } from "effect/http" |
| 10 | +import { Config } from "../../config.js" |
9 | 11 | import { Watcher } from "../../filesystem/watcher.js" |
10 | 12 | import { InstructionDiscovery } from "../../instruction-discovery.js" |
11 | 13 | import { Instructions } from "../../instructions/index.js" |
12 | 14 | import { Location } from "../../location.js" |
13 | | -import { AbsolutePath } from "../../schema.js" |
14 | 15 |
|
15 | 16 | type Loaded = |
16 | 17 | | { readonly type: "available"; readonly files: InstructionDiscovery.File[] } |
17 | 18 | | { readonly type: "unavailable" } |
18 | 19 |
|
19 | 20 | export const Plugin = define({ |
20 | 21 | id: "opencode.config.instruction", |
21 | | - effect: Effect.fn(function* () { |
| 22 | + effect: Effect.fn(function* (ctx) { |
22 | 23 | const discovery = yield* InstructionDiscovery.Service |
23 | | - // Nothing this plugin watches or loads can contribute when both scopes |
24 | | - // are disabled; skip the resolves, the watcher fiber, and the transform. |
25 | | - if (!discovery.project && !discovery.global) return |
| 24 | + // Activate even when both discovery scopes are disabled: configured `instructions` |
| 25 | + // entries are explicit requests, not discovery. |
26 | 26 | yield* Effect.gen(function* () { |
| 27 | + const config = yield* Config.Service |
27 | 28 | const fs = yield* FSUtil.Service |
28 | 29 | const global = yield* Global.Service |
| 30 | + const http = HttpClient.filterStatusOk(yield* HttpClient.HttpClient) |
29 | 31 | const location = yield* Location.Service |
30 | 32 | const watcher = yield* Watcher.Service |
31 | 33 | const changes = yield* PubSub.sliding<string>(1) |
| 34 | + const watches = yield* FiberMap.make<string>() |
32 | 35 | const lock = Semaphore.makeUnsafe(1) |
33 | 36 | const start = yield* fs.resolve(location.directory) |
34 | 37 | const root = yield* fs.resolve(location.project.directory) |
@@ -37,6 +40,10 @@ export const Plugin = define({ |
37 | 40 | const stop = FSUtil.contains(home, start) ? home : root |
38 | 41 | const globalFile = yield* fs.resolve(join(global.config, "AGENTS.md")) |
39 | 42 | const loaded: { current: Loaded } = { current: { type: "available", files: [] } } |
| 43 | + const configured: { sources: string[]; remote: Map<string, InstructionDiscovery.File> } = { |
| 44 | + sources: [], |
| 45 | + remote: new Map(), |
| 46 | + } |
40 | 47 |
|
41 | 48 | const publish = (update: Watcher.Update) => PubSub.publish(changes, update.path).pipe(Effect.asVoid) |
42 | 49 | // The ancestor walk can reach the global file when the location sits |
@@ -49,15 +56,15 @@ export const Plugin = define({ |
49 | 56 | .filter((file) => discovery.global || file !== globalFile) |
50 | 57 | : []), |
51 | 58 | ] |
52 | | - for (const path of new Set(candidates)) { |
53 | | - const updates = yield* watcher.subscribe({ path, type: "file" }) |
| 59 | + for (const file of new Set(candidates)) { |
| 60 | + const updates = yield* watcher.subscribe({ path: file, type: "file" }) |
54 | 61 | yield* updates.pipe(Stream.runForEach(publish), Effect.forkScoped({ startImmediately: true })) |
55 | 62 | } |
56 | 63 |
|
57 | | - const read = Effect.fn("ConfigInstructionPlugin.read")(function* (path: string) { |
58 | | - const content = yield* fs.readFileStringSafe(path) |
59 | | - if (content !== undefined) return new InstructionDiscovery.File({ path: AbsolutePath.make(path), content }) |
60 | | - yield* Effect.logDebug("instruction file skipped", { path, reason: "unavailable" }) |
| 64 | + const read = Effect.fn("ConfigInstructionPlugin.read")(function* (filepath: string) { |
| 65 | + const content = yield* fs.readFileStringSafe(filepath) |
| 66 | + if (content !== undefined) return new InstructionDiscovery.File({ path: filepath, content }) |
| 67 | + yield* Effect.logDebug("instruction file skipped", { path: filepath, reason: "unavailable" }) |
61 | 68 | }) |
62 | 69 |
|
63 | 70 | const globalSource = Effect.fn("ConfigInstructionPlugin.globalSource")(function* () { |
@@ -78,6 +85,83 @@ export const Plugin = define({ |
78 | 85 | return files.filter((file): file is InstructionDiscovery.File => file !== undefined) |
79 | 86 | }) |
80 | 87 |
|
| 88 | + // Relative patterns search upward from the location to the project root. Without |
| 89 | + // project discovery they resolve against the global config directory instead. |
| 90 | + const match = Effect.fn("ConfigInstructionPlugin.match")(function* (source: string) { |
| 91 | + const expanded = source.startsWith("~/") ? path.join(global.home, source.slice(2)) : source |
| 92 | + const options = { absolute: true, include: "file", dot: true } as const |
| 93 | + if (path.isAbsolute(expanded)) |
| 94 | + return yield* fs |
| 95 | + .scan(path.basename(expanded), { ...options, cwd: path.dirname(expanded) }) |
| 96 | + .pipe(Effect.orElseSucceed(() => [] as string[])) |
| 97 | + if (project) return yield* fs.globUp(expanded, start, root) |
| 98 | + if (!discovery.global) return [] |
| 99 | + return yield* fs |
| 100 | + .scan(expanded, { ...options, cwd: global.config }) |
| 101 | + .pipe(Effect.orElseSucceed(() => [] as string[])) |
| 102 | + }) |
| 103 | + |
| 104 | + const configuredSource = Effect.fn("ConfigInstructionPlugin.configuredSource")(function* () { |
| 105 | + const files: InstructionDiscovery.File[] = [] |
| 106 | + const watched = new Set<string>() |
| 107 | + for (const source of configured.sources) { |
| 108 | + if (isUrl(source)) { |
| 109 | + const file = configured.remote.get(source) |
| 110 | + if (file) files.push(file) |
| 111 | + continue |
| 112 | + } |
| 113 | + for (const item of (yield* match(source)).toSorted()) { |
| 114 | + const resolved = yield* fs.resolve(item) |
| 115 | + if (watched.has(resolved)) continue |
| 116 | + watched.add(resolved) |
| 117 | + const file = yield* read(resolved) |
| 118 | + if (file) files.push(file) |
| 119 | + } |
| 120 | + } |
| 121 | + for (const [key] of Array.from(watches)) if (!watched.has(key)) yield* FiberMap.remove(watches, key) |
| 122 | + for (const file of watched) { |
| 123 | + const updates = yield* watcher.subscribe({ path: file, type: "file" }) |
| 124 | + yield* FiberMap.run(watches, file, updates.pipe(Stream.runForEach(publish)), { |
| 125 | + onlyIfMissing: true, |
| 126 | + startImmediately: true, |
| 127 | + }) |
| 128 | + } |
| 129 | + return files |
| 130 | + }) |
| 131 | + |
| 132 | + // Remote instructions are fetched only when config loads, not on every file rescan. |
| 133 | + // A failed fetch keeps the last good copy or skips the URL, so an unreachable URL |
| 134 | + // never makes the whole instruction set unavailable. |
| 135 | + const reconfigure = Effect.fn("ConfigInstructionPlugin.reconfigure")( |
| 136 | + function* () { |
| 137 | + const entries = yield* config.entries() |
| 138 | + const sources = Array.from( |
| 139 | + new Set(entries.flatMap((entry) => (entry.type === "document" ? (entry.info.instructions ?? []) : []))), |
| 140 | + ) |
| 141 | + const fetched = yield* Effect.forEach( |
| 142 | + sources.filter(isUrl), |
| 143 | + (url) => |
| 144 | + HttpClientRequest.get(url).pipe( |
| 145 | + http.execute, |
| 146 | + Effect.flatMap((response) => response.text), |
| 147 | + Effect.timeout("5 seconds"), |
| 148 | + Effect.map((content) => new InstructionDiscovery.File({ path: url, content })), |
| 149 | + Effect.catchCause((cause) => |
| 150 | + Effect.logWarning("failed to fetch instruction url", { url, cause }).pipe( |
| 151 | + Effect.as(configured.remote.get(url)), |
| 152 | + ), |
| 153 | + ), |
| 154 | + ), |
| 155 | + { concurrency: 4 }, |
| 156 | + ) |
| 157 | + configured.sources = sources |
| 158 | + configured.remote = new Map( |
| 159 | + fetched.filter((file) => file !== undefined).map((file) => [file.path, file] as const), |
| 160 | + ) |
| 161 | + }, |
| 162 | + (effect) => lock.withPermit(effect), |
| 163 | + ) |
| 164 | + |
81 | 165 | const isolate = <A, E, R>(source: string, effect: Effect.Effect<A, E, R>) => |
82 | 166 | effect.pipe( |
83 | 167 | Effect.catchCause((cause) => |
@@ -92,10 +176,11 @@ export const Plugin = define({ |
92 | 176 | const sources = yield* Effect.all({ |
93 | 177 | global: isolate("global", globalSource()), |
94 | 178 | project: isolate("project", projectSource()), |
| 179 | + configured: isolate("configured", configuredSource()), |
95 | 180 | }) |
96 | 181 | loaded.current = |
97 | | - Array.isArray(sources.global) && Array.isArray(sources.project) |
98 | | - ? { type: "available", files: [...sources.global, ...sources.project] } |
| 182 | + Array.isArray(sources.global) && Array.isArray(sources.project) && Array.isArray(sources.configured) |
| 183 | + ? { type: "available", files: [...sources.global, ...sources.project, ...sources.configured] } |
99 | 184 | : { type: "unavailable" } |
100 | 185 | if (!file) return |
101 | 186 | yield* Effect.logDebug("instructions rescanned", { |
@@ -115,6 +200,13 @@ export const Plugin = define({ |
115 | 200 | Stream.runForEach((file) => refresh(file).pipe(Effect.andThen(discovery.reload()))), |
116 | 201 | Effect.forkScoped({ startImmediately: true }), |
117 | 202 | ) |
| 203 | + // Subscribe before the first config read so an update in between still triggers a reload. |
| 204 | + yield* ctx.event.subscribe().pipe( |
| 205 | + Stream.filter((event) => event.type === "config.updated"), |
| 206 | + Stream.runForEach(() => reconfigure().pipe(Effect.andThen(refresh()), Effect.andThen(discovery.reload()))), |
| 207 | + Effect.forkScoped({ startImmediately: true }), |
| 208 | + ) |
| 209 | + yield* reconfigure() |
118 | 210 | yield* refresh() |
119 | 211 | yield* discovery.transform((editor) => { |
120 | 212 | if (loaded.current.type === "unavailable") { |
@@ -141,3 +233,7 @@ function ancestorDirectories(start: string, stop: string): string[] { |
141 | 233 | if (sameDirectory(start, stop)) return [start] |
142 | 234 | return [start, ...ancestorDirectories(dirname(start), stop)] |
143 | 235 | } |
| 236 | + |
| 237 | +function isUrl(source: string) { |
| 238 | + return URL.canParse(source) && /^https?:$/.test(new URL(source).protocol) |
| 239 | +} |