2026-03-11 10:57:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* persona-studio · 开发任务路由
|
2026-03-13 13:56:33 +08:00
|
|
|
|
* POST /api/ps/build/start 触发铸渊代理开发工作流
|
|
|
|
|
|
*
|
|
|
|
|
|
* 当用户点击「我要开发」时,自动触发铸渊代理(Agent)
|
|
|
|
|
|
* 使用用户自带的 API Key 进行 5 步自动化开发。
|
2026-03-11 10:57:23 +08:00
|
|
|
|
*/
|
|
|
|
|
|
const express = require('express');
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
const memoryManager = require('../brain/memory-manager');
|
2026-03-13 13:56:33 +08:00
|
|
|
|
const agentWorkflow = require('../brain/agent-workflow');
|
2026-03-11 10:57:23 +08:00
|
|
|
|
const emailSender = require('../utils/email-sender');
|
|
|
|
|
|
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
// 邮箱后端正则二次校验
|
|
|
|
|
|
const EMAIL_RE = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
|
|
|
|
|
2026-03-13 13:56:33 +08:00
|
|
|
|
// 支持 EXP-XXX 和 GUEST 两种 dev_id 格式
|
|
|
|
|
|
function isValidDevId(devId) {
|
|
|
|
|
|
return devId === 'GUEST' || /^EXP-\d{3,}$/.test(devId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-11 10:57:23 +08:00
|
|
|
|
// POST /api/ps/build/start
|
|
|
|
|
|
router.post('/start', async (req, res) => {
|
2026-03-13 13:56:33 +08:00
|
|
|
|
const { dev_id, email, contact, conversation, api_base, api_key, model } = req.body || {};
|
2026-03-11 10:57:23 +08:00
|
|
|
|
|
2026-03-13 13:56:33 +08:00
|
|
|
|
if (!dev_id || !isValidDevId(dev_id)) {
|
2026-03-11 10:57:23 +08:00
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
code: 'INVALID_ID',
|
|
|
|
|
|
message: '无效的开发编号'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!email) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
code: 'MISSING_EMAIL',
|
|
|
|
|
|
message: '请提供邮箱地址'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
// 后端二次邮箱校验
|
|
|
|
|
|
if (!EMAIL_RE.test(email)) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
code: 'INVALID_EMAIL',
|
|
|
|
|
|
message: '邮箱格式不正确'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-13 13:56:33 +08:00
|
|
|
|
// 防止 header injection:API Key 不得包含换行符
|
|
|
|
|
|
if (api_key && /[\r\n]/.test(api_key)) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
code: 'INVALID_API_KEY',
|
|
|
|
|
|
message: 'API Key 格式无效'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-11 19:50:12 +08:00
|
|
|
|
// 联系方式输入清理(去除潜在 HTML/script 注入)
|
|
|
|
|
|
const safeContact = contact ? String(contact).replace(/[<>&"']/g, '').substring(0, 100) : null;
|
|
|
|
|
|
|
2026-03-11 10:57:23 +08:00
|
|
|
|
// 先立即响应,后台异步处理
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
error: false,
|
2026-03-13 13:56:33 +08:00
|
|
|
|
message: '铸渊代理已启动,完成后将发送到 ' + email,
|
|
|
|
|
|
status: 'agent_started'
|
2026-03-11 10:57:23 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-13 13:56:33 +08:00
|
|
|
|
// 异步执行铸渊代理开发工作流
|
2026-03-11 10:57:23 +08:00
|
|
|
|
(async () => {
|
2026-03-13 13:56:33 +08:00
|
|
|
|
const broadcastRaw = req.app.locals.broadcastToClient || function () {};
|
|
|
|
|
|
const broadcast = function (data) { broadcastRaw(dev_id, data); };
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
|
2026-03-11 10:57:23 +08:00
|
|
|
|
try {
|
2026-03-13 13:56:33 +08:00
|
|
|
|
// 启动 Agent 工作流
|
|
|
|
|
|
broadcast({
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
type: 'progress',
|
2026-03-13 13:56:33 +08:00
|
|
|
|
message: '🌀 铸渊代理已唤醒,正在启动开发工作流...',
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
status: 'building',
|
2026-03-13 13:56:33 +08:00
|
|
|
|
status_text: '代理已启动'
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-13 13:56:33 +08:00
|
|
|
|
const result = await agentWorkflow.runWorkflow({
|
|
|
|
|
|
dev_id: dev_id,
|
2026-03-11 10:57:23 +08:00
|
|
|
|
conversation: conversation || [],
|
2026-03-13 13:56:33 +08:00
|
|
|
|
api_base: api_base || '',
|
|
|
|
|
|
api_key: api_key || '',
|
|
|
|
|
|
model: model || '',
|
|
|
|
|
|
broadcast: broadcast
|
2026-03-11 10:57:23 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-13 13:56:33 +08:00
|
|
|
|
// 记录项目(GUEST 也记录,方便追踪)
|
|
|
|
|
|
try {
|
|
|
|
|
|
memoryManager.addProject(dev_id, {
|
|
|
|
|
|
name: result.projectName || 'untitled',
|
|
|
|
|
|
email: email,
|
|
|
|
|
|
contact: safeContact,
|
|
|
|
|
|
status: 'completed',
|
|
|
|
|
|
created_at: new Date().toISOString(),
|
|
|
|
|
|
files: result.files || []
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 自动提取知识(仅注册开发者)
|
|
|
|
|
|
if (dev_id !== 'GUEST') {
|
|
|
|
|
|
memoryManager.autoExtractKnowledge(dev_id, conversation, result.projectName);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (memErr) {
|
|
|
|
|
|
console.error('Memory save error (non-fatal):', memErr.message);
|
|
|
|
|
|
}
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
|
|
|
|
|
|
// 通知预览就绪
|
2026-03-13 13:56:33 +08:00
|
|
|
|
broadcast({
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
type: 'preview_ready',
|
|
|
|
|
|
project: result.projectName,
|
|
|
|
|
|
message: '✅ 预览已就绪'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-13 13:56:33 +08:00
|
|
|
|
broadcast({
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
type: 'complete',
|
|
|
|
|
|
message: '🎉 全部完成!邮件正在发送'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-13 13:56:33 +08:00
|
|
|
|
// 发邮件(异步,不阻塞主流程)
|
|
|
|
|
|
emailSender.sendCompletion({
|
2026-03-11 10:57:23 +08:00
|
|
|
|
to: email,
|
2026-03-13 13:56:33 +08:00
|
|
|
|
dev_id: dev_id,
|
2026-03-11 10:57:23 +08:00
|
|
|
|
projectName: result.projectName,
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
summary: result.summary,
|
|
|
|
|
|
files: result.files
|
2026-03-13 13:56:33 +08:00
|
|
|
|
}).catch(function (emailErr) {
|
|
|
|
|
|
console.error('Email send error (non-fatal):', emailErr.message);
|
2026-03-11 10:57:23 +08:00
|
|
|
|
});
|
|
|
|
|
|
} catch (err) {
|
2026-03-13 13:56:33 +08:00
|
|
|
|
console.error('Agent workflow error:', err.message);
|
|
|
|
|
|
broadcast({
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
type: 'error',
|
2026-03-13 13:56:33 +08:00
|
|
|
|
message: '铸渊代理遇到问题: ' + err.message
|
feat: implement split-screen layout, enhanced email modal, preview panel, and WebSocket
- chat.html: Enhanced modal with email+contact fields, split-screen HTML structure,
resizer divider, preview panel with status bar and iframe
- chat.js: Email validation (frontend regex), email prefill on repeat visits,
split-screen enterDevMode(), draggable resizer with touch support,
preview panel status updates, WebSocket connection for build progress
- style.css: Split-screen dev-mode layout, resizer styles, preview panel styles
with status indicators (waiting/building/done/error), modal field styles
- backend: Preview API route, WebSocket server for progress push,
enhanced build route with email validation and broadcast,
enhanced email template with file list and brand signature
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
2026-03-11 19:47:50 +08:00
|
|
|
|
});
|
2026-03-11 10:57:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
})();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|