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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import { json } from '@remix-run/node';
import type { ActionFunction } from '@remix-run/node';
import { exec } from 'child_process';
import { promisify } from 'util';

const execAsync = promisify(exec);

interface UpdateRequestBody {
  branch: string;
  autoUpdate?: boolean;
}

interface UpdateProgress {
  stage: 'fetch' | 'pull' | 'install' | 'build' | 'complete';
  message: string;
  progress?: number;
  error?: string;
  details?: {
    changedFiles?: string[];
    additions?: number;
    deletions?: number;
    commitMessages?: string[];
    totalSize?: string;
    currentCommit?: string;
    remoteCommit?: string;
    updateReady?: boolean;
    changelog?: string;
    compareUrl?: string;
  };
}

export const action: ActionFunction = async ({ request }) => {
  if (request.method !== 'POST') {
    return json({ error: 'Method not allowed' }, { status: 405 });
  }

  try {
    const body = await request.json();

    if (!body || typeof body !== 'object' || !('branch' in body) || typeof body.branch !== 'string') {
      return json({ error: 'Invalid request body: branch is required and must be a string' }, { status: 400 });
    }

    const { branch, autoUpdate = false } = body as UpdateRequestBody;

    // Create a ReadableStream to send progress updates
    const stream = new ReadableStream({
      async start(controller) {
        const encoder = new TextEncoder();
        const sendProgress = (update: UpdateProgress) => {
          controller.enqueue(encoder.encode(JSON.stringify(update) + '\n'));
        };

        try {
          // Initial check stage
          sendProgress({
            stage: 'fetch',
            message: 'Checking repository status...',
            progress: 0,
          });

          // Check if remote exists
          let defaultBranch = branch || 'main'; // Make branch mutable

          try {
            await execAsync('git remote get-url upstream');
            sendProgress({
              stage: 'fetch',
              message: 'Repository remote verified',
              progress: 10,
            });
          } catch {
            throw new Error(
              'No upstream repository found. Please set up the upstream repository first by running:\ngit remote add upstream https://github.com/stackblitz-labs/bolt.diy.git',
            );
          }

          // Get default branch if not specified
          if (!branch) {
            sendProgress({
              stage: 'fetch',
              message: 'Detecting default branch...',
              progress: 20,
            });

            try {
              const { stdout } = await execAsync('git remote show upstream | grep "HEAD branch" | cut -d" " -f5');
              defaultBranch = stdout.trim() || 'main';
              sendProgress({
                stage: 'fetch',
                message: `Using branch: ${defaultBranch}`,
                progress: 30,
              });
            } catch {
              defaultBranch = 'main'; // Fallback to main if we can't detect
              sendProgress({
                stage: 'fetch',
                message: 'Using default branch: main',
                progress: 30,
              });
            }
          }

          // Fetch stage
          sendProgress({
            stage: 'fetch',
            message: 'Fetching latest changes...',
            progress: 40,
          });

          // Fetch all remotes
          await execAsync('git fetch --all');
          sendProgress({
            stage: 'fetch',
            message: 'Remote changes fetched',
            progress: 50,
          });

          // Check if remote branch exists
          try {
            await execAsync(`git rev-parse --verify upstream/${defaultBranch}`);
            sendProgress({
              stage: 'fetch',
              message: 'Remote branch verified',
              progress: 60,
            });
          } catch {
            throw new Error(
              `Remote branch 'upstream/${defaultBranch}' not found. Please ensure the upstream repository is properly configured.`,
            );
          }

          // Get current commit hash and remote commit hash
          sendProgress({
            stage: 'fetch',
            message: 'Comparing versions...',
            progress: 70,
          });

          const { stdout: currentCommit } = await execAsync('git rev-parse HEAD');
          const { stdout: remoteCommit } = await execAsync(`git rev-parse upstream/${defaultBranch}`);

          // If we're on the same commit, no update is available
          if (currentCommit.trim() === remoteCommit.trim()) {
            sendProgress({
              stage: 'complete',
              message: 'No updates available. You are on the latest version.',
              progress: 100,
              details: {
                currentCommit: currentCommit.trim().substring(0, 7),
                remoteCommit: remoteCommit.trim().substring(0, 7),
              },
            });
            return;
          }

          sendProgress({
            stage: 'fetch',
            message: 'Analyzing changes...',
            progress: 80,
          });

          // Initialize variables
          let changedFiles: string[] = [];
          let commitMessages: string[] = [];
          let stats: RegExpMatchArray | null = null;
          let totalSizeInBytes = 0;

          // Format size for display
          const formatSize = (bytes: number) => {
            if (bytes === 0) {
              return '0 B';
            }

            const k = 1024;
            const sizes = ['B', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));

            return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
          };

          // Get list of changed files and their sizes
          try {
            const { stdout: diffOutput } = await execAsync(
              `git diff --name-status ${currentCommit.trim()}..${remoteCommit.trim()}`,
            );
            const files = diffOutput.split('\n').filter(Boolean);

            if (files.length === 0) {
              sendProgress({
                stage: 'complete',
                message: `No file changes detected between your version and upstream/${defaultBranch}. You might be on a different branch.`,
                progress: 100,
                details: {
                  currentCommit: currentCommit.trim().substring(0, 7),
                  remoteCommit: remoteCommit.trim().substring(0, 7),
                },
              });
              return;
            }

            sendProgress({
              stage: 'fetch',
              message: `Found ${files.length} changed files, calculating sizes...`,
              progress: 90,
            });

            // Get size information for each changed file
            for (const line of files) {
              const [status, file] = line.split('\t');

              if (status !== 'D') {
                // Skip deleted files
                try {
                  const { stdout: sizeOutput } = await execAsync(`git cat-file -s ${remoteCommit.trim()}:${file}`);
                  const size = parseInt(sizeOutput) || 0;
                  totalSizeInBytes += size;
                } catch {
                  console.debug(`Could not get size for file: ${file}`);
                }
              }
            }

            changedFiles = files.map((line) => {
              const [status, file] = line.split('\t');
              return `${status === 'M' ? 'Modified' : status === 'A' ? 'Added' : 'Deleted'}: ${file}`;
            });
          } catch (err) {
            console.debug('Failed to get changed files:', err);
            throw new Error(`Failed to compare changes with upstream/${defaultBranch}. Are you on the correct branch?`);
          }

          // Get commit messages between current and remote
          try {
            const { stdout: logOutput } = await execAsync(
              `git log --pretty=format:"%h|%s|%aI" ${currentCommit.trim()}..${remoteCommit.trim()}`,
            );

            // Parse and group commits by type
            const commits = logOutput
              .split('\n')
              .filter(Boolean)
              .map((line) => {
                const [hash, subject, timestamp] = line.split('|');
                let type = 'other';
                let message = subject;

                if (subject.startsWith('feat:') || subject.startsWith('feature:')) {
                  type = 'feature';
                  message = subject.replace(/^feat(?:ure)?:/, '').trim();
                } else if (subject.startsWith('fix:')) {
                  type = 'fix';
                  message = subject.replace(/^fix:/, '').trim();
                } else if (subject.startsWith('docs:')) {
                  type = 'docs';
                  message = subject.replace(/^docs:/, '').trim();
                } else if (subject.startsWith('style:')) {
                  type = 'style';
                  message = subject.replace(/^style:/, '').trim();
                } else if (subject.startsWith('refactor:')) {
                  type = 'refactor';
                  message = subject.replace(/^refactor:/, '').trim();
                } else if (subject.startsWith('perf:')) {
                  type = 'perf';
                  message = subject.replace(/^perf:/, '').trim();
                } else if (subject.startsWith('test:')) {
                  type = 'test';
                  message = subject.replace(/^test:/, '').trim();
                } else if (subject.startsWith('build:')) {
                  type = 'build';
                  message = subject.replace(/^build:/, '').trim();
                } else if (subject.startsWith('ci:')) {
                  type = 'ci';
                  message = subject.replace(/^ci:/, '').trim();
                }

                return {
                  hash,
                  type,
                  message,
                  timestamp: new Date(timestamp),
                };
              });

            // Group commits by type
            const groupedCommits = commits.reduce(
              (acc, commit) => {
                if (!acc[commit.type]) {
                  acc[commit.type] = [];
                }

                acc[commit.type].push(commit);

                return acc;
              },
              {} as Record<string, typeof commits>,
            );

            // Format commit messages with emojis and timestamps
            const formattedMessages = Object.entries(groupedCommits).map(([type, commits]) => {
              const emoji = {
                feature: '✨',
                fix: 'πŸ›',
                docs: 'πŸ“š',
                style: 'πŸ’Ž',
                refactor: '♻️',
                perf: '⚑',
                test: 'πŸ§ͺ',
                build: 'πŸ› οΈ',
                ci: 'βš™οΈ',
                other: 'πŸ”',
              }[type];

              const title = {
                feature: 'Features',
                fix: 'Bug Fixes',
                docs: 'Documentation',
                style: 'Styles',
                refactor: 'Code Refactoring',
                perf: 'Performance',
                test: 'Tests',
                build: 'Build',
                ci: 'CI',
                other: 'Other Changes',
              }[type];

              return `### ${emoji} ${title}\n\n${commits
                .map((c) => `* ${c.message} (${c.hash.substring(0, 7)}) - ${c.timestamp.toLocaleString()}`)
                .join('\n')}`;
            });

            commitMessages = formattedMessages;
          } catch {
            // Handle silently - empty commitMessages array will be used
          }

          // Get diff stats using the specific commits
          try {
            const { stdout: diffStats } = await execAsync(
              `git diff --shortstat ${currentCommit.trim()}..${remoteCommit.trim()}`,
            );
            stats = diffStats.match(
              /(\d+) files? changed(?:, (\d+) insertions?\\(\\+\\))?(?:, (\d+) deletions?\\(-\\))?/,
            );
          } catch {
            // Handle silently - null stats will be used
          }

          // If we somehow still have no changes detected
          if (!stats && changedFiles.length === 0) {
            sendProgress({
              stage: 'complete',
              message: `No changes detected between your version and upstream/${defaultBranch}. This might be unexpected - please check your git status.`,
              progress: 100,
            });
            return;
          }

          // Fetch changelog
          sendProgress({
            stage: 'fetch',
            message: 'Fetching changelog...',
            progress: 95,
          });

          const changelog = await fetchChangelog(currentCommit.trim(), remoteCommit.trim());

          // We have changes, send the details
          sendProgress({
            stage: 'fetch',
            message: `Changes detected on upstream/${defaultBranch}`,
            progress: 100,
            details: {
              changedFiles,
              additions: stats?.[2] ? parseInt(stats[2]) : 0,
              deletions: stats?.[3] ? parseInt(stats[3]) : 0,
              commitMessages,
              totalSize: formatSize(totalSizeInBytes),
              currentCommit: currentCommit.trim().substring(0, 7),
              remoteCommit: remoteCommit.trim().substring(0, 7),
              updateReady: true,
              changelog,
              compareUrl: `https://github.com/stackblitz-labs/bolt.diy/compare/${currentCommit.trim().substring(0, 7)}...${remoteCommit.trim().substring(0, 7)}`,
            },
          });

          // Only proceed with update if autoUpdate is true
          if (!autoUpdate) {
            sendProgress({
              stage: 'complete',
              message: 'Update is ready to be applied. Click "Update Now" to proceed.',
              progress: 100,
              details: {
                changedFiles,
                additions: stats?.[2] ? parseInt(stats[2]) : 0,
                deletions: stats?.[3] ? parseInt(stats[3]) : 0,
                commitMessages,
                totalSize: formatSize(totalSizeInBytes),
                currentCommit: currentCommit.trim().substring(0, 7),
                remoteCommit: remoteCommit.trim().substring(0, 7),
                updateReady: true,
                changelog,
                compareUrl: `https://github.com/stackblitz-labs/bolt.diy/compare/${currentCommit.trim().substring(0, 7)}...${remoteCommit.trim().substring(0, 7)}`,
              },
            });
            return;
          }

          // Pull stage
          sendProgress({
            stage: 'pull',
            message: `Pulling changes from upstream/${defaultBranch}...`,
            progress: 0,
          });

          await execAsync(`git pull upstream ${defaultBranch}`);

          sendProgress({
            stage: 'pull',
            message: 'Changes pulled successfully',
            progress: 100,
          });

          // Install stage
          sendProgress({
            stage: 'install',
            message: 'Installing dependencies...',
            progress: 0,
          });

          await execAsync('pnpm install');

          sendProgress({
            stage: 'install',
            message: 'Dependencies installed successfully',
            progress: 100,
          });

          // Build stage
          sendProgress({
            stage: 'build',
            message: 'Building application...',
            progress: 0,
          });

          await execAsync('pnpm build');

          sendProgress({
            stage: 'build',
            message: 'Build completed successfully',
            progress: 100,
          });

          // Complete
          sendProgress({
            stage: 'complete',
            message: 'Update completed successfully! Click Restart to apply changes.',
            progress: 100,
          });
        } catch (err) {
          sendProgress({
            stage: 'complete',
            message: 'Update failed',
            error: err instanceof Error ? err.message : 'Unknown error occurred',
          });
        } finally {
          controller.close();
        }
      },
    });

    return new Response(stream, {
      headers: {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        Connection: 'keep-alive',
      },
    });
  } catch (err) {
    console.error('Update preparation failed:', err);
    return json(
      {
        success: false,
        error: err instanceof Error ? err.message : 'Unknown error occurred while preparing update',
      },
      { status: 500 },
    );
  }
};

