skill.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. export * as Skill from "./skill"
  2. import { Schema } from "effect"
  3. import { AbsolutePath } from "./schema"
  4. export interface DirectorySource extends Schema.Schema.Type<typeof DirectorySource> {}
  5. export const DirectorySource = Schema.Struct({
  6. type: Schema.Literal("directory"),
  7. path: AbsolutePath,
  8. }).annotate({ identifier: "SkillV2.DirectorySource" })
  9. export interface UrlSource extends Schema.Schema.Type<typeof UrlSource> {}
  10. export const UrlSource = Schema.Struct({
  11. type: Schema.Literal("url"),
  12. url: Schema.String,
  13. }).annotate({ identifier: "SkillV2.UrlSource" })
  14. export interface Info extends Schema.Schema.Type<typeof Info> {}
  15. export const Info = Schema.Struct({
  16. name: Schema.String,
  17. description: Schema.String.pipe(Schema.optional),
  18. slash: Schema.Boolean.pipe(Schema.optional),
  19. location: AbsolutePath,
  20. content: Schema.String,
  21. }).annotate({ identifier: "SkillV2.Info" })
  22. export interface EmbeddedSource extends Schema.Schema.Type<typeof EmbeddedSource> {}
  23. export const EmbeddedSource = Schema.Struct({
  24. type: Schema.Literal("embedded"),
  25. skill: Schema.suspend(() => Info),
  26. }).annotate({ identifier: "SkillV2.EmbeddedSource" })
  27. export type Source = DirectorySource | UrlSource | EmbeddedSource
  28. export const Source = Object.assign(
  29. Schema.Union([DirectorySource, UrlSource, EmbeddedSource]).pipe(
  30. Schema.toTaggedUnion("type"),
  31. Schema.annotate({ identifier: "SkillV2.Source" }),
  32. ),
  33. {
  34. equals: (a: Source, b: Source) => {
  35. if (a.type !== b.type) return false
  36. if (a.type === "directory" && b.type === "directory") return a.path === b.path
  37. if (a.type === "url" && b.type === "url") return a.url === b.url
  38. if (a.type === "embedded" && b.type === "embedded") return a.skill.name === b.skill.name
  39. return false
  40. },
  41. key: (source: Source) =>
  42. source.type === "directory"
  43. ? `directory:${source.path}`
  44. : source.type === "url"
  45. ? `url:${source.url}`
  46. : `embedded:${source.skill.name}`,
  47. },
  48. )