mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
refactor(core): accept inline skill content or a loader
This commit is contained in:
parent
1d927dd7c7
commit
573f8d71cc
8 changed files with 48 additions and 45 deletions
|
|
@ -453,7 +453,8 @@ export const make = Effect.fn("PluginHost.make")(function* (
|
|||
callback({
|
||||
list: () => mutable(editor.list()),
|
||||
get: editor.get,
|
||||
add: (value, load) => editor.add(Schema.decodeUnknownSync(Skill.Info)(value), load),
|
||||
add: (value: Skill.Info & { readonly content?: string }, load?: Skill.Load) =>
|
||||
editor.add({ ...Schema.decodeUnknownSync(Skill.Info)(value), content: value.content }, load),
|
||||
update: editor.update,
|
||||
remove: editor.remove,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -25,24 +25,20 @@ export const Plugin = define({
|
|||
effect: Effect.fn(function* (ctx) {
|
||||
const reportContent = yield* reportContentWithDiagnostics(ctx.app)
|
||||
yield* ctx.skill.transform((editor) => {
|
||||
editor.add(
|
||||
Skill.Info.make({
|
||||
id: Skill.ID.make("opencode"),
|
||||
name: Skill.Name.make("OpenCode"),
|
||||
description: OpencodeDescription,
|
||||
path: AbsolutePath.make("/builtin/opencode.md"),
|
||||
}),
|
||||
() => Effect.succeed(OpencodeContent),
|
||||
)
|
||||
editor.add(
|
||||
Skill.Info.make({
|
||||
id: Skill.ID.make("report"),
|
||||
name: Skill.Name.make("Report"),
|
||||
description: REPORT_DESCRIPTION,
|
||||
path: AbsolutePath.make("/builtin/report.md"),
|
||||
}),
|
||||
() => Effect.succeed(reportContent),
|
||||
)
|
||||
editor.add({
|
||||
id: Skill.ID.make("opencode"),
|
||||
name: Skill.Name.make("OpenCode"),
|
||||
description: OpencodeDescription,
|
||||
path: AbsolutePath.make("/builtin/opencode.md"),
|
||||
content: OpencodeContent,
|
||||
})
|
||||
editor.add({
|
||||
id: Skill.ID.make("report"),
|
||||
name: Skill.Name.make("Report"),
|
||||
description: REPORT_DESCRIPTION,
|
||||
path: AbsolutePath.make("/builtin/report.md"),
|
||||
content: reportContent,
|
||||
})
|
||||
})
|
||||
}),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -83,7 +83,8 @@ export type Data = {
|
|||
export type Editor = {
|
||||
list: () => readonly Types.DeepMutable<Info>[]
|
||||
get: (id: string) => Types.DeepMutable<Info> | undefined
|
||||
add: (skill: Info, load: Load) => void
|
||||
/** Pass `content` inline, or a `load` that produces it when the skill is used. */
|
||||
add: (skill: Info & { readonly content?: string }, load?: Load) => void
|
||||
update: (id: string, update: (skill: Types.DeepMutable<Info>) => void) => void
|
||||
remove: (id: string) => void
|
||||
}
|
||||
|
|
@ -107,8 +108,9 @@ const layer = Layer.effect(
|
|||
list: () => Array.from(editor.skills.values()),
|
||||
get: (id) => editor.skills.get(ID.make(id)),
|
||||
add: (skill, load) => {
|
||||
editor.skills.set(skill.id, { ...skill } as Types.DeepMutable<Info>)
|
||||
editor.loaders.set(skill.id, load)
|
||||
const { content, ...info } = skill
|
||||
editor.skills.set(info.id, info as Types.DeepMutable<Info>)
|
||||
editor.loaders.set(info.id, load ?? (() => Effect.succeed(content ?? "")))
|
||||
},
|
||||
update: (id, update) => {
|
||||
const current = editor.skills.get(ID.make(id))
|
||||
|
|
|
|||
|
|
@ -17,14 +17,13 @@ const info = (id: string, description: string) =>
|
|||
description,
|
||||
path: AbsolutePath.make(`/skills/${id}/SKILL.md`),
|
||||
})
|
||||
|
||||
const add = (editor: Skill.Editor, skill: Skill.Info) => editor.add(skill, () => Effect.succeed(`# ${skill.id}`))
|
||||
const loaded = (id: string, description: string) => ({ ...info(id, description), content: `# ${id}` })
|
||||
|
||||
describe("Skill", () => {
|
||||
it.effect("reads the current editor entry by ID", () =>
|
||||
Effect.gen(function* () {
|
||||
const skill = yield* Skill.Service
|
||||
yield* skill.transform((editor) => add(editor, info("review", "Initial")))
|
||||
yield* skill.transform((editor) => editor.add(loaded("review", "Initial")))
|
||||
yield* skill.transform((editor) => {
|
||||
expect(editor.get("review")).toBe(editor.list()[0])
|
||||
expect(editor.get("missing")).toBeUndefined()
|
||||
|
|
@ -44,9 +43,9 @@ describe("Skill", () => {
|
|||
Effect.gen(function* () {
|
||||
const skill = yield* Skill.Service
|
||||
yield* skill.transform((editor) => {
|
||||
add(editor, info("review", "First"))
|
||||
add(editor, info("deploy", "Deploy"))
|
||||
add(editor, info("review", "Second"))
|
||||
editor.add(loaded("review", "First"))
|
||||
editor.add(loaded("deploy", "Deploy"))
|
||||
editor.add(loaded("review", "Second"))
|
||||
expect(editor.list().map((item) => item.id)).toEqual([Skill.ID.make("review"), Skill.ID.make("deploy")])
|
||||
})
|
||||
|
||||
|
|
@ -60,7 +59,7 @@ describe("Skill", () => {
|
|||
Effect.gen(function* () {
|
||||
const skill = yield* Skill.Service
|
||||
yield* skill.transform((editor) => {
|
||||
add(editor, info("review", "Initial"))
|
||||
editor.add(loaded("review", "Initial"))
|
||||
editor.update("review", (value) => {
|
||||
value.description = "Updated"
|
||||
value.id = Skill.ID.make("ignored")
|
||||
|
|
@ -68,7 +67,7 @@ describe("Skill", () => {
|
|||
editor.update("missing", () => {
|
||||
throw new Error("unreachable")
|
||||
})
|
||||
add(editor, info("deploy", "Deploy"))
|
||||
editor.add(loaded("deploy", "Deploy"))
|
||||
editor.remove("deploy")
|
||||
})
|
||||
|
||||
|
|
@ -80,7 +79,7 @@ describe("Skill", () => {
|
|||
Effect.gen(function* () {
|
||||
const skill = yield* Skill.Service
|
||||
const original = info("review", "Initial")
|
||||
yield* skill.transform((editor) => add(editor, original))
|
||||
yield* skill.transform((editor) => editor.add({ ...original, content: "# review" }))
|
||||
const updated = yield* skill.transform((editor) =>
|
||||
editor.update("review", (value) => {
|
||||
value.description = "Updated"
|
||||
|
|
@ -105,7 +104,7 @@ describe("Skill", () => {
|
|||
)
|
||||
yield* Effect.yieldNow
|
||||
|
||||
yield* skill.transform((editor) => add(editor, info("review", "Visible")))
|
||||
yield* skill.transform((editor) => editor.add(loaded("review", "Visible")))
|
||||
expect(yield* Deferred.await(updated).pipe(Effect.timeout("1 second"))).toEqual([info("review", "Visible")])
|
||||
yield* Fiber.interrupt(fiber)
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ import type { Transform } from "./registration.js"
|
|||
export interface SkillEditor {
|
||||
list(): readonly Types.DeepMutable<Skill.Info>[]
|
||||
get(id: string): Types.DeepMutable<Skill.Info> | undefined
|
||||
/** `load` produces the skill body (SKILL.md without frontmatter) when the skill is used. */
|
||||
add(skill: Skill.Info & { readonly content: string }): void
|
||||
/** `load` produces the skill body (SKILL.md without frontmatter) when the skill is used, so nothing is held until then. */
|
||||
add(skill: Skill.Info, load: () => Effect.Effect<string>): void
|
||||
update(id: string, update: (skill: Types.DeepMutable<Skill.Info>) => void): void
|
||||
remove(id: string): void
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { Tool } from "@opencode/schema/tool"
|
||||
import type { Rpc } from "@opencode/schema/rpc"
|
||||
import type { Skill } from "@opencode/schema/skill"
|
||||
import type { RpcCallOptions, RpcEventPayload } from "@opencode/client/promise/api"
|
||||
import { Effect, Schema, SchemaAST, Stream } from "effect"
|
||||
import type { Scope } from "effect"
|
||||
|
|
@ -457,7 +458,10 @@ export function fromPromise(plugin: Plugin) {
|
|||
host.skill.transform((editor) =>
|
||||
callback({
|
||||
...editor,
|
||||
add: (skill, load) => editor.add(skill, () => Effect.promise(load)),
|
||||
add: (skill: Skill.Info & { readonly content?: string }, load?: () => Promise<string>) =>
|
||||
load
|
||||
? editor.add(skill, () => Effect.promise(load))
|
||||
: editor.add({ ...skill, content: skill.content ?? "" }),
|
||||
}),
|
||||
),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ import type { DeepMutable } from "./types.js"
|
|||
export interface SkillEditor {
|
||||
list(): readonly DeepMutable<Skill.Info>[]
|
||||
get(id: string): DeepMutable<Skill.Info> | undefined
|
||||
/** `load` produces the skill body (SKILL.md without frontmatter) when the skill is used. */
|
||||
add(skill: Skill.Info & { readonly content: string }): void
|
||||
/** `load` produces the skill body (SKILL.md without frontmatter) when the skill is used, so nothing is held until then. */
|
||||
add(skill: Skill.Info, load: () => Promise<string>): void
|
||||
update(id: string, update: (skill: DeepMutable<Skill.Info>) => void): void
|
||||
remove(id: string): void
|
||||
|
|
|
|||
|
|
@ -810,16 +810,15 @@ when the skill is absent.
|
|||
```ts
|
||||
await ctx.skill.transform((editor) => {
|
||||
const skills = editor.list()
|
||||
editor.add(
|
||||
{
|
||||
id: "review",
|
||||
name: "Review",
|
||||
description: "Review the current changes",
|
||||
path: "/workspace/.opencode/skills/review.md",
|
||||
},
|
||||
// Runs when the skill is used; the registry holds only metadata.
|
||||
async () => "Review the current changes for correctness and missing tests.",
|
||||
)
|
||||
editor.add({
|
||||
id: "review",
|
||||
name: "Review",
|
||||
description: "Review the current changes",
|
||||
path: "/workspace/.opencode/skills/review.md",
|
||||
content: "Review the current changes for correctness and missing tests.",
|
||||
})
|
||||
// Or pass a loader as the second argument to read the body only when the skill is used.
|
||||
editor.add({ id: "remote", name: "Remote", path: "/skills/remote.md" }, async () => fetchBody())
|
||||
const review = editor.get("review")
|
||||
editor.update("review", (skill) => {
|
||||
skill.autoinvoke = true
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue