Repository navigation
Expand file tree
/
Copy pathmodelCatalog.ts
More file actions
150 lines (147 loc) · 3.57 KB
/
Copy pathmodelCatalog.ts
File metadata and controls
150 lines (147 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { useEffect } from "react";
import { QueryClient, useQuery } from "@tanstack/react-query";
import { request } from "./api";
import type { Provider } from "./types";
export type Model = { id: string; name: string; efforts: string[] };
// Credentials remain in closures; keys and the memory-only cache contain no tokens.
let credential = "";
const client = new QueryClient({
defaultOptions: {
queries: {
retry: false,
staleTime: 60_000,
gcTime: 300_000,
refetchOnWindowFocus: false,
},
},
});
export function clearModelCatalog() {
client.clear();
credential = "";
}
function entryFor(
token: string,
provider: Provider,
environment: string,
account?: string | null,
generation?: number,
) {
if (credential !== token) {
clearModelCatalog();
credential = token;
}
return [
"models",
provider,
environment,
account ?? null,
generation ?? 0,
] as const;
}
function options(
token: string,
provider: Provider,
environment: string,
account?: string | null,
generation?: number,
) {
return {
queryKey: entryFor(token, provider, environment, account, generation),
queryFn: async () => {
const value = await request<{ models: Model[] }>(
token,
`/api/models/${provider}?environment_id=${encodeURIComponent(environment)}${account ? `&account_id=${encodeURIComponent(account)}` : ""}`,
);
if (!Array.isArray(value.models))
throw new Error("Invalid model catalog");
return value.models;
},
};
}
function load(
token: string,
provider: Provider,
environment: string,
account?: string | null,
generation?: number,
) {
return client.fetchQuery(
options(token, provider, environment, account, generation),
);
}
/** Warm only configured connections; keep results in memory and coalesce with menus. */
export async function prewarmModels(
token: string,
connections: {
provider: Provider;
environment: string;
account?: string | null;
generation?: number;
}[],
) {
const unique = [
...new Map(
connections.map((c) => [
JSON.stringify([
c.provider,
c.environment,
c.account ?? null,
c.generation ?? 0,
]),
c,
]),
).values(),
];
if (unique.length)
entryFor(
token,
unique[0].provider,
unique[0].environment,
unique[0].account,
unique[0].generation,
);
let next = 0;
await Promise.all(
Array.from({ length: Math.min(2, unique.length) }, async () => {
while (next < unique.length && credential === token) {
const connection = unique[next++];
try {
await load(
token,
connection.provider,
connection.environment,
connection.account,
connection.generation,
);
} catch {
/* Retry on next visit or refresh. */
}
}
}),
);
}
export function useModelCatalog(
token: string,
provider: Provider,
environment: string,
enabled: boolean,
refreshKey?: unknown,
account?: string | null,
generation?: number,
) {
const query = useQuery(
{ ...options(token, provider, environment, account, generation), enabled },
client,
);
useEffect(() => {
if (enabled)
void load(token, provider, environment, account, generation).catch(
() => {},
);
}, [token, provider, environment, enabled, refreshKey, account, generation]);
return {
models: enabled ? (query.data ?? []) : [],
loading: enabled && query.isPending,
failed: enabled && query.isError,
};
}