VietCat commited on
Commit
6e261a5
·
1 Parent(s): f92231e

quick fix timestamp

Browse files
Files changed (1) hide show
  1. app/message_processor.py +17 -8
app/message_processor.py CHANGED
@@ -181,7 +181,13 @@ class MessageProcessor:
181
  total_chars = 0
182
 
183
  # Bước 1: Sắp xếp history theo timestamp tăng dần (cũ -> mới)
184
- sorted_history = sorted(history, key=lambda x: x.get('timestamp', 0))
 
 
 
 
 
 
185
 
186
  # Bước 2: Duyệt từ mới -> cũ để loại bỏ message cũ nếu cần
187
  for row in reversed(sorted_history):
@@ -238,6 +244,8 @@ class MessageProcessor:
238
  # if isinstance(ts, int):
239
  # return ts
240
  flat = []
 
 
241
  for t in ts:
242
  if isinstance(t, list):
243
  flat.extend(self.flatten_timestamp(t))
@@ -396,7 +404,8 @@ class MessageProcessor:
396
  logger.info(f"[MOCK] Creating Facebook post for sender_id={sender_id} with history={history}")
397
  return "https://facebook.com/mock_post_url"
398
 
399
- async def handle_muc_phat(self, conversation_context, conv, page_token, sender_id):
 
400
  vehicle = conv.get('originalvehicle', '')
401
  action = conv.get('originalaction', '')
402
  question = conv.get('originalquestion', '')
@@ -430,7 +439,7 @@ class MessageProcessor:
430
  # conv['isdone'] = False
431
  return response
432
 
433
- async def handle_quy_tac(self, conversation_context, conv, message_text):
434
  prompt = (
435
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
436
  f"Lịch sử:\n{conversation_context}"
@@ -443,7 +452,7 @@ class MessageProcessor:
443
  conv['isdone'] = True
444
  return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng trả lời về quy tắc giao thông sẽ sớm có mặt."
445
 
446
- async def handle_bao_hieu(self, conversation_context, conv, message_text):
447
  prompt = (
448
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
449
  f"Lịch sử:\n{conversation_context}"
@@ -456,7 +465,7 @@ class MessageProcessor:
456
  conv['isdone'] = True
457
  return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng trả lời về báo hiệu đường bộ sẽ sớm có mặt."
458
 
459
- async def handle_quy_trinh(self, conversation_context, conv, message_text):
460
  prompt = (
461
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
462
  f"Lịch sử:\n{conversation_context}"
@@ -469,7 +478,7 @@ class MessageProcessor:
469
  conv['isdone'] = True
470
  return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng trả lời về quy trình xử lý vi phạm giao thông sẽ sớm có mặt."
471
 
472
- async def handle_ca_nhan(self, conversation_context, conv, message_text):
473
  # Nếu câu hỏi là về thông tin cá nhân của bot, hướng dẫn LLM trả lời đúng
474
  prompt = (
475
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
@@ -484,7 +493,7 @@ class MessageProcessor:
484
  conv['isdone'] = True
485
  return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng này sẽ sớm có mặt."
486
 
487
- async def handle_khac(self, conversation_context, conv, message_text):
488
  prompt = (
489
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
490
  f"Lịch sử:\n{conversation_context}"
@@ -495,4 +504,4 @@ class MessageProcessor:
495
  )
496
  answer = await self.channel.llm.generate_text(message_text)
497
  conv['isdone'] = True
498
- return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng này sẽ sớm có mặt."
 
181
  total_chars = 0
182
 
183
  # Bước 1: Sắp xếp history theo timestamp tăng dần (cũ -> mới)
184
+ # <<< FIX LỖI 2 TẠI ĐÂY >>>
185
+ def get_sortable_timestamp(row):
186
+ ts = row.get('timestamp', 0)
187
+ if isinstance(ts, list):
188
+ return max(ts) if ts else 0
189
+ return ts
190
+ sorted_history = sorted(history, key=get_sortable_timestamp)
191
 
192
  # Bước 2: Duyệt từ mới -> cũ để loại bỏ message cũ nếu cần
193
  for row in reversed(sorted_history):
 
244
  # if isinstance(ts, int):
245
  # return ts
246
  flat = []
247
+ if not isinstance(ts, list):
248
+ ts = [ts]
249
  for t in ts:
250
  if isinstance(t, list):
251
  flat.extend(self.flatten_timestamp(t))
 
404
  logger.info(f"[MOCK] Creating Facebook post for sender_id={sender_id} with history={history}")
405
  return "https://facebook.com/mock_post_url"
406
 
407
+ # <<< FIX LỖI 1 TẠI ĐÂY >>>
408
+ async def handle_muc_phat(self, conv, conversation_context, page_token, sender_id):
409
  vehicle = conv.get('originalvehicle', '')
410
  action = conv.get('originalaction', '')
411
  question = conv.get('originalquestion', '')
 
439
  # conv['isdone'] = False
440
  return response
441
 
442
+ async def handle_quy_tac(self, conv, conversation_context, message_text):
443
  prompt = (
444
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
445
  f"Lịch sử:\n{conversation_context}"
 
452
  conv['isdone'] = True
453
  return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng trả lời về quy tắc giao thông sẽ sớm có mặt."
454
 
455
+ async def handle_bao_hieu(self, conv, conversation_context, message_text):
456
  prompt = (
457
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
458
  f"Lịch sử:\n{conversation_context}"
 
465
  conv['isdone'] = True
466
  return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng trả lời về báo hiệu đường bộ sẽ sớm có mặt."
467
 
468
+ async def handle_quy_trinh(self, conv, conversation_context, message_text):
469
  prompt = (
470
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
471
  f"Lịch sử:\n{conversation_context}"
 
478
  conv['isdone'] = True
479
  return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng trả lời về quy trình xử lý vi phạm giao thông sẽ sớm có mặt."
480
 
481
+ async def handle_ca_nhan(self, conv, conversation_context, message_text):
482
  # Nếu câu hỏi là về thông tin cá nhân của bot, hướng dẫn LLM trả lời đúng
483
  prompt = (
484
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
 
493
  conv['isdone'] = True
494
  return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng này sẽ sớm có mặt."
495
 
496
+ async def handle_khac(self, conv, conversation_context, message_text):
497
  prompt = (
498
  "Biết rằng bạn đã có lịch sử trao đổi như sau:"
499
  f"Lịch sử:\n{conversation_context}"
 
504
  )
505
  answer = await self.channel.llm.generate_text(message_text)
506
  conv['isdone'] = True
507
+ return answer.strip() if answer and answer.strip() else "[Đang phát triển] Tính năng này sẽ sớm có mặt."