Skip to content

Commit d6cef55

Browse files
Merge pull request #16 from WecoAI/refactor/unify-terms-for-clarity
Refactor/unify terms for clarity
2 parents 8f9d7a4 + 4c5ea49 commit d6cef55

File tree

3 files changed

+137
-231
lines changed

3 files changed

+137
-231
lines changed

weco/api.py

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def handle_api_error(e: requests.exceptions.HTTPError, console: rich.console.Con
1717
# sys.exit(1)
1818

1919

20-
def start_optimization_session(
20+
def start_optimization_run(
2121
console: rich.console.Console,
2222
source_code: str,
2323
evaluation_command: str,
@@ -29,14 +29,14 @@ def start_optimization_session(
2929
search_policy_config: Dict[str, Any],
3030
additional_instructions: str = None,
3131
api_keys: Dict[str, Any] = {},
32-
auth_headers: dict = {}, # Add auth_headers
32+
auth_headers: dict = {},
3333
timeout: int = 800,
3434
) -> Dict[str, Any]:
35-
"""Start the optimization session."""
35+
"""Start the optimization run."""
3636
with console.status("[bold green]Starting Optimization..."):
3737
try:
3838
response = requests.post(
39-
f"{__base_url__}/sessions", # Path is relative to base_url
39+
f"{__base_url__}/runs",
4040
json={
4141
"source_code": source_code,
4242
"additional_instructions": additional_instructions,
@@ -49,37 +49,37 @@ def start_optimization_session(
4949
},
5050
"metadata": {"client_name": "cli", "client_version": __pkg_version__, **api_keys},
5151
},
52-
headers=auth_headers, # Add headers
52+
headers=auth_headers,
5353
timeout=timeout,
5454
)
5555
response.raise_for_status()
5656
return response.json()
5757
except requests.exceptions.HTTPError as e:
5858
handle_api_error(e, console)
59-
sys.exit(1) # Exit if starting session fails
59+
sys.exit(1)
6060
except requests.exceptions.RequestException as e:
61-
console.print(f"[bold red]Network Error starting session: {e}[/]")
61+
console.print(f"[bold red]Network Error starting run: {e}[/]")
6262
sys.exit(1)
6363

6464

6565
def evaluate_feedback_then_suggest_next_solution(
66-
session_id: str,
66+
run_id: str,
6767
execution_output: str,
6868
additional_instructions: str = None,
6969
api_keys: Dict[str, Any] = {},
70-
auth_headers: dict = {}, # Add auth_headers
70+
auth_headers: dict = {},
7171
timeout: int = 800,
7272
) -> Dict[str, Any]:
7373
"""Evaluate the feedback and suggest the next solution."""
7474
try:
7575
response = requests.post(
76-
f"{__base_url__}/sessions/{session_id}/suggest", # Path is relative to base_url
76+
f"{__base_url__}/runs/{run_id}/suggest",
7777
json={
7878
"execution_output": execution_output,
7979
"additional_instructions": additional_instructions,
8080
"metadata": {**api_keys},
8181
},
82-
headers=auth_headers, # Add headers
82+
headers=auth_headers,
8383
timeout=timeout,
8484
)
8585
response.raise_for_status()
@@ -93,16 +93,13 @@ def evaluate_feedback_then_suggest_next_solution(
9393
raise # Re-raise the exception
9494

9595

96-
def get_optimization_session_status(
97-
session_id: str, include_history: bool = False, auth_headers: dict = {}, timeout: int = 800
96+
def get_optimization_run_status(
97+
run_id: str, include_history: bool = False, auth_headers: dict = {}, timeout: int = 800
9898
) -> Dict[str, Any]:
99-
"""Get the current status of the optimization session."""
99+
"""Get the current status of the optimization run."""
100100
try:
101101
response = requests.get(
102-
f"{__base_url__}/sessions/{session_id}", # Path is relative to base_url
103-
params={"include_history": include_history},
104-
headers=auth_headers,
105-
timeout=timeout,
102+
f"{__base_url__}/runs/{run_id}", params={"include_history": include_history}, headers=auth_headers, timeout=timeout
106103
)
107104
response.raise_for_status()
108105
return response.json()
@@ -114,49 +111,36 @@ def get_optimization_session_status(
114111
raise # Re-raise
115112

116113

117-
def send_heartbeat(
118-
session_id: str,
119-
auth_headers: dict = {},
120-
timeout: int = 10, # Shorter timeout for non-critical heartbeat
121-
) -> bool:
114+
def send_heartbeat(run_id: str, auth_headers: dict = {}, timeout: int = 10) -> bool:
122115
"""Send a heartbeat signal to the backend."""
123116
try:
124-
response = requests.put(f"{__base_url__}/sessions/{session_id}/heartbeat", headers=auth_headers, timeout=timeout)
125-
response.raise_for_status() # Raises HTTPError for bad responses (4xx or 5xx)
117+
response = requests.put(f"{__base_url__}/runs/{run_id}/heartbeat", headers=auth_headers, timeout=timeout)
118+
response.raise_for_status()
126119
return True
127120
except requests.exceptions.HTTPError as e:
128-
# Log non-critical errors like 409 Conflict (session not running)
129121
if e.response.status_code == 409:
130-
print(f"Heartbeat ignored: Session {session_id} is not running.", file=sys.stderr)
122+
print(f"Heartbeat ignored: Run {run_id} is not running.", file=sys.stderr)
131123
else:
132-
print(f"Heartbeat failed for session {session_id}: HTTP {e.response.status_code}", file=sys.stderr)
133-
# Don't exit, just report failure
124+
print(f"Heartbeat failed for run {run_id}: HTTP {e.response.status_code}", file=sys.stderr)
134125
return False
135126
except requests.exceptions.RequestException as e:
136-
# Network errors are also non-fatal for heartbeats
137-
print(f"Heartbeat network error for session {session_id}: {e}", file=sys.stderr)
127+
print(f"Heartbeat network error for run {run_id}: {e}", file=sys.stderr)
138128
return False
139129

140130

141131
def report_termination(
142-
session_id: str,
143-
status_update: str,
144-
reason: str,
145-
details: Optional[str] = None,
146-
auth_headers: dict = {},
147-
timeout: int = 30, # Reasonably longer timeout for important termination message
132+
run_id: str, status_update: str, reason: str, details: Optional[str] = None, auth_headers: dict = {}, timeout: int = 30
148133
) -> bool:
149134
"""Report the termination reason to the backend."""
150135
try:
151136
response = requests.post(
152-
f"{__base_url__}/sessions/{session_id}/terminate",
137+
f"{__base_url__}/runs/{run_id}/terminate",
153138
json={"status_update": status_update, "termination_reason": reason, "termination_details": details},
154139
headers=auth_headers,
155140
timeout=timeout,
156141
)
157142
response.raise_for_status()
158143
return True
159144
except requests.exceptions.RequestException as e:
160-
# Log failure, but don't prevent CLI exit
161-
print(f"Warning: Failed to report termination to backend for session {session_id}: {e}", file=sys.stderr)
145+
print(f"Warning: Failed to report termination to backend for run {run_id}: {e}", file=sys.stderr)
162146
return False

0 commit comments

Comments
 (0)