Class: ChatsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper, AttachmentsHelper, ChatsHelper
Defined in:
app/controllers/chats_controller.rb

Constant Summary collapse

ALLOWED_MODELS =
[
  'meta-llama/llama-4-scout',
  'deepseek/deepseek-r1-distill-llama-70b',
  'deepseek/deepseek-r1',
  'deepseek/deepseek-chat-v3-0324',
  'x-ai/grok-3-beta',
  'openai/gpt-4o-2024-11-20',
  'openai/gpt-4.1',
  'openai/o4-mini-high',
  'openai/gpt-4.1-mini'
]
DEFAULT_MODEL =

DEFAULT_MODEL = ‘anthropic/claude-3.5-haiku:nitro’

'x-ai/grok-3-beta'

Instance Method Summary collapse

Methods included from ChatsHelper

#calculate_chat_analytics, #calculate_messages_analytics, #format_chats_per_day_data, #format_messages_per_day_data

Methods included from AttachmentsHelper

#base64_attachments_to_files, #base64_with_filename_to_files

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details

#createObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/chats_controller.rb', line 96

def create
  return render(status: :bad_request) unless ALLOWED_MODELS.include?(params[:model_name])

  chat = current_company.chats.create!(
    title: params[:prompt].truncate(35, omission: '...'),
    active: true
  )

  chat.chat_messages.create!(
    content: params[:prompt],
    completion_model_name: params[:model_name],
    sender: 'user'
  )

  PythonAiResponseCreator.perform_later(
    chat_id: chat.id,
    user_id: user_global_id,
    model: model_id(model_name: params[:model_name], online: params[:online])
  )

  render json: {
    redirect_to: "/app/ai/chat/#{chat.chat_id}"
  }
end

#destroyObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/controllers/chats_controller.rb', line 144

def destroy
  chat = current_company.chats.where(active: true).find(params[:id])
  chat.chat_messages.update_all(content: nil)
  chat.update!(active: false)

  render json: {
    message: 'Message deleted successfully',
    redirect_to: '/app/ai/chat'
  }
rescue ActiveRecord::RecordNotFound
  render json: {
    error: "You can't access this chat or it does not exist."
  }, status: :not_found
end

#indexObject

DEFAULT_MODEL = ‘meta-llama/llama-4-scout:nitro’ DEFAULT_MODEL = ‘x-ai/grok-3-beta’ DEFAULT_MODEL = ‘deepseek/deepseek-r1-0528’



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/chats_controller.rb', line 35

def index
  query = params[:query] || ''
  order = params[:order] || 'desc'
  order_by = params[:orderBy] || 'updated_at'
  limit = params[:limit] || 100
  offset = params[:offset] || 0

  chats = current_company.chats.where(active: true)

  data,  = fetch_records(
    records: chats,
    order_by: order_by,
    order: order,
    query: query,
    limit: limit,
    offset: offset,
    search_fields: Chat.search_fields
  ).values_at(:data, :metadata)

  data = data.as_json

  render json: {
    data: data,
    metadata: 
  }
end

#send_messageObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/chats_controller.rb', line 121

def send_message
  return render(status: :bad_request) unless ALLOWED_MODELS.include?(params[:model_name])

  chat = current_company.chats.where(active: true).find(params[:id])

  chat_message = chat.chat_messages.create!(
    content: params[:prompt],
    completion_model_name: params[:model_name],
    sender: 'user'
  )
  chat_message.broadcast_message('new_message')

  PythonAiResponseCreator.perform_later(
    chat_id: chat.id,
    user_id: user_global_id,
    model: model_id(model_name: params[:model_name], online: params[:online])
  )

  render json: {
    redirect_to: "/app/ai/chat/#{chat.chat_id}"
  }
end

#showObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/chats_controller.rb', line 62

def show
  chat = current_company.chats
                        .where(active: true)
                        .find(params[:id])

  # Get the chat with only the 10 most recent messages
  chat_messages = chat
                  .chat_messages
                  .select(
                    :content,
                    :sender,
                    :status,
                    :chat_message_id,
                    :chat_id,
                    :created_at,
                    :updated_at,
                    :completion_model_name,
                    :completion_cost,
                    :completion_time,
                    :completion_latency,
                    :speed
                  )
                  .order(created_at: :desc)
                  .limit(10)
                  .reverse

  data = chat.as_json
  data[:chat_messages] = chat_messages

  render json: {
    data: data
  }
end

#usage_per_dayObject



159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/chats_controller.rb', line 159

def usage_per_day
  service = ChatUsageService.new(current_company)
  chats_data = service.chats_per_day_data
  messages_data = service.messages_per_day_data

  render json: {
    chats_per_day: chats_data[:chats_per_day],
    messages_per_day: messages_data[:messages_per_day],
    **chats_data.except(:chats_per_day),
    **messages_data.except(:messages_per_day)
  }
end