|
| 1 | +import { |
| 2 | + agentForRDPComputer, |
| 3 | + type RDPComputerAgentOpt, |
| 4 | +} from '@midscene/computer'; |
| 5 | +import 'dotenv/config'; |
| 6 | + |
| 7 | +const rdpTarget = { |
| 8 | + host: 'REPLACE_WITH_YOUR_RDP_HOST', |
| 9 | + port: 3389, |
| 10 | + username: 'Admin', |
| 11 | + password: 'REPLACE_WITH_YOUR_RDP_PASSWORD', |
| 12 | + ignoreCertificate: true, |
| 13 | + adminSession: false, |
| 14 | + domain: undefined, |
| 15 | + securityProtocol: 'auto', |
| 16 | +} satisfies RDPComputerAgentOpt; |
| 17 | + |
| 18 | +function validateDemoConfig() { |
| 19 | + for (const [key, value] of Object.entries({ |
| 20 | + host: rdpTarget.host, |
| 21 | + password: rdpTarget.password, |
| 22 | + })) { |
| 23 | + const trimmed = value?.trim(); |
| 24 | + if (!trimmed || trimmed.startsWith('REPLACE_WITH_')) { |
| 25 | + throw new Error( |
| 26 | + `Please update rdpTarget.${key} in computer/rdp-demo/demo.ts before running the demo.`, |
| 27 | + ); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +async function waitForRemoteDesktopReady( |
| 33 | + agent: Awaited<ReturnType<typeof agentForRDPComputer>>, |
| 34 | + maxAttempts = 10, |
| 35 | + delayMs = 3_000, |
| 36 | +) { |
| 37 | + let lastError: unknown; |
| 38 | + |
| 39 | + for (let attempt = 1; attempt <= maxAttempts; attempt++) { |
| 40 | + try { |
| 41 | + await agent.aiAssert( |
| 42 | + 'The remote screenshot is not a blank white screen. Some visible Windows UI such as the taskbar, desktop icons, the Start menu, or an application window is present.', |
| 43 | + ); |
| 44 | + return; |
| 45 | + } catch (error) { |
| 46 | + lastError = error; |
| 47 | + if (attempt === maxAttempts) { |
| 48 | + break; |
| 49 | + } |
| 50 | + await new Promise((resolve) => setTimeout(resolve, delayMs)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + throw new Error( |
| 55 | + `The remote desktop never became visible after ${maxAttempts} attempts.`, |
| 56 | + { cause: lastError }, |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +async function main() { |
| 61 | + validateDemoConfig(); |
| 62 | + |
| 63 | + const agent = await agentForRDPComputer({ |
| 64 | + ...rdpTarget, |
| 65 | + aiActionContext: |
| 66 | + 'You are controlling a remote Windows desktop directly through the RDP protocol. Every screenshot and action comes from the remote machine itself.', |
| 67 | + generateReport: true, |
| 68 | + }); |
| 69 | + |
| 70 | + try { |
| 71 | + await waitForRemoteDesktopReady(agent); |
| 72 | + |
| 73 | + await agent.aiAct( |
| 74 | + 'Click the Windows Start button, open the Settings app, then navigate to the Windows Update page. Stop only after the Windows Update page is clearly visible in the remote screenshot.', |
| 75 | + ); |
| 76 | + |
| 77 | + await agent.aiAssert( |
| 78 | + 'The Windows Update page inside the Settings app is open and visible in the remote screenshot.', |
| 79 | + ); |
| 80 | + |
| 81 | + const windowsUpdateSummary = await agent.aiQuery<{ |
| 82 | + pageTitle: string; |
| 83 | + statusSummary: string; |
| 84 | + }>( |
| 85 | + '{pageTitle: string, statusSummary: string}, read the visible Windows Update page and return its main page title and the short status summary shown near the top of the page.', |
| 86 | + ); |
| 87 | + |
| 88 | + console.log('Connected to remote desktop:', rdpTarget.host); |
| 89 | + console.log('Windows Update page title:', windowsUpdateSummary.pageTitle); |
| 90 | + console.log('Windows Update status:', windowsUpdateSummary.statusSummary); |
| 91 | + } finally { |
| 92 | + await agent.destroy(); |
| 93 | + } |
| 94 | + |
| 95 | + if (agent.reportFile) { |
| 96 | + console.log('Report saved to:', agent.reportFile); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +main().catch((error) => { |
| 101 | + console.error(error); |
| 102 | + process.exitCode = 1; |
| 103 | +}); |
0 commit comments