dsh-model-think-level 2.2.0 → 2.3.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 -5
- package/lib/client/ConfigPane.d.ts +4 -2
- package/lib/client/image-config-client.d.ts +6 -0
- package/lib/client/injection/image-card.d.ts +19 -7
- package/lib/client/locales.d.ts +2 -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 +16 -5
- package/lib/client.js.map +3 -3
- 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.js +348 -116
- package/lib/index.js.map +4 -4
- package/lib/order-store.d.ts +22 -0
- package/package.json +1 -1
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;
|
|
@@ -4126,10 +4144,11 @@ function syncImageConfig(stored, providers, now = () => /* @__PURE__ */ new Date
|
|
|
4126
4144
|
};
|
|
4127
4145
|
});
|
|
4128
4146
|
const ids = new Set(models.map((model) => model.id));
|
|
4147
|
+
const keepDefault = stored.defaultModel !== null && (models.length === 0 || ids.has(stored.defaultModel));
|
|
4129
4148
|
return {
|
|
4130
4149
|
...stored,
|
|
4131
4150
|
syncedAt: now().toISOString(),
|
|
4132
|
-
defaultModel:
|
|
4151
|
+
defaultModel: keepDefault ? stored.defaultModel : null,
|
|
4133
4152
|
models
|
|
4134
4153
|
};
|
|
4135
4154
|
}
|
|
@@ -4166,6 +4185,82 @@ function updateImageConfig(mutate, file = imageConfigFilePath()) {
|
|
|
4166
4185
|
// src/image/generate.ts
|
|
4167
4186
|
import { mkdirSync as mkdirSync5, readFileSync as readFileSync6, writeFileSync as writeFileSync5, existsSync as existsSync2, statSync } from "node:fs";
|
|
4168
4187
|
import { dirname as dirname6, join as join6, resolve as resolve2 } from "node:path";
|
|
4188
|
+
|
|
4189
|
+
// src/image/deadline.ts
|
|
4190
|
+
function createImageDeadline(timeoutMs, caller, errorFor) {
|
|
4191
|
+
const controller = new AbortController();
|
|
4192
|
+
const expiresAt = Date.now() + timeoutMs;
|
|
4193
|
+
let timer;
|
|
4194
|
+
const forwardAbort = () => {
|
|
4195
|
+
controller.abort(caller?.reason);
|
|
4196
|
+
};
|
|
4197
|
+
const cleanup = () => {
|
|
4198
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
4199
|
+
timer = void 0;
|
|
4200
|
+
caller?.removeEventListener("abort", forwardAbort);
|
|
4201
|
+
};
|
|
4202
|
+
controller.signal.addEventListener("abort", cleanup, { once: true });
|
|
4203
|
+
if (caller?.aborted === true) forwardAbort();
|
|
4204
|
+
else {
|
|
4205
|
+
caller?.addEventListener("abort", forwardAbort, { once: true });
|
|
4206
|
+
timer = setTimeout(() => controller.abort(new DOMException("Image deadline exceeded", "TimeoutError")), timeoutMs);
|
|
4207
|
+
}
|
|
4208
|
+
const check = () => {
|
|
4209
|
+
if (caller?.aborted === true) forwardAbort();
|
|
4210
|
+
if (!controller.signal.aborted && Date.now() >= expiresAt) {
|
|
4211
|
+
controller.abort(new DOMException("Image deadline exceeded", "TimeoutError"));
|
|
4212
|
+
}
|
|
4213
|
+
if (controller.signal.aborted) throw errorFor(caller?.aborted === true ? "aborted" : "timeout");
|
|
4214
|
+
};
|
|
4215
|
+
return {
|
|
4216
|
+
signal: controller.signal,
|
|
4217
|
+
check,
|
|
4218
|
+
run: (work) => new Promise((resolve3, reject) => {
|
|
4219
|
+
let settled = false;
|
|
4220
|
+
const finish = (done) => {
|
|
4221
|
+
if (settled) return;
|
|
4222
|
+
settled = true;
|
|
4223
|
+
controller.signal.removeEventListener("abort", onAbort);
|
|
4224
|
+
done();
|
|
4225
|
+
};
|
|
4226
|
+
const onAbort = () => {
|
|
4227
|
+
try {
|
|
4228
|
+
check();
|
|
4229
|
+
} catch (error) {
|
|
4230
|
+
finish(() => reject(error));
|
|
4231
|
+
}
|
|
4232
|
+
};
|
|
4233
|
+
try {
|
|
4234
|
+
check();
|
|
4235
|
+
controller.signal.addEventListener("abort", onAbort, { once: true });
|
|
4236
|
+
check();
|
|
4237
|
+
Promise.resolve(work()).then((value) => {
|
|
4238
|
+
try {
|
|
4239
|
+
check();
|
|
4240
|
+
finish(() => resolve3(value));
|
|
4241
|
+
} catch (error) {
|
|
4242
|
+
finish(() => reject(error));
|
|
4243
|
+
}
|
|
4244
|
+
}, (error) => {
|
|
4245
|
+
try {
|
|
4246
|
+
check();
|
|
4247
|
+
} catch (cancelled) {
|
|
4248
|
+
error = cancelled;
|
|
4249
|
+
}
|
|
4250
|
+
finish(() => reject(error));
|
|
4251
|
+
});
|
|
4252
|
+
} catch (error) {
|
|
4253
|
+
finish(() => reject(error));
|
|
4254
|
+
}
|
|
4255
|
+
}),
|
|
4256
|
+
dispose: () => {
|
|
4257
|
+
cleanup();
|
|
4258
|
+
controller.signal.removeEventListener("abort", cleanup);
|
|
4259
|
+
}
|
|
4260
|
+
};
|
|
4261
|
+
}
|
|
4262
|
+
|
|
4263
|
+
// src/image/generate.ts
|
|
4169
4264
|
var GenerateError = class extends Error {
|
|
4170
4265
|
kind;
|
|
4171
4266
|
status;
|
|
@@ -4196,11 +4291,43 @@ function sanitize(text, key) {
|
|
|
4196
4291
|
out = out.replace(/(?:Bearer\s+|sk-)[A-Za-z0-9_\-=.]{8,}/g, "***");
|
|
4197
4292
|
return out;
|
|
4198
4293
|
}
|
|
4199
|
-
async function
|
|
4294
|
+
async function responseBytes(response, deadline, limit = Infinity) {
|
|
4295
|
+
deadline.check();
|
|
4296
|
+
const reader = response.body?.getReader();
|
|
4297
|
+
if (reader === void 0) return new Uint8Array();
|
|
4298
|
+
const chunks = [];
|
|
4299
|
+
let length = 0;
|
|
4300
|
+
let complete = false;
|
|
4301
|
+
try {
|
|
4302
|
+
while (length < limit) {
|
|
4303
|
+
const { done, value } = await deadline.run(() => reader.read());
|
|
4304
|
+
if (done) {
|
|
4305
|
+
complete = true;
|
|
4306
|
+
break;
|
|
4307
|
+
}
|
|
4308
|
+
const chunk = value.subarray(0, limit - length);
|
|
4309
|
+
chunks.push(chunk);
|
|
4310
|
+
length += chunk.length;
|
|
4311
|
+
}
|
|
4312
|
+
deadline.check();
|
|
4313
|
+
const bytes = new Uint8Array(length);
|
|
4314
|
+
let offset = 0;
|
|
4315
|
+
for (const chunk of chunks) {
|
|
4316
|
+
bytes.set(chunk, offset);
|
|
4317
|
+
offset += chunk.length;
|
|
4318
|
+
}
|
|
4319
|
+
return bytes;
|
|
4320
|
+
} finally {
|
|
4321
|
+
if (!complete) void reader.cancel().catch(() => {
|
|
4322
|
+
});
|
|
4323
|
+
reader.releaseLock();
|
|
4324
|
+
}
|
|
4325
|
+
}
|
|
4326
|
+
async function boundedText(response, deadline, limit = 4096) {
|
|
4200
4327
|
try {
|
|
4201
|
-
|
|
4202
|
-
return text.length > limit ? text.slice(0, limit) : text;
|
|
4328
|
+
return new TextDecoder().decode(await responseBytes(response, deadline, limit));
|
|
4203
4329
|
} catch {
|
|
4330
|
+
deadline.check();
|
|
4204
4331
|
return "";
|
|
4205
4332
|
}
|
|
4206
4333
|
}
|
|
@@ -4329,13 +4456,13 @@ function resolveDestination(requested, fallbackDir, slug, now) {
|
|
|
4329
4456
|
if (!existsSync2(candidate)) return candidate;
|
|
4330
4457
|
}
|
|
4331
4458
|
}
|
|
4332
|
-
async function imagesOfAnswer(answer, send) {
|
|
4459
|
+
async function imagesOfAnswer(answer, send, deadline, key) {
|
|
4333
4460
|
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
4461
|
const data = answer["data"];
|
|
4335
4462
|
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);
|
|
4463
|
+
for (const nestedKey of ["images", "output", "result"]) {
|
|
4464
|
+
const nested = answer[nestedKey];
|
|
4465
|
+
if (Array.isArray(nested) && nested.length > 0) return imagesOfAnswer({ data: nested }, send, deadline, key);
|
|
4339
4466
|
}
|
|
4340
4467
|
throw new GenerateError("upstream", "\u4E0A\u6E38\u5E94\u7B54\u7F3A\u5C11 data \u6570\u7EC4");
|
|
4341
4468
|
}
|
|
@@ -4347,29 +4474,49 @@ async function imagesOfAnswer(answer, send) {
|
|
|
4347
4474
|
images.push(record["b64_json"]);
|
|
4348
4475
|
continue;
|
|
4349
4476
|
}
|
|
4477
|
+
deadline.check();
|
|
4350
4478
|
if (typeof record["url"] === "string" && record["url"].length > 0) {
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
|
|
4356
|
-
|
|
4479
|
+
try {
|
|
4480
|
+
const response = await deadline.run(() => send(record["url"], { redirect: "follow", signal: deadline.signal }));
|
|
4481
|
+
if (!response.ok) throw new GenerateError("upstream", `\u4E0B\u8F7D\u4E0A\u6E38\u56FE\u7247\u5931\u8D25\uFF08${response.status}\uFF09`);
|
|
4482
|
+
const bytes = await responseBytes(response, deadline);
|
|
4483
|
+
let binary = "";
|
|
4484
|
+
for (let index = 0; index < bytes.length; index += 1) binary += String.fromCharCode(bytes[index]);
|
|
4485
|
+
deadline.check();
|
|
4486
|
+
images.push(btoa(binary));
|
|
4487
|
+
} catch (error) {
|
|
4488
|
+
deadline.check();
|
|
4489
|
+
if (error instanceof GenerateError) throw error;
|
|
4490
|
+
throw new GenerateError("upstream", `\u4E0B\u8F7D\u4E0A\u6E38\u56FE\u7247\u5931\u8D25\uFF1A${sanitize(error instanceof Error ? error.message : String(error), key)}`);
|
|
4491
|
+
}
|
|
4357
4492
|
continue;
|
|
4358
4493
|
}
|
|
4359
4494
|
throw new GenerateError("upstream", "\u4E0A\u6E38\u5E94\u7B54\u65E2\u65E0 b64_json \u4E5F\u65E0 url");
|
|
4360
4495
|
}
|
|
4361
4496
|
return images;
|
|
4362
4497
|
}
|
|
4363
|
-
function
|
|
4364
|
-
return signal
|
|
4498
|
+
function createGenerateDeadline(timeoutMs, signal) {
|
|
4499
|
+
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
4500
|
}
|
|
4366
|
-
async function generateImage(deps, request) {
|
|
4501
|
+
async function generateImage(deps, request, sharedDeadline) {
|
|
4367
4502
|
const config = deps.config?.() ?? readImageConfig();
|
|
4503
|
+
const deadline = sharedDeadline ?? createGenerateDeadline(deps.timeoutMs ?? config.defaults.timeoutMs, request.signal);
|
|
4504
|
+
try {
|
|
4505
|
+
deadline.check();
|
|
4506
|
+
return await generateWithinDeadline(deps, request, config, deadline);
|
|
4507
|
+
} catch (error) {
|
|
4508
|
+
deadline.check();
|
|
4509
|
+
throw error;
|
|
4510
|
+
} finally {
|
|
4511
|
+
if (sharedDeadline === void 0) deadline.dispose();
|
|
4512
|
+
}
|
|
4513
|
+
}
|
|
4514
|
+
async function generateWithinDeadline(deps, request, config, deadline) {
|
|
4368
4515
|
const prompt = request.prompt.trim();
|
|
4369
4516
|
if (prompt.length === 0) throw new GenerateError("param", "\u63D0\u793A\u8BCD\u4E3A\u7A7A");
|
|
4370
4517
|
const model = resolveImageModel(config, request.model);
|
|
4371
4518
|
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 ??
|
|
4519
|
+
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
4520
|
}
|
|
4374
4521
|
if (model.api !== "images") {
|
|
4375
4522
|
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 +4525,7 @@ async function generateImage(deps, request) {
|
|
|
4378
4525
|
throw new GenerateError("config", `\u6A21\u578B ${model.id} \u6240\u5728\u4F9B\u5E94\u5546\u672A\u914D\u7F6E baseURL`);
|
|
4379
4526
|
}
|
|
4380
4527
|
const params = effectiveParams(config, model, request);
|
|
4381
|
-
const key = model.keyRef === void 0 ? void 0 : await deps.resolveKey(model.keyRef);
|
|
4528
|
+
const key = model.keyRef === void 0 ? void 0 : await deadline.run(() => deps.resolveKey(model.keyRef, deadline.signal));
|
|
4382
4529
|
if (model.keyRef !== void 0 && key === void 0) {
|
|
4383
4530
|
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
4531
|
}
|
|
@@ -4387,6 +4534,7 @@ async function generateImage(deps, request) {
|
|
|
4387
4534
|
const referenceReads = [
|
|
4388
4535
|
...(request.referenceFiles ?? []).map((file) => ({ name: file.name, bytes: file.bytes, mime: file.mime })),
|
|
4389
4536
|
...(request.referenceImages ?? []).map((path) => {
|
|
4537
|
+
deadline.check();
|
|
4390
4538
|
const absolute = resolve2(path);
|
|
4391
4539
|
if (!existsSync2(absolute)) throw new GenerateError("reference", `\u53C2\u8003\u56FE\u4E0D\u5B58\u5728\u6216\u4E0D\u53EF\u8BFB\uFF1A${path}`);
|
|
4392
4540
|
try {
|
|
@@ -4403,10 +4551,8 @@ async function generateImage(deps, request) {
|
|
|
4403
4551
|
let count = params.n;
|
|
4404
4552
|
let lastFailure;
|
|
4405
4553
|
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]);
|
|
4554
|
+
deadline.check();
|
|
4555
|
+
const signal = deadline.signal;
|
|
4410
4556
|
let response;
|
|
4411
4557
|
try {
|
|
4412
4558
|
if (referenceReads.length > 0) {
|
|
@@ -4419,7 +4565,7 @@ async function generateImage(deps, request) {
|
|
|
4419
4565
|
};
|
|
4420
4566
|
if (format !== "none") fields["output_format"] = "png";
|
|
4421
4567
|
const { body: body2, contentType } = buildMultipart(boundary, fields, referenceReads);
|
|
4422
|
-
response = await deps.send(`${base}/images/edits`, {
|
|
4568
|
+
response = await deadline.run(() => deps.send(`${base}/images/edits`, {
|
|
4423
4569
|
method: "POST",
|
|
4424
4570
|
headers: {
|
|
4425
4571
|
"content-type": contentType,
|
|
@@ -4427,7 +4573,7 @@ async function generateImage(deps, request) {
|
|
|
4427
4573
|
},
|
|
4428
4574
|
body: body2,
|
|
4429
4575
|
signal
|
|
4430
|
-
});
|
|
4576
|
+
}));
|
|
4431
4577
|
} else {
|
|
4432
4578
|
const payload = {
|
|
4433
4579
|
model: model.modelId,
|
|
@@ -4438,7 +4584,7 @@ async function generateImage(deps, request) {
|
|
|
4438
4584
|
};
|
|
4439
4585
|
if (format === "b64_json") payload["response_format"] = "b64_json";
|
|
4440
4586
|
if (format === "output_format") payload["output_format"] = "png";
|
|
4441
|
-
response = await deps.send(`${base}/images/generations`, {
|
|
4587
|
+
response = await deadline.run(() => deps.send(`${base}/images/generations`, {
|
|
4442
4588
|
method: "POST",
|
|
4443
4589
|
headers: {
|
|
4444
4590
|
"content-type": "application/json",
|
|
@@ -4446,30 +4592,33 @@ async function generateImage(deps, request) {
|
|
|
4446
4592
|
},
|
|
4447
4593
|
body: JSON.stringify(payload),
|
|
4448
4594
|
signal
|
|
4449
|
-
});
|
|
4595
|
+
}));
|
|
4450
4596
|
}
|
|
4451
4597
|
} catch (error) {
|
|
4598
|
+
deadline.check();
|
|
4452
4599
|
if (error instanceof GenerateError) throw error;
|
|
4453
|
-
if (callerAborted(request.signal)) throw new GenerateError("aborted", "\u751F\u6210\u5DF2\u53D6\u6D88");
|
|
4454
4600
|
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
4601
|
throw new GenerateError("upstream", `\u65E0\u6CD5\u8FDE\u63A5 ${base}\uFF1A${sanitize(message, key)}`);
|
|
4457
4602
|
}
|
|
4458
4603
|
if (response.ok) {
|
|
4459
4604
|
let answer;
|
|
4460
4605
|
try {
|
|
4461
|
-
answer = JSON.parse(await
|
|
4606
|
+
answer = JSON.parse(new TextDecoder().decode(await responseBytes(response, deadline)));
|
|
4462
4607
|
} catch {
|
|
4608
|
+
deadline.check();
|
|
4463
4609
|
throw new GenerateError("upstream", "\u4E0A\u6E38\u5E94\u7B54\u4E0D\u662F\u5408\u6CD5 JSON");
|
|
4464
4610
|
}
|
|
4465
|
-
const payloads = await imagesOfAnswer(answer, deps.send);
|
|
4611
|
+
const payloads = await imagesOfAnswer(answer, deps.send, deadline, key);
|
|
4466
4612
|
const images = [];
|
|
4467
4613
|
for (const data of payloads) {
|
|
4614
|
+
deadline.check();
|
|
4468
4615
|
const filePath = resolveDestination(request.outputPath, outputDirOf(deps, config), promptSlug(prompt), now);
|
|
4469
4616
|
const bytes = Uint8Array.from(atob(data), (ch) => ch.charCodeAt(0));
|
|
4617
|
+
deadline.check();
|
|
4470
4618
|
writeFileSync5(filePath, bytes);
|
|
4471
4619
|
images.push({ filePath, data });
|
|
4472
4620
|
}
|
|
4621
|
+
deadline.check();
|
|
4473
4622
|
if (revisions.length > 0 && deps.persistLastGood !== void 0) {
|
|
4474
4623
|
deps.persistLastGood(model.id, {
|
|
4475
4624
|
...model.params,
|
|
@@ -4479,6 +4628,7 @@ async function generateImage(deps, request) {
|
|
|
4479
4628
|
maxN: model.params.maxN
|
|
4480
4629
|
});
|
|
4481
4630
|
}
|
|
4631
|
+
deadline.check();
|
|
4482
4632
|
return {
|
|
4483
4633
|
images,
|
|
4484
4634
|
mime: "image/png",
|
|
@@ -4488,7 +4638,7 @@ async function generateImage(deps, request) {
|
|
|
4488
4638
|
revisions: [...revisions]
|
|
4489
4639
|
};
|
|
4490
4640
|
}
|
|
4491
|
-
const bodyText = await boundedText(response);
|
|
4641
|
+
const bodyText = await boundedText(response, deadline);
|
|
4492
4642
|
let body;
|
|
4493
4643
|
try {
|
|
4494
4644
|
body = JSON.parse(bodyText);
|
|
@@ -4661,49 +4811,61 @@ function attachmentRefOf(id) {
|
|
|
4661
4811
|
var OUTPUT_SCHEMA = {
|
|
4662
4812
|
type: "object",
|
|
4663
4813
|
additionalProperties: false,
|
|
4814
|
+
required: ["images", "model", "prompt", "paramsUsed"],
|
|
4664
4815
|
properties: {
|
|
4665
4816
|
images: {
|
|
4666
4817
|
type: "array",
|
|
4667
|
-
required: true,
|
|
4668
4818
|
items: {
|
|
4669
4819
|
type: "object",
|
|
4670
4820
|
additionalProperties: false,
|
|
4821
|
+
required: ["attachment", "filePath"],
|
|
4671
4822
|
properties: {
|
|
4672
|
-
attachment: {
|
|
4673
|
-
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
width: { type: "integer"
|
|
4681
|
-
height: { type: "integer"
|
|
4682
|
-
|
|
4683
|
-
|
|
4684
|
-
|
|
4823
|
+
attachment: {
|
|
4824
|
+
type: "object",
|
|
4825
|
+
additionalProperties: false,
|
|
4826
|
+
required: ["attachmentId", "mediaType", "bytes", "width", "height"],
|
|
4827
|
+
properties: {
|
|
4828
|
+
attachmentId: { type: "string" },
|
|
4829
|
+
mediaType: { type: "string" },
|
|
4830
|
+
bytes: { type: "integer" },
|
|
4831
|
+
width: { type: "integer" },
|
|
4832
|
+
height: { type: "integer" },
|
|
4833
|
+
name: { type: "string" }
|
|
4834
|
+
}
|
|
4835
|
+
},
|
|
4836
|
+
filePath: { type: "string" }
|
|
4685
4837
|
}
|
|
4686
4838
|
}
|
|
4687
4839
|
},
|
|
4688
|
-
model: { type: "string"
|
|
4689
|
-
prompt: { type: "string"
|
|
4690
|
-
paramsUsed: {
|
|
4691
|
-
|
|
4692
|
-
|
|
4693
|
-
|
|
4694
|
-
|
|
4840
|
+
model: { type: "string" },
|
|
4841
|
+
prompt: { type: "string" },
|
|
4842
|
+
paramsUsed: {
|
|
4843
|
+
type: "object",
|
|
4844
|
+
additionalProperties: false,
|
|
4845
|
+
required: ["size", "quality", "n"],
|
|
4846
|
+
properties: {
|
|
4847
|
+
size: { type: "string" },
|
|
4848
|
+
quality: { type: "string" },
|
|
4849
|
+
n: { type: "integer" }
|
|
4850
|
+
}
|
|
4851
|
+
},
|
|
4695
4852
|
revisions: { type: "array", items: { type: "string" } }
|
|
4696
4853
|
}
|
|
4697
4854
|
};
|
|
4698
4855
|
var TOOL_PARAMETERS = {
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4856
|
+
type: "object",
|
|
4857
|
+
additionalProperties: false,
|
|
4858
|
+
required: ["prompt"],
|
|
4859
|
+
properties: {
|
|
4860
|
+
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" },
|
|
4861
|
+
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" },
|
|
4862
|
+
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" },
|
|
4863
|
+
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" },
|
|
4864
|
+
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" },
|
|
4865
|
+
reference_images: { type: "array", items: { type: "string" }, description: "\u672C\u5730\u53C2\u8003\u56FE\u8DEF\u5F84\uFF1B\u975E\u7A7A\u8D70 /images/edits\u3002" },
|
|
4866
|
+
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" },
|
|
4867
|
+
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" }
|
|
4868
|
+
}
|
|
4707
4869
|
};
|
|
4708
4870
|
function parseArgs(raw) {
|
|
4709
4871
|
if (typeof raw !== "object" || raw === null) return "\u53C2\u6570\u4E0D\u662F\u5BF9\u8C61\uFF1Bprompt \u4E3A\u5FC5\u586B\u5B57\u7B26\u4E32";
|
|
@@ -4738,6 +4900,7 @@ function attachmentValueOf(ref) {
|
|
|
4738
4900
|
...ref.name === void 0 ? {} : { name: ref.name }
|
|
4739
4901
|
};
|
|
4740
4902
|
}
|
|
4903
|
+
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
4904
|
function summaryText(value) {
|
|
4742
4905
|
const paths = value.images.map((image) => image.filePath);
|
|
4743
4906
|
const lines = [
|
|
@@ -4745,7 +4908,10 @@ function summaryText(value) {
|
|
|
4745
4908
|
`size ${value.paramsUsed.size} \xB7 quality ${value.paramsUsed.quality} \xB7 n ${value.paramsUsed.n}`,
|
|
4746
4909
|
`\u843D\u76D8\uFF1A${paths.join("\u3001")}`,
|
|
4747
4910
|
value.revisions.length > 0 ? `\u53C2\u6570\u56DE\u9000\uFF1A${value.revisions.join("\u3001")}` : "",
|
|
4748
|
-
|
|
4911
|
+
IMAGE_REPLY_HINT,
|
|
4912
|
+
"\u53EF\u76F4\u63A5\u590D\u5236\u5230\u6700\u7EC8\u56DE\u590D\u7684\u56FE\u7247 Markdown\uFF1A",
|
|
4913
|
+
...paths.map((path, index) => `}>)`),
|
|
4914
|
+
"\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
4915
|
];
|
|
4750
4916
|
return lines.filter((line) => line.length > 0).join("\n");
|
|
4751
4917
|
}
|
|
@@ -4753,7 +4919,7 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
4753
4919
|
const tools = ctx.tools;
|
|
4754
4920
|
const definitionFor = (name3) => ({
|
|
4755
4921
|
name: name3,
|
|
4756
|
-
description:
|
|
4922
|
+
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
4923
|
parameters: TOOL_PARAMETERS,
|
|
4758
4924
|
output: {
|
|
4759
4925
|
schema: OUTPUT_SCHEMA,
|
|
@@ -4773,40 +4939,53 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
4773
4939
|
async execute(rawArgs, exec) {
|
|
4774
4940
|
const parsed = parseArgs(rawArgs);
|
|
4775
4941
|
if (typeof parsed === "string") throw new Error(parsed);
|
|
4776
|
-
const
|
|
4777
|
-
|
|
4778
|
-
|
|
4779
|
-
|
|
4780
|
-
|
|
4781
|
-
|
|
4782
|
-
|
|
4783
|
-
|
|
4942
|
+
const config = deps.generateDeps.config?.() ?? deps.readConfig();
|
|
4943
|
+
const deadline = createGenerateDeadline(deps.generateDeps.timeoutMs ?? config.defaults.timeoutMs, exec.signal);
|
|
4944
|
+
try {
|
|
4945
|
+
deadline.check();
|
|
4946
|
+
const referenceFiles = [];
|
|
4947
|
+
for (const [index, id] of (parsed.referenceAttachments ?? []).entries()) {
|
|
4948
|
+
try {
|
|
4949
|
+
const stored = await deadline.run(() => deps.attachments.readImage(attachmentRefOf(id), deadline.signal));
|
|
4950
|
+
const name4 = stored.ref?.name ?? `reference-${index + 1}.png`;
|
|
4951
|
+
referenceFiles.push({ name: name4, mime: stored.ref?.mediaType ?? "image/png", bytes: stored.data });
|
|
4952
|
+
} catch (error) {
|
|
4953
|
+
deadline.check();
|
|
4954
|
+
throw new Error(`\u53C2\u8003\u56FE\uFF08\u9644\u4EF6 ${id}\uFF09\u4E0D\u53EF\u8BFB\uFF1A${error instanceof Error ? error.message : String(error)}`);
|
|
4955
|
+
}
|
|
4784
4956
|
}
|
|
4957
|
+
const result = await generateImage({ ...deps.generateDeps, config: () => config }, {
|
|
4958
|
+
prompt: parsed.prompt,
|
|
4959
|
+
...parsed.model === void 0 ? {} : { model: parsed.model },
|
|
4960
|
+
...parsed.size === void 0 ? {} : { size: parsed.size },
|
|
4961
|
+
...parsed.quality === void 0 ? {} : { quality: parsed.quality },
|
|
4962
|
+
...parsed.n === void 0 ? {} : { n: parsed.n },
|
|
4963
|
+
...parsed.referenceImages === void 0 ? {} : { referenceImages: parsed.referenceImages },
|
|
4964
|
+
...referenceFiles.length > 0 ? { referenceFiles } : {},
|
|
4965
|
+
...parsed.outputPath === void 0 ? {} : { outputPath: parsed.outputPath },
|
|
4966
|
+
signal: deadline.signal
|
|
4967
|
+
}, deadline);
|
|
4968
|
+
const images = [];
|
|
4969
|
+
for (const image of result.images) {
|
|
4970
|
+
deadline.check();
|
|
4971
|
+
const bytes = Uint8Array.from(atob(image.data), (ch) => ch.charCodeAt(0));
|
|
4972
|
+
const attachment = await deadline.run(() => deps.attachments.saveImage({ data: bytes, mediaType: result.mime, name: "generated-image" }, deadline.signal));
|
|
4973
|
+
images.push({ attachment: attachmentValueOf(attachment), filePath: image.filePath });
|
|
4974
|
+
}
|
|
4975
|
+
deadline.check();
|
|
4976
|
+
return {
|
|
4977
|
+
images,
|
|
4978
|
+
model: result.model,
|
|
4979
|
+
prompt: result.prompt,
|
|
4980
|
+
paramsUsed: result.paramsUsed,
|
|
4981
|
+
revisions: result.revisions
|
|
4982
|
+
};
|
|
4983
|
+
} catch (error) {
|
|
4984
|
+
deadline.check();
|
|
4985
|
+
throw error;
|
|
4986
|
+
} finally {
|
|
4987
|
+
deadline.dispose();
|
|
4785
4988
|
}
|
|
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
4989
|
},
|
|
4811
4990
|
presentCall: (args) => {
|
|
4812
4991
|
const record = typeof args === "object" && args !== null ? args : {};
|
|
@@ -4851,11 +5030,14 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
4851
5030
|
}
|
|
4852
5031
|
function imageToolHint(config) {
|
|
4853
5032
|
if (config.models.length === 0) return "";
|
|
4854
|
-
const
|
|
5033
|
+
const defaults = `size ${config.defaults.size}\uFF0Cquality ${config.defaults.quality}\uFF0Cn ${config.defaults.n}`;
|
|
5034
|
+
const resolved = resolveImageModel(config);
|
|
5035
|
+
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
5036
|
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\
|
|
5037
|
+
opening,
|
|
5038
|
+
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",
|
|
5039
|
+
"\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",
|
|
5040
|
+
IMAGE_REPLY_HINT
|
|
4859
5041
|
];
|
|
4860
5042
|
return lines.join("\n");
|
|
4861
5043
|
}
|
|
@@ -4893,6 +5075,40 @@ function markerRange(text) {
|
|
|
4893
5075
|
const lineEnd = text.indexOf("\n", endMarker);
|
|
4894
5076
|
return { start, end: lineEnd === -1 ? text.length : lineEnd + 1 };
|
|
4895
5077
|
}
|
|
5078
|
+
function linesOf(text) {
|
|
5079
|
+
const out = [];
|
|
5080
|
+
let start = 0;
|
|
5081
|
+
for (const line of text.split("\n")) {
|
|
5082
|
+
out.push({ text: line, start });
|
|
5083
|
+
start += line.length + 1;
|
|
5084
|
+
}
|
|
5085
|
+
return out;
|
|
5086
|
+
}
|
|
5087
|
+
function staleEntryRange(text) {
|
|
5088
|
+
const lines = linesOf(text);
|
|
5089
|
+
const ownId = new RegExp(`^\\s*-\\s+id:\\s*['"]?${IMAGE_MCP_PATCH_ID}['"]?\\s*$`);
|
|
5090
|
+
const anyId = /^\s*-\s+id:/;
|
|
5091
|
+
for (const [index, line] of lines.entries()) {
|
|
5092
|
+
if (!/^-(\s|$)/.test(line.text)) continue;
|
|
5093
|
+
let end = lines.length;
|
|
5094
|
+
for (let probe = index + 1; probe < lines.length; probe += 1) {
|
|
5095
|
+
const candidate = lines[probe];
|
|
5096
|
+
if (candidate.text.trim().length === 0) continue;
|
|
5097
|
+
if (!/^\s/.test(candidate.text)) {
|
|
5098
|
+
end = probe;
|
|
5099
|
+
break;
|
|
5100
|
+
}
|
|
5101
|
+
}
|
|
5102
|
+
const body = lines.slice(index + 1, end);
|
|
5103
|
+
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);
|
|
5104
|
+
if (!owned) continue;
|
|
5105
|
+
return { start: line.start, end: end < lines.length ? lines[end].start : text.length };
|
|
5106
|
+
}
|
|
5107
|
+
return void 0;
|
|
5108
|
+
}
|
|
5109
|
+
function legacyRange(text) {
|
|
5110
|
+
return markerRange(text) ?? staleEntryRange(text);
|
|
5111
|
+
}
|
|
4896
5112
|
function validateAfterSplice(text) {
|
|
4897
5113
|
const begins = text.split(MARK_BEGIN).length - 1;
|
|
4898
5114
|
const ends = text.split(MARK_END).length - 1;
|
|
@@ -4911,7 +5127,7 @@ var ModifiedError = class extends Error {
|
|
|
4911
5127
|
};
|
|
4912
5128
|
function spliceOnce(patchFile, mtimeMs) {
|
|
4913
5129
|
const current = readFileSync7(patchFile, "utf8");
|
|
4914
|
-
const range =
|
|
5130
|
+
const range = legacyRange(current);
|
|
4915
5131
|
if (range === void 0) return;
|
|
4916
5132
|
let next = current.slice(0, range.start) + current.slice(range.end);
|
|
4917
5133
|
if (current.slice(range.end).trim().length === 0) next = next.replace(/\n+$/, "\n");
|
|
@@ -4942,7 +5158,7 @@ function removeLegacyImageMcpPatch(home, patchFile) {
|
|
|
4942
5158
|
} catch {
|
|
4943
5159
|
return void 0;
|
|
4944
5160
|
}
|
|
4945
|
-
if (
|
|
5161
|
+
if (legacyRange(text) === void 0) return void 0;
|
|
4946
5162
|
if (!text.includes(`- id: ${IMAGE_MCP_PATCH_ID}`)) {
|
|
4947
5163
|
return void 0;
|
|
4948
5164
|
}
|
|
@@ -6847,6 +7063,7 @@ function apply(ctx, config = {}) {
|
|
|
6847
7063
|
try {
|
|
6848
7064
|
if (piSection() === void 0) return;
|
|
6849
7065
|
const providers = providersOf();
|
|
7066
|
+
if (Object.keys(providers).length === 0) return;
|
|
6850
7067
|
updateImageConfig((document) => syncImageConfig(document, providers));
|
|
6851
7068
|
} catch {
|
|
6852
7069
|
}
|
|
@@ -6894,16 +7111,26 @@ function apply(ctx, config = {}) {
|
|
|
6894
7111
|
generateDeps: imageGenerateDeps
|
|
6895
7112
|
}
|
|
6896
7113
|
);
|
|
6897
|
-
const imageToolState = { name: void 0 };
|
|
7114
|
+
const imageToolState = { name: void 0, error: void 0 };
|
|
6898
7115
|
ctx.inject(["tools", "attachments"], (toolCtx) => {
|
|
6899
7116
|
toolCtx.effect(
|
|
6900
7117
|
() => {
|
|
6901
|
-
|
|
6902
|
-
|
|
6903
|
-
|
|
6904
|
-
|
|
6905
|
-
|
|
7118
|
+
let registered;
|
|
7119
|
+
try {
|
|
7120
|
+
registered = registerGenerateImageTool(toolCtx, {
|
|
7121
|
+
generateDeps: imageGenerateDeps,
|
|
7122
|
+
attachments: toolCtx.attachments,
|
|
7123
|
+
readConfig: () => readImageConfig()
|
|
7124
|
+
});
|
|
7125
|
+
} catch (error) {
|
|
7126
|
+
imageToolState.name = void 0;
|
|
7127
|
+
imageToolState.error = error instanceof Error ? error.message : String(error);
|
|
7128
|
+
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}`);
|
|
7129
|
+
throw error;
|
|
7130
|
+
}
|
|
7131
|
+
const { name: name2, dispose } = registered;
|
|
6906
7132
|
imageToolState.name = name2;
|
|
7133
|
+
imageToolState.error = void 0;
|
|
6907
7134
|
return () => {
|
|
6908
7135
|
imageToolState.name = void 0;
|
|
6909
7136
|
dispose();
|
|
@@ -7767,8 +7994,12 @@ function apply(ctx, config = {}) {
|
|
|
7767
7994
|
return;
|
|
7768
7995
|
}
|
|
7769
7996
|
try {
|
|
7770
|
-
const
|
|
7771
|
-
|
|
7997
|
+
const { document, stale } = commitUiOrderGuarded(body, Date.now());
|
|
7998
|
+
if (stale) {
|
|
7999
|
+
sendJson(res, 409, { ok: false, error: "stale", data: document });
|
|
8000
|
+
return;
|
|
8001
|
+
}
|
|
8002
|
+
sendJson(res, 200, { ok: true, data: document });
|
|
7772
8003
|
} catch (error) {
|
|
7773
8004
|
sendJson(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
|
|
7774
8005
|
}
|
|
@@ -8062,7 +8293,8 @@ function apply(ctx, config = {}) {
|
|
|
8062
8293
|
// and the client's keyed image card both read this name back.
|
|
8063
8294
|
tool: {
|
|
8064
8295
|
registered: imageToolState.name !== void 0,
|
|
8065
|
-
name: imageToolState.name ?? null
|
|
8296
|
+
name: imageToolState.name ?? null,
|
|
8297
|
+
...imageToolState.error === void 0 ? {} : { error: imageToolState.error }
|
|
8066
8298
|
}
|
|
8067
8299
|
});
|
|
8068
8300
|
return;
|