2026-04-21 17:24:11 +02:00
|
|
|
package ai.nomyo;
|
|
|
|
|
|
|
|
|
|
import ai.nomyo.errors.*;
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-23 13:36:46 +02:00
|
|
|
* High-level OpenAI-compatible entrypoint with automatic hybrid encryption (AES-256-GCM + RSA-4096).
|
2026-04-21 17:24:11 +02:00
|
|
|
*/
|
|
|
|
|
@Getter
|
|
|
|
|
public class SecureChatCompletion {
|
|
|
|
|
|
|
|
|
|
private final SecureCompletionClient client;
|
|
|
|
|
private final String apiKey;
|
|
|
|
|
private final String keyDir;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-23 13:36:46 +02:00
|
|
|
* Default settings: {@code https://api.nomyo.ai}, HTTPS-only, secure memory, ephemeral keys, 2 retries.
|
2026-04-21 17:24:11 +02:00
|
|
|
*/
|
|
|
|
|
public SecureChatCompletion() {
|
|
|
|
|
this(Constants.DEFAULT_BASE_URL, false, null, true, null, Constants.DEFAULT_MAX_RETRIES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-23 13:36:46 +02:00
|
|
|
* @param baseUrl NOMYO Router base URL (HTTPS enforced unless {@code allowHttp})
|
|
|
|
|
* @param allowHttp permit {@code http://} URLs (development only)
|
|
|
|
|
* @param apiKey Bearer token (can also be passed per-call via {@link #create})
|
|
|
|
|
* @param secureMemory enable memory locking/zeroing
|
|
|
|
|
* @param keyDir RSA key directory; {@code null} = ephemeral
|
|
|
|
|
* @param maxRetries retries on 429/500/502/503/504 + network errors (exponential backoff)
|
2026-04-21 17:24:11 +02:00
|
|
|
*/
|
2026-04-23 13:36:46 +02:00
|
|
|
public SecureChatCompletion(String baseUrl, boolean allowHttp, String apiKey, boolean secureMemory, String keyDir, int maxRetries) {
|
2026-04-21 17:24:11 +02:00
|
|
|
this.client = new SecureCompletionClient(baseUrl, allowHttp, secureMemory, maxRetries);
|
|
|
|
|
this.apiKey = apiKey;
|
|
|
|
|
this.keyDir = keyDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-23 13:36:46 +02:00
|
|
|
* Main entrypoint — same signature as {@code openai.ChatCompletion.create()}.
|
|
|
|
|
* All kwargs are passed through to the OpenAI-compatible API.
|
|
|
|
|
* <p>Streaming is <b>not supported</b> (server rejects with HTTP 400).
|
|
|
|
|
* Security tiers: "standard", "high", "maximum".
|
2026-04-21 17:24:11 +02:00
|
|
|
*
|
2026-04-23 13:36:46 +02:00
|
|
|
* @param model model identifier (required)
|
|
|
|
|
* @param messages OpenAI-format message list (required)
|
|
|
|
|
* @param kwargs additional OpenAI-compatible params (temperature, maxTokens, etc.)
|
|
|
|
|
* @return decrypted response map
|
|
|
|
|
* @throws SecurityError encryption/decryption failure
|
|
|
|
|
* @throws APIConnectionError network error
|
|
|
|
|
* @throws InvalidRequestError HTTP 400
|
|
|
|
|
* @throws AuthenticationError HTTP 401
|
|
|
|
|
* @throws ForbiddenError HTTP 403
|
|
|
|
|
* @throws RateLimitError HTTP 429
|
|
|
|
|
* @throws ServerError HTTP 500
|
|
|
|
|
* @throws ServiceUnavailableError HTTP 503
|
|
|
|
|
* @throws APIError other errors
|
2026-04-21 17:24:11 +02:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
// Send secure request
|
|
|
|
|
// Return decrypted response map
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience variant with no additional parameters.
|
|
|
|
|
*/
|
|
|
|
|
public Map<String, Object> create(String model, List<Map<String, Object>> messages) {
|
|
|
|
|
return create(model, messages, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-23 13:36:46 +02:00
|
|
|
* Async alias for {@link #create(String, List, Map)}.
|
2026-04-21 17:24:11 +02:00
|
|
|
*/
|
|
|
|
|
public Map<String, Object> acreate(String model, List<Map<String, Object>> messages, Map<String, Object> kwargs) {
|
|
|
|
|
return create(model, messages, kwargs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-23 13:36:46 +02:00
|
|
|
* Async alias for {@link #create(String, List)}.
|
2026-04-21 17:24:11 +02:00
|
|
|
*/
|
|
|
|
|
public Map<String, Object> acreate(String model, List<Map<String, Object>> messages) {
|
|
|
|
|
return create(model, messages);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-23 13:36:46 +02:00
|
|
|
* Delegates to {@link SecureCompletionClient#close()}.
|
2026-04-21 17:24:11 +02:00
|
|
|
*/
|
|
|
|
|
public void close() {
|
|
|
|
|
client.close();
|
|
|
|
|
}
|
|
|
|
|
}
|