refactor: 优化异常处理,使用 cached_property 替代 property,增强代码可读性;添加 RelationshipType 枚举以规范化关系类型

This commit is contained in:
catlog22
2025-12-21 17:01:49 +08:00
parent c12ef3e772
commit fa81793bea
3 changed files with 13 additions and 112 deletions

View File

@@ -2,6 +2,7 @@
from __future__ import annotations
from enum import Enum
from typing import Any, Dict, List, Optional, Tuple
from pydantic import BaseModel, Field, field_validator
@@ -62,24 +63,23 @@ class IndexedFile(BaseModel):
return cleaned
class RelationshipType(str, Enum):
"""Types of code relationships."""
CALL = "call"
INHERITS = "inherits"
IMPORTS = "imports"
class CodeRelationship(BaseModel):
"""A relationship between code symbols (e.g., function calls, inheritance)."""
source_symbol: str = Field(..., min_length=1, description="Name of source symbol")
target_symbol: str = Field(..., min_length=1, description="Name of target symbol")
relationship_type: str = Field(..., min_length=1, description="Type of relationship (call, inherits, etc.)")
relationship_type: RelationshipType = Field(..., description="Type of relationship (call, inherits, etc.)")
source_file: str = Field(..., min_length=1, description="File path containing source symbol")
target_file: Optional[str] = Field(default=None, description="File path containing target (None if same file)")
source_line: int = Field(..., ge=1, description="Line number where relationship occurs (1-based)")
@field_validator("relationship_type")
@classmethod
def validate_relationship_type(cls, value: str) -> str:
allowed_types = {"call", "inherits", "imports"}
if value not in allowed_types:
raise ValueError(f"relationship_type must be one of {allowed_types}")
return value
class AdditionalLocation(BaseModel):
"""A pointer to another location where a similar result was found.