@dnotifier-realtime/dnotifier 1.1.10 → 1.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +466 -41
- package/dist/cjs/dnotifier.js +1 -1
- package/dist/cjs/providers/openai.js +1 -1
- package/dist/cjs/transport/httpTransport.js +1 -1
- package/dist/cjs/transport/websocketTransport.js +1 -1
- package/dist/cjs/utils/buffer.js +1 -1
- package/dist/cjs/utils/frame.js +1 -1
- package/dist/cjs/utils/id.js +1 -1
- package/dist/cjs/utils/payload.js +1 -1
- package/dist/config/endpoints.d.ts +2 -2
- package/dist/config/endpoints.d.ts.map +1 -1
- package/dist/config/endpoints.js +1 -1
- package/dist/dnotifier.cjs +7 -33
- package/dist/dnotifier.js +1 -1
- package/dist/providers/openai.js +1 -1
- package/dist/transport/httpTransport.js +1 -1
- package/dist/transport/websocketTransport.js +1 -1
- package/dist/utils/buffer.js +1 -1
- package/dist/utils/frame.js +1 -1
- package/dist/utils/id.js +1 -1
- package/dist/utils/logs.js +1 -1
- package/dist/utils/payload.js +1 -1
- package/dist/workflow/agent.js +1 -1
- package/dist/workflow/context.d.ts.map +1 -1
- package/dist/workflow/context.js +1 -1
- package/dist/workflow/errors.js +1 -1
- package/dist/workflow/ids.js +1 -1
- package/dist/workflow/jsonPayload.js +1 -1
- package/dist/workflow/mockObservabilityClient.js +1 -1
- package/dist/workflow/observability.js +1 -1
- package/dist/workflow/runner.js +1 -1
- package/dist/workflow/stepTypes.js +1 -1
- package/dist/workflow/workflow.js +1 -1
- package/dist/workflow/workflowObservabilityClient.d.ts.map +1 -1
- package/dist/workflow/workflowObservabilityClient.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,42 +1,338 @@
|
|
|
1
|
-
# DNotifier SDK
|
|
1
|
+
# DNotifier JavaScript SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**DNotifier** is a real-time notification and messaging SDK for **Node.js** and **browsers**. It supports **WebSocket** and **HTTP** transports, **binary streaming**, framed packets, an **AI pipeline**, **knowledge-base (RAG)** APIs, optional **session logging**, and **multi-step workflows** with agents and observability.
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Features
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
9
|
+
- WebSocket and HTTP transport
|
|
10
|
+
- Secure authentication handshake
|
|
11
|
+
- Real-time messaging (text and binary)
|
|
12
|
+
- Large payloads with automatic chunking and reassembly
|
|
13
|
+
- AI: `sendAI`, `fetchAIHistory`, semantic `search`
|
|
14
|
+
- Knowledge base: add, update, get, list, delete, and search documents
|
|
15
|
+
- Chat history fetch and delete
|
|
16
|
+
- Optional SDK session logging (`logs: true`)
|
|
17
|
+
- Workflows: named agents, sequential pipelines, dashboard observability
|
|
18
|
+
- Plan limits from auth (`getPlanLimits`)
|
|
17
19
|
|
|
18
20
|
---
|
|
19
21
|
|
|
20
|
-
##
|
|
22
|
+
## Installation
|
|
21
23
|
|
|
22
24
|
```bash
|
|
23
|
-
npm
|
|
25
|
+
npm install @dnotifier-realtime/dnotifier
|
|
24
26
|
```
|
|
25
27
|
|
|
26
28
|
---
|
|
27
29
|
|
|
28
|
-
##
|
|
30
|
+
## Quick start
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
```js
|
|
33
|
+
import { DNotifier } from "@dnotifier-realtime/dnotifier";
|
|
34
|
+
|
|
35
|
+
const notifier = new DNotifier({
|
|
36
|
+
appId: "your-app-id",
|
|
37
|
+
secret: "your-app-secret",
|
|
38
|
+
transport: "ws", // or "http"
|
|
39
|
+
userId: "user-123",
|
|
40
|
+
onConnected: () => console.log("Connected"),
|
|
41
|
+
onMessage: (msg) => {
|
|
42
|
+
console.log("From:", msg.metadata.sender);
|
|
43
|
+
console.log("Payload:", msg.payload.toJSON());
|
|
44
|
+
},
|
|
45
|
+
onDisconnected: () => console.log("Disconnected"),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
await notifier.connect();
|
|
49
|
+
|
|
50
|
+
notifier.send({
|
|
51
|
+
senderId: "user-123",
|
|
52
|
+
receiverId: "user-456",
|
|
53
|
+
data: { type: "text", text: "Hello from DNotifier" },
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
For **Node.js WebSocket**, pass a `WebSocket` implementation:
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
import WebSocket from "ws";
|
|
61
|
+
|
|
62
|
+
const notifier = new DNotifier({
|
|
63
|
+
appId: "your-app-id",
|
|
64
|
+
secret: "your-app-secret",
|
|
65
|
+
transport: "ws",
|
|
66
|
+
userId: "user-123",
|
|
67
|
+
WebSocketImpl: WebSocket,
|
|
68
|
+
onConnected: () => {},
|
|
69
|
+
onMessage: () => {},
|
|
70
|
+
onDisconnected: () => {},
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Connection and configuration
|
|
77
|
+
|
|
78
|
+
| Option | Type | Description |
|
|
79
|
+
|--------|------|-------------|
|
|
80
|
+
| `appId` | `string` | Your DNotifier application id |
|
|
81
|
+
| `secret` | `string` | Application secret |
|
|
82
|
+
| `transport` | `"ws"` \| `"http"` | `"ws"` for realtime messaging; `"http"` for RPC-style AI/RAG calls |
|
|
83
|
+
| `userId` | `string` | End-user or agent id for auth and routing |
|
|
84
|
+
| `logs` | `boolean` | When `true`, tracks AI sessions in the DNotifier logs dashboard |
|
|
85
|
+
| `url` | `string` | Optional custom WebSocket URL |
|
|
86
|
+
| `WebSocketImpl` | `class` | Required in Node when using `transport: "ws"` |
|
|
87
|
+
| `onConnected` | `function` | Called when the client is ready |
|
|
88
|
+
| `onMessage` | `function` | Called with `{ metadata, payload }` for incoming messages |
|
|
89
|
+
| `onDisconnected` | `function` | Called when the connection closes |
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
await notifier.connect();
|
|
93
|
+
|
|
94
|
+
const limits = notifier.getPlanLimits();
|
|
95
|
+
console.log(limits?.aiEnabled, limits?.maxAIRequestsPerMonth);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
After `connect()`, these properties are available:
|
|
99
|
+
|
|
100
|
+
| Property | Description |
|
|
101
|
+
|----------|-------------|
|
|
102
|
+
| `isConnected` | Whether the client is connected |
|
|
103
|
+
| `aiEnabled` | Whether AI is enabled for the current plan |
|
|
104
|
+
| `authToken` | Auth token from the last successful connect |
|
|
105
|
+
| `messageSizeLimit` | Max message size in bytes (from plan) |
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Real-time messaging
|
|
110
|
+
|
|
111
|
+
### Text and structured messages
|
|
112
|
+
|
|
113
|
+
Use any `type` in `data` that fits your app (`text`, `image`, `audio`, `doc`, etc.):
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
await notifier.send({
|
|
117
|
+
senderId: "user-123",
|
|
118
|
+
receiverId: "user-456",
|
|
119
|
+
data: {
|
|
120
|
+
type: "text",
|
|
121
|
+
text: "hello from dnotifier sdk!",
|
|
122
|
+
},
|
|
123
|
+
saveHistory: true, // default true
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Send to multiple receivers:
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
await notifier.send({
|
|
131
|
+
senderId: "user-123",
|
|
132
|
+
receiverIds: ["user-456", "user-789"],
|
|
133
|
+
data: { type: "text", text: "Hello everyone" },
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Images, audio, and binary
|
|
138
|
+
|
|
139
|
+
Prefer `send()` with a typed payload. The SDK chunks large payloads automatically.
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
import { readFileSync } from "fs";
|
|
143
|
+
|
|
144
|
+
const imageBytes = readFileSync("./photo.png");
|
|
145
|
+
|
|
146
|
+
await notifier.send({
|
|
147
|
+
senderId: "user-123",
|
|
148
|
+
receiverId: "user-456",
|
|
149
|
+
data: {
|
|
150
|
+
type: "image",
|
|
151
|
+
content: imageBytes,
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Handle incoming messages in `onMessage`:
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
onMessage: (msg) => {
|
|
160
|
+
const body = msg.payload.toJSON();
|
|
161
|
+
if (body?.type === "image") {
|
|
162
|
+
// body.content — image bytes or base64 depending on your app convention
|
|
163
|
+
} else if (body?.type === "text") {
|
|
164
|
+
console.log(body.text);
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Raw binary (`sendBinary`)
|
|
170
|
+
|
|
171
|
+
```js
|
|
172
|
+
notifier.sendBinary({
|
|
173
|
+
senderId: "user-123",
|
|
174
|
+
receiverIds: ["user-456"],
|
|
175
|
+
buffer: new Uint8Array([0x00, 0x01, 0x02]),
|
|
176
|
+
type: "binary",
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## AI
|
|
183
|
+
|
|
184
|
+
Requires `aiEnabled` on your plan. Use `transport: "http"` for request/response AI calls.
|
|
185
|
+
|
|
186
|
+
### Simple prompt
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
const response = await notifier.sendAI({
|
|
190
|
+
senderId: "user-123",
|
|
191
|
+
message: { text: "Summarize our refund policy in two sentences." },
|
|
192
|
+
});
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Chat-style messages
|
|
196
|
+
|
|
197
|
+
```js
|
|
198
|
+
const response = await notifier.sendAI({
|
|
199
|
+
senderId: "user-123",
|
|
200
|
+
message: {
|
|
201
|
+
useKnowledgeBase: true,
|
|
202
|
+
messages: [
|
|
203
|
+
{ role: "system", content: "You are a helpful support agent." },
|
|
204
|
+
{ role: "user", content: "How do I reset my password?" },
|
|
205
|
+
],
|
|
206
|
+
},
|
|
207
|
+
saveHistory: true,
|
|
208
|
+
});
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Continue an existing session
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
const first = await notifier.sendAI({
|
|
215
|
+
senderId: "user-123",
|
|
216
|
+
message: { text: "Start a new support session." },
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const sessionId = first?.metadata?.packet?.id;
|
|
220
|
+
|
|
221
|
+
await notifier.sendAI({
|
|
222
|
+
senderId: "user-123",
|
|
223
|
+
sessionId,
|
|
224
|
+
message: { text: "Follow-up question in the same session." },
|
|
225
|
+
});
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### AI history
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
const history = await notifier.fetchAIHistory({ senderId: "user-123" });
|
|
232
|
+
|
|
233
|
+
await notifier.deleteAIHistoryMessage({
|
|
234
|
+
senderId: "user-123",
|
|
235
|
+
messageId: "message-id-to-delete",
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
## Knowledge base (RAG)
|
|
242
|
+
|
|
243
|
+
```js
|
|
244
|
+
// Add
|
|
245
|
+
await notifier.addDocument({
|
|
246
|
+
senderId: "user-123",
|
|
247
|
+
recordId: "doc-001",
|
|
248
|
+
content: "Refund requests are processed within 5 business days.",
|
|
249
|
+
type: "text",
|
|
250
|
+
metadata: { source: "help-center" },
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
// Update
|
|
254
|
+
await notifier.updateDocument({
|
|
255
|
+
senderId: "user-123",
|
|
256
|
+
recordId: "doc-001",
|
|
257
|
+
content: "Updated refund policy text...",
|
|
258
|
+
type: "text",
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// Get one document
|
|
262
|
+
const doc = await notifier.getDocument({
|
|
263
|
+
senderId: "user-123",
|
|
264
|
+
recordId: "doc-001",
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
// List
|
|
268
|
+
const list = await notifier.listDocuments({
|
|
269
|
+
senderId: "user-123",
|
|
270
|
+
limit: 20,
|
|
271
|
+
offset: 0,
|
|
272
|
+
type: "text",
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// Semantic search
|
|
276
|
+
const hits = await notifier.search({
|
|
277
|
+
senderId: "user-123",
|
|
278
|
+
query: "how long do refunds take",
|
|
279
|
+
limit: 5,
|
|
280
|
+
minSimilarity: 0.7,
|
|
281
|
+
filterbySource: "help-center",
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// Delete
|
|
285
|
+
await notifier.deleteDocument({
|
|
286
|
+
senderId: "user-123",
|
|
287
|
+
recordId: "doc-001",
|
|
288
|
+
});
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## Chat history
|
|
294
|
+
|
|
295
|
+
```js
|
|
296
|
+
await notifier.fetchChatHistory({
|
|
297
|
+
senderId: "user-123",
|
|
298
|
+
receiverIds: ["user-456"],
|
|
299
|
+
});
|
|
300
|
+
// Server responds via onMessage with the history payload.
|
|
301
|
+
|
|
302
|
+
await notifier.deleteChatHistoryMessage({
|
|
303
|
+
senderId: "user-123",
|
|
304
|
+
messageId: "message-id-to-delete",
|
|
305
|
+
});
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
---
|
|
309
|
+
|
|
310
|
+
## Session logging
|
|
311
|
+
|
|
312
|
+
Enable automatic AI session tracking in the DNotifier dashboard:
|
|
313
|
+
|
|
314
|
+
```js
|
|
315
|
+
const notifier = new DNotifier({
|
|
316
|
+
appId: "your-app-id",
|
|
317
|
+
secret: "your-app-secret",
|
|
318
|
+
transport: "http",
|
|
319
|
+
userId: "user-123",
|
|
320
|
+
logs: true,
|
|
321
|
+
onConnected: () => {},
|
|
322
|
+
onMessage: () => {},
|
|
323
|
+
onDisconnected: () => {},
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
When `logs: true`, `sendAI` reports session start, completion, and token usage to DNotifier.
|
|
32
328
|
|
|
33
329
|
---
|
|
34
330
|
|
|
35
|
-
##
|
|
331
|
+
## Workflows and agents
|
|
36
332
|
|
|
37
|
-
|
|
333
|
+
Build deterministic, multi-step AI pipelines with **named agents**, shared **workflow state**, and optional **observability** (execution and step telemetry in the DNotifier workflow dashboard).
|
|
38
334
|
|
|
39
|
-
### Define agents
|
|
335
|
+
### 1. Define agents
|
|
40
336
|
|
|
41
337
|
```js
|
|
42
338
|
import { DNotifier } from "@dnotifier-realtime/dnotifier";
|
|
@@ -45,28 +341,42 @@ const intentAgent = DNotifier.defineAgent({
|
|
|
45
341
|
name: "intent-agent",
|
|
46
342
|
async run(ctx) {
|
|
47
343
|
await ctx.sendAI({
|
|
48
|
-
message: { text: "
|
|
344
|
+
message: { text: "Classify: search or general?" },
|
|
49
345
|
saveHistory: false,
|
|
50
346
|
label: "Intent classification",
|
|
51
347
|
});
|
|
52
348
|
return { intent: "search" };
|
|
53
349
|
},
|
|
54
350
|
});
|
|
351
|
+
|
|
352
|
+
const generalAgent = DNotifier.defineAgent({
|
|
353
|
+
name: "general-agent",
|
|
354
|
+
async run(ctx) {
|
|
355
|
+
const question = ctx.input?.question ?? ctx.input;
|
|
356
|
+
return { answer: `You asked: ${question}` };
|
|
357
|
+
},
|
|
358
|
+
});
|
|
55
359
|
```
|
|
56
360
|
|
|
57
|
-
### Define
|
|
361
|
+
### 2. Define a workflow
|
|
58
362
|
|
|
59
363
|
```js
|
|
60
364
|
const workflow = new DNotifier.Workflow({
|
|
61
365
|
name: "intent-router",
|
|
62
366
|
description: "Route user input to search or general Q&A",
|
|
63
|
-
observability: true,
|
|
367
|
+
observability: true,
|
|
64
368
|
async entry(ctx) {
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
369
|
+
const { intent } = await ctx.runAgent("intent-agent");
|
|
370
|
+
|
|
371
|
+
if (intent === "search") {
|
|
372
|
+
await ctx.search({
|
|
373
|
+
query: String(ctx.input),
|
|
374
|
+
limit: 5,
|
|
375
|
+
label: "Knowledge search",
|
|
376
|
+
});
|
|
68
377
|
return { branch: "search" };
|
|
69
378
|
}
|
|
379
|
+
|
|
70
380
|
const answer = await ctx.runAgent("general-agent", {
|
|
71
381
|
input: { question: ctx.input },
|
|
72
382
|
});
|
|
@@ -78,7 +388,7 @@ const workflow = new DNotifier.Workflow({
|
|
|
78
388
|
});
|
|
79
389
|
```
|
|
80
390
|
|
|
81
|
-
### Run
|
|
391
|
+
### 3. Run the workflow
|
|
82
392
|
|
|
83
393
|
```js
|
|
84
394
|
const notifier = new DNotifier({
|
|
@@ -86,6 +396,9 @@ const notifier = new DNotifier({
|
|
|
86
396
|
secret: "your-app-secret",
|
|
87
397
|
transport: "http",
|
|
88
398
|
userId: "user-123",
|
|
399
|
+
onConnected: () => {},
|
|
400
|
+
onMessage: () => {},
|
|
401
|
+
onDisconnected: () => {},
|
|
89
402
|
});
|
|
90
403
|
|
|
91
404
|
await notifier.connect();
|
|
@@ -95,27 +408,139 @@ const run = await notifier.runWorkflow({
|
|
|
95
408
|
input: "How does messaging work?",
|
|
96
409
|
});
|
|
97
410
|
|
|
98
|
-
console.log(run.executionId
|
|
411
|
+
console.log(run.executionId); // present when observability: true
|
|
412
|
+
console.log(run.result); // { branch: "search" } or similar
|
|
413
|
+
console.log(run.state); // shared state from the run
|
|
99
414
|
```
|
|
100
415
|
|
|
101
|
-
###
|
|
416
|
+
### Multi-stage pipeline example
|
|
102
417
|
|
|
103
|
-
|
|
418
|
+
```js
|
|
419
|
+
const createAgent = DNotifier.defineAgent({
|
|
420
|
+
name: "content-creator",
|
|
421
|
+
async run(ctx) {
|
|
422
|
+
const brief = ctx.input;
|
|
423
|
+
const aiResponse = await ctx.sendAI({
|
|
424
|
+
message: {
|
|
425
|
+
useKnowledgeBase: false,
|
|
426
|
+
messages: [
|
|
427
|
+
{ role: "system", content: "Write a blog draft in markdown." },
|
|
428
|
+
{ role: "user", content: `Topic: ${brief.topic}` },
|
|
429
|
+
],
|
|
430
|
+
},
|
|
431
|
+
saveHistory: false,
|
|
432
|
+
label: "Draft creation",
|
|
433
|
+
});
|
|
434
|
+
const content = aiResponse?.data?.content ?? "";
|
|
435
|
+
ctx.state.draft = content;
|
|
436
|
+
return { content };
|
|
437
|
+
},
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
const workflow = new DNotifier.Workflow({
|
|
441
|
+
name: "blog-article-writer",
|
|
442
|
+
description: "Create, refine, and optimize a blog post",
|
|
443
|
+
observability: true,
|
|
444
|
+
async entry(ctx) {
|
|
445
|
+
const draft = await ctx.runAgent("content-creator");
|
|
446
|
+
const refined = await ctx.runAgent("clarity-editor", {
|
|
447
|
+
input: { content: draft.content },
|
|
448
|
+
});
|
|
449
|
+
return { article: refined.content };
|
|
450
|
+
},
|
|
451
|
+
}).registerAgents({
|
|
452
|
+
"content-creator": createAgent,
|
|
453
|
+
"clarity-editor": clarityEditorAgent,
|
|
454
|
+
});
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### `WorkflowContext` (inside `entry` and agent `run`)
|
|
458
|
+
|
|
459
|
+
| Member / method | Description |
|
|
460
|
+
|-----------------|-------------|
|
|
461
|
+
| `ctx.input` | Workflow input passed to `runWorkflow` (or overridden per `runAgent`) |
|
|
462
|
+
| `ctx.state` | Shared mutable object for the current execution |
|
|
463
|
+
| `ctx.agentName` | Name of the agent currently running (inside agent handlers) |
|
|
464
|
+
| `ctx.runAgent(name, { input? })` | Invoke a registered agent |
|
|
465
|
+
| `ctx.sendAI({ message, saveHistory?, label? })` | AI call; steps recorded when observability is on |
|
|
466
|
+
| `ctx.fetchAIHistory({ label? })` | Fetch AI history |
|
|
467
|
+
| `ctx.search({ query, limit?, minSimilarity?, filterbySource?, label? })` | Semantic search |
|
|
468
|
+
| `ctx.addDocument(opts)` | Add knowledge-base document |
|
|
469
|
+
| `ctx.updateDocument(opts)` | Update document |
|
|
470
|
+
| `ctx.deleteDocument(opts)` | Delete document |
|
|
471
|
+
| `ctx.listDocuments(opts?)` | List documents |
|
|
472
|
+
| `ctx.getDocument(opts)` | Get document by id |
|
|
473
|
+
| `ctx.recordStep({ label, input?, output?, status?, type? })` | Record a custom step when observability is enabled |
|
|
474
|
+
|
|
475
|
+
When `observability: true` on the workflow, SDK calls and `recordStep` are sent to the DNotifier workflow dashboard. No extra setup is required beyond `connect()`.
|
|
476
|
+
|
|
477
|
+
---
|
|
478
|
+
|
|
479
|
+
## API reference
|
|
480
|
+
|
|
481
|
+
### `DNotifier`
|
|
104
482
|
|
|
105
483
|
| Method | Description |
|
|
106
484
|
|--------|-------------|
|
|
107
|
-
| `
|
|
108
|
-
| `
|
|
109
|
-
| `
|
|
110
|
-
| `
|
|
111
|
-
| `
|
|
112
|
-
| `
|
|
113
|
-
| `
|
|
485
|
+
| `connect()` | Authenticate and connect |
|
|
486
|
+
| `disconnect()` | Close the WebSocket connection |
|
|
487
|
+
| `getPlanLimits()` | Plan limits from last auth |
|
|
488
|
+
| `send({ senderId, receiverId?, receiverIds?, data, saveHistory? })` | Send a message |
|
|
489
|
+
| `sendBinary({ senderId, receiverIds, buffer, type? })` | Send raw bytes |
|
|
490
|
+
| `sendAI({ senderId, message, saveHistory?, sessionId? })` | Send to AI pipeline |
|
|
491
|
+
| `fetchAIHistory({ senderId })` | Fetch AI conversation history |
|
|
492
|
+
| `deleteAIHistoryMessage({ senderId, messageId })` | Delete one AI history message |
|
|
493
|
+
| `fetchChatHistory({ senderId, receiverIds })` | Request chat history (response via `onMessage`) |
|
|
494
|
+
| `deleteChatHistoryMessage({ senderId, messageId })` | Delete one chat history message |
|
|
495
|
+
| `search({ senderId, query, limit?, minSimilarity?, filterbySource? })` | Semantic search over knowledge base |
|
|
496
|
+
| `addDocument({ senderId, recordId, content, type?, metadata? })` | Add a document |
|
|
497
|
+
| `updateDocument({ senderId, recordId, content, type?, metadata? })` | Update a document |
|
|
498
|
+
| `getDocument({ senderId, recordId })` | Get a document |
|
|
499
|
+
| `listDocuments({ senderId, limit?, offset?, type? })` | List documents |
|
|
500
|
+
| `deleteDocument({ senderId, recordId })` | Delete a document |
|
|
501
|
+
| `runWorkflow({ workflow, input, senderId? })` | Run a workflow after `connect()` |
|
|
502
|
+
| `sendWithOpenAI(...)` | **Deprecated** — use `sendAI` |
|
|
114
503
|
|
|
115
|
-
###
|
|
504
|
+
### Workflow exports
|
|
116
505
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
506
|
+
| Export | Description |
|
|
507
|
+
|--------|-------------|
|
|
508
|
+
| `DNotifier.defineAgent({ name, run })` | Create a named agent |
|
|
509
|
+
| `DNotifier.Workflow` | Workflow definition class |
|
|
510
|
+
| `DNotifier.WorkflowContext` | Context type passed to `entry` and agent `run` |
|
|
511
|
+
| `DNotifier.WorkflowError` | Workflow validation and runtime errors |
|
|
512
|
+
| `Agent` | Agent class (also available via `defineAgent`) |
|
|
513
|
+
|
|
514
|
+
`runWorkflow` is the recommended way to execute a workflow. `WorkflowRunner` is exported for advanced custom hosting but is not required for typical use.
|
|
515
|
+
|
|
516
|
+
### Message and payload types
|
|
517
|
+
|
|
518
|
+
**`DNotifierMessage`**
|
|
519
|
+
|
|
520
|
+
- `metadata` — `{ id, sender, timestamp, type }`
|
|
521
|
+
- `payload` — `Payload` instance
|
|
522
|
+
|
|
523
|
+
**`Payload`**
|
|
524
|
+
|
|
525
|
+
- `toJSON()` — parse JSON body
|
|
526
|
+
- `toString()` — string body
|
|
527
|
+
- `toBase64()` — base64 string
|
|
528
|
+
- `raw()` — raw bytes
|
|
529
|
+
|
|
530
|
+
**`getPlanLimits()` return value**
|
|
531
|
+
|
|
532
|
+
- `messagesHardLimit`, `maxAIRequestsPerMonth`, `maxAIWordsPerMonth`
|
|
533
|
+
- `knowledgeBaseMaxWords`, `aiEnabled`, `maxUsers`, `maxRowsPerUser`
|
|
534
|
+
|
|
535
|
+
**`runWorkflow` return value**
|
|
536
|
+
|
|
537
|
+
- `result` — return value of the workflow `entry` function
|
|
538
|
+
- `state` — final shared workflow state
|
|
539
|
+
- `executionId` — present when `observability: true`
|
|
540
|
+
|
|
541
|
+
---
|
|
542
|
+
|
|
543
|
+
## Links
|
|
120
544
|
|
|
121
|
-
|
|
545
|
+
- **Product:** [dnotifier.com](https://www.dnotifier.com)
|
|
546
|
+
- **Documentation:** [Product docs](https://dnotifier.gitbook.io/product-docs)
|
package/dist/cjs/dnotifier.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['DNotifier']=void 0x0;const httpTransport_js_1=require('./transport/httpTransport.js'),websocketTransport_js_1=require('./transport/websocketTransport.js'),id_js_1=require('./utils/id.js'),frame_js_1=require('./utils/frame.js'),payload_js_1=require('./utils/payload.js'),buffer_js_1=require('./utils/buffer.js'),openai_js_1=require('./providers/openai.js');class DNotifier{constructor({appId:
|
|
1
|
+
'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['DNotifier']=void 0x0;const httpTransport_js_1=require('./transport/httpTransport.js'),websocketTransport_js_1=require('./transport/websocketTransport.js'),id_js_1=require('./utils/id.js'),frame_js_1=require('./utils/frame.js'),payload_js_1=require('./utils/payload.js'),buffer_js_1=require('./utils/buffer.js'),openai_js_1=require('./providers/openai.js');class DNotifier{constructor({appId:_0x3c2fc3,secret:_0x455829,transport:_0x28f55a,userId:_0x358eae,onConnected:_0x11d653,onMessage:_0x304687,onDisconnected:_0x1465c1,WebSocketImpl:_0x58f42b}){if(!(_0x3c2fc3&&_0x455829&&_0x28f55a&&_0x358eae))throw new Error('appId,\x20secret,\x20transport,\x20and\x20userId\x20are\x20required');this['appId']=_0x3c2fc3,this['secret']=_0x455829,this['userId']=_0x358eae,this['url']='wss://link.dchatter.com/ws',this['onConnectedCallback']=_0x11d653||(()=>{}),this['onMessageCallback']=_0x304687||(()=>{}),this['onDisconnectedCallback']=_0x1465c1||(()=>{}),this['isConnected']=!0x1,this['transport']='ws'===_0x28f55a?new websocketTransport_js_1['WebsocketTransport'](_0x58f42b):new httpTransport_js_1['HttpTransport']();}async['connect'](){const _0x507ee7=new httpTransport_js_1['HttpTransport']({'url':'https://link.dchatter.com/api/v1/sdk/auth'}),_0x5427e1=await _0x507ee7['send']({'appId':this['appId'],'appSecret':this['secret'],'userID':this['userId']});if(0xc8!==_0x5427e1['_responseCode'])throw new Error('Authentication\x20Failed.');this['authToken']=_0x5427e1['_responseData'],await this['transport']['connect'](this['url']);const _0x16b3b6={'type':'text','code':0x65,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],this['userId']),'timestamp':Date['now'](),'token':this['authToken']},_0x167fd9=(0x0,buffer_js_1['toUint8'])(JSON['stringify']({'token':this['authToken']})),_0x1d3fa2=(0x0,frame_js_1['framePacket'])(_0x16b3b6,_0x167fd9);this['transport']['sendBinary'](_0x1d3fa2);let _0x53b103=!0x1;this['transport']['on']('message',_0x3e26f7=>{try{const {metadata:_0x4632a7,data:_0x39bf3e}=_0x3e26f7;if(!_0x53b103&&'102'===_0x4632a7['packet']['code']&&_0x4632a7['packet']['sender']['includes']((0x0,id_js_1['prefixId'])(this['appId'],this['userId'])))return _0x53b103=!0x0,this['isConnected']=!0x0,void this['onConnectedCallback']();_0x53b103&&this['onMessageCallback']({'metadata':{'sender':_0x4632a7['packet']['sender'],'timestamp':_0x4632a7['packet']['timestamp']},'payload':new payload_js_1['Payload'](_0x39bf3e)});}catch(_0x239dde){console['error']('Packet\x20error:',_0x239dde);}}),this['transport']['on']('close',({code:_0x52caa8,reason:_0x4cfcbf})=>{this['isConnected']&&(this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':_0x52caa8,'reason':_0x4cfcbf||'Disconnected'}));}),this['transport']['on']('error',_0x41e94d=>{this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':0x1f4,'reason':(null==_0x41e94d?void 0x0:_0x41e94d['message'])||'Transport\x20error'});});}['send']({senderId:_0xebbe2b,receiverId:_0x14709a,receiverIds:_0x2d5922,data:_0x36c5fe}){if(!_0xebbe2b)throw new Error('senderId\x20required');if(!_0x14709a&&!_0x2d5922)throw new Error('receiverId\x20or\x20receiverIds\x20required');const _0x52e089=null!=_0x2d5922?_0x2d5922:[_0x14709a],_0x4b5bba={'type':'text','code':0xc8,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0xebbe2b),'receivers':_0x52e089['map'](_0xe4d002=>(0x0,id_js_1['prefixId'])(this['appId'],_0xe4d002)),'timestamp':Date['now'](),'token':this['authToken']},_0x385347=(0x0,buffer_js_1['toUint8'])(JSON['stringify'](_0x36c5fe)),_0x310aa2=(0x0,frame_js_1['framePacket'])(_0x4b5bba,_0x385347);this['transport']['sendBinary'](_0x310aa2);}['sendBinary']({senderId:_0x7c507a,receiverIds:_0x268a8e,buffer:_0x1f7df0,type:_0x1f3c1d='binary'}){if(!_0x7c507a)throw new Error('senderId\x20required');if(!_0x1f7df0)throw new Error('buffer\x20is\x20required');const _0x4c6266={'type':_0x1f3c1d,'code':0xca,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x7c507a),'receivers':_0x268a8e['map'](_0x12aeba=>(0x0,id_js_1['prefixId'])(this['appId'],_0x12aeba)),'timestamp':Date['now'](),'token':this['authToken']},_0x12e982=(0x0,frame_js_1['framePacket'])(_0x4c6266,_0x1f7df0);this['transport']['sendBinary'](_0x12e982);}async['sendWithOpenAI']({senderId:_0x2997e0,message:_0x984cf5,openaiApiKey:_0x39e8d8,model:_0x2e38ad,baseUrl:_0x22d4dd}){if(!_0x2997e0)throw new Error('senderId\x20is\x20required');if(!_0x984cf5)throw new Error('message\x20is\x20required');if(!_0x39e8d8)throw new Error('openaiApiKey\x20is\x20required');const {content:_0x502ea4}=await(0x0,openai_js_1['chatStream'])({'apiKey':_0x39e8d8,'model':_0x2e38ad,'messages':_0x984cf5,'baseUrl':_0x22d4dd}),_0x2c9bcb={'type':'openai','content':_0x502ea4,'ts':Date['now']()};return this['onMessageCallback']({'metadata':{'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x2997e0),'timestamp':Date['now']()},'payload':new payload_js_1['Payload']((0x0,buffer_js_1['toUint8'])(JSON['stringify'](_0x2c9bcb)))}),{'content':_0x502ea4};}}exports['DNotifier']=DNotifier;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['chat']=chat,exports['chatStream']=chatStream;const DEFAULT_BASE_URL='https://api.openai.com',DEFAULT_MODEL='gpt-4o-mini';function normalizeMessages(
|
|
1
|
+
'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['chat']=chat,exports['chatStream']=chatStream;const DEFAULT_BASE_URL='https://api.openai.com',DEFAULT_MODEL='gpt-4o-mini';function normalizeMessages(_0x386fb5){if('string'==typeof _0x386fb5)return[{'role':'user','content':_0x386fb5}];if(Array['isArray'](_0x386fb5)&&_0x386fb5['length']>0x0)return _0x386fb5['map'](_0x48d0eb=>({'role':_0x48d0eb['role']||'user','content':'string'==typeof _0x48d0eb['content']?_0x48d0eb['content']:String(_0x48d0eb['content'])}));throw new Error('message\x20must\x20be\x20a\x20non-empty\x20string\x20or\x20array\x20of\x20{\x20role,\x20content\x20}');}async function chat(_0x58a765){var _0xdddfea,_0x13c9fd,_0x2e37ba,_0x543d3d,_0x5aecaa,_0x1f455d;const {apiKey:_0x256b75,model:_0x3b5cff=DEFAULT_MODEL,messages:_0x36e987,baseUrl:_0x33040e=DEFAULT_BASE_URL}=_0x58a765;if(!_0x256b75||!_0x36e987)throw new Error('openai.chat:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x37529b=_0x33040e['replace'](/\/$/,'')+'/v1/chat/completions',_0x53ed34={'model':_0x3b5cff,'messages':normalizeMessages(_0x36e987),'stream':!0x1},_0x582497=await fetch(_0x37529b,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x256b75},'body':JSON['stringify'](_0x53ed34)}),_0x315772=await _0x582497['json']();if(!_0x582497['ok']){const _0x54a725=new Error((null===(_0xdddfea=null==_0x315772?void 0x0:_0x315772['error'])||void 0x0===_0xdddfea?void 0x0:_0xdddfea['message'])||'OpenAI\x20API\x20error:\x20'+_0x582497['status']);throw _0x54a725['status']=_0x582497['status'],_0x54a725['code']=null===(_0x13c9fd=null==_0x315772?void 0x0:_0x315772['error'])||void 0x0===_0x13c9fd?void 0x0:_0x13c9fd['code'],_0x54a725;}return{'content':null!==(_0x1f455d=null===(_0x5aecaa=null===(_0x543d3d=null===(_0x2e37ba=null==_0x315772?void 0x0:_0x315772['choices'])||void 0x0===_0x2e37ba?void 0x0:_0x2e37ba[0x0])||void 0x0===_0x543d3d?void 0x0:_0x543d3d['message'])||void 0x0===_0x5aecaa?void 0x0:_0x5aecaa['content'])&&void 0x0!==_0x1f455d?_0x1f455d:'','usage':null==_0x315772?void 0x0:_0x315772['usage'],'raw':_0x315772};}async function chatStream(_0x33be1d){var _0x4ad51f,_0x30639b,_0x2be3a5,_0x588c2d,_0x33e8a2;const {apiKey:_0x534466,model:_0x4bc1bb=DEFAULT_MODEL,messages:_0x38ed7d,baseUrl:_0x3fc251=DEFAULT_BASE_URL,onChunk:_0x5611a4}=_0x33be1d;if(!_0x534466||!_0x38ed7d)throw new Error('openai.chatStream:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x3cc0ec=_0x3fc251['replace'](/\/$/,'')+'/v1/chat/completions',_0x307078={'model':_0x4bc1bb,'messages':normalizeMessages(_0x38ed7d),'stream':!0x0},_0x5ca860=await fetch(_0x3cc0ec,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x534466},'body':JSON['stringify'](_0x307078)});if(!_0x5ca860['ok']){const _0x3b5387=await _0x5ca860['json']()['catch'](()=>({})),_0x4afc06=new Error((null===(_0x4ad51f=null==_0x3b5387?void 0x0:_0x3b5387['error'])||void 0x0===_0x4ad51f?void 0x0:_0x4ad51f['message'])||'OpenAI\x20API\x20error:\x20'+_0x5ca860['status']);throw _0x4afc06['status']=_0x5ca860['status'],_0x4afc06['code']=null===(_0x30639b=null==_0x3b5387?void 0x0:_0x3b5387['error'])||void 0x0===_0x30639b?void 0x0:_0x30639b['code'],_0x4afc06;}const _0x290091=_0x5ca860['body']['getReader'](),_0x257f45=new TextDecoder();let _0x3bb59b='',_0x1f4a40='';for(;;){const {done:_0x389fed,value:_0x45d676}=await _0x290091['read']();if(_0x389fed)break;_0x1f4a40+=_0x257f45['decode'](_0x45d676,{'stream':!0x0});const _0x57d998=_0x1f4a40['split']('\x0a');_0x1f4a40=_0x57d998['pop']()||'';for(const _0x3452a6 of _0x57d998){const _0x270fe2=_0x3452a6['trim']();if(''!==_0x270fe2&&'data:\x20[DONE]'!==_0x270fe2&&_0x270fe2['startsWith']('data:\x20'))try{const _0x21be28=JSON['parse'](_0x270fe2['slice'](0x6)),_0x101b9b=null===(_0x33e8a2=null===(_0x588c2d=null===(_0x2be3a5=null==_0x21be28?void 0x0:_0x21be28['choices'])||void 0x0===_0x2be3a5?void 0x0:_0x2be3a5[0x0])||void 0x0===_0x588c2d?void 0x0:_0x588c2d['delta'])||void 0x0===_0x33e8a2?void 0x0:_0x33e8a2['content'];'string'==typeof _0x101b9b&&(_0x3bb59b+=_0x101b9b,_0x5611a4&&_0x5611a4(_0x101b9b));}catch(_0x25fe9b){}}}return{'content':_0x3bb59b};}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['HttpTransport']=void 0x0;class HttpTransport{constructor({url:
|
|
1
|
+
'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['HttpTransport']=void 0x0;class HttpTransport{constructor({url:_0x2e63a4}){this['url']=_0x2e63a4;}async['send'](_0x56bfa4){const _0x40241a=await fetch(''+this['url'],{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](_0x56bfa4)});if(!_0x40241a['ok']){const _0x378590=await _0x40241a['text']();throw new Error('DNotifier\x20HTTP\x20error:\x20'+_0x378590);}return await _0x40241a['json']();}['sendBinary'](){throw new Error('Binary\x20is\x20not\x20supported\x20over\x20HTTP\x20yet');}}exports['HttpTransport']=HttpTransport;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['WebsocketTransport']=void 0x0;const events_1=require('events'),frame_js_1=require('../utils/frame.js');class WebsocketTransport extends events_1['default']{constructor(
|
|
1
|
+
'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['WebsocketTransport']=void 0x0;const events_1=require('events'),frame_js_1=require('../utils/frame.js');class WebsocketTransport extends events_1['default']{constructor(_0x2e8d87=null){if(super(),this['socket']=null,this['WebSocketImpl']=_0x2e8d87||('undefined'!=typeof window?window['WebSocket']:null),!this['WebSocketImpl'])throw new Error('No\x20WebSocket\x20implementation\x20provided');}['connect'](_0x318a76){return new Promise((_0x3a179,_0x286c96)=>{this['socket']=new this['WebSocketImpl'](_0x318a76),'undefined'!=typeof window&&(this['socket']['binaryType']='arrayBuffer'),this['_on']('open',()=>_0x3a179(!0x0)),this['_on']('error',_0x5cba1d=>{console['error']('WS\x20error',_0x5cba1d),_0x286c96(_0x5cba1d);}),this['_on']('close',_0x393817=>{const _0x121204=null==_0x393817?void 0x0:_0x393817['code'],_0x44efb8=null==_0x393817?void 0x0:_0x393817['reason'];this['emit']('close',{'code':_0x121204,'reason':_0x44efb8});}),this['_on']('message',async(_0x99e772,_0x2f2985)=>{var _0x5db75f,_0x552966=null!==(_0x5db75f=null==_0x99e772?void 0x0:_0x99e772['data'])&&void 0x0!==_0x5db75f?_0x5db75f:_0x99e772;'undefined'!=typeof Blob&&_0x552966 instanceof Blob&&(_0x552966=await _0x552966['arrayBuffer']());const _0x21c5fe=(0x0,frame_js_1['decodePacket'])(_0x552966);_0x21c5fe&&this['emit']('message',_0x21c5fe,_0x2f2985);});});}['_on'](_0x321d25,_0x45f042){this['socket']['on']?this['socket']['on'](_0x321d25,_0x45f042):this['socket']['on'+_0x321d25]=_0x45f042;}['send'](_0x3632a5){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x3632a5);}['sendBinary'](_0x1e859b){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x1e859b);}['isOpen'](){return this['socket']&&this['socket']['readyState']===this['WebSocketImpl']['OPEN'];}['close'](){var _0x4322c4;null===(_0x4322c4=this['socket'])||void 0x0===_0x4322c4||_0x4322c4['close']();}}exports['WebsocketTransport']=WebsocketTransport;
|