mirror of
https://github.com/7836246/cursor2api.git
synced 2026-05-08 14:48:18 +08:00
- 重构为标准 Go 项目结构 (cmd/server, internal/) - 配置改为 YAML 格式 - 添加 Anthropic Messages API 支持 - 添加 OpenAI Chat API 支持 - 浏览器自动化处理人机验证 - 添加详细中文注释 - 添加免责声明
365 lines
10 KiB
HTML
365 lines
10 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>Cursor2API 测试</title>
|
|
<style>
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
background: linear-gradient(135deg, #fef9e7 0%, #fdebd0 100%);
|
|
min-height: 100vh;
|
|
display: flex;
|
|
flex-direction: column;
|
|
}
|
|
.container {
|
|
max-width: 1400px;
|
|
width: 95%;
|
|
margin: 30px auto;
|
|
padding: 24px;
|
|
flex: 1;
|
|
display: flex;
|
|
flex-direction: column;
|
|
}
|
|
.header {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 16px;
|
|
margin-bottom: 24px;
|
|
padding-bottom: 20px;
|
|
border-bottom: 1px solid rgba(224, 213, 192, 0.5);
|
|
}
|
|
.header h1 {
|
|
font-size: 22px;
|
|
color: #2c3e50;
|
|
font-weight: 600;
|
|
letter-spacing: -0.5px;
|
|
}
|
|
.status {
|
|
padding: 6px 16px;
|
|
border-radius: 20px;
|
|
font-size: 12px;
|
|
font-weight: 500;
|
|
background: #e8e8e8;
|
|
color: #666;
|
|
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
|
}
|
|
.status.ready { background: linear-gradient(135deg, #d4edda, #c3e6cb); color: #155724; }
|
|
.status.loading { background: linear-gradient(135deg, #fff3cd, #ffeeba); color: #856404; }
|
|
.chat-area {
|
|
flex: 1;
|
|
background: linear-gradient(180deg, #fffdf8 0%, #fffbf0 100%);
|
|
border-radius: 16px;
|
|
border: 1px solid #e0d5c0;
|
|
box-shadow: 0 4px 20px rgba(0,0,0,0.04), inset 0 1px 0 rgba(255,255,255,0.8);
|
|
overflow-y: auto;
|
|
padding: 28px;
|
|
margin-bottom: 24px;
|
|
min-height: 500px;
|
|
}
|
|
.message {
|
|
margin-bottom: 20px;
|
|
padding: 16px 20px;
|
|
border-radius: 12px;
|
|
max-width: 80%;
|
|
transition: transform 0.2s ease, box-shadow 0.2s ease;
|
|
}
|
|
.message:hover {
|
|
transform: translateY(-1px);
|
|
box-shadow: 0 4px 12px rgba(0,0,0,0.06);
|
|
}
|
|
.message.user {
|
|
background: linear-gradient(135deg, #f8f3e8 0%, #f5f0e5 100%);
|
|
margin-left: auto;
|
|
border: 1px solid #e0d5c0;
|
|
border-radius: 12px 12px 4px 12px;
|
|
}
|
|
.message.assistant {
|
|
background: linear-gradient(135deg, #ffffff 0%, #fefefe 100%);
|
|
border: 1px solid #e8e0d0;
|
|
border-radius: 12px 12px 12px 4px;
|
|
box-shadow: 0 2px 8px rgba(0,0,0,0.03);
|
|
}
|
|
.message .role {
|
|
font-size: 11px;
|
|
color: #999;
|
|
margin-bottom: 6px;
|
|
font-weight: 600;
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.5px;
|
|
}
|
|
.message .content {
|
|
font-size: 15px;
|
|
line-height: 1.7;
|
|
color: #2c3e50;
|
|
white-space: pre-wrap;
|
|
}
|
|
.input-area {
|
|
background: linear-gradient(135deg, #ffffff 0%, #fefefe 100%);
|
|
border-radius: 16px;
|
|
border: 1px solid #e0d5c0;
|
|
padding: 16px 20px;
|
|
box-shadow: 0 4px 20px rgba(0,0,0,0.06), 0 1px 3px rgba(0,0,0,0.04);
|
|
}
|
|
.input-row {
|
|
display: flex;
|
|
gap: 12px;
|
|
}
|
|
textarea {
|
|
flex: 1;
|
|
border: none;
|
|
outline: none;
|
|
font-size: 15px;
|
|
resize: none;
|
|
min-height: 44px;
|
|
max-height: 180px;
|
|
font-family: inherit;
|
|
line-height: 1.5;
|
|
color: #2c3e50;
|
|
}
|
|
textarea::placeholder { color: #b0b0b0; }
|
|
.send-btn {
|
|
width: 42px;
|
|
height: 42px;
|
|
border-radius: 50%;
|
|
border: none;
|
|
background: linear-gradient(135deg, #f5a623 0%, #e67e22 100%);
|
|
color: white;
|
|
cursor: pointer;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
transition: all 0.25s ease;
|
|
box-shadow: 0 4px 12px rgba(245, 166, 35, 0.35);
|
|
}
|
|
.send-btn:hover {
|
|
transform: scale(1.08) translateY(-2px);
|
|
box-shadow: 0 6px 20px rgba(245, 166, 35, 0.45);
|
|
}
|
|
.send-btn:disabled {
|
|
opacity: 0.5;
|
|
cursor: not-allowed;
|
|
transform: none;
|
|
box-shadow: none;
|
|
}
|
|
.controls {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 12px;
|
|
margin-top: 10px;
|
|
padding-top: 10px;
|
|
border-top: 1px solid #f0e8d8;
|
|
}
|
|
.model-select {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
background: linear-gradient(135deg, #f8f4ec 0%, #f5f1e8 100%);
|
|
padding: 8px 16px;
|
|
border-radius: 20px;
|
|
font-size: 13px;
|
|
color: #666;
|
|
border: 1px solid rgba(224, 213, 192, 0.5);
|
|
transition: all 0.2s ease;
|
|
}
|
|
.model-select:hover {
|
|
background: linear-gradient(135deg, #f5f1e8 0%, #f0ece2 100%);
|
|
border-color: rgba(224, 213, 192, 0.8);
|
|
}
|
|
.model-select select {
|
|
border: none;
|
|
background: transparent;
|
|
font-size: 13px;
|
|
color: #2c3e50;
|
|
cursor: pointer;
|
|
outline: none;
|
|
font-weight: 500;
|
|
}
|
|
.agent-badge {
|
|
background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
|
|
padding: 6px 14px;
|
|
border-radius: 15px;
|
|
font-size: 12px;
|
|
color: #2e7d32;
|
|
font-weight: 600;
|
|
border: 1px solid rgba(46, 125, 50, 0.15);
|
|
}
|
|
.footer {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
font-size: 12px;
|
|
color: #a0a0a0;
|
|
padding: 16px 4px;
|
|
}
|
|
.footer span {
|
|
padding: 4px 0;
|
|
}
|
|
.typing {
|
|
display: inline-block;
|
|
}
|
|
.typing::after {
|
|
content: '...';
|
|
animation: dots 1.5s infinite;
|
|
}
|
|
@keyframes dots {
|
|
0%, 20% { content: '.'; }
|
|
40% { content: '..'; }
|
|
60%, 100% { content: '...'; }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="header">
|
|
<h1>🚀 Cursor2API 测试</h1>
|
|
<span id="status" class="status loading">检查中...</span>
|
|
</div>
|
|
|
|
<div class="chat-area" id="chatArea"></div>
|
|
|
|
<div class="input-area">
|
|
<div class="input-row">
|
|
<textarea id="input" placeholder="询问有关文档的问题" rows="1"></textarea>
|
|
<button class="send-btn" id="sendBtn" onclick="sendMessage()">
|
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
<line x1="12" y1="19" x2="12" y2="5"></line>
|
|
<polyline points="5 12 12 5 19 12"></polyline>
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
<div class="controls">
|
|
<span class="agent-badge">∞ Agent</span>
|
|
<div class="model-select">
|
|
<select id="modelSelect">
|
|
<option value="anthropic/claude-sonnet-4.5">Sonnet 4.5</option>
|
|
<option value="openai/gpt-5-nano">GPT-5 Nano</option>
|
|
<option value="google/gemini-2.5-flash">Gemini 2.5</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="footer">
|
|
<span>Tokenizer Off</span>
|
|
<span id="tokenCount">上下文: 0/200k (0%)</span>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const chatArea = document.getElementById('chatArea');
|
|
const input = document.getElementById('input');
|
|
const sendBtn = document.getElementById('sendBtn');
|
|
const statusEl = document.getElementById('status');
|
|
const modelSelect = document.getElementById('modelSelect');
|
|
|
|
let isLoading = false;
|
|
|
|
// 检查服务状态
|
|
async function checkStatus() {
|
|
try {
|
|
const res = await fetch('/health');
|
|
if (res.ok) {
|
|
statusEl.textContent = '就绪';
|
|
statusEl.className = 'status ready';
|
|
}
|
|
} catch (e) {
|
|
statusEl.textContent = '离线';
|
|
statusEl.className = 'status';
|
|
}
|
|
}
|
|
checkStatus();
|
|
|
|
function addMessage(role, content) {
|
|
const div = document.createElement('div');
|
|
div.className = `message ${role}`;
|
|
div.innerHTML = `
|
|
<div class="role">${role === 'user' ? '你' : 'AI'}</div>
|
|
<div class="content">${content}</div>
|
|
`;
|
|
chatArea.appendChild(div);
|
|
chatArea.scrollTop = chatArea.scrollHeight;
|
|
return div;
|
|
}
|
|
|
|
async function sendMessage() {
|
|
const text = input.value.trim();
|
|
if (!text || isLoading) return;
|
|
|
|
isLoading = true;
|
|
sendBtn.disabled = true;
|
|
input.value = '';
|
|
|
|
addMessage('user', text);
|
|
const assistantMsg = addMessage('assistant', '<span class="typing">思考中</span>');
|
|
const contentEl = assistantMsg.querySelector('.content');
|
|
|
|
try {
|
|
const res = await fetch('/v1/chat/completions', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
model: modelSelect.value,
|
|
messages: [{ role: 'user', content: text }],
|
|
stream: true
|
|
})
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const err = await res.json();
|
|
contentEl.textContent = `错误: ${err.error || res.statusText}`;
|
|
return;
|
|
}
|
|
|
|
const reader = res.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
let fullContent = '';
|
|
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
|
|
const chunk = decoder.decode(value);
|
|
const lines = chunk.split('\n');
|
|
|
|
for (const line of lines) {
|
|
if (line.startsWith('data: ')) {
|
|
const data = line.slice(6);
|
|
if (data === '[DONE]') continue;
|
|
try {
|
|
const json = JSON.parse(data);
|
|
const delta = json.choices?.[0]?.delta?.content || '';
|
|
fullContent += delta;
|
|
contentEl.textContent = fullContent || '...';
|
|
} catch (e) {}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!fullContent) {
|
|
contentEl.textContent = '(无响应)';
|
|
}
|
|
} catch (e) {
|
|
contentEl.textContent = `请求失败: ${e.message}`;
|
|
} finally {
|
|
isLoading = false;
|
|
sendBtn.disabled = false;
|
|
}
|
|
}
|
|
|
|
input.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
e.preventDefault();
|
|
sendMessage();
|
|
}
|
|
});
|
|
|
|
// 自动调整输入框高度
|
|
input.addEventListener('input', () => {
|
|
input.style.height = 'auto';
|
|
input.style.height = Math.min(input.scrollHeight, 150) + 'px';
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|