dsh-agent-sdk-bridge 1.2.1 → 1.2.3
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/lib/index.js +334 -81
- package/lib/types/codebuddy-state.d.ts +23 -0
- package/lib/types/codebuddy.d.ts +7 -5
- package/lib/types/models.d.ts +1 -0
- package/lib/types/sdk.d.ts +74 -8
- package/lib/types/usage.d.ts +1 -0
- package/package.json +1 -1
- package/src/cli.ts +11 -0
- package/src/codebuddy-state.ts +53 -0
- package/src/codebuddy.ts +85 -27
- package/src/models.ts +12 -3
- package/src/sdk.ts +172 -22
- package/src/session.ts +35 -6
- package/src/usage.ts +31 -27
package/lib/types/codebuddy.d.ts
CHANGED
|
@@ -282,12 +282,14 @@ export declare class CodeBuddySession {
|
|
|
282
282
|
* compacts (and the gateway rejects) anything past 276K — while the host,
|
|
283
283
|
* told 1M, keeps showing and feeding a larger history. Replaying that history
|
|
284
284
|
* then collapses to a summary. Applying the same number the host divides by
|
|
285
|
-
* keeps the two in step;
|
|
286
|
-
*
|
|
285
|
+
* keeps the two in step; the adapter therefore sends a window for every model
|
|
286
|
+
* it knows about, not only the ones that advertise a choice — the mainland
|
|
287
|
+
* catalog declares no lengths anywhere and its physical `maxInputTokens` is a
|
|
288
|
+
* provider number the CLI is free to ignore when left to itself.
|
|
287
289
|
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
290
|
+
* A rejected value keeps the CLI on its own default, which is the one case
|
|
291
|
+
* where the two denominators can still disagree; it is reported nowhere, so
|
|
292
|
+
* the host's figure stays the visible contract.
|
|
291
293
|
*/
|
|
292
294
|
private applyContextWindow;
|
|
293
295
|
/**
|
package/lib/types/models.d.ts
CHANGED
|
@@ -67,6 +67,7 @@ export declare class QoderModelCatalog {
|
|
|
67
67
|
/** dsh catalog entries: the enabled live list, or the static fallback. */
|
|
68
68
|
models(): Promise<readonly QoderCatalogModel[]>;
|
|
69
69
|
private fetch;
|
|
70
|
+
private read;
|
|
70
71
|
}
|
|
71
72
|
/**
|
|
72
73
|
* The credit multiplier upstream quotes for one model.
|
package/lib/types/sdk.d.ts
CHANGED
|
@@ -3,17 +3,35 @@
|
|
|
3
3
|
*
|
|
4
4
|
* The Qoder editions publish the same API under different scopes, so the rest
|
|
5
5
|
* of the plugin imports this module instead of a package name. The CN SDK is a
|
|
6
|
-
* real dependency and is loaded
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* real dependency and is loaded eagerly; the international one is optional at
|
|
7
|
+
* runtime — a deployment that only has a CN account may not have it installed —
|
|
8
|
+
* so it is imported on demand and its absence is reported to the settings page
|
|
9
|
+
* as a plain error instead of failing the plugin's mount.
|
|
10
10
|
*
|
|
11
11
|
* CodeBuddy has its own, unrelated API and is loaded dynamically for the same
|
|
12
12
|
* reason: an account may never use it, and its package carries a large CLI.
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* ## Why modules are reloaded
|
|
15
|
+
*
|
|
16
|
+
* Every Qoder SDK routes all queries through one **process-wide daemon** that
|
|
17
|
+
* is initialized with the first query's launch configuration (CLI path,
|
|
18
|
+
* interpreter, environment, proxy) and then refuses any different one for the
|
|
19
|
+
* life of the process:
|
|
20
|
+
*
|
|
21
|
+
* DaemonConfigurationConflictError:
|
|
22
|
+
* Global daemon is already initialized with different options
|
|
23
|
+
*
|
|
24
|
+
* The host is long-lived, so its configuration is not: a CLI can be
|
|
25
|
+
* reinstalled, an edition can appear, an installer can change the machine
|
|
26
|
+
* environment. When that happens the ready daemon no longer matches and every
|
|
27
|
+
* later query fails until the process restarts — a state the settings page
|
|
28
|
+
* cannot recover from on its own.
|
|
29
|
+
*
|
|
30
|
+
* So the module tracks the configuration signature it loaded under and takes a
|
|
31
|
+
* fresh instance whenever the signature changes; {@link resetSdk} does the same
|
|
32
|
+
* after a conflict is observed. A new instance has an empty daemon registry, so
|
|
33
|
+
* it initializes against the current configuration instead of arguing with the
|
|
34
|
+
* old one.
|
|
17
35
|
*
|
|
18
36
|
* @module dsh-agent-sdk-bridge/sdk
|
|
19
37
|
*/
|
|
@@ -26,12 +44,60 @@ export type QoderSdkModule = typeof QoderSdk;
|
|
|
26
44
|
/** The CodeBuddy SDK module, typed from its declarations. */
|
|
27
45
|
export type CodeBuddySdkModule = typeof CodeBuddySdk;
|
|
28
46
|
/**
|
|
29
|
-
*
|
|
47
|
+
* The launch configuration a Qoder query would use, as a comparable string.
|
|
48
|
+
*
|
|
49
|
+
* Mirrors the fields the SDK's own daemon compares before reusing itself. The
|
|
50
|
+
* environment is included because the SDK hands it to the CLI verbatim, and a
|
|
51
|
+
* difference there is enough to make the daemon reject a query.
|
|
52
|
+
*
|
|
53
|
+
* @param brand - which edition's configuration to describe.
|
|
54
|
+
* @returns a stable string that changes exactly when the configuration does.
|
|
55
|
+
*/
|
|
56
|
+
export declare function configurationSignature(brand: QoderBrand): string;
|
|
57
|
+
/**
|
|
58
|
+
* Load the SDK for one Qoder edition, under its current configuration.
|
|
30
59
|
*
|
|
31
60
|
* @param brand - which edition's SDK to load.
|
|
32
61
|
* @returns the SDK module.
|
|
33
62
|
*/
|
|
34
63
|
export declare function loadSdk(brand: QoderBrand): Promise<QoderSdkModule>;
|
|
64
|
+
/**
|
|
65
|
+
* Drop the loaded instance so the next {@link loadSdk} starts a new daemon.
|
|
66
|
+
*
|
|
67
|
+
* @param brand - which edition to reset.
|
|
68
|
+
*/
|
|
69
|
+
export declare function resetSdk(brand: QoderBrand): void;
|
|
70
|
+
/**
|
|
71
|
+
* Whether an error is the SDK's daemon refusing a different configuration.
|
|
72
|
+
*
|
|
73
|
+
* @param error - whatever a query threw.
|
|
74
|
+
*/
|
|
75
|
+
export declare function isDaemonConflict(error: unknown): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Run one idempotent SDK operation, recovering from a daemon conflict once.
|
|
78
|
+
*
|
|
79
|
+
* A conflict means the daemon was initialized under a configuration that no
|
|
80
|
+
* longer applies. A fresh module instance starts an empty daemon with the
|
|
81
|
+
* current one, so the retry succeeds without a restart. Only use this for
|
|
82
|
+
* operations that can be repeated without side effects (reads), never for a
|
|
83
|
+
* turn that may already have emitted output.
|
|
84
|
+
*
|
|
85
|
+
* @param brand - which edition to query.
|
|
86
|
+
* @param run - the operation, given the module to use.
|
|
87
|
+
*/
|
|
88
|
+
export declare function withSdk<T>(brand: QoderBrand, run: (sdk: QoderSdkModule) => Promise<T>): Promise<T>;
|
|
89
|
+
/**
|
|
90
|
+
* Open one cheap control query so a stale daemon is discovered before a turn
|
|
91
|
+
* starts streaming.
|
|
92
|
+
*
|
|
93
|
+
* A turn cannot be retried once it has emitted output, so the session path has
|
|
94
|
+
* to find out about a configuration conflict first. `getUsageInfo` is a
|
|
95
|
+
* control request: it initializes the daemon and reads the account without
|
|
96
|
+
* running a model turn.
|
|
97
|
+
*
|
|
98
|
+
* @param brand - which edition to probe.
|
|
99
|
+
*/
|
|
100
|
+
export declare function probeDaemon(brand: QoderBrand): Promise<void>;
|
|
35
101
|
/**
|
|
36
102
|
* Load the CodeBuddy Agent SDK.
|
|
37
103
|
*
|
package/lib/types/usage.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "dsh-agent-sdk-bridge",
|
|
3
3
|
"displayName": "Agent SDK Bridge",
|
|
4
4
|
"description": "DeepSeek Harness LLM adapter bridge for Qoder CN, Qoder International, Tencent CodeBuddy and MiniMax Code subscriptions. It reuses each provider's own SDK or official ACP server and CLI login, shows available quota, and exposes per-provider settings pages with independent international and mainland login regions.",
|
|
5
|
-
"version": "1.2.
|
|
5
|
+
"version": "1.2.3",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "k1-h2",
|
package/src/cli.ts
CHANGED
|
@@ -245,6 +245,17 @@ export function cliConflictMessage(brand: QoderBrand): string {
|
|
|
245
245
|
* @returns the original detail, prefixed with the actionable explanation.
|
|
246
246
|
*/
|
|
247
247
|
export function cliFailureText(detail: string, brand: QoderBrand): string {
|
|
248
|
+
// The SDK routes every query through one process-wide daemon pinned to the
|
|
249
|
+
// first launch configuration it saw. When the machine changes under it (a
|
|
250
|
+
// reinstall, a new edition, a different environment) every query is rejected
|
|
251
|
+
// with this message until the process restarts. The plugin now drops the SDK
|
|
252
|
+
// instance at that point, so the next request recovers; say that instead of
|
|
253
|
+
// echoing an English internal error into the page.
|
|
254
|
+
if (/DaemonConfigurationConflict|Global daemon is already initialized with different options/i.test(detail)) {
|
|
255
|
+
const command = brandProfile(brand).cliCommands[0]
|
|
256
|
+
return `本机 ${command} 的运行配置在本次会话期间发生了变化,SDK 的本地守护进程仍按旧配置运行。`
|
|
257
|
+
+ `插件已重置这部分运行时,请重试一次;若仍失败,重启 DSH Web。原始信息:${detail}`
|
|
258
|
+
}
|
|
248
259
|
if (!/exit(?:ed)?(?: with)? code 42|sdk_worker_removed/i.test(detail)) return detail
|
|
249
260
|
const profile = brandProfile(brand)
|
|
250
261
|
const pkg = profile.cliPackages[0]
|
package/src/codebuddy-state.ts
CHANGED
|
@@ -28,6 +28,24 @@ export interface CodeBuddySavedUser {
|
|
|
28
28
|
userNickname: string
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* A stored account reading with its credential removed.
|
|
33
|
+
*
|
|
34
|
+
* The CLI's `accountInfo()` answer carries a bearer token, and older versions
|
|
35
|
+
* persisted that object whole — a second copy of the credential in a file that
|
|
36
|
+
* gets backed up and pasted into bug reports, one the plugin never read. A
|
|
37
|
+
* region holds exactly one credential, in `token`.
|
|
38
|
+
*/
|
|
39
|
+
export function withoutCredential(account: CodeBuddyAccount | undefined): CodeBuddyAccount | undefined {
|
|
40
|
+
if (account === undefined) return undefined
|
|
41
|
+
const clean: Record<string, unknown> = {}
|
|
42
|
+
for (const [key, value] of Object.entries(account as Record<string, unknown>)) {
|
|
43
|
+
if (/token|secret|credential|password|cookie/i.test(key)) continue
|
|
44
|
+
clean[key] = value
|
|
45
|
+
}
|
|
46
|
+
return clean as CodeBuddyAccount
|
|
47
|
+
}
|
|
48
|
+
|
|
31
49
|
/** Everything remembered for one login region. */
|
|
32
50
|
export interface CodeBuddySavedRegion {
|
|
33
51
|
/** Bearer credential from the SDK login, used to restore the login. */
|
|
@@ -93,6 +111,7 @@ export class CodeBuddyStateStore {
|
|
|
93
111
|
const parsed = JSON.parse(readFileSync(this.file, 'utf8')) as Partial<SavedFile>
|
|
94
112
|
if (parsed.version === 1 && parsed.regions !== null && typeof parsed.regions === 'object') {
|
|
95
113
|
this.state = { version: 1, regions: parsed.regions }
|
|
114
|
+
this.recoverLostTokens()
|
|
96
115
|
}
|
|
97
116
|
} catch {
|
|
98
117
|
// A missing or unreadable file is the normal first-run state.
|
|
@@ -100,6 +119,37 @@ export class CodeBuddyStateStore {
|
|
|
100
119
|
return this.state
|
|
101
120
|
}
|
|
102
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Lift a credential the old layout hid inside the account object.
|
|
124
|
+
*
|
|
125
|
+
* The CLI's `accountInfo()` answer carries a bearer token of its own, and an
|
|
126
|
+
* earlier version persisted that whole object here. When the top-level
|
|
127
|
+
* `token` was later dropped — which any quota failure used to do, because
|
|
128
|
+
* expiry was *guessed* from the error text — the region read as signed out
|
|
129
|
+
* even though a working credential still sat in the file, and nothing could
|
|
130
|
+
* recover it without a fresh browser login. This picks the copy back up.
|
|
131
|
+
*
|
|
132
|
+
* {@link persist} no longer stores a token inside the account, so this runs
|
|
133
|
+
* only over files written by those older versions.
|
|
134
|
+
*/
|
|
135
|
+
private recoverLostTokens(): void {
|
|
136
|
+
let recovered = false
|
|
137
|
+
for (const [key, saved] of Object.entries(this.state.regions)) {
|
|
138
|
+
const region = key as CodeBuddyRegion
|
|
139
|
+
const hidden = (saved?.account as { token?: unknown } | undefined)?.token
|
|
140
|
+
if (saved === undefined || saved.token !== undefined) continue
|
|
141
|
+
if (typeof hidden !== 'string' || hidden.length === 0) continue
|
|
142
|
+
this.state.regions[region] = {
|
|
143
|
+
...saved,
|
|
144
|
+
token: hidden,
|
|
145
|
+
signedIn: true,
|
|
146
|
+
account: withoutCredential(saved.account),
|
|
147
|
+
}
|
|
148
|
+
recovered = true
|
|
149
|
+
}
|
|
150
|
+
if (recovered) this.persist()
|
|
151
|
+
}
|
|
152
|
+
|
|
103
153
|
region(region: CodeBuddyRegion): CodeBuddySavedRegion | undefined {
|
|
104
154
|
return this.ensureLoaded().regions[region]
|
|
105
155
|
}
|
|
@@ -138,6 +188,9 @@ export class CodeBuddyStateStore {
|
|
|
138
188
|
if (value === undefined) delete next[key as keyof CodeBuddySavedRegion]
|
|
139
189
|
else (next as Record<string, unknown>)[key] = value
|
|
140
190
|
}
|
|
191
|
+
// The account object arrives from the CLI carrying a bearer token of its
|
|
192
|
+
// own; keep exactly one credential per region on disk.
|
|
193
|
+
if (next.account !== undefined) next.account = withoutCredential(next.account)
|
|
141
194
|
state.regions[region] = next
|
|
142
195
|
this.persist()
|
|
143
196
|
}
|
package/src/codebuddy.ts
CHANGED
|
@@ -21,7 +21,7 @@ import type {
|
|
|
21
21
|
CanUseTool, Message, RawLanguageModel, UserMessage,
|
|
22
22
|
} from '@tencent-ai/agent-sdk'
|
|
23
23
|
import type { CodeBuddyRegion } from './brand.ts'
|
|
24
|
-
import { CodeBuddyStateStore, codeBuddyCliDir } from './codebuddy-state.ts'
|
|
24
|
+
import { CodeBuddyStateStore, codeBuddyCliDir, withoutCredential } from './codebuddy-state.ts'
|
|
25
25
|
import type { CodeBuddySavedUser } from './codebuddy-state.ts'
|
|
26
26
|
import { jsonSchemaToShape } from './jsonschema.ts'
|
|
27
27
|
import type { Live } from './live.ts'
|
|
@@ -218,12 +218,26 @@ function tokenFor(region: CodeBuddyRegion): string | undefined {
|
|
|
218
218
|
* pins the environment, so a fresh process always reads the right catalog.
|
|
219
219
|
*/
|
|
220
220
|
function cliEnv(region: CodeBuddyRegion): { env: Record<string, string> } {
|
|
221
|
-
const env
|
|
221
|
+
const env = cliDirEnv(region)
|
|
222
222
|
const token = tokenFor(region)
|
|
223
223
|
if (token !== undefined) env.CODEBUDDY_AUTH_TOKEN = token
|
|
224
224
|
return { env }
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
+
/**
|
|
228
|
+
* The region's own CLI directory, with no credential attached.
|
|
229
|
+
*
|
|
230
|
+
* The login, logout and silent-token calls need this and nothing else: they
|
|
231
|
+
* must not carry a bearer token (login starts fresh, logout clears), but they
|
|
232
|
+
* absolutely must be scoped to one region. Without it they acted on the CLI's
|
|
233
|
+
* default `~/.codebuddy`, which both regions share — so signing in for one
|
|
234
|
+
* region overwrote the other's login, and `logout` for the region the page was
|
|
235
|
+
* showing left the credential of the other one standing.
|
|
236
|
+
*/
|
|
237
|
+
function cliDirEnv(region: CodeBuddyRegion): Record<string, string> {
|
|
238
|
+
return { CODEBUDDY_CONFIG_DIR: codeBuddyCliDir(region) }
|
|
239
|
+
}
|
|
240
|
+
|
|
227
241
|
/** Remember a bearer token in memory and on disk. */
|
|
228
242
|
function rememberBearerToken(region: CodeBuddyRegion, token: string, user?: CodeBuddySavedUser): void {
|
|
229
243
|
if (token.length === 0) return
|
|
@@ -448,12 +462,15 @@ export class CodeBuddyAccountReader {
|
|
|
448
462
|
|
|
449
463
|
private async fetch(region: CodeBuddyRegion): Promise<CodeBuddyUsageSnapshot> {
|
|
450
464
|
const at = Date.now()
|
|
451
|
-
//
|
|
452
|
-
//
|
|
453
|
-
//
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
465
|
+
// Ask the CLI even when this region holds no credential of ours.
|
|
466
|
+
//
|
|
467
|
+
// Refusing here used to be the guard against adopting the *other* region's
|
|
468
|
+
// login: the CLI's default directory is shared, so a probe could answer with
|
|
469
|
+
// whichever account logged in last. Every call now runs inside this region's
|
|
470
|
+
// own directory, so the answer can only be this region's — and the refusal
|
|
471
|
+
// had become pure damage: any accident that dropped the local copy of a
|
|
472
|
+
// working token left the card saying "not signed in" for an account the CLI
|
|
473
|
+
// still had, with no path back short of a fresh browser login.
|
|
457
474
|
try {
|
|
458
475
|
const account = await readAccount(region, readLive(this.timeoutMs, CODEBUDDY_FETCH_TIMEOUT_MS))
|
|
459
476
|
// An unauthenticated CLI answers with an empty object or an anonymous
|
|
@@ -470,7 +487,7 @@ export class CodeBuddyAccountReader {
|
|
|
470
487
|
return { at, account, quota }
|
|
471
488
|
} catch (quotaError) {
|
|
472
489
|
const message = quotaError instanceof Error ? quotaError.message : String(quotaError)
|
|
473
|
-
if (
|
|
490
|
+
if (isCredentialRejected(quotaError)) forgetToken(region)
|
|
474
491
|
else codeBuddyState.patch(region, { quota: undefined, quotaError: message })
|
|
475
492
|
return { at, account, quotaError: message }
|
|
476
493
|
}
|
|
@@ -500,7 +517,9 @@ async function readAccount(region: CodeBuddyRegion, timeoutMs: number): Promise<
|
|
|
500
517
|
})
|
|
501
518
|
try {
|
|
502
519
|
await withTimeout(q.connect(), timeoutMs)
|
|
503
|
-
|
|
520
|
+
// The CLI's answer carries a bearer token beside the identity; it goes on
|
|
521
|
+
// to the settings page through `/usage`, so the credential comes off here.
|
|
522
|
+
return withoutCredential(await withTimeout(q.accountInfo(), timeoutMs)) ?? {}
|
|
504
523
|
} finally {
|
|
505
524
|
await q.return().catch(() => undefined)
|
|
506
525
|
}
|
|
@@ -609,9 +628,14 @@ function formatLocalTime(date: Date): string {
|
|
|
609
628
|
/**
|
|
610
629
|
* A bearer token for the region, from a recent login or a silent SDK read.
|
|
611
630
|
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
614
|
-
*
|
|
631
|
+
* The silent read is the documented behaviour of `authenticate` — return the
|
|
632
|
+
* CLI's existing token instead of opening a browser when the region is signed
|
|
633
|
+
* in — but it did not work as measured: on a machine with a live CodeBuddy
|
|
634
|
+
* login it timed out for both regions, each in the shared default directory and
|
|
635
|
+
* in both per-region directories. So treat it as best effort, and never as a
|
|
636
|
+
* place to recover from: the copy this plugin saved is the only credential a
|
|
637
|
+
* session can count on, which is why dropping it used to read as "not signed
|
|
638
|
+
* in" even while the account still worked.
|
|
615
639
|
*/
|
|
616
640
|
async function readBearerToken(region: CodeBuddyRegion): Promise<string> {
|
|
617
641
|
const cached = tokenFor(region)
|
|
@@ -619,6 +643,10 @@ async function readBearerToken(region: CodeBuddyRegion): Promise<string> {
|
|
|
619
643
|
const sdk = await loadCodeBuddySdk()
|
|
620
644
|
const result = await withTimeout(sdk.unstable_v2_authenticate({
|
|
621
645
|
environment: codeBuddyEnvironment(region),
|
|
646
|
+
// Scoped to this region's directory. Left out, the CLI answered from its
|
|
647
|
+
// shared default directory — whichever region had signed in last — and
|
|
648
|
+
// `rememberBearerToken` below then recorded *that* account as this region's.
|
|
649
|
+
env: cliDirEnv(region),
|
|
622
650
|
timeout: 3000,
|
|
623
651
|
onAuthUrl: () => {
|
|
624
652
|
// A quota read must never pop a browser; the page owns login.
|
|
@@ -682,7 +710,7 @@ export async function fetchCodeBuddyQuota(
|
|
|
682
710
|
const text = await response.text()
|
|
683
711
|
if (!response.ok) {
|
|
684
712
|
if (response.status === 401 || response.status === 403) {
|
|
685
|
-
throw new
|
|
713
|
+
throw new CredentialRejectedError('CodeBuddy 登录已过期,请重新登录')
|
|
686
714
|
}
|
|
687
715
|
const detail = text.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 160)
|
|
688
716
|
throw new Error(`CodeBuddy 额度接口返回 HTTP ${response.status}${detail === '' ? '' : `:${detail}`}`)
|
|
@@ -696,9 +724,33 @@ export async function fetchCodeBuddyQuota(
|
|
|
696
724
|
return parseCodeBuddyQuota(payload)
|
|
697
725
|
}
|
|
698
726
|
|
|
699
|
-
/**
|
|
700
|
-
|
|
701
|
-
|
|
727
|
+
/**
|
|
728
|
+
* The credential this region holds was rejected by the endpoint.
|
|
729
|
+
*
|
|
730
|
+
* Thrown where the rejection is *known* — an explicit HTTP 401/403 — rather
|
|
731
|
+
* than inferred from the error text. Inferring it was the bug: the loose
|
|
732
|
+
* matcher below used to treat any failure whose message happened to contain
|
|
733
|
+
* "token", "401" inside a number, or an HTML body mentioning a token, as an
|
|
734
|
+
* expired login. It then dropped a working credential, and because a missing
|
|
735
|
+
* local token reads as "signed out", the page showed an account as not signed
|
|
736
|
+
* in while the CLI still had a usable login.
|
|
737
|
+
*/
|
|
738
|
+
class CredentialRejectedError extends Error {}
|
|
739
|
+
|
|
740
|
+
/**
|
|
741
|
+
* Whether a quota failure genuinely means the credential no longer works.
|
|
742
|
+
*
|
|
743
|
+
* The typed error carries the CLI's own expiry sentence, which is also matched
|
|
744
|
+
* here because the message is ours, fixed, and not derived from server text.
|
|
745
|
+
* Everything else — a 5xx, a bad payload, a timeout, an unreachable endpoint —
|
|
746
|
+
* leaves the credential alone.
|
|
747
|
+
*
|
|
748
|
+
* @param error - whatever quota reading threw.
|
|
749
|
+
*/
|
|
750
|
+
function isCredentialRejected(error: unknown): boolean {
|
|
751
|
+
if (error instanceof CredentialRejectedError) return true
|
|
752
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
753
|
+
return message === 'CodeBuddy 登录已过期,请重新登录'
|
|
702
754
|
}
|
|
703
755
|
|
|
704
756
|
/** Read quota, retrying once with a fresh token after an auth failure. */
|
|
@@ -709,8 +761,7 @@ async function readQuotaWithToken(region: CodeBuddyRegion): Promise<CodeBuddyQuo
|
|
|
709
761
|
try {
|
|
710
762
|
return await fetchCodeBuddyQuota(region, token)
|
|
711
763
|
} catch (error) {
|
|
712
|
-
|
|
713
|
-
if (!isExpiredToken(detail)) throw error
|
|
764
|
+
if (!isCredentialRejected(error)) throw error
|
|
714
765
|
forgetToken(region)
|
|
715
766
|
const renewed = await readBearerToken(region).catch((renewError: unknown) => {
|
|
716
767
|
throw new Error(authErrorText(region, renewError))
|
|
@@ -768,7 +819,7 @@ export class CodeBuddyAuth {
|
|
|
768
819
|
try {
|
|
769
820
|
const sdk = await loadCodeBuddySdk()
|
|
770
821
|
await withTimeout(
|
|
771
|
-
sdk.unstable_v2_logout({ environment: codeBuddyEnvironment(region) }),
|
|
822
|
+
sdk.unstable_v2_logout({ environment: codeBuddyEnvironment(region), env: cliDirEnv(region) }),
|
|
772
823
|
CODEBUDDY_FETCH_TIMEOUT_MS,
|
|
773
824
|
)
|
|
774
825
|
} catch (error) {
|
|
@@ -796,14 +847,18 @@ export class CodeBuddyAuth {
|
|
|
796
847
|
// it returns that token without ever calling `onAuthUrl`, so the click
|
|
797
848
|
// looks dead and an expired credential survives forever. A login click
|
|
798
849
|
// always means "open the browser", so drop the stored credential first.
|
|
850
|
+
// Both calls are scoped to this region's directory: the CLI's default
|
|
851
|
+
// directory is shared, so an unscoped logout here signed the *other*
|
|
852
|
+
// region out as a side effect of logging this one in.
|
|
799
853
|
await withTimeout(
|
|
800
|
-
sdk.unstable_v2_logout({ environment: codeBuddyEnvironment(region) }),
|
|
854
|
+
sdk.unstable_v2_logout({ environment: codeBuddyEnvironment(region), env: cliDirEnv(region) }),
|
|
801
855
|
LOGOUT_TIMEOUT_MS,
|
|
802
856
|
).catch(() => undefined)
|
|
803
857
|
if (!this.current(run)) return
|
|
804
858
|
bearerTokens.delete(region)
|
|
805
859
|
const result = await sdk.unstable_v2_authenticate({
|
|
806
860
|
environment: codeBuddyEnvironment(region),
|
|
861
|
+
env: cliDirEnv(region),
|
|
807
862
|
timeout: LOGIN_TIMEOUT_MS,
|
|
808
863
|
onAuthUrl: (state) => {
|
|
809
864
|
// The bundled CLI opens the browser itself as part of the login
|
|
@@ -976,12 +1031,14 @@ export class CodeBuddySession {
|
|
|
976
1031
|
* compacts (and the gateway rejects) anything past 276K — while the host,
|
|
977
1032
|
* told 1M, keeps showing and feeding a larger history. Replaying that history
|
|
978
1033
|
* then collapses to a summary. Applying the same number the host divides by
|
|
979
|
-
* keeps the two in step;
|
|
980
|
-
*
|
|
1034
|
+
* keeps the two in step; the adapter therefore sends a window for every model
|
|
1035
|
+
* it knows about, not only the ones that advertise a choice — the mainland
|
|
1036
|
+
* catalog declares no lengths anywhere and its physical `maxInputTokens` is a
|
|
1037
|
+
* provider number the CLI is free to ignore when left to itself.
|
|
981
1038
|
*
|
|
982
|
-
*
|
|
983
|
-
*
|
|
984
|
-
*
|
|
1039
|
+
* A rejected value keeps the CLI on its own default, which is the one case
|
|
1040
|
+
* where the two denominators can still disagree; it is reported nowhere, so
|
|
1041
|
+
* the host's figure stays the visible contract.
|
|
985
1042
|
*/
|
|
986
1043
|
private async applyContextWindow(session: SessionImpl): Promise<void> {
|
|
987
1044
|
const window = this.contextWindow
|
|
@@ -992,7 +1049,8 @@ export class CodeBuddySession {
|
|
|
992
1049
|
try {
|
|
993
1050
|
await session.setConfig({ contextWindow: window })
|
|
994
1051
|
} catch {
|
|
995
|
-
//
|
|
1052
|
+
// Nothing selectable here, or the value was refused: the model's own
|
|
1053
|
+
// default stands rather than failing the turn over a budget hint.
|
|
996
1054
|
}
|
|
997
1055
|
}
|
|
998
1056
|
|
package/src/models.ts
CHANGED
|
@@ -17,10 +17,11 @@ import type { QoderBrand } from './brand.ts'
|
|
|
17
17
|
import { brandProfile } from './brand.ts'
|
|
18
18
|
import type { QoderCatalogModel } from './catalog.ts'
|
|
19
19
|
import { QODER_MODELS } from './catalog.ts'
|
|
20
|
-
import { cliConflictMessage, cliFailureText, foreignCliOverride, qoderCliOptions } from './cli.ts'
|
|
20
|
+
import { cliConflictMessage, cliFailureText, foreignCliOverride, qoderCliOptions, resolveQoderCli } from './cli.ts'
|
|
21
21
|
import type { Live } from './live.ts'
|
|
22
22
|
import { readLive } from './live.ts'
|
|
23
|
-
import {
|
|
23
|
+
import type { QoderSdkModule } from './sdk.ts'
|
|
24
|
+
import { withSdk } from './sdk.ts'
|
|
24
25
|
|
|
25
26
|
/** How long a fetched catalog stays fresh by default. */
|
|
26
27
|
export const DEFAULT_MODEL_CACHE_TTL_MS = 5 * 60_000
|
|
@@ -152,7 +153,15 @@ export class QoderModelCatalog {
|
|
|
152
153
|
private async fetch(): Promise<readonly ModelInfo[]> {
|
|
153
154
|
const conflict = foreignCliOverride(this.brand)
|
|
154
155
|
if (conflict !== undefined) throw new Error(cliConflictMessage(this.brand))
|
|
155
|
-
|
|
156
|
+
// Without a CLI of our own there is no configuration to initialize the
|
|
157
|
+
// SDK's daemon with. Asking anyway pinned it to the SDK's defaults, and the
|
|
158
|
+
// first query after the CLI was installed then conflicted with it for the
|
|
159
|
+
// rest of the process's life (see `sdk.ts`).
|
|
160
|
+
if (resolveQoderCli(this.brand) === undefined) throw new Error(cliConflictMessage(this.brand))
|
|
161
|
+
return withSdk(this.brand, sdk => this.read(sdk))
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private async read(sdk: QoderSdkModule): Promise<readonly ModelInfo[]> {
|
|
156
165
|
const q = sdk.query({
|
|
157
166
|
prompt: inertInput(),
|
|
158
167
|
options: {
|