gnodet commented on code in PR #29026:
URL: https://github.com/apache/camel/pull/29026#discussion_r4449025663
##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -221,42 +268,58 @@ public static AiAgentBody<?> textToAiAgentBody(String
text, Exchange exchange) {
/**
* Creates the appropriate LangChain4j Content object based on the MIME
type.
+ * <p>
+ * Textual types ({@code text/*}, JSON, XML and YAML, also with a {@code
+json} or {@code +xml} suffix) become a
+ * {@link TextContent}.
+ * </p>
*/
static Content createContent(byte[] data, String mimeType) {
String base64Data = Base64.getEncoder().encodeToString(data);
+ String type = mimeType.toLowerCase(Locale.ROOT);
- if (mimeType.startsWith("image/")) {
+ if (type.startsWith("image/")) {
Image image = Image.builder()
.base64Data(base64Data)
.mimeType(mimeType)
.build();
return ImageContent.from(image);
- } else if (mimeType.startsWith("audio/")) {
+ } else if (type.startsWith("audio/")) {
Audio audio = Audio.builder()
.base64Data(base64Data)
.mimeType(mimeType)
.build();
return AudioContent.from(audio);
- } else if (mimeType.startsWith("video/")) {
+ } else if (type.startsWith("video/")) {
Video video = Video.builder()
.base64Data(base64Data)
.mimeType(mimeType)
.build();
return VideoContent.from(video);
- } else if ("application/pdf".equals(mimeType)) {
+ } else if ("application/pdf".equals(type)) {
PdfFile pdfFile = PdfFile.builder()
.base64Data(base64Data)
.build();
return PdfFileContent.from(pdfFile);
- } else if (mimeType.startsWith("text/")) {
+ } else if (isTextual(type)) {
return TextContent.from(new String(data, StandardCharsets.UTF_8));
} else {
throw new IllegalArgumentException(
"Unsupported MIME type: " + mimeType
- + ". Supported types: image/*,
audio/*, video/*, application/pdf, text/*");
+ + ". Supported types: image/*,
audio/*, video/*, application/pdf, text/*,"
+ + " application/json,
application/xml, application/yaml, */*+json, */*+xml");
}
}
+ private static boolean isTextual(String type) {
+ return type.startsWith("text/")
+ || "application/json".equals(type)
+ || "application/xml".equals(type)
+ || "application/yaml".equals(type)
+ || "application/x-yaml".equals(type)
+ || type.endsWith("+json")
+ || type.endsWith("+xml");
+ }
+
Review Comment:
The new `isTextual()` method covers `application/yaml`/`application/x-yaml`,
but `detectMimeTypeFromExtension()` (not changed in this PR) has no
`.yaml`/`.yml` → `application/yaml` mapping. A `File` or `Path` body named
`config.yaml` will throw `IllegalArgumentException` before reaching
`isTextual()`. Missing entries:
```java
case "yaml":
case "yml":
return "application/yaml";
```
> _Hermès (gnodet) — automated review_
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]