rusticai-api 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,338 @@
1
+ from datetime import datetime, timezone
2
+ from enum import StrEnum
3
+ from typing import Dict, List, Optional
4
+
5
+ import shortuuid
6
+ from pydantic import BaseModel, JsonValue, computed_field
7
+ from sqlalchemy.ext.mutable import MutableDict
8
+ from sqlmodel import JSON, Column, Field, Relationship, SQLModel
9
+
10
+ from rustic_ai.core.guild.dsl import GuildSpec
11
+ from rustic_ai.core.guild.metaprog.agent_registry import AgentEntry
12
+
13
+
14
+ class BlueprintExposure(StrEnum):
15
+ PUBLIC = "public"
16
+ PRIVATE = "private"
17
+ ORGANIZATION = "organization"
18
+ SHARED = "shared"
19
+ ORGANIZATION_AND_CHILDREN = "organization_and_children" # NOTE: Not used, for future possible use
20
+
21
+
22
+ class BlueprintSharedWithOrganization(SQLModel, table=True):
23
+ organization_id: str = Field(primary_key=True)
24
+ blueprint_id: str = Field(primary_key=True, foreign_key="blueprint.id")
25
+
26
+
27
+ class BlueprintCommandBase(SQLModel):
28
+ blueprint_id: str = Field(foreign_key="blueprint.id")
29
+ command: str
30
+
31
+
32
+ class BlueprintCommandCreate(BlueprintCommandBase):
33
+ pass
34
+
35
+
36
+ class BlueprintCommand(BlueprintCommandBase, table=True):
37
+ __tablename__ = "blueprint_command"
38
+ id: str = Field(default_factory=shortuuid.uuid, primary_key=True)
39
+
40
+ blueprint: "Blueprint" = Relationship(back_populates="commands")
41
+
42
+
43
+ class BlueprintCommandResponse(BlueprintCommandBase):
44
+ id: str = Field(primary_key=True)
45
+ pass
46
+
47
+
48
+ class BlueprintStarterPromptBase(SQLModel):
49
+ blueprint_id: str = Field(foreign_key="blueprint.id")
50
+ prompt: str
51
+
52
+
53
+ class BlueprintStarterPromptCreate(BlueprintStarterPromptBase):
54
+ pass
55
+
56
+
57
+ class BlueprintStarterPrompt(BlueprintStarterPromptBase, table=True):
58
+ __tablename__ = "blueprint_starter_prompt"
59
+ id: str = Field(default_factory=shortuuid.uuid, primary_key=True)
60
+
61
+ blueprint: "Blueprint" = Relationship(back_populates="starter_prompts")
62
+
63
+
64
+ class BlueprintStarterPromptResponse(BlueprintStarterPromptBase):
65
+ id: str = Field(primary_key=True)
66
+ pass
67
+
68
+
69
+ class TagBase(SQLModel):
70
+ tag: str
71
+
72
+
73
+ class TagCreate(TagBase):
74
+ pass
75
+
76
+
77
+ class TagResponse(TagBase):
78
+ id: int
79
+
80
+
81
+ class BlueprintTag(SQLModel, table=True):
82
+ __tablename__ = "blueprint_tag"
83
+ tag_id: int = Field(foreign_key="tag.id", primary_key=True)
84
+ blueprint_id: str = Field(foreign_key="blueprint.id", primary_key=True)
85
+
86
+
87
+ class Tag(TagBase, table=True):
88
+ __tablename__ = "tag"
89
+ id: int = Field(default=None, primary_key=True)
90
+ tag: str = Field(index=True, unique=True)
91
+ created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
92
+ updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
93
+ blueprints: List["Blueprint"] = Relationship(back_populates="tags", link_model=BlueprintTag)
94
+
95
+
96
+ class BlueprintBase(SQLModel):
97
+ name: str = Field(index=True)
98
+ description: str = Field(index=True)
99
+ version: str
100
+ icon: Optional[str] = Field(default=None)
101
+ intro_msg: Optional[str] = Field(default=None)
102
+
103
+ exposure: BlueprintExposure = Field(default=BlueprintExposure.PRIVATE)
104
+
105
+ author_id: str
106
+ organization_id: Optional[str]
107
+ category_id: Optional[str] = Field(foreign_key="blueprint_category.id")
108
+
109
+
110
+ class BlueprintCreate(BlueprintBase):
111
+ spec: GuildSpec
112
+ tags: Optional[List[str]] = None
113
+ commands: Optional[List[str]] = None
114
+ starter_prompts: Optional[List[str]] = None
115
+ agent_icons: Optional[dict] = None
116
+
117
+
118
+ class BlueprintAgentLink(SQLModel, table=True):
119
+ __tablename__ = "blueprint_agent_link"
120
+
121
+ blueprint_id: str = Field(default=None, foreign_key="blueprint.id", primary_key=True)
122
+ qualified_class_name: str = Field(default=None, foreign_key="agent_entry.qualified_class_name", primary_key=True)
123
+
124
+
125
+ class Blueprint(BlueprintBase, table=True):
126
+ __tablename__ = "blueprint"
127
+
128
+ id: str = Field(default_factory=shortuuid.uuid, primary_key=True)
129
+ created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
130
+ updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
131
+ spec: Dict = Field(default_factory=dict, sa_column=Column(JSON))
132
+
133
+ author_id: str
134
+ organization_id: Optional[str]
135
+ category: Optional["BlueprintCategory"] = Relationship(back_populates="blueprints")
136
+
137
+ commands: List[BlueprintCommand] = Relationship(back_populates="blueprint")
138
+ starter_prompts: List[BlueprintStarterPrompt] = Relationship(back_populates="blueprint")
139
+ tags: List[Tag] = Relationship(back_populates="blueprints", link_model=BlueprintTag)
140
+
141
+ reviews: List["BlueprintReview"] = Relationship(back_populates="blueprint")
142
+ agents: List["CatalogAgentEntry"] = Relationship(back_populates="blueprints", link_model=BlueprintAgentLink)
143
+
144
+
145
+ class BlueprintInfoResponse(BaseModel):
146
+ id: str
147
+ name: str
148
+ description: str
149
+ version: str
150
+ exposure: BlueprintExposure
151
+ author_id: str
152
+ created_at: datetime
153
+ updated_at: datetime
154
+ icon: Optional[str]
155
+ organization_id: Optional[str]
156
+ category_id: Optional[str]
157
+ category_name: Optional[str]
158
+
159
+ @classmethod
160
+ def from_table(cls, blueprint: Blueprint) -> "BlueprintInfoResponse":
161
+ return cls(
162
+ id=blueprint.id,
163
+ name=blueprint.name,
164
+ description=blueprint.description,
165
+ version=blueprint.version,
166
+ exposure=blueprint.exposure,
167
+ author_id=blueprint.author_id,
168
+ created_at=blueprint.created_at,
169
+ updated_at=blueprint.updated_at,
170
+ icon=blueprint.icon,
171
+ organization_id=blueprint.organization_id,
172
+ category_id=blueprint.category.id if blueprint.category else None,
173
+ category_name=blueprint.category.name if blueprint.category else None,
174
+ )
175
+
176
+
177
+ class BlueprintDetailsResponse(BlueprintInfoResponse):
178
+ spec: Dict
179
+ tags: List[str]
180
+ commands: List[str]
181
+ starter_prompts: List[str]
182
+ intro_msg: Optional[str]
183
+
184
+ @classmethod
185
+ def from_table(cls, blueprint: Blueprint) -> "BlueprintDetailsResponse":
186
+ blueprint_info = BlueprintInfoResponse.from_table(blueprint)
187
+ return cls(
188
+ **blueprint_info.model_dump(),
189
+ intro_msg=blueprint.intro_msg,
190
+ spec=blueprint.spec,
191
+ tags=[tag.tag for tag in blueprint.tags],
192
+ starter_prompts=[sp.prompt for sp in blueprint.starter_prompts],
193
+ commands=[c.command for c in blueprint.commands],
194
+ )
195
+
196
+
197
+ class BlueprintResponseWithAccessReason(BlueprintInfoResponse):
198
+ access_organization_id: Optional[str] = Field(default=None)
199
+
200
+
201
+ class BlueprintCategoryBase(SQLModel):
202
+ name: str
203
+ description: str
204
+
205
+
206
+ class BlueprintCategoryCreate(BlueprintCategoryBase):
207
+ pass
208
+
209
+
210
+ class BlueprintCategory(BlueprintCategoryBase, table=True):
211
+ __tablename__ = "blueprint_category"
212
+ id: str = Field(default_factory=shortuuid.uuid, primary_key=True)
213
+ created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
214
+ updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
215
+
216
+ blueprints: List[Blueprint] = Relationship(back_populates="category")
217
+
218
+
219
+ class BlueprintCategoryResponse(BlueprintCategoryBase):
220
+ id: str = Field(primary_key=True)
221
+ pass
222
+
223
+
224
+ class BlueprintReviewBase(SQLModel):
225
+ user_id: str
226
+ rating: int
227
+ review: str = Field(default=None)
228
+
229
+
230
+ class BlueprintReviewCreate(BlueprintReviewBase):
231
+ pass
232
+
233
+
234
+ class BlueprintReview(BlueprintReviewBase, table=True):
235
+ __tablename__ = "blueprint_reviews"
236
+ blueprint_id: str = Field(foreign_key="blueprint.id")
237
+ id: str = Field(default_factory=shortuuid.uuid, primary_key=True)
238
+ created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
239
+ updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
240
+
241
+ user_id: str
242
+ blueprint: Blueprint = Relationship(back_populates="reviews")
243
+
244
+
245
+ class BlueprintReviewResponse(BlueprintReviewBase):
246
+ blueprint_id: str = Field(foreign_key="blueprint.id")
247
+ pass
248
+
249
+
250
+ class BlueprintReviewsResponse(SQLModel):
251
+ reviews: List[BlueprintReviewResponse]
252
+
253
+ @computed_field
254
+ def average_rating(self) -> float:
255
+ if not self.reviews:
256
+ return 0
257
+ rating = sum(review.rating for review in self.reviews) / len(self.reviews)
258
+ return rating
259
+
260
+ @computed_field
261
+ def total_reviews(self) -> int:
262
+ return len(self.reviews)
263
+
264
+
265
+ class DatabaseConflictError(Exception):
266
+ pass
267
+
268
+
269
+ class BlueprintGuild(SQLModel, table=True):
270
+ __tablename__ = "blueprint_guild"
271
+ guild_id: str = Field(foreign_key="guilds.id", primary_key=True, index=True)
272
+ blueprint_id: str = Field(foreign_key="blueprint.id", index=True)
273
+
274
+
275
+ class UserGuild(SQLModel, table=True):
276
+ __tablename__ = "user_guild"
277
+ guild_id: str = Field(foreign_key="guilds.id", primary_key=True, index=True)
278
+ user_id: str = Field(primary_key=True, index=True)
279
+
280
+
281
+ class OrganizationGuild(SQLModel, table=True):
282
+ __tablename__ = "organization_guild"
283
+ guild_id: str = Field(foreign_key="guilds.id", primary_key=True, index=True)
284
+ organization_id: str = Field(primary_key=True, index=True)
285
+
286
+
287
+ class BasicGuildInfo(SQLModel):
288
+ id: str
289
+ name: str
290
+ blueprint_id: Optional[str] = Field(default=None)
291
+ icon: Optional[str] = Field(default=None)
292
+
293
+
294
+ class AgentIcon(SQLModel, table=True):
295
+ __tablename__ = "agent_icon"
296
+ agent_class: str = Field(primary_key=True, index=True)
297
+ icon: str
298
+
299
+
300
+ class AgentNameWithIcon(SQLModel):
301
+ agent_name: str
302
+ icon: str
303
+
304
+
305
+ class BlueprintAgentIcon(AgentNameWithIcon, table=True):
306
+ __tablename__ = "blueprint_agent_icon"
307
+ blueprint_id: str = Field(foreign_key="blueprint.id", primary_key=True, index=True)
308
+ agent_name: str = Field(primary_key=True, index=True)
309
+ icon: str
310
+
311
+
312
+ class BlueprintAgentsIconReqRes(BaseModel):
313
+ agent_icons: list[AgentNameWithIcon]
314
+
315
+
316
+ class CatalogAgentEntry(SQLModel, table=True):
317
+ __tablename__ = "agent_entry"
318
+ agent_name: str = Field(..., description="The name of the agent.")
319
+ qualified_class_name: str = Field(..., primary_key=True, index=True)
320
+ agent_doc: Optional[str] = Field(default="")
321
+ agent_props_schema: Dict[str, JsonValue] = Field(sa_column=Column(JSON))
322
+ message_handlers: Dict[str, JsonValue] = Field(sa_column=Column(JSON))
323
+ agent_dependencies: dict = Field(
324
+ sa_column=Column(MutableDict.as_mutable(JSON(none_as_null=True)), default={}),
325
+ description="A dictionary containing agent dependencies.",
326
+ )
327
+ blueprints: List["Blueprint"] = Relationship(back_populates="agents", link_model=BlueprintAgentLink)
328
+
329
+ @classmethod
330
+ def from_base(cls, base: AgentEntry) -> "CatalogAgentEntry":
331
+ return cls(
332
+ agent_name=base.agent_name,
333
+ qualified_class_name=base.qualified_class_name,
334
+ agent_doc=base.agent_doc,
335
+ message_handlers={key: value.model_dump() for key, value in base.message_handlers.items()},
336
+ agent_props_schema=base.agent_props_schema,
337
+ agent_dependencies={dep.dependency_key: dep.model_dump() for dep in base.agent_dependencies},
338
+ )