Open History to see the last commit
1import type { ServerProviderModel } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Schema from "effect/Schema";
4import { HttpClient, HttpClientRequest } from "effect/http";
5import { mapCodexModelCapabilities } from "./CodexProvider.ts";
6
7export class ChatGptCatalogError extends Schema.TaggedError<ChatGptCatalogError>()(
8 "ChatGptCatalogError",
9 { status: Schema.Int },
10) {}
11
12const Catalog = Schema.Struct({
13 models: Schema.Array(
14 Schema.Struct({
15 slug: Schema.NonEmptyString,
16 display_name: Schema.NonEmptyString,
17 visibility: Schema.String,
18 default_reasoning_level: Schema.optionalKey(Schema.NullOr(Schema.NonEmptyString)),
19 supported_reasoning_levels: Schema.optionalKey(
20 Schema.NullOr(
21 Schema.Array(
22 Schema.Struct({
23 effort: Schema.NonEmptyString,
24 description: Schema.optionalKey(Schema.NullOr(Schema.String)),
25 }),
26 ),
27 ),
28 ),
29 }),
30 ),
31});
32
33export const chatGptModels = Effect.fn("chatGptModels")(function* (
34 accessToken: string,
35 nativeModels: ReadonlyArray<ServerProviderModel>,
36) {
37 const http = yield* HttpClient.HttpClient;
38 const response = yield* http.execute(
39 HttpClientRequest.get("https://api.openai.com/v1/models").pipe(
40 HttpClientRequest.bearerToken(accessToken),
41 ),
42 );
43 if (response.status !== 200) return yield* new ChatGptCatalogError({ status: response.status });
44 const catalog = yield* response.json.pipe(Effect.flatMap(Schema.decodeUnknownEffect(Catalog)));
45 return catalog.models
46 .filter((model) => model.visibility === "list")
47 .map((model): ServerProviderModel => {
48 const native = nativeModels.find((candidate) => candidate.slug === model.slug);
49 // A pinned Codex CLI can predate a model the account already has; the catalog still lists its efforts.
50 const catalogCapabilities =
51 model.default_reasoning_level && model.supported_reasoning_levels
52 ? mapCodexModelCapabilities({
53 id: model.slug,
54 model: model.slug,
55 displayName: model.display_name,
56 description: "",
57 hidden: false,
58 isDefault: false,
59 defaultReasoningEffort: model.default_reasoning_level,
60 supportedReasoningEfforts: model.supported_reasoning_levels.map((level) => ({
61 reasoningEffort: level.effort,
62 description: level.description ?? "",
63 })),
64 })
65 : null;
66 return {
67 ...native,
68 capabilities: native?.capabilities ?? catalogCapabilities,
69 slug: model.slug,
70 name: model.display_name,
71 isCustom: false,
72 };
73 });
74}, Effect.timeout("15 seconds"));