[feat] change str id type to UUID
This commit is contained in:
parent
e629e34c4d
commit
770af1fe76
11 changed files with 150 additions and 173 deletions
|
|
@ -24,16 +24,13 @@ class DockerSandboxRuntime(SandboxRuntime):
|
|||
def create(
|
||||
self,
|
||||
*,
|
||||
session_id: str,
|
||||
chat_id: str,
|
||||
session_id: UUID,
|
||||
chat_id: UUID,
|
||||
created_at: datetime,
|
||||
expires_at: datetime,
|
||||
) -> SandboxSession:
|
||||
normalized_chat_id = chat_id
|
||||
|
||||
try:
|
||||
normalized_chat_id = _canonical_chat_id(chat_id)
|
||||
chat_path = self._chat_path(normalized_chat_id)
|
||||
chat_path = self._chat_path(chat_id)
|
||||
dependencies_path = self._readonly_host_path(
|
||||
self._config.dependencies_host_path
|
||||
)
|
||||
|
|
@ -44,19 +41,19 @@ class DockerSandboxRuntime(SandboxRuntime):
|
|||
container = self._client.containers.run(
|
||||
self._config.image,
|
||||
detach=True,
|
||||
labels=self._labels(session_id, normalized_chat_id, expires_at),
|
||||
labels=self._labels(session_id, chat_id, expires_at),
|
||||
mounts=self._mounts(chat_path, dependencies_path, lambda_tools_path),
|
||||
)
|
||||
except (DockerException, OSError, ValueError) as exc:
|
||||
raise SandboxStartError(normalized_chat_id) from exc
|
||||
raise SandboxStartError(str(chat_id)) from exc
|
||||
|
||||
container_id = str(getattr(container, 'id', '')).strip()
|
||||
if not container_id:
|
||||
raise SandboxStartError(normalized_chat_id)
|
||||
raise SandboxStartError(str(chat_id))
|
||||
|
||||
return SandboxSession(
|
||||
session_id=session_id,
|
||||
chat_id=normalized_chat_id,
|
||||
chat_id=chat_id,
|
||||
container_id=container_id,
|
||||
status=SandboxStatus.RUNNING,
|
||||
created_at=created_at,
|
||||
|
|
@ -74,13 +71,13 @@ class DockerSandboxRuntime(SandboxRuntime):
|
|||
|
||||
def _labels(
|
||||
self,
|
||||
session_id: str,
|
||||
chat_id: str,
|
||||
session_id: UUID,
|
||||
chat_id: UUID,
|
||||
expires_at: datetime,
|
||||
) -> dict[str, str]:
|
||||
return {
|
||||
'session_id': session_id,
|
||||
'chat_id': chat_id,
|
||||
'session_id': str(session_id),
|
||||
'chat_id': str(chat_id),
|
||||
'expires_at': expires_at.isoformat(),
|
||||
}
|
||||
|
||||
|
|
@ -110,12 +107,9 @@ class DockerSandboxRuntime(SandboxRuntime):
|
|||
),
|
||||
]
|
||||
|
||||
def _chat_path(self, chat_id: str) -> Path:
|
||||
if not chat_id.strip():
|
||||
raise ValueError('invalid chat path')
|
||||
|
||||
def _chat_path(self, chat_id: UUID) -> Path:
|
||||
chats_root = self._host_path(self._config.chats_root)
|
||||
chat_path = (chats_root / chat_id).resolve(strict=False)
|
||||
chat_path = (chats_root / str(chat_id)).resolve(strict=False)
|
||||
if not chat_path.is_relative_to(chats_root):
|
||||
raise ValueError('invalid chat path')
|
||||
return chat_path
|
||||
|
|
@ -128,7 +122,3 @@ class DockerSandboxRuntime(SandboxRuntime):
|
|||
|
||||
def _host_path(self, path_value: str) -> Path:
|
||||
return Path(path_value).expanduser().resolve(strict=False)
|
||||
|
||||
|
||||
def _canonical_chat_id(chat_id: str) -> str:
|
||||
return str(UUID(str(chat_id).strip()))
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
|
||||
class HealthResponse(BaseModel):
|
||||
|
|
@ -11,19 +11,14 @@ class HealthResponse(BaseModel):
|
|||
|
||||
|
||||
class CreateSandboxRequest(BaseModel):
|
||||
model_config = ConfigDict(extra='forbid', str_strip_whitespace=True)
|
||||
model_config = ConfigDict(extra='forbid')
|
||||
|
||||
chat_id: str = Field(min_length=1)
|
||||
|
||||
@field_validator('chat_id')
|
||||
@classmethod
|
||||
def validate_chat_id(cls, value: str) -> str:
|
||||
return str(UUID(value))
|
||||
chat_id: UUID
|
||||
|
||||
|
||||
class SandboxSessionResponse(BaseModel):
|
||||
session_id: str
|
||||
chat_id: str
|
||||
session_id: UUID
|
||||
chat_id: UUID
|
||||
container_id: str
|
||||
status: str
|
||||
expires_at: datetime
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue