feat: add Captain::Scenario Model and API [CW-4597] (#11907)
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
class Api::V1::Accounts::Captain::ScenariosController < Api::V1::Accounts::BaseController
|
||||
before_action :current_account
|
||||
before_action -> { check_authorization(Captain::Scenario) }
|
||||
before_action :set_assistant
|
||||
before_action :set_scenario, only: [:show, :update, :destroy]
|
||||
|
||||
def index
|
||||
@scenarios = assistant_scenarios.enabled
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
def create
|
||||
@scenario = assistant_scenarios.create!(scenario_params.merge(account: Current.account))
|
||||
end
|
||||
|
||||
def update
|
||||
@scenario.update!(scenario_params)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@scenario.destroy
|
||||
head :no_content
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_assistant
|
||||
@assistant = account_assistants.find(params[:assistant_id])
|
||||
end
|
||||
|
||||
def account_assistants
|
||||
@account_assistants ||= Current.account.captain_assistants
|
||||
end
|
||||
|
||||
def set_scenario
|
||||
@scenario = assistant_scenarios.find(params[:id])
|
||||
end
|
||||
|
||||
def assistant_scenarios
|
||||
@assistant.scenarios
|
||||
end
|
||||
|
||||
def scenario_params
|
||||
params.require(:scenario).permit(:title, :description, :instruction, :enabled, tools: [])
|
||||
end
|
||||
end
|
||||
@@ -30,6 +30,7 @@ class Captain::Assistant < ApplicationRecord
|
||||
through: :captain_inboxes
|
||||
has_many :messages, as: :sender, dependent: :nullify
|
||||
has_many :copilot_threads, dependent: :destroy_async
|
||||
has_many :scenarios, class_name: 'Captain::Scenario', dependent: :destroy_async
|
||||
|
||||
validates :name, presence: true
|
||||
validates :description, presence: true
|
||||
|
||||
44
enterprise/app/models/captain/scenario.rb
Normal file
44
enterprise/app/models/captain/scenario.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: captain_scenarios
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# description :text
|
||||
# enabled :boolean default(TRUE), not null
|
||||
# instruction :text
|
||||
# title :string
|
||||
# tools :jsonb
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint not null
|
||||
# assistant_id :bigint not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_captain_scenarios_on_account_id (account_id)
|
||||
# index_captain_scenarios_on_assistant_id (assistant_id)
|
||||
# index_captain_scenarios_on_assistant_id_and_enabled (assistant_id,enabled)
|
||||
# index_captain_scenarios_on_enabled (enabled)
|
||||
#
|
||||
class Captain::Scenario < ApplicationRecord
|
||||
self.table_name = 'captain_scenarios'
|
||||
|
||||
belongs_to :assistant, class_name: 'Captain::Assistant'
|
||||
belongs_to :account
|
||||
|
||||
validates :title, presence: true
|
||||
validates :description, presence: true
|
||||
validates :instruction, presence: true
|
||||
validates :assistant_id, presence: true
|
||||
validates :account_id, presence: true
|
||||
|
||||
scope :enabled, -> { where(enabled: true) }
|
||||
|
||||
before_save :populate_tools
|
||||
|
||||
private
|
||||
|
||||
def populate_tools
|
||||
# TODO: Implement tools population logic
|
||||
end
|
||||
end
|
||||
21
enterprise/app/policies/captain/scenario_policy.rb
Normal file
21
enterprise/app/policies/captain/scenario_policy.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class Captain::ScenarioPolicy < ApplicationPolicy
|
||||
def index?
|
||||
true
|
||||
end
|
||||
|
||||
def show?
|
||||
true
|
||||
end
|
||||
|
||||
def create?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def update?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def destroy?
|
||||
@account_user.administrator?
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/captain/scenario', scenario: @scenario
|
||||
@@ -0,0 +1,5 @@
|
||||
json.data do
|
||||
json.array! @scenarios do |scenario|
|
||||
json.partial! 'api/v1/models/captain/scenario', scenario: scenario
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/captain/scenario', scenario: @scenario
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! 'api/v1/models/captain/scenario', scenario: @scenario
|
||||
@@ -0,0 +1,16 @@
|
||||
json.id scenario.id
|
||||
json.title scenario.title
|
||||
json.description scenario.description
|
||||
json.instruction scenario.instruction
|
||||
json.tools scenario.tools
|
||||
json.enabled scenario.enabled
|
||||
json.assistant_id scenario.assistant_id
|
||||
json.account_id scenario.account_id
|
||||
json.created_at scenario.created_at
|
||||
json.updated_at scenario.updated_at
|
||||
if scenario.assistant.present?
|
||||
json.assistant do
|
||||
json.id scenario.assistant.id
|
||||
json.name scenario.assistant.name
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user