coding-tool-x 3.3.2 → 3.3.3
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/package.json
CHANGED
|
@@ -31,6 +31,17 @@ function sanitizeChannel(channel) {
|
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
function selectLatestEnabledChannel(channels) {
|
|
35
|
+
if (!Array.isArray(channels) || channels.length === 0) return null;
|
|
36
|
+
const enabledChannels = channels.filter(ch => ch.enabled !== false);
|
|
37
|
+
if (enabledChannels.length === 0) return null;
|
|
38
|
+
return enabledChannels.reduce((latest, current) => {
|
|
39
|
+
const latestTs = Number(latest?.updatedAt || latest?.createdAt || 0);
|
|
40
|
+
const currentTs = Number(current?.updatedAt || current?.createdAt || 0);
|
|
41
|
+
return currentTs > latestTs ? current : latest;
|
|
42
|
+
}, enabledChannels[0]);
|
|
43
|
+
}
|
|
44
|
+
|
|
34
45
|
// 保存激活渠道ID
|
|
35
46
|
function saveActiveChannelId(channelId) {
|
|
36
47
|
ensureStorageDirMigrated();
|
|
@@ -171,9 +182,10 @@ router.post('/stop', async (req, res) => {
|
|
|
171
182
|
// 1. 获取当前激活渠道(优先使用启动动态切换时记录的渠道ID)
|
|
172
183
|
const { channels } = getChannels();
|
|
173
184
|
const activeChannelId = loadActiveChannelId();
|
|
174
|
-
let activeChannel =
|
|
175
|
-
|
|
176
|
-
|
|
185
|
+
let activeChannel = selectLatestEnabledChannel(channels);
|
|
186
|
+
if (!activeChannel && activeChannelId) {
|
|
187
|
+
activeChannel = channels.find(ch => ch.id === activeChannelId);
|
|
188
|
+
}
|
|
177
189
|
if (!activeChannel) {
|
|
178
190
|
const enabledChannels = channels.filter(ch => ch.enabled !== false);
|
|
179
191
|
activeChannel = enabledChannels[0] || channels[0] || null;
|
|
@@ -26,6 +26,17 @@ function sanitizeChannel(channel) {
|
|
|
26
26
|
return rest;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
function selectLatestEnabledChannel(channels) {
|
|
30
|
+
if (!Array.isArray(channels) || channels.length === 0) return null;
|
|
31
|
+
const enabledChannels = channels.filter(ch => ch.enabled !== false);
|
|
32
|
+
if (enabledChannels.length === 0) return null;
|
|
33
|
+
return enabledChannels.reduce((latest, current) => {
|
|
34
|
+
const latestTs = Number(latest?.updatedAt || latest?.createdAt || 0);
|
|
35
|
+
const currentTs = Number(current?.updatedAt || current?.createdAt || 0);
|
|
36
|
+
return currentTs > latestTs ? current : latest;
|
|
37
|
+
}, enabledChannels[0]);
|
|
38
|
+
}
|
|
39
|
+
|
|
29
40
|
// 保存激活渠道ID
|
|
30
41
|
function saveActiveChannelId(channelId) {
|
|
31
42
|
ensureStorageDirMigrated();
|
|
@@ -156,9 +167,10 @@ router.post('/stop', async (req, res) => {
|
|
|
156
167
|
// 1. 获取当前激活渠道(优先使用启动动态切换时记录的渠道ID)
|
|
157
168
|
const { channels } = getChannels();
|
|
158
169
|
const activeChannelId = loadActiveChannelId();
|
|
159
|
-
let activeChannel =
|
|
160
|
-
|
|
161
|
-
|
|
170
|
+
let activeChannel = selectLatestEnabledChannel(channels);
|
|
171
|
+
if (!activeChannel && activeChannelId) {
|
|
172
|
+
activeChannel = channels.find(ch => ch.id === activeChannelId);
|
|
173
|
+
}
|
|
162
174
|
if (!activeChannel) {
|
|
163
175
|
const enabledChannels = channels.filter(ch => ch.enabled !== false);
|
|
164
176
|
activeChannel = enabledChannels[0] || channels[0] || null;
|
|
@@ -31,6 +31,17 @@ function sanitizeChannel(channel) {
|
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
function selectLatestEnabledChannel(channels) {
|
|
35
|
+
if (!Array.isArray(channels) || channels.length === 0) return null;
|
|
36
|
+
const enabledChannels = channels.filter(ch => ch.enabled !== false);
|
|
37
|
+
if (enabledChannels.length === 0) return null;
|
|
38
|
+
return enabledChannels.reduce((latest, current) => {
|
|
39
|
+
const latestTs = Number(latest?.updatedAt || latest?.createdAt || 0);
|
|
40
|
+
const currentTs = Number(current?.updatedAt || current?.createdAt || 0);
|
|
41
|
+
return currentTs > latestTs ? current : latest;
|
|
42
|
+
}, enabledChannels[0]);
|
|
43
|
+
}
|
|
44
|
+
|
|
34
45
|
// 保存激活渠道ID
|
|
35
46
|
function saveActiveChannelId(channelId) {
|
|
36
47
|
ensureStorageDirMigrated();
|
|
@@ -191,9 +202,10 @@ router.post('/stop', async (req, res) => {
|
|
|
191
202
|
// 1. 获取当前激活渠道(优先使用启动动态切换时记录的渠道ID)
|
|
192
203
|
const { channels } = getChannels();
|
|
193
204
|
const activeChannelId = loadActiveChannelId();
|
|
194
|
-
let activeChannel =
|
|
195
|
-
|
|
196
|
-
|
|
205
|
+
let activeChannel = selectLatestEnabledChannel(channels);
|
|
206
|
+
if (!activeChannel && activeChannelId) {
|
|
207
|
+
activeChannel = channels.find(ch => ch.id === activeChannelId);
|
|
208
|
+
}
|
|
197
209
|
if (!activeChannel) {
|
|
198
210
|
const enabledChannels = channels.filter(ch => ch.enabled !== false);
|
|
199
211
|
activeChannel = enabledChannels[0] || channels[0] || null;
|
package/src/server/api/proxy.js
CHANGED
|
@@ -27,6 +27,17 @@ function sanitizeChannelForResponse(channel) {
|
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
function selectLatestEnabledChannel(channels) {
|
|
31
|
+
if (!Array.isArray(channels) || channels.length === 0) return null;
|
|
32
|
+
const enabledChannels = channels.filter(ch => ch.enabled !== false);
|
|
33
|
+
if (enabledChannels.length === 0) return null;
|
|
34
|
+
return enabledChannels.reduce((latest, current) => {
|
|
35
|
+
const latestTs = Number(latest?.updatedAt || latest?.createdAt || 0);
|
|
36
|
+
const currentTs = Number(current?.updatedAt || current?.createdAt || 0);
|
|
37
|
+
return currentTs > latestTs ? current : latest;
|
|
38
|
+
}, enabledChannels[0]);
|
|
39
|
+
}
|
|
40
|
+
|
|
30
41
|
// 保存激活渠道ID
|
|
31
42
|
function saveActiveChannelId(channelId) {
|
|
32
43
|
ensureStorageDirMigrated();
|
|
@@ -210,6 +221,9 @@ router.post('/start', async (req, res) => {
|
|
|
210
221
|
// 停止代理
|
|
211
222
|
router.post('/stop', async (req, res) => {
|
|
212
223
|
try {
|
|
224
|
+
const channelsBeforeStop = getAllChannels();
|
|
225
|
+
const latestEnabledChannel = selectLatestEnabledChannel(channelsBeforeStop);
|
|
226
|
+
|
|
213
227
|
// 1. 停止代理服务器
|
|
214
228
|
const proxyResult = await stopProxyServer();
|
|
215
229
|
const activeChannelId = loadActiveChannelId();
|
|
@@ -230,6 +244,10 @@ router.post('/stop', async (req, res) => {
|
|
|
230
244
|
ch.baseUrl === currentSettings.baseUrl && ch.apiKey === currentSettings.apiKey
|
|
231
245
|
);
|
|
232
246
|
}
|
|
247
|
+
// Fallback: keep latest enabled channel when leaving dynamic switching mode
|
|
248
|
+
if (!restoredChannel && latestEnabledChannel) {
|
|
249
|
+
restoredChannel = channels.find(ch => ch.id === latestEnabledChannel.id) || latestEnabledChannel;
|
|
250
|
+
}
|
|
233
251
|
// Fallback: use previously active channel id
|
|
234
252
|
if (!restoredChannel && activeChannelId) {
|
|
235
253
|
restoredChannel = channels.find(ch => ch.id === activeChannelId);
|
|
@@ -242,9 +260,12 @@ router.post('/stop', async (req, res) => {
|
|
|
242
260
|
// 没有备份,选择权重最高的启用渠道
|
|
243
261
|
const { getBestChannelForRestore, updateClaudeSettings } = require('../services/channels');
|
|
244
262
|
const channels = getAllChannels();
|
|
245
|
-
restoredChannel =
|
|
246
|
-
? channels.find(ch => ch.id ===
|
|
263
|
+
restoredChannel = latestEnabledChannel
|
|
264
|
+
? channels.find(ch => ch.id === latestEnabledChannel.id)
|
|
247
265
|
: null;
|
|
266
|
+
if (!restoredChannel && activeChannelId) {
|
|
267
|
+
restoredChannel = channels.find(ch => ch.id === activeChannelId);
|
|
268
|
+
}
|
|
248
269
|
if (!restoredChannel) {
|
|
249
270
|
restoredChannel = getBestChannelForRestore();
|
|
250
271
|
}
|