GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(core): load configured instructions (#55539) · anomalyco/opencode@1e63a3b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/core
    • src
      • config/plugin
        • instruction.ts
      • instruction-discovery.ts
    • test
      • instruction-discovery.test.ts
  • services/www/src/docs/content
    • config.mdx
    • instructions.mdx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/core/src/config/plugin/instruction.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/config/plugin/instruction.ts
+111-15Lines changed: 111 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@ import { define } from "@opencode/plugin/effect/plugin"
44
import { FSUtil } from "@opencode/util/fs-util"
55
import { Global } from "@opencode/util/global"
66
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"
911
import { Watcher } from "../../filesystem/watcher.js"
1012
import { InstructionDiscovery } from "../../instruction-discovery.js"
1113
import { Instructions } from "../../instructions/index.js"
1214
import { Location } from "../../location.js"
13-
import { AbsolutePath } from "../../schema.js"
1415

1516
type Loaded =
1617
| { readonly type: "available"; readonly files: InstructionDiscovery.File[] }
1718
| { readonly type: "unavailable" }
1819

1920
export const Plugin = define({
2021
id: "opencode.config.instruction",
21-
effect: Effect.fn(function* () {
22+
effect: Effect.fn(function* (ctx) {
2223
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.
2626
yield* Effect.gen(function* () {
27+
const config = yield* Config.Service
2728
const fs = yield* FSUtil.Service
2829
const global = yield* Global.Service
30+
const http = HttpClient.filterStatusOk(yield* HttpClient.HttpClient)
2931
const location = yield* Location.Service
3032
const watcher = yield* Watcher.Service
3133
const changes = yield* PubSub.sliding<string>(1)
34+
const watches = yield* FiberMap.make<string>()
3235
const lock = Semaphore.makeUnsafe(1)
3336
const start = yield* fs.resolve(location.directory)
3437
const root = yield* fs.resolve(location.project.directory)
@@ -37,6 +40,10 @@ export const Plugin = define({
3740
const stop = FSUtil.contains(home, start) ? home : root
3841
const globalFile = yield* fs.resolve(join(global.config, "AGENTS.md"))
3942
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+
}
4047

4148
const publish = (update: Watcher.Update) => PubSub.publish(changes, update.path).pipe(Effect.asVoid)
4249
// The ancestor walk can reach the global file when the location sits
@@ -49,15 +56,15 @@ export const Plugin = define({
4956
.filter((file) => discovery.global || file !== globalFile)
5057
: []),
5158
]
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" })
5461
yield* updates.pipe(Stream.runForEach(publish), Effect.forkScoped({ startImmediately: true }))
5562
}
5663

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" })
6168
})
6269

6370
const globalSource = Effect.fn("ConfigInstructionPlugin.globalSource")(function* () {
@@ -78,6 +85,83 @@ export const Plugin = define({
7885
return files.filter((file): file is InstructionDiscovery.File => file !== undefined)
7986
})
8087

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+
81165
const isolate = <A, E, R>(source: string, effect: Effect.Effect<A, E, R>) =>
82166
effect.pipe(
83167
Effect.catchCause((cause) =>
@@ -92,10 +176,11 @@ export const Plugin = define({
92176
const sources = yield* Effect.all({
93177
global: isolate("global", globalSource()),
94178
project: isolate("project", projectSource()),
179+
configured: isolate("configured", configuredSource()),
95180
})
96181
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] }
99184
: { type: "unavailable" }
100185
if (!file) return
101186
yield* Effect.logDebug("instructions rescanned", {
@@ -115,6 +200,13 @@ export const Plugin = define({
115200
Stream.runForEach((file) => refresh(file).pipe(Effect.andThen(discovery.reload()))),
116201
Effect.forkScoped({ startImmediately: true }),
117202
)
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()
118210
yield* refresh()
119211
yield* discovery.transform((editor) => {
120212
if (loaded.current.type === "unavailable") {
@@ -141,3 +233,7 @@ function ancestorDirectories(start: string, stop: string): string[] {
141233
if (sameDirectory(start, stop)) return [start]
142234
return [start, ...ancestorDirectories(dirname(start), stop)]
143235
}
236+
237+
function isUrl(source: string) {
238+
return URL.canParse(source) && /^https?:$/.test(new URL(source).protocol)
239+
}
Collapse file

‎packages/core/src/instruction-discovery.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/instruction-discovery.ts
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { makeLocationNode } from "@opencode/util/effect/app-node"
55
import { createPatch } from "diff"
66
import { Bus } from "./bus.js"
77
import { Instructions } from "./instructions/index.js"
8-
import { AbsolutePath } from "./schema.js"
98
import { State } from "./state.js"
109

1110
export class File extends Schema.Class<File>("InstructionDiscovery.File")({
12-
path: AbsolutePath,
11+
// An absolute file path, or the URL of a configured remote instruction.
12+
path: Schema.String,
1313
content: Schema.String,
1414
}) {}
1515

@@ -21,7 +21,7 @@ export const Event = {
2121
}
2222

2323
export type Data = {
24-
files: Map<AbsolutePath, Types.DeepMutable<File>>
24+
files: Map<string, Types.DeepMutable<File>>
2525
available: boolean
2626
}
2727

@@ -64,12 +64,12 @@ export const layer = (options?: Options) =>
6464
list: () => Array.from(editor.files.values()),
6565
add: (file) => editor.files.set(file.path, new File(file) as Types.DeepMutable<File>),
6666
update: (path, update) => {
67-
const current = editor.files.get(AbsolutePath.make(path))
67+
const current = editor.files.get(path)
6868
if (!current) return
6969
update(current)
70-
current.path = AbsolutePath.make(path)
70+
current.path = path
7171
},
72-
remove: (path) => editor.files.delete(AbsolutePath.make(path)),
72+
remove: (path) => editor.files.delete(path),
7373
unavailable: () => {
7474
editor.available = false
7575
},

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.