@widgetic/chat 0.1.6 → 0.1.8

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.
@@ -1,6 +1,7 @@
1
1
  import type { ChatConfig, ChatContext } from '../types/index.js';
2
+ import type { AttachmentInput } from '../types/attachment.js';
2
3
  interface ChatMethods {
3
- sendMessage: (content: string, attachments?: File[]) => Promise<void>;
4
+ sendMessage: (content: string, attachments?: Array<File | AttachmentInput>) => Promise<void>;
4
5
  addAssistantMessage: (content: string, skipPersist?: boolean) => void;
5
6
  addUserMessage: (content: string, attachments?: Array<{
6
7
  id: string;
@@ -119,6 +120,14 @@ interface Props {
119
120
  resizeExpandsContainer?: boolean;
120
121
  /** Delta in pixels, plus the resulting chat container height (for parent-owned resize). */
121
122
  onMessagesResize?: (deltaPx: number, nextContainerHeightPx?: number) => void;
123
+ /**
124
+ * Widget Git HEAD from the host (panel lastCommitId). Used so Head / Restore chrome
125
+ * still appears when assistant commit bubbles were never persisted.
126
+ */
127
+ widgetHeadCommitSha?: string | null;
128
+ /** Host is running widget codegen — send button becomes stop. */
129
+ isGenerating?: boolean;
130
+ onStopGeneration?: () => void;
122
131
  }
123
132
  declare const Chat: import("svelte").Component<Props, {}, "">;
124
133
  type Chat = ReturnType<typeof Chat>;
@@ -9,10 +9,21 @@
9
9
  isRecording: boolean;
10
10
  isSending: boolean;
11
11
  isUploading: boolean;
12
+ isGenerating?: boolean;
13
+ /**
14
+ * When false, skip the in-input attachments tray (parent renders it in the chat footer).
15
+ * Drag-drop on `.chat-input-ct` still works.
16
+ */
17
+ showAttachmentsTray?: boolean;
12
18
  onSend: (content: string, attachments?: AttachmentInput[]) => void;
19
+ onStop?: () => void;
13
20
  onInputChange: (text: string) => void;
14
21
  onAttachmentsChange: (attachments: AttachmentInput[]) => void;
22
+ /** OS file drag-and-drop onto the input area (not attachment reorder). */
23
+ onFilesDropped?: (files: File[]) => void;
15
24
  placeholder?: string;
25
+ class?: string;
26
+ style?: string;
16
27
  }
17
28
 
18
29
  let {
@@ -21,20 +32,65 @@
21
32
  isRecording,
22
33
  isSending,
23
34
  isUploading,
24
- onSend,
35
+ isGenerating = false,
36
+ showAttachmentsTray = true,
37
+ onSend,
38
+ onStop,
25
39
  onInputChange,
26
40
  onAttachmentsChange,
27
- placeholder = "Type your message..."
41
+ onFilesDropped,
42
+ placeholder = "Type your message...",
43
+ class: className = '',
44
+ style = '',
28
45
  }: Props = $props();
29
46
 
47
+ let isFileDragOver = $state(false);
48
+
49
+ function isOsFileDrag(event: DragEvent): boolean {
50
+ return !!event.dataTransfer?.types?.includes('Files');
51
+ }
52
+
53
+ function handleFileDragEnter(event: DragEvent) {
54
+ if (!isOsFileDrag(event)) return;
55
+ event.preventDefault();
56
+ isFileDragOver = true;
57
+ }
58
+
59
+ function handleFileDragOver(event: DragEvent) {
60
+ if (!isOsFileDrag(event)) return;
61
+ event.preventDefault();
62
+ if (event.dataTransfer) event.dataTransfer.dropEffect = 'copy';
63
+ isFileDragOver = true;
64
+ }
65
+
66
+ function handleFileDragLeave(event: DragEvent) {
67
+ if (!isOsFileDrag(event)) return;
68
+ const related = event.relatedTarget as Node | null;
69
+ if (related && (event.currentTarget as HTMLElement).contains(related)) return;
70
+ isFileDragOver = false;
71
+ }
72
+
73
+ function handleFileDrop(event: DragEvent) {
74
+ if (!isOsFileDrag(event)) return;
75
+ event.preventDefault();
76
+ event.stopPropagation();
77
+ isFileDragOver = false;
78
+ const droppedFiles = Array.from(event.dataTransfer?.files || []).filter(
79
+ (file) => file && file.size > 0,
80
+ );
81
+ if (droppedFiles.length === 0) return;
82
+ chatLog('[ChatInput] OS files dropped:', droppedFiles.length);
83
+ onFilesDropped?.(droppedFiles);
84
+ }
85
+
30
86
  let textArea: HTMLTextAreaElement;
31
87
 
32
88
  onMount(() => {
33
- chatLog('ChatInput mounted for value:', value);
89
+ // chatLog('ChatInput mounted for value:', value);
34
90
  });
35
91
 
36
92
  onDestroy(() => {
37
- chatLog('ChatInput destroyed for value:', value);
93
+ // chatLog('ChatInput destroyed for value:', value);
38
94
  });
39
95
 
40
96
  function handleSubmit() {
@@ -58,11 +114,54 @@
58
114
  }
59
115
  }
60
116
 
117
+ const createdObjectUrls = new Set<string>();
118
+
119
+ function getAttachmentPreviewUrl(attachment: AttachmentInput): string {
120
+ if (attachment.preview) {
121
+ return attachment.preview;
122
+ }
123
+ if (!attachment.file) {
124
+ return '';
125
+ }
126
+ const objectUrl = URL.createObjectURL(attachment.file);
127
+ createdObjectUrls.add(objectUrl);
128
+ attachment.preview = objectUrl;
129
+ return objectUrl;
130
+ }
131
+
132
+ function isImageAttachment(attachment: AttachmentInput): boolean {
133
+ return (
134
+ attachment.type === 'image' ||
135
+ !!attachment.file?.type?.startsWith('image/') ||
136
+ !!attachment.preview
137
+ );
138
+ }
139
+
61
140
  function removeAttachment(index: number) {
141
+ const removed = attachments[index];
142
+ if (removed?.preview && removed.preview.startsWith('blob:')) {
143
+ try {
144
+ URL.revokeObjectURL(removed.preview);
145
+ createdObjectUrls.delete(removed.preview);
146
+ } catch {
147
+ /* ignore error */
148
+ }
149
+ }
62
150
  const newAttachments = attachments.filter((_, i) => i !== index);
63
151
  onAttachmentsChange(newAttachments);
64
152
  }
65
153
 
154
+ onDestroy(() => {
155
+ for (const url of createdObjectUrls) {
156
+ try {
157
+ URL.revokeObjectURL(url);
158
+ } catch {
159
+ /* ignore error */
160
+ }
161
+ }
162
+ createdObjectUrls.clear();
163
+ });
164
+
66
165
  let draggingAttachmentIndex: number | null = $state(null);
67
166
 
68
167
  function handleAttachmentDragStart(index: number, e: DragEvent) {
@@ -74,11 +173,14 @@
74
173
  }
75
174
 
76
175
  function handleAttachmentDragOver(e: DragEvent) {
176
+ // Do not hijack OS file drops — those are handled on the input container.
177
+ if (isOsFileDrag(e)) return;
77
178
  e.preventDefault();
78
179
  if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
79
180
  }
80
181
 
81
182
  function handleAttachmentDrop(targetIndex: number, e: DragEvent) {
183
+ if (isOsFileDrag(e)) return;
82
184
  e.preventDefault();
83
185
  if (draggingAttachmentIndex === null || draggingAttachmentIndex === targetIndex) {
84
186
  draggingAttachmentIndex = null;
@@ -98,8 +200,7 @@
98
200
  let lightboxSrc: string | null = $state(null);
99
201
 
100
202
  function openLightbox(attachment: AttachmentInput) {
101
- const src = attachment.preview || URL.createObjectURL(attachment.file);
102
- lightboxSrc = src;
203
+ lightboxSrc = getAttachmentPreviewUrl(attachment);
103
204
  }
104
205
 
105
206
  // Auto-resize textarea up to max-height, then scroll
@@ -114,7 +215,7 @@
114
215
  // Update textarea value when prop changes (for clearing)
115
216
  $effect(() => {
116
217
  if (textArea && textArea.value !== value) {
117
- chatLog('ChatInput: Updating textarea value from', textArea.value, 'to', value);
218
+ // chatLog('ChatInput: Updating textarea value from', textArea.value, 'to', value);
118
219
  textArea.value = value;
119
220
  // Re-trigger auto-resize after value change
120
221
  textArea.style.height = 'auto';
@@ -134,15 +235,25 @@
134
235
 
135
236
  // Debug value changes
136
237
  $effect(() => {
137
- chatLog('ChatInput: Value prop changed to:', value);
238
+ // chatLog('ChatInput: Value prop changed to:', value);
138
239
  });
139
240
  </script>
140
241
 
141
- <div class="chat-input-ct border-t border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800">
142
- <!-- Attachments Preview -->
143
- {#if attachments.length > 0}
242
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
243
+ <div
244
+ class="chat-input-ct border-t border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 transition-colors {className}
245
+ {isFileDragOver ? 'chat-input-file-drag-over border-2 border-dashed border-blue-400 bg-blue-50/70 dark:bg-blue-900/20' : ''}"
246
+ {style}
247
+ ondragenter={handleFileDragEnter}
248
+ ondragover={handleFileDragOver}
249
+ ondragleave={handleFileDragLeave}
250
+ ondrop={handleFileDrop}
251
+ >
252
+ <!-- Attachments Preview (skipped when parent owns the footer tray) -->
253
+ {#if showAttachmentsTray && attachments.length > 0}
144
254
  <div class="attachments-preview-ct flex flex-wrap gap-2 p-3 border-b border-gray-100 dark:border-gray-700">
145
- {#each attachments as attachment, index}
255
+ {#each attachments as attachment, index (attachment.preview || attachment.file?.name || index)}
256
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
146
257
  <div
147
258
  class="attachment-preview-item-ct relative bg-gray-50 dark:bg-gray-700 rounded-lg overflow-hidden"
148
259
  draggable="true"
@@ -152,12 +263,12 @@
152
263
  ondragend={handleAttachmentDragEnd}
153
264
  >
154
265
  <span class="attachment-index-badge absolute top-0.5 left-0.5 z-10 w-4 h-4 flex items-center justify-center rounded-full bg-blue-600 text-white text-[9px] font-bold shadow">{index + 1}</span>
155
- {#if attachment.file.type.startsWith('image/')}
266
+ {#if isImageAttachment(attachment)}
156
267
  <!-- svelte-ignore a11y_click_events_have_key_events -->
157
268
  <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
158
269
  <img
159
- src={attachment.preview || URL.createObjectURL(attachment.file)}
160
- alt={attachment.file.name}
270
+ src={getAttachmentPreviewUrl(attachment)}
271
+ alt={attachment.file?.name || 'attachment'}
161
272
  class="image-preview-ct w-16 h-16 object-cover cursor-pointer"
162
273
  onclick={() => openLightbox(attachment)}
163
274
  />
@@ -165,13 +276,13 @@
165
276
  <!-- File Preview -->
166
277
  <div class="file-preview-ct flex items-center gap-2 p-3 w-32">
167
278
  <span class="text-lg">📄</span>
168
- <span class="text-xs text-gray-600 dark:text-gray-300 truncate flex-1">{attachment.file.name}</span>
279
+ <span class="text-xs text-gray-600 dark:text-gray-300 truncate flex-1">{attachment.file?.name || 'attachment'}</span>
169
280
  </div>
170
281
  {/if}
171
282
 
172
283
  <!-- Remove Button -->
173
284
  <button
174
- class="absolute top-1 right-1 w-5 h-5 bg-red-500 text-white rounded-full flex items-center justify-center text-xs hover:bg-red-600"
285
+ class="remove-attachment-btn absolute top-1 right-1 w-5 h-5 bg-red-500 text-white rounded-full flex items-center justify-center text-xs hover:bg-red-600 cursor-pointer"
175
286
  onclick={() => removeAttachment(index)}
176
287
  aria-label="Remove attachment"
177
288
  >
@@ -200,21 +311,33 @@
200
311
  class="flex-1 bg-transparent border-none outline-none resize-y text-sm placeholder-gray-500 dark:text-gray-100 dark:placeholder-gray-400"
201
312
  style="min-height: 30px; max-height: 120px; overflow-y: auto;"
202
313
  rows="1"
203
- disabled={isSending}
314
+ disabled={isSending && !isGenerating}
204
315
  ></textarea>
205
316
 
206
317
  <div class="send-button-ct flex items-center gap-2">
207
318
  <!-- Send Button -->
208
319
  <button
209
- class="send-button-btn w-8 h-8 rounded-lg bg-blue-600 hover:bg-blue-700 flex items-center justify-center transition-colors"
210
- class:bg-gray-300={(!value.trim() && attachments.length === 0) || isSending}
211
- class:cursor-not-allowed={(!value.trim() && attachments.length === 0) || isSending}
212
- onclick={handleSubmit}
213
- disabled={(!value.trim() && attachments.length === 0) || isSending}
214
- aria-label={isSending ? 'Sending message...' : 'Send message'}
215
- title={isSending ? 'Sending message...' : 'Send message'}
320
+ class="send-button-btn w-8 h-8 rounded-lg flex items-center justify-center transition-colors"
321
+ class:bg-blue-600={!isGenerating}
322
+ class:hover:bg-blue-700={!isGenerating}
323
+ class:bg-red-600={isGenerating}
324
+ class:hover:bg-red-700={isGenerating}
325
+ class:bg-gray-300={!isGenerating && ((!value.trim() && attachments.length === 0) || isSending)}
326
+ class:cursor-not-allowed={!isGenerating && ((!value.trim() && attachments.length === 0) || isSending)}
327
+ onclick={() => {
328
+ if (isGenerating) {
329
+ onStop?.();
330
+ return;
331
+ }
332
+ handleSubmit();
333
+ }}
334
+ disabled={!isGenerating && ((!value.trim() && attachments.length === 0) || isSending)}
335
+ aria-label={isGenerating ? 'Stop generation' : (isSending ? 'Sending message...' : 'Send message')}
336
+ title={isGenerating ? 'Stop generation' : (isSending ? 'Sending message...' : 'Send message')}
216
337
  >
217
- {#if isSending}
338
+ {#if isGenerating}
339
+ <span class="block w-3 h-3 rounded-[2px] bg-white"></span>
340
+ {:else if isSending}
218
341
  <svg class="w-4 h-4 animate-spin" viewBox="0 0 24 24">
219
342
  <circle cx="12" cy="12" r="10" fill="none" stroke="white" stroke-width="2" opacity="0.3"/>
220
343
  <path d="M22 12a10 10 0 0 1-10 10" fill="none" stroke="white" stroke-width="2" stroke-linecap="round"/>
@@ -245,6 +368,12 @@
245
368
  {/if}
246
369
 
247
370
  <style>
371
+ .send-button-btn:focus,
372
+ .send-button-btn:focus-visible {
373
+ outline: none;
374
+ box-shadow: none;
375
+ }
376
+
248
377
  .input-lightbox-overlay {
249
378
  position: fixed;
250
379
  inset: 0;
@@ -5,10 +5,21 @@ interface Props {
5
5
  isRecording: boolean;
6
6
  isSending: boolean;
7
7
  isUploading: boolean;
8
+ isGenerating?: boolean;
9
+ /**
10
+ * When false, skip the in-input attachments tray (parent renders it in the chat footer).
11
+ * Drag-drop on `.chat-input-ct` still works.
12
+ */
13
+ showAttachmentsTray?: boolean;
8
14
  onSend: (content: string, attachments?: AttachmentInput[]) => void;
15
+ onStop?: () => void;
9
16
  onInputChange: (text: string) => void;
10
17
  onAttachmentsChange: (attachments: AttachmentInput[]) => void;
18
+ /** OS file drag-and-drop onto the input area (not attachment reorder). */
19
+ onFilesDropped?: (files: File[]) => void;
11
20
  placeholder?: string;
21
+ class?: string;
22
+ style?: string;
12
23
  }
13
24
  declare const ChatInput: import("svelte").Component<Props, {}, "">;
14
25
  type ChatInput = ReturnType<typeof ChatInput>;