Open History to see the last commit
1import * as nodePath from "node:path";
2import { type ServerProvider, ServerProvider as ServerProviderSchema } from "@t3tools/contracts";
3import { Cause, Effect, FileSystem, Schema } from "effect";
4
5import { writeFileStringAtomically } from "../atomicWrite.ts";
6
7export const PROVIDER_CACHE_IDS = [
8 "codex",
9 "claudeAgent",
10 "opencode",
11 "cursor",
12] as const satisfies ReadonlyArray<ServerProvider["provider"]>;
13
14const decodeProviderStatusCache = Schema.decodeUnknownEffect(
15 Schema.fromJsonString(ServerProviderSchema),
16);
17
18const providerOrderRank = (provider: ServerProvider["provider"]): number => {
19 const rank = PROVIDER_CACHE_IDS.indexOf(provider);
20 return rank === -1 ? Number.MAX_SAFE_INTEGER : rank;
21};
22
23const mergeProviderModels = (
24 fallbackModels: ReadonlyArray<ServerProvider["models"][number]>,
25 cachedModels: ReadonlyArray<ServerProvider["models"][number]>,
26): ReadonlyArray<ServerProvider["models"][number]> => {
27 const fallbackSlugs = new Set(fallbackModels.map((model) => model.slug));
28 return [...fallbackModels, ...cachedModels.filter((model) => !fallbackSlugs.has(model.slug))];
29};
30
31export const orderProviderSnapshots = (
32 providers: ReadonlyArray<ServerProvider>,
33): ReadonlyArray<ServerProvider> =>
34 [...providers].toSorted(
35 (left, right) => providerOrderRank(left.provider) - providerOrderRank(right.provider),
36 );
37
38export const hydrateCachedProvider = (input: {
39 readonly cachedProvider: ServerProvider;
40 readonly fallbackProvider: ServerProvider;
41}): ServerProvider => {
42 if (
43 !input.fallbackProvider.enabled ||
44 input.cachedProvider.enabled !== input.fallbackProvider.enabled
45 ) {
46 return input.fallbackProvider;
47 }
48
49 const { message: _fallbackMessage, ...fallbackWithoutMessage } = input.fallbackProvider;
50 const hydratedProvider: ServerProvider = {
51 ...fallbackWithoutMessage,
52 models: mergeProviderModels(input.fallbackProvider.models, input.cachedProvider.models),
53 installed: input.cachedProvider.installed,
54 version: input.cachedProvider.version,
55 status: input.cachedProvider.status,
56 auth: input.cachedProvider.auth,
57 checkedAt: input.cachedProvider.checkedAt,
58 slashCommands: input.cachedProvider.slashCommands,
59 skills: input.cachedProvider.skills,
60 };
61
62 return input.cachedProvider.message
63 ? { ...hydratedProvider, message: input.cachedProvider.message }
64 : hydratedProvider;
65};
66
67export const resolveProviderStatusCachePath = (input: {
68 readonly cacheDir: string;
69 readonly provider: ServerProvider["provider"];
70}) => nodePath.join(input.cacheDir, `${input.provider}.json`);
71
72export const readProviderStatusCache = (filePath: string) =>
73 Effect.gen(function* () {
74 const fs = yield* FileSystem.FileSystem;
75 const exists = yield* fs.exists(filePath).pipe(Effect.orElseSucceed(() => false));
76 if (!exists) {
77 return undefined;
78 }
79
80 const raw = yield* fs.readFileString(filePath).pipe(Effect.orElseSucceed(() => ""));
81 const trimmed = raw.trim();
82 if (trimmed.length === 0) {
83 return undefined;
84 }
85
86 return yield* decodeProviderStatusCache(trimmed).pipe(
87 Effect.matchCauseEffect({
88 onFailure: (cause) =>
89 Effect.logWarning("failed to parse provider status cache, ignoring", {
90 path: filePath,
91 issues: Cause.pretty(cause),
92 }).pipe(Effect.as(undefined)),
93 onSuccess: Effect.succeed,
94 }),
95 );
96 });
97
98export const writeProviderStatusCache = (input: {
99 readonly filePath: string;
100 readonly provider: ServerProvider;
101}) =>
102 writeFileStringAtomically({
103 filePath: input.filePath,
104 contents: `${JSON.stringify(input.provider, null, 2)}\n`,
105 });