english-optimizer-cli 1.3.0 โ 1.5.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/README.md +10 -0
- package/dist/ai/api-provider.js +38 -7
- package/dist/config/test.js +108 -0
- package/dist/index.js +13 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -220,8 +220,18 @@ fuck-abc config
|
|
|
220
220
|
|
|
221
221
|
# Run interactive setup wizard
|
|
222
222
|
fuck-abc config --setup
|
|
223
|
+
|
|
224
|
+
# Test API configuration
|
|
225
|
+
fuck-abc test
|
|
223
226
|
```
|
|
224
227
|
|
|
228
|
+
The test command will:
|
|
229
|
+
|
|
230
|
+
- Verify your API configuration is valid
|
|
231
|
+
- Test connection to the API provider
|
|
232
|
+
- Run a simple optimization test
|
|
233
|
+
- Show detailed error messages if something is wrong
|
|
234
|
+
|
|
225
235
|
The setup wizard will guide you through:
|
|
226
236
|
|
|
227
237
|
- Choosing AI provider (local Ollama or cloud API)
|
package/dist/ai/api-provider.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ApiProvider = void 0;
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
9
|
const templates_1 = require("../prompts/templates");
|
|
9
10
|
class ApiProvider {
|
|
10
11
|
constructor(config) {
|
|
@@ -25,14 +26,14 @@ class ApiProvider {
|
|
|
25
26
|
max_tokens: 2000,
|
|
26
27
|
}, {
|
|
27
28
|
headers: {
|
|
28
|
-
|
|
29
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
29
30
|
'Content-Type': 'application/json',
|
|
30
31
|
},
|
|
31
32
|
timeout: 60000, // 60 seconds timeout
|
|
32
33
|
});
|
|
33
34
|
if (response.data && response.data.choices && response.data.choices.length > 0) {
|
|
34
35
|
let result = response.data.choices[0].message.content.trim();
|
|
35
|
-
// Remove quotes if
|
|
36
|
+
// Remove quotes if model wrapped the response in them
|
|
36
37
|
if (result.startsWith('"') && result.endsWith('"')) {
|
|
37
38
|
result = result.slice(1, -1);
|
|
38
39
|
}
|
|
@@ -52,6 +53,11 @@ class ApiProvider {
|
|
|
52
53
|
const axiosError = error;
|
|
53
54
|
if (axiosError.response) {
|
|
54
55
|
const status = axiosError.response.status;
|
|
56
|
+
const data = axiosError.response.data;
|
|
57
|
+
// Handle GLM specific errors
|
|
58
|
+
if (data?.error?.code === '1113') {
|
|
59
|
+
throw new Error('GLM account balance is insufficient. Please recharge your account at https://open.bigmodel.cn/usercenter/finance');
|
|
60
|
+
}
|
|
55
61
|
if (status === 401) {
|
|
56
62
|
throw new Error('Invalid API key. Please check your API credentials.');
|
|
57
63
|
}
|
|
@@ -78,14 +84,14 @@ class ApiProvider {
|
|
|
78
84
|
max_tokens: 2000,
|
|
79
85
|
}, {
|
|
80
86
|
headers: {
|
|
81
|
-
|
|
87
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
82
88
|
'Content-Type': 'application/json',
|
|
83
89
|
},
|
|
84
90
|
timeout: 60000,
|
|
85
91
|
});
|
|
86
92
|
if (response.data && response.data.choices && response.data.choices.length > 0) {
|
|
87
93
|
let result = response.data.choices[0].message.content.trim();
|
|
88
|
-
// Remove quotes if
|
|
94
|
+
// Remove quotes if model wrapped the response in them
|
|
89
95
|
if (result.startsWith('"') && result.endsWith('"')) {
|
|
90
96
|
result = result.slice(1, -1);
|
|
91
97
|
}
|
|
@@ -98,6 +104,11 @@ class ApiProvider {
|
|
|
98
104
|
const axiosError = error;
|
|
99
105
|
if (axiosError.response) {
|
|
100
106
|
const status = axiosError.response.status;
|
|
107
|
+
const data = axiosError.response.data;
|
|
108
|
+
// Handle GLM specific errors
|
|
109
|
+
if (data?.error?.code === '1113') {
|
|
110
|
+
throw new Error('GLM account balance is insufficient. Please recharge your account at https://open.bigmodel.cn/usercenter/finance');
|
|
111
|
+
}
|
|
101
112
|
if (status === 401) {
|
|
102
113
|
throw new Error('Invalid API key. Please check your API credentials.');
|
|
103
114
|
}
|
|
@@ -115,21 +126,41 @@ class ApiProvider {
|
|
|
115
126
|
return false;
|
|
116
127
|
}
|
|
117
128
|
try {
|
|
118
|
-
// Try a simple API call to check if
|
|
129
|
+
// Try a simple API call to check if key is valid
|
|
119
130
|
const response = await axios_1.default.post(`${this.config.baseUrl}/chat/completions`, {
|
|
120
131
|
model: this.config.model,
|
|
121
132
|
messages: [{ role: 'user', content: 'test' }],
|
|
122
133
|
max_tokens: 5,
|
|
123
134
|
}, {
|
|
124
135
|
headers: {
|
|
125
|
-
|
|
136
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
126
137
|
'Content-Type': 'application/json',
|
|
127
138
|
},
|
|
128
139
|
timeout: 10000,
|
|
129
140
|
});
|
|
130
141
|
return response.status === 200;
|
|
131
142
|
}
|
|
132
|
-
catch {
|
|
143
|
+
catch (error) {
|
|
144
|
+
if (error.response) {
|
|
145
|
+
console.log('\n' + chalk_1.default.yellow('API connection failed with status:'), error.response.status);
|
|
146
|
+
if (error.response.data) {
|
|
147
|
+
console.log(chalk_1.default.yellow('Error details:'), JSON.stringify(error.response.data));
|
|
148
|
+
// Throw error for GLM balance issues
|
|
149
|
+
if (error.response.data?.error?.code === '1113') {
|
|
150
|
+
throw new Error('GLM account balance is insufficient. Please recharge your account at https://open.bigmodel.cn/usercenter/finance');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else if (error.request) {
|
|
155
|
+
console.log('\n' + chalk_1.default.yellow('No response from API'));
|
|
156
|
+
console.log(chalk_1.default.gray('Possible issues:'));
|
|
157
|
+
console.log(chalk_1.default.gray(' - Network connectivity'));
|
|
158
|
+
console.log(chalk_1.default.gray(' - API URL is incorrect'));
|
|
159
|
+
console.log(chalk_1.default.gray(' - Firewall blocking the request'));
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
console.log('\n' + chalk_1.default.yellow('Request setup error:'), error.message);
|
|
163
|
+
}
|
|
133
164
|
return false;
|
|
134
165
|
}
|
|
135
166
|
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.testConfiguration = testConfiguration;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const config_1 = require("./config");
|
|
9
|
+
const provider_1 = require("../ai/provider");
|
|
10
|
+
async function testConfiguration() {
|
|
11
|
+
console.log(chalk_1.default.cyan.bold('\n๐งช Testing API Configuration...\n'));
|
|
12
|
+
const config = config_1.configManager.getConfig();
|
|
13
|
+
try {
|
|
14
|
+
const provider = (0, provider_1.createProvider)(config);
|
|
15
|
+
console.log(chalk_1.default.gray('Provider:'), chalk_1.default.white.bold(config.ai.provider));
|
|
16
|
+
if (config.ai.provider === 'ollama') {
|
|
17
|
+
console.log(chalk_1.default.gray('Ollama URL:'), chalk_1.default.white(config.ai.ollama.baseUrl));
|
|
18
|
+
console.log(chalk_1.default.gray('Ollama Model:'), chalk_1.default.white(config.ai.ollama.model));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
console.log(chalk_1.default.gray('API Provider:'), chalk_1.default.white.bold(config.ai.api.provider));
|
|
22
|
+
console.log(chalk_1.default.gray('API URL:'), chalk_1.default.white(config.ai.api.baseUrl));
|
|
23
|
+
console.log(chalk_1.default.gray('API Model:'), chalk_1.default.white(config.ai.api.model));
|
|
24
|
+
console.log(chalk_1.default.gray('API Key:'), chalk_1.default.white(config.ai.api.apiKey.substring(0, 8) + '...'));
|
|
25
|
+
}
|
|
26
|
+
console.log(chalk_1.default.gray('\n' + 'โ'.repeat(50) + '\n'));
|
|
27
|
+
console.log(chalk_1.default.cyan('๐ Testing connection...\n'));
|
|
28
|
+
let isAvailable = false;
|
|
29
|
+
try {
|
|
30
|
+
isAvailable = await provider.isAvailable();
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
if (e.message.includes('balance is insufficient')) {
|
|
34
|
+
console.log(chalk_1.default.red.bold('\nโ GLM Account Balance Issue!\n'));
|
|
35
|
+
console.log(chalk_1.default.yellow('Your GLM account has insufficient balance.\n'));
|
|
36
|
+
console.log(chalk_1.default.cyan('๐ก To fix:'));
|
|
37
|
+
console.log(chalk_1.default.white(' 1. Visit https://open.bigmodel.cn/usercenter/finance'));
|
|
38
|
+
console.log(chalk_1.default.white(' 2. Recharge your account'));
|
|
39
|
+
console.log(chalk_1.default.white(' 3. Run test again: fuck-abc test\n'));
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
if (isAvailable) {
|
|
45
|
+
console.log(chalk_1.default.green.bold('โ
API configuration is valid!\n'));
|
|
46
|
+
if (config.ai.provider === 'api') {
|
|
47
|
+
console.log(chalk_1.default.gray('Attempting a simple test request...\n'));
|
|
48
|
+
try {
|
|
49
|
+
const testText = 'Hello';
|
|
50
|
+
const optimized = await provider.optimize(testText, 'professional');
|
|
51
|
+
console.log(chalk_1.default.cyan('Test Input:'), chalk_1.default.white(testText));
|
|
52
|
+
console.log(chalk_1.default.cyan('Test Output:'), chalk_1.default.white(optimized));
|
|
53
|
+
console.log('\n' + chalk_1.default.green.bold('โ
Full test passed! Your API is working correctly.\n'));
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.log(chalk_1.default.yellow('\nโ ๏ธ Connection successful but request failed:'));
|
|
57
|
+
console.log(chalk_1.default.gray(error.message));
|
|
58
|
+
console.log('\n' + chalk_1.default.cyan('Possible issues:'));
|
|
59
|
+
console.log(chalk_1.default.gray(' - API key may be invalid or expired'));
|
|
60
|
+
console.log(chalk_1.default.gray(' - Model may not be available'));
|
|
61
|
+
console.log(chalk_1.default.gray(' - API endpoint may have changed'));
|
|
62
|
+
console.log('\n' + chalk_1.default.cyan('Please check your API configuration:'));
|
|
63
|
+
console.log(chalk_1.default.white(' fuck-abc config --setup\n'));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
console.log(chalk_1.default.green.bold('๐ Your local Ollama is ready!\n'));
|
|
68
|
+
console.log(chalk_1.default.gray('You can start using:'));
|
|
69
|
+
console.log(chalk_1.default.white.bold(' fuck-abc\n'));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
console.log(chalk_1.default.red.bold('โ API configuration is invalid!\n'));
|
|
74
|
+
if (config.ai.provider === 'ollama') {
|
|
75
|
+
console.log(chalk_1.default.red('Issues:'));
|
|
76
|
+
console.log(chalk_1.default.gray(' - Ollama is not running'));
|
|
77
|
+
console.log(chalk_1.default.gray(' - Ollama URL is incorrect'));
|
|
78
|
+
console.log('\n' + chalk_1.default.cyan('To start Ollama:'));
|
|
79
|
+
console.log(chalk_1.default.gray(' docker-compose up -d'));
|
|
80
|
+
console.log(chalk_1.default.gray('\nOr install Ollama locally:'));
|
|
81
|
+
console.log(chalk_1.default.gray(' https://ollama.com/download'));
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
console.log(chalk_1.default.red('Issues:'));
|
|
85
|
+
console.log(chalk_1.default.gray(' - API key is invalid or missing'));
|
|
86
|
+
console.log(chalk_1.default.gray(' - API URL is incorrect'));
|
|
87
|
+
console.log(chalk_1.default.gray(' - Network connection failed'));
|
|
88
|
+
console.log('\n' + chalk_1.default.cyan('Please check your configuration:'));
|
|
89
|
+
console.log(chalk_1.default.white(' fuck-abc config --setup'));
|
|
90
|
+
console.log(chalk_1.default.gray('\nOr view current config:'));
|
|
91
|
+
console.log(chalk_1.default.white(' fuck-abc config\n'));
|
|
92
|
+
}
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.log(chalk_1.default.red.bold('\nโ Configuration error!\n'));
|
|
98
|
+
console.log(chalk_1.default.gray(error.message));
|
|
99
|
+
if (error.message.includes('API key')) {
|
|
100
|
+
console.log('\n' + chalk_1.default.cyan('๐ก Tip: Make sure your API key is correct and active.'));
|
|
101
|
+
console.log(chalk_1.default.gray(' For OpenAI: https://platform.openai.com/api-keys'));
|
|
102
|
+
console.log(chalk_1.default.gray(' For GLM: https://open.bigmodel.cn/usercenter/apikeys'));
|
|
103
|
+
console.log(chalk_1.default.gray(' For DeepSeek: https://platform.deepseek.com/\n'));
|
|
104
|
+
}
|
|
105
|
+
process.exit(1);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=test.js.map
|
package/dist/index.js
CHANGED
|
@@ -321,5 +321,18 @@ program
|
|
|
321
321
|
process.exit(1);
|
|
322
322
|
}
|
|
323
323
|
});
|
|
324
|
+
program
|
|
325
|
+
.command('test')
|
|
326
|
+
.description('Test API configuration and connection')
|
|
327
|
+
.action(async () => {
|
|
328
|
+
try {
|
|
329
|
+
const { testConfiguration } = await Promise.resolve().then(() => __importStar(require('./config/test')));
|
|
330
|
+
await testConfiguration();
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
(0, display_1.displayError)(error);
|
|
334
|
+
process.exit(1);
|
|
335
|
+
}
|
|
336
|
+
});
|
|
324
337
|
program.parse();
|
|
325
338
|
//# sourceMappingURL=index.js.map
|