146 lines
5.0 KiB
TypeScript
146 lines
5.0 KiB
TypeScript
import { apiFetch } from "@/core/api";
|
|
import { getBackendBaseURL } from "@/core/config";
|
|
|
|
export interface ManagedModel {
|
|
name: string;
|
|
display_name: string | null;
|
|
description: string | null;
|
|
use: string;
|
|
model: string;
|
|
base_url: string | null;
|
|
request_timeout: number | null;
|
|
max_retries: number | null;
|
|
max_input_tokens: number | null;
|
|
max_tokens: number | null;
|
|
temperature: number | null;
|
|
stream_usage: boolean | null;
|
|
supports_thinking: boolean;
|
|
supports_reasoning_effort: boolean;
|
|
supports_vision: boolean;
|
|
when_thinking_enabled: Record<string, unknown> | null;
|
|
when_thinking_disabled: Record<string, unknown> | null;
|
|
thinking: Record<string, unknown> | null;
|
|
use_responses_api: boolean | null;
|
|
output_version: string | null;
|
|
extra_settings: Record<string, unknown>;
|
|
api_key_configured: boolean;
|
|
api_key_source: string | null;
|
|
source: "config.yaml";
|
|
}
|
|
|
|
export interface ManagedModelsResponse {
|
|
models: ManagedModel[];
|
|
config_path: string;
|
|
updated_at: string | null;
|
|
}
|
|
|
|
export interface ModelSavePayload {
|
|
name: string;
|
|
display_name?: string | null;
|
|
description?: string | null;
|
|
use: string;
|
|
model: string;
|
|
api_key?: string;
|
|
clear_api_key?: boolean;
|
|
base_url?: string | null;
|
|
request_timeout?: number | null;
|
|
max_retries?: number | null;
|
|
max_input_tokens?: number | null;
|
|
max_tokens?: number | null;
|
|
temperature?: number | null;
|
|
stream_usage?: boolean | null;
|
|
supports_thinking: boolean;
|
|
supports_vision: boolean;
|
|
when_thinking_enabled?: Record<string, unknown> | null;
|
|
when_thinking_disabled?: Record<string, unknown> | null;
|
|
thinking?: Record<string, unknown> | null;
|
|
use_responses_api?: boolean | null;
|
|
output_version?: string | null;
|
|
extra_settings: Record<string, unknown>;
|
|
}
|
|
|
|
export interface ModelConnectionTestResult {
|
|
success: boolean;
|
|
latency_ms: number;
|
|
message: string;
|
|
preview?: string | null;
|
|
}
|
|
|
|
async function readError(response: Response, fallback: string): Promise<never> {
|
|
const payload = await response.json().catch(() => null) as { detail?: string } | null;
|
|
throw new Error(payload?.detail || fallback);
|
|
}
|
|
|
|
export async function loadManagedModels(): Promise<ManagedModelsResponse> {
|
|
const response = await apiFetch(`${getBackendBaseURL()}/api/admin/models`);
|
|
if (!response.ok) return readError(response, "加载模型配置失败");
|
|
return response.json() as Promise<ManagedModelsResponse>;
|
|
}
|
|
|
|
export async function createManagedModel(payload: ModelSavePayload): Promise<ManagedModel> {
|
|
const response = await apiFetch(`${getBackendBaseURL()}/api/admin/models`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
if (!response.ok) return readError(response, "创建模型失败");
|
|
return response.json() as Promise<ManagedModel>;
|
|
}
|
|
|
|
export async function updateManagedModel(name: string, payload: ModelSavePayload): Promise<ManagedModel> {
|
|
const response = await apiFetch(`${getBackendBaseURL()}/api/admin/models/${encodeURIComponent(name)}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
if (!response.ok) return readError(response, "保存模型配置失败");
|
|
return response.json() as Promise<ManagedModel>;
|
|
}
|
|
|
|
export async function updateManagedModelReasoningEffortCapability(
|
|
name: string,
|
|
supportsReasoningEffort: boolean,
|
|
): Promise<ManagedModel> {
|
|
const response = await apiFetch(
|
|
`${getBackendBaseURL()}/api/admin/models/capabilities/reasoning-effort/${encodeURIComponent(name)}`,
|
|
{
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
supports_reasoning_effort: supportsReasoningEffort,
|
|
}),
|
|
},
|
|
);
|
|
if (!response.ok) {
|
|
return readError(response, "保存模型思考强度能力失败");
|
|
}
|
|
return response.json() as Promise<ManagedModel>;
|
|
}
|
|
|
|
export async function deleteManagedModel(name: string): Promise<void> {
|
|
const response = await apiFetch(`${getBackendBaseURL()}/api/admin/models/${encodeURIComponent(name)}`, {
|
|
method: "DELETE",
|
|
});
|
|
if (!response.ok) return readError(response, "删除模型失败");
|
|
}
|
|
|
|
export async function reorderManagedModels(names: string[]): Promise<ManagedModel[]> {
|
|
const response = await apiFetch(`${getBackendBaseURL()}/api/admin/models/order`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ names }),
|
|
});
|
|
if (!response.ok) return readError(response, "调整模型顺序失败");
|
|
return response.json() as Promise<ManagedModel[]>;
|
|
}
|
|
|
|
export async function testManagedModel(model: ModelSavePayload): Promise<ModelConnectionTestResult> {
|
|
const response = await apiFetch(`${getBackendBaseURL()}/api/admin/models/test`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ model }),
|
|
});
|
|
if (!response.ok) return readError(response, "模型连接测试失败");
|
|
return response.json() as Promise<ModelConnectionTestResult>;
|
|
}
|