> 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/examples/multi-agent-workflow/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

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

const intentAgent = DNotifier.defineAgent({
  name: "intent-agent",
  async run(ctx) {
    const result = await ctx.sendAI({
      message: {
        text: `Classify this question as "search" (needs knowledge base) or "general". Question: ${ctx.input}`,
      },
      saveHistory: false,
      label: "Intent classification",
    });
    const text = (result?.data?.content ?? "").toLowerCase();
    const intent = text.includes("search") ? "search" : "general";
    ctx.state.intent = intent;
    return { intent };
  },
});

const generalAgent = DNotifier.defineAgent({
  name: "general-agent",
  async run(ctx) {
    const question = ctx.input?.question ?? ctx.input;
    const result = await ctx.sendAI({
      message: { text: `Answer briefly: ${question}` },
      saveHistory: false,
      label: "General Q&A",
    });
    return { answer: result?.data?.content ?? "" };
  },
});

const workflow = new DNotifier.Workflow({
  name: "intent-router",
  description: "Route to search or general Q&A",
  observability: true,
  async entry(ctx) {
    const { intent } = await ctx.runAgent("intent-agent");

    if (intent === "search") {
      const hits = await ctx.search({
        query: String(ctx.input),
        limit: 5,
        label: "Knowledge search",
      });
      return { branch: "search", hits };
    }

    const { answer } = await ctx.runAgent("general-agent", {
      input: { question: ctx.input },
    });
    return { branch: "general", answer };
  },
}).registerAgents({
  "intent-agent": intentAgent,
  "general-agent": generalAgent,
});

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "http",
  userId: "workflow-user",
  onConnected: () => {},
  onMessage: () => {},
  onDisconnected: () => {},
});

await notifier.connect();

const run = await notifier.runWorkflow({
  workflow,
  input: "How does messaging work?",
});

console.log("executionId:", run.executionId);
console.log("result:", run.result);
console.log("state:", run.state);
```

## Custom step

Inside any agent or `entry`:

```js
await ctx.recordStep({
  label: "Custom validation",
  input: { value: ctx.input },
  output: { ok: true },
  status: "ok",
  type: "custom",
});
```

## View telemetry

When `observability: true`, open the DNotifier workflow dashboard to inspect `executionId`, steps, latency, and token usage.
