Skip to content
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
7 changes: 3 additions & 4 deletions src/app/endpoints/authorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

from fastapi import APIRouter, Depends

from authentication.interface import AuthTuple
from authentication import get_auth_dependency
from models.responses import AuthorizedResponse, UnauthorizedResponse, ForbiddenResponse
from authentication.interface import AuthTuple
from models.responses import AuthorizedResponse, ForbiddenResponse, UnauthorizedResponse

logger = logging.getLogger(__name__)
router = APIRouter(tags=["authorized"])
auth_dependency = get_auth_dependency()


authorized_responses: dict[int | str, dict[str, Any]] = {
Expand All @@ -38,7 +37,7 @@

@router.post("/authorized", responses=authorized_responses)
async def authorized_endpoint_handler(
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
) -> AuthorizedResponse:
"""
Handle request to the /authorized endpoint.
Expand Down
8 changes: 3 additions & 5 deletions src/app/endpoints/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import logging
from typing import Annotated, Any

from fastapi import APIRouter, Request, Depends
from fastapi import APIRouter, Depends, Request

from authentication.interface import AuthTuple
from authentication import get_auth_dependency
from authentication.interface import AuthTuple
from authorization.middleware import authorize
from configuration import configuration
from models.config import Action, Configuration
Expand All @@ -15,8 +15,6 @@
logger = logging.getLogger(__name__)
router = APIRouter(tags=["config"])

auth_dependency = get_auth_dependency()


get_config_responses: dict[int | str, dict[str, Any]] = {
200: {
Expand Down Expand Up @@ -63,7 +61,7 @@
@router.get("/config", responses=get_config_responses)
@authorize(Action.GET_CONFIG)
async def config_endpoint_handler(
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
request: Request,
) -> Configuration:
"""
Expand Down
18 changes: 8 additions & 10 deletions src/app/endpoints/conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
import logging
from typing import Any

from fastapi import APIRouter, Depends, HTTPException, Request, status
from llama_stack_client import APIConnectionError, NotFoundError

from fastapi import APIRouter, HTTPException, Request, status, Depends

from client import AsyncLlamaStackClientHolder
from configuration import configuration
from app.database import get_session
from authentication import get_auth_dependency
from authorization.middleware import authorize
from client import AsyncLlamaStackClientHolder
from configuration import configuration
from models.config import Action
from models.database.conversations import UserConversation
from models.responses import (
ConversationResponse,
ConversationDeleteResponse,
ConversationsListResponse,
ConversationDetails,
ConversationResponse,
ConversationsListResponse,
UnauthorizedResponse,
)
from utils.endpoints import (
Expand All @@ -30,7 +29,6 @@

logger = logging.getLogger("app.endpoints.handlers")
router = APIRouter(tags=["conversations"])
auth_dependency = get_auth_dependency()

conversation_responses: dict[int | str, dict[str, Any]] = {
200: {
Expand Down Expand Up @@ -180,7 +178,7 @@ def simplify_session_data(session_data: dict) -> list[dict[str, Any]]:
@authorize(Action.LIST_CONVERSATIONS)
async def get_conversations_list_endpoint_handler(
request: Request,
auth: Any = Depends(auth_dependency),
auth: Any = Depends(get_auth_dependency()),
) -> ConversationsListResponse:
"""Handle request to retrieve all conversations for the authenticated user."""
check_configuration_loaded(configuration)
Expand Down Expand Up @@ -242,7 +240,7 @@ async def get_conversations_list_endpoint_handler(
async def get_conversation_endpoint_handler(
request: Request,
conversation_id: str,
auth: Any = Depends(auth_dependency),
auth: Any = Depends(get_auth_dependency()),
) -> ConversationResponse:
"""
Handle request to retrieve a conversation by ID.
Expand Down Expand Up @@ -370,7 +368,7 @@ async def get_conversation_endpoint_handler(
async def delete_conversation_endpoint_handler(
request: Request,
conversation_id: str,
auth: Any = Depends(auth_dependency),
auth: Any = Depends(get_auth_dependency()),
) -> ConversationDeleteResponse:
"""
Handle request to delete a conversation by ID.
Expand Down
15 changes: 7 additions & 8 deletions src/app/endpoints/conversations_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
import logging
from typing import Any

from fastapi import APIRouter, Request, Depends, HTTPException, status
from fastapi import APIRouter, Depends, HTTPException, Request, status

from configuration import configuration
from authentication import get_auth_dependency
from authorization.middleware import authorize
from configuration import configuration
from models.cache_entry import CacheEntry
from models.config import Action
from models.responses import (
ConversationsListResponseV2,
ConversationResponse,
ConversationDeleteResponse,
ConversationResponse,
ConversationsListResponseV2,
UnauthorizedResponse,
)
from utils.endpoints import check_configuration_loaded
from utils.suid import check_suid

logger = logging.getLogger("app.endpoints.handlers")
router = APIRouter(tags=["conversations_v2"])
auth_dependency = get_auth_dependency()


conversation_responses: dict[int | str, dict[str, Any]] = {
Expand Down Expand Up @@ -93,7 +92,7 @@
@authorize(Action.LIST_CONVERSATIONS)
async def get_conversations_list_endpoint_handler(
request: Request, # pylint: disable=unused-argument
auth: Any = Depends(auth_dependency),
auth: Any = Depends(get_auth_dependency()),
) -> ConversationsListResponseV2:
"""Handle request to retrieve all conversations for the authenticated user."""
check_configuration_loaded(configuration)
Expand Down Expand Up @@ -123,7 +122,7 @@ async def get_conversations_list_endpoint_handler(
async def get_conversation_endpoint_handler(
request: Request, # pylint: disable=unused-argument
conversation_id: str,
auth: Any = Depends(auth_dependency),
auth: Any = Depends(get_auth_dependency()),
) -> ConversationResponse:
"""Handle request to retrieve a conversation by ID."""
check_configuration_loaded(configuration)
Expand Down Expand Up @@ -159,7 +158,7 @@ async def get_conversation_endpoint_handler(
async def delete_conversation_endpoint_handler(
request: Request, # pylint: disable=unused-argument
conversation_id: str,
auth: Any = Depends(auth_dependency),
auth: Any = Depends(get_auth_dependency()),
) -> ConversationDeleteResponse:
"""Handle request to delete a conversation by ID."""
check_configuration_loaded(configuration)
Expand Down
16 changes: 8 additions & 8 deletions src/app/endpoints/feedback.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Handler for REST API endpoint for user feedback."""

import json
import logging
import threading
from typing import Annotated, Any
from datetime import UTC, datetime
from pathlib import Path
import json
from datetime import datetime, UTC
from fastapi import APIRouter, HTTPException, Depends, Request, status
from typing import Annotated, Any

from fastapi import APIRouter, Depends, HTTPException, Request, status

from authentication import get_auth_dependency
from authentication.interface import AuthTuple
Expand All @@ -18,15 +19,14 @@
ErrorResponse,
FeedbackResponse,
FeedbackStatusUpdateResponse,
ForbiddenResponse,
StatusResponse,
UnauthorizedResponse,
ForbiddenResponse,
)
from utils.suid import get_suid

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/feedback", tags=["feedback"])
auth_dependency = get_auth_dependency()
feedback_status_lock = threading.Lock()

# Response for the feedback endpoint
Expand Down Expand Up @@ -87,7 +87,7 @@ async def assert_feedback_enabled(_request: Request) -> None:
@authorize(Action.FEEDBACK)
async def feedback_endpoint_handler(
feedback_request: FeedbackRequest,
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
_ensure_feedback_enabled: Any = Depends(assert_feedback_enabled),
) -> FeedbackResponse:
"""Handle feedback requests.
Expand Down Expand Up @@ -183,7 +183,7 @@ def feedback_status() -> StatusResponse:
@authorize(Action.ADMIN)
async def update_feedback_status(
feedback_update_request: FeedbackStatusUpdateRequest,
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
) -> FeedbackStatusUpdateResponse:
"""
Handle feedback status update requests.
Expand Down
14 changes: 6 additions & 8 deletions src/app/endpoints/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@
import logging
from typing import Annotated, Any

from fastapi import APIRouter, Depends, Response, status
from llama_stack.providers.datatypes import HealthStatus

from fastapi import APIRouter, status, Response, Depends
from client import AsyncLlamaStackClientHolder
from authentication.interface import AuthTuple
from authentication import get_auth_dependency
from authentication.interface import AuthTuple
from authorization.middleware import authorize
from client import AsyncLlamaStackClientHolder
from models.config import Action
from models.responses import (
LivenessResponse,
ReadinessResponse,
ProviderHealthStatus,
ReadinessResponse,
)

logger = logging.getLogger("app.endpoints.handlers")
router = APIRouter(tags=["health"])

auth_dependency = get_auth_dependency()


async def get_providers_health_statuses() -> list[ProviderHealthStatus]:
"""
Expand Down Expand Up @@ -80,7 +78,7 @@ async def get_providers_health_statuses() -> list[ProviderHealthStatus]:
@router.get("/readiness", responses=get_readiness_responses)
@authorize(Action.INFO)
async def readiness_probe_get_method(
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
response: Response,
) -> ReadinessResponse:
"""
Expand Down Expand Up @@ -126,7 +124,7 @@ async def readiness_probe_get_method(
@router.get("/liveness", responses=get_liveness_responses)
@authorize(Action.INFO)
async def liveness_probe_get_method(
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
) -> LivenessResponse:
"""
Return the liveness status of the service.
Expand Down
11 changes: 4 additions & 7 deletions src/app/endpoints/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
import logging
from typing import Annotated, Any

from fastapi import APIRouter, HTTPException, Request, status
from fastapi import Depends
from fastapi import APIRouter, Depends, HTTPException, Request, status
from llama_stack_client import APIConnectionError

from authentication.interface import AuthTuple
from authentication import get_auth_dependency
from authentication.interface import AuthTuple
from authorization.middleware import authorize
from configuration import configuration
from client import AsyncLlamaStackClientHolder
from configuration import configuration
from models.config import Action
from models.responses import InfoResponse
from version import __version__

logger = logging.getLogger("app.endpoints.handlers")
router = APIRouter(tags=["info"])

auth_dependency = get_auth_dependency()


get_info_responses: dict[int | str, dict[str, Any]] = {
200: {
Expand All @@ -40,7 +37,7 @@
@router.get("/info", responses=get_info_responses)
@authorize(Action.INFO)
async def info_endpoint_handler(
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
request: Request,
) -> InfoResponse:
"""
Expand Down
13 changes: 6 additions & 7 deletions src/app/endpoints/metrics.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
"""Handler for REST API call to provide metrics."""

from typing import Annotated

from fastapi import APIRouter, Depends, Request
from fastapi.responses import PlainTextResponse
from fastapi import APIRouter, Request, Depends
from prometheus_client import (
generate_latest,
CONTENT_TYPE_LATEST,
generate_latest,
)

from authentication.interface import AuthTuple
from authentication import get_auth_dependency
from authentication.interface import AuthTuple
from authorization.middleware import authorize
from models.config import Action
from metrics.utils import setup_model_metrics
from models.config import Action

router = APIRouter(tags=["metrics"])

auth_dependency = get_auth_dependency()


@router.get("/metrics", response_class=PlainTextResponse)
@authorize(Action.GET_METRICS)
async def metrics_endpoint_handler(
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
request: Request,
) -> PlainTextResponse:
"""
Expand Down
7 changes: 2 additions & 5 deletions src/app/endpoints/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

from authentication import get_auth_dependency
from authentication.interface import AuthTuple
from authorization.middleware import authorize
from client import AsyncLlamaStackClientHolder
from configuration import configuration
from authorization.middleware import authorize
from models.config import Action
from models.responses import ModelsResponse
from utils.endpoints import check_configuration_loaded
Expand All @@ -20,9 +20,6 @@
router = APIRouter(tags=["models"])


auth_dependency = get_auth_dependency()


models_responses: dict[int | str, dict[str, Any]] = {
200: {
"models": [
Expand Down Expand Up @@ -54,7 +51,7 @@
@authorize(Action.GET_MODELS)
async def models_endpoint_handler(
request: Request,
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
) -> ModelsResponse:
"""
Handle requests to the /models endpoint.
Expand Down
3 changes: 1 addition & 2 deletions src/app/endpoints/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@

logger = logging.getLogger("app.endpoints.handlers")
router = APIRouter(tags=["query"])
auth_dependency = get_auth_dependency()

query_response: dict[int | str, dict[str, Any]] = {
200: {
Expand Down Expand Up @@ -174,7 +173,7 @@ def evaluate_model_hints(
async def query_endpoint_handler(
request: Request,
query_request: QueryRequest,
auth: Annotated[AuthTuple, Depends(auth_dependency)],
auth: Annotated[AuthTuple, Depends(get_auth_dependency())],
mcp_headers: dict[str, dict[str, str]] = Depends(mcp_headers_dependency),
) -> QueryResponse:
"""
Expand Down
Loading
Loading