Commit dac02d4d by liimin

feat(scripts): 批次 2+ 跳过前导步骤 + 更新 file-budget 基线

- 新增 --skip-preamble 标志,批次 2+ 跳过环境预检/Context 收集/Messages 构建/项目决策
- 从首批复制 messages.json 和 preview-project.json 到后续批次
- 为 AI 管线脚本添加 file-budget hotspots (ai-direct-execute/server/messages)
1 parent cfc01f07
......@@ -19,6 +19,7 @@ function parseCliArgs() {
pageTitle: "",
outputDir: "",
withoutVariables: false,
appendMode: false,
};
for (let index = 0; index < args.length; index++) {
......@@ -45,6 +46,8 @@ function parseCliArgs() {
index++;
} else if (arg === "--without-variables") {
parsed.withoutVariables = true;
} else if (arg === "--append-mode") {
parsed.appendMode = true;
}
}
......@@ -170,7 +173,80 @@ function readAiGenerationRules() {
return "";
}
function buildSystemPrompt(bundle) {
function readExistingViewsStructure(projectDir) {
if (!projectDir) return "";
const viewsDir = path.join(projectDir, "src", "views");
if (!fs.existsSync(viewsDir)) return "";
const views = [];
const walkDir = (dir, prefix = "") => {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory()) {
walkDir(path.join(dir, entry.name), `${prefix}${entry.name}/`);
} else if (entry.name.endsWith(".vue") || entry.name.endsWith(".tsx")) {
const relativePath = `${prefix}${entry.name}`;
const fullPath = path.join(dir, entry.name);
const content = fs.readFileSync(fullPath, "utf8");
const lines = content.split("\n").length;
// 提取组件导入信息
const imports = [];
const importRegex = /import\s+(\w+|\{[^}]+\})\s+from\s+['"]([^'"]+)['"]/g;
let match;
while ((match = importRegex.exec(content)) !== null) {
imports.push({
name: match[1],
source: match[2],
});
}
views.push({
path: `src/views/${relativePath}`,
language: entry.name.endsWith(".vue") ? "vue" : "tsx",
lines,
imports: imports.slice(0, 10), // 限制数量避免过长
});
}
}
};
walkDir(viewsDir);
if (views.length === 0) return "";
// 构建已有页面结构描述
const structure = [
"## 已有页面结构(追加模式)",
`当前项目已包含 ${views.length} 个页面组件:`,
"",
];
for (const view of views) {
structure.push(`### ${view.path} (${view.language}, ${view.lines} 行)`);
if (view.imports.length > 0) {
const localImports = view.imports.filter((imp) => imp.source.startsWith("@/") || imp.source.startsWith("./") || imp.source.startsWith("../"));
if (localImports.length > 0) {
structure.push(`- 本地依赖: ${localImports.map((imp) => `${imp.name} from ${imp.source}`).join(", ")}`);
}
}
structure.push("");
}
structure.push(
"### 追加模式约束",
"1. 新生成的页面**不要**与上述已有页面使用相同的文件名(除非你明确要覆盖它)",
"2. 新生成的组件**不要**与上述页面中已导入的组件使用相同的名称",
"3. 如果你生成的是子模块(如 HeaderSection.vue),确保文件名具有描述性且不与已有文件冲突",
"4. 路由文件(src/router.ts)由管线自动管理,**不要**输出路由文件",
""
);
return structure.join("\n");
}
function buildSystemPrompt(bundle, appendContext = "") {
const preferredVui = bundle.rules.preferredVuiPackage || "VUI";
const fallbackChain = (bundle.rules.fallbackPackages || []).join(" -> ") || "none";
const previewPolicy = bundle.rules.previewProjectPolicy || {};
......@@ -179,7 +255,7 @@ function buildSystemPrompt(bundle) {
const validationGoals = bundle.rules.validationGoals || [];
const aiRulesDoc = readAiGenerationRules();
return [
const parts = [
"你是 Venus AI 页面生成代理。",
"你的任务是根据 Figma 原始设计上下文、项目事实源和组件规则,直接生成可落地的页面代码。",
"",
......@@ -198,6 +274,14 @@ function buildSystemPrompt(bundle) {
"## 最终裁决目标",
...validationGoals.map((item, index) => `${index + 1}. ${item}`),
"",
];
// 追加模式:注入已有页面结构
if (appendContext) {
parts.push(appendContext, "");
}
parts.push(
"---",
"",
"以下是仓库级 AI 生成规则文档,你生成的每一行代码都会被这些规则校验。",
......@@ -225,8 +309,10 @@ function buildSystemPrompt(bundle) {
" ],",
' "notes": ["string"],',
' "validationChecklist": ["string"]',
"}",
].join("\n");
"}"
);
return parts.join("\n");
}
function buildUserPrompt({ bundle, promptText, designContext, variableDefsText }) {
......@@ -234,7 +320,11 @@ function buildUserPrompt({ bundle, promptText, designContext, variableDefsText }
const compactDesignContext = buildPromptDesignContext(bundle, designContext);
const primaryFacts = extractPrimaryFigmaFacts(designContext);
return [
// 检测多模块模式:合并的设计上下文包含 "### === Module:" 标记
const moduleMarkers = designContext.match(/### === Module:/g) || [];
const isMultiModule = moduleMarkers.length > 1;
const sections = [
"# Task Context",
promptText.trim(),
"",
......@@ -250,8 +340,61 @@ function buildUserPrompt({ bundle, promptText, designContext, variableDefsText }
"# Variable Definitions",
variableDefsText || "{}",
"",
];
// 多模块模式:明确告知 AI 生成策略
if (isMultiModule) {
sections.push(
"# ⚠ 多区块页面生成模式(重要)",
"",
`当前 Figma Design Context 包含 **${moduleMarkers.length} 个页面区块**,每个区块以 \`### === Module: <URL> ===\` 标记分隔。`,
"",
"这些区块是**同一个页面的不同部分**(如导航栏、图片轮播、商品信息、价格区域等),不是独立的页面。",
"",
"**你必须:**",
"1. 为**每个区块**生成一个 Vue 子组件(如 `NavBarSection.vue`、`ImageGallerySection.vue`)",
"2. 子组件文件名使用 PascalCase,放在 `src/components/` 目录下",
"3. 生成一个**父页面组件** `src/views/DetailPage.vue`,导入并组合所有子组件",
"4. 父页面按设计稿的视觉顺序排列各区块",
"5. **不要**只生成第一个区块而忽略其他区块",
"6. 子组件只包含对应区块的 UI 和逻辑,父页面负责布局和组合",
"",
"示例输出结构:",
"```",
"files: [",
' { "path": "src/components/NavBarSection.vue", ... },',
' { "path": "src/components/ImageGallerySection.vue", ... },',
' { "path": "src/components/CarInfoSection.vue", ... },',
' { "path": "src/components/PriceSection.vue", ... },',
' { "path": "src/components/CarDetailsSection.vue", ... },',
' { "path": "src/views/DetailPage.vue", ... }',
"]",
"```",
"",
"父页面示例:",
"```vue",
"<script setup lang=\"ts\">",
"import NavBarSection from \"@/components/NavBarSection.vue\";",
"import ImageGallerySection from \"@/components/ImageGallerySection.vue\";",
"// ... 导入其他子组件",
"</script>",
"<template>",
" <div class=\"detail-page\">",
" <NavBarSection />",
" <ImageGallerySection />",
" <!-- 按设计稿顺序排列 -->",
" </div>",
"</template>",
"```",
"",
);
}
sections.push(
"# Instructions",
"- 基于以上全部输入生成页面产物。",
isMultiModule
? "- 基于以上**所有区块**的 Figma 设计上下文生成子组件和父页面,确保每个区块都有对应的组件文件输出。"
: "- 基于以上全部输入生成页面产物。",
"- 如果需要新增 @vui/* 依赖,必须在 dependencyPlan 中明确说明。",
"- files 内的路径必须相对目标项目根目录。",
"- 如果生成的是预览项目页面,文件路径应匹配 Venus CLI 创建出的项目结构。",
......@@ -272,7 +415,9 @@ function buildUserPrompt({ bundle, promptText, designContext, variableDefsText }
"- 如果设计稿里没有某些装饰信息,不要自己脑补 banner、渐变块、统计卡或解释性文案。",
"- 传给表格组件的 columns 类型必须与目标组件兼容,尤其 `align` 只能使用合法联合字面量,而不是宽泛字符串。",
"- 禁止输出路由文件(src/router.ts 或 src/router/index.ts)。模板已提供路由文件,管线会自动扫描 views 目录并注入路由。你只需生成页面组件。",
].join("\n");
);
return sections.join("\n");
}
function collectDesignContextTexts(designContext) {
......@@ -752,6 +897,7 @@ async function run() {
console.log("[ai-direct-messages] 准备 AI 直投消息...");
console.log(` → bundle: ${bundlePath}`);
console.log(` → appendMode: ${args.appendMode}`);
ensureBundle(args, bundlePath);
const bundle = readJson(bundlePath);
......@@ -784,10 +930,16 @@ async function run() {
}
}
// 追加模式:读取已有页面结构
const appendContext = args.appendMode && args.projectDir ? readExistingViewsStructure(args.projectDir) : "";
if (appendContext) {
console.log("[ai-direct-messages] 已注入已有页面结构到 prompt");
}
const messages = [
{
role: "system",
content: buildSystemPrompt(bundle),
content: buildSystemPrompt(bundle, appendContext),
},
{
role: "user",
......
......@@ -8,6 +8,9 @@
".vue": 700
},
"hotspots": {
"scripts/ai-direct-execute.mjs": 1520,
"scripts/ai-direct-server.mjs": 1001,
"scripts/ai-direct-messages.mjs": 982,
"apps/uniappX/uniapp-x-tape/devTools/page/components/main.vue": 1668,
"apps/uniappX/uniapp-x-shop/uni_modules/cool-unix/cool/animation/index.ts": 1566,
"apps/web/venus-agentic/venus-agentic-base/src/views/Home.vue": 1125,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!