feat: add response guidelines and guardrails field (#11911)

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Shivam Mishra
2025-07-15 08:56:03 +05:30
committed by GitHub
parent 1132556512
commit 4378506a35
6 changed files with 72 additions and 16 deletions

View File

@@ -0,0 +1,6 @@
class AddResponseGuidelinesAndGuardrailsToCaptainAssistants < ActiveRecord::Migration[7.1]
def change
add_column :captain_assistants, :response_guidelines, :jsonb, default: []
add_column :captain_assistants, :guardrails, :jsonb, default: []
end
end

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2025_07_10_145708) do ActiveRecord::Schema[7.1].define(version: 2025_07_14_104358) do
# These extensions should be enabled to support this database # These extensions should be enabled to support this database
enable_extension "pg_stat_statements" enable_extension "pg_stat_statements"
enable_extension "pg_trgm" enable_extension "pg_trgm"
@@ -277,6 +277,8 @@ ActiveRecord::Schema[7.1].define(version: 2025_07_10_145708) do
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.jsonb "config", default: {}, null: false t.jsonb "config", default: {}, null: false
t.jsonb "response_guidelines", default: []
t.jsonb "guardrails", default: []
t.index ["account_id"], name: "index_captain_assistants_on_account_id" t.index ["account_id"], name: "index_captain_assistants_on_account_id"
end end

View File

@@ -43,12 +43,18 @@ class Api::V1::Accounts::Captain::AssistantsController < Api::V1::Accounts::Base
end end
def assistant_params def assistant_params
params.require(:assistant).permit(:name, :description, permitted = params.require(:assistant).permit(:name, :description,
config: [ config: [
:product_name, :feature_faq, :feature_memory, :product_name, :feature_faq, :feature_memory,
:welcome_message, :handoff_message, :resolution_message, :welcome_message, :handoff_message, :resolution_message,
:instructions, :temperature :instructions, :temperature
]) ])
# Handle array parameters separately to allow partial updates
permitted[:response_guidelines] = params[:assistant][:response_guidelines] if params[:assistant][:response_guidelines].present?
permitted[:guardrails] = params[:assistant][:guardrails] if params[:assistant][:guardrails].present?
permitted
end end
def playground_params def playground_params

View File

@@ -5,7 +5,9 @@
# id :bigint not null, primary key # id :bigint not null, primary key
# config :jsonb not null # config :jsonb not null
# description :string # description :string
# guardrails :jsonb
# name :string not null # name :string not null
# response_guidelines :jsonb
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# account_id :bigint not null # account_id :bigint not null

View File

@@ -2,6 +2,8 @@ json.account_id resource.account_id
json.config resource.config json.config resource.config
json.created_at resource.created_at.to_i json.created_at resource.created_at.to_i
json.description resource.description json.description resource.description
json.guardrails resource.guardrails
json.id resource.id json.id resource.id
json.name resource.name json.name resource.name
json.response_guidelines resource.response_guidelines
json.updated_at resource.updated_at.to_i json.updated_at resource.updated_at.to_i

View File

@@ -62,7 +62,9 @@ RSpec.describe 'Api::V1::Accounts::Captain::Assistants', type: :request do
{ {
assistant: { assistant: {
name: 'New Assistant', name: 'New Assistant',
description: 'Assistant Description' description: 'Assistant Description',
response_guidelines: ['Be helpful', 'Be concise'],
guardrails: ['No harmful content', 'Stay on topic']
} }
} }
end end
@@ -96,6 +98,8 @@ RSpec.describe 'Api::V1::Accounts::Captain::Assistants', type: :request do
end.to change(Captain::Assistant, :count).by(1) end.to change(Captain::Assistant, :count).by(1)
expect(json_response[:name]).to eq('New Assistant') expect(json_response[:name]).to eq('New Assistant')
expect(json_response[:response_guidelines]).to eq(['Be helpful', 'Be concise'])
expect(json_response[:guardrails]).to eq(['No harmful content', 'Stay on topic'])
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
end end
end end
@@ -106,7 +110,9 @@ RSpec.describe 'Api::V1::Accounts::Captain::Assistants', type: :request do
let(:update_attributes) do let(:update_attributes) do
{ {
assistant: { assistant: {
name: 'Updated Assistant' name: 'Updated Assistant',
response_guidelines: ['Updated guideline'],
guardrails: ['Updated guardrail']
} }
} }
end end
@@ -139,6 +145,38 @@ RSpec.describe 'Api::V1::Accounts::Captain::Assistants', type: :request do
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(json_response[:name]).to eq('Updated Assistant') expect(json_response[:name]).to eq('Updated Assistant')
expect(json_response[:response_guidelines]).to eq(['Updated guideline'])
expect(json_response[:guardrails]).to eq(['Updated guardrail'])
end
it 'updates only response_guidelines when only that is provided' do
assistant.update!(response_guidelines: ['Original guideline'], guardrails: ['Original guardrail'])
original_name = assistant.name
patch "/api/v1/accounts/#{account.id}/captain/assistants/#{assistant.id}",
params: { assistant: { response_guidelines: ['New guideline only'] } },
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(json_response[:name]).to eq(original_name)
expect(json_response[:response_guidelines]).to eq(['New guideline only'])
expect(json_response[:guardrails]).to eq(['Original guardrail'])
end
it 'updates only guardrails when only that is provided' do
assistant.update!(response_guidelines: ['Original guideline'], guardrails: ['Original guardrail'])
original_name = assistant.name
patch "/api/v1/accounts/#{account.id}/captain/assistants/#{assistant.id}",
params: { assistant: { guardrails: ['New guardrail only'] } },
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(json_response[:name]).to eq(original_name)
expect(json_response[:response_guidelines]).to eq(['Original guideline'])
expect(json_response[:guardrails]).to eq(['New guardrail only'])
end end
end end
end end