fix(agent): 医院工具切换至 hospital 集合并补齐支付约束

hospital_info_list/query/set 改为读写 hospital 集合,适配 entityName/nicknames/fudanRank;医院子代理与提示词同步字段口径;health_profile_set 更新启用 schema 校验;escort_record_set 限制线下收款方式,存在未支付流水时禁止改价。
This commit is contained in:
lik
2026-11-06 17:38:28 +08:00
parent 623ecf2651
commit 9ab2a39d08
8 changed files with 342 additions and 343 deletions
+81 -131
View File
@@ -1,176 +1,126 @@
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { DBModel } from "../../../../../models/index.js";
import { escapeRegex, parseObjectId } from "../utils.js";
import { parseObjectId } from "../utils.js";
const TEXT_FIELDS = ["name", "entityName", "province", "city", "address", "phone", "level"];
function toHospitalView(hospital) {
return {
id: hospital._id,
name: hospital.name ?? "",
entityName: hospital.entityName ?? "",
nicknames: hospital.nicknames ?? [],
province: hospital.province ?? "",
city: hospital.city ?? "",
address: hospital.address ?? "",
phone: hospital.phone ?? "",
level: hospital.level ?? "",
departments: hospital.departments ?? [],
fudanRank: hospital.fudanRank ?? null,
updatedAt: hospital.meta?.updatetime ?? null,
};
}
/**
* 医院基础信息维护工具
*
* 只维护 hospital 集合的基础字段;名称/主体变化时用模型静态方法,
* 由服务端同步拼音、昵称与复旦榜档位,避免手写派生字段。
*/
const hospitalInfoSetTool = tool(
async ({ hospitalId, hospitalName, section, action, content, guides, entry, entryName, updatedBy }) => {
async ({ hospitalId, hospitalName, name, entityName, province, city, address, phone, departments, level }) => {
try {
// 优先按 id 定位;名称只允许唯一命中,分院/院区多命中时返回候选让上层确认
let org = null;
// 优先按 id 定位;只用名称时必须唯一命中,多命中返回候选让上层确认
let hospital = null;
if (hospitalId) {
const id = parseObjectId(hospitalId);
if (!id) {
return { success: false, error: `Invalid hospital ID: ${hospitalId}` };
}
org = await DBModel.Organization.findOne({ _id: id, type: "hospital" }).exec();
if (!org) {
hospital = await DBModel.Hospital.findHospitalById(id);
if (!hospital) {
return { success: false, error: `未找到 id 为「${hospitalId}」的医院` };
}
} else {
if (!hospitalName) {
return { success: false, error: "请提供 hospitalId 或 hospitalName" };
}
const re = { $regex: escapeRegex(hospitalName), $options: "i" };
const orgs = await DBModel.Organization.find({
type: "hospital",
$or: [{ name: re }, { shortName: re }, { pinyin: re }, { pinyinFL: re }],
}).exec();
if (orgs.length === 0) {
const result = await DBModel.Hospital.findHospitals({
page: 1,
pageSize: 10,
keyword: hospitalName,
});
if (result.total === 0) {
return { success: false, error: `未找到名称包含「${hospitalName}」的医院,请确认名称后重试` };
}
if (orgs.length > 1) {
if (result.total > 1) {
return {
success: false,
error: `名称包含「${hospitalName}」的医院有 ${orgs.length} 家,请改用 hospitalId 精确写入`,
candidates: orgs.map((o) => ({
id: o._id,
name: o.name,
shortName: o.shortName ?? "",
parentId: o.parentId ?? null,
city: o.address?.city ?? "",
error: `名称包含「${hospitalName}」的医院有 ${result.total} 家,请改用 hospitalId 精确写入`,
candidates: result.list.map((item) => ({
id: item._id,
name: item.name,
entityName: item.entityName ?? "",
city: item.city ?? "",
})),
};
}
org = orgs[0];
hospital = result.list[0];
}
const input = { name, entityName, province, city, address, phone, level };
const update = {};
if (section === "overview") {
// 文本块全量替换
if (typeof content !== "string") {
return { success: false, error: "overview 需要提供 content 文本(写入前请先查询现有内容并合并)" };
for (const key of TEXT_FIELDS) {
const value = input[key];
if (value === undefined) continue;
const text = String(value ?? "").trim();
if (key === "name" && !text) {
return { success: false, error: "医院名称不能为空" };
}
update.overview = content;
} else if (section === "guides") {
// 场景键值对浅合并,值为 null 表示删除该场景
if (!guides || typeof guides !== "object" || Array.isArray(guides)) {
return { success: false, error: "guides 需要提供对象,键为场景名,值为文本(值为 null 表示删除该场景)" };
}
const merged = { ...(org.guides ?? {}) };
for (const [key, value] of Object.entries(guides)) {
if (value === null) {
delete merged[key];
} else {
merged[key] = value;
}
}
update.guides = merged;
} else if (section === "photos" || section === "departments" || section === "doctors") {
// 列表区块:按锚点 upsert(新增或整条替换)/ remove
const list = [...(org[section] ?? [])];
if (action === "upsert") {
if (!entry || typeof entry !== "object") {
return { success: false, error: "upsert 需要提供 entry 对象" };
}
let idx = -1;
if (section === "photos") {
if (!entry.url) return { success: false, error: "photos 条目必须包含 url 作为锚点" };
idx = list.findIndex((p) => p.url === entry.url);
} else if (section === "doctors") {
if (!entry.name || !entry.department) {
return { success: false, error: "doctors 条目必须包含 name 和 department 作为锚点" };
}
idx = list.findIndex((d) => d.name === entry.name && d.department === entry.department);
} else {
if (!entry.name) return { success: false, error: "departments 条目必须包含 name 作为锚点" };
idx = list.findIndex((d) => d.name === entry.name);
}
if (idx >= 0) {
list[idx] = { ...list[idx], ...entry };
} else {
list.push(entry);
}
update[section] = list;
} else if (action === "remove") {
if (!entryName) {
return {
success: false,
error: `remove 需要提供 entryName(${section === "photos" ? "照片url" : section === "doctors" ? "医生姓名" : "科室名称"})`,
};
}
if (section === "photos") {
update[section] = list.filter((p) => p.url !== entryName);
} else {
update[section] = list.filter((d) => d.name !== entryName);
}
} else {
return { success: false, error: "列表区块(photos/departments/doctors)需要指定 action: upsert 或 remove" };
}
} else {
return { success: false, error: "未知的 section,可选值:overview / photos / departments / doctors / guides" };
update[key] = text;
}
update.infoUpdatedAt = new Date();
if (updatedBy) {
update.infoUpdatedBy = updatedBy;
if (departments !== undefined) {
if (!Array.isArray(departments)) {
return { success: false, error: "科室需为字符串数组" };
}
update.departments = departments.map((item) => String(item ?? "").trim()).filter(Boolean);
}
const updated = await DBModel.Organization.findByIdAndUpdate(org._id, { $set: update }, { new: true }).exec();
return {
success: true,
data: {
overview: updated.overview ?? "",
photos: updated.photos ?? [],
departments: updated.departments ?? [],
doctors: updated.doctors ?? [],
guides: updated.guides ?? {},
infoUpdatedAt: updated.infoUpdatedAt ?? null,
infoUpdatedBy: updated.infoUpdatedBy ?? "",
},
};
if (Object.keys(update).length === 0) {
return { success: false, error: "未提供任何要更新的字段" };
}
const updated = await DBModel.Hospital.updateHospitalById(hospital._id, update);
if (!updated) {
return { success: false, error: `未找到 id 为「${hospital._id}」的医院` };
}
return { success: true, data: toHospitalView(updated) };
} catch (error) {
if (error?.code === 11000) {
return { success: false, error: "医院名称已存在,请使用「主体名称-分院名称」格式避免重名" };
}
return { success: false, error: error.message };
}
},
{
name: "hospital_info_set",
description:
"录入/更新医院就医信息。优先用 hospitalId 定位;只用 hospitalName 时必须唯一命中,分院/院区多命中会返回 candidates,需改用 hospitalId。按区块维护:overview(医院级文本,全量替换,先查后改避免丢失)、guides(场景指南键值对,浅合并)、photos/departments/doctors(列表,按锚点 upsert 或 remove)。",
"录入/更新医院基础信息。优先用 hospitalId 定位;只用 hospitalName 时必须唯一命中,多命中会返回 candidates,需改用 hospitalId。可更新 name、entityName、province、city、address、phone、departments(科室名称数组,整体替换)、level。名称是唯一键,改名会自动同步拼音、昵称与复旦榜档位;分院/院区是独立记录,名称约定为「主体名称-分院名称」。",
schema: z.object({
hospitalId: z.string().optional().describe("医院 id(MongoDB ObjectId,来自 hospital_info_list),优先使用"),
hospitalName: z.string().optional().describe("医院名称/简称(仅在唯一命中时可用,分院/院区建议改用 hospitalId)"),
section: z
.enum(["overview", "photos", "departments", "doctors", "guides"])
.describe("要更新的区块"),
action: z
.enum(["set", "upsert", "remove"])
.optional()
.describe("操作类型:overview/guides 用 set;photos/departments/doctors 用 upsert 或 remove"),
content: z.string().optional().describe("overview 区块的完整文本(全量替换)"),
guides: z
.record(z.string(), z.string().nullable())
.optional()
.describe('guides 区块的场景键值对,如 {"住院流程": "..."};值为 null 表示删除该场景'),
entry: z
.object({
name: z.string().optional().describe("科室名或医生名(锚点)"),
department: z.string().optional().describe("医生所属科室(医生锚点之一)"),
detail: z
.string()
.optional()
.describe("信息全文:科室(位置/电话/类型/提示)或医生(职称/擅长/号别/坐班停诊)"),
photo: z.string().optional().describe("照片链接"),
url: z.string().optional().describe("照片链接(photos 锚点)"),
caption: z.string().optional().describe("照片说明"),
})
.optional()
.describe("photos/departments/doctors 条目(upsert 时必填)"),
entryName: z.string().optional().describe("remove 的锚点:科室名称 / 医生姓名 / 照片url"),
updatedBy: z.string().optional().describe("操作人标识(用户ID或姓名),用于审计"),
hospitalName: z.string().optional().describe("医院名称/主体名称/拼音/昵称(仅在唯一命中时可用)"),
name: z.string().optional().describe("医院名称;分院/院区用「主体名称-分院名称」"),
entityName: z.string().optional().describe("主体名称;留空时由名称自动派生"),
province: z.string().optional().describe("省份,如 北京市"),
city: z.string().optional().describe("城市,如 北京市"),
address: z.string().optional().describe("医院地址"),
phone: z.string().optional().describe("联系电话"),
departments: z.array(z.string()).optional().describe("科室名称数组,整体替换"),
level: z.string().optional().describe("医院等级,如 三级甲等"),
}),
}
);