Finish functionality

This commit is contained in:
Oracle 2026-04-26 18:21:05 +02:00
parent b6af1c9792
commit 89d5282b0f
Signed by: Oracle
SSH key fingerprint: SHA256:x4/RtnjUyuHkdvmwNDsWSfcfF1V5PNr3OpriZqOvCX8
9 changed files with 583 additions and 133 deletions

View file

@ -3,8 +3,12 @@ package ai.nomyo;
import ai.nomyo.errors.*;
import lombok.Getter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
/**
* High-level OpenAI-compatible entrypoint with automatic hybrid encryption (AES-256-GCM + RSA-4096).
@ -23,6 +27,14 @@ public class SecureChatCompletion {
this(Constants.DEFAULT_BASE_URL, false, null, true, null, Constants.DEFAULT_MAX_RETRIES);
}
public SecureChatCompletion(String baseUrl, String apiKey) {
this(baseUrl, false, apiKey, true, null, Constants.DEFAULT_MAX_RETRIES);
}
public SecureChatCompletion(String baseUrl) {
this(baseUrl, false, null, true, null, Constants.DEFAULT_MAX_RETRIES);
}
/**
* @param baseUrl NOMYO Router base URL (HTTPS enforced unless {@code allowHttp})
* @param allowHttp permit {@code http://} URLs (development only)
@ -57,15 +69,85 @@ public class SecureChatCompletion {
* @throws ServiceUnavailableError HTTP 503
* @throws APIError other errors
*/
@SuppressWarnings("unchecked")
public Map<String, Object> create(String model, List<Map<String, Object>> messages, Map<String, Object> kwargs) {
// Build payload from model, messages, and kwargs
// Validate stream is false
// Validate securityTier if provided
// Use per-call api_key override if provided, else instance apiKey
// Create temp client if baseUrl override provided
// Validate required parameters
if (model == null || model.isEmpty()) {
throw new IllegalArgumentException("model is required");
}
if (messages == null || messages.isEmpty()) {
throw new IllegalArgumentException("messages is required and cannot be empty");
}
// Build payload
Map<String, Object> payload = new HashMap<>();
payload.put("model", model);
payload.put("messages", messages);
// Add kwargs
if (kwargs != null) {
// Check for stream parameter
if (kwargs.containsKey("stream")) {
Object streamValue = kwargs.get("stream");
boolean stream = streamValue instanceof Boolean ? (Boolean) streamValue : Boolean.parseBoolean(streamValue.toString());
if (stream) {
throw new IllegalArgumentException("Streaming is not supported");
}
}
// Check for security_tier
if (kwargs.containsKey("security_tier")) {
Object tier = kwargs.get("security_tier");
if (tier != null && !Constants.VALID_SECURITY_TIERS.contains(tier.toString())) {
throw new IllegalArgumentException(
"Invalid security_tier: '" + tier + "'. Must be one of: " + Constants.VALID_SECURITY_TIERS);
}
}
payload.putAll(kwargs);
}
// Determine API key (per-call override or instance key)
String apiKey = this.apiKey;
if (kwargs != null && kwargs.containsKey("api_key")) {
Object key = kwargs.get("api_key");
if (key != null) {
apiKey = key.toString();
}
}
// Determine security tier
String securityTier = null;
if (kwargs != null && kwargs.containsKey("security_tier")) {
securityTier = kwargs.get("security_tier").toString();
}
// Generate payload ID
String payloadId = UUID.randomUUID().toString();
// Send secure request
// Return decrypted response map
return null;
try {
return client.sendSecureRequest(payload, payloadId, apiKey, securityTier).get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Request interrupted", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
switch (cause) {
case SecurityError securityError -> throw new RuntimeException(cause);
case InvalidRequestError invalidRequestError -> throw new RuntimeException(cause);
case AuthenticationError authenticationError -> throw new RuntimeException(cause);
case ForbiddenError forbiddenError -> throw new RuntimeException(cause);
case RateLimitError rateLimitError -> throw new RuntimeException(cause);
case ServerError serverError -> throw new RuntimeException(cause);
case ServiceUnavailableError serviceUnavailableError -> throw new RuntimeException(cause);
case APIError apiError -> throw new RuntimeException(cause);
case APIConnectionError apiConnectionError -> throw new RuntimeException(cause);
case SecureCompletionClient.ValueError valueError -> throw new IllegalArgumentException(cause);
default ->
throw new RuntimeException("Request failed: " + cause.getMessage(), cause);
}
}
}
/**