@jclaw/core 0.3.0 → 0.4.0
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/dist/cli/jclaw.js +126 -31
- package/package.json +3 -3
package/dist/cli/jclaw.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* JClaw CLI -
|
|
4
|
+
* JClaw CLI - Support flexible LLM configuration via environment variables
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { JClawAgent } from '../runtime/agent.js';
|
|
@@ -29,19 +29,21 @@ function showHelp() {
|
|
|
29
29
|
|
|
30
30
|
示例:
|
|
31
31
|
jclaw execute "分析我的简历,推荐公司和职位"
|
|
32
|
-
jclaw execute "读取 resume.pdf,生成 Cover Letter"
|
|
33
32
|
jclaw chat
|
|
34
33
|
|
|
35
34
|
环境变量:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
LLM_BASE_URL API 基础地址(可选,默认:https://api.openai.com/v1)
|
|
36
|
+
LLM_API_KEY API 密钥(必需)
|
|
37
|
+
LLM_MODEL_NAME 模型名称(可选,默认:gpt-4)
|
|
38
|
+
|
|
39
|
+
或使用 provider 特定变量:
|
|
40
|
+
OPENAI_API_KEY, INFINI_API_KEY, AZURE_OPENAI_KEY, ANTHROPIC_API_KEY
|
|
41
|
+
LLM_PROVIDER provider 名称(openai, infini-openai, azure-openai, anthropic)
|
|
39
42
|
`);
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
// 从配置文件加载 LLM 设置
|
|
43
46
|
async function loadLLMConfig() {
|
|
44
|
-
// 尝试加载配置文件
|
|
45
47
|
const configFiles = ['./jclaw.config.js', './jclaw.config.mjs'];
|
|
46
48
|
|
|
47
49
|
for (const configFile of configFiles) {
|
|
@@ -50,7 +52,6 @@ async function loadLLMConfig() {
|
|
|
50
52
|
const config = await import(configFile);
|
|
51
53
|
const { llm, providers } = config.default || config;
|
|
52
54
|
|
|
53
|
-
// 如果配置中指定了 provider
|
|
54
55
|
if (llm && llm.provider && providers && providers[llm.provider]) {
|
|
55
56
|
const provider = providers[llm.provider];
|
|
56
57
|
const apiKey = process.env[provider.apiKeyEnv];
|
|
@@ -67,7 +68,6 @@ async function loadLLMConfig() {
|
|
|
67
68
|
};
|
|
68
69
|
}
|
|
69
70
|
|
|
70
|
-
// 如果配置中有直接的 LLM 配置
|
|
71
71
|
if (llm && llm.apiKey) {
|
|
72
72
|
return {
|
|
73
73
|
apiBase: llm.apiBase || 'https://api.openai.com/v1',
|
|
@@ -81,28 +81,89 @@ async function loadLLMConfig() {
|
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
// 没有配置文件,使用环境变量
|
|
85
84
|
return null;
|
|
86
85
|
}
|
|
87
86
|
|
|
88
|
-
// 获取 LLM
|
|
87
|
+
// 获取 LLM 配置(支持多种环境变量组合)
|
|
89
88
|
async function getLLMConfig() {
|
|
90
89
|
// 1. 优先使用配置文件
|
|
91
90
|
const configFile = await loadLLMConfig();
|
|
92
91
|
if (configFile) return configFile;
|
|
93
92
|
|
|
94
|
-
// 2.
|
|
93
|
+
// 2. 使用通用环境变量(最灵活)
|
|
94
|
+
if (process.env.LLM_API_KEY) {
|
|
95
|
+
return {
|
|
96
|
+
apiBase: process.env.LLM_BASE_URL || 'https://api.openai.com/v1',
|
|
97
|
+
apiKey: process.env.LLM_API_KEY,
|
|
98
|
+
model: process.env.LLM_MODEL_NAME || 'gpt-4'
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 3. 使用 LLM_PROVIDER 指定 provider
|
|
103
|
+
if (process.env.LLM_PROVIDER) {
|
|
104
|
+
const providerConfigs = {
|
|
105
|
+
'openai': {
|
|
106
|
+
baseURL: 'https://api.openai.com/v1',
|
|
107
|
+
apiKeyEnv: 'OPENAI_API_KEY',
|
|
108
|
+
defaultModel: 'gpt-4'
|
|
109
|
+
},
|
|
110
|
+
'infini-openai': {
|
|
111
|
+
baseURL: 'https://cloud.infini-ai.com/maas/coding/v1',
|
|
112
|
+
apiKeyEnv: 'INFINI_API_KEY',
|
|
113
|
+
defaultModel: 'kimi-k2.5'
|
|
114
|
+
},
|
|
115
|
+
'azure-openai': {
|
|
116
|
+
baseURL: 'https://YOUR_RESOURCE.openai.azure.com',
|
|
117
|
+
apiKeyEnv: 'AZURE_OPENAI_KEY',
|
|
118
|
+
defaultModel: 'gpt-4'
|
|
119
|
+
},
|
|
120
|
+
'anthropic': {
|
|
121
|
+
baseURL: 'https://api.anthropic.com',
|
|
122
|
+
apiKeyEnv: 'ANTHROPIC_API_KEY',
|
|
123
|
+
defaultModel: 'claude-3-opus'
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const provider = providerConfigs[process.env.LLM_PROVIDER];
|
|
128
|
+
if (provider) {
|
|
129
|
+
const apiKey = process.env[provider.apiKeyEnv];
|
|
130
|
+
if (!apiKey) {
|
|
131
|
+
console.error(`❌ 错误:请设置 ${provider.apiKeyEnv} 环境变量`);
|
|
132
|
+
process.exit(1);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
apiBase: provider.baseURL,
|
|
137
|
+
apiKey: apiKey,
|
|
138
|
+
model: process.env.LLM_MODEL_NAME || provider.defaultModel
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 4. 使用默认 OpenAI 环境变量
|
|
95
144
|
if (process.env.OPENAI_API_KEY) {
|
|
96
145
|
return {
|
|
97
|
-
apiBase: process.env.
|
|
146
|
+
apiBase: process.env.LLM_BASE_URL || 'https://api.openai.com/v1',
|
|
98
147
|
apiKey: process.env.OPENAI_API_KEY,
|
|
99
|
-
model: process.env.
|
|
148
|
+
model: process.env.LLM_MODEL_NAME || 'gpt-4'
|
|
100
149
|
};
|
|
101
150
|
}
|
|
102
151
|
|
|
103
|
-
//
|
|
104
|
-
console.error('❌ 错误:请设置
|
|
105
|
-
console.error('
|
|
152
|
+
// 5. 错误提示
|
|
153
|
+
console.error('❌ 错误:请设置 LLM 配置');
|
|
154
|
+
console.error('');
|
|
155
|
+
console.error('方式 1: 使用通用环境变量(推荐)');
|
|
156
|
+
console.error(' export LLM_API_KEY=sk-...');
|
|
157
|
+
console.error(' export LLM_BASE_URL=https://api.openai.com/v1');
|
|
158
|
+
console.error(' export LLM_MODEL_NAME=gpt-4');
|
|
159
|
+
console.error('');
|
|
160
|
+
console.error('方式 2: 使用 provider 特定变量');
|
|
161
|
+
console.error(' export LLM_PROVIDER=infini-openai');
|
|
162
|
+
console.error(' export INFINI_API_KEY=sk-cp-...');
|
|
163
|
+
console.error(' export LLM_MODEL_NAME=kimi-k2.5');
|
|
164
|
+
console.error('');
|
|
165
|
+
console.error('方式 3: 使用配置文件');
|
|
166
|
+
console.error(' 创建 jclaw.config.js');
|
|
106
167
|
process.exit(1);
|
|
107
168
|
}
|
|
108
169
|
|
|
@@ -144,11 +205,6 @@ async function execute(prompt) {
|
|
|
144
205
|
async function chat() {
|
|
145
206
|
const llmConfig = await getLLMConfig();
|
|
146
207
|
|
|
147
|
-
if (!process.env.OPENAI_API_KEY && !llmConfig.apiKey) {
|
|
148
|
-
console.error('❌ 请设置 OPENAI_API_KEY 或使用配置文件');
|
|
149
|
-
process.exit(1);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
208
|
console.log('🧬 JClaw 交互模式 (输入 "exit" 退出)\n');
|
|
153
209
|
console.log(`📡 使用模型:${llmConfig.model}\n`);
|
|
154
210
|
|
|
@@ -227,12 +283,22 @@ export default {
|
|
|
227
283
|
'jclaw.config.js': configTemplate,
|
|
228
284
|
'README.md': `# JClaw Project
|
|
229
285
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
286
|
+
## 使用环境变量
|
|
287
|
+
|
|
288
|
+
### 方式 1: 通用环境变量(推荐)
|
|
289
|
+
export LLM_API_KEY=sk-...
|
|
290
|
+
export LLM_BASE_URL=https://api.openai.com/v1
|
|
291
|
+
export LLM_MODEL_NAME=gpt-4
|
|
292
|
+
jclaw "你的任务"
|
|
293
|
+
|
|
294
|
+
### 方式 2: Provider 特定变量
|
|
295
|
+
export LLM_PROVIDER=infini-openai
|
|
296
|
+
export INFINI_API_KEY=sk-cp-...
|
|
297
|
+
export LLM_MODEL_NAME=kimi-k2.5
|
|
298
|
+
jclaw "你的任务"
|
|
233
299
|
|
|
234
|
-
|
|
235
|
-
|
|
300
|
+
## 或使用配置文件
|
|
301
|
+
jclaw "你的任务"
|
|
236
302
|
`
|
|
237
303
|
};
|
|
238
304
|
|
|
@@ -244,12 +310,42 @@ export default {
|
|
|
244
310
|
}
|
|
245
311
|
|
|
246
312
|
console.log('\n🎉 初始化完成!\n');
|
|
247
|
-
console.log('下一步:');
|
|
248
|
-
console.log(' 1. 编辑 jclaw.config.js 配置你的 LLM');
|
|
249
|
-
console.log(' 2. export OPENAI_API_KEY=sk-...');
|
|
250
|
-
console.log(' 3. jclaw execute "你的任务"');
|
|
251
313
|
}
|
|
252
314
|
|
|
315
|
+
// 主逻辑
|
|
316
|
+
switch (command) {
|
|
317
|
+
case 'execute':
|
|
318
|
+
case 'exec':
|
|
319
|
+
case 'e':
|
|
320
|
+
const prompt = args.slice(1).join(' ');
|
|
321
|
+
if (!prompt) {
|
|
322
|
+
console.error('❌ 错误:请提供任务描述');
|
|
323
|
+
console.error('示例:jclaw execute "分析我的简历"');
|
|
324
|
+
process.exit(1);
|
|
325
|
+
}
|
|
326
|
+
execute(prompt);
|
|
327
|
+
break;
|
|
328
|
+
|
|
329
|
+
case 'chat':
|
|
330
|
+
case 'c':
|
|
331
|
+
chat();
|
|
332
|
+
break;
|
|
333
|
+
|
|
334
|
+
case 'init':
|
|
335
|
+
case 'i':
|
|
336
|
+
init();
|
|
337
|
+
break;
|
|
338
|
+
|
|
339
|
+
case 'help':
|
|
340
|
+
case 'h':
|
|
341
|
+
case '--help':
|
|
342
|
+
case '-h':
|
|
343
|
+
showHelp();
|
|
344
|
+
break;
|
|
345
|
+
|
|
346
|
+
default:
|
|
347
|
+
if (command)
|
|
348
|
+
|
|
253
349
|
// 主逻辑
|
|
254
350
|
switch (command) {
|
|
255
351
|
case 'execute':
|
|
@@ -283,7 +379,6 @@ switch (command) {
|
|
|
283
379
|
|
|
284
380
|
default:
|
|
285
381
|
if (command) {
|
|
286
|
-
// 如果没有命令,但有参数,当作 execute 处理
|
|
287
382
|
execute(args.join(' '));
|
|
288
383
|
} else {
|
|
289
384
|
showHelp();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jclaw/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Universal self-evolving Agent framework with
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Universal self-evolving Agent framework with flexible LLM config",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"files": ["dist", "README.md"],
|
|
18
18
|
"scripts": {"build": "tsc"},
|
|
19
|
-
"keywords": ["ai", "agent", "
|
|
19
|
+
"keywords": ["ai", "agent", "cli", "llm"],
|
|
20
20
|
"author": "JClaw Team",
|
|
21
21
|
"license": "MIT"
|
|
22
22
|
}
|