Skip to content

Commit

Permalink
API for assistant/chats (#64)
Browse files Browse the repository at this point in the history
* initial api

* api views

* fix tests

* adjust fields

* show messages

* add messages api

* message tests
  • Loading branch information
vswamidass-sfdc authored Sep 9, 2024
1 parent c47fb7d commit aad86ef
Show file tree
Hide file tree
Showing 24 changed files with 350 additions and 121 deletions.
9 changes: 9 additions & 0 deletions app/controllers/api/v1/assistants_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Api
module V1
class AssistantsController < BaseAssistantsController
skip_before_action :verify_authenticity_token, only: :create
end
end
end
9 changes: 9 additions & 0 deletions app/controllers/api/v1/chats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Api
module V1
class ChatsController < BaseChatsController
skip_before_action :verify_authenticity_token, only: :create
end
end
end
7 changes: 7 additions & 0 deletions app/controllers/api/v1/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Api
module V1
class MessagesController < BaseMessagesController
skip_before_action :verify_authenticity_token, only: :create
end
end
end
62 changes: 1 addition & 61 deletions app/controllers/assistants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
class AssistantsController < ApplicationController
class AssistantsController < BaseAssistantsController
before_action :set_assistant, only: %i[show edit update destroy]

# GET /assistants or /assistants.json
def index
@assistants = Assistant.all.order(status: :desc)
end

# GET /assistants/1 or /assistants/1.json
def show; end

Expand All @@ -17,59 +12,4 @@ def new

# GET /assistants/1/edit
def edit; end

# POST /assistants or /assistants.json
def create
json = JSON.parse(params[:json])
@assistant = Assistant.new(json || assistant_params)

authorize @assistant

respond_to do |format|
if @assistant.save
format.html { redirect_to assistant_url(@assistant), notice: 'Assistant was successfully created.' }
format.json { render :show, status: :created, location: @assistant }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @assistant.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /assistants/1 or /assistants/1.json
def update
authorize @assistant

respond_to do |format|
if @assistant.update(assistant_params)
format.html { redirect_to assistant_url(@assistant), notice: 'Assistant was successfully updated.' }
format.json { render :show, status: :ok, location: @assistant }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @assistant.errors, status: :unprocessable_entity }
end
end
end

# DELETE /assistants/1 or /assistants/1.json
def destroy
@assistant.destroy!

respond_to do |format|
format.html { redirect_to assistants_url, notice: 'Assistant was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_assistant
@assistant = Assistant.find(params[:id])
end

# Only allow a list of trusted parameters through.
def assistant_params
params.require(:assistant).permit(:libraries, :name, :input, :output, :context, :instructions, :description, :status, :quip_url, :confluence_spaces, :user_id)
end
end
63 changes: 63 additions & 0 deletions app/controllers/base_assistants_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class BaseAssistantsController < ApplicationController
before_action :set_assistant, only: %i[show edit update destroy]

# GET /assistants or /assistants.json
def index
@assistants = Assistant.all.order(status: :desc)
end

# POST /assistants or /assistants.json
def create
json = JSON.parse(params[:json])
@assistant = Assistant.new(json || assistant_params)

authorize @assistant

respond_to do |format|
if @assistant.save
format.html { redirect_to assistant_url(@assistant), notice: 'Assistant was successfully created.' }
format.json { render :show, status: :created, location: @assistant }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @assistant.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /assistants/1 or /assistants/1.json
def update
authorize @assistant

respond_to do |format|
if @assistant.update(assistant_params)
format.html { redirect_to assistant_url(@assistant), notice: 'Assistant was successfully updated.' }
format.json { render :show, status: :ok, location: @assistant }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @assistant.errors, status: :unprocessable_entity }
end
end
end

# DELETE /assistants/1 or /assistants/1.json
def destroy
@assistant.destroy!

respond_to do |format|
format.html { redirect_to assistants_url, notice: 'Assistant was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_assistant
@assistant = Assistant.find(params[:id])
end

# Only allow a list of trusted parameters through.
def assistant_params
params.require(:assistant).permit(:libraries, :name, :input, :output, :context, :instructions, :description, :status, :quip_url, :confluence_spaces)
end
end
55 changes: 55 additions & 0 deletions app/controllers/base_chats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class BaseChatsController < ApplicationController
before_action :set_chat, only: %i[show edit update destroy]

# GET /chats or /chats.json
def index
@chats = if params[:all].present?
Chat.all.page(params[:page])
else
Chat.where(user_id: current_user.id).page(params[:page])
end
end

# POST /chats or /chats.json
def create
@chat = Chat.new(chat_params)
@chat.user_id = current_user.id

if assistant = Assistant.find_by(id: params[:assistant_id])
@chat.assistant = assistant
end

respond_to do |format|
if @chat.save
@chat.messages.create(content: @chat.first_message, user_id: @chat.user_id, from: :user)
format.html { redirect_to chat_url(@chat) }
format.json { render :show, status: :created, location: @chat }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @chat.errors, status: :unprocessable_entity }
end
end
end

