-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(root): add @novu/chat-sdk-adapter Chat SDK platform adapter fixes NV-8063 #11593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3fd05cd
feat(root): add @chat-adapter/novu Chat SDK platform adapter fixes NV…
scopsy a5bcaac
chore(root): rename package to @novu/chat-sdk-adapter fixes NV-8063
scopsy 934fd65
refactor(root): use official @chat-adapter/state-memory; fix adapter …
scopsy 4ead536
feat(chat-adapter): expose Novu subscriber via getNovuContext and get…
scopsy fbe1465
fix
scopsy 960a8fb
feat(chat-adapter): add card reply testing to playground fixes NV-8063
scopsy 16eda18
feat(chat-adapter): expand NovuContext and wire outbound files fixes …
scopsy f3b9514
Update agent.ts
scopsy b987ef7
fix(chat-adapter): optimistic snapshot patches and review fixes NV-8063
scopsy c530b5a
fix(chat-adapter): address PR review for dedupe, HMAC, and fetchMessa…
scopsy 20a7e51
style(chat-adapter): align quote style with monorepo biome config
scopsy 04d8843
Update settings.json
scopsy 5e7d1f5
Update package.json
scopsy 1b7c84a
Update package.json
scopsy dff2934
chore(chat-adapter): release v0.0.2
scopsy e6a3334
chore(chat-adapter): align package metadata with Chat SDK guidelines …
scopsy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| tsconfig.tsbuildinfo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # @novu/chat-sdk-adapter | ||
|
|
||
| A [Chat SDK](https://www.npmjs.com/package/chat) platform adapter that exposes **all of Novu's | ||
| normalized chat channels — Slack, WhatsApp, Microsoft Teams, Telegram, and Email — as a single | ||
| platform**. Novu does the per-channel normalization (one `Conversation` + `Subscriber` + history) | ||
| and calls your bridge; your Chat SDK app is the brain. One handler set serves every channel with no | ||
| per-channel code. | ||
|
|
||
| ``` | ||
| End-user channels ──platform webhooks──▶ NOVU (normalize) ──POST AgentBridgeRequest (HMAC)──▶ | ||
| your Chat SDK app (@novu/chat-sdk-adapter) ──AgentReplyPayload → POST /v1/agents/:id/reply──▶ NOVU ──▶ channel | ||
| ``` | ||
|
|
||
| ## Install | ||
|
|
||
| ```bash | ||
| npm install @novu/chat-sdk-adapter chat @chat-adapter/state-memory | ||
| ``` | ||
|
|
||
| `chat` is a peer dependency. `react` is an optional peer (only needed for JSX cards). | ||
| A `StateAdapter` is required by the Chat SDK — use the official `@chat-adapter/state-memory` | ||
| for local/single-instance, or a shared adapter (`@chat-adapter/state-redis`, | ||
| `@chat-adapter/state-ioredis`, `@chat-adapter/state-pg`) for production. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```ts | ||
| import { Chat } from 'chat'; | ||
| import { createMemoryState } from '@chat-adapter/state-memory'; | ||
| import { createNovuAdapter, getNovuContext } from '@novu/chat-sdk-adapter'; | ||
|
|
||
| const novu = createNovuAdapter({ | ||
| apiKey: process.env.NOVU_SECRET_KEY!, // Authorization for reply POSTs | ||
| agentIdentifier: 'support-agent', | ||
| bridgeSecret: process.env.NOVU_SECRET_KEY!, // verifies inbound HMAC | ||
| // apiBaseUrl: 'https://eu.api.novu.co', // defaults to https://api.novu.co | ||
| // bridgeUrl: 'https://my-app.com/api/novu',// optional boot-time bridge registration | ||
| }); | ||
|
|
||
| const chat = new Chat({ | ||
| userName: 'support', | ||
| adapters: { novu }, | ||
| state: createMemoryState(), // official @chat-adapter/state-memory; single-instance only | ||
| }); | ||
|
|
||
| chat.onNewMention(async (thread, message) => { | ||
| if (thread.isDM) { | ||
| await thread.post(`Hi (DM)! You said: ${message.text}`); | ||
| } else { | ||
| await thread.post(`Hi! You said: ${message.text}`); | ||
| } | ||
| }); | ||
|
|
||
| chat.onSubscribedMessage(async (thread, message) => { | ||
| await thread.post(`echo: ${message.text}`); | ||
|
|
||
| // Opt-in, Novu-only capabilities: | ||
| const ctx = getNovuContext(thread); | ||
|
|
||
| // Full Novu subscriber (email, phone, avatar, locale, custom `data`): | ||
| const subscriber = await ctx.getSubscriber(); | ||
|
|
||
| // Canonical transcript for LLM context: | ||
| const history = await ctx.getHistory(); | ||
| const ticketId = await ctx.getMetadata('ticketId'); | ||
|
|
||
| if (subscriber?.data?.plan === 'enterprise') { | ||
| await thread.post('Priority support enabled.'); | ||
| } | ||
|
|
||
| if (ctx.platform === 'whatsapp') { | ||
| await ctx.trigger('escalation-email', { payload: { text: message.text } }); | ||
| } | ||
| }); | ||
|
|
||
| // Post markdown with a file attachment: | ||
| await thread.post({ | ||
| markdown: 'See attached report', | ||
| files: [{ filename: 'report.txt', data: Buffer.from('...'), mimeType: 'text/plain' }], | ||
| }); | ||
|
|
||
| // Portable, SDK-native identity lookup — works for any Chat SDK code, | ||
| // returns the standard `UserInfo` shape (id, name, email, avatarUrl): | ||
| const user = await novu.getUser(message.author.userId); | ||
|
|
||
| await chat.initialize(); | ||
| ``` | ||
|
|
||
| Wire the webhook route to `novu.handleWebhook(request)` (any Web `Request`/`Response` runtime — | ||
| Next.js route handlers, Hono, etc.). | ||
|
|
||
| ## Behavior & v1 scope | ||
|
|
||
| - **In:** messages, button actions, reactions, full Novu history, subscriber identity, platform | ||
| awareness, dedup (per `deliveryId`). | ||
| - **Subscriber:** portable identity rides each message's `author`; the SDK-native | ||
| `adapter.getUser(userId)` maps the subscriber to `UserInfo` (id/name/email/avatar); and the full | ||
| rich profile (`phone`, `locale`, custom `data`) is available via | ||
| `getNovuContext(thread).getSubscriber()`. | ||
| - **Conversation & history:** `getNovuContext(thread).getConversation()` for status/metadata; | ||
| `getHistory()` for the canonical Novu transcript (best for LLM context); `getMetadata(key)` to | ||
| read conversation metadata; `getEmailContext()` on email threads. | ||
| - **Out:** markdown, cards, **files** (via postable `files`/`attachments`), edits (in-place), reaction adds, edit-based streaming (via the chat | ||
| package's built-in cadence), plus opt-in `getNovuContext().trigger / setMetadata / clearMetadata / resolve`. | ||
| - **Routing (recommended):** do **not** register `onDirectMessage` — use `onNewMention` for the | ||
| first message (`thread.isDM` for DM vs channel) and `onSubscribedMessage` for all follow-ups. | ||
| The adapter pre-subscribes when `messageCount > 1` (Novu history always includes the | ||
| current message, so history length is not used). If you register | ||
| `onDirectMessage`, Chat SDK sends **every** DM there and `onSubscribedMessage` never runs for DMs. | ||
| - **Security:** the inbound HMAC (`novu-signature`) is verified over the raw body; the reply URL is | ||
| **derived from your config** and the request's `replyUrl` is ignored, so a forged request can never | ||
| exfiltrate your `apiKey`. | ||
| - **Not implemented in v1:** `deleteMessage`, modals, outbound-initiated DMs (`openDM`), code-driven | ||
| channel provisioning, Novu-side turn serialization. | ||
|
|
||
| ## State | ||
|
|
||
| This adapter does not ship its own state layer — it relies on the Chat SDK's standard | ||
| `StateAdapter`. Use the official memory adapter `@chat-adapter/state-memory` | ||
| (`createMemoryState()`), which is in-process and safe for a single instance. For | ||
| horizontally-scaled or serverless bridges with more than one warm instance, pass a shared | ||
| state adapter (`@chat-adapter/state-redis`, `@chat-adapter/state-ioredis`, or | ||
| `@chat-adapter/state-pg`) to `new Chat({ state })` so locks and dedup are correct. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| { | ||
| "name": "@novu/chat-sdk-adapter", | ||
| "version": "0.0.1", | ||
| "private": true, | ||
| "type": "module", | ||
| "description": "Novu adapter for the Chat SDK — expose all of Novu's normalized chat channels (Slack, WhatsApp, Teams, Telegram, Email) as a single Chat SDK platform adapter", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "files": [ | ||
| "dist/" | ||
| ], | ||
| "scripts": { | ||
| "afterinstall": "pnpm build", | ||
| "prebuild": "rimraf dist tsconfig.tsbuildinfo", | ||
| "build": "tsc -p tsconfig.json", | ||
| "watch:build": "tsc -p tsconfig.json -w", | ||
| "test": "vitest run", | ||
| "test:watch": "vitest", | ||
| "check": "biome check .", | ||
| "check:fix": "biome check --write ." | ||
| }, | ||
| "peerDependencies": { | ||
| "chat": ">=4.30.0", | ||
| "react": ">=18.0.0 || >=19.0.0" | ||
| }, | ||
| "peerDependenciesMeta": { | ||
| "react": { | ||
| "optional": true | ||
| } | ||
| }, | ||
| "devDependencies": { | ||
| "@chat-adapter/state-memory": "4.30.0", | ||
| "@types/react": "^19.0.0", | ||
| "rimraf": "~3.0.2", | ||
| "typescript": "5.6.2", | ||
| "vitest": "^1.2.1" | ||
| }, | ||
| "nx": { | ||
| "tags": [ | ||
| "type:package" | ||
| ] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "name": "@novu/chat-sdk-adapter", | ||
| "sourceRoot": "packages/chat-adapter/src", | ||
| "projectType": "library", | ||
| "targets": { | ||
| "build": { | ||
| "executor": "nx:run-commands", | ||
| "options": { | ||
| "command": "pnpm --filter @novu/chat-sdk-adapter run build", | ||
| "cwd": "{workspaceRoot}" | ||
| } | ||
| }, | ||
| "test": { | ||
| "executor": "nx:run-commands", | ||
| "options": { | ||
| "command": "pnpm --filter @novu/chat-sdk-adapter run test", | ||
| "cwd": "{workspaceRoot}" | ||
| } | ||
| }, | ||
| "lint": { | ||
| "executor": "nx:run-commands", | ||
| "options": { | ||
| "command": "npx biome lint packages/chat-adapter" | ||
| } | ||
| } | ||
| }, | ||
| "tags": ["type:package"] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.