dsh-model-think-level 1.1.4 → 1.1.6
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/README.md +45 -8
- package/docs/image.png +0 -0
- package/docs/image2.png +0 -0
- package/docs/image3.png +0 -0
- package/docs/image4.png +0 -0
- package/docs/image5.png +0 -0
- package/lib/client/LongCacheSection.d.ts +36 -0
- package/lib/client/injection/autofill-run.d.ts +2 -0
- package/lib/client/injection/composer-menu.d.ts +18 -5
- package/lib/client/injection/model-menu.d.ts +30 -2
- package/lib/client/injection/models-page-editor.d.ts +52 -1
- package/lib/client/locales.d.ts +9 -4
- package/lib/client/ops.d.ts +75 -2
- package/lib/client/styles.d.ts +1 -1
- package/lib/client.js +69 -299
- package/lib/client.js.map +4 -4
- package/lib/index.js +223 -105
- package/lib/index.js.map +3 -3
- package/lib/key-index.d.ts +4 -3
- package/package.json +19 -4
package/lib/index.js
CHANGED
|
@@ -92,11 +92,31 @@ function parseKeyIndex(raw) {
|
|
|
92
92
|
return { version: 1, providers: out };
|
|
93
93
|
}
|
|
94
94
|
function readKeyIndex(file = keyIndexFilePath()) {
|
|
95
|
+
let text;
|
|
95
96
|
try {
|
|
96
|
-
|
|
97
|
-
} catch {
|
|
98
|
-
|
|
97
|
+
text = readFileSync(file, "utf8");
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
|
|
100
|
+
return emptyKeyIndex();
|
|
101
|
+
}
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
const raw = JSON.parse(text);
|
|
105
|
+
const record = typeof raw === "object" && raw !== null && !Array.isArray(raw) ? raw : void 0;
|
|
106
|
+
const providers = record?.["providers"];
|
|
107
|
+
if (record === void 0 || record["version"] !== void 0 && record["version"] !== 1 || typeof providers !== "object" || providers === null || Array.isArray(providers)) {
|
|
108
|
+
throw new Error("the key index is malformed; repair it before changing keys");
|
|
99
109
|
}
|
|
110
|
+
for (const [route, entries] of Object.entries(providers)) {
|
|
111
|
+
if (route.length === 0 || !Array.isArray(entries) || entries.some((entry) => {
|
|
112
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) return true;
|
|
113
|
+
const ref = entry["ref"];
|
|
114
|
+
return typeof ref !== "string" || ref.length === 0;
|
|
115
|
+
})) {
|
|
116
|
+
throw new Error("the key index is malformed; repair it before changing keys");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return parseKeyIndex(raw);
|
|
100
120
|
}
|
|
101
121
|
function writeKeyIndex(document, file = keyIndexFilePath()) {
|
|
102
122
|
mkdirSync(dirname(file), { recursive: true });
|
|
@@ -2498,13 +2518,36 @@ async function forgetCredential(credentials, ref) {
|
|
|
2498
2518
|
return error instanceof Error ? error.message : String(error);
|
|
2499
2519
|
}
|
|
2500
2520
|
}
|
|
2521
|
+
var HostReadError = class extends Error {
|
|
2522
|
+
};
|
|
2523
|
+
async function resolveCredential(credentials, ref) {
|
|
2524
|
+
if (credentials === void 0 || typeof credentials.resolve !== "function") {
|
|
2525
|
+
throw new HostReadError("the credential store is unavailable");
|
|
2526
|
+
}
|
|
2527
|
+
let hit;
|
|
2528
|
+
try {
|
|
2529
|
+
hit = await credentials.resolve(ref);
|
|
2530
|
+
} catch {
|
|
2531
|
+
throw new HostReadError("the credential store could not read the reference");
|
|
2532
|
+
}
|
|
2533
|
+
if (isRefusal(hit) || hit !== void 0 && !isRecord(hit)) {
|
|
2534
|
+
throw new HostReadError("the credential store could not read the reference");
|
|
2535
|
+
}
|
|
2536
|
+
if (hit === void 0) return void 0;
|
|
2537
|
+
const value = hit["value"];
|
|
2538
|
+
if (value !== void 0 && typeof value !== "string") {
|
|
2539
|
+
throw new HostReadError("the credential store returned an invalid value");
|
|
2540
|
+
}
|
|
2541
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
2542
|
+
}
|
|
2501
2543
|
async function credentialFacts(credentials, refs) {
|
|
2502
2544
|
const out = {};
|
|
2503
2545
|
if (credentials === void 0 || refs.length === 0) return out;
|
|
2504
2546
|
const describe = credentials.describe;
|
|
2505
2547
|
if (typeof describe === "function") {
|
|
2506
2548
|
try {
|
|
2507
|
-
const
|
|
2549
|
+
const answer = await describe.call(credentials, [...refs]);
|
|
2550
|
+
const records = isRefusal(answer) ? void 0 : describeRecords(answer);
|
|
2508
2551
|
for (const ref of refs) {
|
|
2509
2552
|
const info = records?.[ref];
|
|
2510
2553
|
if (isRecord(info) && typeof info["configured"] === "boolean") {
|
|
@@ -2514,11 +2557,9 @@ async function credentialFacts(credentials, refs) {
|
|
|
2514
2557
|
} catch {
|
|
2515
2558
|
}
|
|
2516
2559
|
}
|
|
2517
|
-
const resolve2 = credentials.resolve;
|
|
2518
2560
|
for (const ref of refs) {
|
|
2519
2561
|
try {
|
|
2520
|
-
const
|
|
2521
|
-
const value = hit !== void 0 && typeof hit.value === "string" ? hit.value : void 0;
|
|
2562
|
+
const value = await resolveCredential(credentials, ref);
|
|
2522
2563
|
const configured = out[ref]?.configured ?? (value !== void 0 && value.length > 0);
|
|
2523
2564
|
const masked = value === void 0 ? void 0 : maskKeyValue(value);
|
|
2524
2565
|
out[ref] = masked === void 0 || masked.length === 0 ? { configured } : { configured, masked };
|
|
@@ -2723,15 +2764,19 @@ function apply(ctx, config = {}) {
|
|
|
2723
2764
|
const settings = ctx.settings;
|
|
2724
2765
|
let piSectionCache;
|
|
2725
2766
|
let piSectionCached = false;
|
|
2726
|
-
const piSection = () => {
|
|
2767
|
+
const piSection = (required = false) => {
|
|
2727
2768
|
if (piSectionCached) return piSectionCache;
|
|
2728
2769
|
let value;
|
|
2729
2770
|
try {
|
|
2730
2771
|
value = settings.describe().find((entry) => entry.ns === PI_NS)?.value;
|
|
2731
2772
|
} catch {
|
|
2773
|
+
if (required) throw new HostReadError("the llm-pi-ai settings could not be read");
|
|
2774
|
+
return void 0;
|
|
2775
|
+
}
|
|
2776
|
+
if (value === void 0) {
|
|
2777
|
+
if (required) throw new HostReadError("the llm-pi-ai settings entry is unavailable");
|
|
2732
2778
|
return void 0;
|
|
2733
2779
|
}
|
|
2734
|
-
if (value === void 0) return void 0;
|
|
2735
2780
|
piSectionCache = value;
|
|
2736
2781
|
piSectionCached = true;
|
|
2737
2782
|
return value;
|
|
@@ -2785,11 +2830,15 @@ function apply(ctx, config = {}) {
|
|
|
2785
2830
|
if (llm === void 0 || typeof llm.prepareCall !== "function" || typeof llm.stream !== "function") return;
|
|
2786
2831
|
const origPrepare = llm.prepareCall;
|
|
2787
2832
|
const origStream = llm.stream;
|
|
2788
|
-
|
|
2789
|
-
|
|
2833
|
+
let disposed = false;
|
|
2834
|
+
const prepare = (callConfig, signal) => origPrepare.call(llm, disposed ? callConfig : guardCallConfig(callConfig, piSection()), signal);
|
|
2835
|
+
const stream = (options) => origStream.call(llm, disposed ? options : guardCallConfig(options, piSection()));
|
|
2836
|
+
llm.prepareCall = prepare;
|
|
2837
|
+
llm.stream = stream;
|
|
2790
2838
|
ctx.effect(() => () => {
|
|
2791
|
-
|
|
2792
|
-
llm.
|
|
2839
|
+
disposed = true;
|
|
2840
|
+
if (llm.prepareCall === prepare) llm.prepareCall = origPrepare;
|
|
2841
|
+
if (llm.stream === stream) llm.stream = origStream;
|
|
2793
2842
|
}, "dsh-model-think-level: default-guard");
|
|
2794
2843
|
});
|
|
2795
2844
|
}
|
|
@@ -2853,7 +2902,7 @@ function apply(ctx, config = {}) {
|
|
|
2853
2902
|
} else {
|
|
2854
2903
|
installPiAiBridge(availableLlm);
|
|
2855
2904
|
}
|
|
2856
|
-
const
|
|
2905
|
+
const environmentConflicts = detectHeaderConflicts();
|
|
2857
2906
|
ctx.inject(["webServer"], (webServerCtx) => {
|
|
2858
2907
|
ctx.effect(
|
|
2859
2908
|
() => webServerCtx.webServer.register({
|
|
@@ -2881,7 +2930,13 @@ function apply(ctx, config = {}) {
|
|
|
2881
2930
|
return;
|
|
2882
2931
|
}
|
|
2883
2932
|
const route = url.searchParams.get("route") ?? "";
|
|
2884
|
-
|
|
2933
|
+
let section;
|
|
2934
|
+
try {
|
|
2935
|
+
section = piSection(true);
|
|
2936
|
+
} catch {
|
|
2937
|
+
sendJson(res, 502, { ok: false, error: "the llm-pi-ai settings could not be read" });
|
|
2938
|
+
return;
|
|
2939
|
+
}
|
|
2885
2940
|
const profile = isRecord(section) && isRecord(section["providers"]) ? section["providers"][route] : void 0;
|
|
2886
2941
|
if (!isRecord(profile)) {
|
|
2887
2942
|
sendJson(res, 400, { ok: false, error: `no llm-pi-ai provider route "${route}"` });
|
|
@@ -2912,10 +2967,10 @@ function apply(ctx, config = {}) {
|
|
|
2912
2967
|
let apiKey;
|
|
2913
2968
|
if (apiKeyEnv !== void 0) {
|
|
2914
2969
|
try {
|
|
2915
|
-
|
|
2916
|
-
const hit = credentials === void 0 ? void 0 : await credentials.resolve(apiKeyEnv);
|
|
2917
|
-
apiKey = hit !== void 0 && typeof hit.value === "string" && hit.value.length > 0 ? hit.value : void 0;
|
|
2970
|
+
apiKey = await resolveCredential(ctx.get("credentials"), apiKeyEnv);
|
|
2918
2971
|
} catch {
|
|
2972
|
+
sendJson(res, 502, { ok: false, error: "the credential store could not read the reference" });
|
|
2973
|
+
return;
|
|
2919
2974
|
}
|
|
2920
2975
|
}
|
|
2921
2976
|
try {
|
|
@@ -3012,7 +3067,13 @@ function apply(ctx, config = {}) {
|
|
|
3012
3067
|
}
|
|
3013
3068
|
const url = new URL(req.url ?? "/", "http://x");
|
|
3014
3069
|
const route = url.searchParams.get("route") ?? "";
|
|
3015
|
-
|
|
3070
|
+
let section;
|
|
3071
|
+
try {
|
|
3072
|
+
section = piSection(true);
|
|
3073
|
+
} catch {
|
|
3074
|
+
sendJson(res, 502, { ok: false, error: "the llm-pi-ai settings could not be read" });
|
|
3075
|
+
return;
|
|
3076
|
+
}
|
|
3016
3077
|
const profile = isRecord(section) && isRecord(section["providers"]) ? section["providers"][route] : void 0;
|
|
3017
3078
|
if (!isRecord(profile)) {
|
|
3018
3079
|
sendJson(res, 400, { ok: false, error: `no llm-pi-ai provider route "${route}"` });
|
|
@@ -3020,7 +3081,13 @@ function apply(ctx, config = {}) {
|
|
|
3020
3081
|
}
|
|
3021
3082
|
const apiKeyEnv = typeof profile["apiKeyEnv"] === "string" ? profile["apiKeyEnv"] : void 0;
|
|
3022
3083
|
const named = url.searchParams.get("ref") ?? "";
|
|
3023
|
-
|
|
3084
|
+
let ref;
|
|
3085
|
+
try {
|
|
3086
|
+
ref = named.length > 0 ? named === apiKeyEnv || hasKeyEntry(readKeyIndex(keyIndexFilePath()), route, named) ? named : void 0 : apiKeyEnv;
|
|
3087
|
+
} catch {
|
|
3088
|
+
sendJson(res, 500, { ok: false, error: "the key index could not be read" });
|
|
3089
|
+
return;
|
|
3090
|
+
}
|
|
3024
3091
|
if (ref === void 0) {
|
|
3025
3092
|
sendJson(res, 404, {
|
|
3026
3093
|
ok: false,
|
|
@@ -3030,11 +3097,10 @@ function apply(ctx, config = {}) {
|
|
|
3030
3097
|
}
|
|
3031
3098
|
let value;
|
|
3032
3099
|
try {
|
|
3033
|
-
|
|
3034
|
-
const hit = credentials === void 0 ? void 0 : await credentials.resolve(ref);
|
|
3035
|
-
value = hit !== void 0 && typeof hit.value === "string" && hit.value.length > 0 ? hit.value : void 0;
|
|
3100
|
+
value = await resolveCredential(ctx.get("credentials"), ref);
|
|
3036
3101
|
} catch {
|
|
3037
|
-
|
|
3102
|
+
sendJson(res, 502, { ok: false, error: "the credential store could not read the reference" });
|
|
3103
|
+
return;
|
|
3038
3104
|
}
|
|
3039
3105
|
if (value === void 0) {
|
|
3040
3106
|
sendJson(res, 404, { ok: false, error: `no credential is configured for ${ref}` });
|
|
@@ -3045,6 +3111,7 @@ function apply(ctx, config = {}) {
|
|
|
3045
3111
|
}),
|
|
3046
3112
|
"dsh-model-think-level: provider-key route"
|
|
3047
3113
|
);
|
|
3114
|
+
let keyMutationTail = Promise.resolve();
|
|
3048
3115
|
ctx.effect(
|
|
3049
3116
|
() => webServerCtx.webServer.register({
|
|
3050
3117
|
kind: "exact",
|
|
@@ -3061,70 +3128,79 @@ function apply(ctx, config = {}) {
|
|
|
3061
3128
|
}
|
|
3062
3129
|
const url = new URL(req.url ?? "/", "http://x");
|
|
3063
3130
|
const route = url.searchParams.get("route") ?? "";
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3131
|
+
let releaseMutation;
|
|
3132
|
+
try {
|
|
3133
|
+
if (req.method === "POST") {
|
|
3134
|
+
const previous = keyMutationTail;
|
|
3135
|
+
keyMutationTail = new Promise((resolve2) => {
|
|
3136
|
+
releaseMutation = resolve2;
|
|
3137
|
+
});
|
|
3138
|
+
await previous;
|
|
3070
3139
|
}
|
|
3071
|
-
|
|
3072
|
-
if (route.length === 0 || providerProfileOf(entryOf()?.value, route) === void 0) {
|
|
3073
|
-
sendJson(res, 400, { ok: false, error: `no llm-pi-ai provider route "${route}"` });
|
|
3074
|
-
return;
|
|
3075
|
-
}
|
|
3076
|
-
const credentials = ctx.get("credentials");
|
|
3077
|
-
const indexFile = keyIndexFilePath();
|
|
3078
|
-
const enabledNow = () => enabledRefOf(providerProfileOf(entryOf()?.value, route));
|
|
3079
|
-
const setEnabledKey = async (ref2) => {
|
|
3080
|
-
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
3081
|
-
const entry = entryOf();
|
|
3082
|
-
if (entry === void 0) return "the llm-pi-ai settings entry is unavailable";
|
|
3140
|
+
const entryOf = () => {
|
|
3083
3141
|
try {
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
);
|
|
3089
|
-
return void 0;
|
|
3090
|
-
} catch (error) {
|
|
3091
|
-
if (attempt === 0 && isConflictError(error)) continue;
|
|
3092
|
-
return error instanceof Error ? error.message : String(error);
|
|
3142
|
+
const found = settings.describe().find((candidate) => candidate.ns === PI_NS);
|
|
3143
|
+
if (found === void 0) throw new HostReadError("the llm-pi-ai settings entry is unavailable");
|
|
3144
|
+
return { value: found.value, revision: found.revision };
|
|
3145
|
+
} catch {
|
|
3146
|
+
throw new HostReadError("the llm-pi-ai settings could not be read");
|
|
3093
3147
|
}
|
|
3148
|
+
};
|
|
3149
|
+
if (route.length === 0 || providerProfileOf(entryOf()?.value, route) === void 0) {
|
|
3150
|
+
sendJson(res, 400, { ok: false, error: `no llm-pi-ai provider route "${route}"` });
|
|
3151
|
+
return;
|
|
3094
3152
|
}
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
const
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3153
|
+
const credentials = ctx.get("credentials");
|
|
3154
|
+
const indexFile = keyIndexFilePath();
|
|
3155
|
+
const enabledNow = () => enabledRefOf(providerProfileOf(entryOf()?.value, route));
|
|
3156
|
+
const setEnabledKey = async (ref2) => {
|
|
3157
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
3158
|
+
const entry = entryOf();
|
|
3159
|
+
if (entry === void 0) return "the llm-pi-ai settings entry is unavailable";
|
|
3160
|
+
try {
|
|
3161
|
+
await settings.mutate(
|
|
3162
|
+
PI_NS,
|
|
3163
|
+
ref2 === void 0 ? [{ op: "unset", path: ["providers", route, "apiKeyEnv"] }] : [{ op: "set", path: ["providers", route, "apiKeyEnv"], value: ref2 }],
|
|
3164
|
+
entry.revision
|
|
3165
|
+
);
|
|
3166
|
+
return void 0;
|
|
3167
|
+
} catch (error) {
|
|
3168
|
+
if (attempt === 0 && isConflictError(error)) continue;
|
|
3169
|
+
return error instanceof Error ? error.message : String(error);
|
|
3170
|
+
}
|
|
3171
|
+
}
|
|
3172
|
+
return "the settings document kept changing; try again";
|
|
3173
|
+
};
|
|
3174
|
+
const payload = async () => {
|
|
3175
|
+
const enabledRef = enabledNow();
|
|
3176
|
+
const listed = keyEntriesOf(readKeyIndex(indexFile), route);
|
|
3177
|
+
const refs = listed.map((entry) => entry.ref);
|
|
3178
|
+
if (enabledRef !== void 0 && !refs.includes(enabledRef)) refs.push(enabledRef);
|
|
3179
|
+
const facts = await credentialFacts(credentials, refs);
|
|
3180
|
+
const entries = refs.map((ref2) => {
|
|
3181
|
+
const fact = facts[ref2];
|
|
3182
|
+
const view = {
|
|
3183
|
+
ref: ref2,
|
|
3184
|
+
enabled: ref2 === enabledRef,
|
|
3185
|
+
configured: fact?.configured ?? null
|
|
3186
|
+
};
|
|
3187
|
+
if (fact?.masked !== void 0) view.masked = fact.masked;
|
|
3188
|
+
const stored = listed.find((candidate) => candidate.ref === ref2);
|
|
3189
|
+
if (stored?.alias !== void 0) view.alias = stored.alias;
|
|
3190
|
+
return view;
|
|
3191
|
+
});
|
|
3192
|
+
return { ok: true, route, enabledRef: enabledRef ?? null, entries };
|
|
3193
|
+
};
|
|
3194
|
+
if (req.method === "GET") {
|
|
3195
|
+
sendJson(res, 200, await payload());
|
|
3196
|
+
return;
|
|
3197
|
+
}
|
|
3198
|
+
const body = await readJsonBody(req);
|
|
3199
|
+
const op = isRecord(body) && typeof body["op"] === "string" ? body["op"] : "";
|
|
3200
|
+
const ref = isRecord(body) && typeof body["ref"] === "string" ? body["ref"] : void 0;
|
|
3201
|
+
const alias = isRecord(body) ? normalizeKeyAlias(body["alias"]) : void 0;
|
|
3202
|
+
const value = isRecord(body) && typeof body["value"] === "string" ? body["value"] : void 0;
|
|
3203
|
+
const refuse = (error) => sendJson(res, 400, { ok: false, error });
|
|
3128
3204
|
if (op === "add") {
|
|
3129
3205
|
if (!isLegalKeyValue(value)) {
|
|
3130
3206
|
refuse("a key must be printable ASCII without spaces");
|
|
@@ -3133,19 +3209,37 @@ function apply(ctx, config = {}) {
|
|
|
3133
3209
|
const taken = keyEntriesOf(readKeyIndex(indexFile), route).map((candidate) => candidate.ref);
|
|
3134
3210
|
const active = enabledNow();
|
|
3135
3211
|
if (active !== void 0 && !taken.includes(active)) taken.push(active);
|
|
3136
|
-
|
|
3212
|
+
let created = nextKeyRef(route, taken);
|
|
3213
|
+
for (; ; ) {
|
|
3214
|
+
const facts = await credentialFacts(credentials, [created]);
|
|
3215
|
+
const configured = facts[created]?.configured;
|
|
3216
|
+
if (configured === void 0) throw new HostReadError("the credential store could not confirm a free reference");
|
|
3217
|
+
if (!configured && await resolveCredential(credentials, created) === void 0) break;
|
|
3218
|
+
taken.push(created);
|
|
3219
|
+
created = nextKeyRef(route, taken);
|
|
3220
|
+
}
|
|
3137
3221
|
const refused = await storeCredential(credentials, created, value);
|
|
3138
3222
|
if (refused !== void 0) {
|
|
3139
3223
|
refuse(refused);
|
|
3140
3224
|
return;
|
|
3141
3225
|
}
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3226
|
+
let indexed = false;
|
|
3227
|
+
try {
|
|
3228
|
+
updateKeyIndex((document) => withKeyEntry(document, route, created, alias), indexFile);
|
|
3229
|
+
indexed = true;
|
|
3230
|
+
if (enabledNow() === void 0) {
|
|
3231
|
+
const failed = await setEnabledKey(created);
|
|
3232
|
+
if (failed !== void 0) throw new Error(failed);
|
|
3148
3233
|
}
|
|
3234
|
+
} catch (error) {
|
|
3235
|
+
const rollback = await forgetCredential(credentials, created);
|
|
3236
|
+
if (rollback === void 0 && indexed) {
|
|
3237
|
+
updateKeyIndex((document) => withoutKeyEntry(document, route, created), indexFile);
|
|
3238
|
+
}
|
|
3239
|
+
if (rollback !== void 0) {
|
|
3240
|
+
throw new Error(`${error instanceof Error ? error.message : String(error)}; credential rollback failed for ${created}: ${rollback}`);
|
|
3241
|
+
}
|
|
3242
|
+
throw error;
|
|
3149
3243
|
}
|
|
3150
3244
|
} else if (op === "enable") {
|
|
3151
3245
|
const listed = readKeyIndex(indexFile);
|
|
@@ -3198,32 +3292,56 @@ function apply(ctx, config = {}) {
|
|
|
3198
3292
|
refuse("that key is not listed for this provider");
|
|
3199
3293
|
return;
|
|
3200
3294
|
}
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3295
|
+
const configured = (await credentialConfigured(credentials, [ref]))[ref];
|
|
3296
|
+
if (configured === void 0) throw new HostReadError("the credential store could not read the reference");
|
|
3297
|
+
const wasEnabled = ref === enabledNow();
|
|
3298
|
+
const remaining = keyEntriesOf(document, route).map((candidate) => candidate.ref).filter((candidate) => candidate !== ref);
|
|
3299
|
+
const replacement = remaining[0];
|
|
3300
|
+
if (wasEnabled) {
|
|
3301
|
+
const failed = await setEnabledKey(replacement);
|
|
3204
3302
|
if (failed !== void 0) {
|
|
3205
3303
|
refuse(failed);
|
|
3206
3304
|
return;
|
|
3207
3305
|
}
|
|
3208
3306
|
}
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3307
|
+
try {
|
|
3308
|
+
updateKeyIndex((document2) => withoutKeyEntry(document2, route, ref), indexFile);
|
|
3309
|
+
if (configured) {
|
|
3310
|
+
const refused = await forgetCredential(credentials, ref);
|
|
3311
|
+
if (refused !== void 0) throw new Error(refused);
|
|
3312
|
+
}
|
|
3313
|
+
} catch (error) {
|
|
3314
|
+
const failures = [];
|
|
3315
|
+
try {
|
|
3316
|
+
updateKeyIndex((current) => {
|
|
3317
|
+
const providers = { ...current.providers };
|
|
3318
|
+
const original = document.providers[route];
|
|
3319
|
+
if (original === void 0) delete providers[route];
|
|
3320
|
+
else providers[route] = original;
|
|
3321
|
+
return { version: 1, providers };
|
|
3322
|
+
}, indexFile);
|
|
3323
|
+
} catch {
|
|
3324
|
+
failures.push("key index rollback failed");
|
|
3325
|
+
}
|
|
3326
|
+
if (wasEnabled && enabledNow() === replacement) {
|
|
3327
|
+
const failed = await setEnabledKey(ref);
|
|
3328
|
+
if (failed !== void 0) failures.push(`profile rollback failed: ${failed}`);
|
|
3215
3329
|
}
|
|
3330
|
+
throw new Error([error instanceof Error ? error.message : String(error), ...failures].join("; "));
|
|
3216
3331
|
}
|
|
3217
|
-
updateKeyIndex((document2) => withoutKeyEntry(document2, route, ref), indexFile);
|
|
3218
3332
|
} else {
|
|
3219
3333
|
refuse(`unknown key operation "${op}"`);
|
|
3220
3334
|
return;
|
|
3221
3335
|
}
|
|
3336
|
+
sendJson(res, 200, await payload());
|
|
3222
3337
|
} catch (error) {
|
|
3223
|
-
sendJson(res,
|
|
3224
|
-
|
|
3338
|
+
sendJson(res, error instanceof HostReadError ? 502 : 500, {
|
|
3339
|
+
ok: false,
|
|
3340
|
+
error: error instanceof Error ? error.message : String(error)
|
|
3341
|
+
});
|
|
3342
|
+
} finally {
|
|
3343
|
+
releaseMutation?.();
|
|
3225
3344
|
}
|
|
3226
|
-
sendJson(res, 200, await payload());
|
|
3227
3345
|
}
|
|
3228
3346
|
}),
|
|
3229
3347
|
"dsh-model-think-level: key index route"
|
|
@@ -3282,7 +3400,7 @@ function apply(ctx, config = {}) {
|
|
|
3282
3400
|
/** Same-origin differences for any configured request header. */
|
|
3283
3401
|
headerConflicts: index.headerConflicts,
|
|
3284
3402
|
/** What was found of the plugins that rewrite the same surface. */
|
|
3285
|
-
environment:
|
|
3403
|
+
environment: environmentConflicts
|
|
3286
3404
|
}
|
|
3287
3405
|
});
|
|
3288
3406
|
}
|