priori-chat-sdk 1.0.0
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 +254 -0
- package/dist/index.d.mts +460 -0
- package/dist/index.d.ts +460 -0
- package/dist/index.js +984 -0
- package/dist/index.mjs +947 -0
- package/package.json +35 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
type Content = {
|
|
2
|
+
/**
|
|
3
|
+
* Unique identifier for the content
|
|
4
|
+
*/
|
|
5
|
+
content_id: string;
|
|
6
|
+
/**
|
|
7
|
+
* URL to the attached media
|
|
8
|
+
*/
|
|
9
|
+
url: string;
|
|
10
|
+
};
|
|
11
|
+
type Conversation$1 = {
|
|
12
|
+
/**
|
|
13
|
+
* ID of the bot associated with this conversation
|
|
14
|
+
*/
|
|
15
|
+
bot_id: string;
|
|
16
|
+
/**
|
|
17
|
+
* The unique ID of the conversation
|
|
18
|
+
*/
|
|
19
|
+
id: string;
|
|
20
|
+
/**
|
|
21
|
+
* Messages in the conversation
|
|
22
|
+
*/
|
|
23
|
+
messages: Array<Message$1>;
|
|
24
|
+
/**
|
|
25
|
+
* ID of the user associated with this conversation
|
|
26
|
+
*/
|
|
27
|
+
user_id?: string | null;
|
|
28
|
+
};
|
|
29
|
+
type ConversationHeader = {
|
|
30
|
+
/**
|
|
31
|
+
* ID of the bot associated with this conversation
|
|
32
|
+
*/
|
|
33
|
+
bot_id: string;
|
|
34
|
+
/**
|
|
35
|
+
* Unique identifier for the conversation
|
|
36
|
+
*/
|
|
37
|
+
id: string;
|
|
38
|
+
/**
|
|
39
|
+
* ID of the bot associated with this conversation
|
|
40
|
+
*/
|
|
41
|
+
user_id: string;
|
|
42
|
+
};
|
|
43
|
+
type CreateConversationResponse = {
|
|
44
|
+
conversation: Conversation$1;
|
|
45
|
+
};
|
|
46
|
+
type GetConversationResponse = {
|
|
47
|
+
/**
|
|
48
|
+
* ID of the bot associated with this conversation
|
|
49
|
+
*/
|
|
50
|
+
bot_id: string;
|
|
51
|
+
/**
|
|
52
|
+
* Messages in the conversation
|
|
53
|
+
*/
|
|
54
|
+
messages: Array<Message$1>;
|
|
55
|
+
/**
|
|
56
|
+
* ID of the user associated with this conversation
|
|
57
|
+
*/
|
|
58
|
+
user_id: string;
|
|
59
|
+
};
|
|
60
|
+
type ListConversationsResponse = {
|
|
61
|
+
/**
|
|
62
|
+
* List of conversations
|
|
63
|
+
*/
|
|
64
|
+
conversations: Array<ConversationHeader>;
|
|
65
|
+
};
|
|
66
|
+
type Message$1 = {
|
|
67
|
+
attached_media?: Content | null;
|
|
68
|
+
/**
|
|
69
|
+
* Whether this message is from a bot (true) or human (false)
|
|
70
|
+
*/
|
|
71
|
+
from_bot: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Unique identifier for the content
|
|
74
|
+
*/
|
|
75
|
+
id?: string | null;
|
|
76
|
+
/**
|
|
77
|
+
* The text content of the message
|
|
78
|
+
*/
|
|
79
|
+
text: string;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Options for listing conversations
|
|
84
|
+
*/
|
|
85
|
+
interface ListConversationsOptions {
|
|
86
|
+
bot_id?: string;
|
|
87
|
+
user_id?: string;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Options for getting a conversation by ID
|
|
91
|
+
*/
|
|
92
|
+
interface GetConversationOptions {
|
|
93
|
+
id: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Represents media content attached to a message
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const media: AttachedMedia = {
|
|
101
|
+
* url: "https://example.com/image.jpg"
|
|
102
|
+
* };
|
|
103
|
+
*
|
|
104
|
+
* // Use with message
|
|
105
|
+
* await conversation.sendMessage("Check out this image!", media);
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
interface AttachedMedia {
|
|
109
|
+
url: string;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Represents a message in the conversation
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* const userMessage: Message = {
|
|
116
|
+
* text: "Hey there!",
|
|
117
|
+
* from_bot: false,
|
|
118
|
+
* };
|
|
119
|
+
*
|
|
120
|
+
* const botMessageWithMedia: Message = {
|
|
121
|
+
* text: "Just sent you a pic (;",
|
|
122
|
+
* from_bot: true,
|
|
123
|
+
* attached_media: { url: "https://example.com/image.jpg" },
|
|
124
|
+
* timestamp: new Date("Sat Jul 05 2025 16:20:05")
|
|
125
|
+
* };
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
interface Message {
|
|
129
|
+
id?: string;
|
|
130
|
+
text: string;
|
|
131
|
+
from_bot: boolean;
|
|
132
|
+
attached_media?: AttachedMedia;
|
|
133
|
+
timestamp?: Date;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Configuration for retrieving a conversation by its unique ID
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* const config: ConversationWithId = {
|
|
140
|
+
* conversation_id: "87654321-4321-4321-4321-210987654321"
|
|
141
|
+
* };
|
|
142
|
+
*
|
|
143
|
+
* const { conversation } = await client.conversation(config);
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
interface ConversationWithId {
|
|
147
|
+
conversation_id: string;
|
|
148
|
+
pollingInterval?: number;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Configuration for retrieving or creating a conversation by user and bot pair.
|
|
152
|
+
* Note: There can only be one conversation between a specific user_id and bot_id.
|
|
153
|
+
* If a conversation already exists, it will be retrieved; otherwise, a new one will be created.
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* const config: ConversationWithUserBot = {
|
|
157
|
+
* user_id: "user-123",
|
|
158
|
+
* bot_id: "12345678-1234-1234-1234-123456789012"
|
|
159
|
+
* };
|
|
160
|
+
*
|
|
161
|
+
* // This will either find the existing conversation or create a new one
|
|
162
|
+
* const { conversation } = await client.conversation(config);
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
interface ConversationWithUserBot {
|
|
166
|
+
user_id: string;
|
|
167
|
+
bot_id: string;
|
|
168
|
+
pollingInterval?: number;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Union type for conversation configuration options.
|
|
172
|
+
* You can either specify a conversation_id directly, or provide user_id + bot_id
|
|
173
|
+
* to find/create the unique conversation between that user and bot.
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* // Option 1: Use existing conversation ID
|
|
177
|
+
* const byId: ConversationOptions = {
|
|
178
|
+
* conversation_id: "87654321-4321-4321-4321-210987654321"
|
|
179
|
+
* };
|
|
180
|
+
*
|
|
181
|
+
* // Option 2: Use user + bot pair (will find existing or create new)
|
|
182
|
+
* const byUserBot: ConversationOptions = {
|
|
183
|
+
* user_id: "user-123",
|
|
184
|
+
* bot_id: "12345678-1234-1234-1234-123456789012"
|
|
185
|
+
* };
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
type ConversationOptions = ConversationWithId | ConversationWithUserBot;
|
|
189
|
+
/**
|
|
190
|
+
* Callback functions for handling conversation events
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* const callbacks: ConversationCallbacks = {
|
|
194
|
+
* onInitialData: (data) => {
|
|
195
|
+
* console.log(`Loaded ${data.messages.length} previous messages`);
|
|
196
|
+
* data.messages.forEach(msg => {
|
|
197
|
+
* console.log(`${msg.from_bot ? 'Bot' : 'User'}: ${msg.text}`);
|
|
198
|
+
* });
|
|
199
|
+
* },
|
|
200
|
+
* onNewMessage: (message) => {
|
|
201
|
+
* if (message.from_bot) {
|
|
202
|
+
* console.log(`Bot: ${message.text}`);
|
|
203
|
+
* } else {
|
|
204
|
+
* console.log(`User: ${message.text}`);
|
|
205
|
+
* }
|
|
206
|
+
* },
|
|
207
|
+
* onError: (error) => {
|
|
208
|
+
* console.error('Conversation error:', error.message);
|
|
209
|
+
* }
|
|
210
|
+
* };
|
|
211
|
+
*
|
|
212
|
+
* const { conversation } = await client.conversation(options, callbacks);
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
interface ConversationCallbacks {
|
|
216
|
+
onInitialData?: (data: GetConversationResponse) => void;
|
|
217
|
+
onNewMessage?: (message: Message) => void;
|
|
218
|
+
onError?: (error: Error) => void;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Represents a conversation between a user and a bot.
|
|
222
|
+
* Provides real-time message handling through it's methods and callbacks
|
|
223
|
+
*/
|
|
224
|
+
declare class Conversation {
|
|
225
|
+
private client;
|
|
226
|
+
private conversationId;
|
|
227
|
+
private pollingInterval;
|
|
228
|
+
private pollingTimer?;
|
|
229
|
+
private lastKnownMessageCount;
|
|
230
|
+
private callbacks;
|
|
231
|
+
private isInitialized;
|
|
232
|
+
private constructor();
|
|
233
|
+
/**
|
|
234
|
+
* Creates a new conversation instance and initializes it.
|
|
235
|
+
* @param client - The PrioriChat client instance
|
|
236
|
+
* @param options - Configuration options for the conversation
|
|
237
|
+
* @param callbacks - Event callbacks for handling conversation events
|
|
238
|
+
* @returns Promise resolving to the conversation instance and initial data containing message history
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* const client = new PrioriChat("your-api-key");
|
|
242
|
+
*
|
|
243
|
+
* const { conversation, initialData } = await Conversation.create(
|
|
244
|
+
* client,
|
|
245
|
+
* { user_id: "user-123", bot_id: "12345678-1234-1234-1234-123456789012" },
|
|
246
|
+
* {
|
|
247
|
+
* onNewMessage: (message) => {
|
|
248
|
+
* console.log(`${message.from_bot ? 'Bot' : 'User'}: ${message.text}`);
|
|
249
|
+
* },
|
|
250
|
+
* onError: (error) => {
|
|
251
|
+
* console.error("Error:", error);
|
|
252
|
+
* }
|
|
253
|
+
* }
|
|
254
|
+
* );
|
|
255
|
+
*
|
|
256
|
+
* // Print message history
|
|
257
|
+
* initialData.messages.forEach(msg => {
|
|
258
|
+
* console.log(`${msg.from_bot ? 'Bot' : 'User'}: ${msg.text}`);
|
|
259
|
+
* });
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
static create(client: PrioriChat, options: ConversationOptions, callbacks?: ConversationCallbacks): Promise<{
|
|
263
|
+
conversation: Conversation;
|
|
264
|
+
initialData: GetConversationResponse;
|
|
265
|
+
}>;
|
|
266
|
+
private initialize;
|
|
267
|
+
private startPolling;
|
|
268
|
+
/**
|
|
269
|
+
* Sends a message to the conversation
|
|
270
|
+
* @param text - The text content of the message
|
|
271
|
+
* @param attachedMedia - Optional attached media content
|
|
272
|
+
* @returns Promise that resolves when message is sent
|
|
273
|
+
* @example
|
|
274
|
+
* ```ts
|
|
275
|
+
* // Send a simple text message
|
|
276
|
+
* await conversation.sendMessage("Hello, how can you help me?");
|
|
277
|
+
*
|
|
278
|
+
* // Send a message with media attachment
|
|
279
|
+
* await conversation.sendMessage("Here's an image", {
|
|
280
|
+
* url: "https://example.com/image.jpg"
|
|
281
|
+
* });
|
|
282
|
+
*
|
|
283
|
+
* // Continue the conversation by sending more messages
|
|
284
|
+
* // The onNewMessage callback will handle bot responses
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
sendMessage(text: string, attachedMedia?: AttachedMedia): Promise<void>;
|
|
288
|
+
/**
|
|
289
|
+
* Stops polling for new messages and cleans up resources.
|
|
290
|
+
* Call this method when you're done with the conversation to prevent memory leaks.
|
|
291
|
+
* @example
|
|
292
|
+
* ```ts
|
|
293
|
+
* // Always disconnect when done
|
|
294
|
+
* conversation.disconnect();
|
|
295
|
+
*
|
|
296
|
+
* // Or use in React useEffect cleanup
|
|
297
|
+
* useEffect(() => {
|
|
298
|
+
* const setupConversation = async () => {
|
|
299
|
+
* const { conversation } = await client.conversation(...);
|
|
300
|
+
* setConversation(conversation);
|
|
301
|
+
* };
|
|
302
|
+
*
|
|
303
|
+
* setupConversation();
|
|
304
|
+
*
|
|
305
|
+
* return () => {
|
|
306
|
+
* conversation?.disconnect();
|
|
307
|
+
* };
|
|
308
|
+
* }, []);
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
disconnect(): void;
|
|
312
|
+
/**
|
|
313
|
+
* Gets the current conversation ID.
|
|
314
|
+
* @returns The conversation ID string
|
|
315
|
+
* @example
|
|
316
|
+
* ```ts
|
|
317
|
+
* console.log(`Current conversation: ${conversation.id}`);
|
|
318
|
+
* // Output: Current conversation: 87654321-4321-4321-4321-210987654321
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
get id(): string;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
declare class ApiError extends Error {
|
|
325
|
+
name: string;
|
|
326
|
+
status?: number;
|
|
327
|
+
method?: string;
|
|
328
|
+
url?: string;
|
|
329
|
+
payload?: unknown;
|
|
330
|
+
constructor({ message, status, method, url, payload, }: {
|
|
331
|
+
message: string;
|
|
332
|
+
status?: number;
|
|
333
|
+
method?: string;
|
|
334
|
+
url?: string;
|
|
335
|
+
payload?: unknown;
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Options for creating a new conversation
|
|
340
|
+
*/
|
|
341
|
+
interface CreateConversationOptions {
|
|
342
|
+
bot_id: string;
|
|
343
|
+
user_id: string;
|
|
344
|
+
create_user_if_not_exists?: boolean;
|
|
345
|
+
with_messages?: Array<{
|
|
346
|
+
text: string;
|
|
347
|
+
from_bot: boolean;
|
|
348
|
+
id?: string;
|
|
349
|
+
attached_media?: AttachedMedia;
|
|
350
|
+
}>;
|
|
351
|
+
}
|
|
352
|
+
declare class PrioriChat {
|
|
353
|
+
private client;
|
|
354
|
+
private apiToken;
|
|
355
|
+
constructor(api_token: string, baseURL?: string);
|
|
356
|
+
private setupErrorInterceptor;
|
|
357
|
+
/**
|
|
358
|
+
* Creates a new conversation between a user and bot
|
|
359
|
+
* @param options - The conversation creation options
|
|
360
|
+
* @param options.bot_id - ID of the bot to associate with this conversation
|
|
361
|
+
* @param options.user_id - ID of the user to associate with this conversation
|
|
362
|
+
* @param options.create_user_if_not_exists - Whether to create the user if they don't exist (defaults to true)
|
|
363
|
+
* @param options.with_messages - Optional list of initial messages for the conversation
|
|
364
|
+
* @returns Promise resolving to the created conversation
|
|
365
|
+
* @example
|
|
366
|
+
* ```ts
|
|
367
|
+
* const client = new PrioriChat("your-api-key");
|
|
368
|
+
*
|
|
369
|
+
* const result = await client.createConversation({
|
|
370
|
+
* bot_id: "12345678-1234-1234-1234-123456789012",
|
|
371
|
+
* user_id: "user-123",
|
|
372
|
+
* create_user_if_not_exists: true
|
|
373
|
+
* });
|
|
374
|
+
*
|
|
375
|
+
* console.log(`Created conversation: ${result.conversation.id}`);
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
createConversation(options: CreateConversationOptions): Promise<CreateConversationResponse>;
|
|
379
|
+
/**
|
|
380
|
+
* Lists conversations with optional filtering
|
|
381
|
+
* @param options - Optional filtering options
|
|
382
|
+
* @param options.bot_id - Filter conversations by bot ID
|
|
383
|
+
* @param options.user_id - Filter conversations by user ID
|
|
384
|
+
* @returns Promise resolving to list of conversations
|
|
385
|
+
* @example
|
|
386
|
+
* ```ts
|
|
387
|
+
* const client = new PrioriChat("your-api-key");
|
|
388
|
+
*
|
|
389
|
+
* // List all conversations
|
|
390
|
+
* const allConversations = await client.listConversations();
|
|
391
|
+
*
|
|
392
|
+
* // List conversations for a specific user and bot
|
|
393
|
+
* const userConversations = await client.listConversations({
|
|
394
|
+
* user_id: "user-123",
|
|
395
|
+
* bot_id: "12345678-1234-1234-1234-123456789012"
|
|
396
|
+
* });
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
listConversations(options?: ListConversationsOptions): Promise<ListConversationsResponse>;
|
|
400
|
+
/**
|
|
401
|
+
* Retrieves a specific conversation by ID
|
|
402
|
+
* @param options - The conversation retrieval options
|
|
403
|
+
* @param options.id - The ID of the conversation to retrieve
|
|
404
|
+
* @returns Promise resolving to the conversation details
|
|
405
|
+
* @example
|
|
406
|
+
* ```ts
|
|
407
|
+
* const client = new PrioriChat("your-api-key");
|
|
408
|
+
*
|
|
409
|
+
* const conversation = await client.getConversation({
|
|
410
|
+
* id: "87654321-4321-4321-4321-210987654321"
|
|
411
|
+
* });
|
|
412
|
+
*
|
|
413
|
+
* console.log(`Found ${conversation.messages.length} messages`);
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
getConversation(options: GetConversationOptions): Promise<GetConversationResponse>;
|
|
417
|
+
/**
|
|
418
|
+
* Creates a new Conversation instance for real-time messaging
|
|
419
|
+
* @param options - Conversation initialization options (conversation_id OR user_id + bot_id)
|
|
420
|
+
* @param callbacks - Event callbacks for handling messages and errors
|
|
421
|
+
* @returns Promise resolving to conversation instance and initial data
|
|
422
|
+
* @example
|
|
423
|
+
* ```ts
|
|
424
|
+
* const client = new PrioriChat("your-api-key");
|
|
425
|
+
*
|
|
426
|
+
* const { conversation, initialData } = await client.conversation(
|
|
427
|
+
* { user_id: "user-123", bot_id: "12345678-1234-1234-1234-123456789012" },
|
|
428
|
+
* {
|
|
429
|
+
* onNewMessage: (message) => {
|
|
430
|
+
* if (message.from_bot) {
|
|
431
|
+
* console.log(`Bot: ${message.text}`);
|
|
432
|
+
* }
|
|
433
|
+
* },
|
|
434
|
+
* onError: (error) => {
|
|
435
|
+
* console.error("Conversation error:", error);
|
|
436
|
+
* }
|
|
437
|
+
* }
|
|
438
|
+
* );
|
|
439
|
+
*
|
|
440
|
+
* // Print initial message history
|
|
441
|
+
* console.log(`Loaded ${initialData.messages.length} previous messages`);
|
|
442
|
+
* initialData.messages.forEach(msg => {
|
|
443
|
+
* const sender = msg.from_bot ? "Bot" : "User";
|
|
444
|
+
* console.log(`${sender}: ${msg.text}`);
|
|
445
|
+
* });
|
|
446
|
+
*
|
|
447
|
+
* // Send a message to start/continue the conversation
|
|
448
|
+
* await conversation.sendMessage("Hello!");
|
|
449
|
+
*
|
|
450
|
+
* // Continue the conversation by sending more messages
|
|
451
|
+
* // The onNewMessage callback will handle incoming bot responses
|
|
452
|
+
* ```
|
|
453
|
+
*/
|
|
454
|
+
conversation(options: ConversationOptions, callbacks?: ConversationCallbacks): Promise<{
|
|
455
|
+
conversation: Conversation;
|
|
456
|
+
initialData: GetConversationResponse;
|
|
457
|
+
}>;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export { ApiError, type AttachedMedia, Conversation, type ConversationCallbacks, type ConversationOptions, type ConversationWithId, type ConversationWithUserBot, type CreateConversationOptions, type CreateConversationResponse, type GetConversationOptions, type GetConversationResponse, type ListConversationsOptions, type ListConversationsResponse, type Message, PrioriChat };
|