koishi-plugin-p-draw 1.5.4 → 1.5.6
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/index.js +27 -14
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const { Schema, h } = require('koishi')
|
|
|
2
2
|
const fs = require('fs')
|
|
3
3
|
const fsp = require('fs/promises')
|
|
4
4
|
const path = require('path')
|
|
5
|
+
const os = require('os')
|
|
5
6
|
const crypto = require('crypto')
|
|
6
7
|
const { pathToFileURL } = require('url')
|
|
7
8
|
|
|
@@ -173,10 +174,18 @@ exports.apply = async function apply(ctx, cfg) {
|
|
|
173
174
|
// 用户自选模型偏好(userid -> unet 文件名),持久化在 p_draw_config.user_models
|
|
174
175
|
if (!cfg.userModels || typeof cfg.userModels !== 'object') cfg.userModels = {}
|
|
175
176
|
|
|
176
|
-
const tempDir = path.join(
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
177
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pdraw-'))
|
|
178
|
+
// 启动时清理上次运行遗留的临时文件
|
|
179
|
+
try {
|
|
180
|
+
const leftovers = fs.readdirSync(os.tmpdir()).filter((n) => n.startsWith('pdraw-'))
|
|
181
|
+
for (const name of leftovers) {
|
|
182
|
+
const dir = path.join(os.tmpdir(), name)
|
|
183
|
+
try {
|
|
184
|
+
for (const file of fs.readdirSync(dir)) fs.unlinkSync(path.join(dir, file))
|
|
185
|
+
fs.rmdirSync(dir)
|
|
186
|
+
} catch (e) { /* ignore */ }
|
|
187
|
+
}
|
|
188
|
+
} catch (e) { /* ignore */ }
|
|
180
189
|
|
|
181
190
|
const baseUrl = () => normalizeBaseUrl(cfg.comfyuiBaseUrl)
|
|
182
191
|
|
|
@@ -469,12 +478,11 @@ exports.apply = async function apply(ctx, cfg) {
|
|
|
469
478
|
return `已扣除 ${totalPrice} P 点,${modelSeed}`
|
|
470
479
|
}
|
|
471
480
|
|
|
472
|
-
function buildGenerationReply(session, { successCount, count, failures, notes = []
|
|
473
|
-
const reply = []
|
|
474
|
-
if (
|
|
475
|
-
if (
|
|
476
|
-
|
|
477
|
-
return reply.filter(Boolean).join('\n')
|
|
481
|
+
function buildGenerationReply(session, { successCount, count, failures, notes = [] }) {
|
|
482
|
+
const reply = []
|
|
483
|
+
if (notes.length) reply.push(notes.join('\n'))
|
|
484
|
+
if (failures.length) reply.push(session.text('.batch-partial', [successCount, count, failures.length, failures.join(';')]))
|
|
485
|
+
return reply.filter(Boolean).join('\n')
|
|
478
486
|
}
|
|
479
487
|
|
|
480
488
|
async function sendNotices(session, notices, opts = {}) {
|
|
@@ -1483,10 +1491,12 @@ exports.apply = async function apply(ctx, cfg) {
|
|
|
1483
1491
|
// 发图:合并转发,不引用原指令
|
|
1484
1492
|
await sendImagesAsForward(session, forwardOutputs)
|
|
1485
1493
|
|
|
1486
|
-
|
|
1494
|
+
await sendNotices(session, [chargeNotice])
|
|
1495
|
+
|
|
1496
|
+
return buildGenerationReply(session, { successCount, count, failures, notes })
|
|
1487
1497
|
}
|
|
1488
1498
|
|
|
1489
|
-
// ---------------- 权限 ----------------
|
|
1499
|
+
// ---------------- 权限 ----------------
|
|
1490
1500
|
function readableOptimizeReason(reason) {
|
|
1491
1501
|
if (!reason) return ''
|
|
1492
1502
|
if (reason === 'llm_not_configured') return '(未配置 LLM 模型名或接口地址)'
|
|
@@ -2011,8 +2021,10 @@ exports.apply = async function apply(ctx, cfg) {
|
|
|
2011
2021
|
// 发图:合并转发,不引用原指令
|
|
2012
2022
|
await sendImagesAsForward(session, forwardOutputs)
|
|
2013
2023
|
|
|
2014
|
-
|
|
2015
|
-
|
|
2024
|
+
await sendNotices(session, [chargeNotice])
|
|
2025
|
+
|
|
2026
|
+
return buildGenerationReply(session, { successCount, count, failures })
|
|
2027
|
+
}
|
|
2016
2028
|
// ---------------- 提示词优化券交互式确认 ----------------
|
|
2017
2029
|
// 仅全局优化关闭 + 非管理员 + 已配置 LLM(tokenOpt 分支)时进入。
|
|
2018
2030
|
// 返回:{ cancelled, tokenUsedCount, perImageOptimize, noOptimizeReason }
|
|
@@ -2152,6 +2164,7 @@ exports.apply = async function apply(ctx, cfg) {
|
|
|
2152
2164
|
for (const file of files) {
|
|
2153
2165
|
try { fs.unlinkSync(path.join(tempDir, file)) } catch (e) { /* ignore */ }
|
|
2154
2166
|
}
|
|
2167
|
+
try { fs.rmdirSync(tempDir) } catch (e) { /* ignore */ }
|
|
2155
2168
|
} catch (e) { /* ignore */ }
|
|
2156
2169
|
})
|
|
2157
2170
|
|