integrate official Tripo async models
This commit is contained in:
1 parent
a022162382
commit
962eef26e5
11 files changed
+160
-20
No files matched your search
+14
-3
@@ -85,12 +85,18 @@ public class AsyncProviderClient {
|
||||
Map<String,Object> row = jdbc.queryForMap("SELECT code, base_url, credential_ciphertext FROM model_providers WHERE id=? AND status='active' AND deleted_at IS NULL", id);
|
||||
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
|
||||
String key = ciphertext.isBlank() ? "" : cipher.decrypt(ciphertext);
|
||||
if (key.isBlank()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
String code = String.valueOf(row.get("code"));
|
||||
List<String> keys = new ArrayList<>();
|
||||
keys.add(key);
|
||||
if (!key.isBlank()) keys.add(key);
|
||||
addEnvironmentKeys(keys, code);
|
||||
addEnvironmentKeys(keys, id);
|
||||
// Tripo's official API key may be supplied through the server secret
|
||||
// environment so it never has to be committed or exposed in SQL.
|
||||
if (key.isBlank() && isTripo(code, String.valueOf(row.get("base_url")))) {
|
||||
addEnvironmentKeys(keys, "TRIPO_API_KEY");
|
||||
addEnvironmentKeys(keys, "MOYI_TRIPO_API_KEY");
|
||||
}
|
||||
if (keys.isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
return new Provider(code, String.valueOf(row.get("base_url")), keys.stream().distinct().toList());
|
||||
} catch (EmptyResultDataAccessException exception) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道不存在或已停用", 503); }
|
||||
}
|
||||
@@ -100,10 +106,15 @@ public class AsyncProviderClient {
|
||||
private String encode(String value) { return java.net.URLEncoder.encode(value, java.nio.charset.StandardCharsets.UTF_8); }
|
||||
private String write(Object value) throws JsonProcessingException { return mapper.writeValueAsString(value == null ? Map.of() : value); }
|
||||
private void addEnvironmentKeys(List<String> keys, String alias) {
|
||||
String name = "MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(java.util.Locale.ROOT).replace('-', '_');
|
||||
String name = alias.endsWith("_API_KEY") ? alias : "MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(java.util.Locale.ROOT).replace('-', '_');
|
||||
String configured = environment.getProperty(name);
|
||||
if (configured != null && !configured.isBlank()) for (String value : configured.split("\\|")) if (!value.isBlank()) keys.add(value.trim());
|
||||
}
|
||||
private boolean isTripo(String code, String baseUrl) {
|
||||
String normalizedCode = code == null ? "" : code.toLowerCase(java.util.Locale.ROOT);
|
||||
String normalizedUrl = baseUrl == null ? "" : baseUrl.toLowerCase(java.util.Locale.ROOT);
|
||||
return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d");
|
||||
}
|
||||
private record Provider(String code, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
|
||||
private Provider(String code, String baseUrl, List<String> apiKeys) { this(code, baseUrl, apiKeys, new AtomicInteger()); }
|
||||
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
|
||||
|
||||
@@ -230,12 +230,27 @@ public class AsyncTaskService {
|
||||
|
||||
private Map<String,Object> result(Map<String,Object> response) {
|
||||
Object raw = response.get("output");
|
||||
if (raw == null && response.get("data") instanceof Map<?,?> data) raw = data.get("output");
|
||||
if (raw instanceof Map<?,?> map) { Map<String,Object> value = new LinkedHashMap<>(); map.forEach((k,v)->value.put(String.valueOf(k),v)); return value; }
|
||||
Map<?,?> data = response.get("data") instanceof Map<?,?> value ? value : null;
|
||||
if (raw == null && data != null) raw = data.get("output");
|
||||
if (raw instanceof Map<?,?> map) {
|
||||
Map<String,Object> value = new LinkedHashMap<>();
|
||||
map.forEach((k,v)->value.put(String.valueOf(k),v));
|
||||
copyAlias(value, "model_url", "modelUrl");
|
||||
copyAlias(value, "rendered_image_url", "previewUrl");
|
||||
copyAlias(value, "video_url", "videoUrl");
|
||||
if (value.get("url") == null && value.get("modelUrl") != null) value.put("url", value.get("modelUrl"));
|
||||
if (value.get("url") == null && value.get("videoUrl") != null) value.put("url", value.get("videoUrl"));
|
||||
if (data != null && data.get("credits_consumed") != null) value.put("creditsConsumed", data.get("credits_consumed"));
|
||||
return value;
|
||||
}
|
||||
if (raw instanceof String value && !value.isBlank()) return Map.of("url", value);
|
||||
return null;
|
||||
}
|
||||
|
||||
private void copyAlias(Map<String,Object> value, String source, String target) {
|
||||
if (value.get(target) == null && value.get(source) != null) value.put(target, value.get(source));
|
||||
}
|
||||
|
||||
private String writeJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException exception) { return "{}"; } }
|
||||
|
||||
private BigDecimal taskCharge(Map<String,Object> model) {
|
||||
|
||||
Reference in new issue
Block a user