+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
261 lines
8.7 KiB
TypeScript
261 lines
8.7 KiB
TypeScript
import path from "path"
|
|
import { Context, Duration, Effect, Layer, Option, Schedule, Schema } from "effect"
|
|
import { HttpClient, HttpClientRequest } from "effect/unstable/http"
|
|
import { ModelsDev } from "@opencode-ai/schema/models-dev"
|
|
import { Global } from "./global"
|
|
import { Flag } from "./flag/flag"
|
|
import { Flock } from "./util/flock"
|
|
import { Hash } from "./util/hash"
|
|
import { FSUtil } from "./fs-util"
|
|
import { InstallationChannel, InstallationVersion } from "./installation/version"
|
|
import { EventV2 } from "./event"
|
|
import { makeGlobalNode } from "./effect/app-node"
|
|
import { httpClient } from "./effect/app-node-platform"
|
|
|
|
export const CatalogModelStatus = Schema.Literals(["alpha", "beta", "deprecated"])
|
|
export type CatalogModelStatus = typeof CatalogModelStatus.Type
|
|
|
|
const USER_AGENT = `opencode/${InstallationChannel}/${InstallationVersion}/${Flag.OPENCODE_CLIENT}`
|
|
|
|
const CostTier = Schema.Struct({
|
|
input: Schema.Finite,
|
|
output: Schema.Finite,
|
|
cache_read: Schema.optional(Schema.Finite),
|
|
cache_write: Schema.optional(Schema.Finite),
|
|
tier: Schema.Struct({
|
|
type: Schema.Literal("context"),
|
|
size: Schema.Finite,
|
|
}),
|
|
})
|
|
|
|
const Cost = Schema.Struct({
|
|
input: Schema.Finite,
|
|
output: Schema.Finite,
|
|
cache_read: Schema.optional(Schema.Finite),
|
|
cache_write: Schema.optional(Schema.Finite),
|
|
tiers: Schema.optional(Schema.Array(CostTier)),
|
|
context_over_200k: Schema.optional(
|
|
Schema.Struct({
|
|
input: Schema.Finite,
|
|
output: Schema.Finite,
|
|
cache_read: Schema.optional(Schema.Finite),
|
|
cache_write: Schema.optional(Schema.Finite),
|
|
}),
|
|
),
|
|
})
|
|
|
|
const ReasoningOption = Schema.Union([
|
|
Schema.Struct({
|
|
type: Schema.Literal("effort"),
|
|
values: Schema.Array(Schema.String),
|
|
}),
|
|
Schema.Struct({
|
|
type: Schema.Literal("toggle"),
|
|
}),
|
|
Schema.Struct({
|
|
type: Schema.Literal("budget_tokens"),
|
|
min: Schema.optional(Schema.Finite),
|
|
max: Schema.optional(Schema.Finite),
|
|
}),
|
|
])
|
|
|
|
export const Model = Schema.Struct({
|
|
id: Schema.String,
|
|
name: Schema.String,
|
|
family: Schema.optional(Schema.String),
|
|
release_date: Schema.String,
|
|
attachment: Schema.Boolean,
|
|
reasoning: Schema.Boolean,
|
|
reasoning_options: Schema.optional(Schema.Array(ReasoningOption)),
|
|
temperature: Schema.Boolean,
|
|
tool_call: Schema.Boolean,
|
|
interleaved: Schema.optional(
|
|
Schema.Union([
|
|
Schema.Literal(true),
|
|
Schema.Struct({
|
|
field: Schema.Literals(["reasoning", "reasoning_content", "reasoning_details"]),
|
|
}),
|
|
]),
|
|
),
|
|
cost: Schema.optional(Cost),
|
|
limit: Schema.Struct({
|
|
context: Schema.Finite,
|
|
input: Schema.optional(Schema.Finite),
|
|
output: Schema.Finite,
|
|
}),
|
|
modalities: Schema.optional(
|
|
Schema.Struct({
|
|
input: Schema.Array(Schema.Literals(["text", "audio", "image", "video", "pdf"])),
|
|
output: Schema.Array(Schema.Literals(["text", "audio", "image", "video", "pdf"])),
|
|
}),
|
|
),
|
|
experimental: Schema.optional(
|
|
Schema.Struct({
|
|
modes: Schema.optional(
|
|
Schema.Record(
|
|
Schema.String,
|
|
Schema.Struct({
|
|
cost: Schema.optional(Cost),
|
|
provider: Schema.optional(
|
|
Schema.Struct({
|
|
body: Schema.optional(Schema.Record(Schema.String, Schema.MutableJson)),
|
|
headers: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
}),
|
|
),
|
|
}),
|
|
),
|
|
),
|
|
}),
|
|
),
|
|
status: Schema.optional(CatalogModelStatus),
|
|
provider: Schema.optional(
|
|
Schema.Struct({ npm: Schema.optional(Schema.String), api: Schema.optional(Schema.String) }),
|
|
),
|
|
})
|
|
export type Model = Schema.Schema.Type<typeof Model>
|
|
|
|
export const Provider = Schema.Struct({
|
|
api: Schema.optional(Schema.String),
|
|
name: Schema.String,
|
|
env: Schema.Array(Schema.String),
|
|
id: Schema.String,
|
|
npm: Schema.optional(Schema.String),
|
|
models: Schema.Record(Schema.String, Model),
|
|
})
|
|
|
|
export type Provider = Schema.Schema.Type<typeof Provider>
|
|
|
|
export const Event = ModelsDev.Event
|
|
|
|
declare const OPENCODE_MODELS_DEV: Record<string, Provider> | undefined
|
|
|
|
export interface Interface {
|
|
readonly get: () => Effect.Effect<Record<string, Provider>>
|
|
readonly refresh: (force?: boolean) => Effect.Effect<void>
|
|
}
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/ModelsDev") {}
|
|
|
|
const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const fs = yield* FSUtil.Service
|
|
const events = yield* EventV2.Service
|
|
const http = HttpClient.filterStatusOk(
|
|
(yield* HttpClient.HttpClient).pipe(
|
|
HttpClient.retryTransient({
|
|
retryOn: "errors-and-responses",
|
|
times: 2,
|
|
schedule: Schedule.exponential(200).pipe(Schedule.jittered),
|
|
}),
|
|
),
|
|
)
|
|
|
|
const source = Flag.OPENCODE_MODELS_URL || "https://models.dev"
|
|
const filepath = path.join(
|
|
Global.Path.cache,
|
|
source === "https://models.dev" ? "models.json" : `models-${Hash.fast(source)}.json`,
|
|
)
|
|
const ttl = Duration.minutes(5)
|
|
const lockKey = `models-dev:${filepath}`
|
|
|
|
const fresh = Effect.fnUntraced(function* () {
|
|
const stat = yield* fs.stat(filepath).pipe(Effect.catch(() => Effect.succeed(undefined)))
|
|
if (!stat) return false
|
|
const mtime = Option.getOrElse(stat.mtime, () => new Date(0)).getTime()
|
|
return Date.now() - mtime < Duration.toMillis(ttl)
|
|
})
|
|
|
|
const fetchApi = Effect.fn("ModelsDev.fetchApi")(function* () {
|
|
return yield* HttpClientRequest.get(`${source}/api.json`).pipe(
|
|
HttpClientRequest.setHeader("User-Agent", USER_AGENT),
|
|
http.execute,
|
|
Effect.flatMap((res) => res.text),
|
|
Effect.timeout("10 seconds"),
|
|
)
|
|
})
|
|
|
|
const loadFromDisk = fs.readJson(Flag.OPENCODE_MODELS_PATH ?? filepath).pipe(
|
|
Effect.catch((error) => {
|
|
if (
|
|
Flag.OPENCODE_MODELS_PATH === undefined &&
|
|
error._tag === "FileSystemError" &&
|
|
error.method === "readJson"
|
|
) {
|
|
return fs.remove(filepath, { force: true }).pipe(Effect.ignore, Effect.as(undefined))
|
|
}
|
|
return Effect.succeed(undefined)
|
|
}),
|
|
Effect.map((v) => v as Record<string, Provider> | undefined),
|
|
)
|
|
|
|
const loadSnapshot = Effect.sync(() =>
|
|
typeof OPENCODE_MODELS_DEV === "undefined" ? undefined : OPENCODE_MODELS_DEV,
|
|
)
|
|
|
|
const fetchAndWrite = Effect.fn("ModelsDev.fetchAndWrite")(function* () {
|
|
const text = yield* fetchApi()
|
|
const tempfile = `${filepath}.${process.pid}.${Date.now()}.tmp`
|
|
yield* fs.writeWithDirs(tempfile, text).pipe(
|
|
Effect.andThen(fs.rename(tempfile, filepath)),
|
|
Effect.catch((error) =>
|
|
Effect.gen(function* () {
|
|
yield* fs.remove(tempfile, { force: true }).pipe(Effect.ignore)
|
|
return yield* Effect.fail(error)
|
|
}),
|
|
),
|
|
)
|
|
return text
|
|
})
|
|
|
|
const populate = Effect.gen(function* () {
|
|
const fromDisk = yield* loadFromDisk
|
|
if (fromDisk) return fromDisk
|
|
const snapshot = yield* loadSnapshot
|
|
if (snapshot) return snapshot
|
|
if (Flag.OPENCODE_DISABLE_MODELS_FETCH) return {}
|
|
// Flock is cross-process: concurrent opencode CLIs can race on this cache file.
|
|
const text = yield* Effect.scoped(
|
|
Effect.gen(function* () {
|
|
yield* Flock.effect(lockKey)
|
|
return yield* fetchAndWrite()
|
|
}),
|
|
)
|
|
return JSON.parse(text) as Record<string, Provider>
|
|
}).pipe(Effect.withSpan("ModelsDev.populate"), Effect.orDie)
|
|
|
|
const [cachedGet, invalidate] = yield* Effect.cachedInvalidateWithTTL(populate, Duration.infinity)
|
|
|
|
const get = (): Effect.Effect<Record<string, Provider>> => cachedGet
|
|
|
|
const refresh = Effect.fn("ModelsDev.refresh")(function* (force = false) {
|
|
if (!force && (yield* fresh())) return
|
|
yield* Effect.scoped(
|
|
Effect.gen(function* () {
|
|
yield* Flock.effect(lockKey)
|
|
// Re-check under the lock: another process may have refreshed between
|
|
// our outer check and lock acquisition.
|
|
if (!force && (yield* fresh())) return
|
|
yield* fetchAndWrite()
|
|
yield* invalidate
|
|
yield* events.publish(Event.Refreshed, {})
|
|
}),
|
|
).pipe(
|
|
Effect.tapCause((cause) => Effect.logError("Failed to fetch models.dev", { cause: cause })),
|
|
Effect.ignore,
|
|
)
|
|
})
|
|
|
|
if (!Flag.OPENCODE_DISABLE_MODELS_FETCH && !process.argv.includes("--get-yargs-completions")) {
|
|
// Schedule.spaced runs the effect once, then waits between completions.
|
|
yield* Effect.forkScoped(refresh().pipe(Effect.repeat(Schedule.spaced("60 minutes")), Effect.ignore))
|
|
}
|
|
|
|
return Service.of({ get, refresh })
|
|
}),
|
|
)
|
|
|
|
export const node = makeGlobalNode({ service: Service, layer: layer, deps: [FSUtil.node, EventV2.node, httpClient] })
|
|
|
|
export * as ModelsDev from "./models-dev"
|