loopsandbox 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,137 @@
1
+ """LoopSandbox Python SDK.
2
+
3
+ Covers the HTTP API of LoopSandbox: sandboxes, command executions, background
4
+ processes, files, directory watches, code sessions (``run_code`` shares state across
5
+ calls), browser control, fork, pause, volumes, templates and API keys. Zero runtime
6
+ dependencies (stdlib urllib). The HTTP transport is injectable for testing.
7
+ """
8
+ from __future__ import annotations
9
+
10
+ from .client import LoopSandbox, SandboxPage, Session
11
+ from .client import Sandbox as SandboxHandle
12
+ from .errors import (
13
+ AuthenticationException,
14
+ CommandExitException,
15
+ ConflictError,
16
+ FileNotFoundException,
17
+ ForbiddenError,
18
+ InvalidArgumentException,
19
+ LoopSandboxError,
20
+ NotFoundError,
21
+ NotFoundException,
22
+ OperationTimeoutError,
23
+ PayloadTooLargeError,
24
+ QuotaExceededError,
25
+ RateLimitError,
26
+ RateLimitException,
27
+ SandboxException,
28
+ SandboxNotFoundException,
29
+ ServerError,
30
+ StreamError,
31
+ TimeoutException,
32
+ UnauthorizedError,
33
+ UnsupportedError,
34
+ ValidationError,
35
+ )
36
+ from .models import (
37
+ ApiKey,
38
+ ArtifactUrl,
39
+ Capabilities,
40
+ Capability,
41
+ CellResult,
42
+ ExecResult,
43
+ FileEntry,
44
+ FileEvent,
45
+ FileEvents,
46
+ FileInfo,
47
+ LogEntry,
48
+ NetworkDenial,
49
+ NetworkPolicy,
50
+ Process,
51
+ ProcessOutput,
52
+ SandboxInfo,
53
+ SandboxLogs,
54
+ SandboxMetrics,
55
+ SignedFileUrl,
56
+ StreamEvent,
57
+ Template,
58
+ Secret,
59
+ Snapshot,
60
+ Volume,
61
+ Watch,
62
+ )
63
+ from .sandbox import (
64
+ ALL_TRAFFIC,
65
+ CommandHandle,
66
+ CommandResult,
67
+ EntryInfo,
68
+ FileType,
69
+ Sandbox,
70
+ SandboxPaginator,
71
+ WriteInfo,
72
+ )
73
+ from .transport import Response, StreamResponse, StreamTransport, Transport
74
+
75
+ __all__ = [
76
+ "LoopSandbox",
77
+ "Sandbox",
78
+ "SandboxHandle",
79
+ "SandboxPaginator",
80
+ "ALL_TRAFFIC",
81
+ "CommandHandle",
82
+ "CommandResult",
83
+ "EntryInfo",
84
+ "FileType",
85
+ "WriteInfo",
86
+ "SandboxException",
87
+ "AuthenticationException",
88
+ "InvalidArgumentException",
89
+ "NotFoundException",
90
+ "SandboxNotFoundException",
91
+ "FileNotFoundException",
92
+ "CommandExitException",
93
+ "TimeoutException",
94
+ "RateLimitException",
95
+ "SandboxPage",
96
+ "Session",
97
+ "CellResult",
98
+ "ExecResult",
99
+ "StreamEvent",
100
+ "SandboxInfo",
101
+ "FileEntry",
102
+ "FileInfo",
103
+ "SignedFileUrl",
104
+ "ArtifactUrl",
105
+ "Process",
106
+ "ProcessOutput",
107
+ "LogEntry",
108
+ "SandboxLogs",
109
+ "SandboxMetrics",
110
+ "Watch",
111
+ "FileEvent",
112
+ "FileEvents",
113
+ "Secret",
114
+ "Snapshot",
115
+ "Volume",
116
+ "ApiKey",
117
+ "Capability",
118
+ "Capabilities",
119
+ "Template",
120
+ "LoopSandboxError",
121
+ "ValidationError",
122
+ "UnauthorizedError",
123
+ "QuotaExceededError",
124
+ "ForbiddenError",
125
+ "NotFoundError",
126
+ "ConflictError",
127
+ "PayloadTooLargeError",
128
+ "RateLimitError",
129
+ "UnsupportedError",
130
+ "OperationTimeoutError",
131
+ "ServerError",
132
+ "StreamError",
133
+ "Transport",
134
+ "StreamTransport",
135
+ "Response",
136
+ "StreamResponse",
137
+ ]
@@ -0,0 +1,124 @@
1
+ """Agent-framework adapters (M20 agent-native DX): expose a LoopSandbox sandbox as tools for
2
+ popular agent frameworks so an agent can run code / commands / file ops in an isolated sandbox
3
+ with a one-liner. Two frameworks are supported: OpenAI function/tool-calling and LangChain.
4
+
5
+ The adapters are thin and dependency-light: the OpenAI adapter is pure-stdlib (returns the tool
6
+ JSON schema + a dispatcher); the LangChain adapter imports langchain lazily so this module never
7
+ forces langchain as a dependency.
8
+ """
9
+ from __future__ import annotations
10
+
11
+ from typing import Any, Callable
12
+
13
+ from . import Sandbox
14
+
15
+
16
+ # --- OpenAI tool-calling adapter -------------------------------------------------------------
17
+
18
+ def openai_tools() -> list[dict]:
19
+ """Return the OpenAI `tools` schema for the sandbox verbs (run_code, run, write_file,
20
+ read_file). Pass these to the Chat Completions / Responses API `tools=` parameter."""
21
+ return [
22
+ {
23
+ "type": "function",
24
+ "function": {
25
+ "name": "sandbox_run_code",
26
+ "description": "Run Python code in the sandbox; state is shared across calls.",
27
+ "parameters": {
28
+ "type": "object",
29
+ "properties": {"code": {"type": "string"}},
30
+ "required": ["code"],
31
+ },
32
+ },
33
+ },
34
+ {
35
+ "type": "function",
36
+ "function": {
37
+ "name": "sandbox_run",
38
+ "description": "Run a shell-free command (argv) in the sandbox.",
39
+ "parameters": {
40
+ "type": "object",
41
+ "properties": {"cmd": {"type": "array", "items": {"type": "string"}}},
42
+ "required": ["cmd"],
43
+ },
44
+ },
45
+ },
46
+ {
47
+ "type": "function",
48
+ "function": {
49
+ "name": "sandbox_write_file",
50
+ "description": "Write a text file under /workspace in the sandbox.",
51
+ "parameters": {
52
+ "type": "object",
53
+ "properties": {"path": {"type": "string"}, "content": {"type": "string"}},
54
+ "required": ["path", "content"],
55
+ },
56
+ },
57
+ },
58
+ {
59
+ "type": "function",
60
+ "function": {
61
+ "name": "sandbox_read_file",
62
+ "description": "Read a text file under /workspace in the sandbox.",
63
+ "parameters": {
64
+ "type": "object",
65
+ "properties": {"path": {"type": "string"}},
66
+ "required": ["path"],
67
+ },
68
+ },
69
+ },
70
+ ]
71
+
72
+
73
+ def openai_dispatch(sandbox: Sandbox, name: str, args: dict[str, Any]) -> str:
74
+ """Execute one OpenAI tool call against `sandbox` and return a string result to feed back to
75
+ the model as the tool message. Raises ValueError for an unknown tool name (fail-fast)."""
76
+ if name == "sandbox_run_code":
77
+ r = sandbox.run_code(args["code"])
78
+ return r.stdout if r.exit_code == 0 else f"error (exit {r.exit_code}): {r.stderr}"
79
+ if name == "sandbox_run":
80
+ r = sandbox.run(list(args["cmd"]))
81
+ return r.stdout if r.exit_code == 0 else f"error (exit {r.exit_code}): {r.stderr}"
82
+ if name == "sandbox_write_file":
83
+ sandbox.write_file(args["path"], args["content"].encode())
84
+ return "ok"
85
+ if name == "sandbox_read_file":
86
+ return sandbox.read_file(args["path"]).decode(errors="replace")
87
+ raise ValueError(f"unknown sandbox tool: {name}")
88
+
89
+
90
+ # --- LangChain adapter ------------------------------------------------------------------------
91
+
92
+ def langchain_tools(sandbox: Sandbox) -> list:
93
+ """Return LangChain StructuredTools bound to `sandbox`. langchain is imported lazily so this
94
+ module has no hard langchain dependency; a clear ImportError is raised if it is missing."""
95
+ try:
96
+ from langchain_core.tools import StructuredTool
97
+ except ImportError as e: # pragma: no cover - exercised only without langchain installed
98
+ raise ImportError(
99
+ "langchain_tools requires langchain-core; install it with `pip install langchain-core`"
100
+ ) from e
101
+
102
+ def _run_code(code: str) -> str:
103
+ return openai_dispatch(sandbox, "sandbox_run_code", {"code": code})
104
+
105
+ def _run(cmd: list[str]) -> str:
106
+ return openai_dispatch(sandbox, "sandbox_run", {"cmd": cmd})
107
+
108
+ return [
109
+ StructuredTool.from_function(_run_code, name="sandbox_run_code",
110
+ description="Run Python code in an isolated sandbox (state shared)."),
111
+ StructuredTool.from_function(_run, name="sandbox_run",
112
+ description="Run a shell-free command (argv) in an isolated sandbox."),
113
+ ]
114
+
115
+
116
+ # A generic callable-map adapter for any framework that takes {name: fn}.
117
+ def tool_functions(sandbox: Sandbox) -> dict[str, Callable[..., str]]:
118
+ """Return a {tool_name: callable} map for frameworks that accept plain callables."""
119
+ return {
120
+ "sandbox_run_code": lambda code: openai_dispatch(sandbox, "sandbox_run_code", {"code": code}),
121
+ "sandbox_run": lambda cmd: openai_dispatch(sandbox, "sandbox_run", {"cmd": cmd}),
122
+ "sandbox_write_file": lambda path, content: openai_dispatch(sandbox, "sandbox_write_file", {"path": path, "content": content}),
123
+ "sandbox_read_file": lambda path: openai_dispatch(sandbox, "sandbox_read_file", {"path": path}),
124
+ }