-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
279 lines (228 loc) · 8.52 KB
/
models.py
File metadata and controls
279 lines (228 loc) · 8.52 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
Data models for skill-split.
This module defines all data classes used throughout the skill-split system.
Each model represents a distinct component of the file parsing and storage pipeline.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Dict, List, Optional
class FileFormat(Enum):
"""Detected file structure type."""
MARKDOWN_HEADINGS = "markdown_headings"
XML_TAGS = "xml_tags"
MIXED = "mixed"
UNKNOWN = "unknown"
# NEW formats for component handlers
JSON = "json" # JSON files (config, plugins, hooks)
JSON_SCHEMA = "json_schema" # JSON with known schema
SHELL_SCRIPT = "shell" # Shell scripts
MULTI_FILE = "multi_file" # Component spanning multiple files
# Script file formats (Phase 10+)
PYTHON_SCRIPT = "python" # Python (.py) files
JAVASCRIPT_TYPESCRIPT = "javascript_typescript" # JavaScript/TypeScript files
class FileType(Enum):
"""Type of file based on its location/purpose."""
SKILL = "skill" # /skills/*/SKILL.md
COMMAND = "command" # /commands/*/*.md
REFERENCE = "reference" # /get-shit-done/references/*.md
# NEW types for component handlers
AGENT = "agent" # /agents/*/*.md
PLUGIN = "plugin" # plugin.json + .mcp.json + hooks.json
HOOK = "hook" # hooks.json + shell scripts
OUTPUT_STYLE = "output_style" # /output-styles/*.md
CONFIG = "config" # settings.json, mcp_config.json
DOCUMENTATION = "documentation" # README.md, CLAUDE.md, reference/*.md
# Script files (Phase 10+)
SCRIPT = "script" # Python, Shell, JavaScript, TypeScript files
@dataclass
class Section:
"""
A parsed section of a document.
Attributes:
level: Heading level (1-6 for # through ######) or -1 for XML tags
title: The heading title or tag name
content: The full content under this section (excluding children)
line_start: Starting line number (1-based) in original file
line_end: Ending line number (1-based) in original file
children: Nested subsections
parent: Reference to parent section (for tree navigation)
file_id: Origin file ID (UUID for Supabase, int for SQLite) - optional
file_type: Origin file type - optional, used for composition
"""
level: int
title: str
content: str
line_start: int
line_end: int
closing_tag_prefix: str = "" # Whitespace before closing tag (e.g., " " for " </tag>")
children: List[Section] = field(default_factory=list)
parent: Optional[Section] = field(default=None, repr=False)
file_id: Optional[str] = None # UUID for Supabase, int for SQLite (stored as str for consistency)
file_type: Optional[FileType] = None # Origin file type, preserves context through composition
def add_child(self, child: Section) -> None:
"""Add a child section, setting parent reference."""
child.parent = self
self.children.append(child)
def get_all_content(self) -> str:
"""
Get all content including children recursively.
Returns:
The section content plus all descendant content.
"""
result = self.content
for child in self.children:
result += child.get_all_content()
return result
def to_dict(self) -> dict:
"""Convert to dictionary for JSON serialization."""
return {
"level": self.level,
"title": self.title,
"content": self.content,
"line_start": self.line_start,
"line_end": self.line_end,
"children": [child.to_dict() for child in self.children],
}
@dataclass
class ParsedDocument:
"""
A fully parsed document with structure preserved.
Attributes:
frontmatter: YAML frontmatter as string (empty string if none)
sections: List of top-level sections
file_type: The type of file (skill, command, reference)
format: The detected format type
original_path: Original file path for reference
"""
frontmatter: str
sections: List[Section]
file_type: FileType
format: FileFormat
original_path: str
def get_section_by_title(self, title: str) -> Optional[Section]:
"""
Find a section by title (top-level only).
Args:
title: The section title to search for
Returns:
The matching section or None
"""
for section in self.sections:
if section.title == title:
return section
return None
def to_dict(self) -> dict:
"""Convert to dictionary for JSON serialization."""
return {
"frontmatter": self.frontmatter,
"sections": [s.to_dict() for s in self.sections],
"file_type": self.file_type.value,
"format": self.format.value,
"original_path": self.original_path,
}
@dataclass
class FileMetadata:
"""
Metadata about a parsed file.
Attributes:
path: Full file path
type: File type (skill, command, reference)
frontmatter: YAML frontmatter as string (or None)
hash: SHA256 hash of original file content
"""
path: str
type: FileType
frontmatter: Optional[str]
hash: str
@dataclass
class ValidationResult:
"""
Result of a validation operation.
A perfect round-trip is expected: original file content should
exactly match recomposed content byte-for-byte.
When hashes match:
- is_valid=True
- errors list is empty (cleared on match)
- files_match=True
When hashes do not match:
- is_valid=False
- errors contains failure details
- warnings contains diagnostic information
Attributes:
is_valid: Whether validation passed (True when hashes match)
original_hash: SHA256 hash of the original file
recomposed_hash: SHA256 hash of the recomposed content
files_match: Whether original and recomposed hashes match
errors: List of error messages (empty when hashes match)
warnings: List of warning messages (diagnostic info on failure)
"""
is_valid: bool
original_hash: str = ""
recomposed_hash: str = ""
files_match: bool = False
errors: List[str] = field(default_factory=list)
warnings: List[str] = field(default_factory=list)
def add_error(self, message: str) -> None:
"""Add an error message and mark invalid."""
self.errors.append(message)
self.is_valid = False
def add_warning(self, message: str) -> None:
"""Add a warning message."""
self.warnings.append(message)
@dataclass
class ComponentMetadata:
"""
Metadata for non-markdown components.
For multi-file components (plugins, hooks), tracks
related files and their relationships.
Attributes:
component_type: Type of component (FileType)
primary_file: Main file path
related_files: Associated file paths
schema_version: For validation
dependencies: Other components this depends on
"""
component_type: FileType
primary_file: str
related_files: List[str] = field(default_factory=list)
schema_version: str = "1.0"
dependencies: List[str] = field(default_factory=list)
def to_dict(self) -> dict:
"""Convert to dictionary for JSON serialization."""
return {
"component_type": self.component_type.value,
"primary_file": self.primary_file,
"related_files": self.related_files,
"schema_version": self.schema_version,
"dependencies": self.dependencies,
}
@dataclass
class ComposedSkill:
"""Represents a skill composed from multiple sections."""
section_ids: List[int]
sections: Dict[int, "Section"]
output_path: str
frontmatter: str
title: str
description: str
composed_hash: str = ""
def to_dict(self) -> dict:
"""Serialize to dictionary."""
return {
"section_ids": self.section_ids,
"output_path": self.output_path,
"frontmatter": self.frontmatter,
"title": self.title,
"description": self.description,
"composed_hash": self.composed_hash,
}
@dataclass
class CompositionContext:
"""Metadata for skill composition process."""
source_files: List[str]
source_sections: int
target_format: FileFormat
created_at: str
validation_status: str = "pending"
errors: List[str] = field(default_factory=list)