diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java b/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java index e08e851..0e4f77d 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/catalog/CatalogService.java @@ -541,6 +541,8 @@ public class CatalogService { // while unsupported task types remain visibly pending. boolean asyncExecutor = asyncExecutorReady( resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type")); + String upstream=resultSet.getString("upstream_model").toLowerCase(java.util.Locale.ROOT); + if(upstream.startsWith("mj_") || upstream.startsWith("suno") || upstream.startsWith("lyria") || upstream.equals("swap_face")) asyncExecutor=false; boolean syncExecutor=List.of("chat","text","code","multimodal","llm","reasoning","vision","embedding").contains(resultSet.getString("model_type")) && ("openai-compatible".equalsIgnoreCase(providerProtocol)||"provider-mock".equals(resultSet.getString("provider_id"))); model.put("executionReady", "async".equals(resultSet.getString("call_mode")) ? asyncExecutor : syncExecutor); var protocols=new java.util.ArrayList(); @@ -548,10 +550,21 @@ public class CatalogService { for(String protocol:List.of("messages","responses","gemini")) if(com.moyi.controlplane.gateway.NativeProtocolMeter.supports(model,protocol)) protocols.add(protocol); model.put("supportedProtocols",protocols); model.put("billingReady", billingReady(pricing, resultSet.getString("call_mode"), resultSet.getString("provider_id"))); + if(!Boolean.TRUE.equals(model.get("billingReady"))) { + String reason=pricing==null ? "当前型号报价待核实" : "计费规则待核实"; + if("async".equals(model.get("mode")) && pricing!=null) { + try { com.moyi.controlplane.task.AsyncPriceSchedule.validate(pricing); } + catch(ApiException e) { reason=e.getMessage(); } + } + model.put("billingReason",reason); + } // Keep the internal route UUID for permission storage and expose a // stable, readable identifier for the user console and third-party // clients. The gateway uses the same value in /v1/models. model.put("gatewayModelId", GatewayModelIds.publicId(model)); + if(model.get("capabilities") instanceof Map capabilities && Boolean.TRUE.equals(capabilities.get("integrationDeferred"))) { + model.put("integrationDeferred",true); model.put("executionReady",false); model.put("billingReady",false); model.put("billingReason","暂不接入"); + } model.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class)); return model; } @@ -577,10 +590,7 @@ public class CatalogService { if (pricing == null) return false; String unit = String.valueOf(pricing.getOrDefault("unit", "")).trim().toLowerCase(java.util.Locale.ROOT); if ("async".equals(mode)) { - if (unit.contains("token")) return false; - Object amount = pricing.get("price"); - if (amount == null) amount = pricing.get("input"); - return amount != null; + return com.moyi.controlplane.task.AsyncPriceSchedule.ready(pricing); } return unit.equals("cny / 1m tokens") && pricing.get("input") != null && pricing.get("output") != null; } diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayModelIds.java b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayModelIds.java index 859192e..3ba6b36 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayModelIds.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/gateway/GatewayModelIds.java @@ -45,7 +45,7 @@ public final class GatewayModelIds { List> exact = authorized.stream() .filter(model -> requested.equals(publicId(model)) || matchesRouteTag(requested, model)) .toList(); - if (exact.size() == 1) return exact.getFirst(); + if (exact.size() == 1) return requireOnboarding(exact.getFirst()); if (exact.size() > 1) throw ambiguous(); // Avoid treating a readable public identifier as a legacy database ID // if an administrator happens to create a route with the same text. @@ -54,13 +54,18 @@ public final class GatewayModelIds { if (direct != null) { if (!catalog.keyCanInvoke(key, String.valueOf(direct.get("id")))) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403); - return direct; + return requireOnboarding(direct); } List> aliases = authorized.stream() .filter(model -> equalsNonBlank(requested, model.get("modelKey")) || equalsNonBlank(requested, model.get("upstreamModel"))) .toList(); if (aliases.size() > 1) throw ambiguous(); - return aliases.isEmpty() ? null : aliases.getFirst(); + return aliases.isEmpty() ? null : requireOnboarding(aliases.getFirst()); + } + + private static Map requireOnboarding(Map model) { + if(Boolean.TRUE.equals(model.get("integrationDeferred"))) throw new ApiException("MODEL_ONBOARDING_DEFERRED","该模型暂不接入",503); + return model; } private static boolean matchesRouteTag(String requested, Map model) { diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncPriceSchedule.java b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncPriceSchedule.java new file mode 100644 index 0000000..74c2fe9 --- /dev/null +++ b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncPriceSchedule.java @@ -0,0 +1,95 @@ +package com.moyi.controlplane.task; + +import com.moyi.controlplane.common.ApiException; +import com.moyi.controlplane.gateway.TokenPriceSchedule; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.*; + +/** Task prices are frozen at admission; only provider-reported usage can settle tokens. */ +public final class AsyncPriceSchedule { + private AsyncPriceSchedule() {} + public static boolean ready(Map pricing) { + try { validate(pricing); return true; } catch (ApiException e) { return false; } + } + public static void validate(Map pricing) { + if (pricing == null) throw unavailable("该分组缺少人民币售价"); + String unit=String.valueOf(pricing.get("unit")); + if ("CNY / 1M tokens".equals(unit)) { + if (!List.of("openai-image-tokens","gemini-image-tokens").contains(pricing.getOrDefault("taskBilling",""))) + throw unavailable("已配置 Token 单价,尚未配置对应的异步用量结算"); + TokenPriceSchedule.validate(pricing); + if(pricing.get("tiers") instanceof List tiers && !tiers.isEmpty()) throw unavailable("图片上下文阶梯尚未接入异步结算"); + if(pricing.containsKey("cacheWrite5m") || pricing.containsKey("cacheWrite1h")) throw unavailable("图片缓存写入计费尚未接入"); + if(pricing.containsKey("imageInput")) TokenPriceSchedule.amount(pricing.get("imageInput")); + if(pricing.containsKey("cachedImageInput")) TokenPriceSchedule.amount(pricing.get("cachedImageInput")); + } else { + if (!List.of("CNY / task","CNY / image","CNY / generation").contains(unit)) + throw unavailable("该计费单位尚未接入异步结算"); + TokenPriceSchedule.amount(pricing.getOrDefault("price",pricing.get("input"))); + } + } + public static BigDecimal reserve(Map pricing, Map input) { + validate(pricing); + if(input.get("n")!=null) { + try { if(new BigDecimal(String.valueOf(input.get("n"))).compareTo(BigDecimal.ONE)!=0) throw new NumberFormatException(); } + catch(Exception e) { throw new ApiException("INVALID_TASK_INPUT","每个图片任务仅支持生成 1 张图片"); } + } + if(!tokens(pricing)) return TokenPriceSchedule.amount(pricing.getOrDefault("price",pricing.get("input"))); + if(String.valueOf(input.getOrDefault("prompt","")).getBytes(java.nio.charset.StandardCharsets.UTF_8).length>16384) + throw new ApiException("INVALID_TASK_INPUT","图片提示词最多 16384 字节"); + // A conservative admission estimate, not a substituted usage charge. + // Final provider usage may exceed this hold, as with synchronous metering. + BigDecimal inputRate=TokenPriceSchedule.amount(pricing.get("input")) + .max(TokenPriceSchedule.amount(pricing.getOrDefault("imageInput",pricing.get("input")))); + return inputRate.multiply(BigDecimal.valueOf(65536)) + .add(TokenPriceSchedule.amount(pricing.get("output")).multiply(BigDecimal.valueOf(32768))) + .divide(new BigDecimal("1000000"),8,RoundingMode.CEILING); + } + public static boolean tokens(Map pricing) { return pricing!=null && "CNY / 1M tokens".equals(pricing.get("unit")); } + public record Bill(BigDecimal amount,long input,long output,long imageInput) {} + public static Bill bill(Map pricing,Map result) { + validate(pricing); + if(!tokens(pricing)) return new Bill(TokenPriceSchedule.amount(pricing.getOrDefault("price",pricing.get("input"))),0,0,0); + Map usage=map(result==null?null:result.get("usage")); + long input,output,image=0,cachedText=0,cachedImage=0; + if("gemini-image-tokens".equals(pricing.get("taskBilling"))) { + usage=map(result==null?null:result.get("usageMetadata")); + input=count(usage.get("promptTokenCount")); + output=count(usage.get("candidatesTokenCount"))+optional(usage,"thoughtsTokenCount"); + if(optional(usage,"cachedContentTokenCount")>0) throw missing(); + } else { + input=count(usage.get("input_tokens")); output=count(usage.get("output_tokens")); + var detail=map(usage.get("input_tokens_details")); + long cached=optional(detail,"cached_tokens"); + if(optional(usage,"cached_tokens")>0) cached=count(usage.get("cached_tokens")); + if(pricing.containsKey("imageInput")) { + image=count(detail.get("image_tokens")); + long text=count(detail.get("text_tokens")); + if(image+text!=input) throw missing(); + } + if(cached>0) { + var cachedDetail=map(detail.get("cached_tokens_details")); + cachedImage=count(cachedDetail.get("image_tokens")); cachedText=count(cachedDetail.get("text_tokens")); + if(cachedText+cachedImage!=cached || cachedImage>image || cachedText>input-image + || !pricing.containsKey("cachedInput") || !pricing.containsKey("cachedImageInput")) throw missing(); + } + } + if(input+output==0 || image>input) throw missing(); + BigDecimal amount=TokenPriceSchedule.amount(pricing.get("input")).multiply(BigDecimal.valueOf(input-image-cachedText)) + .add(TokenPriceSchedule.amount(pricing.getOrDefault("imageInput",pricing.get("input"))).multiply(BigDecimal.valueOf(image-cachedImage))) + .add(TokenPriceSchedule.amount(pricing.getOrDefault("cachedInput",0)).multiply(BigDecimal.valueOf(cachedText))) + .add(TokenPriceSchedule.amount(pricing.getOrDefault("cachedImageInput",0)).multiply(BigDecimal.valueOf(cachedImage))) + .add(TokenPriceSchedule.amount(pricing.get("output")).multiply(BigDecimal.valueOf(output))) + .divide(new BigDecimal("1000000"),8,RoundingMode.HALF_UP); + return new Bill(amount,input,output,image); + } + private static Map map(Object value) { return value instanceof Map m ? m : Map.of(); } + private static long optional(Map value,String field) { return value.get(field)==null?0:count(value.get(field)); } + private static long count(Object value) { + try { long n=new BigDecimal(String.valueOf(value)).longValueExact(); if(n<0 || n>100_000_000) throw new NumberFormatException(); return n; } + catch(Exception e) { throw missing(); } + } + private static ApiException unavailable(String message) { return new ApiException("ASYNC_BILLING_UNSUPPORTED",message,503); } + private static ApiException missing() { return new ApiException("TASK_USAGE_PENDING","生成已完成,但上游未返回完整可核对的用量;暂不扣费,等待核账",502); } +} diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncProviderClient.java b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncProviderClient.java index 1cc79ab..246ec2f 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncProviderClient.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncProviderClient.java @@ -129,6 +129,7 @@ public class AsyncProviderClient { String media = mediaUrl(input); String base = trim(provider.baseUrl()); if ("image".equalsIgnoreCase(taskType)) { + if(model.startsWith("gemini-") && model.contains("image")) return submitGeminiImage(provider,routeId,model,input); Map body = new LinkedHashMap<>(); body.put("model", model); body.put("prompt", prompt); body.put("n", 1); // DALL-E accepts `url` while GPT-image models require @@ -137,6 +138,7 @@ public class AsyncProviderClient { // choose its documented default and normalize either shape. copyParameter(input, body, "response_format"); copyParameter(input, body, "size"); + copyParameter(input, body, "quality"); String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors); Map response = media == null ? request("POST", base + "/images/generations", apiKey, body, "上游图片任务创建失败") @@ -185,6 +187,53 @@ public class AsyncProviderClient { if (value != null) body.put(name, value); } + private Map submitGeminiImage(Provider provider,String routeId,String model,Map input) { + var parts=new ArrayList>(); + parts.add(Map.of("text",String.valueOf(input.getOrDefault("prompt","")))); + String media=mediaUrl(input); + if(media!=null) { + try { + MediaBytes file=downloadMedia(media); + parts.add(Map.of("inlineData",Map.of("mimeType",file.contentType(),"data",Base64.getEncoder().encodeToString(file.bytes())))); + } catch(ApiException e) { throw e; } + catch(Exception e) { throw new ApiException("MEDIA_FETCH_FAILED","无法读取参考图",400); } + } + var config=new LinkedHashMap(); + config.put("responseModalities",List.of("TEXT","IMAGE")); + var imageConfig=new LinkedHashMap(); + Object size=input.getOrDefault("imageSize",input.get("size")); + if(size!=null) { + if(!List.of("1K","2K","4K").contains(String.valueOf(size))) throw new ApiException("INVALID_TASK_INPUT","Gemini 图片尺寸请使用 1K、2K 或 4K",400); + imageConfig.put("imageSize",size); + } + copyParameter(input,imageConfig,"aspectRatio"); + if(!imageConfig.isEmpty()) config.put("imageConfig",imageConfig); + String base=trim(provider.baseUrl()).replaceFirst("/v1$",""); + String key=provider.nextApiKey(routeId,model,environment,keyCursors); + var response=request("POST",base+"/v1beta/models/"+encode(model)+":generateContent",key, + Map.of("contents",List.of(Map.of("role","user","parts",parts)),"generationConfig",config),"上游图片任务创建失败"); + var result=new LinkedHashMap(); + if(response.get("candidates") instanceof List candidates && !candidates.isEmpty() + && candidates.getFirst() instanceof Map candidate && candidate.get("content") instanceof Map content + && content.get("parts") instanceof List returned) { + for(Object raw:returned) if(raw instanceof Map part) { + if(part.get("text") instanceof String text) result.put("text",text); + Object inline=part.get("inlineData"); if(inline==null) inline=part.get("inline_data"); + if(inline instanceof Map data && data.get("data") instanceof String b64 && !result.containsKey("base64")) { + if(b64.length()>16*1024*1024) throw new ApiException("UPSTREAM_MEDIA_TOO_LARGE","返回图片超过保存限制",502); + result.put("base64",b64); result.put("mimeType",data.get("mimeType")==null?"image/png":data.get("mimeType")); + } + } + } + if(!result.containsKey("base64")) throw new ApiException("UPSTREAM_INVALID_MEDIA","上游未返回生成图片",502); + copyUsage(response,result); + return Map.of("_moyiInlineResult",result,"_moyiUpstreamKeyFingerprint",keyFingerprint(key)); + } + + private void copyUsage(Map response,Map result) { + for(String name:List.of("usage","usageMetadata")) if(response.get(name) instanceof Map value) result.put(name,value); + } + private String mediaUrl(Map input) { Object raw = input.get("media_url"); if (raw == null || String.valueOf(raw).isBlank()) return null; @@ -227,11 +276,12 @@ public class AsyncProviderClient { if (url == null) url = result.get("audio_url"); if (url != null) result.put("url", url); if (result.get("b64_json") != null) result.put("base64", result.get("b64_json")); + copyUsage(response,result); return result; } } Object url = response.get("url"); - if (url != null) return Map.of("url", url); + if (url != null) { var result=new LinkedHashMap(); result.put("url",url); copyUsage(response,result); return result; } return null; } diff --git a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java index 6f4e888..5ad0952 100644 --- a/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java +++ b/control-plane-java/src/main/java/com/moyi/controlplane/task/AsyncTaskService.java @@ -109,6 +109,7 @@ public class AsyncTaskService { if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404); String modelId = String.valueOf(model.get("id")); if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400); + if (Boolean.FALSE.equals(model.get("executionReady"))) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该模型的专用执行接口尚未接入", 503); if (!"provider-mock".equals(model.get("providerId")) && providerClient == null) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "异步执行器未配置", 501); // Serialize the queue-capacity check with task insertion for this // model. Without the transaction advisory lock concurrent creates @@ -123,7 +124,9 @@ public class AsyncTaskService { } UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id"))); UUID userId = catalogService.keyOwnerId(apiKeyId); - BigDecimal reservedAmount = taskCharge(model); + @SuppressWarnings("unchecked") Map frozenPricing=(Map) model.get("pricing"); + if(AsyncPriceSchedule.tokens(frozenPricing) && !"image".equals(taskType)) throw new ApiException("ASYNC_BILLING_UNSUPPORTED","该媒体类型的 Token 用量结算尚未接入",503); + BigDecimal reservedAmount = "provider-mock".equals(model.get("providerId")) ? BigDecimal.ZERO : AsyncPriceSchedule.reserve(frozenPricing,input); reserveWallet(userId, reservedAmount); // Real async providers are submitted by the worker below. Creating a // task must only reserve the wallet and enqueue the work; submitting @@ -144,6 +147,7 @@ public class AsyncTaskService { throw exception; } usage.recordTask(userId, apiKeyId, modelId, String.valueOf(model.get("providerId")), id, "queued", input, null, null, null); + if (!"provider-mock".equals(model.get("providerId"))) jdbc.update("UPDATE model_tasks SET pricing_snapshot=CAST(? AS jsonb),price_version=(SELECT max(id) FROM model_price_history WHERE model_id=?) WHERE id=?",writeJson(frozenPricing),modelId,id); catalogService.touchKey(key); return taskView(id); } @@ -295,14 +299,27 @@ public class AsyncTaskService { // crash after marking failed/succeeded leaves a hold that polling never // revisits. The conditional update also prevents duplicate settlement. inTransaction(() -> { + Map task=jdbc.queryForMap("SELECT status,pricing_snapshot::text AS pricing_snapshot FROM model_tasks WHERE id=? FOR UPDATE",taskId); + if(!List.of("queued","running").contains(task.get("status"))) return null; + AsyncPriceSchedule.Bill bill=null; + String finalCode=code,finalMessage=message; + if("succeeded".equals(status) && task.get("pricing_snapshot")!=null) { + try { bill=AsyncPriceSchedule.bill(readMap(String.valueOf(task.get("pricing_snapshot"))),result); } + catch(ApiException e) { finalCode="TASK_USAGE_PENDING"; finalMessage="生成已完成,但上游计费用量未能核对;暂不扣费,等待核账"; } + } int changed = jdbc.update( "UPDATE model_tasks SET status=?, progress=?, result=CAST(? AS jsonb), error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')", status, progress, result == null ? null : writeJson(result), - code == null ? null : writeJson(Map.of("code", code, "message", message)), now(), now(), taskId); + finalCode == null ? null : writeJson(Map.of("code", finalCode, "message", finalMessage)), now(), now(), taskId); if (changed == 0) return null; - if ("succeeded".equals(status)) settleTaskInTransaction(taskId); + if ("TASK_USAGE_PENDING".equals(finalCode)) { + releaseTaskInTransaction(taskId); + jdbc.update("UPDATE model_tasks SET cost_amount=NULL WHERE id=?",taskId); + jdbc.update("UPDATE usage_events SET cost_amount=NULL,cost_status='pending',observed_usage=CAST(? AS jsonb) WHERE provider_task_id=? AND event_kind='task'",writeJson(usageOnly(result)),taskId); + } + else if ("succeeded".equals(status)) settleTaskInTransaction(taskId,bill); else releaseTaskInTransaction(taskId); - usage.updateTask(taskId, status, result, code, message); + usage.updateTask(taskId, status, result, finalCode, finalMessage); return null; }); } @@ -407,6 +424,11 @@ public class AsyncTaskService { String status = normalizedStatus(response); int progress = progress(response, status); Map result = result(response); + if(result!=null) { + result=new LinkedHashMap<>(result); + result.putAll(usageOnly(response)); + if(response.get("data") instanceof Map nested && result.get("usage")==null && nested.get("usage") instanceof Map u) result.put("usage",u); + } if ("succeeded".equals(status) || "failed".equals(status) || "cancelled".equals(status)) { // Persistence failures must roll back and remain pollable, rather // than being mistaken for upstream failures and refunded. @@ -491,21 +513,10 @@ public class AsyncTaskService { private String writeJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException exception) { return "{}"; } } - private BigDecimal taskCharge(Map model) { - if ("provider-mock".equals(model.get("providerId"))) return BigDecimal.ZERO; - if (!(model.get("pricing") instanceof Map pricing)) throw new ApiException("MODEL_NOT_PRICED", "该异步模型尚未配置按任务计费价格", 503); - Object unitValue = pricing.get("unit"); - String unit = String.valueOf(unitValue == null ? "" : unitValue).toLowerCase(java.util.Locale.ROOT); - if (unit.contains("token")) throw new ApiException("ASYNC_BILLING_UNSUPPORTED", "异步任务不能使用 Token 价格,需配置按次、按生成或按媒体单位的固定售价", 503); - Object value = pricing.get("price"); - if (value == null) value = pricing.get("input"); - try { - BigDecimal amount = new BigDecimal(String.valueOf(value)); - if (amount.signum() < 0 || amount.scale() > 8 || amount.compareTo(new BigDecimal("1000000")) > 0) throw new NumberFormatException(); - return amount.setScale(8, RoundingMode.CEILING); - } catch (Exception exception) { - throw new ApiException("MODEL_NOT_PRICED", "该异步模型需配置有效的固定任务售价", 503); - } + private Map usageOnly(Map result) { + var selected=new LinkedHashMap(); + if(result!=null) for(String field:List.of("usage","usageMetadata")) if(result.get(field) instanceof Map value) selected.put(field,value); + return selected; } private void reserveWallet(UUID userId, BigDecimal amount) { @@ -520,20 +531,22 @@ public class AsyncTaskService { if (amount.signum() > 0) jdbc.update("UPDATE user_wallets SET reserved_balance=GREATEST(0,reserved_balance-?),updated_at=now() WHERE user_id=?", amount, userId); } - private void settleTaskInTransaction(String taskId) { + private void settleTaskInTransaction(String taskId,AsyncPriceSchedule.Bill bill) { Map task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId); - BigDecimal amount = (BigDecimal) task.get("reserved_amount"); + BigDecimal reserved = (BigDecimal) task.get("reserved_amount"); + BigDecimal amount = bill==null ? reserved : bill.amount(); policies.release(taskId); gatewayGuard.releaseTaskUserSlot((UUID)task.get("user_id"),taskId); - if (amount == null || amount.signum() == 0 || task.get("cost_amount") != null) return; + if (amount == null || task.get("cost_amount") != null) return; UUID userId = (UUID) task.get("user_id"); Map wallet = jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE", userId); BigDecimal before = (BigDecimal) wallet.get("balance"); BigDecimal after = before.subtract(amount); - jdbc.update("UPDATE user_wallets SET balance=?,reserved_balance=GREATEST(0,reserved_balance-?),updated_at=now() WHERE user_id=?", after, amount, userId); - jdbc.update("INSERT INTO wallet_entries(id,user_id,request_id,amount,balance_after,currency,reason,operation,requested_amount,balance_before,actor_user_id) VALUES (?,?,?,?,?,'CNY',?,'usage',?,?,NULL)", UUID.randomUUID(), userId, taskRequestId(taskId), amount.negate(), after, "异步模型任务 · " + taskId, amount, before); + jdbc.update("UPDATE user_wallets SET balance=?,reserved_balance=GREATEST(0,reserved_balance-?),updated_at=now() WHERE user_id=?", after, reserved, userId); + if(amount.signum()>0) jdbc.update("INSERT INTO wallet_entries(id,user_id,request_id,amount,balance_after,currency,reason,operation,requested_amount,balance_before,actor_user_id) VALUES (?,?,?,?,?,'CNY',?,'usage',?,?,NULL)", UUID.randomUUID(), userId, taskRequestId(taskId), amount.negate(), after, "异步模型任务 · " + taskId, amount, before); jdbc.update("UPDATE model_tasks SET cost_amount=? WHERE id=?", amount, taskId); jdbc.update("UPDATE usage_events SET cost_amount=? WHERE provider_task_id=? AND event_kind='task'", amount, taskId); + if(bill!=null) jdbc.update("UPDATE usage_events SET input_units=?,output_units=?,observed_usage=jsonb_build_object('input_tokens',?::bigint,'output_tokens',?::bigint,'image_input_tokens',?::bigint) WHERE provider_task_id=? AND event_kind='task'",bill.input(),bill.output(),bill.input(),bill.output(),bill.imageInput(),taskId); } private void releaseTaskInTransaction(String taskId) { diff --git a/control-plane-java/src/main/resources/db/migration/V41__async_price_snapshots.sql b/control-plane-java/src/main/resources/db/migration/V41__async_price_snapshots.sql new file mode 100644 index 0000000..90e4be5 --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V41__async_price_snapshots.sql @@ -0,0 +1,3 @@ +-- NULL snapshots identify legacy fixed-price tasks; preserve their reserved charge. +ALTER TABLE model_tasks ADD COLUMN pricing_snapshot jsonb; +ALTER TABLE model_tasks ADD COLUMN price_version bigint REFERENCES model_price_history(id); diff --git a/control-plane-java/src/main/resources/db/migration/V42__verified_media_pricing.sql b/control-plane-java/src/main/resources/db/migration/V42__verified_media_pricing.sql new file mode 100644 index 0000000..5526728 --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V42__verified_media_pricing.sql @@ -0,0 +1,22 @@ +-- Exact old-price guards preserve administrator edits; no enable-state or wallet changes. +WITH reviewed(id,upstream_model,old_price,new_price) AS (VALUES +('relay-route-6922f2ae-e466-4270-bf98-d952e3b6a69d', 'grok-imagine-image-quality', NULL, '{"unit":"CNY / image","price":0.2016,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.05,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb), +('relay-route-db04c9f4-e7ee-4235-9aed-882cdf9dad47', 'gemini-3-pro-image-preview-c', NULL, '{"unit":"CNY / image","price":0.8064,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.2,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb), +('relay-route-6cf83465-37e8-4368-a08c-16a685cf3670', 'gpt-image-2-c', NULL, '{"unit":"CNY / image","price":0.4032,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.1,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb), +('relay-route-f5455597-e007-416a-8341-420deb6f2ae8', 'gpt-image-2-pro', NULL, '{"unit":"CNY / image","price":0.8064,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.2,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb), +('relay-route-d8589945-6521-452c-9242-052a7a89aac8', 'gemini-2.5-flash-image-c', NULL, '{"unit":"CNY / image","price":0.16128,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.04,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb), +('relay-route-062b0509-5a12-4e60-8257-d6287c9b57ec', 'gemini-3.1-flash-image-preview-c', NULL, '{"unit":"CNY / image","price":0.48384,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.12,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb), +('relay-route-288a8bca-9560-432e-924b-0bd756208d24', 'qwen-image-2.0-pro', NULL, '{"unit":"CNY / image","price":0.3342924,"discount":6.5,"discountLabel":"6.5折","exchangeRate":7.2,"standardPrice":{"price":0.07143,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb), +('relay-route-26d35841-51ef-4c99-96d0-e2cff9b773d1', 'qwen-image-max', NULL, '{"unit":"CNY / image","price":0.3342924,"discount":6.5,"discountLabel":"6.5折","exchangeRate":7.2,"standardPrice":{"price":0.07143,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb), +('relay-route-d4a9d44c-b5a2-407f-92d4-5ae50067b87a', 'gemini-2.5-flash-image', '{"unit":"CNY / 1M tokens","input":1.2096,"output":120.96,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.3,"output":30},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.2096,"output":120.96,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.3,"output":30},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb), +('relay-route-2f0c9dfe-07a2-4751-a08d-6cdfb6b96d34', 'gemini-3-pro-image-preview', '{"unit":"CNY / 1M tokens","input":8.064,"output":483.84,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":2,"output":120},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":8.064,"output":483.84,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":2,"output":120},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb), +('relay-route-8ccecb3f-595f-4bb0-9147-5548303a6cda', 'gemini-3.1-flash-image', '{"unit":"CNY / 1M tokens","input":2.016,"output":241.92,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.5,"output":60},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":2.016,"output":241.92,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.5,"output":60},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb), +('relay-route-916cd7b1-cc65-40ec-bc40-541b2bd0c33d', 'gemini-3.1-flash-image-preview', '{"unit":"CNY / 1M tokens","input":2.016,"output":241.92,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.5,"output":60},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":2.016,"output":241.92,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.5,"output":60},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb), +('relay-route-5e15dbed-0d9e-44df-a491-d121c9bdd635', 'gemini-3.1-flash-lite-image', '{"unit":"CNY / 1M tokens","input":1.008,"output":120.96,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.25,"output":30},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.008,"output":120.96,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.25,"output":30},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb), +('relay-route-4886de15-b83c-4616-a74f-dfe52e0c99ba', 'gpt-image-2', '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2,"imageInput":8.0},"discountLabel":"5.6折","taskBilling":"openai-image-tokens","imageInput":32.256,"usageSource":"provider-reported-only"}'::jsonb), +('relay-route-12d63ec3-e2e5-4a5d-98c9-ac1f7628c4b2', 'gpt-image-2.5-flare', '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2,"imageInput":8.0},"discountLabel":"5.6折","taskBilling":"openai-image-tokens","imageInput":32.256,"usageSource":"provider-reported-only"}'::jsonb), +('relay-route-30eacdea-b609-47f5-80f9-0140154ddf45', 'gpt-image-2.5-sunburst', '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2,"imageInput":8.0},"discountLabel":"5.6折","taskBilling":"openai-image-tokens","imageInput":32.256,"usageSource":"provider-reported-only"}'::jsonb), +('relay-route-106ba3ca-c6a4-4d24-9f45-44fc6158b212', 'gemini-3-pro-image', '{"unit":"CNY / 1M tokens","input":8.064,"output":483.84,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":2,"output":120},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":8.064,"output":483.84,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":2,"output":120},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb)) +UPDATE model_catalog m SET pricing=r.new_price,updated_at=now() FROM reviewed r,model_providers p +WHERE m.id=r.id AND m.upstream_model=r.upstream_model AND m.provider_id=p.id AND p.code='apipro' +AND m.deleted_at IS NULL AND m.model_type='image' AND m.call_mode='async' AND m.pricing IS NOT DISTINCT FROM r.old_price; diff --git a/control-plane-java/src/main/resources/db/migration/V43__defer_requested_model_families.sql b/control-plane-java/src/main/resources/db/migration/V43__defer_requested_model_families.sql new file mode 100644 index 0000000..a40db58 --- /dev/null +++ b/control-plane-java/src/main/resources/db/migration/V43__defer_requested_model_families.sql @@ -0,0 +1,8 @@ +-- User confirmed on 2026-10-13: these unpriced families are outside current onboarding. +-- Keep catalog rows, grants and enable-state intact; admission checks the explicit flag. +UPDATE model_catalog m +SET capabilities=coalesce(m.capabilities,'{}'::jsonb)||'{"integrationDeferred":true}'::jsonb,updated_at=now() +FROM model_providers p +WHERE m.provider_id=p.id AND p.code='apipro' AND m.deleted_at IS NULL AND m.pricing IS NULL +AND (lower(m.upstream_model) ~ '^(sparkdesk|step[-/]|stepfun/|mimo-|mj_|midjourney|suno_|veo-|lyria-|wanx|wan[0-9])' + OR m.upstream_model='swap_face'); diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/gateway/DeferredModelTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/DeferredModelTest.java new file mode 100644 index 0000000..563ae35 --- /dev/null +++ b/control-plane-java/src/test/java/com/moyi/controlplane/gateway/DeferredModelTest.java @@ -0,0 +1,20 @@ +package com.moyi.controlplane.gateway; +import com.moyi.controlplane.catalog.CatalogService; +import com.moyi.controlplane.common.ApiException; +import java.util.*; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class DeferredModelTest { + @Test void deferredRouteIsRejectedByPublicIdLegacyIdAndAliasEvenAfterPricing() { + var catalog=mock(CatalogService.class); + var key=Map.of("id","test"); + var model=Map.of("id","deferred","upstreamModel","SparkDesk","integrationDeferred",true,"pricing",Map.of("input",1,"output",1,"unit","CNY / 1M tokens")); + when(catalog.listModels(false)).thenReturn(List.of(model)); + when(catalog.keyCanInvoke(key,"deferred")).thenReturn(true); + when(catalog.activeModel(anyString())).thenReturn(null); + when(catalog.activeModel("deferred")).thenReturn(model); + for(String id:List.of("deferred","SparkDesk",GatewayModelIds.publicId(model))) assertEquals("MODEL_ONBOARDING_DEFERRED",assertThrows(ApiException.class,()->GatewayModelIds.resolve(catalog,key,id)).getCode()); + } +} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncPriceScheduleTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncPriceScheduleTest.java new file mode 100644 index 0000000..9d2ade4 --- /dev/null +++ b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncPriceScheduleTest.java @@ -0,0 +1,39 @@ +package com.moyi.controlplane.task; + +import com.moyi.controlplane.common.ApiException; +import java.math.BigDecimal; +import java.util.*; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class AsyncPriceScheduleTest { + private Map image() { return new LinkedHashMap<>(Map.of("unit","CNY / 1M tokens","input",20.16,"imageInput",32.256,"output",120.96,"taskBilling","openai-image-tokens")); } + @Test void inputModalitiesUseDifferentRatesAndOnlyActualUsageIsCharged() { + var pricing=image(); + var result=Map.of("usage",Map.of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",10,"image_tokens",40))); + assertEquals(new BigDecimal("0.00753984"),AsyncPriceSchedule.bill(pricing,result).amount()); + assertTrue(AsyncPriceSchedule.reserve(pricing,Map.of("prompt","draw")).compareTo(AsyncPriceSchedule.bill(pricing,result).amount())>0); + } + @Test void missingOrInvalidUsageCannotBecomeAnEstimatedCharge() { + assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(image(),Map.of("base64","png"))); + for(Object invalid:List.of(-1,0.5,"NaN",100_000_001)) assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(image(),Map.of("usage",Map.of("input_tokens",invalid,"output_tokens",50)))); + assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(image(),Map.of("usage",Map.of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",50,"image_tokens",1))))); + } + @Test void ambiguousCachedModalitiesAreNotDoubleCounted() { + var price=image(); price.put("cachedInput",8.064); + var usage=Map.of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",10,"image_tokens",40,"cached_tokens",10)); + assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(price,Map.of("usage",usage))); + } + @Test void geminiUsageIncludesThinkingAndRequiresPromptAndCandidateCounts() { + var price=Map.of("unit","CNY / 1M tokens","input",8.064,"output",483.84,"taskBilling","gemini-image-tokens"); + var bill=AsyncPriceSchedule.bill(price,Map.of("usageMetadata",Map.of("promptTokenCount",100,"candidatesTokenCount",1000,"thoughtsTokenCount",20))); + assertEquals(new BigDecimal("0.49432320"),bill.amount()); + assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(price,Map.of("usageMetadata",Map.of("totalTokenCount",1120)))); + } + @Test void fixedCnyPricesAreSingleImageAndUnsupportedUnitsAreRejected() { + var price=Map.of("unit","CNY / image","price",0.4032); + assertEquals(new BigDecimal("0.4032"),AsyncPriceSchedule.reserve(price,Map.of("n",1))); + assertThrows(ApiException.class,()->AsyncPriceSchedule.reserve(price,Map.of("n",2))); + for(String unit:List.of("USD / task","CNY / second","CNY / 1M tokens")) assertFalse(AsyncPriceSchedule.ready(Map.of("unit",unit,"input",1,"output",1,"price",1))); + } +} diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncProviderClientTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncProviderClientTest.java index fcadbaf..69872a1 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncProviderClientTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncProviderClientTest.java @@ -24,6 +24,29 @@ import org.springframework.mock.env.MockEnvironment; import org.springframework.jdbc.core.JdbcTemplate; class AsyncProviderClientTest { + @Test void imagesPreserveUsageAndGeminiUsesItsNativeEndpoint() throws Exception { + HttpServer server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0); + var path=new AtomicReference(); + server.createContext("/",exchange->{ + path.set(exchange.getRequestURI().getPath()); + String body=path.get().contains("generateContent") + ? "{\"candidates\":[{\"content\":{\"parts\":[{\"inlineData\":{\"mimeType\":\"image/png\",\"data\":\"cG5n\"}}]}}],\"usageMetadata\":{\"promptTokenCount\":20,\"candidatesTokenCount\":100}}" + : "{\"data\":[{\"b64_json\":\"cG5n\"}],\"usage\":{\"input_tokens\":10,\"output_tokens\":50}}"; + byte[] bytes=body.getBytes(StandardCharsets.UTF_8); exchange.sendResponseHeaders(200,bytes.length); + try(var out=exchange.getResponseBody()){out.write(bytes);} + }); + server.start(); + try { + var jdbc=mock(JdbcTemplate.class); var cipher=new SecretCipher("test-encryption"); + when(jdbc.queryForMap(any(String.class),eq("relay"))).thenReturn(Map.of("code","apipro","protocol","openai-compatible","base_url","http://127.0.0.1:"+server.getAddress().getPort()+"/v1","credential_ciphertext",cipher.encrypt("test-key"))); + var client=new AsyncProviderClient(jdbc,cipher,new ObjectMapper(),mock(Environment.class)); + var image=(Map)client.submit("relay","gpt-image-2","image",Map.of("prompt","cat")).get("_moyiInlineResult"); + assertEquals("/v1/images/generations",path.get()); assertTrue(image.containsKey("usage")); + var gemini=(Map)client.submit("relay","gemini-3-pro-image-preview","image",Map.of("prompt","cat")).get("_moyiInlineResult"); + assertEquals("/v1beta/models/gemini-3-pro-image-preview:generateContent",path.get()); + assertEquals("cG5n",gemini.get("base64")); assertTrue(gemini.containsKey("usageMetadata")); + } finally { server.stop(0); } + } @Test void upstreamErrorDoesNotExposeProviderMessage() throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); diff --git a/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncTaskPostgresTest.java b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncTaskPostgresTest.java index 252e467..479a2b1 100644 --- a/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncTaskPostgresTest.java +++ b/control-plane-java/src/test/java/com/moyi/controlplane/task/AsyncTaskPostgresTest.java @@ -186,4 +186,32 @@ class AsyncTaskPostgresTest { verify(provider,never()).poll(anyString(),anyString()); assertEquals("running",status()); } + + @Test void imageTokensSettleSnapshotOnceAndReleaseUnusedReservation() throws Exception { + var pricing=Map.of("unit","CNY / 1M tokens","input",20.16,"imageInput",32.256,"output",120.96,"taskBilling","openai-image-tokens"); + jdbc.update("UPDATE model_tasks SET pricing_snapshot=CAST(? AS jsonb) WHERE id=?",json.writeValueAsString(pricing),taskId); + var result=Map.of("url","https://example.test/image.png","usage",Map.of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",10,"image_tokens",40))); + ReflectionTestUtils.invokeMethod(tasks,"finishTask",taskId,"succeeded",100,result,null,null); + ReflectionTestUtils.invokeMethod(tasks,"finishTask",taskId,"succeeded",100,result,null,null); + fail(); + assertEquals(new BigDecimal("0.00753984"),cost()); + assertEquals(new BigDecimal("9.99246016"),wallet("balance")); + assertEquals(0,wallet("reserved_balance").signum()); + assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId)); + assertEquals(50L,jdbc.queryForObject("SELECT input_units FROM usage_events WHERE provider_task_id=?",Long.class,taskId)); + } + + @Test void completedImageWithoutUsageIsReturnedAndHeldForReconciliationWithoutCharging() throws Exception { + var pricing=Map.of("unit","CNY / 1M tokens","input",1,"output",10,"taskBilling","gemini-image-tokens"); + jdbc.update("UPDATE model_tasks SET pricing_snapshot=CAST(? AS jsonb) WHERE id=?",json.writeValueAsString(pricing),taskId); + var result=Map.of("base64","png"); + ReflectionTestUtils.invokeMethod(tasks,"finishTask",taskId,"succeeded",100,result,null,null); + assertEquals("succeeded",status()); + assertNull(cost()); + assertEquals(new BigDecimal("10.00000000"),wallet("balance")); + assertEquals(0,wallet("reserved_balance").signum()); + assertEquals("pending",jdbc.queryForObject("SELECT cost_status FROM usage_events WHERE provider_task_id=?",String.class,taskId)); + assertEquals("TASK_USAGE_PENDING",jdbc.queryForObject("SELECT error_code FROM usage_events WHERE provider_task_id=?",String.class,taskId)); + assertTrue(jdbc.queryForObject("SELECT result::text FROM model_tasks WHERE id=?",String.class,taskId).contains("png")); + } } diff --git a/docs/20-media-pricing-completion.md b/docs/20-media-pricing-completion.md new file mode 100644 index 0000000..9d72aaa --- /dev/null +++ b/docs/20-media-pricing-completion.md @@ -0,0 +1,50 @@ +# 图片价格与异步用量结算(2026-10-13) + +## 本轮范围 + +用户指出目录中仍有大量“价格待配置”“异步按任务计费待配置”。核对发现:无报价、无采购折扣、异步 Token 计费未实现、媒体原生协议不匹配是不同问题。保持现有平台结构,补齐已核实的配置和结算。 + +用户确认 Spark、Step、MiMo、Midjourney、Suno、Veo/Lyria、通义万相暂不接入。V43 为相应未定价路由记录 integrationDeferred;网关拒绝调用,目录显示“暂不接入”。保留原有目录、Key 授权和启用状态,管理统计不再把它们计入本期待配置价格数量。 + +## 已核实的补价 + +V42 使用 2026-10-13 捕获的 APIPro `/api/pricing` 和已确认折扣。人民币汇率沿用 7.2,对外折数为采购折数加 1。仅匹配具体路由、渠道、型号、旧 JSON,保护管理员后续修改。 + +| 型号 | 人民币单张售价 | +|---|---:| +| grok-imagine-image-quality | 0.2016 | +| gemini-2.5-flash-image-c | 0.16128 | +| gemini-3-pro-image-preview-c | 0.8064 | +| gemini-3.1-flash-image-preview-c | 0.48384 | +| gpt-image-2-c | 0.4032 | +| gpt-image-2-pro | 0.8064 | +| qwen-image-2.0-pro | 0.3342924 | +| qwen-image-max | 0.3342924 | + +另有 9 条图片 Token 路由接通真实用量结算,原输入/输出售价不变:Gemini 2.5 Flash Image,Gemini 3 Pro Image 及 Preview,Gemini 3.1 Flash Image 及 Preview,Gemini 3.1 Flash Lite Image,GPT Image 2、2.5 Flare、2.5 Sunburst。后三条补充图像输入价 32.256 元/百万 Token,来源是渠道 input × image_ratio。文字输入仍为 20.16,输出 120.96。 + +## 结算行为 + +V41 保存创建任务时的完整价格快照和价格版本。固定人民币单张/任务/生成价格按原价结算;不把美元、秒、Token 单价当成单任务费。 + +图片 Token 请求先冻结估算额度(65536 输入 Token、32768 输出 Token,输入取最高分类价),完成后只按上游实际用量收费并释放预留。该冻结值不是消费上限;最终真实用量可能超出估算。提示词限 16384 UTF-8 字节,每任务 1 张。余额不足在提交上游前拒绝。 + +OpenAI 图片保留 usage,区分文字与图片输入;Gemini 使用 generateContent 并保留 usageMetadata。缺失用量、无效数字、输入细分不一致、不能区分的缓存用量,不估算扣款:保留成功结果、释放冻结额度、消费留空、标记 TASK_USAGE_PENDING 和成本待对账。后续核对不自动补扣用户余额。渠道未提供图像缓存分类报价时,缓存请求可能进入待核账;普通无缓存响应可结算。 + +终态、钱包扣款/释放和用量日志使用同一事务;重复轮询不会重复扣款,数据库失败保持可重试。旧任务继续按旧预留金额结算。原任务计费与现有账单不追溯修改。 + +## 尚待核实 + +不补猜测报价:APIPro 当前目录缺失的旧型号,ApiHub 动态分档报价,TokenFoundryX 缺失型号。Gemini 2.5 Flash Image Preview 渠道原价异常保留待核状态。视频 Token 路由的渠道分组对应不同提交接口;音频二进制响应缺少可核对用量。这些不能用固定任务费代替,继续限制调用。 + +配置就绪不是逐模型付费实测。本轮使用本地 HTTP 桩和隔离 PostgreSQL 验证;上线检查读取实际价格、状态和账本,不创建付费媒体任务。 + +## 来源和复现 + +- APIPro 当前报价: +- 图片接口和计费方式:、 +- Gemini 生图接口: +- 图片用量字段: +- Seedance 分组接口差异: + +`scripts/complete-media-prices.py` 离线生成 17 条有旧价保护的迁移。`scripts/test_enterprise_increment.py` 在临时本机 PostgreSQL 执行全部回归,不连接生产数据库。 diff --git a/prototype/src/services/catalog.ts b/prototype/src/services/catalog.ts index 152b71f..2b00842 100644 --- a/prototype/src/services/catalog.ts +++ b/prototype/src/services/catalog.ts @@ -36,6 +36,7 @@ export interface CatalogModel { input?: number output?: number cachedInput?: number + imageInput?: number unit?: string discount?: number discountLabel?: string @@ -48,6 +49,8 @@ export interface CatalogModel { } pricingConfigured?: boolean billingReady?: boolean + billingReason?: string + integrationDeferred?: boolean priority?: number maxConcurrency?: number executionReady?: boolean diff --git a/prototype/src/services/model-catalog.ts b/prototype/src/services/model-catalog.ts index 2c77ae2..a2d9ddd 100644 --- a/prototype/src/services/model-catalog.ts +++ b/prototype/src/services/model-catalog.ts @@ -43,6 +43,7 @@ export function summarizeModelCatalog( for (const route of routes) { if (route.status !== 'active' || !publishedModelIds.has(route.logicalModelId) || !activeProviderIds.has(route.providerId)) continue + if (route.integrationDeferred) continue if (route.pricingConfigured !== true) { unpricedRoutes++ continue diff --git a/prototype/src/services/pricing.ts b/prototype/src/services/pricing.ts index a5d8748..e51c481 100644 --- a/prototype/src/services/pricing.ts +++ b/prototype/src/services/pricing.ts @@ -3,6 +3,7 @@ export interface DisplayPricing { input?: number output?: number cachedInput?: number + imageInput?: number unit?: string discount?: number discountLabel?: string @@ -81,10 +82,12 @@ export function platformPriceLabel(pricing?: DisplayPricing) { if (!pricing) return '价格待配置' if (isTokenUnit(pricing.unit)) { const unit = pricing.unit === 'CNY / 1M tokens' ? '人民币 / 1M(一百万 Token)' : pricing.unit || '人民币 / 1M(一百万 Token)' - return `${numberLabel(pricing.input)} 输入 / ${numberLabel(pricing.output)} 输出 · ${unit}` + const image = pricing.imageInput === undefined ? '' : ` / ${numberLabel(pricing.imageInput)} 图像输入` + return `${numberLabel(pricing.input)} ${image ? '文字输入' : '输入'}${image} / ${numberLabel(pricing.output)} 输出 · ${unit}` } const price = pricing.price ?? pricing.input - return price === undefined ? `计费单位:${unitLabel(pricing.unit)}` : `平台售价 ${numberLabel(price)} / ${unitSuffix(pricing)}` + const suffix = ({ image: '张(单张请求)', task: '任务', generation: '次生成' } as Record)[unitSuffix(pricing)] || unitSuffix(pricing) + return price === undefined ? `计费单位:${unitLabel(pricing.unit)}` : `平台售价 ${numberLabel(price)} / ${suffix}` } /** Explicit admin action. Settlement still uses saved CNY rates, never applies a second discount. */ diff --git a/prototype/src/types.ts b/prototype/src/types.ts index a21fae7..4171cc3 100644 --- a/prototype/src/types.ts +++ b/prototype/src/types.ts @@ -44,6 +44,7 @@ export interface ModelItem { input?: number output?: number cachedInput?: number + imageInput?: number unit?: string discount?: number discountLabel?: string @@ -55,6 +56,8 @@ export interface ModelItem { } pricingConfigured?: boolean billingReady?: boolean + billingReason?: string + integrationDeferred?: boolean priority?: number maxConcurrency?: number executionReady?: boolean diff --git a/prototype/src/views/UserConsole.vue b/prototype/src/views/UserConsole.vue index 537bced..f19a26e 100644 --- a/prototype/src/views/UserConsole.vue +++ b/prototype/src/views/UserConsole.vue @@ -348,15 +348,17 @@ function hasCallableGroup(model: GalleryModel) { function modelAvailabilityLabel(model: GalleryModel) { if (!model.groups.length) return '待上线' + if (model.groups.every(group => group.integrationDeferred)) return '暂不接入' if (hasCallableGroup(model)) return '在线' if (model.mode === '异步' && model.groups.some(group => group.pricingConfigured && group.executionReady === false)) return '异步执行器待接入' - if (model.mode === '异步' && model.groups.some(group => group.pricingConfigured && group.billingReady === false)) return '异步按任务计费待配置' + if (model.mode === '异步' && model.groups.some(group => group.pricingConfigured && group.billingReady === false)) return '用量计费待核实' return '价格待配置' } function modelUnavailableLabel(model: GalleryModel) { + if (model.groups.length && model.groups.every(group => group.integrationDeferred)) return '暂不接入' if (model.mode === '异步' && model.groups.some(group => group.pricingConfigured && group.executionReady === false)) return '异步执行器待接入' - if (model.mode === '异步' && model.groups.some(group => group.pricingConfigured && group.billingReady === false)) return '异步按任务计费待配置' + if (model.mode === '异步' && model.groups.some(group => group.pricingConfigured && group.billingReady === false)) return '用量计费待核实' return '价格待配置' } @@ -617,7 +619,7 @@ function mapCatalogModels(items: unknown, providers: unknown): ModelItem[] { ...mapCatalogModel(item), logicalModelId: item.logicalModelId, gatewayModelId: item.gatewayModelId, provider: providerNames.get(providerId) || providerId || '未知渠道', vendorName: model.vendorName, - sourceLabel: model.sourceLabel, pricing: model.pricing, pricingConfigured: model.pricingConfigured, billingReady: model.billingReady, priority: model.priority, maxConcurrency: model.maxConcurrency, executionReady: model.executionReady, inputTypes, outputTypes, + sourceLabel: model.sourceLabel, pricing: model.pricing, pricingConfigured: model.pricingConfigured, billingReady: model.billingReady, billingReason: model.billingReason, integrationDeferred: model.integrationDeferred, priority: model.priority, maxConcurrency: model.maxConcurrency, executionReady: model.executionReady, inputTypes, outputTypes, } }) } @@ -975,7 +977,8 @@ $env:OPENAI_API_KEY="你的 MOYI 用户 API Key" 标准价:{{ groupStandardPriceLabel(group) }} · {{ groupPriceLabel(group) }} · 对外 {{ groupDiscountLabel(group) }} - +

{{ group.billingReason }}

+ diff --git a/prototype/tests/model-catalog.test.mjs b/prototype/tests/model-catalog.test.mjs index 32b230a..6737204 100644 --- a/prototype/tests/model-catalog.test.mjs +++ b/prototype/tests/model-catalog.test.mjs @@ -31,6 +31,7 @@ test('catalog summary distinguishes published models, ready models, ready routes route('hidden', 'hidden'), route('hidden-unpriced', 'hidden', { pricingConfigured: false }), route('orphan-definition', 'missing'), + route('deferred', 'pricing-only', { integrationDeferred: true, pricingConfigured: false }), ] assert.deepEqual(summarizeModelCatalog(definitions, routes, providers), { diff --git a/prototype/tests/pricing.test.mjs b/prototype/tests/pricing.test.mjs index ed3f6ea..c90cef0 100644 --- a/prototype/tests/pricing.test.mjs +++ b/prototype/tests/pricing.test.mjs @@ -6,10 +6,15 @@ test('fixed-unit pricing shows platform task price without inventing a standard const tripo = { price: 1.44, unit: 'CNY / task', upstreamCredits: 20, exchangeRate: 7.2, priceSource: 'Tripo 官方', standardPrice: { price: 0.2, unit: 'USD / task', credits: 20 } } assert.equal(standardPriceLabel(tripo), '$0.2 / task') assert.equal(modelStandardPriceLabel([tripo]), '$0.2 / task') - assert.equal(platformPriceLabel(tripo), '平台售价 ¥1.44 / task') + assert.equal(platformPriceLabel(tripo), '平台售价 ¥1.44 / 任务') assert.equal(groupDiscountLabel(tripo), '折扣未提供') }) +test('image prices distinguish per-image and text/image token inputs', () => { + assert.equal(platformPriceLabel({price:0.4032,unit:'CNY / image'}),'平台售价 ¥0.4032 / 张(单张请求)') + assert.equal(platformPriceLabel({input:20.16,imageInput:32.256,output:120.96,unit:'CNY / 1M tokens'}),'¥20.16 文字输入 / ¥32.256 图像输入 / ¥120.96 输出 · 人民币 / 1M(一百万 Token)') +}) + test('official standard price source is displayed, while a standard-price marker is not treated as a discount', () => { const tripo = { price: 1.44, unit: 'CNY / task', discountLabel: '标准价', standardPrice: { price: 0.2, unit: 'USD / task', credits: 20, priceSource: 'Tripo 官方 API 定价' } } assert.equal(standardPriceLabel(tripo), '$0.2 / task · Tripo 官方 API 定价') diff --git a/scripts/complete-media-prices.py b/scripts/complete-media-prices.py new file mode 100644 index 0000000..5fbe05c --- /dev/null +++ b/scripts/complete-media-prices.py @@ -0,0 +1,81 @@ +"""Generate guarded media price changes from the captured APIPro quote (no network).""" +import argparse +import json +from decimal import Decimal, ROUND_HALF_UP +from pathlib import Path + +FIXED = { + 'grok-imagine-image-quality': '5.6', + 'gemini-2.5-flash-image-c': '5.6', + 'gemini-3-pro-image-preview-c': '5.6', + 'gemini-3.1-flash-image-preview-c': '5.6', + 'gpt-image-2-c': '5.6', 'gpt-image-2-pro': '5.6', + 'qwen-image-2.0-pro': '6.5', 'qwen-image-max': '6.5', +} +TOKENS = { + 'gemini-2.5-flash-image', 'gemini-3-pro-image', 'gemini-3-pro-image-preview', + 'gemini-3.1-flash-image', 'gemini-3.1-flash-image-preview', 'gemini-3.1-flash-lite-image', + 'gpt-image-2', 'gpt-image-2.5-flare', 'gpt-image-2.5-sunburst', +} + +def number(value): + return float(Decimal(str(value)).quantize(Decimal('.00000001'), rounding=ROUND_HALF_UP)) + +def changes(catalog, source): + quotes = {row['model_name']: row for row in source['data']} + result = [] + for model in catalog: + name = model['model'] + if model['provider'] != 'apipro' or model['deleted'] or model['type'] != 'image' or model['mode'] != 'async': + continue + quote = quotes.get(name) + if name in FIXED and model['pricing'] is None: + assert quote and quote['quota_type'] == 1 and quote['model_price'] > 0 + discount = Decimal(FIXED[name]) + price = {'unit': 'CNY / image', 'price': number(Decimal(str(quote['model_price'])) * Decimal('7.2') * discount / 10), + 'discount': float(discount), 'discountLabel': f'{discount}折', 'exchangeRate': 7.2, + 'standardPrice': {'price': quote['model_price'], 'unit': 'USD / image'}, + 'priceSource': 'APIPro 模型广场 2026-10-13,单张请求'} + elif name in TOKENS and model['pricing']: + assert quote and quote['quota_type'] == 0 + price = dict(model['pricing']) + usd_input = Decimal(str(quote['model_ratio'])) * 2 + usd_output = usd_input * Decimal(str(quote['completion_ratio'])) + # Do not activate a route if a newer quote conflicts with its stored rate. + for field, raw in [('input', usd_input), ('output', usd_output)]: + assert number(price[field]) == number(raw * Decimal('7.2') * Decimal('.56')), name + price['taskBilling'] = 'gemini-image-tokens' if name.startswith('gemini') else 'openai-image-tokens' + if quote.get('image_ratio') is not None: + price['imageInput'] = number(usd_input * Decimal(str(quote['image_ratio'])) * Decimal('7.2') * Decimal('.56')) + price['listPriceUsd'] = dict(price['listPriceUsd'], imageInput=number(usd_input * Decimal(str(quote['image_ratio'])))) + price['usageSource'] = 'provider-reported-only' + else: + continue + result.append({'id': model['id'], 'model': name, 'before': model['pricing'], 'after': price}) + assert len(result) == len(FIXED) + len(TOKENS), f'Expected 17 exact routes, got {len(result)}' + return result + +def main(): + args = argparse.ArgumentParser() + args.add_argument('--sources', type=Path, required=True) + args.add_argument('--migration', type=Path, required=True) + opt = args.parse_args() + plan = changes(json.loads((opt.sources/'catalog.json').read_text(encoding='utf-8')), + json.loads((opt.sources/'apipro.json').read_text(encoding='utf-8'))) + def literal(value): + return "'" + json.dumps(value, ensure_ascii=False, separators=(',', ':')).replace("'", "''") + "'::jsonb" + rows = [] + for row in plan: + old = 'NULL' if row['before'] is None else literal(row['before']) + rows.append(f"('{row['id']}', '{row['model']}', {old}, {literal(row['after'])})") + sql = "-- Exact old-price guards preserve administrator edits; no enable-state or wallet changes.\n" + sql += "WITH reviewed(id,upstream_model,old_price,new_price) AS (VALUES\n" + ',\n'.join(rows) + ")\n" + sql += "UPDATE model_catalog m SET pricing=r.new_price,updated_at=now() FROM reviewed r,model_providers p\n" + sql += "WHERE m.id=r.id AND m.upstream_model=r.upstream_model AND m.provider_id=p.id AND p.code='apipro'\n" + sql += "AND m.deleted_at IS NULL AND m.model_type='image' AND m.call_mode='async' AND m.pricing IS NOT DISTINCT FROM r.old_price;\n" + opt.migration.write_text(sql, encoding='utf-8') + (opt.sources/'media-price-changes.json').write_text(json.dumps(plan, ensure_ascii=False, indent=2), encoding='utf-8') + print(f'Prepared {len(plan)} guarded changes (8 missing prices, 9 usage billing configurations).') + +if __name__ == '__main__': + main() diff --git a/scripts/package-deployment.ps1 b/scripts/package-deployment.ps1 index ff0c73d..f53a13e 100644 --- a/scripts/package-deployment.ps1 +++ b/scripts/package-deployment.ps1 @@ -30,7 +30,7 @@ New-Item -ItemType Directory -Force -Path $artifactRoot | Out-Null $staging = Join-Path $artifactRoot ('moyi-test-' + $releaseId) New-Item -ItemType Directory -Path $staging | Out-Null # Copy tracked deployment files only, never .env, certificates or .git. -$tracked = @(git ls-files infra/moyi scripts/live-gateway-smoke.py docs/11-server-test-deployment.md docs/13-model-groups-registration-credit.md docs/14-lifecycle-usage-credit-adjustments.md docs/15-relay-onboarding.md docs/16-streaming-chat-billing.md docs/17-catalog-and-key-selection.md docs/18-pricing-currency-and-discounts.md docs/19-enterprise-gateway-increment.md) +$tracked = @(git ls-files infra/moyi scripts/live-gateway-smoke.py docs/11-server-test-deployment.md docs/13-model-groups-registration-credit.md docs/14-lifecycle-usage-credit-adjustments.md docs/15-relay-onboarding.md docs/16-streaming-chat-billing.md docs/17-catalog-and-key-selection.md docs/18-pricing-currency-and-discounts.md docs/19-enterprise-gateway-increment.md docs/20-media-pricing-completion.md) foreach ($relative in $tracked) { if ($relative -match '(^|/)(\.env\.cloud|\.deploy-mode|certs)(/|$)') { throw 'Sensitive path in release allowlist.' } $destination = Join-Path $staging $relative