Skip to content

Commit 342132b

Browse files
committed
feat(core): show context files with ~ and symlinked config paths
1 parent 251fbaa commit 342132b

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎packages/core/src/plugin/custom/context.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
export * as CustomContextPlugin from "./context.js"
22

3-
import { relative } from "path"
3+
import { join, relative } from "path"
44
import { define } from "@opencode/plugin/effect/plugin"
55
import { CustomContext } from "@opencode/schema/custom-context"
66
import { FSUtil } from "@opencode/util/fs-util"
7+
import { Global } from "@opencode/util/global"
78
import { Effect, Option, Schema } from "effect"
89
import { InstructionDiscovery } from "../../instruction-discovery.js"
910
import { Instructions } from "../../instructions/index.js"
@@ -16,15 +17,19 @@ export const Plugin = define({
1617
id: "custom.context",
1718
effect: Effect.fn("CustomContextPlugin")(function* (ctx) {
1819
const discovery = yield* InstructionDiscovery.Service
20+
const fs = yield* FSUtil.Service
1921
const location = yield* Location.Service
22+
const global = yield* Global.Service
23+
const config = global.config
24+
const configReal = yield* fs.resolve(config)
2025

2126
yield* ctx.rpc
2227
.register(CustomContext.Contract, {
2328
list: (input) =>
2429
Effect.gen(function* () {
2530
const loaded = yield* discovery.list()
2631
const history = yield* ctx.session.context({ sessionID: input.sessionID })
27-
return contextFiles(location.project.directory, loaded, history)
32+
return contextFiles(location.project.directory, global.home, config, configReal, loaded, history)
2833
}).pipe(Effect.orDie),
2934
})
3035
.pipe(Effect.orDie)
@@ -37,6 +42,9 @@ const InjectedPaths = Schema.Struct({
3742

3843
export function contextFiles(
3944
root: string,
45+
home: string,
46+
config: string,
47+
configReal: string,
4048
loaded: ReadonlyArray<InstructionDiscovery.File> | Instructions.Unavailable,
4149
history: ReadonlyArray<SessionMessage.Info>,
4250
) {
@@ -47,6 +55,16 @@ export function contextFiles(
4755
const ambient = Array.isArray(loaded) ? loaded.map((file) => file.path) : []
4856
return {
4957
unavailable: !Array.isArray(loaded),
50-
files: [...new Set([...ambient, ...nested])].map((file) => (FSUtil.contains(root, file) ? relative(root, file) : file)),
58+
files: [...new Set([...ambient, ...nested])].map((file) => display(root, home, config, configReal, file)),
5159
}
5260
}
61+
62+
// Discovery stores real paths, so a config dir that is a symlink shows its target.
63+
// Display the path the user configured instead.
64+
function display(root: string, home: string, config: string, configReal: string, file: string) {
65+
if (FSUtil.contains(root, file)) return relative(root, file)
66+
const logical = FSUtil.contains(configReal, file) ? join(config, relative(configReal, file)) : file
67+
if (!FSUtil.contains(home, logical)) return logical
68+
const rest = relative(home, logical)
69+
return rest === "" ? "~" : `~/${rest}`
70+
}

‎packages/core/test/plugin/custom.test.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,31 @@ it.effect("collects context files without duplicates and flags unavailable disco
8080
}),
8181
]
8282
const ambient = [new InstructionDiscovery.File({ path: AbsolutePath.make(`${project}/AGENTS.md`), content: "root" })]
83-
expect(CustomContextPlugin.contextFiles(project, ambient, history)).toEqual({
83+
const home = "/home/user"
84+
const config = `${home}/.config/opencode`
85+
expect(CustomContextPlugin.contextFiles(project, home, config, config, ambient, history)).toEqual({
8486
unavailable: false,
8587
files: ["AGENTS.md", "packages/web/AGENTS.md"],
8688
})
87-
expect(CustomContextPlugin.contextFiles(project, Instructions.unavailable, history)).toEqual({
89+
expect(CustomContextPlugin.contextFiles(project, home, config, config, Instructions.unavailable, history)).toEqual({
8890
unavailable: true,
8991
files: ["AGENTS.md", "packages/web/AGENTS.md"],
9092
})
91-
expect(CustomContextPlugin.contextFiles(project, [], [])).toEqual({ unavailable: false, files: [] })
93+
expect(CustomContextPlugin.contextFiles(project, home, config, config, [], [])).toEqual({
94+
unavailable: false,
95+
files: [],
96+
})
97+
const linked = `${home}/dotfiles/.config/opencode`
98+
const global = [
99+
new InstructionDiscovery.File({
100+
path: AbsolutePath.make(`${linked}/AGENTS.md`),
101+
content: "global",
102+
}),
103+
]
104+
expect(CustomContextPlugin.contextFiles(project, home, config, linked, global, [])).toEqual({
105+
unavailable: false,
106+
files: ["~/.config/opencode/AGENTS.md"],
107+
})
92108
}),
93109
)
94110

0 commit comments

Comments
 (0)