🐛 fix(store.py): add missing import for FlowCreate and FlowRead models

 feat(store.py): add support for fetching specific fields when listing components
 feat(store.py): add support for forking components from the store
 feat(store.py): add support for searching components with various filters and sorting options
🐛 fix(schema.py): add missing parent field to ComponentResponse and StoreComponentCreate models
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-10-23 17:14:09 -03:00
commit ee0c11b4a6
2 changed files with 60 additions and 4 deletions

View file

@ -1,8 +1,10 @@
from typing import List, Optional
from uuid import UUID
from langflow.services.auth import utils as auth_utils
from langflow.services.database.models.flow.flow import FlowCreate, FlowRead
from langflow.services.database.models.user.user import User
from langflow.services.deps import (
get_session,
get_store_service,
get_settings_service,
)
@ -54,11 +56,12 @@ def list_components(
settings_service=Depends(get_settings_service),
):
try:
fields = ["id", "name", "description", "user_created"]
if store_api_Key:
decrypted = auth_utils.decrypt_api_key(store_api_Key, settings_service)
else:
decrypted = None
result = store_service.list_components(decrypted, page, limit)
result = store_service.list_components(decrypted, page, limit, fields=fields)
return result
except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc))
@ -70,6 +73,30 @@ def read_component(
store_service: StoreService = Depends(get_store_service),
store_api_Key: str = Depends(get_user_store_api_key),
settings_service=Depends(get_settings_service),
session=Depends(get_session),
):
# If the component is from the store, we need to get it from the store
try:
decrypted = auth_utils.decrypt_api_key(store_api_Key, settings_service)
component = store_service.get(decrypted, component_id)
except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
if component is None:
raise HTTPException(status_code=400, detail="Component not found")
return component
@router.post("/components/{component_id}/fork", response_model=FlowRead)
def fork_component(
component_id: UUID,
store_service: StoreService = Depends(get_store_service),
store_api_Key: str = Depends(get_user_store_api_key),
settings_service=Depends(get_settings_service),
user: User = Depends(auth_utils.get_current_active_user),
session=Depends(get_session),
):
# If the component is from the store, we need to get it from the store
try:
@ -80,7 +107,36 @@ def read_component(
if component is None:
raise HTTPException(status_code=400, detail="Component not found")
return component
# Now we need to get the component id and put it in StoreComponentCreate.parent
parent_id = component.id
new_component = StoreComponentCreate(
name=component.name,
data=component.data,
parent=parent_id,
description=component.description,
tags=component.tags,
)
try:
created_component = store_service.upload(decrypted, new_component.dict())
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
if created_component is None:
raise HTTPException(status_code=500, detail="Component not created")
# Now save it locally
db_component = FlowCreate(
name=created_component.name,
data=created_component.data,
description=created_component.description,
user_id=user.id,
)
session.add(db_component)
session.commit()
session.refresh(db_component)
return db_component
@router.get("/search", response_model=List[ComponentResponse])
@ -92,7 +148,6 @@ async def search_endpoint(
tags: Optional[List[str]] = Query(None),
date_from: Optional[datetime] = Query(None),
date_to: Optional[datetime] = Query(None),
sort_by: Optional[str] = Query("likes"),
sort: Optional[List[str]] = Query(None),
fields: Optional[List[str]] = Query(None),
store_service: "StoreService" = Depends(get_store_service),
@ -108,7 +163,6 @@ async def search_endpoint(
tags=tags,
date_from=date_from,
date_to=date_to,
sort_by=sort_by,
sort=sort,
fields=fields,
)

View file

@ -18,6 +18,7 @@ class ComponentResponse(BaseModel):
data: Optional[dict]
tags: Optional[List[int]]
likes_count: Optional[List[UUID]]
parent: Optional[UUID]
class StoreComponentCreate(BaseModel):
@ -25,3 +26,4 @@ class StoreComponentCreate(BaseModel):
description: Optional[str]
data: dict
tags: Optional[List[str]]
parent: Optional[UUID]