fix: scope provider model keys and pin async credentials
This commit is contained in:
1 parent
58dc904c98
commit
558c0dcdd5
8 files changed
+166
-55
No files matched your search
+17
-11
@@ -295,7 +295,7 @@ public class OpenAiCompatibleProvider {
|
|||||||
if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim());
|
if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim());
|
||||||
addEnvironmentKeys(keys, providerId, environment);
|
addEnvironmentKeys(keys, providerId, environment);
|
||||||
if (providerCode != null && !providerCode.isBlank() && !providerCode.equals(providerId)) addEnvironmentKeys(keys, providerCode, environment);
|
if (providerCode != null && !providerCode.isBlank() && !providerCode.equals(providerId)) addEnvironmentKeys(keys, providerCode, environment);
|
||||||
return new ProviderConfig(providerId, keys.stream().distinct().toList(), baseUrl, cursor);
|
return new ProviderConfig(providerId, providerCode, keys.stream().distinct().toList(), baseUrl, cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addEnvironmentKeys(List<String> keys, String alias, org.springframework.core.env.Environment environment) {
|
private void addEnvironmentKeys(List<String> keys, String alias, org.springframework.core.env.Environment environment) {
|
||||||
@@ -304,7 +304,7 @@ public class OpenAiCompatibleProvider {
|
|||||||
if (configured != null && !configured.isBlank()) for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
if (configured != null && !configured.isBlank()) for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
private record ProviderConfig(String alias, List<String> apiKeys, String baseUrl, AtomicInteger cursor) {
|
private record ProviderConfig(String alias, String providerCode, List<String> apiKeys, String baseUrl, AtomicInteger cursor) {
|
||||||
private boolean hasKey() { return !apiKeys.isEmpty(); }
|
private boolean hasKey() { return !apiKeys.isEmpty(); }
|
||||||
private boolean hasKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
private boolean hasKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
||||||
return !apiKeys.isEmpty() || !routeKeys(routeId, upstreamModel, environment).isEmpty();
|
return !apiKeys.isEmpty() || !routeKeys(routeId, upstreamModel, environment).isEmpty();
|
||||||
@@ -322,19 +322,25 @@ public class OpenAiCompatibleProvider {
|
|||||||
|
|
||||||
private List<String> routeKeys(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
private List<String> routeKeys(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
||||||
if (environment == null) return List.of();
|
if (environment == null) return List.of();
|
||||||
for (String prefix : List.of("MOYI_UPSTREAM_API_KEYS_ROUTE_", "MOYI_UPSTREAM_API_KEYS_MODEL_")) {
|
if (routeId != null && !routeId.isBlank()) {
|
||||||
if (prefix.startsWith("MOYI_UPSTREAM_API_KEYS_ROUTE_") && (routeId == null || routeId.isBlank())) continue;
|
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||||
String selector = prefix.endsWith("MODEL_") ? upstreamModel : routeId;
|
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||||
if (selector == null || selector.isBlank()) continue;
|
}
|
||||||
String value = environment.getProperty(prefix + normalize(selector));
|
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||||
if (value == null || value.isBlank()) continue;
|
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
|
||||||
List<String> keys = new ArrayList<>();
|
String value = environment.getProperty(scopedName);
|
||||||
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
if (value == null || value.isBlank()) return List.of();
|
||||||
if (!keys.isEmpty()) return keys.stream().distinct().toList();
|
return parseKeys(value);
|
||||||
}
|
}
|
||||||
return List.of();
|
return List.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<String> parseKeys(String value) {
|
||||||
|
List<String> keys = new ArrayList<>();
|
||||||
|
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||||
|
return keys.stream().distinct().toList();
|
||||||
|
}
|
||||||
|
|
||||||
private static String normalize(String value) {
|
private static String normalize(String value) {
|
||||||
return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_");
|
return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_");
|
||||||
}
|
}
|
||||||
|
|||||||
+83
-34
@@ -13,6 +13,9 @@ import java.net.http.HttpResponse;
|
|||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -68,7 +71,8 @@ public class AsyncProviderClient {
|
|||||||
else if (!prompt.isBlank()) body.put("prompt", prompt);
|
else if (!prompt.isBlank()) body.put("prompt", prompt);
|
||||||
body.put("texture", true);
|
body.put("texture", true);
|
||||||
body.put("pbr", true);
|
body.put("pbr", true);
|
||||||
return request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), provider.nextApiKey(routeId, upstreamModel, environment, keyCursors), body, "上游 3D 任务创建失败");
|
String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors);
|
||||||
|
return withKeyFingerprint(request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), apiKey, body, "上游 3D 任务创建失败"), apiKey);
|
||||||
}
|
}
|
||||||
if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) {
|
if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) {
|
||||||
List<Map<String,Object>> content = new ArrayList<>();
|
List<Map<String,Object>> content = new ArrayList<>();
|
||||||
@@ -79,7 +83,8 @@ public class AsyncProviderClient {
|
|||||||
content.add(Map.of("type", "image_url", "image_url", Map.of("url", media)));
|
content.add(Map.of("type", "image_url", "image_url", Map.of("url", media)));
|
||||||
}
|
}
|
||||||
Map<String,Object> body = Map.of("model", upstreamModel, "content", content);
|
Map<String,Object> body = Map.of("model", upstreamModel, "content", content);
|
||||||
return request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", provider.nextApiKey(routeId, upstreamModel, environment, keyCursors), body, "上游视频任务创建失败");
|
String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors);
|
||||||
|
return withKeyFingerprint(request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", apiKey, body, "上游视频任务创建失败"), apiKey);
|
||||||
}
|
}
|
||||||
if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
|
if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
|
||||||
return submitOpenAiCompatible(provider, routeId, upstreamModel, taskType, input);
|
return submitOpenAiCompatible(provider, routeId, upstreamModel, taskType, input);
|
||||||
@@ -96,14 +101,18 @@ public class AsyncProviderClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Map<String,Object> poll(String providerId, String routeId, String upstreamModel, String providerTaskId) {
|
public Map<String,Object> poll(String providerId, String routeId, String upstreamModel, String providerTaskId) {
|
||||||
|
return poll(providerId, routeId, upstreamModel, providerTaskId, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String,Object> poll(String providerId, String routeId, String upstreamModel, String providerTaskId, String keyFingerprint) {
|
||||||
Provider provider = provider(providerId, routeId, upstreamModel);
|
Provider provider = provider(providerId, routeId, upstreamModel);
|
||||||
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
|
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
|
||||||
String url;
|
String url;
|
||||||
String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors);
|
String apiKey = provider.keyForFingerprint(routeId, upstreamModel, environment, keyCursors, keyFingerprint);
|
||||||
if (isTripo(provider.code(), provider.baseUrl())) url = tripoBase(provider.baseUrl()) + "/tasks/" + encode(providerTaskId);
|
if (isTripo(provider.code(), provider.baseUrl())) url = tripoBase(provider.baseUrl()) + "/tasks/" + encode(providerTaskId);
|
||||||
else if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) url = trim(provider.baseUrl()) + "/contents/generations/tasks/" + encode(providerTaskId);
|
else if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) url = trim(provider.baseUrl()) + "/contents/generations/tasks/" + encode(providerTaskId);
|
||||||
else if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
|
else if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
|
||||||
return pollOpenAiCompatible(provider, routeId, upstreamModel, providerTaskId);
|
return pollOpenAiCompatible(provider, routeId, upstreamModel, providerTaskId, keyFingerprint);
|
||||||
}
|
}
|
||||||
else throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
|
else throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
|
||||||
return request("GET", url, apiKey, null, "上游异步任务查询失败");
|
return request("GET", url, apiKey, null, "上游异步任务查询失败");
|
||||||
@@ -128,12 +137,13 @@ public class AsyncProviderClient {
|
|||||||
// choose its documented default and normalize either shape.
|
// choose its documented default and normalize either shape.
|
||||||
copyParameter(input, body, "response_format");
|
copyParameter(input, body, "response_format");
|
||||||
copyParameter(input, body, "size");
|
copyParameter(input, body, "size");
|
||||||
|
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
||||||
Map<String,Object> response = media == null
|
Map<String,Object> response = media == null
|
||||||
? request("POST", base + "/images/generations", provider.nextApiKey(routeId, model, environment, keyCursors), body, "上游图片任务创建失败")
|
? request("POST", base + "/images/generations", apiKey, body, "上游图片任务创建失败")
|
||||||
: requestMultipart("POST", base + "/images/edits", provider.nextApiKey(routeId, model, environment, keyCursors), body, "image", media, "上游图片任务创建失败");
|
: requestMultipart("POST", base + "/images/edits", apiKey, body, "image", media, "上游图片任务创建失败");
|
||||||
Map<String,Object> result = compatibleResult(response);
|
Map<String,Object> result = compatibleResult(response);
|
||||||
if (result != null) return Map.of("_moyiInlineResult", result);
|
if (result != null) return Map.of("_moyiInlineResult", result, "_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
|
||||||
return response;
|
return withKeyFingerprint(response, apiKey);
|
||||||
}
|
}
|
||||||
if ("video".equalsIgnoreCase(taskType)) {
|
if ("video".equalsIgnoreCase(taskType)) {
|
||||||
Map<String,Object> body = new LinkedHashMap<>();
|
Map<String,Object> body = new LinkedHashMap<>();
|
||||||
@@ -144,14 +154,14 @@ public class AsyncProviderClient {
|
|||||||
// official API and by relays that proxy it faithfully.
|
// official API and by relays that proxy it faithfully.
|
||||||
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
||||||
try {
|
try {
|
||||||
return requestMultipart("POST", base + "/videos", apiKey, body,
|
return withKeyFingerprint(requestMultipart("POST", base + "/videos", apiKey, body,
|
||||||
media == null ? null : "input_reference", media, "上游视频任务创建失败");
|
media == null ? null : "input_reference", media, "上游视频任务创建失败"), apiKey);
|
||||||
} catch (ApiException first) {
|
} catch (ApiException first) {
|
||||||
// A few older relays retained /videos/generations. Retry only
|
// A few older relays retained /videos/generations. Retry only
|
||||||
// on a route miss; authentication/credit errors are returned.
|
// on a route miss; authentication/credit errors are returned.
|
||||||
if (!isNotFound(first)) throw first;
|
if (!isNotFound(first)) throw first;
|
||||||
return requestMultipart("POST", base + "/videos/generations", apiKey, body,
|
return withKeyFingerprint(requestMultipart("POST", base + "/videos/generations", apiKey, body,
|
||||||
media == null ? null : "input_reference", media, "上游视频任务创建失败");
|
media == null ? null : "input_reference", media, "上游视频任务创建失败"), apiKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ("audio".equalsIgnoreCase(taskType)) {
|
if ("audio".equalsIgnoreCase(taskType)) {
|
||||||
@@ -164,7 +174,8 @@ public class AsyncProviderClient {
|
|||||||
body.put("voice", input.getOrDefault("voice", "alloy"));
|
body.put("voice", input.getOrDefault("voice", "alloy"));
|
||||||
body.put("response_format", input.getOrDefault("response_format", "mp3"));
|
body.put("response_format", input.getOrDefault("response_format", "mp3"));
|
||||||
copyParameter(input, body, "speed");
|
copyParameter(input, body, "speed");
|
||||||
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", provider.nextApiKey(routeId, model, environment, keyCursors), body, "上游语音任务创建失败"));
|
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
||||||
|
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", apiKey, body, "上游语音任务创建失败"), "_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
|
||||||
}
|
}
|
||||||
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道的媒体接口尚未配置", 501);
|
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道的媒体接口尚未配置", 501);
|
||||||
}
|
}
|
||||||
@@ -188,9 +199,9 @@ public class AsyncProviderClient {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String,Object> pollOpenAiCompatible(Provider provider, String routeId, String upstreamModel, String providerTaskId) {
|
private Map<String,Object> pollOpenAiCompatible(Provider provider, String routeId, String upstreamModel, String providerTaskId, String keyFingerprint) {
|
||||||
String base = trim(provider.baseUrl());
|
String base = trim(provider.baseUrl());
|
||||||
String apiKey = provider.nextApiKey(routeId, upstreamModel, environment, keyCursors);
|
String apiKey = provider.keyForFingerprint(routeId, upstreamModel, environment, keyCursors, keyFingerprint);
|
||||||
try {
|
try {
|
||||||
return request("GET", base + "/videos/" + encode(providerTaskId), apiKey, null, "上游异步任务查询失败");
|
return request("GET", base + "/videos/" + encode(providerTaskId), apiKey, null, "上游异步任务查询失败");
|
||||||
} catch (ApiException first) {
|
} catch (ApiException first) {
|
||||||
@@ -423,47 +434,85 @@ public class AsyncProviderClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<String> routeKeys(String providerCode, String providerId, String routeId, String upstreamModel) {
|
private List<String> routeKeys(String providerCode, String providerId, String routeId, String upstreamModel) {
|
||||||
for (String prefix : List.of("MOYI_UPSTREAM_API_KEYS_ROUTE_", "MOYI_UPSTREAM_API_KEYS_MODEL_")) {
|
if (routeId != null && !routeId.isBlank()) {
|
||||||
if (prefix.startsWith("MOYI_UPSTREAM_API_KEYS_ROUTE_") && (routeId == null || routeId.isBlank())) continue;
|
String configured = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||||
String alias = prefix.endsWith("MODEL_") ? upstreamModel : routeId;
|
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
||||||
if (alias == null || alias.isBlank()) continue;
|
}
|
||||||
String configured = environment.getProperty(prefix + normalize(alias));
|
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||||
if (configured == null || configured.isBlank()) continue;
|
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
|
||||||
List<String> keys = new ArrayList<>();
|
String configured = environment.getProperty(scopedName);
|
||||||
for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
||||||
if (!keys.isEmpty()) return keys.stream().distinct().toList();
|
|
||||||
}
|
}
|
||||||
return List.of();
|
return List.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<String> parseKeys(String configured) {
|
||||||
|
List<String> keys = new ArrayList<>();
|
||||||
|
for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||||
|
return keys.stream().distinct().toList();
|
||||||
|
}
|
||||||
|
|
||||||
private String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
private String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||||
|
private Map<String,Object> withKeyFingerprint(Map<String,Object> response, String apiKey) {
|
||||||
|
Map<String,Object> copy = new LinkedHashMap<>();
|
||||||
|
if (response != null) copy.putAll(response);
|
||||||
|
copy.put("_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String keyFingerprint(String apiKey) {
|
||||||
|
if (apiKey == null || apiKey.isBlank()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "异步任务未选择可轮询的上游凭据", 503);
|
||||||
|
try {
|
||||||
|
byte[] digest = MessageDigest.getInstance("SHA-256").digest(apiKey.getBytes(StandardCharsets.UTF_8));
|
||||||
|
StringBuilder result = new StringBuilder(digest.length * 2);
|
||||||
|
for (byte value : digest) result.append(String.format(java.util.Locale.ROOT, "%02x", value));
|
||||||
|
return result.toString();
|
||||||
|
} catch (NoSuchAlgorithmException exception) {
|
||||||
|
throw new IllegalStateException("SHA-256 is required by the Java runtime", exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isTripo(String code, String baseUrl) {
|
private boolean isTripo(String code, String baseUrl) {
|
||||||
String normalizedCode = code == null ? "" : code.toLowerCase(java.util.Locale.ROOT);
|
String normalizedCode = code == null ? "" : code.toLowerCase(java.util.Locale.ROOT);
|
||||||
String normalizedUrl = baseUrl == null ? "" : baseUrl.toLowerCase(java.util.Locale.ROOT);
|
String normalizedUrl = baseUrl == null ? "" : baseUrl.toLowerCase(java.util.Locale.ROOT);
|
||||||
return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d");
|
return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d");
|
||||||
}
|
}
|
||||||
private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
|
private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
|
||||||
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
|
private String nextApiKey() {
|
||||||
|
if (apiKeys.isEmpty()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "异步渠道没有可用的上游凭据", 503);
|
||||||
|
return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size()));
|
||||||
|
}
|
||||||
private String nextApiKey(String routeId, String upstreamModel, Environment environment, ConcurrentMap<String, AtomicInteger> cursors) {
|
private String nextApiKey(String routeId, String upstreamModel, Environment environment, ConcurrentMap<String, AtomicInteger> cursors) {
|
||||||
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
|
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
|
||||||
if (routeKeys.isEmpty()) return nextApiKey();
|
if (routeKeys.isEmpty()) return nextApiKey();
|
||||||
AtomicInteger routeCursor = cursors.computeIfAbsent("route:" + (routeId == null ? upstreamModel : routeId), ignored -> new AtomicInteger());
|
AtomicInteger routeCursor = cursors.computeIfAbsent("route:" + (routeId == null ? upstreamModel : routeId), ignored -> new AtomicInteger());
|
||||||
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
||||||
}
|
}
|
||||||
|
private String keyForFingerprint(String routeId, String upstreamModel, Environment environment,
|
||||||
|
ConcurrentMap<String, AtomicInteger> cursors, String fingerprint) {
|
||||||
|
if (fingerprint == null || fingerprint.isBlank()) return nextApiKey(routeId, upstreamModel, environment, cursors);
|
||||||
|
List<String> candidates = routeKeys(routeId, upstreamModel, environment);
|
||||||
|
if (candidates.isEmpty()) candidates = apiKeys;
|
||||||
|
for (String candidate : candidates) if (fingerprint.equals(keyFingerprint(candidate))) return candidate;
|
||||||
|
throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "创建任务时使用的上游凭据已从服务器配置中移除,无法安全轮询", 503);
|
||||||
|
}
|
||||||
private List<String> routeKeys(String routeId, String upstreamModel, Environment environment) {
|
private List<String> routeKeys(String routeId, String upstreamModel, Environment environment) {
|
||||||
if (environment == null) return List.of();
|
if (environment == null) return List.of();
|
||||||
for (String prefix : List.of("MOYI_UPSTREAM_API_KEYS_ROUTE_", "MOYI_UPSTREAM_API_KEYS_MODEL_")) {
|
if (routeId != null && !routeId.isBlank()) {
|
||||||
if (prefix.startsWith("MOYI_UPSTREAM_API_KEYS_ROUTE_") && (routeId == null || routeId.isBlank())) continue;
|
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||||
String selector = prefix.endsWith("MODEL_") ? upstreamModel : routeId;
|
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||||
if (selector == null || selector.isBlank()) continue;
|
}
|
||||||
String value = environment.getProperty(prefix + normalize(selector));
|
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||||
if (value == null || value.isBlank()) continue;
|
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(code()) + "_" + normalize(upstreamModel));
|
||||||
List<String> keys = new ArrayList<>();
|
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||||
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
|
||||||
if (!keys.isEmpty()) return keys.stream().distinct().toList();
|
|
||||||
}
|
}
|
||||||
return List.of();
|
return List.of();
|
||||||
}
|
}
|
||||||
|
private static List<String> parseKeys(String value) {
|
||||||
|
List<String> keys = new ArrayList<>();
|
||||||
|
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||||
|
return keys.stream().distinct().toList();
|
||||||
|
}
|
||||||
private static String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
private static String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,7 +180,7 @@ public class AsyncTaskService {
|
|||||||
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
|
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
|
||||||
);
|
);
|
||||||
taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null));
|
taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null));
|
||||||
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.model_id, t.provider_task_id, t.provider_deadline_at, t.created_at, m.provider_id, m.upstream_model FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
|
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.model_id, t.provider_task_id, t.provider_key_fingerprint, t.provider_deadline_at, t.created_at, m.provider_id, m.upstream_model FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
|
||||||
for (Map<String,Object> row : pending) pollProviderTask(row);
|
for (Map<String,Object> row : pending) pollProviderTask(row);
|
||||||
dispatchQueuedTasks();
|
dispatchQueuedTasks();
|
||||||
}
|
}
|
||||||
@@ -217,7 +217,7 @@ public class AsyncTaskService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
OffsetDateTime submittedAt = now();
|
OffsetDateTime submittedAt = now();
|
||||||
jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId);
|
jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_key_fingerprint=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), upstream.get("_moyiUpstreamKeyFingerprint"), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId);
|
||||||
} catch (RuntimeException exception) {
|
} catch (RuntimeException exception) {
|
||||||
String code = exception instanceof ApiException error ? error.getCode() : "UPSTREAM_TASK_SUBMIT_FAILED";
|
String code = exception instanceof ApiException error ? error.getCode() : "UPSTREAM_TASK_SUBMIT_FAILED";
|
||||||
String message = exception instanceof ApiException error ? error.getMessage() : "上游异步任务创建失败";
|
String message = exception instanceof ApiException error ? error.getMessage() : "上游异步任务创建失败";
|
||||||
@@ -381,8 +381,12 @@ public class AsyncTaskService {
|
|||||||
}
|
}
|
||||||
Map<String,Object> response;
|
Map<String,Object> response;
|
||||||
try {
|
try {
|
||||||
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("model_id")), String.valueOf(row.get("upstream_model")), String.valueOf(row.get("provider_task_id")));
|
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("model_id")), String.valueOf(row.get("upstream_model")), String.valueOf(row.get("provider_task_id")), row.get("provider_key_fingerprint") == null ? null : String.valueOf(row.get("provider_key_fingerprint")));
|
||||||
} catch (RuntimeException exception) {
|
} catch (RuntimeException exception) {
|
||||||
|
if (exception instanceof ApiException error && "UPSTREAM_KEY_NOT_AVAILABLE".equals(error.getCode())) {
|
||||||
|
failTask(taskId, error.getCode(), error.getMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Even a permanent authentication/configuration error only proves
|
// Even a permanent authentication/configuration error only proves
|
||||||
// that polling failed, not that an already accepted job stopped.
|
// that polling failed, not that an already accepted job stopped.
|
||||||
// Keep its hold and retry until the persisted deadline.
|
// Keep its hold and retry until the persisted deadline.
|
||||||
|
|||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
ALTER TABLE model_tasks
|
||||||
|
ADD COLUMN IF NOT EXISTS provider_key_fingerprint varchar(64);
|
||||||
|
|
||||||
|
COMMENT ON COLUMN model_tasks.provider_key_fingerprint IS
|
||||||
|
'SHA-256 fingerprint of the upstream credential selected for this async task; never stores the credential itself';
|
||||||
+2
-1
@@ -103,7 +103,8 @@ class OpenAiCompatibleProviderHttpTest {
|
|||||||
});
|
});
|
||||||
server.start();
|
server.start();
|
||||||
environment.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "route-key")
|
environment.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "route-key")
|
||||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_GPT_5", "model-key");
|
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_PROVIDER_DEEPSEEK_GPT_5", "model-key")
|
||||||
|
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_GPT_5", "wrong-global-key");
|
||||||
|
|
||||||
provider.chat("provider-deepseek", "gpt-5", "route-a", textInput(128));
|
provider.chat("provider-deepseek", "gpt-5", "route-a", textInput(128));
|
||||||
provider.chat("provider-deepseek", "gpt-5", "route-b", textInput(128));
|
provider.chat("provider-deepseek", "gpt-5", "route-b", textInput(128));
|
||||||
|
|||||||
+44
@@ -14,11 +14,13 @@ import com.moyi.controlplane.security.SecretCipher;
|
|||||||
import com.sun.net.httpserver.HttpServer;
|
import com.sun.net.httpserver.HttpServer;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.CsvSource;
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.mock.env.MockEnvironment;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
class AsyncProviderClientTest {
|
class AsyncProviderClientTest {
|
||||||
@@ -139,6 +141,48 @@ class AsyncProviderClientTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void asyncPollingReusesTheSubmittedKeyFingerprint() throws Exception {
|
||||||
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||||
|
AtomicReference<String> submitAuthorization = new AtomicReference<>();
|
||||||
|
AtomicReference<String> pollAuthorization = new AtomicReference<>();
|
||||||
|
server.createContext("/videos", exchange -> {
|
||||||
|
String authorization = exchange.getRequestHeaders().getFirst("Authorization");
|
||||||
|
if (exchange.getRequestURI().getPath().equals("/videos")) {
|
||||||
|
submitAuthorization.set(authorization);
|
||||||
|
exchange.getRequestBody().readAllBytes();
|
||||||
|
byte[] response = "{\"id\":\"video-1\",\"status\":\"queued\"}".getBytes(StandardCharsets.UTF_8);
|
||||||
|
exchange.sendResponseHeaders(200, response.length);
|
||||||
|
try (var output = exchange.getResponseBody()) { output.write(response); }
|
||||||
|
} else {
|
||||||
|
pollAuthorization.set(authorization);
|
||||||
|
byte[] response = "{\"id\":\"video-1\",\"status\":\"succeeded\"}".getBytes(StandardCharsets.UTF_8);
|
||||||
|
exchange.sendResponseHeaders(200, response.length);
|
||||||
|
try (var output = exchange.getResponseBody()) { output.write(response); }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
server.start();
|
||||||
|
try {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
SecretCipher cipher = new SecretCipher("test-encryption");
|
||||||
|
when(jdbc.queryForMap(any(String.class), eq("provider-video"))).thenReturn(Map.of(
|
||||||
|
"code", "relay-video", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", ""
|
||||||
|
));
|
||||||
|
MockEnvironment environment = new MockEnvironment()
|
||||||
|
.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "key-a|key-b")
|
||||||
|
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_RELAY_VIDEO_SORA_2", "wrong-model-key");
|
||||||
|
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), environment);
|
||||||
|
|
||||||
|
Map<String,Object> submitted = client.submit("provider-video", "sora-2", "route-a", "video", Map.of("prompt", "a cat"));
|
||||||
|
String fingerprint = String.valueOf(submitted.get("_moyiUpstreamKeyFingerprint"));
|
||||||
|
assertTrue(fingerprint.matches("[0-9a-f]{64}"));
|
||||||
|
client.poll("provider-video", "route-a", "sora-2", "video-1", fingerprint);
|
||||||
|
assertEquals(submitAuthorization.get(), pollAuthorization.get());
|
||||||
|
} finally {
|
||||||
|
server.stop(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"})
|
@CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"})
|
||||||
void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception {
|
void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception {
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ MOYI 用户对外使用的统一 API Base URL 为 `https://moyi.soulofmodel.com/
|
|||||||
|
|
||||||
## 一个渠道配置多个上游 Key
|
## 一个渠道配置多个上游 Key
|
||||||
|
|
||||||
可以。当前 OpenAI 兼容转发器支持为同一渠道配置多个上游 Key,并按请求轮换,适合把上游的单 Key 并发拆到多个 Key。新版还支持“按 MOYI 模型分组固定 Key 池”:路由分组 Key 优先,其次是上游模型 Key,再其次是渠道级 Key,最后才回退到管理端渠道里的单个加密凭据。这样 APIPro、ApiHub、TokenFoundryX 可以为每个上游分组分别配置一个或多个 Key,不会把某个分组的 Key 错轮到另一个分组。
|
可以。当前 OpenAI 兼容转发器支持为同一渠道配置多个上游 Key,并按请求轮换,适合把上游的单 Key 并发拆到多个 Key。新版还支持“按 MOYI 模型分组固定 Key 池”:路由分组 Key 优先,其次是带渠道编码的上游模型 Key,再其次是渠道级 Key,最后才回退到管理端渠道里的单个加密凭据。模型 Key 必须带渠道编码,避免 APIPro、ApiHub、TokenFoundryX 的同名模型误用彼此的凭据。
|
||||||
|
|
||||||
服务器环境变量优先使用渠道编码(管理端渠道的 code),也兼容渠道 ID 转成大写下划线后的名字,Key 之间用 `|` 分隔。例如渠道编码为 `apipro` 时:
|
服务器环境变量优先使用渠道编码(管理端渠道的 code),也兼容渠道 ID 转成大写下划线后的名字,Key 之间用 `|` 分隔。例如渠道编码为 `apipro` 时:
|
||||||
|
|
||||||
@@ -206,12 +206,14 @@ MOYI_UPSTREAM_API_KEYS_APIPRO='key_a|key_b|key_c'
|
|||||||
MOYI_UPSTREAM_API_KEYS_ROUTE_RELAY_ROUTE_044E37BB_AB61_4C6D_88A8_13ADDA55E4C0B='group_key'
|
MOYI_UPSTREAM_API_KEYS_ROUTE_RELAY_ROUTE_044E37BB_AB61_4C6D_88A8_13ADDA55E4C0B='group_key'
|
||||||
```
|
```
|
||||||
|
|
||||||
如果希望同一个上游模型的多个分组共用一个 Key 池,可以使用:
|
如果希望同一个渠道中的上游模型的多个分组共用一个 Key 池,可以使用带渠道编码的变量。渠道编码为 `apipro`、上游模型为 `glm-4.7` 时:
|
||||||
|
|
||||||
```ini
|
```ini
|
||||||
MOYI_UPSTREAM_API_KEYS_MODEL_GLM_4_7='model_key_a|model_key_b'
|
MOYI_UPSTREAM_API_KEYS_MODEL_APIPRO_GLM_4_7='model_key_a|model_key_b'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
不再使用没有渠道编码的 `MOYI_UPSTREAM_API_KEYS_MODEL_GLM_4_7` 形式;它会被忽略,防止同名模型跨供应商串用 Key。
|
||||||
|
|
||||||
动态变量由 `docker-compose.cloud.yml` 的 API 服务 `env_file: .env.cloud` 注入容器;真实 Key 只能写服务器上的 `.env.cloud`,不能写 Git、迁移文件或聊天记录。每个分组创建一把 Key 时只填单个值。同步请求支持用竖线分隔多把 Key 轮换;异步任务建议每个分组只配置一把 Key,因为部分供应商要求用创建任务的同一凭据轮询任务状态。调用和异步创建/轮询都使用同一优先级规则。
|
动态变量由 `docker-compose.cloud.yml` 的 API 服务 `env_file: .env.cloud` 注入容器;真实 Key 只能写服务器上的 `.env.cloud`,不能写 Git、迁移文件或聊天记录。每个分组创建一把 Key 时只填单个值。同步请求支持用竖线分隔多把 Key 轮换;异步任务建议每个分组只配置一把 Key,因为部分供应商要求用创建任务的同一凭据轮询任务状态。调用和异步创建/轮询都使用同一优先级规则。
|
||||||
|
|
||||||
保留管理端渠道中的单个凭据作为回退 Key。同步聊天和已接入的异步执行器都会在每个请求或轮询请求前轮换 Key。轮换只解决 Key 分摊,不能突破上游账户总并发、余额、模型授权或速率限制;平台自己的模型并发和单个 MOYI Key 并发仍由网关队列控制。多个 Key 应来自同一渠道、具有相同模型授权,并在服务器密钥文件中保存,不能提交到 Git。当前没有把这些 Key 明文放进管理端数据库的功能,避免把上游密钥暴露给前端;需要新增或撤销 Key 时修改服务器 `.env.cloud` 后重启 API 容器即可。
|
保留管理端渠道中的单个凭据作为回退 Key。同步聊天和已接入的异步执行器都会在每个请求或轮询请求前轮换 Key。轮换只解决 Key 分摊,不能突破上游账户总并发、余额、模型授权或速率限制;平台自己的模型并发和单个 MOYI Key 并发仍由网关队列控制。多个 Key 应来自同一渠道、具有相同模型授权,并在服务器密钥文件中保存,不能提交到 Git。当前没有把这些 Key 明文放进管理端数据库的功能,避免把上游密钥暴露给前端;需要新增或撤销 Key 时修改服务器 `.env.cloud` 后重启 API 容器即可。
|
||||||
@@ -94,8 +94,8 @@ OSS_ACCESS_KEY_SECRET=
|
|||||||
# Optional per-route upstream key pools. Do not commit real values. Replace
|
# Optional per-route upstream key pools. Do not commit real values. Replace
|
||||||
# ROUTE_ID with the normalized MOYI route/model id (uppercase, non-alphanumerics
|
# ROUTE_ID with the normalized MOYI route/model id (uppercase, non-alphanumerics
|
||||||
# replaced by underscores), and separate multiple keys with '|'. Route pools
|
# replaced by underscores), and separate multiple keys with '|'. Route pools
|
||||||
# take precedence over model pools, then channel-level pools, then the single
|
# take precedence over provider-scoped model pools, then channel-level pools,
|
||||||
# encrypted credential stored in the admin channel record.
|
# then the single encrypted credential stored in the admin channel record.
|
||||||
# MOYI_UPSTREAM_API_KEYS_ROUTE_<NORMALIZED_ROUTE_ID>='upstream_key'
|
# MOYI_UPSTREAM_API_KEYS_ROUTE_<NORMALIZED_ROUTE_ID>='upstream_key'
|
||||||
# MOYI_UPSTREAM_API_KEYS_MODEL_<NORMALIZED_UPSTREAM_MODEL>='upstream_key'
|
# MOYI_UPSTREAM_API_KEYS_MODEL_<NORMALIZED_PROVIDER_CODE>_<NORMALIZED_UPSTREAM_MODEL>='upstream_key'
|
||||||
# MOYI_UPSTREAM_API_KEYS_APIPRO='key_for_channel_fallback_1|key_for_channel_fallback_2'
|
# MOYI_UPSTREAM_API_KEYS_APIPRO='key_for_channel_fallback_1|key_for_channel_fallback_2'
|
||||||
Reference in new issue
Block a user