From 573f8d71cc577c5baab8ef53e483d5aa0f9ef604 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Mon, 14 Sep 2026 16:43:35 -0500 Subject: [PATCH] refactor(core): accept inline skill content or a loader --- packages/core/src/plugin/host.ts | 3 +- packages/core/src/plugin/skill.ts | 32 ++++++++----------- packages/core/src/skill.ts | 8 +++-- packages/core/test/skill.test.ts | 19 ++++++----- packages/plugin/src/effect/skill.ts | 3 +- packages/plugin/src/promise/adapter.ts | 6 +++- packages/plugin/src/promise/skill.ts | 3 +- .../src/docs/content/build/plugins/index.mdx | 19 ++++++----- 8 files changed, 48 insertions(+), 45 deletions(-) diff --git a/packages/core/src/plugin/host.ts b/packages/core/src/plugin/host.ts index f1d0d17d49a..1ad591bccda 100644 --- a/packages/core/src/plugin/host.ts +++ b/packages/core/src/plugin/host.ts @@ -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, }) diff --git a/packages/core/src/plugin/skill.ts b/packages/core/src/plugin/skill.ts index 39cad96c77c..65560e8e169 100644 --- a/packages/core/src/plugin/skill.ts +++ b/packages/core/src/plugin/skill.ts @@ -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, + }) }) }), }) diff --git a/packages/core/src/skill.ts b/packages/core/src/skill.ts index 2b0f0c2812a..08d998e610b 100644 --- a/packages/core/src/skill.ts +++ b/packages/core/src/skill.ts @@ -83,7 +83,8 @@ export type Data = { export type Editor = { list: () => readonly Types.DeepMutable[] get: (id: string) => Types.DeepMutable | 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) => 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) - editor.loaders.set(skill.id, load) + const { content, ...info } = skill + editor.skills.set(info.id, info as Types.DeepMutable) + editor.loaders.set(info.id, load ?? (() => Effect.succeed(content ?? ""))) }, update: (id, update) => { const current = editor.skills.get(ID.make(id)) diff --git a/packages/core/test/skill.test.ts b/packages/core/test/skill.test.ts index e55a4bd7984..3d2104b2e0e 100644 --- a/packages/core/test/skill.test.ts +++ b/packages/core/test/skill.test.ts @@ -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) }), diff --git a/packages/plugin/src/effect/skill.ts b/packages/plugin/src/effect/skill.ts index 2a49d798730..8a545e3b82f 100644 --- a/packages/plugin/src/effect/skill.ts +++ b/packages/plugin/src/effect/skill.ts @@ -6,7 +6,8 @@ import type { Transform } from "./registration.js" export interface SkillEditor { list(): readonly Types.DeepMutable[] get(id: string): Types.DeepMutable | 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): void update(id: string, update: (skill: Types.DeepMutable) => void): void remove(id: string): void diff --git a/packages/plugin/src/promise/adapter.ts b/packages/plugin/src/promise/adapter.ts index f7999827184..1d724b81d4a 100644 --- a/packages/plugin/src/promise/adapter.ts +++ b/packages/plugin/src/promise/adapter.ts @@ -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) => + load + ? editor.add(skill, () => Effect.promise(load)) + : editor.add({ ...skill, content: skill.content ?? "" }), }), ), ), diff --git a/packages/plugin/src/promise/skill.ts b/packages/plugin/src/promise/skill.ts index 672a1bef5e3..e0dfbe34f45 100644 --- a/packages/plugin/src/promise/skill.ts +++ b/packages/plugin/src/promise/skill.ts @@ -6,7 +6,8 @@ import type { DeepMutable } from "./types.js" export interface SkillEditor { list(): readonly DeepMutable[] get(id: string): DeepMutable | 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): void update(id: string, update: (skill: DeepMutable) => void): void remove(id: string): void diff --git a/services/www/src/docs/content/build/plugins/index.mdx b/services/www/src/docs/content/build/plugins/index.mdx index ae12a56d5df..d5eb93c66c8 100644 --- a/services/www/src/docs/content/build/plugins/index.mdx +++ b/services/www/src/docs/content/build/plugins/index.mdx @@ -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