|
| 1 | +interface TechnologyLike { |
| 2 | + category?: string |
| 3 | + name?: string |
| 4 | + kind?: string |
| 5 | + confidence?: string |
| 6 | + evidence?: string[] |
| 7 | + sources?: string[] |
| 8 | + source?: string |
| 9 | + url?: string |
| 10 | + version?: string |
| 11 | +} |
| 12 | + |
| 13 | +interface TechStackReportInput { |
| 14 | + url?: string |
| 15 | + title?: string |
| 16 | + generatedAt?: string |
| 17 | + technologies?: TechnologyLike[] |
| 18 | + resources?: { total?: number } | null |
| 19 | + headerCount?: number |
| 20 | + headers?: unknown |
| 21 | +} |
| 22 | + |
| 23 | +const cleanText = (value: unknown): string => String(value ?? '').trim() |
| 24 | + |
| 25 | +const cleanInlineText = (value: unknown): string => cleanText(value).replace(/\s+/g, ' ') |
| 26 | + |
| 27 | +const cleanList = (value: unknown): string[] => { |
| 28 | + if (!Array.isArray(value)) return [] |
| 29 | + return value |
| 30 | + .filter(item => typeof item === 'string' || typeof item === 'number') |
| 31 | + .map(item => cleanInlineText(item)) |
| 32 | + .filter(Boolean) |
| 33 | +} |
| 34 | + |
| 35 | +const getHeaderCount = (input: TechStackReportInput): number => { |
| 36 | + if (typeof input.headerCount === 'number' && Number.isFinite(input.headerCount) && input.headerCount >= 0) { |
| 37 | + return input.headerCount |
| 38 | + } |
| 39 | + if (Array.isArray(input.headers)) return input.headers.length |
| 40 | + if (input.headers && typeof input.headers === 'object') return Object.keys(input.headers).length |
| 41 | + return 0 |
| 42 | +} |
| 43 | + |
| 44 | +const getResourceCount = (input: TechStackReportInput): number => { |
| 45 | + const total = Number(input.resources?.total || 0) |
| 46 | + return Number.isFinite(total) && total >= 0 ? total : 0 |
| 47 | +} |
| 48 | + |
| 49 | +const normalizeTechnologies = (items: TechnologyLike[] = []) => |
| 50 | + items |
| 51 | + .map(item => ({ |
| 52 | + category: cleanInlineText(item.category) || '未分类', |
| 53 | + name: cleanInlineText(item.name), |
| 54 | + version: cleanInlineText(item.version), |
| 55 | + kind: cleanInlineText(item.kind), |
| 56 | + confidence: cleanInlineText(item.confidence) || '未知', |
| 57 | + sources: cleanList(item.sources || (item.source ? [item.source] : [])), |
| 58 | + evidence: cleanList(item.evidence), |
| 59 | + url: cleanText(item.url) |
| 60 | + })) |
| 61 | + .filter(item => item.name) |
| 62 | + |
| 63 | +type NormalizedTechnology = ReturnType<typeof normalizeTechnologies>[number] |
| 64 | + |
| 65 | +const groupTechnologies = (items: NormalizedTechnology[]) => { |
| 66 | + const groups = new Map<string, typeof items>() |
| 67 | + for (const item of items) { |
| 68 | + const group = groups.get(item.category) || [] |
| 69 | + group.push(item) |
| 70 | + groups.set(item.category, group) |
| 71 | + } |
| 72 | + return [...groups.entries()] |
| 73 | +} |
| 74 | + |
| 75 | +const buildReportHeader = ( |
| 76 | + input: TechStackReportInput, |
| 77 | + technologies: NormalizedTechnology[], |
| 78 | + resourcesTotal: number, |
| 79 | + headerCount: number, |
| 80 | + generatedAt: string |
| 81 | +) => [ |
| 82 | + '# StackPrism 技术栈报告', |
| 83 | + '', |
| 84 | + `URL: ${cleanInlineText(input.url) || '未知'}`, |
| 85 | + `标题: ${cleanInlineText(input.title) || '未知'}`, |
| 86 | + `生成时间: ${generatedAt}`, |
| 87 | + '报告范围: 当前弹窗结果', |
| 88 | + `技术总数: ${technologies.length}`, |
| 89 | + `资源数: ${resourcesTotal}`, |
| 90 | + `主文档响应头数: ${headerCount}`, |
| 91 | + '', |
| 92 | + '## 人类阅读摘要' |
| 93 | +] |
| 94 | + |
| 95 | +const appendHumanSummary = (lines: string[], technologies: NormalizedTechnology[]) => { |
| 96 | + if (!technologies.length) { |
| 97 | + lines.push('', '未检测到明确技术栈。') |
| 98 | + return |
| 99 | + } |
| 100 | + for (const [category, items] of groupTechnologies(technologies)) { |
| 101 | + lines.push('', `### ${category} (${items.length})`) |
| 102 | + for (const item of items) { |
| 103 | + const name = item.version ? `${item.name} ${item.version}` : item.name |
| 104 | + lines.push(`- ${name} [${item.confidence}]`) |
| 105 | + if (item.kind) lines.push(` - 类型: ${item.kind}`) |
| 106 | + if (item.sources.length) lines.push(` - 来源: ${item.sources.join(', ')}`) |
| 107 | + if (item.evidence.length) lines.push(` - 依据: ${item.evidence.join(' | ')}`) |
| 108 | + if (item.url) lines.push(` - 链接: ${item.url}`) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +const buildStructuredPayload = ( |
| 114 | + input: TechStackReportInput, |
| 115 | + technologies: NormalizedTechnology[], |
| 116 | + resourcesTotal: number, |
| 117 | + headerCount: number, |
| 118 | + generatedAt: string |
| 119 | +) => ({ |
| 120 | + schema: 'stackprism.tech_stack_report.v1', |
| 121 | + url: cleanText(input.url), |
| 122 | + title: cleanText(input.title), |
| 123 | + generatedAt, |
| 124 | + summary: { |
| 125 | + technologyCount: technologies.length, |
| 126 | + resourceCount: resourcesTotal, |
| 127 | + headerCount |
| 128 | + }, |
| 129 | + technologies |
| 130 | +}) |
| 131 | + |
| 132 | +export const formatTechStackReport = (input: TechStackReportInput): string => { |
| 133 | + const technologies = normalizeTechnologies(input.technologies) |
| 134 | + const resourcesTotal = getResourceCount(input) |
| 135 | + const headerCount = getHeaderCount(input) |
| 136 | + const generatedAt = cleanText(input.generatedAt) || new Date().toISOString() |
| 137 | + const lines = buildReportHeader(input, technologies, resourcesTotal, headerCount, generatedAt) |
| 138 | + const structured = buildStructuredPayload(input, technologies, resourcesTotal, headerCount, generatedAt) |
| 139 | + |
| 140 | + appendHumanSummary(lines, technologies) |
| 141 | + lines.push('', '## AI Agent 结构化数据', '', '````json', JSON.stringify(structured, null, 2), '````') |
| 142 | + return lines.join('\n') |
| 143 | +} |
0 commit comments