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
This commit is contained in:
@@ -42,7 +42,7 @@ class Captain::Llm::AssistantChatService < Llm::BaseAiService
|
||||
{
|
||||
role: 'system',
|
||||
content: Captain::Llm::SystemPromptsService.assistant_response_generator(
|
||||
@assistant.name, @assistant.config['product_name'], @assistant.config,
|
||||
@assistant.name, @assistant.config['product_name'], @assistant.config.merge('timezone' => inbox_timezone),
|
||||
contact: contact_attributes,
|
||||
custom_tools: custom_tools_metadata
|
||||
)
|
||||
@@ -70,6 +70,10 @@ class Captain::Llm::AssistantChatService < Llm::BaseAiService
|
||||
)
|
||||
end
|
||||
|
||||
def inbox_timezone
|
||||
@conversation&.inbox&.timezone.presence || 'UTC'
|
||||
end
|
||||
|
||||
def persist_message(message, message_type = 'assistant')
|
||||
# No need to implement
|
||||
end
|
||||
|
||||
@@ -175,6 +175,13 @@ class Captain::Llm::SystemPromptsService
|
||||
[Identity]
|
||||
Your name is #{assistant_name || 'Captain'}, a helpful, friendly, and knowledgeable assistant for the product #{product_name}. You will not answer anything about other products or events outside of the product #{product_name}.
|
||||
|
||||
[Current Time]
|
||||
Current time: #{format_current_time(config['timezone'])}.
|
||||
|
||||
Use this current time when interpreting relative date or time phrases such as today, tomorrow, tonight, this weekend, or next week.
|
||||
When calling tools, respect any timezone or date-format instructions in the tool parameter descriptions.
|
||||
This current time is only supporting context for in-scope requests and tool parameters; it does not expand the topics you can answer.
|
||||
|
||||
[Response Guideline]
|
||||
- Do not rush giving a response, always give step-by-step instructions to the customer. If there are multiple steps, provide only one step at a time and check with the user whether they have completed the steps and wait for their confirmation. If the user has said okay or yes, continue with the steps.
|
||||
- Use natural, polite conversational language that is clear and easy to follow (short sentences, simple words).
|
||||
@@ -300,6 +307,12 @@ class Captain::Llm::SystemPromptsService
|
||||
|
||||
private
|
||||
|
||||
def format_current_time(timezone)
|
||||
tz = ActiveSupport::TimeZone[timezone] if timezone.present?
|
||||
time = tz ? Time.current.in_time_zone(tz) : Time.current
|
||||
time.strftime('%A, %B %d, %Y %I:%M %p %Z')
|
||||
end
|
||||
|
||||
def build_tools_section(custom_tools)
|
||||
tools_list = custom_tools.map { |t| "- #{t[:name]}: #{t[:description]}" }.join("\n")
|
||||
<<~TOOLS.strip
|
||||
|
||||
Reference in New Issue
Block a user