skill.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. export * as ConfigSkillPlugin from "./skill"
  2. import { define } from "@opencode-ai/plugin/v2/effect"
  3. import path from "path"
  4. import { Effect } from "effect"
  5. import { Config } from "../../config"
  6. import { AbsolutePath } from "../../schema"
  7. import { SkillV2 } from "../../skill"
  8. export const Plugin = define({
  9. id: "config-skill",
  10. effect: Effect.fn(function* (ctx) {
  11. const config = yield* Config.Service
  12. yield* ctx.skill.transform(
  13. Effect.fn(function* (draft) {
  14. const entries = yield* config.entries()
  15. const directories = entries.flatMap((entry) => (entry.type === "directory" ? [entry.path] : []))
  16. const items = entries.flatMap((entry) => (entry.type === "document" ? (entry.info.skills ?? []) : []))
  17. for (const directory of directories) {
  18. draft.source(
  19. new SkillV2.DirectorySource({ type: "directory", path: AbsolutePath.make(path.join(directory, "skill")) }),
  20. )
  21. draft.source(
  22. new SkillV2.DirectorySource({ type: "directory", path: AbsolutePath.make(path.join(directory, "skills")) }),
  23. )
  24. }
  25. for (const item of items) {
  26. if (URL.canParse(item) && /^(https?:)$/.test(new URL(item).protocol)) {
  27. draft.source(new SkillV2.UrlSource({ type: "url", url: item }))
  28. continue
  29. }
  30. const expanded = item.startsWith("~/") ? path.join(ctx.path.home, item.slice(2)) : item
  31. draft.source(
  32. new SkillV2.DirectorySource({
  33. type: "directory",
  34. path: AbsolutePath.make(
  35. path.isAbsolute(expanded) ? expanded : path.join(ctx.location.directory, expanded),
  36. ),
  37. }),
  38. )
  39. }
  40. }),
  41. )
  42. }),
  43. })