feat(dacli): v2.0 auto-fix pipeline + structured logging
Core changes:
- _auto_fix.ts: 3-tier fix pipeline (Silent/Assertive/Suggest)
- Silent: jnpfKey whitelist, child table component restrictions, _fk suffix removal
- Assertive: datetime→datePicker, timePicker→datePicker, int+dict→select, FK→relationForm
- Suggest: FK找不到匹配表单, int+select缺dictType
- logfile.ts: structured JSON logs under ~/.dacli/logs/{appCode}/
- log/last.ts: dacli log last command for querying
- form/create.ts: integrated auto_fix pipeline + AI token pre-check auto-re-login
Verified:
- NCPF AI token expired → auto re-login before AI call
- f_signup_time (timePicker) → auto-fixed to datePicker ✅
- f_user_id (inputNumber) → suggestion (no matching 'user' form in testspace)
- Output ends with v2 one-line summary + 'dacli log last' hint
Co-Authored-By: Claude <[email protected]>
This commit is contained in:
1 parent
61405e128d
commit
741a175766
5 files changed
+775
-1
No files matched your search
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* core/logfile.ts — 结构化命令日志 (v2.0+)
|
||||
*
|
||||
* 设计:
|
||||
* - 每次命令执行写一个 JSON 日志文件
|
||||
* - stdout: AI 看结果摘要 (6 行)
|
||||
* - 日志文件: 人查详情 (修正/回滚/原始请求)
|
||||
*
|
||||
* 文件结构:
|
||||
* ~/.dacli/logs/{appCode}/
|
||||
* ├── {timestamp}-{command}-{enCode}.json
|
||||
* └── tail -> 最新日志 (符号链接)
|
||||
*/
|
||||
|
||||
import { mkdirSync, writeFileSync, symlinkSync, existsSync, unlinkSync, readFileSync } from 'node:fs'
|
||||
import { join, dirname } from 'node:path'
|
||||
import { homedir } from 'node:os'
|
||||
|
||||
const LOG_BASE = join(homedir(), '.dacli', 'logs')
|
||||
|
||||
export interface FixEntry {
|
||||
id: string
|
||||
level: 'silent' | 'assertive' | 'suggest'
|
||||
field?: string
|
||||
summary: string
|
||||
from?: string
|
||||
to?: string
|
||||
rollback?: string
|
||||
candidates?: string[]
|
||||
confidence?: 'extremely_high' | 'high' | 'medium'
|
||||
}
|
||||
|
||||
export interface CommandLog {
|
||||
command: string
|
||||
timestamp: string
|
||||
duration_ms: number
|
||||
result: 'success' | 'failure' | 'partial'
|
||||
appCode: string
|
||||
summary: {
|
||||
target?: string
|
||||
id?: string
|
||||
silent_fixes: number
|
||||
assertive_fixes: number
|
||||
suggestions: number
|
||||
error?: string
|
||||
}
|
||||
silent_fixes: FixEntry[]
|
||||
assertive_fixes: FixEntry[]
|
||||
suggestions: FixEntry[]
|
||||
raw?: {
|
||||
request?: unknown
|
||||
response?: unknown
|
||||
}
|
||||
}
|
||||
|
||||
/** 保存日志,返回日志文件路径 */
|
||||
export function saveCommandLog(appCode: string, log: CommandLog): string {
|
||||
const dir = join(LOG_BASE, appCode)
|
||||
mkdirSync(dir, { recursive: true })
|
||||
|
||||
const ts = log.timestamp.replace(/[:.]/g, '-').slice(0, 19)
|
||||
const filename = `${ts}-${log.command.replace(/\s+/g, '-')}.json`
|
||||
const filepath = join(dir, filename)
|
||||
writeFileSync(filepath, JSON.stringify(log, null, 2))
|
||||
|
||||
// 更新 tail 符号链接
|
||||
const tailPath = join(dir, 'tail')
|
||||
try {
|
||||
if (existsSync(tailPath)) unlinkSync(tailPath)
|
||||
symlinkSync(filepath, tailPath)
|
||||
} catch {
|
||||
// symlink 失败不影响主流程
|
||||
}
|
||||
|
||||
return filepath
|
||||
}
|
||||
|
||||
/** 读取最新日志 */
|
||||
export function readLatestLog(appCode: string): CommandLog | null {
|
||||
const tailPath = join(LOG_BASE, appCode, 'tail')
|
||||
try {
|
||||
if (!existsSync(tailPath)) return null
|
||||
return JSON.parse(readFileSync(tailPath, 'utf-8'))
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** 获取日志目录 */
|
||||
export function getLogDir(appCode: string): string {
|
||||
return join(LOG_BASE, appCode)
|
||||
}
|
||||
|
||||
/** 格式化 fix 条目为人类可读 (用于 dacli log --last) */
|
||||
export function formatFixForDisplay(fix: FixEntry): string {
|
||||
const icon = fix.level === 'silent' ? ' ' : fix.level === 'assertive' ? '✅' : '💡'
|
||||
let line = `${icon} ${fix.summary}`
|
||||
if (fix.level === 'assertive' && fix.rollback) {
|
||||
line += `\n 回滚: ${fix.rollback}`
|
||||
}
|
||||
if (fix.level === 'suggest' && fix.candidates?.length) {
|
||||
line += `\n 候选: ${fix.candidates.join(' / ')}`
|
||||
}
|
||||
return line
|
||||
}
|
||||
|
||||
/** 格式化完整日志为人类可读的摘要 */
|
||||
export function formatLogSummary(log: CommandLog): string {
|
||||
const lines: string[] = []
|
||||
lines.push(`━━ dacli ${log.command} — ${log.timestamp}`)
|
||||
lines.push('')
|
||||
lines.push(` 结果: ${log.result === 'success' ? '✓ 成功' : log.result === 'partial' ? '⚠ 部分成功' : '✗ 失败'}`)
|
||||
if (log.summary.target) {
|
||||
lines.push(` 目标: ${log.summary.target}${log.summary.id ? ` (id=${log.summary.id})` : ''}`)
|
||||
}
|
||||
if (log.summary.error) {
|
||||
lines.push(` 错误: ${log.summary.error}`)
|
||||
}
|
||||
lines.push('')
|
||||
|
||||
if (log.silent_fixes.length > 0 || log.assertive_fixes.length > 0) {
|
||||
lines.push(`━━ 修正 ${log.silent_fixes.length + log.assertive_fixes.length} 处:`)
|
||||
for (const f of [...log.silent_fixes, ...log.assertive_fixes]) {
|
||||
lines.push(formatFixForDisplay(f))
|
||||
}
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
if (log.suggestions.length > 0) {
|
||||
lines.push(`━━ 建议 ${log.suggestions.length} 项:`)
|
||||
for (const s of log.suggestions) {
|
||||
lines.push(formatFixForDisplay(s))
|
||||
}
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
/** 为 stdout 生成单行摘要 (AI 看的) */
|
||||
export function formatOneLineSummary(log: CommandLog): string {
|
||||
const { result, summary } = log
|
||||
const icon = result === 'success' ? '✓' : result === 'partial' ? '⚠' : '✗'
|
||||
const parts: string[] = []
|
||||
if (summary.target) parts.push(summary.target)
|
||||
if (summary.id) parts.push(`(id=${summary.id.slice(0, 8)}...)`)
|
||||
const fixCount = summary.silent_fixes + summary.assertive_fixes
|
||||
if (fixCount > 0) parts.push(`修正 ${fixCount} 处`)
|
||||
if (summary.suggestions > 0) parts.push(`建议 ${summary.suggestions} 项`)
|
||||
if (log.duration_ms) parts.push(`[${(log.duration_ms / 1000).toFixed(1)}s]`)
|
||||
let line = `${icon} ${parts.join(' ')}`
|
||||
if (result === 'failure' && summary.error) {
|
||||
line += `\n ${summary.error}`
|
||||
}
|
||||
return line
|
||||
}
|
||||
Reference in new issue
Block a user