feat: add audited async task reconciliation and frozen bindings
This commit is contained in:
1 parent
5be06a97c1
commit
c716ec7478
28 files changed
+1312
-76
No files matched your search
+64
@@ -0,0 +1,64 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.URI;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/** Only normalized assets and numeric usage may enter a manual completion, never raw provider payloads. */
|
||||
final class TaskReconciliationResult {
|
||||
private static final Set<String> ASSETS = Set.of("url", "modelUrl", "videoUrl", "imageUrl", "audioUrl", "previewUrl");
|
||||
private static final Set<String> OPENAI = Set.of("input_tokens", "output_tokens", "total_tokens", "cached_tokens", "input_tokens_details");
|
||||
private static final Set<String> DETAILS = Set.of("text_tokens", "image_tokens", "cached_tokens", "cached_tokens_details");
|
||||
private static final Set<String> CACHED = Set.of("text_tokens", "image_tokens");
|
||||
private static final Set<String> GEMINI = Set.of("promptTokenCount", "candidatesTokenCount", "thoughtsTokenCount", "cachedContentTokenCount", "totalTokenCount");
|
||||
|
||||
static Map<String,Object> normalize(Map<String,Object> input, ObjectMapper json) {
|
||||
if (input == null) throw invalid();
|
||||
try { if (json.writeValueAsBytes(input).length > 32_768) throw invalid(); }
|
||||
catch (JsonProcessingException exception) { throw invalid(); }
|
||||
var result = new LinkedHashMap<String,Object>();
|
||||
for (var entry : input.entrySet()) {
|
||||
String field = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
if (ASSETS.contains(field)) {
|
||||
if (!(value instanceof String text) || text.length() > 4096 || text.chars().anyMatch(Character::isISOControl)) throw invalid();
|
||||
try {
|
||||
URI uri = URI.create(text);
|
||||
if (!("https".equalsIgnoreCase(uri.getScheme()) || "http".equalsIgnoreCase(uri.getScheme())) || uri.getHost() == null || uri.getUserInfo() != null) throw invalid();
|
||||
} catch (IllegalArgumentException exception) { throw invalid(); }
|
||||
result.put(field, text);
|
||||
} else if (Set.of("format", "mimeType").contains(field)) {
|
||||
if (!(value instanceof String text) || text.isBlank() || text.length() > 128 || text.chars().anyMatch(Character::isISOControl)) throw invalid();
|
||||
result.put(field, text);
|
||||
} else if ("usage".equals(field)) result.put(field, numericMap(value, OPENAI));
|
||||
else if ("usageMetadata".equals(field)) result.put(field, numericMap(value, GEMINI));
|
||||
else throw invalid();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Map<String,Object> numericMap(Object value, Set<String> allowed) {
|
||||
if (!(value instanceof Map<?,?> map)) throw invalid();
|
||||
var result = new LinkedHashMap<String,Object>();
|
||||
for (var entry : map.entrySet()) {
|
||||
if (!(entry.getKey() instanceof String name) || !allowed.contains(name)) throw invalid();
|
||||
if ("input_tokens_details".equals(name)) result.put(name, numericMap(entry.getValue(), DETAILS));
|
||||
else if ("cached_tokens_details".equals(name)) result.put(name, numericMap(entry.getValue(), CACHED));
|
||||
else {
|
||||
try {
|
||||
long count = new BigDecimal(String.valueOf(entry.getValue())).longValueExact();
|
||||
if (count < 0 || count > 100_000_000) throw invalid();
|
||||
result.put(name, count);
|
||||
} catch (NumberFormatException | ArithmeticException exception) { throw invalid(); }
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ApiException invalid() { return new ApiException("INVALID_RECONCILIATION_RESULT", "只接受有界的 HTTP(S) 结果地址和规范化数值用量,不接受原始上游响应或密钥", 422); }
|
||||
}
|
||||
Reference in new issue
Block a user