SummaryFile.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. const API = require("../../lib/API")
  2. const AccessControl = require("../../lib/AccessControl")
  3. const { BaseStdResponse } = require("../../BaseStdResponse")
  4. const db = require("../../plugin/DataBase/db")
  5. const axios = require('axios')
  6. const { core_url } = require('../../config.json')
  7. const path = require('path')
  8. const { isBinaryFileSync } = require('isbinaryfile')
  9. class SummaryFile extends API {
  10. constructor() {
  11. super()
  12. this.setMethod("GET")
  13. this.setPath("/AI/SummaryFile")
  14. }
  15. // 定义支持的语言后缀
  16. languageExtensions = {
  17. "JavaScript": [".js", ".jsx", ".mjs", ".cjs"],
  18. "TypeScript": [".ts", ".tsx"],
  19. "Python": [".py"],
  20. "Java": [".java"],
  21. "C": [".c"],
  22. "C++": [".cpp", ".cc", ".cxx", ".h", ".hpp"],
  23. "C#": [".cs"],
  24. "Go": [".go"],
  25. "PHP": [".php"],
  26. "HTML": [".html", ".htm"],
  27. "CSS": [".css"],
  28. "SCSS": [".scss", ".sass"],
  29. "JSON": [".json"],
  30. "YAML": [".yml", ".yaml"],
  31. "Markdown": [".md"],
  32. "Shell": [".sh"],
  33. "Bash": [".bash"],
  34. "PowerShell": [".ps1"],
  35. "Ruby": [".rb"],
  36. "Vue": [".vue"], // Vue文件
  37. "LESS": [".less"], // LESS样式文件
  38. "Sass": [".sass"], // Sass文件
  39. "Haml": [".haml"], // Haml文件
  40. "Elixir": [".ex", ".exs"], // Elixir语言文件
  41. "Erlang": [".erl", ".hrl"], // Erlang语言文件
  42. "Swift": [".swift"], // Swift语言文件
  43. "Kotlin": [".kt", ".kts"], // Kotlin语言文件
  44. "Rust": [".rs"], // Rust语言文件
  45. "Dart": [".dart"], // Dart语言文件
  46. "Lua": [".lua"], // Lua语言文件
  47. "R": [".r"], // R语言文件
  48. "Perl": [".pl"], // Perl语言文件
  49. "SQL": [".sql"], // SQL文件
  50. "Dockerfile": [".dockerfile", "Dockerfile"], // Dockerfile
  51. "GraphQL": [".graphql", ".gql"], // GraphQL文件
  52. "JSON5": [".json5"], // JSON5文件
  53. "TOML": [".toml"], // TOML文件
  54. "Text": [".txt", ".text"],
  55. "Assembly": [".asm", ".s"],
  56. "Objective-C": [".m", ".mm"],
  57. "F#": [".fs", ".fsi", ".fsx", ".fsscript"],
  58. "Haskell": [".hs", ".lhs"],
  59. "OCaml": [".ml", ".mli"],
  60. "Nim": [".nim", ".nims"],
  61. "Julia": [".jl"],
  62. "Fortran": [".f", ".for", ".f90", ".f95"],
  63. "COBOL": [".cbl", ".cob"],
  64. "VHDL": [".vhd", ".vhdl"],
  65. "Verilog": [".v", ".vh"],
  66. "Ada": [".adb", ".ads"],
  67. "Matlab": [".m"], // 注意 Objective-C 也使用 .m,可根据上下文判断
  68. "Scala": [".scala"],
  69. "VB.NET": [".vb"],
  70. "Groovy": [".groovy", ".gvy", ".gy", ".gsh"],
  71. "Makefile": ["Makefile", "makefile", ".mk"],
  72. "Clojure": [".clj", ".cljs", ".cljc", ".edn"],
  73. "Common Lisp": [".lisp", ".lsp"],
  74. "Scheme": [".scm", ".ss"],
  75. "Prolog": [".pl", ".pro", ".P"],
  76. "Smalltalk": [".st", ".squeak"],
  77. "Tcl": [".tcl"],
  78. "Crystal": [".cr"],
  79. "Solidity": [".sol"],
  80. "Vim script": [".vim"],
  81. "ReScript": [".res", ".resi"],
  82. "ReasonML": [".re", ".rei"],
  83. "Pug": [".pug", ".jade"],
  84. "Handlebars": [".hbs", ".handlebars"],
  85. "XML": [".xml"],
  86. "INI": [".ini", ".cfg", ".conf"],
  87. "Log": [".log"],
  88. "ENV": [".env"],
  89. "ReStructuredText": [".rst"],
  90. "AsciiDoc": [".adoc", ".asciidoc"],
  91. "Racket": [".rkt"],
  92. "Zig": [".zig"],
  93. "Haxe": [".hx"],
  94. "Dotenv": [".env"],
  95. "Config": [".config"],
  96. "PlantUML": [".puml", ".plantuml"],
  97. "Visual Basic": [".bas", ".vbs"],
  98. "Batch": [".bat", ".cmd"],
  99. "BibTeX": [".bib"],
  100. "TeX/LaTeX": [".tex", ".ltx", ".sty", ".cls"],
  101. "Apache": [".htaccess", "httpd.conf"],
  102. "NGINX": ["nginx.conf"],
  103. "Terraform": [".tf", ".tfvars"],
  104. "HCL": [".hcl"],
  105. "QML": [".qml"],
  106. "Cue": [".cue"],
  107. "GDScript": [".gd"], // 用于 Godot 引擎
  108. "ANTLR": [".g4"],
  109. "Pascal": [".pas"],
  110. "Logtalk": [".lgt"],
  111. "Awk": [".awk"],
  112. "Sed": [".sed"],
  113. "ConfigScript": [".conf", ".cfg"],
  114. "YANG": [".yang"],
  115. "NetLogo": [".nlogo"]
  116. }
  117. isBinaryFile(filePath) {
  118. try {
  119. return isBinaryFileSync(filePath)
  120. } catch (err) {
  121. return false
  122. }
  123. }
  124. async onRequest(req, res) {
  125. let { uuid, session, id, hash, filePath } = req.query
  126. if ([uuid, session, id, hash, filePath].some(value => value === '' || value === null || value === undefined))
  127. return res.json({
  128. ...BaseStdResponse.MISSING_PARAMETER
  129. })
  130. // 检查 session
  131. if (!await AccessControl.checkSession(uuid, session))
  132. return res.status(401).json({
  133. ...BaseStdResponse.ACCESS_DENIED
  134. })
  135. const ext = path.extname(filePath).toLowerCase()
  136. const language = Object.keys(this.languageExtensions).find(lang =>
  137. this.languageExtensions[lang].includes(ext)
  138. )
  139. if (!language)
  140. return res.json({
  141. ...BaseStdResponse.ERR,
  142. msg: '该文件类型不支持AI分析!'
  143. })
  144. let sql = 'SELECT state, path, url FROM repos WHERE create_user = ? AND id = ?'
  145. let rows = await db.query(sql, [uuid, id])
  146. if (!rows || rows.length === 0)
  147. return res.json({
  148. ...BaseStdResponse.ERR,
  149. msg: '未找到仓库'
  150. })
  151. if (rows[0].state !== 1 || !rows[0].path)
  152. return res.json({
  153. ...BaseStdResponse.ERR,
  154. msg: '仓库未成功克隆!'
  155. })
  156. try {
  157. filePath = path.join(rows[0].path, filePath)
  158. if (this.isBinaryFile(filePath))
  159. return res.json({
  160. ...BaseStdResponse.ERR,
  161. msg: '该文件类型不支持AI总结!'
  162. })
  163. const time = new Date().getTime()
  164. sql = 'INSERT INTO file_summary_tasks (repo_id, create_time, create_user, repo_hash, filepath) VALUES (?, ?, ?, ?, ?)'
  165. let r = await db.query(sql, [id, time, uuid, hash, filePath])
  166. if (!r || r.affectedRows !== 1)
  167. return res.json({
  168. ...BaseStdResponse.ERR,
  169. msg: '扫描任务添加失败!'
  170. })
  171. res.json({
  172. ...BaseStdResponse.OK
  173. })
  174. let endpoint = core_url + '/ai/summaryFile'
  175. await axios.post(endpoint, { uuid, repo_url: rows[0].url, task_id: String(r.insertId), file_path: filePath, repo_id: id })
  176. } catch (error) {
  177. this.logger.error('添加AI分析任务失败!' + error.stack)
  178. return res.json({
  179. ...BaseStdResponse.ERR,
  180. msg: '添加AI分析任务失败!'
  181. })
  182. }
  183. }
  184. }
  185. module.exports.SummaryFile = SummaryFile