klg-asutk-app/backend/app/services/legal_agents/judicial_practice.py
Yuriy 0150aba4f5 Consolidation: KLG ASUTK + PAPA integration
- Unify API: lib/api.ts uses /api/v1, inbox uses /api/inbox (rewrites)
- Remove localhost refs: openapi, inbox page
- Add rewrites: /api/inbox|tmc -> inbox-server, /api/v1 -> FastAPI
- Add stub routes: knowledge/insights, recommendations, search, log-error
- Transfer from PAPA: prompts (inspection, tmc), scripts, supabase, data/tmc-requests
- Fix inbox-server: ORDER BY created_at, package.json
- Remove redundant app/api/inbox/files route (rewrites handle it)
- knowledge/ in gitignore (large PDFs)

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 17:18:31 +03:00

45 lines
2.2 KiB
Python

"""
Агент подбора судебной практики по теме документа.
"""
import json
from .base import BaseLegalAgent, AgentResult
class JudicialPracticeAgent(BaseLegalAgent):
"""Подбирает релевантную судебную практику. Ожидает practice_candidates из БД в context."""
def __init__(self, llm_client=None):
super().__init__("JudicialPracticeAgent", llm_client)
def run(self, context: dict) -> AgentResult:
content = (context.get("content") or "")[:4000]
title = context.get("title", "")
candidates = context.get("practice_candidates") or []
if not candidates:
return AgentResult(True, {"selected_ids": [], "relevance_summary": ""}, agent_name=self.name)
system = (
"Ты — юрист. Выбери из перечня судебных решений те, что релевантны документу. "
"Верни JSON: {\"selected_ids\": [\"id1\", \"id2\"], \"relevance_summary\": \"кратко, почему они применимы\"}"
)
docs = "\n---\n".join(
f"ID: {p.get('id','')}\nСуд: {p.get('court_name','')}\nНомер: {p.get('case_number','')}\nРезюме: {(p.get('summary') or '')[:800]}"
for p in candidates[:15]
)
user = f"Документ: {title}\n\nФрагмент:\n{content[:2000]}\n\nСудебная практика:\n{docs}"
out = self._call_llm(system, user, json_mode=True)
if out:
try:
data = json.loads(out)
ids = [str(x) for x in (data.get("selected_ids") or []) if x][:10]
summary = str(data.get("relevance_summary") or "")[:1000]
return AgentResult(True, {"selected_ids": ids, "relevance_summary": summary}, agent_name=self.name)
except Exception:
pass
ids = [str(p.get("id", "")) for p in candidates[:3] if p.get("id")]
return AgentResult(True, {"selected_ids": ids, "relevance_summary": "Автоматический подбор (LLM недоступен)."}, agent_name=self.name)