You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "chandru9 (via GitHub)" <gi...@apache.org> on 2026/03/04 15:18:30 UTC

[PR] Camel 22510 avoid leaking internal data types [camel]

chandru9 opened a new pull request, #21704:
URL: https://github.com/apache/camel/pull/21704

   # Description
   
   <!--
   - Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   -->
   
   # Target
   
   - [ ] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch)
   
   # Tracking
   - [ ] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it).
   
   <!--
   # *Note*: trivial changes like, typos, minor documentation fixes and other small items do not require a JIRA issue. In this case your pull request should address just this issue, without pulling in other changes.
   -->
   
   # Apache Camel coding standards and style
   
   - [ ] I checked that each commit in the pull request has a meaningful subject line and body.
   
   <!--
   If you're unsure, you can format the pull request title like `[CAMEL-XXX] Fixes bug in camel-file component`, where you replace `CAMEL-XXX` with the appropriate JIRA issue.
   -->
   
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes.
   
   <!--
   You can run the aforementioned command in your module so that the build auto-formats your code. This will also be verified as part of the checks and your PR may be rejected if if there are uncommited changes after running `mvn clean install -DskipTests`.
   
   You can learn more about the contribution guidelines at https://github.com/apache/camel/blob/main/CONTRIBUTING.md
   -->
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2905627172


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +165,44 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text     String as message
+     * @param  exchange the Camel exchange containing headers
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        TextContent content = TextContent.from(text);
+        return buildAiAgentBody(exchange, content, text);
+    }
+
+    /**
+     * Support Camel stream caching bodies (ByteArrayInputStreamCache) and delegate them to the existing InputStream
+     * converter.
+     *
+     * @param  cache    the cached input stream containing the message body
+     * @param  exchange the Camel exchange containing headers such as * {@code CamelLangChain4jAgentMediaType},
+     *                  user/system messages, * and optional memory identifiers
+     * @return          an {@link AiAgentBody} containing the appropriate * LangChain4j {@link Content} representation
+     */
+    @Converter
+    public static AiAgentBody<?> inputStreamCacheToAiAgentBody(

Review Comment:
   okay would be removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "orpiske (via GitHub)" <gi...@apache.org>.
orpiske commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2900007575


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text
+     * @param  exchange
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        String userMessage = exchange.getIn().getHeader(USER_MESSAGE, String.class);
+        String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE, String.class);
+        Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+        TextContent content = TextContent.from(text);
+
+        AiAgentBody<TextContent> body = new AiAgentBody<>();
+        body.setUserMessage(userMessage != null ? userMessage : text);
+        body.setSystemMessage(systemMessage);
+        body.setMemoryId(memoryId);
+        body.setContent(content);
+        return body;
+    }
+
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.

Review Comment:
   That approach is correct, we just need to make sure any limitation is documented (sorry I wasn't clear in my request earlier)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4006334552

   Thanks, removing that AWT BufferedImage is welcome. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2899529901


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text
+     * @param  exchange
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        String userMessage = exchange.getIn().getHeader(USER_MESSAGE, String.class);
+        String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE, String.class);
+        Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+        TextContent content = TextContent.from(text);
+
+        AiAgentBody<TextContent> body = new AiAgentBody<>();
+        body.setUserMessage(userMessage != null ? userMessage : text);
+        body.setSystemMessage(systemMessage);
+        body.setMemoryId(memoryId);
+        body.setContent(content);
+        return body;
+    }
+
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.

Review Comment:
   @orpiske: thanks for the comments. support for the BufferedImage type would be removed. as suggested by @davsclaus and @jamesnetherton. I am currently adopting the other review comments and push the changes shortly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4029101075

   There are uncommitted changes
   HEAD detached at pull/21704/merge
   Changes not staged for commit:
     (use "git add <file>..." to update what will be committed)
     (use "git restore <file>..." to discard changes in working directory)
   	modified:   components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-3999560895

   use junit6 instead of 5


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "orpiske (via GitHub)" <gi...@apache.org>.
orpiske commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2900007575


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text
+     * @param  exchange
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        String userMessage = exchange.getIn().getHeader(USER_MESSAGE, String.class);
+        String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE, String.class);
+        Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+        TextContent content = TextContent.from(text);
+
+        AiAgentBody<TextContent> body = new AiAgentBody<>();
+        body.setUserMessage(userMessage != null ? userMessage : text);
+        body.setSystemMessage(systemMessage);
+        body.setMemoryId(memoryId);
+        body.setContent(content);
+        return body;
+    }
+
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.

