BlockBerg commited on
Commit
4ccfa13
Β·
verified Β·
1 Parent(s): 3ad19dc
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -48,19 +48,25 @@ agent = Agent(
48
  markdown=True,
49
  )
50
 
51
- # βœ… Chat function
52
  def ask_agent(messages, history):
53
  print("====== DEBUG: ask_agent START ======")
54
  print("πŸ“₯ Incoming messages:", messages)
55
  print("πŸ“š Chat history:", history)
56
 
 
57
  if isinstance(messages, str):
58
- print("πŸ§ͺ Detected string instead of message list. Wrapping as message.")
59
  messages = [{"role": "user", "content": messages}]
60
-
61
- if not isinstance(messages, list) or not messages:
62
- print("⚠️ Invalid or empty messages list.")
63
- return {"role": "assistant", "content": "Hi! How can I help you with treasury insights today?"}
 
 
 
 
 
 
 
64
 
65
  try:
66
  latest_user_message = messages[-1]["content"]
@@ -72,13 +78,10 @@ def ask_agent(messages, history):
72
  try:
73
  response = agent.run(latest_user_message)
74
  print(f"βœ… Agent response:\n{response}")
75
- return messages + [{"role": "assistant", "content": response.content}] # βœ… RETURN FULL LIST
76
  except Exception as e:
77
  print(f"❌ Agent failed: {e}")
78
- return messages + [{"role": "assistant", "content": "Something went wrong on my end. Try again?"}]
79
-
80
- print("====== DEBUG: ask_agent END ======")
81
-
82
 
83
  def yes(message, history):
84
  return "yes"
 
48
  markdown=True,
49
  )
50
 
 
51
  def ask_agent(messages, history):
52
  print("====== DEBUG: ask_agent START ======")
53
  print("πŸ“₯ Incoming messages:", messages)
54
  print("πŸ“š Chat history:", history)
55
 
56
+ # βœ… Handle Hugging Face's tendency to send just a string
57
  if isinstance(messages, str):
 
58
  messages = [{"role": "user", "content": messages}]
59
+ elif isinstance(messages, list):
60
+ # Defensive fix: if list of strings, wrap each
61
+ if all(isinstance(m, str) for m in messages):
62
+ messages = [{"role": "user", "content": m} for m in messages]
63
+ # If it's a list but not valid messages, throw a descriptive error
64
+ elif not all(isinstance(m, dict) and "role" in m and "content" in m for m in messages):
65
+ print("❌ Malformed message list")
66
+ return {"role": "assistant", "content": "Sorry, I couldn't understand the message format."}
67
+ else:
68
+ print("❌ Completely invalid input type")
69
+ return {"role": "assistant", "content": "Sorry, I didn't understand that input."}
70
 
71
  try:
72
  latest_user_message = messages[-1]["content"]
 
78
  try:
79
  response = agent.run(latest_user_message)
80
  print(f"βœ… Agent response:\n{response}")
81
+ return {"role": "assistant", "content": response.content}
82
  except Exception as e:
83
  print(f"❌ Agent failed: {e}")
84
+ return {"role": "assistant", "content": "Something went wrong on my end. Try again?"}
 
 
 
85
 
86
  def yes(message, history):
87
  return "yes"