feat: add EIP4 agent CLI and skills plan

This commit is contained in:
2026-06-17 07:47:22 +08:00
parent 3b4d3dbd24
commit 5bf641838a
15 changed files with 935 additions and 0 deletions

View File

@@ -0,0 +1,328 @@
package com.eip4.platform.cli;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public final class Eip4Cli {
private static final String DEFAULT_BASE_URL = "http://localhost:8084";
private final ObjectMapper objectMapper;
private final Eip4HttpClient httpClient;
public Eip4Cli(Eip4HttpClient httpClient) {
this.objectMapper = new ObjectMapper();
this.httpClient = httpClient;
}
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
int code = new Eip4Cli(new JdkEip4HttpClient(objectMapper)).run(args, System.out, System.err);
if (code != 0) {
System.exit(code);
}
}
int run(String[] args, PrintStream out, PrintStream err) {
try {
if (args.length == 0 || isHelp(args[0])) {
printHelp(out);
return 0;
}
return switch (args[0]) {
case "env" -> runEnv(args, out, err);
case "ai" -> runAi(args, out, err);
default -> fail(err, "Unknown command: " + args[0]);
};
} catch (IllegalArgumentException ex) {
return fail(err, ex.getMessage());
} catch (IOException ex) {
return fail(err, "I/O failed: " + ex.getMessage());
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
return fail(err, "Interrupted");
}
}
private int runEnv(String[] args, PrintStream out, PrintStream err) throws IOException, InterruptedException {
if (args.length < 2 || isHelp(args[1])) {
printEnvHelp(out);
return 0;
}
if (!"status".equals(args[1])) {
return fail(err, "Unknown env command: " + args[1]);
}
Options options = Options.parse(args, 2);
String baseUrl = baseUrl(options);
URI uri = endpoint(baseUrl, "/v3/api-docs");
Eip4HttpResponse response = httpClient.send("GET", uri, null);
if (!response.successful()) {
err.println("EIP4 env NOT_READY");
err.println("baseUrl: " + baseUrl);
err.println("status: " + response.statusCode());
return 2;
}
out.println("EIP4 env READY");
out.println("baseUrl: " + baseUrl);
out.println("apiDocs: " + uri);
return 0;
}
private int runAi(String[] args, PrintStream out, PrintStream err) throws IOException, InterruptedException {
if (args.length < 2 || isHelp(args[1])) {
printAiHelp(out);
return 0;
}
if (!"auto-develop".equals(args[1])) {
return fail(err, "Unknown ai command: " + args[1]);
}
Options options = Options.parse(args, 2);
ObjectNode request = buildAutoDevelopRequest(options);
String baseUrl = baseUrl(options);
Eip4HttpResponse response = httpClient.send(
"POST",
endpoint(baseUrl, "/api/ai/features/auto-develop"),
request);
if (!response.successful()) {
err.println("AI auto-develop failed");
err.println("status: " + response.statusCode());
err.println(response.body());
return 2;
}
JsonNode body = response.body().isBlank()
? objectMapper.createObjectNode()
: objectMapper.readTree(response.body());
String formatted = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(body);
String output = options.single("output");
if (output == null) {
out.println(formatted);
} else {
Path outputPath = Path.of(output);
if (outputPath.getParent() != null) {
Files.createDirectories(outputPath.getParent());
}
Files.writeString(outputPath, formatted + System.lineSeparator(), StandardCharsets.UTF_8);
out.println("Wrote run result: " + outputPath.toAbsolutePath());
printAutoDevelopSummary(body, out);
}
return 0;
}
private ObjectNode buildAutoDevelopRequest(Options options) {
ObjectNode root = objectMapper.createObjectNode();
String baseUrl = baseUrl(options);
root.put("baseUrl", baseUrl);
root.put("generateDeploymentPackage", !options.has("no-deployment-package"));
String maxRepairAttempts = options.single("max-repair-attempts");
if (maxRepairAttempts != null) {
root.put("maxRepairAttempts", parseInt(maxRepairAttempts, "max-repair-attempts"));
}
ObjectNode feature = root.putObject("feature");
putRequired(feature, "objective", options, "objective");
putRequired(feature, "featureName", options, "feature-name");
putRequired(feature, "tableName", options, "table-name");
putRequired(feature, "entityName", options, "entity-name");
putRequired(feature, "templateId", options, "template-id");
putRequired(feature, "menuName", options, "menu-name");
List<String> fields = options.values("field");
if (fields.isEmpty()) {
throw new IllegalArgumentException("Missing required option: --field");
}
ArrayNode fieldNodes = feature.putArray("fields");
for (String field : fields) {
fieldNodes.add(parseField(field));
}
return root;
}
private ObjectNode parseField(String spec) {
String[] parts = spec.split(":", -1);
if (parts.length < 3 || parts.length > 5) {
throw new IllegalArgumentException("Invalid --field, expected column:label:type[:length][:required]");
}
ObjectNode node = objectMapper.createObjectNode();
node.put("column", requiredPart(parts[0], "field column"));
node.put("label", requiredPart(parts[1], "field label"));
node.put("type", requiredPart(parts[2], "field type").toLowerCase(Locale.ROOT));
if (parts.length >= 4 && !parts[3].isBlank()) {
node.put("length", parseInt(parts[3], "field length"));
}
if (parts.length >= 5 && !parts[4].isBlank()) {
node.put("required", Boolean.parseBoolean(parts[4]));
} else {
node.put("required", false);
}
return node;
}
private void putRequired(ObjectNode node, String jsonName, Options options, String optionName) {
node.put(jsonName, requiredPart(options.single(optionName), optionName));
}
private String baseUrl(Options options) {
String value = options.single("base-url");
if (value == null || value.isBlank()) {
value = System.getenv("EIP4_BASE_URL");
}
if (value == null || value.isBlank()) {
value = DEFAULT_BASE_URL;
}
while (value.endsWith("/")) {
value = value.substring(0, value.length() - 1);
}
return value;
}
private URI endpoint(String baseUrl, String path) {
return URI.create(baseUrl + path);
}
private void printAutoDevelopSummary(JsonNode body, PrintStream out) {
out.println("status: " + text(body, "status"));
out.println("taskId: " + text(body, "taskId"));
out.println("changeSetId: " + text(body, "changeSetId"));
JsonNode apply = body.path("applyRun");
if (!apply.isMissingNode()) {
out.println("applyStatus: " + text(apply, "status"));
}
JsonNode smoke = body.path("smokeRun");
if (!smoke.isMissingNode()) {
out.println("smokeStatus: " + text(smoke, "status"));
}
}
private String text(JsonNode node, String field) {
JsonNode value = node.path(field);
return value.isMissingNode() || value.isNull() ? "" : value.asText();
}
private int parseInt(String value, String name) {
try {
return Integer.parseInt(value);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException("Invalid integer for " + name + ": " + value);
}
}
private String requiredPart(String value, String name) {
if (value == null || value.isBlank()) {
throw new IllegalArgumentException("Missing required value: " + name);
}
return value;
}
private boolean isHelp(String arg) {
return "--help".equals(arg) || "-h".equals(arg) || "help".equals(arg);
}
private int fail(PrintStream err, String message) {
err.println(message);
err.println("Run `eip4 --help` for usage.");
return 1;
}
private void printHelp(PrintStream out) {
out.println("""
EIP4 CLI
Usage:
eip4 env status [--base-url URL]
eip4 ai auto-develop --objective TEXT --feature-name NAME --table-name TABLE --entity-name NAME --template-id ID --menu-name NAME --field column:label:type[:length][:required]
Environment:
EIP4_BASE_URL defaults the target service URL when --base-url is omitted.
""");
}
private void printEnvHelp(PrintStream out) {
out.println("""
Usage:
eip4 env status [--base-url URL]
""");
}
private void printAiHelp(PrintStream out) {
out.println("""
Usage:
eip4 ai auto-develop [options]
Required:
--objective TEXT
--feature-name NAME
--table-name TABLE
--entity-name NAME
--template-id ID
--menu-name NAME
--field column:label:type[:length][:required]
Optional:
--base-url URL
--max-repair-attempts N
--no-deployment-package
--output FILE
""");
}
private static final class Options {
private final Map<String, List<String>> values = new LinkedHashMap<>();
static Options parse(String[] args, int from) {
Options options = new Options();
for (int i = from; i < args.length; i++) {
String arg = args[i];
if (!arg.startsWith("--")) {
throw new IllegalArgumentException("Unexpected argument: " + arg);
}
String name = arg.substring(2);
if (name.isBlank()) {
throw new IllegalArgumentException("Invalid option: " + arg);
}
if ("no-deployment-package".equals(name)) {
options.add(name, "true");
continue;
}
if (i + 1 >= args.length || args[i + 1].startsWith("--")) {
throw new IllegalArgumentException("Missing value for option: " + arg);
}
options.add(name, args[++i]);
}
return options;
}
boolean has(String name) {
return values.containsKey(name);
}
String single(String name) {
List<String> list = values.get(name);
if (list == null || list.isEmpty()) {
return null;
}
return list.get(list.size() - 1);
}
List<String> values(String name) {
return values.getOrDefault(name, List.of());
}
private void add(String name, String value) {
values.computeIfAbsent(name, ignored -> new ArrayList<>()).add(value);
}
}
}

