> 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/define-agent/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

Create reusable **named agents** for workflow pipelines.

## Intent classification agent

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

const intentAgent = DNotifier.defineAgent({
  name: "intent-agent",
  provider: "open_ai",
  model: "gpt-4o",
  async run(ctx) {
    const text =
      typeof ctx.input === "string"
        ? ctx.input
        : ctx.input?.text ?? ctx.input?.question ?? "";

    if (!String(text).trim()) {
      throw new DNotifier.WorkflowError("intent-agent: empty input", {
        agentName: "intent-agent",
      });
    }

    const aiResponse = await ctx.sendAI({
      message: {
        useKnowledgeBase: false,
        messages: [
          {
            role: "system",
            content:
              'Classify the user message. Reply JSON only: {"intent":"search"} for documentation lookup, or {"intent":"general"} for general chat.',
          },
          { role: "user", content: String(text) },
        ],
      },
      saveHistory: false,
      label: "Intent classification",
    });

    const content =
      aiResponse?.data?.content ??
      aiResponse?.content ??
      JSON.stringify(aiResponse);

    let parsed;
    try {
      parsed = JSON.parse(content);
    } catch {
      parsed = {
        intent: String(content).toLowerCase().includes("search")
          ? "search"
          : "general",
      };
    }

    const intent = parsed?.intent === "search" ? "search" : "general";
    ctx.state.lastIntent = intent;

    return { intent, raw: parsed };
  },
});
```

## General Q\&A agent

```js
const generalAgent = DNotifier.defineAgent({
  name: "general-agent",
  async run(ctx) {
    const question =
      typeof ctx.input === "string"
        ? ctx.input
        : ctx.input?.question ?? JSON.stringify(ctx.input);

    const aiResponse = await ctx.sendAI({
      message: {
        useKnowledgeBase: false,
        messages: [
          { role: "system", content: "You are a helpful assistant. Answer concisely." },
          { role: "user", content: String(question) },
        ],
      },
      saveHistory: false,
      label: "General Q&A",
    });

    const answer =
      aiResponse?.data?.content ??
      aiResponse?.content ??
      JSON.stringify(aiResponse);

    ctx.state.generalAnswer = answer;
    return { answer };
  },
});
```

## Tool agent (no custom `run`)

```js
const add = notifier.tools.register({
  name: "math.add",
  description: "Add two numbers",
  inputSchema: {
    type: "object",
    properties: { a: { type: "number" }, b: { type: "number" } },
    required: ["a", "b"],
  },
  async execute(input) {
    return { sum: Number(input.a) + Number(input.b) };
  },
});

const mathAgent = DNotifier.defineAgent({
  name: "math-assistant",
  instructions: "Use math.add when the user asks to add numbers.",
  provider: "open_ai",
  model: "gpt-4o",
  tools: [add],
  permissions: { "math.add": ["execute"] },
});

const result = await notifier.runAgent(mathAgent, {
  input: "What is 21 + 21?",
});
```

Full walkthrough: [**Tools & MCP**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/javascript-typescript.md) · [**Local tool example**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-tool-agent/javascript-typescript.md)

## Export pattern

```js
// agents/intent-agent.js
export const intentAgent = DNotifier.defineAgent({ name: "intent-agent", async run(ctx) { /* ... */ } });

// workflow.js
import { intentAgent } from "./agents/intent-agent.js";
```

## Learn more

* [**Tools & MCP**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/javascript-typescript.md)
* [**Build a workflow**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/build-workflow/javascript-typescript.md)
* [**Example: Intent router**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-intent-router/javascript-typescript.md)
