feat(agent): 管理端 agent 支持陪诊员并按角色收敛工具
陪诊员身份以 Employee 在职档案为准,显式管理员优先;管理端小程序首次登录会写入 role:['user'],不能据此判断。管理员保留跨用户查询与医院资料写入,陪诊员只开放本人订单查询/改单(归属校验、状态与字段白名单、并发保护)与本人档案,患者档案与财务操作不可用。
This commit is contained in:
@@ -18,8 +18,11 @@ import { createHospitalInfoSubagent } from "../../subagent/hospital_info.js";
|
||||
import {
|
||||
webFetchTool, webSearchTool, getCalendarInfoTool, getYearHolidaysTool,
|
||||
httpGetTool, escortRecordQueryTool, escortRecordSetTool,
|
||||
employeeQueryTool, employeeReviewQueryTool
|
||||
employeeQueryTool, employeeReviewQueryTool,
|
||||
createAttendantEscortRecordQueryTool, buildAttendantEscortRecordSetTool,
|
||||
createAttendantEmployeeQueryTool
|
||||
} from "../../infra/tools/index.js";
|
||||
import { resolveEscortAdminRole } from "../../../utils/permissions.js";
|
||||
|
||||
export default class EscortAdminAgent {
|
||||
constructor(options = {}) {
|
||||
@@ -39,11 +42,18 @@ export default class EscortAdminAgent {
|
||||
}
|
||||
|
||||
// msg: { agent: 'escort-admin', type: 'chat', ts: "2023-08-01 10:00:00", content: "你好" }
|
||||
async streamChat(userInfo, msgs, callback) {
|
||||
// role: 入口已解析的 admin / attendant;不传时在这里兜底解析,避免绕过入口直接调用
|
||||
async streamChat(userInfo, msgs, callback, role = null) {
|
||||
if (!msgs.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const effectiveRole = role ?? await resolveEscortAdminRole(userInfo);
|
||||
if (!effectiveRole) {
|
||||
callback("main", "ai", "当前账号未绑定在职陪诊员档案,请联系管理员开通。", msgs[0]?.id);
|
||||
return;
|
||||
}
|
||||
|
||||
const userId = userInfo._id;
|
||||
|
||||
// LRU:按用户取会话,并更新使用顺序
|
||||
@@ -78,21 +88,23 @@ export default class EscortAdminAgent {
|
||||
// 长会话下工具结果会挤占上下文,按完整轮次裁剪,避免上下文溢出与"忘记"前文
|
||||
session.messages = trimHistory(session.messages, this.maxHistoryChars);
|
||||
|
||||
// 用户资料变更后需重建 agent,否则 systemPrompt 仍是旧资料
|
||||
const fingerprint = this._userFingerprint(userInfo);
|
||||
if (session.agent && session.fingerprint !== fingerprint) {
|
||||
// 用户资料或角色变更后需重建 agent,否则 systemPrompt 与工具集仍是旧的
|
||||
const fingerprint = this._userFingerprint(userInfo, effectiveRole);
|
||||
if (session.agent && (session.fingerprint !== fingerprint || session.role !== effectiveRole)) {
|
||||
session.agent = null;
|
||||
}
|
||||
session.fingerprint = fingerprint;
|
||||
session.role = effectiveRole;
|
||||
|
||||
const agent = session.agent ?? this._genAgent(userInfo);
|
||||
const agent = session.agent ?? this._genAgent(userInfo, effectiveRole);
|
||||
session.agent = agent;
|
||||
|
||||
// 输出护栏:禁用词(身份泄露/空泛客套/越界承诺)拦截;
|
||||
// 长度上限只作用于主回答,子代理输出会单独展示,不受主回答篇幅约束
|
||||
const guardLabel = effectiveRole === "attendant" ? "escort-admin:attendant" : "escort-admin:admin";
|
||||
const guards = {
|
||||
main: new OutputGuard({ label: "admin", maxChars: this.maxOutputChars }),
|
||||
subagent: new OutputGuard({ label: "admin:subagent", maxChars: Infinity }),
|
||||
main: new OutputGuard({ label: guardLabel, maxChars: this.maxOutputChars }),
|
||||
subagent: new OutputGuard({ label: `${guardLabel}:subagent`, maxChars: Infinity }),
|
||||
};
|
||||
const lastAiId = { main: "", subagent: "" };
|
||||
|
||||
@@ -167,13 +179,43 @@ export default class EscortAdminAgent {
|
||||
}
|
||||
|
||||
// 用户资料指纹:姓名/手机号/角色变化时需重建 agent,否则 systemPrompt 仍是旧资料
|
||||
_userFingerprint(userInfo) {
|
||||
_userFingerprint(userInfo, role = "admin") {
|
||||
if (!userInfo) return "anonymous";
|
||||
const profile = userInfo.profile ?? {};
|
||||
return JSON.stringify([profile.name, profile.mobile, userInfo.app]);
|
||||
return JSON.stringify([profile.name, profile.mobile, userInfo.app, role]);
|
||||
}
|
||||
|
||||
_genAgent(userInfo) {
|
||||
// 公共安全工具:天气、路线、日期、节假日与网页检索,不涉及业务数据
|
||||
_commonTools() {
|
||||
return [webFetchTool, webSearchTool, httpGetTool, getCalendarInfoTool, getYearHolidaysTool];
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理端工具集
|
||||
* 仅显式管理员或员工档案为管理员时开放:跨用户查询、患者档案、医院资料维护等
|
||||
*/
|
||||
_adminTools() {
|
||||
return [
|
||||
...this._commonTools(),
|
||||
escortRecordQueryTool, escortRecordSetTool,
|
||||
employeeQueryTool, employeeReviewQueryTool
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 陪诊员工具集
|
||||
* 只给本人订单与本人档案,禁止跨用户查询、财务字段与医院资料写入
|
||||
*/
|
||||
_attendantTools(userInfo) {
|
||||
return [
|
||||
...this._commonTools(),
|
||||
createAttendantEscortRecordQueryTool(userInfo),
|
||||
buildAttendantEscortRecordSetTool({ attendantId: userInfo?._id }),
|
||||
createAttendantEmployeeQueryTool(userInfo)
|
||||
];
|
||||
}
|
||||
|
||||
_genAgent(userInfo, role = "admin") {
|
||||
const rootDir = process.cwd();
|
||||
// 受限后端:锁定沙箱根并禁止读取 conf.json/.env/logs 及用户数据目录
|
||||
const backend = createRestrictedBackend({ rootDir, dataDir: config.agent.dataDir });
|
||||
@@ -184,17 +226,23 @@ export default class EscortAdminAgent {
|
||||
temperature: this.temperature
|
||||
});
|
||||
|
||||
const isAdminRole = role === "admin";
|
||||
const subagents = isAdminRole
|
||||
? [
|
||||
createPatientRecordSubagent(this.flashModel),
|
||||
// 医院资料维护只给管理员;陪诊员只读
|
||||
createHospitalInfoSubagent(this.flashModel, { allowWrite: true }),
|
||||
]
|
||||
: [createHospitalInfoSubagent(this.flashModel, { allowWrite: false })];
|
||||
|
||||
return createDeepAgent({
|
||||
name: "deep-agent",
|
||||
model: this.flashModel,
|
||||
systemPrompt: EscortAdminPrompts.buildSystemPrompt(userInfo),
|
||||
systemPrompt: EscortAdminPrompts.buildSystemPrompt(userInfo, role),
|
||||
backend,
|
||||
middleware: [todoListMiddleware()], // deepagents 1.12 起 todo 计划工具改为显式开启,多步记录操作用
|
||||
tools: [webFetchTool, webSearchTool, httpGetTool,
|
||||
getCalendarInfoTool, getYearHolidaysTool,
|
||||
escortRecordQueryTool, escortRecordSetTool,
|
||||
employeeQueryTool, employeeReviewQueryTool],
|
||||
subagents: [createPatientRecordSubagent(this.flashModel), createHospitalInfoSubagent(this.flashModel, { allowWrite: true })],
|
||||
tools: isAdminRole ? this._adminTools() : this._attendantTools(userInfo),
|
||||
subagents,
|
||||
skills: ["./agent/infra/skills/"],
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user