index.html
8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>CodeRio 编排服务</title>
<style>
:root { --bg:#0f172a; --card:#1e293b; --fg:#e2e8f0; --muted:#94a3b8; --accent:#38bdf8; --ok:#22c55e; --err:#ef4444; }
* { box-sizing: border-box; }
body { margin:0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif; background:var(--bg); color:var(--fg); }
header { padding:16px 24px; border-bottom:1px solid #334155; display:flex; align-items:center; gap:12px; }
header h1 { font-size:18px; margin:0; }
.badge { font-size:12px; padding:2px 8px; border-radius:999px; background:#334155; color:var(--muted); }
main { display:grid; grid-template-columns: 360px 1fr; gap:16px; padding:16px 24px; }
@media (max-width: 900px){ main{ grid-template-columns:1fr; } }
.card { background:var(--card); border:1px solid #334155; border-radius:12px; padding:16px; }
.card h2 { font-size:14px; margin:0 0 12px; color:var(--accent); }
label { display:block; font-size:12px; color:var(--muted); margin:10px 0 4px; }
input, select { width:100%; padding:8px 10px; border-radius:8px; border:1px solid #475569; background:#0f172a; color:var(--fg); font-size:13px; }
button { margin-top:12px; width:100%; padding:10px; border:0; border-radius:8px; background:var(--accent); color:#06283d; font-weight:600; cursor:pointer; }
button.ghost { background:#334155; color:var(--fg); }
button.danger { background:var(--err); color:#fff; }
.row { display:flex; gap:8px; }
.row > * { flex:1; }
#logs { background:#0b1120; border:1px solid #334155; border-radius:8px; height:360px; overflow:auto; padding:10px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size:12px; white-space:pre-wrap; }
.log-line { padding:1px 0; border-bottom:1px solid #1e293b; }
.log-info { color:#7dd3fc; } .log-error { color:#fca5a5; } .log-success { color:#86efac; } .log-warn { color:#fcd34d; }
.jobs { margin-top:10px; }
.job { border:1px solid #334155; border-radius:8px; padding:8px 10px; margin-bottom:8px; font-size:12px; }
.job .id { color:var(--muted); font-size:11px; }
.status { display:inline-block; padding:1px 8px; border-radius:999px; font-size:11px; }
.status.queued { background:#475569; } .status.running { background:#0ea5e9; } .status.done { background:#16a34a; } .status.error { background:#dc2626; }
#previewFrame { width:100%; height:420px; border:1px solid #334155; border-radius:8px; background:#fff; margin-top:8px; }
.hint { font-size:11px; color:var(--muted); margin-top:6px; }
a { color:var(--accent); }
</style>
</head>
<body>
<header>
<h1>🎨 CodeRio 编排服务</h1>
<span class="badge" id="cfgBadge">loading…</span>
</header>
<main>
<div>
<div class="card">
<h2>新建生成任务</h2>
<label>Figma 链接(需含 ?node-id=)</label>
<input id="figmaUrl" placeholder="https://www.figma.com/design/xxx?node-id=1-2" />
<label>模式</label>
<select id="mode">
<option value="full">完整(生成 + 校验)</option>
<option value="code">仅代码</option>
<option value="with-report">仅报告</option>
</select>
<label>输出目录(可选,默认服务端 data/output)</label>
<input id="outputDir" placeholder="留空使用服务端默认" />
<label>应用名(可选,默认 my-app)</label>
<input id="appName" placeholder="my-app" />
<button id="generateBtn">开始生成</button>
<div class="hint">密钥由服务端统一持有,前端不接触任何 apiKey / token。</div>
</div>
<div class="card" style="margin-top:16px;">
<h2>任务历史</h2>
<div class="jobs" id="jobs"></div>
</div>
<div class="card" style="margin-top:16px;">
<h2>预览</h2>
<div class="row">
<button id="previewBtn">启动预览</button>
<button id="stopPreviewBtn" class="danger">停止预览</button>
</div>
<div class="hint" id="previewHint">生成完成后可启动预览(dev server)</div>
<iframe id="previewFrame" style="display:none;"></iframe>
</div>
</div>
<div class="card">
<h2>实时日志 (SSE)</h2>
<div id="logs"></div>
</div>
</main>
<script>
const $ = (id) => document.getElementById(id);
const logsEl = $('logs');
function addLog(level, msg) {
const line = document.createElement('div');
line.className = 'log-line log-' + (level || 'info');
const t = new Date().toLocaleTimeString();
line.textContent = `[${t}] ${msg}`;
logsEl.appendChild(line);
logsEl.scrollTop = logsEl.scrollHeight;
}
// ---- config ----
fetch('/api/config').then(r => r.json()).then(c => {
$('cfgBadge').textContent = `${c.provider} / ${c.model} · 并发${c.maxConcurrent}`;
$('cfgBadge').title = `baseUrl: ${c.baseUrl}\napiKey: ${c.apiKeySet ? '已配置' : '未配置'}\nfigmaToken: ${c.figmaTokenSet ? '已配置' : '未配置'}`;
}).catch(e => addLog('error', 'config 加载失败: ' + e));
// ---- SSE ----
const es = new EventSource('/api/stream');
es.addEventListener('log', (e) => { try { const d = JSON.parse(e.data); addLog(d.level, d.message); } catch {} });
es.addEventListener('jobs', (e) => { try { renderJobs(JSON.parse(e.data).jobs); } catch {} });
es.addEventListener('preview', (e) => { try { renderPreview(JSON.parse(e.data)); } catch {} });
es.onerror = () => addLog('warn', 'SSE 连接中断,重连中…');
function renderJobs(jobs) {
const el = $('jobs');
el.innerHTML = '';
if (!jobs.length) { el.innerHTML = '<div class="hint">暂无任务</div>'; return; }
for (const j of jobs) {
const d = document.createElement('div');
d.className = 'job';
d.innerHTML = `<div class="id">${j.id}</div>
<span class="status ${j.status}">${j.status}</span>
<span style="margin-left:6px;">${j.mode}</span>
${j.error ? `<div style="color:#fca5a5;margin-top:4px;">${j.error}</div>` : ''}`;
el.appendChild(d);
}
}
function renderPreview(state) {
const hint = $('previewHint');
const frame = $('previewFrame');
if (state.running && state.url) {
hint.textContent = `预览运行中: ${state.url}`;
frame.style.display = 'block';
if (frame.src !== state.url) frame.src = state.url;
} else if (state.error) {
hint.textContent = '预览错误: ' + state.error;
frame.style.display = 'none';
} else {
hint.textContent = '生成完成后可启动预览(dev server)';
frame.style.display = 'none';
}
}
// ---- generate ----
$('generateBtn').onclick = async () => {
const figmaUrl = $('figmaUrl').value.trim();
if (!figmaUrl) { alert('请填写 Figma 链接'); return; }
const body = {
figmaUrl,
mode: $('mode').value,
outputDir: $('outputDir').value.trim() || undefined,
appName: $('appName').value.trim() || undefined,
};
addLog('info', '提交生成任务…');
const r = await fetch('/api/generate', { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify(body) });
const data = await r.json();
addLog(data.error ? 'error' : 'success', data.error || (data.message + ' (job ' + data.jobId + ')'));
};
// ---- preview ----
$('previewBtn').onclick = async () => {
const r = await fetch('/api/preview', { method:'POST', headers:{'Content-Type':'application/json'}, body: '{}' });
const d = await r.json();
if (!d.ok) addLog('error', '预览启动失败: ' + (d.error || ''));
else addLog('success', '预览启动中…');
};
$('stopPreviewBtn').onclick = async () => {
await fetch('/api/preview/stop', { method:'POST' });
addLog('info', '已请求停止预览');
};
</script>
</body>
</html>