Merge pull request #803 from cantino/clear-memory

Allow user to clear memory of an agent

Akinori MUSHA 9 years ago
parent
commit
db63908c91

+ 15 - 0
app/assets/javascripts/pages/agent-show-page.js.coffee

@@ -2,6 +2,7 @@ class @AgentShowPage
2 2
   constructor: ->
3 3
     $(".agent-show #show-tabs a[href='#logs'], #logs .refresh").on "click", @fetchLogs
4 4
     $(".agent-show #logs .clear").on "click", @clearLogs
5
+    $(".agent-show #memory .clear").on "click", @clearMemory
5 6
 
6 7
     # Trigger tabs when navigated to.
7 8
     if tab = window.location.href.match(/tab=(\w+)\b/i)?[1]
@@ -39,6 +40,20 @@ class @AgentShowPage
39 40
         $("#logs .spinner").stop(true, true).fadeOut ->
40 41
           $("#logs .refresh, #logs .clear").show()
41 42
 
43
+  clearMemory: (e) ->
44
+    if confirm("Are you sure you want to clear memory of this Agent?")
45
+      agentId = $(e.target).closest("[data-agent-id]").data("agent-id")
46
+      e.preventDefault()
47
+      $("#memory .spinner").css(display: 'inline-block')
48
+      $("#memory .clear").hide()
49
+      $.post "/agents/#{agentId}/memory", { "_method": "DELETE" }
50
+        .done ->
51
+          $("#memory .spinner").fadeOut ->
52
+            $("#memory + .memory").text "{\n}\n"
53
+        .fail ->
54
+          $("#memory .spinner").fadeOut ->
55
+            $("#memory .clear").css(display: 'inline-block')
56
+
42 57
 $ ->
43 58
   Utils.registerPage(AgentShowPage, forPathsMatching: /^agents\/\d+/)
44 59
 

+ 33 - 1
app/assets/stylesheets/application.css.scss.erb

@@ -61,7 +61,7 @@ img.odin {
61 61
   display: none;
62 62
 }
63 63
 
64
-img.spinner {
64
+.spinner {
65 65
   display: none;
66 66
   vertical-align: bottom;
67 67
 }
@@ -172,6 +172,13 @@ span.not-applicable:after {
172 172
   font-weight: bold;
173 173
 }
174 174
 
175
+// Memory
176
+
177
+#memory .action-icon {
178
+  display: inline-block;
179
+  cursor: pointer;
180
+}
181
+
175 182
 // Credentials
176 183
 
177 184
 #ace-credential-value {
@@ -285,3 +292,28 @@ $service-colors:      #55acee     #8fc857     #444444     #2c4762     #007EE5;
285 292
 .label-service {
286 293
   @include services;
287 294
 }
295
+
296
+// Font Awesome Spinner
297
+
298
+.fa-spin-custom {
299
+  @include animation(spin 1000ms infinite linear);
300
+}
301
+
302
+@mixin keyframes($name)
303
+{
304
+  @-webkit-keyframes $name {
305
+    @content;
306
+  }
307
+  @keyframes $name {
308
+    @content;
309
+  }
310
+}
311
+
312
+@include keyframes(spin) {
313
+  0% {
314
+    @include rotate(0deg);
315
+  }
316
+  100% {
317
+    @include rotate(359deg);
318
+  }
319
+}

+ 10 - 0
app/controllers/agents_controller.rb

@@ -112,6 +112,16 @@ class AgentsController < ApplicationController
112 112
     end
113 113
   end
114 114
 
115
+  def destroy_memory
116
+    @agent = current_user.agents.find(params[:id])
117
+    @agent.update!(memory: {})
118
+
119
+    respond_to do |format|
120
+      format.html { redirect_back "Memory erased for '#{@agent.name}'" }
121
+      format.json { head :ok }
122
+    end
123
+  end
124
+
115 125
   def show
116 126
     @agent = current_user.agents.find(params[:id])
117 127
 

+ 6 - 2
app/views/agents/show.html.erb

@@ -162,9 +162,13 @@
162 162
               <pre><%= Utils.pretty_jsonify @agent.options || {} %></pre>
163 163
             </p>
164 164
 
165
-            <p>
165
+            <p id="memory" data-agent-id="<%= @agent.id %>">
166 166
               <b>Memory:</b>
167
-              <pre><%= Utils.pretty_jsonify @agent.memory || {} %></pre>
167
+              <% if @agent.memory.present? %>
168
+                <i class="fa fa-spinner fa-spin spinner"></i>
169
+                <i class="fa fa-trash action-icon clear"></i>
170
+              <% end %>
171
+              <pre class="memory"><%= Utils.pretty_jsonify @agent.memory || {} %></pre>
168 172
             </p>
169 173
           </div>
170 174
         </div>

+ 1 - 0
config/routes.rb

@@ -6,6 +6,7 @@ Huginn::Application.routes.draw do
6 6
       post :handle_details_post
7 7
       put :leave_scenario
8 8
       delete :remove_events
9
+      delete :memory, action: :destroy_memory
9 10
     end
10 11
 
11 12
     collection do

+ 20 - 0
spec/controllers/agents_controller_spec.rb

@@ -374,4 +374,24 @@ describe AgentsController do
374 374
       }
375 375
     end
376 376
   end
377
+
378
+  describe "DELETE memory" do
379
+    it "clears memory of the agent" do
380
+      agent = agents(:bob_website_agent)
381
+      agent.update!(memory: { "test" => 42 })
382
+      sign_in users(:bob)
383
+      delete :destroy_memory, id: agent.to_param
384
+      expect(agent.reload.memory).to eq({})
385
+    end
386
+
387
+    it "does not clear memory of an agent not owned by the current user" do
388
+      agent = agents(:jane_website_agent)
389
+      agent.update!(memory: { "test" => 42 })
390
+      sign_in users(:bob)
391
+      expect {
392
+        delete :destroy_memory, id: agent.to_param
393
+      }.to raise_error(ActiveRecord::RecordNotFound)
394
+      expect(agent.reload.memory).to eq({ "test" => 42})
395
+    end
396
+  end
377 397
 end