File size: 22,336 Bytes
d4758d3
5383ef0
 
 
 
8dde1b0
5383ef0
 
 
 
 
1a4d797
5383ef0
 
6cf0e88
8a206ae
 
 
 
6cf0e88
 
8a206ae
6cf0e88
 
 
 
8a206ae
6cf0e88
8a206ae
 
 
 
6cf0e88
 
 
 
8a206ae
 
 
 
6cf0e88
5383ef0
 
8dde1b0
 
5383ef0
 
caef4d9
 
 
 
8dde1b0
5383ef0
 
 
 
 
 
 
 
 
 
12272d1
 
caef4d9
 
 
 
 
 
 
 
 
 
 
 
 
 
5383ef0
 
 
 
 
 
 
 
 
 
ad573c0
 
5383ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad573c0
5383ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0aa618
5383ef0
08380b7
 
5383ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a206ae
 
 
 
08fd421
 
 
8a206ae
 
 
 
 
 
5383ef0
 
 
 
 
 
1a4d797
5383ef0
 
a16dc1f
 
35318fb
 
 
a16dc1f
 
1a4d797
12272d1
 
 
 
 
 
8a206ae
 
6a9c39c
8a206ae
 
a16dc1f
 
8a206ae
 
 
 
 
 
 
 
a16dc1f
 
 
8a206ae
 
1baccaa
 
8a206ae
 
5383ef0
1a4d797
 
 
 
 
 
5383ef0
 
 
8a206ae
 
5383ef0
35318fb
 
 
 
 
5383ef0
 
 
 
 
 
 
 
 
 
35318fb
8a206ae
 
35318fb
8a206ae
 
 
35318fb
 
 
 
 
8a206ae
 
6a9c39c
8a206ae
4768abb
8a206ae
 
4768abb
8a206ae
 
 
 
 
 
 
 
 
6cf0e88
 
 
 
 
8a206ae
6a9c39c
 
 
35318fb
 
 
 
 
 
 
6a9c39c
5383ef0
 
 
 
 
 
 
 
8a206ae
 
 
 
 
 
 
 
 
6a9c39c
8a206ae
 
 
 
 
 
 
 
 
6cf0e88
 
 
 
 
8a206ae
6a9c39c
 
5383ef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cf0e88
 
 
 
5383ef0
12272d1
 
 
 
 
 
 
6a9c39c
6cf0e88
 
 
6a9c39c
6cf0e88
6a9c39c
6cf0e88
 
 
 
 
6a9c39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a206ae
 
6a9c39c
 
 
a16dc1f
6a9c39c
 
 
 
 
 
8a206ae
35318fb
 
 
 
 
 
 
 
 
 
 
8a206ae
 
 
 
 
6a9c39c
8a206ae
 
 
6cf0e88
8a206ae
 
 
 
 
6cf0e88
 
8a206ae
 
 
 
6cf0e88
8a206ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import OpenAI from 'openai';
import { safeSend, broadcastToUser } from './helpers.js';
import { LIGHTNING_BASE, PUBLIC_URL } from './config.js';
import { sessionStore, deviceSessionStore } from './sessionStore.js';
import { rateLimiter } from './rateLimiter.js';
import { initGuestRequestLimiter, consumeGuestRequest } from './guestRequestLimiter.js';
import {
  verifySupabaseToken, getUserSettings, saveUserSettings,
  getUserProfile, setUsername, getSubscriptionInfo,
  getTierConfig, getUsageInfo,
} from './auth.js';
import { streamChat, extractSessionName } from './chatStream.js';
import crypto from 'crypto';

/**
 * Message Structure: Tree-based with versioned tails
 * 
 * Each message has versions, and each version has a complete tail of subsequent messages.
 * Messages only exist within parent tails (no separate flat array).
 * 
 * {
 *   id: "msg-123",
 *   role: "user" | "assistant",
 *   content: string | array,
 *   timestamp: number,
 *   versions: [
 *     {
 *       content: string | array,
 *       tail: [  // Full message objects
 *         { id, role, content, timestamp, versions: [...], currentVersionIdx, ... },
 *         ...
 *       ],
 *       timestamp: number
 *     },
 *     ...
 *   ],
 *   currentVersionIdx: 0,
 *   toolCalls?: [...]
 * }
 */

const activeStreams = new Map();

