File size: 6,024 Bytes
f2200ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from typing import List, Dict, Tuple
from pydantic import BaseModel, Field, field_validator

class PaperInput(BaseModel):
    """Validated paper input schema"""
    paper_title: str = Field(..., min_length=5, max_length=500)
    paper_abstract: str = Field(..., min_length=50, max_length=5000)
    reviews: List[str] = Field(..., min_length=1, max_length=10)
    
    @field_validator('paper_title')
    @classmethod
    def validate_title(cls, v: str) -> str:
        """Validate paper title"""
        if not v or not v.strip():
            raise ValueError("Paper title cannot be empty")
        return v.strip()
    
    @field_validator('paper_abstract')
    @classmethod
    def validate_abstract(cls, v: str) -> str:
        """Validate paper abstract"""
        if not v or not v.strip():
            raise ValueError("Paper abstract cannot be empty")
        if len(v.strip()) < 50:
            raise ValueError("Paper abstract must be at least 50 characters")
        return v.strip()
    
    @field_validator('reviews')
    @classmethod
    def validate_reviews(cls, v: List[str]) -> List[str]:
        """Validate reviews"""
        if not v:
            raise ValueError("At least one review is required")
        
        valid_reviews = []
        for i, review in enumerate(v):
            if not isinstance(review, str):
                raise ValueError(f"Review {i} must be a string")
            
            cleaned = review.strip()
            if len(cleaned) < 50:
                raise ValueError(f"Review {i} must be at least 50 characters")
            
            valid_reviews.append(cleaned)
        
        return valid_reviews

class CritiqueInput(BaseModel):
    """Validated critique input schema"""
    reviews: List[str] = Field(..., min_length=1, max_length=10)
    
    @field_validator('reviews')
    @classmethod
    def validate_reviews(cls, v: List[str]) -> List[str]:
        """Validate reviews"""
        if not v:
            raise ValueError("At least one review is required")
        
        valid_reviews = []
        for review in v:
            if isinstance(review, str) and len(review.strip()) >= 50:
                valid_reviews.append(review.strip())
        
        if not valid_reviews:
            raise ValueError("No valid reviews found (must be at least 50 characters)")
        
        return valid_reviews

class DisagreementInput(BaseModel):
    """Validated disagreement detection input schema"""
    critiques: List[Dict] = Field(..., min_length=2)
    
    @field_validator('critiques')
    @classmethod
    def validate_critiques(cls, v: List[Dict]) -> List[Dict]:
        """Validate critique structure"""
        if len(v) < 2:
            raise ValueError("At least 2 critiques required for disagreement detection")
        
        required_keys = {'Methodology', 'Experiments', 'Clarity', 'Significance', 'Novelty'}
        
        for i, critique in enumerate(v):
            if not isinstance(critique, dict):
                raise ValueError(f"Critique {i} must be a dictionary")
            
            # Check if critique has the expected structure
            if not any(key in critique for key in required_keys):
                raise ValueError(f"Critique {i} missing required categories")
        
        return v

class SearchInput(BaseModel):
    """Validated search input schema"""
    paper_title: str = Field(..., min_length=5, max_length=500)
    paper_abstract: str = Field(..., min_length=50, max_length=5000)
    critiques: List[Dict] = Field(..., min_length=1)
    
    @field_validator('paper_title')
    @classmethod
    def validate_title(cls, v: str) -> str:
        """Validate paper title"""
        if not v or not v.strip():
            raise ValueError("Paper title cannot be empty")
        return v.strip()
    
    @field_validator('paper_abstract')
    @classmethod
    def validate_abstract(cls, v: str) -> str:
        """Validate paper abstract"""
        if not v or not v.strip():
            raise ValueError("Paper abstract cannot be empty")
        return v.strip()

def validate_paper_input(
    paper_title: str,
    paper_abstract: str,
    reviews: List[str]
) -> Tuple[bool, str]:
    """
    Validate paper input data
    
    Args:
        paper_title: Paper title
        paper_abstract: Paper abstract
        reviews: List of review texts
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    try:
        PaperInput(
            paper_title=paper_title,
            paper_abstract=paper_abstract,
            reviews=reviews
        )
        return True, ""
    except Exception as e:
        return False, str(e)

def validate_critique_input(reviews: List[str]) -> Tuple[bool, str]:
    """
    Validate critique extraction input
    
    Args:
        reviews: List of review texts
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    try:
        CritiqueInput(reviews=reviews)
        return True, ""
    except Exception as e:
        return False, str(e)

def validate_disagreement_input(critiques: List[Dict]) -> Tuple[bool, str]:
    """
    Validate disagreement detection input
    
    Args:
        critiques: List of critique dictionaries
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    try:
        DisagreementInput(critiques=critiques)
        return True, ""
    except Exception as e:
        return False, str(e)

def validate_search_input(
    paper_title: str,
    paper_abstract: str,
    critiques: List[Dict]
) -> Tuple[bool, str]:
    """
    Validate search input
    
    Args:
        paper_title: Paper title
        paper_abstract: Paper abstract
        critiques: List of critique dictionaries
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    try:
        SearchInput(
            paper_title=paper_title,
            paper_abstract=paper_abstract,
            critiques=critiques
        )
        return True, ""
    except Exception as e:
        return False, str(e)