Files¶
Understanding Files in Hide¶
In Hide, the Files API allows coding agents to interact with the project's file system within the devcontainer environment. This enables operations such as creating, reading, updating, and deleting files. All file paths are relative to the project's root directory.
Note
For all code examples, the server is assumed to be running on localhost:8080
. Adjust the URL if your Hide server is running on a different host or port.
Note
For all requests, replace {project_id}
with your actual project ID.
Creating a File¶
To create a new file in your project:
This will create a file named example.txt
in your project's root directory with the content Hello, World!
.
Listing Files¶
To list all files in your project:
This will return a list of all files recursively in your project's root directory.
Reading a File¶
To read the contents of a specific file:
This will return the contents of the file example.txt
in your project's root directory.
Reading files supports different parameters such as specifying a range of lines, or including the line numbers in the response. To include the line numbers, set the showLineNumbers
parameter to true
:
To specify a range of lines, set the startLine
and numLines
parameters:
Updating a File¶
Updating files can be done in three ways: by replacing the entire file, by updating lines, or by applying unified diffs. We will look at each of these in the next sections.
Replacing the entire file¶
To replace the entire file, we can use the update type overwrite
and provide the new content as the content
parameter:
This will replace the entire file with the new content.
Updating lines¶
To update lines, we can use the update type linediff
and provide the line diff as the lineDiff
parameter:
This will update the lines from line 1 to 3 with the new content.
Applying unified diffs¶
To apply unified diffs, we can use the update type udiff
and provide the patch as the patch
parameter:
from hide.model import FileUpdateType, UdiffUpdate
result = hide_client.update_file(
project_id="my-project",
path="path/to/file.py",
type=FileUpdateType.UDIFF,
udiff=UdiffUpdate(
patch="""--- path/to/file.py
+++ path/to/file.py
@@ -1,2 +1,2 @@
def hello_world():
- print('Hello, World!')
+ print('Hello, World!!!')
"""
)
)
This will apply the unified diff to the file and update the lines accordingly.
Deleting a File¶
To delete a specific file:
This will delete the file example.txt
in your project's root directory.
Error Handling¶
The API uses standard HTTP status codes to indicate the success or failure of requests:
- 200: Successful operation
- 404: File or project not found
- 400: Bad request (e.g., invalid input)
- 500: Internal server error
Always check the status code and response body for detailed error messages.