chore: import upstream snapshot with attribution
SDK Tests / changes (push) Successful in 2m29s
Real E2E Tests / changes (push) Successful in 2m29s
Deploy Docs Pages / build (push) Has been cancelled
Deploy Docs Pages / deploy (push) Has been cancelled
Real E2E Tests / JavaScript E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Python E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Java E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / C# E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Go E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Real E2E CI (push) Has been cancelled
SDK Tests / SDK CI (push) Has been cancelled
SDK Tests / CLI Tests (push) Has been cancelled
SDK Tests / Python SDK Quality (code-interpreter) (push) Has been cancelled
SDK Tests / Python SDK Quality (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (sandbox) (push) Has been cancelled
SDK Tests / CLI Quality (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Go SDK Quality And Tests (push) Has been cancelled
SDK Tests / changes (push) Successful in 2m29s
Real E2E Tests / changes (push) Successful in 2m29s
Deploy Docs Pages / build (push) Has been cancelled
Deploy Docs Pages / deploy (push) Has been cancelled
Real E2E Tests / JavaScript E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Python E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Java E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / C# E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Go E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Real E2E CI (push) Has been cancelled
SDK Tests / SDK CI (push) Has been cancelled
SDK Tests / CLI Tests (push) Has been cancelled
SDK Tests / Python SDK Quality (code-interpreter) (push) Has been cancelled
SDK Tests / Python SDK Quality (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (sandbox) (push) Has been cancelled
SDK Tests / CLI Quality (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Go SDK Quality And Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#
|
||||
# Copyright 2025 Alibaba Group Holding Ltd.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
import pytest
|
||||
from opensandbox.config import ConnectionConfig
|
||||
from opensandbox.models.sandboxes import SandboxEndpoint
|
||||
|
||||
from code_interpreter.adapters.code_adapter import CodesAdapter
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_code_service_eager_init_and_client_available() -> None:
|
||||
cfg = ConnectionConfig(protocol="http")
|
||||
endpoint = SandboxEndpoint(endpoint="localhost:44772", port=44772)
|
||||
adapter = CodesAdapter(endpoint, cfg)
|
||||
|
||||
client = await adapter._get_client()
|
||||
assert client is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_code_service_eager_init_merges_endpoint_headers() -> None:
|
||||
cfg = ConnectionConfig(
|
||||
protocol="http", headers={"X-Base": "base", "X-Shared": "base"}
|
||||
)
|
||||
endpoint = SandboxEndpoint(
|
||||
endpoint="localhost:44772",
|
||||
port=44772,
|
||||
headers={"X-Endpoint": "endpoint", "X-Shared": "endpoint"},
|
||||
)
|
||||
adapter = CodesAdapter(endpoint, cfg)
|
||||
|
||||
client = await adapter._get_client()
|
||||
httpx_client = client.get_async_httpx_client()
|
||||
|
||||
assert httpx_client.headers["X-Base"] == "base"
|
||||
assert httpx_client.headers["X-Endpoint"] == "endpoint"
|
||||
assert httpx_client.headers["X-Shared"] == "endpoint"
|
||||
@@ -0,0 +1,69 @@
|
||||
#
|
||||
# Copyright 2025 Alibaba Group Holding Ltd.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
import pytest
|
||||
from opensandbox.config import ConnectionConfig
|
||||
from opensandbox.exceptions import InvalidArgumentException
|
||||
from opensandbox.models.sandboxes import SandboxEndpoint
|
||||
|
||||
from code_interpreter import CodeInterpreter
|
||||
|
||||
|
||||
class _FakeSandbox:
|
||||
def __init__(self) -> None:
|
||||
self._id = str(__import__("uuid").uuid4())
|
||||
self.connection_config = ConnectionConfig(protocol="http")
|
||||
self.files = object()
|
||||
self.commands = object()
|
||||
self.metrics = object()
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
|
||||
async def get_endpoint(self, port: int) -> SandboxEndpoint:
|
||||
return SandboxEndpoint(endpoint="localhost:44772", port=port)
|
||||
|
||||
async def is_healthy(self) -> bool:
|
||||
return True
|
||||
|
||||
async def get_info(self): # pragma: no cover
|
||||
raise RuntimeError("not used")
|
||||
|
||||
async def get_metrics(self): # pragma: no cover
|
||||
raise RuntimeError("not used")
|
||||
|
||||
async def renew(self, timeout): # pragma: no cover
|
||||
raise RuntimeError("not used")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_requires_sandbox() -> None:
|
||||
with pytest.raises(InvalidArgumentException):
|
||||
await CodeInterpreter.create(sandbox=None) # type: ignore[arg-type]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_wires_code_service_and_delegates_properties() -> None:
|
||||
sbx = _FakeSandbox()
|
||||
ci = await CodeInterpreter.create(sandbox=sbx) # type: ignore[arg-type]
|
||||
|
||||
assert ci.id == sbx.id
|
||||
assert ci.files is sbx.files
|
||||
assert ci.commands is sbx.commands
|
||||
assert ci.metrics is sbx.metrics
|
||||
|
||||
# codes service should be present and callable (no network)
|
||||
assert ci.codes is not None
|
||||
@@ -0,0 +1,71 @@
|
||||
#
|
||||
# Copyright 2025 Alibaba Group Holding Ltd.
|
||||
#
|
||||
# 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 pytest
|
||||
from opensandbox.config import ConnectionConfig
|
||||
from opensandbox.models.sandboxes import SandboxEndpoint
|
||||
|
||||
from code_interpreter.adapters.code_adapter import CodesAdapter
|
||||
|
||||
|
||||
class _Resp:
|
||||
def __init__(self, *, status_code: int, parsed) -> None:
|
||||
self.status_code = status_code
|
||||
self.parsed = parsed
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_context_uses_openapi_and_converts(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from opensandbox.api.execd.models.code_context import CodeContext as ApiCodeContext
|
||||
|
||||
async def _fake_asyncio_detailed(*, client, body):
|
||||
assert body.language == "python"
|
||||
return _Resp(status_code=200, parsed=ApiCodeContext(language="python", id="ctx-1"))
|
||||
|
||||
monkeypatch.setattr(
|
||||
"opensandbox.api.execd.api.code_interpreting.create_code_context.asyncio_detailed",
|
||||
_fake_asyncio_detailed,
|
||||
)
|
||||
|
||||
adapter = CodesAdapter(
|
||||
SandboxEndpoint(endpoint="localhost:44772", port=44772),
|
||||
ConnectionConfig(protocol="http"),
|
||||
)
|
||||
ctx = await adapter.create_context("python")
|
||||
assert ctx.id == "ctx-1"
|
||||
assert ctx.language == "python"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_interrupt_calls_openapi(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
called = {"id": None}
|
||||
|
||||
async def _fake_asyncio_detailed(*, client, id):
|
||||
called["id"] = id
|
||||
return _Resp(status_code=204, parsed=None)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"opensandbox.api.execd.api.code_interpreting.interrupt_code.asyncio_detailed",
|
||||
_fake_asyncio_detailed,
|
||||
)
|
||||
|
||||
adapter = CodesAdapter(
|
||||
SandboxEndpoint(endpoint="localhost:44772", port=44772),
|
||||
ConnectionConfig(protocol="http"),
|
||||
)
|
||||
await adapter.interrupt("exec-1")
|
||||
assert called["id"] == "exec-1"
|
||||
@@ -0,0 +1,167 @@
|
||||
#
|
||||
# Copyright 2025 Alibaba Group Holding Ltd.
|
||||
#
|
||||
# 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 json
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from opensandbox.config import ConnectionConfig
|
||||
from opensandbox.exceptions import InvalidArgumentException, SandboxApiException
|
||||
from opensandbox.models.sandboxes import SandboxEndpoint
|
||||
|
||||
from code_interpreter.adapters.code_adapter import CodesAdapter
|
||||
from code_interpreter.adapters.converter.code_execution_converter import (
|
||||
CodeExecutionConverter,
|
||||
)
|
||||
from code_interpreter.models.code import CodeContext, SupportedLanguage
|
||||
|
||||
|
||||
class _SseTransport(httpx.AsyncBaseTransport):
|
||||
def __init__(self) -> None:
|
||||
self.last_request: httpx.Request | None = None
|
||||
|
||||
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
|
||||
self.last_request = request
|
||||
body = (
|
||||
request.content.decode("utf-8")
|
||||
if isinstance(request.content, (bytes, bytearray))
|
||||
else ""
|
||||
)
|
||||
payload = json.loads(body) if body else {}
|
||||
|
||||
if request.url.path == "/code" and payload.get("code") == "print(1)":
|
||||
sse = (
|
||||
b'data: {"type":"init","text":"exec-1","timestamp":1}\n\n'
|
||||
b'data: {"type":"stdout","text":"1\\n","timestamp":2}\n\n'
|
||||
b'data: {"type":"execution_complete","timestamp":3,"execution_time":7}\n\n'
|
||||
)
|
||||
return httpx.Response(
|
||||
200,
|
||||
headers={"Content-Type": "text/event-stream"},
|
||||
content=sse,
|
||||
request=request,
|
||||
)
|
||||
|
||||
if request.url.path == "/code" and payload.get("code") == "print(2)":
|
||||
assert payload["context"]["language"] == "go"
|
||||
sse = (
|
||||
b'data: {"type":"init","text":"exec-2","timestamp":1}\n\n'
|
||||
b'data: {"type":"stdout","text":"2\\n","timestamp":2}\n\n'
|
||||
b'data: {"type":"execution_complete","timestamp":3,"execution_time":7}\n\n'
|
||||
)
|
||||
return httpx.Response(
|
||||
200,
|
||||
headers={"Content-Type": "text/event-stream"},
|
||||
content=sse,
|
||||
request=request,
|
||||
)
|
||||
|
||||
return httpx.Response(
|
||||
400,
|
||||
headers={"x-request-id": "req-code-123"},
|
||||
content=b"bad",
|
||||
request=request,
|
||||
)
|
||||
|
||||
|
||||
def test_code_execution_converter_includes_context() -> None:
|
||||
ctx = CodeContext(id="c1", language=SupportedLanguage.PYTHON)
|
||||
d = CodeExecutionConverter.to_api_run_code_request("print(1)", ctx)
|
||||
assert d["code"] == "print(1)"
|
||||
assert d["context"]["id"] == "c1"
|
||||
assert d["context"]["language"] == "python"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_code_streaming_happy_path_updates_execution() -> None:
|
||||
cfg = ConnectionConfig(protocol="http", transport=_SseTransport())
|
||||
endpoint = SandboxEndpoint(endpoint="localhost:44772", port=44772)
|
||||
adapter = CodesAdapter(endpoint, cfg)
|
||||
|
||||
execution = await adapter.run("print(1)")
|
||||
assert execution.id == "exec-1"
|
||||
assert execution.logs.stdout[0].text == "1\n"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_code_streaming_merges_endpoint_headers() -> None:
|
||||
transport = _SseTransport()
|
||||
cfg = ConnectionConfig(
|
||||
protocol="http",
|
||||
transport=transport,
|
||||
headers={"X-Base": "base", "X-Shared": "base"},
|
||||
)
|
||||
endpoint = SandboxEndpoint(
|
||||
endpoint="localhost:44772",
|
||||
port=44772,
|
||||
headers={"X-Endpoint": "endpoint", "X-Shared": "endpoint"},
|
||||
)
|
||||
adapter = CodesAdapter(endpoint, cfg)
|
||||
|
||||
execution = await adapter.run("print(1)")
|
||||
|
||||
assert execution.id == "exec-1"
|
||||
assert transport.last_request is not None
|
||||
assert transport.last_request.headers["X-Base"] == "base"
|
||||
assert transport.last_request.headers["X-Endpoint"] == "endpoint"
|
||||
assert transport.last_request.headers["X-Shared"] == "endpoint"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_code_can_accept_language_string_without_context() -> None:
|
||||
cfg = ConnectionConfig(protocol="http", transport=_SseTransport())
|
||||
endpoint = SandboxEndpoint(endpoint="localhost:44772", port=44772)
|
||||
adapter = CodesAdapter(endpoint, cfg)
|
||||
|
||||
execution = await adapter.run("print(2)", language=SupportedLanguage.GO)
|
||||
assert execution.id == "exec-2"
|
||||
assert execution.logs.stdout[0].text == "2\n"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_code_rejects_blank_code() -> None:
|
||||
cfg = ConnectionConfig(protocol="http")
|
||||
endpoint = SandboxEndpoint(endpoint="localhost:44772", port=44772)
|
||||
adapter = CodesAdapter(endpoint, cfg)
|
||||
|
||||
with pytest.raises(InvalidArgumentException):
|
||||
await adapter.run(" ")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_code_rejects_mismatched_language_and_context() -> None:
|
||||
cfg = ConnectionConfig(protocol="http", transport=_SseTransport())
|
||||
endpoint = SandboxEndpoint(endpoint="localhost:44772", port=44772)
|
||||
adapter = CodesAdapter(endpoint, cfg)
|
||||
|
||||
with pytest.raises(InvalidArgumentException):
|
||||
await adapter.run(
|
||||
"print(1)",
|
||||
context=CodeContext(language=SupportedLanguage.PYTHON),
|
||||
language=SupportedLanguage.GO,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_code_non_200_raises_api_exception() -> None:
|
||||
cfg = ConnectionConfig(protocol="http", transport=_SseTransport())
|
||||
endpoint = SandboxEndpoint(endpoint="localhost:44772", port=44772)
|
||||
adapter = CodesAdapter(endpoint, cfg)
|
||||
|
||||
with pytest.raises(SandboxApiException) as ei:
|
||||
await adapter.run("other")
|
||||
assert ei.value.request_id == "req-code-123"
|
||||
@@ -0,0 +1,47 @@
|
||||
#
|
||||
# Copyright 2025 Alibaba Group Holding Ltd.
|
||||
#
|
||||
# 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 opensandbox.config.connection_sync import ConnectionConfigSync
|
||||
from opensandbox.models.sandboxes import SandboxEndpoint
|
||||
|
||||
from code_interpreter.sync.adapters.code_adapter import CodesAdapterSync
|
||||
|
||||
|
||||
def test_sync_adapter_merges_endpoint_headers_into_both_clients() -> None:
|
||||
cfg = ConnectionConfigSync(protocol="http", headers={"X-Base": "base"})
|
||||
endpoint = SandboxEndpoint(
|
||||
endpoint="localhost:44772",
|
||||
headers={"X-Endpoint": "endpoint"},
|
||||
)
|
||||
|
||||
adapter = CodesAdapterSync(endpoint, cfg)
|
||||
|
||||
assert adapter._httpx_client.headers["X-Base"] == "base"
|
||||
assert adapter._httpx_client.headers["X-Endpoint"] == "endpoint"
|
||||
assert adapter._sse_client.headers["X-Base"] == "base"
|
||||
assert adapter._sse_client.headers["X-Endpoint"] == "endpoint"
|
||||
|
||||
|
||||
def test_sync_adapter_endpoint_headers_override_connection_headers() -> None:
|
||||
cfg = ConnectionConfigSync(protocol="http", headers={"X-Shared": "base"})
|
||||
endpoint = SandboxEndpoint(
|
||||
endpoint="localhost:44772",
|
||||
headers={"X-Shared": "endpoint"},
|
||||
)
|
||||
|
||||
adapter = CodesAdapterSync(endpoint, cfg)
|
||||
|
||||
assert adapter._httpx_client.headers["X-Shared"] == "endpoint"
|
||||
assert adapter._sse_client.headers["X-Shared"] == "endpoint"
|
||||
Reference in New Issue
Block a user