实践教程
更新时间 2026-05-09 17:04:43
最近更新时间: 2026-05-09 17:04:43
本文演示一次完整的 SDK 调用流程:创建会话、执行桌面操作、创建文件、读取文件并关闭会话。
完整示例
import Client from '@ctyun/desktop-agent-sdk';
async function main() {
const client = new Client({
apiKey: process.env.AIUSE_API_KEY,
apiSecret: process.env.AIUSE_API_SECRET,
desktopCode: process.env.AIUSE_DESKTOP_CODE,
serviceURL: process.env.AIUSE_SERVICE_URL || 'https://desk.ctyun.cn:8816'
});
let session = null;
try {
session = await client.createSession();
console.log('会话创建成功:', session.sessionId);
await session.computer.screen_shot();
await session.computer.move_mouse({ x: 500, y: 300 });
await session.computer.click_mouse({ x: 500, y: 300 });
await session.computer.type_text({ text: 'Hello from AIuse SDK!' });
await session.filesystem.create_directory({
path: 'C:/ai-user-test'
});
await session.filesystem.write_file({
path: 'C:/ai-user-test/hello.txt',
mode: 'overwrite',
content: 'Hello from AIuse SDK'
});
const fileResult = await session.filesystem.read_file({
path: 'C:/ai-user-test/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/desktop-agent-sdk。环境变量
AIUSE_API_KEY、AIUSE_API_SECRET、AIUSE_DESKTOP_CODE已正确配置。AccessKey 已关联目标云电脑。
目标云电脑在线。
实践建议
先截图确认桌面状态,再执行鼠标和键盘操作。
复杂 GUI 任务建议拆分为多个阶段,每个阶段记录结果。
文件操作建议使用独立任务目录,避免误覆盖业务文件。
异常场景下也要关闭会话。