koishi-plugin-aka-ai-generator 0.2.17 → 0.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/lib/index.js +46 -70
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -740,8 +740,9 @@ __name(createImageProvider, "createImageProvider");
|
|
|
740
740
|
// src/index.ts
|
|
741
741
|
var name = "aka-ai-generator";
|
|
742
742
|
var COMMANDS = {
|
|
743
|
-
|
|
744
|
-
|
|
743
|
+
IMG_TO_IMG: "图生图",
|
|
744
|
+
TXT_TO_IMG: "文生图",
|
|
745
|
+
COMPOSE_IMAGE: "合成图",
|
|
745
746
|
CHANGE_POSE: "改姿势",
|
|
746
747
|
OPTIMIZE_DESIGN: "修改设计",
|
|
747
748
|
PIXELATE: "变像素",
|
|
@@ -852,35 +853,6 @@ function apply(ctx, config) {
|
|
|
852
853
|
return value?.replace(/^\-+/, "").trim().toLowerCase();
|
|
853
854
|
}
|
|
854
855
|
__name(normalizeSuffix, "normalizeSuffix");
|
|
855
|
-
function parseNumImagesFromPrompt(prompt) {
|
|
856
|
-
if (!prompt || typeof prompt !== "string") {
|
|
857
|
-
return { numImages: void 0, cleanedPrompt: prompt };
|
|
858
|
-
}
|
|
859
|
-
const patterns = [
|
|
860
|
-
/生成\s*([1-4])\s*张(?:图片)?/i,
|
|
861
|
-
/([1-4])\s*张(?:图片)?/,
|
|
862
|
-
/生成\s*([1-4])\s*个(?:图片)?/i,
|
|
863
|
-
/([1-4])\s*个(?:图片)?/,
|
|
864
|
-
/num[:\s]*([1-4])/i,
|
|
865
|
-
/数量[:\s]*([1-4])/i
|
|
866
|
-
];
|
|
867
|
-
let numImages = void 0;
|
|
868
|
-
let cleanedPrompt = prompt;
|
|
869
|
-
for (const pattern of patterns) {
|
|
870
|
-
const match = prompt.match(pattern);
|
|
871
|
-
if (match) {
|
|
872
|
-
const num = parseInt(match[1], 10);
|
|
873
|
-
if (num >= 1 && num <= 4) {
|
|
874
|
-
numImages = num;
|
|
875
|
-
cleanedPrompt = prompt.replace(pattern, "").trim();
|
|
876
|
-
cleanedPrompt = cleanedPrompt.replace(/\s+/g, " ").replace(/[,,]\s*$/, "").trim();
|
|
877
|
-
break;
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
return { numImages, cleanedPrompt };
|
|
882
|
-
}
|
|
883
|
-
__name(parseNumImagesFromPrompt, "parseNumImagesFromPrompt");
|
|
884
856
|
function buildModelMappingIndex(mappings) {
|
|
885
857
|
const map = /* @__PURE__ */ new Map();
|
|
886
858
|
if (!Array.isArray(mappings)) return map;
|
|
@@ -1005,7 +977,8 @@ function apply(ctx, config) {
|
|
|
1005
977
|
// 非管理员指令(包含动态风格指令)
|
|
1006
978
|
userCommands: [
|
|
1007
979
|
...getStyleCommands(),
|
|
1008
|
-
{ name: COMMANDS.
|
|
980
|
+
{ name: COMMANDS.TXT_TO_IMG, description: "根据文字描述生成图像" },
|
|
981
|
+
{ name: COMMANDS.IMG_TO_IMG, description: "使用自定义prompt进行图像处理(图生图)" },
|
|
1009
982
|
{ name: COMMANDS.COMPOSE_IMAGE, description: "合成多张图片,使用自定义prompt控制合成效果" },
|
|
1010
983
|
{ name: COMMANDS.QUERY_QUOTA, description: "查询用户额度信息" }
|
|
1011
984
|
],
|
|
@@ -1277,6 +1250,23 @@ function apply(ctx, config) {
|
|
|
1277
1250
|
async function getInputData(session, imgParam, mode) {
|
|
1278
1251
|
const collectedImages = [];
|
|
1279
1252
|
let collectedText = "";
|
|
1253
|
+
if (mode === "text") {
|
|
1254
|
+
if (typeof imgParam === "string" && imgParam.trim()) {
|
|
1255
|
+
return { images: [], text: imgParam.trim() };
|
|
1256
|
+
}
|
|
1257
|
+
if (session.content) {
|
|
1258
|
+
const elements2 = import_koishi.h.parse(session.content);
|
|
1259
|
+
const text2 = import_koishi.h.select(elements2, "text").map((e) => e.attrs.content).join(" ").trim();
|
|
1260
|
+
if (text2) return { images: [], text: text2 };
|
|
1261
|
+
}
|
|
1262
|
+
await session.send("请输入画面描述");
|
|
1263
|
+
const msg = await session.prompt(3e4);
|
|
1264
|
+
if (!msg) return { error: "等待超时" };
|
|
1265
|
+
const elements = import_koishi.h.parse(msg);
|
|
1266
|
+
const text = import_koishi.h.select(elements, "text").map((e) => e.attrs.content).join(" ").trim();
|
|
1267
|
+
if (!text) return { error: "未检测到描述" };
|
|
1268
|
+
return { images: [], text };
|
|
1269
|
+
}
|
|
1280
1270
|
if (imgParam) {
|
|
1281
1271
|
if (typeof imgParam === "object" && imgParam.attrs?.src) {
|
|
1282
1272
|
collectedImages.push(imgParam.attrs.src);
|
|
@@ -1295,7 +1285,7 @@ function apply(ctx, config) {
|
|
|
1295
1285
|
if (collectedImages.length > 0) {
|
|
1296
1286
|
if (mode === "single") {
|
|
1297
1287
|
if (collectedImages.length > 1) {
|
|
1298
|
-
return { error: '本功能仅支持处理一张图片,检测到多张图片。如需合成多张图片请使用"
|
|
1288
|
+
return { error: '本功能仅支持处理一张图片,检测到多张图片。如需合成多张图片请使用"合成图"命令' };
|
|
1299
1289
|
}
|
|
1300
1290
|
return { images: collectedImages };
|
|
1301
1291
|
}
|
|
@@ -1453,26 +1443,14 @@ ${infoParts.join("\n")}`;
|
|
|
1453
1443
|
userPromptParts.push(modifiers.customPromptSuffix);
|
|
1454
1444
|
}
|
|
1455
1445
|
const userPromptText = userPromptParts.join(" - ");
|
|
1456
|
-
|
|
1457
|
-
let cleanedUserPrompt = userPromptText;
|
|
1458
|
-
if (userPromptText) {
|
|
1459
|
-
const parsed = parseNumImagesFromPrompt(userPromptText);
|
|
1460
|
-
if (parsed.numImages) {
|
|
1461
|
-
promptNumImages = parsed.numImages;
|
|
1462
|
-
cleanedUserPrompt = parsed.cleanedPrompt;
|
|
1463
|
-
if (config.logLevel === "debug") {
|
|
1464
|
-
logger.debug("从 prompt 中解析到生成数量", { numImages: promptNumImages, cleanedPrompt: cleanedUserPrompt });
|
|
1465
|
-
}
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
|
-
const numImages = options?.num || promptNumImages || config.defaultNumImages;
|
|
1446
|
+
const numImages = options?.num || config.defaultNumImages;
|
|
1469
1447
|
const limitCheck = await checkDailyLimit(session.userId, numImages);
|
|
1470
1448
|
if (!limitCheck.allowed) {
|
|
1471
1449
|
return limitCheck.message;
|
|
1472
1450
|
}
|
|
1473
1451
|
const promptSegments = [style.prompt];
|
|
1474
|
-
if (
|
|
1475
|
-
promptSegments.push(
|
|
1452
|
+
if (userPromptText) {
|
|
1453
|
+
promptSegments.push(userPromptText);
|
|
1476
1454
|
}
|
|
1477
1455
|
const mergedPrompt = promptSegments.filter(Boolean).join(" - ");
|
|
1478
1456
|
const requestContext = {
|
|
@@ -1499,7 +1477,19 @@ ${infoParts.join("\n")}`;
|
|
|
1499
1477
|
}
|
|
1500
1478
|
}
|
|
1501
1479
|
}
|
|
1502
|
-
ctx.command(COMMANDS.
|
|
1480
|
+
ctx.command(`${COMMANDS.TXT_TO_IMG} [prompt:text]`, "根据文字描述生成图像").option("num", "-n <num:number> 生成图片数量 (1-4)").action(async ({ session, options }, prompt) => {
|
|
1481
|
+
if (!session?.userId) return "会话无效";
|
|
1482
|
+
const numImages = options?.num || config.defaultNumImages;
|
|
1483
|
+
const limitCheck = await checkDailyLimit(session.userId, numImages);
|
|
1484
|
+
if (!limitCheck.allowed) {
|
|
1485
|
+
return limitCheck.message;
|
|
1486
|
+
}
|
|
1487
|
+
const requestContext = {
|
|
1488
|
+
numImages
|
|
1489
|
+
};
|
|
1490
|
+
return processImageWithTimeout(session, prompt, "", COMMANDS.TXT_TO_IMG, requestContext, {}, "text");
|
|
1491
|
+
});
|
|
1492
|
+
ctx.command(COMMANDS.IMG_TO_IMG, "使用自定义prompt进行图像处理").option("num", "-n <num:number> 生成图片数量 (1-4)").action(async ({ session, options }) => {
|
|
1503
1493
|
if (!session?.userId) return "会话无效";
|
|
1504
1494
|
return Promise.race([
|
|
1505
1495
|
(async () => {
|
|
@@ -1522,10 +1512,10 @@ ${infoParts.join("\n")}`;
|
|
|
1522
1512
|
const text = textElements.map((el) => el.attrs.content).join(" ").trim();
|
|
1523
1513
|
if (images.length > 0) {
|
|
1524
1514
|
if (collectedImages.length > 0) {
|
|
1525
|
-
return '本功能仅支持处理一张图片,如需合成多张图片请使用"
|
|
1515
|
+
return '本功能仅支持处理一张图片,如需合成多张图片请使用"合成图"命令';
|
|
1526
1516
|
}
|
|
1527
1517
|
if (images.length > 1) {
|
|
1528
|
-
return '本功能仅支持处理一张图片,检测到多张图片。如需合成多张图片请使用"
|
|
1518
|
+
return '本功能仅支持处理一张图片,检测到多张图片。如需合成多张图片请使用"合成图"命令';
|
|
1529
1519
|
}
|
|
1530
1520
|
for (const img of images) {
|
|
1531
1521
|
collectedImages.push(img.attrs.src);
|
|
@@ -1550,20 +1540,13 @@ ${infoParts.join("\n")}`;
|
|
|
1550
1540
|
return "未检测到图片,请重新发送";
|
|
1551
1541
|
}
|
|
1552
1542
|
if (collectedImages.length > 1) {
|
|
1553
|
-
return '本功能仅支持处理一张图片,检测到多张图片。如需合成多张图片请使用"
|
|
1543
|
+
return '本功能仅支持处理一张图片,检测到多张图片。如需合成多张图片请使用"合成图"命令';
|
|
1554
1544
|
}
|
|
1555
1545
|
if (!prompt) {
|
|
1556
1546
|
return "未检测到prompt描述,请重新发送";
|
|
1557
1547
|
}
|
|
1558
|
-
const { numImages: promptNumImages, cleanedPrompt } = parseNumImagesFromPrompt(prompt);
|
|
1559
|
-
if (promptNumImages) {
|
|
1560
|
-
prompt = cleanedPrompt;
|
|
1561
|
-
if (config.logLevel === "debug") {
|
|
1562
|
-
logger.debug("从 prompt 中解析到生成数量", { numImages: promptNumImages, cleanedPrompt });
|
|
1563
|
-
}
|
|
1564
|
-
}
|
|
1565
1548
|
const imageUrl = collectedImages[0];
|
|
1566
|
-
const imageCount = options?.num ||
|
|
1549
|
+
const imageCount = options?.num || config.defaultNumImages;
|
|
1567
1550
|
if (imageCount < 1 || imageCount > 4) {
|
|
1568
1551
|
return "生成数量必须在 1-4 之间";
|
|
1569
1552
|
}
|
|
@@ -1593,7 +1576,7 @@ Prompt: ${prompt}`);
|
|
|
1593
1576
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
1594
1577
|
}
|
|
1595
1578
|
}
|
|
1596
|
-
await recordUserUsage(session, COMMANDS.
|
|
1579
|
+
await recordUserUsage(session, COMMANDS.IMG_TO_IMG, resultImages.length);
|
|
1597
1580
|
activeTasks.delete(userId);
|
|
1598
1581
|
} catch (error) {
|
|
1599
1582
|
activeTasks.delete(userId);
|
|
@@ -1661,14 +1644,7 @@ Prompt: ${prompt}`);
|
|
|
1661
1644
|
if (!prompt) {
|
|
1662
1645
|
return "未检测到prompt描述,请重新发送";
|
|
1663
1646
|
}
|
|
1664
|
-
const
|
|
1665
|
-
if (promptNumImages) {
|
|
1666
|
-
prompt = cleanedPrompt;
|
|
1667
|
-
if (config.logLevel === "debug") {
|
|
1668
|
-
logger.debug("从 prompt 中解析到生成数量", { numImages: promptNumImages, cleanedPrompt });
|
|
1669
|
-
}
|
|
1670
|
-
}
|
|
1671
|
-
const imageCount = options?.num || promptNumImages || config.defaultNumImages;
|
|
1647
|
+
const imageCount = options?.num || config.defaultNumImages;
|
|
1672
1648
|
if (imageCount < 1 || imageCount > 4) {
|
|
1673
1649
|
return "生成数量必须在 1-4 之间";
|
|
1674
1650
|
}
|
|
@@ -1683,7 +1659,7 @@ Prompt: ${prompt}`);
|
|
|
1683
1659
|
numImages: imageCount,
|
|
1684
1660
|
imageCount: collectedImages.length
|
|
1685
1661
|
});
|
|
1686
|
-
await session.send(
|
|
1662
|
+
await session.send(`开始合成图(${collectedImages.length}张)...
|
|
1687
1663
|
Prompt: ${prompt}`);
|
|
1688
1664
|
try {
|
|
1689
1665
|
activeTasks.set(userId, "processing");
|