437 lines
12 KiB
HTML
437 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, viewport-fit=cover" />
|
|
<meta name="theme-color" content="#0f1419" />
|
|
<title>llm.famzheng.me</title>
|
|
<style>
|
|
:root {
|
|
color-scheme: dark;
|
|
--bg: #0f1419;
|
|
--bg-elev: #161b22;
|
|
--soft: rgba(255,255,255,.06);
|
|
--softer: rgba(255,255,255,.03);
|
|
--border: rgba(255,255,255,.12);
|
|
--fg: rgba(255,255,255,.94);
|
|
--dim: rgba(255,255,255,.55);
|
|
--accent: #7c3aed;
|
|
--accent2: #06b6d4;
|
|
--danger: #ef4444;
|
|
}
|
|
* { box-sizing: border-box; }
|
|
html, body {
|
|
margin: 0; padding: 0;
|
|
background: var(--bg);
|
|
color: var(--fg);
|
|
font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC',
|
|
'Microsoft YaHei', system-ui, sans-serif;
|
|
font-size: 15px;
|
|
-webkit-text-size-adjust: 100%;
|
|
-webkit-tap-highlight-color: transparent;
|
|
}
|
|
body {
|
|
/* dynamic viewport — 处理移动端软键盘 */
|
|
height: 100dvh;
|
|
overflow: hidden;
|
|
}
|
|
@supports not (height: 100dvh) {
|
|
body { height: 100vh; }
|
|
}
|
|
main {
|
|
height: 100%;
|
|
max-width: 760px;
|
|
margin: 0 auto;
|
|
padding: 12px 14px env(safe-area-inset-bottom, 12px);
|
|
display: grid;
|
|
grid-template-rows: auto auto 1fr auto;
|
|
gap: 10px;
|
|
}
|
|
|
|
header {
|
|
display: flex;
|
|
align-items: baseline;
|
|
justify-content: space-between;
|
|
gap: 8px;
|
|
}
|
|
h1 {
|
|
font-size: 1.15rem; margin: 0; font-weight: 600;
|
|
background: linear-gradient(135deg, #fff, var(--accent2));
|
|
-webkit-background-clip: text; background-clip: text;
|
|
color: transparent;
|
|
}
|
|
header small { color: var(--dim); font-size: 0.78rem; }
|
|
|
|
.config { display: flex; gap: 6px; flex-wrap: wrap; }
|
|
.config input {
|
|
flex: 1; min-width: 0;
|
|
padding: 8px 10px;
|
|
background: var(--soft);
|
|
border: 1px solid var(--border);
|
|
border-radius: 6px;
|
|
color: var(--fg); font: inherit;
|
|
}
|
|
.config input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
|
|
|
|
.thread {
|
|
overflow-y: auto;
|
|
padding: 6px 2px;
|
|
display: flex; flex-direction: column; gap: 10px;
|
|
border-top: 1px solid var(--border);
|
|
border-bottom: 1px solid var(--border);
|
|
scrollbar-gutter: stable;
|
|
/* iOS momentum */
|
|
-webkit-overflow-scrolling: touch;
|
|
}
|
|
.thread::-webkit-scrollbar { width: 8px; }
|
|
.thread::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
|
|
.thread::-webkit-scrollbar-thumb:hover { background: var(--dim); }
|
|
|
|
.empty {
|
|
margin: auto 0; text-align: center; color: var(--dim);
|
|
padding: 24px; line-height: 1.6; font-size: 0.92rem;
|
|
}
|
|
.empty kbd {
|
|
display: inline-block; padding: 1px 6px; border-radius: 4px;
|
|
background: var(--soft); border: 1px solid var(--border);
|
|
font-family: inherit; font-size: 0.85em;
|
|
}
|
|
|
|
.row { display: flex; }
|
|
.row.user { justify-content: flex-end; }
|
|
.row.assistant { justify-content: flex-start; }
|
|
.row.err { justify-content: stretch; }
|
|
|
|
.bubble {
|
|
max-width: min(85%, 640px);
|
|
padding: 10px 13px;
|
|
border-radius: 14px;
|
|
white-space: pre-wrap;
|
|
word-wrap: break-word;
|
|
overflow-wrap: anywhere;
|
|
line-height: 1.5;
|
|
font-size: 0.95rem;
|
|
box-shadow: 0 1px 2px rgba(0,0,0,.25);
|
|
}
|
|
.row.user .bubble {
|
|
background: linear-gradient(135deg, var(--accent), #4f46e5);
|
|
color: white;
|
|
border-bottom-right-radius: 4px;
|
|
}
|
|
.row.assistant .bubble {
|
|
background: var(--bg-elev);
|
|
border: 1px solid var(--border);
|
|
border-bottom-left-radius: 4px;
|
|
}
|
|
.row.err .bubble {
|
|
background: rgba(239,68,68,.12);
|
|
border: 1px solid rgba(239,68,68,.4);
|
|
color: #ff8080;
|
|
max-width: 100%; width: 100%;
|
|
font-size: 0.85rem;
|
|
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
}
|
|
|
|
.meta {
|
|
margin-top: 4px;
|
|
font-size: 0.72rem;
|
|
color: var(--dim);
|
|
display: flex; gap: 8px; align-items: center;
|
|
}
|
|
.copy-btn {
|
|
background: transparent; border: none;
|
|
color: var(--dim); cursor: pointer;
|
|
font-size: 0.72rem; padding: 0;
|
|
}
|
|
.copy-btn:hover { color: var(--fg); }
|
|
|
|
.typing {
|
|
display: inline-flex; gap: 4px;
|
|
padding: 14px 14px;
|
|
}
|
|
.typing span {
|
|
width: 6px; height: 6px; border-radius: 50%;
|
|
background: var(--dim); animation: bounce 1.2s infinite;
|
|
}
|
|
.typing span:nth-child(2) { animation-delay: .15s; }
|
|
.typing span:nth-child(3) { animation-delay: .30s; }
|
|
@keyframes bounce {
|
|
0%,60%,100% { transform: translateY(0); opacity: .45; }
|
|
30% { transform: translateY(-4px); opacity: 1; }
|
|
}
|
|
|
|
footer {
|
|
display: flex; gap: 8px; align-items: flex-end;
|
|
}
|
|
textarea {
|
|
flex: 1;
|
|
resize: none;
|
|
min-height: 44px;
|
|
max-height: 200px;
|
|
padding: 10px 12px;
|
|
background: var(--soft);
|
|
border: 1px solid var(--border);
|
|
border-radius: 10px;
|
|
color: var(--fg);
|
|
font: inherit; line-height: 1.4;
|
|
overflow-y: auto;
|
|
}
|
|
textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
|
|
|
|
.send {
|
|
background: linear-gradient(135deg, var(--accent), var(--accent2));
|
|
color: white; border: none;
|
|
padding: 0 18px;
|
|
height: 44px;
|
|
border-radius: 10px;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
flex-shrink: 0;
|
|
}
|
|
.send:disabled {
|
|
background: var(--soft); color: var(--dim); cursor: not-allowed;
|
|
}
|
|
.ghost {
|
|
background: transparent; border: 1px solid var(--border);
|
|
color: var(--fg);
|
|
padding: 8px 12px;
|
|
border-radius: 6px;
|
|
font-size: 0.85rem;
|
|
cursor: pointer;
|
|
}
|
|
.ghost:hover { background: var(--soft); }
|
|
|
|
details {
|
|
color: var(--dim); font-size: 0.85rem;
|
|
grid-row: auto;
|
|
}
|
|
details summary { cursor: pointer; padding: 4px 0; }
|
|
details summary:hover { color: var(--fg); }
|
|
details pre {
|
|
background: rgba(0,0,0,.4); padding: 10px;
|
|
border-radius: 8px; overflow-x: auto;
|
|
border: 1px solid var(--border);
|
|
color: var(--fg);
|
|
font-size: 0.82rem;
|
|
margin: 6px 0 0;
|
|
}
|
|
|
|
@media (max-width: 520px) {
|
|
main { padding: 8px 10px env(safe-area-inset-bottom, 8px); gap: 8px; }
|
|
h1 { font-size: 1rem; }
|
|
header small { display: none; }
|
|
.bubble { font-size: 0.92rem; }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<header>
|
|
<h1>llm.famzheng.me</h1>
|
|
<small id="meta">gemma-4-31b-it · 反向代理</small>
|
|
</header>
|
|
|
|
<div class="config">
|
|
<input id="token" type="password" autocomplete="off" spellcheck="false"
|
|
placeholder="your auth token" />
|
|
<button class="ghost" id="reset" type="button">清空对话</button>
|
|
</div>
|
|
|
|
<div class="thread" id="thread">
|
|
<div class="empty" id="empty">
|
|
填好 token 后开聊。<br />
|
|
<kbd>Enter</kbd> 发送 · <kbd>Shift+Enter</kbd> 换行
|
|
</div>
|
|
</div>
|
|
|
|
<footer>
|
|
<textarea id="input" rows="1" placeholder="说点什么..."
|
|
autocomplete="off" autocapitalize="off"></textarea>
|
|
<button class="send" id="send" type="button">发送</button>
|
|
</footer>
|
|
|
|
<details>
|
|
<summary>curl example</summary>
|
|
<pre>curl -X POST https://llm.famzheng.me/v1/chat/completions \
|
|
-H 'Authorization: token <your-token>' \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{
|
|
"model": "gemma-4-31b-it",
|
|
"messages": [{"role":"user","content":"hello"}]
|
|
}'</pre>
|
|
</details>
|
|
</main>
|
|
|
|
<script>
|
|
const TOKEN_KEY = 'llm-proxy-token'
|
|
const tokenInput = document.getElementById('token')
|
|
const sendBtn = document.getElementById('send')
|
|
const resetBtn = document.getElementById('reset')
|
|
const input = document.getElementById('input')
|
|
const thread = document.getElementById('thread')
|
|
const empty = document.getElementById('empty')
|
|
|
|
tokenInput.value = localStorage.getItem(TOKEN_KEY) || ''
|
|
tokenInput.addEventListener('change', () => {
|
|
localStorage.setItem(TOKEN_KEY, tokenInput.value.trim())
|
|
})
|
|
|
|
const history = []
|
|
|
|
function clearEmpty() {
|
|
if (empty && empty.parentNode === thread) thread.removeChild(empty)
|
|
}
|
|
|
|
function scrollToBottom() {
|
|
// double rAF: 一次让浏览器 layout 新节点,第二次再滚
|
|
requestAnimationFrame(() => {
|
|
requestAnimationFrame(() => {
|
|
thread.scrollTo({ top: thread.scrollHeight, behavior: 'smooth' })
|
|
})
|
|
})
|
|
}
|
|
|
|
function addBubble(role, text, opts = {}) {
|
|
clearEmpty()
|
|
const row = document.createElement('div')
|
|
row.className = 'row ' + role
|
|
const bubble = document.createElement('div')
|
|
bubble.className = 'bubble'
|
|
bubble.textContent = text
|
|
row.appendChild(bubble)
|
|
if (role === 'assistant' && !opts.err) {
|
|
const meta = document.createElement('div')
|
|
meta.className = 'meta'
|
|
const copy = document.createElement('button')
|
|
copy.className = 'copy-btn'
|
|
copy.type = 'button'
|
|
copy.textContent = '复制'
|
|
copy.addEventListener('click', async () => {
|
|
try {
|
|
await navigator.clipboard.writeText(text)
|
|
copy.textContent = '已复制'
|
|
setTimeout(() => (copy.textContent = '复制'), 1200)
|
|
} catch {
|
|
copy.textContent = '复制失败'
|
|
}
|
|
})
|
|
meta.appendChild(copy)
|
|
bubble.appendChild(document.createElement('br'))
|
|
bubble.appendChild(meta)
|
|
}
|
|
thread.appendChild(row)
|
|
scrollToBottom()
|
|
return bubble
|
|
}
|
|
|
|
function addErr(text) {
|
|
clearEmpty()
|
|
const row = document.createElement('div')
|
|
row.className = 'row err'
|
|
const b = document.createElement('div')
|
|
b.className = 'bubble'
|
|
b.textContent = text
|
|
row.appendChild(b)
|
|
thread.appendChild(row)
|
|
scrollToBottom()
|
|
}
|
|
|
|
function addTyping() {
|
|
clearEmpty()
|
|
const row = document.createElement('div')
|
|
row.className = 'row assistant'
|
|
const bubble = document.createElement('div')
|
|
bubble.className = 'bubble typing'
|
|
bubble.innerHTML = '<span></span><span></span><span></span>'
|
|
row.appendChild(bubble)
|
|
thread.appendChild(row)
|
|
scrollToBottom()
|
|
return row
|
|
}
|
|
|
|
// textarea 自动 grow
|
|
function autoGrow() {
|
|
input.style.height = 'auto'
|
|
const next = Math.min(input.scrollHeight, 200)
|
|
input.style.height = next + 'px'
|
|
}
|
|
input.addEventListener('input', autoGrow)
|
|
|
|
async function send() {
|
|
const text = input.value.trim()
|
|
const token = tokenInput.value.trim()
|
|
if (!text) return
|
|
if (!token) {
|
|
addErr('先在上方填 token。')
|
|
tokenInput.focus()
|
|
return
|
|
}
|
|
input.value = ''
|
|
autoGrow()
|
|
history.push({ role: 'user', content: text })
|
|
addBubble('user', text)
|
|
sendBtn.disabled = true
|
|
const dot = addTyping()
|
|
try {
|
|
const res = await fetch('/v1/chat/completions', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'token ' + token,
|
|
},
|
|
body: JSON.stringify({
|
|
model: 'gemma-4-31b-it',
|
|
messages: history,
|
|
}),
|
|
})
|
|
const body = await res.text()
|
|
dot.remove()
|
|
if (!res.ok) {
|
|
addErr(`HTTP ${res.status} — ${body || '(空响应)'}`)
|
|
history.pop()
|
|
return
|
|
}
|
|
let data
|
|
try {
|
|
data = JSON.parse(body)
|
|
} catch {
|
|
addErr('上游返回非 JSON: ' + body.slice(0, 300))
|
|
history.pop()
|
|
return
|
|
}
|
|
const reply = data?.choices?.[0]?.message?.content?.trim() || '(空回复)'
|
|
history.push({ role: 'assistant', content: reply })
|
|
addBubble('assistant', reply)
|
|
} catch (e) {
|
|
dot.remove()
|
|
addErr('网络错误: ' + e.message)
|
|
history.pop()
|
|
} finally {
|
|
sendBtn.disabled = false
|
|
input.focus()
|
|
}
|
|
}
|
|
|
|
sendBtn.addEventListener('click', send)
|
|
input.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
e.preventDefault()
|
|
send()
|
|
}
|
|
})
|
|
resetBtn.addEventListener('click', () => {
|
|
history.length = 0
|
|
thread.innerHTML = ''
|
|
thread.appendChild(empty)
|
|
input.focus()
|
|
})
|
|
|
|
// 自动聚焦:如果已有 token 聚焦输入框,否则聚焦 token 框
|
|
window.addEventListener('load', () => {
|
|
if (tokenInput.value) input.focus()
|
|
else tokenInput.focus()
|
|
})
|
|
</script>
|
|
</body>
|
|
</html>
|