-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-api-keys.js
More file actions
93 lines (77 loc) · 2.9 KB
/
sync-api-keys.js
File metadata and controls
93 lines (77 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
/**
* 同步 API Keys 腳本
* 將 localStorage 的 API Keys 同步到統一的 JSON 檔案
*/
const fs = require('fs').promises;
const path = require('path');
const UnifiedApiKeyManager = require('./src/plugins/claude-code/lib/api-key-manager');
async function syncApiKeys() {
console.log('========================================');
console.log(' API Keys 同步工具');
console.log('========================================');
console.log('');
try {
// 讀取瀏覽器端的 localStorage 資料(從提示輸入)
console.log('請複製瀏覽器 Console 中 localStorage 的 API Keys 資料');
console.log('在瀏覽器 Console 執行:');
console.log(' JSON.stringify(JSON.parse(localStorage.getItem("api_keys_sync_data")))');
console.log('');
console.log('然後貼上結果(按 Ctrl+D 結束輸入):');
console.log('');
// 從標準輸入讀取
const chunks = [];
for await (const chunk of process.stdin) {
chunks.push(chunk);
}
const input = Buffer.concat(chunks).toString().trim();
if (!input) {
console.error('❌ 錯誤:未提供資料');
process.exit(1);
}
// 解析資料
const data = JSON.parse(input);
const keys = data.keys || [];
console.log(`\n找到 ${keys.length} 組 API Keys`);
console.log('');
// 初始化統一管理器
const manager = new UnifiedApiKeyManager();
await manager.initialize();
// 清除舊 Keys
await manager.clearAll();
console.log('已清除舊的 API Keys');
// 新增所有 Keys
let successCount = 0;
for (const key of keys) {
if (key.key) {
const result = await manager.addKey(key.key);
if (result.success) {
successCount++;
console.log(`✅ 已新增: ${manager.maskKey(key.key)}`);
} else {
console.error(`❌ 新增失敗: ${result.error}`);
}
}
}
console.log('');
console.log('========================================');
console.log(`同步完成!成功同步 ${successCount}/${keys.length} 組 Keys`);
console.log('========================================');
} catch (error) {
console.error('');
console.error('❌ 同步失敗:', error.message);
console.error('');
console.error('請確認:');
console.error('1. 資料格式正確(JSON 格式)');
console.error('2. API Keys 格式正確(以 AIza 開頭)');
process.exit(1);
}
}
// 執行同步
if (require.main === module) {
syncApiKeys().catch(error => {
console.error('執行失敗:', error);
process.exit(1);
});
}
module.exports = syncApiKeys;