iachat/enterprise/app/services/captain/llm/assistant_chat_service.rb
Aakash Bakhle 290dd3abf5
feat: allow captain to access contact attributes (#13850)
# Pull Request Template

## Description

Captain v1 does not have access to contact attributes. Added a toggle to
let user choose if they want contact information available to Captain.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration.

Specs and locally
<img width="1924" height="740" alt="CleanShot 2026-03-19 at 18 48 19@2x"
src="https://github.com/user-attachments/assets/353cfeaa-cd58-40eb-89e7-d660a1dc1185"
/>

![Uploading CleanShot 2026-03-19 at 18.53.26@2x.png…]()


## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published in downstream
modules
2026-03-20 16:15:06 +05:30

63 lines
1.9 KiB
Ruby

class Captain::Llm::AssistantChatService < Llm::BaseAiService
include Captain::ChatHelper
def initialize(assistant: nil, conversation: nil, source: nil)
super()
@assistant = assistant
@conversation = conversation
@conversation_id = conversation&.display_id
@source = source
@messages = [system_message]
@response = ''
@tools = build_tools
end
# additional_message: A single message (String) from the user that should be appended to the chat.
# It can be an empty String or nil when you only want to supply historical messages.
# message_history: An Array of already formatted messages that provide the previous context.
# role: The role for the additional_message (defaults to `user`).
#
# NOTE: Parameters are provided as keyword arguments to improve clarity and avoid relying on
# positional ordering.
def generate_response(additional_message: nil, message_history: [], role: 'user')
@messages += message_history
@messages << { role: role, content: additional_message } if additional_message.present?
request_chat_completion
end
private
def build_tools
[Captain::Tools::SearchDocumentationService.new(@assistant, user: nil)]
end
def system_message
{
role: 'system',
content: Captain::Llm::SystemPromptsService.assistant_response_generator(
@assistant.name, @assistant.config['product_name'], @assistant.config,
contact: contact_attributes
)
}
end
def contact_attributes
return nil unless @conversation&.contact
return nil unless @assistant&.feature_contact_attributes
@conversation.contact.attributes.symbolize_keys.slice(
:id, :name, :email, :phone_number, :identifier, :custom_attributes
)
end
def persist_message(message, message_type = 'assistant')
# No need to implement
end
def feature_name
'assistant'
end
end