Новый раг
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,26 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.modules.rag.contracts import EvidenceLink, EvidenceType, RagDocument, RagLayer, RagSource, RagSpan
|
||||
from app.modules.rag.indexing.code.entrypoints.registry import Entrypoint
|
||||
|
||||
|
||||
class EntrypointDocumentBuilder:
|
||||
def build(self, source: RagSource, entrypoint: Entrypoint) -> RagDocument:
|
||||
return RagDocument(
|
||||
layer=RagLayer.CODE_ENTRYPOINTS,
|
||||
lang="python",
|
||||
source=source,
|
||||
title=entrypoint.route_or_command,
|
||||
text=f"{entrypoint.framework} {entrypoint.entry_type} {entrypoint.route_or_command}",
|
||||
span=RagSpan(entrypoint.start_line, entrypoint.end_line),
|
||||
metadata={
|
||||
"entry_id": entrypoint.entry_id,
|
||||
"entry_type": entrypoint.entry_type,
|
||||
"framework": entrypoint.framework,
|
||||
"route_or_command": entrypoint.route_or_command,
|
||||
"handler_symbol_id": entrypoint.handler_symbol_id,
|
||||
"lang_payload": entrypoint.metadata,
|
||||
"artifact_type": "CODE",
|
||||
},
|
||||
links=[EvidenceLink(type=EvidenceType.CODE_SPAN, target_id=entrypoint.entry_id, path=source.path, start_line=entrypoint.start_line, end_line=entrypoint.end_line)],
|
||||
)
|
||||
@@ -0,0 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from hashlib import sha256
|
||||
|
||||
from app.modules.rag.indexing.code.entrypoints.registry import Entrypoint
|
||||
|
||||
|
||||
class FastApiEntrypointDetector:
|
||||
_METHODS = {"get", "post", "put", "patch", "delete"}
|
||||
|
||||
def detect(self, *, path: str, symbols: list) -> list[Entrypoint]:
|
||||
items: list[Entrypoint] = []
|
||||
for symbol in symbols:
|
||||
decorators = symbol.decorators or []
|
||||
for decorator in decorators:
|
||||
name = decorator.lower()
|
||||
tail = name.split(".")[-1]
|
||||
if tail not in self._METHODS and ".route" not in name:
|
||||
continue
|
||||
route = decorator.split("(")[-1].rstrip(")") if "(" in decorator else decorator
|
||||
items.append(
|
||||
Entrypoint(
|
||||
entry_id=sha256(f"{path}|fastapi|{symbol.symbol_id}|{decorator}".encode("utf-8")).hexdigest(),
|
||||
entry_type="http",
|
||||
framework="fastapi",
|
||||
route_or_command=route,
|
||||
handler_symbol_id=symbol.symbol_id,
|
||||
path=path,
|
||||
start_line=symbol.start_line,
|
||||
end_line=symbol.end_line,
|
||||
metadata={"methods": [tail.upper()] if tail in self._METHODS else []},
|
||||
)
|
||||
)
|
||||
return items
|
||||
28
app/modules/rag/indexing/code/entrypoints/flask_detector.py
Normal file
28
app/modules/rag/indexing/code/entrypoints/flask_detector.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from hashlib import sha256
|
||||
|
||||
from app.modules.rag.indexing.code.entrypoints.registry import Entrypoint
|
||||
|
||||
|
||||
class FlaskEntrypointDetector:
|
||||
def detect(self, *, path: str, symbols: list) -> list[Entrypoint]:
|
||||
items: list[Entrypoint] = []
|
||||
for symbol in symbols:
|
||||
for decorator in symbol.decorators or []:
|
||||
lowered = decorator.lower()
|
||||
if ".route" not in lowered:
|
||||
continue
|
||||
items.append(
|
||||
Entrypoint(
|
||||
entry_id=sha256(f"{path}|flask|{symbol.symbol_id}|{decorator}".encode("utf-8")).hexdigest(),
|
||||
entry_type="http",
|
||||
framework="flask",
|
||||
route_or_command=decorator,
|
||||
handler_symbol_id=symbol.symbol_id,
|
||||
path=path,
|
||||
start_line=symbol.start_line,
|
||||
end_line=symbol.end_line,
|
||||
)
|
||||
)
|
||||
return items
|
||||
27
app/modules/rag/indexing/code/entrypoints/registry.py
Normal file
27
app/modules/rag/indexing/code/entrypoints/registry.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class Entrypoint:
|
||||
entry_id: str
|
||||
entry_type: str
|
||||
framework: str
|
||||
route_or_command: str
|
||||
handler_symbol_id: str
|
||||
path: str
|
||||
start_line: int
|
||||
end_line: int
|
||||
metadata: dict = field(default_factory=dict)
|
||||
|
||||
|
||||
class EntrypointDetectorRegistry:
|
||||
def __init__(self, detectors: list) -> None:
|
||||
self._detectors = detectors
|
||||
|
||||
def detect_all(self, *, path: str, symbols: list) -> list[Entrypoint]:
|
||||
items: list[Entrypoint] = []
|
||||
for detector in self._detectors:
|
||||
items.extend(detector.detect(path=path, symbols=symbols))
|
||||
return items
|
||||
@@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from hashlib import sha256
|
||||
|
||||
from app.modules.rag.indexing.code.entrypoints.registry import Entrypoint
|
||||
|
||||
|
||||
class TyperClickEntrypointDetector:
|
||||
def detect(self, *, path: str, symbols: list) -> list[Entrypoint]:
|
||||
items: list[Entrypoint] = []
|
||||
for symbol in symbols:
|
||||
for decorator in symbol.decorators or []:
|
||||
lowered = decorator.lower()
|
||||
if ".command" not in lowered and ".callback" not in lowered:
|
||||
continue
|
||||
framework = "typer" if "typer" in lowered else "click"
|
||||
items.append(
|
||||
Entrypoint(
|
||||
entry_id=sha256(f"{path}|{framework}|{symbol.symbol_id}|{decorator}".encode("utf-8")).hexdigest(),
|
||||
entry_type="cli",
|
||||
framework=framework,
|
||||
route_or_command=decorator,
|
||||
handler_symbol_id=symbol.symbol_id,
|
||||
path=path,
|
||||
start_line=symbol.start_line,
|
||||
end_line=symbol.end_line,
|
||||
)
|
||||
)
|
||||
return items
|
||||
Reference in New Issue
Block a user