Skip to content

Commit a483fdf

Browse files
Extract LangSmith URL generation and run finding logic into utility functions (#701)
Extracted the LangSmith URL generation and run finding logic from the CodeAgent class into utility functions in a new file called get_langsmith_url.py. This improves code organization, reduces duplication, and makes the functionality reusable across the codebase. Link to Devin run: https://app.devin.ai/sessions/6f8a32358770466aaf2838b3896c1078 --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
1 parent 353bf8f commit a483fdf

File tree

3 files changed

+107
-75
lines changed

3 files changed

+107
-75
lines changed

src/codegen/agents/code_agent.py

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from langsmith import Client
88

99
from codegen.extensions.langchain.agent import create_codebase_agent
10+
from codegen.extensions.langchain.utils.get_langsmith_url import find_and_print_langsmith_run_url
1011

1112
if TYPE_CHECKING:
1213
from codegen import Codebase
@@ -83,52 +84,8 @@ def run(self, prompt: str, thread_id: Optional[str] = None) -> str:
8384

8485
# Try to find run IDs in the LangSmith client's recent runs
8586
try:
86-
# Get the most recent runs with proper filter parameters
87-
# We need to provide at least one filter parameter as required by the API
88-
recent_runs = list(
89-
self.langsmith_client.list_runs(
90-
# Use the project name from environment variable
91-
project_name=self.project_name,
92-
# Limit to just the most recent run
93-
limit=1,
94-
)
95-
)
96-
97-
if recent_runs and len(recent_runs) > 0:
98-
# Make sure we have a valid run object with an id attribute
99-
if hasattr(recent_runs[0], "id"):
100-
# Convert the ID to string to ensure it's in the right format
101-
run_id = str(recent_runs[0].id)
102-
103-
# Get the run URL using the run_id parameter
104-
run_url = self.get_langsmith_url(run_id=run_id)
105-
106-
separator = "=" * 60
107-
print(f"\n{separator}\n🔍 LangSmith Run URL: {run_url}\n{separator}")
108-
else:
109-
separator = "=" * 60
110-
print(f"\n{separator}\nRun object has no 'id' attribute: {recent_runs[0]}\n{separator}")
111-
else:
112-
# If no runs found with project name, try a more general approach
113-
# Use a timestamp filter to get recent runs (last 10 minutes)
114-
import datetime
115-
116-
ten_minutes_ago = datetime.datetime.now() - datetime.timedelta(minutes=10)
117-
118-
recent_runs = list(self.langsmith_client.list_runs(start_time=ten_minutes_ago.isoformat(), limit=1))
119-
120-
if recent_runs and len(recent_runs) > 0 and hasattr(recent_runs[0], "id"):
121-
# Convert the ID to string to ensure it's in the right format
122-
run_id = str(recent_runs[0].id)
123-
124-
# Get the run URL using the run_id parameter
125-
run_url = self.get_langsmith_url(run_id=run_id)
126-
127-
separator = "=" * 60
128-
print(f"\n{separator}\n🔍 LangSmith Run URL: {run_url}\n{separator}")
129-
else:
130-
separator = "=" * 60
131-
print(f"\n{separator}\nNo valid runs found\n{separator}")
87+
# Find and print the LangSmith run URL
88+
find_and_print_langsmith_run_url(self.langsmith_client, self.project_name)
13289
except Exception as e:
13390
separator = "=" * 60
13491
print(f"\n{separator}\nCould not retrieve LangSmith URL: {e}")
@@ -138,32 +95,3 @@ def run(self, prompt: str, thread_id: Optional[str] = None) -> str:
13895
print(separator)
13996

14097
return result
141-
142-
def get_langsmith_url(self, run_id: str, project_name: Optional[str] = None) -> str:
143-
"""Get the URL for a run in LangSmith.
144-
145-
Args:
146-
run_id: The ID of the run
147-
project_name: Optional name of the project
148-
149-
Returns:
150-
The URL for the run in LangSmith
151-
"""
152-
# Construct the URL directly using the host URL and run ID
153-
# This avoids the issue with the client's get_run_url method expecting a run object
154-
host_url = self.langsmith_client._host_url
155-
tenant_id = self.langsmith_client._get_tenant_id()
156-
157-
# If project_name is not provided, use the default one
158-
if project_name is None:
159-
project_name = self.project_name
160-
161-
try:
162-
# Get the project ID from the project name
163-
project_id = self.langsmith_client.read_project(project_name=project_name).id
164-
# Construct the URL
165-
return f"{host_url}/o/{tenant_id}/projects/p/{project_id}/r/{run_id}?poll=true"
166-
except Exception as e:
167-
# If we can't get the project ID, construct a URL without it
168-
print(f"Could not get project ID for {project_name}: {e}")
169-
return f"{host_url}/o/{tenant_id}/r/{run_id}?poll=true"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from codegen.extensions.langchain.utils.get_langsmith_url import find_and_print_langsmith_run_url, get_langsmith_url
2+
3+
__all__ = ["find_and_print_langsmith_run_url", "get_langsmith_url"]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import datetime
2+
from typing import Optional
3+
4+
from langsmith import Client
5+
6+
7+
def get_langsmith_url(client: Client, run_id: str, project_name: Optional[str] = None) -> str:
8+
"""Get the URL for a run in LangSmith.
9+
10+
Args:
11+
client: The LangSmith client
12+
run_id: The ID of the run
13+
project_name: Optional name of the project
14+
15+
Returns:
16+
The URL for the run in LangSmith
17+
"""
18+
# Construct the URL directly using the host URL and run ID
19+
# This avoids the issue with the client's get_run_url method expecting a run object
20+
host_url = client._host_url
21+
tenant_id = client._get_tenant_id()
22+
23+
try:
24+
# Get the project ID from the project name
25+
if project_name is not None:
26+
project_id = client.read_project(project_name=project_name).id
27+
# Construct the URL
28+
return f"{host_url}/o/{tenant_id}/projects/p/{project_id}/r/{run_id}?poll=true"
29+
else:
30+
# If project_name is not provided, construct a URL without it
31+
return f"{host_url}/o/{tenant_id}/r/{run_id}?poll=true"
32+
except Exception as e:
33+
# If we can't get the project ID, construct a URL without it
34+
print(f"Could not get project ID for {project_name}: {e}")
35+
return f"{host_url}/o/{tenant_id}/r/{run_id}?poll=true"
36+
37+
38+
def find_and_print_langsmith_run_url(client: Client, project_name: Optional[str] = None) -> Optional[str]:
39+
"""Find the most recent LangSmith run and print its URL.
40+
41+
Args:
42+
client: The LangSmith client
43+
project_name: Optional name of the project
44+
45+
Returns:
46+
The URL for the run in LangSmith if found, None otherwise
47+
"""
48+
separator = "=" * 60
49+
50+
try:
51+
# Get the most recent runs with proper filter parameters
52+
# We need to provide at least one filter parameter as required by the API
53+
recent_runs = list(
54+
client.list_runs(
55+
# Use the project name from environment variable
56+
project_name=project_name,
57+
# Limit to just the most recent run
58+
limit=1,
59+
)
60+
)
61+
62+
if recent_runs and len(recent_runs) > 0:
63+
# Make sure we have a valid run object with an id attribute
64+
if hasattr(recent_runs[0], "id"):
65+
# Convert the ID to string to ensure it's in the right format
66+
run_id = str(recent_runs[0].id)
67+
68+
# Get the run URL using the run_id parameter
69+
run_url = get_langsmith_url(client, run_id=run_id, project_name=project_name)
70+
71+
print(f"\n{separator}\n🔍 LangSmith Run URL: {run_url}\n{separator}")
72+
return run_url
73+
else:
74+
print(f"\n{separator}\nRun object has no 'id' attribute: {recent_runs[0]}\n{separator}")
75+
return None
76+
else:
77+
# If no runs found with project name, try a more general approach
78+
# Use a timestamp filter to get recent runs (last 10 minutes)
79+
ten_minutes_ago = datetime.datetime.now() - datetime.timedelta(minutes=10)
80+
81+
recent_runs = list(client.list_runs(start_time=ten_minutes_ago.isoformat(), limit=1))
82+
83+
if recent_runs and len(recent_runs) > 0 and hasattr(recent_runs[0], "id"):
84+
# Convert the ID to string to ensure it's in the right format
85+
run_id = str(recent_runs[0].id)
86+
87+
# Get the run URL using the run_id parameter
88+
run_url = get_langsmith_url(client, run_id=run_id, project_name=project_name)
89+
90+
print(f"\n{separator}\n🔍 LangSmith Run URL: {run_url}\n{separator}")
91+
return run_url
92+
else:
93+
print(f"\n{separator}\nNo valid runs found\n{separator}")
94+
return None
95+
except Exception as e:
96+
print(f"\n{separator}\nCould not retrieve LangSmith URL: {e}")
97+
import traceback
98+
99+
print(traceback.format_exc())
100+
print(separator)
101+
return None

0 commit comments

Comments
 (0)