agent-show-page.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. this.AgentShowPage = class AgentShowPage {
  2. constructor() {
  3. let tab;
  4. $(".agent-show #show-tabs a[href='#logs'], #logs .refresh").on(
  5. "click",
  6. this.fetchLogs
  7. );
  8. $(".agent-show #logs .clear").on("click", this.clearLogs);
  9. $(".agent-show #memory .clear").on("click", this.clearMemory);
  10. $("#toggle-memory").on("click", this.toggleMemory);
  11. // Trigger tabs when navigated to.
  12. if (
  13. (tab = __guard__(window.location.href.match(/tab=(\w+)\b/i), (x) => x[1]))
  14. ) {
  15. if (["details", "logs"].includes(tab)) {
  16. $(`.agent-show .nav-pills li a[href='#${tab}']`).click();
  17. }
  18. }
  19. }
  20. fetchLogs(e) {
  21. const agentId = $(e.target).closest("[data-agent-id]").data("agent-id");
  22. e.preventDefault();
  23. $("#logs .spinner").show();
  24. $("#logs .refresh, #logs .clear").hide();
  25. return $.get(`/agents/${agentId}/logs`, (html) => {
  26. $("#logs .logs").html(html);
  27. $("#logs .logs .show-log-details").each(function () {
  28. const $button = $(this);
  29. return $button.on("click", function (e) {
  30. e.preventDefault();
  31. return Utils.showDynamicModal("<pre></pre>", {
  32. title: $button.data("modal-title"),
  33. body(body) {
  34. return $(body).find("pre").text($button.data("modal-content"));
  35. },
  36. });
  37. });
  38. });
  39. return $("#logs .spinner")
  40. .stop(true, true)
  41. .fadeOut(() => $("#logs .refresh, #logs .clear").show());
  42. });
  43. }
  44. clearLogs(e) {
  45. if (confirm("Are you sure you want to clear all logs for this Agent?")) {
  46. const agentId = $(e.target).closest("[data-agent-id]").data("agent-id");
  47. e.preventDefault();
  48. $("#logs .spinner").show();
  49. $("#logs .refresh, #logs .clear").hide();
  50. return $.post(
  51. `/agents/${agentId}/logs/clear`,
  52. { _method: "DELETE" },
  53. (html) => {
  54. $("#logs .logs").html(html);
  55. $("#show-tabs li a.recent-errors").removeClass("recent-errors");
  56. return $("#logs .spinner")
  57. .stop(true, true)
  58. .fadeOut(() => $("#logs .refresh, #logs .clear").show());
  59. }
  60. );
  61. }
  62. }
  63. toggleMemory(e) {
  64. e.preventDefault();
  65. if ($("pre.memory").hasClass("hidden")) {
  66. $("pre.memory").removeClass("hidden");
  67. return $("#toggle-memory").text("Hide");
  68. } else {
  69. $("pre.memory").addClass("hidden");
  70. return $("#toggle-memory").text("Show");
  71. }
  72. }
  73. clearMemory(e) {
  74. if (
  75. confirm(
  76. "Are you sure you want to completely clear the memory of this Agent?"
  77. )
  78. ) {
  79. const agentId = $(e.target).closest("[data-agent-id]").data("agent-id");
  80. e.preventDefault();
  81. $("#memory .spinner").css({ display: "inline-block" });
  82. $("#memory .clear").hide();
  83. return $.post(`/agents/${agentId}/memory`, { _method: "DELETE" })
  84. .done(() =>
  85. $("#memory .spinner").fadeOut(() =>
  86. $("#memory + .memory").text("{\n}\n")
  87. )
  88. )
  89. .fail(() =>
  90. $("#memory .spinner").fadeOut(() =>
  91. $("#memory .clear").css({ display: "inline-block" })
  92. )
  93. );
  94. }
  95. }
  96. };
  97. $(() =>
  98. Utils.registerPage(AgentShowPage, { forPathsMatching: /^agents\/\d+/ })
  99. );
  100. function __guard__(value, transform) {
  101. return typeof value !== "undefined" && value !== null
  102. ? transform(value)
  103. : undefined;
  104. }