Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

11.6 KB
Raw Blame
#
# Copyright 2026 The OpenSandbox Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from __future__ import annotations

import datetime
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeVar, cast

from attrs import define as _attrs_define
from attrs import field as _attrs_field
from dateutil.parser import isoparse

from ..models.session_state_profile import SessionStateProfile
from ..models.session_state_status import SessionStateStatus
from ..models.session_state_uid_mode import SessionStateUidMode
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.bind_mount import BindMount
    from ..models.env_passthrough_spec import EnvPassthroughSpec
    from ..models.isolated_overlay_spec import IsolatedOverlaySpec
    from ..models.isolated_workspace_spec import IsolatedWorkspaceSpec


T = TypeVar("T", bound="SessionState")


@_attrs_define
class SessionState:
    """State of an isolated session. Runtime status fields (status, created_at, last_run_at, idle_remaining_seconds) are
    always present. Creation-parameter fields (profile, workspace, overlays, binds, share_net, env_passthrough, uid,
    gid, uid_mode, extra_writable, idle_timeout_seconds) echo the parameters used to create the session and let a
    stateless client rebuild a session handle from just a session ID (e.g. after a client restart or in serverless
    workers). `overlays` lists the effective mounts; for sessions created via the legacy `workspace` field it repeats
    that workspace as a single element, and `workspace` itself is only echoed for such single-overlay sessions. Older
    execd builds may omit the creation-parameter fields; clients must tolerate them being absent.

        Attributes:
            status (SessionStateStatus | Unset):
            created_at (datetime.datetime | Unset):
            last_run_at (datetime.datetime | Unset):
            idle_remaining_seconds (int | None | Unset):
            profile (SessionStateProfile | Unset): Profile the session was created with.
            workspace (IsolatedWorkspaceSpec | Unset):
            overlays (list[IsolatedOverlaySpec] | Unset): Effective overlay mounts of the session.
            extra_writable (list[str] | Unset):
            binds (list[BindMount] | Unset):
            share_net (bool | Unset):
            env_passthrough (EnvPassthroughSpec | Unset):
            uid (int | Unset):
            gid (int | Unset):
            uid_mode (SessionStateUidMode | Unset):
            idle_timeout_seconds (int | Unset):
    """

    status: SessionStateStatus | Unset = UNSET
    created_at: datetime.datetime | Unset = UNSET
    last_run_at: datetime.datetime | Unset = UNSET
    idle_remaining_seconds: int | None | Unset = UNSET
    profile: SessionStateProfile | Unset = UNSET
    workspace: IsolatedWorkspaceSpec | Unset = UNSET
    overlays: list[IsolatedOverlaySpec] | Unset = UNSET
    extra_writable: list[str] | Unset = UNSET
    binds: list[BindMount] | Unset = UNSET
    share_net: bool | Unset = UNSET
    env_passthrough: EnvPassthroughSpec | Unset = UNSET
    uid: int | Unset = UNSET
    gid: int | Unset = UNSET
    uid_mode: SessionStateUidMode | Unset = UNSET
    idle_timeout_seconds: int | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        status: str | Unset = UNSET
        if not isinstance(self.status, Unset):
            status = self.status.value

        created_at: str | Unset = UNSET
        if not isinstance(self.created_at, Unset):
            created_at = self.created_at.isoformat()

        last_run_at: str | Unset = UNSET
        if not isinstance(self.last_run_at, Unset):
            last_run_at = self.last_run_at.isoformat()

        idle_remaining_seconds: int | None | Unset
        if isinstance(self.idle_remaining_seconds, Unset):
            idle_remaining_seconds = UNSET
        else:
            idle_remaining_seconds = self.idle_remaining_seconds

        profile: str | Unset = UNSET
        if not isinstance(self.profile, Unset):
            profile = self.profile.value

        workspace: dict[str, Any] | Unset = UNSET
        if not isinstance(self.workspace, Unset):
            workspace = self.workspace.to_dict()

        overlays: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.overlays, Unset):
            overlays = []
            for overlays_item_data in self.overlays:
                overlays_item = overlays_item_data.to_dict()
                overlays.append(overlays_item)

        extra_writable: list[str] | Unset = UNSET
        if not isinstance(self.extra_writable, Unset):
            extra_writable = self.extra_writable

        binds: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.binds, Unset):
            binds = []
            for binds_item_data in self.binds:
                binds_item = binds_item_data.to_dict()
                binds.append(binds_item)

        share_net = self.share_net

        env_passthrough: dict[str, Any] | Unset = UNSET
        if not isinstance(self.env_passthrough, Unset):
            env_passthrough = self.env_passthrough.to_dict()

        uid = self.uid

        gid = self.gid

        uid_mode: str | Unset = UNSET
        if not isinstance(self.uid_mode, Unset):
            uid_mode = self.uid_mode.value

        idle_timeout_seconds = self.idle_timeout_seconds

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if status is not UNSET:
            field_dict["status"] = status
        if created_at is not UNSET:
            field_dict["created_at"] = created_at
        if last_run_at is not UNSET:
            field_dict["last_run_at"] = last_run_at
        if idle_remaining_seconds is not UNSET:
            field_dict["idle_remaining_seconds"] = idle_remaining_seconds
        if profile is not UNSET:
            field_dict["profile"] = profile
        if workspace is not UNSET:
            field_dict["workspace"] = workspace
        if overlays is not UNSET:
            field_dict["overlays"] = overlays
        if extra_writable is not UNSET:
            field_dict["extra_writable"] = extra_writable
        if binds is not UNSET:
            field_dict["binds"] = binds
        if share_net is not UNSET:
            field_dict["share_net"] = share_net
        if env_passthrough is not UNSET:
            field_dict["env_passthrough"] = env_passthrough
        if uid is not UNSET:
            field_dict["uid"] = uid
        if gid is not UNSET:
            field_dict["gid"] = gid
        if uid_mode is not UNSET:
            field_dict["uid_mode"] = uid_mode
        if idle_timeout_seconds is not UNSET:
            field_dict["idle_timeout_seconds"] = idle_timeout_seconds

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.bind_mount import BindMount
        from ..models.env_passthrough_spec import EnvPassthroughSpec
        from ..models.isolated_overlay_spec import IsolatedOverlaySpec
        from ..models.isolated_workspace_spec import IsolatedWorkspaceSpec

        d = dict(src_dict)
        _status = d.pop("status", UNSET)
        status: SessionStateStatus | Unset
        if isinstance(_status, Unset):
            status = UNSET
        else:
            status = SessionStateStatus(_status)

        _created_at = d.pop("created_at", UNSET)
        created_at: datetime.datetime | Unset
        if isinstance(_created_at, Unset):
            created_at = UNSET
        else:
            created_at = isoparse(_created_at)

        _last_run_at = d.pop("last_run_at", UNSET)
        last_run_at: datetime.datetime | Unset
        if isinstance(_last_run_at, Unset):
            last_run_at = UNSET
        else:
            last_run_at = isoparse(_last_run_at)

        def _parse_idle_remaining_seconds(data: object) -> int | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(int | None | Unset, data)

        idle_remaining_seconds = _parse_idle_remaining_seconds(d.pop("idle_remaining_seconds", UNSET))

        _profile = d.pop("profile", UNSET)
        profile: SessionStateProfile | Unset
        if isinstance(_profile, Unset):
            profile = UNSET
        else:
            profile = SessionStateProfile(_profile)

        _workspace = d.pop("workspace", UNSET)
        workspace: IsolatedWorkspaceSpec | Unset
        if isinstance(_workspace, Unset):
            workspace = UNSET
        else:
            workspace = IsolatedWorkspaceSpec.from_dict(_workspace)

        _overlays = d.pop("overlays", UNSET)
        overlays: list[IsolatedOverlaySpec] | Unset = UNSET
        if _overlays is not UNSET:
            overlays = []
            for overlays_item_data in _overlays:
                overlays_item = IsolatedOverlaySpec.from_dict(overlays_item_data)

                overlays.append(overlays_item)

        extra_writable = cast(list[str], d.pop("extra_writable", UNSET))

        _binds = d.pop("binds", UNSET)
        binds: list[BindMount] | Unset = UNSET
        if _binds is not UNSET:
            binds = []
            for binds_item_data in _binds:
                binds_item = BindMount.from_dict(binds_item_data)

                binds.append(binds_item)

        share_net = d.pop("share_net", UNSET)

        _env_passthrough = d.pop("env_passthrough", UNSET)
        env_passthrough: EnvPassthroughSpec | Unset
        if isinstance(_env_passthrough, Unset):
            env_passthrough = UNSET
        else:
            env_passthrough = EnvPassthroughSpec.from_dict(_env_passthrough)

        uid = d.pop("uid", UNSET)

        gid = d.pop("gid", UNSET)

        _uid_mode = d.pop("uid_mode", UNSET)
        uid_mode: SessionStateUidMode | Unset
        if isinstance(_uid_mode, Unset):
            uid_mode = UNSET
        else:
            uid_mode = SessionStateUidMode(_uid_mode)

        idle_timeout_seconds = d.pop("idle_timeout_seconds", UNSET)

        session_state = cls(
            status=status,
            created_at=created_at,
            last_run_at=last_run_at,
            idle_remaining_seconds=idle_remaining_seconds,
            profile=profile,
            workspace=workspace,
            overlays=overlays,
            extra_writable=extra_writable,
            binds=binds,
            share_net=share_net,
            env_passthrough=env_passthrough,
            uid=uid,
            gid=gid,
            uid_mode=uid_mode,
            idle_timeout_seconds=idle_timeout_seconds,
        )

        session_state.additional_properties = d
        return session_state

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties