Skip to content
Ask
Reference

API Reference

The public surface of @inbrowser/workspace: workspace creation, file systems, preview, shell, git, packages, and agent tools.

This page describes the public surface of @inbrowser/workspace.

Import Paths

Import pathExports
@inbrowser/workspacecreateBrowserWorkspace, the core BrowserWorkspace types, and lightweight file/package helpers
@inbrowser/workspace/fsfile system adapters and path helpers
@inbrowser/workspace/previewgeneric esbuild workspace compilation primitives
@inbrowser/workspace/preview/reactReact-specific preview runtime helpers
@inbrowser/workspace/shelljust-bash workspace shell
@inbrowser/workspace/snapshotspersisted workspace snapshot manager
@inbrowser/workspace/gitbrowser-native workspace git service
@inbrowser/workspace/packagesbrowser package registry and CDN resolver
@inbrowser/workspace/agent-toolsoptional structural agent-tool factories

The root import lazy-loads preview, shell, and git services. Use subpaths when you need direct access to those services.

createBrowserWorkspace

TS
function createBrowserWorkspace(options: BrowserWorkspaceOptions): Promise<BrowserWorkspace>;

BrowserWorkspaceOptions:

FieldTypeDescription
idstringStable workspace id. OPFS storage is scoped by this value.
rootstringVirtual workspace root. Defaults to /work.
storage'opfs-with-memory-fallback' | 'opfs' | 'memory'Storage mode. Defaults to OPFS with memory fallback.

BrowserWorkspace:

MemberDescription
idWorkspace id passed at creation.
rootVirtual root path, usually /work.
storageStatus'best-effort' for OPFS or 'memory' for in-memory storage.
fsWorkspace file system.
packagesBrowser package registry.
snapshotsPersisted local restore points for the workspace working tree.
createReactPreview(options)Lazily creates a React preview runtime.
createShell(options?)Lazily creates a jailed shell.
createGit(options?)Lazily creates a git service.
dispose()Reserved lifecycle hook.

File System

TS
interface WorkspaceFileSystem {
  kind: 'opfs' | 'memory';
  root: string;
  promises: WorkspaceFileSystemPromises;
  watch(callback: (event: WorkspaceFileEvent) => void): () => void;
  snapshot(root?: string): Promise<WorkspaceSnapshot>;
  restore(snapshot: WorkspaceSnapshot, options?: { clearRoot?: boolean }): Promise<void>;
}

WorkspaceFileSystemPromises:

MethodDescription
readFile(path)Reads bytes.
readFile(path, 'utf8')Reads text.
writeFile(path, data)Writes bytes or text. Parent directories are created by the memory adapter.
mkdir(path, options?)Creates a directory.
readdir(path)Returns names.
readdir(path, { withFileTypes: true })Returns dirent-like objects.
stat(path)Returns file or directory stats.
lstat(path)Same as stat in V1; symlinks are not part of the public promise.
unlink(path)Deletes a file.
rmdir(path, options?)Deletes a directory.
rename(from, to)Moves a file or directory.

React Preview

TS
function createReactPreviewRuntime(options: ReactPreviewRuntimeOptions): ReactPreviewRuntime;

ReactPreviewRuntimeOptions:

FieldTypeDescription
fsWorkspaceFileSystemFile system used for relative imports.
entrystringEntry module path, such as /work/src/App.tsx.
reactRecord<string, unknown>Host React module.
jsxRuntimeRecord<string, unknown>Host react/jsx-runtime module.
jsxDevRuntimeRecord<string, unknown>Optional host react/jsx-dev-runtime module.
extraHostModulesRecord<string, PreviewHostModule>Additional host aliases.
importMapRecord<string, string>Browser package imports.

ReactPreviewRuntime:

MethodDescription
compile(source?)Bundles the entry and returns diagnostics or an evaluator.
scope(extra?)Builds the host-module scope passed to evaluate.

Shell

TS
function createWorkspaceShell(options: CreateWorkspaceShellOptions): WorkspaceShell;

WorkspaceShell:

MethodDescription
exec(command, options?)Runs a command through just-bash.
cwd()Returns the persisted current directory.
setCwd(path)Sets the current directory, clamped to the workspace root.

The shell is not a Node process. It does not guarantee npm, Vite servers, or native binaries.

Snapshots

TS
const beforeEdit = await workspace.snapshots.create({ label: 'before edit' });
await workspace.snapshots.restore(beforeEdit.id);

WorkspaceSnapshotManager:

MethodDescription
create(options?)Persists a restore point for the workspace working tree.
list()Returns persisted snapshot records without file payloads.
get(id)Returns one snapshot record, or null if it is missing.
restore(id)Replaces the current working tree with the snapshot contents.

Snapshots are local restore points for working files. They are useful for undo, agent checkpoints, and before/after previews. They are not commit history. Restore preserves .git so explicit Git history survives working-tree rollback.

Git

TS
function createWorkspaceGit(options: { fs: WorkspaceFileSystem; dir: string }): WorkspaceGit;

WorkspaceGit:

MethodDescription
init()Initialises a git repository.
currentBranch()Returns the current branch or null.
status()Returns structured status rows.
stageAll()Stages additions, modifications, and deletions.
commit(options)Creates a commit and returns the oid.
checkout(branch, options?)Checks out or creates a branch.
log(options?)Returns commit log entries.
listFiles(options?)Lists tracked files.

Packages

TS
function createPackageRegistry(options: CreatePackageRegistryOptions): WorkspacePackageRegistry;

WorkspacePackageRegistry:

MethodDescription
install(spec)Resolves and records a browser-compatible package.
uninstall(name)Removes a package from the registry.
list()Returns installed package records.
getImportMap()Returns the import map used by preview compilation.

The default resolver uses esm.sh. Hosts can inject another resolver.

Agent Tools

createWorkspaceTools({ workspace }) returns structural tool handlers for reading, writing, listing, shell commands, git status, and package installs.

The returned tools are intentionally not tied to a specific agent package at runtime. They are shaped to be compatible with @inbrowser/agent without making @inbrowser/workspace depend on agent policy.