koishi-plugin-aka-ai-generator 0.2.6 → 0.2.7
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/lib/index.js +66 -6
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -701,6 +701,35 @@ function apply(ctx, config) {
|
|
|
701
701
|
return value?.replace(/^\-+/, "").trim().toLowerCase();
|
|
702
702
|
}
|
|
703
703
|
__name(normalizeSuffix, "normalizeSuffix");
|
|
704
|
+
function parseNumImagesFromPrompt(prompt) {
|
|
705
|
+
if (!prompt || typeof prompt !== "string") {
|
|
706
|
+
return { numImages: void 0, cleanedPrompt: prompt };
|
|
707
|
+
}
|
|
708
|
+
const patterns = [
|
|
709
|
+
/生成\s*([1-4])\s*张(?:图片)?/i,
|
|
710
|
+
/([1-4])\s*张(?:图片)?/,
|
|
711
|
+
/生成\s*([1-4])\s*个(?:图片)?/i,
|
|
712
|
+
/([1-4])\s*个(?:图片)?/,
|
|
713
|
+
/num[:\s]*([1-4])/i,
|
|
714
|
+
/数量[:\s]*([1-4])/i
|
|
715
|
+
];
|
|
716
|
+
let numImages = void 0;
|
|
717
|
+
let cleanedPrompt = prompt;
|
|
718
|
+
for (const pattern of patterns) {
|
|
719
|
+
const match = prompt.match(pattern);
|
|
720
|
+
if (match) {
|
|
721
|
+
const num = parseInt(match[1], 10);
|
|
722
|
+
if (num >= 1 && num <= 4) {
|
|
723
|
+
numImages = num;
|
|
724
|
+
cleanedPrompt = prompt.replace(pattern, "").trim();
|
|
725
|
+
cleanedPrompt = cleanedPrompt.replace(/\s+/g, " ").replace(/[,,]\s*$/, "").trim();
|
|
726
|
+
break;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
return { numImages, cleanedPrompt };
|
|
731
|
+
}
|
|
732
|
+
__name(parseNumImagesFromPrompt, "parseNumImagesFromPrompt");
|
|
704
733
|
function buildModelMappingIndex(mappings) {
|
|
705
734
|
const map = /* @__PURE__ */ new Map();
|
|
706
735
|
if (!Array.isArray(mappings)) return map;
|
|
@@ -1184,16 +1213,33 @@ ${infoParts.join("\n")}`;
|
|
|
1184
1213
|
return limitCheck.message;
|
|
1185
1214
|
}
|
|
1186
1215
|
const modifiers = parseStyleCommandModifiers(argv, img);
|
|
1187
|
-
|
|
1216
|
+
let userPromptParts = [];
|
|
1188
1217
|
if (modifiers.customAdditions?.length) {
|
|
1189
|
-
|
|
1218
|
+
userPromptParts.push(...modifiers.customAdditions);
|
|
1190
1219
|
}
|
|
1191
1220
|
if (modifiers.customPromptSuffix) {
|
|
1192
|
-
|
|
1221
|
+
userPromptParts.push(modifiers.customPromptSuffix);
|
|
1222
|
+
}
|
|
1223
|
+
const userPromptText = userPromptParts.join(" - ");
|
|
1224
|
+
let promptNumImages = void 0;
|
|
1225
|
+
let cleanedUserPrompt = userPromptText;
|
|
1226
|
+
if (userPromptText) {
|
|
1227
|
+
const parsed = parseNumImagesFromPrompt(userPromptText);
|
|
1228
|
+
if (parsed.numImages) {
|
|
1229
|
+
promptNumImages = parsed.numImages;
|
|
1230
|
+
cleanedUserPrompt = parsed.cleanedPrompt;
|
|
1231
|
+
if (config.logLevel === "debug") {
|
|
1232
|
+
logger.debug("从 prompt 中解析到生成数量", { numImages: promptNumImages, cleanedPrompt: cleanedUserPrompt });
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
const promptSegments = [style.prompt];
|
|
1237
|
+
if (cleanedUserPrompt) {
|
|
1238
|
+
promptSegments.push(cleanedUserPrompt);
|
|
1193
1239
|
}
|
|
1194
1240
|
const mergedPrompt = promptSegments.filter(Boolean).join(" - ");
|
|
1195
1241
|
const requestContext = {
|
|
1196
|
-
numImages: options?.num
|
|
1242
|
+
numImages: options?.num || promptNumImages
|
|
1197
1243
|
};
|
|
1198
1244
|
if (modifiers.modelMapping?.provider) {
|
|
1199
1245
|
requestContext.provider = modifiers.modelMapping.provider;
|
|
@@ -1275,8 +1321,15 @@ ${infoParts.join("\n")}`;
|
|
|
1275
1321
|
if (!prompt) {
|
|
1276
1322
|
return "未检测到prompt描述,请重新发送";
|
|
1277
1323
|
}
|
|
1324
|
+
const { numImages: promptNumImages, cleanedPrompt } = parseNumImagesFromPrompt(prompt);
|
|
1325
|
+
if (promptNumImages) {
|
|
1326
|
+
prompt = cleanedPrompt;
|
|
1327
|
+
if (config.logLevel === "debug") {
|
|
1328
|
+
logger.debug("从 prompt 中解析到生成数量", { numImages: promptNumImages, cleanedPrompt });
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1278
1331
|
const imageUrl = collectedImages[0];
|
|
1279
|
-
const imageCount = options?.num || config.defaultNumImages;
|
|
1332
|
+
const imageCount = options?.num || promptNumImages || config.defaultNumImages;
|
|
1280
1333
|
if (imageCount < 1 || imageCount > 4) {
|
|
1281
1334
|
return "生成数量必须在 1-4 之间";
|
|
1282
1335
|
}
|
|
@@ -1374,7 +1427,14 @@ Prompt: ${prompt}`);
|
|
|
1374
1427
|
if (!prompt) {
|
|
1375
1428
|
return "未检测到prompt描述,请重新发送";
|
|
1376
1429
|
}
|
|
1377
|
-
const
|
|
1430
|
+
const { numImages: promptNumImages, cleanedPrompt } = parseNumImagesFromPrompt(prompt);
|
|
1431
|
+
if (promptNumImages) {
|
|
1432
|
+
prompt = cleanedPrompt;
|
|
1433
|
+
if (config.logLevel === "debug") {
|
|
1434
|
+
logger.debug("从 prompt 中解析到生成数量", { numImages: promptNumImages, cleanedPrompt });
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
const imageCount = options?.num || promptNumImages || config.defaultNumImages;
|
|
1378
1438
|
if (imageCount < 1 || imageCount > 4) {
|
|
1379
1439
|
return "生成数量必须在 1-4 之间";
|
|
1380
1440
|
}
|