|
| 1 | +# @lighthouse-tooling/sdk-wrapper |
| 2 | + |
| 3 | +Unified SDK wrapper that abstracts Lighthouse and Kavach SDK complexity for AI agents. This is a foundational component used by MCP servers and IDE extensions to interact with Lighthouse storage services. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Unified Interface**: Single SDK for all Lighthouse operations |
| 8 | +- **Authentication Management**: Automatic JWT token refresh |
| 9 | +- **Progress Tracking**: Real-time progress updates with event emission |
| 10 | +- **Error Handling**: Comprehensive error handling with retry logic |
| 11 | +- **TypeScript Support**: Full TypeScript definitions and documentation |
| 12 | +- **AI-Friendly**: Designed specifically for AI agent integration |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @lighthouse-tooling/sdk-wrapper |
| 18 | +``` |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +```typescript |
| 23 | +import { LighthouseAISDK } from "@lighthouse-tooling/sdk-wrapper"; |
| 24 | + |
| 25 | +// Initialize SDK |
| 26 | +const sdk = new LighthouseAISDK({ |
| 27 | + apiKey: "your-lighthouse-api-key", |
| 28 | + baseUrl: "https://node.lighthouse.storage", // optional |
| 29 | + timeout: 30000, // optional |
| 30 | + maxRetries: 3, // optional |
| 31 | + debug: false, // optional |
| 32 | +}); |
| 33 | + |
| 34 | +// Initialize and authenticate |
| 35 | +await sdk.initialize(); |
| 36 | + |
| 37 | +// Upload a file |
| 38 | +const fileInfo = await sdk.uploadFile("./data/model.json", { |
| 39 | + fileName: "my-model.json", |
| 40 | + mimeType: "application/json", |
| 41 | + encrypt: false, |
| 42 | + onProgress: (progress) => { |
| 43 | + console.log(`Upload progress: ${progress.percentage}%`); |
| 44 | + }, |
| 45 | +}); |
| 46 | + |
| 47 | +console.log("File uploaded:", fileInfo.hash); |
| 48 | + |
| 49 | +// Download a file |
| 50 | +await sdk.downloadFile(fileInfo.hash, "./downloads/model.json", { |
| 51 | + onProgress: (progress) => { |
| 52 | + console.log(`Download progress: ${progress.percentage}%`); |
| 53 | + }, |
| 54 | +}); |
| 55 | + |
| 56 | +// List uploaded files |
| 57 | +const files = await sdk.listFiles(10, 0); |
| 58 | +console.log("Uploaded files:", files.files); |
| 59 | + |
| 60 | +// Cleanup |
| 61 | +sdk.destroy(); |
| 62 | +``` |
| 63 | + |
| 64 | +## API Reference |
| 65 | + |
| 66 | +### LighthouseAISDK |
| 67 | + |
| 68 | +Main SDK class that provides unified access to Lighthouse functionality. |
| 69 | + |
| 70 | +#### Constructor |
| 71 | + |
| 72 | +```typescript |
| 73 | +new LighthouseAISDK(config: LighthouseConfig) |
| 74 | +``` |
| 75 | + |
| 76 | +#### Methods |
| 77 | + |
| 78 | +##### `initialize(): Promise<void>` |
| 79 | + |
| 80 | +Initialize the SDK and authenticate with Lighthouse. |
| 81 | + |
| 82 | +##### `uploadFile(filePath: string, options?: UploadOptions): Promise<FileInfo>` |
| 83 | + |
| 84 | +Upload a file to Lighthouse with progress tracking. |
| 85 | + |
| 86 | +**Parameters:** |
| 87 | + |
| 88 | +- `filePath`: Path to the file to upload |
| 89 | +- `options`: Upload configuration options |
| 90 | + |
| 91 | +**Returns:** Promise resolving to file information |
| 92 | + |
| 93 | +##### `downloadFile(cid: string, outputPath: string, options?: DownloadOptions): Promise<string>` |
| 94 | + |
| 95 | +Download a file from Lighthouse. |
| 96 | + |
| 97 | +**Parameters:** |
| 98 | + |
| 99 | +- `cid`: IPFS CID of the file to download |
| 100 | +- `outputPath`: Local path to save the downloaded file |
| 101 | +- `options`: Download configuration options |
| 102 | + |
| 103 | +**Returns:** Promise resolving to the output file path |
| 104 | + |
| 105 | +##### `getFileInfo(cid: string): Promise<FileInfo>` |
| 106 | + |
| 107 | +Get metadata and information about a file. |
| 108 | + |
| 109 | +##### `listFiles(limit?: number, offset?: number): Promise<ListFilesResponse>` |
| 110 | + |
| 111 | +List files uploaded by the authenticated user. |
| 112 | + |
| 113 | +##### `getAuthState(): AuthState` |
| 114 | + |
| 115 | +Get current authentication state. |
| 116 | + |
| 117 | +##### `getActiveOperations(): string[]` |
| 118 | + |
| 119 | +Get list of active operation IDs. |
| 120 | + |
| 121 | +##### `getOperationProgress(operationId: string): ProgressInfo | null` |
| 122 | + |
| 123 | +Get progress information for a specific operation. |
| 124 | + |
| 125 | +##### `cancelOperation(operationId: string): void` |
| 126 | + |
| 127 | +Cancel an ongoing operation. |
| 128 | + |
| 129 | +##### `destroy(): void` |
| 130 | + |
| 131 | +Cleanup resources and disconnect. |
| 132 | + |
| 133 | +### Events |
| 134 | + |
| 135 | +The SDK emits various events for progress tracking and status updates: |
| 136 | + |
| 137 | +```typescript |
| 138 | +sdk.on("upload:start", (event) => console.log("Upload started")); |
| 139 | +sdk.on("upload:progress", (event) => |
| 140 | + console.log("Upload progress:", event.data) |
| 141 | +); |
| 142 | +sdk.on("upload:complete", (event) => console.log("Upload completed")); |
| 143 | +sdk.on("upload:error", (event) => console.error("Upload failed:", event.error)); |
| 144 | + |
| 145 | +sdk.on("download:start", (event) => console.log("Download started")); |
| 146 | +sdk.on("download:progress", (event) => |
| 147 | + console.log("Download progress:", event.data) |
| 148 | +); |
| 149 | +sdk.on("download:complete", (event) => console.log("Download completed")); |
| 150 | +sdk.on("download:error", (event) => |
| 151 | + console.error("Download failed:", event.error) |
| 152 | +); |
| 153 | + |
| 154 | +sdk.on("auth:refresh", () => console.log("Token refreshed")); |
| 155 | +sdk.on("auth:error", (error) => console.error("Auth error:", error)); |
| 156 | +``` |
| 157 | + |
| 158 | +## Configuration |
| 159 | + |
| 160 | +### LighthouseConfig |
| 161 | + |
| 162 | +```typescript |
| 163 | +interface LighthouseConfig { |
| 164 | + /** API key for authentication */ |
| 165 | + apiKey: string; |
| 166 | + /** Base URL for Lighthouse API (optional) */ |
| 167 | + baseUrl?: string; |
| 168 | + /** Timeout for requests in milliseconds */ |
| 169 | + timeout?: number; |
| 170 | + /** Maximum number of retry attempts */ |
| 171 | + maxRetries?: number; |
| 172 | + /** Enable debug logging */ |
| 173 | + debug?: boolean; |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +### UploadOptions |
| 178 | + |
| 179 | +```typescript |
| 180 | +interface UploadOptions { |
| 181 | + /** File name override */ |
| 182 | + fileName?: string; |
| 183 | + /** MIME type override */ |
| 184 | + mimeType?: string; |
| 185 | + /** Progress callback function */ |
| 186 | + onProgress?: (progress: ProgressInfo) => void; |
| 187 | + /** Enable encryption */ |
| 188 | + encrypt?: boolean; |
| 189 | + /** Custom metadata */ |
| 190 | + metadata?: Record<string, any>; |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +### DownloadOptions |
| 195 | + |
| 196 | +```typescript |
| 197 | +interface DownloadOptions { |
| 198 | + /** Progress callback function */ |
| 199 | + onProgress?: (progress: ProgressInfo) => void; |
| 200 | + /** Expected file size for progress calculation */ |
| 201 | + expectedSize?: number; |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +## Error Handling |
| 206 | + |
| 207 | +The SDK provides comprehensive error handling with automatic retry logic for transient failures: |
| 208 | + |
| 209 | +```typescript |
| 210 | +try { |
| 211 | + const fileInfo = await sdk.uploadFile("./large-file.zip"); |
| 212 | + console.log("Upload successful:", fileInfo.hash); |
| 213 | +} catch (error) { |
| 214 | + if (error.message.includes("network")) { |
| 215 | + console.log("Network error - will retry automatically"); |
| 216 | + } else { |
| 217 | + console.error("Upload failed:", error.message); |
| 218 | + } |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +## Development |
| 223 | + |
| 224 | +### Building |
| 225 | + |
| 226 | +```bash |
| 227 | +npm run build |
| 228 | +``` |
| 229 | + |
| 230 | +### Testing |
| 231 | + |
| 232 | +```bash |
| 233 | +npm test |
| 234 | +npm run test:watch |
| 235 | +npm run test:coverage |
| 236 | +``` |
| 237 | + |
| 238 | +### Linting |
| 239 | + |
| 240 | +```bash |
| 241 | +npm run lint |
| 242 | +npm run lint:fix |
| 243 | +``` |
| 244 | + |
| 245 | +## Contributing |
| 246 | + |
| 247 | +1. Fork the repository |
| 248 | +2. Create a feature branch |
| 249 | +3. Make your changes |
| 250 | +4. Add tests for new functionality |
| 251 | +5. Ensure all tests pass |
| 252 | +6. Submit a pull request |
| 253 | + |
| 254 | +## License |
| 255 | + |
| 256 | +MIT License - see LICENSE file for details. |
0 commit comments