|
| 1 | +from fastapi import Depends, HTTPException |
| 2 | +from fastapi_versionizer.versionizer import api_version |
| 3 | + |
| 4 | +from skynet.auth.customer_id import CustomerId |
| 5 | +from skynet.env import summary_minimum_payload_length |
| 6 | +from skynet.logs import get_logger |
| 7 | +from skynet.modules.ttt.customer_configs.utils import get_customerconfig_key, get_existing_customer_config |
| 8 | +from skynet.modules.ttt.customer_configs.v1.models import CustomerConfig, CustomerConfigPayload, CustomerConfigResponse |
| 9 | +from skynet.modules.ttt.persistence import db |
| 10 | +from skynet.utils import get_router |
| 11 | + |
| 12 | +router = get_router() |
| 13 | +log = get_logger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def validate_customer_config_payload(payload: CustomerConfigPayload) -> None: |
| 17 | + if not payload.summary_prompt.strip(): |
| 18 | + raise HTTPException(status_code=422, detail="summary_prompt cannot be empty") |
| 19 | + |
| 20 | + if len(payload.summary_prompt.strip()) < summary_minimum_payload_length: |
| 21 | + raise HTTPException( |
| 22 | + status_code=422, detail=f"summary_prompt must be at least {summary_minimum_payload_length} characters" |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +@api_version(1) |
| 27 | +@router.get('/config') |
| 28 | +async def get_customer_config(customer_id=Depends(CustomerId())) -> CustomerConfig: |
| 29 | + """ |
| 30 | + Get the current customer config. |
| 31 | + """ |
| 32 | + config = await get_existing_customer_config(customer_id) |
| 33 | + |
| 34 | + if config: |
| 35 | + return CustomerConfig(summary_prompt=config.get('summary_prompt')) |
| 36 | + |
| 37 | + raise HTTPException(status_code=404, detail='Customer configuration not found') |
| 38 | + |
| 39 | + |
| 40 | +@api_version(1) |
| 41 | +@router.post('/config', dependencies=[Depends(validate_customer_config_payload)]) |
| 42 | +async def set_customer_config( |
| 43 | + payload: CustomerConfigPayload, customer_id=Depends(CustomerId()) |
| 44 | +) -> CustomerConfigResponse: |
| 45 | + """ |
| 46 | + Set the customer config. |
| 47 | + """ |
| 48 | + # Store in database |
| 49 | + key = get_customerconfig_key(customer_id) |
| 50 | + config = {'summary_prompt': payload.summary_prompt} |
| 51 | + |
| 52 | + import json |
| 53 | + |
| 54 | + await db.set(key, json.dumps(config)) |
| 55 | + |
| 56 | + log.info(f"Updated customer config for customer {customer_id}") |
| 57 | + |
| 58 | + return CustomerConfigResponse() |
| 59 | + |
| 60 | + |
| 61 | +@api_version(1) |
| 62 | +@router.delete('/config') |
| 63 | +async def delete_customer_config(customer_id=Depends(CustomerId())) -> CustomerConfigResponse: |
| 64 | + """ |
| 65 | + Delete the customer config. |
| 66 | + """ |
| 67 | + config = await get_existing_customer_config(customer_id) |
| 68 | + |
| 69 | + if not config: |
| 70 | + raise HTTPException(status_code=404, detail='Customer configuration not found') |
| 71 | + |
| 72 | + key = get_customerconfig_key(customer_id) |
| 73 | + await db.delete(key) |
| 74 | + |
| 75 | + log.info(f"Deleted customer config for customer {customer_id}") |
| 76 | + |
| 77 | + return CustomerConfigResponse() |
0 commit comments