| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- export * as ConfigCommand from "./command"
- import * as Log from "@opencode-ai/core/util/log"
- import { Cause, Exit, Schema } from "effect"
- import { Glob } from "@opencode-ai/core/util/glob"
- import { configEntryNameFromPath } from "./entry-name"
- import { InvalidError } from "./error"
- import * as ConfigMarkdown from "./markdown"
- import { ConfigModelID } from "./model-id"
- const log = Log.create({ service: "config" })
- export const Info = Schema.Struct({
- template: Schema.String,
- description: Schema.optional(Schema.String),
- agent: Schema.optional(Schema.String),
- model: Schema.optional(ConfigModelID),
- subtask: Schema.optional(Schema.Boolean),
- })
- export type Info = Schema.Schema.Type<typeof Info>
- const decodeInfo = Schema.decodeUnknownExit(Info)
- export async function load(dir: string) {
- const result: Record<string, Info> = {}
- for (const item of await Glob.scan("{command,commands}/**/*.md", {
- cwd: dir,
- absolute: true,
- dot: true,
- symlink: true,
- })) {
- const md = await ConfigMarkdown.parse(item).catch((err) => {
- log.error("failed to load command", { command: item, err })
- return undefined
- })
- if (!md) continue
- const patterns = ["/.opencode/command/", "/.opencode/commands/", "/command/", "/commands/"]
- const name = configEntryNameFromPath(item, patterns)
- const config = {
- name,
- ...md.data,
- template: md.content.trim(),
- }
- const parsed = decodeInfo(config, { errors: "all", propertyOrder: "original" })
- if (Exit.isSuccess(parsed)) {
- result[config.name] = parsed.value
- continue
- }
- throw new InvalidError({ path: item, message: Cause.pretty(parsed.cause) }, { cause: Cause.squash(parsed.cause) })
- }
- return result
- }
|