checkpoint: 提交当前工作区代码(同义词配置初版 + 既有流程改动)
- 新增 inq_synonym 表、62组同义词种子与智能体只读接口 - Python 同义词加载/归一化与 1板/2箱 机读 - 同步当前分支既有协作、报价识别、陆运等改动
This commit is contained in:
@@ -10,7 +10,7 @@ from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
from typing import Any, Optional
|
||||
|
||||
from agent.policy import inquiry_copy as copy
|
||||
from agent.schema.field_validate import (
|
||||
@@ -24,6 +24,7 @@ from agent.schema.land_options import (
|
||||
drop_land_prompt_echo,
|
||||
harvest_land_options,
|
||||
land_cargo_name,
|
||||
land_option_gaps,
|
||||
option_prompt,
|
||||
scrub_land_placeholder_ports,
|
||||
validate_land_options,
|
||||
@@ -293,12 +294,181 @@ def _segment_required_check(seg: Segment) -> tuple[dict[str, Any], dict[str, str
|
||||
return check, facts
|
||||
|
||||
|
||||
def refresh_segments(segments: list[Segment]) -> tuple[bool, list[Segment], str]:
|
||||
def land_option_card_segment(segments: list[Segment]) -> Optional[Segment]:
|
||||
"""
|
||||
多段里还缺业务类型/报价场景的陆运段。
|
||||
|
||||
切段时同一种运输方式出现两次已被拒(same_mode),所以最多一段陆运;
|
||||
这里仍只返回第一个,脏书签也不会让同一张卡问两遍。
|
||||
副作用:无,不改入参。
|
||||
"""
|
||||
for seg in segments:
|
||||
if seg.mode == "LAND" and land_option_gaps(seg.facts):
|
||||
return seg
|
||||
return None
|
||||
|
||||
|
||||
def segment_gap_body(seg: Segment, *, options_on_card: bool = False) -> str:
|
||||
"""
|
||||
单段缺口正文。出卡流程用它判断除陆运段外是否还有别的段要补。
|
||||
|
||||
与 refresh_segments 共用 _segment_required_check/_segment_clarify,
|
||||
不另写一套必填口径。副作用:无,不改入参。
|
||||
"""
|
||||
check, _ = _segment_required_check(seg)
|
||||
return _segment_clarify(seg, check, options_on_card=options_on_card)
|
||||
|
||||
|
||||
# 只可能属于陆运段的字段标签。销售补字段常不写段号,只看这些词就能落到陆运段。
|
||||
# 不含「目的港/目的地/体积/重量」:海运和空运也在用,认了会把单段询价抢回多段。
|
||||
_LAND_ANSWER_LABELS = ("业务类型", "报价场景", "车型/数量", "通关口岸", "始发站", "货物品名")
|
||||
|
||||
# 只在某一种运输方式的补问里出现的字段标签。混合回填时按它把一句话拆给两段:
|
||||
# 「车型/数量:3T + 体积:2cbm + 包装类型:托盘」应拆成陆运和空运各一份。
|
||||
_EXCLUSIVE_MODE_LABELS = (
|
||||
("LAND", _LAND_ANSWER_LABELS),
|
||||
(
|
||||
"AIR",
|
||||
(
|
||||
"起运地",
|
||||
"包装类型",
|
||||
"包装方式",
|
||||
"件数",
|
||||
"增值服务",
|
||||
"added_services",
|
||||
"比例",
|
||||
"尺寸",
|
||||
"货源地",
|
||||
"货好时间",
|
||||
"收货地址",
|
||||
),
|
||||
),
|
||||
("SEA", ("货物数量", "整柜或拼柜", "箱型箱量", "派送地址")),
|
||||
)
|
||||
|
||||
|
||||
def _exclusive_mode(label: str) -> str:
|
||||
"""这个字段标签是否只属于一种运输方式;都属于或都不属于时返回空串。"""
|
||||
base = re.sub(r"[((](?:非)?必填[))]$", "", (label or "").strip()).strip()
|
||||
if not base:
|
||||
return ""
|
||||
for mode, labels in _EXCLUSIVE_MODE_LABELS:
|
||||
if base in labels:
|
||||
return mode
|
||||
return ""
|
||||
|
||||
|
||||
def is_land_segment_answer(segments: list[Segment], text: str) -> bool:
|
||||
"""
|
||||
本句是不是在补多段里陆运段的字段。
|
||||
|
||||
只认陆运专属标签(业务类型/报价场景/车型/数量…):这些词不会是在说海运/空运。
|
||||
陆运段已齐时不拦,避免把新开的单段询价粘在旧草稿上。副作用:无,只读段。
|
||||
"""
|
||||
raw = (text or "").strip()
|
||||
if not raw:
|
||||
return False
|
||||
land = next((seg for seg in segments if seg.mode == "LAND"), None)
|
||||
if land is None:
|
||||
return False
|
||||
if not any(label in raw for label in _LAND_ANSWER_LABELS):
|
||||
return False
|
||||
return bool(segment_gap_body(land, options_on_card=False).strip())
|
||||
|
||||
|
||||
def _reply_label_keys(text: str) -> list[str]:
|
||||
"""回复里显式写出的字段名(原词,不归一)。"""
|
||||
return [
|
||||
str(key).strip()
|
||||
for key in parse_labeled_facts(text or "")
|
||||
if str(key).strip()
|
||||
]
|
||||
|
||||
|
||||
def _label_in_prompt(label: str, body: str) -> bool:
|
||||
"""
|
||||
回复里的字段名是否出现在该段的补问正文里。
|
||||
|
||||
中文标签要求左侧不是汉字:避免「品名」误命中「货物品名」。
|
||||
副作用:无。
|
||||
"""
|
||||
base = re.sub(r"[((](?:非)?必填[))]$", "", (label or "").strip()).strip()
|
||||
if not base:
|
||||
return False
|
||||
return re.search(rf"(?<![一-鿿]){re.escape(base)}", body or "") is not None
|
||||
|
||||
|
||||
def segments_matching_supplement_prompt(
|
||||
segments: list[Segment], text: str
|
||||
) -> list[Segment]:
|
||||
"""
|
||||
回复里的字段名能对上哪些「还缺字段」的段补问。
|
||||
|
||||
补问正文就是那一段刚问销售的东西:字段名全部出现在正文里,才算这一段的答案。
|
||||
用原词而不归一是因为「始发站/货物品名」归一后会变成海运也用的「起运港/品名」,
|
||||
会把陆运的补句错落到海运段。副作用:无,只读段。
|
||||
"""
|
||||
keys = _reply_label_keys(text)
|
||||
if not keys:
|
||||
return []
|
||||
matched: list[Segment] = []
|
||||
for seg in segments:
|
||||
body = segment_gap_body(seg, options_on_card=False)
|
||||
if not body.strip():
|
||||
continue
|
||||
if all(_label_in_prompt(key, body) for key in keys):
|
||||
matched.append(seg)
|
||||
return matched
|
||||
|
||||
|
||||
def is_multi_supplement_answer(segments: list[Segment], text: str) -> bool:
|
||||
"""
|
||||
本句是不是在补多段里某一段的字段(不写段号也认)。
|
||||
|
||||
为什么需要(2026-11-23/24 正服截图):多段补问发出后,销售把复制清单填好回传,
|
||||
或只回「车型/数量:8T」「贸易条款:门到门」,不写段号;选流把多段草稿让掉后
|
||||
落到单段,反问「请告知我运输方式」。
|
||||
|
||||
判定只用显式「字段名:值」,纯口语/纯数字仍让位给单段,避免把新询价粘在旧草稿上。
|
||||
先看字段名能不能对上某一段的补问正文;对不上再按字段归属兜底。
|
||||
2026-11-23 的事故里销售其实是在另开海运单,多段里没有可落的海运段,因此不会被拦。
|
||||
副作用:无,只读段。
|
||||
"""
|
||||
raw = (text or "").strip()
|
||||
if not raw:
|
||||
return False
|
||||
raw_labels = parse_labeled_facts(raw)
|
||||
# 先归一:货物品名→品名、始发站→起运港,才能和段的内部键对上。
|
||||
parsed = normalize_facts(raw_labels)
|
||||
if not parsed:
|
||||
# 陆运选项卡的兜底清单用「业务类型/报价场景」,不在通用标签表里。
|
||||
return is_land_segment_answer(segments, raw)
|
||||
if segments_matching_supplement_prompt(segments, raw):
|
||||
return True
|
||||
if is_land_segment_answer(segments, raw):
|
||||
return True
|
||||
# 专属字段指向的运输方式在多段里存在(哪怕该段已齐,也可能是改正)。
|
||||
modes = {seg.mode for seg in segments}
|
||||
if any(_exclusive_mode(key) in modes for key in raw_labels):
|
||||
return True
|
||||
targets = _indexes_for_supplement(segments, parsed)
|
||||
if len(targets) != 1:
|
||||
return False
|
||||
return targets[0] in _lacking_indexes(segments)
|
||||
|
||||
|
||||
def refresh_segments(
|
||||
segments: list[Segment],
|
||||
*,
|
||||
options_on_card: bool = False,
|
||||
) -> tuple[bool, list[Segment], str]:
|
||||
"""
|
||||
按各段运输方式校验必填,并把默认报价日期写回。
|
||||
|
||||
返回:是否已齐、更新后的段、缺口文案。缺口一条消息说完,不套群摘要。
|
||||
待补充的举例与单聊相同:字段名一行,下一行「(如:茶叶)」。
|
||||
options_on_card=True 时,陆运段的业务类型/报价场景由调用方发卡收集,
|
||||
正文只留一句点卡提示;卡发不出去时调用方仍用 option_prompt 兜底。
|
||||
"""
|
||||
updated: list[Segment] = []
|
||||
blocks: list[str] = []
|
||||
@@ -307,7 +477,7 @@ def refresh_segments(segments: list[Segment]) -> tuple[bool, list[Segment], str]
|
||||
check, facts = _segment_required_check(seg)
|
||||
filled = Segment(index=seg.index, mode=seg.mode, facts=facts, source=seg.source)
|
||||
updated.append(filled)
|
||||
body = _segment_clarify(filled, check)
|
||||
body = _segment_clarify(filled, check, options_on_card=options_on_card)
|
||||
if body:
|
||||
ready = False
|
||||
blocks.append(f"段{filled.index}:{filled.word()}\n{body}")
|
||||
@@ -348,24 +518,88 @@ def merge_supplement(
|
||||
if str(val or "").strip():
|
||||
target.facts[key] = str(val).strip()
|
||||
return out
|
||||
parsed = _unlabeled_supplement_facts(text, extra_facts)
|
||||
# 先归一:逐字段认领拿到的内部键才能和 handled 对上(包装类型→包装方式等)。
|
||||
parsed = normalize_facts(_unlabeled_supplement_facts(text, extra_facts))
|
||||
if not parsed:
|
||||
return segments
|
||||
lacking = _lacking_indexes(segments)
|
||||
if len(lacking) == 1:
|
||||
targets = lacking
|
||||
else:
|
||||
# 核对后改海运格子、或两段都缺时只补海运字段:按字段归属落到唯一一段。
|
||||
targets = _indexes_for_supplement(segments, parsed)
|
||||
if len(targets) != 1:
|
||||
return segments
|
||||
out = [Segment(index=s.index, mode=s.mode, facts=dict(s.facts)) for s in segments]
|
||||
for seg in out:
|
||||
if seg.index != targets[0]:
|
||||
by_index = {seg.index: seg for seg in out}
|
||||
|
||||
# ① 先按字段逐项认领:同一句可能同时在补两段。
|
||||
# 例:「车型/数量:3T、体积:2cbm、包装类型:托盘」——
|
||||
# 车型/数量进陆运,包装类型进空运,体积两段都把它当自己的字段时只补空着的。
|
||||
raw_facts = parse_labeled_facts(text)
|
||||
exclusive_modes = {mode for key in raw_facts if (mode := _exclusive_mode(key))}
|
||||
bodies = {
|
||||
seg.index: segment_gap_body(seg, options_on_card=False) for seg in out
|
||||
}
|
||||
handled: set[str] = set()
|
||||
for raw_key, raw_val in raw_facts.items():
|
||||
token = str(raw_val or "").strip()
|
||||
if not token:
|
||||
continue
|
||||
for key, val in parsed.items():
|
||||
internal = normalize_facts({raw_key: token})
|
||||
if not internal:
|
||||
continue
|
||||
mode = _exclusive_mode(raw_key)
|
||||
owners = [
|
||||
seg
|
||||
for seg in out
|
||||
if bodies.get(seg.index, "").strip()
|
||||
and _label_in_prompt(raw_key, bodies[seg.index])
|
||||
]
|
||||
if not owners:
|
||||
# 专属字段指向的段已齐时也收下:销售可能是在改正已经填过的值。
|
||||
if mode:
|
||||
owners = [seg for seg in out if seg.mode == mode]
|
||||
if not owners:
|
||||
continue
|
||||
if mode:
|
||||
owned = [seg for seg in owners if seg.mode == mode]
|
||||
if owned:
|
||||
owners = owned
|
||||
elif exclusive_modes:
|
||||
# 共享字段(体积/贸易条款等)优先给本句里写到专属字段的那一段。
|
||||
preferred = [seg for seg in owners if seg.mode in exclusive_modes]
|
||||
if preferred:
|
||||
owners = preferred
|
||||
# 共享字段落到多段时只填空:这句话未必是在改另一段已有的值。
|
||||
# 只落到唯一一段(含按专属字段收窄后)就允许覆盖,销售可能是在改这个值。
|
||||
multi_owner = len(owners) > 1
|
||||
for seg in owners:
|
||||
for key, val in internal.items():
|
||||
value = str(parsed.get(key) or val).strip()
|
||||
if not value:
|
||||
continue
|
||||
if not mode and multi_owner and str(seg.facts.get(key) or "").strip():
|
||||
continue
|
||||
seg.facts[key] = value
|
||||
handled.update(internal)
|
||||
|
||||
# ② 剩下的是没有显式标签的字段(陆运选项、口语机量、LLM 注入),沿用单段归属。
|
||||
remaining = {key: val for key, val in parsed.items() if key not in handled}
|
||||
if remaining:
|
||||
matched = [
|
||||
seg.index for seg in segments_matching_supplement_prompt(segments, text)
|
||||
]
|
||||
if len(matched) == 1:
|
||||
targets = matched
|
||||
elif is_land_segment_answer(segments, text):
|
||||
# 陆运展示名(始发站/货物品名)归一后会撞上海运键,先按陆运专属词认。
|
||||
targets = [seg.index for seg in segments if seg.mode == "LAND"]
|
||||
else:
|
||||
lacking = _lacking_indexes(segments)
|
||||
if len(lacking) == 1:
|
||||
targets = lacking
|
||||
else:
|
||||
# 核对后改海运格子、或两段都缺时只补海运字段:按字段归属落到唯一一段。
|
||||
targets = _indexes_for_supplement(segments, remaining)
|
||||
if len(targets) != 1:
|
||||
return out
|
||||
target = by_index[targets[0]]
|
||||
for key, val in remaining.items():
|
||||
if str(val or "").strip():
|
||||
seg.facts[key] = str(val).strip()
|
||||
target.facts[key] = str(val).strip()
|
||||
return out
|
||||
|
||||
|
||||
@@ -515,11 +749,18 @@ def includes_air(segments: list[Segment]) -> bool:
|
||||
return any(seg.mode == "AIR" for seg in segments)
|
||||
|
||||
|
||||
def _segment_clarify(seg: Segment, check: dict[str, Any]) -> str:
|
||||
def _segment_clarify(
|
||||
seg: Segment,
|
||||
check: dict[str, Any],
|
||||
*,
|
||||
options_on_card: bool = False,
|
||||
) -> str:
|
||||
"""
|
||||
一段的补问正文,格式对齐单聊:已识别 + 待补充带举例。
|
||||
|
||||
不要再写一遍「请确认并补充」和运输方式,外层已经有段号。
|
||||
options_on_card=True 时,陆运还缺业务类型/报价场景只留点卡提示,
|
||||
不再把复制清单塞进多段正文(清单留给卡发不出去时的兜底)。
|
||||
"""
|
||||
if check.get("ok") and not check.get("need_land_subtype"):
|
||||
return ""
|
||||
@@ -527,6 +768,10 @@ def _segment_clarify(seg: Segment, check: dict[str, Any]) -> str:
|
||||
if seg.mode == "LAND" and (
|
||||
check.get("need_land_subtype") or check.get("need_land_options")
|
||||
):
|
||||
# 缺业务类型/报价场景:多段由调用方发卡,正文只留一句点卡提示。
|
||||
# 选项已齐、只缺品名时仍留在这张复制清单里,只问品名,不进始发站待填。
|
||||
if options_on_card and land_option_gaps(seg.facts):
|
||||
return copy.MULTI_LAND_OPTIONS_ON_CARD
|
||||
# 和单聊同一张清单:类型、线路、分类、货物品名。已填的留在复制框上面。
|
||||
return option_prompt(current=seg.facts)
|
||||
if seg.mode == "LAND":
|
||||
|
||||
Reference in New Issue
Block a user