@linxin666/dsh-pet 0.4.5 → 0.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +2 -2
- package/README.md +30 -3
- package/README.zh.md +30 -3
- package/lib/client.js +178 -62
- package/lib/client.js.map +1 -1
- package/lib/index.js +734 -26
- package/lib/live2d-vendor.js +129 -95
- package/lib/live2d-vendor.js.map +1 -1
- package/lib/types/announce.d.ts +16 -0
- package/lib/types/announce.d.ts.map +1 -1
- package/lib/types/announce.js +16 -0
- package/lib/types/client/PetDockEntry.d.ts +6 -0
- package/lib/types/client/PetDockEntry.d.ts.map +1 -1
- package/lib/types/client/PetDockEntry.js +1 -1
- package/lib/types/client/PetSettingsCard.d.ts +4 -0
- package/lib/types/client/PetSettingsCard.d.ts.map +1 -1
- package/lib/types/client/PetSettingsCard.js +16 -1
- package/lib/types/client/PetSprite.d.ts +7 -0
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +23 -13
- package/lib/types/client/index.d.ts.map +1 -1
- package/lib/types/client/index.js +43 -11
- package/lib/types/client/locales.d.ts +4 -0
- package/lib/types/client/locales.d.ts.map +1 -1
- package/lib/types/client/locales.js +4 -0
- package/lib/types/client/renderers/frames2d.d.ts +5 -3
- package/lib/types/client/renderers/frames2d.d.ts.map +1 -1
- package/lib/types/client/renderers/frames2d.js +139 -33
- package/lib/types/index.d.ts +14 -3
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.js +15 -2
- package/lib/types/panel-actions.d.ts +96 -0
- package/lib/types/panel-actions.d.ts.map +1 -0
- package/lib/types/panel-actions.js +134 -0
- package/lib/types/routes.d.ts.map +1 -1
- package/lib/types/routes.js +10 -0
- package/lib/types/service.d.ts +137 -9
- package/lib/types/service.d.ts.map +1 -1
- package/lib/types/service.js +151 -19
- package/package.json +26 -30
- package/src/announce.ts +17 -0
- package/src/client/PetDockEntry.test.tsx +1 -0
- package/src/client/PetDockEntry.tsx +7 -0
- package/src/client/PetSettingsCard.test.tsx +88 -0
- package/src/client/PetSettingsCard.tsx +31 -0
- package/src/client/PetSprite.test.tsx +197 -0
- package/src/client/PetSprite.tsx +56 -12
- package/src/client/index.test.tsx +74 -4
- package/src/client/index.ts +43 -11
- package/src/client/locales.ts +4 -0
- package/src/client/renderers/frames2d.test.ts +103 -1
- package/src/client/renderers/frames2d.ts +143 -30
- package/src/index.test.ts +22 -0
- package/src/index.ts +28 -2
- package/src/panel-actions.test.ts +130 -0
- package/src/panel-actions.ts +182 -0
- package/src/registry.test.ts +3 -2
- package/src/routes.ts +9 -0
- package/src/service.test.ts +128 -0
- package/src/service.ts +206 -22
- package/lib/invariant.js +0 -34
- package/lib/state-NvDEoCln.js +0 -455
- package/lib/types/invariant.d.ts +0 -10
- package/lib/types/invariant.d.ts.map +0 -1
- package/lib/types/invariant.js +0 -27
- package/src/invariant.ts +0 -40
package/lib/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { _ as RemarkPicker, a as AFFINITY_MAX, c as applyInteraction, d as emptyAffinity, f as rankOf, g as REMARK_LINE_MAX, h as REMARK_LINES_MAX, i as rowOf, l as applyTurnReward, m as REMARK_KINDS, n as animationForPhase, o as AFFINITY_RANKS, p as BUILTIN_REMARKS, r as defaultPetStateConfig, s as affinityViewOf, t as PetStateMachine, u as defaultAffinityConfig, v as builtinRemark, y as normalizePetRemarks } from "./state-NvDEoCln.js";
|
|
2
1
|
import z from "@deepseek-ai/schemastery";
|
|
3
2
|
import { Service } from "@deepseek-ai/cordis";
|
|
4
3
|
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, realpathSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
@@ -18,10 +17,10 @@ const ANNOUNCE_DEFAULT_TTL_MS = 1e4;
|
|
|
18
17
|
*/
|
|
19
18
|
const ANNOUNCE_MAX_TTL_MS = 72e5;
|
|
20
19
|
/** Hard bounds: lengths and the TTL range (1 s .. 60 s). */
|
|
21
|
-
const TITLE_MAX = 80;
|
|
20
|
+
const TITLE_MAX$1 = 80;
|
|
22
21
|
const TEXT_MAX = 120;
|
|
23
22
|
const SOURCE_MAX = 64;
|
|
24
|
-
function boundedString(value, max) {
|
|
23
|
+
function boundedString$1(value, max) {
|
|
25
24
|
if (typeof value !== "string") return void 0;
|
|
26
25
|
const trimmed = value.trim();
|
|
27
26
|
if (trimmed === "") return void 0;
|
|
@@ -37,14 +36,14 @@ function boundedString(value, max) {
|
|
|
37
36
|
function parseAnnouncement(input, now) {
|
|
38
37
|
if (typeof input !== "object" || input === null) return void 0;
|
|
39
38
|
const data = input;
|
|
40
|
-
const source = boundedString(data.source, SOURCE_MAX);
|
|
41
|
-
const title = boundedString(data.title, TITLE_MAX);
|
|
39
|
+
const source = boundedString$1(data.source, SOURCE_MAX);
|
|
40
|
+
const title = boundedString$1(data.title, TITLE_MAX$1);
|
|
42
41
|
if (source === void 0 || title === void 0) return void 0;
|
|
43
42
|
const kind = data.kind === "balance" || data.kind === "cost" || data.kind === "plan" ? data.kind : void 0;
|
|
44
43
|
if (kind === void 0) return void 0;
|
|
45
|
-
const amount = boundedString(data.amount, TEXT_MAX);
|
|
46
|
-
const resetAt = boundedString(data.resetAt, TEXT_MAX);
|
|
47
|
-
const note = boundedString(data.note, TITLE_MAX);
|
|
44
|
+
const amount = boundedString$1(data.amount, TEXT_MAX);
|
|
45
|
+
const resetAt = boundedString$1(data.resetAt, TEXT_MAX);
|
|
46
|
+
const note = boundedString$1(data.note, TITLE_MAX$1);
|
|
48
47
|
const percent = typeof data.percent === "number" && Number.isFinite(data.percent) ? Math.max(0, Math.min(100, data.percent)) : void 0;
|
|
49
48
|
if ((kind === "balance" || kind === "cost") && amount === void 0) return void 0;
|
|
50
49
|
if (kind === "plan" && percent === void 0) return void 0;
|
|
@@ -997,6 +996,362 @@ function projectAssistantStreamFrame(frame, runtime, nowMs = Date.now()) {
|
|
|
997
996
|
}
|
|
998
997
|
}
|
|
999
998
|
//#endregion
|
|
999
|
+
//#region src/remarks.ts
|
|
1000
|
+
/** Every remark slot, in a stable order. */
|
|
1001
|
+
const REMARK_KINDS = [
|
|
1002
|
+
"pet",
|
|
1003
|
+
"petCooldown",
|
|
1004
|
+
"feed",
|
|
1005
|
+
"feedCooldown",
|
|
1006
|
+
"noTreats"
|
|
1007
|
+
];
|
|
1008
|
+
/** Longest accepted reaction line (characters, trimmed before slicing). */
|
|
1009
|
+
const REMARK_LINE_MAX = 120;
|
|
1010
|
+
/** Longest accepted pool per slot. */
|
|
1011
|
+
const REMARK_LINES_MAX = 64;
|
|
1012
|
+
/**
|
|
1013
|
+
* Built-in default remark library. Every pool is plain zh copy in the
|
|
1014
|
+
* whale-girl voice; the first line of each pool is the legacy reaction the
|
|
1015
|
+
* plugin has always spoken, so existing installs and tests keep their
|
|
1016
|
+
* wording while the pool adds variety. No emoji characters anywhere (the
|
|
1017
|
+
* repo bans them); ~ is the whale-girl's signature.
|
|
1018
|
+
*/
|
|
1019
|
+
const BUILTIN_REMARKS = {
|
|
1020
|
+
pet: [
|
|
1021
|
+
"咕噜咕噜~被摸摸好舒服!",
|
|
1022
|
+
"再摸摸这里,痒痒的~",
|
|
1023
|
+
"头顶温度刚刚好,安心~",
|
|
1024
|
+
"被摸到耳朵啦,扑通扑通!",
|
|
1025
|
+
"你的手掌好温暖,舍不得你走~",
|
|
1026
|
+
"呼噜呼噜~就靠在这里不走了!",
|
|
1027
|
+
"今天的摸头也收货成功!",
|
|
1028
|
+
"蹭蹭你的手心,这是回礼~",
|
|
1029
|
+
"多摸摸我,亲密度会涨哦!",
|
|
1030
|
+
"闭眼享受中,请勿打扰~",
|
|
1031
|
+
"头再低一点,够不着了~",
|
|
1032
|
+
"呼噜呼噜,声音都冒出来了",
|
|
1033
|
+
"这手感,比小鱼干还上瘾",
|
|
1034
|
+
"摸到第三下,满意",
|
|
1035
|
+
"耳朵后面,别漏了……啊,舒服",
|
|
1036
|
+
"被摸得尾巴都卷起来了"
|
|
1037
|
+
],
|
|
1038
|
+
petCooldown: [
|
|
1039
|
+
"摸过头啦,让鲸鱼娘歇口气~",
|
|
1040
|
+
"羽毛都快被摸秃啦,缓一缓~",
|
|
1041
|
+
"呼……先让我喘口气嘛!",
|
|
1042
|
+
"再摸就要睡着了哦~",
|
|
1043
|
+
"稍微休息一下,待会儿再摸~",
|
|
1044
|
+
"头顶要冒烟啦,停一停!",
|
|
1045
|
+
"我知道你喜欢我,但也要节制呀~",
|
|
1046
|
+
"歇一歇,摸摸的手感会更好哦~",
|
|
1047
|
+
"咕……等我回个蓝~",
|
|
1048
|
+
"让我先消化一下刚才的爱!",
|
|
1049
|
+
"痒痒的,先让我缓一下……",
|
|
1050
|
+
"再摸就掉线了,真的",
|
|
1051
|
+
"库存的呼噜声用完了",
|
|
1052
|
+
"手歇会儿,我也要补个蓝",
|
|
1053
|
+
"舒服归舒服,得缓缓呀",
|
|
1054
|
+
"再摸下去,我就要融化了"
|
|
1055
|
+
],
|
|
1056
|
+
feed: [
|
|
1057
|
+
"呜哇!小鱼干好好吃!",
|
|
1058
|
+
"咔嚓咔嚓,美味到尾巴打结~",
|
|
1059
|
+
"这条小鱼干是刚晒好的,好香!",
|
|
1060
|
+
"谢谢你,胃里暖暖的~",
|
|
1061
|
+
"囤粮 +1,今天也有好好被爱!",
|
|
1062
|
+
"好吃到想转圈圈~",
|
|
1063
|
+
"小鱼干最好吃了,再来亿条!",
|
|
1064
|
+
"饱餐一顿,马上满血复活~",
|
|
1065
|
+
"这个味道,是幸福的味道!",
|
|
1066
|
+
"吃完了还不忘舔舔爪子~",
|
|
1067
|
+
"这小鱼干,是今天的顶配",
|
|
1068
|
+
"一口下去,精神头全回来了",
|
|
1069
|
+
"脆!香!就是这个味儿",
|
|
1070
|
+
"边吃边摇尾巴,形象不要了",
|
|
1071
|
+
"好吃到眼睛都眯起来了",
|
|
1072
|
+
"这块鱼干我记住了,懂我的"
|
|
1073
|
+
],
|
|
1074
|
+
feedCooldown: [
|
|
1075
|
+
"吃饱啦,晚点再喂~",
|
|
1076
|
+
"肚子圆滚滚的,装不下啦~",
|
|
1077
|
+
"再喂就要变成球啦!",
|
|
1078
|
+
"让我慢慢消化这份心意~",
|
|
1079
|
+
"小鱼干的香气还没散呢~",
|
|
1080
|
+
"呼……满足得动不了了~",
|
|
1081
|
+
"先散步一圈再吃下一顿!",
|
|
1082
|
+
"肚皮已经鼓鼓的啦~",
|
|
1083
|
+
"好吃是好吃,可也得节制呀~",
|
|
1084
|
+
"等我饿了会告诉你哦~",
|
|
1085
|
+
"胃说它满了,脑子说还能吃",
|
|
1086
|
+
"这条得留着慢慢品",
|
|
1087
|
+
"先消消食,待会儿再战",
|
|
1088
|
+
"塞不下了,真塞不下了",
|
|
1089
|
+
"闻着香,可惜没地方放了",
|
|
1090
|
+
"嗝……这顿值了"
|
|
1091
|
+
],
|
|
1092
|
+
noTreats: [
|
|
1093
|
+
"没有小鱼干了,多陪我工作一会儿吧~",
|
|
1094
|
+
"粮仓空空,陪我完成几轮任务就会有小鱼干啦~",
|
|
1095
|
+
"小鱼干在路上啦,先一起加油工作!",
|
|
1096
|
+
"嘴巴寂寞了……快去完成一轮任务!",
|
|
1097
|
+
"陪我多工作一会儿,鱼干自动到账~",
|
|
1098
|
+
"现在喂我也只会饿着肚子说谢谢哦~",
|
|
1099
|
+
"粮仓见底啦,用几轮任务换一条鱼干吧~",
|
|
1100
|
+
"饿着肚子等你完成下一轮任务~",
|
|
1101
|
+
"小鱼干藏在你的工作里,去找找看!",
|
|
1102
|
+
"先工作后干饭,我们的约定哦~",
|
|
1103
|
+
"粮仓见底,全靠感情撑着了",
|
|
1104
|
+
"饿是真饿,活也是真得干",
|
|
1105
|
+
"画饼充饥……不对,画鱼干充饥",
|
|
1106
|
+
"没鱼干的日子,靠意志力过",
|
|
1107
|
+
"下一轮任务,我闻到了鱼干味",
|
|
1108
|
+
"先记账上,欠我两条,记住啦"
|
|
1109
|
+
]
|
|
1110
|
+
};
|
|
1111
|
+
/** The legacy first line of one kind (direct callers' fallback copy). */
|
|
1112
|
+
function builtinRemark(kind) {
|
|
1113
|
+
return BUILTIN_REMARKS[kind][0];
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Normalize a manifest 'remarks' block into per-kind line pools. Unknown
|
|
1117
|
+
* slots and non-string entries are skipped with a warning; empty pools are
|
|
1118
|
+
* dropped so the built-in library takes the slot. Returns undefined when no
|
|
1119
|
+
* usable slot remains.
|
|
1120
|
+
*/
|
|
1121
|
+
function normalizePetRemarks(raw, onWarning = () => {}) {
|
|
1122
|
+
if (raw === void 0) return void 0;
|
|
1123
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
1124
|
+
onWarning("remarks must be an object with pet/petCooldown/feed/feedCooldown/noTreats slots");
|
|
1125
|
+
return;
|
|
1126
|
+
}
|
|
1127
|
+
const remarks = {};
|
|
1128
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
1129
|
+
if (!REMARK_KINDS.includes(key)) {
|
|
1130
|
+
onWarning("unknown remarks slot " + JSON.stringify(key));
|
|
1131
|
+
continue;
|
|
1132
|
+
}
|
|
1133
|
+
const lines = (Array.isArray(value) ? value : [value]).filter((line) => typeof line === "string").map((line) => line.trim()).filter((line) => line !== "").slice(0, 64).map((line) => line.slice(0, 120));
|
|
1134
|
+
if (lines.length === 0) {
|
|
1135
|
+
onWarning("remarks slot " + key + " carries no usable lines");
|
|
1136
|
+
continue;
|
|
1137
|
+
}
|
|
1138
|
+
remarks[key] = lines;
|
|
1139
|
+
}
|
|
1140
|
+
return Object.keys(remarks).length === 0 ? void 0 : remarks;
|
|
1141
|
+
}
|
|
1142
|
+
/**
|
|
1143
|
+
* Round-robin reaction picker over the effective pools (per-pet custom lines
|
|
1144
|
+
* override the built-in pool per slot). Counters are per slot, so each slot
|
|
1145
|
+
* cycles its own list independently and picks stay deterministic for tests.
|
|
1146
|
+
*/
|
|
1147
|
+
var RemarkPicker = class {
|
|
1148
|
+
counters = /* @__PURE__ */ new Map();
|
|
1149
|
+
pools;
|
|
1150
|
+
constructor(overrides, voiceOverrides) {
|
|
1151
|
+
this.pools = {};
|
|
1152
|
+
for (const kind of REMARK_KINDS) {
|
|
1153
|
+
const custom = overrides?.[kind];
|
|
1154
|
+
const voice = voiceOverrides?.[kind];
|
|
1155
|
+
if (custom !== void 0 && custom.length > 0) this.pools[kind] = custom;
|
|
1156
|
+
else if (voice !== void 0 && voice.length > 0) this.pools[kind] = voice;
|
|
1157
|
+
else this.pools[kind] = BUILTIN_REMARKS[kind];
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
/** The effective pool for one slot (custom override or built-in). */
|
|
1161
|
+
pool(kind) {
|
|
1162
|
+
return this.pools[kind];
|
|
1163
|
+
}
|
|
1164
|
+
/** The next line for one slot (round-robin within its pool). */
|
|
1165
|
+
pick(kind) {
|
|
1166
|
+
const pool = this.pools[kind];
|
|
1167
|
+
const index = (this.counters.get(kind) ?? 0) % pool.length;
|
|
1168
|
+
this.counters.set(kind, index + 1);
|
|
1169
|
+
return pool[index];
|
|
1170
|
+
}
|
|
1171
|
+
/** Select a line from a stable external counter without changing local picker state. */
|
|
1172
|
+
pickAt(kind, count) {
|
|
1173
|
+
const pool = this.pools[kind];
|
|
1174
|
+
return pool[Math.max(0, Math.floor(count)) % pool.length];
|
|
1175
|
+
}
|
|
1176
|
+
};
|
|
1177
|
+
//#endregion
|
|
1178
|
+
//#region src/affinity.ts
|
|
1179
|
+
/**
|
|
1180
|
+
* Affinity score — pure, clock-injected. The pet grows closer the more you
|
|
1181
|
+
* work together and care for it: every completed turn earns a small reward,
|
|
1182
|
+
* petting earns a tiny one (cooldown-gated), feeding earns the most.
|
|
1183
|
+
* Persistence lives in the service; this module only computes transitions.
|
|
1184
|
+
* Reaction copy resolves from the shared remark library (first line of each
|
|
1185
|
+
* built-in pool); the ledger layers per-pet custom remarks on top.
|
|
1186
|
+
* @module @linxin666/dsh-pet/affinity
|
|
1187
|
+
*/
|
|
1188
|
+
/**
|
|
1189
|
+
* Affinity points cap. Historically 100; removed so long-term companions
|
|
1190
|
+
* keep growing — the default limit is now the full 999,999,999 range.
|
|
1191
|
+
*/
|
|
1192
|
+
const AFFINITY_MAX = 999999999;
|
|
1193
|
+
/** Affinity ranks by points; the pet visibly grows with its rank.
|
|
1194
|
+
* The original four tiers (0/25/50/80) are unchanged; higher tiers reach
|
|
1195
|
+
* into the extended cap so the ladder stays meaningful for veteran
|
|
1196
|
+
* companions. Marker glyphs are plain ASCII (the repo bans all emoji
|
|
1197
|
+
* characters); they read as a growing star trail alongside the rank name. */
|
|
1198
|
+
const AFFINITY_RANKS = [
|
|
1199
|
+
{
|
|
1200
|
+
min: 0,
|
|
1201
|
+
name: "幼鲸",
|
|
1202
|
+
emoji: "*"
|
|
1203
|
+
},
|
|
1204
|
+
{
|
|
1205
|
+
min: 25,
|
|
1206
|
+
name: "伙伴",
|
|
1207
|
+
emoji: "**"
|
|
1208
|
+
},
|
|
1209
|
+
{
|
|
1210
|
+
min: 50,
|
|
1211
|
+
name: "挚友",
|
|
1212
|
+
emoji: "***"
|
|
1213
|
+
},
|
|
1214
|
+
{
|
|
1215
|
+
min: 80,
|
|
1216
|
+
name: "深海羁绊",
|
|
1217
|
+
emoji: "****"
|
|
1218
|
+
},
|
|
1219
|
+
{
|
|
1220
|
+
min: 200,
|
|
1221
|
+
name: "心有灵犀",
|
|
1222
|
+
emoji: "*****"
|
|
1223
|
+
},
|
|
1224
|
+
{
|
|
1225
|
+
min: 500,
|
|
1226
|
+
name: "传说羁绊",
|
|
1227
|
+
emoji: "******"
|
|
1228
|
+
},
|
|
1229
|
+
{
|
|
1230
|
+
min: 2e3,
|
|
1231
|
+
name: "神话羁绊",
|
|
1232
|
+
emoji: "*******"
|
|
1233
|
+
},
|
|
1234
|
+
{
|
|
1235
|
+
min: 1e4,
|
|
1236
|
+
name: "永恒之契",
|
|
1237
|
+
emoji: "********"
|
|
1238
|
+
},
|
|
1239
|
+
{
|
|
1240
|
+
min: 1e5,
|
|
1241
|
+
name: "鲸生共渡",
|
|
1242
|
+
emoji: "*********"
|
|
1243
|
+
}
|
|
1244
|
+
];
|
|
1245
|
+
const defaultAffinityConfig = {
|
|
1246
|
+
turnReward: 1,
|
|
1247
|
+
petReward: 1,
|
|
1248
|
+
petCooldownMs: 1e4,
|
|
1249
|
+
feedReward: 5,
|
|
1250
|
+
feedCooldownMs: 3e4
|
|
1251
|
+
};
|
|
1252
|
+
function emptyAffinity() {
|
|
1253
|
+
return {
|
|
1254
|
+
points: 0,
|
|
1255
|
+
lastPetAt: 0,
|
|
1256
|
+
lastFeedAt: 0,
|
|
1257
|
+
pets: 0,
|
|
1258
|
+
feeds: 0,
|
|
1259
|
+
petRejects: 0,
|
|
1260
|
+
feedRejects: 0,
|
|
1261
|
+
turns: 0
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
/** Rank for a point total. */
|
|
1265
|
+
function rankOf(points) {
|
|
1266
|
+
let rank = AFFINITY_RANKS[0];
|
|
1267
|
+
for (const candidate of AFFINITY_RANKS) if (points >= candidate.min) rank = candidate;
|
|
1268
|
+
return rank;
|
|
1269
|
+
}
|
|
1270
|
+
/** Derive the read-only view of one affinity state at a wall-clock instant. */
|
|
1271
|
+
function affinityViewOf(state, nowMs, config = defaultAffinityConfig) {
|
|
1272
|
+
const rank = rankOf(state.points);
|
|
1273
|
+
return {
|
|
1274
|
+
points: state.points,
|
|
1275
|
+
rank: rank.name,
|
|
1276
|
+
rankEmoji: rank.emoji,
|
|
1277
|
+
pets: state.pets,
|
|
1278
|
+
feeds: state.feeds,
|
|
1279
|
+
turns: state.turns,
|
|
1280
|
+
petCooldown: nowMs - state.lastPetAt < config.petCooldownMs,
|
|
1281
|
+
feedCooldown: nowMs - state.lastFeedAt < config.feedCooldownMs
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
function clamp$1(points) {
|
|
1285
|
+
return Math.min(AFFINITY_MAX, Math.max(0, points));
|
|
1286
|
+
}
|
|
1287
|
+
/** Built-in reaction selected deterministically from a persisted counter. */
|
|
1288
|
+
function countedRemark(kind, count) {
|
|
1289
|
+
const pool = BUILTIN_REMARKS[kind];
|
|
1290
|
+
return pool[Math.max(0, Math.floor(count)) % pool.length] ?? builtinRemark(kind);
|
|
1291
|
+
}
|
|
1292
|
+
/**
|
|
1293
|
+
* Apply one interaction to a copy of the state (immutable style: returns a
|
|
1294
|
+
* new object; the caller replaces the persisted state). Cooldowns only
|
|
1295
|
+
* apply once the pet has been interacted with at least once (last*At === 0
|
|
1296
|
+
* means "never", so the first pet/feed always lands).
|
|
1297
|
+
*/
|
|
1298
|
+
function applyInteraction(state, kind, nowMs, config = defaultAffinityConfig) {
|
|
1299
|
+
const next = { ...state };
|
|
1300
|
+
if (kind === "pet") {
|
|
1301
|
+
if (state.lastPetAt !== 0 && nowMs - state.lastPetAt < config.petCooldownMs) {
|
|
1302
|
+
next.petRejects += 1;
|
|
1303
|
+
return {
|
|
1304
|
+
affinity: next,
|
|
1305
|
+
delta: 0,
|
|
1306
|
+
reaction: countedRemark("petCooldown", state.petRejects),
|
|
1307
|
+
accepted: false
|
|
1308
|
+
};
|
|
1309
|
+
}
|
|
1310
|
+
next.lastPetAt = nowMs;
|
|
1311
|
+
next.pets += 1;
|
|
1312
|
+
next.points = clamp$1(state.points + config.petReward);
|
|
1313
|
+
return {
|
|
1314
|
+
affinity: next,
|
|
1315
|
+
delta: config.petReward,
|
|
1316
|
+
reaction: countedRemark("pet", state.pets),
|
|
1317
|
+
accepted: true
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
if (kind === "feed") {
|
|
1321
|
+
if (state.lastFeedAt !== 0 && nowMs - state.lastFeedAt < config.feedCooldownMs) {
|
|
1322
|
+
next.feedRejects += 1;
|
|
1323
|
+
return {
|
|
1324
|
+
affinity: next,
|
|
1325
|
+
delta: 0,
|
|
1326
|
+
reaction: countedRemark("feedCooldown", state.feedRejects),
|
|
1327
|
+
accepted: false
|
|
1328
|
+
};
|
|
1329
|
+
}
|
|
1330
|
+
next.lastFeedAt = nowMs;
|
|
1331
|
+
next.feeds += 1;
|
|
1332
|
+
next.points = clamp$1(state.points + config.feedReward);
|
|
1333
|
+
return {
|
|
1334
|
+
affinity: next,
|
|
1335
|
+
delta: config.feedReward,
|
|
1336
|
+
reaction: countedRemark("feed", state.feeds),
|
|
1337
|
+
accepted: true
|
|
1338
|
+
};
|
|
1339
|
+
}
|
|
1340
|
+
return {
|
|
1341
|
+
affinity: state,
|
|
1342
|
+
delta: 0,
|
|
1343
|
+
reaction: "",
|
|
1344
|
+
accepted: false
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
/** Reward one completed turn (called by the host on `done`). */
|
|
1348
|
+
function applyTurnReward(state, config = defaultAffinityConfig) {
|
|
1349
|
+
const next = { ...state };
|
|
1350
|
+
next.turns += 1;
|
|
1351
|
+
next.points = clamp$1(state.points + config.turnReward);
|
|
1352
|
+
return next;
|
|
1353
|
+
}
|
|
1354
|
+
//#endregion
|
|
1000
1355
|
//#region src/treats.ts
|
|
1001
1356
|
const defaultTreatConfig = {
|
|
1002
1357
|
turnsPerTreat: 30,
|
|
@@ -1338,6 +1693,127 @@ var PetLedger = class {
|
|
|
1338
1693
|
}
|
|
1339
1694
|
};
|
|
1340
1695
|
//#endregion
|
|
1696
|
+
//#region src/panel-actions.ts
|
|
1697
|
+
/**
|
|
1698
|
+
* Hover-panel action contract — the extension point a sibling plugin uses to
|
|
1699
|
+
* add its own button to the pet's floating hover panel (issue #6).
|
|
1700
|
+
*
|
|
1701
|
+
* A plugin registers one action in process through the pet service
|
|
1702
|
+
* (`ctx.pet.registerPanelAction({ id, label, onSelect })`); the host validates
|
|
1703
|
+
* it into a bounded view, serves the registered actions in the state snapshot
|
|
1704
|
+
* (`PetStateView.panelActions`), and the browser half renders one button per
|
|
1705
|
+
* entry at the end of the panel's action row. A click travels back through
|
|
1706
|
+
* `POST /api/pet/panel-action`, which dispatches to the registering plugin's
|
|
1707
|
+
* own callback — the pet owns the row, the plugin owns what the button does.
|
|
1708
|
+
*
|
|
1709
|
+
* The validation and the registry live in this pure module so the contract has
|
|
1710
|
+
* exactly one home and stays testable without the cordis service.
|
|
1711
|
+
* @module @linxin666/dsh-pet/panel-actions
|
|
1712
|
+
*/
|
|
1713
|
+
/** How many plugin actions one hover panel can carry at most. */
|
|
1714
|
+
const MAX_PANEL_ACTIONS = 8;
|
|
1715
|
+
/**
|
|
1716
|
+
* Action id grammar: a lowercase id in the shape pet and decoration ids use
|
|
1717
|
+
* (`^` + kebab/dot segments). The id is what a click travels back on, so it
|
|
1718
|
+
* has to be stable, addressable and free of separators the wire would treat as
|
|
1719
|
+
* structure.
|
|
1720
|
+
*/
|
|
1721
|
+
const ID_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
1722
|
+
/**
|
|
1723
|
+
* Label bound. The action row is compact and wraps beyond a few characters per
|
|
1724
|
+
* button, so a plugin names its action (换装, Quota) rather than a sentence;
|
|
1725
|
+
* the optional title carries the longer explanation as a tooltip.
|
|
1726
|
+
*/
|
|
1727
|
+
const LABEL_MAX = 24;
|
|
1728
|
+
/** Title (tooltip) bound; the same reason voice-pack labels are bounded. */
|
|
1729
|
+
const TITLE_MAX = 80;
|
|
1730
|
+
function boundedString(value, max) {
|
|
1731
|
+
if (typeof value !== "string") return void 0;
|
|
1732
|
+
const trimmed = value.trim();
|
|
1733
|
+
if (trimmed === "") return void 0;
|
|
1734
|
+
return trimmed.length <= max ? trimmed : trimmed.slice(0, max);
|
|
1735
|
+
}
|
|
1736
|
+
/**
|
|
1737
|
+
* Validate one registration. Returns undefined — never throws — when the shape
|
|
1738
|
+
* is unusable: the id must match the id grammar and the label must survive
|
|
1739
|
+
* truncation, so a click can always be dispatched back to a known row.
|
|
1740
|
+
* @param input - the registration a sibling plugin passed to the service.
|
|
1741
|
+
* @returns the wire view, or undefined when the registration is malformed.
|
|
1742
|
+
*/
|
|
1743
|
+
function normalizePanelAction(input) {
|
|
1744
|
+
if (typeof input !== "object" || input === null) return void 0;
|
|
1745
|
+
const data = input;
|
|
1746
|
+
const id = typeof data.id === "string" ? data.id.trim() : void 0;
|
|
1747
|
+
if (id === void 0 || !ID_PATTERN.test(id)) return void 0;
|
|
1748
|
+
const label = boundedString(data.label, LABEL_MAX);
|
|
1749
|
+
if (label === void 0) return void 0;
|
|
1750
|
+
const title = boundedString(data.title, TITLE_MAX);
|
|
1751
|
+
const order = typeof data.order === "number" && Number.isFinite(data.order) ? data.order : 0;
|
|
1752
|
+
return {
|
|
1753
|
+
id,
|
|
1754
|
+
label,
|
|
1755
|
+
...title === void 0 ? {} : { title },
|
|
1756
|
+
order
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
/**
|
|
1760
|
+
* The panel's action registry: one bounded, ordered table of plugin actions.
|
|
1761
|
+
*
|
|
1762
|
+
* Registration is id-wins (a later registration of the same id replaces the
|
|
1763
|
+
* earlier one, and the superseded disposer stops touching the table so a
|
|
1764
|
+
* disposing plugin can never remove the action that replaced it). The table is
|
|
1765
|
+
* in-memory only — it dies with the pet service fiber — and every entry is
|
|
1766
|
+
* reclaimed by the disposer the registering plugin runs with its own fiber.
|
|
1767
|
+
*/
|
|
1768
|
+
var PanelActionRegistry = class {
|
|
1769
|
+
actions = /* @__PURE__ */ new Map();
|
|
1770
|
+
/**
|
|
1771
|
+
* Register one action.
|
|
1772
|
+
* @param input - the registration a sibling plugin passed to the service.
|
|
1773
|
+
* @returns a disposer removing this registration.
|
|
1774
|
+
* @throws when the registration is malformed. Unlike `announce` — a hot path
|
|
1775
|
+
* whose payload a caller may build from untrusted data — a registration is
|
|
1776
|
+
* built once at the registering plugin's own apply, so a bad id or label is a
|
|
1777
|
+
* wiring mistake that should fail where it was written.
|
|
1778
|
+
*/
|
|
1779
|
+
register(input) {
|
|
1780
|
+
const registration = typeof input === "object" && input !== null ? input : {};
|
|
1781
|
+
const view = normalizePanelAction(registration);
|
|
1782
|
+
if (view === void 0) throw new Error("[dsh-pet] invalid panel action: id and label are required");
|
|
1783
|
+
if (typeof registration.onSelect !== "function") throw new Error("[dsh-pet] invalid panel action: onSelect must be a function");
|
|
1784
|
+
if (!this.actions.has(view.id) && this.actions.size >= 8) throw new Error("[dsh-pet] panel action limit reached: 8");
|
|
1785
|
+
const entry = {
|
|
1786
|
+
view,
|
|
1787
|
+
onSelect: registration.onSelect
|
|
1788
|
+
};
|
|
1789
|
+
this.actions.set(view.id, entry);
|
|
1790
|
+
return () => {
|
|
1791
|
+
if (this.actions.get(view.id) === entry) this.actions.delete(view.id);
|
|
1792
|
+
};
|
|
1793
|
+
}
|
|
1794
|
+
/** The registered actions as the state view serves them, in render order. */
|
|
1795
|
+
views() {
|
|
1796
|
+
return [...this.actions.values()].map((entry) => entry.view).sort((a, b) => a.order - b.order || a.id.localeCompare(b.id));
|
|
1797
|
+
}
|
|
1798
|
+
/**
|
|
1799
|
+
* Dispatch a click to the plugin that registered this id.
|
|
1800
|
+
* @param id - the id the browser half reported.
|
|
1801
|
+
* @param context - what the pet looked like when the click arrived.
|
|
1802
|
+
* @returns whether the id was registered. A handler that throws rejects:
|
|
1803
|
+
* the caller decides how a sibling plugin's failure is reported.
|
|
1804
|
+
*/
|
|
1805
|
+
async select(id, context) {
|
|
1806
|
+
const entry = this.actions.get(id);
|
|
1807
|
+
if (entry === void 0) return false;
|
|
1808
|
+
await entry.onSelect(context);
|
|
1809
|
+
return true;
|
|
1810
|
+
}
|
|
1811
|
+
/** Drop every registration (service teardown; tests). */
|
|
1812
|
+
clear() {
|
|
1813
|
+
this.actions.clear();
|
|
1814
|
+
}
|
|
1815
|
+
};
|
|
1816
|
+
//#endregion
|
|
1341
1817
|
//#region src/dsh-home.ts
|
|
1342
1818
|
/**
|
|
1343
1819
|
* DSH_HOME resolution shared by the plugin family's Host halves: the
|
|
@@ -4581,6 +5057,104 @@ function petEntryView(entry, globalVoice) {
|
|
|
4581
5057
|
};
|
|
4582
5058
|
}
|
|
4583
5059
|
//#endregion
|
|
5060
|
+
//#region src/state.ts
|
|
5061
|
+
const defaultPetStateConfig = {
|
|
5062
|
+
celebrateMs: 2400,
|
|
5063
|
+
failureMs: 2400
|
|
5064
|
+
};
|
|
5065
|
+
/**
|
|
5066
|
+
* Map one activity phase onto the animation contract.
|
|
5067
|
+
* - thinking → `running` and tool → `running-right` (focused work).
|
|
5068
|
+
* - review → `review` while answer text is streaming.
|
|
5069
|
+
* - waiting → `waiting` (expectant pose, needs user input).
|
|
5070
|
+
* - done → `jumping` (celebration), then back to `idle` after the window.
|
|
5071
|
+
* - failed → `failed` briefly, then back to `idle`.
|
|
5072
|
+
* - idle → `idle` (calm breathing loop).
|
|
5073
|
+
*/
|
|
5074
|
+
function animationForPhase(phase) {
|
|
5075
|
+
switch (phase) {
|
|
5076
|
+
case "thinking": return "running";
|
|
5077
|
+
case "tool": return "running-right";
|
|
5078
|
+
case "review": return "review";
|
|
5079
|
+
case "waiting": return "waiting";
|
|
5080
|
+
case "done": return "jumping";
|
|
5081
|
+
case "failed": return "failed";
|
|
5082
|
+
case "idle": return "idle";
|
|
5083
|
+
}
|
|
5084
|
+
}
|
|
5085
|
+
/** The spritesheet row index for one animation track. */
|
|
5086
|
+
function rowOf(animation) {
|
|
5087
|
+
return {
|
|
5088
|
+
"idle": 0,
|
|
5089
|
+
"running-right": 1,
|
|
5090
|
+
"running-left": 2,
|
|
5091
|
+
"waving": 3,
|
|
5092
|
+
"jumping": 4,
|
|
5093
|
+
"failed": 5,
|
|
5094
|
+
"waiting": 6,
|
|
5095
|
+
"running": 7,
|
|
5096
|
+
"review": 8
|
|
5097
|
+
}[animation];
|
|
5098
|
+
}
|
|
5099
|
+
/**
|
|
5100
|
+
* PetStateMachine — one instance per host process. Holds only the latest
|
|
5101
|
+
* input snapshot and terminal-state timing; no storage, no side effects.
|
|
5102
|
+
*/
|
|
5103
|
+
var PetStateMachine = class {
|
|
5104
|
+
now;
|
|
5105
|
+
phase = "idle";
|
|
5106
|
+
line;
|
|
5107
|
+
phrase;
|
|
5108
|
+
sessionActive = false;
|
|
5109
|
+
doneAt;
|
|
5110
|
+
failedAt;
|
|
5111
|
+
config;
|
|
5112
|
+
constructor(config = defaultPetStateConfig, now = Date.now) {
|
|
5113
|
+
this.now = now;
|
|
5114
|
+
this.config = {
|
|
5115
|
+
...defaultPetStateConfig,
|
|
5116
|
+
...config
|
|
5117
|
+
};
|
|
5118
|
+
}
|
|
5119
|
+
/** Consume one projected activity update. */
|
|
5120
|
+
onActivityStatus(input) {
|
|
5121
|
+
this.phase = input.phase;
|
|
5122
|
+
this.line = input.line;
|
|
5123
|
+
this.phrase = input.phrase;
|
|
5124
|
+
this.doneAt = input.phase === "done" ? this.now() : void 0;
|
|
5125
|
+
this.failedAt = input.phase === "failed" ? this.now() : void 0;
|
|
5126
|
+
}
|
|
5127
|
+
/** A session became the active one (or a fresh session started). */
|
|
5128
|
+
onSessionActive() {
|
|
5129
|
+
this.sessionActive = true;
|
|
5130
|
+
}
|
|
5131
|
+
/** The active session was disposed (or none left). */
|
|
5132
|
+
onSessionDisposed() {
|
|
5133
|
+
this.sessionActive = false;
|
|
5134
|
+
this.phase = "idle";
|
|
5135
|
+
this.line = void 0;
|
|
5136
|
+
this.phrase = void 0;
|
|
5137
|
+
this.doneAt = void 0;
|
|
5138
|
+
this.failedAt = void 0;
|
|
5139
|
+
}
|
|
5140
|
+
/** Render the current animation decision. */
|
|
5141
|
+
render() {
|
|
5142
|
+
const nowMs = this.now();
|
|
5143
|
+
let animation = animationForPhase(this.phase);
|
|
5144
|
+
const doneSettled = this.phase === "done" && this.doneAt !== void 0 && nowMs - this.doneAt >= this.config.celebrateMs;
|
|
5145
|
+
const failedSettled = this.phase === "failed" && this.failedAt !== void 0 && nowMs - this.failedAt >= this.config.failureMs;
|
|
5146
|
+
if (doneSettled || failedSettled) animation = "idle";
|
|
5147
|
+
const bubble = this.phase === "idle" || doneSettled || failedSettled ? void 0 : this.phrase ?? this.line;
|
|
5148
|
+
return {
|
|
5149
|
+
animation,
|
|
5150
|
+
...bubble === void 0 ? {} : { bubble },
|
|
5151
|
+
animationStartedAt: nowMs,
|
|
5152
|
+
phase: this.phase,
|
|
5153
|
+
sessionActive: this.sessionActive
|
|
5154
|
+
};
|
|
5155
|
+
}
|
|
5156
|
+
};
|
|
5157
|
+
//#endregion
|
|
4584
5158
|
//#region src/service.ts
|
|
4585
5159
|
/**
|
|
4586
5160
|
* Pet host service — the `pet.*` RPC domain. A composition facade: it wires
|
|
@@ -4597,6 +5171,18 @@ function petEntryView(entry, globalVoice) {
|
|
|
4597
5171
|
*/
|
|
4598
5172
|
/** The unified gameplay currency: the shared treat (小鱼干) ledger. */
|
|
4599
5173
|
const GAMEPLAY_TREATS_CURRENCY = "treats";
|
|
5174
|
+
/** Every accepted {@link PetStatusBubbleMode}, in settings-card order. */
|
|
5175
|
+
const STATUS_BUBBLE_MODES = ["auto", "off"];
|
|
5176
|
+
/** The shipped default; the switch changes nothing until a user turns it off. */
|
|
5177
|
+
const DEFAULT_STATUS_BUBBLE_MODE = "auto";
|
|
5178
|
+
/**
|
|
5179
|
+
* Normalize a stored, live or hand-edited bubble mode.
|
|
5180
|
+
* @param value - the value the settings document carries, when any.
|
|
5181
|
+
* @returns the effective mode; anything but 'off' renders the bubbles.
|
|
5182
|
+
*/
|
|
5183
|
+
function statusBubbleMode(value) {
|
|
5184
|
+
return value === "off" ? "off" : DEFAULT_STATUS_BUBBLE_MODE;
|
|
5185
|
+
}
|
|
4600
5186
|
/** Hard cap on simultaneously displayed session bubbles (most recent first). */
|
|
4601
5187
|
const MAX_SESSION_BUBBLES = 12;
|
|
4602
5188
|
/**
|
|
@@ -4615,8 +5201,17 @@ var PetService = class extends Service {
|
|
|
4615
5201
|
enabled;
|
|
4616
5202
|
/** Status-decoration master switch (M5, #567); mirrored from settings. */
|
|
4617
5203
|
decorationEnabled;
|
|
4618
|
-
/**
|
|
4619
|
-
|
|
5204
|
+
/** Whether the pet renders its own session/status bubbles (issue #6). */
|
|
5205
|
+
statusBubbles;
|
|
5206
|
+
/**
|
|
5207
|
+
* Plugin-authored announcements, one slot per `source` (issue #1812). The
|
|
5208
|
+
* contract started as a single slot, where a second announcing plugin
|
|
5209
|
+
* displaced the first; the map is the upgrade the design note predicted for
|
|
5210
|
+
* exactly this moment. Insertion order is the render order and re-announcing
|
|
5211
|
+
* a source updates its entry in place, so two publishers on different
|
|
5212
|
+
* cadences never swap positions.
|
|
5213
|
+
*/
|
|
5214
|
+
announcements = /* @__PURE__ */ new Map();
|
|
4620
5215
|
disposeActivity;
|
|
4621
5216
|
/** Session whose most recent meaningful event currently drives the global pet. */
|
|
4622
5217
|
displaySession;
|
|
@@ -4633,6 +5228,13 @@ var PetService = class extends Service {
|
|
|
4633
5228
|
*/
|
|
4634
5229
|
sessionActivity = /* @__PURE__ */ new Map();
|
|
4635
5230
|
/**
|
|
5231
|
+
* Hover-panel actions registered by sibling plugins (issue #6). The pet owns
|
|
5232
|
+
* the panel row and serves the registrations; each plugin owns the callback a
|
|
5233
|
+
* click dispatches to. Bounded, in-memory, reclaimed with the registering
|
|
5234
|
+
* plugin's disposer.
|
|
5235
|
+
*/
|
|
5236
|
+
panelActions = new PanelActionRegistry();
|
|
5237
|
+
/**
|
|
4636
5238
|
* Sessions whose reward source is the official event stream. This metadata
|
|
4637
5239
|
* outlives transient visual resets so a derived legacy `done` cannot reward
|
|
4638
5240
|
* the same turn again after the pet is disabled and re-enabled.
|
|
@@ -4661,12 +5263,10 @@ var PetService = class extends Service {
|
|
|
4661
5263
|
petId: this.registry.defaultEntry().id
|
|
4662
5264
|
};
|
|
4663
5265
|
const selected = this.registry.byId(persist.petId) ?? this.registry.defaultEntry();
|
|
4664
|
-
const voiceRemarks = mergeVoicePacks(this.registry.globalVoice, selected.voice)?.remarks;
|
|
4665
5266
|
const ledgerConfig = {
|
|
4666
5267
|
affinity: config.affinity,
|
|
4667
5268
|
treats: config.treats,
|
|
4668
|
-
|
|
4669
|
-
voiceRemarks
|
|
5269
|
+
...this.remarkLayers(selected)
|
|
4670
5270
|
};
|
|
4671
5271
|
this.ledger = new PetLedger(persist, ledgerConfig);
|
|
4672
5272
|
this.stateConfig = {
|
|
@@ -4676,6 +5276,7 @@ var PetService = class extends Service {
|
|
|
4676
5276
|
this.machine = new PetStateMachine(this.stateConfig);
|
|
4677
5277
|
this.enabled = config.enabled ?? true;
|
|
4678
5278
|
this.decorationEnabled = config.decorationEnabled ?? true;
|
|
5279
|
+
this.statusBubbles = statusBubbleMode(config.statusBubbles);
|
|
4679
5280
|
this.syncActivity();
|
|
4680
5281
|
}
|
|
4681
5282
|
/**
|
|
@@ -4699,22 +5300,115 @@ var PetService = class extends Service {
|
|
|
4699
5300
|
isEnabled() {
|
|
4700
5301
|
return this.enabled;
|
|
4701
5302
|
}
|
|
5303
|
+
/**
|
|
5304
|
+
* The remark layers one pet contributes to the ledger: its manifest pools
|
|
5305
|
+
* first, then the voice packs (a pet directory's voice.json over the global
|
|
5306
|
+
* `$DSH_HOME/pets/.voice.json`). Every path that seats the ledger's remark
|
|
5307
|
+
* pools resolves them here, so no call site can apply the manifest pools and
|
|
5308
|
+
* silently drop the voice layer: doing that reverted a pet whose lines a voice
|
|
5309
|
+
* pack had translated back to the built-in pools the moment the settings
|
|
5310
|
+
* surface applied.
|
|
5311
|
+
* @param entry - the pet whose remark pools apply.
|
|
5312
|
+
* @returns the layers `PetLedger.setRemarks` takes.
|
|
5313
|
+
*/
|
|
5314
|
+
remarkLayers(entry) {
|
|
5315
|
+
return {
|
|
5316
|
+
remarks: entry.remarks,
|
|
5317
|
+
voiceRemarks: mergeVoicePacks(this.registry.globalVoice, entry.voice)?.remarks
|
|
5318
|
+
};
|
|
5319
|
+
}
|
|
5320
|
+
/** Re-seat the ledger's remark pools from one pet's layers. */
|
|
5321
|
+
applyRemarks(entry) {
|
|
5322
|
+
const layers = this.remarkLayers(entry);
|
|
5323
|
+
this.ledger.setRemarks(layers.remarks, layers.voiceRemarks);
|
|
5324
|
+
}
|
|
4702
5325
|
/** RPC: current pet state snapshot. */
|
|
4703
5326
|
async state(currentSessionId) {
|
|
4704
5327
|
return this.view(currentSessionId);
|
|
4705
5328
|
}
|
|
4706
5329
|
/**
|
|
4707
|
-
* RPC: one plugin-authored announcement bubble
|
|
4708
|
-
*
|
|
4709
|
-
*
|
|
4710
|
-
*
|
|
5330
|
+
* RPC: one plugin-authored announcement bubble, held in the publisher's own
|
|
5331
|
+
* slot keyed by `source` (issue #1812). The payload is validated into a
|
|
5332
|
+
* bounded PetAnnouncement; a malformed one is dropped silently — a sibling
|
|
5333
|
+
* plugin's bug must never surface as pet breakage. Re-announcing replaces
|
|
5334
|
+
* only the caller's own slot, so publishers coexist instead of displacing
|
|
5335
|
+
* each other. Announcements are in-memory only.
|
|
5336
|
+
*
|
|
5337
|
+
* The slot table is bounded by {@link MAX_ANNOUNCEMENTS}. Expired entries are
|
|
5338
|
+
* pruned first, so a silent publisher never holds a slot; when the table is
|
|
5339
|
+
* genuinely full, the entry that has been silent longest gives up its slot
|
|
5340
|
+
* to the arriving one — a publisher that keeps refreshing is never evicted.
|
|
4711
5341
|
*/
|
|
4712
5342
|
announce(input) {
|
|
4713
5343
|
const parsed = parseAnnouncement(input, Date.now());
|
|
4714
5344
|
if (parsed === void 0) return { ok: false };
|
|
4715
|
-
|
|
5345
|
+
const now = Date.now();
|
|
5346
|
+
for (const [source, entry] of this.announcements) if (!announcementFresh(entry, now)) this.announcements.delete(source);
|
|
5347
|
+
if (!this.announcements.has(parsed.source) && this.announcements.size >= 4) {
|
|
5348
|
+
let oldest = "";
|
|
5349
|
+
let oldestAt = Number.POSITIVE_INFINITY;
|
|
5350
|
+
for (const [source, entry] of this.announcements) if (entry.at < oldestAt) {
|
|
5351
|
+
oldest = source;
|
|
5352
|
+
oldestAt = entry.at;
|
|
5353
|
+
}
|
|
5354
|
+
if (oldest !== "") this.announcements.delete(oldest);
|
|
5355
|
+
}
|
|
5356
|
+
this.announcements.set(parsed.source, parsed);
|
|
4716
5357
|
return { ok: true };
|
|
4717
5358
|
}
|
|
5359
|
+
/**
|
|
5360
|
+
* The announcement slots still fresh, in render order. An expired entry
|
|
5361
|
+
* simply stops appearing (the client's 2 s poll drops it); no timer owns its
|
|
5362
|
+
* removal.
|
|
5363
|
+
*/
|
|
5364
|
+
freshAnnouncements() {
|
|
5365
|
+
const now = Date.now();
|
|
5366
|
+
return [...this.announcements.values()].filter((entry) => announcementFresh(entry, now));
|
|
5367
|
+
}
|
|
5368
|
+
/**
|
|
5369
|
+
* Register one action into the hover panel's action row (issue #6). The pet
|
|
5370
|
+
* renders the button and dispatches the click back to `onSelect`; the plugin
|
|
5371
|
+
* decides what the button does. Registering is a wiring step, so a malformed
|
|
5372
|
+
* registration throws at the caller's own apply instead of being dropped.
|
|
5373
|
+
*
|
|
5374
|
+
* The registration is reclaimed by the disposer, so the idiomatic call binds
|
|
5375
|
+
* it to the registering plugin's fiber:
|
|
5376
|
+
* ```ts
|
|
5377
|
+
* ctx.effect(() => ctx.pet.registerPanelAction({
|
|
5378
|
+
* id: 'pet-quota-swap',
|
|
5379
|
+
* label: '换装',
|
|
5380
|
+
* onSelect: async () => { await cycleToNextPet(ctx.pet) },
|
|
5381
|
+
* }))
|
|
5382
|
+
* ```
|
|
5383
|
+
* @param action - the registration to serve in the panel row.
|
|
5384
|
+
* @returns a disposer that removes this registration.
|
|
5385
|
+
* @throws when the id, label or callback is unusable, or the row is full.
|
|
5386
|
+
*/
|
|
5387
|
+
registerPanelAction(action) {
|
|
5388
|
+
return this.panelActions.register(action);
|
|
5389
|
+
}
|
|
5390
|
+
/**
|
|
5391
|
+
* RPC: dispatch one hover-panel action click back to the plugin that
|
|
5392
|
+
* registered it. A rejected id is a stale click (the plugin disposed its
|
|
5393
|
+
* registration between render and click), and a throwing callback is that
|
|
5394
|
+
* plugin's own failure — neither may surface as pet breakage, so both answer
|
|
5395
|
+
* `{ ok: false }` instead of throwing out of the route.
|
|
5396
|
+
* @param id - the action id the browser half reported.
|
|
5397
|
+
* @returns whether the click reached a registered action.
|
|
5398
|
+
*/
|
|
5399
|
+
async selectPanelAction(id) {
|
|
5400
|
+
try {
|
|
5401
|
+
return await this.panelActions.select(id, { petId: this.selectedPetId() }) ? { ok: true } : {
|
|
5402
|
+
ok: false,
|
|
5403
|
+
error: "unknown-panel-action"
|
|
5404
|
+
};
|
|
5405
|
+
} catch {
|
|
5406
|
+
return {
|
|
5407
|
+
ok: false,
|
|
5408
|
+
error: "panel-action-failed"
|
|
5409
|
+
};
|
|
5410
|
+
}
|
|
5411
|
+
}
|
|
4718
5412
|
/** Current persisted display config (read-only view). */
|
|
4719
5413
|
display() {
|
|
4720
5414
|
return { ...this.ledger.snapshot.display };
|
|
@@ -4762,8 +5456,7 @@ var PetService = class extends Service {
|
|
|
4762
5456
|
error: "unknown-pet"
|
|
4763
5457
|
};
|
|
4764
5458
|
this.ledger.setPetId(entry.id);
|
|
4765
|
-
|
|
4766
|
-
this.ledger.setRemarks(entry.remarks, voiceRemarks);
|
|
5459
|
+
this.applyRemarks(entry);
|
|
4767
5460
|
this.flush();
|
|
4768
5461
|
this.syncSettingsFromPet();
|
|
4769
5462
|
return {
|
|
@@ -5185,10 +5878,11 @@ var PetService = class extends Service {
|
|
|
5185
5878
|
*/
|
|
5186
5879
|
applySettingsSection(section) {
|
|
5187
5880
|
this.decorationEnabled = section.decorationEnabled ?? true;
|
|
5881
|
+
this.statusBubbles = statusBubbleMode(section.statusBubbles);
|
|
5188
5882
|
const selected = typeof section.petId === "string" ? this.registry.byId(section.petId) : void 0;
|
|
5189
5883
|
if (selected !== void 0) {
|
|
5190
5884
|
this.ledger.setPetId(selected.id);
|
|
5191
|
-
this.
|
|
5885
|
+
this.applyRemarks(selected);
|
|
5192
5886
|
} else if (section.petId !== void 0) this.syncSettingsFromPet();
|
|
5193
5887
|
const next = { ...this.ledger.snapshot.display };
|
|
5194
5888
|
next.visible = section.visible;
|
|
@@ -5252,8 +5946,10 @@ var PetService = class extends Service {
|
|
|
5252
5946
|
settleGameplay(state, gameplayDef, Date.now(), { sessionActive: snapshot.sessionActive });
|
|
5253
5947
|
gameplay = this.gameplayViewOf(state);
|
|
5254
5948
|
}
|
|
5255
|
-
const
|
|
5949
|
+
const announcements = this.freshAnnouncements();
|
|
5950
|
+
const announcement = announcements.reduce((freshest, entry) => freshest === void 0 || entry.at > freshest.at ? entry : freshest, void 0);
|
|
5256
5951
|
const skin = this.persistedSkin(entry);
|
|
5952
|
+
const panelActions = this.panelActions.views();
|
|
5257
5953
|
return {
|
|
5258
5954
|
animation: snapshot.animation,
|
|
5259
5955
|
...snapshot.bubble === void 0 ? {} : { bubble: snapshot.bubble },
|
|
@@ -5262,6 +5958,9 @@ var PetService = class extends Service {
|
|
|
5262
5958
|
sessions,
|
|
5263
5959
|
...decoration === void 0 ? {} : { decoration },
|
|
5264
5960
|
...announcement === void 0 ? {} : { announcement },
|
|
5961
|
+
...announcements.length === 0 ? {} : { announcements },
|
|
5962
|
+
statusBubbles: this.statusBubbles,
|
|
5963
|
+
...panelActions.length === 0 ? {} : { panelActions },
|
|
5265
5964
|
affinity: this.ledger.affinityView(Date.now()),
|
|
5266
5965
|
display: {
|
|
5267
5966
|
...this.ledger.snapshot.display,
|
|
@@ -5970,6 +6669,11 @@ function makePetRoutes(deps) {
|
|
|
5970
6669
|
if (typeof petId !== "string") return Promise.reject(/* @__PURE__ */ new Error("invalid-pet"));
|
|
5971
6670
|
return service.setPetId(petId);
|
|
5972
6671
|
}),
|
|
6672
|
+
postRoute(ctx, "/api/pet/panel-action", (body) => {
|
|
6673
|
+
const id = body.id;
|
|
6674
|
+
if (typeof id !== "string") return Promise.reject(/* @__PURE__ */ new Error("invalid-panel-action"));
|
|
6675
|
+
return service.selectPanelAction(id);
|
|
6676
|
+
}),
|
|
5973
6677
|
postRoute(ctx, "/api/pet/gameplay/touch", (body) => {
|
|
5974
6678
|
const zone = body.zone;
|
|
5975
6679
|
if (zone !== void 0 && typeof zone !== "string") return Promise.reject(/* @__PURE__ */ new Error("invalid-zone"));
|
|
@@ -6145,7 +6849,8 @@ const PET_FORM_DEFAULTS = {
|
|
|
6145
6849
|
bubbleScale: 1,
|
|
6146
6850
|
petId: DEFAULT_PET_ID,
|
|
6147
6851
|
enabled: true,
|
|
6148
|
-
decorationEnabled: true
|
|
6852
|
+
decorationEnabled: true,
|
|
6853
|
+
statusBubbles: DEFAULT_STATUS_BUBBLE_MODE
|
|
6149
6854
|
};
|
|
6150
6855
|
/**
|
|
6151
6856
|
* Plugin configuration. Under the 0.1.7 settings model a plugin's own Cordis
|
|
@@ -6170,7 +6875,8 @@ const Config = z.object({
|
|
|
6170
6875
|
bubbleScale: z.number().step(.05).min(BUBBLE_SCALE_MIN).max(2).default(PET_FORM_DEFAULTS.bubbleScale).volatile(),
|
|
6171
6876
|
petId: z.string().volatile(),
|
|
6172
6877
|
enabled: z.boolean().default(PET_FORM_DEFAULTS.enabled).volatile(),
|
|
6173
|
-
decorationEnabled: z.boolean().default(PET_FORM_DEFAULTS.decorationEnabled).volatile()
|
|
6878
|
+
decorationEnabled: z.boolean().default(PET_FORM_DEFAULTS.decorationEnabled).volatile(),
|
|
6879
|
+
statusBubbles: z.union([...STATUS_BUBBLE_MODES]).default(PET_FORM_DEFAULTS.statusBubbles).volatile()
|
|
6174
6880
|
});
|
|
6175
6881
|
/**
|
|
6176
6882
|
* Read one live config field.
|
|
@@ -6236,7 +6942,8 @@ function petSettingsSection(config, fallbackPetId, persisted = {}, committed = {
|
|
|
6236
6942
|
bubbleScale: displayField("bubbleScale", config.bubbleScale, persisted, committed),
|
|
6237
6943
|
petId: readLive(config.petId, fallbackPetId),
|
|
6238
6944
|
enabled,
|
|
6239
|
-
decorationEnabled: readLive(config.decorationEnabled, PET_FORM_DEFAULTS.decorationEnabled)
|
|
6945
|
+
decorationEnabled: readLive(config.decorationEnabled, PET_FORM_DEFAULTS.decorationEnabled),
|
|
6946
|
+
statusBubbles: statusBubbleMode(readLive(config.statusBubbles, void 0))
|
|
6240
6947
|
};
|
|
6241
6948
|
}
|
|
6242
6949
|
/** The settings row ids a pet install is served under, most specific first. */
|
|
@@ -6281,6 +6988,7 @@ function applyImpl(ctx, config = {}) {
|
|
|
6281
6988
|
...config,
|
|
6282
6989
|
enabled: readLive(config.enabled, PET_FORM_DEFAULTS.enabled),
|
|
6283
6990
|
decorationEnabled: readLive(config.decorationEnabled, PET_FORM_DEFAULTS.decorationEnabled),
|
|
6991
|
+
statusBubbles: statusBubbleMode(readLive(config.statusBubbles, void 0)),
|
|
6284
6992
|
registry
|
|
6285
6993
|
});
|
|
6286
6994
|
service.settingsNamespaceProvider = () => servedRow(ctx)?.ns;
|
|
@@ -6324,4 +7032,4 @@ function applyImpl(ctx, config = {}) {
|
|
|
6324
7032
|
syncSettings();
|
|
6325
7033
|
}
|
|
6326
7034
|
//#endregion
|
|
6327
|
-
export { AFFINITY_MAX, AFFINITY_RANKS, BUILTIN_REMARKS, Config, DEFAULT_FRAME_COUNTS, DEFAULT_PET_CELL, DEFAULT_PET_COLUMNS, DEFAULT_PET_ID, DEFAULT_PET_NAME, DEFAULT_PET_ROW_COUNT, DEFAULT_TRACK_PATTERNS, MAX_SESSION_BUBBLES, PET_API_PREFIX, PET_ASSET_PREFIX, PET_FORM_DEFAULTS, PET_NAME_MAX_LENGTH, PET_ROW_ORDER, PetService, PetStateMachine, REMARK_KINDS, REMARK_LINES_MAX, REMARK_LINE_MAX, RemarkPicker, animationForPhase, apply, applyInteraction, applyTurnReward, builtinRemark, codexPetsDir, consumeTreat, defaultDisplayConfig, defaultTreatConfig, emptyAffinity, emptyPersist, emptyTreatLedger, inject, loadPetPersist, loadPetRegistry, makePetRoutes, name, normalizePetRemarks, petEntryView, petHomeDir, petPackageRoot, petSettingsSection, rankOf, resolvePetManifest, rowOf, savePetPersist, settleTreatGrants };
|
|
7035
|
+
export { AFFINITY_MAX, AFFINITY_RANKS, BUILTIN_REMARKS, Config, DEFAULT_FRAME_COUNTS, DEFAULT_PET_CELL, DEFAULT_PET_COLUMNS, DEFAULT_PET_ID, DEFAULT_PET_NAME, DEFAULT_PET_ROW_COUNT, DEFAULT_STATUS_BUBBLE_MODE, DEFAULT_TRACK_PATTERNS, MAX_PANEL_ACTIONS, MAX_SESSION_BUBBLES, PET_API_PREFIX, PET_ASSET_PREFIX, PET_FORM_DEFAULTS, PET_NAME_MAX_LENGTH, PET_ROW_ORDER, PanelActionRegistry, PetService, PetStateMachine, REMARK_KINDS, REMARK_LINES_MAX, REMARK_LINE_MAX, RemarkPicker, STATUS_BUBBLE_MODES, animationForPhase, apply, applyInteraction, applyTurnReward, builtinRemark, codexPetsDir, consumeTreat, defaultDisplayConfig, defaultTreatConfig, emptyAffinity, emptyPersist, emptyTreatLedger, inject, loadPetPersist, loadPetRegistry, makePetRoutes, name, normalizePanelAction, normalizePetRemarks, petEntryView, petHomeDir, petPackageRoot, petSettingsSection, rankOf, resolvePetManifest, rowOf, savePetPersist, settleTreatGrants, statusBubbleMode };
|