2025-09-02 01:08:42 +08:00
|
|
|
from datetime import UTC, datetime
|
2025-04-02 13:00:25 +08:00
|
|
|
|
2025-10-23 01:39:01 +08:00
|
|
|
from langchain.messages import AIMessageChunk, ToolMessage
|
2025-09-01 22:37:03 +08:00
|
|
|
from langchain_core.runnables import RunnableConfig
|
2025-10-08 21:54:10 +08:00
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
from src.agents.common.base import BaseAgent
|
2025-07-02 02:38:36 +08:00
|
|
|
|
2025-03-29 17:33:09 +08:00
|
|
|
|
2025-07-02 02:38:36 +08:00
|
|
|
async def agent_cli(agent: BaseAgent, config: RunnableConfig | None = None):
|
2025-03-25 05:40:07 +08:00
|
|
|
config = config or {}
|
|
|
|
|
if "configurable" not in config:
|
|
|
|
|
config["configurable"] = {}
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
user_input = input("\nUser: ")
|
|
|
|
|
if user_input.lower() in ["quit", "exit", "q"]:
|
|
|
|
|
print("Goodbye!")
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
stream_flag = False
|
2025-05-16 23:46:25 +08:00
|
|
|
async for msg, metadata in agent.stream_messages([{"role": "user", "content": user_input}], config):
|
2025-03-25 05:40:07 +08:00
|
|
|
if isinstance(msg, AIMessageChunk):
|
|
|
|
|
content = msg.content or msg.tool_calls
|
|
|
|
|
|
|
|
|
|
if not content:
|
2025-05-23 15:30:14 +08:00
|
|
|
if stream_flag:
|
2025-03-25 05:40:07 +08:00
|
|
|
print()
|
|
|
|
|
stream_flag = False
|
|
|
|
|
continue
|
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
if not stream_flag and content:
|
2025-03-25 05:40:07 +08:00
|
|
|
print(f"AI: {content}", end="", flush=True)
|
|
|
|
|
stream_flag = True
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
elif content:
|
|
|
|
|
print(f"{content}", end="", flush=True)
|
|
|
|
|
|
|
|
|
|
if isinstance(msg, ToolMessage):
|
|
|
|
|
print(f"Tool: {msg.content}")
|
2025-04-02 13:00:25 +08:00
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
2025-04-02 13:00:25 +08:00
|
|
|
def get_cur_time_with_utc():
|
2025-05-24 11:29:45 +08:00
|
|
|
return datetime.now(tz=UTC).isoformat()
|