> 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/ai/chat-style-messages/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

Send structured **role-based messages** and optionally enable **knowledge-base retrieval**.

## Support agent with system prompt

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

const USER_ID = "user-123";

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

await notifier.connect();

const response = await notifier.sendAI({
  senderId: USER_ID,
  message: {
    useKnowledgeBase: false,
    messages: [
      {
        role: "system",
        content:
          "You are a helpful support agent. Answer in under 100 words.",
      },
      { role: "user", content: "How do I reset my password?" },
    ],
  },
  saveHistory: true,
});

console.log(response?.data?.content ?? response);
```

## RAG-enabled Q\&A

```js
const response = await notifier.sendAI({
  senderId: USER_ID,
  message: {
    useKnowledgeBase: true,
    messages: [
      {
        role: "system",
        content:
          "Answer using only the provided knowledge. If unsure, say you do not know.",
      },
      { role: "user", content: "What is the refund timeline?" },
    ],
  },
});
```

## Few-shot classification

```js
const response = await notifier.sendAI({
  senderId: USER_ID,
  message: {
    messages: [
      {
        role: "system",
        content: 'Classify intent. Reply JSON only: {"intent":"billing"|"technical"}',
      },
      { role: "user", content: "My invoice is wrong" },
      { role: "assistant", content: '{"intent":"billing"}' },
      { role: "user", content: "The app crashes on launch" },
    ],
  },
  saveHistory: false,
});
```

## Multi-turn in one call (no session)

You can include prior turns in `messages` manually. For server-managed continuity, prefer [**Sessions**](https://dnotifier.gitbook.io/product-docs/ai/sessions/javascript-typescript.md) with `sessionId`.

## TypeScript types

```ts
type ChatMessage = {
  role: "system" | "user" | "assistant";
  content: string;
};

type ChatAiMessage = {
  useKnowledgeBase?: boolean;
  messages: ChatMessage[];
};

await notifier.sendAI({
  senderId: USER_ID,
  message: {
    useKnowledgeBase: true,
    messages: [
      { role: "system", content: "You are helpful." },
      { role: "user", content: "Hello" },
    ],
  } satisfies ChatAiMessage,
});
```

## Learn more

* [**Semantic search**](https://dnotifier.gitbook.io/product-docs/ai/semantic-search/javascript-typescript.md) — Manual retrieval before `sendAI`
* [**Manage documents**](https://dnotifier.gitbook.io/product-docs/ai/manage-documents/javascript-typescript.md) — Index content for RAG
