提交 9c0b03b9 authored 作者: yujiejie's avatar yujiejie

验收台2.0

上级 a0a14685
......@@ -1559,6 +1559,188 @@ function restoreMentorDeployReviewParams() {
renderContent();
}
// ===== 导师部署验收台:真实 API 测试(文本模型)=====
function getMentorDeployBaseUrl() {
var sid = Number(mentorState.deployReviewStudentId);
var p = getMentorDeployReviewParams(sid);
var sub = getMentorDeploySubmission(sid);
var a = (sub && sub.artifact) || {};
var url = String(p.apiUrl || '').trim().replace(/\/+$/, '');
if (!url) return '';
// API 地址未带端口且学生提交过端口时自动拼接;导师手动在地址中带端口则不重复拼接
if (!/:\d+$/.test(url)) {
var port = String(a.port || '').trim();
if (port) url += ':' + port;
}
return url;
}
function joinMentorDeployPath(base, path) {
var p = String(path || '').trim();
if (!p) return base;
return base + (p.charAt(0) === '/' ? p : '/' + p);
}
function getMentorDeployModelsUrl() {
return getMentorDeployBaseUrl() + '/v1/models';
}
function getMentorDeployChatUrl() {
var p = getMentorDeployReviewParams(Number(mentorState.deployReviewStudentId));
return joinMentorDeployPath(getMentorDeployBaseUrl(), p.apiPath || '/v1/chat/completions');
}
function getMentorDeployRequestHeaders() {
var p = getMentorDeployReviewParams(Number(mentorState.deployReviewStudentId));
var headers = { 'Content-Type': 'application/json' };
var key = String(p.apiKey || '').trim();
if (key) headers['Authorization'] = 'Bearer ' + key;
return headers;
}
function runMentorDeployModelsTest() {
var sid = Number(mentorState.deployReviewStudentId);
var p = getMentorDeployReviewParams(sid);
var base = getMentorDeployBaseUrl();
if (!base) {
showStudentFlowModal('缺少 API 地址', '请在验收参数中填写 API 地址后再测试。');
return;
}
if (!mentorDeployReviewState.tests[sid]) mentorDeployReviewState.tests[sid] = {};
var url = base + '/v1/models';
var startedAt = Date.now();
mentorDeployReviewState.tests[sid].models = { status: 'loading', url: url };
renderContent();
fetch(url, { method: 'GET', headers: getMentorDeployRequestHeaders() })
.then(function(res) {
return res.text().then(function(text) {
var latency = Date.now() - startedAt;
var data = null;
var raw = text;
try { data = JSON.parse(text); raw = JSON.stringify(data, null, 2); } catch (e) { data = null; }
var modelIds = [];
if (data && Array.isArray(data.data)) {
modelIds = data.data.map(function(m) { return m.id; }).filter(Boolean);
}
var httpOk = res.status >= 200 && res.status < 300;
var modelFound = !!p.modelName && modelIds.indexOf(p.modelName) >= 0;
var pass = httpOk && (!p.modelName || modelFound);
var err = '';
if (!httpOk) {
err = '接口返回非 2xx:HTTP ' + res.status + (data && data.error && data.error.message ? ' · ' + data.error.message : '');
} else if (p.modelName && !modelFound) {
err = 'HTTP 200,但模型列表未包含声明的模型名称「' + p.modelName + '」,请核对模型名称是否与部署服务一致。';
}
mentorDeployReviewState.tests[sid].models = {
status: pass ? 'pass' : 'fail',
httpStatus: res.status,
latency: latency,
modelFound: modelFound,
modelIds: modelIds,
raw: raw,
error: err,
testedAt: getMentorDeployNowTimestamp(),
url: url
};
if (currentPage === 'mentor-deploy-review') renderContent();
});
})
.catch(function(err) {
var latency = Date.now() - startedAt;
mentorDeployReviewState.tests[sid].models = {
status: 'fail',
httpStatus: 0,
latency: latency,
modelFound: false,
modelIds: [],
raw: null,
error: '网络请求失败:' + (err && err.message ? err.message : String(err)) + '。如果被浏览器 CORS 跨域拦截,请确认服务端已允许跨域(Access-Control-Allow-Origin)后再测试。',
testedAt: getMentorDeployNowTimestamp(),
url: url
};
if (currentPage === 'mentor-deploy-review') renderContent();
});
}
function runMentorDeployChatTest() {
var sid = Number(mentorState.deployReviewStudentId);
var p = getMentorDeployReviewParams(sid);
var base = getMentorDeployBaseUrl();
if (!base) {
showStudentFlowModal('缺少 API 地址', '请在验收参数中填写 API 地址后再测试。');
return;
}
if (!mentorDeployReviewState.tests[sid]) mentorDeployReviewState.tests[sid] = {};
var url = joinMentorDeployPath(base, p.apiPath || '/v1/chat/completions');
var startedAt = Date.now();
mentorDeployReviewState.tests[sid].chat = { status: 'loading', url: url };
renderContent();
var body = {
model: p.modelName || '',
messages: [{ role: 'user', content: p.prompt || '请用一句话介绍你自己。' }],
max_tokens: parseInt(p.maxTokens, 10) || 2048
};
var temp = parseFloat(p.temperature);
if (!isNaN(temp)) body.temperature = temp;
fetch(url, {
method: 'POST',
headers: getMentorDeployRequestHeaders(),
body: JSON.stringify(body)
})
.then(function(res) {
return res.text().then(function(text) {
var latency = Date.now() - startedAt;
var data = null;
var raw = text;
try { data = JSON.parse(text); raw = JSON.stringify(data, null, 2); } catch (e) { data = null; }
var content = '';
var returnedModel = '';
if (data) {
if (data.choices && data.choices[0] && data.choices[0].message && data.choices[0].message.content != null) {
content = data.choices[0].message.content;
}
returnedModel = data.model || '';
}
var httpOk = res.status >= 200 && res.status < 300;
var pass = httpOk && content != null && String(content).trim().length > 0;
var err = '';
if (!httpOk) {
err = '接口返回非 2xx:HTTP ' + res.status + (data && data.error && data.error.message ? ' · ' + data.error.message : '');
} else if (!pass) {
err = 'HTTP 200,但未解析到非空的 choices[0].message.content,请确认返回结构与 OpenAI 兼容格式一致。';
}
mentorDeployReviewState.tests[sid].chat = {
status: pass ? 'pass' : 'fail',
httpStatus: res.status,
latency: latency,
responseText: String(content),
returnedModel: returnedModel,
raw: raw,
error: err,
testedAt: getMentorDeployNowTimestamp(),
url: url
};
if (currentPage === 'mentor-deploy-review') renderContent();
});
})
.catch(function(err) {
var latency = Date.now() - startedAt;
mentorDeployReviewState.tests[sid].chat = {
status: 'fail',
httpStatus: 0,
latency: latency,
responseText: '',
returnedModel: '',
raw: null,
error: '网络请求失败:' + (err && err.message ? err.message : String(err)) + '。如果被浏览器 CORS 跨域拦截,请确认服务端已允许跨域后再测试。',
testedAt: getMentorDeployNowTimestamp(),
url: url
};
if (currentPage === 'mentor-deploy-review') renderContent();
});
}
function mentorDeployTestNotReady(name) {
showStudentFlowModal('测试功能待接入', '「' + name + '」的 API 测试将在下一步接入,当前为验收台静态布局预览。');
}
......@@ -1578,8 +1760,17 @@ function buildMentorDeployTestMeta(result) {
if (!result) {
return '<div class="flex items-center gap-3 text-xs text-[#86909C] mt-2"><span>状态码:—</span><span>耗时:—</span></div>';
}
var codeCls = result.httpStatus >= 200 && result.httpStatus < 300 ? 'text-emerald-600' : (result.httpStatus ? 'text-rose-500' : 'text-[#86909C]');
return '<div class="flex items-center gap-3 text-xs mt-2"><span class="text-[#86909C]">状态码:<b class="' + codeCls + '">' + (result.httpStatus || '—') + '</b></span><span class="text-[#86909C]">耗时:<b class="text-[#1D2129]">' + (result.latency != null ? result.latency + ' ms' : '—') + '</b></span></div>';
var codeText = result.httpStatus || '—';
var codeCls = 'text-[#86909C]';
if (result.httpStatus === 0) {
codeText = '网络错误';
codeCls = 'text-rose-500';
} else if (result.httpStatus >= 200 && result.httpStatus < 300) {
codeCls = 'text-emerald-600';
} else if (result.httpStatus) {
codeCls = 'text-rose-500';
}
return '<div class="flex items-center gap-3 text-xs mt-2"><span class="text-[#86909C]">状态码:<b class="' + codeCls + '">' + codeText + '</b></span><span class="text-[#86909C]">耗时:<b class="text-[#1D2129]">' + (result.latency != null ? result.latency + ' ms' : '—') + '</b></span></div>';
}
function buildMentorDeployTestError(result) {
......@@ -1620,30 +1811,38 @@ function buildMentorDeployTextModelItems(sid) {
var modelsResult = tests.models || null;
var chatResult = tests.chat || null;
var modelName = p.modelName;
var modelsLoading = !!(modelsResult && modelsResult.status === 'loading');
var chatLoading = !!(chatResult && chatResult.status === 'loading');
return '<div class="bg-white rounded-lg border border-[#E5E6EB] p-4 mb-4">' +
'<div class="flex items-center justify-between mb-3"><h3 class="font-semibold text-[#1D2129]">文本模型验收项</h3><span class="text-xs px-2 py-1 rounded bg-[#F2F3F5] text-[#86909C]">' + buildMentorDeployTestSummary(tests, ['models', 'chat']) + '</span></div>' +
'<div class="p-3 rounded-lg bg-[#F7F8FA] mb-3">' +
buildMentorDeployTestItemHeader('GET', '/v1/models', '模型列表', modelsResult) +
'<div class="mt-2 text-xs font-mono text-[#4E5969] bg-white border border-[#E5E6EB] rounded p-2 break-all">' + mentorDeployReviewEsc(getMentorDeployModelsUrl() || 'API 地址未填写') + '</div>' +
buildMentorDeployTestMeta(modelsResult) +
(modelsResult && modelsResult.modelIds && modelsResult.modelIds.length
? '<div class="mt-1.5 text-xs text-[#4E5969]">返回 ' + modelsResult.modelIds.length + ' 个模型:<span class="font-mono">' + mentorDeployReviewEsc(modelsResult.modelIds.slice(0, 8).join('、')) + (modelsResult.modelIds.length > 8 ? '…' : '') + '</span></div>'
: '') +
(modelsResult && modelsResult.modelFound != null
? '<div class="mt-1.5 text-xs ' + (modelsResult.modelFound ? 'text-emerald-600' : 'text-rose-500') + '">' + (modelsResult.modelFound ? '已包含声明的模型 ' + mentorDeployReviewEsc(modelName) : '未找到声明的模型 ' + mentorDeployReviewEsc(modelName)) + '</div>'
: '') +
buildMentorDeployTestError(modelsResult) +
(modelsResult && modelsResult.raw != null ? '<pre class="mt-2 bg-[#1D2129] text-emerald-300 rounded p-3 text-[10px] overflow-auto max-h-32 leading-relaxed">' + mentorDeployReviewEsc(modelsResult.raw) + '</pre>' : '') +
'<button class="btn-outline w-full mt-3 py-2 text-sm" onclick="mentorDeployTestNotReady(\'模型列表\')">测试</button>' +
'<button class="btn-outline w-full mt-3 py-2 text-sm" onclick="runMentorDeployModelsTest()"' + (modelsLoading ? ' disabled' : '') + '>' + (modelsLoading ? '测试中…' : '测试') + '</button>' +
'</div>' +
'<div class="p-3 rounded-lg bg-[#F7F8FA]">' +
buildMentorDeployTestItemHeader('POST', '/v1/chat/completions', '聊天补全', chatResult) +
'<div class="mt-2 text-xs font-mono text-[#4E5969] bg-white border border-[#E5E6EB] rounded p-2 break-all">' + mentorDeployReviewEsc(getMentorDeployChatUrl() || 'API 地址未填写') + '</div>' +
'<div class="mt-2"><label class="text-xs font-medium text-[#4E5969] block mb-1">测试提示词</label><textarea class="input-field text-sm min-h-[64px] resize-none" oninput="setMentorDeployReviewParam(\'prompt\', this.value)">' + mentorDeployReviewEsc(p.prompt) + '</textarea></div>' +
'<div class="grid grid-cols-2 gap-2 mt-2">' +
'<div><label class="text-xs font-medium text-[#4E5969] block mb-1">Max Tokens</label><input class="input-field text-sm" value="' + mentorDeployReviewEsc(p.maxTokens) + '" oninput="setMentorDeployReviewParam(\'maxTokens\', this.value)"></div>' +
'<div><label class="text-xs font-medium text-[#4E5969] block mb-1">Temperature</label><input class="input-field text-sm" value="' + mentorDeployReviewEsc(p.temperature) + '" oninput="setMentorDeployReviewParam(\'temperature\', this.value)"></div>' +
'</div>' +
buildMentorDeployTestMeta(chatResult) +
(chatResult && chatResult.returnedModel ? '<div class="mt-1.5 text-xs text-[#4E5969]">返回模型:<b class="font-mono">' + mentorDeployReviewEsc(chatResult.returnedModel) + '</b></div>' : '') +
(chatResult && chatResult.responseText ? '<div class="mt-2 p-2 rounded bg-white border border-[#E5E6EB] text-xs leading-relaxed whitespace-pre-wrap">' + mentorDeployReviewEsc(chatResult.responseText) + '</div>' : '') +
buildMentorDeployTestError(chatResult) +
(chatResult && chatResult.raw != null ? '<pre class="mt-2 bg-[#1D2129] text-emerald-300 rounded p-3 text-[10px] overflow-auto max-h-32 leading-relaxed">' + mentorDeployReviewEsc(chatResult.raw) + '</pre>' : '') +
'<button class="btn-primary w-full mt-3 py-2 text-sm" onclick="mentorDeployTestNotReady(\'聊天补全\')">测试</button>' +
'<button class="btn-primary w-full mt-3 py-2 text-sm" onclick="runMentorDeployChatTest()"' + (chatLoading ? ' disabled' : '') + '>' + (chatLoading ? '测试中…' : '测试') + '</button>' +
'</div>' +
'</div>';
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论