@@ -12,13 +12,23 @@ import {
1212 readdirSync ,
1313 statSync ,
1414 unlinkSync ,
15+ writeFileSync ,
1516} from "fs" ;
1617import { dirname , join , resolve } from "path" ;
1718import { tmpdir } from "os" ;
1819import { fileURLToPath } from "url" ;
1920
2021const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
21- const sidecarDir = join ( __dirname , ".." , "src-tauri" , "sidecars" ) ;
22+ const readArg = ( name ) => {
23+ const raw = process . argv . slice ( 2 ) ;
24+ const direct = raw . find ( ( arg ) => arg . startsWith ( `${ name } =` ) ) ;
25+ if ( direct ) return direct . split ( "=" ) [ 1 ] ;
26+ const index = raw . indexOf ( name ) ;
27+ if ( index >= 0 && raw [ index + 1 ] ) return raw [ index + 1 ] ;
28+ return null ;
29+ } ;
30+ const sidecarOverride = process . env . OPENWORK_SIDECAR_DIR ?. trim ( ) || readArg ( "--outdir" ) ;
31+ const sidecarDir = sidecarOverride ? resolve ( sidecarOverride ) : join ( __dirname , ".." , "src-tauri" , "sidecars" ) ;
2232const packageJsonPath = resolve ( __dirname , ".." , "package.json" ) ;
2333const opencodeVersion = ( ( ) => {
2434 if ( process . env . OPENCODE_VERSION ?. trim ( ) ) return process . env . OPENCODE_VERSION . trim ( ) ;
@@ -43,7 +53,6 @@ const owpenbotVersion = (() => {
4353 }
4454 return null ;
4555} ) ( ) ;
46- const owpenbotAssetOverride = process . env . OWPENBOT_ASSET ?. trim ( ) || null ;
4756
4857// Target triple for native platform binaries
4958const resolvedTargetTriple = ( ( ) => {
@@ -116,11 +125,16 @@ const resolveBuildScript = (dir) => {
116125const owpenbotBaseName = "owpenbot" ;
117126const owpenbotName = process . platform === "win32" ? `${ owpenbotBaseName } .exe` : owpenbotBaseName ;
118127const owpenbotPath = join ( sidecarDir , owpenbotName ) ;
128+ const owpenbotBuildName = bunTarget
129+ ? `${ owpenbotBaseName } -${ bunTarget } ${ bunTarget . includes ( "windows" ) ? ".exe" : "" } `
130+ : owpenbotName ;
131+ const owpenbotBuildPath = join ( sidecarDir , owpenbotBuildName ) ;
119132const owpenbotTargetTriple = resolvedTargetTriple ;
120133const owpenbotTargetName = owpenbotTargetTriple
121134 ? `${ owpenbotBaseName } -${ owpenbotTargetTriple } ${ owpenbotTargetTriple . includes ( "windows" ) ? ".exe" : "" } `
122135 : null ;
123136const owpenbotTargetPath = owpenbotTargetName ? join ( sidecarDir , owpenbotTargetName ) : null ;
137+ const owpenbotDir = resolve ( __dirname , ".." , ".." , "owpenbot" ) ;
124138const readHeader = ( filePath , length = 256 ) => {
125139 const fd = openSync ( filePath , "r" ) ;
126140 try {
@@ -404,173 +418,112 @@ if (shouldDownloadOpencode) {
404418 console . log ( `OpenCode sidecar updated to ${ normalizedOpencodeVersion } .` ) ;
405419}
406420
421+ const owpenbotPkgRaw = readFileSync ( resolve ( owpenbotDir , "package.json" ) , "utf8" ) ;
422+ const owpenbotPkg = JSON . parse ( owpenbotPkgRaw ) ;
423+ const owpenbotPkgVersion = String ( owpenbotPkg . version ?? "" ) . trim ( ) ;
407424const normalizedOwpenbotVersion = owpenbotVersion ?. startsWith ( "v" )
408425 ? owpenbotVersion . slice ( 1 )
409426 : owpenbotVersion ;
427+ const expectedOwpenbotVersion = normalizedOwpenbotVersion || owpenbotPkgVersion ;
410428
411- if ( ! normalizedOwpenbotVersion ) {
412- console . error (
413- "Owpenbot version is not configured. Set OWPENBOT_VERSION or add owpenbotVersion to packages/desktop/package.json."
414- ) ;
429+ if ( ! expectedOwpenbotVersion ) {
430+ console . error ( "Owpenbot version missing. Set owpenbotVersion or ensure package.json has version." ) ;
415431 process . exit ( 1 ) ;
416432}
417433
418- const owpenbotAssetByTarget = {
419- "aarch64-apple-darwin" : "owpenbot-darwin-arm64.zip" ,
420- "x86_64-apple-darwin" : "owpenbot-darwin-x64.zip" ,
421- "x86_64-unknown-linux-gnu" : "owpenbot-linux-x64.tar.gz" ,
422- "aarch64-unknown-linux-gnu" : "owpenbot-linux-arm64.tar.gz" ,
423- "x86_64-pc-windows-msvc" : "owpenbot-windows-x64.zip" ,
424- "aarch64-pc-windows-msvc" : "owpenbot-windows-x64.zip" ,
425- } ;
426-
427- const owpenbotAsset =
428- owpenbotAssetOverride ?? ( resolvedTargetTriple ? owpenbotAssetByTarget [ resolvedTargetTriple ] : null ) ;
429-
430- const owpenbotUrl = owpenbotAsset
431- ? `https://github.com/different-ai/owpenbot/releases/download/v${ normalizedOwpenbotVersion } /${ owpenbotAsset } `
432- : null ;
433-
434- const owpenbotCandidatePath = owpenbotTargetPath ?? owpenbotPath ;
435- const existingOwpenbotVersion =
436- owpenbotCandidatePath && existsSync ( owpenbotCandidatePath )
437- ? readBinaryVersion ( owpenbotCandidatePath )
438- : null ;
439-
440- const shouldDownloadOwpenbot =
441- ! owpenbotCandidatePath ||
442- ! existsSync ( owpenbotCandidatePath ) ||
443- isStubBinary ( owpenbotCandidatePath ) ||
444- ! existingOwpenbotVersion ||
445- existingOwpenbotVersion !== normalizedOwpenbotVersion ;
446-
447- if ( ! shouldDownloadOwpenbot ) {
448- console . log ( `Owpenbot sidecar already present (${ existingOwpenbotVersion } ).` ) ;
434+ if ( normalizedOwpenbotVersion && owpenbotPkgVersion && normalizedOwpenbotVersion !== owpenbotPkgVersion ) {
435+ console . error ( `Owpenbot version mismatch: desktop=${ normalizedOwpenbotVersion } , package=${ owpenbotPkgVersion } ` ) ;
436+ process . exit ( 1 ) ;
449437}
450438
451- if ( shouldDownloadOwpenbot ) {
452- if ( ! owpenbotAsset || ! owpenbotUrl ) {
453- console . error (
454- `No owpenbot asset configured for target ${ resolvedTargetTriple ?? "unknown" } . Set OWPENBOT_ASSET to override.`
455- ) ;
456- process . exit ( 1 ) ;
457- }
458-
439+ const shouldBuildOwpenbot = ! existsSync ( owpenbotBuildPath ) || isStubBinary ( owpenbotBuildPath ) ;
440+ if ( shouldBuildOwpenbot ) {
459441 mkdirSync ( sidecarDir , { recursive : true } ) ;
460-
461- const stamp = Date . now ( ) ;
462- const archivePath = join ( tmpdir ( ) , `owpenbot-${ stamp } -${ owpenbotAsset } ` ) ;
463- const extractDir = join ( tmpdir ( ) , `owpenbot-${ stamp } ` ) ;
464- const checksumUrl = `https://github.com/different-ai/owpenbot/releases/download/v${ normalizedOwpenbotVersion } /SHA256SUMS` ;
465- const checksumPath = join ( tmpdir ( ) , `owpenbot-${ stamp } -SHA256SUMS` ) ;
466-
467- mkdirSync ( extractDir , { recursive : true } ) ;
468-
469- if ( process . platform === "win32" ) {
470- const psQuote = ( value ) => `'${ value . replace ( / ' / g, "''" ) } '` ;
471- const downloadScript = [
472- "$ErrorActionPreference = 'Stop'" ,
473- `Invoke-WebRequest -Uri ${ psQuote ( owpenbotUrl ) } -OutFile ${ psQuote ( archivePath ) } ` ,
474- ] . join ( "; " ) ;
475- const checksumScript = [
476- "$ErrorActionPreference = 'Stop'" ,
477- `Invoke-WebRequest -Uri ${ psQuote ( checksumUrl ) } -OutFile ${ psQuote ( checksumPath ) } ` ,
478- ] . join ( "; " ) ;
479-
480- const downloadResult = spawnSync ( "powershell" , [ "-NoProfile" , "-Command" , downloadScript ] , {
481- stdio : "inherit" ,
482- } ) ;
483- if ( downloadResult . status !== 0 ) {
484- process . exit ( downloadResult . status ?? 1 ) ;
485- }
486-
487- const checksumResult = spawnSync ( "powershell" , [ "-NoProfile" , "-Command" , checksumScript ] , {
488- stdio : "inherit" ,
489- } ) ;
490- if ( checksumResult . status !== 0 ) {
491- process . exit ( checksumResult . status ?? 1 ) ;
492- }
493- } else {
494- const downloadResult = spawnSync ( "curl" , [ "-fsSL" , "-o" , archivePath , owpenbotUrl ] , {
495- stdio : "inherit" ,
496- } ) ;
497- if ( downloadResult . status !== 0 ) {
498- process . exit ( downloadResult . status ?? 1 ) ;
499- }
500-
501- const checksumResult = spawnSync ( "curl" , [ "-fsSL" , "-o" , checksumPath , checksumUrl ] , {
502- stdio : "inherit" ,
503- } ) ;
504- if ( checksumResult . status !== 0 ) {
505- process . exit ( checksumResult . status ?? 1 ) ;
442+ if ( existsSync ( owpenbotBuildPath ) ) {
443+ try {
444+ unlinkSync ( owpenbotBuildPath ) ;
445+ } catch {
446+ // ignore
506447 }
507448 }
508-
509- const checksumContent = readFileSync ( checksumPath , "utf8" ) ;
510- const expectedHash = parseChecksum ( checksumContent , owpenbotAsset ) ;
511- if ( ! expectedHash ) {
512- console . error ( `Owpenbot checksum missing for ${ owpenbotAsset } .` ) ;
513- process . exit ( 1 ) ;
514- }
515- const actualHash = sha256File ( archivePath ) ;
516- if ( actualHash !== expectedHash ) {
517- console . error ( `Owpenbot checksum mismatch for ${ owpenbotAsset } .` ) ;
449+ const owpenbotScript = resolveBuildScript ( owpenbotDir ) ;
450+ if ( ! existsSync ( owpenbotScript ) ) {
451+ console . error ( `Owpenbot build script not found at ${ owpenbotScript } ` ) ;
518452 process . exit ( 1 ) ;
519453 }
520-
521- if ( process . platform === "win32" ) {
522- const psQuote = ( value ) => `'${ value . replace ( / ' / g, "''" ) } '` ;
523- const extractScript = [
524- "$ErrorActionPreference = 'Stop'" ,
525- `Expand-Archive -Path ${ psQuote ( archivePath ) } -DestinationPath ${ psQuote ( extractDir ) } -Force` ,
526- ] . join ( "; " ) ;
527- const extractResult = spawnSync ( "powershell" , [ "-NoProfile" , "-Command" , extractScript ] , {
528- stdio : "inherit" ,
529- } ) ;
530- if ( extractResult . status !== 0 ) {
531- process . exit ( extractResult . status ?? 1 ) ;
532- }
533- } else if ( owpenbotAsset . endsWith ( ".zip" ) ) {
534- const unzipResult = spawnSync ( "unzip" , [ "-q" , archivePath , "-d" , extractDir ] , {
535- stdio : "inherit" ,
536- } ) ;
537- if ( unzipResult . status !== 0 ) {
538- process . exit ( unzipResult . status ?? 1 ) ;
539- }
540- } else if ( owpenbotAsset . endsWith ( ".tar.gz" ) ) {
541- const tarResult = spawnSync ( "tar" , [ "-xzf" , archivePath , "-C" , extractDir ] , {
542- stdio : "inherit" ,
543- } ) ;
544- if ( tarResult . status !== 0 ) {
545- process . exit ( tarResult . status ?? 1 ) ;
546- }
547- } else {
548- console . error ( `Unknown owpenbot archive type: ${ owpenbotAsset } ` ) ;
549- process . exit ( 1 ) ;
454+ const owpenbotArgs = [ owpenbotScript , "--outdir" , sidecarDir , "--filename" , "owpenbot" ] ;
455+ if ( bunTarget ) {
456+ owpenbotArgs . push ( "--target" , bunTarget ) ;
550457 }
551-
552- const extractedBinary = findOwpenbotBinary ( extractDir ) ;
553- if ( ! extractedBinary ) {
554- console . error ( "Owpenbot binary not found after extraction." ) ;
555- process . exit ( 1 ) ;
458+ const result = spawnSync ( "bun" , owpenbotArgs , { cwd : owpenbotDir , stdio : "inherit" } ) ;
459+ if ( result . status !== 0 ) {
460+ process . exit ( result . status ?? 1 ) ;
556461 }
462+ }
557463
558- const owpenbotTargets = [ owpenbotTargetPath , owpenbotPath ] . filter ( Boolean ) ;
559- for ( const target of owpenbotTargets ) {
464+ if ( existsSync ( owpenbotBuildPath ) ) {
465+ const shouldCopyCanonical = ! existsSync ( owpenbotPath ) || isStubBinary ( owpenbotPath ) ;
466+ if ( shouldCopyCanonical && owpenbotBuildPath !== owpenbotPath ) {
560467 try {
561- if ( existsSync ( target ) ) {
562- unlinkSync ( target ) ;
563- }
468+ if ( existsSync ( owpenbotPath ) ) unlinkSync ( owpenbotPath ) ;
564469 } catch {
565470 // ignore
566471 }
567- copyFileSync ( extractedBinary , target ) ;
568- try {
569- chmodSync ( target , 0o755 ) ;
570- } catch {
571- // ignore
472+ copyFileSync ( owpenbotBuildPath , owpenbotPath ) ;
473+ }
474+
475+ if ( owpenbotTargetPath ) {
476+ const shouldCopyTarget = ! existsSync ( owpenbotTargetPath ) || isStubBinary ( owpenbotTargetPath ) ;
477+ if ( shouldCopyTarget && owpenbotBuildPath !== owpenbotTargetPath ) {
478+ try {
479+ if ( existsSync ( owpenbotTargetPath ) ) unlinkSync ( owpenbotTargetPath ) ;
480+ } catch {
481+ // ignore
482+ }
483+ copyFileSync ( owpenbotBuildPath , owpenbotTargetPath ) ;
572484 }
573485 }
486+ }
487+
488+ const openworkServerVersion = ( ( ) => {
489+ try {
490+ const raw = readFileSync ( resolve ( openworkServerDir , "package.json" ) , "utf8" ) ;
491+ return String ( JSON . parse ( raw ) . version ?? "" ) . trim ( ) ;
492+ } catch {
493+ return null ;
494+ }
495+ } ) ( ) ;
496+
497+ const versions = {
498+ opencode : {
499+ version : normalizedOpencodeVersion ,
500+ sha256 : opencodeCandidatePath && existsSync ( opencodeCandidatePath ) ? sha256File ( opencodeCandidatePath ) : null ,
501+ } ,
502+ "openwork-server" : {
503+ version : openworkServerVersion ,
504+ sha256 : existsSync ( openworkServerPath ) ? sha256File ( openworkServerPath ) : null ,
505+ } ,
506+ owpenbot : {
507+ version : expectedOwpenbotVersion ,
508+ sha256 : existsSync ( owpenbotPath ) ? sha256File ( owpenbotPath ) : null ,
509+ } ,
510+ } ;
574511
575- console . log ( `Owpenbot sidecar updated to ${ normalizedOwpenbotVersion } .` ) ;
512+ const missing = Object . entries ( versions )
513+ . filter ( ( [ , info ] ) => ! info . version || ! info . sha256 )
514+ . map ( ( [ name ] ) => name ) ;
515+
516+ if ( missing . length ) {
517+ console . error ( `Sidecar version metadata incomplete for: ${ missing . join ( ", " ) } ` ) ;
518+ process . exit ( 1 ) ;
519+ }
520+
521+ const versionsPath = join ( sidecarDir , "versions.json" ) ;
522+ try {
523+ mkdirSync ( sidecarDir , { recursive : true } ) ;
524+ const content = JSON . stringify ( versions , null , 2 ) + "\n" ;
525+ writeFileSync ( versionsPath , content , "utf8" ) ;
526+ } catch ( error ) {
527+ console . error ( `Failed to write versions.json: ${ error } ` ) ;
528+ process . exit ( 1 ) ;
576529}
0 commit comments