Open History to see the last commit
1import type { OpenCodeClient } from "@opencode/client/effect";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4
5import * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
6import type { OpenCode2Model } from "./status.ts";
7
8// Cold catalogs take seconds; model reads already retry empty catalogs.
9const PROVIDER_NAME_TIMEOUT = "3 seconds";
10
11/** Provider names are optional decoration; bound their read so models cannot stall. */
12export const loadOpenCode2Catalog = (
13 client: OpenCodeClient,
14 directory: string,
15): Effect.Effect<ReadonlyArray<OpenCode2Model>, OpenCodeRuntime.OpenCodeRuntimeError> => {
16 const location = { directory };
17 return Effect.all(
18 {
19 models: client.model.list({ location }),
20 providers: client.provider
21 .list({ location })
22 .pipe(Effect.timeout(PROVIDER_NAME_TIMEOUT), Effect.option),
23 },
24 { concurrency: "unbounded" },
25 ).pipe(
26 Effect.map(({ models, providers }) => {
27 const providerNames = new Map(
28 (Option.getOrUndefined(providers)?.data ?? []).map((provider) => [
29 provider.id,
30 provider.name,
31 ]),
32 );
33 return models.data.map((model) => {
34 const providerName = providerNames.get(model.providerID);
35 return {
36 providerID: model.providerID,
37 id: model.id,
38 name: model.name,
39 ...(providerName ? { providerName } : {}),
40 variants: model.variants.map((variant) => ({ id: variant.id })),
41 family: model.family,
42 released: model.time.released,
43 deprecated: model.status === "deprecated",
44 };
45 });
46 }),
47 Effect.mapError(
48 (cause) =>
49 new OpenCodeRuntime.OpenCodeRuntimeError({
50 operation: "model.list",
51 detail: "The OpenCode server could not list its models.",
52 cause,
53 }),
54 ),
55 );
56};