Spaces:
Sleeping
Sleeping
File size: 1,828 Bytes
c03605c 71ee143 c03605c 71ee143 c03605c 71ee143 c03605c 71ee143 c03605c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import chainlit as cl
from typing import cast
# Ensure configuration is validated at import time so missing environment
# variables cause the process to exit with a clear message before Chainlit starts.
from config import Config
_config = Config() # will exit(1) with a printed message if required vars are missing
from study_chatbot.agent import create_study_agent
from agents import Runner, Agent
@cl.on_chat_start
async def on_start():
"""Create and store the study agent in the user session when a chat starts."""
print("Chat Started")
agent = create_study_agent()
cl.user_session.set("agent", agent)
cl.user_session.set("chat_history", [])
await cl.Message(content="Welcome! How can I help you today?").send()
@cl.on_message
async def main(message: cl.Message) -> None:
"""Process incoming messages with the triage agent instead of echoing."""
# Extract text from message safely
text = message.content
print("Received message:", text)
if not text:
await cl.Message(content="Please send a short text question or request.").send()
return
# send a temporary thinking message
thinking = cl.Message(content="Thinking...")
await thinking.send()
agent = cast(Agent, cl.user_session.get("agent"))
history = cl.user_session.get("chat_history") or []
history.append({"role": "user", "content": text})
try:
print("Running agent with history:", history)
result = await Runner.run(agent, history)
print("Agent result:", result)
response = result.final_output
thinking.content = str(response)
await thinking.send()
cl.user_session.set("chat_history", result.to_input_list())
except Exception as e:
thinking.content = f"Error: {e}"
await thinking.update() |