Saved scripts¶
Users can save scripts to their account and load them back into the editor. Each script stores the code, its language and version, a name and an optional description, and belongs to the user who created it. Every repository query filters by user_id, so a user only ever sees their own scripts. Someone else's script id answers 404.
class SavedScriptResponse(BaseModel):
script_id: str
name: str
script: str
lang: str
lang_version: str
description: str | None = None
created_at: datetime
updated_at: datetime
model_config = ConfigDict(from_attributes=True, json_schema_serialization_defaults_required=True)
Runtime check¶
Create and update refuse a language version the runtime registry doesn't offer, with a 422 RuntimeNotSupportedError. An update that changes only the language or only the version is checked against the stored other half. Reads aren't checked, so a script saved on a version that has since been dropped still loads. Running it is what fails.
async def create_saved_script(
self,
*,
user_id: str,
name: str,
script: str,
lang: str,
lang_version: str,
description: str | None,
) -> SavedScriptDocument:
self.logger.info(
"Creating new saved script",
user_id=user_id,
script_name=name,
script_length=len(script),
)
self._ensure_runtime_offered(lang, lang_version)
created_script = SavedScriptDocument(
user_id=user_id,
name=name,
script=script,
lang=lang,
lang_version=lang_version,
description=description,
)
await self.saved_script_repo.create_saved_script(created_script)
self.logger.info(
"Successfully created saved script",
script_id=str(created_script.script_id),
user_id=user_id,
script_name=created_script.name,
)
return created_script
API¶
Storage¶
Scripts live in the saved_scripts collection, with a unique index on script_id and an index on user_id.
Frontend¶
The editor lists saved scripts in its options panel. Loading one sets the code, the language and the version. Save updates the loaded script, or creates a new one if nothing is loaded, the name was changed, or the update answers 404.
Key files¶
| File | Purpose |
|---|---|
saved_script_service.py |
Business logic and runtime check |
saved_scripts.py |
API routes |
saved_script.py |
Request and response models |
saved_script_repository.py |
MongoDB access |