javaeeduke commited on
Commit
ca6663e
·
verified ·
1 Parent(s): bde48f5

Delete node inject-secrets.js

Browse files
Files changed (1) hide show
  1. node inject-secrets.js +0 -104
node inject-secrets.js DELETED
@@ -1,104 +0,0 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const { v4: uuidv4 } = require('uuid');
4
-
5
- // 1. 寻找原项目在 Docker 容器内的 SQLite 数据库路径
6
- const dbPaths = [
7
- path.join(__dirname, 'server/prisma/dev.db'),
8
- path.join(__dirname, 'prisma/dev.db'),
9
- '/app/server/prisma/dev.db'
10
- ];
11
-
12
- let dbPath = '';
13
- for (const p of dbPaths) {
14
- if (fs.existsSync(p)) {
15
- dbPath = p;
16
- break;
17
- }
18
- }
19
-
20
- if (!dbPath) {
21
- console.log("⚠️ 未探测到初始数据库文件,正在尝试使用标准路径...");
22
- dbPath = '/app/server/prisma/dev.db';
23
- }
24
-
25
- // 2. 映射你配置的 HF Secrets 名字与项目数据库里的 Provider 标识
26
- const secretsMap = {
27
- "GOOGLE_API_KEY": "google",
28
- "GROQ_API_KEY": "groq",
29
- "GITHUB_TOKEN": "github",
30
- "OPENROUTER_API_KEY": "openrouter",
31
- "MISTRAL_API_KEY": "mistral",
32
- "TOGETHER_API_KEY": "together",
33
- "NVIDIA_API_KEY": "nvidia",
34
- "COHERE_API_KEY": "cohere",
35
- "HF_TOKEN": "huggingface",
36
- "CEREBRAS_API_KEY": "cerebras",
37
- "SAMBANOVA_API_KEY": "sambanova",
38
- "CLOUDFLARE_API_TOKEN": "cloudflare",
39
- "ZHIPU_API_KEY": "zhipu"
40
- };
41
-
42
- async function injectSecrets() {
43
- console.log(`📦 正在通过 Node.js 连接核心数据库: ${dbPath}`);
44
-
45
- // 动态载入原项目自带的 sqlite3 驱动(Prisma 底层自带或依赖 node-sqlite3)
46
- // 为了防止部分环境缺少原生绑定,我们使用原项目必定存在的更好办法:直接通过 sqlite3 库或执行 prisma 脚本
47
- // 这里使用最稳妥的纯原生更好理解:由于原项目使用了 Prisma,直接用原生 fs 或通过 sqlite 写入
48
- try {
49
- const sqlite3 = require('sqlite3').verbose();
50
- const db = new sqlite3.Database(dbPath);
51
-
52
- db.serialize(() => {
53
- // 探测表名
54
- db.all("SELECT name FROM sqlite_master WHERE type='table';", [], (err, tables) => {
55
- if (err) {
56
- console.error("❌ 读取数据库表失败:", err);
57
- return;
58
- }
59
-
60
- const tableNames = tables.map(t => t.name);
61
- console.log(`📊 探测到系统当前表结构: ${tableNames.join(', ')}`);
62
-
63
- const targetTable = tableNames.includes('Provider') ? 'Provider' : tableNames.find(t => t.toLowerCase().includes('key') || t.toLowerCase().includes('provider'));
64
-
65
- if (!targetTable) {
66
- console.log("❌ 未能识别到存储表,请在前端手动添加任意一个Key完成首次初始化。");
67
- return;
68
- }
69
-
70
- let successCount = 0;
71
- const now = new Date().toISOString();
72
-
73
- Object.entries(secretsMap).forEach(([secretName, providerId]) => {
74
- const actualKey = process.env[secretName]; // Node.js 读取环境变量的标准写法
75
- if (!actualKey) return;
76
-
77
- // 检查是否存在
78
- db.get(`SELECT id FROM ${targetTable} WHERE name = ? OR provider = ?`, [providerId, providerId], (err, row) => {
79
- if (!row) {
80
- const recordId = uuidv4();
81
- // 尝试标准 Prisma 插入
82
- db.run(`INSERT INTO ${targetTable} (id, name, provider, apiKey, status, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?, ?)`,
83
- [recordId, providerId, providerId, actualKey, 'active', now, now],
84
- function(err) {
85
- if (err) {
86
- // 备用极简插入
87
- db.run(`INSERT INTO ${targetTable} (id, name, apiKey) VALUES (?, ?, ?)`, [recordId, providerId, actualKey]);
88
- }
89
- }
90
- );
91
- successCount++;
92
- console.log(`✅ 成功自动恢复平台通道: ${providerId}`);
93
- }
94
- });
95
- });
96
- });
97
- });
98
-
99
- } catch (e) {
100
- console.log("⚠️ 正在等待系统原生 Prisma 初始化中,稍后将自动建立映射关系。");
101
- }
102
- }
103
-
104
- injectSecrets();