-
Notifications
You must be signed in to change notification settings - Fork 78
Created necessary methods for Persona CRUD #1232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,12 @@ | |
from codegate import __version__ | ||
from codegate.api import v1_models, v1_processing | ||
from codegate.db.connection import AlreadyExistsError, DbReader | ||
from codegate.db.models import AlertSeverity, WorkspaceWithModel | ||
from codegate.db.models import AlertSeverity, Persona, WorkspaceWithModel | ||
from codegate.muxing.persona import ( | ||
PersonaDoesNotExistError, | ||
PersonaManager, | ||
PersonaSimilarDescriptionError, | ||
) | ||
from codegate.providers import crud as provendcrud | ||
from codegate.workspaces import crud | ||
|
||
|
@@ -21,6 +26,7 @@ | |
v1 = APIRouter() | ||
wscrud = crud.WorkspaceCrud() | ||
pcrud = provendcrud.ProviderCrud() | ||
persona_manager = PersonaManager() | ||
|
||
# This is a singleton object | ||
dbreader = DbReader() | ||
|
@@ -665,3 +671,89 @@ async def get_workspace_token_usage(workspace_name: str) -> v1_models.TokenUsage | |
except Exception: | ||
logger.exception("Error while getting messages") | ||
raise HTTPException(status_code=500, detail="Internal server error") | ||
|
||
|
||
@v1.get("/personas", tags=["Personas"], generate_unique_id_function=uniq_name) | ||
async def list_personas() -> List[Persona]: | ||
"""List all personas.""" | ||
try: | ||
personas = await dbreader.get_all_personas() | ||
return personas | ||
except Exception: | ||
logger.exception("Error while getting personas") | ||
raise HTTPException(status_code=500, detail="Internal server error") | ||
|
||
|
||
@v1.get("/personas/{persona_name}", tags=["Personas"], generate_unique_id_function=uniq_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use an annotated object to do input validation on |
||
async def get_persona(persona_name: str) -> Persona: | ||
"""Get a persona by name.""" | ||
try: | ||
persona = await dbreader.get_persona_by_name(persona_name) | ||
if not persona: | ||
raise HTTPException(status_code=404, detail=f"Persona {persona_name} not found") | ||
return persona | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we be catching the exception when a persona doesn't exist? |
||
except Exception as e: | ||
if isinstance(e, HTTPException): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the db reader be raising an HTTP exception? This sounds off |
||
raise e | ||
logger.exception(f"Error while getting persona {persona_name}") | ||
raise HTTPException(status_code=500, detail="Internal server error") | ||
|
||
|
||
@v1.post("/personas", tags=["Personas"], generate_unique_id_function=uniq_name, status_code=201) | ||
async def create_persona(request: v1_models.PersonaRequest) -> Persona: | ||
"""Create a new persona.""" | ||
try: | ||
await persona_manager.add_persona(request.name, request.description) | ||
persona = await dbreader.get_persona_by_name(request.name) | ||
return persona | ||
except PersonaSimilarDescriptionError: | ||
logger.exception("Error while creating persona") | ||
raise HTTPException(status_code=409, detail="Persona has a similar description to another") | ||
except AlreadyExistsError: | ||
logger.exception("Error while creating persona") | ||
raise HTTPException(status_code=409, detail="Persona already exists") | ||
except Exception: | ||
logger.exception("Error while creating persona") | ||
raise HTTPException(status_code=500, detail="Internal server error") | ||
|
||
|
||
@v1.put("/personas/{persona_name}", tags=["Personas"], generate_unique_id_function=uniq_name) | ||
async def update_persona(persona_name: str, request: v1_models.PersonaUpdateRequest) -> Persona: | ||
"""Update an existing persona.""" | ||
try: | ||
await persona_manager.update_persona( | ||
persona_name, request.new_name, request.new_description | ||
) | ||
persona = await dbreader.get_persona_by_name(request.new_name) | ||
return persona | ||
except PersonaSimilarDescriptionError: | ||
logger.exception("Error while updating persona") | ||
raise HTTPException(status_code=409, detail="Persona has a similar description to another") | ||
except PersonaDoesNotExistError: | ||
logger.exception("Error while updating persona") | ||
raise HTTPException(status_code=404, detail="Persona does not exist") | ||
except AlreadyExistsError: | ||
logger.exception("Error while updating persona") | ||
raise HTTPException(status_code=409, detail="Persona already exists") | ||
except Exception: | ||
logger.exception("Error while updating persona") | ||
raise HTTPException(status_code=500, detail="Internal server error") | ||
|
||
|
||
@v1.delete( | ||
"/personas/{persona_name}", | ||
tags=["Personas"], | ||
generate_unique_id_function=uniq_name, | ||
status_code=204, | ||
) | ||
async def delete_persona(persona_name: str): | ||
"""Delete a persona.""" | ||
try: | ||
await persona_manager.delete_persona(persona_name) | ||
return Response(status_code=204) | ||
except PersonaDoesNotExistError: | ||
logger.exception("Error while updating persona") | ||
raise HTTPException(status_code=404, detail="Persona does not exist") | ||
except Exception: | ||
logger.exception("Error while deleting persona") | ||
raise HTTPException(status_code=500, detail="Internal server error") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not leverage the persona manager here to keep the code consistent? Adding the dbreader here seems like an antipattern