dsh-model-think-level 2.5.1 → 2.6.1
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 +17 -6
- 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 +12 -1
- 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 -124
- package/lib/index.js.map +4 -4
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -3421,8 +3421,11 @@ var RESOURCE_MISMATCH = [
|
|
|
3421
3421
|
/created under a different [a-z0-9 ._-]{0,48}?resource/i,
|
|
3422
3422
|
/same resource that created the item/i
|
|
3423
3423
|
];
|
|
3424
|
+
var ITEM_PAIRING_MISMATCH = [
|
|
3425
|
+
/provided without its required [^\n]{0,40}?reasoning/i
|
|
3426
|
+
];
|
|
3424
3427
|
function isHistoryStateMismatch(text) {
|
|
3425
|
-
return RESOURCE_MISMATCH.some((pattern) => pattern.test(text));
|
|
3428
|
+
return RESOURCE_MISMATCH.some((pattern) => pattern.test(text)) || ITEM_PAIRING_MISMATCH.some((pattern) => pattern.test(text));
|
|
3426
3429
|
}
|
|
3427
3430
|
var HISTORY_STAGES = ["portable_ids", "drop_reasoning"];
|
|
3428
3431
|
function degradeForHistory(context, stage) {
|
|
@@ -4506,8 +4509,12 @@ function detectHeaderConflicts() {
|
|
|
4506
4509
|
};
|
|
4507
4510
|
}
|
|
4508
4511
|
|
|
4509
|
-
// src/image/
|
|
4512
|
+
// src/image/state.ts
|
|
4513
|
+
import { createHash, randomUUID as randomUUID3 } from "node:crypto";
|
|
4510
4514
|
import { mkdirSync as mkdirSync6, readFileSync as readFileSync7, renameSync as renameSync6, unlinkSync as unlinkSync6, writeFileSync as writeFileSync6 } from "node:fs";
|
|
4515
|
+
import { dirname as dirname8 } from "node:path";
|
|
4516
|
+
|
|
4517
|
+
// src/image/config.ts
|
|
4511
4518
|
import { dirname as dirname7, join as join7 } from "node:path";
|
|
4512
4519
|
|
|
4513
4520
|
// src/image/sizes.ts
|
|
@@ -4549,7 +4556,7 @@ function stringList(value, limit = 32) {
|
|
|
4549
4556
|
const out = value.filter((entry) => typeof entry === "string" && entry.length > 0).slice(0, limit);
|
|
4550
4557
|
return out.length > 0 ? out : void 0;
|
|
4551
4558
|
}
|
|
4552
|
-
function
|
|
4559
|
+
function parseImageParams(raw) {
|
|
4553
4560
|
if (!isRecord2(raw)) return void 0;
|
|
4554
4561
|
const sizes = stringList(raw["sizes"])?.filter(isSize);
|
|
4555
4562
|
const qualities = stringList(raw["qualities"]);
|
|
@@ -4604,7 +4611,7 @@ function parseImageModel(raw) {
|
|
|
4604
4611
|
const api = raw["api"] === "images" ? "images" : "none";
|
|
4605
4612
|
const endpointRaw = isRecord2(raw["endpoint"]) ? raw["endpoint"] : {};
|
|
4606
4613
|
const status = endpointRaw["status"] === "ok" || endpointRaw["status"] === "unsupported" ? endpointRaw["status"] : "unverified";
|
|
4607
|
-
const params =
|
|
4614
|
+
const params = parseImageParams(raw["params"]) ?? { ...IMAGE_FALLBACK_PARAMS };
|
|
4608
4615
|
return {
|
|
4609
4616
|
id,
|
|
4610
4617
|
route,
|
|
@@ -4620,37 +4627,9 @@ function parseImageModel(raw) {
|
|
|
4620
4627
|
checkedAt: typeof endpointRaw["checkedAt"] === "string" ? endpointRaw["checkedAt"] : null
|
|
4621
4628
|
},
|
|
4622
4629
|
params,
|
|
4623
|
-
lastGoodParams:
|
|
4630
|
+
lastGoodParams: parseImageParams(raw["lastGoodParams"]) ?? null
|
|
4624
4631
|
};
|
|
4625
4632
|
}
|
|
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
4633
|
function deriveImageSeeds(providers) {
|
|
4655
4634
|
const seeds = [];
|
|
4656
4635
|
for (const [route, profile] of Object.entries(providers)) {
|
|
@@ -4680,28 +4659,6 @@ function deriveImageSeeds(providers) {
|
|
|
4680
4659
|
}
|
|
4681
4660
|
return seeds;
|
|
4682
4661
|
}
|
|
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
4662
|
function resolveImageModel(config, explicit) {
|
|
4706
4663
|
if (explicit !== void 0 && explicit.length > 0) {
|
|
4707
4664
|
const direct = config.models.find((model) => model.id === explicit);
|
|
@@ -4726,15 +4683,187 @@ function effectiveParams(config, model, request) {
|
|
|
4726
4683
|
n: model === void 0 ? n : Math.min(n, model.params.maxN)
|
|
4727
4684
|
};
|
|
4728
4685
|
}
|
|
4729
|
-
|
|
4730
|
-
|
|
4731
|
-
|
|
4732
|
-
|
|
4686
|
+
|
|
4687
|
+
// src/image/state.ts
|
|
4688
|
+
var ImageConfigurationError = class extends Error {
|
|
4689
|
+
constructor(code, message) {
|
|
4690
|
+
super(message);
|
|
4691
|
+
this.code = code;
|
|
4692
|
+
this.name = "ImageConfigurationError";
|
|
4693
|
+
}
|
|
4694
|
+
code;
|
|
4695
|
+
};
|
|
4696
|
+
function isRecord3(value) {
|
|
4697
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4698
|
+
}
|
|
4699
|
+
function sourceOf(model) {
|
|
4700
|
+
return createHash("sha256").update(JSON.stringify([
|
|
4701
|
+
model.route,
|
|
4702
|
+
model.modelId,
|
|
4703
|
+
model.api,
|
|
4704
|
+
model.baseURL,
|
|
4705
|
+
model.keyRef ?? null
|
|
4706
|
+
])).digest("hex");
|
|
4707
|
+
}
|
|
4708
|
+
function preferencesOf(raw) {
|
|
4709
|
+
const parsed = parseImageConfig(raw);
|
|
4710
|
+
const observations = /* @__PURE__ */ Object.create(null);
|
|
4711
|
+
if (raw["version"] === 3) {
|
|
4712
|
+
if (isRecord3(raw["observations"])) {
|
|
4713
|
+
for (const [id, value] of Object.entries(raw["observations"])) {
|
|
4714
|
+
if (!isRecord3(value) || typeof value["source"] !== "string" || !/^[a-f0-9]{64}$/.test(value["source"])) continue;
|
|
4715
|
+
const endpoint = isRecord3(value["endpoint"]) ? value["endpoint"] : {};
|
|
4716
|
+
observations[id] = {
|
|
4717
|
+
source: value["source"],
|
|
4718
|
+
endpoint: {
|
|
4719
|
+
kind: "images",
|
|
4720
|
+
status: endpoint["status"] === "ok" || endpoint["status"] === "unsupported" ? endpoint["status"] : "unverified",
|
|
4721
|
+
checkedAt: typeof endpoint["checkedAt"] === "string" ? endpoint["checkedAt"] : null
|
|
4722
|
+
},
|
|
4723
|
+
lastGoodParams: parseImageParams(value["lastGoodParams"]) ?? null
|
|
4724
|
+
};
|
|
4725
|
+
}
|
|
4726
|
+
}
|
|
4727
|
+
} else {
|
|
4728
|
+
for (const model of parsed.models) {
|
|
4729
|
+
observations[model.id] = {
|
|
4730
|
+
source: sourceOf(model),
|
|
4731
|
+
endpoint: model.endpoint,
|
|
4732
|
+
lastGoodParams: model.lastGoodParams
|
|
4733
|
+
};
|
|
4734
|
+
}
|
|
4735
|
+
}
|
|
4736
|
+
return {
|
|
4737
|
+
version: 3,
|
|
4738
|
+
enabled: parsed.enabled,
|
|
4739
|
+
defaultModel: parsed.defaultModel,
|
|
4740
|
+
defaults: parsed.defaults,
|
|
4741
|
+
outputDir: parsed.outputDir,
|
|
4742
|
+
observations
|
|
4743
|
+
};
|
|
4733
4744
|
}
|
|
4745
|
+
var ImageConfiguration = class {
|
|
4746
|
+
constructor(deps) {
|
|
4747
|
+
this.deps = deps;
|
|
4748
|
+
this.file = deps.file ?? imageConfigFilePath();
|
|
4749
|
+
this.now = deps.now ?? (() => /* @__PURE__ */ new Date());
|
|
4750
|
+
}
|
|
4751
|
+
deps;
|
|
4752
|
+
file;
|
|
4753
|
+
now;
|
|
4754
|
+
load() {
|
|
4755
|
+
let text;
|
|
4756
|
+
try {
|
|
4757
|
+
text = readFileSync7(this.file, "utf8");
|
|
4758
|
+
} catch (error) {
|
|
4759
|
+
if (error.code === "ENOENT") return { document: preferencesOf({}) };
|
|
4760
|
+
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");
|
|
4761
|
+
}
|
|
4762
|
+
let raw;
|
|
4763
|
+
try {
|
|
4764
|
+
raw = JSON.parse(text);
|
|
4765
|
+
} catch {
|
|
4766
|
+
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");
|
|
4767
|
+
}
|
|
4768
|
+
if (!isRecord3(raw) || raw["version"] !== void 0 && raw["version"] !== 1 && raw["version"] !== 2 && raw["version"] !== 3) {
|
|
4769
|
+
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");
|
|
4770
|
+
}
|
|
4771
|
+
return { document: preferencesOf(raw), ...raw["version"] === 3 ? {} : { legacyText: text } };
|
|
4772
|
+
}
|
|
4773
|
+
preferences() {
|
|
4774
|
+
return this.load().document;
|
|
4775
|
+
}
|
|
4776
|
+
base(preferences) {
|
|
4777
|
+
return {
|
|
4778
|
+
...emptyImageConfig(this.now),
|
|
4779
|
+
enabled: preferences.enabled,
|
|
4780
|
+
defaultModel: preferences.defaultModel,
|
|
4781
|
+
defaults: preferences.defaults,
|
|
4782
|
+
outputDir: preferences.outputDir
|
|
4783
|
+
};
|
|
4784
|
+
}
|
|
4785
|
+
read() {
|
|
4786
|
+
return this.project(this.preferences());
|
|
4787
|
+
}
|
|
4788
|
+
project(preferences) {
|
|
4789
|
+
let descriptor;
|
|
4790
|
+
try {
|
|
4791
|
+
descriptor = this.deps.describe().find((entry) => entry.ns === "llm-pi-ai");
|
|
4792
|
+
} catch {
|
|
4793
|
+
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");
|
|
4794
|
+
}
|
|
4795
|
+
if (descriptor === void 0) {
|
|
4796
|
+
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");
|
|
4797
|
+
}
|
|
4798
|
+
const section = descriptor.value;
|
|
4799
|
+
if (!isRecord3(section) || section["providers"] !== void 0 && !isRecord3(section["providers"])) {
|
|
4800
|
+
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");
|
|
4801
|
+
}
|
|
4802
|
+
const providers = section["providers"] ?? {};
|
|
4803
|
+
const models = deriveImageSeeds(providers).map((seed) => {
|
|
4804
|
+
const recorded = preferences.observations[seed.id];
|
|
4805
|
+
const observation = recorded?.source === sourceOf(seed) ? recorded : void 0;
|
|
4806
|
+
return {
|
|
4807
|
+
...seed,
|
|
4808
|
+
params: { ...IMAGE_FALLBACK_PARAMS },
|
|
4809
|
+
endpoint: observation?.endpoint ?? { kind: "images", status: "unverified", checkedAt: null },
|
|
4810
|
+
lastGoodParams: observation?.lastGoodParams ?? null
|
|
4811
|
+
};
|
|
4812
|
+
});
|
|
4813
|
+
return { ...this.base(preferences), models };
|
|
4814
|
+
}
|
|
4815
|
+
/** The saved switch wins even while the model provider is unavailable. */
|
|
4816
|
+
forGeneration() {
|
|
4817
|
+
const preferences = this.preferences();
|
|
4818
|
+
return preferences.enabled ? this.project(preferences) : this.base(preferences);
|
|
4819
|
+
}
|
|
4820
|
+
update(mutate) {
|
|
4821
|
+
const current = this.load();
|
|
4822
|
+
const next = preferencesOf(mutate(current.document));
|
|
4823
|
+
mkdirSync6(dirname8(this.file), { recursive: true });
|
|
4824
|
+
if (current.legacyText !== void 0) {
|
|
4825
|
+
try {
|
|
4826
|
+
writeFileSync6(`${this.file}.v2.bak`, current.legacyText, { encoding: "utf8", flag: "wx", mode: 384 });
|
|
4827
|
+
} catch (error) {
|
|
4828
|
+
if (error.code !== "EEXIST") throw error;
|
|
4829
|
+
}
|
|
4830
|
+
}
|
|
4831
|
+
const temporary = `${this.file}.${process.pid}.${randomUUID3()}.tmp`;
|
|
4832
|
+
try {
|
|
4833
|
+
writeFileSync6(temporary, `${JSON.stringify(next, null, 2)}
|
|
4834
|
+
`, { encoding: "utf8", flag: "wx", mode: 384 });
|
|
4835
|
+
renameSync6(temporary, this.file);
|
|
4836
|
+
} catch (error) {
|
|
4837
|
+
try {
|
|
4838
|
+
unlinkSync6(temporary);
|
|
4839
|
+
} catch {
|
|
4840
|
+
}
|
|
4841
|
+
throw error;
|
|
4842
|
+
}
|
|
4843
|
+
return next;
|
|
4844
|
+
}
|
|
4845
|
+
record(model, params) {
|
|
4846
|
+
this.update((document) => ({
|
|
4847
|
+
...document,
|
|
4848
|
+
observations: {
|
|
4849
|
+
...document.observations,
|
|
4850
|
+
[model.id]: {
|
|
4851
|
+
source: sourceOf(model),
|
|
4852
|
+
endpoint: {
|
|
4853
|
+
kind: "images",
|
|
4854
|
+
status: params === null ? "unsupported" : "ok",
|
|
4855
|
+
checkedAt: this.now().toISOString()
|
|
4856
|
+
},
|
|
4857
|
+
lastGoodParams: params
|
|
4858
|
+
}
|
|
4859
|
+
}
|
|
4860
|
+
}));
|
|
4861
|
+
}
|
|
4862
|
+
};
|
|
4734
4863
|
|
|
4735
4864
|
// src/image/generate.ts
|
|
4736
4865
|
import { mkdirSync as mkdirSync7, readFileSync as readFileSync8, writeFileSync as writeFileSync7, existsSync as existsSync2, statSync } from "node:fs";
|
|
4737
|
-
import { dirname as
|
|
4866
|
+
import { dirname as dirname9, join as join8, resolve as resolve3 } from "node:path";
|
|
4738
4867
|
|
|
4739
4868
|
// src/image/deadline.ts
|
|
4740
4869
|
function createImageDeadline(timeoutMs, caller, errorFor) {
|
|
@@ -4999,7 +5128,7 @@ function resolveDestination(requested, fallbackDir, slug, now) {
|
|
|
4999
5128
|
mkdirSync7(target, { recursive: true });
|
|
5000
5129
|
return destinationOf(target, slug, now);
|
|
5001
5130
|
}
|
|
5002
|
-
mkdirSync7(
|
|
5131
|
+
mkdirSync7(dirname9(target), { recursive: true });
|
|
5003
5132
|
if (!existsSync2(target)) return target;
|
|
5004
5133
|
for (let index = 2; ; index += 1) {
|
|
5005
5134
|
const candidate = target.replace(/\.png$/i, `-${index}.png`);
|
|
@@ -5049,7 +5178,7 @@ function createGenerateDeadline(timeoutMs, signal) {
|
|
|
5049
5178
|
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
5179
|
}
|
|
5051
5180
|
async function generateImage(deps, request, sharedDeadline) {
|
|
5052
|
-
const config = deps.config
|
|
5181
|
+
const config = deps.config();
|
|
5053
5182
|
const deadline = sharedDeadline ?? createGenerateDeadline(deps.timeoutMs ?? config.defaults.timeoutMs, request.signal);
|
|
5054
5183
|
try {
|
|
5055
5184
|
deadline.check();
|
|
@@ -5170,14 +5299,14 @@ async function generateWithinDeadline(deps, request, config, deadline) {
|
|
|
5170
5299
|
images.push({ filePath, data });
|
|
5171
5300
|
}
|
|
5172
5301
|
deadline.check();
|
|
5173
|
-
if (
|
|
5302
|
+
if (deps.persistLastGood !== void 0) {
|
|
5174
5303
|
deps.persistLastGood(model.id, {
|
|
5175
5304
|
...model.params,
|
|
5176
5305
|
responseFormat: format,
|
|
5177
5306
|
sizes: model.params.sizes,
|
|
5178
5307
|
qualities: model.params.qualities,
|
|
5179
5308
|
maxN: model.params.maxN
|
|
5180
|
-
});
|
|
5309
|
+
}, model);
|
|
5181
5310
|
}
|
|
5182
5311
|
deadline.check();
|
|
5183
5312
|
return {
|
|
@@ -5198,7 +5327,7 @@ async function generateWithinDeadline(deps, request, config, deadline) {
|
|
|
5198
5327
|
}
|
|
5199
5328
|
const combined = sanitize([upstreamMessage(body), bodyText].filter((part) => typeof part === "string" && part.length > 0).join(" "), key);
|
|
5200
5329
|
lastFailure = classifyFailure2(response.status, combined);
|
|
5201
|
-
if (lastFailure.kind === "endpoint") deps.persistLastGood?.(model.id, null);
|
|
5330
|
+
if (lastFailure.kind === "endpoint") deps.persistLastGood?.(model.id, null, model);
|
|
5202
5331
|
const revision = fallbackRevisionFor(response.status, bodyText);
|
|
5203
5332
|
if (revision !== void 0 && revisions.length < 4) {
|
|
5204
5333
|
if (revision === "removed response_format" || revision === "removed output_format") {
|
|
@@ -5331,14 +5460,16 @@ function registerImagegenCommand(commands, deps) {
|
|
|
5331
5460
|
description: "\u7528\u751F\u56FE\u6A21\u578B\u751F\u6210\u56FE\u7247",
|
|
5332
5461
|
input: { hint: IMAGEGEN_HINT, attachments: true },
|
|
5333
5462
|
handler: async (invocation) => {
|
|
5334
|
-
const known = deps.modelIds();
|
|
5335
5463
|
let parsed;
|
|
5464
|
+
let generationDeps;
|
|
5336
5465
|
try {
|
|
5337
|
-
|
|
5466
|
+
const config = deps.generateDeps.config();
|
|
5467
|
+
generationDeps = { ...deps.generateDeps, config: () => config };
|
|
5468
|
+
parsed = parseImagegenArgs(invocationTextOf(invocation), config.models.map((model) => model.id));
|
|
5338
5469
|
} catch (error) {
|
|
5339
5470
|
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
5340
5471
|
}
|
|
5341
|
-
const answer = await executeImagegen(deps, parsed);
|
|
5472
|
+
const answer = await executeImagegen({ ...deps, generateDeps: generationDeps }, parsed);
|
|
5342
5473
|
if (answer.ok) {
|
|
5343
5474
|
try {
|
|
5344
5475
|
deps.echo?.(invocation, answer.text, answer.filePaths[0] ?? "");
|
|
@@ -5490,7 +5621,7 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
5490
5621
|
async execute(rawArgs, exec) {
|
|
5491
5622
|
const parsed = parseArgs(rawArgs);
|
|
5492
5623
|
if (typeof parsed === "string") throw new Error(parsed);
|
|
5493
|
-
const config = deps.generateDeps.config
|
|
5624
|
+
const config = deps.generateDeps.config();
|
|
5494
5625
|
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
5626
|
const deadline = createGenerateDeadline(deps.generateDeps.timeoutMs ?? config.defaults.timeoutMs, exec.signal);
|
|
5496
5627
|
try {
|
|
@@ -5580,12 +5711,12 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
5580
5711
|
}
|
|
5581
5712
|
};
|
|
5582
5713
|
}
|
|
5583
|
-
function imageToolHint(config) {
|
|
5714
|
+
function imageToolHint(config, toolName = IMAGE_TOOL_NAME) {
|
|
5584
5715
|
if (!config.enabled) return "";
|
|
5585
|
-
if (config.models.length === 0) return "";
|
|
5716
|
+
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
5717
|
const defaults = `size ${config.defaults.size}\uFF0Cquality ${config.defaults.quality}\uFF0Cn ${config.defaults.n}`;
|
|
5587
5718
|
const resolved = resolveImageModel(config);
|
|
5588
|
-
const opening = resolved === void 0 ? `\u751F\u56FE\u8BF7\u8C03\u7528
|
|
5719
|
+
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
5720
|
const lines = [
|
|
5590
5721
|
opening,
|
|
5591
5722
|
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 +5728,7 @@ function imageToolHint(config) {
|
|
|
5597
5728
|
|
|
5598
5729
|
// src/image/legacy-cleanup.ts
|
|
5599
5730
|
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
|
|
5731
|
+
import { dirname as dirname10, join as join9 } from "node:path";
|
|
5601
5732
|
var MARK_BEGIN = `# >>> ${IMAGE_MCP_PATCH_MARK}:begin`;
|
|
5602
5733
|
var MARK_END = `# <<< ${IMAGE_MCP_PATCH_MARK}:end`;
|
|
5603
5734
|
function resolveLegacyPatchFile(home = harnessHome()) {
|
|
@@ -5693,7 +5824,7 @@ function spliceOnce(patchFile, mtimeMs) {
|
|
|
5693
5824
|
} catch {
|
|
5694
5825
|
}
|
|
5695
5826
|
}
|
|
5696
|
-
mkdirSync8(
|
|
5827
|
+
mkdirSync8(dirname10(patchFile), { recursive: true });
|
|
5697
5828
|
writeFileSync8(patchFile, next, "utf8");
|
|
5698
5829
|
const written = readFileSync9(patchFile, "utf8");
|
|
5699
5830
|
if (written !== next) throw new Error("the patch file did not hold the written bytes after the write");
|
|
@@ -5882,7 +6013,7 @@ function backfillRolePin(model, providers, route) {
|
|
|
5882
6013
|
// src/fingerprint/bank.ts
|
|
5883
6014
|
import { existsSync as existsSync4, readFileSync as readFileSync10 } from "node:fs";
|
|
5884
6015
|
import { fileURLToPath } from "node:url";
|
|
5885
|
-
import { dirname as
|
|
6016
|
+
import { dirname as dirname11, join as join10 } from "node:path";
|
|
5886
6017
|
|
|
5887
6018
|
// src/fingerprint/core.ts
|
|
5888
6019
|
var VALUE_MIN = 1;
|
|
@@ -6167,10 +6298,10 @@ function parseBank(raw) {
|
|
|
6167
6298
|
}
|
|
6168
6299
|
function bankCandidates() {
|
|
6169
6300
|
const candidates = [];
|
|
6170
|
-
let dir =
|
|
6301
|
+
let dir = dirname11(fileURLToPath(import.meta.url));
|
|
6171
6302
|
for (let depth = 0; depth < 5; depth += 1) {
|
|
6172
6303
|
candidates.push(join10(dir, "assets", "unified_bank.json"));
|
|
6173
|
-
const parent =
|
|
6304
|
+
const parent = dirname11(dir);
|
|
6174
6305
|
if (parent === dir) break;
|
|
6175
6306
|
dir = parent;
|
|
6176
6307
|
}
|
|
@@ -6457,7 +6588,7 @@ async function runChallenge(options) {
|
|
|
6457
6588
|
|
|
6458
6589
|
// src/fingerprint/store.ts
|
|
6459
6590
|
import { mkdirSync as mkdirSync9, readFileSync as readFileSync11, renameSync as renameSync7, unlinkSync as unlinkSync7, writeFileSync as writeFileSync9 } from "node:fs";
|
|
6460
|
-
import { dirname as
|
|
6591
|
+
import { dirname as dirname12, join as join11 } from "node:path";
|
|
6461
6592
|
var STATUSES = /* @__PURE__ */ new Set(["identified", "doubtful", "failed"]);
|
|
6462
6593
|
function emptyFingerprintStore(bankBuiltAt2 = "") {
|
|
6463
6594
|
return { version: 1, bankBuiltAt: bankBuiltAt2, records: {} };
|
|
@@ -6600,7 +6731,7 @@ function readFingerprintStore(bankBuiltAt2 = "", file = fingerprintFilePath()) {
|
|
|
6600
6731
|
return parseFingerprintStore(raw, bankBuiltAt2);
|
|
6601
6732
|
}
|
|
6602
6733
|
function writeFingerprintStore(document, file = fingerprintFilePath()) {
|
|
6603
|
-
mkdirSync9(
|
|
6734
|
+
mkdirSync9(dirname12(file), { recursive: true });
|
|
6604
6735
|
const temporary = `${file}.${process.pid}.tmp`;
|
|
6605
6736
|
writeFileSync9(temporary, `${JSON.stringify(document, null, 2)}
|
|
6606
6737
|
`, "utf8");
|
|
@@ -7125,14 +7256,22 @@ function apply(ctx, config = {}) {
|
|
|
7125
7256
|
const settings = ctx.settings;
|
|
7126
7257
|
let piDescriptorCache;
|
|
7127
7258
|
let piDescriptorCached = false;
|
|
7128
|
-
|
|
7129
|
-
|
|
7259
|
+
let piDescriptorReading = false;
|
|
7260
|
+
const piDescriptor = (required = false, fresh = false) => {
|
|
7261
|
+
if (piDescriptorReading) {
|
|
7262
|
+
if (required) throw new HostReadError("the llm-pi-ai settings are being read");
|
|
7263
|
+
return void 0;
|
|
7264
|
+
}
|
|
7265
|
+
if (piDescriptorCached && !fresh) return piDescriptorCache;
|
|
7130
7266
|
let descriptor;
|
|
7131
7267
|
try {
|
|
7268
|
+
piDescriptorReading = true;
|
|
7132
7269
|
descriptor = settings.describe().find((entry) => entry.ns === PI_NS);
|
|
7133
7270
|
} catch {
|
|
7134
7271
|
if (required) throw new HostReadError("the llm-pi-ai settings could not be read");
|
|
7135
7272
|
return void 0;
|
|
7273
|
+
} finally {
|
|
7274
|
+
piDescriptorReading = false;
|
|
7136
7275
|
}
|
|
7137
7276
|
if (descriptor === void 0 || !isRecord(descriptor.value)) {
|
|
7138
7277
|
if (required) throw new HostReadError("the llm-pi-ai settings entry is unavailable");
|
|
@@ -7517,18 +7656,33 @@ function apply(ctx, config = {}) {
|
|
|
7517
7656
|
refreshHeaders();
|
|
7518
7657
|
refreshKeyScopes();
|
|
7519
7658
|
ctx.effect(() => () => stopHeaderRetries(), "dsh-model-think-level: header retries");
|
|
7659
|
+
const refreshModelConsumers = () => {
|
|
7660
|
+
refreshHeaders();
|
|
7661
|
+
refreshKeyScopes();
|
|
7662
|
+
const source = catalogueSource();
|
|
7663
|
+
if (source !== catalogueSourceStamp) {
|
|
7664
|
+
catalogueSourceStamp = source;
|
|
7665
|
+
invalidateListings();
|
|
7666
|
+
}
|
|
7667
|
+
scheduleKeyProbes();
|
|
7668
|
+
};
|
|
7669
|
+
let modelConsumerRefreshQueued = false;
|
|
7670
|
+
let modelConsumersDisposed = false;
|
|
7671
|
+
ctx.effect(() => () => {
|
|
7672
|
+
modelConsumersDisposed = true;
|
|
7673
|
+
});
|
|
7520
7674
|
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();
|
|
7675
|
+
if (ns !== PI_NS) return;
|
|
7676
|
+
if (!piDescriptorReading) {
|
|
7677
|
+
refreshModelConsumers();
|
|
7678
|
+
return;
|
|
7531
7679
|
}
|
|
7680
|
+
if (modelConsumerRefreshQueued) return;
|
|
7681
|
+
modelConsumerRefreshQueued = true;
|
|
7682
|
+
queueMicrotask(() => {
|
|
7683
|
+
modelConsumerRefreshQueued = false;
|
|
7684
|
+
if (!modelConsumersDisposed) refreshModelConsumers();
|
|
7685
|
+
});
|
|
7532
7686
|
});
|
|
7533
7687
|
overlaySource.keys = async ({ route, model }) => {
|
|
7534
7688
|
const scopes = keyScopes.current[route] ?? [];
|
|
@@ -7651,18 +7805,14 @@ function apply(ctx, config = {}) {
|
|
|
7651
7805
|
} catch (error) {
|
|
7652
7806
|
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
7807
|
}
|
|
7654
|
-
const
|
|
7655
|
-
|
|
7656
|
-
|
|
7657
|
-
|
|
7658
|
-
if (Object.keys(providers).length === 0) return;
|
|
7659
|
-
updateImageConfig((document) => syncImageConfig(document, providers));
|
|
7660
|
-
} catch {
|
|
7808
|
+
const imageConfiguration = new ImageConfiguration({
|
|
7809
|
+
describe: () => {
|
|
7810
|
+
const descriptor = piDescriptor(true, true);
|
|
7811
|
+
return descriptor === void 0 ? [] : [descriptor];
|
|
7661
7812
|
}
|
|
7662
|
-
};
|
|
7663
|
-
syncImageConfigNow();
|
|
7813
|
+
});
|
|
7664
7814
|
const imageGenerateDeps = {
|
|
7665
|
-
config: () =>
|
|
7815
|
+
config: () => imageConfiguration.forGeneration(),
|
|
7666
7816
|
resolveKey: async (ref) => {
|
|
7667
7817
|
try {
|
|
7668
7818
|
return await resolveCredential(ctx.get("credentials"), ref);
|
|
@@ -7670,22 +7820,12 @@ function apply(ctx, config = {}) {
|
|
|
7670
7820
|
return void 0;
|
|
7671
7821
|
}
|
|
7672
7822
|
},
|
|
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
7823
|
send: headerOverlayBypass(),
|
|
7676
|
-
persistLastGood: (
|
|
7824
|
+
persistLastGood: (_modelId, params, model) => {
|
|
7677
7825
|
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 {
|
|
7826
|
+
imageConfiguration.record(model, params);
|
|
7827
|
+
} catch (error) {
|
|
7828
|
+
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
7829
|
}
|
|
7690
7830
|
}
|
|
7691
7831
|
};
|
|
@@ -7698,7 +7838,7 @@ function apply(ctx, config = {}) {
|
|
|
7698
7838
|
registerImagegenCommand(
|
|
7699
7839
|
commandsService,
|
|
7700
7840
|
{
|
|
7701
|
-
modelIds: () =>
|
|
7841
|
+
modelIds: () => imageConfiguration.forGeneration().models.map((model) => model.id),
|
|
7702
7842
|
generate: generateImage,
|
|
7703
7843
|
generateDeps: imageGenerateDeps
|
|
7704
7844
|
}
|
|
@@ -7723,7 +7863,7 @@ function apply(ctx, config = {}) {
|
|
|
7723
7863
|
registered = registerGenerateImageTool(toolCtx, {
|
|
7724
7864
|
generateDeps: imageGenerateDeps,
|
|
7725
7865
|
attachments: toolCtx.attachments,
|
|
7726
|
-
readConfig: () =>
|
|
7866
|
+
readConfig: () => imageConfiguration.forGeneration()
|
|
7727
7867
|
});
|
|
7728
7868
|
imageToolState.name = registered.name;
|
|
7729
7869
|
imageToolState.error = void 0;
|
|
@@ -7734,8 +7874,13 @@ function apply(ctx, config = {}) {
|
|
|
7734
7874
|
}
|
|
7735
7875
|
};
|
|
7736
7876
|
const reconcile = () => {
|
|
7737
|
-
|
|
7738
|
-
|
|
7877
|
+
try {
|
|
7878
|
+
if (imageConfiguration.preferences().enabled) acquire();
|
|
7879
|
+
else release();
|
|
7880
|
+
} catch (error) {
|
|
7881
|
+
release();
|
|
7882
|
+
imageToolState.error = error instanceof Error ? error.message : String(error);
|
|
7883
|
+
}
|
|
7739
7884
|
};
|
|
7740
7885
|
reconcileImageTool = reconcile;
|
|
7741
7886
|
toolCtx.effect(
|
|
@@ -7753,7 +7898,18 @@ function apply(ctx, config = {}) {
|
|
|
7753
7898
|
() => promptCtx.systemPrompt.context({
|
|
7754
7899
|
name: "dsh-model-think-level:image-tool",
|
|
7755
7900
|
order: 61,
|
|
7756
|
-
text: () =>
|
|
7901
|
+
text: () => {
|
|
7902
|
+
reconcileImageTool?.();
|
|
7903
|
+
try {
|
|
7904
|
+
const config2 = imageConfiguration.forGeneration();
|
|
7905
|
+
if (config2.enabled && imageToolState.name === void 0) {
|
|
7906
|
+
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`;
|
|
7907
|
+
}
|
|
7908
|
+
return imageToolHint(config2, imageToolState.name);
|
|
7909
|
+
} catch (error) {
|
|
7910
|
+
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`;
|
|
7911
|
+
}
|
|
7912
|
+
}
|
|
7757
7913
|
}),
|
|
7758
7914
|
"dsh-model-think-level: image tool hint"
|
|
7759
7915
|
);
|
|
@@ -8999,10 +9155,39 @@ function apply(ctx, config = {}) {
|
|
|
8999
9155
|
return;
|
|
9000
9156
|
}
|
|
9001
9157
|
if (req.method === "GET") {
|
|
9002
|
-
|
|
9003
|
-
|
|
9158
|
+
reconcileImageTool?.();
|
|
9159
|
+
let config2;
|
|
9160
|
+
try {
|
|
9161
|
+
config2 = imageConfiguration.read();
|
|
9162
|
+
} catch (error) {
|
|
9163
|
+
let preferences;
|
|
9164
|
+
try {
|
|
9165
|
+
preferences = imageConfiguration.preferences();
|
|
9166
|
+
} catch {
|
|
9167
|
+
}
|
|
9168
|
+
sendJson(res, 503, {
|
|
9169
|
+
ok: false,
|
|
9170
|
+
error: error instanceof Error ? error.message : String(error),
|
|
9171
|
+
source: "live-settings",
|
|
9172
|
+
storageVersion: 3,
|
|
9173
|
+
...preferences === void 0 ? {} : {
|
|
9174
|
+
enabled: preferences.enabled,
|
|
9175
|
+
defaultModel: preferences.defaultModel,
|
|
9176
|
+
defaults: preferences.defaults,
|
|
9177
|
+
outputDir: preferences.outputDir
|
|
9178
|
+
},
|
|
9179
|
+
tool: {
|
|
9180
|
+
registered: imageToolState.name !== void 0,
|
|
9181
|
+
name: imageToolState.name ?? null,
|
|
9182
|
+
...imageToolState.error === void 0 ? {} : { error: imageToolState.error }
|
|
9183
|
+
}
|
|
9184
|
+
});
|
|
9185
|
+
return;
|
|
9186
|
+
}
|
|
9004
9187
|
sendJson(res, 200, {
|
|
9005
9188
|
ok: true,
|
|
9189
|
+
source: "live-settings",
|
|
9190
|
+
storageVersion: 3,
|
|
9006
9191
|
enabled: config2.enabled,
|
|
9007
9192
|
models: config2.models,
|
|
9008
9193
|
defaultModel: config2.defaultModel,
|
|
@@ -9025,7 +9210,7 @@ function apply(ctx, config = {}) {
|
|
|
9025
9210
|
return;
|
|
9026
9211
|
}
|
|
9027
9212
|
try {
|
|
9028
|
-
|
|
9213
|
+
imageConfiguration.update((document) => {
|
|
9029
9214
|
let next = document;
|
|
9030
9215
|
if (typeof body["enabled"] === "boolean") next = { ...next, enabled: body["enabled"] };
|
|
9031
9216
|
if (typeof body["defaultModel"] === "string" && body["defaultModel"].length > 0) {
|
|
@@ -9049,7 +9234,6 @@ function apply(ctx, config = {}) {
|
|
|
9049
9234
|
}
|
|
9050
9235
|
return next;
|
|
9051
9236
|
});
|
|
9052
|
-
syncImageConfigNow();
|
|
9053
9237
|
try {
|
|
9054
9238
|
reconcileImageTool?.();
|
|
9055
9239
|
} catch {
|