dsh-agent-sdk-bridge 1.3.4 → 1.3.5
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 +95 -122
- package/lib/types/sdk.d.ts +78 -52
- package/package.json +1 -1
- package/src/models.ts +13 -4
- package/src/sdk.ts +144 -104
- package/src/session.ts +6 -6
- package/src/usage.ts +19 -4
package/lib/index.js
CHANGED
|
@@ -4,7 +4,6 @@ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSyn
|
|
|
4
4
|
import { homedir, tmpdir } from "node:os";
|
|
5
5
|
import * as path from "node:path";
|
|
6
6
|
import { delimiter, dirname, join } from "node:path";
|
|
7
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
8
7
|
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
9
8
|
import { createSdkMcpServer } from "@qodercn-ai/qodercn-agent-sdk";
|
|
10
9
|
import { z } from "zod";
|
|
@@ -13,6 +12,7 @@ import { createServer } from "node:http";
|
|
|
13
12
|
import { Readable, Writable } from "node:stream";
|
|
14
13
|
import { ClientSideConnection, PROTOCOL_VERSION, ndJsonStream } from "@agentclientprotocol/sdk";
|
|
15
14
|
import { TextDecoder } from "node:util";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
16
|
import { isVolatile } from "@deepseek-ai/cosmokit";
|
|
17
17
|
import z$1 from "@deepseek-ai/schemastery";
|
|
18
18
|
//#region src/brand.ts
|
|
@@ -640,61 +640,18 @@ function isWorkerRuntime(entry) {
|
|
|
640
640
|
}
|
|
641
641
|
//#endregion
|
|
642
642
|
//#region src/sdk.ts
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
* of the plugin imports this module instead of a package name. The CN SDK is a
|
|
648
|
-
* real dependency and is loaded eagerly; the international one is optional at
|
|
649
|
-
* runtime — a deployment that only has a CN account may not have it installed —
|
|
650
|
-
* so it is imported on demand and its absence is reported to the settings page
|
|
651
|
-
* as a plain error instead of failing the plugin's mount.
|
|
652
|
-
*
|
|
653
|
-
* CodeBuddy has its own, unrelated API and is loaded dynamically for the same
|
|
654
|
-
* reason: an account may never use it, and its package carries a large CLI.
|
|
655
|
-
*
|
|
656
|
-
* ## Why modules are reloaded
|
|
657
|
-
*
|
|
658
|
-
* Every Qoder SDK routes all queries through one **process-wide daemon** that
|
|
659
|
-
* is initialized with the first query's launch configuration (CLI path,
|
|
660
|
-
* interpreter, environment, proxy) and then refuses any different one for the
|
|
661
|
-
* life of the process:
|
|
662
|
-
*
|
|
663
|
-
* DaemonConfigurationConflictError:
|
|
664
|
-
* Global daemon is already initialized with different options
|
|
665
|
-
*
|
|
666
|
-
* The host is long-lived, so its configuration is not: a CLI can be
|
|
667
|
-
* reinstalled, an edition can appear, an installer can change the machine
|
|
668
|
-
* environment. When that happens the ready daemon no longer matches and every
|
|
669
|
-
* later query fails until the process restarts — a state the settings page
|
|
670
|
-
* cannot recover from on its own.
|
|
671
|
-
*
|
|
672
|
-
* So the module tracks the configuration signature it loaded under and takes a
|
|
673
|
-
* fresh instance whenever the signature changes; {@link resetSdk} does the same
|
|
674
|
-
* after a conflict is observed. A new instance has an empty daemon registry, so
|
|
675
|
-
* it initializes against the current configuration instead of arguing with the
|
|
676
|
-
* old one.
|
|
677
|
-
*
|
|
678
|
-
* @module dsh-agent-sdk-bridge/sdk
|
|
679
|
-
*/
|
|
680
|
-
const generations = /* @__PURE__ */ new Map();
|
|
681
|
-
/** Brands whose next load must take a fresh instance even if the signature matches. */
|
|
682
|
-
const forced = /* @__PURE__ */ new Set();
|
|
683
|
-
let counter = 0;
|
|
643
|
+
const pinned = /* @__PURE__ */ new Map();
|
|
644
|
+
const modules = /* @__PURE__ */ new Map();
|
|
645
|
+
/** Resets in flight, so a query cannot start against a daemon being stopped. */
|
|
646
|
+
const resets = /* @__PURE__ */ new Map();
|
|
684
647
|
let codeBuddySdk;
|
|
685
|
-
const require = createRequire(import.meta.url);
|
|
686
648
|
/**
|
|
687
|
-
*
|
|
688
|
-
*
|
|
689
|
-
* Mirrors the fields the SDK's own daemon compares before reusing itself. The
|
|
690
|
-
* environment is included because the SDK hands it to the CLI verbatim, and a
|
|
691
|
-
* difference there is enough to make the daemon reject a query.
|
|
649
|
+
* Describe a launch configuration as a comparable string.
|
|
692
650
|
*
|
|
693
|
-
*
|
|
694
|
-
*
|
|
651
|
+
* The fields are the ones the SDK's daemon compares before reusing itself. The
|
|
652
|
+
* environment is included because the SDK hands it to the CLI verbatim.
|
|
695
653
|
*/
|
|
696
|
-
function
|
|
697
|
-
const options = qoderCliOptions(brand);
|
|
654
|
+
function signatureOf(options) {
|
|
698
655
|
const env = Object.entries(options.env ?? {}).filter(([, value]) => value !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
699
656
|
return JSON.stringify({
|
|
700
657
|
path: options.pathToQoderCLIExecutable ?? null,
|
|
@@ -703,87 +660,98 @@ function configurationSignature(brand) {
|
|
|
703
660
|
});
|
|
704
661
|
}
|
|
705
662
|
/**
|
|
706
|
-
*
|
|
663
|
+
* This edition's launch configuration, computed once per process.
|
|
664
|
+
*
|
|
665
|
+
* Frozen on purpose: the daemon accepts exactly one configuration, so every
|
|
666
|
+
* query has to send the same one. Recomputing it per call let concurrent
|
|
667
|
+
* reads disagree — a machine-wide `QODERCLI_PATH`, a CLI that finished
|
|
668
|
+
* updating between two calls, a PATH entry that appeared — and the daemon
|
|
669
|
+
* rejected whichever arrived second, permanently.
|
|
707
670
|
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
710
|
-
* the module loader as usual.
|
|
671
|
+
* {@link resetDaemon} drops the pin, so a real change (install, sign-in, CLI
|
|
672
|
+
* update) is picked up by the next query instead of being frozen out.
|
|
711
673
|
*
|
|
712
|
-
* @param brand - which edition to
|
|
713
|
-
* @param fresh - ignore the already-loaded instance and take a new one.
|
|
674
|
+
* @param brand - which edition's configuration to return.
|
|
714
675
|
*/
|
|
715
|
-
|
|
676
|
+
function cliOptions(brand) {
|
|
677
|
+
const held = pinned.get(brand);
|
|
678
|
+
if (held !== void 0) return held.options;
|
|
679
|
+
const options = qoderCliOptions(brand);
|
|
680
|
+
pinned.set(brand, {
|
|
681
|
+
options,
|
|
682
|
+
signature: signatureOf(options)
|
|
683
|
+
});
|
|
684
|
+
return options;
|
|
685
|
+
}
|
|
686
|
+
/** Import one edition's SDK; a missing optional package is reported in context. */
|
|
687
|
+
async function importSdk(brand) {
|
|
716
688
|
const specifier = brandProfile(brand).sdkPackage;
|
|
717
689
|
try {
|
|
718
|
-
if (!fresh) return await import(
|
|
719
|
-
/* @vite-ignore */
|
|
720
|
-
specifier
|
|
721
|
-
);
|
|
722
|
-
const entry = pathToFileURL(require.resolve(specifier)).href;
|
|
723
|
-
counter += 1;
|
|
724
690
|
return await import(
|
|
725
691
|
/* @vite-ignore */
|
|
726
|
-
|
|
692
|
+
specifier
|
|
727
693
|
);
|
|
728
694
|
} catch (error) {
|
|
729
|
-
|
|
730
|
-
forced.delete(brand);
|
|
695
|
+
modules.delete(brand);
|
|
731
696
|
if (brand === "cn") throw error;
|
|
732
697
|
const detail = error instanceof Error ? error.message : String(error);
|
|
733
698
|
throw new Error(`未安装国际版 SDK(${specifier}):${detail}`);
|
|
734
699
|
}
|
|
735
700
|
}
|
|
736
701
|
/**
|
|
737
|
-
* Load the SDK for one Qoder edition
|
|
702
|
+
* Load the SDK for one Qoder edition.
|
|
703
|
+
*
|
|
704
|
+
* One module instance per edition, reused for the process's lifetime: the
|
|
705
|
+
* daemon it owns is reset explicitly rather than by re-importing the package.
|
|
738
706
|
*
|
|
739
707
|
* @param brand - which edition's SDK to load.
|
|
740
|
-
* @returns the SDK module.
|
|
741
708
|
*/
|
|
742
709
|
function loadSdk(brand) {
|
|
743
|
-
const
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
});
|
|
755
|
-
return module;
|
|
710
|
+
const resetting = resets.get(brand);
|
|
711
|
+
if (resetting !== void 0) return resetting.then(() => loadSdk(brand));
|
|
712
|
+
let held = modules.get(brand);
|
|
713
|
+
if (held === void 0) {
|
|
714
|
+
held = importSdk(brand);
|
|
715
|
+
modules.set(brand, held);
|
|
716
|
+
held.catch(() => {
|
|
717
|
+
if (modules.get(brand) === held) modules.delete(brand);
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
return held;
|
|
756
721
|
}
|
|
757
722
|
/**
|
|
758
|
-
*
|
|
723
|
+
* Stop this edition's daemon so the next query starts a clean one.
|
|
759
724
|
*
|
|
760
|
-
*
|
|
761
|
-
|
|
762
|
-
function resetSdk(brand) {
|
|
763
|
-
generations.delete(brand);
|
|
764
|
-
forced.add(brand);
|
|
765
|
-
}
|
|
766
|
-
/**
|
|
767
|
-
* Drop the loaded generation and stop the daemon it started.
|
|
725
|
+
* Three situations need this, and they are not distinguishable from the
|
|
726
|
+
* outside — all three look like "the account is gone" until something resets:
|
|
768
727
|
*
|
|
769
|
-
*
|
|
770
|
-
*
|
|
771
|
-
*
|
|
772
|
-
*
|
|
773
|
-
* credentials that are on disk now.
|
|
728
|
+
* - the launch configuration changed since the daemon started;
|
|
729
|
+
* - the CLI was updated underneath it;
|
|
730
|
+
* - the credentials on disk changed (sign-in, sign-out, refresh) while the
|
|
731
|
+
* daemon still holds the ones it loaded at start-up.
|
|
774
732
|
*
|
|
775
|
-
*
|
|
776
|
-
*
|
|
777
|
-
*
|
|
733
|
+
* `shutdownDaemon()` is the SDK's own way to clear the process-wide slot, so
|
|
734
|
+
* the next `query()` initializes against the current machine instead of
|
|
735
|
+
* arguing with a stale daemon until DSH restarts.
|
|
778
736
|
*
|
|
779
737
|
* @param brand - which edition's daemon to replace.
|
|
780
738
|
*/
|
|
739
|
+
async function resetDaemon(brand) {
|
|
740
|
+
pinned.delete(brand);
|
|
741
|
+
const held = modules.get(brand);
|
|
742
|
+
if (held === void 0) return;
|
|
743
|
+
const sdk = await held.catch(() => void 0);
|
|
744
|
+
if (sdk === void 0) {
|
|
745
|
+
modules.delete(brand);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
try {
|
|
749
|
+
await sdk.shutdownDaemon();
|
|
750
|
+
} catch {}
|
|
751
|
+
}
|
|
752
|
+
/** Alias kept for the logout path, whose wording is about discarding state. */
|
|
781
753
|
function discardSdk(brand) {
|
|
782
|
-
|
|
783
|
-
generations.delete(brand);
|
|
784
|
-
forced.add(brand);
|
|
785
|
-
if (generation === void 0) return;
|
|
786
|
-
generation.module.then((sdk) => sdk.shutdownDaemon()).catch(() => void 0);
|
|
754
|
+
resetDaemon(brand);
|
|
787
755
|
}
|
|
788
756
|
/**
|
|
789
757
|
* Whether an error is the SDK's daemon refusing a different configuration.
|
|
@@ -795,13 +763,10 @@ function isDaemonConflict(error) {
|
|
|
795
763
|
return text.includes("DaemonConfigurationConflictError") || text.includes("Global daemon is already initialized with different options");
|
|
796
764
|
}
|
|
797
765
|
/**
|
|
798
|
-
* Run one idempotent SDK operation,
|
|
766
|
+
* Run one idempotent SDK operation, repairing a stale daemon once.
|
|
799
767
|
*
|
|
800
|
-
*
|
|
801
|
-
*
|
|
802
|
-
* current one, so the retry succeeds without a restart. Only use this for
|
|
803
|
-
* operations that can be repeated without side effects (reads), never for a
|
|
804
|
-
* turn that may already have emitted output.
|
|
768
|
+
* Only use this for operations that can be repeated without side effects
|
|
769
|
+
* (reads), never for a turn that may already have emitted output.
|
|
805
770
|
*
|
|
806
771
|
* @param brand - which edition to query.
|
|
807
772
|
* @param run - the operation, given the module to use.
|
|
@@ -811,7 +776,7 @@ async function withSdk(brand, run) {
|
|
|
811
776
|
return await run(await loadSdk(brand));
|
|
812
777
|
} catch (error) {
|
|
813
778
|
if (!isDaemonConflict(error)) throw error;
|
|
814
|
-
|
|
779
|
+
await resetDaemon(brand);
|
|
815
780
|
return await run(await loadSdk(brand));
|
|
816
781
|
}
|
|
817
782
|
}
|
|
@@ -833,7 +798,7 @@ async function probeDaemon(brand) {
|
|
|
833
798
|
await new Promise(() => {});
|
|
834
799
|
})(),
|
|
835
800
|
options: {
|
|
836
|
-
...
|
|
801
|
+
...cliOptions(brand),
|
|
837
802
|
auth: sdk.qodercliAuth(),
|
|
838
803
|
tools: [],
|
|
839
804
|
allowedTools: [],
|
|
@@ -1030,13 +995,17 @@ var QoderModelCatalog = class {
|
|
|
1030
995
|
async fetch() {
|
|
1031
996
|
if (foreignCliOverride(this.brand) !== void 0) throw new Error(cliConflictMessage(this.brand));
|
|
1032
997
|
if (resolveQoderCli(this.brand) === void 0) throw new Error(cliConflictMessage(this.brand));
|
|
1033
|
-
|
|
998
|
+
const first = await withSdk(this.brand, (sdk) => this.read(sdk));
|
|
999
|
+
if (first.length > 0) return first;
|
|
1000
|
+
await resetDaemon(this.brand);
|
|
1001
|
+
const retried = await withSdk(this.brand, (sdk) => this.read(sdk));
|
|
1002
|
+
return retried.length > 0 ? retried : first;
|
|
1034
1003
|
}
|
|
1035
1004
|
async read(sdk) {
|
|
1036
1005
|
const q = sdk.query({
|
|
1037
1006
|
prompt: inertInput$2(),
|
|
1038
1007
|
options: {
|
|
1039
|
-
...
|
|
1008
|
+
...cliOptions(this.brand),
|
|
1040
1009
|
auth: sdk.qodercliAuth(),
|
|
1041
1010
|
tools: [],
|
|
1042
1011
|
allowedTools: [],
|
|
@@ -2516,14 +2485,14 @@ var QoderSession = class {
|
|
|
2516
2485
|
try {
|
|
2517
2486
|
await probeDaemon(this.brand);
|
|
2518
2487
|
} catch (error) {
|
|
2519
|
-
if (isDaemonConflict(error))
|
|
2488
|
+
if (isDaemonConflict(error)) await resetDaemon(this.brand);
|
|
2520
2489
|
}
|
|
2521
2490
|
const sdk = await loadSdk(this.brand);
|
|
2522
2491
|
this.registry.prepare();
|
|
2523
2492
|
const q = sdk.query({
|
|
2524
2493
|
prompt: this.channel,
|
|
2525
2494
|
options: {
|
|
2526
|
-
...
|
|
2495
|
+
...cliOptions(this.brand),
|
|
2527
2496
|
auth: sdk.qodercliAuth(),
|
|
2528
2497
|
tools: [],
|
|
2529
2498
|
allowedTools: [],
|
|
@@ -2874,7 +2843,7 @@ var QoderSession = class {
|
|
|
2874
2843
|
});
|
|
2875
2844
|
} catch (error) {
|
|
2876
2845
|
if (isDaemonConflict(error)) {
|
|
2877
|
-
|
|
2846
|
+
await resetDaemon(this.brand);
|
|
2878
2847
|
this.q = null;
|
|
2879
2848
|
q.close().catch(() => void 0);
|
|
2880
2849
|
}
|
|
@@ -3202,7 +3171,7 @@ var QoderSessionManager = class {
|
|
|
3202
3171
|
const q = sdk.query({
|
|
3203
3172
|
prompt,
|
|
3204
3173
|
options: {
|
|
3205
|
-
...
|
|
3174
|
+
...cliOptions(brand),
|
|
3206
3175
|
auth: sdk.qodercliAuth(),
|
|
3207
3176
|
tools: [],
|
|
3208
3177
|
allowedTools: [],
|
|
@@ -3237,7 +3206,7 @@ var QoderSessionManager = class {
|
|
|
3237
3206
|
}
|
|
3238
3207
|
} catch (error) {
|
|
3239
3208
|
if (signal?.aborted !== true) {
|
|
3240
|
-
if (isDaemonConflict(error))
|
|
3209
|
+
if (isDaemonConflict(error)) await resetDaemon(brand);
|
|
3241
3210
|
throw error;
|
|
3242
3211
|
}
|
|
3243
3212
|
}
|
|
@@ -3400,7 +3369,11 @@ var QoderUsage = class {
|
|
|
3400
3369
|
cliMissing: true,
|
|
3401
3370
|
error: cliConflictMessage(this.brand)
|
|
3402
3371
|
};
|
|
3403
|
-
|
|
3372
|
+
const snapshot = await withSdk(this.brand, (sdk) => this.readSnapshot(sdk, at));
|
|
3373
|
+
if (snapshot.usage !== void 0 || snapshot.cliMissing === true) return snapshot;
|
|
3374
|
+
await resetDaemon(this.brand);
|
|
3375
|
+
const retried = await withSdk(this.brand, (sdk) => this.readSnapshot(sdk, at));
|
|
3376
|
+
return retried.usage !== void 0 ? retried : snapshot;
|
|
3404
3377
|
} catch (error) {
|
|
3405
3378
|
const detail = error instanceof Error ? error.message : String(error);
|
|
3406
3379
|
const command = brandProfile(this.brand).cliCommands[0];
|
|
@@ -3415,7 +3388,7 @@ var QoderUsage = class {
|
|
|
3415
3388
|
const q = sdk.query({
|
|
3416
3389
|
prompt: inertInput$1(),
|
|
3417
3390
|
options: {
|
|
3418
|
-
...
|
|
3391
|
+
...cliOptions(this.brand),
|
|
3419
3392
|
auth: sdk.qodercliAuth(),
|
|
3420
3393
|
tools: [],
|
|
3421
3394
|
allowedTools: [],
|
package/lib/types/sdk.d.ts
CHANGED
|
@@ -1,42 +1,48 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The SDK package behind each brand.
|
|
2
|
+
* The SDK package behind each brand, and the one daemon it keeps alive.
|
|
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
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* real dependency; the international one is optional at runtime — a deployment
|
|
7
|
+
* that only has a CN account may not have it installed — so it is imported on
|
|
8
|
+
* demand and its absence is reported to the settings page as a plain error
|
|
9
|
+
* instead of failing the plugin's mount.
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
* reason: an account may never use it, and its package carries a large CLI.
|
|
11
|
+
* ## The daemon, and why it has to be reset
|
|
13
12
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* interpreter, environment, proxy) and then refuses any different one for the
|
|
19
|
-
* life of the process:
|
|
13
|
+
* Every Qoder SDK routes all queries through one **process-wide daemon**,
|
|
14
|
+
* initialized with the first query's launch configuration (CLI path,
|
|
15
|
+
* interpreter, environment, proxy, launch mode) and pinned to it for the life
|
|
16
|
+
* of the process:
|
|
20
17
|
*
|
|
21
18
|
* DaemonConfigurationConflictError:
|
|
22
19
|
* Global daemon is already initialized with different options
|
|
23
20
|
*
|
|
24
|
-
* The host is long-lived, so
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
21
|
+
* The host is long-lived, so the machine is not: the CLI updates itself,
|
|
22
|
+
* an edition appears, an installer changes the environment, a credential is
|
|
23
|
+
* refreshed. Any of those can leave the ready daemon holding a configuration
|
|
24
|
+
* (or a credential) that no longer applies, and from then on *every* query
|
|
25
|
+
* fails until DSH restarts — which is what the settings page reports as a
|
|
26
|
+
* signed-out account with no models and no quota.
|
|
27
|
+
*
|
|
28
|
+
* Two things keep that from happening:
|
|
29
|
+
*
|
|
30
|
+
* - **The launch configuration is frozen per process** ({@link cliOptions}).
|
|
31
|
+
* Every call site used to call `qoderCliOptions()` for itself, so two
|
|
32
|
+
* concurrent reads could describe the machine slightly differently and the
|
|
33
|
+
* second one was rejected. One frozen object per edition means every query
|
|
34
|
+
* in this process is identical by construction.
|
|
35
|
+
* - **A conflict is repaired, not reported** ({@link resetDaemon}). The SDK
|
|
36
|
+
* exports `shutdownDaemon()`, which stops the daemon and clears the slot it
|
|
37
|
+
* occupies, so the next query initializes a fresh one with the current
|
|
38
|
+
* configuration. That is what makes sign-in, a CLI update and a credential
|
|
39
|
+
* refresh take effect without restarting DSH.
|
|
35
40
|
*
|
|
36
41
|
* @module dsh-agent-sdk-bridge/sdk
|
|
37
42
|
*/
|
|
38
43
|
import type * as QoderSdk from '@qodercn-ai/qodercn-agent-sdk';
|
|
39
44
|
import type * as CodeBuddySdk from '@tencent-ai/agent-sdk';
|
|
45
|
+
import type { QoderCliOptions } from './cli.ts';
|
|
40
46
|
import type { CodeBuddyRegion, QoderBrand } from './brand.ts';
|
|
41
47
|
import { codeBuddyEnvironment } from './brand.ts';
|
|
42
48
|
/** The subset of the Qoder SDK surface this plugin uses, as one module type. */
|
|
@@ -44,44 +50,67 @@ export type QoderSdkModule = typeof QoderSdk;
|
|
|
44
50
|
/** The CodeBuddy SDK module, typed from its declarations. */
|
|
45
51
|
export type CodeBuddySdkModule = typeof CodeBuddySdk;
|
|
46
52
|
/**
|
|
47
|
-
*
|
|
53
|
+
* This edition's launch configuration, computed once per process.
|
|
54
|
+
*
|
|
55
|
+
* Frozen on purpose: the daemon accepts exactly one configuration, so every
|
|
56
|
+
* query has to send the same one. Recomputing it per call let concurrent
|
|
57
|
+
* reads disagree — a machine-wide `QODERCLI_PATH`, a CLI that finished
|
|
58
|
+
* updating between two calls, a PATH entry that appeared — and the daemon
|
|
59
|
+
* rejected whichever arrived second, permanently.
|
|
48
60
|
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* difference there is enough to make the daemon reject a query.
|
|
61
|
+
* {@link resetDaemon} drops the pin, so a real change (install, sign-in, CLI
|
|
62
|
+
* update) is picked up by the next query instead of being frozen out.
|
|
52
63
|
*
|
|
53
|
-
* @param brand - which edition's configuration to
|
|
54
|
-
|
|
64
|
+
* @param brand - which edition's configuration to return.
|
|
65
|
+
*/
|
|
66
|
+
export declare function cliOptions(brand: QoderBrand): QoderCliOptions;
|
|
67
|
+
/**
|
|
68
|
+
* The signature of this edition's frozen configuration.
|
|
69
|
+
*
|
|
70
|
+
* Kept for diagnostics and tests: two calls in one process always agree, which
|
|
71
|
+
* is the property the daemon requires.
|
|
72
|
+
*
|
|
73
|
+
* @param brand - which edition to describe.
|
|
55
74
|
*/
|
|
56
75
|
export declare function configurationSignature(brand: QoderBrand): string;
|
|
57
76
|
/**
|
|
58
|
-
* Load the SDK for one Qoder edition
|
|
77
|
+
* Load the SDK for one Qoder edition.
|
|
78
|
+
*
|
|
79
|
+
* One module instance per edition, reused for the process's lifetime: the
|
|
80
|
+
* daemon it owns is reset explicitly rather than by re-importing the package.
|
|
59
81
|
*
|
|
60
82
|
* @param brand - which edition's SDK to load.
|
|
61
|
-
* @returns the SDK module.
|
|
62
83
|
*/
|
|
63
84
|
export declare function loadSdk(brand: QoderBrand): Promise<QoderSdkModule>;
|
|
64
85
|
/**
|
|
65
|
-
*
|
|
86
|
+
* Stop this edition's daemon so the next query starts a clean one.
|
|
66
87
|
*
|
|
67
|
-
*
|
|
68
|
-
|
|
69
|
-
export declare function resetSdk(brand: QoderBrand): void;
|
|
70
|
-
/**
|
|
71
|
-
* Drop the loaded generation and stop the daemon it started.
|
|
88
|
+
* Three situations need this, and they are not distinguishable from the
|
|
89
|
+
* outside — all three look like "the account is gone" until something resets:
|
|
72
90
|
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* credentials that are on disk now.
|
|
91
|
+
* - the launch configuration changed since the daemon started;
|
|
92
|
+
* - the CLI was updated underneath it;
|
|
93
|
+
* - the credentials on disk changed (sign-in, sign-out, refresh) while the
|
|
94
|
+
* daemon still holds the ones it loaded at start-up.
|
|
78
95
|
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
96
|
+
* `shutdownDaemon()` is the SDK's own way to clear the process-wide slot, so
|
|
97
|
+
* the next `query()` initializes against the current machine instead of
|
|
98
|
+
* arguing with a stale daemon until DSH restarts.
|
|
82
99
|
*
|
|
83
100
|
* @param brand - which edition's daemon to replace.
|
|
84
101
|
*/
|
|
102
|
+
export declare function resetDaemon(brand: QoderBrand): Promise<void>;
|
|
103
|
+
/**
|
|
104
|
+
* Fire-and-forget {@link resetDaemon}.
|
|
105
|
+
*
|
|
106
|
+
* For callers that cannot await (a synchronous callback, a consumer loop that
|
|
107
|
+
* has already failed). The daemon is stopped in the background and the next
|
|
108
|
+
* query initializes a fresh one.
|
|
109
|
+
*
|
|
110
|
+
* @param brand - which edition to reset.
|
|
111
|
+
*/
|
|
112
|
+
export declare function resetSdk(brand: QoderBrand): void;
|
|
113
|
+
/** Alias kept for the logout path, whose wording is about discarding state. */
|
|
85
114
|
export declare function discardSdk(brand: QoderBrand): void;
|
|
86
115
|
/**
|
|
87
116
|
* Whether an error is the SDK's daemon refusing a different configuration.
|
|
@@ -90,13 +119,10 @@ export declare function discardSdk(brand: QoderBrand): void;
|
|
|
90
119
|
*/
|
|
91
120
|
export declare function isDaemonConflict(error: unknown): boolean;
|
|
92
121
|
/**
|
|
93
|
-
* Run one idempotent SDK operation,
|
|
122
|
+
* Run one idempotent SDK operation, repairing a stale daemon once.
|
|
94
123
|
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* current one, so the retry succeeds without a restart. Only use this for
|
|
98
|
-
* operations that can be repeated without side effects (reads), never for a
|
|
99
|
-
* turn that may already have emitted output.
|
|
124
|
+
* Only use this for operations that can be repeated without side effects
|
|
125
|
+
* (reads), never for a turn that may already have emitted output.
|
|
100
126
|
*
|
|
101
127
|
* @param brand - which edition to query.
|
|
102
128
|
* @param run - the operation, given the module to use.
|
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.3.
|
|
5
|
+
"version": "1.3.5",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "k1-h2",
|
package/src/models.ts
CHANGED
|
@@ -18,11 +18,11 @@ import type { QoderBrand } from './brand.ts'
|
|
|
18
18
|
import { brandProfile } from './brand.ts'
|
|
19
19
|
import type { QoderCatalogModel } from './catalog.ts'
|
|
20
20
|
import { QODER_MODELS } from './catalog.ts'
|
|
21
|
-
import { cliConflictMessage, cliFailureText, foreignCliOverride,
|
|
21
|
+
import { cliConflictMessage, cliFailureText, foreignCliOverride, resolveQoderCli } from './cli.ts'
|
|
22
22
|
import type { Live } from './live.ts'
|
|
23
23
|
import { readLive } from './live.ts'
|
|
24
24
|
import type { QoderSdkModule } from './sdk.ts'
|
|
25
|
-
import { withSdk } from './sdk.ts'
|
|
25
|
+
import { cliOptions, resetDaemon, withSdk } from './sdk.ts'
|
|
26
26
|
|
|
27
27
|
/** How long a fetched catalog stays fresh by default. */
|
|
28
28
|
export const DEFAULT_MODEL_CACHE_TTL_MS = 5 * 60_000
|
|
@@ -207,14 +207,23 @@ export class QoderModelCatalog {
|
|
|
207
207
|
// first query after the CLI was installed then conflicted with it for the
|
|
208
208
|
// rest of the process's life (see `sdk.ts`).
|
|
209
209
|
if (resolveQoderCli(this.brand) === undefined) throw new Error(cliConflictMessage(this.brand))
|
|
210
|
-
|
|
210
|
+
const first = await withSdk(this.brand, sdk => this.read(sdk))
|
|
211
|
+
if (first.length > 0) return first
|
|
212
|
+
// An empty catalogue is what a daemon answers after its credential went
|
|
213
|
+
// stale: the account lookup it makes first fails, so every `get_models`
|
|
214
|
+
// variant is empty and the page shows no models for a working account.
|
|
215
|
+
// Reset once so the replacement reads the credentials on disk now; keep the
|
|
216
|
+
// empty answer only if the retry is empty too (a plan really has none).
|
|
217
|
+
await resetDaemon(this.brand)
|
|
218
|
+
const retried = await withSdk(this.brand, sdk => this.read(sdk))
|
|
219
|
+
return retried.length > 0 ? retried : first
|
|
211
220
|
}
|
|
212
221
|
|
|
213
222
|
private async read(sdk: QoderSdkModule): Promise<readonly ModelInfo[]> {
|
|
214
223
|
const q = sdk.query({
|
|
215
224
|
prompt: inertInput(),
|
|
216
225
|
options: {
|
|
217
|
-
...
|
|
226
|
+
...cliOptions(this.brand),
|
|
218
227
|
auth: sdk.qodercliAuth(),
|
|
219
228
|
tools: [],
|
|
220
229
|
allowedTools: [],
|
package/src/sdk.ts
CHANGED
|
@@ -1,81 +1,77 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The SDK package behind each brand.
|
|
2
|
+
* The SDK package behind each brand, and the one daemon it keeps alive.
|
|
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
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* real dependency; the international one is optional at runtime — a deployment
|
|
7
|
+
* that only has a CN account may not have it installed — so it is imported on
|
|
8
|
+
* demand and its absence is reported to the settings page as a plain error
|
|
9
|
+
* instead of failing the plugin's mount.
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
* reason: an account may never use it, and its package carries a large CLI.
|
|
11
|
+
* ## The daemon, and why it has to be reset
|
|
13
12
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* interpreter, environment, proxy) and then refuses any different one for the
|
|
19
|
-
* life of the process:
|
|
13
|
+
* Every Qoder SDK routes all queries through one **process-wide daemon**,
|
|
14
|
+
* initialized with the first query's launch configuration (CLI path,
|
|
15
|
+
* interpreter, environment, proxy, launch mode) and pinned to it for the life
|
|
16
|
+
* of the process:
|
|
20
17
|
*
|
|
21
18
|
* DaemonConfigurationConflictError:
|
|
22
19
|
* Global daemon is already initialized with different options
|
|
23
20
|
*
|
|
24
|
-
* The host is long-lived, so
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
21
|
+
* The host is long-lived, so the machine is not: the CLI updates itself,
|
|
22
|
+
* an edition appears, an installer changes the environment, a credential is
|
|
23
|
+
* refreshed. Any of those can leave the ready daemon holding a configuration
|
|
24
|
+
* (or a credential) that no longer applies, and from then on *every* query
|
|
25
|
+
* fails until DSH restarts — which is what the settings page reports as a
|
|
26
|
+
* signed-out account with no models and no quota.
|
|
27
|
+
*
|
|
28
|
+
* Two things keep that from happening:
|
|
29
29
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
30
|
+
* - **The launch configuration is frozen per process** ({@link cliOptions}).
|
|
31
|
+
* Every call site used to call `qoderCliOptions()` for itself, so two
|
|
32
|
+
* concurrent reads could describe the machine slightly differently and the
|
|
33
|
+
* second one was rejected. One frozen object per edition means every query
|
|
34
|
+
* in this process is identical by construction.
|
|
35
|
+
* - **A conflict is repaired, not reported** ({@link resetDaemon}). The SDK
|
|
36
|
+
* exports `shutdownDaemon()`, which stops the daemon and clears the slot it
|
|
37
|
+
* occupies, so the next query initializes a fresh one with the current
|
|
38
|
+
* configuration. That is what makes sign-in, a CLI update and a credential
|
|
39
|
+
* refresh take effect without restarting DSH.
|
|
35
40
|
*
|
|
36
41
|
* @module dsh-agent-sdk-bridge/sdk
|
|
37
42
|
*/
|
|
38
43
|
|
|
39
|
-
import { createRequire } from 'node:module'
|
|
40
|
-
import { pathToFileURL } from 'node:url'
|
|
41
|
-
import * as cnSdk from '@qodercn-ai/qodercn-agent-sdk'
|
|
42
44
|
import type * as QoderSdk from '@qodercn-ai/qodercn-agent-sdk'
|
|
43
45
|
import type * as CodeBuddySdk from '@tencent-ai/agent-sdk'
|
|
46
|
+
import type { QoderCliOptions } from './cli.ts'
|
|
47
|
+
import { qoderCliOptions } from './cli.ts'
|
|
44
48
|
import type { CodeBuddyRegion, QoderBrand } from './brand.ts'
|
|
45
49
|
import { brandProfile, codeBuddyEnvironment } from './brand.ts'
|
|
46
|
-
import { qoderCliOptions } from './cli.ts'
|
|
47
50
|
|
|
48
51
|
/** The subset of the Qoder SDK surface this plugin uses, as one module type. */
|
|
49
52
|
export type QoderSdkModule = typeof QoderSdk
|
|
50
53
|
/** The CodeBuddy SDK module, typed from its declarations. */
|
|
51
54
|
export type CodeBuddySdkModule = typeof CodeBuddySdk
|
|
52
55
|
|
|
53
|
-
/** One
|
|
54
|
-
interface
|
|
56
|
+
/** One edition's frozen launch configuration and the string that identifies it. */
|
|
57
|
+
interface Pinned {
|
|
58
|
+
options: QoderCliOptions
|
|
55
59
|
signature: string
|
|
56
|
-
module: Promise<QoderSdkModule>
|
|
57
60
|
}
|
|
58
61
|
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
const pinned = new Map<QoderBrand, Pinned>()
|
|
63
|
+
const modules = new Map<QoderBrand, Promise<QoderSdkModule>>()
|
|
64
|
+
/** Resets in flight, so a query cannot start against a daemon being stopped. */
|
|
65
|
+
const resets = new Map<QoderBrand, Promise<void>>()
|
|
63
66
|
let codeBuddySdk: Promise<CodeBuddySdkModule> | undefined
|
|
64
67
|
|
|
65
|
-
const require = createRequire(import.meta.url)
|
|
66
|
-
|
|
67
68
|
/**
|
|
68
|
-
*
|
|
69
|
+
* Describe a launch configuration as a comparable string.
|
|
69
70
|
*
|
|
70
|
-
*
|
|
71
|
-
* environment is included because the SDK hands it to the CLI verbatim
|
|
72
|
-
* difference there is enough to make the daemon reject a query.
|
|
73
|
-
*
|
|
74
|
-
* @param brand - which edition's configuration to describe.
|
|
75
|
-
* @returns a stable string that changes exactly when the configuration does.
|
|
71
|
+
* The fields are the ones the SDK's daemon compares before reusing itself. The
|
|
72
|
+
* environment is included because the SDK hands it to the CLI verbatim.
|
|
76
73
|
*/
|
|
77
|
-
|
|
78
|
-
const options = qoderCliOptions(brand)
|
|
74
|
+
function signatureOf(options: QoderCliOptions): string {
|
|
79
75
|
const env = Object.entries(options.env ?? {})
|
|
80
76
|
.filter(([, value]) => value !== undefined)
|
|
81
77
|
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
@@ -87,26 +83,48 @@ export function configurationSignature(brand: QoderBrand): string {
|
|
|
87
83
|
}
|
|
88
84
|
|
|
89
85
|
/**
|
|
90
|
-
*
|
|
86
|
+
* This edition's launch configuration, computed once per process.
|
|
91
87
|
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
88
|
+
* Frozen on purpose: the daemon accepts exactly one configuration, so every
|
|
89
|
+
* query has to send the same one. Recomputing it per call let concurrent
|
|
90
|
+
* reads disagree — a machine-wide `QODERCLI_PATH`, a CLI that finished
|
|
91
|
+
* updating between two calls, a PATH entry that appeared — and the daemon
|
|
92
|
+
* rejected whichever arrived second, permanently.
|
|
95
93
|
*
|
|
96
|
-
* @
|
|
97
|
-
*
|
|
94
|
+
* {@link resetDaemon} drops the pin, so a real change (install, sign-in, CLI
|
|
95
|
+
* update) is picked up by the next query instead of being frozen out.
|
|
96
|
+
*
|
|
97
|
+
* @param brand - which edition's configuration to return.
|
|
98
98
|
*/
|
|
99
|
-
|
|
99
|
+
export function cliOptions(brand: QoderBrand): QoderCliOptions {
|
|
100
|
+
const held = pinned.get(brand)
|
|
101
|
+
if (held !== undefined) return held.options
|
|
102
|
+
const options = qoderCliOptions(brand)
|
|
103
|
+
pinned.set(brand, { options, signature: signatureOf(options) })
|
|
104
|
+
return options
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The signature of this edition's frozen configuration.
|
|
109
|
+
*
|
|
110
|
+
* Kept for diagnostics and tests: two calls in one process always agree, which
|
|
111
|
+
* is the property the daemon requires.
|
|
112
|
+
*
|
|
113
|
+
* @param brand - which edition to describe.
|
|
114
|
+
*/
|
|
115
|
+
export function configurationSignature(brand: QoderBrand): string {
|
|
116
|
+
cliOptions(brand)
|
|
117
|
+
return pinned.get(brand)?.signature ?? ''
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Import one edition's SDK; a missing optional package is reported in context. */
|
|
121
|
+
async function importSdk(brand: QoderBrand): Promise<QoderSdkModule> {
|
|
100
122
|
const specifier = brandProfile(brand).sdkPackage
|
|
101
123
|
try {
|
|
102
|
-
|
|
103
|
-
const entry = pathToFileURL(require.resolve(specifier)).href
|
|
104
|
-
counter += 1
|
|
105
|
-
return await import(/* @vite-ignore */ `${entry}?generation=${counter}`) as QoderSdkModule
|
|
124
|
+
return await import(/* @vite-ignore */ specifier) as QoderSdkModule
|
|
106
125
|
} catch (error) {
|
|
107
126
|
// Let a later read retry: the package may be installed while DSH is running.
|
|
108
|
-
|
|
109
|
-
forced.delete(brand)
|
|
127
|
+
modules.delete(brand)
|
|
110
128
|
if (brand === 'cn') throw error
|
|
111
129
|
const detail = error instanceof Error ? error.message : String(error)
|
|
112
130
|
throw new Error(`未安装国际版 SDK(${specifier}):${detail}`)
|
|
@@ -114,60 +132,85 @@ async function importSdk(brand: QoderBrand, fresh: boolean): Promise<QoderSdkMod
|
|
|
114
132
|
}
|
|
115
133
|
|
|
116
134
|
/**
|
|
117
|
-
* Load the SDK for one Qoder edition
|
|
135
|
+
* Load the SDK for one Qoder edition.
|
|
136
|
+
*
|
|
137
|
+
* One module instance per edition, reused for the process's lifetime: the
|
|
138
|
+
* daemon it owns is reset explicitly rather than by re-importing the package.
|
|
118
139
|
*
|
|
119
140
|
* @param brand - which edition's SDK to load.
|
|
120
|
-
* @returns the SDK module.
|
|
121
141
|
*/
|
|
122
142
|
export function loadSdk(brand: QoderBrand): Promise<QoderSdkModule> {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
143
|
+
// A reset that is still stopping the daemon must finish first: the next
|
|
144
|
+
// query has to initialize a fresh daemon, not attach to the one leaving.
|
|
145
|
+
const resetting = resets.get(brand)
|
|
146
|
+
if (resetting !== undefined) return resetting.then(() => loadSdk(brand))
|
|
147
|
+
let held = modules.get(brand)
|
|
148
|
+
if (held === undefined) {
|
|
149
|
+
held = importSdk(brand)
|
|
150
|
+
modules.set(brand, held)
|
|
151
|
+
held.catch(() => {
|
|
152
|
+
if (modules.get(brand) === held) modules.delete(brand)
|
|
153
|
+
})
|
|
127
154
|
}
|
|
128
|
-
|
|
129
|
-
const module = importSdk(brand, mustReload)
|
|
130
|
-
const generation = { signature, module }
|
|
131
|
-
generations.set(brand, generation)
|
|
132
|
-
module.catch(() => {
|
|
133
|
-
if (generations.get(brand) === generation) generations.delete(brand)
|
|
134
|
-
})
|
|
135
|
-
return module
|
|
155
|
+
return held
|
|
136
156
|
}
|
|
137
157
|
|
|
138
158
|
/**
|
|
139
|
-
*
|
|
159
|
+
* Stop this edition's daemon so the next query starts a clean one.
|
|
140
160
|
*
|
|
141
|
-
*
|
|
161
|
+
* Three situations need this, and they are not distinguishable from the
|
|
162
|
+
* outside — all three look like "the account is gone" until something resets:
|
|
163
|
+
*
|
|
164
|
+
* - the launch configuration changed since the daemon started;
|
|
165
|
+
* - the CLI was updated underneath it;
|
|
166
|
+
* - the credentials on disk changed (sign-in, sign-out, refresh) while the
|
|
167
|
+
* daemon still holds the ones it loaded at start-up.
|
|
168
|
+
*
|
|
169
|
+
* `shutdownDaemon()` is the SDK's own way to clear the process-wide slot, so
|
|
170
|
+
* the next `query()` initializes against the current machine instead of
|
|
171
|
+
* arguing with a stale daemon until DSH restarts.
|
|
172
|
+
*
|
|
173
|
+
* @param brand - which edition's daemon to replace.
|
|
142
174
|
*/
|
|
143
|
-
export function
|
|
144
|
-
|
|
145
|
-
|
|
175
|
+
export async function resetDaemon(brand: QoderBrand): Promise<void> {
|
|
176
|
+
// The pin is dropped first: whatever changed must be reflected in the
|
|
177
|
+
// configuration the new daemon is built with.
|
|
178
|
+
pinned.delete(brand)
|
|
179
|
+
const held = modules.get(brand)
|
|
180
|
+
if (held === undefined) return
|
|
181
|
+
const sdk = await held.catch(() => undefined)
|
|
182
|
+
if (sdk === undefined) {
|
|
183
|
+
modules.delete(brand)
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
await sdk.shutdownDaemon()
|
|
188
|
+
} catch {
|
|
189
|
+
// Nothing running, or it died on its own: either way the slot is free.
|
|
190
|
+
}
|
|
146
191
|
}
|
|
147
192
|
|
|
148
193
|
/**
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
* A daemon loads the CLI's credentials when it initializes, and those
|
|
152
|
-
* credentials are not part of the configuration signature: signing in as
|
|
153
|
-
* another account leaves the daemon answering as the account it started with.
|
|
154
|
-
* Shutting it down and forcing a new instance makes the next query read the
|
|
155
|
-
* credentials that are on disk now.
|
|
194
|
+
* Fire-and-forget {@link resetDaemon}.
|
|
156
195
|
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
196
|
+
* For callers that cannot await (a synchronous callback, a consumer loop that
|
|
197
|
+
* has already failed). The daemon is stopped in the background and the next
|
|
198
|
+
* query initializes a fresh one.
|
|
160
199
|
*
|
|
161
|
-
* @param brand - which edition
|
|
200
|
+
* @param brand - which edition to reset.
|
|
162
201
|
*/
|
|
202
|
+
export function resetSdk(brand: QoderBrand): void {
|
|
203
|
+
const previous = resets.get(brand) ?? Promise.resolve()
|
|
204
|
+
const next = previous.then(() => resetDaemon(brand)).catch(() => undefined)
|
|
205
|
+
resets.set(brand, next)
|
|
206
|
+
void next.finally(() => {
|
|
207
|
+
if (resets.get(brand) === next) resets.delete(brand)
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Alias kept for the logout path, whose wording is about discarding state. */
|
|
163
212
|
export function discardSdk(brand: QoderBrand): void {
|
|
164
|
-
|
|
165
|
-
generations.delete(brand)
|
|
166
|
-
forced.add(brand)
|
|
167
|
-
if (generation === undefined) return
|
|
168
|
-
void generation.module
|
|
169
|
-
.then(sdk => sdk.shutdownDaemon())
|
|
170
|
-
.catch(() => undefined)
|
|
213
|
+
void resetDaemon(brand)
|
|
171
214
|
}
|
|
172
215
|
|
|
173
216
|
/**
|
|
@@ -182,13 +225,10 @@ export function isDaemonConflict(error: unknown): boolean {
|
|
|
182
225
|
}
|
|
183
226
|
|
|
184
227
|
/**
|
|
185
|
-
* Run one idempotent SDK operation,
|
|
228
|
+
* Run one idempotent SDK operation, repairing a stale daemon once.
|
|
186
229
|
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* current one, so the retry succeeds without a restart. Only use this for
|
|
190
|
-
* operations that can be repeated without side effects (reads), never for a
|
|
191
|
-
* turn that may already have emitted output.
|
|
230
|
+
* Only use this for operations that can be repeated without side effects
|
|
231
|
+
* (reads), never for a turn that may already have emitted output.
|
|
192
232
|
*
|
|
193
233
|
* @param brand - which edition to query.
|
|
194
234
|
* @param run - the operation, given the module to use.
|
|
@@ -201,7 +241,7 @@ export async function withSdk<T>(
|
|
|
201
241
|
return await run(await loadSdk(brand))
|
|
202
242
|
} catch (error) {
|
|
203
243
|
if (!isDaemonConflict(error)) throw error
|
|
204
|
-
|
|
244
|
+
await resetDaemon(brand)
|
|
205
245
|
return await run(await loadSdk(brand))
|
|
206
246
|
}
|
|
207
247
|
}
|
|
@@ -222,7 +262,7 @@ export async function probeDaemon(brand: QoderBrand): Promise<void> {
|
|
|
222
262
|
const q = sdk.query({
|
|
223
263
|
prompt: (async function* () { await new Promise<never>(() => {}) })(),
|
|
224
264
|
options: {
|
|
225
|
-
...
|
|
265
|
+
...cliOptions(brand),
|
|
226
266
|
auth: sdk.qodercliAuth(),
|
|
227
267
|
tools: [],
|
|
228
268
|
allowedTools: [],
|
|
@@ -261,4 +301,4 @@ async function importCodeBuddySdk(): Promise<CodeBuddySdkModule> {
|
|
|
261
301
|
|
|
262
302
|
/** The SDK auth environment for a CodeBuddy login region. */
|
|
263
303
|
export { codeBuddyEnvironment }
|
|
264
|
-
export type { CodeBuddyRegion }
|
|
304
|
+
export type { CodeBuddyRegion }
|
package/src/session.ts
CHANGED
|
@@ -25,7 +25,7 @@ import type { Live } from './live.ts'
|
|
|
25
25
|
import { readLive } from './live.ts'
|
|
26
26
|
import { stripMcpNamespace } from './mcp-naming.ts'
|
|
27
27
|
import { feedImageRefs, feedTextLength, feedWideCharCount, renderInitialFeed } from './render.ts'
|
|
28
|
-
import { isDaemonConflict, loadSdk, probeDaemon,
|
|
28
|
+
import { cliOptions, isDaemonConflict, loadSdk, probeDaemon, resetDaemon } from './sdk.ts'
|
|
29
29
|
import { TurnOutputAccounting, estimateTextTokens } from './tokens.ts'
|
|
30
30
|
|
|
31
31
|
/** MCP server name this adapter exposes host tools under. */
|
|
@@ -1054,7 +1054,7 @@ export class QoderSession {
|
|
|
1054
1054
|
try {
|
|
1055
1055
|
await probeDaemon(this.brand)
|
|
1056
1056
|
} catch (error) {
|
|
1057
|
-
if (isDaemonConflict(error))
|
|
1057
|
+
if (isDaemonConflict(error)) await resetDaemon(this.brand)
|
|
1058
1058
|
}
|
|
1059
1059
|
// The SDK is brand-specific: the CN module speaks to qoderclicn and the
|
|
1060
1060
|
// international one to qodercli, each against its own credentials.
|
|
@@ -1070,7 +1070,7 @@ export class QoderSession {
|
|
|
1070
1070
|
// Spawn the CLI instead of running its runtime in a worker thread: in
|
|
1071
1071
|
// this process that thread's sharp fights the host's own over the
|
|
1072
1072
|
// libvips DLL every image needs. See `cli.ts`.
|
|
1073
|
-
...
|
|
1073
|
+
...cliOptions(this.brand),
|
|
1074
1074
|
auth: sdk.qodercliAuth(),
|
|
1075
1075
|
tools: [],
|
|
1076
1076
|
allowedTools: [],
|
|
@@ -1444,7 +1444,7 @@ export class QoderSession {
|
|
|
1444
1444
|
// the same way; dropping it here lets the next turn start a fresh daemon
|
|
1445
1445
|
// instead of inheriting the broken one.
|
|
1446
1446
|
if (isDaemonConflict(error)) {
|
|
1447
|
-
|
|
1447
|
+
await resetDaemon(this.brand)
|
|
1448
1448
|
this.q = null
|
|
1449
1449
|
void q.close().catch(() => undefined)
|
|
1450
1450
|
}
|
|
@@ -1759,7 +1759,7 @@ export class QoderSessionManager {
|
|
|
1759
1759
|
const q = sdk.query({
|
|
1760
1760
|
prompt,
|
|
1761
1761
|
options: {
|
|
1762
|
-
...
|
|
1762
|
+
...cliOptions(brand),
|
|
1763
1763
|
auth: sdk.qodercliAuth(),
|
|
1764
1764
|
tools: [],
|
|
1765
1765
|
allowedTools: [],
|
|
@@ -1797,7 +1797,7 @@ export class QoderSessionManager {
|
|
|
1797
1797
|
if (signal?.aborted !== true) {
|
|
1798
1798
|
// Same poisoning as the warm path: drop the instance so the next
|
|
1799
1799
|
// request starts a daemon that matches the current configuration.
|
|
1800
|
-
if (isDaemonConflict(error))
|
|
1800
|
+
if (isDaemonConflict(error)) await resetDaemon(brand)
|
|
1801
1801
|
throw error
|
|
1802
1802
|
}
|
|
1803
1803
|
}
|
package/src/usage.ts
CHANGED
|
@@ -18,12 +18,12 @@
|
|
|
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, cliFailureText, foreignCliOverride,
|
|
21
|
+
import { cliConflictMessage, cliFailureText, foreignCliOverride, 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'
|
|
25
25
|
import type { QoderSdkModule } from './sdk.ts'
|
|
26
|
-
import { withSdk } from './sdk.ts'
|
|
26
|
+
import { cliOptions, resetDaemon, withSdk } from './sdk.ts'
|
|
27
27
|
|
|
28
28
|
/** How long one quota reading stays fresh by default. */
|
|
29
29
|
export const DEFAULT_USAGE_CACHE_TTL_MS = 60_000
|
|
@@ -137,7 +137,22 @@ export class QoderUsage {
|
|
|
137
137
|
if (conflict !== undefined) {
|
|
138
138
|
return { at, cliMissing: true, error: cliConflictMessage(this.brand) }
|
|
139
139
|
}
|
|
140
|
-
|
|
140
|
+
const snapshot = await withSdk(this.brand, sdk => this.readSnapshot(sdk, at))
|
|
141
|
+
// No quota reading at all — including the case where the account is
|
|
142
|
+
// missing too — is the signature of a daemon that started before the
|
|
143
|
+
// credential was refreshed or replaced: it keeps presenting the identity
|
|
144
|
+
// it loaded at start-up, and the CLI answers nothing for it. That is what
|
|
145
|
+
// the page reported as "logged in but no quota", and it never healed
|
|
146
|
+
// because every later read reused the same daemon.
|
|
147
|
+
//
|
|
148
|
+
// Reset once and read again. The replacement process loads the
|
|
149
|
+
// credentials that are on disk *now*; the first reading is kept only when
|
|
150
|
+
// the retry cannot do better, so a genuinely empty account still renders
|
|
151
|
+
// its own message instead of a spurious error.
|
|
152
|
+
if (snapshot.usage !== undefined || snapshot.cliMissing === true) return snapshot
|
|
153
|
+
await resetDaemon(this.brand)
|
|
154
|
+
const retried = await withSdk(this.brand, sdk => this.readSnapshot(sdk, at))
|
|
155
|
+
return retried.usage !== undefined ? retried : snapshot
|
|
141
156
|
} catch (error) {
|
|
142
157
|
const detail = error instanceof Error ? error.message : String(error)
|
|
143
158
|
// The quota card is the first thing a user sees on a machine without the
|
|
@@ -156,7 +171,7 @@ export class QoderUsage {
|
|
|
156
171
|
const q = sdk.query({
|
|
157
172
|
prompt: inertInput(),
|
|
158
173
|
options: {
|
|
159
|
-
...
|
|
174
|
+
...cliOptions(this.brand),
|
|
160
175
|
auth: sdk.qodercliAuth(),
|
|
161
176
|
tools: [],
|
|
162
177
|
allowedTools: [],
|