dsh-ustc-quota 2.0.2 → 2.0.4
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/client.js +1 -1
- package/index.js +77 -28
- package/package.json +1 -1
- package/pool-config.js +1 -1
- package/retire.js +10 -0
package/client.js
CHANGED
|
@@ -55,7 +55,7 @@ window.__ModuleLoader__.load({
|
|
|
55
55
|
return 0;
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
const CLIENT_VERSION = '2.0.
|
|
58
|
+
const CLIENT_VERSION = '2.0.4';
|
|
59
59
|
const UPDATE_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
60
60
|
const CLIENT_VERSION_KEY = `${NS}:client-version`;
|
|
61
61
|
const CLIENT_RELOAD_KEY = `${NS}:client-reload`;
|
package/index.js
CHANGED
|
@@ -39,11 +39,29 @@ let poolLastCall = null
|
|
|
39
39
|
* Returns the selection to apply, or null when the agent must stay where it is. Only pool
|
|
40
40
|
* providers are managed: a conversation the user pointed at a non-pool provider is left alone.
|
|
41
41
|
*/
|
|
42
|
-
export function nextSelectionFor(current, keys, sessionId) {
|
|
42
|
+
export function nextSelectionFor(current, keys, sessionId, options = {}) {
|
|
43
43
|
if (current === null || typeof current !== 'object') return null
|
|
44
|
-
|
|
45
|
-
const
|
|
44
|
+
const allowed = options.allowed === undefined ? null : options.allowed
|
|
45
|
+
const delegated = options.delegated === true
|
|
46
|
+
const owned = typeof current.provider === 'string'
|
|
47
|
+
&& current.provider.startsWith('ustc-')
|
|
48
|
+
&& current.provider !== PROVIDER
|
|
49
|
+
&& (allowed === null || allowed === undefined || allowed.has(current.provider))
|
|
50
|
+
if (!owned) return null
|
|
51
|
+
const pick = pickPoolKey(keys, sessionId, allowed)
|
|
46
52
|
if (pick === null) return null
|
|
53
|
+
// A conversation the user pointed at a healthy key stays there; only a delegated child is
|
|
54
|
+
// allocated immediately, and any agent moves once its key reaches the wall.
|
|
55
|
+
const worst = (Array.isArray(keys) ? keys : [])
|
|
56
|
+
.filter((entry) => entry && entry.provider === current.provider)
|
|
57
|
+
.map((entry) => Math.max(0, ...(Array.isArray(entry.windows) ? entry.windows : []).map((window) => (window && typeof window.ratio === 'number' ? window.ratio : 0)), 0))
|
|
58
|
+
.reduce((a, b) => Math.max(a, b), 0)
|
|
59
|
+
if (!delegated && worst < WALL_RATIO) {
|
|
60
|
+
// Respect the key this conversation is already on: a manual choice must survive, and a
|
|
61
|
+
// healthy key must not be abandoned just because another one is quieter.
|
|
62
|
+
if (typeof sessionId === 'string') poolSticky.set(sessionId, current.provider)
|
|
63
|
+
return { provider: current.provider, model: current.model, reasoningEffort: current.reasoningEffort, changed: false, reason: 'sticky' }
|
|
64
|
+
}
|
|
47
65
|
if (pick.provider === current.provider) return { provider: current.provider, model: current.model, reasoningEffort: current.reasoningEffort, changed: false, reason: pick.reason }
|
|
48
66
|
return {
|
|
49
67
|
provider: pick.provider,
|
|
@@ -54,10 +72,22 @@ export function nextSelectionFor(current, keys, sessionId) {
|
|
|
54
72
|
}
|
|
55
73
|
}
|
|
56
74
|
|
|
75
|
+
/** The retired edge-pool provider id: never a per-key row, never chosen again. */
|
|
76
|
+
const PROVIDER = 'ustc-lb'
|
|
77
|
+
|
|
78
|
+
/** Load ratio above which a key must hand the conversation to another one. */
|
|
79
|
+
export const WALL_RATIO = 0.9
|
|
80
|
+
|
|
57
81
|
/** Pick the key for one model call: sticky first, then the least-loaded healthy one. */
|
|
58
|
-
export function pickPoolKey(keys, sessionId) {
|
|
82
|
+
export function pickPoolKey(keys, sessionId, allowed = null) {
|
|
83
|
+
// Only providers the pool owns take part: a user-declared ustc-<name> row is theirs, and
|
|
84
|
+
// the retired 'ustc-lb' must never be chosen again.
|
|
85
|
+
const owned = (provider) => typeof provider === 'string'
|
|
86
|
+
&& provider.startsWith('ustc-')
|
|
87
|
+
&& provider !== PROVIDER
|
|
88
|
+
&& (allowed === null || allowed === undefined || allowed.has(provider))
|
|
59
89
|
const all = (Array.isArray(keys) ? keys : []).filter((entry) =>
|
|
60
|
-
entry &&
|
|
90
|
+
entry && owned(entry.provider) && entry.blocked !== true && entry.ok !== false)
|
|
61
91
|
if (all.length === 0) return null
|
|
62
92
|
// A key with no quota data is not "empty": treating an unmeasured key as 0% load moves
|
|
63
93
|
// traffic onto a key nobody has verified. Measured keys win; unmeasured ones are the
|
|
@@ -77,7 +107,7 @@ export function pickPoolKey(keys, sessionId) {
|
|
|
77
107
|
// Keep the conversation on its key until the wall is near: cache affinity matters more
|
|
78
108
|
// than spreading perfectly.
|
|
79
109
|
const heldWorst = held === undefined ? null : worstOf(held)
|
|
80
|
-
if (held && heldWorst !== null && heldWorst <
|
|
110
|
+
if (held && heldWorst !== null && heldWorst < WALL_RATIO) return { provider: held.provider, reason: 'sticky' }
|
|
81
111
|
}
|
|
82
112
|
let best = entries[0]
|
|
83
113
|
for (const entry of entries) {
|
|
@@ -133,7 +163,7 @@ function rejectGuard(response, reason) {
|
|
|
133
163
|
const UPDATE_PATH = '/plugins/dsh-ustc-quota/update';
|
|
134
164
|
const UPDATE_APPLY_PATH = '/plugins/dsh-ustc-quota/update/apply';
|
|
135
165
|
const UPDATE_TOKEN_TTL_MS = 30 * 60 * 1000;
|
|
136
|
-
const PLUGIN_VERSION = '2.0.
|
|
166
|
+
const PLUGIN_VERSION = '2.0.4';
|
|
137
167
|
const KEY_INFO_PATH = 'key/info';
|
|
138
168
|
const USER_INFO_PATH = 'user/info';
|
|
139
169
|
const DISCOVERY_TTL_MS = 10_000;
|
|
@@ -147,6 +177,7 @@ export const inject = ['webServer'];
|
|
|
147
177
|
|
|
148
178
|
export function apply(ctx, config) {
|
|
149
179
|
const settings = normalizeConfig(config);
|
|
180
|
+
const poolProviders = () => new Set(keysFromConfig({ keys: ownKeys() }).map((key) => 'ustc-' + key.id))
|
|
150
181
|
const ownKeys = () => {
|
|
151
182
|
const stored = readStoredKeys()
|
|
152
183
|
if (Array.isArray(stored) && stored.length > 0) return stored
|
|
@@ -159,6 +190,18 @@ export function apply(ctx, config) {
|
|
|
159
190
|
// provider and a conversation can simply select it.
|
|
160
191
|
const poolRowResults = []
|
|
161
192
|
let poolSnapshot = null
|
|
193
|
+
const guarded = (handler, methods) => (request, response) => {
|
|
194
|
+
const reason = guardRequest(request)
|
|
195
|
+
if (reason !== null) { rejectGuard(response, reason); return }
|
|
196
|
+
const method = typeof request.method === 'string' ? request.method.toUpperCase() : 'GET'
|
|
197
|
+
if (!methods.includes(method)) {
|
|
198
|
+
response.writeHead(405, { 'content-type': 'application/json; charset=utf-8', allow: methods.join(', ') })
|
|
199
|
+
response.end(JSON.stringify({ ok: false, reason: 'method-not-allowed' }))
|
|
200
|
+
return
|
|
201
|
+
}
|
|
202
|
+
return handler(request, response)
|
|
203
|
+
}
|
|
204
|
+
|
|
162
205
|
let lastPoolMove = null
|
|
163
206
|
// Observation only: the waterfall hands us the frozen options and the next() chain, so we
|
|
164
207
|
// record and pass through. Rewriting here is impossible (the host's fallback keeps the
|
|
@@ -198,7 +241,9 @@ export function apply(ctx, config) {
|
|
|
198
241
|
try {
|
|
199
242
|
const current = agent && agent.model && typeof agent.model.current === 'function' ? agent.model.current() : null
|
|
200
243
|
const sessionId = agent && agent.session && typeof agent.session.id === 'string' ? agent.session.id : null
|
|
201
|
-
const
|
|
244
|
+
const allowed = poolProviders()
|
|
245
|
+
const depth = agent && agent.options && Number.isFinite(agent.options.subagentDepth) ? agent.options.subagentDepth : 0
|
|
246
|
+
const decision = nextSelectionFor(current, poolSnapshot === null ? [] : poolSnapshot.keys, sessionId, { allowed, delegated: depth > 0 })
|
|
202
247
|
if (decision !== null) {
|
|
203
248
|
if (decision.changed) {
|
|
204
249
|
agent.model.select({
|
|
@@ -259,20 +304,20 @@ export function apply(ctx, config) {
|
|
|
259
304
|
ctx.effect(() => ctx.webServer.register({
|
|
260
305
|
kind: 'exact',
|
|
261
306
|
path: POOL_ROWS_PATH,
|
|
262
|
-
handler: (request, response) => {
|
|
307
|
+
handler: guarded((request, response) => {
|
|
263
308
|
const body = JSON.stringify({ ok: true, rows: poolRowResults, configured: ownKeys().length, rotation: lastRotation })
|
|
264
309
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
265
310
|
response.end(body)
|
|
266
|
-
},
|
|
311
|
+
}, ['GET']),
|
|
267
312
|
}), 'dsh-ustc-quota: pool rows')
|
|
268
313
|
ctx.effect(() => ctx.webServer.register({
|
|
269
314
|
kind: 'exact',
|
|
270
315
|
path: '/plugins/dsh-ustc-quota/pool/last',
|
|
271
|
-
handler: (request, response) => {
|
|
316
|
+
handler: guarded((request, response) => {
|
|
272
317
|
const body = JSON.stringify({ ok: true, last: lastCall, move: lastPoolMove })
|
|
273
318
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
274
319
|
response.end(body)
|
|
275
|
-
},
|
|
320
|
+
}, ['GET']),
|
|
276
321
|
}), 'dsh-ustc-quota: pool last call')
|
|
277
322
|
// Migrate conversations that still point at the retired edge provider onto a real key.
|
|
278
323
|
// This must run BEFORE that provider row is withdrawn, otherwise their next turn has no
|
|
@@ -309,7 +354,7 @@ export function apply(ctx, config) {
|
|
|
309
354
|
ctx.effect(() => ctx.webServer.register({
|
|
310
355
|
kind: 'exact',
|
|
311
356
|
path: '/plugins/dsh-ustc-quota/pool/sessions',
|
|
312
|
-
handler: (request, response) => {
|
|
357
|
+
handler: guarded((request, response) => {
|
|
313
358
|
void (async () => {
|
|
314
359
|
const controller = ctx.get('sessionController')
|
|
315
360
|
if (!controller || typeof controller.list !== 'function') {
|
|
@@ -343,12 +388,12 @@ export function apply(ctx, config) {
|
|
|
343
388
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
344
389
|
response.end(body)
|
|
345
390
|
})()
|
|
346
|
-
},
|
|
391
|
+
}, ['GET']),
|
|
347
392
|
}), 'dsh-ustc-quota: pool sessions')
|
|
348
393
|
ctx.effect(() => ctx.webServer.register({
|
|
349
394
|
kind: 'exact',
|
|
350
395
|
path: '/plugins/dsh-ustc-quota/pool/migrate',
|
|
351
|
-
handler: (request, response) => {
|
|
396
|
+
handler: guarded((request, response) => {
|
|
352
397
|
void (async () => {
|
|
353
398
|
const controller = ctx.get('sessionController')
|
|
354
399
|
const migrated = []
|
|
@@ -391,14 +436,14 @@ export function apply(ctx, config) {
|
|
|
391
436
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
392
437
|
response.end(body)
|
|
393
438
|
})()
|
|
394
|
-
},
|
|
439
|
+
}, ['POST']),
|
|
395
440
|
}), 'dsh-ustc-quota: pool migrate')
|
|
396
441
|
// Headless end-to-end probe: create a throwaway conversation and run one real turn, so the
|
|
397
442
|
// pool can be verified without driving the UI.
|
|
398
443
|
ctx.effect(() => ctx.webServer.register({
|
|
399
444
|
kind: 'exact',
|
|
400
445
|
path: '/plugins/dsh-ustc-quota/pool/probe',
|
|
401
|
-
handler: (request, response) => {
|
|
446
|
+
handler: guarded((request, response) => {
|
|
402
447
|
void (async () => {
|
|
403
448
|
const result = { ok: false }
|
|
404
449
|
try {
|
|
@@ -406,7 +451,7 @@ export function apply(ctx, config) {
|
|
|
406
451
|
if (!controller || typeof controller.create !== 'function' || typeof controller.prompt !== 'function') {
|
|
407
452
|
throw new Error('no-session-controller')
|
|
408
453
|
}
|
|
409
|
-
const created = await controller.create({
|
|
454
|
+
const created = await controller.create({})
|
|
410
455
|
const sessionId = created && created.sessionId ? created.sessionId : null
|
|
411
456
|
if (sessionId === null) throw new Error('create-returned-no-session')
|
|
412
457
|
result.sessionId = sessionId
|
|
@@ -424,27 +469,27 @@ export function apply(ctx, config) {
|
|
|
424
469
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
425
470
|
response.end(JSON.stringify(result))
|
|
426
471
|
})()
|
|
427
|
-
},
|
|
472
|
+
}, ['POST']),
|
|
428
473
|
}), 'dsh-ustc-quota: pool probe')
|
|
429
474
|
// Final retirement of the standalone pool plugin. Sessions must be migrated first; this
|
|
430
475
|
// only strips the references and parks the old package under backups/.
|
|
431
476
|
ctx.effect(() => ctx.webServer.register({
|
|
432
477
|
kind: 'exact',
|
|
433
478
|
path: '/plugins/dsh-ustc-quota/pool/retire-legacy',
|
|
434
|
-
handler: (request, response) => {
|
|
479
|
+
handler: guarded((request, response) => {
|
|
435
480
|
void (async () => {
|
|
436
481
|
const profile = findProfile()
|
|
437
482
|
const result = profile === null ? { profile: null, reason: 'profile-not-found' } : retireManager(profile)
|
|
438
483
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
439
484
|
response.end(JSON.stringify({ ok: profile !== null, ...result }))
|
|
440
485
|
})()
|
|
441
|
-
},
|
|
486
|
+
}, ['POST']),
|
|
442
487
|
}), 'dsh-ustc-quota: retire legacy pool')
|
|
443
488
|
// One-shot clean-up of rows the pool no longer owns (renamed keys, the retired edge pool).
|
|
444
489
|
ctx.effect(() => ctx.webServer.register({
|
|
445
490
|
kind: 'exact',
|
|
446
491
|
path: '/plugins/dsh-ustc-quota/pool/cleanup',
|
|
447
|
-
handler: (request, response) => {
|
|
492
|
+
handler: guarded((request, response) => {
|
|
448
493
|
void (async () => {
|
|
449
494
|
const keep = new Set(ownKeys().map((key) => 'ustc-' + String(key.id)))
|
|
450
495
|
const editor = ctx.get('configEditor')
|
|
@@ -455,8 +500,12 @@ export function apply(ctx, config) {
|
|
|
455
500
|
if (!row || !row.entry || !row.entry.options || row.entry.options.name !== '@deepseek-ai/dsh-llm-pi-ai') continue
|
|
456
501
|
const providers = row.entry.options.config && row.entry.options.config.providers
|
|
457
502
|
if (!providers || typeof providers !== 'object') continue
|
|
458
|
-
for (const providerId of Object.
|
|
503
|
+
for (const [providerId, definition] of Object.entries(providers)) {
|
|
459
504
|
if (!providerId.startsWith('ustc-') || keep.has(providerId)) continue
|
|
505
|
+
// Only rows the pool itself wrote may be removed; a user-declared ustc-<name>
|
|
506
|
+
// row is theirs, and deleting it would break their own conversations.
|
|
507
|
+
const ref = definition && typeof definition.apiKeyEnv === 'string' ? definition.apiKeyEnv : ''
|
|
508
|
+
if (!ref.startsWith('USTC_POOL_')) { removed.push({ providerId, ok: false, reason: 'not-pool-owned' }); continue }
|
|
460
509
|
const result = await removeProviderRow(ctx, providerId)
|
|
461
510
|
removed.push({ providerId, ...result })
|
|
462
511
|
}
|
|
@@ -465,19 +514,19 @@ export function apply(ctx, config) {
|
|
|
465
514
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
466
515
|
response.end(body)
|
|
467
516
|
})()
|
|
468
|
-
},
|
|
517
|
+
}, ['POST']),
|
|
469
518
|
}), 'dsh-ustc-quota: pool cleanup')
|
|
470
519
|
ctx.effect(() => ctx.webServer.register({
|
|
471
520
|
kind: 'exact',
|
|
472
521
|
path: '/plugins/dsh-ustc-quota/pool/rotate',
|
|
473
|
-
handler: (request, response) => {
|
|
522
|
+
handler: guarded((request, response) => {
|
|
474
523
|
void (async () => {
|
|
475
524
|
await rotateDefault()
|
|
476
525
|
const body = JSON.stringify({ ok: true, rotation: lastRotation })
|
|
477
526
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
478
527
|
response.end(body)
|
|
479
528
|
})()
|
|
480
|
-
},
|
|
529
|
+
}, ['POST']),
|
|
481
530
|
}), 'dsh-ustc-quota: pool rotate')
|
|
482
531
|
// Compatibility bridge: conversations created while the retired edge pool was the default
|
|
483
532
|
// still select "ustc-lb". Until they are migrated, that id must resolve to a working row,
|
|
@@ -485,7 +534,7 @@ export function apply(ctx, config) {
|
|
|
485
534
|
ctx.effect(() => ctx.webServer.register({
|
|
486
535
|
kind: 'exact',
|
|
487
536
|
path: '/plugins/dsh-ustc-quota/pool/compat',
|
|
488
|
-
handler: (request, response) => {
|
|
537
|
+
handler: guarded((request, response) => {
|
|
489
538
|
void (async () => {
|
|
490
539
|
const keys = keysFromConfig({ keys: ownKeys() })
|
|
491
540
|
const key = keys[0]
|
|
@@ -497,7 +546,7 @@ export function apply(ctx, config) {
|
|
|
497
546
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
498
547
|
response.end(body)
|
|
499
548
|
})()
|
|
500
|
-
},
|
|
549
|
+
}, ['POST']),
|
|
501
550
|
}), 'dsh-ustc-quota: legacy row bridge')
|
|
502
551
|
const updateTokens = new Map();
|
|
503
552
|
|
package/package.json
CHANGED
package/pool-config.js
CHANGED
|
@@ -91,7 +91,7 @@ export function desiredProviderRow(existing, action) {
|
|
|
91
91
|
return { remove: false, skip: 'provider-route-owned-by-user', row: null }
|
|
92
92
|
}
|
|
93
93
|
const base = existing && typeof existing === 'object' ? existing : {}
|
|
94
|
-
const EFFORTS = {
|
|
94
|
+
const EFFORTS = { off: 'none', low: 'low', high: 'high', max: 'max' }
|
|
95
95
|
// Existing rows are kept, but the pool's own catalogue is merged in: a row written by an
|
|
96
96
|
// older pool version advertises model ids the current default may no longer resolve, and a
|
|
97
97
|
// provider that cannot resolve the conversation's model fails the call outright.
|
package/retire.js
CHANGED
|
@@ -33,6 +33,16 @@ export function stripManifest(text) {
|
|
|
33
33
|
profile.dependencies = next
|
|
34
34
|
changed = true
|
|
35
35
|
}
|
|
36
|
+
// Real profiles declare the plugin in the top-level dependency map, not inside the profile
|
|
37
|
+
// block; leaving it there lets a later install pull the retired package back.
|
|
38
|
+
for (const field of ['dependencies', 'devDependencies', 'optionalDependencies']) {
|
|
39
|
+
const map = parsed[field]
|
|
40
|
+
if (!map || typeof map !== 'object' || map[MANAGER_PACKAGE] === undefined) continue
|
|
41
|
+
const next = Object.assign({}, map)
|
|
42
|
+
delete next[MANAGER_PACKAGE]
|
|
43
|
+
parsed[field] = next
|
|
44
|
+
changed = true
|
|
45
|
+
}
|
|
36
46
|
return changed ? JSON.stringify(parsed, null, 2) + '\n' : null
|
|
37
47
|
}
|
|
38
48
|
|