toolsLibrary.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, 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([
  41. executeQuery(sql, sqlParams),
  42. executeQuery(countSql, countParams),
  43. ]);
  44. res.json({
  45. data,
  46. total: countResult[0].total,
  47. page: parseInt(page),
  48. pageSize: parseInt(pageSize),
  49. });
  50. } catch (error) {
  51. console.error("Error fetching tools library:", error);
  52. res.status(500).json({ error: "Internal server error" });
  53. }
  54. });
  55. router.get("/:id", async (req, res) => {
  56. try {
  57. const { id } = req.params;
  58. const sql = `
  59. SELECT tools_id, tools_name, tools_function_name, mcp_tools_name, tools_full_name,
  60. tools_desc, tools_version, access_task_id, status, call_type,
  61. api_provider, api_url_path, operate_path_data, params_definition,
  62. response_desc, create_time, update_time
  63. FROM tools_library
  64. WHERE tools_id = ?
  65. `;
  66. const data = await executeQuery(sql, [id]);
  67. if (data.length === 0) {
  68. return res.status(404).json({ error: "Tool not found" });
  69. }
  70. res.json(data[0]);
  71. } catch (error) {
  72. console.error("Error fetching tool detail:", error);
  73. res.status(500).json({ error: "Internal server error" });
  74. }
  75. });
  76. router.put("/:id", async (req, res) => {
  77. try {
  78. const { id } = req.params;
  79. const {
  80. tools_name,
  81. tools_function_name,
  82. mcp_tools_name,
  83. tools_full_name,
  84. tools_desc,
  85. tools_version,
  86. access_task_id,
  87. status,
  88. call_type,
  89. api_provider,
  90. api_url_path,
  91. operate_path_data,
  92. params_definition,
  93. response_desc,
  94. } = req.body;
  95. const sql = `
  96. UPDATE tools_library
  97. SET tools_name = ?, tools_function_name = ?, mcp_tools_name = ?, tools_full_name = ?,
  98. tools_desc = ?, tools_version = ?, access_task_id = ?,
  99. status = ?, call_type = ?, api_provider = ?, api_url_path = ?,
  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. access_task_id ?? null,
  113. status ?? null,
  114. call_type ?? null,
  115. api_provider ?? null,
  116. api_url_path ?? null,
  117. operate_path_data ?? null,
  118. params_definition ?? null,
  119. response_desc ?? null,
  120. id,
  121. ];
  122. await executeQuery(sql, params);
  123. res.json({ message: "Tool updated successfully" });
  124. } catch (error) {
  125. console.error("Error updating tool:", error);
  126. res.status(500).json({ error: "Internal server error" });
  127. }
  128. });
  129. router.post("/:id/publish", async (req, res) => {
  130. try {
  131. const { id } = req.params;
  132. const sql = `
  133. UPDATE tools_library
  134. SET status = 'normal', update_time = NOW()
  135. WHERE tools_id = ?
  136. `;
  137. await executeQuery(sql, [id]);
  138. res.json({ message: "Tool published successfully" });
  139. } catch (error) {
  140. console.error("Error publishing tool:", error);
  141. res.status(500).json({ error: "Internal server error" });
  142. }
  143. });
  144. module.exports = router;