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/README.md
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# Priori Chat SDK
|
|
2
|
+
|
|
3
|
+
A TypeScript SDK for building real-time chat applications with the Priori Chat API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install priori-chat-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { PrioriChat } from 'priori-chat-sdk';
|
|
15
|
+
|
|
16
|
+
const client = new PrioriChat("your-api-key");
|
|
17
|
+
|
|
18
|
+
// Start a conversation
|
|
19
|
+
const { conversation, initialData } = await client.conversation(
|
|
20
|
+
{ user_id: "user-123", bot_id: "12345678-1234-1234-1234-123456789012" },
|
|
21
|
+
{
|
|
22
|
+
onNewMessage: (message) => {
|
|
23
|
+
console.log(`${message.from_bot ? 'Bot' : 'User'}: ${message.text}`);
|
|
24
|
+
if (message.attached_media) {
|
|
25
|
+
console.log(` 📎 Image: ${message.attached_media.url}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// Print initial message history
|
|
32
|
+
console.log(`Loaded ${initialData.messages.length} previous messages`);
|
|
33
|
+
initialData.messages.forEach(msg => {
|
|
34
|
+
console.log(`${msg.from_bot ? 'Bot' : 'User'}: ${msg.text}`);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Send a message
|
|
38
|
+
await conversation.sendMessage("Hello!");
|
|
39
|
+
|
|
40
|
+
// Clean up when done
|
|
41
|
+
conversation.disconnect();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Core Concepts
|
|
45
|
+
|
|
46
|
+
### Conversation Identification
|
|
47
|
+
|
|
48
|
+
Conversations can be identified in two ways:
|
|
49
|
+
|
|
50
|
+
1. **By user and bot pair** - There can only be one conversation between a specific `user_id` and `bot_id`. If a conversation already exists, it will be retrieved; otherwise, a new one will be created.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
const options = {
|
|
54
|
+
user_id: "user-123",
|
|
55
|
+
bot_id: "12345678-1234-1234-1234-123456789012"
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
2. **By conversation ID** - If you already have a conversation ID, you can connect directly to it.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const options = {
|
|
63
|
+
conversation_id: "87654321-4321-4321-4321-210987654321"
|
|
64
|
+
};
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Creating Conversations
|
|
68
|
+
|
|
69
|
+
You can also create conversations explicitly before starting real-time messaging:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const result = await client.createConversation({
|
|
73
|
+
bot_id: "12345678-1234-1234-1234-123456789012",
|
|
74
|
+
user_id: "user-123",
|
|
75
|
+
create_user_if_not_exists: true
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
console.log(`Created conversation: ${result.conversation.id}`);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Real-time Messaging
|
|
82
|
+
|
|
83
|
+
### Callbacks and Initial Data
|
|
84
|
+
|
|
85
|
+
When starting a conversation, you receive both the conversation instance and initial data containing the message history:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const { conversation, initialData } = await client.conversation(options, {
|
|
89
|
+
onInitialData: (data) => {
|
|
90
|
+
console.log(`Loaded ${data.messages.length} previous messages`);
|
|
91
|
+
// Display message history in your UI
|
|
92
|
+
data.messages.forEach(msg => {
|
|
93
|
+
console.log(`${msg.from_bot ? 'Bot' : 'User'}: ${msg.text}`);
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
onNewMessage: (message) => {
|
|
98
|
+
// Handle new messages in real-time
|
|
99
|
+
console.log(`${message.from_bot ? 'Bot' : 'User'}: ${message.text}`);
|
|
100
|
+
if (message.attached_media) {
|
|
101
|
+
console.log(` 📎 Image: ${message.attached_media.url}`);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
onError: (error) => {
|
|
106
|
+
console.error('Chat error:', error.message);
|
|
107
|
+
// Show error message to user
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// The initialData contains the same message history that onInitialData receives
|
|
112
|
+
console.log(`Message history: ${initialData.messages.length} messages`);
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Sending Messages
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
// Send text message
|
|
119
|
+
await conversation.sendMessage("How can you help me?");
|
|
120
|
+
|
|
121
|
+
// Send message with image
|
|
122
|
+
await conversation.sendMessage("Here's a screenshot of the issue", {
|
|
123
|
+
url: "https://example.com/screenshot.png"
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Handling Media
|
|
128
|
+
|
|
129
|
+
Both users and bots can send and receive images:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
const { conversation } = await client.conversation(options, {
|
|
133
|
+
onNewMessage: (message) => {
|
|
134
|
+
console.log(`${message.from_bot ? 'Bot' : 'User'}: ${message.text}`);
|
|
135
|
+
if (message.attached_media) {
|
|
136
|
+
console.log(` 📎 Image: ${message.attached_media.url}`);
|
|
137
|
+
// Display image in your UI
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// User sends an image
|
|
143
|
+
// In a real app, you'd get this URL from file upload
|
|
144
|
+
const imageUrl = "https://example.com/user-uploaded-image.jpg"; // Your upload logic
|
|
145
|
+
await conversation.sendMessage("Check out this image!", { url: imageUrl });
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Complete Example
|
|
149
|
+
|
|
150
|
+
Here's a complete implementation demonstrating the SDK usage:
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import { PrioriChat, Conversation, Message } from 'priori-chat-sdk';
|
|
154
|
+
|
|
155
|
+
class ChatApplication {
|
|
156
|
+
private conversation: Conversation | null = null;
|
|
157
|
+
private messages: Message[] = [];
|
|
158
|
+
|
|
159
|
+
async initialize() {
|
|
160
|
+
const client = new PrioriChat("your-api-key");
|
|
161
|
+
|
|
162
|
+
const { conversation, initialData } = await client.conversation(
|
|
163
|
+
{ user_id: "user-123", bot_id: "12345678-1234-1234-1234-123456789012" },
|
|
164
|
+
{
|
|
165
|
+
onInitialData: (data) => {
|
|
166
|
+
this.messages = data.messages;
|
|
167
|
+
this.renderMessages();
|
|
168
|
+
},
|
|
169
|
+
onNewMessage: (message) => {
|
|
170
|
+
this.messages.push(message);
|
|
171
|
+
this.renderMessage(message);
|
|
172
|
+
},
|
|
173
|
+
onError: (error) => {
|
|
174
|
+
console.error('Chat error:', error);
|
|
175
|
+
// Show error notification to user
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
this.conversation = conversation;
|
|
181
|
+
console.log(`Connected to conversation: ${conversation.id}`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async sendMessage(text: string) {
|
|
185
|
+
if (!this.conversation) return;
|
|
186
|
+
await this.conversation.sendMessage(text);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async sendImage(file: File) {
|
|
190
|
+
if (!this.conversation) return;
|
|
191
|
+
|
|
192
|
+
// Upload file and get URL (implement your upload logic)
|
|
193
|
+
const imageUrl = await this.uploadFile(file);
|
|
194
|
+
await this.conversation.sendMessage("Shared an image", { url: imageUrl });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private renderMessages() {
|
|
198
|
+
this.messages.forEach(msg => this.renderMessage(msg));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private renderMessage(message: Message) {
|
|
202
|
+
// Implement your UI rendering logic
|
|
203
|
+
const sender = message.from_bot ? 'Bot' : 'User';
|
|
204
|
+
console.log(`${sender}: ${message.text}`);
|
|
205
|
+
|
|
206
|
+
if (message.attached_media) {
|
|
207
|
+
console.log(` 📎 Image: ${message.attached_media.url}`);
|
|
208
|
+
// Display image in your UI
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private async uploadFile(file: File): Promise<string> {
|
|
213
|
+
// Implement your file upload logic
|
|
214
|
+
// Return the URL of the uploaded file
|
|
215
|
+
return "https://your-storage.com/uploaded-file.jpg";
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
disconnect() {
|
|
219
|
+
this.conversation?.disconnect();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Usage
|
|
224
|
+
const chat = new ChatApplication();
|
|
225
|
+
await chat.initialize();
|
|
226
|
+
|
|
227
|
+
// Send messages
|
|
228
|
+
await chat.sendMessage("Hello!");
|
|
229
|
+
|
|
230
|
+
// Handle file uploads
|
|
231
|
+
// chat.sendImage(selectedFile);
|
|
232
|
+
|
|
233
|
+
// Clean up when done
|
|
234
|
+
chat.disconnect();
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
For a complete working example, see [`examples/interactive-chat.ts`](./examples/interactive-chat.ts).
|
|
238
|
+
|
|
239
|
+
## API Reference
|
|
240
|
+
|
|
241
|
+
For detailed API documentation, see the generated TypeDoc documentation.
|
|
242
|
+
|
|
243
|
+
### Key Classes and Types
|
|
244
|
+
|
|
245
|
+
- `PrioriChat` - Main SDK client
|
|
246
|
+
- `Conversation` - Real-time conversation instance
|
|
247
|
+
- `ConversationOptions` - Configuration for conversation identification
|
|
248
|
+
- `ConversationCallbacks` - Event handlers for real-time updates
|
|
249
|
+
- `Message` - Message structure with text and optional media
|
|
250
|
+
- `AttachedMedia` - Media attachment with URL
|
|
251
|
+
|
|
252
|
+
## TypeScript Support
|
|
253
|
+
|
|
254
|
+
This SDK is written in TypeScript and provides full type safety for all operations.
|
package/dist/index.d.mts
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 };
|