|
| 1 | +# Tencent is pleased to support the open source community by making tRPC-Agent-Python available. |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 4 | +# |
| 5 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 6 | +"""LLM enrichment step: run the agent once over the diff + static findings.""" |
| 7 | +import json |
| 8 | +import re |
| 9 | +import uuid |
| 10 | + |
| 11 | +from trpc_agent_sdk.runners import Runner |
| 12 | +from trpc_agent_sdk.sessions import InMemorySessionService |
| 13 | +from trpc_agent_sdk.types import Content, Part |
| 14 | + |
| 15 | +from agent.prompts import REVIEW_REQUEST_TEMPLATE |
| 16 | + |
| 17 | +from .findings import Finding |
| 18 | +from .redaction import redact_text |
| 19 | + |
| 20 | +_FENCE_RE = re.compile(r"```(?:json)?\s*(\{.*?\})\s*```", re.DOTALL) |
| 21 | + |
| 22 | + |
| 23 | +def parse_llm_output(text: str): |
| 24 | + """Extract (findings_dicts, summary) from model text; ([], "") on failure.""" |
| 25 | + candidates = [text.strip()] |
| 26 | + fence = _FENCE_RE.search(text) |
| 27 | + if fence: |
| 28 | + candidates.insert(0, fence.group(1)) |
| 29 | + for candidate in candidates: |
| 30 | + try: |
| 31 | + payload = json.loads(candidate) |
| 32 | + except (json.JSONDecodeError, ValueError): |
| 33 | + continue |
| 34 | + if isinstance(payload, dict): |
| 35 | + return list(payload.get("findings") or []), str(payload.get("summary") or "") |
| 36 | + return [], "" |
| 37 | + |
| 38 | + |
| 39 | +async def run_llm_review(agent, diff_text: str, static_findings): |
| 40 | + """Run one review turn. Returns (llm_findings, summary, warnings).""" |
| 41 | + warnings = [] |
| 42 | + runner = Runner(app_name="code_review_agent", agent=agent, |
| 43 | + session_service=InMemorySessionService()) |
| 44 | + prompt = REVIEW_REQUEST_TEMPLATE.format( |
| 45 | + findings_json=json.dumps([f.model_dump() for f in static_findings]), |
| 46 | + diff=redact_text(diff_text)) |
| 47 | + message = Content(role="user", parts=[Part.from_text(text=prompt)]) |
| 48 | + final_text = "" |
| 49 | + async for event in runner.run_async(user_id="cr_user", |
| 50 | + session_id=uuid.uuid4().hex, |
| 51 | + new_message=message): |
| 52 | + if event.partial or not event.content or not event.content.parts: |
| 53 | + continue |
| 54 | + for part in event.content.parts: |
| 55 | + if getattr(part, "text", None) and not getattr(part, "thought", None): |
| 56 | + final_text += part.text |
| 57 | + raw_findings, summary = parse_llm_output(final_text) |
| 58 | + if not summary and final_text: |
| 59 | + warnings.append("llm output was not valid JSON; ignored") |
| 60 | + findings = [] |
| 61 | + for raw in raw_findings: |
| 62 | + try: |
| 63 | + findings.append(Finding( |
| 64 | + severity=str(raw.get("severity", "info")), |
| 65 | + category=str(raw.get("category", "security")), |
| 66 | + file=str(raw.get("file", "")), |
| 67 | + line=int(raw.get("line", 0)), |
| 68 | + title=str(raw.get("title", "")), |
| 69 | + evidence=str(raw.get("evidence", "")), |
| 70 | + recommendation=str(raw.get("recommendation", "")), |
| 71 | + confidence=float(raw.get("confidence", 0.5)), |
| 72 | + source="llm")) |
| 73 | + except (TypeError, ValueError): |
| 74 | + warnings.append(f"skipped malformed llm finding: {raw!r}") |
| 75 | + return findings, summary, warnings |
0 commit comments