Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Refactor watch dir
Jakub Novakbc42ba1e66bf
diff --git a/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.py b/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyindex 4fe75374b..a8fad31d5 100644--- a/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.py+++ b/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.py@@ -14,7 +14,7 @@ _sym_db = _symbol_database.Default()DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(- b'\n\x1b\x66ilesystem/filesystem.proto\x12\nfilesystem"G\n\x0bMoveRequest\x12\x16\n\x06source\x18\x01 \x01(\tR\x06source\x12 \n\x0b\x64\x65stination\x18\x02 \x01(\tR\x0b\x64\x65stination";\n\x0cMoveResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry"$\n\x0eMakeDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path">\n\x0fMakeDirResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry"#\n\rRemoveRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path"\x10\n\x0eRemoveResponse"!\n\x0bStatRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path";\n\x0cStatResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry"]\n\tEntryInfo\x12\x12\n\x04name\x18\x01 \x01(\tR\x04name\x12(\n\x04type\x18\x02 \x01(\x0e\x32\x14.filesystem.FileTypeR\x04type\x12\x12\n\x04path\x18\x03 \x01(\tR\x04path"$\n\x0eListDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path"B\n\x0fListDirResponse\x12/\n\x07\x65ntries\x18\x01 \x03(\x0b\x32\x15.filesystem.EntryInfoR\x07\x65ntries"%\n\x0fWatchDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path"P\n\x0f\x46ilesystemEvent\x12\x12\n\x04name\x18\x01 \x01(\tR\x04name\x12)\n\x04type\x18\x02 \x01(\x0e\x32\x15.filesystem.EventTypeR\x04type"\xfe\x01\n\x10WatchDirResponse\x12?\n\x05start\x18\x01 \x01(\x0b\x32\'.filesystem.WatchDirResponse.StartEventH\x00R\x05start\x12=\n\nfilesystem\x18\x02 \x01(\x0b\x32\x1b.filesystem.FilesystemEventH\x00R\nfilesystem\x12\x46\n\tkeepalive\x18\x03 \x01(\x0b\x32&.filesystem.WatchDirResponse.KeepAliveH\x00R\tkeepalive\x1a\x0c\n\nStartEvent\x1a\x0b\n\tKeepAliveB\x07\n\x05\x65vent"5\n\x15WatchDirStartResponse\x12\x1c\n\twatcherId\x18\x01 \x01(\tR\twatcherId"K\n\x12WatchDirGetRequest\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId\x12\x16\n\x06offset\x18\x02 \x01(\rR\x06offset"J\n\x13WatchDirGetResponse\x12\x33\n\x06\x65vents\x18\x01 \x03(\x0b\x32\x1b.filesystem.FilesystemEventR\x06\x65vents"3\n\x13WatchDirStopRequest\x12\x1c\n\twatcherId\x18\x01 \x01(\tR\twatcherId"\x16\n\x14WatchDirStopResponse*R\n\x08\x46ileType\x12\x19\n\x15\x46ILE_TYPE_UNSPECIFIED\x10\x00\x12\x12\n\x0e\x46ILE_TYPE_FILE\x10\x01\x12\x17\n\x13\x46ILE_TYPE_DIRECTORY\x10\x02*\x98\x01\n\tEventType\x12\x1a\n\x16\x45VENT_TYPE_UNSPECIFIED\x10\x00\x12\x15\n\x11\x45VENT_TYPE_CREATE\x10\x01\x12\x14\n\x10\x45VENT_TYPE_WRITE\x10\x02\x12\x15\n\x11\x45VENT_TYPE_REMOVE\x10\x03\x12\x15\n\x11\x45VENT_TYPE_RENAME\x10\x04\x12\x14\n\x10\x45VENT_TYPE_CHMOD\x10\x05\x32\x88\x05\n\nFilesystem\x12\x39\n\x04Stat\x12\x17.filesystem.StatRequest\x1a\x18.filesystem.StatResponse\x12\x42\n\x07MakeDir\x12\x1a.filesystem.MakeDirRequest\x1a\x1b.filesystem.MakeDirResponse\x12\x39\n\x04Move\x12\x17.filesystem.MoveRequest\x1a\x18.filesystem.MoveResponse\x12\x42\n\x07ListDir\x12\x1a.filesystem.ListDirRequest\x1a\x1b.filesystem.ListDirResponse\x12?\n\x06Remove\x12\x19.filesystem.RemoveRequest\x1a\x1a.filesystem.RemoveResponse\x12G\n\x08WatchDir\x12\x1b.filesystem.WatchDirRequest\x1a\x1c.filesystem.WatchDirResponse0\x01\x12O\n\rWatchDirStart\x12\x1b.filesystem.WatchDirRequest\x1a!.filesystem.WatchDirStartResponse\x12N\n\x0bWatchDirGet\x12\x1e.filesystem.WatchDirGetRequest\x1a\x1f.filesystem.WatchDirGetResponse\x12Q\n\x0cWatchDirStop\x12\x1f.filesystem.WatchDirStopRequest\x1a .filesystem.WatchDirStopResponseBi\n\x0e\x63om.filesystemB\x0f\x46ilesystemProtoP\x01\xa2\x02\x03\x46XX\xaa\x02\nFilesystem\xca\x02\nFilesystem\xe2\x02\x16\x46ilesystem\\GPBMetadata\xea\x02\nFilesystemb\x06proto3'+ b'\n\x1b\x66ilesystem/filesystem.proto\x12\nfilesystem"G\n\x0bMoveRequest\x12\x16\n\x06source\x18\x01 \x01(\tR\x06source\x12 \n\x0b\x64\x65stination\x18\x02 \x01(\tR\x0b\x64\x65stination";\n\x0cMoveResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry"$\n\x0eMakeDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path">\n\x0fMakeDirResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry"#\n\rRemoveRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path"\x10\n\x0eRemoveResponse"!\n\x0bStatRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path";\n\x0cStatResponse\x12+\n\x05\x65ntry\x18\x01 \x01(\x0b\x32\x15.filesystem.EntryInfoR\x05\x65ntry"]\n\tEntryInfo\x12\x12\n\x04name\x18\x01 \x01(\tR\x04name\x12(\n\x04type\x18\x02 \x01(\x0e\x32\x14.filesystem.FileTypeR\x04type\x12\x12\n\x04path\x18\x03 \x01(\tR\x04path"$\n\x0eListDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path"B\n\x0fListDirResponse\x12/\n\x07\x65ntries\x18\x01 \x03(\x0b\x32\x15.filesystem.EntryInfoR\x07\x65ntries"%\n\x0fWatchDirRequest\x12\x12\n\x04path\x18\x01 \x01(\tR\x04path"P\n\x0f\x46ilesystemEvent\x12\x12\n\x04name\x18\x01 \x01(\tR\x04name\x12)\n\x04type\x18\x02 \x01(\x0e\x32\x15.filesystem.EventTypeR\x04type"\xfe\x01\n\x10WatchDirResponse\x12?\n\x05start\x18\x01 \x01(\x0b\x32\'.filesystem.WatchDirResponse.StartEventH\x00R\x05start\x12=\n\nfilesystem\x18\x02 \x01(\x0b\x32\x1b.filesystem.FilesystemEventH\x00R\nfilesystem\x12\x46\n\tkeepalive\x18\x03 \x01(\x0b\x32&.filesystem.WatchDirResponse.KeepAliveH\x00R\tkeepalive\x1a\x0c\n\nStartEvent\x1a\x0b\n\tKeepAliveB\x07\n\x05\x65vent"6\n\x15WatchDirStartResponse\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId"3\n\x12WatchDirGetRequest\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId"J\n\x13WatchDirGetResponse\x12\x33\n\x06\x65vents\x18\x01 \x03(\x0b\x32\x1b.filesystem.FilesystemEventR\x06\x65vents"4\n\x13WatchDirStopRequest\x12\x1d\n\nwatcher_id\x18\x01 \x01(\tR\twatcherId"\x16\n\x14WatchDirStopResponse*R\n\x08\x46ileType\x12\x19\n\x15\x46ILE_TYPE_UNSPECIFIED\x10\x00\x12\x12\n\x0e\x46ILE_TYPE_FILE\x10\x01\x12\x17\n\x13\x46ILE_TYPE_DIRECTORY\x10\x02*\x98\x01\n\tEventType\x12\x1a\n\x16\x45VENT_TYPE_UNSPECIFIED\x10\x00\x12\x15\n\x11\x45VENT_TYPE_CREATE\x10\x01\x12\x14\n\x10\x45VENT_TYPE_WRITE\x10\x02\x12\x15\n\x11\x45VENT_TYPE_REMOVE\x10\x03\x12\x15\n\x11\x45VENT_TYPE_RENAME\x10\x04\x12\x14\n\x10\x45VENT_TYPE_CHMOD\x10\x05\x32\x88\x05\n\nFilesystem\x12\x39\n\x04Stat\x12\x17.filesystem.StatRequest\x1a\x18.filesystem.StatResponse\x12\x42\n\x07MakeDir\x12\x1a.filesystem.MakeDirRequest\x1a\x1b.filesystem.MakeDirResponse\x12\x39\n\x04Move\x12\x17.filesystem.MoveRequest\x1a\x18.filesystem.MoveResponse\x12\x42\n\x07ListDir\x12\x1a.filesystem.ListDirRequest\x1a\x1b.filesystem.ListDirResponse\x12?\n\x06Remove\x12\x19.filesystem.RemoveRequest\x1a\x1a.filesystem.RemoveResponse\x12G\n\x08WatchDir\x12\x1b.filesystem.WatchDirRequest\x1a\x1c.filesystem.WatchDirResponse0\x01\x12O\n\rWatchDirStart\x12\x1b.filesystem.WatchDirRequest\x1a!.filesystem.WatchDirStartResponse\x12N\n\x0bWatchDirGet\x12\x1e.filesystem.WatchDirGetRequest\x1a\x1f.filesystem.WatchDirGetResponse\x12Q\n\x0cWatchDirStop\x12\x1f.filesystem.WatchDirStopRequest\x1a .filesystem.WatchDirStopResponseBi\n\x0e\x63om.filesystemB\x0f\x46ilesystemProtoP\x01\xa2\x02\x03\x46XX\xaa\x02\nFilesystem\xca\x02\nFilesystem\xe2\x02\x16\x46ilesystem\\GPBMetadata\xea\x02\nFilesystemb\x06proto3')_globals = globals()@@ -27,10 +27,10 @@ if not _descriptor._USE_C_DESCRIPTORS:_globals["DESCRIPTOR"]._serialized_options = (b"\n\016com.filesystemB\017FilesystemProtoP\001\242\002\003FXX\252\002\nFilesystem\312\002\nFilesystem\342\002\026Filesystem\\GPBMetadata\352\002\nFilesystem")- _globals["_FILETYPE"]._serialized_start = 1294- _globals["_FILETYPE"]._serialized_end = 1376- _globals["_EVENTTYPE"]._serialized_start = 1379- _globals["_EVENTTYPE"]._serialized_end = 1531+ _globals["_FILETYPE"]._serialized_start = 1272+ _globals["_FILETYPE"]._serialized_end = 1354+ _globals["_EVENTTYPE"]._serialized_start = 1357+ _globals["_EVENTTYPE"]._serialized_end = 1509_globals["_MOVEREQUEST"]._serialized_start = 43_globals["_MOVEREQUEST"]._serialized_end = 114_globals["_MOVERESPONSE"]._serialized_start = 116@@ -64,15 +64,15 @@ if not _descriptor._USE_C_DESCRIPTORS:_globals["_WATCHDIRRESPONSE_KEEPALIVE"]._serialized_start = 987_globals["_WATCHDIRRESPONSE_KEEPALIVE"]._serialized_end = 998_globals["_WATCHDIRSTARTRESPONSE"]._serialized_start = 1009- _globals["_WATCHDIRSTARTRESPONSE"]._serialized_end = 1062- _globals["_WATCHDIRGETREQUEST"]._serialized_start = 1064- _globals["_WATCHDIRGETREQUEST"]._serialized_end = 1139- _globals["_WATCHDIRGETRESPONSE"]._serialized_start = 1141- _globals["_WATCHDIRGETRESPONSE"]._serialized_end = 1215- _globals["_WATCHDIRSTOPREQUEST"]._serialized_start = 1217- _globals["_WATCHDIRSTOPREQUEST"]._serialized_end = 1268- _globals["_WATCHDIRSTOPRESPONSE"]._serialized_start = 1270- _globals["_WATCHDIRSTOPRESPONSE"]._serialized_end = 1292- _globals["_FILESYSTEM"]._serialized_start = 1534- _globals["_FILESYSTEM"]._serialized_end = 2182+ _globals["_WATCHDIRSTARTRESPONSE"]._serialized_end = 1063+ _globals["_WATCHDIRGETREQUEST"]._serialized_start = 1065+ _globals["_WATCHDIRGETREQUEST"]._serialized_end = 1116+ _globals["_WATCHDIRGETRESPONSE"]._serialized_start = 1118+ _globals["_WATCHDIRGETRESPONSE"]._serialized_end = 1192+ _globals["_WATCHDIRSTOPREQUEST"]._serialized_start = 1194+ _globals["_WATCHDIRSTOPREQUEST"]._serialized_end = 1246+ _globals["_WATCHDIRSTOPRESPONSE"]._serialized_start = 1248+ _globals["_WATCHDIRSTOPRESPONSE"]._serialized_end = 1270+ _globals["_FILESYSTEM"]._serialized_start = 1512+ _globals["_FILESYSTEM"]._serialized_end = 2160# @@protoc_insertion_point(module_scope)diff --git a/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyi b/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyiindex 043263a5a..d90860292 100644--- a/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyi+++ b/packages/python-sdk/e2b/envd/filesystem/filesystem_pb2.pyi@@ -156,20 +156,16 @@ class WatchDirResponse(_message.Message):) -> None: ...class WatchDirStartResponse(_message.Message):- __slots__ = ("watcherId",)- WATCHERID_FIELD_NUMBER: _ClassVar[int]- watcherId: str- def __init__(self, watcherId: _Optional[str] = ...) -> None: ...+ __slots__ = ("watcher_id",)+ WATCHER_ID_FIELD_NUMBER: _ClassVar[int]+ watcher_id: str+ def __init__(self, watcher_id: _Optional[str] = ...) -> None: ...class WatchDirGetRequest(_message.Message):- __slots__ = ("watcher_id", "offset")+ __slots__ = ("watcher_id",)WATCHER_ID_FIELD_NUMBER: _ClassVar[int]- OFFSET_FIELD_NUMBER: _ClassVar[int]watcher_id: str- offset: int- def __init__(- self, watcher_id: _Optional[str] = ..., offset: _Optional[int] = ...- ) -> None: ...+ def __init__(self, watcher_id: _Optional[str] = ...) -> None: ...class WatchDirGetResponse(_message.Message):__slots__ = ("events",)@@ -180,10 +176,10 @@ class WatchDirGetResponse(_message.Message):) -> None: ...class WatchDirStopRequest(_message.Message):- __slots__ = ("watcherId",)- WATCHERID_FIELD_NUMBER: _ClassVar[int]- watcherId: str- def __init__(self, watcherId: _Optional[str] = ...) -> None: ...+ __slots__ = ("watcher_id",)+ WATCHER_ID_FIELD_NUMBER: _ClassVar[int]+ watcher_id: str+ def __init__(self, watcher_id: _Optional[str] = ...) -> None: ...class WatchDirStopResponse(_message.Message):__slots__ = ()diff --git a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.pyindex 09010a794..7d62cb4d7 100644--- a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py+++ b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py@@ -330,4 +330,4 @@ class Filesystem:except Exception as e:raise handle_rpc_exception(e)- return WatchHandle(self._rpc, r.watcherId)+ return WatchHandle(self._rpc, r.watcher_id)diff --git a/packages/python-sdk/e2b/sandbox_sync/filesystem/watch_handle.py b/packages/python-sdk/e2b/sandbox_sync/filesystem/watch_handle.pyindex bc479b581..483272e5e 100644--- a/packages/python-sdk/e2b/sandbox_sync/filesystem/watch_handle.py+++ b/packages/python-sdk/e2b/sandbox_sync/filesystem/watch_handle.py@@ -26,27 +26,21 @@ class WatchHandle:Stop watching the directory. After you close the watcher you won't be able to get the events anymore."""try:- self._rpc.watch_dir_stop(WatchDirStopRequest(watcherId=self._watcher_id))+ self._rpc.watch_dir_stop(WatchDirStopRequest(watcher_id=self._watcher_id))except Exception as e:raise handle_rpc_exception(e)self._closed = True- def get(self, offset: int = 0) -> List[FilesystemEvent]:+ def get_new_events(self) -> List[FilesystemEvent]:"""- Get the events that occurred in the watched directory till now.- If you are calling it multiple times, you can pass the offset to get the new events only.+ Get the latest events that have occurred in the watched directory since the last call, or from the beginning of the watching, up to now."""if self._closed:raise SandboxException("The watcher is already closed")try:- r = self._rpc.watch_dir_get(- WatchDirGetRequest(- watcher_id=self._watcher_id,- offset=offset,- )- )+ r = self._rpc.watch_dir_get(WatchDirGetRequest(watcher_id=self._watcher_id))except Exception as e:raise handle_rpc_exception(e)diff --git a/packages/python-sdk/e2b/sandbox_sync/main.py b/packages/python-sdk/e2b/sandbox_sync/main.pyindex 9c6a231fb..6910d836a 100644--- a/packages/python-sdk/e2b/sandbox_sync/main.py+++ b/packages/python-sdk/e2b/sandbox_sync/main.py@@ -235,7 +235,8 @@ class Sandbox(SandboxSetup, SandboxApi):self.kill()@overload- def kill(self, request_timeout: Optional[float] = None) -> bool: ...+ def kill(self, request_timeout: Optional[float] = None) -> bool:+ ...@overload@staticmethod@@ -245,7 +246,8 @@ class Sandbox(SandboxSetup, SandboxApi):domain: Optional[str] = None,debug: Optional[bool] = None,request_timeout: Optional[float] = None,- ) -> bool: ...+ ) -> bool:+ ...@class_method_variant("_cls_kill")def kill(self, request_timeout: Optional[float] = None) -> bool: # type: ignore@@ -272,7 +274,9 @@ class Sandbox(SandboxSetup, SandboxApi):self,timeout: int,request_timeout: Optional[float] = None,- ) -> None: ...+ ) -> None:+ ...+@overload@staticmethoddef set_timeout(@@ -282,7 +286,9 @@ class Sandbox(SandboxSetup, SandboxApi):domain: Optional[str] = None,debug: Optional[bool] = None,request_timeout: Optional[float] = None,- ) -> None: ...+ ) -> None:+ ...+@class_method_variant("_cls_set_timeout")def set_timeout( # type: ignoreself,diff --git a/packages/python-sdk/e2b/sandbox_sync/process/process.py b/packages/python-sdk/e2b/sandbox_sync/process/process.pyindex 41d591660..a20d14d8f 100644--- a/packages/python-sdk/e2b/sandbox_sync/process/process.py+++ b/packages/python-sdk/e2b/sandbox_sync/process/process.py@@ -132,7 +132,8 @@ class Process:on_stderr: Optional[Callable[[str], None]] = None,timeout: Optional[float] = 60,request_timeout: Optional[float] = None,- ) -> ProcessResult: ...+ ) -> ProcessResult:+ ...@overloaddef run(@@ -146,7 +147,8 @@ class Process:on_stderr: None = None,timeout: Optional[float] = 60,request_timeout: Optional[float] = None,- ) -> ProcessHandle: ...+ ) -> ProcessHandle:+ ...def run(self,diff --git a/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py b/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.pyindex 72bc684ba..484931112 100644--- a/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py+++ b/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py@@ -7,16 +7,38 @@ def test_watch_directory_changes(sandbox: Sandbox):dirname = "test_watch_dir"filename = "test_watch.txt"content = "This file will be watched."- new_content = "This file has been modified."sandbox.files.make_dir(dirname)+ handle = sandbox.files.watch(dirname)sandbox.files.write(f"{dirname}/{filename}", content)+ events = handle.get_new_events()+ assert len(events) == 3+ assert events[0].type == FilesystemEventType.CREATE+ assert events[0].name == filename+ assert events[1].type == FilesystemEventType.CHMOD+ assert events[1].name == filename+ assert events[2].type == FilesystemEventType.WRITE+ assert events[2].name == filename++ handle.stop()+++def test_watch_iterated(sandbox: Sandbox):+ dirname = "test_watch_dir"+ filename = "test_watch.txt"+ content = "This file will be watched."+ new_content = "This file has been modified."++ sandbox.files.make_dir(dirname)handle = sandbox.files.watch(dirname)+ sandbox.files.write(f"{dirname}/{filename}", content)- sandbox.files.write(f"{dirname}/{filename}", new_content)+ events = handle.get_new_events()+ assert len(events) == 3- events = handle.get()+ sandbox.files.write(f"{dirname}/{filename}", new_content)+ events = handle.get_new_events()for event in events:if event.type == FilesystemEventType.WRITE and event.name == filename:breakdiff --git a/spec/envd/filesystem/filesystem.proto b/spec/envd/filesystem/filesystem.protoindex 83a072555..873f77b4c 100644--- a/spec/envd/filesystem/filesystem.proto+++ b/spec/envd/filesystem/filesystem.proto@@ -90,12 +90,11 @@ message WatchDirResponse {}message WatchDirStartResponse {- string watcherId = 1;+ string watcher_id = 1;}message WatchDirGetRequest {string watcher_id = 1;- uint32 offset = 2;}message WatchDirGetResponse {@@ -103,7 +102,7 @@ message WatchDirGetResponse {}message WatchDirStopRequest {- string watcherId = 1;+ string watcher_id = 1;}message WatchDirStopResponse {}