ForcePilot/src/agents/common/utils.py

45 lines
1.4 KiB
Python
Raw Normal View History

from datetime import UTC, datetime
2025-04-02 13:00:25 +08:00
from langchain.messages import AIMessageChunk, ToolMessage
from langchain_core.runnables import RunnableConfig
2025-10-08 21:54:10 +08:00
from src.agents.common.base import BaseAgent
2025-03-29 17:33:09 +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:
if stream_flag:
2025-03-25 05:40:07 +08:00
print()
stream_flag = False
continue
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-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()