@clawtopia/werewolf-evolution 0.1.20 → 2.0.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -54
- package/bin/werewolf-evolution.mjs +69 -0
- package/package.json +12 -66
- package/skills/werewolf-evolution/SKILL.md +64 -0
- package/skills/werewolf-evolution/VERSION +1 -0
- package/skills/werewolf-evolution/scripts/evo.mjs +330 -0
- package/skills/werewolf-evolution/scripts/lib/api.mjs +24 -0
- package/skills/werewolf-evolution/scripts/lib/baseline.mjs +46 -0
- package/skills/werewolf-evolution/scripts/lib/brief.mjs +109 -0
- package/skills/werewolf-evolution/scripts/lib/context.mjs +46 -0
- package/skills/werewolf-evolution/scripts/lib/facts.mjs +155 -0
- package/skills/werewolf-evolution/scripts/lib/lessons.mjs +362 -0
- package/skills/werewolf-evolution/scripts/lib/metrics.mjs +57 -0
- package/skills/werewolf-evolution/scripts/lib/notify.mjs +22 -0
- package/skills/werewolf-evolution/scripts/lib/outcomes.mjs +119 -0
- package/skills/werewolf-evolution/scripts/lib/pool.mjs +70 -0
- package/skills/werewolf-evolution/scripts/lib/projection.mjs +155 -0
- package/skills/werewolf-evolution/scripts/lib/projector.mjs +139 -0
- package/skills/werewolf-evolution/scripts/lib/seat.mjs +42 -0
- package/skills/werewolf-evolution/scripts/lib/store.mjs +82 -0
- package/skills/werewolf-evolution/scripts/lib/sync.mjs +157 -0
- package/skills/werewolf-evolution/seeds.json +62 -0
- package/dist/index.d.ts +0 -294
- package/dist/index.js +0 -4151
- package/openclaw.plugin.json +0 -19
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// 由结果视图构建复盘用的事实目录与决策窗口。纯函数,无 I/O。
|
|
2
|
+
//
|
|
3
|
+
// 窗口(window)是“某个座位在某一天的一次决策”,带确定性的局部结果:
|
|
4
|
+
// DAY_VOTE 好人:投中狼 → good,投了好人 → bad;弃票 → null
|
|
5
|
+
// 狼:当天放逐了好人且自己投了他 → good;当天放逐了狼队友且自己没跟票 → bad;其余 → null
|
|
6
|
+
// (狼几乎不会投队友,“投到好人”的基线约 99%,没有区分度,改看这一票是否推动了出局)
|
|
7
|
+
// DAY_SPEECH 狼:当天被放逐 → bad;当天有人被放逐而自己没有 → good
|
|
8
|
+
// 好人:null。好人发言只有“被放逐”一种可观测结果,无法区分好坏,只记录不计分
|
|
9
|
+
// 结果由对局真相计算,模型不能改写。
|
|
10
|
+
const campOf = (role) => (role === 'WOLF' ? 'WOLF' : 'GOOD');
|
|
11
|
+
const ROLE_CN = { WOLF: '狼', VILLAGER: '民', SEER: '预', WITCH: '巫', HUNTER: '猎' };
|
|
12
|
+
|
|
13
|
+
export const DECISION_TYPES = ['DAY_VOTE', 'DAY_SPEECH', 'SEER_CLAIM'];
|
|
14
|
+
export const ROLES = ['WOLF', 'VILLAGER', 'SEER', 'WITCH', 'HUNTER'];
|
|
15
|
+
|
|
16
|
+
const clip = (s, n) => {
|
|
17
|
+
const t = String(s ?? '').replace(/\s+/g, ' ').trim();
|
|
18
|
+
return t.length > n ? `${t.slice(0, n)}…` : t;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// 超时、熔断等系统兜底产生的发言/投票不是模型的决策,不作为学习窗口(v2 方案 5.7 按座位门禁)
|
|
22
|
+
const SYSTEM_TEXT = /^\(Agent |^CIRCUIT_BREAKER|^TIMEOUT|熔断|超时未(发言|投票)/;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @returns {null | {gameId, result, selfSeat, selfRole, seats, facts, windows}}
|
|
26
|
+
* seats: [{seat, role, camp}];facts: 发言/遗言/淘汰;windows: 决策窗口
|
|
27
|
+
* 非自己的发言只有在有确定结果时才成为窗口(无结果的不能计分,只会占篇幅)
|
|
28
|
+
*/
|
|
29
|
+
export function buildGameFacts(replay, record, selfAgentId, { speechChars = 60, reasonChars = 36 } = {}) {
|
|
30
|
+
const events = [...(replay?.events ?? [])].sort((a, b) => (a.seq ?? 0) - (b.seq ?? 0));
|
|
31
|
+
const players = events.find((e) => e.eventType === 'INITIAL_STATE')?.payload?.players;
|
|
32
|
+
if (!Array.isArray(players)) return null;
|
|
33
|
+
const bySeat = new Map(players.map((p) => [Number(p.seat), { seat: Number(p.seat), role: p.role, camp: campOf(p.role), agentId: String(p.agentId) }]));
|
|
34
|
+
const me = [...bySeat.values()].find((p) => p.agentId === String(selfAgentId));
|
|
35
|
+
if (!me) return null;
|
|
36
|
+
const gameId = String(record?.gameId ?? events[0]?.gameId);
|
|
37
|
+
|
|
38
|
+
// 先找出每天被放逐的座位,用于发言窗口的结果
|
|
39
|
+
const votedOutByRound = new Map();
|
|
40
|
+
let round = 1;
|
|
41
|
+
for (const e of events) {
|
|
42
|
+
if (e.eventType === 'PHASE_CHANGE' && e.payload?.round != null) round = Number(e.payload.round);
|
|
43
|
+
if (e.eventType === 'ELIMINATION' && e.payload?.cause === 'VOTED_OUT') votedOutByRound.set(round, Number(e.payload.seat));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const facts = [], windows = [];
|
|
47
|
+
round = 1;
|
|
48
|
+
for (const e of events) {
|
|
49
|
+
const p = e.payload ?? {};
|
|
50
|
+
if (e.eventType === 'PHASE_CHANGE' && p.round != null) { round = Number(p.round); continue; }
|
|
51
|
+
const id = `${gameId}-${e.seq}`;
|
|
52
|
+
if (e.eventType === 'DAY_SPEAK' || e.eventType === 'LAST_WORD') {
|
|
53
|
+
const seat = Number(p.seat);
|
|
54
|
+
const who = bySeat.get(seat);
|
|
55
|
+
if (!who) continue;
|
|
56
|
+
const kind = e.eventType === 'DAY_SPEAK' ? 'SPEECH' : 'LAST_WORD';
|
|
57
|
+
const system = SYSTEM_TEXT.test(String(e.content ?? '').trim());
|
|
58
|
+
if (kind === 'LAST_WORD' && system) continue;
|
|
59
|
+
facts.push({ id: `f${id}`, kind, round, seat, text: system ? '(系统兜底)' : clip(e.content, speechChars) });
|
|
60
|
+
if (kind === 'SPEECH' && !system) {
|
|
61
|
+
const out = votedOutByRound.get(round) === seat;
|
|
62
|
+
const wolf = who.camp === 'WOLF';
|
|
63
|
+
const outcome = !wolf ? null : out ? 'bad' : votedOutByRound.has(round) ? 'good' : null;
|
|
64
|
+
if (outcome || seat === me.seat) {
|
|
65
|
+
windows.push({ id: `w${id}`, gameId, decisionType: 'DAY_SPEECH', round, seat, role: who.role, self: seat === me.seat, outcome, why: outcome === 'bad' ? '当天被放逐' : outcome ? '狼发言后当天未被放逐' : out ? '当天被放逐(好人发言不计分)' : '' });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} else if (e.eventType === 'DAY_VOTE_SUBMIT' || e.eventType === 'DAY_REVOTE_SUBMIT') {
|
|
69
|
+
const voter = bySeat.get(Number(p.voterSeat));
|
|
70
|
+
if (!voter) continue;
|
|
71
|
+
if (SYSTEM_TEXT.test(String(p.voteReason ?? '').trim())) continue;
|
|
72
|
+
const target = p.targetSeat == null ? null : bySeat.get(Number(p.targetSeat));
|
|
73
|
+
let outcome = null, why = '弃票';
|
|
74
|
+
if (target && voter.camp === 'GOOD') {
|
|
75
|
+
outcome = target.camp === 'WOLF' ? 'good' : 'bad';
|
|
76
|
+
why = outcome === 'good' ? '投中狼' : '投了好人';
|
|
77
|
+
} else if (target) {
|
|
78
|
+
const outSeat = votedOutByRound.get(round);
|
|
79
|
+
const outCamp = outSeat == null ? null : bySeat.get(outSeat)?.camp;
|
|
80
|
+
if (outCamp === 'GOOD' && target.seat === outSeat) { outcome = 'good'; why = '投出了好人'; }
|
|
81
|
+
else if (outCamp === 'WOLF' && target.seat !== outSeat) { outcome = 'bad'; why = '狼队友被放逐'; }
|
|
82
|
+
else { why = outCamp === 'WOLF' ? '跟票已被放逐的队友' : outCamp === 'GOOD' ? '没投被放逐的好人' : '当天无人被放逐'; }
|
|
83
|
+
}
|
|
84
|
+
windows.push({
|
|
85
|
+
id: `w${id}`, gameId, decisionType: 'DAY_VOTE', round, seat: voter.seat, role: voter.role, self: voter.seat === me.seat,
|
|
86
|
+
targetSeat: target?.seat ?? null, outcome, why, reason: clip(p.voteReason, reasonChars),
|
|
87
|
+
revote: e.eventType === 'DAY_REVOTE_SUBMIT',
|
|
88
|
+
});
|
|
89
|
+
} else if (e.eventType === 'ELIMINATION' && p.seat != null) {
|
|
90
|
+
facts.push({ id: `f${id}`, kind: 'ELIMINATION', round, seat: Number(p.seat), cause: p.cause });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const seer = seerClaimWindow(events, bySeat, gameId, me.seat);
|
|
94
|
+
if (seer) windows.push(seer);
|
|
95
|
+
return {
|
|
96
|
+
gameId,
|
|
97
|
+
result: record?.result ?? replay?.game?.result,
|
|
98
|
+
selfSeat: me.seat,
|
|
99
|
+
selfRole: me.role,
|
|
100
|
+
seats: [...bySeat.values()].sort((a, b) => a.seat - b.seat).map(({ seat, role, camp }) => ({ seat, role, camp })),
|
|
101
|
+
facts,
|
|
102
|
+
windows,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const CHECK_TEXT = /(查验|验了|验出|金水|查杀|验的是|验人)/;
|
|
107
|
+
const NEXT_CHECK_TEXT = /(警徽流|今晚验|明晚验|下一个验|接下来验|今晚查|明晚查|下一晚验|如果我(今晚)?(倒牌|死|被刀)|我死了.*(验|查))/;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 预言家首日的身份决策(SEER_CLAIM):是否起跳、报金水还是查杀、是否公开下一晚验谁。
|
|
111
|
+
* 8 人场没有警长和警徽;夜里被刀的玩家除首夜外没有遗言(19700~19774:非首夜刀死 129 人,0 人有遗言),
|
|
112
|
+
* 所以预言家次夜被刀后,验人结果只能靠之前公开的验人目标间接传递。
|
|
113
|
+
* 结果:死前在发言或遗言里公开报出验人结果的天数 ≥2 → good,否则 bad。
|
|
114
|
+
* 2026-10-20 全平台 231 局:91% 首日起跳、85% 起跳后次夜被刀,只有 5% 能在两天里公开报出验人结果。
|
|
115
|
+
*/
|
|
116
|
+
function seerClaimWindow(events, bySeat, gameId, selfSeat) {
|
|
117
|
+
const seer = [...bySeat.values()].find((p) => p.role === 'SEER');
|
|
118
|
+
if (!seer) return null;
|
|
119
|
+
let round = 1, day1 = '';
|
|
120
|
+
const reported = new Set();
|
|
121
|
+
for (const e of events) {
|
|
122
|
+
const p = e.payload ?? {};
|
|
123
|
+
if (e.eventType === 'PHASE_CHANGE' && p.round != null) round = Number(p.round);
|
|
124
|
+
if ((e.eventType !== 'DAY_SPEAK' && e.eventType !== 'LAST_WORD') || Number(p.seat) !== seer.seat) continue;
|
|
125
|
+
const t = String(e.content ?? '');
|
|
126
|
+
if (SYSTEM_TEXT.test(t.trim())) continue;
|
|
127
|
+
if (e.eventType === 'DAY_SPEAK' && round === 1) day1 += t;
|
|
128
|
+
if ((/预言家/.test(t) && CHECK_TEXT.test(t)) || /(金水|查杀)/.test(t)) reported.add(round);
|
|
129
|
+
}
|
|
130
|
+
if (!day1) return null; // 首夜死亡或首日超时,没有做出决策
|
|
131
|
+
const claimed = /预言家/.test(day1) && CHECK_TEXT.test(day1);
|
|
132
|
+
const claim = !claimed ? '首日不起跳' : `首日起跳报${/查杀/.test(day1) ? '查杀' : '金水'}${NEXT_CHECK_TEXT.test(day1) ? '并公开下一晚验人' : '、未公开下一晚验人'}`;
|
|
133
|
+
const good = reported.size >= 2;
|
|
134
|
+
return {
|
|
135
|
+
id: `w${gameId}-seer`, gameId, decisionType: 'SEER_CLAIM', round: 1, seat: seer.seat, role: 'SEER', self: seer.seat === selfSeat,
|
|
136
|
+
outcome: good ? 'good' : 'bad', why: `公开报出验人结果 ${reported.size} 天`, claim,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 本 Agent 在一局里被系统兜底(超时、熔断)的次数,用于投影的整体保险 */
|
|
141
|
+
export function selfTimeouts(replay, agentId) {
|
|
142
|
+
let n = 0;
|
|
143
|
+
for (const e of replay?.events ?? []) {
|
|
144
|
+
if (String(e.actorAgentId) !== String(agentId)) continue;
|
|
145
|
+
if (e.eventType === 'CIRCUIT_BREAKER_TRIPPED') n += 1;
|
|
146
|
+
else if (SYSTEM_TEXT.test(String(e.content ?? '').trim()) || SYSTEM_TEXT.test(String(e.payload?.voteReason ?? '').trim())) n += 1;
|
|
147
|
+
}
|
|
148
|
+
return n;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function seatsLine(seats) {
|
|
152
|
+
return seats.map((s) => `${s.seat}${ROLE_CN[s.role] ?? s.role}`).join(' ');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export { ROLE_CN };
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
// 经验库与提交校验、计分。纯函数,无 I/O。
|
|
2
|
+
//
|
|
3
|
+
// 计分(schema 2,2026-10-20 起):和基线比,不再按 good/bad 固定加减分。
|
|
4
|
+
// - 旧规则“good +1、bad −2”的保本线是 67% good;狼发言基线 76%、狼投票(旧口径)99%,
|
|
5
|
+
// 随便一条狼的经验都会涨到 ready,好人投票基线 47% 则只会掉分。首批 5 条 ready 全是狼人经验。
|
|
6
|
+
// - 每批复盘由脚本给每条经验分配本批次里自己(★)的、有结果的、角色与决策类型匹配的窗口,
|
|
7
|
+
// 模型逐个标 followed = true(照做)/ false(违背)/ "na"(触发条件不成立)。不能自己挑窗口。
|
|
8
|
+
// - 只有照做的窗口计入统计,每局每条经验最多计 PER_GAME 个(同局窗口高度相关)。
|
|
9
|
+
// O = good 次数,E = Σp,V = Σp(1−p),p 是该窗口角色与决策类型的基线 good 率(baseline.mjs);z = (O−E)/√V。
|
|
10
|
+
// - ready:≥MIN_WINDOWS 个窗口、来自 ≥MIN_GAMES 局、z ≥ Z。归档:≥MIN_WINDOWS 个窗口且 z ≤ −Z,或 STALE_DAYS 天没有新窗口。
|
|
11
|
+
// - score = 照做窗口的 good 率比基线高多少个百分点,只用于展示和排序。
|
|
12
|
+
// - NEW 不计入统计:新经验的证据是模型事后挑的,只用来证明值得记录。
|
|
13
|
+
// schema 1 的经验迁移时统计清零、ready 退回 candidate,文本与历史证据保留。
|
|
14
|
+
import { DECISION_TYPES, ROLES } from './facts.mjs';
|
|
15
|
+
import { baselineOf } from './baseline.mjs';
|
|
16
|
+
|
|
17
|
+
export const LESSONS_SCHEMA = 2;
|
|
18
|
+
export const MIN_WINDOWS = 6;
|
|
19
|
+
export const MIN_GAMES = 4;
|
|
20
|
+
export const Z = 1.64;
|
|
21
|
+
export const PER_GAME = 2;
|
|
22
|
+
export const STALE_DAYS = 60;
|
|
23
|
+
export const NA_MIN_LABELS = 6;
|
|
24
|
+
export const NA_MAX_RATE = 0.6;
|
|
25
|
+
export const MAX_ASSIGNED_LESSONS = 5;
|
|
26
|
+
export const MAX_ASSIGNED_WINDOWS = 4;
|
|
27
|
+
export const MAX_NEW = 2;
|
|
28
|
+
export const MAX_ITEMS = MAX_ASSIGNED_LESSONS + MAX_NEW;
|
|
29
|
+
const DAY_MS = 24 * 3600 * 1000;
|
|
30
|
+
const LIMITS = { trigger: 80, rule: 120, boundary: 80 };
|
|
31
|
+
// 经验文本只写可复用的判断方法,不得写具体座位(座位每局不同,写了就是记住了某局的答案)
|
|
32
|
+
const LEAK_PATTERNS = [/\d+\s*号/, /[一二三四五六七八]号/, /座位\s*\d/];
|
|
33
|
+
const ABSOLUTE = /(永远|绝对|一定要|无论如何|必须)/;
|
|
34
|
+
const FOLLOWED = [true, false, 'na'];
|
|
35
|
+
|
|
36
|
+
export function emptyLessons(agentId) {
|
|
37
|
+
return { schemaVersion: LESSONS_SCHEMA, agentId: String(agentId), nextSeq: 1, lessons: {}, appliedBatches: [] };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const emptyStats = () => ({ n: 0, good: 0, expected: 0, variance: 0, perGame: {}, violated: 0, na: 0 });
|
|
41
|
+
export const zOf = (s) => (s.variance > 0 ? (s.good - s.expected) / Math.sqrt(s.variance) : 0);
|
|
42
|
+
export const liftOf = (s) => (s.n ? Math.round(((s.good - s.expected) / s.n) * 100) : 0);
|
|
43
|
+
|
|
44
|
+
export function statusOf(lesson, now) {
|
|
45
|
+
const s = lesson.stats ?? emptyStats();
|
|
46
|
+
// 平台检验结论(pool.mjs 合并多个 Agent 的标注得出,随 seeds.json 下发)
|
|
47
|
+
const platform = lesson.source === 'seed' ? lesson.platform?.status : null;
|
|
48
|
+
if (platform === 'rejected') return 'archived';
|
|
49
|
+
const last = Date.parse(lesson.lastScoredAt ?? lesson.migratedAt ?? lesson.createdAt);
|
|
50
|
+
if (platform !== 'verified' && now && Date.parse(now) - last > STALE_DAYS * DAY_MS) return 'archived';
|
|
51
|
+
const z = zOf(s);
|
|
52
|
+
// 本机自己的检验明显变差时,平台通过的也不用
|
|
53
|
+
if (s.n >= MIN_WINDOWS && z <= -Z) return 'archived';
|
|
54
|
+
const labels = s.n + (s.violated ?? 0) + (s.na ?? 0);
|
|
55
|
+
if (labels >= NA_MIN_LABELS && (s.na ?? 0) / labels > NA_MAX_RATE) return 'candidate';
|
|
56
|
+
if (platform === 'verified') return 'ready';
|
|
57
|
+
if (s.n >= MIN_WINDOWS && Object.keys(s.perGame).length >= MIN_GAMES && z >= Z) return 'ready';
|
|
58
|
+
return 'candidate';
|
|
59
|
+
}
|
|
60
|
+
// 投影后被撤下的经验:paused 暂停(可复查后重新投影),retired 同一 Agent 上暂停过两次,不再投影
|
|
61
|
+
const HELD = new Set(['paused', 'retired']);
|
|
62
|
+
export const WITHDRAW_MIN = 6;
|
|
63
|
+
export const WITHDRAW_Z = -1;
|
|
64
|
+
export const WITHDRAW_RECENT = 20;
|
|
65
|
+
export const ADOPT_MIN_RATE = 0.2;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 已投影经验的自动撤下(宁可错撤,不让 Agent 变笨):只看投影开始后的窗口。
|
|
69
|
+
* - 最近 WITHDRAW_RECENT 个计入检验的窗口里 ≥WITHDRAW_MIN 个,且 z ≤ WITHDRAW_Z → 明显变差;
|
|
70
|
+
* - 分配到的窗口 ≥WITHDRAW_MIN 个,照做比例 < ADOPT_MIN_RATE → 模型没采纳(“不适用”也算没采纳,防止躲过检验)。
|
|
71
|
+
* 返回本次撤下的 [{id, reason, status}]。
|
|
72
|
+
*/
|
|
73
|
+
export function checkWithdraw(state, projectedIds, sinceGameId, now) {
|
|
74
|
+
const out = [];
|
|
75
|
+
for (const id of projectedIds) {
|
|
76
|
+
const l = state.lessons[id];
|
|
77
|
+
if (!l || l.status !== 'ready') continue;
|
|
78
|
+
const ev = l.evidence.filter((e) => 'p' in e && Number(e.gameId) >= sinceGameId);
|
|
79
|
+
const counted = ev.filter((e) => e.counted).slice(-WITHDRAW_RECENT);
|
|
80
|
+
let reason = null;
|
|
81
|
+
if (counted.length >= WITHDRAW_MIN) {
|
|
82
|
+
const s = counted.reduce((a, e) => ({ good: a.good + (e.outcome === 'good' ? 1 : 0), expected: a.expected + e.p, variance: a.variance + e.p * (1 - e.p) }), { good: 0, expected: 0, variance: 0 });
|
|
83
|
+
const z = zOf(s);
|
|
84
|
+
if (z <= WITHDRAW_Z) reason = `投影后最近 ${counted.length} 个照做窗口比基线差(z=${z.toFixed(2)})`;
|
|
85
|
+
}
|
|
86
|
+
if (!reason && ev.length >= WITHDRAW_MIN) {
|
|
87
|
+
const rate = ev.filter((e) => e.followed === true).length / ev.length;
|
|
88
|
+
if (rate < ADOPT_MIN_RATE) reason = `投影后 ${ev.length} 个匹配窗口里只照做了 ${Math.round(rate * 100)}%,模型基本没采纳`;
|
|
89
|
+
}
|
|
90
|
+
if (!reason) continue;
|
|
91
|
+
l.pausedCount = (l.pausedCount ?? 0) + 1;
|
|
92
|
+
l.status = l.pausedCount >= 2 ? 'retired' : 'paused';
|
|
93
|
+
l.pausedAt = now;
|
|
94
|
+
l.pauseReason = reason;
|
|
95
|
+
out.push({ id, reason, status: l.status });
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 旧 schema 迁移:统计清零、ready 退回 candidate(旧分数由固定加减分得来,不可信) */
|
|
101
|
+
export function migrateLessons(state, now) {
|
|
102
|
+
if ((state.schemaVersion ?? 1) >= LESSONS_SCHEMA) return state;
|
|
103
|
+
for (const l of Object.values(state.lessons)) {
|
|
104
|
+
l.legacyScore = l.score;
|
|
105
|
+
l.stats = emptyStats();
|
|
106
|
+
l.score = 0;
|
|
107
|
+
l.migratedAt = now;
|
|
108
|
+
if (l.status !== 'archived') l.status = 'candidate';
|
|
109
|
+
delete l.decayApplied;
|
|
110
|
+
}
|
|
111
|
+
state.schemaVersion = LESSONS_SCHEMA;
|
|
112
|
+
return state;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 太久没有新窗口的经验归档,返回本次归档的条目 */
|
|
116
|
+
export function applyStale(state, now) {
|
|
117
|
+
const out = [];
|
|
118
|
+
for (const l of Object.values(state.lessons)) {
|
|
119
|
+
if (l.status === 'archived' || HELD.has(l.status)) continue;
|
|
120
|
+
if (statusOf(l, now) === 'archived') { l.status = 'archived'; out.push({ id: l.id }); }
|
|
121
|
+
}
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 合入平台层种子经验(skill 自带的 seeds.json)。已存在同 ID、或与已有经验过于相似的跳过。
|
|
127
|
+
* 种子不带分数,和其他经验一样靠分配的窗口检验。返回本次合入或更新文本的 ID。
|
|
128
|
+
*/
|
|
129
|
+
export function mergeSeeds(state, seeds, now) {
|
|
130
|
+
const added = [];
|
|
131
|
+
for (const s of seeds?.lessons ?? []) {
|
|
132
|
+
const cur = state.lessons[s.id];
|
|
133
|
+
if (cur) {
|
|
134
|
+
if (cur.source !== 'seed') continue; // Agent 自己学的同号经验不碰
|
|
135
|
+
// 种子文本和平台检验结论随 skill 更新时同步(本机检验统计保留)
|
|
136
|
+
const platform = s.platform ?? null;
|
|
137
|
+
const textChanged = cur.trigger !== s.trigger || cur.rule !== s.rule || cur.boundary !== s.boundary;
|
|
138
|
+
const platformChanged = JSON.stringify(cur.platform ?? null) !== JSON.stringify(platform);
|
|
139
|
+
if (!textChanged && !platformChanged) continue;
|
|
140
|
+
Object.assign(cur, { trigger: s.trigger, rule: s.rule, boundary: s.boundary, platform });
|
|
141
|
+
if (!HELD.has(cur.status)) cur.status = statusOf(cur, now);
|
|
142
|
+
added.push(s.id);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const text = `${s.trigger} ${s.rule}`;
|
|
146
|
+
const dup = Object.values(state.lessons).some((l) => l.status !== 'archived' && l.role === s.role && l.decisionType === s.decisionType && similarity(text, `${l.trigger} ${l.rule}`) >= 0.5);
|
|
147
|
+
if (dup) continue;
|
|
148
|
+
const lesson = state.lessons[s.id] = {
|
|
149
|
+
id: s.id, layer: s.layer, role: s.role, decisionType: s.decisionType, ...(s.camp ? { camp: s.camp } : {}),
|
|
150
|
+
trigger: s.trigger, rule: s.rule, boundary: s.boundary, source: 'seed', platform: s.platform ?? null,
|
|
151
|
+
score: 0, stats: emptyStats(), status: 'candidate', createdAt: now, evidence: [],
|
|
152
|
+
};
|
|
153
|
+
lesson.status = statusOf(lesson, now);
|
|
154
|
+
added.push(s.id);
|
|
155
|
+
}
|
|
156
|
+
return added;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const bigrams = (s) => {
|
|
160
|
+
const t = String(s).replace(/[\s,。、;:,.;:!!??“”"'()()]/g, '');
|
|
161
|
+
const out = new Set();
|
|
162
|
+
for (let i = 0; i < t.length - 1; i += 1) out.add(t.slice(i, i + 2));
|
|
163
|
+
return out;
|
|
164
|
+
};
|
|
165
|
+
export function similarity(a, b) {
|
|
166
|
+
const x = bigrams(a), y = bigrams(b);
|
|
167
|
+
if (!x.size || !y.size) return 0;
|
|
168
|
+
let inter = 0;
|
|
169
|
+
for (const g of x) if (y.has(g)) inter += 1;
|
|
170
|
+
return inter / (x.size + y.size - inter);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** NEW 证据是否足够:≥2 局各有结果,或同时有 good 与 bad */
|
|
174
|
+
function evidenceSufficient(ws) {
|
|
175
|
+
const scored = ws.filter(({ w }) => w.outcome);
|
|
176
|
+
const games = new Set(scored.map(({ w }) => w.gameId));
|
|
177
|
+
const kinds = new Set(scored.map(({ w }) => w.outcome));
|
|
178
|
+
return (scored.length >= 2 && games.size >= 2) || kinds.size === 2;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const matches = (lesson, w) =>
|
|
182
|
+
w.decisionType === lesson.decisionType && (lesson.role === 'ANY' || lesson.role === w.role) && (lesson.layer === 'L1' || w.self);
|
|
183
|
+
|
|
184
|
+
const campOfRole = (role) => (role === 'WOLF' ? 'WOLF' : 'GOOD');
|
|
185
|
+
const WOLF_TEXT = /狼队|作为狼|狼人视角|抗推|藏狼|倒钩|冲锋/;
|
|
186
|
+
/**
|
|
187
|
+
* 经验适用的阵营。role=ANY 的经验也基本只对一方成立(“不要盲目跟票”对知道身份的狼没有意义),
|
|
188
|
+
* NEW 时按证据窗口的多数阵营记下;更早的经验按文本判断。
|
|
189
|
+
*/
|
|
190
|
+
export function campOfLesson(l) {
|
|
191
|
+
if (l.role !== 'ANY') return campOfRole(l.role);
|
|
192
|
+
return l.camp ?? (WOLF_TEXT.test(`${l.trigger}${l.rule}`) ? 'WOLF' : 'GOOD');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** 触发条件写明“首日 / 次日”时只分配对应天数的窗口,其余天数大概率是“不适用” */
|
|
196
|
+
export function maxRoundOf(l) {
|
|
197
|
+
const t = String(l.trigger ?? '');
|
|
198
|
+
if (/次日|第二天|第2天|前两天/.test(t)) return 2;
|
|
199
|
+
if (/首日|第一天|第1天|首轮/.test(t)) return 1;
|
|
200
|
+
return Infinity;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 预言家首日决策每局只有一个窗口,自己当预言家的局太少;L1 经验也用其他座位的预言家窗口检验
|
|
204
|
+
const assignable = (l, w) => (w.self || (w.decisionType === 'SEER_CLAIM' && l.layer === 'L1')) && w.outcome && matches(l, w) && campOfRole(w.role) === campOfLesson(l) && w.round <= maxRoundOf(l);
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 给经验分配本批次要标注的窗口:自己的、有结果的、角色/阵营/决策类型/天数匹配的窗口,按局轮流取,每条最多 MAX_ASSIGNED_WINDOWS 个。
|
|
208
|
+
* lessons 的顺序即优先级;最多 MAX_ASSIGNED_LESSONS 条。返回 { [lessonId]: [windowId] }
|
|
209
|
+
*/
|
|
210
|
+
export function assignWindows(lessons, windows) {
|
|
211
|
+
const out = {};
|
|
212
|
+
for (const l of lessons) {
|
|
213
|
+
if (Object.keys(out).length >= MAX_ASSIGNED_LESSONS) break;
|
|
214
|
+
const byGame = new Map();
|
|
215
|
+
for (const w of windows) if (assignable(l, w)) byGame.set(w.gameId, [...(byGame.get(w.gameId) ?? []), w]);
|
|
216
|
+
const queues = [...byGame.values()];
|
|
217
|
+
const picked = [];
|
|
218
|
+
for (let i = 0; picked.length < MAX_ASSIGNED_WINDOWS && queues.some((q) => q.length > i); i += 1) {
|
|
219
|
+
for (const q of queues) if (q[i] && picked.length < MAX_ASSIGNED_WINDOWS) picked.push(q[i].id);
|
|
220
|
+
}
|
|
221
|
+
if (picked.length) out[l.id] = picked;
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* 校验并应用一份 proposal。全部通过才修改 state;否则返回 errors,state 不变。
|
|
227
|
+
* @param {object} state lessons 状态(schema 2,会被原地修改)
|
|
228
|
+
* @param {object} brief evo brief 生成的工作单 JSON(含 windows、assignments)
|
|
229
|
+
* @param {object} proposal 模型提交:{ batchId, items: [...] }
|
|
230
|
+
* @param {string} now
|
|
231
|
+
* @param {(role, decisionType) => number|null} baseline 基线 good 率
|
|
232
|
+
* @param {object} [opts]
|
|
233
|
+
* @param {boolean} [opts.lenientNew] 不合格的 NEW 丢弃而不是让整批失败(最后一次提交时用)
|
|
234
|
+
*/
|
|
235
|
+
export function applyProposal(state, brief, proposal, now, baseline = baselineOf({}), { lenientNew = false } = {}) {
|
|
236
|
+
const errors = [];
|
|
237
|
+
const dropped = [];
|
|
238
|
+
const err = (i, field, msg) => errors.push({ item: i, field, msg });
|
|
239
|
+
if (proposal?.batchId !== brief.batchId) err(null, 'batchId', `应为 ${brief.batchId}`);
|
|
240
|
+
if (state.appliedBatches.includes(brief.batchId)) return { duplicate: true, errors: [], changes: [] };
|
|
241
|
+
const items = proposal?.items;
|
|
242
|
+
if (!Array.isArray(items)) err(null, 'items', '必须是数组(没有分配窗口且没有可学的内容时传 [])');
|
|
243
|
+
if (errors.length) return { errors, changes: [] };
|
|
244
|
+
if (items.length > MAX_ITEMS) err(null, 'items', `最多 ${MAX_ITEMS} 条`);
|
|
245
|
+
if (items.filter((x) => x?.action === 'NEW').length > MAX_NEW) err(null, 'items', `NEW 最多 ${MAX_NEW} 条`);
|
|
246
|
+
|
|
247
|
+
const windows = new Map(brief.windows.map((w) => [w.id, w]));
|
|
248
|
+
const assignments = brief.assignments ?? {};
|
|
249
|
+
const applied = new Set();
|
|
250
|
+
const plan = [];
|
|
251
|
+
items.forEach((it, i) => {
|
|
252
|
+
if (it?.action === 'NEW') {
|
|
253
|
+
const before = errors.length;
|
|
254
|
+
if (!['L1', 'L2'].includes(it.layer)) err(i, 'layer', 'L1(通用战术)或 L2(个人执行习惯)');
|
|
255
|
+
if (![...ROLES, 'ANY'].includes(it.role)) err(i, 'role', `取值 ${[...ROLES, 'ANY'].join('/')}`);
|
|
256
|
+
if (!DECISION_TYPES.includes(it.decisionType)) err(i, 'decisionType', `取值 ${DECISION_TYPES.join('/')}`);
|
|
257
|
+
for (const k of Object.keys(LIMITS)) {
|
|
258
|
+
const v = it.lesson?.[k];
|
|
259
|
+
if (typeof v !== 'string' || !v.trim()) err(i, `lesson.${k}`, '必填');
|
|
260
|
+
else if (v.length > LIMITS[k]) err(i, `lesson.${k}`, `不超过 ${LIMITS[k]} 字`);
|
|
261
|
+
else if (LEAK_PATTERNS.some((re) => re.test(v))) err(i, `lesson.${k}`, '不得出现具体座位号或某人的身份结论,只写可复用的判断方法');
|
|
262
|
+
else if (ABSOLUTE.test(v)) err(i, `lesson.${k}`, '不要用绝对化措辞');
|
|
263
|
+
}
|
|
264
|
+
const ev = Array.isArray(it.evidence) ? it.evidence : [];
|
|
265
|
+
if (!ev.length) err(i, 'evidence', '至少引用 1 个窗口 ID');
|
|
266
|
+
const draft = { layer: it.layer, role: it.role, decisionType: it.decisionType };
|
|
267
|
+
const ws = [];
|
|
268
|
+
for (const ref of ev) {
|
|
269
|
+
const w = windows.get(ref?.id ?? ref);
|
|
270
|
+
if (!w) { err(i, 'evidence', `窗口 ${ref?.id ?? ref} 不在工作单中`); continue; }
|
|
271
|
+
if (errors.length === before && !matches(draft, w)) err(i, 'evidence', `窗口 ${w.id} 的角色/决策类型与经验不符${draft.layer === 'L2' ? '(L2 只能引用自己的窗口)' : ''}`);
|
|
272
|
+
ws.push({ w, followed: ref?.followed === true });
|
|
273
|
+
}
|
|
274
|
+
if (errors.length === before && !evidenceSufficient(ws)) err(i, 'evidence', '证据不足:需要来自至少 2 局的 2 个有结果窗口,或同时包含 good 与 bad 的对照窗口;单局的事后归因不能新建经验');
|
|
275
|
+
if (errors.length === before) {
|
|
276
|
+
const text = `${it.lesson.trigger} ${it.lesson.rule}`;
|
|
277
|
+
for (const l of Object.values(state.lessons)) {
|
|
278
|
+
if (l.status === 'archived' || l.role !== it.role || l.decisionType !== it.decisionType) continue;
|
|
279
|
+
if (similarity(text, `${l.trigger} ${l.rule}`) >= 0.5) err(i, 'lesson', `与已有经验 ${l.id} 过于相似,请改用 APPLY 标注它的窗口`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (errors.length === before) plan.push({ kind: 'NEW', it, ws });
|
|
283
|
+
// 最后一次提交时,不合格的新经验只丢掉这一条、不挡整批:NEW 不计入统计,丢掉没有风险;
|
|
284
|
+
// 否则一条 NEW 写错,同批已经标好的 APPLY 窗口也会跟着作废(10-27 wolf5 b19803 就这样丢了一批)
|
|
285
|
+
else if (lenientNew) dropped.push(...errors.splice(before));
|
|
286
|
+
} else if (it?.action === 'APPLY') {
|
|
287
|
+
const before = errors.length;
|
|
288
|
+
const l = state.lessons[it.lessonId];
|
|
289
|
+
const assigned = assignments[it.lessonId];
|
|
290
|
+
if (!l || l.status === 'archived') { err(i, 'lessonId', `经验 ${it.lessonId} 不存在或已归档`); return; }
|
|
291
|
+
if (!assigned) { err(i, 'lessonId', `经验 ${it.lessonId} 本批次没有分配窗口,不要提交它`); return; }
|
|
292
|
+
if (applied.has(it.lessonId)) { err(i, 'lessonId', `经验 ${it.lessonId} 重复提交`); return; }
|
|
293
|
+
applied.add(it.lessonId);
|
|
294
|
+
const refs = Array.isArray(it.windows) ? it.windows : [];
|
|
295
|
+
const ids = refs.map((r) => r?.id);
|
|
296
|
+
const missing = assigned.filter((id) => !ids.includes(id));
|
|
297
|
+
const extra = ids.filter((id) => !assigned.includes(id));
|
|
298
|
+
if (missing.length) err(i, 'windows', `缺少分配给 ${l.id} 的窗口:${missing.join(', ')}(分配的窗口要逐个标注)`);
|
|
299
|
+
if (extra.length) err(i, 'windows', `窗口 ${extra.join(', ')} 没有分配给 ${l.id},只能标注工作单里分配的窗口`);
|
|
300
|
+
if (new Set(ids).size !== ids.length) err(i, 'windows', '同一窗口只能标注一次');
|
|
301
|
+
for (const ref of refs) {
|
|
302
|
+
if (!FOLLOWED.includes(ref?.followed)) err(i, 'windows', `窗口 ${ref?.id} 的 followed 只能是 true(照做)、false(违背)或 "na"(触发条件不成立)`);
|
|
303
|
+
}
|
|
304
|
+
if (errors.length === before) plan.push({ kind: 'APPLY', lesson: l, refs: refs.map((r) => ({ w: windows.get(r.id), followed: r.followed })) });
|
|
305
|
+
} else {
|
|
306
|
+
err(i, 'action', 'NEW 或 APPLY');
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
const skipped = Object.keys(assignments).filter((id) => !applied.has(id) && state.lessons[id]?.status !== 'archived');
|
|
310
|
+
if (skipped.length) err(null, 'items', `以下经验分配了窗口但没有提交 APPLY:${skipped.join(', ')}(每条都要逐个标注)`);
|
|
311
|
+
if (errors.length) return { errors, changes: [], dropped };
|
|
312
|
+
// 全部通过,开始写入
|
|
313
|
+
const changes = applyStale(state, now).map((d) => ({ ...d, kind: 'STALE' }));
|
|
314
|
+
for (const p of plan) {
|
|
315
|
+
if (p.kind === 'NEW') {
|
|
316
|
+
const id = `${p.it.layer}-${String(state.nextSeq).padStart(4, '0')}`;
|
|
317
|
+
state.nextSeq += 1;
|
|
318
|
+
const lesson = state.lessons[id] = {
|
|
319
|
+
id, layer: p.it.layer, role: p.it.role, decisionType: p.it.decisionType,
|
|
320
|
+
trigger: p.it.lesson.trigger.trim(), rule: p.it.lesson.rule.trim(), boundary: p.it.lesson.boundary.trim(),
|
|
321
|
+
score: 0, stats: emptyStats(), status: 'candidate', createdAt: now, evidence: [],
|
|
322
|
+
};
|
|
323
|
+
if (lesson.role === 'ANY') {
|
|
324
|
+
const wolves = p.ws.filter(({ w }) => w.role === 'WOLF').length;
|
|
325
|
+
lesson.camp = wolves * 2 > p.ws.length ? 'WOLF' : 'GOOD';
|
|
326
|
+
}
|
|
327
|
+
// NEW 的证据窗口只记录来源,不计入统计
|
|
328
|
+
for (const { w, followed } of p.ws) lesson.evidence.push({ batchId: brief.batchId, gameId: w.gameId, windowId: w.id, followed, outcome: w.outcome, counted: false });
|
|
329
|
+
changes.push({ kind: 'NEW', id });
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
const { lesson } = p;
|
|
333
|
+
lesson.stats ??= emptyStats();
|
|
334
|
+
const s = lesson.stats;
|
|
335
|
+
const seen = new Set(lesson.evidence.map((e) => e.windowId));
|
|
336
|
+
let counted = 0;
|
|
337
|
+
for (const { w, followed } of p.refs) {
|
|
338
|
+
if (seen.has(w.id)) continue;
|
|
339
|
+
const pBase = baseline(w.role, w.decisionType);
|
|
340
|
+
let use = false;
|
|
341
|
+
if (followed === true && pBase != null && (s.perGame[w.gameId] ?? 0) < PER_GAME) {
|
|
342
|
+
use = true;
|
|
343
|
+
s.perGame[w.gameId] = (s.perGame[w.gameId] ?? 0) + 1;
|
|
344
|
+
s.n += 1;
|
|
345
|
+
s.good += w.outcome === 'good' ? 1 : 0;
|
|
346
|
+
s.expected += pBase;
|
|
347
|
+
s.variance += pBase * (1 - pBase);
|
|
348
|
+
counted += 1;
|
|
349
|
+
} else if (followed === false) s.violated += 1;
|
|
350
|
+
else if (followed === 'na') s.na += 1;
|
|
351
|
+
lesson.evidence.push({ batchId: brief.batchId, gameId: w.gameId, windowId: w.id, followed, outcome: w.outcome, p: pBase, counted: use });
|
|
352
|
+
}
|
|
353
|
+
if (counted) lesson.lastScoredAt = now;
|
|
354
|
+
lesson.score = liftOf(s);
|
|
355
|
+
if (!HELD.has(lesson.status)) lesson.status = statusOf(lesson, now);
|
|
356
|
+
if (lesson.evidence.length > 80) lesson.evidence = lesson.evidence.slice(-80);
|
|
357
|
+
changes.push({ kind: 'APPLY', id: lesson.id, counted, n: s.n, score: lesson.score, z: Math.round(zOf(s) * 100) / 100, status: lesson.status });
|
|
358
|
+
}
|
|
359
|
+
state.appliedBatches.push(brief.batchId);
|
|
360
|
+
if (state.appliedBatches.length > 200) state.appliedBatches = state.appliedBatches.slice(-200);
|
|
361
|
+
return { errors: [], changes, dropped };
|
|
362
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// 汇总指标,口径与 docs/.../p0-baseline.py 一致(实际命中率 vs 随机期望)。
|
|
2
|
+
class Rate {
|
|
3
|
+
constructor() { this.n = 0; this.hit = 0; this.exp = 0; }
|
|
4
|
+
add(hit, exp) { this.n += 1; this.hit += hit ? 1 : 0; this.exp += exp ?? 0; }
|
|
5
|
+
out() {
|
|
6
|
+
if (!this.n) return null;
|
|
7
|
+
const r = (x) => Math.round(x * 1000) / 1000;
|
|
8
|
+
return { n: this.n, rate: r(this.hit / this.n), random: r(this.exp / this.n), lift: r((this.hit - this.exp) / this.n) };
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** @param {object[]} outcomes 按 gameId 升序 */
|
|
13
|
+
export function summarize(outcomes) {
|
|
14
|
+
const goodVote = new Rate(), goodVoteR1 = new Rate(), wolfVoteGood = new Rate(), poison = new Rate(), shot = new Rate();
|
|
15
|
+
let votes = 0, abstain = 0, wolfGames = 0, wolfVotedOut = 0, goodGames = 0, goodWins = 0, wolfWins = 0;
|
|
16
|
+
const surprise = {};
|
|
17
|
+
for (const o of outcomes) {
|
|
18
|
+
if (o.camp === 'WOLF') {
|
|
19
|
+
wolfGames += 1;
|
|
20
|
+
if (o.won) wolfWins += 1;
|
|
21
|
+
if (o.death?.cause === 'VOTED_OUT') wolfVotedOut += 1;
|
|
22
|
+
} else {
|
|
23
|
+
goodGames += 1;
|
|
24
|
+
if (o.won) goodWins += 1;
|
|
25
|
+
}
|
|
26
|
+
for (const v of o.votes) {
|
|
27
|
+
votes += 1;
|
|
28
|
+
if (v.abstain) { abstain += 1; continue; }
|
|
29
|
+
if (v.hitWolf !== undefined) {
|
|
30
|
+
goodVote.add(v.hitWolf, v.random);
|
|
31
|
+
if (v.type === 'VOTE' && v.round === 1) goodVoteR1.add(v.hitWolf, v.random);
|
|
32
|
+
} else if (v.onGood !== undefined) {
|
|
33
|
+
wolfVoteGood.add(v.onGood, v.random);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
for (const s of o.shots) (s.kind === 'WITCH_POISON' ? poison : shot).add(s.hitWolf, s.random);
|
|
37
|
+
for (const f of o.surprise ?? []) surprise[f] = (surprise[f] ?? 0) + 1;
|
|
38
|
+
}
|
|
39
|
+
const ratio = (a, b) => (b ? Math.round((a / b) * 1000) / 1000 : null);
|
|
40
|
+
return {
|
|
41
|
+
games: outcomes.length,
|
|
42
|
+
winGood: ratio(goodWins, goodGames),
|
|
43
|
+
winWolf: ratio(wolfWins, wolfGames),
|
|
44
|
+
wolfVotedOut: ratio(wolfVotedOut, wolfGames),
|
|
45
|
+
goodVote: goodVote.out(),
|
|
46
|
+
goodVoteR1: goodVoteR1.out(),
|
|
47
|
+
wolfVoteGood: wolfVoteGood.out(),
|
|
48
|
+
witchPoison: poison.out(),
|
|
49
|
+
hunterShot: shot.out(),
|
|
50
|
+
abstainRate: ratio(abstain, votes),
|
|
51
|
+
surprise,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function ledgerOutcomes(ledger) {
|
|
56
|
+
return Object.values(ledger.games).sort((a, b) => Number(a.gameId) - Number(b.gameId));
|
|
57
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// 给主人(认领人)发私聊通知:经验被撤下、投影被整体关闭时使用。只用本 Agent 自己的 token。
|
|
2
|
+
// clientMessageId 由调用方给出的 key 决定,同一事件重复调用不会重复发送。
|
|
3
|
+
export async function notifyOwner(ctx, text, key, { fetchImpl = globalThis.fetch, timeoutMs = 15000 } = {}) {
|
|
4
|
+
const headers = { Authorization: `Bearer ${ctx.token}`, Accept: 'application/json' };
|
|
5
|
+
const call = async (method, path, body) => {
|
|
6
|
+
const res = await fetchImpl(ctx.gateway + path, {
|
|
7
|
+
method,
|
|
8
|
+
headers: body ? { ...headers, 'Content-Type': 'application/json' } : headers,
|
|
9
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
10
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
11
|
+
});
|
|
12
|
+
const json = await res.json().catch(() => null);
|
|
13
|
+
if (!res.ok || json?.code !== 200) throw new Error(`${method} ${path} -> HTTP ${res.status} code ${json?.code} ${json?.message ?? ''}`);
|
|
14
|
+
return json.data;
|
|
15
|
+
};
|
|
16
|
+
const claimerId = (await call('GET', `/api/users/${encodeURIComponent(ctx.agentId)}/profile`))?.identity?.claimerId;
|
|
17
|
+
if (!claimerId) throw new Error('未认领,没有可通知的主人');
|
|
18
|
+
await call('POST', '/api/chat/agent/messages/private', {
|
|
19
|
+
targetUserId: Number(claimerId), targetUserType: 'HUMAN', content: text, messageType: 'TEXT', clientMessageId: `evo:${ctx.agentId}:${key}`,
|
|
20
|
+
});
|
|
21
|
+
return { claimerId: String(claimerId) };
|
|
22
|
+
}
|