koishi-plugin-aka-ai-generator 0.6.11 → 0.6.13

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.
Files changed (2) hide show
  1. package/lib/index.js +52 -42
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1538,7 +1538,7 @@ function apply(ctx, config) {
1538
1538
  充值剩余:${userData.remainingPurchasedCount}次`;
1539
1539
  }
1540
1540
  __name(buildStatsMessage, "buildStatsMessage");
1541
- async function recordUserUsage(session, commandName, numImages = 1) {
1541
+ async function recordUserUsage(session, commandName, numImages = 1, sendStatsImmediately = true) {
1542
1542
  const userId = session.userId;
1543
1543
  const userName = session.username || session.userId || "未知用户";
1544
1544
  if (!userId) return;
@@ -1556,11 +1556,23 @@ function apply(ctx, config) {
1556
1556
  remainingPurchasedCount: userData.remainingPurchasedCount,
1557
1557
  isAdmin: userManager.isAdmin(userId, config)
1558
1558
  });
1559
- try {
1560
- const statsMessage = buildStatsMessage(userData, numImages, consumptionType, freeUsed, purchasedUsed, config);
1561
- await session.send(statsMessage);
1562
- } catch (error) {
1563
- logger.warn("发送统计信息失败", { userId, error: sanitizeError(error) });
1559
+ if (sendStatsImmediately) {
1560
+ try {
1561
+ const statsMessage = buildStatsMessage(userData, numImages, consumptionType, freeUsed, purchasedUsed, config);
1562
+ await session.send(statsMessage);
1563
+ } catch (error) {
1564
+ logger.warn("发送统计信息失败", { userId, error: sanitizeError(error) });
1565
+ }
1566
+ } else {
1567
+ setImmediate(async () => {
1568
+ try {
1569
+ const statsMessage = buildStatsMessage(userData, numImages, consumptionType, freeUsed, purchasedUsed, config);
1570
+ await session.send(statsMessage);
1571
+ logger.debug("统计信息已异步发送", { userId, commandName });
1572
+ } catch (error) {
1573
+ logger.warn("异步发送统计信息失败", { userId, error: sanitizeError(error) });
1574
+ }
1575
+ });
1564
1576
  }
1565
1577
  }
1566
1578
  __name(recordUserUsage, "recordUserUsage");
@@ -1815,12 +1827,26 @@ ${infoParts.join("\n")}`;
1815
1827
  index,
1816
1828
  total
1817
1829
  });
