Files
lik 79fbc1ec3a feat(agent): 医院信息子代理改为本地文件知识库并接入双 bot
- 一院一 Markdown 档案(data/hospitals),索引与章节自动维护;读取支持 section 与 24k 截断,写入校验日期与单条长度,别名精确冲突时不新建
- 子代理按 allowWrite 分可写/只读工具集与提示词:管理端与陪诊员可写,就诊人端只读(物理移除写入工具)
- 新增 scripts/import_hospitals.js 存量导入:压缩医生为一行、默认前 60 位,支持 dry-run、幂等与冲突报告,生成 112 家医院档案
- 管理端图片识别原文沿用原样回显策略,不再经模型二次整理
- npm test 212 项通过(新增知识库 12 项、导入 3 项)
2026-11-18 19:28:26 +08:00

539 lines
20 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 存量医院资料导入:doc/hospitals/*.md -> data/hospitals/
*
* 只提取稳定字段和紧凑摘要,丢弃长简介、照片与排班快照:
* - 基础信息/联系电话/官网排班入口 -> 基本信息、联系电话、医生与排班
* - 就医总览 按条目拆分到 医生与排班(挂号)、就诊流程、交通停车、其他(医保)
* - 医生信息压缩为一行:姓名、职称、擅长(≤80 字)、详情页链接,并按上限截取
* - 源文件路径与导入日期记入「其他」,便于回溯
*
* 用法(在 api/health 下执行):
* node scripts/import_hospitals.js # dry-run,只打印报告
* node scripts/import_hospitals.js --apply # 写入 data/hospitals
* node scripts/import_hospitals.js --apply --force # 覆盖已存在且内容不同的文件
* node scripts/import_hospitals.js --source ../../doc/hospitals --max-doctors 120 --date 2026-11-18
*/
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import config from "../config.js";
import { isValidDate } from "../agent/infra/tools/hospital/store.js";
const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url));
const DEFAULT_SOURCE = path.resolve(SCRIPT_DIR, "../../../doc/hospitals");
const MAX_SPECIALTY_CHARS = 80;
const MAX_INTRO_CHARS = 120;
const MAX_DEPT_CHARS = 240;
const MAX_OVERVIEW_CHARS = 300;
const SECTION_ORDER = [
"基本信息",
"联系电话",
"医生与排班",
"就诊流程",
"检查检验",
"住院手术",
"交通停车",
"陪诊注意",
"其他",
];
function parseArgs(argv) {
const options = {
apply: argv.includes("--apply"),
force: argv.includes("--force"),
source: DEFAULT_SOURCE,
target: path.resolve(process.cwd(), config.agent?.dataDir ?? "data", "hospitals"),
date: formatDate(new Date()),
maxDoctors: 60,
};
for (let i = 0; i < argv.length; i += 1) {
const key = argv[i];
const next = argv[i + 1];
if (key === "--source" && next) options.source = path.resolve(next);
else if (key === "--target" && next) options.target = path.resolve(next);
else if (key === "--date" && next) options.date = next;
else if (key === "--max-doctors" && next) options.maxDoctors = Number(next);
}
return options;
}
function formatDate(date) {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
return `${y}-${m}-${d}`;
}
function toFileName(name) {
const safe = String(name ?? "")
.trim()
.replace(/[\\/:*?"<>|]/g, "_")
.replace(/[. ]+$/g, "")
.slice(0, 80);
return `${safe || "未命名医院"}.md`;
}
function stripPrefix(file) {
return path.basename(file, ".md").replace(/^\d+[a-z]?_/, "");
}
function truncate(text, max) {
const value = String(text ?? "").trim();
return value.length > max ? `${value.slice(0, max)}…` : value;
}
// 按 `## ` 切分顶层章节;### 及其内容保留在所属章节内
function splitTopSections(text) {
const sections = new Map();
let current = null;
let buffer = [];
for (const line of text.split(/\r?\n/)) {
const heading = line.match(/^##\s+(.+?)\s*$/);
if (heading) {
if (current) sections.set(current, buffer.join("\n"));
current = heading[1].trim();
buffer = [];
} else if (current) {
buffer.push(line);
}
}
if (current) sections.set(current, buffer.join("\n"));
return sections;
}
function findSection(sections, prefix) {
for (const [key, value] of sections) {
if (key.startsWith(prefix)) return value;
}
return "";
}
function parseFields(body) {
const fields = new Map();
for (const line of body.split(/\r?\n/)) {
const match = line.match(/^-\s+([^::]+)[::]\s*(.+)$/);
if (match) fields.set(match[1].trim(), match[2].trim());
}
return fields;
}
function splitList(value, separators = /[、;;,,]/) {
return String(value ?? "")
.split(separators)
.map((item) => item.trim().replace(/["'“”]/g, "").trim())
.filter(Boolean);
}
// 缩写别名:保留原值,同时拆出括号前的名称
function expandAliases(values, exclude) {
const result = new Set();
for (const value of values) {
const clean = String(value ?? "").trim();
if (!clean) continue;
result.add(clean);
const beforeParen = clean.split(/[((]/)[0].trim();
if (beforeParen) result.add(beforeParen);
}
result.delete(exclude);
return [...result];
}
function parseDoctors(body) {
const lines = body.split(/\r?\n/);
const doctors = [];
let department = "";
for (let i = 0; i < lines.length; i += 1) {
const heading = lines[i].match(/^###\s+(.+?)\s*$/);
if (heading) {
department = heading[1].trim();
continue;
}
const match = lines[i].match(/^-\s+\*\*(.+?)\*\*\s*(?:|(.*))?$/);
if (!match) continue;
let title = "";
let specialty = "";
for (const part of (match[2] || "").split("|")) {
const index = part.indexOf(":");
if (index < 0) {
if (!title) title = extractTitle(part);
continue;
}
const key = part.slice(0, index).trim();
if (key === "擅长") specialty = part.slice(index + 1).trim().replace(/^擅长[::]?/, "").trim();
else if (!title) title = extractTitle(part);
// 出诊/简介/照片等时效或长文本直接丢弃
}
// 详情页在后续缩进行中
let detail = "";
for (let j = i + 1; j < Math.min(i + 4, lines.length); j += 1) {
const detailMatch = lines[j].match(/^\s+-\s+详情页[::]\s*(https?:\/\/\S+)/);
if (detailMatch) {
detail = detailMatch[1];
break;
}
}
doctors.push({ name: match[1].trim(), title, specialty: truncate(specialty, MAX_SPECIALTY_CHARS), detail, department });
}
return doctors;
}
// 职称只保留可识别的头衔,避免把「院长/科室主任」等长描述塞进条目
function extractTitle(part) {
const match = String(part).match(/(知名专家|主任医师|副主任医师|主治医师|住院医师|主任药师|副主任药师|主任技师|副主任技师|教授|副教授)/);
return match ? match[1] : truncate(part, 40);
}
// 主要科室顶层的 `- 科室名` 列表(遇到 ### 子章节停止)
function parseDepartmentNames(body) {
const names = [];
for (const line of body.split(/\r?\n/)) {
if (/^###/.test(line)) break;
const match = line.match(/^-\s+(.+)$/);
if (match) names.push(match[1].trim());
}
return names;
}
function extractDoctorLinks(body) {
const links = [];
for (const line of body.split(/\r?\n/)) {
const match = line.match(/(?:doctorOverview|doctorList|doctorDetail)[::]\s*(https?:\/\/\S+)/);
if (match) links.push(match[1]);
}
return links;
}
function compressIntro(body) {
const clean = body.replace(/\s+/g, " ").trim();
if (!clean) return "";
const stop = clean.search(/[。;]/);
const sentence = stop > 0 ? clean.slice(0, stop + 1) : clean;
return truncate(sentence, MAX_INTRO_CHARS);
}
// 就医总览:按 ①②③ 拆条,按关键词归入目标章节
function splitOverview(body) {
const clean = body.replace(/\s+/g, " ").trim();
if (!clean) return [];
return clean
.split(/(?=[①②③④⑤⑥⑦⑧⑨⑩])/)
.map((item) => item.replace(/^[①②③④⑤⑥⑦⑧⑨⑩]\s*/, "").trim())
.filter(Boolean);
}
function classifyOverview(item) {
// 就医总览的条目自带主题前缀(门诊/挂号/医保/交通),优先按前缀归类
if (/^(交通|停车)/.test(item)) return "交通停车";
if (/^(门诊|急诊|就诊)/.test(item)) return "就诊流程";
if (/^(挂号|预约)/.test(item)) return "医生与排班";
if (/^(医保|社保)/.test(item)) return "其他";
if (/交通|地铁|公交|停车|驾车|路线/.test(item)) return "交通停车";
if (/挂号|预约|号源|放号/.test(item)) return "医生与排班";
if (/医保|社保|结算/.test(item)) return "其他";
return "就诊流程";
}
function pickDoctors(doctors, maxDoctors) {
const rank = (title) => {
if (/知名专家|主任医师/.test(title)) return 3;
if (/副主任医师/.test(title)) return 2;
if (/主治/.test(title)) return 1;
return 0;
};
return doctors
.map((doctor, index) => ({ doctor, index }))
.sort((a, b) => rank(b.doctor.title) - rank(a.doctor.title) || a.index - b.index)
.slice(0, maxDoctors)
.map(({ doctor }) => doctor);
}
export function parseHospitalFile(text, { file, date, maxDoctors = 60 }) {
const sections = splitTopSections(text);
const basic = parseFields(findSection(sections, "基础信息"));
const fullName = basic.get("官方全称") || stripPrefix(file);
const address = basic.get("主院区地址") || "";
const cityMatch = address.match(/^(.+?市)/);
// 解析失败时仍默认北京(存量资料均为北京医院),但通过 cityFallback 提示操作者确认
const city = cityMatch ? cityMatch[1].replace(/市$/, "") : "北京";
const cityFallback = !cityMatch;
const source = `存量资料 ${file}`;
const entry = (content, url) => `${content}${url ? `:${url}` : ""}(来源:${source},${date})`;
const groups = new Map(SECTION_ORDER.map((name) => [name, []]));
const push = (section, content, url) => {
const line = entry(content, url);
if (!groups.get(section).includes(line)) groups.get(section).push(line);
};
// 基本信息
push("基本信息", `官方全称:${fullName}`);
if (basic.get("医院等级")) push("基本信息", `等级:${basic.get("医院等级")}`);
if (basic.get("机构性质")) push("基本信息", `性质:${basic.get("机构性质")}`);
if (address) push("基本信息", `地址:${address}`);
const otherCampuses = basic.get("其他院区");
if (otherCampuses && !/^无/.test(otherCampuses)) push("基本信息", `其他院区:${truncate(otherCampuses, MAX_OVERVIEW_CHARS)}`);
const intro = compressIntro(findSection(sections, "医院简介"));
if (intro) push("基本信息", `简介:${intro}`);
// 联系电话
for (const phone of String(basic.get("联系电话") ?? "").split(/[;;]/).map((item) => item.trim()).filter(Boolean)) {
push("联系电话", `电话:${truncate(phone, 200)}`);
}
// 医生与排班:官方入口 + 挂号渠道 + 医生
const scheduleEntry = basic.get("出诊及医生信息") || basic.get("挂号及医生信息");
if (scheduleEntry) push("医生与排班", "官网排班入口", scheduleEntry);
const doctorSection = findSection(sections, "医生信息与出诊安排") || findSection(sections, "主要科室");
const doctors = parseDoctors(doctorSection);
const doctorLinks = extractDoctorLinks(doctorSection);
if (doctorLinks[0]) push("医生与排班", "医生名单入口", doctorLinks[0]);
// 就医总览拆条
for (const item of splitOverview(findSection(sections, "就医总览"))) {
push(classifyOverview(item), truncate(item, MAX_OVERVIEW_CHARS));
}
// 医生压缩:按职称优先截取,超出时注明总数与入口
const uniqueDoctors = [];
const seenDoctors = new Set();
for (const doctor of doctors) {
if (seenDoctors.has(doctor.name)) continue;
seenDoctors.add(doctor.name);
uniqueDoctors.push(doctor);
}
if (uniqueDoctors.length) {
if (uniqueDoctors.length > maxDoctors) {
push("医生与排班", `医生名单:官网共 ${uniqueDoctors.length} 位,本档案压缩收录前 ${maxDoctors} 位,完整名单见官方医生页`);
}
for (const doctor of pickDoctors(uniqueDoctors, maxDoctors)) {
const title = doctor.title ? `,${doctor.title}` : "";
const specialty = doctor.specialty ? `,擅长${doctor.specialty}` : "";
const detail = doctor.detail ? `(详情:${doctor.detail})` : "";
push("医生与排班", `医生:${doctor.name}${title}${specialty}${detail}`);
}
}
// 其他:科室清单与来源回溯
const departments = parseDepartmentNames(findSection(sections, "主要科室"));
if (departments.length) {
push("其他", `主要科室:${truncate(departments.join("、"), MAX_DEPT_CHARS)}(共 ${departments.length} 个)`);
}
groups.get("其他").push(`资料导入:doc/hospitals/${file}(导入日期 ${date})`);
// 名称:主体沿用文件名,分院加「主体-院区」
const baseName = stripPrefix(file);
const name = baseName;
return {
file,
name,
officialName: fullName,
aliases: [...new Set([
...expandAliases(splitList(basic.get("简称/别名")), name),
...(fullName && fullName !== name ? [fullName] : []),
baseName,
])].filter((alias) => alias && alias !== name),
city,
cityFallback,
sections: groups,
};
}
export function renderHospital(entity, { date }) {
const lines = [`# ${entity.name}`, ""];
if (entity.aliases.length) lines.push(`- 别名:${entity.aliases.join("、")}`);
if (entity.city) lines.push(`- 城市:${entity.city}`);
lines.push(`- 最后更新:${date}`, "");
for (const section of SECTION_ORDER) {
const entries = entity.sections.get(section) || [];
if (!entries.length) continue;
lines.push(`## ${section}`);
for (const line of entries) lines.push(`- ${line}`);
lines.push("");
}
return `${lines.join("\n").trimEnd()}\n`;
}
// 院区名称:优先用「主体-院区」,主体来自同序号主文件
function resolveNames(entities) {
const mains = new Map();
for (const entity of entities) {
const match = entity.file.match(/^(\d+)([a-z]?)_/);
if (match && !match[2]) mains.set(match[1], entity);
}
for (const entity of entities) {
const match = entity.file.match(/^(\d+)([a-z]?)_/);
if (!match || !match[2]) continue;
const parent = mains.get(match[1]);
if (!parent) continue;
const base = stripPrefix(entity.file);
if (base.startsWith(parent.name)) {
entity.name = `${parent.name}-${base.slice(parent.name.length)}`;
} else if (entity.officialName.startsWith(parent.officialName)) {
entity.name = `${parent.officialName}-${entity.officialName.slice(parent.officialName.length)}`;
}
}
// 去重别名,并补上「协和」「同仁」这类口语简称;与任何正式名称重名的别名一律丢弃,
// 否则精确匹配会一次命中多家,反而无法定位
const names = new Set(entities.map((entity) => entity.name));
for (const entity of entities) {
const derived = deriveAliases(entity.name).filter((alias) => !names.has(alias));
entity.aliases = [...new Set([...entity.aliases, ...derived])]
.filter((alias) => alias && alias !== entity.name && !names.has(alias));
}
return entities;
}
const NAME_PREFIXES = ["中国医学科学院", "中国中医科学院", "首都医科大学附属", "北京大学", "北京中医药大学", "北京协和医学院"];
const CITY_PREFIXES = ["北京市", "北京", "首都"];
// 由正式名称派生口语简称:北京协和医院 -> 协和医院 / 北京协和 / 协和
function deriveAliases(name) {
if (!name || name.includes("-") || name.length < 5) return [];
const result = new Set();
const add = (value) => {
if (value && value.length >= 2 && value !== name) result.add(value);
};
let base = name;
for (const prefix of NAME_PREFIXES) {
if (base.startsWith(prefix) && base.length > prefix.length + 2) {
base = base.slice(prefix.length);
break;
}
}
add(base);
for (const prefix of CITY_PREFIXES) {
if (base.startsWith(prefix) && base.length > prefix.length + 1) {
base = base.slice(prefix.length);
add(base);
break;
}
}
if (base.endsWith("医院") && base.length > 2) add(base.slice(0, -2));
if (name.endsWith("医院") && name.length > 4) add(name.slice(0, -2));
// 「第一」「第二」这类序号不能当简称
return [...result].filter((alias) => !/第[一二三四五六七八九十]+$/.test(alias));
}
function buildIndex(entities) {
const lines = entities.map((entity) => {
const parts = [`名称:${entity.name}`];
if (entity.aliases.length) parts.push(`别名:${entity.aliases.join("、")}`);
if (entity.city) parts.push(`城市:${entity.city}`);
parts.push(`文件:${toFileName(entity.name)}`);
return `- ${parts.join(";")}`;
});
return `# 医院索引\n\n${lines.join("\n")}\n`;
}
async function readIfExists(file) {
try {
return await fs.readFile(file, "utf8");
} catch (error) {
if (error.code === "ENOENT") return null;
throw error;
}
}
async function writeAtomic(file, content) {
const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
await fs.writeFile(temp, content, "utf8");
await fs.rename(temp, file);
}
async function main() {
const options = parseArgs(process.argv.slice(2));
if (!isValidDate(options.date)) {
console.error(`[error] --date 格式不正确:${options.date},请使用 YYYY-MM-DD`);
process.exit(1);
}
const files = (await fs.readdir(options.source)).filter((file) => file.endsWith(".md")).sort();
const entities = [];
const warnings = [];
for (const file of files) {
const text = await fs.readFile(path.join(options.source, file), "utf8");
const entity = parseHospitalFile(text, { file, date: options.date, maxDoctors: options.maxDoctors });
if (!entity.officialName) warnings.push(`${file}: 未解析到官方全称`);
if (entity.cityFallback) warnings.push(`${file}: 未能从地址解析城市,默认按「北京」处理`);
entities.push(entity);
}
resolveNames(entities);
// 同名冲突:索引以名称为键,重名会互相覆盖
const seen = new Map();
for (const entity of entities) {
if (seen.has(entity.name)) warnings.push(`重名:${entity.name}(${seen.get(entity.name)} / ${entity.file})`);
else seen.set(entity.name, entity.file);
}
const rendered = entities.map((entity) => ({
entity,
fileName: toFileName(entity.name),
content: renderHospital(entity, { date: options.date }),
}));
console.log(`source=${options.source}`);
console.log(`target=${options.target}`);
console.log(`date=${options.date}, files=${files.length}, maxDoctors=${options.maxDoctors}`);
for (const { entity, fileName, content } of rendered) {
const doctorCount = (entity.sections.get("医生与排班") || []).filter((line) => line.startsWith("医生:")).length;
console.log(`- ${fileName} ${(content.length / 1024).toFixed(1)}KB 医生 ${doctorCount}`);
}
for (const warning of warnings) console.warn(`[warn] ${warning}`);
if (!options.apply) {
console.log("\n[dry-run] 未写入任何文件。确认后加 --apply 执行。");
return;
}
await fs.mkdir(options.target, { recursive: true });
let written = 0;
let skipped = 0;
const conflicts = [];
for (const { fileName, content } of rendered) {
const targetFile = path.join(options.target, fileName);
const existing = await readIfExists(targetFile);
if (existing !== null && existing !== content && !options.force) {
conflicts.push(fileName);
continue;
}
if (existing === content) {
skipped += 1;
continue;
}
await writeAtomic(targetFile, content);
written += 1;
}
const usedSections = new Set(SECTION_ORDER.slice(0, -1));
for (const entity of entities) {
for (const [section, entries] of entity.sections) {
if (entries.length) usedSections.add(section);
}
}
const sectionList = SECTION_ORDER.filter((section) => usedSections.has(section));
await writeAtomic(path.join(options.target, "_sections.md"), `# 医院信息章节\n\n${sectionList.map((item) => `- ${item}`).join("\n")}\n`);
await writeAtomic(path.join(options.target, "index.md"), buildIndex(entities));
console.log(`\n[apply] 写入 ${written} 个,跳过未变更 ${skipped} 个,索引 ${entities.length} 家`);
if (conflicts.length) {
console.warn(`[conflict] ${conflicts.length} 个文件已存在且内容不同,未覆盖(加 --force 覆盖):`);
for (const file of conflicts) console.warn(` - ${file}`);
}
}
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
main().catch((error) => {
console.error(error);
process.exit(1);
});
}