Files
shumai/scripts/build-cli.ts
T
Yiling-J 4ac274cdd4 feat(runner): replace server-side bash sandbox with remote runner (#530)
* feat(runner): replace server-side bash sandbox with remote runner

* fix(runner): address review findings for output bounds, key rotation, defaults, and generated uploads

* chore(docker): remove sandbox privileges and env vars from remaining compose files

* feat(cli): add runner start with bundled SRT sandboxing (#535)

* refactor(agent): expose runner bash without skill gating

* feat(cli): add runner start with optional SRT sandboxing

* refactor(cli): remove runner Python dependency

* feat(cli): bundle SRT and use platform-native runner shells

* refactor(cli): delegate runner processes to Execa

* chore(cli): upgrade @anthropic-ai/sandbox-runtime to 0.0.83

* feat(cli): discover Bash for Windows runners

Use Pi-style Bash discovery and launch behavior, exercise the packaged CLI shell, and wait for pending ZIP metadata reads before closing cancelled downloads.

* fix(runner): enforce ownership and safe reconnects

Gate dispatch on reconciliation, retain cancellations across disconnects, and stop replaced clients. Guard the single-server runner topology with a database lease. Restage missing generated inputs, report signal exits, and bound generated-file cleanup.

* feat(webui): add Korean translations for agent runners

* feat(cli): add runner event logging and --verbose flag (#536)

* feat(cli): add runner event logging and --verbose flag

* fix(cli): log runner capacity rejections accurately and use monotonic duration
2026-11-18 15:58:57 +08:00

133 lines
5.1 KiB
TypeScript

import { createHash } from 'node:crypto'
import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { spawnSync } from 'node:child_process'
import { join, resolve } from 'node:path'
import { parseArgs } from 'node:util'
import { fileURLToPath } from 'node:url'
import { version } from '../package.json'
import { version as cliVersion } from '../apps/cli/package.json'
import { srtBundlePlugin } from './srt-bundle'
export const cliTargets = {
'darwin-arm64': 'bun-darwin-arm64',
'darwin-x64': 'bun-darwin-x64',
'linux-arm64': 'bun-linux-arm64',
'linux-x64': 'bun-linux-x64',
'windows-arm64': 'bun-windows-arm64',
'windows-x64': 'bun-windows-x64',
} as const
export type CliTarget = keyof typeof cliTargets
export const cliTargetNames = Object.keys(cliTargets) as CliTarget[]
export const repoRoot = fileURLToPath(new URL('..', import.meta.url))
export function validateCliVersion(value: string): string {
if (!/^\d+\.\d+\.\d+(?:-[0-9A-Za-z]+(?:[.-][0-9A-Za-z]+)*)?$/.test(value)) {
throw new Error(`Invalid CLI version: ${value}`)
}
return value
}
export function parseCliTarget(value: string): CliTarget {
if (!Object.hasOwn(cliTargets, value)) throw new Error(`Unsupported CLI target: ${value}`)
return value as CliTarget
}
export function archiveName(target: CliTarget, releaseVersion = version): string {
validateCliVersion(releaseVersion)
return `shumai-cli-v${releaseVersion}-${target}.${target.startsWith('windows-') ? 'zip' : 'tar.gz'}`
}
export function sha256(bytes: Uint8Array | string): string {
return createHash('sha256').update(bytes).digest('hex')
}
export function packCliBinary(
staging: string,
outdir: string,
target: CliTarget,
releaseVersion = version,
) {
const executable = target.startsWith('windows-') ? 'shumai-cli.exe' : 'shumai-cli'
const archive = join(outdir, archiveName(target, releaseVersion))
const command = target.startsWith('windows-')
? process.platform === 'win32'
? ['tar', '-a', '-cf', archive, '-C', staging, executable]
: ['zip', '-j', archive, join(staging, executable)]
: ['tar', '-czf', archive, '-C', staging, executable]
const packed = spawnSync(command[0], command.slice(1), { stdio: 'inherit' })
if (packed.error) throw packed.error
if (packed.status !== 0) throw new Error(`Failed to archive ${target}`)
return archive
}
export async function assembleCliRelease(outdir: string, targets = cliTargetNames) {
validateCliVersion(version)
if (cliVersion !== version) throw new Error('Root and CLI package versions must match')
for (const name of ['install.sh', 'install.ps1']) {
const template = await readFile(join(repoRoot, 'scripts', name), 'utf8')
await writeFile(join(outdir, name), template.replaceAll('__SHUMAI_CLI_VERSION__', version))
}
const names = [
...targets.map((target) => archiveName(target)),
'install.sh',
'install.ps1',
].sort()
const lines = await Promise.all(
names.map(async (name) => `${sha256(await readFile(join(outdir, name)))} ${name}\n`),
)
await writeFile(join(outdir, 'checksums.txt'), lines.join(''))
}
export async function buildCliRelease(outdir: string, targets = cliTargetNames) {
validateCliVersion(version)
if (cliVersion !== version) throw new Error('Root and CLI package versions must match')
const notices = await readFile(join(repoRoot, 'apps/cli/THIRD-PARTY-NOTICES.txt'), 'utf8')
await mkdir(outdir, { recursive: true })
const staging = await mkdtemp(join(outdir, '.build-'))
try {
for (const target of targets) {
const executable = target.startsWith('windows-') ? 'shumai-cli.exe' : 'shumai-cli'
const binary = join(staging, executable)
const result = await Bun.build({
entrypoints: [join(repoRoot, 'apps/cli/src/index.ts')],
target: 'bun',
minify: true,
banner: '/*\n' + notices + '*/',
plugins: [srtBundlePlugin(...target.split('-'))],
compile: {
target: cliTargets[target],
outfile: binary,
autoloadDotenv: false,
autoloadBunfig: false,
autoloadPackageJson: false,
autoloadTsconfig: false,
},
define: { 'process.env.NODE_ENV': JSON.stringify('production') },
})
if (!result.success) throw new AggregateError(result.logs, `Failed to compile ${target}`)
await chmod(binary, 0o755)
const archive = join(outdir, archiveName(target))
await rm(archive, { force: true })
packCliBinary(staging, outdir, target)
console.log(`Built ${archive}`)
}
await assembleCliRelease(outdir, targets)
} finally {
await rm(staging, { recursive: true, force: true })
}
}
if (import.meta.main) {
const { values } = parseArgs({
options: {
target: { type: 'string' },
out: { type: 'string', default: 'out/cli-release' },
assemble: { type: 'boolean', default: false },
},
})
const targets = values.target ? [parseCliTarget(values.target)] : cliTargetNames
const outdir = resolve(values.out)
if (values.assemble) await assembleCliRelease(outdir, targets)
else await buildCliRelease(outdir, targets)
}