Skip to main content

Working with Files

Each session runs inside an isolated sandbox with its own workspace directory. Files the agent creates, modifies, or downloads during a session are accessible through the files API. This lets you review agent-written code, download generated artifacts, or inspect the workspace state.

Listing Files

Retrieve a flat list of all files in a session's workspace.

import { AshClient } from '@ash-ai/sdk';

const client = new AshClient({ serverUrl: 'http://localhost:4100', apiKey: process.env.ASH_API_KEY });

const result = await client.getSessionFiles(sessionId);
console.log(`Source: ${result.source}`); // "sandbox" or "snapshot"
for (const file of result.files) {
console.log(`${file.path} (${file.size} bytes, modified ${file.modifiedAt})`);
}

The source field indicates where the file listing came from:

SourceMeaning
sandboxRead from the live sandbox process. The session is active or paused with its sandbox still running.
snapshotRead from a persisted workspace snapshot. The sandbox was reclaimed but workspace state was saved.

Downloading a File (Raw)

Download a file as raw bytes. This is the default behavior and works for any file type — text, binary, images, PDFs, etc. Files up to 100 MB are supported.

const { buffer, mimeType, source } = await client.downloadSessionFile(sessionId, 'output/report.pdf');
console.log(`Type: ${mimeType}, Source: ${source}`);
fs.writeFileSync('report.pdf', buffer);

The response includes these headers:

HeaderDescription
Content-TypeMIME type based on file extension (e.g. application/pdf, text/typescript)
Content-DispositionSuggested filename for download
Content-LengthFile size in bytes
X-Ash-Sourcesandbox or snapshot

Reading a File (JSON)

For text files, you can get the content inline as a JSON response by adding ?format=json. This is useful for building UIs that display file content directly. Limited to 1 MB.

const file = await client.getSessionFile(sessionId, 'src/index.ts');
console.log(`Path: ${file.path}`);
console.log(`Size: ${file.size} bytes`);
console.log(`Source: ${file.source}`);
console.log(file.content);

Limitations (JSON mode)

  • Maximum file size is 1 MB. For larger files, use the raw download.
  • Content is read as UTF-8 text. Binary files should use the raw download instead.
  • Path traversal (..) and absolute paths (/) are rejected with a 400 error.
  • Certain directories are excluded from listings: node_modules, .git, __pycache__, .cache, .npm, .venv, and other common dependency/cache directories.

Workspace Isolation

Each session's workspace is isolated from other sessions and from the host system. The agent can read and write files within its workspace but cannot access files outside of it.

When a session is created, the agent definition folder is copied into the sandbox workspace. Any files the agent creates during the session live alongside the agent definition files.

When a session is paused or ended, the workspace state is persisted as a snapshot. If the session is later resumed with a new sandbox (cold resume), the snapshot is restored so the agent picks up where it left off.

Use Cases

Reviewing agent-written code. After an agent writes code in response to a prompt, list the workspace files and read specific files to review what was generated.

const session = await client.createSession('code-writer');

// Ask the agent to write something
for await (const event of client.sendMessageStream(session.id, 'Write a Python fibonacci function')) {
// wait for completion
}

// Review what was written
const files = await client.getSessionFiles(session.id);
for (const f of files.files) {
if (f.path.endsWith('.py')) {
const content = await client.getSessionFile(session.id, f.path);
console.log(`--- ${content.path} ---`);
console.log(content.content);
}
}

Downloading binary artifacts. If an agent generates images, PDFs, or other binary files, download them directly.

// Download a generated image
const { buffer } = await client.downloadSessionFile(session.id, 'output/chart.png');
fs.writeFileSync('chart.png', buffer);

Accessing files after a session ends. Files remain available from the persisted snapshot.

await client.endSession(session.id);
// Files still accessible from snapshot
const report = await client.getSessionFile(session.id, 'output/report.md');

API Reference

MethodEndpointDescription
GET/api/sessions/:id/filesList all files in the session workspace
GET/api/sessions/:id/files/*pathDownload a file (raw bytes by default, JSON with ?format=json)