Files
chatwoot/enterprise/app/services/captain/llm/assistant_chat_service.rb
Aakash BakhleandGitHub 279dd1876c fix: make captain datetime aware (#14069)
# Pull Request Template

## Description

Captain currently cannot discern today, tomorrow etc. This PR adds
datetime awareness to the system prompt

Fixes:
https://linear.app/chatwoot/issue/AI-148/captain-should-be-aware-of-datetime

## 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.

Locally

<img width="696" height="247" alt="CleanShot 2026-04-27 at 14 47 47"
src="https://github.com/user-attachments/assets/6a73a8d9-f48e-46bb-a306-7b9a28a5fa9c"
/>


## 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-04-27 15:51:49 +05:30

85 lines
2.6 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.merge('timezone' => inbox_timezone),
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 inbox_timezone
@conversation&.inbox&.timezone.presence || 'UTC'
end
def persist_message(message, message_type = 'assistant')
# No need to implement
end
def feature_name
'assistant'
end
end