/ utils / computerUse / swiftLoader.ts
swiftLoader.ts
 1  import type { ComputerUseAPI } from '@ant/computer-use-swift'
 2  
 3  let cached: ComputerUseAPI | undefined
 4  
 5  /**
 6   * Package's js/index.js reads COMPUTER_USE_SWIFT_NODE_PATH (baked by
 7   * build-with-plugins.ts on darwin targets, unset otherwise — falls through to
 8   * the node_modules prebuilds/ path). We cache the loaded native module.
 9   *
10   * The four @MainActor methods (captureExcluding, captureRegion,
11   * apps.listInstalled, resolvePrepareCapture) dispatch to DispatchQueue.main
12   * and will hang under libuv unless CFRunLoop is pumped — call sites wrap
13   * these in drainRunLoop().
14   */
15  export function requireComputerUseSwift(): ComputerUseAPI {
16    if (process.platform !== 'darwin') {
17      throw new Error('@ant/computer-use-swift is macOS-only')
18    }
19    // eslint-disable-next-line @typescript-eslint/no-require-imports
20    return (cached ??= require('@ant/computer-use-swift') as ComputerUseAPI)
21  }
22  
23  export type { ComputerUseAPI }