import { z } from '@midscene/core'; import { inputStrategies } from '@midscene/core/device'; import { type AgentBehaviorInitArgs, agentBehaviorInitArgShape, extractAgentBehaviorInitArgs, getAgentInitArgsSignature, shouldRebuildAgentForInitArgs, } from '@midscene/shared/agent-tools/agent-behavior-init-args'; import { BaseMidsceneTools, type InitArgSpec, } from '@midscene/shared/agent-tools/base-tools'; import type { ToolDefinition } from '@midscene/shared/agent-tools/types'; import { getDebug } from '@midscene/shared/logger'; import { type ComputerAgent, agentForRDPComputer, agentFromComputer, } from './agent'; import { ComputerDevice, type ComputerDeviceOpt } from './device'; import { formatRdpHost, formatRdpServerAddress, normalizeRdpHost, } from './rdp/address'; import type { RDPConnectionConfig, RDPSecurityProtocol } from './rdp/protocol'; const debug = getDebug('agent-tools:computer'); const RDP_SECURITY_PROTOCOLS = [ 'auto', 'tls', 'nla', 'rdp', ] as const satisfies readonly RDPSecurityProtocol[]; const computerInitArgShape = { displayId: z .string() .optional() .describe( 'Display ID for local mode (from computer_list_displays). Ignored when host is set.', ), headless: z .boolean() .optional() .describe( 'Start virtual display via Xvfb (Linux local mode only). Ignored when host is set.', ), keyboardTypeDelay: z .number() .finite() .nonnegative() .optional() .describe( 'Finite non-negative delay in milliseconds between keystrokes. In "legacy" mode, positive values enable key-by-key input; zero or omitted uses clipboard input.', ), inputStrategy: z .enum(inputStrategies) .optional() .describe( 'Text input strategy. "legacy" (default) preserves current Computer behavior, "sequential" sends one Unicode code point at a time, and "bulk" uses one backend input operation. "bulk" requires keyboardTypeDelay to be omitted or set to 0.', ), keyboardModifierDelay: z .number() .finite() .nonnegative() .optional() .describe( 'Finite non-negative delay in milliseconds after each modifier transition and the main key for local libnut keyboard events. Positive values pace shortcuts and the implicit Shift used by uppercase letters during sequential input. Layout-dependent punctuation also requires keyboardLayout. Ignored in RDP mode and by the macOS AppleScript driver.', ), keyboardLayout: z .enum(['en-US']) .optional() .describe( 'Keyboard layout used to resolve layout-dependent shifted punctuation to base keys during sequential local libnut input. Currently only "en-US" is supported. Ignored in RDP mode and by the macOS AppleScript driver.', ), // RDP options. Providing `host` switches connect into RDP mode and routes // the session through the RDP helper binary instead of the local desktop. // All other RDP options below are silently ignored unless `host` is set. host: z .string() .optional() .describe('RDP host (FQDN or IP). Set this to switch into RDP mode.'), port: z .number() .optional() .describe('RDP port (default 3389). Requires host.'), username: z.string().optional().describe('RDP username. Requires host.'), password: z .string() .optional() .describe( 'RDP password. Requires host. Prefer setting via environment or a secrets manager.', ), domain: z.string().optional().describe('RDP domain. Requires host.'), localAddress: z .string() .optional() .describe( 'Local source IP address for the RDP TCP connection. Requires host.', ), adminSession: z .boolean() .optional() .describe('Attach to the RDP admin/console session. Requires host.'), ignoreCertificate: z .boolean() .optional() .describe('Skip TLS certificate validation. Requires host.'), securityProtocol: z .enum(RDP_SECURITY_PROTOCOLS) .optional() .describe( 'RDP security protocol negotiation (default auto). Requires host.', ), desktopWidth: z .number() .optional() .describe('Remote desktop width in pixels. Requires host.'), desktopHeight: z .number() .optional() .describe('Remote desktop height in pixels. Requires host.'), ...agentBehaviorInitArgShape, }; /** Init args for the local desktop agent (macOS/Windows/Linux). */ export type ComputerLocalInitArgs = { mode: 'local'; } & Pick & Pick< ComputerDeviceOpt, | 'inputStrategy' | 'keyboardTypeDelay' | 'keyboardModifierDelay' | 'keyboardLayout' > & AgentBehaviorInitArgs; /** Init args for the RDP remote-desktop agent. */ export type ComputerRDPInitArgs = { mode: 'rdp'; } & RDPConnectionConfig & Pick & AgentBehaviorInitArgs; /** * Discriminated union describing the two ways `computer_*` tools can spawn an * agent. `mode` is filled in by `initArgSpec.adapt` based on whether `host` is * set, so callers (CLI/YAML) never have to provide it explicitly. */ export type ComputerInitArgs = ComputerLocalInitArgs | ComputerRDPInitArgs; type ExtractedComputerInitArgs = Partial< Pick< ComputerDeviceOpt, | 'displayId' | 'headless' | 'inputStrategy' | 'keyboardTypeDelay' | 'keyboardModifierDelay' | 'keyboardLayout' > & RDPConnectionConfig & AgentBehaviorInitArgs >; export interface ComputerMidsceneToolsOptions { /** Keep CLI-owned Xvfb alive until process exit so Xlib clients stay valid. */ keepXvfbAliveUntilProcessExit?: boolean; } function adaptComputerInitArgs( extracted: ExtractedComputerInitArgs | undefined, ): ComputerInitArgs | undefined { if (!extracted || Object.keys(extracted).length === 0) { return undefined; } if (extracted.host) { // Drop local-only fields; they're meaningless in RDP mode. const { displayId: _d, headless: _h, keyboardModifierDelay: _s, keyboardLayout: _l, ...rdpFields } = extracted; const host = normalizeRdpHost(extracted.host); return { mode: 'rdp', ...rdpFields, host, }; } return { mode: 'local', displayId: extracted.displayId, headless: extracted.headless, keyboardTypeDelay: extracted.keyboardTypeDelay, inputStrategy: extracted.inputStrategy, keyboardModifierDelay: extracted.keyboardModifierDelay, keyboardLayout: extracted.keyboardLayout, ...(extractAgentBehaviorInitArgs(extracted) ?? {}), }; } function describeConnectTarget(opts: ComputerInitArgs | undefined): string { if (opts?.mode === 'rdp') { const target = opts.port ? formatRdpServerAddress(opts.host, opts.port) : formatRdpHost(opts.host); const userSuffix = opts.username ? ` as ${opts.username}` : ''; return ` via RDP (${target}${userSuffix})`; } if (opts?.mode === 'local' && opts.displayId) { return ` (Display: ${opts.displayId})`; } return ' (Primary display)'; } function getCliReportSessionTarget(opts: ComputerInitArgs | undefined): string { if (opts?.mode === 'rdp') return `rdp:${formatRdpHost(opts.host)}`; if (opts?.mode === 'local' && opts.displayId) return opts.displayId; return 'primary'; } /** * Computer-specific tools manager * Extends BaseMidsceneTools to provide desktop automation tools */ export class ComputerMidsceneTools extends BaseMidsceneTools< ComputerAgent, ComputerInitArgs > { private lastInitArgsSignature?: string; constructor(private readonly options: ComputerMidsceneToolsOptions = {}) { super(); } protected getCliReportSessionName() { return 'midscene-computer'; } protected readonly initArgSpec: InitArgSpec = { namespace: 'computer', shape: computerInitArgShape, cli: { preferBareKeys: true, }, adapt: (extracted) => adaptComputerInitArgs(extracted as ExtractedComputerInitArgs | undefined), }; protected createTemporaryDevice() { // Create minimal temporary instance return new ComputerDevice({}); } protected async ensureAgent(opts?: ComputerInitArgs): Promise { const nextSignature = getAgentInitArgsSignature(opts); if ( this.agent && shouldRebuildAgentForInitArgs(this.lastInitArgsSignature, nextSignature) ) { try { await this.agent.destroy?.(); } catch (error) { debug('Failed to destroy agent during cleanup:', error); } this.agent = undefined; } if (this.agent) { return this.agent; } const reportOptions = this.readCliReportAgentOptions(); if (opts?.mode === 'rdp') { debug('Creating RDP Computer agent for host:', opts.host); const { mode: _mode, ...rdpFields } = opts; const agent = await agentForRDPComputer({ ...rdpFields, ...(reportOptions ?? {}), }); this.agent = agent; this.lastInitArgsSignature = nextSignature; return agent; } const displayId = opts?.mode === 'local' ? opts.displayId : undefined; const headless = opts?.mode === 'local' ? opts.headless : undefined; const keyboardTypeDelay = opts?.keyboardTypeDelay; const inputStrategy = opts?.inputStrategy; const keyboardModifierDelay = opts?.mode === 'local' ? opts.keyboardModifierDelay : undefined; const keyboardLayout = opts?.mode === 'local' ? opts.keyboardLayout : undefined; debug('Creating Computer agent with displayId:', displayId || 'primary'); const agentOpts = { ...(displayId ? { displayId } : {}), ...(headless !== undefined ? { headless } : {}), ...(keyboardTypeDelay !== undefined ? { keyboardTypeDelay } : {}), ...(inputStrategy !== undefined ? { inputStrategy } : {}), ...(keyboardModifierDelay !== undefined ? { keyboardModifierDelay } : {}), ...(keyboardLayout !== undefined ? { keyboardLayout } : {}), ...(this.options.keepXvfbAliveUntilProcessExit ? { keepXvfbAliveUntilProcessExit: true } : {}), ...(extractAgentBehaviorInitArgs(opts) ?? {}), ...(reportOptions ?? {}), }; const agent = await agentFromComputer( Object.keys(agentOpts).length > 0 ? agentOpts : undefined, ); this.agent = agent; this.lastInitArgsSignature = nextSignature; return agent; } /** * Provide Computer-specific platform tools */ protected preparePlatformTools(): ToolDefinition[] { return [ { name: 'computer_connect', description: [ 'Connect to a computer desktop.', 'Default (local) mode controls the local machine; pass displayId to target a specific local display (see computer_list_displays).', 'Pass host to switch to RDP mode and connect to a remote Windows desktop via the RDP helper binary.', 'RDP-related options (port/username/password/domain/localAddress/securityProtocol/ignoreCertificate/adminSession/desktopWidth/desktopHeight) only take effect when host is set.', ].join(' '), schema: this.getAgentInitArgSchema(), cli: this.getAgentInitArgCliMetadata(), handler: async (args: Record) => { const initArgs = this.extractAgentInitParam(args); const reportSession = this.createNewCliReportSession( getCliReportSessionTarget(initArgs), ); this.commitCliReportSession(reportSession); if (this.agent) { try { await this.agent.destroy?.(); } catch (error) { debug('Failed to destroy agent during connect:', error); } this.agent = undefined; this.lastInitArgsSignature = undefined; } const agent = await this.ensureAgent(initArgs); const screenshot = await agent.interface.screenshotBase64(); return { content: [ { type: 'text', text: `Connected to computer${describeConnectTarget(initArgs)}`, }, ...this.buildScreenshotContent(screenshot), ], }; }, }, { name: 'computer_disconnect', description: 'Disconnect from computer and release resources', schema: {}, handler: this.createDisconnectHandler('computer'), }, { name: 'computer_list_displays', description: 'List all available displays/monitors', schema: {}, handler: async () => { const displays = await ComputerDevice.listDisplays(); return { content: [ { type: 'text', text: `Available displays:\n${displays.map((d) => `- ${d.name} (ID: ${d.id})${d.primary ? ' [PRIMARY]' : ''}`).join('\n')}`, }, ], }; }, }, ]; } }