import { relative } from "node:path"; import type { SearchResult, TableSchema } from "./types.ts"; export function formatSearchResults(query: string, results: SearchResult[], basePath: string): string { if (results.length === 0) { return `未找到与 "${query}" 相关的金蝶知识库结果。`; } const lines = [`"${query}" 的金蝶知识库结果:`, ""]; for (let i = 0; i < results.length; i++) { const result = results[i]; lines.push(`[${i + 1}] ${result.section.heading}`); lines.push(`来源:${relative(basePath, result.file.path)}:${result.section.lineStart + 1}`); lines.push(`相关度:${result.score.toFixed(2)}`); lines.push(result.highlights[0] || result.section.content.trim().split("\n").slice(0, 5).join("\n")); lines.push(""); } return lines.join("\n").trim(); } export function formatTableSchema(tableName: string, schema: TableSchema | undefined): string { if (!schema) { return `未找到 "${tableName}" 的表结构。`; } const lines = [ `表:${schema.name} (${tableName.toUpperCase()})`, `模块:${schema.module || "未知"}`, `说明:${schema.description || "无"}`, "", "字段:", ]; for (const field of schema.fields) { const nullable = field.nullable ? "可空" : "必填"; lines.push(`- ${field.name} ${field.type} ${nullable} - ${field.description}`); } if (schema.relatedTables.length > 0) { lines.push("", "相关表:"); for (const related of schema.relatedTables) { lines.push(`- ${related.table}: ${related.relation} - ${related.description}`); } } return lines.join("\n"); }