diff --git a/memos/commands.py b/memos/commands.py index 11053f0..1064be6 100644 --- a/memos/commands.py +++ b/memos/commands.py @@ -273,5 +273,22 @@ def create(name: str, webhook_url: str, description: str = ""): print(f"Failed to create plugin: {response.status_code} - {response.text}") +@plugin_app.command("bind") +def bind( + library_id: int = typer.Option(..., "--lib", help="ID of the library"), + plugin_id: int = typer.Option(..., "--plugin", help="ID of the plugin"), +): + response = httpx.post( + f"{BASE_URL}/libraries/{library_id}/plugins", + json={"plugin_id": plugin_id}, + ) + if 200 <= response.status_code < 300: + print("Plugin bound to library successfully") + else: + print( + f"Failed to bind plugin to library: {response.status_code} - {response.text}" + ) + + if __name__ == "__main__": app() diff --git a/memos/server.py b/memos/server.py index 9e04a85..11c4fdc 100644 --- a/memos/server.py +++ b/memos/server.py @@ -211,6 +211,12 @@ def list_plugins(db: Session = Depends(get_db)): def add_library_plugin( library_id: int, new_plugin: NewLibraryPluginParam, db: Session = Depends(get_db) ): + library = crud.get_library_by_id(library_id, db) + if any(plugin.id == new_plugin.plugin_id for plugin in library.plugins): + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Plugin already exists in the library", + ) crud.add_plugin_to_library(library_id, new_plugin.plugin_id, db)