feat(openapi): 引入双层 OpenAPI 规范管理 (原始下游 & 包装层工具) 并升级版本至 v0.2.2
🚀 Release / 🚀 Build & Publish Multi-Platform Binaries (push) Has been cancelled
🚀 Release / 🚀 Build & Publish Multi-Platform Binaries (push) Has been cancelled
This commit is contained in:
1 parent
2dfcf67b00
commit
b13959451a
21 files changed
+1435
-12
No files matched your search
@@ -0,0 +1,111 @@
|
||||
import { getOpenApiSchema, listOpenApiPaths, type OpenApiType } from "../../core/embedded-openapi.js";
|
||||
import { booleanOption, stringOption } from "../parser.js";
|
||||
import { printJson, printKeyValue, printTable } from "../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
|
||||
export async function handleOpenApiCommand(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const isRaw =
|
||||
booleanOption(parsed.options, ["raw"]) ||
|
||||
parsed.action === "raw" ||
|
||||
parsed.args[0] === "raw";
|
||||
|
||||
const type: OpenApiType = isRaw ? "raw" : "wrapped";
|
||||
const action = parsed.action?.toLowerCase();
|
||||
|
||||
switch (action) {
|
||||
case "paths":
|
||||
case "list":
|
||||
case "ls": {
|
||||
const paths = listOpenApiPaths(type);
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({ type, count: paths.length, paths }),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const rows = paths.map((p) => [
|
||||
p.method,
|
||||
p.path,
|
||||
p.summary || "-",
|
||||
p.tags.join(", ") || "-",
|
||||
]);
|
||||
|
||||
const table = printTable(["Method", "Path", "Summary", "Tags"], rows);
|
||||
const title = isRaw
|
||||
? "====== 合思/易快报 原生下游 OpenAPI 接口列表 ======"
|
||||
: "====== hose-use 包装层 OpenAPI 接口列表 ======";
|
||||
|
||||
return {
|
||||
stdout: `${title}\n\n${table}\n\n提示: 运行 hose-use openapi ${isRaw ? "--raw " : ""}可直接输出完整 OpenAPI 3.1.0 JSON\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
case "info":
|
||||
case "summary": {
|
||||
const schema = getOpenApiSchema(type);
|
||||
const info = (schema.info as Record<string, unknown>) || {};
|
||||
const servers = ((schema.servers as Array<{ url: string; description?: string }>) || []).map(
|
||||
(s) => `${s.url} (${s.description || "-"})`
|
||||
);
|
||||
const tags = ((schema.tags as Array<{ name: string; description?: string }>) || []).map(
|
||||
(t) => `${t.name}: ${t.description || ""}`
|
||||
);
|
||||
const paths = listOpenApiPaths(type);
|
||||
|
||||
const infoObj = {
|
||||
title: info.title,
|
||||
version: info.version,
|
||||
description: info.description,
|
||||
type,
|
||||
totalEndpoints: paths.length,
|
||||
servers,
|
||||
tags,
|
||||
};
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(infoObj),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const out = printKeyValue({
|
||||
"规范标题": info.title,
|
||||
"规范版本": info.version,
|
||||
"规范类型": isRaw ? "原始下游原生 API (Raw Spec)" : "包装层工具 API (Wrapped Spec)",
|
||||
"接口总数": paths.length,
|
||||
"描述信息": info.description,
|
||||
"服务器列表": servers.join("\n "),
|
||||
"标签分组": tags.join("\n "),
|
||||
});
|
||||
|
||||
return {
|
||||
stdout: `====== OpenAPI 规范概览 ======\n${out}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
case "dump":
|
||||
case "export":
|
||||
case "raw":
|
||||
case undefined:
|
||||
default: {
|
||||
const schema = getOpenApiSchema(type);
|
||||
return {
|
||||
stdout: `${JSON.stringify(schema, null, 2)}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
+30
-5
@@ -1,5 +1,26 @@
|
||||
import type { CliParsedArgs } from "./types.js";
|
||||
|
||||
const KNOWN_BOOLEAN_FLAGS = new Set([
|
||||
"raw",
|
||||
"json",
|
||||
"j",
|
||||
"help",
|
||||
"h",
|
||||
"version",
|
||||
"v",
|
||||
"force",
|
||||
"f",
|
||||
"stateless",
|
||||
"stateful",
|
||||
"check",
|
||||
"check-only",
|
||||
"changelog",
|
||||
"log",
|
||||
"local",
|
||||
"workspace",
|
||||
"external",
|
||||
]);
|
||||
|
||||
export function parseArgs(rawArgs: string[]): CliParsedArgs {
|
||||
const options: Record<string, string | boolean | number> = {};
|
||||
const positional: string[] = [];
|
||||
@@ -20,12 +41,16 @@ export function parseArgs(rawArgs: string[]): CliParsedArgs {
|
||||
options[key] = val;
|
||||
} else {
|
||||
const key = arg.slice(2);
|
||||
const next = rawArgs[i + 1];
|
||||
if (next && !next.startsWith("-")) {
|
||||
options[key] = next;
|
||||
i++;
|
||||
} else {
|
||||
if (KNOWN_BOOLEAN_FLAGS.has(key)) {
|
||||
options[key] = true;
|
||||
} else {
|
||||
const next = rawArgs[i + 1];
|
||||
if (next && !next.startsWith("-")) {
|
||||
options[key] = next;
|
||||
i++;
|
||||
} else {
|
||||
options[key] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
|
||||
@@ -52,6 +52,7 @@ export function printHelp(): string {
|
||||
skill Agent Skill 管理 (install, status, dump)
|
||||
probe 连通性诊断 (probe [corpId])
|
||||
update 版本自更新与发布记录 (update, update check, update --changelog)
|
||||
openapi OpenAPI 3.1.0 规范导出 (openapi, openapi --raw, openapi paths, openapi info)
|
||||
mcp MCP Server 运行 (serve)
|
||||
version 查看版本信息
|
||||
|
||||
@@ -74,5 +75,7 @@ export function printHelp(): string {
|
||||
hose-use skill install # 安装 Agent 技能到用户目录
|
||||
hose-use update # 自动检查并升级到最新版本
|
||||
hose-use update --changelog # 查看版本发布历史
|
||||
hose-use openapi paths # 查看所有 API 路径与概要一览
|
||||
hose-use openapi --raw # 导出原生下游全量 OpenAPI 规范
|
||||
`.trim();
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import { handleSkillCommand } from "./commands/skill.js";
|
||||
import { handleProbeCommand } from "./commands/probe.js";
|
||||
import { handleMcpCommand } from "./commands/mcp.js";
|
||||
import { handleUpdateCommand } from "./commands/update.js";
|
||||
import { handleOpenApiCommand } from "./commands/openapi.js";
|
||||
import type { CliResult, CliRunOptions } from "./types.js";
|
||||
|
||||
export async function runCli(
|
||||
@@ -70,6 +71,10 @@ export async function runCli(
|
||||
case "self-update":
|
||||
case "changelog":
|
||||
return await handleUpdateCommand(parsed, runtime, options);
|
||||
case "openapi":
|
||||
case "schema":
|
||||
case "spec":
|
||||
return await handleOpenApiCommand(parsed, runtime);
|
||||
case "mcp":
|
||||
case "serve":
|
||||
return await handleMcpCommand(parsed, options);
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { getOpenApiSchema, listOpenApiPaths } from "./embedded-openapi.js";
|
||||
|
||||
describe("embedded OpenAPI", () => {
|
||||
test("wrapped OpenAPI contains paths and components", () => {
|
||||
const wrapped = getOpenApiSchema("wrapped");
|
||||
expect(wrapped.openapi).toBe("3.1.0");
|
||||
expect(wrapped.info).toBeDefined();
|
||||
expect(wrapped.paths).toBeDefined();
|
||||
expect(Object.keys(wrapped.paths as object).length).toBeGreaterThan(0);
|
||||
|
||||
const paths = listOpenApiPaths("wrapped");
|
||||
expect(paths.length).toBeGreaterThan(0);
|
||||
expect(paths.some((p) => p.path === "/api/auth/login")).toBe(true);
|
||||
expect(paths.some((p) => p.path === "/api/workflows")).toBe(true);
|
||||
});
|
||||
|
||||
test("raw downstream OpenAPI contains downstream paths", () => {
|
||||
const raw = getOpenApiSchema("raw");
|
||||
expect(raw.openapi).toBe("3.1.0");
|
||||
expect(raw.info).toBeDefined();
|
||||
expect(raw.paths).toBeDefined();
|
||||
expect(Object.keys(raw.paths as object).length).toBeGreaterThan(0);
|
||||
|
||||
const paths = listOpenApiPaths("raw");
|
||||
expect(paths.length).toBeGreaterThan(0);
|
||||
expect(paths.some((p) => p.path === "/api/common/security/getPublicKey")).toBe(true);
|
||||
expect(paths.some((p) => p.path === "/api/open/oauth2/token")).toBe(true);
|
||||
expect(paths.some((p) => p.path === "/api/easo/pipelines/logs")).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import wrappedOpenApi from "../../dist/openapi.json" with { type: "json" };
|
||||
import rawOpenApi from "../../dist/raw-hose-openapi.json" with { type: "json" };
|
||||
|
||||
export type OpenApiType = "wrapped" | "raw";
|
||||
|
||||
export function getOpenApiSchema(type: OpenApiType = "wrapped"): Record<string, unknown> {
|
||||
if (type === "raw") {
|
||||
return rawOpenApi as Record<string, unknown>;
|
||||
}
|
||||
return wrappedOpenApi as Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function listOpenApiPaths(type: OpenApiType = "wrapped"): Array<{
|
||||
path: string;
|
||||
method: string;
|
||||
summary: string;
|
||||
tags: string[];
|
||||
}> {
|
||||
const schema = getOpenApiSchema(type);
|
||||
const paths = (schema.paths as Record<string, Record<string, any>>) || {};
|
||||
const results: Array<{ path: string; method: string; summary: string; tags: string[] }> = [];
|
||||
|
||||
for (const [pathKey, methods] of Object.entries(paths)) {
|
||||
if (typeof methods !== "object" || methods === null) continue;
|
||||
for (const [method, def] of Object.entries(methods)) {
|
||||
if (["get", "post", "put", "delete", "patch"].includes(method.toLowerCase()) && typeof def === "object") {
|
||||
results.push({
|
||||
path: pathKey,
|
||||
method: method.toUpperCase(),
|
||||
summary: def.summary || def.description || "",
|
||||
tags: def.tags || [],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
+1
-1
@@ -1 +1 @@
|
||||
export const VERSION = "0.2.1";
|
||||
export const VERSION = "0.2.2";
|
||||
Reference in new issue
Block a user