aixxai 1.4.2 → 1.4.4

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,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
- // ⚠️ 安全警告:当前用HTTP明文传输API Key(http://),存在凭据被嗅探风险。
14
- // TODO(安全):后端配置好HTTPS证书后,把下面的 URL 改为 'https://14.103.27.195:8080/v1'。
15
- const DEFAULT_BASE_URL = 'http://14.103.27.195:8080/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
+ // ⚠️ 安全警告:当前用HTTP明文传输API Key(http://),存在凭据被嗅探风险。
14
+ // TODO(安全):后端配置好HTTPS证书后,把下面的 URL 改为 'https://14.103.27.195:8080/v1'。
15
+ const DEFAULT_BASE_URL = 'http://14.103.27.195:8080/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,100 +1,100 @@
1
- # AIXX 快速开始
2
-
3
- **一条命令装完即用,30秒开始让AI调用万物。**
4
-
5
- ---
6
-
7
- ## 安装(唯一需要做的一步)
8
-
9
- ```bash
10
- npx aixxai install
11
- ```
12
-
13
- 装完自动完成:
14
- 1. **自动注册AIXX账号**(免费,送¥5试用额度)并创建你的专属API Key
15
- 2. **自动写入本地配置**(~/.aixx/config.json)
16
- 3. **自动配置本地agent**(ZCode / Claude Code 检测到就自动配好,装完重启agent即可用)
17
- 4. **自动测试连通性**
18
-
19
- 带推荐码安装(推荐人获消费分润):`npx aixxai install zhangsan`
20
-
21
- 脚本/CI等非交互环境:`npx aixxai install --yes`
22
-
23
- **不需要手动export任何环境变量。**
24
-
25
- ## 配置读取顺序(了解即可)
26
-
27
- 所有AIXX功能(CLI命令、skill)按这个顺序找key,装完即用:
28
- 1. 环境变量 `AIXX_API_KEY`
29
- 2. `~/.aixx/config.json`(install自动写入,兜底主力)
30
- 3. `~/.aixx/account.json`(install自动注册时保存)
31
-
32
- 忘了key在哪?运行 `npx aixxai config` 查看(key脱敏显示),或直接看 `~/.aixx/` 目录。
33
-
34
- ## 验证
35
-
36
- 装完后,让你的 agent 说一句:
37
- > "帮我用 deepseek-chat 翻译:Hello World"
38
-
39
- 或运行 `npx aixxai test` 测连通性。
40
-
41
- ## 进阶:在代码里直接用(OpenAI兼容)
42
-
43
- AIXX 兼容 OpenAI 接口。base_url 和 key 在 `~/.aixx/config.json` 里能找到:
44
-
45
- ```python
46
- from openai import OpenAI
47
-
48
- client = OpenAI(
49
- api_key="sk-你的key",
50
- base_url="http://14.103.27.195:8080/v1"
51
- )
52
-
53
- response = client.chat.completions.create(
54
- model="deepseek-chat",
55
- messages=[{"role": "user", "content": "你好"}]
56
- )
57
- print(response.choices[0].message.content)
58
- ```
59
-
60
- ```javascript
61
- import OpenAI from "openai";
62
-
63
- const client = new OpenAI({
64
- apiKey: "sk-你的key",
65
- baseURL: "http://14.103.27.195:8080/v1",
66
- });
67
-
68
- const response = await client.chat.completions.create({
69
- model: "deepseek-chat",
70
- messages: [{ role: "user", content: "你好" }],
71
- });
72
- console.log(response.choices[0].message.content);
73
- ```
74
-
75
- > ⚠️ **安全提示**:当前后端用 HTTP 明文传输,存在凭据被嗅探风险,HTTPS 上线后请换用 https 地址。
76
-
77
- ## 常见问题
78
-
79
- **Q: 报错 "Unauthorized"**
80
- A: API Key 不对,重新运行 `npx aixxai install`,或检查 `~/.aixx/config.json` 里的key是否完整(含 `sk-` 前缀)。
81
-
82
- **Q: 报错 "insufficient quota"**
83
- A: 额度不足,运行 `npx aixxai recharge` 查看充值方式。
84
-
85
- **Q: 报错 "model price not configured"**
86
- A: 该模型未配置价格,联系管理员或换用 deepseek-chat。
87
-
88
- **Q: Windows下环境变量没生效?**
89
- A: 不影响使用——所有命令都会兜底读 `~/.aixx/config.json`。想配环境变量的话重开终端(setx写入的变量对新终端生效)。
90
-
91
- ## 下一步
92
-
93
- - 查看 [`./SKILL.md`](./SKILL.md) 了解完整能力
94
- - 查看 [`./references/chat.md`](./references/chat.md) 了解LLM调用细节
95
- - 查看 [`./references/data.md`](./references/data.md) 查社媒/电商数据
96
- - 查看 [`./references/apps.md`](./references/apps.md) 发通知/推送/邮件
97
- - 查看 [`./references/account.md`](./references/account.md) 查询用量
98
-
99
- ---
100
- 维护者:龙龙(AIXX PM)| 2026-08-08 建立 | 2026-08-14 更新:装完即用,删手动export(任务书A)
1
+ # AIXX 快速开始
2
+
3
+ **一条命令装完即用,30秒开始让AI调用万物。**
4
+
5
+ ---
6
+
7
+ ## 安装(唯一需要做的一步)
8
+
9
+ ```bash
10
+ npx aixxai install
11
+ ```
12
+
13
+ 装完自动完成:
14
+ 1. **自动注册AIXX账号**(免费,送¥5试用额度)并创建你的专属API Key
15
+ 2. **自动写入本地配置**(~/.aixx/config.json)
16
+ 3. **自动配置本地agent**(ZCode / Claude Code 检测到就自动配好,装完重启agent即可用)
17
+ 4. **自动测试连通性**
18
+
19
+ 带推荐码安装(推荐人获消费分润):`npx aixxai install zhangsan`
20
+
21
+ 脚本/CI等非交互环境:`npx aixxai install --yes`
22
+
23
+ **不需要手动export任何环境变量。**
24
+
25
+ ## 配置读取顺序(了解即可)
26
+
27
+ 所有AIXX功能(CLI命令、skill)按这个顺序找key,装完即用:
28
+ 1. 环境变量 `AIXX_API_KEY`
29
+ 2. `~/.aixx/config.json`(install自动写入,兜底主力)
30
+ 3. `~/.aixx/account.json`(install自动注册时保存)
31
+
32
+ 忘了key在哪?运行 `npx aixxai config` 查看(key脱敏显示),或直接看 `~/.aixx/` 目录。
33
+
34
+ ## 验证
35
+
36
+ 装完后,让你的 agent 说一句:
37
+ > "帮我用 deepseek-chat 翻译:Hello World"
38
+
39
+ 或运行 `npx aixxai test` 测连通性。
40
+
41
+ ## 进阶:在代码里直接用(OpenAI兼容)
42
+
43
+ AIXX 兼容 OpenAI 接口。base_url 和 key 在 `~/.aixx/config.json` 里能找到:
44
+
45
+ ```python
46
+ from openai import OpenAI
47
+
48
+ client = OpenAI(
49
+ api_key="sk-你的key",
50
+ base_url="http://14.103.27.195:8080/v1"
51
+ )
52
+
53
+ response = client.chat.completions.create(
54
+ model="deepseek-chat",
55
+ messages=[{"role": "user", "content": "你好"}]
56
+ )
57
+ print(response.choices[0].message.content)
58
+ ```
59
+
60
+ ```javascript
61
+ import OpenAI from "openai";
62
+
63
+ const client = new OpenAI({
64
+ apiKey: "sk-你的key",
65
+ baseURL: "http://14.103.27.195:8080/v1",
66
+ });
67
+
68
+ const response = await client.chat.completions.create({
69
+ model: "deepseek-chat",
70
+ messages: [{ role: "user", content: "你好" }],
71
+ });
72
+ console.log(response.choices[0].message.content);
73
+ ```
74
+
75
+ > ⚠️ **安全提示**:当前后端用 HTTP 明文传输,存在凭据被嗅探风险,HTTPS 上线后请换用 https 地址。
76
+
77
+ ## 常见问题
78
+
79
+ **Q: 报错 "Unauthorized"**
80
+ A: API Key 不对,重新运行 `npx aixxai install`,或检查 `~/.aixx/config.json` 里的key是否完整(含 `sk-` 前缀)。
81
+
82
+ **Q: 报错 "insufficient quota"**
83
+ A: 额度不足,运行 `npx aixxai recharge` 查看充值方式。
84
+
85
+ **Q: 报错 "model price not configured"**
86
+ A: 该模型未配置价格,联系管理员或换用 deepseek-chat。
87
+
88
+ **Q: Windows下环境变量没生效?**
89
+ A: 不影响使用——所有命令都会兜底读 `~/.aixx/config.json`。想配环境变量的话重开终端(setx写入的变量对新终端生效)。
90
+
91
+ ## 下一步
92
+
93
+ - 查看 [`./SKILL.md`](./SKILL.md) 了解完整能力
94
+ - 查看 [`./references/chat.md`](./references/chat.md) 了解LLM调用细节
95
+ - 查看 [`./references/data.md`](./references/data.md) 查社媒/电商数据
96
+ - 查看 [`./references/apps.md`](./references/apps.md) 发通知/推送/邮件
97
+ - 查看 [`./references/account.md`](./references/account.md) 查询用量
98
+
99
+ ---
100
+ 维护者:龙龙(AIXX PM)| 2026-08-08 建立 | 2026-08-14 更新:装完即用,删手动export(任务书A)