prompt.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { Schema } from "effect"
  2. import { optional } from "./schema"
  3. import { statics } from "./schema"
  4. export interface Source extends Schema.Schema.Type<typeof Source> {}
  5. export const Source = Schema.Struct({
  6. start: Schema.Finite,
  7. end: Schema.Finite,
  8. text: Schema.String,
  9. }).annotate({ identifier: "Prompt.Source" })
  10. export interface FileAttachment extends Schema.Schema.Type<typeof FileAttachment> {}
  11. export const FileAttachment = Schema.Struct({
  12. uri: Schema.String,
  13. mime: Schema.String,
  14. name: Schema.String.pipe(optional),
  15. description: Schema.String.pipe(optional),
  16. source: Source.pipe(optional),
  17. })
  18. .annotate({ identifier: "Prompt.FileAttachment" })
  19. .pipe(
  20. statics((schema) => ({
  21. create: (input: FileAttachment) =>
  22. schema.make({
  23. uri: input.uri,
  24. mime: input.mime,
  25. name: input.name,
  26. description: input.description,
  27. source: input.source,
  28. }),
  29. })),
  30. )
  31. export interface AgentAttachment extends Schema.Schema.Type<typeof AgentAttachment> {}
  32. export const AgentAttachment = Schema.Struct({
  33. name: Schema.String,
  34. source: Source.pipe(optional),
  35. }).annotate({ identifier: "Prompt.AgentAttachment" })
  36. export interface Prompt extends Schema.Schema.Type<typeof Prompt> {}
  37. export const Prompt = Schema.Struct({
  38. text: Schema.String,
  39. files: Schema.Array(FileAttachment).pipe(optional),
  40. agents: Schema.Array(AgentAttachment).pipe(optional),
  41. })
  42. .annotate({ identifier: "Prompt" })
  43. .pipe(
  44. statics((schema) => ({
  45. equivalence: Schema.toEquivalence(schema),
  46. fromUserMessage: (input: Pick<Prompt, "text" | "files" | "agents">) =>
  47. schema.make({
  48. text: input.text,
  49. ...(input.files === undefined ? {} : { files: input.files }),
  50. ...(input.agents === undefined ? {} : { agents: input.agents }),
  51. }),
  52. })),
  53. )