UNPKG

test-wuying-agentbay-sdk

Version:

TypeScript SDK for interacting with the Wuying AgentBay cloud runtime environment

168 lines (137 loc) โ€ข 5.55 kB
/** * Watch Directory Example * * This example demonstrates how to use the watch_directory functionality * to monitor file changes in a directory. */ import { AgentBay, CreateSessionParams } from "wuying-agentbay-sdk"; import { FileChangeEvent } from "wuying-agentbay-sdk"; async function main(): Promise<void> { // Get API key from environment variable const apiKey = process.env.AGENTBAY_API_KEY; if (!apiKey) { console.log("โŒ Please set the AGENTBAY_API_KEY environment variable"); return; } console.log("๐Ÿš€ Watch Directory Example"); console.log("=".repeat(50)); try { // Initialize AgentBay client const agentbay = new AgentBay({ apiKey }); console.log("โœ… AgentBay client initialized"); // Create session with code_latest ImageId const sessionParams: CreateSessionParams = { imageId: "code_latest" }; const sessionResult = await agentbay.create(sessionParams); if (!sessionResult.success) { console.log(`โŒ Failed to create session: ${sessionResult.errorMessage}`); return; } const session = sessionResult.session; console.log(`โœ… Session created: ${session.getSessionId()}`); try { // Create a test directory to monitor const testDir = "/tmp/watch_example"; console.log(`\n๐Ÿ“ Creating test directory: ${testDir}`); const createResult = await session.fileSystem.createDirectory(testDir); if (!createResult.success) { console.log(`โŒ Failed to create directory: ${createResult.errorMessage}`); return; } console.log("โœ… Test directory created"); // Set up file change monitoring const detectedChanges: FileChangeEvent[] = []; const onFileChange = (events: FileChangeEvent[]) => { console.log(`\n๐Ÿ”” Detected ${events.length} file changes:`); for (const event of events) { console.log(` ๐Ÿ“„ ${event.eventType.toUpperCase()}: ${event.path} (${event.pathType})`); } detectedChanges.push(...events); }; console.log("\n๐Ÿ‘๏ธ Starting directory monitoring..."); console.log(" Press Ctrl+C to stop monitoring"); // Start monitoring const controller = new AbortController(); const watchPromise = session.fileSystem.watchDirectory( testDir, onFileChange, 1000, // Check every second controller.signal ); console.log("โœ… Directory monitoring started"); // Demonstrate file operations console.log("\n๐Ÿ”จ Demonstrating file operations..."); // Create some files const filesToCreate = [ { name: "example1.txt", content: "Hello, World!" }, { name: "example2.txt", content: "This is a test file." }, { name: "config.json", content: '{"setting": "value"}' } ]; for (const file of filesToCreate) { const filepath = `${testDir}/${file.name}`; console.log(` Creating: ${file.name}`); const writeResult = await session.fileSystem.writeFile(filepath, file.content); if (writeResult.success) { console.log(` โœ… Created: ${file.name}`); } else { console.log(` โŒ Failed to create ${file.name}: ${writeResult.errorMessage}`); } // Give time for monitoring to detect changes await new Promise(resolve => setTimeout(resolve, 1500)); } // Modify a file console.log("\n Modifying example1.txt..."); const modifyResult = await session.fileSystem.writeFile( `${testDir}/example1.txt`, "Hello, World! - Modified content" ); if (modifyResult.success) { console.log(" โœ… Modified example1.txt"); } else { console.log(` โŒ Failed to modify file: ${modifyResult.errorMessage}`); } // Wait a bit more to capture all events console.log("\nโณ Waiting for final events..."); await new Promise(resolve => setTimeout(resolve, 3000)); // Stop monitoring console.log("\n๐Ÿ›‘ Stopping directory monitoring..."); controller.abort(); await watchPromise; console.log("โœ… Directory monitoring stopped"); // Summary console.log("\n๐Ÿ“Š Summary:"); console.log(` Total events detected: ${detectedChanges.length}`); if (detectedChanges.length > 0) { console.log(" Event breakdown:"); const eventTypes: Record<string, number> = {}; for (const event of detectedChanges) { eventTypes[event.eventType] = (eventTypes[event.eventType] || 0) + 1; } for (const [eventType, count] of Object.entries(eventTypes)) { console.log(` ${eventType}: ${count}`); } } console.log("\nโœจ Example completed successfully!"); } finally { // Clean up session console.log("\n๐Ÿงน Cleaning up..."); const deleteResult = await agentbay.delete(session); if (deleteResult.success) { console.log("โœ… Session cleaned up successfully"); } else { console.log(`โš ๏ธ Session cleanup warning: ${deleteResult.errorMessage}`); } } } catch (error) { if (error instanceof Error && error.name === 'AbortError') { console.log("\n\nโน๏ธ Monitoring stopped by user"); } else { console.log(`\nโŒ An error occurred: ${error}`); } } } // Handle Ctrl+C gracefully process.on('SIGINT', () => { console.log("\n\nโน๏ธ Received interrupt signal, stopping..."); process.exit(0); }); main().catch(console.error);