Cuong2004 commited on
Commit
d2d559e
·
1 Parent(s): ff05395

Refactor hospital suggestion logic in agent executor to ensure location is validated before attempting to find the best matching hospital. Enhance logging for user requests without location and update hospital-related keywords for improved recognition.

Browse files
Files changed (1) hide show
  1. src/agent/agent-executor.ts +58 -36
src/agent/agent-executor.ts CHANGED
@@ -422,27 +422,33 @@ Ví dụ format markdown NGẮN GỌN:
422
  logger.error('[REPORT] Hospital tool (MCP) execution failed');
423
  // Continue without hospital info
424
  }
425
- } else if (location && this.shouldSuggestHospital(userText)) {
426
  // Also suggest hospital if user explicitly requests it
427
- logger.info(`[AGENT] Step 5: Finding best matching hospital (user requested)${condition ? ` for condition: ${condition}` : ''}...`);
428
- logger.info('[REPORT] Hospital tool (MCP) will be executed (user explicitly requested)');
429
- try {
430
- const bestHospital = await this.mapsService.findBestMatchingHospital(
431
- location,
432
- condition,
433
- 'bệnh viện'
434
- );
435
- if (bestHospital) {
436
- logger.info(`[AGENT] Found best matching hospital: ${bestHospital.name} (${bestHospital.distance_km}km away${bestHospital.specialty_score ? `, specialty match: ${bestHospital.specialty_score.toFixed(2)}` : ''})`);
437
- logger.info(`[REPORT] Hospital tool (MCP) executed successfully: ${bestHospital.name}`);
438
- return {
439
- ...finalResult,
440
- nearest_clinic: bestHospital
441
- };
 
 
 
 
 
442
  }
443
- } catch (error) {
444
- logger.error({ error }, '[AGENT] Failed to find best matching hospital');
445
- logger.error('[REPORT] Hospital tool (MCP) execution failed');
 
446
  }
447
  } else {
448
  if (location) {
@@ -550,24 +556,33 @@ Ví dụ format markdown NGẮN GỌN:
550
  logger.error({ error }, '[AGENT] Failed to find best matching hospital');
551
  // Continue without hospital info
552
  }
553
- } else if (location && this.shouldSuggestHospital(userText)) {
554
  // Also suggest hospital if user explicitly requests it
555
- logger.info(`[AGENT] Step 4: Finding best matching hospital (user requested)${condition ? ` for condition: ${condition}` : ''}...`);
556
- try {
557
- const bestHospital = await this.mapsService.findBestMatchingHospital(
558
- location,
559
- condition,
560
- 'bệnh viện'
561
- );
562
- if (bestHospital) {
563
- logger.info(`[AGENT] Found best matching hospital: ${bestHospital.name} (${bestHospital.distance_km}km away${bestHospital.specialty_score ? `, specialty match: ${bestHospital.specialty_score.toFixed(2)}` : ''})`);
564
- return {
565
- ...finalResult,
566
- nearest_clinic: bestHospital
567
- };
 
 
 
 
 
 
 
568
  }
569
- } catch (error) {
570
- logger.error({ error }, '[AGENT] Failed to find best matching hospital');
 
 
571
  }
572
  }
573
 
