cowork-dash 0.1.6__py3-none-any.whl → 0.1.7__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,468 @@
1
+ """Session-ephemeral virtual filesystem for multi-user isolation.
2
+
3
+ Provides an in-memory filesystem that isolates files, canvas, and uploads
4
+ between different user sessions. Each session gets its own virtual workspace
5
+ that is automatically cleaned up when the session ends.
6
+ """
7
+
8
+ import threading
9
+ import time
10
+ import uuid
11
+ from datetime import datetime
12
+ from pathlib import PurePosixPath
13
+ from typing import Any, Dict, Iterator, List, Optional, Union
14
+
15
+
16
+ class VirtualPath:
17
+ """Path-like object for virtual filesystem paths.
18
+
19
+ Provides a subset of pathlib.Path interface for compatibility
20
+ with existing code that uses Path objects.
21
+ """
22
+
23
+ def __init__(self, path: str, fs: "VirtualFilesystem"):
24
+ self._path = PurePosixPath(path)
25
+ self._fs = fs
26
+
27
+ def __str__(self) -> str:
28
+ return str(self._path)
29
+
30
+ def __repr__(self) -> str:
31
+ return f"VirtualPath({str(self._path)!r})"
32
+
33
+ def __truediv__(self, other: Union[str, "VirtualPath"]) -> "VirtualPath":
34
+ if isinstance(other, VirtualPath):
35
+ other = str(other._path)
36
+ return VirtualPath(str(self._path / other), self._fs)
37
+
38
+ def __eq__(self, other: Any) -> bool:
39
+ if isinstance(other, VirtualPath):
40
+ return str(self._path) == str(other._path)
41
+ if isinstance(other, str):
42
+ return str(self._path) == other
43
+ return False
44
+
45
+ def __hash__(self) -> int:
46
+ return hash(str(self._path))
47
+
48
+ @property
49
+ def name(self) -> str:
50
+ return self._path.name
51
+
52
+ @property
53
+ def stem(self) -> str:
54
+ return self._path.stem
55
+
56
+ @property
57
+ def suffix(self) -> str:
58
+ return self._path.suffix
59
+
60
+ @property
61
+ def parent(self) -> "VirtualPath":
62
+ return VirtualPath(str(self._path.parent), self._fs)
63
+
64
+ @property
65
+ def parts(self) -> tuple:
66
+ return self._path.parts
67
+
68
+ def resolve(self) -> "VirtualPath":
69
+ """Return the path with no .. or . components."""
70
+ # Normalize the path
71
+ parts = []
72
+ for part in self._path.parts:
73
+ if part == "..":
74
+ if parts and parts[-1] != "/":
75
+ parts.pop()
76
+ elif part != ".":
77
+ parts.append(part)
78
+ if not parts:
79
+ parts = ["/"]
80
+ return VirtualPath("/".join(parts) if parts[0] != "/" else "/" + "/".join(parts[1:]), self._fs)
81
+
82
+ def exists(self) -> bool:
83
+ return self._fs.exists(str(self._path))
84
+
85
+ def is_file(self) -> bool:
86
+ return self._fs.is_file(str(self._path))
87
+
88
+ def is_dir(self) -> bool:
89
+ return self._fs.is_dir(str(self._path))
90
+
91
+ def mkdir(self, parents: bool = False, exist_ok: bool = False) -> None:
92
+ self._fs.mkdir(str(self._path), parents=parents, exist_ok=exist_ok)
93
+
94
+ def read_text(self, encoding: str = "utf-8") -> str:
95
+ return self._fs.read_text(str(self._path), encoding=encoding)
96
+
97
+ def read_bytes(self) -> bytes:
98
+ return self._fs.read_bytes(str(self._path))
99
+
100
+ def write_text(self, data: str, encoding: str = "utf-8") -> int:
101
+ return self._fs.write_text(str(self._path), data, encoding=encoding)
102
+
103
+ def write_bytes(self, data: bytes) -> int:
104
+ return self._fs.write_bytes(str(self._path), data)
105
+
106
+ def unlink(self, missing_ok: bool = False) -> None:
107
+ self._fs.unlink(str(self._path), missing_ok=missing_ok)
108
+
109
+ def rmdir(self) -> None:
110
+ self._fs.rmdir(str(self._path))
111
+
112
+ def iterdir(self) -> Iterator["VirtualPath"]:
113
+ for name in self._fs.listdir(str(self._path)):
114
+ yield self / name
115
+
116
+ def glob(self, pattern: str) -> Iterator["VirtualPath"]:
117
+ """Simple glob implementation for virtual filesystem."""
118
+ for path in self._fs.glob(str(self._path), pattern):
119
+ yield VirtualPath(path, self._fs)
120
+
121
+ def relative_to(self, other: Union[str, "VirtualPath"]) -> "VirtualPath":
122
+ if isinstance(other, VirtualPath):
123
+ other = str(other._path)
124
+ return VirtualPath(str(self._path.relative_to(other)), self._fs)
125
+
126
+
127
+ class VirtualFilesystem:
128
+ """In-memory filesystem for session isolation.
129
+
130
+ Stores files as a flat dictionary mapping paths to content.
131
+ Directories are tracked implicitly by path prefixes.
132
+ """
133
+
134
+ def __init__(self, root: str = "/"):
135
+ self._root = root.rstrip("/") or "/"
136
+ self._files: Dict[str, bytes] = {}
137
+ self._directories: set = {self._root}
138
+ self._lock = threading.Lock()
139
+ self._created_at = datetime.now()
140
+ self._last_accessed = datetime.now()
141
+
142
+ def _normalize_path(self, path: str) -> str:
143
+ """Normalize path to absolute form within the virtual filesystem."""
144
+ if not path.startswith("/"):
145
+ path = f"{self._root}/{path}"
146
+ # Remove trailing slashes except for root
147
+ path = path.rstrip("/") or "/"
148
+ # Resolve . and ..
149
+ parts = []
150
+ for part in path.split("/"):
151
+ if part == "..":
152
+ if parts:
153
+ parts.pop()
154
+ elif part and part != ".":
155
+ parts.append(part)
156
+ return "/" + "/".join(parts)
157
+
158
+ def _touch_access(self) -> None:
159
+ """Update last accessed time."""
160
+ self._last_accessed = datetime.now()
161
+
162
+ @property
163
+ def root(self) -> VirtualPath:
164
+ """Get the root path as a VirtualPath object."""
165
+ return VirtualPath(self._root, self)
166
+
167
+ def path(self, p: str) -> VirtualPath:
168
+ """Create a VirtualPath for the given path string."""
169
+ return VirtualPath(p, self)
170
+
171
+ def exists(self, path: str) -> bool:
172
+ """Check if path exists (file or directory)."""
173
+ self._touch_access()
174
+ norm_path = self._normalize_path(path)
175
+ with self._lock:
176
+ if norm_path in self._files:
177
+ return True
178
+ if norm_path in self._directories:
179
+ return True
180
+ return False
181
+
182
+ def is_file(self, path: str) -> bool:
183
+ """Check if path is a file."""
184
+ self._touch_access()
185
+ norm_path = self._normalize_path(path)
186
+ with self._lock:
187
+ return norm_path in self._files
188
+
189
+ def is_dir(self, path: str) -> bool:
190
+ """Check if path is a directory."""
191
+ self._touch_access()
192
+ norm_path = self._normalize_path(path)
193
+ with self._lock:
194
+ return norm_path in self._directories
195
+
196
+ def mkdir(self, path: str, parents: bool = False, exist_ok: bool = False) -> None:
197
+ """Create a directory."""
198
+ self._touch_access()
199
+ norm_path = self._normalize_path(path)
200
+
201
+ with self._lock:
202
+ if norm_path in self._files:
203
+ raise FileExistsError(f"File exists: {path}")
204
+
205
+ if norm_path in self._directories:
206
+ if exist_ok:
207
+ return
208
+ raise FileExistsError(f"Directory exists: {path}")
209
+
210
+ # Check parent exists
211
+ parent = "/".join(norm_path.split("/")[:-1]) or "/"
212
+ if parent not in self._directories:
213
+ if parents:
214
+ # Create parent directories recursively
215
+ parts = norm_path.split("/")[1:] # Skip leading empty string
216
+ current = ""
217
+ for part in parts:
218
+ current = f"{current}/{part}"
219
+ self._directories.add(current)
220
+ else:
221
+ raise FileNotFoundError(f"Parent directory does not exist: {parent}")
222
+ else:
223
+ self._directories.add(norm_path)
224
+
225
+ def read_bytes(self, path: str) -> bytes:
226
+ """Read file as bytes."""
227
+ self._touch_access()
228
+ norm_path = self._normalize_path(path)
229
+ with self._lock:
230
+ if norm_path not in self._files:
231
+ raise FileNotFoundError(f"File not found: {path}")
232
+ return self._files[norm_path]
233
+
234
+ def read_text(self, path: str, encoding: str = "utf-8") -> str:
235
+ """Read file as text."""
236
+ return self.read_bytes(path).decode(encoding)
237
+
238
+ def write_bytes(self, path: str, data: bytes) -> int:
239
+ """Write bytes to file."""
240
+ self._touch_access()
241
+ norm_path = self._normalize_path(path)
242
+
243
+ with self._lock:
244
+ # Check parent directory exists
245
+ parent = "/".join(norm_path.split("/")[:-1]) or "/"
246
+ if parent not in self._directories:
247
+ raise FileNotFoundError(f"Parent directory does not exist: {parent}")
248
+
249
+ self._files[norm_path] = data
250
+ return len(data)
251
+
252
+ def write_text(self, path: str, data: str, encoding: str = "utf-8") -> int:
253
+ """Write text to file."""
254
+ return self.write_bytes(path, data.encode(encoding))
255
+
256
+ def unlink(self, path: str, missing_ok: bool = False) -> None:
257
+ """Delete a file."""
258
+ self._touch_access()
259
+ norm_path = self._normalize_path(path)
260
+
261
+ with self._lock:
262
+ if norm_path not in self._files:
263
+ if missing_ok:
264
+ return
265
+ raise FileNotFoundError(f"File not found: {path}")
266
+ del self._files[norm_path]
267
+
268
+ def rmdir(self, path: str) -> None:
269
+ """Remove a directory (must be empty)."""
270
+ self._touch_access()
271
+ norm_path = self._normalize_path(path)
272
+
273
+ with self._lock:
274
+ if norm_path not in self._directories:
275
+ raise FileNotFoundError(f"Directory not found: {path}")
276
+
277
+ # Check if directory is empty
278
+ prefix = norm_path + "/"
279
+ for p in self._files:
280
+ if p.startswith(prefix):
281
+ raise OSError(f"Directory not empty: {path}")
282
+ for p in self._directories:
283
+ if p.startswith(prefix):
284
+ raise OSError(f"Directory not empty: {path}")
285
+
286
+ self._directories.remove(norm_path)
287
+
288
+ def listdir(self, path: str) -> List[str]:
289
+ """List contents of a directory."""
290
+ self._touch_access()
291
+ norm_path = self._normalize_path(path)
292
+
293
+ with self._lock:
294
+ if norm_path not in self._directories:
295
+ raise FileNotFoundError(f"Directory not found: {path}")
296
+
297
+ prefix = norm_path + "/" if norm_path != "/" else "/"
298
+ prefix_len = len(prefix)
299
+
300
+ items = set()
301
+
302
+ # Find files in this directory
303
+ for p in self._files:
304
+ if p.startswith(prefix):
305
+ remainder = p[prefix_len:]
306
+ if "/" not in remainder:
307
+ items.add(remainder)
308
+
309
+ # Find subdirectories
310
+ for p in self._directories:
311
+ if p.startswith(prefix) and p != norm_path:
312
+ remainder = p[prefix_len:]
313
+ if "/" not in remainder:
314
+ items.add(remainder)
315
+
316
+ return sorted(items)
317
+
318
+ def glob(self, path: str, pattern: str) -> List[str]:
319
+ """Simple glob matching within a directory."""
320
+ import fnmatch
321
+
322
+ self._touch_access()
323
+ norm_path = self._normalize_path(path)
324
+
325
+ results = []
326
+
327
+ with self._lock:
328
+ prefix = norm_path + "/" if norm_path != "/" else "/"
329
+ prefix_len = len(prefix)
330
+
331
+ # Check all files
332
+ for p in self._files:
333
+ if p.startswith(prefix):
334
+ remainder = p[prefix_len:]
335
+ if fnmatch.fnmatch(remainder, pattern):
336
+ results.append(p)
337
+
338
+ # Check directories
339
+ for p in self._directories:
340
+ if p.startswith(prefix) and p != norm_path:
341
+ remainder = p[prefix_len:]
342
+ if fnmatch.fnmatch(remainder, pattern):
343
+ results.append(p)
344
+
345
+ return sorted(results)
346
+
347
+ class SessionManager:
348
+ """Manages per-session virtual filesystems.
349
+
350
+ Creates and tracks virtual filesystems for each user session,
351
+ providing automatic cleanup of inactive sessions.
352
+ """
353
+
354
+ def __init__(
355
+ self,
356
+ session_timeout_seconds: int = 3600, # 1 hour default
357
+ cleanup_interval_seconds: int = 300, # 5 minutes
358
+ ):
359
+ self._sessions: Dict[str, Dict[str, Any]] = {}
360
+ self._lock = threading.Lock()
361
+ self._session_timeout = session_timeout_seconds
362
+ self._cleanup_interval = cleanup_interval_seconds
363
+ self._last_cleanup = time.time()
364
+
365
+ def create_session(self, session_id: Optional[str] = None) -> str:
366
+ """Create a new session with its own virtual filesystem.
367
+
368
+ Returns the session ID (generated if not provided).
369
+ """
370
+ if session_id is None:
371
+ session_id = str(uuid.uuid4())
372
+
373
+ with self._lock:
374
+ if session_id not in self._sessions:
375
+ self._sessions[session_id] = {
376
+ "filesystem": VirtualFilesystem(root="/workspace"),
377
+ "created_at": datetime.now(),
378
+ "last_accessed": datetime.now(),
379
+ "agent_state": None,
380
+ "thread_id": str(uuid.uuid4()),
381
+ }
382
+
383
+ # Initialize default directories
384
+ fs = self._sessions[session_id]["filesystem"]
385
+ fs.mkdir("/workspace", exist_ok=True)
386
+ fs.mkdir("/workspace/.canvas", exist_ok=True)
387
+
388
+ self._maybe_cleanup()
389
+ return session_id
390
+
391
+ def get_session(self, session_id: str) -> Optional[Dict[str, Any]]:
392
+ """Get session data by ID."""
393
+ with self._lock:
394
+ session = self._sessions.get(session_id)
395
+ if session:
396
+ session["last_accessed"] = datetime.now()
397
+ session["filesystem"]._last_accessed = datetime.now()
398
+ return session
399
+
400
+ def get_filesystem(self, session_id: str) -> Optional[VirtualFilesystem]:
401
+ """Get the virtual filesystem for a session."""
402
+ session = self.get_session(session_id)
403
+ return session["filesystem"] if session else None
404
+
405
+ def get_thread_id(self, session_id: str) -> Optional[str]:
406
+ """Get the LangGraph thread ID for a session."""
407
+ session = self.get_session(session_id)
408
+ return session["thread_id"] if session else None
409
+
410
+ def get_or_create_session(self, session_id: Optional[str] = None) -> str:
411
+ """Get existing session or create new one."""
412
+ if session_id and session_id in self._sessions:
413
+ self.get_session(session_id) # Touch access time
414
+ return session_id
415
+ return self.create_session(session_id)
416
+
417
+ def delete_session(self, session_id: str) -> bool:
418
+ """Delete a session and its virtual filesystem."""
419
+ with self._lock:
420
+ if session_id in self._sessions:
421
+ del self._sessions[session_id]
422
+ return True
423
+ return False
424
+
425
+ def _maybe_cleanup(self) -> None:
426
+ """Clean up expired sessions if enough time has passed."""
427
+ now = time.time()
428
+ if now - self._last_cleanup < self._cleanup_interval:
429
+ return
430
+
431
+ self._last_cleanup = now
432
+ self._cleanup_expired_sessions()
433
+
434
+ def _cleanup_expired_sessions(self) -> int:
435
+ """Remove sessions that have been inactive too long."""
436
+ now = datetime.now()
437
+ expired = []
438
+
439
+ with self._lock:
440
+ for session_id, session in self._sessions.items():
441
+ last_accessed = session["last_accessed"]
442
+ if (now - last_accessed).total_seconds() > self._session_timeout:
443
+ expired.append(session_id)
444
+
445
+ for session_id in expired:
446
+ del self._sessions[session_id]
447
+
448
+ if expired:
449
+ print(f"Session cleanup: removed {len(expired)} expired sessions")
450
+
451
+ return len(expired)
452
+
453
+
454
+ # Global session manager instance
455
+ _session_manager: Optional[SessionManager] = None
456
+
457
+
458
+ def get_session_manager() -> SessionManager:
459
+ """Get the global session manager instance."""
460
+ global _session_manager
461
+ if _session_manager is None:
462
+ _session_manager = SessionManager()
463
+ return _session_manager
464
+
465
+
466
+ def get_virtual_filesystem(session_id: str) -> Optional[VirtualFilesystem]:
467
+ """Convenience function to get a session's virtual filesystem."""
468
+ return get_session_manager().get_filesystem(session_id)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cowork-dash
3
- Version: 0.1.6
3
+ Version: 0.1.7
4
4
  Summary: AI Agent Web Interface with Filesystem and Canvas Visualization
