integrate official Tripo async models

This commit is contained in:
wlx committed 2026-09-27 16:45:36 +08:00
1 parent a022162382
commit 962eef26e5
11 files changed
+160 -20

No files matched your search

@@ -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) {
@@ -0,0 +1,109 @@
-- Tripo's current official international API is openapi.tripo3d.com.
-- The API key is intentionally supplied outside migrations through the
-- encrypted provider credential or TRIPO_API_KEY in the deployment secret.
UPDATE model_providers
SET base_url = 'https://openapi.tripo3d.com',
name = 'Tripo 官方',
description = 'Tripo 官方异步 3D 生成 API。',
updated_at = now()
WHERE id = 'provider-vast'
AND code = 'vast-tripo'
AND deleted_at IS NULL;
DO $$
DECLARE
item record;
provider_id_value varchar(64);
provider_status_value varchar(16);
logical_id_value varchar(64);
route_status_value varchar(16);
tripo_price jsonb := jsonb_build_object(
'price', 1.44,
'unit', 'CNY / task',
'upstreamCredits', 20,
'exchangeRate', 7.2,
'priceSource', 'Tripo 官方 API 定价',
'pricingNote', 'H 系列文生 3D,标准贴图;未加平台服务费'
);
BEGIN
SELECT id, status INTO provider_id_value, provider_status_value
FROM model_providers
WHERE id = 'provider-vast' AND code = 'vast-tripo' AND deleted_at IS NULL;
IF provider_id_value IS NULL THEN
RETURN;
END IF;
FOR item IN
SELECT * FROM (VALUES
('tripo-v3', 'Tripo H v3.1 文生 3D', 'v3.1-20260211'),
('tripo-h-v3-0', 'Tripo H v3.0 文生 3D', 'v3.0-20250812'),
('tripo-h-v2-5', 'Tripo H v2.5 文生 3D', 'v2.5-20250123')
) AS models(model_key_value, public_name_value, upstream_model_value)
LOOP
INSERT INTO model_definitions (
id, model_key, public_name, vendor_name, model_type, call_mode,
description, input_types, output_types, status
)
SELECT 'tripo-def-' || replace(item.model_key_value, '.', '-') || '-' || substr(md5(item.model_key_value), 1, 8),
item.model_key_value, item.public_name_value, 'Tripo', '3d', 'async',
'Tripo 官方 H 系列异步文生 3D 模型。',
'["text"]'::jsonb, '["model", "thumbnail"]'::jsonb, 'active'
WHERE NOT EXISTS (
SELECT 1 FROM model_definitions d
WHERE d.model_key = item.model_key_value AND d.deleted_at IS NULL
);
SELECT d.id INTO logical_id_value
FROM model_definitions d
WHERE d.model_key = item.model_key_value AND d.deleted_at IS NULL
ORDER BY d.created_at
LIMIT 1;
route_status_value := CASE WHEN provider_status_value = 'active' THEN 'active' ELSE 'disabled' END;
INSERT INTO model_catalog (
id, provider_id, model_key, source_label, public_name, upstream_model,
model_type, call_mode, description, input_types, output_types,
capabilities, pricing, priority, max_concurrency, logical_model_id, status
)
SELECT 'tripo-route-' || replace(item.model_key_value, '.', '-') || '-' || substr(md5(item.upstream_model_value), 1, 8),
provider_id_value, item.model_key_value, 'Tripo 官方', item.public_name_value,
item.upstream_model_value, '3d', 'async',
'Tripo 官方 H 系列异步文生 3D 模型。',
'["text"]'::jsonb, '["model", "thumbnail"]'::jsonb,
'{"taskPolling":true,"formats":["glb","obj","fbx"]}'::jsonb,
tripo_price, 100, 1, logical_id_value, route_status_value
WHERE NOT EXISTS (
SELECT 1 FROM model_catalog m
WHERE m.logical_model_id = logical_id_value
AND m.provider_id = provider_id_value
AND m.upstream_model = item.upstream_model_value
AND m.deleted_at IS NULL
);
UPDATE model_catalog
SET source_label = 'Tripo 官方',
public_name = item.public_name_value,
description = 'Tripo 官方 H 系列异步文生 3D 模型。',
input_types = '["text"]'::jsonb,
output_types = '["model", "thumbnail"]'::jsonb,
capabilities = '{"taskPolling":true,"formats":["glb","obj","fbx"]}'::jsonb,
pricing = tripo_price,
priority = 100,
max_concurrency = 1,
status = route_status_value,
updated_at = now()
WHERE provider_id = provider_id_value
AND upstream_model = item.upstream_model_value
AND deleted_at IS NULL;
END LOOP;
END $$;
-- Keep the existing logical model's manufacturer and public metadata aligned
-- even when an earlier deployment created it before V16.
UPDATE model_definitions
SET vendor_name = 'Tripo',
public_name = CASE WHEN model_key = 'tripo-v3' THEN 'Tripo H v3.1 文生 3D' ELSE public_name END,
updated_at = now()
WHERE model_key IN ('tripo-v3', 'tripo-h-v3-0', 'tripo-h-v2-5')
AND deleted_at IS NULL;