同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。 补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。 统一午夜黑及其他主题的控件细节、中文和业务数字字体。 排除本地业务数据、凭据、预览、发布与验收产物。 验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
This commit is contained in:
@@ -0,0 +1,235 @@
|
||||
"""Resolve product mentions against the caller's already-authorized catalogue.
|
||||
|
||||
This module only identifies catalogue names. It does not construct queries,
|
||||
change filters, aggregate products, or infer results from an absent name.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import unicodedata
|
||||
from decimal import Decimal, InvalidOperation
|
||||
|
||||
_DIMENSIONS = re.compile(r'(?<![\d.])(?:d\d+(?:\.\d+)?(?:x\d+(?:\.\d+)?){1,}|(?<!d)\d+(?:\.\d+)?(?:x\d+(?:\.\d+)?){2,})(?![\d.]|x\d)')
|
||||
_WORD = re.compile(r'[a-z0-9.+]')
|
||||
_SEPARATORS = str.maketrans({'×': 'x', '✕': 'x', '✖': 'x', '*': 'x', 'φ': 'd', 'ø': 'd', '⌀': 'd'})
|
||||
_CUSTOMER_VALUE = re.compile(r'''(?:客户代码|客户编码|客户编号|客户代号|客户号)(?:为|是)?[::=]?["'“‘「『【((]*([a-z0-9][a-z0-9.+_-]{0,127})''')
|
||||
|
||||
|
||||
def _normalized(value):
|
||||
return ''.join(unicodedata.normalize('NFKC', str(value)).casefold().translate(_SEPARATORS).split())
|
||||
|
||||
|
||||
def _dimension_key(value):
|
||||
try:
|
||||
round_spec = value.startswith('d')
|
||||
dimensions = value[1:] if round_spec else value
|
||||
return ('round' if round_spec else 'block', *(Decimal(part).normalize() for part in dimensions.split('x')))
|
||||
except InvalidOperation:
|
||||
return ()
|
||||
|
||||
|
||||
def _exact_spans(question, name):
|
||||
"""Find a whole catalogue name, not a prefix of another specification."""
|
||||
if len(name) < 3 or name.isdigit():
|
||||
return []
|
||||
spans = []
|
||||
pos = 0
|
||||
while True:
|
||||
start = question.find(name, pos)
|
||||
if start < 0:
|
||||
break
|
||||
end = start + len(name)
|
||||
if (not start or not _WORD.fullmatch(question[start - 1])) and (end == len(question) or not _WORD.fullmatch(question[end])):
|
||||
spans.append((start, end))
|
||||
pos = start + 1
|
||||
return spans
|
||||
|
||||
|
||||
def identify_products(question, options, *, limit=20, respect_exclusions=False):
|
||||
"""Return explicit names and exact-dimension candidates, bounded for planning.
|
||||
|
||||
``options`` must be the permission-filtered response from /api/options for
|
||||
the selected release. Decimal dimensions are compared numerically, never
|
||||
as prefixes, so 18x15x4 does not match 18x15x4.1.
|
||||
"""
|
||||
options = options if isinstance(options, dict) else {}
|
||||
products = options.get('products') if isinstance(options.get('products'), list) else []
|
||||
names = sorted({name.strip() for name in products
|
||||
if isinstance(name, str) and name.strip()})
|
||||
text = _normalized(question)
|
||||
paths = options.get('organizationPaths', [])
|
||||
by_name = {name: {'name': name, 'departments': set(), 'groups': set()} for name in names}
|
||||
if isinstance(paths, list):
|
||||
for path in paths:
|
||||
if not isinstance(path, dict) or path.get('product') not in by_name:
|
||||
continue
|
||||
entry = by_name[path['product']]
|
||||
for field, output in (('department', 'departments'), ('group', 'groups')):
|
||||
value = path.get(field)
|
||||
if isinstance(value, str) and value.strip():
|
||||
entry[output].add(value.strip())
|
||||
|
||||
def records(selected):
|
||||
return [{'name': name, 'departments': sorted(by_name[name]['departments']),
|
||||
'groups': sorted(by_name[name]['groups'])} for name in selected[:limit]]
|
||||
|
||||
normalized_names = {name: _normalized(name) for name in names}
|
||||
# A customer code can itself look like a dimensional specification. It
|
||||
# must stay a field value rather than accidentally become a product.
|
||||
other_field_values = [match.span(1) for match in _CUSTOMER_VALUE.finditer(text)]
|
||||
def is_other_field_value(start, end):
|
||||
return any(field_start <= start and end <= field_end for field_start, field_end in other_field_values)
|
||||
exact = {name: spans for name, value in normalized_names.items()
|
||||
if (spans := [(start, end) for start, end in _exact_spans(text, value)
|
||||
if not is_other_field_value(start, end)])}
|
||||
dimensional = {}
|
||||
for name, value in normalized_names.items():
|
||||
for match in _DIMENSIONS.finditer(value):
|
||||
dimensional.setdefault(_dimension_key(match.group()), set()).add(name)
|
||||
|
||||
mentions = []
|
||||
full_candidates_by_span = {}
|
||||
excluded_names = set()
|
||||
for match in _DIMENSIONS.finditer(text):
|
||||
if is_other_field_value(match.start(), match.end()):
|
||||
continue
|
||||
# A complete catalogue name around this token is a stronger explicit
|
||||
# choice than a bare dimensional shorthand.
|
||||
chosen = sorted(name for name, spans in exact.items()
|
||||
if any(start <= match.start() and match.end() <= end for start, end in spans))
|
||||
# An explicit but unknown suffix is part of the user's specification;
|
||||
# never silently replace it with an existing variant of the same size.
|
||||
suffix = re.match(r'[+_-][a-z0-9][a-z0-9_.-]*', text[match.end():])
|
||||
candidates = chosen or ([] if suffix else sorted(dimensional.get(_dimension_key(match.group()), ())))
|
||||
status = ('exact' if len(chosen) == 1 else 'unique' if not chosen and len(candidates) == 1 else
|
||||
'ambiguous' if candidates else 'none')
|
||||
end = match.end() + (len(suffix.group()) if suffix else 0)
|
||||
prefix = re.split(r'[,,。;;!?!?]',text[:match.start()])[-1]
|
||||
tail = r'(?:的|规格|尺寸|产品|为|是|:|:){0,6}$'
|
||||
excluded = bool(respect_exclusions and re.search(
|
||||
r'(?:不要包含|不要包括|不包含|不包括|不计入|不要看|不看|排除|剔除|去除)'+tail,prefix)
|
||||
and not re.search(r'(?:不要|不用|不再|取消)(?:排除|剔除|去除)'+tail,prefix))
|
||||
if excluded: excluded_names.update(candidates)
|
||||
full_candidates_by_span[(match.start(),end)] = candidates
|
||||
mentions.append({'query': text[match.start():end], 'normalizedSpan': [match.start(), end],
|
||||
'status': status, 'candidates': records(candidates),
|
||||
'candidateCount': len(candidates), 'truncated': len(candidates) > limit,
|
||||
**({'intent':'exclude'} if excluded else {})})
|
||||
|
||||
if respect_exclusions:
|
||||
positive = []
|
||||
for mention in mentions:
|
||||
if mention.get('intent') == 'exclude': continue
|
||||
original = full_candidates_by_span[tuple(mention['normalizedSpan'])]
|
||||
kept = [name for name in original if name not in excluded_names]
|
||||
status = mention['status'] if kept == original else 'unique' if len(kept)==1 else 'ambiguous' if kept else 'none'
|
||||
positive.append({**mention,'candidates':records(kept),'candidateCount':len(kept),
|
||||
'truncated':len(kept)>limit,'status':status})
|
||||
effective_mentions = positive
|
||||
else:
|
||||
effective_mentions = mentions
|
||||
|
||||
if not mentions and exact:
|
||||
selected = sorted(exact)
|
||||
mentions = [{'query': '', 'status': 'exact' if len(selected) == 1 else 'multiple',
|
||||
'candidates': records(selected), 'candidateCount': len(selected),
|
||||
'truncated': len(selected) > limit}]
|
||||
effective_mentions = mentions
|
||||
if not effective_mentions:
|
||||
match = {'query': '', 'status': 'none', 'candidates': [], 'candidateCount': 0, 'truncated': False}
|
||||
elif len(effective_mentions) == 1:
|
||||
match = dict(effective_mentions[0])
|
||||
else:
|
||||
selected = sorted({entry['name'] for mention in effective_mentions for entry in mention['candidates']})
|
||||
match = {'query': '', 'status': 'ambiguous' if any(m['status'] == 'ambiguous' for m in effective_mentions) else 'multiple',
|
||||
'candidates': records(selected), 'candidateCount': len(selected),
|
||||
'truncated': any(m['truncated'] for m in mentions) or len(selected) > limit}
|
||||
match['mentions'] = mentions
|
||||
return {'mentionedProducts': sorted(exact)[:limit], 'productMatch': match,
|
||||
**({'explicitlyExcludedProducts':sorted(excluded_names)} if respect_exclusions else {})}
|
||||
|
||||
|
||||
def authorized_extra_fields(catalog):
|
||||
"""Extract only field metadata returned by the actor-scoped API catalog."""
|
||||
if not isinstance(catalog, dict) or catalog.get('kind') != 'analysis_catalog' or not isinstance(catalog.get('extraFields'), list):
|
||||
return {'extraFieldsStatus': 'unavailable', 'extraFields': []}
|
||||
fields = []
|
||||
for item in catalog['extraFields']:
|
||||
if not isinstance(item, dict) or item.get('enabled') is False:
|
||||
continue
|
||||
key, label = item.get('key'), item.get('label')
|
||||
if (not isinstance(key, str) or not re.fullmatch(r'[A-Za-z][A-Za-z0-9_]{0,63}', key)
|
||||
or not isinstance(label, str) or not label.strip() or len(label) > 80):
|
||||
continue
|
||||
fields.append({'key': key, 'label': label.strip()})
|
||||
return {'extraFieldsStatus': 'ready', 'extraFields': fields}
|
||||
|
||||
|
||||
def resolve_clarification_choice(question, options, previous_task):
|
||||
"""Resolve a short suffix selection only within the saved pending choices.
|
||||
|
||||
The caller must supply the prior task from the same authorized session and
|
||||
release, and current options must come from the permission-filtered API.
|
||||
``None`` means the reply did not unambiguously select a saved candidate.
|
||||
"""
|
||||
if not isinstance(previous_task, dict):
|
||||
return None
|
||||
pending = previous_task.get('productResolution')
|
||||
if not isinstance(pending, dict) or not isinstance(pending.get('originalQuestion'), str):
|
||||
return None
|
||||
# Once a choice has been planned, restore it only for the same short reply
|
||||
# on retry. Never attach a previously selected product to a new question.
|
||||
is_pending = previous_task.get('needsClarification') is True
|
||||
if not is_pending:
|
||||
if (not isinstance(pending.get('selectedName'), str)
|
||||
or not isinstance(pending.get('selectionQuestion'), str)
|
||||
or _normalized(question) != _normalized(pending['selectionQuestion'])):
|
||||
return None
|
||||
previous_match = pending.get('productMatch')
|
||||
if (not isinstance(previous_match, dict) or previous_match.get('status') != 'ambiguous'
|
||||
or previous_match.get('truncated') is not False):
|
||||
return None
|
||||
entries = previous_match.get('candidates')
|
||||
if not isinstance(entries, list) or type(previous_match.get('candidateCount')) is not int:
|
||||
return None
|
||||
names = [item.get('name') for item in entries if isinstance(item, dict) and isinstance(item.get('name'), str)]
|
||||
if len(names) != len(entries) or len(set(names)) != len(names) or len(names) != previous_match['candidateCount']:
|
||||
return None
|
||||
# A short explicit selection only; unrelated follow-ups or requests for
|
||||
# multiple variants do not silently reuse an old pending clarification.
|
||||
reply = str(question).strip()
|
||||
ui_prefix = '继续上一轮分析,保留未修改的原目标和条件。\n我的选择:'
|
||||
if reply.startswith(ui_prefix):
|
||||
reply = reply[len(ui_prefix):].strip()
|
||||
text = _normalized(reply)
|
||||
full_reply = re.sub(r'^(?:我想选|我选择|我选|我要|选择|查看|分析|选|看|用)', '', text)
|
||||
full_reply = re.sub(r'(?:这个|那个|这款|那款|的|规格|产品|型号|即可|吧|[。!?.!?])+$', '', full_reply)
|
||||
full_names = [name for name in names if _normalized(name) == full_reply]
|
||||
choice = re.fullmatch(r'(?:我想选|我选择|我选|我要|选|选择|查看|分析|看|用)?\+?([a-z0-9][a-z0-9_-]{0,19})(?:这个|那个|这款|那款|的|规格|产品|型号|即可|吧|[。!?.!?])*', text)
|
||||
if not choice and len(full_names) != 1:
|
||||
return None
|
||||
token = choice.group(1) if choice else None
|
||||
available = options.get('products') if isinstance(options, dict) else None
|
||||
if not isinstance(available, list):
|
||||
return None
|
||||
authorized = {item for item in available if isinstance(item, str)}
|
||||
selected = []
|
||||
for name in names:
|
||||
if name not in authorized:
|
||||
continue
|
||||
suffix = re.search(r'\+([a-z0-9][a-z0-9_-]{0,19})$', _normalized(name))
|
||||
if name in full_names or suffix and suffix.group(1) == token:
|
||||
selected.append(name)
|
||||
if len(selected) != 1:
|
||||
return None
|
||||
if not is_pending and selected[0] != pending['selectedName']:
|
||||
return None
|
||||
result = identify_products(selected[0], options)
|
||||
match = result['productMatch']
|
||||
if (match['status'] != 'exact' or match['candidateCount'] != 1
|
||||
or match['candidates'][0]['name'] != selected[0]):
|
||||
return None
|
||||
match['selectedFromClarification'] = True
|
||||
match['previousQuestion'] = pending['originalQuestion']
|
||||
match['previousQuery'] = previous_match.get('query', '')
|
||||
return result
|
||||
Reference in New Issue
Block a user