Spaces:
Sleeping
Sleeping
Commit ·
c040755
1
Parent(s): e5cf3fa
Add trace viewer accordion back
Browse filesUses render_trace from the harness package — no local copy needed.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
app.py
CHANGED
|
@@ -22,6 +22,7 @@ from dotenv import load_dotenv
|
|
| 22 |
from llm_harness.agent import run_agent_loop
|
| 23 |
from llm_harness.prompt import build_system_prompt
|
| 24 |
from llm_harness.tools import TOOL_DEFINITIONS
|
|
|
|
| 25 |
from llm_harness.types import Message, ToolCallEvent, ToolResultEvent
|
| 26 |
|
| 27 |
from sandbox_e2b import run_python as e2b_run_python
|
|
@@ -75,6 +76,7 @@ def chat(
|
|
| 75 |
if not MODEL:
|
| 76 |
yield (
|
| 77 |
"Error: LH_MODEL not set.",
|
|
|
|
| 78 |
workspace_path,
|
| 79 |
scratch_path,
|
| 80 |
session_cost,
|
|
@@ -85,6 +87,7 @@ def chat(
|
|
| 85 |
yield (
|
| 86 |
f"Session cost limit reached (${session_cost:.2f} / "
|
| 87 |
f"${MAX_SESSION_COST:.2f}). Start a new session.",
|
|
|
|
| 88 |
workspace_path,
|
| 89 |
scratch_path,
|
| 90 |
session_cost,
|
|
@@ -126,7 +129,7 @@ def chat(
|
|
| 126 |
if isinstance(event, ToolCallEvent):
|
| 127 |
tool_call_count += 1
|
| 128 |
status = f"*Exploring documents ({tool_call_count} tool calls)...*"
|
| 129 |
-
yield status, workspace_path, scratch_path, session_cost
|
| 130 |
elif isinstance(event, ToolResultEvent):
|
| 131 |
continue
|
| 132 |
else:
|
|
@@ -135,6 +138,7 @@ def chat(
|
|
| 135 |
except Exception as exc:
|
| 136 |
yield (
|
| 137 |
f"Error: {exc}",
|
|
|
|
| 138 |
workspace_path,
|
| 139 |
scratch_path,
|
| 140 |
session_cost,
|
|
@@ -145,9 +149,14 @@ def chat(
|
|
| 145 |
trace.wall_time_s = round(time.monotonic() - start, 2)
|
| 146 |
answer = trace.answer or "(no answer)"
|
| 147 |
stats = format_stats(trace)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
yield (
|
| 150 |
f"{answer}\n\n---\n{stats}",
|
|
|
|
| 151 |
workspace_path,
|
| 152 |
scratch_path,
|
| 153 |
session_cost,
|
|
@@ -180,20 +189,24 @@ def build_app() -> gr.Blocks:
|
|
| 180 |
show_label=False,
|
| 181 |
)
|
| 182 |
|
|
|
|
|
|
|
|
|
|
| 183 |
def respond(
|
| 184 |
message, history, files, workspace_path, scratch_path, session_cost
|
| 185 |
):
|
| 186 |
history = history or []
|
| 187 |
history.append({"role": "user", "content": message})
|
| 188 |
|
| 189 |
-
for response, wp, sp, sc in chat(
|
| 190 |
message, history[:-1], files, workspace_path, scratch_path, session_cost
|
| 191 |
):
|
| 192 |
history_with_response = [
|
| 193 |
*history,
|
| 194 |
{"role": "assistant", "content": response},
|
| 195 |
]
|
| 196 |
-
|
|
|
|
| 197 |
|
| 198 |
msg.submit(
|
| 199 |
respond,
|
|
@@ -208,6 +221,8 @@ def build_app() -> gr.Blocks:
|
|
| 208 |
outputs=[
|
| 209 |
chatbot,
|
| 210 |
msg,
|
|
|
|
|
|
|
| 211 |
workspace_state,
|
| 212 |
scratch_state,
|
| 213 |
cost_state,
|
|
|
|
| 22 |
from llm_harness.agent import run_agent_loop
|
| 23 |
from llm_harness.prompt import build_system_prompt
|
| 24 |
from llm_harness.tools import TOOL_DEFINITIONS
|
| 25 |
+
from llm_harness.trace_viewer import render_trace
|
| 26 |
from llm_harness.types import Message, ToolCallEvent, ToolResultEvent
|
| 27 |
|
| 28 |
from sandbox_e2b import run_python as e2b_run_python
|
|
|
|
| 76 |
if not MODEL:
|
| 77 |
yield (
|
| 78 |
"Error: LH_MODEL not set.",
|
| 79 |
+
"",
|
| 80 |
workspace_path,
|
| 81 |
scratch_path,
|
| 82 |
session_cost,
|
|
|
|
| 87 |
yield (
|
| 88 |
f"Session cost limit reached (${session_cost:.2f} / "
|
| 89 |
f"${MAX_SESSION_COST:.2f}). Start a new session.",
|
| 90 |
+
"",
|
| 91 |
workspace_path,
|
| 92 |
scratch_path,
|
| 93 |
session_cost,
|
|
|
|
| 129 |
if isinstance(event, ToolCallEvent):
|
| 130 |
tool_call_count += 1
|
| 131 |
status = f"*Exploring documents ({tool_call_count} tool calls)...*"
|
| 132 |
+
yield status, "", workspace_path, scratch_path, session_cost
|
| 133 |
elif isinstance(event, ToolResultEvent):
|
| 134 |
continue
|
| 135 |
else:
|
|
|
|
| 138 |
except Exception as exc:
|
| 139 |
yield (
|
| 140 |
f"Error: {exc}",
|
| 141 |
+
"",
|
| 142 |
workspace_path,
|
| 143 |
scratch_path,
|
| 144 |
session_cost,
|
|
|
|
| 149 |
trace.wall_time_s = round(time.monotonic() - start, 2)
|
| 150 |
answer = trace.answer or "(no answer)"
|
| 151 |
stats = format_stats(trace)
|
| 152 |
+
trace_html = render_trace(
|
| 153 |
+
{"question": message, "passed": True, "assertions": {}, "trace": asdict(trace)},
|
| 154 |
+
max_chars=2000,
|
| 155 |
+
)
|
| 156 |
|
| 157 |
yield (
|
| 158 |
f"{answer}\n\n---\n{stats}",
|
| 159 |
+
trace_html,
|
| 160 |
workspace_path,
|
| 161 |
scratch_path,
|
| 162 |
session_cost,
|
|
|
|
| 189 |
show_label=False,
|
| 190 |
)
|
| 191 |
|
| 192 |
+
with gr.Accordion("Trace", open=False, visible=False) as trace_accordion:
|
| 193 |
+
trace_display = gr.HTML("")
|
| 194 |
+
|
| 195 |
def respond(
|
| 196 |
message, history, files, workspace_path, scratch_path, session_cost
|
| 197 |
):
|
| 198 |
history = history or []
|
| 199 |
history.append({"role": "user", "content": message})
|
| 200 |
|
| 201 |
+
for response, trace_html, wp, sp, sc in chat(
|
| 202 |
message, history[:-1], files, workspace_path, scratch_path, session_cost
|
| 203 |
):
|
| 204 |
history_with_response = [
|
| 205 |
*history,
|
| 206 |
{"role": "assistant", "content": response},
|
| 207 |
]
|
| 208 |
+
visible = gr.update(visible=bool(trace_html))
|
| 209 |
+
yield history_with_response, "", trace_html, visible, wp, sp, sc
|
| 210 |
|
| 211 |
msg.submit(
|
| 212 |
respond,
|
|
|
|
| 221 |
outputs=[
|
| 222 |
chatbot,
|
| 223 |
msg,
|
| 224 |
+
trace_display,
|
| 225 |
+
trace_accordion,
|
| 226 |
workspace_state,
|
| 227 |
scratch_state,
|
| 228 |
cost_state,
|