]> gitweb.mndrdr.org Git - arelpe.git/commitdiff
Fixed the debugging code for summaries
authorAidan Cornelius-Bell <[email protected]>
Sun, 8 Dec 2024 06:55:39 +0000 (17:25 +1030)
committerAidan Cornelius-Bell <[email protected]>
Sun, 8 Dec 2024 06:55:39 +0000 (17:25 +1030)
app/controllers/posts_controller.rb
app/services/kagi_summarizer_service.rb
app/views/posts/index.html.erb
app/views/posts/show.html.erb
db/migrate/20241208065057_add_api_response_to_posts.rb [new file with mode: 0644]
db/schema.rb

index ef1d82b5048b44654b3b14748a97584d967af9a6..0d038683374f1bcafd583a3d4f093f7b31a3602b 100644 (file)
@@ -126,16 +126,12 @@ class PostsController < ApplicationController
 
     summarizer = KagiSummarizerService.new
 
-    # Determine what content to summarize based on post type
     content_to_summarize = if @post.bookmark? && @post.url.present?
       Rails.logger.debug "Using URL for summarization: #{@post.url}"
       @post.url
     elsif @post.content.present?
       Rails.logger.debug "Using content for summarization (first 100 chars): #{@post.content.truncate(100)}"
       @post.content
-    else
-      Rails.logger.warn "No content available for summarization"
-      nil
     end
 
     if content_to_summarize.nil?
@@ -146,28 +142,36 @@ class PostsController < ApplicationController
     type = @post.bookmark? && @post.url.present? ? :url : :text
     result = summarizer.summarize(content_to_summarize, type)
 
-    Rails.logger.debug "Summarization result: #{result.inspect}"
-
-    if result && result['output'].present?
-      success = @post.update(summary: result['output'])
-      Rails.logger.debug "Update post with summary: #{success ? 'successful' : 'failed'}"
-      if success
+    Rails.logger.debug "Full API result: #{result.inspect}"
+
+    # Always store the API response and timestamp
+    @post.update(
+      summarizer_api_response: {
+        response_body: result[:raw_response],
+        status_code: result[:status_code],
+        timestamp: Time.current
+      },
+      last_summarized_at: Time.current
+    )
+
+    if result[:success] && result[:parsed_response]
+      summary_text = result[:parsed_response]['output'] ||
+                    result[:parsed_response].dig('data', 'output') ||
+                    result[:parsed_response]['summary'] ||
+                    result[:parsed_response].dig('data', 'summary')
+
+      if summary_text.present?
+        @post.update(summary: summary_text)
         redirect_to @post, notice: 'Summary generated successfully.'
       else
-        Rails.logger.error "Failed to save summary: #{@post.errors.full_messages}"
-        redirect_to @post, alert: "Failed to save summary: #{@post.errors.full_messages.join(', ')}"
+        redirect_to @post, alert: 'API response did not contain expected summary content.'
       end
     else
-      error_message = if result
-        "API returned success but no summary output was present"
-      else
-        "Failed to generate summary from API"
-      end
-      Rails.logger.error error_message
-      redirect_to @post, alert: error_message
+      redirect_to @post, alert: "API request failed with status #{result[:status_code]}"
     end
   end
 
+
   private
     # Use callbacks to share common setup or constraints between actions.
     def set_post
index 318dd8c60653b2ddd950f45a03e74d1e31b89600..2fc40301794aaa828b458ed677035135b429d143 100644 (file)
@@ -34,14 +34,12 @@ class KagiSummarizerService
     response = self.class.post('/summarize', @options.merge(body: body.to_json))
     Rails.logger.debug "Received response from Kagi API: Status #{response.code}, Body: #{response.body}"
 
-    if response.success?
-      parsed_response = JSON.parse(response.body)
-      Rails.logger.debug "Parsed response: #{parsed_response.inspect}"
-      parsed_response
-    else
-      Rails.logger.error "Kagi API error: #{response.code} - #{response.body}"
-      Rails.logger.error "Full response object: #{response.inspect}"
-      nil
-    end
+    # Return both the parsed response and the raw response
+    {
+      success: response.success?,
+      status_code: response.code,
+      raw_response: response.body,
+      parsed_response: response.success? ? JSON.parse(response.body) : nil
+    }
   end
 end
index 9a58f61aaadd5113e744912ce5a03fa1751826c7..ce4c9c970bec742c6f2a44d39fbb73225b1e7f33 100644 (file)
@@ -29,7 +29,7 @@
               <%= link_to "View", post, class: "button small" %>
               <%= link_to "Edit", edit_post_path(post), class: "button small" %>
               <%= link_to "Delete", post, method: :delete, data: { confirm: "Are you sure you want to delete this post?" }, class: "button small danger" %>
-              <%= button_to "Generate Summary", summarize_post_path(post), class: "button small summary", data: { confirm: 'This will call the Kagi API. Continue?' } %>
+              <%= if post.summary.blank? then button_to "Generate Summary", summarize_post_path(post), class: "button small summary", data: { confirm: 'This will call the Kagi API. Continue?' } end %>
             </td>
           </tr>
         <% end %>
index 2f00e086f7b0533b578af1c9b2ed32d3bee62f52..6ee72e715aaa1ee5de72931e5a33deb063cd1baf 100644 (file)
@@ -8,7 +8,7 @@
 
     <div>
       <%= link_to "Edit this post", edit_post_path(@post), class: "button" %>
-      <%= button_to "Summarise this post", summarize_post_path(@post), class: "button" %>
+      <%= if @post.summary.blank? then button_to "Summarise this post", summarize_post_path(@post), class: "button" end %>
       <%= button_to "Destroy this post", @post, method: :delete %>
     </div>
   </div>
diff --git a/db/migrate/20241208065057_add_api_response_to_posts.rb b/db/migrate/20241208065057_add_api_response_to_posts.rb
new file mode 100644 (file)
index 0000000..dcd35ca
--- /dev/null
@@ -0,0 +1,6 @@
+class AddApiResponseToPosts < ActiveRecord::Migration[7.2]
+  def change
+    add_column :posts, :summarizer_api_response, :json
+    add_column :posts, :last_summarized_at, :datetime
+  end
+end
index ffb94b52f3c83f1f5a8891a94e834cf814d777c6..e5e4662220bbf5f54b3f5979187b9bff00ae81da 100644 (file)
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema[7.2].define(version: 2024_12_08_061729) do
+ActiveRecord::Schema[7.2].define(version: 2024_12_08_065057) do
   create_table "api_keys", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
     t.string "key"
     t.datetime "created_at", null: false
@@ -39,6 +39,8 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_08_061729) do
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
     t.text "summary"
+    t.json "summarizer_api_response"
+    t.datetime "last_summarized_at"
     t.index ["post_type"], name: "index_posts_on_post_type"
     t.index ["published_at"], name: "index_posts_on_published_at"
     t.index ["slug"], name: "index_posts_on_slug", unique: true