dsh-model-think-level 2.5.1 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/lib/client/image-config-client.d.ts +7 -22
- package/lib/client/injection/imagegen-entry.d.ts +4 -17
- package/lib/client.js +6 -5
- package/lib/client.js.map +3 -3
- package/lib/image/config.d.ts +11 -15
- package/lib/image/generate.d.ts +3 -3
- package/lib/image/state.d.ts +45 -0
- package/lib/image/tool.d.ts +1 -1
- package/lib/index.js +304 -123
- package/lib/index.js.map +4 -4
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -4506,8 +4506,12 @@ function detectHeaderConflicts() {
|
|
|
4506
4506
|
};
|
|
4507
4507
|
}
|
|
4508
4508
|
|
|
4509
|
-
// src/image/
|
|
4509
|
+
// src/image/state.ts
|
|
4510
|
+
import { createHash, randomUUID as randomUUID3 } from "node:crypto";
|
|
4510
4511
|
import { mkdirSync as mkdirSync6, readFileSync as readFileSync7, renameSync as renameSync6, unlinkSync as unlinkSync6, writeFileSync as writeFileSync6 } from "node:fs";
|
|
4512
|
+
import { dirname as dirname8 } from "node:path";
|
|
4513
|
+
|
|
4514
|
+
// src/image/config.ts
|
|
4511
4515
|
import { dirname as dirname7, join as join7 } from "node:path";
|
|
4512
4516
|
|
|
4513
4517
|
// src/image/sizes.ts
|
|
@@ -4549,7 +4553,7 @@ function stringList(value, limit = 32) {
|
|
|
4549
4553
|
const out = value.filter((entry) => typeof entry === "string" && entry.length > 0).slice(0, limit);
|
|
4550
4554
|
return out.length > 0 ? out : void 0;
|
|
4551
4555
|
}
|
|
4552
|
-
function
|
|
4556
|
+
function parseImageParams(raw) {
|
|
4553
4557
|
if (!isRecord2(raw)) return void 0;
|
|
4554
4558
|
const sizes = stringList(raw["sizes"])?.filter(isSize);
|
|
4555
4559
|
const qualities = stringList(raw["qualities"]);
|
|
@@ -4604,7 +4608,7 @@ function parseImageModel(raw) {
|
|
|
4604
4608
|
const api = raw["api"] === "images" ? "images" : "none";
|
|
4605
4609
|
const endpointRaw = isRecord2(raw["endpoint"]) ? raw["endpoint"] : {};
|
|
4606
4610
|
const status = endpointRaw["status"] === "ok" || endpointRaw["status"] === "unsupported" ? endpointRaw["status"] : "unverified";
|
|
4607
|
-
const params =
|
|
4611
|
+
const params = parseImageParams(raw["params"]) ?? { ...IMAGE_FALLBACK_PARAMS };
|
|
4608
4612
|
return {
|
|
4609
4613
|
id,
|
|
4610
4614
|
route,
|
|
@@ -4620,37 +4624,9 @@ function parseImageModel(raw) {
|
|
|
4620
4624
|
checkedAt: typeof endpointRaw["checkedAt"] === "string" ? endpointRaw["checkedAt"] : null
|
|
4621
4625
|
},
|
|
4622
4626
|
params,
|
|
4623
|
-
lastGoodParams:
|
|
4627
|
+
lastGoodParams: parseImageParams(raw["lastGoodParams"]) ?? null
|
|
4624
4628
|
};
|
|
4625
4629
|
}
|
|
4626
|
-
function readImageConfig(file = imageConfigFilePath()) {
|
|
4627
|
-
let text;
|
|
4628
|
-
try {
|
|
4629
|
-
text = readFileSync7(file, "utf8");
|
|
4630
|
-
} catch {
|
|
4631
|
-
return emptyImageConfig();
|
|
4632
|
-
}
|
|
4633
|
-
try {
|
|
4634
|
-
return parseImageConfig(JSON.parse(text));
|
|
4635
|
-
} catch {
|
|
4636
|
-
return emptyImageConfig();
|
|
4637
|
-
}
|
|
4638
|
-
}
|
|
4639
|
-
function writeImageConfig(document, file = imageConfigFilePath()) {
|
|
4640
|
-
mkdirSync6(dirname7(file), { recursive: true });
|
|
4641
|
-
const temporary = `${file}.${process.pid}.tmp`;
|
|
4642
|
-
writeFileSync6(temporary, `${JSON.stringify(document, null, 2)}
|
|
4643
|
-
`, "utf8");
|
|
4644
|
-
try {
|
|
4645
|
-
renameSync6(temporary, file);
|
|
4646
|
-
} catch (error) {
|
|
4647
|
-
try {
|
|
4648
|
-
unlinkSync6(temporary);
|
|
4649
|
-
} catch {
|
|
4650
|
-
}
|
|
4651
|
-
throw error;
|
|
4652
|
-
}
|
|
4653
|
-
}
|
|
4654
4630
|
function deriveImageSeeds(providers) {
|
|
4655
4631
|
const seeds = [];
|
|
4656
4632
|
for (const [route, profile] of Object.entries(providers)) {
|
|
@@ -4680,28 +4656,6 @@ function deriveImageSeeds(providers) {
|
|
|
4680
4656
|
}
|
|
4681
4657
|
return seeds;
|
|
4682
4658
|
}
|
|
4683
|
-
function syncImageConfig(stored, providers, now = () => /* @__PURE__ */ new Date()) {
|
|
4684
|
-
const seeds = deriveImageSeeds(providers);
|
|
4685
|
-
const previous = new Map(stored.models.map((model) => [model.id, model]));
|
|
4686
|
-
const models = seeds.map((seed) => {
|
|
4687
|
-
const kept = previous.get(seed.id);
|
|
4688
|
-
const params = kept !== void 0 ? kept.params : { ...IMAGE_FALLBACK_PARAMS };
|
|
4689
|
-
return {
|
|
4690
|
-
...seed,
|
|
4691
|
-
endpoint: kept?.endpoint ?? { kind: "images", status: "unverified", checkedAt: null },
|
|
4692
|
-
params,
|
|
4693
|
-
lastGoodParams: kept?.lastGoodParams ?? null
|
|
4694
|
-
};
|
|
4695
|
-
});
|
|
4696
|
-
const ids = new Set(models.map((model) => model.id));
|
|
4697
|
-
const keepDefault = stored.defaultModel !== null && (models.length === 0 || ids.has(stored.defaultModel));
|
|
4698
|
-
return {
|
|
4699
|
-
...stored,
|
|
4700
|
-
syncedAt: now().toISOString(),
|
|
4701
|
-
defaultModel: keepDefault ? stored.defaultModel : null,
|
|
4702
|
-
models
|
|
4703
|
-
};
|
|
4704
|
-
}
|
|
4705
4659
|
function resolveImageModel(config, explicit) {
|
|
4706
4660
|
if (explicit !== void 0 && explicit.length > 0) {
|
|
4707
4661
|
const direct = config.models.find((model) => model.id === explicit);
|
|
@@ -4726,15 +4680,187 @@ function effectiveParams(config, model, request) {
|
|
|
4726
4680
|
n: model === void 0 ? n : Math.min(n, model.params.maxN)
|
|
4727
4681
|
};
|
|
4728
4682
|
}
|
|
4729
|
-
|
|
4730
|
-
|
|
4731
|
-
|
|
4732
|
-
|
|
4683
|
+
|
|
4684
|
+
// src/image/state.ts
|
|
4685
|
+
var ImageConfigurationError = class extends Error {
|
|
4686
|
+
constructor(code, message) {
|
|
4687
|
+
super(message);
|
|
4688
|
+
this.code = code;
|
|
4689
|
+
this.name = "ImageConfigurationError";
|
|
4690
|
+
}
|
|
4691
|
+
code;
|
|
4692
|
+
};
|
|
4693
|
+
function isRecord3(value) {
|
|
4694
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4695
|
+
}
|
|
4696
|
+
function sourceOf(model) {
|
|
4697
|
+
return createHash("sha256").update(JSON.stringify([
|
|
4698
|
+
model.route,
|
|
4699
|
+
model.modelId,
|
|
4700
|
+
model.api,
|
|
4701
|
+
model.baseURL,
|
|
4702
|
+
model.keyRef ?? null
|
|
4703
|
+
])).digest("hex");
|
|
4704
|
+
}
|
|
4705
|
+
function preferencesOf(raw) {
|
|
4706
|
+
const parsed = parseImageConfig(raw);
|
|
4707
|
+
const observations = /* @__PURE__ */ Object.create(null);
|
|
4708
|
+
if (raw["version"] === 3) {
|
|
4709
|
+
if (isRecord3(raw["observations"])) {
|
|
4710
|
+
for (const [id, value] of Object.entries(raw["observations"])) {
|
|
4711
|
+
if (!isRecord3(value) || typeof value["source"] !== "string" || !/^[a-f0-9]{64}$/.test(value["source"])) continue;
|
|
4712
|
+
const endpoint = isRecord3(value["endpoint"]) ? value["endpoint"] : {};
|
|
4713
|
+
observations[id] = {
|
|
4714
|
+
source: value["source"],
|
|
4715
|
+
endpoint: {
|
|
4716
|
+
kind: "images",
|
|
4717
|
+
status: endpoint["status"] === "ok" || endpoint["status"] === "unsupported" ? endpoint["status"] : "unverified",
|
|
4718
|
+
checkedAt: typeof endpoint["checkedAt"] === "string" ? endpoint["checkedAt"] : null
|
|
4719
|
+
},
|
|
4720
|
+
lastGoodParams: parseImageParams(value["lastGoodParams"]) ?? null
|
|
4721
|
+
};
|
|
4722
|
+
}
|
|
4723
|
+
}
|
|
4724
|
+
} else {
|
|
4725
|
+
for (const model of parsed.models) {
|
|
4726
|
+
observations[model.id] = {
|
|
4727
|
+
source: sourceOf(model),
|
|
4728
|
+
endpoint: model.endpoint,
|
|
4729
|
+
lastGoodParams: model.lastGoodParams
|
|
4730
|
+
};
|
|
4731
|
+
}
|
|
4732
|
+
}
|
|
4733
|
+
return {
|
|
4734
|
+
version: 3,
|
|
4735
|
+
enabled: parsed.enabled,
|
|
4736
|
+
defaultModel: parsed.defaultModel,
|
|
4737
|
+
defaults: parsed.defaults,
|
|
4738
|
+
outputDir: parsed.outputDir,
|
|
4739
|
+
observations
|
|
4740
|
+
};
|
|
4733
4741
|
}
|
|
4742
|
+
var ImageConfiguration = class {
|
|
4743
|
+
constructor(deps) {
|
|
4744
|
+
this.deps = deps;
|
|
4745
|
+
this.file = deps.file ?? imageConfigFilePath();
|
|
4746
|
+
this.now = deps.now ?? (() => /* @__PURE__ */ new Date());
|
|
4747
|
+
}
|
|
4748
|
+
deps;
|
|
4749
|
+
file;
|
|
4750
|
+
now;
|
|
4751
|
+
load() {
|
|
4752
|
+
let text;
|
|
4753
|
+
try {
|
|
4754
|
+
text = readFileSync7(this.file, "utf8");
|
|
4755
|
+
} catch (error) {
|
|
4756
|
+
if (error.code === "ENOENT") return { document: preferencesOf({}) };
|
|
4757
|
+
throw new ImageConfigurationError("preferences-unreadable", "\u751F\u56FE\u914D\u7F6E\u504F\u597D\u6587\u4EF6\u4E0D\u53EF\u8BFB\uFF0C\u8BF7\u68C0\u67E5\u6587\u4EF6\u6743\u9650\u6216\u5B58\u50A8\u72B6\u6001");
|
|
4758
|
+
}
|
|
4759
|
+
let raw;
|
|
4760
|
+
try {
|
|
4761
|
+
raw = JSON.parse(text);
|
|
4762
|
+
} catch {
|
|
4763
|
+
throw new ImageConfigurationError("preferences-unreadable", "\u751F\u56FE\u914D\u7F6E\u504F\u597D\u6587\u4EF6\u683C\u5F0F\u635F\u574F\uFF0C\u539F\u6587\u4EF6\u5DF2\u4FDD\u7559\uFF0C\u8BF7\u4FEE\u590D\u540E\u91CD\u8BD5");
|
|
4764
|
+
}
|
|
4765
|
+
if (!isRecord3(raw) || raw["version"] !== void 0 && raw["version"] !== 1 && raw["version"] !== 2 && raw["version"] !== 3) {
|
|
4766
|
+
throw new ImageConfigurationError("preferences-unreadable", "\u751F\u56FE\u914D\u7F6E\u504F\u597D\u6587\u4EF6\u7248\u672C\u6216\u7ED3\u6784\u65E0\u6548\uFF0C\u539F\u6587\u4EF6\u5DF2\u4FDD\u7559");
|
|
4767
|
+
}
|
|
4768
|
+
return { document: preferencesOf(raw), ...raw["version"] === 3 ? {} : { legacyText: text } };
|
|
4769
|
+
}
|
|
4770
|
+
preferences() {
|
|
4771
|
+
return this.load().document;
|
|
4772
|
+
}
|
|
4773
|
+
base(preferences) {
|
|
4774
|
+
return {
|
|
4775
|
+
...emptyImageConfig(this.now),
|
|
4776
|
+
enabled: preferences.enabled,
|
|
4777
|
+
defaultModel: preferences.defaultModel,
|
|
4778
|
+
defaults: preferences.defaults,
|
|
4779
|
+
outputDir: preferences.outputDir
|
|
4780
|
+
};
|
|
4781
|
+
}
|
|
4782
|
+
read() {
|
|
4783
|
+
return this.project(this.preferences());
|
|
4784
|
+
}
|
|
4785
|
+
project(preferences) {
|
|
4786
|
+
let descriptor;
|
|
4787
|
+
try {
|
|
4788
|
+
descriptor = this.deps.describe().find((entry) => entry.ns === "llm-pi-ai");
|
|
4789
|
+
} catch {
|
|
4790
|
+
throw new ImageConfigurationError("settings-unavailable", "\u751F\u56FE\u914D\u7F6E\u6682\u4E0D\u53EF\u7528\uFF1A\u5BBF\u4E3B\u6A21\u578B\u8BBE\u7F6E\u8BFB\u53D6\u5931\u8D25\uFF0C\u8BF7\u6062\u590D\u8BBE\u7F6E\u670D\u52A1\u540E\u91CD\u8BD5");
|
|
4791
|
+
}
|
|
4792
|
+
if (descriptor === void 0) {
|
|
4793
|
+
throw new ImageConfigurationError("settings-unavailable", "\u751F\u56FE\u914D\u7F6E\u5C1A\u672A\u5C31\u7EEA\uFF1Allm-pi-ai \u5C1A\u672A\u8FD0\u884C\u6216\u6B63\u5728\u91CD\u8F7D\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5");
|
|
4794
|
+
}
|
|
4795
|
+
const section = descriptor.value;
|
|
4796
|
+
if (!isRecord3(section) || section["providers"] !== void 0 && !isRecord3(section["providers"])) {
|
|
4797
|
+
throw new ImageConfigurationError("settings-invalid", "\u751F\u56FE\u914D\u7F6E\u65E0\u6548\uFF1A\u5BBF\u4E3B\u6A21\u578B\u8BBE\u7F6E\u7ED3\u6784\u5F02\u5E38\uFF0C\u8BF7\u68C0\u67E5 llm-pi-ai \u914D\u7F6E");
|
|
4798
|
+
}
|
|
4799
|
+
const providers = section["providers"] ?? {};
|
|
4800
|
+
const models = deriveImageSeeds(providers).map((seed) => {
|
|
4801
|
+
const recorded = preferences.observations[seed.id];
|
|
4802
|
+
const observation = recorded?.source === sourceOf(seed) ? recorded : void 0;
|
|
4803
|
+
return {
|
|
4804
|
+
...seed,
|
|
4805
|
+
params: { ...IMAGE_FALLBACK_PARAMS },
|
|
4806
|
+
endpoint: observation?.endpoint ?? { kind: "images", status: "unverified", checkedAt: null },
|
|
4807
|
+
lastGoodParams: observation?.lastGoodParams ?? null
|
|
4808
|
+
};
|
|
4809
|
+
});
|
|
4810
|
+
return { ...this.base(preferences), models };
|
|
4811
|
+
}
|
|
4812
|
+
/** The saved switch wins even while the model provider is unavailable. */
|
|
4813
|
+
forGeneration() {
|
|
4814
|
+
const preferences = this.preferences();
|
|
4815
|
+
return preferences.enabled ? this.project(preferences) : this.base(preferences);
|
|
4816
|
+
}
|
|
4817
|
+
update(mutate) {
|
|
4818
|
+
const current = this.load();
|
|
4819
|
+
const next = preferencesOf(mutate(current.document));
|
|
4820
|
+
mkdirSync6(dirname8(this.file), { recursive: true });
|
|
4821
|
+
if (current.legacyText !== void 0) {
|
|
4822
|
+
try {
|
|
4823
|
+
writeFileSync6(`${this.file}.v2.bak`, current.legacyText, { encoding: "utf8", flag: "wx", mode: 384 });
|
|
4824
|
+
} catch (error) {
|
|
4825
|
+
if (error.code !== "EEXIST") throw error;
|
|
4826
|
+
}
|
|
4827
|
+
}
|
|
4828
|
+
const temporary = `${this.file}.${process.pid}.${randomUUID3()}.tmp`;
|
|
4829
|
+
try {
|
|
4830
|
+
writeFileSync6(temporary, `${JSON.stringify(next, null, 2)}
|
|
4831
|
+
`, { encoding: "utf8", flag: "wx", mode: 384 });
|
|
4832
|
+
renameSync6(temporary, this.file);
|
|
4833
|
+
} catch (error) {
|
|
4834
|
+
try {
|
|
4835
|
+
unlinkSync6(temporary);
|
|
4836
|
+
} catch {
|
|
4837
|
+
}
|
|
4838
|
+
throw error;
|
|
4839
|
+
}
|
|
4840
|
+
return next;
|
|
4841
|
+
}
|
|
4842
|
+
record(model, params) {
|
|
4843
|
+
this.update((document) => ({
|
|
4844
|
+
...document,
|
|
4845
|
+
observations: {
|
|
4846
|
+
...document.observations,
|
|
4847
|
+
[model.id]: {
|
|
4848
|
+
source: sourceOf(model),
|
|
4849
|
+
endpoint: {
|
|
4850
|
+
kind: "images",
|
|
4851
|
+
status: params === null ? "unsupported" : "ok",
|
|
4852
|
+
checkedAt: this.now().toISOString()
|
|
4853
|
+
},
|
|
4854
|
+
lastGoodParams: params
|
|
4855
|
+
}
|
|
4856
|
+
}
|
|
4857
|
+
}));
|
|
4858
|
+
}
|
|
4859
|
+
};
|
|
4734
4860
|
|
|
4735
4861
|
// src/image/generate.ts
|
|
4736
4862
|
import { mkdirSync as mkdirSync7, readFileSync as readFileSync8, writeFileSync as writeFileSync7, existsSync as existsSync2, statSync } from "node:fs";
|
|
4737
|
-
import { dirname as
|
|
4863
|
+
import { dirname as dirname9, join as join8, resolve as resolve3 } from "node:path";
|
|
4738
4864
|
|
|
4739
4865
|
// src/image/deadline.ts
|
|
4740
4866
|
function createImageDeadline(timeoutMs, caller, errorFor) {
|
|
@@ -4999,7 +5125,7 @@ function resolveDestination(requested, fallbackDir, slug, now) {
|
|
|
4999
5125
|
mkdirSync7(target, { recursive: true });
|
|
5000
5126
|
return destinationOf(target, slug, now);
|
|
5001
5127
|
}
|
|
5002
|
-
mkdirSync7(
|
|
5128
|
+
mkdirSync7(dirname9(target), { recursive: true });
|
|
5003
5129
|
if (!existsSync2(target)) return target;
|
|
5004
5130
|
for (let index = 2; ; index += 1) {
|
|
5005
5131
|
const candidate = target.replace(/\.png$/i, `-${index}.png`);
|
|
@@ -5049,7 +5175,7 @@ function createGenerateDeadline(timeoutMs, signal) {
|
|
|
5049
5175
|
return createImageDeadline(timeoutMs, signal, (kind) => new GenerateError(kind, kind === "aborted" ? "\u751F\u6210\u5DF2\u53D6\u6D88" : `\u751F\u6210\u8D85\u65F6\uFF08${timeoutMs}ms\uFF09\u2014\u2014\u53EF\u5728\u914D\u7F6E\u9875\u8C03\u5927\u8D85\u65F6`));
|
|
5050
5176
|
}
|
|
5051
5177
|
async function generateImage(deps, request, sharedDeadline) {
|
|
5052
|
-
const config = deps.config
|
|
5178
|
+
const config = deps.config();
|
|
5053
5179
|
const deadline = sharedDeadline ?? createGenerateDeadline(deps.timeoutMs ?? config.defaults.timeoutMs, request.signal);
|
|
5054
5180
|
try {
|
|
5055
5181
|
deadline.check();
|
|
@@ -5170,14 +5296,14 @@ async function generateWithinDeadline(deps, request, config, deadline) {
|
|
|
5170
5296
|
images.push({ filePath, data });
|
|
5171
5297
|
}
|
|
5172
5298
|
deadline.check();
|
|
5173
|
-
if (
|
|
5299
|
+
if (deps.persistLastGood !== void 0) {
|
|
5174
5300
|
deps.persistLastGood(model.id, {
|
|
5175
5301
|
...model.params,
|
|
5176
5302
|
responseFormat: format,
|
|
5177
5303
|
sizes: model.params.sizes,
|
|
5178
5304
|
qualities: model.params.qualities,
|
|
5179
5305
|
maxN: model.params.maxN
|
|
5180
|
-
});
|
|
5306
|
+
}, model);
|
|
5181
5307
|
}
|
|
5182
5308
|
deadline.check();
|
|
5183
5309
|
return {
|
|
@@ -5198,7 +5324,7 @@ async function generateWithinDeadline(deps, request, config, deadline) {
|
|
|
5198
5324
|
}
|
|
5199
5325
|
const combined = sanitize([upstreamMessage(body), bodyText].filter((part) => typeof part === "string" && part.length > 0).join(" "), key);
|
|
5200
5326
|
lastFailure = classifyFailure2(response.status, combined);
|
|
5201
|
-
if (lastFailure.kind === "endpoint") deps.persistLastGood?.(model.id, null);
|
|
5327
|
+
if (lastFailure.kind === "endpoint") deps.persistLastGood?.(model.id, null, model);
|
|
5202
5328
|
const revision = fallbackRevisionFor(response.status, bodyText);
|
|
5203
5329
|
if (revision !== void 0 && revisions.length < 4) {
|
|
5204
5330
|
if (revision === "removed response_format" || revision === "removed output_format") {
|
|
@@ -5331,14 +5457,16 @@ function registerImagegenCommand(commands, deps) {
|
|
|
5331
5457
|
description: "\u7528\u751F\u56FE\u6A21\u578B\u751F\u6210\u56FE\u7247",
|
|
5332
5458
|
input: { hint: IMAGEGEN_HINT, attachments: true },
|
|
5333
5459
|
handler: async (invocation) => {
|
|
5334
|
-
const known = deps.modelIds();
|
|
5335
5460
|
let parsed;
|
|
5461
|
+
let generationDeps;
|
|
5336
5462
|
try {
|
|
5337
|
-
|
|
5463
|
+
const config = deps.generateDeps.config();
|
|
5464
|
+
generationDeps = { ...deps.generateDeps, config: () => config };
|
|
5465
|
+
parsed = parseImagegenArgs(invocationTextOf(invocation), config.models.map((model) => model.id));
|
|
5338
5466
|
} catch (error) {
|
|
5339
5467
|
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
5340
5468
|
}
|
|
5341
|
-
const answer = await executeImagegen(deps, parsed);
|
|
5469
|
+
const answer = await executeImagegen({ ...deps, generateDeps: generationDeps }, parsed);
|
|
5342
5470
|
if (answer.ok) {
|
|
5343
5471
|
try {
|
|
5344
5472
|
deps.echo?.(invocation, answer.text, answer.filePaths[0] ?? "");
|
|
@@ -5490,7 +5618,7 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
5490
5618
|
async execute(rawArgs, exec) {
|
|
5491
5619
|
const parsed = parseArgs(rawArgs);
|
|
5492
5620
|
if (typeof parsed === "string") throw new Error(parsed);
|
|
5493
|
-
const config = deps.generateDeps.config
|
|
5621
|
+
const config = deps.generateDeps.config();
|
|
5494
5622
|
if (!config.enabled) throw new Error("\u751F\u56FE\u670D\u52A1\u5DF2\u5173\u95ED\u2014\u2014\u53EF\u5728\u300C\u9AD8\u7EA7\u8BBE\u7F6E \u2192 \u751F\u56FE\u300D\u91CC\u91CD\u65B0\u5F00\u542F");
|
|
5495
5623
|
const deadline = createGenerateDeadline(deps.generateDeps.timeoutMs ?? config.defaults.timeoutMs, exec.signal);
|
|
5496
5624
|
try {
|
|
@@ -5580,12 +5708,12 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
5580
5708
|
}
|
|
5581
5709
|
};
|
|
5582
5710
|
}
|
|
5583
|
-
function imageToolHint(config) {
|
|
5711
|
+
function imageToolHint(config, toolName = IMAGE_TOOL_NAME) {
|
|
5584
5712
|
if (!config.enabled) return "";
|
|
5585
|
-
if (config.models.length === 0) return "";
|
|
5713
|
+
if (config.models.length === 0) return "\u751F\u56FE\u914D\u7F6E\u5F53\u524D\u6CA1\u6709\u6807\u8BB0\u4E3A\u300C\u751F\u56FE\u300D\u7684\u6A21\u578B\uFF1B\u8BF7\u5230\u300C\u6A21\u578B\u914D\u7F6E \u2192 \u8F93\u5165\u6A21\u6001\u300D\u52FE\u9009\u751F\u56FE\u540E\u91CD\u8BD5\u3002\u8BF7\u660E\u786E\u62A5\u544A\u8FD9\u4E2A\u914D\u7F6E\u72B6\u6001\uFF0C\u4E0D\u8981\u628A\u5B83\u89E3\u91CA\u4E3A\u5DE5\u5177\u672A\u52A0\u8F7D\u3002";
|
|
5586
5714
|
const defaults = `size ${config.defaults.size}\uFF0Cquality ${config.defaults.quality}\uFF0Cn ${config.defaults.n}`;
|
|
5587
5715
|
const resolved = resolveImageModel(config);
|
|
5588
|
-
const opening = resolved === void 0 ? `\u751F\u56FE\u8BF7\u8C03\u7528
|
|
5716
|
+
const opening = resolved === void 0 ? `\u751F\u56FE\u8BF7\u8C03\u7528 ${toolName} \u5DE5\u5177\u3002\u5F53\u524D\u6CA1\u6709\u552F\u4E00\u7684\u9ED8\u8BA4\u751F\u56FE\u6A21\u578B\uFF08\u5171 ${config.models.length} \u4E2A\u5019\u9009\uFF09\uFF0C\u8C03\u7528\u65F6\u5FC5\u987B\u663E\u5F0F\u586B model\uFF0C\u53EF\u9009\u503C\uFF1A${config.models.map((model) => model.id).join("\u3001")}\uFF1Bsize/quality/n \u53EF\u7F3A\u7701\uFF08\u9ED8\u8BA4 ${defaults}\uFF09\u3002` : `\u751F\u56FE\u8BF7\u8C03\u7528 ${toolName} \u5DE5\u5177\uFF08\u7F3A\u7701\u53C2\u6570\u5373\u53EF\uFF1A\u9ED8\u8BA4\u6A21\u578B ${resolved.id}\uFF0C${defaults}\uFF09\u3002`;
|
|
5589
5717
|
const lines = [
|
|
5590
5718
|
opening,
|
|
5591
5719
|
resolved === void 0 ? "\u7528\u6237\u6CA1\u6709\u70B9\u540D\u6A21\u578B\u65F6\uFF0C\u5148\u95EE\u6E05\u7528\u54EA\u4E2A\u6A21\u578B\uFF0C\u4E0D\u8981\u81EA\u5DF1\u6311\u4E00\u4E2A\u2014\u2014\u731C\u9519\u7684\u6A21\u578B\u4F1A\u76F4\u63A5\u751F\u6210\u5931\u8D25\u3002\u6539\u56FE\u7528\u5BF9\u8BDD\u5185\u56FE\u7247\u7684\u9644\u4EF6 ID \u586B reference_attachments\uFF0C\u672C\u5730\u6587\u4EF6\u586B reference_images\u3002" : "\u7528\u6237\u6CA1\u6709\u70B9\u540D\u6A21\u578B\u6216\u53C2\u6570\u65F6\uFF0C\u4E0D\u8981\u586B model/size/quality/n\uFF1B\u6539\u56FE\u7528\u5BF9\u8BDD\u5185\u56FE\u7247\u7684\u9644\u4EF6 ID \u586B reference_attachments\uFF0C\u672C\u5730\u6587\u4EF6\u586B reference_images\u3002",
|
|
@@ -5597,7 +5725,7 @@ function imageToolHint(config) {
|
|
|
5597
5725
|
|
|
5598
5726
|
// src/image/legacy-cleanup.ts
|
|
5599
5727
|
import { copyFileSync, existsSync as existsSync3, mkdirSync as mkdirSync8, readFileSync as readFileSync9, readdirSync as readdirSync2, statSync as statSync2, writeFileSync as writeFileSync8 } from "node:fs";
|
|
5600
|
-
import { dirname as
|
|
5728
|
+
import { dirname as dirname10, join as join9 } from "node:path";
|
|
5601
5729
|
var MARK_BEGIN = `# >>> ${IMAGE_MCP_PATCH_MARK}:begin`;
|
|
5602
5730
|
var MARK_END = `# <<< ${IMAGE_MCP_PATCH_MARK}:end`;
|
|
5603
5731
|
function resolveLegacyPatchFile(home = harnessHome()) {
|
|
@@ -5693,7 +5821,7 @@ function spliceOnce(patchFile, mtimeMs) {
|
|
|
5693
5821
|
} catch {
|
|
5694
5822
|
}
|
|
5695
5823
|
}
|
|
5696
|
-
mkdirSync8(
|
|
5824
|
+
mkdirSync8(dirname10(patchFile), { recursive: true });
|
|
5697
5825
|
writeFileSync8(patchFile, next, "utf8");
|
|
5698
5826
|
const written = readFileSync9(patchFile, "utf8");
|
|
5699
5827
|
if (written !== next) throw new Error("the patch file did not hold the written bytes after the write");
|
|
@@ -5882,7 +6010,7 @@ function backfillRolePin(model, providers, route) {
|
|
|
5882
6010
|
// src/fingerprint/bank.ts
|
|
5883
6011
|
import { existsSync as existsSync4, readFileSync as readFileSync10 } from "node:fs";
|
|
5884
6012
|
import { fileURLToPath } from "node:url";
|
|
5885
|
-
import { dirname as
|
|
6013
|
+
import { dirname as dirname11, join as join10 } from "node:path";
|
|
5886
6014
|
|
|
5887
6015
|
// src/fingerprint/core.ts
|
|
5888
6016
|
var VALUE_MIN = 1;
|
|
@@ -6167,10 +6295,10 @@ function parseBank(raw) {
|
|
|
6167
6295
|
}
|
|
6168
6296
|
function bankCandidates() {
|
|
6169
6297
|
const candidates = [];
|
|
6170
|
-
let dir =
|
|
6298
|
+
let dir = dirname11(fileURLToPath(import.meta.url));
|
|
6171
6299
|
for (let depth = 0; depth < 5; depth += 1) {
|
|
6172
6300
|
candidates.push(join10(dir, "assets", "unified_bank.json"));
|
|
6173
|
-
const parent =
|
|
6301
|
+
const parent = dirname11(dir);
|
|
6174
6302
|
if (parent === dir) break;
|
|
6175
6303
|
dir = parent;
|
|
6176
6304
|
}
|
|
@@ -6457,7 +6585,7 @@ async function runChallenge(options) {
|
|
|
6457
6585
|
|
|
6458
6586
|
// src/fingerprint/store.ts
|
|
6459
6587
|
import { mkdirSync as mkdirSync9, readFileSync as readFileSync11, renameSync as renameSync7, unlinkSync as unlinkSync7, writeFileSync as writeFileSync9 } from "node:fs";
|
|
6460
|
-
import { dirname as
|
|
6588
|
+
import { dirname as dirname12, join as join11 } from "node:path";
|
|
6461
6589
|
var STATUSES = /* @__PURE__ */ new Set(["identified", "doubtful", "failed"]);
|
|
6462
6590
|
function emptyFingerprintStore(bankBuiltAt2 = "") {
|
|
6463
6591
|
return { version: 1, bankBuiltAt: bankBuiltAt2, records: {} };
|
|
@@ -6600,7 +6728,7 @@ function readFingerprintStore(bankBuiltAt2 = "", file = fingerprintFilePath()) {
|
|
|
6600
6728
|
return parseFingerprintStore(raw, bankBuiltAt2);
|
|
6601
6729
|
}
|
|
6602
6730
|
function writeFingerprintStore(document, file = fingerprintFilePath()) {
|
|
6603
|
-
mkdirSync9(
|
|
6731
|
+
mkdirSync9(dirname12(file), { recursive: true });
|
|
6604
6732
|
const temporary = `${file}.${process.pid}.tmp`;
|
|
6605
6733
|
writeFileSync9(temporary, `${JSON.stringify(document, null, 2)}
|
|
6606
6734
|
`, "utf8");
|
|
@@ -7125,14 +7253,22 @@ function apply(ctx, config = {}) {
|
|
|
7125
7253
|
const settings = ctx.settings;
|
|
7126
7254
|
let piDescriptorCache;
|
|
7127
7255
|
let piDescriptorCached = false;
|
|
7128
|
-
|
|
7129
|
-
|
|
7256
|
+
let piDescriptorReading = false;
|
|
7257
|
+
const piDescriptor = (required = false, fresh = false) => {
|
|
7258
|
+
if (piDescriptorReading) {
|
|
7259
|
+
if (required) throw new HostReadError("the llm-pi-ai settings are being read");
|
|
7260
|
+
return void 0;
|
|
7261
|
+
}
|
|
7262
|
+
if (piDescriptorCached && !fresh) return piDescriptorCache;
|
|
7130
7263
|
let descriptor;
|
|
7131
7264
|
try {
|
|
7265
|
+
piDescriptorReading = true;
|
|
7132
7266
|
descriptor = settings.describe().find((entry) => entry.ns === PI_NS);
|
|
7133
7267
|
} catch {
|
|
7134
7268
|
if (required) throw new HostReadError("the llm-pi-ai settings could not be read");
|
|
7135
7269
|
return void 0;
|
|
7270
|
+
} finally {
|
|
7271
|
+
piDescriptorReading = false;
|
|
7136
7272
|
}
|
|
7137
7273
|
if (descriptor === void 0 || !isRecord(descriptor.value)) {
|
|
7138
7274
|
if (required) throw new HostReadError("the llm-pi-ai settings entry is unavailable");
|
|
@@ -7517,18 +7653,33 @@ function apply(ctx, config = {}) {
|
|
|
7517
7653
|
refreshHeaders();
|
|
7518
7654
|
refreshKeyScopes();
|
|
7519
7655
|
ctx.effect(() => () => stopHeaderRetries(), "dsh-model-think-level: header retries");
|
|
7656
|
+
const refreshModelConsumers = () => {
|
|
7657
|
+
refreshHeaders();
|
|
7658
|
+
refreshKeyScopes();
|
|
7659
|
+
const source = catalogueSource();
|
|
7660
|
+
if (source !== catalogueSourceStamp) {
|
|
7661
|
+
catalogueSourceStamp = source;
|
|
7662
|
+
invalidateListings();
|
|
7663
|
+
}
|
|
7664
|
+
scheduleKeyProbes();
|
|
7665
|
+
};
|
|
7666
|
+
let modelConsumerRefreshQueued = false;
|
|
7667
|
+
let modelConsumersDisposed = false;
|
|
7668
|
+
ctx.effect(() => () => {
|
|
7669
|
+
modelConsumersDisposed = true;
|
|
7670
|
+
});
|
|
7520
7671
|
ctx.on("settings/document-updated", (ns) => {
|
|
7521
|
-
if (ns
|
|
7522
|
-
|
|
7523
|
-
|
|
7524
|
-
|
|
7525
|
-
const source = catalogueSource();
|
|
7526
|
-
if (source !== catalogueSourceStamp) {
|
|
7527
|
-
catalogueSourceStamp = source;
|
|
7528
|
-
invalidateListings();
|
|
7529
|
-
}
|
|
7530
|
-
scheduleKeyProbes();
|
|
7672
|
+
if (ns !== PI_NS) return;
|
|
7673
|
+
if (!piDescriptorReading) {
|
|
7674
|
+
refreshModelConsumers();
|
|
7675
|
+
return;
|
|
7531
7676
|
}
|
|
7677
|
+
if (modelConsumerRefreshQueued) return;
|
|
7678
|
+
modelConsumerRefreshQueued = true;
|
|
7679
|
+
queueMicrotask(() => {
|
|
7680
|
+
modelConsumerRefreshQueued = false;
|
|
7681
|
+
if (!modelConsumersDisposed) refreshModelConsumers();
|
|
7682
|
+
});
|
|
7532
7683
|
});
|
|
7533
7684
|
overlaySource.keys = async ({ route, model }) => {
|
|
7534
7685
|
const scopes = keyScopes.current[route] ?? [];
|
|
@@ -7651,18 +7802,14 @@ function apply(ctx, config = {}) {
|
|
|
7651
7802
|
} catch (error) {
|
|
7652
7803
|
console.warn(`[dsh-model-think-level] \u65E7\u7248\u751F\u56FE MCP \u8865\u4E01\u6761\u76EE\u81EA\u52A8\u79FB\u9664\u5931\u8D25\uFF08\u53EF\u624B\u52A8\u5220\u9664\u6807\u8BB0\u5757\uFF09\uFF1A${error instanceof Error ? error.message : String(error)}`);
|
|
7653
7804
|
}
|
|
7654
|
-
const
|
|
7655
|
-
|
|
7656
|
-
|
|
7657
|
-
|
|
7658
|
-
if (Object.keys(providers).length === 0) return;
|
|
7659
|
-
updateImageConfig((document) => syncImageConfig(document, providers));
|
|
7660
|
-
} catch {
|
|
7805
|
+
const imageConfiguration = new ImageConfiguration({
|
|
7806
|
+
describe: () => {
|
|
7807
|
+
const descriptor = piDescriptor(true, true);
|
|
7808
|
+
return descriptor === void 0 ? [] : [descriptor];
|
|
7661
7809
|
}
|
|
7662
|
-
};
|
|
7663
|
-
syncImageConfigNow();
|
|
7810
|
+
});
|
|
7664
7811
|
const imageGenerateDeps = {
|
|
7665
|
-
config: () =>
|
|
7812
|
+
config: () => imageConfiguration.forGeneration(),
|
|
7666
7813
|
resolveKey: async (ref) => {
|
|
7667
7814
|
try {
|
|
7668
7815
|
return await resolveCredential(ctx.get("credentials"), ref);
|
|
@@ -7670,22 +7817,12 @@ function apply(ctx, config = {}) {
|
|
|
7670
7817
|
return void 0;
|
|
7671
7818
|
}
|
|
7672
7819
|
},
|
|
7673
|
-
// Beneath the wrapper: the images call composes its own auth from the
|
|
7674
|
-
// model row's reference, exactly like the listing probe above.
|
|
7675
7820
|
send: headerOverlayBypass(),
|
|
7676
|
-
persistLastGood: (
|
|
7821
|
+
persistLastGood: (_modelId, params, model) => {
|
|
7677
7822
|
try {
|
|
7678
|
-
|
|
7679
|
-
|
|
7680
|
-
|
|
7681
|
-
if (model.id !== modelId) return model;
|
|
7682
|
-
if (params === null) {
|
|
7683
|
-
return { ...model, endpoint: { ...model.endpoint, status: "unsupported" } };
|
|
7684
|
-
}
|
|
7685
|
-
return { ...model, lastGoodParams: params };
|
|
7686
|
-
})
|
|
7687
|
-
}));
|
|
7688
|
-
} catch {
|
|
7823
|
+
imageConfiguration.record(model, params);
|
|
7824
|
+
} catch (error) {
|
|
7825
|
+
console.warn(`[dsh-model-think-level] \u751F\u56FE\u8C03\u7528\u8BCA\u65AD\u4FDD\u5B58\u5931\u8D25\uFF1A${error instanceof Error ? error.message : String(error)}`);
|
|
7689
7826
|
}
|
|
7690
7827
|
}
|
|
7691
7828
|
};
|
|
@@ -7698,7 +7835,7 @@ function apply(ctx, config = {}) {
|
|
|
7698
7835
|
registerImagegenCommand(
|
|
7699
7836
|
commandsService,
|
|
7700
7837
|
{
|
|
7701
|
-
modelIds: () =>
|
|
7838
|
+
modelIds: () => imageConfiguration.forGeneration().models.map((model) => model.id),
|
|
7702
7839
|
generate: generateImage,
|
|
7703
7840
|
generateDeps: imageGenerateDeps
|
|
7704
7841
|
}
|
|
@@ -7723,7 +7860,7 @@ function apply(ctx, config = {}) {
|
|
|
7723
7860
|
registered = registerGenerateImageTool(toolCtx, {
|
|
7724
7861
|
generateDeps: imageGenerateDeps,
|
|
7725
7862
|
attachments: toolCtx.attachments,
|
|
7726
|
-
readConfig: () =>
|
|
7863
|
+
readConfig: () => imageConfiguration.forGeneration()
|
|
7727
7864
|
});
|
|
7728
7865
|
imageToolState.name = registered.name;
|
|
7729
7866
|
imageToolState.error = void 0;
|
|
@@ -7734,8 +7871,13 @@ function apply(ctx, config = {}) {
|
|
|
7734
7871
|
}
|
|
7735
7872
|
};
|
|
7736
7873
|
const reconcile = () => {
|
|
7737
|
-
|
|
7738
|
-
|
|
7874
|
+
try {
|
|
7875
|
+
if (imageConfiguration.preferences().enabled) acquire();
|
|
7876
|
+
else release();
|
|
7877
|
+
} catch (error) {
|
|
7878
|
+
release();
|
|
7879
|
+
imageToolState.error = error instanceof Error ? error.message : String(error);
|
|
7880
|
+
}
|
|
7739
7881
|
};
|
|
7740
7882
|
reconcileImageTool = reconcile;
|
|
7741
7883
|
toolCtx.effect(
|
|
@@ -7753,7 +7895,18 @@ function apply(ctx, config = {}) {
|
|
|
7753
7895
|
() => promptCtx.systemPrompt.context({
|
|
7754
7896
|
name: "dsh-model-think-level:image-tool",
|
|
7755
7897
|
order: 61,
|
|
7756
|
-
text: () =>
|
|
7898
|
+
text: () => {
|
|
7899
|
+
reconcileImageTool?.();
|
|
7900
|
+
try {
|
|
7901
|
+
const config2 = imageConfiguration.forGeneration();
|
|
7902
|
+
if (config2.enabled && imageToolState.name === void 0) {
|
|
7903
|
+
return `\u751F\u56FE\u5DE5\u5177\u6682\u4E0D\u53EF\u7528\uFF1A${imageToolState.error ?? "\u5BBF\u4E3B\u5DE5\u5177\u6216\u9644\u4EF6\u670D\u52A1\u5C1A\u672A\u5C31\u7EEA"}\u3002\u8BF7\u660E\u786E\u62A5\u544A\u8BE5\u539F\u56E0\u3002`;
|
|
7904
|
+
}
|
|
7905
|
+
return imageToolHint(config2, imageToolState.name);
|
|
7906
|
+
} catch (error) {
|
|
7907
|
+
return `\u751F\u56FE\u914D\u7F6E\u6682\u4E0D\u53EF\u7528\uFF1A${error instanceof Error ? error.message : String(error)}\u3002\u8BF7\u660E\u786E\u62A5\u544A\u8BE5\u72B6\u6001\uFF0C\u4E0D\u8981\u628A\u8BFB\u53D6\u5931\u8D25\u89E3\u91CA\u4E3A\u672A\u914D\u7F6E\u6A21\u578B\u3002`;
|
|
7908
|
+
}
|
|
7909
|
+
}
|
|
7757
7910
|
}),
|
|
7758
7911
|
"dsh-model-think-level: image tool hint"
|
|
7759
7912
|
);
|
|
@@ -8999,10 +9152,39 @@ function apply(ctx, config = {}) {
|
|
|
8999
9152
|
return;
|
|
9000
9153
|
}
|
|
9001
9154
|
if (req.method === "GET") {
|
|
9002
|
-
|
|
9003
|
-
|
|
9155
|
+
reconcileImageTool?.();
|
|
9156
|
+
let config2;
|
|
9157
|
+
try {
|
|
9158
|
+
config2 = imageConfiguration.read();
|
|
9159
|
+
} catch (error) {
|
|
9160
|
+
let preferences;
|
|
9161
|
+
try {
|
|
9162
|
+
preferences = imageConfiguration.preferences();
|
|
9163
|
+
} catch {
|
|
9164
|
+
}
|
|
9165
|
+
sendJson(res, 503, {
|
|
9166
|
+
ok: false,
|
|
9167
|
+
error: error instanceof Error ? error.message : String(error),
|
|
9168
|
+
source: "live-settings",
|
|
9169
|
+
storageVersion: 3,
|
|
9170
|
+
...preferences === void 0 ? {} : {
|
|
9171
|
+
enabled: preferences.enabled,
|
|
9172
|
+
defaultModel: preferences.defaultModel,
|
|
9173
|
+
defaults: preferences.defaults,
|
|
9174
|
+
outputDir: preferences.outputDir
|
|
9175
|
+
},
|
|
9176
|
+
tool: {
|
|
9177
|
+
registered: imageToolState.name !== void 0,
|
|
9178
|
+
name: imageToolState.name ?? null,
|
|
9179
|
+
...imageToolState.error === void 0 ? {} : { error: imageToolState.error }
|
|
9180
|
+
}
|
|
9181
|
+
});
|
|
9182
|
+
return;
|
|
9183
|
+
}
|
|
9004
9184
|
sendJson(res, 200, {
|
|
9005
9185
|
ok: true,
|
|
9186
|
+
source: "live-settings",
|
|
9187
|
+
storageVersion: 3,
|
|
9006
9188
|
enabled: config2.enabled,
|
|
9007
9189
|
models: config2.models,
|
|
9008
9190
|
defaultModel: config2.defaultModel,
|
|
@@ -9025,7 +9207,7 @@ function apply(ctx, config = {}) {
|
|
|
9025
9207
|
return;
|
|
9026
9208
|
}
|
|
9027
9209
|
try {
|
|
9028
|
-
|
|
9210
|
+
imageConfiguration.update((document) => {
|
|
9029
9211
|
let next = document;
|
|
9030
9212
|
if (typeof body["enabled"] === "boolean") next = { ...next, enabled: body["enabled"] };
|
|
9031
9213
|
if (typeof body["defaultModel"] === "string" && body["defaultModel"].length > 0) {
|
|
@@ -9049,7 +9231,6 @@ function apply(ctx, config = {}) {
|
|
|
9049
9231
|
}
|
|
9050
9232
|
return next;
|
|
9051
9233
|
});
|
|
9052
|
-
syncImageConfigNow();
|
|
9053
9234
|
try {
|
|
9054
9235
|
reconcileImageTool?.();
|
|
9055
9236
|
} catch {
|