dsh-agent-sdk-bridge 1.1.7 → 1.2.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.
package/src/usage.ts CHANGED
@@ -18,7 +18,7 @@
18
18
  import type { AccountInfo, UsageInfo } from '@qodercn-ai/qodercn-agent-sdk'
19
19
  import type { Live } from './live.ts'
20
20
  import { readLive } from './live.ts'
21
- import { cliConflictMessage, foreignCliOverride, qoderCliOptions } from './cli.ts'
21
+ import { cliConflictMessage, cliFailureText, foreignCliOverride, qoderCliOptions, resolveQoderCli } from './cli.ts'
22
22
  import { withTimeout } from './models.ts'
23
23
  import type { QoderBrand } from './brand.ts'
24
24
  import { brandProfile } from './brand.ts'
@@ -39,6 +39,16 @@ export interface UsageSnapshot {
39
39
  account?: AccountInfo
40
40
  /** Why the quota is missing, in the CLI's own words. */
41
41
  error?: string
42
+ /**
43
+ * Whether this edition's CLI is absent from the machine right now.
44
+ *
45
+ * The page keeps its last good reading visible while a refresh fails, which
46
+ * is right for a dropped network — but not after an uninstall: the account,
47
+ * plan and credits it shows belong to a CLI that is no longer installed.
48
+ * This flag tells the page to drop that reading instead of labelling it
49
+ * "last reading".
50
+ */
51
+ cliMissing?: boolean
42
52
  }
43
53
 
44
54
  /** Streaming prompt that never yields, so no model turn runs during the read. */
@@ -69,6 +79,14 @@ export class QoderUsage {
69
79
  * @returns the reading; `error` carries a failed lookup instead of throwing.
70
80
  */
71
81
  async read(fresh = false): Promise<UsageSnapshot> {
82
+ // An uninstalled CLI makes every cached number describe a machine state
83
+ // that no longer exists: the account, plan and credits came from a binary
84
+ // that is gone. Answer that before the cache is consulted, and drop the
85
+ // reading so the page cannot paint it again on the next render.
86
+ if (resolveQoderCli(this.brand) === undefined) {
87
+ this.cached = undefined
88
+ return this.missingCli()
89
+ }
72
90
  const ttlMs = readLive(this.ttlMs, DEFAULT_USAGE_CACHE_TTL_MS)
73
91
  if (!fresh && this.cached !== undefined && Date.now() - this.cached.at < ttlMs) return this.cached
74
92
  this.inflight ??= this.fetch().finally(() => { this.inflight = undefined })
@@ -82,11 +100,23 @@ export class QoderUsage {
82
100
  this.cached = undefined
83
101
  }
84
102
 
103
+ /** The reading for a machine whose CLI is not installed. */
104
+ private missingCli(): UsageSnapshot {
105
+ const command = brandProfile(this.brand).cliCommands[0]
106
+ return {
107
+ at: Date.now(),
108
+ cliMissing: true,
109
+ error: `未检测到可用的 ${command},请先安装并登录后再使用此版本。`,
110
+ }
111
+ }
112
+
85
113
  private async fetch(): Promise<UsageSnapshot> {
86
114
  const at = Date.now()
87
115
  try {
88
116
  const conflict = foreignCliOverride(this.brand)
89
- if (conflict !== undefined) return { at, error: cliConflictMessage(this.brand) }
117
+ if (conflict !== undefined) {
118
+ return { at, cliMissing: true, error: cliConflictMessage(this.brand) }
119
+ }
90
120
  const sdk = await loadSdk(this.brand)
91
121
  const timeoutMs = readLive(this.timeoutMs, DEFAULT_USAGE_TIMEOUT_MS)
92
122
  const q = sdk.query({
@@ -121,7 +151,7 @@ export class QoderUsage {
121
151
  const missingCli = /ENOENT|not found|Cannot find package|未安装国际版 SDK/i.test(detail)
122
152
  return {
123
153
  at,
124
- error: missingCli ? `未检测到可用的 ${command},请先安装并登录后再读取额度。` : detail,
154
+ error: missingCli ? `未检测到可用的 ${command},请先安装并登录后再读取额度。` : cliFailureText(detail, this.brand),
125
155
  }
126
156
  }
127
157
  }