You can watch a directory for changes using the files.watchDir() method in JavaScript and files.watch_dir() method in Python.
Since events are tracked asynchronously, their delivery may be delayed. It’s recommended not to collect or close watcher immediately after making a change.
import { Sandbox, FilesystemEventType } from 'novita-sandbox/code-interpreter'

const sandbox = await Sandbox.create()
const dirname = '/tmp'

// Start watching directory for changes
const handle = await sandbox.files.watchDir(dirname, async (event) => {
  console.log(`got event: ${event.type} - ${event.name}`)
  if (event.type === FilesystemEventType.WRITE) {
    console.log(`wrote to file ${event.name}`)
  }
})

// Trigger file write event
await sandbox.files.write(`${dirname}/test-file`, 'test-file-content')

// Stop watching directory for changes
handle.stop()

await sandbox.kill()

Recursive Watching

You can enable recursive watching using the parameter recursive.
When rapidly creating new folders (e.g., deeply nested path of folders), events other than CREATE might not be emitted. To avoid this behavior, create the required folder structure in advance.
import { Sandbox, FilesystemEventType } from 'novita-sandbox/code-interpreter'

const sandbox = await Sandbox.create()
const dirname = '/tmp'

// Start watching directory for changes
const handle = await sandbox.files.watchDir(dirname, async (event) => {
  console.log(`got event: ${event.type} - ${event.name}`)
  if (event.type === FilesystemEventType.WRITE) {
    console.log(`wrote to file ${event.name}`)
  }
}, {
  recursive: true
})

// Trigger file write event
await sandbox.files.write(`${dirname}/test-folder/test-file`, 'test-file-content')

// Stop watching directory for changes
handle.stop()

await sandbox.kill()