import { spawnSync } from 'node:child_process' import { chmodSync, copyFileSync, mkdirSync, rmSync, writeFileSync } from 'node:fs' import path from 'node:path' const repoRoot = path.resolve(import.meta.dirname, '../..') const packagePath = path.join(repoRoot, 'native', 'computer-use-macos') const binaryPath = path.join(packagePath, '.build', 'release', 'orca-computer-use-macos') const appPath = path.join(packagePath, '.build', 'release', 'Orca Computer Use.app') const appExecutablePath = path.join(appPath, 'Contents', 'MacOS', 'orca-computer-use-macos') const appIconPath = path.join(appPath, 'Contents', 'Resources', 'AppIcon.icns') const entitlementsPath = path.join( repoRoot, 'resources', 'build', 'entitlements.computer-use.mac.plist' ) const bundleId = process.env.ORCA_COMPUTER_MACOS_BUNDLE_ID ?? 'com.stablyai.orca.computer-use' const displayName = 'Orca Computer Use' const signingIdentity = resolveSigningIdentity() const universalTriples = ['arm64-apple-macosx', 'x86_64-apple-macosx'] if (process.platform !== 'darwin') { process.exit(0) } buildUniversalBinary() chmodSync(binaryPath, 0o755) createHelperApp() function buildUniversalBinary() { const builtBinaries = universalTriples.map((triple) => { run('swift', ['build', '-c', 'release', '--package-path', packagePath, '--triple', triple]) return path.join(packagePath, '.build', triple, 'release', 'orca-computer-use-macos') }) mkdirSync(path.dirname(binaryPath), { recursive: true }) run('lipo', ['-create', ...builtBinaries, '-output', binaryPath]) } function createHelperApp() { rmSync(appPath, { recursive: true, force: true }) mkdirSync(path.dirname(appExecutablePath), { recursive: true }) mkdirSync(path.join(appPath, 'Contents', 'Resources'), { recursive: true }) copyFileSync(binaryPath, appExecutablePath) copyFileSync(path.join(repoRoot, 'resources', 'build', 'icon.icns'), appIconPath) chmodSync(appExecutablePath, 0o755) writeFileSync(path.join(appPath, 'Contents', 'Info.plist'), infoPlist(), 'utf8') const signer = spawnSync('codesign', codesignArgs(signingIdentity, appPath), { stdio: 'inherit' }) if (signer.signal) { process.kill(process.pid, signer.signal) } if (signer.status !== 0) { process.exit(signer.status ?? 1) } } function codesignArgs(identity, targetPath) { const args = ['--force', '--deep', '--sign', identity] if (process.env.ORCA_MAC_RELEASE === '1' && identity !== '-') { args.push('--options', 'runtime', '--timestamp', '--entitlements', entitlementsPath) } args.push(targetPath) return args } function resolveSigningIdentity() { const explicitIdentity = process.env.ORCA_COMPUTER_MACOS_SIGN_IDENTITY ?? process.env.CSC_NAME if (explicitIdentity) { return explicitIdentity } const identities = spawnSync('security', ['find-identity', '-v', '-p', 'codesigning'], { encoding: 'utf8' }) if (identities.status !== 0 || !identities.stdout) { return '-' } const developmentMatch = identities.stdout.match(/"([^"]*Apple Development:[^"]+)"/) if (process.env.ORCA_MAC_RELEASE !== '1' && developmentMatch) { return developmentMatch[1] } const releaseMatch = identities.stdout.match(/"([^"]*Developer ID Application:[^"]+)"/) ?? identities.stdout.match(/"([^"]*Apple Distribution:[^"]+)"/) return releaseMatch?.[1] ?? developmentMatch?.[1] ?? '-' } function run(command, args) { const result = spawnSync(command, args, { stdio: 'inherit' }) if (result.signal) { process.kill(process.pid, result.signal) } if (result.status !== 0) { process.exit(result.status ?? 1) } } function infoPlist() { return ` CFBundleDevelopmentRegion en CFBundleExecutable orca-computer-use-macos CFBundleIdentifier ${escapePlist(bundleId)} CFBundleInfoDictionaryVersion 6.0 CFBundleIconFile AppIcon CFBundleName ${escapePlist(displayName)} CFBundleDisplayName ${escapePlist(displayName)} CFBundlePackageType APPL CFBundleShortVersionString 1.0 CFBundleVersion 1 LSMinimumSystemVersion 14.0 LSUIElement NSAccessibilityUsageDescription Orca Computer Use needs Accessibility permission to read and interact with app interfaces when you ask Orca to use apps. NSScreenCaptureUsageDescription Orca Computer Use needs Screen Recording permission to capture app windows when you ask Orca to inspect your screen. ` } function escapePlist(value) { return value .replaceAll('&', '&') .replaceAll('<', '<') .replaceAll('>', '>') .replaceAll('"', '"') .replaceAll("'", ''') }