techresearchspace commited on
Commit
964ad3d
·
verified ·
1 Parent(s): e3deb9e

Update agents.py

Browse files
Files changed (1) hide show
  1. agents.py +23 -0
agents.py CHANGED
@@ -398,6 +398,28 @@ def build_graph():
398
  return graph.compile()
399
 
400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401
  def run_task(app, task: str, session_state: dict) -> str:
402
  """Run one task through the graph. session_state is the gr.State dict
403
  used to persist conversation history across turns in the UI."""
@@ -413,6 +435,7 @@ def run_task(app, task: str, session_state: dict) -> str:
413
  # round-trips and is what actually prevents runaway loops.
414
  final_state = app.invoke(initial_state, config={"recursion_limit": 2 * MAX_TURNS + 5})
415
  answer = final_state["messages"][-1].content
 
416
 
417
  session_state.setdefault("history", []).append({"task": task, "answer": answer})
418
  return answer
 
398
  return graph.compile()
399
 
400
 
401
+ AGENT_LABELS = {
402
+ "search_agent": "🔍 Search agent",
403
+ "code_agent": "💻 Code agent",
404
+ "data_agent": "📊 Data agent",
405
+ }
406
+
407
+
408
+ def _format_attribution(results: dict) -> str:
409
+ """Build a deterministic 'which agent did what' footer from state.results.
410
+ Built in code rather than asked of the model, so attribution can't drift
411
+ from what actually ran — dict insertion order matches invocation order
412
+ since specialists write into results as they're called."""
413
+ if not results:
414
+ return ""
415
+ lines = ["", "---", "**Agents used:**"]
416
+ for name, summary in results.items():
417
+ label = AGENT_LABELS.get(name, name)
418
+ preview = summary if len(summary) <= 160 else summary[:157] + "..."
419
+ lines.append(f"- **{label}** — {preview}")
420
+ return "\n".join(lines)
421
+
422
+
423
  def run_task(app, task: str, session_state: dict) -> str:
424
  """Run one task through the graph. session_state is the gr.State dict
425
  used to persist conversation history across turns in the UI."""
 
435
  # round-trips and is what actually prevents runaway loops.
436
  final_state = app.invoke(initial_state, config={"recursion_limit": 2 * MAX_TURNS + 5})
437
  answer = final_state["messages"][-1].content
438
+ answer += _format_attribution(final_state.get("results", {}))
439
 
440
  session_state.setdefault("history", []).append({"task": task, "answer": answer})
441
  return answer