mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 22:34:47 +00:00
* Add comprehensive AI/LLM configuration documentation * Fix Chat.start! to use default model when model is nil or empty * Ensure all controllers use Chat.default_model for consistency * Move AI doc inside `hosting/` * Probably too much error handling --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
56 lines
1.5 KiB
Ruby
56 lines
1.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Api::V1::MessagesController < Api::V1::BaseController
|
|
before_action :require_ai_enabled
|
|
before_action :ensure_write_scope, only: [ :create, :retry ]
|
|
before_action :set_chat
|
|
|
|
def create
|
|
@message = @chat.messages.build(
|
|
content: message_params[:content],
|
|
type: "UserMessage",
|
|
ai_model: message_params[:model].presence || Chat.default_model
|
|
)
|
|
|
|
if @message.save
|
|
AssistantResponseJob.perform_later(@message)
|
|
render :show, status: :created
|
|
else
|
|
render json: { error: "Failed to create message", details: @message.errors.full_messages }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def retry
|
|
last_message = @chat.messages.ordered.last
|
|
|
|
if last_message&.type == "AssistantMessage"
|
|
new_message = @chat.messages.create!(
|
|
type: "AssistantMessage",
|
|
content: "",
|
|
ai_model: last_message.ai_model
|
|
)
|
|
|
|
AssistantResponseJob.perform_later(new_message)
|
|
render json: { message: "Retry initiated", message_id: new_message.id }, status: :accepted
|
|
else
|
|
render json: { error: "No assistant message to retry" }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def ensure_write_scope
|
|
authorize_scope!(:write)
|
|
end
|
|
|
|
def set_chat
|
|
@chat = Current.user.chats.find(params[:chat_id])
|
|
rescue ActiveRecord::RecordNotFound
|
|
render json: { error: "Chat not found" }, status: :not_found
|
|
end
|
|
|
|
def message_params
|
|
params.permit(:content, :model)
|
|
end
|
|
end
|