aixxai 1.7.6 → 1.7.8

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.
package/src/utils/env.js CHANGED
@@ -1,136 +1,138 @@
1
- /**
2
- * 环境变量配置工具
3
- *
4
- * 把AIXX_API_KEY和AIXX_BASE_URL写入用户的shell配置
5
- * 支持 bash/zsh (Linux/macOS) 和 PowerShell (Windows)
6
- */
7
-
8
- import { existsSync, readFileSync, writeFileSync, mkdirSync, appendFileSync } from 'fs';
9
- import { homedir } from 'os';
10
- import { join } from 'path';
11
- import { exec } from 'child_process';
12
-
13
- // 会话16换门:统一走域名入口(HTTPS加密+CORS白名单+错key锁定+余额脱敏的网关),旧IP:8080仅存量兼容
14
- // 后端地址统一走萬安云域名网关(账号管理与模型调用)
15
- const DEFAULT_BASE_URL = 'https://console.aixx.ai/v1';
16
-
17
- /**
18
- * key脱敏:显示为 前6后4 格式(如 sk-NOj3...5Vfe)。
19
- * 所有命令的输出里,key一律用这个函数处理,绝不打印完整key。
20
- */
21
- export function maskKey(key) {
22
- if (!key || typeof key !== 'string') return '';
23
- if (key.length <= 10) return key.slice(0, 6) + '...';
24
- return `${key.slice(0, 6)}...${key.slice(-4)}`;
25
- }
26
-
27
- /**
28
- * 统一配置读取链(所有子命令共用,别各自为政):
29
- * 环境变量 AIXX_API_KEY → ~/.aixx/config.json → ~/.aixx/account.json
30
- * 返回 key 字符串,找不到返回 null。
31
- */
32
- export function loadApiKey() {
33
- if (process.env.AIXX_API_KEY) return process.env.AIXX_API_KEY;
34
- const home = homedir();
35
- for (const file of [join(home, '.aixx', 'config.json'), join(home, '.aixx', 'account.json')]) {
36
- if (!existsSync(file)) continue;
37
- try {
38
- const conf = JSON.parse(readFileSync(file, 'utf-8'));
39
- if (conf.AIXX_API_KEY || conf.apiKey) return conf.AIXX_API_KEY || conf.apiKey;
40
- } catch (e) {
41
- // 单个文件解析失败就试下一个
42
- }
43
- }
44
- return null;
45
- }
46
-
47
- export async function setupEnv(apiKey, baseUrl = DEFAULT_BASE_URL) {
48
- const platform = process.platform;
49
- const home = homedir();
50
-
51
- // 1. 写入AIXX本地配置文件(所有平台通用)
52
- const aixxDir = join(home, '.aixx');
53
- if (!existsSync(aixxDir)) mkdirSync(aixxDir, { recursive: true });
54
- const configFile = join(aixxDir, 'config.json');
55
- writeFileSync(configFile, JSON.stringify({
56
- AIXX_API_KEY: apiKey,
57
- AIXX_BASE_URL: baseUrl,
58
- configuredAt: new Date().toISOString()
59
- }, null, 2));
60
-
61
- // 2. 写入shell配置(Linux/macOS)
62
- if (platform !== 'win32') {
63
- const envLines = [
64
- ``,
65
- `# AIXX 配置 (由 aixxai 添加)`,
66
- `export AIXX_API_KEY="${apiKey}"`,
67
- `export AIXX_BASE_URL="${baseUrl}"`,
68
- ].join('\n');
69
-
70
- // 检测当前用的shell(优先看$SHELL,比按文件存在猜更准)
71
- // Mac默认zsh,Linux多是bash。$SHELL是用户登录shell,最可靠。
72
- const currentShell = process.env.SHELL || '';
73
- let shellConfigs;
74
- if (currentShell.includes('zsh')) {
75
- // zsh用户:写.zshrc(Mac默认)
76
- shellConfigs = [join(home, '.zshrc')];
77
- } else if (currentShell.includes('bash')) {
78
- // bash用户:Linux写.bashrc,Mac写.bash_profile(Mac的bash读.bash_profile)
79
- shellConfigs = platform === 'darwin'
80
- ? [join(home, '.bash_profile'), join(home, '.bashrc')]
81
- : [join(home, '.bashrc'), join(home, '.bash_profile')];
82
- } else {
83
- // 检测不到(如fish等),按文件存在兜底
84
- shellConfigs = [
85
- join(home, '.zshrc'),
86
- join(home, '.bashrc'),
87
- join(home, '.bash_profile'),
88
- ];
89
- }
90
-
91
- let writtenFile = null;
92
- for (const conf of shellConfigs) {
93
- if (existsSync(conf)) {
94
- // 检查是否已配置过(避免重复)
95
- const existing = readFileSync(conf, 'utf-8');
96
- if (!existing.includes('AIXX_API_KEY')) {
97
- appendFileSync(conf, envLines);
98
- console.log(` → 已写入 ${conf}`);
99
- } else {
100
- console.log(` → ${conf} 已有AIXX配置,跳过`);
101
- }
102
- writtenFile = conf;
103
- break;
104
- }
105
- }
106
-
107
- if (!writtenFile) {
108
- // 没找到现成配置文件,按当前shell创建一个
109
- const fallback = currentShell.includes('zsh') ? join(home, '.zshrc') : join(home, '.bashrc');
110
- appendFileSync(fallback, envLines);
111
- console.log(` → 已创建并写入 ${fallback}`);
112
- writtenFile = fallback;
113
- }
114
-
115
- const rcName = writtenFile.split(/[\\/]/).pop();
116
- console.log(` ⚠️ 请运行 source ~/${rcName} 让配置生效(或重开终端)`);
117
- return true;
118
- } else {
119
- // Windows: 用 setx 真写入用户环境变量(对新开的终端生效)。
120
- // setx 有 1024 字符限制,AIXX key 很短,没问题。
121
- // 注意:第1步的 ~/.aixx/config.json 已经写好了(所有平台兜底),
122
- // 环境变量只是锦上添花——setx 失败不影响任何功能。
123
- try {
124
- const setx = (name, value) =>
125
- new Promise((resolve, reject) => {
126
- exec(`setx ${name} "${value}"`, (err) => (err ? reject(err) : resolve()));
127
- });
128
- await Promise.all([setx('AIXX_API_KEY', apiKey), setx('AIXX_BASE_URL', baseUrl)]);
129
- console.log('✅ 已写入Windows用户环境变量(setx,重开终端后生效)');
130
- return true;
131
- } catch (e) {
132
- console.log('⚠️ 未写入环境变量,但已写入 ~/.aixx/config.json,功能不受影响');
133
- return false;
134
- }
135
- }
136
- }
1
+ /**
2
+ * 环境变量配置工具
3
+ *
4
+ * 把AIXX_API_KEY和AIXX_BASE_URL写入用户的shell配置
5
+ * 支持 bash/zsh (Linux/macOS) 和 PowerShell (Windows)
6
+ */
7
+
8
+ import { existsSync, readFileSync, writeFileSync, mkdirSync, appendFileSync } from 'fs';
9
+ import { homedir } from 'os';
10
+ import { join } from 'path';
11
+ import { exec } from 'child_process';
12
+
13
+ // 会话16换门:统一走域名入口(HTTPS加密+CORS白名单+错key锁定+余额脱敏的网关),旧IP:8080仅存量兼容
14
+ // 后端地址统一走萬安云域名网关(账号管理与模型调用)
15
+ const DEFAULT_BASE_URL = 'https://console.aixx.ai/v1';
16
+
17
+ /**
18
+ * key脱敏:显示为 前6后4 格式(如 sk-NOj3...5Vfe)。
19
+ * 所有命令的输出里,key一律用这个函数处理,绝不打印完整key。
20
+ */
21
+ export function maskKey(key) {
22
+ if (!key || typeof key !== 'string') return '';
23
+ if (key.length <= 10) return key.slice(0, 6) + '...';
24
+ return `${key.slice(0, 6)}...${key.slice(-4)}`;
25
+ }
26
+
27
+ /**
28
+ * 统一配置读取链(所有子命令共用,别各自为政):
29
+ * 环境变量 AIXX_API_KEY → ~/.aixx/config.json → ~/.aixx/account.json
30
+ * 返回 key 字符串,找不到返回 null。
31
+ */
32
+ export function loadApiKey() {
33
+ if (process.env.AIXX_API_KEY) return process.env.AIXX_API_KEY;
34
+ const home = homedir();
35
+ for (const file of [join(home, '.aixx', 'config.json'), join(home, '.aixx', 'account.json')]) {
36
+ if (!existsSync(file)) continue;
37
+ try {
38
+ const conf = JSON.parse(readFileSync(file, 'utf-8'));
39
+ if (conf.AIXX_API_KEY || conf.apiKey) return conf.AIXX_API_KEY || conf.apiKey;
40
+ } catch (e) {
41
+ // 单个文件解析失败就试下一个
42
+ }
43
+ }
44
+ return null;
45
+ }
46
+
47
+ export async function setupEnv(apiKey, baseUrl = DEFAULT_BASE_URL) {
48
+ const platform = process.platform;
49
+ const home = homedir();
50
+
51
+ // 1. 写入AIXX本地配置文件(所有平台通用)
52
+ const aixxDir = join(home, '.aixx');
53
+ if (!existsSync(aixxDir)) mkdirSync(aixxDir, { recursive: true });
54
+ const configFile = join(aixxDir, 'config.json');
55
+ // 1122修: merge写(旧字段如AIXX_USER_ID/USER_PASS保留)——整写会抹掉裸注册身份致7.5重复注册
56
+ let cfg = {};
57
+ try { cfg = JSON.parse(readFileSync(configFile, 'utf-8')) || {}; } catch (e) {}
58
+ cfg.AIXX_API_KEY = apiKey;
59
+ cfg.AIXX_BASE_URL = baseUrl;
60
+ cfg.configuredAt = new Date().toISOString();
61
+ writeFileSync(configFile, JSON.stringify(cfg, null, 2));
62
+
63
+ // 2. 写入shell配置(Linux/macOS)
64
+ if (platform !== 'win32') {
65
+ const envLines = [
66
+ ``,
67
+ `# AIXX 配置 (由 aixxai 添加)`,
68
+ `export AIXX_API_KEY="${apiKey}"`,
69
+ `export AIXX_BASE_URL="${baseUrl}"`,
70
+ ].join('\n');
71
+
72
+ // 检测当前用的shell(优先看$SHELL,比按文件存在猜更准)
73
+ // Mac默认zsh,Linux多是bash。$SHELL是用户登录shell,最可靠。
74
+ const currentShell = process.env.SHELL || '';
75
+ let shellConfigs;
76
+ if (currentShell.includes('zsh')) {
77
+ // zsh用户:写.zshrc(Mac默认)
78
+ shellConfigs = [join(home, '.zshrc')];
79
+ } else if (currentShell.includes('bash')) {
80
+ // bash用户:Linux写.bashrc,Mac写.bash_profile(Mac的bash读.bash_profile)
81
+ shellConfigs = platform === 'darwin'
82
+ ? [join(home, '.bash_profile'), join(home, '.bashrc')]
83
+ : [join(home, '.bashrc'), join(home, '.bash_profile')];
84
+ } else {
85
+ // 检测不到(如fish等),按文件存在兜底
86
+ shellConfigs = [
87
+ join(home, '.zshrc'),
88
+ join(home, '.bashrc'),
89
+ join(home, '.bash_profile'),
90
+ ];
91
+ }
92
+
93
+ let writtenFile = null;
94
+ for (const conf of shellConfigs) {
95
+ if (existsSync(conf)) {
96
+ // 检查是否已配置过(避免重复)
97
+ const existing = readFileSync(conf, 'utf-8');
98
+ if (!existing.includes('AIXX_API_KEY')) {
99
+ appendFileSync(conf, envLines);
100
+ console.log(` → 已写入 ${conf}`);
101
+ } else {
102
+ console.log(` → ${conf} 已有AIXX配置,跳过`);
103
+ }
104
+ writtenFile = conf;
105
+ break;
106
+ }
107
+ }
108
+
109
+ if (!writtenFile) {
110
+ // 没找到现成配置文件,按当前shell创建一个
111
+ const fallback = currentShell.includes('zsh') ? join(home, '.zshrc') : join(home, '.bashrc');
112
+ appendFileSync(fallback, envLines);
113
+ console.log(` → 已创建并写入 ${fallback}`);
114
+ writtenFile = fallback;
115
+ }
116
+
117
+ const rcName = writtenFile.split(/[\\/]/).pop();
118
+ console.log(` ⚠️ 请运行 source ~/${rcName} 让配置生效(或重开终端)`);
119
+ return true;
120
+ } else {
121
+ // Windows: 用 setx 真写入用户环境变量(对新开的终端生效)。
122
+ // setx 有 1024 字符限制,AIXX key 很短,没问题。
123
+ // 注意:第1步的 ~/.aixx/config.json 已经写好了(所有平台兜底),
124
+ // 环境变量只是锦上添花——setx 失败不影响任何功能。
125
+ try {
126
+ const setx = (name, value) =>
127
+ new Promise((resolve, reject) => {
128
+ exec(`setx ${name} "${value}"`, (err) => (err ? reject(err) : resolve()));
129
+ });
130
+ await Promise.all([setx('AIXX_API_KEY', apiKey), setx('AIXX_BASE_URL', baseUrl)]);
131
+ console.log('✅ 已写入Windows用户环境变量(setx,重开终端后生效)');
132
+ return true;
133
+ } catch (e) {
134
+ console.log('⚠️ 未写入环境变量,但已写入 ~/.aixx/config.json,功能不受影响');
135
+ return false;
136
+ }
137
+ }
138
+ }