# Pull Request Template ## Description Adds custom tool support to v1 ## Type of change - [x] New feature (non-breaking change which adds functionality) ## 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. <img width="1816" height="958" alt="CleanShot 2026-03-24 at 11 37 33@2x" src="https://github.com/user-attachments/assets/2777a953-8b65-4a2d-88ec-39f395b3fb47" /> <img width="378" height="488" alt="CleanShot 2026-03-24 at 11 38 18@2x" src="https://github.com/user-attachments/assets/f6973c99-efd0-40e4-90fe-4472a2f63cea" /> <img width="1884" height="1452" alt="CleanShot 2026-03-24 at 11 38 32@2x" src="https://github.com/user-attachments/assets/9fba4fc4-0c33-46da-888a-52ec6bad6130" /> ## Checklist: - [x] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
81 lines
2.4 KiB
Ruby
81 lines
2.4 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
|
|
tools = [Captain::Tools::SearchDocumentationService.new(@assistant, user: nil)]
|
|
return tools unless custom_tools_enabled?
|
|
|
|
tools + @assistant.account.captain_custom_tools.enabled.map do |ct|
|
|
ct.tool(@assistant, base_class: Captain::Tools::CustomHttpTool, conversation: @conversation)
|
|
end
|
|
end
|
|
|
|
def system_message
|
|
{
|
|
role: 'system',
|
|
content: Captain::Llm::SystemPromptsService.assistant_response_generator(
|
|
@assistant.name, @assistant.config['product_name'], @assistant.config,
|
|
contact: contact_attributes,
|
|
custom_tools: custom_tools_metadata
|
|
)
|
|
}
|
|
end
|
|
|
|
def custom_tools_metadata
|
|
return [] unless custom_tools_enabled?
|
|
|
|
@assistant.account.captain_custom_tools.enabled.map do |ct|
|
|
{ name: ct.slug, description: ct.description }
|
|
end
|
|
end
|
|
|
|
def custom_tools_enabled?
|
|
@assistant.account.feature_enabled?('custom_tools')
|
|
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
|