feat: Phase 1 — 项目骨架搭建,集成 Gin、SQLite 和设置页面

This commit is contained in:
2026-06-18 22:48:16 +08:00
parent 46f1bbf0f1
commit 1e9b1a2129
26 changed files with 2518 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
{{define "head"}}
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PR-Helper</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="/static/css/style.css">
<script src="/static/js/htmx.min.js"></script>
</head>
{{end}}
{{define "nav"}}
<nav class="bg-white shadow-sm border-b border-gray-200">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-14">
<div class="flex items-center space-x-8">
<a href="/" class="text-lg font-bold text-indigo-600 hover:text-indigo-700">🔀 PR-Helper</a>
<a href="/" class="text-sm text-gray-600 hover:text-gray-900">首页</a>
<a href="/settings" class="text-sm text-gray-600 hover:text-gray-900">设置</a>
</div>
</div>
</div>
</nav>
{{end}}
{{define "footer"}}
<footer class="bg-white border-t border-gray-200 py-4">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center text-sm text-gray-400">
PR-Helper — AI-powered PR description & code review
</div>
</footer>
{{end}}
+102
View File
@@ -0,0 +1,102 @@
<!DOCTYPE html>
<html lang="zh-CN" class="h-full">
{{template "head" .}}
<body class="h-full bg-gray-50 text-gray-900">
<div class="min-h-full flex flex-col">
{{template "nav" .}}
<main class="flex-1">
<div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<h1 class="text-2xl font-bold mb-6">生成 PR 描述</h1>
<!-- Ref Selection -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
<h2 class="text-lg font-semibold mb-4">选择范围</h2>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Base (基准)</label>
<input type="text" id="base-ref" placeholder="main"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Head (目标)</label>
<input type="text" id="head-ref" placeholder="feature/branch"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
</div>
</div>
<button id="generate-btn" onclick="generatePR()"
class="mt-4 bg-green-600 text-white px-5 py-2 rounded-md text-sm font-medium hover:bg-green-700">
生成 PR 描述
</button>
</div>
<!-- Output -->
<div id="pr-output" class="hidden bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<div class="flex justify-between items-center mb-4">
<h2 class="text-lg font-semibold">PR 描述</h2>
<button onclick="copyMarkdown()" class="text-sm text-indigo-600 hover:text-indigo-800">📋 复制 Markdown</button>
</div>
<div id="pr-title" class="text-xl font-bold mb-2"></div>
<div id="pr-type" class="inline-block bg-indigo-100 text-indigo-700 text-xs px-2 py-1 rounded mb-4"></div>
<div id="pr-summary" class="text-gray-700 mb-4"></div>
<div id="pr-details" class="prose prose-sm max-w-none"></div>
<div id="pr-impact" class="mt-4 p-3 bg-yellow-50 rounded text-sm text-yellow-800"></div>
</div>
</div>
</main>
{{template "footer" .}}
</div>
<script>
async function generatePR() {
const base = document.getElementById('base-ref').value.trim();
const head = document.getElementById('head-ref').value.trim();
if (!base || !head) { alert('请填写 base 和 head'); return; }
const btn = document.getElementById('generate-btn');
const output = document.getElementById('pr-output');
btn.disabled = true; btn.textContent = '生成中...';
output.classList.remove('hidden');
try {
const resp = await fetch('/api/repos/{{.ID}}/generate', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({base, head})
});
if (!resp.ok) { const data = await resp.json(); throw new Error(data.error || '生成失败'); }
const reader = resp.body.getReader();
const decoder = new TextDecoder();
let buffer = '', eventType = '';
while (true) {
const {done, value} = await reader.read();
if (done) break;
buffer += decoder.decode(value, {stream: true});
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('event: ')) eventType = line.slice(7).trim();
else if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.slice(6));
if (eventType === 'title') document.getElementById('pr-title').textContent = data.content;
if (eventType === 'type') document.getElementById('pr-type').textContent = data.content;
if (eventType === 'summary') document.getElementById('pr-summary').textContent = data.content;
if (eventType === 'detail') document.getElementById('pr-details').innerHTML = data.content;
if (eventType === 'impact') document.getElementById('pr-impact').textContent = '影响范围: ' + data.content;
} catch {}
}
}
}
} catch (err) {
document.getElementById('pr-summary').textContent = '错误: ' + err.message;
} finally {
btn.disabled = false; btn.textContent = '生成 PR 描述';
}
}
function copyMarkdown() {
const title = document.getElementById('pr-title').textContent;
const type = document.getElementById('pr-type').textContent;
const summary = document.getElementById('pr-summary').textContent;
const details = document.getElementById('pr-details').innerText;
const impact = document.getElementById('pr-impact').textContent;
const md = '# ' + title + '\n\n**Type:** ' + type + '\n\n## Summary\n' + summary + '\n\n## Details\n' + details + '\n\n## Impact\n' + impact;
navigator.clipboard.writeText(md);
}
</script>
</body>
</html>
+120
View File
@@ -0,0 +1,120 @@
<!DOCTYPE html>
<html lang="zh-CN" class="h-full">
{{template "head" .}}
<body class="h-full bg-gray-50 text-gray-900">
<div class="min-h-full flex flex-col">
{{template "nav" .}}
<main class="flex-1">
<div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Hero -->
<div class="text-center mb-10">
<h1 class="text-3xl font-bold text-gray-900 mb-3">PR-Helper</h1>
<p class="text-gray-500">输入 Git 仓库 URL,自动生成 PR 描述并进行 AI 代码审查</p>
</div>
<!-- Clone Form -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-8">
<h2 class="text-lg font-semibold mb-4">克隆仓库</h2>
<form id="clone-form" class="flex gap-3">
<input type="text" id="repo-url" name="url" placeholder="https://github.com/user/repo.git"
class="flex-1 rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500" required>
<button type="submit" id="clone-btn"
class="bg-indigo-600 text-white px-5 py-2 rounded-md text-sm font-medium hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">
克隆
</button>
</form>
<div id="clone-progress" class="hidden mt-4">
<div class="flex items-center gap-3 mb-2">
<div class="animate-spin rounded-full h-4 w-4 border-2 border-indigo-600 border-t-transparent"></div>
<span id="clone-status" class="text-sm text-gray-600">准备中...</span>
</div>
<div class="w-full bg-gray-200 rounded-full h-2">
<div id="clone-bar" class="bg-indigo-600 h-2 rounded-full transition-all duration-300" style="width:0%"></div>
</div>
</div>
<div id="clone-error" class="hidden mt-3 text-sm text-red-600"></div>
</div>
<!-- Cached Repos -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<div class="flex justify-between items-center mb-4">
<h2 class="text-lg font-semibold">已缓存仓库</h2>
<button hx-post="/api/repos/cleanup" hx-swap="none"
class="text-sm text-gray-500 hover:text-red-600"
hx-on::after-request="window.location.reload()">
清理过期缓存
</button>
</div>
<div id="repos-list">
{{if .Repos}}
<div class="divide-y divide-gray-100">
{{range .Repos}}
<div class="py-3 flex items-center justify-between">
<div>
<a href="/repo/{{.ID}}" class="text-sm font-medium text-indigo-600 hover:text-indigo-800">{{.URL}}</a>
<p class="text-xs text-gray-400 mt-1">最后使用: {{.LastUsed}}</p>
</div>
<button hx-delete="/api/repos/{{.ID}}" hx-confirm="确定删除此仓库缓存?"
hx-swap="none" hx-on::after-request="window.location.reload()"
class="text-xs text-red-500 hover:text-red-700">删除</button>
</div>
{{end}}
</div>
{{else}}
<p class="text-sm text-gray-400 text-center py-6">暂无缓存仓库</p>
{{end}}
</div>
</div>
</div>
</main>
{{template "footer" .}}
</div>
<script>
document.getElementById('clone-form').addEventListener('submit', async function(e) {
e.preventDefault();
const url = document.getElementById('repo-url').value.trim();
if (!url) return;
const btn = document.getElementById('clone-btn');
const progress = document.getElementById('clone-progress');
const status = document.getElementById('clone-status');
const bar = document.getElementById('clone-bar');
const errorEl = document.getElementById('clone-error');
btn.disabled = true;
progress.classList.remove('hidden');
errorEl.classList.add('hidden');
bar.style.width = '0%';
try {
const resp = await fetch('/api/repos', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({url: url})
});
if (!resp.ok) { const data = await resp.json(); throw new Error(data.error || 'clone failed'); }
const reader = resp.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');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.slice(6));
if (data.progress !== undefined) bar.style.width = data.progress + '%';
if (data.message) status.textContent = data.message;
if (data.done) { window.location.href = '/repo/' + data.repo_id; return; }
} catch {}
}
}
}
} catch (err) {
errorEl.textContent = err.message;
errorEl.classList.remove('hidden');
} finally {
btn.disabled = false;
}
});
</script>
</body>
</html>
+51
View File
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="zh-CN" class="h-full">
{{template "head" .}}
<body class="h-full bg-gray-50 text-gray-900">
<div class="min-h-full flex flex-col">
{{template "nav" .}}
<main class="flex-1">
<div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="flex items-center justify-between mb-6">
<h1 class="text-2xl font-bold">仓库详情</h1>
<div class="flex gap-3">
<a href="/repo/{{.ID}}/generate"
class="bg-green-600 text-white px-4 py-2 rounded-md text-sm font-medium hover:bg-green-700">
生成 PR 描述
</a>
<a href="/repo/{{.ID}}/review"
class="bg-blue-600 text-white px-4 py-2 rounded-md text-sm font-medium hover:bg-blue-700">
AI 代码审查
</a>
</div>
</div>
<!-- Repo Info -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
<div id="repo-info" class="text-sm text-gray-500">加载中...</div>
</div>
<!-- Git Graph -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<h2 class="text-lg font-semibold mb-4">Git Graph</h2>
<div id="git-graph" class="overflow-auto" style="min-height: 400px;">
<p class="text-sm text-gray-400 text-center py-10">Git Graph 将在 Phase 3 实现</p>
</div>
</div>
</div>
</main>
{{template "footer" .}}
</div>
<script>
fetch('/api/repos').then(r => r.json()).then(repos => {
const repo = repos.find(r => r.id == {{.ID}});
const el = document.getElementById('repo-info');
if (repo) {
el.innerHTML = '<p><strong>URL:</strong> ' + repo.url + '</p>' +
'<p class="mt-1"><strong>大小:</strong> ' + (repo.size_bytes / 1024 / 1024).toFixed(1) + ' MB</p>' +
'<p class="mt-1"><strong>克隆时间:</strong> ' + repo.cloned_at + '</p>';
} else {
el.innerHTML = '<p class="text-red-500">仓库未找到</p>';
}
});
</script>
</body>
</html>
+113
View File
@@ -0,0 +1,113 @@
<!DOCTYPE html>
<html lang="zh-CN" class="h-full">
{{template "head" .}}
<body class="h-full bg-gray-50 text-gray-900">
<div class="min-h-full flex flex-col">
{{template "nav" .}}
<main class="flex-1">
<div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<h1 class="text-2xl font-bold mb-6">AI 代码审查</h1>
<!-- Ref Selection + Top-N -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
<h2 class="text-lg font-semibold mb-4">审查配置</h2>
<div class="grid grid-cols-3 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Base</label>
<input type="text" id="base-ref" placeholder="main"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Head</label>
<input type="text" id="head-ref" placeholder="feature/branch"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Top-N 文件数</label>
<input type="number" id="top-n" value="{{.TopN}}" min="0"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
<p class="text-xs text-gray-400 mt-1">0 = 分析全部文件</p>
</div>
</div>
<button id="review-btn" onclick="startReview()"
class="mt-4 bg-blue-600 text-white px-5 py-2 rounded-md text-sm font-medium hover:bg-blue-700">
开始审查
</button>
</div>
<!-- Review Output -->
<div id="review-output" class="hidden space-y-6">
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<h2 class="text-lg font-semibold mb-3">整体评估</h2>
<div id="review-summary" class="prose prose-sm max-w-none text-gray-700"></div>
</div>
<div id="file-reviews" class="space-y-4"></div>
</div>
</div>
</main>
{{template "footer" .}}
</div>
<script>
async function startReview() {
const base = document.getElementById('base-ref').value.trim();
const head = document.getElementById('head-ref').value.trim();
const topN = parseInt(document.getElementById('top-n').value) || 0;
if (!base || !head) { alert('请填写 base 和 head'); return; }
const btn = document.getElementById('review-btn');
const output = document.getElementById('review-output');
const fileReviews = document.getElementById('file-reviews');
btn.disabled = true; btn.textContent = '审查中...';
output.classList.remove('hidden');
fileReviews.innerHTML = '';
document.getElementById('review-summary').innerHTML = '';
let currentFileEl = null;
try {
const resp = await fetch('/api/repos/{{.ID}}/review', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({base, head, top_n: topN})
});
if (!resp.ok) { const data = await resp.json(); throw new Error(data.error || '审查失败'); }
const reader = resp.body.getReader();
const decoder = new TextDecoder();
let buffer = '', eventType = '';
while (true) {
const {done, value} = await reader.read();
if (done) break;
buffer += decoder.decode(value, {stream: true});
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('event: ')) eventType = line.slice(7).trim();
else if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.slice(6));
if (eventType === 'file_start') {
currentFileEl = document.createElement('div');
currentFileEl.className = 'bg-white rounded-lg shadow-sm border border-gray-200 p-6';
currentFileEl.innerHTML = '<h3 class="font-semibold text-gray-900 mb-3">📄 ' + data.file + '</h3><div class="space-y-2"></div>';
fileReviews.appendChild(currentFileEl);
}
if (eventType === 'suggestion' && currentFileEl) {
const colors = {critical: 'red', warning: 'yellow', info: 'green'};
const icons = {critical: '🔴', warning: '🟡', info: '🟢'};
const c = colors[data.severity] || 'gray';
const icon = icons[data.severity] || '⚪';
const div = document.createElement('div');
div.className = 'border-l-4 border-' + c + '-400 bg-' + c + '-50 px-4 py-2 rounded-r text-sm';
div.innerHTML = '<span class="font-medium">' + icon + ' ' + data.severity + '</span><p class="mt-1 text-gray-700">' + data.content + '</p>';
currentFileEl.querySelector('.space-y-2').appendChild(div);
}
if (eventType === 'file_end') currentFileEl = null;
if (eventType === 'summary') document.getElementById('review-summary').innerHTML = data.content;
} catch {}
}
}
}
} catch (err) {
document.getElementById('review-summary').textContent = '错误: ' + err.message;
} finally {
btn.disabled = false; btn.textContent = '开始审查';
}
}
</script>
</body>
</html>
+96
View File
@@ -0,0 +1,96 @@
<!DOCTYPE html>
<html lang="zh-CN" class="h-full">
{{template "head" .}}
<body class="h-full bg-gray-50 text-gray-900">
<div class="min-h-full flex flex-col">
{{template "nav" .}}
<main class="flex-1">
<div class="max-w-2xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<h1 class="text-2xl font-bold mb-6">设置</h1>
<form id="settings-form" class="space-y-6">
<!-- LLM Settings -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<h2 class="text-lg font-semibold mb-4">LLM 配置</h2>
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">API Endpoint</label>
<input type="text" name="llm.endpoint" value="{{index .Settings "llm.endpoint"}}"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">API Key</label>
<input type="password" name="llm.api_key" value="{{index .Settings "llm.api_key"}}"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500" placeholder="sk-...">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Model</label>
<input type="text" name="llm.model" value="{{index .Settings "llm.model"}}"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
</div>
</div>
</div>
<!-- Review Settings -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<h2 class="text-lg font-semibold mb-4">审查配置</h2>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Top-N 文件数</label>
<p class="text-xs text-gray-400 mb-2">大 diff 时优先分析变更最大的 N 个文件,设为 0 分析全部</p>
<input type="number" name="review.top_n" value="{{index .Settings "review.top_n"}}" min="0"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
</div>
</div>
<!-- Cache Settings -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<h2 class="text-lg font-semibold mb-4">缓存配置</h2>
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">缓存过期天数</label>
<input type="number" name="cache.max_age_days" value="{{index .Settings "cache.max_age_days"}}" min="1"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">最大缓存大小 (MB)</label>
<input type="number" name="cache.max_size_mb" value="{{index .Settings "cache.max_size_mb"}}" min="100"
class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500">
</div>
</div>
</div>
<!-- Submit -->
<div class="flex items-center gap-3">
<button type="submit"
class="bg-indigo-600 text-white px-5 py-2 rounded-md text-sm font-medium hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">
保存设置
</button>
<span id="save-status" class="text-sm hidden"></span>
</div>
</form>
</div>
</main>
{{template "footer" .}}
</div>
<script>
document.getElementById('settings-form').addEventListener('submit', async function(e) {
e.preventDefault();
const form = new FormData(this);
const data = {};
for (const [key, val] of form.entries()) data[key] = val;
const status = document.getElementById('save-status');
try {
const resp = await fetch('/api/settings', {
method: 'PUT',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
});
if (!resp.ok) throw new Error('保存失败');
status.textContent = '✓ 已保存';
status.className = 'text-sm text-green-600';
} catch (err) {
status.textContent = '✗ ' + err.message;
status.className = 'text-sm text-red-600';
}
status.classList.remove('hidden');
setTimeout(() => status.classList.add('hidden'), 3000);
});
</script>
</body>
</html>