davsclaus opened a new pull request, #29026: URL: https://github.com/apache/camel/pull/29026
Fixes [CAMEL-26202](https://issues.apache.org/jira/browse/CAMEL-26202). All four bugs of the issue were real and reproduced with a fake `ChatModel`. All four are fixed. ### 1. A file, bytes or stream body without a user message sent the content twice **Was:** the converters set `""` as the user message for a `WrappedFile`, `byte[]` or `InputStream` body. The agent then called `chat(@UserMessage String, @UserMessage Content)` with an empty text, and LangChain4j put the content twice in the user message (for example `[TextContent "Hello file", TextContent "Hello file"]`, or the same image twice). That doubles the tokens. **Now:** `AiAgentWithoutMemoryService` and `AiAgentWithMemoryService` have content-only methods (`chat(@UserMessage Content)`, with or without a system message, with a memory id for the memory service). `AgentWithoutMemory` and `AgentWithMemory` call them when there is a content and the user message is empty or blank. The converters are unchanged (still `""`), so custom `Agent` implementations see the same `AiAgentBody` as before. ### 2. An AiAgentBody with a blank user message and a content failed **Was:** user message `" "` plus a content failed in LangChain4j with "template cannot be null or blank". **Now:** a blank user message is handled like an empty one (same fix as 1). Only the content is sent. ### 3. A .json file or JSON bytes failed with Unsupported MIME type **Was:** `.json` maps to `application/json`, but `createContent` accepted only `image/*`, `audio/*`, `video/*`, `application/pdf` and `text/*`. **Now:** `application/json`, `application/xml`, `application/yaml`, `application/x-yaml` and types with a `+json` or `+xml` suffix are sent as a `TextContent` (UTF-8). `image/svg+xml` stays an image, as before. On the same lines: the MIME type now matches case-insensitively (`Application/XML` failed before), and a content type without parameters is trimmed like one with parameters. ### 4. No converter for java.io.File or java.nio.file.Path **Now:** `fileToAiAgentBody(File, Exchange)` and `pathToAiAgentBody(Path, Exchange)` work like the `WrappedFile` converter: the MIME type comes from `CamelLangChain4jAgentMediaType` or `CamelFileContentType`, else from the file extension. The `Path` converter reads with `Files.readAllBytes(path)`, so it also works for a non-default file system. ### Tests New `LangChain4jAgentContentBodyTest` (12 tests) with a capturing fake `ChatModel`. It checks the user message the model receives, with and without memory and with a system message. 10 of the 11 first tests failed before the fix (content twice, "template cannot be null or blank", Unsupported MIME type, no type converter); the 11th (file plus user message) is a regression guard. Module unit tests: 98 run, 0 failures. ### Docs and generated files - `@BodyTypes` text of the endpoint no longer says a `java.io.File` fails; it lists `File`/`Path`, says JSON, XML and YAML go as text, and that the content goes alone without the user message header. Regenerated `langchain4j-agent.json` and copied it to the catalog. No yaml-dsl-validator test refers to `langchain4j-agent`. - `langchain4j-agent-multimodal.adoc`: `File`/`Path` row in the input table, the MIME type to content mapping, and that without the header the user message holds only the content. Copied to the catalog docs. - `LangChain4jAgentConverterLoader` regenerated. - No upgrade guide entry: inputs that failed now work, and the content is no longer duplicated. No option or header changed. ### Not changed - The converters still set `""` as the default user message (see 1). - Moderation still checks only the user message text, not a `TextContent` from a file. That is out of scope here. _Claude Code on behalf of Claus Ibsen (davsclaus)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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]
