davsclaus commented on code in PR #29026:
URL: https://github.com/apache/camel/pull/29026#discussion_r4451497442
##########
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:
Confirmed: a `File` or `Path` named `config.yaml` failed with "Cannot
determine MIME type for file". Fixed in 6b2799c7bc31: `.yaml` and `.yml` now
map to `application/yaml`. The same extension table is now the fallback for a
`byte[]`/`InputStream` with only a `CamelFileName` header, since
`URLConnection.guessContentTypeFromName` returns null for `.yaml` too. New
tests cover `File` and `Path` bodies with `.yaml`, `.YAML` and `.yml`, and
bytes with `CamelFileName=config.yaml` (all 3 failed before). The other text
types in the docs already had a mapping (`.txt`, `.csv`, `.html`, `.md`,
`.xml`, `.json`).
_Claude Code on behalf of Claus Ibsen (davsclaus)_
--
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]