33from uuid import uuid4
44
55from langchain .tools import BaseTool
6- from langchain_core .messages import AIMessage
76from langsmith import Client
8-
7+ from langchain_core .messages import AIMessage
8+ from codegen .agents .loggers import ExternalLogger
9+ from codegen .agents .tracer import MessageStreamTracer
910from codegen .extensions .langchain .agent import create_codebase_agent
1011from codegen .extensions .langchain .utils .get_langsmith_url import find_and_print_langsmith_run_url
1112
@@ -42,7 +43,7 @@ def __init__(self, codebase: "Codebase", model_provider: str = "anthropic", mode
4243 self .project_name = os .environ .get ("LANGCHAIN_PROJECT" , "RELACE" )
4344 print (f"Using LangSmith project: { self .project_name } " )
4445
45- def run (self , prompt : str , thread_id : Optional [str ] = None ) -> str :
46+ def run (self , prompt : str , thread_id : Optional [str ] = None , logger : Optional [ ExternalLogger ] = None ) -> str :
4647 """Run the agent with a prompt.
4748
4849 Args:
@@ -55,20 +56,32 @@ def run(self, prompt: str, thread_id: Optional[str] = None) -> str:
5556 if thread_id is None :
5657 thread_id = str (uuid4 ())
5758
59+
60+
5861 # this message has a reducer which appends the current message to the existing history
5962 # see more https://langchain-ai.github.io/langgraph/concepts/low_level/#reducers
6063 input = {"messages" : [("user" , prompt )]}
6164
6265 # we stream the steps instead of invoke because it allows us to access intermediate nodes
63- stream = self .agent .stream (input , config = {"configurable" : {"thread_id" : thread_id , "metadata" : {"project" : self .project_name }}, "recursion_limit" : 100 }, stream_mode = "values" )
66+ stream = self .agent .stream (input , config = {"configurable" : {"thread_id" : thread_id , "metadata" : {"project" : self .project_name }}, "recursion_limit" : 100 }, stream_mode = "values" )
67+ # Get the stream from the graph
68+ # Create the external logger (optional)
69+ # Create the tracer
70+ _tracer = MessageStreamTracer (logger = logger )
71+
72+ # Process the stream with the tracer
73+ traced_stream = _tracer .process_stream (stream )
6474
6575 # Keep track of run IDs from the stream
6676 run_ids = []
6777
68- for s in stream :
78+ for s in traced_stream :
6979 message = s ["messages" ][- 1 ]
80+ message .pretty_print ()
81+
7082 if isinstance (message , tuple ):
71- print (message )
83+ # print(message)
84+ pass
7285 else :
7386 if isinstance (message , AIMessage ) and isinstance (message .content , list ) and "text" in message .content [0 ]:
7487 AIMessage (message .content [0 ]["text" ]).pretty_print ()
@@ -82,7 +95,7 @@ def run(self, prompt: str, thread_id: Optional[str] = None) -> str:
8295 # Get the last message content
8396 result = s ["messages" ][- 1 ].content
8497
85- # Try to find run IDs in the LangSmith client's recent runs
98+ # # Try to find run IDs in the LangSmith client's recent runs
8699 try :
87100 # Find and print the LangSmith run URL
88101 find_and_print_langsmith_run_url (self .langsmith_client , self .project_name )
0 commit comments