Review Comment:
   That approach is correct, we just need to make sure any limitation or expectation is documented (sorry I wasn't clear in my request earlier)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4033681601

   
   org.apache.camel.component.langchain4j.agent.integration.LangChain4jAgentAutoConversionTest.shouldFailForUnsupportedMimeType | 0.025s | FAILURE
   -- | -- | --
   org.apache.camel.component.langchain4j.agent.integration.LangChain4jAgentAutoConversionTest.shouldAutoConvertInputStream | 0.035s | FAILURE
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4006107483

   @davsclaus : PR intended to improve the usability of the langchain4j-agent component by enabling automatic body conversion to AiAgentBody for common input types such as files, streams,text.image etc. 
   final advantage is to avoid the usage of convertBodyTo(). 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4003379267

   > Do we expect users will want to convert from `BufferedImage`? Just wondering if we can avoid introducing AWT and just check the mime / content-type and use `byte[]` or `InputStream` if image processing is required?
   
   No we dont use that anywhere else. And indeed its not a type anyone really use.
   
   Also this PR has an unit that doesn't really test the newly introduced converter


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2899521715


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text
+     * @param  exchange
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        String userMessage = exchange.getIn().getHeader(USER_MESSAGE, String.class);
+        String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE, String.class);
+        Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+        TextContent content = TextContent.from(text);
+
+        AiAgentBody<TextContent> body = new AiAgentBody<>();
+        body.setUserMessage(userMessage != null ? userMessage : text);
+        body.setSystemMessage(systemMessage);
+        body.setMemoryId(memoryId);
+        body.setContent(content);
+        return body;
+    }
+
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  bufferedImage
+     * @param  exchange
+     * @return               an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> bufferImageToAiAgentBody(BufferedImage bufferedImage, Exchange exchange) {
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            ImageIO.write(bufferedImage, "png", baos);

Review Comment:
   it does not always expect png but output format is forced to be png.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "jamesnetherton (via GitHub)" <gi...@apache.org>.
jamesnetherton commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4003028619

   Do we expect users will want to convert from `BufferedImage`? Just wondering if we can avoid introducing AWT and just check the mime / content-type and use `byte[]` or `InputStream` if image processing is required?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4032206132

   Hello @gnodet, thank you for the detailed review, 
   2 - I have removed the star imports in my previous commits also after running format command somehow star imports are back in my IDE, I observed lately, but I preserved this part. :)
   3 - Agent.processBody() is deprecated.
   4- You are correct that AgentWithMemory and AgentWithoutMemory differentiate text-only vs multimodal messages based on content == null.
    With the current implementation creating a TextContent, String payloads would always follow the multimodal path.
   To preserve the previous behavior, 
   I have updated the textToAiAgentBody converter so that it only sets the userMessage and leaves content as null, ensuring text payloads continue to use the existing text-only flow.
   5 -  Adjusted to use exchange.getMessage().
   6 - Test renamed.
   7 - test cases adjusted
   8 - removed the blank line.
   9 - also retained inline comments,.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2899529901


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text
+     * @param  exchange
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        String userMessage = exchange.getIn().getHeader(USER_MESSAGE, String.class);
+        String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE, String.class);
+        Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+        TextContent content = TextContent.from(text);
+
+        AiAgentBody<TextContent> body = new AiAgentBody<>();
+        body.setUserMessage(userMessage != null ? userMessage : text);
+        body.setSystemMessage(systemMessage);
+        body.setMemoryId(memoryId);
+        body.setContent(content);
+        return body;
+    }
+
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.

Review Comment:
   @orpiske: thanks for the comments. support for the BufferedImage type would be removed. as suggested by @davsclaus and @jamesnetherton. I am currently adopting the other review comments and push the changes shortly. do you see we would still use explicit conversion for Buffered Image ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2905595091


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text
+     * @param  exchange
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        String userMessage = exchange.getIn().getHeader(USER_MESSAGE, String.class);
+        String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE, String.class);
+        Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+        TextContent content = TextContent.from(text);
+
+        AiAgentBody<TextContent> body = new AiAgentBody<>();
+        body.setUserMessage(userMessage != null ? userMessage : text);
+        body.setSystemMessage(systemMessage);
+        body.setMemoryId(memoryId);
+        body.setContent(content);
+        return body;
+    }
+
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.

