Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import type { ServerProviderModel } from "@t3tools/contracts";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Schema from "effect/Schema";
|
|
import { HttpClient, HttpClientRequest } from "effect/http";
|
|
|
|
export class ChatGptCatalogError extends Schema.TaggedError<ChatGptCatalogError>()(
|
|
"ChatGptCatalogError",
|
|
{ status: Schema.Int },
|
|
) {}
|
|
|
|
const Catalog = Schema.Struct({
|
|
models: Schema.Array(
|
|
Schema.Struct({
|
|
slug: Schema.NonEmptyString,
|
|
display_name: Schema.NonEmptyString,
|
|
visibility: Schema.String,
|
|
}),
|
|
),
|
|
});
|
|
|
|
/** Account choices come from OpenAI; native model/list contributes capability metadata only. */
|
|
export const chatGptModels = Effect.fn("chatGptModels")(function* (
|
|
accessToken: string,
|
|
nativeModels: ReadonlyArray<ServerProviderModel>,
|
|
) {
|
|
const http = yield* HttpClient.HttpClient;
|
|
const response = yield* http.execute(
|
|
HttpClientRequest.get("https://api.openai.com/v1/models").pipe(
|
|
HttpClientRequest.bearerToken(accessToken),
|
|
),
|
|
);
|
|
if (response.status !== 200) return yield* new ChatGptCatalogError({ status: response.status });
|
|
const catalog = yield* response.json.pipe(Effect.flatMap(Schema.decodeUnknownEffect(Catalog)));
|
|
return catalog.models
|
|
.filter((model) => model.visibility === "list")
|
|
.map(
|
|
(model) =>
|
|
({
|
|
...nativeModels.find((native) => native.slug === model.slug),
|
|
capabilities:
|
|
nativeModels.find((native) => native.slug === model.slug)?.capabilities ?? null,
|
|
slug: model.slug,
|
|
name: model.display_name,
|
|
isCustom: false,
|
|
}) satisfies ServerProviderModel,
|
|
);
|
|
}, Effect.timeout("15 seconds"));
|