> 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/build-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

Compose agents into a **workflow** with an `entry` function and `registerAgents`.

## Intent router workflow

```js
import { DNotifier } from "@dnotifier-realtime/dnotifier";
import { intentAgent } from "./agents/intent-agent.js";
import { generalAgent } from "./agents/general-agent.js";

async function intentRouterEntry(ctx) {
  const intentResult = await ctx.runAgent("intent-agent");
  const intent = intentResult?.intent ?? "general";
  ctx.state.intent = intent;

  if (intent === "search") {
    const query =
      typeof ctx.input === "string"
        ? ctx.input
        : ctx.input?.query ?? ctx.input?.text ?? "";
    const hits = await ctx.search({
      query: String(query),
      limit: 10,
      minSimilarity: 0.3,
      filterbySource: "help-center",
      label: "Semantic search",
    });
    ctx.state.searchResults = hits;
    return { branch: "search", intent: intentResult, hits };
  }

  const enrichedInput = {
    question:
      typeof ctx.input === "string"
        ? ctx.input
        : ctx.input?.question ?? ctx.input,
    intentMeta: intentResult,
  };
  const generalResult = await ctx.runAgent("general-agent", {
    input: enrichedInput,
  });

  return { branch: "general", intent: intentResult, answer: generalResult };
}

export const intentRouterWorkflow = new DNotifier.Workflow({
  name: "support-intent-router",
  description: "Classify intent then search or general chat",
  observability: true,
  entry: intentRouterEntry,
}).registerAgents({
  "intent-agent": intentAgent,
  "general-agent": generalAgent,
});
```

## Blog pipeline workflow (sequential)

```js
const blogArticleWorkflow = new DNotifier.Workflow({
  name: "blog-article-writer",
  description: "Create, refine, and optimize a blog post",
  observability: true,
  async entry(ctx) {
    const brief = ctx.input;
    ctx.state.blogBrief = brief;

    const creation = await ctx.runAgent("content-creator");
    ctx.state.stage = "content_creation";

    const clarity = await ctx.runAgent("clarity-editor", {
      input: { content: creation.content },
    });
    ctx.state.stage = "clarity";

    const seo = await ctx.runAgent("seo-optimizer", {
      input: {
        content: clarity.content,
        topic: brief.topic,
        keywords: brief.keywords,
      },
    });
    ctx.state.stage = "seo";

    return {
      article: seo.content,
      seo: {
        metaTitle: seo.metaTitle,
        metaDescription: seo.metaDescription,
        slug: seo.slug,
        keywords: seo.keywords,
      },
    };
  },
}).registerAgents({
  "content-creator": contentCreatorAgent,
  "clarity-editor": clarityEditorAgent,
  "seo-optimizer": seoOptimizerAgent,
});
```

## Parallel pipeline (`runAgents`)

```js
async function blogArticleParallelEntry(ctx) {
  const brief = ctx.input;
  ctx.state.blogBrief = brief;

  const planning = await ctx.runAgents([
    { name: "outline-planner" },
    { name: "keyword-researcher" },
  ]);
  const [outlinePlan, keywordResearch] = planning.results;

  const creation = await ctx.runAgent("content-creator", {
    input: {
      ...brief,
      outline: outlinePlan.outline,
      keywords: keywordResearch.keywords,
    },
  });

  const refinement = await ctx.runAgents([
    { name: "clarity-editor", input: { content: creation.content } },
    {
      name: "seo-optimizer",
      input: { content: creation.content, topic: brief.topic },
    },
  ]);
  const [clarity, seo] = refinement.results;

  return { article: clarity.content, seo };
}
```

See [**Parallel agents & shared state**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/step-graph.md) for more patterns.

## Module layout suggestion

```
workflows/
  intent-router/
    agents/
      intent-agent.js
      general-agent.js
    workflow.js
  blog-writer/
    agents/
      content-creator.js
      clarity-editor.js
      seo-optimizer.js
    workflow.js
```

## Learn more

* [**Parallel agents & shared state**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/step-graph.md)
* [**Run a workflow**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/run-workflow/javascript-typescript.md)
* [**Example: Multi-agent pipeline**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-multi-agent/javascript-typescript.md)
