redhairedshanks1 commited on
Commit
3804e08
Β·
1 Parent(s): e6e087d

Update api_routes_v2.py

Browse files
Files changed (1) hide show
  1. api_routes_v2.py +80 -13
api_routes_v2.py CHANGED
@@ -875,7 +875,10 @@ async def get_session_history(
875
 
876
  # Extract tools/components
877
  components = pipeline_def.get("components") or pipeline_def.get("pipeline_steps", [])
878
- tools_list = [comp.get("tool_name", "unknown") for comp in components]
 
 
 
879
 
880
  enhanced_pipe["tools"] = tools_list
881
  enhanced_pipe["component_count"] = len(components)
@@ -1125,7 +1128,7 @@ async def chat_unified(
1125
  # Extract user-facing output
1126
  result_text = _extract_user_facing_text(result)
1127
  friendly = f"πŸŽ‰ Pipeline completed successfully!"
1128
- output = {"component_summary": "Pipeline executed successfully", "steps": len(proposed.get("pipeline_steps", []))}
1129
  final_output = {"text": result_text, "result": result}
1130
 
1131
  api_data = {
@@ -1204,13 +1207,27 @@ async def chat_unified(
1204
  prefer_bedrock=bool(prefer_bedrock),
1205
  )
1206
 
 
 
 
 
 
 
1207
  # V3: Create pipeline record in S3 and MongoDB
1208
  pipeline_id = _create_pipeline_record(chat_id, pipeline, status="proposed", created_from="request")
 
 
1209
  session_manager.update_session(chat_id, {"proposed_pipeline": pipeline, "state": "pipeline_proposed"})
1210
 
 
 
1211
  pipeline_name = pipeline.get("pipeline_name", "Document Processing")
1212
- steps_list = pipeline.get("pipeline_steps", [])
1213
- steps_summary = "\n".join([f" {i+1}. {step.get('tool', 'Unknown')}" for i, step in enumerate(steps_list)])
 
 
 
 
1214
 
1215
  friendly = (
1216
  f"🎯 **Pipeline Created: {pipeline_name}**\n"
@@ -1245,7 +1262,10 @@ async def chat_unified(
1245
  "pipeline_id": pipeline_id,
1246
  "pipeline_name": pipeline_name,
1247
  "steps_count": len(steps_list),
1248
- "tools": [step.get("tool", "unknown") for step in steps_list]
 
 
 
1249
  }
1250
 
1251
  return _assistant_response_payload(
@@ -1288,6 +1308,13 @@ async def chat_unified(
1288
  file_path=session.get("current_file"),
1289
  prefer_bedrock=bool(prefer_bedrock)
1290
  )
 
 
 
 
 
 
 
1291
  # V3: Create pipeline record for edited pipeline
1292
  pipeline_id = _create_pipeline_record(chat_id, new_pipeline, status="proposed", created_from="edit")
1293
  session_manager.update_session(chat_id, {"proposed_pipeline": new_pipeline, "state": "pipeline_proposed"})
@@ -1301,12 +1328,18 @@ async def chat_unified(
1301
  "pipeline": new_pipeline
1302
  }
1303
 
 
 
 
1304
  # Create output summary for modified pipeline
1305
  output = {
1306
  "pipeline_id": pipeline_id,
1307
  "pipeline_name": new_pipeline.get("pipeline_name", "Document Processing"),
1308
- "steps_count": len(new_pipeline.get("pipeline_steps", [])),
1309
- "tools": [step.get("tool", "unknown") for step in new_pipeline.get("pipeline_steps", [])],
 
 
 
1310
  "modification": "edited"
1311
  }
1312
 
@@ -1500,15 +1533,27 @@ async def chat_unified_stream(
1500
  prefer_bedrock=bool(prefer_bedrock),
1501
  )
1502
 
 
 
 
 
 
 
1503
  # V3 LIFECYCLE: Create pipeline record in S3 + MongoDB immediately
1504
  pipeline_id = _create_pipeline_record(chat_id, pipeline, status="proposed", created_from="request")
1505
 
1506
  # Update session with proposed pipeline
1507
  session_manager.update_session(chat_id, {"proposed_pipeline": pipeline, "state": "pipeline_proposed"})
1508
 
 
 
1509
  pipeline_name = pipeline.get("pipeline_name", "Document Processing")
1510
- steps_list = pipeline.get("pipeline_steps", [])
1511
- steps_summary = "\n".join([f" {i+1}. {step.get('tool', 'Unknown')}" for i, step in enumerate(steps_list)])
 
 
 
 
1512
 
1513
  friendly = (
1514
  f"🎯 **Pipeline Created: {pipeline_name}**\n"
@@ -1527,14 +1572,22 @@ async def chat_unified_stream(
1527
  "pipeline_id": pipeline_id,
1528
  "pipeline_name": pipeline_name,
1529
  "steps_count": len(steps_list),
1530
- "tools": [step.get("tool", "unknown") for step in steps_list]
 
 
 
1531
  }
1532
  })
1533
  return
1534
  except Exception as e:
1535
  friendly = f"❌ Error generating pipeline: {str(e)}"
1536
  _add_and_mirror_message(chat_id, "assistant", friendly)
1537
- yield emit({"type": "assistant_final", "content": friendly, "error": str(e)})
 
 
 
 
 
1538
  return
1539
 
1540
  # Pipeline proposed
@@ -1706,12 +1759,23 @@ async def chat_unified_stream(
1706
  file_path=session_local.get("current_file"),
1707
  prefer_bedrock=bool(prefer_bedrock)
1708
  )
 
 
 
 
 
 
 
1709
  # V3: Create pipeline record for edited pipeline
1710
  pipeline_id = _create_pipeline_record(chat_id, new_pipeline, status="proposed", created_from="edit")
1711
  session_manager.update_session(chat_id, {"proposed_pipeline": new_pipeline, "state": "pipeline_proposed"})
1712
  formatted = format_pipeline_for_display(new_pipeline)
1713
  friendly = formatted + f"\n\n```json\n{json.dumps(new_pipeline, indent=2)}\n```"
1714
  _add_and_mirror_message(chat_id, "assistant", friendly)
 
 
 
 
1715
  yield emit({
1716
  "type": "assistant_final",
1717
  "content": friendly,
@@ -1719,8 +1783,11 @@ async def chat_unified_stream(
1719
  "output": {
1720
  "pipeline_id": pipeline_id,
1721
  "pipeline_name": new_pipeline.get("pipeline_name", "Document Processing"),
1722
- "steps_count": len(new_pipeline.get("pipeline_steps", [])),
1723
- "tools": [step.get("tool", "unknown") for step in new_pipeline.get("pipeline_steps", [])],
 
 
 
1724
  "modification": "edited"
1725
  }
1726
  })
 
875
 
876
  # Extract tools/components
877
  components = pipeline_def.get("components") or pipeline_def.get("pipeline_steps", [])
878
+ tools_list = [
879
+ comp.get("tool_name", comp.get("tool", "unknown"))
880
+ for comp in components
881
+ ]
882
 
883
  enhanced_pipe["tools"] = tools_list
884
  enhanced_pipe["component_count"] = len(components)
 
1128
  # Extract user-facing output
1129
  result_text = _extract_user_facing_text(result)
1130
  friendly = f"πŸŽ‰ Pipeline completed successfully!"
1131
+ output = {"component_summary": "Pipeline executed successfully", "steps": len(proposed.get("pipeline_steps", proposed.get("components", [])))}
1132
  final_output = {"text": result_text, "result": result}
1133
 
1134
  api_data = {
 
1207
  prefer_bedrock=bool(prefer_bedrock),
1208
  )
1209
 
1210
+ # FIX: Ensure pipeline has both 'components' and 'pipeline_steps' for compatibility
1211
+ if "components" in pipeline and "pipeline_steps" not in pipeline:
1212
+ pipeline["pipeline_steps"] = pipeline["components"]
1213
+ elif "pipeline_steps" in pipeline and "components" not in pipeline:
1214
+ pipeline["components"] = pipeline["pipeline_steps"]
1215
+
1216
  # V3: Create pipeline record in S3 and MongoDB
1217
  pipeline_id = _create_pipeline_record(chat_id, pipeline, status="proposed", created_from="request")
1218
+
1219
+ # Update session with pipeline that includes pipeline_id
1220
  session_manager.update_session(chat_id, {"proposed_pipeline": pipeline, "state": "pipeline_proposed"})
1221
 
1222
+ # Get steps list from either field
1223
+ steps_list = pipeline.get("pipeline_steps", pipeline.get("components", []))
1224
  pipeline_name = pipeline.get("pipeline_name", "Document Processing")
1225
+
1226
+ # Create steps summary with proper tool name extraction
1227
+ steps_summary = "\n".join([
1228
+ f" {i+1}. {step.get('tool_name', step.get('tool', 'Unknown'))}"
1229
+ for i, step in enumerate(steps_list)
1230
+ ])
1231
 
1232
  friendly = (
1233
  f"🎯 **Pipeline Created: {pipeline_name}**\n"
 
1262
  "pipeline_id": pipeline_id,
1263
  "pipeline_name": pipeline_name,
1264
  "steps_count": len(steps_list),
1265
+ "tools": [
1266
+ step.get("tool_name", step.get("tool", "unknown"))
1267
+ for step in steps_list
1268
+ ]
1269
  }
1270
 
1271
  return _assistant_response_payload(
 
1308
  file_path=session.get("current_file"),
1309
  prefer_bedrock=bool(prefer_bedrock)
1310
  )
1311
+
1312
+ # FIX: Ensure pipeline has both 'components' and 'pipeline_steps' for compatibility
1313
+ if "components" in new_pipeline and "pipeline_steps" not in new_pipeline:
1314
+ new_pipeline["pipeline_steps"] = new_pipeline["components"]
1315
+ elif "pipeline_steps" in new_pipeline and "components" not in new_pipeline:
1316
+ new_pipeline["components"] = new_pipeline["pipeline_steps"]
1317
+
1318
  # V3: Create pipeline record for edited pipeline
1319
  pipeline_id = _create_pipeline_record(chat_id, new_pipeline, status="proposed", created_from="edit")
1320
  session_manager.update_session(chat_id, {"proposed_pipeline": new_pipeline, "state": "pipeline_proposed"})
 
1328
  "pipeline": new_pipeline
1329
  }
1330
 
1331
+ # Get steps list for output
1332
+ steps_list = new_pipeline.get("pipeline_steps", new_pipeline.get("components", []))
1333
+
1334
  # Create output summary for modified pipeline
1335
  output = {
1336
  "pipeline_id": pipeline_id,
1337
  "pipeline_name": new_pipeline.get("pipeline_name", "Document Processing"),
1338
+ "steps_count": len(steps_list),
1339
+ "tools": [
1340
+ step.get("tool_name", step.get("tool", "unknown"))
1341
+ for step in steps_list
1342
+ ],
1343
  "modification": "edited"
1344
  }
1345
 
 
1533
  prefer_bedrock=bool(prefer_bedrock),
1534
  )
1535
 
1536
+ # FIX: Ensure pipeline has both 'components' and 'pipeline_steps' for compatibility
1537
+ if "components" in pipeline and "pipeline_steps" not in pipeline:
1538
+ pipeline["pipeline_steps"] = pipeline["components"]
1539
+ elif "pipeline_steps" in pipeline and "components" not in pipeline:
1540
+ pipeline["components"] = pipeline["pipeline_steps"]
1541
+
1542
  # V3 LIFECYCLE: Create pipeline record in S3 + MongoDB immediately
1543
  pipeline_id = _create_pipeline_record(chat_id, pipeline, status="proposed", created_from="request")
1544
 
1545
  # Update session with proposed pipeline
1546
  session_manager.update_session(chat_id, {"proposed_pipeline": pipeline, "state": "pipeline_proposed"})
1547
 
1548
+ # Get steps list from either field
1549
+ steps_list = pipeline.get("pipeline_steps", pipeline.get("components", []))
1550
  pipeline_name = pipeline.get("pipeline_name", "Document Processing")
1551
+
1552
+ # Create steps summary with proper tool name extraction
1553
+ steps_summary = "\n".join([
1554
+ f" {i+1}. {step.get('tool_name', step.get('tool', 'Unknown'))}"
1555
+ for i, step in enumerate(steps_list)
1556
+ ])
1557
 
1558
  friendly = (
1559
  f"🎯 **Pipeline Created: {pipeline_name}**\n"
 
1572
  "pipeline_id": pipeline_id,
1573
  "pipeline_name": pipeline_name,
1574
  "steps_count": len(steps_list),
1575
+ "tools": [
1576
+ step.get("tool_name", step.get("tool", "unknown"))
1577
+ for step in steps_list
1578
+ ]
1579
  }
1580
  })
1581
  return
1582
  except Exception as e:
1583
  friendly = f"❌ Error generating pipeline: {str(e)}"
1584
  _add_and_mirror_message(chat_id, "assistant", friendly)
1585
+ yield emit({
1586
+ "type": "assistant_final",
1587
+ "content": friendly,
1588
+ "error": str(e),
1589
+ "exception": str(e)
1590
+ })
1591
  return
1592
 
1593
  # Pipeline proposed
 
1759
  file_path=session_local.get("current_file"),
1760
  prefer_bedrock=bool(prefer_bedrock)
1761
  )
