feat(tools): run_python, web search/fetch, market data, options, portfolio

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016DvdRZkyCy9NJpHYDdPqmS
This commit is contained in:
Aryan SinghandClaude Opus 4.8 committed 2026-07-01 16:29:29 -05:00
1 parent f41cf75d8a
commit 63085113ed
6 files changed
+552

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
"""run_python — the calculator. Agents call this for ALL arithmetic instead of doing
mental math (LLMs are unreliable at it). Executes in an isolated subprocess with a hard
timeout. Kept math-only by convention; runs model-generated code on this machine, so
scope is deliberately narrow.
"""
from __future__ import annotations
import subprocess
import sys
from langchain_core.tools import tool
_TIMEOUT_S = 5
@tool
def run_python(code: str) -> str:
"""Execute a short Python snippet for EXACT math and return its stdout.
Use for ANY arithmetic: position sizing, percentages, P/E, returns, averages, ratios.
You MUST print() the result. `math` and `statistics` are importable.
Example: `print(round(1000 / 23.55, 4))`. No network or file access; 5s limit.
"""
try:
proc = subprocess.run(
[sys.executable, "-I", "-c", code],
capture_output=True,
text=True,
timeout=_TIMEOUT_S,
)
except subprocess.TimeoutExpired:
return f"ERROR: execution timed out ({_TIMEOUT_S}s limit)."
except Exception as e: # pragma: no cover - defensive
return f"ERROR: {type(e).__name__}: {e}"
if proc.returncode != 0:
return f"ERROR: {proc.stderr.strip() or 'non-zero exit'}"
out = proc.stdout.strip()
return out or "(no output — did you forget to print()?)"