Federico Mariani created CAMEL-23740:
----------------------------------------

             Summary: LangChain4jAgentConverter WrappedFile converter fails for 
FTP/SFTP GenericFile — requires manual convertBodyTo workaround
                 Key: CAMEL-23740
                 URL: https://issues.apache.org/jira/browse/CAMEL-23740
             Project: Camel
          Issue Type: Bug
          Components: camel-langchain4j-agent
    Affects Versions: 4.20.0
            Reporter: Federico Mariani


The _LangChain4jAgentConverter.toAiAgentBody(WrappedFile)_ type converter 
assumes the wrapped file is always a _java.io.File_:

{code:java}
  Object fileObj = wrappedFile.getFile();
  if (!(fileObj instanceof File)) {
      throw new IllegalArgumentException(
          "WrappedFile must contain a java.io.File instance, got: " + 
fileObj.getClass().getName());
  }
{code}


This fails for FTP and SFTP components, which produce _GenericFile<FTPFile> / 
GenericFile<SftpRemoteFile>_. The underlying file object is not a 
_java.io.File_, so the converter throws IllegalArgumentException.

Since this is the most specific *@Converter* match for the body type, Camel 
picks it over the byte[] or InputStream converters. The exception gets caught 
by the route's error handler (retry + DLQ), making the failure silent and hard 
to diagnose.

*Workaround*: Add _convertBodyTo: byte[]_ before _to: langchain4j-agent:..._ to 
force the body through the byteArrayToAiAgentBody converter, which works 
correctly (it detects MIME type from the CamelFileName header).

*Expected behavior*: The WrappedFile converter should handle remote file types 
by falling back to reading the body as byte[] when getFile() does not return a 
java.io.File. For example:

{code:java}
  if (!(fileObj instanceof File)) {
      byte[] data = exchange.getContext().getTypeConverter()
          .convertTo(byte[].class, exchange, wrappedFile);
      String mimeType = detectMimeTypeFromHeaders(exchange);
      Content content = createContent(data, mimeType);
      return buildAiAgentBody(exchange, content, "");
  }
{code}

Affected components: camel-langchain4j-agent with camel-ftp or camel-ftp (SFTP)

*Reproducer*: A YAML route that reads images from FTP and sends them to 
langchain4j-agent:

{code:java}
  - route:
      id: ftp-to-agent
      from:
        uri: "ftp:localhost:2121/images?noop=true&binary=true"
        steps:
          - setHeader:
              name: CamelLangChain4jAgentUserMessage
              constant:
                expression: "Describe this image"
          # Without this line, the WrappedFile converter fails silently
          # - convertBodyTo:
          #     type: "byte[]"
          - to:
              uri: "langchain4j-agent:my-agent"
{code}




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to