index.html 20.9 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
<!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: #f7f8fa;
                --panel: #ffffff;
                --border: #e6e8eb;
                --text: #1f2329;
                --muted: #8a9099;
                --primary: #534ab7;
                --primary-soft: #eeeefe;
                --green: #1d9e75;
                --red: #e24b4a;
                --amber: #ba7517;
                --radius: 12px;
            }
            * {
                box-sizing: border-box;
            }
            body {
                margin: 0;
                font-family: -apple-system, blinkmacsystemfont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
                background: var(--bg);
                color: var(--text);
                font-size: 14px;
                line-height: 1.6;
            }
            header {
                padding: 20px 28px;
                background: var(--panel);
                border-bottom: 1px solid var(--border);
                display: flex;
                align-items: center;
                justify-content: space-between;
            }
            header h1 {
                font-size: 18px;
                margin: 0;
                font-weight: 600;
            }
            header .sub {
                color: var(--muted);
                font-size: 12px;
                margin-top: 2px;
            }
            .badge {
                font-size: 12px;
                padding: 4px 10px;
                border-radius: 999px;
                background: var(--primary-soft);
                color: var(--primary);
                font-weight: 500;
            }
            .wrap {
                max-width: 980px;
                margin: 24px auto;
                padding: 0 20px;
                display: grid;
                grid-template-columns: 1fr 1fr;
                gap: 20px;
            }
            @media (max-width: 760px) {
                .wrap {
                    grid-template-columns: 1fr;
                }
            }
            .card {
                background: var(--panel);
                border: 1px solid var(--border);
                border-radius: var(--radius);
                padding: 20px;
            }
            .card h2 {
                font-size: 15px;
                margin: 0 0 14px;
                font-weight: 600;
            }
            label {
                display: block;
                font-size: 12px;
                color: var(--muted);
                margin: 12px 0 6px;
            }
            input,
            select {
                width: 100%;
                padding: 9px 11px;
                border: 1px solid var(--border);
                border-radius: 8px;
                font-size: 13px;
                background: #fff;
                color: var(--text);
            }
            input:focus,
            select:focus {
                outline: none;
                border-color: var(--primary);
            }
            .hint {
                font-size: 11px;
                color: var(--muted);
                margin-top: 4px;
            }
            button {
                margin-top: 16px;
                width: 100%;
                padding: 10px 14px;
                border: none;
                border-radius: 8px;
                background: var(--primary);
                color: #fff;
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
            }
            button.secondary {
                background: var(--primary-soft);
                color: var(--primary);
            }
            button:disabled {
                opacity: 0.5;
                cursor: not-allowed;
            }
            .row {
                display: flex;
                gap: 10px;
            }
            .row > * {
                flex: 1;
            }
            .logwrap {
                margin: 0 20px 28px;
                max-width: 980px;
                margin-left: auto;
                margin-right: auto;
            }
            .loghead {
                display: flex;
                align-items: center;
                justify-content: space-between;
                margin-bottom: 10px;
            }
            .status {
                font-size: 12px;
                padding: 4px 10px;
                border-radius: 999px;
                background: #f1efea;
                color: var(--muted);
            }
            .status.idle {
                background: #f1efea;
            }
            .status.running {
                background: #e6f1fb;
                color: #185fa5;
            }
            .status.done {
                background: #eaf3de;
                color: var(--green);
            }
            .status.error {
                background: #fcebeb;
                color: var(--red);
            }
            #log {
                background: #0f1115;
                color: #d7dbe0;
                border-radius: var(--radius);
                padding: 14px 16px;
                height: 320px;
                overflow-y: auto;
                font-family: 'SFMono-Regular', consolas, 'Liberation Mono', monospace;
                font-size: 12px;
                line-height: 1.55;
                white-space: pre-wrap;
                word-break: break-word;
            }
            #log .info {
                color: #8ab4f8;
            }
            #log .warn {
                color: #f5c777;
            }
            #log .error {
                color: #ff8a8a;
            }
            #log .success {
                color: #7fdca0;
            }
            .result {
                margin-top: 14px;
                padding: 12px 14px;
                border: 1px dashed var(--border);
                border-radius: 8px;
                font-size: 13px;
                display: none;
            }
            .result.show {
                display: block;
            }
            .result code {
                background: var(--primary-soft);
                padding: 2px 6px;
                border-radius: 5px;
                color: var(--primary);
            }
            .copy {
                margin-left: 8px;
                font-size: 11px;
                color: var(--primary);
                cursor: pointer;
                border: 1px solid var(--border);
                border-radius: 5px;
                padding: 1px 6px;
            }
            .msg {
                font-size: 12px;
                margin-top: 10px;
                min-height: 16px;
            }
            .msg.ok {
                color: var(--green);
            }
            .msg.err {
                color: var(--red);
            }
        </style>
    </head>
    <body>
        <header>
            <div>
                <h1>🎨 CodeRio 服务模式</h1>
                <div class="sub">配置 AI / Figma 参数,一键将设计稿转换为生产级前端代码</div>
            </div>
            <span class="badge" id="conn">连接中…</span>
        </header>

        <div class="wrap">
            <div class="card">
                <h2>① 模型与密钥配置</h2>
                <label>模型服务商</label>
                <select id="provider">
                    <option value="openai" selected>OpenAI 兼容 (Qwen / DeepSeek / 自建)</option>
                    <option value="anthropic">Anthropic</option>
                    <option value="google">Google</option>
                </select>
                <label>模型名称</label>
                <input id="model" placeholder="如 qwen3.8-max(需多模态视觉能力)" />
                <label>API Base URL</label>
                <input id="baseUrl" placeholder="https://...maas.aliyuncs.com/compatible-mode/v1" />
                <label>API Key</label>
                <input id="apiKey" type="password" placeholder="sk-..." />
                <label>Figma Token</label>
                <input id="figmaToken" type="password" placeholder="Figma Personal Access Token" />
                <div class="hint">配置保存在 ~/.coderio/config.yaml,与 CLI 共用。</div>
                <button class="secondary" id="saveConfig">保存配置</button>
                <div class="msg" id="configMsg"></div>
            </div>

            <div class="card">
                <h2>② 生成页面</h2>
                <label>Figma 链接 <span style="color: var(--red)">*</span></label>
                <input id="figmaUrl" placeholder="https://www.figma.com/design/..." />
                <label>执行模式</label>
                <select id="mode">
                    <option value="code">仅生成代码(code)</option>
                    <option value="with-report">生成 + 单次校验报告(with-report)</option>
                    <option value="full" selected>生成 + 迭代校验修复(full)</option>
                </select>
                <div class="row">
                    <div>
                        <label>输出目录</label>
                        <input id="outputDir" placeholder="留空=当前目录/coderio" />
                    </div>
                    <div>
                        <label>应用名</label>
                        <input id="appName" placeholder="my-app" />
                    </div>
                </div>
                <div class="hint">技术栈:React + Vite + TypeScript + Tailwind(当前版本)。</div>
                <button id="generate">🚀 生成页面</button>
                <div class="msg" id="genMsg"></div>
            </div>
        </div>

        <div class="logwrap">
            <div class="card">
                <h2>③ 预览生成结果</h2>
                <div class="hint">
                    启动一个持久化的本地开发服务器(Vite),用于实时查看生成的页面。验证阶段内部临时起的 5837
                    服务会自动关闭,此处的预览服务会一直运行,直到你点「停止预览」。
                </div>
                <div class="row">
                    <button id="previewStart">▶ 启动预览</button>
                    <button class="secondary" id="previewStop" disabled>■ 停止预览</button>
                </div>
                <div class="msg" id="previewMsg"></div>
                <div class="result" id="previewResult"></div>
            </div>
        </div>

        <div class="logwrap">
            <div class="loghead">
                <h2 style="margin: 0; font-size: 15px">实时进度</h2>
                <span class="status idle" id="status">空闲</span>
            </div>
            <div id="log"></div>
            <div class="result" id="result"></div>
        </div>

        <script>
            const $ = (id) => document.getElementById(id);
            const logEl = $('log');
            const statusEl = $('status');

            function setStatus(state, text) {
                statusEl.className = 'status ' + state;
                statusEl.textContent = text;
            }

            function appendLog(entry) {
                const line = document.createElement('div');
                const cls = entry.level === 'log' ? '' : entry.level;
                if (cls) line.className = cls;
                line.textContent = '[' + entry.timestamp.split(' ')[1] + '] ' + entry.message;
                logEl.appendChild(line);
                logEl.scrollTop = logEl.scrollHeight;
            }

            // Load existing config
            async function loadConfig() {
                try {
                    const r = await fetch('/api/config');
                    const c = await r.json();
                    if (c.model) {
                        $('provider').value = c.model.provider || 'qwen';
                        $('model').value = c.model.model || '';
                        $('baseUrl').value = c.model.baseUrl || '';
                        $('apiKey').value = c.model.apiKey || '';
                    }
                    if (c.figma) $('figmaToken').value = c.figma.token || '';
                    $('conn').textContent = '已连接';
                    $('conn').style.color = 'var(--green)';
                } catch (e) {
                    $('conn').textContent = '连接失败';
                    $('conn').style.color = 'var(--red)';
                }
            }

            // Save config
            $('saveConfig').addEventListener('click', async () => {
                const body = {
                    model: {
                        provider: $('provider').value,
                        model: $('model').value.trim(),
                        baseUrl: $('baseUrl').value.trim(),
                        apiKey: $('apiKey').value.trim(),
                    },
                    figma: { token: $('figmaToken').value.trim() },
                };
                const msg = $('configMsg');
                msg.className = 'msg';
                msg.textContent = '保存中…';
                try {
                    const r = await fetch('/api/config', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify(body),
                    });
                    const j = await r.json();
                    if (j.ok) {
                        msg.className = 'msg ok';
                        msg.textContent = '✓ 配置已保存';
                    } else {
                        msg.className = 'msg err';
                        msg.textContent = '✖ ' + (j.error || '保存失败');
                    }
                } catch (e) {
                    msg.className = 'msg err';
                    msg.textContent = '✖ 请求失败';
                }
            });

            // Generate
            $('generate').addEventListener('click', async () => {
                const figmaUrl = $('figmaUrl').value.trim();
                if (!figmaUrl) {
                    $('genMsg').className = 'msg err';
                    $('genMsg').textContent = '✖ 请填写 Figma 链接';
                    return;
                }
                const genMsg = $('genMsg');
                genMsg.className = 'msg';
                genMsg.textContent = '提交任务中…';
                $('result').className = 'result';
                try {
                    const r = await fetch('/api/generate', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({
                            figmaUrl,
                            mode: $('mode').value,
                            outputDir: $('outputDir').value.trim() || undefined,
                            appName: $('appName').value.trim() || undefined,
                        }),
                    });
                    const j = await r.json();
                    if (!r.ok) {
                        genMsg.className = 'msg err';
                        genMsg.textContent = '✖ ' + (j.error || '提交失败');
                        return;
                    }
                    genMsg.className = 'msg ok';
                    genMsg.textContent = '✓ 已启动,任务 ID: ' + j.jobId;
                    $('generate').disabled = true;
                } catch (e) {
                    genMsg.className = 'msg err';
                    genMsg.textContent = '✖ 请求失败';
                }
            });

            function showResult(job) {
                const box = $('result');
                box.className = 'result show';
                if (job.status === 'done') {
                    let html = '✓ 生成完成!<br/>产物目录:<code>' + (job.outputDir || '-') + '</code>';
                    if (job.reportPath) {
                        html += '<br/>校验报告:<code>' + job.reportPath + '</code>';
                    }
                    box.innerHTML = html;
                } else if (job.status === 'error') {
                    box.className = 'result show';
                    box.innerHTML = '✖ 生成失败:' + (job.error || '未知错误');
                }
            }

            // Preview
            function updatePreviewUI(st) {
                const msg = $('previewMsg');
                const box = $('previewResult');
                if (st.running && !st.url && !st.error) {
                    msg.className = 'msg';
                    msg.textContent = '预览服务启动中,等待地址…';
                    $('previewStart').disabled = true;
                    $('previewStop').disabled = false;
                }
                if (st.url) {
                    box.className = 'result show';
                    const local = st.localUrl ? `<code>${st.localUrl}</code>` : '-';
                    const net = st.url
                        ? `<a href="${st.url}" target="_blank" style="color:var(--primary)">${st.url}</a>`
                        : '-';
                    box.innerHTML =
                        '✓ 预览已启动<br/>本地地址:' + local + '<br/>网络地址:' + net;
                    $('previewStop').disabled = false;
                    $('previewStart').disabled = true;
                    msg.className = 'msg ok';
                    msg.textContent = '预览运行中';
                }
                if (st.error) {
                    msg.className = 'msg err';
                    msg.textContent = '✖ ' + st.error;
                    $('previewStart').disabled = false;
                    $('previewStop').disabled = true;
                }
                if (!st.running && !st.url && !st.error) {
                    $('previewStart').disabled = false;
                    $('previewStop').disabled = true;
                    box.className = 'result';
                }
            }

            $('previewStart').addEventListener('click', async () => {
                const msg = $('previewMsg');
                msg.className = 'msg';
                msg.textContent = '正在启动预览服务…';
                $('previewStart').disabled = true;
                try {
                    const r = await fetch('/api/preview', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({}),
                    });
                    const j = await r.json();
                    if (!j.ok) {
                        msg.className = 'msg err';
                        msg.textContent = '✖ ' + (j.error || '启动失败');
                        $('previewStart').disabled = false;
                        return;
                    }
                    msg.textContent = '预览服务启动中,等待地址…';
                } catch (e) {
                    msg.className = 'msg err';
                    msg.textContent = '✖ 请求失败';
                    $('previewStart').disabled = false;
                }
            });

            $('previewStop').addEventListener('click', async () => {
                try {
                    await fetch('/api/preview/stop', { method: 'POST' });
                } catch {
                    /* ignore */
                }
                $('previewStart').disabled = false;
                $('previewStop').disabled = true;
                $('previewResult').className = 'result';
                $('previewMsg').textContent = '已停止预览';
            });

            // SSE stream
            function connectStream() {
                const es = new EventSource('/api/stream');
                es.addEventListener('log', (e) => appendLog(JSON.parse(e.data)));
                es.addEventListener('job', (e) => {
                    const job = JSON.parse(e.data);
                    if (job.status === 'running') {
                        setStatus('running', '生成中');
                        $('generate').disabled = true;
                    } else if (job.status === 'done') {
                        setStatus('done', '完成');
                        $('generate').disabled = false;
                        showResult(job);
                    } else if (job.status === 'error') {
                        setStatus('error', '失败');
                        $('generate').disabled = false;
                        showResult(job);
                    }
                });
                es.addEventListener('preview', (e) => {
                    try {
                        updatePreviewUI(JSON.parse(e.data));
                    } catch {
                        /* ignore */
                    }
                });
                es.onerror = () => {
                    $('conn').textContent = '流断开,重连中…';
                    $('conn').style.color = 'var(--amber)';
                };
            }

            loadConfig();
            connectStream();
        </script>
    </body>
</html>