|
| 1 | +import process from 'node:process' |
| 2 | +import fs from 'fs-extra' |
| 3 | +import path from 'pathe' |
| 4 | +import { logger } from '../core/logger' |
| 5 | + |
| 6 | +export type AgenticTemplateFormat = 'md' | 'json' |
| 7 | + |
| 8 | +export interface GenerateAgenticTemplateOptions { |
| 9 | + cwd?: string |
| 10 | + output?: string |
| 11 | + force?: boolean |
| 12 | + format?: AgenticTemplateFormat |
| 13 | + /** |
| 14 | + * 任务名,用于快速生成带目录与后缀的文件。 |
| 15 | + */ |
| 16 | + name?: string |
| 17 | + /** |
| 18 | + * 基础目录,配合 name 使用。 |
| 19 | + * @default 'agentic' |
| 20 | + */ |
| 21 | + baseDir?: string |
| 22 | +} |
| 23 | + |
| 24 | +export type AgenticTemplateTask = string | (Omit<GenerateAgenticTemplateOptions, 'cwd'> & { name?: string }) |
| 25 | + |
| 26 | +const agenticSections = [ |
| 27 | + '目标/产物', |
| 28 | + '约束(性能/风格/兼容/不可改动范围)', |
| 29 | + '验收标准(要跑的命令、预期输出/文件)', |
| 30 | + '仓库路径', |
| 31 | + '允许操作(可/不可写文件,可运行的命令清单,可否联网)', |
| 32 | + '上下文线索(日志/文件/模块/相关 issue)', |
| 33 | + '里程碑(根因→设计→实现→验证)', |
| 34 | +] as const |
| 35 | + |
| 36 | +function renderMarkdownTemplate() { |
| 37 | + return `${agenticSections.map(title => `## ${title}\n- `).join('\n\n')}\n` |
| 38 | +} |
| 39 | + |
| 40 | +function renderJsonTemplate() { |
| 41 | + const payload: Record<string, string> = {} |
| 42 | + for (const title of agenticSections) { |
| 43 | + payload[title] = '' |
| 44 | + } |
| 45 | + return `${JSON.stringify(payload, null, 2)}\n` |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * 生成 Agentic 任务提示词模板,支持输出到 stdout 或文件。 |
| 50 | + */ |
| 51 | +export async function generateAgenticTemplate(options: GenerateAgenticTemplateOptions = {}) { |
| 52 | + const cwd = options.cwd ?? process.cwd() |
| 53 | + const format = options.format ?? 'md' |
| 54 | + const baseDir = options.baseDir ?? 'agentic' |
| 55 | + |
| 56 | + if (format !== 'md' && format !== 'json') { |
| 57 | + throw new Error(`不支持的模板格式:${format}`) |
| 58 | + } |
| 59 | + |
| 60 | + const template = format === 'md' |
| 61 | + ? renderMarkdownTemplate() |
| 62 | + : renderJsonTemplate() |
| 63 | + |
| 64 | + const ext = format === 'json' ? 'json' : 'md' |
| 65 | + let outputPath = options.output |
| 66 | + if (!outputPath && options.name) { |
| 67 | + outputPath = path.join(baseDir, `${options.name}.${ext}`) |
| 68 | + } |
| 69 | + |
| 70 | + if (!outputPath) { |
| 71 | + process.stdout.write(template) |
| 72 | + return template |
| 73 | + } |
| 74 | + |
| 75 | + const targetPath = path.resolve(cwd, outputPath) |
| 76 | + const targetDir = path.dirname(targetPath) |
| 77 | + await fs.ensureDir(targetDir) |
| 78 | + const exists = await fs.pathExists(targetPath) |
| 79 | + |
| 80 | + if (exists && !options.force) { |
| 81 | + throw new Error(`目标文件已存在:${path.relative(cwd, targetPath)}`) |
| 82 | + } |
| 83 | + |
| 84 | + await fs.outputFile(targetPath, template, 'utf8') |
| 85 | + const actionLabel = exists ? '已覆盖模板' : '已生成模板' |
| 86 | + logger.success(`${actionLabel}:${path.relative(cwd, targetPath)}`) |
| 87 | + |
| 88 | + return template |
| 89 | +} |
| 90 | + |
| 91 | +export async function loadAgenticTasks(filePath: string, cwd: string) { |
| 92 | + const fullPath = path.resolve(cwd, filePath) |
| 93 | + const tasks = await fs.readJson(fullPath) |
| 94 | + if (!Array.isArray(tasks)) { |
| 95 | + throw new TypeError('任务清单需要是数组') |
| 96 | + } |
| 97 | + return tasks as AgenticTemplateTask[] |
| 98 | +} |
| 99 | + |
| 100 | +export async function generateAgenticTemplates(tasks: AgenticTemplateTask[], defaults: GenerateAgenticTemplateOptions = {}) { |
| 101 | + const results: string[] = [] |
| 102 | + for (const task of tasks) { |
| 103 | + const normalized = typeof task === 'string' |
| 104 | + ? { ...defaults, name: task } |
| 105 | + : { ...defaults, ...task } |
| 106 | + results.push(await generateAgenticTemplate(normalized)) |
| 107 | + } |
| 108 | + return results |
| 109 | +} |
0 commit comments