@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.
Files changed (72) hide show
  1. package/README.md +440 -0
  2. package/dist/adapters/index.d.ts +2 -0
  3. package/dist/adapters/index.js +2 -0
  4. package/dist/adapters/widgeticAdapter.d.ts +185 -0
  5. package/dist/adapters/widgeticAdapter.js +766 -0
  6. package/dist/components/ActionBar.svelte +342 -0
  7. package/dist/components/ActionBar.svelte.d.ts +37 -0
  8. package/dist/components/AttachmentDisplay.svelte +547 -0
  9. package/dist/components/AttachmentDisplay.svelte.d.ts +12 -0
  10. package/dist/components/Chat.svelte +1253 -0
  11. package/dist/components/Chat.svelte.d.ts +112 -0
  12. package/dist/components/ChatHeader.svelte +182 -0
  13. package/dist/components/ChatHeader.svelte.d.ts +12 -0
  14. package/dist/components/ChatInput.svelte +290 -0
  15. package/dist/components/ChatInput.svelte.d.ts +15 -0
  16. package/dist/components/ChatMessages.svelte +996 -0
  17. package/dist/components/ChatMessages.svelte.d.ts +28 -0
  18. package/dist/components/CodeBlock.svelte +286 -0
  19. package/dist/components/CodeBlock.svelte.d.ts +10 -0
  20. package/dist/components/ContextPreview.svelte +66 -0
  21. package/dist/components/ContextPreview.svelte.d.ts +8 -0
  22. package/dist/components/LoadingIndicator.svelte +151 -0
  23. package/dist/components/LoadingIndicator.svelte.d.ts +9 -0
  24. package/dist/components/StatusIndicator.svelte +116 -0
  25. package/dist/components/StatusIndicator.svelte.d.ts +9 -0
  26. package/dist/components/SuggestionButtons.svelte +244 -0
  27. package/dist/components/SuggestionButtons.svelte.d.ts +26 -0
  28. package/dist/components/index.d.ts +12 -0
  29. package/dist/components/index.js +12 -0
  30. package/dist/config.d.ts +43 -0
  31. package/dist/config.js +67 -0
  32. package/dist/constants/colors.d.ts +20 -0
  33. package/dist/constants/colors.js +16 -0
  34. package/dist/index.d.ts +11 -0
  35. package/dist/index.js +20 -0
  36. package/dist/services/chatService.d.ts +72 -0
  37. package/dist/services/chatService.js +355 -0
  38. package/dist/services/index.d.ts +2 -0
  39. package/dist/services/index.js +2 -0
  40. package/dist/stores/chatStore.d.ts +46 -0
  41. package/dist/stores/chatStore.js +219 -0
  42. package/dist/stores/index.d.ts +2 -0
  43. package/dist/stores/index.js +2 -0
  44. package/dist/types/adapter.d.ts +86 -0
  45. package/dist/types/adapter.js +1 -0
  46. package/dist/types/api-temp.d.ts +61 -0
  47. package/dist/types/api-temp.js +42 -0
  48. package/dist/types/attachment.d.ts +84 -0
  49. package/dist/types/attachment.js +33 -0
  50. package/dist/types/chat.d.ts +114 -0
  51. package/dist/types/chat.js +1 -0
  52. package/dist/types/config.d.ts +89 -0
  53. package/dist/types/config.js +63 -0
  54. package/dist/types/context.d.ts +108 -0
  55. package/dist/types/context.js +11 -0
  56. package/dist/types/conversation.d.ts +45 -0
  57. package/dist/types/conversation.js +1 -0
  58. package/dist/types/events.d.ts +141 -0
  59. package/dist/types/events.js +1 -0
  60. package/dist/types/index.d.ts +16 -0
  61. package/dist/types/index.js +10 -0
  62. package/dist/types/message.d.ts +82 -0
  63. package/dist/types/message.js +1 -0
  64. package/dist/types/state.d.ts +117 -0
  65. package/dist/types/state.js +1 -0
  66. package/dist/utils/fileUtils.d.ts +93 -0
  67. package/dist/utils/fileUtils.js +299 -0
  68. package/dist/utils/index.d.ts +3 -0
  69. package/dist/utils/index.js +4 -0
  70. package/dist/utils/logger.d.ts +4 -0
  71. package/dist/utils/logger.js +28 -0
  72. package/package.json +104 -0
