179 lines
7.9 KiB
JavaScript
179 lines
7.9 KiB
JavaScript
import { tool } from "@langchain/core/tools";
|
||
import { z } from "zod";
|
||
import { DBModel } from "../../../../../models/index.js";
|
||
import { escapeRegex, parseObjectId } from "../utils.js";
|
||
|
||
const hospitalInfoSetTool = tool(
|
||
async ({ hospitalId, hospitalName, section, action, content, guides, entry, entryName, updatedBy }) => {
|
||
try {
|
||
// 优先按 id 定位;名称只允许唯一命中,分院/院区多命中时返回候选让上层确认
|
||
let org = 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) {
|
||
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];
|
||
}
|
||
|
||
const update = {};
|
||
|
||
if (section === "overview") {
|
||
// 文本块全量替换
|
||
if (typeof content !== "string") {
|
||
return { success: false, error: "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 = { ...(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.infoUpdatedAt = new Date();
|
||
if (updatedBy) {
|
||
update.infoUpdatedBy = updatedBy;
|
||
}
|
||
|
||
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 ?? "",
|
||
},
|
||
};
|
||
} catch (error) {
|
||
return { success: false, error: error.message };
|
||
}
|
||
},
|
||
{
|
||
name: "hospital_info_set",
|
||
description:
|
||
"录入/更新医院就医信息。优先用 hospitalId 定位;只用 hospitalName 时必须唯一命中,分院/院区多命中会返回 candidates,需改用 hospitalId。按区块维护:overview(医院级文本,全量替换,先查后改避免丢失)、guides(场景指南键值对,浅合并)、photos/departments/doctors(列表,按锚点 upsert 或 remove)。",
|
||
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或姓名),用于审计"),
|
||
}),
|
||
}
|
||
);
|
||
|
||
export { hospitalInfoSetTool };
|