feat(panel): add hybrid SQLite+WAL reader, side-question dedup, and cache throttling

- Read local conversation_summaries.db and <id>.db (+ .db-wal composite signatures) first while falling back to RPC when local DBs are absent
- Deduplicate cloned parent steps and gen_metadata across forks and Side Questions, render zh/en Side Question badges, and protect mainline context watermarks
- Throttle trajectory_usage_cache.json writes to 30s during RUNNING sessions and flush immediately on IDLE or exit
- Add phased pricing for gemini-4-argon-b and gemini-4-argon-trial, and suppress false upstream update prompts on custom GitHub forks

Co-authored-by: orbitoo <elbertoo001@gmail.com>
This commit is contained in:
fish
2026-11-17 17:37:19 +09:00
co-authored by orbitoo
parent 64e13f5cfe
commit dba3473494
19 changed files with 3406 additions and 656 deletions
+139
View File
@@ -272,6 +272,55 @@ export const DEFAULT_DOWNLOAD_URL =
export const DEFAULT_ARCHIVE_FALLBACK_URL =
`https://codeload.github.com/${OFFICIAL_GITHUB_REPO}/zip/refs/heads/main`;
/**
* 检测当前插件目录是否关联了非官方自定义 GitHub Fork 远端(如二次开发分支)。
* 命中自定义 Fork 时保留上游价格目录热同步,但将插件版本号与更新包地址锚定至用户自己的 Fork,避免误报更新。
*
* @returns {string} 自定义 Fork 的 `owner/repo`,未命中或为官方仓库时返回 `''`
*/
export function detectCustomGitForkRepo() {
try {
const dotGitPath = join(PLUGIN_ROOT, '.git');
if (!existsSync(dotGitPath)) return '';
let gitConfigPath = join(dotGitPath, 'config');
if (!existsSync(gitConfigPath)) {
const ptrText = readFileSync(dotGitPath, 'utf8');
const m = typeof ptrText === 'string' && ptrText.match(/^gitdir:\s*(.+)$/im);
if (m) {
const gitDir = m[1].trim();
const commonDirFile = join(gitDir, 'commondir');
if (existsSync(commonDirFile)) {
const relCommon = String(readFileSync(commonDirFile, 'utf8') || '').trim();
gitConfigPath = join(gitDir, relCommon, 'config');
} else {
gitConfigPath = join(gitDir, 'config');
}
}
}
if (!existsSync(gitConfigPath)) return '';
const cfg = readFileSync(gitConfigPath, 'utf8');
if (typeof cfg !== 'string' || !cfg) return '';
const originBlock = cfg.match(/\[remote\s+"origin"\]([\s\S]*?)(?:\r?\n\[|$)/i);
if (!originBlock) return '';
const urlMatch = originBlock[1].match(/^\s*url\s*=\s*(.+)$/im);
if (!urlMatch) return '';
const rawUrl = urlMatch[1].trim();
const ghMatch = rawUrl.match(/github\.com[:/]([^/\s]+\/[^/\s]+?)(?:\.git)?\/?$/i);
if (!ghMatch) return '';
const repo = ghMatch[1];
const lower = repo.toLowerCase();
if (
lower === OFFICIAL_GITHUB_REPO.toLowerCase() ||
LEGACY_GITHUB_REPOS.some((r) => r.toLowerCase() === lower)
) {
return '';
}
return repo;
} catch {
return '';
}
}
/** 云端目录常规自动检查间隔(6 小时)与失败冷却时间(5 分钟)。 */
export const CLOUD_SYNC_TTL_MS = 6 * 3600 * 1000;
export const CLOUD_SYNC_RETRY_COOLDOWN_MS = 5 * 60 * 1000;
@@ -441,11 +490,93 @@ export const trajectoryCache = Object.fromEntries(safeCacheEntries);
/**
* 缓存状态与外部深层统计缓存 Memo。
* 运行中会话采用 30 秒节流落盘(转入 IDLE、重建缓存或进程退出时立即刷盘),消除高频文件替换开销。
*/
export const CACHE_RUNNING_SAVE_INTERVAL_MS = 30_000;
export const cacheState = {
needsSave: initialCacheMigrated,
lastSavedAtMs: 0,
flushTimer: null,
};
function cancelPendingCacheFlushTimer() {
if (cacheState.flushTimer) {
try {
clearTimeout(cacheState.flushTimer);
} catch {
// 忽略沙箱无 clearTimeout 异常
}
cacheState.flushTimer = null;
}
}
/**
* 立即将当前内存态 `trajectoryCache` 原子刷入磁盘并清理挂起定时器。
*
* @returns {boolean}
*/
export function flushTrajectoryCacheNow() {
cancelPendingCacheFlushTimer();
const ok = saveJson(CACHE_FILE, trajectoryCache);
cacheState.needsSave = !ok;
if (ok) {
cacheState.lastSavedAtMs = Date.now();
}
return ok;
}
/**
* 按当前是否存在运行中会话决定立即写盘还是 30 秒节流延迟写盘。
* 默认单元测试沙箱(`lsRuntimeEndpoint.addr === 'test.invalid'`)同步落盘以保持断言确定性。
*
* @param {{ hasRunningSessions?: boolean, allowThrottleInTest?: boolean }} [options={}]
*/
export function persistTrajectoryCacheIfNeeded({
hasRunningSessions = false,
allowThrottleInTest = false,
} = {}) {
if (
(lsRuntimeEndpoint.addr === 'test.invalid' && !allowThrottleInTest) ||
typeof setTimeout !== 'function'
) {
flushTrajectoryCacheNow();
return;
}
const nowMs = Date.now();
const elapsedMs = nowMs - (cacheState.lastSavedAtMs || 0);
if (
!hasRunningSessions ||
cacheState.lastSavedAtMs === 0 ||
elapsedMs >= CACHE_RUNNING_SAVE_INTERVAL_MS
) {
flushTrajectoryCacheNow();
return;
}
cacheState.needsSave = true;
if (!cacheState.flushTimer) {
const delayMs = Math.max(100, CACHE_RUNNING_SAVE_INTERVAL_MS - elapsedMs);
const timer = setTimeout(() => {
cacheState.flushTimer = null;
if (cacheState.needsSave) {
flushTrajectoryCacheNow();
}
}, delayMs);
if (timer && typeof timer.unref === 'function') {
timer.unref();
}
cacheState.flushTimer = timer;
}
}
if (typeof process !== 'undefined' && typeof process?.once === 'function') {
process.once('exit', () => {
if (cacheState.needsSave) {
flushTrajectoryCacheNow();
}
});
}
export const externalDeepStatsMemo = {
sig: '',
convos: new Map(),
@@ -454,6 +585,9 @@ export const externalDeepStatsMemo = {
export const localSummaryMemo = {
sig: '',
byId: new Map(),
available: false,
summariesObj: {},
sideQuestionParents: {},
};
/** 并发请求去重表(防止同一参数并发重复拉取)。 */
@@ -502,14 +636,19 @@ export const pluginUpdateState = {
* @returns {boolean} 是否成功写回空白缓存文件
*/
export function clearRuntimeCaches() {
cancelPendingCacheFlushTimer();
for (const key of Object.keys(trajectoryCache)) {
delete trajectoryCache[key];
}
cacheState.needsSave = false;
cacheState.lastSavedAtMs = 0;
externalDeepStatsMemo.sig = '';
externalDeepStatsMemo.convos.clear();
localSummaryMemo.sig = '';
localSummaryMemo.byId.clear();
localSummaryMemo.available = false;
localSummaryMemo.summariesObj = {};
localSummaryMemo.sideQuestionParents = {};
quotaSummaryCache.ts = 0;
quotaSummaryCache.lastAttemptTs = 0;
quotaSummaryCache.accountName = '';