From 96746cc592848e690499c354d262e7119218ef99 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 9 Jul 2025 13:39:24 +0530 Subject: [PATCH] feat: add scenarios model --- ...20250709075622_create_captain_scenarios.rb | 18 +++++++ db/schema.rb | 19 +++++++- enterprise/app/models/captain/assistant.rb | 1 + enterprise/app/models/captain/scenario.rb | 48 +++++++++++++++++++ spec/factories/captain/scenario.rb | 11 +++++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20250709075622_create_captain_scenarios.rb create mode 100644 enterprise/app/models/captain/scenario.rb create mode 100644 spec/factories/captain/scenario.rb diff --git a/db/migrate/20250709075622_create_captain_scenarios.rb b/db/migrate/20250709075622_create_captain_scenarios.rb new file mode 100644 index 000000000..d705d1033 --- /dev/null +++ b/db/migrate/20250709075622_create_captain_scenarios.rb @@ -0,0 +1,18 @@ +class CreateCaptainScenarios < ActiveRecord::Migration[7.1] + def change + create_table :captain_scenarios do |t| + t.string :title + t.text :description + t.text :instruction + t.jsonb :tools + t.boolean :enabled, default: true, null: false + t.references :assistant, null: false, foreign_key: { to_table: :captain_assistants } + t.references :account, null: false + + t.timestamps + end + + add_index :captain_scenarios, :enabled + add_index :captain_scenarios, [:assistant_id, :enabled] + end +end diff --git a/db/schema.rb b/db/schema.rb index af0c89086..b3cb528cb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2025_06_27_195529) do +ActiveRecord::Schema[7.1].define(version: 2025_07_09_075622) do # These extensions should be enabled to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm" @@ -305,6 +305,22 @@ ActiveRecord::Schema[7.1].define(version: 2025_06_27_195529) do t.index ["inbox_id"], name: "index_captain_inboxes_on_inbox_id" end + create_table "captain_scenarios", force: :cascade do |t| + t.string "title" + t.text "description" + t.text "instruction" + t.jsonb "tools" + t.boolean "enabled", default: true, null: false + t.bigint "assistant_id", null: false + t.bigint "account_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["account_id"], name: "index_captain_scenarios_on_account_id" + t.index ["assistant_id", "enabled"], name: "index_captain_scenarios_on_assistant_id_and_enabled" + t.index ["assistant_id"], name: "index_captain_scenarios_on_assistant_id" + t.index ["enabled"], name: "index_captain_scenarios_on_enabled" + end + create_table "categories", force: :cascade do |t| t.integer "account_id", null: false t.integer "portal_id", null: false @@ -1114,6 +1130,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_06_27_195529) do add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" + add_foreign_key "captain_scenarios", "captain_assistants", column: "assistant_id" add_foreign_key "inboxes", "portals" create_trigger("accounts_after_insert_row_tr", :generated => true, :compatibility => 1). on("accounts"). diff --git a/enterprise/app/models/captain/assistant.rb b/enterprise/app/models/captain/assistant.rb index 40cf99df9..8f1227231 100644 --- a/enterprise/app/models/captain/assistant.rb +++ b/enterprise/app/models/captain/assistant.rb @@ -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 diff --git a/enterprise/app/models/captain/scenario.rb b/enterprise/app/models/captain/scenario.rb new file mode 100644 index 000000000..0878ab2e9 --- /dev/null +++ b/enterprise/app/models/captain/scenario.rb @@ -0,0 +1,48 @@ +# == 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) +# +# Foreign Keys +# +# fk_rails_... (assistant_id => captain_assistants.id) +# +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 diff --git a/spec/factories/captain/scenario.rb b/spec/factories/captain/scenario.rb new file mode 100644 index 000000000..937106f58 --- /dev/null +++ b/spec/factories/captain/scenario.rb @@ -0,0 +1,11 @@ +FactoryBot.define do + factory :captain_scenario, class: 'Captain::Scenario' do + sequence(:title) { |n| "Scenario #{n}" } + description { 'Test scenario description' } + instruction { 'Test scenario instruction for the assistant to follow' } + tools { nil } + enabled { true } + association :assistant, factory: :captain_assistant + association :account + end +end \ No newline at end of file