File management on the Colab VM will be implemented using the Jupyter Contents API.
- API:
GET /api/contents/<path>(as seen in HAR L68181). - Parameters:
authuser: 0colab-runtime-proxy-token: <session_token>
- Response: JSON with
contentfield containing an array of directory entries. - Display: Pretty-print the list (similar to
ls -For a formatted table).
- API:
PUT /api/contents/<remote_path>(as seen in HAR). - Payload: JSON body:
{ "name": "filename.txt", "path": "path/filename.txt", "type": "file", "format": "text", "content": "..." } - Base64 Encoding: Use
format: base64for binary files. - Progress: Implement a simple progress bar for large uploads by chunking or providing status updates.
- API:
GET /api/contents/<remote_path>?content=1(as seen in HAR). - Response: JSON with
contentfield. - Handling: Decodes content based on
format(text or base64) and saves it locally.
- API:
DELETE /api/contents/<remote_path>.
- Approach: Combines downloading the remote file, opening it in the user's
$EDITORlocally, and subsequently uploading the changed file if modifications were made. - State tracking: Uses a SHA-256 hash to track file changes securely and deterministically between before and after the editor is invoked.
- Fallbacks: Creates an empty local temporary file if the target file on the Colab runtime doesn't exist yet, essentially acting like
touch.
- Base URL: The backend URL obtained during session assignment.
- Proxy Token: The
colab-runtime-proxy-tokenis required for each request. - Error Handling: Handle 404 (not found) and 403 (unauthorized).
- Large Files: The Contents API might have limitations for very large files. If so, we'll implement a fallback via the kernel (streaming chunks).
TDD is mandatory for all file management features.
- Test Case: Verify
colab lscorrectly parses a JupytercontentsJSON response withtype: directoryandtype: file. - Test Case: Verify
colab uploadcorrectly base64-encodes a binary local file for thePUTpayload. - Test Case: Verify
colab downloadcorrectly decodes thecontentfield from theGETresponse and saves it locally. - Test Case: Verify
colab editsafely handles when a file is or isn't modified. - Test Case: Verify
colab editsecurely opens a system editor safely through mocks without hanging the testing environment.
- Test Case: Verify 404 responses are correctly caught and presented as a "File not found" error to the user.
- Test Case: Verify correct handling of large file uploads exceeding API limits via kernel streaming.