1- // @effect -diagnostics nodeBuiltinImport:off - createRequire is the only way the single-executable reaches a file-backed package.
21import * as NodeCrypto from "node:crypto" ;
3- import * as NodeFS from "node:fs" ;
42import * as NodeModule from "node:module" ;
5- import * as NodePath from "node:path" ;
63
74import type { CuaDriverMcpConfiguration , DesktopCuaDriverReport } from "@t3tools/contracts" ;
85import type { EmbeddedCuaDriverHost , EmbeddedDriverConnection } from "@trycua/cua-driver/embedded" ;
96import * as Context from "effect/Context" ;
107import * as Deferred from "effect/Deferred" ;
118import * as Effect from "effect/Effect" ;
9+ import * as FileSystem from "effect/FileSystem" ;
1210import * as Layer from "effect/Layer" ;
1311import * as Option from "effect/Option" ;
12+ import * as Path from "effect/Path" ;
1413import * as Schema from "effect/Schema" ;
1514import * as Scope from "effect/Scope" ;
1615import * as Semaphore from "effect/Semaphore" ;
@@ -297,23 +296,32 @@ type StandaloneHostModule = {
297296// package through the same lookup paths and require its entry file directly.
298297const requireForCuaDriver = NodeModule . createRequire ( import . meta. url ) ;
299298
300- const loadEmbeddedCuaDriver = ( ) : Promise < StandaloneHostModule > =>
301- Promise . resolve ( ) . then ( ( ) => {
302- for ( const lookupPath of requireForCuaDriver . resolve . paths ( "@trycua/cua-driver" ) ?? [ ] ) {
303- const packageDir = NodePath . join ( lookupPath , "@trycua" , "cua-driver" ) ;
304- if ( NodeFS . existsSync ( NodePath . join ( packageDir , "package.json" ) ) ) {
305- return requireForCuaDriver (
306- NodePath . join ( packageDir , "dist" , "embedded.js" ) ,
307- ) as StandaloneHostModule ;
308- }
299+ const loadEmbeddedCuaDriver = Effect . fn ( "CuaDriver.loadEmbeddedSdk" ) ( function * ( ) {
300+ const fs = yield * FileSystem . FileSystem ;
301+ const path = yield * Path . Path ;
302+ for ( const lookupPath of requireForCuaDriver . resolve . paths ( "@trycua/cua-driver" ) ?? [ ] ) {
303+ const packageDir = path . join ( lookupPath , "@trycua" , "cua-driver" ) ;
304+ if (
305+ yield * fs
306+ . exists ( path . join ( packageDir , "package.json" ) )
307+ . pipe ( Effect . orElseSucceed ( ( ) => false ) )
308+ ) {
309+ return yield * Effect . try ( {
310+ try : ( ) =>
311+ requireForCuaDriver ( path . join ( packageDir , "dist" , "embedded.js" ) ) as StandaloneHostModule ,
312+ catch : ( cause ) => new CuaDriverSdkLoadError ( { cause } ) ,
313+ } ) ;
309314 }
310- throw new Error ( "@trycua/cua-driver is not installed beside this server." ) ;
315+ }
316+ return yield * new CuaDriverSdkLoadError ( {
317+ cause : "@trycua/cua-driver is not installed beside this server." ,
311318 } ) ;
319+ } ) ;
312320
313321/** A timed-out native cleanup still owns the factory until every call settles. */
314322export const makeStandaloneHostFactory = Effect . fn ( "CuaDriver.standaloneHostFactory" ) ( function * (
315323 binaryPath : string ,
316- loadEmbedded : ( ) => Promise < StandaloneHostModule > = loadEmbeddedCuaDriver ,
324+ loadEmbedded : Effect . Effect < StandaloneHostModule , CuaDriverSdkLoadError > ,
317325) {
318326 const mutex = yield * Semaphore . make ( 1 ) ;
319327 let occupied = false ;
@@ -324,10 +332,7 @@ export const makeStandaloneHostFactory = Effect.fn("CuaDriver.standaloneHostFact
324332 if ( occupied ) {
325333 return yield * new CuaDriverBusyError ( { } ) ;
326334 }
327- const { EmbeddedCuaDriverHost } = yield * Effect . tryPromise ( {
328- try : loadEmbedded ,
329- catch : ( cause ) => new CuaDriverSdkLoadError ( { cause } ) ,
330- } ) ;
335+ const { EmbeddedCuaDriverHost } = yield * loadEmbedded ;
331336 const host = yield * Effect . try ( {
332337 try : ( ) => new EmbeddedCuaDriverHost ( binaryPath , "com.t3tools.t3code.server" ) ,
333338 catch : ( cause ) => new CuaDriverHostCreateError ( { binaryPath, cause } ) ,
@@ -469,12 +474,20 @@ export const layer = Layer.effect(
469474 CuaDriver ,
470475 Effect . gen ( function * ( ) {
471476 const config = yield * ServerConfig . ServerConfig ;
477+ const fs = yield * FileSystem . FileSystem ;
478+ const path = yield * Path . Path ;
472479 const binaryPath = process . env . T3CODE_CUA_DRIVER_PATH ?. trim ( ) ;
473480 const createHost =
474481 config . mode === "desktop" && config . desktopTelemetryControlFd !== undefined
475482 ? yield * makeDesktopHostFactory ( )
476483 : binaryPath
477- ? yield * makeStandaloneHostFactory ( binaryPath )
484+ ? yield * makeStandaloneHostFactory (
485+ binaryPath ,
486+ loadEmbeddedCuaDriver ( ) . pipe (
487+ Effect . provideService ( FileSystem . FileSystem , fs ) ,
488+ Effect . provideService ( Path . Path , path ) ,
489+ ) ,
490+ )
478491 : Effect . logWarning (
479492 "Cua Driver requires the T3 desktop host or an explicit T3CODE_CUA_DRIVER_PATH on this server." ,
480493 ) . pipe ( Effect . andThen ( Effect . fail ( new CuaDriverNotConfiguredError ( { } ) ) ) ) ;
0 commit comments