415 lines
12 KiB
HTML
415 lines
12 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Dify 中转服务 — 测试面板</title>
|
|
<style>
|
|
:root {
|
|
--bg: #0f1117;
|
|
--surface: #1a1d27;
|
|
--border: #2a2d3a;
|
|
--primary: #6366f1;
|
|
--primary-hover: #4f46e5;
|
|
--accent: #22d3ee;
|
|
--text: #e2e8f0;
|
|
--muted: #64748b;
|
|
--success: #22c55e;
|
|
--error: #ef4444;
|
|
--warning: #f59e0b;
|
|
}
|
|
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
|
|
body {
|
|
font-family: 'Inter', 'PingFang SC', 'Microsoft YaHei', sans-serif;
|
|
background: var(--bg);
|
|
color: var(--text);
|
|
min-height: 100vh;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 24px;
|
|
}
|
|
|
|
.container {
|
|
width: 100%;
|
|
max-width: 720px;
|
|
}
|
|
|
|
.card {
|
|
background: var(--surface);
|
|
border: 1px solid var(--border);
|
|
border-radius: 16px;
|
|
padding: 32px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.header {
|
|
text-align: center;
|
|
margin-bottom: 32px;
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 1.5rem;
|
|
font-weight: 700;
|
|
background: linear-gradient(135deg, var(--primary), var(--accent));
|
|
-webkit-background-clip: text;
|
|
-webkit-text-fill-color: transparent;
|
|
margin-bottom: 6px;
|
|
}
|
|
|
|
.header p {
|
|
font-size: 0.85rem;
|
|
color: var(--muted);
|
|
}
|
|
|
|
.field {
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.field label {
|
|
display: block;
|
|
font-size: 0.8rem;
|
|
font-weight: 600;
|
|
color: var(--muted);
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.05em;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
.field input, .field textarea {
|
|
width: 100%;
|
|
background: #111827;
|
|
border: 1px solid var(--border);
|
|
border-radius: 10px;
|
|
color: var(--text);
|
|
font-size: 0.9rem;
|
|
padding: 10px 14px;
|
|
transition: border-color 0.2s;
|
|
outline: none;
|
|
}
|
|
|
|
.field input:focus, .field textarea:focus {
|
|
border-color: var(--primary);
|
|
}
|
|
|
|
.field textarea {
|
|
resize: vertical;
|
|
min-height: 120px;
|
|
font-family: 'Fira Code', 'Consolas', monospace;
|
|
}
|
|
|
|
.field small {
|
|
display: block;
|
|
color: var(--muted);
|
|
font-size: 0.75rem;
|
|
margin-top: 6px;
|
|
}
|
|
|
|
.row {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr;
|
|
gap: 16px;
|
|
}
|
|
|
|
.btn-row {
|
|
display: flex;
|
|
gap: 12px;
|
|
margin-top: 24px;
|
|
}
|
|
|
|
.btn {
|
|
flex: 1;
|
|
padding: 11px 20px;
|
|
border: none;
|
|
border-radius: 10px;
|
|
font-size: 0.9rem;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.btn-primary {
|
|
background: var(--primary);
|
|
color: #fff;
|
|
}
|
|
|
|
.btn-primary:hover { background: var(--primary-hover); }
|
|
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
|
|
|
|
.btn-ghost {
|
|
background: transparent;
|
|
border: 1px solid var(--border);
|
|
color: var(--muted);
|
|
}
|
|
|
|
.btn-ghost:hover { border-color: var(--primary); color: var(--text); }
|
|
|
|
/* Status badge */
|
|
.status-bar {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
padding: 12px 16px;
|
|
background: #111827;
|
|
border: 1px solid var(--border);
|
|
border-radius: 10px;
|
|
margin-bottom: 20px;
|
|
font-size: 0.85rem;
|
|
}
|
|
|
|
.dot {
|
|
width: 8px;
|
|
height: 8px;
|
|
border-radius: 50%;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.dot.ok { background: var(--success); box-shadow: 0 0 6px var(--success); }
|
|
.dot.err { background: var(--error); box-shadow: 0 0 6px var(--error); }
|
|
.dot.idle { background: var(--muted); }
|
|
|
|
/* Output */
|
|
#output {
|
|
background: #0a0d14;
|
|
border: 1px solid var(--border);
|
|
border-radius: 12px;
|
|
padding: 16px;
|
|
font-family: 'Fira Code', 'Consolas', monospace;
|
|
font-size: 0.8rem;
|
|
line-height: 1.7;
|
|
color: #a5b4fc;
|
|
white-space: pre-wrap;
|
|
word-break: break-all;
|
|
max-height: 400px;
|
|
overflow-y: auto;
|
|
min-height: 80px;
|
|
}
|
|
|
|
#output::-webkit-scrollbar { width: 6px; }
|
|
#output::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
|
|
|
|
.error-text { color: #fca5a5; }
|
|
.success-text { color: #86efac; }
|
|
.warn-text { color: #fcd34d; }
|
|
|
|
/* Model select */
|
|
#model { margin-bottom: 20px; }
|
|
|
|
/* Streaming indicator */
|
|
.streaming-indicator {
|
|
display: none;
|
|
align-items: center;
|
|
gap: 8px;
|
|
color: var(--accent);
|
|
font-size: 0.85rem;
|
|
margin-bottom: 12px;
|
|
}
|
|
|
|
.spinner {
|
|
width: 14px;
|
|
height: 14px;
|
|
border: 2px solid var(--border);
|
|
border-top-color: var(--accent);
|
|
border-radius: 50%;
|
|
animation: spin 0.7s linear infinite;
|
|
}
|
|
|
|
@keyframes spin { to { transform: rotate(360deg); } }
|
|
|
|
.section-title {
|
|
font-size: 0.75rem;
|
|
font-weight: 700;
|
|
color: var(--muted);
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.08em;
|
|
margin-bottom: 12px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
|
|
<div class="card">
|
|
<div class="header">
|
|
<h1>Dify 中转服务 — 测试面板</h1>
|
|
<p>快速验证 digital-human-relay 与 Dify 的连通性</p>
|
|
</div>
|
|
|
|
<!-- Status -->
|
|
<div class="status-bar">
|
|
<div class="dot idle" id="statusDot"></div>
|
|
<span id="statusText">尚未测试</span>
|
|
</div>
|
|
|
|
<div class="section-title">中转服务配置</div>
|
|
|
|
<div class="field">
|
|
<label>中转服务地址</label>
|
|
<input type="text" id="relayUrl" placeholder="http://你的服务器IP:8000" />
|
|
<small>填写 digital-human 中转服务的根地址,末尾不要加斜杠</small>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="field">
|
|
<label>Dify API 密钥</label>
|
|
<input type="password" id="apiKey" placeholder="app-xxxxxxxx" />
|
|
</div>
|
|
<div class="field">
|
|
<label>模型名称</label>
|
|
<input type="text" id="modelName" placeholder="dify" />
|
|
</div>
|
|
</div>
|
|
|
|
<div class="btn-row">
|
|
<button class="btn btn-ghost" onclick="testHealth()">健康检查</button>
|
|
<button class="btn btn-primary" id="sendBtn" onclick="sendMessage()">发送测试请求</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="section-title">响应输出</div>
|
|
<div class="streaming-indicator" id="streamingIndicator">
|
|
<div class="spinner"></div>
|
|
<span>AI 正在生成回复…</span>
|
|
</div>
|
|
<div id="output">// 响应结果将在此显示</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<script>
|
|
// ── State ──────────────────────────────────────────────────
|
|
let abortController = null;
|
|
|
|
// ── Helpers ────────────────────────────────────────────────
|
|
const relayUrl = () => document.getElementById('relayUrl').value.replace(/\/$/, '');
|
|
const apiKey = () => document.getElementById('apiKey').value.trim();
|
|
const model = () => document.getElementById('modelName').value.trim() || 'dify';
|
|
|
|
function setStatus(type, text) {
|
|
const dot = document.getElementById('statusDot');
|
|
const span = document.getElementById('statusText');
|
|
dot.className = `dot ${type}`;
|
|
span.textContent = text;
|
|
}
|
|
|
|
function append(text, cls = '') {
|
|
const el = document.getElementById('output');
|
|
if (cls) {
|
|
const span = document.createElement('span');
|
|
span.className = cls;
|
|
span.textContent = text;
|
|
el.appendChild(span);
|
|
} else {
|
|
el.appendChild(document.createTextNode(text));
|
|
}
|
|
el.scrollTop = el.scrollHeight;
|
|
}
|
|
|
|
function clearOutput() {
|
|
document.getElementById('output').innerHTML = '';
|
|
}
|
|
|
|
function setLoading(loading) {
|
|
const btn = document.getElementById('sendBtn');
|
|
btn.disabled = loading;
|
|
document.getElementById('streamingIndicator').style.display = loading ? 'flex' : 'none';
|
|
}
|
|
|
|
// ── Health Check ───────────────────────────────────────────
|
|
async function testHealth() {
|
|
const url = relayUrl();
|
|
if (!url) { alert('请先填写"中转服务地址"'); return; }
|
|
clearOutput();
|
|
setStatus('idle', '检测中…');
|
|
try {
|
|
const res = await fetch(`${url}/health`);
|
|
const data = await res.json().catch(() => ({}));
|
|
clearOutput();
|
|
append(`HTTP 状态码: ${res.status}\n`, res.ok ? 'success-text' : 'error-text');
|
|
append(JSON.stringify(data, null, 2), res.ok ? 'success-text' : 'error-text');
|
|
setStatus(res.ok ? 'ok' : 'err', res.ok ? `服务正常 — ${data.status || 'ok'}` : `HTTP ${res.status}`);
|
|
} catch (e) {
|
|
clearOutput();
|
|
append(`连接失败:${e.message}`, 'error-text');
|
|
setStatus('err', '连接失败');
|
|
}
|
|
}
|
|
|
|
// ── Send Message ───────────────────────────────────────────
|
|
async function sendMessage() {
|
|
const url = relayUrl();
|
|
const key = apiKey();
|
|
if (!url) { alert('请填写"中转服务地址"'); return; }
|
|
if (!key) { alert('请填写"Dify API 密钥"'); return; }
|
|
|
|
// abort previous
|
|
if (abortController) abortController.abort();
|
|
abortController = new AbortController();
|
|
|
|
clearOutput();
|
|
setLoading(true);
|
|
setStatus('idle', '请求中…');
|
|
|
|
const payload = {
|
|
model: model(),
|
|
stream: false,
|
|
messages: [
|
|
{ role: 'user', content: '你好,请用一句话介绍自己。' }
|
|
]
|
|
};
|
|
|
|
append(`POST ${url}/v1/chat/completions\n`);
|
|
append(`Authorization: Bearer ${key.substring(0, 8)}…\n\n`);
|
|
append(`Request:\n${JSON.stringify(payload, null, 2)}\n\n`);
|
|
append(`Response:\n`);
|
|
|
|
try {
|
|
// Direct to Dify (bypass relay — useful if relay itself has issues)
|
|
const endpoint = `${url}/v1/chat/completions`;
|
|
const res = await fetch(endpoint, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${key}`,
|
|
},
|
|
body: JSON.stringify(payload),
|
|
signal: abortController.signal,
|
|
});
|
|
|
|
const raw = await res.text();
|
|
let data;
|
|
try { data = JSON.parse(raw); } catch { data = raw; }
|
|
|
|
append(`[${res.status}] `, res.ok ? 'success-text' : 'error-text');
|
|
append(`${res.ok ? 'OK' : 'ERROR'}\n\n`, res.ok ? 'success-text' : 'error-text');
|
|
|
|
if (res.ok && data.choices) {
|
|
const reply = data.choices[0]?.message?.content || '';
|
|
append(`回复内容:\n${reply}`, 'success-text');
|
|
setStatus('ok', '请求成功');
|
|
} else if (res.ok && data.error) {
|
|
append(`Dify 错误:${JSON.stringify(data.error, null, 2)}`, 'error-text');
|
|
setStatus('err', 'Dify 返回错误');
|
|
} else {
|
|
append(`原始响应:\n${typeof data === 'string' ? data : JSON.stringify(data, null, 2)}`, 'error-text');
|
|
setStatus('err', `HTTP ${res.status}`);
|
|
}
|
|
} catch (e) {
|
|
if (e.name === 'AbortError') {
|
|
append('请求已取消', 'warn-text');
|
|
setStatus('idle', '已取消');
|
|
} else {
|
|
append(`网络错误:${e.message}`, 'error-text');
|
|
setStatus('err', '网络错误');
|
|
}
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|