mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-10 03:08:27 +00:00
64 lines
2.5 KiB
TypeScript
64 lines
2.5 KiB
TypeScript
export * as ConfigReferencePlugin from "./reference"
|
|
|
|
import { define } from "@opencode-ai/plugin/v2/effect"
|
|
import path from "path"
|
|
import { Effect } from "effect"
|
|
import { Config } from "../../config"
|
|
import { ConfigReference } from "../reference"
|
|
import { Reference } from "../../reference"
|
|
import { AbsolutePath } from "../../schema"
|
|
|
|
export const Plugin = define({
|
|
id: "core/config-reference",
|
|
effect: Effect.fn(function* (ctx) {
|
|
const config = yield* Config.Service
|
|
yield* ctx.reference.transform(
|
|
Effect.fn(function* (draft) {
|
|
const entries = new Map<string, Reference.Source>()
|
|
for (const doc of (yield* config.entries()).filter(
|
|
(entry): entry is Config.Document => entry.type === "document",
|
|
)) {
|
|
const directory = doc.path ? path.dirname(doc.path) : ctx.location.directory
|
|
for (const [name, entry] of Object.entries(doc.info.references ?? {})) {
|
|
if (!validAlias(name)) continue
|
|
entries.set(
|
|
name,
|
|
local(entry)
|
|
? new Reference.LocalSource({
|
|
type: "local",
|
|
path: AbsolutePath.make(
|
|
localPath(directory, ctx.path.home, typeof entry === "string" ? entry : entry.path),
|
|
),
|
|
description: typeof entry === "string" ? undefined : entry.description,
|
|
hidden: typeof entry === "string" ? undefined : entry.hidden,
|
|
})
|
|
: new Reference.GitSource({
|
|
type: "git",
|
|
repository: typeof entry === "string" ? entry : entry.repository,
|
|
branch: typeof entry === "string" ? undefined : entry.branch,
|
|
description: typeof entry === "string" ? undefined : entry.description,
|
|
hidden: typeof entry === "string" ? undefined : entry.hidden,
|
|
}),
|
|
)
|
|
}
|
|
}
|
|
for (const [name, source] of entries) draft.add(name, source)
|
|
}),
|
|
)
|
|
}),
|
|
})
|
|
|
|
function validAlias(name: string) {
|
|
return name.length > 0 && !/[\/\s`,]/.test(name)
|
|
}
|
|
|
|
function local(entry: ConfigReference.Entry): entry is string | ConfigReference.Local {
|
|
return typeof entry === "string"
|
|
? entry.startsWith(".") || entry.startsWith("/") || entry.startsWith("~")
|
|
: "path" in entry
|
|
}
|
|
|
|
function localPath(directory: string, home: string, value: string) {
|
|
if (value.startsWith("~/")) return path.join(home, value.slice(2))
|
|
return path.isAbsolute(value) ? value : path.resolve(directory, value)
|
|
}
|