feat: scenario tools [CW-4597] (#11908)
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
26
enterprise/lib/captain/tools/add_contact_note_tool.rb
Normal file
26
enterprise/lib/captain/tools/add_contact_note_tool.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
class Captain::Tools::AddContactNoteTool < Captain::Tools::BasePublicTool
|
||||
description 'Add a note to a contact profile'
|
||||
param :note, type: 'string', desc: 'The note content to add to the contact'
|
||||
|
||||
def perform(tool_context, note:)
|
||||
contact = find_contact(tool_context.state)
|
||||
return 'Contact not found' unless contact
|
||||
|
||||
return 'Note content is required' if note.blank?
|
||||
|
||||
log_tool_usage('add_contact_note', { contact_id: contact.id, note_length: note.length })
|
||||
|
||||
create_contact_note(contact, note)
|
||||
"Note added successfully to contact #{contact.name} (ID: #{contact.id})"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_contact_note(contact, note)
|
||||
contact.notes.create!(content: note)
|
||||
end
|
||||
|
||||
def permissions
|
||||
%w[contact_manage]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,34 @@
|
||||
class Captain::Tools::AddLabelToConversationTool < Captain::Tools::BasePublicTool
|
||||
description 'Add a label to a conversation'
|
||||
param :label_name, type: 'string', desc: 'The name of the label to add'
|
||||
|
||||
def perform(tool_context, label_name:)
|
||||
conversation = find_conversation(tool_context.state)
|
||||
return 'Conversation not found' unless conversation
|
||||
|
||||
label_name = label_name&.strip&.downcase
|
||||
return 'Label name is required' if label_name.blank?
|
||||
|
||||
label = find_label(label_name)
|
||||
return 'Label not found' unless label
|
||||
|
||||
add_label_to_conversation(conversation, label_name)
|
||||
|
||||
log_tool_usage('added_label', conversation_id: conversation.id, label: label_name)
|
||||
|
||||
"Label '#{label_name}' added to conversation ##{conversation.display_id}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_label(label_name)
|
||||
account_scoped(Label).find_by(title: label_name)
|
||||
end
|
||||
|
||||
def add_label_to_conversation(conversation, label_name)
|
||||
conversation.add_labels(label_name)
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "Failed to add label to conversation: #{e.message}"
|
||||
raise
|
||||
end
|
||||
end
|
||||
33
enterprise/lib/captain/tools/add_private_note_tool.rb
Normal file
33
enterprise/lib/captain/tools/add_private_note_tool.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
class Captain::Tools::AddPrivateNoteTool < Captain::Tools::BasePublicTool
|
||||
description 'Add a private note to a conversation'
|
||||
param :note, type: 'string', desc: 'The private note content'
|
||||
|
||||
def perform(tool_context, note:)
|
||||
conversation = find_conversation(tool_context.state)
|
||||
return 'Conversation not found' unless conversation
|
||||
|
||||
return 'Note content is required' if note.blank?
|
||||
|
||||
log_tool_usage('add_private_note', { conversation_id: conversation.id, note_length: note.length })
|
||||
create_private_note(conversation, note)
|
||||
|
||||
'Private note added successfully'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_private_note(conversation, note)
|
||||
conversation.messages.create!(
|
||||
account: @assistant.account,
|
||||
inbox: conversation.inbox,
|
||||
sender: @assistant,
|
||||
message_type: :outgoing,
|
||||
content: note,
|
||||
private: true
|
||||
)
|
||||
end
|
||||
|
||||
def permissions
|
||||
%w[conversation_manage conversation_unassigned_manage conversation_participating_manage]
|
||||
end
|
||||
end
|
||||
45
enterprise/lib/captain/tools/base_public_tool.rb
Normal file
45
enterprise/lib/captain/tools/base_public_tool.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
require 'agents'
|
||||
|
||||
class Captain::Tools::BasePublicTool < Agents::Tool
|
||||
def initialize(assistant)
|
||||
@assistant = assistant
|
||||
super()
|
||||
end
|
||||
|
||||
def active?
|
||||
# Public tools are always active
|
||||
true
|
||||
end
|
||||
|
||||
def permissions
|
||||
# Override in subclasses to specify required permissions
|
||||
# Returns empty array for public tools (no permissions required)
|
||||
[]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def account_scoped(model_class)
|
||||
model_class.where(account_id: @assistant.account_id)
|
||||
end
|
||||
|
||||
def find_conversation(state)
|
||||
conversation_id = state&.dig(:conversation, :id)
|
||||
return nil unless conversation_id
|
||||
|
||||
account_scoped(::Conversation).find_by(id: conversation_id)
|
||||
end
|
||||
|
||||
def find_contact(state)
|
||||
contact_id = state&.dig(:contact, :id)
|
||||
return nil unless contact_id
|
||||
|
||||
account_scoped(::Contact).find_by(id: contact_id)
|
||||
end
|
||||
|
||||
def log_tool_usage(action, details = {})
|
||||
Rails.logger.info do
|
||||
"#{self.class.name}: #{action} for assistant #{@assistant&.id} - #{details.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
48
enterprise/lib/captain/tools/update_priority_tool.rb
Normal file
48
enterprise/lib/captain/tools/update_priority_tool.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
class Captain::Tools::UpdatePriorityTool < Captain::Tools::BasePublicTool
|
||||
description 'Update the priority of a conversation'
|
||||
param :priority, type: 'string', desc: 'The priority level: low, medium, high, urgent, or nil to remove priority'
|
||||
|
||||
def perform(tool_context, priority:)
|
||||
@conversation = find_conversation(tool_context.state)
|
||||
return 'Conversation not found' unless @conversation
|
||||
|
||||
@normalized_priority = normalize_priority(priority)
|
||||
return "Invalid priority. Valid options: #{valid_priority_options}" unless valid_priority?(@normalized_priority)
|
||||
|
||||
log_tool_usage('update_priority', { conversation_id: @conversation.id, priority: priority })
|
||||
|
||||
execute_priority_update
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def execute_priority_update
|
||||
update_conversation_priority(@conversation, @normalized_priority)
|
||||
priority_text = @normalized_priority || 'none'
|
||||
"Priority updated to '#{priority_text}' for conversation ##{@conversation.display_id}"
|
||||
end
|
||||
|
||||
def normalize_priority(priority)
|
||||
priority == 'nil' || priority.blank? ? nil : priority
|
||||
end
|
||||
|
||||
def valid_priority?(priority)
|
||||
valid_priorities.include?(priority)
|
||||
end
|
||||
|
||||
def valid_priorities
|
||||
@valid_priorities ||= [nil] + Conversation.priorities.keys
|
||||
end
|
||||
|
||||
def valid_priority_options
|
||||
(valid_priorities.compact + ['nil']).join(', ')
|
||||
end
|
||||
|
||||
def update_conversation_priority(conversation, priority)
|
||||
conversation.update!(priority: priority)
|
||||
end
|
||||
|
||||
def permissions
|
||||
%w[conversation_manage conversation_unassigned_manage conversation_participating_manage]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user