Skip to content

Commit

Permalink
v0.1.5 - expose formatted list function
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Jul 28, 2023
1 parent adab6af commit 8583241
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
11 changes: 10 additions & 1 deletion agentcomlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
remove_file,
update_file,
list_files,
list_files_formatted,
get_file,
)
from .server import start_server, get_server, set_storage_path, send_message, register_message_handler, unregister_message_handler
from .server import (
start_server,
get_server,
set_storage_path,
send_message,
register_message_handler,
unregister_message_handler,
)

__all__ = [
"get_storage_path",
Expand All @@ -21,5 +29,6 @@
"remove_file",
"update_file",
"list_files",
"list_files_formatted",
"get_file",
]
64 changes: 63 additions & 1 deletion agentcomlink/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@ class FilePath(BaseModel):


def get_server():
"""Retrieve the global FastAPI instance."""
global app
return app


# get the path of the folder that is parent to this one
def get_parent_path():
"""Return the absolute path of the parent directory of this script."""
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def start_server(storage_path=None, port=8000):
"""
Start the FastAPI server.
:param storage_path: The path where to store the files.
:param port: The port on which to start the server.
:return: The FastAPI application.
"""
global app
if storage_path:
set_storage_path(storage_path)
Expand All @@ -52,16 +60,23 @@ def start_server(storage_path=None, port=8000):


def stop_server():
"""Stop the FastAPI server by setting the global app to None."""
global app
app = None


@app.get("/")
async def get():
"""Handle a GET request to the root of the server, responding with an HTML page."""
return HTMLResponse(page)


def send_message(message):
"""
Send a message to the websocket.
:param message: The message to send.
"""
global ws
if ws is not None:
print("send text")
Expand All @@ -70,17 +85,32 @@ def send_message(message):


def register_message_handler(handler):
"""
Register a handler for messages received through the websocket.
:param handler: The handler to register.
"""
global handlers
handlers.append(handler)


def unregister_message_handler(handler):
"""
Unregister a handler for messages received through the websocket.
:param handler: The handler to unregister.
"""
global handlers
handlers.remove(handler)


@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
"""
Establish a websocket connection.
:param websocket: The websocket through which to communicate.
"""
global ws
ws = websocket
await websocket.accept()
Expand All @@ -99,6 +129,13 @@ async def websocket_endpoint(websocket: WebSocket):

@router.post("/file/")
async def http_add_file(path: str = Form(...), file: UploadFile = File(...)):
"""
Create a new file at a given path with the provided content.
:param path: The path where to create the file.
:param file: The content to put in the file.
:return: A success message.
"""
check_files()
with open(os.path.join(storage_path, path), "wb") as f:
f.write(await file.read())
Expand All @@ -107,6 +144,12 @@ async def http_add_file(path: str = Form(...), file: UploadFile = File(...)):

@router.delete("/file/{path}")
def http_remove_file(path: str):
"""
Delete a file at a given path.
:param path: The path of the file to delete.
:return: A success message.
"""
check_files()
try:
os.remove(os.path.join(storage_path, path))
Expand All @@ -117,6 +160,13 @@ def http_remove_file(path: str):

@router.put("/file/")
async def http_update_file(path: str = Form(...), file: UploadFile = File(...)):
"""
Update a file at a given path with the provided content.
:param path: The path of the file to update.
:param file: The new content to put in the file.
:return: A success message.
"""
check_files()
with open(os.path.join(storage_path, path), "wb") as f:
f.write(await file.read())
Expand All @@ -125,12 +175,24 @@ async def http_update_file(path: str = Form(...), file: UploadFile = File(...)):

@router.get("/files/")
def http_list_files(path: str = "."):
"""
List all files in a given directory.
:param path: The path of the directory.
:return: A list of files.
"""
check_files()
return {"files": os.listdir(os.path.join(storage_path, path))}


@router.get("/file/{path}")
def http_get_file(path: str):
"""
Retrieve a file at a given path.
:param path: The path of the file to retrieve.
:return: The file as a response.
"""
check_files()
try:
return FileResponse(os.path.join(storage_path, path))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='agentcomlink',
version='0.1.4',
version='0.1.5',
description='Simple chat, debug and file management panel for agents',
long_description=long_description, # added this line
long_description_content_type="text/markdown", # and this line
Expand Down

0 comments on commit 8583241

Please sign in to comment.