feat(dacli): v2.1+ 命令输出 + auto-fix helper (含 workflow G-X1 扩展)

src/commands/_output_filter.ts (新): 跨命令输出过滤 + CommandLog 落盘
  - setupOutputFilter: 默认 quiet, --verbose/--json 解除
  - addOutputOptions: 注册 --verbose/--json
  - printCommandEnd: 落 L3 CommandLog + 打印 L1 summary + 'dacli log last' 提示
  - 三档 result: success / partial / failure

src/commands/_auto_fix.ts (改): workflow enCode 也参与 G-X1 BS-47 重名查重
  - CommandFixContext 新增 existingWorkflows 字段
  - fetchExistingLists 加 workflows flag + GET /workflow/template 调用
  - fixUniquenessSuggest 把 workflows 加入 allExisting 冲突检查

tests/unit/output_filter.test.ts (新): 11 个 helper 单元测试

tests/unit/auto_fix.test.ts (新): 23 baseline + 3 本 session workflow G-X1 测试
This commit is contained in:
ssw committed 2026-07-21 13:00:16 +08:00
1 parent 3d4002f6cc
commit 508cabface
3 files changed
+654

No files matched your search

+187
View File
@@ -0,0 +1,187 @@
/**
* commands/_output_filter.ts — 跨命令输出过滤 + CommandLog 落盘 (dacli v2.1+, 2026-07-21)
*
* 取代 commands/workflow/_output.ts, 升级为跨命令通用 helper.
* 复用 dacli form/create.ts 的输出过滤模式 + saveCommandLog 集成 (见 dacli/README.md:213-228):
* - 默认 (quiet=true): info/ok/warn 输出到 buffer, 错误即时输出
* - --verbose: 所有日志即时输出 (含 step machine/API 调用过程)
* - --json: 原始 JSON 输出 (不受 quiet 影响, stdout 走原始数据)
* - 完成后: 落 CommandLog (供 dacli log last 查询) + 打印摘要 + "详情: dacli log last" 提示
*
* 与 workflow/_output.ts 的差异 (迁移要点):
* - printCommandEnd 签名变 (opts, command, summary) → printCommandEnd({ command, appCode, summary, fixes, result, error, duration_ms, verbose, json })
* 旧用法只在 workflow/create.ts:383 一处, shim 文件不转发旧签名, 调用方需更新.
* - 新增 saveCommandLog 集成 (旧版本仅打印 key-value summary, 不落结构化日志)
* - 新增 result='failure' 路径 (旧版本错误路径直接 process.exit(1), 不落任何日志)
* - 删 printCommandEndSimple (dead code, 全 codebase 0 调用方)
*
* 用法 (在 .action() 里):
* import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../_output_filter.ts'
*
* .option('--verbose', ..., false).option('--json', ..., false)
* .action(async (opts) => {
* setupOutputFilter(opts)
* const c = await JnpfClient.fromToken()
* // ... 命令逻辑 ...
* printCommandEnd({
* command: 'workflow create',
* appCode: c.appCode,
* summary: { id, enCode, flowId },
* fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
* duration_ms: Date.now() - startTime,
* verbose: opts.verbose,
* json: opts.json,
* })
* })
*/
import type { Command } from 'commander'
import { log } from '../core/log.ts'
import {
saveCommandLog,
formatOneLineSummary,
type FixEntry,
type CommandLog,
} from '../core/logfile.ts'
export interface OutputOpts {
/** --verbose: 完整输出 (含 step machine、API 调用等过程日志) */
verbose?: boolean
/** --json: 原始 JSON 输出 (不受 verbose 影响, 始终输出 JSON) */
json?: boolean
}
/**
* 在 .action() 开头调用: 设置 quiet 模式
* --verbose 或 --json: 非 quiet (全部输出)
* 默认: quiet (info/ok/warn 走 buffer, 错误即时)
*/
export function setupOutputFilter(opts: OutputOpts): void {
log.setQuiet(!opts.verbose && !opts.json)
}
/**
* 给 Command 加 --verbose + --json 选项 (统一风格)
*/
export function addOutputOptions(cmd: Command): Command {
return cmd
.option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false)
.option('--json', '原始 JSON 输出 (不受 --verbose 影响, 始终输出 JSON)', false)
}
export interface PrintCommandEndArgs {
/** 命令名 (e.g. 'workflow create') */
command: string
/** appCode (有则落 CommandLog, 缺失则跳过 L3 历史查询) */
appCode?: string
/** 结果状态, 默认 'success' */
result?: 'success' | 'failure' | 'partial'
/** stdout 要打印的摘要 (含 id/enCode 等) — json 模式跳过 */
summary?: Record<string, unknown>
/** 命令执行的 fix entries (跨命令 silent/assertive/suggest, 来自 runCommandAutoFix 等) */
fixes?: FixEntry[]
/** 错误信息 (result='failure' 时填这里, stdout + CommandLog 都打) */
error?: string
/** 命令耗时 (毫秒, 用于 CommandLog.duration_ms) */
duration_ms?: number
/** --verbose / --json 透传 */
verbose?: boolean
json?: boolean
}
/**
* 命令结束后调用: 落 CommandLog (L3) + 打印 summary + "详情: dacli log last" 提示 (L1)
*
* @param args 含 command/appCode/result/summary/fixes/error/duration_ms/verbose/json
*
* 行为矩阵:
* - json=true: stdout 跳过 summary + 提示, 但 CommandLog 仍落 (供 dacli log last 查询)
* - verbose=true: log.flush() 立刻输出过程日志 + summary + 提示
* - 默认 quiet: log.discard() 丢弃过程 buffer + stdout 打印 summary + 提示
* - appCode 缺失: 跳过 L3 (仅打印 stdout)
* - result='failure': stdout 额外打印 error (如果有)
*/
export function printCommandEnd(args: PrintCommandEndArgs): void {
const {
command,
appCode,
summary,
fixes,
error,
duration_ms,
} = args
const result = args.result || 'success'
const opts: OutputOpts = { verbose: args.verbose, json: args.json }
// ---- L3: 落 CommandLog (供 dacli log last 查询) ----
if (appCode) {
try {
const silent = fixes?.filter(f => f.level === 'silent') || []
const assertive = fixes?.filter(f => f.level === 'assertive') || []
const suggestions = fixes?.filter(f => f.level === 'suggest') || []
const target = (summary?.enCode as string) || (summary?.id as string) || undefined
const cmdLog: CommandLog = {
command,
timestamp: new Date().toISOString(),
duration_ms: duration_ms || 0,
result,
appCode,
summary: {
target,
id: summary?.id as string | undefined,
silent_fixes: silent.length,
assertive_fixes: assertive.length,
suggestions: suggestions.length,
error,
},
silent_fixes: silent,
assertive_fixes: assertive,
suggestions,
}
saveCommandLog(appCode, cmdLog)
// ---- L1: stdout 打印摘要 ----
if (!opts.json) {
if (opts.verbose) {
log.flush()
log.info('')
log.info(formatOneLineSummary(cmdLog))
log.info(` 详情: dacli log last`)
} else {
log.discard()
const oneLine = formatOneLineSummary(cmdLog)
process.stdout.write(`\n${oneLine}\n`)
process.stdout.write(` 详情: dacli log last\n`)
}
}
} catch {
// 日志写入失败不阻塞主流程, 但 stdout 仍要打印 (兜底)
if (!opts.json) {
if (opts.verbose) log.flush()
else log.discard()
if (summary && Object.keys(summary).length > 0) {
const lines = Object.entries(summary)
.map(([k, v]) => ` ${k}: ${v}`)
.join('\n')
process.stdout.write(`\n${lines}\n`)
}
if (error) process.stdout.write(` 错误: ${error}\n`)
process.stdout.write(` 详情: dacli log last\n`)
}
}
} else {
// ---- 无 appCode: 仅 L1 stdout, 跳过 L3 ----
if (!opts.json) {
if (opts.verbose) log.flush()
else log.discard()
if (summary && Object.keys(summary).length > 0) {
const lines = Object.entries(summary)
.map(([k, v]) => ` ${k}: ${v}`)
.join('\n')
process.stdout.write(`\n${lines}\n`)
}
if (error) process.stdout.write(` 错误: ${error}\n`)
process.stdout.write(` 详情: dacli log last\n`)
}
}
}