feat(agent): 医院信息子代理改为本地文件知识库并接入双 bot

- 一院一 Markdown 档案(data/hospitals),索引与章节自动维护;读取支持 section 与 24k 截断,写入校验日期与单条长度,别名精确冲突时不新建
- 子代理按 allowWrite 分可写/只读工具集与提示词:管理端与陪诊员可写,就诊人端只读(物理移除写入工具)
- 新增 scripts/import_hospitals.js 存量导入:压缩医生为一行、默认前 60 位,支持 dry-run、幂等与冲突报告,生成 112 家医院档案
- 管理端图片识别原文沿用原样回显策略,不再经模型二次整理
- npm test 212 项通过(新增知识库 12 项、导入 3 项)
This commit is contained in:
lik
2026-11-18 19:28:26 +08:00
parent 6d2fc04424
commit 79fbc1ec3a
14 changed files with 1723 additions and 38 deletions
+333
View File
@@ -0,0 +1,333 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import {
createHospitalKnowledgeStore,
DEFAULT_SECTIONS,
MAX_ENTRY_CHARS,
MAX_READ_CHARS,
} from "../agent/infra/tools/hospital/store.js";
import { createHospitalInfoSubagent } from "../agent/subagent/hospital_info.js";
async function withStore(fn) {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "hospital-knowledge-"));
try {
const store = createHospitalKnowledgeStore({
rootDir: root,
now: () => new Date(2026, 10, 18),
});
await fn(store, root);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
}
test("初始化后返回默认章节,且没有医院", async () => {
await withStore(async (store) => {
const result = await store.list();
assert.deepEqual(result.sections, DEFAULT_SECTIONS);
assert.equal(result.index.length, 0);
});
});
test("新医院必须显式建档,避免误建重复档案", async () => {
await withStore(async (store) => {
const rejected = await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69156114",
source: "官网",
});
assert.equal(rejected.success, false);
assert.equal(rejected.reason, "not-found");
const created = await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69156114",
source: "官网",
aliases: ["协和", "北京协和"],
city: "北京",
createIfMissing: true,
});
assert.equal(created.success, true);
assert.equal(created.section, "联系电话");
assert.match(created.line, /^- 总机:010-69156114(来源:官网,2026-11-18)$/);
const read = await store.read("协和");
assert.equal(read.found, true);
assert.equal(read.name, "北京协和医院");
assert.match(read.content, /^# 北京协和医院$/m);
assert.match(read.content, /- 别名:协和、北京协和/);
assert.match(read.content, /## 联系电话\n\n- 总机:010-69156114/);
});
});
test("重复写入同一行不会产生重复条目", async () => {
await withStore(async (store) => {
await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69156114",
source: "官网",
createIfMissing: true,
});
const again = await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69156114",
source: "官网",
});
assert.equal(again.success, true);
assert.equal(again.duplicate, true);
const read = await store.read("北京协和医院");
assert.equal(read.content.match(/010-69156114/g).length, 1);
});
});
test("新章节登记到章节清单,并插入在「其他」之前", async () => {
await withStore(async (store) => {
await store.write({
hospital: "北京协和医院",
section: "其他",
content: "停车入口在西门",
source: "陪诊员经验",
createIfMissing: true,
});
await store.write({
hospital: "北京协和医院",
section: "医保结算",
content: "外地医保需先备案",
source: "陪诊员经验",
});
const { sections } = await store.list();
assert.ok(sections.includes("医保结算"));
assert.ok(sections.indexOf("医保结算") < sections.indexOf("其他"));
const read = await store.read("北京协和医院");
assert.ok(read.content.indexOf("## 医保结算") < read.content.indexOf("## 其他"));
});
});
test("替换模式只更新命中条目并保留来源", async () => {
await withStore(async (store) => {
await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69156114",
source: "官网",
createIfMissing: true,
});
await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "急诊:010-69155999",
source: "官网",
});
const replaced = await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69150000",
source: "管理员口述",
mode: "replace",
match: "010-69156114",
});
assert.equal(replaced.success, true);
const read = await store.read("北京协和医院");
assert.doesNotMatch(read.content, /010-69156114/);
assert.match(read.content, /- 总机:010-69150000(来源:管理员口述,2026-11-18)/);
assert.match(read.content, /- 急诊:010-69155999/);
});
});
test("明确新建时,分院不会被总院的候选挡住", async () => {
await withStore(async (store) => {
await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69156114",
createIfMissing: true,
});
const branch = await store.write({
hospital: "北京协和医院-西单院区",
section: "联系电话",
content: "总机:010-69150001",
aliases: ["协和西单院区"],
createIfMissing: true,
});
assert.equal(branch.success, true);
assert.notEqual(branch.file, "北京协和医院.md");
const read = await store.read("协和西单院区");
assert.equal(read.found, true);
assert.equal(read.name, "北京协和医院-西单院区");
});
});
test("别名精确冲突时写入返回歧义候选,createIfMissing 也不例外", async () => {
await withStore(async (store, root) => {
await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69151188",
aliases: ["协和"],
city: "北京",
createIfMissing: true,
});
await store.write({
hospital: "武汉协和医院",
section: "联系电话",
content: "总机:027-85726114",
aliases: ["协和"],
city: "武汉",
createIfMissing: true,
});
const rejected = await store.write({
hospital: "协和",
section: "联系电话",
content: "总机:010-00000000",
createIfMissing: true,
});
assert.equal(rejected.success, false);
assert.equal(rejected.reason, "ambiguous");
assert.equal(rejected.candidates.length, 2);
// 不产生第三个重复档案
const files = (await fs.readdir(root)).filter((name) => name.endsWith(".md") && !name.startsWith("_") && name !== "index.md");
assert.equal(files.length, 2);
// 使用正式名称仍然可以写入
const ok = await store.write({
hospital: "武汉协和医院",
section: "联系电话",
content: "总机:027-85726115",
mode: "replace",
match: "027-85726114",
});
assert.equal(ok.success, true);
});
});
test("链接只接受 http/https", async () => {
await withStore(async (store) => {
await assert.rejects(
store.write({
hospital: "北京协和医院",
section: "医生与排班",
content: "官网排班",
url: "javascript:alert(1)",
createIfMissing: true,
}),
/只支持 http\/https/,
);
});
});
test("按章节读取只返回目标章节", async () => {
await withStore(async (store) => {
await store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69151188",
source: "官网",
createIfMissing: true,
});
await store.write({
hospital: "北京协和医院",
section: "就诊流程",
content: "一层自助机取号后先到分诊台报到",
source: "陪诊员经验",
});
const result = await store.read("北京协和医院", { section: "联系电话" });
assert.equal(result.found, true);
assert.match(result.content, /## 联系电话/);
assert.match(result.content, /010-69151188/);
assert.doesNotMatch(result.content, /分诊台/);
assert.deepEqual(result.sections.sort(), ["就诊流程", "联系电话"].sort());
const missing = await store.read("北京协和医院", { section: "交通停车" });
assert.equal(missing.found, true);
assert.equal(missing.sectionMissing, true);
assert.ok(missing.sections.includes("联系电话"));
});
});
test("整档超过读取上限时截断并提示按章节读取", async () => {
await withStore(async (store) => {
const chunk = "a".repeat(MAX_ENTRY_CHARS - 10);
for (let i = 0; i < Math.ceil(MAX_READ_CHARS / chunk.length) + 1; i += 1) {
await store.write({
hospital: "北京协和医院",
section: "其他",
content: `${i}-${chunk}`,
source: "测试",
createIfMissing: i === 0,
});
}
const result = await store.read("北京协和医院");
assert.equal(result.truncated, true);
assert.equal(result.content.length, MAX_READ_CHARS);
assert.match(result.note, /section/);
assert.ok(result.sections.includes("其他"));
});
});
test("写入校验日期格式与单条内容长度", async () => {
await withStore(async (store) => {
await assert.rejects(
store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69151188",
createIfMissing: true,
date: "昨天",
}),
/YYYY-MM-DD/,
);
await assert.rejects(
store.write({
hospital: "北京协和医院",
section: "联系电话",
content: "总机:010-69151188",
createIfMissing: true,
date: "2026-02-30",
}),
/YYYY-MM-DD/,
);
await assert.rejects(
store.write({
hospital: "北京协和医院",
section: "其他",
content: "长".repeat(MAX_ENTRY_CHARS + 1),
createIfMissing: true,
}),
/内容过长/,
);
});
});
test("子代理按 allowWrite 生成不同工具集", () => {
const readOnly = createHospitalInfoSubagent({}, { allowWrite: false });
const writable = createHospitalInfoSubagent({}, { allowWrite: true });
const names = (subagent) => subagent.tools.map((item) => item.name);
assert.ok(!names(readOnly).includes("hospital_knowledge_write"));
assert.ok(names(readOnly).includes("hospital_knowledge_list"));
assert.ok(names(readOnly).includes("hospital_knowledge_read"));
assert.ok(names(writable).includes("hospital_knowledge_write"));
assert.match(readOnly.systemPrompt, /只读,不修改知识库/);
assert.doesNotMatch(readOnly.systemPrompt, /# 记录规则/);
assert.match(writable.systemPrompt, /# 记录规则/);
});