View File

@@ -0,0 +1,10 @@
package com.eip4.platform.cli;
import java.io.IOException;
import java.net.URI;
import com.fasterxml.jackson.databind.JsonNode;
interface Eip4HttpClient {
Eip4HttpResponse send(String method, URI uri, JsonNode body) throws IOException, InterruptedException;
}

View File

@@ -0,0 +1,7 @@
package com.eip4.platform.cli;
record Eip4HttpResponse(int statusCode, String body) {
boolean successful() {
return statusCode >= 200 && statusCode < 300;
}
}

View File

@@ -0,0 +1,39 @@
package com.eip4.platform.cli;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
final class JdkEip4HttpClient implements Eip4HttpClient {
private static final Duration TIMEOUT = Duration.ofSeconds(30);
private final HttpClient client;
private final ObjectMapper objectMapper;
JdkEip4HttpClient(ObjectMapper objectMapper) {
this.client = HttpClient.newBuilder().connectTimeout(TIMEOUT).build();
this.objectMapper = objectMapper;
}
@Override
public Eip4HttpResponse send(String method, URI uri, JsonNode body) throws IOException, InterruptedException {
HttpRequest.BodyPublisher publisher = body == null
? HttpRequest.BodyPublishers.noBody()
: HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(body));
HttpRequest request = HttpRequest.newBuilder(uri)
.timeout(TIMEOUT)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.method(method, publisher)
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return new Eip4HttpResponse(response.statusCode(), response.body());
}
}