initGuestRequestLimiter().catch(err => console.error('Failed to initialize guest request limiter:', err));

export async function handleWsMessage(ws, msg, wsClients) {
  const client = wsClients.get(ws); if (!client) return;
  // Require turnstile verification for most message types
  if (!client.verified && msg.type !== 'ping' && msg.type !== 'turnstile:verify') {
    return safeSend(ws, { type: 'error', message: 'turnstile:required' });
  }

  const h = handlers[msg.type];
  if (h) return h(ws, msg, client, wsClients);
  safeSend(ws, { type: 'error', message: `Unknown: ${msg.type}` });
}

function bcast(wsClients, userId, data, excludeWs) {
  broadcastToUser(wsClients, userId, data, excludeWs);
}

const handlers = {
  'ping': (ws) => { safeSend(ws, { type: 'pong' }); },

  'turnstile:verify': async (ws, msg, client) => {
    try {
      const token = msg?.token;
      const secret = process.env.TURNSTILE_SECRET_KEY;
      if (!token || !secret) return safeSend(ws, { type: 'turnstile:error', message: 'Missing token or server not configured' });
      const params = new URLSearchParams(); params.append('secret', secret); params.append('response', token);
      if (client.ip) params.append('remoteip', client.ip);
      const r = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', { method: 'POST', body: params });
      const j = await r.json();
      if (j?.success) { client.verified = true; return safeSend(ws, { type: 'turnstile:ok' }); }
      return safeSend(ws, { type: 'turnstile:error', message: 'Verification failed' });
    } catch (e) { console.error('ws turnstile verify', e); return safeSend(ws, { type: 'turnstile:error', message: 'Server error' }); }
  },

  'auth:login': async (ws, msg, client, wsClients) => {
    const { accessToken, tempId: clientTempId } = msg;
    if (!accessToken) return safeSend(ws, { type: 'auth:error', message: 'Missing token' });
    const user = await verifySupabaseToken(accessToken);
    if (!user) return safeSend(ws, { type: 'auth:error', message: 'Invalid token' });

    client.userId = user.id; client.accessToken = accessToken; client.authenticated = true;
    client.deviceToken = deviceSessionStore.create(user.id, client.ip, client.userAgent);
    sessionStore.markOnline(user.id, ws);

    if (clientTempId) client.tempId = clientTempId;
    const tId = client.tempId;
    await sessionStore.transferTempToUser(tId, user.id, accessToken);

    const [sessions, settings, profile] = await Promise.all([
      sessionStore.loadUserSessions(user.id, accessToken),
      getUserSettings(user.id, accessToken),
      getUserProfile(user.id, accessToken),
    ]);

    safeSend(ws, { type: 'auth:ok', userId: user.id, email: user.email,
      deviceToken: client.deviceToken, sessions: sessions.map(ser), settings, profile });

    bcast(wsClients, user.id, { type: 'auth:newLogin', message: 'New login on your account.',
      ip: client.ip, userAgent: client.userAgent, timestamp: new Date().toISOString() }, ws);
  },

  'auth:logout': (ws, msg, client) => {
    if (client.deviceToken) deviceSessionStore.revoke(client.deviceToken);
    Object.assign(client, { userId: null, authenticated: false, accessToken: null, deviceToken: null });
    safeSend(ws, { type: 'auth:loggedOut' });
  },

  'auth:guest': (ws, msg, client) => {
    const t = msg.tempId || client.tempId;
    client.tempId = t;
    sessionStore.initTemp(t);
    safeSend(ws, { type: 'auth:guestOk', tempId: t, sessions: sessionStore.getTempSessions(t).map(ser) });
  },

  'sessions:list': (ws, msg, client) => {
    const list = client.userId
      ? sessionStore.getUserSessions(client.userId)
      : sessionStore.getTempSessions(client.tempId);
    list.sort((a, b) => b.created - a.created);
    safeSend(ws, { type: 'sessions:list', sessions: list.map(ser) });
  },

  'sessions:create': async (ws, msg, client) => {
    const s = client.userId
      ? await sessionStore.createUserSession(client.userId, client.accessToken)
      : sessionStore.createTempSession(client.tempId);
    safeSend(ws, { type: 'sessions:created', session: ser(s) });
  },

  'sessions:delete': async (ws, msg, client) => {
    if (client.userId) await sessionStore.deleteUserSession(client.userId, client.accessToken, msg.sessionId);
    else sessionStore.deleteTempSession(client.tempId, msg.sessionId);
    safeSend(ws, { type: 'sessions:deleted', sessionId: msg.sessionId });
  },

  'sessions:deleteAll': async (ws, msg, client) => {
    if (client.userId) await sessionStore.deleteAllUserSessions(client.userId, client.accessToken);
    else sessionStore.deleteTempAll(client.tempId);
    safeSend(ws, { type: 'sessions:deletedAll' });
  },

  'sessions:rename': async (ws, msg, client) => {
    const name = (msg.name || '').trim(); if (!name) return;
    if (client.userId)
      await sessionStore.updateUserSession(client.userId, client.accessToken, msg.sessionId, { name });
    else sessionStore.updateTempSession(client.tempId, msg.sessionId, { name });
    safeSend(ws, { type: 'sessions:renamed', sessionId: msg.sessionId, name });
  },

  'sessions:get': (ws, msg, client) => {
    const s = client.userId
      ? sessionStore.getUserSession(client.userId, msg.sessionId)
      : sessionStore.getTempSession(client.tempId, msg.sessionId);
    if (!s) return safeSend(ws, { type: 'error', message: 'Session not found' });
    safeSend(ws, { type: 'sessions:data', session: ser(s) });
  },

  'sessions:share': async (ws, msg, client) => {
    if (!client.userId) return safeSend(ws, { type: 'error', message: 'Sign in to share' });
    const token = await sessionStore.createShareToken(client.userId, client.accessToken, msg.sessionId);
    if (!token) return safeSend(ws, { type: 'error', message: 'Share failed' });
    safeSend(ws, { type: 'sessions:shareUrl', url: `${PUBLIC_URL}/?share=${token}`, sessionId: msg.sessionId });
  },

  'sessions:import': async (ws, msg, client) => {
    if (!client.userId) return safeSend(ws, { type: 'error', message: 'Sign in to import' });
    const s = await sessionStore.importSharedSession(client.userId, client.accessToken, msg.token);
    if (!s) return safeSend(ws, { type: 'error', message: 'Invalid share link' });
    safeSend(ws, { type: 'sessions:imported', session: ser(s) });
  },

  'chat:send': async (ws, msg, client) => {
    const { sessionId, content, tools } = msg;
    if (!client.userId) {
      const allowed = await consumeGuestRequest(client.ip || 'unknown');
      if (!allowed) return safeSend(ws, { type: 'guest:rateLimit', message: 'Guest request limit exceeded' });
      if (!sessionStore.tempCanSend(client.tempId)) return safeSend(ws, { type: 'chat:limitReached' });
      sessionStore.tempBump(client.tempId);
    }
    const session = client.userId
      ? sessionStore.getUserSession(client.userId, sessionId)
      : sessionStore.getTempSession(client.tempId, sessionId);
    if (!session) return safeSend(ws, { type: 'error', message: 'Session not found' });

    if (activeStreams.has(ws)) activeStreams.get(ws).abort();
    const abort = new AbortController();
    activeStreams.set(ws, abort);
    safeSend(ws, { type: 'chat:start', sessionId });

    let fullText = '';
    const assetsCollected = [], toolCallsCollected = [];

    // Extract flat history from tree structure
    const rootMessage = session.history?.[0];
    const flatHistory = rootMessage ? extractFlatHistory(rootMessage) : [];

    await streamChat({
      sessionId,
      model: session.model,
      history: flatHistory, 
      userMessage: content, 
      tools: tools || {},
      accessToken: client.accessToken, 
      clientId: msg.clientId, 
      abortSignal: abort.signal,
      onToken(t) { fullText += t; safeSend(ws, { type: 'chat:token', token: t, sessionId }); },
      onToolCall(call) {
        safeSend(ws, { type: 'chat:toolCall', call, sessionId });
        if (call.state === 'resolved' || call.state === 'canceled') toolCallsCollected.push(call);
      },
      onNewAsset(asset) { safeSend(ws, { type: 'chat:asset', asset, sessionId }); assetsCollected.push(asset); },
      async onDone(text, toolCalls, aborted, sessionNameFromTag) {
        activeStreams.delete(ws);
        const finalText = text || fullText;
        
        // Only create user entry if content was actually provided
        const hasContent = content !== undefined && content !== null && content !== '' && 
          !(Array.isArray(content) && content.length === 0);
        const userEntry = hasContent
          ? buildEntry('user', content)
          : null;

        const resolvedMap = new Map(toolCallsCollected.map(c => [c.id, c]));
        const mergedCalls = (toolCalls || []).map(c => {
          const resolved = resolvedMap.get(c.id) || {};
          return { ...c, state: resolved.state || 'resolved', result: resolved.result };
        });
        const asstEntry = buildEntry('assistant', finalText, mergedCalls);

        // Rebuild tree structure with new messages appended
        let newRootMessage = rootMessage ? validateAndRepairTree(JSON.parse(JSON.stringify(rootMessage))) : null;
        
        if (!newRootMessage) {
          // First message in session - must have user entry
          if (!userEntry) return safeSend(ws, { type: 'error', message: 'No content for first message' });
          newRootMessage = userEntry;
          const asstWrap = { ...asstEntry };
          newRootMessage.versions[0].tail = [asstWrap];
        } else {
          // Append to current tail
          const currentVerIdx = newRootMessage.currentVersionIdx ?? 0;
          let currentTail = newRootMessage.versions[currentVerIdx].tail || [];
          currentTail = JSON.parse(JSON.stringify(currentTail));
          if (userEntry) {
            currentTail.push(userEntry);
          }
          currentTail.push(asstEntry);
          newRootMessage.versions[currentVerIdx].tail = currentTail;
        }

        const newHistory = [newRootMessage];

        let newName = session.name;
        if (sessionNameFromTag) {
          newName = sessionNameFromTag;
        } else if (!session.history?.length || session.name === 'New Chat') {
          newName = session.name;
        }

        if (client.userId)
          await sessionStore.updateUserSession(client.userId, client.accessToken, sessionId, { history: newHistory, name: newName });
        else sessionStore.updateTempSession(client.tempId, sessionId, { history: newHistory, name: newName });
        
        safeSend(ws, { type: aborted ? 'chat:aborted' : 'chat:done', sessionId, name: newName, history: extractFlatHistory(newRootMessage) });
      },
      onError(err) { 
        activeStreams.delete(ws);
        console.error('streamChat error:', err);
        safeSend(ws, { type: 'chat:error', error: String(err), sessionId }); 
      },
    });
  },

  'chat:stop': (ws) => { if (activeStreams.has(ws)) { activeStreams.get(ws).abort(); activeStreams.delete(ws); } },

  'chat:editMessage': async (ws, msg, client) => {
    const { sessionId, messageIndex, newContent } = msg;
    const session = client.userId
      ? sessionStore.getUserSession(client.userId, sessionId)
      : sessionStore.getTempSession(client.tempId, sessionId);
    if (!session) return safeSend(ws, { type: 'error', message: 'Session not found' });
    
    const rootMessage = session.history?.[0];
    if (!rootMessage) return safeSend(ws, { type: 'error', message: 'No history' });
    
    const flatHistory = extractFlatHistory(rootMessage);
    const targetMsg = flatHistory[messageIndex];
    if (!targetMsg) {
      console.error(`chat:editMessage: Message at index ${messageIndex} not found. History length: ${flatHistory.length}`);
      return safeSend(ws, { type: 'error', message: 'Message not found' });
    }
    console.log(`chat:editMessage: Editing message ${targetMsg.id} at index ${messageIndex}`);
    
    // Find the target message in the tree and add new version
    const newRoot = validateAndRepairTree(JSON.parse(JSON.stringify(rootMessage)));
    const found = findAndUpdateMessage(newRoot, targetMsg.id, (msgInTree) => {
      // Add new version with EMPTY tail (no responses yet for this edited version)
      msgInTree.versions.push({
        content: newContent,
        tail: [],  // New version starts fresh, no tail
        timestamp: Date.now()
      });
      msgInTree.currentVersionIdx = msgInTree.versions.length - 1;
      msgInTree.content = newContent;
    });
    
    if (!found) return;
    
    const newHistory = [newRoot];
    if (client.userId) {
      await sessionStore.updateUserSession(client.userId, client.accessToken, sessionId, { history: newHistory });
    } else {
      sessionStore.updateTempSession(client.tempId, sessionId, { history: newHistory });
    }
    
    // Send back the updated message with its ID and the full flat history
    const updatedFlatHistory = extractFlatHistory(newRoot);
    const updatedTargetMsg = updatedFlatHistory[messageIndex];
    
    if (!updatedTargetMsg) {
      console.error(`chat:editMessage: Updated message not found at index ${messageIndex}. Updated history length: ${updatedFlatHistory.length}`);
      return safeSend(ws, { type: 'error', message: 'Failed to apply edit - message lost' });
    }
    
    console.log(`chat:editMessage: Edit complete. Message ${updatedTargetMsg.id} now has ${updatedTargetMsg.versions?.length ?? 0} versions`);
    safeSend(ws, { type: 'chat:messageEdited', sessionId, messageId: targetMsg.id, messageIndex, message: updatedTargetMsg, history: updatedFlatHistory });
  },

  'chat:selectVersion': async (ws, msg, client) => {
    const { sessionId, messageIndex, versionIdx } = msg;
    const session = client.userId
      ? sessionStore.getUserSession(client.userId, sessionId)
      : sessionStore.getTempSession(client.tempId, sessionId);
    if (!session) return;
    
    const rootMessage = session.history?.[0];
    if (!rootMessage) return;
    
    const flatHistory = extractFlatHistory(rootMessage);
    const targetMsg = flatHistory[messageIndex];
    if (!targetMsg || !targetMsg.versions || versionIdx >= targetMsg.versions.length) return;
    
    // Find and update the message in tree, switching to specified version
    const newRoot = validateAndRepairTree(JSON.parse(JSON.stringify(rootMessage)));
    const found = findAndUpdateMessage(newRoot, targetMsg.id, (msgInTree) => {
      msgInTree.currentVersionIdx = versionIdx;
      msgInTree.content = msgInTree.versions[versionIdx].content;
      // Tail is automatically correct since each version has its own tail
    });
    
    if (!found) return;
    
    const newHistory = [newRoot];
    if (client.userId) {
      await sessionStore.updateUserSession(client.userId, client.accessToken, sessionId, { history: newHistory });
    } else {
      sessionStore.updateTempSession(client.tempId, sessionId, { history: newHistory });
    }
    
    // Send back with messageId for clarity
    safeSend(ws, { type: 'chat:versionSelected', sessionId, messageId: targetMsg.id, messageIndex, history: extractFlatHistory(newRoot) });
  },

  'settings:get': async (ws, msg, client) => {
    const s = client.userId
      ? await getUserSettings(client.userId, client.accessToken)
      : { theme: 'dark', webSearch: true, imageGen: true, videoGen: true, audioGen: true };
    safeSend(ws, { type: 'settings:data', settings: s });
  },
  'settings:save': async (ws, msg, client, wsClients) => {
    if (!client.userId) return;
    await saveUserSettings(client.userId, client.accessToken, msg.settings);
    safeSend(ws, { type: 'settings:saved' });
    bcast(wsClients, client.userId, { type: 'settings:updated', settings: msg.settings }, ws);
  },

  'account:getProfile':      async (ws, msg, c) => { if (!c.userId) return; safeSend(ws, { type: 'account:profile', profile: await getUserProfile(c.userId, c.accessToken) }); },
  'account:setUsername':     async (ws, msg, c) => { if (!c.userId) return; safeSend(ws, { type: 'account:usernameResult', ...await setUsername(c.userId, c.accessToken, msg.username) }); },
  'account:getSubscription': async (ws, msg, c) => { if (!c.userId) return; safeSend(ws, { type: 'account:subscription', info: await getSubscriptionInfo(c.accessToken) }); },
  'account:getUsage':        async (ws, msg, c) => { safeSend(ws, { type: 'account:usage', usage: await getUsageInfo(c.accessToken) }); },
  'account:getTierConfig':   async (ws)          => { safeSend(ws, { type: 'account:tierConfig', config: await getTierConfig() }); },
  'account:getSessions':     (ws, msg, c)         => { if (!c.userId) return; safeSend(ws, { type: 'account:deviceSessions', sessions: deviceSessionStore.getForUser(c.userId), currentToken: c.deviceToken }); },
  'account:revokeSession':   (ws, msg, c)         => { if (!c.userId) return; deviceSessionStore.revoke(msg.token); safeSend(ws, { type: 'account:sessionRevoked', token: msg.token }); },
  'account:revokeAllOthers': (ws, msg, c, wsClients) => {
    if (!c.userId) return;
    deviceSessionStore.revokeAllExcept(c.userId, c.deviceToken);
    for (const [ows, oc] of wsClients)
      if (oc.userId === c.userId && ows !== ws) safeSend(ows, { type: 'auth:forcedLogout', reason: 'Session revoked by another device' });
    safeSend(ws, { type: 'account:allOthersRevoked' });
  },
};

