Files
闫旭隆 e0aff02e31 20260109
2026-01-09 13:36:01 +08:00

633 lines
19 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>需求文档生成器</title>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Microsoft YaHei', sans-serif;
background: #f5f5f5;
color: #333;
min-height: 100vh;
}
.container {
max-width: 900px;
margin: 0 auto;
padding: 40px 20px;
}
/* 启动页 */
.start-page {
text-align: center;
padding: 100px 20px;
}
.start-page h1 {
font-size: 2.5em;
color: #1a1a1a;
margin-bottom: 16px;
}
.start-page p {
color: #666;
font-size: 1.1em;
margin-bottom: 40px;
}
.start-btn {
background: #2563eb;
color: #fff;
border: none;
padding: 16px 48px;
font-size: 1.1em;
border-radius: 8px;
cursor: pointer;
transition: background 0.2s;
}
.start-btn:hover { background: #1d4ed8; }
.start-btn:disabled { background: #9ca3af; cursor: not-allowed; }
/* 主界面 */
.main-page { display: none; }
.main-page.active { display: block; }
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 24px;
padding-bottom: 16px;
border-bottom: 1px solid #e5e5e5;
}
.header h1 { font-size: 1.5em; color: #1a1a1a; }
.header .status {
display: flex;
align-items: center;
gap: 8px;
color: #666;
font-size: 0.9em;
}
.header .status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #d1d5db;
}
.header .status-dot.active {
background: #22c55e;
animation: pulse 1.5s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
/* 日志区域 */
.log-section {
background: #fff;
border: 1px solid #e5e5e5;
border-radius: 8px;
margin-bottom: 20px;
}
.log-header {
padding: 12px 16px;
border-bottom: 1px solid #e5e5e5;
font-weight: 500;
color: #374151;
font-size: 0.9em;
}
#output {
padding: 16px;
max-height: 400px;
overflow-y: auto;
font-family: 'SF Mono', Consolas, monospace;
font-size: 13px;
line-height: 1.6;
}
.msg { padding: 8px 12px; margin-bottom: 8px; border-radius: 6px; }
.msg.status { color: #6b7280; background: transparent; padding: 4px 0; }
.msg.assistant { color: #1f2937; background: #f3f4f6; white-space: pre-wrap; }
.msg.tool { color: #7c3aed; background: #f5f3ff; font-size: 0.9em; }
.msg.error { color: #dc2626; background: #fef2f2; }
.msg.result { color: #059669; background: #ecfdf5; font-weight: 500; }
.msg.user { color: #1d4ed8; background: #eff6ff; }
/* 输入区域 */
.input-section {
background: #fff;
border: 1px solid #e5e5e5;
border-radius: 8px;
padding: 16px;
display: flex;
gap: 12px;
}
.input-section textarea {
flex: 1;
border: 1px solid #d1d5db;
border-radius: 6px;
padding: 12px;
font-size: 14px;
resize: none;
min-height: 60px;
font-family: inherit;
}
.input-section textarea:focus { outline: none; border-color: #2563eb; }
.input-section button {
background: #2563eb;
color: #fff;
border: none;
padding: 12px 24px;
border-radius: 6px;
cursor: pointer;
font-weight: 500;
align-self: flex-end;
}
.input-section button:hover { background: #1d4ed8; }
.input-section button:disabled { background: #9ca3af; cursor: not-allowed; }
/* 文档区域 */
.doc-section {
display: none;
background: #fff;
border: 1px solid #e5e5e5;
border-radius: 8px;
margin-top: 20px;
}
.doc-section.active { display: block; }
.doc-header {
padding: 12px 16px;
border-bottom: 1px solid #e5e5e5;
display: flex;
justify-content: space-between;
align-items: center;
}
.doc-header span { font-weight: 500; color: #059669; }
.doc-header button {
background: #f3f4f6;
border: 1px solid #d1d5db;
padding: 6px 12px;
border-radius: 4px;
cursor: pointer;
font-size: 0.9em;
}
.doc-header button:hover { background: #e5e7eb; }
#docContent {
padding: 24px;
max-height: 500px;
overflow-y: auto;
line-height: 1.8;
}
#docContent h1, #docContent h2, #docContent h3 { margin-top: 24px; margin-bottom: 12px; }
#docContent h1 { font-size: 1.5em; border-bottom: 1px solid #e5e5e5; padding-bottom: 8px; }
#docContent h2 { font-size: 1.25em; }
#docContent ul, #docContent ol { margin-left: 20px; margin-bottom: 12px; }
#docContent li { margin-bottom: 6px; }
#docContent table { border-collapse: collapse; width: 100%; margin: 12px 0; }
#docContent th, #docContent td { border: 1px solid #e5e5e5; padding: 8px 12px; text-align: left; }
#docContent th { background: #f9fafb; }
#docContent code { background: #f3f4f6; padding: 2px 6px; border-radius: 4px; font-size: 0.9em; }
#docContent pre { background: #1f2937; color: #f9fafb; padding: 16px; border-radius: 6px; overflow-x: auto; }
#docContent pre code { background: transparent; }
/* 弹窗遮罩 */
.modal-overlay {
display: none;
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 1000;
align-items: center;
justify-content: center;
}
.modal-overlay.active { display: flex; }
/* 弹窗 */
.modal {
background: #fff;
border-radius: 12px;
width: 90%;
max-width: 500px;
max-height: 80vh;
overflow-y: auto;
box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1), 0 10px 10px -5px rgba(0,0,0,0.04);
}
.modal-header {
padding: 20px 24px;
border-bottom: 1px solid #e5e5e5;
}
.modal-header h2 {
font-size: 1.1em;
color: #1f2937;
}
.modal-body { padding: 24px; }
.modal-footer {
padding: 16px 24px;
border-top: 1px solid #e5e5e5;
text-align: right;
}
.modal-footer button {
background: #2563eb;
color: #fff;
border: none;
padding: 10px 24px;
border-radius: 6px;
cursor: pointer;
font-weight: 500;
}
.modal-footer button:hover { background: #1d4ed8; }
/* 问题样式 */
.question-item { margin-bottom: 20px; }
.question-item:last-child { margin-bottom: 0; }
.question-label {
font-weight: 500;
color: #374151;
margin-bottom: 12px;
display: block;
}
.question-tag {
display: inline-block;
background: #dbeafe;
color: #1d4ed8;
padding: 2px 8px;
border-radius: 4px;
font-size: 0.8em;
margin-right: 8px;
}
.option-item {
display: flex;
align-items: flex-start;
padding: 12px;
margin-bottom: 8px;
border: 1px solid #e5e5e5;
border-radius: 6px;
cursor: pointer;
transition: all 0.15s;
}
.option-item:hover { border-color: #2563eb; background: #f8fafc; }
.option-item.selected { border-color: #2563eb; background: #eff6ff; }
.option-item input { margin-right: 12px; margin-top: 2px; }
.option-label { font-weight: 500; color: #1f2937; }
.option-desc { color: #6b7280; font-size: 0.9em; margin-top: 2px; }
.other-input {
width: 100%;
margin-top: 8px;
padding: 8px 12px;
border: 1px solid #d1d5db;
border-radius: 4px;
font-size: 14px;
}
.other-input:focus { outline: none; border-color: #2563eb; }
</style>
</head>
<body>
<!-- 启动页 -->
<div class="start-page" id="startPage">
<div class="container">
<h1>需求文档生成器</h1>
<p>通过智能访谈收集需求,自动生成结构化需求文档</p>
<button class="start-btn" id="startBtn" onclick="startSkill()">开始生成</button>
</div>
</div>
<!-- 主界面 -->
<div class="main-page" id="mainPage">
<div class="container">
<div class="header">
<h1>需求文档生成器</h1>
<div class="status">
<span class="status-dot" id="statusDot"></span>
<span id="statusText">就绪</span>
</div>
</div>
<div class="log-section">
<div class="log-header">执行日志</div>
<div id="output"></div>
</div>
<div class="input-section">
<textarea id="userInput" placeholder="输入内容..." onkeydown="handleKeyDown(event)"></textarea>
<button id="sendBtn" onclick="sendMessage()">发送</button>
</div>
<div class="doc-section" id="docSection">
<div class="doc-header">
<span>需求文档已生成</span>
<button onclick="downloadDocument()">下载</button>
</div>
<div id="docContent"></div>
</div>
</div>
</div>
<!-- 问题弹窗 -->
<div class="modal-overlay" id="modal">
<div class="modal">
<div class="modal-header">
<h2>请回答以下问题</h2>
</div>
<div class="modal-body" id="modalBody"></div>
<div class="modal-footer">
<button onclick="submitAnswers()">提交</button>
</div>
</div>
</div>
<script>
let currentSessionId = null;
let currentRequestId = null;
let currentQuestions = [];
let isProcessing = false;
function log(text, type = '') {
const output = document.getElementById('output');
const div = document.createElement('div');
div.className = 'msg ' + type;
div.innerHTML = text.replace(/\n/g, '<br>');
output.appendChild(div);
output.scrollTop = output.scrollHeight;
}
function setStatus(text, active = false) {
document.getElementById('statusText').textContent = text;
document.getElementById('statusDot').classList.toggle('active', active);
}
async function startSkill() {
document.getElementById('startPage').style.display = 'none';
document.getElementById('mainPage').classList.add('active');
isProcessing = true;
document.getElementById('sendBtn').disabled = true;
setStatus('执行中...', true);
await connectSSE('/api/stream?prompt=' + encodeURIComponent('生成需求文档'));
isProcessing = false;
document.getElementById('sendBtn').disabled = false;
setStatus('就绪', false);
}
async function sendMessage() {
const input = document.getElementById('userInput');
const prompt = input.value.trim();
if (!prompt || isProcessing) return;
isProcessing = true;
document.getElementById('sendBtn').disabled = true;
setStatus('执行中...', true);
log(prompt, 'user');
input.value = '';
let url = `/api/stream?prompt=${encodeURIComponent(prompt)}`;
if (currentSessionId) {
url += `&sessionId=${encodeURIComponent(currentSessionId)}`;
}
await connectSSE(url);
isProcessing = false;
document.getElementById('sendBtn').disabled = false;
setStatus('就绪', false);
}
function handleKeyDown(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
}
async function connectSSE(url) {
try {
const res = await fetch(url);
currentRequestId = res.headers.get('X-Request-Id');
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('data: ')) {
try {
handleEvent(JSON.parse(line.slice(6)));
} catch (e) {}
}
}
}
} catch (e) {
log('连接错误: ' + e.message, 'error');
}
}
function handleEvent(data) {
switch (data.type) {
case 'status':
log(data.message, 'status');
break;
case 'session':
currentSessionId = data.clientSessionId;
break;
case 'delta':
let last = document.querySelector('#output .msg.assistant:last-child');
if (!last) {
last = document.createElement('div');
last.className = 'msg assistant';
document.getElementById('output').appendChild(last);
}
last.innerHTML += data.text.replace(/\n/g, '<br>');
document.getElementById('output').scrollTop = document.getElementById('output').scrollHeight;
break;
case 'assistant':
log(data.content, 'assistant');
break;
case 'tool_start':
if (data.toolName && data.toolName !== 'AskUserQuestion') {
log('[' + data.toolName + '] 调用中...', 'tool');
}
break;
case 'tool_calls':
if (data.tools) {
data.tools.forEach(t => {
if (t.name === 'AskUserQuestion') return;
let info = '[' + t.name + '] ';
if (t.name === 'Task' && t.input) {
// Task 工具显示 sub-agent 名称和描述
const agent = t.input.subagent_type || t.input.agent || '';
const desc = t.input.description || '';
info += agent + (desc ? ' - ' + desc : '');
} else if (t.name === 'Read' && t.input?.file_path) {
info += t.input.file_path.split(/[/\\]/).pop();
} else if (t.name === 'Write' && t.input?.file_path) {
info += '写入 ' + t.input.file_path.split(/[/\\]/).pop();
} else if (t.name === 'Edit' && t.input?.file_path) {
info += '编辑 ' + t.input.file_path.split(/[/\\]/).pop();
} else if (t.name === 'Bash' && t.input?.command) {
const cmd = t.input.command;
info += cmd.length > 50 ? cmd.substring(0, 50) + '...' : cmd;
} else if (t.name === 'Skill') {
info += t.input?.skill || '';
} else if (t.input) {
const str = JSON.stringify(t.input);
info += str.length > 40 ? str.substring(0, 40) + '...' : str;
}
log(info, 'tool');
});
}
break;
case 'ask_user_question':
currentRequestId = data.requestId;
showModal(data.questions);
break;
case 'result':
log('执行完成', 'result');
checkDocument();
// 如果是等待用户输入的阶段,高亮输入框
highlightInput();
break;
case 'done':
log(data.message, 'status');
break;
case 'error':
log('错误: ' + data.message, 'error');
break;
}
}
function showModal(questions) {
currentQuestions = questions;
const body = document.getElementById('modalBody');
body.innerHTML = '';
questions.forEach((q, i) => {
const div = document.createElement('div');
div.className = 'question-item';
div.innerHTML = `
<label class="question-label">
<span class="question-tag">${q.header || '问题'}</span>
${q.question}
</label>
<div class="options" data-idx="${i}" data-multi="${q.multiSelect || false}">
${q.options.map((opt, j) => `
<div class="option-item" onclick="selectOption(this, ${i}, ${j})">
<input type="${q.multiSelect ? 'checkbox' : 'radio'}" name="q${i}">
<div>
<div class="option-label">${opt.label}</div>
${opt.description ? `<div class="option-desc">${opt.description}</div>` : ''}
</div>
</div>
`).join('')}
<div class="option-item" onclick="selectOption(this, ${i}, -1)">
<input type="${q.multiSelect ? 'checkbox' : 'radio'}" name="q${i}" value="__other__">
<div style="flex:1">
<div class="option-label">其他</div>
<input type="text" class="other-input" id="other${i}" placeholder="输入自定义内容" onclick="event.stopPropagation()">
</div>
</div>
</div>
`;
body.appendChild(div);
});
document.getElementById('modal').classList.add('active');
}
function selectOption(el, qIdx, oIdx) {
const input = el.querySelector('input[type="radio"], input[type="checkbox"]');
const container = el.parentElement;
const isMulti = container.dataset.multi === 'true';
if (!isMulti) {
container.querySelectorAll('.option-item').forEach(item => {
item.classList.remove('selected');
item.querySelector('input').checked = false;
});
}
el.classList.toggle('selected');
input.checked = el.classList.contains('selected');
}
async function submitAnswers() {
const answers = {};
currentQuestions.forEach((q, i) => {
const container = document.querySelector(`[data-idx="${i}"]`);
const checked = container.querySelectorAll('input:checked');
const values = [];
checked.forEach(input => {
if (input.value === '__other__') {
const other = document.getElementById(`other${i}`);
if (other.value.trim()) values.push(other.value.trim());
} else {
const label = input.closest('.option-item').querySelector('.option-label');
if (label) values.push(label.textContent);
}
});
answers[q.question] = values.join(', ') || '(未选择)';
});
log('已回答: ' + Object.values(answers).join('; '), 'user');
try {
await fetch('/api/answer', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ requestId: currentRequestId, answers })
});
} catch (e) {
log('提交失败: ' + e.message, 'error');
}
document.getElementById('modal').classList.remove('active');
}
async function checkDocument() {
try {
const res = await fetch('/api/document?type=final');
if (res.ok) {
const data = await res.json();
document.getElementById('docSection').classList.add('active');
document.getElementById('docContent').innerHTML = marked.parse(data.content);
}
} catch (e) {}
}
function downloadDocument() {
window.open('/api/download?type=final', '_blank');
}
function highlightInput() {
const input = document.getElementById('userInput');
const section = document.querySelector('.input-section');
// 添加高亮样式
section.style.border = '2px solid #2563eb';
section.style.boxShadow = '0 0 0 3px rgba(37, 99, 235, 0.2)';
input.placeholder = '请在此输入您的项目描述,然后点击发送...';
input.focus();
// 3秒后移除高亮
setTimeout(() => {
section.style.border = '1px solid #e5e5e5';
section.style.boxShadow = 'none';
}, 3000);
}
</script>
</body>
</html>