Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Add CRUD endpoints for system prompts #681

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/codegate/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,59 @@ async def get_workspace_messages(workspace_name: str) -> List[Conversation]:
return await dashboard.parse_messages_in_conversations(prompts_outputs)
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")


@v1.get(
"/workspaces/{workspace_name}/system-prompt",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
)
async def get_workspace_system_prompt(workspace_name: str) -> v1_models.SystemPrompt:
"""Get the system prompt for a workspace."""
try:
ws = await wscrud.get_workspace_by_name(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")

if ws.system_prompt is None:
return v1_models.SystemPrompt(prompt="")

return v1_models.SystemPrompt(prompt=ws.system_prompt)


@v1.put(
"/workspaces/{workspace_name}/system-prompt",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
status_code=204,
)
async def set_workspace_system_prompt(workspace_name: str, request: v1_models.SystemPrompt):
try:
# This already checks if the workspace exists
await wscrud.update_workspace_system_prompt(workspace_name, [request.prompt])
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")

return Response(status_code=204)


@v1.delete(
"/workspaces/{workspace_name}/system-prompt",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
status_code=204,
)
async def delete_workspace_system_prompt(workspace_name: str):
try:
# This already checks if the workspace exists
await wscrud.update_workspace_system_prompt(workspace_name, [])
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")

return Response(status_code=204)
4 changes: 4 additions & 0 deletions src/codegate/api/v1_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Workspace(pydantic.BaseModel):
is_active: bool


class SystemPrompt(pydantic.BaseModel):
prompt: str


class ActiveWorkspace(Workspace):
# TODO: use a more specific type for last_updated
last_updated: Any
Expand Down
Loading