Skip to content
Closed
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
20 changes: 18 additions & 2 deletions backend/app/api/routes/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from app.core import logging, settings
from app.models import UserOrganization
from app.utils import APIResponse
from app.crud import get_key_by_org

logger = logging.getLogger(__name__)
router = APIRouter(tags=["threads"])
Expand Down Expand Up @@ -167,7 +168,15 @@
_current_user: UserOrganization = Depends(get_current_user_org),
):
"""Asynchronous endpoint that processes requests in background."""
client = OpenAI(api_key=settings.OPENAI_API_KEY)
api_key = get_key_by_org(session=_session, org_id=_current_user.organization_id)

Check warning on line 171 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L171

Added line #L171 was not covered by tests

if not api_key:
return APIResponse.failure_response(

Check warning on line 174 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L173-L174

Added lines #L173 - L174 were not covered by tests
error="API key not configured for this organization."
)

client = OpenAI(api_key=api_key)

Check warning on line 178 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L178

Added line #L178 was not covered by tests

langfuse_context.configure(
secret_key=settings.LANGFUSE_SECRET_KEY,
public_key=settings.LANGFUSE_PUBLIC_KEY,
Expand Down Expand Up @@ -206,7 +215,14 @@
_current_user: UserOrganization = Depends(get_current_user_org),
):
"""Synchronous endpoint that processes requests immediately."""
client = OpenAI(api_key=settings.OPENAI_API_KEY)
api_key = get_key_by_org(session=_session, org_id=_current_user.organization_id)

Check warning on line 218 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L218

Added line #L218 was not covered by tests

if not api_key:
return APIResponse.failure_response(

Check warning on line 221 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L220-L221

Added lines #L220 - L221 were not covered by tests
error="API key not configured for this organization."
)

client = OpenAI(api_key=api_key)

Check warning on line 225 in backend/app/api/routes/threads.py

View check run for this annotation

Codecov / codecov/patch

backend/app/api/routes/threads.py#L225

Added line #L225 was not covered by tests

# Validate thread
is_valid, error_message = validate_thread(client, request.get("thread_id"))
Expand Down
8 changes: 8 additions & 0 deletions backend/app/crud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@
get_api_keys_by_organization,
delete_api_key,
)

from .credentials import (
set_creds_for_org,
get_creds_by_org,
get_key_by_org,
update_creds_for_org,
remove_creds_for_org,
)