dsh-ustc-quota 2.0.1 → 2.0.2

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.
Files changed (3) hide show
  1. package/client.js +1 -1
  2. package/index.js +59 -2
  3. package/package.json +1 -1
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.1';
58
+ const CLIENT_VERSION = '2.0.2';
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
@@ -33,6 +33,27 @@ const POOL_ROWS_PATH = '/plugins/dsh-ustc-quota/pool/rows'
33
33
  const poolSticky = new Map()
34
34
  let poolLastCall = null
35
35
 
36
+ /**
37
+ * Decide whether the step about to run should switch keys.
38
+ *
39
+ * Returns the selection to apply, or null when the agent must stay where it is. Only pool
40
+ * providers are managed: a conversation the user pointed at a non-pool provider is left alone.
41
+ */
42
+ export function nextSelectionFor(current, keys, sessionId) {
43
+ if (current === null || typeof current !== 'object') return null
44
+ if (typeof current.provider !== 'string' || !current.provider.startsWith('ustc-')) return null
45
+ const pick = pickPoolKey(keys, sessionId)
46
+ if (pick === null) return null
47
+ if (pick.provider === current.provider) return { provider: current.provider, model: current.model, reasoningEffort: current.reasoningEffort, changed: false, reason: pick.reason }
48
+ return {
49
+ provider: pick.provider,
50
+ model: current.model,
51
+ ...(current.reasoningEffort === undefined ? {} : { reasoningEffort: current.reasoningEffort }),
52
+ changed: true,
53
+ reason: pick.reason,
54
+ }
55
+ }
56
+
36
57
  /** Pick the key for one model call: sticky first, then the least-loaded healthy one. */
37
58
  export function pickPoolKey(keys, sessionId) {
38
59
  const all = (Array.isArray(keys) ? keys : []).filter((entry) =>
@@ -112,7 +133,7 @@ function rejectGuard(response, reason) {
112
133
  const UPDATE_PATH = '/plugins/dsh-ustc-quota/update';
113
134
  const UPDATE_APPLY_PATH = '/plugins/dsh-ustc-quota/update/apply';
114
135
  const UPDATE_TOKEN_TTL_MS = 30 * 60 * 1000;
115
- const PLUGIN_VERSION = '2.0.1';
136
+ const PLUGIN_VERSION = '2.0.2';
116
137
  const KEY_INFO_PATH = 'key/info';
117
138
  const USER_INFO_PATH = 'user/info';
118
139
  const DISCOVERY_TTL_MS = 10_000;
@@ -137,6 +158,8 @@ export function apply(ctx, config) {
137
158
  // Every configured key gets its native provider row, so the host lists it like any other
138
159
  // provider and a conversation can simply select it.
139
160
  const poolRowResults = []
161
+ let poolSnapshot = null
162
+ let lastPoolMove = null
140
163
  // Observation only: the waterfall hands us the frozen options and the next() chain, so we
141
164
  // record and pass through. Rewriting here is impossible (the host's fallback keeps the
142
165
  // original options), which is why selection happens through selectModel instead.
@@ -164,6 +187,40 @@ export function apply(ctx, config) {
164
187
  readKeys: ownKeys,
165
188
  onChange: () => {},
166
189
  })
190
+ // Every agent — top-level or delegated child — gets its key here.
191
+ //
192
+ // A subagent inherits the parent's provider at creation (dsh-subagent
193
+ // parentAgentOptionsForDelegation) and the controller's selectModel refuses child sessions
194
+ // with a subagent-ownership error, but the agent's own model cursor is writable: appending
195
+ // a model/selection event is exactly what the model picker does. Doing it in pre-step means
196
+ // the very step about to run already uses the chosen key.
197
+ ctx.on('agent/pre-step', ({ agent, step }, next) => {
198
+ try {
199
+ const current = agent && agent.model && typeof agent.model.current === 'function' ? agent.model.current() : null
200
+ const sessionId = agent && agent.session && typeof agent.session.id === 'string' ? agent.session.id : null
201
+ const decision = nextSelectionFor(current, poolSnapshot === null ? [] : poolSnapshot.keys, sessionId)
202
+ if (decision !== null) {
203
+ if (decision.changed) {
204
+ agent.model.select({
205
+ provider: decision.provider,
206
+ model: decision.model,
207
+ ...(decision.reasoningEffort === undefined ? {} : { reasoningEffort: decision.reasoningEffort }),
208
+ })
209
+ }
210
+ lastPoolMove = { sessionId, from: current.provider, to: decision.provider, reason: decision.reason, changed: decision.changed, step, at: Date.now() }
211
+ }
212
+ } catch { /* never break a step */ }
213
+ return next()
214
+ })
215
+ // Keep a fresh snapshot for the pre-step hook without awaiting inside it.
216
+ ctx.effect(() => {
217
+ const refresh = () => {
218
+ void reader.snapshot(false, 'zh').then((snap) => { poolSnapshot = snap && Array.isArray(snap.keys) ? snap : null }).catch(() => {})
219
+ }
220
+ refresh()
221
+ const timer = setInterval(refresh, 60_000)
222
+ return () => clearInterval(timer)
223
+ }, 'dsh-ustc-quota: pool snapshot cache')
167
224
  // New conversations inherit the agent default model, so that default is the only lever
168
225
  // the pool has over a conversation's FIRST call. Keep it on the least loaded key.
169
226
  let lastRotation = null
@@ -212,7 +269,7 @@ export function apply(ctx, config) {
212
269
  kind: 'exact',
213
270
  path: '/plugins/dsh-ustc-quota/pool/last',
214
271
  handler: (request, response) => {
215
- const body = JSON.stringify({ ok: true, last: lastCall })
272
+ const body = JSON.stringify({ ok: true, last: lastCall, move: lastPoolMove })
216
273
  response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
217
274
  response.end(body)
218
275
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-ustc-quota",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Live 3h/12h/24h USTC token quota card in the DSH sidebar (works with the USTC TokenWorks distribution).",