Files
claude-code/src/utils/computerUse/swiftLoader.ts
T
unraid ca086b045a fix: resolve Windows Computer Use request_access and screenshot errors
Two root causes fixed:

1. swiftLoader.ts: require('@ant/computer-use-swift') returns a module
   with { ComputerUseAPI } class, not an instance. macOS native .node
   exports a plain object. Fixed by detecting class export and calling
   new ComputerUseAPI().

2. executor.ts resolvePrepareCapture: toolCalls.ts expects result to have
   { hidden: string[], displayId: number } fields. Our ComputerUseAPI
   returns { base64, width, height } only. Fixed by backfilling missing
   fields with defaults.

Verified: request_access → screenshot → left_click all work on Windows.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 00:09:51 +08:00

30 lines
1.2 KiB
TypeScript

import type { ComputerUseAPI } from '@ant/computer-use-swift'
let cached: ComputerUseAPI | undefined
/**
* Package's js/index.js reads COMPUTER_USE_SWIFT_NODE_PATH (baked by
* build-with-plugins.ts on darwin targets, unset otherwise — falls through to
* the node_modules prebuilds/ path). We cache the loaded native module.
*
* The four @MainActor methods (captureExcluding, captureRegion,
* apps.listInstalled, resolvePrepareCapture) dispatch to DispatchQueue.main
* and will hang under libuv unless CFRunLoop is pumped — call sites wrap
* these in drainRunLoop().
*/
export function requireComputerUseSwift(): ComputerUseAPI {
if (cached) return cached
// eslint-disable-next-line @typescript-eslint/no-require-imports
const mod = require('@ant/computer-use-swift')
// macOS native .node exports a plain object with apps/display/screenshot directly.
// Our cross-platform package exports { ComputerUseAPI } class — needs instantiation.
if (mod.ComputerUseAPI && typeof mod.ComputerUseAPI === 'function') {
cached = new mod.ComputerUseAPI() as ComputerUseAPI
} else {
cached = mod as ComputerUseAPI
}
return cached
}
export type { ComputerUseAPI }