|
11 | 11 | from app.core import logging, settings |
12 | 12 | from app.models import UserOrganization |
13 | 13 | from app.utils import APIResponse |
14 | | -from app.crud import get_key_by_org |
15 | | -from app.core.security import decrypt_api_key |
| 14 | +from app.crud.credentials import get_provider_credential |
| 15 | +from app.core.security import decrypt_credentials |
16 | 16 |
|
17 | 17 | logger = logging.getLogger(__name__) |
18 | 18 | router = APIRouter(tags=["threads"]) |
@@ -169,15 +169,17 @@ async def threads( |
169 | 169 | _current_user: UserOrganization = Depends(get_current_user_org), |
170 | 170 | ): |
171 | 171 | """Asynchronous endpoint that processes requests in background.""" |
172 | | - encrypted_key = get_key_by_org( |
173 | | - session=_session, org_id=_current_user.organization_id |
| 172 | + credentials = get_provider_credential( |
| 173 | + session=_session, |
| 174 | + org_id=_current_user.organization_id, |
| 175 | + provider="openai", |
| 176 | + project_id=request.get("project_id"), |
174 | 177 | ) |
175 | | - api_key = decrypt_api_key(encrypted_key) |
176 | | - if not api_key: |
| 178 | + if not credentials or "api_key" not in credentials: |
177 | 179 | return APIResponse.failure_response( |
178 | | - error="API key not configured for this organization." |
| 180 | + error="OpenAI API key not configured for this organization." |
179 | 181 | ) |
180 | | - client = OpenAI(api_key=api_key) |
| 182 | + client = OpenAI(api_key=credentials["api_key"]) |
181 | 183 |
|
182 | 184 | langfuse_context.configure( |
183 | 185 | secret_key=settings.LANGFUSE_SECRET_KEY, |
@@ -218,16 +220,18 @@ async def threads_sync( |
218 | 220 | ): |
219 | 221 | """Synchronous endpoint that processes requests immediately.""" |
220 | 222 |
|
221 | | - encrypted_key = get_key_by_org( |
222 | | - session=_session, org_id=_current_user.organization_id |
| 223 | + credentials = get_provider_credential( |
| 224 | + session=_session, |
| 225 | + org_id=_current_user.organization_id, |
| 226 | + provider="openai", |
| 227 | + project_id=_current_user.project_id, |
223 | 228 | ) |
224 | | - api_key = decrypt_api_key(encrypted_key) |
225 | | - if not api_key: |
| 229 | + if not credentials or "api_key" not in credentials: |
226 | 230 | return APIResponse.failure_response( |
227 | | - error="API key not configured for this organization." |
| 231 | + error="OpenAI API key not configured for this organization." |
228 | 232 | ) |
229 | 233 |
|
230 | | - client = OpenAI(api_key=api_key) |
| 234 | + client = OpenAI(api_key=credentials["api_key"]) |
231 | 235 |
|
232 | 236 | # Validate thread |
233 | 237 | is_valid, error_message = validate_thread(client, request.get("thread_id")) |
|
0 commit comments