fix(config): resolve agent/command names from relative paths (#28359)

This commit is contained in:
Kit Langton
2026-05-19 16:48:32 -04:00
committed by GitHub
parent c035c35eba
commit e94d46af86
4 changed files with 73 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
export * as ConfigAgent from "./agent"
import path from "path"
import { Exit, Schema, SchemaGetter } from "effect"
import { PositiveInt } from "@opencode-ai/core/schema"
import * as Log from "@opencode-ai/core/util/log"
@@ -116,8 +117,7 @@ export async function load(dir: string) {
})
if (!md) continue
const patterns = ["/.opencode/agent/", "/.opencode/agents/", "/agent/", "/agents/"]
const name = configEntryNameFromPath(item, patterns)
const name = configEntryNameFromPath(path.relative(dir, item), ["agent/", "agents/"])
const config = {
name,
@@ -144,7 +144,7 @@ export async function loadMode(dir: string) {
if (!md) continue
const config = {
name: configEntryNameFromPath(item, []),
name: configEntryNameFromPath(path.relative(dir, item), ["mode/", "modes/"]),
...md.data,
prompt: md.content.trim(),
}

View File

@@ -1,5 +1,6 @@
export * as ConfigCommand from "./command"
import path from "path"
import * as Log from "@opencode-ai/core/util/log"
import { Cause, Exit, Schema } from "effect"
import { Glob } from "@opencode-ai/core/util/glob"
@@ -36,8 +37,7 @@ export async function load(dir: string) {
})
if (!md) continue
const patterns = ["/.opencode/command/", "/.opencode/commands/", "/command/", "/commands/"]
const name = configEntryNameFromPath(item, patterns)
const name = configEntryNameFromPath(path.relative(dir, item), ["command/", "commands/"])
const config = {
name,

View File

@@ -1,16 +1,19 @@
import path from "path"
function sliceAfterMatch(filePath: string, searchRoots: string[]) {
const normalizedPath = filePath.replaceAll("\\", "/")
for (const searchRoot of searchRoots) {
const index = normalizedPath.indexOf(searchRoot)
if (index === -1) continue
return normalizedPath.slice(index + searchRoot.length)
// Strips a known prefix from an already-relative path. Callers should pass the
// path relative to the directory they scanned (e.g. `path.relative(dir, item)`)
// so the prefix match is anchored. Matching anywhere in an absolute path used
// to mis-key agents whose home/parent segments coincidentally contained one of
// the prefix names (see #25713).
function stripPrefix(relativePath: string, prefixes: string[]) {
const normalized = relativePath.replaceAll("\\", "/")
for (const prefix of prefixes) {
if (normalized.startsWith(prefix)) return normalized.slice(prefix.length)
}
}
export function configEntryNameFromPath(filePath: string, searchRoots: string[]) {
const candidate = sliceAfterMatch(filePath, searchRoots) ?? path.basename(filePath)
export function configEntryNameFromPath(relativePath: string, prefixes: string[]) {
const candidate = stripPrefix(relativePath, prefixes) ?? path.basename(relativePath)
const ext = path.extname(candidate)
return ext.length ? candidate.slice(0, -ext.length) : candidate
}