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:
@@ -2,6 +2,7 @@ import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import EscortAgent from "../agent/bots/bot-escort/agent.js";
|
||||
import EscortAdminAgent from "../agent/bots/bot-escort-admin/agent.js";
|
||||
|
||||
// 一份超过护栏 400 字上限的识别原文:用来确认回显分支不再截断长报告
|
||||
const IMAGE_TEXT = [
|
||||
@@ -58,3 +59,31 @@ test("空白识别原文不投递", async () => {
|
||||
assert.equal(chunks.length, 0);
|
||||
assert.equal(agent.messages.length, 0);
|
||||
});
|
||||
|
||||
test("陪诊师端图片识别原文完整回显,不经过模型与输出护栏", async () => {
|
||||
const agent = new EscortAdminAgent();
|
||||
const chunks = [];
|
||||
const user = { _id: "admin-format-user", profile: {}, location: {} };
|
||||
|
||||
await agent.streamChat(
|
||||
user,
|
||||
[{ type: "format", ts: "2026-11-01 10:00:00", content: IMAGE_TEXT, id: "req_admin_format" }],
|
||||
(source, type, content, id) => {
|
||||
chunks.push({ source, type, content, id });
|
||||
},
|
||||
"admin"
|
||||
);
|
||||
|
||||
assert.ok(chunks.length > 1);
|
||||
assert.equal(chunks.map(item => item.content).join(""), IMAGE_TEXT);
|
||||
assert.deepEqual(new Set(chunks.map(item => item.id)), new Set(["req_admin_format"]));
|
||||
assert.deepEqual(new Set(chunks.map(item => item.source)), new Set(["main"]));
|
||||
assert.deepEqual(new Set(chunks.map(item => item.type)), new Set(["ai"]));
|
||||
|
||||
// 原文进会话历史,后续追问图片内容时模型能看到
|
||||
const session = agent.sessions.get(user._id);
|
||||
assert.equal(session.messages.length, 1);
|
||||
assert.match(session.messages[0].text, /白细胞计数 WBC 6\.2/);
|
||||
// 走回显分支时不构建 agent,也就不会产生模型调用
|
||||
assert.equal(session.agent, null);
|
||||
});
|
||||
|
||||
@@ -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, /# 记录规则/);
|
||||
});
|
||||
@@ -0,0 +1,108 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { parseHospitalFile, renderHospital } from "../scripts/import_hospitals.js";
|
||||
|
||||
const SOURCE = `# 示例市第一医院
|
||||
|
||||
## 基础信息
|
||||
|
||||
- 官方全称:示例市第一医院
|
||||
- 简称/别名:示例一院、"一院"
|
||||
- 医院等级:三级甲等
|
||||
- 机构性质:公立
|
||||
- 联系电话:010-12345678;010-87654321
|
||||
- 官方网站:https://example.com/
|
||||
- 出诊及医生信息:https://example.com/schedule
|
||||
- 主院区地址:北京市东城区示例路1号
|
||||
- 其他院区:无(单院区)
|
||||
|
||||
## 医院简介
|
||||
|
||||
示例医院始建于1950年,是一所综合医院。第二句话不应保留。
|
||||
|
||||
## 就医总览
|
||||
|
||||
①门诊:工作日8:00开诊;②挂号渠道:114平台;③医保:定点机构;④交通:地铁1号线示例站。
|
||||
|
||||
## 主要科室
|
||||
|
||||
- 心内科
|
||||
- 呼吸科
|
||||
|
||||
## 医生信息与出诊安排(2026-09-09 抓取)
|
||||
|
||||
> 信息来源:
|
||||
> - doctorOverview:https://example.com/doctors
|
||||
|
||||
### 心内科
|
||||
|
||||
- **张三**|主任医师|擅长:冠心病、高血压|出诊:星期一上午|简介:这是很长的医生简介,应被丢弃
|
||||
- 照片:https://example.com/a.jpg
|
||||
- 详情页:https://example.com/d/1.html
|
||||
- **李四**|副主任医师|擅长:擅长心律失常
|
||||
- 详情页:https://example.com/d/2.html
|
||||
|
||||
## 数据来源
|
||||
|
||||
1. https://example.com/source
|
||||
|
||||
## 核实状态
|
||||
|
||||
- [x] 医院等级
|
||||
`;
|
||||
|
||||
const DATE = "2026-11-18";
|
||||
|
||||
test("存量文件按章节映射并压缩医生信息", () => {
|
||||
const entity = parseHospitalFile(SOURCE, { file: "99_示例医院.md", date: DATE });
|
||||
const section = (name) => entity.sections.get(name);
|
||||
|
||||
assert.equal(entity.name, "示例医院");
|
||||
assert.ok(entity.aliases.includes("示例一院"));
|
||||
assert.equal(entity.city, "北京");
|
||||
|
||||
assert.equal(section("联系电话").length, 2);
|
||||
assert.ok(section("联系电话")[0].startsWith("电话:010-12345678"));
|
||||
|
||||
const doctorLines = section("医生与排班");
|
||||
assert.ok(doctorLines.some((line) => line.includes("官网排班入口:https://example.com/schedule")));
|
||||
assert.ok(doctorLines.some((line) => line.includes("医生名单入口:https://example.com/doctors")));
|
||||
assert.ok(doctorLines.some((line) => line.includes("挂号渠道:114平台")));
|
||||
|
||||
const zhang = doctorLines.find((line) => line.includes("张三"));
|
||||
assert.match(zhang, /主任医师/);
|
||||
assert.match(zhang, /擅长冠心病、高血压/);
|
||||
assert.match(zhang, /详情:https:\/\/example\.com\/d\/1\.html/);
|
||||
assert.doesNotMatch(zhang, /简介|照片/);
|
||||
|
||||
const li = doctorLines.find((line) => line.includes("李四"));
|
||||
assert.match(li, /擅长心律失常/);
|
||||
assert.doesNotMatch(li, /擅长擅长/);
|
||||
|
||||
assert.ok(section("就诊流程").some((line) => line.startsWith("门诊:")));
|
||||
assert.ok(section("交通停车").some((line) => line.startsWith("交通:")));
|
||||
assert.ok(section("其他").some((line) => line.startsWith("医保:")));
|
||||
assert.ok(section("其他").some((line) => line.includes("主要科室:心内科、呼吸科")));
|
||||
assert.ok(section("其他").some((line) => line.includes("资料导入:doc/hospitals/99_示例医院.md")));
|
||||
});
|
||||
|
||||
test("医生数量超限时按职称保留并注明总数", () => {
|
||||
const entity = parseHospitalFile(SOURCE, { file: "99_示例医院.md", date: DATE, maxDoctors: 1 });
|
||||
const doctorLines = entity.sections.get("医生与排班").filter((line) => line.startsWith("医生:"));
|
||||
|
||||
assert.equal(doctorLines.length, 1);
|
||||
assert.ok(doctorLines[0].includes("张三"));
|
||||
assert.ok(entity.sections.get("医生与排班").some((line) => line.includes("官网共 2 位")));
|
||||
});
|
||||
|
||||
test("渲染结果只包含有内容的章节且条目带来源日期", () => {
|
||||
const entity = parseHospitalFile(SOURCE, { file: "99_示例医院.md", date: DATE });
|
||||
const text = renderHospital(entity, { date: DATE });
|
||||
|
||||
assert.match(text, /^# 示例医院$/m);
|
||||
assert.match(text, /- 最后更新:2026-11-18/);
|
||||
assert.doesNotMatch(text, /## 检查检验/);
|
||||
assert.doesNotMatch(text, /## 住院手术/);
|
||||
assert.match(text, /(来源:存量资料 99_示例医院\.md,2026-11-18)/);
|
||||
});
|
||||
Reference in New Issue
Block a user