-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial api * api views * fix tests * adjust fields * show messages * add messages api * message tests
- Loading branch information
1 parent
c47fb7d
commit aad86ef
Showing
24 changed files
with
350 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.