Spaces:
Sleeping
Sleeping
File size: 4,575 Bytes
b92d20c 2d2c6e4 b92d20c 2d2c6e4 b92d20c 2d2c6e4 b92d20c 2d2c6e4 b92d20c 2d2c6e4 b92d20c 2d2c6e4 b92d20c 2d2c6e4 50242cc 2d2c6e4 b92d20c 2d2c6e4 b92d20c 2d2c6e4 b92d20c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | """
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FILE: run_basic_agent.py
FOLDER: examples/
PURPOSE: Demonstrates running one complete episode in CodeReview-Env
USED BY: Anyone wanting to try the environment manually
KEY FUNCTIONS: main() β connects, resets, steps, prints reward
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
"""
import os
import asyncio
from codereview_env.client import CodeReviewEnv
from codereview_env.models import CodeReviewAction
async def main():
print("============================================================")
print(" π€ Welcome to CodeReview-Env Basic Agent Run ")
print(" This script will connect to the environment, load a PR")
print(" diff, and submit a hardcoded actionable review.")
print("============================================================\n")
port = os.getenv("PORT", "8000") # Use 7860 if running via docker mapping
base_url = f"http://localhost:{port}"
print(f"[*] Connecting to Environment Server at {base_url}...")
async with CodeReviewEnv(base_url=base_url) as env:
try:
print("\n[*} Calling reset()...")
obs = await env.reset()
print(f" Loaded file: {obs.filename} ({obs.language})")
print(" PR Diff Snippet:")
print("------------------------------------------------------------")
print(
obs.pr_diff[:300] + "\n..." if len(obs.pr_diff) > 300 else obs.pr_diff
)
print("------------------------------------------------------------\n")
hardcoded_review = (
"Line 3: There's an off-by-one error here. The loop should use < len(items) "
"instead of <= len(items). Consider using enumerate() for cleaner iteration."
)
print(f'[*] Submitting Review:\n "{hardcoded_review}"\n')
action = CodeReviewAction(
review_comment=hardcoded_review, severity="major", line_references=[3]
)
result = await env.step(action)
# The result could be a StepResult or mapping depending on OpenEnv integration
info = (
result.info
if hasattr(result, "info")
else result.get("info", {}) if isinstance(result, dict) else {}
)
reward = float(
result.reward
if hasattr(result, "reward")
else result.get("reward", 0.05) if hasattr(result, "get") else 0.05
)
breakdown = info.get("reward_breakdown", {})
checks = breakdown.get("checks", {})
llm = breakdown.get("llm_scores", {})
def tick(val):
return "β
" if val else "β"
table = f"""
βββββββββββββββββββββββββββββββ¬βββββββββ
β Check β Result β
βββββββββββββββββββββββββββββββΌβββββββββ€
β Not empty β {tick(checks.get('not_empty'))} β
β Detailed (>100 chars) β {tick(checks.get('is_detailed'))} β
β References line numbers β {tick(checks.get('has_line_references'))} β
β Actionable language β {tick(checks.get('is_actionable'))} β
β Not generic β {tick(checks.get('not_generic'))} β
β LLM Bug Detection β {llm.get('bug_detection', 0)}/10 β
β LLM Specificity β {llm.get('specificity', 0)}/10 β
β LLM Actionability β {llm.get('actionability', 0)}/10 β
βββββββββββββββββββββββββββββββΌβββββββββ€
β TOTAL REWARD β {reward:.2f} β
βββββββββββββββββββββββββββββββ΄βββββββββ
"""
print(table)
print("Run with: python examples/run_basic_agent.py\n")
except Exception as e:
print(f"Error communicating with environment: {e}")
print("Make sure your API server is running (uvicorn server.app:app)")
if __name__ == "__main__":
asyncio.run(main())
|