|
| 1 | +# Browser Compatibility Guide |
| 2 | + |
| 3 | +This document outlines the browser-compatible file operations API in the OpenHands TypeScript Client. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The TypeScript client has been updated to work natively in browser environments without Node.js dependencies. The main changes involve file upload and download operations that now work with browser-native data types like `Blob`, `File`, and strings instead of file system paths. |
| 8 | + |
| 9 | +## File Upload API |
| 10 | + |
| 11 | +### `fileUpload(content, destinationPath, fileName?)` |
| 12 | + |
| 13 | +Upload content to the remote workspace. |
| 14 | + |
| 15 | +**Parameters:** |
| 16 | +- `content: string | Blob | File` - The content to upload |
| 17 | +- `destinationPath: string` - Where to save the file on the remote workspace |
| 18 | +- `fileName?: string` - Optional filename (auto-detected for File objects) |
| 19 | + |
| 20 | +**Examples:** |
| 21 | + |
| 22 | +```typescript |
| 23 | +const workspace = new RemoteWorkspace({ |
| 24 | + host: 'http://localhost:3000', |
| 25 | + workingDir: '/tmp', |
| 26 | + apiKey: 'your-api-key' |
| 27 | +}); |
| 28 | + |
| 29 | +// Upload text content |
| 30 | +await workspace.fileUpload('Hello, World!', '/tmp/hello.txt', 'hello.txt'); |
| 31 | + |
| 32 | +// Upload a File object (from file input) |
| 33 | +const fileInput = document.getElementById('fileInput') as HTMLInputElement; |
| 34 | +const file = fileInput.files[0]; |
| 35 | +await workspace.fileUpload(file, '/tmp/uploads/'); |
| 36 | + |
| 37 | +// Upload a Blob |
| 38 | +const blob = new Blob(['Some data'], { type: 'text/plain' }); |
| 39 | +await workspace.fileUpload(blob, '/tmp/data.txt', 'data.txt'); |
| 40 | +``` |
| 41 | + |
| 42 | +### Convenience Methods |
| 43 | + |
| 44 | +#### `uploadText(text, destinationPath, fileName?)` |
| 45 | +Shorthand for uploading text content. |
| 46 | + |
| 47 | +```typescript |
| 48 | +await workspace.uploadText('Hello, World!', '/tmp/hello.txt'); |
| 49 | +``` |
| 50 | + |
| 51 | +#### `uploadFileObject(file, destinationPath)` |
| 52 | +Shorthand for uploading File objects. |
| 53 | + |
| 54 | +```typescript |
| 55 | +const file = fileInput.files[0]; |
| 56 | +await workspace.uploadFileObject(file, '/tmp/uploads/'); |
| 57 | +``` |
| 58 | + |
| 59 | +## File Download API |
| 60 | + |
| 61 | +### `fileDownload(sourcePath)` |
| 62 | + |
| 63 | +Download a file from the remote workspace. Returns content as string or Blob. |
| 64 | + |
| 65 | +**Parameters:** |
| 66 | +- `sourcePath: string` - Path to the file on the remote workspace |
| 67 | + |
| 68 | +**Returns:** `Promise<FileDownloadResult>` |
| 69 | + |
| 70 | +```typescript |
| 71 | +interface FileDownloadResult { |
| 72 | + success: boolean; |
| 73 | + source_path: string; |
| 74 | + content: string | Blob; |
| 75 | + file_size?: number; |
| 76 | + error?: string; |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +**Example:** |
| 81 | + |
| 82 | +```typescript |
| 83 | +const result = await workspace.fileDownload('/tmp/data.txt'); |
| 84 | +if (result.success) { |
| 85 | + console.log('File content:', result.content); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Convenience Methods |
| 90 | + |
| 91 | +#### `downloadAsText(sourcePath)` |
| 92 | +Download file content as a string. |
| 93 | + |
| 94 | +```typescript |
| 95 | +const text = await workspace.downloadAsText('/tmp/hello.txt'); |
| 96 | +console.log(text); // "Hello, World!" |
| 97 | +``` |
| 98 | + |
| 99 | +#### `downloadAsBlob(sourcePath)` |
| 100 | +Download file content as a Blob. |
| 101 | + |
| 102 | +```typescript |
| 103 | +const blob = await workspace.downloadAsBlob('/tmp/image.png'); |
| 104 | +// Use blob for further processing |
| 105 | +``` |
| 106 | + |
| 107 | +#### `downloadAndSave(sourcePath, saveAsFileName?)` |
| 108 | +Download a file and trigger browser download dialog. |
| 109 | + |
| 110 | +```typescript |
| 111 | +// This will prompt the user to save the file |
| 112 | +await workspace.downloadAndSave('/tmp/report.pdf', 'my-report.pdf'); |
| 113 | +``` |
| 114 | + |
| 115 | +## Migration from Node.js API |
| 116 | + |
| 117 | +### Before (Node.js only) |
| 118 | +```typescript |
| 119 | +// Old API - required file system paths |
| 120 | +await workspace.fileUpload('/local/path/file.txt', '/remote/path/file.txt'); |
| 121 | +await workspace.fileDownload('/remote/path/file.txt', '/local/path/file.txt'); |
| 122 | +``` |
| 123 | + |
| 124 | +### After (Browser compatible) |
| 125 | +```typescript |
| 126 | +// New API - works with browser data types |
| 127 | +const fileInput = document.getElementById('file') as HTMLInputElement; |
| 128 | +const file = fileInput.files[0]; |
| 129 | +await workspace.fileUpload(file, '/remote/path/file.txt'); |
| 130 | + |
| 131 | +const result = await workspace.fileDownload('/remote/path/file.txt'); |
| 132 | +if (result.success) { |
| 133 | + // Use result.content (string or Blob) |
| 134 | + console.log(result.content); |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +## Browser Testing |
| 139 | + |
| 140 | +A test file `test-browser.html` is included to verify browser compatibility. Open it in a browser after building the project to test the API without a running server. |
| 141 | + |
| 142 | +## Node.js Compatibility |
| 143 | + |
| 144 | +The new API is also compatible with Node.js environments. You can still use the client in Node.js applications by providing appropriate data types: |
| 145 | + |
| 146 | +```typescript |
| 147 | +import fs from 'fs'; |
| 148 | + |
| 149 | +// Read file content and upload |
| 150 | +const content = await fs.promises.readFile('/local/file.txt', 'utf8'); |
| 151 | +await workspace.fileUpload(content, '/remote/file.txt', 'file.txt'); |
| 152 | + |
| 153 | +// Download and save |
| 154 | +const result = await workspace.fileDownload('/remote/file.txt'); |
| 155 | +if (result.success && typeof result.content === 'string') { |
| 156 | + await fs.promises.writeFile('/local/downloaded.txt', result.content); |
| 157 | +} |
| 158 | +``` |
0 commit comments