package run.webup.platform; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.node.*; import java.math.*; import java.util.*; import org.springframework.stereotype.Service; @Service public class ModelGateway { public record Call(String id, String name, JsonNode arguments, String argumentError) { public Call(String id, String name, JsonNode arguments) { this(id, name, arguments, null); } } private Call parseCall(String id, String name, JsonNode supplied, boolean encoded) { JsonNode arguments = supplied; try { if (encoded && supplied.isTextual()) arguments = json.reader() .with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) .readTree(supplied.asText()); if (arguments == null || !arguments.isObject()) throw new IllegalArgumentException(); if (!Set.of("list_files", "read_file", "write_file", "delete_file", "run_command") .contains(name)) return new Call(id, name, arguments, "Unknown tool. Use one of the provided tools."); String required = name.equals("run_command") ? "command" : "path"; if (!arguments.path(required).isTextual() || (name.equals("write_file") && !arguments.path("content").isTextual())) throw new IllegalArgumentException(); return new Call(id, name, arguments); } catch (Exception invalid) { return new Call( id, name, NullNode.instance, "Invalid tool arguments. Return a complete JSON object matching the tool schema; split" + " large files into smaller operations."); } } public record Usage( long input, long output, long cacheRead, long cacheWrite, boolean exact, JsonNode raw) {} public record Result( ObjectNode assistant, List calls, Usage usage, String requestId, String stopReason) { public Result(ObjectNode assistant, List calls, Usage usage, String requestId) { this(assistant, calls, usage, requestId, "stop"); } } private final ObjectMapper json; private final ProviderTransport transport; public ModelGateway(ObjectMapper json, ProviderTransport transport) { this.json = json; this.transport = transport; } public ArrayNode tools() { ArrayNode tools = json.createArrayNode(); for (String name : List.of("list_files", "read_file", "write_file", "delete_file", "run_command")) { ObjectNode fn = json.createObjectNode(); fn.put("name", name); fn.put( "description", switch (name) { case "list_files" -> "List files under the project workspace"; case "read_file" -> "Read a UTF-8 project file"; case "write_file" -> "Create or replace a UTF-8 project file"; case "delete_file" -> "Delete a project file"; default -> "Run a shell command in the isolated project sandbox with timeout and resource" + " limits"; }); ObjectNode schema = fn.putObject("parameters"); schema.put("type", "object"); ObjectNode props = schema.putObject("properties"); String required = name.equals("run_command") ? "command" : "path"; props.putObject(required).put("type", "string"); schema.putArray("required").add(required); if (name.equals("write_file")) { props.putObject("content").put("type", "string"); ((ArrayNode) schema.get("required")).add("content"); } schema.put("additionalProperties", false); ObjectNode tool = json.createObjectNode(); tool.put("type", "function"); tool.set("function", fn); tools.add(tool); } return tools; } private void addMerged(ArrayNode list, ObjectNode message, String field) { if (!list.isEmpty() && list.get(list.size() - 1).path("role").equals(message.path("role"))) { ((ArrayNode) list.get(list.size() - 1).get(field)).addAll((ArrayNode) message.get(field)); } else list.add(message); } public ObjectNode request( String type, String model, int maxOutput, List history, boolean withTools) { ObjectNode body = json.createObjectNode(); ArrayNode definitions = tools(); if (type.equals("OPENAI")) { body.put("model", model); body.put("max_tokens", maxOutput); ArrayNode messages = body.putArray("messages"); for (JsonNode m : history) { ObjectNode copy = ((ObjectNode) m).deepCopy(); copy.remove(List.of("native", "nativeType", "is_error")); messages.add(copy); } if (withTools) body.set("tools", definitions); return body; } if (type.equals("ANTHROPIC")) { body.put("model", model); body.put("max_tokens", maxOutput); ArrayNode messages = body.putArray("messages"); for (JsonNode m : history) { String role = m.path("role").asText(); if (role.equals("system")) { body.put("system", m.path("content").asText()); continue; } ObjectNode converted = json.createObjectNode(); converted.put("role", role.equals("tool") ? "user" : role); ArrayNode content = converted.putArray("content"); if (role.equals("tool")) { ObjectNode block = content.addObject(); block.put("type", "tool_result"); block.put("tool_use_id", m.path("tool_call_id").asText()); block.put("content", m.path("content").asText()); if (m.path("is_error").asBoolean(false)) block.put("is_error", true); } else if (role.equals("assistant") && m.path("nativeType").asText().equals(type)) { content.addAll((ArrayNode) m.get("native")); } else { if (!m.path("content").asText().isEmpty()) { content.addObject().put("type", "text").put("text", m.path("content").asText()); } for (JsonNode tc : m.path("tool_calls")) { ObjectNode b = content.addObject(); b.put("type", "tool_use"); b.put("id", tc.path("id").asText()); b.put("name", tc.path("function").path("name").asText()); try { b.set("input", json.readTree(tc.path("function").path("arguments").asText())); } catch (Exception e) { throw new ApiError.Failure(502, "历史工具参数无效"); } } } addMerged(messages, converted, "content"); } if (withTools) { ArrayNode ts = body.putArray("tools"); for (JsonNode t : definitions) { ObjectNode fn = ((ObjectNode) t.get("function")).deepCopy(); fn.set("input_schema", fn.remove("parameters")); ts.add(fn); } } return body; } ArrayNode contents = body.putArray("contents"); body.putObject("generationConfig").put("maxOutputTokens", maxOutput); for (JsonNode m : history) { String role = m.path("role").asText(); if (role.equals("system")) { body.putObject("systemInstruction") .putArray("parts") .addObject() .put("text", m.path("content").asText()); continue; } ObjectNode converted = json.createObjectNode(); converted.put("role", role.equals("assistant") ? "model" : "user"); ArrayNode parts = converted.putArray("parts"); if (role.equals("tool")) { ObjectNode f = parts.addObject().putObject("functionResponse"); f.put("id", m.path("tool_call_id").asText()); f.put("name", m.path("name").asText()); f.putObject("response").put("output", m.path("content").asText()); } else if (role.equals("assistant") && m.path("nativeType").asText().equals(type)) { parts.addAll((ArrayNode) m.get("native")); } else { if (!m.path("content").asText().isEmpty()) parts.addObject().put("text", m.path("content").asText()); for (JsonNode tc : m.path("tool_calls")) { ObjectNode f = parts.addObject().putObject("functionCall"); f.put("name", tc.path("function").path("name").asText()); try { f.set("args", json.readTree(tc.path("function").path("arguments").asText())); } catch (Exception e) { throw new ApiError.Failure(502, "历史工具参数无效"); } } } addMerged(contents, converted, "parts"); } if (withTools) { ArrayNode fns = body.putArray("tools").addObject().putArray("functionDeclarations"); for (JsonNode t : definitions) fns.add(t.get("function")); } return body; } public Result call( String type, String base, String key, String model, int maxOutput, int timeout, List history) { return call(type, base, key, model, maxOutput, timeout, history, "BEARER"); } public Result call( String type, String base, String key, String model, int maxOutput, int timeout, List history, String authMode) { String path = switch (type) { case "ANTHROPIC" -> "/messages"; case "GEMINI" -> "/models/" + model + ":generateContent"; default -> "/chat/completions"; }; ObjectNode payload = executionRequest(type, base, model, maxOutput, history, true); JsonNode response = "BEARER".equals(authMode) || !"OPENAI".equals(type) ? transport.request(base, path, key, type, timeout, payload) : transport.requestAuthenticated(base, path, key, type, timeout, payload, authMode); return parse(type, response); } public Result audit( String type, String base, String key, String model, int timeout, List history, String authMode) { return audit(type, base, key, model, 1536, timeout, history, authMode); } public Result audit( String type, String base, String key, String model, int maxOutput, int timeout, List history, String authMode) { String path = switch (type) { case "ANTHROPIC" -> "/messages"; case "GEMINI" -> "/models/" + model + ":generateContent"; default -> "/chat/completions"; }; var payload = executionRequest(type, base, model, maxOutput, history, false); var response = "BEARER".equals(authMode) || !"OPENAI".equals(type) ? transport.request(base, path, key, type, Math.min(timeout, 45), payload) : transport.requestAuthenticated( base, path, key, type, Math.min(timeout, 45), payload, authMode); return parse(type, response); } private ObjectNode executionRequest( String type, String base, String model, int maxOutput, List history, boolean withTools) { var payload = request(type, model, maxOutput, history, withTools); // DeepSeek currently enables thinking by default. Small output budgets can be // consumed entirely by reasoning, leaving truncated tools or no final report. if (type.equals("OPENAI") && maxOutput <= 8192 && "api.deepseek.com".equalsIgnoreCase(java.net.URI.create(base).getHost()) && Set.of("deepseek-flash", "deepseek-v4-pro").contains(model)) payload.putObject("thinking").put("type", "disabled"); return payload; } public Result parse(String type, JsonNode response) { ObjectNode assistant = json.createObjectNode(); assistant.put("role", "assistant"); StringBuilder text = new StringBuilder(); List calls = new ArrayList<>(); JsonNode usage; long input, output, read, write; boolean exact; if (type.equals("OPENAI")) { JsonNode m = response.path("choices").path(0).path("message"); if (m.isMissingNode()) throw new ApiError.Failure(502, "模型未返回有效消息"); assistant = (ObjectNode) m.deepCopy(); for (JsonNode tc : assistant.path("tool_calls")) { JsonNode supplied = tc.path("function").path("arguments"); Call call = parseCall( tc.path("id").asText(), tc.path("function").path("name").asText(), supplied, true); calls.add(call); // Some compatible relays return an object; normalize outbound history to the wire format. if (supplied.isObject()) ((ObjectNode) tc.get("function")).put("arguments", supplied.toString()); } usage = response.path("usage"); read = usage.path("prompt_tokens_details").path("cached_tokens").asLong(0); write = usage.path("prompt_tokens_details").path("cache_write_tokens").asLong(0); input = usage.path("prompt_tokens").asLong(0) - read - write; output = usage.path("completion_tokens").asLong(0); exact = usage.has("prompt_tokens") && usage.has("completion_tokens"); } else { JsonNode blocks = type.equals("ANTHROPIC") ? response.path("content") : response.path("candidates").path(0).path("content").path("parts"); if (!blocks.isArray()) throw new ApiError.Failure(502, "模型未返回有效消息"); assistant.set("native", blocks); assistant.put("nativeType", type); for (JsonNode b : blocks) { if (type.equals("ANTHROPIC") ? b.path("type").asText().equals("tool_use") : b.has("functionCall")) { JsonNode f = type.equals("ANTHROPIC") ? b : b.get("functionCall"); calls.add( parseCall( f.path("id").asText(UUID.randomUUID().toString()), f.path("name").asText(), f.path(type.equals("ANTHROPIC") ? "input" : "args"), false)); } else if (b.has("text") && !b.path("thought").asBoolean(false)) text.append(b.path("text").asText()); } assistant.put("content", text.toString()); ArrayNode tcs = assistant.putArray("tool_calls"); for (Call c : calls) { ObjectNode tc = tcs.addObject(); tc.put("id", c.id()); tc.put("type", "function"); tc.putObject("function").put("name", c.name()).put("arguments", c.arguments().toString()); } if (calls.isEmpty()) assistant.remove("tool_calls"); if (type.equals("ANTHROPIC")) { usage = response.path("usage"); input = usage.path("input_tokens").asLong(0); output = usage.path("output_tokens").asLong(0); read = usage.path("cache_read_input_tokens").asLong(0); write = usage.path("cache_creation_input_tokens").asLong(0); exact = usage.has("input_tokens") && usage.has("output_tokens"); } else { usage = response.path("usageMetadata"); read = usage.path("cachedContentTokenCount").asLong(0); write = 0; input = usage.path("promptTokenCount").asLong(0) - read; output = usage.path("candidatesTokenCount").asLong(0) + usage.path("thoughtsTokenCount").asLong(0); exact = usage.has("promptTokenCount") && usage.has("candidatesTokenCount"); } } if (input < 0 || output < 0 || read < 0 || write < 0) throw new ApiError.Failure(502, "供应商 usage 计数无效"); String stopReason = switch (type) { case "ANTHROPIC" -> response.path("stop_reason").asText("end_turn"); case "GEMINI" -> response.path("candidates").path(0).path("finishReason").asText("STOP"); default -> response.path("choices").path(0).path("finish_reason").asText("stop"); }; return new Result( assistant, calls, new Usage(input, output, read, write, exact, usage), response .path("id") .asText(response.path("responseId").asText(UUID.randomUUID().toString())), stopReason); } public static BigDecimal cost(Usage u, Map price) { return BigDecimal.valueOf(u.input()) .multiply((BigDecimal) price.get("input_price")) .add(BigDecimal.valueOf(u.output()).multiply((BigDecimal) price.get("output_price"))) .add(BigDecimal.valueOf(u.cacheRead()).multiply((BigDecimal) price.get("cache_read_price"))) .add( BigDecimal.valueOf(u.cacheWrite()) .multiply((BigDecimal) price.get("cache_write_price"))) .divide(BigDecimal.valueOf(1000000), 8, RoundingMode.UP); } }