mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
Some checks failed
check-nix / nix-eval (push) Waiting to run
check / typecheck (push) Waiting to run
deploy-files / files-build (push) Waiting to run
deploy-files / files-deploy (push) Blocked by required conditions
deploy-site / site-build (push) Waiting to run
deploy-site / site-deploy (push) Blocked by required conditions
deploy / legacy-build (push) Waiting to run
deploy / legacy-deploy (push) Blocked by required conditions
deploy / data-build (push) Waiting to run
deploy / data-deploy (push) Blocked by required conditions
Process daemon / Miri (callback ownership) (push) Has been cancelled
Process daemon / Core (blacksmith-6vcpu-macos-15) (push) Has been cancelled
Process daemon / Core (ubuntu-22.04) (push) Has been cancelled
Process daemon / Windows (arm64) (push) Has been cancelled
Process daemon / Windows (x64) (push) Has been cancelled
160 lines
5.8 KiB
TypeScript
160 lines
5.8 KiB
TypeScript
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,
|
|
})),
|
|
)
|