|
3 | 3 | import logging |
4 | 4 |
|
5 | 5 | from rest_framework import serializers |
| 6 | +from rest_framework.exceptions import PermissionDenied |
6 | 7 | from rest_framework.request import Request |
7 | 8 | from rest_framework.response import Response |
8 | 9 |
|
|
12 | 13 | from sentry.api.bases.organization import OrganizationEndpoint, OrganizationPermission |
13 | 14 | from sentry.models.organization import Organization |
14 | 15 | from sentry.ratelimits.config import RateLimitConfig |
15 | | -from sentry.seer.explorer.client import continue_seer_run, get_seer_run, start_seer_run |
| 16 | +from sentry.seer.explorer.client import SeerExplorerClient |
| 17 | +from sentry.seer.models import SeerPermissionError |
16 | 18 | from sentry.types.ratelimit import RateLimit, RateLimitCategory |
17 | 19 |
|
18 | 20 | logger = logging.getLogger(__name__) |
@@ -77,8 +79,11 @@ def get( |
77 | 79 | return Response({"session": None}, status=404) |
78 | 80 |
|
79 | 81 | try: |
80 | | - state = get_seer_run(run_id=int(run_id), organization=organization, user=request.user) |
| 82 | + client = SeerExplorerClient(organization, request.user) |
| 83 | + state = client.get_run(run_id=int(run_id)) |
81 | 84 | return Response({"session": state.dict()}) |
| 85 | + except SeerPermissionError as e: |
| 86 | + raise PermissionDenied(e.message) from e |
82 | 87 | except ValueError: |
83 | 88 | return Response({"session": None}, status=404) |
84 | 89 |
|
@@ -106,23 +111,22 @@ def post( |
106 | 111 | insert_index = validated_data.get("insert_index") |
107 | 112 | on_page_context = validated_data.get("on_page_context") |
108 | 113 |
|
109 | | - # Use client to start or continue run |
110 | | - if run_id: |
111 | | - # Continue existing conversation |
112 | | - result_run_id = continue_seer_run( |
113 | | - run_id=int(run_id), |
114 | | - organization=organization, |
115 | | - prompt=query, |
116 | | - user=request.user, |
117 | | - insert_index=insert_index, |
118 | | - on_page_context=on_page_context, |
119 | | - ) |
120 | | - else: |
121 | | - # Start new conversation |
122 | | - result_run_id = start_seer_run( |
123 | | - organization=organization, |
124 | | - prompt=query, |
125 | | - user=request.user, |
126 | | - on_page_context=on_page_context, |
127 | | - ) |
128 | | - return Response({"run_id": result_run_id}) |
| 114 | + try: |
| 115 | + client = SeerExplorerClient(organization, request.user) |
| 116 | + if run_id: |
| 117 | + # Continue existing conversation |
| 118 | + result_run_id = client.continue_run( |
| 119 | + run_id=int(run_id), |
| 120 | + prompt=query, |
| 121 | + insert_index=insert_index, |
| 122 | + on_page_context=on_page_context, |
| 123 | + ) |
| 124 | + else: |
| 125 | + # Start new conversation |
| 126 | + result_run_id = client.start_run( |
| 127 | + prompt=query, |
| 128 | + on_page_context=on_page_context, |
| 129 | + ) |
| 130 | + return Response({"run_id": result_run_id}) |
| 131 | + except SeerPermissionError as e: |
| 132 | + raise PermissionDenied(e.message) from e |
0 commit comments