dsh-model-think-level 2.5.0 → 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/history-compat.d.ts +5 -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 +308 -125
- package/lib/index.js.map +4 -4
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -3465,8 +3465,10 @@ function degradeBlock(block, stage) {
|
|
|
3465
3465
|
if (type === "text") {
|
|
3466
3466
|
const signature = block["textSignature"];
|
|
3467
3467
|
if (typeof signature !== "string") return void 0;
|
|
3468
|
-
|
|
3469
|
-
|
|
3468
|
+
if (stripSignatureId(signature) === void 0) return void 0;
|
|
3469
|
+
const copy = { ...block };
|
|
3470
|
+
delete copy["textSignature"];
|
|
3471
|
+
return copy;
|
|
3470
3472
|
}
|
|
3471
3473
|
if (type === "toolCall") {
|
|
3472
3474
|
const id = block["id"];
|
|
@@ -4504,8 +4506,12 @@ function detectHeaderConflicts() {
|
|
|
4504
4506
|
};
|
|
4505
4507
|
}
|
|
4506
4508
|
|
|
4507
|
-
// src/image/
|
|
4509
|
+
// src/image/state.ts
|
|
4510
|
+
import { createHash, randomUUID as randomUUID3 } from "node:crypto";
|
|
4508
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
|
|
4509
4515
|
import { dirname as dirname7, join as join7 } from "node:path";
|
|
4510
4516
|
|
|
4511
4517
|
// src/image/sizes.ts
|
|
@@ -4547,7 +4553,7 @@ function stringList(value, limit = 32) {
|
|
|
4547
4553
|
const out = value.filter((entry) => typeof entry === "string" && entry.length > 0).slice(0, limit);
|
|
4548
4554
|
return out.length > 0 ? out : void 0;
|
|
4549
4555
|
}
|
|
4550
|
-
function
|
|
4556
|
+
function parseImageParams(raw) {
|
|
4551
4557
|
if (!isRecord2(raw)) return void 0;
|
|
4552
4558
|
const sizes = stringList(raw["sizes"])?.filter(isSize);
|
|
4553
4559
|
const qualities = stringList(raw["qualities"]);
|
|
@@ -4602,7 +4608,7 @@ function parseImageModel(raw) {
|
|
|
4602
4608
|
const api = raw["api"] === "images" ? "images" : "none";
|
|
4603
4609
|
const endpointRaw = isRecord2(raw["endpoint"]) ? raw["endpoint"] : {};
|
|
4604
4610
|
const status = endpointRaw["status"] === "ok" || endpointRaw["status"] === "unsupported" ? endpointRaw["status"] : "unverified";
|
|
4605
|
-
const params =
|
|
4611
|
+
const params = parseImageParams(raw["params"]) ?? { ...IMAGE_FALLBACK_PARAMS };
|
|
4606
4612
|
return {
|
|
4607
4613
|
id,
|
|
4608
4614
|
route,
|
|
@@ -4618,37 +4624,9 @@ function parseImageModel(raw) {
|
|
|
4618
4624
|
checkedAt: typeof endpointRaw["checkedAt"] === "string" ? endpointRaw["checkedAt"] : null
|
|
4619
4625
|
},
|
|
4620
4626
|
params,
|
|
4621
|
-
lastGoodParams:
|
|
4627
|
+
lastGoodParams: parseImageParams(raw["lastGoodParams"]) ?? null
|
|
4622
4628
|
};
|
|
4623
4629
|
}
|
|
4624
|
-
function readImageConfig(file = imageConfigFilePath()) {
|
|
4625
|
-
let text;
|
|
4626
|
-
try {
|
|
4627
|
-
text = readFileSync7(file, "utf8");
|
|
4628
|
-
} catch {
|
|
4629
|
-
return emptyImageConfig();
|
|
4630
|
-
}
|
|
4631
|
-
try {
|
|
4632
|
-
return parseImageConfig(JSON.parse(text));
|
|
4633
|
-
} catch {
|
|
4634
|
-
return emptyImageConfig();
|
|
4635
|
-
}
|
|
4636
|
-
}
|
|
4637
|
-
function writeImageConfig(document, file = imageConfigFilePath()) {
|
|
4638
|
-
mkdirSync6(dirname7(file), { recursive: true });
|
|
4639
|
-
const temporary = `${file}.${process.pid}.tmp`;
|
|
4640
|
-
writeFileSync6(temporary, `${JSON.stringify(document, null, 2)}
|
|
4641
|
-
`, "utf8");
|
|
4642
|
-
try {
|
|
4643
|
-
renameSync6(temporary, file);
|
|
4644
|
-
} catch (error) {
|
|
4645
|
-
try {
|
|
4646
|
-
unlinkSync6(temporary);
|
|
4647
|
-
} catch {
|
|
4648
|
-
}
|
|
4649
|
-
throw error;
|
|
4650
|
-
}
|
|
4651
|
-
}
|
|
4652
4630
|
function deriveImageSeeds(providers) {
|
|
4653
4631
|
const seeds = [];
|
|
4654
4632
|
for (const [route, profile] of Object.entries(providers)) {
|
|
@@ -4678,28 +4656,6 @@ function deriveImageSeeds(providers) {
|
|
|
4678
4656
|
}
|
|
4679
4657
|
return seeds;
|
|
4680
4658
|
}
|
|
4681
|
-
function syncImageConfig(stored, providers, now = () => /* @__PURE__ */ new Date()) {
|
|
4682
|
-
const seeds = deriveImageSeeds(providers);
|
|
4683
|
-
const previous = new Map(stored.models.map((model) => [model.id, model]));
|
|
4684
|
-
const models = seeds.map((seed) => {
|
|
4685
|
-
const kept = previous.get(seed.id);
|
|
4686
|
-
const params = kept !== void 0 ? kept.params : { ...IMAGE_FALLBACK_PARAMS };
|
|
4687
|
-
return {
|
|
4688
|
-
...seed,
|
|
4689
|
-
endpoint: kept?.endpoint ?? { kind: "images", status: "unverified", checkedAt: null },
|
|
4690
|
-
params,
|
|
4691
|
-
lastGoodParams: kept?.lastGoodParams ?? null
|
|
4692
|
-
};
|
|
4693
|
-
});
|
|
4694
|
-
const ids = new Set(models.map((model) => model.id));
|
|
4695
|
-
const keepDefault = stored.defaultModel !== null && (models.length === 0 || ids.has(stored.defaultModel));
|
|
4696
|
-
return {
|
|
4697
|
-
...stored,
|
|
4698
|
-
syncedAt: now().toISOString(),
|
|
4699
|
-
defaultModel: keepDefault ? stored.defaultModel : null,
|
|
4700
|
-
models
|
|
4701
|
-
};
|
|
4702
|
-
}
|
|
4703
4659
|
function resolveImageModel(config, explicit) {
|
|
4704
4660
|
if (explicit !== void 0 && explicit.length > 0) {
|
|
4705
4661
|
const direct = config.models.find((model) => model.id === explicit);
|
|
@@ -4724,15 +4680,187 @@ function effectiveParams(config, model, request) {
|
|
|
4724
4680
|
n: model === void 0 ? n : Math.min(n, model.params.maxN)
|
|
4725
4681
|
};
|
|
4726
4682
|
}
|
|
4727
|
-
|
|
4728
|
-
|
|
4729
|
-
|
|
4730
|
-
|
|
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
|
+
};
|
|
4731
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
|
+
};
|
|
4732
4860
|
|
|
4733
4861
|
// src/image/generate.ts
|
|
4734
4862
|
import { mkdirSync as mkdirSync7, readFileSync as readFileSync8, writeFileSync as writeFileSync7, existsSync as existsSync2, statSync } from "node:fs";
|
|
4735
|
-
import { dirname as
|
|
4863
|
+
import { dirname as dirname9, join as join8, resolve as resolve3 } from "node:path";
|
|
4736
4864
|
|
|
4737
4865
|
// src/image/deadline.ts
|
|
4738
4866
|
function createImageDeadline(timeoutMs, caller, errorFor) {
|
|
@@ -4997,7 +5125,7 @@ function resolveDestination(requested, fallbackDir, slug, now) {
|
|
|
4997
5125
|
mkdirSync7(target, { recursive: true });
|
|
4998
5126
|
return destinationOf(target, slug, now);
|
|
4999
5127
|
}
|
|
5000
|
-
mkdirSync7(
|
|
5128
|
+
mkdirSync7(dirname9(target), { recursive: true });
|
|
5001
5129
|
if (!existsSync2(target)) return target;
|
|
5002
5130
|
for (let index = 2; ; index += 1) {
|
|
5003
5131
|
const candidate = target.replace(/\.png$/i, `-${index}.png`);
|
|
@@ -5047,7 +5175,7 @@ function createGenerateDeadline(timeoutMs, signal) {
|
|
|
5047
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`));
|
|
5048
5176
|
}
|
|
5049
5177
|
async function generateImage(deps, request, sharedDeadline) {
|
|
5050
|
-
const config = deps.config
|
|
5178
|
+
const config = deps.config();
|
|
5051
5179
|
const deadline = sharedDeadline ?? createGenerateDeadline(deps.timeoutMs ?? config.defaults.timeoutMs, request.signal);
|
|
5052
5180
|
try {
|
|
5053
5181
|
deadline.check();
|
|
@@ -5168,14 +5296,14 @@ async function generateWithinDeadline(deps, request, config, deadline) {
|
|
|
5168
5296
|
images.push({ filePath, data });
|
|
5169
5297
|
}
|
|
5170
5298
|
deadline.check();
|
|
5171
|
-
if (
|
|
5299
|
+
if (deps.persistLastGood !== void 0) {
|
|
5172
5300
|
deps.persistLastGood(model.id, {
|
|
5173
5301
|
...model.params,
|
|
5174
5302
|
responseFormat: format,
|
|
5175
5303
|
sizes: model.params.sizes,
|
|
5176
5304
|
qualities: model.params.qualities,
|
|
5177
5305
|
maxN: model.params.maxN
|
|
5178
|
-
});
|
|
5306
|
+
}, model);
|
|
5179
5307
|
}
|
|
5180
5308
|
deadline.check();
|
|
5181
5309
|
return {
|
|
@@ -5196,7 +5324,7 @@ async function generateWithinDeadline(deps, request, config, deadline) {
|
|
|
5196
5324
|
}
|
|
5197
5325
|
const combined = sanitize([upstreamMessage(body), bodyText].filter((part) => typeof part === "string" && part.length > 0).join(" "), key);
|
|
5198
5326
|
lastFailure = classifyFailure2(response.status, combined);
|
|
5199
|
-
if (lastFailure.kind === "endpoint") deps.persistLastGood?.(model.id, null);
|
|
5327
|
+
if (lastFailure.kind === "endpoint") deps.persistLastGood?.(model.id, null, model);
|
|
5200
5328
|
const revision = fallbackRevisionFor(response.status, bodyText);
|
|
5201
5329
|
if (revision !== void 0 && revisions.length < 4) {
|
|
5202
5330
|
if (revision === "removed response_format" || revision === "removed output_format") {
|
|
@@ -5329,14 +5457,16 @@ function registerImagegenCommand(commands, deps) {
|
|
|
5329
5457
|
description: "\u7528\u751F\u56FE\u6A21\u578B\u751F\u6210\u56FE\u7247",
|
|
5330
5458
|
input: { hint: IMAGEGEN_HINT, attachments: true },
|
|
5331
5459
|
handler: async (invocation) => {
|
|
5332
|
-
const known = deps.modelIds();
|
|
5333
5460
|
let parsed;
|
|
5461
|
+
let generationDeps;
|
|
5334
5462
|
try {
|
|
5335
|
-
|
|
5463
|
+
const config = deps.generateDeps.config();
|
|
5464
|
+
generationDeps = { ...deps.generateDeps, config: () => config };
|
|
5465
|
+
parsed = parseImagegenArgs(invocationTextOf(invocation), config.models.map((model) => model.id));
|
|
5336
5466
|
} catch (error) {
|
|
5337
5467
|
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
5338
5468
|
}
|
|
5339
|
-
const answer = await executeImagegen(deps, parsed);
|
|
5469
|
+
const answer = await executeImagegen({ ...deps, generateDeps: generationDeps }, parsed);
|
|
5340
5470
|
if (answer.ok) {
|
|
5341
5471
|
try {
|
|
5342
5472
|
deps.echo?.(invocation, answer.text, answer.filePaths[0] ?? "");
|
|
@@ -5488,7 +5618,7 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
5488
5618
|
async execute(rawArgs, exec) {
|
|
5489
5619
|
const parsed = parseArgs(rawArgs);
|
|
5490
5620
|
if (typeof parsed === "string") throw new Error(parsed);
|
|
5491
|
-
const config = deps.generateDeps.config
|
|
5621
|
+
const config = deps.generateDeps.config();
|
|
5492
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");
|
|
5493
5623
|
const deadline = createGenerateDeadline(deps.generateDeps.timeoutMs ?? config.defaults.timeoutMs, exec.signal);
|
|
5494
5624
|
try {
|
|
@@ -5578,12 +5708,12 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
5578
5708
|
}
|
|
5579
5709
|
};
|
|
5580
5710
|
}
|
|
5581
|
-
function imageToolHint(config) {
|
|
5711
|
+
function imageToolHint(config, toolName = IMAGE_TOOL_NAME) {
|
|
5582
5712
|
if (!config.enabled) return "";
|
|
5583
|
-
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";
|
|
5584
5714
|
const defaults = `size ${config.defaults.size}\uFF0Cquality ${config.defaults.quality}\uFF0Cn ${config.defaults.n}`;
|
|
5585
5715
|
const resolved = resolveImageModel(config);
|
|
5586
|
-
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`;
|
|
5587
5717
|
const lines = [
|
|
5588
5718
|
opening,
|
|
5589
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",
|
|
@@ -5595,7 +5725,7 @@ function imageToolHint(config) {
|
|
|
5595
5725
|
|
|
5596
5726
|
// src/image/legacy-cleanup.ts
|
|
5597
5727
|
import { copyFileSync, existsSync as existsSync3, mkdirSync as mkdirSync8, readFileSync as readFileSync9, readdirSync as readdirSync2, statSync as statSync2, writeFileSync as writeFileSync8 } from "node:fs";
|
|
5598
|
-
import { dirname as
|
|
5728
|
+
import { dirname as dirname10, join as join9 } from "node:path";
|
|
5599
5729
|
var MARK_BEGIN = `# >>> ${IMAGE_MCP_PATCH_MARK}:begin`;
|
|
5600
5730
|
var MARK_END = `# <<< ${IMAGE_MCP_PATCH_MARK}:end`;
|
|
5601
5731
|
function resolveLegacyPatchFile(home = harnessHome()) {
|
|
@@ -5691,7 +5821,7 @@ function spliceOnce(patchFile, mtimeMs) {
|
|
|
5691
5821
|
} catch {
|
|
5692
5822
|
}
|
|
5693
5823
|
}
|
|
5694
|
-
mkdirSync8(
|
|
5824
|
+
mkdirSync8(dirname10(patchFile), { recursive: true });
|
|
5695
5825
|
writeFileSync8(patchFile, next, "utf8");
|
|
5696
5826
|
const written = readFileSync9(patchFile, "utf8");
|
|
5697
5827
|
if (written !== next) throw new Error("the patch file did not hold the written bytes after the write");
|
|
@@ -5880,7 +6010,7 @@ function backfillRolePin(model, providers, route) {
|
|
|
5880
6010
|
// src/fingerprint/bank.ts
|
|
5881
6011
|
import { existsSync as existsSync4, readFileSync as readFileSync10 } from "node:fs";
|
|
5882
6012
|
import { fileURLToPath } from "node:url";
|
|
5883
|
-
import { dirname as
|
|
6013
|
+
import { dirname as dirname11, join as join10 } from "node:path";
|
|
5884
6014
|
|
|
5885
6015
|
// src/fingerprint/core.ts
|
|
5886
6016
|
var VALUE_MIN = 1;
|
|
@@ -6165,10 +6295,10 @@ function parseBank(raw) {
|
|
|
6165
6295
|
}
|
|
6166
6296
|
function bankCandidates() {
|
|
6167
6297
|
const candidates = [];
|
|
6168
|
-
let dir =
|
|
6298
|
+
let dir = dirname11(fileURLToPath(import.meta.url));
|
|
6169
6299
|
for (let depth = 0; depth < 5; depth += 1) {
|
|
6170
6300
|
candidates.push(join10(dir, "assets", "unified_bank.json"));
|
|
6171
|
-
const parent =
|
|
6301
|
+
const parent = dirname11(dir);
|
|
6172
6302
|
if (parent === dir) break;
|
|
6173
6303
|
dir = parent;
|
|
6174
6304
|
}
|
|
@@ -6455,7 +6585,7 @@ async function runChallenge(options) {
|
|
|
6455
6585
|
|
|
6456
6586
|
// src/fingerprint/store.ts
|
|
6457
6587
|
import { mkdirSync as mkdirSync9, readFileSync as readFileSync11, renameSync as renameSync7, unlinkSync as unlinkSync7, writeFileSync as writeFileSync9 } from "node:fs";
|
|
6458
|
-
import { dirname as
|
|
6588
|
+
import { dirname as dirname12, join as join11 } from "node:path";
|
|
6459
6589
|
var STATUSES = /* @__PURE__ */ new Set(["identified", "doubtful", "failed"]);
|
|
6460
6590
|
function emptyFingerprintStore(bankBuiltAt2 = "") {
|
|
6461
6591
|
return { version: 1, bankBuiltAt: bankBuiltAt2, records: {} };
|
|
@@ -6598,7 +6728,7 @@ function readFingerprintStore(bankBuiltAt2 = "", file = fingerprintFilePath()) {
|
|
|
6598
6728
|
return parseFingerprintStore(raw, bankBuiltAt2);
|
|
6599
6729
|
}
|
|
6600
6730
|
function writeFingerprintStore(document, file = fingerprintFilePath()) {
|
|
6601
|
-
mkdirSync9(
|
|
6731
|
+
mkdirSync9(dirname12(file), { recursive: true });
|
|
6602
6732
|
const temporary = `${file}.${process.pid}.tmp`;
|
|
6603
6733
|
writeFileSync9(temporary, `${JSON.stringify(document, null, 2)}
|
|
6604
6734
|
`, "utf8");
|
|
@@ -7123,14 +7253,22 @@ function apply(ctx, config = {}) {
|
|
|
7123
7253
|
const settings = ctx.settings;
|
|
7124
7254
|
let piDescriptorCache;
|
|
7125
7255
|
let piDescriptorCached = false;
|
|
7126
|
-
|
|
7127
|
-
|
|
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;
|
|
7128
7263
|
let descriptor;
|
|
7129
7264
|
try {
|
|
7265
|
+
piDescriptorReading = true;
|
|
7130
7266
|
descriptor = settings.describe().find((entry) => entry.ns === PI_NS);
|
|
7131
7267
|
} catch {
|
|
7132
7268
|
if (required) throw new HostReadError("the llm-pi-ai settings could not be read");
|
|
7133
7269
|
return void 0;
|
|
7270
|
+
} finally {
|
|
7271
|
+
piDescriptorReading = false;
|
|
7134
7272
|
}
|
|
7135
7273
|
if (descriptor === void 0 || !isRecord(descriptor.value)) {
|
|
7136
7274
|
if (required) throw new HostReadError("the llm-pi-ai settings entry is unavailable");
|
|
@@ -7515,18 +7653,33 @@ function apply(ctx, config = {}) {
|
|
|
7515
7653
|
refreshHeaders();
|
|
7516
7654
|
refreshKeyScopes();
|
|
7517
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
|
+
});
|
|
7518
7671
|
ctx.on("settings/document-updated", (ns) => {
|
|
7519
|
-
if (ns
|
|
7520
|
-
|
|
7521
|
-
|
|
7522
|
-
|
|
7523
|
-
const source = catalogueSource();
|
|
7524
|
-
if (source !== catalogueSourceStamp) {
|
|
7525
|
-
catalogueSourceStamp = source;
|
|
7526
|
-
invalidateListings();
|
|
7527
|
-
}
|
|
7528
|
-
scheduleKeyProbes();
|
|
7672
|
+
if (ns !== PI_NS) return;
|
|
7673
|
+
if (!piDescriptorReading) {
|
|
7674
|
+
refreshModelConsumers();
|
|
7675
|
+
return;
|
|
7529
7676
|
}
|
|
7677
|
+
if (modelConsumerRefreshQueued) return;
|
|
7678
|
+
modelConsumerRefreshQueued = true;
|
|
7679
|
+
queueMicrotask(() => {
|
|
7680
|
+
modelConsumerRefreshQueued = false;
|
|
7681
|
+
if (!modelConsumersDisposed) refreshModelConsumers();
|
|
7682
|
+
});
|
|
7530
7683
|
});
|
|
7531
7684
|
overlaySource.keys = async ({ route, model }) => {
|
|
7532
7685
|
const scopes = keyScopes.current[route] ?? [];
|
|
@@ -7649,18 +7802,14 @@ function apply(ctx, config = {}) {
|
|
|
7649
7802
|
} catch (error) {
|
|
7650
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)}`);
|
|
7651
7804
|
}
|
|
7652
|
-
const
|
|
7653
|
-
|
|
7654
|
-
|
|
7655
|
-
|
|
7656
|
-
if (Object.keys(providers).length === 0) return;
|
|
7657
|
-
updateImageConfig((document) => syncImageConfig(document, providers));
|
|
7658
|
-
} catch {
|
|
7805
|
+
const imageConfiguration = new ImageConfiguration({
|
|
7806
|
+
describe: () => {
|
|
7807
|
+
const descriptor = piDescriptor(true, true);
|
|
7808
|
+
return descriptor === void 0 ? [] : [descriptor];
|
|
7659
7809
|
}
|
|
7660
|
-
};
|
|
7661
|
-
syncImageConfigNow();
|
|
7810
|
+
});
|
|
7662
7811
|
const imageGenerateDeps = {
|
|
7663
|
-
config: () =>
|
|
7812
|
+
config: () => imageConfiguration.forGeneration(),
|
|
7664
7813
|
resolveKey: async (ref) => {
|
|
7665
7814
|
try {
|
|
7666
7815
|
return await resolveCredential(ctx.get("credentials"), ref);
|
|
@@ -7668,22 +7817,12 @@ function apply(ctx, config = {}) {
|
|
|
7668
7817
|
return void 0;
|
|
7669
7818
|
}
|
|
7670
7819
|
},
|
|
7671
|
-
// Beneath the wrapper: the images call composes its own auth from the
|
|
7672
|
-
// model row's reference, exactly like the listing probe above.
|
|
7673
7820
|
send: headerOverlayBypass(),
|
|
7674
|
-
persistLastGood: (
|
|
7821
|
+
persistLastGood: (_modelId, params, model) => {
|
|
7675
7822
|
try {
|
|
7676
|
-
|
|
7677
|
-
|
|
7678
|
-
|
|
7679
|
-
if (model.id !== modelId) return model;
|
|
7680
|
-
if (params === null) {
|
|
7681
|
-
return { ...model, endpoint: { ...model.endpoint, status: "unsupported" } };
|
|
7682
|
-
}
|
|
7683
|
-
return { ...model, lastGoodParams: params };
|
|
7684
|
-
})
|
|
7685
|
-
}));
|
|
7686
|
-
} 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)}`);
|
|
7687
7826
|
}
|
|
7688
7827
|
}
|
|
7689
7828
|
};
|
|
@@ -7696,7 +7835,7 @@ function apply(ctx, config = {}) {
|
|
|
7696
7835
|
registerImagegenCommand(
|
|
7697
7836
|
commandsService,
|
|
7698
7837
|
{
|
|
7699
|
-
modelIds: () =>
|
|
7838
|
+
modelIds: () => imageConfiguration.forGeneration().models.map((model) => model.id),
|
|
7700
7839
|
generate: generateImage,
|
|
7701
7840
|
generateDeps: imageGenerateDeps
|
|
7702
7841
|
}
|
|
@@ -7721,7 +7860,7 @@ function apply(ctx, config = {}) {
|
|
|
7721
7860
|
registered = registerGenerateImageTool(toolCtx, {
|
|
7722
7861
|
generateDeps: imageGenerateDeps,
|
|
7723
7862
|
attachments: toolCtx.attachments,
|
|
7724
|
-
readConfig: () =>
|
|
7863
|
+
readConfig: () => imageConfiguration.forGeneration()
|
|
7725
7864
|
});
|
|
7726
7865
|
imageToolState.name = registered.name;
|
|
7727
7866
|
imageToolState.error = void 0;
|
|
@@ -7732,8 +7871,13 @@ function apply(ctx, config = {}) {
|
|
|
7732
7871
|
}
|
|
7733
7872
|
};
|
|
7734
7873
|
const reconcile = () => {
|
|
7735
|
-
|
|
7736
|
-
|
|
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
|
+
}
|
|
7737
7881
|
};
|
|
7738
7882
|
reconcileImageTool = reconcile;
|
|
7739
7883
|
toolCtx.effect(
|
|
@@ -7751,7 +7895,18 @@ function apply(ctx, config = {}) {
|
|
|
7751
7895
|
() => promptCtx.systemPrompt.context({
|
|
7752
7896
|
name: "dsh-model-think-level:image-tool",
|
|
7753
7897
|
order: 61,
|
|
7754
|
-
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
|
+
}
|
|
7755
7910
|
}),
|
|
7756
7911
|
"dsh-model-think-level: image tool hint"
|
|
7757
7912
|
);
|
|
@@ -8997,10 +9152,39 @@ function apply(ctx, config = {}) {
|
|
|
8997
9152
|
return;
|
|
8998
9153
|
}
|
|
8999
9154
|
if (req.method === "GET") {
|
|
9000
|
-
|
|
9001
|
-
|
|
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
|
+
}
|
|
9002
9184
|
sendJson(res, 200, {
|
|
9003
9185
|
ok: true,
|
|
9186
|
+
source: "live-settings",
|
|
9187
|
+
storageVersion: 3,
|
|
9004
9188
|
enabled: config2.enabled,
|
|
9005
9189
|
models: config2.models,
|
|
9006
9190
|
defaultModel: config2.defaultModel,
|
|
@@ -9023,7 +9207,7 @@ function apply(ctx, config = {}) {
|
|
|
9023
9207
|
return;
|
|
9024
9208
|
}
|
|
9025
9209
|
try {
|
|
9026
|
-
|
|
9210
|
+
imageConfiguration.update((document) => {
|
|
9027
9211
|
let next = document;
|
|
9028
9212
|
if (typeof body["enabled"] === "boolean") next = { ...next, enabled: body["enabled"] };
|
|
9029
9213
|
if (typeof body["defaultModel"] === "string" && body["defaultModel"].length > 0) {
|
|
@@ -9047,7 +9231,6 @@ function apply(ctx, config = {}) {
|
|
|
9047
9231
|
}
|
|
9048
9232
|
return next;
|
|
9049
9233
|
});
|
|
9050
|
-
syncImageConfigNow();
|
|
9051
9234
|
try {
|
|
9052
9235
|
reconcileImageTool?.();
|
|
9053
9236
|
} catch {
|