Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

3.7 KB
Raw Blame
#!/usr/bin/env node

import * as esbuild from 'esbuild';
import * as fs from 'fs';
import * as path from 'path';
import { execFileSync } from 'child_process';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootDir = path.join(__dirname, '..');

async function buildViewer() {
  console.log('Building React viewer...');

  try {
    await esbuild.build({
      entryPoints: [path.join(rootDir, 'src/ui/viewer/index.tsx')],
      bundle: true,
      minify: true,
      sourcemap: false,
      target: ['es2020'],
      format: 'iife',
      outfile: path.join(rootDir, 'plugin/ui/viewer-bundle.js'),
      jsx: 'automatic',
      loader: {
        '.tsx': 'tsx',
        '.ts': 'ts'
      },
      define: {
        'process.env.NODE_ENV': '"production"'
      }
    });

    const htmlTemplate = fs.readFileSync(
      path.join(rootDir, 'src/ui/viewer-template.html'),
      'utf-8'
    );
    // The pre-paint theme script is built from src/ui/theme-choice.ts (the key and choices' one definition), not typed
    // into the template. esbuild turns the TypeScript module into one the build can import; it throws when the
    // template has no mark, which fails the build.
    const prePaint = await esbuild.build({
      entryPoints: [path.join(rootDir, 'src/ui/viewer/utils/theme-pre-paint.ts')],
      bundle: true,
      write: false,
      format: 'esm',
      platform: 'neutral',
      logLevel: 'error'
    });
    const { withViewerThemePrePaintScript } = await import(
      `data:text/javascript;base64,${Buffer.from(prePaint.outputFiles[0].text).toString('base64')}`
    );
    fs.writeFileSync(
      path.join(rootDir, 'plugin/ui/viewer.html'),
      withViewerThemePrePaintScript(htmlTemplate)
    );

    const fontsDir = path.join(rootDir, 'src/ui/viewer/assets/fonts');
    const outputFontsDir = path.join(rootDir, 'plugin/ui/assets/fonts');

    if (fs.existsSync(fontsDir)) {
      fs.mkdirSync(outputFontsDir, { recursive: true });
      const fontFiles = fs.readdirSync(fontsDir);
      for (const file of fontFiles) {
        fs.copyFileSync(
          path.join(fontsDir, file),
          path.join(outputFontsDir, file)
        );
      }
    }

    const srcUiDir = path.join(rootDir, 'src/ui');
    const outputUiDir = path.join(rootDir, 'plugin/ui');
    // The My Work React app (src/ui/my-work) has its own package.json and lockfile; Vite writes it
    // to plugin/ui/work-view with fonts bundled locally, so an offline install renders immediately.
    // execFileSync throws on a non-zero exit, which fails the build. Windows needs a shell to run npm.cmd.
    const myWorkAppDir = path.join(srcUiDir, 'my-work');
    const npmRunOptions = { cwd: myWorkAppDir, stdio: 'inherit', shell: process.platform === 'win32' };
    execFileSync('npm', ['ci', '--no-audit', '--no-fund'], npmRunOptions);
    execFileSync('npm', ['run', 'build'], npmRunOptions);
    const iconFiles = fs.readdirSync(srcUiDir).filter(file => file.startsWith('icon-thick-') && file.endsWith('.svg'));
    for (const file of iconFiles) {
      fs.copyFileSync(
        path.join(srcUiDir, file),
        path.join(outputUiDir, file)
      );
    }

    console.log('✓ React viewer built successfully');
    console.log('  - plugin/ui/viewer-bundle.js');
    console.log('  - plugin/ui/viewer.html (from viewer-template.html, with the pre-paint theme script)');
    console.log('  - plugin/ui/work-view/* (My Work app, Vite build of src/ui/my-work)');
    console.log('  - plugin/ui/assets/fonts/* (font files)');
    console.log(`  - plugin/ui/icon-thick-*.svg (${iconFiles.length} icon files)`);
  } catch (error) {
    console.error('Failed to build viewer:', error);
    process.exit(1);
  }
}

buildViewer();