diff --git a/api/transformerlab/routers/auth.py b/api/transformerlab/routers/auth.py index 3dd47796b..468ea7c2d 100644 --- a/api/transformerlab/routers/auth.py +++ b/api/transformerlab/routers/auth.py @@ -205,6 +205,7 @@ async def get_user_teams(user: User = Depends(current_active_user), session: Asy user_teams = result.scalars().all() # If user has no team associations, create personal team as owner + # (dont seed experiment as existing user may already have experiments from old workspace) if not user_teams: personal_team = await create_personal_team(session, user) user_team = UserTeam(user_id=str(user.id), team_id=personal_team.id, role=TeamRole.OWNER.value) diff --git a/api/transformerlab/routers/teams.py b/api/transformerlab/routers/teams.py index 24a4617fa..e9651906c 100644 --- a/api/transformerlab/routers/teams.py +++ b/api/transformerlab/routers/teams.py @@ -11,6 +11,9 @@ from datetime import datetime, timedelta from os import getenv +from lab import Experiment +from lab.dirs import set_organization_id + class TeamCreate(BaseModel): name: str @@ -76,6 +79,23 @@ async def create_team( session.add(user_team) await session.commit() + # Create default experiment "alpha" for the new team + # Temporarily set the organization context to the new team ID + # so the experiment is created in the correct workspace + try: + # Set organization context to the new team ID + # The middleware will handle context for the next request + set_organization_id(team.id) + + # Create the default experiment + Experiment("alpha", create_new=True) + except Exception as e: + # Log error but don't fail team creation if experiment creation fails + print(f"Warning: Failed to create default experiment 'alpha' for team {team.id}: {e}") + finally: + # Clear the organization context (middleware will set it for next request) + set_organization_id(None) + return TeamResponse(id=team.id, name=team.name) diff --git a/api/transformerlab/services/experiment_init.py b/api/transformerlab/services/experiment_init.py index ff94f0be9..9192a50f0 100644 --- a/api/transformerlab/services/experiment_init.py +++ b/api/transformerlab/services/experiment_init.py @@ -225,8 +225,10 @@ async def migrate_workspace_to_org(team_id: str): # Directory not empty or other error, leave it pass - # make the directory again (because default sdk behaviour is to create this directory again when auth isnt dont -- which will happen at startup) - os.makedirs(old_workspace, exist_ok=True) + # Recreate workspace directory (default sdk behaviour is to create this directory again when auth isnt done -- which will happen at startup) + if not storage.exists(old_workspace): + storage.makedirs(old_workspace, exist_ok=True) + # Add a text file in the old workspace saying where the migration happened with open(os.path.join(old_workspace, "migration.txt"), "w") as f: f.write(f"Migration happened from {old_workspace} to {new_workspace}")