58
58
59
59
logger = logging .getLogger (__name__ )
60
60
61
+ DEFAULT_MAX_VARCHAR_LENGTH = 256
62
+
61
63
62
64
class DynamicJSON (TypeDecorator ):
63
65
"""A JSON-like type that uses JSONB on PostgreSQL and TEXT with JSON
@@ -92,17 +94,25 @@ def process_result_value(self, value, dialect: Dialect):
92
94
93
95
class Base (DeclarativeBase ):
94
96
"""Base class for database tables."""
97
+
95
98
pass
96
99
97
100
98
101
class StorageSession (Base ):
99
102
"""Represents a session stored in the database."""
103
+
100
104
__tablename__ = "sessions"
101
105
102
- app_name : Mapped [str ] = mapped_column (String , primary_key = True )
103
- user_id : Mapped [str ] = mapped_column (String , primary_key = True )
106
+ app_name : Mapped [str ] = mapped_column (
107
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), primary_key = True
108
+ )
109
+ user_id : Mapped [str ] = mapped_column (
110
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), primary_key = True
111
+ )
104
112
id : Mapped [str ] = mapped_column (
105
- String , primary_key = True , default = lambda : str (uuid .uuid4 ())
113
+ String (DEFAULT_MAX_VARCHAR_LENGTH ),
114
+ primary_key = True ,
115
+ default = lambda : str (uuid .uuid4 ()),
106
116
)
107
117
108
118
state : Mapped [MutableDict [str , Any ]] = mapped_column (
@@ -125,16 +135,27 @@ def __repr__(self):
125
135
126
136
class StorageEvent (Base ):
127
137
"""Represents an event stored in the database."""
138
+
128
139
__tablename__ = "events"
129
140
130
- id : Mapped [str ] = mapped_column (String , primary_key = True )
131
- app_name : Mapped [str ] = mapped_column (String , primary_key = True )
132
- user_id : Mapped [str ] = mapped_column (String , primary_key = True )
133
- session_id : Mapped [str ] = mapped_column (String , primary_key = True )
141
+ id : Mapped [str ] = mapped_column (
142
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), primary_key = True
143
+ )
144
+ app_name : Mapped [str ] = mapped_column (
145
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), primary_key = True
146
+ )
147
+ user_id : Mapped [str ] = mapped_column (
148
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), primary_key = True
149
+ )
150
+ session_id : Mapped [str ] = mapped_column (
151
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), primary_key = True
152
+ )
134
153
135
- invocation_id : Mapped [str ] = mapped_column (String )
136
- author : Mapped [str ] = mapped_column (String )
137
- branch : Mapped [str ] = mapped_column (String , nullable = True )
154
+ invocation_id : Mapped [str ] = mapped_column (String (DEFAULT_MAX_VARCHAR_LENGTH ))
155
+ author : Mapped [str ] = mapped_column (String (DEFAULT_MAX_VARCHAR_LENGTH ))
156
+ branch : Mapped [str ] = mapped_column (
157
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), nullable = True
158
+ )
138
159
timestamp : Mapped [DateTime ] = mapped_column (DateTime (), default = func .now ())
139
160
content : Mapped [dict [str , Any ]] = mapped_column (DynamicJSON , nullable = True )
140
161
actions : Mapped [MutableDict [str , Any ]] = mapped_column (PickleType )
@@ -147,8 +168,10 @@ class StorageEvent(Base):
147
168
)
148
169
partial : Mapped [bool ] = mapped_column (Boolean , nullable = True )
149
170
turn_complete : Mapped [bool ] = mapped_column (Boolean , nullable = True )
150
- error_code : Mapped [str ] = mapped_column (String , nullable = True )
151
- error_message : Mapped [str ] = mapped_column (String , nullable = True )
171
+ error_code : Mapped [str ] = mapped_column (
172
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), nullable = True
173
+ )
174
+ error_message : Mapped [str ] = mapped_column (String (1024 ), nullable = True )
152
175
interrupted : Mapped [bool ] = mapped_column (Boolean , nullable = True )
153
176
154
177
storage_session : Mapped [StorageSession ] = relationship (
@@ -182,23 +205,33 @@ def long_running_tool_ids(self, value: set[str]):
182
205
183
206
class StorageAppState (Base ):
184
207
"""Represents an app state stored in the database."""
208
+
185
209
__tablename__ = "app_states"
186
210
187
- app_name : Mapped [str ] = mapped_column (String , primary_key = True )
211
+ app_name : Mapped [str ] = mapped_column (
212
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), primary_key = True
213
+ )
188
214
state : Mapped [MutableDict [str , Any ]] = mapped_column (
189
215
MutableDict .as_mutable (DynamicJSON ), default = {}
190
216
)
191
217
update_time : Mapped [DateTime ] = mapped_column (
192
218
DateTime (), default = func .now (), onupdate = func .now ()
193
219
)
194
220
195
-
196
221
class StorageUserState (Base ):
197
222
"""Represents a user state stored in the database."""
223
+
198
224
__tablename__ = "user_states"
199
225
200
- app_name : Mapped [s
67F4
tr ] = mapped_column (String , primary_key = True )
201
- user_id : Mapped [str ] = mapped_column (String , primary_key = True )
226
+ app_name : Mapped [str ] = mapped_column (
227
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), primary_key = True
228
+ )
229
+ user_id : Mapped [str ] = mapped_column (
230
+ String (DEFAULT_MAX_VARCHAR_LENGTH ), primary_key = True
231
+ )
232
+ state : Mapped [MutableDict [str , Any ]] = mapped_column (
233
+ MutableDict .as_mutable (DynamicJSON ), default = {}
234
+ )
202
235
state : Mapped [MutableDict [str , Any ]] = mapped_column (
203
236
MutableDict .as_mutable (DynamicJSON ), default = {}
204
237
)
0 commit comments