mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-27 01:19:25 +08:00
- Decode SQLite step_payload 140.2.6 (GenerateImage) and inspect 32KB PNG/JPEG/WebP headers across SQLite and ConnectRPC trajectories, auto-upgrading cached image-generator subagent entries - Map Gemini 3.1 Flash Image, 3 Pro Image, and 2.5 Flash Image resolution tiers (0.5K/1K/2K/4K) and reference image inputs to Google AI Studio token counts and dedicated imageOutput rates - Surface generate_image steps with true startedAt-to-completedAt duration and localized live activity labels while isolating text LLM TPS, TTFT, context window gauges, compaction inference, and exported report averages
517 lines
19 KiB
JavaScript
517 lines
19 KiB
JavaScript
/**
|
||
* @fileoverview 模型价格与占位符目录加载、Schema 安全清洗及本地/云端目录合并。
|
||
*
|
||
* 价格口径与安全原则:
|
||
* - 所有单价单位均为 USD / 1M Tokens(`input`: 未命中缓存输入, `cache`: 缓存读取, `output`: 输出与思考);
|
||
* - 价格仅用于本地 API 等效成本折算,不代表订阅配额的扣减比例;
|
||
* - 离线基准统一从随包发布的 `pricing-catalog.json` 读取,避免在 JS 源码中重复维护双份硬编码字典;
|
||
* - 未收录或身份未知的模型严格保持未定价状态(`null`),绝不借用其他模型价格,也绝不允许 `null`/`0` 伪价格混入;
|
||
* - 版本号(遵循 SemVer 2.0)与更新包 URL 均经过严格格式与可信 GitHub 域名白名单校验。
|
||
*/
|
||
|
||
import { existsSync } from 'node:fs';
|
||
import { join } from 'node:path';
|
||
import { parseIsoMs } from '../trajectory-metrics.mjs';
|
||
import {
|
||
DEFAULT_ARCHIVE_FALLBACK_URL,
|
||
DEFAULT_DOWNLOAD_URL,
|
||
DEFAULT_RELEASE_URL,
|
||
LEGACY_GITHUB_REPOS,
|
||
LOCAL_CATALOG_FILE,
|
||
OFFICIAL_GITHUB_REPO,
|
||
PLUGIN_MANIFEST_FILE,
|
||
PLUGIN_ROOT,
|
||
REMOTE_CATALOG_CACHE_FILE,
|
||
cloudSyncState,
|
||
detectCustomGitForkRepo,
|
||
loadJson,
|
||
pluginUpdateState,
|
||
} from './config.mjs';
|
||
|
||
/**
|
||
* 当前生效的模型价格表与占位符映射表(通过就地更新属性保持跨模块引用一致)。
|
||
*/
|
||
export const MODEL_PRICING = {};
|
||
export const PLACEHOLDER_MAP = {};
|
||
export let pricingCatalogRevision = 0;
|
||
let pricingPhaseBoundariesMs = [];
|
||
|
||
/**
|
||
* 返回给定时刻已跨过的全局价格阶段边界计数,供遥测与聚合快照在跨越优惠截止时间点时自动失效重算。
|
||
*
|
||
* @param {number} [atMs=Date.now()]
|
||
* @returns {number}
|
||
*/
|
||
export function getPricingPhaseEpoch(atMs = Date.now()) {
|
||
const t = Number(atMs);
|
||
if (!Number.isFinite(t) || t <= 0 || pricingPhaseBoundariesMs.length === 0) return 0;
|
||
let epoch = 0;
|
||
for (let i = 0; i < pricingPhaseBoundariesMs.length; i++) {
|
||
if (t >= pricingPhaseBoundariesMs[i]) epoch = i + 1;
|
||
else break;
|
||
}
|
||
return epoch;
|
||
}
|
||
|
||
/**
|
||
* 校验字符串是否为合法的 SemVer 2.0 语义化版本号格式(支持 `-prerelease` 与 `+build` 元数据)。
|
||
*
|
||
* @param {*} v
|
||
* @returns {boolean}
|
||
*/
|
||
export function isValidSemver(v) {
|
||
return (
|
||
typeof v === 'string' &&
|
||
/^v?\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(v.trim())
|
||
);
|
||
}
|
||
|
||
const OFFICIAL_REPO_PATH_PREFIX = `/${OFFICIAL_GITHUB_REPO.toLowerCase()}`;
|
||
const LEGACY_REPO_PATH_PREFIXES = LEGACY_GITHUB_REPOS.map((repo) => `/${repo.toLowerCase()}`);
|
||
|
||
/**
|
||
* 判断来源 URL 是否属于已迁移的旧官方 GitHub 仓库地址。
|
||
*
|
||
* @param {*} rawUrl
|
||
* @returns {boolean}
|
||
*/
|
||
function isLegacyGithubRepoUrl(rawUrl) {
|
||
if (typeof rawUrl !== 'string' || !rawUrl.trim()) return false;
|
||
try {
|
||
const parsed = new URL(rawUrl.trim());
|
||
const host = parsed.hostname.toLowerCase();
|
||
const pathname = parsed.pathname.toLowerCase();
|
||
if (
|
||
host !== 'github.com' &&
|
||
host !== 'raw.githubusercontent.com' &&
|
||
host !== 'codeload.github.com'
|
||
) {
|
||
return false;
|
||
}
|
||
return LEGACY_REPO_PATH_PREFIXES.some(
|
||
(prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`)
|
||
);
|
||
} catch {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 校验更新/下载链接是否属于可信的 GitHub 官方仓库与 HTTPS 域名白名单。
|
||
*
|
||
* @param {*} rawUrl
|
||
* @param {string} fallbackUrl
|
||
* @returns {string}
|
||
*/
|
||
function sanitizeTrustedGithubUrl(rawUrl, fallbackUrl) {
|
||
if (typeof rawUrl !== 'string' || !rawUrl.trim()) return fallbackUrl;
|
||
try {
|
||
const parsed = new URL(rawUrl.trim());
|
||
if (parsed.protocol !== 'https:') return fallbackUrl;
|
||
const host = parsed.hostname.toLowerCase();
|
||
const pathname = parsed.pathname.toLowerCase();
|
||
if (
|
||
host === 'github.com' ||
|
||
host === 'raw.githubusercontent.com' ||
|
||
host === 'codeload.github.com'
|
||
) {
|
||
const hasRepoPrefix =
|
||
pathname === OFFICIAL_REPO_PATH_PREFIX ||
|
||
pathname.startsWith(`${OFFICIAL_REPO_PATH_PREFIX}/`);
|
||
return hasRepoPrefix ? parsed.toString() : fallbackUrl;
|
||
}
|
||
return fallbackUrl;
|
||
} catch {
|
||
return fallbackUrl;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 按 SemVer 2.0 规范(§10 忽略 `+build` 元数据,§11.4 按 ASCII 码序与纯数字段逐级比较预发布标签)比较两个版本号。
|
||
*
|
||
* @param {string} a
|
||
* @param {string} b
|
||
* @returns {number} `1` 表示 `a > b`,`-1` 表示 `a < b`,`0` 表示相等
|
||
*/
|
||
export function compareSemver(a, b) {
|
||
if (!isValidSemver(a) || !isValidSemver(b)) return 0;
|
||
const parse = (v) => {
|
||
const cleaned = String(v || '')
|
||
.trim()
|
||
.replace(/^v/i, '')
|
||
.replace(/\+.*$/, '');
|
||
const dashIdx = cleaned.indexOf('-');
|
||
const coreStr = dashIdx >= 0 ? cleaned.slice(0, dashIdx) : cleaned;
|
||
const preStr = dashIdx >= 0 ? cleaned.slice(dashIdx + 1) : '';
|
||
const core = coreStr.split('.').map((n) => {
|
||
const parsed = parseInt(n, 10);
|
||
return Number.isFinite(parsed) && parsed >= 0 ? parsed : 0;
|
||
});
|
||
return { core, pre: preStr };
|
||
};
|
||
const pa = parse(a);
|
||
const pb = parse(b);
|
||
const len = Math.max(pa.core.length, pb.core.length, 3);
|
||
for (let i = 0; i < len; i++) {
|
||
const va = pa.core[i] || 0;
|
||
const vb = pb.core[i] || 0;
|
||
if (va > vb) return 1;
|
||
if (va < vb) return -1;
|
||
}
|
||
if (!pa.pre && !pb.pre) return 0;
|
||
if (!pa.pre && pb.pre) return 1;
|
||
if (pa.pre && !pb.pre) return -1;
|
||
|
||
const idsA = pa.pre.split('.');
|
||
const idsB = pb.pre.split('.');
|
||
const minLen = Math.min(idsA.length, idsB.length);
|
||
for (let i = 0; i < minLen; i++) {
|
||
const idA = idsA[i];
|
||
const idB = idsB[i];
|
||
if (idA === idB) continue;
|
||
const numA = /^\d+$/.test(idA);
|
||
const numB = /^\d+$/.test(idB);
|
||
if (numA && numB) {
|
||
const bigA = BigInt(idA);
|
||
const bigB = BigInt(idB);
|
||
if (bigA !== bigB) return bigA > bigB ? 1 : -1;
|
||
continue;
|
||
}
|
||
// SemVer 2.0 §11.4.1:纯数字标识符的优先级始终低于含字母标识符
|
||
if (numA && !numB) return -1;
|
||
if (!numA && numB) return 1;
|
||
// SemVer 2.0 §11.4.2:含字母标识符按 ASCII 字节序比较
|
||
return idA < idB ? -1 : 1;
|
||
}
|
||
if (idsA.length === idsB.length) return 0;
|
||
return idsA.length > idsB.length ? 1 : -1;
|
||
}
|
||
|
||
/**
|
||
* 读取当前已安装插件的 `plugin.json` 版本号。
|
||
*
|
||
* @returns {string}
|
||
*/
|
||
export function readInstalledPluginVersion() {
|
||
const manifest = loadJson(PLUGIN_MANIFEST_FILE, null);
|
||
const ver = typeof manifest?.version === 'string' ? manifest.version.trim() : '';
|
||
return isValidSemver(ver) ? ver.replace(/^v/i, '') : '1.4.4';
|
||
}
|
||
|
||
function normalizeModelFamily(rawFamily) {
|
||
const f = String(rawFamily || '').trim();
|
||
const lower = f.toLowerCase();
|
||
if (lower === 'gemini' || lower === 'google') return 'Gemini';
|
||
if (lower === 'claude' || lower === 'anthropic') return 'Claude';
|
||
if (lower === 'gpt' || lower === 'openai') return 'GPT';
|
||
if (lower === 'other') return 'Other';
|
||
return f;
|
||
}
|
||
|
||
function isValidPricingRateTriplet(entry) {
|
||
if (!entry || typeof entry !== 'object') return false;
|
||
if (
|
||
typeof entry.input !== 'number' ||
|
||
typeof entry.cache !== 'number' ||
|
||
typeof entry.output !== 'number'
|
||
) {
|
||
return false;
|
||
}
|
||
const { input, cache, output } = entry;
|
||
if (!Number.isFinite(input) || input < 0 || input > 10000) return false;
|
||
if (!Number.isFinite(cache) || cache < 0 || cache > 10000) return false;
|
||
if (!Number.isFinite(output) || output < 0 || output > 10000) return false;
|
||
if (input === 0 && output === 0) return false;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 校验并归一化单个时间阶段单价配置(左闭右开 `[fromMs, untilMs)`)。
|
||
* 至少需声明合法的 `from` 或 `until` ISO 时间戳,且若同时声明须满足 `fromMs < untilMs`。
|
||
*/
|
||
function sanitizeOptionalImageOutputRate(val) {
|
||
return typeof val === 'number' && Number.isFinite(val) && val > 0 && val <= 10000 ? val : null;
|
||
}
|
||
|
||
function sanitizePricingPhase(rawPhase) {
|
||
if (!isValidPricingRateTriplet(rawPhase)) return null;
|
||
let from = null;
|
||
let fromMs = -Infinity;
|
||
if (rawPhase.from !== undefined && rawPhase.from !== null) {
|
||
if (typeof rawPhase.from !== 'string' || !rawPhase.from.trim()) return null;
|
||
const parsedFrom = parseIsoMs(rawPhase.from.trim());
|
||
if (!parsedFrom || parsedFrom <= 0) return null;
|
||
from = rawPhase.from.trim();
|
||
fromMs = parsedFrom;
|
||
}
|
||
let until = null;
|
||
let untilMs = Infinity;
|
||
if (rawPhase.until !== undefined && rawPhase.until !== null) {
|
||
if (typeof rawPhase.until !== 'string' || !rawPhase.until.trim()) return null;
|
||
const parsedUntil = parseIsoMs(rawPhase.until.trim());
|
||
if (!parsedUntil || parsedUntil <= 0) return null;
|
||
until = rawPhase.until.trim();
|
||
untilMs = parsedUntil;
|
||
}
|
||
if (from === null && until === null) return null;
|
||
if (fromMs >= untilMs) return null;
|
||
const imageOutput = sanitizeOptionalImageOutputRate(rawPhase.imageOutput);
|
||
return {
|
||
...(from !== null ? { from, fromMs } : {}),
|
||
...(until !== null ? { until, untilMs } : {}),
|
||
input: rawPhase.input,
|
||
cache: rawPhase.cache,
|
||
output: rawPhase.output,
|
||
...(imageOutput !== null ? { imageOutput } : {}),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 校验单条模型价格配置是否合法:
|
||
* - `input`/`cache`/`output` 必须为原生 `number` 类型(阻止 `Number(null) === 0` 或 `Number("") === 0` 的隐式转换漏洞);
|
||
* - 数值必须非负且 `<= 10000`,且至少 `input > 0` 或 `output > 0`(拒绝全 0 伪价格将未定价模型篡改为 $0.00);
|
||
* - `short`/`family` 必须为非空字符串。
|
||
*/
|
||
function isValidPricingEntry(entry) {
|
||
if (!isValidPricingRateTriplet(entry)) return false;
|
||
if (typeof entry.short !== 'string' || !entry.short.trim()) return false;
|
||
if (typeof entry.family !== 'string' || !entry.family.trim()) return false;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 校验单条枚举占位符映射是否合法。
|
||
*/
|
||
function isValidPlaceholderEntry(entry) {
|
||
if (!entry || typeof entry !== 'object') return false;
|
||
if (typeof entry.modelId !== 'string' || !entry.modelId.trim()) return false;
|
||
if (typeof entry.label !== 'string' || !entry.label.trim()) return false;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 清洗并校验来自本地或云端的 `pricing-catalog.json` 原始对象。
|
||
* 任何格式不合法的单条记录会被自动过滤,防止异常数据污染内存价格表。
|
||
*
|
||
* @param {object} raw
|
||
* @returns {object|null}
|
||
*/
|
||
export function sanitizeCatalogPayload(raw) {
|
||
if (!raw || typeof raw !== 'object') return null;
|
||
const pricing = {};
|
||
let validPricingCount = 0;
|
||
if (raw.pricing && typeof raw.pricing === 'object') {
|
||
for (const [k, v] of Object.entries(raw.pricing)) {
|
||
const key = String(k || '').trim();
|
||
if (!key || !isValidPricingEntry(v)) continue;
|
||
let phases = null;
|
||
if (Array.isArray(v.phases)) {
|
||
const cleanedPhases = v.phases.map(sanitizePricingPhase).filter(Boolean);
|
||
if (cleanedPhases.length > 0) {
|
||
phases = cleanedPhases;
|
||
}
|
||
}
|
||
const imageOutput = sanitizeOptionalImageOutputRate(v.imageOutput);
|
||
pricing[key] = {
|
||
input: v.input,
|
||
cache: v.cache,
|
||
output: v.output,
|
||
...(imageOutput !== null ? { imageOutput } : {}),
|
||
short: String(v.short).trim(),
|
||
family: normalizeModelFamily(v.family),
|
||
...(phases ? { phases } : {}),
|
||
};
|
||
validPricingCount++;
|
||
}
|
||
}
|
||
|
||
const placeholders = {};
|
||
let validPlaceholderCount = 0;
|
||
if (raw.placeholders && typeof raw.placeholders === 'object') {
|
||
for (const [k, v] of Object.entries(raw.placeholders)) {
|
||
const key = String(k || '').trim();
|
||
if (!key || !isValidPlaceholderEntry(v)) continue;
|
||
placeholders[key] = {
|
||
modelId: String(v.modelId).trim(),
|
||
label: String(v.label).trim(),
|
||
};
|
||
validPlaceholderCount++;
|
||
}
|
||
}
|
||
|
||
const rawLatestVer =
|
||
typeof raw.latestPluginVersion === 'string' ? raw.latestPluginVersion.trim() : '';
|
||
const latestPluginVersion = isValidSemver(rawLatestVer)
|
||
? rawLatestVer.replace(/^v/i, '')
|
||
: null;
|
||
if (validPricingCount === 0 && validPlaceholderCount === 0 && !latestPluginVersion) {
|
||
return null;
|
||
}
|
||
|
||
const explicitReleaseUrl = sanitizeTrustedGithubUrl(raw.releaseUrl, '');
|
||
const explicitDownloadUrl = sanitizeTrustedGithubUrl(raw.downloadUrl, '');
|
||
const explicitArchiveUrl = sanitizeTrustedGithubUrl(raw.archiveFallbackUrl, '');
|
||
const rawSourceUrl = typeof raw.sourceUrl === 'string' ? raw.sourceUrl.trim() : '';
|
||
const isLegacySource = isLegacyGithubRepoUrl(rawSourceUrl);
|
||
|
||
return {
|
||
schemaVersion: Number(raw.schemaVersion || 1),
|
||
catalogVersion:
|
||
typeof raw.catalogVersion === 'string' && raw.catalogVersion.trim()
|
||
? raw.catalogVersion.trim()
|
||
: null,
|
||
updatedAt:
|
||
typeof raw.updatedAt === 'string' && raw.updatedAt.trim() ? raw.updatedAt.trim() : null,
|
||
latestPluginVersion,
|
||
releaseUrl: explicitReleaseUrl || DEFAULT_RELEASE_URL,
|
||
downloadUrl: explicitDownloadUrl || DEFAULT_DOWNLOAD_URL,
|
||
archiveFallbackUrl: explicitArchiveUrl || DEFAULT_ARCHIVE_FALLBACK_URL,
|
||
hasReleaseUrl: Boolean(explicitReleaseUrl),
|
||
hasDownloadUrl: Boolean(explicitDownloadUrl),
|
||
hasArchiveFallbackUrl: Boolean(explicitArchiveUrl),
|
||
fetchedAtMs: isLegacySource ? 0 : Number(raw.fetchedAtMs || 0),
|
||
sourceUrl: isLegacySource ? '' : rawSourceUrl,
|
||
pricing,
|
||
placeholders,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 按「随包本地目录 -> 云端缓存(按 `updatedAt` 新旧自动裁决优先级)」合并重建内存价格表。
|
||
*/
|
||
export function rebuildActiveCatalog() {
|
||
const localRaw = loadJson(LOCAL_CATALOG_FILE, null);
|
||
const remoteRaw = loadJson(REMOTE_CATALOG_CACHE_FILE, null);
|
||
const localCat = sanitizeCatalogPayload(localRaw);
|
||
const remoteCat = sanitizeCatalogPayload(remoteRaw);
|
||
|
||
const localUpdatedMs = parseIsoMs(localCat?.updatedAt);
|
||
const remoteUpdatedMs = parseIsoMs(remoteCat?.updatedAt);
|
||
|
||
const layers = [];
|
||
if (localCat && remoteCat) {
|
||
if (localUpdatedMs > remoteUpdatedMs) {
|
||
layers.push({ kind: 'remote', cat: remoteCat }, { kind: 'local', cat: localCat });
|
||
} else {
|
||
layers.push({ kind: 'local', cat: localCat }, { kind: 'remote', cat: remoteCat });
|
||
}
|
||
} else if (remoteCat) {
|
||
layers.push({ kind: 'remote', cat: remoteCat });
|
||
} else if (localCat) {
|
||
layers.push({ kind: 'local', cat: localCat });
|
||
}
|
||
|
||
const nextPricing = {};
|
||
const nextPlaceholders = {};
|
||
let activeSource = 'builtin';
|
||
let catalogVersion = 'builtin';
|
||
let updatedAt = null;
|
||
const installedVersion = readInstalledPluginVersion();
|
||
const customForkRepo = detectCustomGitForkRepo();
|
||
const customForkPrefix = customForkRepo ? `/${customForkRepo.toLowerCase()}` : '';
|
||
let latestPluginVersion = installedVersion;
|
||
let releaseUrl = customForkRepo
|
||
? `https://github.com/${customForkRepo}/releases/latest`
|
||
: DEFAULT_RELEASE_URL;
|
||
let downloadUrl = customForkRepo
|
||
? `https://github.com/${customForkRepo}/releases/latest/download/usage-monitor.zip`
|
||
: DEFAULT_DOWNLOAD_URL;
|
||
let archiveFallbackUrl = customForkRepo
|
||
? `https://codeload.github.com/${customForkRepo}/zip/refs/heads/main`
|
||
: DEFAULT_ARCHIVE_FALLBACK_URL;
|
||
|
||
for (const { kind, cat } of layers) {
|
||
for (const [pk, pv] of Object.entries(cat.pricing)) {
|
||
const prevImgOut = nextPricing[pk]?.imageOutput;
|
||
nextPricing[pk] =
|
||
prevImgOut !== undefined && pv.imageOutput === undefined
|
||
? { ...pv, imageOutput: prevImgOut }
|
||
: pv;
|
||
}
|
||
Object.assign(nextPlaceholders, cat.placeholders);
|
||
if (kind === 'remote' || cat.catalogVersion || cat.updatedAt) {
|
||
activeSource = kind;
|
||
}
|
||
if (cat.catalogVersion) catalogVersion = cat.catalogVersion;
|
||
if (cat.updatedAt) updatedAt = cat.updatedAt;
|
||
const isFromCustomFork = Boolean(
|
||
customForkPrefix &&
|
||
typeof cat.sourceUrl === 'string' &&
|
||
cat.sourceUrl.toLowerCase().includes(customForkPrefix)
|
||
);
|
||
const shouldAcceptCatalogReleaseMeta = !customForkRepo || isFromCustomFork;
|
||
if (shouldAcceptCatalogReleaseMeta && cat.latestPluginVersion) {
|
||
// 以当前更高优先级层的 latestPluginVersion 为准,但永不低于本地已安装版本;自定义 Fork 忽略上游版本号
|
||
latestPluginVersion =
|
||
compareSemver(cat.latestPluginVersion, installedVersion) > 0
|
||
? cat.latestPluginVersion
|
||
: installedVersion;
|
||
}
|
||
if (shouldAcceptCatalogReleaseMeta) {
|
||
if (cat.hasReleaseUrl) releaseUrl = cat.releaseUrl;
|
||
if (cat.hasDownloadUrl) downloadUrl = cat.downloadUrl;
|
||
if (cat.hasArchiveFallbackUrl) archiveFallbackUrl = cat.archiveFallbackUrl;
|
||
}
|
||
}
|
||
|
||
for (const k of Object.keys(MODEL_PRICING)) delete MODEL_PRICING[k];
|
||
Object.assign(MODEL_PRICING, nextPricing);
|
||
|
||
const boundarySet = new Set();
|
||
for (const entry of Object.values(nextPricing)) {
|
||
if (!Array.isArray(entry?.phases)) continue;
|
||
for (const ph of entry.phases) {
|
||
if (Number.isFinite(ph.fromMs) && ph.fromMs > 0) boundarySet.add(ph.fromMs);
|
||
if (Number.isFinite(ph.untilMs) && ph.untilMs > 0) boundarySet.add(ph.untilMs);
|
||
}
|
||
}
|
||
pricingPhaseBoundariesMs = Array.from(boundarySet).sort((a, b) => a - b);
|
||
|
||
for (const k of Object.keys(PLACEHOLDER_MAP)) delete PLACEHOLDER_MAP[k];
|
||
Object.assign(PLACEHOLDER_MAP, nextPlaceholders);
|
||
pricingCatalogRevision += 1;
|
||
|
||
cloudSyncState.source = activeSource;
|
||
cloudSyncState.catalogVersion = catalogVersion;
|
||
cloudSyncState.updatedAt = updatedAt;
|
||
cloudSyncState.fetchedAtMs = remoteCat?.fetchedAtMs || cloudSyncState.fetchedAtMs || 0;
|
||
cloudSyncState.sourceUrl = remoteCat?.sourceUrl || cloudSyncState.sourceUrl || '';
|
||
cloudSyncState.latestPluginVersion = latestPluginVersion;
|
||
cloudSyncState.releaseUrl = releaseUrl;
|
||
cloudSyncState.downloadUrl = downloadUrl;
|
||
cloudSyncState.archiveFallbackUrl = archiveFallbackUrl;
|
||
}
|
||
|
||
// 模块初始化时立即构建一次当前生效的价格目录
|
||
rebuildActiveCatalog();
|
||
|
||
/**
|
||
* 获取供前端展示的云端价格目录状态与插件更新状态快照。
|
||
*/
|
||
export function getCloudStatus() {
|
||
const currentPluginVersion = readInstalledPluginVersion();
|
||
const rawLatest = cloudSyncState.latestPluginVersion || currentPluginVersion;
|
||
const latestPluginVersion =
|
||
compareSemver(rawLatest, currentPluginVersion) > 0 ? rawLatest : currentPluginVersion;
|
||
const updateAvailable = compareSemver(latestPluginVersion, currentPluginVersion) > 0;
|
||
return {
|
||
currentPluginVersion,
|
||
latestPluginVersion,
|
||
updateAvailable,
|
||
installMode: existsSync(join(PLUGIN_ROOT, '.git')) ? 'git' : 'zip',
|
||
releaseUrl: cloudSyncState.releaseUrl || DEFAULT_RELEASE_URL,
|
||
downloadUrl: cloudSyncState.downloadUrl || DEFAULT_DOWNLOAD_URL,
|
||
catalogVersion: cloudSyncState.catalogVersion || 'builtin',
|
||
catalogUpdatedAt: cloudSyncState.updatedAt || null,
|
||
lastSyncedAtMs: cloudSyncState.fetchedAtMs || 0,
|
||
source: cloudSyncState.source || 'builtin',
|
||
modelCount: Object.keys(MODEL_PRICING).length,
|
||
placeholderCount: Object.keys(PLACEHOLDER_MAP).length,
|
||
isSyncing: Boolean(cloudSyncState.isSyncing),
|
||
lastSyncError: cloudSyncState.lastError || null,
|
||
isUpdating: Boolean(pluginUpdateState.isUpdating),
|
||
lastUpdateResult: pluginUpdateState.lastResult || null,
|
||
lastUpdateError: pluginUpdateState.lastError || null,
|
||
};
|
||
}
|