5
5
  Project-URL: Homepage, https://github.com/dkedar7/cowork-dash
6
6
  Project-URL: Documentation, https://github.com/dkedar7/cowork-dash/blob/main/README.md
@@ -0,0 +1,22 @@
1
+ cowork_dash/__init__.py,sha256=37qBKl7g12Zos8GFukXLXligCdpD32e2qe9F8cd8Qdk,896
2
+ cowork_dash/__main__.py,sha256=CCM9VIkWuwh7hwVGNBBgCCbeVAcHj1soyBVXUaPgABk,131
3
+ cowork_dash/agent.py,sha256=JhCeQuMJj3VaQij2i8OPznKN6qGAixN-hakueiGfdt0,5851
4
+ cowork_dash/app.py,sha256=a7Z1N81h5BbI0rYfvy4_j4RDepVVTzJ622ma4KFjBec,109947
5
+ cowork_dash/backends.py,sha256=YQE8f65o2qGxIIfvBITAS6krCLjl8D9UixW-pgbdgZk,15050
6
+ cowork_dash/canvas.py,sha256=TNDku1AKpkajj1RgJW3zUDIq45H3M2RNR1plcsBS3dc,16969
7
+ cowork_dash/cli.py,sha256=E7H9p21XcvuF6Kae4rjVVOavU7U8-TY0qFLHLPOtYWg,7125
8
+ cowork_dash/components.py,sha256=SlR0U1d0XThdcPZYButE_Fqt8MWPoLAgOTqtwfisvOQ,23776
9
+ cowork_dash/config.py,sha256=PoAxUbctyd_2JjH4eVffKhOPCEOvaRyPSzCmS4MFPi8,4570
10
+ cowork_dash/file_utils.py,sha256=KwYHOtVkp-anysqktGsLvtb3yg-N4e3QhnliSfvFr6o,12533
11
+ cowork_dash/layout.py,sha256=6K6O7rxnYxcrIzMOQYtyth12wRGcZ_l8qKaZFvbxvRo,16634
12
+ cowork_dash/tools.py,sha256=fkQjeJGD-NfAcwpRTjsbTwGryg_QY7zHTI-SqKxroko,33061
13
+ cowork_dash/virtual_fs.py,sha256=PAAdRiMkxgJ4xPpGUyAUd69KbH-nFlt-FjldfB1FrQ4,16296
14
+ cowork_dash/assets/app.js,sha256=Fo_tgFat9eXtpvrptLHmS0qAs04pfF0IaQYiT-Tohm8,8768
15
+ cowork_dash/assets/favicon.ico,sha256=IiP0rVr0m-TBGGmCY88SyFC14drNwDhLRqb0n2ZufKk,54252
16
+ cowork_dash/assets/favicon.svg,sha256=MdT50APCvIlWh3HSwW5SNXYWB3q_wKfuLP-JV53SnKg,1065
17
+ cowork_dash/assets/styles.css,sha256=w4D9jAneSUjCZxdXu1tIbbKPt_HnNF0nax4hy9liOHw,24095
18
+ cowork_dash-0.1.7.dist-info/METADATA,sha256=vu1E31NrMChJtrHexsfSowH5rFVAhBA9m-DF5x39En4,6719
19
+ cowork_dash-0.1.7.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
20
+ cowork_dash-0.1.7.dist-info/entry_points.txt,sha256=lL_9XJINiky3nh13tLqWd61LitKbbyh085ROATH9fck,53
21
+ cowork_dash-0.1.7.dist-info/licenses/LICENSE,sha256=2SFXFfIa_c_g_uwY0JApQDXI1mWqEfJeG87Pn4ehLMQ,1072
22
+ cowork_dash-0.1.7.dist-info/RECORD,,
@@ -1,20 +0,0 @@
1
- cowork_dash/__init__.py,sha256=37qBKl7g12Zos8GFukXLXligCdpD32e2qe9F8cd8Qdk,896
2
- cowork_dash/__main__.py,sha256=CCM9VIkWuwh7hwVGNBBgCCbeVAcHj1soyBVXUaPgABk,131
3
- cowork_dash/agent.py,sha256=70UGztL0tOEFMXc1T1GavGzFuQWDjDEoy2yDIQz7SAA,4531
4
- cowork_dash/app.py,sha256=X9-aSwabisU5XuPoBGdl3Ys0U8ffnogOK5rN6Da-ZNg,97759
5
- cowork_dash/canvas.py,sha256=sF00c0amtF5AayLG8JKMSPwKEumNfDXd6r6gSDfvTHk,15446
6
- cowork_dash/cli.py,sha256=RlTRp1VB8f8xbFo_rrNly0COIMMGO0IZi3TYXP3yTCI,6719
7
- cowork_dash/components.py,sha256=3Nb9zblt3c3cHuE4mP6jLCJI-vALGxcpNqlQ1Sbr_rE,23832
8
- cowork_dash/config.py,sha256=v-qdQUtWgJE8nYxMgSquMBlWS_tMTfoYx5oPSJt0V30,3774
9
- cowork_dash/file_utils.py,sha256=R1Pzr05iLfzPgPKIMplF6upTX1sJy1xzMTQ7gjsVesA,9108
10
- cowork_dash/layout.py,sha256=mdC7YaC7u__Yo-liRjbHOaiDAJ5n1pQKaprQ5qMsg2s,16222
11
- cowork_dash/tools.py,sha256=pHX8PhsY70YwCF5JZfb7vZLoc5c89QWQB6T6OhR5svU,25862
12
- cowork_dash/assets/app.js,sha256=Fo_tgFat9eXtpvrptLHmS0qAs04pfF0IaQYiT-Tohm8,8768
13
- cowork_dash/assets/favicon.ico,sha256=IiP0rVr0m-TBGGmCY88SyFC14drNwDhLRqb0n2ZufKk,54252
14
- cowork_dash/assets/favicon.svg,sha256=MdT50APCvIlWh3HSwW5SNXYWB3q_wKfuLP-JV53SnKg,1065
15
- cowork_dash/assets/styles.css,sha256=iHwGFZhzTP3nQEa_vyhoa-ADAW9DV-kU5OqYP7N2S7Q,23891
16
- cowork_dash-0.1.6.dist-info/METADATA,sha256=_EDlZ7kDCGbPucYeHZnOFN4v5zP8wzOh5sYAP2v4wzI,6719
17
- cowork_dash-0.1.6.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
18
- cowork_dash-0.1.6.dist-info/entry_points.txt,sha256=lL_9XJINiky3nh13tLqWd61LitKbbyh085ROATH9fck,53
19
- cowork_dash-0.1.6.dist-info/licenses/LICENSE,sha256=2SFXFfIa_c_g_uwY0JApQDXI1mWqEfJeG87Pn4ehLMQ,1072
20
- cowork_dash-0.1.6.dist-info/RECORD,,