示例工程 ch02-four-layers
ch02-four-layers/01-layer-map/index.ts
// 01-layer-map —— 把四层的依赖关系与公开入口打出来,用机器断言代替「我记得是这样」。
//
// 运行:node --experimental-strip-types index.ts
//
// 数据来源是仓库里提交的 vendor 快照(vendor/pi-src/packages/*/package.json),
// 不是 node_modules 的安装布局——pnpm 会把传递依赖放在嵌套目录里,按安装布局看会误判。
import { readFileSync } from "node:fs";
import * as agentCore from "@earendil-works/pi-agent-core";
import * as codingAgent from "@earendil-works/pi-coding-agent";
const VENDOR = new URL("../../../vendor/pi-src/packages/", import.meta.url);
const PACKAGES = ["ai", "agent", "coding-agent", "tui", "mcp", "codemode", "schema", "telemetry"];
interface PkgInfo {
name: string;
version: string;
piDeps: string[];
}
function readPkg(dir: string): PkgInfo {
const raw = JSON.parse(readFileSync(new URL(`${dir}/package.json`, VENDOR), "utf8")) as {
name?: string;
version?: string;
dependencies?: Record<string, string>;
};
const piDeps = Object.keys(raw.dependencies ?? {})
.filter((dep) => dep.startsWith("@earendil-works/"))
.map((dep) => dep.replace("@earendil-works/pi-", ""))
.sort();
return { name: raw.name ?? dir, version: raw.version ?? "?", piDeps };
}
const pkgs = new Map(PACKAGES.map((dir) => [dir, readPkg(dir)]));
console.log("包依赖图(只列 @earendil-works/pi-* 依赖,去掉公共前缀)\n");
for (const [dir, info] of pkgs) {
console.log(`${info.name.padEnd(34)} ${info.version.padEnd(7)} → ${info.piDeps.join(", ") || "(无)"}`);
}
console.log("\n运行时导出(实际 import 的结果)\n");
for (const [label, mod, names] of [
["pi-agent-core", agentCore as Record<string, unknown>, ["agentLoop", "Agent", "setDefaultStreamFn"]],
["pi-coding-agent", codingAgent as Record<string, unknown>, ["createAgentSession", "ModelRuntime", "SessionManager"]],
] as const) {
for (const name of names) {
console.log(` ${label}.${name}: ${typeof mod[name]}`);
}
}
const ok = (label: string, pass: boolean): void => {
console.log(`${pass ? "✓" : "✗"} ${label}`);
if (!pass) process.exitCode = 1;
};
const deps = (dir: string) => pkgs.get(dir)?.piDeps ?? [];
ok("pi-ai 不依赖任何 UI/框架包(只有 schema 与 telemetry)", deps("ai").join(",") === "schema,telemetry");
ok("pi-agent-core 依赖 pi-ai,但不依赖 pi-coding-agent", deps("agent").includes("ai") && !deps("agent").includes("coding-agent"));
ok("pi-coding-agent 同时依赖 pi-agent-core 与 pi-ai", deps("coding-agent").includes("agent-core") && deps("coding-agent").includes("ai"));
ok("pi-coding-agent 依赖 pi-tui(UI 在会话层之上,不在循环层)", deps("coding-agent").includes("tui"));
ok("pi-agent-core 导出 agentLoop", typeof agentCore.agentLoop === "function");
ok("pi-agent-core 不导出 createAgentSession", !("createAgentSession" in agentCore));
ok("createAgentSession 只在 pi-coding-agent 里", typeof codingAgent.createAgentSession === "function");
ok("四层版本都是 1.6.0", ["ai", "agent", "coding-agent", "tui"].every((dir) => pkgs.get(dir)?.version === "1.6.0"));
ok("tui 不依赖 coding-agent(没有反向依赖)", !deps("tui").includes("coding-agent"));
console.log("✓ OK");