koishi-plugin-aka-ai-generator 0.6.12 → 0.6.15
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 +51 -39
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1056,8 +1056,12 @@ var UserManager = class {
|
|
|
1056
1056
|
}
|
|
1057
1057
|
// 批量更新用户数据 (用于充值)
|
|
1058
1058
|
async updateUsersBatch(updates) {
|
|
1059
|
+
await this.loadUsersData();
|
|
1059
1060
|
await this.dataLock.acquire(async () => {
|
|
1060
|
-
|
|
1061
|
+
if (!this.usersCache) {
|
|
1062
|
+
this.logger.error("updateUsersBatch: usersCache 为空,这不应该发生");
|
|
1063
|
+
this.usersCache = {};
|
|
1064
|
+
}
|
|
1061
1065
|
updates(this.usersCache);
|
|
1062
1066
|
await this.saveUsersDataInternal();
|
|
1063
1067
|
});
|
|
@@ -1158,20 +1162,26 @@ var UserManager = class {
|
|
|
1158
1162
|
return { ...rateLimitCheck };
|
|
1159
1163
|
}
|
|
1160
1164
|
this.updateRateLimit(userId);
|
|
1165
|
+
await this.loadUsersData();
|
|
1166
|
+
const userData = await this.getUserData(userId, userName);
|
|
1161
1167
|
return await this.dataLock.acquire(async () => {
|
|
1162
|
-
|
|
1168
|
+
if (!this.usersCache) {
|
|
1169
|
+
this.logger.error("checkAndReserveQuota: usersCache 为空,这不应该发生");
|
|
1170
|
+
this.usersCache = {};
|
|
1171
|
+
}
|
|
1172
|
+
const cachedUserData = this.usersCache[userId] || userData;
|
|
1163
1173
|
const today = (/* @__PURE__ */ new Date()).toDateString();
|
|
1164
|
-
const lastReset = new Date(
|
|
1165
|
-
let dailyCount =
|
|
1174
|
+
const lastReset = new Date(cachedUserData.lastDailyReset || cachedUserData.createdAt).toDateString();
|
|
1175
|
+
let dailyCount = cachedUserData.dailyUsageCount;
|
|
1166
1176
|
if (today !== lastReset) {
|
|
1167
1177
|
dailyCount = 0;
|
|
1168
1178
|
}
|
|
1169
1179
|
const remainingToday = Math.max(0, config.dailyFreeLimit - dailyCount);
|
|
1170
|
-
const totalAvailable = remainingToday +
|
|
1180
|
+
const totalAvailable = remainingToday + cachedUserData.remainingPurchasedCount;
|
|
1171
1181
|
if (totalAvailable < numImages) {
|
|
1172
1182
|
return {
|
|
1173
1183
|
allowed: false,
|
|
1174
|
-
message: `生成 ${numImages} 张图片需要 ${numImages} 次可用次数,但您的可用次数不足(今日免费剩余:${remainingToday}次,充值剩余:${
|
|
1184
|
+
message: `生成 ${numImages} 张图片需要 ${numImages} 次可用次数,但您的可用次数不足(今日免费剩余:${remainingToday}次,充值剩余:${cachedUserData.remainingPurchasedCount}次,共${totalAvailable}次)`
|
|
1175
1185
|
};
|
|
1176
1186
|
}
|
|
1177
1187
|
const reservationId = `${userId}_${Date.now()}_${Math.random()}`;
|
|
@@ -1180,8 +1190,12 @@ var UserManager = class {
|
|
|
1180
1190
|
}
|
|
1181
1191
|
// 扣减额度并记录使用
|
|
1182
1192
|
async consumeQuota(userId, userName, commandName, numImages, config) {
|
|
1193
|
+
await this.loadUsersData();
|
|
1183
1194
|
return await this.dataLock.acquire(async () => {
|
|
1184
|
-
|
|
1195
|
+
if (!this.usersCache) {
|
|
1196
|
+
this.logger.error("consumeQuota: usersCache 为空,这不应该发生");
|
|
1197
|
+
this.usersCache = {};
|
|
1198
|
+
}
|
|
1185
1199
|
let userData = this.usersCache[userId];
|
|
1186
1200
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1187
1201
|
const today = (/* @__PURE__ */ new Date()).toDateString();
|
|
@@ -1827,12 +1841,26 @@ ${infoParts.join("\n")}`;
|
|
|
1827
1841
|
index,
|
|
1828
1842
|
total
|
|
1829
1843
|
});
|
|
1844
|
+
logger.info("准备发送图片", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
|
|
1845
|
+
try {
|
|
1846
|
+
await session.send(import_koishi2.h.image(imageUrl));
|
|
1847
|
+
logger.info("流式处理:图片已发送", { index: index + 1, total, userId });
|
|
1848
|
+
} catch (sendError) {
|
|
1849
|
+
logger.error("发送图片失败", {
|
|
1850
|
+
userId,
|
|
1851
|
+
error: sanitizeError(sendError),
|
|
1852
|
+
errorMessage: sendError?.message,
|
|
1853
|
+
index: index + 1,
|
|
1854
|
+
total
|
|
1855
|
+
});
|
|
1856
|
+
throw sendError;
|
|
1857
|
+
}
|
|
1830
1858
|
if (!creditDeducted && generatedImages.length > 0) {
|
|
1831
1859
|
creditDeducted = true;
|
|
1832
1860
|
logger.info("准备扣除积分", { userId, totalImages: total, currentIndex: index });
|
|
1833
1861
|
try {
|
|
1834
1862
|
await recordUserUsage(session, styleName, total, false);
|
|
1835
|
-
logger.info("
|
|
1863
|
+
logger.info("流式处理:积分已扣除", {
|
|
1836
1864
|
userId,
|
|
1837
1865
|
totalImages: total,
|
|
1838
1866
|
currentIndex: index
|
|
@@ -1843,23 +1871,8 @@ ${infoParts.join("\n")}`;
|
|
|
1843
1871
|
error: sanitizeError(creditError),
|
|
1844
1872
|
totalImages: total
|
|
1845
1873
|
});
|
|
1846
|
-
throw creditError;
|
|
1847
1874
|
}
|
|
1848
1875
|
}
|
|
1849
|
-
logger.info("准备发送图片", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
|
|
1850
|
-
try {
|
|
1851
|
-
await session.send(import_koishi2.h.image(imageUrl));
|
|
1852
|
-
logger.info("流式处理:图片已发送", { index: index + 1, total, userId });
|
|
1853
|
-
} catch (sendError) {
|
|
1854
|
-
logger.error("发送图片失败", {
|
|
1855
|
-
userId,
|
|
1856
|
-
error: sanitizeError(sendError),
|
|
1857
|
-
errorMessage: sendError?.message,
|
|
1858
|
-
index: index + 1,
|
|
1859
|
-
total
|
|
1860
|
-
});
|
|
1861
|
-
throw sendError;
|
|
1862
|
-
}
|
|
1863
1876
|
if (total > 1 && index < total - 1) {
|
|
1864
1877
|
logger.debug("多张图片,添加延时", { index, total });
|
|
1865
1878
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
@@ -2061,12 +2074,26 @@ Prompt: ${prompt}`);
|
|
|
2061
2074
|
index,
|
|
2062
2075
|
total
|
|
2063
2076
|
});
|
|
2077
|
+
logger.info("准备发送图片 (COMPOSE_IMAGE)", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
|
|
2078
|
+
try {
|
|
2079
|
+
await session.send(import_koishi2.h.image(imageUrl));
|
|
2080
|
+
logger.info("流式处理:图片已发送 (COMPOSE_IMAGE)", { index: index + 1, total, userId });
|
|
2081
|
+
} catch (sendError) {
|
|
2082
|
+
logger.error("发送图片失败 (COMPOSE_IMAGE)", {
|
|
2083
|
+
userId,
|
|
2084
|
+
error: sanitizeError(sendError),
|
|
2085
|
+
errorMessage: sendError?.message,
|
|
2086
|
+
index: index + 1,
|
|
2087
|
+
total
|
|
2088
|
+
});
|
|
2089
|
+
throw sendError;
|
|
2090
|
+
}
|
|
2064
2091
|
if (!creditDeducted && generatedImages.length > 0) {
|
|
2065
2092
|
creditDeducted = true;
|
|
2066
2093
|
logger.info("准备扣除积分 (COMPOSE_IMAGE)", { userId, totalImages: total, currentIndex: index });
|
|
2067
2094
|
try {
|
|
2068
2095
|
await recordUserUsage(session, COMMANDS.COMPOSE_IMAGE, total, false);
|
|
2069
|
-
logger.info("
|
|
2096
|
+
logger.info("流式处理:积分已扣除 (COMPOSE_IMAGE)", {
|
|
2070
2097
|
userId,
|
|
2071
2098
|
totalImages: total,
|
|
2072
2099
|
currentIndex: index
|
|
@@ -2077,23 +2104,8 @@ Prompt: ${prompt}`);
|
|
|
2077
2104
|
error: sanitizeError(creditError),
|
|
2078
2105
|
totalImages: total
|
|
2079
2106
|
});
|
|
2080
|
-
throw creditError;
|
|
2081
2107
|
}
|
|
2082
2108
|
}
|
|
2083
|
-
logger.info("准备发送图片 (COMPOSE_IMAGE)", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
|
|
2084
|
-
try {
|
|
2085
|
-
await session.send(import_koishi2.h.image(imageUrl));
|
|
2086
|
-
logger.info("流式处理:图片已发送 (COMPOSE_IMAGE)", { index: index + 1, total, userId });
|
|
2087
|
-
} catch (sendError) {
|
|
2088
|
-
logger.error("发送图片失败 (COMPOSE_IMAGE)", {
|
|
2089
|
-
userId,
|
|
2090
|
-
error: sanitizeError(sendError),
|
|
2091
|
-
errorMessage: sendError?.message,
|
|
2092
|
-
index: index + 1,
|
|
2093
|
-
total
|
|
2094
|
-
});
|
|
2095
|
-
throw sendError;
|
|
2096
|
-
}
|
|
2097
2109
|
if (total > 1 && index < total - 1) {
|
|
2098
2110
|
logger.debug("多张图片,添加延时 (COMPOSE_IMAGE)", { index, total });
|
|
2099
2111
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|