@widgetic/chat 0.1.4
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 +440 -0
- package/dist/adapters/index.d.ts +2 -0
- package/dist/adapters/index.js +2 -0
- package/dist/adapters/widgeticAdapter.d.ts +185 -0
- package/dist/adapters/widgeticAdapter.js +766 -0
- package/dist/components/ActionBar.svelte +342 -0
- package/dist/components/ActionBar.svelte.d.ts +37 -0
- package/dist/components/AttachmentDisplay.svelte +547 -0
- package/dist/components/AttachmentDisplay.svelte.d.ts +12 -0
- package/dist/components/Chat.svelte +1253 -0
- package/dist/components/Chat.svelte.d.ts +112 -0
- package/dist/components/ChatHeader.svelte +182 -0
- package/dist/components/ChatHeader.svelte.d.ts +12 -0
- package/dist/components/ChatInput.svelte +290 -0
- package/dist/components/ChatInput.svelte.d.ts +15 -0
- package/dist/components/ChatMessages.svelte +996 -0
- package/dist/components/ChatMessages.svelte.d.ts +28 -0
- package/dist/components/CodeBlock.svelte +286 -0
- package/dist/components/CodeBlock.svelte.d.ts +10 -0
- package/dist/components/ContextPreview.svelte +66 -0
- package/dist/components/ContextPreview.svelte.d.ts +8 -0
- package/dist/components/LoadingIndicator.svelte +151 -0
- package/dist/components/LoadingIndicator.svelte.d.ts +9 -0
- package/dist/components/StatusIndicator.svelte +116 -0
- package/dist/components/StatusIndicator.svelte.d.ts +9 -0
- package/dist/components/SuggestionButtons.svelte +244 -0
- package/dist/components/SuggestionButtons.svelte.d.ts +26 -0
- package/dist/components/index.d.ts +12 -0
- package/dist/components/index.js +12 -0
- package/dist/config.d.ts +43 -0
- package/dist/config.js +67 -0
- package/dist/constants/colors.d.ts +20 -0
- package/dist/constants/colors.js +16 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +20 -0
- package/dist/services/chatService.d.ts +72 -0
- package/dist/services/chatService.js +355 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.js +2 -0
- package/dist/stores/chatStore.d.ts +46 -0
- package/dist/stores/chatStore.js +219 -0
- package/dist/stores/index.d.ts +2 -0
- package/dist/stores/index.js +2 -0
- package/dist/types/adapter.d.ts +86 -0
- package/dist/types/adapter.js +1 -0
- package/dist/types/api-temp.d.ts +61 -0
- package/dist/types/api-temp.js +42 -0
- package/dist/types/attachment.d.ts +84 -0
- package/dist/types/attachment.js +33 -0
- package/dist/types/chat.d.ts +114 -0
- package/dist/types/chat.js +1 -0
- package/dist/types/config.d.ts +89 -0
- package/dist/types/config.js +63 -0
- package/dist/types/context.d.ts +108 -0
- package/dist/types/context.js +11 -0
- package/dist/types/conversation.d.ts +45 -0
- package/dist/types/conversation.js +1 -0
- package/dist/types/events.d.ts +141 -0
- package/dist/types/events.js +1 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/index.js +10 -0
- package/dist/types/message.d.ts +82 -0
- package/dist/types/message.js +1 -0
- package/dist/types/state.d.ts +117 -0
- package/dist/types/state.js +1 -0
- package/dist/utils/fileUtils.d.ts +93 -0
- package/dist/utils/fileUtils.js +299 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/logger.d.ts +4 -0
- package/dist/utils/logger.js +28 -0
- package/package.json +104 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
23
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
+
function step(op) {
|
|
26
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
+
switch (op[0]) {
|
|
31
|
+
case 0: case 1: t = op; break;
|
|
32
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
+
default:
|
|
36
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
+
if (t[2]) _.ops.pop();
|
|
41
|
+
_.trys.pop(); continue;
|
|
42
|
+
}
|
|
43
|
+
op = body.call(thisArg, _);
|
|
44
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
import { chatLog } from '../utils/logger.js';
|
|
49
|
+
import { ConversationsApi, MessagesApi } from '@widgetic/api-sdk';
|
|
50
|
+
/**
|
|
51
|
+
* Chat service implementation using Widgetic API SDK
|
|
52
|
+
*/
|
|
53
|
+
var WidgeticChatService = /** @class */ (function () {
|
|
54
|
+
function WidgeticChatService(configuration) {
|
|
55
|
+
this.conversationsApi = new ConversationsApi(configuration);
|
|
56
|
+
this.messagesApi = new MessagesApi(configuration);
|
|
57
|
+
chatLog('ChatService initialized (mock mode)');
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get all conversations for a given context (canvas + component)
|
|
61
|
+
*/
|
|
62
|
+
WidgeticChatService.prototype.getConversations = function (contextId) {
|
|
63
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
64
|
+
var _a, canvasId, componentId;
|
|
65
|
+
return __generator(this, function (_b) {
|
|
66
|
+
_a = this.parseContextId(contextId), canvasId = _a[0], componentId = _a[1];
|
|
67
|
+
try {
|
|
68
|
+
// Mock implementation for now
|
|
69
|
+
chatLog('Getting conversations for:', { canvasId: canvasId, componentId: componentId });
|
|
70
|
+
return [2 /*return*/, []];
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.error('Failed to get conversations:', error);
|
|
74
|
+
throw new Error('Failed to load conversations');
|
|
75
|
+
}
|
|
76
|
+
return [2 /*return*/];
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Create a new conversation
|
|
82
|
+
*/
|
|
83
|
+
WidgeticChatService.prototype.createConversation = function (contextId, data) {
|
|
84
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
85
|
+
var _a, canvasId, componentId, request, mockConversation;
|
|
86
|
+
return __generator(this, function (_b) {
|
|
87
|
+
_a = this.parseContextId(contextId), canvasId = _a[0], componentId = _a[1];
|
|
88
|
+
request = {
|
|
89
|
+
title: data.title || 'New Chat',
|
|
90
|
+
completed: false
|
|
91
|
+
};
|
|
92
|
+
try {
|
|
93
|
+
chatLog('Creating conversation:', { canvasId: canvasId, componentId: componentId, request: request });
|
|
94
|
+
mockConversation = {
|
|
95
|
+
id: "conv_".concat(Date.now()),
|
|
96
|
+
title: request.title,
|
|
97
|
+
completed: request.completed,
|
|
98
|
+
createdAt: new Date(),
|
|
99
|
+
updatedAt: new Date(),
|
|
100
|
+
_unreadCount: 0,
|
|
101
|
+
_lastActivity: new Date(),
|
|
102
|
+
_isActive: false
|
|
103
|
+
};
|
|
104
|
+
return [2 /*return*/, mockConversation];
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
console.error('Failed to create conversation:', error);
|
|
108
|
+
throw new Error('Failed to create conversation');
|
|
109
|
+
}
|
|
110
|
+
return [2 /*return*/];
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Update an existing conversation
|
|
116
|
+
*/
|
|
117
|
+
WidgeticChatService.prototype.updateConversation = function (conversationId, updates) {
|
|
118
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
119
|
+
return __generator(this, function (_a) {
|
|
120
|
+
// We need to extract canvas and component IDs from the conversation
|
|
121
|
+
// This would typically be stored in the client state or passed differently
|
|
122
|
+
throw new Error('Update conversation not yet implemented - need context IDs');
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Delete a conversation
|
|
128
|
+
*/
|
|
129
|
+
WidgeticChatService.prototype.deleteConversation = function (conversationId) {
|
|
130
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
131
|
+
return __generator(this, function (_a) {
|
|
132
|
+
// Similar issue - need canvas and component IDs
|
|
133
|
+
throw new Error('Delete conversation not yet implemented - need context IDs');
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Get all messages for a conversation
|
|
139
|
+
*/
|
|
140
|
+
WidgeticChatService.prototype.getMessages = function (conversationId) {
|
|
141
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
142
|
+
return __generator(this, function (_a) {
|
|
143
|
+
try {
|
|
144
|
+
chatLog('Getting messages for conversation:', conversationId);
|
|
145
|
+
return [2 /*return*/, []];
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
console.error('Failed to get messages:', error);
|
|
149
|
+
return [2 /*return*/, []];
|
|
150
|
+
}
|
|
151
|
+
return [2 /*return*/];
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Send a new message
|
|
157
|
+
*/
|
|
158
|
+
WidgeticChatService.prototype.sendMessage = function (conversationId, data) {
|
|
159
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
160
|
+
var processedAttachments, i, attachment, input, stableUrl, url, _a, _b, url, request, mockMessage, error_1;
|
|
161
|
+
var _c, _d, _e, _f;
|
|
162
|
+
return __generator(this, function (_g) {
|
|
163
|
+
switch (_g.label) {
|
|
164
|
+
case 0:
|
|
165
|
+
_g.trys.push([0, 11, , 12]);
|
|
166
|
+
processedAttachments = [];
|
|
167
|
+
if (!(data.attachments && data.attachments.length > 0)) return [3 /*break*/, 10];
|
|
168
|
+
chatLog('Processing attachments:', data.attachments.length);
|
|
169
|
+
i = 0;
|
|
170
|
+
_g.label = 1;
|
|
171
|
+
case 1:
|
|
172
|
+
if (!(i < data.attachments.length)) return [3 /*break*/, 10];
|
|
173
|
+
attachment = data.attachments[i];
|
|
174
|
+
if (!(attachment && typeof attachment === 'object' && 'file' in attachment)) return [3 /*break*/, 6];
|
|
175
|
+
input = attachment;
|
|
176
|
+
stableUrl = input.uploadedUrl;
|
|
177
|
+
_a = stableUrl;
|
|
178
|
+
if (_a) return [3 /*break*/, 5];
|
|
179
|
+
if (!input.file) return [3 /*break*/, 3];
|
|
180
|
+
return [4 /*yield*/, this.fileToDataUrl(input.file)];
|
|
181
|
+
case 2:
|
|
182
|
+
_b = _g.sent();
|
|
183
|
+
return [3 /*break*/, 4];
|
|
184
|
+
case 3:
|
|
185
|
+
_b = '';
|
|
186
|
+
_g.label = 4;
|
|
187
|
+
case 4:
|
|
188
|
+
_a = (_b);
|
|
189
|
+
_g.label = 5;
|
|
190
|
+
case 5:
|
|
191
|
+
url = _a;
|
|
192
|
+
processedAttachments.push({
|
|
193
|
+
id: "att_".concat(Date.now(), "_").concat(i, "_").concat(Math.random().toString(36).substr(2, 9)),
|
|
194
|
+
url: url,
|
|
195
|
+
file_type: ((_c = input.file) === null || _c === void 0 ? void 0 : _c.type) || 'image/png',
|
|
196
|
+
file_name: ((_d = input.file) === null || _d === void 0 ? void 0 : _d.name) || 'attachment',
|
|
197
|
+
file_size: (_e = input.file) === null || _e === void 0 ? void 0 : _e.size,
|
|
198
|
+
metadata: {
|
|
199
|
+
originalName: (_f = input.file) === null || _f === void 0 ? void 0 : _f.name,
|
|
200
|
+
uploadedAt: new Date().toISOString(),
|
|
201
|
+
storage: stableUrl ? 'upload-service' : 'inline-data-url',
|
|
202
|
+
},
|
|
203
|
+
created_at: new Date().toISOString(),
|
|
204
|
+
display_order: i + 1,
|
|
205
|
+
});
|
|
206
|
+
return [3 /*break*/, 9];
|
|
207
|
+
case 6:
|
|
208
|
+
if (!(attachment instanceof File)) return [3 /*break*/, 8];
|
|
209
|
+
return [4 /*yield*/, this.fileToDataUrl(attachment)];
|
|
210
|
+
case 7:
|
|
211
|
+
url = _g.sent();
|
|
212
|
+
processedAttachments.push({
|
|
213
|
+
id: "att_".concat(Date.now(), "_").concat(i, "_").concat(Math.random().toString(36).substr(2, 9)),
|
|
214
|
+
url: url,
|
|
215
|
+
file_type: attachment.type,
|
|
216
|
+
file_name: attachment.name,
|
|
217
|
+
file_size: attachment.size,
|
|
218
|
+
metadata: {
|
|
219
|
+
originalName: attachment.name,
|
|
220
|
+
uploadedAt: new Date().toISOString(),
|
|
221
|
+
storage: 'inline-data-url',
|
|
222
|
+
},
|
|
223
|
+
created_at: new Date().toISOString(),
|
|
224
|
+
display_order: i + 1,
|
|
225
|
+
});
|
|
226
|
+
return [3 /*break*/, 9];
|
|
227
|
+
case 8:
|
|
228
|
+
processedAttachments.push(attachment);
|
|
229
|
+
_g.label = 9;
|
|
230
|
+
case 9:
|
|
231
|
+
i++;
|
|
232
|
+
return [3 /*break*/, 1];
|
|
233
|
+
case 10:
|
|
234
|
+
request = {
|
|
235
|
+
messageContent: data.messageContent,
|
|
236
|
+
messageType: 'user',
|
|
237
|
+
attachments: processedAttachments
|
|
238
|
+
};
|
|
239
|
+
chatLog('Sending message:', { conversationId: conversationId, request: request });
|
|
240
|
+
mockMessage = {
|
|
241
|
+
id: "msg_".concat(Date.now(), "_").concat(Math.random().toString(36).substr(2, 9)),
|
|
242
|
+
conversationId: conversationId,
|
|
243
|
+
messageContent: data.messageContent,
|
|
244
|
+
messageType: 'user',
|
|
245
|
+
attachments: processedAttachments,
|
|
246
|
+
isCommit: false,
|
|
247
|
+
userId: 'test_user',
|
|
248
|
+
createdAt: new Date(),
|
|
249
|
+
updatedAt: new Date(),
|
|
250
|
+
_optimistic: false,
|
|
251
|
+
_failed: false,
|
|
252
|
+
_retryCount: 0
|
|
253
|
+
};
|
|
254
|
+
chatLog('Mock message created:', mockMessage);
|
|
255
|
+
return [2 /*return*/, mockMessage];
|
|
256
|
+
case 11:
|
|
257
|
+
error_1 = _g.sent();
|
|
258
|
+
console.error('Failed to send message:', error_1);
|
|
259
|
+
throw new Error('Failed to send message');
|
|
260
|
+
case 12: return [2 /*return*/];
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* Update a message
|
|
267
|
+
*/
|
|
268
|
+
WidgeticChatService.prototype.updateMessage = function (messageId, updates) {
|
|
269
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
270
|
+
return __generator(this, function (_a) {
|
|
271
|
+
chatLog('Update message not yet implemented:', messageId, updates);
|
|
272
|
+
throw new Error('Update message not yet implemented');
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* Delete a message
|
|
278
|
+
*/
|
|
279
|
+
WidgeticChatService.prototype.deleteMessage = function (messageId) {
|
|
280
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
281
|
+
return __generator(this, function (_a) {
|
|
282
|
+
chatLog('Delete message not yet implemented:', messageId);
|
|
283
|
+
throw new Error('Delete message not yet implemented');
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* Read a file as a data URL (persistable in DB until upload service exists).
|
|
289
|
+
*/
|
|
290
|
+
WidgeticChatService.prototype.fileToDataUrl = function (file) {
|
|
291
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
292
|
+
return __generator(this, function (_a) {
|
|
293
|
+
chatLog('Reading attachment as data URL:', file.name);
|
|
294
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
295
|
+
var reader = new FileReader();
|
|
296
|
+
reader.onload = function () { return resolve(String(reader.result)); };
|
|
297
|
+
reader.onerror = function () { return reject(reader.error); };
|
|
298
|
+
reader.readAsDataURL(file);
|
|
299
|
+
})];
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
};
|
|
303
|
+
/** @deprecated Use fileToDataUrl — blob URLs are not reload-safe */
|
|
304
|
+
WidgeticChatService.prototype.uploadFile = function (file) {
|
|
305
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
306
|
+
return __generator(this, function (_a) {
|
|
307
|
+
return [2 /*return*/, this.fileToDataUrl(file)];
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
};
|
|
311
|
+
/**
|
|
312
|
+
* Process AI response (placeholder for LLM integration)
|
|
313
|
+
*/
|
|
314
|
+
WidgeticChatService.prototype.processAIResponse = function (conversationId, message) {
|
|
315
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
316
|
+
var aiResponse;
|
|
317
|
+
return __generator(this, function (_a) {
|
|
318
|
+
switch (_a.label) {
|
|
319
|
+
case 0:
|
|
320
|
+
// This would integrate with the AI service to generate responses
|
|
321
|
+
// For now, return a mock response
|
|
322
|
+
// Add a small delay to ensure unique timestamp
|
|
323
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 10); })];
|
|
324
|
+
case 1:
|
|
325
|
+
// This would integrate with the AI service to generate responses
|
|
326
|
+
// For now, return a mock response
|
|
327
|
+
// Add a small delay to ensure unique timestamp
|
|
328
|
+
_a.sent();
|
|
329
|
+
aiResponse = __assign(__assign({}, message), { id: "ai_".concat(Date.now(), "_").concat(Math.random().toString(36).substr(2, 9)), messageType: 'assistant', messageContent: "AI response to: ".concat(message.messageContent || '[attachment only]'), createdAt: new Date(), updatedAt: new Date(), userId: 'ai_assistant', attachments: [], _optimistic: false, _failed: false, _retryCount: 0 });
|
|
330
|
+
chatLog('AI response generated:', aiResponse);
|
|
331
|
+
return [2 /*return*/, aiResponse];
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
};
|
|
336
|
+
/**
|
|
337
|
+
* Parse context ID to extract canvas and component IDs.
|
|
338
|
+
* Supports "canvas_id:component_id" format or a single ID (used as both).
|
|
339
|
+
*/
|
|
340
|
+
WidgeticChatService.prototype.parseContextId = function (contextId) {
|
|
341
|
+
var parts = contextId.split(':');
|
|
342
|
+
if (parts.length === 2) {
|
|
343
|
+
return [parts[0], parts[1]];
|
|
344
|
+
}
|
|
345
|
+
return [contextId, contextId];
|
|
346
|
+
};
|
|
347
|
+
return WidgeticChatService;
|
|
348
|
+
}());
|
|
349
|
+
export { WidgeticChatService };
|
|
350
|
+
/**
|
|
351
|
+
* Create a chat service instance with the given configuration
|
|
352
|
+
*/
|
|
353
|
+
export function createChatService(configuration) {
|
|
354
|
+
return new WidgeticChatService(configuration);
|
|
355
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ChatState, ChatAction, ChatMessage, ChatConversation, InputState, UIState, ActiveContextState } from '../types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Main chat store instance
|
|
4
|
+
*/
|
|
5
|
+
export declare const chatStore: {
|
|
6
|
+
subscribe: (this: void, run: import("svelte/store").Subscriber<ChatState>, invalidate?: () => void) => import("svelte/store").Unsubscriber;
|
|
7
|
+
/**
|
|
8
|
+
* Dispatch actions to update the store
|
|
9
|
+
*/
|
|
10
|
+
dispatch: (action: ChatAction) => void;
|
|
11
|
+
/**
|
|
12
|
+
* Reset the store to initial state
|
|
13
|
+
*/
|
|
14
|
+
reset: () => void;
|
|
15
|
+
/**
|
|
16
|
+
* Helper methods for common operations
|
|
17
|
+
*/
|
|
18
|
+
actions: {
|
|
19
|
+
setActiveContext: (contextId: string | null, isExpanded?: boolean) => void;
|
|
20
|
+
updateInput: (updates: Partial<InputState>) => void;
|
|
21
|
+
updateUI: (updates: Partial<UIState>) => void;
|
|
22
|
+
addMessage: (conversationId: string, message: ChatMessage) => void;
|
|
23
|
+
setConversations: (contextId: string, conversations: ChatConversation[]) => void;
|
|
24
|
+
setMessages: (conversationId: string, messages: ChatMessage[]) => void;
|
|
25
|
+
updateMessageId: (conversationId: string, oldId: string, newId: string) => void;
|
|
26
|
+
updateMessageContent: (conversationId: string, messageId: string, messageContent: string) => void;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Derived stores for specific parts of the state
|
|
31
|
+
*/
|
|
32
|
+
export declare const activeContext: {
|
|
33
|
+
subscribe: (callback: (value: ActiveContextState) => void) => import("svelte/store").Unsubscriber;
|
|
34
|
+
};
|
|
35
|
+
export declare const inputState: {
|
|
36
|
+
subscribe: (callback: (value: InputState) => void) => import("svelte/store").Unsubscriber;
|
|
37
|
+
};
|
|
38
|
+
export declare const uiState: {
|
|
39
|
+
subscribe: (callback: (value: UIState) => void) => import("svelte/store").Unsubscriber;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Reactive getters for current state
|
|
43
|
+
*/
|
|
44
|
+
export declare const getCurrentState: () => ChatState;
|
|
45
|
+
export declare const getCurrentMessages: (conversationId: string) => ChatMessage[];
|
|
46
|
+
export declare const getCurrentConversations: (contextId: string) => ChatConversation[];
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
13
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
14
|
+
if (ar || !(i in from)) {
|
|
15
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
16
|
+
ar[i] = from[i];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
20
|
+
};
|
|
21
|
+
import { writable } from 'svelte/store';
|
|
22
|
+
import { chatLog } from '../utils/logger.js';
|
|
23
|
+
/**
|
|
24
|
+
* Initial state for the chat store
|
|
25
|
+
*/
|
|
26
|
+
var initialState = {
|
|
27
|
+
activeContext: {
|
|
28
|
+
contextId: null,
|
|
29
|
+
isExpanded: false
|
|
30
|
+
},
|
|
31
|
+
input: {
|
|
32
|
+
text: '',
|
|
33
|
+
attachments: [],
|
|
34
|
+
isRecording: false,
|
|
35
|
+
isSending: false,
|
|
36
|
+
isUploading: false,
|
|
37
|
+
uploadProgress: {},
|
|
38
|
+
history: [],
|
|
39
|
+
historyIndex: -1
|
|
40
|
+
},
|
|
41
|
+
ui: {
|
|
42
|
+
isExpanded: false,
|
|
43
|
+
isLoading: false,
|
|
44
|
+
activeTab: 'chat',
|
|
45
|
+
errorMessage: null,
|
|
46
|
+
lastAction: null
|
|
47
|
+
},
|
|
48
|
+
messages: {
|
|
49
|
+
byConversationId: {},
|
|
50
|
+
isLoading: {},
|
|
51
|
+
errors: {}
|
|
52
|
+
},
|
|
53
|
+
conversations: {
|
|
54
|
+
byContextId: {},
|
|
55
|
+
isLoading: {},
|
|
56
|
+
errors: {}
|
|
57
|
+
},
|
|
58
|
+
availableContexts: []
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Create the chat store with rune-like reactive state
|
|
62
|
+
*/
|
|
63
|
+
function createChatStore() {
|
|
64
|
+
var _a = writable(initialState), subscribe = _a.subscribe, set = _a.set, update = _a.update;
|
|
65
|
+
return {
|
|
66
|
+
subscribe: subscribe,
|
|
67
|
+
/**
|
|
68
|
+
* Dispatch actions to update the store
|
|
69
|
+
*/
|
|
70
|
+
dispatch: function (action) {
|
|
71
|
+
update(function (state) {
|
|
72
|
+
var _a, _b, _c, _d, _e, _f;
|
|
73
|
+
var _g;
|
|
74
|
+
chatLog("Chat Action: ".concat(action.type), action.payload);
|
|
75
|
+
switch (action.type) {
|
|
76
|
+
case 'SET_ACTIVE_CONTEXT':
|
|
77
|
+
return __assign(__assign({}, state), { activeContext: __assign(__assign({}, state.activeContext), { contextId: action.payload.contextId, isExpanded: (_g = action.payload.isExpanded) !== null && _g !== void 0 ? _g : state.activeContext.isExpanded, expandedAt: action.payload.contextId ? new Date() : undefined }) });
|
|
78
|
+
case 'UPDATE_INPUT':
|
|
79
|
+
return __assign(__assign({}, state), { input: __assign(__assign({}, state.input), action.payload) });
|
|
80
|
+
case 'UPDATE_UI':
|
|
81
|
+
return __assign(__assign({}, state), { ui: __assign(__assign({}, state.ui), action.payload) });
|
|
82
|
+
case 'ADD_MESSAGE':
|
|
83
|
+
var _h = action.payload, conversationId = _h.conversationId, message = _h.message;
|
|
84
|
+
return __assign(__assign({}, state), { messages: __assign(__assign({}, state.messages), { byConversationId: __assign(__assign({}, state.messages.byConversationId), (_a = {}, _a[conversationId] = __spreadArray(__spreadArray([], (state.messages.byConversationId[conversationId] || []), true), [
|
|
85
|
+
message
|
|
86
|
+
], false), _a)) }) });
|
|
87
|
+
case 'UPDATE_MESSAGE':
|
|
88
|
+
var _j = action.payload, convId = _j.conversationId, messageId_1 = _j.messageId, updates_1 = _j.updates;
|
|
89
|
+
var existingMessages = state.messages.byConversationId[convId] || [];
|
|
90
|
+
return __assign(__assign({}, state), { messages: __assign(__assign({}, state.messages), { byConversationId: __assign(__assign({}, state.messages.byConversationId), (_b = {}, _b[convId] = existingMessages.map(function (msg) {
|
|
91
|
+
return msg.id === messageId_1 ? __assign(__assign({}, msg), updates_1) : msg;
|
|
92
|
+
}), _b)) }) });
|
|
93
|
+
case 'ADD_CONVERSATION':
|
|
94
|
+
var _k = action.payload, contextId = _k.contextId, conversation = _k.conversation;
|
|
95
|
+
return __assign(__assign({}, state), { conversations: __assign(__assign({}, state.conversations), { byContextId: __assign(__assign({}, state.conversations.byContextId), (_c = {}, _c[contextId] = __spreadArray(__spreadArray([], (state.conversations.byContextId[contextId] || []), true), [
|
|
96
|
+
conversation
|
|
97
|
+
], false), _c)) }) });
|
|
98
|
+
case 'UPDATE_CONVERSATION':
|
|
99
|
+
var _l = action.payload, ctxId = _l.contextId, convIdUpd_1 = _l.conversationId, convUpdates_1 = _l.updates;
|
|
100
|
+
var existingConversations = state.conversations.byContextId[ctxId] || [];
|
|
101
|
+
return __assign(__assign({}, state), { conversations: __assign(__assign({}, state.conversations), { byContextId: __assign(__assign({}, state.conversations.byContextId), (_d = {}, _d[ctxId] = existingConversations.map(function (conv) {
|
|
102
|
+
return conv.id === convIdUpd_1 ? __assign(__assign({}, conv), convUpdates_1) : conv;
|
|
103
|
+
}), _d)) }) });
|
|
104
|
+
case 'SET_LOADING':
|
|
105
|
+
var _m = action.payload, key = _m.key, loading = _m.loading;
|
|
106
|
+
return __assign(__assign({}, state), { messages: __assign(__assign({}, state.messages), { isLoading: __assign(__assign({}, state.messages.isLoading), (_e = {}, _e[key] = loading, _e)) }) });
|
|
107
|
+
case 'SET_ERROR':
|
|
108
|
+
var _o = action.payload, errorKey = _o.key, error = _o.error;
|
|
109
|
+
return __assign(__assign({}, state), { messages: __assign(__assign({}, state.messages), { errors: __assign(__assign({}, state.messages.errors), (_f = {}, _f[errorKey] = error, _f)) }) });
|
|
110
|
+
default:
|
|
111
|
+
chatLog("Unknown action type: ".concat(action.type));
|
|
112
|
+
return state;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
/**
|
|
117
|
+
* Reset the store to initial state
|
|
118
|
+
*/
|
|
119
|
+
reset: function () { return set(initialState); },
|
|
120
|
+
/**
|
|
121
|
+
* Helper methods for common operations
|
|
122
|
+
*/
|
|
123
|
+
actions: {
|
|
124
|
+
setActiveContext: function (contextId, isExpanded) {
|
|
125
|
+
update(function (state) { return (__assign(__assign({}, state), { activeContext: {
|
|
126
|
+
contextId: contextId,
|
|
127
|
+
isExpanded: isExpanded !== null && isExpanded !== void 0 ? isExpanded : state.activeContext.isExpanded,
|
|
128
|
+
expandedAt: contextId ? new Date() : undefined
|
|
129
|
+
} })); });
|
|
130
|
+
},
|
|
131
|
+
updateInput: function (updates) {
|
|
132
|
+
update(function (state) { return (__assign(__assign({}, state), { input: __assign(__assign({}, state.input), updates) })); });
|
|
133
|
+
},
|
|
134
|
+
updateUI: function (updates) {
|
|
135
|
+
update(function (state) { return (__assign(__assign({}, state), { ui: __assign(__assign({}, state.ui), updates) })); });
|
|
136
|
+
},
|
|
137
|
+
addMessage: function (conversationId, message) {
|
|
138
|
+
update(function (state) {
|
|
139
|
+
var _a;
|
|
140
|
+
return (__assign(__assign({}, state), { messages: __assign(__assign({}, state.messages), { byConversationId: __assign(__assign({}, state.messages.byConversationId), (_a = {}, _a[conversationId] = __spreadArray(__spreadArray([], (state.messages.byConversationId[conversationId] || []), true), [
|
|
141
|
+
message
|
|
142
|
+
], false), _a)) }) }));
|
|
143
|
+
});
|
|
144
|
+
},
|
|
145
|
+
setConversations: function (contextId, conversations) {
|
|
146
|
+
update(function (state) {
|
|
147
|
+
var _a;
|
|
148
|
+
return (__assign(__assign({}, state), { conversations: __assign(__assign({}, state.conversations), { byContextId: __assign(__assign({}, state.conversations.byContextId), (_a = {}, _a[contextId] = conversations, _a)) }) }));
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
setMessages: function (conversationId, messages) {
|
|
152
|
+
update(function (state) {
|
|
153
|
+
var _a;
|
|
154
|
+
return (__assign(__assign({}, state), { messages: __assign(__assign({}, state.messages), { byConversationId: __assign(__assign({}, state.messages.byConversationId), (_a = {}, _a[conversationId] = messages, _a)) }) }));
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
updateMessageId: function (conversationId, oldId, newId) {
|
|
158
|
+
update(function (state) {
|
|
159
|
+
var _a;
|
|
160
|
+
var msgs = state.messages.byConversationId[conversationId];
|
|
161
|
+
if (!msgs)
|
|
162
|
+
return state;
|
|
163
|
+
return __assign(__assign({}, state), { messages: __assign(__assign({}, state.messages), { byConversationId: __assign(__assign({}, state.messages.byConversationId), (_a = {}, _a[conversationId] = msgs.map(function (msg) {
|
|
164
|
+
return msg.id === oldId ? __assign(__assign({}, msg), { id: newId }) : msg;
|
|
165
|
+
}), _a)) }) });
|
|
166
|
+
});
|
|
167
|
+
},
|
|
168
|
+
updateMessageContent: function (conversationId, messageId, messageContent) {
|
|
169
|
+
update(function (state) {
|
|
170
|
+
var _a;
|
|
171
|
+
var msgs = state.messages.byConversationId[conversationId];
|
|
172
|
+
if (!msgs)
|
|
173
|
+
return state;
|
|
174
|
+
return __assign(__assign({}, state), { messages: __assign(__assign({}, state.messages), { byConversationId: __assign(__assign({}, state.messages.byConversationId), (_a = {}, _a[conversationId] = msgs.map(function (msg) {
|
|
175
|
+
return msg.id === messageId ? __assign(__assign({}, msg), { messageContent: messageContent }) : msg;
|
|
176
|
+
}), _a)) }) });
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Main chat store instance
|
|
184
|
+
*/
|
|
185
|
+
export var chatStore = createChatStore();
|
|
186
|
+
/**
|
|
187
|
+
* Derived stores for specific parts of the state
|
|
188
|
+
*/
|
|
189
|
+
export var activeContext = {
|
|
190
|
+
subscribe: function (callback) {
|
|
191
|
+
return chatStore.subscribe(function (state) { return callback(state.activeContext); });
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
export var inputState = {
|
|
195
|
+
subscribe: function (callback) {
|
|
196
|
+
return chatStore.subscribe(function (state) { return callback(state.input); });
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
export var uiState = {
|
|
200
|
+
subscribe: function (callback) {
|
|
201
|
+
return chatStore.subscribe(function (state) { return callback(state.ui); });
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Reactive getters for current state
|
|
206
|
+
*/
|
|
207
|
+
export var getCurrentState = function () {
|
|
208
|
+
var currentState;
|
|
209
|
+
chatStore.subscribe(function (state) { currentState = state; })();
|
|
210
|
+
return currentState;
|
|
211
|
+
};
|
|
212
|
+
export var getCurrentMessages = function (conversationId) {
|
|
213
|
+
var state = getCurrentState();
|
|
214
|
+
return state.messages.byConversationId[conversationId] || [];
|
|
215
|
+
};
|
|
216
|
+
export var getCurrentConversations = function (contextId) {
|
|
217
|
+
var state = getCurrentState();
|
|
218
|
+
return state.conversations.byContextId[contextId] || [];
|
|
219
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { chatStore, activeContext, inputState, uiState, getCurrentState, getCurrentMessages, getCurrentConversations } from './chatStore.js';
|
|
2
|
+
export type { ChatState, ChatAction, InputState, UIState, ActiveContextState, MessageState, ConversationState } from '../types/index.js';
|