test-wuying-agentbay-sdk
Version:
TypeScript SDK for interacting with the Wuying AgentBay cloud runtime environment
87 lines (74 loc) • 3.45 kB
text/typescript
import { AgentBay, logError, log } from 'wuying-agentbay-sdk';
async function main() {
try {
// Use the test API key function from test-helpers
const apiKey = process.env.AGENTBAY_API_KEY || 'akm-xxx'; // Replace with your actual API key
if (!process.env.AGENTBAY_API_KEY) {
log('Warning: Using placeholder API key. Set AGENTBAY_API_KEY environment variable for production use.');
}
// Initialize the AgentBay client
const agentBay = new AgentBay({ apiKey });
// Create a new session with labels
log('Creating a new session with labels...');
const createResponse = await agentBay.create({imageId:'browser_latest'});
const session = createResponse.session;
log(`Session created with ID: ${session.sessionId}`);
log(`Create Session RequestId: ${createResponse.requestId}`);
// Execute a command
log('\nExecuting a command...');
const commandResponse = await session.command.executeCommand('ls -la');
log('Command result:', commandResponse.output);
log(`Execute Command RequestId: ${commandResponse.requestId}`);
// Read a file
log('\nReading a file...');
const fileResponse = await session.fileSystem.readFile('/etc/hosts');
log(`File content: ${fileResponse.content}`);
log(`Read File RequestId: ${fileResponse.requestId}`);
// Get the session link
log('\nGetting session link...');
try {
const linkResponse = await session.getLink();
log(`Session link: ${linkResponse.data}`);
log(`Get Link RequestId: ${linkResponse.requestId}`);
} catch (error) {
log(`Note: Failed to get session link: ${error}`);
}
// Note: Screenshot functionality is available via session.mobile.screenshot() or session.computer.screenshot()
// depending on the session type (mobile or computer). Browser sessions don't have a direct screenshot API
// at the session level - use browser automation tools like Playwright for browser screenshots.
// List all sessions by labels
log('\nListing sessions...');
try {
// List sessions with labels (page 1, limit 5)
const listResponse = await agentBay.list({ test: 'basic-usage' }, 1, 5);
log(`Available sessions count: ${listResponse.sessionIds.length}`);
if (listResponse.totalCount !== undefined) {
log(`Total count: ${listResponse.totalCount}`);
}
if (listResponse.maxResults !== undefined) {
log(`Max results: ${listResponse.maxResults}`);
}
log('Session IDs:', listResponse.sessionIds);
log(`List Sessions RequestId: ${listResponse.requestId}`);
// Demonstrate pagination if there's a next token
if (listResponse.nextToken) {
log('\nFetching next page...');
const nextPageResponse = await agentBay.list({ test: 'basic-usage' }, 2, 5);
log(`Next page sessions count: ${nextPageResponse.sessionIds.length}`);
log(`Next page RequestId: ${nextPageResponse.requestId}`);
}
} catch (error) {
log(`Note: Failed to list sessions: ${error}`);
}
// Delete the session
log('\nDeleting the session...');
const deleteResponse = await agentBay.delete(session);
log('Session deleted successfully');
log(`Delete Session RequestId: ${deleteResponse.requestId}`);
} catch (error) {
logError('Error:', error);
// In a Node.js environment, you would use process.exit(1) here
throw error;
}
}
main();