"""Resolve same-conversation quote edits and return local facts without model I/O.""" from decimal import Decimal, InvalidOperation import re import sqlite3 from .db import connect from .quote_data import load_book, postcode_fee from .quote_engine import US, QUOTE_WORDS, parse_intent, is_followup, variants, render_rates def intent_with_context(message, conv_id, book): from copy import deepcopy codes = {row['code'] for row in book['warehouses']} codes.update(value.upper() for rate in book['rates'] for value in rate['destinations']) intent = parse_intent(message,codes) def destination_reset(value): if value.get('warehouse') or value.get('unknown_warehouses'):return {'postal','zone','zones'} if value.get('postal'):return {'warehouse','warehouses','unknown_warehouses','zone','zones'} if value.get('zone'):return {'warehouse','warehouses','unknown_warehouses','postal'} return set() excluded=destination_reset(intent) selected_destination=bool(excluded) if selected_destination and not intent.get('unknown_warehouses'):excluded.add('unknown_warehouses') if intent['transport'] or intent['kinds'] or intent['services']:excluded.update(('air_only','air_requested')) if intent['air_only'] or intent['tax']=='excluded':excluded.add('kinds') if intent.get('weight') is not None and not intent.get('packages'):excluded.add('packages') explicit_dims=bool(intent['dims']) if conv_id and is_followup(message,intent): connection=connect() try: recent=connection.execute("SELECT content FROM messages WHERE conv_id=? AND role='user' ORDER BY id DESC LIMIT 10",(conv_id,)).fetchall() finally:connection.close() for (previous,) in recent: if re.search(r'理赔|赔偿|破损|丢失|退件|投诉|查单|轨迹|签收',previous):break earlier=parse_intent(previous,codes) if earlier['country']=='other':break if not (QUOTE_WORDS.search(previous) or US.search(previous) or is_followup(previous,earlier) or any(earlier.get(key) for key in ('weight','dims','cbm','warehouse','postal'))):break if not selected_destination and destination_reset(earlier): excluded.update(destination_reset(earlier));selected_destination=True if intent['dims'] is None and earlier['dims']: intent['dims']=earlier['dims'];intent['dim_unit']=earlier['dim_unit'] for key,value in earlier.items(): if key in ('mode','text','errors','dim_unit','dims'):continue if intent.get(key) is None and key not in excluded:intent[key]=deepcopy(value) if not is_followup(previous,earlier):break if explicit_dims and intent.get('packages'): for piece in intent['packages']: piece['dims']=intent['dims'];piece['dim_unit']=intent['dim_unit'] if (intent['warehouse'] or intent.get('unknown_warehouses')) and intent['country'] is None:intent['country']='US' return intent def answer_quote(message, conv_id): if re.search(r'理赔|赔偿|破损|丢失|退件|投诉|查单|轨迹|签收', message): return None # Avoid a new read for ordinary chat; short followups need their local context. if not (US.search(message) or QUOTE_WORDS.search(message) or conv_id or re.search(r'(?