dsh-plugin-cyrene-live2d 0.2.9 → 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/README.md +8 -2
- package/assets/default-prefs.json +8 -8
- package/assets/icon.svg +89 -89
- package/assets/locale/notes/en.json +6 -6
- package/assets/locale/notes/zh-CN.json +6 -6
- package/assets/locale/reminder/en.json +6 -6
- package/assets/locale/reminder/zh-CN.json +6 -6
- package/assets/model/Cyrene.model3.json +441 -441
- package/assets/model/bongo-standard/Live2d_expression0.exp3.json +9 -9
- package/assets/model/bongo-standard/cat.cdi3.json +296 -296
- package/assets/model/bongo-standard/cat.model3.json +33 -33
- package/assets/model/bongo-standard/cat.physics3.json +232 -232
- package/assets/model/expressions/0.exp3.json +24 -24
- package/assets/model/expressions/1.exp3.json +11 -11
- package/assets/model/expressions/10.exp3.json +9 -9
- package/assets/model/expressions/11.exp3.json +9 -9
- package/assets/model/expressions/2.exp3.json +9 -9
- package/assets/model/expressions/3.exp3.json +9 -9
- package/assets/model/expressions/4.exp3.json +11 -11
- package/assets/model/expressions/5.exp3.json +11 -11
- package/assets/model/expressions/6.exp3.json +11 -11
- package/assets/model/expressions/7.exp3.json +11 -11
- package/assets/model/expressions/8.exp3.json +11 -11
- package/assets/model/expressions/9.exp3.json +9 -9
- package/assets/model/model.physics3.json +641 -641
- package/assets/model/motions/Tick3_3.motion3.json +1347 -1347
- package/assets/model/motions//345/212/250/344/275/2346_0.motion3.json +699 -699
- package/assets/model/motions//345/212/250/344/275/2346_1.motion3.json +1241 -1241
- package/assets/model/motions//345/212/250/344/275/2346_2.motion3.json +1117 -1117
- package/assets/model/motions//345/212/250/344/275/2346_3.motion3.json +1106 -1106
- package/assets/model/we-q/expressions/FileReferences_Expressions_0_File_0.exp3.json +19 -19
- package/assets/model/we-q/expressions/FileReferences_Expressions_1_File_0.exp3.json +41 -41
- package/assets/model/we-q/expressions/FileReferences_Expressions_2_File_0.exp3.json +104 -104
- package/assets/model/we-q/model0.model3.json +245 -245
- package/assets/model/we-q/motions/FileReferences_Motions_shake#1_0_File_0.motion3.json +520 -520
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_0_File_0.motion3.json +499 -499
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_1_File_0.motion3.json +453 -453
- package/assets/model/we-q/motions/FileReferences_Motions_wink#1_0_File_0.motion3.json +478 -478
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_0_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_1_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/idle/FileReferences_Motions_Idleidle_0_File_0.motion3.json +422 -422
- package/assets/model/we-q/physics/FileReferences_Physics_0.physic3.json +236 -236
- package/assets/runtime/live2dcubismcore.min.js +8 -8
- package/assets/stickers.json +84 -84
- package/assets/vendor/cubism4.min.js +1 -1
- package/assets/vendor/pixi.min.js +9 -9
- package/assets/vendor/theme-cyrene-factory.js +4314 -4314
- package/assets/vendor/theme-cyrene.js +4317 -4317
- package/assets/voice/angry-00.opus +0 -0
- package/assets/voice/angry-02.opus +0 -0
- package/assets/voice/approval-00.opus +0 -0
- package/assets/voice/approval-01.opus +0 -0
- package/assets/voice/approval-02.opus +0 -0
- package/assets/voice/askAck-00.opus +0 -0
- package/assets/voice/away-01.opus +0 -0
- package/assets/voice/away-02.opus +0 -0
- package/assets/voice/away-03.opus +0 -0
- package/assets/voice/away-05.opus +0 -0
- package/assets/voice/back-00.opus +0 -0
- package/assets/voice/back-01.opus +0 -0
- package/assets/voice/backLong-05.opus +0 -0
- package/assets/voice/backShort-03.opus +0 -0
- package/assets/voice/backShort-04.opus +0 -0
- package/assets/voice/backShort-07.opus +0 -0
- package/assets/voice/backVeryLong-01.opus +0 -0
- package/assets/voice/backVeryLong-02.opus +0 -0
- package/assets/voice/backVeryLong-03.opus +0 -0
- package/assets/voice/backVeryLong-04.opus +0 -0
- package/assets/voice/backVeryLong-05.opus +0 -0
- package/assets/voice/backVeryLong-07.opus +0 -0
- package/assets/voice/backVeryLong-08.opus +0 -0
- package/assets/voice/balLow-00.opus +0 -0
- package/assets/voice/balLow-01.opus +0 -0
- package/assets/voice/birthday-02.opus +0 -0
- package/assets/voice/click-00.opus +0 -0
- package/assets/voice/click-01.opus +0 -0
- package/assets/voice/click-07.opus +0 -0
- package/assets/voice/click-10.opus +0 -0
- package/assets/voice/click-11.opus +0 -0
- package/assets/voice/click-12.opus +0 -0
- package/assets/voice/click-16.opus +0 -0
- package/assets/voice/click-17.opus +0 -0
- package/assets/voice/click-18.opus +0 -0
- package/assets/voice/comfort-03.opus +0 -0
- package/assets/voice/comfort-04.opus +0 -0
- package/assets/voice/cost10-00.opus +0 -0
- package/assets/voice/cost2-00.opus +0 -0
- package/assets/voice/cost5-00.opus +0 -0
- package/assets/voice/costTurn-01.opus +0 -0
- package/assets/voice/costTurn-02.opus +0 -0
- package/assets/voice/ctxFull-01.opus +0 -0
- package/assets/voice/ctxHigh-00.opus +0 -0
- package/assets/voice/dawn-01.opus +0 -0
- package/assets/voice/dawn-04.opus +0 -0
- package/assets/voice/dawn-06.opus +0 -0
- package/assets/voice/dawn-07.opus +0 -0
- package/assets/voice/deepWake-03.opus +0 -0
- package/assets/voice/deepWake-04.opus +0 -0
- package/assets/voice/deepWake-06.opus +0 -0
- package/assets/voice/deepWake-07.opus +0 -0
- package/assets/voice/done-05.opus +0 -0
- package/assets/voice/done-08.opus +0 -0
- package/assets/voice/done-09.opus +0 -0
- package/assets/voice/done-11.opus +0 -0
- package/assets/voice/empty-00.opus +0 -0
- package/assets/voice/encourage-02.opus +0 -0
- package/assets/voice/failed-01.opus +0 -0
- package/assets/voice/failed-05.opus +0 -0
- package/assets/voice/failkind-auth-01.opus +0 -0
- package/assets/voice/failkind-auth-02.opus +0 -0
- package/assets/voice/failkind-ctx-00.opus +0 -0
- package/assets/voice/failkind-missing-01.opus +0 -0
- package/assets/voice/focusEnd-02.opus +0 -0
- package/assets/voice/focusEnd-04.opus +0 -0
- package/assets/voice/forgive-00.opus +0 -0
- package/assets/voice/hello-00.opus +0 -0
- package/assets/voice/hello-03.opus +0 -0
- package/assets/voice/hold-00.opus +0 -0
- package/assets/voice/idleTap-00.opus +0 -0
- package/assets/voice/idleTapLong-02.opus +0 -0
- package/assets/voice/lateNight-00.opus +0 -0
- package/assets/voice/lateNight-02.opus +0 -0
- package/assets/voice/lateNight-03.opus +0 -0
- package/assets/voice/lateNight-06.opus +0 -0
- package/assets/voice/lateNight-07.opus +0 -0
- package/assets/voice/murmur-04.opus +0 -0
- package/assets/voice/night-01.opus +0 -0
- package/assets/voice/night-03.opus +0 -0
- package/assets/voice/night-05.opus +0 -0
- package/assets/voice/noon-04.opus +0 -0
- package/assets/voice/petArm-02.opus +0 -0
- package/assets/voice/petCheek-00.opus +0 -0
- package/assets/voice/petCheek-01.opus +0 -0
- package/assets/voice/petCheek-02.opus +0 -0
- package/assets/voice/petHead-01.opus +0 -0
- package/assets/voice/petHead-03.opus +0 -0
- package/assets/voice/petHead-04.opus +0 -0
- package/assets/voice/praised-01.opus +0 -0
- package/assets/voice/praised-03.opus +0 -0
- package/assets/voice/question-00.opus +0 -0
- package/assets/voice/question-03.opus +0 -0
- package/assets/voice/question-04.opus +0 -0
- package/assets/voice/question-06.opus +0 -0
- package/assets/voice/shy-00.opus +0 -0
- package/assets/voice/spot-0-0.opus +0 -0
- package/assets/voice/spot-0-1.opus +0 -0
- package/assets/voice/spot-0-2.opus +0 -0
- package/assets/voice/spot-3-0.opus +0 -0
- package/assets/voice/spot-4-0.opus +0 -0
- package/assets/voice/spot-4-2.opus +0 -0
- package/assets/voice/spot-5-2.opus +0 -0
- package/assets/voice/spot-6-0.opus +0 -0
- package/assets/voice/spot-6-1.opus +0 -0
- package/assets/voice/spot-7-0.opus +0 -0
- package/assets/voice/spot-7-2.opus +0 -0
- package/assets/voice/spot-8-1.opus +0 -0
- package/assets/voice/tagent-00.opus +0 -0
- package/assets/voice/thinkFast-01.opus +0 -0
- package/assets/voice/thinkFast-02.opus +0 -0
- package/assets/voice/thinkFast-03.opus +0 -0
- package/assets/voice/thinkSlow-01.opus +0 -0
- package/assets/voice/thinkSlow-03.opus +0 -0
- package/assets/voice/thinking-01.opus +0 -0
- package/assets/voice/thinking-02.opus +0 -0
- package/assets/voice/thinking-03.opus +0 -0
- package/assets/voice/thinking-09.opus +0 -0
- package/assets/voice/tread-00.opus +0 -0
- package/assets/voice/tread-01.opus +0 -0
- package/assets/voice/trun-02.opus +0 -0
- package/assets/voice/tsun-00.opus +0 -0
- package/assets/voice/tsun-01.opus +0 -0
- package/assets/voice/tsun-04.opus +0 -0
- package/assets/voice/twrite-00.opus +0 -0
- package/assets/voice/unknown-01.opus +0 -0
- package/assets/voice/waitApproval-00.opus +0 -0
- package/assets/voice/waitApproval-01.opus +0 -0
- package/assets/voice/waitApproval-02.opus +0 -0
- package/assets/voice/waitApproval-03.opus +0 -0
- package/assets/voice/waitApprovalLate-02.opus +0 -0
- package/assets/voice/waitApprovalLate-03.opus +0 -0
- package/assets/voice/waitAsk-00.opus +0 -0
- package/assets/voice/waitAsk-02.opus +0 -0
- package/assets/voice/waitAskLate-03.opus +0 -0
- package/assets/voice/wake-03.opus +0 -0
- package/assets/voice/wake-04.opus +0 -0
- package/assets/voice/wake-05.opus +0 -0
- package/assets/voice/wake-06.opus +0 -0
- package/lib/client.js +7 -7
- package/lib/features/notes.js +172 -172
- package/lib/features/reminder.js +216 -216
- package/lib/l2d-plan.js +295 -295
- package/lib/l2d-tools.js +293 -293
- package/lib/ledger.js +163 -163
- package/lib/mys/config.js +183 -183
- package/lib/mys/ds.js +209 -209
- package/lib/mys/http.js +85 -85
- package/lib/mys/mys.js +419 -419
- package/lib/mys/qr.js +434 -434
- package/lib/mys/scheduler.js +206 -206
- package/lib/mys/store.js +248 -248
- package/lib/mys/tz.js +174 -174
- package/lib/wavtrim.js +245 -245
- package/lib/worldbook/NOTICE.md +18 -18
- package/lib/worldbook/data/Cyrene.md +111 -111
- package/lib/worldbook/data/_glossary.md +181 -181
- package/lib/worldbook/data/characters.md +289 -289
- package/lib/worldbook/data/story.md +130 -130
- package/lib/worldbook/data/world.md +182 -182
- package/lib/worldbook.js +189 -189
- package/package.json +72 -69
package/lib/features/reminder.js
CHANGED
|
@@ -1,216 +1,216 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 小昔涟 · 副插件 ①:提醒(reminder)
|
|
3
|
-
*
|
|
4
|
-
* 这是**同包里的第二个插件 entry**:`cordis.patch.yml` 里多一行,插件页就会多一行,
|
|
5
|
-
* 可以单独启停。它自己不画任何界面(一个包只有一份 client bundle),
|
|
6
|
-
* 面板那一屏由主客户端 `client.js` 通过下面这几条路由读写。
|
|
7
|
-
*
|
|
8
|
-
* 为什么这样切:
|
|
9
|
-
* - 数据独立(`~/.dsh/cyrene-live2d/reminder.json`),主包一个字节都不碰;
|
|
10
|
-
* - 路由独立(`/cyrene-reminder/...`),主包挂了它照样能用;
|
|
11
|
-
* - 它挂了也不影响桌宠(各是各的 fiber)。
|
|
12
|
-
*
|
|
13
|
-
* 约定:宿主只负责「存 / 读 / 到点标 fired」,**不负责出声**。
|
|
14
|
-
* 出声的是主客户端(它手上才有语音),靠轮询 `/api/due` 发现该说了。
|
|
15
|
-
*/
|
|
16
|
-
import fs from 'node:fs'
|
|
17
|
-
import path from 'node:path'
|
|
18
|
-
import { homedir } from 'node:os'
|
|
19
|
-
import { guardLocalRequest } from '../http-guard.js'
|
|
20
|
-
|
|
21
|
-
const PACKAGE_ID = 'dsh-plugin-cyrene-live2d'
|
|
22
|
-
const PREFIX = '/cyrene-reminder'
|
|
23
|
-
// 2026-11-18: honour DSH_HOME like the main plugin does, otherwise the data
|
|
24
|
-
// directory silently splits (uninstall/migrate/backup miss the two sub-plugins).
|
|
25
|
-
const DSH_HOME = process.env.DSH_HOME || path.join(homedir(), '.dsh')
|
|
26
|
-
const DATA_DIR = path.join(DSH_HOME, 'cyrene-live2d')
|
|
27
|
-
const FILE = path.join(DATA_DIR, 'reminder.json')
|
|
28
|
-
/** 到点后最多补说这么久;再旧的就不说了(免得开机一口气念五条)。 */
|
|
29
|
-
const GRACE_MS = 30 * 60 * 1000
|
|
30
|
-
|
|
31
|
-
export const inject = ['webServer']
|
|
32
|
-
|
|
33
|
-
/* ───────────────── 存储 ───────────────── */
|
|
34
|
-
|
|
35
|
-
function readStore() {
|
|
36
|
-
try {
|
|
37
|
-
const j = JSON.parse(fs.readFileSync(FILE, 'utf8'))
|
|
38
|
-
if (j && Array.isArray(j.items)) return { items: j.items }
|
|
39
|
-
} catch {}
|
|
40
|
-
return { items: [] }
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** 原子写:tmp → fsync → rename(和主包同一套规矩,别让半份 JSON 落地)。 */
|
|
44
|
-
function writeStore(store) {
|
|
45
|
-
try {
|
|
46
|
-
fs.mkdirSync(DATA_DIR, { recursive: true })
|
|
47
|
-
const tmp = FILE + '.tmp'
|
|
48
|
-
const buf = Buffer.from(JSON.stringify({ v: 1, items: store.items }, null, 2), 'utf8')
|
|
49
|
-
const fd = fs.openSync(tmp, 'w')
|
|
50
|
-
try { fs.writeSync(fd, buf, 0, buf.length); try { fs.fsyncSync(fd) } catch {} } finally { fs.closeSync(fd) }
|
|
51
|
-
fs.renameSync(tmp, FILE)
|
|
52
|
-
return true
|
|
53
|
-
} catch { return false }
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/* ───────────────── 数据形状 ───────────────── */
|
|
57
|
-
|
|
58
|
-
const MAX_ITEMS = 50
|
|
59
|
-
const MAX_TEXT = 200
|
|
60
|
-
|
|
61
|
-
function normalizeItem(raw) {
|
|
62
|
-
const at = Number(raw && raw.at)
|
|
63
|
-
const text = String((raw && raw.text) || '').trim().slice(0, MAX_TEXT)
|
|
64
|
-
if (!Number.isFinite(at) || at <= 0 || !text) return null
|
|
65
|
-
return {
|
|
66
|
-
id: String((raw && raw.id) || '') || 'r' + Math.random().toString(36).slice(2, 10),
|
|
67
|
-
at,
|
|
68
|
-
text,
|
|
69
|
-
fired: !!(raw && raw.fired),
|
|
70
|
-
createdAt: Number((raw && raw.createdAt) || Date.now()),
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/* ───────────────── 路由 ───────────────── */
|
|
75
|
-
|
|
76
|
-
function sendJson(res, status, obj) {
|
|
77
|
-
const body = Buffer.from(JSON.stringify(obj), 'utf8')
|
|
78
|
-
res.writeHead(status, {
|
|
79
|
-
'content-type': 'application/json; charset=utf-8',
|
|
80
|
-
'content-length': body.length,
|
|
81
|
-
'cache-control': 'no-store',
|
|
82
|
-
})
|
|
83
|
-
res.end(body)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async function readBody(req, limit) {
|
|
87
|
-
const chunks = []
|
|
88
|
-
let size = 0
|
|
89
|
-
for await (const chunk of req) {
|
|
90
|
-
size += chunk.length
|
|
91
|
-
if (size > limit) throw new Error('body too large')
|
|
92
|
-
chunks.push(chunk)
|
|
93
|
-
}
|
|
94
|
-
return Buffer.concat(chunks).toString('utf8')
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** 到点且还没说过的(给主客户端轮询用)。 */
|
|
98
|
-
function dueList(now) {
|
|
99
|
-
const store = readStore()
|
|
100
|
-
return store.items
|
|
101
|
-
.filter((it) => !it.fired && it.at <= now && now - it.at <= GRACE_MS)
|
|
102
|
-
.sort((a, b) => a.at - b.at)
|
|
103
|
-
.slice(0, 3)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function apply(ctx) {
|
|
107
|
-
// 启动自检:目录在不在、文件能不能读——只写日志,不抛。
|
|
108
|
-
try {
|
|
109
|
-
fs.mkdirSync(DATA_DIR, { recursive: true })
|
|
110
|
-
const s = readStore()
|
|
111
|
-
ctx.logger?.info?.(`${PACKAGE_ID}: 提醒插件已启动(${s.items.length} 条)`)
|
|
112
|
-
} catch (e) {
|
|
113
|
-
ctx.logger?.warn?.(`${PACKAGE_ID}: 提醒插件启动时读不到数据(会用空列表)`, e)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// ★ handler 只收 (req, res):第三个参数是 undefined(主插件是这么写的,我们照抄才对)。
|
|
117
|
-
// 早先写成 (req,res,url) 直接读 url.pathname → TypeError → 宿主回 400 空响应。
|
|
118
|
-
ctx.effect(() => ctx.webServer.register({
|
|
119
|
-
kind: 'prefix',
|
|
120
|
-
path: PREFIX,
|
|
121
|
-
handler: async (req, res) => {
|
|
122
|
-
let rel = '/'
|
|
123
|
-
try {
|
|
124
|
-
const url = new URL(req.url || '/', 'http://localhost')
|
|
125
|
-
rel = decodeURIComponent(url.pathname.slice(PREFIX.length)) || '/'
|
|
126
|
-
} catch {
|
|
127
|
-
sendJson(res, 400, { ok: false, error: 'bad url' })
|
|
128
|
-
return
|
|
129
|
-
}
|
|
130
|
-
// 只接受本地同源写请求(2026-11-18)
|
|
131
|
-
if (rel.startsWith('/api/') && !guardLocalRequest(req, res, sendJson)) return
|
|
132
|
-
const now = Date.now()
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
if (rel === '/api/list') {
|
|
136
|
-
if (req.method !== 'GET') { sendJson(res, 405, { ok: false, error: 'method not allowed' }); return }
|
|
137
|
-
const store = readStore()
|
|
138
|
-
sendJson(res, 200, {
|
|
139
|
-
ok: true,
|
|
140
|
-
now,
|
|
141
|
-
graceMs: GRACE_MS,
|
|
142
|
-
items: store.items.slice().sort((a, b) => a.at - b.at),
|
|
143
|
-
due: dueList(now),
|
|
144
|
-
})
|
|
145
|
-
return
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (rel === '/api/due') {
|
|
149
|
-
if (req.method !== 'GET') { sendJson(res, 405, { ok: false, error: 'method not allowed' }); return }
|
|
150
|
-
sendJson(res, 200, { ok: true, now, due: dueList(now) })
|
|
151
|
-
return
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (req.method !== 'POST') { sendJson(res, 405, { ok: false, error: 'method not allowed' }); return }
|
|
155
|
-
|
|
156
|
-
const raw = await readBody(req, 16 * 1024)
|
|
157
|
-
const payload = JSON.parse(String(raw || '{}'))
|
|
158
|
-
const store = readStore()
|
|
159
|
-
|
|
160
|
-
if (rel === '/api/add') {
|
|
161
|
-
const item = normalizeItem({ ...payload, createdAt: now, fired: false })
|
|
162
|
-
if (!item) { sendJson(res, 400, { ok: false, error: 'bad item' }); return }
|
|
163
|
-
store.items.push(item)
|
|
164
|
-
store.items.sort((a, b) => a.at - b.at)
|
|
165
|
-
while (store.items.length > MAX_ITEMS) store.items.shift() // 超了丢最早的
|
|
166
|
-
|
|
167
|
-
// 写盘失败必须报错:以前这里不回查 writeStore 的返回值,磁盘满/权限被拒时会「看起来存上了」。
|
|
168
|
-
|
|
169
|
-
if (!writeStore(store)) { sendJson(res, 500, { ok: false, error: 'write failed' }); return }
|
|
170
|
-
sendJson(res, 200, { ok: true, item, count: store.items.length })
|
|
171
|
-
return
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (rel === '/api/del') {
|
|
175
|
-
const id = String(payload.id || '')
|
|
176
|
-
const before = store.items.length
|
|
177
|
-
store.items = store.items.filter((it) => it.id !== id)
|
|
178
|
-
|
|
179
|
-
if (!writeStore(store)) { sendJson(res, 500, { ok: false, error: 'write failed' }); return }
|
|
180
|
-
sendJson(res, 200, { ok: true, removed: before - store.items.length })
|
|
181
|
-
return
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (rel === '/api/ack') {
|
|
185
|
-
// 主客户端说完之后来标一次:fired=true,不再重复播。
|
|
186
|
-
const id = String(payload.id || '')
|
|
187
|
-
const hit = store.items.find((it) => it.id === id)
|
|
188
|
-
if (hit) {
|
|
189
|
-
hit.fired = true
|
|
190
|
-
if (!writeStore(store)) { sendJson(res, 500, { ok: false, error: 'write failed' }); return }
|
|
191
|
-
}
|
|
192
|
-
sendJson(res, 200, { ok: !!hit })
|
|
193
|
-
return
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
sendJson(res, 404, { ok: false, error: 'unknown endpoint' })
|
|
197
|
-
} catch (e) {
|
|
198
|
-
try { sendJson(res, 500, { ok: false, error: String((e && e.message) || e) }) } catch {}
|
|
199
|
-
}
|
|
200
|
-
},
|
|
201
|
-
}))
|
|
202
|
-
|
|
203
|
-
// 定时器:每分钟把「过期太久还没说」的标记成 fired,避免开机补念一堆。
|
|
204
|
-
const sweep = setInterval(() => {
|
|
205
|
-
try {
|
|
206
|
-
const now = Date.now()
|
|
207
|
-
const store = readStore()
|
|
208
|
-
let changed = false
|
|
209
|
-
for (const it of store.items) {
|
|
210
|
-
if (!it.fired && now - it.at > GRACE_MS) { it.fired = true; changed = true }
|
|
211
|
-
}
|
|
212
|
-
if (changed) writeStore(store)
|
|
213
|
-
} catch {}
|
|
214
|
-
}, 60 * 1000)
|
|
215
|
-
ctx.effect(() => () => { try { clearInterval(sweep) } catch {} })
|
|
216
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 小昔涟 · 副插件 ①:提醒(reminder)
|
|
3
|
+
*
|
|
4
|
+
* 这是**同包里的第二个插件 entry**:`cordis.patch.yml` 里多一行,插件页就会多一行,
|
|
5
|
+
* 可以单独启停。它自己不画任何界面(一个包只有一份 client bundle),
|
|
6
|
+
* 面板那一屏由主客户端 `client.js` 通过下面这几条路由读写。
|
|
7
|
+
*
|
|
8
|
+
* 为什么这样切:
|
|
9
|
+
* - 数据独立(`~/.dsh/cyrene-live2d/reminder.json`),主包一个字节都不碰;
|
|
10
|
+
* - 路由独立(`/cyrene-reminder/...`),主包挂了它照样能用;
|
|
11
|
+
* - 它挂了也不影响桌宠(各是各的 fiber)。
|
|
12
|
+
*
|
|
13
|
+
* 约定:宿主只负责「存 / 读 / 到点标 fired」,**不负责出声**。
|
|
14
|
+
* 出声的是主客户端(它手上才有语音),靠轮询 `/api/due` 发现该说了。
|
|
15
|
+
*/
|
|
16
|
+
import fs from 'node:fs'
|
|
17
|
+
import path from 'node:path'
|
|
18
|
+
import { homedir } from 'node:os'
|
|
19
|
+
import { guardLocalRequest } from '../http-guard.js'
|
|
20
|
+
|
|
21
|
+
const PACKAGE_ID = 'dsh-plugin-cyrene-live2d'
|
|
22
|
+
const PREFIX = '/cyrene-reminder'
|
|
23
|
+
// 2026-11-18: honour DSH_HOME like the main plugin does, otherwise the data
|
|
24
|
+
// directory silently splits (uninstall/migrate/backup miss the two sub-plugins).
|
|
25
|
+
const DSH_HOME = process.env.DSH_HOME || path.join(homedir(), '.dsh')
|
|
26
|
+
const DATA_DIR = path.join(DSH_HOME, 'cyrene-live2d')
|
|
27
|
+
const FILE = path.join(DATA_DIR, 'reminder.json')
|
|
28
|
+
/** 到点后最多补说这么久;再旧的就不说了(免得开机一口气念五条)。 */
|
|
29
|
+
const GRACE_MS = 30 * 60 * 1000
|
|
30
|
+
|
|
31
|
+
export const inject = ['webServer']
|
|
32
|
+
|
|
33
|
+
/* ───────────────── 存储 ───────────────── */
|
|
34
|
+
|
|
35
|
+
function readStore() {
|
|
36
|
+
try {
|
|
37
|
+
const j = JSON.parse(fs.readFileSync(FILE, 'utf8'))
|
|
38
|
+
if (j && Array.isArray(j.items)) return { items: j.items }
|
|
39
|
+
} catch {}
|
|
40
|
+
return { items: [] }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 原子写:tmp → fsync → rename(和主包同一套规矩,别让半份 JSON 落地)。 */
|
|
44
|
+
function writeStore(store) {
|
|
45
|
+
try {
|
|
46
|
+
fs.mkdirSync(DATA_DIR, { recursive: true })
|
|
47
|
+
const tmp = FILE + '.tmp'
|
|
48
|
+
const buf = Buffer.from(JSON.stringify({ v: 1, items: store.items }, null, 2), 'utf8')
|
|
49
|
+
const fd = fs.openSync(tmp, 'w')
|
|
50
|
+
try { fs.writeSync(fd, buf, 0, buf.length); try { fs.fsyncSync(fd) } catch {} } finally { fs.closeSync(fd) }
|
|
51
|
+
fs.renameSync(tmp, FILE)
|
|
52
|
+
return true
|
|
53
|
+
} catch { return false }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* ───────────────── 数据形状 ───────────────── */
|
|
57
|
+
|
|
58
|
+
const MAX_ITEMS = 50
|
|
59
|
+
const MAX_TEXT = 200
|
|
60
|
+
|
|
61
|
+
function normalizeItem(raw) {
|
|
62
|
+
const at = Number(raw && raw.at)
|
|
63
|
+
const text = String((raw && raw.text) || '').trim().slice(0, MAX_TEXT)
|
|
64
|
+
if (!Number.isFinite(at) || at <= 0 || !text) return null
|
|
65
|
+
return {
|
|
66
|
+
id: String((raw && raw.id) || '') || 'r' + Math.random().toString(36).slice(2, 10),
|
|
67
|
+
at,
|
|
68
|
+
text,
|
|
69
|
+
fired: !!(raw && raw.fired),
|
|
70
|
+
createdAt: Number((raw && raw.createdAt) || Date.now()),
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* ───────────────── 路由 ───────────────── */
|
|
75
|
+
|
|
76
|
+
function sendJson(res, status, obj) {
|
|
77
|
+
const body = Buffer.from(JSON.stringify(obj), 'utf8')
|
|
78
|
+
res.writeHead(status, {
|
|
79
|
+
'content-type': 'application/json; charset=utf-8',
|
|
80
|
+
'content-length': body.length,
|
|
81
|
+
'cache-control': 'no-store',
|
|
82
|
+
})
|
|
83
|
+
res.end(body)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function readBody(req, limit) {
|
|
87
|
+
const chunks = []
|
|
88
|
+
let size = 0
|
|
89
|
+
for await (const chunk of req) {
|
|
90
|
+
size += chunk.length
|
|
91
|
+
if (size > limit) throw new Error('body too large')
|
|
92
|
+
chunks.push(chunk)
|
|
93
|
+
}
|
|
94
|
+
return Buffer.concat(chunks).toString('utf8')
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 到点且还没说过的(给主客户端轮询用)。 */
|
|
98
|
+
function dueList(now) {
|
|
99
|
+
const store = readStore()
|
|
100
|
+
return store.items
|
|
101
|
+
.filter((it) => !it.fired && it.at <= now && now - it.at <= GRACE_MS)
|
|
102
|
+
.sort((a, b) => a.at - b.at)
|
|
103
|
+
.slice(0, 3)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function apply(ctx) {
|
|
107
|
+
// 启动自检:目录在不在、文件能不能读——只写日志,不抛。
|
|
108
|
+
try {
|
|
109
|
+
fs.mkdirSync(DATA_DIR, { recursive: true })
|
|
110
|
+
const s = readStore()
|
|
111
|
+
ctx.logger?.info?.(`${PACKAGE_ID}: 提醒插件已启动(${s.items.length} 条)`)
|
|
112
|
+
} catch (e) {
|
|
113
|
+
ctx.logger?.warn?.(`${PACKAGE_ID}: 提醒插件启动时读不到数据(会用空列表)`, e)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ★ handler 只收 (req, res):第三个参数是 undefined(主插件是这么写的,我们照抄才对)。
|
|
117
|
+
// 早先写成 (req,res,url) 直接读 url.pathname → TypeError → 宿主回 400 空响应。
|
|
118
|
+
ctx.effect(() => ctx.webServer.register({
|
|
119
|
+
kind: 'prefix',
|
|
120
|
+
path: PREFIX,
|
|
121
|
+
handler: async (req, res) => {
|
|
122
|
+
let rel = '/'
|
|
123
|
+
try {
|
|
124
|
+
const url = new URL(req.url || '/', 'http://localhost')
|
|
125
|
+
rel = decodeURIComponent(url.pathname.slice(PREFIX.length)) || '/'
|
|
126
|
+
} catch {
|
|
127
|
+
sendJson(res, 400, { ok: false, error: 'bad url' })
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
// 只接受本地同源写请求(2026-11-18)
|
|
131
|
+
if (rel.startsWith('/api/') && !guardLocalRequest(req, res, sendJson)) return
|
|
132
|
+
const now = Date.now()
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
if (rel === '/api/list') {
|
|
136
|
+
if (req.method !== 'GET') { sendJson(res, 405, { ok: false, error: 'method not allowed' }); return }
|
|
137
|
+
const store = readStore()
|
|
138
|
+
sendJson(res, 200, {
|
|
139
|
+
ok: true,
|
|
140
|
+
now,
|
|
141
|
+
graceMs: GRACE_MS,
|
|
142
|
+
items: store.items.slice().sort((a, b) => a.at - b.at),
|
|
143
|
+
due: dueList(now),
|
|
144
|
+
})
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (rel === '/api/due') {
|
|
149
|
+
if (req.method !== 'GET') { sendJson(res, 405, { ok: false, error: 'method not allowed' }); return }
|
|
150
|
+
sendJson(res, 200, { ok: true, now, due: dueList(now) })
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (req.method !== 'POST') { sendJson(res, 405, { ok: false, error: 'method not allowed' }); return }
|
|
155
|
+
|
|
156
|
+
const raw = await readBody(req, 16 * 1024)
|
|
157
|
+
const payload = JSON.parse(String(raw || '{}'))
|
|
158
|
+
const store = readStore()
|
|
159
|
+
|
|
160
|
+
if (rel === '/api/add') {
|
|
161
|
+
const item = normalizeItem({ ...payload, createdAt: now, fired: false })
|
|
162
|
+
if (!item) { sendJson(res, 400, { ok: false, error: 'bad item' }); return }
|
|
163
|
+
store.items.push(item)
|
|
164
|
+
store.items.sort((a, b) => a.at - b.at)
|
|
165
|
+
while (store.items.length > MAX_ITEMS) store.items.shift() // 超了丢最早的
|
|
166
|
+
|
|
167
|
+
// 写盘失败必须报错:以前这里不回查 writeStore 的返回值,磁盘满/权限被拒时会「看起来存上了」。
|
|
168
|
+
|
|
169
|
+
if (!writeStore(store)) { sendJson(res, 500, { ok: false, error: 'write failed' }); return }
|
|
170
|
+
sendJson(res, 200, { ok: true, item, count: store.items.length })
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (rel === '/api/del') {
|
|
175
|
+
const id = String(payload.id || '')
|
|
176
|
+
const before = store.items.length
|
|
177
|
+
store.items = store.items.filter((it) => it.id !== id)
|
|
178
|
+
|
|
179
|
+
if (!writeStore(store)) { sendJson(res, 500, { ok: false, error: 'write failed' }); return }
|
|
180
|
+
sendJson(res, 200, { ok: true, removed: before - store.items.length })
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (rel === '/api/ack') {
|
|
185
|
+
// 主客户端说完之后来标一次:fired=true,不再重复播。
|
|
186
|
+
const id = String(payload.id || '')
|
|
187
|
+
const hit = store.items.find((it) => it.id === id)
|
|
188
|
+
if (hit) {
|
|
189
|
+
hit.fired = true
|
|
190
|
+
if (!writeStore(store)) { sendJson(res, 500, { ok: false, error: 'write failed' }); return }
|
|
191
|
+
}
|
|
192
|
+
sendJson(res, 200, { ok: !!hit })
|
|
193
|
+
return
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
sendJson(res, 404, { ok: false, error: 'unknown endpoint' })
|
|
197
|
+
} catch (e) {
|
|
198
|
+
try { sendJson(res, 500, { ok: false, error: String((e && e.message) || e) }) } catch {}
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
}))
|
|
202
|
+
|
|
203
|
+
// 定时器:每分钟把「过期太久还没说」的标记成 fired,避免开机补念一堆。
|
|
204
|
+
const sweep = setInterval(() => {
|
|
205
|
+
try {
|
|
206
|
+
const now = Date.now()
|
|
207
|
+
const store = readStore()
|
|
208
|
+
let changed = false
|
|
209
|
+
for (const it of store.items) {
|
|
210
|
+
if (!it.fired && now - it.at > GRACE_MS) { it.fired = true; changed = true }
|
|
211
|
+
}
|
|
212
|
+
if (changed) writeStore(store)
|
|
213
|
+
} catch {}
|
|
214
|
+
}, 60 * 1000)
|
|
215
|
+
ctx.effect(() => () => { try { clearInterval(sweep) } catch {} })
|
|
216
|
+
}
|