Files
leadchat/enterprise/app/services/captain/assistant/trace_payload_helper.rb
Aakash Bakhle 7cec4ebaae feat: support multimodal user messages in captain v2 (#13581)
Extract and pass image attachments from the latest user message to the
runner,
excluding the last user message from the context for processing.

Fixes #13588 

# Pull Request Template

## Description

Adds image support to captain v2

## Type of change

Please delete options that are not relevant.

- [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 local testing

<img width="754" height="1008" alt="image"
src="https://github.com/user-attachments/assets/914cbc2c-9d30-42d0-87d4-9e5430845c87"
/>

langfuse also shows media correctly with the instrumentation code:
<img width="1800" height="1260" alt="image"
src="https://github.com/user-attachments/assets/ce0f5fa6-b1a5-42ec-a213-9a82b1751037"
/>


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

---------

Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 19:37:41 +05:30

52 lines
1.3 KiB
Ruby

module Captain::Assistant::TracePayloadHelper
private
def enrich_context_with_trace_payload!(context, message_history, message_to_process)
context[:captain_v2_trace_input] = serialize_trace_messages(message_history)
context[:captain_v2_trace_current_input] = serialize_trace_content(message_to_process)
end
def serialize_trace_messages(message_history)
message_history.map do |message|
{
role: message[:role].to_s,
content: trace_content_payload(message[:content])
}
end.to_json
end
def serialize_trace_content(content)
payload = trace_content_payload(content)
return '' if payload.blank?
payload.is_a?(String) ? payload : payload.to_json
end
def trace_content_payload(content)
case content
when RubyLLM::Content
trace_parts_from_ruby_llm_content(content)
when Array, Hash
content
when NilClass
''
else
content.to_s
end
end
def trace_parts_from_ruby_llm_content(content)
parts = []
parts << { type: 'text', text: content.text } if content.text.present?
content.attachments.each do |attachment|
parts << { type: 'image_url', image_url: { url: attachment.source.to_s } }
end
return '' if parts.blank?
return parts.first[:text] if parts.one? && parts.first[:type] == 'text'
parts
end
end