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.
Files changed (210) hide show
  1. package/README.md +8 -2
  2. package/assets/default-prefs.json +8 -8
  3. package/assets/icon.svg +89 -89
  4. package/assets/locale/notes/en.json +6 -6
  5. package/assets/locale/notes/zh-CN.json +6 -6
  6. package/assets/locale/reminder/en.json +6 -6
  7. package/assets/locale/reminder/zh-CN.json +6 -6
  8. package/assets/model/Cyrene.model3.json +441 -441
  9. package/assets/model/bongo-standard/Live2d_expression0.exp3.json +9 -9
  10. package/assets/model/bongo-standard/cat.cdi3.json +296 -296
  11. package/assets/model/bongo-standard/cat.model3.json +33 -33
  12. package/assets/model/bongo-standard/cat.physics3.json +232 -232
  13. package/assets/model/expressions/0.exp3.json +24 -24
  14. package/assets/model/expressions/1.exp3.json +11 -11
  15. package/assets/model/expressions/10.exp3.json +9 -9
  16. package/assets/model/expressions/11.exp3.json +9 -9
  17. package/assets/model/expressions/2.exp3.json +9 -9
  18. package/assets/model/expressions/3.exp3.json +9 -9
  19. package/assets/model/expressions/4.exp3.json +11 -11
  20. package/assets/model/expressions/5.exp3.json +11 -11
  21. package/assets/model/expressions/6.exp3.json +11 -11
  22. package/assets/model/expressions/7.exp3.json +11 -11
  23. package/assets/model/expressions/8.exp3.json +11 -11
  24. package/assets/model/expressions/9.exp3.json +9 -9
  25. package/assets/model/model.physics3.json +641 -641
  26. package/assets/model/motions/Tick3_3.motion3.json +1347 -1347
  27. package/assets/model/motions//345/212/250/344/275/2346_0.motion3.json +699 -699
  28. package/assets/model/motions//345/212/250/344/275/2346_1.motion3.json +1241 -1241
  29. package/assets/model/motions//345/212/250/344/275/2346_2.motion3.json +1117 -1117
  30. package/assets/model/motions//345/212/250/344/275/2346_3.motion3.json +1106 -1106
  31. package/assets/model/we-q/expressions/FileReferences_Expressions_0_File_0.exp3.json +19 -19
  32. package/assets/model/we-q/expressions/FileReferences_Expressions_1_File_0.exp3.json +41 -41
  33. package/assets/model/we-q/expressions/FileReferences_Expressions_2_File_0.exp3.json +104 -104
  34. package/assets/model/we-q/model0.model3.json +245 -245
  35. package/assets/model/we-q/motions/FileReferences_Motions_shake#1_0_File_0.motion3.json +520 -520
  36. package/assets/model/we-q/motions/FileReferences_Motions_talk#1_0_File_0.motion3.json +499 -499
  37. package/assets/model/we-q/motions/FileReferences_Motions_talk#1_1_File_0.motion3.json +453 -453
  38. package/assets/model/we-q/motions/FileReferences_Motions_wink#1_0_File_0.motion3.json +478 -478
  39. package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_0_File_0.motion3.json +450 -450
  40. package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_1_File_0.motion3.json +450 -450
  41. package/assets/model/we-q/motions/idle/FileReferences_Motions_Idleidle_0_File_0.motion3.json +422 -422
  42. package/assets/model/we-q/physics/FileReferences_Physics_0.physic3.json +236 -236
  43. package/assets/runtime/live2dcubismcore.min.js +8 -8
  44. package/assets/stickers.json +84 -84
  45. package/assets/vendor/cubism4.min.js +1 -1
  46. package/assets/vendor/pixi.min.js +9 -9
  47. package/assets/vendor/theme-cyrene-factory.js +4314 -4314
  48. package/assets/vendor/theme-cyrene.js +4317 -4317
  49. package/assets/voice/angry-00.opus +0 -0
  50. package/assets/voice/angry-02.opus +0 -0
  51. package/assets/voice/approval-00.opus +0 -0
  52. package/assets/voice/approval-01.opus +0 -0
  53. package/assets/voice/approval-02.opus +0 -0
  54. package/assets/voice/askAck-00.opus +0 -0
  55. package/assets/voice/away-01.opus +0 -0
  56. package/assets/voice/away-02.opus +0 -0
  57. package/assets/voice/away-03.opus +0 -0
  58. package/assets/voice/away-05.opus +0 -0
  59. package/assets/voice/back-00.opus +0 -0
  60. package/assets/voice/back-01.opus +0 -0
  61. package/assets/voice/backLong-05.opus +0 -0
  62. package/assets/voice/backShort-03.opus +0 -0
  63. package/assets/voice/backShort-04.opus +0 -0
  64. package/assets/voice/backShort-07.opus +0 -0
  65. package/assets/voice/backVeryLong-01.opus +0 -0
  66. package/assets/voice/backVeryLong-02.opus +0 -0
  67. package/assets/voice/backVeryLong-03.opus +0 -0
  68. package/assets/voice/backVeryLong-04.opus +0 -0
  69. package/assets/voice/backVeryLong-05.opus +0 -0
  70. package/assets/voice/backVeryLong-07.opus +0 -0
  71. package/assets/voice/backVeryLong-08.opus +0 -0
  72. package/assets/voice/balLow-00.opus +0 -0
  73. package/assets/voice/balLow-01.opus +0 -0
  74. package/assets/voice/birthday-02.opus +0 -0
  75. package/assets/voice/click-00.opus +0 -0
  76. package/assets/voice/click-01.opus +0 -0
  77. package/assets/voice/click-07.opus +0 -0
  78. package/assets/voice/click-10.opus +0 -0
  79. package/assets/voice/click-11.opus +0 -0
  80. package/assets/voice/click-12.opus +0 -0
  81. package/assets/voice/click-16.opus +0 -0
  82. package/assets/voice/click-17.opus +0 -0
  83. package/assets/voice/click-18.opus +0 -0
  84. package/assets/voice/comfort-03.opus +0 -0
  85. package/assets/voice/comfort-04.opus +0 -0
  86. package/assets/voice/cost10-00.opus +0 -0
  87. package/assets/voice/cost2-00.opus +0 -0
  88. package/assets/voice/cost5-00.opus +0 -0
  89. package/assets/voice/costTurn-01.opus +0 -0
  90. package/assets/voice/costTurn-02.opus +0 -0
  91. package/assets/voice/ctxFull-01.opus +0 -0
  92. package/assets/voice/ctxHigh-00.opus +0 -0
  93. package/assets/voice/dawn-01.opus +0 -0
  94. package/assets/voice/dawn-04.opus +0 -0
  95. package/assets/voice/dawn-06.opus +0 -0
  96. package/assets/voice/dawn-07.opus +0 -0
  97. package/assets/voice/deepWake-03.opus +0 -0
  98. package/assets/voice/deepWake-04.opus +0 -0
  99. package/assets/voice/deepWake-06.opus +0 -0
  100. package/assets/voice/deepWake-07.opus +0 -0
  101. package/assets/voice/done-05.opus +0 -0
  102. package/assets/voice/done-08.opus +0 -0
  103. package/assets/voice/done-09.opus +0 -0
  104. package/assets/voice/done-11.opus +0 -0
  105. package/assets/voice/empty-00.opus +0 -0
  106. package/assets/voice/encourage-02.opus +0 -0
  107. package/assets/voice/failed-01.opus +0 -0
  108. package/assets/voice/failed-05.opus +0 -0
  109. package/assets/voice/failkind-auth-01.opus +0 -0
  110. package/assets/voice/failkind-auth-02.opus +0 -0
  111. package/assets/voice/failkind-ctx-00.opus +0 -0
  112. package/assets/voice/failkind-missing-01.opus +0 -0
  113. package/assets/voice/focusEnd-02.opus +0 -0
  114. package/assets/voice/focusEnd-04.opus +0 -0
  115. package/assets/voice/forgive-00.opus +0 -0
  116. package/assets/voice/hello-00.opus +0 -0
  117. package/assets/voice/hello-03.opus +0 -0
  118. package/assets/voice/hold-00.opus +0 -0
  119. package/assets/voice/idleTap-00.opus +0 -0
  120. package/assets/voice/idleTapLong-02.opus +0 -0
  121. package/assets/voice/lateNight-00.opus +0 -0
  122. package/assets/voice/lateNight-02.opus +0 -0
  123. package/assets/voice/lateNight-03.opus +0 -0
  124. package/assets/voice/lateNight-06.opus +0 -0
  125. package/assets/voice/lateNight-07.opus +0 -0
  126. package/assets/voice/murmur-04.opus +0 -0
  127. package/assets/voice/night-01.opus +0 -0
  128. package/assets/voice/night-03.opus +0 -0
  129. package/assets/voice/night-05.opus +0 -0
  130. package/assets/voice/noon-04.opus +0 -0
  131. package/assets/voice/petArm-02.opus +0 -0
  132. package/assets/voice/petCheek-00.opus +0 -0
  133. package/assets/voice/petCheek-01.opus +0 -0
  134. package/assets/voice/petCheek-02.opus +0 -0
  135. package/assets/voice/petHead-01.opus +0 -0
  136. package/assets/voice/petHead-03.opus +0 -0
  137. package/assets/voice/petHead-04.opus +0 -0
  138. package/assets/voice/praised-01.opus +0 -0
  139. package/assets/voice/praised-03.opus +0 -0
  140. package/assets/voice/question-00.opus +0 -0
  141. package/assets/voice/question-03.opus +0 -0
  142. package/assets/voice/question-04.opus +0 -0
  143. package/assets/voice/question-06.opus +0 -0
  144. package/assets/voice/shy-00.opus +0 -0
  145. package/assets/voice/spot-0-0.opus +0 -0
  146. package/assets/voice/spot-0-1.opus +0 -0
  147. package/assets/voice/spot-0-2.opus +0 -0
  148. package/assets/voice/spot-3-0.opus +0 -0
  149. package/assets/voice/spot-4-0.opus +0 -0
  150. package/assets/voice/spot-4-2.opus +0 -0
  151. package/assets/voice/spot-5-2.opus +0 -0
  152. package/assets/voice/spot-6-0.opus +0 -0
  153. package/assets/voice/spot-6-1.opus +0 -0
  154. package/assets/voice/spot-7-0.opus +0 -0
  155. package/assets/voice/spot-7-2.opus +0 -0
  156. package/assets/voice/spot-8-1.opus +0 -0
  157. package/assets/voice/tagent-00.opus +0 -0
  158. package/assets/voice/thinkFast-01.opus +0 -0
  159. package/assets/voice/thinkFast-02.opus +0 -0
  160. package/assets/voice/thinkFast-03.opus +0 -0
  161. package/assets/voice/thinkSlow-01.opus +0 -0
  162. package/assets/voice/thinkSlow-03.opus +0 -0
  163. package/assets/voice/thinking-01.opus +0 -0
  164. package/assets/voice/thinking-02.opus +0 -0
  165. package/assets/voice/thinking-03.opus +0 -0
  166. package/assets/voice/thinking-09.opus +0 -0
  167. package/assets/voice/tread-00.opus +0 -0
  168. package/assets/voice/tread-01.opus +0 -0
  169. package/assets/voice/trun-02.opus +0 -0
  170. package/assets/voice/tsun-00.opus +0 -0
  171. package/assets/voice/tsun-01.opus +0 -0
  172. package/assets/voice/tsun-04.opus +0 -0
  173. package/assets/voice/twrite-00.opus +0 -0
  174. package/assets/voice/unknown-01.opus +0 -0
  175. package/assets/voice/waitApproval-00.opus +0 -0
  176. package/assets/voice/waitApproval-01.opus +0 -0
  177. package/assets/voice/waitApproval-02.opus +0 -0
  178. package/assets/voice/waitApproval-03.opus +0 -0
  179. package/assets/voice/waitApprovalLate-02.opus +0 -0
  180. package/assets/voice/waitApprovalLate-03.opus +0 -0
  181. package/assets/voice/waitAsk-00.opus +0 -0
  182. package/assets/voice/waitAsk-02.opus +0 -0
  183. package/assets/voice/waitAskLate-03.opus +0 -0
  184. package/assets/voice/wake-03.opus +0 -0
  185. package/assets/voice/wake-04.opus +0 -0
  186. package/assets/voice/wake-05.opus +0 -0
  187. package/assets/voice/wake-06.opus +0 -0
  188. package/lib/client.js +7 -7
  189. package/lib/features/notes.js +172 -172
  190. package/lib/features/reminder.js +216 -216
  191. package/lib/l2d-plan.js +295 -295
  192. package/lib/l2d-tools.js +293 -293
  193. package/lib/ledger.js +163 -163
  194. package/lib/mys/config.js +183 -183
  195. package/lib/mys/ds.js +209 -209
  196. package/lib/mys/http.js +85 -85
  197. package/lib/mys/mys.js +419 -419
  198. package/lib/mys/qr.js +434 -434
  199. package/lib/mys/scheduler.js +206 -206
  200. package/lib/mys/store.js +248 -248
  201. package/lib/mys/tz.js +174 -174
  202. package/lib/wavtrim.js +245 -245
  203. package/lib/worldbook/NOTICE.md +18 -18
  204. package/lib/worldbook/data/Cyrene.md +111 -111
  205. package/lib/worldbook/data/_glossary.md +181 -181
  206. package/lib/worldbook/data/characters.md +289 -289
  207. package/lib/worldbook/data/story.md +130 -130
  208. package/lib/worldbook/data/world.md +182 -182
  209. package/lib/worldbook.js +189 -189
  210. package/package.json +72 -69
