Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ include NOTICE
include LICENSE.certifi
recursive-include examples *.md *.py
include requirements.txt
include dashscope/applications/agentstudio/sandbox/web-preview/preview.mjs
54 changes: 52 additions & 2 deletions dashscope/applications/agentstudio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ def _user_agent(base_url: str) -> str:
return f"dashscope-agentstudio-python/{__version__} (+{base_url})"


def _sandbox_timeout(value) -> float:
if isinstance(value, httpx.Timeout):
value = value.read
elif isinstance(value, tuple):
value = value[1]
return 30.0 if value is None else float(value)


class Client:
"""Synchronous AgentStudio client.

Expand Down Expand Up @@ -223,9 +231,30 @@ def __init__(
self.webhook_endpoints = WebhookEndpoints(self)
self.security = Security(self)
self.usage = Usage(self)
self._sandbox = None

@property
def sandbox(self):
"""Sandbox lifecycle and Harness Agent APIs, mounted on first use."""
if self._sandbox is None:
from .sandbox import SandboxClient

self._sandbox = SandboxClient(
base_url=self.transport.base_url,
api_key=self.transport.api_key,
timeout=_sandbox_timeout(self.transport.timeout),
# Share the parent HTTP pool without transferring ownership.
# pylint: disable-next=protected-access
http_client=self.transport._client,
)
return self._sandbox

def close(self) -> None:
self.transport.close()
try:
if self._sandbox is not None:
self._sandbox.close()
finally:
self.transport.close()

def __enter__(self) -> "Client":
return self
Expand Down Expand Up @@ -307,9 +336,30 @@ def __init__(
self.webhook_endpoints = AsyncWebhookEndpoints(self)
self.security = AsyncSecurity(self)
self.usage = AsyncUsage(self)
self._sandbox = None

@property
def sandbox(self):
"""Asynchronous Sandbox sub-client, sharing this client's HTTP pool."""
if self._sandbox is None:
from .sandbox import AsyncSandboxClient

self._sandbox = AsyncSandboxClient(
base_url=self.transport.base_url,
api_key=self.transport.api_key,
timeout=_sandbox_timeout(self.transport.timeout),
# Share the parent HTTP pool without transferring ownership.
# pylint: disable-next=protected-access
http_client=self.transport._client,
)
return self._sandbox

async def aclose(self) -> None:
await self.transport.aclose()
try:
if self._sandbox is not None:
await self._sandbox.aclose()
finally:
await self.transport.aclose()

async def __aenter__(self) -> "AsyncClient":
return self
Expand Down
113 changes: 113 additions & 0 deletions dashscope/applications/agentstudio/sandbox/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.
"""Sandbox lifecycle, Harness Agents, files and local native Web UI."""

from .agents import (
AgentHandle,
AgentsClient,
AsyncAgentHandle,
AsyncAgentsClient,
AsyncLaunchOperation,
AsyncOperationHandle,
AsyncSessionHandle,
AsyncTurnHandle,
LaunchOperation,
OperationHandle,
SessionHandle,
TurnHandle,
)
from .client import AsyncSandboxClient, SandboxClient
from .exceptions import SandboxAPIError, SandboxWaitTimeoutError
from .models import (
AgentInstanceInfo,
AgentOperationInfo,
AgentPage,
AgentSessionInfo,
AgentTemplateConfig,
AgentTurnInfo,
BuildStatus,
Compute,
ConnectionOptions,
FileMount,
LaunchRequest,
ModelConfig,
OfficialImage,
SandboxConnection,
SandboxCreateRequest,
SandboxInfo,
TemplateCreateRequest,
TemplateFile,
TemplateInfo,
TemplateMount,
TemplateUpdateRequest,
TurnResult,
UploadedFile,
UploadRequest,
UploadTicket,
)
from .sandboxes import (
AsyncSandboxHandle,
AsyncSandboxesClient,
SandboxHandle,
SandboxesClient,
)
from .streaming import AsyncEventSubscription, EventSubscription
from .template_files import AsyncTemplateFilesClient, TemplateFilesClient
from .templates import AsyncTemplatesClient, TemplatesClient
from .web_ui import SandboxWebUi, WebUiOptions

__all__ = [
"SandboxClient",
"AsyncSandboxClient",
"SandboxAPIError",
"SandboxWaitTimeoutError",
"OfficialImage",
"ModelConfig",
"LaunchRequest",
"Compute",
"FileMount",
"TemplateCreateRequest",
"TemplateUpdateRequest",
"SandboxCreateRequest",
"ConnectionOptions",
"SandboxConnection",
"SandboxInfo",
"TemplateInfo",
"TemplateMount",
"BuildStatus",
"AgentInstanceInfo",
"AgentOperationInfo",
"AgentPage",
"AgentSessionInfo",
"AgentTurnInfo",
"AgentTemplateConfig",
"TurnResult",
"UploadedFile",
"TemplateFile",
"UploadTicket",
"UploadRequest",
"AgentsClient",
"AsyncAgentsClient",
"AgentHandle",
"AsyncAgentHandle",
"SessionHandle",
"AsyncSessionHandle",
"TurnHandle",
"AsyncTurnHandle",
"OperationHandle",
"AsyncOperationHandle",
"LaunchOperation",
"AsyncLaunchOperation",
"EventSubscription",
"AsyncEventSubscription",
"SandboxesClient",
"AsyncSandboxesClient",
"SandboxHandle",
"AsyncSandboxHandle",
"TemplatesClient",
"AsyncTemplatesClient",
"TemplateFilesClient",
"AsyncTemplateFilesClient",
"SandboxWebUi",
"WebUiOptions",
]
Loading
Loading