skill.ts 1.8 KB

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