|
30 | 30 | HumanClarification, |
31 | 31 | HumanFeedback, |
32 | 32 | InputTask, |
| 33 | + Plan, |
| 34 | + PlanStatus, |
33 | 35 | PlanWithSteps, |
34 | 36 | Step, |
35 | 37 | UserLanguage, |
@@ -292,6 +294,132 @@ async def input_task_endpoint(input_task: InputTask, request: Request): |
292 | 294 | ) from e |
293 | 295 |
|
294 | 296 |
|
| 297 | +@app.post("/api/create_plan") |
| 298 | +async def create_plan_endpoint(input_task: InputTask, request: Request): |
| 299 | + """ |
| 300 | + Create a new plan without full processing. |
| 301 | + |
| 302 | + --- |
| 303 | + tags: |
| 304 | + - Plans |
| 305 | + parameters: |
| 306 | + - name: user_principal_id |
| 307 | + in: header |
| 308 | + type: string |
| 309 | + required: true |
| 310 | + description: User ID extracted from the authentication header |
| 311 | + - name: body |
| 312 | + in: body |
| 313 | + required: true |
| 314 | + schema: |
| 315 | + type: object |
| 316 | + properties: |
| 317 | + session_id: |
| 318 | + type: string |
| 319 | + description: Session ID for the plan |
| 320 | + description: |
| 321 | + type: string |
| 322 | + description: The task description to validate and create plan for |
| 323 | + responses: |
| 324 | + 200: |
| 325 | + description: Plan created successfully |
| 326 | + schema: |
| 327 | + type: object |
| 328 | + properties: |
| 329 | + plan_id: |
| 330 | + type: string |
| 331 | + description: The ID of the newly created plan |
| 332 | + status: |
| 333 | + type: string |
| 334 | + description: Success message |
| 335 | + session_id: |
| 336 | + type: string |
| 337 | + description: Session ID associated with the plan |
| 338 | + 400: |
| 339 | + description: RAI check failed or invalid input |
| 340 | + schema: |
| 341 | + type: object |
| 342 | + properties: |
| 343 | + detail: |
| 344 | + type: string |
| 345 | + description: Error message |
| 346 | + """ |
| 347 | + # Perform RAI check on the description |
| 348 | + if not await rai_success(input_task.description): |
| 349 | + track_event_if_configured( |
| 350 | + "RAI failed", |
| 351 | + { |
| 352 | + "status": "Plan not created - RAI check failed", |
| 353 | + "description": input_task.description, |
| 354 | + "session_id": input_task.session_id, |
| 355 | + }, |
| 356 | + ) |
| 357 | + raise HTTPException( |
| 358 | + status_code=400, |
| 359 | + detail="Task description failed safety validation. Please revise your request." |
| 360 | + ) |
| 361 | + |
| 362 | + # Get authenticated user |
| 363 | + authenticated_user = get_authenticated_user_details(request_headers=request.headers) |
| 364 | + user_id = authenticated_user["user_principal_id"] |
| 365 | + |
| 366 | + if not user_id: |
| 367 | + track_event_if_configured( |
| 368 | + "UserIdNotFound", {"status_code": 400, "detail": "no user"} |
| 369 | + ) |
| 370 | + raise HTTPException(status_code=400, detail="no user") |
| 371 | + |
| 372 | + # Generate session ID if not provided |
| 373 | + if not input_task.session_id: |
| 374 | + input_task.session_id = str(uuid.uuid4()) |
| 375 | + |
| 376 | + try: |
| 377 | + # Initialize memory store |
| 378 | + kernel, memory_store = await initialize_runtime_and_context( |
| 379 | + input_task.session_id, user_id |
| 380 | + ) |
| 381 | + |
| 382 | + # Create a new Plan object |
| 383 | + plan = Plan( |
| 384 | + session_id=input_task.session_id, |
| 385 | + user_id=user_id, |
| 386 | + initial_goal=input_task.description, |
| 387 | + overall_status=PlanStatus.in_progress, |
| 388 | + source=AgentType.PLANNER.value |
| 389 | + ) |
| 390 | + |
| 391 | + # Save the plan to the database |
| 392 | + await memory_store.add_plan(plan) |
| 393 | + |
| 394 | + # Log successful plan creation |
| 395 | + track_event_if_configured( |
| 396 | + "PlanCreated", |
| 397 | + { |
| 398 | + "status": f"Plan created with ID: {plan.id}", |
| 399 | + "session_id": input_task.session_id, |
| 400 | + "plan_id": plan.id, |
| 401 | + "description": input_task.description, |
| 402 | + }, |
| 403 | + ) |
| 404 | + |
| 405 | + return { |
| 406 | + "plan_id": plan.id, |
| 407 | + "status": "Plan created successfully", |
| 408 | + "session_id": input_task.session_id, |
| 409 | + } |
| 410 | + |
| 411 | + except Exception as e: |
| 412 | + track_event_if_configured( |
| 413 | + "CreatePlanError", |
| 414 | + { |
| 415 | + "session_id": input_task.session_id, |
| 416 | + "description": input_task.description, |
| 417 | + "error": str(e), |
| 418 | + }, |
| 419 | + ) |
| 420 | + raise HTTPException(status_code=400, detail=f"Error creating plan: {e}") |
| 421 | + |
| 422 | + |
295 | 423 | @app.post("/api/human_feedback") |
296 | 424 | async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Request): |
297 | 425 | """ |
|
0 commit comments