@gatemux/dsh-gatemux-login-core 1.3.1 → 1.4.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.
Files changed (4) hide show
  1. package/account.js +53 -8
  2. package/llm.js +49 -35
  3. package/package.json +19 -17
  4. package/search.js +17 -9
package/account.js CHANGED
@@ -170,8 +170,17 @@ function apiErrorCode(body) {
170
170
  *
171
171
  * @param ctx - the account provider's lifetime.
172
172
  */
173
- function installGateMuxTaskCancellation(ctx) {
173
+ function installGateMuxTaskCancellation(ctx, account) {
174
174
  ctx.inject(['agents'], (scope) => {
175
+ const runningGateMuxTask = () => scope.agents.list().some(agent =>
176
+ agent.status === 'running' && agent.session.requestContext()?.provider === PROVIDER)
177
+ // DSH 0.2.2-rc.4 moved the account's running-task answer from inspecting
178
+ // ctx.agents to reports registered by the routes billing the account. The
179
+ // report is optional here so an older account base still constructs.
180
+ if (typeof account.registerRunningTaskReport === 'function') {
181
+ const dispose = account.registerRunningTaskReport(runningGateMuxTask)
182
+ if (typeof scope.effect === 'function') scope.effect(() => dispose, 'gatemux-account: running task report')
183
+ }
175
184
  scope.on('agent/error', ({ error }) => {
176
185
  if (error instanceof LlmError && error.code === 'ACCOUNT_SIGN_IN_REQUIRED') {
177
186
  scope.emit('deepseek-account/model-sign-in-required')
@@ -205,6 +214,8 @@ export class GateMuxAccount extends DeepSeekAccount {
205
214
  attempt
206
215
  listeners = new Set()
207
216
  detailsLifetime = new AbortController()
217
+ /** Aborts credentials released for in-flight requests when the account signs out. */
218
+ credentialRevoked = new AbortController()
208
219
  lastProfile
209
220
  refreshInFlight
210
221
  removing
@@ -216,7 +227,7 @@ export class GateMuxAccount extends DeepSeekAccount {
216
227
  */
217
228
  constructor(ctx, config = {}) {
218
229
  super(ctx)
219
- installGateMuxTaskCancellation(ctx)
230
+ installGateMuxTaskCancellation(ctx, this)
220
231
  const clientId = config.clientId ?? launchEnvironmentOf(ctx).get(CLIENT_ID_ENV)?.value
221
232
  if (typeof clientId !== 'string' || clientId.length === 0) {
222
233
  throw new GateMuxAuthError(
@@ -255,6 +266,7 @@ export class GateMuxAccount extends DeepSeekAccount {
255
266
  })
256
267
  ctx.effect(() => async () => {
257
268
  this.closed = true
269
+ this.credentialRevoked.abort()
258
270
  this.invalidateDetails()
259
271
  const active = this.attempt
260
272
  if (active !== undefined) {
@@ -333,16 +345,26 @@ export class GateMuxAccount extends DeepSeekAccount {
333
345
  return { status: 'ready', value: [{ currency, balance }], bonusWallets: [] }
334
346
  }
335
347
 
348
+ // The Remote signatures below must keep the base class's parameter names
349
+ // exactly. This module ships plain JavaScript, so the Typert Gateway has no
350
+ // generated descriptor to read: it reflects each overridden method's source
351
+ // text and uses the raw parameter names as the wire field names
352
+ // (`methodParameterNames` in the gateway). A renamed or dropped parameter is
353
+ // therefore a field mismatch that rejects the call before this code runs —
354
+ // which is what made sign-in fail with "please retry" while no browser ever
355
+ // opened. Parameters must stay plain identifiers: defaults, destructuring,
356
+ // and rest all fail the gateway's signature check outright.
357
+
336
358
  /** GateMux has no notification feed; the account UI shows none. */
337
- async getNotifications() { return null }
359
+ async getNotifications(client) { return null }
338
360
 
339
361
  /** GateMux has no notification content. */
340
- async getNotificationContent() { return null }
362
+ async getNotificationContent(id, client) { return null }
341
363
 
342
364
  /** GateMux has no notification events to report. */
343
- async reportNotificationEvent() { return false }
365
+ async reportNotificationEvent(id, event, client) { return false }
344
366
 
345
- async startSignIn(_client, callbackOrigin, loginSource) {
367
+ async startSignIn(client, callbackOrigin, loginSource) {
346
368
  if (this.removing !== undefined) await this.removing
347
369
  const origin = loginOrigin(callbackOrigin)
348
370
  if (this.closed) throw new GateMuxAuthError('provider is closed', 'protocol')
@@ -360,6 +382,7 @@ export class GateMuxAccount extends DeepSeekAccount {
360
382
  if (this.closed) throw new GateMuxAuthError('provider is closed', 'protocol')
361
383
  if (this.attempt !== active) return this.getState()
362
384
  }
385
+ this.credentialRevoked = new AbortController()
363
386
  const attempt = {
364
387
  origin,
365
388
  loginSource,
@@ -404,9 +427,9 @@ export class GateMuxAccount extends DeepSeekAccount {
404
427
  return this.getState()
405
428
  }
406
429
 
407
- async cancelSignIn(id) {
430
+ async cancelSignIn(attemptId) {
408
431
  const attempt = this.attempt
409
- if (attempt?.view.id === id) {
432
+ if (attempt?.view.id === attemptId) {
410
433
  if (attempt.view.phase !== 'committing') {
411
434
  attempt.controller.abort()
412
435
  this.ctx.authorization.cancel(KEY)
@@ -435,6 +458,7 @@ export class GateMuxAccount extends DeepSeekAccount {
435
458
  }
436
459
  this.attempt = undefined
437
460
  this.ctx.emit('deepseek-account/signed-out')
461
+ this.credentialRevoked.abort()
438
462
  this.changed('credentials-changed')
439
463
  return this.getState()
440
464
  })().finally(() => { this.removing = undefined })
@@ -477,6 +501,26 @@ export class GateMuxAccount extends DeepSeekAccount {
477
501
  }
478
502
  }
479
503
 
504
+ /**
505
+ * Release the GateMux credential for one request. DSH 0.2.2-rc.4's
506
+ * deepseek-auth service calls this on the account stand-in; GateMux accepts
507
+ * the OAuth token as a Bearer header, unlike the DeepSeek account header.
508
+ * @param url - the request destination.
509
+ * @returns request headers, the revocation signal, and the rejection hook.
510
+ */
511
+ async resolveRequestCredential(url) {
512
+ const revoked = this.credentialRevoked.signal
513
+ const token = await this.resolveToken(url)
514
+ if (token === undefined) return undefined
515
+ return {
516
+ headers: { authorization: `Bearer ${token}` },
517
+ revoked,
518
+ reject: async () => {
519
+ try { await this.rejectToken(token) } catch { /* the destination's rejection is the failure to report */ }
520
+ },
521
+ }
522
+ }
523
+
480
524
  /** Resolve a token only for the GateMux REST origin, refreshing it when stale. */
481
525
  async resolveToken(url) {
482
526
  if (this.closed || this.removing !== undefined) return undefined
@@ -785,6 +829,7 @@ export class GateMuxAccount extends DeepSeekAccount {
785
829
  this.ctx.emit('deepseek-account/session-expired')
786
830
  this.attempt = undefined
787
831
  this.ctx.emit('deepseek-account/signed-out')
832
+ this.credentialRevoked.abort()
788
833
  this.changed('credentials-changed')
789
834
  return this.getState()
790
835
  })().finally(() => { this.removing = undefined })
package/llm.js CHANGED
@@ -27,7 +27,7 @@ export { Config }
27
27
  /** Route owned by this plugin; distinct from the pi-ai `gatemux` route. */
28
28
  export const PROVIDER = 'gatemux-account'
29
29
  export const name = 'gatemux-account'
30
- export const inject = ['llm']
30
+ export const inject = ['llm', 'deepseekAuth']
31
31
 
32
32
  /** Listing value a model must carry to be reachable through this route's Messages transport. */
33
33
  const MESSAGES_PROTOCOL = 'anthropic-messages'
@@ -144,6 +144,13 @@ export function apply(ctx, config) {
144
144
  // Validate the composed configuration now so a misconfiguration fails loudly at load.
145
145
  baseOptions()
146
146
 
147
+ // DSH 0.2.2-rc.4's deepseek-auth service owns the account credential candidates;
148
+ // connect the GateMux deployment to this route so its account candidate serves it.
149
+ ctx.effect(
150
+ () => ctx.deepseekAuth.connect('account', PROVIDER, () => ({ baseURL: baseOptions().baseURL })),
151
+ 'gatemux-account: account deployment',
152
+ )
153
+
147
154
  // The catalog the endpoint last reported. Both the advertised model list and
148
155
  // every later capability resolution read it, because `modelInfo` derives a
149
156
  // model's context window and input modalities from `connection.models`: a
@@ -176,41 +183,53 @@ export function apply(ctx, config) {
176
183
  return entries.map(servedModel).filter(model => model !== undefined)
177
184
  }
178
185
 
179
- const resolveAuth = async (connection) => {
180
- const account = ctx.get('deepseekAccount')
181
- const token = await account?.resolveToken(connection.baseURL)
182
- if (token === undefined) {
183
- throw new LlmError(
184
- 'Sign in with GateMux to use the GateMux model route. Model requests are billed to your own GateMux balance.',
185
- 'ACCOUNT_SIGN_IN_REQUIRED',
186
- )
187
- }
186
+ const resolveAuth = async (connection, endpoint) => {
187
+ const authorization = await ctx.deepseekAuth.authorize({
188
+ consumer: 'gatemux-account',
189
+ accepts: ['account'],
190
+ destination: { url: endpoint },
191
+ connections: { account: { baseURL: connection.baseURL } },
192
+ })
193
+ if (authorization === undefined) return undefined
188
194
  return {
189
- // GateMux accepts `Authorization: Bearer`; it rejects the DeepSeek-only `x-dsh-auth-token` header.
190
- headers: { authorization: `Bearer ${token}` },
195
+ headers: authorization.headers,
196
+ revoked: authorization.revoked,
191
197
  onRequestError: async (error) => {
192
- if (!(error instanceof LlmError)) return error
193
- const status = error.failure.status
194
- // 402 insufficient_balance and an exhausted quota both mean the user must top up.
195
- if (error.code === QUOTA_EXCEEDED_CODE) {
196
- return new LlmError(error.message, ACCOUNT_QUOTA_EXCEEDED_CODE, { ...error.failure, cause: error })
197
- }
198
- // 403 scope_denied / model_not_allowed / ip_not_allowed stay AUTH: they are
199
- // configuration faults that re-authorizing the same way would not fix.
200
- if (status !== 401) return error
201
- // 401 credential_invalid: the credential is revoked, expired, or suspended.
202
- // Re-authorization is required, so the stored credential is dropped.
203
- const rejected = new LlmError(error.message, 'ACCOUNT_TOKEN_INVALID', { ...error.failure, cause: error })
204
198
  try {
205
- await account?.rejectToken(token)
199
+ // A sign-out ends every request carrying the released credential.
200
+ if (authorization.revoked?.aborted === true) {
201
+ return new LlmError(
202
+ 'Signed out of GateMux while the request was running.',
203
+ 'ACCOUNT_SIGN_IN_REQUIRED',
204
+ { cause: error },
205
+ )
206
+ }
207
+ if (!(error instanceof LlmError)) return error
208
+ const status = error.failure.status
209
+ // 402 insufficient_balance and an exhausted quota both mean the user must top up.
210
+ if (error.code === QUOTA_EXCEEDED_CODE) {
211
+ return new LlmError(error.message, ACCOUNT_QUOTA_EXCEEDED_CODE, { ...error.failure, cause: error })
212
+ }
213
+ // 403 scope_denied / model_not_allowed / ip_not_allowed stay AUTH: they
214
+ // are configuration faults that re-authorizing the same way would not fix.
215
+ if (status !== 401) return error
216
+ // 401 credential_invalid: the credential is revoked, expired, or suspended.
217
+ const rejected = new LlmError(error.message, 'ACCOUNT_TOKEN_INVALID', { ...error.failure, cause: error })
218
+ await authorization.reject?.()
219
+ return rejected
206
220
  } catch (failure) {
207
- logger.warn('credential removal after rejection failed', failure)
221
+ logger.warn('credential rejection handling failed', failure)
222
+ return error
208
223
  }
209
- return rejected
210
224
  },
211
225
  }
212
226
  }
213
227
 
228
+ const missingCredential = () => new LlmError(
229
+ 'Sign in with GateMux to use the GateMux model route. Model requests are billed to your own GateMux balance.',
230
+ 'ACCOUNT_SIGN_IN_REQUIRED',
231
+ )
232
+
214
233
  ctx.llm.registerConfigurableProviders([
215
234
  { provider: PROVIDER, displayName: 'GateMux Account', settingsNs: ctx.fiber.entry?.options.id ?? name, settingsPath: [] },
216
235
  ])
@@ -218,17 +237,12 @@ export function apply(ctx, config) {
218
237
  registerDeepSeekProvider(ctx, PROVIDER, {
219
238
  options,
220
239
  resolveAuth,
240
+ missingCredential,
221
241
  providerName: 'GateMux Account',
222
242
  discoverModels: async (provider) => {
223
243
  const connection = baseOptions()
224
- let auth
225
- try {
226
- auth = await resolveAuth(connection)
227
- } catch (error) {
228
- // With no signed-in user the route advertises nothing rather than failing discovery.
229
- if (error instanceof LlmError && error.code === 'ACCOUNT_SIGN_IN_REQUIRED') return []
230
- throw error
231
- }
244
+ const auth = await resolveAuth(connection, listingUrl(connection.baseURL))
245
+ if (auth === undefined) return []
232
246
  try {
233
247
  discovered = await loadCatalog(connection, auth.headers)
234
248
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gatemux/dsh-gatemux-login-core",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "description": "GateMux account provider and model route for DeepSeek Harness. Shared code; install a region bundle instead of this package.",
6
6
  "publishConfig": {
@@ -24,26 +24,28 @@
24
24
  },
25
25
  "peerDependencies": {
26
26
  "@deepseek-ai/cordis": "^4.0.4",
27
- "@deepseek-ai/dsh-authorization": "^0.2.2-rc.2",
28
- "@deepseek-ai/dsh-credentials": "^0.2.2-rc.2",
29
- "@deepseek-ai/dsh-deepseek-account": "^0.2.2-rc.2",
30
- "@deepseek-ai/dsh-host-webserver": "^0.2.2-rc.2",
31
- "@deepseek-ai/dsh-launch-environment": "^0.2.2-rc.2",
32
- "@deepseek-ai/dsh-llm": "^0.2.2-rc.2",
33
- "@deepseek-ai/dsh-llm-deepseek": "^0.2.2-rc.2",
34
- "@deepseek-ai/dsh-web": "^0.2.2-rc.2",
27
+ "@deepseek-ai/dsh-authorization": "^0.2.2-rc.4",
28
+ "@deepseek-ai/dsh-credentials": "^0.2.2-rc.4",
29
+ "@deepseek-ai/dsh-deepseek-account": "^0.2.2-rc.4",
30
+ "@deepseek-ai/dsh-deepseek-auth": "^0.2.2-rc.4",
31
+ "@deepseek-ai/dsh-host-webserver": "^0.2.2-rc.4",
32
+ "@deepseek-ai/dsh-launch-environment": "^0.2.2-rc.4",
33
+ "@deepseek-ai/dsh-llm": "^0.2.2-rc.4",
34
+ "@deepseek-ai/dsh-llm-deepseek": "^0.2.2-rc.4",
35
+ "@deepseek-ai/dsh-web": "^0.2.2-rc.4",
35
36
  "@deepseek-ai/schemastery": "^3.18.4"
36
37
  },
37
38
  "devDependencies": {
38
39
  "@deepseek-ai/cordis": "^4.0.4",
39
- "@deepseek-ai/dsh-authorization": "^0.2.2-rc.2",
40
- "@deepseek-ai/dsh-credentials": "^0.2.2-rc.2",
41
- "@deepseek-ai/dsh-deepseek-account": "^0.2.2-rc.2",
42
- "@deepseek-ai/dsh-host-webserver": "^0.2.2-rc.2",
43
- "@deepseek-ai/dsh-launch-environment": "^0.2.2-rc.2",
44
- "@deepseek-ai/dsh-llm": "^0.2.2-rc.2",
45
- "@deepseek-ai/dsh-llm-deepseek": "^0.2.2-rc.2",
46
- "@deepseek-ai/dsh-web": "^0.2.2-rc.2",
40
+ "@deepseek-ai/dsh-authorization": "^0.2.2-rc.4",
41
+ "@deepseek-ai/dsh-credentials": "^0.2.2-rc.4",
42
+ "@deepseek-ai/dsh-deepseek-account": "^0.2.2-rc.4",
43
+ "@deepseek-ai/dsh-deepseek-auth": "^0.2.2-rc.4",
44
+ "@deepseek-ai/dsh-host-webserver": "^0.2.2-rc.4",
45
+ "@deepseek-ai/dsh-launch-environment": "^0.2.2-rc.4",
46
+ "@deepseek-ai/dsh-llm": "^0.2.2-rc.4",
47
+ "@deepseek-ai/dsh-llm-deepseek": "^0.2.2-rc.4",
48
+ "@deepseek-ai/dsh-web": "^0.2.2-rc.4",
47
49
  "@deepseek-ai/schemastery": "^3.18.4"
48
50
  }
49
51
  }
package/search.js CHANGED
@@ -53,7 +53,7 @@ export const Config = Schema.object({
53
53
  export const name = 'gatemux-search'
54
54
 
55
55
  /** The web seam this provider registers into. */
56
- export const inject = ['web']
56
+ export const inject = ['web', 'deepseekAuth']
57
57
 
58
58
  /** One normalized result item, kept before the seam applies its own bounds. */
59
59
  function sourceOf(item, seen) {
@@ -158,9 +158,13 @@ export class GateMuxSearchProvider {
158
158
  */
159
159
  async search(request, signal) {
160
160
  if (signal?.aborted === true) throw new WebError('GateMux web search was cancelled', 'WEB_ABORTED')
161
- const account = this.ctx.get('deepseekAccount')
162
- const token = await account?.resolveToken(this.endpoint)
163
- if (token === undefined || token.length === 0) {
161
+ const authorization = await this.ctx.deepseekAuth.authorize({
162
+ consumer: 'gatemux-search',
163
+ accepts: ['account'],
164
+ destination: { url: this.endpoint },
165
+ connections: { account: { baseURL: this.config.baseURL } },
166
+ })
167
+ if (authorization === undefined) {
164
168
  throw new WebError(
165
169
  'Sign in with GateMux to use web search. The search is billed to your own GateMux balance.',
166
170
  'ACCOUNT_SIGN_IN_REQUIRED',
@@ -177,16 +181,17 @@ export class GateMuxSearchProvider {
177
181
  tools: [{ type: 'web_search_20250305', name: 'web_search', max_uses: this.config.maxUses }],
178
182
  }
179
183
  const timeout = AbortSignal.timeout(this.config.timeoutMs)
180
- const fused = signal === undefined ? timeout : AbortSignal.any([signal, timeout])
184
+ const signals = [timeout]
185
+ if (signal !== undefined) signals.push(signal)
186
+ if (authorization.revoked !== undefined) signals.push(authorization.revoked)
187
+ const fused = signals.length === 1 ? signals[0] : AbortSignal.any(signals)
181
188
  let response
182
189
  try {
183
190
  response = await fetch(this.endpoint, {
184
191
  method: 'POST',
185
192
  redirect: 'error',
186
193
  headers: {
187
- // GateMux accepts the account's Bearer credential; the DeepSeek-only
188
- // `x-dsh-auth-token` header is not used here.
189
- authorization: `Bearer ${token}`,
194
+ ...authorization.headers,
190
195
  'anthropic-version': ANTHROPIC_VERSION,
191
196
  'content-type': 'application/json',
192
197
  accept: 'application/json',
@@ -196,6 +201,9 @@ export class GateMuxSearchProvider {
196
201
  })
197
202
  } catch (error) {
198
203
  if (signal?.aborted === true) throw new WebError('GateMux web search was cancelled', 'WEB_ABORTED')
204
+ if (authorization.revoked?.aborted === true) {
205
+ throw new WebError('Signed out of GateMux while the search was running.', 'ACCOUNT_SIGN_IN_REQUIRED', { cause: error })
206
+ }
199
207
  throw new WebError(`GateMux web search request failed: ${String(error)}`, 'WEB_PROVIDER_ERROR', { cause: error })
200
208
  }
201
209
 
@@ -210,7 +218,7 @@ export class GateMuxSearchProvider {
210
218
  }
211
219
  if (response.status === 401) {
212
220
  try {
213
- await account?.rejectToken(token)
221
+ await authorization.reject?.()
214
222
  } catch {
215
223
  // The inference failure is the answer; a storage failure cannot replace it.
216
224
  }