feat: publish readable model ids and media task adapters

This commit is contained in:
wlx committed 2026-09-29 01:29:07 +08:00
1 parent ba98b47618
commit 94eaee7b95
16 files changed
+654 -41

No files matched your search

@@ -67,7 +67,7 @@ class GatewayDeploymentTest {
}
@Test
void modelsKeepRouteIdAndExposeDisplayMetadata() {
void modelsExposeReadableIdAndKeepRouteIdForDiagnostics() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
@@ -90,7 +90,9 @@ class GatewayDeploymentTest {
Map<String, Object> response = controller.models("Bearer test-key");
@SuppressWarnings("unchecked")
Map<String, Object> item = ((List<Map<String, Object>>) response.get("data")).getFirst();
assertEquals("route-1", item.get("id"));
assertEquals("deepseek-v4-flash--apihub--" + tag("route-1"), item.get("id"));
assertEquals("route-1", item.get("moyi_route_id"));
assertEquals("DeepSeek V4 Flash", item.get("name"));
assertEquals("deepseek-v4-flash", item.get("model_key"));
assertEquals("DeepSeek V4 Flash", item.get("public_name"));
assertEquals("DeepSeek", item.get("vendor_name"));
@@ -98,6 +100,88 @@ class GatewayDeploymentTest {
assertEquals("deepseek-v4-flash", item.get("upstream_model"));
}
@Test
void duplicateModelKeysAreDisambiguatedBySourceGroup() {
var first = Map.<String, Object>of(
"id", "route-a", "providerId", "provider-a", "modelKey", "gpt-5.5",
"publicName", "GPT-5.5", "sourceLabel", "ApiHub 国内", "upstreamModel", "gpt-5.5");
var second = Map.<String, Object>of(
"id", "route-b", "providerId", "provider-b", "modelKey", "gpt-5.5",
"publicName", "GPT-5.5", "sourceLabel", "APIPro", "upstreamModel", "gpt-5.5");
var visible = List.of(first, second);
assertEquals("gpt-5.5--apihub--" + tag("route-a"), GatewayModelIds.publicId(first));
assertEquals("gpt-5.5--apipro--" + tag("route-b"), GatewayModelIds.publicId(second));
}
@Test
void readableIdResolvesOnlyToAnAuthorizedRoute() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> route = Map.of(
"id", "route-readable", "providerId", "provider-a", "modelKey", "glm-5",
"publicName", "GLM-5", "sourceLabel", "ApiHub 国内", "upstreamModel", "glm-5");
when(catalog.activeModel("glm-5--apihub")).thenReturn(null);
when(catalog.listModels(false)).thenReturn(List.of(route));
when(catalog.keyCanInvoke(key, "route-readable")).thenReturn(true);
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
assertSame(route, controller.resolveModel(key, "glm-5--apihub--" + tag("route-readable")));
verify(catalog).keyCanInvoke(key, "route-readable");
}
@Test
void legacyRouteIdRemainsCompatibleAndUnauthorizedRouteIsRejected() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> route = Map.of("id", "route-legacy", "modelKey", "gpt-5", "sourceLabel", "APIPro");
when(catalog.listModels(false)).thenReturn(List.of());
when(catalog.activeModel("route-legacy")).thenReturn(route);
when(catalog.keyCanInvoke(key, "route-legacy")).thenReturn(true);
assertSame(route, GatewayModelIds.resolve(catalog, key, "route-legacy"));
Map<String, Object> forbidden = Map.of("id", "route-forbidden", "modelKey", "gpt-5", "sourceLabel", "APIPro");
when(catalog.activeModel("route-forbidden")).thenReturn(forbidden);
when(catalog.keyCanInvoke(key, "route-forbidden")).thenReturn(false);
ApiException error = assertThrows(ApiException.class, () -> GatewayModelIds.resolve(catalog, key, "route-forbidden"));
assertEquals("MODEL_FORBIDDEN", error.getCode());
assertEquals(403, error.getStatus());
}
@Test
void routeTagKeepsPreviouslyImportedIdUsableAfterRename() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> renamed = Map.of(
"id", "route-renamed", "modelKey", "new-model-key", "publicName", "New Model",
"sourceLabel", "New Group", "upstreamModel", "new-upstream");
String oldPublicId = "old-model--old-group--" + tag("route-renamed");
when(catalog.listModels(false)).thenReturn(List.of(renamed));
when(catalog.keyCanInvoke(key, "route-renamed")).thenReturn(true);
assertSame(renamed, GatewayModelIds.resolve(catalog, key, oldPublicId));
verify(catalog, never()).activeModel(oldPublicId);
}
@Test
void legacyAliasWithMultipleAuthorizedRoutesIsRejectedAsAmbiguous() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> first = Map.of("id", "route-a", "modelKey", "same-model", "upstreamModel", "same-model", "sourceLabel", "A");
Map<String, Object> second = Map.of("id", "route-b", "modelKey", "same-model", "upstreamModel", "same-model", "sourceLabel", "B");
when(catalog.listModels(false)).thenReturn(List.of(first, second));
when(catalog.activeModel("same-model")).thenReturn(null);
when(catalog.keyCanInvoke(key, "route-a")).thenReturn(true);
when(catalog.keyCanInvoke(key, "route-b")).thenReturn(true);
ApiException error = assertThrows(ApiException.class, () -> GatewayModelIds.resolve(catalog, key, "same-model"));
assertEquals("MODEL_AMBIGUOUS", error.getCode());
assertEquals(400, error.getStatus());
}
private static String tag(String route) {
try {
return java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(route.getBytes(java.nio.charset.StandardCharsets.UTF_8))).substring(0, 16);
} catch (java.security.NoSuchAlgorithmException exception) { throw new AssertionError(exception); }
}
@Test
void invalidRedisPrefixIsRejected() {
assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, ""));