Repository navigation
Expand file tree
/
Copy pathcongregation.js
More file actions
127 lines (120 loc) · 7.16 KB
/
Copy pathcongregation.js
File metadata and controls
127 lines (120 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// congregation.js — client side of the DAY-1 FOUNDING CONVERSATION (#132b). Once, when a fresh town opens on
// its founding congregation, the founding cast (names + personalities + what each was grown from) is sent to
// /api/ry-farms-congregation, and the returned turn-by-turn script is handed to the sim's congregation director
// (world._foundingScript). DISPLAY TEXT ONLY: the director reads it to choose what each founder SAYS; the sim
// never reads it, so the seeded world stays deterministic whether or not the model answers. Any failure at all
// (offline, no LLM, bad response, slow) simply leaves the director's authored offline pools carrying the scene.
const CONG_ENDPOINT = '/api/ry-farms-congregation';
const CONG_TIMEOUT_MS = 30000; // one generation, kicked at boot; the offline pools cover the wait
let inflight = false;
let doneFor = null; // town seed we've already asked for (once per town, ever)
function shortNameOf(f) { return f.sheet.name.split(' ')[0]; }
// the founder view the model writes against — only what THEY are (knowledge hygiene, like dm.js/conscience.js)
function founderView(f) {
const s = f.sheet, p = s.personality;
return {
name: shortNameOf(f),
archetype: s.archetype || null,
personality: p ? { label: p.label, creed: p.creed } : null,
keepsake: s.memory && String(s.memory.title || '').slice(0, 60),
dream: s.dream ? s.dream.yearn : null,
};
}
// #vote-voice — the DAY-10 ELECTION SCENE. When the real founding gathering opens (roles.foundingPhase ===
// 'gathering', never a rehearsal), the candidate slates (pure reads via world.electionSlates()) are sent with
// scene:'election'; the returned stump speeches land in world._electionScript for the sim's #tickVoteDay
// director. Same contract as the founding conversation: display text only, any failure leaves the authored
// STUMP_LINES pool carrying the scene, and a script that names only some candidates still helps (the director
// falls back per-candidate).
let electionInflight = false;
let electionDoneFor = null; // `${seed}#${day}` we've already asked for (once per vote day)
export async function requestElectionScene(world) {
if (!world || world.rehearsal || !world.roles || world.roles.foundingPhase !== 'gathering') return;
if (typeof world.electionSlates !== 'function') return;
const key = world.seed + '#' + world.day;
if (electionInflight || electionDoneFor === key) return;
const slates = world.electionSlates();
const seeds = [...new Set([...slates.manager, ...slates.watch])];
const cands = seeds.map(s => world.farmers.find(f => f.sheet.seed === s)).filter(Boolean);
if (cands.length < 2) return;
electionInflight = true; electionDoneFor = key;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), CONG_TIMEOUT_MS);
try {
const res = await fetch(CONG_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
scene: 'election',
culture: world.culture,
candidates: cands.map(f => ({
...founderView(f),
standingFor: [slates.manager.includes(f.sheet.seed) && 'manager', slates.watch.includes(f.sheet.seed) && 'watch'].filter(Boolean),
})),
}),
signal: controller.signal,
});
if (!res.ok) throw new Error(`congregation endpoint ${res.status}`);
const data = await res.json();
if (!data || data.fallback || !Array.isArray(data.script)) throw new Error(data?.error || 'no script');
const byName = new Map(cands.map(f => [shortNameOf(f).toLowerCase(), f.sheet.seed]));
const script = [];
for (const t of data.script) {
const seed = byName.get(String(t.speaker || '').trim().toLowerCase());
const text = String(t.line || '').trim();
if (seed != null && text) script.push({ seed, text });
}
// adopt while the gathering still holds — a slow answer landing after dusk is useless
if (script.length >= 2 && world.roles.foundingPhase === 'gathering') world._electionScript = script;
// #delib-variety — the LLM crowd murmurs replace the canned deliberation pool for this vote day
if (Array.isArray(data.mutters) && data.mutters.length >= 4 && world.roles.foundingPhase === 'gathering')
world._electionMutters = data.mutters.map(m => String(m).trim()).filter(Boolean).slice(0, 12);
} catch {
/* any failure: the authored stump pool stands in — nothing to do */
} finally {
clearTimeout(timer);
electionInflight = false;
}
}
// Kick the generation for `world` if it's a fresh founding congregation we haven't asked about yet. Fire-and-
// forget: it stores world._foundingScript when (if) it arrives; the director swaps it in from that turn on.
export async function requestCongregation(world) {
if (!world || typeof world.congregating !== 'function' || !world.congregating()) return;
if (inflight || doneFor === world.seed) return;
const founders = world.farmers.slice(0, 8);
if (founders.length < 2) return;
inflight = true; doneFor = world.seed;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), CONG_TIMEOUT_MS);
try {
const res = await fetch(CONG_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ culture: world.culture, founders: founders.map(founderView) }),
signal: controller.signal,
});
if (!res.ok) throw new Error(`congregation endpoint ${res.status}`);
const data = await res.json();
if (!data || data.fallback || !Array.isArray(data.script)) throw new Error(data?.error || 'no script');
// map each turn's speaker NAME back to a founder seed, so the director voices the intended founder
const byName = new Map(founders.map(f => [shortNameOf(f).toLowerCase(), f.sheet.seed]));
const script = [];
for (const t of data.script) {
const seed = byName.get(String(t.speaker || '').trim().toLowerCase());
const text = String(t.line || '').trim();
if (seed != null && text) script.push({ seed, text });
}
// #Codex29 P1 — mirror the endpoint's coverage gate client-side (defense-in-depth): a script that names
// only a couple of the founders is dropped, so the director's authored pools (which cover everyone) stand
// in rather than a mostly-procedural hybrid. only adopt it if the town is STILL congregating (a slow
// answer that lands after the scene is useless).
const distinct = new Set(script.map(s => s.seed)).size;
const need = Math.min(founders.length, Math.max(4, Math.ceil(founders.length * 0.6)));
if (script.length >= 4 && distinct >= need && world.congregating()) world._foundingScript = script;
} catch {
/* any failure: the director's authored offline pools stand in — nothing to do */
} finally {
clearTimeout(timer);
inflight = false;
}
}