feat: 实现基础设施层
axios 请求封装及七个业务模块 API,Pinia 状态管理(auth/system/models/tools),Mock 适配器与数据,以及流式对话、轮询、倒计时组合式函数。
This commit is contained in:
268
frontend/src/mock/adapter.ts
Normal file
268
frontend/src/mock/adapter.ts
Normal file
@@ -0,0 +1,268 @@
|
||||
/**
|
||||
* Axios Mock Adapter
|
||||
* 拦截所有 API 请求并返回 mock 数据
|
||||
* 通过 URL + method 路由到对应的 mock 响应
|
||||
*/
|
||||
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
|
||||
import {
|
||||
mockLoginOk,
|
||||
mockHealth,
|
||||
mockSystemInfo,
|
||||
mockModels,
|
||||
mockTrainedModels,
|
||||
mockLocalModels,
|
||||
mockDatasets,
|
||||
mockFineTuneList,
|
||||
mockCompareList,
|
||||
mockEvalList,
|
||||
mockDimensions,
|
||||
mockLogFiles,
|
||||
mockTrainingLogFiles,
|
||||
mockLogContent,
|
||||
} from './data'
|
||||
|
||||
/** 模拟网络延迟 */
|
||||
function delay(ms = 200): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
/** 构造 Axios 风格的成功响应 */
|
||||
function ok(data: any, config: AxiosRequestConfig = {}) {
|
||||
return {
|
||||
data: { code: 0, message: 'ok', data },
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
headers: {},
|
||||
config,
|
||||
}
|
||||
}
|
||||
|
||||
/** 构造失败响应 */
|
||||
function fail(message: string, code = 500, config: AxiosRequestConfig = {}) {
|
||||
return {
|
||||
data: { code, message },
|
||||
status: code,
|
||||
statusText: message,
|
||||
headers: {},
|
||||
config,
|
||||
}
|
||||
}
|
||||
|
||||
/** 简易 URL 解析(去掉 baseURL 前缀) */
|
||||
function stripBaseURL(url: string): string {
|
||||
// url 可能带查询参数
|
||||
const [path] = url.split('?')
|
||||
return path
|
||||
}
|
||||
|
||||
/** 通过路径 + method 匹配 mock 响应 */
|
||||
async function handleMock(config: AxiosRequestConfig) {
|
||||
await delay(150) // 模拟网络延迟
|
||||
|
||||
const url = stripBaseURL(config.url || '')
|
||||
const method = (config.method || 'get').toLowerCase()
|
||||
const params = config.params || {}
|
||||
const body = typeof config.data === 'string' ? safeJSON(config.data) : config.data || {}
|
||||
|
||||
// ==================== 认证 ====================
|
||||
if (url === '/login' && method === 'post') {
|
||||
if (body.username === 'admin' && body.password === 'admin') {
|
||||
return ok(mockLoginOk.data)
|
||||
}
|
||||
return fail('账号或密码错误', 401)
|
||||
}
|
||||
if (url === '/web-log' && method === 'post') {
|
||||
return ok({ received: true })
|
||||
}
|
||||
|
||||
// ==================== 系统监控 ====================
|
||||
if (url === '/health' && method === 'get') return ok(mockHealth)
|
||||
if (url === '/system-info' && method === 'get') return ok(mockSystemInfo)
|
||||
|
||||
// ==================== 模型管理 ====================
|
||||
if (url === '/model-manage' && method === 'get') return ok(mockModels)
|
||||
if (url === '/model-manage/local-models' && method === 'get') return ok(mockLocalModels)
|
||||
if (url === '/model-manage/trained-models' && method === 'get') return ok(mockTrainedModels)
|
||||
if (url === '/model-manage/merge' && method === 'post') {
|
||||
return ok({ merged: true, path: '/data/saves/' + body.model_name + '-merged' })
|
||||
}
|
||||
// 模型详情 / 编辑 / 删除 / 用途更新
|
||||
let m = url.match(/^\/model-manage\/([^/]+)$/)
|
||||
if (m && method === 'get') {
|
||||
const id = m[1]
|
||||
const found = mockModels.find((x) => String(x.id) === id || x.name === id)
|
||||
return found ? ok(found) : fail('模型不存在', 404)
|
||||
}
|
||||
m = url.match(/^\/model-manage\/([^/]+)$/)
|
||||
if (m && (method === 'put' || method === 'delete')) {
|
||||
return ok({ id: m[1], ...body })
|
||||
}
|
||||
m = url.match(/^\/model-manage\/name\/([^/]+)$/)
|
||||
if (m && method === 'get') {
|
||||
const name = decodeURIComponent(m[1])
|
||||
const found = mockModels.find((x) => x.name === name)
|
||||
return found ? ok(found) : fail('模型不存在', 404)
|
||||
}
|
||||
m = url.match(/^\/model-manage\/trained-models\/([^/]+)$/)
|
||||
if (m && method === 'delete') {
|
||||
return ok({ deleted: m[1] })
|
||||
}
|
||||
m = url.match(/^\/model-manage\/([^/]+)\/purpose$/)
|
||||
if (m && method === 'put') {
|
||||
return ok({ id: m[1], purpose: body.purpose })
|
||||
}
|
||||
|
||||
// ==================== 数据集 ====================
|
||||
if (url === '/dataset-manage' && method === 'get') return ok(mockDatasets)
|
||||
if (url === '/dataset-manage' && method === 'post') {
|
||||
const newId = Math.max(...mockDatasets.map((d) => Number(d.id))) + 1
|
||||
return ok({ id: newId })
|
||||
}
|
||||
m = url.match(/^\/dataset-manage\/([^/]+)$/)
|
||||
if (m && method === 'get') {
|
||||
const found = mockDatasets.find((x) => String(x.id) === m[1])
|
||||
return found ? ok(found) : fail('数据集不存在', 404)
|
||||
}
|
||||
if (m && (method === 'put' || method === 'delete')) {
|
||||
return ok({ id: m[1] })
|
||||
}
|
||||
m = url.match(/^\/dataset-manage\/upload\/([^/]+)$/)
|
||||
if (m && method === 'post') return ok({ uploaded: true })
|
||||
m = url.match(/^\/dataset-manage\/preview\/([^/]+)$/)
|
||||
if (m && method === 'get') return ok(mockLogContent)
|
||||
|
||||
// ==================== 训练任务 ====================
|
||||
if (url === '/fine-tune' && method === 'get') return ok(mockFineTuneList)
|
||||
if (url === '/fine-tune' && method === 'post') {
|
||||
return ok({ id: Math.floor(Math.random() * 10000) + 100 })
|
||||
}
|
||||
if (url === '/fine-tune/start' && method === 'post') {
|
||||
return ok({ started: true })
|
||||
}
|
||||
if (url === '/fine-tune/check-name' && method === 'get') {
|
||||
return ok({ exists: false })
|
||||
}
|
||||
m = url.match(/^\/fine-tune\/progress\/([^/]+)$/)
|
||||
if (m && method === 'get') {
|
||||
const task = mockFineTuneList.find((t) => String(t.id) === m[1])
|
||||
if (!task) return fail('任务不存在', 404)
|
||||
if (task.status === 'running') {
|
||||
return ok({
|
||||
status: task.status,
|
||||
progress: task.progress ?? 0,
|
||||
step: `${Math.floor((task.progress ?? 0) * 10)}/1000`,
|
||||
speed: '1.23s/it',
|
||||
eta: '00:23:45',
|
||||
})
|
||||
}
|
||||
return ok({ status: task.status, progress: task.progress ?? 0 })
|
||||
}
|
||||
m = url.match(/^\/fine-tune\/([^/]+)$/)
|
||||
if (m && method === 'get') {
|
||||
const found = mockFineTuneList.find((x) => String(x.id) === m[1])
|
||||
return found ? ok(found) : fail('任务不存在', 404)
|
||||
}
|
||||
m = url.match(/^\/fine-tune\/stop\/([^/]+)$/)
|
||||
if (m && method === 'post') return ok({ stopped: true })
|
||||
m = url.match(/^\/fine-tune\/([^/]+)$/)
|
||||
if (m && (method === 'put' || method === 'delete')) {
|
||||
return ok({ id: m[1] })
|
||||
}
|
||||
if (url === '/fine-tune/tensorboard/start' && method === 'post') {
|
||||
return ok({ url: 'http://10.10.10.77:6006' })
|
||||
}
|
||||
|
||||
// ==================== 模型推理/对比 ====================
|
||||
if (url === '/model-compare' && method === 'get') return ok(mockCompareList)
|
||||
if (url === '/model-compare' && method === 'post') {
|
||||
return ok({ id: Math.floor(Math.random() * 10000) + 100 })
|
||||
}
|
||||
m = url.match(/^\/model-compare\/([^/]+)$/)
|
||||
if (m && method === 'get') {
|
||||
const found = mockCompareList.find((x) => String(x.id) === m[1])
|
||||
return found ? ok(found) : fail('任务不存在', 404)
|
||||
}
|
||||
if (m && method === 'delete') return ok({ deleted: m[1] })
|
||||
m = url.match(/^\/model-compare\/([^/]+)\/load-status$/)
|
||||
if (m && method === 'get') {
|
||||
return ok({ all_ready: true, loaded_models: [] })
|
||||
}
|
||||
if (m && method === 'post') return ok({ updated: true })
|
||||
m = url.match(/^\/model-compare\/([^/]+)\/load$/)
|
||||
if (m && method === 'post') return ok({ loading: true })
|
||||
m = url.match(/^\/model-compare\/([^/]+)\/unload$/)
|
||||
if (m && method === 'post') return ok({ stopped: true })
|
||||
m = url.match(/^\/model-compare\/([^/]+)\/start-model$/)
|
||||
if (m && method === 'post') {
|
||||
return ok({ pid: 12345 + Math.floor(Math.random() * 100), port: 18000 + Math.floor(Math.random() * 1000) })
|
||||
}
|
||||
if (url === '/model-compare/all/stop-all' && method === 'post') return ok({ stopped: true })
|
||||
if (url === '/model-compare/stop-by-pid' && method === 'post') return ok({ stopped: true })
|
||||
if (url === '/model-compare/chat-with-port' && method === 'post') {
|
||||
// 模拟推理回答(用于对比结果页)
|
||||
const userQ = body?.messages?.find((m: any) => m.role === 'user')?.content || '你的问题'
|
||||
const answer = `这是一个针对「${userQ.slice(0, 30)}」的模拟回答。\n\n在真实环境中,对比结果页会通过端口代理调用对应模型服务,返回该模型的真实推理结果。\n\n模型参数量、温度、Top-p 等参数都会影响输出内容。\n\n- 模型:${body?.model_name || '未知'}\n- 端口:${body?.port || '-'}\n- 参数:temperature=${body?.temperature ?? 0.7}, max_tokens=${body?.max_tokens ?? 2048}`
|
||||
return ok({ response: answer, content: answer })
|
||||
}
|
||||
if (url === '/model-compare/stream-chat' && method === 'post') {
|
||||
// 模拟流式对话(前端 useStreamChat 会按块读取)
|
||||
const userQ = body?.user_question || ''
|
||||
const sysPrompt = body?.system_prompt || ''
|
||||
const answer = `${sysPrompt ? '【系统提示】' + sysPrompt.slice(0, 50) + '\n\n' : ''}关于「${userQ}」的回答:\n\n这是一段**模拟流式输出**。在真实部署中,后端会通过 SSE/WebSocket 逐字推送 token。\n\n## 模型参数\n- 温度(temperature):${body?.temperature ?? 0.7}\n- 最大长度(max_tokens):${body?.max_tokens ?? 2048}\n\n## 思考过程\n让我先分析这个问题...\n- 识别用户意图\n- 检索相关知识\n- 生成回答\n\n回答已生成。以上为前端 Mock 演示内容。`
|
||||
return ok({ response: answer })
|
||||
}
|
||||
if (url === '/model-compare/test-stream' && method === 'post') {
|
||||
return ok({ response: '测试流式输出' })
|
||||
}
|
||||
if (url === '/model-chat/batch' && method === 'post') return ok({ responses: [] })
|
||||
if (url === '/model-chat/local/chat' && method === 'post') return ok({ response: '本地模型回复' })
|
||||
if (url === '/model-chat/local/preload' && method === 'post') return ok({ loaded: true })
|
||||
if (url === '/model-chat/trained/preload' && method === 'post') return ok({ loaded: true })
|
||||
|
||||
// ==================== 模型评测 ====================
|
||||
if (url === '/model-eval' && method === 'get') return ok(mockEvalList)
|
||||
if (url === '/model-eval' && method === 'delete') return ok({ deleted: true })
|
||||
if (url === '/model-eval/start' && method === 'post') return ok({ task_id: Math.floor(Math.random() * 1000) + 1 })
|
||||
if (url === '/dimension' && method === 'get') return ok(mockDimensions)
|
||||
if (url === '/dimension' && method === 'post') {
|
||||
return ok({ id: Math.floor(Math.random() * 1000) + 100 })
|
||||
}
|
||||
m = url.match(/^\/dimension\/([^/]+)$/)
|
||||
if (m && method === 'get') {
|
||||
const found = mockDimensions.find((x) => String(x.id) === m[1])
|
||||
return found ? ok(found) : fail('维度不存在', 404)
|
||||
}
|
||||
if (m && (method === 'put' || method === 'delete')) {
|
||||
return ok({ id: m[1] })
|
||||
}
|
||||
|
||||
// ==================== 日志 ====================
|
||||
if (url === '/log-files' && method === 'get') return ok(mockLogFiles)
|
||||
if (url === '/log-content' && method === 'get') return ok(mockLogContent)
|
||||
if (url === '/training-log-files' && method === 'get') return ok(mockTrainingLogFiles)
|
||||
if (url === '/training-log-content' && method === 'get') return ok(mockLogContent)
|
||||
|
||||
// 未匹配的请求 → 兜底返回空成功(避免阻断 UI)
|
||||
console.warn('[Mock] 未匹配路由:', method.toUpperCase(), url, params)
|
||||
return ok({ mocked: true, url, method, params, body })
|
||||
}
|
||||
|
||||
function safeJSON(str: string) {
|
||||
try {
|
||||
return JSON.parse(str)
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
/** 给 axios instance 安装 mock adapter */
|
||||
export function installMockAdapter(instance: AxiosInstance) {
|
||||
instance.defaults.adapter = async (config: AxiosRequestConfig) => {
|
||||
try {
|
||||
const response = await handleMock(config)
|
||||
return response
|
||||
} catch (e: any) {
|
||||
return fail(e.message || 'Mock 错误', 500, config)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user