coding-tool-x 3.9.5-261014 → 3.9.6-261016
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/web/assets/{AgentsDrawer-K97azdxi.js → AgentsDrawer-D2oYKcOj.js} +1 -1
- package/dist/web/assets/{Analytics-CNPDGLNe.js → Analytics-Cy3QSSrn.js} +1 -1
- package/dist/web/assets/{AssetPathField-BzwSZryS.js → AssetPathField-BGuw5ZR7.js} +1 -1
- package/dist/web/assets/{ChatHistoryDrawer-BVbfqK-u.js → ChatHistoryDrawer-BZ8FY_-9.js} +1 -1
- package/dist/web/assets/{CollapseItem-DgBQWlsJ.js → CollapseItem-DOoxtmtj.js} +1 -1
- package/dist/web/assets/{CommandsDrawer-885BtWjs.js → CommandsDrawer-YCgZAeNx.js} +1 -1
- package/dist/web/assets/{ConfigExportDrawer-jn00iZWm.js → ConfigExportDrawer-BlAHQidK.js} +1 -1
- package/dist/web/assets/{ConfigTemplates-DoUG3mOo.js → ConfigTemplates-C6qSF4Ht.js} +1 -1
- package/dist/web/assets/{ConfigTemplatesDrawer-BQ8h43E6.js → ConfigTemplatesDrawer-lRT_6-jD.js} +1 -1
- package/dist/web/assets/{ConfigTemplatesPanel-VCghYqMx.js → ConfigTemplatesPanel-DrS57Y8V.js} +1 -1
- package/dist/web/assets/{FavoritesDrawer-CKjlAe4O.js → FavoritesDrawer-CuOcHtqM.js} +1 -1
- package/dist/web/assets/{GatewayConvertDrawer-Cin2yotJ.js → GatewayConvertDrawer-b0pIvH02.js} +1 -1
- package/dist/web/assets/{Home-ui0qANow.js → Home-DvfQcP4b.js} +1 -1
- package/dist/web/assets/{MarkdownEditor-LiZaVnJu.js → MarkdownEditor-BLS6HmBb.js} +1 -1
- package/dist/web/assets/{McpDrawer-ClaSJOLK.js → McpDrawer-BHt7zFf0.js} +1 -1
- package/dist/web/assets/{McpFormDrawer-w1EJUIsa.js → McpFormDrawer-DpTMHc-e.js} +1 -1
- package/dist/web/assets/{PluginManager-C_Yr9wyj.js → PluginManager-CYAuy5dm.js} +1 -1
- package/dist/web/assets/{PluginsDrawer-CUnoai6T.js → PluginsDrawer-CoyprWNl.js} +1 -1
- package/dist/web/assets/{PluginsPanel-CNShDbDF.js → PluginsPanel-C3AzXlkH.js} +1 -1
- package/dist/web/assets/{ProjectList-CZ2ZwKRn.js → ProjectList-pJo0xp0Q.js} +1 -1
- package/dist/web/assets/{PromptsDrawer-BhB_q-KZ.js → PromptsDrawer-CM-irN84.js} +1 -1
- package/dist/web/assets/{RecentSessionsDrawer-BZ8YKqwq.js → RecentSessionsDrawer-l5nO_5bS.js} +1 -1
- package/dist/web/assets/{SessionCard-CEFt-CNZ.js → SessionCard-BqTFOt05.js} +1 -1
- package/dist/web/assets/{SessionList-CTO4NiMY.js → SessionList-Bs_YFKW5.js} +1 -1
- package/dist/web/assets/{SettingsDrawer-BiOXu9lZ.js → SettingsDrawer-COszrj_Y.js} +1 -1
- package/dist/web/assets/{SkillManager-Bf87paMe.js → SkillManager-DgLhL2Qv.js} +1 -1
- package/dist/web/assets/{SkillsDrawer-BwkyaeRk.js → SkillsDrawer-CT69YSEE.js} +1 -1
- package/dist/web/assets/{SkillsPanel-obdDEThm.js → SkillsPanel-Dm8FkYD6.js} +1 -1
- package/dist/web/assets/{Upload-DdjJgjTW.js → Upload-BHTymsxe.js} +1 -1
- package/dist/web/assets/{WorkspaceDrawer-UWKpEUXI.js → WorkspaceDrawer-DLXh_c7u.js} +1 -1
- package/dist/web/assets/{WorkspaceManager-rKzSKzKC.js → WorkspaceManager-E_IoRTlC.js} +1 -1
- package/dist/web/assets/{WorkspacePanel-BCMcITVD.js → WorkspacePanel-DLlTIU8Y.js} +1 -1
- package/dist/web/assets/index-DP0Ggf-8.js +2 -0
- package/dist/web/assets/{index-DvoMGWQv.css → index-DjIuAtaS.css} +1 -1
- package/dist/web/assets/{platforms-JV36QBCw.js → platforms-DNVEAtqx.js} +1 -1
- package/dist/web/assets/platforms-p9AiybLU.js +1 -0
- package/dist/web/assets/{sessions-wvj5oaYr.js → sessions-DJiJJgvg.js} +1 -1
- package/dist/web/assets/sessions-DSLpMXCq.js +1 -0
- package/dist/web/assets/{useResponsiveDrawer-Cn6gH3VG.js → useResponsiveDrawer-D9ti7g-U.js} +1 -1
- package/dist/web/index.html +4 -4
- package/package.json +1 -1
- package/src/commands/channels.js +88 -95
- package/src/commands/toggle-proxy.js +26 -19
- package/src/platforms/drivers/claude/channels.js +11 -1
- package/src/platforms/drivers/claude/proxy.js +1 -2
- package/src/platforms/drivers/codex/channels.js +14 -2
- package/src/platforms/drivers/codex/proxy.js +1 -2
- package/src/platforms/drivers/dsh/api-operations.js +1 -1
- package/src/platforms/drivers/dsh/channels.js +3 -7
- package/src/platforms/drivers/dsh/proxy.js +0 -4
- package/src/platforms/drivers/gemini/channels.js +10 -12
- package/src/platforms/drivers/gemini/proxy.js +1 -2
- package/src/platforms/drivers/omp/channels.js +53 -13
- package/src/platforms/drivers/omp/gateway-routing.js +69 -33
- package/src/platforms/drivers/omp/gateway.js +102 -40
- package/src/platforms/drivers/omp/proxy.js +1 -2
- package/src/platforms/drivers/opencode/channels.js +53 -24
- package/src/platforms/drivers/opencode/proxy.js +1 -2
- package/src/platforms/manifest-schema.js +53 -1
- package/src/platforms/manifests/claude.json +11 -0
- package/src/platforms/manifests/codex.json +11 -16
- package/src/platforms/manifests/dsh.json +13 -0
- package/src/platforms/manifests/gemini.json +17 -16
- package/src/platforms/manifests/omp.json +6 -16
- package/src/platforms/manifests/opencode.json +21 -16
- package/src/server/api/platforms.js +29 -0
- package/src/server/services/memory-pressure.js +101 -0
- package/src/shared/channel-driver.js +24 -14
- package/dist/web/assets/index-aW6itxZg.js +0 -2
- package/dist/web/assets/platforms-DILqyP72.js +0 -1
- package/dist/web/assets/sessions-WXODMvLY.js +0 -1
|
@@ -3,28 +3,68 @@
|
|
|
3
3
|
const { createChannelDriver } = require('../../../shared/channel-driver');
|
|
4
4
|
const { ok, failed } = require('../../../shared/driver-result');
|
|
5
5
|
|
|
6
|
+
function providerApiForChannel(channel = {}, input = {}) {
|
|
7
|
+
if (channel.providerApi) return channel.providerApi;
|
|
8
|
+
if (channel.gatewaySourceType === 'claude') return 'anthropic-messages';
|
|
9
|
+
if (channel.gatewaySourceType === 'gemini') return 'google-generative-ai';
|
|
10
|
+
if (channel.gatewaySourceType === 'codex') return 'responses';
|
|
11
|
+
return input.wireApi || 'openai-completions';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function createChannelArgs(input, rest) {
|
|
15
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) return [input, ...rest];
|
|
16
|
+
const { name, baseUrl, apiKey, extra, ...fields } = input;
|
|
17
|
+
const inputFields = { ...fields, ...(extra || {}) };
|
|
18
|
+
const channel = {
|
|
19
|
+
wireApi: 'openai',
|
|
20
|
+
providerKey: '',
|
|
21
|
+
authMode: 'api_key',
|
|
22
|
+
oauthProviderId: '',
|
|
23
|
+
transport: '',
|
|
24
|
+
routingGroup: '',
|
|
25
|
+
gatewaySourceType: 'openai_compatible',
|
|
26
|
+
enabled: true,
|
|
27
|
+
model: null,
|
|
28
|
+
allowedModels: [],
|
|
29
|
+
speedTestModel: null,
|
|
30
|
+
presetId: null,
|
|
31
|
+
websiteUrl: '',
|
|
32
|
+
balanceToken: '',
|
|
33
|
+
balanceUserId: null,
|
|
34
|
+
models: [],
|
|
35
|
+
modelMetadataMode: 'auto',
|
|
36
|
+
modelBindings: [],
|
|
37
|
+
providerConfig: {},
|
|
38
|
+
...inputFields
|
|
39
|
+
};
|
|
40
|
+
channel.enabled = channel.enabled !== false;
|
|
41
|
+
channel.allowedModels = Array.isArray(channel.allowedModels) ? channel.allowedModels : [];
|
|
42
|
+
channel.models = Array.isArray(channel.models) ? channel.models : [];
|
|
43
|
+
channel.modelBindings = Array.isArray(channel.modelBindings) ? channel.modelBindings : [];
|
|
44
|
+
channel.providerApi = providerApiForChannel(channel, inputFields);
|
|
45
|
+
return [name, baseUrl, apiKey, channel];
|
|
46
|
+
}
|
|
47
|
+
|
|
6
48
|
function createDriver(context = {}) {
|
|
49
|
+
const { resetChannelHealth } = require('../../../server/services/channel-health');
|
|
7
50
|
const driver = createChannelDriver({
|
|
8
51
|
...context,
|
|
9
52
|
platform: 'omp',
|
|
10
53
|
servicePath: './omp/channels-implementation',
|
|
11
54
|
localServicePath: '../platforms/drivers/omp/channels-implementation',
|
|
12
55
|
syncMethod: 'syncCurrentOmpChannel',
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const { name, baseUrl, apiKey, extra, ...channelExtra } = input;
|
|
16
|
-
return [name, baseUrl, apiKey, { ...channelExtra, ...extra }];
|
|
17
|
-
})()
|
|
18
|
-
: [input, ...rest],
|
|
19
|
-
cliMetadata: {
|
|
20
|
-
supportsCliCreate: false,
|
|
21
|
-
supportsCliToggle: true,
|
|
22
|
-
managedProviderConfig: true,
|
|
23
|
-
defaultPort: 20092,
|
|
24
|
-
createUnavailableMessage: '提示: OMP 渠道请通过 Web UI 或 API 添加后,再在这里启停调度。'
|
|
25
|
-
},
|
|
56
|
+
modelListType: context.manifest?.channelModelType || 'openai_compatible',
|
|
57
|
+
createArgs: createChannelArgs,
|
|
26
58
|
dashboardChannelShape: 'array'
|
|
27
59
|
});
|
|
60
|
+
const saveOrder = driver.saveOrder;
|
|
61
|
+
driver.saveOrder = input => saveOrder(input?.order || input);
|
|
62
|
+
driver.resetHealth = input => {
|
|
63
|
+
const channelId = typeof input === 'string' ? input : input?.channelId || input?.body?.channelId;
|
|
64
|
+
if (!channelId) throw new Error('Channel id is required');
|
|
65
|
+
resetChannelHealth(channelId, 'omp');
|
|
66
|
+
return { channelId, reset: true };
|
|
67
|
+
};
|
|
28
68
|
driver.syncManagedProviderExtension = (...args) => (
|
|
29
69
|
driver._service().syncManagedProviderExtension(...args)
|
|
30
70
|
);
|
|
@@ -165,6 +165,69 @@ function mergeGroupField(channels = [], field) {
|
|
|
165
165
|
return result;
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
+
function collectRoutingGroups(channels = []) {
|
|
169
|
+
const enabled = channels.filter(channel => (
|
|
170
|
+
channel
|
|
171
|
+
&& channel.enabled !== false
|
|
172
|
+
&& (channel.baseUrl || normalizeText(channel.authMode) === 'oauth')
|
|
173
|
+
));
|
|
174
|
+
const groups = new Map();
|
|
175
|
+
enabled.forEach((channel) => {
|
|
176
|
+
const key = getGroupKey(channel);
|
|
177
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
178
|
+
groups.get(key).push(channel);
|
|
179
|
+
});
|
|
180
|
+
return groups;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function getGroupUnsupportedReason(groupChannels, gateway = {}) {
|
|
184
|
+
const template = groupChannels[0];
|
|
185
|
+
if (!SUPPORTED_PROVIDER_APIS.has(getProviderApi(template))) {
|
|
186
|
+
return 'unsupported-provider-api';
|
|
187
|
+
}
|
|
188
|
+
const authMode = normalizeText(template.authMode) || 'api_key';
|
|
189
|
+
const supportedOAuthIds = new Set(
|
|
190
|
+
Array.isArray(gateway.supportedOAuthChannelIds) ? gateway.supportedOAuthChannelIds : []
|
|
191
|
+
);
|
|
192
|
+
const oauthGatewayUnavailable = authMode === 'oauth' && groupChannels.some(channel => (
|
|
193
|
+
!normalizeText(channel.baseUrl)
|
|
194
|
+
|| !normalizeText(channel.apiKey)
|
|
195
|
+
|| normalizeText(channel.transport || channel.providerConfig?.transport) !== 'pi-native'
|
|
196
|
+
|| !supportedOAuthIds.has(channel.id)
|
|
197
|
+
));
|
|
198
|
+
return oauthGatewayUnavailable ? 'oauth-auth-gateway-unavailable' : '';
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function getDeclaredModelIds(channel = {}) {
|
|
202
|
+
const result = [];
|
|
203
|
+
const seen = new Set();
|
|
204
|
+
const append = (raw) => {
|
|
205
|
+
const id = normalizeText(typeof raw === 'string' ? raw : (raw?.id || raw?.name));
|
|
206
|
+
const key = id.toLowerCase();
|
|
207
|
+
if (!id || seen.has(key)) return;
|
|
208
|
+
seen.add(key);
|
|
209
|
+
result.push(id);
|
|
210
|
+
};
|
|
211
|
+
(Array.isArray(channel.models) ? channel.models : []).forEach(append);
|
|
212
|
+
(Array.isArray(channel.allowedModels) ? channel.allowedModels : []).forEach(append);
|
|
213
|
+
append(channel.model);
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function normalizeOmpModelId(value) {
|
|
218
|
+
return normalizeText(value).replace(/:(minimal|low|medium|high|xhigh|max|off)$/i, '');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function channelSupportsOmpModel(channel = {}, modelId = '') {
|
|
222
|
+
const requested = normalizeOmpModelId(modelId).toLowerCase();
|
|
223
|
+
if (!requested) return true;
|
|
224
|
+
const declared = getDeclaredModelIds(channel)
|
|
225
|
+
.map(normalizeOmpModelId)
|
|
226
|
+
.filter(Boolean)
|
|
227
|
+
.map(value => value.toLowerCase());
|
|
228
|
+
return declared.length === 0 || declared.includes(requested);
|
|
229
|
+
}
|
|
230
|
+
|
|
168
231
|
function buildManagedBaseUrl(host, port, routeToken) {
|
|
169
232
|
return `http://${host}:${port}/omp/${routeToken}`;
|
|
170
233
|
}
|
|
@@ -180,17 +243,7 @@ function prepareManagedOmpChannels(channels = [], gateway = {}) {
|
|
|
180
243
|
throw new Error('OMP gateway secret is required before managed providers can be generated');
|
|
181
244
|
}
|
|
182
245
|
|
|
183
|
-
const
|
|
184
|
-
channel
|
|
185
|
-
&& channel.enabled !== false
|
|
186
|
-
&& (channel.baseUrl || normalizeText(channel.authMode) === 'oauth')
|
|
187
|
-
));
|
|
188
|
-
const groups = new Map();
|
|
189
|
-
enabled.forEach((channel) => {
|
|
190
|
-
const key = getGroupKey(channel);
|
|
191
|
-
if (!groups.has(key)) groups.set(key, []);
|
|
192
|
-
groups.get(key).push(channel);
|
|
193
|
-
});
|
|
246
|
+
const groups = collectRoutingGroups(channels);
|
|
194
247
|
|
|
195
248
|
const providerCounts = new Map();
|
|
196
249
|
groups.forEach((groupChannels) => {
|
|
@@ -205,35 +258,16 @@ function prepareManagedOmpChannels(channels = [], gateway = {}) {
|
|
|
205
258
|
groups.forEach((groupChannels, groupKey) => {
|
|
206
259
|
assertCompatibleGroup(groupChannels);
|
|
207
260
|
const template = groupChannels[0];
|
|
208
|
-
|
|
261
|
+
const unsupportedReason = getGroupUnsupportedReason(groupChannels, gateway);
|
|
262
|
+
if (unsupportedReason) {
|
|
209
263
|
groupChannels.forEach(channel => unsupportedChannels.push({
|
|
210
264
|
id: channel.id,
|
|
211
265
|
name: channel.name,
|
|
212
|
-
reason:
|
|
266
|
+
reason: unsupportedReason
|
|
213
267
|
}));
|
|
214
268
|
return;
|
|
215
269
|
}
|
|
216
270
|
const authMode = normalizeText(template.authMode) || 'api_key';
|
|
217
|
-
const transport = normalizeText(template.transport || template.providerConfig?.transport);
|
|
218
|
-
const supportedOAuthIds = new Set(
|
|
219
|
-
Array.isArray(gateway.supportedOAuthChannelIds) ? gateway.supportedOAuthChannelIds : []
|
|
220
|
-
);
|
|
221
|
-
const oauthGatewayUnavailable = authMode === 'oauth' && (
|
|
222
|
-
groupChannels.some(channel => (
|
|
223
|
-
!normalizeText(channel.baseUrl)
|
|
224
|
-
|| !normalizeText(channel.apiKey)
|
|
225
|
-
|| normalizeText(channel.transport || channel.providerConfig?.transport) !== 'pi-native'
|
|
226
|
-
|| !supportedOAuthIds.has(channel.id)
|
|
227
|
-
))
|
|
228
|
-
);
|
|
229
|
-
if (oauthGatewayUnavailable) {
|
|
230
|
-
groupChannels.forEach(channel => unsupportedChannels.push({
|
|
231
|
-
id: channel.id,
|
|
232
|
-
name: channel.name,
|
|
233
|
-
reason: 'oauth-auth-gateway-unavailable'
|
|
234
|
-
}));
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
271
|
|
|
238
272
|
const providerId = normalizeProviderId(getProviderKey(template));
|
|
239
273
|
const routeToken = stableToken(secret, 'route', groupKey);
|
|
@@ -294,6 +328,8 @@ function resolveOmpGatewayRoute(pathname, channels, gateway) {
|
|
|
294
328
|
}
|
|
295
329
|
|
|
296
330
|
module.exports = {
|
|
331
|
+
channelSupportsOmpModel,
|
|
332
|
+
normalizeOmpModelId,
|
|
297
333
|
prepareManagedOmpChannels,
|
|
298
334
|
resolveOmpGatewayRoute,
|
|
299
335
|
getRoutingGroup,
|
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { memoryPressureMiddleware, rejectUpgradeUnderPressure } = require('../../../server/services/memory-pressure');
|
|
4
|
+
|
|
3
5
|
const crypto = require('crypto');
|
|
4
6
|
const { downstreamHeaders } = require('../../../shared/proxy-transport');
|
|
5
7
|
const http = require('http');
|
|
6
8
|
const https = require('https');
|
|
7
9
|
const httpProxy = require('http-proxy');
|
|
8
10
|
const { URL } = require('url');
|
|
9
|
-
const {
|
|
11
|
+
const {
|
|
12
|
+
allocateChannel,
|
|
13
|
+
releaseChannel,
|
|
14
|
+
getSchedulerState
|
|
15
|
+
} = require('../../../server/services/channel-scheduler');
|
|
10
16
|
const { recordSuccess, recordFailure } = require('../../../server/services/channel-health');
|
|
11
|
-
const {
|
|
12
|
-
const {
|
|
17
|
+
const { broadcastSchedulerState } = require('../../../server/websocket-server');
|
|
18
|
+
const { attachServerShutdownHandling, expediteServerShutdown, PROXY_IDLE_TIMEOUT_MS } = require('../../../server/services/server-shutdown');
|
|
19
|
+
const {
|
|
20
|
+
resolveOmpGatewayRoute,
|
|
21
|
+
channelSupportsOmpModel,
|
|
22
|
+
normalizeOmpModelId
|
|
23
|
+
} = require('./gateway-routing');
|
|
13
24
|
const { persistProxyRequestSnapshot } = require('../../../server/services/request-logger');
|
|
14
25
|
|
|
15
26
|
const RETRYABLE_STATUS = new Set([401, 403, 429, 502, 503, 504]);
|
|
@@ -115,6 +126,7 @@ function rewriteCredential(headers, requestUrl, route, channel, options = {}) {
|
|
|
115
126
|
}
|
|
116
127
|
|
|
117
128
|
function readRequestBody(req) {
|
|
129
|
+
if (req.aborted || req.destroyed) return Promise.reject(createAbortError());
|
|
118
130
|
return new Promise((resolve, reject) => {
|
|
119
131
|
const chunks = [];
|
|
120
132
|
let size = 0;
|
|
@@ -157,35 +169,40 @@ function readRequestBody(req) {
|
|
|
157
169
|
});
|
|
158
170
|
}
|
|
159
171
|
|
|
160
|
-
function extractModelId(body, contentType = '') {
|
|
161
|
-
if (
|
|
172
|
+
function extractModelId(body, contentType = '', requestUrl = '') {
|
|
173
|
+
if (body?.length && String(contentType).includes('json')) {
|
|
174
|
+
try {
|
|
175
|
+
const parsed = JSON.parse(body.toString('utf8'));
|
|
176
|
+
const bodyModel = typeof parsed.model === 'string'
|
|
177
|
+
? parsed.model.trim()
|
|
178
|
+
: String(parsed.model?.id || parsed.model?.name || parsed.modelId || parsed.model_id || '').trim();
|
|
179
|
+
if (bodyModel) return bodyModel;
|
|
180
|
+
} catch {
|
|
181
|
+
// Some provider endpoints carry the model in the URL instead of JSON.
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
162
185
|
try {
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
186
|
+
const url = new URL(String(requestUrl || '/'), 'http://127.0.0.1');
|
|
187
|
+
const queryModel = url.searchParams.get('model')
|
|
188
|
+
|| url.searchParams.get('model_id')
|
|
189
|
+
|| url.searchParams.get('modelId');
|
|
190
|
+
if (queryModel?.trim()) return queryModel.trim();
|
|
191
|
+
|
|
192
|
+
// Gemini and Vertex encode the model in paths such as
|
|
193
|
+
// /v1beta/models/gemini-2.5-pro:generateContent.
|
|
194
|
+
const match = url.pathname.match(/(?:^|\/)models\/([^/:]+):[^/]+$/i);
|
|
195
|
+
if (match?.[1]) {
|
|
196
|
+
try {
|
|
197
|
+
return decodeURIComponent(match[1]).trim();
|
|
198
|
+
} catch {
|
|
199
|
+
return match[1].trim();
|
|
200
|
+
}
|
|
167
201
|
}
|
|
168
|
-
return String(parsed.modelId || parsed.model_id || '').trim();
|
|
169
202
|
} catch {
|
|
170
203
|
return '';
|
|
171
204
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
function normalizeModelId(value) {
|
|
175
|
-
return String(value || '').trim().replace(/:(minimal|low|medium|high|xhigh|max|off)$/i, '');
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function channelSupportsModel(channel, modelId) {
|
|
179
|
-
const requested = normalizeModelId(modelId).toLowerCase();
|
|
180
|
-
if (!requested) return true;
|
|
181
|
-
const declared = [];
|
|
182
|
-
(Array.isArray(channel.allowedModels) ? channel.allowedModels : []).forEach(value => declared.push(value));
|
|
183
|
-
(Array.isArray(channel.models) ? channel.models : []).forEach((model) => {
|
|
184
|
-
declared.push(typeof model === 'string' ? model : (model?.id || model?.name));
|
|
185
|
-
});
|
|
186
|
-
if (channel.model) declared.push(channel.model);
|
|
187
|
-
const normalized = [...new Set(declared.map(normalizeModelId).filter(Boolean).map(value => value.toLowerCase()))];
|
|
188
|
-
return normalized.length === 0 || normalized.includes(requested);
|
|
205
|
+
return '';
|
|
189
206
|
}
|
|
190
207
|
|
|
191
208
|
function writeJson(res, statusCode, payload) {
|
|
@@ -253,6 +270,8 @@ function createOmpGateway(options = {}) {
|
|
|
253
270
|
const getChannels = options.getChannels;
|
|
254
271
|
const allocate = options.allocateChannel || allocateChannel;
|
|
255
272
|
const release = options.releaseChannel || releaseChannel;
|
|
273
|
+
const readSchedulerState = options.getSchedulerState || getSchedulerState;
|
|
274
|
+
const publishSchedulerState = options.broadcastSchedulerState || broadcastSchedulerState;
|
|
256
275
|
const onSuccess = options.recordSuccess || recordSuccess;
|
|
257
276
|
const onFailure = options.recordFailure || recordFailure;
|
|
258
277
|
const websocketProxy = httpProxy.createProxyServer({
|
|
@@ -269,6 +288,22 @@ function createOmpGateway(options = {}) {
|
|
|
269
288
|
let inflightRequests = 0;
|
|
270
289
|
const activeUpstreams = new Set();
|
|
271
290
|
|
|
291
|
+
function broadcastCurrentSchedulerState() {
|
|
292
|
+
try {
|
|
293
|
+
publishSchedulerState('omp', readSchedulerState('omp'));
|
|
294
|
+
} catch {
|
|
295
|
+
// Scheduler broadcasts are observational and must not interrupt a request.
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function releaseAndBroadcast(channelId) {
|
|
300
|
+
try {
|
|
301
|
+
release(channelId, 'omp');
|
|
302
|
+
} finally {
|
|
303
|
+
broadcastCurrentSchedulerState();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
272
307
|
function gatewayDescriptor() {
|
|
273
308
|
return { host, port, secret };
|
|
274
309
|
}
|
|
@@ -282,7 +317,7 @@ function createOmpGateway(options = {}) {
|
|
|
282
317
|
const channels = currentChannels();
|
|
283
318
|
const candidateIds = route.channelIds.filter((channelId) => {
|
|
284
319
|
const channel = channels.find(item => item.id === channelId);
|
|
285
|
-
return channel &&
|
|
320
|
+
return channel && channelSupportsOmpModel(channel, modelId);
|
|
286
321
|
});
|
|
287
322
|
if (candidateIds.length === 0) {
|
|
288
323
|
throw Object.assign(
|
|
@@ -290,7 +325,7 @@ function createOmpGateway(options = {}) {
|
|
|
290
325
|
{ statusCode: 422 }
|
|
291
326
|
);
|
|
292
327
|
}
|
|
293
|
-
|
|
328
|
+
const channel = await allocate({
|
|
294
329
|
source: 'omp',
|
|
295
330
|
enableSessionBinding: false,
|
|
296
331
|
candidateIds,
|
|
@@ -298,9 +333,11 @@ function createOmpGateway(options = {}) {
|
|
|
298
333
|
routingGroup: route.routingGroup,
|
|
299
334
|
providerKey: route.providerKey,
|
|
300
335
|
providerApi: route.providerApi,
|
|
301
|
-
modelId:
|
|
336
|
+
modelId: normalizeOmpModelId(modelId),
|
|
302
337
|
signal
|
|
303
338
|
});
|
|
339
|
+
if (channel) broadcastCurrentSchedulerState();
|
|
340
|
+
return channel;
|
|
304
341
|
}
|
|
305
342
|
|
|
306
343
|
function forwardAttempt(req, route, channel, body, signal) {
|
|
@@ -309,7 +346,7 @@ function createOmpGateway(options = {}) {
|
|
|
309
346
|
const releaseOnce = () => {
|
|
310
347
|
if (released) return;
|
|
311
348
|
released = true;
|
|
312
|
-
|
|
349
|
+
releaseAndBroadcast(channel.id);
|
|
313
350
|
};
|
|
314
351
|
if (signal?.aborted) {
|
|
315
352
|
releaseOnce();
|
|
@@ -363,6 +400,9 @@ function createOmpGateway(options = {}) {
|
|
|
363
400
|
});
|
|
364
401
|
|
|
365
402
|
activeUpstreams.add(upstreamRequest);
|
|
403
|
+
upstreamRequest.setTimeout(PROXY_IDLE_TIMEOUT_MS, () => {
|
|
404
|
+
upstreamRequest.destroy(new Error('OMP upstream request timeout'));
|
|
405
|
+
});
|
|
366
406
|
const abortUpstream = () => {
|
|
367
407
|
const error = createAbortError();
|
|
368
408
|
if (upstreamResponse) {
|
|
@@ -385,7 +425,7 @@ function createOmpGateway(options = {}) {
|
|
|
385
425
|
async function handleProxyRequest(req, res, route, signal) {
|
|
386
426
|
const body = await readRequestBody(req);
|
|
387
427
|
if (signal?.aborted) throw createAbortError();
|
|
388
|
-
const modelId = extractModelId(body, req.headers['content-type']);
|
|
428
|
+
const modelId = extractModelId(body, req.headers['content-type'], route.upstreamPath);
|
|
389
429
|
const attemptedIds = [];
|
|
390
430
|
let lastError = null;
|
|
391
431
|
|
|
@@ -398,7 +438,7 @@ function createOmpGateway(options = {}) {
|
|
|
398
438
|
break;
|
|
399
439
|
}
|
|
400
440
|
if (signal?.aborted) {
|
|
401
|
-
|
|
441
|
+
releaseAndBroadcast(channel.id);
|
|
402
442
|
lastError = createAbortError();
|
|
403
443
|
break;
|
|
404
444
|
}
|
|
@@ -409,7 +449,7 @@ function createOmpGateway(options = {}) {
|
|
|
409
449
|
if (attempt === 0 && RETRYABLE_STATUS.has(result.statusCode)) {
|
|
410
450
|
result.response.destroy();
|
|
411
451
|
result.release();
|
|
412
|
-
onFailure(channel.id, 'omp');
|
|
452
|
+
if (!signal?.aborted) onFailure(channel.id, 'omp');
|
|
413
453
|
lastError = Object.assign(new Error(`OMP upstream returned HTTP ${result.statusCode}`), {
|
|
414
454
|
statusCode: result.statusCode
|
|
415
455
|
});
|
|
@@ -427,7 +467,7 @@ function createOmpGateway(options = {}) {
|
|
|
427
467
|
if (result.statusCode < 400 && !error) {
|
|
428
468
|
onSuccess(channel.id, 'omp');
|
|
429
469
|
} else {
|
|
430
|
-
onFailure(channel.id, 'omp');
|
|
470
|
+
if (!signal?.aborted) onFailure(channel.id, 'omp');
|
|
431
471
|
}
|
|
432
472
|
resolve();
|
|
433
473
|
};
|
|
@@ -455,7 +495,7 @@ function createOmpGateway(options = {}) {
|
|
|
455
495
|
if (error?.name === 'AbortError') {
|
|
456
496
|
break;
|
|
457
497
|
}
|
|
458
|
-
onFailure(channel.id, 'omp');
|
|
498
|
+
if (!signal?.aborted) onFailure(channel.id, 'omp');
|
|
459
499
|
}
|
|
460
500
|
}
|
|
461
501
|
|
|
@@ -514,6 +554,11 @@ function createOmpGateway(options = {}) {
|
|
|
514
554
|
}
|
|
515
555
|
});
|
|
516
556
|
|
|
557
|
+
req.proxyPlatform = 'omp';
|
|
558
|
+
let admitted = false;
|
|
559
|
+
memoryPressureMiddleware(req, res, () => { admitted = true; });
|
|
560
|
+
if (!admitted) return;
|
|
561
|
+
|
|
517
562
|
inflightRequests++;
|
|
518
563
|
const abortController = new AbortController();
|
|
519
564
|
const abortDownstream = () => abortController.abort();
|
|
@@ -541,6 +586,7 @@ function createOmpGateway(options = {}) {
|
|
|
541
586
|
const statusText = {
|
|
542
587
|
401: 'Unauthorized',
|
|
543
588
|
404: 'Not Found',
|
|
589
|
+
422: 'Unprocessable Entity',
|
|
544
590
|
503: 'Service Unavailable'
|
|
545
591
|
}[statusCode] || 'Bad Gateway';
|
|
546
592
|
socket.end([
|
|
@@ -554,6 +600,8 @@ function createOmpGateway(options = {}) {
|
|
|
554
600
|
}
|
|
555
601
|
|
|
556
602
|
async function handleUpgrade(req, socket, head) {
|
|
603
|
+
req.proxyPlatform = 'omp';
|
|
604
|
+
if (rejectUpgradeUnderPressure(req, socket)) return;
|
|
557
605
|
if (draining) {
|
|
558
606
|
rejectUpgrade(socket, 503, 'OMP gateway is draining');
|
|
559
607
|
return;
|
|
@@ -583,10 +631,22 @@ function createOmpGateway(options = {}) {
|
|
|
583
631
|
route.upstreamPath = `${route.upstreamPath}${parsed.search}`;
|
|
584
632
|
|
|
585
633
|
let channel;
|
|
634
|
+
const controller = new AbortController();
|
|
635
|
+
const abortWaiting = () => controller.abort();
|
|
636
|
+
socket.once('close', abortWaiting);
|
|
637
|
+
socket.once('error', abortWaiting);
|
|
586
638
|
try {
|
|
587
|
-
|
|
639
|
+
const modelId = extractModelId(null, '', route.upstreamPath);
|
|
640
|
+
channel = await allocateForRoute(route, [], modelId, controller.signal);
|
|
588
641
|
} catch (error) {
|
|
589
|
-
rejectUpgrade(socket, 503, error.message || 'No compatible OMP channel is available');
|
|
642
|
+
rejectUpgrade(socket, error.statusCode || 503, error.message || 'No compatible OMP channel is available');
|
|
643
|
+
return;
|
|
644
|
+
} finally {
|
|
645
|
+
socket.removeListener('close', abortWaiting);
|
|
646
|
+
socket.removeListener('error', abortWaiting);
|
|
647
|
+
}
|
|
648
|
+
if (controller.signal.aborted || socket.destroyed) {
|
|
649
|
+
releaseAndBroadcast(channel.id);
|
|
590
650
|
return;
|
|
591
651
|
}
|
|
592
652
|
|
|
@@ -607,7 +667,7 @@ function createOmpGateway(options = {}) {
|
|
|
607
667
|
if (!failed) {
|
|
608
668
|
onSuccess(channel.id, 'omp');
|
|
609
669
|
}
|
|
610
|
-
|
|
670
|
+
releaseAndBroadcast(channel.id);
|
|
611
671
|
};
|
|
612
672
|
socket.once('close', releaseOnce);
|
|
613
673
|
socket.once('error', (error) => {
|
|
@@ -631,7 +691,9 @@ function createOmpGateway(options = {}) {
|
|
|
631
691
|
websocketProxy.ws(req, socket, head, {
|
|
632
692
|
target: upstream.origin,
|
|
633
693
|
changeOrigin: true,
|
|
634
|
-
secure: upstream.protocol === 'https:'
|
|
694
|
+
secure: upstream.protocol === 'https:',
|
|
695
|
+
proxyTimeout: PROXY_IDLE_TIMEOUT_MS,
|
|
696
|
+
timeout: 0
|
|
635
697
|
});
|
|
636
698
|
} catch (error) {
|
|
637
699
|
markFailedOnce(error);
|
|
@@ -8,8 +8,7 @@ function createDriver(context = {}) {
|
|
|
8
8
|
platform: 'omp',
|
|
9
9
|
servicePath: './omp/proxy-implementation',
|
|
10
10
|
localServicePath: '../platforms/drivers/omp/proxy-implementation',
|
|
11
|
-
exports: { status: 'getOmpProxyStatus', start: 'startOmpProxyServer', stop: 'stopOmpProxyServer' }
|
|
12
|
-
cliMetadata: { managedProviderConfig: true, defaultPort: 20092 }
|
|
11
|
+
exports: { status: 'getOmpProxyStatus', start: 'startOmpProxyServer', stop: 'stopOmpProxyServer' }
|
|
13
12
|
});
|
|
14
13
|
}
|
|
15
14
|
|
|
@@ -2,39 +2,68 @@
|
|
|
2
2
|
|
|
3
3
|
const { createChannelDriver } = require('../../../shared/channel-driver');
|
|
4
4
|
|
|
5
|
+
function createChannelArgs(input, rest) {
|
|
6
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) return [input, ...rest];
|
|
7
|
+
const { name, baseUrl, apiKey, extra, ...fields } = input;
|
|
8
|
+
const inputFields = { ...fields, ...(extra || {}) };
|
|
9
|
+
const channel = {
|
|
10
|
+
wireApi: 'openai',
|
|
11
|
+
gatewaySourceType: 'codex',
|
|
12
|
+
enabled: true,
|
|
13
|
+
weight: 1,
|
|
14
|
+
maxConcurrency: null,
|
|
15
|
+
model: null,
|
|
16
|
+
modelRedirects: [],
|
|
17
|
+
speedTestModel: null,
|
|
18
|
+
presetId: null,
|
|
19
|
+
providerKey: '',
|
|
20
|
+
websiteUrl: '',
|
|
21
|
+
allowedModels: [],
|
|
22
|
+
authMode: 'api_key',
|
|
23
|
+
oauthProviderId: '',
|
|
24
|
+
balanceToken: '',
|
|
25
|
+
balanceUserId: null,
|
|
26
|
+
...inputFields
|
|
27
|
+
};
|
|
28
|
+
channel.wireApi = channel.wireApi || 'openai';
|
|
29
|
+
channel.gatewaySourceType = channel.gatewaySourceType || 'codex';
|
|
30
|
+
channel.enabled = channel.enabled !== false;
|
|
31
|
+
channel.weight = channel.weight || 1;
|
|
32
|
+
channel.maxConcurrency = channel.maxConcurrency || null;
|
|
33
|
+
channel.model = channel.model || null;
|
|
34
|
+
channel.modelRedirects = channel.modelRedirects || [];
|
|
35
|
+
channel.speedTestModel = channel.speedTestModel || null;
|
|
36
|
+
channel.presetId = channel.presetId || null;
|
|
37
|
+
channel.providerKey = channel.providerKey || '';
|
|
38
|
+
channel.websiteUrl = channel.websiteUrl || '';
|
|
39
|
+
channel.allowedModels = channel.allowedModels || [];
|
|
40
|
+
channel.authMode = channel.authMode || 'api_key';
|
|
41
|
+
channel.oauthProviderId = channel.oauthProviderId || '';
|
|
42
|
+
channel.balanceToken = channel.balanceToken || '';
|
|
43
|
+
channel.balanceUserId = channel.balanceUserId || null;
|
|
44
|
+
return [name, baseUrl, apiKey, channel];
|
|
45
|
+
}
|
|
46
|
+
|
|
5
47
|
function createDriver(context = {}) {
|
|
48
|
+
const { resetChannelHealth } = require('../../../server/services/channel-health');
|
|
6
49
|
const driver = createChannelDriver({
|
|
7
50
|
...context,
|
|
8
51
|
platform: 'opencode',
|
|
9
52
|
servicePath: './opencode/channels-implementation',
|
|
10
53
|
localServicePath: '../opencode/channels-implementation',
|
|
11
54
|
syncMethod: 'syncCurrentOpenCodeChannel',
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const { name, baseUrl, apiKey, extra, ...channelExtra } = input;
|
|
15
|
-
return [name, baseUrl, apiKey, { ...channelExtra, ...extra }];
|
|
16
|
-
})()
|
|
17
|
-
: [input, ...rest],
|
|
18
|
-
cliMetadata: {
|
|
19
|
-
supportsCliCreate: true,
|
|
20
|
-
supportsCliToggle: true,
|
|
21
|
-
defaultPort: 20091,
|
|
22
|
-
createQuestions: [
|
|
23
|
-
{
|
|
24
|
-
type: 'input',
|
|
25
|
-
name: 'wireApi',
|
|
26
|
-
message: 'Wire API (默认: openai):',
|
|
27
|
-
default: 'openai'
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
type: 'input',
|
|
31
|
-
name: 'model',
|
|
32
|
-
message: '默认模型(可选,直接回车跳过):'
|
|
33
|
-
}
|
|
34
|
-
]
|
|
35
|
-
},
|
|
55
|
+
modelListType: context.manifest?.channelModelType || 'openai_compatible',
|
|
56
|
+
createArgs: createChannelArgs,
|
|
36
57
|
dashboardChannelShape: 'array'
|
|
37
58
|
});
|
|
59
|
+
const saveOrder = driver.saveOrder;
|
|
60
|
+
driver.saveOrder = input => saveOrder(input?.order || input);
|
|
61
|
+
driver.resetHealth = input => {
|
|
62
|
+
const channelId = typeof input === 'string' ? input : input?.channelId || input?.body?.channelId;
|
|
63
|
+
if (!channelId) throw new Error('Channel id is required');
|
|
64
|
+
resetChannelHealth(channelId, 'opencode');
|
|
65
|
+
return { channelId, reset: true };
|
|
66
|
+
};
|
|
38
67
|
const auth = require('../../channel-auth-service');
|
|
39
68
|
driver.getAuth = context => auth.getChannelAuth('opencode', { channelId: context?.params?.channelId || context?.query?.channelId || '' });
|
|
40
69
|
driver.syncLocalAuth = context => auth.syncLocalChannelAuth('opencode', { channelId: context?.body?.channelId || context?.params?.channelId || context?.query?.channelId || '' });
|
|
@@ -8,8 +8,7 @@ function createDriver(context = {}) {
|
|
|
8
8
|
platform: 'opencode',
|
|
9
9
|
servicePath: './opencode/proxy-implementation',
|
|
10
10
|
localServicePath: '../opencode/proxy-implementation',
|
|
11
|
-
exports: { status: 'getOpenCodeProxyStatus', start: 'startOpenCodeProxyServer', stop: 'stopOpenCodeProxyServer' }
|
|
12
|
-
cliMetadata: { defaultPort: 20091 }
|
|
11
|
+
exports: { status: 'getOpenCodeProxyStatus', start: 'startOpenCodeProxyServer', stop: 'stopOpenCodeProxyServer' }
|
|
13
12
|
});
|
|
14
13
|
}
|
|
15
14
|
|