skill.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. export * as SkillV2 from "./skill"
  2. import path from "path"
  3. import { Context, Effect, Layer, Schema, Types } from "effect"
  4. import { AgentV2 } from "./agent"
  5. import { ConfigMarkdown } from "./config/markdown"
  6. import { FSUtil } from "./fs-util"
  7. import { PermissionV2 } from "./permission"
  8. import { AbsolutePath, withStatics } from "./schema"
  9. import { SkillDiscovery } from "./skill/discovery"
  10. import { State } from "./state"
  11. export class DirectorySource extends Schema.Class<DirectorySource>("SkillV2.DirectorySource")({
  12. type: Schema.Literal("directory"),
  13. path: AbsolutePath,
  14. }) {}
  15. export class UrlSource extends Schema.Class<UrlSource>("SkillV2.UrlSource")({
  16. type: Schema.Literal("url"),
  17. url: Schema.String,
  18. }) {}
  19. export class EmbeddedSource extends Schema.Class<EmbeddedSource>("SkillV2.EmbeddedSource")({
  20. type: Schema.Literal("embedded"),
  21. skill: Schema.suspend(() => Info),
  22. }) {}
  23. export const Source = Schema.Union([DirectorySource, UrlSource, EmbeddedSource]).pipe(
  24. Schema.toTaggedUnion("type"),
  25. withStatics(() => ({
  26. equals: (a: DirectorySource | UrlSource | EmbeddedSource, b: DirectorySource | UrlSource | EmbeddedSource) => {
  27. if (a.type !== b.type) return false
  28. if (a.type === "directory" && b.type === "directory") return a.path === b.path
  29. if (a.type === "url" && b.type === "url") return a.url === b.url
  30. if (a.type === "embedded" && b.type === "embedded") return a.skill.name === b.skill.name
  31. return false
  32. },
  33. key: (source: DirectorySource | UrlSource | EmbeddedSource) =>
  34. source.type === "directory"
  35. ? `directory:${source.path}`
  36. : source.type === "url"
  37. ? `url:${source.url}`
  38. : `embedded:${source.skill.name}`,
  39. })),
  40. )
  41. export type Source = typeof Source.Type
  42. export class Info extends Schema.Class<Info>("SkillV2.Info")({
  43. name: Schema.String,
  44. description: Schema.String.pipe(Schema.optional),
  45. slash: Schema.Boolean.pipe(Schema.optional),
  46. location: AbsolutePath,
  47. content: Schema.String,
  48. }) {}
  49. export const available = (skills: ReadonlyArray<Info>, agent: AgentV2.Info) =>
  50. skills.filter((skill) => PermissionV2.evaluate("skill", skill.name, agent.permissions).effect !== "deny")
  51. const Frontmatter = Schema.Struct({
  52. name: Schema.String.pipe(Schema.optional),
  53. description: Schema.String.pipe(Schema.optional),
  54. slash: Schema.Boolean.pipe(Schema.optional),
  55. })
  56. const decodeFrontmatter = Schema.decodeUnknownOption(Frontmatter)
  57. export type Data = {
  58. sources: Types.DeepMutable<Source>[]
  59. }
  60. export type Draft = {
  61. source: (source: Source) => void
  62. list: () => readonly Source[]
  63. }
  64. export interface Interface extends State.Transformable<Draft> {
  65. readonly sources: () => Effect.Effect<Source[]>
  66. readonly list: () => Effect.Effect<Info[]>
  67. }
  68. export class Service extends Context.Service<Service, Interface>()("@opencode/v2/Skill") {}
  69. export const layer = Layer.effect(
  70. Service,
  71. Effect.gen(function* () {
  72. const discovery = yield* SkillDiscovery.Service
  73. const fs = yield* FSUtil.Service
  74. const state = State.create<Data, Draft>({
  75. initial: () => ({ sources: [] }),
  76. draft: (draft) => ({
  77. source: (source) => {
  78. if (draft.sources.some((item) => Source.equals(item, source))) return
  79. draft.sources.push(source as Types.DeepMutable<Source>)
  80. },
  81. list: () => draft.sources as Source[],
  82. }),
  83. })
  84. const load = Effect.fn("SkillV2.load")(function* (source: Source) {
  85. const skills: Info[] = []
  86. if (source.type === "embedded") return [source.skill]
  87. const directories = source.type === "directory" ? [source.path] : yield* discovery.pull(source.url)
  88. for (const directory of directories) {
  89. const files = yield* fs
  90. .glob("{*.md,**/SKILL.md}", { cwd: directory, absolute: true, include: "file", symlink: true, dot: true })
  91. .pipe(Effect.catch(() => Effect.succeed([] as string[])))
  92. for (const filepath of files.toSorted()) {
  93. const content = yield* fs.readFileStringSafe(filepath).pipe(Effect.catch(() => Effect.succeed(undefined)))
  94. if (!content) continue
  95. const markdown = ConfigMarkdown.parseOption(content)
  96. if (!markdown) continue
  97. const frontmatter = decodeFrontmatter(markdown.data).valueOrUndefined
  98. if (!frontmatter) continue
  99. const name =
  100. frontmatter.name !== undefined
  101. ? frontmatter.name
  102. : path.dirname(filepath) === directory
  103. ? path.basename(filepath, ".md")
  104. : undefined
  105. if (!name) continue
  106. skills.push(
  107. new Info({
  108. name,
  109. description: frontmatter.description,
  110. slash: frontmatter.slash,
  111. location: AbsolutePath.make(filepath),
  112. content: markdown.content,
  113. }),
  114. )
  115. }
  116. }
  117. return skills
  118. })
  119. // QUESTION(Dax): Should local skill sources invalidate on filesystem watch
  120. // events, following the reload policy chosen for other context sources?
  121. const cache = new Map<string, Info[]>()
  122. const list = Effect.fn("SkillV2.list")(function* () {
  123. const skills = new Map<string, Info>()
  124. for (const source of state.get().sources) {
  125. const key = Source.key(source)
  126. const loaded = cache.get(key) ?? (yield* load(source))
  127. cache.set(key, loaded)
  128. for (const skill of loaded) skills.set(skill.name, skill)
  129. }
  130. return Array.from(skills.values())
  131. })
  132. return Service.of({
  133. transform: state.transform,
  134. reload: state.reload,
  135. sources: Effect.fn("SkillV2.sources")(function* () {
  136. return state.get().sources
  137. }),
  138. list,
  139. })
  140. }),
  141. )
  142. export const locationLayer = layer.pipe(Layer.provide(SkillDiscovery.defaultLayer))