package/lib/l2d-plan.js CHANGED
@@ -1,295 +1,295 @@
1
- /**
2
- * 小昔涟 · 语义动作协议 v1 —— 编译器(纯函数,零依赖)
3
- *
4
- * 借自 鲸语 Jingyu Live2D Agent 的 compiler.py(MIT):
5
- * "Semantic action IR -> bounded, eased parameter tracks. Never parses prose."
6
- * 这条纪律照抄:**这里只吃结构化计划,绝不解析自然语言**。自然语言由模型出成
7
- * {version:1, actions:[...]},本地只做校验与编译。
8
- *
9
- * 三条硬规矩(都是他们踩出来的,2026-11-07 移植):
10
- * 1. 参数不在能力目录里 → 直接拒绝(不能让模型瞎编参数名)
11
- * 2. 参数是作者物理的输出 → 直接拒绝(驱动它会和物理打架,画面抽搐)
12
- * 3. 同一参数被两个动作重叠驱动(>15ms)→ 直接拒绝,除非显式 expression overlay
13
- *
14
- * 为什么把「compile」和「execute」拆开:校验失败时**根本不该执行**,
15
- * 只该把错误交回给模型让它改一次。合起来就只能"先做再发现问题"。
16
- *
17
- * 这份文件在两边各存一份同字节副本(插件 lib/ 与独立版 src/),
18
- * 用 md5 校验一致性 —— 不许一边改了另一边没改。
19
- */
20
-
21
- export const PLAN_VERSION = 1
22
- export const MAX_ACTIONS = 24
23
- export const OVERLAP_EPSILON = 0.015 // 秒;比这短的重叠视为可忽略
24
-
25
- const KINDS = ['look', 'nod', 'shake_head', 'expression', 'hold', 'param']
26
- const DIRECTIONS = ['left', 'right', 'up', 'down', 'center']
27
-
28
- export class PlanError extends Error {
29
- constructor(message) {
30
- super(message)
31
- this.name = 'PlanError'
32
- }
33
- }
34
-
35
- /* ─────────────── 数值与曲线 ─────────────── */
36
-
37
- function num(value, name, low, high) {
38
- if (typeof value === 'boolean' || typeof value !== 'number' || !Number.isFinite(value)) {
39
- throw new PlanError(`${name} 必须是有限数字`)
40
- }
41
- if (value < low || value > high) {
42
- throw new PlanError(`${name} 超出范围 ${low}..${high}`)
43
- }
44
- return value
45
- }
46
-
47
- const r5 = (v) => Math.round(v * 1e5) / 1e5
48
- const r6 = (v) => Math.round(v * 1e6) / 1e6
49
-
50
- /** 段间线性插值。点必须按时间升序(编译器保证)。 */
51
- export function sampleTrack(track, t) {
52
- const pts = track.points
53
- if (!pts.length) return 0
54
- if (t <= pts[0][0]) return pts[0][1]
55
- const last = pts[pts.length - 1]
56
- if (t >= last[0]) return last[1]
57
- for (let i = 1; i < pts.length; i++) {
58
- const [t1, v1] = pts[i]
59
- if (t <= t1) {
60
- const [t0, v0] = pts[i - 1]
61
- const span = t1 - t0
62
- if (span <= 0) return v1
63
- const u = (t - t0) / span
64
- return v0 + (v1 - v0) * u
65
- }
66
- }
67
- return last[1]
68
- }
69
-
70
- /* ─────────────── 能力目录 ─────────────── */
71
-
72
- /**
73
- * 校验能力目录的形状。目录由渲染端在模型加载后导出(真实 min/max/default),
74
- * 不能手写 —— 手写的那份一定会和模型的 moc3 漂开。
75
- */
76
- export function checkCaps(caps) {
77
- if (!caps || typeof caps !== 'object') throw new PlanError('能力目录缺失(渲染端没导出)')
78
- const params = caps.params
79
- if (!params || typeof params !== 'object') throw new PlanError('能力目录里没有 params')
80
- const ids = Object.keys(params)
81
- if (!ids.length) throw new PlanError('能力目录里 params 是空的')
82
- for (const id of ids) {
83
- const p = params[id]
84
- if (!p || !Number.isFinite(p.min) || !Number.isFinite(p.max) || !Number.isFinite(p.default)) {
85
- throw new PlanError(`参数 ${id} 的 min/max/default 不完整`)
86
- }
87
- if (p.min > p.max) throw new PlanError(`参数 ${id} 的 min > max`)
88
- }
89
- return { params, physics: new Set(Array.isArray(caps.physics) ? caps.physics : []) }
90
- }
91
-
92
- /* ─────────────── 动作构造器 ─────────────── */
93
-
94
- /** 保形 hold:从中立位缓入到目标值、停住、再缓回中立位。 */
95
- function envelope(defaultValue, value, duration, fade) {
96
- const f = Math.min(fade, duration * 0.28)
97
- return [
98
- [0, defaultValue],
99
- [f, value],
100
- [duration - f, value],
101
- [duration, defaultValue],
102
- ]
103
- }
104
-
105
- /** 波浪:点头/摇头用。首尾都回到中立位,中间按 count 次来回。 */
106
- function wavePoints(defaultValue, amp, duration, count) {
107
- const out = [[0, defaultValue], [0.12 * duration, defaultValue - amp * 0.16]]
108
- for (let n = 0; n < count; n++) {
109
- const st = 0.17 * duration + (n * 0.66 * duration) / count
110
- const w = (0.66 * duration) / count
111
- out.push([st, defaultValue])
112
- out.push([st + w * 0.32, defaultValue + amp * (1 - 0.1 * n)])
113
- out.push([st + w * 0.76, defaultValue - amp * 0.85])
114
- }
115
- out.push([duration, defaultValue])
116
- return out.sort((a, b) => a[0] - b[0])
117
- }
118
-
119
- /* ─────────────── 主函数 ─────────────── */
120
-
121
- /**
122
- * @param {object} plan {version:1, intent?, summary?, actions:[...]}
123
- * @param {object} caps 能力目录(渲染端导出)
124
- * @returns {{ok:true, tracks:Array, warnings:string[], ms:number} | {ok:false, error:string}}
125
- */
126
- export function compilePlan(plan, caps) {
127
- try {
128
- return { ok: true, ...compileInner(plan, caps) }
129
- } catch (e) {
130
- const msg = e instanceof PlanError ? e.message : `编译失败:${(e && e.message) || e}`
131
- return { ok: false, error: msg }
132
- }
133
- }
134
-
135
- function compileInner(plan, caps) {
136
- const { params, physics } = checkCaps(caps)
137
-
138
- if (!plan || typeof plan !== 'object') throw new PlanError('动作计划必须是对象')
139
- if (plan.version !== PLAN_VERSION) throw new PlanError(`动作计划需要 version: ${PLAN_VERSION}`)
140
-
141
- const actions = plan.actions
142
- if (!Array.isArray(actions) || actions.length < 1 || actions.length > MAX_ACTIONS) {
143
- throw new PlanError(`actions 必须包含 1..${MAX_ACTIONS} 个动作`)
144
- }
145
- if (String(plan.intent || '').length > 2000) throw new PlanError('intent 过长(上限 2000 字)')
146
- if (String(plan.summary || '').length > 1000) throw new PlanError('summary 过长(上限 1000 字)')
147
-
148
- const tracks = []
149
- const warnings = []
150
-
151
- const add = (pid, start, duration, pointsIn, index, label, meta = {}) => {
152
- if (!(pid in params)) throw new PlanError(`模型没有参数 ${pid}`)
153
- if (physics.has(pid)) throw new PlanError(`${pid} 归作者物理控制,不能直接驱动`)
154
- const p = params[pid]
155
- const clipped = []
156
- let sawClamp = false
157
- for (const [t, v] of pointsIn) {
158
- if (!Number.isFinite(v)) throw new PlanError('参数曲线包含非有限值')
159
- const c = Math.max(p.min, Math.min(p.max, v))
160
- if (c !== v) sawClamp = true
161
- clipped.push([r5(t), r6(c)])
162
- }
163
- if (sawClamp) warnings.push(`${pid} 已限制到模型范围 ${p.min}..${p.max}`)
164
-
165
- // 通道冲突:同一参数被另一个动作重叠驱动
166
- for (const old of tracks) {
167
- if (old.id !== pid || old.action === index) continue
168
- const overlap =
169
- Math.min(old.start + old.duration, start + duration) - Math.max(old.start, start)
170
- if (overlap <= OVERLAP_EPSILON) continue
171
- // 显式 expression overlay 可以叠在主轨上(表情本来就是叠层)
172
- const overlayOk =
173
- !!meta.expression && meta.layer === 'overlay' && !old.expression && old.owner !== 'gesture'
174
- if (overlayOk) continue
175
- throw new PlanError(
176
- `动作 ${old.action + 1} 与 ${index + 1} 同时控制 ${pid},请错开时间或改用 expression overlay`
177
- )
178
- }
179
-
180
- tracks.push({
181
- id: pid,
182
- start: r5(start),
183
- duration: r5(duration),
184
- points: clipped,
185
- action: index,
186
- label,
187
- owner: meta.expression ? 'expression' : 'semantic',
188
- stage: meta.expression ? 'expression' : 'param',
189
- })
190
- }
191
-
192
- let endMs = 0
193
-
194
- for (let index = 0; index < actions.length; index++) {
195
- const action = actions[index]
196
- if (!action || typeof action !== 'object') throw new PlanError('每个动作必须是对象')
197
-
198
- const kind = action.kind
199
- if (!KINDS.includes(kind)) throw new PlanError(`不支持的动作类型 ${kind}`)
200
- const at = num(action.at === undefined ? 0 : action.at, 'at', 0, 59)
201
- const duration = num(action.duration === undefined ? 2.5 : action.duration, 'duration', 0.2, 20)
202
- const strength = num(action.strength === undefined ? 0.6 : action.strength, 'strength', 0, 1)
203
- const count = action.count === undefined ? 1 : action.count
204
- if (!Number.isInteger(count) || count < 1 || count > 4) {
205
- throw new PlanError('count 必须是 1..4 的整数')
206
- }
207
- endMs = Math.max(endMs, at + duration)
208
-
209
- if (kind === 'look') {
210
- const direction = action.direction || 'center'
211
- if (!DIRECTIONS.includes(direction)) throw new PlanError('不支持的视线方向')
212
- const label = {
213
- left: '看向左侧', right: '看向右侧', up: '抬眼看上方',
214
- down: '低头看下方', center: '看向你',
215
- }[direction]
216
- const x = direction === 'left' ? -1 : direction === 'right' ? 1 : 0
217
- const y = direction === 'up' ? 1 : direction === 'down' ? -1 : 0
218
- const headDelay = Math.min(0.09, duration * 0.15)
219
- const bodyDelay = Math.min(0.19, duration * 0.3)
220
-
221
- // 局部保形:从中立位缓入、停住、缓回。delay 让头/身比眼珠慢半拍,
222
- // 这个错位就是「活人感」的来源(照抄鲸语的 head_delay / body_delay)。
223
- const hp = (pid, value, fade, delay) => {
224
- const d0 = delay || 0
225
- const p0 = params[pid]
226
- if (!p0) throw new PlanError(`模型没有参数 ${pid}(${label} 需要它)`)
227
- const dur0 = duration - d0
228
- add(pid, at + d0, dur0, envelope(p0.default, value, dur0, fade), index, label)
229
- }
230
-
231
- if (x !== 0 || direction === 'center') {
232
- hp('ParamEyeBallX', x * 0.75 * strength, 0.22)
233
- hp('ParamAngleX', x * 28 * strength, 0.28, headDelay)
234
- hp('ParamBodyAngleX', x * 14 * strength, 0.34, bodyDelay)
235
- }
236
- if (y !== 0) {
237
- hp('ParamEyeBallY', y * 0.6 * strength, 0.22)
238
- hp('ParamAngleY', y * 18 * strength, 0.28, headDelay)
239
- }
240
- } else if (kind === 'nod' || kind === 'shake_head') {
241
- const pid = kind === 'nod' ? 'ParamAngleY' : 'ParamAngleX'
242
- if (!(pid in params)) {
243
- throw new PlanError(`模型没有参数 ${pid}(${kind} 需要它)`)
244
- }
245
- if (duration < count * 0.45) {
246
- throw new PlanError('重复点头/摇头的时间太短,请至少每次 0.45 秒')
247
- }
248
- const label = kind === 'nod' ? `点头${count}次` : `轻轻摇头${count}次`
249
- const amp = (kind === 'nod' ? -15 : 20) * strength
250
- const d = params[pid].default
251
- // 物理输出参数不能驱动 —— add 会拦,这里先拦一次给出更清楚的措辞
252
- if (physics.has(pid)) {
253
- throw new PlanError(`${pid} 归作者物理控制,不能拿来做${kind}`)
254
- }
255
- add(pid, at, duration, wavePoints(d, amp, duration, count), index, label)
256
- } else if (kind === 'hold' || kind === 'param') {
257
- const pid = action.param
258
- if (typeof pid !== 'string' || !pid) throw new PlanError(`${kind} 动作需要 param 字段`)
259
- const p = params[pid]
260
- if (!p) throw new PlanError(`模型没有参数 ${pid}`)
261
- // 范围不在这里拦:交给 add 统一裁剪并记 warning。
262
- // 理由(照抄鲸语):曲线在包络里本来就会略微越界,逐点裁剪是常态;
263
- // hold 的 value 若单独从严拒绝,会出现「同样的越界,曲线能过、显式值不能过」的双标。
264
- if (typeof action.value === 'boolean' || typeof action.value !== 'number' || !Number.isFinite(action.value)) {
265
- throw new PlanError('value 必须是有限数字')
266
- }
267
- const value = action.value
268
- const label = action.label || `${pid} = ${value}`
269
- add(pid, at, duration, envelope(p.default, value, duration, 0.42), index, label)
270
- } else if (kind === 'expression') {
271
- const name = action.name
272
- const exps = Array.isArray(caps.expressions) ? caps.expressions : []
273
- if (!name) throw new PlanError('expression 动作需要 name')
274
- if (exps.length && !exps.includes(name)) {
275
- throw new PlanError(`模型没有表情 ${name}(可用:${exps.join(', ')})`)
276
- }
277
- // 表情走独立通道:标记 overlay,不占参数轨
278
- tracks.push({
279
- id: `expression:${name}`,
280
- start: r5(at),
281
- duration: r5(duration),
282
- points: [],
283
- action: index,
284
- label: `表情 ${name}`,
285
- owner: 'expression',
286
- stage: 'expression',
287
- expression: name,
288
- })
289
- }
290
- }
291
-
292
- if (!tracks.length) throw new PlanError('计划编译后没有任何可执行的轨道')
293
- tracks.sort((a, b) => a.start - b.start || a.action - b.action)
294
- return { tracks, warnings, ms: Math.round(endMs * 1000) }
295
- }
1
+ /**
2
+ * 小昔涟 · 语义动作协议 v1 —— 编译器(纯函数,零依赖)
3
+ *
4
+ * 借自 鲸语 Jingyu Live2D Agent 的 compiler.py(MIT):
5
+ * "Semantic action IR -> bounded, eased parameter tracks. Never parses prose."
6
+ * 这条纪律照抄:**这里只吃结构化计划,绝不解析自然语言**。自然语言由模型出成
7
+ * {version:1, actions:[...]},本地只做校验与编译。
8
+ *
9
+ * 三条硬规矩(都是他们踩出来的,2026-11-07 移植):
10
+ * 1. 参数不在能力目录里 → 直接拒绝(不能让模型瞎编参数名)
11
+ * 2. 参数是作者物理的输出 → 直接拒绝(驱动它会和物理打架,画面抽搐)
12
+ * 3. 同一参数被两个动作重叠驱动(>15ms)→ 直接拒绝,除非显式 expression overlay
13
+ *
14
+ * 为什么把「compile」和「execute」拆开:校验失败时**根本不该执行**,
15
+ * 只该把错误交回给模型让它改一次。合起来就只能"先做再发现问题"。
16
+ *
17
+ * 这份文件在两边各存一份同字节副本(插件 lib/ 与独立版 src/),
18
+ * 用 md5 校验一致性 —— 不许一边改了另一边没改。
19
+ */
20
+
21
+ export const PLAN_VERSION = 1
22
+ export const MAX_ACTIONS = 24
23
+ export const OVERLAP_EPSILON = 0.015 // 秒;比这短的重叠视为可忽略
24
+
25
+ const KINDS = ['look', 'nod', 'shake_head', 'expression', 'hold', 'param']
26
+ const DIRECTIONS = ['left', 'right', 'up', 'down', 'center']
27
+
28
+ export class PlanError extends Error {
29
+ constructor(message) {
30
+ super(message)
31
+ this.name = 'PlanError'
32
+ }
33
+ }
34
+
35
+ /* ─────────────── 数值与曲线 ─────────────── */
36
+
37
+ function num(value, name, low, high) {
38
+ if (typeof value === 'boolean' || typeof value !== 'number' || !Number.isFinite(value)) {
39
+ throw new PlanError(`${name} 必须是有限数字`)
40
+ }
41
+ if (value < low || value > high) {
42
+ throw new PlanError(`${name} 超出范围 ${low}..${high}`)
43
+ }
44
+ return value
45
+ }
46
+
47
+ const r5 = (v) => Math.round(v * 1e5) / 1e5
48
+ const r6 = (v) => Math.round(v * 1e6) / 1e6
49
+
50
+ /** 段间线性插值。点必须按时间升序(编译器保证)。 */
51
+ export function sampleTrack(track, t) {
52
+ const pts = track.points
53
+ if (!pts.length) return 0
54
+ if (t <= pts[0][0]) return pts[0][1]
55
+ const last = pts[pts.length - 1]
56
+ if (t >= last[0]) return last[1]
57
+ for (let i = 1; i < pts.length; i++) {
58
+ const [t1, v1] = pts[i]
59
+ if (t <= t1) {
60
+ const [t0, v0] = pts[i - 1]
61
+ const span = t1 - t0
62
+ if (span <= 0) return v1
63
+ const u = (t - t0) / span
64
+ return v0 + (v1 - v0) * u
65
+ }
66
+ }
67
+ return last[1]
68
+ }
69
+
70
+ /* ─────────────── 能力目录 ─────────────── */
71
+
72
+ /**
73
+ * 校验能力目录的形状。目录由渲染端在模型加载后导出(真实 min/max/default),
74
+ * 不能手写 —— 手写的那份一定会和模型的 moc3 漂开。
75
+ */
76
+ export function checkCaps(caps) {
77
+ if (!caps || typeof caps !== 'object') throw new PlanError('能力目录缺失(渲染端没导出)')
78
+ const params = caps.params
79
+ if (!params || typeof params !== 'object') throw new PlanError('能力目录里没有 params')
80
+ const ids = Object.keys(params)
81
+ if (!ids.length) throw new PlanError('能力目录里 params 是空的')
82
+ for (const id of ids) {
83
+ const p = params[id]
84
+ if (!p || !Number.isFinite(p.min) || !Number.isFinite(p.max) || !Number.isFinite(p.default)) {
85
+ throw new PlanError(`参数 ${id} 的 min/max/default 不完整`)
86
+ }
87
+ if (p.min > p.max) throw new PlanError(`参数 ${id} 的 min > max`)
88
+ }
89
+ return { params, physics: new Set(Array.isArray(caps.physics) ? caps.physics : []) }
90
+ }
91
+
92
+ /* ─────────────── 动作构造器 ─────────────── */
93
+
94
+ /** 保形 hold:从中立位缓入到目标值、停住、再缓回中立位。 */
95
+ function envelope(defaultValue, value, duration, fade) {
96
+ const f = Math.min(fade, duration * 0.28)
97
+ return [
98
+ [0, defaultValue],
99
+ [f, value],
100
+ [duration - f, value],
101
+ [duration, defaultValue],
102
+ ]
103
+ }
104
+
105
+ /** 波浪:点头/摇头用。首尾都回到中立位,中间按 count 次来回。 */
106
+ function wavePoints(defaultValue, amp, duration, count) {
107
+ const out = [[0, defaultValue], [0.12 * duration, defaultValue - amp * 0.16]]
108
+ for (let n = 0; n < count; n++) {
109
+ const st = 0.17 * duration + (n * 0.66 * duration) / count
110
+ const w = (0.66 * duration) / count
111
+ out.push([st, defaultValue])
112
+ out.push([st + w * 0.32, defaultValue + amp * (1 - 0.1 * n)])
113
+ out.push([st + w * 0.76, defaultValue - amp * 0.85])
114
+ }
115
+ out.push([duration, defaultValue])
116
+ return out.sort((a, b) => a[0] - b[0])
117
+ }
118
+
119
+ /* ─────────────── 主函数 ─────────────── */
120
+
121
+ /**
122
+ * @param {object} plan {version:1, intent?, summary?, actions:[...]}
123
+ * @param {object} caps 能力目录(渲染端导出)
124
+ * @returns {{ok:true, tracks:Array, warnings:string[], ms:number} | {ok:false, error:string}}
125
+ */
126
+ export function compilePlan(plan, caps) {
127
+ try {
128
+ return { ok: true, ...compileInner(plan, caps) }
129
+ } catch (e) {
130
+ const msg = e instanceof PlanError ? e.message : `编译失败:${(e && e.message) || e}`
131
+ return { ok: false, error: msg }
132
+ }
133
+ }
134
+
135
+ function compileInner(plan, caps) {
136
+ const { params, physics } = checkCaps(caps)
137
+
138
+ if (!plan || typeof plan !== 'object') throw new PlanError('动作计划必须是对象')
139
+ if (plan.version !== PLAN_VERSION) throw new PlanError(`动作计划需要 version: ${PLAN_VERSION}`)
140
+
141
+ const actions = plan.actions
142
+ if (!Array.isArray(actions) || actions.length < 1 || actions.length > MAX_ACTIONS) {
143
+ throw new PlanError(`actions 必须包含 1..${MAX_ACTIONS} 个动作`)
144
+ }
145
+ if (String(plan.intent || '').length > 2000) throw new PlanError('intent 过长(上限 2000 字)')
146
+ if (String(plan.summary || '').length > 1000) throw new PlanError('summary 过长(上限 1000 字)')
147
+
148
+ const tracks = []
149
+ const warnings = []
150
+
151
+ const add = (pid, start, duration, pointsIn, index, label, meta = {}) => {
152
+ if (!(pid in params)) throw new PlanError(`模型没有参数 ${pid}`)
153
+ if (physics.has(pid)) throw new PlanError(`${pid} 归作者物理控制,不能直接驱动`)
154
+ const p = params[pid]
155
+ const clipped = []
156
+ let sawClamp = false
157
+ for (const [t, v] of pointsIn) {
158
+ if (!Number.isFinite(v)) throw new PlanError('参数曲线包含非有限值')
159
+ const c = Math.max(p.min, Math.min(p.max, v))
160
+ if (c !== v) sawClamp = true
161
+ clipped.push([r5(t), r6(c)])
162
+ }
163
+ if (sawClamp) warnings.push(`${pid} 已限制到模型范围 ${p.min}..${p.max}`)
164
+
165
+ // 通道冲突:同一参数被另一个动作重叠驱动
166
+ for (const old of tracks) {
167
+ if (old.id !== pid || old.action === index) continue
168
+ const overlap =
169
+ Math.min(old.start + old.duration, start + duration) - Math.max(old.start, start)
170
+ if (overlap <= OVERLAP_EPSILON) continue
171
+ // 显式 expression overlay 可以叠在主轨上(表情本来就是叠层)
172
+ const overlayOk =
173
+ !!meta.expression && meta.layer === 'overlay' && !old.expression && old.owner !== 'gesture'
174
+ if (overlayOk) continue
175
+ throw new PlanError(
176
+ `动作 ${old.action + 1} 与 ${index + 1} 同时控制 ${pid},请错开时间或改用 expression overlay`
177
+ )
178
+ }
179
+
180
+ tracks.push({
181
+ id: pid,
182
+ start: r5(start),
183
+ duration: r5(duration),
184
+ points: clipped,
185
+ action: index,
186
+ label,
187
+ owner: meta.expression ? 'expression' : 'semantic',
188
+ stage: meta.expression ? 'expression' : 'param',
189
+ })
190
+ }
191
+
192
+ let endMs = 0
193
+
194
+ for (let index = 0; index < actions.length; index++) {
195
+ const action = actions[index]
196
+ if (!action || typeof action !== 'object') throw new PlanError('每个动作必须是对象')
197
+
198
+ const kind = action.kind
199
+ if (!KINDS.includes(kind)) throw new PlanError(`不支持的动作类型 ${kind}`)
200
+ const at = num(action.at === undefined ? 0 : action.at, 'at', 0, 59)
201
+ const duration = num(action.duration === undefined ? 2.5 : action.duration, 'duration', 0.2, 20)
202
+ const strength = num(action.strength === undefined ? 0.6 : action.strength, 'strength', 0, 1)
203
+ const count = action.count === undefined ? 1 : action.count
204
+ if (!Number.isInteger(count) || count < 1 || count > 4) {
205
+ throw new PlanError('count 必须是 1..4 的整数')
206
+ }
207
+ endMs = Math.max(endMs, at + duration)
208
+
209
+ if (kind === 'look') {
210
+ const direction = action.direction || 'center'
211
+ if (!DIRECTIONS.includes(direction)) throw new PlanError('不支持的视线方向')
212
+ const label = {
213
+ left: '看向左侧', right: '看向右侧', up: '抬眼看上方',
214
+ down: '低头看下方', center: '看向你',
215
+ }[direction]
216
+ const x = direction === 'left' ? -1 : direction === 'right' ? 1 : 0
217
+ const y = direction === 'up' ? 1 : direction === 'down' ? -1 : 0
218
+ const headDelay = Math.min(0.09, duration * 0.15)
219
+ const bodyDelay = Math.min(0.19, duration * 0.3)
220
+
221
+ // 局部保形:从中立位缓入、停住、缓回。delay 让头/身比眼珠慢半拍,
222
+ // 这个错位就是「活人感」的来源(照抄鲸语的 head_delay / body_delay)。
223
+ const hp = (pid, value, fade, delay) => {
224
+ const d0 = delay || 0
225
+ const p0 = params[pid]
226
+ if (!p0) throw new PlanError(`模型没有参数 ${pid}(${label} 需要它)`)
227
+ const dur0 = duration - d0
228
+ add(pid, at + d0, dur0, envelope(p0.default, value, dur0, fade), index, label)
229
+ }
230
+
231
+ if (x !== 0 || direction === 'center') {
232
+ hp('ParamEyeBallX', x * 0.75 * strength, 0.22)
233
+ hp('ParamAngleX', x * 28 * strength, 0.28, headDelay)
234
+ hp('ParamBodyAngleX', x * 14 * strength, 0.34, bodyDelay)
235
+ }
236
+ if (y !== 0) {
237
+ hp('ParamEyeBallY', y * 0.6 * strength, 0.22)
238
+ hp('ParamAngleY', y * 18 * strength, 0.28, headDelay)
239
+ }
240
+ } else if (kind === 'nod' || kind === 'shake_head') {
241
+ const pid = kind === 'nod' ? 'ParamAngleY' : 'ParamAngleX'
242
+ if (!(pid in params)) {
243
+ throw new PlanError(`模型没有参数 ${pid}(${kind} 需要它)`)
244
+ }
245
+ if (duration < count * 0.45) {
246
+ throw new PlanError('重复点头/摇头的时间太短,请至少每次 0.45 秒')
247
+ }
248
+ const label = kind === 'nod' ? `点头${count}次` : `轻轻摇头${count}次`
249
+ const amp = (kind === 'nod' ? -15 : 20) * strength
250
+ const d = params[pid].default
251
+ // 物理输出参数不能驱动 —— add 会拦,这里先拦一次给出更清楚的措辞
252
+ if (physics.has(pid)) {
253
+ throw new PlanError(`${pid} 归作者物理控制,不能拿来做${kind}`)
254
+ }
255
+ add(pid, at, duration, wavePoints(d, amp, duration, count), index, label)
256
+ } else if (kind === 'hold' || kind === 'param') {
257
+ const pid = action.param
258
+ if (typeof pid !== 'string' || !pid) throw new PlanError(`${kind} 动作需要 param 字段`)
259
+ const p = params[pid]
260
+ if (!p) throw new PlanError(`模型没有参数 ${pid}`)
261
+ // 范围不在这里拦:交给 add 统一裁剪并记 warning。
262
+ // 理由(照抄鲸语):曲线在包络里本来就会略微越界,逐点裁剪是常态;
263
+ // hold 的 value 若单独从严拒绝,会出现「同样的越界,曲线能过、显式值不能过」的双标。
264
+ if (typeof action.value === 'boolean' || typeof action.value !== 'number' || !Number.isFinite(action.value)) {
265
+ throw new PlanError('value 必须是有限数字')
266
+ }
267
+ const value = action.value
268
+ const label = action.label || `${pid} = ${value}`
269
+ add(pid, at, duration, envelope(p.default, value, duration, 0.42), index, label)
270
+ } else if (kind === 'expression') {
271
+ const name = action.name
272
+ const exps = Array.isArray(caps.expressions) ? caps.expressions : []
273
+ if (!name) throw new PlanError('expression 动作需要 name')
274
+ if (exps.length && !exps.includes(name)) {
275
+ throw new PlanError(`模型没有表情 ${name}(可用:${exps.join(', ')})`)
276
+ }
277
+ // 表情走独立通道:标记 overlay,不占参数轨
278
+ tracks.push({
279
+ id: `expression:${name}`,
280
+ start: r5(at),
281
+ duration: r5(duration),
282
+ points: [],
283
+ action: index,
284
+ label: `表情 ${name}`,
285
+ owner: 'expression',
286
+ stage: 'expression',
287
+ expression: name,
288
+ })
289
+ }
290
+ }
291
+
292
+ if (!tracks.length) throw new PlanError('计划编译后没有任何可执行的轨道')
293
+ tracks.sort((a, b) => a.start - b.start || a.action - b.action)
294
+ return { tracks, warnings, ms: Math.round(endMs * 1000) }
295
+ }