dsh-wechat-access 0.0.0-stage → 0.0.2
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/CHANGELOG.md +56 -0
- package/LICENSE +675 -0
- package/README.md +278 -2
- package/cordis.patch.yml +5 -0
- package/examples/config.example.json +98 -0
- package/index.mjs +123 -0
- package/lib/access.mjs +131 -0
- package/lib/anysearch.mjs +143 -0
- package/lib/capabilities.mjs +312 -0
- package/lib/client.js +2532 -0
- package/lib/config.mjs +509 -0
- package/lib/emotion-store.mjs +352 -0
- package/lib/emotion.mjs +428 -0
- package/lib/endocrine/LICENSE +21 -0
- package/lib/endocrine/README.md +39 -0
- package/lib/endocrine/diseases.js +791 -0
- package/lib/endocrine/engine.mjs +131 -0
- package/lib/endocrine/store.mjs +253 -0
- package/lib/endocrine/upstream.mjs +1230 -0
- package/lib/group-gate.mjs +305 -0
- package/lib/imagegen.mjs +360 -0
- package/lib/lock.mjs +204 -0
- package/lib/meme-classify.mjs +190 -0
- package/lib/memes.mjs +442 -0
- package/lib/memory.mjs +437 -0
- package/lib/normalize.mjs +230 -0
- package/lib/parser.mjs +602 -0
- package/lib/reply-settled.mjs +130 -0
- package/lib/runtime.mjs +3367 -0
- package/lib/startup.mjs +70 -0
- package/lib/steam-binding.mjs +144 -0
- package/lib/steam.mjs +334 -0
- package/lib/store.mjs +259 -0
- package/lib/thoughts.mjs +149 -0
- package/lib/time-context.mjs +182 -0
- package/lib/tools.mjs +583 -0
- package/lib/ui.mjs +997 -0
- package/lib/util.mjs +489 -0
- package/lib/virtual-life-generate.mjs +421 -0
- package/lib/virtual-life.mjs +971 -0
- package/lib/weather.mjs +172 -0
- package/lib/wechat-client.mjs +476 -0
- package/lib/wechat-events.mjs +269 -0
- package/locale/zh.json +6 -0
- package/package.json +77 -4
|
@@ -0,0 +1,1230 @@
|
|
|
1
|
+
// dsh-fatfish-endocrine — host half.
|
|
2
|
+
// 女性肥鱼(默认形态):14 个腺体激素(含卵巢雌二醇/孕酮)的昼夜节律 + 28 天月经周期 + 事件脉冲 + 17 个内置疾病(core/secondary 档)。
|
|
3
|
+
// 默认不注入提示词(C 档默认关)。
|
|
4
|
+
// 本插件只改语气/风格,不参与任何拒绝、权限、不可逆动作的判定。
|
|
5
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
6
|
+
import { homedir } from 'node:os'
|
|
7
|
+
import { dirname, join } from 'node:path'
|
|
8
|
+
|
|
9
|
+
import { DISEASES } from './diseases.js'
|
|
10
|
+
|
|
11
|
+
export const name = 'dsh-fatfish-endocrine'
|
|
12
|
+
export const inject = ['tools']
|
|
13
|
+
|
|
14
|
+
export const ROUTE = '/fatfish-endocrine'
|
|
15
|
+
export const SETTINGS_PATH = join(homedir(), '.dsh', 'fatfish-endocrine.json')
|
|
16
|
+
const PROFILE_DIR = process.env.DSH_PROFILE_DIR || join(homedir(), '.dsh', 'profiles', 'desktop')
|
|
17
|
+
|
|
18
|
+
// 旧的「疾病整合包」——检测到就由本插件优先接管两边重合的病。
|
|
19
|
+
export const LEGACY_PACKAGE = 'dsh-sick-fatfish'
|
|
20
|
+
export const LEGACY_TOOLS = ['sick_on', 'sick_off', 'sick_status', 'sick_roll', 'sick_set_level', 'sick_care', 'sick_relief', 'sick_cure', 'sick_worse']
|
|
21
|
+
// sick-fatfish 的疾病 id → 本插件内置 slug;只列两边都有的 10 个重合病。
|
|
22
|
+
export const LEGACY_MAP = {
|
|
23
|
+
'dsh-carpal-tunnel': 'dsh-carpal-tunnel',
|
|
24
|
+
'dsh-dry-eye': 'dsh-dry-eye',
|
|
25
|
+
'dsh-eczema': 'dsh-eczema',
|
|
26
|
+
'dsh-gout': 'dsh-gout',
|
|
27
|
+
'dsh-ibs': 'dsh-ibs',
|
|
28
|
+
'dsh-insomnia': 'dsh-insomnia',
|
|
29
|
+
'dsh-kidney-stone': 'dsh-kidney-stone',
|
|
30
|
+
'dsh-migraine': 'dsh-migraine',
|
|
31
|
+
'dsh-orthostatic': 'dsh-orthostatic',
|
|
32
|
+
'dsh-tinnitus': 'dsh-tinnitus',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 14 个腺体,每个一个代表激素;卵巢的雌二醇/孕酮随月经周期变化。
|
|
36
|
+
// baseline/amp/phase 是昼夜节律参数;tau 是脉冲衰减时间常数(秒)。
|
|
37
|
+
// intro 是面板上的科普说明(点开才看,不进提示词)。
|
|
38
|
+
export const HORMONES = [
|
|
39
|
+
{ id: 'crh', name: 'CRH', en: 'CRH', organ: '下丘脑', unit: '', baseline: 30, amp: 10, phase: 7, tau: 180, color: '#7aa2ff',
|
|
40
|
+
intro: '下丘脑分泌的"总调度":应激时第一个升高,命令垂体前叶放 ACTH。长期偏高会让人焦虑、睡浅;偏低则应激反应迟钝。' },
|
|
41
|
+
{ id: 'acth', name: 'ACTH', en: 'ACTH', organ: '垂体前叶', unit: '', baseline: 28, amp: 8, phase: 8, tau: 180, color: '#6f93f2',
|
|
42
|
+
intro: '垂体前叶分泌,专门催肾上腺皮质产皮质醇。它高通常意味着皮质醇也高;它低则皮质醇可能不足。' },
|
|
43
|
+
{ id: 'adh', name: 'ADH', en: 'ADH', organ: '垂体后叶', unit: '', baseline: 45, amp: 6, phase: 2, tau: 240, color: '#5ad1c8',
|
|
44
|
+
intro: '垂体后叶释放(下丘脑合成),让肾脏把水收回来。偏高→尿少、血钠被稀释;偏低→尿多,甚至尿崩。' },
|
|
45
|
+
{ id: 't4', name: 'T4', en: 'T4', organ: '甲状腺', unit: '', baseline: 50, amp: 6, phase: 10, tau: 360, color: '#f0b35a',
|
|
46
|
+
intro: '甲状腺分泌的主激素,管全身代谢速度。偏高→心悸、怕热、消瘦(甲亢);偏低→怕冷、乏力、反应慢(甲减)。' },
|
|
47
|
+
{ id: 'pth', name: 'PTH', en: 'PTH', organ: '甲状旁腺', unit: '', baseline: 40, amp: 4, phase: 3, tau: 300, color: '#c9a3ff',
|
|
48
|
+
intro: '甲状旁腺分泌,把骨里的钙调进血里。偏高→骨钙流失、肾结石;偏低→血钙低、手脚发麻抽搐。' },
|
|
49
|
+
{ id: 'cortisol', name: '皮质醇', en: 'Cortisol', organ: '肾上腺皮质', unit: '', baseline: 35, amp: 22, phase: 8, tau: 120, color: '#ff8f6b',
|
|
50
|
+
intro: '肾上腺皮质分泌的"应激激素",早晨最高、夜里最低。升血糖、压炎症;长期偏高会向心性肥胖、血糖高、易感染,偏低则乏力、低血压。' },
|
|
51
|
+
{ id: 'epinephrine', name: '肾上腺素', en: 'Epinephrine', organ: '肾上腺髓质', unit: '', baseline: 18, amp: 4, phase: 12, tau: 20, color: '#ff5c7a',
|
|
52
|
+
intro: '肾上腺髓质分泌,几秒内拉高心率、血压、血糖——战斗或逃跑。短时高是心慌手抖;长期高心血管吃不消。' },
|
|
53
|
+
{ id: 'insulin', name: '胰岛素', en: 'Insulin', organ: '胰岛', unit: '', baseline: 38, amp: 12, phase: 13, tau: 60, color: '#6ee7a8',
|
|
54
|
+
intro: '胰岛 β 细胞分泌,唯一降血糖的激素,同时促合成、存脂肪。偏高→低血糖;偏低→血糖高(糖尿病)。' },
|
|
55
|
+
{ id: 'testosterone', name: '睾酮', en: 'Testosterone', organ: '性腺', unit: '', baseline: 55, amp: 8, phase: 6, tau: 600, color: '#ffd166',
|
|
56
|
+
intro: '性腺分泌(女性卵巢与肾上腺也产少量),管性欲、肌肉骨骼与情绪驱动。偏高→痤疮、易怒;偏低→乏力、性欲低。' },
|
|
57
|
+
{ id: 'melatonin', name: '褪黑素', en: 'Melatonin', organ: '松果体', unit: '', baseline: 30, amp: 28, phase: 2, tau: 90, color: '#8b9bff',
|
|
58
|
+
intro: '松果体分泌,天黑后升高,告诉身体"该睡了"。偏高→困倦;偏低→入睡难、昼夜节律乱。' },
|
|
59
|
+
{ id: 'thymosin', name: '胸腺素', en: 'Thymosin', organ: '胸腺', unit: '', baseline: 45, amp: 3, phase: 11, tau: 720, color: '#c4d36e',
|
|
60
|
+
intro: '胸腺分泌,帮 T 细胞成熟。青春期后胸腺萎缩、它随之下降,所以老年人免疫应答变慢。' },
|
|
61
|
+
{ id: 'leptin', name: '瘦素', en: 'Leptin', organ: '脂肪/胃肠道', unit: '', baseline: 50, amp: 10, phase: 21, tau: 180, color: '#f29ec4',
|
|
62
|
+
intro: '脂肪组织分泌的"饱腹信号",告诉大脑能量够。偏高常见于瘦素抵抗(吃得多却不觉得饱);偏低→饥饿感强。' },
|
|
63
|
+
// 卵巢两个随月经周期起伏的激素;baseline 是周期缺失时的兜底值,实际值走 cycleValue()。
|
|
64
|
+
{ id: 'estradiol', name: '雌二醇', en: 'Estradiol', organ: '卵巢', unit: '', baseline: 45, amp: 0, phase: 12, tau: 600, color: '#ff9ecb', cyclic: true,
|
|
65
|
+
intro: '卵巢卵泡分泌,女性第二性征、子宫内膜增生、骨密度都靠它,随月经周期起伏。偏高→乳胀、情绪波动;偏低→潮热、骨量下降。' },
|
|
66
|
+
{ id: 'progesterone', name: '孕酮', en: 'Progesterone', organ: '卵巢', unit: '', baseline: 35, amp: 0, phase: 22, tau: 600, color: '#c58cff', cyclic: true,
|
|
67
|
+
intro: '卵巢黄体分泌,排卵后升高,维持子宫内膜、平稳情绪、略升体温。偏高→嗜睡、便秘;偏低→经前烦躁、黄体不足。' },
|
|
68
|
+
// 多巴胺不是内分泌腺激素,但它决定「显著性/动机」,精神分裂样状态绕不开它,所以单列一条。
|
|
69
|
+
{ id: 'dopamine', name: '多巴胺', en: 'Dopamine', organ: '中脑·纹状体', unit: '', baseline: 50, amp: 8, phase: 14, tau: 300, color: '#b48cff',
|
|
70
|
+
intro: '中脑 VTA 与黑质投射到纹状体与前额叶,管动机、奖赏与「什么值得注意」。过高→显著性放大、联想跳跃(精神病样状态的核心机制之一);过低→动力缺失、快感缺失。' },
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
// 月经周期:28 天,四段。E2/P4 由下面的关键点曲线给出,其余激素受周期轻微调制。
|
|
74
|
+
export const CYCLE_PHASES = [
|
|
75
|
+
{ id: 'menstrual', label: '月经期', short: '经期' },
|
|
76
|
+
{ id: 'follicular', label: '卵泡期', short: '卵泡' },
|
|
77
|
+
{ id: 'ovulation', label: '排卵期', short: '排卵' },
|
|
78
|
+
{ id: 'luteal', label: '黄体期', short: '黄体' },
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
const PHASE_LABEL = Object.fromEntries(CYCLE_PHASES.map((p) => [p.id, p.label]))
|
|
82
|
+
|
|
83
|
+
// 关键点(周期第几天 → 值),线性插值。
|
|
84
|
+
const CYCLE_CURVES = {
|
|
85
|
+
estradiol: [[1, 20], [5, 25], [9, 55], [12, 80], [14, 90], [17, 60], [21, 70], [25, 40], [28, 20]],
|
|
86
|
+
progesterone: [[1, 8], [5, 10], [10, 10], [14, 15], [17, 30], [21, 75], [24, 60], [27, 25], [28, 15]],
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function cycleValue(id, day, length = 28) {
|
|
90
|
+
const pts = CYCLE_CURVES[id]
|
|
91
|
+
if (!pts) return null
|
|
92
|
+
const len = Number(length) > 1 ? Number(length) : 28
|
|
93
|
+
const d = clamp(Number(day) || 1, 1, len)
|
|
94
|
+
// 非 28 天时按比例映射到 28 天曲线。
|
|
95
|
+
const dd = len === 28 ? d : 1 + ((d - 1) / (len - 1)) * 27
|
|
96
|
+
for (let i = 1; i < pts.length; i += 1) {
|
|
97
|
+
if (dd <= pts[i][0]) {
|
|
98
|
+
const [x0, y0] = pts[i - 1]
|
|
99
|
+
const [x1, y1] = pts[i]
|
|
100
|
+
const t = (dd - x0) / (x1 - x0 || 1)
|
|
101
|
+
return Math.round((y0 + (y1 - y0) * t) * 10) / 10
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return pts[pts.length - 1][1]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function cyclePhase(day, length = 28) {
|
|
108
|
+
const len = Number(length) > 1 ? Number(length) : 28
|
|
109
|
+
const d = clamp(Math.round(Number(day) || 1), 1, len)
|
|
110
|
+
const ov = Math.max(2, Math.round(len / 2))
|
|
111
|
+
if (d <= 5) return 'menstrual'
|
|
112
|
+
if (d < ov) return 'follicular'
|
|
113
|
+
if (d === ov) return 'ovulation'
|
|
114
|
+
return 'luteal'
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function phaseLabel(id) {
|
|
118
|
+
return PHASE_LABEL[id] || id
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const SCENARIOS = {
|
|
122
|
+
stayUp: { id: 'stayUp', label: '熬夜', push: { melatonin: -25, cortisol: 25, leptin: -10 } },
|
|
123
|
+
scare: { id: 'scare', label: '惊吓', push: { epinephrine: 45, cortisol: 15, insulin: -10 } },
|
|
124
|
+
full: { id: 'full', label: '饱食', push: { insulin: 30, leptin: 20 } },
|
|
125
|
+
calm: { id: 'calm', label: '平静', push: {}, reset: true },
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 市面常见药物 → 对激素的一次性拉扯(行为刻度,不是剂量/用药建议)。
|
|
129
|
+
// relief 可选:对某个内置病有缓解作用(急救式打激素没有这个能力)。
|
|
130
|
+
// intro 是面板上的科普说明(点开才看,不进提示词)。
|
|
131
|
+
export const DRUGS = [
|
|
132
|
+
{ id: 'coffee', name: '咖啡(咖啡因)', note: '一杯美式', push: { cortisol: 12, melatonin: -10, epinephrine: 8 },
|
|
133
|
+
intro: '咖啡因阻断腺苷受体——腺苷是大脑积累的"困意信号",被挡住就暂时不困。半衰期约 5 小时,下午喝容易夜里睡不着;过量会心悸、手抖。' },
|
|
134
|
+
{ id: 'tea', name: '浓茶(茶碱)', note: '一杯浓绿茶', push: { cortisol: 8, epinephrine: 5 },
|
|
135
|
+
intro: '咖啡因加茶碱,提神比咖啡温和,也有轻度利尿作用。空腹喝浓茶会刺激胃、心慌。' },
|
|
136
|
+
{ id: 'melatonin', name: '褪黑素片', note: '3mg,助眠', push: { melatonin: 30 }, relief: { slug: 'dsh-insomnia', amount: 15 },
|
|
137
|
+
intro: '外源褪黑素,用来调时差或帮助入睡,常用 0.5–3mg、睡前 30 分钟。它只是把"天黑了"的信号补上,不治失眠的根因(焦虑、呼吸暂停等)。' },
|
|
138
|
+
{ id: 'ibuprofen', name: '布洛芬', note: '一粒,消炎止痛', push: { cortisol: -10 }, relief: { slug: 'dsh-migraine', amount: 12 },
|
|
139
|
+
intro: '非甾体抗炎药,抑制前列腺素合成,止痛、退热、抗炎。饭后吃减少胃刺激;胃溃疡或肾功能差的人慎用。' },
|
|
140
|
+
{ id: 'levothyroxine', name: '优甲乐(左甲状腺素)', note: '一片,补 T4', push: { t4: 25 },
|
|
141
|
+
intro: '左甲状腺素钠,甲减的替代治疗:把身体缺的 T4 补上。晨起空腹吃,与钙片、铁剂间隔 4 小时,否则吸收被抢。' },
|
|
142
|
+
{ id: 'metformin', name: '二甲双胍', note: '一片,降糖', push: { insulin: -12 },
|
|
143
|
+
intro: '2 型糖尿病一线药:减少肝脏输出糖、提高胰岛素敏感性。常见胃肠反应,随餐吃可减轻。' },
|
|
144
|
+
{ id: 'glucose', name: '葡萄糖片', note: '低血糖时含一片', push: { insulin: 18, leptin: 8 },
|
|
145
|
+
intro: '低血糖急救用,含服后约 15 分钟复测血糖。它只救急,不替代调整降糖方案。' },
|
|
146
|
+
{ id: 'ors', name: '口服补液盐', note: '一袋冲水', push: { adh: 15 }, relief: { slug: 'dsh-orthostatic', amount: 10 },
|
|
147
|
+
intro: '水加电解质,用于腹泻脱水或体位性低血压。按说明整袋冲水,不要自己加糖或盐。' },
|
|
148
|
+
{ id: 'propranolol', name: '普萘洛尔', note: '一片,压心率', push: { epinephrine: -15, cortisol: -6 }, relief: { slug: 'dsh-hyperthyroid', amount: 8 },
|
|
149
|
+
intro: 'β 受体阻滞剂,压心率、减轻甲亢的心悸手抖,也用于焦虑。哮喘、心动过缓的人慎用。' },
|
|
150
|
+
{ id: 'pill', name: '短效避孕药', note: '按周期吃', push: { estradiol: 15, progesterone: 12 },
|
|
151
|
+
intro: '短效复方避孕药:雌激素加孕酮,抑制排卵、让周期规律。要按天规律吃,漏服会掉效果。' },
|
|
152
|
+
{ id: 'prednisone', name: '泼尼松', note: '激素药,按医生说的', push: { cortisol: 25 }, relief: { slug: 'dsh-urticaria', amount: 12 },
|
|
153
|
+
intro: '糖皮质激素,抗炎、抗过敏、免疫抑制。长期用不能突然停,要按医嘱逐步减量。' },
|
|
154
|
+
{ id: 'beer', name: '啤酒', note: '一听', push: { adh: -12, cortisol: 6 },
|
|
155
|
+
intro: '酒精抑制 ADH,所以喝完尿多;少量让人放松,但过量伤肝、破坏深睡眠,也会让夜里更容易醒。' },
|
|
156
|
+
]
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
export function clamp(v, lo, hi) {
|
|
160
|
+
return Math.min(hi, Math.max(lo, v))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// 给模型看的档位:只回答偏高/偏低/正常,不回答具体数值。
|
|
164
|
+
export function levelOf(v) {
|
|
165
|
+
const n = Number(v) || 0
|
|
166
|
+
return n >= 65 ? '偏高' : n <= 35 ? '偏低' : '正常'
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function hourOf(now = Date.now()) {
|
|
170
|
+
const d = new Date(now)
|
|
171
|
+
return d.getHours() + d.getMinutes() / 60 + d.getSeconds() / 3600
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function diurnal(hour, phase) {
|
|
175
|
+
return Math.cos((2 * Math.PI * (hour - phase)) / 24)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 精神状态 → 激素偏移。全部是模拟参数,label 一律带「示意」,不当诊断用。
|
|
179
|
+
// 前三条是常见档(由 fatfish-mood 判定);后四条是稀有档(由 fatfish-psych 极低概率触发)。
|
|
180
|
+
export const MENTAL_STATES = {
|
|
181
|
+
none: { label: '', effects: {} },
|
|
182
|
+
depressive: { label: '抑郁样(示意)', effects: { cortisol: 8, t4: -3, testosterone: -3, leptin: 3 } },
|
|
183
|
+
anxiety: { label: '焦虑样(示意)', effects: { crh: 5, acth: 4, epinephrine: 5 } },
|
|
184
|
+
psychosis: { label: '精神病样(示意)', effects: { dopamine: 12, cortisol: 4 } },
|
|
185
|
+
manic: { label: '躁狂样(示意)', effects: { dopamine: 10, epinephrine: 6, cortisol: -4 } },
|
|
186
|
+
trauma: { label: '创伤后样(示意)', effects: { crh: 6, cortisol: -6, epinephrine: 5 } },
|
|
187
|
+
ocd: { label: '强迫样(示意)', effects: { cortisol: 4, crh: 3 } },
|
|
188
|
+
dissociative: { label: '解离样(示意)', effects: { dopamine: -4, cortisol: 2 } },
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export const RARE_STATE_IDS = ['manic', 'trauma', 'ocd', 'dissociative']
|
|
192
|
+
|
|
193
|
+
export function valueOf(hormone, hour, pulse = 0, diseaseAdj = 0, cycleDay = null, moodAdj = 0, loadAdj = 0, stateAdj = 0, ampScale = 1) {
|
|
194
|
+
const extra = moodAdj + loadAdj + stateAdj
|
|
195
|
+
if (hormone && hormone.cyclic && Number.isFinite(cycleDay)) {
|
|
196
|
+
const cv = cycleValue(hormone.id, cycleDay)
|
|
197
|
+
if (cv != null) return clamp(cv + pulse * 0.3 + diseaseAdj + extra, 0, 100)
|
|
198
|
+
}
|
|
199
|
+
const amp = hormone.amp * clamp(ampScale, 0.4, 1.4)
|
|
200
|
+
return clamp(hormone.baseline + amp * diurnal(hour, hormone.phase) + pulse + diseaseAdj + extra, 0, 100)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function diseaseTier(severity) {
|
|
204
|
+
const s = Number(severity) || 0
|
|
205
|
+
if (s >= 70) return 'flare'
|
|
206
|
+
if (s >= 35) return 'ache'
|
|
207
|
+
return 'stiff'
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function createEngine(options = {}) {
|
|
211
|
+
const now0 = options.now == null ? Date.now() : Number(options.now)
|
|
212
|
+
const random = typeof options.random === 'function' ? options.random : Math.random
|
|
213
|
+
const autoFlare = options.autoFlare !== false
|
|
214
|
+
const pulse = Object.fromEntries(HORMONES.map((h) => [h.id, 0]))
|
|
215
|
+
const diseaseAdj = Object.fromEntries(HORMONES.map((h) => [h.id, 0]))
|
|
216
|
+
const disease = Object.fromEntries(DISEASES.map((d) => [d.slug, {
|
|
217
|
+
severity: 0,
|
|
218
|
+
active: false,
|
|
219
|
+
curing: false,
|
|
220
|
+
flares: 0,
|
|
221
|
+
reliefUntil: 0,
|
|
222
|
+
reliefKind: '',
|
|
223
|
+
startedAt: 0,
|
|
224
|
+
}]))
|
|
225
|
+
let last = now0
|
|
226
|
+
const cycle = {
|
|
227
|
+
length: clamp(Math.round(Number(options.cycleLength) || 28), 21, 45),
|
|
228
|
+
day: 1,
|
|
229
|
+
startedAt: now0,
|
|
230
|
+
adj: Object.fromEntries(HORMONES.map((h) => [h.id, 0])),
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const mood = {
|
|
234
|
+
id: 'calm',
|
|
235
|
+
label: '平静',
|
|
236
|
+
intensity: 0,
|
|
237
|
+
valence: 0.6,
|
|
238
|
+
arousal: 0.2,
|
|
239
|
+
effects: [], // [{hormone, delta, tauSec}],由 fatfish-mood 插件通过 service 传入
|
|
240
|
+
adj: Object.fromEntries(HORMONES.map((h) => [h.id, 0])),
|
|
241
|
+
since: now0,
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 精神状态层:由 fatfish-mood 插件推送 load(累积负荷)/ protect(保护)/ sensitization(敏化)/ state(状态)。
|
|
245
|
+
// 这里只把它折算成激素偏移,不做判定、不做诊断。
|
|
246
|
+
const mental = {
|
|
247
|
+
load: 0,
|
|
248
|
+
protect: 0,
|
|
249
|
+
sensitization: 0,
|
|
250
|
+
state: 'none',
|
|
251
|
+
label: '',
|
|
252
|
+
rare: false,
|
|
253
|
+
since: now0,
|
|
254
|
+
adj: Object.fromEntries(HORMONES.map((h) => [h.id, 0])),
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// 恢复持久化状态。这里只接受已知字段并逐个校验,避免坏文件把引擎拖死。
|
|
258
|
+
if (options.state && typeof options.state === 'object') {
|
|
259
|
+
const saved = options.state
|
|
260
|
+
const savedLast = Number(saved.last)
|
|
261
|
+
if (Number.isFinite(savedLast)) last = savedLast
|
|
262
|
+
if (saved.pulse && typeof saved.pulse === 'object') {
|
|
263
|
+
for (const h of HORMONES) pulse[h.id] = clamp(Number(saved.pulse[h.id]) || 0, -80, 80)
|
|
264
|
+
}
|
|
265
|
+
if (saved.diseaseAdj && typeof saved.diseaseAdj === 'object') {
|
|
266
|
+
for (const h of HORMONES) diseaseAdj[h.id] = clamp(Number(saved.diseaseAdj[h.id]) || 0, -20, 20)
|
|
267
|
+
}
|
|
268
|
+
if (saved.disease && typeof saved.disease === 'object') {
|
|
269
|
+
for (const d of DISEASES) {
|
|
270
|
+
const src = saved.disease[d.slug]
|
|
271
|
+
if (!src || typeof src !== 'object') continue
|
|
272
|
+
const target = disease[d.slug]
|
|
273
|
+
target.severity = clamp(Number(src.severity) || 0, 0, 100)
|
|
274
|
+
target.active = Boolean(src.active) || target.severity > 0
|
|
275
|
+
target.curing = Boolean(src.curing)
|
|
276
|
+
target.flares = Math.max(0, Math.round(Number(src.flares) || 0))
|
|
277
|
+
target.reliefUntil = Number(src.reliefUntil) || 0
|
|
278
|
+
target.reliefKind = String(src.reliefKind || '')
|
|
279
|
+
target.startedAt = Number(src.startedAt) || 0
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (saved.cycle && typeof saved.cycle === 'object') {
|
|
283
|
+
cycle.length = clamp(Math.round(Number(saved.cycle.length) || 28), 21, 45)
|
|
284
|
+
cycle.day = clamp(Math.round(Number(saved.cycle.day) || 1), 1, cycle.length)
|
|
285
|
+
cycle.startedAt = Number(saved.cycle.startedAt) || now0
|
|
286
|
+
if (saved.cycle.adj && typeof saved.cycle.adj === 'object') {
|
|
287
|
+
for (const h of HORMONES) cycle.adj[h.id] = clamp(Number(saved.cycle.adj[h.id]) || 0, -20, 20)
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (saved.mood && typeof saved.mood === 'object') {
|
|
291
|
+
mood.id = String(saved.mood.id || 'calm')
|
|
292
|
+
mood.label = String(saved.mood.label || '平静')
|
|
293
|
+
mood.intensity = clamp(Number(saved.mood.intensity) || 0, 0, 100)
|
|
294
|
+
mood.valence = Number.isFinite(Number(saved.mood.valence)) ? Number(saved.mood.valence) : 0.6
|
|
295
|
+
mood.arousal = Number.isFinite(Number(saved.mood.arousal)) ? Number(saved.mood.arousal) : 0.2
|
|
296
|
+
mood.effects = Array.isArray(saved.mood.effects) ? saved.mood.effects.filter(e => e && HORMONES.some(h => h.id === e.hormone)).map(e => ({ hormone: e.hormone, delta: clamp(Number(e.delta) || 0, -15, 15), tauSec: clamp(Number(e.tauSec) || 600, 30, 1800) })) : []
|
|
297
|
+
mood.since = Number(saved.mood.since) || now0
|
|
298
|
+
if (saved.mood.adj && typeof saved.mood.adj === 'object') {
|
|
299
|
+
for (const h of HORMONES) mood.adj[h.id] = clamp(Number(saved.mood.adj[h.id]) || 0, -15, 15)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (saved.mental && typeof saved.mental === 'object' && saved.mental.state in MENTAL_STATES) {
|
|
303
|
+
mental.load = clamp(Number(saved.mental.load) || 0, 0, 100)
|
|
304
|
+
mental.protect = clamp(Number(saved.mental.protect) || 0, 0, 100)
|
|
305
|
+
mental.sensitization = clamp(Number(saved.mental.sensitization) || 0, 0, 100)
|
|
306
|
+
mental.state = saved.mental.state
|
|
307
|
+
mental.label = MENTAL_STATES[mental.state].label
|
|
308
|
+
mental.rare = Boolean(saved.mental.rare)
|
|
309
|
+
mental.since = Number(saved.mental.since) || now0
|
|
310
|
+
if (saved.mental.adj && typeof saved.mental.adj === 'object') {
|
|
311
|
+
for (const h of HORMONES) mental.adj[h.id] = clamp(Number(saved.mental.adj[h.id]) || 0, -20, 20)
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function cycleDayAt(now) {
|
|
317
|
+
const days = (now - cycle.startedAt) / 86400000
|
|
318
|
+
const n = Math.floor(days)
|
|
319
|
+
return 1 + ((n % cycle.length) + cycle.length) % cycle.length
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function cycleTarget(phase) {
|
|
323
|
+
if (phase === 'menstrual') return { cortisol: 3 }
|
|
324
|
+
if (phase === 'luteal') return { cortisol: 6, leptin: -6, melatonin: -2 }
|
|
325
|
+
if (phase === 'ovulation') return { testosterone: 3, epinephrine: 2 }
|
|
326
|
+
return {}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function cycleFlareMul(phase) {
|
|
330
|
+
if (phase === 'menstrual') return 1.5
|
|
331
|
+
if (phase === 'luteal') return 1.35
|
|
332
|
+
return 1
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function tick(now = Date.now()) {
|
|
336
|
+
const dt = Math.max(0, (now - last) / 1000)
|
|
337
|
+
last = now
|
|
338
|
+
for (const h of HORMONES) {
|
|
339
|
+
pulse[h.id] *= Math.exp(-dt / h.tau)
|
|
340
|
+
if (Math.abs(pulse[h.id]) < 0.05) pulse[h.id] = 0
|
|
341
|
+
}
|
|
342
|
+
cycle.day = cycleDayAt(now)
|
|
343
|
+
const phase = cyclePhase(cycle.day, cycle.length)
|
|
344
|
+
const ct = cycleTarget(phase)
|
|
345
|
+
const ck = 1 - Math.exp(-dt / 600)
|
|
346
|
+
for (const h of HORMONES) {
|
|
347
|
+
const t = clamp(ct[h.id] || 0, -20, 20)
|
|
348
|
+
cycle.adj[h.id] += (t - cycle.adj[h.id]) * ck
|
|
349
|
+
}
|
|
350
|
+
const minutes = dt / 60
|
|
351
|
+
for (const d of DISEASES) {
|
|
352
|
+
const s = disease[d.slug]
|
|
353
|
+
if (!s.active && !s.curing) continue
|
|
354
|
+
if (s.curing) {
|
|
355
|
+
s.severity *= Math.exp(-dt / 600)
|
|
356
|
+
if (s.severity < 0.5) {
|
|
357
|
+
s.severity = 0
|
|
358
|
+
s.active = false
|
|
359
|
+
s.curing = false
|
|
360
|
+
s.reliefUntil = 0
|
|
361
|
+
s.reliefKind = ''
|
|
362
|
+
}
|
|
363
|
+
continue
|
|
364
|
+
}
|
|
365
|
+
s.severity = clamp(s.severity + (Number(d.gain) || 0.3) * minutes * 4, 0, 100)
|
|
366
|
+
if (autoFlare && random() < (Number(d.flareChance) || 0.3) * (dt / 3600) * cycleFlareMul(phase)) {
|
|
367
|
+
s.severity = clamp(s.severity + (Number(d.flareGain) || 20), 0, 100)
|
|
368
|
+
s.flares += 1
|
|
369
|
+
}
|
|
370
|
+
if (s.reliefUntil > now) s.severity = clamp(s.severity - 6 * minutes, 0, 100)
|
|
371
|
+
}
|
|
372
|
+
const target = Object.fromEntries(HORMONES.map((h) => [h.id, 0]))
|
|
373
|
+
for (const d of DISEASES) {
|
|
374
|
+
const s = disease[d.slug]
|
|
375
|
+
if (s.severity <= 0) continue
|
|
376
|
+
for (const [h, v] of Object.entries(d.hormones || {})) {
|
|
377
|
+
if (h in target) target[h] += v * (s.severity / 100)
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const k = 1 - Math.exp(-dt / 300)
|
|
381
|
+
for (const h of HORMONES) {
|
|
382
|
+
const t = clamp(target[h.id], -20, 20)
|
|
383
|
+
diseaseAdj[h.id] += (t - diseaseAdj[h.id]) * k
|
|
384
|
+
}
|
|
385
|
+
// 心情层:每个效应对目标激素做一阶低通(各自 tau),满强度按 intensity/100 缩放,夹 ±15。
|
|
386
|
+
const moodTarget = Object.fromEntries(HORMONES.map((h) => [h.id, 0]))
|
|
387
|
+
const moodTau = Object.fromEntries(HORMONES.map((h) => [h.id, 600]))
|
|
388
|
+
for (const e of mood.effects) {
|
|
389
|
+
if (!(e.hormone in moodTarget)) continue
|
|
390
|
+
moodTarget[e.hormone] += Number(e.delta) || 0
|
|
391
|
+
moodTau[e.hormone] = Math.min(moodTau[e.hormone], clamp(Number(e.tauSec) || 600, 30, 1800))
|
|
392
|
+
}
|
|
393
|
+
const moodScale = clamp(Number(mood.intensity) || 0, 0, 100) / 100
|
|
394
|
+
for (const h of HORMONES) {
|
|
395
|
+
const t = clamp(moodTarget[h.id] * moodScale, -15, 15)
|
|
396
|
+
const mk = 1 - Math.exp(-dt / moodTau[h.id])
|
|
397
|
+
mood.adj[h.id] += (t - mood.adj[h.id]) * mk
|
|
398
|
+
}
|
|
399
|
+
// 精神状态层:状态 → 激素偏移,τ=1800s 平滑;负荷本身直接压皮质醇(见 snapshot/curveOf)。
|
|
400
|
+
const st = MENTAL_STATES[mental.state] || MENTAL_STATES.none
|
|
401
|
+
const sk = 1 - Math.exp(-dt / 1800)
|
|
402
|
+
for (const h of HORMONES) {
|
|
403
|
+
const t = clamp(Number(st.effects[h.id] || 0), -20, 20)
|
|
404
|
+
mental.adj[h.id] += (t - mental.adj[h.id]) * sk
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// 负荷只压皮质醇:抬基线、压昼夜振幅(对应抑郁的昼夜斜率变平)。
|
|
409
|
+
function loadAdjOf(h) {
|
|
410
|
+
return h.id === 'cortisol' ? clamp(mental.load * 0.12, 0, 12) : 0
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function ampScaleOf(h) {
|
|
414
|
+
return h.id === 'cortisol' ? 1 - clamp(mental.load, 0, 100) / 250 : 1
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function snapshot(now = Date.now()) {
|
|
418
|
+
tick(now)
|
|
419
|
+
const hour = hourOf(now)
|
|
420
|
+
return HORMONES.map((h) => ({
|
|
421
|
+
id: h.id,
|
|
422
|
+
name: h.name,
|
|
423
|
+
en: h.en,
|
|
424
|
+
organ: h.organ,
|
|
425
|
+
color: h.color,
|
|
426
|
+
intro: h.intro,
|
|
427
|
+
baseline: h.baseline,
|
|
428
|
+
amp: h.amp,
|
|
429
|
+
phase: h.phase,
|
|
430
|
+
pulse: Math.round(pulse[h.id] * 10) / 10,
|
|
431
|
+
diseaseAdj: Math.round(diseaseAdj[h.id] * 10) / 10,
|
|
432
|
+
value: Math.round(valueOf(h, hour, pulse[h.id], diseaseAdj[h.id] + cycle.adj[h.id], cycle.day, mood.adj[h.id], loadAdjOf(h), mental.adj[h.id], ampScaleOf(h)) * 10) / 10,
|
|
433
|
+
curve: curveOf(h, now),
|
|
434
|
+
}))
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function curveOf(h, now = Date.now()) {
|
|
438
|
+
const hour = hourOf(now)
|
|
439
|
+
const day = cycleDayAt(now)
|
|
440
|
+
const out = []
|
|
441
|
+
for (let i = 0; i < 24; i += 1) {
|
|
442
|
+
const hh = (hour + i) % 24
|
|
443
|
+
const p = pulse[h.id] * Math.exp(-(i * 3600) / h.tau)
|
|
444
|
+
out.push(Math.round(valueOf(h, hh, p, diseaseAdj[h.id] + cycle.adj[h.id], day, mood.adj[h.id], loadAdjOf(h), mental.adj[h.id], ampScaleOf(h)) * 10) / 10)
|
|
445
|
+
}
|
|
446
|
+
return out
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function push(id, delta) {
|
|
450
|
+
if (!(id in pulse)) return { ok: false, error: `unknown hormone: ${id}` }
|
|
451
|
+
const d = Number(delta)
|
|
452
|
+
if (!Number.isFinite(d)) return { ok: false, error: 'delta must be a number' }
|
|
453
|
+
pulse[id] = clamp(pulse[id] + d, -80, 80)
|
|
454
|
+
return { ok: true, id, pulse: Math.round(pulse[id] * 10) / 10 }
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// 强行治疗:注射某个激素(急救式,只拉数值,不治病,按 tau 回落)。
|
|
458
|
+
function inject(id, delta) {
|
|
459
|
+
const d = Number(delta)
|
|
460
|
+
if (!Number.isFinite(d)) return { ok: false, error: 'delta must be a number' }
|
|
461
|
+
return push(id, clamp(d, -60, 60))
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// 强行治疗:吃药(市面常见药 → 一组激素的一次性拉扯;部分药对对应病有缓解)。
|
|
465
|
+
function takeDrug(id) {
|
|
466
|
+
const drug = DRUGS.find((x) => x.id === id)
|
|
467
|
+
if (!drug) return { ok: false, error: `unknown drug: ${id}`, drugs: DRUGS.map((x) => x.id) }
|
|
468
|
+
for (const [h, v] of Object.entries(drug.push)) push(h, v)
|
|
469
|
+
let relieved = null
|
|
470
|
+
if (drug.relief) {
|
|
471
|
+
const s = disease[drug.relief.slug]
|
|
472
|
+
if (s && s.severity > 0) {
|
|
473
|
+
s.severity = Math.max(0, s.severity - drug.relief.amount)
|
|
474
|
+
relieved = { slug: drug.relief.slug, amount: drug.relief.amount, severity: Math.round(s.severity) }
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return { ok: true, id, name: drug.name, effects: drug.push, relieved }
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function reset() {
|
|
481
|
+
for (const h of HORMONES) pulse[h.id] = 0
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function applyScenario(id) {
|
|
485
|
+
const sc = SCENARIOS[id]
|
|
486
|
+
if (!sc) return { ok: false, error: `unknown scenario: ${id}` }
|
|
487
|
+
if (sc.reset) reset()
|
|
488
|
+
for (const [hid, delta] of Object.entries(sc.push)) push(hid, delta)
|
|
489
|
+
return { ok: true, scenario: id }
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function diseases(now = Date.now()) {
|
|
493
|
+
return DISEASES.map((d) => {
|
|
494
|
+
const s = disease[d.slug]
|
|
495
|
+
const sev = Math.round(s.severity)
|
|
496
|
+
const tier = diseaseTier(sev)
|
|
497
|
+
return {
|
|
498
|
+
slug: d.slug,
|
|
499
|
+
name: d.name,
|
|
500
|
+
severity: sev,
|
|
501
|
+
active: sev > 0,
|
|
502
|
+
curing: s.curing,
|
|
503
|
+
tier,
|
|
504
|
+
bodyText: (d.body && d.body[tier]) || '',
|
|
505
|
+
voice: (d.voice && (tier === 'flare' ? d.voice.flare : d.voice.base)) || '',
|
|
506
|
+
reliefs: (d.reliefs || []).map((r) => ({ id: r.id, label: r.label })),
|
|
507
|
+
note: d.note,
|
|
508
|
+
intro: d.intro || '',
|
|
509
|
+
}
|
|
510
|
+
})
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function triggerDisease(slug) {
|
|
514
|
+
if (!(slug in disease)) return { ok: false, error: `unknown disease: ${slug}` }
|
|
515
|
+
const s = disease[slug]
|
|
516
|
+
s.active = true
|
|
517
|
+
s.curing = false
|
|
518
|
+
s.severity = Math.max(s.severity, 30)
|
|
519
|
+
s.startedAt = s.startedAt || Date.now()
|
|
520
|
+
s.reliefUntil = 0
|
|
521
|
+
s.reliefKind = ''
|
|
522
|
+
return { ok: true, disease: slug, severity: Math.round(s.severity) }
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function reliefDisease(slug, reliefId) {
|
|
526
|
+
if (!(slug in disease)) return { ok: false, error: `unknown disease: ${slug}` }
|
|
527
|
+
const d = DISEASES.find((x) => x.slug === slug)
|
|
528
|
+
const r = (d && d.reliefs || []).find((x) => x.id === reliefId)
|
|
529
|
+
if (!r) return { ok: false, error: `unknown relief: ${reliefId}` }
|
|
530
|
+
const s = disease[slug]
|
|
531
|
+
s.severity = clamp(s.severity - Number(r.relief || 0), 0, 100)
|
|
532
|
+
s.reliefUntil = Date.now() + (Number(r.minutes) || 20) * 60_000
|
|
533
|
+
s.reliefKind = reliefId
|
|
534
|
+
return { ok: true, disease: slug, relief: reliefId, severity: Math.round(s.severity) }
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function cureDisease(slug) {
|
|
538
|
+
if (!(slug in disease)) return { ok: false, error: `unknown disease: ${slug}` }
|
|
539
|
+
const s = disease[slug]
|
|
540
|
+
s.curing = true
|
|
541
|
+
s.active = true
|
|
542
|
+
return { ok: true, disease: slug, curing: true }
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function cycleInfo(now = Date.now()) {
|
|
546
|
+
tick(now)
|
|
547
|
+
const phase = cyclePhase(cycle.day, cycle.length)
|
|
548
|
+
const ov = Math.max(2, Math.round(cycle.length / 2))
|
|
549
|
+
const phases = [
|
|
550
|
+
{ id: 'menstrual', label: '月经期', from: 1, to: 5 },
|
|
551
|
+
{ id: 'follicular', label: '卵泡期', from: 6, to: ov - 1 },
|
|
552
|
+
{ id: 'ovulation', label: '排卵期', from: ov, to: ov },
|
|
553
|
+
{ id: 'luteal', label: '黄体期', from: ov + 1, to: cycle.length },
|
|
554
|
+
]
|
|
555
|
+
return {
|
|
556
|
+
day: cycle.day,
|
|
557
|
+
length: cycle.length,
|
|
558
|
+
phase,
|
|
559
|
+
phaseLabel: phaseLabel(phase),
|
|
560
|
+
ovDay: ov,
|
|
561
|
+
phases,
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function setCycleDay(day, now = Date.now()) {
|
|
566
|
+
const n = Math.round(Number(day))
|
|
567
|
+
if (!Number.isFinite(n) || n < 1 || n > cycle.length) {
|
|
568
|
+
return { ok: false, error: `day must be 1..${cycle.length}` }
|
|
569
|
+
}
|
|
570
|
+
cycle.day = n
|
|
571
|
+
cycle.startedAt = now - (n - 1) * 86400000
|
|
572
|
+
return { ok: true, ...cycleInfo(now) }
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function advanceCycle(n = 1, now = Date.now()) {
|
|
576
|
+
const step = Number(n)
|
|
577
|
+
if (!Number.isFinite(step) || step === 0) return { ok: false, error: 'n must be a non-zero number' }
|
|
578
|
+
return setCycleDay(cycle.day + step, now)
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function resetCycle(now = Date.now()) {
|
|
582
|
+
return setCycleDay(1, now)
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// 心情:由 fatfish-mood 插件把「情绪 → 一组激素偏移」传进来;这里只做状态层,不碰规则层。
|
|
586
|
+
function setMood(m = {}) {
|
|
587
|
+
const rawId = String(m.id || '').trim()
|
|
588
|
+
const rawLabel = String(m.label || '').trim()
|
|
589
|
+
const intensity = clamp(Math.round(Number(m.intensity) || 0), 0, 100)
|
|
590
|
+
if (rawId && rawId !== 'calm' && intensity > 0 && !rawLabel) {
|
|
591
|
+
return { ok: false, error: `missing label for mood: ${rawId}`, moods: [] }
|
|
592
|
+
}
|
|
593
|
+
const id = rawId || 'calm'
|
|
594
|
+
const effects = Array.isArray(m.effects)
|
|
595
|
+
? m.effects
|
|
596
|
+
.filter((e) => e && HORMONES.some((h) => h.id === e.hormone))
|
|
597
|
+
.map((e) => ({
|
|
598
|
+
hormone: e.hormone,
|
|
599
|
+
delta: clamp(Number(e.delta) || 0, -15, 15),
|
|
600
|
+
tauSec: clamp(Number(e.tauSec) || 600, 30, 1800),
|
|
601
|
+
}))
|
|
602
|
+
: []
|
|
603
|
+
mood.id = intensity > 0 && id !== 'calm' ? id : 'calm'
|
|
604
|
+
mood.label = intensity > 0 && id !== 'calm' ? String(m.label || id) : '平静'
|
|
605
|
+
mood.intensity = mood.id === 'calm' ? 0 : intensity
|
|
606
|
+
mood.effects = mood.id === 'calm' ? [] : effects
|
|
607
|
+
mood.valence = Number.isFinite(Number(m.valence)) ? Number(m.valence) : 0
|
|
608
|
+
mood.arousal = Number.isFinite(Number(m.arousal)) ? Number(m.arousal) : 0
|
|
609
|
+
mood.since = Date.now()
|
|
610
|
+
return { ok: true, ...moodInfo() }
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function clearMood() {
|
|
614
|
+
return setMood({ id: 'calm', intensity: 0 })
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
function moodInfo() {
|
|
618
|
+
return {
|
|
619
|
+
id: mood.id,
|
|
620
|
+
label: mood.label,
|
|
621
|
+
intensity: mood.intensity,
|
|
622
|
+
valence: mood.valence,
|
|
623
|
+
arousal: mood.arousal,
|
|
624
|
+
effects: mood.effects,
|
|
625
|
+
adj: Object.fromEntries(HORMONES.map((h) => [h.id, Math.round(mood.adj[h.id] * 10) / 10])),
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// 精神状态:由 fatfish-mood 推送负荷/保护/敏化与状态标签;这里只校验并折算,不做判定。
|
|
630
|
+
// 稀有档(fatfish-psych 触发)优先级高于常见档:稀有档生效期间,mood 的常规推送只同步数值,不改状态。
|
|
631
|
+
function setMental(m = {}) {
|
|
632
|
+
const load = clamp(Math.round(Number(m.load) || 0), 0, 100)
|
|
633
|
+
const protect = clamp(Math.round(Number(m.protect) || 0), 0, 100)
|
|
634
|
+
const sens = clamp(Math.round(Number(m.sensitization) || 0), 0, 100)
|
|
635
|
+
const raw = String(m.state || 'none').trim()
|
|
636
|
+
if (!(raw in MENTAL_STATES)) {
|
|
637
|
+
return { ok: false, error: `unknown state: ${raw}`, states: Object.keys(MENTAL_STATES) }
|
|
638
|
+
}
|
|
639
|
+
const incomingRare = m.rare === true || RARE_STATE_IDS.includes(raw)
|
|
640
|
+
// 稀有档保护:常规推送只同步数值;但 state='none' 永远允许(clearMental 要能清)。
|
|
641
|
+
if (mental.rare && !incomingRare && raw !== 'none') {
|
|
642
|
+
mental.load = load
|
|
643
|
+
mental.protect = protect
|
|
644
|
+
mental.sensitization = sens
|
|
645
|
+
return { ok: true, ...mentalInfo(), kept: 'rare' }
|
|
646
|
+
}
|
|
647
|
+
if (raw !== mental.state) mental.since = Date.now()
|
|
648
|
+
mental.load = load
|
|
649
|
+
mental.protect = protect
|
|
650
|
+
mental.sensitization = sens
|
|
651
|
+
mental.state = raw
|
|
652
|
+
mental.rare = incomingRare
|
|
653
|
+
mental.label = MENTAL_STATES[raw].label
|
|
654
|
+
return { ok: true, ...mentalInfo() }
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function clearMental() {
|
|
658
|
+
return setMental({ load: 0, protect: 0, sensitization: 0, state: 'none' })
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function mentalInfo() {
|
|
662
|
+
const st = MENTAL_STATES[mental.state] || MENTAL_STATES.none
|
|
663
|
+
return {
|
|
664
|
+
load: Math.round(mental.load * 10) / 10,
|
|
665
|
+
protect: Math.round(mental.protect * 10) / 10,
|
|
666
|
+
sensitization: Math.round(mental.sensitization * 10) / 10,
|
|
667
|
+
state: mental.state,
|
|
668
|
+
label: st.label,
|
|
669
|
+
rare: mental.rare,
|
|
670
|
+
since: mental.since,
|
|
671
|
+
adj: Object.fromEntries(HORMONES.map((h) => [h.id, Math.round(mental.adj[h.id] * 10) / 10])),
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function state(now = Date.now()) {
|
|
676
|
+
return { hour: Math.round(hourOf(now) * 100) / 100, items: snapshot(now), diseases: diseases(now), cycle: cycleInfo(now), mood: moodInfo(), mental: mentalInfo() }
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
function serialize() {
|
|
680
|
+
return {
|
|
681
|
+
version: 1,
|
|
682
|
+
last,
|
|
683
|
+
pulse: { ...pulse },
|
|
684
|
+
diseaseAdj: { ...diseaseAdj },
|
|
685
|
+
disease: Object.fromEntries(DISEASES.map((d) => [d.slug, { ...disease[d.slug] }])),
|
|
686
|
+
cycle: { length: cycle.length, day: cycle.day, startedAt: cycle.startedAt, adj: { ...cycle.adj } },
|
|
687
|
+
mood: {
|
|
688
|
+
id: mood.id, label: mood.label, intensity: mood.intensity,
|
|
689
|
+
valence: mood.valence, arousal: mood.arousal, since: mood.since,
|
|
690
|
+
effects: mood.effects.map((e) => ({ ...e })), adj: { ...mood.adj },
|
|
691
|
+
},
|
|
692
|
+
mental: {
|
|
693
|
+
load: mental.load, protect: mental.protect, sensitization: mental.sensitization,
|
|
694
|
+
state: mental.state, label: mental.label, rare: mental.rare, since: mental.since,
|
|
695
|
+
adj: { ...mental.adj },
|
|
696
|
+
},
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
return { tick, snapshot, curveOf, push, inject, takeDrug, reset, applyScenario, diseases, triggerDisease, reliefDisease, cureDisease, state, serialize, pulse, disease, diseaseAdj, cycle, cycleInfo, setCycleDay, advanceCycle, resetCycle, setMood, clearMood, moodInfo, mood, setMental, clearMental, mentalInfo, mental }
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
const STYLE_HINTS = {
|
|
704
|
+
cortisol: '皮质醇高:说话短、少展开,遇事先给结论。',
|
|
705
|
+
epinephrine: '肾上腺素高:句子更直接,不绕弯。',
|
|
706
|
+
melatonin: '褪黑素高:夜间模式,只回必要内容。',
|
|
707
|
+
insulin: '胰岛素高:饱食后,语气松一点。',
|
|
708
|
+
leptin: '瘦素高:不急,慢慢说。',
|
|
709
|
+
testosterone: '睾酮高:更敢顶,但不攻击身份。',
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
const MOOD_HINTS = {
|
|
713
|
+
happy: '开心:语气松一点,可以多一点轻快。',
|
|
714
|
+
excited: '兴奋:可以多展开,但仍先给结论。',
|
|
715
|
+
anxious: '焦虑:句子更短,先给结论,别铺垫。',
|
|
716
|
+
angry: '生气:语气硬一点,但不攻击人、不翻旧账。',
|
|
717
|
+
sad: '低落:少感叹,语速放慢,别硬撑。',
|
|
718
|
+
tired: '疲惫:能省则省,短句优先。',
|
|
719
|
+
close: '亲密:语气软一点,可以多一句关心。',
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
const MENTAL_HINTS = {
|
|
723
|
+
depressive: '抑郁样(示意):话少但不冷,别劝、别总结人生。',
|
|
724
|
+
anxiety: '焦虑样(示意):先给确定的信息,别抛一串不确定的选项。',
|
|
725
|
+
psychosis: '精神病样(示意):只作语气参考,不做任何诊断或症状描述。',
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
export function renderStateText(items, diseases = [], cycle = null, legacy = null, mood = null, mental = null, redact = true) {
|
|
729
|
+
const tier = (x) => (x >= 65 ? '偏高' : x <= 35 ? '偏低' : '正常')
|
|
730
|
+
const num = (x) => (redact ? '' : ` ${Math.round(x)}`)
|
|
731
|
+
const active = (Array.isArray(diseases) ? diseases : []).filter((d) => d && d.severity > 0)
|
|
732
|
+
.sort((a, b) => b.severity - a.severity)
|
|
733
|
+
.slice(0, 2)
|
|
734
|
+
const list = Array.isArray(items) ? items : []
|
|
735
|
+
const byId = Object.fromEntries(list.map((it) => [it.id, it]))
|
|
736
|
+
const v = (id) => (byId[id] ? (redact ? tier(byId[id].value) : Math.round(byId[id].value)) : '—')
|
|
737
|
+
const cyc = cycle && typeof cycle === 'object' && cycle.day ? cycle : null
|
|
738
|
+
const overlap = legacyOverlap()
|
|
739
|
+
const legacyLine = legacy && legacy.detected
|
|
740
|
+
? `接管:检测到 ${legacy.packageName}(${legacy.source === 'runtime' ? '运行中' : '已安装'}),重合疾病 ${overlap.length} 个(${overlap.map((x) => x.name).join('、')})以本段为准;不重合的病仍由它管理。`
|
|
741
|
+
: ''
|
|
742
|
+
const cycLine = cyc
|
|
743
|
+
? `周期:${cyc.phaseLabel || phaseLabel(cyc.phase)} 第 ${cyc.day}/${cyc.length} 天(雌二醇 ${v('estradiol')} / 孕酮 ${v('progesterone')})`
|
|
744
|
+
: ''
|
|
745
|
+
const cycHint = cyc
|
|
746
|
+
? ({
|
|
747
|
+
menstrual: '经期:身体不适,耐心低,只说必要。',
|
|
748
|
+
luteal: '黄体期:容易烦,句子更短,别绕弯。',
|
|
749
|
+
ovulation: '排卵期:精力好,可以多展开一点。',
|
|
750
|
+
})[cyc.phase] || ''
|
|
751
|
+
: ''
|
|
752
|
+
const mo = mood && typeof mood === 'object' && mood.id && mood.id !== 'calm' && Number(mood.intensity) > 0 ? mood : null
|
|
753
|
+
const moodAdjText = mo && mo.adj
|
|
754
|
+
? Object.entries(mo.adj)
|
|
755
|
+
.filter(([, v]) => Math.abs(Number(v)) >= 0.5)
|
|
756
|
+
.slice(0, 4)
|
|
757
|
+
.map(([k, v]) => `${k} ${v > 0 ? '+' : ''}${v}`)
|
|
758
|
+
.join(' / ')
|
|
759
|
+
: ''
|
|
760
|
+
const moodLine = mo ? `心情:${mo.label} ${Math.round(mo.intensity)}${moodAdjText ? `(${moodAdjText})` : ''}` : ''
|
|
761
|
+
const mt = mental && typeof mental === 'object'
|
|
762
|
+
&& (Number(mental.load) > 0 || (mental.state && mental.state !== 'none')) ? mental : null
|
|
763
|
+
const mentalLine = mt
|
|
764
|
+
? `负荷 ${Math.round(Number(mental.load) || 0)}/100${Number(mental.protect) > 0 ? `(保护 ${Math.round(Number(mental.protect))})` : ''}${mental.state && mental.state !== 'none' ? ` · 状态:${mental.label || mental.state}` : ''}`
|
|
765
|
+
: ''
|
|
766
|
+
if (list.length === 0) {
|
|
767
|
+
const sick = active.length ? `当前状态:${active.map((d) => `${d.name}(${tier(d.severity)})`).join(';')}。` : ''
|
|
768
|
+
return [
|
|
769
|
+
'【肥鱼身体状态 · 仅影响语气与风格】',
|
|
770
|
+
sick || '各项激素大体平稳。',
|
|
771
|
+
moodLine,
|
|
772
|
+
mentalLine,
|
|
773
|
+
cycLine,
|
|
774
|
+
cycHint,
|
|
775
|
+
legacyLine,
|
|
776
|
+
'边界:拒绝、权限、不可逆动作的判定不读取本段;本段只改说话方式。',
|
|
777
|
+
].filter(Boolean).join('\n')
|
|
778
|
+
}
|
|
779
|
+
const high = list.filter((i) => i.value >= 65).sort((a, b) => b.value - a.value)
|
|
780
|
+
const low = list.filter((i) => i.value <= 35).sort((a, b) => a.value - b.value)
|
|
781
|
+
const parts = []
|
|
782
|
+
if (high.length) parts.push(high.slice(0, 3).map((i) => `${i.name}${num(i.value)} 偏高`).join(';'))
|
|
783
|
+
if (low.length) parts.push(low.slice(0, 3).map((i) => `${i.name}${num(i.value)} 偏低`).join(';'))
|
|
784
|
+
const body = parts.length ? `${parts.join('。')}。` : '各项激素大体平稳。'
|
|
785
|
+
const hints = []
|
|
786
|
+
if (cycHint) hints.push(cycHint)
|
|
787
|
+
if (mo && MOOD_HINTS[mo.id]) hints.push(MOOD_HINTS[mo.id])
|
|
788
|
+
if (mt && MENTAL_HINTS[mt.state]) hints.push(MENTAL_HINTS[mt.state])
|
|
789
|
+
for (const i of high) {
|
|
790
|
+
if (STYLE_HINTS[i.id] && !hints.includes(STYLE_HINTS[i.id])) hints.push(STYLE_HINTS[i.id])
|
|
791
|
+
}
|
|
792
|
+
if (low.some((i) => i.id === 'melatonin')) hints.push('褪黑素低:白天模式,正常节奏。')
|
|
793
|
+
const sickLines = []
|
|
794
|
+
for (const d of active) {
|
|
795
|
+
sickLines.push(`${d.name}(${tier(d.severity)},${d.tier === 'flare' ? '重' : d.tier === 'ache' ? '中度' : '轻'}):${d.bodyText}`)
|
|
796
|
+
if (d.voice && !hints.includes(d.voice)) hints.push(d.voice)
|
|
797
|
+
}
|
|
798
|
+
const hintText = hints.length ? `建议:${hints.slice(0, 4).join(' ')}` : '建议:按平时语气。'
|
|
799
|
+
return [
|
|
800
|
+
'【肥鱼身体状态 · 仅影响语气与风格】',
|
|
801
|
+
body,
|
|
802
|
+
moodLine,
|
|
803
|
+
mentalLine,
|
|
804
|
+
cycLine,
|
|
805
|
+
sickLines.length ? `疾病:${sickLines.join(';')}` : '',
|
|
806
|
+
hintText,
|
|
807
|
+
legacyLine,
|
|
808
|
+
'边界:拒绝、权限、不可逆动作的判定不读取本段;本段只改说话方式。',
|
|
809
|
+
].filter(Boolean).join('\n')
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
export function legacyOverlap() {
|
|
813
|
+
return Object.keys(LEGACY_MAP).map((legacyId) => {
|
|
814
|
+
const slug = LEGACY_MAP[legacyId]
|
|
815
|
+
const d = DISEASES.find((x) => x.slug === slug)
|
|
816
|
+
return { legacyId, slug, name: d ? d.name : slug }
|
|
817
|
+
})
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// 三路探测:运行时工具表(最硬)→ 安装清单(profile 里被引用)→ 都没有。
|
|
821
|
+
export function detectLegacy(tools = null) {
|
|
822
|
+
const hit = []
|
|
823
|
+
try {
|
|
824
|
+
const defs = tools && typeof tools.definitions === 'function' ? tools.definitions() : []
|
|
825
|
+
for (const def of defs || []) if (def && LEGACY_TOOLS.includes(def.name)) hit.push(def.name)
|
|
826
|
+
} catch {
|
|
827
|
+
// 探测失败继续走安装层兜底,不在这里静默放行
|
|
828
|
+
}
|
|
829
|
+
if (hit.length) return { detected: true, source: 'runtime', tools: hit, packageName: LEGACY_PACKAGE }
|
|
830
|
+
let installed = false
|
|
831
|
+
for (const file of [join(PROFILE_DIR, 'package.json'), join(PROFILE_DIR, 'cordis.patch.yml')]) {
|
|
832
|
+
try {
|
|
833
|
+
if (readFileSync(file, 'utf8').includes(LEGACY_PACKAGE)) {
|
|
834
|
+
installed = true
|
|
835
|
+
break
|
|
836
|
+
}
|
|
837
|
+
} catch {
|
|
838
|
+
// 文件不存在即跳过
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
if (installed) return { detected: true, source: 'installed', tools: [], packageName: LEGACY_PACKAGE }
|
|
842
|
+
return { detected: false, source: 'none', tools: [], packageName: LEGACY_PACKAGE }
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
export function readSettings(path = SETTINGS_PATH) {
|
|
846
|
+
try {
|
|
847
|
+
const raw = JSON.parse(readFileSync(path, 'utf8'))
|
|
848
|
+
return raw && typeof raw === 'object' && !Array.isArray(raw) ? raw : {}
|
|
849
|
+
} catch {
|
|
850
|
+
return {}
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
export function writeSettings(next, path = SETTINGS_PATH) {
|
|
855
|
+
mkdirSync(dirname(path), { recursive: true })
|
|
856
|
+
const tmp = `${path}.tmp`
|
|
857
|
+
writeFileSync(tmp, `${JSON.stringify(next, null, 2)}\n`, 'utf8')
|
|
858
|
+
renameSync(tmp, path)
|
|
859
|
+
return next
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
function readBody(req) {
|
|
863
|
+
return new Promise((resolve, reject) => {
|
|
864
|
+
let size = 0
|
|
865
|
+
const chunks = []
|
|
866
|
+
req.on('data', (c) => {
|
|
867
|
+
size += c.length
|
|
868
|
+
if (size > 64 * 1024) {
|
|
869
|
+
reject(new Error('body too large'))
|
|
870
|
+
req.destroy()
|
|
871
|
+
return
|
|
872
|
+
}
|
|
873
|
+
chunks.push(c)
|
|
874
|
+
})
|
|
875
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
|
|
876
|
+
req.on('error', reject)
|
|
877
|
+
})
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
export function apply(ctx, config = {}) {
|
|
881
|
+
const settings = readSettings()
|
|
882
|
+
const state = {
|
|
883
|
+
injectPrompt: settings.injectPrompt ?? config.injectPrompt ?? false,
|
|
884
|
+
tickSeconds: Number(config.tickSeconds) > 0 ? Number(config.tickSeconds) : 30,
|
|
885
|
+
}
|
|
886
|
+
const engine = createEngine()
|
|
887
|
+
let legacy = detectLegacy(ctx.tools)
|
|
888
|
+
let takeover = null
|
|
889
|
+
const doTakeover = () => {
|
|
890
|
+
if (!legacy.detected || takeover) return
|
|
891
|
+
if (ctx.tools && typeof ctx.tools.restrict === 'function' && legacy.tools.length) {
|
|
892
|
+
try {
|
|
893
|
+
ctx.effect(() => ctx.tools.restrict({ deny: legacy.tools }), 'fatfish-endocrine: legacy takeover')
|
|
894
|
+
takeover = { mode: 'denied', denied: legacy.tools }
|
|
895
|
+
} catch (err) {
|
|
896
|
+
takeover = { mode: 'soft', reason: err && err.message ? err.message : String(err) }
|
|
897
|
+
}
|
|
898
|
+
} else {
|
|
899
|
+
takeover = {
|
|
900
|
+
mode: 'soft',
|
|
901
|
+
reason: legacy.tools.length ? 'tools.restrict unavailable' : 'detected by install manifest only',
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
doTakeover()
|
|
906
|
+
// sick-fatfish 可能比本插件后加载:每次读状态时重探一次,发现即接管(只执行一次)。
|
|
907
|
+
const refreshLegacy = () => {
|
|
908
|
+
if (legacy.detected && takeover) return
|
|
909
|
+
const again = detectLegacy(ctx.tools)
|
|
910
|
+
if (again.detected) {
|
|
911
|
+
legacy = again
|
|
912
|
+
doTakeover()
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
const view = (mask = false) => {
|
|
917
|
+
refreshLegacy()
|
|
918
|
+
const s = engine.state()
|
|
919
|
+
const items = mask
|
|
920
|
+
? s.items.map((x) => ({ id: x.id, name: x.name, organ: x.organ, level: levelOf(x.value) }))
|
|
921
|
+
: s.items
|
|
922
|
+
const diseases = mask
|
|
923
|
+
? (s.diseases || []).map((d) => ({ slug: d.slug, name: d.name, organ: d.organ, level: levelOf(d.severity), tier: d.tier }))
|
|
924
|
+
: s.diseases
|
|
925
|
+
const cycle = mask && s.cycle
|
|
926
|
+
? { day: s.cycle.day, length: s.cycle.length, phase: s.cycle.phase, phaseLabel: s.cycle.phaseLabel, ovDay: s.cycle.ovDay }
|
|
927
|
+
: s.cycle
|
|
928
|
+
const mood = mask && s.mood
|
|
929
|
+
? { id: s.mood.id, label: s.mood.label, intensity: s.mood.intensity, since: s.mood.since }
|
|
930
|
+
: s.mood
|
|
931
|
+
return {
|
|
932
|
+
ok: true,
|
|
933
|
+
now: Date.now(),
|
|
934
|
+
hour: s.hour,
|
|
935
|
+
items,
|
|
936
|
+
diseases,
|
|
937
|
+
cycle,
|
|
938
|
+
mood,
|
|
939
|
+
masked: Boolean(mask),
|
|
940
|
+
settings: { injectPrompt: state.injectPrompt, tickSeconds: state.tickSeconds },
|
|
941
|
+
legacy: {
|
|
942
|
+
detected: legacy.detected,
|
|
943
|
+
source: legacy.source,
|
|
944
|
+
packageName: legacy.packageName,
|
|
945
|
+
tools: legacy.tools,
|
|
946
|
+
overlap: legacyOverlap(),
|
|
947
|
+
takeover: takeover ? takeover.mode : 'none',
|
|
948
|
+
detail: takeover ? (takeover.reason || `denied ${takeover.denied ? takeover.denied.length : 0} tools`) : '',
|
|
949
|
+
},
|
|
950
|
+
preview: renderStateText(s.items, s.diseases, s.cycle, legacy, s.mood, s.mental),
|
|
951
|
+
scenarios: Object.values(SCENARIOS).map((x) => ({ id: x.id, label: x.label })),
|
|
952
|
+
drugs: DRUGS.map((x) => ({ id: x.id, name: x.name, note: x.note, intro: x.intro, effects: x.push, relief: x.relief || null })),
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
ctx.inject(['webServer'], (scope) => {
|
|
957
|
+
const handler = async (req, res) => {
|
|
958
|
+
const send = (code, payload) => {
|
|
959
|
+
res.writeHead(code, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
960
|
+
res.end(JSON.stringify(payload))
|
|
961
|
+
}
|
|
962
|
+
try {
|
|
963
|
+
if (req.method === 'GET') {
|
|
964
|
+
send(200, view())
|
|
965
|
+
return
|
|
966
|
+
}
|
|
967
|
+
if (req.method !== 'POST') {
|
|
968
|
+
res.writeHead(405)
|
|
969
|
+
res.end()
|
|
970
|
+
return
|
|
971
|
+
}
|
|
972
|
+
const body = JSON.parse((await readBody(req)) || '{}')
|
|
973
|
+
if (body.action === 'push') {
|
|
974
|
+
const out = engine.push(body.hormone, body.delta)
|
|
975
|
+
if (!out.ok) send(400, out)
|
|
976
|
+
else send(200, view())
|
|
977
|
+
return
|
|
978
|
+
}
|
|
979
|
+
if (body.action === 'inject') {
|
|
980
|
+
const out = engine.inject(body.hormone, body.delta)
|
|
981
|
+
if (!out.ok) send(400, out)
|
|
982
|
+
else send(200, view())
|
|
983
|
+
return
|
|
984
|
+
}
|
|
985
|
+
if (body.action === 'drug') {
|
|
986
|
+
const out = engine.takeDrug(body.drug)
|
|
987
|
+
if (!out.ok) send(400, out)
|
|
988
|
+
else send(200, view())
|
|
989
|
+
return
|
|
990
|
+
}
|
|
991
|
+
if (body.action === 'scenario') {
|
|
992
|
+
const out = engine.applyScenario(body.name)
|
|
993
|
+
if (!out.ok) send(400, out)
|
|
994
|
+
else send(200, view())
|
|
995
|
+
return
|
|
996
|
+
}
|
|
997
|
+
if (body.action === 'trigger') {
|
|
998
|
+
const out = engine.triggerDisease(body.disease)
|
|
999
|
+
if (!out.ok) send(400, out)
|
|
1000
|
+
else send(200, view())
|
|
1001
|
+
return
|
|
1002
|
+
}
|
|
1003
|
+
if (body.action === 'relief') {
|
|
1004
|
+
const out = engine.reliefDisease(body.disease, body.relief)
|
|
1005
|
+
if (!out.ok) send(400, out)
|
|
1006
|
+
else send(200, view())
|
|
1007
|
+
return
|
|
1008
|
+
}
|
|
1009
|
+
if (body.action === 'cure') {
|
|
1010
|
+
const out = engine.cureDisease(body.disease)
|
|
1011
|
+
if (!out.ok) send(400, out)
|
|
1012
|
+
else send(200, view())
|
|
1013
|
+
return
|
|
1014
|
+
}
|
|
1015
|
+
if (body.action === 'cycleAdvance') {
|
|
1016
|
+
const out = engine.advanceCycle(body.n == null ? 1 : body.n)
|
|
1017
|
+
if (!out.ok) send(400, out)
|
|
1018
|
+
else send(200, view())
|
|
1019
|
+
return
|
|
1020
|
+
}
|
|
1021
|
+
if (body.action === 'cycleSet') {
|
|
1022
|
+
const out = engine.setCycleDay(body.day)
|
|
1023
|
+
if (!out.ok) send(400, out)
|
|
1024
|
+
else send(200, view())
|
|
1025
|
+
return
|
|
1026
|
+
}
|
|
1027
|
+
if (body.action === 'cycleReset') {
|
|
1028
|
+
const out = engine.resetCycle()
|
|
1029
|
+
if (!out.ok) send(400, out)
|
|
1030
|
+
else send(200, view())
|
|
1031
|
+
return
|
|
1032
|
+
}
|
|
1033
|
+
if (body.action === 'mood') {
|
|
1034
|
+
const out = engine.setMood({
|
|
1035
|
+
id: body.mood,
|
|
1036
|
+
label: body.label,
|
|
1037
|
+
intensity: body.intensity,
|
|
1038
|
+
valence: body.valence,
|
|
1039
|
+
arousal: body.arousal,
|
|
1040
|
+
effects: body.effects,
|
|
1041
|
+
})
|
|
1042
|
+
send(200, { ...out, ...view() })
|
|
1043
|
+
return
|
|
1044
|
+
}
|
|
1045
|
+
if (body.action === 'moodClear') {
|
|
1046
|
+
engine.clearMood()
|
|
1047
|
+
send(200, view())
|
|
1048
|
+
return
|
|
1049
|
+
}
|
|
1050
|
+
if (body.action === 'mental') {
|
|
1051
|
+
const out = engine.setMental({
|
|
1052
|
+
load: body.load,
|
|
1053
|
+
protect: body.protect,
|
|
1054
|
+
sensitization: body.sensitization,
|
|
1055
|
+
state: body.state,
|
|
1056
|
+
})
|
|
1057
|
+
if (!out.ok) send(400, out)
|
|
1058
|
+
else send(200, view())
|
|
1059
|
+
return
|
|
1060
|
+
}
|
|
1061
|
+
if (body.action === 'mentalClear') {
|
|
1062
|
+
engine.clearMental()
|
|
1063
|
+
send(200, view())
|
|
1064
|
+
return
|
|
1065
|
+
}
|
|
1066
|
+
if (body.action === 'settings') {
|
|
1067
|
+
const next = { ...readSettings(), injectPrompt: Boolean(body.injectPrompt) }
|
|
1068
|
+
writeSettings(next)
|
|
1069
|
+
state.injectPrompt = next.injectPrompt
|
|
1070
|
+
send(200, view())
|
|
1071
|
+
return
|
|
1072
|
+
}
|
|
1073
|
+
send(400, { ok: false, error: `unknown action: ${body.action}` })
|
|
1074
|
+
} catch (err) {
|
|
1075
|
+
send(500, { ok: false, error: err && err.message ? err.message : String(err) })
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
ctx.effect(() => scope.webServer.register({
|
|
1079
|
+
name: 'fatfish-endocrine',
|
|
1080
|
+
kind: 'exact',
|
|
1081
|
+
path: config.route || ROUTE,
|
|
1082
|
+
handler,
|
|
1083
|
+
}), 'fatfish-endocrine: route')
|
|
1084
|
+
})
|
|
1085
|
+
|
|
1086
|
+
if (state.injectPrompt) {
|
|
1087
|
+
ctx.inject(['systemPrompt'], (scope) => {
|
|
1088
|
+
ctx.effect(() => scope.systemPrompt.section({
|
|
1089
|
+
name: 'fatfish-endocrine',
|
|
1090
|
+
order: 900,
|
|
1091
|
+
text: () => {
|
|
1092
|
+
refreshLegacy()
|
|
1093
|
+
const s = engine.state()
|
|
1094
|
+
return renderStateText(s.items, s.diseases, s.cycle, legacy, s.mood, s.mental)
|
|
1095
|
+
},
|
|
1096
|
+
}), 'fatfish-endocrine: prompt section')
|
|
1097
|
+
})
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
if (ctx.tools && typeof ctx.tools.register === 'function') {
|
|
1101
|
+
ctx.tools.register({
|
|
1102
|
+
name: 'fatfish_endocrine',
|
|
1103
|
+
description: '肥鱼内分泌系统的状态与手动操作。status 看 14 个腺体、28 天月经周期与 17 个内置疾病当前值;preview 看会注入的风格文本;push 推一个激素(hormone id,delta -80..80);inject 强行治疗·急救注射一个激素(hormone + delta,-60..60,只拉数值、不改变疾病,按 tau 回落,不能根治);drug 强行治疗·吃药(drug 传 id,如 coffee / melatonin / ibuprofen,市面常见药映射到一组激素;部分药能缓解对应的内置病,返回 relieved);scenario 用预设场景(stayUp 熬夜 / scare 惊吓 / full 饱食 / calm 平静);trigger 让一个内置疾病发病(disease 传 slug);relief 缓解(disease + relief id);cure 让病按 tau 回落;cycle 看周期(第几天/阶段);cycleAdvance 推进 n 天(默认 1);cycleSet 跳到第 day 天;cycleReset 回第 1 天。若检测到旧插件 dsh-sick-fatfish 同时装载,本插件优先接管两边重合的 10 个病(失眠/痛风/偏头痛等),并尝试用宿主 tools.restrict 拦掉它的 sick_* 工具,拦不住时降级为提示词接管声明(view.legacy 里看 mode)。只影响语气与风格,不参与拒绝、权限与不可逆动作的判定。',
|
|
1104
|
+
parameters: {
|
|
1105
|
+
type: 'object',
|
|
1106
|
+
properties: {
|
|
1107
|
+
action: { type: 'string', enum: ['status', 'preview', 'push', 'inject', 'drug', 'scenario', 'trigger', 'relief', 'cure', 'cycle', 'cycleAdvance', 'cycleSet', 'cycleReset', 'mood', 'moodClear', 'mental', 'mentalClear'] },
|
|
1108
|
+
hormone: { type: 'string', description: '激素 id,如 cortisol、melatonin、estradiol' },
|
|
1109
|
+
delta: { type: 'number', description: '推多少(-80..80;inject 时 -60..60)' },
|
|
1110
|
+
drug: { type: 'string', description: '药物 id,如 coffee、melatonin、ibuprofen、levothyroxine' },
|
|
1111
|
+
name: { type: 'string', description: '场景 id:stayUp / scare / full / calm' },
|
|
1112
|
+
disease: { type: 'string', description: '疾病 slug,如 dsh-insomnia' },
|
|
1113
|
+
relief: { type: 'string', description: '缓解项 id,如 dark' },
|
|
1114
|
+
day: { type: 'number', description: '周期第几天(1..28)' },
|
|
1115
|
+
n: { type: 'number', description: 'cycleAdvance 推进几天,默认 1' },
|
|
1116
|
+
mood: { type: 'string', description: '心情 id(由 fatfish-mood 插件提供,如 anxious/angry/happy)' },
|
|
1117
|
+
intensity: { type: 'number', description: '心情强度 0..100' },
|
|
1118
|
+
load: { type: 'number', description: '累积负荷 0..100(由 fatfish-mood 推送)' },
|
|
1119
|
+
protect: { type: 'number', description: '保护值 0..100' },
|
|
1120
|
+
sensitization: { type: 'number', description: '敏化 0..100' },
|
|
1121
|
+
state: { type: 'string', description: '精神状态:none / depressive / anxiety / psychosis' },
|
|
1122
|
+
},
|
|
1123
|
+
required: ['action'],
|
|
1124
|
+
},
|
|
1125
|
+
output: {
|
|
1126
|
+
schema: {
|
|
1127
|
+
type: 'object',
|
|
1128
|
+
additionalProperties: true,
|
|
1129
|
+
properties: { ok: { type: 'boolean' } },
|
|
1130
|
+
required: ['ok'],
|
|
1131
|
+
},
|
|
1132
|
+
render: (_args, value) => [{ type: 'text', text: value.preview || 'ok' }],
|
|
1133
|
+
},
|
|
1134
|
+
timeoutMs: 10000,
|
|
1135
|
+
isConcurrencySafe: true,
|
|
1136
|
+
async execute(args = {}) {
|
|
1137
|
+
if (args.action === 'push') {
|
|
1138
|
+
const out = engine.push(args.hormone, args.delta)
|
|
1139
|
+
return { ...out, ...view(true) }
|
|
1140
|
+
}
|
|
1141
|
+
if (args.action === 'inject') {
|
|
1142
|
+
const out = engine.inject(args.hormone, args.delta == null ? 20 : args.delta)
|
|
1143
|
+
return { ...out, ...view(true) }
|
|
1144
|
+
}
|
|
1145
|
+
if (args.action === 'drug') {
|
|
1146
|
+
const out = engine.takeDrug(args.drug)
|
|
1147
|
+
return { ...out, ...view(true) }
|
|
1148
|
+
}
|
|
1149
|
+
if (args.action === 'scenario') {
|
|
1150
|
+
const out = engine.applyScenario(args.name)
|
|
1151
|
+
return { ...out, ...view(true) }
|
|
1152
|
+
}
|
|
1153
|
+
if (args.action === 'trigger') {
|
|
1154
|
+
const out = engine.triggerDisease(args.disease)
|
|
1155
|
+
return { ...out, ...view(true) }
|
|
1156
|
+
}
|
|
1157
|
+
if (args.action === 'relief') {
|
|
1158
|
+
const out = engine.reliefDisease(args.disease, args.relief)
|
|
1159
|
+
return { ...out, ...view(true) }
|
|
1160
|
+
}
|
|
1161
|
+
if (args.action === 'cure') {
|
|
1162
|
+
const out = engine.cureDisease(args.disease)
|
|
1163
|
+
return { ...out, ...view(true) }
|
|
1164
|
+
}
|
|
1165
|
+
if (args.action === 'cycle') {
|
|
1166
|
+
const s = engine.state()
|
|
1167
|
+
return { ok: true, cycle: s.cycle, preview: renderStateText(s.items, s.diseases, s.cycle, legacy, s.mood, s.mental) }
|
|
1168
|
+
}
|
|
1169
|
+
if (args.action === 'cycleAdvance') {
|
|
1170
|
+
const out = engine.advanceCycle(args.n == null ? 1 : args.n)
|
|
1171
|
+
return { ...out, ...view(true) }
|
|
1172
|
+
}
|
|
1173
|
+
if (args.action === 'cycleSet') {
|
|
1174
|
+
const out = engine.setCycleDay(args.day)
|
|
1175
|
+
return { ...out, ...view(true) }
|
|
1176
|
+
}
|
|
1177
|
+
if (args.action === 'cycleReset') {
|
|
1178
|
+
const out = engine.resetCycle()
|
|
1179
|
+
return { ...out, ...view(true) }
|
|
1180
|
+
}
|
|
1181
|
+
if (args.action === 'mood') {
|
|
1182
|
+
const out = engine.setMood({ id: args.mood, intensity: args.intensity })
|
|
1183
|
+
return { ...out, ...view(true) }
|
|
1184
|
+
}
|
|
1185
|
+
if (args.action === 'moodClear') {
|
|
1186
|
+
engine.clearMood()
|
|
1187
|
+
return view(true)
|
|
1188
|
+
}
|
|
1189
|
+
if (args.action === 'mental') {
|
|
1190
|
+
const out = engine.setMental({
|
|
1191
|
+
load: args.load,
|
|
1192
|
+
protect: args.protect,
|
|
1193
|
+
sensitization: args.sensitization,
|
|
1194
|
+
state: args.state,
|
|
1195
|
+
})
|
|
1196
|
+
return { ...out, ...view(true) }
|
|
1197
|
+
}
|
|
1198
|
+
if (args.action === 'mentalClear') {
|
|
1199
|
+
engine.clearMental()
|
|
1200
|
+
return view(true)
|
|
1201
|
+
}
|
|
1202
|
+
if (args.action === 'preview') {
|
|
1203
|
+
const s = engine.state()
|
|
1204
|
+
return { ok: true, preview: renderStateText(s.items, s.diseases, s.cycle, legacy, s.mood, s.mental) }
|
|
1205
|
+
}
|
|
1206
|
+
return view(true)
|
|
1207
|
+
},
|
|
1208
|
+
})
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
// 对外 service:给 fatfish-mood 这类情绪插件直接调用,避免它们走 HTTP 猜端口。
|
|
1212
|
+
const api = {
|
|
1213
|
+
version: 1,
|
|
1214
|
+
hormoneIds: HORMONES.map((h) => h.id),
|
|
1215
|
+
getView: () => view(),
|
|
1216
|
+
getState: () => engine.state(),
|
|
1217
|
+
setMood: (m) => engine.setMood(m),
|
|
1218
|
+
clearMood: () => engine.clearMood(),
|
|
1219
|
+
moodInfo: () => engine.moodInfo(),
|
|
1220
|
+
setMental: (m) => engine.setMental(m),
|
|
1221
|
+
clearMental: () => engine.clearMental(),
|
|
1222
|
+
mentalInfo: () => engine.mentalInfo(),
|
|
1223
|
+
}
|
|
1224
|
+
try {
|
|
1225
|
+
if (typeof ctx.provide === 'function') ctx.provide('fatfishEndocrine', api)
|
|
1226
|
+
else if (typeof ctx.set === 'function') ctx.set('fatfishEndocrine', api)
|
|
1227
|
+
} catch {
|
|
1228
|
+
// 宿主不支持 provide/set 时,mood 插件会降级为只写提示词、不改激素
|
|
1229
|
+
}
|
|
1230
|
+
}
|