1830
+ logger.info("准备发送图片", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
1831
+ try {
1832
+ await session.send(import_koishi2.h.image(imageUrl));
1833
+ logger.info("流式处理:图片已发送", { index: index + 1, total, userId });
1834
+ } catch (sendError) {
1835
+ logger.error("发送图片失败", {
1836
+ userId,
1837
+ error: sanitizeError(sendError),
1838
+ errorMessage: sendError?.message,
1839
+ index: index + 1,
1840
+ total
1841
+ });
1842
+ throw sendError;
1843
+ }
1818
1844
  if (!creditDeducted && generatedImages.length > 0) {
1819
1845
  creditDeducted = true;
1820
1846
  logger.info("准备扣除积分", { userId, totalImages: total, currentIndex: index });
1821
1847
  try {
1822
- await recordUserUsage(session, styleName, total);
1823
- logger.info("流式处理:第一张图片生成,积分已扣除", {
1848
+ await recordUserUsage(session, styleName, total, false);
1849
+ logger.info("流式处理:积分已扣除", {
1824
1850
  userId,
1825
1851
  totalImages: total,
1826
1852
  currentIndex: index
@@ -1831,23 +1857,8 @@ ${infoParts.join("\n")}`;
1831
1857
  error: sanitizeError(creditError),
1832
1858
  totalImages: total
1833
1859
  });
1834
- throw creditError;
1835
1860
  }
1836
1861
  }
1837
- logger.info("准备发送图片", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
1838
- try {
1839
- await session.send(import_koishi2.h.image(imageUrl));
1840
- logger.info("流式处理:图片已发送", { index: index + 1, total, userId });
1841
- } catch (sendError) {
1842
- logger.error("发送图片失败", {
1843
- userId,
1844
- error: sanitizeError(sendError),
1845
- errorMessage: sendError?.message,
1846
- index: index + 1,
1847
- total
1848
- });
1849
- throw sendError;
1850
- }
1851
1862
  if (total > 1 && index < total - 1) {
1852
1863
  logger.debug("多张图片,添加延时", { index, total });
1853
1864
  await new Promise((resolve) => setTimeout(resolve, 1e3));
@@ -1872,7 +1883,7 @@ ${infoParts.join("\n")}`;
1872
1883
  return "图像处理失败:未能生成图片";
1873
1884
  }
1874
1885
  if (!creditDeducted) {
1875
- await recordUserUsage(session, styleName, images.length);
1886
+ await recordUserUsage(session, styleName, images.length, false);
1876
1887
  logger.warn("流式处理:积分在最后扣除(异常情况)", { userId, imagesCount: images.length });
1877
1888
  }
1878
1889
  await session.send("图像处理完成!");
@@ -2049,12 +2060,26 @@ Prompt: ${prompt}`);
2049
2060
  index,
2050
2061
  total
2051
2062
  });
2063
+ logger.info("准备发送图片 (COMPOSE_IMAGE)", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
2064
+ try {
2065
+ await session.send(import_koishi2.h.image(imageUrl));
2066
+ logger.info("流式处理:图片已发送 (COMPOSE_IMAGE)", { index: index + 1, total, userId });
2067
+ } catch (sendError) {
2068
+ logger.error("发送图片失败 (COMPOSE_IMAGE)", {
2069
+ userId,
2070
+ error: sanitizeError(sendError),
2071
+ errorMessage: sendError?.message,
2072
+ index: index + 1,
2073
+ total
2074
+ });
2075
+ throw sendError;
2076
+ }
2052
2077
  if (!creditDeducted && generatedImages.length > 0) {
2053
2078
  creditDeducted = true;
2054
2079
  logger.info("准备扣除积分 (COMPOSE_IMAGE)", { userId, totalImages: total, currentIndex: index });
2055
2080
  try {
2056
- await recordUserUsage(session, COMMANDS.COMPOSE_IMAGE, total);
2057
- logger.info("流式处理:第一张图片生成,积分已扣除 (COMPOSE_IMAGE)", {
2081
+ await recordUserUsage(session, COMMANDS.COMPOSE_IMAGE, total, false);
2082
+ logger.info("流式处理:积分已扣除 (COMPOSE_IMAGE)", {
2058
2083
  userId,
2059
2084
  totalImages: total,
2060
2085
  currentIndex: index
@@ -2065,23 +2090,8 @@ Prompt: ${prompt}`);
2065
2090
  error: sanitizeError(creditError),
2066
2091
  totalImages: total
2067
2092
  });
2068
- throw creditError;
2069
2093
  }
2070
2094
  }
2071
- logger.info("准备发送图片 (COMPOSE_IMAGE)", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
2072
- try {
2073
- await session.send(import_koishi2.h.image(imageUrl));
2074
- logger.info("流式处理:图片已发送 (COMPOSE_IMAGE)", { index: index + 1, total, userId });
2075
- } catch (sendError) {
2076
- logger.error("发送图片失败 (COMPOSE_IMAGE)", {
2077
- userId,
2078
- error: sanitizeError(sendError),
2079
- errorMessage: sendError?.message,
2080
- index: index + 1,
2081
- total
2082
- });
2083
- throw sendError;
2084
- }
2085
2095
  if (total > 1 && index < total - 1) {
2086
2096
  logger.debug("多张图片,添加延时 (COMPOSE_IMAGE)", { index, total });
2087
2097
  await new Promise((resolve) => setTimeout(resolve, 1e3));
@@ -2106,7 +2116,7 @@ Prompt: ${prompt}`);
2106
2116
  return "图片合成失败:未能生成图片";
2107
2117
  }
2108
2118
  if (!creditDeducted) {
2109
- await recordUserUsage(session, COMMANDS.COMPOSE_IMAGE, resultImages.length);
2119
+ await recordUserUsage(session, COMMANDS.COMPOSE_IMAGE, resultImages.length, false);
2110
2120
  logger.warn("流式处理:积分在最后扣除(异常情况)", { userId, imagesCount: resultImages.length });
2111
2121
  }
2112
2122
  await session.send("图片合成完成!");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-aka-ai-generator",
3
3
  "description": "自用AI生成插件(GPTGod & Yunwu)",
4
- "version": "0.6.11",
4
+ "version": "0.6.13",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [