dsh-model-think-level 2.2.0 → 2.3.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 +18 -6
- package/lib/client/ConfigPane.d.ts +16 -5
- package/lib/client/KeyManager.d.ts +3 -0
- package/lib/client/auto-continue-pref.d.ts +33 -0
- package/lib/client/auto-continue-rules.d.ts +96 -0
- package/lib/client/image-config-client.d.ts +16 -1
- package/lib/client/index.d.ts +1 -0
- package/lib/client/injection/auto-continue-run.d.ts +53 -0
- package/lib/client/injection/config-button-seat.d.ts +2 -2
- package/lib/client/injection/image-card.d.ts +19 -7
- package/lib/client/injection/session-directory.d.ts +44 -0
- package/lib/client/locales.d.ts +19 -0
- package/lib/client/order-sync.d.ts +15 -4
- package/lib/client/provider-enabled.d.ts +11 -0
- package/lib/client/styles.d.ts +1 -1
- package/lib/client.js +161 -8
- package/lib/client.js.map +4 -4
- package/lib/image/config.d.ts +6 -0
- package/lib/image/deadline.d.ts +13 -0
- package/lib/image/generate.d.ts +8 -5
- package/lib/image/tool.d.ts +12 -3
- package/lib/index.d.ts +6 -0
- package/lib/index.js +436 -126
- package/lib/index.js.map +4 -4
- package/lib/order-store.d.ts +22 -0
- package/package.json +5 -3
package/lib/index.js
CHANGED
|
@@ -759,10 +759,28 @@ function writeUiOrder(document, file = uiOrderFilePath()) {
|
|
|
759
759
|
throw error;
|
|
760
760
|
}
|
|
761
761
|
}
|
|
762
|
-
function
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
return
|
|
762
|
+
function savedAtOf(patch) {
|
|
763
|
+
if (typeof patch !== "object" || patch === null) return void 0;
|
|
764
|
+
const at = patch["savedAt"];
|
|
765
|
+
return typeof at === "number" && Number.isFinite(at) && at > 0 ? at : void 0;
|
|
766
|
+
}
|
|
767
|
+
function sameUiOrderContent(left, right) {
|
|
768
|
+
const shape = (document) => JSON.stringify([
|
|
769
|
+
document.providerOrder,
|
|
770
|
+
document.modelOrder,
|
|
771
|
+
document.providersDisabled
|
|
772
|
+
]);
|
|
773
|
+
return shape(left) === shape(right);
|
|
774
|
+
}
|
|
775
|
+
function commitUiOrderGuarded(patch, at, file = uiOrderFilePath()) {
|
|
776
|
+
const current = readUiOrder(file);
|
|
777
|
+
const incoming = savedAtOf(patch);
|
|
778
|
+
const merged = mergeUiOrder(current, patch, at);
|
|
779
|
+
if (incoming !== void 0 && incoming <= current.savedAt && !sameUiOrderContent(merged, current)) {
|
|
780
|
+
return { document: current, stale: true };
|
|
781
|
+
}
|
|
782
|
+
writeUiOrder(merged, file);
|
|
783
|
+
return { document: merged, stale: false };
|
|
766
784
|
}
|
|
767
785
|
|
|
768
786
|
// src/multi-key-listing.ts
|
|
@@ -3298,7 +3316,7 @@ function makeWrapper(base, entries) {
|
|
|
3298
3316
|
const restKey = kind === "unserved" ? restKeyOf(override.route, candidate.ref, model) : restKeyOf(override.route, candidate.ref);
|
|
3299
3317
|
keyRest.note(restKey, kind, retryAfterMs === void 0 ? void 0 : { retryAfterMs });
|
|
3300
3318
|
};
|
|
3301
|
-
const
|
|
3319
|
+
const callerAborted = () => init?.signal?.aborted === true || typeof Request !== "undefined" && input instanceof Request && input.signal.aborted;
|
|
3302
3320
|
if (keyCandidates.length <= 1) {
|
|
3303
3321
|
const solo = keyCandidates[0];
|
|
3304
3322
|
const response = await attempt(solo?.value);
|
|
@@ -3324,7 +3342,7 @@ function makeWrapper(base, entries) {
|
|
|
3324
3342
|
try {
|
|
3325
3343
|
response = await attempt(candidate.value);
|
|
3326
3344
|
} catch (error) {
|
|
3327
|
-
if (
|
|
3345
|
+
if (callerAborted()) throw error;
|
|
3328
3346
|
lastFailure = error;
|
|
3329
3347
|
keyRest.note(restKeyOf(override.route, candidate.ref), "refused");
|
|
3330
3348
|
continue;
|
|
@@ -3966,6 +3984,7 @@ function imageConfigFilePath(home = harnessHome()) {
|
|
|
3966
3984
|
function emptyImageConfig(now = () => /* @__PURE__ */ new Date()) {
|
|
3967
3985
|
return {
|
|
3968
3986
|
version: 2,
|
|
3987
|
+
enabled: true,
|
|
3969
3988
|
syncedAt: now().toISOString(),
|
|
3970
3989
|
defaultModel: null,
|
|
3971
3990
|
defaults: { size: "1024x1024", quality: "high", n: 1, timeoutMs: 3e5 },
|
|
@@ -4021,6 +4040,9 @@ function parseImageConfig(raw, now = () => /* @__PURE__ */ new Date()) {
|
|
|
4021
4040
|
}
|
|
4022
4041
|
return {
|
|
4023
4042
|
version: 2,
|
|
4043
|
+
// A config written before the switch existed has no opinion on it: the
|
|
4044
|
+
// shipped behaviour was "on", so only an explicit false turns it off.
|
|
4045
|
+
enabled: raw["enabled"] !== false,
|
|
4024
4046
|
syncedAt: typeof raw["syncedAt"] === "string" ? raw["syncedAt"] : base.syncedAt,
|
|
4025
4047
|
defaultModel: typeof raw["defaultModel"] === "string" && raw["defaultModel"].length > 0 ? raw["defaultModel"] : null,
|
|
4026
4048
|
defaults,
|
|
@@ -4126,10 +4148,11 @@ function syncImageConfig(stored, providers, now = () => /* @__PURE__ */ new Date
|
|
|
4126
4148
|
};
|
|
4127
4149
|
});
|
|
4128
4150
|
const ids = new Set(models.map((model) => model.id));
|
|
4151
|
+
const keepDefault = stored.defaultModel !== null && (models.length === 0 || ids.has(stored.defaultModel));
|
|
4129
4152
|
return {
|
|
4130
4153
|
...stored,
|
|
4131
4154
|
syncedAt: now().toISOString(),
|
|
4132
|
-
defaultModel:
|
|
4155
|
+
defaultModel: keepDefault ? stored.defaultModel : null,
|
|
4133
4156
|
models
|
|
4134
4157
|
};
|
|
4135
4158
|
}
|
|
@@ -4166,6 +4189,82 @@ function updateImageConfig(mutate, file = imageConfigFilePath()) {
|
|
|
4166
4189
|
// src/image/generate.ts
|
|
4167
4190
|
import { mkdirSync as mkdirSync5, readFileSync as readFileSync6, writeFileSync as writeFileSync5, existsSync as existsSync2, statSync } from "node:fs";
|
|
4168
4191
|
import { dirname as dirname6, join as join6, resolve as resolve2 } from "node:path";
|
|
4192
|
+
|
|
4193
|
+
// src/image/deadline.ts
|
|
4194
|
+
function createImageDeadline(timeoutMs, caller, errorFor) {
|
|
4195
|
+
const controller = new AbortController();
|
|
4196
|
+
const expiresAt = Date.now() + timeoutMs;
|
|
4197
|
+
let timer;
|
|
4198
|
+
const forwardAbort = () => {
|
|
4199
|
+
controller.abort(caller?.reason);
|
|
4200
|
+
};
|
|
4201
|
+
const cleanup = () => {
|
|
4202
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
4203
|
+
timer = void 0;
|
|
4204
|
+
caller?.removeEventListener("abort", forwardAbort);
|
|
4205
|
+
};
|
|
4206
|
+
controller.signal.addEventListener("abort", cleanup, { once: true });
|
|
4207
|
+
if (caller?.aborted === true) forwardAbort();
|
|
4208
|
+
else {
|
|
4209
|
+
caller?.addEventListener("abort", forwardAbort, { once: true });
|
|
4210
|
+
timer = setTimeout(() => controller.abort(new DOMException("Image deadline exceeded", "TimeoutError")), timeoutMs);
|
|
4211
|
+
}
|
|
4212
|
+
const check = () => {
|
|
4213
|
+
if (caller?.aborted === true) forwardAbort();
|
|
4214
|
+
if (!controller.signal.aborted && Date.now() >= expiresAt) {
|
|
4215
|
+
controller.abort(new DOMException("Image deadline exceeded", "TimeoutError"));
|
|
4216
|
+
}
|
|
4217
|
+
if (controller.signal.aborted) throw errorFor(caller?.aborted === true ? "aborted" : "timeout");
|
|
4218
|
+
};
|
|
4219
|
+
return {
|
|
4220
|
+
signal: controller.signal,
|
|
4221
|
+
check,
|
|
4222
|
+
run: (work) => new Promise((resolve3, reject) => {
|
|
4223
|
+
let settled = false;
|
|
4224
|
+
const finish = (done) => {
|
|
4225
|
+
if (settled) return;
|
|
4226
|
+
settled = true;
|
|
4227
|
+
controller.signal.removeEventListener("abort", onAbort);
|
|
4228
|
+
done();
|
|
4229
|
+
};
|
|
4230
|
+
const onAbort = () => {
|
|
4231
|
+
try {
|
|
4232
|
+
check();
|
|
4233
|
+
} catch (error) {
|
|
4234
|
+
finish(() => reject(error));
|
|
4235
|
+
}
|
|
4236
|
+
};
|
|
4237
|
+
try {
|
|
4238
|
+
check();
|
|
4239
|
+
controller.signal.addEventListener("abort", onAbort, { once: true });
|
|
4240
|
+
check();
|
|
4241
|
+
Promise.resolve(work()).then((value) => {
|
|
4242
|
+
try {
|
|
4243
|
+
check();
|
|
4244
|
+
finish(() => resolve3(value));
|
|
4245
|
+
} catch (error) {
|
|
4246
|
+
finish(() => reject(error));
|
|
4247
|
+
}
|
|
4248
|
+
}, (error) => {
|
|
4249
|
+
try {
|
|
4250
|
+
check();
|
|
4251
|
+
} catch (cancelled) {
|
|
4252
|
+
error = cancelled;
|
|
4253
|
+
}
|
|
4254
|
+
finish(() => reject(error));
|
|
4255
|
+
});
|
|
4256
|
+
} catch (error) {
|
|
4257
|
+
finish(() => reject(error));
|
|
4258
|
+
}
|
|
4259
|
+
}),
|
|
4260
|
+
dispose: () => {
|
|
4261
|
+
cleanup();
|
|
4262
|
+
controller.signal.removeEventListener("abort", cleanup);
|
|
4263
|
+
}
|
|
4264
|
+
};
|
|
4265
|
+
}
|
|
4266
|
+
|
|
4267
|
+
// src/image/generate.ts
|
|
4169
4268
|
var GenerateError = class extends Error {
|
|
4170
4269
|
kind;
|
|
4171
4270
|
status;
|
|
@@ -4196,11 +4295,43 @@ function sanitize(text, key) {
|
|
|
4196
4295
|
out = out.replace(/(?:Bearer\s+|sk-)[A-Za-z0-9_\-=.]{8,}/g, "***");
|
|
4197
4296
|
return out;
|
|
4198
4297
|
}
|
|
4199
|
-
async function
|
|
4298
|
+
async function responseBytes(response, deadline, limit = Infinity) {
|
|
4299
|
+
deadline.check();
|
|
4300
|
+
const reader = response.body?.getReader();
|
|
4301
|
+
if (reader === void 0) return new Uint8Array();
|
|
4302
|
+
const chunks = [];
|
|
4303
|
+
let length = 0;
|
|
4304
|
+
let complete = false;
|
|
4200
4305
|
try {
|
|
4201
|
-
|
|
4202
|
-
|
|
4306
|
+
while (length < limit) {
|
|
4307
|
+
const { done, value } = await deadline.run(() => reader.read());
|
|
4308
|
+
if (done) {
|
|
4309
|
+
complete = true;
|
|
4310
|
+
break;
|
|
4311
|
+
}
|
|
4312
|
+
const chunk = value.subarray(0, limit - length);
|
|
4313
|
+
chunks.push(chunk);
|
|
4314
|
+
length += chunk.length;
|
|
4315
|
+
}
|
|
4316
|
+
deadline.check();
|
|
4317
|
+
const bytes = new Uint8Array(length);
|
|
4318
|
+
let offset = 0;
|
|
4319
|
+
for (const chunk of chunks) {
|
|
4320
|
+
bytes.set(chunk, offset);
|
|
4321
|
+
offset += chunk.length;
|
|
4322
|
+
}
|
|
4323
|
+
return bytes;
|
|
4324
|
+
} finally {
|
|
4325
|
+
if (!complete) void reader.cancel().catch(() => {
|
|
4326
|
+
});
|
|
4327
|
+
reader.releaseLock();
|
|
4328
|
+
}
|
|
4329
|
+
}
|
|
4330
|
+
async function boundedText(response, deadline, limit = 4096) {
|
|
4331
|
+
try {
|
|
4332
|
+
return new TextDecoder().decode(await responseBytes(response, deadline, limit));
|
|
4203
4333
|
} catch {
|
|
4334
|
+
deadline.check();
|
|
4204
4335
|
return "";
|
|
4205
4336
|
}
|
|
4206
4337
|
}
|
|
@@ -4329,13 +4460,13 @@ function resolveDestination(requested, fallbackDir, slug, now) {
|
|
|
4329
4460
|
if (!existsSync2(candidate)) return candidate;
|
|
4330
4461
|
}
|
|
4331
4462
|
}
|
|
4332
|
-
async function imagesOfAnswer(answer, send) {
|
|
4463
|
+
async function imagesOfAnswer(answer, send, deadline, key) {
|
|
4333
4464
|
if (typeof answer !== "object" || answer === null) throw new GenerateError("upstream", "\u4E0A\u6E38\u8FD4\u56DE\u4E86\u65E0\u6CD5\u8BC6\u522B\u7684\u5E94\u7B54\u7ED3\u6784");
|
|
4334
4465
|
const data = answer["data"];
|
|
4335
4466
|
if (!Array.isArray(data) || data.length === 0) {
|
|
4336
|
-
for (const
|
|
4337
|
-
const nested = answer[
|
|
4338
|
-
if (Array.isArray(nested) && nested.length > 0) return imagesOfAnswer({ data: nested }, send);
|
|
4467
|
+
for (const nestedKey of ["images", "output", "result"]) {
|
|
4468
|
+
const nested = answer[nestedKey];
|
|
4469
|
+
if (Array.isArray(nested) && nested.length > 0) return imagesOfAnswer({ data: nested }, send, deadline, key);
|
|
4339
4470
|
}
|
|
4340
4471
|
throw new GenerateError("upstream", "\u4E0A\u6E38\u5E94\u7B54\u7F3A\u5C11 data \u6570\u7EC4");
|
|
4341
4472
|
}
|
|
@@ -4347,29 +4478,50 @@ async function imagesOfAnswer(answer, send) {
|
|
|
4347
4478
|
images.push(record["b64_json"]);
|
|
4348
4479
|
continue;
|
|
4349
4480
|
}
|
|
4481
|
+
deadline.check();
|
|
4350
4482
|
if (typeof record["url"] === "string" && record["url"].length > 0) {
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
|
|
4356
|
-
|
|
4483
|
+
try {
|
|
4484
|
+
const response = await deadline.run(() => send(record["url"], { redirect: "follow", signal: deadline.signal }));
|
|
4485
|
+
if (!response.ok) throw new GenerateError("upstream", `\u4E0B\u8F7D\u4E0A\u6E38\u56FE\u7247\u5931\u8D25\uFF08${response.status}\uFF09`);
|
|
4486
|
+
const bytes = await responseBytes(response, deadline);
|
|
4487
|
+
let binary = "";
|
|
4488
|
+
for (let index = 0; index < bytes.length; index += 1) binary += String.fromCharCode(bytes[index]);
|
|
4489
|
+
deadline.check();
|
|
4490
|
+
images.push(btoa(binary));
|
|
4491
|
+
} catch (error) {
|
|
4492
|
+
deadline.check();
|
|
4493
|
+
if (error instanceof GenerateError) throw error;
|
|
4494
|
+
throw new GenerateError("upstream", `\u4E0B\u8F7D\u4E0A\u6E38\u56FE\u7247\u5931\u8D25\uFF1A${sanitize(error instanceof Error ? error.message : String(error), key)}`);
|
|
4495
|
+
}
|
|
4357
4496
|
continue;
|
|
4358
4497
|
}
|
|
4359
4498
|
throw new GenerateError("upstream", "\u4E0A\u6E38\u5E94\u7B54\u65E2\u65E0 b64_json \u4E5F\u65E0 url");
|
|
4360
4499
|
}
|
|
4361
4500
|
return images;
|
|
4362
4501
|
}
|
|
4363
|
-
function
|
|
4364
|
-
return signal
|
|
4502
|
+
function createGenerateDeadline(timeoutMs, signal) {
|
|
4503
|
+
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`));
|
|
4365
4504
|
}
|
|
4366
|
-
async function generateImage(deps, request) {
|
|
4505
|
+
async function generateImage(deps, request, sharedDeadline) {
|
|
4367
4506
|
const config = deps.config?.() ?? readImageConfig();
|
|
4507
|
+
const deadline = sharedDeadline ?? createGenerateDeadline(deps.timeoutMs ?? config.defaults.timeoutMs, request.signal);
|
|
4508
|
+
try {
|
|
4509
|
+
deadline.check();
|
|
4510
|
+
return await generateWithinDeadline(deps, request, config, deadline);
|
|
4511
|
+
} catch (error) {
|
|
4512
|
+
deadline.check();
|
|
4513
|
+
throw error;
|
|
4514
|
+
} finally {
|
|
4515
|
+
if (sharedDeadline === void 0) deadline.dispose();
|
|
4516
|
+
}
|
|
4517
|
+
}
|
|
4518
|
+
async function generateWithinDeadline(deps, request, config, deadline) {
|
|
4519
|
+
if (!config.enabled) throw new GenerateError("config", "\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");
|
|
4368
4520
|
const prompt = request.prompt.trim();
|
|
4369
4521
|
if (prompt.length === 0) throw new GenerateError("param", "\u63D0\u793A\u8BCD\u4E3A\u7A7A");
|
|
4370
4522
|
const model = resolveImageModel(config, request.model);
|
|
4371
4523
|
if (model === void 0) {
|
|
4372
|
-
throw new GenerateError("config", config.models.length === 0 ? "\u6CA1\u6709\u5DF2\u914D\u7F6E\u7684\u751F\u56FE\u6A21\u578B\u2014\u2014\u8BF7\u5230 \u6A21\u578B\u914D\u7F6E \u2192 \u8F93\u5165\u6A21\u6001 \u52FE\u9009\u300C\u751F\u56FE\u300D\uFF0C\u6216\u5728\u63D2\u4EF6\u914D\u7F6E\u9875\u6DFB\u52A0" : `\u751F\u56FE\u6A21\u578B\u4E0D\u5B58\u5728\u6216\u65E0\u6CD5\u552F\u4E00\u5B9A\u4F4D\uFF1A${request.model ?? config.defaultModel ??
|
|
4524
|
+
throw new GenerateError("config", config.models.length === 0 ? "\u6CA1\u6709\u5DF2\u914D\u7F6E\u7684\u751F\u56FE\u6A21\u578B\u2014\u2014\u8BF7\u5230 \u6A21\u578B\u914D\u7F6E \u2192 \u8F93\u5165\u6A21\u6001 \u52FE\u9009\u300C\u751F\u56FE\u300D\uFF0C\u6216\u5728\u63D2\u4EF6\u914D\u7F6E\u9875\u6DFB\u52A0" : `\u751F\u56FE\u6A21\u578B\u4E0D\u5B58\u5728\u6216\u65E0\u6CD5\u552F\u4E00\u5B9A\u4F4D\uFF1A${request.model ?? config.defaultModel ?? `(\u672A\u8BBE\u9ED8\u8BA4\uFF0C\u5019\u9009 ${config.models.map((candidate) => candidate.id).join("\u3001")})`}\u3002\u8BF7\u5E26\u4E0A model \u53C2\u6570\uFF0C\u6216\u5230\u63D2\u4EF6\u914D\u7F6E\u9875\u628A\u5176\u4E2D\u4E00\u4E2A\u8BBE\u4E3A\u9ED8\u8BA4\u6A21\u578B`);
|
|
4373
4525
|
}
|
|
4374
4526
|
if (model.api !== "images") {
|
|
4375
4527
|
throw new GenerateError("endpoint", `\u8BE5\u6A21\u578B\uFF08${model.id}\uFF09\u7684\u534F\u8BAE\u6CA1\u6709\u517C\u5BB9\u7684 images \u7AEF\u70B9\uFF0C\u751F\u6210\u5DF2\u7981\u7528`);
|
|
@@ -4378,7 +4530,7 @@ async function generateImage(deps, request) {
|
|
|
4378
4530
|
throw new GenerateError("config", `\u6A21\u578B ${model.id} \u6240\u5728\u4F9B\u5E94\u5546\u672A\u914D\u7F6E baseURL`);
|
|
4379
4531
|
}
|
|
4380
4532
|
const params = effectiveParams(config, model, request);
|
|
4381
|
-
const key = model.keyRef === void 0 ? void 0 : await deps.resolveKey(model.keyRef);
|
|
4533
|
+
const key = model.keyRef === void 0 ? void 0 : await deadline.run(() => deps.resolveKey(model.keyRef, deadline.signal));
|
|
4382
4534
|
if (model.keyRef !== void 0 && key === void 0) {
|
|
4383
4535
|
throw new GenerateError("auth", `\u8BE5 route\uFF08${model.route}\uFF09\u672A\u914D\u7F6E\u5BC6\u94A5\uFF08\u5F15\u7528 ${model.keyRef}\uFF09\u2014\u2014\u8BF7\u5230\u6A21\u578B\u914D\u7F6E\u9875\u586B\u5199`);
|
|
4384
4536
|
}
|
|
@@ -4387,6 +4539,7 @@ async function generateImage(deps, request) {
|
|
|
4387
4539
|
const referenceReads = [
|
|
4388
4540
|
...(request.referenceFiles ?? []).map((file) => ({ name: file.name, bytes: file.bytes, mime: file.mime })),
|
|
4389
4541
|
...(request.referenceImages ?? []).map((path) => {
|
|
4542
|
+
deadline.check();
|
|
4390
4543
|
const absolute = resolve2(path);
|
|
4391
4544
|
if (!existsSync2(absolute)) throw new GenerateError("reference", `\u53C2\u8003\u56FE\u4E0D\u5B58\u5728\u6216\u4E0D\u53EF\u8BFB\uFF1A${path}`);
|
|
4392
4545
|
try {
|
|
@@ -4403,10 +4556,8 @@ async function generateImage(deps, request) {
|
|
|
4403
4556
|
let count = params.n;
|
|
4404
4557
|
let lastFailure;
|
|
4405
4558
|
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
4406
|
-
|
|
4407
|
-
const
|
|
4408
|
-
const deadline = AbortSignal.timeout(timeoutMs);
|
|
4409
|
-
const signal = request.signal === void 0 ? deadline : AbortSignal.any([deadline, request.signal]);
|
|
4559
|
+
deadline.check();
|
|
4560
|
+
const signal = deadline.signal;
|
|
4410
4561
|
let response;
|
|
4411
4562
|
try {
|
|
4412
4563
|
if (referenceReads.length > 0) {
|
|
@@ -4419,7 +4570,7 @@ async function generateImage(deps, request) {
|
|
|
4419
4570
|
};
|
|
4420
4571
|
if (format !== "none") fields["output_format"] = "png";
|
|
4421
4572
|
const { body: body2, contentType } = buildMultipart(boundary, fields, referenceReads);
|
|
4422
|
-
response = await deps.send(`${base}/images/edits`, {
|
|
4573
|
+
response = await deadline.run(() => deps.send(`${base}/images/edits`, {
|
|
4423
4574
|
method: "POST",
|
|
4424
4575
|
headers: {
|
|
4425
4576
|
"content-type": contentType,
|
|
@@ -4427,7 +4578,7 @@ async function generateImage(deps, request) {
|
|
|
4427
4578
|
},
|
|
4428
4579
|
body: body2,
|
|
4429
4580
|
signal
|
|
4430
|
-
});
|
|
4581
|
+
}));
|
|
4431
4582
|
} else {
|
|
4432
4583
|
const payload = {
|
|
4433
4584
|
model: model.modelId,
|
|
@@ -4438,7 +4589,7 @@ async function generateImage(deps, request) {
|
|
|
4438
4589
|
};
|
|
4439
4590
|
if (format === "b64_json") payload["response_format"] = "b64_json";
|
|
4440
4591
|
if (format === "output_format") payload["output_format"] = "png";
|
|
4441
|
-
response = await deps.send(`${base}/images/generations`, {
|
|
4592
|
+
response = await deadline.run(() => deps.send(`${base}/images/generations`, {
|
|
4442
4593
|
method: "POST",
|
|
4443
4594
|
headers: {
|
|
4444
4595
|
"content-type": "application/json",
|
|
@@ -4446,30 +4597,33 @@ async function generateImage(deps, request) {
|
|
|
4446
4597
|
},
|
|
4447
4598
|
body: JSON.stringify(payload),
|
|
4448
4599
|
signal
|
|
4449
|
-
});
|
|
4600
|
+
}));
|
|
4450
4601
|
}
|
|
4451
4602
|
} catch (error) {
|
|
4603
|
+
deadline.check();
|
|
4452
4604
|
if (error instanceof GenerateError) throw error;
|
|
4453
|
-
if (callerAborted(request.signal)) throw new GenerateError("aborted", "\u751F\u6210\u5DF2\u53D6\u6D88");
|
|
4454
4605
|
const message = error instanceof Error ? error.message : String(error);
|
|
4455
|
-
if (/abort|timeout/i.test(message)) throw new GenerateError("timeout", `\u751F\u6210\u8D85\u65F6\uFF08${timeoutMs}ms\uFF09\u2014\u2014\u53EF\u5728\u914D\u7F6E\u9875\u8C03\u5927\u8D85\u65F6`);
|
|
4456
4606
|
throw new GenerateError("upstream", `\u65E0\u6CD5\u8FDE\u63A5 ${base}\uFF1A${sanitize(message, key)}`);
|
|
4457
4607
|
}
|
|
4458
4608
|
if (response.ok) {
|
|
4459
4609
|
let answer;
|
|
4460
4610
|
try {
|
|
4461
|
-
answer = JSON.parse(await
|
|
4611
|
+
answer = JSON.parse(new TextDecoder().decode(await responseBytes(response, deadline)));
|
|
4462
4612
|
} catch {
|
|
4613
|
+
deadline.check();
|
|
4463
4614
|
throw new GenerateError("upstream", "\u4E0A\u6E38\u5E94\u7B54\u4E0D\u662F\u5408\u6CD5 JSON");
|
|
4464
4615
|
}
|
|
4465
|
-
const payloads = await imagesOfAnswer(answer, deps.send);
|
|
4616
|
+
const payloads = await imagesOfAnswer(answer, deps.send, deadline, key);
|
|
4466
4617
|
const images = [];
|
|
4467
4618
|
for (const data of payloads) {
|
|
4619
|
+
deadline.check();
|
|
4468
4620
|
const filePath = resolveDestination(request.outputPath, outputDirOf(deps, config), promptSlug(prompt), now);
|
|
4469
4621
|
const bytes = Uint8Array.from(atob(data), (ch) => ch.charCodeAt(0));
|
|
4622
|
+
deadline.check();
|
|
4470
4623
|
writeFileSync5(filePath, bytes);
|
|
4471
4624
|
images.push({ filePath, data });
|
|
4472
4625
|
}
|
|
4626
|
+
deadline.check();
|
|
4473
4627
|
if (revisions.length > 0 && deps.persistLastGood !== void 0) {
|
|
4474
4628
|
deps.persistLastGood(model.id, {
|
|
4475
4629
|
...model.params,
|
|
@@ -4479,6 +4633,7 @@ async function generateImage(deps, request) {
|
|
|
4479
4633
|
maxN: model.params.maxN
|
|
4480
4634
|
});
|
|
4481
4635
|
}
|
|
4636
|
+
deadline.check();
|
|
4482
4637
|
return {
|
|
4483
4638
|
images,
|
|
4484
4639
|
mime: "image/png",
|
|
@@ -4488,7 +4643,7 @@ async function generateImage(deps, request) {
|
|
|
4488
4643
|
revisions: [...revisions]
|
|
4489
4644
|
};
|
|
4490
4645
|
}
|
|
4491
|
-
const bodyText = await boundedText(response);
|
|
4646
|
+
const bodyText = await boundedText(response, deadline);
|
|
4492
4647
|
let body;
|
|
4493
4648
|
try {
|
|
4494
4649
|
body = JSON.parse(bodyText);
|
|
@@ -4661,49 +4816,61 @@ function attachmentRefOf(id) {
|
|
|
4661
4816
|
var OUTPUT_SCHEMA = {
|
|
4662
4817
|
type: "object",
|
|
4663
4818
|
additionalProperties: false,
|
|
4819
|
+
required: ["images", "model", "prompt", "paramsUsed"],
|
|
4664
4820
|
properties: {
|
|
4665
4821
|
images: {
|
|
4666
4822
|
type: "array",
|
|
4667
|
-
required: true,
|
|
4668
4823
|
items: {
|
|
4669
4824
|
type: "object",
|
|
4670
4825
|
additionalProperties: false,
|
|
4826
|
+
required: ["attachment", "filePath"],
|
|
4671
4827
|
properties: {
|
|
4672
|
-
attachment: {
|
|
4673
|
-
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
width: { type: "integer"
|
|
4681
|
-
height: { type: "integer"
|
|
4682
|
-
|
|
4683
|
-
|
|
4684
|
-
|
|
4828
|
+
attachment: {
|
|
4829
|
+
type: "object",
|
|
4830
|
+
additionalProperties: false,
|
|
4831
|
+
required: ["attachmentId", "mediaType", "bytes", "width", "height"],
|
|
4832
|
+
properties: {
|
|
4833
|
+
attachmentId: { type: "string" },
|
|
4834
|
+
mediaType: { type: "string" },
|
|
4835
|
+
bytes: { type: "integer" },
|
|
4836
|
+
width: { type: "integer" },
|
|
4837
|
+
height: { type: "integer" },
|
|
4838
|
+
name: { type: "string" }
|
|
4839
|
+
}
|
|
4840
|
+
},
|
|
4841
|
+
filePath: { type: "string" }
|
|
4685
4842
|
}
|
|
4686
4843
|
}
|
|
4687
4844
|
},
|
|
4688
|
-
model: { type: "string"
|
|
4689
|
-
prompt: { type: "string"
|
|
4690
|
-
paramsUsed: {
|
|
4691
|
-
|
|
4692
|
-
|
|
4693
|
-
|
|
4694
|
-
|
|
4845
|
+
model: { type: "string" },
|
|
4846
|
+
prompt: { type: "string" },
|
|
4847
|
+
paramsUsed: {
|
|
4848
|
+
type: "object",
|
|
4849
|
+
additionalProperties: false,
|
|
4850
|
+
required: ["size", "quality", "n"],
|
|
4851
|
+
properties: {
|
|
4852
|
+
size: { type: "string" },
|
|
4853
|
+
quality: { type: "string" },
|
|
4854
|
+
n: { type: "integer" }
|
|
4855
|
+
}
|
|
4856
|
+
},
|
|
4695
4857
|
revisions: { type: "array", items: { type: "string" } }
|
|
4696
4858
|
}
|
|
4697
4859
|
};
|
|
4698
4860
|
var TOOL_PARAMETERS = {
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4861
|
+
type: "object",
|
|
4862
|
+
additionalProperties: false,
|
|
4863
|
+
required: ["prompt"],
|
|
4864
|
+
properties: {
|
|
4865
|
+
prompt: { type: "string", description: "\u5B8C\u6574\u753B\u9762\u63CF\u8FF0\uFF1A\u4E3B\u4F53\u3001\u6784\u56FE\u3001\u98CE\u683C\u3001\u5149\u7167\uFF0C\u4EE5\u53CA\u9700\u8981\u51FA\u73B0\u7684\u6587\u5B57\u3002" },
|
|
4866
|
+
model: { type: "string", description: "\u751F\u56FE\u6A21\u578B\uFF08route/modelId \u6216\u663E\u793A\u540D\uFF09\uFF1B\u7F3A\u7701\u7528\u914D\u7F6E\u9875\u7684\u9ED8\u8BA4\u6A21\u578B\uFF0C\u7528\u6237\u6CA1\u6709\u70B9\u540D\u65F6\u7701\u7565\u3002" },
|
|
4867
|
+
size: { type: "string", description: "\u5982 1024x1024\uFF1B\u7F3A\u7701\u53D6\u6A21\u578B\u884C \u2192 \u9ED8\u8BA4\u53C2\u6570\u3002\u7528\u6237\u6CA1\u6709\u70B9\u540D\u65F6\u7701\u7565\u3002" },
|
|
4868
|
+
quality: { type: "string", description: "\u5982 high\uFF1B\u7F3A\u7701\u53D6\u6A21\u578B\u884C \u2192 \u9ED8\u8BA4\u53C2\u6570\u3002\u7528\u6237\u6CA1\u6709\u70B9\u540D\u65F6\u7701\u7565\u3002" },
|
|
4869
|
+
n: { type: "integer", description: "\u751F\u6210\u5F20\u6570\uFF1B\u7F3A\u7701\u53D6\u6A21\u578B\u884C \u2192 \u9ED8\u8BA4\u53C2\u6570\uFF0C\u8D85\u8FC7\u8BE5\u6A21\u578B\u4E0A\u9650\u65F6\u62A5\u9519\u5E76\u5217\u51FA\u5141\u8BB8\u503C\u3002" },
|
|
4870
|
+
reference_images: { type: "array", items: { type: "string" }, description: "\u672C\u5730\u53C2\u8003\u56FE\u8DEF\u5F84\uFF1B\u975E\u7A7A\u8D70 /images/edits\u3002" },
|
|
4871
|
+
reference_attachments: { type: "array", items: { type: "string" }, description: "\u5BF9\u8BDD\u5185\u56FE\u7247\u7684\u9644\u4EF6 ID\uFF08sha256:\u2026 \u6216\u88F8\u6458\u8981\uFF09\uFF1B\u4E0E\u672C\u5730\u8DEF\u5F84\u53EF\u6DF7\u7528\u3002" },
|
|
4872
|
+
path: { type: "string", description: "\u843D\u76D8\u6587\u4EF6\uFF08\u4EE5 .png \u7ED3\u5C3E\uFF0C\u5B58\u5728\u5219\u81EA\u52A8\u52A0 -2 \u540E\u7F00\uFF0C\u4E0D\u8986\u76D6\uFF09\u6216\u76EE\u5F55\uFF1B\u7F3A\u7701 generated-images/<slug>-<ts>.png\u3002" }
|
|
4873
|
+
}
|
|
4707
4874
|
};
|
|
4708
4875
|
function parseArgs(raw) {
|
|
4709
4876
|
if (typeof raw !== "object" || raw === null) return "\u53C2\u6570\u4E0D\u662F\u5BF9\u8C61\uFF1Bprompt \u4E3A\u5FC5\u586B\u5B57\u7B26\u4E32";
|
|
@@ -4738,6 +4905,7 @@ function attachmentValueOf(ref) {
|
|
|
4738
4905
|
...ref.name === void 0 ? {} : { name: ref.name }
|
|
4739
4906
|
};
|
|
4740
4907
|
}
|
|
4908
|
+
var IMAGE_REPLY_HINT = "\u751F\u56FE\u6210\u529F\u540E\uFF0C\u6700\u7EC8\u56DE\u590D\u5FC5\u987B\u9010\u5F20\u76F4\u63A5\u5D4C\u5165\u6240\u6709\u751F\u6210\u7684\u56FE\u7247\uFF1A\u7528\u8FD4\u56DE\u7684\u771F\u5B9E filePath \u5199 \uFF0CWindows \u8DEF\u5F84\u7528 /\u3002\u4E0D\u80FD\u53EA\u8BF4\u201C\u56FE\u7247\u5DF2\u751F\u6210/\u5DF2\u9644\u5230\u5BF9\u8BDD\u201D\uFF0C\u4E0D\u80FD\u53EA\u653E\u6587\u4EF6\u94FE\u63A5\u6216\u4F9D\u8D56\u5DE5\u5177\u5361\u7247\u3002";
|
|
4741
4909
|
function summaryText(value) {
|
|
4742
4910
|
const paths = value.images.map((image) => image.filePath);
|
|
4743
4911
|
const lines = [
|
|
@@ -4745,7 +4913,10 @@ function summaryText(value) {
|
|
|
4745
4913
|
`size ${value.paramsUsed.size} \xB7 quality ${value.paramsUsed.quality} \xB7 n ${value.paramsUsed.n}`,
|
|
4746
4914
|
`\u843D\u76D8\uFF1A${paths.join("\u3001")}`,
|
|
4747
4915
|
value.revisions.length > 0 ? `\u53C2\u6570\u56DE\u9000\uFF1A${value.revisions.join("\u3001")}` : "",
|
|
4748
|
-
|
|
4916
|
+
IMAGE_REPLY_HINT,
|
|
4917
|
+
"\u53EF\u76F4\u63A5\u590D\u5236\u5230\u6700\u7EC8\u56DE\u590D\u7684\u56FE\u7247 Markdown\uFF1A",
|
|
4918
|
+
...paths.map((path, index) => `}>)`),
|
|
4919
|
+
"\u4E0D\u8981\u865A\u6784\u56FE\u7247 URL\uFF0C\u4E5F\u4E0D\u8981\u8C03\u7528 read\u3001glob \u6216\u5176\u4ED6\u5DE5\u5177\u53BB\u627E\u6216\u9A8C\u8BC1\u56FE\u7247\uFF1B\u76F4\u63A5\u4F7F\u7528\u4E0A\u9762\u7684\u771F\u5B9E\u8DEF\u5F84\u5D4C\u56FE\u3002"
|
|
4749
4920
|
];
|
|
4750
4921
|
return lines.filter((line) => line.length > 0).join("\n");
|
|
4751
4922
|
}
|
|
@@ -4753,7 +4924,7 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
4753
4924
|
const tools = ctx.tools;
|
|
4754
4925
|
const definitionFor = (name3) => ({
|
|
4755
4926
|
name: name3,
|
|
4756
|
-
description:
|
|
4927
|
+
description: `\u7528\u5F53\u524D\u914D\u7F6E\u7684\u751F\u56FE\u6A21\u578B\u751F\u6210\u56FE\u7247\u5E76\u76F4\u63A5\u9644\u5230\u5BF9\u8BDD\u3002\u7528\u6237\u8981\u753B\u56FE\u3001\u751F\u6210\u3001\u51FA\u56FE\u65F6\u8C03\u7528\uFF1B\u6539\u56FE\u8BF7\u5E26\u53C2\u8003\u56FE\u3002\u53C2\u6570\u4E00\u5F8B\u7F3A\u7701\uFF08\u8BFB\u914D\u7F6E\u9ED8\u8BA4\uFF09\uFF0C\u9664\u975E\u7528\u6237\u660E\u786E\u70B9\u540D\u3002\u4E0D\u8981\u865A\u6784\u56FE\u7247 URL\u3002${IMAGE_REPLY_HINT}`,
|
|
4757
4928
|
parameters: TOOL_PARAMETERS,
|
|
4758
4929
|
output: {
|
|
4759
4930
|
schema: OUTPUT_SCHEMA,
|
|
@@ -4773,40 +4944,54 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
4773
4944
|
async execute(rawArgs, exec) {
|
|
4774
4945
|
const parsed = parseArgs(rawArgs);
|
|
4775
4946
|
if (typeof parsed === "string") throw new Error(parsed);
|
|
4776
|
-
const
|
|
4777
|
-
|
|
4778
|
-
|
|
4779
|
-
|
|
4780
|
-
|
|
4781
|
-
|
|
4782
|
-
|
|
4783
|
-
|
|
4947
|
+
const config = deps.generateDeps.config?.() ?? deps.readConfig();
|
|
4948
|
+
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");
|
|
4949
|
+
const deadline = createGenerateDeadline(deps.generateDeps.timeoutMs ?? config.defaults.timeoutMs, exec.signal);
|
|
4950
|
+
try {
|
|
4951
|
+
deadline.check();
|
|
4952
|
+
const referenceFiles = [];
|
|
4953
|
+
for (const [index, id] of (parsed.referenceAttachments ?? []).entries()) {
|
|
4954
|
+
try {
|
|
4955
|
+
const stored = await deadline.run(() => deps.attachments.readImage(attachmentRefOf(id), deadline.signal));
|
|
4956
|
+
const name4 = stored.ref?.name ?? `reference-${index + 1}.png`;
|
|
4957
|
+
referenceFiles.push({ name: name4, mime: stored.ref?.mediaType ?? "image/png", bytes: stored.data });
|
|
4958
|
+
} catch (error) {
|
|
4959
|
+
deadline.check();
|
|
4960
|
+
throw new Error(`\u53C2\u8003\u56FE\uFF08\u9644\u4EF6 ${id}\uFF09\u4E0D\u53EF\u8BFB\uFF1A${error instanceof Error ? error.message : String(error)}`);
|
|
4961
|
+
}
|
|
4784
4962
|
}
|
|
4963
|
+
const result = await generateImage({ ...deps.generateDeps, config: () => config }, {
|
|
4964
|
+
prompt: parsed.prompt,
|
|
4965
|
+
...parsed.model === void 0 ? {} : { model: parsed.model },
|
|
4966
|
+
...parsed.size === void 0 ? {} : { size: parsed.size },
|
|
4967
|
+
...parsed.quality === void 0 ? {} : { quality: parsed.quality },
|
|
4968
|
+
...parsed.n === void 0 ? {} : { n: parsed.n },
|
|
4969
|
+
...parsed.referenceImages === void 0 ? {} : { referenceImages: parsed.referenceImages },
|
|
4970
|
+
...referenceFiles.length > 0 ? { referenceFiles } : {},
|
|
4971
|
+
...parsed.outputPath === void 0 ? {} : { outputPath: parsed.outputPath },
|
|
4972
|
+
signal: deadline.signal
|
|
4973
|
+
}, deadline);
|
|
4974
|
+
const images = [];
|
|
4975
|
+
for (const image of result.images) {
|
|
4976
|
+
deadline.check();
|
|
4977
|
+
const bytes = Uint8Array.from(atob(image.data), (ch) => ch.charCodeAt(0));
|
|
4978
|
+
const attachment = await deadline.run(() => deps.attachments.saveImage({ data: bytes, mediaType: result.mime, name: "generated-image" }, deadline.signal));
|
|
4979
|
+
images.push({ attachment: attachmentValueOf(attachment), filePath: image.filePath });
|
|
4980
|
+
}
|
|
4981
|
+
deadline.check();
|
|
4982
|
+
return {
|
|
4983
|
+
images,
|
|
4984
|
+
model: result.model,
|
|
4985
|
+
prompt: result.prompt,
|
|
4986
|
+
paramsUsed: result.paramsUsed,
|
|
4987
|
+
revisions: result.revisions
|
|
4988
|
+
};
|
|
4989
|
+
} catch (error) {
|
|
4990
|
+
deadline.check();
|
|
4991
|
+
throw error;
|
|
4992
|
+
} finally {
|
|
4993
|
+
deadline.dispose();
|
|
4785
4994
|
}
|
|
4786
|
-
const result = await generateImage(deps.generateDeps, {
|
|
4787
|
-
prompt: parsed.prompt,
|
|
4788
|
-
...parsed.model === void 0 ? {} : { model: parsed.model },
|
|
4789
|
-
...parsed.size === void 0 ? {} : { size: parsed.size },
|
|
4790
|
-
...parsed.quality === void 0 ? {} : { quality: parsed.quality },
|
|
4791
|
-
...parsed.n === void 0 ? {} : { n: parsed.n },
|
|
4792
|
-
...parsed.referenceImages === void 0 ? {} : { referenceImages: parsed.referenceImages },
|
|
4793
|
-
...referenceFiles.length > 0 ? { referenceFiles } : {},
|
|
4794
|
-
...parsed.outputPath === void 0 ? {} : { outputPath: parsed.outputPath },
|
|
4795
|
-
signal: exec.signal
|
|
4796
|
-
});
|
|
4797
|
-
const images = [];
|
|
4798
|
-
for (const image of result.images) {
|
|
4799
|
-
const bytes = Uint8Array.from(atob(image.data), (ch) => ch.charCodeAt(0));
|
|
4800
|
-
const attachment = await deps.attachments.saveImage({ data: bytes, mediaType: result.mime, name: "generated-image" });
|
|
4801
|
-
images.push({ attachment: attachmentValueOf(attachment), filePath: image.filePath });
|
|
4802
|
-
}
|
|
4803
|
-
return {
|
|
4804
|
-
images,
|
|
4805
|
-
model: result.model,
|
|
4806
|
-
prompt: result.prompt,
|
|
4807
|
-
paramsUsed: result.paramsUsed,
|
|
4808
|
-
revisions: result.revisions
|
|
4809
|
-
};
|
|
4810
4995
|
},
|
|
4811
4996
|
presentCall: (args) => {
|
|
4812
4997
|
const record = typeof args === "object" && args !== null ? args : {};
|
|
@@ -4850,12 +5035,16 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
4850
5035
|
};
|
|
4851
5036
|
}
|
|
4852
5037
|
function imageToolHint(config) {
|
|
5038
|
+
if (!config.enabled) return "";
|
|
4853
5039
|
if (config.models.length === 0) return "";
|
|
4854
|
-
const
|
|
5040
|
+
const defaults = `size ${config.defaults.size}\uFF0Cquality ${config.defaults.quality}\uFF0Cn ${config.defaults.n}`;
|
|
5041
|
+
const resolved = resolveImageModel(config);
|
|
5042
|
+
const opening = resolved === void 0 ? `\u751F\u56FE\u8BF7\u8C03\u7528 generate_image \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 generate_image \u5DE5\u5177\uFF08\u7F3A\u7701\u53C2\u6570\u5373\u53EF\uFF1A\u9ED8\u8BA4\u6A21\u578B ${resolved.id}\uFF0C${defaults}\uFF09\u3002`;
|
|
4855
5043
|
const lines = [
|
|
4856
|
-
|
|
4857
|
-
"\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",
|
|
4858
|
-
"\u4E0D\u8981\u81EA\u5DF1\u7F16\u9020\u56FE\u7247 URL\uFF0C\u4E5F\u4E0D\u8981\u7528\u522B\u7684\u5DE5\u5177\u627E\u751F\u6210\u7684\u56FE\u7247\u2014\u2014\u6210\u529F\u540E\
|
|
5044
|
+
opening,
|
|
5045
|
+
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",
|
|
5046
|
+
"\u4E0D\u8981\u81EA\u5DF1\u7F16\u9020\u56FE\u7247 URL\uFF0C\u4E5F\u4E0D\u8981\u7528\u522B\u7684\u5DE5\u5177\u627E\u751F\u6210\u7684\u56FE\u7247\u2014\u2014\u6210\u529F\u540E\u5DE5\u5177\u4F1A\u8FD4\u56DE\u9644\u4EF6\u548C\u771F\u5B9E filePath\u3002",
|
|
5047
|
+
IMAGE_REPLY_HINT
|
|
4859
5048
|
];
|
|
4860
5049
|
return lines.join("\n");
|
|
4861
5050
|
}
|
|
@@ -4893,6 +5082,40 @@ function markerRange(text) {
|
|
|
4893
5082
|
const lineEnd = text.indexOf("\n", endMarker);
|
|
4894
5083
|
return { start, end: lineEnd === -1 ? text.length : lineEnd + 1 };
|
|
4895
5084
|
}
|
|
5085
|
+
function linesOf(text) {
|
|
5086
|
+
const out = [];
|
|
5087
|
+
let start = 0;
|
|
5088
|
+
for (const line of text.split("\n")) {
|
|
5089
|
+
out.push({ text: line, start });
|
|
5090
|
+
start += line.length + 1;
|
|
5091
|
+
}
|
|
5092
|
+
return out;
|
|
5093
|
+
}
|
|
5094
|
+
function staleEntryRange(text) {
|
|
5095
|
+
const lines = linesOf(text);
|
|
5096
|
+
const ownId = new RegExp(`^\\s*-\\s+id:\\s*['"]?${IMAGE_MCP_PATCH_ID}['"]?\\s*$`);
|
|
5097
|
+
const anyId = /^\s*-\s+id:/;
|
|
5098
|
+
for (const [index, line] of lines.entries()) {
|
|
5099
|
+
if (!/^-(\s|$)/.test(line.text)) continue;
|
|
5100
|
+
let end = lines.length;
|
|
5101
|
+
for (let probe = index + 1; probe < lines.length; probe += 1) {
|
|
5102
|
+
const candidate = lines[probe];
|
|
5103
|
+
if (candidate.text.trim().length === 0) continue;
|
|
5104
|
+
if (!/^\s/.test(candidate.text)) {
|
|
5105
|
+
end = probe;
|
|
5106
|
+
break;
|
|
5107
|
+
}
|
|
5108
|
+
}
|
|
5109
|
+
const body = lines.slice(index + 1, end);
|
|
5110
|
+
const owned = /^-\s*insert:\s*$/.test(line.text) ? body.some((entry) => ownId.test(entry.text)) && !body.some((entry) => anyId.test(entry.text) && !ownId.test(entry.text)) : ownId.test(line.text);
|
|
5111
|
+
if (!owned) continue;
|
|
5112
|
+
return { start: line.start, end: end < lines.length ? lines[end].start : text.length };
|
|
5113
|
+
}
|
|
5114
|
+
return void 0;
|
|
5115
|
+
}
|
|
5116
|
+
function legacyRange(text) {
|
|
5117
|
+
return markerRange(text) ?? staleEntryRange(text);
|
|
5118
|
+
}
|
|
4896
5119
|
function validateAfterSplice(text) {
|
|
4897
5120
|
const begins = text.split(MARK_BEGIN).length - 1;
|
|
4898
5121
|
const ends = text.split(MARK_END).length - 1;
|
|
@@ -4911,7 +5134,7 @@ var ModifiedError = class extends Error {
|
|
|
4911
5134
|
};
|
|
4912
5135
|
function spliceOnce(patchFile, mtimeMs) {
|
|
4913
5136
|
const current = readFileSync7(patchFile, "utf8");
|
|
4914
|
-
const range =
|
|
5137
|
+
const range = legacyRange(current);
|
|
4915
5138
|
if (range === void 0) return;
|
|
4916
5139
|
let next = current.slice(0, range.start) + current.slice(range.end);
|
|
4917
5140
|
if (current.slice(range.end).trim().length === 0) next = next.replace(/\n+$/, "\n");
|
|
@@ -4942,7 +5165,7 @@ function removeLegacyImageMcpPatch(home, patchFile) {
|
|
|
4942
5165
|
} catch {
|
|
4943
5166
|
return void 0;
|
|
4944
5167
|
}
|
|
4945
|
-
if (
|
|
5168
|
+
if (legacyRange(text) === void 0) return void 0;
|
|
4946
5169
|
if (!text.includes(`- id: ${IMAGE_MCP_PATCH_ID}`)) {
|
|
4947
5170
|
return void 0;
|
|
4948
5171
|
}
|
|
@@ -5896,6 +6119,7 @@ var name = PLUGIN_ID;
|
|
|
5896
6119
|
var inject = ["settings", "llm"];
|
|
5897
6120
|
var PI_NS = PI_AI_NS;
|
|
5898
6121
|
var BOOT_RETRY_DELAYS_MS = [1e3, 2e3, 4e3, 8e3, 16e3, 3e4];
|
|
6122
|
+
var ROUTE_SWEEP_GRACE_MS = 3e4;
|
|
5899
6123
|
var PROBE_TIMEOUT_MS = 15e3;
|
|
5900
6124
|
var BALANCE_TIMEOUT_MS = 8e3;
|
|
5901
6125
|
var CREDENTIAL_IPC_BUDGET_MS = 1e4;
|
|
@@ -5945,6 +6169,7 @@ var Config = Schema.object({
|
|
|
5945
6169
|
modalityAutofill: Schema.boolean().default(true),
|
|
5946
6170
|
probeTimeoutMs: Schema.natural().min(1).default(PROBE_TIMEOUT_MS),
|
|
5947
6171
|
bootRetryDelaysMs: Schema.array(Schema.natural().min(1)).default([...BOOT_RETRY_DELAYS_MS]),
|
|
6172
|
+
routeSweepGraceMs: Schema.natural().default(ROUTE_SWEEP_GRACE_MS),
|
|
5948
6173
|
defaultGuard: Schema.boolean().default(true),
|
|
5949
6174
|
uaOverride: Schema.boolean().default(true)
|
|
5950
6175
|
});
|
|
@@ -6318,6 +6543,7 @@ function apply(ctx, config = {}) {
|
|
|
6318
6543
|
modalityAutofill: config.modalityAutofill !== false,
|
|
6319
6544
|
probeTimeoutMs: config.probeTimeoutMs ?? PROBE_TIMEOUT_MS,
|
|
6320
6545
|
bootRetryDelaysMs: config.bootRetryDelaysMs ?? [...BOOT_RETRY_DELAYS_MS],
|
|
6546
|
+
routeSweepGraceMs: config.routeSweepGraceMs ?? ROUTE_SWEEP_GRACE_MS,
|
|
6321
6547
|
defaultGuard: config.defaultGuard !== false,
|
|
6322
6548
|
uaOverride: config.uaOverride !== false
|
|
6323
6549
|
};
|
|
@@ -6500,9 +6726,33 @@ function apply(ctx, config = {}) {
|
|
|
6500
6726
|
if (key.startsWith(prefix)) listingCache.delete(key);
|
|
6501
6727
|
}
|
|
6502
6728
|
};
|
|
6729
|
+
const vanishedSince = /* @__PURE__ */ new Map();
|
|
6730
|
+
const sweepTimers = /* @__PURE__ */ new Set();
|
|
6731
|
+
ctx.effect(() => () => {
|
|
6732
|
+
for (const timer of sweepTimers) clearTimeout(timer);
|
|
6733
|
+
sweepTimers.clear();
|
|
6734
|
+
}, "dsh-model-think-level: route-sweep timers");
|
|
6735
|
+
let recheckTimer;
|
|
6736
|
+
let recheckAt = 0;
|
|
6737
|
+
const scheduleSweep = (at) => {
|
|
6738
|
+
if (recheckTimer !== void 0) {
|
|
6739
|
+
if (recheckAt <= at) return;
|
|
6740
|
+
clearTimeout(recheckTimer);
|
|
6741
|
+
sweepTimers.delete(recheckTimer);
|
|
6742
|
+
}
|
|
6743
|
+
recheckAt = at;
|
|
6744
|
+
const timer = setTimeout(() => {
|
|
6745
|
+
sweepTimers.delete(timer);
|
|
6746
|
+
if (recheckTimer === timer) recheckTimer = void 0;
|
|
6747
|
+
void Promise.resolve().then(() => sweepVanishedRoutes());
|
|
6748
|
+
}, Math.max(0, at - Date.now()));
|
|
6749
|
+
recheckTimer = timer;
|
|
6750
|
+
sweepTimers.add(timer);
|
|
6751
|
+
};
|
|
6503
6752
|
const sweepVanishedRoutes = () => {
|
|
6504
|
-
|
|
6505
|
-
|
|
6753
|
+
const section = piSection();
|
|
6754
|
+
if (!isRecord(section) || !isRecord(section["providers"])) return false;
|
|
6755
|
+
const live = section["providers"];
|
|
6506
6756
|
const indexFile = keyIndexFilePath();
|
|
6507
6757
|
let index;
|
|
6508
6758
|
try {
|
|
@@ -6510,14 +6760,35 @@ function apply(ctx, config = {}) {
|
|
|
6510
6760
|
} catch {
|
|
6511
6761
|
return true;
|
|
6512
6762
|
}
|
|
6763
|
+
for (const route of vanishedSince.keys()) if (route in live) vanishedSince.delete(route);
|
|
6513
6764
|
const stale = /* @__PURE__ */ new Set();
|
|
6514
6765
|
for (const route of Object.keys(index.providers)) if (!(route in live)) stale.add(route);
|
|
6515
6766
|
for (const route of Object.keys(index.endpoints ?? {})) if (!(route in live)) stale.add(route);
|
|
6516
6767
|
for (const route of Object.keys(index.balances ?? {})) if (!(route in live)) stale.add(route);
|
|
6517
6768
|
if (stale.size === 0) return true;
|
|
6769
|
+
const graceMs = resolved.routeSweepGraceMs;
|
|
6770
|
+
const now = Date.now();
|
|
6771
|
+
const due = /* @__PURE__ */ new Set();
|
|
6772
|
+
for (const route of stale) {
|
|
6773
|
+
const since = vanishedSince.get(route);
|
|
6774
|
+
if (since === void 0) {
|
|
6775
|
+
if (graceMs <= 0) due.add(route);
|
|
6776
|
+
else vanishedSince.set(route, now);
|
|
6777
|
+
} else if (now - since >= graceMs) {
|
|
6778
|
+
due.add(route);
|
|
6779
|
+
}
|
|
6780
|
+
}
|
|
6781
|
+
let earliest = Infinity;
|
|
6782
|
+
for (const route of stale) {
|
|
6783
|
+
if (due.has(route)) continue;
|
|
6784
|
+
const since = vanishedSince.get(route);
|
|
6785
|
+
if (since !== void 0) earliest = Math.min(earliest, since + graceMs);
|
|
6786
|
+
}
|
|
6787
|
+
if (earliest !== Infinity) scheduleSweep(earliest);
|
|
6788
|
+
if (due.size === 0) return true;
|
|
6518
6789
|
const survivors = /* @__PURE__ */ new Set();
|
|
6519
6790
|
for (const [route, entries] of Object.entries(index.providers)) {
|
|
6520
|
-
if (
|
|
6791
|
+
if (due.has(route)) continue;
|
|
6521
6792
|
for (const entry of entries) survivors.add(entry.ref);
|
|
6522
6793
|
}
|
|
6523
6794
|
for (const profile of Object.values(live)) {
|
|
@@ -6527,15 +6798,19 @@ function apply(ctx, config = {}) {
|
|
|
6527
6798
|
}
|
|
6528
6799
|
const credentials = ctx.get("credentials");
|
|
6529
6800
|
let swept = false;
|
|
6530
|
-
for (const route of
|
|
6801
|
+
for (const route of due) {
|
|
6531
6802
|
const refs = keyEntriesOf(index, route).map((entry) => entry.ref);
|
|
6803
|
+
const hadEndpoints = index.endpoints?.[route] !== void 0;
|
|
6804
|
+
const hadBalance = index.balances?.[route] !== void 0;
|
|
6532
6805
|
try {
|
|
6533
6806
|
updateKeyIndex((current) => withoutRoute(current, route), indexFile);
|
|
6534
6807
|
} catch (error) {
|
|
6535
6808
|
console.error(`[dsh-model-think-level] key route cleanup for "${route}" failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
6536
6809
|
continue;
|
|
6537
6810
|
}
|
|
6811
|
+
vanishedSince.delete(route);
|
|
6538
6812
|
swept = true;
|
|
6813
|
+
console.info(`[dsh-model-think-level] provider "${route}" left the settings document past its grace window; swept key-index metadata (${refs.length} key(s)${hadEndpoints ? ", extra endpoints" : ""}${hadBalance ? ", balance URL" : ""})`);
|
|
6539
6814
|
for (const ref of refs) {
|
|
6540
6815
|
forgetServes(route, ref);
|
|
6541
6816
|
const here = `${route}\0${ref}`;
|
|
@@ -6703,11 +6978,6 @@ function apply(ctx, config = {}) {
|
|
|
6703
6978
|
}
|
|
6704
6979
|
});
|
|
6705
6980
|
{
|
|
6706
|
-
const sweepTimers = /* @__PURE__ */ new Set();
|
|
6707
|
-
ctx.effect(() => () => {
|
|
6708
|
-
for (const timer of sweepTimers) clearTimeout(timer);
|
|
6709
|
-
sweepTimers.clear();
|
|
6710
|
-
}, "dsh-model-think-level: route-sweep boot retries");
|
|
6711
6981
|
const bootRouteSweep = (attempt) => {
|
|
6712
6982
|
void Promise.resolve().then(() => sweepVanishedRoutes()).then((ready) => {
|
|
6713
6983
|
if (ready || attempt >= resolved.bootRetryDelaysMs.length) return;
|
|
@@ -6847,6 +7117,7 @@ function apply(ctx, config = {}) {
|
|
|
6847
7117
|
try {
|
|
6848
7118
|
if (piSection() === void 0) return;
|
|
6849
7119
|
const providers = providersOf();
|
|
7120
|
+
if (Object.keys(providers).length === 0) return;
|
|
6850
7121
|
updateImageConfig((document) => syncImageConfig(document, providers));
|
|
6851
7122
|
} catch {
|
|
6852
7123
|
}
|
|
@@ -6894,19 +7165,47 @@ function apply(ctx, config = {}) {
|
|
|
6894
7165
|
generateDeps: imageGenerateDeps
|
|
6895
7166
|
}
|
|
6896
7167
|
);
|
|
6897
|
-
const imageToolState = { name: void 0 };
|
|
7168
|
+
const imageToolState = { name: void 0, error: void 0 };
|
|
7169
|
+
let reconcileImageTool;
|
|
6898
7170
|
ctx.inject(["tools", "attachments"], (toolCtx) => {
|
|
6899
|
-
|
|
6900
|
-
|
|
6901
|
-
|
|
7171
|
+
let registered;
|
|
7172
|
+
const release = () => {
|
|
7173
|
+
const current = registered;
|
|
7174
|
+
registered = void 0;
|
|
7175
|
+
imageToolState.name = void 0;
|
|
7176
|
+
if (current === void 0) return;
|
|
7177
|
+
try {
|
|
7178
|
+
current.dispose();
|
|
7179
|
+
} catch {
|
|
7180
|
+
}
|
|
7181
|
+
};
|
|
7182
|
+
const acquire = () => {
|
|
7183
|
+
if (registered !== void 0) return;
|
|
7184
|
+
try {
|
|
7185
|
+
registered = registerGenerateImageTool(toolCtx, {
|
|
6902
7186
|
generateDeps: imageGenerateDeps,
|
|
6903
7187
|
attachments: toolCtx.attachments,
|
|
6904
7188
|
readConfig: () => readImageConfig()
|
|
6905
7189
|
});
|
|
6906
|
-
imageToolState.name =
|
|
7190
|
+
imageToolState.name = registered.name;
|
|
7191
|
+
imageToolState.error = void 0;
|
|
7192
|
+
} catch (error) {
|
|
7193
|
+
imageToolState.name = void 0;
|
|
7194
|
+
imageToolState.error = error instanceof Error ? error.message : String(error);
|
|
7195
|
+
console.warn(`[dsh-model-think-level] \u5185\u5EFA\u751F\u56FE\u5DE5\u5177\u6CE8\u518C\u5931\u8D25\uFF08\u8BCA\u65AD\u8DEF\u7531 ${IMAGE_CONFIG_PATH} \u7684 tool.error \u4F1A\u5E26\u56DE\u8FD9\u6761\u539F\u56E0\uFF09\uFF1A${imageToolState.error}`);
|
|
7196
|
+
}
|
|
7197
|
+
};
|
|
7198
|
+
const reconcile = () => {
|
|
7199
|
+
if (readImageConfig().enabled) acquire();
|
|
7200
|
+
else release();
|
|
7201
|
+
};
|
|
7202
|
+
reconcileImageTool = reconcile;
|
|
7203
|
+
toolCtx.effect(
|
|
7204
|
+
() => {
|
|
7205
|
+
reconcile();
|
|
6907
7206
|
return () => {
|
|
6908
|
-
|
|
6909
|
-
|
|
7207
|
+
reconcileImageTool = void 0;
|
|
7208
|
+
release();
|
|
6910
7209
|
};
|
|
6911
7210
|
},
|
|
6912
7211
|
"dsh-model-think-level: generate_image tool"
|
|
@@ -7767,8 +8066,12 @@ function apply(ctx, config = {}) {
|
|
|
7767
8066
|
return;
|
|
7768
8067
|
}
|
|
7769
8068
|
try {
|
|
7770
|
-
const
|
|
7771
|
-
|
|
8069
|
+
const { document, stale } = commitUiOrderGuarded(body, Date.now());
|
|
8070
|
+
if (stale) {
|
|
8071
|
+
sendJson(res, 409, { ok: false, error: "stale", data: document });
|
|
8072
|
+
return;
|
|
8073
|
+
}
|
|
8074
|
+
sendJson(res, 200, { ok: true, data: document });
|
|
7772
8075
|
} catch (error) {
|
|
7773
8076
|
sendJson(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
|
|
7774
8077
|
}
|
|
@@ -8053,6 +8356,7 @@ function apply(ctx, config = {}) {
|
|
|
8053
8356
|
const config2 = readImageConfig();
|
|
8054
8357
|
sendJson(res, 200, {
|
|
8055
8358
|
ok: true,
|
|
8359
|
+
enabled: config2.enabled,
|
|
8056
8360
|
models: config2.models,
|
|
8057
8361
|
defaultModel: config2.defaultModel,
|
|
8058
8362
|
defaults: config2.defaults,
|
|
@@ -8062,7 +8366,8 @@ function apply(ctx, config = {}) {
|
|
|
8062
8366
|
// and the client's keyed image card both read this name back.
|
|
8063
8367
|
tool: {
|
|
8064
8368
|
registered: imageToolState.name !== void 0,
|
|
8065
|
-
name: imageToolState.name ?? null
|
|
8369
|
+
name: imageToolState.name ?? null,
|
|
8370
|
+
...imageToolState.error === void 0 ? {} : { error: imageToolState.error }
|
|
8066
8371
|
}
|
|
8067
8372
|
});
|
|
8068
8373
|
return;
|
|
@@ -8075,6 +8380,7 @@ function apply(ctx, config = {}) {
|
|
|
8075
8380
|
try {
|
|
8076
8381
|
updateImageConfig((document) => {
|
|
8077
8382
|
let next = document;
|
|
8383
|
+
if (typeof body["enabled"] === "boolean") next = { ...next, enabled: body["enabled"] };
|
|
8078
8384
|
if (typeof body["defaultModel"] === "string" && body["defaultModel"].length > 0) {
|
|
8079
8385
|
next = { ...next, defaultModel: body["defaultModel"] };
|
|
8080
8386
|
}
|
|
@@ -8097,6 +8403,10 @@ function apply(ctx, config = {}) {
|
|
|
8097
8403
|
return next;
|
|
8098
8404
|
});
|
|
8099
8405
|
syncImageConfigNow();
|
|
8406
|
+
try {
|
|
8407
|
+
reconcileImageTool?.();
|
|
8408
|
+
} catch {
|
|
8409
|
+
}
|
|
8100
8410
|
sendJson(res, 200, { ok: true });
|
|
8101
8411
|
} catch (error) {
|
|
8102
8412
|
sendJson(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
|