fix_udd_utf8.mjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { writeFileSync } from 'node:fs';
  2. import { dirname, join } from 'node:path';
  3. import { fileURLToPath } from 'node:url';
  4. const scriptDir = dirname(fileURLToPath(import.meta.url));
  5. const deep = join(scriptDir, '../src/main/java/com/fs/company/service/workflow/deepdialogue');
  6. function u(s) {
  7. return s.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
  8. }
  9. function writeUtf8(path, content) {
  10. writeFileSync(path, content.replace(/\r\n/g, '\n'), 'utf8');
  11. }
  12. writeUtf8(
  13. join(deep, 'UniversalSemanticDecomposition.java'),
  14. u(`package com.fs.company.service.workflow.deepdialogue;
  15. import java.util.EnumMap;
  16. import java.util.Map;
  17. /**
  18. * \u4e09\u5c42\u8bed\u4e49\u62c6\u89e3\u7ed3\u679c\u3002
  19. */
  20. public class UniversalSemanticDecomposition {
  21. private SurfaceQuestionType surfaceQuestionType = SurfaceQuestionType.OTHER;
  22. private String explicitQuestion;
  23. private String implicitSignal;
  24. private String latentNeed;
  25. private boolean shouldPauseProbing;
  26. private String pauseReason;
  27. private final Map<DeepDialogueDimension, String> dimensionUpdates = new EnumMap<>(DeepDialogueDimension.class);
  28. public SurfaceQuestionType getSurfaceQuestionType() { return surfaceQuestionType; }
  29. public void setSurfaceQuestionType(SurfaceQuestionType surfaceQuestionType) {
  30. this.surfaceQuestionType = surfaceQuestionType != null ? surfaceQuestionType : SurfaceQuestionType.OTHER;
  31. }
  32. public String getExplicitQuestion() { return explicitQuestion; }
  33. public void setExplicitQuestion(String explicitQuestion) { this.explicitQuestion = explicitQuestion; }
  34. public String getImplicitSignal() { return implicitSignal; }
  35. public void setImplicitSignal(String implicitSignal) { this.implicitSignal = implicitSignal; }
  36. public String getLatentNeed() { return latentNeed; }
  37. public void setLatentNeed(String latentNeed) { this.latentNeed = latentNeed; }
  38. public boolean isShouldPauseProbing() { return shouldPauseProbing; }
  39. public void setShouldPauseProbing(boolean shouldPauseProbing) { this.shouldPauseProbing = shouldPauseProbing; }
  40. public String getPauseReason() { return pauseReason; }
  41. public void setPauseReason(String pauseReason) { this.pauseReason = pauseReason; }
  42. public Map<DeepDialogueDimension, String> getDimensionUpdates() { return dimensionUpdates; }
  43. }
  44. `)
  45. );
  46. writeUtf8(
  47. join(deep, 'MedicalEfficacyDialoguePolicy.java'),
  48. u(`package com.fs.company.service.workflow.deepdialogue;
  49. import java.util.regex.Pattern;
  50. /**
  51. * Medical efficacy consultations should be answered via UDD (compliant reply + probing),
  52. * not semantic handoff to human.
  53. */
  54. public final class MedicalEfficacyDialoguePolicy {
  55. private static final Pattern MEDICAL_EFFICACY = Pattern.compile(
  56. "\u6cbb\u7597|\u7597\u6548|\u80fd\u5426\u6cbb|\u80fd\u4e0d\u80fd\u6cbb|\u53ef\u4ee5\u6cbb|\u80fd\u6cbb|\u6709\u7528\u5417|\u6709\u6548\u5417|\u6709\u4f5c\u7528|\u80fd\u5426|\u80fd\u4e0d\u80fd"
  57. + "|\u7cd6\u5c3f\u75c5|\u9ad8\u8840\u538b|\u8840\u538b|\u8840\u7cd6|\u75c7\u72b6|\u836f\u7269|\u7528\u836f|\u670d\u836f|\u533b\u751f|\u533b\u9662|\u5c31\u8bca|\u5065\u5eb7|\u517b\u751f|\u996e\u98df|\u80fd\u5403|\u80fd\u4e0d\u80fd\u5403");
  58. /** \u6295\u8bc9 / \u6cd5\u52a1 / \u7d27\u6025\u573a\u666f\uff1a\u4ecd\u5e94\u8f6c\u4eba\u5de5\uff0c\u5373\u4fbf\u8bdd\u9898\u6d89\u53ca\u533b\u7597\u3002 */
  59. private static final Pattern FORCE_HANDOFF = Pattern.compile(
  60. "\u6295\u8bc9|\u9000\u6b3e|\u62a5\u544a|\u66dd\u5149|12315|\u5f8b\u5e08|\u62a5\u8b66|\u8d77\u8bc9|\u6025\u6551|\u7d27\u6025|\u547d\u5371|\u660f\u8ff7|\u81f4\u547d");
  61. public static final String RENDER_COMPLIANCE_HINT =
  62. "\u3010\u533b\u7597\u5408\u89c4\u3011\u4e0d\u5f97\u8bca\u65ad\u6216\u5f00\u5904\u65b9\uff0c\u4e0d\u627f\u8bfa\u6cbb\u6108\u3002"
  63. + "\u53ef\u7ed9\u51fa\u666e\u901a\u5065\u5eb7/\u996e\u98df\u79d1\u666e\u4fe1\u606f\uff0c\u5e76\u8bf4\u660e\u4e0d\u80fd\u66ff\u4ee3\u836f\u7269\u4e0e\u533b\u751f\u6cbb\u7597\u3002"
  64. + "\u5982\u75c7\u72b6\u660e\u663e\u6216\u6301\u7eed\u6076\u5316\uff0c\u5efa\u8bae\u53ca\u65f6\u5c31\u533b\u3002";
  65. private MedicalEfficacyDialoguePolicy() {
  66. }
  67. public static boolean isMedicalIndustry(String industryType) {
  68. if (industryType == null || industryType.isBlank()) {
  69. return false;
  70. }
  71. String t = industryType.trim().toLowerCase();
  72. return "medical".equals(t) || "health".equals(t) || "tcm".equals(t);
  73. }
  74. public static boolean isMedicalEfficacyConsultation(String message) {
  75. return message != null && !message.isBlank() && MEDICAL_EFFICACY.matcher(message.trim()).find();
  76. }
  77. public static boolean requiresForcedHandoff(String message) {
  78. return message != null && FORCE_HANDOFF.matcher(message).find();
  79. }
  80. /**
  81. * @return true when handoff should be deferred so UDD can answer + probe
  82. */
  83. public static boolean shouldAnswerViaUdd(String industryType, String message) {
  84. if (message == null || message.isBlank() || requiresForcedHandoff(message)) {
  85. return false;
  86. }
  87. if (!isMedicalEfficacyConsultation(message)) {
  88. return false;
  89. }
  90. return isMedicalIndustry(industryType) || isMedicalEfficacyConsultation(message);
  91. }
  92. /** Use medical corpus/compliance when industry is medical or message is medical efficacy. */
  93. public static String resolveEffectiveIndustry(String industryType, String message) {
  94. if (isMedicalIndustry(industryType)) {
  95. return industryType.trim();
  96. }
  97. if (isMedicalEfficacyConsultation(message)) {
  98. return "medical";
  99. }
  100. return industryType;
  101. }
  102. }
  103. `)
  104. );
  105. console.log('fixed UniversalSemanticDecomposition.java and MedicalEfficacyDialoguePolicy.java');