-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathapi.ts
More file actions
132 lines (127 loc) · 5.32 KB
/
Copy pathapi.ts
File metadata and controls
132 lines (127 loc) · 5.32 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
import { fetch as tauriFetch } from '@tauri-apps/plugin-http'
import z, { ZodType } from 'zod'
import { ProviderConfig, ProviderModelConfig, ProviderType } from '../../../tauri'
export const defaultBaseURL = (type: ProviderType): string => {
return {
[ProviderType.Anthropic]: 'https://api.anthropic.com/v1',
[ProviderType.DeepSeek]: 'https://api.deepseek.com/v1',
[ProviderType.GitHubModels]: 'https://models.github.ai/inference',
[ProviderType.GoogleGemini]: 'https://generativelanguage.googleapis.com/v1beta',
[ProviderType.Groq]: 'https://api.groq.com/openai/v1',
[ProviderType.Mistral]: 'https://api.mistral.ai/v1',
[ProviderType.Ollama]: 'http://localhost:11434/v1',
[ProviderType.OpenAI]: 'https://api.openai.com/v1',
[ProviderType.OpenRouter]: 'https://openrouter.ai/api/v1',
[ProviderType.VercelAIGateway]: 'https://ai-gateway.vercel.sh/v1',
[ProviderType.xAI]: 'https://api.x.ai/v1',
[ProviderType.OpenAICompatible]: ''
}[type]
}
export const normalizeBaseURL = <T>(url: string, emptyFallback: T): string | T => {
const trimmed = url.trim()
if (trimmed === '') {
return emptyFallback
}
return trimmed.replace(/\/+$/, '')
}
export const fetchModels = async (config: ProviderConfig): Promise<ProviderModelConfig[]> => {
const fetchJSON = async <T extends ZodType>(url: string, schema: T, headers?: Record<string, string>) => {
const res = await tauriFetch(url, { method: 'GET', headers })
if (res.status !== 200) {
throw `StatusCode: ${res.status}\nFrom: ${url}`
}
const parsed = schema.safeParse(await res.json())
if (!parsed.success) {
const issue = parsed.error.issues[0]
const path = issue.path.length > 0 ? ` at '${issue.path.join('.')}'` : ''
throw `From: ${url}\nFailed to parse JSON${path}, ${issue.message}`
}
return parsed.data
}
let baseURL = normalizeBaseURL(config.baseURL, defaultBaseURL(config.type))
switch (config.type) {
case ProviderType.DeepSeek:
case ProviderType.Groq:
case ProviderType.xAI:
case ProviderType.OpenAI:
case ProviderType.VercelAIGateway:
case ProviderType.Ollama:
case ProviderType.OpenAICompatible: {
const schema = z.object({
data: z.array(
z
.object({
id: z.string()
})
.transform((val) => {
return { id: val.id, name: val.id }
})
)
})
const headers = { Authorization: `Bearer ${config.apiKey}` }
return fetchJSON(`${baseURL}/models`, schema, headers).then((json) => json.data)
}
case ProviderType.Mistral:
case ProviderType.OpenRouter: {
const schema = z.object({
data: z.array(
z.object({
id: z.string(),
name: z.string()
})
)
})
const headers = { Authorization: `Bearer ${config.apiKey}` }
return fetchJSON(`${baseURL}/models`, schema, headers).then((json) => json.data)
}
case ProviderType.GitHubModels: {
const schema = z.array(
z.object({
id: z.string(),
name: z.string()
})
)
const headers = { Authorization: `Bearer ${config.apiKey}` }
return fetchJSON(`https://models.github.ai/catalog/models`, schema, headers)
}
case ProviderType.Anthropic: {
const schema = z.object({
data: z.array(
z
.object({
id: z.string(),
display_name: z.string()
})
.transform((val) => {
return { id: val.id, name: val.display_name }
})
)
})
const headers = {
'x-api-key': config.apiKey,
'anthropic-version': '2023-06-01',
'anthropic-dangerous-direct-browser-access': 'true',
'dangerouslyAllowBrowser': 'true'
}
return fetchJSON(`${baseURL}/models`, schema, headers).then((json) => json.data)
}
case ProviderType.GoogleGemini: {
const schema = z.object({
models: z.array(
z
// TODO: The documentation mentions a baseModelId field, but it doesn't actually exist
// https://ai.google.dev/api/models?hl=zh-cn#Model
.object({
name: z.string(),
displayName: z.string()
})
.transform((val) => {
return { id: val.name, name: val.displayName }
})
)
})
const headers = { 'x-goog-api-key': config.apiKey }
return fetchJSON(`${baseURL}/models?pageSize=1000`, schema, headers).then((json) => json.models)
}
}
}