export * as SkillInstructions from "./instructions.js" import { makeLocationNode } from "@opencode/util/effect/app-node" import { Context, Effect, Layer, Schema } from "effect" import { Permission } from "../permission.js" import { Skill } from "../skill.js" import { Instructions } from "../instructions/index.js" const Summary = Schema.Struct({ id: Skill.ID, name: Skill.Name, description: Schema.String, }) type Summary = typeof Summary.Type const entries = (skills: ReadonlyArray) => skills.flatMap((skill) => [ " ", ` ${skill.id}`, ` ${skill.name}`, ` ${skill.description}`, " ", ]) const render = (skills: ReadonlyArray) => [ "Skills provide specialized instructions and workflows for specific tasks.", "Use the skill tool to load a skill when a task matches its description.", "The user may also invoke a skill directly. When that happens, its instructions appear in the conversation as a block, the same shape the skill tool returns. A skill that is already present this way does not need to be invoked again.", ...(skills.length === 0 ? ["No skills are currently available."] : ["", ...entries(skills), ""]), ].join("\n") const update = (previous: ReadonlyArray, current: ReadonlyArray) => { const diff = Instructions.diffByKey( previous, current, (skill) => skill.id, (before, after) => before.name !== after.name || before.description !== after.description, ) // Additions and removals render as small deltas; anything else restates the full list. if (diff.changed.length > 0 || (diff.added.length === 0 && diff.removed.length === 0)) return [ "The available skills have changed. This list supersedes the previous available skills list.", render(current), ].join("\n") return [ ...(diff.added.length === 0 ? [] : ["New skills are available in addition to those previously listed:", ...entries(diff.added)]), ...(diff.removed.length === 0 ? [] : [ `The following skill IDs are no longer available and must not be used: ${diff.removed.map((skill) => skill.id).join(", ")}.`, ]), ].join("\n") } export interface Interface { /** Lists skills the given ruleset does not deny; callers pass the merged agent and Session permissions. */ readonly load: (permissions: Permission.Ruleset) => Effect.Effect } export class Service extends Context.Service()("@opencode/SkillInstructions") {} const layer = Layer.effect( Service, Effect.gen(function* () { const skills = yield* Skill.Service return Service.of({ load: Effect.fn("SkillInstructions.load")(function* (permissions) { const available = Skill.available(yield* skills.list(), permissions) .flatMap((skill) => skill.description === undefined || skill.autoinvoke === false ? [] : [{ id: skill.id, name: skill.name, description: skill.description }], ) .toSorted((a, b) => a.id.localeCompare(b.id)) return Instructions.make>({ key: Instructions.Key.make("core/skill-guidance"), codec: Schema.toCodecJson(Schema.Array(Summary)), read: Effect.succeed(available.length === 0 ? Instructions.removed : available), render: { initial: render, changed: update, removed: () => "Skill guidance is no longer available. Do not use any previously listed skill.", }, }) }), }) }), ) export const node = makeLocationNode({ service: Service, layer, deps: [Skill.node] })