@nodesandbox-ai/plugin-pi

Pi

Add the pi terminal command to an individual sandbox. Its CLI and dependencies install on demand through the runtime's standard npm and Node APIs.

$npm i @nodesandbox-ai/client @nodesandbox-ai/plugin-pi

Run Pi

pi.ts
import { Sandbox } from '@nodesandbox-ai/client';
import { piPlugin } from '@nodesandbox-ai/plugin-pi';

const pi = piPlugin({
  onReady: () => console.log('Pi is prepared'),
  onError: error => console.error(error),
});
const sandbox = await Sandbox.create({
  apiKey: 'ns_live_…',
  source: { type: 'files', files: {} },
  plugins: [pi],
});
const terminal = sandbox.terminals.create({ startCommand: 'pi' });
terminal.onOutput(chunk => console.log(chunk));
await terminal.open();

Attach your terminal UI's input and resize events with the terminal API, or pass this plugin to Elements and add a { id: 'pi', label: 'Pi', icon: <PiIcon />, render: <SandboxTerminal command="pi" /> } button to sidenavItems. Pi then appears in the desktop sidenav and mobile menu; see Elements agent integration.

The plugin is lazy. Use preload: true or sandbox.plugins.get(pi).prepare() to install without launching it. Preparation is shared per sandbox and failures can be retried. Project dependencies and Pi install in separate directories.

Bare pi and provider/model/session flags open its TUI. Use pi -p "Summarize this project" for a command that returns a result and exits.

Credentials and files

pi-options.ts
const pi = piPlugin({
  credentials: async signal => {
    const response = await fetch('/api/pi/credentials', { signal });
    if (!response.ok) throw new Error('Unable to load Pi credentials');
    return (await response.json()).credentials; // auth.json string or null
  },
  onCredentialsChanged: value => {
    void savePiCredentials(value).catch(console.error);
  },
  files: {
    '/home/user/.pi/agent/settings.json': JSON.stringify({ theme: 'dark' }),
    '/AGENTS.md': 'Run project tests before finishing changes.',
  },
});

Implement the credential endpoint and save function in your app. Credentials use Pi's auth.json format, such as { "anthropic": { "type": "api_key", "key": "user-key" } }. They live at /home/user/.pi/agent/auth.json. Omitted credentials preserve an existing file; null clears it. Changes are watched and flushed after commands.

Use files for settings, models, themes, skills, and extensions. Text, bytes, and lazy file loaders are supported. Files are injected after installation and before launch. Pi's theme is configured independently of the surrounding terminal.

The instance supports prepare(), setCredentials(string | null), and clearSession(). Clearing the session clears credentials and emits null; it does not remove conversation files. Persist Pi's sessions from /home/user/.pi/agent/sessions in your app if they need to survive sandbox disposal. See shared plugin options.

Consult Pi's provider documentation for auth formats. Provider OAuth flows that require native callback servers may need additional integration.

Versions and packs

APIBehavior
version?: string'latest' (default), the newest npm release when Pi is prepared, or an exact upstream npm version. If npm cannot answer, an installed Pi is kept, or TESTED_PI_VERSION is installed. Dependencies install into /opt/pi.
bundleUrl?: stringOptional HTTP(S) URL of a compatible pack JSON. Replaces npm installation.

The default needs no R2 bucket or native binary. A new Pi release reaches your sandboxes without a new plugin; pin version when you need a fixed release. npm resolves transitive version ranges.

The plugin uses Earendil Works' Pi and adapts its find tool and @file picker to a JavaScript search helper over the sandbox filesystem. It does not download a native fd. New upstream versions must retain compatible search modules.

pi-assets.ts
piPlugin({ version: '1.4.0' }); // pin an exact release instead of the latest

// Alternatively, serve a complete compatible installation from your CDN:
piPlugin({ bundleUrl: 'https://cdn.example/pi/release-id/pi.pack.json' });

A pack has a files object mapping paths relative to /opt/pi to text or { base64: string } for binary data. Export a working NodeSandbox installation, including the CLI at node_modules/@earendil-works/pi-coding-agent/dist/cli.js, all dependencies, manifests, and assets. An npm tarball or native executable cannot be used as a pack.

Use an immutable URL and configure CORS for the embedding app. Choose the build when creating a new sandbox. Pi is never bundled into the runtime host. Use a current host with per-process OAuth capture control so Pi manages its own credentials without entering the legacy Claude capture path.

Provider HTTP proxy

pi-relay.ts
piPlugin({
  piRelay: {
    url: 'https://your-app.example/api/pi-proxy',
    headers: { 'X-Relay-Authorization': 'Bearer user-session-token' },
  },
});

Omit piRelay to retain the sandbox's networking. A configured proxy receives the original target in its url query parameter; forward the method, body, and provider headers and stream the response. This is an HTTP proxy, not a WebSocket relay.

Your app controls authentication. Use a dedicated relay header, consume it before forwarding upstream, and allow the runtime origin, headers, methods, and OPTIONS through CORS. The relay applies to Pi's process and children; npm and pack downloads keep their own asset URLs.