Files
api_health/agent/infra/tools/db/admin/hospital_info_query.js
T

127 lines
4.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { DBModel } from "../../../../../models/index.js";
import { escapeRegex, parseObjectId } from "../utils.js";
// 只返回就医需要的字段,避免把历史遗留字段整文档带出
const HOSPITAL_FIELDS = [
"name", "shortName", "parentId", "level", "nature", "address", "contact", "status",
"overview", "photos", "departments", "doctors", "guides", "infoUpdatedAt", "infoUpdatedBy",
].join(" ");
function toHospitalView(org, parentName = "") {
return {
_id: org._id,
name: org.name,
shortName: org.shortName ?? "",
parentId: org.parentId ?? null,
parentName,
level: org.level ?? "",
nature: org.nature ?? "",
address: org.address ?? {},
contact: org.contact ?? {},
status: org.status ?? "",
overview: org.overview ?? "",
photos: org.photos ?? [],
departments: org.departments ?? [],
doctors: org.doctors ?? [],
guides: org.guides ?? {},
infoUpdatedAt: org.infoUpdatedAt ?? null,
infoUpdatedBy: org.infoUpdatedBy ?? "",
};
}
// 分院/院区返回总院名称,便于上层确认归属
async function getParentNames(orgs) {
const ids = orgs.map((o) => o.parentId).filter(Boolean);
if (ids.length === 0) return new Map();
const parents = await DBModel.Organization.find({ _id: { $in: ids } })
.select("name")
.lean();
return new Map(parents.map((p) => [String(p._id), p.name]));
}
/**
* 医院信息查询工具
*
* 两种用法:
* - 传 hospitalId:返回该院完整档案(id 可先用 hospital_info_list 获取);
* - 传 hospitalName:按名称/简称/拼音模糊查询,并可按科室 / 医生名过滤,返回面向上层展示的精简字段。
*/
const hospitalInfoQueryTool = tool(
async ({ hospitalId, hospitalName, department, doctorName }) => {
try {
if (hospitalId) {
const id = parseObjectId(hospitalId);
if (!id) {
return { success: false, error: `Invalid hospital ID: ${hospitalId}` };
}
const org = await DBModel.Organization.findOne({ _id: id, type: "hospital" })
.select(HOSPITAL_FIELDS)
.lean();
if (!org) {
return { success: false, error: `未找到 id 为「${hospitalId}」的医院` };
}
const parents = await getParentNames([org]);
return { success: true, total: 1, data: [toHospitalView(org, parents.get(String(org.parentId)) ?? "")] };
}
if (!hospitalName) {
return { success: false, error: "请提供 hospitalId 或 hospitalName" };
}
const re = { $regex: escapeRegex(hospitalName), $options: "i" };
const filter = {
type: "hospital",
$or: [{ name: re }, { shortName: re }, { pinyin: re }, { pinyinFL: re }],
};
// 医院总量小,一次最多取 10 家,命中过多时由调用方细化名称
const [total, orgs] = await Promise.all([
DBModel.Organization.countDocuments(filter),
DBModel.Organization.find(filter).select(HOSPITAL_FIELDS).limit(10).lean(),
]);
if (total === 0) {
return { success: false, error: `未找到名称包含「${hospitalName}」的医院` };
}
const parents = await getParentNames(orgs);
const data = orgs.map((org) => {
let departments = org.departments ?? [];
let doctors = org.doctors ?? [];
if (department) {
departments = departments.filter((d) => d.name?.includes(department));
doctors = doctors.filter((d) => d.department?.includes(department));
}
if (doctorName) {
doctors = doctors.filter((d) => d.name?.includes(doctorName));
}
return {
...toHospitalView(org, parents.get(String(org.parentId)) ?? ""),
departments,
doctors,
};
});
return { success: true, total, data };
} catch (error) {
return { success: false, error: error.message };
}
},
{
name: "hospital_info_query",
description:
"查询医院就医信息。传 hospitalId 返回该院完整档案;传 hospitalName 按名称/简称/拼音模糊查询,可再按科室、医生名过滤,返回医院总览(门诊/挂号/缴费/医保/交通)、科室(位置/电话/就诊提示)、医生(职称/擅长/号别/坐班)、场景指南、照片。分院/院区会返回 parentName。最多返回 10 家医院。",
schema: z.object({
hospitalId: z
.string()
.optional()
.describe("医院 id(MongoDB ObjectId,来自 hospital_info_list),提供时忽略其他查询条件"),
hospitalName: z.string().optional().describe("医院名称/简称,支持模糊匹配(如 协和、协和西院)"),
department: z.string().optional().describe("科室名称过滤(如 风湿免疫科、放射科)"),
doctorName: z.string().optional().describe("医生姓名过滤"),
}),
}
);
export { hospitalInfoQueryTool };