|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import process from 'node:process'; |
| 4 | +import { getCliVersion } from '../../utils/version.js'; |
| 5 | +import { copyToClipboard } from '../utils/commandUtils.js'; |
| 6 | +import { CommandKind, SlashCommand } from './types.js'; |
| 7 | +import { t } from '../utils/i18n.js'; |
| 8 | + |
| 9 | +type ReportOptions = { |
| 10 | + copy: boolean; |
| 11 | + full: boolean; |
| 12 | +}; |
| 13 | + |
| 14 | +const parseReportOptions = (args: string): ReportOptions => { |
| 15 | + const tokens = args.split(/\s+/).filter(Boolean); |
| 16 | + let copy = true; |
| 17 | + let full = false; |
| 18 | + |
| 19 | + for (let i = 0; i < tokens.length; i += 1) { |
| 20 | + const token = tokens[i]; |
| 21 | + if (token === '--no-copy') { |
| 22 | + copy = false; |
| 23 | + continue; |
| 24 | + } |
| 25 | + if (token === '--copy') { |
| 26 | + copy = true; |
| 27 | + continue; |
| 28 | + } |
| 29 | + if (token === '--full') { |
| 30 | + full = true; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return { copy, full }; |
| 35 | +}; |
| 36 | + |
| 37 | +const formatCheck = (label: string, ok: boolean, detail?: string): string => { |
| 38 | + const status = ok ? 'ok' : 'missing'; |
| 39 | + if (detail) { |
| 40 | + return `- ${label}: ${status} (${detail})`; |
| 41 | + } |
| 42 | + return `- ${label}: ${status}`; |
| 43 | +}; |
| 44 | + |
| 45 | +const formatRecentLines = (title: string, items: string[]): string => { |
| 46 | + if (items.length === 0) { |
| 47 | + return `${title}: (none)`; |
| 48 | + } |
| 49 | + return [title, ...items.map((item) => `- ${item}`)].join('\n'); |
| 50 | +}; |
| 51 | + |
| 52 | +export const reportCommand: SlashCommand = { |
| 53 | + name: 'report', |
| 54 | + description: t('command.report.description'), |
| 55 | + kind: CommandKind.BUILT_IN, |
| 56 | + action: async (context, args) => { |
| 57 | + const options = parseReportOptions(args); |
| 58 | + const cliVersion = await getCliVersion(); |
| 59 | + const config = context.services.config; |
| 60 | + const projectRoot = config?.getProjectRoot() ?? process.cwd(); |
| 61 | + const settings = context.services.settings.merged; |
| 62 | + |
| 63 | + const cliBuildStamp = path.join( |
| 64 | + projectRoot, |
| 65 | + 'packages', |
| 66 | + 'cli', |
| 67 | + 'dist', |
| 68 | + '.last_build', |
| 69 | + ); |
| 70 | + const coreBuildStamp = path.join( |
| 71 | + projectRoot, |
| 72 | + 'packages', |
| 73 | + 'core', |
| 74 | + 'dist', |
| 75 | + '.last_build', |
| 76 | + ); |
| 77 | + const genaiTypes = path.join( |
| 78 | + projectRoot, |
| 79 | + 'node_modules', |
| 80 | + '@google', |
| 81 | + 'genai', |
| 82 | + 'dist', |
| 83 | + 'genai.d.ts', |
| 84 | + ); |
| 85 | + |
| 86 | + const reportLines: string[] = [ |
| 87 | + '# DeepV Code Report', |
| 88 | + '', |
| 89 | + '## Environment', |
| 90 | + `- CLI version: ${cliVersion}`, |
| 91 | + `- Node: ${process.version}`, |
| 92 | + `- Platform: ${process.platform} ${process.arch}`, |
| 93 | + `- Project root: ${projectRoot}`, |
| 94 | + `- Auth type: ${settings.selectedAuthType ?? 'not configured'}`, |
| 95 | + `- Server URL: ${process.env.DEEPX_SERVER_URL ?? 'default'}`, |
| 96 | + '', |
| 97 | + '## Build & Dependencies', |
| 98 | + formatCheck( |
| 99 | + 'CLI build stamp', |
| 100 | + fs.existsSync(cliBuildStamp), |
| 101 | + 'packages/cli/dist/.last_build', |
| 102 | + ), |
| 103 | + formatCheck( |
| 104 | + 'Core build stamp', |
| 105 | + fs.existsSync(coreBuildStamp), |
| 106 | + 'packages/core/dist/.last_build', |
| 107 | + ), |
| 108 | + formatCheck( |
| 109 | + 'GenAI types', |
| 110 | + fs.existsSync(genaiTypes), |
| 111 | + 'node_modules/@google/genai/dist/genai.d.ts', |
| 112 | + ), |
| 113 | + ]; |
| 114 | + |
| 115 | + if (options.full) { |
| 116 | + const recentErrors = (context.ui.history ?? []) |
| 117 | + .filter((item) => item.type === 'error' && item.text) |
| 118 | + .slice(-5) |
| 119 | + .map((item) => item.text ?? ''); |
| 120 | + |
| 121 | + const recentDebug = (context.ui.debugMessages ?? []) |
| 122 | + .slice(-5) |
| 123 | + .map((item) => item.content ?? '') |
| 124 | + .filter((item) => item); |
| 125 | + |
| 126 | + reportLines.push( |
| 127 | + '', |
| 128 | + '## Recent Errors', |
| 129 | + formatRecentLines('Errors', recentErrors), |
| 130 | + '', |
| 131 | + '## Recent Debug Messages', |
| 132 | + formatRecentLines('Debug', recentDebug), |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + const report = reportLines.join('\n'); |
| 137 | + |
| 138 | + if (options.copy) { |
| 139 | + try { |
| 140 | + await copyToClipboard(report); |
| 141 | + } catch (error) { |
| 142 | + const message = |
| 143 | + error instanceof Error ? error.message : String(error ?? 'unknown'); |
| 144 | + return { |
| 145 | + type: 'message', |
| 146 | + messageType: 'error', |
| 147 | + content: `${t('command.report.copy_failed')} ${message}`, |
| 148 | + }; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return { |
| 153 | + type: 'message', |
| 154 | + messageType: 'info', |
| 155 | + content: options.copy |
| 156 | + ? `${report}\n\n${t('command.report.copied')}` |
| 157 | + : report, |
| 158 | + }; |
| 159 | + }, |
| 160 | +}; |
0 commit comments