Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
213 lines (182 loc) · 6.92 KB
/
Copy pathmodels.py
File metadata and controls
213 lines (182 loc) · 6.92 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
"""
Data models and validation for the synthesis pool system.
"""
from dataclasses import dataclass, field
from datetime import date
from typing import Optional, List
from enum import Enum
class TimestampConfidence(str, Enum):
EXACT = "exact"
MONTH = "month"
QUARTER = "quarter"
FUZZY = "fuzzy"
# SourceType is now a plain string to accept any value
# Previously was an Enum but now accepts any source type dynamically
class DecayState(str, Enum):
FRESH = "fresh"
COMPOSTING = "composting"
INERT = "inert"
class SynthesisType(str, Enum):
MEDICINE = "medicine"
MATERIAL = "material"
FUEL = "fuel"
TOXIN = "toxin"
UNKNOWN = "unknown"
class PoolHealth(str, Enum):
THRIVING = "thriving"
STRESSED = "stressed"
TRAUMATIZED = "traumatized"
DISSOCIATED = "dissociated"
@dataclass
class MemoryMatter:
"""Raw input fed to the pool."""
id: str
timestamp_actual: date
timestamp_confidence: TimestampConfidence
source_type: str
source_date: date
raw: str
layers: str # pipe-separated
tags: str # comma-separated
toxicity: int # 0-5
nutrient_density: int # 0-5
decay_state: DecayState
name: Optional[str] = None # LLM-generated evocative name
pool_reaction: Optional[str] = None
linked_matter: Optional[str] = None # comma-separated ids
def __post_init__(self):
"""Validate fields."""
if not isinstance(self.timestamp_actual, date):
if isinstance(self.timestamp_actual, str):
self.timestamp_actual = date.fromisoformat(self.timestamp_actual)
if not isinstance(self.source_date, date):
if isinstance(self.source_date, str):
self.source_date = date.fromisoformat(self.source_date)
if isinstance(self.timestamp_confidence, str):
self.timestamp_confidence = TimestampConfidence(self.timestamp_confidence)
# source_type is now a plain string - no conversion needed
if isinstance(self.decay_state, str):
self.decay_state = DecayState(self.decay_state)
if not (0 <= self.toxicity <= 5):
raise ValueError(f"toxicity must be 0-5, got {self.toxicity}")
if not (0 <= self.nutrient_density <= 5):
raise ValueError(f"nutrient_density must be 0-5, got {self.nutrient_density}")
def to_dict(self):
"""Convert to dictionary for CSV writing."""
return {
'id': self.id,
'timestamp_actual': self.timestamp_actual.isoformat(),
'timestamp_confidence': self.timestamp_confidence.value,
'source_type': self.source_type,
'source_date': self.source_date.isoformat(),
'raw': self.raw,
'layers': self.layers,
'tags': self.tags,
'toxicity': self.toxicity,
'nutrient_density': self.nutrient_density,
'decay_state': self.decay_state.value,
'name': self.name or '',
'pool_reaction': self.pool_reaction or '',
'linked_matter': self.linked_matter or ''
}
@classmethod
def from_dict(cls, data: dict):
"""Create from dictionary (CSV row)."""
return cls(
id=data['id'],
timestamp_actual=data['timestamp_actual'],
timestamp_confidence=data['timestamp_confidence'],
source_type=data['source_type'],
source_date=data['source_date'],
raw=data['raw'],
layers=data['layers'],
tags=data['tags'],
toxicity=int(data['toxicity']),
nutrient_density=int(data['nutrient_density']),
decay_state=data['decay_state'],
name=data.get('name') if data.get('name') else None,
pool_reaction=data['pool_reaction'] if data.get('pool_reaction') else None,
linked_matter=data['linked_matter'] if data.get('linked_matter') else None
)
@dataclass
class SynthesisOutput:
"""What the pool has grown/produced."""
id: str
type: SynthesisType
derived_from: str # comma-separated memory_matter ids
concentration: int # 1-5
label: str
description: str
application: str
harvest_date: date
def __post_init__(self):
"""Validate fields."""
if not isinstance(self.harvest_date, date):
if isinstance(self.harvest_date, str):
self.harvest_date = date.fromisoformat(self.harvest_date)
if isinstance(self.type, str):
self.type = SynthesisType(self.type)
if not (1 <= self.concentration <= 5):
raise ValueError(f"concentration must be 1-5, got {self.concentration}")
def to_dict(self):
"""Convert to dictionary for CSV writing."""
return {
'id': self.id,
'type': self.type.value,
'derived_from': self.derived_from,
'concentration': self.concentration,
'label': self.label,
'description': self.description,
'application': self.application,
'harvest_date': self.harvest_date.isoformat()
}
@classmethod
def from_dict(cls, data: dict):
"""Create from dictionary (CSV row)."""
return cls(
id=data['id'],
type=data['type'],
derived_from=data['derived_from'],
concentration=int(data['concentration']),
label=data['label'],
description=data['description'],
application=data['application'],
harvest_date=data['harvest_date']
)
@dataclass
class PoolState:
"""Snapshot of pool health over time."""
snapshot_date: date
health: PoolHealth
matter_count: int
active_synthesis: str
distress_signals: str
contamination: str
def __post_init__(self):
"""Validate fields."""
if not isinstance(self.snapshot_date, date):
if isinstance(self.snapshot_date, str):
self.snapshot_date = date.fromisoformat(self.snapshot_date)
if isinstance(self.health, str):
self.health = PoolHealth(self.health)
def to_dict(self):
"""Convert to dictionary for CSV writing."""
return {
'snapshot_date': self.snapshot_date.isoformat(),
'health': self.health.value,
'matter_count': self.matter_count,
'active_synthesis': self.active_synthesis,
'distress_signals': self.distress_signals,
'contamination': self.contamination
}
@classmethod
def from_dict(cls, data: dict):
"""Create from dictionary (CSV row)."""
return cls(
snapshot_date=data['snapshot_date'],
health=data['health'],
matter_count=int(data['matter_count']),
active_synthesis=data['active_synthesis'],
distress_signals=data['distress_signals'],
contamination=data['contamination']
)