> 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/rag-assistant/javascript-typescript.md).

# JavaScript / TypeScript

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

const SENDER = "support-agent";

const articles = [
  {
    recordId: "refund-policy",
    content: "Refund requests are processed within 5 business days after approval.",
    metadata: { source: "help-center", topic: "billing" },
  },
  {
    recordId: "password-reset",
    content: "Reset your password from Settings > Security > Send reset email.",
    metadata: { source: "help-center", topic: "account" },
  },
];

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

await notifier.connect();

// 1. Index documents
for (const doc of articles) {
  await notifier.addDocument({
    senderId: SENDER,
    recordId: doc.recordId,
    content: doc.content,
    type: "text",
    metadata: doc.metadata,
  });
  console.log("Indexed:", doc.recordId);
}

// 2. Semantic search
const question = "How long do refunds take?";
const hits = await notifier.search({
  senderId: SENDER,
  query: question,
  limit: 3,
  minSimilarity: 0.6,
  filterbySource: "help-center",
});
console.log("Search hits:", hits);

// 3. RAG answer
const answer = await notifier.sendAI({
  senderId: SENDER,
  message: {
    useKnowledgeBase: true,
    messages: [
      { role: "system", content: "Answer using only the knowledge base. Be concise." },
      { role: "user", content: question },
    ],
  },
  saveHistory: true,
});

console.log("RAG answer:", JSON.stringify(answer, null, 2));

// Cleanup (optional)
// await notifier.deleteDocument({ senderId: SENDER, recordId: "refund-policy" });
```

## Updating a document

```js
await notifier.updateDocument({
  senderId: SENDER,
  recordId: "refund-policy",
  content: "Refunds are processed within 3 business days.",
  type: "text",
});
```

## Listing documents

```js
const list = await notifier.listDocuments({
  senderId: SENDER,
  limit: 20,
  offset: 0,
});
```