# DELETE /chats/1 or /chats/1.json
def destroy
@chat.destroy!

respond_to do |format|
format.html { redirect_to chats_url, notice: 'Chat was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_chat
@chat = Chat.find(params[:id])
end

# Only allow a list of trusted parameters through.
def chat_params
params.require(:chat).permit(:assistant_id, :first_message)
end
end
52 changes: 52 additions & 0 deletions app/controllers/base_messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class BaseMessagesController < ApplicationController
before_action :set_message, only: %i[show edit update destroy]
before_action :set_chat

# GET /messages or /messages.json
def index
@messages = if @chat
@chat.messages.order(created_at: :asc)
else
Message.all.order(created_at: :asc)
end

@messages
end

# GET /messages/1 or /messages/1.json
def show; end

# POST /messages or /messages.json
def create
@message = Message.new(message_params)
@message.chat_id = @chat.id
@message.user_id = current_user.id
@message.from = :user

respond_to do |format|
if @message.save
format.html { redirect_to @chat }
format.json { render :show, status: :created, location: @message }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @message.errors, status: :unprocessable_entity }
end
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_message
@message = Message.find(params[:id])
end

def set_chat
@chat = Chat.find(params[:chat_id])
end

# Only allow a list of trusted parameters through.
def message_params
params.require(:message).permit(:chat_id, :content, :from)
end
end
56 changes: 1 addition & 55 deletions app/controllers/chats_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
class ChatsController < ApplicationController
before_action :set_chat, only: %i[show edit update destroy]

# GET /chats or /chats.json
def index
@chats = if params[:all].present?
Chat.all.page(params[:page])
else
Chat.where(user_id: current_user.id).page(params[:page])
end
end

class ChatsController < BaseChatsController
# GET /chats/1 or /chats/1.json
def show
@show_footer = false
Expand All @@ -23,47 +12,4 @@ def new

# GET /chats/1/edit
def edit; end

# POST /chats or /chats.json
def create
@chat = Chat.new(chat_params)
@chat.user_id = current_user.id

if assistant = Assistant.find_by(id: params[:assistant_id])
@chat.assistant = assistant
end

respond_to do |format|
if @chat.save
@chat.messages.create(content: @chat.first_message, user_id: @chat.user_id, from: :user)
format.html { redirect_to chat_url(@chat) }
format.json { render :show, status: :created, location: @chat }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @chat.errors, status: :unprocessable_entity }
end
end
end

# DELETE /chats/1 or /chats/1.json
def destroy
@chat.destroy!

respond_to do |format|
format.html { redirect_to chats_url, notice: 'Chat was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_chat
@chat = Chat.find(params[:id])
end

# Only allow a list of trusted parameters through.
def chat_params
params.require(:chat).permit(:assistant_id, :first_message)
end
end
5 changes: 1 addition & 4 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ def index
@messages
end

# GET /messages/1 or /messages/1.json
def show; end

# GET /messages/new
def new
@message = Message.new
Expand All @@ -28,7 +25,7 @@ def edit; end
def create
@message = Message.new(message_params)
@message.chat_id = @chat.id
@message.user_id = @current_user.id
@message.user_id = current_user.id
@message.from = :user

respond_to do |format|
Expand Down
1 change: 1 addition & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Message < ApplicationRecord
belongs_to :user
after_create :enqueue_generate_message_response_job
validates :from, presence: true
validates :content, presence: true

enum from: { user: 0, assistant: 1 }
enum status: { ready: 0, generating: 1 }
Expand Down
4 changes: 4 additions & 0 deletions app/views/api/v1/assistants/_assistant.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

json.extract! assistant, :id, :name, :libraries, :input, :output, :instructions, :context, :description, :created_at, :updated_at, :status, :quip_url, :confluence_spaces
json.url assistant_url(assistant)
3 changes: 3 additions & 0 deletions app/views/api/v1/assistants/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

json.array! @assistants, partial: 'api/v1/assistants/assistant', as: :assistant
3 changes: 3 additions & 0 deletions app/views/api/v1/assistants/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

json.partial! 'api/v1/assistants/assistant', assistant: @assistant
11 changes: 11 additions & 0 deletions app/views/api/v1/chats/_chat.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

json.extract! chat, :id, :created_at, :updated_at, :first_message, :user_id

# Include URL
json.url chat_url(chat)

# Include all the messages associated with the chat
json.messages chat.messages do |message|
json.extract! message, :id, :content, :created_at, :user_id, :status, :from
end
3 changes: 3 additions & 0 deletions app/views/api/v1/chats/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

json.array! @chats, partial: 'api/v1/chats/chat', as: :chat
3 changes: 3 additions & 0 deletions app/views/api/v1/chats/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

json.partial! 'api/v1/chats/chat', chat: @chat
Loading

0 comments on commit aad86ef

Please sign in to comment.