fix
This commit is contained in:
@@ -658,6 +658,8 @@ async function sendMessage(token, conversationId, message, onEvent, signal) {
|
||||
```
|
||||
|
||||
- “换个头”替换现有头部;“换个腿”替换整组 LEG 实例,包括左右及中性组件;“脚”对应 FOOT,不联动替换。
|
||||
- 纯换件沿用已保存的有效需求,不把模型重新描述的历史要求或“未指定新头部材质、尺寸”等说明当作新增条件;只有当轮明确附带的新要求才参与新增约束校验。
|
||||
- 换件动作仅在当轮执行,不累计历史目标。例如先“换个头”、再“单独换个左臂”,第二轮只更换左臂;模型重复输出的历史目标会在来源校验后移除,没有当轮有效目标时拒绝执行。
|
||||
- “换个左臂”只替换左侧全部实例,不为外观配套自动更换右臂;“换第二个左臂”只替换该侧按序号排列的第二个实例。也可在文字中明确当前完整实例编码。
|
||||
- 多个目标一起选件和校验,排除目标组原组件编码;固定其他组件和父子拓扑,不增加组件,不丢弃原有子组件。新增硬参数必须满足且可验证,已有缺件和未验证项保留,不允许新增硬条件违例或扩大既有数值偏差。
|
||||
- 任一目标没有替代件、连接不兼容、要求不满足或搜索达到上限时,整组保留原组合及原有效需求,仍保存新版本;未执行的新条件不会自动成为后续有效约束。后续替换可以继续基于该保留版本。
|
||||
|
||||
@@ -1271,6 +1271,82 @@ class DemandFlowApiTest {
|
||||
.isInstanceOf(com.linkerbot.cadworld.common.exception.BizException.class);
|
||||
}
|
||||
|
||||
@Test void capturedRealModelHeadReplacementAppliesWithoutInventingNewRequirements() throws Exception {
|
||||
defaultHumanoidModel(); humanoidComponents("sided");
|
||||
doAnswer(invocation -> {
|
||||
ObjectNode raw = humanoidRaw(invocation.getArgument(0));
|
||||
intent(raw, "form", "整机", "具有类人人形形态");
|
||||
raw.withArray("unmodeled").addObject().put("text", "未明确指定人形机器人的具体自由度、运动性能、载荷、尺寸、材料、外观细节、应用场景及执行任务等参数")
|
||||
.put("reason", "用户未提供具体参数").putArray("evidenceIds").add("e1"); return raw;
|
||||
}).when(model).analyze(anyList());
|
||||
Session session = create(); JsonNode first = submit(session, "m1", "给我一个人形机器人", List.of());
|
||||
duplicate("CW-HEAD_0", "CW-HEAD_0-NEW", "另一种头部");
|
||||
try (java.io.InputStream stream = getClass().getResourceAsStream("/fixtures/head-replacement-model-output.json")) {
|
||||
JsonNode captured = json.readTree(stream);
|
||||
doAnswer(invocation -> captured.deepCopy()).when(model).analyze(anyList());
|
||||
}
|
||||
JsonNode replaced = submit(session, "m2", "换个头", List.of());
|
||||
assertThat(replaced.path("match").path("change").path("status").asText()).isEqualTo("applied");
|
||||
assertThat(replaced.path("result").path("analysis").path("operation").path("targets").get(0).path("side").asText()).isEqualTo("all");
|
||||
Map<String, String> expected = codesByRole(first.path("match")); expected.put("HEAD_0:1", "CW-HEAD_0-NEW");
|
||||
assertThat(codesByRole(replaced.path("match"))).isEqualTo(expected);
|
||||
assertThat(replaced.path("result").path("analysis").path("unmodeled")).isEqualTo(first.path("result").path("analysis").path("unmodeled"));
|
||||
}
|
||||
|
||||
@Test void capturedRealModelConsecutiveReplacementDoesNotRepeatThePreviousHeadChange() throws Exception {
|
||||
defaultHumanoidModel(); humanoidComponents("sided");
|
||||
Session session = create(); JsonNode first = submit(session, "r1", "给我一个人形机器人", List.of());
|
||||
duplicate("CW-HEAD_0", "CW-HEAD_0-NEW", "另一种头部");
|
||||
replacementModel("HEAD", "all");
|
||||
JsonNode head = submit(session, "r2", "换个头", List.of());
|
||||
assertThat(head.path("match").path("change").path("status").asText()).isEqualTo("applied");
|
||||
duplicate("CW-ARM_L", "CW-ARM_L-NEW", "另一种左臂");
|
||||
try (java.io.InputStream stream = getClass().getResourceAsStream("/fixtures/consecutive-replacement-model-output.json")) {
|
||||
JsonNode captured = json.readTree(stream);
|
||||
doAnswer(invocation -> captured.deepCopy()).when(model).analyze(anyList());
|
||||
}
|
||||
JsonNode arm = submit(session, "r3", "单独换个左臂", List.of());
|
||||
assertThat(arm.path("match").path("change").path("status").asText()).isEqualTo("applied");
|
||||
assertThat(arm.path("match").path("change").path("targetInstanceIds")).hasSize(1);
|
||||
assertThat(arm.path("match").path("change").path("baseAnalysisId").asText()).isEqualTo(head.path("analysisId").asText());
|
||||
Map<String, String> expected = codesByRole(first.path("match"));
|
||||
expected.put("HEAD_0:1", "CW-HEAD_0-NEW"); expected.put("ARM_L:1", "CW-ARM_L-NEW");
|
||||
assertThat(codesByRole(arm.path("match"))).isEqualTo(expected);
|
||||
assertThat(arm.path("result").path("analysis").path("operation").path("targets")).hasSize(1);
|
||||
assertThat(generatedMatch(session, arm.path("analysisId").asText(), 3)).isEqualTo(arm.path("match"));
|
||||
}
|
||||
|
||||
@Test void headReplacementIgnoresRestatedHistoryAndInventedMissingParameters() throws Exception {
|
||||
defaultHumanoidModel(); humanoidComponents("sided");
|
||||
doAnswer(invocation -> {
|
||||
ObjectNode raw = humanoidRaw(invocation.getArgument(0));
|
||||
intent(raw, "form", "整机", "具有类人人形形态");
|
||||
raw.withArray("unmodeled").addObject().put("text", "用户未指定自由度、运动性能、载荷、尺寸、材料等参数")
|
||||
.put("reason", "未给出具体参数").putArray("evidenceIds").add("e1"); return raw;
|
||||
}).when(model).analyze(anyList());
|
||||
Session session = create(); JsonNode first = submit(session, "m1", "给我一个人形机器人", List.of());
|
||||
duplicate("CW-HEAD_0", "CW-HEAD_0-NEW", "另一种头部");
|
||||
doAnswer(invocation -> {
|
||||
List<DemandMessage> history = invocation.getArgument(0);
|
||||
ObjectNode raw = replacementRaw(history, "HEAD", "all");
|
||||
raw.withArray("observations").addObject().put("id", "old-shape").put("messageId", history.getFirst().id())
|
||||
.put("imageId", "").put("kind", "user_text").put("content", history.getFirst().text()).put("clarity", "clear").putArray("region");
|
||||
intent(raw, "form", "整机", "保持双足类人外形");
|
||||
((ObjectNode) raw.path("intentItems").get(0)).putArray("evidenceIds").add("old-shape");
|
||||
raw.withArray("unmodeled").addObject().put("text", "未明确人形机器人的具体尺寸与质量")
|
||||
.put("reason", "历史需求缺少参数").putArray("evidenceIds").add("old-shape");
|
||||
intent(raw, "other", "头部", "替换当前头部组件");
|
||||
raw.withArray("unmodeled").addObject().put("text", "未指定新头部的外形、材料和自由度")
|
||||
.put("reason", "当前替换未给出具体参数").putArray("evidenceIds").add("e1"); return raw;
|
||||
}).when(model).analyze(anyList());
|
||||
JsonNode replaced = submit(session, "m2", "换个头", List.of());
|
||||
assertThat(replaced.path("match").path("change").path("status").asText()).isEqualTo("applied");
|
||||
Map<String, String> expected = codesByRole(first.path("match")); expected.put("HEAD_0:1", "CW-HEAD_0-NEW");
|
||||
assertThat(codesByRole(replaced.path("match"))).isEqualTo(expected);
|
||||
assertThat(replaced.path("result").path("analysis").path("intentItems").toString()).contains("具有类人人形形态").doesNotContain("保持双足类人外形", "替换当前头部组件");
|
||||
assertThat(replaced.path("result").path("analysis").path("unmodeled")).isEqualTo(first.path("result").path("analysis").path("unmodeled"));
|
||||
}
|
||||
|
||||
@ParameterizedTest @ValueSource(booleans = {true, false})
|
||||
void replacementCannotCommitAfterNewMessageOrCancellationAndCancelledRevisionCanRetry(boolean supersede) throws Exception {
|
||||
defaultHumanoidModel(); humanoidComponents("sided");
|
||||
|
||||
+7
-1
@@ -81,7 +81,13 @@ public final class DemandMatchService {
|
||||
for (JsonNode check : baseline.match().path("requirementChecks")) previous.put(checkKey(baseline.analysis().result(), check), check.path("status").asText());
|
||||
for (JsonNode check : requirements.check(revised, response)) {
|
||||
if (!"must".equals(check.path("priority").asText()) || "satisfied".equals(check.path("status").asText())) continue;
|
||||
if (!check.path("status").asText().equals(previous.get(checkKey(revised, check)))) return false;
|
||||
String priorStatus = previous.get(checkKey(revised, check));
|
||||
if (!check.path("status").asText().equals(priorStatus)) {
|
||||
log.info("局部替换要求校验未通过 requestId={} baseAnalysisId={} intentId={} field={} status={} previousStatus={}",
|
||||
RequestIdHolder.get(), baseline.analysis().analysisId(), check.path("intentId").asText(),
|
||||
check.path("field").asText(), check.path("status").asText(), priorStatus);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
+17
-1
@@ -23,7 +23,6 @@ public final class DemandNormalizer {
|
||||
JsonNode canonical = canonicalCategories(input);
|
||||
rules.validate(canonical);
|
||||
ObjectNode raw = (ObjectNode) canonical;
|
||||
DemandOperation.validate(raw, history.getLast());
|
||||
Map<String, DemandMessage> messages = new LinkedHashMap<>();
|
||||
Map<String, String> attachments = new LinkedHashMap<>();
|
||||
for (DemandMessage message : history) if ("user".equals(message.role())) {
|
||||
@@ -52,6 +51,10 @@ public final class DemandNormalizer {
|
||||
}
|
||||
}
|
||||
}
|
||||
DemandOperation.validate(raw, history.getLast());
|
||||
if ("replace".equals(raw.path("operation").path("type").asText())) {
|
||||
retainReplacementChanges(raw, evidence, history.getLast());
|
||||
}
|
||||
if (!raw.path("questions").isEmpty()) warnings.add("部分需求存在歧义,已使用明确的信息和默认方案生成候选。");
|
||||
Set<String> intentIds = new LinkedHashSet<>();
|
||||
for (JsonNode item : raw.path("intentItems")) {
|
||||
@@ -146,6 +149,19 @@ public final class DemandNormalizer {
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 历史有效条件以已保存快照为准,不接受模型把旧要求换一种说法后作为本轮新条件。 */
|
||||
private void retainReplacementChanges(ObjectNode raw, Map<String, JsonNode> evidence, DemandMessage latest) {
|
||||
boolean operationOnly = DemandOperation.onlyReplacementInstruction(latest, raw.path("operation"));
|
||||
for (String field : List.of("intentItems", "keywords", "robotTypes", "partCategories", "excludedCategories", "unmodeled", "constraints")) {
|
||||
ArrayNode changes = json.createArrayNode();
|
||||
for (JsonNode item : raw.path(field)) {
|
||||
List<JsonNode> sources = resolve(item, evidence);
|
||||
if (!operationOnly && sources.stream().anyMatch((JsonNode source) -> latest.id().equals(source.path("messageId").asText()))) changes.add(item);
|
||||
}
|
||||
raw.set(field, changes);
|
||||
}
|
||||
}
|
||||
|
||||
/** 兼容历史模型编码,仅规范化类别字段,保留用户原文和证据。 */
|
||||
private JsonNode canonicalCategories(JsonNode input) {
|
||||
if (input == null) return null;
|
||||
|
||||
+82
@@ -1,14 +1,19 @@
|
||||
package com.linkerbot.cadworld.service.demand;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.linkerbot.cadworld.common.api.RequestIdHolder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/** 操作证据必须来自当轮用户;装配角色由真实实例确定,不能按组件真实类别猜侧别。 */
|
||||
@Slf4j
|
||||
final class DemandOperation {
|
||||
private static final Map<String, String> NOUNS = Map.of("TORSO", "躯干|身体", "HEAD", "头|脑袋", "WAIST", "腰",
|
||||
"ARM", "手臂|胳膊|臂", "HAND", "手(?!臂)", "LEG", "腿|足", "FOOT", "脚", "TAIL", "尾");
|
||||
@@ -22,6 +27,7 @@ final class DemandOperation {
|
||||
.matcher(latest.text()).find()) throw DemandRules.invalid("operation.type");
|
||||
return;
|
||||
}
|
||||
retainCurrentTargets(raw, latest);
|
||||
if (operation.path("targets").isEmpty()) throw DemandRules.invalid("operation.targets");
|
||||
for (JsonNode target : operation.path("targets")) {
|
||||
List<String> quotes = new ArrayList<>();
|
||||
@@ -37,6 +43,11 @@ final class DemandOperation {
|
||||
String quote = String.join(" ", quotes);
|
||||
String family = target.path("family").asText();
|
||||
String noun = NOUNS.get(family);
|
||||
// 单侧独有部位的 0 就是无侧别;臂/腿的未指定侧别不能被模型默认成仅换中性实例。
|
||||
if ("0".equals(target.path("side").asText()) && (Set.of("HEAD", "TORSO", "WAIST", "TAIL").contains(family)
|
||||
|| !Pattern.compile("中性|通用|不分左右|(?:" + family + "|" + noun + ")_0", Pattern.CASE_INSENSITIVE).matcher(quote).find())) {
|
||||
((ObjectNode) target).put("side", "all");
|
||||
}
|
||||
String requestedInstance = target.path("instanceId").asText();
|
||||
List<String> directives = new ArrayList<>();
|
||||
for (String clause : quote.split("[,,。;;!!\\n]")) {
|
||||
@@ -73,6 +84,77 @@ final class DemandOperation {
|
||||
}
|
||||
}
|
||||
|
||||
/** observations 已通过真实历史校验;历史动作不能累计成为本轮的换件范围。 */
|
||||
private static void retainCurrentTargets(JsonNode raw, DemandMessage latest) {
|
||||
ArrayNode targets = (ArrayNode) raw.path("operation").path("targets");
|
||||
List<JsonNode> historicalTargets = new ArrayList<>();
|
||||
int ignored = 0;
|
||||
for (int index = targets.size() - 1; index >= 0; index--) {
|
||||
ObjectNode target = (ObjectNode) targets.get(index);
|
||||
ArrayNode currentEvidence = target.arrayNode();
|
||||
for (JsonNode evidenceId : target.path("evidenceIds")) {
|
||||
JsonNode source = null;
|
||||
for (JsonNode observation : raw.path("observations")) if (evidenceId.asText().equals(observation.path("id").asText())) source = observation;
|
||||
if (source == null || !"user_text".equals(source.path("kind").asText())) throw DemandRules.invalid("operation.evidenceIds");
|
||||
if (latest.id().equals(source.path("messageId").asText())) currentEvidence.add(evidenceId);
|
||||
}
|
||||
if (currentEvidence.isEmpty()) {
|
||||
historicalTargets.add(target);
|
||||
targets.remove(index);
|
||||
ignored++;
|
||||
} else target.set("evidenceIds", currentEvidence);
|
||||
}
|
||||
// 本轮再次指定历史部位时,必须有对应的当轮目标,不能静默缩小多目标提交范围。
|
||||
for (JsonNode historical : historicalTargets) {
|
||||
boolean covered = false;
|
||||
for (JsonNode current : targets) covered |= sameSelector(historical, current);
|
||||
if (!covered && requestedAgain(historical, latest.text())) throw DemandRules.invalid("operation.evidenceIds");
|
||||
}
|
||||
if (ignored > 0) log.info("局部替换忽略历史操作目标 requestId={} messageId={} ignoredTargetCount={} currentTargetCount={}",
|
||||
RequestIdHolder.get(), latest.id(), ignored, targets.size());
|
||||
}
|
||||
|
||||
private static boolean sameSelector(JsonNode first, JsonNode second) {
|
||||
for (String field : List.of("family", "side", "instanceId", "ordinal")) {
|
||||
String left = first.path(field).asText();
|
||||
String right = second.path(field).asText();
|
||||
if ("side".equals(field) && Set.of("HEAD", "TORSO", "WAIST", "TAIL").contains(first.path("family").asText())) {
|
||||
if ("0".equals(left)) left = "all";
|
||||
if ("0".equals(right)) right = "all";
|
||||
}
|
||||
if (!left.equals(right)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean requestedAgain(JsonNode target, String text) {
|
||||
String family = target.path("family").asText();
|
||||
for (String clause : text.split("[,,。;;!!\\n]")) {
|
||||
if (!clause.matches("(?is).*(?:换|替代|replace|change).*")
|
||||
|| clause.matches("(?s).*(?:不要|不想|不用|不需要|禁止|别动|不动|不换|别换|保留|保持).*")) continue;
|
||||
if (Pattern.compile("(?:" + NOUNS.get(family) + "|" + family + "_)", Pattern.CASE_INSENSITIVE).matcher(clause).find()) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 纯换件不表示新增参数;不能把模型补写的“未指定材质/性能”等说明当作硬要求。 */
|
||||
static boolean onlyReplacementInstruction(DemandMessage latest, JsonNode operation) {
|
||||
if (!latest.images().isEmpty()) return false;
|
||||
String remainder = latest.text();
|
||||
for (JsonNode target : operation.path("targets")) {
|
||||
String instance = target.path("instanceId").asText();
|
||||
if (!instance.isEmpty()) remainder = remainder.replace(instance, "");
|
||||
remainder = remainder.replaceAll("(?i)" + target.path("family").asText() + "_[LR0]", "");
|
||||
remainder = remainder.replaceAll("(?i)(?:" + NOUNS.get(target.path("family").asText()) + ")_[LR0]", "");
|
||||
}
|
||||
remainder = remainder.replaceAll("(?:其他|其余)(?:组件|部件)?(?:都)?(?:保持)?不变", "")
|
||||
.replaceAll("第\\s*(?:[0-9]+|[一二两三四五六七八九十]+)\\s*(?:个|条|只|件)?", "")
|
||||
.replaceAll("(?:不分左右|中性|通用|左侧|右侧|左|右)?(?:头部|脑袋|头|躯干|身体|腰部|腰|手臂|胳膊|臂|手部|手|腿部|腿|足部|足|脚部|脚|尾巴|尾部|尾)", "")
|
||||
.replaceAll("(?:麻烦|帮我|给我|我想|想要|请|把|将|更换|替换|替代|换|另一个|一个|一条|一只|一组|整组|全部|所有|单独|重新|一下|再|也|成|掉|个|条|只|和|及|与|并)", "")
|
||||
.replaceAll("[\\s\\p{Punct},。!?、;:]+", "");
|
||||
return remainder.isEmpty();
|
||||
}
|
||||
|
||||
static Set<String> targets(JsonNode operation, JsonNode match) {
|
||||
Set<String> result = new LinkedHashSet<>();
|
||||
for (JsonNode selector : operation.path("targets")) {
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
9. keywords 提取主体、仿生对象、形态、功能、环境、材料、尺寸、数值、偏好及禁止项;规范化同义词但保留否定。source=explicit 或 inferred,推断注明 reason,不能推断数字、牌号或性能。否定词保留在 term 中,如“不要飞行”。
|
||||
10. 后续明确修改覆盖旧值,补充累加;不得把各轮旧值全部 AND 叠加。用户后来补充“单位是N·m”时结合之前用户数值,将两轮证据都引用,不能从助手示例获取数值。用户否定旧需求后,将其从当前有效关键词和约束中移除。
|
||||
|
||||
10a. operation 始终输出。普通生成或整体重新生成使用 {"type":"generate","targets":[]}。当轮用户要求在现有组合上换件时使用 type=replace。targets 每项包含 family=TORSO/HEAD/WAIST/ARM/HAND/LEG/FOOT/TAIL、side=all/L/R/0、instanceId、ordinal、evidenceIds;未指定实例和序号时两个字符串为空。腿、足对应 LEG,脚对应 FOOT。未说明左右的“换个腿”使用 family=LEG、side=all,后端替换现有整组实例;“换个左臂”使用 ARM、L,仅替换该侧。指定“第2个左臂”时 ordinal="2";中文序号转换为数字。明确写出现有完整实例编码才填写 instanceId;不猜序号或实例编码,不额外替换手、脚或对侧。每项证据必须是当轮用户真实文字,必须覆盖替换动作和对应部位;每个指定侧分别列项,不将“换左臂和右腿”扩展成整个臂和腿组。没有现有方案时仍如实输出 replace,由后端说明缺少基准。
|
||||
10b. 服务端提供的 effectiveRequirements 是当前已保存的有效需求,优先于历史里尚未执行或失败的改动。输出在该有效需求上应用当轮修改后的完整需求;历史只提供真实来源证据,不能重新启用失败条件。替换动作只进入 operation,不作为数量、部件包含、硬性能或 unmodeled 条目;“换个头”不改变原构型和数量,不把“个”解析为新增一个头。附带的新参数仍进入 constraints/intentItems;针对未指定左右部位的新条件,按上下文实际装配角色分别输出。不得因换单侧而自行要求对侧也换。
|
||||
10a. operation 始终输出。普通生成或整体重新生成使用 {"type":"generate","targets":[]}。当轮用户要求在现有组合上换件时使用 type=replace。targets 每项包含 family=TORSO/HEAD/WAIST/ARM/HAND/LEG/FOOT/TAIL、side=all/L/R/0、instanceId、ordinal、evidenceIds;未指定实例和序号时两个字符串为空。腿、足对应 LEG,脚对应 FOOT。未说明左右的“换个腿”使用 family=LEG、side=all,后端替换现有整组实例;“换个左臂”使用 ARM、L,仅替换该侧。指定“第2个左臂”时 ordinal="2";中文序号转换为数字。明确写出现有完整实例编码才填写 instanceId;不猜序号或实例编码,不额外替换手、脚或对侧。operation 是仅执行一次的当轮动作,targets 不累计历史换件目标。先说“换个头”、下一轮说“单独换个左臂”时,下一轮只输出 ARM、L,不再输出 HEAD。每项证据必须是当轮用户真实文字,必须覆盖替换动作和对应部位;每个指定侧分别列项,不将“换左臂和右腿”扩展成整个臂和腿组。没有现有方案时仍如实输出 replace,由后端说明缺少基准。
|
||||
10b. 服务端提供的 effectiveRequirements 是当前已保存的有效需求,优先于历史里尚未执行或失败的改动。输出在该有效需求上应用当轮修改后的完整需求;历史只提供真实来源证据,不能重新启用失败条件。替换动作只进入 operation,不作为数量、部件包含、硬性能或 unmodeled 条目;“换个头”不改变原构型和数量,不把“个”解析为新增一个头。未提供的参数不是需求,不把“未指定新头部的材质、尺寸、自由度”等补参数说明放进 intentItems、constraints 或 unmodeled。附带的新参数仍进入 constraints/intentItems;针对未指定左右部位的新条件,按上下文实际装配角色分别输出。不得因换单侧而自行要求对侧也换。HEAD/TORSO/WAIST/TAIL 没有左右侧,替换使用 side=all;臂、手、腿、脚未说明侧别也使用 all,不默认仅换 _0 实例。
|
||||
|
||||
三、类型与部位
|
||||
11. 配置目录:{{CATALOG_JSON}}。robotTypes 只使用目录中的模板 id;程序展开 requiredParts,你不输出该字段。能辨认形态时选对应模板,图片推断标 inferred。未指定形态时 robotTypes=[],后端使用默认模板并说明。目录不代表库存齐全或能力达标。
|
||||
|
||||
+79
@@ -303,6 +303,45 @@ class DemandNormalizerTest {
|
||||
assertThatThrownBy(() -> normalizer.normalize(raw, text(text))).isInstanceOf(BizException.class);
|
||||
}
|
||||
|
||||
@Test void consecutiveReplacementKeepsOnlyCurrentTargetsAndEvidence() {
|
||||
String previous = "换个头";
|
||||
String current = "单独换个左臂";
|
||||
ObjectNode raw = minimal(current);
|
||||
((ObjectNode) raw.path("observations").get(0)).put("messageId", "m2");
|
||||
raw.withArray("observations").addObject().put("id", "old").put("messageId", "m1").put("imageId", "")
|
||||
.put("kind", "user_text").put("content", previous).put("clarity", "clear").putArray("region");
|
||||
raw.putObject("operation").put("type", "replace").putArray("targets").addObject()
|
||||
.put("family", "HEAD").put("side", "all").put("instanceId", "").put("ordinal", "").putArray("evidenceIds").add("old");
|
||||
((ObjectNode) raw.path("operation")).withArray("targets").addObject().put("family", "ARM").put("side", "L").put("instanceId", "").put("ordinal", "")
|
||||
.putArray("evidenceIds").add("old").add("e1");
|
||||
List<DemandMessage> history = List.of(new DemandMessage("m1", "user", previous, List.of()), new DemandMessage("m2", "user", current, List.of()));
|
||||
JsonNode operation = normalizer.normalize(raw, history).analysis().path("operation");
|
||||
assertThat(operation.path("targets")).hasSize(1);
|
||||
assertThat(operation.path("targets").get(0).path("family").asText()).isEqualTo("ARM");
|
||||
assertThat(operation.path("targets").get(0).path("evidenceIds")).containsExactly(json.getNodeFactory().textNode("e1"));
|
||||
((ObjectNode) raw.path("observations").get(1)).put("content", "伪造的历史换头指令");
|
||||
assertThatThrownBy(() -> normalizer.normalize(raw, history)).isInstanceOf(BizException.class);
|
||||
}
|
||||
|
||||
@Test void discardingHistoryCannotSilentlyDropATargetRequestedAgainInTheCurrentGroup() {
|
||||
String previous = "换个头";
|
||||
String current = "换个头和左臂";
|
||||
ObjectNode raw = minimal(current);
|
||||
((ObjectNode) raw.path("observations").get(0)).put("messageId", "m2");
|
||||
raw.withArray("observations").addObject().put("id", "old").put("messageId", "m1").put("imageId", "")
|
||||
.put("kind", "user_text").put("content", previous).put("clarity", "clear").putArray("region");
|
||||
ObjectNode operation = raw.putObject("operation").put("type", "replace");
|
||||
operation.putArray("targets").addObject().put("family", "HEAD").put("side", "all").put("instanceId", "").put("ordinal", "")
|
||||
.putArray("evidenceIds").add("old");
|
||||
operation.withArray("targets").addObject().put("family", "ARM").put("side", "L").put("instanceId", "").put("ordinal", "")
|
||||
.putArray("evidenceIds").add("e1");
|
||||
List<DemandMessage> history = List.of(new DemandMessage("m1", "user", previous, List.of()), new DemandMessage("m2", "user", current, List.of()));
|
||||
assertThatThrownBy(() -> normalizer.normalize(raw, history)).isInstanceOf(BizException.class);
|
||||
operation.withArray("targets").addObject().put("family", "HEAD").put("side", "0").put("instanceId", "").put("ordinal", "")
|
||||
.putArray("evidenceIds").add("e1");
|
||||
assertThat(normalizer.normalize(raw, history).analysis().path("operation").path("targets")).hasSize(2);
|
||||
}
|
||||
|
||||
@Test void supportsChineseOrdinalWithoutInventingAnInstanceCode() {
|
||||
String text = "换第二个左臂";
|
||||
ObjectNode raw = minimal(text);
|
||||
@@ -336,6 +375,46 @@ class DemandNormalizerTest {
|
||||
assertThatThrownBy(() -> normalizer.normalize(raw, text(text))).isInstanceOf(BizException.class);
|
||||
}
|
||||
|
||||
@ParameterizedTest @ValueSource(strings = {"换个头", "请帮我换一个头部", "把头部换成另一个", "换个头,其他部件保持不变"})
|
||||
void plainReplacementCannotCreateMissingParameterRequirements(String text) {
|
||||
ObjectNode raw = minimal(text);
|
||||
raw.putObject("operation").put("type", "replace").putArray("targets").addObject()
|
||||
.put("family", "HEAD").put("side", "0").put("instanceId", "").put("ordinal", "").putArray("evidenceIds").add("e1");
|
||||
raw.withArray("unmodeled").addObject().put("text", "未指定新头部的材质、尺寸、自由度")
|
||||
.put("reason", "未给出具体参数").putArray("evidenceIds").add("e1");
|
||||
DemandResult result = normalizer.normalize(raw, text(text));
|
||||
assertThat(result.analysis().path("operation").path("targets").get(0).path("side").asText()).isEqualTo("all");
|
||||
assertThat(result.analysis().path("unmodeled")).isEmpty();
|
||||
assertThat(result.request().path("partRequirements")).isEmpty();
|
||||
}
|
||||
|
||||
@Test void replacementUsesOnlyCurrentSourceChangesAndKeepsAnExplicitNewParameter() {
|
||||
String initial = "人形机器人,20自由度";
|
||||
String current = "换个头,头部质量不超过2kg";
|
||||
ObjectNode raw = minimal(current);
|
||||
((ObjectNode) raw.path("observations").get(0)).put("messageId", "m2");
|
||||
raw.withArray("observations").addObject().put("id", "old").put("messageId", "m1").put("imageId", "")
|
||||
.put("kind", "user_text").put("content", initial).put("clarity", "clear").putArray("region");
|
||||
raw.putObject("operation").put("type", "replace").putArray("targets").addObject()
|
||||
.put("family", "HEAD").put("side", "all").put("instanceId", "").put("ordinal", "").putArray("evidenceIds").add("e1");
|
||||
constraint(raw, "robot", "degreesOfFreedom", "20", "eq", null, "old");
|
||||
constraint(raw, "part", "massKg", "2kg", "lte", "HEAD_0", "e1");
|
||||
DemandResult result = normalizer.normalize(raw, List.of(new DemandMessage("m1", "user", initial, List.of()), new DemandMessage("m2", "user", current, List.of())));
|
||||
assertThat(result.request().path("robotRequirements").has("degreesOfFreedom")).isFalse();
|
||||
assertThat(result.request().path("partRequirements").get(0).path("massKg").decimalValue()).isEqualByComparingTo("2");
|
||||
assertThat(result.analysis().path("constraints")).hasSize(1);
|
||||
assertThat(raw.path("constraints")).hasSize(2);
|
||||
}
|
||||
|
||||
@ParameterizedTest @ValueSource(strings = {"换个通用臂", "换个ARM_0", "换个臂_0"})
|
||||
void explicitNeutralLimbDoesNotExpandToBothSides(String text) {
|
||||
ObjectNode raw = minimal(text);
|
||||
raw.putObject("operation").put("type", "replace").putArray("targets").addObject()
|
||||
.put("family", "ARM").put("side", "0").put("instanceId", "").put("ordinal", "").putArray("evidenceIds").add("e1");
|
||||
DemandResult result = normalizer.normalize(raw, text(text));
|
||||
assertThat(result.analysis().path("operation").path("targets").get(0).path("side").asText()).isEqualTo("0");
|
||||
}
|
||||
|
||||
private JsonNode fixture(String name) throws Exception {
|
||||
try (InputStream stream = getClass().getResourceAsStream("/robot-demand/" + name + ".json")) { return json.readTree(stream); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user