- 新增 IntentShortcut:说「我要请假」这类只有意图的短句时,
编排器在调模型前先做窄匹配(去空白标点 + 剥前后缀后正好等于标准说法、
且原句 ≤12 字),命中即直接下发表单并跳过 LLM,消除「偶尔不准」。
- 请假拆两个工具:leave.submit(type=2 学生申请,待审核)与
leave.register(type=1 教师帮助登记,免审,必填 classId+stuId)。
RolePolicy 分别归入 STUDENT_ONLY / STAFF_ONLY,模型视野里只有一个。
- ToolDef.fixed 由 Map<String,String> 改为 Map<String,Object>:
type/continuousLeave 必须按数字下发,字符串会被模块侧 Jackson 判非法。
- ToolParam/FormField 新增 optionsKeys(valueKey,labelKey) 与 dependsOn:
下拉改为下发 BrowserCall optionCall(含 {{占位符}}),由前端带用户登录态
代取后按 valueKey/labelKey 映射;stuId 依赖 classId,选完班级才解锁。
ToolExecutor#prepareOptionCall 不做必填校验,只拼占位查询串。
- 修 leave.types/myClasses/classStudents 描述,明确「填表时不要先调它」。
- README:4.2 能力划分更新 + 新增「请假为何分两个工具」;5.1 增加
5.1.0 裸意图直达(含身份对照表)与表单 JSON 示例。
- 测试:新增 IntentDirectFormTest(5)、LeaveIntentFormTest(9),
以「被调用即失败」的假 LLM 断言「我要请假」不经过大模型;
RolePermissionTest 补 leave.submit/register 的互斥断言。全量 62/62 通过。
This commit is contained in:
@@ -62,17 +62,19 @@ public class AgentOrchestrator {
|
||||
private final ToolExecutor executor;
|
||||
private final SessionStore sessionStore;
|
||||
private final AgentAuditService auditService;
|
||||
private final IntentShortcut intentShortcut;
|
||||
|
||||
@Autowired
|
||||
public AgentOrchestrator(AgentSettings settings, LlmClient llmClient, ToolRegistry registry,
|
||||
ToolExecutor executor, SessionStore sessionStore,
|
||||
AgentAuditService auditService) {
|
||||
AgentAuditService auditService, IntentShortcut intentShortcut) {
|
||||
this.settings = settings;
|
||||
this.llmClient = llmClient;
|
||||
this.registry = registry;
|
||||
this.executor = executor;
|
||||
this.sessionStore = sessionStore;
|
||||
this.auditService = auditService;
|
||||
this.intentShortcut = intentShortcut;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,6 +113,13 @@ public class AgentOrchestrator {
|
||||
ChatResponse resp = new ChatResponse();
|
||||
resp.setSessionId(session.getSessionId());
|
||||
|
||||
// ★ 裸意图直达:「我要请假」这类只有意图、没有细节的句子,直接按身份弹表单,
|
||||
// 不交给大模型决策(模型可能先去查字典、或自行编造时间直接提交,都不合预期)。
|
||||
ChatResponse direct = directForm(req.getMessage(), caller, authorizedModIds, roles, session, out);
|
||||
if (direct != null) {
|
||||
return direct;
|
||||
}
|
||||
|
||||
List<ChatMessage> messages = session.getMessages();
|
||||
if (messages.isEmpty()) {
|
||||
messages.add(ChatMessage.system(buildSystemPrompt(userDesc, authorizedModIds, roles)));
|
||||
@@ -398,6 +407,54 @@ public class AgentOrchestrator {
|
||||
|
||||
/* ==================== 表单:缺参拦截 与 直提交 ==================== */
|
||||
|
||||
/**
|
||||
* 裸意图直达:命中「只有意图、没有细节」的句子(如「我要请假」)时,
|
||||
* 按当前身份直接构建表单返回 —— 学生拿「请假申请」,教职工拿「请假登记(帮学生请假)」。
|
||||
*
|
||||
* <p>判据与能力目录 / 模型侧过滤**完全一致**({@code visibleTools}),
|
||||
* 因此不存在「直达出一个本身份无权使用的表单」的可能;不命中或不适用就返回 {@code null},
|
||||
* 照常交给大模型处理。
|
||||
*/
|
||||
private ChatResponse directForm(String message, CallerContext caller, Set<Integer> authorizedModIds,
|
||||
Set<AgentRole> roles, SessionStore.SessionState session, EventSink out) {
|
||||
IntentShortcut.Intent intent = intentShortcut.match(message);
|
||||
if (intent == null) {
|
||||
return null;
|
||||
}
|
||||
boolean staff = roles != null
|
||||
&& (roles.contains(AgentRole.TEACHER) || roles.contains(AgentRole.ADMIN));
|
||||
String toolName = staff ? intent.getStaffTool() : intent.getStudentTool();
|
||||
ToolDef def = toolName == null ? null : registry.get(toolName);
|
||||
if (def == null) {
|
||||
return null;
|
||||
}
|
||||
boolean visible = registry.visibleTools(authorizedModIds, roles).stream()
|
||||
.anyMatch(t -> t.getName().equals(def.getName()));
|
||||
if (!visible) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, Object> empty = new LinkedHashMap<>();
|
||||
List<ToolParam> missing = missingRequired(def, empty);
|
||||
if (missing.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String reply = staff ? intent.getStaffReply() : intent.getStudentReply();
|
||||
FormRequest form = buildForm(def, empty, missing, caller);
|
||||
form.setDescription(reply);
|
||||
|
||||
ChatResponse resp = new ChatResponse();
|
||||
resp.setSessionId(session.getSessionId());
|
||||
resp.setForm(form);
|
||||
resp.setReply(reply);
|
||||
out.emit(AgentEvent.of("form", "需要补充信息:" + String.join("、", form.getMissing())));
|
||||
log.info("意图直达弹表单:session={} 身份={} 工具={}", session.getSessionId(),
|
||||
staff ? "教职工" : "学生", def.getName());
|
||||
finish(resp, session, 0, 0, out);
|
||||
return resp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 预检本轮的写操作工具:若有必填参数缺失,则撤销刚加入的 assistant 消息并返回一张补全表单。
|
||||
*
|
||||
@@ -592,10 +649,21 @@ public class AgentOrchestrator {
|
||||
List<String> missingLabels = new ArrayList<>();
|
||||
for (ToolParam p : missing) {
|
||||
FormField f = p.toFormField();
|
||||
// 字典类字段(如请假类型):先跑一次它的来源工具,把可选项直接摆到下拉框里
|
||||
// 判据是「声明了来源」而不是「当前没有选项」—— 参数可能已带内置兜底,
|
||||
// 实时字典取到就覆盖兜底,取不到则保留兜底(下拉框不会退化成数字框)。
|
||||
if (StringUtils.isNotBlank(p.getOptionsFrom())) {
|
||||
// 字典类字段(如请假类型、班级、学生):优先交给前端用**用户自己的登录态**代取。
|
||||
//
|
||||
// ★ 为什么不在这里直接执行字典工具:业务模块的用户态接口只认浏览器 CAS 会话,
|
||||
// 服务端直调必然 code=302(README 第 11 节)。以前"服务端取一次、失败就退化成
|
||||
// 数字输入框",结果教师登记请假时得手填 classId/stuId 编号——根本没法用。
|
||||
// 现在把「去哪儿取」以 optionCall 下发(依赖字段留 {{占位符}}),
|
||||
// 前端在学生选完班级后自己拉名单,下拉框才是真的下拉框。
|
||||
BrowserToolRequest.BrowserCall optionCall = buildOptionCall(p, caller);
|
||||
if (optionCall != null) {
|
||||
f.setOptionCall(optionCall);
|
||||
f.setWidget("select");
|
||||
// 有内置兜底的(如请假类型 病假/事假)先摆上,前端取到学校实时字典后覆盖
|
||||
f.setOptionsFallback(p.isUsingFallbackOptions());
|
||||
} else if (StringUtils.isNotBlank(p.getOptionsFrom())) {
|
||||
// 没有代取通道(模块未配地址 / 越权)→ 退回服务端取一次,取不到就保留原控件让用户手填编号
|
||||
List<Map<String, String>> opts = dictOptions(p.getOptionsFrom(), caller, dictCache);
|
||||
if (!opts.isEmpty()) {
|
||||
f.setOptions(opts);
|
||||
@@ -666,9 +734,43 @@ public class AgentOrchestrator {
|
||||
}
|
||||
}
|
||||
}
|
||||
// 服务端取不到实时字典时,至少把内置兜底的中文名翻出来(leaveType=1 → 病假),
|
||||
// 免得「已识别」标签里显示一个裸编号让用户看不懂。
|
||||
if (p.getFallbackValues() != null && !p.getFallbackValues().isEmpty()) {
|
||||
int idx = p.getFallbackValues().indexOf(text);
|
||||
if (idx >= 0 && p.getFallbackLabels() != null && idx < p.getFallbackLabels().size()) {
|
||||
return p.getFallbackLabels().get(idx);
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成某个字段「浏览器代取选项」的调用描述;不可用时返回 {@code null}。
|
||||
*
|
||||
* <p>用于业务模块的字典接口(班级、学生名单、请假类型…)——这些接口只认浏览器 CAS 会话,
|
||||
* 服务端直调必然 302,所以只能把地址交给前端、由前端带用户登录态去取。
|
||||
*/
|
||||
private BrowserToolRequest.BrowserCall buildOptionCall(ToolParam p, CallerContext caller) {
|
||||
if (StringUtils.isBlank(p.getOptionsFrom())) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
ToolDef dict = registry.get(p.getOptionsFrom());
|
||||
if (dict == null || !dict.isReadOnly()) {
|
||||
return null;
|
||||
}
|
||||
// 与 fetchDictOptions 同源的角色约束:越权的字典不外泄地址
|
||||
if (settings.isEnforceRolePermission() && !dict.allowsAny(caller.getRoles())) {
|
||||
return null;
|
||||
}
|
||||
return executor.prepareOptionCall(dict, p.getDependsOn());
|
||||
} catch (Exception e) {
|
||||
log.warn("生成代取选项描述失败 {}:{}", p.getOptionsFrom(), e.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 取字典选项(带缓存;取不到返回空列表,调用方据此退化为普通输入框) */
|
||||
private List<Map<String, String>> dictOptions(String toolName, CallerContext caller,
|
||||
Map<String, List<Map<String, String>>> cache) {
|
||||
@@ -898,8 +1000,12 @@ public class AgentOrchestrator {
|
||||
sb.append("6. 回答用简体中文,简洁直白,先给结论再给必要细节;不要输出 JSON 原文、不要暴露内部工具名与技术细节。\n");
|
||||
sb.append("7. 涉及审批、删除、批量操作等有副作用的动作,执行后要明确告知“已完成什么、影响范围”。\n");
|
||||
|
||||
sb.append("8. ★ 权限边界:你**只能**调用上面列出的能力。如果用户请求需要其它身份(例如学生要求“审批请假”“发布公告”,")
|
||||
.append("或教师要求“提交学生的请假申请”),要如实说明“该操作需要 XX 身份”,不要试图越权,也不要承诺你会去办。\n");
|
||||
sb.append("8. ★ 权限边界:你**只能**调用上面列出的能力。如果用户请求需要其它身份(例如学生要求「审批请假」「发布公告」),")
|
||||
.append("要如实说明「该操作需要 XX 身份」,不要试图越权,也不要承诺你会去办。\n");
|
||||
|
||||
sb.append("9. ★ 请假要分清两条链路:「提交请假申请」= 学生给自己请假(提交后待班主任审核);")
|
||||
.append("「帮学生登记请假」= 教师给学生登记(免审直接生效,必须先选班级与学生)。")
|
||||
.append("下发给你的工具已按当前身份过滤,按标题选即可,不要互换。\n");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user