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

CodexChatGptModels.ts

46 lines1.6 KB

1import type { ServerProviderModel } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Schema from "effect/Schema";
4import { HttpClient, HttpClientRequest } from "effect/http";
5​
6export class ChatGptCatalogError extends Schema.TaggedError<ChatGptCatalogError>()(
7 "ChatGptCatalogError",
8 { status: Schema.Int },
9) {}
10​
11const Catalog = Schema.Struct({
12 models: Schema.Array(
13 Schema.Struct({
14 slug: Schema.NonEmptyString,
15 display_name: Schema.NonEmptyString,
16 visibility: Schema.String,
17 }),
18 ),
19});
20​
21export const chatGptModels = Effect.fn("chatGptModels")(function* (
22 accessToken: string,
23 nativeModels: ReadonlyArray<ServerProviderModel>,
24) {
25 const http = yield* HttpClient.HttpClient;
26 const response = yield* http.execute(
27 HttpClientRequest.get("https://api.openai.com/v1/models").pipe(
28 HttpClientRequest.bearerToken(accessToken),
29 ),
30 );
31 if (response.status !== 200) return yield* new ChatGptCatalogError({ status: response.status });
32 const catalog = yield* response.json.pipe(Effect.flatMap(Schema.decodeUnknownEffect(Catalog)));
33 return catalog.models
34 .filter((model) => model.visibility === "list")
35 .map(
36 (model) =>
37 ({
38 ...nativeModels.find((native) => native.slug === model.slug),
39 capabilities:
40 nativeModels.find((native) => native.slug === model.slug)?.capabilities ?? null,
41 slug: model.slug,
42 name: model.display_name,
43 isCustom: false,
44 }) satisfies ServerProviderModel,
45 );
46}, Effect.timeout("15 seconds"));