diff --git a/enterprise/lib/captain/tools/add_label_to_conversation_tool.rb b/enterprise/lib/captain/tools/add_label_to_conversation_tool.rb index 0e1b0b526..429f33b7b 100644 --- a/enterprise/lib/captain/tools/add_label_to_conversation_tool.rb +++ b/enterprise/lib/captain/tools/add_label_to_conversation_tool.rb @@ -1,38 +1,28 @@ class Captain::Tools::AddLabelToConversationTool < Captain::Tools::BasePublicTool description 'Add a label to a conversation' - param :conversation_id, type: 'integer', desc: 'The ID of the conversation' param :label_name, type: 'string', desc: 'The name of the label to add' - def perform(_tool_context, conversation_id:, label_name:) + 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? - return error_response('Conversation ID is required') if conversation_id.blank? - return error_response('Label name is required') if label_name.blank? - - conversation = find_conversation(conversation_id) - return error_response('Conversation not found') unless conversation - - label = find_or_create_label(label_name) - return error_response('Failed to find or create label') unless label + 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) + log_tool_usage('added_label', conversation_id: conversation.id, label: label_name) - success_response(conversation, label_name) + "Label '#{label_name}' added to conversation ##{conversation.display_id}" end private - def find_conversation(conversation_id) - account_scoped(Conversation).find_by(id: conversation_id) - end - - def find_or_create_label(label_name) - account_scoped(Label).find_or_create_by(title: label_name) - rescue ActiveRecord::RecordInvalid => e - Rails.logger.error "Failed to create label: #{e.message}" - nil + def find_label(label_name) + account_scoped(Label).find_by(title: label_name) end def add_label_to_conversation(conversation, label_name) @@ -41,22 +31,4 @@ class Captain::Tools::AddLabelToConversationTool < Captain::Tools::BasePublicToo Rails.logger.error "Failed to add label to conversation: #{e.message}" raise end - - def success_response(conversation, label_name) - { - success: true, - message: "Label '#{label_name}' added to conversation ##{conversation.display_id}", - conversation_id: conversation.id, - display_id: conversation.display_id, - label: label_name, - all_labels: conversation.label_list - } - end - - def error_response(message) - { - success: false, - error: message - } - end end diff --git a/enterprise/lib/captain/tools/add_private_note_tool.rb b/enterprise/lib/captain/tools/add_private_note_tool.rb index aa98ec03c..4a2328d43 100644 --- a/enterprise/lib/captain/tools/add_private_note_tool.rb +++ b/enterprise/lib/captain/tools/add_private_note_tool.rb @@ -1,33 +1,26 @@ class Captain::Tools::AddPrivateNoteTool < Captain::Tools::BasePublicTool description 'Add a private note to a conversation' - param :conversation_id, type: 'string', desc: 'The display ID of the conversation' param :note, type: 'string', desc: 'The private note content' - def perform(_tool_context, conversation_id:, note:) - log_tool_usage('add_private_note', { conversation_id: conversation_id, note_length: note.length }) - - return 'Missing required parameters: conversation_id, note' if conversation_id.blank? || note.blank? - - conversation = find_conversation(conversation_id) + def perform(tool_context, note:) + conversation = find_conversation(tool_context.state) return 'Conversation not found' unless conversation + log_tool_usage('add_private_note', { conversation_id: conversation.id, note_length: note.length }) create_private_note(conversation, note) - "Private note added successfully to conversation #{conversation_id}" + + 'Private note added successfully' end private - def find_conversation(conversation_id) - account_scoped(::Conversation).find_by(display_id: conversation_id) - end - def create_private_note(conversation, note) conversation.messages.create!( account: @assistant.account, inbox: conversation.inbox, - sender: @user, + sender: @assistant, + message_type: :outgoing, content: note, - message_type: 'activity', private: true ) end diff --git a/enterprise/lib/captain/tools/base_public_tool.rb b/enterprise/lib/captain/tools/base_public_tool.rb index 8982e649c..27fa59ac4 100644 --- a/enterprise/lib/captain/tools/base_public_tool.rb +++ b/enterprise/lib/captain/tools/base_public_tool.rb @@ -23,6 +23,13 @@ class Captain::Tools::BasePublicTool < Agents::Tool model_class.where(account_id: @assistant.account_id) end + def find_conversation(state) + conversation_id = state[:conversation][:id] + return nil unless conversation_id + + account_scoped(::Conversation).find_by(id: conversation_id) + end + def log_tool_usage(action, details = {}) Rails.logger.info do "#{self.class.name}: #{action} for assistant #{@assistant&.id} - #{details.inspect}" diff --git a/enterprise/lib/captain/tools/update_priority_tool.rb b/enterprise/lib/captain/tools/update_priority_tool.rb index 1396bb4a1..8fc75f601 100644 --- a/enterprise/lib/captain/tools/update_priority_tool.rb +++ b/enterprise/lib/captain/tools/update_priority_tool.rb @@ -1,40 +1,25 @@ class Captain::Tools::UpdatePriorityTool < Captain::Tools::BasePublicTool description 'Update the priority of a conversation' - param :conversation_id, type: 'string', desc: 'The display ID of the conversation' param :priority, type: 'string', desc: 'The priority level: low, medium, high, urgent, or nil to remove priority' - def perform(_tool_context, conversation_id:, priority:) - log_tool_usage('update_priority', { conversation_id: conversation_id, priority: priority }) + def perform(tool_context, priority:) + @conversation = find_conversation(tool_context.state) + return 'Conversation not found' unless @conversation - error = validate_and_prepare(conversation_id, priority) - return error if error + @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 validate_and_prepare(conversation_id, priority) - return 'Missing required parameter: conversation_id' if conversation_id.blank? - - @conversation = find_conversation(conversation_id) - return 'Conversation not found' unless @conversation - - @normalized_priority = normalize_priority(priority) - return "Invalid priority. Valid options: #{valid_priority_options}" unless valid_priority?(@normalized_priority) - - @conversation_id = conversation_id - nil - end - def execute_priority_update update_conversation_priority(@conversation, @normalized_priority) priority_text = @normalized_priority || 'none' - "Priority updated to '#{priority_text}' for conversation #{@conversation_id}" - end - - def find_conversation(conversation_id) - account_scoped(::Conversation).find_by(display_id: conversation_id) + "Priority updated to '#{priority_text}' for conversation ##{@conversation.display_id}" end def normalize_priority(priority)