mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-06 19:25:24 +00:00
feat: support bind plugin by name
This commit is contained in:
parent
5882950c39
commit
fca387b22d
@ -723,18 +723,22 @@ def create(name: str, webhook_url: str, description: str = ""):
|
|||||||
@plugin_app.command("bind")
|
@plugin_app.command("bind")
|
||||||
def bind(
|
def bind(
|
||||||
library_id: int = typer.Option(..., "--lib", help="ID of the library"),
|
library_id: int = typer.Option(..., "--lib", help="ID of the library"),
|
||||||
plugin_id: int = typer.Option(..., "--plugin", help="ID of the plugin"),
|
plugin: str = typer.Option(..., "--plugin", help="ID or name of the plugin"),
|
||||||
):
|
):
|
||||||
|
try:
|
||||||
|
plugin_id = int(plugin)
|
||||||
|
plugin_param = {"plugin_id": plugin_id}
|
||||||
|
except ValueError:
|
||||||
|
plugin_param = {"plugin_name": plugin}
|
||||||
|
|
||||||
response = httpx.post(
|
response = httpx.post(
|
||||||
f"{BASE_URL}/libraries/{library_id}/plugins",
|
f"{BASE_URL}/libraries/{library_id}/plugins",
|
||||||
json={"plugin_id": plugin_id},
|
json=plugin_param,
|
||||||
)
|
)
|
||||||
if 200 <= response.status_code < 300:
|
if response.status_code == 204:
|
||||||
print("Plugin bound to library successfully")
|
print("Plugin bound to library successfully")
|
||||||
else:
|
else:
|
||||||
print(
|
print(f"Failed to bind plugin to library: {response.status_code} - {response.text}")
|
||||||
f"Failed to bind plugin to library: {response.status_code} - {response.text}"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@plugin_app.command("unbind")
|
@plugin_app.command("unbind")
|
||||||
|
@ -1,4 +1,11 @@
|
|||||||
from pydantic import BaseModel, ConfigDict, DirectoryPath, HttpUrl, Field
|
from pydantic import (
|
||||||
|
BaseModel,
|
||||||
|
ConfigDict,
|
||||||
|
DirectoryPath,
|
||||||
|
HttpUrl,
|
||||||
|
Field,
|
||||||
|
model_validator,
|
||||||
|
)
|
||||||
from typing import List, Optional, Any, Dict
|
from typing import List, Optional, Any, Dict
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
@ -79,7 +86,18 @@ class NewPluginParam(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class NewLibraryPluginParam(BaseModel):
|
class NewLibraryPluginParam(BaseModel):
|
||||||
plugin_id: int
|
plugin_id: Optional[int] = None
|
||||||
|
plugin_name: Optional[str] = None
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def check_either_id_or_name(self):
|
||||||
|
plugin_id = self.plugin_id
|
||||||
|
plugin_name = self.plugin_name
|
||||||
|
if not (plugin_id or plugin_name):
|
||||||
|
raise ValueError("Either plugin_id or plugin_name must be provided")
|
||||||
|
if plugin_id is not None and plugin_name is not None:
|
||||||
|
raise ValueError("Only one of plugin_id or plugin_name should be provided")
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
class Folder(BaseModel):
|
class Folder(BaseModel):
|
||||||
@ -214,15 +232,18 @@ class FacetCount(BaseModel):
|
|||||||
highlighted: str
|
highlighted: str
|
||||||
value: str
|
value: str
|
||||||
|
|
||||||
|
|
||||||
class FacetStats(BaseModel):
|
class FacetStats(BaseModel):
|
||||||
total_values: int
|
total_values: int
|
||||||
|
|
||||||
|
|
||||||
class Facet(BaseModel):
|
class Facet(BaseModel):
|
||||||
counts: List[FacetCount]
|
counts: List[FacetCount]
|
||||||
field_name: str
|
field_name: str
|
||||||
sampled: bool
|
sampled: bool
|
||||||
stats: FacetStats
|
stats: FacetStats
|
||||||
|
|
||||||
|
|
||||||
class TextMatchInfo(BaseModel):
|
class TextMatchInfo(BaseModel):
|
||||||
best_field_score: str
|
best_field_score: str
|
||||||
best_field_weight: int
|
best_field_weight: int
|
||||||
@ -232,9 +253,11 @@ class TextMatchInfo(BaseModel):
|
|||||||
tokens_matched: int
|
tokens_matched: int
|
||||||
typo_prefix_score: int
|
typo_prefix_score: int
|
||||||
|
|
||||||
|
|
||||||
class HybridSearchInfo(BaseModel):
|
class HybridSearchInfo(BaseModel):
|
||||||
rank_fusion_score: float
|
rank_fusion_score: float
|
||||||
|
|
||||||
|
|
||||||
class SearchHit(BaseModel):
|
class SearchHit(BaseModel):
|
||||||
document: EntitySearchResult
|
document: EntitySearchResult
|
||||||
highlight: Dict[str, Any] = {}
|
highlight: Dict[str, Any] = {}
|
||||||
@ -243,12 +266,14 @@ class SearchHit(BaseModel):
|
|||||||
text_match: Optional[int] = None
|
text_match: Optional[int] = None
|
||||||
text_match_info: Optional[TextMatchInfo] = None
|
text_match_info: Optional[TextMatchInfo] = None
|
||||||
|
|
||||||
|
|
||||||
class RequestParams(BaseModel):
|
class RequestParams(BaseModel):
|
||||||
collection_name: str
|
collection_name: str
|
||||||
first_q: str
|
first_q: str
|
||||||
per_page: int
|
per_page: int
|
||||||
q: str
|
q: str
|
||||||
|
|
||||||
|
|
||||||
class SearchResult(BaseModel):
|
class SearchResult(BaseModel):
|
||||||
facet_counts: List[Facet]
|
facet_counts: List[Facet]
|
||||||
found: int
|
found: int
|
||||||
@ -257,4 +282,4 @@ class SearchResult(BaseModel):
|
|||||||
page: int
|
page: int
|
||||||
request_params: RequestParams
|
request_params: RequestParams
|
||||||
search_cutoff: bool
|
search_cutoff: bool
|
||||||
search_time_ms: int
|
search_time_ms: int
|
||||||
|
@ -602,12 +602,29 @@ def add_library_plugin(
|
|||||||
library_id: int, new_plugin: NewLibraryPluginParam, db: Session = Depends(get_db)
|
library_id: int, new_plugin: NewLibraryPluginParam, db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
library = crud.get_library_by_id(library_id, db)
|
library = crud.get_library_by_id(library_id, db)
|
||||||
if any(plugin.id == new_plugin.plugin_id for plugin in library.plugins):
|
if library is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND, detail="Library not found"
|
||||||
|
)
|
||||||
|
|
||||||
|
plugin = None
|
||||||
|
if new_plugin.plugin_id is not None:
|
||||||
|
plugin = crud.get_plugin_by_id(new_plugin.plugin_id, db)
|
||||||
|
elif new_plugin.plugin_name is not None:
|
||||||
|
plugin = crud.get_plugin_by_name(new_plugin.plugin_name, db)
|
||||||
|
|
||||||
|
if plugin is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND, detail="Plugin not found"
|
||||||
|
)
|
||||||
|
|
||||||
|
if any(p.id == plugin.id for p in library.plugins):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail="Plugin already exists in the library",
|
detail="Plugin already exists in the library",
|
||||||
)
|
)
|
||||||
crud.add_plugin_to_library(library_id, new_plugin.plugin_id, db)
|
|
||||||
|
crud.add_plugin_to_library(library_id, plugin.id, db)
|
||||||
|
|
||||||
|
|
||||||
@app.delete(
|
@app.delete(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user