@@ -920,10 +935,17 @@ Viết bằng tiếng Việt, markdown format, ngắn gọn.`;
920
  'đi bệnh viện',
921
  'đến bệnh viện',
922
  'khám ở đâu',
 
 
 
923
  'đi khám',
924
  'cần đi khám',
925
  'gợi ý bệnh viện',
926
- 'tìm bệnh viện'
 
 
 
 
927
  ];
928
 
929
  return hospitalKeywords.some(keyword => lowerText.includes(keyword));
 
422
  logger.error('[REPORT] Hospital tool (MCP) execution failed');
423
  // Continue without hospital info
424
  }
425
+ } else if (this.shouldSuggestHospital(userText)) {
426
  // Also suggest hospital if user explicitly requests it
427
+ if (location) {
428
+ logger.info(`[AGENT] Step 5: Finding best matching hospital (user requested)${condition ? ` for condition: ${condition}` : ''}...`);
429
+ logger.info('[REPORT] Hospital tool (MCP) will be executed (user explicitly requested)');
430
+ try {
431
+ const bestHospital = await this.mapsService.findBestMatchingHospital(
432
+ location,
433
+ condition,
434
+ 'bệnh viện'
435
+ );
436
+ if (bestHospital) {
437
+ logger.info(`[AGENT] Found best matching hospital: ${bestHospital.name} (${bestHospital.distance_km}km away${bestHospital.specialty_score ? `, specialty match: ${bestHospital.specialty_score.toFixed(2)}` : ''})`);
438
+ logger.info(`[REPORT] ✓ Hospital tool (MCP) executed successfully: ${bestHospital.name}`);
439
+ return {
440
+ ...finalResult,
441
+ nearest_clinic: bestHospital
442
+ };
443
+ }
444
+ } catch (error) {
445
+ logger.error({ error }, '[AGENT] Failed to find best matching hospital');
446
+ logger.error('[REPORT] Hospital tool (MCP) execution failed');
447
  }
448
+ } else {
449
+ logger.info('[REPORT] Hospital tool (MCP) requested by user but no location provided - will request location in response');
450
+ // Add a note to the response that location is needed
451
+ (finalResult as any).needs_location_for_hospital = true;
452
  }
453
  } else {
454
  if (location) {
 
556
  logger.error({ error }, '[AGENT] Failed to find best matching hospital');
557
  // Continue without hospital info
558
  }
559
+ } else if (this.shouldSuggestHospital(userText)) {
560
  // Also suggest hospital if user explicitly requests it
561
+ if (location) {
562
+ logger.info(`[AGENT] Step 4: Finding best matching hospital (user requested)${condition ? ` for condition: ${condition}` : ''}...`);
563
+ logger.info('[REPORT] Hospital tool (MCP) will be executed (user explicitly requested)');
564
+ try {
565
+ const bestHospital = await this.mapsService.findBestMatchingHospital(
566
+ location,
567
+ condition,
568
+ 'bệnh viện'
569
+ );
570
+ if (bestHospital) {
571
+ logger.info(`[AGENT] Found best matching hospital: ${bestHospital.name} (${bestHospital.distance_km}km away${bestHospital.specialty_score ? `, specialty match: ${bestHospital.specialty_score.toFixed(2)}` : ''})`);
572
+ logger.info(`[REPORT] ✓ Hospital tool (MCP) executed successfully: ${bestHospital.name}`);
573
+ return {
574
+ ...finalResult,
575
+ nearest_clinic: bestHospital
576
+ };
577
+ }
578
+ } catch (error) {
579
+ logger.error({ error }, '[AGENT] Failed to find best matching hospital');
580
+ logger.error('[REPORT] Hospital tool (MCP) execution failed');
581
  }
582
+ } else {
583
+ logger.info('[REPORT] Hospital tool (MCP) requested by user but no location provided - will request location in response');
584
+ // Add a note to the response that location is needed
585
+ (finalResult as any).needs_location_for_hospital = true;
586
  }
587
  }
588
 
 
935
  'đi bệnh viện',
936
  'đến bệnh viện',
937
  'khám ở đâu',
938
+ 'nên đi khám ở đâu',
939
+ 'nên khám ở đâu',
940
+ 'đi khám ở đâu',
941
  'đi khám',
942
  'cần đi khám',
943
  'gợi ý bệnh viện',
944
+ 'tìm bệnh viện',
945
+ 'tìm nơi khám',
946
+ 'nơi khám',
947
+ 'địa chỉ khám',
948
+ 'chỗ khám'
949
  ];
950
 
951
  return hospitalKeywords.some(keyword => lowerText.includes(keyword));