model.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { DateTime, Schema } from "effect"
  2. import { DateTimeUtcFromMillis } from "effect/Schema"
  3. import { ProviderV2 } from "./provider"
  4. export const ID = Schema.String.pipe(Schema.brand("ModelV2.ID"))
  5. export type ID = typeof ID.Type
  6. export const VariantID = Schema.String.pipe(Schema.brand("VariantID"))
  7. export type VariantID = typeof VariantID.Type
  8. // Grouping of models, eg claude opus, claude sonnet
  9. export const Family = Schema.String.pipe(Schema.brand("Family"))
  10. export type Family = typeof Family.Type
  11. export const Capabilities = Schema.Struct({
  12. tools: Schema.Boolean,
  13. // mime patterns, image, audio, video/*, text/*
  14. input: Schema.String.pipe(Schema.Array),
  15. output: Schema.String.pipe(Schema.Array),
  16. })
  17. export type Capabilities = typeof Capabilities.Type
  18. export const Cost = Schema.Struct({
  19. tier: Schema.Struct({
  20. type: Schema.Literal("context"),
  21. size: Schema.Int,
  22. }).pipe(Schema.optional),
  23. input: Schema.Finite,
  24. output: Schema.Finite,
  25. cache: Schema.Struct({
  26. read: Schema.Finite,
  27. write: Schema.Finite,
  28. }),
  29. })
  30. export const Ref = Schema.Struct({
  31. id: ID,
  32. providerID: ProviderV2.ID,
  33. variant: VariantID.pipe(Schema.optional),
  34. })
  35. export type Ref = typeof Ref.Type
  36. export const Api = Schema.Union([
  37. Schema.Struct({
  38. id: ID,
  39. ...ProviderV2.AISDK.fields,
  40. }),
  41. Schema.Struct({
  42. id: ID,
  43. ...ProviderV2.Native.fields,
  44. }),
  45. ]).pipe(Schema.toTaggedUnion("type"))
  46. export type Api = typeof Api.Type
  47. export class Info extends Schema.Class<Info>("ModelV2.Info")({
  48. id: ID,
  49. providerID: ProviderV2.ID,
  50. family: Family.pipe(Schema.optional),
  51. name: Schema.String,
  52. api: Api,
  53. capabilities: Capabilities,
  54. request: Schema.Struct({
  55. ...ProviderV2.Request.fields,
  56. variant: Schema.String.pipe(Schema.optional),
  57. }),
  58. variants: Schema.Struct({
  59. id: VariantID,
  60. ...ProviderV2.Request.fields,
  61. }).pipe(Schema.Array),
  62. time: Schema.Struct({
  63. released: DateTimeUtcFromMillis,
  64. }),
  65. cost: Cost.pipe(Schema.Array),
  66. status: Schema.Literals(["alpha", "beta", "deprecated", "active"]),
  67. enabled: Schema.Boolean,
  68. limit: Schema.Struct({
  69. context: Schema.Int,
  70. input: Schema.Int.pipe(Schema.optional),
  71. output: Schema.Int,
  72. }),
  73. }) {
  74. static empty(providerID: ProviderV2.ID, modelID: ID): Info {
  75. return new Info({
  76. id: modelID,
  77. providerID,
  78. name: modelID,
  79. api: {
  80. id: modelID,
  81. type: "native",
  82. settings: {},
  83. },
  84. capabilities: {
  85. tools: false,
  86. input: [],
  87. output: [],
  88. },
  89. request: {
  90. headers: {},
  91. body: {},
  92. },
  93. variants: [],
  94. time: {
  95. released: DateTime.makeUnsafe(0),
  96. },
  97. cost: [],
  98. status: "active",
  99. enabled: true,
  100. limit: {
  101. context: 0,
  102. output: 0,
  103. },
  104. })
  105. }
  106. }
  107. export function parse(input: string): { providerID: ProviderV2.ID; modelID: ID } {
  108. const [providerID, ...modelID] = input.split("/")
  109. return {
  110. providerID: ProviderV2.ID.make(providerID),
  111. modelID: ID.make(modelID.join("/")),
  112. }
  113. }
  114. export * as ModelV2 from "./model"