feat(org): 平铺就医信息并支持分院/院区
This commit is contained in:
@@ -1,30 +1,50 @@
|
||||
import { tool } from "@langchain/core/tools";
|
||||
import { z } from "zod";
|
||||
import { DBModel } from "../../../../../models/index.js";
|
||||
import { escapeRegex } from "../utils.js";
|
||||
import { escapeRegex, parseObjectId } from "../utils.js";
|
||||
|
||||
const hospitalInfoSetTool = tool(
|
||||
async ({ hospitalName, section, action, content, guides, entry, entryName, updatedBy }) => {
|
||||
async ({ hospitalId, hospitalName, section, action, content, guides, entry, entryName, updatedBy }) => {
|
||||
try {
|
||||
if (!hospitalName) {
|
||||
return { success: false, error: "hospitalName 不能为空" };
|
||||
}
|
||||
// 优先按 id 定位;名称只允许唯一命中,分院/院区多命中时返回候选让上层确认
|
||||
let org = null;
|
||||
|
||||
// 定位医院:名称必须唯一命中;转义正则元字符,避免非法表达式与 ReDoS
|
||||
const orgs = await DBModel.Organization.find({
|
||||
name: { $regex: escapeRegex(hospitalName), $options: "i" },
|
||||
}).exec();
|
||||
if (orgs.length === 0) {
|
||||
return { success: false, error: `未找到名称包含「${hospitalName}」的机构,请确认医院名称后重试` };
|
||||
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) {
|
||||
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) {
|
||||
return { success: false, error: `未找到名称包含「${hospitalName}」的医院,请确认名称后重试` };
|
||||
}
|
||||
if (orgs.length > 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 ?? "",
|
||||
})),
|
||||
};
|
||||
}
|
||||
org = orgs[0];
|
||||
}
|
||||
if (orgs.length > 1) {
|
||||
return {
|
||||
success: false,
|
||||
error: `名称包含「${hospitalName}」的机构有 ${orgs.length} 家(${orgs.map((o) => o.name).join("、")}),请使用更精确的名称`,
|
||||
};
|
||||
}
|
||||
const org = orgs[0];
|
||||
const fa = org.forAgent ?? {};
|
||||
|
||||
const update = {};
|
||||
|
||||
@@ -33,13 +53,13 @@ const hospitalInfoSetTool = tool(
|
||||
if (typeof content !== "string") {
|
||||
return { success: false, error: "overview 需要提供 content 文本(写入前请先查询现有内容并合并)" };
|
||||
}
|
||||
update["forAgent.overview"] = content;
|
||||
update.overview = content;
|
||||
} else if (section === "guides") {
|
||||
// 场景键值对浅合并,值为 null 表示删除该场景
|
||||
if (!guides || typeof guides !== "object" || Array.isArray(guides)) {
|
||||
return { success: false, error: "guides 需要提供对象,键为场景名,值为文本(值为 null 表示删除该场景)" };
|
||||
}
|
||||
const merged = { ...(fa.guides ?? {}) };
|
||||
const merged = { ...(org.guides ?? {}) };
|
||||
for (const [key, value] of Object.entries(guides)) {
|
||||
if (value === null) {
|
||||
delete merged[key];
|
||||
@@ -47,10 +67,10 @@ const hospitalInfoSetTool = tool(
|
||||
merged[key] = value;
|
||||
}
|
||||
}
|
||||
update["forAgent.guides"] = merged;
|
||||
update.guides = merged;
|
||||
} else if (section === "photos" || section === "departments" || section === "doctors") {
|
||||
// 列表区块:按锚点 upsert(新增或整条替换)/ remove
|
||||
const list = [...(fa[section] ?? [])];
|
||||
const list = [...(org[section] ?? [])];
|
||||
|
||||
if (action === "upsert") {
|
||||
if (!entry || typeof entry !== "object") {
|
||||
@@ -74,7 +94,7 @@ const hospitalInfoSetTool = tool(
|
||||
} else {
|
||||
list.push(entry);
|
||||
}
|
||||
update[`forAgent.${section}`] = list;
|
||||
update[section] = list;
|
||||
} else if (action === "remove") {
|
||||
if (!entryName) {
|
||||
return {
|
||||
@@ -83,9 +103,9 @@ const hospitalInfoSetTool = tool(
|
||||
};
|
||||
}
|
||||
if (section === "photos") {
|
||||
update[`forAgent.${section}`] = list.filter((p) => p.url !== entryName);
|
||||
update[section] = list.filter((p) => p.url !== entryName);
|
||||
} else {
|
||||
update[`forAgent.${section}`] = list.filter((d) => d.name !== entryName);
|
||||
update[section] = list.filter((d) => d.name !== entryName);
|
||||
}
|
||||
} else {
|
||||
return { success: false, error: "列表区块(photos/departments/doctors)需要指定 action: upsert 或 remove" };
|
||||
@@ -94,13 +114,24 @@ const hospitalInfoSetTool = tool(
|
||||
return { success: false, error: "未知的 section,可选值:overview / photos / departments / doctors / guides" };
|
||||
}
|
||||
|
||||
update["forAgent.updatedAt"] = new Date();
|
||||
update.infoUpdatedAt = new Date();
|
||||
if (updatedBy) {
|
||||
update["forAgent.updatedBy"] = updatedBy;
|
||||
update.infoUpdatedBy = updatedBy;
|
||||
}
|
||||
|
||||
const updated = await DBModel.Organization.findByIdAndUpdate(org._id, { $set: update }, { new: true }).exec();
|
||||
return { success: true, data: updated.forAgent };
|
||||
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 ?? "",
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
@@ -108,9 +139,10 @@ const hospitalInfoSetTool = tool(
|
||||
{
|
||||
name: "hospital_info_set",
|
||||
description:
|
||||
"录入/更新医院就医信息。按区块维护:overview(医院级文本,全量替换,先查后改避免丢失)、guides(场景指南键值对,浅合并)、photos/departments/doctors(列表,按锚点 upsert 或 remove)。医院名称必须唯一命中。",
|
||||
"录入/更新医院就医信息。优先用 hospitalId 定位;只用 hospitalName 时必须唯一命中,分院/院区多命中会返回 candidates,需改用 hospitalId。按区块维护:overview(医院级文本,全量替换,先查后改避免丢失)、guides(场景指南键值对,浅合并)、photos/departments/doctors(列表,按锚点 upsert 或 remove)。",
|
||||
schema: z.object({
|
||||
hospitalName: z.string().describe("医院名称(需唯一命中,如 北京协和医院)"),
|
||||
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("要更新的区块"),
|
||||
|
||||
Reference in New Issue
Block a user