Review Comment:
   @orpiske : okay, you mean to say we would still use Buffered Image with with AWT dependency  and document it for the users reference ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "Croway (via GitHub)" <gi...@apache.org>.
Croway commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-3998779792

   please rebase this branch


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2905627172


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +165,44 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text     String as message
+     * @param  exchange the Camel exchange containing headers
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        TextContent content = TextContent.from(text);
+        return buildAiAgentBody(exchange, content, text);
+    }
+
+    /**
+     * Support Camel stream caching bodies (ByteArrayInputStreamCache) and delegate them to the existing InputStream
+     * converter.
+     *
+     * @param  cache    the cached input stream containing the message body
+     * @param  exchange the Camel exchange containing headers such as * {@code CamelLangChain4jAgentMediaType},
+     *                  user/system messages, * and optional memory identifiers
+     * @return          an {@link AiAgentBody} containing the appropriate * LangChain4j {@link Content} representation
+     */
+    @Converter
+    public static AiAgentBody<?> inputStreamCacheToAiAgentBody(

Review Comment:
   okay will be removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4029736123

   @Croway , @davsclaus , @jamesnetherton and @orpiske 
   Thank you for the support.  


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-3999053790

   :star2: Thank you for your contribution to the Apache Camel project! :star2:
     :robot: CI automation will test this PR automatically.
   
     :camel: Apache Camel Committers, please review the following items:
   
     * First-time contributors **require MANUAL approval** for the GitHub Actions to run
     * You can use the command `/component-test (camel-)component-name1 (camel-)component-name2..` to request a test from the test bot although they are normally detected and executed by CI.
     * You can label PRs using `build-all`, `build-dependents`, `skip-tests` and `test-dependents` to fine-tune the checks executed by this PR.
     * Build and test logs are available in the summary page. **Only** [Apache Camel committers](https://camel.apache.org/community/team/#committers) have access to the summary.
     
     :warning: Be careful when sharing logs. Review their contents before sharing them publicly.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4003381221

   @chandru9 can you eplain your reason for this PR


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #21704:
URL: https://github.com/apache/camel/pull/21704


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "orpiske (via GitHub)" <gi...@apache.org>.
orpiske commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2899492376


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text
+     * @param  exchange
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        String userMessage = exchange.getIn().getHeader(USER_MESSAGE, String.class);
+        String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE, String.class);
+        Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+        TextContent content = TextContent.from(text);
+
+        AiAgentBody<TextContent> body = new AiAgentBody<>();
+        body.setUserMessage(userMessage != null ? userMessage : text);
+        body.setSystemMessage(systemMessage);
+        body.setMemoryId(memoryId);
+        body.setContent(content);
+        return body;
+    }
+
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  bufferedImage
+     * @param  exchange
+     * @return               an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> bufferImageToAiAgentBody(BufferedImage bufferedImage, Exchange exchange) {
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            ImageIO.write(bufferedImage, "png", baos);

Review Comment:
   Are we positively sure the image format is always PNG? 



##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text
+     * @param  exchange
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        String userMessage = exchange.getIn().getHeader(USER_MESSAGE, String.class);
+        String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE, String.class);
+        Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+        TextContent content = TextContent.from(text);
+
+        AiAgentBody<TextContent> body = new AiAgentBody<>();
+        body.setUserMessage(userMessage != null ? userMessage : text);
+        body.setSystemMessage(systemMessage);
+        body.setMemoryId(memoryId);
+        body.setContent(content);
+        return body;
+    }
+
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  bufferedImage
+     * @param  exchange
+     * @return               an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> bufferImageToAiAgentBody(BufferedImage bufferedImage, Exchange exchange) {
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            ImageIO.write(bufferedImage, "png", baos);
+            byte[] bytes = baos.toByteArray();
+            String base64 = Base64.getEncoder().encodeToString(bytes);
+            Image img = Image.builder().base64Data(base64).mimeType("image/png").build();

Review Comment:
   As with the format, we may need to find a way to make this configurable or auto-detectable. 



##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text
+     * @param  exchange
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        String userMessage = exchange.getIn().getHeader(USER_MESSAGE, String.class);
+        String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE, String.class);
+        Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+        TextContent content = TextContent.from(text);
+
+        AiAgentBody<TextContent> body = new AiAgentBody<>();
+        body.setUserMessage(userMessage != null ? userMessage : text);
+        body.setSystemMessage(systemMessage);
+        body.setMemoryId(memoryId);
+        body.setContent(content);
+        return body;
+    }
+
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.

