Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Base64;

Expand Down Expand Up @@ -195,8 +196,8 @@ public static AiAgentBody<?> byteArrayToAiAgentBody(byte[] data, Exchange exchan
*/
@Converter
public static AiAgentBody<?> inputStreamToAiAgentBody(InputStream inputStream, Exchange exchange) {
try {
byte[] data = inputStream.readAllBytes();
try (InputStream in = inputStream) {
byte[] data = in.readAllBytes();
return byteArrayToAiAgentBody(data, exchange);
} catch (IOException e) {
throw new IllegalArgumentException("Failed to read input stream", e);
Expand Down Expand Up @@ -248,7 +249,7 @@ static Content createContent(byte[] data, String mimeType) {
.build();
return PdfFileContent.from(pdfFile);
} else if (mimeType.startsWith("text/")) {
return TextContent.from(new String(data));
return TextContent.from(new String(data, StandardCharsets.UTF_8));
} else {
throw new IllegalArgumentException(
"Unsupported MIME type: " + mimeType
Expand All @@ -271,13 +272,13 @@ private static String detectMimeType(String fileName, Exchange exchange) {
// Check agent-specific header first (highest priority)
String mediaType = exchange.getMessage().getHeader(MEDIA_TYPE, String.class);
if (mediaType != null) {
return mediaType;
return normalizeContentType(mediaType);
}

// Check file component's content type header
String fileContentType = exchange.getMessage().getHeader(Exchange.FILE_CONTENT_TYPE, String.class);
if (fileContentType != null) {
return fileContentType;
return normalizeContentType(fileContentType);
}

if (fileName == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;

import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
Expand All @@ -32,9 +35,11 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class LangChain4jAgentConverterTest extends CamelTestSupport {

Expand All @@ -58,6 +63,37 @@ void shouldConvertLocalFile() throws Exception {
assertInstanceOf(TextContent.class, body.getContent());
}

@Test
void shouldDecodeTextBytesAsUtf8() {
Exchange exchange = context.getEndpoint("direct:test").createExchange();
exchange.getMessage().setHeader(Headers.MEDIA_TYPE, "text/plain");

byte[] utf8 = "café ☕".getBytes(StandardCharsets.UTF_8);
AiAgentBody<?> body = context.getTypeConverter().convertTo(AiAgentBody.class, exchange, utf8);

assertInstanceOf(TextContent.class, body.getContent());
assertEquals("café ☕", ((TextContent) body.getContent()).text());
}

@Test
void shouldCloseInputStreamAfterConversion() {
Exchange exchange = context.getEndpoint("direct:test").createExchange();
exchange.getMessage().setHeader(Headers.MEDIA_TYPE, "text/plain");

AtomicBoolean closed = new AtomicBoolean(false);
ByteArrayInputStream stream = new ByteArrayInputStream("Hello".getBytes(StandardCharsets.UTF_8)) {
@Override
public void close() throws IOException {
closed.set(true);
super.close();
}
};

context.getTypeConverter().convertTo(AiAgentBody.class, exchange, stream);

assertTrue(closed.get(), "the input stream must be closed after conversion");
}

@Test
void shouldConvertRemoteWrappedFileUsingFileNameHeader() {
Exchange exchange = context.getEndpoint("direct:test").createExchange();
Expand Down