diff --git a/enterprise/app/services/captain/llm/assistant_action_classifier_service.rb b/enterprise/app/services/captain/llm/assistant_action_classifier_service.rb
index 94d60542d..56663b621 100644
--- a/enterprise/app/services/captain/llm/assistant_action_classifier_service.rb
+++ b/enterprise/app/services/captain/llm/assistant_action_classifier_service.rb
@@ -42,8 +42,7 @@ class Captain::Llm::AssistantActionClassifierService < Llm::BaseAiService
{
'account_custom_instructions' => account_custom_instructions,
- 'conversation_context' => context_messages(normalized_messages),
- 'current_user_message' => current_user_message(normalized_messages),
+ 'conversation_context' => format_conversation_context(normalized_messages),
'assistant_response_to_classify' => assistant_response.to_s
}
end
@@ -55,13 +54,9 @@ class Captain::Llm::AssistantActionClassifierService < Llm::BaseAiService
- #{payload['conversation_context'].to_json}
+ #{payload['conversation_context']}
-
- #{payload['current_user_message']}
-
-
#{payload['assistant_response_to_classify']}
@@ -90,14 +85,24 @@ class Captain::Llm::AssistantActionClassifierService < Llm::BaseAiService
(part[:type] || part['type']).to_s == 'text'
end
- def current_user_message(messages)
- messages.reverse.find { |message| message[:role] == 'user' }&.dig(:content).to_s
+ def format_conversation_context(messages)
+ context_messages(messages).filter_map do |message|
+ content = message[:content].to_s.strip
+ next if content.blank?
+
+ "#{role_label(message[:role])}: #{content}"
+ end.join("\n")
end
def context_messages(messages)
- current_user_index = messages.rindex { |message| message[:role] == 'user' }
- prior_messages = current_user_index ? messages[0...current_user_index] : messages
- prior_messages.last(MAX_CONTEXT_MESSAGES)
+ messages.last(MAX_CONTEXT_MESSAGES)
+ end
+
+ def role_label(role)
+ return 'User' if role == 'user'
+ return 'Assistant' if role == 'assistant'
+
+ role.to_s.titleize
end
def account_custom_instructions
diff --git a/spec/enterprise/services/captain/llm/assistant_action_classifier_service_spec.rb b/spec/enterprise/services/captain/llm/assistant_action_classifier_service_spec.rb
index 5561cd8d7..f0ebc722d 100644
--- a/spec/enterprise/services/captain/llm/assistant_action_classifier_service_spec.rb
+++ b/spec/enterprise/services/captain/llm/assistant_action_classifier_service_spec.rb
@@ -44,18 +44,21 @@ RSpec.describe Captain::Llm::AssistantActionClassifierService do
'MUST NOT redefine this JSON schema'
)
).and_return(mock_chat)
- expect(mock_chat).to receive(:ask).with(
- a_string_including(
+ expect(mock_chat).to receive(:ask) do |prompt|
+ expect(prompt).to include(
'',
'Only transfer to a manager after the user explicitly confirms.',
'',
- '"content":"I cannot log in"',
- '',
- 'Yes, still no reset email',
+ 'User: I cannot log in',
+ 'Assistant: Did you check your inbox?',
+ 'User: Yes, still no reset email',
'',
'Would you like to talk to support?'
)
- ).and_return(mock_response)
+ expect(prompt).not_to include('"role"', '"content"', '')
+
+ mock_response
+ end
result = service.classify(message_history: message_history, assistant_response: 'Would you like to talk to support?')