|
2 | 2 |
|
3 | 3 | from typing import Dict, List, Optional
|
4 | 4 |
|
5 |
| -from pydantic import BaseModel, Field, model_validator |
| 5 | +from pydantic import BaseModel, Field, field_validator, model_validator |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class GetTextFileContentsRequest(BaseModel):
|
@@ -120,3 +120,80 @@ class FileRanges(BaseModel):
|
120 | 120 | ranges: List[FileRange] = Field(
|
121 | 121 | ..., description="List of line ranges to read from the file"
|
122 | 122 | )
|
| 123 | + |
| 124 | + |
| 125 | +class InsertTextFileContentsRequest(BaseModel): |
| 126 | + """Request model for inserting text into a file. |
| 127 | +
|
| 128 | + Example: |
| 129 | + { |
| 130 | + "path": "/path/to/file", |
| 131 | + "file_hash": "abc123...", |
| 132 | + "after": 5, # Insert after line 5 |
| 133 | + "contents": "new content" |
| 134 | + } |
| 135 | + or |
| 136 | + { |
| 137 | + "path": "/path/to/file", |
| 138 | + "file_hash": "abc123...", |
| 139 | + "before": 5, # Insert before line 5 |
| 140 | + "contents": "new content" |
| 141 | + } |
| 142 | + """ |
| 143 | + |
| 144 | + path: str = Field(..., description="Path to the text file") |
| 145 | + file_hash: str = Field(..., description="Hash of original contents") |
| 146 | + after: Optional[int] = Field( |
| 147 | + None, description="Line number after which to insert content" |
| 148 | + ) |
| 149 | + before: Optional[int] = Field( |
| 150 | + None, description="Line number before which to insert content" |
| 151 | + ) |
| 152 | + contents: str = Field(..., description="Content to insert") |
| 153 | + |
| 154 | + @model_validator(mode="after") |
| 155 | + def validate_position(self) -> "InsertTextFileContentsRequest": |
| 156 | + """Validate that exactly one of after or before is specified.""" |
| 157 | + if (self.after is None and self.before is None) or ( |
| 158 | + self.after is not None and self.before is not None |
| 159 | + ): |
| 160 | + raise ValueError("Exactly one of 'after' or 'before' must be specified") |
| 161 | + return self |
| 162 | + |
| 163 | + @field_validator("after", "before") |
| 164 | + def validate_line_number(cls, v) -> Optional[int]: |
| 165 | + """Validate that line numbers are positive.""" |
| 166 | + if v is not None and v < 1: |
| 167 | + raise ValueError("Line numbers must be positive") |
| 168 | + return v |
| 169 | + |
| 170 | + |
| 171 | +class DeleteTextFileContentsRequest(BaseModel): |
| 172 | + """Request model for deleting text from a file. |
| 173 | +
|
| 174 | + Example: |
| 175 | + { |
| 176 | + "path": "/path/to/file", |
| 177 | + "file_hash": "abc123...", |
| 178 | + "ranges": [ |
| 179 | + { |
| 180 | + "start": 5, |
| 181 | + "end": 10, |
| 182 | + "range_hash": "def456..." |
| 183 | + } |
| 184 | + ] |
| 185 | + } |
| 186 | + """ |
| 187 | + |
| 188 | + path: str = Field(..., description="Path to the text file") |
| 189 | + file_hash: str = Field(..., description="Hash of original contents") |
| 190 | + ranges: List[FileRange] = Field(..., description="List of ranges to delete") |
| 191 | + |
| 192 | + @field_validator("range_hash") |
| 193 | + def validate_range_hash(cls, v: str) -> str: |
| 194 | + """Validate that range_hash is not empty.""" |
| 195 | + if not v: |
| 196 | + raise ValueError("range_hash cannot be empty") |
| 197 | + return v |
| 198 | + |
| 199 | + range_hash: str = Field(..., description="Hash of the content to be deleted") |
0 commit comments