8
8
from fastapi .exceptions import HTTPException , RequestValidationError
9
9
from fastapi .params import Depends
10
10
from fastapi .security import HTTPBasic
11
+ from pydantic import UUID4
11
12
from pydantic .main import BaseModel
12
13
from starlette .background import BackgroundTasks
13
14
from starlette .responses import JSONResponse
32
33
from intent_deployer .config import get_settings
33
34
from intent_deployer .deploy import deploy_policy
34
35
from intent_deployer .dialogflow import DialogflowContext
36
+ from intent_deployer .state_track import ActiveIntent , list_intents , lookup_intent , new_status_tracker
35
37
from intent_deployer .switches import Switches
36
38
from intent_deployer .types import (
37
39
APIResponse ,
38
40
APIResponseStatus ,
41
+ CreatedIntentResponse ,
42
+ GetIntentResponse ,
39
43
IntentDeployException ,
44
+ ListIntentResponse ,
40
45
NileIntentRequest ,
41
46
)
42
47
from intents .utils import PushIntentPolicy
@@ -207,20 +212,25 @@ async def dialogflow_webhook(
207
212
@app .post (
208
213
f"/{ intent .__name__ } " ,
209
214
name = f"invoke_{ intent .__name__ } " ,
210
- response_model = APIResponse ,
215
+ response_model = CreatedIntentResponse ,
211
216
responses = {400 : {"model" : APIResponse }, 422 : {"model" : APIResponse }},
212
217
)
213
218
async def invoke_intent (body : intent , background_tasks : BackgroundTasks ):
219
+ ctx = None
214
220
try :
215
- ctx = Context (background_tasks )
221
+ tracked_intent = new_status_tracker (intent .__name__ , body )
222
+ ctx = Context (background_tasks , tracked_intent )
216
223
await handle_intent (ctx , body )
217
224
218
- return APIResponse (status = APIResponseStatus (code = 200 , details = "success" ))
225
+ return CreatedIntentResponse (status = APIResponseStatus (code = 200 , details = "success" ),
226
+ intent_id = tracked_intent .id )
219
227
except IntentDeployException as e :
220
228
log .exception (e )
229
+ if ctx : ctx .push_status ("failure" , str (e ))
221
230
return format_json_error (400 , f"Could not deploy intent. \n { e } " )
222
231
except Exception as e :
223
232
log .exception (e )
233
+ if ctx : ctx .push_status ("failure" , str (e ))
224
234
return format_json_error (400 , f"Could not deploy intent. internal error" )
225
235
226
236
@@ -234,3 +244,19 @@ async def alertmanager_webhook(
234
244
await handle_alert (body )
235
245
236
246
return APIResponse (status = APIResponseStatus (code = 200 , details = "success" ))
247
+
248
+ @app .get ("/intents" , response_model = ListIntentResponse )
249
+ async def list_intents_endpoint ():
250
+ return ListIntentResponse (status = APIResponseStatus (code = 200 , details = "success" ),
251
+ intents = list_intents ())
252
+
253
+ @app .get ("/intents/{intent_id}" , response_model = GetIntentResponse ,
254
+ responses = {404 : {"model" : APIResponse }})
255
+ async def get_intent_endpoint (intent_id : UUID4 ):
256
+ intent_status = lookup_intent (intent_id )
257
+
258
+ if intent_status :
259
+ return GetIntentResponse (status = APIResponseStatus (code = 200 , details = "success" ),
260
+ intent = intent_status )
261
+
262
+ return format_json_error (404 , f"Could not find an intent with id: { intent_id } " )
0 commit comments