function ser(s) { return { id: s.id, name: s.name, created: s.created, history: s.history || [], model: s.model }; }

function generateMessageId() {
  return `msg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}

function buildEntry(role, content, toolCalls = []) {
  const normalizedCalls = toolCalls.map(c => ({
    id:     c.id,
    name:   c.name || c.function?.name,
    args:   c.args ?? (c.function?.arguments ? (() => { try { return JSON.parse(c.function.arguments); } catch { return c.function.arguments; } })() : {}),
    state:  c.state || 'resolved',
    result: c.result,
  }));
  const validContent = (content === undefined || content === null) ? '' : content;
  return { 
    id: generateMessageId(),
    role, 
    content: validContent, 
    timestamp: Date.now(),
    versions: [{ content: validContent, tail: [], timestamp: Date.now() }], 
    currentVersionIdx: 0,
    ...(normalizedCalls.length ? { toolCalls: normalizedCalls } : {}) 
  };
}

/**
 * Validate and repair tree structure after cloning/modification
 * Ensures all messages and versions have valid content property
 */
function validateAndRepairTree(rootMessage) {
  const repair = (msg) => {
    if (!msg) return;
    // Ensure message has content
    if (msg.content === undefined || msg.content === null) {
      msg.content = '';
    }
    // Ensure versions array and each version's content
    if (msg.versions && Array.isArray(msg.versions)) {
      for (const version of msg.versions) {
        if (version.content === undefined || version.content === null) {
          version.content = '';
        }
        // Recursively repair tail messages
        if (version.tail && Array.isArray(version.tail)) {
          for (const tailMsg of version.tail) {
            repair(tailMsg);
          }
        }
      }
    }
  };
  repair(rootMessage);
  return rootMessage;
}

function extractFlatHistory(rootMessage) {
  if (!rootMessage) return [];
  
  // Helper to ensure message has valid content
  const ensureValidContent = (msg) => {
    if (msg.content === undefined || msg.content === null) {
      msg.content = '';
    }
    return msg;
  };
  
  const history = [ensureValidContent(rootMessage)];
  const currentVerIdx = rootMessage.currentVersionIdx ?? 0;
  
  if (!Array.isArray(rootMessage.versions)) {
    console.warn(`extractFlatHistory: Root message ${rootMessage.id} missing versions array`);
    return history;
  }
  
  if (currentVerIdx >= rootMessage.versions.length) {
    console.warn(`extractFlatHistory: Root message currentVersionIdx ${currentVerIdx} out of bounds (${rootMessage.versions.length} versions)`);
    return history;
  }
  
  const currentTail = rootMessage.versions[currentVerIdx]?.tail;
  
  if (currentTail && Array.isArray(currentTail)) {
    const walkTail = (tail) => {
      for (const msg of tail) {
        history.push(ensureValidContent(msg));
        const ver = msg.versions?.[msg.currentVersionIdx ?? 0];
        if (ver?.tail && Array.isArray(ver.tail)) {
          walkTail(ver.tail);
        }
      }
    };
    walkTail(currentTail);
  }
  return history;
}

function findAndUpdateMessage(rootMessage, targetId, updateFn) {
  if (rootMessage.id === targetId) {
    updateFn(rootMessage);
    return true;
  }
  
  const search = (msg) => {
    const verIdx = msg.currentVersionIdx ?? 0;
    const tail = msg.versions?.[verIdx]?.tail;
    if (!tail || !Array.isArray(tail)) return false;
    
    for (const child of tail) {
      if (child.id === targetId) {
        updateFn(child);
        return true;
      }
      if (search(child)) return true;
    }
    return false;
  };
  
  return search(rootMessage);
}