From f15b5e715591ab298ce4c6f55108462a0a4f5088 Mon Sep 17 00:00:00 2001 From: Danny Jelsma <73849717+Dannyj1@users.noreply.github.com> Date: Thu, 8 May 2025 16:31:16 +0200 Subject: [PATCH 1/9] refactor: Remove unnecessary dependency cycle between MistralClient and HttpService --- src/main/java/nl/dannyj/mistral/MistralClient.java | 2 +- .../java/nl/dannyj/mistral/services/HttpService.java | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/java/nl/dannyj/mistral/MistralClient.java b/src/main/java/nl/dannyj/mistral/MistralClient.java index 0b7e24c..4c2ab07 100644 --- a/src/main/java/nl/dannyj/mistral/MistralClient.java +++ b/src/main/java/nl/dannyj/mistral/MistralClient.java @@ -251,7 +251,7 @@ public void createChatCompletionStream(@NonNull ChatCompletionRequest request, @ * @return A new instance of MistralService */ private MistralService buildMistralService() { - return new MistralService(this, new HttpService(this)); + return new MistralService(this, new HttpService(this.httpClient)); } /** diff --git a/src/main/java/nl/dannyj/mistral/services/HttpService.java b/src/main/java/nl/dannyj/mistral/services/HttpService.java index bc1735f..77791a1 100644 --- a/src/main/java/nl/dannyj/mistral/services/HttpService.java +++ b/src/main/java/nl/dannyj/mistral/services/HttpService.java @@ -17,7 +17,6 @@ package nl.dannyj.mistral.services; import lombok.NonNull; -import nl.dannyj.mistral.MistralClient; import nl.dannyj.mistral.exceptions.MistralAPIException; import okhttp3.Callback; import okhttp3.MediaType; @@ -37,15 +36,15 @@ public class HttpService { private static final String API_URL = "https://api.mistral.ai/v1"; - private final MistralClient client; + private final OkHttpClient httpClient; /** * Constructor that initializes the HttpService with a provided MistralClient. * - * @param client The MistralClient to be used for making requests to the Mistral AI API + * @param httpClient The OkHttpClient to be used for making requests to the Mistral AI API */ - public HttpService(@NonNull MistralClient client) { - this.client = client; + public HttpService(@NonNull OkHttpClient httpClient) { + this.httpClient = httpClient; } /** @@ -91,7 +90,6 @@ public void streamPost(@NonNull String urlPath, @NonNull String body, Callback c .url(API_URL + urlPath) .post(RequestBody.create(body, MediaType.parse("application/json"))) .build(); - OkHttpClient httpClient = client.getHttpClient(); httpClient.newCall(request).enqueue(callBack); } @@ -104,8 +102,6 @@ public void streamPost(@NonNull String urlPath, @NonNull String body, Callback c * @throws MistralAPIException If the response is not successful, the response body is null or an IOException occurs in the objectmapper */ private String executeRequest(Request request) { - OkHttpClient httpClient = client.getHttpClient(); - try (Response response = httpClient.newCall(request).execute()) { if (!response.isSuccessful()) { throw new MistralAPIException("Received unexpected response code " + response.code() + ": " + (response.body() != null ? response.body().string() : response)); From 39e5f138b27aa9f8f3ea78f9c487e05701a30833 Mon Sep 17 00:00:00 2001 From: Danny Jelsma <73849717+Dannyj1@users.noreply.github.com> Date: Thu, 8 May 2025 16:36:11 +0200 Subject: [PATCH 2/9] refactor: Remove unnecessary dependency cycle between MistralClient and MistralHeaderInterceptor --- .../java/nl/dannyj/mistral/MistralClient.java | 2 +- .../interceptors/MistralHeaderInterceptor.java | 18 ++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/nl/dannyj/mistral/MistralClient.java b/src/main/java/nl/dannyj/mistral/MistralClient.java index 4c2ab07..01acbe1 100644 --- a/src/main/java/nl/dannyj/mistral/MistralClient.java +++ b/src/main/java/nl/dannyj/mistral/MistralClient.java @@ -260,7 +260,7 @@ private MistralService buildMistralService() { * @return A new instance of OkHttpClient */ private OkHttpClient buildHttpClient(int readTimeoutSeconds, int connectTimeoutSeconds, int writeTimeoutSeconds) { - MistralHeaderInterceptor mistralInterceptor = new MistralHeaderInterceptor(this); + MistralHeaderInterceptor mistralInterceptor = new MistralHeaderInterceptor(this.getApiKey()); return new OkHttpClient.Builder() .readTimeout(readTimeoutSeconds, TimeUnit.SECONDS) diff --git a/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java b/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java index 5941bed..6d1a6a8 100644 --- a/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java +++ b/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java @@ -16,8 +16,6 @@ package nl.dannyj.mistral.interceptors; -import lombok.NonNull; -import nl.dannyj.mistral.MistralClient; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; @@ -27,10 +25,14 @@ public class MistralHeaderInterceptor implements Interceptor { - private final MistralClient client; + private final String apiKey; - public MistralHeaderInterceptor(@NonNull MistralClient client) { - this.client = client; + public MistralHeaderInterceptor(@NotNull String apiKey) { + if (apiKey == null || apiKey.isBlank()) { + throw new IllegalArgumentException("No API key provided in MistralClient"); + } + + this.apiKey = apiKey; } @NotNull @@ -39,10 +41,6 @@ public Response intercept(@NotNull Chain chain) throws IOException { Request request = chain.request(); Request.Builder newRequestBuilder = request.newBuilder(); - if (client.getApiKey() == null || client.getApiKey().isBlank()) { - throw new IllegalArgumentException("No API key provided in MistralClient"); - } - if (request.header("Content-Type") == null) { newRequestBuilder.addHeader("Content-Type", "application/json"); } @@ -52,7 +50,7 @@ public Response intercept(@NotNull Chain chain) throws IOException { } if (request.header("Authorization") == null) { - newRequestBuilder.addHeader("Authorization", "Bearer " + client.getApiKey()); + newRequestBuilder.addHeader("Authorization", "Bearer " + this.apiKey); } Request newRequest = newRequestBuilder.build(); From 9ba38a1567246d421df6819b68e467a09fee6115 Mon Sep 17 00:00:00 2001 From: Danny Jelsma <73849717+Dannyj1@users.noreply.github.com> Date: Thu, 8 May 2025 16:38:49 +0200 Subject: [PATCH 3/9] refactor: Remove unnecessary dependency cycle between MistralClient and MistralService --- .../java/nl/dannyj/mistral/MistralClient.java | 2 +- .../mistral/services/MistralService.java | 22 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/nl/dannyj/mistral/MistralClient.java b/src/main/java/nl/dannyj/mistral/MistralClient.java index 01acbe1..1e130b0 100644 --- a/src/main/java/nl/dannyj/mistral/MistralClient.java +++ b/src/main/java/nl/dannyj/mistral/MistralClient.java @@ -251,7 +251,7 @@ public void createChatCompletionStream(@NonNull ChatCompletionRequest request, @ * @return A new instance of MistralService */ private MistralService buildMistralService() { - return new MistralService(this, new HttpService(this.httpClient)); + return new MistralService(new HttpService(this.httpClient), this.objectMapper); } /** diff --git a/src/main/java/nl/dannyj/mistral/services/MistralService.java b/src/main/java/nl/dannyj/mistral/services/MistralService.java index b295189..92646d6 100644 --- a/src/main/java/nl/dannyj/mistral/services/MistralService.java +++ b/src/main/java/nl/dannyj/mistral/services/MistralService.java @@ -17,13 +17,13 @@ package nl.dannyj.mistral.services; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolationException; import jakarta.validation.Validation; import jakarta.validation.Validator; import jakarta.validation.ValidatorFactory; import lombok.NonNull; -import nl.dannyj.mistral.MistralClient; import nl.dannyj.mistral.exceptions.InvalidJsonException; import nl.dannyj.mistral.exceptions.UnexpectedResponseEndException; import nl.dannyj.mistral.exceptions.UnexpectedResponseException; @@ -55,17 +55,17 @@ public class MistralService { private final HttpService httpService; - private final MistralClient client; + private final ObjectMapper objectMapper; private final Validator validator; /** * Constructor that initializes the MistralService with a provided MistralClient and HttpService. * - * @param client The MistralClient to be used for interacting with the Mistral AI API - * @param httpService The HttpService to be used for making HTTP requests + * @param httpService The HttpService to be used for making HTTP requests to the Mistral AI API + * @param objectMapper The ObjectMapper to be used for converting objects to and from JSON */ - public MistralService(@NonNull MistralClient client, @NonNull HttpService httpService) { - this.client = client; + public MistralService(@NonNull HttpService httpService, @NonNull ObjectMapper objectMapper) { + this.objectMapper = objectMapper; this.httpService = httpService; try (ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory()) { @@ -118,7 +118,7 @@ public void createChatCompletionStream(@NonNull ChatCompletionRequest request, @ validateRequest(request); try { - String requestJson = client.getObjectMapper().writeValueAsString(request); + String requestJson = this.objectMapper.writeValueAsString(request); httpService.streamPost("/chat/completions", requestJson, new Callback() { @Override @@ -161,7 +161,7 @@ public ListModelsResponse listModels() { String response = httpService.get("/models"); try { - return client.getObjectMapper().readValue(response, ListModelsResponse.class); + return this.objectMapper.readValue(response, ListModelsResponse.class); } catch (JsonProcessingException e) { throw new UnexpectedResponseException("Received unexpected response from the Mistral.ai API (mistral-java-client might need to be updated): " + response, e); } @@ -238,7 +238,7 @@ private U postRequest(String endpoint, T String requestJson = null; try { - requestJson = client.getObjectMapper().writeValueAsString(request); + requestJson = this.objectMapper.writeValueAsString(request); } catch (JsonProcessingException e) { throw new InvalidJsonException("Failed to convert request to JSON", e); } @@ -246,7 +246,7 @@ private U postRequest(String endpoint, T try { response = httpService.post(endpoint, requestJson); - return client.getObjectMapper().readValue(response, responseType); + return this.objectMapper.readValue(response, responseType); } catch (JsonProcessingException e) { throw new UnexpectedResponseException("Received unexpected response from the Mistral.ai API (mistral-java-client might need to be updated): " + response, e); } @@ -266,7 +266,7 @@ private void handleResponseBody(@NotNull ResponseBody responseBody, ChatCompleti } try { - MessageChunk messageChunk = client.getObjectMapper().readValue(chunk, MessageChunk.class); + MessageChunk messageChunk = this.objectMapper.readValue(chunk, MessageChunk.class); callback.onChunkReceived(messageChunk); } catch (JsonProcessingException e) { From 24701b6d4f60cd700c36024b54e6bd28393d42af Mon Sep 17 00:00:00 2001 From: Danny Jelsma <73849717+Dannyj1@users.noreply.github.com> Date: Thu, 8 May 2025 16:42:12 +0200 Subject: [PATCH 4/9] refactor: Make defaultDeserializer in ContentChunkListDeserializer transient --- .../mistral/serialization/ContentChunkListDeserializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/nl/dannyj/mistral/serialization/ContentChunkListDeserializer.java b/src/main/java/nl/dannyj/mistral/serialization/ContentChunkListDeserializer.java index 75d5b78..1375832 100644 --- a/src/main/java/nl/dannyj/mistral/serialization/ContentChunkListDeserializer.java +++ b/src/main/java/nl/dannyj/mistral/serialization/ContentChunkListDeserializer.java @@ -34,7 +34,7 @@ public class ContentChunkListDeserializer extends StdDeserializer> implements ContextualDeserializer { - private JsonDeserializer defaultDeserializer; + private transient JsonDeserializer defaultDeserializer; public ContentChunkListDeserializer() { this(null); From 99335a37c367513ecbfd360fc24952590a8d3f87 Mon Sep 17 00:00:00 2001 From: Danny Jelsma <73849717+Dannyj1@users.noreply.github.com> Date: Thu, 8 May 2025 16:44:36 +0200 Subject: [PATCH 5/9] refactor: Reduce complexity of ToolChoiceOptionDeserializer --- .../serialization/ToolChoiceOptionDeserializer.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/main/java/nl/dannyj/mistral/serialization/ToolChoiceOptionDeserializer.java b/src/main/java/nl/dannyj/mistral/serialization/ToolChoiceOptionDeserializer.java index bfedb83..b0ec2b2 100644 --- a/src/main/java/nl/dannyj/mistral/serialization/ToolChoiceOptionDeserializer.java +++ b/src/main/java/nl/dannyj/mistral/serialization/ToolChoiceOptionDeserializer.java @@ -41,16 +41,8 @@ public ToolChoiceOption deserialize(JsonParser jp, DeserializationContext ctxt) if (token == JsonToken.VALUE_STRING) { String enumValue = jp.getText().toUpperCase(); - try { - return ToolChoiceEnum.valueOf(enumValue); - } catch (IllegalArgumentException e) { - if ("ANY".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.ANY; - if ("AUTO".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.AUTO; - if ("NONE".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.NONE; - if ("REQUIRED".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.REQUIRED; - - throw ctxt.weirdStringException(enumValue, ToolChoiceEnum.class, "Not a valid ToolChoiceEnum value"); - } + + return ToolChoiceEnum.valueOf(enumValue); } else if (token == JsonToken.START_OBJECT) { return mapper.readValue(jp, SpecificToolChoice.class); } From e6fb6bcd3b23693b32b1d5fc2e78ca98666fdf81 Mon Sep 17 00:00:00 2001 From: Danny Jelsma <73849717+Dannyj1@users.noreply.github.com> Date: Thu, 8 May 2025 17:00:44 +0200 Subject: [PATCH 6/9] docs: Fix invalid use of javadoc's @param on field --- .../nl/dannyj/mistral/models/completion/message/ChatMessage.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/nl/dannyj/mistral/models/completion/message/ChatMessage.java b/src/main/java/nl/dannyj/mistral/models/completion/message/ChatMessage.java index 3007374..a988cb3 100644 --- a/src/main/java/nl/dannyj/mistral/models/completion/message/ChatMessage.java +++ b/src/main/java/nl/dannyj/mistral/models/completion/message/ChatMessage.java @@ -52,7 +52,6 @@ public abstract class ChatMessage { /** * The content of the message. Can be null or a list of content chunks. * - * @param content The list of content chunks, or null. * @return The list of content chunks, or null. */ @Nullable From 95dbaaa73f4ec761213ff8ca37eae3da95c65e9a Mon Sep 17 00:00:00 2001 From: Danny Jelsma <73849717+Dannyj1@users.noreply.github.com> Date: Thu, 8 May 2025 17:11:07 +0200 Subject: [PATCH 7/9] refactor: Replace all NotNull annotations with Lombok's NonNull --- .../mistral/builders/MessageListBuilder.java | 14 +++++++------- .../interceptors/MistralHeaderInterceptor.java | 5 +++-- .../completion/message/AssistantMessage.java | 6 +++--- .../models/completion/message/ToolMessage.java | 6 +++--- .../models/completion/message/UserMessage.java | 6 +++--- .../nl/dannyj/mistral/services/MistralService.java | 7 +++---- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/nl/dannyj/mistral/builders/MessageListBuilder.java b/src/main/java/nl/dannyj/mistral/builders/MessageListBuilder.java index 966ab00..625b541 100644 --- a/src/main/java/nl/dannyj/mistral/builders/MessageListBuilder.java +++ b/src/main/java/nl/dannyj/mistral/builders/MessageListBuilder.java @@ -18,7 +18,7 @@ import jakarta.annotation.Nullable; import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; +import lombok.NonNull; import nl.dannyj.mistral.models.completion.message.AssistantMessage; import nl.dannyj.mistral.models.completion.message.ChatMessage; import nl.dannyj.mistral.models.completion.message.SystemMessage; @@ -59,7 +59,7 @@ public MessageListBuilder(List messages) { * @param content The text content of the system message. Cannot be null. * @return This builder instance. */ - public MessageListBuilder system(@NotNull String content) { + public MessageListBuilder system(@NonNull String content) { this.messages.add(new SystemMessage(content)); return this; } @@ -70,7 +70,7 @@ public MessageListBuilder system(@NotNull String content) { * @param content The text content of the assistant message. Cannot be null. * @return This builder instance. */ - public MessageListBuilder assistant(@NotNull String content) { + public MessageListBuilder assistant(@NonNull String content) { this.messages.add(new AssistantMessage(content)); return this; } @@ -81,7 +81,7 @@ public MessageListBuilder assistant(@NotNull String content) { * @param toolCalls The list of tool calls. Cannot be null or empty. * @return This builder instance. */ - public MessageListBuilder assistant(@NotNull @NotEmpty List toolCalls) { + public MessageListBuilder assistant(@NonNull @NotEmpty List toolCalls) { this.messages.add(new AssistantMessage(toolCalls)); return this; } @@ -92,7 +92,7 @@ public MessageListBuilder assistant(@NotNull @NotEmpty List toolCalls) * @param content The text content of the user message. Cannot be null. * @return This builder instance. */ - public MessageListBuilder user(@NotNull String content) { + public MessageListBuilder user(@NonNull String content) { this.messages.add(new UserMessage(content)); return this; } @@ -104,7 +104,7 @@ public MessageListBuilder user(@NotNull String content) { * @param toolCallId The ID of the tool call this message responds to. Can be null. * @return This builder instance. */ - public MessageListBuilder tool(@NotNull String content, @Nullable String toolCallId) { + public MessageListBuilder tool(@NonNull String content, @Nullable String toolCallId) { this.messages.add(new ToolMessage(content, toolCallId)); return this; } @@ -117,7 +117,7 @@ public MessageListBuilder tool(@NotNull String content, @Nullable String toolCal * @param message The ChatMessage object to be added. Cannot be null. * @return This builder instance. */ - public MessageListBuilder message(@NotNull ChatMessage message) { + public MessageListBuilder message(@NonNull ChatMessage message) { this.messages.add(message); return this; } diff --git a/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java b/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java index 6d1a6a8..fe6915f 100644 --- a/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java +++ b/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java @@ -16,6 +16,7 @@ package nl.dannyj.mistral.interceptors; +import lombok.NonNull; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; @@ -27,7 +28,7 @@ public class MistralHeaderInterceptor implements Interceptor { private final String apiKey; - public MistralHeaderInterceptor(@NotNull String apiKey) { + public MistralHeaderInterceptor(@NonNull String apiKey) { if (apiKey == null || apiKey.isBlank()) { throw new IllegalArgumentException("No API key provided in MistralClient"); } @@ -37,7 +38,7 @@ public MistralHeaderInterceptor(@NotNull String apiKey) { @NotNull @Override - public Response intercept(@NotNull Chain chain) throws IOException { + public Response intercept(@NonNull Chain chain) throws IOException { Request request = chain.request(); Request.Builder newRequestBuilder = request.newBuilder(); diff --git a/src/main/java/nl/dannyj/mistral/models/completion/message/AssistantMessage.java b/src/main/java/nl/dannyj/mistral/models/completion/message/AssistantMessage.java index 462fa66..64263d7 100644 --- a/src/main/java/nl/dannyj/mistral/models/completion/message/AssistantMessage.java +++ b/src/main/java/nl/dannyj/mistral/models/completion/message/AssistantMessage.java @@ -20,10 +20,10 @@ import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.annotation.Nullable; import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.NonNull; import lombok.Setter; import nl.dannyj.mistral.models.completion.content.ContentChunk; import nl.dannyj.mistral.models.completion.content.TextChunk; @@ -69,7 +69,7 @@ public class AssistantMessage extends ChatMessage { * * @param textContent The text content. */ - public AssistantMessage(@NotNull String textContent) { + public AssistantMessage(@NonNull String textContent) { this.content = Collections.singletonList(new TextChunk(textContent)); this.toolCalls = null; } @@ -79,7 +79,7 @@ public AssistantMessage(@NotNull String textContent) { * * @param toolCalls The list of tool calls. Cannot be null or empty. */ - public AssistantMessage(@NotNull @NotEmpty List toolCalls) { + public AssistantMessage(@NonNull @NotEmpty List toolCalls) { this.content = null; this.toolCalls = toolCalls; } diff --git a/src/main/java/nl/dannyj/mistral/models/completion/message/ToolMessage.java b/src/main/java/nl/dannyj/mistral/models/completion/message/ToolMessage.java index 74c5622..bb89f76 100644 --- a/src/main/java/nl/dannyj/mistral/models/completion/message/ToolMessage.java +++ b/src/main/java/nl/dannyj/mistral/models/completion/message/ToolMessage.java @@ -19,10 +19,10 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.annotation.Nullable; -import jakarta.validation.constraints.NotNull; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.NonNull; import lombok.Setter; import nl.dannyj.mistral.models.completion.content.ContentChunk; import nl.dannyj.mistral.models.completion.content.TextChunk; @@ -68,7 +68,7 @@ public class ToolMessage extends ChatMessage { * @param textContent The text content (result) of the tool call. Cannot be null. * @param toolCallId The ID of the tool call this message responds to. Can be null. */ - public ToolMessage(@NotNull String textContent, @Nullable String toolCallId) { + public ToolMessage(@NonNull String textContent, @Nullable String toolCallId) { this.content = Collections.singletonList(new TextChunk(textContent)); this.toolCallId = toolCallId; } @@ -79,7 +79,7 @@ public ToolMessage(@NotNull String textContent, @Nullable String toolCallId) { * @param contentChunks The list of content chunks representing the tool result. Cannot be null or empty. * @param toolCallId The ID of the tool call this message responds to. Can be null. */ - public ToolMessage(@NotNull @jakarta.validation.constraints.NotEmpty List contentChunks, @Nullable String toolCallId) { + public ToolMessage(@NonNull @jakarta.validation.constraints.NotEmpty List contentChunks, @Nullable String toolCallId) { this.content = contentChunks; this.toolCallId = toolCallId; } diff --git a/src/main/java/nl/dannyj/mistral/models/completion/message/UserMessage.java b/src/main/java/nl/dannyj/mistral/models/completion/message/UserMessage.java index 5bd7c4e..7704a5c 100644 --- a/src/main/java/nl/dannyj/mistral/models/completion/message/UserMessage.java +++ b/src/main/java/nl/dannyj/mistral/models/completion/message/UserMessage.java @@ -17,9 +17,9 @@ package nl.dannyj.mistral.models.completion.message; import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import lombok.NonNull; import nl.dannyj.mistral.models.completion.content.ContentChunk; import nl.dannyj.mistral.models.completion.content.TextChunk; @@ -39,7 +39,7 @@ public class UserMessage extends ChatMessage { * * @param textContent The text content for the user message. Cannot be null or empty. */ - public UserMessage(@NotNull String textContent) { + public UserMessage(@NonNull String textContent) { if (textContent.isEmpty()) { throw new IllegalArgumentException("User message text content cannot be empty."); } @@ -51,7 +51,7 @@ public UserMessage(@NotNull String textContent) { * * @param contentChunks The list of content chunks. Cannot be null or empty. */ - public UserMessage(@NotNull @NotEmpty List contentChunks) { + public UserMessage(@NonNull @NotEmpty List contentChunks) { this.content = contentChunks; } diff --git a/src/main/java/nl/dannyj/mistral/services/MistralService.java b/src/main/java/nl/dannyj/mistral/services/MistralService.java index 92646d6..620f493 100644 --- a/src/main/java/nl/dannyj/mistral/services/MistralService.java +++ b/src/main/java/nl/dannyj/mistral/services/MistralService.java @@ -41,7 +41,6 @@ import okhttp3.Call; import okhttp3.Callback; import okhttp3.ResponseBody; -import org.jetbrains.annotations.NotNull; import java.io.BufferedReader; import java.io.IOException; @@ -122,7 +121,7 @@ public void createChatCompletionStream(@NonNull ChatCompletionRequest request, @ httpService.streamPost("/chat/completions", requestJson, new Callback() { @Override - public void onResponse(@NotNull Call call, @NotNull okhttp3.Response response) { + public void onResponse(@NonNull Call call, @NonNull okhttp3.Response response) { if (!response.isSuccessful()) { callback.onError(new UnexpectedResponseException("Received unexpected response code " + response.code() + ": " + response)); return; @@ -141,7 +140,7 @@ public void onResponse(@NotNull Call call, @NotNull okhttp3.Response response) { } @Override - public void onFailure(@NotNull Call call, @NotNull IOException e) { + public void onFailure(@NonNull Call call, @NonNull IOException e) { callback.onError(e); } }); @@ -252,7 +251,7 @@ private U postRequest(String endpoint, T } } - private void handleResponseBody(@NotNull ResponseBody responseBody, ChatCompletionChunkCallback callback) throws IOException { + private void handleResponseBody(@NonNull ResponseBody responseBody, ChatCompletionChunkCallback callback) throws IOException { BufferedReader reader = new BufferedReader(responseBody.charStream()); String line; From 18c9c4f2aed70791afeb44066327bffdafb0b54a Mon Sep 17 00:00:00 2001 From: Danny Jelsma <73849717+Dannyj1@users.noreply.github.com> Date: Thu, 8 May 2025 17:11:57 +0200 Subject: [PATCH 8/9] refactor: Fix new SonarQube issue regarding unnecessary null check --- .../dannyj/mistral/interceptors/MistralHeaderInterceptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java b/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java index fe6915f..16840b0 100644 --- a/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java +++ b/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java @@ -29,7 +29,7 @@ public class MistralHeaderInterceptor implements Interceptor { private final String apiKey; public MistralHeaderInterceptor(@NonNull String apiKey) { - if (apiKey == null || apiKey.isBlank()) { + if (apiKey.isBlank()) { throw new IllegalArgumentException("No API key provided in MistralClient"); } From a7ffe61c996ca3761910a94da19b75c6f1f581f4 Mon Sep 17 00:00:00 2001 From: Danny Jelsma <73849717+Dannyj1@users.noreply.github.com> Date: Thu, 8 May 2025 17:13:25 +0200 Subject: [PATCH 9/9] docs: Update documentation that became obsolete due to code cleanup --- .../dannyj/mistral/interceptors/MistralHeaderInterceptor.java | 2 +- src/main/java/nl/dannyj/mistral/services/HttpService.java | 2 +- src/main/java/nl/dannyj/mistral/services/MistralService.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java b/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java index 16840b0..e433285 100644 --- a/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java +++ b/src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java @@ -30,7 +30,7 @@ public class MistralHeaderInterceptor implements Interceptor { public MistralHeaderInterceptor(@NonNull String apiKey) { if (apiKey.isBlank()) { - throw new IllegalArgumentException("No API key provided in MistralClient"); + throw new IllegalArgumentException("No API key provided"); } this.apiKey = apiKey; diff --git a/src/main/java/nl/dannyj/mistral/services/HttpService.java b/src/main/java/nl/dannyj/mistral/services/HttpService.java index 77791a1..4cf057a 100644 --- a/src/main/java/nl/dannyj/mistral/services/HttpService.java +++ b/src/main/java/nl/dannyj/mistral/services/HttpService.java @@ -39,7 +39,7 @@ public class HttpService { private final OkHttpClient httpClient; /** - * Constructor that initializes the HttpService with a provided MistralClient. + * Constructor that initializes the HttpService with a provided OkHttpClient. * * @param httpClient The OkHttpClient to be used for making requests to the Mistral AI API */ diff --git a/src/main/java/nl/dannyj/mistral/services/MistralService.java b/src/main/java/nl/dannyj/mistral/services/MistralService.java index 620f493..a28da8e 100644 --- a/src/main/java/nl/dannyj/mistral/services/MistralService.java +++ b/src/main/java/nl/dannyj/mistral/services/MistralService.java @@ -58,7 +58,7 @@ public class MistralService { private final Validator validator; /** - * Constructor that initializes the MistralService with a provided MistralClient and HttpService. + * Constructor that initializes the MistralService with a provided HttpService and ObjectMapper. * * @param httpService The HttpService to be used for making HTTP requests to the Mistral AI API * @param objectMapper The ObjectMapper to be used for converting objects to and from JSON