feat: 修正 Dify 端点为 /chat-messages,补全 inputs 字段;新增直连测试页和 relay 测试页
This commit is contained in:
@@ -162,8 +162,12 @@ async def chat_completions(request: Request):
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
# 转发到 Dify
|
||||
dify_endpoint = f"{DIFY_URL.rstrip('/')}/chat/completions"
|
||||
# 转发到 Dify(Dify 原生端点 /chat-messages)
|
||||
# 确保 body 包含 inputs 字段(Dify 应用可能要求此字段)
|
||||
if "inputs" not in body:
|
||||
body["inputs"] = {}
|
||||
|
||||
dify_endpoint = f"{DIFY_URL.rstrip('/')}/chat-messages"
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
|
||||
294
test-dify.html
Normal file
294
test-dify.html
Normal file
@@ -0,0 +1,294 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Dify API — 直连测试</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: 680px; }
|
||||
.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); }
|
||||
.badge {
|
||||
display: inline-block; background: #1e2a3a; border: 1px solid var(--border);
|
||||
border-radius: 20px; font-size: 0.7rem; color: var(--accent);
|
||||
padding: 2px 10px; margin-top: 8px; letter-spacing: 0.05em;
|
||||
}
|
||||
.field { margin-bottom: 18px; }
|
||||
.field label {
|
||||
display: block; font-size: 0.78rem; font-weight: 600;
|
||||
color: var(--muted); text-transform: uppercase;
|
||||
letter-spacing: 0.06em; margin-bottom: 8px;
|
||||
}
|
||||
.field input {
|
||||
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;
|
||||
font-family: 'Fira Code', 'Consolas', monospace;
|
||||
}
|
||||
.field input:focus { border-color: var(--primary); }
|
||||
.field small { display: block; color: var(--muted); font-size: 0.75rem; margin-top: 5px; }
|
||||
.btn-row { display: flex; gap: 12px; margin-top: 24px; }
|
||||
.btn {
|
||||
flex: 1; padding: 11px; 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-bar {
|
||||
display: flex; align-items: center; gap: 10px;
|
||||
padding: 10px 14px; 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 {
|
||||
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.75; color: #a5b4fc;
|
||||
white-space: pre-wrap; word-break: break-all; max-height: 420px;
|
||||
overflow-y: auto; min-height: 80px;
|
||||
}
|
||||
#output::-webkit-scrollbar { width: 5px; }
|
||||
#output::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
|
||||
.error-text { color: #fca5a5; }
|
||||
.success-text { color: #86efac; }
|
||||
.warn-text { color: #fcd34d; }
|
||||
.meta-text { color: #94a3b8; }
|
||||
.spinner-wrap {
|
||||
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 API 直连测试</h1>
|
||||
<p>直接在浏览器测试 Dify 接口,无需中转服务</p>
|
||||
<span class="badge">纯前端 · 不经过任何中转</span>
|
||||
</div>
|
||||
|
||||
<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>API BASE_URL</label>
|
||||
<input type="text" id="baseUrl" placeholder="https://atwdify.antiwearvalve.com" />
|
||||
<small>填 Dify 对外可访问的根地址,末尾不要加斜杠</small>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>API KEY</label>
|
||||
<input type="password" id="apiKey" placeholder="app-xxxxxxxxxxxxxxxx" />
|
||||
<small>从 Dify 应用 → API 密钥 中复制完整密钥</small>
|
||||
</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="spinner-wrap" id="spinner">
|
||||
<div class="spinner"></div>
|
||||
<span>AI 正在生成回复…</span>
|
||||
</div>
|
||||
<div id="output">// 响应结果将在此显示</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let abortCtrl = null;
|
||||
|
||||
function $id(id) { return document.getElementById(id); }
|
||||
function baseUrl() { return $id('baseUrl').value.replace(/\/$/, ''); }
|
||||
function apiKey() { return $id('apiKey').value.trim(); }
|
||||
|
||||
function setStatus(type, text) {
|
||||
$id('statusDot').className = 'dot ' + type;
|
||||
$id('statusText').textContent = text;
|
||||
}
|
||||
function clearOut() { $id('output').innerHTML = ''; }
|
||||
function append(text, cls) {
|
||||
var el = $id('output');
|
||||
var span = document.createElement('span');
|
||||
if (cls) span.className = cls;
|
||||
span.textContent = text;
|
||||
el.appendChild(span);
|
||||
el.scrollTop = el.scrollHeight;
|
||||
}
|
||||
function nl() { append('\n'); }
|
||||
|
||||
function setLoading(on) {
|
||||
$id('sendBtn').disabled = on;
|
||||
$id('spinner').style.display = on ? 'flex' : 'none';
|
||||
}
|
||||
|
||||
// ── Health Check ───────────────────────────────────────
|
||||
async function testHealth() {
|
||||
var url = baseUrl();
|
||||
if (!url) { alert('请填写 API BASE_URL'); return; }
|
||||
clearOut();
|
||||
setStatus('idle', '检测中…');
|
||||
append('POST ' + url + '/chat-messages\n', 'meta-text');
|
||||
try {
|
||||
var res = await fetch(url + '/chat-messages', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + (apiKey() || ''),
|
||||
},
|
||||
body: JSON.stringify({
|
||||
query: 'health-check',
|
||||
user: 'test-user',
|
||||
response_mode: 'blocking',
|
||||
inputs: {},
|
||||
}),
|
||||
});
|
||||
var raw = await res.text();
|
||||
var data;
|
||||
try { data = JSON.parse(raw); } catch { data = raw; }
|
||||
|
||||
nl();
|
||||
if (res.status === 200) {
|
||||
append('✅ 连接成功 — HTTP ' + res.status + '\n', 'success-text');
|
||||
append('接口可达\n', 'meta-text');
|
||||
setStatus('ok', '接口可达');
|
||||
} else if (res.status >= 400) {
|
||||
append('[HTTP ' + res.status + '] 连接可达\n', 'success-text');
|
||||
append('响应:' + JSON.stringify(data, null, 2), '');
|
||||
setStatus('ok', '接口可达');
|
||||
} else {
|
||||
append('[HTTP ' + res.status + ']\n', '');
|
||||
append(JSON.stringify(data, null, 2), '');
|
||||
setStatus('ok', 'HTTP ' + res.status);
|
||||
}
|
||||
} catch (e) {
|
||||
nl();
|
||||
append('❌ 连接失败:' + e.message, 'error-text');
|
||||
setStatus('err', '无法连接');
|
||||
}
|
||||
}
|
||||
|
||||
// ── Send Message ───────────────────────────────────────
|
||||
async function sendMessage() {
|
||||
var url = baseUrl();
|
||||
var key = apiKey();
|
||||
if (!url) { alert('请填写 API BASE_URL'); return; }
|
||||
if (!key) { alert('请填写 API KEY'); return; }
|
||||
|
||||
if (abortCtrl) abortCtrl.abort();
|
||||
abortCtrl = new AbortController();
|
||||
|
||||
clearOut();
|
||||
setLoading(true);
|
||||
setStatus('idle', '请求中…');
|
||||
|
||||
var payload = {
|
||||
query: '你好,请用一句话介绍自己。',
|
||||
user: 'test-user',
|
||||
response_mode: 'blocking',
|
||||
inputs: {},
|
||||
};
|
||||
|
||||
append('POST ' + url + '/chat-messages\n', 'meta-text');
|
||||
append('Authorization: Bearer ' + key.slice(0, 8) + '…\n', 'meta-text');
|
||||
nl();
|
||||
append('Request:\n' + JSON.stringify(payload, null, 2) + '\n\n', 'meta-text');
|
||||
append('Response:\n');
|
||||
|
||||
try {
|
||||
var res = await fetch(url + '/chat-messages', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + key,
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
signal: abortCtrl.signal,
|
||||
});
|
||||
|
||||
var raw = await res.text();
|
||||
var 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.answer) {
|
||||
append('回复内容:\n' + data.answer, 'success-text');
|
||||
if (data.conversation_id) {
|
||||
append('\n\n会话ID:' + data.conversation_id, 'meta-text');
|
||||
}
|
||||
setStatus('ok', '测试成功');
|
||||
} else if (data.error) {
|
||||
append('Dify 错误:\n' + JSON.stringify(data.error, null, 2), 'error-text');
|
||||
setStatus('err', 'Dify 返回错误');
|
||||
} else {
|
||||
append(typeof data === 'string' ? data : JSON.stringify(data, null, 2), '');
|
||||
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>
|
||||
414
test.html
Normal file
414
test.html
Normal file
@@ -0,0 +1,414 @@
|
||||
<!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>
|
||||
Reference in New Issue
Block a user