feat(cli): eip4 init 一键接入 Claude Code/Codex/OpenCode (G1)

- 新增 eip4 init:把 agent skills 装到目标项目
  - Claude Code → .claude/skills/<name>/SKILL.md(代码生成 frontmatter)
  - Codex/OpenCode → 根 AGENTS.md(EIP4-SKILLS 标记块,内嵌 skill 全文,块外保留,幂等)
  - base URL 按 --base-url/--env/current env 渲染
- docs/skills/*.md 的 base URL 改为 {{EIP4_BASE_URL}} 占位符
- 新增 eip4-skills/manifest.json + pom 用 maven-resources-plugin 把 docs/skills 打进 jar(init 默认从 classpath 读,无需仓库在场)
- 文档:AI_AGENT_Nocobase_INTEGRATION_GAP_AND_PLAN(NocoBase 对照 + G1-G3 计划)、skills/README、平台 README
- TDD 5 个新测试,platform-cli 共 20 单测全绿;真实 jar 端到端验证通过

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-22 14:14:26 +08:00
parent 2fc093f25e
commit 5b06aeceff
10 changed files with 478 additions and 14 deletions

View File

@@ -51,6 +51,33 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-agent-skills</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/eip4-skills</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/../docs/skills</directory>
<includes>
<include>*.md</include>
</includes>
<excludes>
<exclude>README.md</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,6 +1,7 @@
package com.eip4.platform.cli;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
@@ -60,6 +61,7 @@ public final class Eip4Cli {
return 0;
}
return switch (args[0]) {
case "init" -> runInit(args, out, err);
case "env" -> runEnv(args, out, err);
case "ai" -> runAi(args, out, err);
case "release" -> runRelease(args, out, err);
@@ -689,6 +691,119 @@ public final class Eip4Cli {
return new CliTarget("", Eip4EnvStore.normalizeBaseUrl(value));
}
private int runInit(String[] args, PrintStream out, PrintStream err) throws IOException {
if (args.length >= 2 && isHelp(args[1])) {
printInitHelp(out);
return 0;
}
Options options = Options.parse(args, 1);
String baseUrl = baseUrl(options);
Path targetDir = Path.of(options.single("dir") == null ? "." : options.single("dir"));
SkillSource source = skillSource(options.single("skills-dir"));
String manifestJson = source.read("manifest.json");
if (manifestJson == null) {
return fail(err, "找不到 skills manifest.json(--skills-dir 指向的目录或内置资源缺失)");
}
JsonNode manifest = objectMapper.readTree(manifestJson);
Path claudeSkills = targetDir.resolve(".claude").resolve("skills");
StringBuilder catalog = new StringBuilder();
StringBuilder bodies = new StringBuilder();
int installed = 0;
for (JsonNode skill : manifest.path("skills")) {
String file = skill.path("file").asText();
String name = skill.path("name").asText();
String description = skill.path("description").asText();
String body = source.read(file);
if (body == null) {
return fail(err, "缺少 skill 文件: " + file);
}
String rendered = body.replace("{{EIP4_BASE_URL}}", baseUrl);
Path skillDir = claudeSkills.resolve(name);
Files.createDirectories(skillDir);
String doc = "---\nname: " + name + "\ndescription: " + description + "\n---\n\n" + rendered;
Files.writeString(skillDir.resolve("SKILL.md"), doc, StandardCharsets.UTF_8);
catalog.append("- **").append(name).append("** — ").append(description).append('\n');
bodies.append("\n---\n\n### Skill: ").append(name).append("\n\n").append(rendered).append('\n');
installed++;
}
Path agents = writeAgentsMd(targetDir, baseUrl, catalog.toString(), bodies.toString());
out.println("Installed " + installed + " EIP4 skill(s) for Claude Code at " + claudeSkills);
out.println("Updated " + agents + " for Codex / OpenCode");
out.println("baseUrl: " + baseUrl);
out.println("Next: cd " + targetDir + " && claude");
return 0;
}
private static final String SKILLS_BEGIN = "<!-- EIP4-SKILLS:BEGIN (managed by `eip4 init`) -->";
private static final String SKILLS_END = "<!-- EIP4-SKILLS:END -->";
private Path writeAgentsMd(Path targetDir, String baseUrl, String catalog, String bodies) throws IOException {
String block = SKILLS_BEGIN + "\n"
+ "## EIP4 平台接入(AI Agent)\n\n"
+ "Base URL: " + baseUrl + "\n\n"
+ "已安装 skills(Claude Code 见 `.claude/skills/`;以下内嵌完整正文,供 Codex / OpenCode 使用):\n"
+ catalog + "\n"
+ "通用原则:优先用 `platform-cli`,其次 REST API;本地写默认放行、生产写阻断;"
+ "AI 产物恒为 READY_FOR_HUMAN_REVIEW,需人工复核后发布。\n"
+ bodies
+ SKILLS_END + "\n";
Path agents = targetDir.resolve("AGENTS.md");
String updated;
if (Files.exists(agents)) {
String existing = Files.readString(agents, StandardCharsets.UTF_8);
int begin = existing.indexOf(SKILLS_BEGIN);
int end = existing.indexOf(SKILLS_END);
if (begin >= 0 && end > begin) {
updated = existing.substring(0, begin) + block
+ existing.substring(end + SKILLS_END.length()).replaceFirst("^\\R", "");
} else {
String sep = existing.isEmpty() || existing.endsWith("\n\n")
? "" : (existing.endsWith("\n") ? "\n" : "\n\n");
updated = existing + sep + block;
}
} else {
Files.createDirectories(targetDir);
updated = "# AGENTS\n\nEIP4 项目的 AI Agent 接入说明。\n\n" + block;
}
Files.writeString(agents, updated, StandardCharsets.UTF_8);
return agents;
}
private SkillSource skillSource(String skillsDir) {
if (skillsDir != null && !skillsDir.isBlank()) {
Path dir = Path.of(skillsDir);
return name -> {
Path file = dir.resolve(name);
return Files.exists(file) ? Files.readString(file, StandardCharsets.UTF_8) : null;
};
}
return name -> {
try (InputStream in = Eip4Cli.class.getResourceAsStream("/eip4-skills/" + name)) {
return in == null ? null : new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
};
}
@FunctionalInterface
private interface SkillSource {
String read(String name) throws IOException;
}
private void printInitHelp(PrintStream out) {
out.println("""
Usage:
eip4 init [--dir DIR] [--base-url URL|--env NAME] [--skills-dir DIR]
Installs EIP4 agent skills so Claude Code / Codex / OpenCode can drive
the platform. Renders the configured base URL into the skill examples.
--dir target project root (default: current directory)
--base-url base URL to render (default: current env, then http://localhost:8084)
--env use a configured env's base URL
--skills-dir read skills from a directory instead of the bundled set
""");
}
private void ensureCrossEnvConfirmed(Options options) throws IOException {
String envName = options.single("env");
if (envName == null || envName.isBlank() || options.has("yes")) {

View File

@@ -0,0 +1,19 @@
{
"skills": [
{
"file": "eip4-env-manage.md",
"name": "eip4-env-manage",
"description": "EIP4 环境探测、启动检查、端口与 profile 边界"
},
{
"file": "eip4-ai-feature-development.md",
"name": "eip4-ai-feature-development",
"description": "从自然语言需求生成实体、服务流、页面、菜单并跑本地 smoke"
},
{
"file": "eip4-release-manage.md",
"name": "eip4-release-manage",
"description": "EIP4 部署包、人审、发布前校验与回滚边界"
}
]
}

View File

@@ -381,6 +381,133 @@ class Eip4CliTest {
assertTrue(result.err().contains("rollbackSqlStatements 仍包含"));
}
@Test
void initRendersClaudeSkillWithRenderedBaseUrl() throws Exception {
Path skillsDir = writeSkillsFixture();
Path target = tempDir.resolve("proj");
CliResult result = run(new CapturingClient(new Eip4HttpResponse(200, "{}")),
"init",
"--dir", target.toString(),
"--skills-dir", skillsDir.toString(),
"--base-url", "http://localhost:8093");
assertEquals(0, result.code());
Path skill = target.resolve(".claude/skills/eip4-demo/SKILL.md");
assertTrue(Files.exists(skill));
String content = Files.readString(skill, StandardCharsets.UTF_8);
assertTrue(content.startsWith("---"), "should carry YAML frontmatter");
assertTrue(content.contains("name: eip4-demo"));
assertTrue(content.contains("description: 演示用 skill"));
assertTrue(content.contains("http://localhost:8093/api/ai/features/auto-develop"));
assertFalse(content.contains("{{EIP4_BASE_URL}}"), "placeholder must be rendered");
}
@Test
void initGeneratesAgentsMdWithSkillCatalog() throws Exception {
Path skillsDir = writeSkillsFixture();
Path target = tempDir.resolve("proj");
CliResult result = run(new CapturingClient(new Eip4HttpResponse(200, "{}")),
"init",
"--dir", target.toString(),
"--skills-dir", skillsDir.toString(),
"--base-url", "http://localhost:8093");
assertEquals(0, result.code());
Path agents = target.resolve("AGENTS.md");
assertTrue(Files.exists(agents));
String md = Files.readString(agents, StandardCharsets.UTF_8);
assertTrue(md.contains("EIP4-SKILLS:BEGIN"), "should carry a managed marker block");
assertTrue(md.contains("EIP4-SKILLS:END"));
assertTrue(md.contains("eip4-demo"));
assertTrue(md.contains("演示用 skill"));
assertTrue(md.contains("http://localhost:8093"));
}
@Test
void initUsesCurrentEnvBaseUrlWhenNoFlag() throws Exception {
Path skillsDir = writeSkillsFixture();
Path target = tempDir.resolve("proj");
Eip4EnvStore store = tempStore();
run(store, new CapturingClient(new Eip4HttpResponse(200, "{}")),
"env", "add", "--name", "dev", "--base-url", "http://localhost:8093", "--use");
CliResult result = run(store, new CapturingClient(new Eip4HttpResponse(200, "{}")),
"init",
"--dir", target.toString(),
"--skills-dir", skillsDir.toString());
assertEquals(0, result.code());
String content = Files.readString(
target.resolve(".claude/skills/eip4-demo/SKILL.md"), StandardCharsets.UTF_8);
assertTrue(content.contains("http://localhost:8093"), "should fall back to current env base URL");
}
@Test
void initPreservesUserContentOutsideMarkerBlock() throws Exception {
Path skillsDir = writeSkillsFixture();
Path target = tempDir.resolve("proj");
Files.createDirectories(target);
Files.writeString(target.resolve("AGENTS.md"),
"# My project\n\n用户自定义说明,必须保留。\n", StandardCharsets.UTF_8);
CliResult result = run(new CapturingClient(new Eip4HttpResponse(200, "{}")),
"init",
"--dir", target.toString(),
"--skills-dir", skillsDir.toString(),
"--base-url", "http://localhost:8093");
assertEquals(0, result.code());
String md = Files.readString(target.resolve("AGENTS.md"), StandardCharsets.UTF_8);
assertTrue(md.contains("# My project"), "user heading must survive");
assertTrue(md.contains("用户自定义说明,必须保留。"), "user content must survive");
assertTrue(md.contains("EIP4-SKILLS:BEGIN"), "managed block must be injected");
assertEquals(1, md.split("EIP4-SKILLS:BEGIN", -1).length - 1, "exactly one managed block");
}
@Test
void initEmbedsFullSkillBodyIntoAgentsMd() throws Exception {
Path skillsDir = writeSkillsFixture();
Path target = tempDir.resolve("proj");
CliResult result = run(new CapturingClient(new Eip4HttpResponse(200, "{}")),
"init",
"--dir", target.toString(),
"--skills-dir", skillsDir.toString(),
"--base-url", "http://localhost:8093");
assertEquals(0, result.code());
String md = Files.readString(target.resolve("AGENTS.md"), StandardCharsets.UTF_8);
// Codex / OpenCode read AGENTS.md, not .claude/skills — the full body must be inline.
assertTrue(md.contains("eip4 ai auto-develop"), "AGENTS.md should embed the full skill body");
assertTrue(md.contains("http://localhost:8093/api/ai/features/auto-develop"),
"embedded body must also be rendered");
}
private Path writeSkillsFixture() throws Exception {
Path dir = tempDir.resolve("skills-src");
Files.createDirectories(dir);
Files.writeString(dir.resolve("manifest.json"), """
{
"skills": [
{"file": "eip4-demo.md", "name": "eip4-demo", "description": "演示用 skill"}
]
}
""", StandardCharsets.UTF_8);
Files.writeString(dir.resolve("eip4-demo.md"), """
# Skill: eip4-demo
调用示例:
```bash
eip4 ai auto-develop --base-url {{EIP4_BASE_URL}}
curl {{EIP4_BASE_URL}}/api/ai/features/auto-develop
```
""", StandardCharsets.UTF_8);
return dir;
}
private Path validPackageFile(String name) throws Exception {
return packageFile(name, "aipkg-1",
"insert into sys_page_schema values ('x')",