Repository navigation
Expand file tree
/
Copy pathmodel.ts
More file actions
160 lines (145 loc) · 5.8 KB
/
Copy pathmodel.ts
File metadata and controls
160 lines (145 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
export * as Model from "./model.js"
import { Schema, Struct } from "effect"
import { optional, statics } from "./schema.js"
import { Provider } from "./provider.js"
import { Compatibility, MaxTokensField, MediaInput, ReasoningField } from "./model-compatibility.js"
import { Money } from "./money.js"
import { FreeformFormat } from "./tool.js"
import { ephemeral, inventory } from "./event.js"
export const ID = Schema.String.pipe(Schema.brand("Model.ID"))
export type ID = typeof ID.Type
const Updated = ephemeral({ type: "model.updated", schema: {} })
export const Event = { Updated, Definitions: inventory(Updated) }
export const VariantID = Schema.String.pipe(Schema.brand("Model.VariantID"))
export type VariantID = typeof VariantID.Type
export const Ref = Schema.Struct({
id: ID,
providerID: Provider.ID,
variant: VariantID.pipe(optional),
})
.annotate({ identifier: "Model.Ref" })
.pipe(
statics((schema) => ({
parse: (input: string) => {
const providerEnd = input.indexOf("/")
if (providerEnd <= 0) throw new Error(`Invalid model reference: ${input}`)
const providerID = input.slice(0, providerEnd)
const variantStart = input.indexOf("#", providerEnd + 1)
const id = input.slice(providerEnd + 1, variantStart === -1 ? undefined : variantStart)
const variant = variantStart === -1 ? undefined : input.slice(variantStart + 1)
if (!id || providerID.includes("#") || (variant !== undefined && (!variant || variant.includes("#"))))
throw new Error(`Invalid model reference: ${input}`)
return schema.make({
providerID: Provider.ID.make(providerID),
id: ID.make(id),
...(variant ? { variant: VariantID.make(variant) } : {}),
})
},
})),
)
export interface Ref extends Schema.Schema.Type<typeof Ref> {}
export { Compatibility, MaxTokensField, MediaInput, ReasoningField }
export const Family = Schema.String.pipe(Schema.brand("Model.Family"))
export type Family = typeof Family.Type
/** What a model produces, from models.dev. Open-ended: new types keep being added. Only chat models are offered for sessions; an unset type means chat. */
export type Type = "chat" | "decision" | "image" | "video" | (string & {})
export const Type: Schema.Codec<Type> = Schema.Union([
Schema.Literals(["chat", "decision", "image", "video"]),
Schema.String,
]).annotate({ identifier: "Model.Type" })
export const Settings = Schema.StructWithRest(
Schema.Struct(Struct.pick(Provider.Settings.schema.fields, ["compaction", "cache", "encoding"])),
// Provider packages may define arbitrary model-level options beyond OpenCode's shared compaction policy.
[Schema.Record(Schema.String, Schema.Any)],
).annotate({ identifier: "Model.Settings" })
export type Settings = typeof Settings.Type
// Variants switch request settings. The prompt cache belongs to the model, so a variant can't change it.
const VariantSettings = Schema.StructWithRest(
Schema.Struct(Struct.omit(Settings.schema.fields, ["cache"])),
Settings.records,
).annotate({ identifier: "Model.VariantSettings" })
export const Overlays = {
settings: Settings.pipe(optional),
headers: Schema.Record(Schema.String, Schema.String).pipe(optional),
body: Schema.Record(Schema.String, Schema.Any).pipe(optional),
}
export interface Capabilities extends Schema.Schema.Type<typeof Capabilities> {}
export const Capabilities = Schema.Struct({
tools: Schema.Boolean,
/** Freeform tool input formats advertised by the selected API. Absent or empty means JSON tools only. */
freeform: Schema.Array(FreeformFormat).pipe(optional),
input: Schema.Array(Schema.String),
output: Schema.Array(Schema.String),
/** Whether the model accepts a sampling temperature. Unknown when unset. */
temperature: Schema.Boolean.pipe(optional),
})
.annotate({ identifier: "Model.Capabilities" })
.pipe(
statics(() => ({
default: () => ({ tools: true, input: ["text", "image"], output: ["text"] }) satisfies Capabilities,
})),
)
export interface Cost extends Schema.Schema.Type<typeof Cost> {}
export const Cost = Schema.Struct({
tier: Schema.Struct({
type: Schema.tag("context"),
size: Schema.Int,
}).pipe(optional),
input: Money.USDPerMillionTokens,
output: Money.USDPerMillionTokens,
cache: Schema.Struct({
read: Money.USDPerMillionTokens,
write: Money.USDPerMillionTokens,
}),
}).annotate({ identifier: "Model.Cost" })
export interface Variant extends Schema.Schema.Type<typeof Variant> {}
export const Variant = Schema.Struct({
id: VariantID,
...Overlays,
settings: VariantSettings.pipe(optional),
}).annotate({ identifier: "Model.Variant" })
export interface Info extends Schema.Schema.Type<typeof Info> {}
export const Info = Schema.Struct({
id: ID,
modelID: ID,
providerID: Provider.ID,
canonical: Provider.ID.pipe(optional),
type: Type.pipe(optional),
family: Family.pipe(optional),
name: Schema.String,
compatibility: Compatibility.pipe(optional),
package: Provider.Package.pipe(optional),
...Overlays,
capabilities: Capabilities,
variants: Schema.Array(Variant),
time: Schema.Struct({
released: Schema.Finite,
}),
cost: Schema.Array(Cost),
status: Schema.Literals(["alpha", "beta", "deprecated", "active"]),
enabled: Schema.Boolean,
limit: Schema.Struct({
context: Schema.Int,
input: Schema.Int.pipe(optional),
output: Schema.Int,
}),
})
.annotate({ identifier: "Model.Info" })
.pipe(
statics(() => ({
default: (providerID: Provider.ID, id: ID) =>
({
id,
modelID: id,
providerID,
name: id,
capabilities: Capabilities.default(),
variants: [],
time: { released: 0 },
cost: [],
status: "active",
enabled: true,
limit: { context: 200_000, output: 32_000 },
}) satisfies Info,
})),
)