-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
392 lines (326 loc) · 15 KB
/
server.js
File metadata and controls
392 lines (326 loc) · 15 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* ============================================================
VISION STACK — Real-Time Workshop Server
Socket.io + Express | Run: node server.js
============================================================ */
const express = require('express');
const { createServer } = require('http');
const { Server } = require('socket.io');
const { randomUUID } = require('crypto');
const path = require('path');
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, { cors: { origin: '*' } });
app.use(express.json());
app.use(express.static(path.join(__dirname)));
// ---- AI SYNTHESIS ------------------------------------------
function buildPrompt(phase, inputs) {
switch (phase) {
case 'principles': {
const list = inputs.ideas.map((v, i) => `${i + 1}. ${v}`).join('\n');
return `Act as an expert organizational designer. Review these team behaviors and cluster them into 4-6 distinct thematic principles. For each theme, provide a punchy, memorable title (2-4 words) and a 1-sentence description of the behavior.
Team behaviors:
${list}
Respond with ONLY a valid JSON array, no markdown, no extra text:
[{"title": "...", "description": "..."}, ...]`;
}
case 'purpose': {
return `Based on these team inputs about who we help, their struggles, and our impact, generate 7 distinct, inspiring Purpose Statements. Each must be exactly one sentence long, evoke emotion, and explain the fundamental reason this team exists.
Who we help: ${inputs.who.join('; ')}
Their biggest struggles: ${inputs.struggle.join('; ')}
The change we create: ${inputs.change.join('; ')}
Respond with ONLY a valid JSON array of exactly 7 strings, no markdown, no extra text:
["statement 1", "statement 2", ...]`;
}
case 'mission': {
const lines = inputs.drafts.map((d, i) =>
`${i + 1}. We build ${d.solution || '...'} for ${d.audience || '...'} so they can ${d.outcome || '...'}`
).join('\n');
return `I am facilitating a team strategy workshop. Analyze these rough mission statement drafts and synthesize the core ideas into exactly 4 polished, concise mission statements. Each should clearly define what we build, who it's for, and the outcome. Use the format "We build [X] for [Y] so they can [Z]."
Drafts:
${lines}
Respond with ONLY a valid JSON array of exactly 4 strings, no markdown, no extra text:
["We build ...", "We build ...", "We build ...", "We build ..."]`;
}
case 'strategy': {
const clusters = [
inputs.clusters.ux.length && `UX & Design: ${inputs.clusters.ux.join(', ')}`,
inputs.clusters.technical.length && `Technical: ${inputs.clusters.technical.join(', ')}`,
inputs.clusters.process.length && `Process & Operations: ${inputs.clusters.process.join(', ')}`,
inputs.clusters.custom.length && `${inputs.customLabel || 'Other'}: ${inputs.clusters.custom.join(', ')}`,
].filter(Boolean).join('\n');
return `We are establishing our strategic pillars. Act as a critical business strategist. First, synthesize these inputs into exactly 3 clear strategic pillars. Then write an aggressive critique: point out what we are failing to prioritize, the risks, and how to make these pillars more rigorous.
Inputs:
${clusters}
Respond with ONLY a valid JSON object, no markdown, no extra text:
{"pillars": [{"title": "...", "description": "..."}, {"title": "...", "description": "..."}, {"title": "...", "description": "..."}], "critique": "..."}`;
}
case 'okrs': {
const objLines = inputs.objectives.map((o, i) => `Objective ${i + 1}: ${o}`).join('\n');
return `Here are our Objectives and rough metric ideas. For each objective, generate exactly 4 distinct, SMART (Specific, Measurable, Achievable, Relevant, Time-bound) Key Results. Make them aggressive but realistic.
${objLines}
Rough metric ideas: ${inputs.metricIdeas.join(', ')}
Respond with ONLY a valid JSON array (one object per objective), no markdown, no extra text:
[{"objective": "...", "keyResults": ["...", "...", "...", "..."]}, ...]`;
}
default:
throw new Error(`Unknown phase: ${phase}`);
}
}
app.get('/api/health', (req, res) => {
res.json({ aiEnabled: !!process.env.ANTHROPIC_API_KEY });
});
function validateInputs(phase, inputs) {
switch (phase) {
case 'principles':
if (!inputs.ideas?.length) return 'Add at least one idea before synthesizing.';
break;
case 'purpose':
if (!inputs.who?.length && !inputs.struggle?.length && !inputs.change?.length)
return 'Add ideas to at least one of the three prompts.';
break;
case 'mission':
if (!inputs.drafts?.length) return 'Fill in at least one mission draft.';
break;
case 'strategy':
if (Object.values(inputs.clusters || {}).every(arr => !arr?.length))
return 'Add at least one idea to any cluster.';
break;
case 'okrs':
if (!inputs.objectives?.length) return 'Add at least one Objective first.';
if (!inputs.metricIdeas?.length) return 'Add at least one metric idea.';
break;
}
return null;
}
app.post('/api/synthesize', async (req, res) => {
const { phase, inputs } = req.body || {};
if (!phase || !inputs) {
return res.status(400).json({ error: 'Missing phase or inputs' });
}
const validationError = validateInputs(phase, inputs);
if (validationError) {
return res.status(400).json({ error: validationError });
}
const apiKey = process.env.ANTHROPIC_API_KEY;
if (!apiKey) {
return res.status(503).json({ error: 'AI synthesis is not configured. Set the ANTHROPIC_API_KEY environment variable and restart the server.' });
}
try {
const Anthropic = require('@anthropic-ai/sdk');
const client = new Anthropic({ apiKey });
const prompt = buildPrompt(phase, inputs);
const message = await client.messages.create({
model: 'claude-opus-4-6',
max_tokens: 2048,
system: 'You are an expert facilitator and organizational strategist. Always respond with raw, valid JSON only — no markdown code fences, no prose, no explanations. Output must be directly parseable by JSON.parse().',
messages: [{ role: 'user', content: prompt }],
});
const raw = message.content[0]?.type === 'text' ? message.content[0].text : '';
const cleaned = raw.replace(/^```json\s*/i, '').replace(/^```\s*/i, '').replace(/\s*```$/i, '').trim();
let result;
try {
result = JSON.parse(cleaned);
} catch (_) {
const m = cleaned.match(/(\[[\s\S]*\]|\{[\s\S]*\})/);
if (!m) throw new Error('AI returned an unparseable response. Please try again.');
result = JSON.parse(m[1]);
}
res.json({ result });
} catch (err) {
console.error(`[AI synthesis] phase=${phase} error:`, err.message);
res.status(500).json({ error: err.message || 'AI synthesis failed' });
}
});
// ---- SESSION STORE -----------------------------------------
const sessions = new Map(); // code -> session
const COLORS = [
'#5b5bd6', '#16a34a', '#d97706', '#dc2626',
'#7c3aed', '#0891b2', '#db2777', '#059669'
];
function makeCode() {
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
let c = '';
for (let i = 0; i < 4; i++) c += chars[Math.floor(Math.random() * chars.length)];
return c;
}
function createSession(teamName, facilitatorName) {
return {
teamName,
facilitatorName,
hostSocketId: null,
phase: 'principles',
stack: { principles: [], purpose: null, mission: null, strategy: [], okrs: [] },
aiResults: { principles: null, purpose: null, mission: null, strategy: null, okrs: null },
selections: { principles: [], purpose: null, mission: null, strategy: [], okrs: [[], []] },
ideas: {
principles: [],
purpose: { who: [], struggle: [], change: [] },
mission: [],
strategy: { ux: [], technical: [], process: [], custom: [] },
customLabel: 'Culture & People',
okrs: { objectives: ['', ''], metrics: [] }
},
participants: new Map() // socketId → { name, color, isFacilitator }
};
}
function getSlotArray(session, phase, slot) {
const ideas = session.ideas;
if (phase === 'principles') return ideas.principles;
if (phase === 'purpose') return ideas.purpose[slot];
if (phase === 'strategy') return ideas.strategy[slot];
if (phase === 'okrs') return ideas.okrs.metrics;
if (phase === 'mission') return ideas.mission;
return null;
}
function serializePresence(session) {
return [...session.participants.entries()].map(([id, p]) => ({ id, ...p }));
}
// ---- SOCKET EVENTS -----------------------------------------
io.on('connection', (socket) => {
let roomCode = null;
let role = null;
// HOST creates a new session
socket.on('host:create', ({ teamName, facilitatorName }) => {
let code;
do { code = makeCode(); } while (sessions.has(code));
const session = createSession(teamName, facilitatorName);
session.hostSocketId = socket.id;
const color = COLORS[0];
session.participants.set(socket.id, { name: facilitatorName || 'Facilitator', color, isFacilitator: true });
sessions.set(code, session);
roomCode = code;
role = 'facilitator';
socket.join(code);
socket.emit('host:ready', { code, myColor: color });
broadcastPresence(code);
});
// PARTICIPANT joins an existing session
socket.on('participant:join', ({ code, name }) => {
code = (code || '').toUpperCase().trim();
const session = sessions.get(code);
if (!session) {
socket.emit('join:error', 'Room not found. Double-check the code and try again.');
return;
}
const usedColors = new Set([...session.participants.values()].map(p => p.color));
const color = COLORS.find(c => !usedColors.has(c)) || COLORS[session.participants.size % COLORS.length];
session.participants.set(socket.id, { name: name || 'Participant', color, isFacilitator: false });
roomCode = code;
role = 'participant';
socket.join(code);
// Send full current state so late joiners are in sync
socket.emit('participant:ready', {
myColor: color,
myName: name,
phase: session.phase,
teamName: session.teamName,
facilitatorName: session.facilitatorName,
ideas: session.ideas,
aiResults: session.aiResults,
selections: session.selections,
stack: session.stack
});
broadcastPresence(code);
});
// ---- IDEA CRUD (any participant, including host) ----------
socket.on('idea:add', ({ phase, slot, value, id }) => {
if (!roomCode) return;
const session = sessions.get(roomCode);
const me = session?.participants.get(socket.id);
if (!session || !me) return;
const arr = getSlotArray(session, phase, slot);
if (!arr) return;
const idea = { id: id || randomUUID(), value, ownerSocketId: socket.id, ownerName: me.name, ownerColor: me.color };
arr.push(idea);
io.to(roomCode).emit('ideas:update', { phase, slot, ideas: arr });
});
socket.on('idea:edit', ({ phase, slot, id, value }) => {
if (!roomCode) return;
const session = sessions.get(roomCode);
const me = session?.participants.get(socket.id);
const arr = getSlotArray(session, phase, slot);
if (!arr || !me) return;
const idea = arr.find(i => i.id === id);
if (idea && (idea.ownerSocketId === socket.id || me.isFacilitator)) {
idea.value = value;
io.to(roomCode).emit('ideas:update', { phase, slot, ideas: arr });
}
});
socket.on('idea:remove', ({ phase, slot, id }) => {
if (!roomCode) return;
const session = sessions.get(roomCode);
const me = session?.participants.get(socket.id);
const arr = getSlotArray(session, phase, slot);
if (!arr || !me) return;
const idx = arr.findIndex(i => i.id === id);
if (idx !== -1 && (arr[idx].ownerSocketId === socket.id || me.isFacilitator)) {
arr.splice(idx, 1);
io.to(roomCode).emit('ideas:update', { phase, slot, ideas: arr });
}
});
// ---- FACILITATOR-ONLY EVENTS -----------------------------
function isHost() { return role === 'facilitator'; }
socket.on('phase:advance', ({ phase }) => {
if (!isHost() || !roomCode) return;
const session = sessions.get(roomCode);
if (session) { session.phase = phase; io.to(roomCode).emit('phase:changed', { phase }); }
});
socket.on('ai:thinking', ({ phase }) => {
if (!isHost() || !roomCode) return;
io.to(roomCode).emit('ai:thinking', { phase });
});
socket.on('ai:result', ({ phase, result }) => {
if (!isHost() || !roomCode) return;
const session = sessions.get(roomCode);
if (session) { session.aiResults[phase] = result; io.to(roomCode).emit('ai:result', { phase, result }); }
});
socket.on('selection:update', ({ phase, selection }) => {
if (!isHost() || !roomCode) return;
const session = sessions.get(roomCode);
if (session) { session.selections[phase] = selection; io.to(roomCode).emit('selection:changed', { phase, selection }); }
});
socket.on('stack:commit', ({ layer, data }) => {
if (!isHost() || !roomCode) return;
const session = sessions.get(roomCode);
if (session) { session.stack[layer] = data; io.to(roomCode).emit('stack:updated', { stack: session.stack }); }
});
socket.on('okr:objectives', ({ objectives }) => {
if (!isHost() || !roomCode) return;
const session = sessions.get(roomCode);
if (session) { session.ideas.okrs.objectives = objectives; socket.to(roomCode).emit('okr:objectives', { objectives }); }
});
socket.on('strategy:customLabel', ({ label }) => {
if (!isHost() || !roomCode) return;
const session = sessions.get(roomCode);
if (session) { session.ideas.customLabel = label; socket.to(roomCode).emit('strategy:customLabel', { label }); }
});
// ---- DISCONNECT ------------------------------------------
socket.on('disconnect', () => {
if (!roomCode) return;
const session = sessions.get(roomCode);
if (!session) return;
session.participants.delete(socket.id);
broadcastPresence(roomCode);
// Clean up empty sessions after 30 min
if (session.participants.size === 0) {
setTimeout(() => {
const s = sessions.get(roomCode);
if (s && s.participants.size === 0) sessions.delete(roomCode);
}, 30 * 60 * 1000);
}
});
function broadcastPresence(code) {
const session = sessions.get(code);
if (!session) return;
io.to(code).emit('presence:update', serializePresence(session));
}
});
// ---- START -------------------------------------------------
const PORT = process.env.PORT || 3000;
httpServer.listen(PORT, () => {
console.log('\n Vision Stack Workshop Server');
console.log(' ────────────────────────────────────────');
console.log(` Local: http://localhost:${PORT}`);
console.log(' Share your 4-letter room code with participants.');
console.log(' For remote workshops, use: npx ngrok http 3000\n');
});