实践教程 本文演示一次完整的 SDK 调用流程:创建会话、执行桌面操作、创建文件、读取文件并关闭会话。 完整示例 plaintext import Client from '@ctyun/desktopagentsdk'; async function main() { const client new Client({ apiKey: process.env.AIUSEAPIKEY, apiSecret: process.env.AIUSEAPISECRET, desktopCode: process.env.AIUSEDESKTOPCODE, serviceURL: process.env.AIUSESERVICEURL ' }); let session null; try { session await client.createSession(); console.log('会话创建成功:', session.sessionId); await session.computer.screenshot(); await session.computer.movemouse({ x: 500, y: 300 }); await session.computer.clickmouse({ x: 500, y: 300 }); await session.computer.typetext({ text: 'Hello from AIuse SDK!' }); await session.filesystem.createdirectory({ path: 'C:/aiusertest' }); await session.filesystem.writefile({ path: 'C:/aiusertest/hello.txt', mode: 'overwrite', content: 'Hello from AIuse SDK' }); const fileResult await session.filesystem.readfile({ path: 'C:/aiusertest/hello.txt', offset: 0, length: 0 }); console.log('文件内容:', fileResult.data?.content); } catch (error) { console.error('操作失败:', error); } finally { if (session) { await session.close(); console.log('会话已关闭'); } } } main().catch(console.error); 运行前检查 已安装 Node.js 18 或以上版本。 已安装 @ctyun/desktopagentsdk。 环境变量 AIUSEAPIKEY、AIUSEAPISECRET、AIUSEDESKTOPCODE 已正确配置。 AccessKey 已关联目标云电脑。 目标云电脑在线。 实践建议 先截图确认桌面状态,再执行鼠标和键盘操作。 复杂 GUI 任务建议拆分为多个阶段,每个阶段记录结果。 文件操作建议使用独立任务目录,避免误覆盖业务文件。 异常场景下也要关闭会话。