@toothfairyai/sdk 0.1.0-beta.1
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/LICENSE +21 -0
- package/README.md +395 -0
- package/dist/chat/ChatManager.d.ts +22 -0
- package/dist/chat/ChatManager.d.ts.map +1 -0
- package/dist/chat/ChatManager.js +89 -0
- package/dist/chat/ChatManager.js.map +1 -0
- package/dist/client.d.ts +38 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +126 -0
- package/dist/client.js.map +1 -0
- package/dist/documents/DocumentManager.d.ts +34 -0
- package/dist/documents/DocumentManager.d.ts.map +1 -0
- package/dist/documents/DocumentManager.js +268 -0
- package/dist/documents/DocumentManager.js.map +1 -0
- package/dist/entities/EntityManager.d.ts +24 -0
- package/dist/entities/EntityManager.d.ts.map +1 -0
- package/dist/entities/EntityManager.js +56 -0
- package/dist/entities/EntityManager.js.map +1 -0
- package/dist/folders/FolderManager.d.ts +30 -0
- package/dist/folders/FolderManager.d.ts.map +1 -0
- package/dist/folders/FolderManager.js +86 -0
- package/dist/folders/FolderManager.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +42 -0
- package/dist/index.js.map +1 -0
- package/dist/prompts/PromptManager.d.ts +23 -0
- package/dist/prompts/PromptManager.d.ts.map +1 -0
- package/dist/prompts/PromptManager.js +83 -0
- package/dist/prompts/PromptManager.js.map +1 -0
- package/dist/streaming/StreamingManager.d.ts +53 -0
- package/dist/streaming/StreamingManager.d.ts.map +1 -0
- package/dist/streaming/StreamingManager.js +217 -0
- package/dist/streaming/StreamingManager.js.map +1 -0
- package/dist/types/index.d.ts +273 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +17 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ToothFairyAI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
# ToothFairyAI SDK for JavaScript/TypeScript
|
|
2
|
+
|
|
3
|
+
A comprehensive, TypeScript-first SDK for integrating with the ToothFairyAI API. Built for developers who want to embed AI agent functionality into their applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @toothfairyai/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { ToothFairyClient } from '@toothfairyai/sdk';
|
|
15
|
+
|
|
16
|
+
const client = new ToothFairyClient({
|
|
17
|
+
apiKey: 'your-api-key',
|
|
18
|
+
workspaceId: 'your-workspace-id'
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// Send a message to an agent
|
|
22
|
+
const response = await client.chat.sendToAgent('Hello', 'agent-id');
|
|
23
|
+
console.log(response.agentResponse);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- 🔄 **Real-time Streaming**: Event-driven streaming for live agent interactions
|
|
29
|
+
- 📄 **Document Management**: Upload, download, and search through documents
|
|
30
|
+
- 🏷️ **Entity Management**: Manage topics, intents, and NER entities
|
|
31
|
+
- 📁 **Folder Organization**: Organize documents hierarchically
|
|
32
|
+
- 🤖 **Prompt Management**: Create and manage AI prompts
|
|
33
|
+
- 💬 **Chat Management**: Handle conversations and messaging
|
|
34
|
+
- 🔧 **TypeScript First**: Comprehensive type definitions included
|
|
35
|
+
- 📦 **Modular Design**: Use only what you need
|
|
36
|
+
|
|
37
|
+
## Architecture
|
|
38
|
+
|
|
39
|
+
The SDK is organized into logical modules:
|
|
40
|
+
|
|
41
|
+
- **`client.chat`** - Chat and messaging operations
|
|
42
|
+
- **`client.streaming`** - Real-time streaming functionality
|
|
43
|
+
- **`client.documents`** - Document upload, download, search, and management
|
|
44
|
+
- **`client.entities`** - Entity management (topics, intents, NER)
|
|
45
|
+
- **`client.folders`** - Folder organization
|
|
46
|
+
- **`client.prompts`** - AI prompt management
|
|
47
|
+
|
|
48
|
+
## Configuration
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
const client = new ToothFairyClient({
|
|
52
|
+
apiKey: 'your-api-key', // Required: Your ToothFairyAI API key
|
|
53
|
+
workspaceId: 'your-workspace-id', // Required: Your workspace ID
|
|
54
|
+
baseUrl: 'https://api.toothfairyai.com', // Optional: API base URL
|
|
55
|
+
aiUrl: 'https://ai.toothfairyai.com', // Optional: AI service URL
|
|
56
|
+
aiStreamUrl: 'https://ai-stream.toothfairyai.com', // Optional: Streaming URL
|
|
57
|
+
timeout: 120000 // Optional: Request timeout in ms
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Usage Examples
|
|
62
|
+
|
|
63
|
+
### Real-time Streaming Chat
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
// Start a streaming conversation
|
|
67
|
+
const session = await client.streaming.sendToAgent('Hello', 'agent-id');
|
|
68
|
+
|
|
69
|
+
// Listen for different types of events
|
|
70
|
+
session.on('status', (data) => {
|
|
71
|
+
console.log('Connection status:', data.status);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
session.on('progress', (data) => {
|
|
75
|
+
console.log('Agent processing:', data.processing_status);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
session.on('data', (data) => {
|
|
79
|
+
console.log('Agent response:', data.text);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
session.on('complete', (data) => {
|
|
83
|
+
console.log('Final response:', data);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
session.on('error', (error) => {
|
|
87
|
+
console.error('Streaming error:', error);
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Document Management
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
// Upload a document
|
|
95
|
+
const uploadResult = await client.documents.upload('/path/to/file.pdf', {
|
|
96
|
+
onProgress: (percent, loaded, total) => {
|
|
97
|
+
console.log(`Upload progress: ${percent}%`);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Create a document from URL
|
|
102
|
+
const document = await client.documents.createFromPath(
|
|
103
|
+
'https://example.com/policy.pdf',
|
|
104
|
+
'user-123',
|
|
105
|
+
{
|
|
106
|
+
title: 'Company Policy',
|
|
107
|
+
folderId: 'folder-id'
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// Search documents semantically
|
|
112
|
+
const searchResults = await client.documents.search('company policy', {
|
|
113
|
+
topK: 5
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Download a file
|
|
117
|
+
await client.documents.download(
|
|
118
|
+
'filename.pdf',
|
|
119
|
+
'/local/path/file.pdf',
|
|
120
|
+
{
|
|
121
|
+
onProgress: (percent, loaded, total) => {
|
|
122
|
+
console.log(`Download progress: ${percent}%`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Chat Management
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
// Create a new chat
|
|
132
|
+
const chat = await client.chat.create({
|
|
133
|
+
name: 'Customer Support Chat',
|
|
134
|
+
primaryRole: 'agent-id',
|
|
135
|
+
customerId: 'customer-123',
|
|
136
|
+
externalParticipantId: '+1234567890',
|
|
137
|
+
channelSettings: {
|
|
138
|
+
sms: {
|
|
139
|
+
isEnabled: true,
|
|
140
|
+
recipient: '+1234567890',
|
|
141
|
+
providerID: 'twilio'
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Send a message in the chat
|
|
147
|
+
const message = await client.chat.createMessage({
|
|
148
|
+
chatID: chat.id,
|
|
149
|
+
text: 'I need help with my order',
|
|
150
|
+
role: 'user',
|
|
151
|
+
userID: 'customer-123'
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Get agent response (non-streaming)
|
|
155
|
+
const response = await client.chat.sendToAgent(
|
|
156
|
+
'Hello, I need help',
|
|
157
|
+
'agent-id',
|
|
158
|
+
{
|
|
159
|
+
phoneNumber: '+1234567890',
|
|
160
|
+
customerId: 'customer-123',
|
|
161
|
+
customerInfo: {
|
|
162
|
+
name: 'John Doe',
|
|
163
|
+
email: 'john@example.com'
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Entity Management
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
// Create a topic entity
|
|
173
|
+
const topic = await client.entities.create(
|
|
174
|
+
'user-123',
|
|
175
|
+
'Customer Support',
|
|
176
|
+
'topic',
|
|
177
|
+
{
|
|
178
|
+
description: 'Customer support related topics',
|
|
179
|
+
emoji: '🎧'
|
|
180
|
+
}
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
// List all entities
|
|
184
|
+
const entities = await client.entities.list();
|
|
185
|
+
|
|
186
|
+
// Search entities
|
|
187
|
+
const supportEntities = await client.entities.search('support');
|
|
188
|
+
|
|
189
|
+
// Get entities by type
|
|
190
|
+
const topics = await client.entities.getByType('topic');
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Folder Management
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
// Create a folder
|
|
197
|
+
const folder = await client.folders.create('user-123', 'Documents', {
|
|
198
|
+
description: 'Document collection',
|
|
199
|
+
emoji: '📁'
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// Create a subfolder
|
|
203
|
+
const subfolder = await client.folders.create('user-123', 'Contracts', {
|
|
204
|
+
parent: folder.id
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// Get folder hierarchy
|
|
208
|
+
const folderTree = await client.folders.getTree();
|
|
209
|
+
|
|
210
|
+
// Search folders
|
|
211
|
+
const contractFolders = await client.folders.search('contract');
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Prompt Management
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
// Create a prompt
|
|
218
|
+
const prompt = await client.prompts.create('user-123', {
|
|
219
|
+
type: 'system',
|
|
220
|
+
label: 'Customer Service Prompt',
|
|
221
|
+
promptLength: 150,
|
|
222
|
+
interpolationString: 'You are a helpful customer service agent...',
|
|
223
|
+
scope: 'customer_support'
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// Get prompts by type
|
|
227
|
+
const systemPrompts = await client.prompts.getByType('system');
|
|
228
|
+
|
|
229
|
+
// Get prompts for a specific agent
|
|
230
|
+
const agentPrompts = await client.prompts.getByAgent('agent-id');
|
|
231
|
+
|
|
232
|
+
// Clone a prompt with modifications
|
|
233
|
+
const clonedPrompt = await client.prompts.clone('prompt-id', 'user-123', {
|
|
234
|
+
label: 'Modified Customer Service Prompt'
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Error Handling
|
|
239
|
+
|
|
240
|
+
The SDK provides structured error handling:
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
import { ToothFairyError } from '@toothfairyai/sdk';
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
const response = await client.chat.sendToAgent('Hello', 'invalid-agent');
|
|
247
|
+
} catch (error) {
|
|
248
|
+
if (error instanceof ToothFairyError) {
|
|
249
|
+
console.error('API Error:', error.message);
|
|
250
|
+
console.error('Error Code:', error.code);
|
|
251
|
+
console.error('Status Code:', error.statusCode);
|
|
252
|
+
console.error('Response:', error.response);
|
|
253
|
+
} else {
|
|
254
|
+
console.error('Unknown error:', error);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Integration Examples
|
|
260
|
+
|
|
261
|
+
### Express.js Server
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
import express from 'express';
|
|
265
|
+
import { ToothFairyClient } from '@toothfairyai/sdk';
|
|
266
|
+
|
|
267
|
+
const app = express();
|
|
268
|
+
const client = new ToothFairyClient({
|
|
269
|
+
apiKey: process.env.TOOTHFAIRY_API_KEY!,
|
|
270
|
+
workspaceId: process.env.TOOTHFAIRY_WORKSPACE_ID!
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
app.post('/chat', async (req, res) => {
|
|
274
|
+
const { message, agentId } = req.body;
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
const response = await client.chat.sendToAgent(message, agentId);
|
|
278
|
+
res.json(response);
|
|
279
|
+
} catch (error) {
|
|
280
|
+
res.status(500).json({ error: error.message });
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
app.listen(3000);
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### Next.js API Route
|
|
288
|
+
|
|
289
|
+
```typescript
|
|
290
|
+
// pages/api/chat.ts
|
|
291
|
+
import { NextApiRequest, NextApiResponse } from 'next';
|
|
292
|
+
import { ToothFairyClient } from '@toothfairyai/sdk';
|
|
293
|
+
|
|
294
|
+
const client = new ToothFairyClient({
|
|
295
|
+
apiKey: process.env.TOOTHFAIRY_API_KEY!,
|
|
296
|
+
workspaceId: process.env.TOOTHFAIRY_WORKSPACE_ID!
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
300
|
+
const { message, agentId } = req.body;
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
const response = await client.chat.sendToAgent(message, agentId);
|
|
304
|
+
res.status(200).json(response);
|
|
305
|
+
} catch (error) {
|
|
306
|
+
res.status(500).json({ error: error.message });
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### React Component with Streaming
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
import React, { useState, useEffect } from 'react';
|
|
315
|
+
import { ToothFairyClient } from '@toothfairyai/sdk';
|
|
316
|
+
|
|
317
|
+
const client = new ToothFairyClient({
|
|
318
|
+
apiKey: process.env.REACT_APP_TOOTHFAIRY_API_KEY!,
|
|
319
|
+
workspaceId: process.env.REACT_APP_TOOTHFAIRY_WORKSPACE_ID!
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
function ChatComponent() {
|
|
323
|
+
const [messages, setMessages] = useState<string[]>([]);
|
|
324
|
+
const [currentResponse, setCurrentResponse] = useState('');
|
|
325
|
+
|
|
326
|
+
const sendMessage = async (message: string) => {
|
|
327
|
+
const session = await client.streaming.sendToAgent(message, 'agent-id');
|
|
328
|
+
|
|
329
|
+
session.on('data', (data) => {
|
|
330
|
+
setCurrentResponse(prev => prev + data.text);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
session.on('complete', () => {
|
|
334
|
+
setMessages(prev => [...prev, currentResponse]);
|
|
335
|
+
setCurrentResponse('');
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
return (
|
|
340
|
+
<div>
|
|
341
|
+
{messages.map((msg, i) => <div key={i}>{msg}</div>)}
|
|
342
|
+
{currentResponse && <div>{currentResponse}</div>}
|
|
343
|
+
</div>
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
## Comparison with CLI
|
|
349
|
+
|
|
350
|
+
| Feature | SDK (`@toothfairyai/sdk`) | CLI (`@toothfairyai/cli`) |
|
|
351
|
+
|---------|---------------------------|---------------------------|
|
|
352
|
+
| **Purpose** | Programmatic integration in applications | Interactive command-line operations |
|
|
353
|
+
| **Usage** | `import { ToothFairyClient } from '@toothfairyai/sdk'` | `toothfairy stream "message" --agent-id xyz` |
|
|
354
|
+
| **Dependencies** | Minimal (axios only) | CLI tools (commander, chalk, ora) |
|
|
355
|
+
| **TypeScript** | TypeScript-first with comprehensive types | Basic TypeScript support |
|
|
356
|
+
| **Streaming** | EventEmitter-based real-time events | Callback-based streaming |
|
|
357
|
+
| **Error Handling** | Structured error classes | CLI-friendly error messages |
|
|
358
|
+
| **Use Cases** | Web apps, servers, integrations | Testing, debugging, CI/CD |
|
|
359
|
+
|
|
360
|
+
## Development
|
|
361
|
+
|
|
362
|
+
### Building
|
|
363
|
+
|
|
364
|
+
```bash
|
|
365
|
+
npm run build
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### Testing
|
|
369
|
+
|
|
370
|
+
```bash
|
|
371
|
+
npm test
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### Linting
|
|
375
|
+
|
|
376
|
+
```bash
|
|
377
|
+
npm run lint
|
|
378
|
+
npm run lint:fix
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
## Requirements
|
|
382
|
+
|
|
383
|
+
- Node.js >= 14.0.0
|
|
384
|
+
- TypeScript >= 4.9.5 (for development)
|
|
385
|
+
|
|
386
|
+
## License
|
|
387
|
+
|
|
388
|
+
MIT
|
|
389
|
+
|
|
390
|
+
## Support
|
|
391
|
+
|
|
392
|
+
For issues and questions:
|
|
393
|
+
- GitHub Issues: [Report an issue](https://gitea.toothfairyai.com/ToothFairyAI/tooth-fairy-website/toothfairy-sdk/issues)
|
|
394
|
+
- Documentation: [Full API documentation](https://docs.toothfairyai.com)
|
|
395
|
+
- Email: support@toothfairyai.com
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Chat, ChatCreateData, Message, MessageCreateData, AgentResponse, ListResponse } from '../types';
|
|
2
|
+
import { IToothFairyClient } from '../client';
|
|
3
|
+
export declare class ChatManager {
|
|
4
|
+
private client;
|
|
5
|
+
constructor(client: IToothFairyClient);
|
|
6
|
+
create(chatData: ChatCreateData): Promise<Chat>;
|
|
7
|
+
update(chatData: Partial<Chat> & {
|
|
8
|
+
id: string;
|
|
9
|
+
}): Promise<Chat>;
|
|
10
|
+
get(chatId: string): Promise<Chat>;
|
|
11
|
+
list(limit?: number, offset?: number): Promise<ListResponse<Chat>>;
|
|
12
|
+
createMessage(messageData: MessageCreateData): Promise<Message>;
|
|
13
|
+
getMessage(messageId: string): Promise<Message>;
|
|
14
|
+
sendToAgent(message: string, agentId: string, options?: {
|
|
15
|
+
phoneNumber?: string;
|
|
16
|
+
customerId?: string;
|
|
17
|
+
providerId?: string;
|
|
18
|
+
customerInfo?: Record<string, any>;
|
|
19
|
+
}): Promise<AgentResponse>;
|
|
20
|
+
private generateCustomerId;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=ChatManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatManager.d.ts","sourceRoot":"","sources":["../../src/chat/ChatManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,cAAc,EACd,OAAO,EACP,iBAAiB,EAEjB,aAAa,EACb,YAAY,EACb,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AA6B9C,qBAAa,WAAW;IACV,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,iBAAiB;IAQvC,MAAM,CAAC,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAU/C,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAU/D,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAWlC,IAAI,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAqBlE,aAAa,CAAC,WAAW,EAAE,iBAAiB,GAAG,OAAO,CAAC,OAAO,CAAC;IAU/D,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAY/C,WAAW,CACf,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,OAAO,GAAE;QACP,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;KAC/B,GACL,OAAO,CAAC,aAAa,CAAC;IAiEzB,OAAO,CAAC,kBAAkB;CAU3B"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChatManager = void 0;
|
|
4
|
+
class ChatManager {
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
}
|
|
8
|
+
async create(chatData) {
|
|
9
|
+
return this.client.request('POST', 'chat/create', chatData);
|
|
10
|
+
}
|
|
11
|
+
async update(chatData) {
|
|
12
|
+
return this.client.request('POST', 'chat/update', chatData);
|
|
13
|
+
}
|
|
14
|
+
async get(chatId) {
|
|
15
|
+
return this.client.request('GET', `chat/get/${chatId}`);
|
|
16
|
+
}
|
|
17
|
+
async list(limit, offset) {
|
|
18
|
+
const params = {};
|
|
19
|
+
if (limit)
|
|
20
|
+
params.limit = limit;
|
|
21
|
+
if (offset)
|
|
22
|
+
params.offset = offset;
|
|
23
|
+
const response = await this.client.request('GET', 'chat/list', null, { params });
|
|
24
|
+
return {
|
|
25
|
+
items: Array.isArray(response) ? response : [],
|
|
26
|
+
total: Array.isArray(response) ? response.length : 0,
|
|
27
|
+
limit,
|
|
28
|
+
offset
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async createMessage(messageData) {
|
|
32
|
+
return this.client.request('POST', 'chat_message/create', messageData);
|
|
33
|
+
}
|
|
34
|
+
async getMessage(messageId) {
|
|
35
|
+
return this.client.request('GET', `chat_message/get/${messageId}`);
|
|
36
|
+
}
|
|
37
|
+
async sendToAgent(message, agentId, options = {}) {
|
|
38
|
+
const { phoneNumber = '+1234567890', customerId = this.generateCustomerId(message), providerId = 'default-sms-provider', customerInfo = {} } = options;
|
|
39
|
+
const chatData = {
|
|
40
|
+
name: customerId,
|
|
41
|
+
primaryRole: agentId,
|
|
42
|
+
externalParticipantId: phoneNumber,
|
|
43
|
+
channelSettings: {
|
|
44
|
+
sms: {
|
|
45
|
+
isEnabled: true,
|
|
46
|
+
recipient: phoneNumber,
|
|
47
|
+
providerID: providerId,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
customerId,
|
|
51
|
+
customerInfo,
|
|
52
|
+
isAIReplying: true,
|
|
53
|
+
};
|
|
54
|
+
const createdChat = await this.create(chatData);
|
|
55
|
+
const messageData = {
|
|
56
|
+
chatID: createdChat.id,
|
|
57
|
+
text: message,
|
|
58
|
+
role: 'user',
|
|
59
|
+
userID: 'SDK',
|
|
60
|
+
};
|
|
61
|
+
const createdMessage = await this.createMessage(messageData);
|
|
62
|
+
const agentData = {
|
|
63
|
+
chatid: createdChat.id,
|
|
64
|
+
messages: [
|
|
65
|
+
{
|
|
66
|
+
text: createdMessage.text,
|
|
67
|
+
role: createdMessage.role,
|
|
68
|
+
userID: createdMessage.userID || 'System User',
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
agentid: agentId,
|
|
72
|
+
};
|
|
73
|
+
const agentResponse = await this.client.aiRequest('POST', 'chatter', agentData);
|
|
74
|
+
return {
|
|
75
|
+
chatId: createdChat.id,
|
|
76
|
+
messageId: createdMessage.id,
|
|
77
|
+
agentResponse,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
generateCustomerId(message) {
|
|
81
|
+
const hash = Math.abs(message.split('').reduce((a, b) => {
|
|
82
|
+
a = (a << 5) - a + b.charCodeAt(0);
|
|
83
|
+
return a & a;
|
|
84
|
+
}, 0)) % 10000;
|
|
85
|
+
return `sdk-user-${hash}`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.ChatManager = ChatManager;
|
|
89
|
+
//# sourceMappingURL=ChatManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatManager.js","sourceRoot":"","sources":["../../src/chat/ChatManager.ts"],"names":[],"mappings":";;;AAsCA,MAAa,WAAW;IACtB,YAAoB,MAAyB;QAAzB,WAAM,GAAN,MAAM,CAAmB;IAAG,CAAC;IAQjD,KAAK,CAAC,MAAM,CAAC,QAAwB;QACnC,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAO,MAAM,EAAE,aAAa,EAAE,QAAQ,CAAC,CAAC;IACpE,CAAC;IAQD,KAAK,CAAC,MAAM,CAAC,QAAwC;QACnD,OAAO,IAAI,CAAC,MAAM,CAA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AAC,GAAG,CACnB,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;YAChC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YACnC,OAAO,CAAC,GAAG,CAAC,CAAC;QACf,CAAC,EAAE,CAAC,CAAC,CACN,GAAG,KAAK,CAAC;QAEV,OAAO,YAAY,IAAI,EAAE,CAAC;IAC5B,CAAC;CACF;AAvKD,kCAuKC"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { ToothFairyClientConfig } from './types';
|
|
3
|
+
import { ChatManager } from './chat/ChatManager';
|
|
4
|
+
import { DocumentManager } from './documents/DocumentManager';
|
|
5
|
+
import { EntityManager } from './entities/EntityManager';
|
|
6
|
+
import { FolderManager } from './folders/FolderManager';
|
|
7
|
+
import { PromptManager } from './prompts/PromptManager';
|
|
8
|
+
import { StreamingManager } from './streaming/StreamingManager';
|
|
9
|
+
export interface IToothFairyClient {
|
|
10
|
+
getConfig(): Required<ToothFairyClientConfig>;
|
|
11
|
+
getWorkspaceId(): string;
|
|
12
|
+
request<T = any>(method: 'GET' | 'POST' | 'PUT' | 'DELETE', endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
|
|
13
|
+
aiRequest<T = any>(method: 'GET' | 'POST' | 'PUT' | 'DELETE', endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
|
|
14
|
+
getStreamingUrl(endpoint: string): string;
|
|
15
|
+
chat: ChatManager;
|
|
16
|
+
}
|
|
17
|
+
export declare class ToothFairyClient implements IToothFairyClient {
|
|
18
|
+
private readonly httpClient;
|
|
19
|
+
private readonly config;
|
|
20
|
+
readonly chat: ChatManager;
|
|
21
|
+
readonly documents: DocumentManager;
|
|
22
|
+
readonly entities: EntityManager;
|
|
23
|
+
readonly folders: FolderManager;
|
|
24
|
+
readonly prompts: PromptManager;
|
|
25
|
+
readonly streaming: StreamingManager;
|
|
26
|
+
constructor(config: ToothFairyClientConfig);
|
|
27
|
+
getConfig(): Required<ToothFairyClientConfig>;
|
|
28
|
+
getWorkspaceId(): string;
|
|
29
|
+
request<T = any>(method: 'GET' | 'POST' | 'PUT' | 'DELETE', endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
|
|
30
|
+
aiRequest<T = any>(method: 'GET' | 'POST' | 'PUT' | 'DELETE', endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
|
|
31
|
+
getStreamingUrl(endpoint: string): string;
|
|
32
|
+
testConnection(): Promise<boolean>;
|
|
33
|
+
getHealth(): Promise<{
|
|
34
|
+
status: string;
|
|
35
|
+
timestamp: string;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAc,EAAiB,kBAAkB,EAAiB,MAAM,OAAO,CAAC;AAChF,OAAO,EAAE,sBAAsB,EAAgC,MAAM,SAAS,CAAC;AAC/E,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAKhE,MAAM,WAAW,iBAAiB;IAChC,SAAS,IAAI,QAAQ,CAAC,sBAAsB,CAAC,CAAC;IAC9C,cAAc,IAAI,MAAM,CAAC;IACzB,OAAO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