-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathopenWebUi.ts
More file actions
53 lines (46 loc) 路 1.79 KB
/
Copy pathopenWebUi.ts
File metadata and controls
53 lines (46 loc) 路 1.79 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
import type { Ai, GetTokenResult } from "core/ports/Ai";
import { oidcTokenExchange, OidcTokenExchangeError } from "core/tools/oidcTokenExchange";
import { z } from "zod";
export function createAi(params: {
id: string;
name: string;
webUiUrl: string;
oauthProvider: string;
getOidcAccessToken: () => Promise<string>;
}): Ai {
const { id, name, webUiUrl, oauthProvider, getOidcAccessToken } = params;
const apiBase = `${webUiUrl}/api`;
return {
id,
name,
provider: "openai",
webUiUrl,
apiBase,
getToken: async (): Promise<GetTokenResult> => {
const oidcAccessToken = await getOidcAccessToken();
return oidcTokenExchange({
tokenExchangeEndpoint: `${webUiUrl}/api/v1/auths/oauth/${oauthProvider}/token/exchange`,
oidcAccessToken
})
.then(token => ({ status: "success" as const, token }))
.catch((error: unknown) => {
if (error instanceof OidcTokenExchangeError && error.status === 403) {
return { status: "no-account" as const };
}
return { status: "error" as const };
});
},
listModels: async (token: string) => {
const response = await fetch(`${apiBase}/models`, {
headers: { Authorization: `Bearer ${token}` }
});
if (!response.ok) {
throw new Error(`Failed to list models (${response.status})`);
}
const { data } = z
.object({ data: z.array(z.object({ id: z.string(), name: z.string() })) })
.parse(await response.json());
return data.map(({ id, name }) => ({ id, name }));
}
};
}