> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/checkpoints/javascript-typescript.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# JavaScript / TypeScript

{% hint style="info" %}
Concepts and use cases: [Checkpoints, sessions & resume](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/checkpoints.md)
{% endhint %}

## Multi-turn agent

```js
import { DNotifier } from "@dnotifier-realtime/dnotifier";

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  userId: "user-123",
  transport: "http",
});
await notifier.connect();

const agent = DNotifier.defineAgent({
  name: "support",
  instructions: "Help the user. Remember details they already shared.",
  provider: "open_ai",
  model: "gpt-4o",
  tools: [/* your tools */],
});

// Pass your own sessionId, or omit it and read result.sessionId after the first run.
const first = await notifier.runAgent(agent, {
  input: "I need help with order 991.",
  checkpointer: "memory", // SDK creates sessionId when you don't pass one
});

const sessionId = first.sessionId; // save this for follow-ups

const followUp = await notifier.runAgent(agent, {
  input: "Can you repeat the order number?",
  sessionId,
});

console.log(followUp.output);
console.log(followUp.sessionId, followUp.executionId, followUp.checkpointId);
```

## Retry after failure

```js
async function runWithRetry(agent, input, sessionId) {
  try {
    return await notifier.runAgent(agent, { input, sessionId });
  } catch (err) {
    console.warn("first attempt failed:", err.message);
    return await notifier.runAgent(agent, {
      input,
      sessionId,
      mode: "retry",
    });
  }
}

const result = await runWithRetry(
  agent,
  "Call the booking tool, then confirm.",
  "booking-session-1"
);
```

## Replay from a checkpoint

```js
// After a run, keep result.checkpointId for debugging
await notifier.runAgent(agent, {
  sessionId: "booking-session-1",
  fromCheckpointId: previous.checkpointId,
  mode: "replay",
});
```

## Workflow with session continuity

```js
const workflow = new DNotifier.Workflow({
  name: "fulfill-order",
  observability: true,
  async entry(ctx) {
    // ctx.state is restored when sessionId was used on a prior run
    ctx.state.attempts = (ctx.state.attempts ?? 0) + 1;
    return ctx.runAgent("support");
  },
}).registerAgent("support", agent);

const first = await notifier.runWorkflow({
  workflow,
  input: { orderId: "1001" },
  sessionId: "order-1001",
});

const retry = await notifier.runWorkflow({
  workflow,
  input: { orderId: "1001" },
  sessionId: "order-1001",
  mode: "retry",
});
```

## In-memory checkpointer (tests / demos)

```js
import { DNotifier, MemoryCheckpointer } from "@dnotifier-realtime/dnotifier";

const checkpointer = new MemoryCheckpointer();

await notifier.runAgent(agent, {
  input: "Remember that my plan is Pro.",
  sessionId: "demo",
  checkpointer,
});

await notifier.runAgent(agent, {
  input: "What plan am I on?",
  sessionId: "demo",
  checkpointer,
});
```

## Options reference

| Option             | Type                                             | Description                               |
| ------------------ | ------------------------------------------------ | ----------------------------------------- |
| `sessionId`        | `string`                                         | Continue this session’s memory            |
| `checkpointer`     | `"memory"` \| `"remote"` \| object \| `false`    | Store override; `false` disables          |
| `resume`           | `boolean`                                        | Continue from latest checkpoint           |
| `mode`             | `"run"` \| `"resume"` \| `"retry"` \| `"replay"` | Explicit run mode                         |
| `fromCheckpointId` | `string`                                         | Target checkpoint for resume/retry/replay |

## Next steps

* [Checkpoints overview](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/checkpoints.md)
* [Run a workflow](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/run-workflow.md)
* [Workflow observability](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/observability.md)
