import { tool } from "@langchain/core/tools"; import { z } from "zod"; import { DBModel } from "../../../../../models/index.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, name, entityName, province, city, address, phone, departments, level }) => { try { // 优先按 id 定位;只用名称时必须唯一命中,多命中返回候选让上层确认 let hospital = null; if (hospitalId) { const id = parseObjectId(hospitalId); if (!id) { return { success: false, error: `Invalid hospital ID: ${hospitalId}` }; } hospital = await DBModel.Hospital.findHospitalById(id); if (!hospital) { return { success: false, error: `未找到 id 为「${hospitalId}」的医院` }; } } else { if (!hospitalName) { return { success: false, error: "请提供 hospitalId 或 hospitalName" }; } const result = await DBModel.Hospital.findHospitals({ page: 1, pageSize: 10, keyword: hospitalName, }); if (result.total === 0) { return { success: false, error: `未找到名称包含「${hospitalName}」的医院,请确认名称后重试` }; } if (result.total > 1) { return { success: false, error: `名称包含「${hospitalName}」的医院有 ${result.total} 家,请改用 hospitalId 精确写入`, candidates: result.list.map((item) => ({ id: item._id, name: item.name, entityName: item.entityName ?? "", city: item.city ?? "", })), }; } hospital = result.list[0]; } const input = { name, entityName, province, city, address, phone, level }; const update = {}; 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[key] = text; } if (departments !== undefined) { if (!Array.isArray(departments)) { return { success: false, error: "科室需为字符串数组" }; } update.departments = departments.map((item) => String(item ?? "").trim()).filter(Boolean); } 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。可更新 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("医院名称/主体名称/拼音/昵称(仅在唯一命中时可用)"), 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("医院等级,如 三级甲等"), }), } ); export { hospitalInfoSetTool };