# Node.js

Canonical URL: https://developers.iagenify.com/sdk/runtimes/node/
Language: en

The full surface: a session cached on disk, a real workspace, shells that outlive an answer, and an agent that works on the machine it runs on.

Status: node ≥ 18 · filesystem · shell

## Where the session lives {#session}

In `~/.iagenify/session`, inside a directory created with mode `0700`. It survives restarts, so a long-lived process exchanges the key once and never again.

```javascript
// One account per machine: nothing to configure
const api = new IaGenify({ apiKey: process.env.IAGENIFY_API_KEY });

// Several accounts on one machine: give each its own file
const api = new IaGenify({
  apiKey: process.env.IAGENIFY_API_KEY,
  sessionPath: "/var/lib/myapp/sessions/tenant-a",
});
```

> **One file, one account**
>
> Two clients with different keys sharing a sessionPath overwrite each other. Give each its own path.

## The workspace {#workspace}

A Node machine registers itself as a storage on the first key exchange. Its files live in `~/.iagenify/files` and stay there: a computer storage keeps no bytes on the server, and an upload aimed at one is refused with `storage_is_local`.

```javascript
const pc = await api.storage.get();   // this computer

await pc.files.write("Launch plan/brief.md", "# Brief\n");
await pc.files.mkdir("Archive");

await pc.files.list();                // the root
await pc.files.list("Launch plan");
await pc.files.list("", { recursive: true });
// [{ name, path, type: "file" | "folder", size, mimeType, modifiedAt }]

const { content } = await pc.files.read("Launch plan/brief.md");

await pc.files.move("Launch plan/brief.md", "Archive/brief.md");
await pc.files.remove("Archive");
```

### Nothing is lost by default

| Action | What is kept |
| --- | --- |
| Overwriting a file | the previous version, under .versions |
| Removing a file or folder | the item, under .trash |
| Moving onto an existing path | nothing is overwritten — the move is refused |

Those two folders are the agent's safety net, and they are ordinary folders: you can read them, restore from them, or empty them yourself.

## Terminals {#terminals}

Only here. A terminal is a real shell that outlives the answer that started it — a dev server, a watcher, a build. It lives in the Node process running the SDK, so restarting that process ends it.

```javascript
// An open shell you send lines to
const shell = await pc.terminals.create({ name: "dev", cwd: "site" });
await shell.run("npm install");
await shell.run("npm run dev");

// Or a command that is the terminal, started immediately
const server = await pc.terminals.create({ name: "api", command: "node server.js" });

await pc.terminals.list();            // every open terminal
await pc.terminals.get(shell.id);
await pc.terminals.remove(shell.id);
```

### Reading the output

`output()` is a cursor, not a stream. Ask from a position and you get what came after it, plus the next position.

```javascript
let cursor = 0;

for (;;) {
  const out = await shell.output({ since: cursor });
  if (out.text) process.stdout.write(out.text);
  cursor = out.cursor;
  if (out.truncated) console.warn("fell behind — older output was dropped");
  if (out.status !== "running") { console.log("exit", out.exitCode); break; }
  await new Promise((r) => setTimeout(r, 400));
}
```

| Field | Meaning |
| --- | --- |
| text | what was printed after since |
| cursor | an absolute position — pass it back next time |
| status | running, or finished |
| exitCode | set once it has finished |
| truncated | your since was older than what is still kept |

### Stopping things

```javascript
shell.busy;              // true while a command is running
shell.cwd;               // follows every cd

await shell.interrupt(); // Ctrl+C: stops the command, keeps the shell
await shell.kill();      // ends the shell and every process it started
```

> **interrupt() and kill() are not the same**
>
> interrupt() stops what is running and leaves you a shell to use. kill() takes the whole process tree with it, which is what you want for a dev server you are done with.

## An agent on this machine {#agent}

Nothing to supply: the disk and the shell are already there, so the full tool set is available.

```javascript
const agent = api.agents.create({
  runtime: "local",
  machine: pc,
  model: "gemini-3.5-flash",
  mode: "ask",
  budget: 200,
  onPermission: async ({ tool, args, category }) => {
    if (category === "exec") return "deny";     // no commands in this process
    return "once";
  },
});

await agent.run({
  message: "Read Launch plan/brief.md and list what is missing.",
  onEvent: (e) => console.log(e.type, e.tool ?? ""),
});
```

## On a server, with nobody to ask {#headless}

`onPermission` is called with no human behind it. Answer it as a policy rather than leaving it unset.

```javascript
const ALLOWED = new Set(["read", "list", "glob", "grep", "write", "edit"]);

api.agents.create({
  runtime: "local",
  machine: pc,
  model: "gemini-3.5-flash",
  mode: "auto",
  budget: 500,
  onPermission: async ({ tool }) => (ALLOWED.has(tool) ? "always" : "deny"),
});
```

```python
import os

# One account per machine: nothing to configure
api = IaGenify(api_key=os.environ["IAGENIFY_API_KEY"])

# Several accounts on one machine: give each its own file
api = IaGenify(
    api_key=os.environ["IAGENIFY_API_KEY"],
    session_path="/var/lib/myapp/sessions/tenant-a",
)
```

```python
pc = api.storage.get()   # this computer

pc.files.write("Launch plan/brief.md", "# Brief\n")
pc.files.mkdir("Archive")

pc.files.list()                # the root
pc.files.list("Launch plan")
pc.files.list("", recursive=True)
# [{"name", "path", "type": "file" | "folder", "size"}]

content = pc.files.read("Launch plan/brief.md")["content"]

pc.files.move("Launch plan/brief.md", "Archive/brief.md")
pc.files.remove("Archive")
```

```python
# An open shell you send lines to
shell = pc.terminals.create(name="dev", cwd="site")
shell.run("npm install")
shell.run("npm run dev")

# Or a command that is the terminal, started immediately
server = pc.terminals.create(name="api", command="node server.js")

pc.terminals.list()            # every open terminal
pc.terminals.get(shell.id)
pc.terminals.remove(shell.id)
```

```python
import sys, time

cursor = 0

while True:
    out = shell.output(since=cursor)
    if out["text"]:
        sys.stdout.write(out["text"])
    cursor = out["cursor"]
    if out["status"] != "running":
        print("exit", out["exitCode"])
        break
    time.sleep(0.4)
```

```python
shell.busy     # True while the shell process is alive
shell.cwd      # the folder it was started in

shell.interrupt()  # Ctrl+C: stops the command, keeps the shell
shell.kill()       # ends the shell and every process it started
```

```python
def on_permission(request):
    if request["category"] == "exec":
        return "deny"     # no commands in this process
    return "once"

agent = api.agents.create(
    runtime="local",
    machine=pc,
    model="gemini-3.5-flash",
    mode="ask",
    budget=200,
    on_permission=on_permission,
)

agent.run(
    message="Read Launch plan/brief.md and list what is missing.",
    on_event=lambda e: print(e["type"], e.get("tool", "")),
)
```

```python
ALLOWED = {"read", "list", "glob", "grep", "write", "edit"}

api.agents.create(
    runtime="local",
    machine=pc,
    model="gemini-3.5-flash",
    mode="auto",
    budget=500,
    on_permission=lambda request: "always" if request["tool"] in ALLOWED else "deny",
)
```
