refactor(core,packaging): relocate embedded assets and binary runtime to src/packaging and enforce core whitelist

This commit is contained in:
gitea committed 2026-09-30 00:29:20 +08:00
1 parent 3864f0ffd0
commit 27c86e052a
10 files changed
+20 -5

No files matched your search

+31
View File
@@ -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);
});
});
+38
View File
@@ -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;
}
+75
View File
@@ -0,0 +1,75 @@
import { describe, expect, test } from "bun:test";
import { mkdtempSync, rmSync, existsSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { join } from "node:path";
import { EMBEDDED_SKILL_FILES, getDefaultGlobalSkillDir, getSkillContent, installSkillFiles } from "./embedded-skill.js";
describe("embedded-skill - 多文件内嵌 Skill 体系", () => {
test("全局 Skill 目录固定为 ~/.agents/skills/hose-use", () => {
expect(getDefaultGlobalSkillDir()).toBe(join(homedir(), ".agents", "skills", "hose-use"));
});
test("正确内嵌 SKILL.md、agents/openai.yaml 及各领域参考专卷", () => {
expect(EMBEDDED_SKILL_FILES["SKILL.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["SKILL.md"]).toContain("name: hose-use");
expect(getSkillContent()).toContain("name: hose-use");
expect(EMBEDDED_SKILL_FILES["agents/openai.yaml"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["agents/openai.yaml"]).toContain("hose_cli");
expect(EMBEDDED_SKILL_FILES["references/auth.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["references/auth.md"]).toContain("RSA");
expect(EMBEDDED_SKILL_FILES["references/bill.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["references/bill.md"]).toContain("bill get");
expect(EMBEDDED_SKILL_FILES["references/workflow.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["references/workflow.md"]).toContain("workflow find");
expect(EMBEDDED_SKILL_FILES["references/service.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["references/service.md"]).toContain("POST /api/cli");
expect(EMBEDDED_SKILL_FILES["references/contact.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["references/contact.md"]).toContain("contact search");
expect(EMBEDDED_SKILL_FILES["references/approval.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["references/approval.md"]).toContain("hose-aipro");
expect(EMBEDDED_SKILL_FILES["references/feetype.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["references/feetype.md"]).toContain("feetype");
expect(EMBEDDED_SKILL_FILES["references/store.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["references/store.md"]).toContain("hose-use keys");
expect(EMBEDDED_SKILL_FILES["SKILL.md"]).toContain("--reveal -o <path>");
expect(EMBEDDED_SKILL_FILES["SKILL.md"]).toContain("references/store.md");
// 套件结构:根入口路由到 multi/ 下的子技能
expect(EMBEDDED_SKILL_FILES["SKILL.md"]).toContain("multi/ekuaibao-mvel/SKILL.md");
expect(EMBEDDED_SKILL_FILES["multi/ekuaibao-mvel/SKILL.md"]).toContain("name: ekuaibao-mvel");
expect(EMBEDDED_SKILL_FILES["multi/ekuaibao-mvel/agents/openai.yaml"]).toContain("ekuaibao-mvel");
expect(EMBEDDED_SKILL_FILES["multi/ekuaibao-nodes/SKILL.md"]).toContain("name: ekuaibao-nodes");
expect(EMBEDDED_SKILL_FILES["multi/ekuaibao-nodes/references/gray-nodes.md"]).toContain("灰度节点清单");
});
test("installSkillFiles 递归保留层级结构并落盘到目标目录", () => {
const tmp = mkdtempSync(join(tmpdir(), "hose-skill-test-"));
try {
const res = installSkillFiles(tmp);
expect(res.targetDir).toBe(tmp);
expect(res.files.length).toBe(18);
expect(existsSync(join(tmp, "SKILL.md"))).toBe(true);
expect(existsSync(join(tmp, "agents", "openai.yaml"))).toBe(true);
expect(existsSync(join(tmp, "references", "auth.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "approval.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "bill.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "contact.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "feetype.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "role.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "workflow.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "service.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "store.md"))).toBe(true);
expect(existsSync(join(tmp, "multi", "ekuaibao-mvel", "SKILL.md"))).toBe(true);
expect(existsSync(join(tmp, "multi", "ekuaibao-nodes", "references", "gray-nodes.md"))).toBe(true);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
});
+77
View File
@@ -0,0 +1,77 @@
import { mkdirSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import skillMain from "../../skills/hose-use/SKILL.md" with { type: "text" };
import agentOpenAi from "../../skills/hose-use/agents/openai.yaml" with { type: "text" };
import refAuth from "../../skills/hose-use/references/auth.md" with { type: "text" };
import refBill from "../../skills/hose-use/references/bill.md" with { type: "text" };
import refWorkflow from "../../skills/hose-use/references/workflow.md" with { type: "text" };
import refService from "../../skills/hose-use/references/service.md" with { type: "text" };
import refContact from "../../skills/hose-use/references/contact.md" with { type: "text" };
import refTask from "../../skills/hose-use/references/task.md" with { type: "text" };
import refMessage from "../../skills/hose-use/references/message.md" with { type: "text" };
import refDimension from "../../skills/hose-use/references/dimension.md" with { type: "text" };
import refRole from "../../skills/hose-use/references/role.md" with { type: "text" };
import refApproval from "../../skills/hose-use/references/approval.md" with { type: "text" };
import refFeeType from "../../skills/hose-use/references/feetype.md" with { type: "text" };
import refStore from "../../skills/hose-use/references/store.md" with { type: "text" };
import multiMvel from "../../skills/hose-use/multi/ekuaibao-mvel/SKILL.md" with { type: "text" };
import multiMvelAgent from "../../skills/hose-use/multi/ekuaibao-mvel/agents/openai.yaml" with { type: "text" };
import multiNodes from "../../skills/hose-use/multi/ekuaibao-nodes/SKILL.md" with { type: "text" };
import multiNodesGray from "../../skills/hose-use/multi/ekuaibao-nodes/references/gray-nodes.md" with { type: "text" };
export const EMBEDDED_SKILL_FILES: Record<string, string> = {
"SKILL.md": skillMain,
"agents/openai.yaml": agentOpenAi,
"references/auth.md": refAuth,
"references/approval.md": refApproval,
"references/bill.md": refBill,
"references/feetype.md": refFeeType,
"references/contact.md": refContact,
"references/task.md": refTask,
"references/message.md": refMessage,
"references/dimension.md": refDimension,
"references/role.md": refRole,
"references/workflow.md": refWorkflow,
"references/service.md": refService,
"references/store.md": refStore,
"multi/ekuaibao-mvel/SKILL.md": multiMvel,
"multi/ekuaibao-mvel/agents/openai.yaml": multiMvelAgent,
"multi/ekuaibao-nodes/SKILL.md": multiNodes,
"multi/ekuaibao-nodes/references/gray-nodes.md": multiNodesGray,
};
export function getSkillContent(): string {
return EMBEDDED_SKILL_FILES["SKILL.md"] || "";
}
export function getDefaultGlobalSkillDir(): string {
return join(homedir(), ".agents", "skills", "hose-use");
}
export function installSkillFiles(targetDir: string = getDefaultGlobalSkillDir()): {
targetDir: string;
files: string[];
} {
mkdirSync(targetDir, { recursive: true });
const installed: string[] = [];
for (const [relPath, content] of Object.entries(EMBEDDED_SKILL_FILES)) {
const fullPath = join(targetDir, relPath);
mkdirSync(dirname(fullPath), { recursive: true });
writeFileSync(fullPath, content, "utf8");
installed.push(relPath);
}
return {
targetDir,
files: installed,
};
}
export function syncGlobalSkillIfPresent(): { synced: boolean; targetDir?: string } {
const globalDir = getDefaultGlobalSkillDir();
installSkillFiles(globalDir);
return { synced: true, targetDir: globalDir };
}
+40
View File
@@ -0,0 +1,40 @@
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
export function isCompiledBinary(execPath = process.execPath, metaUrl = import.meta.url): boolean {
if (metaUrl.includes("/$bunfs/") || metaUrl.includes("\\$bunfs\\")) {
return true;
}
const lower = execPath.toLowerCase().replace(/\\/g, "/");
return !lower.endsWith("/bun") && !lower.endsWith("/bun.exe");
}
export function resolveAppRoot(entryFile?: string, isCompiled = isCompiledBinary()): string {
if (isCompiled) {
return process.cwd();
}
const file = entryFile || fileURLToPath(import.meta.url);
return dirname(dirname(file));
}
export type LauncherConfig = {
command: string;
args?: string[];
};
export function resolveDefaultLauncher(
entryFile: string,
isCompiled = isCompiledBinary(),
execPath = process.execPath
): LauncherConfig {
if (isCompiled) {
return {
command: execPath,
args: [],
};
}
return {
command: execPath,
args: [entryFile],
};
}