Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

3.5 KB
Raw Blame
import react from '@vitejs/plugin-react';
import path from 'node:path';
import type { Plugin, ProxyOptions } from 'vite';
import { defaultClientConditions, defineConfig } from 'vite';
import { compression } from 'vite-plugin-compression2';
// Maintained ESM fork of vite-plugin-monaco-editor (works with Vite 6 ESM config).
import monacoEditorPlugin from 'vite-plugin-monaco-editor-esm';

const SERVER = process.env.VITE_SERVER_URL ?? 'http://localhost:8790';
const PORT = Number(process.env.FRONTEND_PORT ?? 3000);
if (!Number.isInteger(PORT)) {
  throw new Error(`FRONTEND_PORT must be an integer, got "${process.env.FRONTEND_PORT}"`);
}

/** Vite writes this into the production shell; the server substitutes at process start. Must not appear in JS identifiers. */
const SHELL_BASE_TOKEN = '%%TRUEFORGE_BASE_PATH%%';

const apiProxy: ProxyOptions = {
  target: SERVER,
  changeOrigin: true,
  // Keep SSE streams open without buffering.
  configure(proxy) {
    proxy.on('proxyRes', proxyRes => {
      if (proxyRes.headers['content-type']?.includes('text/event-stream')) {
        proxyRes.headers['cache-control'] = 'no-cache';
        proxyRes.headers['x-accel-buffering'] = 'no';
      }
    });
  },
};

function shellBaseTokenPlugin(): Plugin {
  return {
    name: 'trueforge-shell-base-token',
    transformIndexHtml: {
      order: 'post',
      handler(html) {
        return html
          .replaceAll('src="./assets/', `src="${SHELL_BASE_TOKEN}assets/`)
          .replaceAll('href="./assets/', `href="${SHELL_BASE_TOKEN}assets/`);
      },
    },
  };
}

export default defineConfig(({ command }) => ({
  base: command === 'build' ? './' : '/',
  plugins: [
    react(),
    monacoEditorPlugin({
      // monaco-editor 0.56+ remaps `./*` → `./esm/vs/*.js`, so the plugin's
      // default `monaco-editor/esm/vs/...` entries double-prefix and fail.
      // Point at current export paths instead (and skip the built-in list).
      languageWorkers: [],
      customWorkers: [
        { label: 'editorWorkerService', entry: 'monaco-editor/editor/editor.worker' },
        { label: 'css', entry: 'monaco-editor/languages/features/css/css.worker' },
        { label: 'html', entry: 'monaco-editor/languages/features/html/html.worker' },
        { label: 'json', entry: 'monaco-editor/languages/features/json/json.worker' },
        { label: 'typescript', entry: 'monaco-editor/languages/features/typescript/ts.worker' },
      ],
      // Production `base` is `./`; without this the plugin writes workers under `./monacoeditorwork`.
      customDistPath: (root, buildOutDir) => path.join(root, buildOutDir, 'monacoeditorwork'),
    }),
    // The server serves these siblings instead of compressing per request.
    compression({
      algorithms: ['br', 'gz'],
      threshold: 1024,
      skipIfLargerOrEqual: true,
    }),
    ...(command === 'build' ? [shellBaseTokenPlugin()] : []),
  ],
  // Single React / assistant-ui Context instance (avoids "requires an AuiProvider").
  resolve: {
    // Never add 'import'/'require' here: Vite applies those per import kind, and
    // forcing 'import' makes CJS deps require @babel/runtime's ESM helpers.
    conditions: ['trueforge-dev', ...defaultClientConditions],
    dedupe: ['react', 'react-dom', '@assistant-ui/core', '@assistant-ui/store', '@assistant-ui/react'],
  },
  server: {
    port: PORT,
    // Fail if FRONTEND_PORT is taken — never silently hop to 3001/3010/etc.
    strictPort: true,
    // Proxy API routes (including /api/internal) to the Harness.
    proxy: { '/api': apiProxy },
  },
}));