self review changes for pdf support for captain
This commit is contained in:
+1
-5
@@ -67,11 +67,7 @@ Rails.application.routes.draw do
|
||||
resources :copilot_threads, only: [:index, :create] do
|
||||
resources :copilot_messages, only: [:index, :create]
|
||||
end
|
||||
resources :documents, only: [:index, :show, :create, :destroy] do
|
||||
collection do
|
||||
post :upload_pdf
|
||||
end
|
||||
end
|
||||
resources :documents, only: [:index, :show, :create, :destroy]
|
||||
end
|
||||
resources :agent_bots, only: [:index, :create, :show, :update, :destroy] do
|
||||
delete :avatar, on: :member
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
class AddPdfSupportToCaptainDocuments < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
add_column :captain_documents, :content_type, :string
|
||||
add_column :captain_documents, :file_size, :bigint
|
||||
|
||||
add_index :captain_documents, :content_type
|
||||
end
|
||||
end
|
||||
@@ -292,13 +292,10 @@ ActiveRecord::Schema[7.1].define(version: 2025_08_05_082345) do
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "status", default: 0, null: false
|
||||
t.string "content_type"
|
||||
t.bigint "file_size"
|
||||
t.jsonb "metadata", default: {}
|
||||
t.index ["account_id"], name: "index_captain_documents_on_account_id"
|
||||
t.index ["assistant_id", "external_link"], name: "index_captain_documents_on_assistant_id_and_external_link", unique: true
|
||||
t.index ["assistant_id"], name: "index_captain_documents_on_assistant_id"
|
||||
t.index ["content_type"], name: "index_captain_documents_on_content_type"
|
||||
t.index ["status"], name: "index_captain_documents_on_status"
|
||||
end
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@ class Api::V1::Accounts::Captain::DocumentsController < Api::V1::Accounts::BaseC
|
||||
before_action -> { check_authorization(Captain::Assistant) }
|
||||
|
||||
before_action :set_current_page, only: [:index]
|
||||
before_action :set_documents, except: [:create, :upload_pdf]
|
||||
before_action :set_documents, except: [:create]
|
||||
before_action :set_document, only: [:show, :destroy]
|
||||
before_action :set_assistant, only: [:create, :upload_pdf]
|
||||
before_action :set_assistant, only: [:create]
|
||||
RESULTS_PER_PAGE = 25
|
||||
|
||||
def index
|
||||
@@ -34,23 +34,6 @@ class Api::V1::Accounts::Captain::DocumentsController < Api::V1::Accounts::BaseC
|
||||
render_could_not_create_error('Failed to create document')
|
||||
end
|
||||
|
||||
def upload_pdf
|
||||
pdf_file = params[:pdf_file]
|
||||
|
||||
return render_could_not_create_error('PDF file is required') if pdf_file.blank?
|
||||
return render_could_not_create_error('Invalid file type') unless valid_pdf?(pdf_file)
|
||||
return render_could_not_create_error('File too large (max 512MB)') if pdf_file.size > 512.megabytes
|
||||
return render_could_not_create_error('Missing Assistant') if @assistant.nil?
|
||||
|
||||
create_pdf_document(pdf_file)
|
||||
render :show
|
||||
rescue Captain::Document::LimitExceededError => e
|
||||
render_could_not_create_error(e.message)
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "PDF upload error: #{e.message}"
|
||||
render_could_not_create_error('Failed to upload PDF')
|
||||
end
|
||||
|
||||
def destroy
|
||||
@document.destroy
|
||||
head :no_content
|
||||
@@ -67,7 +50,7 @@ class Api::V1::Accounts::Captain::DocumentsController < Api::V1::Accounts::BaseC
|
||||
end
|
||||
|
||||
def set_assistant
|
||||
assistant_id = document_params[:assistant_id] || params[:assistant_id]
|
||||
assistant_id = params.dig(:document, :assistant_id) || params[:assistant_id]
|
||||
@assistant = Current.account.captain_assistants.find_by(id: assistant_id)
|
||||
end
|
||||
|
||||
@@ -82,17 +65,4 @@ class Api::V1::Accounts::Captain::DocumentsController < Api::V1::Accounts::BaseC
|
||||
def document_params
|
||||
params.require(:document).permit(:name, :external_link, :assistant_id, :pdf_file)
|
||||
end
|
||||
|
||||
def valid_pdf?(file)
|
||||
file.content_type == 'application/pdf'
|
||||
end
|
||||
|
||||
def create_pdf_document(pdf_file)
|
||||
@document = @assistant.documents.build(
|
||||
name: pdf_file.original_filename,
|
||||
account: Current.account
|
||||
)
|
||||
@document.pdf_file.attach(pdf_file)
|
||||
@document.save!
|
||||
end
|
||||
end
|
||||
|
||||
@@ -36,6 +36,7 @@ class Captain::Document < ApplicationRecord
|
||||
validates :content, length: { maximum: 200_000 }
|
||||
validates :pdf_file, presence: true, if: :pdf_document?
|
||||
validate :validate_pdf_format, if: :pdf_document?
|
||||
validate :validate_file_attachment, if: -> { pdf_file.attached? }
|
||||
before_validation :ensure_account_id
|
||||
before_validation :set_external_link_for_pdf
|
||||
|
||||
@@ -55,7 +56,15 @@ class Captain::Document < ApplicationRecord
|
||||
scope :for_assistant, ->(assistant_id) { where(assistant_id: assistant_id) }
|
||||
|
||||
def pdf_document?
|
||||
(external_link&.ends_with?('.pdf')) || (pdf_file.attached? && pdf_file.content_type == 'application/pdf')
|
||||
(external_link&.ends_with?('.pdf')) || (pdf_file.attached? && pdf_file.blob.content_type == 'application/pdf')
|
||||
end
|
||||
|
||||
def content_type
|
||||
pdf_file.blob.content_type if pdf_file.attached?
|
||||
end
|
||||
|
||||
def file_size
|
||||
pdf_file.blob.byte_size if pdf_file.attached?
|
||||
end
|
||||
|
||||
def openai_file_id
|
||||
@@ -112,11 +121,15 @@ class Captain::Document < ApplicationRecord
|
||||
def validate_pdf_format
|
||||
return unless pdf_file.attached?
|
||||
|
||||
errors.add(:pdf_file, 'must be a PDF file') unless pdf_file.content_type == 'application/pdf'
|
||||
errors.add(:pdf_file, 'must be a PDF file') unless pdf_file.blob.content_type == 'application/pdf'
|
||||
end
|
||||
|
||||
return unless pdf_file.byte_size > 512.megabytes
|
||||
def validate_file_attachment
|
||||
return unless pdf_file.attached?
|
||||
|
||||
errors.add(:pdf_file, 'must be less than 512MB')
|
||||
return unless pdf_file.blob.byte_size > 20.megabytes
|
||||
|
||||
errors.add(:pdf_file, 'must be less than 20MB')
|
||||
end
|
||||
|
||||
def set_external_link_for_pdf
|
||||
|
||||
@@ -3,9 +3,11 @@ json.assistant do
|
||||
json.partial! 'api/v1/models/captain/assistant', formats: [:json], resource: resource.assistant
|
||||
end
|
||||
json.content resource.content
|
||||
json.content_type resource.content_type
|
||||
json.created_at resource.created_at.to_i
|
||||
json.external_link resource.external_link
|
||||
json.display_url resource.display_url
|
||||
json.file_size resource.file_size
|
||||
json.id resource.id
|
||||
json.name resource.name
|
||||
json.status resource.status
|
||||
|
||||
Reference in New Issue
Block a user