yymaxapi 1.0.96 → 1.0.97

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bin/yymaxapi.js +94 -0
  2. package/package.json +1 -1
package/bin/yymaxapi.js CHANGED
@@ -2834,6 +2834,88 @@ function syncMirroredAuthStores(paths) {
2834
2834
  }
2835
2835
  }
2836
2836
 
2837
+ function getManagedAgentSessionDirs(paths, agentId) {
2838
+ if (!paths?.configDir || !agentId) return [];
2839
+ const dirs = [path.join(paths.configDir, 'agents', agentId, 'sessions')];
2840
+
2841
+ for (const target of paths.syncTargets || []) {
2842
+ const baseDir = path.dirname(target);
2843
+ dirs.push(path.join(baseDir, 'agents', agentId, 'sessions'));
2844
+ }
2845
+
2846
+ return [...new Set(dirs.filter(Boolean))];
2847
+ }
2848
+
2849
+ function archiveManagedSessionFile(sessionDir, sessionId) {
2850
+ if (!sessionDir || !sessionId) return false;
2851
+ const sessionFile = path.join(sessionDir, `${sessionId}.jsonl`);
2852
+ if (!fs.existsSync(sessionFile)) return false;
2853
+
2854
+ const archiveDir = path.join(sessionDir, 'archive');
2855
+ if (!fs.existsSync(archiveDir)) {
2856
+ fs.mkdirSync(archiveDir, { recursive: true });
2857
+ }
2858
+
2859
+ const archiveName = `${sessionId}-${Date.now()}.jsonl`;
2860
+ fs.renameSync(sessionFile, path.join(archiveDir, archiveName));
2861
+ return true;
2862
+ }
2863
+
2864
+ function resetManagedAgentSessionStore(sessionDir, agentId) {
2865
+ if (!sessionDir || !agentId) return { changed: false, archived: 0 };
2866
+ const sessionsPath = path.join(sessionDir, 'sessions.json');
2867
+ if (!fs.existsSync(sessionsPath)) return { changed: false, archived: 0 };
2868
+
2869
+ let store;
2870
+ try {
2871
+ store = JSON.parse(fs.readFileSync(sessionsPath, 'utf8'));
2872
+ } catch {
2873
+ return { changed: false, archived: 0 };
2874
+ }
2875
+
2876
+ if (!store || typeof store !== 'object' || Array.isArray(store)) {
2877
+ return { changed: false, archived: 0 };
2878
+ }
2879
+
2880
+ const keyPrefix = `agent:${agentId}:`;
2881
+ const keysToRemove = Object.keys(store).filter(key => key.startsWith(keyPrefix));
2882
+ if (keysToRemove.length === 0) return { changed: false, archived: 0 };
2883
+
2884
+ const remaining = { ...store };
2885
+ const removedSessionIds = new Set();
2886
+ for (const key of keysToRemove) {
2887
+ const sessionId = String(store[key]?.sessionId || '').trim();
2888
+ if (sessionId) removedSessionIds.add(sessionId);
2889
+ delete remaining[key];
2890
+ }
2891
+
2892
+ let archived = 0;
2893
+ for (const sessionId of removedSessionIds) {
2894
+ const stillReferenced = Object.values(remaining).some(entry => String(entry?.sessionId || '').trim() === sessionId);
2895
+ if (stillReferenced) continue;
2896
+ if (archiveManagedSessionFile(sessionDir, sessionId)) archived += 1;
2897
+ }
2898
+
2899
+ fs.writeFileSync(sessionsPath, JSON.stringify(remaining, null, 2), 'utf8');
2900
+ return { changed: true, archived };
2901
+ }
2902
+
2903
+ function resetManagedAgentSessionsWithSync(paths, agentId) {
2904
+ const dirs = getManagedAgentSessionDirs(paths, agentId);
2905
+ let changed = false;
2906
+ let archived = 0;
2907
+
2908
+ for (const sessionDir of dirs) {
2909
+ try {
2910
+ const result = resetManagedAgentSessionStore(sessionDir, agentId);
2911
+ if (result.changed) changed = true;
2912
+ archived += result.archived || 0;
2913
+ } catch { /* best-effort */ }
2914
+ }
2915
+
2916
+ return { changed, archived };
2917
+ }
2918
+
2837
2919
  function renameProviderInAuthStore(paths, oldProvider, newProvider) {
2838
2920
  if (!paths?.authProfiles || !oldProvider || !newProvider || oldProvider === newProvider) return false;
2839
2921
  const store = readAuthStore(paths.authProfiles);
@@ -4616,6 +4698,12 @@ async function autoActivate(paths, args = {}) {
4616
4698
  if (yunyiLayoutResult.applied) {
4617
4699
  syncManagedYunyiAuthProfiles(paths, config);
4618
4700
  }
4701
+ if (!isClaudePrimary) {
4702
+ const resetResult = resetManagedAgentSessionsWithSync(paths, YYMAXAPI_OPENCLAW_GPT_AGENT_ID);
4703
+ if (resetResult.changed) {
4704
+ console.log(chalk.gray(` 已重置 ${YYMAXAPI_OPENCLAW_GPT_AGENT_ID} 的活动会话映射`));
4705
+ }
4706
+ }
4619
4707
  const opencodeDefaultModelKey = isClaudePrimary ? `yunyi-claude/${claudeModelId}` : `yunyi-codex/${codexModelId}`;
4620
4708
  try { syncExternalTools('claude', claudeBaseUrl, apiKey, { codexBaseUrl, claudeModelId, opencodeDefaultModelKey }); } catch { /* ignore */ }
4621
4709
  try { syncExternalTools('codex', codexBaseUrl, apiKey, { modelId: codexModelId }); } catch { /* ignore */ }
@@ -5589,6 +5677,12 @@ async function switchModel(paths) {
5589
5677
  primary = finalSelected;
5590
5678
  ensureGatewaySettings(config);
5591
5679
  writeConfigWithSync(paths, config);
5680
+ if (selectedAgentId === YYMAXAPI_OPENCLAW_GPT_AGENT_ID) {
5681
+ const resetResult = resetManagedAgentSessionsWithSync(paths, selectedAgentId);
5682
+ if (resetResult.changed) {
5683
+ console.log(chalk.gray(` 已重置 ${selectedAgentId} 的活动会话映射`));
5684
+ }
5685
+ }
5592
5686
 
5593
5687
  const selectedProviderKey = finalSelected.split('/')[0];
5594
5688
  const selectedProviderConfig = providers[selectedProviderKey];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yymaxapi",
3
- "version": "1.0.96",
3
+ "version": "1.0.97",
4
4
  "description": "跨平台 OpenClaw/Clawdbot 配置管理工具 - 管理中转地址、模型切换、API Keys、测速优化",
5
5
  "main": "bin/yymaxapi.js",
6
6
  "bin": {