import { tool } from "@langchain/core/tools"; import { z } from "zod"; import { DBModel } from "../../../../../models/index.js"; import { escapeRegex, parseObjectId } from "../utils.js"; /** * 健康档案查询内部实现 * * 仅供 db/user 与 db/admin 下的工具包装使用,不直接暴露给 Agent: * - 用户端包装只传 userId,强制按当前登录用户隔离,且不暴露 userId 筛选; * - 管理端包装传 allowCrossUser,才允许跨用户查询。 */ function buildHealthProfileQueryTool({ userId = null, allowCrossUser = false } = {}) { // 用户端只认调用方绑定的登录用户,管理端不绑定归属 const boundUserId = allowCrossUser ? null : parseObjectId(userId); const shape = { profileId: z .string() .optional() .describe("健康档案 ID(_id),提供时精确查询单条,忽略其他筛选条件"), name: z.string().optional().describe("患者姓名(支持中文模糊、全拼、拼音首字母匹配,如 张三/zhangsan/zs)"), mobile: z.string().optional().describe("患者电话(精确匹配)"), page: z.number().int().min(1).optional().describe("页码,从 1 开始"), pageSize: z .number() .int() .min(1) .max(50) .optional() .describe("每页数量(1-50),默认 20"), }; if (allowCrossUser) { shape.userId = z.string().optional().describe("所属用户ID(精确匹配,仅管理端可用)"); } return tool( async ({ profileId, userId: queryUserId, name, mobile, page = 1, pageSize = 20 }) => { try { // 用户端未登录/身份异常时直接拒绝,绝不回落到跨用户查询 if (!allowCrossUser && !boundUserId) { return { success: false, error: "用户未登录,无法查询健康档案" }; } // 用户端把归属条件强制并入所有查询(包括按档案 ID 精确查询) const scope = boundUserId ? { userId: boundUserId } : {}; // 按档案 _id 精确查询单条 if (profileId) { const profileObjectId = parseObjectId(profileId); if (!profileObjectId) { return { success: false, error: `Invalid profile ID: ${profileId}` }; } const doc = await DBModel.HealthProfile.findOne({ _id: profileObjectId, ...scope }).lean(); if (!doc) { // 用户端不区分「不存在」与「不属于本人」,避免凭 ID 探测他人档案 return { success: false, error: allowCrossUser ? `未找到 ID 为 ${profileId} 的健康档案` : "未找到对应的健康档案", }; } return { success: true, data: [doc], total: 1, page: 1, pageSize: 1 }; } const filter = { ...scope }; if (allowCrossUser && queryUserId) { const ownerId = parseObjectId(queryUserId); if (!ownerId) { return { success: false, error: `Invalid user ID: ${queryUserId}` }; } filter.userId = ownerId; } if (name) { // 转义正则特殊字符,避免查询报错或 ReDoS;姓名支持中文、全拼、拼音首字母匹配 const re = { $regex: escapeRegex(name), $options: "i" }; filter.$or = [{ "profile.name": re }, { "profile.pinyin": re }, { "profile.pinyinFL": re }]; } if (mobile) { filter["profile.mobile"] = mobile; } const skip = (page - 1) * pageSize; const [list, total] = await Promise.all([ DBModel.HealthProfile.find(filter) .sort({ "meta.createtime": -1 }) .skip(skip) .limit(pageSize) .lean(), DBModel.HealthProfile.countDocuments(filter), ]); return { success: true, data: list, total, page, pageSize, }; } catch (error) { return { success: false, error: error.message }; } }, { name: "health_profile_query", description: allowCrossUser ? "查询患者健康档案(管理端)。支持按档案 ID 精确查询,或按用户 ID、患者姓名(模糊)、患者电话筛选,返回分页结果。" : "查询当前登录用户本人的健康档案。支持按档案 ID 精确查询,或按患者姓名(模糊)、患者电话筛选,返回分页结果。查询自动限定为当前登录用户,不会返回他人档案。", schema: z.object(shape), } ); } /** 内部实现:由 db/user 与 db/admin 包装后暴露给对应 Agent */ export { buildHealthProfileQueryTool };