1762
+
1763
+ # FIX: Ensure pipeline has both 'components' and 'pipeline_steps' for compatibility
1764
+ if "components" in new_pipeline and "pipeline_steps" not in new_pipeline:
1765
+ new_pipeline["pipeline_steps"] = new_pipeline["components"]
1766
+ elif "pipeline_steps" in new_pipeline and "components" not in new_pipeline:
1767
+ new_pipeline["components"] = new_pipeline["pipeline_steps"]
1768
+
1769
  # V3: Create pipeline record for edited pipeline
1770
  pipeline_id = _create_pipeline_record(chat_id, new_pipeline, status="proposed", created_from="edit")
1771
  session_manager.update_session(chat_id, {"proposed_pipeline": new_pipeline, "state": "pipeline_proposed"})
1772
  formatted = format_pipeline_for_display(new_pipeline)
1773
  friendly = formatted + f"\n\n```json\n{json.dumps(new_pipeline, indent=2)}\n```"
1774
  _add_and_mirror_message(chat_id, "assistant", friendly)
1775
+
1776
+ # Get steps list for output
1777
+ steps_list = new_pipeline.get("pipeline_steps", new_pipeline.get("components", []))
1778
+
1779
  yield emit({
1780
  "type": "assistant_final",
1781
  "content": friendly,
 
1783
  "output": {
1784
  "pipeline_id": pipeline_id,
1785
  "pipeline_name": new_pipeline.get("pipeline_name", "Document Processing"),
1786
+ "steps_count": len(steps_list),
1787
+ "tools": [
1788
+ step.get("tool_name", step.get("tool", "unknown"))
1789
+ for step in steps_list
1790
+ ],
1791
  "modification": "edited"
1792
  }
1793
  })