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- 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 (
|
| 426 |
// Also suggest hospital if user explicitly requests it
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
}
|
| 443 |
-
}
|
| 444 |
-
logger.
|
| 445 |
-
|
|
|
|
| 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 (
|
| 554 |
// Also suggest hospital if user explicitly requests it
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
}
|
| 569 |
-
}
|
| 570 |
-
logger.
|
|
|
|
|
|
|
| 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));
|