diff --git a/memos/commands.py b/memos/commands.py index 98dd59c..11053f0 100644 --- a/memos/commands.py +++ b/memos/commands.py @@ -13,14 +13,21 @@ from tqdm import tqdm app = typer.Typer() lib_app = typer.Typer() +plugin_app = typer.Typer() +app.add_typer(plugin_app, name="plugin") app.add_typer(lib_app, name="lib") BASE_URL = "http://localhost:8080" + def format_timestamp(timestamp): if isinstance(timestamp, str): return timestamp - return datetime.fromtimestamp(timestamp, tz=timezone.utc).replace(tzinfo=None).isoformat() + return ( + datetime.fromtimestamp(timestamp, tz=timezone.utc) + .replace(tzinfo=None) + .isoformat() + ) def display_libraries(libraries): @@ -111,7 +118,9 @@ def scan(library_id: int): pbar.update(1) file_path = Path(root) / file absolute_file_path = file_path.resolve() # Get absolute path - scanned_files.add(str(absolute_file_path)) # Add to scanned files set + scanned_files.add( + str(absolute_file_path) + ) # Add to scanned files set file_stat = file_path.stat() file_type = ( mimetypes.guess_type(file_path)[0] or "application/octet-stream" @@ -195,7 +204,7 @@ def scan(library_id: int): while True: existing_files_response = httpx.get( f"{BASE_URL}/libraries/{library_id}/folders/{folder['id']}/entities", - params={"limit": limit, "offset": offset} + params={"limit": limit, "offset": offset}, ) if existing_files_response.status_code != 200: tqdm.write( @@ -214,7 +223,9 @@ def scan(library_id: int): f"{BASE_URL}/libraries/{library_id}/entities/{existing_file['id']}" ) if 200 <= delete_response.status_code < 300: - tqdm.write(f"Deleted file from library: {existing_file['filepath']}") + tqdm.write( + f"Deleted file from library: {existing_file['filepath']}" + ) total_files_deleted += 1 else: tqdm.write( @@ -228,5 +239,39 @@ def scan(library_id: int): print(f"Total files deleted: {total_files_deleted}") +def display_plugins(plugins): + table = [] + for plugin in plugins: + table.append( + [plugin["id"], plugin["name"], plugin["description"], plugin["webhook_url"]] + ) + print( + tabulate( + table, + headers=["ID", "Name", "Description", "Webhook URL"], + tablefmt="plain", + ) + ) + + +@plugin_app.command("ls") +def ls(): + response = httpx.get(f"{BASE_URL}/plugins") + plugins = response.json() + display_plugins(plugins) + + +@plugin_app.command("create") +def create(name: str, webhook_url: str, description: str = ""): + response = httpx.post( + f"{BASE_URL}/plugins", + json={"name": name, "description": description, "webhook_url": webhook_url}, + ) + if 200 <= response.status_code < 300: + print("Plugin created successfully") + else: + print(f"Failed to create plugin: {response.status_code} - {response.text}") + + if __name__ == "__main__": app() diff --git a/memos/crud.py b/memos/crud.py index ce1db21..4b55203 100644 --- a/memos/crud.py +++ b/memos/crud.py @@ -122,6 +122,10 @@ def create_plugin(newPlugin: NewPluginParam, db: Session) -> Plugin: return db_plugin +def get_plugins(db: Session) -> List[Plugin]: + return db.query(PluginModel).all() + + def get_plugin_by_name(plugin_name: str, db: Session) -> Plugin | None: return ( db.query(PluginModel) diff --git a/memos/server.py b/memos/server.py index a7d602e..9e04a85 100644 --- a/memos/server.py +++ b/memos/server.py @@ -200,6 +200,13 @@ def new_plugin(new_plugin: NewPluginParam, db: Session = Depends(get_db)): return plugin +@app.get("/plugins", response_model=List[Plugin]) +def list_plugins(db: Session = Depends(get_db)): + plugins = crud.get_plugins(db) + return plugins + + + @app.post("/libraries/{library_id}/plugins", status_code=status.HTTP_204_NO_CONTENT) def add_library_plugin( library_id: int, new_plugin: NewLibraryPluginParam, db: Session = Depends(get_db)