Review Comment:
   Depending on the details below, regarding the image format, we may need to document the limitations here and in the component docs as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "chandru9 (via GitHub)" <gi...@apache.org>.
chandru9 commented on PR #21704:
URL: https://github.com/apache/camel/pull/21704#issuecomment-4006008211

   @jamesnetherton : Good point. The BufferedImage converter was added to support direct image processing inputs, but since Camel routes typically handle images as byte[], InputStream, or WrappedFile, and we already detect the MIME type to create the appropriate ImageContent, introducing BufferedImage may not be necessary.
   
   To avoid introducing the AWT dependency, I can remove the BufferedImage converter and rely on byte[] / InputStream based conversion using the detected content-type. This should cover the intended use cases for image processing in routes.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Camel 22510 avoid leaking internal data types [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2901698813


##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -19,30 +19,24 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URLConnection;
 import java.nio.file.Files;
 import java.util.Base64;
 
 import dev.langchain4j.data.audio.Audio;
 import dev.langchain4j.data.image.Image;
-import dev.langchain4j.data.message.AudioContent;
-import dev.langchain4j.data.message.Content;
-import dev.langchain4j.data.message.ImageContent;
-import dev.langchain4j.data.message.PdfFileContent;
-import dev.langchain4j.data.message.TextContent;
-import dev.langchain4j.data.message.VideoContent;
+import dev.langchain4j.data.message.*;
 import dev.langchain4j.data.pdf.PdfFile;
 import dev.langchain4j.data.video.Video;
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
+import org.apache.camel.Message;
 import org.apache.camel.WrappedFile;
 import org.apache.camel.component.langchain4j.agent.api.AiAgentBody;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.camel.component.langchain4j.agent.api.Headers.MEDIA_TYPE;
-import static org.apache.camel.component.langchain4j.agent.api.Headers.MEMORY_ID;
-import static org.apache.camel.component.langchain4j.agent.api.Headers.SYSTEM_MESSAGE;
-import static org.apache.camel.component.langchain4j.agent.api.Headers.USER_MESSAGE;
+import static org.apache.camel.component.langchain4j.agent.api.Headers.*;

Review Comment:
   Avoid star imports



##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +165,44 @@ public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, E
         }
     }
 
+    /**
+     * Converts a {@link String} to an {@link AiAgentBody} with the appropriate {@link Content} type.
+     * <p>
+     * This converter is useful for the text components that return Text Body.
+     * </p>
+     *
+     * @param  text     String as message
+     * @param  exchange the Camel exchange containing headers
+     * @return          an AiAgentBody with the appropriate Content type
+     */
+    @Converter
+    public static AiAgentBody<?> textToAiAgentBody(String text, Exchange exchange) {
+        TextContent content = TextContent.from(text);
+        return buildAiAgentBody(exchange, content, text);
+    }
+
+    /**
+     * Support Camel stream caching bodies (ByteArrayInputStreamCache) and delegate them to the existing InputStream
+     * converter.
+     *
+     * @param  cache    the cached input stream containing the message body
+     * @param  exchange the Camel exchange containing headers such as * {@code CamelLangChain4jAgentMediaType},
+     *                  user/system messages, * and optional memory identifiers
+     * @return          an {@link AiAgentBody} containing the appropriate * LangChain4j {@link Content} representation
+     */
+    @Converter
+    public static AiAgentBody<?> inputStreamCacheToAiAgentBody(

Review Comment:
   ByteArrayInputStreamCache is already an InputStream so it would be able to use the existing converter as-is, so this converter is not needed



##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -19,30 +19,24 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URLConnection;
 import java.nio.file.Files;
 import java.util.Base64;
 
 import dev.langchain4j.data.audio.Audio;
 import dev.langchain4j.data.image.Image;
-import dev.langchain4j.data.message.AudioContent;
-import dev.langchain4j.data.message.Content;
-import dev.langchain4j.data.message.ImageContent;
-import dev.langchain4j.data.message.PdfFileContent;
-import dev.langchain4j.data.message.TextContent;
-import dev.langchain4j.data.message.VideoContent;
+import dev.langchain4j.data.message.*;

Review Comment:
   Avoid star imports



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org