| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import { existsSync, realpathSync } from "node:fs";
- import { homedir } from "node:os";
- import { dirname, join, resolve } from "node:path";
- import {
- materializeZCodeBuiltinProviderConfig,
- NodeZCodeBuiltinProviderConfigSource,
- PERSONAL_PROVIDER_CONFIG_FILE_NAME,
- resolveZCodeBuiltinCachePaths,
- resolveZCodeBuiltinClientPlatform,
- ZCODE_BUILTIN_PROVIDER_BUNDLED_CONFIG_FILE_ENV,
- ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV,
- ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV,
- type ZCodeBuiltinRefreshEvent,
- } from "@zcode/provider-node";
- import { resolveRuntimeZCodeEndpointOrigin, ZCODE_VERSION } from "@zcode/shared";
- import type { CliEnv } from "./env.js";
- export const SEA_ZCODE_BUILTIN_PROVIDER_CONFIG_ASSET_KEY = "zcode-provider/zcode-builtin.json";
- export function createCliProviderRefreshReporter(
- stderr: Pick<NodeJS.WriteStream, "write"> = process.stderr,
- ) {
- return {
- onBuiltinRefreshError(error: unknown) {
- stderr.write(
- `ZCode Built-in 刷新失败: ${error instanceof Error ? error.message : "unknown error"}\n`,
- );
- },
- onBuiltinRefreshResult(event: ZCodeBuiltinRefreshEvent) {
- // TTL 检查不是生产事件;成功更新才默认留痕,不能输出 CDN URL 查询参数或内容。
- if (event.result === "updated" || process.env.NODE_ENV !== "production") {
- stderr.write(
- `ZCode Built-in ${event.result}${event.reason ? ` (${event.reason})` : ""}${event.revision === undefined ? "" : ` revision=${event.revision} source=CDN`}\n`,
- );
- }
- },
- };
- }
- type SeaProviderConfigAssets = Pick<typeof import("node:sea"), "getAsset" | "isSea">;
- interface PrepareCliProviderRuntimeEnvOptions {
- readonly argv: readonly string[];
- readonly env: CliEnv;
- readonly dataBaseDir?: string;
- readonly entrypoint?: string;
- readonly sea?: SeaProviderConfigAssets;
- readonly appVersion?: string;
- readonly platform?: string;
- }
- /** 为运行 Core 或写入模型选择的 CLI Entry 定位同一 Environment 的 Provider Config。 */
- export async function prepareCliProviderRuntimeEnv(
- options: PrepareCliProviderRuntimeEnvOptions,
- ): Promise<Record<string, string>> {
- if (!requiresProviderRuntime(options.argv)) return {};
- const explicitZCodeBuiltin = options.env[ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]?.trim();
- const explicitPersonal = options.env[ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]?.trim();
- const dataBaseDir = options.dataBaseDir ?? options.env.ZCODE_DATA_BASE_DIR?.trim() ?? homedir();
- if (explicitZCodeBuiltin && explicitPersonal) {
- return {
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: explicitZCodeBuiltin,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: explicitPersonal,
- };
- }
- const zcodeBuiltinFilePath =
- explicitZCodeBuiltin ??
- (await resolveBundledZCodeBuiltinProviderConfig({
- dataBaseDir,
- entrypoint: options.entrypoint ?? process.argv[1],
- sea: options.sea ?? getSeaProviderConfigAssets(),
- }));
- const personalFilePath =
- explicitPersonal ?? join(dataBaseDir, ".zcode", "v2", PERSONAL_PROVIDER_CONFIG_FILE_NAME);
- const appVersion = options.appVersion ?? ZCODE_VERSION;
- const platform = options.platform ?? resolveZCodeBuiltinClientPlatform();
- const zcodeEndpointOrigin = resolveRuntimeZCodeEndpointOrigin(options.env);
- const cachePaths = resolveZCodeBuiltinCachePaths({
- environmentConfigRoot: join(dataBaseDir, ".zcode", "v2"),
- platform,
- appVersion,
- zcodeEndpointOrigin,
- });
- const source = new NodeZCodeBuiltinProviderConfigSource({
- bundledFilePath: zcodeBuiltinFilePath,
- activeFilePath: cachePaths.activeFilePath,
- watch: false,
- });
- // 入口只准备资源和路径;下载由 Prompt/TUI 长生命周期 Runtime 持有并取消。
- try {
- await source.read();
- } finally {
- source.dispose();
- }
- return {
- [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: cachePaths.activeFilePath,
- [ZCODE_BUILTIN_PROVIDER_BUNDLED_CONFIG_FILE_ENV]: zcodeBuiltinFilePath,
- [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: personalFilePath,
- };
- }
- function requiresProviderRuntime(argv: readonly string[]): boolean {
- if (argv.some((arg) => arg === "--help" || arg === "-h" || arg === "--version" || arg === "-v")) {
- return false;
- }
- if (
- argv.some(
- (arg) =>
- arg === "--prompt" ||
- arg.startsWith("--prompt=") ||
- arg === "--target" ||
- arg.startsWith("--target="),
- )
- ) {
- return true;
- }
- const command = argv[0];
- if (command === undefined || command.startsWith("-")) return true;
- return (
- command === "tui" ||
- command === "app-server" ||
- command === "agent-server" ||
- command === "login" ||
- command === "logout"
- );
- }
- async function resolveBundledZCodeBuiltinProviderConfig(input: {
- readonly dataBaseDir: string;
- readonly entrypoint: string | undefined;
- readonly sea: SeaProviderConfigAssets | undefined;
- }): Promise<string> {
- if (input.sea?.isSea()) {
- const content = input.sea.getAsset(SEA_ZCODE_BUILTIN_PROVIDER_CONFIG_ASSET_KEY, "utf8");
- return materializeZCodeBuiltinProviderConfig({
- environmentConfigRoot: join(input.dataBaseDir, ".zcode", "v2"),
- content,
- });
- }
- const entrypoint = input.entrypoint?.trim();
- if (!entrypoint) throw new Error("无法定位 CLI ZCode Built-in Provider Config:缺少入口路径");
- // 全局 bin 可以是软链接,随包配置必须相对真实入口定位。
- const entryDirectory = dirname(realpathSync(resolve(entrypoint)));
- const candidates = [
- join(entryDirectory, "provider", "zcode-builtin.json"),
- resolve(entryDirectory, "../../../../../config/provider/zcode-builtin.json"),
- ];
- const candidate = candidates.find((filePath) => existsSync(filePath));
- if (candidate) return candidate;
- throw new Error(`无法定位 CLI ZCode Built-in Provider Config:${candidates.join(", ")}`);
- }
- function getSeaProviderConfigAssets(): SeaProviderConfigAssets | undefined {
- const getBuiltinModule = process.getBuiltinModule as
- | ((id: "node:sea") => typeof import("node:sea"))
- | undefined;
- return getBuiltinModule?.("node:sea");
- }
|