skill.ts 2.0 KB

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