refactor: share plugin package version lookup

This commit is contained in:
Peter Steinberger
2026-04-20 23:30:49 +01:00
parent 4fb2e2309e
commit 6464cf4756
4 changed files with 35 additions and 62 deletions

View File

@@ -1,31 +1,14 @@
import type { Agent } from "node:https";
import { createRequire } from "node:module";
import * as Lark from "@larksuiteoapi/node-sdk";
import { resolveAmbientNodeProxyAgent } from "openclaw/plugin-sdk/extension-shared";
import {
readPluginPackageVersion,
resolveAmbientNodeProxyAgent,
} from "openclaw/plugin-sdk/extension-shared";
import type { FeishuConfig, FeishuDomain, ResolvedFeishuAccount } from "./types.js";
const require = createRequire(import.meta.url);
const PACKAGE_JSON_CANDIDATES = [
"../package.json",
"./package.json",
"../../package.json",
] as const;
function readPluginVersion(): string {
for (const candidate of PACKAGE_JSON_CANDIDATES) {
try {
const version = (require(candidate) as { version?: unknown }).version;
if (typeof version === "string" && version.trim().length > 0) {
return version;
}
} catch {
// Ignore missing candidate paths across source and bundled layouts.
}
}
return "unknown";
}
const pluginVersion = readPluginVersion();
const pluginVersion = readPluginPackageVersion({ require });
export { pluginVersion };

View File

@@ -1,6 +1,7 @@
import { createRequire } from "node:module";
import os from "node:os";
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
import { readPluginPackageVersion } from "openclaw/plugin-sdk/extension-shared";
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import { debugLog, debugError } from "./utils/debug-log.js";
@@ -12,26 +13,7 @@ const TOKEN_URL = "https://bots.qq.com/app/getAppAccessToken";
// Plugin User-Agent format: QQBotPlugin/{version} (Node/{nodeVersion}; {os})
const _require = createRequire(import.meta.url);
const PACKAGE_JSON_CANDIDATES = [
"../package.json",
"./package.json",
"../../package.json",
] as const;
function readPluginVersion(): string {
for (const candidate of PACKAGE_JSON_CANDIDATES) {
try {
const version = (_require(candidate) as { version?: unknown }).version;
if (typeof version === "string" && version.trim().length > 0) {
return version;
}
} catch {
// Ignore missing candidate paths across source and bundled layouts.
}
}
return "unknown";
}
const _pluginVersion = readPluginVersion();
const _pluginVersion = readPluginPackageVersion({ require: _require });
export const PLUGIN_USER_AGENT = `QQBotPlugin/${_pluginVersion} (Node/${process.versions.node}; ${os.platform()})`;
// =========================================================================

View File

@@ -11,33 +11,15 @@ import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { resolveRuntimeServiceVersion } from "openclaw/plugin-sdk/cli-runtime";
import { readPluginPackageVersion } from "openclaw/plugin-sdk/extension-shared";
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
import type { QQBotAccountConfig } from "./types.js";
import { debugLog } from "./utils/debug-log.js";
import { getHomeDir, getQQBotDataDir, isWindows } from "./utils/platform.js";
const require = createRequire(import.meta.url);
const PACKAGE_JSON_CANDIDATES = [
"../package.json",
"./package.json",
"../../package.json",
] as const;
function readPluginVersion(): string {
for (const candidate of PACKAGE_JSON_CANDIDATES) {
try {
const version = (require(candidate) as { version?: unknown }).version;
if (typeof version === "string" && version.trim().length > 0) {
return version;
}
} catch {
// Ignore missing candidate paths across source and bundled layouts.
}
}
return "unknown";
}
// Read the package version from package.json.
const PLUGIN_VERSION = readPluginVersion();
const PLUGIN_VERSION = readPluginPackageVersion({ require });
const QQBOT_PLUGIN_GITHUB_URL = "https://github.com/openclaw/openclaw/tree/main/extensions/qqbot";
const QQBOT_UPGRADE_GUIDE_URL = "https://q.qq.com/qqbot/openclaw/upgrade.html";

View File

@@ -135,6 +135,32 @@ export function createDeferred<T>() {
return { promise, resolve, reject };
}
const DEFAULT_PACKAGE_JSON_VERSION_CANDIDATES = [
"../package.json",
"./package.json",
"../../package.json",
] as const;
type PackageJsonRequire = (id: string) => unknown;
export function readPluginPackageVersion(params: {
require: PackageJsonRequire;
candidates?: readonly string[];
fallback?: string;
}): string {
for (const candidate of params.candidates ?? DEFAULT_PACKAGE_JSON_VERSION_CANDIDATES) {
try {
const version = (params.require(candidate) as { version?: unknown }).version;
if (typeof version === "string" && version.trim().length > 0) {
return version;
}
} catch {
// Ignore missing candidate paths across source and bundled layouts.
}
}
return params.fallback ?? "unknown";
}
let proxyAgentConstructorPromise: Promise<typeof import("proxy-agent").ProxyAgent> | null = null;
async function loadProxyAgentConstructor(): Promise<typeof import("proxy-agent").ProxyAgent> {