feat(tools): 数据库查询按权限拆分为 user/admin,强制 userId 隔离
- query/ 为内部实现,user/ 仅供 bot-escort(绑定登录用户、schema 不暴露 userId),admin/ 仅供 escort-admin(显式跨用户) - 新增 employee、employee_review 查询;health_profile 用户端补齐 userId 隔离,按 _id 精确查询也带归属条件 - organization 是公共数据且无 userId,不进用户端;机构与写入工具统一移入 admin/ - utils 增加严格 ObjectId 解析,避免 CastError 与畸形 ID 进入查询 - 用户端员工视图剔除手机号、证件号、证书编号、紧急联系人与内部备注 - 新增 test/db_tools.test.js,用模型桩断言查询条件确实带 userId
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { tool } from "@langchain/core/tools";
|
||||
import { z } from "zod";
|
||||
import mongoose from "mongoose";
|
||||
import { DBModel } from "../../../../../models/index.js";
|
||||
import { escapeRegex } from "../utils.js";
|
||||
|
||||
/**
|
||||
* 医院信息查询工具
|
||||
*
|
||||
* 两种用法:
|
||||
* - 传 hospitalId:返回该院完整档案(id 可先用 hospital_info_list 获取);
|
||||
* - 传 hospitalName:按名称模糊查询,并可按科室 / 医生名过滤,返回面向上层展示的精简字段。
|
||||
*/
|
||||
const hospitalInfoQueryTool = tool(
|
||||
async ({ hospitalId, hospitalName, department, doctorName }) => {
|
||||
try {
|
||||
if (hospitalId) {
|
||||
if (!mongoose.Types.ObjectId.isValid(hospitalId)) {
|
||||
return { success: false, error: `Invalid hospital ID: ${hospitalId}` };
|
||||
}
|
||||
const org = await DBModel.Organization.findById(hospitalId).lean();
|
||||
if (!org) {
|
||||
return { success: false, error: `未找到 id 为「${hospitalId}」的机构` };
|
||||
}
|
||||
return { success: true, total: 1, data: [org] };
|
||||
}
|
||||
|
||||
if (!hospitalName) {
|
||||
return { success: false, error: "请提供 hospitalId 或 hospitalName" };
|
||||
}
|
||||
|
||||
const filter = { name: { $regex: escapeRegex(hospitalName), $options: "i" } };
|
||||
// 医院总量小,一次最多取 10 家,命中过多时由调用方细化名称
|
||||
const [total, orgs] = await Promise.all([
|
||||
DBModel.Organization.countDocuments(filter),
|
||||
DBModel.Organization.find(filter).limit(10).lean(),
|
||||
]);
|
||||
if (total === 0) {
|
||||
return { success: false, error: `未找到名称包含「${hospitalName}」的机构` };
|
||||
}
|
||||
|
||||
const data = orgs.map((org) => {
|
||||
const fa = org.forAgent ?? {};
|
||||
let departments = fa.departments ?? [];
|
||||
let doctors = fa.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 {
|
||||
_id: org._id,
|
||||
name: org.name,
|
||||
level: org.level ?? "",
|
||||
address: org.address ?? {},
|
||||
infoUpdatedAt: fa.updatedAt ?? null,
|
||||
overview: fa.overview ?? "",
|
||||
photos: fa.photos ?? [],
|
||||
departments,
|
||||
doctors,
|
||||
guides: fa.guides ?? {},
|
||||
};
|
||||
});
|
||||
|
||||
return { success: true, total, data };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "hospital_info_query",
|
||||
description:
|
||||
"查询医院就医信息。传 hospitalId 返回该院完整档案;传 hospitalName 按名称模糊查询,可再按科室、医生名过滤,返回医院总览(门诊/挂号/缴费/医保/交通)、科室(位置/电话/就诊提示)、医生(职称/擅长/号别/坐班)、场景指南、照片。最多返回 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 };
|
||||
Reference in New Issue
Block a user