@@ -0,0 +1,299 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ 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);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ /**
38
+ * File type configuration mapping
39
+ */
40
+ export var fileTypeConfig = {
41
+ image: {
42
+ type: 'image',
43
+ icon: 'image',
44
+ preview: true,
45
+ accept: ['image/jpeg', 'image/png', 'image/svg+xml'],
46
+ maxSize: 10 * 1024 * 1024, // 10MB
47
+ },
48
+ video: {
49
+ type: 'video',
50
+ icon: 'video',
51
+ preview: true,
52
+ accept: ['video/mp4'],
53
+ maxSize: 100 * 1024 * 1024, // 100MB
54
+ },
55
+ document: {
56
+ type: 'document',
57
+ icon: 'file-text',
58
+ preview: false,
59
+ accept: ['application/pdf', 'text/plain', 'text/markdown', '.mermaid', '.md'],
60
+ maxSize: 25 * 1024 * 1024, // 25MB
61
+ },
62
+ other: {
63
+ type: 'other',
64
+ icon: 'file',
65
+ preview: false,
66
+ accept: ['*/*'],
67
+ maxSize: 10 * 1024 * 1024, // 10MB
68
+ },
69
+ };
70
+ /**
71
+ * Detect file type from MIME type and file name
72
+ */
73
+ export function detectFileType(mimeType, fileName) {
74
+ // Check by file extension first for special cases
75
+ if (fileName) {
76
+ var extension = getFileExtension(fileName);
77
+ if (extension === 'mermaid' || extension === 'md') {
78
+ return 'document';
79
+ }
80
+ }
81
+ if (mimeType.startsWith('image/')) {
82
+ return 'image';
83
+ }
84
+ if (mimeType.startsWith('video/')) {
85
+ return 'video';
86
+ }
87
+ if (mimeType === 'application/pdf' ||
88
+ mimeType === 'text/plain' ||
89
+ mimeType === 'text/markdown' ||
90
+ mimeType.startsWith('text/')) {
91
+ return 'document';
92
+ }
93
+ return 'other';
94
+ }
95
+ /**
96
+ * Validate a file for upload
97
+ */
98
+ export function validateFile(file, config) {
99
+ var fileType = detectFileType(file.type, file.name);
100
+ var typeConfig = fileTypeConfig[fileType];
101
+ // Check file size
102
+ var maxSize = (config === null || config === void 0 ? void 0 : config.maxSize) || typeConfig.maxSize;
103
+ if (file.size > maxSize) {
104
+ return {
105
+ valid: false,
106
+ error: "File too large. Maximum size is ".concat(formatFileSize(maxSize)),
107
+ code: 'FILE_TOO_LARGE'
108
+ };
109
+ }
110
+ // Check file type - handle both MIME types and file extensions
111
+ var allowedTypes = (config === null || config === void 0 ? void 0 : config.allowedTypes) || typeConfig.accept;
112
+ var extension = getFileExtension(file.name);
113
+ if (!allowedTypes.includes('*/*')) {
114
+ // Check if MIME type is allowed
115
+ var mimeTypeAllowed = allowedTypes.includes(file.type);
116
+ // Check if file extension is explicitly allowed (e.g., .mermaid)
117
+ var extensionAllowed = allowedTypes.includes(".".concat(extension)) || allowedTypes.includes(extension);
118
+ // Special handling for .mermaid and .md files with text/plain MIME type
119
+ var isSpecialTextFile = (extension === 'mermaid' || extension === 'md') &&
120
+ file.type === 'text/plain' &&
121
+ (allowedTypes.includes('text/plain') || allowedTypes.includes('text/markdown'));
122
+ if (!mimeTypeAllowed && !extensionAllowed && !isSpecialTextFile) {
123
+ return {
124
+ valid: false,
125
+ error: "File type not supported. Allowed types: ".concat(allowedTypes.join(', ')),
126
+ code: 'INVALID_TYPE'
127
+ };
128
+ }
129
+ }
130
+ return {
131
+ valid: true
132
+ };
133
+ }
134
+ /**
135
+ * Validate multiple files
136
+ */
137
+ export function validateFiles(files, config) {
138
+ if ((config === null || config === void 0 ? void 0 : config.maxFiles) && files.length > config.maxFiles) {
139
+ return {
140
+ valid: false,
141
+ error: "Too many files. Maximum allowed: ".concat(config.maxFiles),
142
+ code: 'TOO_MANY_FILES'
143
+ };
144
+ }
145
+ for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
146
+ var file = files_1[_i];
147
+ var validation = validateFile(file, config);
148
+ if (!validation.valid) {
149
+ return validation;
150
+ }
151
+ }
152
+ return {
153
+ valid: true
154
+ };
155
+ }
156
+ /**
157
+ * Format file size in human-readable format
158
+ */
159
+ export function formatFileSize(bytes) {
160
+ if (bytes === 0)
161
+ return '0 B';
162
+ var k = 1024;
163
+ var sizes = ['B', 'KB', 'MB', 'GB'];
164
+ var i = Math.floor(Math.log(bytes) / Math.log(k));
165
+ return "".concat(parseFloat((bytes / Math.pow(k, i)).toFixed(1)), " ").concat(sizes[i]);
166
+ }
167
+ /**
168
+ * Get file extension from filename
169
+ */
170
+ export function getFileExtension(filename) {
171
+ var _a;
172
+ return ((_a = filename.split('.').pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || '';
173
+ }
174
+ /**
175
+ * Generate unique file ID
176
+ */
177
+ export function generateFileId() {
178
+ return "file_".concat(Date.now(), "_").concat(Math.random().toString(36).substr(2, 9));
179
+ }
180
+ /**
181
+ * Create a preview URL for supported file types
182
+ */
183
+ export function createFilePreview(file) {
184
+ return new Promise(function (resolve) {
185
+ var fileType = detectFileType(file.type, file.name);
186
+ if (fileType === 'image') {
187
+ var reader = new FileReader();
188
+ reader.onload = function (e) {
189
+ var _a;
190
+ resolve(((_a = e.target) === null || _a === void 0 ? void 0 : _a.result) || null);
191
+ };
192
+ reader.onerror = function () { return resolve(null); };
193
+ reader.readAsDataURL(file);
194
+ }
195
+ else {
196
+ resolve(null);
197
+ }
198
+ });
199
+ }
200
+ /**
201
+ * Create a pending attachment from a file
202
+ */
203
+ export function createPendingAttachment(file) {
204
+ return __awaiter(this, void 0, Promise, function () {
205
+ var fileType, preview;
206
+ return __generator(this, function (_a) {
207
+ switch (_a.label) {
208
+ case 0:
209
+ fileType = detectFileType(file.type, file.name);
210
+ return [4 /*yield*/, createFilePreview(file)];
211
+ case 1:
212
+ preview = _a.sent();
213
+ return [2 /*return*/, {
214
+ id: generateFileId(),
215
+ file: file,
216
+ type: fileType,
217
+ preview: preview || undefined,
218
+ uploadStatus: 'pending',
219
+ uploadProgress: 0,
220
+ retryCount: 0
221
+ }];
222
+ }
223
+ });
224
+ });
225
+ }
226
+ /**
227
+ * Get MIME type accept string for file input
228
+ */
229
+ export function getAcceptString(types) {
230
+ var acceptTypes = new Set();
231
+ for (var _i = 0, types_1 = types; _i < types_1.length; _i++) {
232
+ var type = types_1[_i];
233
+ var config = fileTypeConfig[type];
234
+ for (var _a = 0, _b = config.accept; _a < _b.length; _a++) {
235
+ var mimeType = _b[_a];
236
+ acceptTypes.add(mimeType);
237
+ }
238
+ }
239
+ return Array.from(acceptTypes).join(',');
240
+ }
241
+ /**
242
+ * Check if file supports preview
243
+ */
244
+ export function supportsPreview(file) {
245
+ var fileType = detectFileType(file.type, file.name);
246
+ return fileTypeConfig[fileType].preview;
247
+ }
248
+ /**
249
+ * Get appropriate icon for file type
250
+ */
251
+ export function getFileIcon(file) {
252
+ var fileType = detectFileType(file.type, file.name);
253
+ return fileTypeConfig[fileType].icon;
254
+ }
255
+ /**
256
+ * Compress image file if needed
257
+ */
258
+ export function compressImage(file, options) {
259
+ return new Promise(function (resolve, reject) {
260
+ if (!file.type.startsWith('image/')) {
261
+ resolve(file);
262
+ return;
263
+ }
264
+ var canvas = document.createElement('canvas');
265
+ var ctx = canvas.getContext('2d');
266
+ var img = new Image();
267
+ img.onload = function () {
268
+ var _a = options.maxWidth, maxWidth = _a === void 0 ? 1920 : _a, _b = options.maxHeight, maxHeight = _b === void 0 ? 1080 : _b, _c = options.quality, quality = _c === void 0 ? 0.8 : _c;
269
+ var width = img.width, height = img.height;
270
+ // Calculate new dimensions
271
+ if (width > maxWidth) {
272
+ height = (height * maxWidth) / width;
273
+ width = maxWidth;
274
+ }
275
+ if (height > maxHeight) {
276
+ width = (width * maxHeight) / height;
277
+ height = maxHeight;
278
+ }
279
+ canvas.width = width;
280
+ canvas.height = height;
281
+ // Draw and compress
282
+ ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(img, 0, 0, width, height);
283
+ canvas.toBlob(function (blob) {
284
+ if (blob) {
285
+ var compressedFile = new File([blob], file.name, {
286
+ type: file.type,
287
+ lastModified: Date.now()
288
+ });
289
+ resolve(compressedFile);
290
+ }
291
+ else {
292
+ resolve(file);
293
+ }
294
+ }, file.type, quality);
295
+ };
296
+ img.onerror = function () { return resolve(file); };
297
+ img.src = URL.createObjectURL(file);
298
+ });
299
+ }
@@ -0,0 +1,3 @@
1
+ export { fileTypeConfig, detectFileType, validateFile, validateFiles, formatFileSize, getFileExtension, generateFileId, createFilePreview, createPendingAttachment, getAcceptString, supportsPreview, getFileIcon, compressImage } from './fileUtils.js';
2
+ export { setLogsEnabled, chatLog, chatWarn, chatError } from './logger.js';
3
+ export type { AttachmentType, AttachmentValidation, PendingAttachment, UploadStatus } from '../types/index.js';
@@ -0,0 +1,4 @@
1
+ // File utilities
2
+ export { fileTypeConfig, detectFileType, validateFile, validateFiles, formatFileSize, getFileExtension, generateFileId, createFilePreview, createPendingAttachment, getAcceptString, supportsPreview, getFileIcon, compressImage } from './fileUtils.js';
3
+ // Logger
4
+ export { setLogsEnabled, chatLog, chatWarn, chatError } from './logger.js';
@@ -0,0 +1,4 @@
1
+ export declare function setLogsEnabled(enabled: boolean): void;
2
+ export declare function chatLog(...args: any[]): void;
3
+ export declare function chatWarn(...args: any[]): void;
4
+ export declare function chatError(...args: any[]): void;
@@ -0,0 +1,28 @@
1
+ var logsEnabled = true;
2
+ export function setLogsEnabled(enabled) {
3
+ logsEnabled = enabled;
4
+ }
5
+ export function chatLog() {
6
+ var args = [];
7
+ for (var _i = 0; _i < arguments.length; _i++) {
8
+ args[_i] = arguments[_i];
9
+ }
10
+ if (logsEnabled)
11
+ console.log.apply(console, args);
12
+ }
13
+ export function chatWarn() {
14
+ var args = [];
15
+ for (var _i = 0; _i < arguments.length; _i++) {
16
+ args[_i] = arguments[_i];
17
+ }
18
+ if (logsEnabled)
19
+ console.warn.apply(console, args);
20
+ }
21
+ export function chatError() {
22
+ var args = [];
23
+ for (var _i = 0; _i < arguments.length; _i++) {
24
+ args[_i] = arguments[_i];
25
+ }
26
+ // Errors are always shown regardless of showLogs
27
+ console.error.apply(console, args);
28
+ }
package/package.json ADDED
@@ -0,0 +1,104 @@
1
+ {
2
+ "name": "@widgetic/chat",
3
+ "version": "0.1.4",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "svelte": "./dist/index.js",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "svelte": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "./components": {
15
+ "types": "./dist/components/index.d.ts",
16
+ "svelte": "./dist/components/index.js",
17
+ "default": "./dist/components/index.js"
18
+ },
19
+ "./stores": {
20
+ "types": "./dist/stores/index.d.ts",
21
+ "svelte": "./dist/stores/index.js",
22
+ "default": "./dist/stores/index.js"
23
+ },
24
+ "./services": {
25
+ "types": "./dist/services/index.d.ts",
26
+ "svelte": "./dist/services/index.js",
27
+ "default": "./dist/services/index.js"
28
+ },
29
+ "./types": {
30
+ "types": "./dist/types/index.d.ts",
31
+ "default": "./dist/types/index.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "scripts": {
38
+ "i": "npm run clean-modules && npm i && npm run lps",
39
+ "clean-modules": "rm -rf node_modules",
40
+ "lps": "volta run npm link --save @widgetic/api-sdk && volta run npm link --save @widgetic/design-system",
41
+ "dev": "npm run dev-sv -- --open",
42
+ "dev-sv": "NODE_ENV=development vite dev",
43
+ "build": "vite build",
44
+ "package": "svelte-package",
45
+ "package:watch": "svelte-package --watch",
46
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --diagnostic-sources \"js,svelte\"",
47
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
48
+ "lint": "prettier --check . && eslint .",
49
+ "format": "prettier --write ."
50
+ },
51
+ "peerDependencies": {
52
+ "svelte": "^5.25.6",
53
+ "@widgetic/api-sdk": "*",
54
+ "@widgetic/design-system": ">=0.3.0"
55
+ },
56
+ "dependencies": {
57
+ "highlight.js": "^11.11.1",
58
+ "socket.io-client": "^4.7.5"
59
+ },
60
+ "devDependencies": {
61
+ "@sveltejs/adapter-auto": "^3.0.0",
62
+ "@sveltejs/adapter-node": "^5.2.3",
63
+ "@sveltejs/kit": "^2.20.3",
64
+ "@sveltejs/package": "^2.3.7",
65
+ "@sveltejs/vite-plugin-svelte": "^5.0.3",
66
+ "@tailwindcss/postcss": "^4.1.4",
67
+ "@types/eslint": "^9.6.0",
68
+ "autoprefixer": "^10.4.20",
69
+ "eslint": "^9.0.0",
70
+ "eslint-config-prettier": "^9.1.0",
71
+ "eslint-plugin-svelte": "^2.36.0",
72
+ "postcss": "^8.5.3",
73
+ "prettier": "^3.1.1",
74
+ "prettier-plugin-svelte": "^3.1.2",
75
+ "svelte": "^5.25.6",
76
+ "svelte-check": "^4.0.0",
77
+ "tailwind-merge": "^2.5.2",
78
+ "tailwind-variants": "^0.2.1",
79
+ "tailwindcss": "^4.1.4",
80
+ "typescript": "^5.8.3",
81
+ "typescript-eslint": "^8.0.0",
82
+ "vite": "^6.2.6",
83
+ "@widgetic/api-sdk": "^1.0.10",
84
+ "@widgetic/design-system": "^0.3.89"
85
+ },
86
+ "overrides": {
87
+ "bits-ui": {
88
+ "@melt-ui/svelte": "^0.86.3"
89
+ },
90
+ "vaul-svelte": {
91
+ "bits-ui": {
92
+ "@melt-ui/svelte": "^0.86.3"
93
+ }
94
+ },
95
+ "svelte": "^5.25.6"
96
+ },
97
+ "publishConfig": {
98
+ "access": "public"
99
+ },
100
+ "repository": {
101
+ "type": "git",
102
+ "url": "https://gitlab.com/widgeticai/Frontend/chat.git"
103
+ }
104
+ }