Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

openCode2Catalog.ts

59 lines1.9 KB

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/**
9 * A server loads its provider list with the rest of its catalog, which takes a couple of seconds
10 * on a cold start, and the model loader retries while the catalog is empty anyway.
11 */
12const PROVIDER_NAME_TIMEOUT = "3 seconds";
13​
14/**
15 * The models carry the payload and the names are decoration, so a provider read is bounded as
16 * well as optional: one that never answers must not hold the models back.
17 */
18export const loadOpenCode2Catalog = (
19 client: OpenCodeClient,
20 directory: string,
21): Effect.Effect<ReadonlyArray<OpenCode2Model>, OpenCodeRuntime.OpenCodeRuntimeError> => {
22 const location = { directory };
23 return Effect.all(
24 {
25 models: client.model.list({ location }),
26 providers: client.provider
27 .list({ location })
28 .pipe(Effect.timeout(PROVIDER_NAME_TIMEOUT), Effect.option),
29 },
30 { concurrency: "unbounded" },
31 ).pipe(
32 Effect.map(({ models, providers }) => {
33 const providerNames = new Map(
34 (Option.getOrUndefined(providers)?.data ?? []).map((provider) => [
35 provider.id,
36 provider.name,
37 ]),
38 );
39 return models.data.map((model) => {
40 const providerName = providerNames.get(model.providerID);
41 return {
42 providerID: model.providerID,
43 id: model.id,
44 name: model.name,
45 ...(providerName ? { providerName } : {}),
46 variants: model.variants.map((variant) => ({ id: variant.id })),
47 };
48 });
49 }),
50 Effect.mapError(
51 (cause) =>
52 new OpenCodeRuntime.OpenCodeRuntimeError({
53 operation: "model.list",
54 detail: "The OpenCode server could not list its models.",
55 cause,
56 }),
57 ),
58 );
59};