fix: classify upstream failures and prevent empty billed responses

This commit is contained in:
wlx committed 2026-09-28 04:55:00 +08:00
1 parent b455d8d79f
commit 9acc69d4c7
28 files changed
+1449 -131

No files matched your search

@@ -162,9 +162,44 @@ public class GatewayController {
private int elapsed(long started) { return (int)Math.min(Integer.MAX_VALUE,System.currentTimeMillis()-started); }
private void requireCompletion(Map<String,Object> body) {
if(body.containsKey("error") || !(body.get("choices") instanceof List<?> choices) || choices.isEmpty()
|| !(choices.getFirst() instanceof Map<?,?> choice) || choice.get("finish_reason")==null)
|| !(choices.getFirst() instanceof Map<?,?> choice) || choice.get("finish_reason")==null || !hasAssistantOutput(choice.get("message")))
throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游未返回完整的聊天结果,本次未扣费",502);
}
/**
* Reasoning models may finish with an empty final content field while
* returning reasoning_content. That is a valid, billable upstream result
* and the UI renders the reasoning separately. A response with neither
* content, reasoning, nor tool calls is a genuine empty response and must
* not be settled as a successful paid call.
*/
private boolean hasAssistantOutput(Object rawMessage) {
if (!(rawMessage instanceof Map<?,?> message)) return false;
if (hasAssistantValue(message.get("content"))) return true;
for (String field : List.of("reasoning_content", "reasoning", "thinking")) {
if (hasText(message.get(field))) return true;
}
if (message.get("tool_calls") instanceof List<?> calls) {
return calls.stream().anyMatch(call -> call instanceof Map<?,?> item && !item.isEmpty());
}
return false;
}
private boolean hasAssistantValue(Object value) {
if (hasText(value)) return true;
if (!(value instanceof List<?> parts)) return false;
return parts.stream().anyMatch(part -> {
if (!(part instanceof Map<?,?> map)) return hasText(part);
for (String field : List.of("text", "content", "thinking", "reasoning", "reasoning_content")) {
if (hasText(map.get(field))) return true;
}
return false;
});
}
private boolean hasText(Object value) {
return value instanceof String text && !text.isBlank();
}
private Map<String,Object> mockResponse(String modelId,Map<String,Object> input) {
String prompt = extractPrompt(input.get("messages"));
String content = "MOYI Java Mock 已收到:" + (prompt.isBlank() ? "(空消息)" : prompt);