langchain-pilldrops 0.1.0__tar.gz
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.
- langchain_pilldrops-0.1.0/.gitignore +6 -0
- langchain_pilldrops-0.1.0/PKG-INFO +66 -0
- langchain_pilldrops-0.1.0/README.md +54 -0
- langchain_pilldrops-0.1.0/langchain_pilldrops/__init__.py +5 -0
- langchain_pilldrops-0.1.0/langchain_pilldrops/sandbox.py +119 -0
- langchain_pilldrops-0.1.0/pyproject.toml +19 -0
- langchain_pilldrops-0.1.0/tests/test_sandbox.py +63 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: langchain-pilldrops
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: PillDrops sandbox integration for Deep Agents
|
|
5
|
+
Project-URL: Homepage, https://pilldrops.com
|
|
6
|
+
Project-URL: Repository, https://github.com/pilldrops-oss/pilldrops-integrations
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Requires-Python: <4.0,>=3.11
|
|
9
|
+
Requires-Dist: deepagents<0.8.0,>=0.7.0
|
|
10
|
+
Requires-Dist: pilldrops>=1.6.0
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# langchain-pilldrops
|
|
14
|
+
|
|
15
|
+
[PillDrops](https://pilldrops.com) sandbox integration for [Deep Agents](https://docs.langchain.com/oss/python/deepagents/overview).
|
|
16
|
+
The agent's shell commands and file tools run in a PillDrops cloud sandbox instead of on
|
|
17
|
+
your machine.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install langchain-pilldrops
|
|
23
|
+
export PILLDROPS_API_KEY=sf_live_... # from https://pilldrops.com/dashboard
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Use
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from deepagents import create_deep_agent
|
|
30
|
+
from pilldrops import Sandbox
|
|
31
|
+
|
|
32
|
+
from langchain_pilldrops import PillDropsSandbox
|
|
33
|
+
|
|
34
|
+
sandbox = Sandbox.create(template="code-interpreter", timeout=1800)
|
|
35
|
+
try:
|
|
36
|
+
agent = create_deep_agent(
|
|
37
|
+
model="anthropic:claude-opus-5-5",
|
|
38
|
+
backend=PillDropsSandbox(sandbox=sandbox),
|
|
39
|
+
)
|
|
40
|
+
result = agent.invoke({"messages": [{"role": "user", "content": "Write fib.py, run it for n=30 and tell me the result"}]})
|
|
41
|
+
print(result["messages"][-1].content)
|
|
42
|
+
finally:
|
|
43
|
+
sandbox.kill()
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The backend on its own:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
backend = PillDropsSandbox(sandbox=sandbox, timeout=300)
|
|
50
|
+
print(backend.execute("python3 --version").output)
|
|
51
|
+
backend.upload_files([("/tmp/data.csv", b"a,b\n1,2\n")])
|
|
52
|
+
print(backend.download_files(["/tmp/data.csv"])[0].content)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Notes
|
|
56
|
+
|
|
57
|
+
- Any template works: `code-interpreter` has Python with the data stack, `node` has Node.js,
|
|
58
|
+
`browser` has Chromium. See [templates](https://pilldrops.com/templates).
|
|
59
|
+
- `timeout` is the default for each command, in seconds (30 minutes unless set). The sandbox's
|
|
60
|
+
own lifetime is set on `Sandbox.create(timeout=...)`.
|
|
61
|
+
- Uploads and downloads work for any absolute path. A download is limited to about 7 MB; use
|
|
62
|
+
`sandbox.download()` from the PillDrops SDK for larger files.
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
MIT
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# langchain-pilldrops
|
|
2
|
+
|
|
3
|
+
[PillDrops](https://pilldrops.com) sandbox integration for [Deep Agents](https://docs.langchain.com/oss/python/deepagents/overview).
|
|
4
|
+
The agent's shell commands and file tools run in a PillDrops cloud sandbox instead of on
|
|
5
|
+
your machine.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install langchain-pilldrops
|
|
11
|
+
export PILLDROPS_API_KEY=sf_live_... # from https://pilldrops.com/dashboard
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Use
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from deepagents import create_deep_agent
|
|
18
|
+
from pilldrops import Sandbox
|
|
19
|
+
|
|
20
|
+
from langchain_pilldrops import PillDropsSandbox
|
|
21
|
+
|
|
22
|
+
sandbox = Sandbox.create(template="code-interpreter", timeout=1800)
|
|
23
|
+
try:
|
|
24
|
+
agent = create_deep_agent(
|
|
25
|
+
model="anthropic:claude-opus-5-5",
|
|
26
|
+
backend=PillDropsSandbox(sandbox=sandbox),
|
|
27
|
+
)
|
|
28
|
+
result = agent.invoke({"messages": [{"role": "user", "content": "Write fib.py, run it for n=30 and tell me the result"}]})
|
|
29
|
+
print(result["messages"][-1].content)
|
|
30
|
+
finally:
|
|
31
|
+
sandbox.kill()
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The backend on its own:
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
backend = PillDropsSandbox(sandbox=sandbox, timeout=300)
|
|
38
|
+
print(backend.execute("python3 --version").output)
|
|
39
|
+
backend.upload_files([("/tmp/data.csv", b"a,b\n1,2\n")])
|
|
40
|
+
print(backend.download_files(["/tmp/data.csv"])[0].content)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Notes
|
|
44
|
+
|
|
45
|
+
- Any template works: `code-interpreter` has Python with the data stack, `node` has Node.js,
|
|
46
|
+
`browser` has Chromium. See [templates](https://pilldrops.com/templates).
|
|
47
|
+
- `timeout` is the default for each command, in seconds (30 minutes unless set). The sandbox's
|
|
48
|
+
own lifetime is set on `Sandbox.create(timeout=...)`.
|
|
49
|
+
- Uploads and downloads work for any absolute path. A download is limited to about 7 MB; use
|
|
50
|
+
`sandbox.download()` from the PillDrops SDK for larger files.
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""PillDrops sandbox backend for Deep Agents."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import posixpath
|
|
7
|
+
import shlex
|
|
8
|
+
import uuid
|
|
9
|
+
|
|
10
|
+
from deepagents.backends.protocol import (
|
|
11
|
+
ExecuteResponse,
|
|
12
|
+
FileDownloadResponse,
|
|
13
|
+
FileUploadResponse,
|
|
14
|
+
)
|
|
15
|
+
from deepagents.backends.sandbox import BaseSandbox
|
|
16
|
+
from pilldrops import Sandbox
|
|
17
|
+
|
|
18
|
+
# Exit codes of the shell checks in upload_files and download_files.
|
|
19
|
+
_NOT_FOUND = 3
|
|
20
|
+
_IS_DIRECTORY = 4
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class PillDropsSandbox(BaseSandbox):
|
|
24
|
+
"""Deep Agents backend that runs commands and file operations in a PillDrops sandbox.
|
|
25
|
+
|
|
26
|
+
File operations inherited from `BaseSandbox` run through `execute()`. Uploads and
|
|
27
|
+
downloads go through the sandbox too, so they work for any absolute path.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, *, sandbox: Sandbox, timeout: int = 30 * 60) -> None:
|
|
31
|
+
"""Wrap an existing sandbox.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
sandbox: A sandbox from `pilldrops.Sandbox.create()` or `Sandbox.connect()`.
|
|
35
|
+
timeout: Default command timeout in seconds when `execute()` gets none.
|
|
36
|
+
"""
|
|
37
|
+
self._sandbox = sandbox
|
|
38
|
+
self._default_timeout = timeout
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def id(self) -> str:
|
|
42
|
+
"""Return the PillDrops sandbox id."""
|
|
43
|
+
return self._sandbox.id
|
|
44
|
+
|
|
45
|
+
def execute(self, command: str, *, timeout: int | None = None) -> ExecuteResponse:
|
|
46
|
+
"""Run a shell command in the sandbox and return its combined output."""
|
|
47
|
+
result = self._sandbox.run(command, timeout=timeout or self._default_timeout)
|
|
48
|
+
output = result.stdout
|
|
49
|
+
if result.stderr.strip():
|
|
50
|
+
output += f"\n<stderr>{result.stderr.strip()}</stderr>"
|
|
51
|
+
return ExecuteResponse(output=output, exit_code=result.exit_code, truncated=result.truncated)
|
|
52
|
+
|
|
53
|
+
def upload_files(self, files: list[tuple[str, bytes]]) -> list[FileUploadResponse]:
|
|
54
|
+
"""Write files into the sandbox, creating parent directories."""
|
|
55
|
+
responses = []
|
|
56
|
+
for path, content in files:
|
|
57
|
+
if not path.startswith("/"):
|
|
58
|
+
responses.append(FileUploadResponse(path=path, error="invalid_path"))
|
|
59
|
+
continue
|
|
60
|
+
# The files API is rooted at the sandbox home, so stage there and move into place.
|
|
61
|
+
staged = f".pilldrops-upload-{uuid.uuid4().hex}"
|
|
62
|
+
try:
|
|
63
|
+
self._sandbox.files.write(staged, content)
|
|
64
|
+
quoted = shlex.quote(path)
|
|
65
|
+
moved = self._sandbox.run(
|
|
66
|
+
f"test -d {quoted} && exit {_IS_DIRECTORY}; "
|
|
67
|
+
f"mkdir -p {shlex.quote(posixpath.dirname(path))} && mv {staged} {quoted}"
|
|
68
|
+
)
|
|
69
|
+
except Exception as err: # noqa: BLE001 - reported per file, as the protocol asks
|
|
70
|
+
responses.append(FileUploadResponse(path=path, error=str(err)))
|
|
71
|
+
continue
|
|
72
|
+
if moved.exit_code == 0:
|
|
73
|
+
responses.append(FileUploadResponse(path=path, error=None))
|
|
74
|
+
else:
|
|
75
|
+
self._sandbox.run(f"rm -f {staged}")
|
|
76
|
+
responses.append(FileUploadResponse(path=path, error=_upload_error(moved)))
|
|
77
|
+
return responses
|
|
78
|
+
|
|
79
|
+
def download_files(self, paths: list[str]) -> list[FileDownloadResponse]:
|
|
80
|
+
"""Read files from the sandbox."""
|
|
81
|
+
responses = []
|
|
82
|
+
for path in paths:
|
|
83
|
+
if not path.startswith("/"):
|
|
84
|
+
responses.append(FileDownloadResponse(path=path, content=None, error="invalid_path"))
|
|
85
|
+
continue
|
|
86
|
+
quoted = shlex.quote(path)
|
|
87
|
+
try:
|
|
88
|
+
result = self._sandbox.run(
|
|
89
|
+
f"test -e {quoted} || exit {_NOT_FOUND}; test -d {quoted} && exit {_IS_DIRECTORY}; "
|
|
90
|
+
f"base64 < {quoted}"
|
|
91
|
+
)
|
|
92
|
+
except Exception as err: # noqa: BLE001 - reported per file, as the protocol asks
|
|
93
|
+
responses.append(FileDownloadResponse(path=path, content=None, error=str(err)))
|
|
94
|
+
continue
|
|
95
|
+
if result.exit_code == 0 and not result.truncated:
|
|
96
|
+
responses.append(FileDownloadResponse(path=path, content=base64.b64decode(result.stdout), error=None))
|
|
97
|
+
else:
|
|
98
|
+
responses.append(FileDownloadResponse(path=path, content=None, error=_download_error(result)))
|
|
99
|
+
return responses
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _upload_error(result) -> str:
|
|
103
|
+
if result.exit_code == _IS_DIRECTORY:
|
|
104
|
+
return "is_directory"
|
|
105
|
+
if "Permission denied" in result.stderr:
|
|
106
|
+
return "permission_denied"
|
|
107
|
+
return result.stderr.strip() or "upload failed"
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _download_error(result) -> str:
|
|
111
|
+
if result.exit_code == _NOT_FOUND:
|
|
112
|
+
return "file_not_found"
|
|
113
|
+
if result.exit_code == _IS_DIRECTORY:
|
|
114
|
+
return "is_directory"
|
|
115
|
+
if "Permission denied" in result.stderr:
|
|
116
|
+
return "permission_denied"
|
|
117
|
+
if result.truncated:
|
|
118
|
+
return "file too large to download through exec (about 7 MB limit)"
|
|
119
|
+
return result.stderr.strip() or "download failed"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "langchain-pilldrops"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "PillDrops sandbox integration for Deep Agents"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.11,<4.0"
|
|
12
|
+
dependencies = ["deepagents>=0.7.0,<0.8.0", "pilldrops>=1.6.0"]
|
|
13
|
+
|
|
14
|
+
[project.urls]
|
|
15
|
+
Homepage = "https://pilldrops.com"
|
|
16
|
+
Repository = "https://github.com/pilldrops-oss/pilldrops-integrations"
|
|
17
|
+
|
|
18
|
+
[tool.hatch.build.targets.wheel]
|
|
19
|
+
packages = ["langchain_pilldrops"]
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""PillDropsSandbox against a stand-in sandbox that runs commands in a local directory."""
|
|
2
|
+
import subprocess
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
from pilldrops import ExecResult
|
|
6
|
+
|
|
7
|
+
from langchain_pilldrops import PillDropsSandbox
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class _LocalFiles:
|
|
11
|
+
def __init__(self, home):
|
|
12
|
+
self._home = home
|
|
13
|
+
|
|
14
|
+
def write(self, path, content):
|
|
15
|
+
(self._home / path).write_bytes(content)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class _LocalSandbox:
|
|
19
|
+
id = "sb-local"
|
|
20
|
+
|
|
21
|
+
def __init__(self, home):
|
|
22
|
+
self._home = home
|
|
23
|
+
self.files = _LocalFiles(home)
|
|
24
|
+
|
|
25
|
+
def run(self, cmd, cwd="", timeout=60, env=None):
|
|
26
|
+
p = subprocess.run(["sh", "-c", cmd], cwd=self._home, capture_output=True, text=True, timeout=timeout)
|
|
27
|
+
return ExecResult(p.stdout, p.stderr, p.returncode, False)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.fixture
|
|
31
|
+
def backend(tmp_path):
|
|
32
|
+
home = tmp_path / "home"
|
|
33
|
+
home.mkdir()
|
|
34
|
+
return PillDropsSandbox(sandbox=_LocalSandbox(home))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_execute_combines_stdout_and_stderr(backend):
|
|
38
|
+
r = backend.execute("echo out; echo err >&2; exit 3")
|
|
39
|
+
assert r.output == "out\n\n<stderr>err</stderr>"
|
|
40
|
+
assert r.exit_code == 3
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_upload_then_download_round_trips_bytes_anywhere(backend, tmp_path):
|
|
44
|
+
target = str(tmp_path / "outside" / "nested" / "data.bin")
|
|
45
|
+
blob = bytes(range(256)) * 50
|
|
46
|
+
assert backend.upload_files([(target, blob)])[0].error is None
|
|
47
|
+
assert backend.download_files([target])[0].content == blob
|
|
48
|
+
assert list((tmp_path / "home").iterdir()) == [] # nothing staged is left behind
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_errors_are_reported_per_file(backend, tmp_path):
|
|
52
|
+
missing, folder = str(tmp_path / "missing.txt"), str(tmp_path)
|
|
53
|
+
got = backend.download_files(["relative.txt", missing, folder])
|
|
54
|
+
assert [r.error for r in got] == ["invalid_path", "file_not_found", "is_directory"]
|
|
55
|
+
assert backend.upload_files([("relative.txt", b"x")])[0].error == "invalid_path"
|
|
56
|
+
assert backend.upload_files([(folder, b"x")])[0].error == "is_directory"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_inherited_file_tools_work(backend, tmp_path):
|
|
60
|
+
path = str(tmp_path / "notes.txt")
|
|
61
|
+
assert backend.write(path, "alpha\nbeta\n").error is None
|
|
62
|
+
assert backend.edit(path, "beta", "gamma").error is None
|
|
63
|
+
assert "gamma" in backend.read(path).file_data["content"]
|