mirror of
https://github.com/hanzoai/bot.git
synced 2026-11-23 04:17:14 +00:00
The public repo could not be installed or run, and the two defects share a cause: a rename left constants pointing at names that do not exist. Install: @hanzo/iam was pinned to github:hanzo-js/iam#main, a private repo, so pnpm install failed with ERR_PNPM_FETCH_404 for anyone without access to it — including CI, which passes no token, so the image install could not have resolved it either. The package is published, so depend on it from npm. That leaves no dependency needing credentials, so the Dockerfile's GIT_TOKEN plumbing goes too; it wrote the token into .npmrc and took it on a build arg, which docker history keeps. The SDK names its server-side types Config/AuthResult/JwtClaims, so alias them to the Iam* names the gateway wrapper exposes. Config is what validateToken and IamClient accept; IAMConfig is the browser entrypoint's config and does not belong here. CLI: package.json declares hanzo-bot.mjs as its bin, but ENTRY_WRAPPER_PAIRS listed bot.mjs twice and hanzo-bot.mjs never, so isMainModule() saw no pair, entry.ts skipped its body, and every invocation exited 0 with no output — a gateway that starts, registers nothing and stops. Derive the pairs from the bin wrappers and the entry files hanzo-bot.mjs imports, as a cross product, so a row cannot be duplicated or dropped. Version: resolveBinaryVersion() skips any package.json whose name is not CORE_PACKAGE_NAME, which read "bot" while the package ships as @hanzo/bot, so the package.json path resolved nothing and the version came from build-info.json alone — absent from a bundle-only build, which then reported 0.0.0. Both new tests fail when their defect is reintroduced: they assert against the bins package.json declares and against the real package.json, rather than restating the value under test. Also from hanzobot/core: cloud-node detection behind one helper, node-host reusing startPlaygroundRegistration instead of a second hand-rolled copy, the playground node API on /v1, and the install smoke tests naming the package @hanzo/bot rather than the bin name.
133 lines
3.7 KiB
TypeScript
133 lines
3.7 KiB
TypeScript
import { createRequire } from "node:module";
|
|
|
|
declare const __BOT_VERSION__: string | undefined;
|
|
// Must match the "name" in package.json. The guard below walks up from the module
|
|
// looking for that package.json and skips any other one it passes on the way (a
|
|
// consumer's own, or an unrelated nested one), so a stale value here silently
|
|
// resolves no version at all.
|
|
const CORE_PACKAGE_NAME = "@hanzo/bot";
|
|
|
|
const PACKAGE_JSON_CANDIDATES = [
|
|
"../package.json",
|
|
"../../package.json",
|
|
"../../../package.json",
|
|
"./package.json",
|
|
] as const;
|
|
|
|
const BUILD_INFO_CANDIDATES = [
|
|
"../build-info.json",
|
|
"../../build-info.json",
|
|
"./build-info.json",
|
|
] as const;
|
|
|
|
function readVersionFromJsonCandidates(
|
|
moduleUrl: string,
|
|
candidates: readonly string[],
|
|
opts: { requirePackageName?: boolean } = {},
|
|
): string | null {
|
|
try {
|
|
const require = createRequire(moduleUrl);
|
|
for (const candidate of candidates) {
|
|
try {
|
|
const parsed = require(candidate) as { name?: string; version?: string };
|
|
const version = parsed.version?.trim();
|
|
if (!version) {
|
|
continue;
|
|
}
|
|
if (opts.requirePackageName && parsed.name !== CORE_PACKAGE_NAME) {
|
|
continue;
|
|
}
|
|
return version;
|
|
} catch {
|
|
// ignore missing or unreadable candidate
|
|
}
|
|
}
|
|
return null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function firstNonEmpty(...values: Array<string | undefined>): string | undefined {
|
|
for (const value of values) {
|
|
const trimmed = value?.trim();
|
|
if (trimmed) {
|
|
return trimmed;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export function readVersionFromPackageJsonForModuleUrl(moduleUrl: string): string | null {
|
|
return readVersionFromJsonCandidates(moduleUrl, PACKAGE_JSON_CANDIDATES, {
|
|
requirePackageName: true,
|
|
});
|
|
}
|
|
|
|
export function readVersionFromBuildInfoForModuleUrl(moduleUrl: string): string | null {
|
|
return readVersionFromJsonCandidates(moduleUrl, BUILD_INFO_CANDIDATES);
|
|
}
|
|
|
|
export function resolveVersionFromModuleUrl(moduleUrl: string): string | null {
|
|
return (
|
|
readVersionFromPackageJsonForModuleUrl(moduleUrl) ||
|
|
readVersionFromBuildInfoForModuleUrl(moduleUrl)
|
|
);
|
|
}
|
|
|
|
export function resolveBinaryVersion(params: {
|
|
moduleUrl: string;
|
|
injectedVersion?: string;
|
|
bundledVersion?: string;
|
|
fallback?: string;
|
|
}): string {
|
|
return (
|
|
firstNonEmpty(params.injectedVersion) ||
|
|
resolveVersionFromModuleUrl(params.moduleUrl) ||
|
|
firstNonEmpty(params.bundledVersion) ||
|
|
params.fallback ||
|
|
"0.0.0"
|
|
);
|
|
}
|
|
|
|
export type RuntimeVersionEnv = {
|
|
[key: string]: string | undefined;
|
|
};
|
|
|
|
export const RUNTIME_SERVICE_VERSION_FALLBACK = "unknown";
|
|
|
|
export function resolveUsableRuntimeVersion(version: string | undefined): string | undefined {
|
|
const trimmed = version?.trim();
|
|
// "0.0.0" is the resolver's hard fallback when module metadata cannot be read.
|
|
// Prefer explicit service/package markers in that edge case.
|
|
if (!trimmed || trimmed === "0.0.0") {
|
|
return undefined;
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
export function resolveRuntimeServiceVersion(
|
|
env: RuntimeVersionEnv = process.env as RuntimeVersionEnv,
|
|
fallback = RUNTIME_SERVICE_VERSION_FALLBACK,
|
|
): string {
|
|
const runtimeVersion = resolveUsableRuntimeVersion(VERSION);
|
|
|
|
return (
|
|
firstNonEmpty(
|
|
env["BOT_VERSION"],
|
|
runtimeVersion,
|
|
env["BOT_SERVICE_VERSION"],
|
|
env["npm_package_version"],
|
|
) ?? fallback
|
|
);
|
|
}
|
|
|
|
// Single source of truth for the current HanzoBot version.
|
|
// - Embedded/bundled builds: injected define or env var.
|
|
// - Dev/npm builds: package.json.
|
|
export const VERSION = resolveBinaryVersion({
|
|
moduleUrl: import.meta.url,
|
|
injectedVersion: typeof __BOT_VERSION__ === "string" ? __BOT_VERSION__ : undefined,
|
|
bundledVersion: process.env.BOT_BUNDLED_VERSION,
|
|
});
|