// Add this function to fetch the changelog
async function fetchChangelog(currentCommit: string, remoteCommit: string): Promise<string> {
  try {
    // First try to get the changelog.md content
    const { stdout: changelogContent } = await execAsync('git show upstream/main:changelog.md');

    // If we have a changelog, return it
    if (changelogContent) {
      return changelogContent;
    }

    // If no changelog.md, generate one in a similar format
    let changelog = '# Changes in this Update\n\n';

    // Get commit messages grouped by type
    const { stdout: commitLog } = await execAsync(
      `git log --pretty=format:"%h|%s|%b" ${currentCommit.trim()}..${remoteCommit.trim()}`,
    );

    const commits = commitLog.split('\n').filter(Boolean);
    const categorizedCommits: Record<string, string[]> = {
      '✨ Features': [],
      'πŸ› Bug Fixes': [],
      'πŸ“š Documentation': [],
      'πŸ’Ž Styles': [],
      '♻️ Code Refactoring': [],
      '⚑ Performance': [],
      'πŸ§ͺ Tests': [],
      'πŸ› οΈ Build': [],
      'βš™οΈ CI': [],
      'πŸ” Other Changes': [],
    };

    // Categorize commits
    for (const commit of commits) {
      const [hash, subject] = commit.split('|');
      let category = 'πŸ” Other Changes';

      if (subject.startsWith('feat:') || subject.startsWith('feature:')) {
        category = '✨ Features';
      } else if (subject.startsWith('fix:')) {
        category = 'πŸ› Bug Fixes';
      } else if (subject.startsWith('docs:')) {
        category = 'πŸ“š Documentation';
      } else if (subject.startsWith('style:')) {
        category = 'πŸ’Ž Styles';
      } else if (subject.startsWith('refactor:')) {
        category = '♻️ Code Refactoring';
      } else if (subject.startsWith('perf:')) {
        category = '⚑ Performance';
      } else if (subject.startsWith('test:')) {
        category = 'πŸ§ͺ Tests';
      } else if (subject.startsWith('build:')) {
        category = 'πŸ› οΈ Build';
      } else if (subject.startsWith('ci:')) {
        category = 'βš™οΈ CI';
      }

      const message = subject.includes(':') ? subject.split(':')[1].trim() : subject.trim();
      categorizedCommits[category].push(`* ${message} (${hash.substring(0, 7)})`);
    }

    // Build changelog content
    for (const [category, commits] of Object.entries(categorizedCommits)) {
      if (commits.length > 0) {
        changelog += `\n## ${category}\n\n${commits.join('\n')}\n`;
      }
    }

    // Add stats
    const { stdout: stats } = await execAsync(`git diff --shortstat ${currentCommit.trim()}..${remoteCommit.trim()}`);

    if (stats) {
      changelog += '\n## πŸ“Š Stats\n\n';
      changelog += `${stats.trim()}\n`;
    }

    return changelog;
  } catch (error) {
    console.error('Error fetching changelog:', error);
    return 'Unable to fetch changelog';
  }
}