english-optimizer-cli 1.4.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/dist/ai/api-provider.js +38 -7
- package/dist/config/test.js +16 -1
- package/package.json +1 -1
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
|
}
|
package/dist/config/test.js
CHANGED
|
@@ -25,7 +25,22 @@ async function testConfiguration() {
|
|
|
25
25
|
}
|
|
26
26
|
console.log(chalk_1.default.gray('\n' + 'ā'.repeat(50) + '\n'));
|
|
27
27
|
console.log(chalk_1.default.cyan('š Testing connection...\n'));
|
|
28
|
-
|
|
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
|
+
}
|
|
29
44
|
if (isAvailable) {
|
|
30
45
|
console.log(chalk_1.default.green.bold('ā
API configuration is valid!\n'));
|
|
31
46
|
if (config.ai.provider === 'api') {
|