fix_all_java_garbled.mjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs';
  2. import { dirname, join } from 'node:path';
  3. import { fileURLToPath } from 'node:url';
  4. const repo = 'd:/work/ylrz_saas';
  5. const scriptDir = dirname(fileURLToPath(import.meta.url));
  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(rel, content) {
  10. let text = content.replace(/\r\n/g, '\n');
  11. if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
  12. writeFileSync(join(repo, rel), text, 'utf8');
  13. console.log('fixed', rel);
  14. }
  15. function stripBom(rel) {
  16. const path = join(repo, rel);
  17. const raw = readFileSync(path);
  18. if (raw.length >= 3 && raw[0] === 0xef && raw[1] === 0xbb && raw[2] === 0xbf) {
  19. writeFileSync(path, raw.slice(3), 'utf8');
  20. console.log('stripped BOM', rel);
  21. }
  22. }
  23. writeUtf8(
  24. 'java/fs-saas-company/src/main/java/com/fs/company/controller/workflow/LobsterDeepDialogueController.java',
  25. u(`package com.fs.company.controller.workflow;
  26. import com.fs.common.core.controller.BaseController;
  27. import com.fs.common.core.domain.AjaxResult;
  28. import com.fs.common.utils.ServletUtils;
  29. import com.fs.company.service.workflow.deepdialogue.DeepDialogueDebugService;
  30. import com.fs.framework.security.LoginUser;
  31. import com.fs.framework.service.TokenService;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.security.access.prepost.PreAuthorize;
  34. import org.springframework.web.bind.annotation.*;
  35. import java.util.Map;
  36. /**
  37. * UDD \u6df1\u5ea6\u5bf9\u8bdd\u8c03\u8bd5\uff08\u62c6\u89e3/\u8ba1\u5212/\u586b\u69fd/\u72b6\u6001\u67e5\u770b\uff09\u3002
  38. */
  39. @RestController
  40. @RequestMapping("/workflow/lobster/deep-dialogue")
  41. public class LobsterDeepDialogueController extends BaseController {
  42. @Autowired(required = false)
  43. private DeepDialogueDebugService debugService;
  44. @Autowired
  45. private TokenService tokenService;
  46. private Long companyId() {
  47. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  48. return loginUser != null && loginUser.getCompany() != null
  49. ? loginUser.getCompany().getCompanyId() : null;
  50. }
  51. @PreAuthorize("@ss.hasPermi('workflow:lobster:query')")
  52. @PostMapping("/debug")
  53. public AjaxResult debug(@RequestBody Map<String, Object> body) {
  54. if (debugService == null) {
  55. return AjaxResult.error("UDD \u8c03\u8bd5\u670d\u52a1\u672a\u542f\u7528");
  56. }
  57. String userKey = body.get("userKey") != null ? body.get("userKey").toString() : "udd-debug-user";
  58. String industryType = (String) body.get("industryType");
  59. String message = (String) body.get("message");
  60. if (message == null || message.isBlank()) {
  61. return AjaxResult.error("\u5ba2\u6237\u6d88\u606f\u4e0d\u80fd\u4e3a\u7a7a");
  62. }
  63. boolean persistState = Boolean.TRUE.equals(body.get("persistState"));
  64. boolean skipLlmRender = body.get("skipLlmRender") == null
  65. || Boolean.parseBoolean(body.get("skipLlmRender").toString());
  66. return AjaxResult.success(debugService.debugGenerate(
  67. companyId(), userKey, industryType, message.trim(), persistState, skipLlmRender));
  68. }
  69. @PreAuthorize("@ss.hasPermi('workflow:lobster:query')")
  70. @GetMapping("/state")
  71. public AjaxResult state(@RequestParam String userKey) {
  72. if (debugService == null) {
  73. return AjaxResult.success();
  74. }
  75. return AjaxResult.success(debugService.getState(companyId(), userKey));
  76. }
  77. @PreAuthorize("@ss.hasPermi('workflow:lobster:edit')")
  78. @DeleteMapping("/state")
  79. public AjaxResult clearState(@RequestParam String userKey) {
  80. if (debugService != null) {
  81. debugService.clearState(companyId(), userKey);
  82. }
  83. return AjaxResult.success();
  84. }
  85. @PreAuthorize("@ss.hasPermi('workflow:lobster:query')")
  86. @GetMapping("/scenario-suite")
  87. public AjaxResult scenarioSuite() {
  88. if (debugService == null) {
  89. return AjaxResult.success();
  90. }
  91. return AjaxResult.success(debugService.runScenarioSuite());
  92. }
  93. }
  94. `)
  95. );
  96. {
  97. const rel =
  98. 'java/fs-saas-company/src/main/java/com/fs/company/controller/workflow/LobsterIndustryCorpusMatrixController.java';
  99. let text = readFileSync(join(repo, rel), 'utf8').replace(/\r\n/g, '\n');
  100. text = text.replace(/^ \* .*corpus_kind=industry_matrix.*$/m, u(' * \u884c\u4e1a\u8bed\u6599\u77e9\u9635\uff08corpus_kind=industry_matrix\uff09\uff0c\u4e0e\u9500\u51a0\u8bed\u6599\u5206\u5f00\u7ba1\u7406\u3002'));
  101. text = text.replace(
  102. /return AjaxResult\.error\("[^"]*"\);/g,
  103. (match, offset, whole) => {
  104. const ctx = whole.slice(Math.max(0, offset - 200), offset + 40);
  105. if (ctx.includes('matrixService == null')) {
  106. return u('return AjaxResult.error("\u884c\u4e1a\u8bed\u6599\u77e9\u9635\u670d\u52a1\u672a\u542f\u7528");');
  107. }
  108. if (ctx.includes('customerQuestion == null')) {
  109. return u('return AjaxResult.error("\u884c\u4e1a\u3001\u5ba2\u6237\u95ee\u9898\u3001\u56de\u590d\u4e0d\u80fd\u4e3a\u7a7a");');
  110. }
  111. if (ctx.includes('writeToKb && knowledgeBaseId')) {
  112. return u('return AjaxResult.error("\u5199\u5165\u77e5\u8bc6\u5e93\u65f6\u8bf7\u63d0\u4f9b knowledgeBaseId");');
  113. }
  114. if (ctx.includes('industryType == null') || ctx.includes('industryType.isBlank()')) {
  115. return u('return AjaxResult.error("\u884c\u4e1a\u4e0d\u80fd\u4e3a\u7a7a");');
  116. }
  117. return match;
  118. }
  119. );
  120. text = text.replace(
  121. /data\.put\("message", count > 0 \? "[^"]*" \+ count \+ "[^"]*"[\s\S]*?: "[^"]*"\);/,
  122. u('data.put("message", count > 0 ? "\u5df2\u521d\u59cb\u5316 " + count + " \u6761\u884c\u4e1a\u8bed\u6599"\n : "\u8be5\u884c\u4e1a\u5df2\u6709\u8bed\u6599\uff0c\u672a\u91cd\u590d\u521d\u59cb\u5316\uff08\u53ef\u7528 force=true \u8ffd\u52a0\uff09");')
  123. );
  124. text = text.replace(
  125. /data\.put\("message", count > 0 \? "[^"]*" \+ count \+ "[^"]*" : "[^"]*"\);/,
  126. u('data.put("message", count > 0 ? "\u5df2\u4ece\u9500\u51a0\u8bed\u6599\u5bfc\u5165 " + count + " \u6761\u5230\u884c\u4e1a\u77e9\u9635" : "\u6ca1\u6709\u53ef\u5bfc\u5165\u7684\u6761\u76ee");')
  127. );
  128. writeUtf8(rel, text);
  129. }
  130. {
  131. const rel =
  132. 'java/fs-saas-company/src/main/java/com/fs/company/controller/workflow/LobsterUserSegmentController.java';
  133. let text = readFileSync(join(repo, rel), 'utf8');
  134. text = text.replace(
  135. /return AjaxResult\.error\("[^"]*"\);/,
  136. u('return AjaxResult.error("\u7528\u6237\u5206\u7fa4\u4e0d\u5b58\u5728");')
  137. );
  138. writeUtf8(rel, text);
  139. }
  140. {
  141. const rel = 'java/fs-service/src/main/java/com/fs/company/domain/CompanyTagBindingExecStats.java';
  142. let text = readFileSync(join(repo, rel), 'utf8');
  143. text = text.replace(/^\/\*\*[\s\S]*?\*\//m, u(`/**
  144. * \u6807\u7b7e\u7ed1\u5b9a\u6267\u884c\u6982\u51b5\u7edf\u8ba1\uff08\u5217\u8868\u9875\u5c55\u793a\uff0c\u5b9a\u65f6/\u624b\u52a8\u5237\u65b0\uff09\u3002
  145. */`));
  146. writeUtf8(rel, text);
  147. }
  148. {
  149. const rel =
  150. 'java/fs-service/src/main/java/com/fs/company/service/workflow/scheduler/LobsterBindingExecStatsScheduler.java';
  151. let text = readFileSync(join(repo, rel), 'utf8');
  152. text = text.replace(/^\/\*\*[\s\S]*?\*\//m, u(`/**
  153. * \u9f99\u8679\u6807\u7b7e\u7ed1\u5b9a\u6267\u884c\u6982\u51b5\u5b9a\u65f6\u7edf\u8ba1\uff0c\u9ed8\u8ba4\u6bcf 30 \u5206\u949f\u5168\u91cf\u5237\u65b0\u3002
  154. */`));
  155. writeUtf8(rel, text);
  156. }
  157. {
  158. const rel =
  159. 'java/fs-service/src/main/java/com/fs/company/service/workflow/scope/LobsterTemplateVisibleScopeUtil.java';
  160. let text = readFileSync(join(repo, rel), 'utf8');
  161. text = text.replace(/^\/\*\*[\s\S]*?\*\//m, u(`/**
  162. * \u9f99\u8679\u5de5\u4f5c\u6d41\u6a21\u677f\u5206\u516c\u53f8\u53ef\u89c1\u8303\u56f4\uff1bvisible_company_ids \u9017\u53f7\u5206\u9694\uff0c\u7a7a\u8868\u793a\u4ec5\u6240\u5c5e company_id \u53ef\u89c1\u3002
  163. */`));
  164. writeUtf8(rel, text);
  165. }
  166. writeUtf8(
  167. 'java/fs-service/src/main/java/com/fs/company/service/workflow/trigger/TimeIntentDetector.java',
  168. readFileSync(
  169. join(repo, 'java/fs-service/src/main/java/com/fs/company/service/workflow/trigger/TimeIntentDetector.java'),
  170. 'utf8'
  171. )
  172. .replace(/\r\n/g, '\n')
  173. .replace(/\[\\u70b9:\?\?\]/g, '[??:??]')
  174. .replace(/\[:?\?\?\]/g, '[:??]')
  175. .replace(/^\/\*\*[\s\S]*?\*\//m, u(`/**
  176. * \u4ece\u5ba2\u6237\u6d88\u606f\u89e3\u6790\u7ea6\u5b9a\u65f6\u95f4\u610f\u56fe\uff0c\u7528\u4e8e waitUntil \u8282\u70b9\u3002
  177. */`))
  178. .replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))
  179. );
  180. {
  181. const rel = 'java/scripts/FindTenantManageMenu.java';
  182. let text = readFileSync(join(repo, rel), 'utf8');
  183. text = text.replace(
  184. /FROM sys_menu WHERE menu_name LIKE '[^']*' OR path LIKE '%tenant%'[\s\S]*?LIMIT 80/,
  185. u("FROM sys_menu WHERE menu_name LIKE '%\u79df\u6237%' OR path LIKE '%tenant%' "
  186. + "OR menu_name LIKE '%\u7ba1\u7406%' ORDER BY menu_id LIMIT 80")
  187. );
  188. writeUtf8(rel, text);
  189. }
  190. {
  191. const rel = 'java/scripts/ProbeSimulateLiquor.java';
  192. let text = readFileSync(join(repo, rel), 'utf8');
  193. text = text.replace(
  194. /template_name LIKE '%\?\?%' OR template_name LIKE '%\?\?%'/,
  195. u("template_name LIKE '%\u9152%' OR template_name LIKE '%\u767d\u9152%'")
  196. );
  197. writeUtf8(rel, text);
  198. }
  199. {
  200. const ddlSource = readFileSync(
  201. join(repo, 'java/fs-service/src/main/resources/db/tenant-initTable.sql'),
  202. 'utf8'
  203. );
  204. const start = ddlSource.indexOf('CREATE TABLE `company_lobster_tag_user_rel`');
  205. const tail = u(") ENGINE=InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='\u9f99\u8679\u6807\u7b7e-\u4f01\u5fae\u5ba2\u6237\u5173\u8054\u8868';");
  206. const end = ddlSource.indexOf(tail, start);
  207. const tableDdl = ddlSource.slice(start, end + tail.length);
  208. const rel = 'java/scripts/ApplyCompanyLobsterTagUserRelTable.java';
  209. let text = readFileSync(join(repo, rel), 'utf8');
  210. text = text.replace(
  211. /private static final String DDL = """[\s\S]*?""";/,
  212. `private static final String DDL = """\n ${tableDdl.split('\n').map((l) => (l ? ' ' + l : l)).join('\n')}\n """;`
  213. );
  214. writeUtf8(rel, text);
  215. }
  216. for (const rel of [
  217. 'java/scripts/CheckMenuEncoding.java',
  218. 'java/scripts/CheckTables.java',
  219. 'java/scripts/ProbeCelery.java',
  220. 'java/scripts/QCols.java',
  221. ]) {
  222. stripBom(rel);
  223. }
  224. console.log('batch java garbled fix done');