feat: connect console to catalog and project keys
This commit is contained in:
1 parent
5e8e462548
commit
ec51e8c802
13 files changed
+503
-22
No files matched your search
@@ -0,0 +1,162 @@
|
||||
export type CatalogModelType = 'text' | 'code' | 'multimodal' | 'image' | 'audio' | 'video' | 'embedding' | '3d'
|
||||
export type CatalogCallMode = 'sync' | 'stream' | 'async'
|
||||
|
||||
export interface CatalogProvider {
|
||||
id: string
|
||||
code: string
|
||||
name: string
|
||||
protocol: 'openai-compatible' | 'http' | 'sdk'
|
||||
status: 'active' | 'disabled'
|
||||
}
|
||||
|
||||
export interface CatalogModel {
|
||||
id: string
|
||||
providerId: string
|
||||
publicName: string
|
||||
upstreamModel: string
|
||||
type: CatalogModelType
|
||||
mode: CatalogCallMode
|
||||
description: string
|
||||
inputTypes: string[]
|
||||
outputTypes: string[]
|
||||
capabilities: Record<string, unknown>
|
||||
status: 'active' | 'disabled'
|
||||
}
|
||||
|
||||
export interface CatalogProject {
|
||||
id: string
|
||||
ownerUserId: string
|
||||
name: string
|
||||
description: string
|
||||
status: 'active' | 'disabled'
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
export interface CatalogApiKey {
|
||||
id: string
|
||||
scopeType: 'personal' | 'project'
|
||||
projectId?: string
|
||||
name: string
|
||||
keyPrefix: string
|
||||
scopes: string[]
|
||||
modelIds: string[]
|
||||
status: 'active' | 'disabled'
|
||||
createdAt: string
|
||||
lastUsedAt?: string
|
||||
revokedAt?: string
|
||||
}
|
||||
|
||||
export interface CreatedCatalogKey {
|
||||
key: string
|
||||
apiKey: CatalogApiKey
|
||||
}
|
||||
|
||||
export interface ChatCompletionResult {
|
||||
id: string
|
||||
model: string
|
||||
choices: Array<{ message: { role: string; content: string }; finish_reason: string }>
|
||||
usage?: { prompt_tokens: number; completion_tokens: number; total_tokens: number }
|
||||
}
|
||||
|
||||
export interface AsyncTaskResult {
|
||||
id: string
|
||||
model: string
|
||||
status: 'queued' | 'running' | 'succeeded'
|
||||
created_at: string
|
||||
updated_at: string
|
||||
result?: { format: string; url: string }
|
||||
}
|
||||
|
||||
export class CatalogApiError extends Error {
|
||||
constructor(readonly status: number, message: string, readonly code?: string) {
|
||||
super(message)
|
||||
}
|
||||
}
|
||||
|
||||
class HttpCatalogClient {
|
||||
constructor(private readonly baseUrl: string) {}
|
||||
|
||||
async listModels() {
|
||||
return this.request<{ providers: CatalogProvider[]; models: CatalogModel[] }>('/api/models')
|
||||
}
|
||||
|
||||
async listProjects() {
|
||||
return this.request<{ projects: CatalogProject[] }>('/api/projects')
|
||||
}
|
||||
|
||||
async createProject(input: { name: string; description?: string }) {
|
||||
return this.request<{ project: CatalogProject }>('/api/projects', { method: 'POST', body: JSON.stringify(input) })
|
||||
}
|
||||
|
||||
async listKeys() {
|
||||
return this.request<{ keys: CatalogApiKey[] }>('/api/keys')
|
||||
}
|
||||
|
||||
async createKey(input: { name: string; projectId?: string; scopes?: string[]; modelIds?: string[] }) {
|
||||
return this.request<CreatedCatalogKey>('/api/keys', { method: 'POST', body: JSON.stringify(input) })
|
||||
}
|
||||
|
||||
async revokeKey(id: string) {
|
||||
await this.request(`/api/keys/${encodeURIComponent(id)}/revoke`, { method: 'POST' })
|
||||
}
|
||||
|
||||
async chatCompletion(apiKey: string, input: { model: string; prompt: string }) {
|
||||
return this.request<ChatCompletionResult>('/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${apiKey}` },
|
||||
body: JSON.stringify({ model: input.model, messages: [{ role: 'user', content: input.prompt }] }),
|
||||
})
|
||||
}
|
||||
|
||||
async create3dTask(apiKey: string, input: { model: string; prompt: string }) {
|
||||
const payload = await this.request<{ task: AsyncTaskResult }>('/v1/3d/tasks', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${apiKey}` },
|
||||
body: JSON.stringify(input),
|
||||
})
|
||||
return payload.task
|
||||
}
|
||||
|
||||
async getTask(apiKey: string, taskId: string) {
|
||||
const payload = await this.request<{ task: AsyncTaskResult }>(`/v1/tasks/${encodeURIComponent(taskId)}`, {
|
||||
headers: { Authorization: `Bearer ${apiKey}` },
|
||||
})
|
||||
return payload.task
|
||||
}
|
||||
|
||||
async listAdminModels() {
|
||||
return this.request<{ models: CatalogModel[] }>('/api/admin/models')
|
||||
}
|
||||
|
||||
async listAdminProviders() {
|
||||
return this.request<{ providers: CatalogProvider[] }>('/api/admin/providers')
|
||||
}
|
||||
|
||||
async createAdminProvider(input: { code: string; name: string; protocol: CatalogProvider['protocol']; description?: string }) {
|
||||
return this.request<{ provider: CatalogProvider }>('/api/admin/providers', { method: 'POST', body: JSON.stringify(input) })
|
||||
}
|
||||
|
||||
async createAdminModel(input: { providerId: string; publicName: string; upstreamModel: string; type: CatalogModelType; mode: CatalogCallMode; description?: string }) {
|
||||
return this.request<{ model: CatalogModel }>('/api/admin/models', { method: 'POST', body: JSON.stringify(input) })
|
||||
}
|
||||
|
||||
private async request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
let response: Response
|
||||
try {
|
||||
response = await fetch(`${this.baseUrl}${path}`, {
|
||||
...init,
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json', ...init?.headers },
|
||||
})
|
||||
} catch {
|
||||
throw new CatalogApiError(0, '无法连接 Control Plane,请确认服务已启动。')
|
||||
}
|
||||
if (response.status === 204) return undefined as T
|
||||
const payload = await response.json().catch(() => ({})) as { error?: { code?: string; message?: string } }
|
||||
if (!response.ok) throw new CatalogApiError(response.status, payload.error?.message || '目录请求失败', payload.error?.code)
|
||||
return payload as T
|
||||
}
|
||||
}
|
||||
|
||||
const controlPlaneUrl = (import.meta.env.VITE_CONTROL_PLANE_URL || 'http://127.0.0.1:8787').replace(/\/$/, '')
|
||||
export const catalogClient = new HttpCatalogClient(controlPlaneUrl)
|
||||
Reference in new issue
Block a user