Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/opencode/src/config/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ConfigAgentV1 } from "@opencode-ai/core/v1/config/agent"
import { configEntryNameFromPath } from "./entry-name"
import * as ConfigMarkdown from "./markdown"
import { ConfigParse } from "./parse"
import { ConfigVariable } from "./variable"

export async function load(dir: string) {
const result: Record<string, ConfigAgentV1.Info> = {}
Expand All @@ -20,11 +21,16 @@ export async function load(dir: string) {
if (!md) continue

const name = configEntryNameFromPath(path.relative(dir, item), ["agent/", "agents/"])
const prompt = await ConfigVariable.substitute({
type: "path",
path: item,
text: md.content.trim() || String(md.data.prompt ?? ""),
})

const config = {
name,
...md.data,
prompt: md.content.trim(),
prompt,
}
result[config.name] = ConfigParse.schema(ConfigAgentV1.Info, config, item)
}
Expand All @@ -42,10 +48,15 @@ export async function loadMode(dir: string) {
const md = await ConfigMarkdown.parse(item).catch(() => undefined)
if (!md) continue

const prompt = await ConfigVariable.substitute({
type: "path",
path: item,
text: md.content.trim() || String(md.data.prompt ?? ""),
})
const config = {
name: configEntryNameFromPath(path.relative(dir, item), ["mode/", "modes/"]),
...md.data,
prompt: md.content.trim(),
prompt,
}
const parsed = Schema.decodeUnknownExit(ConfigAgentV1.Info)(config, { errors: "all", propertyOrder: "original" })
if (Exit.isSuccess(parsed)) {
Expand Down
35 changes: 35 additions & 0 deletions packages/opencode/test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,41 @@ Nested agent prompt`,
}),
)

it.instance("substitutes {file:} tokens in markdown agent body", () =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* FSUtil.use.writeWithDirs(
path.join(test.directory, ".opencode", "agents", "python-coder.md"),
`---
mode: subagent
---
{file:./skill.txt}`,
)
yield* FSUtil.use.writeWithDirs(path.join(test.directory, ".opencode", "agents", "skill.txt"), "Loaded from file")

const config = yield* Config.use.get()
expect(config.agent?.["python-coder"]?.prompt).toBe("Loaded from file")
}),
)

it.instance("falls back to frontmatter prompt when body is empty and substitutes {file:} tokens", () =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* FSUtil.use.writeWithDirs(
path.join(test.directory, ".opencode", "agents", "python-coder.md"),
`---
mode: subagent
prompt: "{file:./skill.txt}"
---
`,
)
yield* FSUtil.use.writeWithDirs(path.join(test.directory, ".opencode", "agents", "skill.txt"), "Frontmatter prompt")

const config = yield* Config.use.get()
expect(config.agent?.["python-coder"]?.prompt).toBe("Frontmatter prompt")
}),
)

it.instance("loads commands from .opencode/command (singular)", () =>
Effect.gen(function* () {
const test = yield* TestInstance
Expand Down
Loading