Agents - Real-time file watching in Sandboxes
Key Points
- SSE ReadableStream from sandbox.watch()
- Supports create/modify/delete/move events
- Use parseSSEStream for worker-side iteration
Summary
Sandboxes now provide real-time filesystem watching via sandbox.watch(path, options). The call returns a Server-Sent Events (SSE) ReadableStream backed by native inotify so your Worker can receive create, modify, delete, and move events as they happen inside the container. Move events include a from field with the original path.
Key Points
- API:
await sandbox.watch(path, { recursive?, include? })returns an SSE ReadableStream. - Events:
create,modify,delete,move(move includesfrom). - Options:
recursive(boolean, default false) to watch subdirectories;include(string[]) accepts glob filters — omit to receive all events. - Client streaming: proxy the stream directly to a browser using a
ResponsewithContent-Type: text/event-stream.
// Stream events to a browser
const stream = await sandbox.watch("/workspace/src", { recursive: true, include: ["*.ts", "*.js"] });
return new Response(stream, { headers: { "Content-Type": "text/event-stream" } });
- Server-side consumption: use
parseSSEStreamto iterate events inside the Worker without forwarding.
import { parseSSEStream } from "@cloudflare/sandbox";
const stream = await sandbox.watch("/workspace/src", { recursive: true });
for await (const event of parseSSEStream(stream)) {
console.log(event.type, event.path);
}
- Upgrade:
npm i @cloudflare/sandbox@latest.
References
- See the Sandbox file watching reference in the docs for full API details.