@dnotifier-realtime/dnotifier 1.1.10 → 1.1.12
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.js +1 -1
- 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.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.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:_0x2d0ac5,secret:_0x2dd256,transport:_0x4414f1,userId:_0x30279a,onConnected:_0x348073,onMessage:_0x2bea6c,onDisconnected:_0x2519ae,WebSocketImpl:_0x970ea1}){if(!(_0x2d0ac5&&_0x2dd256&&_0x4414f1&&_0x30279a))throw new Error('appId,\x20secret,\x20transport,\x20and\x20userId\x20are\x20required');this['appId']=_0x2d0ac5,this['secret']=_0x2dd256,this['userId']=_0x30279a,this['url']='wss://link.dchatter.com/ws',this['onConnectedCallback']=_0x348073||(()=>{}),this['onMessageCallback']=_0x2bea6c||(()=>{}),this['onDisconnectedCallback']=_0x2519ae||(()=>{}),this['isConnected']=!0x1,this['transport']='ws'===_0x4414f1?new websocketTransport_js_1['WebsocketTransport'](_0x970ea1):new httpTransport_js_1['HttpTransport']();}async['connect'](){const _0x138609=new httpTransport_js_1['HttpTransport']({'url':'https://link.dchatter.com/api/v1/sdk/auth'}),_0x1ee829=await _0x138609['send']({'appId':this['appId'],'appSecret':this['secret'],'userID':this['userId']});if(0xc8!==_0x1ee829['_responseCode'])throw new Error('Authentication\x20Failed.');this['authToken']=_0x1ee829['_responseData'],await this['transport']['connect'](this['url']);const _0x230e0a={'type':'text','code':0x65,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],this['userId']),'timestamp':Date['now'](),'token':this['authToken']},_0x4d6cab=(0x0,buffer_js_1['toUint8'])(JSON['stringify']({'token':this['authToken']})),_0x40b9ec=(0x0,frame_js_1['framePacket'])(_0x230e0a,_0x4d6cab);this['transport']['sendBinary'](_0x40b9ec);let _0x1ea75e=!0x1;this['transport']['on']('message',_0x2beb21=>{try{const {metadata:_0x3d371d,data:_0x116d4d}=_0x2beb21;if(!_0x1ea75e&&'102'===_0x3d371d['packet']['code']&&_0x3d371d['packet']['sender']['includes']((0x0,id_js_1['prefixId'])(this['appId'],this['userId'])))return _0x1ea75e=!0x0,this['isConnected']=!0x0,void this['onConnectedCallback']();_0x1ea75e&&this['onMessageCallback']({'metadata':{'sender':_0x3d371d['packet']['sender'],'timestamp':_0x3d371d['packet']['timestamp']},'payload':new payload_js_1['Payload'](_0x116d4d)});}catch(_0x3b7966){console['error']('Packet\x20error:',_0x3b7966);}}),this['transport']['on']('close',({code:_0x167bea,reason:_0x5e52a5})=>{this['isConnected']&&(this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':_0x167bea,'reason':_0x5e52a5||'Disconnected'}));}),this['transport']['on']('error',_0x551e9a=>{this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':0x1f4,'reason':(null==_0x551e9a?void 0x0:_0x551e9a['message'])||'Transport\x20error'});});}['send']({senderId:_0x2330c5,receiverId:_0x1774ae,receiverIds:_0x53cb3c,data:_0x5bde23}){if(!_0x2330c5)throw new Error('senderId\x20required');if(!_0x1774ae&&!_0x53cb3c)throw new Error('receiverId\x20or\x20receiverIds\x20required');const _0x34a7ea=null!=_0x53cb3c?_0x53cb3c:[_0x1774ae],_0x410ffd={'type':'text','code':0xc8,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x2330c5),'receivers':_0x34a7ea['map'](_0x194b7f=>(0x0,id_js_1['prefixId'])(this['appId'],_0x194b7f)),'timestamp':Date['now'](),'token':this['authToken']},_0x38a125=(0x0,buffer_js_1['toUint8'])(JSON['stringify'](_0x5bde23)),_0x29b8b9=(0x0,frame_js_1['framePacket'])(_0x410ffd,_0x38a125);this['transport']['sendBinary'](_0x29b8b9);}['sendBinary']({senderId:_0x5670e9,receiverIds:_0x11d68a,buffer:_0x880b56,type:_0x6aeb24='binary'}){if(!_0x5670e9)throw new Error('senderId\x20required');if(!_0x880b56)throw new Error('buffer\x20is\x20required');const _0x188dec={'type':_0x6aeb24,'code':0xca,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x5670e9),'receivers':_0x11d68a['map'](_0x591fdd=>(0x0,id_js_1['prefixId'])(this['appId'],_0x591fdd)),'timestamp':Date['now'](),'token':this['authToken']},_0x2deb96=(0x0,frame_js_1['framePacket'])(_0x188dec,_0x880b56);this['transport']['sendBinary'](_0x2deb96);}async['sendWithOpenAI']({senderId:_0x3e1c3f,message:_0x1e8016,openaiApiKey:_0x4a892a,model:_0x18f183,baseUrl:_0x4b4c7e}){if(!_0x3e1c3f)throw new Error('senderId\x20is\x20required');if(!_0x1e8016)throw new Error('message\x20is\x20required');if(!_0x4a892a)throw new Error('openaiApiKey\x20is\x20required');const {content:_0x412637}=await(0x0,openai_js_1['chatStream'])({'apiKey':_0x4a892a,'model':_0x18f183,'messages':_0x1e8016,'baseUrl':_0x4b4c7e}),_0x6cafb5={'type':'openai','content':_0x412637,'ts':Date['now']()};return this['onMessageCallback']({'metadata':{'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x3e1c3f),'timestamp':Date['now']()},'payload':new payload_js_1['Payload']((0x0,buffer_js_1['toUint8'])(JSON['stringify'](_0x6cafb5)))}),{'content':_0x412637};}}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(_0x54ffd7){if('string'==typeof _0x54ffd7)return[{'role':'user','content':_0x54ffd7}];if(Array['isArray'](_0x54ffd7)&&_0x54ffd7['length']>0x0)return _0x54ffd7['map'](_0x45fdca=>({'role':_0x45fdca['role']||'user','content':'string'==typeof _0x45fdca['content']?_0x45fdca['content']:String(_0x45fdca['content'])}));throw new Error('message\x20must\x20be\x20a\x20non-empty\x20string\x20or\x20array\x20of\x20{\x20role,\x20content\x20}');}async function chat(_0x173788){var _0x250e3f,_0x318866,_0x476456,_0x294e3c,_0x53a3e0,_0x3baccc;const {apiKey:_0x4272d0,model:_0x39c37e=DEFAULT_MODEL,messages:_0x4d38f9,baseUrl:_0x3e65a6=DEFAULT_BASE_URL}=_0x173788;if(!_0x4272d0||!_0x4d38f9)throw new Error('openai.chat:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x5d24d3=_0x3e65a6['replace'](/\/$/,'')+'/v1/chat/completions',_0x5e9380={'model':_0x39c37e,'messages':normalizeMessages(_0x4d38f9),'stream':!0x1},_0x44a5d0=await fetch(_0x5d24d3,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x4272d0},'body':JSON['stringify'](_0x5e9380)}),_0x167666=await _0x44a5d0['json']();if(!_0x44a5d0['ok']){const _0x1ef961=new Error((null===(_0x250e3f=null==_0x167666?void 0x0:_0x167666['error'])||void 0x0===_0x250e3f?void 0x0:_0x250e3f['message'])||'OpenAI\x20API\x20error:\x20'+_0x44a5d0['status']);throw _0x1ef961['status']=_0x44a5d0['status'],_0x1ef961['code']=null===(_0x318866=null==_0x167666?void 0x0:_0x167666['error'])||void 0x0===_0x318866?void 0x0:_0x318866['code'],_0x1ef961;}return{'content':null!==(_0x3baccc=null===(_0x53a3e0=null===(_0x294e3c=null===(_0x476456=null==_0x167666?void 0x0:_0x167666['choices'])||void 0x0===_0x476456?void 0x0:_0x476456[0x0])||void 0x0===_0x294e3c?void 0x0:_0x294e3c['message'])||void 0x0===_0x53a3e0?void 0x0:_0x53a3e0['content'])&&void 0x0!==_0x3baccc?_0x3baccc:'','usage':null==_0x167666?void 0x0:_0x167666['usage'],'raw':_0x167666};}async function chatStream(_0x3a9009){var _0x302718,_0x4cd852,_0x289975,_0x1d0950,_0x14827f;const {apiKey:_0x56b1af,model:_0x32843b=DEFAULT_MODEL,messages:_0x59b16b,baseUrl:_0x5615be=DEFAULT_BASE_URL,onChunk:_0xd26da5}=_0x3a9009;if(!_0x56b1af||!_0x59b16b)throw new Error('openai.chatStream:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x50d6a2=_0x5615be['replace'](/\/$/,'')+'/v1/chat/completions',_0x20bfa0={'model':_0x32843b,'messages':normalizeMessages(_0x59b16b),'stream':!0x0},_0x501dc8=await fetch(_0x50d6a2,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x56b1af},'body':JSON['stringify'](_0x20bfa0)});if(!_0x501dc8['ok']){const _0x4e007f=await _0x501dc8['json']()['catch'](()=>({})),_0x2c2b0e=new Error((null===(_0x302718=null==_0x4e007f?void 0x0:_0x4e007f['error'])||void 0x0===_0x302718?void 0x0:_0x302718['message'])||'OpenAI\x20API\x20error:\x20'+_0x501dc8['status']);throw _0x2c2b0e['status']=_0x501dc8['status'],_0x2c2b0e['code']=null===(_0x4cd852=null==_0x4e007f?void 0x0:_0x4e007f['error'])||void 0x0===_0x4cd852?void 0x0:_0x4cd852['code'],_0x2c2b0e;}const _0xed459d=_0x501dc8['body']['getReader'](),_0x1bd02e=new TextDecoder();let _0x5a7838='',_0x2a324c='';for(;;){const {done:_0x57c169,value:_0x14e7ae}=await _0xed459d['read']();if(_0x57c169)break;_0x2a324c+=_0x1bd02e['decode'](_0x14e7ae,{'stream':!0x0});const _0x4e8f35=_0x2a324c['split']('\x0a');_0x2a324c=_0x4e8f35['pop']()||'';for(const _0x16d679 of _0x4e8f35){const _0x35a2c7=_0x16d679['trim']();if(''!==_0x35a2c7&&'data:\x20[DONE]'!==_0x35a2c7&&_0x35a2c7['startsWith']('data:\x20'))try{const _0x4b25fb=JSON['parse'](_0x35a2c7['slice'](0x6)),_0x289235=null===(_0x14827f=null===(_0x1d0950=null===(_0x289975=null==_0x4b25fb?void 0x0:_0x4b25fb['choices'])||void 0x0===_0x289975?void 0x0:_0x289975[0x0])||void 0x0===_0x1d0950?void 0x0:_0x1d0950['delta'])||void 0x0===_0x14827f?void 0x0:_0x14827f['content'];'string'==typeof _0x289235&&(_0x5a7838+=_0x289235,_0xd26da5&&_0xd26da5(_0x289235));}catch(_0x3626a1){}}}return{'content':_0x5a7838};}
|
|
@@ -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:_0x44d5ef}){this['url']=_0x44d5ef;}async['send'](_0x50d460){const _0x177c62=await fetch(''+this['url'],{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](_0x50d460)});if(!_0x177c62['ok']){const _0xbaa202=await _0x177c62['text']();throw new Error('DNotifier\x20HTTP\x20error:\x20'+_0xbaa202);}return await _0x177c62['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(_0x28bcad=null){if(super(),this['socket']=null,this['WebSocketImpl']=_0x28bcad||('undefined'!=typeof window?window['WebSocket']:null),!this['WebSocketImpl'])throw new Error('No\x20WebSocket\x20implementation\x20provided');}['connect'](_0x336f38){return new Promise((_0x14e651,_0x46bd84)=>{this['socket']=new this['WebSocketImpl'](_0x336f38),'undefined'!=typeof window&&(this['socket']['binaryType']='arrayBuffer'),this['_on']('open',()=>_0x14e651(!0x0)),this['_on']('error',_0x470468=>{console['error']('WS\x20error',_0x470468),_0x46bd84(_0x470468);}),this['_on']('close',_0x49f215=>{const _0xceb9eb=null==_0x49f215?void 0x0:_0x49f215['code'],_0x34b5b9=null==_0x49f215?void 0x0:_0x49f215['reason'];this['emit']('close',{'code':_0xceb9eb,'reason':_0x34b5b9});}),this['_on']('message',async(_0x5f3da4,_0x5d05da)=>{var _0xe2d887,_0x1b0737=null!==(_0xe2d887=null==_0x5f3da4?void 0x0:_0x5f3da4['data'])&&void 0x0!==_0xe2d887?_0xe2d887:_0x5f3da4;'undefined'!=typeof Blob&&_0x1b0737 instanceof Blob&&(_0x1b0737=await _0x1b0737['arrayBuffer']());const _0x4fd694=(0x0,frame_js_1['decodePacket'])(_0x1b0737);_0x4fd694&&this['emit']('message',_0x4fd694,_0x5d05da);});});}['_on'](_0x2c77ab,_0x36c4b7){this['socket']['on']?this['socket']['on'](_0x2c77ab,_0x36c4b7):this['socket']['on'+_0x2c77ab]=_0x36c4b7;}['send'](_0x1bdd55){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x1bdd55);}['sendBinary'](_0x2ba019){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x2ba019);}['isOpen'](){return this['socket']&&this['socket']['readyState']===this['WebSocketImpl']['OPEN'];}['close'](){var _0x57cbd5;null===(_0x57cbd5=this['socket'])||void 0x0===_0x57cbd5||_0x57cbd5['close']();}}exports['WebsocketTransport']=WebsocketTransport;
|