You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
5.3 KiB

11 months ago
  1. const { AgentFlows } = require("../utils/agentFlows");
  2. const {
  3. flexUserRoleValid,
  4. ROLES,
  5. } = require("../utils/middleware/multiUserProtected");
  6. const { validatedRequest } = require("../utils/middleware/validatedRequest");
  7. const { Telemetry } = require("../models/telemetry");
  8. function agentFlowEndpoints(app) {
  9. if (!app) return;
  10. // Save a flow configuration
  11. app.post(
  12. "/agent-flows/save",
  13. [validatedRequest, flexUserRoleValid([ROLES.admin])],
  14. async (request, response) => {
  15. try {
  16. const { name, config, uuid } = request.body;
  17. if (!name || !config) {
  18. return response.status(400).json({
  19. success: false,
  20. error: "Name and config are required",
  21. });
  22. }
  23. const flow = AgentFlows.saveFlow(name, config, uuid);
  24. if (!flow) {
  25. return response.status(500).json({
  26. success: false,
  27. error: "Failed to save flow",
  28. });
  29. }
  30. if (!uuid) {
  31. await Telemetry.sendTelemetry("agent_flow_created", {
  32. blockCount: config.blocks?.length || 0,
  33. });
  34. }
  35. return response.status(200).json({
  36. success: true,
  37. flow,
  38. });
  39. } catch (error) {
  40. console.error("Error saving flow:", error);
  41. return response.status(500).json({
  42. success: false,
  43. error: error.message,
  44. });
  45. }
  46. }
  47. );
  48. // List all available flows
  49. app.get(
  50. "/agent-flows/list",
  51. [validatedRequest, flexUserRoleValid([ROLES.admin])],
  52. async (_request, response) => {
  53. try {
  54. const flows = AgentFlows.listFlows();
  55. return response.status(200).json({
  56. success: true,
  57. flows,
  58. });
  59. } catch (error) {
  60. console.error("Error listing flows:", error);
  61. return response.status(500).json({
  62. success: false,
  63. error: error.message,
  64. });
  65. }
  66. }
  67. );
  68. // Get a specific flow by UUID
  69. app.get(
  70. "/agent-flows/:uuid",
  71. [validatedRequest, flexUserRoleValid([ROLES.admin])],
  72. async (request, response) => {
  73. try {
  74. const { uuid } = request.params;
  75. const flow = AgentFlows.loadFlow(uuid);
  76. if (!flow) {
  77. return response.status(404).json({
  78. success: false,
  79. error: "Flow not found",
  80. });
  81. }
  82. return response.status(200).json({
  83. success: true,
  84. flow,
  85. });
  86. } catch (error) {
  87. console.error("Error getting flow:", error);
  88. return response.status(500).json({
  89. success: false,
  90. error: error.message,
  91. });
  92. }
  93. }
  94. );
  95. // Run a specific flow
  96. // app.post(
  97. // "/agent-flows/:uuid/run",
  98. // [validatedRequest, flexUserRoleValid([ROLES.admin])],
  99. // async (request, response) => {
  100. // try {
  101. // const { uuid } = request.params;
  102. // const { variables = {} } = request.body;
  103. // // TODO: Implement flow execution
  104. // console.log("Running flow with UUID:", uuid);
  105. // await Telemetry.sendTelemetry("agent_flow_executed", {
  106. // variableCount: Object.keys(variables).length,
  107. // });
  108. // return response.status(200).json({
  109. // success: true,
  110. // results: {
  111. // success: true,
  112. // results: "test",
  113. // variables: variables,
  114. // },
  115. // });
  116. // } catch (error) {
  117. // console.error("Error running flow:", error);
  118. // return response.status(500).json({
  119. // success: false,
  120. // error: error.message,
  121. // });
  122. // }
  123. // }
  124. // );
  125. // Delete a specific flow
  126. app.delete(
  127. "/agent-flows/:uuid",
  128. [validatedRequest, flexUserRoleValid([ROLES.admin])],
  129. async (request, response) => {
  130. try {
  131. const { uuid } = request.params;
  132. const { success } = AgentFlows.deleteFlow(uuid);
  133. if (!success) {
  134. return response.status(500).json({
  135. success: false,
  136. error: "Failed to delete flow",
  137. });
  138. }
  139. return response.status(200).json({
  140. success,
  141. });
  142. } catch (error) {
  143. console.error("Error deleting flow:", error);
  144. return response.status(500).json({
  145. success: false,
  146. error: error.message,
  147. });
  148. }
  149. }
  150. );
  151. // Toggle flow active status
  152. app.post(
  153. "/agent-flows/:uuid/toggle",
  154. [validatedRequest, flexUserRoleValid([ROLES.admin])],
  155. async (request, response) => {
  156. try {
  157. const { uuid } = request.params;
  158. const { active } = request.body;
  159. const flow = AgentFlows.loadFlow(uuid);
  160. if (!flow) {
  161. return response
  162. .status(404)
  163. .json({ success: false, error: "Flow not found" });
  164. }
  165. flow.config.active = active;
  166. const { success } = AgentFlows.saveFlow(flow.name, flow.config, uuid);
  167. if (!success) {
  168. return response
  169. .status(500)
  170. .json({ success: false, error: "Failed to update flow" });
  171. }
  172. return response.json({ success: true, flow });
  173. } catch (error) {
  174. console.error("Error toggling flow:", error);
  175. response.status(500).json({ success: false, error: error.message });
  176. }
  177. }
  178. );
  179. }
  180. module.exports = { agentFlowEndpoints };