toolsLibrary.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. const express = require("express");
  2. const router = express.Router();
  3. const { executeQuery } = require("../config/database");
  4. router.get("/", async (req, res) => {
  5. try {
  6. const { page = 1, pageSize = 10, toolsName, mcpToolsName, status } = req.query;
  7. const offset = (page - 1) * pageSize;
  8. // 构建WHERE条件
  9. let whereConditions = [];
  10. let sqlParams = [];
  11. let countParams = [];
  12. if (toolsName) {
  13. whereConditions.push("tools_name LIKE ?");
  14. sqlParams.push(`%${toolsName}%`);
  15. countParams.push(`%${toolsName}%`);
  16. }
  17. if (mcpToolsName) {
  18. whereConditions.push("mcp_tools_name LIKE ?");
  19. sqlParams.push(`%${mcpToolsName}%`);
  20. countParams.push(`%${mcpToolsName}%`);
  21. }
  22. if (status) {
  23. whereConditions.push("status = ?");
  24. sqlParams.push(status);
  25. countParams.push(status);
  26. }
  27. const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(" AND ")}` : "";
  28. const sql = `
  29. SELECT tools_id, tools_name, tools_function_name, mcp_tools_name, tools_full_name,
  30. tools_desc, tools_version, access_task_id, status, call_type,
  31. api_provider, api_url_path, website_location, website_name, website_address, create_time, update_time
  32. FROM tools_library
  33. ${whereClause}
  34. ORDER BY create_time DESC
  35. LIMIT ? OFFSET ?
  36. `;
  37. const countSql = `SELECT COUNT(*) as total FROM tools_library ${whereClause}`;
  38. // 添加分页参数
  39. sqlParams.push(parseInt(pageSize), offset);
  40. const [data, countResult] = await Promise.all([executeQuery(sql, sqlParams), executeQuery(countSql, countParams)]);
  41. res.json({
  42. data,
  43. total: countResult[0].total,
  44. page: parseInt(page),
  45. pageSize: parseInt(pageSize),
  46. });
  47. } catch (error) {
  48. console.error("Error fetching tools library:", error);
  49. res.status(500).json({ error: "Internal server error" });
  50. }
  51. });
  52. router.get("/:id", async (req, res) => {
  53. try {
  54. const { id } = req.params;
  55. const sql = `
  56. SELECT tools_id, tools_name, tools_function_name, mcp_tools_name, tools_full_name,
  57. tools_desc, tools_version, access_task_id, status, call_type,
  58. api_provider, api_url_path, website_location, website_name, website_address, operate_path_data, params_definition,
  59. response_desc, create_time, update_time
  60. FROM tools_library
  61. WHERE tools_id = ?
  62. `;
  63. const data = await executeQuery(sql, [id]);
  64. if (data.length === 0) {
  65. return res.status(404).json({ error: "Tool not found" });
  66. }
  67. res.json(data[0]);
  68. } catch (error) {
  69. console.error("Error fetching tool detail:", error);
  70. res.status(500).json({ error: "Internal server error" });
  71. }
  72. });
  73. router.put("/:id", async (req, res) => {
  74. try {
  75. const { id } = req.params;
  76. const {
  77. tools_name,
  78. tools_function_name,
  79. mcp_tools_name,
  80. tools_full_name,
  81. tools_desc,
  82. tools_version,
  83. status,
  84. call_type,
  85. api_provider,
  86. api_url_path,
  87. website_location,
  88. website_name,
  89. website_address,
  90. operate_path_data,
  91. params_definition,
  92. response_desc,
  93. } = req.body;
  94. const sql = `
  95. UPDATE tools_library
  96. SET tools_name = ?, tools_function_name = ?, mcp_tools_name = ?, tools_full_name = ?,
  97. tools_desc = ?, tools_version = ?,
  98. status = ?, call_type = ?, api_provider = ?, api_url_path = ?,
  99. website_location = ?, website_name = ?, website_address = ?,
  100. operate_path_data = ?, params_definition = ?, response_desc = ?,
  101. update_time = NOW()
  102. WHERE tools_id = ?
  103. `;
  104. // 将 undefined 值转换为 null,避免数据库绑定参数错误
  105. const params = [
  106. tools_name ?? null,
  107. tools_function_name ?? null,
  108. mcp_tools_name ?? null,
  109. tools_full_name ?? null,
  110. tools_desc ?? null,
  111. tools_version ?? null,
  112. status ?? null,
  113. call_type ?? null,
  114. api_provider ?? null,
  115. api_url_path ?? null,
  116. website_location ?? null,
  117. website_name ?? null,
  118. website_address ?? null,
  119. operate_path_data ?? null,
  120. params_definition ?? null,
  121. response_desc ?? null,
  122. id,
  123. ];
  124. await executeQuery(sql, params);
  125. res.json({ message: "Tool updated successfully" });
  126. } catch (error) {
  127. console.error("Error updating tool:", error);
  128. res.status(500).json({ error: "Internal server error" });
  129. }
  130. });
  131. router.post("/:id/publish", async (req, res) => {
  132. try {
  133. const { id } = req.params;
  134. const sql = `
  135. UPDATE tools_library
  136. SET status = 'normal', update_time = NOW()
  137. WHERE tools_id = ?
  138. `;
  139. await executeQuery(sql, [id]);
  140. res.json({ message: "Tool published successfully" });
  141. } catch (error) {
  142. console.error("Error publishing tool:", error);
  143. res.status(500).json({ error: "Internal server error" });
  144. }
  145. });
  146. module.exports = router;