From 91fedf1477356a7c29fb3bce7f821f302783c2ca Mon Sep 17 00:00:00 2001 From: yiling Date: Fri, 12 Jun 2026 21:08:40 +0800 Subject: [PATCH 1/4] fix(cli): resolve all external module imports in global CLI package --- apps/web/src/index.ts | 67 +++++++++++++++++++------- scripts/build-npm.ts | 108 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 138 insertions(+), 37 deletions(-) diff --git a/apps/web/src/index.ts b/apps/web/src/index.ts index c00c70d6..8db017c9 100644 --- a/apps/web/src/index.ts +++ b/apps/web/src/index.ts @@ -1,3 +1,4 @@ +import { join } from 'node:path' import { loadEnvConfig } from '@shumai/core/src/env-loader' loadEnvConfig(process.cwd(), process.env.NODE_ENV !== 'production') @@ -41,25 +42,57 @@ if (process.env.WORKFLOW_EXECUTOR === 'temporal') { Promise.all(queuesToStart.map((q) => workflowService.startWorkers(q))).catch(console.error) } -const server = Bun.serve({ - port: 3000, - fetch: app.fetch, - maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE - ? parseInt(process.env.MAX_REQUEST_BODY_SIZE) - : 1024 * 1024 * 1024 * 10, // Default 10GB - routes: { - // Serve index.html for root - '/': index, +const isProd = process.env.NODE_ENV === 'production' - // Proxy API requests to Hono - '/api/*': app.fetch, - '/files/*': app.fetch, +const server = Bun.serve( + isProd + ? { + port: 3000, + maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE + ? parseInt(process.env.MAX_REQUEST_BODY_SIZE) + : 1024 * 1024 * 1024 * 10, // Default 10GB + development: false, + async fetch(req) { + const url = new URL(req.url) - // Catch-all for SPA routing (fallback to index.html) - '/*': index, - }, - development: process.env.NODE_ENV !== 'production', -}) + if (!url.pathname.startsWith('/api/') && !url.pathname.startsWith('/files/')) { + const filepath = join(import.meta.dir, url.pathname) + const file = Bun.file(filepath) + if (await file.exists()) { + return new Response(file) + } + + const htmlFile = Bun.file(join(import.meta.dir, 'shumai-app.html')) + if (await htmlFile.exists()) { + return new Response(htmlFile, { + headers: { 'Content-Type': 'text/html' }, + }) + } + } + + return app.fetch(req) + }, + } + : { + port: 3000, + maxRequestBodySize: process.env.MAX_REQUEST_BODY_SIZE + ? parseInt(process.env.MAX_REQUEST_BODY_SIZE) + : 1024 * 1024 * 1024 * 10, // Default 10GB + development: true, + fetch: app.fetch, + routes: { + // Serve index.html for root + '/': index, + + // Proxy API requests to Hono + '/api/*': app.fetch, + '/files/*': app.fetch, + + // Catch-all for SPA routing (fallback to index.html) + '/*': index, + }, + }, +) console.log(`šŸš€ Server running at ${server.url}`) diff --git a/scripts/build-npm.ts b/scripts/build-npm.ts index 78a41b9c..20afee32 100644 --- a/scripts/build-npm.ts +++ b/scripts/build-npm.ts @@ -173,17 +173,62 @@ async function buildPlatformPackages( } // Function to build main wrapper package -async function buildMainPackage( - appName: string, - description: string, - hasPrisma: boolean, - extraDependencies: Record = {}, -) { +// Function to build main wrapper package +async function buildMainPackage({ + appName, + description, + hasPrisma, + entrypoint, + plugins = [], + extraDependencies = {}, +}: { + appName: string + description: string + hasPrisma: boolean + entrypoint: string + // eslint-disable-next-line @typescript-eslint/no-explicit-any + plugins?: any[] + extraDependencies?: Record +}) { console.log(`\nšŸ“„ Packaging wrapper app ${appName}...`) const mainOutDir = path.join(distDir, appName) const binOutDir = path.join(mainOutDir, 'bin') await mkdir(binOutDir, { recursive: true }) + // 1. Bundle the JS application code for wrapper + console.log(`šŸ“¦ Bundling JS application code for wrapper ${appName}...`) + const appJsName = `${appName}-app.js` + + const buildResult = await Bun.build({ + entrypoints: [entrypoint], + root: '.', + target: 'bun', + outdir: binOutDir, + minify: true, + naming: `${appName}-app.[ext]`, + plugins, + external: commonExternal, + define: { + 'process.env.NODE_ENV': JSON.stringify('production'), + }, + publicPath: '/', + }) + + if (!buildResult.success) { + console.error(`āŒ JS bundling failed for ${appName}!`) + for (const log of buildResult.logs) { + console.error(log) + } + process.exit(1) + } + + // Confirm that the bundle exists + const finalJsFile = path.join(binOutDir, appJsName) + if (!existsSync(finalJsFile)) { + console.error(`āŒ Could not find compiled JS bundle at ${finalJsFile}`) + process.exit(1) + } + // Write JS wrapper script const wrapperContent = `#!/usr/bin/env node import { spawn } from 'node:child_process' @@ -239,8 +284,14 @@ if (!binaryPath) { process.exit(1) } -const child = spawn(binaryPath, process.argv.slice(2), { +const appJsPath = join(__dirname, '${appName}-app.js') + +const child = spawn(binaryPath, ['run', appJsPath, ...process.argv.slice(2)], { stdio: 'inherit', + env: { + ...process.env, + BUN_BE_BUN: '1', + }, }) child.on('close', (code) => { @@ -301,32 +352,49 @@ child.on('error', (err) => { } // 2. Build shumai (Main app & platform binaries) -await buildPlatformPackages('shumai', './apps/web/src/index.ts', [ +const shumaiPlugins = [ temporalWorkflow({ bundleOptions: {}, }), tailwindPlugin, -]) -await buildMainPackage('shumai', 'A fullstack AI-powered media workspace and workflow engine', true) +] +await buildPlatformPackages('shumai', './apps/web/src/index.ts', shumaiPlugins) +await buildMainPackage({ + appName: 'shumai', + description: 'A fullstack AI-powered media workspace and workflow engine', + hasPrisma: true, + entrypoint: './apps/web/src/index.ts', + plugins: shumaiPlugins, +}) // 3. Build shumai-agent (Agent app & platform binaries) -await buildPlatformPackages('shumai-agent', './apps/agent/src/index.ts', [ +const agentPlugins = [ temporalWorkflow({ bundleOptions: {}, }), -]) -await buildMainPackage('shumai-agent', 'AI worker agent for the shumai media workspace', false) +] +await buildPlatformPackages('shumai-agent', './apps/agent/src/index.ts', agentPlugins) +await buildMainPackage({ + appName: 'shumai-agent', + description: 'AI worker agent for the shumai media workspace', + hasPrisma: false, + entrypoint: './apps/agent/src/index.ts', + plugins: agentPlugins, +}) // 4. Build shumai-transcode (Transcode app & platform binaries) -await buildPlatformPackages('shumai-transcode', './apps/transcode/src/index.ts', [ +const transcodePlugins = [ temporalWorkflow({ bundleOptions: {}, }), -]) -await buildMainPackage( - 'shumai-transcode', - 'Media transcoding worker for the shumai media workspace', - false, -) +] +await buildPlatformPackages('shumai-transcode', './apps/transcode/src/index.ts', transcodePlugins) +await buildMainPackage({ + appName: 'shumai-transcode', + description: 'Media transcoding worker for the shumai media workspace', + hasPrisma: false, + entrypoint: './apps/transcode/src/index.ts', + plugins: transcodePlugins, +}) console.log('\nāœ… All applications and platform binaries built and packaged successfully!') From 84aa1662be6a23f49539fd4682e997eab7c4cb46 Mon Sep 17 00:00:00 2001 From: yiling Date: Fri, 12 Jun 2026 21:24:59 +0800 Subject: [PATCH 2/4] fix(docker): align build output structure and fix web 404 error --- Dockerfile | 2 +- apps/web/src/index.ts | 18 ++++++++++++++++-- build.ts | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index b0509815..1130e2fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -70,4 +70,4 @@ COPY --chown=bun:bun --from=builder /app/prisma.config.ts.prod ./prisma.config.t EXPOSE 3000 # Default command: run migrations and start API server -CMD ["sh", "-c", "bun prisma migrate deploy && bun apps/web/src/index.js"] +CMD ["sh", "-c", "bun prisma migrate deploy && bun index.js"] diff --git a/apps/web/src/index.ts b/apps/web/src/index.ts index 8db017c9..744f967e 100644 --- a/apps/web/src/index.ts +++ b/apps/web/src/index.ts @@ -59,15 +59,29 @@ const server = Bun.serve( const filepath = join(import.meta.dir, url.pathname) const file = Bun.file(filepath) if (await file.exists()) { - return new Response(file) + try { + const stat = await file.stat() + if (stat.isFile()) { + return new Response(file) + } + } catch { + // ignore stat errors + } } - const htmlFile = Bun.file(join(import.meta.dir, 'shumai-app.html')) + const htmlFile = Bun.file(join(import.meta.dir, 'index.html')) if (await htmlFile.exists()) { return new Response(htmlFile, { headers: { 'Content-Type': 'text/html' }, }) } + + const htmlFileNpm = Bun.file(join(import.meta.dir, 'shumai-app.html')) + if (await htmlFileNpm.exists()) { + return new Response(htmlFileNpm, { + headers: { 'Content-Type': 'text/html' }, + }) + } } return app.fetch(req) diff --git a/build.ts b/build.ts index 25ce4446..f5aac79b 100644 --- a/build.ts +++ b/build.ts @@ -20,6 +20,7 @@ await Bun.build({ target: 'bun', outdir: outdir, minify: false, + naming: '[name].[ext]', plugins: [ temporalWorkflow({ bundleOptions: {}, From d06dfc0ab3ba23c3bfffb93b4519da9e884f6dfd Mon Sep 17 00:00:00 2001 From: yiling Date: Fri, 12 Jun 2026 21:50:18 +0800 Subject: [PATCH 3/4] perf(npm): optimize platform binary compilation using dummy runner --- scripts/build-npm.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/scripts/build-npm.ts b/scripts/build-npm.ts index 20afee32..e1005ce5 100644 --- a/scripts/build-npm.ts +++ b/scripts/build-npm.ts @@ -16,6 +16,9 @@ if (existsSync(distDir)) { } await mkdir(distDir, { recursive: true }) +const dummyRunnerPath = path.join(distDir, 'dummy-runner.ts') +await writeFile(dummyRunnerPath, '// Shumai Runner Stub\n', 'utf-8') + const rootPackageJson = JSON.parse(await Bun.file('package.json').text()) const version = rootPackageJson.version || '0.1.0' @@ -358,7 +361,7 @@ const shumaiPlugins = [ }), tailwindPlugin, ] -await buildPlatformPackages('shumai', './apps/web/src/index.ts', shumaiPlugins) +await buildPlatformPackages('shumai', dummyRunnerPath) await buildMainPackage({ appName: 'shumai', description: 'A fullstack AI-powered media workspace and workflow engine', @@ -373,7 +376,7 @@ const agentPlugins = [ bundleOptions: {}, }), ] -await buildPlatformPackages('shumai-agent', './apps/agent/src/index.ts', agentPlugins) +await buildPlatformPackages('shumai-agent', dummyRunnerPath) await buildMainPackage({ appName: 'shumai-agent', description: 'AI worker agent for the shumai media workspace', @@ -388,7 +391,7 @@ const transcodePlugins = [ bundleOptions: {}, }), ] -await buildPlatformPackages('shumai-transcode', './apps/transcode/src/index.ts', transcodePlugins) +await buildPlatformPackages('shumai-transcode', dummyRunnerPath) await buildMainPackage({ appName: 'shumai-transcode', description: 'Media transcoding worker for the shumai media workspace', @@ -397,4 +400,8 @@ await buildMainPackage({ plugins: transcodePlugins, }) +if (existsSync(dummyRunnerPath)) { + await rm(dummyRunnerPath) +} + console.log('\nāœ… All applications and platform binaries built and packaged successfully!') From dc63ab3eaf0814e581d1915d38d6da3f57394fd7 Mon Sep 17 00:00:00 2001 From: yiling Date: Fri, 12 Jun 2026 21:54:15 +0800 Subject: [PATCH 4/4] fix(npm): resolve binary output path mapping in build-npm script --- scripts/build-npm.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/scripts/build-npm.ts b/scripts/build-npm.ts index e1005ce5..fe103644 100644 --- a/scripts/build-npm.ts +++ b/scripts/build-npm.ts @@ -1,4 +1,4 @@ -import { rm, mkdir, cp, writeFile } from 'node:fs/promises' +import { rm, mkdir, cp, writeFile, readdir } from 'node:fs/promises' import { existsSync } from 'node:fs' import path from 'node:path' import tailwindPlugin from 'bun-plugin-tailwind' @@ -115,21 +115,18 @@ async function compileApp({ process.exit(1) } - // Rename generated binary (usually Bun names it 'src' or the folder name of the entrypoint) - const generatedFile = path.join(outdir, 'src') - const generatedFileExe = path.join(outdir, 'src.exe') + // Rename generated binary dynamically by finding the output file in outdir const finalFile = path.join(outdir, binaryName) + const files = await readdir(outdir) + const generatedFilename = files.find((f) => f !== binaryName) - if (existsSync(generatedFile)) { + if (generatedFilename) { + const generatedPath = path.join(outdir, generatedFilename) console.log(`āž”ļø Moving binary to final location: ${finalFile}`) - await cp(generatedFile, finalFile) - await rm(generatedFile) - } else if (existsSync(generatedFileExe)) { - console.log(`āž”ļø Moving binary to final location: ${finalFile}`) - await cp(generatedFileExe, finalFile) - await rm(generatedFileExe) - } else { - console.error(`āŒ Could not find compiled binary at ${generatedFile} or ${generatedFileExe}`) + await cp(generatedPath, finalFile) + await rm(generatedPath) + } else if (!existsSync(finalFile)) { + console.error(`āŒ Could not find compiled binary in ${outdir}`) process.exit(1) } }