"use strict";
/**
 * Copyright (c) 2026 ByteDance Ltd. and/or its affiliates
 * SPDX-License-Identifier: MIT
 *
 * 插件版本号管理
 *
 * 从 package.json 读取版本号并生成 User-Agent 字符串。
 */
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { readFileSync } from 'node:fs';
/** 缓存的版本号 */
let cachedVersion;
/**
 * 获取插件版本号（从 package.json 读取）
 *
 * @returns 版本号字符串，如 "2026.2.28.5"；读取失败返回 "unknown"
 */
export function getPluginVersion() {
    if (cachedVersion)
        return cachedVersion;
    try {
        // 当前文件: src/core/version.ts → 向上两级到达项目根目录
        const __filename = fileURLToPath(import.meta.url);
        const __dirname = dirname(__filename);
        const packageJsonPath = join(__dirname, '..', '..', 'package.json');
        const raw = readFileSync(packageJsonPath, 'utf8');
        const pkg = JSON.parse(raw);
        cachedVersion = pkg.version ?? 'unknown';
        return cachedVersion;
    }
    catch {
        cachedVersion = 'unknown';
        return cachedVersion;
    }
}
/**
 * 生成 User-Agent 字符串
 *
 * @returns User-Agent 字符串，格式：`openclaw-lark/{version}`
 *
 * @example
 * ```typescript
 * getUserAgent() // => "openclaw-lark/2026.2.28.5"
 * ```
 */
export function getUserAgent() {
    return `openclaw-lark/${getPluginVersion()}`;
}
